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