writing-clients 26 KB

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