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