root/i2c/trunk/kernel/i2c.h @ 3317

Revision 3317, 13.7 KB (checked in by frodo, 15 years ago)

Second part of Kyösti Mälkki's patches

MOD_{INC,DEC}_USE_COUNT should now really be fail-safe.

I wrote in email to Kyösti about this:

If I understand it correctly, the scheme you propose is as follows:

  • Each struct i2c_algorithm variable has inc_use and dec_use hooks in it.
  • The hooks should do a MOD_{INC,DEC}_USE_COUNT in the appropriate adapter modules (of course, these can only be done in the module which needs to be locked)
  • The struct i2c_driver had already similar hooks, for locking the client modules
  • The i2c_{inc,dec}_use_client function should *not* be called when a client is (de)registered; instead, it should only be called when a resource is used that makes it impossible to remove the client and its adapter. For example, the opening of a /proc file or the moving into a /proc directory owned by the client driver. I have applied the changes to i2c-core and i2c.h as in your patch, as well as to i2c-algo-bit. I saw you added MOD_INC_USE_COUNT to the algorithm drivers. That means we can't remove an algorithm module if an adapter module is still connected to it. Perhaps we should do the same as the in the case a driver module is deleted: scan for connected adapters and remove them one by one (including their clients, of course). What do you think? As i2c-algo-pcf is structured similar to i2c-algo-bit, I saw no reason not to apply the same patches to it, including the MOD_{INC,DEC}_USE_COUNT patch. If you know of a reason why I should not have done this, please tell me and/or revert the changes. I know this will break PCF adapter drivers; well, too bad :-) All included adapter drivers have been patched to do MOD_{INC,DEV}_USE_COUNT calls.
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* ------------------------------------------------------------------------- */
2/*                                                                           */
3/* i2c.h - definitions for the \iic-bus interface                            */
4/*                                                                           */
5/* ------------------------------------------------------------------------- */
6/*   Copyright (C) 1995 Simon G. Vogl
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                */
21/* ------------------------------------------------------------------------- */
22#ifndef _I2C_H
23#define _I2C_H
24
25#ifdef __KERNEL__
26
27/* define spinlock to use spinlocks for sync., else use semaphores*/
28/*#define I2C_SPINLOCK*/
29
30#ifdef I2C_SPINLOCK
31#include <asm/spinlock.h>               /* for spinlock_t               */
32#else
33#include <asm/page.h>                   /* for 2.2.xx                   */
34#if LINUX_VERSION_CODE < 0x020019
35#include <linux/sched.h>
36#else
37#include <asm/semaphore.h>
38#endif
39#endif
40/* --- General options ------------------------------------------------ */
41
42#define I2C_ALGO_MAX    4               /* control memory consumption   */
43#define I2C_ADAP_MAX    16
44#define I2C_DRIVER_MAX  16
45#define I2C_CLIENT_MAX  32
46
47struct i2c_msg;
48struct i2c_algorithm;
49struct i2c_adapter;
50struct i2c_client;
51struct i2c_driver;
52
53
54/*
55 * The master routines are the ones normally used to transmit data to devices
56 * on a bus (or read from them). Apart from two basic transfer functions to transmit
57 * one message at a time, a more complex version can be used to transmit an arbitrary
58 * number of messages without interruption.
59 */
60extern int i2c_master_send(struct i2c_client *,const char* ,int);
61extern int i2c_master_recv(struct i2c_client *,char* ,int);
62
63/* Transfer num messages.
64 */
65extern int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg msg[],int num);
66
67/* Two convinience functions: send bytes to/recv from a chip with sending one byte of
68 * data first, normally a register address.
69 */
70extern int i2c_master_send_subadress(struct i2c_client *client,const char *buf ,int count, int subadress);
71extern int i2c_master_recv_subadress(struct i2c_client *client,const char *buf ,int count, int subadress);
72
73
74/*
75 * Some adapter types (i.e. PCF 8584 based ones) may support slave behaviuor.
76 * This is not tested/implemented yet and will change in the future.
77 */
78extern int i2c_slave_send(struct i2c_client *,char*,int);
79extern int i2c_slave_recv(struct i2c_client *,char*,int);
80
81
82
83/*
84 * I2C Message - could be used in the current interface to
85 */
86struct i2c_msg {
87        unsigned char addr;     /* slave address                        */
88        unsigned short flags;           
89#define I2C_M_TEN       0x10    /* we have a ten bit chip address       */
90#define I2C_M_TEN0      0x10    /* herein lie the first 2 bits          */
91#define I2C_M_TEN1      0x12
92#define I2C_M_TEN2      0x14
93#define I2C_M_TEN3      0x16
94#define I2C_M_TENMASK   0x06
95#define I2C_M_RD        0x01
96        short len;              /* msg length                           */
97        char *buf;              /* pointer to msg data                  */
98};
99
100/*
101 * A driver is capable of handling one or more physical devices present on
102 * I2C adapters. This information is used to inform the driver of adapter
103 * events.
104 */
105
106struct i2c_driver {
107        char name[32];
108        int id;
109        unsigned int flags;             /* div., see below              */
110
111        /* notifies the driver that a new bus has appeared. This routine
112         * can be used by the driver to test if the bus meets its conditions
113         * & seek for the presence of the chip(s) it supports. If found, it
114         * registers the client(s) that are on the bus to the i2c admin. via
115         * i2c_attach_client
116         */
117        int (*attach_adapter)(struct i2c_adapter *);
118
119        /* tells the driver that a client is about to be deleted & gives it
120         * the chance to remove its private data. Also, if the client struct
121         * has been dynamically allocated by the driver in the function above,
122         * it must be freed here.
123         */
124        int (*detach_client)(struct i2c_client *);
125       
126        /* a ioctl like command that can be used to perform specific functions
127         * with the device.
128         */
129        int (*command)(struct i2c_client *client,unsigned int cmd, void *arg);
130       
131        /* These two are mainly used for bookkeeping & dynamic unloading of
132         * kernel modules. inc_use tells the driver that a client is being 
133         * used by another module & that it should increase its ref. counter.
134         * dec_use is the inverse operation.
135         * NB: Make sure you have no circular dependencies, or else you get a
136         * deadlock when trying to unload the modules.
137         */
138        void (*inc_use)(struct i2c_client *client);
139        void (*dec_use)(struct i2c_client *client);
140};
141
142/*
143 * i2c_client identifies a single device (i.e. chip) that is connected to an
144 * i2c bus. The behaviour is defined by the routines of the driver. This
145 * function is mainly used for lookup & other admin. functions.
146 */
147struct i2c_client {
148        char name[32];
149        int id;
150        unsigned int flags;             /* div., see below              */
151        unsigned char addr;             /* chip address - NOTE: 7bit    */
152                                        /* addresses are stored in the  */
153                                        /* _LOWER_ 7 bits of this char  */
154                                        /* 10 bit addresses use the full*/
155                                        /* 8 bits & the flags like in   */
156                                        /* i2c_msg                      */
157        struct i2c_adapter *adapter;    /* the adapter we sit on        */
158        struct i2c_driver *driver;      /* and our access routines      */
159        void *data;                     /* for the clients              */
160};
161
162
163/*
164 * The following structs are for those who like to implement new bus drivers:
165 * i2c_algorithm is the interface to a class of hardware solutions which can
166 * be addressed using the same bus algorithms - i.e. bit-banging or the PCF8584
167 * to name two of the most common.
168 */
169struct i2c_algorithm {
170        char name[32];                          /* textual description  */
171        unsigned int id;       
172        /*
173        int (*master_send)(struct i2c_client *,const char*,int);
174        int (*master_recv)(struct i2c_client *,char*,int);
175        int (*master_comb)(struct i2c_client *,char*,const char*,int,int,int);
176        */
177        int (*master_xfer)(struct i2c_adapter *adap,struct i2c_msg msgs[], int num);
178
179        /* --- these optional/future use for some adapter types.*/
180        int (*slave_send)(struct i2c_adapter *,char*,int);
181        int (*slave_recv)(struct i2c_adapter *,char*,int);
182
183        /* --- ioctl like call to set div. parameters. */
184        int (*algo_control)(struct i2c_adapter *, unsigned int, unsigned long);
185
186        /* --- administration stuff. */
187        int (*client_register)(struct i2c_client *);
188        int (*client_unregister)(struct i2c_client *);
189
190        void (*inc_use)(struct i2c_adapter *);
191        void (*dec_use)(struct i2c_adapter *);
192};
193
194
195/*
196 * i2c_adapter is the structure used to identify a physical i2c bus along
197 * with the access algorithms necessary to access it.
198 */
199struct i2c_adapter {
200        char name[32];  /* some useful name to identify the adapter     */
201        unsigned int id;/* == is algo->id | hwdep.struct->id,           */
202                        /* for registered values see below              */
203        struct i2c_algorithm *algo;/* the algorithm to access the bus   */
204
205        void *data;     /* private data for the adapter                 */
206                        /* some data fields that are used by all types  */
207                        /* these data fields are readonly to the public */
208                        /* and can be set via the i2c_ioctl call        */
209
210                        /* data fields that are valid for all devices   */
211#ifdef I2C_SPINLOCK
212        spinlock_t lock;/* used to access the adapter exclusively       */
213        unsigned long lockflags;
214#else
215        struct semaphore lock; 
216#endif
217        unsigned int flags;/* flags specifying div. data                */
218
219        struct i2c_client *clients[I2C_CLIENT_MAX];
220        int client_count;
221
222        int timeout;
223        int retries;
224};
225
226
227/*flags for the driver struct:
228 */
229#define DF_NOTIFY       0x01            /* notify on bus (de/a)ttaches  */
230
231
232/* ----- functions exported by i2c.o */
233
234/* administration...
235 */
236extern int i2c_add_algorithm(struct i2c_algorithm *);
237extern int i2c_del_algorithm(struct i2c_algorithm *);
238
239extern int i2c_add_adapter(struct i2c_adapter *);
240extern int i2c_del_adapter(struct i2c_adapter *);
241
242extern int i2c_add_driver(struct i2c_driver *);
243extern int i2c_del_driver(struct i2c_driver *);
244
245extern int i2c_attach_client(struct i2c_client *);
246extern int i2c_detach_client(struct i2c_client *);
247
248extern void i2c_inc_use_client(struct i2c_client *);
249extern void i2c_dec_use_client(struct i2c_client *);
250
251
252/*
253 * A utility function used in the attach-phase of drivers. Returns at the first address
254 * that acks in the given range.
255 */
256extern int i2c_probe(struct i2c_client *client, int low_addr, int hi_addr);
257
258/* An ioctl like call to set div. parameters of the adapter.
259 */
260extern int i2c_control(struct i2c_client *,unsigned int, unsigned long);
261
262/* This call returns a unique low identifier for each registered adapter,
263 * or -1 if the adapter was not regisitered.
264 */
265extern int i2c_adapter_id(struct i2c_adapter *adap);
266
267#endif /* __KERNEL__ */
268
269
270/* ----- commands for the ioctl like i2c_command call:
271 * note that additional calls are defined in the algorithm and hw
272 *      dependent layers - these can be listed here, or see the
273 *      corresponding header files.
274 */
275                                /* -> bit-adapter specific ioctls       */
276#define I2C_RETRIES     0x0701  /* number times a device adress should  */
277                                /* be polled when not acknowledging     */
278#define I2C_TIMEOUT     0x0702  /* set timeout - call with int          */
279
280
281/* this is for i2c-dev.c        */
282#define I2C_SLAVE       0x0703  /* Change slave address                 */
283                                /* Attn.: Slave address is 7 bits long, */
284                                /*      these are to be passed as the   */
285                                /*      lowest 7 bits in the arg.       */
286                                /* for 10-bit addresses pass lower 8bits*/
287#define I2C_TENBIT      0x0704  /*      with 0-3 as arg to this call    */
288                                /*      a value <0 resets to 7 bits     */
289
290#define I2C_ACK_TEST    0x0710  /* See if a slave is at a specific adress */
291
292/* ... algo-bit.c recognizes */
293#define I2C_UDELAY      0x0705  /* set delay in microsecs between each  */
294                                /* written byte (except address)        */
295#define I2C_MDELAY      0x0706  /* millisec delay between written bytes */
296
297
298
299
300/*
301 * ---- Driver types -----------------------------------------------------
302 *       device id name + number        function description, i2c address(es)
303 */
304
305#define I2C_DRIVERID_MSP3400     1
306#define I2C_DRIVERID_TUNER       2
307#define I2C_DRIVERID_VIDEOTEXT   3
308#define I2C_DRIVERID_GL518SM     4      /* hardware monitor cpu temp.   */
309#define I2C_DRIVERID_TEA6420     5      /* audio matrix switch          */
310#define I2C_DRIVERID_TEA6415C    6      /* video matrix switch          */
311#define I2C_DRIVERID_TDA9840     7      /* stereo sound processor       */
312#define I2C_DRIVERID_SAA7111A    8      /* video input processor        */
313#define I2C_DRIVERID_SAA5281     9      /* videotext decoder            */
314#define I2C_DRIVERID_SAA7112    10      /* video decoder, image scaler  */
315#define I2C_DRIVERID_SAA7120    11      /* video encoder                */
316#define I2C_DRIVERID_SAA7121    12      /* video encoder                */
317#define I2C_DRIVERID_SAA7185B   13      /* video encoder                */
318#define I2C_DRIVERID_CH7003     14      /* digital pc to tv encoder     */
319#define I2C_DRIVERID_PCF8574A   15      /* i2c expander - 8 bit in/out  */
320#define I2C_DRIVERID_PCF8582C   16      /* eeprom                       */
321#define I2C_DRIVERID_AT24Cxx    17      /* eeprom 1/2/4/8/16 K          */
322
323#define I2C_DRIVERID_EXP0       0xF0    /* experimental use id's        */
324#define I2C_DRIVERID_EXP1       0xF1
325#define I2C_DRIVERID_EXP2       0xF2
326#define I2C_DRIVERID_EXP3       0xF3
327
328                                        /* the 1000-1999 range is used by
329                                           the lm_sensor developers     */
330#define I2C_DRIVERID_I2CDEV     1000
331#define I2C_DRIVERID_I2CPROC    1001
332#define I2C_DRIVERID_LM78       1002
333#define I2C_DRIVERID_LM75       1003
334#define I2C_DRIVERID_GL518      1004
335#define I2C_DRIVERID_EEPROM     1005
336#define I2C_DRIVERID_W83781D    1006
337#define I2C_DRIVERID_LM80       1007
338#define I2C_DRIVERID_ADM1021    1008
339#define I2C_DRIVERID_ADM9240    1009
340#define I2C_DRIVERID_LTC1710    1010
341#define I2C_DRIVERID_BT848      1200
342
343/*
344 * ---- Adapter types ----------------------------------------------------
345 *
346 * First, we distinguish between several algorithms to access the hardware
347 * interface types, as a PCF 8584 needs other care than a bit adapter.
348 */
349
350#define ALGO_NONE       0x000000
351#define ALGO_BIT        0x010000        /* bit style adapters           */
352#define ALGO_PCF        0x020000        /* PCF 8584 style adapters      */
353#define ALGO_SMBUS      0x040000
354#define ALGO_ISA        0x050000
355#define ALGO_SAA7146A   0x060000        /* SAA 7146 video decoder bus   */
356
357
358#define ALGO_EXP        0x800000        /* experimental                 */
359
360#define ALGO_MASK       0xff0000        /* Mask for algorithms          */
361#define ALGO_SHIFT      0x10    /* right shift to get index values      */
362
363#define I2C_HW_ADAPS    0x10000         /* # adapter types              */
364#define I2C_HW_MASK     0xffff         
365
366
367/* hw specific modules that are defined per algorithm layer
368 */
369
370/* --- Bit algorithm adapters                                           */
371#define HW_B_LP         0x00    /* Parallel port Philips style adapter  */
372#define HW_B_LPC        0x01    /* Parallel port, over control reg.     */
373#define HW_B_SER        0x02    /* Serial line interface                */
374#define HW_B_ELV        0x03    /* ELV Card                             */
375#define HW_B_VELLE      0x04    /* Vellemann K8000                      */
376#define HW_B_BT848      0x05    /* BT848 video boards                   */
377#define HW_B_WNV        0x06    /* Winnov Videums                       */
378#define HW_B_VIA        0x07    /* Via vt82c586b                        */
379#define HW_B_HYDRA      0x08    /* Apple Hydra Mac I/O                  */
380
381/* --- PCF 8584 based algorithms                                        */
382#define HW_P_LP         0x00    /* Parallel port interface              */
383#define HW_P_ISA        0x01    /* generic ISA Bus inteface card        */
384#define HW_P_ELEK       0x02    /* Elektor ISA Bus inteface card        */
385
386
387
388
389/* ----- I2C-DEV: char device interface stuff ------------------------- */
390
391#define I2C_MAJOR       89              /* Device major number          */
392
393
394#  if LINUX_VERSION_CODE < 0x020100
395/* Hack to make this thing compile under 2.0.xx kernels
396 */
397
398#  ifdef MODULE
399#    define MODULE_AUTHOR(noone)
400#    define MODULE_DESCRIPTION(none)
401#    define MODULE_PARM(no,param)
402#    define MODULE_PARM_DESC(no,description)
403#    define EXPORT_SYMBOL(noexport)
404#    define EXPORT_NO_SYMBOLS
405#  endif
406
407#  ifndef NULL
408#    define NULL ( (void *) 0 )
409#  endif
410#endif
411
412#  ifndef ENODEV
413#    include <asm/errno.h>
414#  endif
415#endif
Note: See TracBrowser for help on using the browser.