Driver core: create lock/unlock functions for struct device
[linux-2.6.git] / drivers / pcmcia / ds.c
blobad93ebd7b2a2f8d3e5cdd65b07bff388d49e3db2
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 /*====================================================================*/
47 static void pcmcia_check_driver(struct pcmcia_driver *p_drv)
49 struct pcmcia_device_id *did = p_drv->id_table;
50 unsigned int i;
51 u32 hash;
53 if (!p_drv->probe || !p_drv->remove)
54 printk(KERN_DEBUG "pcmcia: %s lacks a requisite callback "
55 "function\n", p_drv->drv.name);
57 while (did && did->match_flags) {
58 for (i = 0; i < 4; i++) {
59 if (!did->prod_id[i])
60 continue;
62 hash = crc32(0, did->prod_id[i], strlen(did->prod_id[i]));
63 if (hash == did->prod_id_hash[i])
64 continue;
66 printk(KERN_DEBUG "pcmcia: %s: invalid hash for "
67 "product string \"%s\": is 0x%x, should "
68 "be 0x%x\n", p_drv->drv.name, did->prod_id[i],
69 did->prod_id_hash[i], hash);
70 printk(KERN_DEBUG "pcmcia: see "
71 "Documentation/pcmcia/devicetable.txt for "
72 "details\n");
74 did++;
77 return;
81 /*======================================================================*/
84 struct pcmcia_dynid {
85 struct list_head node;
86 struct pcmcia_device_id id;
89 /**
90 * pcmcia_store_new_id - add a new PCMCIA device ID to this driver and re-probe devices
91 * @driver: target device driver
92 * @buf: buffer for scanning device ID data
93 * @count: input size
95 * Adds a new dynamic PCMCIA device ID to this driver,
96 * and causes the driver to probe for all devices again.
98 static ssize_t
99 pcmcia_store_new_id(struct device_driver *driver, const char *buf, size_t count)
101 struct pcmcia_dynid *dynid;
102 struct pcmcia_driver *pdrv = to_pcmcia_drv(driver);
103 __u16 match_flags, manf_id, card_id;
104 __u8 func_id, function, device_no;
105 __u32 prod_id_hash[4] = {0, 0, 0, 0};
106 int fields = 0;
107 int retval = 0;
109 fields = sscanf(buf, "%hx %hx %hx %hhx %hhx %hhx %x %x %x %x",
110 &match_flags, &manf_id, &card_id, &func_id, &function, &device_no,
111 &prod_id_hash[0], &prod_id_hash[1], &prod_id_hash[2], &prod_id_hash[3]);
112 if (fields < 6)
113 return -EINVAL;
115 dynid = kzalloc(sizeof(struct pcmcia_dynid), GFP_KERNEL);
116 if (!dynid)
117 return -ENOMEM;
119 dynid->id.match_flags = match_flags;
120 dynid->id.manf_id = manf_id;
121 dynid->id.card_id = card_id;
122 dynid->id.func_id = func_id;
123 dynid->id.function = function;
124 dynid->id.device_no = device_no;
125 memcpy(dynid->id.prod_id_hash, prod_id_hash, sizeof(__u32) * 4);
127 mutex_lock(&pdrv->dynids.lock);
128 list_add_tail(&dynid->node, &pdrv->dynids.list);
129 mutex_unlock(&pdrv->dynids.lock);
131 if (get_driver(&pdrv->drv)) {
132 retval = driver_attach(&pdrv->drv);
133 put_driver(&pdrv->drv);
136 if (retval)
137 return retval;
138 return count;
140 static DRIVER_ATTR(new_id, S_IWUSR, NULL, pcmcia_store_new_id);
142 static void
143 pcmcia_free_dynids(struct pcmcia_driver *drv)
145 struct pcmcia_dynid *dynid, *n;
147 mutex_lock(&drv->dynids.lock);
148 list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
149 list_del(&dynid->node);
150 kfree(dynid);
152 mutex_unlock(&drv->dynids.lock);
155 static int
156 pcmcia_create_newid_file(struct pcmcia_driver *drv)
158 int error = 0;
159 if (drv->probe != NULL)
160 error = driver_create_file(&drv->drv, &driver_attr_new_id);
161 return error;
166 * pcmcia_register_driver - register a PCMCIA driver with the bus core
167 * @driver: the &driver being registered
169 * Registers a PCMCIA driver with the PCMCIA bus core.
171 int pcmcia_register_driver(struct pcmcia_driver *driver)
173 int error;
175 if (!driver)
176 return -EINVAL;
178 pcmcia_check_driver(driver);
180 /* initialize common fields */
181 driver->drv.bus = &pcmcia_bus_type;
182 driver->drv.owner = driver->owner;
183 mutex_init(&driver->dynids.lock);
184 INIT_LIST_HEAD(&driver->dynids.list);
186 pr_debug("registering driver %s\n", driver->drv.name);
188 error = driver_register(&driver->drv);
189 if (error < 0)
190 return error;
192 error = pcmcia_create_newid_file(driver);
193 if (error)
194 driver_unregister(&driver->drv);
196 return error;
198 EXPORT_SYMBOL(pcmcia_register_driver);
201 * pcmcia_unregister_driver - unregister a PCMCIA driver with the bus core
202 * @driver: the &driver being unregistered
204 void pcmcia_unregister_driver(struct pcmcia_driver *driver)
206 pr_debug("unregistering driver %s\n", driver->drv.name);
207 driver_unregister(&driver->drv);
208 pcmcia_free_dynids(driver);
210 EXPORT_SYMBOL(pcmcia_unregister_driver);
213 /* pcmcia_device handling */
215 struct pcmcia_device *pcmcia_get_dev(struct pcmcia_device *p_dev)
217 struct device *tmp_dev;
218 tmp_dev = get_device(&p_dev->dev);
219 if (!tmp_dev)
220 return NULL;
221 return to_pcmcia_dev(tmp_dev);
224 void pcmcia_put_dev(struct pcmcia_device *p_dev)
226 if (p_dev)
227 put_device(&p_dev->dev);
230 static void pcmcia_release_function(struct kref *ref)
232 struct config_t *c = container_of(ref, struct config_t, ref);
233 pr_debug("releasing config_t\n");
234 kfree(c);
237 static void pcmcia_release_dev(struct device *dev)
239 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
240 int i;
241 dev_dbg(dev, "releasing device\n");
242 pcmcia_put_socket(p_dev->socket);
243 for (i = 0; i < 4; i++)
244 kfree(p_dev->prod_id[i]);
245 kfree(p_dev->devname);
246 kref_put(&p_dev->function_config->ref, pcmcia_release_function);
247 kfree(p_dev);
251 static int pcmcia_device_probe(struct device *dev)
253 struct pcmcia_device *p_dev;
254 struct pcmcia_driver *p_drv;
255 struct pcmcia_socket *s;
256 cistpl_config_t cis_config;
257 int ret = 0;
259 dev = get_device(dev);
260 if (!dev)
261 return -ENODEV;
263 p_dev = to_pcmcia_dev(dev);
264 p_drv = to_pcmcia_drv(dev->driver);
265 s = p_dev->socket;
267 dev_dbg(dev, "trying to bind to %s\n", p_drv->drv.name);
269 if ((!p_drv->probe) || (!p_dev->function_config) ||
270 (!try_module_get(p_drv->owner))) {
271 ret = -EINVAL;
272 goto put_dev;
275 /* set up some more device information */
276 ret = pccard_read_tuple(p_dev->socket, p_dev->func, CISTPL_CONFIG,
277 &cis_config);
278 if (!ret) {
279 p_dev->conf.ConfigBase = cis_config.base;
280 p_dev->conf.Present = cis_config.rmask[0];
281 } else {
282 dev_printk(KERN_INFO, dev,
283 "pcmcia: could not parse base and rmask0 of CIS\n");
284 p_dev->conf.ConfigBase = 0;
285 p_dev->conf.Present = 0;
288 ret = p_drv->probe(p_dev);
289 if (ret) {
290 dev_dbg(dev, "binding to %s failed with %d\n",
291 p_drv->drv.name, ret);
292 goto put_module;
295 mutex_lock(&s->ops_mutex);
296 if ((s->pcmcia_state.has_pfc) &&
297 (p_dev->socket->device_count == 1) && (p_dev->device_no == 0))
298 pcmcia_parse_uevents(s, PCMCIA_UEVENT_REQUERY);
299 mutex_unlock(&s->ops_mutex);
301 put_module:
302 if (ret)
303 module_put(p_drv->owner);
304 put_dev:
305 if (ret)
306 put_device(dev);
307 return ret;
312 * Removes a PCMCIA card from the device tree and socket list.
314 static void pcmcia_card_remove(struct pcmcia_socket *s, struct pcmcia_device *leftover)
316 struct pcmcia_device *p_dev;
317 struct pcmcia_device *tmp;
319 dev_dbg(leftover ? &leftover->dev : &s->dev,
320 "pcmcia_card_remove(%d) %s\n", s->sock,
321 leftover ? leftover->devname : "");
323 mutex_lock(&s->ops_mutex);
324 if (!leftover)
325 s->device_count = 0;
326 else
327 s->device_count = 1;
328 mutex_unlock(&s->ops_mutex);
330 /* unregister all pcmcia_devices registered with this socket, except leftover */
331 list_for_each_entry_safe(p_dev, tmp, &s->devices_list, socket_device_list) {
332 if (p_dev == leftover)
333 continue;
335 mutex_lock(&s->ops_mutex);
336 list_del(&p_dev->socket_device_list);
337 p_dev->_removed = 1;
338 mutex_unlock(&s->ops_mutex);
340 dev_dbg(&p_dev->dev, "unregistering device\n");
341 device_unregister(&p_dev->dev);
344 return;
347 static int pcmcia_device_remove(struct device *dev)
349 struct pcmcia_device *p_dev;
350 struct pcmcia_driver *p_drv;
351 int i;
353 p_dev = to_pcmcia_dev(dev);
354 p_drv = to_pcmcia_drv(dev->driver);
356 dev_dbg(dev, "removing device\n");
358 /* If we're removing the primary module driving a
359 * pseudo multi-function card, we need to unbind
360 * all devices
362 if ((p_dev->socket->pcmcia_state.has_pfc) &&
363 (p_dev->socket->device_count > 0) &&
364 (p_dev->device_no == 0))
365 pcmcia_card_remove(p_dev->socket, p_dev);
367 /* detach the "instance" */
368 if (!p_drv)
369 return 0;
371 if (p_drv->remove)
372 p_drv->remove(p_dev);
374 p_dev->dev_node = NULL;
376 /* check for proper unloading */
377 if (p_dev->_irq || p_dev->_io || p_dev->_locked)
378 dev_printk(KERN_INFO, dev,
379 "pcmcia: driver %s did not release config properly\n",
380 p_drv->drv.name);
382 for (i = 0; i < MAX_WIN; i++)
383 if (p_dev->_win & CLIENT_WIN_REQ(i))
384 dev_printk(KERN_INFO, dev,
385 "pcmcia: driver %s did not release window properly\n",
386 p_drv->drv.name);
388 /* references from pcmcia_probe_device */
389 pcmcia_put_dev(p_dev);
390 module_put(p_drv->owner);
392 return 0;
397 * pcmcia_device_query -- determine information about a pcmcia device
399 static int pcmcia_device_query(struct pcmcia_device *p_dev)
401 cistpl_manfid_t manf_id;
402 cistpl_funcid_t func_id;
403 cistpl_vers_1_t *vers1;
404 unsigned int i;
406 vers1 = kmalloc(sizeof(*vers1), GFP_KERNEL);
407 if (!vers1)
408 return -ENOMEM;
410 if (!pccard_read_tuple(p_dev->socket, BIND_FN_ALL,
411 CISTPL_MANFID, &manf_id)) {
412 mutex_lock(&p_dev->socket->ops_mutex);
413 p_dev->manf_id = manf_id.manf;
414 p_dev->card_id = manf_id.card;
415 p_dev->has_manf_id = 1;
416 p_dev->has_card_id = 1;
417 mutex_unlock(&p_dev->socket->ops_mutex);
420 if (!pccard_read_tuple(p_dev->socket, p_dev->func,
421 CISTPL_FUNCID, &func_id)) {
422 mutex_lock(&p_dev->socket->ops_mutex);
423 p_dev->func_id = func_id.func;
424 p_dev->has_func_id = 1;
425 mutex_unlock(&p_dev->socket->ops_mutex);
426 } else {
427 /* rule of thumb: cards with no FUNCID, but with
428 * common memory device geometry information, are
429 * probably memory cards (from pcmcia-cs) */
430 cistpl_device_geo_t *devgeo;
432 devgeo = kmalloc(sizeof(*devgeo), GFP_KERNEL);
433 if (!devgeo) {
434 kfree(vers1);
435 return -ENOMEM;
437 if (!pccard_read_tuple(p_dev->socket, p_dev->func,
438 CISTPL_DEVICE_GEO, devgeo)) {
439 dev_dbg(&p_dev->dev,
440 "mem device geometry probably means "
441 "FUNCID_MEMORY\n");
442 mutex_lock(&p_dev->socket->ops_mutex);
443 p_dev->func_id = CISTPL_FUNCID_MEMORY;
444 p_dev->has_func_id = 1;
445 mutex_unlock(&p_dev->socket->ops_mutex);
447 kfree(devgeo);
450 if (!pccard_read_tuple(p_dev->socket, BIND_FN_ALL, CISTPL_VERS_1,
451 vers1)) {
452 mutex_lock(&p_dev->socket->ops_mutex);
453 for (i = 0; i < min_t(unsigned int, 4, vers1->ns); i++) {
454 char *tmp;
455 unsigned int length;
456 char *new;
458 tmp = vers1->str + vers1->ofs[i];
460 length = strlen(tmp) + 1;
461 if ((length < 2) || (length > 255))
462 continue;
464 new = kmalloc(sizeof(char) * length, GFP_KERNEL);
465 if (!new)
466 continue;
468 new = strncpy(new, tmp, length);
470 tmp = p_dev->prod_id[i];
471 p_dev->prod_id[i] = new;
472 kfree(tmp);
474 mutex_unlock(&p_dev->socket->ops_mutex);
477 kfree(vers1);
478 return 0;
482 /* device_add_lock is needed to avoid double registration by cardmgr and kernel.
483 * Serializes pcmcia_device_add; will most likely be removed in future.
485 * While it has the caveat that adding new PCMCIA devices inside(!) device_register()
486 * won't work, this doesn't matter much at the moment: the driver core doesn't
487 * support it either.
489 static DEFINE_MUTEX(device_add_lock);
491 struct pcmcia_device *pcmcia_device_add(struct pcmcia_socket *s, unsigned int function)
493 struct pcmcia_device *p_dev, *tmp_dev;
494 int i;
496 s = pcmcia_get_socket(s);
497 if (!s)
498 return NULL;
500 mutex_lock(&device_add_lock);
502 pr_debug("adding device to %d, function %d\n", s->sock, function);
504 p_dev = kzalloc(sizeof(struct pcmcia_device), GFP_KERNEL);
505 if (!p_dev)
506 goto err_put;
508 mutex_lock(&s->ops_mutex);
509 p_dev->device_no = (s->device_count++);
510 mutex_unlock(&s->ops_mutex);
512 /* max of 2 devices per card */
513 if (p_dev->device_no >= 2)
514 goto err_free;
516 p_dev->socket = s;
517 p_dev->func = function;
519 p_dev->dev.bus = &pcmcia_bus_type;
520 p_dev->dev.parent = s->dev.parent;
521 p_dev->dev.release = pcmcia_release_dev;
522 /* by default don't allow DMA */
523 p_dev->dma_mask = DMA_MASK_NONE;
524 p_dev->dev.dma_mask = &p_dev->dma_mask;
525 dev_set_name(&p_dev->dev, "%d.%d", p_dev->socket->sock, p_dev->device_no);
526 if (!dev_name(&p_dev->dev))
527 goto err_free;
528 p_dev->devname = kasprintf(GFP_KERNEL, "pcmcia%s", dev_name(&p_dev->dev));
529 if (!p_dev->devname)
530 goto err_free;
531 dev_dbg(&p_dev->dev, "devname is %s\n", p_dev->devname);
533 mutex_lock(&s->ops_mutex);
536 * p_dev->function_config must be the same for all card functions.
537 * Note that this is serialized by the device_add_lock, so that
538 * only one such struct will be created.
540 list_for_each_entry(tmp_dev, &s->devices_list, socket_device_list)
541 if (p_dev->func == tmp_dev->func) {
542 p_dev->function_config = tmp_dev->function_config;
543 p_dev->io = tmp_dev->io;
544 p_dev->irq = tmp_dev->irq;
545 kref_get(&p_dev->function_config->ref);
548 /* Add to the list in pcmcia_bus_socket */
549 list_add(&p_dev->socket_device_list, &s->devices_list);
551 mutex_unlock(&s->ops_mutex);
553 if (!p_dev->function_config) {
554 dev_dbg(&p_dev->dev, "creating config_t\n");
555 p_dev->function_config = kzalloc(sizeof(struct config_t),
556 GFP_KERNEL);
557 if (!p_dev->function_config)
558 goto err_unreg;
559 kref_init(&p_dev->function_config->ref);
562 dev_printk(KERN_NOTICE, &p_dev->dev,
563 "pcmcia: registering new device %s\n",
564 p_dev->devname);
566 pcmcia_device_query(p_dev);
568 if (device_register(&p_dev->dev))
569 goto err_unreg;
571 mutex_unlock(&device_add_lock);
573 return p_dev;
575 err_unreg:
576 mutex_lock(&s->ops_mutex);
577 list_del(&p_dev->socket_device_list);
578 mutex_unlock(&s->ops_mutex);
580 err_free:
581 mutex_lock(&s->ops_mutex);
582 s->device_count--;
583 mutex_unlock(&s->ops_mutex);
585 for (i = 0; i < 4; i++)
586 kfree(p_dev->prod_id[i]);
587 kfree(p_dev->devname);
588 kfree(p_dev);
589 err_put:
590 mutex_unlock(&device_add_lock);
591 pcmcia_put_socket(s);
593 return NULL;
597 static int pcmcia_card_add(struct pcmcia_socket *s)
599 cistpl_longlink_mfc_t mfc;
600 unsigned int no_funcs, i, no_chains;
601 int ret = -EAGAIN;
603 mutex_lock(&s->ops_mutex);
604 if (!(s->resource_setup_done)) {
605 dev_dbg(&s->dev,
606 "no resources available, delaying card_add\n");
607 mutex_unlock(&s->ops_mutex);
608 return -EAGAIN; /* try again, but later... */
611 if (pcmcia_validate_mem(s)) {
612 dev_dbg(&s->dev, "validating mem resources failed, "
613 "delaying card_add\n");
614 mutex_unlock(&s->ops_mutex);
615 return -EAGAIN; /* try again, but later... */
617 mutex_unlock(&s->ops_mutex);
619 ret = pccard_validate_cis(s, &no_chains);
620 if (ret || !no_chains) {
621 dev_dbg(&s->dev, "invalid CIS or invalid resources\n");
622 return -ENODEV;
625 if (!pccard_read_tuple(s, BIND_FN_ALL, CISTPL_LONGLINK_MFC, &mfc))
626 no_funcs = mfc.nfn;
627 else
628 no_funcs = 1;
629 s->functions = no_funcs;
631 for (i = 0; i < no_funcs; i++)
632 pcmcia_device_add(s, i);
634 return ret;
638 static int pcmcia_requery_callback(struct device *dev, void * _data)
640 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
641 if (!p_dev->dev.driver) {
642 dev_dbg(dev, "update device information\n");
643 pcmcia_device_query(p_dev);
646 return 0;
650 static void pcmcia_requery(struct pcmcia_socket *s)
652 int present, has_pfc;
654 mutex_lock(&s->ops_mutex);
655 present = s->pcmcia_state.present;
656 mutex_unlock(&s->ops_mutex);
658 if (!present)
659 return;
661 if (s->functions == 0) {
662 pcmcia_card_add(s);
663 return;
666 /* some device information might have changed because of a CIS
667 * update or because we can finally read it correctly... so
668 * determine it again, overwriting old values if necessary. */
669 bus_for_each_dev(&pcmcia_bus_type, NULL, NULL, pcmcia_requery_callback);
671 /* if the CIS changed, we need to check whether the number of
672 * functions changed. */
673 if (s->fake_cis) {
674 int old_funcs, new_funcs;
675 cistpl_longlink_mfc_t mfc;
677 /* does this cis override add or remove functions? */
678 old_funcs = s->functions;
680 if (!pccard_read_tuple(s, BIND_FN_ALL, CISTPL_LONGLINK_MFC,
681 &mfc))
682 new_funcs = mfc.nfn;
683 else
684 new_funcs = 1;
685 if (old_funcs > new_funcs) {
686 pcmcia_card_remove(s, NULL);
687 pcmcia_card_add(s);
688 } else if (new_funcs > old_funcs) {
689 s->functions = new_funcs;
690 pcmcia_device_add(s, 1);
694 /* If the PCMCIA device consists of two pseudo devices,
695 * call pcmcia_device_add() -- which will fail if both
696 * devices are already registered. */
697 mutex_lock(&s->ops_mutex);
698 has_pfc = s->pcmcia_state.has_pfc;
699 mutex_unlock(&s->ops_mutex);
700 if (has_pfc)
701 pcmcia_device_add(s, 0);
703 /* we re-scan all devices, not just the ones connected to this
704 * socket. This does not matter, though. */
705 if (bus_rescan_devices(&pcmcia_bus_type))
706 dev_warn(&s->dev, "rescanning the bus failed\n");
710 #ifdef CONFIG_PCMCIA_LOAD_CIS
713 * pcmcia_load_firmware - load CIS from userspace if device-provided is broken
714 * @dev: the pcmcia device which needs a CIS override
715 * @filename: requested filename in /lib/firmware/
717 * This uses the in-kernel firmware loading mechanism to use a "fake CIS" if
718 * the one provided by the card is broken. The firmware files reside in
719 * /lib/firmware/ in userspace.
721 static int pcmcia_load_firmware(struct pcmcia_device *dev, char * filename)
723 struct pcmcia_socket *s = dev->socket;
724 const struct firmware *fw;
725 int ret = -ENOMEM;
727 if (!filename)
728 return -EINVAL;
730 dev_dbg(&dev->dev, "trying to load CIS file %s\n", filename);
732 if (request_firmware(&fw, filename, &dev->dev) == 0) {
733 if (fw->size >= CISTPL_MAX_CIS_SIZE) {
734 ret = -EINVAL;
735 dev_printk(KERN_ERR, &dev->dev,
736 "pcmcia: CIS override is too big\n");
737 goto release;
740 if (!pcmcia_replace_cis(s, fw->data, fw->size))
741 ret = 0;
742 else {
743 dev_printk(KERN_ERR, &dev->dev,
744 "pcmcia: CIS override failed\n");
745 goto release;
749 /* update information */
750 pcmcia_device_query(dev);
752 /* requery (as number of functions might have changed) */
753 pcmcia_parse_uevents(s, PCMCIA_UEVENT_REQUERY);
755 release:
756 release_firmware(fw);
758 return ret;
761 #else /* !CONFIG_PCMCIA_LOAD_CIS */
763 static inline int pcmcia_load_firmware(struct pcmcia_device *dev, char * filename)
765 return -ENODEV;
768 #endif
771 static inline int pcmcia_devmatch(struct pcmcia_device *dev,
772 struct pcmcia_device_id *did)
774 if (did->match_flags & PCMCIA_DEV_ID_MATCH_MANF_ID) {
775 if ((!dev->has_manf_id) || (dev->manf_id != did->manf_id))
776 return 0;
779 if (did->match_flags & PCMCIA_DEV_ID_MATCH_CARD_ID) {
780 if ((!dev->has_card_id) || (dev->card_id != did->card_id))
781 return 0;
784 if (did->match_flags & PCMCIA_DEV_ID_MATCH_FUNCTION) {
785 if (dev->func != did->function)
786 return 0;
789 if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID1) {
790 if (!dev->prod_id[0])
791 return 0;
792 if (strcmp(did->prod_id[0], dev->prod_id[0]))
793 return 0;
796 if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID2) {
797 if (!dev->prod_id[1])
798 return 0;
799 if (strcmp(did->prod_id[1], dev->prod_id[1]))
800 return 0;
803 if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID3) {
804 if (!dev->prod_id[2])
805 return 0;
806 if (strcmp(did->prod_id[2], dev->prod_id[2]))
807 return 0;
810 if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID4) {
811 if (!dev->prod_id[3])
812 return 0;
813 if (strcmp(did->prod_id[3], dev->prod_id[3]))
814 return 0;
817 if (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO) {
818 if (dev->device_no != did->device_no)
819 return 0;
820 mutex_lock(&dev->socket->ops_mutex);
821 dev->socket->pcmcia_state.has_pfc = 1;
822 mutex_unlock(&dev->socket->ops_mutex);
825 if (did->match_flags & PCMCIA_DEV_ID_MATCH_FUNC_ID) {
826 int ret;
828 if ((!dev->has_func_id) || (dev->func_id != did->func_id))
829 return 0;
831 /* if this is a pseudo-multi-function device,
832 * we need explicit matches */
833 if (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO)
834 return 0;
835 if (dev->device_no)
836 return 0;
838 /* also, FUNC_ID matching needs to be activated by userspace
839 * after it has re-checked that there is no possible module
840 * with a prod_id/manf_id/card_id match.
842 mutex_lock(&dev->socket->ops_mutex);
843 ret = dev->allow_func_id_match;
844 mutex_unlock(&dev->socket->ops_mutex);
846 if (!ret) {
847 dev_dbg(&dev->dev,
848 "skipping FUNC_ID match until userspace ACK\n");
849 return 0;
853 if (did->match_flags & PCMCIA_DEV_ID_MATCH_FAKE_CIS) {
854 dev_dbg(&dev->dev, "device needs a fake CIS\n");
855 if (!dev->socket->fake_cis)
856 pcmcia_load_firmware(dev, did->cisfile);
858 if (!dev->socket->fake_cis)
859 return 0;
862 if (did->match_flags & PCMCIA_DEV_ID_MATCH_ANONYMOUS) {
863 int i;
864 for (i = 0; i < 4; i++)
865 if (dev->prod_id[i])
866 return 0;
867 if (dev->has_manf_id || dev->has_card_id || dev->has_func_id)
868 return 0;
871 return 1;
875 static int pcmcia_bus_match(struct device *dev, struct device_driver *drv)
877 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
878 struct pcmcia_driver *p_drv = to_pcmcia_drv(drv);
879 struct pcmcia_device_id *did = p_drv->id_table;
880 struct pcmcia_dynid *dynid;
882 /* match dynamic devices first */
883 mutex_lock(&p_drv->dynids.lock);
884 list_for_each_entry(dynid, &p_drv->dynids.list, node) {
885 dev_dbg(dev, "trying to match to %s\n", drv->name);
886 if (pcmcia_devmatch(p_dev, &dynid->id)) {
887 dev_dbg(dev, "matched to %s\n", drv->name);
888 mutex_unlock(&p_drv->dynids.lock);
889 return 1;
892 mutex_unlock(&p_drv->dynids.lock);
894 #ifdef CONFIG_PCMCIA_IOCTL
895 /* matching by cardmgr */
896 if (p_dev->cardmgr == p_drv) {
897 dev_dbg(dev, "cardmgr matched to %s\n", drv->name);
898 return 1;
900 #endif
902 while (did && did->match_flags) {
903 dev_dbg(dev, "trying to match to %s\n", drv->name);
904 if (pcmcia_devmatch(p_dev, did)) {
905 dev_dbg(dev, "matched to %s\n", drv->name);
906 return 1;
908 did++;
911 return 0;
914 #ifdef CONFIG_HOTPLUG
916 static int pcmcia_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
918 struct pcmcia_device *p_dev;
919 int i;
920 u32 hash[4] = { 0, 0, 0, 0};
922 if (!dev)
923 return -ENODEV;
925 p_dev = to_pcmcia_dev(dev);
927 /* calculate hashes */
928 for (i = 0; i < 4; i++) {
929 if (!p_dev->prod_id[i])
930 continue;
931 hash[i] = crc32(0, p_dev->prod_id[i], strlen(p_dev->prod_id[i]));
934 if (add_uevent_var(env, "SOCKET_NO=%u", p_dev->socket->sock))
935 return -ENOMEM;
937 if (add_uevent_var(env, "DEVICE_NO=%02X", p_dev->device_no))
938 return -ENOMEM;
940 if (add_uevent_var(env, "MODALIAS=pcmcia:m%04Xc%04Xf%02Xfn%02Xpfn%02X"
941 "pa%08Xpb%08Xpc%08Xpd%08X",
942 p_dev->has_manf_id ? p_dev->manf_id : 0,
943 p_dev->has_card_id ? p_dev->card_id : 0,
944 p_dev->has_func_id ? p_dev->func_id : 0,
945 p_dev->func,
946 p_dev->device_no,
947 hash[0],
948 hash[1],
949 hash[2],
950 hash[3]))
951 return -ENOMEM;
953 return 0;
956 #else
958 static int pcmcia_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
960 return -ENODEV;
963 #endif
965 /************************ runtime PM support ***************************/
967 static int pcmcia_dev_suspend(struct device *dev, pm_message_t state);
968 static int pcmcia_dev_resume(struct device *dev);
970 static int runtime_suspend(struct device *dev)
972 int rc;
974 device_lock(dev);
975 rc = pcmcia_dev_suspend(dev, PMSG_SUSPEND);
976 device_unlock(dev);
977 return rc;
980 static int runtime_resume(struct device *dev)
982 int rc;
984 device_lock(dev);
985 rc = pcmcia_dev_resume(dev);
986 device_unlock(dev);
987 return rc;
990 /************************ per-device sysfs output ***************************/
992 #define pcmcia_device_attr(field, test, format) \
993 static ssize_t field##_show (struct device *dev, struct device_attribute *attr, char *buf) \
995 struct pcmcia_device *p_dev = to_pcmcia_dev(dev); \
996 return p_dev->test ? sprintf(buf, format, p_dev->field) : -ENODEV; \
999 #define pcmcia_device_stringattr(name, field) \
1000 static ssize_t name##_show (struct device *dev, struct device_attribute *attr, char *buf) \
1002 struct pcmcia_device *p_dev = to_pcmcia_dev(dev); \
1003 return p_dev->field ? sprintf(buf, "%s\n", p_dev->field) : -ENODEV; \
1006 pcmcia_device_attr(func, socket, "0x%02x\n");
1007 pcmcia_device_attr(func_id, has_func_id, "0x%02x\n");
1008 pcmcia_device_attr(manf_id, has_manf_id, "0x%04x\n");
1009 pcmcia_device_attr(card_id, has_card_id, "0x%04x\n");
1010 pcmcia_device_stringattr(prod_id1, prod_id[0]);
1011 pcmcia_device_stringattr(prod_id2, prod_id[1]);
1012 pcmcia_device_stringattr(prod_id3, prod_id[2]);
1013 pcmcia_device_stringattr(prod_id4, prod_id[3]);
1016 static ssize_t pcmcia_show_pm_state(struct device *dev, struct device_attribute *attr, char *buf)
1018 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1020 if (p_dev->suspended)
1021 return sprintf(buf, "off\n");
1022 else
1023 return sprintf(buf, "on\n");
1026 static ssize_t pcmcia_store_pm_state(struct device *dev, struct device_attribute *attr,
1027 const char *buf, size_t count)
1029 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1030 int ret = 0;
1032 if (!count)
1033 return -EINVAL;
1035 if ((!p_dev->suspended) && !strncmp(buf, "off", 3))
1036 ret = runtime_suspend(dev);
1037 else if (p_dev->suspended && !strncmp(buf, "on", 2))
1038 ret = runtime_resume(dev);
1040 return ret ? ret : count;
1044 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
1046 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1047 int i;
1048 u32 hash[4] = { 0, 0, 0, 0};
1050 /* calculate hashes */
1051 for (i = 0; i < 4; i++) {
1052 if (!p_dev->prod_id[i])
1053 continue;
1054 hash[i] = crc32(0, p_dev->prod_id[i],
1055 strlen(p_dev->prod_id[i]));
1057 return sprintf(buf, "pcmcia:m%04Xc%04Xf%02Xfn%02Xpfn%02X"
1058 "pa%08Xpb%08Xpc%08Xpd%08X\n",
1059 p_dev->has_manf_id ? p_dev->manf_id : 0,
1060 p_dev->has_card_id ? p_dev->card_id : 0,
1061 p_dev->has_func_id ? p_dev->func_id : 0,
1062 p_dev->func, p_dev->device_no,
1063 hash[0], hash[1], hash[2], hash[3]);
1066 static ssize_t pcmcia_store_allow_func_id_match(struct device *dev,
1067 struct device_attribute *attr, const char *buf, size_t count)
1069 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1071 if (!count)
1072 return -EINVAL;
1074 mutex_lock(&p_dev->socket->ops_mutex);
1075 p_dev->allow_func_id_match = 1;
1076 mutex_unlock(&p_dev->socket->ops_mutex);
1077 pcmcia_parse_uevents(p_dev->socket, PCMCIA_UEVENT_REQUERY);
1079 return count;
1082 static struct device_attribute pcmcia_dev_attrs[] = {
1083 __ATTR(function, 0444, func_show, NULL),
1084 __ATTR(pm_state, 0644, pcmcia_show_pm_state, pcmcia_store_pm_state),
1085 __ATTR_RO(func_id),
1086 __ATTR_RO(manf_id),
1087 __ATTR_RO(card_id),
1088 __ATTR_RO(prod_id1),
1089 __ATTR_RO(prod_id2),
1090 __ATTR_RO(prod_id3),
1091 __ATTR_RO(prod_id4),
1092 __ATTR_RO(modalias),
1093 __ATTR(allow_func_id_match, 0200, NULL, pcmcia_store_allow_func_id_match),
1094 __ATTR_NULL,
1097 /* PM support, also needed for reset */
1099 static int pcmcia_dev_suspend(struct device *dev, pm_message_t state)
1101 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1102 struct pcmcia_driver *p_drv = NULL;
1103 int ret = 0;
1105 mutex_lock(&p_dev->socket->ops_mutex);
1106 if (p_dev->suspended) {
1107 mutex_unlock(&p_dev->socket->ops_mutex);
1108 return 0;
1110 p_dev->suspended = 1;
1111 mutex_unlock(&p_dev->socket->ops_mutex);
1113 dev_dbg(dev, "suspending\n");
1115 if (dev->driver)
1116 p_drv = to_pcmcia_drv(dev->driver);
1118 if (!p_drv)
1119 goto out;
1121 if (p_drv->suspend) {
1122 ret = p_drv->suspend(p_dev);
1123 if (ret) {
1124 dev_printk(KERN_ERR, dev,
1125 "pcmcia: device %s (driver %s) did "
1126 "not want to go to sleep (%d)\n",
1127 p_dev->devname, p_drv->drv.name, ret);
1128 mutex_lock(&p_dev->socket->ops_mutex);
1129 p_dev->suspended = 0;
1130 mutex_unlock(&p_dev->socket->ops_mutex);
1131 goto out;
1135 if (p_dev->device_no == p_dev->func) {
1136 dev_dbg(dev, "releasing configuration\n");
1137 pcmcia_release_configuration(p_dev);
1140 out:
1141 return ret;
1145 static int pcmcia_dev_resume(struct device *dev)
1147 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1148 struct pcmcia_driver *p_drv = NULL;
1149 int ret = 0;
1151 mutex_lock(&p_dev->socket->ops_mutex);
1152 if (!p_dev->suspended) {
1153 mutex_unlock(&p_dev->socket->ops_mutex);
1154 return 0;
1156 p_dev->suspended = 0;
1157 mutex_unlock(&p_dev->socket->ops_mutex);
1159 dev_dbg(dev, "resuming\n");
1161 if (dev->driver)
1162 p_drv = to_pcmcia_drv(dev->driver);
1164 if (!p_drv)
1165 goto out;
1167 if (p_dev->device_no == p_dev->func) {
1168 dev_dbg(dev, "requesting configuration\n");
1169 ret = pcmcia_request_configuration(p_dev, &p_dev->conf);
1170 if (ret)
1171 goto out;
1174 if (p_drv->resume)
1175 ret = p_drv->resume(p_dev);
1177 out:
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 mutex_lock(&s->ops_mutex);
1253 s->pcmcia_state.present = 0;
1254 mutex_unlock(&s->ops_mutex);
1255 pcmcia_card_remove(skt, NULL);
1256 handle_event(skt, event);
1257 mutex_lock(&s->ops_mutex);
1258 destroy_cis_cache(s);
1259 mutex_unlock(&s->ops_mutex);
1260 break;
1262 case CS_EVENT_CARD_INSERTION:
1263 mutex_lock(&s->ops_mutex);
1264 s->pcmcia_state.has_pfc = 0;
1265 s->pcmcia_state.present = 1;
1266 destroy_cis_cache(s); /* to be on the safe side... */
1267 mutex_unlock(&s->ops_mutex);
1268 pcmcia_card_add(skt);
1269 handle_event(skt, event);
1270 break;
1272 case CS_EVENT_EJECTION_REQUEST:
1273 break;
1275 case CS_EVENT_PM_RESUME:
1276 if (verify_cis_cache(skt) != 0) {
1277 dev_dbg(&skt->dev, "cis mismatch - different card\n");
1278 /* first, remove the card */
1279 ds_event(skt, CS_EVENT_CARD_REMOVAL, CS_EVENT_PRI_HIGH);
1280 mutex_lock(&s->ops_mutex);
1281 destroy_cis_cache(skt);
1282 kfree(skt->fake_cis);
1283 skt->fake_cis = NULL;
1284 mutex_unlock(&s->ops_mutex);
1285 /* now, add the new card */
1286 ds_event(skt, CS_EVENT_CARD_INSERTION,
1287 CS_EVENT_PRI_LOW);
1289 handle_event(skt, event);
1290 break;
1292 case CS_EVENT_PM_SUSPEND:
1293 case CS_EVENT_RESET_PHYSICAL:
1294 case CS_EVENT_CARD_RESET:
1295 default:
1296 handle_event(skt, event);
1297 break;
1300 pcmcia_put_socket(s);
1302 return 0;
1303 } /* ds_event */
1306 struct pcmcia_device *pcmcia_dev_present(struct pcmcia_device *_p_dev)
1308 struct pcmcia_device *p_dev;
1309 struct pcmcia_device *ret = NULL;
1311 p_dev = pcmcia_get_dev(_p_dev);
1312 if (!p_dev)
1313 return NULL;
1315 mutex_lock(&p_dev->socket->ops_mutex);
1316 if (!p_dev->socket->pcmcia_state.present)
1317 goto out;
1319 if (p_dev->socket->pcmcia_state.dead)
1320 goto out;
1322 if (p_dev->_removed)
1323 goto out;
1325 if (p_dev->suspended)
1326 goto out;
1328 ret = p_dev;
1329 out:
1330 mutex_unlock(&p_dev->socket->ops_mutex);
1331 pcmcia_put_dev(p_dev);
1332 return ret;
1334 EXPORT_SYMBOL(pcmcia_dev_present);
1337 static struct pcmcia_callback pcmcia_bus_callback = {
1338 .owner = THIS_MODULE,
1339 .event = ds_event,
1340 .requery = pcmcia_requery,
1341 .validate = pccard_validate_cis,
1342 .suspend = pcmcia_bus_suspend,
1343 .resume = pcmcia_bus_resume,
1346 static int __devinit pcmcia_bus_add_socket(struct device *dev,
1347 struct class_interface *class_intf)
1349 struct pcmcia_socket *socket = dev_get_drvdata(dev);
1350 int ret;
1352 socket = pcmcia_get_socket(socket);
1353 if (!socket) {
1354 dev_printk(KERN_ERR, dev,
1355 "PCMCIA obtaining reference to socket failed\n");
1356 return -ENODEV;
1359 ret = sysfs_create_bin_file(&dev->kobj, &pccard_cis_attr);
1360 if (ret) {
1361 dev_printk(KERN_ERR, dev, "PCMCIA registration failed\n");
1362 pcmcia_put_socket(socket);
1363 return ret;
1366 #ifdef CONFIG_PCMCIA_IOCTL
1367 init_waitqueue_head(&socket->queue);
1368 #endif
1369 INIT_LIST_HEAD(&socket->devices_list);
1370 memset(&socket->pcmcia_state, 0, sizeof(u8));
1371 socket->device_count = 0;
1373 ret = pccard_register_pcmcia(socket, &pcmcia_bus_callback);
1374 if (ret) {
1375 dev_printk(KERN_ERR, dev, "PCMCIA registration failed\n");
1376 pcmcia_put_socket(socket);
1377 return ret;
1380 return 0;
1383 static void pcmcia_bus_remove_socket(struct device *dev,
1384 struct class_interface *class_intf)
1386 struct pcmcia_socket *socket = dev_get_drvdata(dev);
1388 if (!socket)
1389 return;
1391 mutex_lock(&socket->ops_mutex);
1392 socket->pcmcia_state.dead = 1;
1393 mutex_unlock(&socket->ops_mutex);
1395 pccard_register_pcmcia(socket, NULL);
1397 /* unregister any unbound devices */
1398 mutex_lock(&socket->skt_mutex);
1399 pcmcia_card_remove(socket, NULL);
1400 release_cis_mem(socket);
1401 mutex_unlock(&socket->skt_mutex);
1403 sysfs_remove_bin_file(&dev->kobj, &pccard_cis_attr);
1405 pcmcia_put_socket(socket);
1407 return;
1411 /* the pcmcia_bus_interface is used to handle pcmcia socket devices */
1412 static struct class_interface pcmcia_bus_interface __refdata = {
1413 .class = &pcmcia_socket_class,
1414 .add_dev = &pcmcia_bus_add_socket,
1415 .remove_dev = &pcmcia_bus_remove_socket,
1419 struct bus_type pcmcia_bus_type = {
1420 .name = "pcmcia",
1421 .uevent = pcmcia_bus_uevent,
1422 .match = pcmcia_bus_match,
1423 .dev_attrs = pcmcia_dev_attrs,
1424 .probe = pcmcia_device_probe,
1425 .remove = pcmcia_device_remove,
1426 .suspend = pcmcia_dev_suspend,
1427 .resume = pcmcia_dev_resume,
1431 static int __init init_pcmcia_bus(void)
1433 int ret;
1435 ret = bus_register(&pcmcia_bus_type);
1436 if (ret < 0) {
1437 printk(KERN_WARNING "pcmcia: bus_register error: %d\n", ret);
1438 return ret;
1440 ret = class_interface_register(&pcmcia_bus_interface);
1441 if (ret < 0) {
1442 printk(KERN_WARNING
1443 "pcmcia: class_interface_register error: %d\n", ret);
1444 bus_unregister(&pcmcia_bus_type);
1445 return ret;
1448 pcmcia_setup_ioctl();
1450 return 0;
1452 fs_initcall(init_pcmcia_bus); /* one level after subsys_initcall so that
1453 * pcmcia_socket_class is already registered */
1456 static void __exit exit_pcmcia_bus(void)
1458 pcmcia_cleanup_ioctl();
1460 class_interface_unregister(&pcmcia_bus_interface);
1462 bus_unregister(&pcmcia_bus_type);
1464 module_exit(exit_pcmcia_bus);
1467 MODULE_ALIAS("ds");