2 * dvb_i2c.h: simplified i2c interface for DVB adapters to get rid of i2c-core.c
4 * Copyright (C) 2002 Holger Waechtler for convergence integrated media GmbH
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
22 #include <linux/errno.h>
23 #include <linux/slab.h>
24 #include <linux/list.h>
25 #include <linux/module.h>
26 #include <asm/semaphore.h>
29 #include "dvb_functions.h"
32 struct dvb_i2c_device {
33 struct list_head list_head;
35 int (*attach) (struct dvb_i2c_bus *i2c, void **data);
36 void (*detach) (struct dvb_i2c_bus *i2c, void *data);
40 LIST_HEAD(dvb_i2c_buslist);
41 LIST_HEAD(dvb_i2c_devicelist);
43 DECLARE_MUTEX(dvb_i2c_mutex);
45 static int register_i2c_client (struct dvb_i2c_bus *i2c, struct dvb_i2c_device *dev)
47 struct dvb_i2c_device *client;
49 if (!(client = kmalloc (sizeof (struct dvb_i2c_device), GFP_KERNEL)))
52 client->detach = dev->detach;
53 client->owner = dev->owner;
54 client->data = dev->data;
56 INIT_LIST_HEAD(&client->list_head);
58 list_add_tail (&client->list_head, &i2c->client_list);
64 static void try_attach_device (struct dvb_i2c_bus *i2c, struct dvb_i2c_device *dev)
67 if (!try_module_get(dev->owner))
71 if (dev->attach (i2c, &dev->data) == 0) {
72 register_i2c_client (i2c, dev);
75 module_put (dev->owner);
80 static void detach_device (struct dvb_i2c_bus *i2c, struct dvb_i2c_device *dev)
82 dev->detach (i2c, dev->data);
85 module_put (dev->owner);
89 static void unregister_i2c_client_from_bus (struct dvb_i2c_device *dev,
90 struct dvb_i2c_bus *i2c)
92 struct list_head *entry, *n;
94 list_for_each_safe (entry, n, &i2c->client_list) {
95 struct dvb_i2c_device *client;
97 client = list_entry (entry, struct dvb_i2c_device, list_head);
99 if (client->detach == dev->detach) {
101 detach_device (i2c, dev);
107 static void unregister_i2c_client_from_all_busses (struct dvb_i2c_device *dev)
109 struct list_head *entry, *n;
111 list_for_each_safe (entry, n, &dvb_i2c_buslist) {
112 struct dvb_i2c_bus *i2c;
114 i2c = list_entry (entry, struct dvb_i2c_bus, list_head);
116 unregister_i2c_client_from_bus (dev, i2c);
121 static void unregister_all_clients_from_bus (struct dvb_i2c_bus *i2c)
123 struct list_head *entry, *n;
125 list_for_each_safe (entry, n, &(i2c->client_list)) {
126 struct dvb_i2c_device *dev;
128 dev = list_entry (entry, struct dvb_i2c_device, list_head);
130 unregister_i2c_client_from_bus (dev, i2c);
135 static void probe_device_on_all_busses (struct dvb_i2c_device *dev)
137 struct list_head *entry;
139 list_for_each (entry, &dvb_i2c_buslist) {
140 struct dvb_i2c_bus *i2c;
142 i2c = list_entry (entry, struct dvb_i2c_bus, list_head);
144 try_attach_device (i2c, dev);
149 static void probe_devices_on_bus (struct dvb_i2c_bus *i2c)
151 struct list_head *entry;
153 list_for_each (entry, &dvb_i2c_devicelist) {
154 struct dvb_i2c_device *dev;
156 dev = list_entry (entry, struct dvb_i2c_device, list_head);
158 try_attach_device (i2c, dev);
163 static struct dvb_i2c_bus* dvb_find_i2c_bus (int (*xfer) (struct dvb_i2c_bus *i2c,
164 const struct i2c_msg msgs[],
166 struct dvb_adapter *adapter,
169 struct list_head *entry;
171 list_for_each (entry, &dvb_i2c_buslist) {
172 struct dvb_i2c_bus *i2c;
174 i2c = list_entry (entry, struct dvb_i2c_bus, list_head);
176 if (i2c->xfer == xfer && i2c->adapter == adapter && i2c->id == id)
185 dvb_register_i2c_bus (int (*xfer) (struct dvb_i2c_bus *i2c,
186 const struct i2c_msg *msgs, int num),
187 void *data, struct dvb_adapter *adapter, int id)
189 struct dvb_i2c_bus *i2c;
191 if (down_interruptible (&dvb_i2c_mutex))
194 if (!(i2c = kmalloc (sizeof (struct dvb_i2c_bus), GFP_KERNEL))) {
199 INIT_LIST_HEAD(&i2c->list_head);
200 INIT_LIST_HEAD(&i2c->client_list);
204 i2c->adapter = adapter;
207 probe_devices_on_bus (i2c);
209 list_add_tail (&i2c->list_head, &dvb_i2c_buslist);
217 void dvb_unregister_i2c_bus (int (*xfer) (struct dvb_i2c_bus *i2c,
218 const struct i2c_msg msgs[], int num),
219 struct dvb_adapter *adapter, int id)
221 struct dvb_i2c_bus *i2c;
223 down (&dvb_i2c_mutex);
225 if ((i2c = dvb_find_i2c_bus (xfer, adapter, id))) {
226 unregister_all_clients_from_bus (i2c);
227 list_del (&i2c->list_head);
235 int dvb_register_i2c_device (struct module *owner,
236 int (*attach) (struct dvb_i2c_bus *i2c, void **data),
237 void (*detach) (struct dvb_i2c_bus *i2c, void *data))
239 struct dvb_i2c_device *entry;
241 if (down_interruptible (&dvb_i2c_mutex))
244 if (!(entry = kmalloc (sizeof (struct dvb_i2c_device), GFP_KERNEL))) {
249 entry->owner = owner;
250 entry->attach = attach;
251 entry->detach = detach;
253 INIT_LIST_HEAD(&entry->list_head);
255 probe_device_on_all_busses (entry);
257 list_add_tail (&entry->list_head, &dvb_i2c_devicelist);
265 int dvb_unregister_i2c_device (int (*attach) (struct dvb_i2c_bus *i2c, void **data))
267 struct list_head *entry, *n;
269 down (&dvb_i2c_mutex);
271 list_for_each_safe (entry, n, &dvb_i2c_devicelist) {
272 struct dvb_i2c_device *dev;
274 dev = list_entry (entry, struct dvb_i2c_device, list_head);
276 if (dev->attach == attach) {
278 unregister_i2c_client_from_all_busses (dev);