1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
43
44
45static DEFINE_MUTEX(core_lock);
46static DEFINE_IDR(i2c_adapter_idr);
47static LIST_HEAD(userspace_devices);
48
49static struct device_type i2c_client_type;
50static int i2c_check_addr(struct i2c_adapter *adapter, int addr);
51static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
52
53
54
55static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
56 const struct i2c_client *client)
57{
58 while (id->name[0]) {
59 if (strcmp(client->name, id->name) == 0)
60 return id;
61 id++;
62 }
63 return NULL;
64}
65
66static int i2c_device_match(struct device *dev, struct device_driver *drv)
67{
68 struct i2c_client *client = i2c_verify_client(dev);
69 struct i2c_driver *driver;
70
71 if (!client)
72 return 0;
73
74 driver = to_i2c_driver(drv);
75
76 if (driver->id_table)
77 return i2c_match_id(driver->id_table, client) != NULL;
78
79 return 0;
80}
81
82#ifdef CONFIG_HOTPLUG
83
84
85static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
86{
87 struct i2c_client *client = to_i2c_client(dev);
88
89 if (add_uevent_var(env, "MODALIAS=%s%s",
90 I2C_MODULE_PREFIX, client->name))
91 return -ENOMEM;
92 dev_dbg(dev, "uevent\n");
93 return 0;
94}
95
96#else
97#define i2c_device_uevent NULL
98#endif
99
100static int i2c_device_probe(struct device *dev)
101{
102 struct i2c_client *client = i2c_verify_client(dev);
103 struct i2c_driver *driver;
104 int status;
105
106 if (!client)
107 return 0;
108
109 driver = to_i2c_driver(dev->driver);
110 if (!driver->probe || !driver->id_table)
111 return -ENODEV;
112 client->driver = driver;
113 if (!device_can_wakeup(&client->dev))
114 device_init_wakeup(&client->dev,
115 client->flags & I2C_CLIENT_WAKE);
116 dev_dbg(dev, "probe\n");
117
118 status = driver->probe(client, i2c_match_id(driver->id_table, client));
119 if (status)
120 client->driver = NULL;
121 return status;
122}
123
124static int i2c_device_remove(struct device *dev)
125{
126 struct i2c_client *client = i2c_verify_client(dev);
127 struct i2c_driver *driver;
128 int status;
129
130 if (!client || !dev->driver)
131 return 0;
132
133 driver = to_i2c_driver(dev->driver);
134 if (driver->remove) {
135 dev_dbg(dev, "remove\n");
136 status = driver->remove(client);
137 } else {
138 dev->driver = NULL;
139 status = 0;
140 }
141 if (status == 0)
142 client->driver = NULL;
143 return status;
144}
145
146static void i2c_device_shutdown(struct device *dev)
147{
148 struct i2c_client *client = i2c_verify_client(dev);
149 struct i2c_driver *driver;
150
151 if (!client || !dev->driver)
152 return;
153 driver = to_i2c_driver(dev->driver);
154 if (driver->shutdown)
155 driver->shutdown(client);
156}
157
158static int i2c_device_suspend(struct device *dev, pm_message_t mesg)
159{
160 struct i2c_client *client = i2c_verify_client(dev);
161 struct i2c_driver *driver;
162
163 if (!client || !dev->driver)
164 return 0;
165 driver = to_i2c_driver(dev->driver);
166 if (!driver->suspend)
167 return 0;
168 return driver->suspend(client, mesg);
169}
170
171static int i2c_device_resume(struct device *dev)
172{
173 struct i2c_client *client = i2c_verify_client(dev);
174 struct i2c_driver *driver;
175
176 if (!client || !dev->driver)
177 return 0;
178 driver = to_i2c_driver(dev->driver);
179 if (!driver->resume)
180 return 0;
181 return driver->resume(client);
182}
183
184static void i2c_client_dev_release(struct device *dev)
185{
186 kfree(to_i2c_client(dev));
187}
188
189static ssize_t
190show_name(struct device *dev, struct device_attribute *attr, char *buf)
191{
192 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
193 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
194}
195
196static ssize_t
197show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
198{
199 struct i2c_client *client = to_i2c_client(dev);
200 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
201}
202
203static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
204static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
205
206static struct attribute *i2c_dev_attrs[] = {
207 &dev_attr_name.attr,
208
209 &dev_attr_modalias.attr,
210 NULL
211};
212
213static struct attribute_group i2c_dev_attr_group = {
214 .attrs = i2c_dev_attrs,
215};
216
217static const struct attribute_group *i2c_dev_attr_groups[] = {
218 &i2c_dev_attr_group,
219 NULL
220};
221
222struct bus_type i2c_bus_type = {
223 .name = "i2c",
224 .match = i2c_device_match,
225 .probe = i2c_device_probe,
226 .remove = i2c_device_remove,
227 .shutdown = i2c_device_shutdown,
228 .suspend = i2c_device_suspend,
229 .resume = i2c_device_resume,
230};
231EXPORT_SYMBOL_GPL(i2c_bus_type);
232
233static struct device_type i2c_client_type = {
234 .groups = i2c_dev_attr_groups,
235 .uevent = i2c_device_uevent,
236 .release = i2c_client_dev_release,
237};
238
239
240
241
242
243
244
245
246
247
248
249struct i2c_client *i2c_verify_client(struct device *dev)
250{
251 return (dev->type == &i2c_client_type)
252 ? to_i2c_client(dev)
253 : NULL;
254}
255EXPORT_SYMBOL(i2c_verify_client);
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274struct i2c_client *
275i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
276{
277 struct i2c_client *client;
278 int status;
279
280 client = kzalloc(sizeof *client, GFP_KERNEL);
281 if (!client)
282 return NULL;
283
284 client->adapter = adap;
285
286 client->dev.platform_data = info->platform_data;
287
288 if (info->archdata)
289 client->dev.archdata = *info->archdata;
290
291 client->flags = info->flags;
292 client->addr = info->addr;
293 client->irq = info->irq;
294
295 strlcpy(client->name, info->type, sizeof(client->name));
296
297
298 status = i2c_check_addr(adap, client->addr);
299 if (status)
300 goto out_err;
301
302 client->dev.parent = &client->adapter->dev;
303 client->dev.bus = &i2c_bus_type;
304 client->dev.type = &i2c_client_type;
305
306 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
307 client->addr);
308 status = device_register(&client->dev);
309 if (status)
310 goto out_err;
311
312 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
313 client->name, dev_name(&client->dev));
314
315 return client;
316
317out_err:
318 dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x "
319 "(%d)\n", client->name, client->addr, status);
320 kfree(client);
321 return NULL;
322}
323EXPORT_SYMBOL_GPL(i2c_new_device);
324
325
326
327
328
329
330
331void i2c_unregister_device(struct i2c_client *client)
332{
333 device_unregister(&client->dev);
334}
335EXPORT_SYMBOL_GPL(i2c_unregister_device);
336
337
338static const struct i2c_device_id dummy_id[] = {
339 { "dummy", 0 },
340 { },
341};
342
343static int dummy_probe(struct i2c_client *client,
344 const struct i2c_device_id *id)
345{
346 return 0;
347}
348
349static int dummy_remove(struct i2c_client *client)
350{
351 return 0;
352}
353
354static struct i2c_driver dummy_driver = {
355 .driver.name = "dummy",
356 .probe = dummy_probe,
357 .remove = dummy_remove,
358 .id_table = dummy_id,
359};
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
380{
381 struct i2c_board_info info = {
382 I2C_BOARD_INFO("dummy", address),
383 };
384
385 return i2c_new_device(adapter, &info);
386}
387EXPORT_SYMBOL_GPL(i2c_new_dummy);
388
389
390
391
392
393static void i2c_adapter_dev_release(struct device *dev)
394{
395 struct i2c_adapter *adap = to_i2c_adapter(dev);
396 complete(&adap->dev_released);
397}
398
399
400
401
402
403
404
405
406
407
408
409static ssize_t
410i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
411 const char *buf, size_t count)
412{
413 struct i2c_adapter *adap = to_i2c_adapter(dev);
414 struct i2c_board_info info;
415 struct i2c_client *client;
416 char *blank, end;
417 int res;
418
419 dev_warn(dev, "The new_device interface is still experimental "
420 "and may change in a near future\n");
421 memset(&info, 0, sizeof(struct i2c_board_info));
422
423 blank = strchr(buf, ' ');
424 if (!blank) {
425 dev_err(dev, "%s: Missing parameters\n", "new_device");
426 return -EINVAL;
427 }
428 if (blank - buf > I2C_NAME_SIZE - 1) {
429 dev_err(dev, "%s: Invalid device name\n", "new_device");
430 return -EINVAL;
431 }
432 memcpy(info.type, buf, blank - buf);
433
434
435 res = sscanf(++blank, "%hi%c", &info.addr, &end);
436 if (res < 1) {
437 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
438 return -EINVAL;
439 }
440 if (res > 1 && end != '\n') {
441 dev_err(dev, "%s: Extra parameters\n", "new_device");
442 return -EINVAL;
443 }
444
445 if (info.addr < 0x03 || info.addr > 0x77) {
446 dev_err(dev, "%s: Invalid I2C address 0x%hx\n", "new_device",
447 info.addr);
448 return -EINVAL;
449 }
450
451 client = i2c_new_device(adap, &info);
452 if (!client)
453 return -EEXIST;
454
455
456 mutex_lock(&core_lock);
457 list_add_tail(&client->detected, &userspace_devices);
458 mutex_unlock(&core_lock);
459 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
460 info.type, info.addr);
461
462 return count;
463}
464
465
466
467
468
469
470
471
472
473
474static ssize_t
475i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
476 const char *buf, size_t count)
477{
478 struct i2c_adapter *adap = to_i2c_adapter(dev);
479 struct i2c_client *client, *next;
480 unsigned short addr;
481 char end;
482 int res;
483
484
485 res = sscanf(buf, "%hi%c", &addr, &end);
486 if (res < 1) {
487 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
488 return -EINVAL;
489 }
490 if (res > 1 && end != '\n') {
491 dev_err(dev, "%s: Extra parameters\n", "delete_device");
492 return -EINVAL;
493 }
494
495
496 res = -ENOENT;
497 mutex_lock(&core_lock);
498 list_for_each_entry_safe(client, next, &userspace_devices, detected) {
499 if (client->addr == addr && client->adapter == adap) {
500 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
501 "delete_device", client->name, client->addr);
502
503 list_del(&client->detected);
504 i2c_unregister_device(client);
505 res = count;
506 break;
507 }
508 }
509 mutex_unlock(&core_lock);
510
511 if (res < 0)
512 dev_err(dev, "%s: Can't find device in list\n",
513 "delete_device");
514 return res;
515}
516
517static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
518static DEVICE_ATTR(delete_device, S_IWUSR, NULL, i2c_sysfs_delete_device);
519
520static struct attribute *i2c_adapter_attrs[] = {
521 &dev_attr_name.attr,
522 &dev_attr_new_device.attr,
523 &dev_attr_delete_device.attr,
524 NULL
525};
526
527static struct attribute_group i2c_adapter_attr_group = {
528 .attrs = i2c_adapter_attrs,
529};
530
531static const struct attribute_group *i2c_adapter_attr_groups[] = {
532 &i2c_adapter_attr_group,
533 NULL
534};
535
536static struct device_type i2c_adapter_type = {
537 .groups = i2c_adapter_attr_groups,
538 .release = i2c_adapter_dev_release,
539};
540
541#ifdef CONFIG_I2C_COMPAT
542static struct class_compat *i2c_adapter_compat_class;
543#endif
544
545static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
546{
547 struct i2c_devinfo *devinfo;
548
549 down_read(&__i2c_board_lock);
550 list_for_each_entry(devinfo, &__i2c_board_list, list) {
551 if (devinfo->busnum == adapter->nr
552 && !i2c_new_device(adapter,
553 &devinfo->board_info))
554 dev_err(&adapter->dev,
555 "Can't create device at 0x%02x\n",
556 devinfo->board_info.addr);
557 }
558 up_read(&__i2c_board_lock);
559}
560
561static int i2c_do_add_adapter(struct device_driver *d, void *data)
562{
563 struct i2c_driver *driver = to_i2c_driver(d);
564 struct i2c_adapter *adap = data;
565
566
567 i2c_detect(adap, driver);
568
569
570 if (driver->attach_adapter) {
571
572 driver->attach_adapter(adap);
573 }
574 return 0;
575}
576
577static int i2c_register_adapter(struct i2c_adapter *adap)
578{
579 int res = 0, dummy;
580
581
582 if (unlikely(WARN_ON(!i2c_bus_type.p))) {
583 res = -EAGAIN;
584 goto out_list;
585 }
586
587 mutex_init(&adap->bus_lock);
588
589
590 if (adap->timeout == 0)
591 adap->timeout = HZ;
592
593 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
594 adap->dev.bus = &i2c_bus_type;
595 adap->dev.type = &i2c_adapter_type;
596 res = device_register(&adap->dev);
597 if (res)
598 goto out_list;
599
600 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
601
602#ifdef CONFIG_I2C_COMPAT
603 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
604 adap->dev.parent);
605 if (res)
606 dev_warn(&adap->dev,
607 "Failed to create compatibility class link\n");
608#endif
609
610
611 if (adap->nr < __i2c_first_dynamic_bus_num)
612 i2c_scan_static_board_info(adap);
613
614
615 mutex_lock(&core_lock);
616 dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap,
617 i2c_do_add_adapter);
618 mutex_unlock(&core_lock);
619
620 return 0;
621
622out_list:
623 mutex_lock(&core_lock);
624 idr_remove(&i2c_adapter_idr, adap->nr);
625 mutex_unlock(&core_lock);
626 return res;
627}
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642int i2c_add_adapter(struct i2c_adapter *adapter)
643{
644 int id, res = 0;
645
646retry:
647 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
648 return -ENOMEM;
649
650 mutex_lock(&core_lock);
651
652 res = idr_get_new_above(&i2c_adapter_idr, adapter,
653 __i2c_first_dynamic_bus_num, &id);
654 mutex_unlock(&core_lock);
655
656 if (res < 0) {
657 if (res == -EAGAIN)
658 goto retry;
659 return res;
660 }
661
662 adapter->nr = id;
663 return i2c_register_adapter(adapter);
664}
665EXPORT_SYMBOL(i2c_add_adapter);
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687int i2c_add_numbered_adapter(struct i2c_adapter *adap)
688{
689 int id;
690 int status;
691
692 if (adap->nr & ~MAX_ID_MASK)
693 return -EINVAL;
694
695retry:
696 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
697 return -ENOMEM;
698
699 mutex_lock(&core_lock);
700
701
702
703 status = idr_get_new_above(&i2c_adapter_idr, adap, adap->nr, &id);
704 if (status == 0 && id != adap->nr) {
705 status = -EBUSY;
706 idr_remove(&i2c_adapter_idr, id);
707 }
708 mutex_unlock(&core_lock);
709 if (status == -EAGAIN)
710 goto retry;
711
712 if (status == 0)
713 status = i2c_register_adapter(adap);
714 return status;
715}
716EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
717
718static int i2c_do_del_adapter(struct device_driver *d, void *data)
719{
720 struct i2c_driver *driver = to_i2c_driver(d);
721 struct i2c_adapter *adapter = data;
722 struct i2c_client *client, *_n;
723 int res;
724
725
726
727 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
728 if (client->adapter == adapter) {
729 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
730 client->name, client->addr);
731 list_del(&client->detected);
732 i2c_unregister_device(client);
733 }
734 }
735
736 if (!driver->detach_adapter)
737 return 0;
738 res = driver->detach_adapter(adapter);
739 if (res)
740 dev_err(&adapter->dev, "detach_adapter failed (%d) "
741 "for driver [%s]\n", res, driver->driver.name);
742 return res;
743}
744
745static int __unregister_client(struct device *dev, void *dummy)
746{
747 struct i2c_client *client = i2c_verify_client(dev);
748 if (client)
749 i2c_unregister_device(client);
750 return 0;
751}
752
753
754
755
756
757
758
759
760
761int i2c_del_adapter(struct i2c_adapter *adap)
762{
763 int res = 0;
764 struct i2c_adapter *found;
765 struct i2c_client *client, *next;
766
767
768 mutex_lock(&core_lock);
769 found = idr_find(&i2c_adapter_idr, adap->nr);
770 mutex_unlock(&core_lock);
771 if (found != adap) {
772 pr_debug("i2c-core: attempting to delete unregistered "
773 "adapter [%s]\n", adap->name);
774 return -EINVAL;
775 }
776
777
778 mutex_lock(&core_lock);
779 res = bus_for_each_drv(&i2c_bus_type, NULL, adap,
780 i2c_do_del_adapter);
781 mutex_unlock(&core_lock);
782 if (res)
783 return res;
784
785
786 list_for_each_entry_safe(client, next, &userspace_devices, detected) {
787 if (client->adapter == adap) {
788 dev_dbg(&adap->dev, "Removing %s at 0x%x\n",
789 client->name, client->addr);
790 list_del(&client->detected);
791 i2c_unregister_device(client);
792 }
793 }
794
795
796
797 res = device_for_each_child(&adap->dev, NULL, __unregister_client);
798
799#ifdef CONFIG_I2C_COMPAT
800 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
801 adap->dev.parent);
802#endif
803
804
805 init_completion(&adap->dev_released);
806 device_unregister(&adap->dev);
807
808
809 wait_for_completion(&adap->dev_released);
810
811
812 mutex_lock(&core_lock);
813 idr_remove(&i2c_adapter_idr, adap->nr);
814 mutex_unlock(&core_lock);
815
816 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
817
818
819
820 memset(&adap->dev, 0, sizeof(adap->dev));
821
822 return 0;
823}
824EXPORT_SYMBOL(i2c_del_adapter);
825
826
827
828
829static int __attach_adapter(struct device *dev, void *data)
830{
831 struct i2c_adapter *adapter;
832 struct i2c_driver *driver = data;
833
834 if (dev->type != &i2c_adapter_type)
835 return 0;
836 adapter = to_i2c_adapter(dev);
837
838 i2c_detect(adapter, driver);
839
840
841 if (driver->attach_adapter)
842 driver->attach_adapter(adapter);
843
844 return 0;
845}
846
847
848
849
850
851
852int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
853{
854 int res;
855
856
857 if (unlikely(WARN_ON(!i2c_bus_type.p)))
858 return -EAGAIN;
859
860
861 driver->driver.owner = owner;
862 driver->driver.bus = &i2c_bus_type;
863
864
865
866
867 res = driver_register(&driver->driver);
868 if (res)
869 return res;
870
871 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
872
873 INIT_LIST_HEAD(&driver->clients);
874
875 mutex_lock(&core_lock);
876 bus_for_each_dev(&i2c_bus_type, NULL, driver, __attach_adapter);
877 mutex_unlock(&core_lock);
878
879 return 0;
880}
881EXPORT_SYMBOL(i2c_register_driver);
882
883static int __detach_adapter(struct device *dev, void *data)
884{
885 struct i2c_adapter *adapter;
886 struct i2c_driver *driver = data;
887 struct i2c_client *client, *_n;
888
889 if (dev->type != &i2c_adapter_type)
890 return 0;
891 adapter = to_i2c_adapter(dev);
892
893
894
895 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
896 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
897 client->name, client->addr);
898 list_del(&client->detected);
899 i2c_unregister_device(client);
900 }
901
902 if (driver->detach_adapter) {
903 if (driver->detach_adapter(adapter))
904 dev_err(&adapter->dev,
905 "detach_adapter failed for driver [%s]\n",
906 driver->driver.name);
907 }
908
909 return 0;
910}
911
912
913
914
915
916
917void i2c_del_driver(struct i2c_driver *driver)
918{
919 mutex_lock(&core_lock);
920 bus_for_each_dev(&i2c_bus_type, NULL, driver, __detach_adapter);
921 mutex_unlock(&core_lock);
922
923 driver_unregister(&driver->driver);
924 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
925}
926EXPORT_SYMBOL(i2c_del_driver);
927
928
929
930static int __i2c_check_addr(struct device *dev, void *addrp)
931{
932 struct i2c_client *client = i2c_verify_client(dev);
933 int addr = *(int *)addrp;
934
935 if (client && client->addr == addr)
936 return -EBUSY;
937 return 0;
938}
939
940static int i2c_check_addr(struct i2c_adapter *adapter, int addr)
941{
942 return device_for_each_child(&adapter->dev, &addr, __i2c_check_addr);
943}
944
945
946
947
948
949
950
951
952
953
954
955
956struct i2c_client *i2c_use_client(struct i2c_client *client)
957{
958 if (client && get_device(&client->dev))
959 return client;
960 return NULL;
961}
962EXPORT_SYMBOL(i2c_use_client);
963
964
965
966
967
968
969
970void i2c_release_client(struct i2c_client *client)
971{
972 if (client)
973 put_device(&client->dev);
974}
975EXPORT_SYMBOL(i2c_release_client);
976
977struct i2c_cmd_arg {
978 unsigned cmd;
979 void *arg;
980};
981
982static int i2c_cmd(struct device *dev, void *_arg)
983{
984 struct i2c_client *client = i2c_verify_client(dev);
985 struct i2c_cmd_arg *arg = _arg;
986
987 if (client && client->driver && client->driver->command)
988 client->driver->command(client, arg->cmd, arg->arg);
989 return 0;
990}
991
992void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
993{
994 struct i2c_cmd_arg cmd_arg;
995
996 cmd_arg.cmd = cmd;
997 cmd_arg.arg = arg;
998 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
999}
1000EXPORT_SYMBOL(i2c_clients_command);
1001
1002static int __init i2c_init(void)
1003{
1004 int retval;
1005
1006 retval = bus_register(&i2c_bus_type);
1007 if (retval)
1008 return retval;
1009#ifdef CONFIG_I2C_COMPAT
1010 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
1011 if (!i2c_adapter_compat_class) {
1012 retval = -ENOMEM;
1013 goto bus_err;
1014 }
1015#endif
1016 retval = i2c_add_driver(&dummy_driver);
1017 if (retval)
1018 goto class_err;
1019 return 0;
1020
1021class_err:
1022#ifdef CONFIG_I2C_COMPAT
1023 class_compat_unregister(i2c_adapter_compat_class);
1024bus_err:
1025#endif
1026 bus_unregister(&i2c_bus_type);
1027 return retval;
1028}
1029
1030static void __exit i2c_exit(void)
1031{
1032 i2c_del_driver(&dummy_driver);
1033#ifdef CONFIG_I2C_COMPAT
1034 class_compat_unregister(i2c_adapter_compat_class);
1035#endif
1036 bus_unregister(&i2c_bus_type);
1037}
1038
1039
1040
1041
1042postcore_initcall(i2c_init);
1043module_exit(i2c_exit);
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
1063{
1064 unsigned long orig_jiffies;
1065 int ret, try;
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084 if (adap->algo->master_xfer) {
1085#ifdef DEBUG
1086 for (ret = 0; ret < num; ret++) {
1087 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
1088 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
1089 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
1090 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
1091 }
1092#endif
1093
1094 if (in_atomic() || irqs_disabled()) {
1095 ret = mutex_trylock(&adap->bus_lock);
1096 if (!ret)
1097
1098 return -EAGAIN;
1099 } else {
1100 mutex_lock_nested(&adap->bus_lock, adap->level);
1101 }
1102
1103
1104 orig_jiffies = jiffies;
1105 for (ret = 0, try = 0; try <= adap->retries; try++) {
1106 ret = adap->algo->master_xfer(adap, msgs, num);
1107 if (ret != -EAGAIN)
1108 break;
1109 if (time_after(jiffies, orig_jiffies + adap->timeout))
1110 break;
1111 }
1112 mutex_unlock(&adap->bus_lock);
1113
1114 return ret;
1115 } else {
1116 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
1117 return -EOPNOTSUPP;
1118 }
1119}
1120EXPORT_SYMBOL(i2c_transfer);
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
1131{
1132 int ret;
1133 struct i2c_adapter *adap=client->adapter;
1134 struct i2c_msg msg;
1135
1136 msg.addr = client->addr;
1137 msg.flags = client->flags & I2C_M_TEN;
1138 msg.len = count;
1139 msg.buf = (char *)buf;
1140
1141 ret = i2c_transfer(adap, &msg, 1);
1142
1143
1144
1145 return (ret == 1) ? count : ret;
1146}
1147EXPORT_SYMBOL(i2c_master_send);
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
1158{
1159 struct i2c_adapter *adap=client->adapter;
1160 struct i2c_msg msg;
1161 int ret;
1162
1163 msg.addr = client->addr;
1164 msg.flags = client->flags & I2C_M_TEN;
1165 msg.flags |= I2C_M_RD;
1166 msg.len = count;
1167 msg.buf = buf;
1168
1169 ret = i2c_transfer(adap, &msg, 1);
1170
1171
1172
1173 return (ret == 1) ? count : ret;
1174}
1175EXPORT_SYMBOL(i2c_master_recv);
1176
1177
1178
1179
1180
1181
1182
1183static int i2c_detect_address(struct i2c_client *temp_client, int kind,
1184 struct i2c_driver *driver)
1185{
1186 struct i2c_board_info info;
1187 struct i2c_adapter *adapter = temp_client->adapter;
1188 int addr = temp_client->addr;
1189 int err;
1190
1191
1192 if (addr < 0x03 || addr > 0x77) {
1193 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
1194 addr);
1195 return -EINVAL;
1196 }
1197
1198
1199 if (i2c_check_addr(adapter, addr))
1200 return 0;
1201
1202
1203 if (kind < 0) {
1204 if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
1205 I2C_SMBUS_QUICK, NULL) < 0)
1206 return 0;
1207
1208
1209 if ((addr & ~0x0f) == 0x50)
1210 i2c_smbus_xfer(adapter, addr, 0, 0, 0,
1211 I2C_SMBUS_QUICK, NULL);
1212 }
1213
1214
1215 memset(&info, 0, sizeof(struct i2c_board_info));
1216 info.addr = addr;
1217 err = driver->detect(temp_client, kind, &info);
1218 if (err) {
1219
1220
1221 return err == -ENODEV ? 0 : err;
1222 }
1223
1224
1225 if (info.type[0] == '\0') {
1226 dev_err(&adapter->dev, "%s detection function provided "
1227 "no name for 0x%x\n", driver->driver.name,
1228 addr);
1229 } else {
1230 struct i2c_client *client;
1231
1232
1233 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
1234 info.type, info.addr);
1235 client = i2c_new_device(adapter, &info);
1236 if (client)
1237 list_add_tail(&client->detected, &driver->clients);
1238 else
1239 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
1240 info.type, info.addr);
1241 }
1242 return 0;
1243}
1244
1245static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
1246{
1247 const struct i2c_client_address_data *address_data;
1248 struct i2c_client *temp_client;
1249 int i, err = 0;
1250 int adap_id = i2c_adapter_id(adapter);
1251
1252 address_data = driver->address_data;
1253 if (!driver->detect || !address_data)
1254 return 0;
1255
1256
1257 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
1258 if (!temp_client)
1259 return -ENOMEM;
1260 temp_client->adapter = adapter;
1261
1262
1263
1264 if (address_data->forces) {
1265 const unsigned short * const *forces = address_data->forces;
1266 int kind;
1267
1268 for (kind = 0; forces[kind]; kind++) {
1269 for (i = 0; forces[kind][i] != I2C_CLIENT_END;
1270 i += 2) {
1271 if (forces[kind][i] == adap_id
1272 || forces[kind][i] == ANY_I2C_BUS) {
1273 dev_dbg(&adapter->dev, "found force "
1274 "parameter for adapter %d, "
1275 "addr 0x%02x, kind %d\n",
1276 adap_id, forces[kind][i + 1],
1277 kind);
1278 temp_client->addr = forces[kind][i + 1];
1279 err = i2c_detect_address(temp_client,
1280 kind, driver);
1281 if (err)
1282 goto exit_free;
1283 }
1284 }
1285 }
1286 }
1287
1288
1289 if (!(adapter->class & driver->class))
1290 goto exit_free;
1291
1292
1293 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
1294 if (address_data->probe[0] == I2C_CLIENT_END
1295 && address_data->normal_i2c[0] == I2C_CLIENT_END)
1296 goto exit_free;
1297
1298 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
1299 "can't probe for chips\n");
1300 err = -EOPNOTSUPP;
1301 goto exit_free;
1302 }
1303
1304
1305
1306 for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
1307 if (address_data->probe[i] == adap_id
1308 || address_data->probe[i] == ANY_I2C_BUS) {
1309 dev_dbg(&adapter->dev, "found probe parameter for "
1310 "adapter %d, addr 0x%02x\n", adap_id,
1311 address_data->probe[i + 1]);
1312 temp_client->addr = address_data->probe[i + 1];
1313 err = i2c_detect_address(temp_client, -1, driver);
1314 if (err)
1315 goto exit_free;
1316 }
1317 }
1318
1319
1320 for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
1321 int j, ignore;
1322
1323 ignore = 0;
1324 for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
1325 j += 2) {
1326 if ((address_data->ignore[j] == adap_id ||
1327 address_data->ignore[j] == ANY_I2C_BUS)
1328 && address_data->ignore[j + 1]
1329 == address_data->normal_i2c[i]) {
1330 dev_dbg(&adapter->dev, "found ignore "
1331 "parameter for adapter %d, "
1332 "addr 0x%02x\n", adap_id,
1333 address_data->ignore[j + 1]);
1334 ignore = 1;
1335 break;
1336 }
1337 }
1338 if (ignore)
1339 continue;
1340
1341 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
1342 "addr 0x%02x\n", adap_id,
1343 address_data->normal_i2c[i]);
1344 temp_client->addr = address_data->normal_i2c[i];
1345 err = i2c_detect_address(temp_client, -1, driver);
1346 if (err)
1347 goto exit_free;
1348 }
1349
1350 exit_free:
1351 kfree(temp_client);
1352 return err;
1353}
1354
1355struct i2c_client *
1356i2c_new_probed_device(struct i2c_adapter *adap,
1357 struct i2c_board_info *info,
1358 unsigned short const *addr_list)
1359{
1360 int i;
1361
1362
1363 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE)) {
1364 dev_err(&adap->dev, "Probing not supported\n");
1365 return NULL;
1366 }
1367
1368 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
1369
1370 if (addr_list[i] < 0x03 || addr_list[i] > 0x77) {
1371 dev_warn(&adap->dev, "Invalid 7-bit address "
1372 "0x%02x\n", addr_list[i]);
1373 continue;
1374 }
1375
1376
1377 if (i2c_check_addr(adap, addr_list[i])) {
1378 dev_dbg(&adap->dev, "Address 0x%02x already in "
1379 "use, not probing\n", addr_list[i]);
1380 continue;
1381 }
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391 if ((addr_list[i] & ~0x07) == 0x30
1392 || (addr_list[i] & ~0x0f) == 0x50
1393 || !i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK)) {
1394 union i2c_smbus_data data;
1395
1396 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1397 I2C_SMBUS_READ, 0,
1398 I2C_SMBUS_BYTE, &data) >= 0)
1399 break;
1400 } else {
1401 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1402 I2C_SMBUS_WRITE, 0,
1403 I2C_SMBUS_QUICK, NULL) >= 0)
1404 break;
1405 }
1406 }
1407
1408 if (addr_list[i] == I2C_CLIENT_END) {
1409 dev_dbg(&adap->dev, "Probing failed, no device found\n");
1410 return NULL;
1411 }
1412
1413 info->addr = addr_list[i];
1414 return i2c_new_device(adap, info);
1415}
1416EXPORT_SYMBOL_GPL(i2c_new_probed_device);
1417
1418struct i2c_adapter* i2c_get_adapter(int id)
1419{
1420 struct i2c_adapter *adapter;
1421
1422 mutex_lock(&core_lock);
1423 adapter = idr_find(&i2c_adapter_idr, id);
1424 if (adapter && !try_module_get(adapter->owner))
1425 adapter = NULL;
1426
1427 mutex_unlock(&core_lock);
1428 return adapter;
1429}
1430EXPORT_SYMBOL(i2c_get_adapter);
1431
1432void i2c_put_adapter(struct i2c_adapter *adap)
1433{
1434 module_put(adap->owner);
1435}
1436EXPORT_SYMBOL(i2c_put_adapter);
1437
1438
1439
1440#define POLY (0x1070U << 3)
1441static u8 crc8(u16 data)
1442{
1443 int i;
1444
1445 for(i = 0; i < 8; i++) {
1446 if (data & 0x8000)
1447 data = data ^ POLY;
1448 data = data << 1;
1449 }
1450 return (u8)(data >> 8);
1451}
1452
1453
1454static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
1455{
1456 int i;
1457
1458 for(i = 0; i < count; i++)
1459 crc = crc8((crc ^ p[i]) << 8);
1460 return crc;
1461}
1462
1463
1464static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
1465{
1466
1467 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
1468 pec = i2c_smbus_pec(pec, &addr, 1);
1469
1470
1471 return i2c_smbus_pec(pec, msg->buf, msg->len);
1472}
1473
1474
1475static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
1476{
1477 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
1478 msg->len++;
1479}
1480
1481
1482
1483
1484
1485
1486static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
1487{
1488 u8 rpec = msg->buf[--msg->len];
1489 cpec = i2c_smbus_msg_pec(cpec, msg);
1490
1491 if (rpec != cpec) {
1492 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
1493 rpec, cpec);
1494 return -EBADMSG;
1495 }
1496 return 0;
1497}
1498
1499
1500
1501
1502
1503
1504
1505
1506s32 i2c_smbus_read_byte(struct i2c_client *client)
1507{
1508 union i2c_smbus_data data;
1509 int status;
1510
1511 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1512 I2C_SMBUS_READ, 0,
1513 I2C_SMBUS_BYTE, &data);
1514 return (status < 0) ? status : data.byte;
1515}
1516EXPORT_SYMBOL(i2c_smbus_read_byte);
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
1527{
1528 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1529 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
1530}
1531EXPORT_SYMBOL(i2c_smbus_write_byte);
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
1542{
1543 union i2c_smbus_data data;
1544 int status;
1545
1546 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1547 I2C_SMBUS_READ, command,
1548 I2C_SMBUS_BYTE_DATA, &data);
1549 return (status < 0) ? status : data.byte;
1550}
1551EXPORT_SYMBOL(i2c_smbus_read_byte_data);
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
1563{
1564 union i2c_smbus_data data;
1565 data.byte = value;
1566 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1567 I2C_SMBUS_WRITE,command,
1568 I2C_SMBUS_BYTE_DATA,&data);
1569}
1570EXPORT_SYMBOL(i2c_smbus_write_byte_data);
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
1581{
1582 union i2c_smbus_data data;
1583 int status;
1584
1585 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1586 I2C_SMBUS_READ, command,
1587 I2C_SMBUS_WORD_DATA, &data);
1588 return (status < 0) ? status : data.word;
1589}
1590EXPORT_SYMBOL(i2c_smbus_read_word_data);
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
1602{
1603 union i2c_smbus_data data;
1604 data.word = value;
1605 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1606 I2C_SMBUS_WRITE,command,
1607 I2C_SMBUS_WORD_DATA,&data);
1608}
1609EXPORT_SYMBOL(i2c_smbus_write_word_data);
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620s32 i2c_smbus_process_call(struct i2c_client *client, u8 command, u16 value)
1621{
1622 union i2c_smbus_data data;
1623 int status;
1624 data.word = value;
1625
1626 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1627 I2C_SMBUS_WRITE, command,
1628 I2C_SMBUS_PROC_CALL, &data);
1629 return (status < 0) ? status : data.word;
1630}
1631EXPORT_SYMBOL(i2c_smbus_process_call);
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648s32 i2c_smbus_read_block_data(struct i2c_client *client, u8 command,
1649 u8 *values)
1650{
1651 union i2c_smbus_data data;
1652 int status;
1653
1654 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1655 I2C_SMBUS_READ, command,
1656 I2C_SMBUS_BLOCK_DATA, &data);
1657 if (status)
1658 return status;
1659
1660 memcpy(values, &data.block[1], data.block[0]);
1661 return data.block[0];
1662}
1663EXPORT_SYMBOL(i2c_smbus_read_block_data);
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
1676 u8 length, const u8 *values)
1677{
1678 union i2c_smbus_data data;
1679
1680 if (length > I2C_SMBUS_BLOCK_MAX)
1681 length = I2C_SMBUS_BLOCK_MAX;
1682 data.block[0] = length;
1683 memcpy(&data.block[1], values, length);
1684 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1685 I2C_SMBUS_WRITE,command,
1686 I2C_SMBUS_BLOCK_DATA,&data);
1687}
1688EXPORT_SYMBOL(i2c_smbus_write_block_data);
1689
1690
1691s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command,
1692 u8 length, u8 *values)
1693{
1694 union i2c_smbus_data data;
1695 int status;
1696
1697 if (length > I2C_SMBUS_BLOCK_MAX)
1698 length = I2C_SMBUS_BLOCK_MAX;
1699 data.block[0] = length;
1700 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1701 I2C_SMBUS_READ, command,
1702 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1703 if (status < 0)
1704 return status;
1705
1706 memcpy(values, &data.block[1], data.block[0]);
1707 return data.block[0];
1708}
1709EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
1710
1711s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
1712 u8 length, const u8 *values)
1713{
1714 union i2c_smbus_data data;
1715
1716 if (length > I2C_SMBUS_BLOCK_MAX)
1717 length = I2C_SMBUS_BLOCK_MAX;
1718 data.block[0] = length;
1719 memcpy(data.block + 1, values, length);
1720 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1721 I2C_SMBUS_WRITE, command,
1722 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1723}
1724EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
1725
1726
1727
1728static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
1729 unsigned short flags,
1730 char read_write, u8 command, int size,
1731 union i2c_smbus_data * data)
1732{
1733
1734
1735
1736
1737 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
1738 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
1739 int num = read_write == I2C_SMBUS_READ?2:1;
1740 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
1741 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1742 };
1743 int i;
1744 u8 partial_pec = 0;
1745 int status;
1746
1747 msgbuf0[0] = command;
1748 switch(size) {
1749 case I2C_SMBUS_QUICK:
1750 msg[0].len = 0;
1751
1752 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
1753 I2C_M_RD : 0);
1754 num = 1;
1755 break;
1756 case I2C_SMBUS_BYTE:
1757 if (read_write == I2C_SMBUS_READ) {
1758
1759 msg[0].flags = I2C_M_RD | flags;
1760 num = 1;
1761 }
1762 break;
1763 case I2C_SMBUS_BYTE_DATA:
1764 if (read_write == I2C_SMBUS_READ)
1765 msg[1].len = 1;
1766 else {
1767 msg[0].len = 2;
1768 msgbuf0[1] = data->byte;
1769 }
1770 break;
1771 case I2C_SMBUS_WORD_DATA:
1772 if (read_write == I2C_SMBUS_READ)
1773 msg[1].len = 2;
1774 else {
1775 msg[0].len=3;
1776 msgbuf0[1] = data->word & 0xff;
1777 msgbuf0[2] = data->word >> 8;
1778 }
1779 break;
1780 case I2C_SMBUS_PROC_CALL:
1781 num = 2;
1782 read_write = I2C_SMBUS_READ;
1783 msg[0].len = 3;
1784 msg[1].len = 2;
1785 msgbuf0[1] = data->word & 0xff;
1786 msgbuf0[2] = data->word >> 8;
1787 break;
1788 case I2C_SMBUS_BLOCK_DATA:
1789 if (read_write == I2C_SMBUS_READ) {
1790 msg[1].flags |= I2C_M_RECV_LEN;
1791 msg[1].len = 1;
1792
1793 } else {
1794 msg[0].len = data->block[0] + 2;
1795 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
1796 dev_err(&adapter->dev,
1797 "Invalid block write size %d\n",
1798 data->block[0]);
1799 return -EINVAL;
1800 }
1801 for (i = 1; i < msg[0].len; i++)
1802 msgbuf0[i] = data->block[i-1];
1803 }
1804 break;
1805 case I2C_SMBUS_BLOCK_PROC_CALL:
1806 num = 2;
1807 read_write = I2C_SMBUS_READ;
1808 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
1809 dev_err(&adapter->dev,
1810 "Invalid block write size %d\n",
1811 data->block[0]);
1812 return -EINVAL;
1813 }
1814 msg[0].len = data->block[0] + 2;
1815 for (i = 1; i < msg[0].len; i++)
1816 msgbuf0[i] = data->block[i-1];
1817 msg[1].flags |= I2C_M_RECV_LEN;
1818 msg[1].len = 1;
1819
1820 break;
1821 case I2C_SMBUS_I2C_BLOCK_DATA:
1822 if (read_write == I2C_SMBUS_READ) {
1823 msg[1].len = data->block[0];
1824 } else {
1825 msg[0].len = data->block[0] + 1;
1826 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
1827 dev_err(&adapter->dev,
1828 "Invalid block write size %d\n",
1829 data->block[0]);
1830 return -EINVAL;
1831 }
1832 for (i = 1; i <= data->block[0]; i++)
1833 msgbuf0[i] = data->block[i];
1834 }
1835 break;
1836 default:
1837 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
1838 return -EOPNOTSUPP;
1839 }
1840
1841 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1842 && size != I2C_SMBUS_I2C_BLOCK_DATA);
1843 if (i) {
1844
1845 if (!(msg[0].flags & I2C_M_RD)) {
1846 if (num == 1)
1847 i2c_smbus_add_pec(&msg[0]);
1848 else
1849 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1850 }
1851
1852 if (msg[num-1].flags & I2C_M_RD)
1853 msg[num-1].len++;
1854 }
1855
1856 status = i2c_transfer(adapter, msg, num);
1857 if (status < 0)
1858 return status;
1859
1860
1861 if (i && (msg[num-1].flags & I2C_M_RD)) {
1862 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
1863 if (status < 0)
1864 return status;
1865 }
1866
1867 if (read_write == I2C_SMBUS_READ)
1868 switch(size) {
1869 case I2C_SMBUS_BYTE:
1870 data->byte = msgbuf0[0];
1871 break;
1872 case I2C_SMBUS_BYTE_DATA:
1873 data->byte = msgbuf1[0];
1874 break;
1875 case I2C_SMBUS_WORD_DATA:
1876 case I2C_SMBUS_PROC_CALL:
1877 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1878 break;
1879 case I2C_SMBUS_I2C_BLOCK_DATA:
1880 for (i = 0; i < data->block[0]; i++)
1881 data->block[i+1] = msgbuf1[i];
1882 break;
1883 case I2C_SMBUS_BLOCK_DATA:
1884 case I2C_SMBUS_BLOCK_PROC_CALL:
1885 for (i = 0; i < msgbuf1[0] + 1; i++)
1886 data->block[i] = msgbuf1[i];
1887 break;
1888 }
1889 return 0;
1890}
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
1906 char read_write, u8 command, int protocol,
1907 union i2c_smbus_data *data)
1908{
1909 unsigned long orig_jiffies;
1910 int try;
1911 s32 res;
1912
1913 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
1914
1915 if (adapter->algo->smbus_xfer) {
1916 mutex_lock(&adapter->bus_lock);
1917
1918
1919 orig_jiffies = jiffies;
1920 for (res = 0, try = 0; try <= adapter->retries; try++) {
1921 res = adapter->algo->smbus_xfer(adapter, addr, flags,
1922 read_write, command,
1923 protocol, data);
1924 if (res != -EAGAIN)
1925 break;
1926 if (time_after(jiffies,
1927 orig_jiffies + adapter->timeout))
1928 break;
1929 }
1930 mutex_unlock(&adapter->bus_lock);
1931 } else
1932 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
1933 command, protocol, data);
1934
1935 return res;
1936}
1937EXPORT_SYMBOL(i2c_smbus_xfer);
1938
1939MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1940MODULE_DESCRIPTION("I2C-Bus main module");
1941MODULE_LICENSE("GPL");
1942