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