pcmcia: deprecate CS_CONFIGURATION_LOCKED
[linux-2.6/mini2440.git] / drivers / pcmcia / ds.c
blob7f38eb06c81ebc25237c3f804f7cd88761eb25ef
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"
35 #include "ds_internal.h"
37 /*====================================================================*/
39 /* Module parameters */
41 MODULE_AUTHOR("David Hinds <dahinds@users.sourceforge.net>");
42 MODULE_DESCRIPTION("PCMCIA Driver Services");
43 MODULE_LICENSE("GPL");
45 #ifdef CONFIG_PCMCIA_DEBUG
46 int ds_pc_debug;
48 module_param_named(pc_debug, ds_pc_debug, int, 0644);
50 #define ds_dbg(lvl, fmt, arg...) do { \
51 if (ds_pc_debug > (lvl)) \
52 printk(KERN_DEBUG "ds: " fmt , ## arg); \
53 } while (0)
54 #define ds_dev_dbg(lvl, dev, fmt, arg...) do { \
55 if (ds_pc_debug > (lvl)) \
56 dev_printk(KERN_DEBUG, dev, "ds: " fmt , ## arg); \
57 } while (0)
58 #else
59 #define ds_dbg(lvl, fmt, arg...) do { } while (0)
60 #define ds_dev_dbg(lvl, dev, fmt, arg...) do { } while (0)
61 #endif
63 spinlock_t pcmcia_dev_list_lock;
65 /*====================================================================*/
67 /* code which was in cs.c before */
69 /* String tables for error messages */
71 typedef struct lookup_t {
72 int key;
73 char *msg;
74 } lookup_t;
76 static const lookup_t error_table[] = {
77 { CS_SUCCESS, "Operation succeeded" },
78 { CS_BAD_ATTRIBUTE, "Bad attribute", },
79 { CS_BAD_BASE, "Bad base address" },
80 { CS_BAD_IRQ, "Bad IRQ" },
81 { CS_BAD_OFFSET, "Bad offset" },
82 { CS_BAD_PAGE, "Bad page number" },
83 { CS_BAD_SIZE, "Bad size" },
84 { CS_BAD_TYPE, "Bad type" },
85 { -EIO, "Input/Output error" },
86 { -ENODEV, "No card present" },
87 { -EINVAL, "Bad parameter" },
88 { CS_BAD_ARGS, "Bad arguments" },
89 { -EACCES, "Configuration locked" },
90 { CS_IN_USE, "Resource in use" },
91 { CS_NO_MORE_ITEMS, "No more items" },
92 { CS_OUT_OF_RESOURCE, "Out of resource" },
93 { CS_BAD_TUPLE, "Bad CIS tuple" }
97 static const lookup_t service_table[] = {
98 { AccessConfigurationRegister, "AccessConfigurationRegister" },
99 { AddSocketServices, "AddSocketServices" },
100 { AdjustResourceInfo, "AdjustResourceInfo" },
101 { CheckEraseQueue, "CheckEraseQueue" },
102 { CloseMemory, "CloseMemory" },
103 { DeregisterClient, "DeregisterClient" },
104 { DeregisterEraseQueue, "DeregisterEraseQueue" },
105 { GetCardServicesInfo, "GetCardServicesInfo" },
106 { GetClientInfo, "GetClientInfo" },
107 { GetConfigurationInfo, "GetConfigurationInfo" },
108 { GetEventMask, "GetEventMask" },
109 { GetFirstClient, "GetFirstClient" },
110 { GetFirstRegion, "GetFirstRegion" },
111 { GetFirstTuple, "GetFirstTuple" },
112 { GetNextClient, "GetNextClient" },
113 { GetNextRegion, "GetNextRegion" },
114 { GetNextTuple, "GetNextTuple" },
115 { GetStatus, "GetStatus" },
116 { GetTupleData, "GetTupleData" },
117 { MapMemPage, "MapMemPage" },
118 { ModifyConfiguration, "ModifyConfiguration" },
119 { ModifyWindow, "ModifyWindow" },
120 { OpenMemory, "OpenMemory" },
121 { ParseTuple, "ParseTuple" },
122 { ReadMemory, "ReadMemory" },
123 { RegisterClient, "RegisterClient" },
124 { RegisterEraseQueue, "RegisterEraseQueue" },
125 { RegisterMTD, "RegisterMTD" },
126 { ReleaseConfiguration, "ReleaseConfiguration" },
127 { ReleaseIO, "ReleaseIO" },
128 { ReleaseIRQ, "ReleaseIRQ" },
129 { ReleaseWindow, "ReleaseWindow" },
130 { RequestConfiguration, "RequestConfiguration" },
131 { RequestIO, "RequestIO" },
132 { RequestIRQ, "RequestIRQ" },
133 { RequestSocketMask, "RequestSocketMask" },
134 { RequestWindow, "RequestWindow" },
135 { ResetCard, "ResetCard" },
136 { SetEventMask, "SetEventMask" },
137 { ValidateCIS, "ValidateCIS" },
138 { WriteMemory, "WriteMemory" },
139 { BindDevice, "BindDevice" },
140 { BindMTD, "BindMTD" },
141 { ReportError, "ReportError" },
142 { SuspendCard, "SuspendCard" },
143 { ResumeCard, "ResumeCard" },
144 { EjectCard, "EjectCard" },
145 { InsertCard, "InsertCard" },
146 { ReplaceCIS, "ReplaceCIS" }
150 static int pcmcia_report_error(struct pcmcia_device *p_dev, error_info_t *err)
152 int i;
153 char *serv;
155 if (!p_dev)
156 printk(KERN_NOTICE);
157 else
158 printk(KERN_NOTICE "%s: ", p_dev->dev.bus_id);
160 for (i = 0; i < ARRAY_SIZE(service_table); i++)
161 if (service_table[i].key == err->func)
162 break;
163 if (i < ARRAY_SIZE(service_table))
164 serv = service_table[i].msg;
165 else
166 serv = "Unknown service number";
168 for (i = 0; i < ARRAY_SIZE(error_table); i++)
169 if (error_table[i].key == err->retcode)
170 break;
171 if (i < ARRAY_SIZE(error_table))
172 printk("%s: %s\n", serv, error_table[i].msg);
173 else
174 printk("%s: Unknown error code %#x\n", serv, err->retcode);
176 return 0;
177 } /* report_error */
179 /* end of code which was in cs.c before */
181 /*======================================================================*/
183 void cs_error(struct pcmcia_device *p_dev, int func, int ret)
185 error_info_t err = { func, ret };
186 pcmcia_report_error(p_dev, &err);
188 EXPORT_SYMBOL(cs_error);
191 static void pcmcia_check_driver(struct pcmcia_driver *p_drv)
193 struct pcmcia_device_id *did = p_drv->id_table;
194 unsigned int i;
195 u32 hash;
197 if (!p_drv->probe || !p_drv->remove)
198 printk(KERN_DEBUG "pcmcia: %s lacks a requisite callback "
199 "function\n", p_drv->drv.name);
201 while (did && did->match_flags) {
202 for (i=0; i<4; i++) {
203 if (!did->prod_id[i])
204 continue;
206 hash = crc32(0, did->prod_id[i], strlen(did->prod_id[i]));
207 if (hash == did->prod_id_hash[i])
208 continue;
210 printk(KERN_DEBUG "pcmcia: %s: invalid hash for "
211 "product string \"%s\": is 0x%x, should "
212 "be 0x%x\n", p_drv->drv.name, did->prod_id[i],
213 did->prod_id_hash[i], hash);
214 printk(KERN_DEBUG "pcmcia: see "
215 "Documentation/pcmcia/devicetable.txt for "
216 "details\n");
218 did++;
221 return;
225 /*======================================================================*/
228 struct pcmcia_dynid {
229 struct list_head node;
230 struct pcmcia_device_id id;
234 * pcmcia_store_new_id - add a new PCMCIA device ID to this driver and re-probe devices
235 * @driver: target device driver
236 * @buf: buffer for scanning device ID data
237 * @count: input size
239 * Adds a new dynamic PCMCIA device ID to this driver,
240 * and causes the driver to probe for all devices again.
242 static ssize_t
243 pcmcia_store_new_id(struct device_driver *driver, const char *buf, size_t count)
245 struct pcmcia_dynid *dynid;
246 struct pcmcia_driver *pdrv = to_pcmcia_drv(driver);
247 __u16 match_flags, manf_id, card_id;
248 __u8 func_id, function, device_no;
249 __u32 prod_id_hash[4] = {0, 0, 0, 0};
250 int fields=0;
251 int retval = 0;
253 fields = sscanf(buf, "%hx %hx %hx %hhx %hhx %hhx %x %x %x %x",
254 &match_flags, &manf_id, &card_id, &func_id, &function, &device_no,
255 &prod_id_hash[0], &prod_id_hash[1], &prod_id_hash[2], &prod_id_hash[3]);
256 if (fields < 6)
257 return -EINVAL;
259 dynid = kzalloc(sizeof(struct pcmcia_dynid), GFP_KERNEL);
260 if (!dynid)
261 return -ENOMEM;
263 INIT_LIST_HEAD(&dynid->node);
264 dynid->id.match_flags = match_flags;
265 dynid->id.manf_id = manf_id;
266 dynid->id.card_id = card_id;
267 dynid->id.func_id = func_id;
268 dynid->id.function = function;
269 dynid->id.device_no = device_no;
270 memcpy(dynid->id.prod_id_hash, prod_id_hash, sizeof(__u32) * 4);
272 spin_lock(&pdrv->dynids.lock);
273 list_add_tail(&pdrv->dynids.list, &dynid->node);
274 spin_unlock(&pdrv->dynids.lock);
276 if (get_driver(&pdrv->drv)) {
277 retval = driver_attach(&pdrv->drv);
278 put_driver(&pdrv->drv);
281 if (retval)
282 return retval;
283 return count;
285 static DRIVER_ATTR(new_id, S_IWUSR, NULL, pcmcia_store_new_id);
287 static void
288 pcmcia_free_dynids(struct pcmcia_driver *drv)
290 struct pcmcia_dynid *dynid, *n;
292 spin_lock(&drv->dynids.lock);
293 list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
294 list_del(&dynid->node);
295 kfree(dynid);
297 spin_unlock(&drv->dynids.lock);
300 static int
301 pcmcia_create_newid_file(struct pcmcia_driver *drv)
303 int error = 0;
304 if (drv->probe != NULL)
305 error = driver_create_file(&drv->drv, &driver_attr_new_id);
306 return error;
311 * pcmcia_register_driver - register a PCMCIA driver with the bus core
312 * @driver: the &driver being registered
314 * Registers a PCMCIA driver with the PCMCIA bus core.
316 int pcmcia_register_driver(struct pcmcia_driver *driver)
318 int error;
320 if (!driver)
321 return -EINVAL;
323 pcmcia_check_driver(driver);
325 /* initialize common fields */
326 driver->drv.bus = &pcmcia_bus_type;
327 driver->drv.owner = driver->owner;
328 spin_lock_init(&driver->dynids.lock);
329 INIT_LIST_HEAD(&driver->dynids.list);
331 ds_dbg(3, "registering driver %s\n", driver->drv.name);
333 error = driver_register(&driver->drv);
334 if (error < 0)
335 return error;
337 error = pcmcia_create_newid_file(driver);
338 if (error)
339 driver_unregister(&driver->drv);
341 return error;
343 EXPORT_SYMBOL(pcmcia_register_driver);
346 * pcmcia_unregister_driver - unregister a PCMCIA driver with the bus core
347 * @driver: the &driver being unregistered
349 void pcmcia_unregister_driver(struct pcmcia_driver *driver)
351 ds_dbg(3, "unregistering driver %s\n", driver->drv.name);
352 driver_unregister(&driver->drv);
353 pcmcia_free_dynids(driver);
355 EXPORT_SYMBOL(pcmcia_unregister_driver);
358 /* pcmcia_device handling */
360 struct pcmcia_device * pcmcia_get_dev(struct pcmcia_device *p_dev)
362 struct device *tmp_dev;
363 tmp_dev = get_device(&p_dev->dev);
364 if (!tmp_dev)
365 return NULL;
366 return to_pcmcia_dev(tmp_dev);
369 void pcmcia_put_dev(struct pcmcia_device *p_dev)
371 if (p_dev)
372 put_device(&p_dev->dev);
375 static void pcmcia_release_function(struct kref *ref)
377 struct config_t *c = container_of(ref, struct config_t, ref);
378 ds_dbg(1, "releasing config_t\n");
379 kfree(c);
382 static void pcmcia_release_dev(struct device *dev)
384 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
385 ds_dev_dbg(1, dev, "releasing device\n");
386 pcmcia_put_socket(p_dev->socket);
387 kfree(p_dev->devname);
388 kref_put(&p_dev->function_config->ref, pcmcia_release_function);
389 kfree(p_dev);
392 static void pcmcia_add_device_later(struct pcmcia_socket *s, int mfc)
394 if (!s->pcmcia_state.device_add_pending) {
395 ds_dev_dbg(1, &s->dev, "scheduling to add %s secondary"
396 " device to %d\n", mfc ? "mfc" : "pfc", s->sock);
397 s->pcmcia_state.device_add_pending = 1;
398 s->pcmcia_state.mfc_pfc = mfc;
399 schedule_work(&s->device_add);
401 return;
404 static int pcmcia_device_probe(struct device * dev)
406 struct pcmcia_device *p_dev;
407 struct pcmcia_driver *p_drv;
408 struct pcmcia_device_id *did;
409 struct pcmcia_socket *s;
410 cistpl_config_t cis_config;
411 int ret = 0;
413 dev = get_device(dev);
414 if (!dev)
415 return -ENODEV;
417 p_dev = to_pcmcia_dev(dev);
418 p_drv = to_pcmcia_drv(dev->driver);
419 s = p_dev->socket;
421 ds_dev_dbg(1, dev, "trying to bind to %s\n", p_drv->drv.name);
423 if ((!p_drv->probe) || (!p_dev->function_config) ||
424 (!try_module_get(p_drv->owner))) {
425 ret = -EINVAL;
426 goto put_dev;
429 /* set up some more device information */
430 ret = pccard_read_tuple(p_dev->socket, p_dev->func, CISTPL_CONFIG,
431 &cis_config);
432 if (!ret) {
433 p_dev->conf.ConfigBase = cis_config.base;
434 p_dev->conf.Present = cis_config.rmask[0];
435 } else {
436 dev_printk(KERN_INFO, dev,
437 "pcmcia: could not parse base and rmask0 of CIS\n");
438 p_dev->conf.ConfigBase = 0;
439 p_dev->conf.Present = 0;
442 ret = p_drv->probe(p_dev);
443 if (ret) {
444 ds_dev_dbg(1, dev, "binding to %s failed with %d\n",
445 p_drv->drv.name, ret);
446 goto put_module;
449 /* handle pseudo multifunction devices:
450 * there are at most two pseudo multifunction devices.
451 * if we're matching against the first, schedule a
452 * call which will then check whether there are two
453 * pseudo devices, and if not, add the second one.
455 did = p_dev->dev.driver_data;
456 if (did && (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO) &&
457 (p_dev->socket->device_count == 1) && (p_dev->device_no == 0))
458 pcmcia_add_device_later(p_dev->socket, 0);
460 put_module:
461 if (ret)
462 module_put(p_drv->owner);
463 put_dev:
464 if (ret)
465 put_device(dev);
466 return (ret);
471 * Removes a PCMCIA card from the device tree and socket list.
473 static void pcmcia_card_remove(struct pcmcia_socket *s, struct pcmcia_device *leftover)
475 struct pcmcia_device *p_dev;
476 struct pcmcia_device *tmp;
477 unsigned long flags;
479 ds_dev_dbg(2, leftover ? &leftover->dev : &s->dev,
480 "pcmcia_card_remove(%d) %s\n", s->sock,
481 leftover ? leftover->devname : "");
483 if (!leftover)
484 s->device_count = 0;
485 else
486 s->device_count = 1;
488 /* unregister all pcmcia_devices registered with this socket, except leftover */
489 list_for_each_entry_safe(p_dev, tmp, &s->devices_list, socket_device_list) {
490 if (p_dev == leftover)
491 continue;
493 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
494 list_del(&p_dev->socket_device_list);
495 p_dev->_removed=1;
496 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
498 ds_dev_dbg(2, &p_dev->dev, "unregistering device\n");
499 device_unregister(&p_dev->dev);
502 return;
505 static int pcmcia_device_remove(struct device * dev)
507 struct pcmcia_device *p_dev;
508 struct pcmcia_driver *p_drv;
509 struct pcmcia_device_id *did;
510 int i;
512 p_dev = to_pcmcia_dev(dev);
513 p_drv = to_pcmcia_drv(dev->driver);
515 ds_dev_dbg(1, dev, "removing device\n");
517 /* If we're removing the primary module driving a
518 * pseudo multi-function card, we need to unbind
519 * all devices
521 did = p_dev->dev.driver_data;
522 if (did && (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO) &&
523 (p_dev->socket->device_count != 0) &&
524 (p_dev->device_no == 0))
525 pcmcia_card_remove(p_dev->socket, p_dev);
527 /* detach the "instance" */
528 if (!p_drv)
529 return 0;
531 if (p_drv->remove)
532 p_drv->remove(p_dev);
534 p_dev->dev_node = NULL;
536 /* check for proper unloading */
537 if (p_dev->_irq || p_dev->_io || p_dev->_locked)
538 dev_printk(KERN_INFO, dev,
539 "pcmcia: driver %s did not release config properly\n",
540 p_drv->drv.name);
542 for (i = 0; i < MAX_WIN; i++)
543 if (p_dev->_win & CLIENT_WIN_REQ(i))
544 dev_printk(KERN_INFO, dev,
545 "pcmcia: driver %s did not release window properly\n",
546 p_drv->drv.name);
548 /* references from pcmcia_probe_device */
549 pcmcia_put_dev(p_dev);
550 module_put(p_drv->owner);
552 return 0;
557 * pcmcia_device_query -- determine information about a pcmcia device
559 static int pcmcia_device_query(struct pcmcia_device *p_dev)
561 cistpl_manfid_t manf_id;
562 cistpl_funcid_t func_id;
563 cistpl_vers_1_t *vers1;
564 unsigned int i;
566 vers1 = kmalloc(sizeof(*vers1), GFP_KERNEL);
567 if (!vers1)
568 return -ENOMEM;
570 if (!pccard_read_tuple(p_dev->socket, p_dev->func,
571 CISTPL_MANFID, &manf_id)) {
572 p_dev->manf_id = manf_id.manf;
573 p_dev->card_id = manf_id.card;
574 p_dev->has_manf_id = 1;
575 p_dev->has_card_id = 1;
578 if (!pccard_read_tuple(p_dev->socket, p_dev->func,
579 CISTPL_FUNCID, &func_id)) {
580 p_dev->func_id = func_id.func;
581 p_dev->has_func_id = 1;
582 } else {
583 /* rule of thumb: cards with no FUNCID, but with
584 * common memory device geometry information, are
585 * probably memory cards (from pcmcia-cs) */
586 cistpl_device_geo_t *devgeo;
588 devgeo = kmalloc(sizeof(*devgeo), GFP_KERNEL);
589 if (!devgeo) {
590 kfree(vers1);
591 return -ENOMEM;
593 if (!pccard_read_tuple(p_dev->socket, p_dev->func,
594 CISTPL_DEVICE_GEO, devgeo)) {
595 ds_dev_dbg(0, &p_dev->dev,
596 "mem device geometry probably means "
597 "FUNCID_MEMORY\n");
598 p_dev->func_id = CISTPL_FUNCID_MEMORY;
599 p_dev->has_func_id = 1;
601 kfree(devgeo);
604 if (!pccard_read_tuple(p_dev->socket, p_dev->func, CISTPL_VERS_1,
605 vers1)) {
606 for (i=0; i < vers1->ns; i++) {
607 char *tmp;
608 unsigned int length;
610 tmp = vers1->str + vers1->ofs[i];
612 length = strlen(tmp) + 1;
613 if ((length < 2) || (length > 255))
614 continue;
616 p_dev->prod_id[i] = kmalloc(sizeof(char) * length,
617 GFP_KERNEL);
618 if (!p_dev->prod_id[i])
619 continue;
621 p_dev->prod_id[i] = strncpy(p_dev->prod_id[i],
622 tmp, length);
626 kfree(vers1);
627 return 0;
631 /* device_add_lock is needed to avoid double registration by cardmgr and kernel.
632 * Serializes pcmcia_device_add; will most likely be removed in future.
634 * While it has the caveat that adding new PCMCIA devices inside(!) device_register()
635 * won't work, this doesn't matter much at the moment: the driver core doesn't
636 * support it either.
638 static DEFINE_MUTEX(device_add_lock);
640 struct pcmcia_device * pcmcia_device_add(struct pcmcia_socket *s, unsigned int function)
642 struct pcmcia_device *p_dev, *tmp_dev;
643 unsigned long flags;
644 int bus_id_len;
646 s = pcmcia_get_socket(s);
647 if (!s)
648 return NULL;
650 mutex_lock(&device_add_lock);
652 ds_dbg(3, "adding device to %d, function %d\n", s->sock, function);
654 /* max of 4 devices per card */
655 if (s->device_count == 4)
656 goto err_put;
658 p_dev = kzalloc(sizeof(struct pcmcia_device), GFP_KERNEL);
659 if (!p_dev)
660 goto err_put;
662 p_dev->socket = s;
663 p_dev->device_no = (s->device_count++);
664 p_dev->func = function;
666 p_dev->dev.bus = &pcmcia_bus_type;
667 p_dev->dev.parent = s->dev.parent;
668 p_dev->dev.release = pcmcia_release_dev;
669 /* by default don't allow DMA */
670 p_dev->dma_mask = DMA_MASK_NONE;
671 p_dev->dev.dma_mask = &p_dev->dma_mask;
672 bus_id_len = sprintf (p_dev->dev.bus_id, "%d.%d", p_dev->socket->sock, p_dev->device_no);
674 p_dev->devname = kmalloc(6 + bus_id_len + 1, GFP_KERNEL);
675 if (!p_dev->devname)
676 goto err_free;
677 sprintf (p_dev->devname, "pcmcia%s", p_dev->dev.bus_id);
678 ds_dev_dbg(3, &p_dev->dev, "devname is %s\n", p_dev->devname);
680 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
683 * p_dev->function_config must be the same for all card functions.
684 * Note that this is serialized by the device_add_lock, so that
685 * only one such struct will be created.
687 list_for_each_entry(tmp_dev, &s->devices_list, socket_device_list)
688 if (p_dev->func == tmp_dev->func) {
689 p_dev->function_config = tmp_dev->function_config;
690 kref_get(&p_dev->function_config->ref);
693 /* Add to the list in pcmcia_bus_socket */
694 list_add(&p_dev->socket_device_list, &s->devices_list);
696 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
698 if (!p_dev->function_config) {
699 ds_dev_dbg(3, &p_dev->dev, "creating config_t\n");
700 p_dev->function_config = kzalloc(sizeof(struct config_t),
701 GFP_KERNEL);
702 if (!p_dev->function_config)
703 goto err_unreg;
704 kref_init(&p_dev->function_config->ref);
707 dev_printk(KERN_NOTICE, &p_dev->dev,
708 "pcmcia: registering new device %s\n",
709 p_dev->devname);
711 pcmcia_device_query(p_dev);
713 if (device_register(&p_dev->dev))
714 goto err_unreg;
716 mutex_unlock(&device_add_lock);
718 return p_dev;
720 err_unreg:
721 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
722 list_del(&p_dev->socket_device_list);
723 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
725 err_free:
726 kfree(p_dev->devname);
727 kfree(p_dev);
728 s->device_count--;
729 err_put:
730 mutex_unlock(&device_add_lock);
731 pcmcia_put_socket(s);
733 return NULL;
737 static int pcmcia_card_add(struct pcmcia_socket *s)
739 cistpl_longlink_mfc_t mfc;
740 unsigned int no_funcs, i, no_chains;
741 int ret = 0;
743 if (!(s->resource_setup_done)) {
744 ds_dev_dbg(3, &s->dev,
745 "no resources available, delaying card_add\n");
746 return -EAGAIN; /* try again, but later... */
749 if (pcmcia_validate_mem(s)) {
750 ds_dev_dbg(3, &s->dev, "validating mem resources failed, "
751 "delaying card_add\n");
752 return -EAGAIN; /* try again, but later... */
755 ret = pccard_validate_cis(s, BIND_FN_ALL, &no_chains);
756 if (ret || !no_chains) {
757 ds_dev_dbg(0, &s->dev, "invalid CIS or invalid resources\n");
758 return -ENODEV;
761 if (!pccard_read_tuple(s, BIND_FN_ALL, CISTPL_LONGLINK_MFC, &mfc))
762 no_funcs = mfc.nfn;
763 else
764 no_funcs = 1;
765 s->functions = no_funcs;
767 for (i=0; i < no_funcs; i++)
768 pcmcia_device_add(s, i);
770 return (ret);
774 static void pcmcia_delayed_add_device(struct work_struct *work)
776 struct pcmcia_socket *s =
777 container_of(work, struct pcmcia_socket, device_add);
778 ds_dev_dbg(1, &s->dev, "adding additional device to %d\n", s->sock);
779 pcmcia_device_add(s, s->pcmcia_state.mfc_pfc);
780 s->pcmcia_state.device_add_pending = 0;
781 s->pcmcia_state.mfc_pfc = 0;
784 static int pcmcia_requery(struct device *dev, void * _data)
786 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
787 if (!p_dev->dev.driver) {
788 ds_dev_dbg(1, dev, "update device information\n");
789 pcmcia_device_query(p_dev);
792 return 0;
795 static void pcmcia_bus_rescan(struct pcmcia_socket *skt, int new_cis)
797 int no_devices = 0;
798 int ret = 0;
799 unsigned long flags;
801 /* must be called with skt_mutex held */
802 ds_dev_dbg(0, &skt->dev, "re-scanning socket %d\n", skt->sock);
804 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
805 if (list_empty(&skt->devices_list))
806 no_devices = 1;
807 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
809 /* If this is because of a CIS override, start over */
810 if (new_cis && !no_devices)
811 pcmcia_card_remove(skt, NULL);
813 /* if no devices were added for this socket yet because of
814 * missing resource information or other trouble, we need to
815 * do this now. */
816 if (no_devices || new_cis) {
817 ret = pcmcia_card_add(skt);
818 if (ret)
819 return;
822 /* some device information might have changed because of a CIS
823 * update or because we can finally read it correctly... so
824 * determine it again, overwriting old values if necessary. */
825 bus_for_each_dev(&pcmcia_bus_type, NULL, NULL, pcmcia_requery);
827 /* we re-scan all devices, not just the ones connected to this
828 * socket. This does not matter, though. */
829 ret = bus_rescan_devices(&pcmcia_bus_type);
830 if (ret)
831 printk(KERN_INFO "pcmcia: bus_rescan_devices failed\n");
834 #ifdef CONFIG_PCMCIA_LOAD_CIS
837 * pcmcia_load_firmware - load CIS from userspace if device-provided is broken
838 * @dev: the pcmcia device which needs a CIS override
839 * @filename: requested filename in /lib/firmware/
841 * This uses the in-kernel firmware loading mechanism to use a "fake CIS" if
842 * the one provided by the card is broken. The firmware files reside in
843 * /lib/firmware/ in userspace.
845 static int pcmcia_load_firmware(struct pcmcia_device *dev, char * filename)
847 struct pcmcia_socket *s = dev->socket;
848 const struct firmware *fw;
849 char path[FIRMWARE_NAME_MAX];
850 int ret = -ENOMEM;
851 int no_funcs;
852 int old_funcs;
853 cistpl_longlink_mfc_t mfc;
855 if (!filename)
856 return -EINVAL;
858 ds_dev_dbg(1, &dev->dev, "trying to load CIS file %s\n", filename);
860 if (strlen(filename) > (FIRMWARE_NAME_MAX - 1)) {
861 dev_printk(KERN_WARNING, &dev->dev,
862 "pcmcia: CIS filename is too long [%s]\n",
863 filename);
864 return -EINVAL;
867 snprintf(path, sizeof(path), "%s", filename);
869 if (request_firmware(&fw, path, &dev->dev) == 0) {
870 if (fw->size >= CISTPL_MAX_CIS_SIZE) {
871 ret = -EINVAL;
872 dev_printk(KERN_ERR, &dev->dev,
873 "pcmcia: CIS override is too big\n");
874 goto release;
877 if (!pcmcia_replace_cis(s, fw->data, fw->size))
878 ret = 0;
879 else {
880 dev_printk(KERN_ERR, &dev->dev,
881 "pcmcia: CIS override failed\n");
882 goto release;
886 /* update information */
887 pcmcia_device_query(dev);
889 /* does this cis override add or remove functions? */
890 old_funcs = s->functions;
892 if (!pccard_read_tuple(s, BIND_FN_ALL, CISTPL_LONGLINK_MFC, &mfc))
893 no_funcs = mfc.nfn;
894 else
895 no_funcs = 1;
896 s->functions = no_funcs;
898 if (old_funcs > no_funcs)
899 pcmcia_card_remove(s, dev);
900 else if (no_funcs > old_funcs)
901 pcmcia_add_device_later(s, 1);
903 release:
904 release_firmware(fw);
906 return (ret);
909 #else /* !CONFIG_PCMCIA_LOAD_CIS */
911 static inline int pcmcia_load_firmware(struct pcmcia_device *dev, char * filename)
913 return -ENODEV;
916 #endif
919 static inline int pcmcia_devmatch(struct pcmcia_device *dev,
920 struct pcmcia_device_id *did)
922 if (did->match_flags & PCMCIA_DEV_ID_MATCH_MANF_ID) {
923 if ((!dev->has_manf_id) || (dev->manf_id != did->manf_id))
924 return 0;
927 if (did->match_flags & PCMCIA_DEV_ID_MATCH_CARD_ID) {
928 if ((!dev->has_card_id) || (dev->card_id != did->card_id))
929 return 0;
932 if (did->match_flags & PCMCIA_DEV_ID_MATCH_FUNCTION) {
933 if (dev->func != did->function)
934 return 0;
937 if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID1) {
938 if (!dev->prod_id[0])
939 return 0;
940 if (strcmp(did->prod_id[0], dev->prod_id[0]))
941 return 0;
944 if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID2) {
945 if (!dev->prod_id[1])
946 return 0;
947 if (strcmp(did->prod_id[1], dev->prod_id[1]))
948 return 0;
951 if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID3) {
952 if (!dev->prod_id[2])
953 return 0;
954 if (strcmp(did->prod_id[2], dev->prod_id[2]))
955 return 0;
958 if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID4) {
959 if (!dev->prod_id[3])
960 return 0;
961 if (strcmp(did->prod_id[3], dev->prod_id[3]))
962 return 0;
965 if (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO) {
966 if (dev->device_no != did->device_no)
967 return 0;
970 if (did->match_flags & PCMCIA_DEV_ID_MATCH_FUNC_ID) {
971 if ((!dev->has_func_id) || (dev->func_id != did->func_id))
972 return 0;
974 /* if this is a pseudo-multi-function device,
975 * we need explicit matches */
976 if (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO)
977 return 0;
978 if (dev->device_no)
979 return 0;
981 /* also, FUNC_ID matching needs to be activated by userspace
982 * after it has re-checked that there is no possible module
983 * with a prod_id/manf_id/card_id match.
985 ds_dev_dbg(0, &dev->dev,
986 "skipping FUNC_ID match until userspace interaction\n");
987 if (!dev->allow_func_id_match)
988 return 0;
991 if (did->match_flags & PCMCIA_DEV_ID_MATCH_FAKE_CIS) {
992 ds_dev_dbg(0, &dev->dev, "device needs a fake CIS\n");
993 if (!dev->socket->fake_cis)
994 pcmcia_load_firmware(dev, did->cisfile);
996 if (!dev->socket->fake_cis)
997 return 0;
1000 if (did->match_flags & PCMCIA_DEV_ID_MATCH_ANONYMOUS) {
1001 int i;
1002 for (i=0; i<4; i++)
1003 if (dev->prod_id[i])
1004 return 0;
1005 if (dev->has_manf_id || dev->has_card_id || dev->has_func_id)
1006 return 0;
1009 dev->dev.driver_data = (void *) did;
1011 return 1;
1015 static int pcmcia_bus_match(struct device * dev, struct device_driver * drv) {
1016 struct pcmcia_device * p_dev = to_pcmcia_dev(dev);
1017 struct pcmcia_driver * p_drv = to_pcmcia_drv(drv);
1018 struct pcmcia_device_id *did = p_drv->id_table;
1019 struct pcmcia_dynid *dynid;
1021 /* match dynamic devices first */
1022 spin_lock(&p_drv->dynids.lock);
1023 list_for_each_entry(dynid, &p_drv->dynids.list, node) {
1024 ds_dev_dbg(3, dev, "trying to match to %s\n", drv->name);
1025 if (pcmcia_devmatch(p_dev, &dynid->id)) {
1026 ds_dev_dbg(0, dev, "matched to %s\n", drv->name);
1027 spin_unlock(&p_drv->dynids.lock);
1028 return 1;
1031 spin_unlock(&p_drv->dynids.lock);
1033 #ifdef CONFIG_PCMCIA_IOCTL
1034 /* matching by cardmgr */
1035 if (p_dev->cardmgr == p_drv) {
1036 ds_dev_dbg(0, dev, "cardmgr matched to %s\n", drv->name);
1037 return 1;
1039 #endif
1041 while (did && did->match_flags) {
1042 ds_dev_dbg(3, dev, "trying to match to %s\n", drv->name);
1043 if (pcmcia_devmatch(p_dev, did)) {
1044 ds_dev_dbg(0, dev, "matched to %s\n", drv->name);
1045 return 1;
1047 did++;
1050 return 0;
1053 #ifdef CONFIG_HOTPLUG
1055 static int pcmcia_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
1057 struct pcmcia_device *p_dev;
1058 int i;
1059 u32 hash[4] = { 0, 0, 0, 0};
1061 if (!dev)
1062 return -ENODEV;
1064 p_dev = to_pcmcia_dev(dev);
1066 /* calculate hashes */
1067 for (i=0; i<4; i++) {
1068 if (!p_dev->prod_id[i])
1069 continue;
1070 hash[i] = crc32(0, p_dev->prod_id[i], strlen(p_dev->prod_id[i]));
1073 if (add_uevent_var(env, "SOCKET_NO=%u", p_dev->socket->sock))
1074 return -ENOMEM;
1076 if (add_uevent_var(env, "DEVICE_NO=%02X", p_dev->device_no))
1077 return -ENOMEM;
1079 if (add_uevent_var(env, "MODALIAS=pcmcia:m%04Xc%04Xf%02Xfn%02Xpfn%02X"
1080 "pa%08Xpb%08Xpc%08Xpd%08X",
1081 p_dev->has_manf_id ? p_dev->manf_id : 0,
1082 p_dev->has_card_id ? p_dev->card_id : 0,
1083 p_dev->has_func_id ? p_dev->func_id : 0,
1084 p_dev->func,
1085 p_dev->device_no,
1086 hash[0],
1087 hash[1],
1088 hash[2],
1089 hash[3]))
1090 return -ENOMEM;
1092 return 0;
1095 #else
1097 static int pcmcia_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
1099 return -ENODEV;
1102 #endif
1104 /************************ runtime PM support ***************************/
1106 static int pcmcia_dev_suspend(struct device *dev, pm_message_t state);
1107 static int pcmcia_dev_resume(struct device *dev);
1109 static int runtime_suspend(struct device *dev)
1111 int rc;
1113 down(&dev->sem);
1114 rc = pcmcia_dev_suspend(dev, PMSG_SUSPEND);
1115 up(&dev->sem);
1116 return rc;
1119 static void runtime_resume(struct device *dev)
1121 int rc;
1123 down(&dev->sem);
1124 rc = pcmcia_dev_resume(dev);
1125 up(&dev->sem);
1128 /************************ per-device sysfs output ***************************/
1130 #define pcmcia_device_attr(field, test, format) \
1131 static ssize_t field##_show (struct device *dev, struct device_attribute *attr, char *buf) \
1133 struct pcmcia_device *p_dev = to_pcmcia_dev(dev); \
1134 return p_dev->test ? sprintf (buf, format, p_dev->field) : -ENODEV; \
1137 #define pcmcia_device_stringattr(name, field) \
1138 static ssize_t name##_show (struct device *dev, struct device_attribute *attr, char *buf) \
1140 struct pcmcia_device *p_dev = to_pcmcia_dev(dev); \
1141 return p_dev->field ? sprintf (buf, "%s\n", p_dev->field) : -ENODEV; \
1144 pcmcia_device_attr(func, socket, "0x%02x\n");
1145 pcmcia_device_attr(func_id, has_func_id, "0x%02x\n");
1146 pcmcia_device_attr(manf_id, has_manf_id, "0x%04x\n");
1147 pcmcia_device_attr(card_id, has_card_id, "0x%04x\n");
1148 pcmcia_device_stringattr(prod_id1, prod_id[0]);
1149 pcmcia_device_stringattr(prod_id2, prod_id[1]);
1150 pcmcia_device_stringattr(prod_id3, prod_id[2]);
1151 pcmcia_device_stringattr(prod_id4, prod_id[3]);
1154 static ssize_t pcmcia_show_pm_state(struct device *dev, struct device_attribute *attr, char *buf)
1156 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1158 if (p_dev->suspended)
1159 return sprintf(buf, "off\n");
1160 else
1161 return sprintf(buf, "on\n");
1164 static ssize_t pcmcia_store_pm_state(struct device *dev, struct device_attribute *attr,
1165 const char *buf, size_t count)
1167 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1168 int ret = 0;
1170 if (!count)
1171 return -EINVAL;
1173 if ((!p_dev->suspended) && !strncmp(buf, "off", 3))
1174 ret = runtime_suspend(dev);
1175 else if (p_dev->suspended && !strncmp(buf, "on", 2))
1176 runtime_resume(dev);
1178 return ret ? ret : count;
1182 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
1184 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1185 int i;
1186 u32 hash[4] = { 0, 0, 0, 0};
1188 /* calculate hashes */
1189 for (i=0; i<4; i++) {
1190 if (!p_dev->prod_id[i])
1191 continue;
1192 hash[i] = crc32(0,p_dev->prod_id[i],strlen(p_dev->prod_id[i]));
1194 return sprintf(buf, "pcmcia:m%04Xc%04Xf%02Xfn%02Xpfn%02X"
1195 "pa%08Xpb%08Xpc%08Xpd%08X\n",
1196 p_dev->has_manf_id ? p_dev->manf_id : 0,
1197 p_dev->has_card_id ? p_dev->card_id : 0,
1198 p_dev->has_func_id ? p_dev->func_id : 0,
1199 p_dev->func, p_dev->device_no,
1200 hash[0], hash[1], hash[2], hash[3]);
1203 static ssize_t pcmcia_store_allow_func_id_match(struct device *dev,
1204 struct device_attribute *attr, const char *buf, size_t count)
1206 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1207 int ret;
1209 if (!count)
1210 return -EINVAL;
1212 mutex_lock(&p_dev->socket->skt_mutex);
1213 p_dev->allow_func_id_match = 1;
1214 mutex_unlock(&p_dev->socket->skt_mutex);
1216 ret = bus_rescan_devices(&pcmcia_bus_type);
1217 if (ret)
1218 printk(KERN_INFO "pcmcia: bus_rescan_devices failed after "
1219 "allowing func_id matches\n");
1221 return count;
1224 static struct device_attribute pcmcia_dev_attrs[] = {
1225 __ATTR(function, 0444, func_show, NULL),
1226 __ATTR(pm_state, 0644, pcmcia_show_pm_state, pcmcia_store_pm_state),
1227 __ATTR_RO(func_id),
1228 __ATTR_RO(manf_id),
1229 __ATTR_RO(card_id),
1230 __ATTR_RO(prod_id1),
1231 __ATTR_RO(prod_id2),
1232 __ATTR_RO(prod_id3),
1233 __ATTR_RO(prod_id4),
1234 __ATTR_RO(modalias),
1235 __ATTR(allow_func_id_match, 0200, NULL, pcmcia_store_allow_func_id_match),
1236 __ATTR_NULL,
1239 /* PM support, also needed for reset */
1241 static int pcmcia_dev_suspend(struct device * dev, pm_message_t state)
1243 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1244 struct pcmcia_driver *p_drv = NULL;
1245 int ret = 0;
1247 if (p_dev->suspended)
1248 return 0;
1250 ds_dev_dbg(2, dev, "suspending\n");
1252 if (dev->driver)
1253 p_drv = to_pcmcia_drv(dev->driver);
1255 if (!p_drv)
1256 goto out;
1258 if (p_drv->suspend) {
1259 ret = p_drv->suspend(p_dev);
1260 if (ret) {
1261 dev_printk(KERN_ERR, dev,
1262 "pcmcia: device %s (driver %s) did "
1263 "not want to go to sleep (%d)\n",
1264 p_dev->devname, p_drv->drv.name, ret);
1265 goto out;
1269 if (p_dev->device_no == p_dev->func) {
1270 ds_dev_dbg(2, dev, "releasing configuration\n");
1271 pcmcia_release_configuration(p_dev);
1274 out:
1275 if (!ret)
1276 p_dev->suspended = 1;
1277 return ret;
1281 static int pcmcia_dev_resume(struct device * dev)
1283 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1284 struct pcmcia_driver *p_drv = NULL;
1285 int ret = 0;
1287 if (!p_dev->suspended)
1288 return 0;
1290 ds_dev_dbg(2, dev, "resuming\n");
1292 if (dev->driver)
1293 p_drv = to_pcmcia_drv(dev->driver);
1295 if (!p_drv)
1296 goto out;
1298 if (p_dev->device_no == p_dev->func) {
1299 ds_dev_dbg(2, dev, "requesting configuration\n");
1300 ret = pcmcia_request_configuration(p_dev, &p_dev->conf);
1301 if (ret)
1302 goto out;
1305 if (p_drv->resume)
1306 ret = p_drv->resume(p_dev);
1308 out:
1309 if (!ret)
1310 p_dev->suspended = 0;
1311 return ret;
1315 static int pcmcia_bus_suspend_callback(struct device *dev, void * _data)
1317 struct pcmcia_socket *skt = _data;
1318 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1320 if (p_dev->socket != skt || p_dev->suspended)
1321 return 0;
1323 return runtime_suspend(dev);
1326 static int pcmcia_bus_resume_callback(struct device *dev, void * _data)
1328 struct pcmcia_socket *skt = _data;
1329 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1331 if (p_dev->socket != skt || !p_dev->suspended)
1332 return 0;
1334 runtime_resume(dev);
1336 return 0;
1339 static int pcmcia_bus_resume(struct pcmcia_socket *skt)
1341 ds_dev_dbg(2, &skt->dev, "resuming socket %d\n", skt->sock);
1342 bus_for_each_dev(&pcmcia_bus_type, NULL, skt, pcmcia_bus_resume_callback);
1343 return 0;
1346 static int pcmcia_bus_suspend(struct pcmcia_socket *skt)
1348 ds_dev_dbg(2, &skt->dev, "suspending socket %d\n", skt->sock);
1349 if (bus_for_each_dev(&pcmcia_bus_type, NULL, skt,
1350 pcmcia_bus_suspend_callback)) {
1351 pcmcia_bus_resume(skt);
1352 return -EIO;
1354 return 0;
1358 /*======================================================================
1360 The card status event handler.
1362 ======================================================================*/
1364 /* Normally, the event is passed to individual drivers after
1365 * informing userspace. Only for CS_EVENT_CARD_REMOVAL this
1366 * is inversed to maintain historic compatibility.
1369 static int ds_event(struct pcmcia_socket *skt, event_t event, int priority)
1371 struct pcmcia_socket *s = pcmcia_get_socket(skt);
1373 if (!s) {
1374 dev_printk(KERN_ERR, &skt->dev,
1375 "PCMCIA obtaining reference to socket " \
1376 "failed, event 0x%x lost!\n", event);
1377 return -ENODEV;
1380 ds_dev_dbg(1, &skt->dev, "ds_event(0x%06x, %d, 0x%p)\n",
1381 event, priority, skt);
1383 switch (event) {
1384 case CS_EVENT_CARD_REMOVAL:
1385 s->pcmcia_state.present = 0;
1386 pcmcia_card_remove(skt, NULL);
1387 handle_event(skt, event);
1388 break;
1390 case CS_EVENT_CARD_INSERTION:
1391 s->pcmcia_state.present = 1;
1392 pcmcia_card_add(skt);
1393 handle_event(skt, event);
1394 break;
1396 case CS_EVENT_EJECTION_REQUEST:
1397 break;
1399 case CS_EVENT_PM_SUSPEND:
1400 case CS_EVENT_PM_RESUME:
1401 case CS_EVENT_RESET_PHYSICAL:
1402 case CS_EVENT_CARD_RESET:
1403 default:
1404 handle_event(skt, event);
1405 break;
1408 pcmcia_put_socket(s);
1410 return 0;
1411 } /* ds_event */
1414 struct pcmcia_device * pcmcia_dev_present(struct pcmcia_device *_p_dev)
1416 struct pcmcia_device *p_dev;
1417 struct pcmcia_device *ret = NULL;
1419 p_dev = pcmcia_get_dev(_p_dev);
1420 if (!p_dev)
1421 return NULL;
1423 if (!p_dev->socket->pcmcia_state.present)
1424 goto out;
1426 if (p_dev->_removed)
1427 goto out;
1429 if (p_dev->suspended)
1430 goto out;
1432 ret = p_dev;
1433 out:
1434 pcmcia_put_dev(p_dev);
1435 return ret;
1437 EXPORT_SYMBOL(pcmcia_dev_present);
1440 static struct pcmcia_callback pcmcia_bus_callback = {
1441 .owner = THIS_MODULE,
1442 .event = ds_event,
1443 .requery = pcmcia_bus_rescan,
1444 .suspend = pcmcia_bus_suspend,
1445 .resume = pcmcia_bus_resume,
1448 static int __devinit pcmcia_bus_add_socket(struct device *dev,
1449 struct class_interface *class_intf)
1451 struct pcmcia_socket *socket = dev_get_drvdata(dev);
1452 int ret;
1454 socket = pcmcia_get_socket(socket);
1455 if (!socket) {
1456 dev_printk(KERN_ERR, dev,
1457 "PCMCIA obtaining reference to socket failed\n");
1458 return -ENODEV;
1462 * Ugly. But we want to wait for the socket threads to have started up.
1463 * We really should let the drivers themselves drive some of this..
1465 msleep(250);
1467 #ifdef CONFIG_PCMCIA_IOCTL
1468 init_waitqueue_head(&socket->queue);
1469 #endif
1470 INIT_LIST_HEAD(&socket->devices_list);
1471 INIT_WORK(&socket->device_add, pcmcia_delayed_add_device);
1472 memset(&socket->pcmcia_state, 0, sizeof(u8));
1473 socket->device_count = 0;
1475 ret = pccard_register_pcmcia(socket, &pcmcia_bus_callback);
1476 if (ret) {
1477 dev_printk(KERN_ERR, dev, "PCMCIA registration failed\n");
1478 pcmcia_put_socket(socket);
1479 return (ret);
1482 return 0;
1485 static void pcmcia_bus_remove_socket(struct device *dev,
1486 struct class_interface *class_intf)
1488 struct pcmcia_socket *socket = dev_get_drvdata(dev);
1490 if (!socket)
1491 return;
1493 socket->pcmcia_state.dead = 1;
1494 pccard_register_pcmcia(socket, NULL);
1496 /* unregister any unbound devices */
1497 mutex_lock(&socket->skt_mutex);
1498 pcmcia_card_remove(socket, NULL);
1499 mutex_unlock(&socket->skt_mutex);
1501 pcmcia_put_socket(socket);
1503 return;
1507 /* the pcmcia_bus_interface is used to handle pcmcia socket devices */
1508 static struct class_interface pcmcia_bus_interface __refdata = {
1509 .class = &pcmcia_socket_class,
1510 .add_dev = &pcmcia_bus_add_socket,
1511 .remove_dev = &pcmcia_bus_remove_socket,
1515 struct bus_type pcmcia_bus_type = {
1516 .name = "pcmcia",
1517 .uevent = pcmcia_bus_uevent,
1518 .match = pcmcia_bus_match,
1519 .dev_attrs = pcmcia_dev_attrs,
1520 .probe = pcmcia_device_probe,
1521 .remove = pcmcia_device_remove,
1522 .suspend = pcmcia_dev_suspend,
1523 .resume = pcmcia_dev_resume,
1527 static int __init init_pcmcia_bus(void)
1529 int ret;
1531 spin_lock_init(&pcmcia_dev_list_lock);
1533 ret = bus_register(&pcmcia_bus_type);
1534 if (ret < 0) {
1535 printk(KERN_WARNING "pcmcia: bus_register error: %d\n", ret);
1536 return ret;
1538 ret = class_interface_register(&pcmcia_bus_interface);
1539 if (ret < 0) {
1540 printk(KERN_WARNING
1541 "pcmcia: class_interface_register error: %d\n", ret);
1542 bus_unregister(&pcmcia_bus_type);
1543 return ret;
1546 pcmcia_setup_ioctl();
1548 return 0;
1550 fs_initcall(init_pcmcia_bus); /* one level after subsys_initcall so that
1551 * pcmcia_socket_class is already registered */
1554 static void __exit exit_pcmcia_bus(void)
1556 pcmcia_cleanup_ioctl();
1558 class_interface_unregister(&pcmcia_bus_interface);
1560 bus_unregister(&pcmcia_bus_type);
1562 module_exit(exit_pcmcia_bus);
1565 MODULE_ALIAS("ds");