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