]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
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 | ||
96de0e25 | 20 | /* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>. |
1da177e4 | 21 | All SMBus-related things are written by Frodo Looijaard <frodol@dds.nl> |
421ef47b JD |
22 | SMBus 2.0 support by Mark Studebaker <mdsxyz123@yahoo.com> and |
23 | Jean Delvare <khali@linux-fr.org> */ | |
1da177e4 | 24 | |
1da177e4 LT |
25 | #include <linux/module.h> |
26 | #include <linux/kernel.h> | |
27 | #include <linux/errno.h> | |
28 | #include <linux/slab.h> | |
29 | #include <linux/i2c.h> | |
30 | #include <linux/init.h> | |
31 | #include <linux/idr.h> | |
32 | #include <linux/seq_file.h> | |
d052d1be | 33 | #include <linux/platform_device.h> |
b3585e4f | 34 | #include <linux/mutex.h> |
b8d6f45b | 35 | #include <linux/completion.h> |
1da177e4 | 36 | #include <asm/uaccess.h> |
87c6c229 | 37 | #include <asm/semaphore.h> |
1da177e4 | 38 | |
9c1600ed DB |
39 | #include "i2c-core.h" |
40 | ||
1da177e4 | 41 | |
caada32a | 42 | static DEFINE_MUTEX(core_lock); |
1da177e4 LT |
43 | static DEFINE_IDR(i2c_adapter_idr); |
44 | ||
a1d9e6e4 | 45 | #define is_newstyle_driver(d) ((d)->probe || (d)->remove) |
f37dd80a DB |
46 | |
47 | /* ------------------------------------------------------------------------- */ | |
48 | ||
1da177e4 LT |
49 | static int i2c_device_match(struct device *dev, struct device_driver *drv) |
50 | { | |
7b4fbc50 DB |
51 | struct i2c_client *client = to_i2c_client(dev); |
52 | struct i2c_driver *driver = to_i2c_driver(drv); | |
53 | ||
54 | /* make legacy i2c drivers bypass driver model probing entirely; | |
55 | * such drivers scan each i2c adapter/bus themselves. | |
56 | */ | |
a1d9e6e4 | 57 | if (!is_newstyle_driver(driver)) |
7b4fbc50 DB |
58 | return 0; |
59 | ||
60 | /* new style drivers use the same kind of driver matching policy | |
61 | * as platform devices or SPI: compare device and driver IDs. | |
62 | */ | |
63 | return strcmp(client->driver_name, drv->name) == 0; | |
1da177e4 LT |
64 | } |
65 | ||
7b4fbc50 DB |
66 | #ifdef CONFIG_HOTPLUG |
67 | ||
68 | /* uevent helps with hotplug: modprobe -q $(MODALIAS) */ | |
7eff2e7a | 69 | static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env) |
7b4fbc50 DB |
70 | { |
71 | struct i2c_client *client = to_i2c_client(dev); | |
7b4fbc50 DB |
72 | |
73 | /* by definition, legacy drivers can't hotplug */ | |
74 | if (dev->driver || !client->driver_name) | |
75 | return 0; | |
76 | ||
7eff2e7a | 77 | if (add_uevent_var(env, "MODALIAS=%s", client->driver_name)) |
7b4fbc50 | 78 | return -ENOMEM; |
7b4fbc50 DB |
79 | dev_dbg(dev, "uevent\n"); |
80 | return 0; | |
81 | } | |
82 | ||
83 | #else | |
84 | #define i2c_device_uevent NULL | |
85 | #endif /* CONFIG_HOTPLUG */ | |
86 | ||
f37dd80a | 87 | static int i2c_device_probe(struct device *dev) |
1da177e4 | 88 | { |
7b4fbc50 DB |
89 | struct i2c_client *client = to_i2c_client(dev); |
90 | struct i2c_driver *driver = to_i2c_driver(dev->driver); | |
91 | ||
92 | if (!driver->probe) | |
93 | return -ENODEV; | |
94 | client->driver = driver; | |
95 | dev_dbg(dev, "probe\n"); | |
96 | return driver->probe(client); | |
f37dd80a | 97 | } |
1da177e4 | 98 | |
f37dd80a DB |
99 | static int i2c_device_remove(struct device *dev) |
100 | { | |
a1d9e6e4 DB |
101 | struct i2c_client *client = to_i2c_client(dev); |
102 | struct i2c_driver *driver; | |
103 | int status; | |
104 | ||
105 | if (!dev->driver) | |
106 | return 0; | |
107 | ||
108 | driver = to_i2c_driver(dev->driver); | |
109 | if (driver->remove) { | |
110 | dev_dbg(dev, "remove\n"); | |
111 | status = driver->remove(client); | |
112 | } else { | |
113 | dev->driver = NULL; | |
114 | status = 0; | |
115 | } | |
116 | if (status == 0) | |
117 | client->driver = NULL; | |
118 | return status; | |
1da177e4 LT |
119 | } |
120 | ||
f37dd80a | 121 | static void i2c_device_shutdown(struct device *dev) |
1da177e4 | 122 | { |
f37dd80a DB |
123 | struct i2c_driver *driver; |
124 | ||
125 | if (!dev->driver) | |
126 | return; | |
127 | driver = to_i2c_driver(dev->driver); | |
128 | if (driver->shutdown) | |
129 | driver->shutdown(to_i2c_client(dev)); | |
1da177e4 LT |
130 | } |
131 | ||
f37dd80a | 132 | static int i2c_device_suspend(struct device * dev, pm_message_t mesg) |
1da177e4 | 133 | { |
f37dd80a DB |
134 | struct i2c_driver *driver; |
135 | ||
136 | if (!dev->driver) | |
137 | return 0; | |
138 | driver = to_i2c_driver(dev->driver); | |
139 | if (!driver->suspend) | |
140 | return 0; | |
141 | return driver->suspend(to_i2c_client(dev), mesg); | |
1da177e4 LT |
142 | } |
143 | ||
f37dd80a | 144 | static int i2c_device_resume(struct device * dev) |
1da177e4 | 145 | { |
f37dd80a DB |
146 | struct i2c_driver *driver; |
147 | ||
148 | if (!dev->driver) | |
149 | return 0; | |
150 | driver = to_i2c_driver(dev->driver); | |
151 | if (!driver->resume) | |
152 | return 0; | |
153 | return driver->resume(to_i2c_client(dev)); | |
1da177e4 LT |
154 | } |
155 | ||
7b4fbc50 DB |
156 | static void i2c_client_release(struct device *dev) |
157 | { | |
158 | struct i2c_client *client = to_i2c_client(dev); | |
159 | complete(&client->released); | |
160 | } | |
161 | ||
9c1600ed DB |
162 | static void i2c_client_dev_release(struct device *dev) |
163 | { | |
164 | kfree(to_i2c_client(dev)); | |
165 | } | |
166 | ||
7b4fbc50 DB |
167 | static ssize_t show_client_name(struct device *dev, struct device_attribute *attr, char *buf) |
168 | { | |
169 | struct i2c_client *client = to_i2c_client(dev); | |
170 | return sprintf(buf, "%s\n", client->name); | |
171 | } | |
172 | ||
173 | static ssize_t show_modalias(struct device *dev, struct device_attribute *attr, char *buf) | |
174 | { | |
175 | struct i2c_client *client = to_i2c_client(dev); | |
176 | return client->driver_name | |
177 | ? sprintf(buf, "%s\n", client->driver_name) | |
178 | : 0; | |
179 | } | |
180 | ||
181 | static struct device_attribute i2c_dev_attrs[] = { | |
182 | __ATTR(name, S_IRUGO, show_client_name, NULL), | |
183 | /* modalias helps coldplug: modprobe $(cat .../modalias) */ | |
184 | __ATTR(modalias, S_IRUGO, show_modalias, NULL), | |
185 | { }, | |
186 | }; | |
187 | ||
83eaaed0 | 188 | static struct bus_type i2c_bus_type = { |
f37dd80a | 189 | .name = "i2c", |
7b4fbc50 | 190 | .dev_attrs = i2c_dev_attrs, |
f37dd80a | 191 | .match = i2c_device_match, |
7b4fbc50 | 192 | .uevent = i2c_device_uevent, |
f37dd80a DB |
193 | .probe = i2c_device_probe, |
194 | .remove = i2c_device_remove, | |
195 | .shutdown = i2c_device_shutdown, | |
196 | .suspend = i2c_device_suspend, | |
197 | .resume = i2c_device_resume, | |
b864c7d5 RK |
198 | }; |
199 | ||
9c1600ed DB |
200 | /** |
201 | * i2c_new_device - instantiate an i2c device for use with a new style driver | |
202 | * @adap: the adapter managing the device | |
203 | * @info: describes one I2C device; bus_num is ignored | |
d64f73be | 204 | * Context: can sleep |
9c1600ed DB |
205 | * |
206 | * Create a device to work with a new style i2c driver, where binding is | |
207 | * handled through driver model probe()/remove() methods. This call is not | |
208 | * appropriate for use by mainboad initialization logic, which usually runs | |
209 | * during an arch_initcall() long before any i2c_adapter could exist. | |
210 | * | |
211 | * This returns the new i2c client, which may be saved for later use with | |
212 | * i2c_unregister_device(); or NULL to indicate an error. | |
213 | */ | |
214 | struct i2c_client * | |
215 | i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info) | |
216 | { | |
217 | struct i2c_client *client; | |
218 | int status; | |
219 | ||
220 | client = kzalloc(sizeof *client, GFP_KERNEL); | |
221 | if (!client) | |
222 | return NULL; | |
223 | ||
224 | client->adapter = adap; | |
225 | ||
226 | client->dev.platform_data = info->platform_data; | |
3bbb835d DB |
227 | device_init_wakeup(&client->dev, info->flags & I2C_CLIENT_WAKE); |
228 | ||
229 | client->flags = info->flags & ~I2C_CLIENT_WAKE; | |
9c1600ed DB |
230 | client->addr = info->addr; |
231 | client->irq = info->irq; | |
232 | ||
233 | strlcpy(client->driver_name, info->driver_name, | |
234 | sizeof(client->driver_name)); | |
235 | strlcpy(client->name, info->type, sizeof(client->name)); | |
236 | ||
237 | /* a new style driver may be bound to this device when we | |
238 | * return from this function, or any later moment (e.g. maybe | |
239 | * hotplugging will load the driver module). and the device | |
240 | * refcount model is the standard driver model one. | |
241 | */ | |
242 | status = i2c_attach_client(client); | |
243 | if (status < 0) { | |
244 | kfree(client); | |
245 | client = NULL; | |
246 | } | |
247 | return client; | |
248 | } | |
249 | EXPORT_SYMBOL_GPL(i2c_new_device); | |
250 | ||
251 | ||
252 | /** | |
253 | * i2c_unregister_device - reverse effect of i2c_new_device() | |
254 | * @client: value returned from i2c_new_device() | |
d64f73be | 255 | * Context: can sleep |
9c1600ed DB |
256 | */ |
257 | void i2c_unregister_device(struct i2c_client *client) | |
a1d9e6e4 DB |
258 | { |
259 | struct i2c_adapter *adapter = client->adapter; | |
260 | struct i2c_driver *driver = client->driver; | |
261 | ||
262 | if (driver && !is_newstyle_driver(driver)) { | |
263 | dev_err(&client->dev, "can't unregister devices " | |
264 | "with legacy drivers\n"); | |
265 | WARN_ON(1); | |
266 | return; | |
267 | } | |
268 | ||
269 | mutex_lock(&adapter->clist_lock); | |
270 | list_del(&client->list); | |
271 | mutex_unlock(&adapter->clist_lock); | |
272 | ||
273 | device_unregister(&client->dev); | |
274 | } | |
9c1600ed | 275 | EXPORT_SYMBOL_GPL(i2c_unregister_device); |
a1d9e6e4 DB |
276 | |
277 | ||
f37dd80a DB |
278 | /* ------------------------------------------------------------------------- */ |
279 | ||
16ffadfc DB |
280 | /* I2C bus adapters -- one roots each I2C or SMBUS segment */ |
281 | ||
83eaaed0 | 282 | static void i2c_adapter_dev_release(struct device *dev) |
1da177e4 | 283 | { |
ef2c8321 | 284 | struct i2c_adapter *adap = to_i2c_adapter(dev); |
1da177e4 LT |
285 | complete(&adap->dev_released); |
286 | } | |
287 | ||
16ffadfc DB |
288 | static ssize_t |
289 | show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf) | |
290 | { | |
ef2c8321 | 291 | struct i2c_adapter *adap = to_i2c_adapter(dev); |
16ffadfc DB |
292 | return sprintf(buf, "%s\n", adap->name); |
293 | } | |
b119dc3f | 294 | |
16ffadfc DB |
295 | static struct device_attribute i2c_adapter_attrs[] = { |
296 | __ATTR(name, S_IRUGO, show_adapter_name, NULL), | |
297 | { }, | |
298 | }; | |
b119dc3f | 299 | |
83eaaed0 | 300 | static struct class i2c_adapter_class = { |
b119dc3f DB |
301 | .owner = THIS_MODULE, |
302 | .name = "i2c-adapter", | |
16ffadfc | 303 | .dev_attrs = i2c_adapter_attrs, |
1da177e4 LT |
304 | }; |
305 | ||
9c1600ed DB |
306 | static void i2c_scan_static_board_info(struct i2c_adapter *adapter) |
307 | { | |
308 | struct i2c_devinfo *devinfo; | |
309 | ||
310 | mutex_lock(&__i2c_board_lock); | |
311 | list_for_each_entry(devinfo, &__i2c_board_list, list) { | |
312 | if (devinfo->busnum == adapter->nr | |
313 | && !i2c_new_device(adapter, | |
314 | &devinfo->board_info)) | |
315 | printk(KERN_ERR "i2c-core: can't create i2c%d-%04x\n", | |
316 | i2c_adapter_id(adapter), | |
317 | devinfo->board_info.addr); | |
318 | } | |
319 | mutex_unlock(&__i2c_board_lock); | |
320 | } | |
321 | ||
026526f5 JD |
322 | static int i2c_do_add_adapter(struct device_driver *d, void *data) |
323 | { | |
324 | struct i2c_driver *driver = to_i2c_driver(d); | |
325 | struct i2c_adapter *adap = data; | |
326 | ||
327 | if (driver->attach_adapter) { | |
328 | /* We ignore the return code; if it fails, too bad */ | |
329 | driver->attach_adapter(adap); | |
330 | } | |
331 | return 0; | |
332 | } | |
333 | ||
6e13e641 | 334 | static int i2c_register_adapter(struct i2c_adapter *adap) |
1da177e4 | 335 | { |
026526f5 | 336 | int res = 0, dummy; |
1da177e4 | 337 | |
5c085d36 IM |
338 | mutex_init(&adap->bus_lock); |
339 | mutex_init(&adap->clist_lock); | |
1da177e4 LT |
340 | INIT_LIST_HEAD(&adap->clients); |
341 | ||
caada32a | 342 | mutex_lock(&core_lock); |
6e13e641 | 343 | |
1da177e4 LT |
344 | /* Add the adapter to the driver core. |
345 | * If the parent pointer is not set up, | |
346 | * we add this adapter to the host bus. | |
347 | */ | |
b119dc3f | 348 | if (adap->dev.parent == NULL) { |
1da177e4 | 349 | adap->dev.parent = &platform_bus; |
fe2c8d51 JD |
350 | pr_debug("I2C adapter driver [%s] forgot to specify " |
351 | "physical device\n", adap->name); | |
b119dc3f | 352 | } |
1da177e4 | 353 | sprintf(adap->dev.bus_id, "i2c-%d", adap->nr); |
1da177e4 | 354 | adap->dev.release = &i2c_adapter_dev_release; |
fccb56e4 | 355 | adap->dev.class = &i2c_adapter_class; |
b119c6c9 JD |
356 | res = device_register(&adap->dev); |
357 | if (res) | |
358 | goto out_list; | |
1da177e4 | 359 | |
b6d7b3d1 JD |
360 | dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name); |
361 | ||
6e13e641 DB |
362 | /* create pre-declared device nodes for new-style drivers */ |
363 | if (adap->nr < __i2c_first_dynamic_bus_num) | |
364 | i2c_scan_static_board_info(adap); | |
365 | ||
7b4fbc50 | 366 | /* let legacy drivers scan this bus for matching devices */ |
026526f5 JD |
367 | dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap, |
368 | i2c_do_add_adapter); | |
1da177e4 | 369 | |
1da177e4 | 370 | out_unlock: |
caada32a | 371 | mutex_unlock(&core_lock); |
1da177e4 | 372 | return res; |
b119c6c9 | 373 | |
b119c6c9 | 374 | out_list: |
b119c6c9 JD |
375 | idr_remove(&i2c_adapter_idr, adap->nr); |
376 | goto out_unlock; | |
1da177e4 LT |
377 | } |
378 | ||
6e13e641 DB |
379 | /** |
380 | * i2c_add_adapter - declare i2c adapter, use dynamic bus number | |
381 | * @adapter: the adapter to add | |
d64f73be | 382 | * Context: can sleep |
6e13e641 DB |
383 | * |
384 | * This routine is used to declare an I2C adapter when its bus number | |
385 | * doesn't matter. Examples: for I2C adapters dynamically added by | |
386 | * USB links or PCI plugin cards. | |
387 | * | |
388 | * When this returns zero, a new bus number was allocated and stored | |
389 | * in adap->nr, and the specified adapter became available for clients. | |
390 | * Otherwise, a negative errno value is returned. | |
391 | */ | |
392 | int i2c_add_adapter(struct i2c_adapter *adapter) | |
393 | { | |
394 | int id, res = 0; | |
395 | ||
396 | retry: | |
397 | if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0) | |
398 | return -ENOMEM; | |
399 | ||
caada32a | 400 | mutex_lock(&core_lock); |
6e13e641 DB |
401 | /* "above" here means "above or equal to", sigh */ |
402 | res = idr_get_new_above(&i2c_adapter_idr, adapter, | |
403 | __i2c_first_dynamic_bus_num, &id); | |
caada32a | 404 | mutex_unlock(&core_lock); |
6e13e641 DB |
405 | |
406 | if (res < 0) { | |
407 | if (res == -EAGAIN) | |
408 | goto retry; | |
409 | return res; | |
410 | } | |
411 | ||
412 | adapter->nr = id; | |
413 | return i2c_register_adapter(adapter); | |
414 | } | |
415 | EXPORT_SYMBOL(i2c_add_adapter); | |
416 | ||
417 | /** | |
418 | * i2c_add_numbered_adapter - declare i2c adapter, use static bus number | |
419 | * @adap: the adapter to register (with adap->nr initialized) | |
d64f73be | 420 | * Context: can sleep |
6e13e641 DB |
421 | * |
422 | * This routine is used to declare an I2C adapter when its bus number | |
423 | * matters. Example: for I2C adapters from system-on-chip CPUs, or | |
424 | * otherwise built in to the system's mainboard, and where i2c_board_info | |
425 | * is used to properly configure I2C devices. | |
426 | * | |
427 | * If no devices have pre-been declared for this bus, then be sure to | |
428 | * register the adapter before any dynamically allocated ones. Otherwise | |
429 | * the required bus ID may not be available. | |
430 | * | |
431 | * When this returns zero, the specified adapter became available for | |
432 | * clients using the bus number provided in adap->nr. Also, the table | |
433 | * of I2C devices pre-declared using i2c_register_board_info() is scanned, | |
434 | * and the appropriate driver model device nodes are created. Otherwise, a | |
435 | * negative errno value is returned. | |
436 | */ | |
437 | int i2c_add_numbered_adapter(struct i2c_adapter *adap) | |
438 | { | |
439 | int id; | |
440 | int status; | |
441 | ||
442 | if (adap->nr & ~MAX_ID_MASK) | |
443 | return -EINVAL; | |
444 | ||
445 | retry: | |
446 | if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0) | |
447 | return -ENOMEM; | |
448 | ||
caada32a | 449 | mutex_lock(&core_lock); |
6e13e641 DB |
450 | /* "above" here means "above or equal to", sigh; |
451 | * we need the "equal to" result to force the result | |
452 | */ | |
453 | status = idr_get_new_above(&i2c_adapter_idr, adap, adap->nr, &id); | |
454 | if (status == 0 && id != adap->nr) { | |
455 | status = -EBUSY; | |
456 | idr_remove(&i2c_adapter_idr, id); | |
457 | } | |
caada32a | 458 | mutex_unlock(&core_lock); |
6e13e641 DB |
459 | if (status == -EAGAIN) |
460 | goto retry; | |
461 | ||
462 | if (status == 0) | |
463 | status = i2c_register_adapter(adap); | |
464 | return status; | |
465 | } | |
466 | EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter); | |
467 | ||
026526f5 JD |
468 | static int i2c_do_del_adapter(struct device_driver *d, void *data) |
469 | { | |
470 | struct i2c_driver *driver = to_i2c_driver(d); | |
471 | struct i2c_adapter *adapter = data; | |
472 | int res; | |
473 | ||
474 | if (!driver->detach_adapter) | |
475 | return 0; | |
476 | res = driver->detach_adapter(adapter); | |
477 | if (res) | |
478 | dev_err(&adapter->dev, "detach_adapter failed (%d) " | |
479 | "for driver [%s]\n", res, driver->driver.name); | |
480 | return res; | |
481 | } | |
482 | ||
d64f73be DB |
483 | /** |
484 | * i2c_del_adapter - unregister I2C adapter | |
485 | * @adap: the adapter being unregistered | |
486 | * Context: can sleep | |
487 | * | |
488 | * This unregisters an I2C adapter which was previously registered | |
489 | * by @i2c_add_adapter or @i2c_add_numbered_adapter. | |
490 | */ | |
1da177e4 LT |
491 | int i2c_del_adapter(struct i2c_adapter *adap) |
492 | { | |
493 | struct list_head *item, *_n; | |
1da177e4 LT |
494 | struct i2c_client *client; |
495 | int res = 0; | |
496 | ||
caada32a | 497 | mutex_lock(&core_lock); |
1da177e4 LT |
498 | |
499 | /* First make sure that this adapter was ever added */ | |
87c6c229 | 500 | if (idr_find(&i2c_adapter_idr, adap->nr) != adap) { |
b6d7b3d1 JD |
501 | pr_debug("i2c-core: attempting to delete unregistered " |
502 | "adapter [%s]\n", adap->name); | |
1da177e4 LT |
503 | res = -EINVAL; |
504 | goto out_unlock; | |
505 | } | |
506 | ||
026526f5 JD |
507 | /* Tell drivers about this removal */ |
508 | res = bus_for_each_drv(&i2c_bus_type, NULL, adap, | |
509 | i2c_do_del_adapter); | |
510 | if (res) | |
511 | goto out_unlock; | |
1da177e4 LT |
512 | |
513 | /* detach any active clients. This must be done first, because | |
a551acc2 | 514 | * it can fail; in which case we give up. */ |
1da177e4 | 515 | list_for_each_safe(item, _n, &adap->clients) { |
a1d9e6e4 DB |
516 | struct i2c_driver *driver; |
517 | ||
1da177e4 | 518 | client = list_entry(item, struct i2c_client, list); |
a1d9e6e4 DB |
519 | driver = client->driver; |
520 | ||
521 | /* new style, follow standard driver model */ | |
522 | if (!driver || is_newstyle_driver(driver)) { | |
523 | i2c_unregister_device(client); | |
524 | continue; | |
525 | } | |
1da177e4 | 526 | |
a1d9e6e4 DB |
527 | /* legacy drivers create and remove clients themselves */ |
528 | if ((res = driver->detach_client(client))) { | |
b6d7b3d1 JD |
529 | dev_err(&adap->dev, "detach_client failed for client " |
530 | "[%s] at address 0x%02x\n", client->name, | |
1da177e4 LT |
531 | client->addr); |
532 | goto out_unlock; | |
533 | } | |
534 | } | |
535 | ||
536 | /* clean up the sysfs representation */ | |
537 | init_completion(&adap->dev_released); | |
1da177e4 | 538 | device_unregister(&adap->dev); |
1da177e4 LT |
539 | |
540 | /* wait for sysfs to drop all references */ | |
541 | wait_for_completion(&adap->dev_released); | |
1da177e4 | 542 | |
6e13e641 | 543 | /* free bus id */ |
1da177e4 LT |
544 | idr_remove(&i2c_adapter_idr, adap->nr); |
545 | ||
b6d7b3d1 | 546 | dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name); |
1da177e4 LT |
547 | |
548 | out_unlock: | |
caada32a | 549 | mutex_unlock(&core_lock); |
1da177e4 LT |
550 | return res; |
551 | } | |
c0564606 | 552 | EXPORT_SYMBOL(i2c_del_adapter); |
1da177e4 LT |
553 | |
554 | ||
7b4fbc50 DB |
555 | /* ------------------------------------------------------------------------- */ |
556 | ||
557 | /* | |
558 | * An i2c_driver is used with one or more i2c_client (device) nodes to access | |
559 | * i2c slave chips, on a bus instance associated with some i2c_adapter. There | |
560 | * are two models for binding the driver to its device: "new style" drivers | |
561 | * follow the standard Linux driver model and just respond to probe() calls | |
562 | * issued if the driver core sees they match(); "legacy" drivers create device | |
563 | * nodes themselves. | |
1da177e4 LT |
564 | */ |
565 | ||
de59cf9e | 566 | int i2c_register_driver(struct module *owner, struct i2c_driver *driver) |
1da177e4 | 567 | { |
7eebcb7c | 568 | int res; |
1da177e4 | 569 | |
7b4fbc50 | 570 | /* new style driver methods can't mix with legacy ones */ |
a1d9e6e4 | 571 | if (is_newstyle_driver(driver)) { |
7b4fbc50 DB |
572 | if (driver->attach_adapter || driver->detach_adapter |
573 | || driver->detach_client) { | |
574 | printk(KERN_WARNING | |
575 | "i2c-core: driver [%s] is confused\n", | |
576 | driver->driver.name); | |
577 | return -EINVAL; | |
578 | } | |
579 | } | |
580 | ||
1da177e4 | 581 | /* add the driver to the list of i2c drivers in the driver core */ |
de59cf9e | 582 | driver->driver.owner = owner; |
1da177e4 | 583 | driver->driver.bus = &i2c_bus_type; |
1da177e4 | 584 | |
6e13e641 DB |
585 | /* for new style drivers, when registration returns the driver core |
586 | * will have called probe() for all matching-but-unbound devices. | |
587 | */ | |
1da177e4 LT |
588 | res = driver_register(&driver->driver); |
589 | if (res) | |
7eebcb7c | 590 | return res; |
438d6c2c | 591 | |
caada32a | 592 | mutex_lock(&core_lock); |
7eebcb7c | 593 | |
35d8b2e6 | 594 | pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name); |
1da177e4 | 595 | |
7b4fbc50 | 596 | /* legacy drivers scan i2c busses directly */ |
8a994755 | 597 | if (driver->attach_adapter) { |
4ad4eac6 DB |
598 | struct i2c_adapter *adapter; |
599 | ||
87c6c229 JD |
600 | down(&i2c_adapter_class.sem); |
601 | list_for_each_entry(adapter, &i2c_adapter_class.devices, | |
602 | dev.node) { | |
1da177e4 LT |
603 | driver->attach_adapter(adapter); |
604 | } | |
87c6c229 | 605 | up(&i2c_adapter_class.sem); |
1da177e4 LT |
606 | } |
607 | ||
caada32a | 608 | mutex_unlock(&core_lock); |
7eebcb7c | 609 | return 0; |
1da177e4 | 610 | } |
de59cf9e | 611 | EXPORT_SYMBOL(i2c_register_driver); |
1da177e4 | 612 | |
a1d9e6e4 DB |
613 | /** |
614 | * i2c_del_driver - unregister I2C driver | |
615 | * @driver: the driver being unregistered | |
d64f73be | 616 | * Context: can sleep |
a1d9e6e4 | 617 | */ |
b3e82096 | 618 | void i2c_del_driver(struct i2c_driver *driver) |
1da177e4 | 619 | { |
87c6c229 | 620 | struct list_head *item2, *_n; |
1da177e4 LT |
621 | struct i2c_client *client; |
622 | struct i2c_adapter *adap; | |
438d6c2c | 623 | |
caada32a | 624 | mutex_lock(&core_lock); |
1da177e4 | 625 | |
a1d9e6e4 DB |
626 | /* new-style driver? */ |
627 | if (is_newstyle_driver(driver)) | |
628 | goto unregister; | |
629 | ||
1da177e4 | 630 | /* Have a look at each adapter, if clients of this driver are still |
438d6c2c | 631 | * attached. If so, detach them to be able to kill the driver |
1da177e4 | 632 | * afterwards. |
1da177e4 | 633 | */ |
87c6c229 JD |
634 | down(&i2c_adapter_class.sem); |
635 | list_for_each_entry(adap, &i2c_adapter_class.devices, dev.node) { | |
1da177e4 | 636 | if (driver->detach_adapter) { |
b3e82096 | 637 | if (driver->detach_adapter(adap)) { |
b6d7b3d1 | 638 | dev_err(&adap->dev, "detach_adapter failed " |
35d8b2e6 LR |
639 | "for driver [%s]\n", |
640 | driver->driver.name); | |
1da177e4 LT |
641 | } |
642 | } else { | |
643 | list_for_each_safe(item2, _n, &adap->clients) { | |
644 | client = list_entry(item2, struct i2c_client, list); | |
645 | if (client->driver != driver) | |
646 | continue; | |
b6d7b3d1 JD |
647 | dev_dbg(&adap->dev, "detaching client [%s] " |
648 | "at 0x%02x\n", client->name, | |
649 | client->addr); | |
b3e82096 | 650 | if (driver->detach_client(client)) { |
b6d7b3d1 JD |
651 | dev_err(&adap->dev, "detach_client " |
652 | "failed for client [%s] at " | |
653 | "0x%02x\n", client->name, | |
1da177e4 | 654 | client->addr); |
1da177e4 LT |
655 | } |
656 | } | |
657 | } | |
658 | } | |
87c6c229 | 659 | up(&i2c_adapter_class.sem); |
1da177e4 | 660 | |
a1d9e6e4 | 661 | unregister: |
1da177e4 | 662 | driver_unregister(&driver->driver); |
35d8b2e6 | 663 | pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name); |
1da177e4 | 664 | |
caada32a | 665 | mutex_unlock(&core_lock); |
1da177e4 | 666 | } |
c0564606 | 667 | EXPORT_SYMBOL(i2c_del_driver); |
1da177e4 | 668 | |
7b4fbc50 DB |
669 | /* ------------------------------------------------------------------------- */ |
670 | ||
1da177e4 LT |
671 | static int __i2c_check_addr(struct i2c_adapter *adapter, unsigned int addr) |
672 | { | |
673 | struct list_head *item; | |
674 | struct i2c_client *client; | |
675 | ||
676 | list_for_each(item,&adapter->clients) { | |
677 | client = list_entry(item, struct i2c_client, list); | |
678 | if (client->addr == addr) | |
679 | return -EBUSY; | |
680 | } | |
681 | return 0; | |
682 | } | |
683 | ||
5e31c2bd | 684 | static int i2c_check_addr(struct i2c_adapter *adapter, int addr) |
1da177e4 LT |
685 | { |
686 | int rval; | |
687 | ||
5c085d36 | 688 | mutex_lock(&adapter->clist_lock); |
1da177e4 | 689 | rval = __i2c_check_addr(adapter, addr); |
5c085d36 | 690 | mutex_unlock(&adapter->clist_lock); |
1da177e4 LT |
691 | |
692 | return rval; | |
693 | } | |
694 | ||
695 | int i2c_attach_client(struct i2c_client *client) | |
696 | { | |
697 | struct i2c_adapter *adapter = client->adapter; | |
b119c6c9 | 698 | int res = 0; |
1da177e4 | 699 | |
5c085d36 | 700 | mutex_lock(&adapter->clist_lock); |
1da177e4 | 701 | if (__i2c_check_addr(client->adapter, client->addr)) { |
b119c6c9 JD |
702 | res = -EBUSY; |
703 | goto out_unlock; | |
1da177e4 LT |
704 | } |
705 | list_add_tail(&client->list,&adapter->clients); | |
438d6c2c | 706 | |
1da177e4 | 707 | client->dev.parent = &client->adapter->dev; |
1da177e4 | 708 | client->dev.bus = &i2c_bus_type; |
9c1600ed DB |
709 | |
710 | if (client->driver) | |
711 | client->dev.driver = &client->driver->driver; | |
712 | ||
de81d2aa | 713 | if (client->driver && !is_newstyle_driver(client->driver)) { |
9c1600ed | 714 | client->dev.release = i2c_client_release; |
de81d2aa DB |
715 | client->dev.uevent_suppress = 1; |
716 | } else | |
9c1600ed | 717 | client->dev.release = i2c_client_dev_release; |
438d6c2c | 718 | |
1da177e4 LT |
719 | snprintf(&client->dev.bus_id[0], sizeof(client->dev.bus_id), |
720 | "%d-%04x", i2c_adapter_id(adapter), client->addr); | |
b6d7b3d1 JD |
721 | dev_dbg(&adapter->dev, "client [%s] registered with bus id %s\n", |
722 | client->name, client->dev.bus_id); | |
b119c6c9 JD |
723 | res = device_register(&client->dev); |
724 | if (res) | |
725 | goto out_list; | |
b119c6c9 | 726 | mutex_unlock(&adapter->clist_lock); |
77ed74da JD |
727 | |
728 | if (adapter->client_register) { | |
729 | if (adapter->client_register(client)) { | |
730 | dev_dbg(&adapter->dev, "client_register " | |
731 | "failed for client [%s] at 0x%02x\n", | |
732 | client->name, client->addr); | |
733 | } | |
734 | } | |
735 | ||
736 | return 0; | |
b119c6c9 | 737 | |
b119c6c9 JD |
738 | out_list: |
739 | list_del(&client->list); | |
740 | dev_err(&adapter->dev, "Failed to attach i2c client %s at 0x%02x " | |
741 | "(%d)\n", client->name, client->addr, res); | |
77ed74da JD |
742 | out_unlock: |
743 | mutex_unlock(&adapter->clist_lock); | |
744 | return res; | |
1da177e4 | 745 | } |
c0564606 | 746 | EXPORT_SYMBOL(i2c_attach_client); |
1da177e4 LT |
747 | |
748 | int i2c_detach_client(struct i2c_client *client) | |
749 | { | |
750 | struct i2c_adapter *adapter = client->adapter; | |
751 | int res = 0; | |
438d6c2c | 752 | |
1da177e4 LT |
753 | if (adapter->client_unregister) { |
754 | res = adapter->client_unregister(client); | |
755 | if (res) { | |
756 | dev_err(&client->dev, | |
86749e85 JD |
757 | "client_unregister [%s] failed, " |
758 | "client not detached\n", client->name); | |
1da177e4 LT |
759 | goto out; |
760 | } | |
761 | } | |
762 | ||
5c085d36 | 763 | mutex_lock(&adapter->clist_lock); |
1da177e4 LT |
764 | list_del(&client->list); |
765 | init_completion(&client->released); | |
1da177e4 | 766 | device_unregister(&client->dev); |
5c085d36 | 767 | mutex_unlock(&adapter->clist_lock); |
1da177e4 LT |
768 | wait_for_completion(&client->released); |
769 | ||
770 | out: | |
771 | return res; | |
772 | } | |
c0564606 | 773 | EXPORT_SYMBOL(i2c_detach_client); |
1da177e4 | 774 | |
e48d3319 JD |
775 | /** |
776 | * i2c_use_client - increments the reference count of the i2c client structure | |
777 | * @client: the client being referenced | |
778 | * | |
779 | * Each live reference to a client should be refcounted. The driver model does | |
780 | * that automatically as part of driver binding, so that most drivers don't | |
781 | * need to do this explicitly: they hold a reference until they're unbound | |
782 | * from the device. | |
783 | * | |
784 | * A pointer to the client with the incremented reference counter is returned. | |
785 | */ | |
786 | struct i2c_client *i2c_use_client(struct i2c_client *client) | |
1da177e4 | 787 | { |
bdc511f4 | 788 | get_device(&client->dev); |
e48d3319 | 789 | return client; |
1da177e4 | 790 | } |
c0564606 | 791 | EXPORT_SYMBOL(i2c_use_client); |
1da177e4 | 792 | |
e48d3319 JD |
793 | /** |
794 | * i2c_release_client - release a use of the i2c client structure | |
795 | * @client: the client being no longer referenced | |
796 | * | |
797 | * Must be called when a user of a client is finished with it. | |
798 | */ | |
799 | void i2c_release_client(struct i2c_client *client) | |
1da177e4 | 800 | { |
bdc511f4 | 801 | put_device(&client->dev); |
1da177e4 | 802 | } |
c0564606 | 803 | EXPORT_SYMBOL(i2c_release_client); |
1da177e4 LT |
804 | |
805 | void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg) | |
806 | { | |
807 | struct list_head *item; | |
808 | struct i2c_client *client; | |
809 | ||
5c085d36 | 810 | mutex_lock(&adap->clist_lock); |
1da177e4 LT |
811 | list_for_each(item,&adap->clients) { |
812 | client = list_entry(item, struct i2c_client, list); | |
35d8b2e6 | 813 | if (!try_module_get(client->driver->driver.owner)) |
1da177e4 LT |
814 | continue; |
815 | if (NULL != client->driver->command) { | |
5c085d36 | 816 | mutex_unlock(&adap->clist_lock); |
1da177e4 | 817 | client->driver->command(client,cmd,arg); |
5c085d36 | 818 | mutex_lock(&adap->clist_lock); |
1da177e4 | 819 | } |
35d8b2e6 | 820 | module_put(client->driver->driver.owner); |
1da177e4 | 821 | } |
5c085d36 | 822 | mutex_unlock(&adap->clist_lock); |
1da177e4 | 823 | } |
c0564606 | 824 | EXPORT_SYMBOL(i2c_clients_command); |
1da177e4 LT |
825 | |
826 | static int __init i2c_init(void) | |
827 | { | |
828 | int retval; | |
829 | ||
830 | retval = bus_register(&i2c_bus_type); | |
1da177e4 LT |
831 | if (retval) |
832 | return retval; | |
833 | return class_register(&i2c_adapter_class); | |
834 | } | |
835 | ||
836 | static void __exit i2c_exit(void) | |
837 | { | |
838 | class_unregister(&i2c_adapter_class); | |
1da177e4 LT |
839 | bus_unregister(&i2c_bus_type); |
840 | } | |
841 | ||
842 | subsys_initcall(i2c_init); | |
843 | module_exit(i2c_exit); | |
844 | ||
845 | /* ---------------------------------------------------- | |
846 | * the functional interface to the i2c busses. | |
847 | * ---------------------------------------------------- | |
848 | */ | |
849 | ||
850 | int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg *msgs, int num) | |
851 | { | |
852 | int ret; | |
853 | ||
854 | if (adap->algo->master_xfer) { | |
855 | #ifdef DEBUG | |
856 | for (ret = 0; ret < num; ret++) { | |
857 | dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, " | |
209d27c3 JD |
858 | "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD) |
859 | ? 'R' : 'W', msgs[ret].addr, msgs[ret].len, | |
860 | (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : ""); | |
1da177e4 LT |
861 | } |
862 | #endif | |
863 | ||
6ea23039 | 864 | mutex_lock_nested(&adap->bus_lock, adap->level); |
1da177e4 | 865 | ret = adap->algo->master_xfer(adap,msgs,num); |
5c085d36 | 866 | mutex_unlock(&adap->bus_lock); |
1da177e4 LT |
867 | |
868 | return ret; | |
869 | } else { | |
870 | dev_dbg(&adap->dev, "I2C level transfers not supported\n"); | |
871 | return -ENOSYS; | |
872 | } | |
873 | } | |
c0564606 | 874 | EXPORT_SYMBOL(i2c_transfer); |
1da177e4 LT |
875 | |
876 | int i2c_master_send(struct i2c_client *client,const char *buf ,int count) | |
877 | { | |
878 | int ret; | |
879 | struct i2c_adapter *adap=client->adapter; | |
880 | struct i2c_msg msg; | |
881 | ||
815f55f2 JD |
882 | msg.addr = client->addr; |
883 | msg.flags = client->flags & I2C_M_TEN; | |
884 | msg.len = count; | |
885 | msg.buf = (char *)buf; | |
438d6c2c | 886 | |
815f55f2 | 887 | ret = i2c_transfer(adap, &msg, 1); |
1da177e4 | 888 | |
815f55f2 JD |
889 | /* If everything went ok (i.e. 1 msg transmitted), return #bytes |
890 | transmitted, else error code. */ | |
891 | return (ret == 1) ? count : ret; | |
1da177e4 | 892 | } |
c0564606 | 893 | EXPORT_SYMBOL(i2c_master_send); |
1da177e4 LT |
894 | |
895 | int i2c_master_recv(struct i2c_client *client, char *buf ,int count) | |
896 | { | |
897 | struct i2c_adapter *adap=client->adapter; | |
898 | struct i2c_msg msg; | |
899 | int ret; | |
815f55f2 JD |
900 | |
901 | msg.addr = client->addr; | |
902 | msg.flags = client->flags & I2C_M_TEN; | |
903 | msg.flags |= I2C_M_RD; | |
904 | msg.len = count; | |
905 | msg.buf = buf; | |
906 | ||
907 | ret = i2c_transfer(adap, &msg, 1); | |
908 | ||
909 | /* If everything went ok (i.e. 1 msg transmitted), return #bytes | |
910 | transmitted, else error code. */ | |
911 | return (ret == 1) ? count : ret; | |
1da177e4 | 912 | } |
c0564606 | 913 | EXPORT_SYMBOL(i2c_master_recv); |
1da177e4 | 914 | |
1da177e4 LT |
915 | /* ---------------------------------------------------- |
916 | * the i2c address scanning function | |
917 | * Will not work for 10-bit addresses! | |
918 | * ---------------------------------------------------- | |
919 | */ | |
a89ba0bc JD |
920 | static int i2c_probe_address(struct i2c_adapter *adapter, int addr, int kind, |
921 | int (*found_proc) (struct i2c_adapter *, int, int)) | |
922 | { | |
923 | int err; | |
924 | ||
925 | /* Make sure the address is valid */ | |
926 | if (addr < 0x03 || addr > 0x77) { | |
927 | dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n", | |
928 | addr); | |
929 | return -EINVAL; | |
9fc6adfa JD |
930 | } |
931 | ||
a89ba0bc JD |
932 | /* Skip if already in use */ |
933 | if (i2c_check_addr(adapter, addr)) | |
934 | return 0; | |
935 | ||
936 | /* Make sure there is something at this address, unless forced */ | |
4c9337da JD |
937 | if (kind < 0) { |
938 | if (i2c_smbus_xfer(adapter, addr, 0, 0, 0, | |
939 | I2C_SMBUS_QUICK, NULL) < 0) | |
940 | return 0; | |
941 | ||
942 | /* prevent 24RF08 corruption */ | |
943 | if ((addr & ~0x0f) == 0x50) | |
944 | i2c_smbus_xfer(adapter, addr, 0, 0, 0, | |
945 | I2C_SMBUS_QUICK, NULL); | |
946 | } | |
a89ba0bc JD |
947 | |
948 | /* Finally call the custom detection function */ | |
949 | err = found_proc(adapter, addr, kind); | |
a89ba0bc JD |
950 | /* -ENODEV can be returned if there is a chip at the given address |
951 | but it isn't supported by this chip driver. We catch it here as | |
952 | this isn't an error. */ | |
114fd183 JD |
953 | if (err == -ENODEV) |
954 | err = 0; | |
955 | ||
956 | if (err) | |
957 | dev_warn(&adapter->dev, "Client creation failed at 0x%x (%d)\n", | |
958 | addr, err); | |
959 | return err; | |
9fc6adfa JD |
960 | } |
961 | ||
1da177e4 | 962 | int i2c_probe(struct i2c_adapter *adapter, |
bfb6df24 | 963 | const struct i2c_client_address_data *address_data, |
1da177e4 LT |
964 | int (*found_proc) (struct i2c_adapter *, int, int)) |
965 | { | |
a89ba0bc | 966 | int i, err; |
1da177e4 LT |
967 | int adap_id = i2c_adapter_id(adapter); |
968 | ||
a89ba0bc JD |
969 | /* Force entries are done first, and are not affected by ignore |
970 | entries */ | |
971 | if (address_data->forces) { | |
bfb6df24 | 972 | const unsigned short * const *forces = address_data->forces; |
a89ba0bc JD |
973 | int kind; |
974 | ||
975 | for (kind = 0; forces[kind]; kind++) { | |
976 | for (i = 0; forces[kind][i] != I2C_CLIENT_END; | |
977 | i += 2) { | |
978 | if (forces[kind][i] == adap_id | |
979 | || forces[kind][i] == ANY_I2C_BUS) { | |
980 | dev_dbg(&adapter->dev, "found force " | |
981 | "parameter for adapter %d, " | |
982 | "addr 0x%02x, kind %d\n", | |
983 | adap_id, forces[kind][i + 1], | |
984 | kind); | |
985 | err = i2c_probe_address(adapter, | |
986 | forces[kind][i + 1], | |
987 | kind, found_proc); | |
988 | if (err) | |
989 | return err; | |
990 | } | |
1da177e4 LT |
991 | } |
992 | } | |
a89ba0bc | 993 | } |
1da177e4 | 994 | |
4366dc94 JD |
995 | /* Stop here if we can't use SMBUS_QUICK */ |
996 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) { | |
997 | if (address_data->probe[0] == I2C_CLIENT_END | |
998 | && address_data->normal_i2c[0] == I2C_CLIENT_END) | |
438d6c2c | 999 | return 0; |
4366dc94 JD |
1000 | |
1001 | dev_warn(&adapter->dev, "SMBus Quick command not supported, " | |
1002 | "can't probe for chips\n"); | |
1003 | return -1; | |
1004 | } | |
1005 | ||
a89ba0bc JD |
1006 | /* Probe entries are done second, and are not affected by ignore |
1007 | entries either */ | |
1008 | for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) { | |
1009 | if (address_data->probe[i] == adap_id | |
1010 | || address_data->probe[i] == ANY_I2C_BUS) { | |
1011 | dev_dbg(&adapter->dev, "found probe parameter for " | |
1012 | "adapter %d, addr 0x%02x\n", adap_id, | |
1013 | address_data->probe[i + 1]); | |
1014 | err = i2c_probe_address(adapter, | |
1015 | address_data->probe[i + 1], | |
1016 | -1, found_proc); | |
1017 | if (err) | |
1018 | return err; | |
1da177e4 | 1019 | } |
a89ba0bc | 1020 | } |
1da177e4 | 1021 | |
a89ba0bc JD |
1022 | /* Normal entries are done last, unless shadowed by an ignore entry */ |
1023 | for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) { | |
1024 | int j, ignore; | |
1025 | ||
1026 | ignore = 0; | |
1027 | for (j = 0; address_data->ignore[j] != I2C_CLIENT_END; | |
1028 | j += 2) { | |
1029 | if ((address_data->ignore[j] == adap_id || | |
1030 | address_data->ignore[j] == ANY_I2C_BUS) | |
1031 | && address_data->ignore[j + 1] | |
1032 | == address_data->normal_i2c[i]) { | |
1033 | dev_dbg(&adapter->dev, "found ignore " | |
1034 | "parameter for adapter %d, " | |
1035 | "addr 0x%02x\n", adap_id, | |
1036 | address_data->ignore[j + 1]); | |
2369df93 MH |
1037 | ignore = 1; |
1038 | break; | |
1da177e4 LT |
1039 | } |
1040 | } | |
a89ba0bc | 1041 | if (ignore) |
1da177e4 LT |
1042 | continue; |
1043 | ||
a89ba0bc JD |
1044 | dev_dbg(&adapter->dev, "found normal entry for adapter %d, " |
1045 | "addr 0x%02x\n", adap_id, | |
1046 | address_data->normal_i2c[i]); | |
1047 | err = i2c_probe_address(adapter, address_data->normal_i2c[i], | |
1048 | -1, found_proc); | |
1049 | if (err) | |
1050 | return err; | |
1da177e4 | 1051 | } |
a89ba0bc | 1052 | |
1da177e4 LT |
1053 | return 0; |
1054 | } | |
c0564606 | 1055 | EXPORT_SYMBOL(i2c_probe); |
1da177e4 | 1056 | |
12b5053a JD |
1057 | struct i2c_client * |
1058 | i2c_new_probed_device(struct i2c_adapter *adap, | |
1059 | struct i2c_board_info *info, | |
1060 | unsigned short const *addr_list) | |
1061 | { | |
1062 | int i; | |
1063 | ||
1064 | /* Stop here if the bus doesn't support probing */ | |
1065 | if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE)) { | |
1066 | dev_err(&adap->dev, "Probing not supported\n"); | |
1067 | return NULL; | |
1068 | } | |
1069 | ||
1070 | mutex_lock(&adap->clist_lock); | |
1071 | for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) { | |
1072 | /* Check address validity */ | |
1073 | if (addr_list[i] < 0x03 || addr_list[i] > 0x77) { | |
1074 | dev_warn(&adap->dev, "Invalid 7-bit address " | |
1075 | "0x%02x\n", addr_list[i]); | |
1076 | continue; | |
1077 | } | |
1078 | ||
1079 | /* Check address availability */ | |
1080 | if (__i2c_check_addr(adap, addr_list[i])) { | |
1081 | dev_dbg(&adap->dev, "Address 0x%02x already in " | |
1082 | "use, not probing\n", addr_list[i]); | |
1083 | continue; | |
1084 | } | |
1085 | ||
1086 | /* Test address responsiveness | |
1087 | The default probe method is a quick write, but it is known | |
1088 | to corrupt the 24RF08 EEPROMs due to a state machine bug, | |
1089 | and could also irreversibly write-protect some EEPROMs, so | |
1090 | for address ranges 0x30-0x37 and 0x50-0x5f, we use a byte | |
1091 | read instead. Also, some bus drivers don't implement | |
1092 | quick write, so we fallback to a byte read it that case | |
1093 | too. */ | |
1094 | if ((addr_list[i] & ~0x07) == 0x30 | |
1095 | || (addr_list[i] & ~0x0f) == 0x50 | |
1096 | || !i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK)) { | |
1097 | if (i2c_smbus_xfer(adap, addr_list[i], 0, | |
1098 | I2C_SMBUS_READ, 0, | |
1099 | I2C_SMBUS_BYTE, NULL) >= 0) | |
1100 | break; | |
1101 | } else { | |
1102 | if (i2c_smbus_xfer(adap, addr_list[i], 0, | |
1103 | I2C_SMBUS_WRITE, 0, | |
1104 | I2C_SMBUS_QUICK, NULL) >= 0) | |
1105 | break; | |
1106 | } | |
1107 | } | |
1108 | mutex_unlock(&adap->clist_lock); | |
1109 | ||
1110 | if (addr_list[i] == I2C_CLIENT_END) { | |
1111 | dev_dbg(&adap->dev, "Probing failed, no device found\n"); | |
1112 | return NULL; | |
1113 | } | |
1114 | ||
1115 | info->addr = addr_list[i]; | |
1116 | return i2c_new_device(adap, info); | |
1117 | } | |
1118 | EXPORT_SYMBOL_GPL(i2c_new_probed_device); | |
1119 | ||
1da177e4 LT |
1120 | struct i2c_adapter* i2c_get_adapter(int id) |
1121 | { | |
1da177e4 | 1122 | struct i2c_adapter *adapter; |
438d6c2c | 1123 | |
caada32a | 1124 | mutex_lock(&core_lock); |
a0920e10 MH |
1125 | adapter = (struct i2c_adapter *)idr_find(&i2c_adapter_idr, id); |
1126 | if (adapter && !try_module_get(adapter->owner)) | |
1127 | adapter = NULL; | |
1128 | ||
caada32a | 1129 | mutex_unlock(&core_lock); |
a0920e10 | 1130 | return adapter; |
1da177e4 | 1131 | } |
c0564606 | 1132 | EXPORT_SYMBOL(i2c_get_adapter); |
1da177e4 LT |
1133 | |
1134 | void i2c_put_adapter(struct i2c_adapter *adap) | |
1135 | { | |
1136 | module_put(adap->owner); | |
1137 | } | |
c0564606 | 1138 | EXPORT_SYMBOL(i2c_put_adapter); |
1da177e4 LT |
1139 | |
1140 | /* The SMBus parts */ | |
1141 | ||
438d6c2c | 1142 | #define POLY (0x1070U << 3) |
1da177e4 LT |
1143 | static u8 |
1144 | crc8(u16 data) | |
1145 | { | |
1146 | int i; | |
438d6c2c | 1147 | |
1da177e4 | 1148 | for(i = 0; i < 8; i++) { |
438d6c2c | 1149 | if (data & 0x8000) |
1da177e4 LT |
1150 | data = data ^ POLY; |
1151 | data = data << 1; | |
1152 | } | |
1153 | return (u8)(data >> 8); | |
1154 | } | |
1155 | ||
421ef47b JD |
1156 | /* Incremental CRC8 over count bytes in the array pointed to by p */ |
1157 | static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count) | |
1da177e4 LT |
1158 | { |
1159 | int i; | |
1160 | ||
1161 | for(i = 0; i < count; i++) | |
421ef47b | 1162 | crc = crc8((crc ^ p[i]) << 8); |
1da177e4 LT |
1163 | return crc; |
1164 | } | |
1165 | ||
421ef47b JD |
1166 | /* Assume a 7-bit address, which is reasonable for SMBus */ |
1167 | static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg) | |
1da177e4 | 1168 | { |
421ef47b JD |
1169 | /* The address will be sent first */ |
1170 | u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD); | |
1171 | pec = i2c_smbus_pec(pec, &addr, 1); | |
1172 | ||
1173 | /* The data buffer follows */ | |
1174 | return i2c_smbus_pec(pec, msg->buf, msg->len); | |
1da177e4 LT |
1175 | } |
1176 | ||
421ef47b JD |
1177 | /* Used for write only transactions */ |
1178 | static inline void i2c_smbus_add_pec(struct i2c_msg *msg) | |
1da177e4 | 1179 | { |
421ef47b JD |
1180 | msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg); |
1181 | msg->len++; | |
1da177e4 LT |
1182 | } |
1183 | ||
421ef47b JD |
1184 | /* Return <0 on CRC error |
1185 | If there was a write before this read (most cases) we need to take the | |
1186 | partial CRC from the write part into account. | |
1187 | Note that this function does modify the message (we need to decrease the | |
1188 | message length to hide the CRC byte from the caller). */ | |
1189 | static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg) | |
1da177e4 | 1190 | { |
421ef47b JD |
1191 | u8 rpec = msg->buf[--msg->len]; |
1192 | cpec = i2c_smbus_msg_pec(cpec, msg); | |
1da177e4 | 1193 | |
1da177e4 LT |
1194 | if (rpec != cpec) { |
1195 | pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n", | |
1196 | rpec, cpec); | |
1197 | return -1; | |
1198 | } | |
438d6c2c | 1199 | return 0; |
1da177e4 LT |
1200 | } |
1201 | ||
1202 | s32 i2c_smbus_write_quick(struct i2c_client *client, u8 value) | |
1203 | { | |
1204 | return i2c_smbus_xfer(client->adapter,client->addr,client->flags, | |
438d6c2c | 1205 | value,0,I2C_SMBUS_QUICK,NULL); |
1da177e4 | 1206 | } |
c0564606 | 1207 | EXPORT_SYMBOL(i2c_smbus_write_quick); |
1da177e4 LT |
1208 | |
1209 | s32 i2c_smbus_read_byte(struct i2c_client *client) | |
1210 | { | |
1211 | union i2c_smbus_data data; | |
1212 | if (i2c_smbus_xfer(client->adapter,client->addr,client->flags, | |
1213 | I2C_SMBUS_READ,0,I2C_SMBUS_BYTE, &data)) | |
1214 | return -1; | |
1215 | else | |
7eff82c8 | 1216 | return data.byte; |
1da177e4 | 1217 | } |
c0564606 | 1218 | EXPORT_SYMBOL(i2c_smbus_read_byte); |
1da177e4 LT |
1219 | |
1220 | s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value) | |
1221 | { | |
1da177e4 | 1222 | return i2c_smbus_xfer(client->adapter,client->addr,client->flags, |
421ef47b | 1223 | I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL); |
1da177e4 | 1224 | } |
c0564606 | 1225 | EXPORT_SYMBOL(i2c_smbus_write_byte); |
1da177e4 LT |
1226 | |
1227 | s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command) | |
1228 | { | |
1229 | union i2c_smbus_data data; | |
1230 | if (i2c_smbus_xfer(client->adapter,client->addr,client->flags, | |
1231 | I2C_SMBUS_READ,command, I2C_SMBUS_BYTE_DATA,&data)) | |
1232 | return -1; | |
1233 | else | |
7eff82c8 | 1234 | return data.byte; |
1da177e4 | 1235 | } |
c0564606 | 1236 | EXPORT_SYMBOL(i2c_smbus_read_byte_data); |
1da177e4 LT |
1237 | |
1238 | s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value) | |
1239 | { | |
1240 | union i2c_smbus_data data; | |
1241 | data.byte = value; | |
1242 | return i2c_smbus_xfer(client->adapter,client->addr,client->flags, | |
1243 | I2C_SMBUS_WRITE,command, | |
1244 | I2C_SMBUS_BYTE_DATA,&data); | |
1245 | } | |
c0564606 | 1246 | EXPORT_SYMBOL(i2c_smbus_write_byte_data); |
1da177e4 LT |
1247 | |
1248 | s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command) | |
1249 | { | |
1250 | union i2c_smbus_data data; | |
1251 | if (i2c_smbus_xfer(client->adapter,client->addr,client->flags, | |
1252 | I2C_SMBUS_READ,command, I2C_SMBUS_WORD_DATA, &data)) | |
1253 | return -1; | |
1254 | else | |
7eff82c8 | 1255 | return data.word; |
1da177e4 | 1256 | } |
c0564606 | 1257 | EXPORT_SYMBOL(i2c_smbus_read_word_data); |
1da177e4 LT |
1258 | |
1259 | s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value) | |
1260 | { | |
1261 | union i2c_smbus_data data; | |
1262 | data.word = value; | |
1263 | return i2c_smbus_xfer(client->adapter,client->addr,client->flags, | |
1264 | I2C_SMBUS_WRITE,command, | |
1265 | I2C_SMBUS_WORD_DATA,&data); | |
1266 | } | |
c0564606 | 1267 | EXPORT_SYMBOL(i2c_smbus_write_word_data); |
1da177e4 | 1268 | |
a64ec07d DB |
1269 | /** |
1270 | * i2c_smbus_read_block_data - SMBus block read request | |
1271 | * @client: Handle to slave device | |
1272 | * @command: Command byte issued to let the slave know what data should | |
1273 | * be returned | |
1274 | * @values: Byte array into which data will be read; big enough to hold | |
1275 | * the data returned by the slave. SMBus allows at most 32 bytes. | |
1276 | * | |
1277 | * Returns the number of bytes read in the slave's response, else a | |
1278 | * negative number to indicate some kind of error. | |
1279 | * | |
1280 | * Note that using this function requires that the client's adapter support | |
1281 | * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers | |
1282 | * support this; its emulation through I2C messaging relies on a specific | |
1283 | * mechanism (I2C_M_RECV_LEN) which may not be implemented. | |
1284 | */ | |
b86a1bc8 JD |
1285 | s32 i2c_smbus_read_block_data(struct i2c_client *client, u8 command, |
1286 | u8 *values) | |
1287 | { | |
1288 | union i2c_smbus_data data; | |
1289 | ||
1290 | if (i2c_smbus_xfer(client->adapter, client->addr, client->flags, | |
1291 | I2C_SMBUS_READ, command, | |
1292 | I2C_SMBUS_BLOCK_DATA, &data)) | |
1293 | return -1; | |
1294 | ||
1295 | memcpy(values, &data.block[1], data.block[0]); | |
1296 | return data.block[0]; | |
1297 | } | |
1298 | EXPORT_SYMBOL(i2c_smbus_read_block_data); | |
1299 | ||
1da177e4 | 1300 | s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command, |
46f5ed75 | 1301 | u8 length, const u8 *values) |
1da177e4 LT |
1302 | { |
1303 | union i2c_smbus_data data; | |
7656032b | 1304 | |
1da177e4 LT |
1305 | if (length > I2C_SMBUS_BLOCK_MAX) |
1306 | length = I2C_SMBUS_BLOCK_MAX; | |
1da177e4 | 1307 | data.block[0] = length; |
7656032b | 1308 | memcpy(&data.block[1], values, length); |
1da177e4 LT |
1309 | return i2c_smbus_xfer(client->adapter,client->addr,client->flags, |
1310 | I2C_SMBUS_WRITE,command, | |
1311 | I2C_SMBUS_BLOCK_DATA,&data); | |
1312 | } | |
c0564606 | 1313 | EXPORT_SYMBOL(i2c_smbus_write_block_data); |
1da177e4 LT |
1314 | |
1315 | /* Returns the number of read bytes */ | |
4b2643d7 JD |
1316 | s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command, |
1317 | u8 length, u8 *values) | |
1da177e4 LT |
1318 | { |
1319 | union i2c_smbus_data data; | |
7656032b | 1320 | |
4b2643d7 JD |
1321 | if (length > I2C_SMBUS_BLOCK_MAX) |
1322 | length = I2C_SMBUS_BLOCK_MAX; | |
1323 | data.block[0] = length; | |
1da177e4 LT |
1324 | if (i2c_smbus_xfer(client->adapter,client->addr,client->flags, |
1325 | I2C_SMBUS_READ,command, | |
1326 | I2C_SMBUS_I2C_BLOCK_DATA,&data)) | |
1327 | return -1; | |
7656032b JD |
1328 | |
1329 | memcpy(values, &data.block[1], data.block[0]); | |
1330 | return data.block[0]; | |
1da177e4 | 1331 | } |
c0564606 | 1332 | EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data); |
1da177e4 | 1333 | |
21bbd691 | 1334 | s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command, |
46f5ed75 | 1335 | u8 length, const u8 *values) |
21bbd691 JD |
1336 | { |
1337 | union i2c_smbus_data data; | |
1338 | ||
1339 | if (length > I2C_SMBUS_BLOCK_MAX) | |
1340 | length = I2C_SMBUS_BLOCK_MAX; | |
1341 | data.block[0] = length; | |
1342 | memcpy(data.block + 1, values, length); | |
1343 | return i2c_smbus_xfer(client->adapter, client->addr, client->flags, | |
1344 | I2C_SMBUS_WRITE, command, | |
1345 | I2C_SMBUS_I2C_BLOCK_DATA, &data); | |
1346 | } | |
c0564606 | 1347 | EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data); |
21bbd691 | 1348 | |
438d6c2c | 1349 | /* Simulate a SMBus command using the i2c protocol |
1da177e4 | 1350 | No checking of parameters is done! */ |
438d6c2c | 1351 | static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr, |
1da177e4 | 1352 | unsigned short flags, |
438d6c2c | 1353 | char read_write, u8 command, int size, |
1da177e4 LT |
1354 | union i2c_smbus_data * data) |
1355 | { | |
1356 | /* So we need to generate a series of msgs. In the case of writing, we | |
1357 | need to use only one message; when reading, we need two. We initialize | |
1358 | most things with sane defaults, to keep the code below somewhat | |
1359 | simpler. */ | |
5c50d188 HI |
1360 | unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3]; |
1361 | unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2]; | |
1da177e4 | 1362 | int num = read_write == I2C_SMBUS_READ?2:1; |
438d6c2c | 1363 | struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 }, |
1da177e4 LT |
1364 | { addr, flags | I2C_M_RD, 0, msgbuf1 } |
1365 | }; | |
1366 | int i; | |
421ef47b | 1367 | u8 partial_pec = 0; |
1da177e4 LT |
1368 | |
1369 | msgbuf0[0] = command; | |
1370 | switch(size) { | |
1371 | case I2C_SMBUS_QUICK: | |
1372 | msg[0].len = 0; | |
1373 | /* Special case: The read/write field is used as data */ | |
1374 | msg[0].flags = flags | (read_write==I2C_SMBUS_READ)?I2C_M_RD:0; | |
1375 | num = 1; | |
1376 | break; | |
1377 | case I2C_SMBUS_BYTE: | |
1378 | if (read_write == I2C_SMBUS_READ) { | |
1379 | /* Special case: only a read! */ | |
1380 | msg[0].flags = I2C_M_RD | flags; | |
1381 | num = 1; | |
1382 | } | |
1383 | break; | |
1384 | case I2C_SMBUS_BYTE_DATA: | |
1385 | if (read_write == I2C_SMBUS_READ) | |
1386 | msg[1].len = 1; | |
1387 | else { | |
1388 | msg[0].len = 2; | |
1389 | msgbuf0[1] = data->byte; | |
1390 | } | |
1391 | break; | |
1392 | case I2C_SMBUS_WORD_DATA: | |
1393 | if (read_write == I2C_SMBUS_READ) | |
1394 | msg[1].len = 2; | |
1395 | else { | |
1396 | msg[0].len=3; | |
1397 | msgbuf0[1] = data->word & 0xff; | |
7eff82c8 | 1398 | msgbuf0[2] = data->word >> 8; |
1da177e4 LT |
1399 | } |
1400 | break; | |
1401 | case I2C_SMBUS_PROC_CALL: | |
1402 | num = 2; /* Special case */ | |
1403 | read_write = I2C_SMBUS_READ; | |
1404 | msg[0].len = 3; | |
1405 | msg[1].len = 2; | |
1406 | msgbuf0[1] = data->word & 0xff; | |
7eff82c8 | 1407 | msgbuf0[2] = data->word >> 8; |
1da177e4 LT |
1408 | break; |
1409 | case I2C_SMBUS_BLOCK_DATA: | |
1da177e4 | 1410 | if (read_write == I2C_SMBUS_READ) { |
209d27c3 JD |
1411 | msg[1].flags |= I2C_M_RECV_LEN; |
1412 | msg[1].len = 1; /* block length will be added by | |
1413 | the underlying bus driver */ | |
1da177e4 LT |
1414 | } else { |
1415 | msg[0].len = data->block[0] + 2; | |
1416 | if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) { | |
1417 | dev_err(&adapter->dev, "smbus_access called with " | |
1418 | "invalid block write size (%d)\n", | |
1419 | data->block[0]); | |
1420 | return -1; | |
1421 | } | |
5c50d188 | 1422 | for (i = 1; i < msg[0].len; i++) |
1da177e4 LT |
1423 | msgbuf0[i] = data->block[i-1]; |
1424 | } | |
1425 | break; | |
1426 | case I2C_SMBUS_BLOCK_PROC_CALL: | |
209d27c3 JD |
1427 | num = 2; /* Another special case */ |
1428 | read_write = I2C_SMBUS_READ; | |
1429 | if (data->block[0] > I2C_SMBUS_BLOCK_MAX) { | |
1430 | dev_err(&adapter->dev, "%s called with invalid " | |
1431 | "block proc call size (%d)\n", __FUNCTION__, | |
1432 | data->block[0]); | |
1433 | return -1; | |
1434 | } | |
1435 | msg[0].len = data->block[0] + 2; | |
1436 | for (i = 1; i < msg[0].len; i++) | |
1437 | msgbuf0[i] = data->block[i-1]; | |
1438 | msg[1].flags |= I2C_M_RECV_LEN; | |
1439 | msg[1].len = 1; /* block length will be added by | |
1440 | the underlying bus driver */ | |
1441 | break; | |
1da177e4 LT |
1442 | case I2C_SMBUS_I2C_BLOCK_DATA: |
1443 | if (read_write == I2C_SMBUS_READ) { | |
4b2643d7 | 1444 | msg[1].len = data->block[0]; |
1da177e4 LT |
1445 | } else { |
1446 | msg[0].len = data->block[0] + 1; | |
30dac746 | 1447 | if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) { |
1da177e4 LT |
1448 | dev_err(&adapter->dev, "i2c_smbus_xfer_emulated called with " |
1449 | "invalid block write size (%d)\n", | |
1450 | data->block[0]); | |
1451 | return -1; | |
1452 | } | |
1453 | for (i = 1; i <= data->block[0]; i++) | |
1454 | msgbuf0[i] = data->block[i]; | |
1455 | } | |
1456 | break; | |
1457 | default: | |
1458 | dev_err(&adapter->dev, "smbus_access called with invalid size (%d)\n", | |
1459 | size); | |
1460 | return -1; | |
1461 | } | |
1462 | ||
421ef47b JD |
1463 | i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK |
1464 | && size != I2C_SMBUS_I2C_BLOCK_DATA); | |
1465 | if (i) { | |
1466 | /* Compute PEC if first message is a write */ | |
1467 | if (!(msg[0].flags & I2C_M_RD)) { | |
438d6c2c | 1468 | if (num == 1) /* Write only */ |
421ef47b JD |
1469 | i2c_smbus_add_pec(&msg[0]); |
1470 | else /* Write followed by read */ | |
1471 | partial_pec = i2c_smbus_msg_pec(0, &msg[0]); | |
1472 | } | |
1473 | /* Ask for PEC if last message is a read */ | |
1474 | if (msg[num-1].flags & I2C_M_RD) | |
438d6c2c | 1475 | msg[num-1].len++; |
421ef47b JD |
1476 | } |
1477 | ||
1da177e4 LT |
1478 | if (i2c_transfer(adapter, msg, num) < 0) |
1479 | return -1; | |
1480 | ||
421ef47b JD |
1481 | /* Check PEC if last message is a read */ |
1482 | if (i && (msg[num-1].flags & I2C_M_RD)) { | |
1483 | if (i2c_smbus_check_pec(partial_pec, &msg[num-1]) < 0) | |
1484 | return -1; | |
1485 | } | |
1486 | ||
1da177e4 LT |
1487 | if (read_write == I2C_SMBUS_READ) |
1488 | switch(size) { | |
1489 | case I2C_SMBUS_BYTE: | |
1490 | data->byte = msgbuf0[0]; | |
1491 | break; | |
1492 | case I2C_SMBUS_BYTE_DATA: | |
1493 | data->byte = msgbuf1[0]; | |
1494 | break; | |
438d6c2c | 1495 | case I2C_SMBUS_WORD_DATA: |
1da177e4 LT |
1496 | case I2C_SMBUS_PROC_CALL: |
1497 | data->word = msgbuf1[0] | (msgbuf1[1] << 8); | |
1498 | break; | |
1499 | case I2C_SMBUS_I2C_BLOCK_DATA: | |
4b2643d7 | 1500 | for (i = 0; i < data->block[0]; i++) |
1da177e4 LT |
1501 | data->block[i+1] = msgbuf1[i]; |
1502 | break; | |
209d27c3 JD |
1503 | case I2C_SMBUS_BLOCK_DATA: |
1504 | case I2C_SMBUS_BLOCK_PROC_CALL: | |
1505 | for (i = 0; i < msgbuf1[0] + 1; i++) | |
1506 | data->block[i] = msgbuf1[i]; | |
1507 | break; | |
1da177e4 LT |
1508 | } |
1509 | return 0; | |
1510 | } | |
1511 | ||
1512 | ||
1513 | s32 i2c_smbus_xfer(struct i2c_adapter * adapter, u16 addr, unsigned short flags, | |
438d6c2c | 1514 | char read_write, u8 command, int size, |
1da177e4 LT |
1515 | union i2c_smbus_data * data) |
1516 | { | |
1517 | s32 res; | |
1da177e4 LT |
1518 | |
1519 | flags &= I2C_M_TEN | I2C_CLIENT_PEC; | |
1da177e4 LT |
1520 | |
1521 | if (adapter->algo->smbus_xfer) { | |
5c085d36 | 1522 | mutex_lock(&adapter->bus_lock); |
1da177e4 LT |
1523 | res = adapter->algo->smbus_xfer(adapter,addr,flags,read_write, |
1524 | command,size,data); | |
5c085d36 | 1525 | mutex_unlock(&adapter->bus_lock); |
1da177e4 LT |
1526 | } else |
1527 | res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write, | |
1528 | command,size,data); | |
1529 | ||
1da177e4 LT |
1530 | return res; |
1531 | } | |
1da177e4 | 1532 | EXPORT_SYMBOL(i2c_smbus_xfer); |
1da177e4 LT |
1533 | |
1534 | MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>"); | |
1535 | MODULE_DESCRIPTION("I2C-Bus main module"); | |
1536 | MODULE_LICENSE("GPL"); |