ASoC: Fix handling of bias levels for non-DAPM codecs
[linux/fpc-iii.git] / drivers / pcmcia / ds.c
blob304ff6d5cf3b2ec13850a253bc908481027cd844
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");
44 #ifdef CONFIG_PCMCIA_DEBUG
45 int ds_pc_debug;
47 module_param_named(pc_debug, ds_pc_debug, int, 0644);
49 #define ds_dbg(lvl, fmt, arg...) do { \
50 if (ds_pc_debug > (lvl)) \
51 printk(KERN_DEBUG "ds: " fmt , ## arg); \
52 } while (0)
53 #define ds_dev_dbg(lvl, dev, fmt, arg...) do { \
54 if (ds_pc_debug > (lvl)) \
55 dev_printk(KERN_DEBUG, dev, "ds: " fmt , ## arg); \
56 } while (0)
57 #else
58 #define ds_dbg(lvl, fmt, arg...) do { } while (0)
59 #define ds_dev_dbg(lvl, dev, fmt, arg...) do { } while (0)
60 #endif
62 spinlock_t pcmcia_dev_list_lock;
64 /*====================================================================*/
66 /* code which was in cs.c before */
68 /* String tables for error messages */
70 typedef struct lookup_t {
71 const int key;
72 const char *msg;
73 } lookup_t;
75 static const lookup_t error_table[] = {
76 { 0, "Operation succeeded" },
77 { -EIO, "Input/Output error" },
78 { -ENODEV, "No card present" },
79 { -EINVAL, "Bad parameter" },
80 { -EACCES, "Configuration locked" },
81 { -EBUSY, "Resource in use" },
82 { -ENOSPC, "No more items" },
83 { -ENOMEM, "Out of resource" },
87 static const lookup_t service_table[] = {
88 { AccessConfigurationRegister, "AccessConfigurationRegister" },
89 { AddSocketServices, "AddSocketServices" },
90 { AdjustResourceInfo, "AdjustResourceInfo" },
91 { CheckEraseQueue, "CheckEraseQueue" },
92 { CloseMemory, "CloseMemory" },
93 { DeregisterClient, "DeregisterClient" },
94 { DeregisterEraseQueue, "DeregisterEraseQueue" },
95 { GetCardServicesInfo, "GetCardServicesInfo" },
96 { GetClientInfo, "GetClientInfo" },
97 { GetConfigurationInfo, "GetConfigurationInfo" },
98 { GetEventMask, "GetEventMask" },
99 { GetFirstClient, "GetFirstClient" },
100 { GetFirstRegion, "GetFirstRegion" },
101 { GetFirstTuple, "GetFirstTuple" },
102 { GetNextClient, "GetNextClient" },
103 { GetNextRegion, "GetNextRegion" },
104 { GetNextTuple, "GetNextTuple" },
105 { GetStatus, "GetStatus" },
106 { GetTupleData, "GetTupleData" },
107 { MapMemPage, "MapMemPage" },
108 { ModifyConfiguration, "ModifyConfiguration" },
109 { ModifyWindow, "ModifyWindow" },
110 { OpenMemory, "OpenMemory" },
111 { ParseTuple, "ParseTuple" },
112 { ReadMemory, "ReadMemory" },
113 { RegisterClient, "RegisterClient" },
114 { RegisterEraseQueue, "RegisterEraseQueue" },
115 { RegisterMTD, "RegisterMTD" },
116 { ReleaseConfiguration, "ReleaseConfiguration" },
117 { ReleaseIO, "ReleaseIO" },
118 { ReleaseIRQ, "ReleaseIRQ" },
119 { ReleaseWindow, "ReleaseWindow" },
120 { RequestConfiguration, "RequestConfiguration" },
121 { RequestIO, "RequestIO" },
122 { RequestIRQ, "RequestIRQ" },
123 { RequestSocketMask, "RequestSocketMask" },
124 { RequestWindow, "RequestWindow" },
125 { ResetCard, "ResetCard" },
126 { SetEventMask, "SetEventMask" },
127 { ValidateCIS, "ValidateCIS" },
128 { WriteMemory, "WriteMemory" },
129 { BindDevice, "BindDevice" },
130 { BindMTD, "BindMTD" },
131 { ReportError, "ReportError" },
132 { SuspendCard, "SuspendCard" },
133 { ResumeCard, "ResumeCard" },
134 { EjectCard, "EjectCard" },
135 { InsertCard, "InsertCard" },
136 { ReplaceCIS, "ReplaceCIS" }
139 const char *pcmcia_error_func(int func)
141 int i;
143 for (i = 0; i < ARRAY_SIZE(service_table); i++)
144 if (service_table[i].key == func)
145 return service_table[i].msg;
147 return "Unknown service number";
149 EXPORT_SYMBOL(pcmcia_error_func);
151 const char *pcmcia_error_ret(int ret)
153 int i;
155 for (i = 0; i < ARRAY_SIZE(error_table); i++)
156 if (error_table[i].key == ret)
157 return error_table[i].msg;
159 return "unknown";
161 EXPORT_SYMBOL(pcmcia_error_ret);
163 /*======================================================================*/
167 static void pcmcia_check_driver(struct pcmcia_driver *p_drv)
169 struct pcmcia_device_id *did = p_drv->id_table;
170 unsigned int i;
171 u32 hash;
173 if (!p_drv->probe || !p_drv->remove)
174 printk(KERN_DEBUG "pcmcia: %s lacks a requisite callback "
175 "function\n", p_drv->drv.name);
177 while (did && did->match_flags) {
178 for (i=0; i<4; i++) {
179 if (!did->prod_id[i])
180 continue;
182 hash = crc32(0, did->prod_id[i], strlen(did->prod_id[i]));
183 if (hash == did->prod_id_hash[i])
184 continue;
186 printk(KERN_DEBUG "pcmcia: %s: invalid hash for "
187 "product string \"%s\": is 0x%x, should "
188 "be 0x%x\n", p_drv->drv.name, did->prod_id[i],
189 did->prod_id_hash[i], hash);
190 printk(KERN_DEBUG "pcmcia: see "
191 "Documentation/pcmcia/devicetable.txt for "
192 "details\n");
194 did++;
197 return;
201 /*======================================================================*/
204 struct pcmcia_dynid {
205 struct list_head node;
206 struct pcmcia_device_id id;
210 * pcmcia_store_new_id - add a new PCMCIA device ID to this driver and re-probe devices
211 * @driver: target device driver
212 * @buf: buffer for scanning device ID data
213 * @count: input size
215 * Adds a new dynamic PCMCIA device ID to this driver,
216 * and causes the driver to probe for all devices again.
218 static ssize_t
219 pcmcia_store_new_id(struct device_driver *driver, const char *buf, size_t count)
221 struct pcmcia_dynid *dynid;
222 struct pcmcia_driver *pdrv = to_pcmcia_drv(driver);
223 __u16 match_flags, manf_id, card_id;
224 __u8 func_id, function, device_no;
225 __u32 prod_id_hash[4] = {0, 0, 0, 0};
226 int fields=0;
227 int retval = 0;
229 fields = sscanf(buf, "%hx %hx %hx %hhx %hhx %hhx %x %x %x %x",
230 &match_flags, &manf_id, &card_id, &func_id, &function, &device_no,
231 &prod_id_hash[0], &prod_id_hash[1], &prod_id_hash[2], &prod_id_hash[3]);
232 if (fields < 6)
233 return -EINVAL;
235 dynid = kzalloc(sizeof(struct pcmcia_dynid), GFP_KERNEL);
236 if (!dynid)
237 return -ENOMEM;
239 INIT_LIST_HEAD(&dynid->node);
240 dynid->id.match_flags = match_flags;
241 dynid->id.manf_id = manf_id;
242 dynid->id.card_id = card_id;
243 dynid->id.func_id = func_id;
244 dynid->id.function = function;
245 dynid->id.device_no = device_no;
246 memcpy(dynid->id.prod_id_hash, prod_id_hash, sizeof(__u32) * 4);
248 spin_lock(&pdrv->dynids.lock);
249 list_add_tail(&pdrv->dynids.list, &dynid->node);
250 spin_unlock(&pdrv->dynids.lock);
252 if (get_driver(&pdrv->drv)) {
253 retval = driver_attach(&pdrv->drv);
254 put_driver(&pdrv->drv);
257 if (retval)
258 return retval;
259 return count;
261 static DRIVER_ATTR(new_id, S_IWUSR, NULL, pcmcia_store_new_id);
263 static void
264 pcmcia_free_dynids(struct pcmcia_driver *drv)
266 struct pcmcia_dynid *dynid, *n;
268 spin_lock(&drv->dynids.lock);
269 list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
270 list_del(&dynid->node);
271 kfree(dynid);
273 spin_unlock(&drv->dynids.lock);
276 static int
277 pcmcia_create_newid_file(struct pcmcia_driver *drv)
279 int error = 0;
280 if (drv->probe != NULL)
281 error = driver_create_file(&drv->drv, &driver_attr_new_id);
282 return error;
287 * pcmcia_register_driver - register a PCMCIA driver with the bus core
288 * @driver: the &driver being registered
290 * Registers a PCMCIA driver with the PCMCIA bus core.
292 int pcmcia_register_driver(struct pcmcia_driver *driver)
294 int error;
296 if (!driver)
297 return -EINVAL;
299 pcmcia_check_driver(driver);
301 /* initialize common fields */
302 driver->drv.bus = &pcmcia_bus_type;
303 driver->drv.owner = driver->owner;
304 spin_lock_init(&driver->dynids.lock);
305 INIT_LIST_HEAD(&driver->dynids.list);
307 ds_dbg(3, "registering driver %s\n", driver->drv.name);
309 error = driver_register(&driver->drv);
310 if (error < 0)
311 return error;
313 error = pcmcia_create_newid_file(driver);
314 if (error)
315 driver_unregister(&driver->drv);
317 return error;
319 EXPORT_SYMBOL(pcmcia_register_driver);
322 * pcmcia_unregister_driver - unregister a PCMCIA driver with the bus core
323 * @driver: the &driver being unregistered
325 void pcmcia_unregister_driver(struct pcmcia_driver *driver)
327 ds_dbg(3, "unregistering driver %s\n", driver->drv.name);
328 driver_unregister(&driver->drv);
329 pcmcia_free_dynids(driver);
331 EXPORT_SYMBOL(pcmcia_unregister_driver);
334 /* pcmcia_device handling */
336 struct pcmcia_device * pcmcia_get_dev(struct pcmcia_device *p_dev)
338 struct device *tmp_dev;
339 tmp_dev = get_device(&p_dev->dev);
340 if (!tmp_dev)
341 return NULL;
342 return to_pcmcia_dev(tmp_dev);
345 void pcmcia_put_dev(struct pcmcia_device *p_dev)
347 if (p_dev)
348 put_device(&p_dev->dev);
351 static void pcmcia_release_function(struct kref *ref)
353 struct config_t *c = container_of(ref, struct config_t, ref);
354 ds_dbg(1, "releasing config_t\n");
355 kfree(c);
358 static void pcmcia_release_dev(struct device *dev)
360 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
361 ds_dev_dbg(1, dev, "releasing device\n");
362 pcmcia_put_socket(p_dev->socket);
363 kfree(p_dev->devname);
364 kref_put(&p_dev->function_config->ref, pcmcia_release_function);
365 kfree(p_dev);
368 static void pcmcia_add_device_later(struct pcmcia_socket *s, int mfc)
370 if (!s->pcmcia_state.device_add_pending) {
371 ds_dev_dbg(1, &s->dev, "scheduling to add %s secondary"
372 " device to %d\n", mfc ? "mfc" : "pfc", s->sock);
373 s->pcmcia_state.device_add_pending = 1;
374 s->pcmcia_state.mfc_pfc = mfc;
375 schedule_work(&s->device_add);
377 return;
380 static int pcmcia_device_probe(struct device * dev)
382 struct pcmcia_device *p_dev;
383 struct pcmcia_driver *p_drv;
384 struct pcmcia_device_id *did;
385 struct pcmcia_socket *s;
386 cistpl_config_t cis_config;
387 int ret = 0;
389 dev = get_device(dev);
390 if (!dev)
391 return -ENODEV;
393 p_dev = to_pcmcia_dev(dev);
394 p_drv = to_pcmcia_drv(dev->driver);
395 s = p_dev->socket;
397 /* The PCMCIA code passes the match data in via dev_set_drvdata(dev)
398 * which is an ugly hack. Once the driver probe is called it may
399 * and often will overwrite the match data so we must save it first
401 * handle pseudo multifunction devices:
402 * there are at most two pseudo multifunction devices.
403 * if we're matching against the first, schedule a
404 * call which will then check whether there are two
405 * pseudo devices, and if not, add the second one.
407 did = dev_get_drvdata(&p_dev->dev);
409 ds_dev_dbg(1, dev, "trying to bind to %s\n", p_drv->drv.name);
411 if ((!p_drv->probe) || (!p_dev->function_config) ||
412 (!try_module_get(p_drv->owner))) {
413 ret = -EINVAL;
414 goto put_dev;
417 /* set up some more device information */
418 ret = pccard_read_tuple(p_dev->socket, p_dev->func, CISTPL_CONFIG,
419 &cis_config);
420 if (!ret) {
421 p_dev->conf.ConfigBase = cis_config.base;
422 p_dev->conf.Present = cis_config.rmask[0];
423 } else {
424 dev_printk(KERN_INFO, dev,
425 "pcmcia: could not parse base and rmask0 of CIS\n");
426 p_dev->conf.ConfigBase = 0;
427 p_dev->conf.Present = 0;
430 ret = p_drv->probe(p_dev);
431 if (ret) {
432 ds_dev_dbg(1, dev, "binding to %s failed with %d\n",
433 p_drv->drv.name, ret);
434 goto put_module;
437 if (did && (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO) &&
438 (p_dev->socket->device_count == 1) && (p_dev->device_no == 0))
439 pcmcia_add_device_later(p_dev->socket, 0);
441 put_module:
442 if (ret)
443 module_put(p_drv->owner);
444 put_dev:
445 if (ret)
446 put_device(dev);
447 return (ret);
452 * Removes a PCMCIA card from the device tree and socket list.
454 static void pcmcia_card_remove(struct pcmcia_socket *s, struct pcmcia_device *leftover)
456 struct pcmcia_device *p_dev;
457 struct pcmcia_device *tmp;
458 unsigned long flags;
460 ds_dev_dbg(2, leftover ? &leftover->dev : &s->dev,
461 "pcmcia_card_remove(%d) %s\n", s->sock,
462 leftover ? leftover->devname : "");
464 if (!leftover)
465 s->device_count = 0;
466 else
467 s->device_count = 1;
469 /* unregister all pcmcia_devices registered with this socket, except leftover */
470 list_for_each_entry_safe(p_dev, tmp, &s->devices_list, socket_device_list) {
471 if (p_dev == leftover)
472 continue;
474 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
475 list_del(&p_dev->socket_device_list);
476 p_dev->_removed=1;
477 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
479 ds_dev_dbg(2, &p_dev->dev, "unregistering device\n");
480 device_unregister(&p_dev->dev);
483 return;
486 static int pcmcia_device_remove(struct device * dev)
488 struct pcmcia_device *p_dev;
489 struct pcmcia_driver *p_drv;
490 struct pcmcia_device_id *did;
491 int i;
493 p_dev = to_pcmcia_dev(dev);
494 p_drv = to_pcmcia_drv(dev->driver);
496 ds_dev_dbg(1, dev, "removing device\n");
498 /* If we're removing the primary module driving a
499 * pseudo multi-function card, we need to unbind
500 * all devices
502 did = dev_get_drvdata(&p_dev->dev);
503 if (did && (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO) &&
504 (p_dev->socket->device_count != 0) &&
505 (p_dev->device_no == 0))
506 pcmcia_card_remove(p_dev->socket, p_dev);
508 /* detach the "instance" */
509 if (!p_drv)
510 return 0;
512 if (p_drv->remove)
513 p_drv->remove(p_dev);
515 p_dev->dev_node = NULL;
517 /* check for proper unloading */
518 if (p_dev->_irq || p_dev->_io || p_dev->_locked)
519 dev_printk(KERN_INFO, dev,
520 "pcmcia: driver %s did not release config properly\n",
521 p_drv->drv.name);
523 for (i = 0; i < MAX_WIN; i++)
524 if (p_dev->_win & CLIENT_WIN_REQ(i))
525 dev_printk(KERN_INFO, dev,
526 "pcmcia: driver %s did not release window properly\n",
527 p_drv->drv.name);
529 /* references from pcmcia_probe_device */
530 pcmcia_put_dev(p_dev);
531 module_put(p_drv->owner);
533 return 0;
538 * pcmcia_device_query -- determine information about a pcmcia device
540 static int pcmcia_device_query(struct pcmcia_device *p_dev)
542 cistpl_manfid_t manf_id;
543 cistpl_funcid_t func_id;
544 cistpl_vers_1_t *vers1;
545 unsigned int i;
547 vers1 = kmalloc(sizeof(*vers1), GFP_KERNEL);
548 if (!vers1)
549 return -ENOMEM;
551 if (!pccard_read_tuple(p_dev->socket, p_dev->func,
552 CISTPL_MANFID, &manf_id)) {
553 p_dev->manf_id = manf_id.manf;
554 p_dev->card_id = manf_id.card;
555 p_dev->has_manf_id = 1;
556 p_dev->has_card_id = 1;
559 if (!pccard_read_tuple(p_dev->socket, p_dev->func,
560 CISTPL_FUNCID, &func_id)) {
561 p_dev->func_id = func_id.func;
562 p_dev->has_func_id = 1;
563 } else {
564 /* rule of thumb: cards with no FUNCID, but with
565 * common memory device geometry information, are
566 * probably memory cards (from pcmcia-cs) */
567 cistpl_device_geo_t *devgeo;
569 devgeo = kmalloc(sizeof(*devgeo), GFP_KERNEL);
570 if (!devgeo) {
571 kfree(vers1);
572 return -ENOMEM;
574 if (!pccard_read_tuple(p_dev->socket, p_dev->func,
575 CISTPL_DEVICE_GEO, devgeo)) {
576 ds_dev_dbg(0, &p_dev->dev,
577 "mem device geometry probably means "
578 "FUNCID_MEMORY\n");
579 p_dev->func_id = CISTPL_FUNCID_MEMORY;
580 p_dev->has_func_id = 1;
582 kfree(devgeo);
585 if (!pccard_read_tuple(p_dev->socket, p_dev->func, CISTPL_VERS_1,
586 vers1)) {
587 for (i=0; i < vers1->ns; i++) {
588 char *tmp;
589 unsigned int length;
591 tmp = vers1->str + vers1->ofs[i];
593 length = strlen(tmp) + 1;
594 if ((length < 2) || (length > 255))
595 continue;
597 p_dev->prod_id[i] = kmalloc(sizeof(char) * length,
598 GFP_KERNEL);
599 if (!p_dev->prod_id[i])
600 continue;
602 p_dev->prod_id[i] = strncpy(p_dev->prod_id[i],
603 tmp, length);
607 kfree(vers1);
608 return 0;
612 /* device_add_lock is needed to avoid double registration by cardmgr and kernel.
613 * Serializes pcmcia_device_add; will most likely be removed in future.
615 * While it has the caveat that adding new PCMCIA devices inside(!) device_register()
616 * won't work, this doesn't matter much at the moment: the driver core doesn't
617 * support it either.
619 static DEFINE_MUTEX(device_add_lock);
621 struct pcmcia_device * pcmcia_device_add(struct pcmcia_socket *s, unsigned int function)
623 struct pcmcia_device *p_dev, *tmp_dev;
624 unsigned long flags;
626 s = pcmcia_get_socket(s);
627 if (!s)
628 return NULL;
630 mutex_lock(&device_add_lock);
632 ds_dbg(3, "adding device to %d, function %d\n", s->sock, function);
634 /* max of 4 devices per card */
635 if (s->device_count == 4)
636 goto err_put;
638 p_dev = kzalloc(sizeof(struct pcmcia_device), GFP_KERNEL);
639 if (!p_dev)
640 goto err_put;
642 p_dev->socket = s;
643 p_dev->device_no = (s->device_count++);
644 p_dev->func = function;
646 p_dev->dev.bus = &pcmcia_bus_type;
647 p_dev->dev.parent = s->dev.parent;
648 p_dev->dev.release = pcmcia_release_dev;
649 /* by default don't allow DMA */
650 p_dev->dma_mask = DMA_MASK_NONE;
651 p_dev->dev.dma_mask = &p_dev->dma_mask;
652 dev_set_name(&p_dev->dev, "%d.%d", p_dev->socket->sock, p_dev->device_no);
653 if (!dev_name(&p_dev->dev))
654 goto err_free;
655 p_dev->devname = kasprintf(GFP_KERNEL, "pcmcia%s", dev_name(&p_dev->dev));
656 if (!p_dev->devname)
657 goto err_free;
658 ds_dev_dbg(3, &p_dev->dev, "devname is %s\n", p_dev->devname);
660 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
663 * p_dev->function_config must be the same for all card functions.
664 * Note that this is serialized by the device_add_lock, so that
665 * only one such struct will be created.
667 list_for_each_entry(tmp_dev, &s->devices_list, socket_device_list)
668 if (p_dev->func == tmp_dev->func) {
669 p_dev->function_config = tmp_dev->function_config;
670 p_dev->io = tmp_dev->io;
671 p_dev->irq = tmp_dev->irq;
672 kref_get(&p_dev->function_config->ref);
675 /* Add to the list in pcmcia_bus_socket */
676 list_add(&p_dev->socket_device_list, &s->devices_list);
678 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
680 if (!p_dev->function_config) {
681 ds_dev_dbg(3, &p_dev->dev, "creating config_t\n");
682 p_dev->function_config = kzalloc(sizeof(struct config_t),
683 GFP_KERNEL);
684 if (!p_dev->function_config)
685 goto err_unreg;
686 kref_init(&p_dev->function_config->ref);
689 dev_printk(KERN_NOTICE, &p_dev->dev,
690 "pcmcia: registering new device %s\n",
691 p_dev->devname);
693 pcmcia_device_query(p_dev);
695 if (device_register(&p_dev->dev))
696 goto err_unreg;
698 mutex_unlock(&device_add_lock);
700 return p_dev;
702 err_unreg:
703 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
704 list_del(&p_dev->socket_device_list);
705 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
707 err_free:
708 kfree(p_dev->devname);
709 kfree(p_dev);
710 s->device_count--;
711 err_put:
712 mutex_unlock(&device_add_lock);
713 pcmcia_put_socket(s);
715 return NULL;
719 static int pcmcia_card_add(struct pcmcia_socket *s)
721 cistpl_longlink_mfc_t mfc;
722 unsigned int no_funcs, i, no_chains;
723 int ret = 0;
725 if (!(s->resource_setup_done)) {
726 ds_dev_dbg(3, &s->dev,
727 "no resources available, delaying card_add\n");
728 return -EAGAIN; /* try again, but later... */
731 if (pcmcia_validate_mem(s)) {
732 ds_dev_dbg(3, &s->dev, "validating mem resources failed, "
733 "delaying card_add\n");
734 return -EAGAIN; /* try again, but later... */
737 ret = pccard_validate_cis(s, BIND_FN_ALL, &no_chains);
738 if (ret || !no_chains) {
739 ds_dev_dbg(0, &s->dev, "invalid CIS or invalid resources\n");
740 return -ENODEV;
743 if (!pccard_read_tuple(s, BIND_FN_ALL, CISTPL_LONGLINK_MFC, &mfc))
744 no_funcs = mfc.nfn;
745 else
746 no_funcs = 1;
747 s->functions = no_funcs;
749 for (i=0; i < no_funcs; i++)
750 pcmcia_device_add(s, i);
752 return (ret);
756 static void pcmcia_delayed_add_device(struct work_struct *work)
758 struct pcmcia_socket *s =
759 container_of(work, struct pcmcia_socket, device_add);
760 ds_dev_dbg(1, &s->dev, "adding additional device to %d\n", s->sock);
761 pcmcia_device_add(s, s->pcmcia_state.mfc_pfc);
762 s->pcmcia_state.device_add_pending = 0;
763 s->pcmcia_state.mfc_pfc = 0;
766 static int pcmcia_requery(struct device *dev, void * _data)
768 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
769 if (!p_dev->dev.driver) {
770 ds_dev_dbg(1, dev, "update device information\n");
771 pcmcia_device_query(p_dev);
774 return 0;
777 static void pcmcia_bus_rescan(struct pcmcia_socket *skt, int new_cis)
779 int no_devices = 0;
780 int ret = 0;
781 unsigned long flags;
783 /* must be called with skt_mutex held */
784 ds_dev_dbg(0, &skt->dev, "re-scanning socket %d\n", skt->sock);
786 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
787 if (list_empty(&skt->devices_list))
788 no_devices = 1;
789 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
791 /* If this is because of a CIS override, start over */
792 if (new_cis && !no_devices)
793 pcmcia_card_remove(skt, NULL);
795 /* if no devices were added for this socket yet because of
796 * missing resource information or other trouble, we need to
797 * do this now. */
798 if (no_devices || new_cis) {
799 ret = pcmcia_card_add(skt);
800 if (ret)
801 return;
804 /* some device information might have changed because of a CIS
805 * update or because we can finally read it correctly... so
806 * determine it again, overwriting old values if necessary. */
807 bus_for_each_dev(&pcmcia_bus_type, NULL, NULL, pcmcia_requery);
809 /* we re-scan all devices, not just the ones connected to this
810 * socket. This does not matter, though. */
811 ret = bus_rescan_devices(&pcmcia_bus_type);
812 if (ret)
813 printk(KERN_INFO "pcmcia: bus_rescan_devices failed\n");
816 #ifdef CONFIG_PCMCIA_LOAD_CIS
819 * pcmcia_load_firmware - load CIS from userspace if device-provided is broken
820 * @dev: the pcmcia device which needs a CIS override
821 * @filename: requested filename in /lib/firmware/
823 * This uses the in-kernel firmware loading mechanism to use a "fake CIS" if
824 * the one provided by the card is broken. The firmware files reside in
825 * /lib/firmware/ in userspace.
827 static int pcmcia_load_firmware(struct pcmcia_device *dev, char * filename)
829 struct pcmcia_socket *s = dev->socket;
830 const struct firmware *fw;
831 int ret = -ENOMEM;
832 int no_funcs;
833 int old_funcs;
834 cistpl_longlink_mfc_t mfc;
836 if (!filename)
837 return -EINVAL;
839 ds_dev_dbg(1, &dev->dev, "trying to load CIS file %s\n", filename);
841 if (request_firmware(&fw, filename, &dev->dev) == 0) {
842 if (fw->size >= CISTPL_MAX_CIS_SIZE) {
843 ret = -EINVAL;
844 dev_printk(KERN_ERR, &dev->dev,
845 "pcmcia: CIS override is too big\n");
846 goto release;
849 if (!pcmcia_replace_cis(s, fw->data, fw->size))
850 ret = 0;
851 else {
852 dev_printk(KERN_ERR, &dev->dev,
853 "pcmcia: CIS override failed\n");
854 goto release;
858 /* update information */
859 pcmcia_device_query(dev);
861 /* does this cis override add or remove functions? */
862 old_funcs = s->functions;
864 if (!pccard_read_tuple(s, BIND_FN_ALL, CISTPL_LONGLINK_MFC, &mfc))
865 no_funcs = mfc.nfn;
866 else
867 no_funcs = 1;
868 s->functions = no_funcs;
870 if (old_funcs > no_funcs)
871 pcmcia_card_remove(s, dev);
872 else if (no_funcs > old_funcs)
873 pcmcia_add_device_later(s, 1);
875 release:
876 release_firmware(fw);
878 return (ret);
881 #else /* !CONFIG_PCMCIA_LOAD_CIS */
883 static inline int pcmcia_load_firmware(struct pcmcia_device *dev, char * filename)
885 return -ENODEV;
888 #endif
891 static inline int pcmcia_devmatch(struct pcmcia_device *dev,
892 struct pcmcia_device_id *did)
894 if (did->match_flags & PCMCIA_DEV_ID_MATCH_MANF_ID) {
895 if ((!dev->has_manf_id) || (dev->manf_id != did->manf_id))
896 return 0;
899 if (did->match_flags & PCMCIA_DEV_ID_MATCH_CARD_ID) {
900 if ((!dev->has_card_id) || (dev->card_id != did->card_id))
901 return 0;
904 if (did->match_flags & PCMCIA_DEV_ID_MATCH_FUNCTION) {
905 if (dev->func != did->function)
906 return 0;
909 if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID1) {
910 if (!dev->prod_id[0])
911 return 0;
912 if (strcmp(did->prod_id[0], dev->prod_id[0]))
913 return 0;
916 if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID2) {
917 if (!dev->prod_id[1])
918 return 0;
919 if (strcmp(did->prod_id[1], dev->prod_id[1]))
920 return 0;
923 if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID3) {
924 if (!dev->prod_id[2])
925 return 0;
926 if (strcmp(did->prod_id[2], dev->prod_id[2]))
927 return 0;
930 if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID4) {
931 if (!dev->prod_id[3])
932 return 0;
933 if (strcmp(did->prod_id[3], dev->prod_id[3]))
934 return 0;
937 if (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO) {
938 if (dev->device_no != did->device_no)
939 return 0;
942 if (did->match_flags & PCMCIA_DEV_ID_MATCH_FUNC_ID) {
943 if ((!dev->has_func_id) || (dev->func_id != did->func_id))
944 return 0;
946 /* if this is a pseudo-multi-function device,
947 * we need explicit matches */
948 if (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO)
949 return 0;
950 if (dev->device_no)
951 return 0;
953 /* also, FUNC_ID matching needs to be activated by userspace
954 * after it has re-checked that there is no possible module
955 * with a prod_id/manf_id/card_id match.
957 ds_dev_dbg(0, &dev->dev,
958 "skipping FUNC_ID match until userspace interaction\n");
959 if (!dev->allow_func_id_match)
960 return 0;
963 if (did->match_flags & PCMCIA_DEV_ID_MATCH_FAKE_CIS) {
964 ds_dev_dbg(0, &dev->dev, "device needs a fake CIS\n");
965 if (!dev->socket->fake_cis)
966 pcmcia_load_firmware(dev, did->cisfile);
968 if (!dev->socket->fake_cis)
969 return 0;
972 if (did->match_flags & PCMCIA_DEV_ID_MATCH_ANONYMOUS) {
973 int i;
974 for (i=0; i<4; i++)
975 if (dev->prod_id[i])
976 return 0;
977 if (dev->has_manf_id || dev->has_card_id || dev->has_func_id)
978 return 0;
981 dev_set_drvdata(&dev->dev, did);
983 return 1;
987 static int pcmcia_bus_match(struct device * dev, struct device_driver * drv) {
988 struct pcmcia_device * p_dev = to_pcmcia_dev(dev);
989 struct pcmcia_driver * p_drv = to_pcmcia_drv(drv);
990 struct pcmcia_device_id *did = p_drv->id_table;
991 struct pcmcia_dynid *dynid;
993 /* match dynamic devices first */
994 spin_lock(&p_drv->dynids.lock);
995 list_for_each_entry(dynid, &p_drv->dynids.list, node) {
996 ds_dev_dbg(3, dev, "trying to match to %s\n", drv->name);
997 if (pcmcia_devmatch(p_dev, &dynid->id)) {
998 ds_dev_dbg(0, dev, "matched to %s\n", drv->name);
999 spin_unlock(&p_drv->dynids.lock);
1000 return 1;
1003 spin_unlock(&p_drv->dynids.lock);
1005 #ifdef CONFIG_PCMCIA_IOCTL
1006 /* matching by cardmgr */
1007 if (p_dev->cardmgr == p_drv) {
1008 ds_dev_dbg(0, dev, "cardmgr matched to %s\n", drv->name);
1009 return 1;
1011 #endif
1013 while (did && did->match_flags) {
1014 ds_dev_dbg(3, dev, "trying to match to %s\n", drv->name);
1015 if (pcmcia_devmatch(p_dev, did)) {
1016 ds_dev_dbg(0, dev, "matched to %s\n", drv->name);
1017 return 1;
1019 did++;
1022 return 0;
1025 #ifdef CONFIG_HOTPLUG
1027 static int pcmcia_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
1029 struct pcmcia_device *p_dev;
1030 int i;
1031 u32 hash[4] = { 0, 0, 0, 0};
1033 if (!dev)
1034 return -ENODEV;
1036 p_dev = to_pcmcia_dev(dev);
1038 /* calculate hashes */
1039 for (i=0; i<4; i++) {
1040 if (!p_dev->prod_id[i])
1041 continue;
1042 hash[i] = crc32(0, p_dev->prod_id[i], strlen(p_dev->prod_id[i]));
1045 if (add_uevent_var(env, "SOCKET_NO=%u", p_dev->socket->sock))
1046 return -ENOMEM;
1048 if (add_uevent_var(env, "DEVICE_NO=%02X", p_dev->device_no))
1049 return -ENOMEM;
1051 if (add_uevent_var(env, "MODALIAS=pcmcia:m%04Xc%04Xf%02Xfn%02Xpfn%02X"
1052 "pa%08Xpb%08Xpc%08Xpd%08X",
1053 p_dev->has_manf_id ? p_dev->manf_id : 0,
1054 p_dev->has_card_id ? p_dev->card_id : 0,
1055 p_dev->has_func_id ? p_dev->func_id : 0,
1056 p_dev->func,
1057 p_dev->device_no,
1058 hash[0],
1059 hash[1],
1060 hash[2],
1061 hash[3]))
1062 return -ENOMEM;
1064 return 0;
1067 #else
1069 static int pcmcia_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
1071 return -ENODEV;
1074 #endif
1076 /************************ runtime PM support ***************************/
1078 static int pcmcia_dev_suspend(struct device *dev, pm_message_t state);
1079 static int pcmcia_dev_resume(struct device *dev);
1081 static int runtime_suspend(struct device *dev)
1083 int rc;
1085 down(&dev->sem);
1086 rc = pcmcia_dev_suspend(dev, PMSG_SUSPEND);
1087 up(&dev->sem);
1088 return rc;
1091 static void runtime_resume(struct device *dev)
1093 int rc;
1095 down(&dev->sem);
1096 rc = pcmcia_dev_resume(dev);
1097 up(&dev->sem);
1100 /************************ per-device sysfs output ***************************/
1102 #define pcmcia_device_attr(field, test, format) \
1103 static ssize_t field##_show (struct device *dev, struct device_attribute *attr, char *buf) \
1105 struct pcmcia_device *p_dev = to_pcmcia_dev(dev); \
1106 return p_dev->test ? sprintf (buf, format, p_dev->field) : -ENODEV; \
1109 #define pcmcia_device_stringattr(name, field) \
1110 static ssize_t name##_show (struct device *dev, struct device_attribute *attr, char *buf) \
1112 struct pcmcia_device *p_dev = to_pcmcia_dev(dev); \
1113 return p_dev->field ? sprintf (buf, "%s\n", p_dev->field) : -ENODEV; \
1116 pcmcia_device_attr(func, socket, "0x%02x\n");
1117 pcmcia_device_attr(func_id, has_func_id, "0x%02x\n");
1118 pcmcia_device_attr(manf_id, has_manf_id, "0x%04x\n");
1119 pcmcia_device_attr(card_id, has_card_id, "0x%04x\n");
1120 pcmcia_device_stringattr(prod_id1, prod_id[0]);
1121 pcmcia_device_stringattr(prod_id2, prod_id[1]);
1122 pcmcia_device_stringattr(prod_id3, prod_id[2]);
1123 pcmcia_device_stringattr(prod_id4, prod_id[3]);
1126 static ssize_t pcmcia_show_pm_state(struct device *dev, struct device_attribute *attr, char *buf)
1128 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1130 if (p_dev->suspended)
1131 return sprintf(buf, "off\n");
1132 else
1133 return sprintf(buf, "on\n");
1136 static ssize_t pcmcia_store_pm_state(struct device *dev, struct device_attribute *attr,
1137 const char *buf, size_t count)
1139 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1140 int ret = 0;
1142 if (!count)
1143 return -EINVAL;
1145 if ((!p_dev->suspended) && !strncmp(buf, "off", 3))
1146 ret = runtime_suspend(dev);
1147 else if (p_dev->suspended && !strncmp(buf, "on", 2))
1148 runtime_resume(dev);
1150 return ret ? ret : count;
1154 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
1156 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1157 int i;
1158 u32 hash[4] = { 0, 0, 0, 0};
1160 /* calculate hashes */
1161 for (i=0; i<4; i++) {
1162 if (!p_dev->prod_id[i])
1163 continue;
1164 hash[i] = crc32(0,p_dev->prod_id[i],strlen(p_dev->prod_id[i]));
1166 return sprintf(buf, "pcmcia:m%04Xc%04Xf%02Xfn%02Xpfn%02X"
1167 "pa%08Xpb%08Xpc%08Xpd%08X\n",
1168 p_dev->has_manf_id ? p_dev->manf_id : 0,
1169 p_dev->has_card_id ? p_dev->card_id : 0,
1170 p_dev->has_func_id ? p_dev->func_id : 0,
1171 p_dev->func, p_dev->device_no,
1172 hash[0], hash[1], hash[2], hash[3]);
1175 static ssize_t pcmcia_store_allow_func_id_match(struct device *dev,
1176 struct device_attribute *attr, const char *buf, size_t count)
1178 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1179 int ret;
1181 if (!count)
1182 return -EINVAL;
1184 mutex_lock(&p_dev->socket->skt_mutex);
1185 p_dev->allow_func_id_match = 1;
1186 mutex_unlock(&p_dev->socket->skt_mutex);
1188 ret = bus_rescan_devices(&pcmcia_bus_type);
1189 if (ret)
1190 printk(KERN_INFO "pcmcia: bus_rescan_devices failed after "
1191 "allowing func_id matches\n");
1193 return count;
1196 static struct device_attribute pcmcia_dev_attrs[] = {
1197 __ATTR(function, 0444, func_show, NULL),
1198 __ATTR(pm_state, 0644, pcmcia_show_pm_state, pcmcia_store_pm_state),
1199 __ATTR_RO(func_id),
1200 __ATTR_RO(manf_id),
1201 __ATTR_RO(card_id),
1202 __ATTR_RO(prod_id1),
1203 __ATTR_RO(prod_id2),
1204 __ATTR_RO(prod_id3),
1205 __ATTR_RO(prod_id4),
1206 __ATTR_RO(modalias),
1207 __ATTR(allow_func_id_match, 0200, NULL, pcmcia_store_allow_func_id_match),
1208 __ATTR_NULL,
1211 /* PM support, also needed for reset */
1213 static int pcmcia_dev_suspend(struct device * dev, pm_message_t state)
1215 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1216 struct pcmcia_driver *p_drv = NULL;
1217 int ret = 0;
1219 if (p_dev->suspended)
1220 return 0;
1222 ds_dev_dbg(2, dev, "suspending\n");
1224 if (dev->driver)
1225 p_drv = to_pcmcia_drv(dev->driver);
1227 if (!p_drv)
1228 goto out;
1230 if (p_drv->suspend) {
1231 ret = p_drv->suspend(p_dev);
1232 if (ret) {
1233 dev_printk(KERN_ERR, dev,
1234 "pcmcia: device %s (driver %s) did "
1235 "not want to go to sleep (%d)\n",
1236 p_dev->devname, p_drv->drv.name, ret);
1237 goto out;
1241 if (p_dev->device_no == p_dev->func) {
1242 ds_dev_dbg(2, dev, "releasing configuration\n");
1243 pcmcia_release_configuration(p_dev);
1246 out:
1247 if (!ret)
1248 p_dev->suspended = 1;
1249 return ret;
1253 static int pcmcia_dev_resume(struct device * dev)
1255 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1256 struct pcmcia_driver *p_drv = NULL;
1257 int ret = 0;
1259 if (!p_dev->suspended)
1260 return 0;
1262 ds_dev_dbg(2, dev, "resuming\n");
1264 if (dev->driver)
1265 p_drv = to_pcmcia_drv(dev->driver);
1267 if (!p_drv)
1268 goto out;
1270 if (p_dev->device_no == p_dev->func) {
1271 ds_dev_dbg(2, dev, "requesting configuration\n");
1272 ret = pcmcia_request_configuration(p_dev, &p_dev->conf);
1273 if (ret)
1274 goto out;
1277 if (p_drv->resume)
1278 ret = p_drv->resume(p_dev);
1280 out:
1281 if (!ret)
1282 p_dev->suspended = 0;
1283 return ret;
1287 static int pcmcia_bus_suspend_callback(struct device *dev, void * _data)
1289 struct pcmcia_socket *skt = _data;
1290 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1292 if (p_dev->socket != skt || p_dev->suspended)
1293 return 0;
1295 return runtime_suspend(dev);
1298 static int pcmcia_bus_resume_callback(struct device *dev, void * _data)
1300 struct pcmcia_socket *skt = _data;
1301 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1303 if (p_dev->socket != skt || !p_dev->suspended)
1304 return 0;
1306 runtime_resume(dev);
1308 return 0;
1311 static int pcmcia_bus_resume(struct pcmcia_socket *skt)
1313 ds_dev_dbg(2, &skt->dev, "resuming socket %d\n", skt->sock);
1314 bus_for_each_dev(&pcmcia_bus_type, NULL, skt, pcmcia_bus_resume_callback);
1315 return 0;
1318 static int pcmcia_bus_suspend(struct pcmcia_socket *skt)
1320 ds_dev_dbg(2, &skt->dev, "suspending socket %d\n", skt->sock);
1321 if (bus_for_each_dev(&pcmcia_bus_type, NULL, skt,
1322 pcmcia_bus_suspend_callback)) {
1323 pcmcia_bus_resume(skt);
1324 return -EIO;
1326 return 0;
1330 /*======================================================================
1332 The card status event handler.
1334 ======================================================================*/
1336 /* Normally, the event is passed to individual drivers after
1337 * informing userspace. Only for CS_EVENT_CARD_REMOVAL this
1338 * is inversed to maintain historic compatibility.
1341 static int ds_event(struct pcmcia_socket *skt, event_t event, int priority)
1343 struct pcmcia_socket *s = pcmcia_get_socket(skt);
1345 if (!s) {
1346 dev_printk(KERN_ERR, &skt->dev,
1347 "PCMCIA obtaining reference to socket " \
1348 "failed, event 0x%x lost!\n", event);
1349 return -ENODEV;
1352 ds_dev_dbg(1, &skt->dev, "ds_event(0x%06x, %d, 0x%p)\n",
1353 event, priority, skt);
1355 switch (event) {
1356 case CS_EVENT_CARD_REMOVAL:
1357 s->pcmcia_state.present = 0;
1358 pcmcia_card_remove(skt, NULL);
1359 handle_event(skt, event);
1360 break;
1362 case CS_EVENT_CARD_INSERTION:
1363 s->pcmcia_state.present = 1;
1364 pcmcia_card_add(skt);
1365 handle_event(skt, event);
1366 break;
1368 case CS_EVENT_EJECTION_REQUEST:
1369 break;
1371 case CS_EVENT_PM_SUSPEND:
1372 case CS_EVENT_PM_RESUME:
1373 case CS_EVENT_RESET_PHYSICAL:
1374 case CS_EVENT_CARD_RESET:
1375 default:
1376 handle_event(skt, event);
1377 break;
1380 pcmcia_put_socket(s);
1382 return 0;
1383 } /* ds_event */
1386 struct pcmcia_device * pcmcia_dev_present(struct pcmcia_device *_p_dev)
1388 struct pcmcia_device *p_dev;
1389 struct pcmcia_device *ret = NULL;
1391 p_dev = pcmcia_get_dev(_p_dev);
1392 if (!p_dev)
1393 return NULL;
1395 if (!p_dev->socket->pcmcia_state.present)
1396 goto out;
1398 if (p_dev->_removed)
1399 goto out;
1401 if (p_dev->suspended)
1402 goto out;
1404 ret = p_dev;
1405 out:
1406 pcmcia_put_dev(p_dev);
1407 return ret;
1409 EXPORT_SYMBOL(pcmcia_dev_present);
1412 static struct pcmcia_callback pcmcia_bus_callback = {
1413 .owner = THIS_MODULE,
1414 .event = ds_event,
1415 .requery = pcmcia_bus_rescan,
1416 .suspend = pcmcia_bus_suspend,
1417 .resume = pcmcia_bus_resume,
1420 static int __devinit pcmcia_bus_add_socket(struct device *dev,
1421 struct class_interface *class_intf)
1423 struct pcmcia_socket *socket = dev_get_drvdata(dev);
1424 int ret;
1426 socket = pcmcia_get_socket(socket);
1427 if (!socket) {
1428 dev_printk(KERN_ERR, dev,
1429 "PCMCIA obtaining reference to socket failed\n");
1430 return -ENODEV;
1434 * Ugly. But we want to wait for the socket threads to have started up.
1435 * We really should let the drivers themselves drive some of this..
1437 msleep(250);
1439 #ifdef CONFIG_PCMCIA_IOCTL
1440 init_waitqueue_head(&socket->queue);
1441 #endif
1442 INIT_LIST_HEAD(&socket->devices_list);
1443 INIT_WORK(&socket->device_add, pcmcia_delayed_add_device);
1444 memset(&socket->pcmcia_state, 0, sizeof(u8));
1445 socket->device_count = 0;
1447 ret = pccard_register_pcmcia(socket, &pcmcia_bus_callback);
1448 if (ret) {
1449 dev_printk(KERN_ERR, dev, "PCMCIA registration failed\n");
1450 pcmcia_put_socket(socket);
1451 return (ret);
1454 return 0;
1457 static void pcmcia_bus_remove_socket(struct device *dev,
1458 struct class_interface *class_intf)
1460 struct pcmcia_socket *socket = dev_get_drvdata(dev);
1462 if (!socket)
1463 return;
1465 socket->pcmcia_state.dead = 1;
1466 pccard_register_pcmcia(socket, NULL);
1468 /* unregister any unbound devices */
1469 mutex_lock(&socket->skt_mutex);
1470 pcmcia_card_remove(socket, NULL);
1471 mutex_unlock(&socket->skt_mutex);
1473 pcmcia_put_socket(socket);
1475 return;
1479 /* the pcmcia_bus_interface is used to handle pcmcia socket devices */
1480 static struct class_interface pcmcia_bus_interface __refdata = {
1481 .class = &pcmcia_socket_class,
1482 .add_dev = &pcmcia_bus_add_socket,
1483 .remove_dev = &pcmcia_bus_remove_socket,
1487 struct bus_type pcmcia_bus_type = {
1488 .name = "pcmcia",
1489 .uevent = pcmcia_bus_uevent,
1490 .match = pcmcia_bus_match,
1491 .dev_attrs = pcmcia_dev_attrs,
1492 .probe = pcmcia_device_probe,
1493 .remove = pcmcia_device_remove,
1494 .suspend = pcmcia_dev_suspend,
1495 .resume = pcmcia_dev_resume,
1499 static int __init init_pcmcia_bus(void)
1501 int ret;
1503 spin_lock_init(&pcmcia_dev_list_lock);
1505 ret = bus_register(&pcmcia_bus_type);
1506 if (ret < 0) {
1507 printk(KERN_WARNING "pcmcia: bus_register error: %d\n", ret);
1508 return ret;
1510 ret = class_interface_register(&pcmcia_bus_interface);
1511 if (ret < 0) {
1512 printk(KERN_WARNING
1513 "pcmcia: class_interface_register error: %d\n", ret);
1514 bus_unregister(&pcmcia_bus_type);
1515 return ret;
1518 pcmcia_setup_ioctl();
1520 return 0;
1522 fs_initcall(init_pcmcia_bus); /* one level after subsys_initcall so that
1523 * pcmcia_socket_class is already registered */
1526 static void __exit exit_pcmcia_bus(void)
1528 pcmcia_cleanup_ioctl();
1530 class_interface_unregister(&pcmcia_bus_interface);
1532 bus_unregister(&pcmcia_bus_type);
1534 module_exit(exit_pcmcia_bus);
1537 MODULE_ALIAS("ds");