]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * ds.c -- 16-bit PCMCIA core support | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License version 2 as | |
6 | * published by the Free Software Foundation. | |
7 | * | |
8 | * The initial developer of the original code is David A. Hinds | |
9 | * <dahinds@users.sourceforge.net>. Portions created by David A. Hinds | |
10 | * are Copyright (C) 1999 David A. Hinds. All Rights Reserved. | |
11 | * | |
12 | * (C) 1999 David A. Hinds | |
8661bb5b | 13 | * (C) 2003 - 2006 Dominik Brodowski |
1da177e4 LT |
14 | */ |
15 | ||
3b659fb8 | 16 | #include <linux/kernel.h> |
1da177e4 | 17 | #include <linux/module.h> |
1da177e4 | 18 | #include <linux/init.h> |
1da177e4 | 19 | #include <linux/errno.h> |
1da177e4 LT |
20 | #include <linux/list.h> |
21 | #include <linux/delay.h> | |
1da177e4 | 22 | #include <linux/workqueue.h> |
840c2ac5 | 23 | #include <linux/crc32.h> |
daa9517d | 24 | #include <linux/firmware.h> |
360b65b9 | 25 | #include <linux/kref.h> |
43d9f7fd | 26 | #include <linux/dma-mapping.h> |
1da177e4 | 27 | |
1da177e4 LT |
28 | #include <pcmcia/cs_types.h> |
29 | #include <pcmcia/cs.h> | |
1da177e4 LT |
30 | #include <pcmcia/cistpl.h> |
31 | #include <pcmcia/ds.h> | |
32 | #include <pcmcia/ss.h> | |
33 | ||
34 | #include "cs_internal.h" | |
35 | ||
36 | /*====================================================================*/ | |
37 | ||
38 | /* Module parameters */ | |
39 | ||
40 | MODULE_AUTHOR("David Hinds <dahinds@users.sourceforge.net>"); | |
41 | MODULE_DESCRIPTION("PCMCIA Driver Services"); | |
42 | MODULE_LICENSE("GPL"); | |
43 | ||
1da177e4 | 44 | |
e7a480d2 | 45 | spinlock_t pcmcia_dev_list_lock; |
1da177e4 | 46 | |
1da177e4 LT |
47 | /*====================================================================*/ |
48 | ||
23a83bfe DB |
49 | static void pcmcia_check_driver(struct pcmcia_driver *p_drv) |
50 | { | |
51 | struct pcmcia_device_id *did = p_drv->id_table; | |
52 | unsigned int i; | |
53 | u32 hash; | |
54 | ||
f8cfa618 | 55 | if (!p_drv->probe || !p_drv->remove) |
ba5bb6b5 PR |
56 | printk(KERN_DEBUG "pcmcia: %s lacks a requisite callback " |
57 | "function\n", p_drv->drv.name); | |
1e212f36 | 58 | |
23a83bfe DB |
59 | while (did && did->match_flags) { |
60 | for (i=0; i<4; i++) { | |
61 | if (!did->prod_id[i]) | |
62 | continue; | |
63 | ||
64 | hash = crc32(0, did->prod_id[i], strlen(did->prod_id[i])); | |
65 | if (hash == did->prod_id_hash[i]) | |
66 | continue; | |
67 | ||
68 | printk(KERN_DEBUG "pcmcia: %s: invalid hash for " | |
69 | "product string \"%s\": is 0x%x, should " | |
70 | "be 0x%x\n", p_drv->drv.name, did->prod_id[i], | |
71 | did->prod_id_hash[i], hash); | |
5085cb26 DB |
72 | printk(KERN_DEBUG "pcmcia: see " |
73 | "Documentation/pcmcia/devicetable.txt for " | |
74 | "details\n"); | |
23a83bfe DB |
75 | } |
76 | did++; | |
77 | } | |
78 | ||
79 | return; | |
80 | } | |
81 | ||
daa9517d | 82 | |
1da177e4 LT |
83 | /*======================================================================*/ |
84 | ||
1da177e4 | 85 | |
6179b556 BW |
86 | struct pcmcia_dynid { |
87 | struct list_head node; | |
88 | struct pcmcia_device_id id; | |
89 | }; | |
90 | ||
91 | /** | |
92 | * pcmcia_store_new_id - add a new PCMCIA device ID to this driver and re-probe devices | |
93 | * @driver: target device driver | |
94 | * @buf: buffer for scanning device ID data | |
95 | * @count: input size | |
96 | * | |
97 | * Adds a new dynamic PCMCIA device ID to this driver, | |
98 | * and causes the driver to probe for all devices again. | |
99 | */ | |
100 | static ssize_t | |
101 | pcmcia_store_new_id(struct device_driver *driver, const char *buf, size_t count) | |
102 | { | |
103 | struct pcmcia_dynid *dynid; | |
104 | struct pcmcia_driver *pdrv = to_pcmcia_drv(driver); | |
105 | __u16 match_flags, manf_id, card_id; | |
106 | __u8 func_id, function, device_no; | |
107 | __u32 prod_id_hash[4] = {0, 0, 0, 0}; | |
108 | int fields=0; | |
109 | int retval = 0; | |
110 | ||
111 | fields = sscanf(buf, "%hx %hx %hx %hhx %hhx %hhx %x %x %x %x", | |
112 | &match_flags, &manf_id, &card_id, &func_id, &function, &device_no, | |
113 | &prod_id_hash[0], &prod_id_hash[1], &prod_id_hash[2], &prod_id_hash[3]); | |
114 | if (fields < 6) | |
115 | return -EINVAL; | |
116 | ||
117 | dynid = kzalloc(sizeof(struct pcmcia_dynid), GFP_KERNEL); | |
118 | if (!dynid) | |
119 | return -ENOMEM; | |
120 | ||
6179b556 BW |
121 | dynid->id.match_flags = match_flags; |
122 | dynid->id.manf_id = manf_id; | |
123 | dynid->id.card_id = card_id; | |
124 | dynid->id.func_id = func_id; | |
125 | dynid->id.function = function; | |
126 | dynid->id.device_no = device_no; | |
127 | memcpy(dynid->id.prod_id_hash, prod_id_hash, sizeof(__u32) * 4); | |
128 | ||
129 | spin_lock(&pdrv->dynids.lock); | |
b4b3d7bb | 130 | list_add_tail(&dynid->node, &pdrv->dynids.list); |
6179b556 BW |
131 | spin_unlock(&pdrv->dynids.lock); |
132 | ||
133 | if (get_driver(&pdrv->drv)) { | |
134 | retval = driver_attach(&pdrv->drv); | |
135 | put_driver(&pdrv->drv); | |
136 | } | |
137 | ||
138 | if (retval) | |
139 | return retval; | |
140 | return count; | |
141 | } | |
142 | static DRIVER_ATTR(new_id, S_IWUSR, NULL, pcmcia_store_new_id); | |
143 | ||
144 | static void | |
145 | pcmcia_free_dynids(struct pcmcia_driver *drv) | |
146 | { | |
147 | struct pcmcia_dynid *dynid, *n; | |
148 | ||
149 | spin_lock(&drv->dynids.lock); | |
150 | list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) { | |
151 | list_del(&dynid->node); | |
152 | kfree(dynid); | |
153 | } | |
154 | spin_unlock(&drv->dynids.lock); | |
155 | } | |
156 | ||
157 | static int | |
158 | pcmcia_create_newid_file(struct pcmcia_driver *drv) | |
159 | { | |
160 | int error = 0; | |
161 | if (drv->probe != NULL) | |
2344c6de | 162 | error = driver_create_file(&drv->drv, &driver_attr_new_id); |
6179b556 BW |
163 | return error; |
164 | } | |
165 | ||
166 | ||
1da177e4 LT |
167 | /** |
168 | * pcmcia_register_driver - register a PCMCIA driver with the bus core | |
78187865 | 169 | * @driver: the &driver being registered |
1da177e4 LT |
170 | * |
171 | * Registers a PCMCIA driver with the PCMCIA bus core. | |
172 | */ | |
1da177e4 LT |
173 | int pcmcia_register_driver(struct pcmcia_driver *driver) |
174 | { | |
6179b556 BW |
175 | int error; |
176 | ||
1da177e4 LT |
177 | if (!driver) |
178 | return -EINVAL; | |
179 | ||
23a83bfe DB |
180 | pcmcia_check_driver(driver); |
181 | ||
1da177e4 LT |
182 | /* initialize common fields */ |
183 | driver->drv.bus = &pcmcia_bus_type; | |
184 | driver->drv.owner = driver->owner; | |
6179b556 BW |
185 | spin_lock_init(&driver->dynids.lock); |
186 | INIT_LIST_HEAD(&driver->dynids.list); | |
1da177e4 | 187 | |
d50dbec3 | 188 | pr_debug("registering driver %s\n", driver->drv.name); |
d9d9ea01 | 189 | |
6179b556 BW |
190 | error = driver_register(&driver->drv); |
191 | if (error < 0) | |
192 | return error; | |
193 | ||
194 | error = pcmcia_create_newid_file(driver); | |
195 | if (error) | |
196 | driver_unregister(&driver->drv); | |
197 | ||
198 | return error; | |
1da177e4 LT |
199 | } |
200 | EXPORT_SYMBOL(pcmcia_register_driver); | |
201 | ||
202 | /** | |
203 | * pcmcia_unregister_driver - unregister a PCMCIA driver with the bus core | |
78187865 | 204 | * @driver: the &driver being unregistered |
1da177e4 LT |
205 | */ |
206 | void pcmcia_unregister_driver(struct pcmcia_driver *driver) | |
207 | { | |
d50dbec3 | 208 | pr_debug("unregistering driver %s\n", driver->drv.name); |
1da177e4 | 209 | driver_unregister(&driver->drv); |
6179b556 | 210 | pcmcia_free_dynids(driver); |
1da177e4 LT |
211 | } |
212 | EXPORT_SYMBOL(pcmcia_unregister_driver); | |
213 | ||
1da177e4 LT |
214 | |
215 | /* pcmcia_device handling */ | |
216 | ||
e7a480d2 | 217 | struct pcmcia_device * pcmcia_get_dev(struct pcmcia_device *p_dev) |
1da177e4 LT |
218 | { |
219 | struct device *tmp_dev; | |
220 | tmp_dev = get_device(&p_dev->dev); | |
221 | if (!tmp_dev) | |
222 | return NULL; | |
223 | return to_pcmcia_dev(tmp_dev); | |
224 | } | |
225 | ||
e7a480d2 | 226 | void pcmcia_put_dev(struct pcmcia_device *p_dev) |
1da177e4 LT |
227 | { |
228 | if (p_dev) | |
229 | put_device(&p_dev->dev); | |
230 | } | |
231 | ||
360b65b9 DB |
232 | static void pcmcia_release_function(struct kref *ref) |
233 | { | |
234 | struct config_t *c = container_of(ref, struct config_t, ref); | |
d50dbec3 | 235 | pr_debug("releasing config_t\n"); |
360b65b9 DB |
236 | kfree(c); |
237 | } | |
238 | ||
1da177e4 LT |
239 | static void pcmcia_release_dev(struct device *dev) |
240 | { | |
241 | struct pcmcia_device *p_dev = to_pcmcia_dev(dev); | |
d50dbec3 | 242 | dev_dbg(dev, "releasing device\n"); |
dc109497 | 243 | pcmcia_put_socket(p_dev->socket); |
bd65a685 | 244 | kfree(p_dev->devname); |
360b65b9 | 245 | kref_put(&p_dev->function_config->ref, pcmcia_release_function); |
1da177e4 LT |
246 | kfree(p_dev); |
247 | } | |
248 | ||
1d2c9042 | 249 | static void pcmcia_add_device_later(struct pcmcia_socket *s, int mfc) |
82d56e6d DB |
250 | { |
251 | if (!s->pcmcia_state.device_add_pending) { | |
d50dbec3 | 252 | dev_dbg(&s->dev, "scheduling to add %s secondary" |
d9d9ea01 | 253 | " device to %d\n", mfc ? "mfc" : "pfc", s->sock); |
82d56e6d | 254 | s->pcmcia_state.device_add_pending = 1; |
1d2c9042 | 255 | s->pcmcia_state.mfc_pfc = mfc; |
82d56e6d DB |
256 | schedule_work(&s->device_add); |
257 | } | |
258 | return; | |
259 | } | |
1da177e4 LT |
260 | |
261 | static int pcmcia_device_probe(struct device * dev) | |
262 | { | |
263 | struct pcmcia_device *p_dev; | |
264 | struct pcmcia_driver *p_drv; | |
82d56e6d | 265 | struct pcmcia_device_id *did; |
f8cfa618 | 266 | struct pcmcia_socket *s; |
af2b3b50 | 267 | cistpl_config_t cis_config; |
1da177e4 LT |
268 | int ret = 0; |
269 | ||
270 | dev = get_device(dev); | |
271 | if (!dev) | |
272 | return -ENODEV; | |
273 | ||
274 | p_dev = to_pcmcia_dev(dev); | |
275 | p_drv = to_pcmcia_drv(dev->driver); | |
f8cfa618 | 276 | s = p_dev->socket; |
1da177e4 | 277 | |
f3e7a7b6 | 278 | /* The PCMCIA code passes the match data in via dev_set_drvdata(dev) |
cec5eb7b AC |
279 | * which is an ugly hack. Once the driver probe is called it may |
280 | * and often will overwrite the match data so we must save it first | |
281 | * | |
282 | * handle pseudo multifunction devices: | |
283 | * there are at most two pseudo multifunction devices. | |
284 | * if we're matching against the first, schedule a | |
285 | * call which will then check whether there are two | |
286 | * pseudo devices, and if not, add the second one. | |
287 | */ | |
f3e7a7b6 | 288 | did = dev_get_drvdata(&p_dev->dev); |
cec5eb7b | 289 | |
d50dbec3 | 290 | dev_dbg(dev, "trying to bind to %s\n", p_drv->drv.name); |
d9d9ea01 | 291 | |
360b65b9 DB |
292 | if ((!p_drv->probe) || (!p_dev->function_config) || |
293 | (!try_module_get(p_drv->owner))) { | |
1da177e4 LT |
294 | ret = -EINVAL; |
295 | goto put_dev; | |
296 | } | |
297 | ||
af2b3b50 DB |
298 | /* set up some more device information */ |
299 | ret = pccard_read_tuple(p_dev->socket, p_dev->func, CISTPL_CONFIG, | |
300 | &cis_config); | |
301 | if (!ret) { | |
302 | p_dev->conf.ConfigBase = cis_config.base; | |
303 | p_dev->conf.Present = cis_config.rmask[0]; | |
304 | } else { | |
ac449d6e DB |
305 | dev_printk(KERN_INFO, dev, |
306 | "pcmcia: could not parse base and rmask0 of CIS\n"); | |
af2b3b50 DB |
307 | p_dev->conf.ConfigBase = 0; |
308 | p_dev->conf.Present = 0; | |
309 | } | |
310 | ||
f8cfa618 | 311 | ret = p_drv->probe(p_dev); |
d9d9ea01 | 312 | if (ret) { |
d50dbec3 | 313 | dev_dbg(dev, "binding to %s failed with %d\n", |
ac449d6e | 314 | p_drv->drv.name, ret); |
82d56e6d | 315 | goto put_module; |
d9d9ea01 | 316 | } |
82d56e6d | 317 | |
4ceadbf5 | 318 | if (did && (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO) && |
82d56e6d | 319 | (p_dev->socket->device_count == 1) && (p_dev->device_no == 0)) |
1d2c9042 | 320 | pcmcia_add_device_later(p_dev->socket, 0); |
f8cfa618 | 321 | |
cec5eb7b | 322 | put_module: |
1da177e4 LT |
323 | if (ret) |
324 | module_put(p_drv->owner); | |
cec5eb7b | 325 | put_dev: |
f8cfa618 | 326 | if (ret) |
1da177e4 LT |
327 | put_device(dev); |
328 | return (ret); | |
329 | } | |
330 | ||
331 | ||
d6ff5a85 DB |
332 | /* |
333 | * Removes a PCMCIA card from the device tree and socket list. | |
334 | */ | |
335 | static void pcmcia_card_remove(struct pcmcia_socket *s, struct pcmcia_device *leftover) | |
336 | { | |
337 | struct pcmcia_device *p_dev; | |
338 | struct pcmcia_device *tmp; | |
339 | unsigned long flags; | |
340 | ||
d50dbec3 | 341 | dev_dbg(leftover ? &leftover->dev : &s->dev, |
ac449d6e DB |
342 | "pcmcia_card_remove(%d) %s\n", s->sock, |
343 | leftover ? leftover->devname : ""); | |
d6ff5a85 DB |
344 | |
345 | if (!leftover) | |
346 | s->device_count = 0; | |
347 | else | |
348 | s->device_count = 1; | |
349 | ||
350 | /* unregister all pcmcia_devices registered with this socket, except leftover */ | |
351 | list_for_each_entry_safe(p_dev, tmp, &s->devices_list, socket_device_list) { | |
352 | if (p_dev == leftover) | |
353 | continue; | |
354 | ||
355 | spin_lock_irqsave(&pcmcia_dev_list_lock, flags); | |
356 | list_del(&p_dev->socket_device_list); | |
357 | p_dev->_removed=1; | |
358 | spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags); | |
359 | ||
d50dbec3 | 360 | dev_dbg(&p_dev->dev, "unregistering device\n"); |
d6ff5a85 DB |
361 | device_unregister(&p_dev->dev); |
362 | } | |
363 | ||
364 | return; | |
365 | } | |
366 | ||
1da177e4 LT |
367 | static int pcmcia_device_remove(struct device * dev) |
368 | { | |
369 | struct pcmcia_device *p_dev; | |
370 | struct pcmcia_driver *p_drv; | |
d6ff5a85 | 371 | struct pcmcia_device_id *did; |
cc3b4866 | 372 | int i; |
1da177e4 | 373 | |
1da177e4 LT |
374 | p_dev = to_pcmcia_dev(dev); |
375 | p_drv = to_pcmcia_drv(dev->driver); | |
d6ff5a85 | 376 | |
d50dbec3 | 377 | dev_dbg(dev, "removing device\n"); |
d9d9ea01 | 378 | |
d6ff5a85 DB |
379 | /* If we're removing the primary module driving a |
380 | * pseudo multi-function card, we need to unbind | |
381 | * all devices | |
382 | */ | |
f3e7a7b6 | 383 | did = dev_get_drvdata(&p_dev->dev); |
b2f51a1c | 384 | if (did && (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO) && |
d6ff5a85 DB |
385 | (p_dev->socket->device_count != 0) && |
386 | (p_dev->device_no == 0)) | |
387 | pcmcia_card_remove(p_dev->socket, p_dev); | |
388 | ||
389 | /* detach the "instance" */ | |
f3990715 DB |
390 | if (!p_drv) |
391 | return 0; | |
1da177e4 | 392 | |
f3990715 | 393 | if (p_drv->remove) |
cc3b4866 DB |
394 | p_drv->remove(p_dev); |
395 | ||
a0aab143 DB |
396 | p_dev->dev_node = NULL; |
397 | ||
f3990715 | 398 | /* check for proper unloading */ |
e2d40963 | 399 | if (p_dev->_irq || p_dev->_io || p_dev->_locked) |
ac449d6e DB |
400 | dev_printk(KERN_INFO, dev, |
401 | "pcmcia: driver %s did not release config properly\n", | |
402 | p_drv->drv.name); | |
cc3b4866 | 403 | |
f3990715 | 404 | for (i = 0; i < MAX_WIN; i++) |
e2d40963 | 405 | if (p_dev->_win & CLIENT_WIN_REQ(i)) |
ac449d6e DB |
406 | dev_printk(KERN_INFO, dev, |
407 | "pcmcia: driver %s did not release window properly\n", | |
408 | p_drv->drv.name); | |
cc3b4866 | 409 | |
f3990715 DB |
410 | /* references from pcmcia_probe_device */ |
411 | pcmcia_put_dev(p_dev); | |
412 | module_put(p_drv->owner); | |
1da177e4 LT |
413 | |
414 | return 0; | |
415 | } | |
416 | ||
417 | ||
1da177e4 LT |
418 | /* |
419 | * pcmcia_device_query -- determine information about a pcmcia device | |
420 | */ | |
421 | static int pcmcia_device_query(struct pcmcia_device *p_dev) | |
422 | { | |
423 | cistpl_manfid_t manf_id; | |
424 | cistpl_funcid_t func_id; | |
76fa82fb | 425 | cistpl_vers_1_t *vers1; |
1da177e4 LT |
426 | unsigned int i; |
427 | ||
76fa82fb IM |
428 | vers1 = kmalloc(sizeof(*vers1), GFP_KERNEL); |
429 | if (!vers1) | |
430 | return -ENOMEM; | |
431 | ||
84897fc0 | 432 | if (!pccard_read_tuple(p_dev->socket, BIND_FN_ALL, |
1da177e4 LT |
433 | CISTPL_MANFID, &manf_id)) { |
434 | p_dev->manf_id = manf_id.manf; | |
435 | p_dev->card_id = manf_id.card; | |
436 | p_dev->has_manf_id = 1; | |
437 | p_dev->has_card_id = 1; | |
438 | } | |
439 | ||
440 | if (!pccard_read_tuple(p_dev->socket, p_dev->func, | |
441 | CISTPL_FUNCID, &func_id)) { | |
442 | p_dev->func_id = func_id.func; | |
443 | p_dev->has_func_id = 1; | |
444 | } else { | |
445 | /* rule of thumb: cards with no FUNCID, but with | |
446 | * common memory device geometry information, are | |
447 | * probably memory cards (from pcmcia-cs) */ | |
76fa82fb IM |
448 | cistpl_device_geo_t *devgeo; |
449 | ||
450 | devgeo = kmalloc(sizeof(*devgeo), GFP_KERNEL); | |
451 | if (!devgeo) { | |
452 | kfree(vers1); | |
453 | return -ENOMEM; | |
454 | } | |
1da177e4 | 455 | if (!pccard_read_tuple(p_dev->socket, p_dev->func, |
76fa82fb | 456 | CISTPL_DEVICE_GEO, devgeo)) { |
d50dbec3 | 457 | dev_dbg(&p_dev->dev, |
ac449d6e DB |
458 | "mem device geometry probably means " |
459 | "FUNCID_MEMORY\n"); | |
1da177e4 LT |
460 | p_dev->func_id = CISTPL_FUNCID_MEMORY; |
461 | p_dev->has_func_id = 1; | |
462 | } | |
76fa82fb | 463 | kfree(devgeo); |
1da177e4 LT |
464 | } |
465 | ||
84897fc0 | 466 | if (!pccard_read_tuple(p_dev->socket, BIND_FN_ALL, CISTPL_VERS_1, |
76fa82fb | 467 | vers1)) { |
c5e09528 | 468 | for (i = 0; i < min_t(unsigned int, 4, vers1->ns); i++) { |
1da177e4 LT |
469 | char *tmp; |
470 | unsigned int length; | |
471 | ||
76fa82fb | 472 | tmp = vers1->str + vers1->ofs[i]; |
1da177e4 LT |
473 | |
474 | length = strlen(tmp) + 1; | |
6e3d4f25 | 475 | if ((length < 2) || (length > 255)) |
1da177e4 LT |
476 | continue; |
477 | ||
478 | p_dev->prod_id[i] = kmalloc(sizeof(char) * length, | |
479 | GFP_KERNEL); | |
480 | if (!p_dev->prod_id[i]) | |
481 | continue; | |
482 | ||
483 | p_dev->prod_id[i] = strncpy(p_dev->prod_id[i], | |
484 | tmp, length); | |
485 | } | |
486 | } | |
487 | ||
76fa82fb | 488 | kfree(vers1); |
1da177e4 LT |
489 | return 0; |
490 | } | |
491 | ||
492 | ||
493 | /* device_add_lock is needed to avoid double registration by cardmgr and kernel. | |
494 | * Serializes pcmcia_device_add; will most likely be removed in future. | |
495 | * | |
496 | * While it has the caveat that adding new PCMCIA devices inside(!) device_register() | |
497 | * won't work, this doesn't matter much at the moment: the driver core doesn't | |
498 | * support it either. | |
499 | */ | |
7fe908dd | 500 | static DEFINE_MUTEX(device_add_lock); |
1da177e4 | 501 | |
dc109497 | 502 | struct pcmcia_device * pcmcia_device_add(struct pcmcia_socket *s, unsigned int function) |
1da177e4 | 503 | { |
360b65b9 | 504 | struct pcmcia_device *p_dev, *tmp_dev; |
1da177e4 LT |
505 | unsigned long flags; |
506 | ||
dc109497 | 507 | s = pcmcia_get_socket(s); |
1da177e4 LT |
508 | if (!s) |
509 | return NULL; | |
510 | ||
7fe908dd | 511 | mutex_lock(&device_add_lock); |
1da177e4 | 512 | |
d50dbec3 | 513 | pr_debug("adding device to %d, function %d\n", s->sock, function); |
d9d9ea01 | 514 | |
6cf5be51 DB |
515 | /* max of 4 devices per card */ |
516 | if (s->device_count == 4) | |
1ad275e3 DB |
517 | goto err_put; |
518 | ||
8084b372 | 519 | p_dev = kzalloc(sizeof(struct pcmcia_device), GFP_KERNEL); |
1da177e4 LT |
520 | if (!p_dev) |
521 | goto err_put; | |
1da177e4 | 522 | |
dc109497 | 523 | p_dev->socket = s; |
1da177e4 LT |
524 | p_dev->device_no = (s->device_count++); |
525 | p_dev->func = function; | |
526 | ||
527 | p_dev->dev.bus = &pcmcia_bus_type; | |
87373318 | 528 | p_dev->dev.parent = s->dev.parent; |
1da177e4 | 529 | p_dev->dev.release = pcmcia_release_dev; |
43d9f7fd JB |
530 | /* by default don't allow DMA */ |
531 | p_dev->dma_mask = DMA_MASK_NONE; | |
532 | p_dev->dev.dma_mask = &p_dev->dma_mask; | |
25096986 KS |
533 | dev_set_name(&p_dev->dev, "%d.%d", p_dev->socket->sock, p_dev->device_no); |
534 | if (!dev_name(&p_dev->dev)) | |
535 | goto err_free; | |
536 | p_dev->devname = kasprintf(GFP_KERNEL, "pcmcia%s", dev_name(&p_dev->dev)); | |
bd65a685 BG |
537 | if (!p_dev->devname) |
538 | goto err_free; | |
d50dbec3 | 539 | dev_dbg(&p_dev->dev, "devname is %s\n", p_dev->devname); |
1da177e4 | 540 | |
1da177e4 | 541 | spin_lock_irqsave(&pcmcia_dev_list_lock, flags); |
360b65b9 DB |
542 | |
543 | /* | |
544 | * p_dev->function_config must be the same for all card functions. | |
545 | * Note that this is serialized by the device_add_lock, so that | |
546 | * only one such struct will be created. | |
547 | */ | |
548 | list_for_each_entry(tmp_dev, &s->devices_list, socket_device_list) | |
549 | if (p_dev->func == tmp_dev->func) { | |
550 | p_dev->function_config = tmp_dev->function_config; | |
3e879f61 K |
551 | p_dev->io = tmp_dev->io; |
552 | p_dev->irq = tmp_dev->irq; | |
360b65b9 DB |
553 | kref_get(&p_dev->function_config->ref); |
554 | } | |
555 | ||
556 | /* Add to the list in pcmcia_bus_socket */ | |
6171b88b | 557 | list_add(&p_dev->socket_device_list, &s->devices_list); |
360b65b9 | 558 | |
1da177e4 LT |
559 | spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags); |
560 | ||
360b65b9 | 561 | if (!p_dev->function_config) { |
d50dbec3 | 562 | dev_dbg(&p_dev->dev, "creating config_t\n"); |
360b65b9 DB |
563 | p_dev->function_config = kzalloc(sizeof(struct config_t), |
564 | GFP_KERNEL); | |
565 | if (!p_dev->function_config) | |
566 | goto err_unreg; | |
567 | kref_init(&p_dev->function_config->ref); | |
568 | } | |
569 | ||
ac449d6e DB |
570 | dev_printk(KERN_NOTICE, &p_dev->dev, |
571 | "pcmcia: registering new device %s\n", | |
572 | p_dev->devname); | |
807277cb | 573 | |
1ad275e3 DB |
574 | pcmcia_device_query(p_dev); |
575 | ||
360b65b9 DB |
576 | if (device_register(&p_dev->dev)) |
577 | goto err_unreg; | |
1da177e4 | 578 | |
7fe908dd | 579 | mutex_unlock(&device_add_lock); |
1da177e4 LT |
580 | |
581 | return p_dev; | |
582 | ||
360b65b9 DB |
583 | err_unreg: |
584 | spin_lock_irqsave(&pcmcia_dev_list_lock, flags); | |
585 | list_del(&p_dev->socket_device_list); | |
586 | spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags); | |
587 | ||
1da177e4 | 588 | err_free: |
bd65a685 | 589 | kfree(p_dev->devname); |
1da177e4 LT |
590 | kfree(p_dev); |
591 | s->device_count--; | |
592 | err_put: | |
7fe908dd | 593 | mutex_unlock(&device_add_lock); |
dc109497 | 594 | pcmcia_put_socket(s); |
1da177e4 LT |
595 | |
596 | return NULL; | |
597 | } | |
598 | ||
599 | ||
600 | static int pcmcia_card_add(struct pcmcia_socket *s) | |
601 | { | |
1da177e4 | 602 | cistpl_longlink_mfc_t mfc; |
c5081d5f | 603 | unsigned int no_funcs, i, no_chains; |
1da177e4 LT |
604 | int ret = 0; |
605 | ||
d9d9ea01 | 606 | if (!(s->resource_setup_done)) { |
d50dbec3 | 607 | dev_dbg(&s->dev, |
ac449d6e | 608 | "no resources available, delaying card_add\n"); |
1da177e4 | 609 | return -EAGAIN; /* try again, but later... */ |
d9d9ea01 | 610 | } |
1da177e4 | 611 | |
d9d9ea01 | 612 | if (pcmcia_validate_mem(s)) { |
d50dbec3 | 613 | dev_dbg(&s->dev, "validating mem resources failed, " |
d9d9ea01 | 614 | "delaying card_add\n"); |
de75914e | 615 | return -EAGAIN; /* try again, but later... */ |
d9d9ea01 | 616 | } |
de75914e | 617 | |
84897fc0 | 618 | ret = pccard_validate_cis(s, &no_chains); |
c5081d5f | 619 | if (ret || !no_chains) { |
d50dbec3 | 620 | dev_dbg(&s->dev, "invalid CIS or invalid resources\n"); |
1da177e4 LT |
621 | return -ENODEV; |
622 | } | |
623 | ||
624 | if (!pccard_read_tuple(s, BIND_FN_ALL, CISTPL_LONGLINK_MFC, &mfc)) | |
625 | no_funcs = mfc.nfn; | |
626 | else | |
627 | no_funcs = 1; | |
1d2c9042 | 628 | s->functions = no_funcs; |
1da177e4 | 629 | |
1da177e4 | 630 | for (i=0; i < no_funcs; i++) |
dc109497 | 631 | pcmcia_device_add(s, i); |
1da177e4 LT |
632 | |
633 | return (ret); | |
634 | } | |
635 | ||
636 | ||
4796b71f | 637 | static void pcmcia_delayed_add_device(struct work_struct *work) |
1ad275e3 | 638 | { |
c4028958 DH |
639 | struct pcmcia_socket *s = |
640 | container_of(work, struct pcmcia_socket, device_add); | |
d50dbec3 | 641 | dev_dbg(&s->dev, "adding additional device to %d\n", s->sock); |
1d2c9042 | 642 | pcmcia_device_add(s, s->pcmcia_state.mfc_pfc); |
b5e43913 | 643 | s->pcmcia_state.device_add_pending = 0; |
1d2c9042 | 644 | s->pcmcia_state.mfc_pfc = 0; |
1ad275e3 DB |
645 | } |
646 | ||
e2f0b534 | 647 | static int pcmcia_requery(struct device *dev, void * _data) |
ff1fa9ef | 648 | { |
e2f0b534 | 649 | struct pcmcia_device *p_dev = to_pcmcia_dev(dev); |
d9d9ea01 | 650 | if (!p_dev->dev.driver) { |
d50dbec3 | 651 | dev_dbg(dev, "update device information\n"); |
e2f0b534 | 652 | pcmcia_device_query(p_dev); |
d9d9ea01 | 653 | } |
e2f0b534 DB |
654 | |
655 | return 0; | |
656 | } | |
657 | ||
4ae1cbf1 | 658 | static void pcmcia_bus_rescan(struct pcmcia_socket *skt, int new_cis) |
e2f0b534 | 659 | { |
4ae1cbf1 | 660 | int no_devices = 0; |
f901b8c4 | 661 | int ret = 0; |
e2f0b534 DB |
662 | unsigned long flags; |
663 | ||
7fe908dd | 664 | /* must be called with skt_mutex held */ |
d50dbec3 | 665 | dev_dbg(&skt->dev, "re-scanning socket %d\n", skt->sock); |
d9d9ea01 | 666 | |
e2f0b534 | 667 | spin_lock_irqsave(&pcmcia_dev_list_lock, flags); |
dc109497 | 668 | if (list_empty(&skt->devices_list)) |
4ae1cbf1 | 669 | no_devices = 1; |
e2f0b534 DB |
670 | spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags); |
671 | ||
4ae1cbf1 DB |
672 | /* If this is because of a CIS override, start over */ |
673 | if (new_cis && !no_devices) | |
674 | pcmcia_card_remove(skt, NULL); | |
675 | ||
e2f0b534 DB |
676 | /* if no devices were added for this socket yet because of |
677 | * missing resource information or other trouble, we need to | |
678 | * do this now. */ | |
4ae1cbf1 | 679 | if (no_devices || new_cis) { |
f901b8c4 | 680 | ret = pcmcia_card_add(skt); |
e2f0b534 DB |
681 | if (ret) |
682 | return; | |
683 | } | |
684 | ||
685 | /* some device information might have changed because of a CIS | |
686 | * update or because we can finally read it correctly... so | |
687 | * determine it again, overwriting old values if necessary. */ | |
688 | bus_for_each_dev(&pcmcia_bus_type, NULL, NULL, pcmcia_requery); | |
689 | ||
690 | /* we re-scan all devices, not just the ones connected to this | |
691 | * socket. This does not matter, though. */ | |
f901b8c4 DB |
692 | ret = bus_rescan_devices(&pcmcia_bus_type); |
693 | if (ret) | |
694 | printk(KERN_INFO "pcmcia: bus_rescan_devices failed\n"); | |
ff1fa9ef | 695 | } |
1ad275e3 | 696 | |
1d2c9042 DB |
697 | #ifdef CONFIG_PCMCIA_LOAD_CIS |
698 | ||
699 | /** | |
700 | * pcmcia_load_firmware - load CIS from userspace if device-provided is broken | |
78187865 RD |
701 | * @dev: the pcmcia device which needs a CIS override |
702 | * @filename: requested filename in /lib/firmware/ | |
1d2c9042 DB |
703 | * |
704 | * This uses the in-kernel firmware loading mechanism to use a "fake CIS" if | |
705 | * the one provided by the card is broken. The firmware files reside in | |
706 | * /lib/firmware/ in userspace. | |
707 | */ | |
708 | static int pcmcia_load_firmware(struct pcmcia_device *dev, char * filename) | |
709 | { | |
710 | struct pcmcia_socket *s = dev->socket; | |
711 | const struct firmware *fw; | |
1d2c9042 DB |
712 | int ret = -ENOMEM; |
713 | int no_funcs; | |
714 | int old_funcs; | |
1d2c9042 DB |
715 | cistpl_longlink_mfc_t mfc; |
716 | ||
717 | if (!filename) | |
718 | return -EINVAL; | |
719 | ||
d50dbec3 | 720 | dev_dbg(&dev->dev, "trying to load CIS file %s\n", filename); |
1d2c9042 | 721 | |
ed62acec | 722 | if (request_firmware(&fw, filename, &dev->dev) == 0) { |
d9d9ea01 DB |
723 | if (fw->size >= CISTPL_MAX_CIS_SIZE) { |
724 | ret = -EINVAL; | |
ac449d6e DB |
725 | dev_printk(KERN_ERR, &dev->dev, |
726 | "pcmcia: CIS override is too big\n"); | |
1d2c9042 | 727 | goto release; |
d9d9ea01 | 728 | } |
1d2c9042 | 729 | |
53efec95 | 730 | if (!pcmcia_replace_cis(s, fw->data, fw->size)) |
1d2c9042 | 731 | ret = 0; |
d9d9ea01 | 732 | else { |
ac449d6e DB |
733 | dev_printk(KERN_ERR, &dev->dev, |
734 | "pcmcia: CIS override failed\n"); | |
d9d9ea01 DB |
735 | goto release; |
736 | } | |
737 | ||
1d2c9042 DB |
738 | |
739 | /* update information */ | |
740 | pcmcia_device_query(dev); | |
741 | ||
742 | /* does this cis override add or remove functions? */ | |
743 | old_funcs = s->functions; | |
744 | ||
745 | if (!pccard_read_tuple(s, BIND_FN_ALL, CISTPL_LONGLINK_MFC, &mfc)) | |
746 | no_funcs = mfc.nfn; | |
747 | else | |
748 | no_funcs = 1; | |
749 | s->functions = no_funcs; | |
750 | ||
751 | if (old_funcs > no_funcs) | |
752 | pcmcia_card_remove(s, dev); | |
753 | else if (no_funcs > old_funcs) | |
754 | pcmcia_add_device_later(s, 1); | |
755 | } | |
756 | release: | |
757 | release_firmware(fw); | |
758 | ||
759 | return (ret); | |
760 | } | |
761 | ||
762 | #else /* !CONFIG_PCMCIA_LOAD_CIS */ | |
763 | ||
764 | static inline int pcmcia_load_firmware(struct pcmcia_device *dev, char * filename) | |
765 | { | |
766 | return -ENODEV; | |
767 | } | |
768 | ||
769 | #endif | |
770 | ||
771 | ||
1ad275e3 DB |
772 | static inline int pcmcia_devmatch(struct pcmcia_device *dev, |
773 | struct pcmcia_device_id *did) | |
774 | { | |
775 | if (did->match_flags & PCMCIA_DEV_ID_MATCH_MANF_ID) { | |
776 | if ((!dev->has_manf_id) || (dev->manf_id != did->manf_id)) | |
777 | return 0; | |
778 | } | |
779 | ||
780 | if (did->match_flags & PCMCIA_DEV_ID_MATCH_CARD_ID) { | |
781 | if ((!dev->has_card_id) || (dev->card_id != did->card_id)) | |
782 | return 0; | |
783 | } | |
784 | ||
785 | if (did->match_flags & PCMCIA_DEV_ID_MATCH_FUNCTION) { | |
786 | if (dev->func != did->function) | |
787 | return 0; | |
788 | } | |
789 | ||
790 | if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID1) { | |
791 | if (!dev->prod_id[0]) | |
792 | return 0; | |
793 | if (strcmp(did->prod_id[0], dev->prod_id[0])) | |
794 | return 0; | |
795 | } | |
796 | ||
797 | if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID2) { | |
798 | if (!dev->prod_id[1]) | |
799 | return 0; | |
800 | if (strcmp(did->prod_id[1], dev->prod_id[1])) | |
801 | return 0; | |
802 | } | |
803 | ||
804 | if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID3) { | |
805 | if (!dev->prod_id[2]) | |
806 | return 0; | |
807 | if (strcmp(did->prod_id[2], dev->prod_id[2])) | |
808 | return 0; | |
809 | } | |
810 | ||
811 | if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID4) { | |
812 | if (!dev->prod_id[3]) | |
813 | return 0; | |
814 | if (strcmp(did->prod_id[3], dev->prod_id[3])) | |
815 | return 0; | |
816 | } | |
817 | ||
818 | if (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO) { | |
1ad275e3 DB |
819 | if (dev->device_no != did->device_no) |
820 | return 0; | |
821 | } | |
822 | ||
823 | if (did->match_flags & PCMCIA_DEV_ID_MATCH_FUNC_ID) { | |
824 | if ((!dev->has_func_id) || (dev->func_id != did->func_id)) | |
825 | return 0; | |
826 | ||
827 | /* if this is a pseudo-multi-function device, | |
828 | * we need explicit matches */ | |
829 | if (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO) | |
830 | return 0; | |
831 | if (dev->device_no) | |
832 | return 0; | |
833 | ||
834 | /* also, FUNC_ID matching needs to be activated by userspace | |
835 | * after it has re-checked that there is no possible module | |
836 | * with a prod_id/manf_id/card_id match. | |
837 | */ | |
d50dbec3 | 838 | dev_dbg(&dev->dev, |
ac449d6e | 839 | "skipping FUNC_ID match until userspace interaction\n"); |
1ad275e3 DB |
840 | if (!dev->allow_func_id_match) |
841 | return 0; | |
842 | } | |
843 | ||
ea7b3882 | 844 | if (did->match_flags & PCMCIA_DEV_ID_MATCH_FAKE_CIS) { |
d50dbec3 | 845 | dev_dbg(&dev->dev, "device needs a fake CIS\n"); |
daa9517d DB |
846 | if (!dev->socket->fake_cis) |
847 | pcmcia_load_firmware(dev, did->cisfile); | |
848 | ||
849 | if (!dev->socket->fake_cis) | |
ea7b3882 | 850 | return 0; |
ea7b3882 DB |
851 | } |
852 | ||
f602ff7e DB |
853 | if (did->match_flags & PCMCIA_DEV_ID_MATCH_ANONYMOUS) { |
854 | int i; | |
855 | for (i=0; i<4; i++) | |
856 | if (dev->prod_id[i]) | |
857 | return 0; | |
858 | if (dev->has_manf_id || dev->has_card_id || dev->has_func_id) | |
859 | return 0; | |
860 | } | |
861 | ||
f3e7a7b6 | 862 | dev_set_drvdata(&dev->dev, did); |
1ad275e3 DB |
863 | |
864 | return 1; | |
865 | } | |
866 | ||
867 | ||
1da177e4 LT |
868 | static int pcmcia_bus_match(struct device * dev, struct device_driver * drv) { |
869 | struct pcmcia_device * p_dev = to_pcmcia_dev(dev); | |
870 | struct pcmcia_driver * p_drv = to_pcmcia_drv(drv); | |
1ad275e3 | 871 | struct pcmcia_device_id *did = p_drv->id_table; |
6179b556 BW |
872 | struct pcmcia_dynid *dynid; |
873 | ||
874 | /* match dynamic devices first */ | |
875 | spin_lock(&p_drv->dynids.lock); | |
876 | list_for_each_entry(dynid, &p_drv->dynids.list, node) { | |
d50dbec3 | 877 | dev_dbg(dev, "trying to match to %s\n", drv->name); |
6179b556 | 878 | if (pcmcia_devmatch(p_dev, &dynid->id)) { |
d50dbec3 | 879 | dev_dbg(dev, "matched to %s\n", drv->name); |
6179b556 BW |
880 | spin_unlock(&p_drv->dynids.lock); |
881 | return 1; | |
882 | } | |
883 | } | |
884 | spin_unlock(&p_drv->dynids.lock); | |
1da177e4 | 885 | |
0e0fad8f | 886 | #ifdef CONFIG_PCMCIA_IOCTL |
1da177e4 | 887 | /* matching by cardmgr */ |
d9d9ea01 | 888 | if (p_dev->cardmgr == p_drv) { |
d50dbec3 | 889 | dev_dbg(dev, "cardmgr matched to %s\n", drv->name); |
1da177e4 | 890 | return 1; |
d9d9ea01 | 891 | } |
0e0fad8f | 892 | #endif |
1da177e4 | 893 | |
1ad275e3 | 894 | while (did && did->match_flags) { |
d50dbec3 | 895 | dev_dbg(dev, "trying to match to %s\n", drv->name); |
d9d9ea01 | 896 | if (pcmcia_devmatch(p_dev, did)) { |
d50dbec3 | 897 | dev_dbg(dev, "matched to %s\n", drv->name); |
1ad275e3 | 898 | return 1; |
d9d9ea01 | 899 | } |
1ad275e3 DB |
900 | did++; |
901 | } | |
902 | ||
1da177e4 LT |
903 | return 0; |
904 | } | |
905 | ||
840c2ac5 DB |
906 | #ifdef CONFIG_HOTPLUG |
907 | ||
7eff2e7a | 908 | static int pcmcia_bus_uevent(struct device *dev, struct kobj_uevent_env *env) |
840c2ac5 DB |
909 | { |
910 | struct pcmcia_device *p_dev; | |
7eff2e7a | 911 | int i; |
840c2ac5 DB |
912 | u32 hash[4] = { 0, 0, 0, 0}; |
913 | ||
914 | if (!dev) | |
915 | return -ENODEV; | |
916 | ||
917 | p_dev = to_pcmcia_dev(dev); | |
918 | ||
919 | /* calculate hashes */ | |
920 | for (i=0; i<4; i++) { | |
921 | if (!p_dev->prod_id[i]) | |
922 | continue; | |
923 | hash[i] = crc32(0, p_dev->prod_id[i], strlen(p_dev->prod_id[i])); | |
924 | } | |
925 | ||
7eff2e7a | 926 | if (add_uevent_var(env, "SOCKET_NO=%u", p_dev->socket->sock)) |
840c2ac5 DB |
927 | return -ENOMEM; |
928 | ||
7eff2e7a | 929 | if (add_uevent_var(env, "DEVICE_NO=%02X", p_dev->device_no)) |
840c2ac5 DB |
930 | return -ENOMEM; |
931 | ||
7eff2e7a | 932 | if (add_uevent_var(env, "MODALIAS=pcmcia:m%04Xc%04Xf%02Xfn%02Xpfn%02X" |
312c004d KS |
933 | "pa%08Xpb%08Xpc%08Xpd%08X", |
934 | p_dev->has_manf_id ? p_dev->manf_id : 0, | |
935 | p_dev->has_card_id ? p_dev->card_id : 0, | |
936 | p_dev->has_func_id ? p_dev->func_id : 0, | |
937 | p_dev->func, | |
938 | p_dev->device_no, | |
939 | hash[0], | |
940 | hash[1], | |
941 | hash[2], | |
942 | hash[3])) | |
840c2ac5 DB |
943 | return -ENOMEM; |
944 | ||
840c2ac5 DB |
945 | return 0; |
946 | } | |
947 | ||
948 | #else | |
949 | ||
7eff2e7a | 950 | static int pcmcia_bus_uevent(struct device *dev, struct kobj_uevent_env *env) |
840c2ac5 DB |
951 | { |
952 | return -ENODEV; | |
953 | } | |
954 | ||
955 | #endif | |
956 | ||
3f8df781 AS |
957 | /************************ runtime PM support ***************************/ |
958 | ||
959 | static int pcmcia_dev_suspend(struct device *dev, pm_message_t state); | |
960 | static int pcmcia_dev_resume(struct device *dev); | |
961 | ||
962 | static int runtime_suspend(struct device *dev) | |
963 | { | |
964 | int rc; | |
965 | ||
966 | down(&dev->sem); | |
967 | rc = pcmcia_dev_suspend(dev, PMSG_SUSPEND); | |
968 | up(&dev->sem); | |
3f8df781 AS |
969 | return rc; |
970 | } | |
971 | ||
972 | static void runtime_resume(struct device *dev) | |
973 | { | |
974 | int rc; | |
975 | ||
976 | down(&dev->sem); | |
977 | rc = pcmcia_dev_resume(dev); | |
978 | up(&dev->sem); | |
3f8df781 AS |
979 | } |
980 | ||
1da177e4 LT |
981 | /************************ per-device sysfs output ***************************/ |
982 | ||
983 | #define pcmcia_device_attr(field, test, format) \ | |
e404e274 | 984 | static ssize_t field##_show (struct device *dev, struct device_attribute *attr, char *buf) \ |
1da177e4 LT |
985 | { \ |
986 | struct pcmcia_device *p_dev = to_pcmcia_dev(dev); \ | |
987 | return p_dev->test ? sprintf (buf, format, p_dev->field) : -ENODEV; \ | |
988 | } | |
989 | ||
990 | #define pcmcia_device_stringattr(name, field) \ | |
e404e274 | 991 | static ssize_t name##_show (struct device *dev, struct device_attribute *attr, char *buf) \ |
1da177e4 LT |
992 | { \ |
993 | struct pcmcia_device *p_dev = to_pcmcia_dev(dev); \ | |
994 | return p_dev->field ? sprintf (buf, "%s\n", p_dev->field) : -ENODEV; \ | |
995 | } | |
996 | ||
997 | pcmcia_device_attr(func, socket, "0x%02x\n"); | |
998 | pcmcia_device_attr(func_id, has_func_id, "0x%02x\n"); | |
999 | pcmcia_device_attr(manf_id, has_manf_id, "0x%04x\n"); | |
1000 | pcmcia_device_attr(card_id, has_card_id, "0x%04x\n"); | |
1001 | pcmcia_device_stringattr(prod_id1, prod_id[0]); | |
1002 | pcmcia_device_stringattr(prod_id2, prod_id[1]); | |
1003 | pcmcia_device_stringattr(prod_id3, prod_id[2]); | |
1004 | pcmcia_device_stringattr(prod_id4, prod_id[3]); | |
1005 | ||
db1019ca DB |
1006 | |
1007 | static ssize_t pcmcia_show_pm_state(struct device *dev, struct device_attribute *attr, char *buf) | |
1008 | { | |
1009 | struct pcmcia_device *p_dev = to_pcmcia_dev(dev); | |
1010 | ||
f6fbe01a | 1011 | if (p_dev->suspended) |
db1019ca DB |
1012 | return sprintf(buf, "off\n"); |
1013 | else | |
1014 | return sprintf(buf, "on\n"); | |
1015 | } | |
1016 | ||
1017 | static ssize_t pcmcia_store_pm_state(struct device *dev, struct device_attribute *attr, | |
1018 | const char *buf, size_t count) | |
1019 | { | |
1020 | struct pcmcia_device *p_dev = to_pcmcia_dev(dev); | |
1021 | int ret = 0; | |
1022 | ||
1023 | if (!count) | |
1024 | return -EINVAL; | |
1025 | ||
f6fbe01a | 1026 | if ((!p_dev->suspended) && !strncmp(buf, "off", 3)) |
3f8df781 | 1027 | ret = runtime_suspend(dev); |
f6fbe01a | 1028 | else if (p_dev->suspended && !strncmp(buf, "on", 2)) |
3f8df781 | 1029 | runtime_resume(dev); |
db1019ca DB |
1030 | |
1031 | return ret ? ret : count; | |
1032 | } | |
1033 | ||
1034 | ||
3704511b | 1035 | static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf) |
3248ff43 DB |
1036 | { |
1037 | struct pcmcia_device *p_dev = to_pcmcia_dev(dev); | |
1038 | int i; | |
1039 | u32 hash[4] = { 0, 0, 0, 0}; | |
1040 | ||
1041 | /* calculate hashes */ | |
1042 | for (i=0; i<4; i++) { | |
1043 | if (!p_dev->prod_id[i]) | |
1044 | continue; | |
1045 | hash[i] = crc32(0,p_dev->prod_id[i],strlen(p_dev->prod_id[i])); | |
1046 | } | |
1047 | return sprintf(buf, "pcmcia:m%04Xc%04Xf%02Xfn%02Xpfn%02X" | |
1048 | "pa%08Xpb%08Xpc%08Xpd%08X\n", | |
1049 | p_dev->has_manf_id ? p_dev->manf_id : 0, | |
1050 | p_dev->has_card_id ? p_dev->card_id : 0, | |
1051 | p_dev->has_func_id ? p_dev->func_id : 0, | |
1052 | p_dev->func, p_dev->device_no, | |
1053 | hash[0], hash[1], hash[2], hash[3]); | |
1054 | } | |
a5b55778 | 1055 | |
3248ff43 DB |
1056 | static ssize_t pcmcia_store_allow_func_id_match(struct device *dev, |
1057 | struct device_attribute *attr, const char *buf, size_t count) | |
a5b55778 DB |
1058 | { |
1059 | struct pcmcia_device *p_dev = to_pcmcia_dev(dev); | |
f901b8c4 | 1060 | int ret; |
db1019ca DB |
1061 | |
1062 | if (!count) | |
1063 | return -EINVAL; | |
a5b55778 | 1064 | |
7fe908dd | 1065 | mutex_lock(&p_dev->socket->skt_mutex); |
a5b55778 | 1066 | p_dev->allow_func_id_match = 1; |
7fe908dd | 1067 | mutex_unlock(&p_dev->socket->skt_mutex); |
a5b55778 | 1068 | |
f901b8c4 DB |
1069 | ret = bus_rescan_devices(&pcmcia_bus_type); |
1070 | if (ret) | |
1071 | printk(KERN_INFO "pcmcia: bus_rescan_devices failed after " | |
1072 | "allowing func_id matches\n"); | |
a5b55778 DB |
1073 | |
1074 | return count; | |
1075 | } | |
1076 | ||
1da177e4 LT |
1077 | static struct device_attribute pcmcia_dev_attrs[] = { |
1078 | __ATTR(function, 0444, func_show, NULL), | |
db1019ca | 1079 | __ATTR(pm_state, 0644, pcmcia_show_pm_state, pcmcia_store_pm_state), |
1da177e4 LT |
1080 | __ATTR_RO(func_id), |
1081 | __ATTR_RO(manf_id), | |
1082 | __ATTR_RO(card_id), | |
1083 | __ATTR_RO(prod_id1), | |
1084 | __ATTR_RO(prod_id2), | |
1085 | __ATTR_RO(prod_id3), | |
1086 | __ATTR_RO(prod_id4), | |
3248ff43 | 1087 | __ATTR_RO(modalias), |
a5b55778 | 1088 | __ATTR(allow_func_id_match, 0200, NULL, pcmcia_store_allow_func_id_match), |
1da177e4 LT |
1089 | __ATTR_NULL, |
1090 | }; | |
1091 | ||
8e9e793d DB |
1092 | /* PM support, also needed for reset */ |
1093 | ||
1094 | static int pcmcia_dev_suspend(struct device * dev, pm_message_t state) | |
1095 | { | |
1096 | struct pcmcia_device *p_dev = to_pcmcia_dev(dev); | |
1097 | struct pcmcia_driver *p_drv = NULL; | |
f6fbe01a | 1098 | int ret = 0; |
8e9e793d | 1099 | |
d6b4fa6d DB |
1100 | if (p_dev->suspended) |
1101 | return 0; | |
1102 | ||
d50dbec3 | 1103 | dev_dbg(dev, "suspending\n"); |
d9d9ea01 | 1104 | |
8e9e793d DB |
1105 | if (dev->driver) |
1106 | p_drv = to_pcmcia_drv(dev->driver); | |
1107 | ||
e2d40963 DB |
1108 | if (!p_drv) |
1109 | goto out; | |
1110 | ||
1111 | if (p_drv->suspend) { | |
8661bb5b | 1112 | ret = p_drv->suspend(p_dev); |
d9d9ea01 | 1113 | if (ret) { |
ac449d6e DB |
1114 | dev_printk(KERN_ERR, dev, |
1115 | "pcmcia: device %s (driver %s) did " | |
1116 | "not want to go to sleep (%d)\n", | |
1117 | p_dev->devname, p_drv->drv.name, ret); | |
f6fbe01a | 1118 | goto out; |
d9d9ea01 | 1119 | } |
8661bb5b | 1120 | } |
8e9e793d | 1121 | |
d9d9ea01 | 1122 | if (p_dev->device_no == p_dev->func) { |
d50dbec3 | 1123 | dev_dbg(dev, "releasing configuration\n"); |
e2d40963 | 1124 | pcmcia_release_configuration(p_dev); |
d9d9ea01 | 1125 | } |
e2d40963 | 1126 | |
f6fbe01a DB |
1127 | out: |
1128 | if (!ret) | |
1129 | p_dev->suspended = 1; | |
1130 | return ret; | |
8e9e793d DB |
1131 | } |
1132 | ||
1133 | ||
1134 | static int pcmcia_dev_resume(struct device * dev) | |
1135 | { | |
1136 | struct pcmcia_device *p_dev = to_pcmcia_dev(dev); | |
1137 | struct pcmcia_driver *p_drv = NULL; | |
f6fbe01a | 1138 | int ret = 0; |
8e9e793d | 1139 | |
d6b4fa6d DB |
1140 | if (!p_dev->suspended) |
1141 | return 0; | |
1142 | ||
d50dbec3 | 1143 | dev_dbg(dev, "resuming\n"); |
d9d9ea01 | 1144 | |
8e9e793d DB |
1145 | if (dev->driver) |
1146 | p_drv = to_pcmcia_drv(dev->driver); | |
1147 | ||
e2d40963 DB |
1148 | if (!p_drv) |
1149 | goto out; | |
1150 | ||
1151 | if (p_dev->device_no == p_dev->func) { | |
d50dbec3 | 1152 | dev_dbg(dev, "requesting configuration\n"); |
e2d40963 DB |
1153 | ret = pcmcia_request_configuration(p_dev, &p_dev->conf); |
1154 | if (ret) | |
1155 | goto out; | |
8661bb5b | 1156 | } |
8e9e793d | 1157 | |
e2d40963 DB |
1158 | if (p_drv->resume) |
1159 | ret = p_drv->resume(p_dev); | |
1160 | ||
f6fbe01a DB |
1161 | out: |
1162 | if (!ret) | |
1163 | p_dev->suspended = 0; | |
1164 | return ret; | |
8e9e793d DB |
1165 | } |
1166 | ||
1167 | ||
1168 | static int pcmcia_bus_suspend_callback(struct device *dev, void * _data) | |
1169 | { | |
1170 | struct pcmcia_socket *skt = _data; | |
1171 | struct pcmcia_device *p_dev = to_pcmcia_dev(dev); | |
1172 | ||
3f8df781 | 1173 | if (p_dev->socket != skt || p_dev->suspended) |
8e9e793d DB |
1174 | return 0; |
1175 | ||
3f8df781 | 1176 | return runtime_suspend(dev); |
8e9e793d DB |
1177 | } |
1178 | ||
1179 | static int pcmcia_bus_resume_callback(struct device *dev, void * _data) | |
1180 | { | |
1181 | struct pcmcia_socket *skt = _data; | |
1182 | struct pcmcia_device *p_dev = to_pcmcia_dev(dev); | |
1183 | ||
3f8df781 | 1184 | if (p_dev->socket != skt || !p_dev->suspended) |
8e9e793d DB |
1185 | return 0; |
1186 | ||
3f8df781 | 1187 | runtime_resume(dev); |
8e9e793d DB |
1188 | |
1189 | return 0; | |
1190 | } | |
1191 | ||
1192 | static int pcmcia_bus_resume(struct pcmcia_socket *skt) | |
1193 | { | |
d50dbec3 | 1194 | dev_dbg(&skt->dev, "resuming socket %d\n", skt->sock); |
8e9e793d DB |
1195 | bus_for_each_dev(&pcmcia_bus_type, NULL, skt, pcmcia_bus_resume_callback); |
1196 | return 0; | |
1197 | } | |
1198 | ||
1199 | static int pcmcia_bus_suspend(struct pcmcia_socket *skt) | |
1200 | { | |
d50dbec3 | 1201 | dev_dbg(&skt->dev, "suspending socket %d\n", skt->sock); |
8e9e793d DB |
1202 | if (bus_for_each_dev(&pcmcia_bus_type, NULL, skt, |
1203 | pcmcia_bus_suspend_callback)) { | |
1204 | pcmcia_bus_resume(skt); | |
1205 | return -EIO; | |
1206 | } | |
1207 | return 0; | |
1208 | } | |
1209 | ||
1da177e4 | 1210 | |
1da177e4 LT |
1211 | /*====================================================================== |
1212 | ||
1213 | The card status event handler. | |
1214 | ||
1215 | ======================================================================*/ | |
1216 | ||
1da177e4 LT |
1217 | /* Normally, the event is passed to individual drivers after |
1218 | * informing userspace. Only for CS_EVENT_CARD_REMOVAL this | |
1219 | * is inversed to maintain historic compatibility. | |
1220 | */ | |
1221 | ||
1222 | static int ds_event(struct pcmcia_socket *skt, event_t event, int priority) | |
1223 | { | |
dc109497 | 1224 | struct pcmcia_socket *s = pcmcia_get_socket(skt); |
1da177e4 | 1225 | |
1617406a | 1226 | if (!s) { |
ac449d6e DB |
1227 | dev_printk(KERN_ERR, &skt->dev, |
1228 | "PCMCIA obtaining reference to socket " \ | |
1229 | "failed, event 0x%x lost!\n", event); | |
1617406a FM |
1230 | return -ENODEV; |
1231 | } | |
1232 | ||
d50dbec3 | 1233 | dev_dbg(&skt->dev, "ds_event(0x%06x, %d, 0x%p)\n", |
ac449d6e | 1234 | event, priority, skt); |
1da177e4 | 1235 | |
f8cfa618 | 1236 | switch (event) { |
1da177e4 | 1237 | case CS_EVENT_CARD_REMOVAL: |
b5e43913 | 1238 | s->pcmcia_state.present = 0; |
d6ff5a85 | 1239 | pcmcia_card_remove(skt, NULL); |
dc109497 | 1240 | handle_event(skt, event); |
1da177e4 | 1241 | break; |
f8cfa618 | 1242 | |
1da177e4 | 1243 | case CS_EVENT_CARD_INSERTION: |
b5e43913 | 1244 | s->pcmcia_state.present = 1; |
1da177e4 | 1245 | pcmcia_card_add(skt); |
dc109497 | 1246 | handle_event(skt, event); |
1da177e4 LT |
1247 | break; |
1248 | ||
1249 | case CS_EVENT_EJECTION_REQUEST: | |
1da177e4 LT |
1250 | break; |
1251 | ||
8e9e793d DB |
1252 | case CS_EVENT_PM_SUSPEND: |
1253 | case CS_EVENT_PM_RESUME: | |
1254 | case CS_EVENT_RESET_PHYSICAL: | |
1255 | case CS_EVENT_CARD_RESET: | |
1da177e4 | 1256 | default: |
dc109497 | 1257 | handle_event(skt, event); |
1da177e4 LT |
1258 | break; |
1259 | } | |
1260 | ||
dc109497 DB |
1261 | pcmcia_put_socket(s); |
1262 | ||
1da177e4 LT |
1263 | return 0; |
1264 | } /* ds_event */ | |
1265 | ||
1266 | ||
9940ec36 DB |
1267 | struct pcmcia_device * pcmcia_dev_present(struct pcmcia_device *_p_dev) |
1268 | { | |
1269 | struct pcmcia_device *p_dev; | |
1270 | struct pcmcia_device *ret = NULL; | |
1271 | ||
1272 | p_dev = pcmcia_get_dev(_p_dev); | |
1273 | if (!p_dev) | |
1274 | return NULL; | |
1275 | ||
1276 | if (!p_dev->socket->pcmcia_state.present) | |
1277 | goto out; | |
1278 | ||
1279 | if (p_dev->_removed) | |
1280 | goto out; | |
1281 | ||
1282 | if (p_dev->suspended) | |
1283 | goto out; | |
1284 | ||
1285 | ret = p_dev; | |
1286 | out: | |
1287 | pcmcia_put_dev(p_dev); | |
1288 | return ret; | |
1289 | } | |
1290 | EXPORT_SYMBOL(pcmcia_dev_present); | |
1291 | ||
1292 | ||
90c6cdd1 DB |
1293 | static struct pcmcia_callback pcmcia_bus_callback = { |
1294 | .owner = THIS_MODULE, | |
1295 | .event = ds_event, | |
1296 | .requery = pcmcia_bus_rescan, | |
8e9e793d DB |
1297 | .suspend = pcmcia_bus_suspend, |
1298 | .resume = pcmcia_bus_resume, | |
90c6cdd1 DB |
1299 | }; |
1300 | ||
87373318 | 1301 | static int __devinit pcmcia_bus_add_socket(struct device *dev, |
d8539d81 | 1302 | struct class_interface *class_intf) |
1da177e4 | 1303 | { |
87373318 | 1304 | struct pcmcia_socket *socket = dev_get_drvdata(dev); |
1da177e4 LT |
1305 | int ret; |
1306 | ||
dc109497 DB |
1307 | socket = pcmcia_get_socket(socket); |
1308 | if (!socket) { | |
ac449d6e DB |
1309 | dev_printk(KERN_ERR, dev, |
1310 | "PCMCIA obtaining reference to socket failed\n"); | |
1da177e4 LT |
1311 | return -ENODEV; |
1312 | } | |
1313 | ||
1da177e4 LT |
1314 | /* |
1315 | * Ugly. But we want to wait for the socket threads to have started up. | |
1316 | * We really should let the drivers themselves drive some of this.. | |
1317 | */ | |
1318 | msleep(250); | |
1319 | ||
e7a480d2 | 1320 | #ifdef CONFIG_PCMCIA_IOCTL |
dc109497 | 1321 | init_waitqueue_head(&socket->queue); |
e7a480d2 | 1322 | #endif |
dc109497 | 1323 | INIT_LIST_HEAD(&socket->devices_list); |
4796b71f | 1324 | INIT_WORK(&socket->device_add, pcmcia_delayed_add_device); |
dc109497 DB |
1325 | memset(&socket->pcmcia_state, 0, sizeof(u8)); |
1326 | socket->device_count = 0; | |
1da177e4 | 1327 | |
90c6cdd1 | 1328 | ret = pccard_register_pcmcia(socket, &pcmcia_bus_callback); |
1da177e4 | 1329 | if (ret) { |
ac449d6e | 1330 | dev_printk(KERN_ERR, dev, "PCMCIA registration failed\n"); |
dc109497 | 1331 | pcmcia_put_socket(socket); |
1da177e4 LT |
1332 | return (ret); |
1333 | } | |
1334 | ||
1335 | return 0; | |
1336 | } | |
1337 | ||
87373318 | 1338 | static void pcmcia_bus_remove_socket(struct device *dev, |
d8539d81 | 1339 | struct class_interface *class_intf) |
1da177e4 | 1340 | { |
87373318 | 1341 | struct pcmcia_socket *socket = dev_get_drvdata(dev); |
1da177e4 | 1342 | |
dc109497 | 1343 | if (!socket) |
1da177e4 LT |
1344 | return; |
1345 | ||
dc109497 | 1346 | socket->pcmcia_state.dead = 1; |
1da177e4 LT |
1347 | pccard_register_pcmcia(socket, NULL); |
1348 | ||
dfbc9e9d | 1349 | /* unregister any unbound devices */ |
8e4d9dcb | 1350 | mutex_lock(&socket->skt_mutex); |
dfbc9e9d | 1351 | pcmcia_card_remove(socket, NULL); |
8e4d9dcb | 1352 | mutex_unlock(&socket->skt_mutex); |
dfbc9e9d | 1353 | |
dc109497 | 1354 | pcmcia_put_socket(socket); |
1da177e4 LT |
1355 | |
1356 | return; | |
1357 | } | |
1358 | ||
1359 | ||
1360 | /* the pcmcia_bus_interface is used to handle pcmcia socket devices */ | |
ed49f5d0 | 1361 | static struct class_interface pcmcia_bus_interface __refdata = { |
1da177e4 | 1362 | .class = &pcmcia_socket_class, |
87373318 GKH |
1363 | .add_dev = &pcmcia_bus_add_socket, |
1364 | .remove_dev = &pcmcia_bus_remove_socket, | |
1da177e4 LT |
1365 | }; |
1366 | ||
1367 | ||
e7a480d2 | 1368 | struct bus_type pcmcia_bus_type = { |
1da177e4 | 1369 | .name = "pcmcia", |
312c004d | 1370 | .uevent = pcmcia_bus_uevent, |
1da177e4 LT |
1371 | .match = pcmcia_bus_match, |
1372 | .dev_attrs = pcmcia_dev_attrs, | |
1d0baa3a RK |
1373 | .probe = pcmcia_device_probe, |
1374 | .remove = pcmcia_device_remove, | |
8e9e793d DB |
1375 | .suspend = pcmcia_dev_suspend, |
1376 | .resume = pcmcia_dev_resume, | |
1da177e4 | 1377 | }; |
1da177e4 LT |
1378 | |
1379 | ||
1380 | static int __init init_pcmcia_bus(void) | |
1381 | { | |
ace7d477 RD |
1382 | int ret; |
1383 | ||
1da177e4 LT |
1384 | spin_lock_init(&pcmcia_dev_list_lock); |
1385 | ||
ace7d477 RD |
1386 | ret = bus_register(&pcmcia_bus_type); |
1387 | if (ret < 0) { | |
1388 | printk(KERN_WARNING "pcmcia: bus_register error: %d\n", ret); | |
1389 | return ret; | |
1390 | } | |
1391 | ret = class_interface_register(&pcmcia_bus_interface); | |
1392 | if (ret < 0) { | |
1393 | printk(KERN_WARNING | |
1394 | "pcmcia: class_interface_register error: %d\n", ret); | |
1395 | bus_unregister(&pcmcia_bus_type); | |
1396 | return ret; | |
1397 | } | |
1da177e4 | 1398 | |
e7a480d2 | 1399 | pcmcia_setup_ioctl(); |
1da177e4 LT |
1400 | |
1401 | return 0; | |
1402 | } | |
1403 | fs_initcall(init_pcmcia_bus); /* one level after subsys_initcall so that | |
1404 | * pcmcia_socket_class is already registered */ | |
1405 | ||
1406 | ||
1407 | static void __exit exit_pcmcia_bus(void) | |
1408 | { | |
e7a480d2 | 1409 | pcmcia_cleanup_ioctl(); |
1da177e4 | 1410 | |
e7a480d2 | 1411 | class_interface_unregister(&pcmcia_bus_interface); |
1da177e4 LT |
1412 | |
1413 | bus_unregister(&pcmcia_bus_type); | |
1414 | } | |
1415 | module_exit(exit_pcmcia_bus); | |
1416 | ||
1417 | ||
1da177e4 | 1418 | MODULE_ALIAS("ds"); |