Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
1da177e4 LT |
2 | /* |
3 | * card.c - contains functions for managing groups of PnP devices | |
4 | * | |
5 | * Copyright 2002 Adam Belay <ambx1@neo.rr.com> | |
1da177e4 LT |
6 | */ |
7 | ||
1da177e4 | 8 | #include <linux/module.h> |
38f6b38d | 9 | #include <linux/mutex.h> |
e436675f | 10 | #include <linux/ctype.h> |
1da177e4 | 11 | #include <linux/slab.h> |
1da177e4 | 12 | #include <linux/pnp.h> |
e86b19ce | 13 | #include <linux/dma-mapping.h> |
1da177e4 LT |
14 | #include "base.h" |
15 | ||
16 | LIST_HEAD(pnp_cards); | |
b449f63c | 17 | static LIST_HEAD(pnp_card_drivers); |
1da177e4 | 18 | |
9dd78466 BH |
19 | static const struct pnp_card_device_id *match_card(struct pnp_card_driver *drv, |
20 | struct pnp_card *card) | |
1da177e4 | 21 | { |
9dd78466 | 22 | const struct pnp_card_device_id *drv_id = drv->id_table; |
07d4e9af | 23 | |
9dd78466 BH |
24 | while (*drv_id->id) { |
25 | if (compare_pnp_id(card->id, drv_id->id)) { | |
1da177e4 | 26 | int i = 0; |
07d4e9af | 27 | |
1da177e4 LT |
28 | for (;;) { |
29 | int found; | |
30 | struct pnp_dev *dev; | |
07d4e9af | 31 | |
1e0aa9ad BH |
32 | if (i == PNP_MAX_DEVICES || |
33 | !*drv_id->devs[i].id) | |
1da177e4 LT |
34 | return drv_id; |
35 | found = 0; | |
36 | card_for_each_dev(card, dev) { | |
1e0aa9ad BH |
37 | if (compare_pnp_id(dev->id, |
38 | drv_id->devs[i].id)) { | |
1da177e4 LT |
39 | found = 1; |
40 | break; | |
41 | } | |
42 | } | |
9dd78466 | 43 | if (!found) |
1da177e4 LT |
44 | break; |
45 | i++; | |
46 | } | |
47 | } | |
48 | drv_id++; | |
49 | } | |
50 | return NULL; | |
51 | } | |
52 | ||
9dd78466 | 53 | static void card_remove(struct pnp_dev *dev) |
1da177e4 LT |
54 | { |
55 | dev->card_link = NULL; | |
56 | } | |
982c6094 | 57 | |
9dd78466 | 58 | static void card_remove_first(struct pnp_dev *dev) |
1da177e4 | 59 | { |
9dd78466 | 60 | struct pnp_card_driver *drv = to_pnp_card_driver(dev->driver); |
07d4e9af | 61 | |
1da177e4 LT |
62 | if (!dev->card || !drv) |
63 | return; | |
64 | if (drv->remove) | |
65 | drv->remove(dev->card_link); | |
66 | drv->link.remove = &card_remove; | |
67 | kfree(dev->card_link); | |
68 | card_remove(dev); | |
69 | } | |
70 | ||
a9adb8db | 71 | static int card_probe(struct pnp_card *card, struct pnp_card_driver *drv) |
1da177e4 | 72 | { |
a9adb8db JJ |
73 | const struct pnp_card_device_id *id; |
74 | struct pnp_card_link *clink; | |
75 | struct pnp_dev *dev; | |
76 | ||
77 | if (!drv->probe) | |
78 | return 0; | |
9dd78466 | 79 | id = match_card(drv, card); |
a9adb8db JJ |
80 | if (!id) |
81 | return 0; | |
82 | ||
83 | clink = pnp_alloc(sizeof(*clink)); | |
84 | if (!clink) | |
85 | return 0; | |
86 | clink->card = card; | |
87 | clink->driver = drv; | |
88 | clink->pm_state = PMSG_ON; | |
89 | ||
90 | if (drv->probe(clink, id) >= 0) | |
91 | return 1; | |
92 | ||
93 | /* Recovery */ | |
94 | card_for_each_dev(card, dev) { | |
95 | if (dev->card_link == clink) | |
96 | pnp_release_card_device(dev); | |
1da177e4 | 97 | } |
a9adb8db | 98 | kfree(clink); |
1da177e4 LT |
99 | return 0; |
100 | } | |
101 | ||
102 | /** | |
103 | * pnp_add_card_id - adds an EISA id to the specified card | |
104 | * @id: pointer to a pnp_id structure | |
105 | * @card: pointer to the desired card | |
1da177e4 | 106 | */ |
25cdcd00 | 107 | static struct pnp_id *pnp_add_card_id(struct pnp_card *card, char *id) |
1da177e4 | 108 | { |
e436675f BH |
109 | struct pnp_id *dev_id, *ptr; |
110 | ||
111 | dev_id = kzalloc(sizeof(struct pnp_id), GFP_KERNEL); | |
112 | if (!dev_id) | |
113 | return NULL; | |
114 | ||
115 | dev_id->id[0] = id[0]; | |
116 | dev_id->id[1] = id[1]; | |
117 | dev_id->id[2] = id[2]; | |
118 | dev_id->id[3] = tolower(id[3]); | |
119 | dev_id->id[4] = tolower(id[4]); | |
120 | dev_id->id[5] = tolower(id[5]); | |
121 | dev_id->id[6] = tolower(id[6]); | |
122 | dev_id->id[7] = '\0'; | |
123 | ||
124 | dev_id->next = NULL; | |
1da177e4 LT |
125 | ptr = card->id; |
126 | while (ptr && ptr->next) | |
127 | ptr = ptr->next; | |
128 | if (ptr) | |
e436675f | 129 | ptr->next = dev_id; |
1da177e4 | 130 | else |
e436675f BH |
131 | card->id = dev_id; |
132 | ||
133 | return dev_id; | |
1da177e4 LT |
134 | } |
135 | ||
9dd78466 | 136 | static void pnp_free_card_ids(struct pnp_card *card) |
1da177e4 | 137 | { |
9dd78466 | 138 | struct pnp_id *id; |
1da177e4 | 139 | struct pnp_id *next; |
07d4e9af | 140 | |
1da177e4 LT |
141 | id = card->id; |
142 | while (id) { | |
143 | next = id->next; | |
144 | kfree(id); | |
145 | id = next; | |
146 | } | |
147 | } | |
148 | ||
149 | static void pnp_release_card(struct device *dmdev) | |
150 | { | |
9dd78466 | 151 | struct pnp_card *card = to_pnp_card(dmdev); |
07d4e9af | 152 | |
1da177e4 LT |
153 | pnp_free_card_ids(card); |
154 | kfree(card); | |
155 | } | |
156 | ||
6bf2aab2 BH |
157 | struct pnp_card *pnp_alloc_card(struct pnp_protocol *protocol, int id, char *pnpid) |
158 | { | |
159 | struct pnp_card *card; | |
160 | struct pnp_id *dev_id; | |
161 | ||
162 | card = kzalloc(sizeof(struct pnp_card), GFP_KERNEL); | |
163 | if (!card) | |
164 | return NULL; | |
165 | ||
166 | card->protocol = protocol; | |
167 | card->number = id; | |
168 | ||
169 | card->dev.parent = &card->protocol->dev; | |
c85e37c5 | 170 | dev_set_name(&card->dev, "%02x:%02x", card->protocol->number, card->number); |
6bf2aab2 | 171 | |
2f4f27d4 | 172 | card->dev.coherent_dma_mask = DMA_BIT_MASK(24); |
e86b19ce RH |
173 | card->dev.dma_mask = &card->dev.coherent_dma_mask; |
174 | ||
6bf2aab2 BH |
175 | dev_id = pnp_add_card_id(card, pnpid); |
176 | if (!dev_id) { | |
177 | kfree(card); | |
178 | return NULL; | |
179 | } | |
180 | ||
181 | return card; | |
182 | } | |
183 | ||
9dd78466 BH |
184 | static ssize_t pnp_show_card_name(struct device *dmdev, |
185 | struct device_attribute *attr, char *buf) | |
1da177e4 LT |
186 | { |
187 | char *str = buf; | |
188 | struct pnp_card *card = to_pnp_card(dmdev); | |
07d4e9af | 189 | |
9dd78466 | 190 | str += sprintf(str, "%s\n", card->name); |
1da177e4 LT |
191 | return (str - buf); |
192 | } | |
193 | ||
9dd78466 | 194 | static DEVICE_ATTR(name, S_IRUGO, pnp_show_card_name, NULL); |
1da177e4 | 195 | |
9dd78466 BH |
196 | static ssize_t pnp_show_card_ids(struct device *dmdev, |
197 | struct device_attribute *attr, char *buf) | |
1da177e4 LT |
198 | { |
199 | char *str = buf; | |
200 | struct pnp_card *card = to_pnp_card(dmdev); | |
9dd78466 | 201 | struct pnp_id *pos = card->id; |
1da177e4 LT |
202 | |
203 | while (pos) { | |
9dd78466 | 204 | str += sprintf(str, "%s\n", pos->id); |
1da177e4 LT |
205 | pos = pos->next; |
206 | } | |
207 | return (str - buf); | |
208 | } | |
209 | ||
9dd78466 | 210 | static DEVICE_ATTR(card_id, S_IRUGO, pnp_show_card_ids, NULL); |
1da177e4 LT |
211 | |
212 | static int pnp_interface_attach_card(struct pnp_card *card) | |
213 | { | |
9dd78466 | 214 | int rc = device_create_file(&card->dev, &dev_attr_name); |
07d4e9af | 215 | |
9dd78466 BH |
216 | if (rc) |
217 | return rc; | |
bfc7ee20 | 218 | |
9dd78466 BH |
219 | rc = device_create_file(&card->dev, &dev_attr_card_id); |
220 | if (rc) | |
221 | goto err_name; | |
bfc7ee20 | 222 | |
1da177e4 | 223 | return 0; |
bfc7ee20 | 224 | |
1e0aa9ad | 225 | err_name: |
9dd78466 | 226 | device_remove_file(&card->dev, &dev_attr_name); |
bfc7ee20 | 227 | return rc; |
1da177e4 LT |
228 | } |
229 | ||
230 | /** | |
231 | * pnp_add_card - adds a PnP card to the PnP Layer | |
232 | * @card: pointer to the card to add | |
233 | */ | |
9dd78466 | 234 | int pnp_add_card(struct pnp_card *card) |
1da177e4 LT |
235 | { |
236 | int error; | |
9dd78466 | 237 | struct list_head *pos, *temp; |
07d4e9af | 238 | |
1da177e4 LT |
239 | card->dev.bus = NULL; |
240 | card->dev.release = &pnp_release_card; | |
241 | error = device_register(&card->dev); | |
5bfc43a0 | 242 | if (error) { |
a05d0781 | 243 | dev_err(&card->dev, "could not register (err=%d)\n", error); |
75365d04 | 244 | put_device(&card->dev); |
5bfc43a0 BH |
245 | return error; |
246 | } | |
247 | ||
248 | pnp_interface_attach_card(card); | |
38f6b38d | 249 | mutex_lock(&pnp_lock); |
5bfc43a0 BH |
250 | list_add_tail(&card->global_list, &pnp_cards); |
251 | list_add_tail(&card->protocol_list, &card->protocol->cards); | |
38f6b38d | 252 | mutex_unlock(&pnp_lock); |
5bfc43a0 BH |
253 | |
254 | /* we wait until now to add devices in order to ensure the drivers | |
255 | * will be able to use all of the related devices on the card | |
256 | * without waiting an unreasonable length of time */ | |
257 | list_for_each(pos, &card->devices) { | |
258 | struct pnp_dev *dev = card_to_pnp_dev(pos); | |
259 | __pnp_add_device(dev); | |
260 | } | |
261 | ||
262 | /* match with card drivers */ | |
263 | list_for_each_safe(pos, temp, &pnp_card_drivers) { | |
264 | struct pnp_card_driver *drv = | |
265 | list_entry(pos, struct pnp_card_driver, | |
266 | global_list); | |
267 | card_probe(card, drv); | |
268 | } | |
269 | return 0; | |
1da177e4 LT |
270 | } |
271 | ||
272 | /** | |
273 | * pnp_remove_card - removes a PnP card from the PnP Layer | |
274 | * @card: pointer to the card to remove | |
275 | */ | |
9dd78466 | 276 | void pnp_remove_card(struct pnp_card *card) |
1da177e4 LT |
277 | { |
278 | struct list_head *pos, *temp; | |
07d4e9af | 279 | |
1da177e4 | 280 | device_unregister(&card->dev); |
38f6b38d | 281 | mutex_lock(&pnp_lock); |
1da177e4 LT |
282 | list_del(&card->global_list); |
283 | list_del(&card->protocol_list); | |
38f6b38d | 284 | mutex_unlock(&pnp_lock); |
9dd78466 | 285 | list_for_each_safe(pos, temp, &card->devices) { |
1da177e4 LT |
286 | struct pnp_dev *dev = card_to_pnp_dev(pos); |
287 | pnp_remove_card_device(dev); | |
288 | } | |
289 | } | |
290 | ||
291 | /** | |
292 | * pnp_add_card_device - adds a device to the specified card | |
293 | * @card: pointer to the card to add to | |
294 | * @dev: pointer to the device to add | |
295 | */ | |
9dd78466 | 296 | int pnp_add_card_device(struct pnp_card *card, struct pnp_dev *dev) |
1da177e4 | 297 | { |
1da177e4 LT |
298 | dev->dev.parent = &card->dev; |
299 | dev->card_link = NULL; | |
c85e37c5 KS |
300 | dev_set_name(&dev->dev, "%02x:%02x.%02x", |
301 | dev->protocol->number, card->number, dev->number); | |
38f6b38d | 302 | mutex_lock(&pnp_lock); |
1da177e4 LT |
303 | dev->card = card; |
304 | list_add_tail(&dev->card_list, &card->devices); | |
38f6b38d | 305 | mutex_unlock(&pnp_lock); |
1da177e4 LT |
306 | return 0; |
307 | } | |
308 | ||
309 | /** | |
310 | * pnp_remove_card_device- removes a device from the specified card | |
1da177e4 LT |
311 | * @dev: pointer to the device to remove |
312 | */ | |
9dd78466 | 313 | void pnp_remove_card_device(struct pnp_dev *dev) |
1da177e4 | 314 | { |
38f6b38d | 315 | mutex_lock(&pnp_lock); |
1da177e4 LT |
316 | dev->card = NULL; |
317 | list_del(&dev->card_list); | |
38f6b38d | 318 | mutex_unlock(&pnp_lock); |
1da177e4 LT |
319 | __pnp_remove_device(dev); |
320 | } | |
321 | ||
322 | /** | |
323 | * pnp_request_card_device - Searches for a PnP device under the specified card | |
3d41088f | 324 | * @clink: pointer to the card link, cannot be NULL |
1da177e4 | 325 | * @id: pointer to a PnP ID structure that explains the rules for finding the device |
25985edc | 326 | * @from: Starting place to search from. If NULL it will start from the beginning. |
1da177e4 | 327 | */ |
9dd78466 BH |
328 | struct pnp_dev *pnp_request_card_device(struct pnp_card_link *clink, |
329 | const char *id, struct pnp_dev *from) | |
1da177e4 | 330 | { |
9dd78466 BH |
331 | struct list_head *pos; |
332 | struct pnp_dev *dev; | |
333 | struct pnp_card_driver *drv; | |
334 | struct pnp_card *card; | |
07d4e9af | 335 | |
1da177e4 | 336 | if (!clink || !id) |
5bfc43a0 BH |
337 | return NULL; |
338 | ||
1da177e4 LT |
339 | card = clink->card; |
340 | drv = clink->driver; | |
341 | if (!from) { | |
342 | pos = card->devices.next; | |
343 | } else { | |
344 | if (from->card != card) | |
5bfc43a0 | 345 | return NULL; |
1da177e4 LT |
346 | pos = from->card_list.next; |
347 | } | |
348 | while (pos != &card->devices) { | |
349 | dev = card_to_pnp_dev(pos); | |
9dd78466 | 350 | if ((!dev->card_link) && compare_pnp_id(dev->id, id)) |
1da177e4 LT |
351 | goto found; |
352 | pos = pos->next; | |
353 | } | |
354 | ||
1da177e4 LT |
355 | return NULL; |
356 | ||
1e0aa9ad | 357 | found: |
1da177e4 LT |
358 | dev->card_link = clink; |
359 | dev->dev.driver = &drv->link.driver; | |
bfc7ee20 JG |
360 | if (pnp_bus_type.probe(&dev->dev)) |
361 | goto err_out; | |
362 | if (device_bind_driver(&dev->dev)) | |
363 | goto err_out; | |
364 | ||
1da177e4 | 365 | return dev; |
bfc7ee20 | 366 | |
1e0aa9ad | 367 | err_out: |
bfc7ee20 JG |
368 | dev->dev.driver = NULL; |
369 | dev->card_link = NULL; | |
bfc7ee20 | 370 | return NULL; |
1da177e4 LT |
371 | } |
372 | ||
373 | /** | |
374 | * pnp_release_card_device - call this when the driver no longer needs the device | |
25985edc | 375 | * @dev: pointer to the PnP device structure |
1da177e4 | 376 | */ |
9dd78466 | 377 | void pnp_release_card_device(struct pnp_dev *dev) |
1da177e4 | 378 | { |
9dd78466 | 379 | struct pnp_card_driver *drv = dev->card_link->driver; |
07d4e9af | 380 | |
1da177e4 LT |
381 | drv->link.remove = &card_remove; |
382 | device_release_driver(&dev->dev); | |
383 | drv->link.remove = &card_remove_first; | |
1da177e4 LT |
384 | } |
385 | ||
4c98cfef TI |
386 | /* |
387 | * suspend/resume callbacks | |
388 | */ | |
389 | static int card_suspend(struct pnp_dev *dev, pm_message_t state) | |
390 | { | |
391 | struct pnp_card_link *link = dev->card_link; | |
07d4e9af | 392 | |
4c98cfef TI |
393 | if (link->pm_state.event == state.event) |
394 | return 0; | |
395 | link->pm_state = state; | |
396 | return link->driver->suspend(link, state); | |
397 | } | |
398 | ||
399 | static int card_resume(struct pnp_dev *dev) | |
400 | { | |
401 | struct pnp_card_link *link = dev->card_link; | |
07d4e9af | 402 | |
4c98cfef TI |
403 | if (link->pm_state.event == PM_EVENT_ON) |
404 | return 0; | |
405 | link->pm_state = PMSG_ON; | |
406 | link->driver->resume(link); | |
407 | return 0; | |
408 | } | |
409 | ||
1da177e4 LT |
410 | /** |
411 | * pnp_register_card_driver - registers a PnP card driver with the PnP Layer | |
412 | * @drv: pointer to the driver to register | |
413 | */ | |
9dd78466 | 414 | int pnp_register_card_driver(struct pnp_card_driver *drv) |
1da177e4 | 415 | { |
982c6094 | 416 | int error; |
1da177e4 LT |
417 | struct list_head *pos, *temp; |
418 | ||
419 | drv->link.name = drv->name; | |
420 | drv->link.id_table = NULL; /* this will disable auto matching */ | |
421 | drv->link.flags = drv->flags; | |
422 | drv->link.probe = NULL; | |
423 | drv->link.remove = &card_remove_first; | |
4c98cfef TI |
424 | drv->link.suspend = drv->suspend ? card_suspend : NULL; |
425 | drv->link.resume = drv->resume ? card_resume : NULL; | |
1da177e4 | 426 | |
982c6094 BH |
427 | error = pnp_register_driver(&drv->link); |
428 | if (error < 0) | |
429 | return error; | |
d1d051b2 | 430 | |
38f6b38d | 431 | mutex_lock(&pnp_lock); |
1da177e4 | 432 | list_add_tail(&drv->global_list, &pnp_card_drivers); |
38f6b38d | 433 | mutex_unlock(&pnp_lock); |
d1d051b2 | 434 | |
9dd78466 BH |
435 | list_for_each_safe(pos, temp, &pnp_cards) { |
436 | struct pnp_card *card = | |
437 | list_entry(pos, struct pnp_card, global_list); | |
438 | card_probe(card, drv); | |
1da177e4 | 439 | } |
982c6094 | 440 | return 0; |
1da177e4 LT |
441 | } |
442 | ||
443 | /** | |
444 | * pnp_unregister_card_driver - unregisters a PnP card driver from the PnP Layer | |
445 | * @drv: pointer to the driver to unregister | |
446 | */ | |
9dd78466 | 447 | void pnp_unregister_card_driver(struct pnp_card_driver *drv) |
1da177e4 | 448 | { |
38f6b38d | 449 | mutex_lock(&pnp_lock); |
1da177e4 | 450 | list_del(&drv->global_list); |
38f6b38d | 451 | mutex_unlock(&pnp_lock); |
1da177e4 LT |
452 | pnp_unregister_driver(&drv->link); |
453 | } | |
454 | ||
1da177e4 LT |
455 | EXPORT_SYMBOL(pnp_request_card_device); |
456 | EXPORT_SYMBOL(pnp_release_card_device); | |
457 | EXPORT_SYMBOL(pnp_register_card_driver); | |
458 | EXPORT_SYMBOL(pnp_unregister_card_driver); |