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
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); \
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); \
58 #define ds_dbg(lvl, fmt, arg...) do { } while (0)
59 #define ds_dev_dbg(lvl, dev, fmt, arg...) do { } while (0)
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
{
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
)
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
)
155 for (i
= 0; i
< ARRAY_SIZE(error_table
); i
++)
156 if (error_table
[i
].key
== ret
)
157 return error_table
[i
].msg
;
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
;
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
])
182 hash
= crc32(0, did
->prod_id
[i
], strlen(did
->prod_id
[i
]));
183 if (hash
== did
->prod_id_hash
[i
])
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 "
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
215 * Adds a new dynamic PCMCIA device ID to this driver,
216 * and causes the driver to probe for all devices again.
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};
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]);
235 dynid
= kzalloc(sizeof(struct pcmcia_dynid
), GFP_KERNEL
);
239 dynid
->id
.match_flags
= match_flags
;
240 dynid
->id
.manf_id
= manf_id
;
241 dynid
->id
.card_id
= card_id
;
242 dynid
->id
.func_id
= func_id
;
243 dynid
->id
.function
= function
;
244 dynid
->id
.device_no
= device_no
;
245 memcpy(dynid
->id
.prod_id_hash
, prod_id_hash
, sizeof(__u32
) * 4);
247 spin_lock(&pdrv
->dynids
.lock
);
248 list_add_tail(&dynid
->node
, &pdrv
->dynids
.list
);
249 spin_unlock(&pdrv
->dynids
.lock
);
251 if (get_driver(&pdrv
->drv
)) {
252 retval
= driver_attach(&pdrv
->drv
);
253 put_driver(&pdrv
->drv
);
260 static DRIVER_ATTR(new_id
, S_IWUSR
, NULL
, pcmcia_store_new_id
);
263 pcmcia_free_dynids(struct pcmcia_driver
*drv
)
265 struct pcmcia_dynid
*dynid
, *n
;
267 spin_lock(&drv
->dynids
.lock
);
268 list_for_each_entry_safe(dynid
, n
, &drv
->dynids
.list
, node
) {
269 list_del(&dynid
->node
);
272 spin_unlock(&drv
->dynids
.lock
);
276 pcmcia_create_newid_file(struct pcmcia_driver
*drv
)
279 if (drv
->probe
!= NULL
)
280 error
= driver_create_file(&drv
->drv
, &driver_attr_new_id
);
286 * pcmcia_register_driver - register a PCMCIA driver with the bus core
287 * @driver: the &driver being registered
289 * Registers a PCMCIA driver with the PCMCIA bus core.
291 int pcmcia_register_driver(struct pcmcia_driver
*driver
)
298 pcmcia_check_driver(driver
);
300 /* initialize common fields */
301 driver
->drv
.bus
= &pcmcia_bus_type
;
302 driver
->drv
.owner
= driver
->owner
;
303 spin_lock_init(&driver
->dynids
.lock
);
304 INIT_LIST_HEAD(&driver
->dynids
.list
);
306 ds_dbg(3, "registering driver %s\n", driver
->drv
.name
);
308 error
= driver_register(&driver
->drv
);
312 error
= pcmcia_create_newid_file(driver
);
314 driver_unregister(&driver
->drv
);
318 EXPORT_SYMBOL(pcmcia_register_driver
);
321 * pcmcia_unregister_driver - unregister a PCMCIA driver with the bus core
322 * @driver: the &driver being unregistered
324 void pcmcia_unregister_driver(struct pcmcia_driver
*driver
)
326 ds_dbg(3, "unregistering driver %s\n", driver
->drv
.name
);
327 driver_unregister(&driver
->drv
);
328 pcmcia_free_dynids(driver
);
330 EXPORT_SYMBOL(pcmcia_unregister_driver
);
333 /* pcmcia_device handling */
335 struct pcmcia_device
* pcmcia_get_dev(struct pcmcia_device
*p_dev
)
337 struct device
*tmp_dev
;
338 tmp_dev
= get_device(&p_dev
->dev
);
341 return to_pcmcia_dev(tmp_dev
);
344 void pcmcia_put_dev(struct pcmcia_device
*p_dev
)
347 put_device(&p_dev
->dev
);
350 static void pcmcia_release_function(struct kref
*ref
)
352 struct config_t
*c
= container_of(ref
, struct config_t
, ref
);
353 ds_dbg(1, "releasing config_t\n");
357 static void pcmcia_release_dev(struct device
*dev
)
359 struct pcmcia_device
*p_dev
= to_pcmcia_dev(dev
);
360 ds_dev_dbg(1, dev
, "releasing device\n");
361 pcmcia_put_socket(p_dev
->socket
);
362 kfree(p_dev
->devname
);
363 kref_put(&p_dev
->function_config
->ref
, pcmcia_release_function
);
367 static void pcmcia_add_device_later(struct pcmcia_socket
*s
, int mfc
)
369 if (!s
->pcmcia_state
.device_add_pending
) {
370 ds_dev_dbg(1, &s
->dev
, "scheduling to add %s secondary"
371 " device to %d\n", mfc
? "mfc" : "pfc", s
->sock
);
372 s
->pcmcia_state
.device_add_pending
= 1;
373 s
->pcmcia_state
.mfc_pfc
= mfc
;
374 schedule_work(&s
->device_add
);
379 static int pcmcia_device_probe(struct device
* dev
)
381 struct pcmcia_device
*p_dev
;
382 struct pcmcia_driver
*p_drv
;
383 struct pcmcia_device_id
*did
;
384 struct pcmcia_socket
*s
;
385 cistpl_config_t cis_config
;
388 dev
= get_device(dev
);
392 p_dev
= to_pcmcia_dev(dev
);
393 p_drv
= to_pcmcia_drv(dev
->driver
);
396 /* The PCMCIA code passes the match data in via dev_set_drvdata(dev)
397 * which is an ugly hack. Once the driver probe is called it may
398 * and often will overwrite the match data so we must save it first
400 * handle pseudo multifunction devices:
401 * there are at most two pseudo multifunction devices.
402 * if we're matching against the first, schedule a
403 * call which will then check whether there are two
404 * pseudo devices, and if not, add the second one.
406 did
= dev_get_drvdata(&p_dev
->dev
);
408 ds_dev_dbg(1, dev
, "trying to bind to %s\n", p_drv
->drv
.name
);
410 if ((!p_drv
->probe
) || (!p_dev
->function_config
) ||
411 (!try_module_get(p_drv
->owner
))) {
416 /* set up some more device information */
417 ret
= pccard_read_tuple(p_dev
->socket
, p_dev
->func
, CISTPL_CONFIG
,
420 p_dev
->conf
.ConfigBase
= cis_config
.base
;
421 p_dev
->conf
.Present
= cis_config
.rmask
[0];
423 dev_printk(KERN_INFO
, dev
,
424 "pcmcia: could not parse base and rmask0 of CIS\n");
425 p_dev
->conf
.ConfigBase
= 0;
426 p_dev
->conf
.Present
= 0;
429 ret
= p_drv
->probe(p_dev
);
431 ds_dev_dbg(1, dev
, "binding to %s failed with %d\n",
432 p_drv
->drv
.name
, ret
);
436 if (did
&& (did
->match_flags
& PCMCIA_DEV_ID_MATCH_DEVICE_NO
) &&
437 (p_dev
->socket
->device_count
== 1) && (p_dev
->device_no
== 0))
438 pcmcia_add_device_later(p_dev
->socket
, 0);
442 module_put(p_drv
->owner
);
451 * Removes a PCMCIA card from the device tree and socket list.
453 static void pcmcia_card_remove(struct pcmcia_socket
*s
, struct pcmcia_device
*leftover
)
455 struct pcmcia_device
*p_dev
;
456 struct pcmcia_device
*tmp
;
459 ds_dev_dbg(2, leftover
? &leftover
->dev
: &s
->dev
,
460 "pcmcia_card_remove(%d) %s\n", s
->sock
,
461 leftover
? leftover
->devname
: "");
468 /* unregister all pcmcia_devices registered with this socket, except leftover */
469 list_for_each_entry_safe(p_dev
, tmp
, &s
->devices_list
, socket_device_list
) {
470 if (p_dev
== leftover
)
473 spin_lock_irqsave(&pcmcia_dev_list_lock
, flags
);
474 list_del(&p_dev
->socket_device_list
);
476 spin_unlock_irqrestore(&pcmcia_dev_list_lock
, flags
);
478 ds_dev_dbg(2, &p_dev
->dev
, "unregistering device\n");
479 device_unregister(&p_dev
->dev
);
485 static int pcmcia_device_remove(struct device
* dev
)
487 struct pcmcia_device
*p_dev
;
488 struct pcmcia_driver
*p_drv
;
489 struct pcmcia_device_id
*did
;
492 p_dev
= to_pcmcia_dev(dev
);
493 p_drv
= to_pcmcia_drv(dev
->driver
);
495 ds_dev_dbg(1, dev
, "removing device\n");
497 /* If we're removing the primary module driving a
498 * pseudo multi-function card, we need to unbind
501 did
= dev_get_drvdata(&p_dev
->dev
);
502 if (did
&& (did
->match_flags
& PCMCIA_DEV_ID_MATCH_DEVICE_NO
) &&
503 (p_dev
->socket
->device_count
!= 0) &&
504 (p_dev
->device_no
== 0))
505 pcmcia_card_remove(p_dev
->socket
, p_dev
);
507 /* detach the "instance" */
512 p_drv
->remove(p_dev
);
514 p_dev
->dev_node
= NULL
;
516 /* check for proper unloading */
517 if (p_dev
->_irq
|| p_dev
->_io
|| p_dev
->_locked
)
518 dev_printk(KERN_INFO
, dev
,
519 "pcmcia: driver %s did not release config properly\n",
522 for (i
= 0; i
< MAX_WIN
; i
++)
523 if (p_dev
->_win
& CLIENT_WIN_REQ(i
))
524 dev_printk(KERN_INFO
, dev
,
525 "pcmcia: driver %s did not release window properly\n",
528 /* references from pcmcia_probe_device */
529 pcmcia_put_dev(p_dev
);
530 module_put(p_drv
->owner
);
537 * pcmcia_device_query -- determine information about a pcmcia device
539 static int pcmcia_device_query(struct pcmcia_device
*p_dev
)
541 cistpl_manfid_t manf_id
;
542 cistpl_funcid_t func_id
;
543 cistpl_vers_1_t
*vers1
;
546 vers1
= kmalloc(sizeof(*vers1
), GFP_KERNEL
);
550 if (!pccard_read_tuple(p_dev
->socket
, BIND_FN_ALL
,
551 CISTPL_MANFID
, &manf_id
)) {
552 p_dev
->manf_id
= manf_id
.manf
;
553 p_dev
->card_id
= manf_id
.card
;
554 p_dev
->has_manf_id
= 1;
555 p_dev
->has_card_id
= 1;
558 if (!pccard_read_tuple(p_dev
->socket
, p_dev
->func
,
559 CISTPL_FUNCID
, &func_id
)) {
560 p_dev
->func_id
= func_id
.func
;
561 p_dev
->has_func_id
= 1;
563 /* rule of thumb: cards with no FUNCID, but with
564 * common memory device geometry information, are
565 * probably memory cards (from pcmcia-cs) */
566 cistpl_device_geo_t
*devgeo
;
568 devgeo
= kmalloc(sizeof(*devgeo
), GFP_KERNEL
);
573 if (!pccard_read_tuple(p_dev
->socket
, p_dev
->func
,
574 CISTPL_DEVICE_GEO
, devgeo
)) {
575 ds_dev_dbg(0, &p_dev
->dev
,
576 "mem device geometry probably means "
578 p_dev
->func_id
= CISTPL_FUNCID_MEMORY
;
579 p_dev
->has_func_id
= 1;
584 if (!pccard_read_tuple(p_dev
->socket
, BIND_FN_ALL
, CISTPL_VERS_1
,
586 for (i
= 0; i
< min_t(unsigned int, 4, vers1
->ns
); i
++) {
590 tmp
= vers1
->str
+ vers1
->ofs
[i
];
592 length
= strlen(tmp
) + 1;
593 if ((length
< 2) || (length
> 255))
596 p_dev
->prod_id
[i
] = kmalloc(sizeof(char) * length
,
598 if (!p_dev
->prod_id
[i
])
601 p_dev
->prod_id
[i
] = strncpy(p_dev
->prod_id
[i
],
611 /* device_add_lock is needed to avoid double registration by cardmgr and kernel.
612 * Serializes pcmcia_device_add; will most likely be removed in future.
614 * While it has the caveat that adding new PCMCIA devices inside(!) device_register()
615 * won't work, this doesn't matter much at the moment: the driver core doesn't
618 static DEFINE_MUTEX(device_add_lock
);
620 struct pcmcia_device
* pcmcia_device_add(struct pcmcia_socket
*s
, unsigned int function
)
622 struct pcmcia_device
*p_dev
, *tmp_dev
;
625 s
= pcmcia_get_socket(s
);
629 mutex_lock(&device_add_lock
);
631 ds_dbg(3, "adding device to %d, function %d\n", s
->sock
, function
);
633 /* max of 4 devices per card */
634 if (s
->device_count
== 4)
637 p_dev
= kzalloc(sizeof(struct pcmcia_device
), GFP_KERNEL
);
642 p_dev
->device_no
= (s
->device_count
++);
643 p_dev
->func
= function
;
645 p_dev
->dev
.bus
= &pcmcia_bus_type
;
646 p_dev
->dev
.parent
= s
->dev
.parent
;
647 p_dev
->dev
.release
= pcmcia_release_dev
;
648 /* by default don't allow DMA */
649 p_dev
->dma_mask
= DMA_MASK_NONE
;
650 p_dev
->dev
.dma_mask
= &p_dev
->dma_mask
;
651 dev_set_name(&p_dev
->dev
, "%d.%d", p_dev
->socket
->sock
, p_dev
->device_no
);
652 if (!dev_name(&p_dev
->dev
))
654 p_dev
->devname
= kasprintf(GFP_KERNEL
, "pcmcia%s", dev_name(&p_dev
->dev
));
657 ds_dev_dbg(3, &p_dev
->dev
, "devname is %s\n", p_dev
->devname
);
659 spin_lock_irqsave(&pcmcia_dev_list_lock
, flags
);
662 * p_dev->function_config must be the same for all card functions.
663 * Note that this is serialized by the device_add_lock, so that
664 * only one such struct will be created.
666 list_for_each_entry(tmp_dev
, &s
->devices_list
, socket_device_list
)
667 if (p_dev
->func
== tmp_dev
->func
) {
668 p_dev
->function_config
= tmp_dev
->function_config
;
669 p_dev
->io
= tmp_dev
->io
;
670 p_dev
->irq
= tmp_dev
->irq
;
671 kref_get(&p_dev
->function_config
->ref
);
674 /* Add to the list in pcmcia_bus_socket */
675 list_add(&p_dev
->socket_device_list
, &s
->devices_list
);
677 spin_unlock_irqrestore(&pcmcia_dev_list_lock
, flags
);
679 if (!p_dev
->function_config
) {
680 ds_dev_dbg(3, &p_dev
->dev
, "creating config_t\n");
681 p_dev
->function_config
= kzalloc(sizeof(struct config_t
),
683 if (!p_dev
->function_config
)
685 kref_init(&p_dev
->function_config
->ref
);
688 dev_printk(KERN_NOTICE
, &p_dev
->dev
,
689 "pcmcia: registering new device %s\n",
692 pcmcia_device_query(p_dev
);
694 if (device_register(&p_dev
->dev
))
697 mutex_unlock(&device_add_lock
);
702 spin_lock_irqsave(&pcmcia_dev_list_lock
, flags
);
703 list_del(&p_dev
->socket_device_list
);
704 spin_unlock_irqrestore(&pcmcia_dev_list_lock
, flags
);
707 kfree(p_dev
->devname
);
711 mutex_unlock(&device_add_lock
);
712 pcmcia_put_socket(s
);
718 static int pcmcia_card_add(struct pcmcia_socket
*s
)
720 cistpl_longlink_mfc_t mfc
;
721 unsigned int no_funcs
, i
, no_chains
;
724 if (!(s
->resource_setup_done
)) {
725 ds_dev_dbg(3, &s
->dev
,
726 "no resources available, delaying card_add\n");
727 return -EAGAIN
; /* try again, but later... */
730 if (pcmcia_validate_mem(s
)) {
731 ds_dev_dbg(3, &s
->dev
, "validating mem resources failed, "
732 "delaying card_add\n");
733 return -EAGAIN
; /* try again, but later... */
736 ret
= pccard_validate_cis(s
, &no_chains
);
737 if (ret
|| !no_chains
) {
738 ds_dev_dbg(0, &s
->dev
, "invalid CIS or invalid resources\n");
742 if (!pccard_read_tuple(s
, BIND_FN_ALL
, CISTPL_LONGLINK_MFC
, &mfc
))
746 s
->functions
= no_funcs
;
748 for (i
=0; i
< no_funcs
; i
++)
749 pcmcia_device_add(s
, i
);
755 static void pcmcia_delayed_add_device(struct work_struct
*work
)
757 struct pcmcia_socket
*s
=
758 container_of(work
, struct pcmcia_socket
, device_add
);
759 ds_dev_dbg(1, &s
->dev
, "adding additional device to %d\n", s
->sock
);
760 pcmcia_device_add(s
, s
->pcmcia_state
.mfc_pfc
);
761 s
->pcmcia_state
.device_add_pending
= 0;
762 s
->pcmcia_state
.mfc_pfc
= 0;
765 static int pcmcia_requery(struct device
*dev
, void * _data
)
767 struct pcmcia_device
*p_dev
= to_pcmcia_dev(dev
);
768 if (!p_dev
->dev
.driver
) {
769 ds_dev_dbg(1, dev
, "update device information\n");
770 pcmcia_device_query(p_dev
);
776 static void pcmcia_bus_rescan(struct pcmcia_socket
*skt
, int new_cis
)
782 /* must be called with skt_mutex held */
783 ds_dev_dbg(0, &skt
->dev
, "re-scanning socket %d\n", skt
->sock
);
785 spin_lock_irqsave(&pcmcia_dev_list_lock
, flags
);
786 if (list_empty(&skt
->devices_list
))
788 spin_unlock_irqrestore(&pcmcia_dev_list_lock
, flags
);
790 /* If this is because of a CIS override, start over */
791 if (new_cis
&& !no_devices
)
792 pcmcia_card_remove(skt
, NULL
);
794 /* if no devices were added for this socket yet because of
795 * missing resource information or other trouble, we need to
797 if (no_devices
|| new_cis
) {
798 ret
= pcmcia_card_add(skt
);
803 /* some device information might have changed because of a CIS
804 * update or because we can finally read it correctly... so
805 * determine it again, overwriting old values if necessary. */
806 bus_for_each_dev(&pcmcia_bus_type
, NULL
, NULL
, pcmcia_requery
);
808 /* we re-scan all devices, not just the ones connected to this
809 * socket. This does not matter, though. */
810 ret
= bus_rescan_devices(&pcmcia_bus_type
);
812 printk(KERN_INFO
"pcmcia: bus_rescan_devices failed\n");
815 #ifdef CONFIG_PCMCIA_LOAD_CIS
818 * pcmcia_load_firmware - load CIS from userspace if device-provided is broken
819 * @dev: the pcmcia device which needs a CIS override
820 * @filename: requested filename in /lib/firmware/
822 * This uses the in-kernel firmware loading mechanism to use a "fake CIS" if
823 * the one provided by the card is broken. The firmware files reside in
824 * /lib/firmware/ in userspace.
826 static int pcmcia_load_firmware(struct pcmcia_device
*dev
, char * filename
)
828 struct pcmcia_socket
*s
= dev
->socket
;
829 const struct firmware
*fw
;
833 cistpl_longlink_mfc_t mfc
;
838 ds_dev_dbg(1, &dev
->dev
, "trying to load CIS file %s\n", filename
);
840 if (request_firmware(&fw
, filename
, &dev
->dev
) == 0) {
841 if (fw
->size
>= CISTPL_MAX_CIS_SIZE
) {
843 dev_printk(KERN_ERR
, &dev
->dev
,
844 "pcmcia: CIS override is too big\n");
848 if (!pcmcia_replace_cis(s
, fw
->data
, fw
->size
))
851 dev_printk(KERN_ERR
, &dev
->dev
,
852 "pcmcia: CIS override failed\n");
857 /* update information */
858 pcmcia_device_query(dev
);
860 /* does this cis override add or remove functions? */
861 old_funcs
= s
->functions
;
863 if (!pccard_read_tuple(s
, BIND_FN_ALL
, CISTPL_LONGLINK_MFC
, &mfc
))
867 s
->functions
= no_funcs
;
869 if (old_funcs
> no_funcs
)
870 pcmcia_card_remove(s
, dev
);
871 else if (no_funcs
> old_funcs
)
872 pcmcia_add_device_later(s
, 1);
875 release_firmware(fw
);
880 #else /* !CONFIG_PCMCIA_LOAD_CIS */
882 static inline int pcmcia_load_firmware(struct pcmcia_device
*dev
, char * filename
)
890 static inline int pcmcia_devmatch(struct pcmcia_device
*dev
,
891 struct pcmcia_device_id
*did
)
893 if (did
->match_flags
& PCMCIA_DEV_ID_MATCH_MANF_ID
) {
894 if ((!dev
->has_manf_id
) || (dev
->manf_id
!= did
->manf_id
))
898 if (did
->match_flags
& PCMCIA_DEV_ID_MATCH_CARD_ID
) {
899 if ((!dev
->has_card_id
) || (dev
->card_id
!= did
->card_id
))
903 if (did
->match_flags
& PCMCIA_DEV_ID_MATCH_FUNCTION
) {
904 if (dev
->func
!= did
->function
)
908 if (did
->match_flags
& PCMCIA_DEV_ID_MATCH_PROD_ID1
) {
909 if (!dev
->prod_id
[0])
911 if (strcmp(did
->prod_id
[0], dev
->prod_id
[0]))
915 if (did
->match_flags
& PCMCIA_DEV_ID_MATCH_PROD_ID2
) {
916 if (!dev
->prod_id
[1])
918 if (strcmp(did
->prod_id
[1], dev
->prod_id
[1]))
922 if (did
->match_flags
& PCMCIA_DEV_ID_MATCH_PROD_ID3
) {
923 if (!dev
->prod_id
[2])
925 if (strcmp(did
->prod_id
[2], dev
->prod_id
[2]))
929 if (did
->match_flags
& PCMCIA_DEV_ID_MATCH_PROD_ID4
) {
930 if (!dev
->prod_id
[3])
932 if (strcmp(did
->prod_id
[3], dev
->prod_id
[3]))
936 if (did
->match_flags
& PCMCIA_DEV_ID_MATCH_DEVICE_NO
) {
937 if (dev
->device_no
!= did
->device_no
)
941 if (did
->match_flags
& PCMCIA_DEV_ID_MATCH_FUNC_ID
) {
942 if ((!dev
->has_func_id
) || (dev
->func_id
!= did
->func_id
))
945 /* if this is a pseudo-multi-function device,
946 * we need explicit matches */
947 if (did
->match_flags
& PCMCIA_DEV_ID_MATCH_DEVICE_NO
)
952 /* also, FUNC_ID matching needs to be activated by userspace
953 * after it has re-checked that there is no possible module
954 * with a prod_id/manf_id/card_id match.
956 ds_dev_dbg(0, &dev
->dev
,
957 "skipping FUNC_ID match until userspace interaction\n");
958 if (!dev
->allow_func_id_match
)
962 if (did
->match_flags
& PCMCIA_DEV_ID_MATCH_FAKE_CIS
) {
963 ds_dev_dbg(0, &dev
->dev
, "device needs a fake CIS\n");
964 if (!dev
->socket
->fake_cis
)
965 pcmcia_load_firmware(dev
, did
->cisfile
);
967 if (!dev
->socket
->fake_cis
)
971 if (did
->match_flags
& PCMCIA_DEV_ID_MATCH_ANONYMOUS
) {
976 if (dev
->has_manf_id
|| dev
->has_card_id
|| dev
->has_func_id
)
980 dev_set_drvdata(&dev
->dev
, did
);
986 static int pcmcia_bus_match(struct device
* dev
, struct device_driver
* drv
) {
987 struct pcmcia_device
* p_dev
= to_pcmcia_dev(dev
);
988 struct pcmcia_driver
* p_drv
= to_pcmcia_drv(drv
);
989 struct pcmcia_device_id
*did
= p_drv
->id_table
;
990 struct pcmcia_dynid
*dynid
;
992 /* match dynamic devices first */
993 spin_lock(&p_drv
->dynids
.lock
);
994 list_for_each_entry(dynid
, &p_drv
->dynids
.list
, node
) {
995 ds_dev_dbg(3, dev
, "trying to match to %s\n", drv
->name
);
996 if (pcmcia_devmatch(p_dev
, &dynid
->id
)) {
997 ds_dev_dbg(0, dev
, "matched to %s\n", drv
->name
);
998 spin_unlock(&p_drv
->dynids
.lock
);
1002 spin_unlock(&p_drv
->dynids
.lock
);
1004 #ifdef CONFIG_PCMCIA_IOCTL
1005 /* matching by cardmgr */
1006 if (p_dev
->cardmgr
== p_drv
) {
1007 ds_dev_dbg(0, dev
, "cardmgr matched to %s\n", drv
->name
);
1012 while (did
&& did
->match_flags
) {
1013 ds_dev_dbg(3, dev
, "trying to match to %s\n", drv
->name
);
1014 if (pcmcia_devmatch(p_dev
, did
)) {
1015 ds_dev_dbg(0, dev
, "matched to %s\n", drv
->name
);
1024 #ifdef CONFIG_HOTPLUG
1026 static int pcmcia_bus_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
1028 struct pcmcia_device
*p_dev
;
1030 u32 hash
[4] = { 0, 0, 0, 0};
1035 p_dev
= to_pcmcia_dev(dev
);
1037 /* calculate hashes */
1038 for (i
=0; i
<4; i
++) {
1039 if (!p_dev
->prod_id
[i
])
1041 hash
[i
] = crc32(0, p_dev
->prod_id
[i
], strlen(p_dev
->prod_id
[i
]));
1044 if (add_uevent_var(env
, "SOCKET_NO=%u", p_dev
->socket
->sock
))
1047 if (add_uevent_var(env
, "DEVICE_NO=%02X", p_dev
->device_no
))
1050 if (add_uevent_var(env
, "MODALIAS=pcmcia:m%04Xc%04Xf%02Xfn%02Xpfn%02X"
1051 "pa%08Xpb%08Xpc%08Xpd%08X",
1052 p_dev
->has_manf_id
? p_dev
->manf_id
: 0,
1053 p_dev
->has_card_id
? p_dev
->card_id
: 0,
1054 p_dev
->has_func_id
? p_dev
->func_id
: 0,
1068 static int pcmcia_bus_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
1075 /************************ runtime PM support ***************************/
1077 static int pcmcia_dev_suspend(struct device
*dev
, pm_message_t state
);
1078 static int pcmcia_dev_resume(struct device
*dev
);
1080 static int runtime_suspend(struct device
*dev
)
1085 rc
= pcmcia_dev_suspend(dev
, PMSG_SUSPEND
);
1090 static void runtime_resume(struct device
*dev
)
1095 rc
= pcmcia_dev_resume(dev
);
1099 /************************ per-device sysfs output ***************************/
1101 #define pcmcia_device_attr(field, test, format) \
1102 static ssize_t field##_show (struct device *dev, struct device_attribute *attr, char *buf) \
1104 struct pcmcia_device *p_dev = to_pcmcia_dev(dev); \
1105 return p_dev->test ? sprintf (buf, format, p_dev->field) : -ENODEV; \
1108 #define pcmcia_device_stringattr(name, field) \
1109 static ssize_t name##_show (struct device *dev, struct device_attribute *attr, char *buf) \
1111 struct pcmcia_device *p_dev = to_pcmcia_dev(dev); \
1112 return p_dev->field ? sprintf (buf, "%s\n", p_dev->field) : -ENODEV; \
1115 pcmcia_device_attr(func
, socket
, "0x%02x\n");
1116 pcmcia_device_attr(func_id
, has_func_id
, "0x%02x\n");
1117 pcmcia_device_attr(manf_id
, has_manf_id
, "0x%04x\n");
1118 pcmcia_device_attr(card_id
, has_card_id
, "0x%04x\n");
1119 pcmcia_device_stringattr(prod_id1
, prod_id
[0]);
1120 pcmcia_device_stringattr(prod_id2
, prod_id
[1]);
1121 pcmcia_device_stringattr(prod_id3
, prod_id
[2]);
1122 pcmcia_device_stringattr(prod_id4
, prod_id
[3]);
1125 static ssize_t
pcmcia_show_pm_state(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
1127 struct pcmcia_device
*p_dev
= to_pcmcia_dev(dev
);
1129 if (p_dev
->suspended
)
1130 return sprintf(buf
, "off\n");
1132 return sprintf(buf
, "on\n");
1135 static ssize_t
pcmcia_store_pm_state(struct device
*dev
, struct device_attribute
*attr
,
1136 const char *buf
, size_t count
)
1138 struct pcmcia_device
*p_dev
= to_pcmcia_dev(dev
);
1144 if ((!p_dev
->suspended
) && !strncmp(buf
, "off", 3))
1145 ret
= runtime_suspend(dev
);
1146 else if (p_dev
->suspended
&& !strncmp(buf
, "on", 2))
1147 runtime_resume(dev
);
1149 return ret
? ret
: count
;
1153 static ssize_t
modalias_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
1155 struct pcmcia_device
*p_dev
= to_pcmcia_dev(dev
);
1157 u32 hash
[4] = { 0, 0, 0, 0};
1159 /* calculate hashes */
1160 for (i
=0; i
<4; i
++) {
1161 if (!p_dev
->prod_id
[i
])
1163 hash
[i
] = crc32(0,p_dev
->prod_id
[i
],strlen(p_dev
->prod_id
[i
]));
1165 return sprintf(buf
, "pcmcia:m%04Xc%04Xf%02Xfn%02Xpfn%02X"
1166 "pa%08Xpb%08Xpc%08Xpd%08X\n",
1167 p_dev
->has_manf_id
? p_dev
->manf_id
: 0,
1168 p_dev
->has_card_id
? p_dev
->card_id
: 0,
1169 p_dev
->has_func_id
? p_dev
->func_id
: 0,
1170 p_dev
->func
, p_dev
->device_no
,
1171 hash
[0], hash
[1], hash
[2], hash
[3]);
1174 static ssize_t
pcmcia_store_allow_func_id_match(struct device
*dev
,
1175 struct device_attribute
*attr
, const char *buf
, size_t count
)
1177 struct pcmcia_device
*p_dev
= to_pcmcia_dev(dev
);
1183 mutex_lock(&p_dev
->socket
->skt_mutex
);
1184 p_dev
->allow_func_id_match
= 1;
1185 mutex_unlock(&p_dev
->socket
->skt_mutex
);
1187 ret
= bus_rescan_devices(&pcmcia_bus_type
);
1189 printk(KERN_INFO
"pcmcia: bus_rescan_devices failed after "
1190 "allowing func_id matches\n");
1195 static struct device_attribute pcmcia_dev_attrs
[] = {
1196 __ATTR(function
, 0444, func_show
, NULL
),
1197 __ATTR(pm_state
, 0644, pcmcia_show_pm_state
, pcmcia_store_pm_state
),
1201 __ATTR_RO(prod_id1
),
1202 __ATTR_RO(prod_id2
),
1203 __ATTR_RO(prod_id3
),
1204 __ATTR_RO(prod_id4
),
1205 __ATTR_RO(modalias
),
1206 __ATTR(allow_func_id_match
, 0200, NULL
, pcmcia_store_allow_func_id_match
),
1210 /* PM support, also needed for reset */
1212 static int pcmcia_dev_suspend(struct device
* dev
, pm_message_t state
)
1214 struct pcmcia_device
*p_dev
= to_pcmcia_dev(dev
);
1215 struct pcmcia_driver
*p_drv
= NULL
;
1218 if (p_dev
->suspended
)
1221 ds_dev_dbg(2, dev
, "suspending\n");
1224 p_drv
= to_pcmcia_drv(dev
->driver
);
1229 if (p_drv
->suspend
) {
1230 ret
= p_drv
->suspend(p_dev
);
1232 dev_printk(KERN_ERR
, dev
,
1233 "pcmcia: device %s (driver %s) did "
1234 "not want to go to sleep (%d)\n",
1235 p_dev
->devname
, p_drv
->drv
.name
, ret
);
1240 if (p_dev
->device_no
== p_dev
->func
) {
1241 ds_dev_dbg(2, dev
, "releasing configuration\n");
1242 pcmcia_release_configuration(p_dev
);
1247 p_dev
->suspended
= 1;
1252 static int pcmcia_dev_resume(struct device
* dev
)
1254 struct pcmcia_device
*p_dev
= to_pcmcia_dev(dev
);
1255 struct pcmcia_driver
*p_drv
= NULL
;
1258 if (!p_dev
->suspended
)
1261 ds_dev_dbg(2, dev
, "resuming\n");
1264 p_drv
= to_pcmcia_drv(dev
->driver
);
1269 if (p_dev
->device_no
== p_dev
->func
) {
1270 ds_dev_dbg(2, dev
, "requesting configuration\n");
1271 ret
= pcmcia_request_configuration(p_dev
, &p_dev
->conf
);
1277 ret
= p_drv
->resume(p_dev
);
1281 p_dev
->suspended
= 0;
1286 static int pcmcia_bus_suspend_callback(struct device
*dev
, void * _data
)
1288 struct pcmcia_socket
*skt
= _data
;
1289 struct pcmcia_device
*p_dev
= to_pcmcia_dev(dev
);
1291 if (p_dev
->socket
!= skt
|| p_dev
->suspended
)
1294 return runtime_suspend(dev
);
1297 static int pcmcia_bus_resume_callback(struct device
*dev
, void * _data
)
1299 struct pcmcia_socket
*skt
= _data
;
1300 struct pcmcia_device
*p_dev
= to_pcmcia_dev(dev
);
1302 if (p_dev
->socket
!= skt
|| !p_dev
->suspended
)
1305 runtime_resume(dev
);
1310 static int pcmcia_bus_resume(struct pcmcia_socket
*skt
)
1312 ds_dev_dbg(2, &skt
->dev
, "resuming socket %d\n", skt
->sock
);
1313 bus_for_each_dev(&pcmcia_bus_type
, NULL
, skt
, pcmcia_bus_resume_callback
);
1317 static int pcmcia_bus_suspend(struct pcmcia_socket
*skt
)
1319 ds_dev_dbg(2, &skt
->dev
, "suspending socket %d\n", skt
->sock
);
1320 if (bus_for_each_dev(&pcmcia_bus_type
, NULL
, skt
,
1321 pcmcia_bus_suspend_callback
)) {
1322 pcmcia_bus_resume(skt
);
1329 /*======================================================================
1331 The card status event handler.
1333 ======================================================================*/
1335 /* Normally, the event is passed to individual drivers after
1336 * informing userspace. Only for CS_EVENT_CARD_REMOVAL this
1337 * is inversed to maintain historic compatibility.
1340 static int ds_event(struct pcmcia_socket
*skt
, event_t event
, int priority
)
1342 struct pcmcia_socket
*s
= pcmcia_get_socket(skt
);
1345 dev_printk(KERN_ERR
, &skt
->dev
,
1346 "PCMCIA obtaining reference to socket " \
1347 "failed, event 0x%x lost!\n", event
);
1351 ds_dev_dbg(1, &skt
->dev
, "ds_event(0x%06x, %d, 0x%p)\n",
1352 event
, priority
, skt
);
1355 case CS_EVENT_CARD_REMOVAL
:
1356 s
->pcmcia_state
.present
= 0;
1357 pcmcia_card_remove(skt
, NULL
);
1358 handle_event(skt
, event
);
1361 case CS_EVENT_CARD_INSERTION
:
1362 s
->pcmcia_state
.present
= 1;
1363 pcmcia_card_add(skt
);
1364 handle_event(skt
, event
);
1367 case CS_EVENT_EJECTION_REQUEST
:
1370 case CS_EVENT_PM_SUSPEND
:
1371 case CS_EVENT_PM_RESUME
:
1372 case CS_EVENT_RESET_PHYSICAL
:
1373 case CS_EVENT_CARD_RESET
:
1375 handle_event(skt
, event
);
1379 pcmcia_put_socket(s
);
1385 struct pcmcia_device
* pcmcia_dev_present(struct pcmcia_device
*_p_dev
)
1387 struct pcmcia_device
*p_dev
;
1388 struct pcmcia_device
*ret
= NULL
;
1390 p_dev
= pcmcia_get_dev(_p_dev
);
1394 if (!p_dev
->socket
->pcmcia_state
.present
)
1397 if (p_dev
->_removed
)
1400 if (p_dev
->suspended
)
1405 pcmcia_put_dev(p_dev
);
1408 EXPORT_SYMBOL(pcmcia_dev_present
);
1411 static struct pcmcia_callback pcmcia_bus_callback
= {
1412 .owner
= THIS_MODULE
,
1414 .requery
= pcmcia_bus_rescan
,
1415 .suspend
= pcmcia_bus_suspend
,
1416 .resume
= pcmcia_bus_resume
,
1419 static int __devinit
pcmcia_bus_add_socket(struct device
*dev
,
1420 struct class_interface
*class_intf
)
1422 struct pcmcia_socket
*socket
= dev_get_drvdata(dev
);
1425 socket
= pcmcia_get_socket(socket
);
1427 dev_printk(KERN_ERR
, dev
,
1428 "PCMCIA obtaining reference to socket failed\n");
1433 * Ugly. But we want to wait for the socket threads to have started up.
1434 * We really should let the drivers themselves drive some of this..
1438 #ifdef CONFIG_PCMCIA_IOCTL
1439 init_waitqueue_head(&socket
->queue
);
1441 INIT_LIST_HEAD(&socket
->devices_list
);
1442 INIT_WORK(&socket
->device_add
, pcmcia_delayed_add_device
);
1443 memset(&socket
->pcmcia_state
, 0, sizeof(u8
));
1444 socket
->device_count
= 0;
1446 ret
= pccard_register_pcmcia(socket
, &pcmcia_bus_callback
);
1448 dev_printk(KERN_ERR
, dev
, "PCMCIA registration failed\n");
1449 pcmcia_put_socket(socket
);
1456 static void pcmcia_bus_remove_socket(struct device
*dev
,
1457 struct class_interface
*class_intf
)
1459 struct pcmcia_socket
*socket
= dev_get_drvdata(dev
);
1464 socket
->pcmcia_state
.dead
= 1;
1465 pccard_register_pcmcia(socket
, NULL
);
1467 /* unregister any unbound devices */
1468 mutex_lock(&socket
->skt_mutex
);
1469 pcmcia_card_remove(socket
, NULL
);
1470 mutex_unlock(&socket
->skt_mutex
);
1472 pcmcia_put_socket(socket
);
1478 /* the pcmcia_bus_interface is used to handle pcmcia socket devices */
1479 static struct class_interface pcmcia_bus_interface __refdata
= {
1480 .class = &pcmcia_socket_class
,
1481 .add_dev
= &pcmcia_bus_add_socket
,
1482 .remove_dev
= &pcmcia_bus_remove_socket
,
1486 struct bus_type pcmcia_bus_type
= {
1488 .uevent
= pcmcia_bus_uevent
,
1489 .match
= pcmcia_bus_match
,
1490 .dev_attrs
= pcmcia_dev_attrs
,
1491 .probe
= pcmcia_device_probe
,
1492 .remove
= pcmcia_device_remove
,
1493 .suspend
= pcmcia_dev_suspend
,
1494 .resume
= pcmcia_dev_resume
,
1498 static int __init
init_pcmcia_bus(void)
1502 spin_lock_init(&pcmcia_dev_list_lock
);
1504 ret
= bus_register(&pcmcia_bus_type
);
1506 printk(KERN_WARNING
"pcmcia: bus_register error: %d\n", ret
);
1509 ret
= class_interface_register(&pcmcia_bus_interface
);
1512 "pcmcia: class_interface_register error: %d\n", ret
);
1513 bus_unregister(&pcmcia_bus_type
);
1517 pcmcia_setup_ioctl();
1521 fs_initcall(init_pcmcia_bus
); /* one level after subsys_initcall so that
1522 * pcmcia_socket_class is already registered */
1525 static void __exit
exit_pcmcia_bus(void)
1527 pcmcia_cleanup_ioctl();
1529 class_interface_unregister(&pcmcia_bus_interface
);
1531 bus_unregister(&pcmcia_bus_type
);
1533 module_exit(exit_pcmcia_bus
);