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