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