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