diff options
Diffstat (limited to 'Documentation/i2c/writing-clients')
-rw-r--r-- | Documentation/i2c/writing-clients | 415 |
1 files changed, 141 insertions, 274 deletions
diff --git a/Documentation/i2c/writing-clients b/Documentation/i2c/writing-clients index fbcff96f4ca1..3d8d36b0ad12 100644 --- a/Documentation/i2c/writing-clients +++ b/Documentation/i2c/writing-clients | |||
@@ -1,5 +1,5 @@ | |||
1 | This is a small guide for those who want to write kernel drivers for I2C | 1 | This is a small guide for those who want to write kernel drivers for I2C |
2 | or SMBus devices. | 2 | or SMBus devices, using Linux as the protocol host/master (not slave). |
3 | 3 | ||
4 | To set up a driver, you need to do several things. Some are optional, and | 4 | To set up a driver, you need to do several things. Some are optional, and |
5 | some things can be done slightly or completely different. Use this as a | 5 | some things can be done slightly or completely different. Use this as a |
@@ -29,8 +29,16 @@ static struct i2c_driver foo_driver = { | |||
29 | .driver = { | 29 | .driver = { |
30 | .name = "foo", | 30 | .name = "foo", |
31 | }, | 31 | }, |
32 | |||
33 | /* iff driver uses driver model ("new style") binding model: */ | ||
34 | .probe = foo_probe, | ||
35 | .remove = foo_remove, | ||
36 | |||
37 | /* else, driver uses "legacy" binding model: */ | ||
32 | .attach_adapter = foo_attach_adapter, | 38 | .attach_adapter = foo_attach_adapter, |
33 | .detach_client = foo_detach_client, | 39 | .detach_client = foo_detach_client, |
40 | |||
41 | /* these may be used regardless of the driver binding model */ | ||
34 | .shutdown = foo_shutdown, /* optional */ | 42 | .shutdown = foo_shutdown, /* optional */ |
35 | .suspend = foo_suspend, /* optional */ | 43 | .suspend = foo_suspend, /* optional */ |
36 | .resume = foo_resume, /* optional */ | 44 | .resume = foo_resume, /* optional */ |
@@ -40,7 +48,8 @@ static struct i2c_driver foo_driver = { | |||
40 | The name field is the driver name, and must not contain spaces. It | 48 | The name field is the driver name, and must not contain spaces. It |
41 | should match the module name (if the driver can be compiled as a module), | 49 | should match the module name (if the driver can be compiled as a module), |
42 | although you can use MODULE_ALIAS (passing "foo" in this example) to add | 50 | although you can use MODULE_ALIAS (passing "foo" in this example) to add |
43 | another name for the module. | 51 | another name for the module. If the driver name doesn't match the module |
52 | name, the module won't be automatically loaded (hotplug/coldplug). | ||
44 | 53 | ||
45 | All other fields are for call-back functions which will be explained | 54 | All other fields are for call-back functions which will be explained |
46 | below. | 55 | below. |
@@ -65,16 +74,13 @@ An example structure is below. | |||
65 | 74 | ||
66 | struct foo_data { | 75 | struct foo_data { |
67 | struct i2c_client client; | 76 | struct i2c_client client; |
68 | struct semaphore lock; /* For ISA access in `sensors' drivers. */ | ||
69 | int sysctl_id; /* To keep the /proc directory entry for | ||
70 | `sensors' drivers. */ | ||
71 | enum chips type; /* To keep the chips type for `sensors' drivers. */ | 77 | enum chips type; /* To keep the chips type for `sensors' drivers. */ |
72 | 78 | ||
73 | /* Because the i2c bus is slow, it is often useful to cache the read | 79 | /* Because the i2c bus is slow, it is often useful to cache the read |
74 | information of a chip for some time (for example, 1 or 2 seconds). | 80 | information of a chip for some time (for example, 1 or 2 seconds). |
75 | It depends of course on the device whether this is really worthwhile | 81 | It depends of course on the device whether this is really worthwhile |
76 | or even sensible. */ | 82 | or even sensible. */ |
77 | struct semaphore update_lock; /* When we are reading lots of information, | 83 | struct mutex update_lock; /* When we are reading lots of information, |
78 | another process should not update the | 84 | another process should not update the |
79 | below information */ | 85 | below information */ |
80 | char valid; /* != 0 if the following fields are valid. */ | 86 | char valid; /* != 0 if the following fields are valid. */ |
@@ -95,8 +101,7 @@ some obscure clients). But we need generic reading and writing routines. | |||
95 | I have found it useful to define foo_read and foo_write function for this. | 101 | I have found it useful to define foo_read and foo_write function for this. |
96 | For some cases, it will be easier to call the i2c functions directly, | 102 | For some cases, it will be easier to call the i2c functions directly, |
97 | but many chips have some kind of register-value idea that can easily | 103 | but many chips have some kind of register-value idea that can easily |
98 | be encapsulated. Also, some chips have both ISA and I2C interfaces, and | 104 | be encapsulated. |
99 | it useful to abstract from this (only for `sensors' drivers). | ||
100 | 105 | ||
101 | The below functions are simple examples, and should not be copied | 106 | The below functions are simple examples, and should not be copied |
102 | literally. | 107 | literally. |
@@ -119,28 +124,101 @@ literally. | |||
119 | return i2c_smbus_write_word_data(client,reg,value); | 124 | return i2c_smbus_write_word_data(client,reg,value); |
120 | } | 125 | } |
121 | 126 | ||
122 | For sensors code, you may have to cope with ISA registers too. Something | ||
123 | like the below often works. Note the locking! | ||
124 | |||
125 | int foo_read_value(struct i2c_client *client, u8 reg) | ||
126 | { | ||
127 | int res; | ||
128 | if (i2c_is_isa_client(client)) { | ||
129 | down(&(((struct foo_data *) (client->data)) -> lock)); | ||
130 | outb_p(reg,client->addr + FOO_ADDR_REG_OFFSET); | ||
131 | res = inb_p(client->addr + FOO_DATA_REG_OFFSET); | ||
132 | up(&(((struct foo_data *) (client->data)) -> lock)); | ||
133 | return res; | ||
134 | } else | ||
135 | return i2c_smbus_read_byte_data(client,reg); | ||
136 | } | ||
137 | |||
138 | Writing is done the same way. | ||
139 | |||
140 | 127 | ||
141 | Probing and attaching | 128 | Probing and attaching |
142 | ===================== | 129 | ===================== |
143 | 130 | ||
131 | The Linux I2C stack was originally written to support access to hardware | ||
132 | monitoring chips on PC motherboards, and thus it embeds some assumptions | ||
133 | that are more appropriate to SMBus (and PCs) than to I2C. One of these | ||
134 | assumptions is that most adapters and devices drivers support the SMBUS_QUICK | ||
135 | protocol to probe device presence. Another is that devices and their drivers | ||
136 | can be sufficiently configured using only such probe primitives. | ||
137 | |||
138 | As Linux and its I2C stack became more widely used in embedded systems | ||
139 | and complex components such as DVB adapters, those assumptions became more | ||
140 | problematic. Drivers for I2C devices that issue interrupts need more (and | ||
141 | different) configuration information, as do drivers handling chip variants | ||
142 | that can't be distinguished by protocol probing, or which need some board | ||
143 | specific information to operate correctly. | ||
144 | |||
145 | Accordingly, the I2C stack now has two models for associating I2C devices | ||
146 | with their drivers: the original "legacy" model, and a newer one that's | ||
147 | fully compatible with the Linux 2.6 driver model. These models do not mix, | ||
148 | since the "legacy" model requires drivers to create "i2c_client" device | ||
149 | objects after SMBus style probing, while the Linux driver model expects | ||
150 | drivers to be given such device objects in their probe() routines. | ||
151 | |||
152 | |||
153 | Standard Driver Model Binding ("New Style") | ||
154 | ------------------------------------------- | ||
155 | |||
156 | System infrastructure, typically board-specific initialization code or | ||
157 | boot firmware, reports what I2C devices exist. For example, there may be | ||
158 | a table, in the kernel or from the boot loader, identifying I2C devices | ||
159 | and linking them to board-specific configuration information about IRQs | ||
160 | and other wiring artifacts, chip type, and so on. That could be used to | ||
161 | create i2c_client objects for each I2C device. | ||
162 | |||
163 | I2C device drivers using this binding model work just like any other | ||
164 | kind of driver in Linux: they provide a probe() method to bind to | ||
165 | those devices, and a remove() method to unbind. | ||
166 | |||
167 | static int foo_probe(struct i2c_client *client); | ||
168 | static int foo_remove(struct i2c_client *client); | ||
169 | |||
170 | Remember that the i2c_driver does not create those client handles. The | ||
171 | handle may be used during foo_probe(). If foo_probe() reports success | ||
172 | (zero not a negative status code) it may save the handle and use it until | ||
173 | foo_remove() returns. That binding model is used by most Linux drivers. | ||
174 | |||
175 | Drivers match devices when i2c_client.driver_name and the driver name are | ||
176 | the same; this approach is used in several other busses that don't have | ||
177 | device typing support in the hardware. The driver and module name should | ||
178 | match, so hotplug/coldplug mechanisms will modprobe the driver. | ||
179 | |||
180 | |||
181 | Device Creation (Standard driver model) | ||
182 | --------------------------------------- | ||
183 | |||
184 | If you know for a fact that an I2C device is connected to a given I2C bus, | ||
185 | you can instantiate that device by simply filling an i2c_board_info | ||
186 | structure with the device address and driver name, and calling | ||
187 | i2c_new_device(). This will create the device, then the driver core will | ||
188 | take care of finding the right driver and will call its probe() method. | ||
189 | If a driver supports different device types, you can specify the type you | ||
190 | want using the type field. You can also specify an IRQ and platform data | ||
191 | if needed. | ||
192 | |||
193 | Sometimes you know that a device is connected to a given I2C bus, but you | ||
194 | don't know the exact address it uses. This happens on TV adapters for | ||
195 | example, where the same driver supports dozens of slightly different | ||
196 | models, and I2C device addresses change from one model to the next. In | ||
197 | that case, you can use the i2c_new_probed_device() variant, which is | ||
198 | similar to i2c_new_device(), except that it takes an additional list of | ||
199 | possible I2C addresses to probe. A device is created for the first | ||
200 | responsive address in the list. If you expect more than one device to be | ||
201 | present in the address range, simply call i2c_new_probed_device() that | ||
202 | many times. | ||
203 | |||
204 | The call to i2c_new_device() or i2c_new_probed_device() typically happens | ||
205 | in the I2C bus driver. You may want to save the returned i2c_client | ||
206 | reference for later use. | ||
207 | |||
208 | |||
209 | Device Deletion (Standard driver model) | ||
210 | --------------------------------------- | ||
211 | |||
212 | Each I2C device which has been created using i2c_new_device() or | ||
213 | i2c_new_probed_device() can be unregistered by calling | ||
214 | i2c_unregister_device(). If you don't call it explicitly, it will be | ||
215 | called automatically before the underlying I2C bus itself is removed, as a | ||
216 | device can't survive its parent in the device driver model. | ||
217 | |||
218 | |||
219 | Legacy Driver Binding Model | ||
220 | --------------------------- | ||
221 | |||
144 | Most i2c devices can be present on several i2c addresses; for some this | 222 | Most i2c devices can be present on several i2c addresses; for some this |
145 | is determined in hardware (by soldering some chip pins to Vcc or Ground), | 223 | is determined in hardware (by soldering some chip pins to Vcc or Ground), |
146 | for others this can be changed in software (by writing to specific client | 224 | for others this can be changed in software (by writing to specific client |
@@ -157,13 +235,9 @@ detection algorithm. | |||
157 | You do not have to use this parameter interface; but don't try to use | 235 | You do not have to use this parameter interface; but don't try to use |
158 | function i2c_probe() if you don't. | 236 | function i2c_probe() if you don't. |
159 | 237 | ||
160 | NOTE: If you want to write a `sensors' driver, the interface is slightly | ||
161 | different! See below. | ||
162 | |||
163 | 238 | ||
164 | 239 | Probing classes (Legacy model) | |
165 | Probing classes | 240 | ------------------------------ |
166 | --------------- | ||
167 | 241 | ||
168 | All parameters are given as lists of unsigned 16-bit integers. Lists are | 242 | All parameters are given as lists of unsigned 16-bit integers. Lists are |
169 | terminated by I2C_CLIENT_END. | 243 | terminated by I2C_CLIENT_END. |
@@ -210,8 +284,8 @@ Note that you *have* to call the defined variable `normal_i2c', | |||
210 | without any prefix! | 284 | without any prefix! |
211 | 285 | ||
212 | 286 | ||
213 | Attaching to an adapter | 287 | Attaching to an adapter (Legacy model) |
214 | ----------------------- | 288 | -------------------------------------- |
215 | 289 | ||
216 | Whenever a new adapter is inserted, or for all adapters if the driver is | 290 | Whenever a new adapter is inserted, or for all adapters if the driver is |
217 | being registered, the callback attach_adapter() is called. Now is the | 291 | being registered, the callback attach_adapter() is called. Now is the |
@@ -237,17 +311,13 @@ them (unless a `force' parameter was used). In addition, addresses that | |||
237 | are already in use (by some other registered client) are skipped. | 311 | are already in use (by some other registered client) are skipped. |
238 | 312 | ||
239 | 313 | ||
240 | The detect client function | 314 | The detect client function (Legacy model) |
241 | -------------------------- | 315 | ----------------------------------------- |
242 | 316 | ||
243 | The detect client function is called by i2c_probe. The `kind' parameter | 317 | The detect client function is called by i2c_probe. The `kind' parameter |
244 | contains -1 for a probed detection, 0 for a forced detection, or a positive | 318 | contains -1 for a probed detection, 0 for a forced detection, or a positive |
245 | number for a forced detection with a chip type forced. | 319 | number for a forced detection with a chip type forced. |
246 | 320 | ||
247 | Below, some things are only needed if this is a `sensors' driver. Those | ||
248 | parts are between /* SENSORS ONLY START */ and /* SENSORS ONLY END */ | ||
249 | markers. | ||
250 | |||
251 | Returning an error different from -ENODEV in a detect function will cause | 321 | Returning an error different from -ENODEV in a detect function will cause |
252 | the detection to stop: other addresses and adapters won't be scanned. | 322 | the detection to stop: other addresses and adapters won't be scanned. |
253 | This should only be done on fatal or internal errors, such as a memory | 323 | This should only be done on fatal or internal errors, such as a memory |
@@ -256,64 +326,20 @@ shortage or i2c_attach_client failing. | |||
256 | For now, you can ignore the `flags' parameter. It is there for future use. | 326 | For now, you can ignore the `flags' parameter. It is there for future use. |
257 | 327 | ||
258 | int foo_detect_client(struct i2c_adapter *adapter, int address, | 328 | int foo_detect_client(struct i2c_adapter *adapter, int address, |
259 | unsigned short flags, int kind) | 329 | int kind) |
260 | { | 330 | { |
261 | int err = 0; | 331 | int err = 0; |
262 | int i; | 332 | int i; |
263 | struct i2c_client *new_client; | 333 | struct i2c_client *client; |
264 | struct foo_data *data; | 334 | struct foo_data *data; |
265 | const char *client_name = ""; /* For non-`sensors' drivers, put the real | 335 | const char *name = ""; |
266 | name here! */ | ||
267 | 336 | ||
268 | /* Let's see whether this adapter can support what we need. | 337 | /* Let's see whether this adapter can support what we need. |
269 | Please substitute the things you need here! | 338 | Please substitute the things you need here! */ |
270 | For `sensors' drivers, add `! is_isa &&' to the if statement */ | ||
271 | if (!i2c_check_functionality(adapter,I2C_FUNC_SMBUS_WORD_DATA | | 339 | if (!i2c_check_functionality(adapter,I2C_FUNC_SMBUS_WORD_DATA | |
272 | I2C_FUNC_SMBUS_WRITE_BYTE)) | 340 | I2C_FUNC_SMBUS_WRITE_BYTE)) |
273 | goto ERROR0; | 341 | goto ERROR0; |
274 | 342 | ||
275 | /* SENSORS ONLY START */ | ||
276 | const char *type_name = ""; | ||
277 | int is_isa = i2c_is_isa_adapter(adapter); | ||
278 | |||
279 | /* Do this only if the chip can additionally be found on the ISA bus | ||
280 | (hybrid chip). */ | ||
281 | |||
282 | if (is_isa) { | ||
283 | |||
284 | /* Discard immediately if this ISA range is already used */ | ||
285 | /* FIXME: never use check_region(), only request_region() */ | ||
286 | if (check_region(address,FOO_EXTENT)) | ||
287 | goto ERROR0; | ||
288 | |||
289 | /* Probe whether there is anything on this address. | ||
290 | Some example code is below, but you will have to adapt this | ||
291 | for your own driver */ | ||
292 | |||
293 | if (kind < 0) /* Only if no force parameter was used */ { | ||
294 | /* We may need long timeouts at least for some chips. */ | ||
295 | #define REALLY_SLOW_IO | ||
296 | i = inb_p(address + 1); | ||
297 | if (inb_p(address + 2) != i) | ||
298 | goto ERROR0; | ||
299 | if (inb_p(address + 3) != i) | ||
300 | goto ERROR0; | ||
301 | if (inb_p(address + 7) != i) | ||
302 | goto ERROR0; | ||
303 | #undef REALLY_SLOW_IO | ||
304 | |||
305 | /* Let's just hope nothing breaks here */ | ||
306 | i = inb_p(address + 5) & 0x7f; | ||
307 | outb_p(~i & 0x7f,address+5); | ||
308 | if ((inb_p(address + 5) & 0x7f) != (~i & 0x7f)) { | ||
309 | outb_p(i,address+5); | ||
310 | return 0; | ||
311 | } | ||
312 | } | ||
313 | } | ||
314 | |||
315 | /* SENSORS ONLY END */ | ||
316 | |||
317 | /* OK. For now, we presume we have a valid client. We now create the | 343 | /* OK. For now, we presume we have a valid client. We now create the |
318 | client structure, even though we cannot fill it completely yet. | 344 | client structure, even though we cannot fill it completely yet. |
319 | But it allows us to access several i2c functions safely */ | 345 | But it allows us to access several i2c functions safely */ |
@@ -323,13 +349,12 @@ For now, you can ignore the `flags' parameter. It is there for future use. | |||
323 | goto ERROR0; | 349 | goto ERROR0; |
324 | } | 350 | } |
325 | 351 | ||
326 | new_client = &data->client; | 352 | client = &data->client; |
327 | i2c_set_clientdata(new_client, data); | 353 | i2c_set_clientdata(client, data); |
328 | 354 | ||
329 | new_client->addr = address; | 355 | client->addr = address; |
330 | new_client->adapter = adapter; | 356 | client->adapter = adapter; |
331 | new_client->driver = &foo_driver; | 357 | client->driver = &foo_driver; |
332 | new_client->flags = 0; | ||
333 | 358 | ||
334 | /* Now, we do the remaining detection. If no `force' parameter is used. */ | 359 | /* Now, we do the remaining detection. If no `force' parameter is used. */ |
335 | 360 | ||
@@ -337,19 +362,17 @@ For now, you can ignore the `flags' parameter. It is there for future use. | |||
337 | parameter was used. */ | 362 | parameter was used. */ |
338 | if (kind < 0) { | 363 | if (kind < 0) { |
339 | /* The below is of course bogus */ | 364 | /* The below is of course bogus */ |
340 | if (foo_read(new_client,FOO_REG_GENERIC) != FOO_GENERIC_VALUE) | 365 | if (foo_read(client, FOO_REG_GENERIC) != FOO_GENERIC_VALUE) |
341 | goto ERROR1; | 366 | goto ERROR1; |
342 | } | 367 | } |
343 | 368 | ||
344 | /* SENSORS ONLY START */ | ||
345 | |||
346 | /* Next, specific detection. This is especially important for `sensors' | 369 | /* Next, specific detection. This is especially important for `sensors' |
347 | devices. */ | 370 | devices. */ |
348 | 371 | ||
349 | /* Determine the chip type. Not needed if a `force_CHIPTYPE' parameter | 372 | /* Determine the chip type. Not needed if a `force_CHIPTYPE' parameter |
350 | was used. */ | 373 | was used. */ |
351 | if (kind <= 0) { | 374 | if (kind <= 0) { |
352 | i = foo_read(new_client,FOO_REG_CHIPTYPE); | 375 | i = foo_read(client, FOO_REG_CHIPTYPE); |
353 | if (i == FOO_TYPE_1) | 376 | if (i == FOO_TYPE_1) |
354 | kind = chip1; /* As defined in the enum */ | 377 | kind = chip1; /* As defined in the enum */ |
355 | else if (i == FOO_TYPE_2) | 378 | else if (i == FOO_TYPE_2) |
@@ -363,63 +386,31 @@ For now, you can ignore the `flags' parameter. It is there for future use. | |||
363 | 386 | ||
364 | /* Now set the type and chip names */ | 387 | /* Now set the type and chip names */ |
365 | if (kind == chip1) { | 388 | if (kind == chip1) { |
366 | type_name = "chip1"; /* For /proc entry */ | 389 | name = "chip1"; |
367 | client_name = "CHIP 1"; | ||
368 | } else if (kind == chip2) { | 390 | } else if (kind == chip2) { |
369 | type_name = "chip2"; /* For /proc entry */ | 391 | name = "chip2"; |
370 | client_name = "CHIP 2"; | ||
371 | } | 392 | } |
372 | 393 | ||
373 | /* Reserve the ISA region */ | ||
374 | if (is_isa) | ||
375 | request_region(address,FOO_EXTENT,type_name); | ||
376 | |||
377 | /* SENSORS ONLY END */ | ||
378 | |||
379 | /* Fill in the remaining client fields. */ | 394 | /* Fill in the remaining client fields. */ |
380 | strcpy(new_client->name,client_name); | 395 | strlcpy(client->name, name, I2C_NAME_SIZE); |
381 | |||
382 | /* SENSORS ONLY BEGIN */ | ||
383 | data->type = kind; | 396 | data->type = kind; |
384 | /* SENSORS ONLY END */ | 397 | mutex_init(&data->update_lock); /* Only if you use this field */ |
385 | |||
386 | data->valid = 0; /* Only if you use this field */ | ||
387 | init_MUTEX(&data->update_lock); /* Only if you use this field */ | ||
388 | 398 | ||
389 | /* Any other initializations in data must be done here too. */ | 399 | /* Any other initializations in data must be done here too. */ |
390 | 400 | ||
391 | /* Tell the i2c layer a new client has arrived */ | ||
392 | if ((err = i2c_attach_client(new_client))) | ||
393 | goto ERROR3; | ||
394 | |||
395 | /* SENSORS ONLY BEGIN */ | ||
396 | /* Register a new directory entry with module sensors. See below for | ||
397 | the `template' structure. */ | ||
398 | if ((i = i2c_register_entry(new_client, type_name, | ||
399 | foo_dir_table_template,THIS_MODULE)) < 0) { | ||
400 | err = i; | ||
401 | goto ERROR4; | ||
402 | } | ||
403 | data->sysctl_id = i; | ||
404 | |||
405 | /* SENSORS ONLY END */ | ||
406 | |||
407 | /* This function can write default values to the client registers, if | 401 | /* This function can write default values to the client registers, if |
408 | needed. */ | 402 | needed. */ |
409 | foo_init_client(new_client); | 403 | foo_init_client(client); |
404 | |||
405 | /* Tell the i2c layer a new client has arrived */ | ||
406 | if ((err = i2c_attach_client(client))) | ||
407 | goto ERROR1; | ||
408 | |||
410 | return 0; | 409 | return 0; |
411 | 410 | ||
412 | /* OK, this is not exactly good programming practice, usually. But it is | 411 | /* OK, this is not exactly good programming practice, usually. But it is |
413 | very code-efficient in this case. */ | 412 | very code-efficient in this case. */ |
414 | 413 | ||
415 | ERROR4: | ||
416 | i2c_detach_client(new_client); | ||
417 | ERROR3: | ||
418 | ERROR2: | ||
419 | /* SENSORS ONLY START */ | ||
420 | if (is_isa) | ||
421 | release_region(address,FOO_EXTENT); | ||
422 | /* SENSORS ONLY END */ | ||
423 | ERROR1: | 414 | ERROR1: |
424 | kfree(data); | 415 | kfree(data); |
425 | ERROR0: | 416 | ERROR0: |
@@ -427,8 +418,8 @@ For now, you can ignore the `flags' parameter. It is there for future use. | |||
427 | } | 418 | } |
428 | 419 | ||
429 | 420 | ||
430 | Removing the client | 421 | Removing the client (Legacy model) |
431 | =================== | 422 | ================================== |
432 | 423 | ||
433 | The detach_client call back function is called when a client should be | 424 | The detach_client call back function is called when a client should be |
434 | removed. It may actually fail, but only when panicking. This code is | 425 | removed. It may actually fail, but only when panicking. This code is |
@@ -436,22 +427,12 @@ much simpler than the attachment code, fortunately! | |||
436 | 427 | ||
437 | int foo_detach_client(struct i2c_client *client) | 428 | int foo_detach_client(struct i2c_client *client) |
438 | { | 429 | { |
439 | int err,i; | 430 | int err; |
440 | |||
441 | /* SENSORS ONLY START */ | ||
442 | /* Deregister with the `i2c-proc' module. */ | ||
443 | i2c_deregister_entry(((struct lm78_data *)(client->data))->sysctl_id); | ||
444 | /* SENSORS ONLY END */ | ||
445 | 431 | ||
446 | /* Try to detach the client from i2c space */ | 432 | /* Try to detach the client from i2c space */ |
447 | if ((err = i2c_detach_client(client))) | 433 | if ((err = i2c_detach_client(client))) |
448 | return err; | 434 | return err; |
449 | 435 | ||
450 | /* HYBRID SENSORS CHIP ONLY START */ | ||
451 | if i2c_is_isa_client(client) | ||
452 | release_region(client->addr,LM78_EXTENT); | ||
453 | /* HYBRID SENSORS CHIP ONLY END */ | ||
454 | |||
455 | kfree(i2c_get_clientdata(client)); | 436 | kfree(i2c_get_clientdata(client)); |
456 | return 0; | 437 | return 0; |
457 | } | 438 | } |
@@ -464,45 +445,34 @@ When the kernel is booted, or when your foo driver module is inserted, | |||
464 | you have to do some initializing. Fortunately, just attaching (registering) | 445 | you have to do some initializing. Fortunately, just attaching (registering) |
465 | the driver module is usually enough. | 446 | the driver module is usually enough. |
466 | 447 | ||
467 | /* Keep track of how far we got in the initialization process. If several | ||
468 | things have to initialized, and we fail halfway, only those things | ||
469 | have to be cleaned up! */ | ||
470 | static int __initdata foo_initialized = 0; | ||
471 | |||
472 | static int __init foo_init(void) | 448 | static int __init foo_init(void) |
473 | { | 449 | { |
474 | int res; | 450 | int res; |
475 | printk("foo version %s (%s)\n",FOO_VERSION,FOO_DATE); | ||
476 | 451 | ||
477 | if ((res = i2c_add_driver(&foo_driver))) { | 452 | if ((res = i2c_add_driver(&foo_driver))) { |
478 | printk("foo: Driver registration failed, module not inserted.\n"); | 453 | printk("foo: Driver registration failed, module not inserted.\n"); |
479 | foo_cleanup(); | ||
480 | return res; | 454 | return res; |
481 | } | 455 | } |
482 | foo_initialized ++; | ||
483 | return 0; | 456 | return 0; |
484 | } | 457 | } |
485 | 458 | ||
486 | void foo_cleanup(void) | 459 | static void __exit foo_cleanup(void) |
487 | { | 460 | { |
488 | if (foo_initialized == 1) { | 461 | i2c_del_driver(&foo_driver); |
489 | if ((res = i2c_del_driver(&foo_driver))) { | ||
490 | printk("foo: Driver registration failed, module not removed.\n"); | ||
491 | return; | ||
492 | } | ||
493 | foo_initialized --; | ||
494 | } | ||
495 | } | 462 | } |
496 | 463 | ||
497 | /* Substitute your own name and email address */ | 464 | /* Substitute your own name and email address */ |
498 | MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>" | 465 | MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>" |
499 | MODULE_DESCRIPTION("Driver for Barf Inc. Foo I2C devices"); | 466 | MODULE_DESCRIPTION("Driver for Barf Inc. Foo I2C devices"); |
500 | 467 | ||
468 | /* a few non-GPL license types are also allowed */ | ||
469 | MODULE_LICENSE("GPL"); | ||
470 | |||
501 | module_init(foo_init); | 471 | module_init(foo_init); |
502 | module_exit(foo_cleanup); | 472 | module_exit(foo_cleanup); |
503 | 473 | ||
504 | Note that some functions are marked by `__init', and some data structures | 474 | Note that some functions are marked by `__init', and some data structures |
505 | by `__init_data'. Hose functions and structures can be removed after | 475 | by `__initdata'. These functions and structures can be removed after |
506 | kernel booting (or module loading) is completed. | 476 | kernel booting (or module loading) is completed. |
507 | 477 | ||
508 | 478 | ||
@@ -632,110 +602,7 @@ General purpose routines | |||
632 | Below all general purpose routines are listed, that were not mentioned | 602 | Below all general purpose routines are listed, that were not mentioned |
633 | before. | 603 | before. |
634 | 604 | ||
635 | /* This call returns a unique low identifier for each registered adapter, | 605 | /* This call returns a unique low identifier for each registered adapter. |
636 | * or -1 if the adapter was not registered. | ||
637 | */ | 606 | */ |
638 | extern int i2c_adapter_id(struct i2c_adapter *adap); | 607 | extern int i2c_adapter_id(struct i2c_adapter *adap); |
639 | 608 | ||
640 | |||
641 | The sensors sysctl/proc interface | ||
642 | ================================= | ||
643 | |||
644 | This section only applies if you write `sensors' drivers. | ||
645 | |||
646 | Each sensors driver creates a directory in /proc/sys/dev/sensors for each | ||
647 | registered client. The directory is called something like foo-i2c-4-65. | ||
648 | The sensors module helps you to do this as easily as possible. | ||
649 | |||
650 | The template | ||
651 | ------------ | ||
652 | |||
653 | You will need to define a ctl_table template. This template will automatically | ||
654 | be copied to a newly allocated structure and filled in where necessary when | ||
655 | you call sensors_register_entry. | ||
656 | |||
657 | First, I will give an example definition. | ||
658 | static ctl_table foo_dir_table_template[] = { | ||
659 | { FOO_SYSCTL_FUNC1, "func1", NULL, 0, 0644, NULL, &i2c_proc_real, | ||
660 | &i2c_sysctl_real,NULL,&foo_func }, | ||
661 | { FOO_SYSCTL_FUNC2, "func2", NULL, 0, 0644, NULL, &i2c_proc_real, | ||
662 | &i2c_sysctl_real,NULL,&foo_func }, | ||
663 | { FOO_SYSCTL_DATA, "data", NULL, 0, 0644, NULL, &i2c_proc_real, | ||
664 | &i2c_sysctl_real,NULL,&foo_data }, | ||
665 | { 0 } | ||
666 | }; | ||
667 | |||
668 | In the above example, three entries are defined. They can either be | ||
669 | accessed through the /proc interface, in the /proc/sys/dev/sensors/* | ||
670 | directories, as files named func1, func2 and data, or alternatively | ||
671 | through the sysctl interface, in the appropriate table, with identifiers | ||
672 | FOO_SYSCTL_FUNC1, FOO_SYSCTL_FUNC2 and FOO_SYSCTL_DATA. | ||
673 | |||
674 | The third, sixth and ninth parameters should always be NULL, and the | ||
675 | fourth should always be 0. The fifth is the mode of the /proc file; | ||
676 | 0644 is safe, as the file will be owned by root:root. | ||
677 | |||
678 | The seventh and eighth parameters should be &i2c_proc_real and | ||
679 | &i2c_sysctl_real if you want to export lists of reals (scaled | ||
680 | integers). You can also use your own function for them, as usual. | ||
681 | Finally, the last parameter is the call-back to gather the data | ||
682 | (see below) if you use the *_proc_real functions. | ||
683 | |||
684 | |||
685 | Gathering the data | ||
686 | ------------------ | ||
687 | |||
688 | The call back functions (foo_func and foo_data in the above example) | ||
689 | can be called in several ways; the operation parameter determines | ||
690 | what should be done: | ||
691 | |||
692 | * If operation == SENSORS_PROC_REAL_INFO, you must return the | ||
693 | magnitude (scaling) in nrels_mag; | ||
694 | * If operation == SENSORS_PROC_REAL_READ, you must read information | ||
695 | from the chip and return it in results. The number of integers | ||
696 | to display should be put in nrels_mag; | ||
697 | * If operation == SENSORS_PROC_REAL_WRITE, you must write the | ||
698 | supplied information to the chip. nrels_mag will contain the number | ||
699 | of integers, results the integers themselves. | ||
700 | |||
701 | The *_proc_real functions will display the elements as reals for the | ||
702 | /proc interface. If you set the magnitude to 2, and supply 345 for | ||
703 | SENSORS_PROC_REAL_READ, it would display 3.45; and if the user would | ||
704 | write 45.6 to the /proc file, it would be returned as 4560 for | ||
705 | SENSORS_PROC_REAL_WRITE. A magnitude may even be negative! | ||
706 | |||
707 | An example function: | ||
708 | |||
709 | /* FOO_FROM_REG and FOO_TO_REG translate between scaled values and | ||
710 | register values. Note the use of the read cache. */ | ||
711 | void foo_in(struct i2c_client *client, int operation, int ctl_name, | ||
712 | int *nrels_mag, long *results) | ||
713 | { | ||
714 | struct foo_data *data = client->data; | ||
715 | int nr = ctl_name - FOO_SYSCTL_FUNC1; /* reduce to 0 upwards */ | ||
716 | |||
717 | if (operation == SENSORS_PROC_REAL_INFO) | ||
718 | *nrels_mag = 2; | ||
719 | else if (operation == SENSORS_PROC_REAL_READ) { | ||
720 | /* Update the readings cache (if necessary) */ | ||
721 | foo_update_client(client); | ||
722 | /* Get the readings from the cache */ | ||
723 | results[0] = FOO_FROM_REG(data->foo_func_base[nr]); | ||
724 | results[1] = FOO_FROM_REG(data->foo_func_more[nr]); | ||
725 | results[2] = FOO_FROM_REG(data->foo_func_readonly[nr]); | ||
726 | *nrels_mag = 2; | ||
727 | } else if (operation == SENSORS_PROC_REAL_WRITE) { | ||
728 | if (*nrels_mag >= 1) { | ||
729 | /* Update the cache */ | ||
730 | data->foo_base[nr] = FOO_TO_REG(results[0]); | ||
731 | /* Update the chip */ | ||
732 | foo_write_value(client,FOO_REG_FUNC_BASE(nr),data->foo_base[nr]); | ||
733 | } | ||
734 | if (*nrels_mag >= 2) { | ||
735 | /* Update the cache */ | ||
736 | data->foo_more[nr] = FOO_TO_REG(results[1]); | ||
737 | /* Update the chip */ | ||
738 | foo_write_value(client,FOO_REG_FUNC_MORE(nr),data->foo_more[nr]); | ||
739 | } | ||
740 | } | ||
741 | } | ||