i2c: i2c stack can remove()
[powerpc.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/seq_file.h>
33 #include <linux/platform_device.h>
34 #include <linux/mutex.h>
35 #include <linux/completion.h>
36 #include <asm/uaccess.h>
37
38
39 static LIST_HEAD(adapters);
40 static LIST_HEAD(drivers);
41 static DEFINE_MUTEX(core_lists);
42 static DEFINE_IDR(i2c_adapter_idr);
43
44 #define is_newstyle_driver(d) ((d)->probe || (d)->remove)
45
46 /* ------------------------------------------------------------------------- */
47
48 static int i2c_device_match(struct device *dev, struct device_driver *drv)
49 {
50         struct i2c_client       *client = to_i2c_client(dev);
51         struct i2c_driver       *driver = to_i2c_driver(drv);
52
53         /* make legacy i2c drivers bypass driver model probing entirely;
54          * such drivers scan each i2c adapter/bus themselves.
55          */
56         if (!is_newstyle_driver(driver))
57                 return 0;
58
59         /* new style drivers use the same kind of driver matching policy
60          * as platform devices or SPI:  compare device and driver IDs.
61          */
62         return strcmp(client->driver_name, drv->name) == 0;
63 }
64
65 #ifdef  CONFIG_HOTPLUG
66
67 /* uevent helps with hotplug: modprobe -q $(MODALIAS) */
68 static int i2c_device_uevent(struct device *dev, char **envp, int num_envp,
69                       char *buffer, int buffer_size)
70 {
71         struct i2c_client       *client = to_i2c_client(dev);
72         int                     i = 0, length = 0;
73
74         /* by definition, legacy drivers can't hotplug */
75         if (dev->driver || !client->driver_name)
76                 return 0;
77
78         if (add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
79                         "MODALIAS=%s", client->driver_name))
80                 return -ENOMEM;
81         envp[i] = NULL;
82         dev_dbg(dev, "uevent\n");
83         return 0;
84 }
85
86 #else
87 #define i2c_device_uevent       NULL
88 #endif  /* CONFIG_HOTPLUG */
89
90 static int i2c_device_probe(struct device *dev)
91 {
92         struct i2c_client       *client = to_i2c_client(dev);
93         struct i2c_driver       *driver = to_i2c_driver(dev->driver);
94
95         if (!driver->probe)
96                 return -ENODEV;
97         client->driver = driver;
98         dev_dbg(dev, "probe\n");
99         return driver->probe(client);
100 }
101
102 static int i2c_device_remove(struct device *dev)
103 {
104         struct i2c_client       *client = to_i2c_client(dev);
105         struct i2c_driver       *driver;
106         int                     status;
107
108         if (!dev->driver)
109                 return 0;
110
111         driver = to_i2c_driver(dev->driver);
112         if (driver->remove) {
113                 dev_dbg(dev, "remove\n");
114                 status = driver->remove(client);
115         } else {
116                 dev->driver = NULL;
117                 status = 0;
118         }
119         if (status == 0)
120                 client->driver = NULL;
121         return status;
122 }
123
124 static void i2c_device_shutdown(struct device *dev)
125 {
126         struct i2c_driver *driver;
127
128         if (!dev->driver)
129                 return;
130         driver = to_i2c_driver(dev->driver);
131         if (driver->shutdown)
132                 driver->shutdown(to_i2c_client(dev));
133 }
134
135 static int i2c_device_suspend(struct device * dev, pm_message_t mesg)
136 {
137         struct i2c_driver *driver;
138
139         if (!dev->driver)
140                 return 0;
141         driver = to_i2c_driver(dev->driver);
142         if (!driver->suspend)
143                 return 0;
144         return driver->suspend(to_i2c_client(dev), mesg);
145 }
146
147 static int i2c_device_resume(struct device * dev)
148 {
149         struct i2c_driver *driver;
150
151         if (!dev->driver)
152                 return 0;
153         driver = to_i2c_driver(dev->driver);
154         if (!driver->resume)
155                 return 0;
156         return driver->resume(to_i2c_client(dev));
157 }
158
159 static void i2c_client_release(struct device *dev)
160 {
161         struct i2c_client *client = to_i2c_client(dev);
162         complete(&client->released);
163 }
164
165 static ssize_t show_client_name(struct device *dev, struct device_attribute *attr, char *buf)
166 {
167         struct i2c_client *client = to_i2c_client(dev);
168         return sprintf(buf, "%s\n", client->name);
169 }
170
171 static ssize_t show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
172 {
173         struct i2c_client *client = to_i2c_client(dev);
174         return client->driver_name
175                 ? sprintf(buf, "%s\n", client->driver_name)
176                 : 0;
177 }
178
179 static struct device_attribute i2c_dev_attrs[] = {
180         __ATTR(name, S_IRUGO, show_client_name, NULL),
181         /* modalias helps coldplug:  modprobe $(cat .../modalias) */
182         __ATTR(modalias, S_IRUGO, show_modalias, NULL),
183         { },
184 };
185
186 struct bus_type i2c_bus_type = {
187         .name           = "i2c",
188         .dev_attrs      = i2c_dev_attrs,
189         .match          = i2c_device_match,
190         .uevent         = i2c_device_uevent,
191         .probe          = i2c_device_probe,
192         .remove         = i2c_device_remove,
193         .shutdown       = i2c_device_shutdown,
194         .suspend        = i2c_device_suspend,
195         .resume         = i2c_device_resume,
196 };
197
198 static void i2c_unregister_device(struct i2c_client *client)
199 {
200         struct i2c_adapter      *adapter = client->adapter;
201         struct i2c_driver       *driver = client->driver;
202
203         if (driver && !is_newstyle_driver(driver)) {
204                 dev_err(&client->dev, "can't unregister devices "
205                         "with legacy drivers\n");
206                 WARN_ON(1);
207                 return;
208         }
209
210         mutex_lock(&adapter->clist_lock);
211         list_del(&client->list);
212         mutex_unlock(&adapter->clist_lock);
213
214         device_unregister(&client->dev);
215 }
216
217
218 /* ------------------------------------------------------------------------- */
219
220 /* I2C bus adapters -- one roots each I2C or SMBUS segment */
221
222 void i2c_adapter_dev_release(struct device *dev)
223 {
224         struct i2c_adapter *adap = to_i2c_adapter(dev);
225         complete(&adap->dev_released);
226 }
227
228 static ssize_t
229 show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf)
230 {
231         struct i2c_adapter *adap = to_i2c_adapter(dev);
232         return sprintf(buf, "%s\n", adap->name);
233 }
234
235 static struct device_attribute i2c_adapter_attrs[] = {
236         __ATTR(name, S_IRUGO, show_adapter_name, NULL),
237         { },
238 };
239
240 struct class i2c_adapter_class = {
241         .owner                  = THIS_MODULE,
242         .name                   = "i2c-adapter",
243         .dev_attrs              = i2c_adapter_attrs,
244 };
245
246
247 /* -----
248  * i2c_add_adapter is called from within the algorithm layer,
249  * when a new hw adapter registers. A new device is register to be
250  * available for clients.
251  */
252 int i2c_add_adapter(struct i2c_adapter *adap)
253 {
254         int id, res = 0;
255         struct list_head   *item;
256         struct i2c_driver  *driver;
257
258         mutex_lock(&core_lists);
259
260         if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0) {
261                 res = -ENOMEM;
262                 goto out_unlock;
263         }
264
265         res = idr_get_new(&i2c_adapter_idr, adap, &id);
266         if (res < 0) {
267                 if (res == -EAGAIN)
268                         res = -ENOMEM;
269                 goto out_unlock;
270         }
271
272         adap->nr =  id & MAX_ID_MASK;
273         mutex_init(&adap->bus_lock);
274         mutex_init(&adap->clist_lock);
275         list_add_tail(&adap->list,&adapters);
276         INIT_LIST_HEAD(&adap->clients);
277
278         /* Add the adapter to the driver core.
279          * If the parent pointer is not set up,
280          * we add this adapter to the host bus.
281          */
282         if (adap->dev.parent == NULL) {
283                 adap->dev.parent = &platform_bus;
284                 pr_debug("I2C adapter driver [%s] forgot to specify "
285                          "physical device\n", adap->name);
286         }
287         sprintf(adap->dev.bus_id, "i2c-%d", adap->nr);
288         adap->dev.release = &i2c_adapter_dev_release;
289         adap->dev.class = &i2c_adapter_class;
290         res = device_register(&adap->dev);
291         if (res)
292                 goto out_list;
293
294         dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
295
296         /* let legacy drivers scan this bus for matching devices */
297         list_for_each(item,&drivers) {
298                 driver = list_entry(item, struct i2c_driver, list);
299                 if (driver->attach_adapter)
300                         /* We ignore the return code; if it fails, too bad */
301                         driver->attach_adapter(adap);
302         }
303
304 out_unlock:
305         mutex_unlock(&core_lists);
306         return res;
307
308 out_list:
309         list_del(&adap->list);
310         idr_remove(&i2c_adapter_idr, adap->nr);
311         goto out_unlock;
312 }
313
314
315 int i2c_del_adapter(struct i2c_adapter *adap)
316 {
317         struct list_head  *item, *_n;
318         struct i2c_adapter *adap_from_list;
319         struct i2c_driver *driver;
320         struct i2c_client *client;
321         int res = 0;
322
323         mutex_lock(&core_lists);
324
325         /* First make sure that this adapter was ever added */
326         list_for_each_entry(adap_from_list, &adapters, list) {
327                 if (adap_from_list == adap)
328                         break;
329         }
330         if (adap_from_list != adap) {
331                 pr_debug("i2c-core: attempting to delete unregistered "
332                          "adapter [%s]\n", adap->name);
333                 res = -EINVAL;
334                 goto out_unlock;
335         }
336
337         list_for_each(item,&drivers) {
338                 driver = list_entry(item, struct i2c_driver, list);
339                 if (driver->detach_adapter)
340                         if ((res = driver->detach_adapter(adap))) {
341                                 dev_err(&adap->dev, "detach_adapter failed "
342                                         "for driver [%s]\n",
343                                         driver->driver.name);
344                                 goto out_unlock;
345                         }
346         }
347
348         /* detach any active clients. This must be done first, because
349          * it can fail; in which case we give up. */
350         list_for_each_safe(item, _n, &adap->clients) {
351                 struct i2c_driver       *driver;
352
353                 client = list_entry(item, struct i2c_client, list);
354                 driver = client->driver;
355
356                 /* new style, follow standard driver model */
357                 if (!driver || is_newstyle_driver(driver)) {
358                         i2c_unregister_device(client);
359                         continue;
360                 }
361
362                 /* legacy drivers create and remove clients themselves */
363                 if ((res = driver->detach_client(client))) {
364                         dev_err(&adap->dev, "detach_client failed for client "
365                                 "[%s] at address 0x%02x\n", client->name,
366                                 client->addr);
367                         goto out_unlock;
368                 }
369         }
370
371         /* clean up the sysfs representation */
372         init_completion(&adap->dev_released);
373         device_unregister(&adap->dev);
374         list_del(&adap->list);
375
376         /* wait for sysfs to drop all references */
377         wait_for_completion(&adap->dev_released);
378
379         /* free dynamically allocated bus id */
380         idr_remove(&i2c_adapter_idr, adap->nr);
381
382         dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
383
384  out_unlock:
385         mutex_unlock(&core_lists);
386         return res;
387 }
388
389
390 /* ------------------------------------------------------------------------- */
391
392 /*
393  * An i2c_driver is used with one or more i2c_client (device) nodes to access
394  * i2c slave chips, on a bus instance associated with some i2c_adapter.  There
395  * are two models for binding the driver to its device:  "new style" drivers
396  * follow the standard Linux driver model and just respond to probe() calls
397  * issued if the driver core sees they match(); "legacy" drivers create device
398  * nodes themselves.
399  */
400
401 int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
402 {
403         int res;
404
405         /* new style driver methods can't mix with legacy ones */
406         if (is_newstyle_driver(driver)) {
407                 if (driver->attach_adapter || driver->detach_adapter
408                                 || driver->detach_client) {
409                         printk(KERN_WARNING
410                                         "i2c-core: driver [%s] is confused\n",
411                                         driver->driver.name);
412                         return -EINVAL;
413                 }
414         }
415
416         /* add the driver to the list of i2c drivers in the driver core */
417         driver->driver.owner = owner;
418         driver->driver.bus = &i2c_bus_type;
419
420         res = driver_register(&driver->driver);
421         if (res)
422                 return res;
423
424         mutex_lock(&core_lists);
425
426         list_add_tail(&driver->list,&drivers);
427         pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
428
429         /* legacy drivers scan i2c busses directly */
430         if (driver->attach_adapter) {
431                 struct i2c_adapter *adapter;
432
433                 list_for_each_entry(adapter, &adapters, list) {
434                         driver->attach_adapter(adapter);
435                 }
436         }
437
438         mutex_unlock(&core_lists);
439         return 0;
440 }
441 EXPORT_SYMBOL(i2c_register_driver);
442
443 /**
444  * i2c_del_driver - unregister I2C driver
445  * @driver: the driver being unregistered
446  */
447 int i2c_del_driver(struct i2c_driver *driver)
448 {
449         struct list_head   *item1, *item2, *_n;
450         struct i2c_client  *client;
451         struct i2c_adapter *adap;
452
453         int res = 0;
454
455         mutex_lock(&core_lists);
456
457         /* new-style driver? */
458         if (is_newstyle_driver(driver))
459                 goto unregister;
460
461         /* Have a look at each adapter, if clients of this driver are still
462          * attached. If so, detach them to be able to kill the driver
463          * afterwards.
464          */
465         list_for_each(item1,&adapters) {
466                 adap = list_entry(item1, struct i2c_adapter, list);
467                 if (driver->detach_adapter) {
468                         if ((res = driver->detach_adapter(adap))) {
469                                 dev_err(&adap->dev, "detach_adapter failed "
470                                         "for driver [%s]\n",
471                                         driver->driver.name);
472                                 goto out_unlock;
473                         }
474                 } else {
475                         list_for_each_safe(item2, _n, &adap->clients) {
476                                 client = list_entry(item2, struct i2c_client, list);
477                                 if (client->driver != driver)
478                                         continue;
479                                 dev_dbg(&adap->dev, "detaching client [%s] "
480                                         "at 0x%02x\n", client->name,
481                                         client->addr);
482                                 if ((res = driver->detach_client(client))) {
483                                         dev_err(&adap->dev, "detach_client "
484                                                 "failed for client [%s] at "
485                                                 "0x%02x\n", client->name,
486                                                 client->addr);
487                                         goto out_unlock;
488                                 }
489                         }
490                 }
491         }
492
493  unregister:
494         driver_unregister(&driver->driver);
495         list_del(&driver->list);
496         pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
497
498  out_unlock:
499         mutex_unlock(&core_lists);
500         return 0;
501 }
502
503 /* ------------------------------------------------------------------------- */
504
505 static int __i2c_check_addr(struct i2c_adapter *adapter, unsigned int addr)
506 {
507         struct list_head   *item;
508         struct i2c_client  *client;
509
510         list_for_each(item,&adapter->clients) {
511                 client = list_entry(item, struct i2c_client, list);
512                 if (client->addr == addr)
513                         return -EBUSY;
514         }
515         return 0;
516 }
517
518 int i2c_check_addr(struct i2c_adapter *adapter, int addr)
519 {
520         int rval;
521
522         mutex_lock(&adapter->clist_lock);
523         rval = __i2c_check_addr(adapter, addr);
524         mutex_unlock(&adapter->clist_lock);
525
526         return rval;
527 }
528
529 int i2c_attach_client(struct i2c_client *client)
530 {
531         struct i2c_adapter *adapter = client->adapter;
532         int res = 0;
533
534         mutex_lock(&adapter->clist_lock);
535         if (__i2c_check_addr(client->adapter, client->addr)) {
536                 res = -EBUSY;
537                 goto out_unlock;
538         }
539         list_add_tail(&client->list,&adapter->clients);
540
541         client->usage_count = 0;
542
543         client->dev.parent = &client->adapter->dev;
544         client->dev.driver = &client->driver->driver;
545         client->dev.bus = &i2c_bus_type;
546         client->dev.release = &i2c_client_release;
547
548         snprintf(&client->dev.bus_id[0], sizeof(client->dev.bus_id),
549                 "%d-%04x", i2c_adapter_id(adapter), client->addr);
550         dev_dbg(&adapter->dev, "client [%s] registered with bus id %s\n",
551                 client->name, client->dev.bus_id);
552         res = device_register(&client->dev);
553         if (res)
554                 goto out_list;
555         mutex_unlock(&adapter->clist_lock);
556
557         if (adapter->client_register)  {
558                 if (adapter->client_register(client)) {
559                         dev_dbg(&adapter->dev, "client_register "
560                                 "failed for client [%s] at 0x%02x\n",
561                                 client->name, client->addr);
562                 }
563         }
564
565         return 0;
566
567 out_list:
568         list_del(&client->list);
569         dev_err(&adapter->dev, "Failed to attach i2c client %s at 0x%02x "
570                 "(%d)\n", client->name, client->addr, res);
571 out_unlock:
572         mutex_unlock(&adapter->clist_lock);
573         return res;
574 }
575
576
577 int i2c_detach_client(struct i2c_client *client)
578 {
579         struct i2c_adapter *adapter = client->adapter;
580         int res = 0;
581
582         if (client->usage_count > 0) {
583                 dev_warn(&client->dev, "Client [%s] still busy, "
584                          "can't detach\n", client->name);
585                 return -EBUSY;
586         }
587
588         if (adapter->client_unregister)  {
589                 res = adapter->client_unregister(client);
590                 if (res) {
591                         dev_err(&client->dev,
592                                 "client_unregister [%s] failed, "
593                                 "client not detached\n", client->name);
594                         goto out;
595                 }
596         }
597
598         mutex_lock(&adapter->clist_lock);
599         list_del(&client->list);
600         init_completion(&client->released);
601         device_unregister(&client->dev);
602         mutex_unlock(&adapter->clist_lock);
603         wait_for_completion(&client->released);
604
605  out:
606         return res;
607 }
608
609 static int i2c_inc_use_client(struct i2c_client *client)
610 {
611
612         if (!try_module_get(client->driver->driver.owner))
613                 return -ENODEV;
614         if (!try_module_get(client->adapter->owner)) {
615                 module_put(client->driver->driver.owner);
616                 return -ENODEV;
617         }
618
619         return 0;
620 }
621
622 static void i2c_dec_use_client(struct i2c_client *client)
623 {
624         module_put(client->driver->driver.owner);
625         module_put(client->adapter->owner);
626 }
627
628 int i2c_use_client(struct i2c_client *client)
629 {
630         int ret;
631
632         ret = i2c_inc_use_client(client);
633         if (ret)
634                 return ret;
635
636         client->usage_count++;
637
638         return 0;
639 }
640
641 int i2c_release_client(struct i2c_client *client)
642 {
643         if (!client->usage_count) {
644                 pr_debug("i2c-core: %s used one too many times\n",
645                          __FUNCTION__);
646                 return -EPERM;
647         }
648
649         client->usage_count--;
650         i2c_dec_use_client(client);
651
652         return 0;
653 }
654
655 void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
656 {
657         struct list_head  *item;
658         struct i2c_client *client;
659
660         mutex_lock(&adap->clist_lock);
661         list_for_each(item,&adap->clients) {
662                 client = list_entry(item, struct i2c_client, list);
663                 if (!try_module_get(client->driver->driver.owner))
664                         continue;
665                 if (NULL != client->driver->command) {
666                         mutex_unlock(&adap->clist_lock);
667                         client->driver->command(client,cmd,arg);
668                         mutex_lock(&adap->clist_lock);
669                 }
670                 module_put(client->driver->driver.owner);
671        }
672        mutex_unlock(&adap->clist_lock);
673 }
674
675 static int __init i2c_init(void)
676 {
677         int retval;
678
679         retval = bus_register(&i2c_bus_type);
680         if (retval)
681                 return retval;
682         return class_register(&i2c_adapter_class);
683 }
684
685 static void __exit i2c_exit(void)
686 {
687         class_unregister(&i2c_adapter_class);
688         bus_unregister(&i2c_bus_type);
689 }
690
691 subsys_initcall(i2c_init);
692 module_exit(i2c_exit);
693
694 /* ----------------------------------------------------
695  * the functional interface to the i2c busses.
696  * ----------------------------------------------------
697  */
698
699 int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg *msgs, int num)
700 {
701         int ret;
702
703         if (adap->algo->master_xfer) {
704 #ifdef DEBUG
705                 for (ret = 0; ret < num; ret++) {
706                         dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
707                                 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
708                                 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
709                                 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
710                 }
711 #endif
712
713                 mutex_lock_nested(&adap->bus_lock, adap->level);
714                 ret = adap->algo->master_xfer(adap,msgs,num);
715                 mutex_unlock(&adap->bus_lock);
716
717                 return ret;
718         } else {
719                 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
720                 return -ENOSYS;
721         }
722 }
723
724 int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
725 {
726         int ret;
727         struct i2c_adapter *adap=client->adapter;
728         struct i2c_msg msg;
729
730         msg.addr = client->addr;
731         msg.flags = client->flags & I2C_M_TEN;
732         msg.len = count;
733         msg.buf = (char *)buf;
734
735         ret = i2c_transfer(adap, &msg, 1);
736
737         /* If everything went ok (i.e. 1 msg transmitted), return #bytes
738            transmitted, else error code. */
739         return (ret == 1) ? count : ret;
740 }
741
742 int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
743 {
744         struct i2c_adapter *adap=client->adapter;
745         struct i2c_msg msg;
746         int ret;
747
748         msg.addr = client->addr;
749         msg.flags = client->flags & I2C_M_TEN;
750         msg.flags |= I2C_M_RD;
751         msg.len = count;
752         msg.buf = buf;
753
754         ret = i2c_transfer(adap, &msg, 1);
755
756         /* If everything went ok (i.e. 1 msg transmitted), return #bytes
757            transmitted, else error code. */
758         return (ret == 1) ? count : ret;
759 }
760
761
762 int i2c_control(struct i2c_client *client,
763         unsigned int cmd, unsigned long arg)
764 {
765         int ret = 0;
766         struct i2c_adapter *adap = client->adapter;
767
768         dev_dbg(&client->adapter->dev, "i2c ioctl, cmd: 0x%x, arg: %#lx\n", cmd, arg);
769         switch (cmd) {
770                 case I2C_RETRIES:
771                         adap->retries = arg;
772                         break;
773                 case I2C_TIMEOUT:
774                         adap->timeout = arg;
775                         break;
776                 default:
777                         if (adap->algo->algo_control!=NULL)
778                                 ret = adap->algo->algo_control(adap,cmd,arg);
779         }
780         return ret;
781 }
782
783 /* ----------------------------------------------------
784  * the i2c address scanning function
785  * Will not work for 10-bit addresses!
786  * ----------------------------------------------------
787  */
788 static int i2c_probe_address(struct i2c_adapter *adapter, int addr, int kind,
789                              int (*found_proc) (struct i2c_adapter *, int, int))
790 {
791         int err;
792
793         /* Make sure the address is valid */
794         if (addr < 0x03 || addr > 0x77) {
795                 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
796                          addr);
797                 return -EINVAL;
798         }
799
800         /* Skip if already in use */
801         if (i2c_check_addr(adapter, addr))
802                 return 0;
803
804         /* Make sure there is something at this address, unless forced */
805         if (kind < 0) {
806                 if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
807                                    I2C_SMBUS_QUICK, NULL) < 0)
808                         return 0;
809
810                 /* prevent 24RF08 corruption */
811                 if ((addr & ~0x0f) == 0x50)
812                         i2c_smbus_xfer(adapter, addr, 0, 0, 0,
813                                        I2C_SMBUS_QUICK, NULL);
814         }
815
816         /* Finally call the custom detection function */
817         err = found_proc(adapter, addr, kind);
818         /* -ENODEV can be returned if there is a chip at the given address
819            but it isn't supported by this chip driver. We catch it here as
820            this isn't an error. */
821         if (err == -ENODEV)
822                 err = 0;
823
824         if (err)
825                 dev_warn(&adapter->dev, "Client creation failed at 0x%x (%d)\n",
826                          addr, err);
827         return err;
828 }
829
830 int i2c_probe(struct i2c_adapter *adapter,
831               struct i2c_client_address_data *address_data,
832               int (*found_proc) (struct i2c_adapter *, int, int))
833 {
834         int i, err;
835         int adap_id = i2c_adapter_id(adapter);
836
837         /* Force entries are done first, and are not affected by ignore
838            entries */
839         if (address_data->forces) {
840                 unsigned short **forces = address_data->forces;
841                 int kind;
842
843                 for (kind = 0; forces[kind]; kind++) {
844                         for (i = 0; forces[kind][i] != I2C_CLIENT_END;
845                              i += 2) {
846                                 if (forces[kind][i] == adap_id
847                                  || forces[kind][i] == ANY_I2C_BUS) {
848                                         dev_dbg(&adapter->dev, "found force "
849                                                 "parameter for adapter %d, "
850                                                 "addr 0x%02x, kind %d\n",
851                                                 adap_id, forces[kind][i + 1],
852                                                 kind);
853                                         err = i2c_probe_address(adapter,
854                                                 forces[kind][i + 1],
855                                                 kind, found_proc);
856                                         if (err)
857                                                 return err;
858                                 }
859                         }
860                 }
861         }
862
863         /* Stop here if we can't use SMBUS_QUICK */
864         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
865                 if (address_data->probe[0] == I2C_CLIENT_END
866                  && address_data->normal_i2c[0] == I2C_CLIENT_END)
867                         return 0;
868
869                 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
870                          "can't probe for chips\n");
871                 return -1;
872         }
873
874         /* Probe entries are done second, and are not affected by ignore
875            entries either */
876         for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
877                 if (address_data->probe[i] == adap_id
878                  || address_data->probe[i] == ANY_I2C_BUS) {
879                         dev_dbg(&adapter->dev, "found probe parameter for "
880                                 "adapter %d, addr 0x%02x\n", adap_id,
881                                 address_data->probe[i + 1]);
882                         err = i2c_probe_address(adapter,
883                                                 address_data->probe[i + 1],
884                                                 -1, found_proc);
885                         if (err)
886                                 return err;
887                 }
888         }
889
890         /* Normal entries are done last, unless shadowed by an ignore entry */
891         for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
892                 int j, ignore;
893
894                 ignore = 0;
895                 for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
896                      j += 2) {
897                         if ((address_data->ignore[j] == adap_id ||
898                              address_data->ignore[j] == ANY_I2C_BUS)
899                          && address_data->ignore[j + 1]
900                             == address_data->normal_i2c[i]) {
901                                 dev_dbg(&adapter->dev, "found ignore "
902                                         "parameter for adapter %d, "
903                                         "addr 0x%02x\n", adap_id,
904                                         address_data->ignore[j + 1]);
905                                 ignore = 1;
906                                 break;
907                         }
908                 }
909                 if (ignore)
910                         continue;
911
912                 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
913                         "addr 0x%02x\n", adap_id,
914                         address_data->normal_i2c[i]);
915                 err = i2c_probe_address(adapter, address_data->normal_i2c[i],
916                                         -1, found_proc);
917                 if (err)
918                         return err;
919         }
920
921         return 0;
922 }
923
924 struct i2c_adapter* i2c_get_adapter(int id)
925 {
926         struct i2c_adapter *adapter;
927
928         mutex_lock(&core_lists);
929         adapter = (struct i2c_adapter *)idr_find(&i2c_adapter_idr, id);
930         if (adapter && !try_module_get(adapter->owner))
931                 adapter = NULL;
932
933         mutex_unlock(&core_lists);
934         return adapter;
935 }
936
937 void i2c_put_adapter(struct i2c_adapter *adap)
938 {
939         module_put(adap->owner);
940 }
941
942 /* The SMBus parts */
943
944 #define POLY    (0x1070U << 3)
945 static u8
946 crc8(u16 data)
947 {
948         int i;
949
950         for(i = 0; i < 8; i++) {
951                 if (data & 0x8000)
952                         data = data ^ POLY;
953                 data = data << 1;
954         }
955         return (u8)(data >> 8);
956 }
957
958 /* Incremental CRC8 over count bytes in the array pointed to by p */
959 static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
960 {
961         int i;
962
963         for(i = 0; i < count; i++)
964                 crc = crc8((crc ^ p[i]) << 8);
965         return crc;
966 }
967
968 /* Assume a 7-bit address, which is reasonable for SMBus */
969 static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
970 {
971         /* The address will be sent first */
972         u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
973         pec = i2c_smbus_pec(pec, &addr, 1);
974
975         /* The data buffer follows */
976         return i2c_smbus_pec(pec, msg->buf, msg->len);
977 }
978
979 /* Used for write only transactions */
980 static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
981 {
982         msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
983         msg->len++;
984 }
985
986 /* Return <0 on CRC error
987    If there was a write before this read (most cases) we need to take the
988    partial CRC from the write part into account.
989    Note that this function does modify the message (we need to decrease the
990    message length to hide the CRC byte from the caller). */
991 static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
992 {
993         u8 rpec = msg->buf[--msg->len];
994         cpec = i2c_smbus_msg_pec(cpec, msg);
995
996         if (rpec != cpec) {
997                 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
998                         rpec, cpec);
999                 return -1;
1000         }
1001         return 0;
1002 }
1003
1004 s32 i2c_smbus_write_quick(struct i2c_client *client, u8 value)
1005 {
1006         return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1007                               value,0,I2C_SMBUS_QUICK,NULL);
1008 }
1009
1010 s32 i2c_smbus_read_byte(struct i2c_client *client)
1011 {
1012         union i2c_smbus_data data;
1013         if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1014                            I2C_SMBUS_READ,0,I2C_SMBUS_BYTE, &data))
1015                 return -1;
1016         else
1017                 return data.byte;
1018 }
1019
1020 s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
1021 {
1022         return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1023                               I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
1024 }
1025
1026 s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
1027 {
1028         union i2c_smbus_data data;
1029         if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1030                            I2C_SMBUS_READ,command, I2C_SMBUS_BYTE_DATA,&data))
1031                 return -1;
1032         else
1033                 return data.byte;
1034 }
1035
1036 s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
1037 {
1038         union i2c_smbus_data data;
1039         data.byte = value;
1040         return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1041                               I2C_SMBUS_WRITE,command,
1042                               I2C_SMBUS_BYTE_DATA,&data);
1043 }
1044
1045 s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
1046 {
1047         union i2c_smbus_data data;
1048         if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1049                            I2C_SMBUS_READ,command, I2C_SMBUS_WORD_DATA, &data))
1050                 return -1;
1051         else
1052                 return data.word;
1053 }
1054
1055 s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
1056 {
1057         union i2c_smbus_data data;
1058         data.word = value;
1059         return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1060                               I2C_SMBUS_WRITE,command,
1061                               I2C_SMBUS_WORD_DATA,&data);
1062 }
1063
1064 s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
1065                                u8 length, const u8 *values)
1066 {
1067         union i2c_smbus_data data;
1068
1069         if (length > I2C_SMBUS_BLOCK_MAX)
1070                 length = I2C_SMBUS_BLOCK_MAX;
1071         data.block[0] = length;
1072         memcpy(&data.block[1], values, length);
1073         return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1074                               I2C_SMBUS_WRITE,command,
1075                               I2C_SMBUS_BLOCK_DATA,&data);
1076 }
1077
1078 /* Returns the number of read bytes */
1079 s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command, u8 *values)
1080 {
1081         union i2c_smbus_data data;
1082
1083         if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1084                               I2C_SMBUS_READ,command,
1085                               I2C_SMBUS_I2C_BLOCK_DATA,&data))
1086                 return -1;
1087
1088         memcpy(values, &data.block[1], data.block[0]);
1089         return data.block[0];
1090 }
1091
1092 s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
1093                                    u8 length, const u8 *values)
1094 {
1095         union i2c_smbus_data data;
1096
1097         if (length > I2C_SMBUS_BLOCK_MAX)
1098                 length = I2C_SMBUS_BLOCK_MAX;
1099         data.block[0] = length;
1100         memcpy(data.block + 1, values, length);
1101         return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1102                               I2C_SMBUS_WRITE, command,
1103                               I2C_SMBUS_I2C_BLOCK_DATA, &data);
1104 }
1105
1106 /* Simulate a SMBus command using the i2c protocol
1107    No checking of parameters is done!  */
1108 static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
1109                                    unsigned short flags,
1110                                    char read_write, u8 command, int size,
1111                                    union i2c_smbus_data * data)
1112 {
1113         /* So we need to generate a series of msgs. In the case of writing, we
1114           need to use only one message; when reading, we need two. We initialize
1115           most things with sane defaults, to keep the code below somewhat
1116           simpler. */
1117         unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
1118         unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
1119         int num = read_write == I2C_SMBUS_READ?2:1;
1120         struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
1121                                   { addr, flags | I2C_M_RD, 0, msgbuf1 }
1122                                 };
1123         int i;
1124         u8 partial_pec = 0;
1125
1126         msgbuf0[0] = command;
1127         switch(size) {
1128         case I2C_SMBUS_QUICK:
1129                 msg[0].len = 0;
1130                 /* Special case: The read/write field is used as data */
1131                 msg[0].flags = flags | (read_write==I2C_SMBUS_READ)?I2C_M_RD:0;
1132                 num = 1;
1133                 break;
1134         case I2C_SMBUS_BYTE:
1135                 if (read_write == I2C_SMBUS_READ) {
1136                         /* Special case: only a read! */
1137                         msg[0].flags = I2C_M_RD | flags;
1138                         num = 1;
1139                 }
1140                 break;
1141         case I2C_SMBUS_BYTE_DATA:
1142                 if (read_write == I2C_SMBUS_READ)
1143                         msg[1].len = 1;
1144                 else {
1145                         msg[0].len = 2;
1146                         msgbuf0[1] = data->byte;
1147                 }
1148                 break;
1149         case I2C_SMBUS_WORD_DATA:
1150                 if (read_write == I2C_SMBUS_READ)
1151                         msg[1].len = 2;
1152                 else {
1153                         msg[0].len=3;
1154                         msgbuf0[1] = data->word & 0xff;
1155                         msgbuf0[2] = data->word >> 8;
1156                 }
1157                 break;
1158         case I2C_SMBUS_PROC_CALL:
1159                 num = 2; /* Special case */
1160                 read_write = I2C_SMBUS_READ;
1161                 msg[0].len = 3;
1162                 msg[1].len = 2;
1163                 msgbuf0[1] = data->word & 0xff;
1164                 msgbuf0[2] = data->word >> 8;
1165                 break;
1166         case I2C_SMBUS_BLOCK_DATA:
1167                 if (read_write == I2C_SMBUS_READ) {
1168                         msg[1].flags |= I2C_M_RECV_LEN;
1169                         msg[1].len = 1; /* block length will be added by
1170                                            the underlying bus driver */
1171                 } else {
1172                         msg[0].len = data->block[0] + 2;
1173                         if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
1174                                 dev_err(&adapter->dev, "smbus_access called with "
1175                                        "invalid block write size (%d)\n",
1176                                        data->block[0]);
1177                                 return -1;
1178                         }
1179                         for (i = 1; i < msg[0].len; i++)
1180                                 msgbuf0[i] = data->block[i-1];
1181                 }
1182                 break;
1183         case I2C_SMBUS_BLOCK_PROC_CALL:
1184                 num = 2; /* Another special case */
1185                 read_write = I2C_SMBUS_READ;
1186                 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
1187                         dev_err(&adapter->dev, "%s called with invalid "
1188                                 "block proc call size (%d)\n", __FUNCTION__,
1189                                 data->block[0]);
1190                         return -1;
1191                 }
1192                 msg[0].len = data->block[0] + 2;
1193                 for (i = 1; i < msg[0].len; i++)
1194                         msgbuf0[i] = data->block[i-1];
1195                 msg[1].flags |= I2C_M_RECV_LEN;
1196                 msg[1].len = 1; /* block length will be added by
1197                                    the underlying bus driver */
1198                 break;
1199         case I2C_SMBUS_I2C_BLOCK_DATA:
1200                 if (read_write == I2C_SMBUS_READ) {
1201                         msg[1].len = I2C_SMBUS_BLOCK_MAX;
1202                 } else {
1203                         msg[0].len = data->block[0] + 1;
1204                         if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
1205                                 dev_err(&adapter->dev, "i2c_smbus_xfer_emulated called with "
1206                                        "invalid block write size (%d)\n",
1207                                        data->block[0]);
1208                                 return -1;
1209                         }
1210                         for (i = 1; i <= data->block[0]; i++)
1211                                 msgbuf0[i] = data->block[i];
1212                 }
1213                 break;
1214         default:
1215                 dev_err(&adapter->dev, "smbus_access called with invalid size (%d)\n",
1216                        size);
1217                 return -1;
1218         }
1219
1220         i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1221                                       && size != I2C_SMBUS_I2C_BLOCK_DATA);
1222         if (i) {
1223                 /* Compute PEC if first message is a write */
1224                 if (!(msg[0].flags & I2C_M_RD)) {
1225                         if (num == 1) /* Write only */
1226                                 i2c_smbus_add_pec(&msg[0]);
1227                         else /* Write followed by read */
1228                                 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1229                 }
1230                 /* Ask for PEC if last message is a read */
1231                 if (msg[num-1].flags & I2C_M_RD)
1232                         msg[num-1].len++;
1233         }
1234
1235         if (i2c_transfer(adapter, msg, num) < 0)
1236                 return -1;
1237
1238         /* Check PEC if last message is a read */
1239         if (i && (msg[num-1].flags & I2C_M_RD)) {
1240                 if (i2c_smbus_check_pec(partial_pec, &msg[num-1]) < 0)
1241                         return -1;
1242         }
1243
1244         if (read_write == I2C_SMBUS_READ)
1245                 switch(size) {
1246                         case I2C_SMBUS_BYTE:
1247                                 data->byte = msgbuf0[0];
1248                                 break;
1249                         case I2C_SMBUS_BYTE_DATA:
1250                                 data->byte = msgbuf1[0];
1251                                 break;
1252                         case I2C_SMBUS_WORD_DATA:
1253                         case I2C_SMBUS_PROC_CALL:
1254                                 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1255                                 break;
1256                         case I2C_SMBUS_I2C_BLOCK_DATA:
1257                                 /* fixed at 32 for now */
1258                                 data->block[0] = I2C_SMBUS_BLOCK_MAX;
1259                                 for (i = 0; i < I2C_SMBUS_BLOCK_MAX; i++)
1260                                         data->block[i+1] = msgbuf1[i];
1261                                 break;
1262                         case I2C_SMBUS_BLOCK_DATA:
1263                         case I2C_SMBUS_BLOCK_PROC_CALL:
1264                                 for (i = 0; i < msgbuf1[0] + 1; i++)
1265                                         data->block[i] = msgbuf1[i];
1266                                 break;
1267                 }
1268         return 0;
1269 }
1270
1271
1272 s32 i2c_smbus_xfer(struct i2c_adapter * adapter, u16 addr, unsigned short flags,
1273                    char read_write, u8 command, int size,
1274                    union i2c_smbus_data * data)
1275 {
1276         s32 res;
1277
1278         flags &= I2C_M_TEN | I2C_CLIENT_PEC;
1279
1280         if (adapter->algo->smbus_xfer) {
1281                 mutex_lock(&adapter->bus_lock);
1282                 res = adapter->algo->smbus_xfer(adapter,addr,flags,read_write,
1283                                                 command,size,data);
1284                 mutex_unlock(&adapter->bus_lock);
1285         } else
1286                 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
1287                                               command,size,data);
1288
1289         return res;
1290 }
1291
1292
1293 /* Next three are needed by i2c-isa */
1294 EXPORT_SYMBOL_GPL(i2c_adapter_dev_release);
1295 EXPORT_SYMBOL_GPL(i2c_adapter_class);
1296 EXPORT_SYMBOL_GPL(i2c_bus_type);
1297
1298 EXPORT_SYMBOL(i2c_add_adapter);
1299 EXPORT_SYMBOL(i2c_del_adapter);
1300 EXPORT_SYMBOL(i2c_del_driver);
1301 EXPORT_SYMBOL(i2c_attach_client);
1302 EXPORT_SYMBOL(i2c_detach_client);
1303 EXPORT_SYMBOL(i2c_use_client);
1304 EXPORT_SYMBOL(i2c_release_client);
1305 EXPORT_SYMBOL(i2c_clients_command);
1306 EXPORT_SYMBOL(i2c_check_addr);
1307
1308 EXPORT_SYMBOL(i2c_master_send);
1309 EXPORT_SYMBOL(i2c_master_recv);
1310 EXPORT_SYMBOL(i2c_control);
1311 EXPORT_SYMBOL(i2c_transfer);
1312 EXPORT_SYMBOL(i2c_get_adapter);
1313 EXPORT_SYMBOL(i2c_put_adapter);
1314 EXPORT_SYMBOL(i2c_probe);
1315
1316 EXPORT_SYMBOL(i2c_smbus_xfer);
1317 EXPORT_SYMBOL(i2c_smbus_write_quick);
1318 EXPORT_SYMBOL(i2c_smbus_read_byte);
1319 EXPORT_SYMBOL(i2c_smbus_write_byte);
1320 EXPORT_SYMBOL(i2c_smbus_read_byte_data);
1321 EXPORT_SYMBOL(i2c_smbus_write_byte_data);
1322 EXPORT_SYMBOL(i2c_smbus_read_word_data);
1323 EXPORT_SYMBOL(i2c_smbus_write_word_data);
1324 EXPORT_SYMBOL(i2c_smbus_write_block_data);
1325 EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
1326 EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
1327
1328 MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1329 MODULE_DESCRIPTION("I2C-Bus main module");
1330 MODULE_LICENSE("GPL");