source: rtems/cpukit/libi2c/libi2c.c @ c85ab23

4.104.11
Last change on this file since c85ab23 was c85ab23, checked in by Joel Sherrill <joel.sherrill@…>, on Aug 5, 2009 at 6:17:12 PM

2009-08-05 Sebastian Huber <sebastian.huber@…>

  • libcsupport/include/rtems/libio_.h, libcsupport/src/fs_null_handlers.c: Null handlers are now const.
  • libi2c/libi2c.c, libi2c/libi2c.h: Documentation. Do not create semaphores on the fly.
  • cpukit/libblock/src/bdpart.c: Fixed format specifier.
  • cpukit/libblock/include/rtems/bdbuf.h, rtems/include/rtems.h, rtems/include/rtems/rtems/asr.h, rtems/include/rtems/rtems/attr.h, rtems/include/rtems/rtems/barrier.h, rtems/include/rtems/rtems/barriermp.h, rtems/include/rtems/rtems/cache.h, rtems/include/rtems/rtems/clock.h, rtems/include/rtems/rtems/config.h, rtems/include/rtems/rtems/dpmem.h, rtems/include/rtems/rtems/event.h, rtems/include/rtems/rtems/eventmp.h, rtems/include/rtems/rtems/eventset.h, rtems/include/rtems/rtems/intr.h, rtems/include/rtems/rtems/message.h, rtems/include/rtems/rtems/modes.h, rtems/include/rtems/rtems/mp.h, rtems/include/rtems/rtems/msgmp.h, rtems/include/rtems/rtems/object.h, rtems/include/rtems/rtems/part.h, rtems/include/rtems/rtems/partmp.h, rtems/include/rtems/rtems/ratemon.h, rtems/include/rtems/rtems/region.h, rtems/include/rtems/rtems/regionmp.h, rtems/include/rtems/rtems/rtemsapi.h, rtems/include/rtems/rtems/sem.h, rtems/include/rtems/rtems/semmp.h, rtems/include/rtems/rtems/signal.h, rtems/include/rtems/rtems/signalmp.h, rtems/include/rtems/rtems/status.h, rtems/include/rtems/rtems/support.h, rtems/include/rtems/rtems/taskmp.h, rtems/include/rtems/rtems/tasks.h, rtems/include/rtems/rtems/timer.h, rtems/include/rtems/rtems/types.h, rtems/inline/rtems/rtems/support.inl: Documentation.
  • include/rtems/irq-extension.h: Documentation. Added API for interrupt servers.
  • Property mode set to 100644
