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