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