File size: 18.6 KB
Line 
1/* $Id$ */
2
3/* libi2c Implementation */
4
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 */ 
49/*
50 * adaptations to also handle SPI devices
51 * by Thomas Doerfler, embedded brains GmbH, Puchheim, Germany
52 */
53#if HAVE_CONFIG_H
54#include "config.h"
55#endif
56
57#include <string.h>
58#include <stdio.h>
59#include <stdlib.h>
60#include <errno.h>
61#include <assert.h>
62#include <stdarg.h>
63
64#include <rtems.h>
65#include <rtems/error.h>
66#include <rtems/bspIo.h>
67#include <rtems/libio.h>
68
69#include <rtems/libi2c.h>
70
71#define DRVNM "libi2c: "
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 {                            \
100                const rtems_driver_address_table *ops = drvs[--drv].drv->ops;   \
101                rval = ops->entry ? ops->entry(major,minor,arg) : dflt; \
102        } while (0)
103
104
105rtems_device_major_number rtems_libi2c_major;
106
107static bool is_initialized = false;
108
109static struct i2cbus
110{
111  rtems_libi2c_bus_t *bush;
112  volatile rtems_id mutex;      /* lock this across start -> stop */
113  volatile bool started;
114  char *name;
115} busses[MAX_NO_BUSSES] = { { 0, 0, 0, 0, 0 } };
116
117static struct
118{
119  rtems_libi2c_drv_t *drv;
120} drvs[MAX_NO_DRIVERS] = { { 0 } };
121
122static rtems_id libmutex = RTEMS_ID_NONE;
123
124#define LOCK(m)         assert(!rtems_semaphore_obtain((m), RTEMS_WAIT, RTEMS_NO_TIMEOUT))
125#define UNLOCK(m)       rtems_semaphore_release((m))
126
127#define LIBLOCK()       LOCK(libmutex)
128#define LIBUNLOCK()     UNLOCK(libmutex)
129
130#define MUTEX_ATTS      \
131        (  RTEMS_PRIORITY                       \
132         | RTEMS_BINARY_SEMAPHORE       \
133         |RTEMS_INHERIT_PRIORITY        \
134         |RTEMS_NO_PRIORITY_CEILING     \
135         |RTEMS_LOCAL )
136
137/* During early stages of life, stdio is not available */
138
139static void
140safe_printf (const char *fmt, ...)
141{
142va_list ap;
143
144        va_start(ap, fmt);
145        if ( _System_state_Is_up( _System_state_Get() ) )
146                vfprintf( stderr, fmt, ap );
147        else
148                vprintk( fmt, ap );
149        va_end(ap);
150}
151
152static rtems_status_code
153mutexCreate (rtems_name nm, rtems_id *pm)
154{
155  rtems_status_code sc;
156
157  if (RTEMS_SUCCESSFUL !=
158      (sc = rtems_semaphore_create (nm, 1, MUTEX_ATTS, 0, pm))) {
159        if ( _System_state_Is_up( _System_state_Get() ) )
160        rtems_error (sc, DRVNM "Unable to create mutex\n");
161        else
162                printk (DRVNM "Unable to create mutex (status code %i)\n", sc);
163  }
164  return sc;
165}
166
167/* Lock a bus avoiding to have a mutex, which is mostly
168 * unused, hanging around all the time. We just create
169 * and delete it on the fly...
170 *
171 * ASSUMES: argument checked by caller
172 */
173
174static void
175lock_bus (int busno)
176{
177  struct i2cbus *bus = &busses[busno];
178
179  if (bus->mutex == RTEMS_ID_NONE) {
180    /*
181     * Nobody is holding the bus mutex - it's not there.  Create it on the fly.
182     */
183    LIBLOCK ();
184    if (bus->mutex == RTEMS_ID_NONE) {
185      rtems_id m = RTEMS_ID_NONE;
186      rtems_status_code sc = mutexCreate (
187        rtems_build_name ('i', '2', 'c', '0' + busno),
188        &m
189      );
190      if (sc != RTEMS_SUCCESSFUL) {
191        LIBUNLOCK ();
192        rtems_panic (DRVNM "Unable to create bus lock");
193        return;
194      }
195      bus->mutex = m;
196    }
197    LIBUNLOCK ();
198  }
199
200  /* Now lock this bus */
201  LOCK (bus->mutex);
202}
203
204static void
205unlock_bus (int busno)
206{
207  struct i2cbus *bus = &busses[busno];
208  UNLOCK (bus->mutex);
209}
210
211/* Note that 'arg' is always passed in as NULL */
212rtems_status_code
213rtems_i2c_init (rtems_device_major_number major, rtems_device_minor_number minor,
214          void *arg)
215{
216  rtems_status_code rval;
217  /* No busses or drivers can be registered at this point;
218   * avoid the macro aborting with an error
219  DECL_CHECKED_DRV (drv, busno, minor)
220   */
221
222  rval = mutexCreate (rtems_build_name ('l', 'I', '2', 'C'), &libmutex);
223
224  if ( RTEMS_SUCCESSFUL == rval ) {
225        is_initialized     = true;
226        rtems_libi2c_major = major;
227  } else {
228        libmutex = RTEMS_ID_NONE;
229  }
230  return rval;
231}
232
233rtems_status_code
234rtems_i2c_open (rtems_device_major_number major, rtems_device_minor_number minor,
235          void *arg)
236{
237  rtems_status_code rval;
238  DECL_CHECKED_DRV (drv, busno, minor)
239
240    if (0 == drv) {
241    rval = RTEMS_SUCCESSFUL;
242  } else {
243    DISPATCH (rval, open_entry, RTEMS_SUCCESSFUL);
244  }
245  return rval;
246}
247
248rtems_status_code
249rtems_i2c_close (rtems_device_major_number major, rtems_device_minor_number minor,
250           void *arg)
251{
252  rtems_status_code rval;
253  DECL_CHECKED_DRV (drv, busno, minor)
254
255    if (0 == drv) {
256    rval = RTEMS_SUCCESSFUL;
257  } else {
258    DISPATCH (rval, close_entry, RTEMS_SUCCESSFUL);
259  }
260  return rval;
261}
262
263rtems_status_code
264rtems_i2c_read (rtems_device_major_number major, rtems_device_minor_number minor,
265          void *arg)
266{
267  int rval;                     /* int so we can check for negative value */
268  rtems_libio_rw_args_t *rwargs = arg;
269  DECL_CHECKED_DRV (drv, busno, minor)
270
271    if (0 == rwargs->count) {
272    rwargs->bytes_moved = 0;
273    return RTEMS_SUCCESSFUL;
274  }
275
276  if (0 == drv) {
277    rval =
278      rtems_libi2c_start_read_bytes (minor, (unsigned char *) rwargs->buffer,
279                                     rwargs->count);
280    if (rval >= 0) {
281      rwargs->bytes_moved = rval;
282      rtems_libi2c_send_stop (minor);
283      rval = RTEMS_SUCCESSFUL;
284    } else {
285      rval = -rval;
286    }
287  } else {
288    DISPATCH (rval, read_entry, RTEMS_NOT_IMPLEMENTED);
289  }
290  return rval;
291}
292
293rtems_status_code
294rtems_i2c_write (rtems_device_major_number major, rtems_device_minor_number minor,
295           void *arg)
296{
297  int rval;                     /* int so we can check for negative value */
298  rtems_libio_rw_args_t *rwargs = arg;
299  DECL_CHECKED_DRV (drv, busno, minor)
300
301    if (0 == rwargs->count) {
302    rwargs->bytes_moved = 0;
303    return RTEMS_SUCCESSFUL;
304  }
305
306  if (0 == drv) {
307    rval =
308      rtems_libi2c_start_write_bytes (minor, (unsigned char *) rwargs->buffer,
309                                      rwargs->count);
310    if (rval >= 0) {
311      rwargs->bytes_moved = rval;
312      rtems_libi2c_send_stop (minor);
313      rval = RTEMS_SUCCESSFUL;
314    } else {
315      rval = -rval;
316    }
317  } else {
318    DISPATCH (rval, write_entry, RTEMS_NOT_IMPLEMENTED);
319  }
320  return rval;
321}
322
323rtems_status_code
324rtems_i2c_ioctl (rtems_device_major_number major, rtems_device_minor_number minor,
325           void *arg)
326{
327  rtems_status_code rval;
328  DECL_CHECKED_DRV (drv, busno, minor)
329
330    if (0 == drv) {
331    rval = RTEMS_NOT_IMPLEMENTED;
332  } else {
333    DISPATCH (rval, control_entry, RTEMS_NOT_IMPLEMENTED);
334  }
335  return rval;
336}
337
338
339/* Our ops just dispatch to the registered drivers */
340const rtems_driver_address_table rtems_libi2c_io_ops = {
341  initialization_entry:  rtems_i2c_init,
342  open_entry:            rtems_i2c_open,
343  close_entry:           rtems_i2c_close,
344  read_entry:            rtems_i2c_read,
345  write_entry:           rtems_i2c_write,
346  control_entry:         rtems_i2c_ioctl,
347};
348
349int
350rtems_libi2c_initialize (void)
351{
352  rtems_status_code sc;
353
354  if (is_initialized) {
355    /*
356     * already called before? then skip this step
357     */
358    return 0;
359  }
360 
361  /* rtems_io_register_driver does NOT currently check nor report back
362   * the return code of the 'init' operation, so we cannot
363   * rely on return code since it may seem OK even if the driver 'init;
364   * op failed.
365   * Let 'init' handle 'is_initialized'...
366   */
367  sc = rtems_io_register_driver (0, &rtems_libi2c_io_ops, &rtems_libi2c_major);
368  if (RTEMS_SUCCESSFUL != sc) {
369    safe_printf(
370             DRVNM "Claiming driver slot failed (rtems status code %i)\n",
371             sc);
372    if (libmutex != RTEMS_ID_NONE) {
373      rtems_semaphore_delete (libmutex);
374    }
375    libmutex = RTEMS_ID_NONE;
376    is_initialized = false;
377    return -1;
378  }
379
380  return 0;
381}
382
383int
384rtems_libi2c_register_bus (const char *name, rtems_libi2c_bus_t * bus)
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) {
397    safe_printf ( DRVNM "Bad name: must be an absolute path starting with '/'\n");
398    return -RTEMS_INVALID_NAME;
399  }
400  /* file must not exist */
401  if (!stat (nmcpy, &sbuf)) {
402    safe_printf ( DRVNM "Bad name: file exists already\n");
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) {
413    safe_printf ( DRVNM "Get %s status failed: %s\n",
414             nmcpy, strerror(errno));
415    return -RTEMS_INVALID_NAME;
416  }
417  /* should be a directory since name terminates in '/' */
418
419
420  if (libmutex == RTEMS_ID_NONE) {
421    safe_printf ( DRVNM "Library not initialized\n");
422    return -RTEMS_NOT_DEFINED;
423  }
424
425  if (bus == NULL || bus->size < sizeof (*bus)) {
426    safe_printf ( DRVNM "No bus-ops or size too small -- misconfiguration?\n");
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 = RTEMS_ID_NONE;
436      busses[i].started = false;
437
438      if (!name)
439        sprintf (nmcpy + strlen (nmcpy), "%i", i);
440
441      if ((err = busses[i].bush->ops->init (busses[i].bush))) {
442        /* initialization failed */
443        i = -err;
444      } else {
445        busses[i].name = nmcpy;;
446        nmcpy = 0;
447      }
448
449      break;
450    }
451  }
452  LIBUNLOCK ();
453
454  if (i >= MAX_NO_BUSSES) {
455    i = -RTEMS_TOO_MANY;
456  }
457
458  free (nmcpy);
459
460  return i;
461}
462
463static int
464not_started (int busno)
465{
466  int rval;
467  lock_bus (busno);
468  rval = !busses[busno].started;
469  unlock_bus (busno);
470  return rval;
471}
472
473rtems_status_code
474rtems_libi2c_send_start (rtems_device_minor_number minor)
475{
476  int rval;
477  DECL_CHECKED_BH (busno, bush, minor, +)
478
479    lock_bus (busno);
480  rval = bush->ops->send_start (bush);
481
482  /* if this failed or is not the first start, unlock */
483  if (rval || busses[busno].started) {
484    /* HMM - what to do if the 1st start failed ?
485     * try to reset...
486     */
487    if (!busses[busno].started) {
488      /* just in case the bus driver fiddles with errno */
489      int errno_saved = errno;
490      bush->ops->init (bush);
491      errno = errno_saved;
492    } else if (rval) {
493      /* failed restart */
494      rtems_libi2c_send_stop (minor);
495    }
496    unlock_bus (busno);
497  } else {
498    /* successful 1st start; keep bus locked until stop is sent */
499    busses[busno].started = true;
500  }
501  return rval;
502}
503
504rtems_status_code
505rtems_libi2c_send_stop (rtems_device_minor_number minor)
506{
507  rtems_status_code rval;
508  DECL_CHECKED_BH (busno, bush, minor, +)
509
510    if (not_started (busno))
511    return RTEMS_NOT_OWNER_OF_RESOURCE;
512
513  rval = bush->ops->send_stop (bush);
514
515  busses[busno].started = false;
516
517  unlock_bus (busno);
518  return rval;
519}
520
521rtems_status_code
522rtems_libi2c_send_addr (rtems_device_minor_number minor, int rw)
523{
524  rtems_status_code sc;
525  DECL_CHECKED_BH (busno, bush, minor, +)
526
527    if (not_started (busno))
528    return RTEMS_NOT_OWNER_OF_RESOURCE;
529
530  sc = bush->ops->send_addr (bush, MINOR2ADDR (minor), rw);
531  if (RTEMS_SUCCESSFUL != sc)
532    rtems_libi2c_send_stop (minor);
533  return sc;
534}
535
536int
537rtems_libi2c_read_bytes (rtems_device_minor_number minor, 
538                         unsigned char *bytes, 
539                         int nbytes)
540{
541  int sc;
542  DECL_CHECKED_BH (busno, bush, minor, -)
543
544  if (not_started (busno))
545    return -RTEMS_NOT_OWNER_OF_RESOURCE;
546
547  sc = bush->ops->read_bytes (bush, bytes, nbytes);
548  if (sc < 0)
549    rtems_libi2c_send_stop (minor);
550  return sc;
551}
552
553int
554rtems_libi2c_write_bytes (rtems_device_minor_number minor, 
555                          const unsigned char *bytes, 
556                          int nbytes)
557{
558  int sc;
559  DECL_CHECKED_BH (busno, bush, minor, -)
560
561  if (not_started (busno))
562    return -RTEMS_NOT_OWNER_OF_RESOURCE;
563
564  sc = bush->ops->write_bytes (bush, (unsigned char *)bytes, nbytes);
565  if (sc < 0)
566    rtems_libi2c_send_stop (minor);
567  return sc;
568}
569
570int
571rtems_libi2c_ioctl (rtems_device_minor_number minor, 
572                    int cmd,
573                    ...)
574{
575  va_list            ap;
576  int sc = 0;
577  void *args;
578  bool is_started = false;
579  DECL_CHECKED_BH (busno, bush, minor, -)
580   
581  va_start(ap, cmd);
582  args = va_arg(ap, void *);
583
584  switch(cmd) {
585    /*
586     * add ioctls defined for this level here:   
587     */
588   
589  case RTEMS_LIBI2C_IOCTL_GET_DRV_T:
590    /*
591     * query driver table entry
592     */
593    *(rtems_libi2c_drv_t **)args = (drvs[MINOR2DRV(minor)-1].drv);
594    break;
595
596  case RTEMS_LIBI2C_IOCTL_START_TFM_READ_WRITE:
597    if (not_started (busno))
598      return -RTEMS_NOT_OWNER_OF_RESOURCE;
599
600    /*
601     * address device, then set transfer mode and perform read_write transfer
602     */
603    /*
604     * perform start/address
605     */
606    if (sc == 0) {
607      sc = rtems_libi2c_send_start (minor);
608      is_started = (sc == 0);
609    }
610    /*
611     * set tfr mode
612     */
613    if (sc == 0) {
614      sc = bush->ops->ioctl
615        (bush, 
616         RTEMS_LIBI2C_IOCTL_SET_TFRMODE, 
617         &((rtems_libi2c_tfm_read_write_t *)args)->tfr_mode);
618    }
619    /*
620     * perform read_write
621     */
622    if (sc == 0) {
623      sc = bush->ops->ioctl
624        (bush, 
625         RTEMS_LIBI2C_IOCTL_READ_WRITE, 
626         &((rtems_libi2c_tfm_read_write_t *)args)->rd_wr);
627    }
628    if ((sc < 0) && (is_started)) {
629      rtems_libi2c_send_stop (minor);
630    }
631    break;
632  default:
633    sc = bush->ops->ioctl (bush, cmd, args);
634    break;
635  }
636    return sc;
637}
638
639static int
640do_s_rw (rtems_device_minor_number minor, 
641         unsigned char *bytes, 
642         int nbytes, 
643         int rw)
644{
645  rtems_status_code   sc;
646  rtems_libi2c_bus_t *bush;
647  int                 status;
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    status = bush->ops->read_bytes (bush, bytes, nbytes);
662  else
663    status = bush->ops->write_bytes (bush, bytes, nbytes);
664
665  if (status < 0) {
666    rtems_libi2c_send_stop (minor);
667  }
668  return status;
669}
670
671int
672rtems_libi2c_start_read_bytes (rtems_device_minor_number minor, 
673                               unsigned char *bytes,
674                               int nbytes)
675{
676  return do_s_rw (minor, bytes, nbytes, 1);
677}
678
679int
680rtems_libi2c_start_write_bytes (rtems_device_minor_number minor, 
681                                const unsigned char *bytes,
682                                int nbytes)
683{
684  return do_s_rw (minor, (unsigned char *)bytes, nbytes, 0);
685}
686
687int
688rtems_libi2c_register_drv (const char *name, rtems_libi2c_drv_t * drvtbl,
689                           unsigned busno, unsigned i2caddr)
690{
691  int i;
692  rtems_status_code err;
693  rtems_device_minor_number minor;
694
695  if (libmutex == RTEMS_ID_NONE) {
696    safe_printf ( DRVNM "Library not initialized\n");
697    return -RTEMS_NOT_DEFINED;
698  }
699
700  if (name && strchr (name, '/')) {
701    safe_printf ( DRVNM "Invalid name: '%s' -- must not contain '/'\n", name);
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
710  if (drvtbl == NULL || drvtbl->size < sizeof (*drvtbl)) {
711    safe_printf ( DRVNM "No driver table or size too small -- misconfiguration?\n");
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;
722      uint32_t mode;
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)) {
741          safe_printf( DRVNM
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}
Note: See TracBrowser for help on using the repository browser.