2 * ds.c -- 16-bit PCMCIA core support
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
8 * The initial developer of the original code is David A. Hinds
9 * <dahinds@users.sourceforge.net>. Portions created by David A. Hinds
10 * are Copyright (C) 1999 David A. Hinds. All Rights Reserved.
12 * (C) 1999 David A. Hinds
13 * (C) 2003 - 2006 Dominik Brodowski
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/errno.h>
20 #include <linux/list.h>
21 #include <linux/delay.h>
22 #include <linux/workqueue.h>
23 #include <linux/crc32.h>
24 #include <linux/firmware.h>
25 #include <linux/kref.h>
26 #include <linux/dma-mapping.h>
28 #include <pcmcia/cs_types.h>
29 #include <pcmcia/cs.h>
30 #include <pcmcia/cistpl.h>
31 #include <pcmcia/ds.h>
32 #include <pcmcia/ss.h>
34 #include "cs_internal.h"
36 /*====================================================================*/
38 /* Module parameters */
40 MODULE_AUTHOR("David Hinds <dahinds@users.sourceforge.net>");
41 MODULE_DESCRIPTION("PCMCIA Driver Services");
42 MODULE_LICENSE("GPL");
45 spinlock_t pcmcia_dev_list_lock
;
47 /*====================================================================*/
49 static void pcmcia_check_driver(struct pcmcia_driver
*p_drv
)
51 struct pcmcia_device_id
*did
= p_drv
->id_table
;
55 if (!p_drv
->probe
|| !p_drv
->remove
)
56 printk(KERN_DEBUG
"pcmcia: %s lacks a requisite callback "
57 "function\n", p_drv
->drv
.name
);
59 while (did
&& did
->match_flags
) {
60 for (i
= 0; i
< 4; i
++) {
64 hash
= crc32(0, did
->prod_id
[i
], strlen(did
->prod_id
[i
]));
65 if (hash
== did
->prod_id_hash
[i
])
68 printk(KERN_DEBUG
"pcmcia: %s: invalid hash for "
69 "product string \"%s\": is 0x%x, should "
70 "be 0x%x\n", p_drv
->drv
.name
, did
->prod_id
[i
],
71 did
->prod_id_hash
[i
], hash
);
72 printk(KERN_DEBUG
"pcmcia: see "
73 "Documentation/pcmcia/devicetable.txt for "
83 /*======================================================================*/
87 struct list_head node
;
88 struct pcmcia_device_id id
;
92 * pcmcia_store_new_id - add a new PCMCIA device ID to this driver and re-probe devices
93 * @driver: target device driver
94 * @buf: buffer for scanning device ID data
97 * Adds a new dynamic PCMCIA device ID to this driver,
98 * and causes the driver to probe for all devices again.
101 pcmcia_store_new_id(struct device_driver
*driver
, const char *buf
, size_t count
)
103 struct pcmcia_dynid
*dynid
;
104 struct pcmcia_driver
*pdrv
= to_pcmcia_drv(driver
);
105 __u16 match_flags
, manf_id
, card_id
;
106 __u8 func_id
, function
, device_no
;
107 __u32 prod_id_hash
[4] = {0, 0, 0, 0};
111 fields
= sscanf(buf
, "%hx %hx %hx %hhx %hhx %hhx %x %x %x %x",
112 &match_flags
, &manf_id
, &card_id
, &func_id
, &function
, &device_no
,
113 &prod_id_hash
[0], &prod_id_hash
[1], &prod_id_hash
[2], &prod_id_hash
[3]);
117 dynid
= kzalloc(sizeof(struct pcmcia_dynid
), GFP_KERNEL
);
121 dynid
->id
.match_flags
= match_flags
;
122 dynid
->id
.manf_id
= manf_id
;
123 dynid
->id
.card_id
= card_id
;
124 dynid
->id
.func_id
= func_id
;
125 dynid
->id
.function
= function
;
126 dynid
->id
.device_no
= device_no
;
127 memcpy(dynid
->id
.prod_id_hash
, prod_id_hash
, sizeof(__u32
) * 4);
129 spin_lock(&pdrv
->dynids
.lock
);
130 list_add_tail(&dynid
->node
, &pdrv
->dynids
.list
);
131 spin_unlock(&pdrv
->dynids
.lock
);
133 if (get_driver(&pdrv
->drv
)) {
134 retval
= driver_attach(&pdrv
->drv
);
135 put_driver(&pdrv
->drv
);
142 static DRIVER_ATTR(new_id
, S_IWUSR
, NULL
, pcmcia_store_new_id
);
145 pcmcia_free_dynids(struct pcmcia_driver
*drv
)
147 struct pcmcia_dynid
*dynid
, *n
;
149 spin_lock(&drv
->dynids
.lock
);
150 list_for_each_entry_safe(dynid
, n
, &drv
->dynids
.list
, node
) {
151 list_del(&dynid
->node
);
154 spin_unlock(&drv
->dynids
.lock
);
158 pcmcia_create_newid_file(struct pcmcia_driver
*drv
)
161 if (drv
->probe
!= NULL
)
162 error
= driver_create_file(&drv
->drv
, &driver_attr_new_id
);
168 * pcmcia_register_driver - register a PCMCIA driver with the bus core
169 * @driver: the &driver being registered
171 * Registers a PCMCIA driver with the PCMCIA bus core.
173 int pcmcia_register_driver(struct pcmcia_driver
*driver
)
180 pcmcia_check_driver(driver
);
182 /* initialize common fields */
183 driver
->drv
.bus
= &pcmcia_bus_type
;
184 driver
->drv
.owner
= driver
->owner
;
185 spin_lock_init(&driver
->dynids
.lock
);
186 INIT_LIST_HEAD(&driver
->dynids
.list
);
188 pr_debug("registering driver %s\n", driver
->drv
.name
);
190 error
= driver_register(&driver
->drv
);
194 error
= pcmcia_create_newid_file(driver
);
196 driver_unregister(&driver
->drv
);
200 EXPORT_SYMBOL(pcmcia_register_driver
);
203 * pcmcia_unregister_driver - unregister a PCMCIA driver with the bus core
204 * @driver: the &driver being unregistered
206 void pcmcia_unregister_driver(struct pcmcia_driver
*driver
)
208 pr_debug("unregistering driver %s\n", driver
->drv
.name
);
209 driver_unregister(&driver
->drv
);
210 pcmcia_free_dynids(driver
);
212 EXPORT_SYMBOL(pcmcia_unregister_driver
);
215 /* pcmcia_device handling */
217 struct pcmcia_device
*pcmcia_get_dev(struct pcmcia_device
*p_dev
)
219 struct device
*tmp_dev
;
220 tmp_dev
= get_device(&p_dev
->dev
);
223 return to_pcmcia_dev(tmp_dev
);
226 void pcmcia_put_dev(struct pcmcia_device
*p_dev
)
229 put_device(&p_dev
->dev
);
232 static void pcmcia_release_function(struct kref
*ref
)
234 struct config_t
*c
= container_of(ref
, struct config_t
, ref
);
235 pr_debug("releasing config_t\n");
239 static void pcmcia_release_dev(struct device
*dev
)
241 struct pcmcia_device
*p_dev
= to_pcmcia_dev(dev
);
242 dev_dbg(dev
, "releasing device\n");
243 pcmcia_put_socket(p_dev
->socket
);
244 kfree(p_dev
->devname
);
245 kref_put(&p_dev
->function_config
->ref
, pcmcia_release_function
);
249 static void pcmcia_add_device_later(struct pcmcia_socket
*s
, int mfc
)
251 if (!s
->pcmcia_state
.device_add_pending
) {
252 dev_dbg(&s
->dev
, "scheduling to add %s secondary"
253 " device to %d\n", mfc
? "mfc" : "pfc", s
->sock
);
254 s
->pcmcia_state
.device_add_pending
= 1;
255 s
->pcmcia_state
.mfc_pfc
= mfc
;
256 schedule_work(&s
->device_add
);
261 static int pcmcia_device_probe(struct device
*dev
)
263 struct pcmcia_device
*p_dev
;
264 struct pcmcia_driver
*p_drv
;
265 struct pcmcia_device_id
*did
;
266 struct pcmcia_socket
*s
;
267 cistpl_config_t cis_config
;
271 dev
= get_device(dev
);
275 p_dev
= to_pcmcia_dev(dev
);
276 p_drv
= to_pcmcia_drv(dev
->driver
);
279 /* The PCMCIA code passes the match data in via dev_set_drvdata(dev)
280 * which is an ugly hack. Once the driver probe is called it may
281 * and often will overwrite the match data so we must save it first
283 * handle pseudo multifunction devices:
284 * there are at most two pseudo multifunction devices.
285 * if we're matching against the first, schedule a
286 * call which will then check whether there are two
287 * pseudo devices, and if not, add the second one.
289 did
= dev_get_drvdata(&p_dev
->dev
);
291 dev_dbg(dev
, "trying to bind to %s\n", p_drv
->drv
.name
);
293 if ((!p_drv
->probe
) || (!p_dev
->function_config
) ||
294 (!try_module_get(p_drv
->owner
))) {
299 /* set up some more device information */
300 ret
= pccard_read_tuple(p_dev
->socket
, p_dev
->func
, CISTPL_CONFIG
,
303 p_dev
->conf
.ConfigBase
= cis_config
.base
;
304 p_dev
->conf
.Present
= cis_config
.rmask
[0];
306 dev_printk(KERN_INFO
, dev
,
307 "pcmcia: could not parse base and rmask0 of CIS\n");
308 p_dev
->conf
.ConfigBase
= 0;
309 p_dev
->conf
.Present
= 0;
312 ret
= p_drv
->probe(p_dev
);
314 dev_dbg(dev
, "binding to %s failed with %d\n",
315 p_drv
->drv
.name
, ret
);
319 spin_lock_irqsave(&pcmcia_dev_list_lock
, flags
);
320 if (did
&& (did
->match_flags
& PCMCIA_DEV_ID_MATCH_DEVICE_NO
) &&
321 (p_dev
->socket
->device_count
== 1) && (p_dev
->device_no
== 0))
322 pcmcia_add_device_later(p_dev
->socket
, 0);
323 spin_unlock_irqrestore(&pcmcia_dev_list_lock
, flags
);
327 module_put(p_drv
->owner
);
336 * Removes a PCMCIA card from the device tree and socket list.
338 static void pcmcia_card_remove(struct pcmcia_socket
*s
, struct pcmcia_device
*leftover
)
340 struct pcmcia_device
*p_dev
;
341 struct pcmcia_device
*tmp
;
344 dev_dbg(leftover
? &leftover
->dev
: &s
->dev
,
345 "pcmcia_card_remove(%d) %s\n", s
->sock
,
346 leftover
? leftover
->devname
: "");
348 spin_lock_irqsave(&pcmcia_dev_list_lock
, flags
);
353 spin_unlock_irqrestore(&pcmcia_dev_list_lock
, flags
);
355 /* unregister all pcmcia_devices registered with this socket, except leftover */
356 list_for_each_entry_safe(p_dev
, tmp
, &s
->devices_list
, socket_device_list
) {
357 if (p_dev
== leftover
)
360 spin_lock_irqsave(&pcmcia_dev_list_lock
, flags
);
361 list_del(&p_dev
->socket_device_list
);
363 spin_unlock_irqrestore(&pcmcia_dev_list_lock
, flags
);
365 dev_dbg(&p_dev
->dev
, "unregistering device\n");
366 device_unregister(&p_dev
->dev
);
372 static int pcmcia_device_remove(struct device
*dev
)
374 struct pcmcia_device
*p_dev
;
375 struct pcmcia_driver
*p_drv
;
376 struct pcmcia_device_id
*did
;
379 p_dev
= to_pcmcia_dev(dev
);
380 p_drv
= to_pcmcia_drv(dev
->driver
);
382 dev_dbg(dev
, "removing device\n");
384 /* If we're removing the primary module driving a
385 * pseudo multi-function card, we need to unbind
388 did
= dev_get_drvdata(&p_dev
->dev
);
389 if (did
&& (did
->match_flags
& PCMCIA_DEV_ID_MATCH_DEVICE_NO
) &&
390 (p_dev
->socket
->device_count
> 0) &&
391 (p_dev
->device_no
== 0))
392 pcmcia_card_remove(p_dev
->socket
, p_dev
);
394 /* detach the "instance" */
399 p_drv
->remove(p_dev
);
401 p_dev
->dev_node
= NULL
;
403 /* check for proper unloading */
404 if (p_dev
->_irq
|| p_dev
->_io
|| p_dev
->_locked
)
405 dev_printk(KERN_INFO
, dev
,
406 "pcmcia: driver %s did not release config properly\n",
409 for (i
= 0; i
< MAX_WIN
; i
++)
410 if (p_dev
->_win
& CLIENT_WIN_REQ(i
))
411 dev_printk(KERN_INFO
, dev
,
412 "pcmcia: driver %s did not release window properly\n",
415 /* references from pcmcia_probe_device */
416 pcmcia_put_dev(p_dev
);
417 module_put(p_drv
->owner
);
424 * pcmcia_device_query -- determine information about a pcmcia device
426 static int pcmcia_device_query(struct pcmcia_device
*p_dev
)
428 cistpl_manfid_t manf_id
;
429 cistpl_funcid_t func_id
;
430 cistpl_vers_1_t
*vers1
;
433 vers1
= kmalloc(sizeof(*vers1
), GFP_KERNEL
);
437 if (!pccard_read_tuple(p_dev
->socket
, BIND_FN_ALL
,
438 CISTPL_MANFID
, &manf_id
)) {
439 p_dev
->manf_id
= manf_id
.manf
;
440 p_dev
->card_id
= manf_id
.card
;
441 p_dev
->has_manf_id
= 1;
442 p_dev
->has_card_id
= 1;
445 if (!pccard_read_tuple(p_dev
->socket
, p_dev
->func
,
446 CISTPL_FUNCID
, &func_id
)) {
447 p_dev
->func_id
= func_id
.func
;
448 p_dev
->has_func_id
= 1;
450 /* rule of thumb: cards with no FUNCID, but with
451 * common memory device geometry information, are
452 * probably memory cards (from pcmcia-cs) */
453 cistpl_device_geo_t
*devgeo
;
455 devgeo
= kmalloc(sizeof(*devgeo
), GFP_KERNEL
);
460 if (!pccard_read_tuple(p_dev
->socket
, p_dev
->func
,
461 CISTPL_DEVICE_GEO
, devgeo
)) {
463 "mem device geometry probably means "
465 p_dev
->func_id
= CISTPL_FUNCID_MEMORY
;
466 p_dev
->has_func_id
= 1;
471 if (!pccard_read_tuple(p_dev
->socket
, BIND_FN_ALL
, CISTPL_VERS_1
,
473 for (i
= 0; i
< min_t(unsigned int, 4, vers1
->ns
); i
++) {
477 tmp
= vers1
->str
+ vers1
->ofs
[i
];
479 length
= strlen(tmp
) + 1;
480 if ((length
< 2) || (length
> 255))
483 p_dev
->prod_id
[i
] = kmalloc(sizeof(char) * length
,
485 if (!p_dev
->prod_id
[i
])
488 p_dev
->prod_id
[i
] = strncpy(p_dev
->prod_id
[i
],
498 /* device_add_lock is needed to avoid double registration by cardmgr and kernel.
499 * Serializes pcmcia_device_add; will most likely be removed in future.
501 * While it has the caveat that adding new PCMCIA devices inside(!) device_register()
502 * won't work, this doesn't matter much at the moment: the driver core doesn't
505 static DEFINE_MUTEX(device_add_lock
);
507 struct pcmcia_device
*pcmcia_device_add(struct pcmcia_socket
*s
, unsigned int function
)
509 struct pcmcia_device
*p_dev
, *tmp_dev
;
512 s
= pcmcia_get_socket(s
);
516 mutex_lock(&device_add_lock
);
518 pr_debug("adding device to %d, function %d\n", s
->sock
, function
);
520 p_dev
= kzalloc(sizeof(struct pcmcia_device
), GFP_KERNEL
);
524 spin_lock_irqsave(&pcmcia_dev_list_lock
, flags
);
525 p_dev
->device_no
= (s
->device_count
++);
526 spin_unlock_irqrestore(&pcmcia_dev_list_lock
, flags
);
528 /* max of 4 devices per card */
529 if (p_dev
->device_no
>= 4)
533 p_dev
->func
= function
;
535 p_dev
->dev
.bus
= &pcmcia_bus_type
;
536 p_dev
->dev
.parent
= s
->dev
.parent
;
537 p_dev
->dev
.release
= pcmcia_release_dev
;
538 /* by default don't allow DMA */
539 p_dev
->dma_mask
= DMA_MASK_NONE
;
540 p_dev
->dev
.dma_mask
= &p_dev
->dma_mask
;
541 dev_set_name(&p_dev
->dev
, "%d.%d", p_dev
->socket
->sock
, p_dev
->device_no
);
542 if (!dev_name(&p_dev
->dev
))
544 p_dev
->devname
= kasprintf(GFP_KERNEL
, "pcmcia%s", dev_name(&p_dev
->dev
));
547 dev_dbg(&p_dev
->dev
, "devname is %s\n", p_dev
->devname
);
549 spin_lock_irqsave(&pcmcia_dev_list_lock
, flags
);
552 * p_dev->function_config must be the same for all card functions.
553 * Note that this is serialized by the device_add_lock, so that
554 * only one such struct will be created.
556 list_for_each_entry(tmp_dev
, &s
->devices_list
, socket_device_list
)
557 if (p_dev
->func
== tmp_dev
->func
) {
558 p_dev
->function_config
= tmp_dev
->function_config
;
559 p_dev
->io
= tmp_dev
->io
;
560 p_dev
->irq
= tmp_dev
->irq
;
561 kref_get(&p_dev
->function_config
->ref
);
564 /* Add to the list in pcmcia_bus_socket */
565 list_add(&p_dev
->socket_device_list
, &s
->devices_list
);
567 spin_unlock_irqrestore(&pcmcia_dev_list_lock
, flags
);
569 if (!p_dev
->function_config
) {
570 dev_dbg(&p_dev
->dev
, "creating config_t\n");
571 p_dev
->function_config
= kzalloc(sizeof(struct config_t
),
573 if (!p_dev
->function_config
)
575 kref_init(&p_dev
->function_config
->ref
);
578 dev_printk(KERN_NOTICE
, &p_dev
->dev
,
579 "pcmcia: registering new device %s\n",
582 pcmcia_device_query(p_dev
);
584 if (device_register(&p_dev
->dev
))
587 mutex_unlock(&device_add_lock
);
592 spin_lock_irqsave(&pcmcia_dev_list_lock
, flags
);
593 list_del(&p_dev
->socket_device_list
);
594 spin_unlock_irqrestore(&pcmcia_dev_list_lock
, flags
);
597 spin_lock_irqsave(&pcmcia_dev_list_lock
, flags
);
599 spin_unlock_irqrestore(&pcmcia_dev_list_lock
, flags
);
601 kfree(p_dev
->devname
);
604 mutex_unlock(&device_add_lock
);
605 pcmcia_put_socket(s
);
611 static int pcmcia_card_add(struct pcmcia_socket
*s
)
613 cistpl_longlink_mfc_t mfc
;
614 unsigned int no_funcs
, i
, no_chains
;
617 if (!(s
->resource_setup_done
)) {
619 "no resources available, delaying card_add\n");
620 return -EAGAIN
; /* try again, but later... */
623 if (pcmcia_validate_mem(s
)) {
624 dev_dbg(&s
->dev
, "validating mem resources failed, "
625 "delaying card_add\n");
626 return -EAGAIN
; /* try again, but later... */
629 ret
= pccard_validate_cis(s
, &no_chains
);
630 if (ret
|| !no_chains
) {
631 dev_dbg(&s
->dev
, "invalid CIS or invalid resources\n");
635 if (!pccard_read_tuple(s
, BIND_FN_ALL
, CISTPL_LONGLINK_MFC
, &mfc
))
639 s
->functions
= no_funcs
;
641 for (i
= 0; i
< no_funcs
; i
++)
642 pcmcia_device_add(s
, i
);
648 static void pcmcia_delayed_add_device(struct work_struct
*work
)
650 struct pcmcia_socket
*s
=
651 container_of(work
, struct pcmcia_socket
, device_add
);
652 dev_dbg(&s
->dev
, "adding additional device to %d\n", s
->sock
);
653 pcmcia_device_add(s
, s
->pcmcia_state
.mfc_pfc
);
654 s
->pcmcia_state
.device_add_pending
= 0;
655 s
->pcmcia_state
.mfc_pfc
= 0;
658 static int pcmcia_requery(struct device
*dev
, void * _data
)
660 struct pcmcia_device
*p_dev
= to_pcmcia_dev(dev
);
661 if (!p_dev
->dev
.driver
) {
662 dev_dbg(dev
, "update device information\n");
663 pcmcia_device_query(p_dev
);
669 static void pcmcia_bus_rescan(struct pcmcia_socket
*skt
, int new_cis
)
675 /* must be called with skt_mutex held */
676 dev_dbg(&skt
->dev
, "re-scanning socket %d\n", skt
->sock
);
678 spin_lock_irqsave(&pcmcia_dev_list_lock
, flags
);
679 if (list_empty(&skt
->devices_list
))
681 spin_unlock_irqrestore(&pcmcia_dev_list_lock
, flags
);
683 /* If this is because of a CIS override, start over */
684 if (new_cis
&& !no_devices
)
685 pcmcia_card_remove(skt
, NULL
);
687 /* if no devices were added for this socket yet because of
688 * missing resource information or other trouble, we need to
690 if (no_devices
|| new_cis
) {
691 ret
= pcmcia_card_add(skt
);
696 /* some device information might have changed because of a CIS
697 * update or because we can finally read it correctly... so
698 * determine it again, overwriting old values if necessary. */
699 bus_for_each_dev(&pcmcia_bus_type
, NULL
, NULL
, pcmcia_requery
);
701 /* we re-scan all devices, not just the ones connected to this
702 * socket. This does not matter, though. */
703 ret
= bus_rescan_devices(&pcmcia_bus_type
);
705 printk(KERN_INFO
"pcmcia: bus_rescan_devices failed\n");
708 #ifdef CONFIG_PCMCIA_LOAD_CIS
711 * pcmcia_load_firmware - load CIS from userspace if device-provided is broken
712 * @dev: the pcmcia device which needs a CIS override
713 * @filename: requested filename in /lib/firmware/
715 * This uses the in-kernel firmware loading mechanism to use a "fake CIS" if
716 * the one provided by the card is broken. The firmware files reside in
717 * /lib/firmware/ in userspace.
719 static int pcmcia_load_firmware(struct pcmcia_device
*dev
, char * filename
)
721 struct pcmcia_socket
*s
= dev
->socket
;
722 const struct firmware
*fw
;
726 cistpl_longlink_mfc_t mfc
;
731 dev_dbg(&dev
->dev
, "trying to load CIS file %s\n", filename
);
733 if (request_firmware(&fw
, filename
, &dev
->dev
) == 0) {
734 if (fw
->size
>= CISTPL_MAX_CIS_SIZE
) {
736 dev_printk(KERN_ERR
, &dev
->dev
,
737 "pcmcia: CIS override is too big\n");
741 if (!pcmcia_replace_cis(s
, fw
->data
, fw
->size
))
744 dev_printk(KERN_ERR
, &dev
->dev
,
745 "pcmcia: CIS override failed\n");
750 /* update information */
751 pcmcia_device_query(dev
);
753 /* does this cis override add or remove functions? */
754 old_funcs
= s
->functions
;
756 if (!pccard_read_tuple(s
, BIND_FN_ALL
, CISTPL_LONGLINK_MFC
, &mfc
))
760 s
->functions
= no_funcs
;
762 if (old_funcs
> no_funcs
)
763 pcmcia_card_remove(s
, dev
);
764 else if (no_funcs
> old_funcs
)
765 pcmcia_add_device_later(s
, 1);
768 release_firmware(fw
);
773 #else /* !CONFIG_PCMCIA_LOAD_CIS */
775 static inline int pcmcia_load_firmware(struct pcmcia_device
*dev
, char * filename
)
783 static inline int pcmcia_devmatch(struct pcmcia_device
*dev
,
784 struct pcmcia_device_id
*did
)
786 if (did
->match_flags
& PCMCIA_DEV_ID_MATCH_MANF_ID
) {
787 if ((!dev
->has_manf_id
) || (dev
->manf_id
!= did
->manf_id
))
791 if (did
->match_flags
& PCMCIA_DEV_ID_MATCH_CARD_ID
) {
792 if ((!dev
->has_card_id
) || (dev
->card_id
!= did
->card_id
))
796 if (did
->match_flags
& PCMCIA_DEV_ID_MATCH_FUNCTION
) {
797 if (dev
->func
!= did
->function
)
801 if (did
->match_flags
& PCMCIA_DEV_ID_MATCH_PROD_ID1
) {
802 if (!dev
->prod_id
[0])
804 if (strcmp(did
->prod_id
[0], dev
->prod_id
[0]))
808 if (did
->match_flags
& PCMCIA_DEV_ID_MATCH_PROD_ID2
) {
809 if (!dev
->prod_id
[1])
811 if (strcmp(did
->prod_id
[1], dev
->prod_id
[1]))
815 if (did
->match_flags
& PCMCIA_DEV_ID_MATCH_PROD_ID3
) {
816 if (!dev
->prod_id
[2])
818 if (strcmp(did
->prod_id
[2], dev
->prod_id
[2]))
822 if (did
->match_flags
& PCMCIA_DEV_ID_MATCH_PROD_ID4
) {
823 if (!dev
->prod_id
[3])
825 if (strcmp(did
->prod_id
[3], dev
->prod_id
[3]))
829 if (did
->match_flags
& PCMCIA_DEV_ID_MATCH_DEVICE_NO
) {
830 if (dev
->device_no
!= did
->device_no
)
834 if (did
->match_flags
& PCMCIA_DEV_ID_MATCH_FUNC_ID
) {
835 if ((!dev
->has_func_id
) || (dev
->func_id
!= did
->func_id
))
838 /* if this is a pseudo-multi-function device,
839 * we need explicit matches */
840 if (did
->match_flags
& PCMCIA_DEV_ID_MATCH_DEVICE_NO
)
845 /* also, FUNC_ID matching needs to be activated by userspace
846 * after it has re-checked that there is no possible module
847 * with a prod_id/manf_id/card_id match.
850 "skipping FUNC_ID match until userspace interaction\n");
851 if (!dev
->allow_func_id_match
)
855 if (did
->match_flags
& PCMCIA_DEV_ID_MATCH_FAKE_CIS
) {
856 dev_dbg(&dev
->dev
, "device needs a fake CIS\n");
857 if (!dev
->socket
->fake_cis
)
858 pcmcia_load_firmware(dev
, did
->cisfile
);
860 if (!dev
->socket
->fake_cis
)
864 if (did
->match_flags
& PCMCIA_DEV_ID_MATCH_ANONYMOUS
) {
866 for (i
= 0; i
< 4; i
++)
869 if (dev
->has_manf_id
|| dev
->has_card_id
|| dev
->has_func_id
)
873 dev_set_drvdata(&dev
->dev
, did
);
879 static int pcmcia_bus_match(struct device
*dev
, struct device_driver
*drv
)
881 struct pcmcia_device
*p_dev
= to_pcmcia_dev(dev
);
882 struct pcmcia_driver
*p_drv
= to_pcmcia_drv(drv
);
883 struct pcmcia_device_id
*did
= p_drv
->id_table
;
884 struct pcmcia_dynid
*dynid
;
886 /* match dynamic devices first */
887 spin_lock(&p_drv
->dynids
.lock
);
888 list_for_each_entry(dynid
, &p_drv
->dynids
.list
, node
) {
889 dev_dbg(dev
, "trying to match to %s\n", drv
->name
);
890 if (pcmcia_devmatch(p_dev
, &dynid
->id
)) {
891 dev_dbg(dev
, "matched to %s\n", drv
->name
);
892 spin_unlock(&p_drv
->dynids
.lock
);
896 spin_unlock(&p_drv
->dynids
.lock
);
898 #ifdef CONFIG_PCMCIA_IOCTL
899 /* matching by cardmgr */
900 if (p_dev
->cardmgr
== p_drv
) {
901 dev_dbg(dev
, "cardmgr matched to %s\n", drv
->name
);
906 while (did
&& did
->match_flags
) {
907 dev_dbg(dev
, "trying to match to %s\n", drv
->name
);
908 if (pcmcia_devmatch(p_dev
, did
)) {
909 dev_dbg(dev
, "matched to %s\n", drv
->name
);
918 #ifdef CONFIG_HOTPLUG
920 static int pcmcia_bus_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
922 struct pcmcia_device
*p_dev
;
924 u32 hash
[4] = { 0, 0, 0, 0};
929 p_dev
= to_pcmcia_dev(dev
);
931 /* calculate hashes */
932 for (i
= 0; i
< 4; i
++) {
933 if (!p_dev
->prod_id
[i
])
935 hash
[i
] = crc32(0, p_dev
->prod_id
[i
], strlen(p_dev
->prod_id
[i
]));
938 if (add_uevent_var(env
, "SOCKET_NO=%u", p_dev
->socket
->sock
))
941 if (add_uevent_var(env
, "DEVICE_NO=%02X", p_dev
->device_no
))
944 if (add_uevent_var(env
, "MODALIAS=pcmcia:m%04Xc%04Xf%02Xfn%02Xpfn%02X"
945 "pa%08Xpb%08Xpc%08Xpd%08X",
946 p_dev
->has_manf_id
? p_dev
->manf_id
: 0,
947 p_dev
->has_card_id
? p_dev
->card_id
: 0,
948 p_dev
->has_func_id
? p_dev
->func_id
: 0,
962 static int pcmcia_bus_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
969 /************************ runtime PM support ***************************/
971 static int pcmcia_dev_suspend(struct device
*dev
, pm_message_t state
);
972 static int pcmcia_dev_resume(struct device
*dev
);
974 static int runtime_suspend(struct device
*dev
)
979 rc
= pcmcia_dev_suspend(dev
, PMSG_SUSPEND
);
984 static int runtime_resume(struct device
*dev
)
989 rc
= pcmcia_dev_resume(dev
);
994 /************************ per-device sysfs output ***************************/
996 #define pcmcia_device_attr(field, test, format) \
997 static ssize_t field##_show (struct device *dev, struct device_attribute *attr, char *buf) \
999 struct pcmcia_device *p_dev = to_pcmcia_dev(dev); \
1000 return p_dev->test ? sprintf(buf, format, p_dev->field) : -ENODEV; \
1003 #define pcmcia_device_stringattr(name, field) \
1004 static ssize_t name##_show (struct device *dev, struct device_attribute *attr, char *buf) \
1006 struct pcmcia_device *p_dev = to_pcmcia_dev(dev); \
1007 return p_dev->field ? sprintf(buf, "%s\n", p_dev->field) : -ENODEV; \
1010 pcmcia_device_attr(func
, socket
, "0x%02x\n");
1011 pcmcia_device_attr(func_id
, has_func_id
, "0x%02x\n");
1012 pcmcia_device_attr(manf_id
, has_manf_id
, "0x%04x\n");
1013 pcmcia_device_attr(card_id
, has_card_id
, "0x%04x\n");
1014 pcmcia_device_stringattr(prod_id1
, prod_id
[0]);
1015 pcmcia_device_stringattr(prod_id2
, prod_id
[1]);
1016 pcmcia_device_stringattr(prod_id3
, prod_id
[2]);
1017 pcmcia_device_stringattr(prod_id4
, prod_id
[3]);
1020 static ssize_t
pcmcia_show_pm_state(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
1022 struct pcmcia_device
*p_dev
= to_pcmcia_dev(dev
);
1024 if (p_dev
->suspended
)
1025 return sprintf(buf
, "off\n");
1027 return sprintf(buf
, "on\n");
1030 static ssize_t
pcmcia_store_pm_state(struct device
*dev
, struct device_attribute
*attr
,
1031 const char *buf
, size_t count
)
1033 struct pcmcia_device
*p_dev
= to_pcmcia_dev(dev
);
1039 if ((!p_dev
->suspended
) && !strncmp(buf
, "off", 3))
1040 ret
= runtime_suspend(dev
);
1041 else if (p_dev
->suspended
&& !strncmp(buf
, "on", 2))
1042 ret
= runtime_resume(dev
);
1044 return ret
? ret
: count
;
1048 static ssize_t
modalias_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
1050 struct pcmcia_device
*p_dev
= to_pcmcia_dev(dev
);
1052 u32 hash
[4] = { 0, 0, 0, 0};
1054 /* calculate hashes */
1055 for (i
= 0; i
< 4; i
++) {
1056 if (!p_dev
->prod_id
[i
])
1058 hash
[i
] = crc32(0, p_dev
->prod_id
[i
],
1059 strlen(p_dev
->prod_id
[i
]));
1061 return sprintf(buf
, "pcmcia:m%04Xc%04Xf%02Xfn%02Xpfn%02X"
1062 "pa%08Xpb%08Xpc%08Xpd%08X\n",
1063 p_dev
->has_manf_id
? p_dev
->manf_id
: 0,
1064 p_dev
->has_card_id
? p_dev
->card_id
: 0,
1065 p_dev
->has_func_id
? p_dev
->func_id
: 0,
1066 p_dev
->func
, p_dev
->device_no
,
1067 hash
[0], hash
[1], hash
[2], hash
[3]);
1070 static ssize_t
pcmcia_store_allow_func_id_match(struct device
*dev
,
1071 struct device_attribute
*attr
, const char *buf
, size_t count
)
1073 struct pcmcia_device
*p_dev
= to_pcmcia_dev(dev
);
1079 mutex_lock(&p_dev
->socket
->skt_mutex
);
1080 p_dev
->allow_func_id_match
= 1;
1081 mutex_unlock(&p_dev
->socket
->skt_mutex
);
1083 ret
= bus_rescan_devices(&pcmcia_bus_type
);
1085 printk(KERN_INFO
"pcmcia: bus_rescan_devices failed after "
1086 "allowing func_id matches\n");
1091 static struct device_attribute pcmcia_dev_attrs
[] = {
1092 __ATTR(function
, 0444, func_show
, NULL
),
1093 __ATTR(pm_state
, 0644, pcmcia_show_pm_state
, pcmcia_store_pm_state
),
1097 __ATTR_RO(prod_id1
),
1098 __ATTR_RO(prod_id2
),
1099 __ATTR_RO(prod_id3
),
1100 __ATTR_RO(prod_id4
),
1101 __ATTR_RO(modalias
),
1102 __ATTR(allow_func_id_match
, 0200, NULL
, pcmcia_store_allow_func_id_match
),
1106 /* PM support, also needed for reset */
1108 static int pcmcia_dev_suspend(struct device
*dev
, pm_message_t state
)
1110 struct pcmcia_device
*p_dev
= to_pcmcia_dev(dev
);
1111 struct pcmcia_driver
*p_drv
= NULL
;
1114 if (p_dev
->suspended
)
1117 dev_dbg(dev
, "suspending\n");
1120 p_drv
= to_pcmcia_drv(dev
->driver
);
1125 if (p_drv
->suspend
) {
1126 ret
= p_drv
->suspend(p_dev
);
1128 dev_printk(KERN_ERR
, dev
,
1129 "pcmcia: device %s (driver %s) did "
1130 "not want to go to sleep (%d)\n",
1131 p_dev
->devname
, p_drv
->drv
.name
, ret
);
1136 if (p_dev
->device_no
== p_dev
->func
) {
1137 dev_dbg(dev
, "releasing configuration\n");
1138 pcmcia_release_configuration(p_dev
);
1143 p_dev
->suspended
= 1;
1148 static int pcmcia_dev_resume(struct device
*dev
)
1150 struct pcmcia_device
*p_dev
= to_pcmcia_dev(dev
);
1151 struct pcmcia_driver
*p_drv
= NULL
;
1154 if (!p_dev
->suspended
)
1157 dev_dbg(dev
, "resuming\n");
1160 p_drv
= to_pcmcia_drv(dev
->driver
);
1165 if (p_dev
->device_no
== p_dev
->func
) {
1166 dev_dbg(dev
, "requesting configuration\n");
1167 ret
= pcmcia_request_configuration(p_dev
, &p_dev
->conf
);
1173 ret
= p_drv
->resume(p_dev
);
1177 p_dev
->suspended
= 0;
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
)
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
)
1201 runtime_resume(dev
);
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
);
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
);
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
);
1241 dev_printk(KERN_ERR
, &skt
->dev
,
1242 "PCMCIA obtaining reference to socket " \
1243 "failed, event 0x%x lost!\n", event
);
1247 dev_dbg(&skt
->dev
, "ds_event(0x%06x, %d, 0x%p)\n",
1248 event
, priority
, skt
);
1251 case CS_EVENT_CARD_REMOVAL
:
1252 s
->pcmcia_state
.present
= 0;
1253 pcmcia_card_remove(skt
, NULL
);
1254 handle_event(skt
, event
);
1255 mutex_lock(&s
->ops_mutex
);
1256 destroy_cis_cache(s
);
1257 mutex_unlock(&s
->ops_mutex
);
1260 case CS_EVENT_CARD_INSERTION
:
1261 s
->pcmcia_state
.present
= 1;
1262 mutex_lock(&s
->ops_mutex
);
1263 destroy_cis_cache(s
); /* to be on the safe side... */
1264 mutex_unlock(&s
->ops_mutex
);
1265 pcmcia_card_add(skt
);
1266 handle_event(skt
, event
);
1269 case CS_EVENT_EJECTION_REQUEST
:
1272 case CS_EVENT_PM_RESUME
:
1273 if (verify_cis_cache(skt
) != 0) {
1274 dev_dbg(&skt
->dev
, "cis mismatch - different card\n");
1275 /* first, remove the card */
1276 ds_event(skt
, CS_EVENT_CARD_REMOVAL
, CS_EVENT_PRI_HIGH
);
1277 mutex_lock(&s
->ops_mutex
);
1278 destroy_cis_cache(skt
);
1279 kfree(skt
->fake_cis
);
1280 skt
->fake_cis
= NULL
;
1281 mutex_unlock(&s
->ops_mutex
);
1282 /* now, add the new card */
1283 ds_event(skt
, CS_EVENT_CARD_INSERTION
,
1286 handle_event(skt
, event
);
1289 case CS_EVENT_PM_SUSPEND
:
1290 case CS_EVENT_RESET_PHYSICAL
:
1291 case CS_EVENT_CARD_RESET
:
1293 handle_event(skt
, event
);
1297 pcmcia_put_socket(s
);
1303 struct pcmcia_device
*pcmcia_dev_present(struct pcmcia_device
*_p_dev
)
1305 struct pcmcia_device
*p_dev
;
1306 struct pcmcia_device
*ret
= NULL
;
1308 p_dev
= pcmcia_get_dev(_p_dev
);
1312 if (!p_dev
->socket
->pcmcia_state
.present
)
1315 if (p_dev
->_removed
)
1318 if (p_dev
->suspended
)
1323 pcmcia_put_dev(p_dev
);
1326 EXPORT_SYMBOL(pcmcia_dev_present
);
1329 static struct pcmcia_callback pcmcia_bus_callback
= {
1330 .owner
= THIS_MODULE
,
1332 .requery
= pcmcia_bus_rescan
,
1333 .validate
= pccard_validate_cis
,
1334 .suspend
= pcmcia_bus_suspend
,
1335 .resume
= pcmcia_bus_resume
,
1338 static int __devinit
pcmcia_bus_add_socket(struct device
*dev
,
1339 struct class_interface
*class_intf
)
1341 struct pcmcia_socket
*socket
= dev_get_drvdata(dev
);
1344 socket
= pcmcia_get_socket(socket
);
1346 dev_printk(KERN_ERR
, dev
,
1347 "PCMCIA obtaining reference to socket failed\n");
1352 * Ugly. But we want to wait for the socket threads to have started up.
1353 * We really should let the drivers themselves drive some of this..
1357 ret
= sysfs_create_bin_file(&dev
->kobj
, &pccard_cis_attr
);
1359 dev_printk(KERN_ERR
, dev
, "PCMCIA registration failed\n");
1360 pcmcia_put_socket(socket
);
1364 #ifdef CONFIG_PCMCIA_IOCTL
1365 init_waitqueue_head(&socket
->queue
);
1367 INIT_LIST_HEAD(&socket
->devices_list
);
1368 INIT_WORK(&socket
->device_add
, pcmcia_delayed_add_device
);
1369 memset(&socket
->pcmcia_state
, 0, sizeof(u8
));
1370 socket
->device_count
= 0;
1372 ret
= pccard_register_pcmcia(socket
, &pcmcia_bus_callback
);
1374 dev_printk(KERN_ERR
, dev
, "PCMCIA registration failed\n");
1375 pcmcia_put_socket(socket
);
1382 static void pcmcia_bus_remove_socket(struct device
*dev
,
1383 struct class_interface
*class_intf
)
1385 struct pcmcia_socket
*socket
= dev_get_drvdata(dev
);
1390 socket
->pcmcia_state
.dead
= 1;
1391 pccard_register_pcmcia(socket
, NULL
);
1393 /* unregister any unbound devices */
1394 mutex_lock(&socket
->skt_mutex
);
1395 pcmcia_card_remove(socket
, NULL
);
1396 release_cis_mem(socket
);
1397 mutex_unlock(&socket
->skt_mutex
);
1399 sysfs_remove_bin_file(&dev
->kobj
, &pccard_cis_attr
);
1401 pcmcia_put_socket(socket
);
1407 /* the pcmcia_bus_interface is used to handle pcmcia socket devices */
1408 static struct class_interface pcmcia_bus_interface __refdata
= {
1409 .class = &pcmcia_socket_class
,
1410 .add_dev
= &pcmcia_bus_add_socket
,
1411 .remove_dev
= &pcmcia_bus_remove_socket
,
1415 struct bus_type pcmcia_bus_type
= {
1417 .uevent
= pcmcia_bus_uevent
,
1418 .match
= pcmcia_bus_match
,
1419 .dev_attrs
= pcmcia_dev_attrs
,
1420 .probe
= pcmcia_device_probe
,
1421 .remove
= pcmcia_device_remove
,
1422 .suspend
= pcmcia_dev_suspend
,
1423 .resume
= pcmcia_dev_resume
,
1427 static int __init
init_pcmcia_bus(void)
1431 spin_lock_init(&pcmcia_dev_list_lock
);
1433 ret
= bus_register(&pcmcia_bus_type
);
1435 printk(KERN_WARNING
"pcmcia: bus_register error: %d\n", ret
);
1438 ret
= class_interface_register(&pcmcia_bus_interface
);
1441 "pcmcia: class_interface_register error: %d\n", ret
);
1442 bus_unregister(&pcmcia_bus_type
);
1446 pcmcia_setup_ioctl();
1450 fs_initcall(init_pcmcia_bus
); /* one level after subsys_initcall so that
1451 * pcmcia_socket_class is already registered */
1454 static void __exit
exit_pcmcia_bus(void)
1456 pcmcia_cleanup_ioctl();
1458 class_interface_unregister(&pcmcia_bus_interface
);
1460 bus_unregister(&pcmcia_bus_type
);
1462 module_exit(exit_pcmcia_bus
);