Rev 420 | Rev 458 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
420 | giacomo | 1 | /* i2c-core.c - a device driver for the iic-bus interface */ |
2 | /* ------------------------------------------------------------------------- */ |
||
3 | /* Copyright (C) 1995-99 Simon G. Vogl |
||
4 | |||
5 | This program is free software; you can redistribute it and/or modify |
||
6 | it under the terms of the GNU General Public License as published by |
||
7 | the Free Software Foundation; either version 2 of the License, or |
||
8 | (at your option) any later version. |
||
9 | |||
10 | This program is distributed in the hope that it will be useful, |
||
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
13 | GNU General Public License for more details. |
||
14 | |||
15 | You should have received a copy of the GNU General Public License |
||
16 | along with this program; if not, write to the Free Software |
||
17 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ |
||
18 | /* ------------------------------------------------------------------------- */ |
||
19 | |||
20 | /* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>. |
||
21 | All SMBus-related things are written by Frodo Looijaard <frodol@dds.nl> |
||
22 | SMBus 2.0 support by Mark Studebaker <mdsxyz123@yahoo.com> */ |
||
23 | |||
429 | giacomo | 24 | /* $Id: i2c-core.c,v 1.2 2004-01-28 18:38:01 giacomo Exp $ */ |
420 | giacomo | 25 | |
26 | /* #define DEBUG 1 */ /* needed to pick up the dev_dbg() calls */ |
||
27 | |||
429 | giacomo | 28 | #include <linuxcomp.h> |
29 | |||
420 | giacomo | 30 | #include <linux/module.h> |
31 | #include <linux/kernel.h> |
||
32 | #include <linux/errno.h> |
||
33 | #include <linux/slab.h> |
||
34 | #include <linux/i2c.h> |
||
35 | #include <linux/init.h> |
||
36 | #include <linux/seq_file.h> |
||
37 | #include <asm/uaccess.h> |
||
38 | |||
39 | |||
40 | #define DEB(x) if (i2c_debug>=1) x; |
||
41 | #define DEB2(x) if (i2c_debug>=2) x; |
||
42 | |||
43 | static LIST_HEAD(adapters); |
||
44 | static LIST_HEAD(drivers); |
||
45 | static DECLARE_MUTEX(core_lists); |
||
46 | |||
47 | /**** debug level */ |
||
48 | static int i2c_debug; |
||
49 | |||
50 | int i2c_device_probe(struct device *dev) |
||
51 | { |
||
52 | return -ENODEV; |
||
53 | } |
||
54 | |||
55 | int i2c_device_remove(struct device *dev) |
||
56 | { |
||
57 | return 0; |
||
58 | } |
||
59 | |||
60 | static void i2c_adapter_dev_release(struct device *dev) |
||
61 | { |
||
62 | struct i2c_adapter *adap = dev_to_i2c_adapter(dev); |
||
63 | complete(&adap->dev_released); |
||
64 | } |
||
65 | |||
66 | static struct device_driver i2c_adapter_driver = { |
||
67 | .name = "i2c_adapter", |
||
68 | .bus = &i2c_bus_type, |
||
69 | .probe = i2c_device_probe, |
||
70 | .remove = i2c_device_remove, |
||
71 | }; |
||
72 | |||
73 | static void i2c_adapter_class_dev_release(struct class_device *dev) |
||
74 | { |
||
75 | struct i2c_adapter *adap = class_dev_to_i2c_adapter(dev); |
||
76 | complete(&adap->class_dev_released); |
||
77 | } |
||
78 | |||
79 | static struct class i2c_adapter_class = { |
||
80 | .name = "i2c-adapter", |
||
81 | .release = &i2c_adapter_class_dev_release, |
||
82 | }; |
||
83 | |||
84 | static ssize_t show_adapter_name(struct device *dev, char *buf) |
||
85 | { |
||
86 | struct i2c_adapter *adap = dev_to_i2c_adapter(dev); |
||
87 | return sprintf(buf, "%s\n", adap->name); |
||
88 | } |
||
89 | static DEVICE_ATTR(name, S_IRUGO, show_adapter_name, NULL); |
||
90 | |||
91 | |||
92 | static void i2c_client_release(struct device *dev) |
||
93 | { |
||
94 | struct i2c_client *client = to_i2c_client(dev); |
||
95 | complete(&client->released); |
||
96 | } |
||
97 | |||
98 | static ssize_t show_client_name(struct device *dev, char *buf) |
||
99 | { |
||
100 | struct i2c_client *client = to_i2c_client(dev); |
||
101 | return sprintf(buf, "%s\n", client->name); |
||
102 | } |
||
103 | |||
104 | /* |
||
105 | * We can't use the DEVICE_ATTR() macro here as we want the same filename for a |
||
106 | * different type of a device. So beware if the DEVICE_ATTR() macro ever |
||
107 | * changes, this definition will also have to change. |
||
108 | */ |
||
109 | static struct device_attribute dev_attr_client_name = { |
||
110 | .attr = {.name = "name", .mode = S_IRUGO, .owner = THIS_MODULE }, |
||
111 | .show = &show_client_name, |
||
112 | }; |
||
113 | |||
114 | |||
115 | /* --------------------------------------------------- |
||
116 | * registering functions |
||
117 | * --------------------------------------------------- |
||
118 | */ |
||
119 | |||
120 | /* ----- |
||
121 | * i2c_add_adapter is called from within the algorithm layer, |
||
122 | * when a new hw adapter registers. A new device is register to be |
||
123 | * available for clients. |
||
124 | */ |
||
125 | int i2c_add_adapter(struct i2c_adapter *adap) |
||
126 | { |
||
127 | static int nr = 0; |
||
128 | struct list_head *item; |
||
129 | struct i2c_driver *driver; |
||
130 | |||
131 | down(&core_lists); |
||
132 | |||
133 | adap->nr = nr++; |
||
134 | init_MUTEX(&adap->bus_lock); |
||
135 | init_MUTEX(&adap->clist_lock); |
||
136 | list_add_tail(&adap->list,&adapters); |
||
137 | INIT_LIST_HEAD(&adap->clients); |
||
138 | |||
139 | /* Add the adapter to the driver core. |
||
140 | * If the parent pointer is not set up, |
||
141 | * we add this adapter to the legacy bus. |
||
142 | */ |
||
143 | if (adap->dev.parent == NULL) |
||
144 | adap->dev.parent = &legacy_bus; |
||
145 | sprintf(adap->dev.bus_id, "i2c-%d", adap->nr); |
||
146 | adap->dev.driver = &i2c_adapter_driver; |
||
147 | adap->dev.release = &i2c_adapter_dev_release; |
||
148 | device_register(&adap->dev); |
||
149 | device_create_file(&adap->dev, &dev_attr_name); |
||
150 | |||
151 | /* Add this adapter to the i2c_adapter class */ |
||
152 | memset(&adap->class_dev, 0x00, sizeof(struct class_device)); |
||
153 | adap->class_dev.dev = &adap->dev; |
||
154 | adap->class_dev.class = &i2c_adapter_class; |
||
155 | strlcpy(adap->class_dev.class_id, adap->dev.bus_id, BUS_ID_SIZE); |
||
156 | class_device_register(&adap->class_dev); |
||
157 | |||
158 | /* inform drivers of new adapters */ |
||
159 | list_for_each(item,&drivers) { |
||
160 | driver = list_entry(item, struct i2c_driver, list); |
||
161 | if (driver->flags & I2C_DF_NOTIFY) |
||
162 | /* We ignore the return code; if it fails, too bad */ |
||
163 | driver->attach_adapter(adap); |
||
164 | } |
||
165 | up(&core_lists); |
||
166 | |||
167 | DEB(dev_dbg(&adap->dev, "registered as adapter #%d\n", adap->nr)); |
||
168 | return 0; |
||
169 | } |
||
170 | |||
171 | |||
172 | int i2c_del_adapter(struct i2c_adapter *adap) |
||
173 | { |
||
174 | struct list_head *item, *_n; |
||
175 | struct i2c_driver *driver; |
||
176 | struct i2c_client *client; |
||
177 | int res = 0; |
||
178 | |||
179 | down(&core_lists); |
||
180 | |||
181 | list_for_each(item,&drivers) { |
||
182 | driver = list_entry(item, struct i2c_driver, list); |
||
183 | if (driver->detach_adapter) |
||
184 | if ((res = driver->detach_adapter(adap))) { |
||
185 | dev_warn(&adap->dev, "can't detach adapter" |
||
186 | "while detaching driver %s: driver not " |
||
187 | "detached!", driver->name); |
||
188 | goto out_unlock; |
||
189 | } |
||
190 | } |
||
191 | |||
192 | /* detach any active clients. This must be done first, because |
||
193 | * it can fail; in which case we give upp. */ |
||
194 | list_for_each_safe(item, _n, &adap->clients) { |
||
195 | client = list_entry(item, struct i2c_client, list); |
||
196 | |||
197 | /* detaching devices is unconditional of the set notify |
||
198 | * flag, as _all_ clients that reside on the adapter |
||
199 | * must be deleted, as this would cause invalid states. |
||
200 | */ |
||
201 | if ((res=client->driver->detach_client(client))) { |
||
202 | dev_err(&adap->dev, "adapter not " |
||
203 | "unregistered, because client at " |
||
204 | "address %02x can't be detached. ", |
||
205 | client->addr); |
||
206 | goto out_unlock; |
||
207 | } |
||
208 | } |
||
209 | |||
210 | /* clean up the sysfs representation */ |
||
211 | init_completion(&adap->dev_released); |
||
212 | init_completion(&adap->class_dev_released); |
||
213 | class_device_unregister(&adap->class_dev); |
||
214 | device_remove_file(&adap->dev, &dev_attr_name); |
||
215 | device_unregister(&adap->dev); |
||
216 | list_del(&adap->list); |
||
217 | |||
218 | /* wait for sysfs to drop all references */ |
||
219 | wait_for_completion(&adap->dev_released); |
||
220 | wait_for_completion(&adap->class_dev_released); |
||
221 | |||
222 | DEB(dev_dbg(&adap->dev, "adapter unregistered\n")); |
||
223 | |||
224 | out_unlock: |
||
225 | up(&core_lists); |
||
226 | return res; |
||
227 | } |
||
228 | |||
229 | |||
230 | /* ----- |
||
231 | * What follows is the "upwards" interface: commands for talking to clients, |
||
232 | * which implement the functions to access the physical information of the |
||
233 | * chips. |
||
234 | */ |
||
235 | |||
236 | int i2c_add_driver(struct i2c_driver *driver) |
||
237 | { |
||
238 | struct list_head *item; |
||
239 | struct i2c_adapter *adapter; |
||
240 | int res = 0; |
||
241 | |||
242 | down(&core_lists); |
||
243 | |||
244 | /* add the driver to the list of i2c drivers in the driver core */ |
||
245 | driver->driver.name = driver->name; |
||
246 | driver->driver.bus = &i2c_bus_type; |
||
247 | driver->driver.probe = i2c_device_probe; |
||
248 | driver->driver.remove = i2c_device_remove; |
||
249 | |||
250 | res = driver_register(&driver->driver); |
||
251 | if (res) |
||
252 | goto out_unlock; |
||
253 | |||
254 | list_add_tail(&driver->list,&drivers); |
||
255 | DEB(printk(KERN_DEBUG "i2c-core.o: driver %s registered.\n",driver->name)); |
||
256 | |||
257 | /* now look for instances of driver on our adapters */ |
||
258 | if (driver->flags & I2C_DF_NOTIFY) { |
||
259 | list_for_each(item,&adapters) { |
||
260 | adapter = list_entry(item, struct i2c_adapter, list); |
||
261 | driver->attach_adapter(adapter); |
||
262 | } |
||
263 | } |
||
264 | |||
265 | out_unlock: |
||
266 | up(&core_lists); |
||
267 | return res; |
||
268 | } |
||
269 | |||
270 | int i2c_del_driver(struct i2c_driver *driver) |
||
271 | { |
||
272 | struct list_head *item1, *item2, *_n; |
||
273 | struct i2c_client *client; |
||
274 | struct i2c_adapter *adap; |
||
275 | |||
276 | int res = 0; |
||
277 | |||
278 | down(&core_lists); |
||
279 | |||
280 | /* Have a look at each adapter, if clients of this driver are still |
||
281 | * attached. If so, detach them to be able to kill the driver |
||
282 | * afterwards. |
||
283 | */ |
||
284 | DEB2(printk(KERN_DEBUG "i2c-core.o: unregister_driver - looking for clients.\n")); |
||
285 | /* removing clients does not depend on the notify flag, else |
||
286 | * invalid operation might (will!) result, when using stale client |
||
287 | * pointers. |
||
288 | */ |
||
289 | list_for_each(item1,&adapters) { |
||
290 | adap = list_entry(item1, struct i2c_adapter, list); |
||
291 | DEB2(dev_dbg(&adap->dev, "examining adapter\n")); |
||
292 | if (driver->detach_adapter) { |
||
293 | if ((res = driver->detach_adapter(adap))) { |
||
294 | dev_warn(&adap->dev, "while unregistering " |
||
295 | "dummy driver %s, adapter could " |
||
296 | "not be detached properly; driver " |
||
297 | "not unloaded!",driver->name); |
||
298 | goto out_unlock; |
||
299 | } |
||
300 | } else { |
||
301 | list_for_each_safe(item2, _n, &adap->clients) { |
||
302 | client = list_entry(item2, struct i2c_client, list); |
||
303 | if (client->driver != driver) |
||
304 | continue; |
||
305 | DEB2(printk(KERN_DEBUG "i2c-core.o: " |
||
306 | "detaching client %s:\n", |
||
307 | client->name)); |
||
308 | if ((res = driver->detach_client(client))) { |
||
309 | dev_err(&adap->dev, "while " |
||
310 | "unregistering driver " |
||
311 | "`%s', the client at " |
||
312 | "address %02x of " |
||
313 | "adapter could not " |
||
314 | "be detached; driver " |
||
315 | "not unloaded!", |
||
316 | driver->name, |
||
317 | client->addr); |
||
318 | goto out_unlock; |
||
319 | } |
||
320 | } |
||
321 | } |
||
322 | } |
||
323 | |||
324 | driver_unregister(&driver->driver); |
||
325 | list_del(&driver->list); |
||
326 | DEB(printk(KERN_DEBUG "i2c-core.o: driver unregistered: %s\n",driver->name)); |
||
327 | |||
328 | out_unlock: |
||
329 | up(&core_lists); |
||
330 | return 0; |
||
331 | } |
||
332 | |||
333 | static int __i2c_check_addr(struct i2c_adapter *adapter, unsigned int addr) |
||
334 | { |
||
335 | struct list_head *item; |
||
336 | struct i2c_client *client; |
||
337 | |||
338 | list_for_each(item,&adapter->clients) { |
||
339 | client = list_entry(item, struct i2c_client, list); |
||
340 | if (client->addr == addr) |
||
341 | return -EBUSY; |
||
342 | } |
||
343 | return 0; |
||
344 | } |
||
345 | |||
346 | int i2c_check_addr(struct i2c_adapter *adapter, int addr) |
||
347 | { |
||
348 | int rval; |
||
349 | |||
350 | down(&adapter->clist_lock); |
||
351 | rval = __i2c_check_addr(adapter, addr); |
||
352 | up(&adapter->clist_lock); |
||
353 | |||
354 | return rval; |
||
355 | } |
||
356 | |||
357 | int i2c_attach_client(struct i2c_client *client) |
||
358 | { |
||
359 | struct i2c_adapter *adapter = client->adapter; |
||
360 | |||
361 | down(&adapter->clist_lock); |
||
362 | if (__i2c_check_addr(client->adapter, client->addr)) { |
||
363 | up(&adapter->clist_lock); |
||
364 | return -EBUSY; |
||
365 | } |
||
366 | list_add_tail(&client->list,&adapter->clients); |
||
367 | up(&adapter->clist_lock); |
||
368 | |||
369 | if (adapter->client_register) { |
||
370 | if (adapter->client_register(client)) { |
||
371 | dev_warn(&adapter->dev, "warning: client_register " |
||
372 | "seems to have failed for client %02x\n", |
||
373 | client->addr); |
||
374 | } |
||
375 | } |
||
376 | |||
377 | DEB(dev_dbg(&adapter->dev, "client [%s] registered to adapter\n", |
||
378 | client->dev.name)); |
||
379 | |||
380 | if (client->flags & I2C_CLIENT_ALLOW_USE) |
||
381 | client->usage_count = 0; |
||
382 | |||
383 | client->dev.parent = &client->adapter->dev; |
||
384 | client->dev.driver = &client->driver->driver; |
||
385 | client->dev.bus = &i2c_bus_type; |
||
386 | client->dev.release = &i2c_client_release; |
||
387 | |||
388 | snprintf(&client->dev.bus_id[0], sizeof(client->dev.bus_id), |
||
389 | "%d-%04x", i2c_adapter_id(adapter), client->addr); |
||
390 | printk("registering %s\n", client->dev.bus_id); |
||
391 | device_register(&client->dev); |
||
392 | device_create_file(&client->dev, &dev_attr_client_name); |
||
393 | |||
394 | return 0; |
||
395 | } |
||
396 | |||
397 | |||
398 | int i2c_detach_client(struct i2c_client *client) |
||
399 | { |
||
400 | struct i2c_adapter *adapter = client->adapter; |
||
401 | int res = 0; |
||
402 | |||
403 | if ((client->flags & I2C_CLIENT_ALLOW_USE) && (client->usage_count > 0)) |
||
404 | return -EBUSY; |
||
405 | |||
406 | if (adapter->client_unregister) { |
||
407 | res = adapter->client_unregister(client); |
||
408 | if (res) { |
||
409 | printk(KERN_ERR |
||
410 | "i2c-core.o: client_unregister [%s] failed, " |
||
411 | "client not detached", client->name); |
||
412 | goto out; |
||
413 | } |
||
414 | } |
||
415 | |||
416 | down(&adapter->clist_lock); |
||
417 | list_del(&client->list); |
||
418 | init_completion(&client->released); |
||
419 | device_remove_file(&client->dev, &dev_attr_client_name); |
||
420 | device_unregister(&client->dev); |
||
421 | up(&adapter->clist_lock); |
||
422 | wait_for_completion(&client->released); |
||
423 | |||
424 | out: |
||
425 | return res; |
||
426 | } |
||
427 | |||
428 | static int i2c_inc_use_client(struct i2c_client *client) |
||
429 | { |
||
430 | |||
431 | if (!try_module_get(client->driver->owner)) |
||
432 | return -ENODEV; |
||
433 | if (!try_module_get(client->adapter->owner)) { |
||
434 | module_put(client->driver->owner); |
||
435 | return -ENODEV; |
||
436 | } |
||
437 | |||
438 | return 0; |
||
439 | } |
||
440 | |||
441 | static void i2c_dec_use_client(struct i2c_client *client) |
||
442 | { |
||
443 | module_put(client->driver->owner); |
||
444 | module_put(client->adapter->owner); |
||
445 | } |
||
446 | |||
447 | int i2c_use_client(struct i2c_client *client) |
||
448 | { |
||
449 | if (!i2c_inc_use_client(client)) |
||
450 | return -ENODEV; |
||
451 | |||
452 | if (client->flags & I2C_CLIENT_ALLOW_USE) { |
||
453 | if (client->flags & I2C_CLIENT_ALLOW_MULTIPLE_USE) |
||
454 | client->usage_count++; |
||
455 | else if (client->usage_count > 0) |
||
456 | goto busy; |
||
457 | else |
||
458 | client->usage_count++; |
||
459 | } |
||
460 | |||
461 | return 0; |
||
462 | busy: |
||
463 | i2c_dec_use_client(client); |
||
464 | return -EBUSY; |
||
465 | } |
||
466 | |||
467 | int i2c_release_client(struct i2c_client *client) |
||
468 | { |
||
469 | if(client->flags & I2C_CLIENT_ALLOW_USE) { |
||
470 | if(client->usage_count>0) |
||
471 | client->usage_count--; |
||
472 | else |
||
473 | { |
||
474 | printk(KERN_WARNING " i2c-core.o: dec_use_client used one too many times\n"); |
||
475 | return -EPERM; |
||
476 | } |
||
477 | } |
||
478 | |||
479 | i2c_dec_use_client(client); |
||
480 | |||
481 | return 0; |
||
482 | } |
||
483 | |||
484 | void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg) |
||
485 | { |
||
486 | struct list_head *item; |
||
487 | struct i2c_client *client; |
||
488 | |||
489 | down(&adap->clist_lock); |
||
490 | list_for_each(item,&adap->clients) { |
||
491 | client = list_entry(item, struct i2c_client, list); |
||
492 | if (!try_module_get(client->driver->owner)) |
||
493 | continue; |
||
494 | if (NULL != client->driver->command) { |
||
495 | up(&adap->clist_lock); |
||
496 | client->driver->command(client,cmd,arg); |
||
497 | down(&adap->clist_lock); |
||
498 | } |
||
499 | module_put(client->driver->owner); |
||
500 | } |
||
501 | up(&adap->clist_lock); |
||
502 | } |
||
503 | |||
504 | |||
505 | /* match always succeeds, as we want the probe() to tell if we really accept this match */ |
||
506 | static int i2c_device_match(struct device *dev, struct device_driver *drv) |
||
507 | { |
||
508 | return 1; |
||
509 | } |
||
510 | |||
511 | struct bus_type i2c_bus_type = { |
||
512 | .name = "i2c", |
||
513 | .match = i2c_device_match, |
||
514 | }; |
||
515 | |||
516 | static int __init i2c_init(void) |
||
517 | { |
||
518 | int retval; |
||
519 | |||
520 | retval = bus_register(&i2c_bus_type); |
||
521 | if (retval) |
||
522 | return retval; |
||
523 | retval = driver_register(&i2c_adapter_driver); |
||
524 | if (retval) |
||
525 | return retval; |
||
526 | return class_register(&i2c_adapter_class); |
||
527 | } |
||
528 | |||
529 | static void __exit i2c_exit(void) |
||
530 | { |
||
531 | class_unregister(&i2c_adapter_class); |
||
532 | driver_unregister(&i2c_adapter_driver); |
||
533 | bus_unregister(&i2c_bus_type); |
||
534 | } |
||
535 | |||
536 | subsys_initcall(i2c_init); |
||
537 | module_exit(i2c_exit); |
||
538 | |||
539 | /* ---------------------------------------------------- |
||
540 | * the functional interface to the i2c busses. |
||
541 | * ---------------------------------------------------- |
||
542 | */ |
||
543 | |||
544 | int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg msgs[],int num) |
||
545 | { |
||
546 | int ret; |
||
547 | |||
548 | if (adap->algo->master_xfer) { |
||
549 | DEB2(dev_dbg(&adap->dev, "master_xfer: with %d msgs.\n", num)); |
||
550 | |||
551 | down(&adap->bus_lock); |
||
552 | ret = adap->algo->master_xfer(adap,msgs,num); |
||
553 | up(&adap->bus_lock); |
||
554 | |||
555 | return ret; |
||
556 | } else { |
||
557 | DEB2(dev_dbg(&adap->dev, "I2C level transfers not supported\n")); |
||
558 | return -ENOSYS; |
||
559 | } |
||
560 | } |
||
561 | |||
562 | int i2c_master_send(struct i2c_client *client,const char *buf ,int count) |
||
563 | { |
||
564 | int ret; |
||
565 | struct i2c_adapter *adap=client->adapter; |
||
566 | struct i2c_msg msg; |
||
567 | |||
568 | if (client->adapter->algo->master_xfer) { |
||
569 | msg.addr = client->addr; |
||
570 | msg.flags = client->flags & I2C_M_TEN; |
||
571 | msg.len = count; |
||
572 | msg.buf = (char *)buf; |
||
573 | |||
574 | DEB2(dev_dbg(&client->adapter->dev, "master_send: writing %d bytes.\n", |
||
575 | count)); |
||
576 | |||
577 | down(&adap->bus_lock); |
||
578 | ret = adap->algo->master_xfer(adap,&msg,1); |
||
579 | up(&adap->bus_lock); |
||
580 | |||
581 | /* if everything went ok (i.e. 1 msg transmitted), return #bytes |
||
582 | * transmitted, else error code. |
||
583 | */ |
||
584 | return (ret == 1 )? count : ret; |
||
585 | } else { |
||
586 | dev_err(&client->adapter->dev, "I2C level transfers not supported\n"); |
||
587 | return -ENOSYS; |
||
588 | } |
||
589 | } |
||
590 | |||
591 | int i2c_master_recv(struct i2c_client *client, char *buf ,int count) |
||
592 | { |
||
593 | struct i2c_adapter *adap=client->adapter; |
||
594 | struct i2c_msg msg; |
||
595 | int ret; |
||
596 | if (client->adapter->algo->master_xfer) { |
||
597 | msg.addr = client->addr; |
||
598 | msg.flags = client->flags & I2C_M_TEN; |
||
599 | msg.flags |= I2C_M_RD; |
||
600 | msg.len = count; |
||
601 | msg.buf = buf; |
||
602 | |||
603 | DEB2(dev_dbg(&client->adapter->dev, "master_recv: reading %d bytes.\n", |
||
604 | count)); |
||
605 | |||
606 | down(&adap->bus_lock); |
||
607 | ret = adap->algo->master_xfer(adap,&msg,1); |
||
608 | up(&adap->bus_lock); |
||
609 | |||
610 | DEB2(printk(KERN_DEBUG "i2c-core.o: master_recv: return:%d (count:%d, addr:0x%02x)\n", |
||
611 | ret, count, client->addr)); |
||
612 | |||
613 | /* if everything went ok (i.e. 1 msg transmitted), return #bytes |
||
614 | * transmitted, else error code. |
||
615 | */ |
||
616 | return (ret == 1 )? count : ret; |
||
617 | } else { |
||
618 | dev_err(&client->adapter->dev, "I2C level transfers not supported\n"); |
||
619 | return -ENOSYS; |
||
620 | } |
||
621 | } |
||
622 | |||
623 | |||
624 | int i2c_control(struct i2c_client *client, |
||
625 | unsigned int cmd, unsigned long arg) |
||
626 | { |
||
627 | int ret = 0; |
||
628 | struct i2c_adapter *adap = client->adapter; |
||
629 | |||
630 | DEB2(printk(KERN_DEBUG "i2c-core.o: i2c ioctl, cmd: 0x%x, arg: %#lx\n", cmd, arg)); |
||
631 | switch ( cmd ) { |
||
632 | case I2C_RETRIES: |
||
633 | adap->retries = arg; |
||
634 | break; |
||
635 | case I2C_TIMEOUT: |
||
636 | adap->timeout = arg; |
||
637 | break; |
||
638 | default: |
||
639 | if (adap->algo->algo_control!=NULL) |
||
640 | ret = adap->algo->algo_control(adap,cmd,arg); |
||
641 | } |
||
642 | return ret; |
||
643 | } |
||
644 | |||
645 | /* ---------------------------------------------------- |
||
646 | * the i2c address scanning function |
||
647 | * Will not work for 10-bit addresses! |
||
648 | * ---------------------------------------------------- |
||
649 | */ |
||
650 | int i2c_probe(struct i2c_adapter *adapter, |
||
651 | struct i2c_client_address_data *address_data, |
||
652 | int (*found_proc) (struct i2c_adapter *, int, int)) |
||
653 | { |
||
654 | int addr,i,found,err; |
||
655 | int adap_id = i2c_adapter_id(adapter); |
||
656 | |||
657 | /* Forget it if we can't probe using SMBUS_QUICK */ |
||
658 | if (! i2c_check_functionality(adapter,I2C_FUNC_SMBUS_QUICK)) |
||
659 | return -1; |
||
660 | |||
661 | for (addr = 0x00; addr <= 0x7f; addr++) { |
||
662 | |||
663 | /* Skip if already in use */ |
||
664 | if (i2c_check_addr(adapter,addr)) |
||
665 | continue; |
||
666 | |||
667 | /* If it is in one of the force entries, we don't do any detection |
||
668 | at all */ |
||
669 | found = 0; |
||
670 | |||
671 | for (i = 0; !found && (address_data->force[i] != I2C_CLIENT_END); i += 3) { |
||
672 | if (((adap_id == address_data->force[i]) || |
||
673 | (address_data->force[i] == ANY_I2C_BUS)) && |
||
674 | (addr == address_data->force[i+1])) { |
||
675 | DEB2(printk(KERN_DEBUG "i2c-core.o: found force parameter for adapter %d, addr %04x\n", |
||
676 | adap_id,addr)); |
||
677 | if ((err = found_proc(adapter,addr,0))) |
||
678 | return err; |
||
679 | found = 1; |
||
680 | } |
||
681 | } |
||
682 | if (found) |
||
683 | continue; |
||
684 | |||
685 | /* If this address is in one of the ignores, we can forget about |
||
686 | it right now */ |
||
687 | for (i = 0; |
||
688 | !found && (address_data->ignore[i] != I2C_CLIENT_END); |
||
689 | i += 2) { |
||
690 | if (((adap_id == address_data->ignore[i]) || |
||
691 | ((address_data->ignore[i] == ANY_I2C_BUS))) && |
||
692 | (addr == address_data->ignore[i+1])) { |
||
693 | DEB2(printk(KERN_DEBUG "i2c-core.o: found ignore parameter for adapter %d, " |
||
694 | "addr %04x\n", adap_id ,addr)); |
||
695 | found = 1; |
||
696 | } |
||
697 | } |
||
698 | for (i = 0; |
||
699 | !found && (address_data->ignore_range[i] != I2C_CLIENT_END); |
||
700 | i += 3) { |
||
701 | if (((adap_id == address_data->ignore_range[i]) || |
||
702 | ((address_data->ignore_range[i]==ANY_I2C_BUS))) && |
||
703 | (addr >= address_data->ignore_range[i+1]) && |
||
704 | (addr <= address_data->ignore_range[i+2])) { |
||
705 | DEB2(printk(KERN_DEBUG "i2c-core.o: found ignore_range parameter for adapter %d, " |
||
706 | "addr %04x\n", adap_id,addr)); |
||
707 | found = 1; |
||
708 | } |
||
709 | } |
||
710 | if (found) |
||
711 | continue; |
||
712 | |||
713 | /* Now, we will do a detection, but only if it is in the normal or |
||
714 | probe entries */ |
||
715 | for (i = 0; |
||
716 | !found && (address_data->normal_i2c[i] != I2C_CLIENT_END); |
||
717 | i += 1) { |
||
718 | if (addr == address_data->normal_i2c[i]) { |
||
719 | found = 1; |
||
720 | DEB2(printk(KERN_DEBUG "i2c-core.o: found normal i2c entry for adapter %d, " |
||
721 | "addr %02x", adap_id,addr)); |
||
722 | } |
||
723 | } |
||
724 | |||
725 | for (i = 0; |
||
726 | !found && (address_data->normal_i2c_range[i] != I2C_CLIENT_END); |
||
727 | i += 2) { |
||
728 | if ((addr >= address_data->normal_i2c_range[i]) && |
||
729 | (addr <= address_data->normal_i2c_range[i+1])) { |
||
730 | found = 1; |
||
731 | DEB2(printk(KERN_DEBUG "i2c-core.o: found normal i2c_range entry for adapter %d, " |
||
732 | "addr %04x\n", adap_id,addr)); |
||
733 | } |
||
734 | } |
||
735 | |||
736 | for (i = 0; |
||
737 | !found && (address_data->probe[i] != I2C_CLIENT_END); |
||
738 | i += 2) { |
||
739 | if (((adap_id == address_data->probe[i]) || |
||
740 | ((address_data->probe[i] == ANY_I2C_BUS))) && |
||
741 | (addr == address_data->probe[i+1])) { |
||
742 | found = 1; |
||
743 | DEB2(printk(KERN_DEBUG "i2c-core.o: found probe parameter for adapter %d, " |
||
744 | "addr %04x\n", adap_id,addr)); |
||
745 | } |
||
746 | } |
||
747 | for (i = 0; |
||
748 | !found && (address_data->probe_range[i] != I2C_CLIENT_END); |
||
749 | i += 3) { |
||
750 | if (((adap_id == address_data->probe_range[i]) || |
||
751 | (address_data->probe_range[i] == ANY_I2C_BUS)) && |
||
752 | (addr >= address_data->probe_range[i+1]) && |
||
753 | (addr <= address_data->probe_range[i+2])) { |
||
754 | found = 1; |
||
755 | DEB2(printk(KERN_DEBUG "i2c-core.o: found probe_range parameter for adapter %d, " |
||
756 | "addr %04x\n", adap_id,addr)); |
||
757 | } |
||
758 | } |
||
759 | if (!found) |
||
760 | continue; |
||
761 | |||
762 | /* OK, so we really should examine this address. First check |
||
763 | whether there is some client here at all! */ |
||
764 | if (i2c_smbus_xfer(adapter,addr,0,0,0,I2C_SMBUS_QUICK,NULL) >= 0) |
||
765 | if ((err = found_proc(adapter,addr,-1))) |
||
766 | return err; |
||
767 | } |
||
768 | return 0; |
||
769 | } |
||
770 | |||
771 | /* |
||
772 | * return id number for a specific adapter |
||
773 | */ |
||
774 | int i2c_adapter_id(struct i2c_adapter *adap) |
||
775 | { |
||
776 | return adap->nr; |
||
777 | } |
||
778 | |||
779 | struct i2c_adapter* i2c_get_adapter(int id) |
||
780 | { |
||
781 | struct list_head *item; |
||
782 | struct i2c_adapter *adapter; |
||
783 | |||
784 | down(&core_lists); |
||
785 | list_for_each(item,&adapters) { |
||
786 | adapter = list_entry(item, struct i2c_adapter, list); |
||
787 | if (id == adapter->nr && |
||
788 | try_module_get(adapter->owner)) { |
||
789 | up(&core_lists); |
||
790 | return adapter; |
||
791 | } |
||
792 | } |
||
793 | up(&core_lists); |
||
794 | return NULL; |
||
795 | } |
||
796 | |||
797 | void i2c_put_adapter(struct i2c_adapter *adap) |
||
798 | { |
||
799 | module_put(adap->owner); |
||
800 | } |
||
801 | |||
802 | /* The SMBus parts */ |
||
803 | |||
804 | #define POLY (0x1070U << 3) |
||
805 | static u8 |
||
806 | crc8(u16 data) |
||
807 | { |
||
808 | int i; |
||
809 | |||
810 | for(i = 0; i < 8; i++) { |
||
811 | if (data & 0x8000) |
||
812 | data = data ^ POLY; |
||
813 | data = data << 1; |
||
814 | } |
||
815 | return (u8)(data >> 8); |
||
816 | } |
||
817 | |||
818 | /* CRC over count bytes in the first array plus the bytes in the rest |
||
819 | array if it is non-null. rest[0] is the (length of rest) - 1 |
||
820 | and is included. */ |
||
821 | u8 i2c_smbus_partial_pec(u8 crc, int count, u8 *first, u8 *rest) |
||
822 | { |
||
823 | int i; |
||
824 | |||
825 | for(i = 0; i < count; i++) |
||
826 | crc = crc8((crc ^ first[i]) << 8); |
||
827 | if(rest != NULL) |
||
828 | for(i = 0; i <= rest[0]; i++) |
||
829 | crc = crc8((crc ^ rest[i]) << 8); |
||
830 | return crc; |
||
831 | } |
||
832 | |||
833 | u8 i2c_smbus_pec(int count, u8 *first, u8 *rest) |
||
834 | { |
||
835 | return i2c_smbus_partial_pec(0, count, first, rest); |
||
836 | } |
||
837 | |||
838 | /* Returns new "size" (transaction type) |
||
839 | Note that we convert byte to byte_data and byte_data to word_data |
||
840 | rather than invent new xxx_PEC transactions. */ |
||
841 | int i2c_smbus_add_pec(u16 addr, u8 command, int size, |
||
842 | union i2c_smbus_data *data) |
||
843 | { |
||
844 | u8 buf[3]; |
||
845 | |||
846 | buf[0] = addr << 1; |
||
847 | buf[1] = command; |
||
848 | switch(size) { |
||
849 | case I2C_SMBUS_BYTE: |
||
850 | data->byte = i2c_smbus_pec(2, buf, NULL); |
||
851 | size = I2C_SMBUS_BYTE_DATA; |
||
852 | break; |
||
853 | case I2C_SMBUS_BYTE_DATA: |
||
854 | buf[2] = data->byte; |
||
855 | data->word = buf[2] || |
||
856 | (i2c_smbus_pec(3, buf, NULL) << 8); |
||
857 | size = I2C_SMBUS_WORD_DATA; |
||
858 | break; |
||
859 | case I2C_SMBUS_WORD_DATA: |
||
860 | /* unsupported */ |
||
861 | break; |
||
862 | case I2C_SMBUS_BLOCK_DATA: |
||
863 | data->block[data->block[0] + 1] = |
||
864 | i2c_smbus_pec(2, buf, data->block); |
||
865 | size = I2C_SMBUS_BLOCK_DATA_PEC; |
||
866 | break; |
||
867 | } |
||
868 | return size; |
||
869 | } |
||
870 | |||
871 | int i2c_smbus_check_pec(u16 addr, u8 command, int size, u8 partial, |
||
872 | union i2c_smbus_data *data) |
||
873 | { |
||
874 | u8 buf[3], rpec, cpec; |
||
875 | |||
876 | buf[1] = command; |
||
877 | switch(size) { |
||
878 | case I2C_SMBUS_BYTE_DATA: |
||
879 | buf[0] = (addr << 1) | 1; |
||
880 | cpec = i2c_smbus_pec(2, buf, NULL); |
||
881 | rpec = data->byte; |
||
882 | break; |
||
883 | case I2C_SMBUS_WORD_DATA: |
||
884 | buf[0] = (addr << 1) | 1; |
||
885 | buf[2] = data->word & 0xff; |
||
886 | cpec = i2c_smbus_pec(3, buf, NULL); |
||
887 | rpec = data->word >> 8; |
||
888 | break; |
||
889 | case I2C_SMBUS_WORD_DATA_PEC: |
||
890 | /* unsupported */ |
||
891 | cpec = rpec = 0; |
||
892 | break; |
||
893 | case I2C_SMBUS_PROC_CALL_PEC: |
||
894 | /* unsupported */ |
||
895 | cpec = rpec = 0; |
||
896 | break; |
||
897 | case I2C_SMBUS_BLOCK_DATA_PEC: |
||
898 | buf[0] = (addr << 1); |
||
899 | buf[2] = (addr << 1) | 1; |
||
900 | cpec = i2c_smbus_pec(3, buf, data->block); |
||
901 | rpec = data->block[data->block[0] + 1]; |
||
902 | break; |
||
903 | case I2C_SMBUS_BLOCK_PROC_CALL_PEC: |
||
904 | buf[0] = (addr << 1) | 1; |
||
905 | rpec = i2c_smbus_partial_pec(partial, 1, |
||
906 | buf, data->block); |
||
907 | cpec = data->block[data->block[0] + 1]; |
||
908 | break; |
||
909 | default: |
||
910 | cpec = rpec = 0; |
||
911 | break; |
||
912 | } |
||
913 | if(rpec != cpec) { |
||
914 | DEB(printk(KERN_DEBUG "i2c-core.o: Bad PEC 0x%02x vs. 0x%02x\n", |
||
915 | rpec, cpec)); |
||
916 | return -1; |
||
917 | } |
||
918 | return 0; |
||
919 | } |
||
920 | |||
921 | s32 i2c_smbus_write_quick(struct i2c_client *client, u8 value) |
||
922 | { |
||
923 | return i2c_smbus_xfer(client->adapter,client->addr,client->flags, |
||
924 | value,0,I2C_SMBUS_QUICK,NULL); |
||
925 | } |
||
926 | |||
927 | s32 i2c_smbus_read_byte(struct i2c_client *client) |
||
928 | { |
||
929 | union i2c_smbus_data data; |
||
930 | if (i2c_smbus_xfer(client->adapter,client->addr,client->flags, |
||
931 | I2C_SMBUS_READ,0,I2C_SMBUS_BYTE, &data)) |
||
932 | return -1; |
||
933 | else |
||
934 | return 0x0FF & data.byte; |
||
935 | } |
||
936 | |||
937 | s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value) |
||
938 | { |
||
939 | union i2c_smbus_data data; /* only for PEC */ |
||
940 | return i2c_smbus_xfer(client->adapter,client->addr,client->flags, |
||
941 | I2C_SMBUS_WRITE,value, I2C_SMBUS_BYTE,&data); |
||
942 | } |
||
943 | |||
944 | s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command) |
||
945 | { |
||
946 | union i2c_smbus_data data; |
||
947 | if (i2c_smbus_xfer(client->adapter,client->addr,client->flags, |
||
948 | I2C_SMBUS_READ,command, I2C_SMBUS_BYTE_DATA,&data)) |
||
949 | return -1; |
||
950 | else |
||
951 | return 0x0FF & data.byte; |
||
952 | } |
||
953 | |||
954 | s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value) |
||
955 | { |
||
956 | union i2c_smbus_data data; |
||
957 | data.byte = value; |
||
958 | return i2c_smbus_xfer(client->adapter,client->addr,client->flags, |
||
959 | I2C_SMBUS_WRITE,command, |
||
960 | I2C_SMBUS_BYTE_DATA,&data); |
||
961 | } |
||
962 | |||
963 | s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command) |
||
964 | { |
||
965 | union i2c_smbus_data data; |
||
966 | if (i2c_smbus_xfer(client->adapter,client->addr,client->flags, |
||
967 | I2C_SMBUS_READ,command, I2C_SMBUS_WORD_DATA, &data)) |
||
968 | return -1; |
||
969 | else |
||
970 | return 0x0FFFF & data.word; |
||
971 | } |
||
972 | |||
973 | s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value) |
||
974 | { |
||
975 | union i2c_smbus_data data; |
||
976 | data.word = value; |
||
977 | return i2c_smbus_xfer(client->adapter,client->addr,client->flags, |
||
978 | I2C_SMBUS_WRITE,command, |
||
979 | I2C_SMBUS_WORD_DATA,&data); |
||
980 | } |
||
981 | |||
982 | s32 i2c_smbus_process_call(struct i2c_client *client, u8 command, u16 value) |
||
983 | { |
||
984 | union i2c_smbus_data data; |
||
985 | data.word = value; |
||
986 | if (i2c_smbus_xfer(client->adapter,client->addr,client->flags, |
||
987 | I2C_SMBUS_WRITE,command, |
||
988 | I2C_SMBUS_PROC_CALL, &data)) |
||
989 | return -1; |
||
990 | else |
||
991 | return 0x0FFFF & data.word; |
||
992 | } |
||
993 | |||
994 | /* Returns the number of read bytes */ |
||
995 | s32 i2c_smbus_read_block_data(struct i2c_client *client, u8 command, u8 *values) |
||
996 | { |
||
997 | union i2c_smbus_data data; |
||
998 | int i; |
||
999 | if (i2c_smbus_xfer(client->adapter,client->addr,client->flags, |
||
1000 | I2C_SMBUS_READ,command, |
||
1001 | I2C_SMBUS_BLOCK_DATA,&data)) |
||
1002 | return -1; |
||
1003 | else { |
||
1004 | for (i = 1; i <= data.block[0]; i++) |
||
1005 | values[i-1] = data.block[i]; |
||
1006 | return data.block[0]; |
||
1007 | } |
||
1008 | } |
||
1009 | |||
1010 | s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command, u8 length, u8 *values) |
||
1011 | { |
||
1012 | union i2c_smbus_data data; |
||
1013 | int i; |
||
1014 | if (length > I2C_SMBUS_BLOCK_MAX) |
||
1015 | length = I2C_SMBUS_BLOCK_MAX; |
||
1016 | for (i = 1; i <= length; i++) |
||
1017 | data.block[i] = values[i-1]; |
||
1018 | data.block[0] = length; |
||
1019 | return i2c_smbus_xfer(client->adapter,client->addr,client->flags, |
||
1020 | I2C_SMBUS_WRITE,command, |
||
1021 | I2C_SMBUS_BLOCK_DATA,&data); |
||
1022 | } |
||
1023 | |||
1024 | /* Returns the number of read bytes */ |
||
1025 | s32 i2c_smbus_block_process_call(struct i2c_client *client, u8 command, u8 length, u8 *values) |
||
1026 | { |
||
1027 | union i2c_smbus_data data; |
||
1028 | int i; |
||
1029 | if (length > I2C_SMBUS_BLOCK_MAX - 1) |
||
1030 | return -1; |
||
1031 | data.block[0] = length; |
||
1032 | for (i = 1; i <= length; i++) |
||
1033 | data.block[i] = values[i-1]; |
||
1034 | if(i2c_smbus_xfer(client->adapter,client->addr,client->flags, |
||
1035 | I2C_SMBUS_WRITE, command, |
||
1036 | I2C_SMBUS_BLOCK_PROC_CALL, &data)) |
||
1037 | return -1; |
||
1038 | for (i = 1; i <= data.block[0]; i++) |
||
1039 | values[i-1] = data.block[i]; |
||
1040 | return data.block[0]; |
||
1041 | } |
||
1042 | |||
1043 | /* Returns the number of read bytes */ |
||
1044 | s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command, u8 *values) |
||
1045 | { |
||
1046 | union i2c_smbus_data data; |
||
1047 | int i; |
||
1048 | if (i2c_smbus_xfer(client->adapter,client->addr,client->flags, |
||
1049 | I2C_SMBUS_READ,command, |
||
1050 | I2C_SMBUS_I2C_BLOCK_DATA,&data)) |
||
1051 | return -1; |
||
1052 | else { |
||
1053 | for (i = 1; i <= data.block[0]; i++) |
||
1054 | values[i-1] = data.block[i]; |
||
1055 | return data.block[0]; |
||
1056 | } |
||
1057 | } |
||
1058 | |||
1059 | s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command, u8 length, u8 *values) |
||
1060 | { |
||
1061 | union i2c_smbus_data data; |
||
1062 | int i; |
||
1063 | if (length > I2C_SMBUS_I2C_BLOCK_MAX) |
||
1064 | length = I2C_SMBUS_I2C_BLOCK_MAX; |
||
1065 | for (i = 1; i <= length; i++) |
||
1066 | data.block[i] = values[i-1]; |
||
1067 | data.block[0] = length; |
||
1068 | return i2c_smbus_xfer(client->adapter,client->addr,client->flags, |
||
1069 | I2C_SMBUS_WRITE,command, |
||
1070 | I2C_SMBUS_I2C_BLOCK_DATA,&data); |
||
1071 | } |
||
1072 | |||
1073 | /* Simulate a SMBus command using the i2c protocol |
||
1074 | No checking of parameters is done! */ |
||
1075 | static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr, |
||
1076 | unsigned short flags, |
||
1077 | char read_write, u8 command, int size, |
||
1078 | union i2c_smbus_data * data) |
||
1079 | { |
||
1080 | /* So we need to generate a series of msgs. In the case of writing, we |
||
1081 | need to use only one message; when reading, we need two. We initialize |
||
1082 | most things with sane defaults, to keep the code below somewhat |
||
1083 | simpler. */ |
||
1084 | unsigned char msgbuf0[34]; |
||
1085 | unsigned char msgbuf1[34]; |
||
1086 | int num = read_write == I2C_SMBUS_READ?2:1; |
||
1087 | struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 }, |
||
1088 | { addr, flags | I2C_M_RD, 0, msgbuf1 } |
||
1089 | }; |
||
1090 | int i; |
||
1091 | |||
1092 | msgbuf0[0] = command; |
||
1093 | switch(size) { |
||
1094 | case I2C_SMBUS_QUICK: |
||
1095 | msg[0].len = 0; |
||
1096 | /* Special case: The read/write field is used as data */ |
||
1097 | msg[0].flags = flags | (read_write==I2C_SMBUS_READ)?I2C_M_RD:0; |
||
1098 | num = 1; |
||
1099 | break; |
||
1100 | case I2C_SMBUS_BYTE: |
||
1101 | if (read_write == I2C_SMBUS_READ) { |
||
1102 | /* Special case: only a read! */ |
||
1103 | msg[0].flags = I2C_M_RD | flags; |
||
1104 | num = 1; |
||
1105 | } |
||
1106 | break; |
||
1107 | case I2C_SMBUS_BYTE_DATA: |
||
1108 | if (read_write == I2C_SMBUS_READ) |
||
1109 | msg[1].len = 1; |
||
1110 | else { |
||
1111 | msg[0].len = 2; |
||
1112 | msgbuf0[1] = data->byte; |
||
1113 | } |
||
1114 | break; |
||
1115 | case I2C_SMBUS_WORD_DATA: |
||
1116 | if (read_write == I2C_SMBUS_READ) |
||
1117 | msg[1].len = 2; |
||
1118 | else { |
||
1119 | msg[0].len=3; |
||
1120 | msgbuf0[1] = data->word & 0xff; |
||
1121 | msgbuf0[2] = (data->word >> 8) & 0xff; |
||
1122 | } |
||
1123 | break; |
||
1124 | case I2C_SMBUS_PROC_CALL: |
||
1125 | num = 2; /* Special case */ |
||
1126 | read_write = I2C_SMBUS_READ; |
||
1127 | msg[0].len = 3; |
||
1128 | msg[1].len = 2; |
||
1129 | msgbuf0[1] = data->word & 0xff; |
||
1130 | msgbuf0[2] = (data->word >> 8) & 0xff; |
||
1131 | break; |
||
1132 | case I2C_SMBUS_BLOCK_DATA: |
||
1133 | case I2C_SMBUS_BLOCK_DATA_PEC: |
||
1134 | if (read_write == I2C_SMBUS_READ) { |
||
1135 | printk(KERN_ERR "i2c-core.o: Block read not supported " |
||
1136 | "under I2C emulation!\n"); |
||
1137 | return -1; |
||
1138 | } else { |
||
1139 | msg[0].len = data->block[0] + 2; |
||
1140 | if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) { |
||
1141 | printk(KERN_ERR "i2c-core.o: smbus_access called with " |
||
1142 | "invalid block write size (%d)\n", |
||
1143 | data->block[0]); |
||
1144 | return -1; |
||
1145 | } |
||
1146 | if(size == I2C_SMBUS_BLOCK_DATA_PEC) |
||
1147 | (msg[0].len)++; |
||
1148 | for (i = 1; i <= msg[0].len; i++) |
||
1149 | msgbuf0[i] = data->block[i-1]; |
||
1150 | } |
||
1151 | break; |
||
1152 | case I2C_SMBUS_BLOCK_PROC_CALL: |
||
1153 | case I2C_SMBUS_BLOCK_PROC_CALL_PEC: |
||
1154 | printk(KERN_ERR "i2c-core.o: Block process call not supported " |
||
1155 | "under I2C emulation!\n"); |
||
1156 | return -1; |
||
1157 | case I2C_SMBUS_I2C_BLOCK_DATA: |
||
1158 | if (read_write == I2C_SMBUS_READ) { |
||
1159 | msg[1].len = I2C_SMBUS_I2C_BLOCK_MAX; |
||
1160 | } else { |
||
1161 | msg[0].len = data->block[0] + 1; |
||
1162 | if (msg[0].len > I2C_SMBUS_I2C_BLOCK_MAX + 1) { |
||
1163 | printk("i2c-core.o: i2c_smbus_xfer_emulated called with " |
||
1164 | "invalid block write size (%d)\n", |
||
1165 | data->block[0]); |
||
1166 | return -1; |
||
1167 | } |
||
1168 | for (i = 1; i <= data->block[0]; i++) |
||
1169 | msgbuf0[i] = data->block[i]; |
||
1170 | } |
||
1171 | break; |
||
1172 | default: |
||
1173 | printk(KERN_ERR "i2c-core.o: smbus_access called with invalid size (%d)\n", |
||
1174 | size); |
||
1175 | return -1; |
||
1176 | } |
||
1177 | |||
1178 | if (i2c_transfer(adapter, msg, num) < 0) |
||
1179 | return -1; |
||
1180 | |||
1181 | if (read_write == I2C_SMBUS_READ) |
||
1182 | switch(size) { |
||
1183 | case I2C_SMBUS_BYTE: |
||
1184 | data->byte = msgbuf0[0]; |
||
1185 | break; |
||
1186 | case I2C_SMBUS_BYTE_DATA: |
||
1187 | data->byte = msgbuf1[0]; |
||
1188 | break; |
||
1189 | case I2C_SMBUS_WORD_DATA: |
||
1190 | case I2C_SMBUS_PROC_CALL: |
||
1191 | data->word = msgbuf1[0] | (msgbuf1[1] << 8); |
||
1192 | break; |
||
1193 | case I2C_SMBUS_I2C_BLOCK_DATA: |
||
1194 | /* fixed at 32 for now */ |
||
1195 | data->block[0] = I2C_SMBUS_I2C_BLOCK_MAX; |
||
1196 | for (i = 0; i < I2C_SMBUS_I2C_BLOCK_MAX; i++) |
||
1197 | data->block[i+1] = msgbuf1[i]; |
||
1198 | break; |
||
1199 | } |
||
1200 | return 0; |
||
1201 | } |
||
1202 | |||
1203 | |||
1204 | s32 i2c_smbus_xfer(struct i2c_adapter * adapter, u16 addr, unsigned short flags, |
||
1205 | char read_write, u8 command, int size, |
||
1206 | union i2c_smbus_data * data) |
||
1207 | { |
||
1208 | s32 res; |
||
1209 | int swpec = 0; |
||
1210 | u8 partial = 0; |
||
1211 | |||
1212 | flags &= I2C_M_TEN | I2C_CLIENT_PEC; |
||
1213 | if((flags & I2C_CLIENT_PEC) && |
||
1214 | !(i2c_check_functionality(adapter, I2C_FUNC_SMBUS_HWPEC_CALC))) { |
||
1215 | swpec = 1; |
||
1216 | if(read_write == I2C_SMBUS_READ && |
||
1217 | size == I2C_SMBUS_BLOCK_DATA) |
||
1218 | size = I2C_SMBUS_BLOCK_DATA_PEC; |
||
1219 | else if(size == I2C_SMBUS_PROC_CALL) |
||
1220 | size = I2C_SMBUS_PROC_CALL_PEC; |
||
1221 | else if(size == I2C_SMBUS_BLOCK_PROC_CALL) { |
||
1222 | i2c_smbus_add_pec(addr, command, |
||
1223 | I2C_SMBUS_BLOCK_DATA, data); |
||
1224 | partial = data->block[data->block[0] + 1]; |
||
1225 | size = I2C_SMBUS_BLOCK_PROC_CALL_PEC; |
||
1226 | } else if(read_write == I2C_SMBUS_WRITE && |
||
1227 | size != I2C_SMBUS_QUICK && |
||
1228 | size != I2C_SMBUS_I2C_BLOCK_DATA) |
||
1229 | size = i2c_smbus_add_pec(addr, command, size, data); |
||
1230 | } |
||
1231 | |||
1232 | if (adapter->algo->smbus_xfer) { |
||
1233 | down(&adapter->bus_lock); |
||
1234 | res = adapter->algo->smbus_xfer(adapter,addr,flags,read_write, |
||
1235 | command,size,data); |
||
1236 | up(&adapter->bus_lock); |
||
1237 | } else |
||
1238 | res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write, |
||
1239 | command,size,data); |
||
1240 | |||
1241 | if(res >= 0 && swpec && |
||
1242 | size != I2C_SMBUS_QUICK && size != I2C_SMBUS_I2C_BLOCK_DATA && |
||
1243 | (read_write == I2C_SMBUS_READ || size == I2C_SMBUS_PROC_CALL_PEC || |
||
1244 | size == I2C_SMBUS_BLOCK_PROC_CALL_PEC)) { |
||
1245 | if(i2c_smbus_check_pec(addr, command, size, partial, data)) |
||
1246 | return -1; |
||
1247 | } |
||
1248 | return res; |
||
1249 | } |
||
1250 | |||
1251 | |||
1252 | /* You should always define `functionality'; the 'else' is just for |
||
1253 | backward compatibility. */ |
||
1254 | u32 i2c_get_functionality (struct i2c_adapter *adap) |
||
1255 | { |
||
1256 | if (adap->algo->functionality) |
||
1257 | return adap->algo->functionality(adap); |
||
1258 | else |
||
1259 | return 0xffffffff; |
||
1260 | } |
||
1261 | |||
1262 | int i2c_check_functionality (struct i2c_adapter *adap, u32 func) |
||
1263 | { |
||
1264 | u32 adap_func = i2c_get_functionality (adap); |
||
1265 | return (func & adap_func) == func; |
||
1266 | } |
||
1267 | |||
1268 | EXPORT_SYMBOL(i2c_add_adapter); |
||
1269 | EXPORT_SYMBOL(i2c_del_adapter); |
||
1270 | EXPORT_SYMBOL(i2c_add_driver); |
||
1271 | EXPORT_SYMBOL(i2c_del_driver); |
||
1272 | EXPORT_SYMBOL(i2c_attach_client); |
||
1273 | EXPORT_SYMBOL(i2c_detach_client); |
||
1274 | EXPORT_SYMBOL(i2c_use_client); |
||
1275 | EXPORT_SYMBOL(i2c_release_client); |
||
1276 | EXPORT_SYMBOL(i2c_clients_command); |
||
1277 | EXPORT_SYMBOL(i2c_check_addr); |
||
1278 | |||
1279 | EXPORT_SYMBOL(i2c_master_send); |
||
1280 | EXPORT_SYMBOL(i2c_master_recv); |
||
1281 | EXPORT_SYMBOL(i2c_control); |
||
1282 | EXPORT_SYMBOL(i2c_transfer); |
||
1283 | EXPORT_SYMBOL(i2c_adapter_id); |
||
1284 | EXPORT_SYMBOL(i2c_get_adapter); |
||
1285 | EXPORT_SYMBOL(i2c_put_adapter); |
||
1286 | EXPORT_SYMBOL(i2c_probe); |
||
1287 | |||
1288 | EXPORT_SYMBOL(i2c_smbus_xfer); |
||
1289 | EXPORT_SYMBOL(i2c_smbus_write_quick); |
||
1290 | EXPORT_SYMBOL(i2c_smbus_read_byte); |
||
1291 | EXPORT_SYMBOL(i2c_smbus_write_byte); |
||
1292 | EXPORT_SYMBOL(i2c_smbus_read_byte_data); |
||
1293 | EXPORT_SYMBOL(i2c_smbus_write_byte_data); |
||
1294 | EXPORT_SYMBOL(i2c_smbus_read_word_data); |
||
1295 | EXPORT_SYMBOL(i2c_smbus_write_word_data); |
||
1296 | EXPORT_SYMBOL(i2c_smbus_process_call); |
||
1297 | EXPORT_SYMBOL(i2c_smbus_read_block_data); |
||
1298 | EXPORT_SYMBOL(i2c_smbus_write_block_data); |
||
1299 | EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data); |
||
1300 | EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data); |
||
1301 | |||
1302 | EXPORT_SYMBOL(i2c_get_functionality); |
||
1303 | EXPORT_SYMBOL(i2c_check_functionality); |
||
1304 | |||
1305 | MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>"); |
||
1306 | MODULE_DESCRIPTION("I2C-Bus main module"); |
||
1307 | MODULE_LICENSE("GPL"); |
||
1308 | |||
1309 | MODULE_PARM(i2c_debug, "i"); |
||
1310 | MODULE_PARM_DESC(i2c_debug,"debug level"); |