[6339f467] | 1 | /* $Id$ */ |
---|
| 2 | |
---|
| 3 | /* libi2c Implementation */ |
---|
| 4 | |
---|
[6a03edd5] | 5 | /* |
---|
| 6 | * Authorship |
---|
| 7 | * ---------- |
---|
| 8 | * This software was created by |
---|
| 9 | * Till Straumann <strauman@slac.stanford.edu>, 2005, |
---|
| 10 | * Stanford Linear Accelerator Center, Stanford University. |
---|
| 11 | * |
---|
| 12 | * Acknowledgement of sponsorship |
---|
| 13 | * ------------------------------ |
---|
| 14 | * This software was produced by |
---|
| 15 | * the Stanford Linear Accelerator Center, Stanford University, |
---|
| 16 | * under Contract DE-AC03-76SFO0515 with the Department of Energy. |
---|
| 17 | * |
---|
| 18 | * Government disclaimer of liability |
---|
| 19 | * ---------------------------------- |
---|
| 20 | * Neither the United States nor the United States Department of Energy, |
---|
| 21 | * nor any of their employees, makes any warranty, express or implied, or |
---|
| 22 | * assumes any legal liability or responsibility for the accuracy, |
---|
| 23 | * completeness, or usefulness of any data, apparatus, product, or process |
---|
| 24 | * disclosed, or represents that its use would not infringe privately owned |
---|
| 25 | * rights. |
---|
| 26 | * |
---|
| 27 | * Stanford disclaimer of liability |
---|
| 28 | * -------------------------------- |
---|
| 29 | * Stanford University makes no representations or warranties, express or |
---|
| 30 | * implied, nor assumes any liability for the use of this software. |
---|
| 31 | * |
---|
| 32 | * Stanford disclaimer of copyright |
---|
| 33 | * -------------------------------- |
---|
| 34 | * Stanford University, owner of the copyright, hereby disclaims its |
---|
| 35 | * copyright and all other rights in this software. Hence, anyone may |
---|
| 36 | * freely use it for any purpose without restriction. |
---|
| 37 | * |
---|
| 38 | * Maintenance of notices |
---|
| 39 | * ---------------------- |
---|
| 40 | * In the interest of clarity regarding the origin and status of this |
---|
| 41 | * SLAC software, this and all the preceding Stanford University notices |
---|
| 42 | * are to remain affixed to any copy or derivative of this software made |
---|
| 43 | * or distributed by the recipient and are to be affixed to any copy of |
---|
| 44 | * software made or distributed by the recipient that contains a copy or |
---|
| 45 | * derivative of this software. |
---|
| 46 | * |
---|
| 47 | * ------------------ SLAC Software Notices, Set 4 OTT.002a, 2004 FEB 03 |
---|
| 48 | */ |
---|
[55a685b] | 49 | /* |
---|
| 50 | * adaptations to also handle SPI devices |
---|
| 51 | * by Thomas Doerfler, embedded brains GmbH, Puchheim, Germany |
---|
| 52 | */ |
---|
[199e748] | 53 | #if HAVE_CONFIG_H |
---|
| 54 | #include "config.h" |
---|
| 55 | #endif |
---|
| 56 | |
---|
[6339f467] | 57 | #include <string.h> |
---|
| 58 | #include <stdio.h> |
---|
| 59 | #include <stdlib.h> |
---|
| 60 | #include <errno.h> |
---|
| 61 | #include <assert.h> |
---|
[55a685b] | 62 | #include <stdarg.h> |
---|
[6339f467] | 63 | |
---|
| 64 | #include <rtems.h> |
---|
| 65 | #include <rtems/error.h> |
---|
[d2ff24c2] | 66 | #include <rtems/bspIo.h> |
---|
[6339f467] | 67 | #include <rtems/libio.h> |
---|
| 68 | |
---|
| 69 | #include <rtems/libi2c.h> |
---|
| 70 | |
---|
[5e4831f] | 71 | #define DRVNM "libi2c: " |
---|
[6339f467] | 72 | |
---|
| 73 | #define MAX_NO_BUSSES 8 /* Also limited by the macro building minor numbers */ |
---|
| 74 | #define MAX_NO_DRIVERS 16 /* Number of high level drivers we support */ |
---|
| 75 | |
---|
| 76 | #define MINOR2ADDR(minor) ((minor)&((1<<10)-1)) |
---|
| 77 | #define MINOR2BUS(minor) (((minor)>>10)&7) |
---|
| 78 | #define MINOR2DRV(minor) ((minor)>>13) |
---|
| 79 | |
---|
| 80 | /* Check the 'minor' argument, i.e., verify that |
---|
| 81 | * we have a driver connected |
---|
| 82 | */ |
---|
| 83 | #define DECL_CHECKED_BH(b, bh, m, s)\ |
---|
| 84 | unsigned b = MINOR2BUS(m); \ |
---|
| 85 | rtems_libi2c_bus_t *bh; \ |
---|
| 86 | if ( b >= MAX_NO_BUSSES || 0 == (bh=busses[b].bush) ) { \ |
---|
| 87 | return s RTEMS_INVALID_NUMBER; \ |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | #define DECL_CHECKED_DRV(d, b, m) \ |
---|
| 91 | unsigned d = MINOR2DRV(m); \ |
---|
| 92 | unsigned b = MINOR2BUS(m); \ |
---|
| 93 | if ( b >= MAX_NO_BUSSES || 0 == busses[b].bush \ |
---|
| 94 | || d > MAX_NO_DRIVERS || (d && 0 == drvs[d-1].drv )) {\ |
---|
| 95 | return RTEMS_INVALID_NUMBER; \ |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | #define DISPATCH(rval, entry, dflt) \ |
---|
| 99 | do { \ |
---|
[5e4831f] | 100 | const rtems_driver_address_table *ops = drvs[--drv].drv->ops; \ |
---|
[6339f467] | 101 | rval = ops->entry ? ops->entry(major,minor,arg) : dflt; \ |
---|
| 102 | } while (0) |
---|
| 103 | |
---|
| 104 | |
---|
| 105 | rtems_device_major_number rtems_libi2c_major; |
---|
| 106 | |
---|
[d2ff24c2] | 107 | static boolean is_initialized = FALSE; |
---|
| 108 | |
---|
[6339f467] | 109 | static struct i2cbus |
---|
| 110 | { |
---|
| 111 | rtems_libi2c_bus_t *bush; |
---|
| 112 | volatile rtems_id mutex; /* lock this across start -> stop */ |
---|
| 113 | volatile short waiting; |
---|
| 114 | volatile char started; |
---|
| 115 | char *name; |
---|
| 116 | } busses[MAX_NO_BUSSES] = { { 0 } }; |
---|
| 117 | |
---|
| 118 | static struct |
---|
| 119 | { |
---|
| 120 | rtems_libi2c_drv_t *drv; |
---|
| 121 | } drvs[MAX_NO_DRIVERS] = { { 0} }; |
---|
| 122 | |
---|
| 123 | static rtems_id libmutex = 0; |
---|
| 124 | |
---|
| 125 | #define LOCK(m) assert(!rtems_semaphore_obtain((m), RTEMS_WAIT, RTEMS_NO_TIMEOUT)) |
---|
| 126 | #define UNLOCK(m) rtems_semaphore_release((m)) |
---|
| 127 | |
---|
| 128 | #define LIBLOCK() LOCK(libmutex) |
---|
| 129 | #define LIBUNLOCK() UNLOCK(libmutex) |
---|
| 130 | |
---|
| 131 | #define MUTEX_ATTS \ |
---|
| 132 | ( RTEMS_PRIORITY \ |
---|
| 133 | | RTEMS_BINARY_SEMAPHORE \ |
---|
| 134 | |RTEMS_INHERIT_PRIORITY \ |
---|
| 135 | |RTEMS_NO_PRIORITY_CEILING \ |
---|
| 136 | |RTEMS_LOCAL ) |
---|
| 137 | |
---|
[d2ff24c2] | 138 | /* During early stages of life, stdio is not available */ |
---|
| 139 | |
---|
| 140 | static void |
---|
| 141 | safe_printf (const char *fmt, ...) |
---|
| 142 | { |
---|
| 143 | va_list ap; |
---|
| 144 | |
---|
| 145 | va_start(ap, fmt); |
---|
| 146 | if ( _System_state_Is_up( _System_state_Get() ) ) |
---|
| 147 | vfprintf( stderr, fmt, ap ); |
---|
| 148 | else |
---|
| 149 | vprintk( fmt, ap ); |
---|
| 150 | va_end(ap); |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | static rtems_status_code |
---|
| 154 | mutexCreate (rtems_name nm, rtems_id *pm) |
---|
[6339f467] | 155 | { |
---|
| 156 | rtems_status_code sc; |
---|
[d2ff24c2] | 157 | |
---|
[6339f467] | 158 | if (RTEMS_SUCCESSFUL != |
---|
[d2ff24c2] | 159 | (sc = rtems_semaphore_create (nm, 1, MUTEX_ATTS, 0, pm))) { |
---|
| 160 | if ( _System_state_Is_up( _System_state_Get() ) ) |
---|
[5e4831f] | 161 | rtems_error (sc, DRVNM "Unable to create mutex\n"); |
---|
[d2ff24c2] | 162 | else |
---|
[5e4831f] | 163 | printk (DRVNM "Unable to create mutex (status code %i)\n", sc); |
---|
[6339f467] | 164 | } |
---|
[d2ff24c2] | 165 | return sc; |
---|
[6339f467] | 166 | } |
---|
| 167 | |
---|
| 168 | /* Lock a bus avoiding to have a mutex, which is mostly |
---|
| 169 | * unused, hanging around all the time. We just create |
---|
| 170 | * and delete it on the fly... |
---|
| 171 | * |
---|
| 172 | * ASSUMES: argument checked by caller |
---|
| 173 | */ |
---|
| 174 | |
---|
| 175 | static void |
---|
| 176 | lock_bus (int busno) |
---|
| 177 | { |
---|
[d2ff24c2] | 178 | rtems_status_code sc; |
---|
| 179 | struct i2cbus *bus = &busses[busno]; |
---|
| 180 | |
---|
[6339f467] | 181 | LIBLOCK (); |
---|
| 182 | if (!bus->waiting) { |
---|
[d2ff24c2] | 183 | rtems_id m; |
---|
[6339f467] | 184 | /* nobody is holding the bus mutex - it's not there. Create it on the fly */ |
---|
[d2ff24c2] | 185 | sc = mutexCreate (rtems_build_name ('i', '2', 'c', '0' + busno), &m); |
---|
| 186 | if ( RTEMS_SUCCESSFUL != sc ) { |
---|
[6339f467] | 187 | LIBUNLOCK (); |
---|
[5e4831f] | 188 | rtems_panic (DRVNM "Unable to create bus lock"); |
---|
[d2ff24c2] | 189 | } else { |
---|
| 190 | bus->mutex = m; |
---|
| 191 | } |
---|
[6339f467] | 192 | } |
---|
| 193 | /* count number of people waiting on this bus; only the last one deletes the mutex */ |
---|
| 194 | bus->waiting++; |
---|
| 195 | LIBUNLOCK (); |
---|
| 196 | /* Now lock this bus */ |
---|
| 197 | LOCK (bus->mutex); |
---|
| 198 | } |
---|
| 199 | |
---|
| 200 | static void |
---|
| 201 | unlock_bus (int busno) |
---|
| 202 | { |
---|
| 203 | struct i2cbus *bus = &busses[busno]; |
---|
| 204 | LIBLOCK (); |
---|
| 205 | UNLOCK (bus->mutex); |
---|
| 206 | if (!--bus->waiting) { |
---|
| 207 | rtems_semaphore_delete (bus->mutex); |
---|
| 208 | } |
---|
| 209 | LIBUNLOCK (); |
---|
| 210 | } |
---|
| 211 | |
---|
| 212 | /* Note that 'arg' is always passed in as NULL */ |
---|
[d2ff24c2] | 213 | rtems_status_code |
---|
| 214 | rtems_i2c_init (rtems_device_major_number major, rtems_device_minor_number minor, |
---|
[6339f467] | 215 | void *arg) |
---|
| 216 | { |
---|
| 217 | rtems_status_code rval; |
---|
[d2ff24c2] | 218 | /* No busses or drivers can be registered at this point; |
---|
| 219 | * avoid the macro aborting with an error |
---|
[6339f467] | 220 | DECL_CHECKED_DRV (drv, busno, minor) |
---|
[d2ff24c2] | 221 | */ |
---|
[6339f467] | 222 | |
---|
[d2ff24c2] | 223 | rval = mutexCreate (rtems_build_name ('l', 'I', '2', 'C'), &libmutex); |
---|
| 224 | |
---|
| 225 | if ( RTEMS_SUCCESSFUL == rval ) { |
---|
| 226 | is_initialized = TRUE; |
---|
| 227 | rtems_libi2c_major = major; |
---|
[6339f467] | 228 | } else { |
---|
[d2ff24c2] | 229 | libmutex = 0; |
---|
[6339f467] | 230 | } |
---|
| 231 | return rval; |
---|
| 232 | } |
---|
| 233 | |
---|
[d2ff24c2] | 234 | rtems_status_code |
---|
| 235 | rtems_i2c_open (rtems_device_major_number major, rtems_device_minor_number minor, |
---|
[6339f467] | 236 | void *arg) |
---|
| 237 | { |
---|
| 238 | rtems_status_code rval; |
---|
| 239 | DECL_CHECKED_DRV (drv, busno, minor) |
---|
| 240 | |
---|
| 241 | if (0 == drv) { |
---|
| 242 | rval = RTEMS_SUCCESSFUL; |
---|
| 243 | } else { |
---|
| 244 | DISPATCH (rval, open_entry, RTEMS_SUCCESSFUL); |
---|
| 245 | } |
---|
| 246 | return rval; |
---|
| 247 | } |
---|
| 248 | |
---|
[d2ff24c2] | 249 | rtems_status_code |
---|
| 250 | rtems_i2c_close (rtems_device_major_number major, rtems_device_minor_number minor, |
---|
[6339f467] | 251 | void *arg) |
---|
| 252 | { |
---|
| 253 | rtems_status_code rval; |
---|
| 254 | DECL_CHECKED_DRV (drv, busno, minor) |
---|
| 255 | |
---|
| 256 | if (0 == drv) { |
---|
| 257 | rval = RTEMS_SUCCESSFUL; |
---|
| 258 | } else { |
---|
| 259 | DISPATCH (rval, close_entry, RTEMS_SUCCESSFUL); |
---|
| 260 | } |
---|
| 261 | return rval; |
---|
| 262 | } |
---|
| 263 | |
---|
[d2ff24c2] | 264 | rtems_status_code |
---|
| 265 | rtems_i2c_read (rtems_device_major_number major, rtems_device_minor_number minor, |
---|
[6339f467] | 266 | void *arg) |
---|
| 267 | { |
---|
| 268 | int rval; /* int so we can check for negative value */ |
---|
| 269 | rtems_libio_rw_args_t *rwargs = arg; |
---|
| 270 | DECL_CHECKED_DRV (drv, busno, minor) |
---|
| 271 | |
---|
| 272 | if (0 == rwargs->count) { |
---|
| 273 | rwargs->bytes_moved = 0; |
---|
| 274 | return RTEMS_SUCCESSFUL; |
---|
| 275 | } |
---|
| 276 | |
---|
| 277 | if (0 == drv) { |
---|
| 278 | rval = |
---|
| 279 | rtems_libi2c_start_read_bytes (minor, (unsigned char *) rwargs->buffer, |
---|
| 280 | rwargs->count); |
---|
| 281 | if (rval >= 0) { |
---|
| 282 | rwargs->bytes_moved = rval; |
---|
| 283 | rtems_libi2c_send_stop (minor); |
---|
| 284 | rval = RTEMS_SUCCESSFUL; |
---|
| 285 | } else { |
---|
| 286 | rval = -rval; |
---|
| 287 | } |
---|
| 288 | } else { |
---|
| 289 | DISPATCH (rval, read_entry, RTEMS_NOT_IMPLEMENTED); |
---|
| 290 | } |
---|
| 291 | return rval; |
---|
| 292 | } |
---|
| 293 | |
---|
[d2ff24c2] | 294 | rtems_status_code |
---|
| 295 | rtems_i2c_write (rtems_device_major_number major, rtems_device_minor_number minor, |
---|
[6339f467] | 296 | void *arg) |
---|
| 297 | { |
---|
| 298 | int rval; /* int so we can check for negative value */ |
---|
| 299 | rtems_libio_rw_args_t *rwargs = arg; |
---|
| 300 | DECL_CHECKED_DRV (drv, busno, minor) |
---|
| 301 | |
---|
| 302 | if (0 == rwargs->count) { |
---|
| 303 | rwargs->bytes_moved = 0; |
---|
| 304 | return RTEMS_SUCCESSFUL; |
---|
| 305 | } |
---|
| 306 | |
---|
| 307 | if (0 == drv) { |
---|
| 308 | rval = |
---|
| 309 | rtems_libi2c_start_write_bytes (minor, (unsigned char *) rwargs->buffer, |
---|
| 310 | rwargs->count); |
---|
| 311 | if (rval >= 0) { |
---|
| 312 | rwargs->bytes_moved = rval; |
---|
| 313 | rtems_libi2c_send_stop (minor); |
---|
| 314 | rval = RTEMS_SUCCESSFUL; |
---|
| 315 | } else { |
---|
| 316 | rval = -rval; |
---|
| 317 | } |
---|
| 318 | } else { |
---|
| 319 | DISPATCH (rval, write_entry, RTEMS_NOT_IMPLEMENTED); |
---|
| 320 | } |
---|
| 321 | return rval; |
---|
| 322 | } |
---|
| 323 | |
---|
[d2ff24c2] | 324 | rtems_status_code |
---|
| 325 | rtems_i2c_ioctl (rtems_device_major_number major, rtems_device_minor_number minor, |
---|
[6339f467] | 326 | void *arg) |
---|
| 327 | { |
---|
| 328 | rtems_status_code rval; |
---|
| 329 | DECL_CHECKED_DRV (drv, busno, minor) |
---|
| 330 | |
---|
| 331 | if (0 == drv) { |
---|
| 332 | rval = RTEMS_NOT_IMPLEMENTED; |
---|
| 333 | } else { |
---|
| 334 | DISPATCH (rval, control_entry, RTEMS_NOT_IMPLEMENTED); |
---|
| 335 | } |
---|
| 336 | return rval; |
---|
| 337 | } |
---|
| 338 | |
---|
| 339 | |
---|
| 340 | /* Our ops just dispatch to the registered drivers */ |
---|
[5e4831f] | 341 | const rtems_driver_address_table rtems_libi2c_io_ops = { |
---|
[d2ff24c2] | 342 | initialization_entry: rtems_i2c_init, |
---|
| 343 | open_entry: rtems_i2c_open, |
---|
| 344 | close_entry: rtems_i2c_close, |
---|
| 345 | read_entry: rtems_i2c_read, |
---|
| 346 | write_entry: rtems_i2c_write, |
---|
| 347 | control_entry: rtems_i2c_ioctl, |
---|
[6339f467] | 348 | }; |
---|
| 349 | |
---|
| 350 | int |
---|
| 351 | rtems_libi2c_initialize () |
---|
| 352 | { |
---|
| 353 | rtems_status_code sc; |
---|
| 354 | |
---|
[55a685b] | 355 | if (is_initialized) { |
---|
| 356 | /* |
---|
| 357 | * already called before? then skip this step |
---|
| 358 | */ |
---|
| 359 | return 0; |
---|
| 360 | } |
---|
| 361 | |
---|
[d2ff24c2] | 362 | /* rtems_io_register_driver does NOT currently check nor report back |
---|
| 363 | * the return code of the 'init' operation, so we cannot |
---|
| 364 | * rely on return code since it may seem OK even if the driver 'init; |
---|
| 365 | * op failed. |
---|
| 366 | * Let 'init' handle 'is_initialized'... |
---|
| 367 | */ |
---|
| 368 | sc = rtems_io_register_driver (0, &rtems_libi2c_io_ops, &rtems_libi2c_major); |
---|
[c57316a] | 369 | if (RTEMS_SUCCESSFUL != sc) { |
---|
[d2ff24c2] | 370 | safe_printf( |
---|
[5e4831f] | 371 | DRVNM "Claiming driver slot failed (rtems status code %i)\n", |
---|
[6339f467] | 372 | sc); |
---|
[d2ff24c2] | 373 | if ( libmutex ) |
---|
| 374 | rtems_semaphore_delete (libmutex); |
---|
[6339f467] | 375 | libmutex = 0; |
---|
[d2ff24c2] | 376 | is_initialized = FALSE; |
---|
[6339f467] | 377 | return -1; |
---|
| 378 | } |
---|
| 379 | |
---|
| 380 | return 0; |
---|
| 381 | } |
---|
| 382 | |
---|
| 383 | int |
---|
[5e4831f] | 384 | rtems_libi2c_register_bus (const char *name, rtems_libi2c_bus_t * bus) |
---|
[6339f467] | 385 | { |
---|
| 386 | int i; |
---|
| 387 | rtems_status_code err; |
---|
| 388 | char *nmcpy = malloc (name ? strlen (name) + 1 : 20); |
---|
| 389 | char tmp, *chpt; |
---|
| 390 | struct stat sbuf; |
---|
| 391 | |
---|
| 392 | strcpy (nmcpy, name ? name : "/dev/i2c"); |
---|
| 393 | |
---|
| 394 | |
---|
| 395 | /* check */ |
---|
| 396 | if ('/' != *nmcpy) { |
---|
[5e4831f] | 397 | safe_printf ( DRVNM "Bad name: must be an absolute path starting with '/'\n"); |
---|
[6339f467] | 398 | return -RTEMS_INVALID_NAME; |
---|
| 399 | } |
---|
| 400 | /* file must not exist */ |
---|
| 401 | if (!stat (nmcpy, &sbuf)) { |
---|
[5e4831f] | 402 | safe_printf ( DRVNM "Bad name: file exists already\n"); |
---|
[6339f467] | 403 | return -RTEMS_INVALID_NAME; |
---|
| 404 | } |
---|
| 405 | |
---|
| 406 | /* we already verified that there is at least one '/' */ |
---|
| 407 | chpt = strrchr (nmcpy, '/') + 1; |
---|
| 408 | tmp = *chpt; |
---|
| 409 | *chpt = 0; |
---|
| 410 | i = stat (nmcpy, &sbuf); |
---|
| 411 | *chpt = tmp; |
---|
| 412 | if (i) { |
---|
[d2ff24c2] | 413 | safe_printf ( DRVNM "Get %s status failed: %s\n", |
---|
| 414 | nmcpy, strerror(errno)); |
---|
[6339f467] | 415 | return -RTEMS_INVALID_NAME; |
---|
| 416 | } |
---|
| 417 | /* should be a directory since name terminates in '/' */ |
---|
| 418 | |
---|
| 419 | |
---|
| 420 | if (!libmutex) { |
---|
[5e4831f] | 421 | safe_printf ( DRVNM "Library not initialized\n"); |
---|
[6339f467] | 422 | return -RTEMS_NOT_DEFINED; |
---|
| 423 | } |
---|
| 424 | |
---|
[5e4831f] | 425 | if (bus == NULL || bus->size < sizeof (*bus)) { |
---|
| 426 | safe_printf ( DRVNM "No bus-ops or size too small -- misconfiguration?\n"); |
---|
[6339f467] | 427 | return -RTEMS_NOT_CONFIGURED; |
---|
| 428 | } |
---|
| 429 | |
---|
| 430 | LIBLOCK (); |
---|
| 431 | for (i = 0; i < MAX_NO_BUSSES; i++) { |
---|
| 432 | if (!busses[i].bush) { |
---|
| 433 | /* found a free slot */ |
---|
| 434 | busses[i].bush = bus; |
---|
| 435 | busses[i].mutex = 0; |
---|
| 436 | busses[i].waiting = 0; |
---|
| 437 | busses[i].started = 0; |
---|
| 438 | |
---|
| 439 | if (!name) |
---|
| 440 | sprintf (nmcpy + strlen (nmcpy), "%i", i); |
---|
| 441 | |
---|
| 442 | if ((err = busses[i].bush->ops->init (busses[i].bush))) { |
---|
| 443 | /* initialization failed */ |
---|
| 444 | i = -err; |
---|
| 445 | } else { |
---|
| 446 | busses[i].name = nmcpy;; |
---|
| 447 | nmcpy = 0; |
---|
| 448 | } |
---|
| 449 | |
---|
| 450 | break; |
---|
| 451 | } |
---|
| 452 | } |
---|
| 453 | LIBUNLOCK (); |
---|
| 454 | |
---|
| 455 | if (i >= MAX_NO_BUSSES) { |
---|
| 456 | i = -RTEMS_TOO_MANY; |
---|
| 457 | } |
---|
| 458 | |
---|
| 459 | free (nmcpy); |
---|
| 460 | |
---|
| 461 | return i; |
---|
| 462 | } |
---|
| 463 | |
---|
| 464 | static int |
---|
| 465 | not_started (int busno) |
---|
| 466 | { |
---|
| 467 | int rval; |
---|
| 468 | lock_bus (busno); |
---|
| 469 | rval = !busses[busno].started; |
---|
| 470 | unlock_bus (busno); |
---|
| 471 | return rval; |
---|
| 472 | } |
---|
| 473 | |
---|
| 474 | rtems_status_code |
---|
[55a685b] | 475 | rtems_libi2c_send_start (rtems_device_minor_number minor) |
---|
[6339f467] | 476 | { |
---|
| 477 | int rval; |
---|
| 478 | DECL_CHECKED_BH (busno, bush, minor, +) |
---|
| 479 | |
---|
| 480 | lock_bus (busno); |
---|
| 481 | rval = bush->ops->send_start (bush); |
---|
| 482 | |
---|
| 483 | /* if this failed or is not the first start, unlock */ |
---|
| 484 | if (rval || busses[busno].started) { |
---|
| 485 | /* HMM - what to do if the 1st start failed ? |
---|
| 486 | * try to reset... |
---|
| 487 | */ |
---|
| 488 | if (!busses[busno].started) { |
---|
| 489 | /* just in case the bus driver fiddles with errno */ |
---|
| 490 | int errno_saved = errno; |
---|
| 491 | bush->ops->init (bush); |
---|
| 492 | errno = errno_saved; |
---|
| 493 | } else if (rval) { |
---|
| 494 | /* failed restart */ |
---|
| 495 | rtems_libi2c_send_stop (minor); |
---|
| 496 | } |
---|
| 497 | unlock_bus (busno); |
---|
| 498 | } else { |
---|
| 499 | /* successful 1st start; keep bus locked until stop is sent */ |
---|
| 500 | busses[busno].started = 1; |
---|
| 501 | } |
---|
| 502 | return rval; |
---|
| 503 | } |
---|
| 504 | |
---|
| 505 | rtems_status_code |
---|
[55a685b] | 506 | rtems_libi2c_send_stop (rtems_device_minor_number minor) |
---|
[6339f467] | 507 | { |
---|
| 508 | rtems_status_code rval; |
---|
| 509 | DECL_CHECKED_BH (busno, bush, minor, +) |
---|
| 510 | |
---|
| 511 | if (not_started (busno)) |
---|
| 512 | return RTEMS_NOT_OWNER_OF_RESOURCE; |
---|
| 513 | |
---|
| 514 | rval = bush->ops->send_stop (bush); |
---|
| 515 | |
---|
| 516 | busses[busno].started = 0; |
---|
| 517 | |
---|
| 518 | unlock_bus (busno); |
---|
| 519 | return rval; |
---|
| 520 | } |
---|
| 521 | |
---|
| 522 | rtems_status_code |
---|
[55a685b] | 523 | rtems_libi2c_send_addr (rtems_device_minor_number minor, int rw) |
---|
[6339f467] | 524 | { |
---|
| 525 | rtems_status_code sc; |
---|
| 526 | DECL_CHECKED_BH (busno, bush, minor, +) |
---|
| 527 | |
---|
| 528 | if (not_started (busno)) |
---|
| 529 | return RTEMS_NOT_OWNER_OF_RESOURCE; |
---|
| 530 | |
---|
| 531 | sc = bush->ops->send_addr (bush, MINOR2ADDR (minor), rw); |
---|
| 532 | if (RTEMS_SUCCESSFUL != sc) |
---|
| 533 | rtems_libi2c_send_stop (minor); |
---|
| 534 | return sc; |
---|
| 535 | } |
---|
| 536 | |
---|
| 537 | int |
---|
[55a685b] | 538 | rtems_libi2c_read_bytes (rtems_device_minor_number minor, |
---|
| 539 | unsigned char *bytes, |
---|
| 540 | int nbytes) |
---|
[6339f467] | 541 | { |
---|
| 542 | int sc; |
---|
| 543 | DECL_CHECKED_BH (busno, bush, minor, -) |
---|
| 544 | |
---|
| 545 | if (not_started (busno)) |
---|
| 546 | return -RTEMS_NOT_OWNER_OF_RESOURCE; |
---|
| 547 | |
---|
| 548 | sc = bush->ops->read_bytes (bush, bytes, nbytes); |
---|
| 549 | if (sc < 0) |
---|
| 550 | rtems_libi2c_send_stop (minor); |
---|
| 551 | return sc; |
---|
| 552 | } |
---|
| 553 | |
---|
| 554 | int |
---|
[55a685b] | 555 | rtems_libi2c_write_bytes (rtems_device_minor_number minor, |
---|
[9ab94a1f] | 556 | const unsigned char *bytes, |
---|
[55a685b] | 557 | int nbytes) |
---|
[6339f467] | 558 | { |
---|
| 559 | int sc; |
---|
| 560 | DECL_CHECKED_BH (busno, bush, minor, -) |
---|
| 561 | |
---|
| 562 | if (not_started (busno)) |
---|
| 563 | return -RTEMS_NOT_OWNER_OF_RESOURCE; |
---|
| 564 | |
---|
[9ab94a1f] | 565 | sc = bush->ops->write_bytes (bush, (unsigned char *)bytes, nbytes); |
---|
[6339f467] | 566 | if (sc < 0) |
---|
| 567 | rtems_libi2c_send_stop (minor); |
---|
| 568 | return sc; |
---|
| 569 | } |
---|
| 570 | |
---|
[55a685b] | 571 | int |
---|
| 572 | rtems_libi2c_ioctl (rtems_device_minor_number minor, |
---|
| 573 | int cmd, |
---|
| 574 | ...) |
---|
| 575 | { |
---|
| 576 | va_list ap; |
---|
| 577 | int sc = 0; |
---|
| 578 | void *args; |
---|
[5e4831f] | 579 | rtems_boolean is_started = FALSE; |
---|
[55a685b] | 580 | DECL_CHECKED_BH (busno, bush, minor, -) |
---|
[42bf1b9] | 581 | |
---|
[55a685b] | 582 | va_start(ap, cmd); |
---|
| 583 | args = va_arg(ap, void *); |
---|
| 584 | |
---|
| 585 | switch(cmd) { |
---|
| 586 | /* |
---|
| 587 | * add ioctls defined for this level here: |
---|
| 588 | */ |
---|
| 589 | |
---|
[42bf1b9] | 590 | case RTEMS_LIBI2C_IOCTL_GET_DRV_T: |
---|
| 591 | /* |
---|
| 592 | * query driver table entry |
---|
| 593 | */ |
---|
| 594 | *(rtems_libi2c_drv_t **)args = (drvs[MINOR2DRV(minor)-1].drv); |
---|
| 595 | break; |
---|
| 596 | |
---|
[55a685b] | 597 | case RTEMS_LIBI2C_IOCTL_START_TFM_READ_WRITE: |
---|
[42bf1b9] | 598 | if (not_started (busno)) |
---|
| 599 | return -RTEMS_NOT_OWNER_OF_RESOURCE; |
---|
| 600 | |
---|
[55a685b] | 601 | /* |
---|
| 602 | * address device, then set transfer mode and perform read_write transfer |
---|
| 603 | */ |
---|
| 604 | /* |
---|
| 605 | * perform start/address |
---|
| 606 | */ |
---|
| 607 | if (sc == 0) { |
---|
| 608 | sc = rtems_libi2c_send_start (minor); |
---|
[42bf1b9] | 609 | is_started = (sc == 0); |
---|
[55a685b] | 610 | } |
---|
| 611 | /* |
---|
| 612 | * set tfr mode |
---|
| 613 | */ |
---|
| 614 | if (sc == 0) { |
---|
| 615 | sc = bush->ops->ioctl |
---|
| 616 | (bush, |
---|
| 617 | RTEMS_LIBI2C_IOCTL_SET_TFRMODE, |
---|
| 618 | &((rtems_libi2c_tfm_read_write_t *)args)->tfr_mode); |
---|
| 619 | } |
---|
| 620 | /* |
---|
| 621 | * perform read_write |
---|
| 622 | */ |
---|
| 623 | if (sc == 0) { |
---|
| 624 | sc = bush->ops->ioctl |
---|
| 625 | (bush, |
---|
| 626 | RTEMS_LIBI2C_IOCTL_READ_WRITE, |
---|
| 627 | &((rtems_libi2c_tfm_read_write_t *)args)->rd_wr); |
---|
| 628 | } |
---|
[42bf1b9] | 629 | if ((sc < 0) && (is_started)) { |
---|
| 630 | rtems_libi2c_send_stop (minor); |
---|
| 631 | } |
---|
[55a685b] | 632 | break; |
---|
| 633 | default: |
---|
| 634 | sc = bush->ops->ioctl (bush, cmd, args); |
---|
| 635 | break; |
---|
| 636 | } |
---|
[42bf1b9] | 637 | return sc; |
---|
[55a685b] | 638 | } |
---|
| 639 | |
---|
[6339f467] | 640 | static int |
---|
[55a685b] | 641 | do_s_rw (rtems_device_minor_number minor, |
---|
| 642 | unsigned char *bytes, |
---|
| 643 | int nbytes, |
---|
| 644 | int rw) |
---|
[6339f467] | 645 | { |
---|
| 646 | rtems_status_code sc; |
---|
| 647 | rtems_libi2c_bus_t *bush; |
---|
| 648 | |
---|
| 649 | if ((sc = rtems_libi2c_send_start (minor))) |
---|
| 650 | return -sc; |
---|
| 651 | |
---|
| 652 | /* at this point, we hold the bus and are sure the minor number is valid */ |
---|
| 653 | bush = busses[MINOR2BUS (minor)].bush; |
---|
| 654 | |
---|
| 655 | if ((sc = bush->ops->send_addr (bush, MINOR2ADDR (minor), rw))) { |
---|
| 656 | rtems_libi2c_send_stop (minor); |
---|
| 657 | return -sc; |
---|
| 658 | } |
---|
| 659 | |
---|
| 660 | if (rw) |
---|
| 661 | sc = bush->ops->read_bytes (bush, bytes, nbytes); |
---|
| 662 | else |
---|
| 663 | sc = bush->ops->write_bytes (bush, bytes, nbytes); |
---|
| 664 | |
---|
| 665 | if (sc < 0) { |
---|
| 666 | rtems_libi2c_send_stop (minor); |
---|
| 667 | } |
---|
| 668 | return sc; |
---|
| 669 | } |
---|
| 670 | |
---|
| 671 | int |
---|
[55a685b] | 672 | rtems_libi2c_start_read_bytes (rtems_device_minor_number minor, |
---|
| 673 | unsigned char *bytes, |
---|
[6339f467] | 674 | int nbytes) |
---|
| 675 | { |
---|
| 676 | return do_s_rw (minor, bytes, nbytes, 1); |
---|
| 677 | } |
---|
| 678 | |
---|
| 679 | int |
---|
[55a685b] | 680 | rtems_libi2c_start_write_bytes (rtems_device_minor_number minor, |
---|
[9ab94a1f] | 681 | const unsigned char *bytes, |
---|
[6339f467] | 682 | int nbytes) |
---|
| 683 | { |
---|
[9ab94a1f] | 684 | return do_s_rw (minor, (unsigned char *)bytes, nbytes, 0); |
---|
[6339f467] | 685 | } |
---|
| 686 | |
---|
| 687 | int |
---|
[5e4831f] | 688 | rtems_libi2c_register_drv (const char *name, rtems_libi2c_drv_t * drvtbl, |
---|
[6339f467] | 689 | unsigned busno, unsigned i2caddr) |
---|
| 690 | { |
---|
| 691 | int i; |
---|
| 692 | rtems_status_code err; |
---|
| 693 | rtems_device_minor_number minor; |
---|
| 694 | |
---|
| 695 | if (!libmutex) { |
---|
[5e4831f] | 696 | safe_printf ( DRVNM "Library not initialized\n"); |
---|
[6339f467] | 697 | return -RTEMS_NOT_DEFINED; |
---|
| 698 | } |
---|
| 699 | |
---|
| 700 | if (name && strchr (name, '/')) { |
---|
[d2ff24c2] | 701 | safe_printf ( DRVNM "Invalid name: '%s' -- must not contain '/'\n", name); |
---|
[6339f467] | 702 | return -RTEMS_INVALID_NAME; |
---|
| 703 | } |
---|
| 704 | |
---|
| 705 | if (busno >= MAX_NO_BUSSES || !busses[busno].bush || i2caddr >= 1 << 10) { |
---|
| 706 | errno = EINVAL; |
---|
| 707 | return -RTEMS_INVALID_NUMBER; |
---|
| 708 | } |
---|
| 709 | |
---|
[5e4831f] | 710 | if (drvtbl == NULL || drvtbl->size < sizeof (*drvtbl)) { |
---|
| 711 | safe_printf ( DRVNM "No driver table or size too small -- misconfiguration?\n"); |
---|
[6339f467] | 712 | return -RTEMS_NOT_CONFIGURED; |
---|
| 713 | } |
---|
| 714 | |
---|
| 715 | /* allocate slot */ |
---|
| 716 | LIBLOCK (); |
---|
| 717 | for (i = 0; i < MAX_NO_DRIVERS; i++) { |
---|
| 718 | /* driver # 0 is special, it is the built-in raw driver */ |
---|
| 719 | if (!drvs[i].drv) { |
---|
| 720 | char *str; |
---|
| 721 | dev_t dev; |
---|
[199e748] | 722 | uint32_t mode; |
---|
[6339f467] | 723 | |
---|
| 724 | /* found a free slot; encode slot + 1 ! */ |
---|
| 725 | minor = ((i + 1) << 13) | RTEMS_LIBI2C_MAKE_MINOR (busno, i2caddr); |
---|
| 726 | |
---|
| 727 | if (name) { |
---|
| 728 | str = malloc (strlen (busses[busno].name) + strlen (name) + 2); |
---|
| 729 | sprintf (str, "%s.%s", busses[busno].name, name); |
---|
| 730 | |
---|
| 731 | dev = rtems_filesystem_make_dev_t (rtems_libi2c_major, minor); |
---|
| 732 | |
---|
| 733 | mode = 0111 | S_IFCHR; |
---|
| 734 | if (drvtbl->ops->read_entry) |
---|
| 735 | mode |= 0444; |
---|
| 736 | if (drvtbl->ops->write_entry) |
---|
| 737 | mode |= 0222; |
---|
| 738 | |
---|
| 739 | /* note that 'umask' is applied to 'mode' */ |
---|
| 740 | if (mknod (str, mode, dev)) { |
---|
[d2ff24c2] | 741 | safe_printf( DRVNM |
---|
[6339f467] | 742 | "Creating device node failed: %s; you can try to do it manually...\n", |
---|
| 743 | strerror (errno)); |
---|
| 744 | } |
---|
| 745 | |
---|
| 746 | free (str); |
---|
| 747 | } |
---|
| 748 | |
---|
| 749 | drvs[i].drv = drvtbl; |
---|
| 750 | |
---|
| 751 | if (drvtbl->ops->initialization_entry) |
---|
| 752 | err = |
---|
| 753 | drvs[i].drv->ops->initialization_entry (rtems_libi2c_major, minor, |
---|
| 754 | 0); |
---|
| 755 | else |
---|
| 756 | err = RTEMS_SUCCESSFUL; |
---|
| 757 | |
---|
| 758 | LIBUNLOCK (); |
---|
| 759 | return err ? -err : minor; |
---|
| 760 | } |
---|
| 761 | } |
---|
| 762 | LIBUNLOCK (); |
---|
| 763 | return -RTEMS_TOO_MANY; |
---|
| 764 | } |
---|