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