iwlwifi: fix skb usage after free
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / pcmcia / ds.c
blob1a4a3c49cc15c71fe9db9ab07f61fd4cdff194ad
1 /*
2 * ds.c -- 16-bit PCMCIA core support
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.
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.
12 * (C) 1999 David A. Hinds
13 * (C) 2003 - 2006 Dominik Brodowski
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/errno.h>
20 #include <linux/list.h>
21 #include <linux/delay.h>
22 #include <linux/workqueue.h>
23 #include <linux/crc32.h>
24 #include <linux/firmware.h>
25 #include <linux/kref.h>
26 #include <linux/dma-mapping.h>
28 #include <pcmcia/cs_types.h>
29 #include <pcmcia/cs.h>
30 #include <pcmcia/cistpl.h>
31 #include <pcmcia/ds.h>
32 #include <pcmcia/ss.h>
34 #include "cs_internal.h"
36 /*====================================================================*/
38 /* Module parameters */
40 MODULE_AUTHOR("David Hinds <dahinds@users.sourceforge.net>");
41 MODULE_DESCRIPTION("PCMCIA Driver Services");
42 MODULE_LICENSE("GPL");
45 spinlock_t pcmcia_dev_list_lock;
47 /*====================================================================*/
49 static void pcmcia_check_driver(struct pcmcia_driver *p_drv)
51 struct pcmcia_device_id *did = p_drv->id_table;
52 unsigned int i;
53 u32 hash;
55 if (!p_drv->probe || !p_drv->remove)
56 printk(KERN_DEBUG "pcmcia: %s lacks a requisite callback "
57 "function\n", p_drv->drv.name);
59 while (did && did->match_flags) {
60 for (i = 0; i < 4; i++) {
61 if (!did->prod_id[i])
62 continue;
64 hash = crc32(0, did->prod_id[i], strlen(did->prod_id[i]));
65 if (hash == did->prod_id_hash[i])
66 continue;
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);
72 printk(KERN_DEBUG "pcmcia: see "
73 "Documentation/pcmcia/devicetable.txt for "
74 "details\n");
76 did++;
79 return;
83 /*======================================================================*/
86 struct pcmcia_dynid {
87 struct list_head node;
88 struct pcmcia_device_id id;
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
97 * Adds a new dynamic PCMCIA device ID to this driver,
98 * and causes the driver to probe for all devices again.
100 static ssize_t
101 pcmcia_store_new_id(struct device_driver *driver, const char *buf, size_t count)
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;
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;
117 dynid = kzalloc(sizeof(struct pcmcia_dynid), GFP_KERNEL);
118 if (!dynid)
119 return -ENOMEM;
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);
129 spin_lock(&pdrv->dynids.lock);
130 list_add_tail(&dynid->node, &pdrv->dynids.list);
131 spin_unlock(&pdrv->dynids.lock);
133 if (get_driver(&pdrv->drv)) {
134 retval = driver_attach(&pdrv->drv);
135 put_driver(&pdrv->drv);
138 if (retval)
139 return retval;
140 return count;
142 static DRIVER_ATTR(new_id, S_IWUSR, NULL, pcmcia_store_new_id);
144 static void
145 pcmcia_free_dynids(struct pcmcia_driver *drv)
147 struct pcmcia_dynid *dynid, *n;
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);
154 spin_unlock(&drv->dynids.lock);
157 static int
158 pcmcia_create_newid_file(struct pcmcia_driver *drv)
160 int error = 0;
161 if (drv->probe != NULL)
162 error = driver_create_file(&drv->drv, &driver_attr_new_id);
163 return error;
168 * pcmcia_register_driver - register a PCMCIA driver with the bus core
169 * @driver: the &driver being registered
171 * Registers a PCMCIA driver with the PCMCIA bus core.
173 int pcmcia_register_driver(struct pcmcia_driver *driver)
175 int error;
177 if (!driver)
178 return -EINVAL;
180 pcmcia_check_driver(driver);
182 /* initialize common fields */
183 driver->drv.bus = &pcmcia_bus_type;
184 driver->drv.owner = driver->owner;
185 spin_lock_init(&driver->dynids.lock);
186 INIT_LIST_HEAD(&driver->dynids.list);
188 pr_debug("registering driver %s\n", driver->drv.name);
190 error = driver_register(&driver->drv);
191 if (error < 0)
192 return error;
194 error = pcmcia_create_newid_file(driver);
195 if (error)
196 driver_unregister(&driver->drv);
198 return error;
200 EXPORT_SYMBOL(pcmcia_register_driver);
203 * pcmcia_unregister_driver - unregister a PCMCIA driver with the bus core
204 * @driver: the &driver being unregistered
206 void pcmcia_unregister_driver(struct pcmcia_driver *driver)
208 pr_debug("unregistering driver %s\n", driver->drv.name);
209 driver_unregister(&driver->drv);
210 pcmcia_free_dynids(driver);
212 EXPORT_SYMBOL(pcmcia_unregister_driver);
215 /* pcmcia_device handling */
217 struct pcmcia_device *pcmcia_get_dev(struct pcmcia_device *p_dev)
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);
226 void pcmcia_put_dev(struct pcmcia_device *p_dev)
228 if (p_dev)
229 put_device(&p_dev->dev);
232 static void pcmcia_release_function(struct kref *ref)
234 struct config_t *c = container_of(ref, struct config_t, ref);
235 pr_debug("releasing config_t\n");
236 kfree(c);
239 static void pcmcia_release_dev(struct device *dev)
241 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
242 dev_dbg(dev, "releasing device\n");
243 pcmcia_put_socket(p_dev->socket);
244 kfree(p_dev->devname);
245 kref_put(&p_dev->function_config->ref, pcmcia_release_function);
246 kfree(p_dev);
249 static void pcmcia_add_device_later(struct pcmcia_socket *s, int mfc)
251 if (!s->pcmcia_state.device_add_pending) {
252 dev_dbg(&s->dev, "scheduling to add %s secondary"
253 " device to %d\n", mfc ? "mfc" : "pfc", s->sock);
254 s->pcmcia_state.device_add_pending = 1;
255 s->pcmcia_state.mfc_pfc = mfc;
256 schedule_work(&s->device_add);
258 return;
261 static int pcmcia_device_probe(struct device *dev)
263 struct pcmcia_device *p_dev;
264 struct pcmcia_driver *p_drv;
265 struct pcmcia_device_id *did;
266 struct pcmcia_socket *s;
267 cistpl_config_t cis_config;
268 int ret = 0;
270 dev = get_device(dev);
271 if (!dev)
272 return -ENODEV;
274 p_dev = to_pcmcia_dev(dev);
275 p_drv = to_pcmcia_drv(dev->driver);
276 s = p_dev->socket;
278 /* The PCMCIA code passes the match data in via dev_set_drvdata(dev)
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
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.
288 did = dev_get_drvdata(&p_dev->dev);
290 dev_dbg(dev, "trying to bind to %s\n", p_drv->drv.name);
292 if ((!p_drv->probe) || (!p_dev->function_config) ||
293 (!try_module_get(p_drv->owner))) {
294 ret = -EINVAL;
295 goto put_dev;
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 {
305 dev_printk(KERN_INFO, dev,
306 "pcmcia: could not parse base and rmask0 of CIS\n");
307 p_dev->conf.ConfigBase = 0;
308 p_dev->conf.Present = 0;
311 ret = p_drv->probe(p_dev);
312 if (ret) {
313 dev_dbg(dev, "binding to %s failed with %d\n",
314 p_drv->drv.name, ret);
315 goto put_module;
318 if (did && (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO) &&
319 (p_dev->socket->device_count == 1) && (p_dev->device_no == 0))
320 pcmcia_add_device_later(p_dev->socket, 0);
322 put_module:
323 if (ret)
324 module_put(p_drv->owner);
325 put_dev:
326 if (ret)
327 put_device(dev);
328 return ret;
333 * Removes a PCMCIA card from the device tree and socket list.
335 static void pcmcia_card_remove(struct pcmcia_socket *s, struct pcmcia_device *leftover)
337 struct pcmcia_device *p_dev;
338 struct pcmcia_device *tmp;
339 unsigned long flags;
341 dev_dbg(leftover ? &leftover->dev : &s->dev,
342 "pcmcia_card_remove(%d) %s\n", s->sock,
343 leftover ? leftover->devname : "");
345 if (!leftover)
346 s->device_count = 0;
347 else
348 s->device_count = 1;
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;
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);
360 dev_dbg(&p_dev->dev, "unregistering device\n");
361 device_unregister(&p_dev->dev);
364 return;
367 static int pcmcia_device_remove(struct device *dev)
369 struct pcmcia_device *p_dev;
370 struct pcmcia_driver *p_drv;
371 struct pcmcia_device_id *did;
372 int i;
374 p_dev = to_pcmcia_dev(dev);
375 p_drv = to_pcmcia_drv(dev->driver);
377 dev_dbg(dev, "removing device\n");
379 /* If we're removing the primary module driving a
380 * pseudo multi-function card, we need to unbind
381 * all devices
383 did = dev_get_drvdata(&p_dev->dev);
384 if (did && (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO) &&
385 (p_dev->socket->device_count != 0) &&
386 (p_dev->device_no == 0))
387 pcmcia_card_remove(p_dev->socket, p_dev);
389 /* detach the "instance" */
390 if (!p_drv)
391 return 0;
393 if (p_drv->remove)
394 p_drv->remove(p_dev);
396 p_dev->dev_node = NULL;
398 /* check for proper unloading */
399 if (p_dev->_irq || p_dev->_io || p_dev->_locked)
400 dev_printk(KERN_INFO, dev,
401 "pcmcia: driver %s did not release config properly\n",
402 p_drv->drv.name);
404 for (i = 0; i < MAX_WIN; i++)
405 if (p_dev->_win & CLIENT_WIN_REQ(i))
406 dev_printk(KERN_INFO, dev,
407 "pcmcia: driver %s did not release window properly\n",
408 p_drv->drv.name);
410 /* references from pcmcia_probe_device */
411 pcmcia_put_dev(p_dev);
412 module_put(p_drv->owner);
414 return 0;
419 * pcmcia_device_query -- determine information about a pcmcia device
421 static int pcmcia_device_query(struct pcmcia_device *p_dev)
423 cistpl_manfid_t manf_id;
424 cistpl_funcid_t func_id;
425 cistpl_vers_1_t *vers1;
426 unsigned int i;
428 vers1 = kmalloc(sizeof(*vers1), GFP_KERNEL);
429 if (!vers1)
430 return -ENOMEM;
432 if (!pccard_read_tuple(p_dev->socket, BIND_FN_ALL,
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;
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) */
448 cistpl_device_geo_t *devgeo;
450 devgeo = kmalloc(sizeof(*devgeo), GFP_KERNEL);
451 if (!devgeo) {
452 kfree(vers1);
453 return -ENOMEM;
455 if (!pccard_read_tuple(p_dev->socket, p_dev->func,
456 CISTPL_DEVICE_GEO, devgeo)) {
457 dev_dbg(&p_dev->dev,
458 "mem device geometry probably means "
459 "FUNCID_MEMORY\n");
460 p_dev->func_id = CISTPL_FUNCID_MEMORY;
461 p_dev->has_func_id = 1;
463 kfree(devgeo);
466 if (!pccard_read_tuple(p_dev->socket, BIND_FN_ALL, CISTPL_VERS_1,
467 vers1)) {
468 for (i = 0; i < min_t(unsigned int, 4, vers1->ns); i++) {
469 char *tmp;
470 unsigned int length;
472 tmp = vers1->str + vers1->ofs[i];
474 length = strlen(tmp) + 1;
475 if ((length < 2) || (length > 255))
476 continue;
478 p_dev->prod_id[i] = kmalloc(sizeof(char) * length,
479 GFP_KERNEL);
480 if (!p_dev->prod_id[i])
481 continue;
483 p_dev->prod_id[i] = strncpy(p_dev->prod_id[i],
484 tmp, length);
488 kfree(vers1);
489 return 0;
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.
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.
500 static DEFINE_MUTEX(device_add_lock);
502 struct pcmcia_device *pcmcia_device_add(struct pcmcia_socket *s, unsigned int function)
504 struct pcmcia_device *p_dev, *tmp_dev;
505 unsigned long flags;
507 s = pcmcia_get_socket(s);
508 if (!s)
509 return NULL;
511 mutex_lock(&device_add_lock);
513 pr_debug("adding device to %d, function %d\n", s->sock, function);
515 /* max of 4 devices per card */
516 if (s->device_count == 4)
517 goto err_put;
519 p_dev = kzalloc(sizeof(struct pcmcia_device), GFP_KERNEL);
520 if (!p_dev)
521 goto err_put;
523 p_dev->socket = s;
524 p_dev->device_no = (s->device_count++);
525 p_dev->func = function;
527 p_dev->dev.bus = &pcmcia_bus_type;
528 p_dev->dev.parent = s->dev.parent;
529 p_dev->dev.release = pcmcia_release_dev;
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;
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));
537 if (!p_dev->devname)
538 goto err_free;
539 dev_dbg(&p_dev->dev, "devname is %s\n", p_dev->devname);
541 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
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.
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;
551 p_dev->io = tmp_dev->io;
552 p_dev->irq = tmp_dev->irq;
553 kref_get(&p_dev->function_config->ref);
556 /* Add to the list in pcmcia_bus_socket */
557 list_add(&p_dev->socket_device_list, &s->devices_list);
559 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
561 if (!p_dev->function_config) {
562 dev_dbg(&p_dev->dev, "creating config_t\n");
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);
570 dev_printk(KERN_NOTICE, &p_dev->dev,
571 "pcmcia: registering new device %s\n",
572 p_dev->devname);
574 pcmcia_device_query(p_dev);
576 if (device_register(&p_dev->dev))
577 goto err_unreg;
579 mutex_unlock(&device_add_lock);
581 return p_dev;
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);
588 err_free:
589 kfree(p_dev->devname);
590 kfree(p_dev);
591 s->device_count--;
592 err_put:
593 mutex_unlock(&device_add_lock);
594 pcmcia_put_socket(s);
596 return NULL;
600 static int pcmcia_card_add(struct pcmcia_socket *s)
602 cistpl_longlink_mfc_t mfc;
603 unsigned int no_funcs, i, no_chains;
604 int ret = 0;
606 if (!(s->resource_setup_done)) {
607 dev_dbg(&s->dev,
608 "no resources available, delaying card_add\n");
609 return -EAGAIN; /* try again, but later... */
612 if (pcmcia_validate_mem(s)) {
613 dev_dbg(&s->dev, "validating mem resources failed, "
614 "delaying card_add\n");
615 return -EAGAIN; /* try again, but later... */
618 ret = pccard_validate_cis(s, &no_chains);
619 if (ret || !no_chains) {
620 dev_dbg(&s->dev, "invalid CIS or invalid resources\n");
621 return -ENODEV;
624 if (!pccard_read_tuple(s, BIND_FN_ALL, CISTPL_LONGLINK_MFC, &mfc))
625 no_funcs = mfc.nfn;
626 else
627 no_funcs = 1;
628 s->functions = no_funcs;
630 for (i = 0; i < no_funcs; i++)
631 pcmcia_device_add(s, i);
633 return ret;
637 static void pcmcia_delayed_add_device(struct work_struct *work)
639 struct pcmcia_socket *s =
640 container_of(work, struct pcmcia_socket, device_add);
641 dev_dbg(&s->dev, "adding additional device to %d\n", s->sock);
642 pcmcia_device_add(s, s->pcmcia_state.mfc_pfc);
643 s->pcmcia_state.device_add_pending = 0;
644 s->pcmcia_state.mfc_pfc = 0;
647 static int pcmcia_requery(struct device *dev, void * _data)
649 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
650 if (!p_dev->dev.driver) {
651 dev_dbg(dev, "update device information\n");
652 pcmcia_device_query(p_dev);
655 return 0;
658 static void pcmcia_bus_rescan(struct pcmcia_socket *skt, int new_cis)
660 int no_devices = 0;
661 int ret = 0;
662 unsigned long flags;
664 /* must be called with skt_mutex held */
665 dev_dbg(&skt->dev, "re-scanning socket %d\n", skt->sock);
667 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
668 if (list_empty(&skt->devices_list))
669 no_devices = 1;
670 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
672 /* If this is because of a CIS override, start over */
673 if (new_cis && !no_devices)
674 pcmcia_card_remove(skt, NULL);
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. */
679 if (no_devices || new_cis) {
680 ret = pcmcia_card_add(skt);
681 if (ret)
682 return;
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);
690 /* we re-scan all devices, not just the ones connected to this
691 * socket. This does not matter, though. */
692 ret = bus_rescan_devices(&pcmcia_bus_type);
693 if (ret)
694 printk(KERN_INFO "pcmcia: bus_rescan_devices failed\n");
697 #ifdef CONFIG_PCMCIA_LOAD_CIS
700 * pcmcia_load_firmware - load CIS from userspace if device-provided is broken
701 * @dev: the pcmcia device which needs a CIS override
702 * @filename: requested filename in /lib/firmware/
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.
708 static int pcmcia_load_firmware(struct pcmcia_device *dev, char * filename)
710 struct pcmcia_socket *s = dev->socket;
711 const struct firmware *fw;
712 int ret = -ENOMEM;
713 int no_funcs;
714 int old_funcs;
715 cistpl_longlink_mfc_t mfc;
717 if (!filename)
718 return -EINVAL;
720 dev_dbg(&dev->dev, "trying to load CIS file %s\n", filename);
722 if (request_firmware(&fw, filename, &dev->dev) == 0) {
723 if (fw->size >= CISTPL_MAX_CIS_SIZE) {
724 ret = -EINVAL;
725 dev_printk(KERN_ERR, &dev->dev,
726 "pcmcia: CIS override is too big\n");
727 goto release;
730 if (!pcmcia_replace_cis(s, fw->data, fw->size))
731 ret = 0;
732 else {
733 dev_printk(KERN_ERR, &dev->dev,
734 "pcmcia: CIS override failed\n");
735 goto release;
739 /* update information */
740 pcmcia_device_query(dev);
742 /* does this cis override add or remove functions? */
743 old_funcs = s->functions;
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;
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);
756 release:
757 release_firmware(fw);
759 return ret;
762 #else /* !CONFIG_PCMCIA_LOAD_CIS */
764 static inline int pcmcia_load_firmware(struct pcmcia_device *dev, char * filename)
766 return -ENODEV;
769 #endif
772 static inline int pcmcia_devmatch(struct pcmcia_device *dev,
773 struct pcmcia_device_id *did)
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;
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;
785 if (did->match_flags & PCMCIA_DEV_ID_MATCH_FUNCTION) {
786 if (dev->func != did->function)
787 return 0;
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;
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;
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;
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;
818 if (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO) {
819 if (dev->device_no != did->device_no)
820 return 0;
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;
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;
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.
838 dev_dbg(&dev->dev,
839 "skipping FUNC_ID match until userspace interaction\n");
840 if (!dev->allow_func_id_match)
841 return 0;
844 if (did->match_flags & PCMCIA_DEV_ID_MATCH_FAKE_CIS) {
845 dev_dbg(&dev->dev, "device needs a fake CIS\n");
846 if (!dev->socket->fake_cis)
847 pcmcia_load_firmware(dev, did->cisfile);
849 if (!dev->socket->fake_cis)
850 return 0;
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;
862 dev_set_drvdata(&dev->dev, did);
864 return 1;
868 static int pcmcia_bus_match(struct device *dev, struct device_driver *drv)
870 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
871 struct pcmcia_driver *p_drv = to_pcmcia_drv(drv);
872 struct pcmcia_device_id *did = p_drv->id_table;
873 struct pcmcia_dynid *dynid;
875 /* match dynamic devices first */
876 spin_lock(&p_drv->dynids.lock);
877 list_for_each_entry(dynid, &p_drv->dynids.list, node) {
878 dev_dbg(dev, "trying to match to %s\n", drv->name);
879 if (pcmcia_devmatch(p_dev, &dynid->id)) {
880 dev_dbg(dev, "matched to %s\n", drv->name);
881 spin_unlock(&p_drv->dynids.lock);
882 return 1;
885 spin_unlock(&p_drv->dynids.lock);
887 #ifdef CONFIG_PCMCIA_IOCTL
888 /* matching by cardmgr */
889 if (p_dev->cardmgr == p_drv) {
890 dev_dbg(dev, "cardmgr matched to %s\n", drv->name);
891 return 1;
893 #endif
895 while (did && did->match_flags) {
896 dev_dbg(dev, "trying to match to %s\n", drv->name);
897 if (pcmcia_devmatch(p_dev, did)) {
898 dev_dbg(dev, "matched to %s\n", drv->name);
899 return 1;
901 did++;
904 return 0;
907 #ifdef CONFIG_HOTPLUG
909 static int pcmcia_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
911 struct pcmcia_device *p_dev;
912 int i;
913 u32 hash[4] = { 0, 0, 0, 0};
915 if (!dev)
916 return -ENODEV;
918 p_dev = to_pcmcia_dev(dev);
920 /* calculate hashes */
921 for (i = 0; i < 4; i++) {
922 if (!p_dev->prod_id[i])
923 continue;
924 hash[i] = crc32(0, p_dev->prod_id[i], strlen(p_dev->prod_id[i]));
927 if (add_uevent_var(env, "SOCKET_NO=%u", p_dev->socket->sock))
928 return -ENOMEM;
930 if (add_uevent_var(env, "DEVICE_NO=%02X", p_dev->device_no))
931 return -ENOMEM;
933 if (add_uevent_var(env, "MODALIAS=pcmcia:m%04Xc%04Xf%02Xfn%02Xpfn%02X"
934 "pa%08Xpb%08Xpc%08Xpd%08X",
935 p_dev->has_manf_id ? p_dev->manf_id : 0,
936 p_dev->has_card_id ? p_dev->card_id : 0,
937 p_dev->has_func_id ? p_dev->func_id : 0,
938 p_dev->func,
939 p_dev->device_no,
940 hash[0],
941 hash[1],
942 hash[2],
943 hash[3]))
944 return -ENOMEM;
946 return 0;
949 #else
951 static int pcmcia_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
953 return -ENODEV;
956 #endif
958 /************************ runtime PM support ***************************/
960 static int pcmcia_dev_suspend(struct device *dev, pm_message_t state);
961 static int pcmcia_dev_resume(struct device *dev);
963 static int runtime_suspend(struct device *dev)
965 int rc;
967 down(&dev->sem);
968 rc = pcmcia_dev_suspend(dev, PMSG_SUSPEND);
969 up(&dev->sem);
970 return rc;
973 static void runtime_resume(struct device *dev)
975 int rc;
977 down(&dev->sem);
978 rc = pcmcia_dev_resume(dev);
979 up(&dev->sem);
982 /************************ per-device sysfs output ***************************/
984 #define pcmcia_device_attr(field, test, format) \
985 static ssize_t field##_show (struct device *dev, struct device_attribute *attr, char *buf) \
987 struct pcmcia_device *p_dev = to_pcmcia_dev(dev); \
988 return p_dev->test ? sprintf(buf, format, p_dev->field) : -ENODEV; \
991 #define pcmcia_device_stringattr(name, field) \
992 static ssize_t name##_show (struct device *dev, struct device_attribute *attr, char *buf) \
994 struct pcmcia_device *p_dev = to_pcmcia_dev(dev); \
995 return p_dev->field ? sprintf(buf, "%s\n", p_dev->field) : -ENODEV; \
998 pcmcia_device_attr(func, socket, "0x%02x\n");
999 pcmcia_device_attr(func_id, has_func_id, "0x%02x\n");
1000 pcmcia_device_attr(manf_id, has_manf_id, "0x%04x\n");
1001 pcmcia_device_attr(card_id, has_card_id, "0x%04x\n");
1002 pcmcia_device_stringattr(prod_id1, prod_id[0]);
1003 pcmcia_device_stringattr(prod_id2, prod_id[1]);
1004 pcmcia_device_stringattr(prod_id3, prod_id[2]);
1005 pcmcia_device_stringattr(prod_id4, prod_id[3]);
1008 static ssize_t pcmcia_show_pm_state(struct device *dev, struct device_attribute *attr, char *buf)
1010 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1012 if (p_dev->suspended)
1013 return sprintf(buf, "off\n");
1014 else
1015 return sprintf(buf, "on\n");
1018 static ssize_t pcmcia_store_pm_state(struct device *dev, struct device_attribute *attr,
1019 const char *buf, size_t count)
1021 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1022 int ret = 0;
1024 if (!count)
1025 return -EINVAL;
1027 if ((!p_dev->suspended) && !strncmp(buf, "off", 3))
1028 ret = runtime_suspend(dev);
1029 else if (p_dev->suspended && !strncmp(buf, "on", 2))
1030 runtime_resume(dev);
1032 return ret ? ret : count;
1036 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
1038 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1039 int i;
1040 u32 hash[4] = { 0, 0, 0, 0};
1042 /* calculate hashes */
1043 for (i = 0; i < 4; i++) {
1044 if (!p_dev->prod_id[i])
1045 continue;
1046 hash[i] = crc32(0, p_dev->prod_id[i],
1047 strlen(p_dev->prod_id[i]));
1049 return sprintf(buf, "pcmcia:m%04Xc%04Xf%02Xfn%02Xpfn%02X"
1050 "pa%08Xpb%08Xpc%08Xpd%08X\n",
1051 p_dev->has_manf_id ? p_dev->manf_id : 0,
1052 p_dev->has_card_id ? p_dev->card_id : 0,
1053 p_dev->has_func_id ? p_dev->func_id : 0,
1054 p_dev->func, p_dev->device_no,
1055 hash[0], hash[1], hash[2], hash[3]);
1058 static ssize_t pcmcia_store_allow_func_id_match(struct device *dev,
1059 struct device_attribute *attr, const char *buf, size_t count)
1061 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1062 int ret;
1064 if (!count)
1065 return -EINVAL;
1067 mutex_lock(&p_dev->socket->skt_mutex);
1068 p_dev->allow_func_id_match = 1;
1069 mutex_unlock(&p_dev->socket->skt_mutex);
1071 ret = bus_rescan_devices(&pcmcia_bus_type);
1072 if (ret)
1073 printk(KERN_INFO "pcmcia: bus_rescan_devices failed after "
1074 "allowing func_id matches\n");
1076 return count;
1079 static struct device_attribute pcmcia_dev_attrs[] = {
1080 __ATTR(function, 0444, func_show, NULL),
1081 __ATTR(pm_state, 0644, pcmcia_show_pm_state, pcmcia_store_pm_state),
1082 __ATTR_RO(func_id),
1083 __ATTR_RO(manf_id),
1084 __ATTR_RO(card_id),
1085 __ATTR_RO(prod_id1),
1086 __ATTR_RO(prod_id2),
1087 __ATTR_RO(prod_id3),
1088 __ATTR_RO(prod_id4),
1089 __ATTR_RO(modalias),
1090 __ATTR(allow_func_id_match, 0200, NULL, pcmcia_store_allow_func_id_match),
1091 __ATTR_NULL,
1094 /* PM support, also needed for reset */
1096 static int pcmcia_dev_suspend(struct device *dev, pm_message_t state)
1098 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1099 struct pcmcia_driver *p_drv = NULL;
1100 int ret = 0;
1102 if (p_dev->suspended)
1103 return 0;
1105 dev_dbg(dev, "suspending\n");
1107 if (dev->driver)
1108 p_drv = to_pcmcia_drv(dev->driver);
1110 if (!p_drv)
1111 goto out;
1113 if (p_drv->suspend) {
1114 ret = p_drv->suspend(p_dev);
1115 if (ret) {
1116 dev_printk(KERN_ERR, dev,
1117 "pcmcia: device %s (driver %s) did "
1118 "not want to go to sleep (%d)\n",
1119 p_dev->devname, p_drv->drv.name, ret);
1120 goto out;
1124 if (p_dev->device_no == p_dev->func) {
1125 dev_dbg(dev, "releasing configuration\n");
1126 pcmcia_release_configuration(p_dev);
1129 out:
1130 if (!ret)
1131 p_dev->suspended = 1;
1132 return ret;
1136 static int pcmcia_dev_resume(struct device *dev)
1138 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1139 struct pcmcia_driver *p_drv = NULL;
1140 int ret = 0;
1142 if (!p_dev->suspended)
1143 return 0;
1145 dev_dbg(dev, "resuming\n");
1147 if (dev->driver)
1148 p_drv = to_pcmcia_drv(dev->driver);
1150 if (!p_drv)
1151 goto out;
1153 if (p_dev->device_no == p_dev->func) {
1154 dev_dbg(dev, "requesting configuration\n");
1155 ret = pcmcia_request_configuration(p_dev, &p_dev->conf);
1156 if (ret)
1157 goto out;
1160 if (p_drv->resume)
1161 ret = p_drv->resume(p_dev);
1163 out:
1164 if (!ret)
1165 p_dev->suspended = 0;
1166 return ret;
1170 static int pcmcia_bus_suspend_callback(struct device *dev, void * _data)
1172 struct pcmcia_socket *skt = _data;
1173 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1175 if (p_dev->socket != skt || p_dev->suspended)
1176 return 0;
1178 return runtime_suspend(dev);
1181 static int pcmcia_bus_resume_callback(struct device *dev, void * _data)
1183 struct pcmcia_socket *skt = _data;
1184 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1186 if (p_dev->socket != skt || !p_dev->suspended)
1187 return 0;
1189 runtime_resume(dev);
1191 return 0;
1194 static int pcmcia_bus_resume(struct pcmcia_socket *skt)
1196 dev_dbg(&skt->dev, "resuming socket %d\n", skt->sock);
1197 bus_for_each_dev(&pcmcia_bus_type, NULL, skt, pcmcia_bus_resume_callback);
1198 return 0;
1201 static int pcmcia_bus_suspend(struct pcmcia_socket *skt)
1203 dev_dbg(&skt->dev, "suspending socket %d\n", skt->sock);
1204 if (bus_for_each_dev(&pcmcia_bus_type, NULL, skt,
1205 pcmcia_bus_suspend_callback)) {
1206 pcmcia_bus_resume(skt);
1207 return -EIO;
1209 return 0;
1213 /*======================================================================
1215 The card status event handler.
1217 ======================================================================*/
1219 /* Normally, the event is passed to individual drivers after
1220 * informing userspace. Only for CS_EVENT_CARD_REMOVAL this
1221 * is inversed to maintain historic compatibility.
1224 static int ds_event(struct pcmcia_socket *skt, event_t event, int priority)
1226 struct pcmcia_socket *s = pcmcia_get_socket(skt);
1228 if (!s) {
1229 dev_printk(KERN_ERR, &skt->dev,
1230 "PCMCIA obtaining reference to socket " \
1231 "failed, event 0x%x lost!\n", event);
1232 return -ENODEV;
1235 dev_dbg(&skt->dev, "ds_event(0x%06x, %d, 0x%p)\n",
1236 event, priority, skt);
1238 switch (event) {
1239 case CS_EVENT_CARD_REMOVAL:
1240 s->pcmcia_state.present = 0;
1241 pcmcia_card_remove(skt, NULL);
1242 handle_event(skt, event);
1243 break;
1245 case CS_EVENT_CARD_INSERTION:
1246 s->pcmcia_state.present = 1;
1247 pcmcia_card_add(skt);
1248 handle_event(skt, event);
1249 break;
1251 case CS_EVENT_EJECTION_REQUEST:
1252 break;
1254 case CS_EVENT_PM_SUSPEND:
1255 case CS_EVENT_PM_RESUME:
1256 case CS_EVENT_RESET_PHYSICAL:
1257 case CS_EVENT_CARD_RESET:
1258 default:
1259 handle_event(skt, event);
1260 break;
1263 pcmcia_put_socket(s);
1265 return 0;
1266 } /* ds_event */
1269 struct pcmcia_device *pcmcia_dev_present(struct pcmcia_device *_p_dev)
1271 struct pcmcia_device *p_dev;
1272 struct pcmcia_device *ret = NULL;
1274 p_dev = pcmcia_get_dev(_p_dev);
1275 if (!p_dev)
1276 return NULL;
1278 if (!p_dev->socket->pcmcia_state.present)
1279 goto out;
1281 if (p_dev->_removed)
1282 goto out;
1284 if (p_dev->suspended)
1285 goto out;
1287 ret = p_dev;
1288 out:
1289 pcmcia_put_dev(p_dev);
1290 return ret;
1292 EXPORT_SYMBOL(pcmcia_dev_present);
1295 static struct pcmcia_callback pcmcia_bus_callback = {
1296 .owner = THIS_MODULE,
1297 .event = ds_event,
1298 .requery = pcmcia_bus_rescan,
1299 .suspend = pcmcia_bus_suspend,
1300 .resume = pcmcia_bus_resume,
1303 static int __devinit pcmcia_bus_add_socket(struct device *dev,
1304 struct class_interface *class_intf)
1306 struct pcmcia_socket *socket = dev_get_drvdata(dev);
1307 int ret;
1309 socket = pcmcia_get_socket(socket);
1310 if (!socket) {
1311 dev_printk(KERN_ERR, dev,
1312 "PCMCIA obtaining reference to socket failed\n");
1313 return -ENODEV;
1317 * Ugly. But we want to wait for the socket threads to have started up.
1318 * We really should let the drivers themselves drive some of this..
1320 msleep(250);
1322 #ifdef CONFIG_PCMCIA_IOCTL
1323 init_waitqueue_head(&socket->queue);
1324 #endif
1325 INIT_LIST_HEAD(&socket->devices_list);
1326 INIT_WORK(&socket->device_add, pcmcia_delayed_add_device);
1327 memset(&socket->pcmcia_state, 0, sizeof(u8));
1328 socket->device_count = 0;
1330 ret = pccard_register_pcmcia(socket, &pcmcia_bus_callback);
1331 if (ret) {
1332 dev_printk(KERN_ERR, dev, "PCMCIA registration failed\n");
1333 pcmcia_put_socket(socket);
1334 return ret;
1337 return 0;
1340 static void pcmcia_bus_remove_socket(struct device *dev,
1341 struct class_interface *class_intf)
1343 struct pcmcia_socket *socket = dev_get_drvdata(dev);
1345 if (!socket)
1346 return;
1348 socket->pcmcia_state.dead = 1;
1349 pccard_register_pcmcia(socket, NULL);
1351 /* unregister any unbound devices */
1352 mutex_lock(&socket->skt_mutex);
1353 pcmcia_card_remove(socket, NULL);
1354 mutex_unlock(&socket->skt_mutex);
1356 pcmcia_put_socket(socket);
1358 return;
1362 /* the pcmcia_bus_interface is used to handle pcmcia socket devices */
1363 static struct class_interface pcmcia_bus_interface __refdata = {
1364 .class = &pcmcia_socket_class,
1365 .add_dev = &pcmcia_bus_add_socket,
1366 .remove_dev = &pcmcia_bus_remove_socket,
1370 struct bus_type pcmcia_bus_type = {
1371 .name = "pcmcia",
1372 .uevent = pcmcia_bus_uevent,
1373 .match = pcmcia_bus_match,
1374 .dev_attrs = pcmcia_dev_attrs,
1375 .probe = pcmcia_device_probe,
1376 .remove = pcmcia_device_remove,
1377 .suspend = pcmcia_dev_suspend,
1378 .resume = pcmcia_dev_resume,
1382 static int __init init_pcmcia_bus(void)
1384 int ret;
1386 spin_lock_init(&pcmcia_dev_list_lock);
1388 ret = bus_register(&pcmcia_bus_type);
1389 if (ret < 0) {
1390 printk(KERN_WARNING "pcmcia: bus_register error: %d\n", ret);
1391 return ret;
1393 ret = class_interface_register(&pcmcia_bus_interface);
1394 if (ret < 0) {
1395 printk(KERN_WARNING
1396 "pcmcia: class_interface_register error: %d\n", ret);
1397 bus_unregister(&pcmcia_bus_type);
1398 return ret;
1401 pcmcia_setup_ioctl();
1403 return 0;
1405 fs_initcall(init_pcmcia_bus); /* one level after subsys_initcall so that
1406 * pcmcia_socket_class is already registered */
1409 static void __exit exit_pcmcia_bus(void)
1411 pcmcia_cleanup_ioctl();
1413 class_interface_unregister(&pcmcia_bus_interface);
1415 bus_unregister(&pcmcia_bus_type);
1417 module_exit(exit_pcmcia_bus);
1420 MODULE_ALIAS("ds");