xref: /openbmc/linux/drivers/pnp/card.c (revision d5cb9783536a41df9f9cba5b0a1d78047ed787f7)
1 /*
2  * card.c - contains functions for managing groups of PnP devices
3  *
4  * Copyright 2002 Adam Belay <ambx1@neo.rr.com>
5  *
6  */
7 
8 #include <linux/config.h>
9 #include <linux/module.h>
10 #include <linux/slab.h>
11 #include <linux/pnp.h>
12 #include "base.h"
13 
14 LIST_HEAD(pnp_cards);
15 LIST_HEAD(pnp_card_drivers);
16 
17 
18 static const struct pnp_card_device_id * match_card(struct pnp_card_driver * drv, struct pnp_card * card)
19 {
20 	const struct pnp_card_device_id * drv_id = drv->id_table;
21 	while (*drv_id->id){
22 		if (compare_pnp_id(card->id,drv_id->id)) {
23 			int i = 0;
24 			for (;;) {
25 				int found;
26 				struct pnp_dev *dev;
27 				if (i == PNP_MAX_DEVICES || ! *drv_id->devs[i].id)
28 					return drv_id;
29 				found = 0;
30 				card_for_each_dev(card, dev) {
31 					if (compare_pnp_id(dev->id, drv_id->devs[i].id)) {
32 						found = 1;
33 						break;
34 					}
35 				}
36 				if (! found)
37 					break;
38 				i++;
39 			}
40 		}
41 		drv_id++;
42 	}
43 	return NULL;
44 }
45 
46 static void card_remove(struct pnp_dev * dev)
47 {
48 	dev->card_link = NULL;
49 }
50 
51 static void card_remove_first(struct pnp_dev * dev)
52 {
53 	struct pnp_card_driver * drv = to_pnp_card_driver(dev->driver);
54 	if (!dev->card || !drv)
55 		return;
56 	if (drv->remove)
57 		drv->remove(dev->card_link);
58 	drv->link.remove = &card_remove;
59 	kfree(dev->card_link);
60 	card_remove(dev);
61 }
62 
63 static int card_probe(struct pnp_card * card, struct pnp_card_driver * drv)
64 {
65 	const struct pnp_card_device_id *id = match_card(drv,card);
66 	if (id) {
67 		struct pnp_card_link * clink = pnp_alloc(sizeof(struct pnp_card_link));
68 		if (!clink)
69 			return 0;
70 		clink->card = card;
71 		clink->driver = drv;
72 		if (drv->probe) {
73 			if (drv->probe(clink, id)>=0)
74 				return 1;
75 			else {
76 				struct pnp_dev * dev;
77 				card_for_each_dev(card, dev) {
78 					if (dev->card_link == clink)
79 						pnp_release_card_device(dev);
80 				}
81 				kfree(clink);
82 			}
83 		} else
84 			return 1;
85 	}
86 	return 0;
87 }
88 
89 /**
90  * pnp_add_card_id - adds an EISA id to the specified card
91  * @id: pointer to a pnp_id structure
92  * @card: pointer to the desired card
93  *
94  */
95 
96 int pnp_add_card_id(struct pnp_id *id, struct pnp_card * card)
97 {
98 	struct pnp_id * ptr;
99 	if (!id)
100 		return -EINVAL;
101 	if (!card)
102 		return -EINVAL;
103 	id->next = NULL;
104 	ptr = card->id;
105 	while (ptr && ptr->next)
106 		ptr = ptr->next;
107 	if (ptr)
108 		ptr->next = id;
109 	else
110 		card->id = id;
111 	return 0;
112 }
113 
114 static void pnp_free_card_ids(struct pnp_card * card)
115 {
116 	struct pnp_id * id;
117 	struct pnp_id *next;
118 	if (!card)
119 		return;
120 	id = card->id;
121 	while (id) {
122 		next = id->next;
123 		kfree(id);
124 		id = next;
125 	}
126 }
127 
128 static void pnp_release_card(struct device *dmdev)
129 {
130 	struct pnp_card * card = to_pnp_card(dmdev);
131 	pnp_free_card_ids(card);
132 	kfree(card);
133 }
134 
135 
136 static ssize_t pnp_show_card_name(struct device *dmdev, struct device_attribute *attr, char *buf)
137 {
138 	char *str = buf;
139 	struct pnp_card *card = to_pnp_card(dmdev);
140 	str += sprintf(str,"%s\n", card->name);
141 	return (str - buf);
142 }
143 
144 static DEVICE_ATTR(name,S_IRUGO,pnp_show_card_name,NULL);
145 
146 static ssize_t pnp_show_card_ids(struct device *dmdev, struct device_attribute *attr, char *buf)
147 {
148 	char *str = buf;
149 	struct pnp_card *card = to_pnp_card(dmdev);
150 	struct pnp_id * pos = card->id;
151 
152 	while (pos) {
153 		str += sprintf(str,"%s\n", pos->id);
154 		pos = pos->next;
155 	}
156 	return (str - buf);
157 }
158 
159 static DEVICE_ATTR(card_id,S_IRUGO,pnp_show_card_ids,NULL);
160 
161 static int pnp_interface_attach_card(struct pnp_card *card)
162 {
163 	device_create_file(&card->dev,&dev_attr_name);
164 	device_create_file(&card->dev,&dev_attr_card_id);
165 	return 0;
166 }
167 
168 /**
169  * pnp_add_card - adds a PnP card to the PnP Layer
170  * @card: pointer to the card to add
171  */
172 
173 int pnp_add_card(struct pnp_card * card)
174 {
175 	int error;
176 	struct list_head * pos, * temp;
177 	if (!card || !card->protocol)
178 		return -EINVAL;
179 
180 	sprintf(card->dev.bus_id, "%02x:%02x", card->protocol->number, card->number);
181 	card->dev.parent = &card->protocol->dev;
182 	card->dev.bus = NULL;
183 	card->dev.release = &pnp_release_card;
184 	error = device_register(&card->dev);
185 
186 	if (error == 0) {
187 		pnp_interface_attach_card(card);
188 		spin_lock(&pnp_lock);
189 		list_add_tail(&card->global_list, &pnp_cards);
190 		list_add_tail(&card->protocol_list, &card->protocol->cards);
191 		spin_unlock(&pnp_lock);
192 
193 		/* we wait until now to add devices in order to ensure the drivers
194 		 * will be able to use all of the related devices on the card
195 		 * without waiting any unresonable length of time */
196 		list_for_each(pos,&card->devices){
197 			struct pnp_dev *dev = card_to_pnp_dev(pos);
198 			__pnp_add_device(dev);
199 		}
200 
201 		/* match with card drivers */
202 		list_for_each_safe(pos,temp,&pnp_card_drivers){
203 			struct pnp_card_driver * drv = list_entry(pos, struct pnp_card_driver, global_list);
204 			card_probe(card,drv);
205 		}
206 	} else
207 		pnp_err("sysfs failure, card '%s' will be unavailable", card->dev.bus_id);
208 	return error;
209 }
210 
211 /**
212  * pnp_remove_card - removes a PnP card from the PnP Layer
213  * @card: pointer to the card to remove
214  */
215 
216 void pnp_remove_card(struct pnp_card * card)
217 {
218 	struct list_head *pos, *temp;
219 	if (!card)
220 		return;
221 	device_unregister(&card->dev);
222 	spin_lock(&pnp_lock);
223 	list_del(&card->global_list);
224 	list_del(&card->protocol_list);
225 	spin_unlock(&pnp_lock);
226 	list_for_each_safe(pos,temp,&card->devices){
227 		struct pnp_dev *dev = card_to_pnp_dev(pos);
228 		pnp_remove_card_device(dev);
229 	}
230 }
231 
232 /**
233  * pnp_add_card_device - adds a device to the specified card
234  * @card: pointer to the card to add to
235  * @dev: pointer to the device to add
236  */
237 
238 int pnp_add_card_device(struct pnp_card * card, struct pnp_dev * dev)
239 {
240 	if (!card || !dev || !dev->protocol)
241 		return -EINVAL;
242 	dev->dev.parent = &card->dev;
243 	dev->card_link = NULL;
244 	snprintf(dev->dev.bus_id, BUS_ID_SIZE, "%02x:%02x.%02x", dev->protocol->number,
245 		 card->number,dev->number);
246 	spin_lock(&pnp_lock);
247 	dev->card = card;
248 	list_add_tail(&dev->card_list, &card->devices);
249 	spin_unlock(&pnp_lock);
250 	return 0;
251 }
252 
253 /**
254  * pnp_remove_card_device- removes a device from the specified card
255  * @dev: pointer to the device to remove
256  */
257 
258 void pnp_remove_card_device(struct pnp_dev * dev)
259 {
260 	spin_lock(&pnp_lock);
261 	dev->card = NULL;
262 	list_del(&dev->card_list);
263 	spin_unlock(&pnp_lock);
264 	__pnp_remove_device(dev);
265 }
266 
267 /**
268  * pnp_request_card_device - Searches for a PnP device under the specified card
269  * @clink: pointer to the card link, cannot be NULL
270  * @id: pointer to a PnP ID structure that explains the rules for finding the device
271  * @from: Starting place to search from. If NULL it will start from the begining.
272  */
273 
274 struct pnp_dev * pnp_request_card_device(struct pnp_card_link *clink, const char * id, struct pnp_dev * from)
275 {
276 	struct list_head * pos;
277 	struct pnp_dev * dev;
278 	struct pnp_card_driver * drv;
279 	struct pnp_card * card;
280 	if (!clink || !id)
281 		goto done;
282 	card = clink->card;
283 	drv = clink->driver;
284 	if (!from) {
285 		pos = card->devices.next;
286 	} else {
287 		if (from->card != card)
288 			goto done;
289 		pos = from->card_list.next;
290 	}
291 	while (pos != &card->devices) {
292 		dev = card_to_pnp_dev(pos);
293 		if ((!dev->card_link) && compare_pnp_id(dev->id,id))
294 			goto found;
295 		pos = pos->next;
296 	}
297 
298 done:
299 	return NULL;
300 
301 found:
302 	down_write(&dev->dev.bus->subsys.rwsem);
303 	dev->card_link = clink;
304 	dev->dev.driver = &drv->link.driver;
305 	if (drv->link.driver.probe) {
306 		if (drv->link.driver.probe(&dev->dev)) {
307 			dev->dev.driver = NULL;
308 			dev->card_link = NULL;
309 			up_write(&dev->dev.bus->subsys.rwsem);
310 			return NULL;
311 		}
312 	}
313 	device_bind_driver(&dev->dev);
314 	up_write(&dev->dev.bus->subsys.rwsem);
315 
316 	return dev;
317 }
318 
319 /**
320  * pnp_release_card_device - call this when the driver no longer needs the device
321  * @dev: pointer to the PnP device stucture
322  */
323 
324 void pnp_release_card_device(struct pnp_dev * dev)
325 {
326 	struct pnp_card_driver * drv = dev->card_link->driver;
327 	if (!drv)
328 		return;
329 	down_write(&dev->dev.bus->subsys.rwsem);
330 	drv->link.remove = &card_remove;
331 	device_release_driver(&dev->dev);
332 	drv->link.remove = &card_remove_first;
333 	up_write(&dev->dev.bus->subsys.rwsem);
334 }
335 
336 /**
337  * pnp_register_card_driver - registers a PnP card driver with the PnP Layer
338  * @drv: pointer to the driver to register
339  */
340 
341 int pnp_register_card_driver(struct pnp_card_driver * drv)
342 {
343 	int count = 0;
344 	struct list_head *pos, *temp;
345 
346 	drv->link.name = drv->name;
347 	drv->link.id_table = NULL;	/* this will disable auto matching */
348 	drv->link.flags = drv->flags;
349 	drv->link.probe = NULL;
350 	drv->link.remove = &card_remove_first;
351 
352 	spin_lock(&pnp_lock);
353 	list_add_tail(&drv->global_list, &pnp_card_drivers);
354 	spin_unlock(&pnp_lock);
355 	pnp_register_driver(&drv->link);
356 
357 	list_for_each_safe(pos,temp,&pnp_cards){
358 		struct pnp_card *card = list_entry(pos, struct pnp_card, global_list);
359 		count += card_probe(card,drv);
360 	}
361 	return count;
362 }
363 
364 /**
365  * pnp_unregister_card_driver - unregisters a PnP card driver from the PnP Layer
366  * @drv: pointer to the driver to unregister
367  */
368 
369 void pnp_unregister_card_driver(struct pnp_card_driver * drv)
370 {
371 	spin_lock(&pnp_lock);
372 	list_del(&drv->global_list);
373 	spin_unlock(&pnp_lock);
374 	pnp_unregister_driver(&drv->link);
375 }
376 
377 EXPORT_SYMBOL(pnp_add_card);
378 EXPORT_SYMBOL(pnp_remove_card);
379 EXPORT_SYMBOL(pnp_add_card_device);
380 EXPORT_SYMBOL(pnp_remove_card_device);
381 EXPORT_SYMBOL(pnp_add_card_id);
382 EXPORT_SYMBOL(pnp_request_card_device);
383 EXPORT_SYMBOL(pnp_release_card_device);
384 EXPORT_SYMBOL(pnp_register_card_driver);
385 EXPORT_SYMBOL(pnp_unregister_card_driver);
386