writing-clients 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  1. This is a small guide for those who want to write kernel drivers for I2C
  2. or SMBus devices.
  3. To set up a driver, you need to do several things. Some are optional, and
  4. some things can be done slightly or completely different. Use this as a
  5. guide, not as a rule book!
  6. General remarks
  7. ===============
  8. Try to keep the kernel namespace as clean as possible. The best way to
  9. do this is to use a unique prefix for all global symbols. This is
  10. especially important for exported symbols, but it is a good idea to do
  11. it for non-exported symbols too. We will use the prefix `foo_' in this
  12. tutorial, and `FOO_' for preprocessor variables.
  13. The driver structure
  14. ====================
  15. Usually, you will implement a single driver structure, and instantiate
  16. all clients from it. Remember, a driver structure contains general access
  17. routines, a client structure specific information like the actual I2C
  18. address.
  19. static struct i2c_driver foo_driver = {
  20. .owner = THIS_MODULE,
  21. .name = "Foo version 2.3 driver",
  22. .id = I2C_DRIVERID_FOO, /* from i2c-id.h, optional */
  23. .flags = I2C_DF_NOTIFY,
  24. .attach_adapter = &foo_attach_adapter,
  25. .detach_client = &foo_detach_client,
  26. .command = &foo_command /* may be NULL */
  27. }
  28. The name can be chosen freely, and may be upto 40 characters long. Please
  29. use something descriptive here.
  30. If used, the id should be a unique ID. The range 0xf000 to 0xffff is
  31. reserved for local use, and you can use one of those until you start
  32. distributing the driver, at which time you should contact the i2c authors
  33. to get your own ID(s). Note that most of the time you don't need an ID
  34. at all so you can just omit it.
  35. Don't worry about the flags field; just put I2C_DF_NOTIFY into it. This
  36. means that your driver will be notified when new adapters are found.
  37. This is almost always what you want.
  38. All other fields are for call-back functions which will be explained
  39. below.
  40. There use to be two additional fields in this structure, inc_use et dec_use,
  41. for module usage count, but these fields were obsoleted and removed.
  42. Extra client data
  43. =================
  44. The client structure has a special `data' field that can point to any
  45. structure at all. You can use this to keep client-specific data. You
  46. do not always need this, but especially for `sensors' drivers, it can
  47. be very useful.
  48. An example structure is below.
  49. struct foo_data {
  50. struct semaphore lock; /* For ISA access in `sensors' drivers. */
  51. int sysctl_id; /* To keep the /proc directory entry for
  52. `sensors' drivers. */
  53. enum chips type; /* To keep the chips type for `sensors' drivers. */
  54. /* Because the i2c bus is slow, it is often useful to cache the read
  55. information of a chip for some time (for example, 1 or 2 seconds).
  56. It depends of course on the device whether this is really worthwhile
  57. or even sensible. */
  58. struct semaphore update_lock; /* When we are reading lots of information,
  59. another process should not update the
  60. below information */
  61. char valid; /* != 0 if the following fields are valid. */
  62. unsigned long last_updated; /* In jiffies */
  63. /* Add the read information here too */
  64. };
  65. Accessing the client
  66. ====================
  67. Let's say we have a valid client structure. At some time, we will need
  68. to gather information from the client, or write new information to the
  69. client. How we will export this information to user-space is less
  70. important at this moment (perhaps we do not need to do this at all for
  71. some obscure clients). But we need generic reading and writing routines.
  72. I have found it useful to define foo_read and foo_write function for this.
  73. For some cases, it will be easier to call the i2c functions directly,
  74. but many chips have some kind of register-value idea that can easily
  75. be encapsulated. Also, some chips have both ISA and I2C interfaces, and
  76. it useful to abstract from this (only for `sensors' drivers).
  77. The below functions are simple examples, and should not be copied
  78. literally.
  79. int foo_read_value(struct i2c_client *client, u8 reg)
  80. {
  81. if (reg < 0x10) /* byte-sized register */
  82. return i2c_smbus_read_byte_data(client,reg);
  83. else /* word-sized register */
  84. return i2c_smbus_read_word_data(client,reg);
  85. }
  86. int foo_write_value(struct i2c_client *client, u8 reg, u16 value)
  87. {
  88. if (reg == 0x10) /* Impossible to write - driver error! */ {
  89. return -1;
  90. else if (reg < 0x10) /* byte-sized register */
  91. return i2c_smbus_write_byte_data(client,reg,value);
  92. else /* word-sized register */
  93. return i2c_smbus_write_word_data(client,reg,value);
  94. }
  95. For sensors code, you may have to cope with ISA registers too. Something
  96. like the below often works. Note the locking!
  97. int foo_read_value(struct i2c_client *client, u8 reg)
  98. {
  99. int res;
  100. if (i2c_is_isa_client(client)) {
  101. down(&(((struct foo_data *) (client->data)) -> lock));
  102. outb_p(reg,client->addr + FOO_ADDR_REG_OFFSET);
  103. res = inb_p(client->addr + FOO_DATA_REG_OFFSET);
  104. up(&(((struct foo_data *) (client->data)) -> lock));
  105. return res;
  106. } else
  107. return i2c_smbus_read_byte_data(client,reg);
  108. }
  109. Writing is done the same way.
  110. Probing and attaching
  111. =====================
  112. Most i2c devices can be present on several i2c addresses; for some this
  113. is determined in hardware (by soldering some chip pins to Vcc or Ground),
  114. for others this can be changed in software (by writing to specific client
  115. registers). Some devices are usually on a specific address, but not always;
  116. and some are even more tricky. So you will probably need to scan several
  117. i2c addresses for your clients, and do some sort of detection to see
  118. whether it is actually a device supported by your driver.
  119. To give the user a maximum of possibilities, some default module parameters
  120. are defined to help determine what addresses are scanned. Several macros
  121. are defined in i2c.h to help you support them, as well as a generic
  122. detection algorithm.
  123. You do not have to use this parameter interface; but don't try to use
  124. function i2c_probe() (or i2c_detect()) if you don't.
  125. NOTE: If you want to write a `sensors' driver, the interface is slightly
  126. different! See below.
  127. Probing classes (i2c)
  128. ---------------------
  129. All parameters are given as lists of unsigned 16-bit integers. Lists are
  130. terminated by I2C_CLIENT_END.
  131. The following lists are used internally:
  132. normal_i2c: filled in by the module writer.
  133. A list of I2C addresses which should normally be examined.
  134. probe: insmod parameter.
  135. A list of pairs. The first value is a bus number (-1 for any I2C bus),
  136. the second is the address. These addresses are also probed, as if they
  137. were in the 'normal' list.
  138. ignore: insmod parameter.
  139. A list of pairs. The first value is a bus number (-1 for any I2C bus),
  140. the second is the I2C address. These addresses are never probed.
  141. This parameter overrules 'normal' and 'probe', but not the 'force' lists.
  142. force: insmod parameter.
  143. A list of pairs. The first value is a bus number (-1 for any I2C bus),
  144. the second is the I2C address. A device is blindly assumed to be on
  145. the given address, no probing is done.
  146. Fortunately, as a module writer, you just have to define the `normal_i2c'
  147. parameter. The complete declaration could look like this:
  148. /* Scan 0x37, and 0x48 to 0x4f */
  149. static unsigned short normal_i2c[] = { 0x37, 0x48, 0x49, 0x4a, 0x4b, 0x4c,
  150. 0x4d, 0x4e, 0x4f, I2C_CLIENT_END };
  151. /* Magic definition of all other variables and things */
  152. I2C_CLIENT_INSMOD;
  153. Note that you *have* to call the defined variable `normal_i2c',
  154. without any prefix!
  155. Probing classes (sensors)
  156. -------------------------
  157. If you write a `sensors' driver, you use a slightly different interface.
  158. As well as I2C addresses, we have to cope with ISA addresses. Also, we
  159. use a enum of chip types. Don't forget to include `sensors.h'.
  160. The following lists are used internally. They are all lists of integers.
  161. normal_i2c: filled in by the module writer. Terminated by SENSORS_I2C_END.
  162. A list of I2C addresses which should normally be examined.
  163. normal_isa: filled in by the module writer. Terminated by SENSORS_ISA_END.
  164. A list of ISA addresses which should normally be examined.
  165. probe: insmod parameter. Initialize this list with SENSORS_I2C_END values.
  166. A list of pairs. The first value is a bus number (SENSORS_ISA_BUS for
  167. the ISA bus, -1 for any I2C bus), the second is the address. These
  168. addresses are also probed, as if they were in the 'normal' list.
  169. ignore: insmod parameter. Initialize this list with SENSORS_I2C_END values.
  170. A list of pairs. The first value is a bus number (SENSORS_ISA_BUS for
  171. the ISA bus, -1 for any I2C bus), the second is the I2C address. These
  172. addresses are never probed. This parameter overrules 'normal' and
  173. 'probe', but not the 'force' lists.
  174. Also used is a list of pointers to sensors_force_data structures:
  175. force_data: insmod parameters. A list, ending with an element of which
  176. the force field is NULL.
  177. Each element contains the type of chip and a list of pairs.
  178. The first value is a bus number (SENSORS_ISA_BUS for the ISA bus,
  179. -1 for any I2C bus), the second is the address.
  180. These are automatically translated to insmod variables of the form
  181. force_foo.
  182. So we have a generic insmod variabled `force', and chip-specific variables
  183. `force_CHIPNAME'.
  184. Fortunately, as a module writer, you just have to define the `normal_i2c'
  185. and `normal_isa' parameters, and define what chip names are used.
  186. The complete declaration could look like this:
  187. /* Scan i2c addresses 0x37, and 0x48 to 0x4f */
  188. static unsigned short normal_i2c[] = { 0x37, 0x48, 0x49, 0x4a, 0x4b, 0x4c,
  189. 0x4d, 0x4e, 0x4f, I2C_CLIENT_END };
  190. /* Scan ISA address 0x290 */
  191. static unsigned int normal_isa[] = {0x0290,SENSORS_ISA_END};
  192. /* Define chips foo and bar, as well as all module parameters and things */
  193. SENSORS_INSMOD_2(foo,bar);
  194. If you have one chip, you use macro SENSORS_INSMOD_1(chip), if you have 2
  195. you use macro SENSORS_INSMOD_2(chip1,chip2), etc. If you do not want to
  196. bother with chip types, you can use SENSORS_INSMOD_0.
  197. A enum is automatically defined as follows:
  198. enum chips { any_chip, chip1, chip2, ... }
  199. Attaching to an adapter
  200. -----------------------
  201. Whenever a new adapter is inserted, or for all adapters if the driver is
  202. being registered, the callback attach_adapter() is called. Now is the
  203. time to determine what devices are present on the adapter, and to register
  204. a client for each of them.
  205. The attach_adapter callback is really easy: we just call the generic
  206. detection function. This function will scan the bus for us, using the
  207. information as defined in the lists explained above. If a device is
  208. detected at a specific address, another callback is called.
  209. int foo_attach_adapter(struct i2c_adapter *adapter)
  210. {
  211. return i2c_probe(adapter,&addr_data,&foo_detect_client);
  212. }
  213. For `sensors' drivers, use the i2c_detect function instead:
  214. int foo_attach_adapter(struct i2c_adapter *adapter)
  215. {
  216. return i2c_detect(adapter,&addr_data,&foo_detect_client);
  217. }
  218. Remember, structure `addr_data' is defined by the macros explained above,
  219. so you do not have to define it yourself.
  220. The i2c_probe or i2c_detect function will call the foo_detect_client
  221. function only for those i2c addresses that actually have a device on
  222. them (unless a `force' parameter was used). In addition, addresses that
  223. are already in use (by some other registered client) are skipped.
  224. The detect client function
  225. --------------------------
  226. The detect client function is called by i2c_probe or i2c_detect.
  227. The `kind' parameter contains 0 if this call is due to a `force'
  228. parameter, and -1 otherwise (for i2c_detect, it contains 0 if
  229. this call is due to the generic `force' parameter, and the chip type
  230. number if it is due to a specific `force' parameter).
  231. Below, some things are only needed if this is a `sensors' driver. Those
  232. parts are between /* SENSORS ONLY START */ and /* SENSORS ONLY END */
  233. markers.
  234. This function should only return an error (any value != 0) if there is
  235. some reason why no more detection should be done anymore. If the
  236. detection just fails for this address, return 0.
  237. For now, you can ignore the `flags' parameter. It is there for future use.
  238. int foo_detect_client(struct i2c_adapter *adapter, int address,
  239. unsigned short flags, int kind)
  240. {
  241. int err = 0;
  242. int i;
  243. struct i2c_client *new_client;
  244. struct foo_data *data;
  245. const char *client_name = ""; /* For non-`sensors' drivers, put the real
  246. name here! */
  247. /* Let's see whether this adapter can support what we need.
  248. Please substitute the things you need here!
  249. For `sensors' drivers, add `! is_isa &&' to the if statement */
  250. if (!i2c_check_functionality(adapter,I2C_FUNC_SMBUS_WORD_DATA |
  251. I2C_FUNC_SMBUS_WRITE_BYTE))
  252. goto ERROR0;
  253. /* SENSORS ONLY START */
  254. const char *type_name = "";
  255. int is_isa = i2c_is_isa_adapter(adapter);
  256. if (is_isa) {
  257. /* If this client can't be on the ISA bus at all, we can stop now
  258. (call `goto ERROR0'). But for kicks, we will assume it is all
  259. right. */
  260. /* Discard immediately if this ISA range is already used */
  261. if (check_region(address,FOO_EXTENT))
  262. goto ERROR0;
  263. /* Probe whether there is anything on this address.
  264. Some example code is below, but you will have to adapt this
  265. for your own driver */
  266. if (kind < 0) /* Only if no force parameter was used */ {
  267. /* We may need long timeouts at least for some chips. */
  268. #define REALLY_SLOW_IO
  269. i = inb_p(address + 1);
  270. if (inb_p(address + 2) != i)
  271. goto ERROR0;
  272. if (inb_p(address + 3) != i)
  273. goto ERROR0;
  274. if (inb_p(address + 7) != i)
  275. goto ERROR0;
  276. #undef REALLY_SLOW_IO
  277. /* Let's just hope nothing breaks here */
  278. i = inb_p(address + 5) & 0x7f;
  279. outb_p(~i & 0x7f,address+5);
  280. if ((inb_p(address + 5) & 0x7f) != (~i & 0x7f)) {
  281. outb_p(i,address+5);
  282. return 0;
  283. }
  284. }
  285. }
  286. /* SENSORS ONLY END */
  287. /* OK. For now, we presume we have a valid client. We now create the
  288. client structure, even though we cannot fill it completely yet.
  289. But it allows us to access several i2c functions safely */
  290. /* Note that we reserve some space for foo_data too. If you don't
  291. need it, remove it. We do it here to help to lessen memory
  292. fragmentation. */
  293. if (! (new_client = kmalloc(sizeof(struct i2c_client) +
  294. sizeof(struct foo_data),
  295. GFP_KERNEL))) {
  296. err = -ENOMEM;
  297. goto ERROR0;
  298. }
  299. /* This is tricky, but it will set the data to the right value. */
  300. client->data = new_client + 1;
  301. data = (struct foo_data *) (client->data);
  302. new_client->addr = address;
  303. new_client->data = data;
  304. new_client->adapter = adapter;
  305. new_client->driver = &foo_driver;
  306. new_client->flags = 0;
  307. /* Now, we do the remaining detection. If no `force' parameter is used. */
  308. /* First, the generic detection (if any), that is skipped if any force
  309. parameter was used. */
  310. if (kind < 0) {
  311. /* The below is of course bogus */
  312. if (foo_read(new_client,FOO_REG_GENERIC) != FOO_GENERIC_VALUE)
  313. goto ERROR1;
  314. }
  315. /* SENSORS ONLY START */
  316. /* Next, specific detection. This is especially important for `sensors'
  317. devices. */
  318. /* Determine the chip type. Not needed if a `force_CHIPTYPE' parameter
  319. was used. */
  320. if (kind <= 0) {
  321. i = foo_read(new_client,FOO_REG_CHIPTYPE);
  322. if (i == FOO_TYPE_1)
  323. kind = chip1; /* As defined in the enum */
  324. else if (i == FOO_TYPE_2)
  325. kind = chip2;
  326. else {
  327. printk("foo: Ignoring 'force' parameter for unknown chip at "
  328. "adapter %d, address 0x%02x\n",i2c_adapter_id(adapter),address);
  329. goto ERROR1;
  330. }
  331. }
  332. /* Now set the type and chip names */
  333. if (kind == chip1) {
  334. type_name = "chip1"; /* For /proc entry */
  335. client_name = "CHIP 1";
  336. } else if (kind == chip2) {
  337. type_name = "chip2"; /* For /proc entry */
  338. client_name = "CHIP 2";
  339. }
  340. /* Reserve the ISA region */
  341. if (is_isa)
  342. request_region(address,FOO_EXTENT,type_name);
  343. /* SENSORS ONLY END */
  344. /* Fill in the remaining client fields. */
  345. strcpy(new_client->name,client_name);
  346. /* SENSORS ONLY BEGIN */
  347. data->type = kind;
  348. /* SENSORS ONLY END */
  349. data->valid = 0; /* Only if you use this field */
  350. init_MUTEX(&data->update_lock); /* Only if you use this field */
  351. /* Any other initializations in data must be done here too. */
  352. /* Tell the i2c layer a new client has arrived */
  353. if ((err = i2c_attach_client(new_client)))
  354. goto ERROR3;
  355. /* SENSORS ONLY BEGIN */
  356. /* Register a new directory entry with module sensors. See below for
  357. the `template' structure. */
  358. if ((i = i2c_register_entry(new_client, type_name,
  359. foo_dir_table_template,THIS_MODULE)) < 0) {
  360. err = i;
  361. goto ERROR4;
  362. }
  363. data->sysctl_id = i;
  364. /* SENSORS ONLY END */
  365. /* This function can write default values to the client registers, if
  366. needed. */
  367. foo_init_client(new_client);
  368. return 0;
  369. /* OK, this is not exactly good programming practice, usually. But it is
  370. very code-efficient in this case. */
  371. ERROR4:
  372. i2c_detach_client(new_client);
  373. ERROR3:
  374. ERROR2:
  375. /* SENSORS ONLY START */
  376. if (is_isa)
  377. release_region(address,FOO_EXTENT);
  378. /* SENSORS ONLY END */
  379. ERROR1:
  380. kfree(new_client);
  381. ERROR0:
  382. return err;
  383. }
  384. Removing the client
  385. ===================
  386. The detach_client call back function is called when a client should be
  387. removed. It may actually fail, but only when panicking. This code is
  388. much simpler than the attachment code, fortunately!
  389. int foo_detach_client(struct i2c_client *client)
  390. {
  391. int err,i;
  392. /* SENSORS ONLY START */
  393. /* Deregister with the `i2c-proc' module. */
  394. i2c_deregister_entry(((struct lm78_data *)(client->data))->sysctl_id);
  395. /* SENSORS ONLY END */
  396. /* Try to detach the client from i2c space */
  397. if ((err = i2c_detach_client(client))) {
  398. printk("foo.o: Client deregistration failed, client not detached.\n");
  399. return err;
  400. }
  401. /* SENSORS ONLY START */
  402. if i2c_is_isa_client(client)
  403. release_region(client->addr,LM78_EXTENT);
  404. /* SENSORS ONLY END */
  405. kfree(client); /* Frees client data too, if allocated at the same time */
  406. return 0;
  407. }
  408. Initializing the module or kernel
  409. =================================
  410. When the kernel is booted, or when your foo driver module is inserted,
  411. you have to do some initializing. Fortunately, just attaching (registering)
  412. the driver module is usually enough.
  413. /* Keep track of how far we got in the initialization process. If several
  414. things have to initialized, and we fail halfway, only those things
  415. have to be cleaned up! */
  416. static int __initdata foo_initialized = 0;
  417. static int __init foo_init(void)
  418. {
  419. int res;
  420. printk("foo version %s (%s)\n",FOO_VERSION,FOO_DATE);
  421. if ((res = i2c_add_driver(&foo_driver))) {
  422. printk("foo: Driver registration failed, module not inserted.\n");
  423. foo_cleanup();
  424. return res;
  425. }
  426. foo_initialized ++;
  427. return 0;
  428. }
  429. void foo_cleanup(void)
  430. {
  431. if (foo_initialized == 1) {
  432. if ((res = i2c_del_driver(&foo_driver))) {
  433. printk("foo: Driver registration failed, module not removed.\n");
  434. return;
  435. }
  436. foo_initialized --;
  437. }
  438. }
  439. /* Substitute your own name and email address */
  440. MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>"
  441. MODULE_DESCRIPTION("Driver for Barf Inc. Foo I2C devices");
  442. module_init(foo_init);
  443. module_exit(foo_cleanup);
  444. Note that some functions are marked by `__init', and some data structures
  445. by `__init_data'. Hose functions and structures can be removed after
  446. kernel booting (or module loading) is completed.
  447. Command function
  448. ================
  449. A generic ioctl-like function call back is supported. You will seldom
  450. need this. You may even set it to NULL.
  451. /* No commands defined */
  452. int foo_command(struct i2c_client *client, unsigned int cmd, void *arg)
  453. {
  454. return 0;
  455. }
  456. Sending and receiving
  457. =====================
  458. If you want to communicate with your device, there are several functions
  459. to do this. You can find all of them in i2c.h.
  460. If you can choose between plain i2c communication and SMBus level
  461. communication, please use the last. All adapters understand SMBus level
  462. commands, but only some of them understand plain i2c!
  463. Plain i2c communication
  464. -----------------------
  465. extern int i2c_master_send(struct i2c_client *,const char* ,int);
  466. extern int i2c_master_recv(struct i2c_client *,char* ,int);
  467. These routines read and write some bytes from/to a client. The client
  468. contains the i2c address, so you do not have to include it. The second
  469. parameter contains the bytes the read/write, the third the length of the
  470. buffer. Returned is the actual number of bytes read/written.
  471. extern int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msg,
  472. int num);
  473. This sends a series of messages. Each message can be a read or write,
  474. and they can be mixed in any way. The transactions are combined: no
  475. stop bit is sent between transaction. The i2c_msg structure contains
  476. for each message the client address, the number of bytes of the message
  477. and the message data itself.
  478. You can read the file `i2c-protocol' for more information about the
  479. actual i2c protocol.
  480. SMBus communication
  481. -------------------
  482. extern s32 i2c_smbus_xfer (struct i2c_adapter * adapter, u16 addr,
  483. unsigned short flags,
  484. char read_write, u8 command, int size,
  485. union i2c_smbus_data * data);
  486. This is the generic SMBus function. All functions below are implemented
  487. in terms of it. Never use this function directly!
  488. extern s32 i2c_smbus_write_quick(struct i2c_client * client, u8 value);
  489. extern s32 i2c_smbus_read_byte(struct i2c_client * client);
  490. extern s32 i2c_smbus_write_byte(struct i2c_client * client, u8 value);
  491. extern s32 i2c_smbus_read_byte_data(struct i2c_client * client, u8 command);
  492. extern s32 i2c_smbus_write_byte_data(struct i2c_client * client,
  493. u8 command, u8 value);
  494. extern s32 i2c_smbus_read_word_data(struct i2c_client * client, u8 command);
  495. extern s32 i2c_smbus_write_word_data(struct i2c_client * client,
  496. u8 command, u16 value);
  497. extern s32 i2c_smbus_write_block_data(struct i2c_client * client,
  498. u8 command, u8 length,
  499. u8 *values);
  500. These ones were removed in Linux 2.6.10 because they had no users, but could
  501. be added back later if needed:
  502. extern s32 i2c_smbus_read_i2c_block_data(struct i2c_client * client,
  503. u8 command, u8 *values);
  504. extern s32 i2c_smbus_read_block_data(struct i2c_client * client,
  505. u8 command, u8 *values);
  506. extern s32 i2c_smbus_write_i2c_block_data(struct i2c_client * client,
  507. u8 command, u8 length,
  508. u8 *values);
  509. extern s32 i2c_smbus_process_call(struct i2c_client * client,
  510. u8 command, u16 value);
  511. extern s32 i2c_smbus_block_process_call(struct i2c_client *client,
  512. u8 command, u8 length,
  513. u8 *values)
  514. All these transactions return -1 on failure. The 'write' transactions
  515. return 0 on success; the 'read' transactions return the read value, except
  516. for read_block, which returns the number of values read. The block buffers
  517. need not be longer than 32 bytes.
  518. You can read the file `smbus-protocol' for more information about the
  519. actual SMBus protocol.
  520. General purpose routines
  521. ========================
  522. Below all general purpose routines are listed, that were not mentioned
  523. before.
  524. /* This call returns a unique low identifier for each registered adapter,
  525. * or -1 if the adapter was not registered.
  526. */
  527. extern int i2c_adapter_id(struct i2c_adapter *adap);
  528. The sensors sysctl/proc interface
  529. =================================
  530. This section only applies if you write `sensors' drivers.
  531. Each sensors driver creates a directory in /proc/sys/dev/sensors for each
  532. registered client. The directory is called something like foo-i2c-4-65.
  533. The sensors module helps you to do this as easily as possible.
  534. The template
  535. ------------
  536. You will need to define a ctl_table template. This template will automatically
  537. be copied to a newly allocated structure and filled in where necessary when
  538. you call sensors_register_entry.
  539. First, I will give an example definition.
  540. static ctl_table foo_dir_table_template[] = {
  541. { FOO_SYSCTL_FUNC1, "func1", NULL, 0, 0644, NULL, &i2c_proc_real,
  542. &i2c_sysctl_real,NULL,&foo_func },
  543. { FOO_SYSCTL_FUNC2, "func2", NULL, 0, 0644, NULL, &i2c_proc_real,
  544. &i2c_sysctl_real,NULL,&foo_func },
  545. { FOO_SYSCTL_DATA, "data", NULL, 0, 0644, NULL, &i2c_proc_real,
  546. &i2c_sysctl_real,NULL,&foo_data },
  547. { 0 }
  548. };
  549. In the above example, three entries are defined. They can either be
  550. accessed through the /proc interface, in the /proc/sys/dev/sensors/*
  551. directories, as files named func1, func2 and data, or alternatively
  552. through the sysctl interface, in the appropriate table, with identifiers
  553. FOO_SYSCTL_FUNC1, FOO_SYSCTL_FUNC2 and FOO_SYSCTL_DATA.
  554. The third, sixth and ninth parameters should always be NULL, and the
  555. fourth should always be 0. The fifth is the mode of the /proc file;
  556. 0644 is safe, as the file will be owned by root:root.
  557. The seventh and eighth parameters should be &i2c_proc_real and
  558. &i2c_sysctl_real if you want to export lists of reals (scaled
  559. integers). You can also use your own function for them, as usual.
  560. Finally, the last parameter is the call-back to gather the data
  561. (see below) if you use the *_proc_real functions.
  562. Gathering the data
  563. ------------------
  564. The call back functions (foo_func and foo_data in the above example)
  565. can be called in several ways; the operation parameter determines
  566. what should be done:
  567. * If operation == SENSORS_PROC_REAL_INFO, you must return the
  568. magnitude (scaling) in nrels_mag;
  569. * If operation == SENSORS_PROC_REAL_READ, you must read information
  570. from the chip and return it in results. The number of integers
  571. to display should be put in nrels_mag;
  572. * If operation == SENSORS_PROC_REAL_WRITE, you must write the
  573. supplied information to the chip. nrels_mag will contain the number
  574. of integers, results the integers themselves.
  575. The *_proc_real functions will display the elements as reals for the
  576. /proc interface. If you set the magnitude to 2, and supply 345 for
  577. SENSORS_PROC_REAL_READ, it would display 3.45; and if the user would
  578. write 45.6 to the /proc file, it would be returned as 4560 for
  579. SENSORS_PROC_REAL_WRITE. A magnitude may even be negative!
  580. An example function:
  581. /* FOO_FROM_REG and FOO_TO_REG translate between scaled values and
  582. register values. Note the use of the read cache. */
  583. void foo_in(struct i2c_client *client, int operation, int ctl_name,
  584. int *nrels_mag, long *results)
  585. {
  586. struct foo_data *data = client->data;
  587. int nr = ctl_name - FOO_SYSCTL_FUNC1; /* reduce to 0 upwards */
  588. if (operation == SENSORS_PROC_REAL_INFO)
  589. *nrels_mag = 2;
  590. else if (operation == SENSORS_PROC_REAL_READ) {
  591. /* Update the readings cache (if necessary) */
  592. foo_update_client(client);
  593. /* Get the readings from the cache */
  594. results[0] = FOO_FROM_REG(data->foo_func_base[nr]);
  595. results[1] = FOO_FROM_REG(data->foo_func_more[nr]);
  596. results[2] = FOO_FROM_REG(data->foo_func_readonly[nr]);
  597. *nrels_mag = 2;
  598. } else if (operation == SENSORS_PROC_REAL_WRITE) {
  599. if (*nrels_mag >= 1) {
  600. /* Update the cache */
  601. data->foo_base[nr] = FOO_TO_REG(results[0]);
  602. /* Update the chip */
  603. foo_write_value(client,FOO_REG_FUNC_BASE(nr),data->foo_base[nr]);
  604. }
  605. if (*nrels_mag >= 2) {
  606. /* Update the cache */
  607. data->foo_more[nr] = FOO_TO_REG(results[1]);
  608. /* Update the chip */
  609. foo_write_value(client,FOO_REG_FUNC_MORE(nr),data->foo_more[nr]);
  610. }
  611. }
  612. }