pcmcia: protect s->device_count
[linux-2.6.git] / drivers / pcmcia / ds.c
blobbcb9ef103427c3850bc69db238d147166fdde1e2
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 unsigned long flags;
269 int ret = 0;
271 dev = get_device(dev);
272 if (!dev)
273 return -ENODEV;
275 p_dev = to_pcmcia_dev(dev);
276 p_drv = to_pcmcia_drv(dev->driver);
277 s = p_dev->socket;
279 /* The PCMCIA code passes the match data in via dev_set_drvdata(dev)
280 * which is an ugly hack. Once the driver probe is called it may
281 * and often will overwrite the match data so we must save it first
283 * handle pseudo multifunction devices:
284 * there are at most two pseudo multifunction devices.
285 * if we're matching against the first, schedule a
286 * call which will then check whether there are two
287 * pseudo devices, and if not, add the second one.
289 did = dev_get_drvdata(&p_dev->dev);
291 dev_dbg(dev, "trying to bind to %s\n", p_drv->drv.name);
293 if ((!p_drv->probe) || (!p_dev->function_config) ||
294 (!try_module_get(p_drv->owner))) {
295 ret = -EINVAL;
296 goto put_dev;
299 /* set up some more device information */
300 ret = pccard_read_tuple(p_dev->socket, p_dev->func, CISTPL_CONFIG,
301 &cis_config);
302 if (!ret) {
303 p_dev->conf.ConfigBase = cis_config.base;
304 p_dev->conf.Present = cis_config.rmask[0];
305 } else {
306 dev_printk(KERN_INFO, dev,
307 "pcmcia: could not parse base and rmask0 of CIS\n");
308 p_dev->conf.ConfigBase = 0;
309 p_dev->conf.Present = 0;
312 ret = p_drv->probe(p_dev);
313 if (ret) {
314 dev_dbg(dev, "binding to %s failed with %d\n",
315 p_drv->drv.name, ret);
316 goto put_module;
319 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
320 if (did && (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO) &&
321 (p_dev->socket->device_count == 1) && (p_dev->device_no == 0))
322 pcmcia_add_device_later(p_dev->socket, 0);
323 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
325 put_module:
326 if (ret)
327 module_put(p_drv->owner);
328 put_dev:
329 if (ret)
330 put_device(dev);
331 return ret;
336 * Removes a PCMCIA card from the device tree and socket list.
338 static void pcmcia_card_remove(struct pcmcia_socket *s, struct pcmcia_device *leftover)
340 struct pcmcia_device *p_dev;
341 struct pcmcia_device *tmp;
342 unsigned long flags;
344 dev_dbg(leftover ? &leftover->dev : &s->dev,
345 "pcmcia_card_remove(%d) %s\n", s->sock,
346 leftover ? leftover->devname : "");
348 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
349 if (!leftover)
350 s->device_count = 0;
351 else
352 s->device_count = 1;
353 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
355 /* unregister all pcmcia_devices registered with this socket, except leftover */
356 list_for_each_entry_safe(p_dev, tmp, &s->devices_list, socket_device_list) {
357 if (p_dev == leftover)
358 continue;
360 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
361 list_del(&p_dev->socket_device_list);
362 p_dev->_removed = 1;
363 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
365 dev_dbg(&p_dev->dev, "unregistering device\n");
366 device_unregister(&p_dev->dev);
369 return;
372 static int pcmcia_device_remove(struct device *dev)
374 struct pcmcia_device *p_dev;
375 struct pcmcia_driver *p_drv;
376 struct pcmcia_device_id *did;
377 int i;
379 p_dev = to_pcmcia_dev(dev);
380 p_drv = to_pcmcia_drv(dev->driver);
382 dev_dbg(dev, "removing device\n");
384 /* If we're removing the primary module driving a
385 * pseudo multi-function card, we need to unbind
386 * all devices
388 did = dev_get_drvdata(&p_dev->dev);
389 if (did && (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO) &&
390 (p_dev->socket->device_count > 0) &&
391 (p_dev->device_no == 0))
392 pcmcia_card_remove(p_dev->socket, p_dev);
394 /* detach the "instance" */
395 if (!p_drv)
396 return 0;
398 if (p_drv->remove)
399 p_drv->remove(p_dev);
401 p_dev->dev_node = NULL;
403 /* check for proper unloading */
404 if (p_dev->_irq || p_dev->_io || p_dev->_locked)
405 dev_printk(KERN_INFO, dev,
406 "pcmcia: driver %s did not release config properly\n",
407 p_drv->drv.name);
409 for (i = 0; i < MAX_WIN; i++)
410 if (p_dev->_win & CLIENT_WIN_REQ(i))
411 dev_printk(KERN_INFO, dev,
412 "pcmcia: driver %s did not release window properly\n",
413 p_drv->drv.name);
415 /* references from pcmcia_probe_device */
416 pcmcia_put_dev(p_dev);
417 module_put(p_drv->owner);
419 return 0;
424 * pcmcia_device_query -- determine information about a pcmcia device
426 static int pcmcia_device_query(struct pcmcia_device *p_dev)
428 cistpl_manfid_t manf_id;
429 cistpl_funcid_t func_id;
430 cistpl_vers_1_t *vers1;
431 unsigned int i;
433 vers1 = kmalloc(sizeof(*vers1), GFP_KERNEL);
434 if (!vers1)
435 return -ENOMEM;
437 if (!pccard_read_tuple(p_dev->socket, BIND_FN_ALL,
438 CISTPL_MANFID, &manf_id)) {
439 p_dev->manf_id = manf_id.manf;
440 p_dev->card_id = manf_id.card;
441 p_dev->has_manf_id = 1;
442 p_dev->has_card_id = 1;
445 if (!pccard_read_tuple(p_dev->socket, p_dev->func,
446 CISTPL_FUNCID, &func_id)) {
447 p_dev->func_id = func_id.func;
448 p_dev->has_func_id = 1;
449 } else {
450 /* rule of thumb: cards with no FUNCID, but with
451 * common memory device geometry information, are
452 * probably memory cards (from pcmcia-cs) */
453 cistpl_device_geo_t *devgeo;
455 devgeo = kmalloc(sizeof(*devgeo), GFP_KERNEL);
456 if (!devgeo) {
457 kfree(vers1);
458 return -ENOMEM;
460 if (!pccard_read_tuple(p_dev->socket, p_dev->func,
461 CISTPL_DEVICE_GEO, devgeo)) {
462 dev_dbg(&p_dev->dev,
463 "mem device geometry probably means "
464 "FUNCID_MEMORY\n");
465 p_dev->func_id = CISTPL_FUNCID_MEMORY;
466 p_dev->has_func_id = 1;
468 kfree(devgeo);
471 if (!pccard_read_tuple(p_dev->socket, BIND_FN_ALL, CISTPL_VERS_1,
472 vers1)) {
473 for (i = 0; i < min_t(unsigned int, 4, vers1->ns); i++) {
474 char *tmp;
475 unsigned int length;
477 tmp = vers1->str + vers1->ofs[i];
479 length = strlen(tmp) + 1;
480 if ((length < 2) || (length > 255))
481 continue;
483 p_dev->prod_id[i] = kmalloc(sizeof(char) * length,
484 GFP_KERNEL);
485 if (!p_dev->prod_id[i])
486 continue;
488 p_dev->prod_id[i] = strncpy(p_dev->prod_id[i],
489 tmp, length);
493 kfree(vers1);
494 return 0;
498 /* device_add_lock is needed to avoid double registration by cardmgr and kernel.
499 * Serializes pcmcia_device_add; will most likely be removed in future.
501 * While it has the caveat that adding new PCMCIA devices inside(!) device_register()
502 * won't work, this doesn't matter much at the moment: the driver core doesn't
503 * support it either.
505 static DEFINE_MUTEX(device_add_lock);
507 struct pcmcia_device *pcmcia_device_add(struct pcmcia_socket *s, unsigned int function)
509 struct pcmcia_device *p_dev, *tmp_dev;
510 unsigned long flags;
512 s = pcmcia_get_socket(s);
513 if (!s)
514 return NULL;
516 mutex_lock(&device_add_lock);
518 pr_debug("adding device to %d, function %d\n", s->sock, function);
520 p_dev = kzalloc(sizeof(struct pcmcia_device), GFP_KERNEL);
521 if (!p_dev)
522 goto err_put;
524 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
525 p_dev->device_no = (s->device_count++);
526 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
528 /* max of 4 devices per card */
529 if (p_dev->device_no >= 4)
530 goto err_free;
532 p_dev->socket = s;
533 p_dev->func = function;
535 p_dev->dev.bus = &pcmcia_bus_type;
536 p_dev->dev.parent = s->dev.parent;
537 p_dev->dev.release = pcmcia_release_dev;
538 /* by default don't allow DMA */
539 p_dev->dma_mask = DMA_MASK_NONE;
540 p_dev->dev.dma_mask = &p_dev->dma_mask;
541 dev_set_name(&p_dev->dev, "%d.%d", p_dev->socket->sock, p_dev->device_no);
542 if (!dev_name(&p_dev->dev))
543 goto err_free;
544 p_dev->devname = kasprintf(GFP_KERNEL, "pcmcia%s", dev_name(&p_dev->dev));
545 if (!p_dev->devname)
546 goto err_free;
547 dev_dbg(&p_dev->dev, "devname is %s\n", p_dev->devname);
549 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
552 * p_dev->function_config must be the same for all card functions.
553 * Note that this is serialized by the device_add_lock, so that
554 * only one such struct will be created.
556 list_for_each_entry(tmp_dev, &s->devices_list, socket_device_list)
557 if (p_dev->func == tmp_dev->func) {
558 p_dev->function_config = tmp_dev->function_config;
559 p_dev->io = tmp_dev->io;
560 p_dev->irq = tmp_dev->irq;
561 kref_get(&p_dev->function_config->ref);
564 /* Add to the list in pcmcia_bus_socket */
565 list_add(&p_dev->socket_device_list, &s->devices_list);
567 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
569 if (!p_dev->function_config) {
570 dev_dbg(&p_dev->dev, "creating config_t\n");
571 p_dev->function_config = kzalloc(sizeof(struct config_t),
572 GFP_KERNEL);
573 if (!p_dev->function_config)
574 goto err_unreg;
575 kref_init(&p_dev->function_config->ref);
578 dev_printk(KERN_NOTICE, &p_dev->dev,
579 "pcmcia: registering new device %s\n",
580 p_dev->devname);
582 pcmcia_device_query(p_dev);
584 if (device_register(&p_dev->dev))
585 goto err_unreg;
587 mutex_unlock(&device_add_lock);
589 return p_dev;
591 err_unreg:
592 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
593 list_del(&p_dev->socket_device_list);
594 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
596 err_free:
597 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
598 s->device_count--;
599 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
601 kfree(p_dev->devname);
602 kfree(p_dev);
603 err_put:
604 mutex_unlock(&device_add_lock);
605 pcmcia_put_socket(s);
607 return NULL;
611 static int pcmcia_card_add(struct pcmcia_socket *s)
613 cistpl_longlink_mfc_t mfc;
614 unsigned int no_funcs, i, no_chains;
615 int ret = 0;
617 if (!(s->resource_setup_done)) {
618 dev_dbg(&s->dev,
619 "no resources available, delaying card_add\n");
620 return -EAGAIN; /* try again, but later... */
623 if (pcmcia_validate_mem(s)) {
624 dev_dbg(&s->dev, "validating mem resources failed, "
625 "delaying card_add\n");
626 return -EAGAIN; /* try again, but later... */
629 ret = pccard_validate_cis(s, &no_chains);
630 if (ret || !no_chains) {
631 dev_dbg(&s->dev, "invalid CIS or invalid resources\n");
632 return -ENODEV;
635 if (!pccard_read_tuple(s, BIND_FN_ALL, CISTPL_LONGLINK_MFC, &mfc))
636 no_funcs = mfc.nfn;
637 else
638 no_funcs = 1;
639 s->functions = no_funcs;
641 for (i = 0; i < no_funcs; i++)
642 pcmcia_device_add(s, i);
644 return ret;
648 static void pcmcia_delayed_add_device(struct work_struct *work)
650 struct pcmcia_socket *s =
651 container_of(work, struct pcmcia_socket, device_add);
652 dev_dbg(&s->dev, "adding additional device to %d\n", s->sock);
653 pcmcia_device_add(s, s->pcmcia_state.mfc_pfc);
654 s->pcmcia_state.device_add_pending = 0;
655 s->pcmcia_state.mfc_pfc = 0;
658 static int pcmcia_requery(struct device *dev, void * _data)
660 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
661 if (!p_dev->dev.driver) {
662 dev_dbg(dev, "update device information\n");
663 pcmcia_device_query(p_dev);
666 return 0;
669 static void pcmcia_bus_rescan(struct pcmcia_socket *skt, int new_cis)
671 int no_devices = 0;
672 int ret = 0;
673 unsigned long flags;
675 /* must be called with skt_mutex held */
676 dev_dbg(&skt->dev, "re-scanning socket %d\n", skt->sock);
678 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
679 if (list_empty(&skt->devices_list))
680 no_devices = 1;
681 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
683 /* If this is because of a CIS override, start over */
684 if (new_cis && !no_devices)
685 pcmcia_card_remove(skt, NULL);
687 /* if no devices were added for this socket yet because of
688 * missing resource information or other trouble, we need to
689 * do this now. */
690 if (no_devices || new_cis) {
691 ret = pcmcia_card_add(skt);
692 if (ret)
693 return;
696 /* some device information might have changed because of a CIS
697 * update or because we can finally read it correctly... so
698 * determine it again, overwriting old values if necessary. */
699 bus_for_each_dev(&pcmcia_bus_type, NULL, NULL, pcmcia_requery);
701 /* we re-scan all devices, not just the ones connected to this
702 * socket. This does not matter, though. */
703 ret = bus_rescan_devices(&pcmcia_bus_type);
704 if (ret)
705 printk(KERN_INFO "pcmcia: bus_rescan_devices failed\n");
708 #ifdef CONFIG_PCMCIA_LOAD_CIS
711 * pcmcia_load_firmware - load CIS from userspace if device-provided is broken
712 * @dev: the pcmcia device which needs a CIS override
713 * @filename: requested filename in /lib/firmware/
715 * This uses the in-kernel firmware loading mechanism to use a "fake CIS" if
716 * the one provided by the card is broken. The firmware files reside in
717 * /lib/firmware/ in userspace.
719 static int pcmcia_load_firmware(struct pcmcia_device *dev, char * filename)
721 struct pcmcia_socket *s = dev->socket;
722 const struct firmware *fw;
723 int ret = -ENOMEM;
724 int no_funcs;
725 int old_funcs;
726 cistpl_longlink_mfc_t mfc;
728 if (!filename)
729 return -EINVAL;
731 dev_dbg(&dev->dev, "trying to load CIS file %s\n", filename);
733 if (request_firmware(&fw, filename, &dev->dev) == 0) {
734 if (fw->size >= CISTPL_MAX_CIS_SIZE) {
735 ret = -EINVAL;
736 dev_printk(KERN_ERR, &dev->dev,
737 "pcmcia: CIS override is too big\n");
738 goto release;
741 if (!pcmcia_replace_cis(s, fw->data, fw->size))
742 ret = 0;
743 else {
744 dev_printk(KERN_ERR, &dev->dev,
745 "pcmcia: CIS override failed\n");
746 goto release;
750 /* update information */
751 pcmcia_device_query(dev);
753 /* does this cis override add or remove functions? */
754 old_funcs = s->functions;
756 if (!pccard_read_tuple(s, BIND_FN_ALL, CISTPL_LONGLINK_MFC, &mfc))
757 no_funcs = mfc.nfn;
758 else
759 no_funcs = 1;
760 s->functions = no_funcs;
762 if (old_funcs > no_funcs)
763 pcmcia_card_remove(s, dev);
764 else if (no_funcs > old_funcs)
765 pcmcia_add_device_later(s, 1);
767 release:
768 release_firmware(fw);
770 return ret;
773 #else /* !CONFIG_PCMCIA_LOAD_CIS */
775 static inline int pcmcia_load_firmware(struct pcmcia_device *dev, char * filename)
777 return -ENODEV;
780 #endif
783 static inline int pcmcia_devmatch(struct pcmcia_device *dev,
784 struct pcmcia_device_id *did)
786 if (did->match_flags & PCMCIA_DEV_ID_MATCH_MANF_ID) {
787 if ((!dev->has_manf_id) || (dev->manf_id != did->manf_id))
788 return 0;
791 if (did->match_flags & PCMCIA_DEV_ID_MATCH_CARD_ID) {
792 if ((!dev->has_card_id) || (dev->card_id != did->card_id))
793 return 0;
796 if (did->match_flags & PCMCIA_DEV_ID_MATCH_FUNCTION) {
797 if (dev->func != did->function)
798 return 0;
801 if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID1) {
802 if (!dev->prod_id[0])
803 return 0;
804 if (strcmp(did->prod_id[0], dev->prod_id[0]))
805 return 0;
808 if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID2) {
809 if (!dev->prod_id[1])
810 return 0;
811 if (strcmp(did->prod_id[1], dev->prod_id[1]))
812 return 0;
815 if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID3) {
816 if (!dev->prod_id[2])
817 return 0;
818 if (strcmp(did->prod_id[2], dev->prod_id[2]))
819 return 0;
822 if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID4) {
823 if (!dev->prod_id[3])
824 return 0;
825 if (strcmp(did->prod_id[3], dev->prod_id[3]))
826 return 0;
829 if (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO) {
830 if (dev->device_no != did->device_no)
831 return 0;
834 if (did->match_flags & PCMCIA_DEV_ID_MATCH_FUNC_ID) {
835 if ((!dev->has_func_id) || (dev->func_id != did->func_id))
836 return 0;
838 /* if this is a pseudo-multi-function device,
839 * we need explicit matches */
840 if (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO)
841 return 0;
842 if (dev->device_no)
843 return 0;
845 /* also, FUNC_ID matching needs to be activated by userspace
846 * after it has re-checked that there is no possible module
847 * with a prod_id/manf_id/card_id match.
849 dev_dbg(&dev->dev,
850 "skipping FUNC_ID match until userspace interaction\n");
851 if (!dev->allow_func_id_match)
852 return 0;
855 if (did->match_flags & PCMCIA_DEV_ID_MATCH_FAKE_CIS) {
856 dev_dbg(&dev->dev, "device needs a fake CIS\n");
857 if (!dev->socket->fake_cis)
858 pcmcia_load_firmware(dev, did->cisfile);
860 if (!dev->socket->fake_cis)
861 return 0;
864 if (did->match_flags & PCMCIA_DEV_ID_MATCH_ANONYMOUS) {
865 int i;
866 for (i = 0; i < 4; i++)
867 if (dev->prod_id[i])
868 return 0;
869 if (dev->has_manf_id || dev->has_card_id || dev->has_func_id)
870 return 0;
873 dev_set_drvdata(&dev->dev, did);
875 return 1;
879 static int pcmcia_bus_match(struct device *dev, struct device_driver *drv)
881 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
882 struct pcmcia_driver *p_drv = to_pcmcia_drv(drv);
883 struct pcmcia_device_id *did = p_drv->id_table;
884 struct pcmcia_dynid *dynid;
886 /* match dynamic devices first */
887 spin_lock(&p_drv->dynids.lock);
888 list_for_each_entry(dynid, &p_drv->dynids.list, node) {
889 dev_dbg(dev, "trying to match to %s\n", drv->name);
890 if (pcmcia_devmatch(p_dev, &dynid->id)) {
891 dev_dbg(dev, "matched to %s\n", drv->name);
892 spin_unlock(&p_drv->dynids.lock);
893 return 1;
896 spin_unlock(&p_drv->dynids.lock);
898 #ifdef CONFIG_PCMCIA_IOCTL
899 /* matching by cardmgr */
900 if (p_dev->cardmgr == p_drv) {
901 dev_dbg(dev, "cardmgr matched to %s\n", drv->name);
902 return 1;
904 #endif
906 while (did && did->match_flags) {
907 dev_dbg(dev, "trying to match to %s\n", drv->name);
908 if (pcmcia_devmatch(p_dev, did)) {
909 dev_dbg(dev, "matched to %s\n", drv->name);
910 return 1;
912 did++;
915 return 0;
918 #ifdef CONFIG_HOTPLUG
920 static int pcmcia_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
922 struct pcmcia_device *p_dev;
923 int i;
924 u32 hash[4] = { 0, 0, 0, 0};
926 if (!dev)
927 return -ENODEV;
929 p_dev = to_pcmcia_dev(dev);
931 /* calculate hashes */
932 for (i = 0; i < 4; i++) {
933 if (!p_dev->prod_id[i])
934 continue;
935 hash[i] = crc32(0, p_dev->prod_id[i], strlen(p_dev->prod_id[i]));
938 if (add_uevent_var(env, "SOCKET_NO=%u", p_dev->socket->sock))
939 return -ENOMEM;
941 if (add_uevent_var(env, "DEVICE_NO=%02X", p_dev->device_no))
942 return -ENOMEM;
944 if (add_uevent_var(env, "MODALIAS=pcmcia:m%04Xc%04Xf%02Xfn%02Xpfn%02X"
945 "pa%08Xpb%08Xpc%08Xpd%08X",
946 p_dev->has_manf_id ? p_dev->manf_id : 0,
947 p_dev->has_card_id ? p_dev->card_id : 0,
948 p_dev->has_func_id ? p_dev->func_id : 0,
949 p_dev->func,
950 p_dev->device_no,
951 hash[0],
952 hash[1],
953 hash[2],
954 hash[3]))
955 return -ENOMEM;
957 return 0;
960 #else
962 static int pcmcia_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
964 return -ENODEV;
967 #endif
969 /************************ runtime PM support ***************************/
971 static int pcmcia_dev_suspend(struct device *dev, pm_message_t state);
972 static int pcmcia_dev_resume(struct device *dev);
974 static int runtime_suspend(struct device *dev)
976 int rc;
978 down(&dev->sem);
979 rc = pcmcia_dev_suspend(dev, PMSG_SUSPEND);
980 up(&dev->sem);
981 return rc;
984 static int runtime_resume(struct device *dev)
986 int rc;
988 down(&dev->sem);
989 rc = pcmcia_dev_resume(dev);
990 up(&dev->sem);
991 return rc;
994 /************************ per-device sysfs output ***************************/
996 #define pcmcia_device_attr(field, test, format) \
997 static ssize_t field##_show (struct device *dev, struct device_attribute *attr, char *buf) \
999 struct pcmcia_device *p_dev = to_pcmcia_dev(dev); \
1000 return p_dev->test ? sprintf(buf, format, p_dev->field) : -ENODEV; \
1003 #define pcmcia_device_stringattr(name, field) \
1004 static ssize_t name##_show (struct device *dev, struct device_attribute *attr, char *buf) \
1006 struct pcmcia_device *p_dev = to_pcmcia_dev(dev); \
1007 return p_dev->field ? sprintf(buf, "%s\n", p_dev->field) : -ENODEV; \
1010 pcmcia_device_attr(func, socket, "0x%02x\n");
1011 pcmcia_device_attr(func_id, has_func_id, "0x%02x\n");
1012 pcmcia_device_attr(manf_id, has_manf_id, "0x%04x\n");
1013 pcmcia_device_attr(card_id, has_card_id, "0x%04x\n");
1014 pcmcia_device_stringattr(prod_id1, prod_id[0]);
1015 pcmcia_device_stringattr(prod_id2, prod_id[1]);
1016 pcmcia_device_stringattr(prod_id3, prod_id[2]);
1017 pcmcia_device_stringattr(prod_id4, prod_id[3]);
1020 static ssize_t pcmcia_show_pm_state(struct device *dev, struct device_attribute *attr, char *buf)
1022 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1024 if (p_dev->suspended)
1025 return sprintf(buf, "off\n");
1026 else
1027 return sprintf(buf, "on\n");
1030 static ssize_t pcmcia_store_pm_state(struct device *dev, struct device_attribute *attr,
1031 const char *buf, size_t count)
1033 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1034 int ret = 0;
1036 if (!count)
1037 return -EINVAL;
1039 if ((!p_dev->suspended) && !strncmp(buf, "off", 3))
1040 ret = runtime_suspend(dev);
1041 else if (p_dev->suspended && !strncmp(buf, "on", 2))
1042 ret = runtime_resume(dev);
1044 return ret ? ret : count;
1048 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
1050 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1051 int i;
1052 u32 hash[4] = { 0, 0, 0, 0};
1054 /* calculate hashes */
1055 for (i = 0; i < 4; i++) {
1056 if (!p_dev->prod_id[i])
1057 continue;
1058 hash[i] = crc32(0, p_dev->prod_id[i],
1059 strlen(p_dev->prod_id[i]));
1061 return sprintf(buf, "pcmcia:m%04Xc%04Xf%02Xfn%02Xpfn%02X"
1062 "pa%08Xpb%08Xpc%08Xpd%08X\n",
1063 p_dev->has_manf_id ? p_dev->manf_id : 0,
1064 p_dev->has_card_id ? p_dev->card_id : 0,
1065 p_dev->has_func_id ? p_dev->func_id : 0,
1066 p_dev->func, p_dev->device_no,
1067 hash[0], hash[1], hash[2], hash[3]);
1070 static ssize_t pcmcia_store_allow_func_id_match(struct device *dev,
1071 struct device_attribute *attr, const char *buf, size_t count)
1073 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1074 int ret;
1076 if (!count)
1077 return -EINVAL;
1079 mutex_lock(&p_dev->socket->skt_mutex);
1080 p_dev->allow_func_id_match = 1;
1081 mutex_unlock(&p_dev->socket->skt_mutex);
1083 ret = bus_rescan_devices(&pcmcia_bus_type);
1084 if (ret)
1085 printk(KERN_INFO "pcmcia: bus_rescan_devices failed after "
1086 "allowing func_id matches\n");
1088 return count;
1091 static struct device_attribute pcmcia_dev_attrs[] = {
1092 __ATTR(function, 0444, func_show, NULL),
1093 __ATTR(pm_state, 0644, pcmcia_show_pm_state, pcmcia_store_pm_state),
1094 __ATTR_RO(func_id),
1095 __ATTR_RO(manf_id),
1096 __ATTR_RO(card_id),
1097 __ATTR_RO(prod_id1),
1098 __ATTR_RO(prod_id2),
1099 __ATTR_RO(prod_id3),
1100 __ATTR_RO(prod_id4),
1101 __ATTR_RO(modalias),
1102 __ATTR(allow_func_id_match, 0200, NULL, pcmcia_store_allow_func_id_match),
1103 __ATTR_NULL,
1106 /* PM support, also needed for reset */
1108 static int pcmcia_dev_suspend(struct device *dev, pm_message_t state)
1110 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1111 struct pcmcia_driver *p_drv = NULL;
1112 int ret = 0;
1114 if (p_dev->suspended)
1115 return 0;
1117 dev_dbg(dev, "suspending\n");
1119 if (dev->driver)
1120 p_drv = to_pcmcia_drv(dev->driver);
1122 if (!p_drv)
1123 goto out;
1125 if (p_drv->suspend) {
1126 ret = p_drv->suspend(p_dev);
1127 if (ret) {
1128 dev_printk(KERN_ERR, dev,
1129 "pcmcia: device %s (driver %s) did "
1130 "not want to go to sleep (%d)\n",
1131 p_dev->devname, p_drv->drv.name, ret);
1132 goto out;
1136 if (p_dev->device_no == p_dev->func) {
1137 dev_dbg(dev, "releasing configuration\n");
1138 pcmcia_release_configuration(p_dev);
1141 out:
1142 if (!ret)
1143 p_dev->suspended = 1;
1144 return ret;
1148 static int pcmcia_dev_resume(struct device *dev)
1150 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1151 struct pcmcia_driver *p_drv = NULL;
1152 int ret = 0;
1154 if (!p_dev->suspended)
1155 return 0;
1157 dev_dbg(dev, "resuming\n");
1159 if (dev->driver)
1160 p_drv = to_pcmcia_drv(dev->driver);
1162 if (!p_drv)
1163 goto out;
1165 if (p_dev->device_no == p_dev->func) {
1166 dev_dbg(dev, "requesting configuration\n");
1167 ret = pcmcia_request_configuration(p_dev, &p_dev->conf);
1168 if (ret)
1169 goto out;
1172 if (p_drv->resume)
1173 ret = p_drv->resume(p_dev);
1175 out:
1176 if (!ret)
1177 p_dev->suspended = 0;
1178 return ret;
1182 static int pcmcia_bus_suspend_callback(struct device *dev, void * _data)
1184 struct pcmcia_socket *skt = _data;
1185 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1187 if (p_dev->socket != skt || p_dev->suspended)
1188 return 0;
1190 return runtime_suspend(dev);
1193 static int pcmcia_bus_resume_callback(struct device *dev, void * _data)
1195 struct pcmcia_socket *skt = _data;
1196 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1198 if (p_dev->socket != skt || !p_dev->suspended)
1199 return 0;
1201 runtime_resume(dev);
1203 return 0;
1206 static int pcmcia_bus_resume(struct pcmcia_socket *skt)
1208 dev_dbg(&skt->dev, "resuming socket %d\n", skt->sock);
1209 bus_for_each_dev(&pcmcia_bus_type, NULL, skt, pcmcia_bus_resume_callback);
1210 return 0;
1213 static int pcmcia_bus_suspend(struct pcmcia_socket *skt)
1215 dev_dbg(&skt->dev, "suspending socket %d\n", skt->sock);
1216 if (bus_for_each_dev(&pcmcia_bus_type, NULL, skt,
1217 pcmcia_bus_suspend_callback)) {
1218 pcmcia_bus_resume(skt);
1219 return -EIO;
1221 return 0;
1225 /*======================================================================
1227 The card status event handler.
1229 ======================================================================*/
1231 /* Normally, the event is passed to individual drivers after
1232 * informing userspace. Only for CS_EVENT_CARD_REMOVAL this
1233 * is inversed to maintain historic compatibility.
1236 static int ds_event(struct pcmcia_socket *skt, event_t event, int priority)
1238 struct pcmcia_socket *s = pcmcia_get_socket(skt);
1240 if (!s) {
1241 dev_printk(KERN_ERR, &skt->dev,
1242 "PCMCIA obtaining reference to socket " \
1243 "failed, event 0x%x lost!\n", event);
1244 return -ENODEV;
1247 dev_dbg(&skt->dev, "ds_event(0x%06x, %d, 0x%p)\n",
1248 event, priority, skt);
1250 switch (event) {
1251 case CS_EVENT_CARD_REMOVAL:
1252 s->pcmcia_state.present = 0;
1253 pcmcia_card_remove(skt, NULL);
1254 handle_event(skt, event);
1255 mutex_lock(&s->ops_mutex);
1256 destroy_cis_cache(s);
1257 mutex_unlock(&s->ops_mutex);
1258 break;
1260 case CS_EVENT_CARD_INSERTION:
1261 s->pcmcia_state.present = 1;
1262 mutex_lock(&s->ops_mutex);
1263 destroy_cis_cache(s); /* to be on the safe side... */
1264 mutex_unlock(&s->ops_mutex);
1265 pcmcia_card_add(skt);
1266 handle_event(skt, event);
1267 break;
1269 case CS_EVENT_EJECTION_REQUEST:
1270 break;
1272 case CS_EVENT_PM_RESUME:
1273 if (verify_cis_cache(skt) != 0) {
1274 dev_dbg(&skt->dev, "cis mismatch - different card\n");
1275 /* first, remove the card */
1276 ds_event(skt, CS_EVENT_CARD_REMOVAL, CS_EVENT_PRI_HIGH);
1277 mutex_lock(&s->ops_mutex);
1278 destroy_cis_cache(skt);
1279 kfree(skt->fake_cis);
1280 skt->fake_cis = NULL;
1281 mutex_unlock(&s->ops_mutex);
1282 /* now, add the new card */
1283 ds_event(skt, CS_EVENT_CARD_INSERTION,
1284 CS_EVENT_PRI_LOW);
1286 handle_event(skt, event);
1287 break;
1289 case CS_EVENT_PM_SUSPEND:
1290 case CS_EVENT_RESET_PHYSICAL:
1291 case CS_EVENT_CARD_RESET:
1292 default:
1293 handle_event(skt, event);
1294 break;
1297 pcmcia_put_socket(s);
1299 return 0;
1300 } /* ds_event */
1303 struct pcmcia_device *pcmcia_dev_present(struct pcmcia_device *_p_dev)
1305 struct pcmcia_device *p_dev;
1306 struct pcmcia_device *ret = NULL;
1308 p_dev = pcmcia_get_dev(_p_dev);
1309 if (!p_dev)
1310 return NULL;
1312 if (!p_dev->socket->pcmcia_state.present)
1313 goto out;
1315 if (p_dev->_removed)
1316 goto out;
1318 if (p_dev->suspended)
1319 goto out;
1321 ret = p_dev;
1322 out:
1323 pcmcia_put_dev(p_dev);
1324 return ret;
1326 EXPORT_SYMBOL(pcmcia_dev_present);
1329 static struct pcmcia_callback pcmcia_bus_callback = {
1330 .owner = THIS_MODULE,
1331 .event = ds_event,
1332 .requery = pcmcia_bus_rescan,
1333 .validate = pccard_validate_cis,
1334 .suspend = pcmcia_bus_suspend,
1335 .resume = pcmcia_bus_resume,
1338 static int __devinit pcmcia_bus_add_socket(struct device *dev,
1339 struct class_interface *class_intf)
1341 struct pcmcia_socket *socket = dev_get_drvdata(dev);
1342 int ret;
1344 socket = pcmcia_get_socket(socket);
1345 if (!socket) {
1346 dev_printk(KERN_ERR, dev,
1347 "PCMCIA obtaining reference to socket failed\n");
1348 return -ENODEV;
1352 * Ugly. But we want to wait for the socket threads to have started up.
1353 * We really should let the drivers themselves drive some of this..
1355 msleep(250);
1357 ret = sysfs_create_bin_file(&dev->kobj, &pccard_cis_attr);
1358 if (ret) {
1359 dev_printk(KERN_ERR, dev, "PCMCIA registration failed\n");
1360 pcmcia_put_socket(socket);
1361 return ret;
1364 #ifdef CONFIG_PCMCIA_IOCTL
1365 init_waitqueue_head(&socket->queue);
1366 #endif
1367 INIT_LIST_HEAD(&socket->devices_list);
1368 INIT_WORK(&socket->device_add, pcmcia_delayed_add_device);
1369 memset(&socket->pcmcia_state, 0, sizeof(u8));
1370 socket->device_count = 0;
1372 ret = pccard_register_pcmcia(socket, &pcmcia_bus_callback);
1373 if (ret) {
1374 dev_printk(KERN_ERR, dev, "PCMCIA registration failed\n");
1375 pcmcia_put_socket(socket);
1376 return ret;
1379 return 0;
1382 static void pcmcia_bus_remove_socket(struct device *dev,
1383 struct class_interface *class_intf)
1385 struct pcmcia_socket *socket = dev_get_drvdata(dev);
1387 if (!socket)
1388 return;
1390 socket->pcmcia_state.dead = 1;
1391 pccard_register_pcmcia(socket, NULL);
1393 /* unregister any unbound devices */
1394 mutex_lock(&socket->skt_mutex);
1395 pcmcia_card_remove(socket, NULL);
1396 release_cis_mem(socket);
1397 mutex_unlock(&socket->skt_mutex);
1399 sysfs_remove_bin_file(&dev->kobj, &pccard_cis_attr);
1401 pcmcia_put_socket(socket);
1403 return;
1407 /* the pcmcia_bus_interface is used to handle pcmcia socket devices */
1408 static struct class_interface pcmcia_bus_interface __refdata = {
1409 .class = &pcmcia_socket_class,
1410 .add_dev = &pcmcia_bus_add_socket,
1411 .remove_dev = &pcmcia_bus_remove_socket,
1415 struct bus_type pcmcia_bus_type = {
1416 .name = "pcmcia",
1417 .uevent = pcmcia_bus_uevent,
1418 .match = pcmcia_bus_match,
1419 .dev_attrs = pcmcia_dev_attrs,
1420 .probe = pcmcia_device_probe,
1421 .remove = pcmcia_device_remove,
1422 .suspend = pcmcia_dev_suspend,
1423 .resume = pcmcia_dev_resume,
1427 static int __init init_pcmcia_bus(void)
1429 int ret;
1431 spin_lock_init(&pcmcia_dev_list_lock);
1433 ret = bus_register(&pcmcia_bus_type);
1434 if (ret < 0) {
1435 printk(KERN_WARNING "pcmcia: bus_register error: %d\n", ret);
1436 return ret;
1438 ret = class_interface_register(&pcmcia_bus_interface);
1439 if (ret < 0) {
1440 printk(KERN_WARNING
1441 "pcmcia: class_interface_register error: %d\n", ret);
1442 bus_unregister(&pcmcia_bus_type);
1443 return ret;
1446 pcmcia_setup_ioctl();
1448 return 0;
1450 fs_initcall(init_pcmcia_bus); /* one level after subsys_initcall so that
1451 * pcmcia_socket_class is already registered */
1454 static void __exit exit_pcmcia_bus(void)
1456 pcmcia_cleanup_ioctl();
1458 class_interface_unregister(&pcmcia_bus_interface);
1460 bus_unregister(&pcmcia_bus_type);
1462 module_exit(exit_pcmcia_bus);
1465 MODULE_ALIAS("ds");