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