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