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 - 2004 Dominik Brodowski
16 #include <linux/config.h>
17 #include <linux/module.h>
18 #include <linux/moduleparam.h>
19 #include <linux/init.h>
20 #include <linux/kernel.h>
21 #include <linux/major.h>
22 #include <linux/string.h>
23 #include <linux/errno.h>
24 #include <linux/slab.h>
26 #include <linux/fcntl.h>
27 #include <linux/sched.h>
28 #include <linux/smp_lock.h>
29 #include <linux/timer.h>
30 #include <linux/ioctl.h>
31 #include <linux/proc_fs.h>
32 #include <linux/poll.h>
33 #include <linux/pci.h>
34 #include <linux/list.h>
35 #include <linux/delay.h>
36 #include <linux/kref.h>
37 #include <linux/workqueue.h>
38 #include <linux/crc32.h>
39 #include <linux/firmware.h>
41 #include <asm/atomic.h>
43 #define IN_CARD_SERVICES
44 #include <pcmcia/version.h>
45 #include <pcmcia/cs_types.h>
46 #include <pcmcia/cs.h>
47 #include <pcmcia/bulkmem.h>
48 #include <pcmcia/cistpl.h>
49 #include <pcmcia/ds.h>
50 #include <pcmcia/ss.h>
52 #include "cs_internal.h"
54 /*====================================================================*/
56 /* Module parameters */
58 MODULE_AUTHOR("David Hinds <dahinds@users.sourceforge.net>");
59 MODULE_DESCRIPTION("PCMCIA Driver Services");
60 MODULE_LICENSE("GPL");
63 static int ds_pc_debug
;
65 module_param_named(pc_debug
, ds_pc_debug
, int, 0644);
67 #define ds_dbg(lvl, fmt, arg...) do { \
68 if (ds_pc_debug > (lvl)) \
69 printk(KERN_DEBUG "ds: " fmt , ## arg); \
72 #define ds_dbg(lvl, fmt, arg...) do { } while (0)
75 /*====================================================================*/
77 /* Device user information */
79 #define USER_MAGIC 0x7ea4
80 #define CHECK_USER(u) \
81 (((u) == NULL) || ((u)->user_magic != USER_MAGIC))
82 typedef struct user_info_t
{
84 int event_head
, event_tail
;
85 event_t event
[MAX_EVENTS
];
86 struct user_info_t
*next
;
87 struct pcmcia_bus_socket
*socket
;
90 /* Socket state information */
91 struct pcmcia_bus_socket
{
93 struct pcmcia_callback callback
;
96 wait_queue_head_t queue
;
97 struct pcmcia_socket
*parent
;
99 /* the PCMCIA devices connected to this socket (normally one, more
100 * for multifunction devices: */
101 struct list_head devices_list
;
102 u8 device_count
; /* the number of devices, used
103 * only internally and subject
104 * to incorrectness and change */
106 u8 device_add_pending
;
107 struct work_struct device_add
;
109 static spinlock_t pcmcia_dev_list_lock
;
111 static struct bus_type pcmcia_bus_type
;
113 #define DS_SOCKET_PRESENT 0x01
114 #define DS_SOCKET_BUSY 0x02
115 #define DS_SOCKET_REMOVAL_PENDING 0x10
116 #define DS_SOCKET_DEAD 0x80
118 /*====================================================================*/
119 #ifdef CONFIG_PCMCIA_IOCTL
121 static int major_dev
= -1;
122 static struct pcmcia_bus_socket
* get_socket_info_by_nr(unsigned int nr
);
123 static struct pcmcia_driver
* get_pcmcia_driver (dev_info_t
*dev_info
);
127 static int unbind_request(struct pcmcia_bus_socket
*s
);
129 /*====================================================================*/
131 /* code which was in cs.c before */
133 /* String tables for error messages */
135 typedef struct lookup_t
{
140 static const lookup_t error_table
[] = {
141 { CS_SUCCESS
, "Operation succeeded" },
142 { CS_BAD_ADAPTER
, "Bad adapter" },
143 { CS_BAD_ATTRIBUTE
, "Bad attribute", },
144 { CS_BAD_BASE
, "Bad base address" },
145 { CS_BAD_EDC
, "Bad EDC" },
146 { CS_BAD_IRQ
, "Bad IRQ" },
147 { CS_BAD_OFFSET
, "Bad offset" },
148 { CS_BAD_PAGE
, "Bad page number" },
149 { CS_READ_FAILURE
, "Read failure" },
150 { CS_BAD_SIZE
, "Bad size" },
151 { CS_BAD_SOCKET
, "Bad socket" },
152 { CS_BAD_TYPE
, "Bad type" },
153 { CS_BAD_VCC
, "Bad Vcc" },
154 { CS_BAD_VPP
, "Bad Vpp" },
155 { CS_BAD_WINDOW
, "Bad window" },
156 { CS_WRITE_FAILURE
, "Write failure" },
157 { CS_NO_CARD
, "No card present" },
158 { CS_UNSUPPORTED_FUNCTION
, "Usupported function" },
159 { CS_UNSUPPORTED_MODE
, "Unsupported mode" },
160 { CS_BAD_SPEED
, "Bad speed" },
161 { CS_BUSY
, "Resource busy" },
162 { CS_GENERAL_FAILURE
, "General failure" },
163 { CS_WRITE_PROTECTED
, "Write protected" },
164 { CS_BAD_ARG_LENGTH
, "Bad argument length" },
165 { CS_BAD_ARGS
, "Bad arguments" },
166 { CS_CONFIGURATION_LOCKED
, "Configuration locked" },
167 { CS_IN_USE
, "Resource in use" },
168 { CS_NO_MORE_ITEMS
, "No more items" },
169 { CS_OUT_OF_RESOURCE
, "Out of resource" },
170 { CS_BAD_HANDLE
, "Bad handle" },
171 { CS_BAD_TUPLE
, "Bad CIS tuple" }
175 static const lookup_t service_table
[] = {
176 { AccessConfigurationRegister
, "AccessConfigurationRegister" },
177 { AddSocketServices
, "AddSocketServices" },
178 { AdjustResourceInfo
, "AdjustResourceInfo" },
179 { CheckEraseQueue
, "CheckEraseQueue" },
180 { CloseMemory
, "CloseMemory" },
181 { DeregisterClient
, "DeregisterClient" },
182 { DeregisterEraseQueue
, "DeregisterEraseQueue" },
183 { GetCardServicesInfo
, "GetCardServicesInfo" },
184 { GetClientInfo
, "GetClientInfo" },
185 { GetConfigurationInfo
, "GetConfigurationInfo" },
186 { GetEventMask
, "GetEventMask" },
187 { GetFirstClient
, "GetFirstClient" },
188 { GetFirstRegion
, "GetFirstRegion" },
189 { GetFirstTuple
, "GetFirstTuple" },
190 { GetNextClient
, "GetNextClient" },
191 { GetNextRegion
, "GetNextRegion" },
192 { GetNextTuple
, "GetNextTuple" },
193 { GetStatus
, "GetStatus" },
194 { GetTupleData
, "GetTupleData" },
195 { MapMemPage
, "MapMemPage" },
196 { ModifyConfiguration
, "ModifyConfiguration" },
197 { ModifyWindow
, "ModifyWindow" },
198 { OpenMemory
, "OpenMemory" },
199 { ParseTuple
, "ParseTuple" },
200 { ReadMemory
, "ReadMemory" },
201 { RegisterClient
, "RegisterClient" },
202 { RegisterEraseQueue
, "RegisterEraseQueue" },
203 { RegisterMTD
, "RegisterMTD" },
204 { ReleaseConfiguration
, "ReleaseConfiguration" },
205 { ReleaseIO
, "ReleaseIO" },
206 { ReleaseIRQ
, "ReleaseIRQ" },
207 { ReleaseWindow
, "ReleaseWindow" },
208 { RequestConfiguration
, "RequestConfiguration" },
209 { RequestIO
, "RequestIO" },
210 { RequestIRQ
, "RequestIRQ" },
211 { RequestSocketMask
, "RequestSocketMask" },
212 { RequestWindow
, "RequestWindow" },
213 { ResetCard
, "ResetCard" },
214 { SetEventMask
, "SetEventMask" },
215 { ValidateCIS
, "ValidateCIS" },
216 { WriteMemory
, "WriteMemory" },
217 { BindDevice
, "BindDevice" },
218 { BindMTD
, "BindMTD" },
219 { ReportError
, "ReportError" },
220 { SuspendCard
, "SuspendCard" },
221 { ResumeCard
, "ResumeCard" },
222 { EjectCard
, "EjectCard" },
223 { InsertCard
, "InsertCard" },
224 { ReplaceCIS
, "ReplaceCIS" }
228 static int pcmcia_report_error(client_handle_t handle
, error_info_t
*err
)
233 if (CHECK_HANDLE(handle
))
236 struct pcmcia_device
*p_dev
= handle_to_pdev(handle
);
237 printk(KERN_NOTICE
"%s: ", p_dev
->dev
.bus_id
);
240 for (i
= 0; i
< ARRAY_SIZE(service_table
); i
++)
241 if (service_table
[i
].key
== err
->func
)
243 if (i
< ARRAY_SIZE(service_table
))
244 serv
= service_table
[i
].msg
;
246 serv
= "Unknown service number";
248 for (i
= 0; i
< ARRAY_SIZE(error_table
); i
++)
249 if (error_table
[i
].key
== err
->retcode
)
251 if (i
< ARRAY_SIZE(error_table
))
252 printk("%s: %s\n", serv
, error_table
[i
].msg
);
254 printk("%s: Unknown error code %#x\n", serv
, err
->retcode
);
259 /* end of code which was in cs.c before */
261 /*======================================================================*/
263 void cs_error(client_handle_t handle
, int func
, int ret
)
265 error_info_t err
= { func
, ret
};
266 pcmcia_report_error(handle
, &err
);
268 EXPORT_SYMBOL(cs_error
);
271 static void pcmcia_check_driver(struct pcmcia_driver
*p_drv
)
273 struct pcmcia_device_id
*did
= p_drv
->id_table
;
277 while (did
&& did
->match_flags
) {
278 for (i
=0; i
<4; i
++) {
279 if (!did
->prod_id
[i
])
282 hash
= crc32(0, did
->prod_id
[i
], strlen(did
->prod_id
[i
]));
283 if (hash
== did
->prod_id_hash
[i
])
286 printk(KERN_DEBUG
"pcmcia: %s: invalid hash for "
287 "product string \"%s\": is 0x%x, should "
288 "be 0x%x\n", p_drv
->drv
.name
, did
->prod_id
[i
],
289 did
->prod_id_hash
[i
], hash
);
290 printk(KERN_DEBUG
"pcmcia: see "
291 "Documentation/pcmcia/devicetable.txt for "
301 #ifdef CONFIG_PCMCIA_LOAD_CIS
304 * pcmcia_load_firmware - load CIS from userspace if device-provided is broken
305 * @dev - the pcmcia device which needs a CIS override
306 * @filename - requested filename in /lib/firmware/cis/
308 * This uses the in-kernel firmware loading mechanism to use a "fake CIS" if
309 * the one provided by the card is broken. The firmware files reside in
310 * /lib/firmware/cis/ in userspace.
312 static int pcmcia_load_firmware(struct pcmcia_device
*dev
, char * filename
)
314 struct pcmcia_socket
*s
= dev
->socket
;
315 const struct firmware
*fw
;
323 ds_dbg(1, "trying to load firmware %s\n", filename
);
325 if (strlen(filename
) > 14)
328 snprintf(path
, 20, "%s", filename
);
330 if (request_firmware(&fw
, path
, &dev
->dev
) == 0) {
331 if (fw
->size
>= CISTPL_MAX_CIS_SIZE
)
334 cis
= kmalloc(sizeof(cisdump_t
), GFP_KERNEL
);
338 memset(cis
, 0, sizeof(cisdump_t
));
340 cis
->Length
= fw
->size
+ 1;
341 memcpy(cis
->Data
, fw
->data
, fw
->size
);
343 if (!pcmcia_replace_cis(s
, cis
))
347 release_firmware(fw
);
352 #else /* !CONFIG_PCMCIA_LOAD_CIS */
354 static inline int pcmcia_load_firmware(struct pcmcia_device
*dev
, char * filename
)
362 /*======================================================================*/
365 static void pcmcia_release_bus_socket(struct kref
*refcount
)
367 struct pcmcia_bus_socket
*s
= container_of(refcount
, struct pcmcia_bus_socket
, refcount
);
368 pcmcia_put_socket(s
->parent
);
372 static void pcmcia_put_bus_socket(struct pcmcia_bus_socket
*s
)
374 kref_put(&s
->refcount
, pcmcia_release_bus_socket
);
377 static struct pcmcia_bus_socket
*pcmcia_get_bus_socket(struct pcmcia_bus_socket
*s
)
379 kref_get(&s
->refcount
);
384 * pcmcia_register_driver - register a PCMCIA driver with the bus core
386 * Registers a PCMCIA driver with the PCMCIA bus core.
388 static int pcmcia_device_probe(struct device
*dev
);
389 static int pcmcia_device_remove(struct device
* dev
);
391 int pcmcia_register_driver(struct pcmcia_driver
*driver
)
396 pcmcia_check_driver(driver
);
398 /* initialize common fields */
399 driver
->drv
.bus
= &pcmcia_bus_type
;
400 driver
->drv
.owner
= driver
->owner
;
401 driver
->drv
.probe
= pcmcia_device_probe
;
402 driver
->drv
.remove
= pcmcia_device_remove
;
404 return driver_register(&driver
->drv
);
406 EXPORT_SYMBOL(pcmcia_register_driver
);
409 * pcmcia_unregister_driver - unregister a PCMCIA driver with the bus core
411 void pcmcia_unregister_driver(struct pcmcia_driver
*driver
)
413 driver_unregister(&driver
->drv
);
415 EXPORT_SYMBOL(pcmcia_unregister_driver
);
417 #ifdef CONFIG_PCMCIA_IOCTL
418 #ifdef CONFIG_PROC_FS
419 static struct proc_dir_entry
*proc_pccard
= NULL
;
421 static int proc_read_drivers_callback(struct device_driver
*driver
, void *d
)
424 struct pcmcia_driver
*p_drv
= container_of(driver
,
425 struct pcmcia_driver
, drv
);
427 *p
+= sprintf(*p
, "%-24.24s 1 %d\n", p_drv
->drv
.name
,
428 #ifdef CONFIG_MODULE_UNLOAD
429 (p_drv
->owner
) ? module_refcount(p_drv
->owner
) : 1
439 static int proc_read_drivers(char *buf
, char **start
, off_t pos
,
440 int count
, int *eof
, void *data
)
444 bus_for_each_drv(&pcmcia_bus_type
, NULL
,
445 (void *) &p
, proc_read_drivers_callback
);
452 /* pcmcia_device handling */
454 static struct pcmcia_device
* pcmcia_get_dev(struct pcmcia_device
*p_dev
)
456 struct device
*tmp_dev
;
457 tmp_dev
= get_device(&p_dev
->dev
);
460 return to_pcmcia_dev(tmp_dev
);
463 static void pcmcia_put_dev(struct pcmcia_device
*p_dev
)
466 put_device(&p_dev
->dev
);
469 static void pcmcia_release_dev(struct device
*dev
)
471 struct pcmcia_device
*p_dev
= to_pcmcia_dev(dev
);
472 ds_dbg(1, "releasing dev %p\n", p_dev
);
473 pcmcia_put_bus_socket(p_dev
->socket
->pcmcia
);
478 static int pcmcia_device_probe(struct device
* dev
)
480 struct pcmcia_device
*p_dev
;
481 struct pcmcia_driver
*p_drv
;
484 dev
= get_device(dev
);
488 p_dev
= to_pcmcia_dev(dev
);
489 p_drv
= to_pcmcia_drv(dev
->driver
);
491 if (!try_module_get(p_drv
->owner
)) {
497 p_dev
->instance
= p_drv
->attach();
498 if ((!p_dev
->instance
) || (p_dev
->client
.state
& CLIENT_UNBOUND
)) {
499 printk(KERN_NOTICE
"ds: unable to create instance "
500 "of '%s'!\n", p_drv
->drv
.name
);
506 module_put(p_drv
->owner
);
508 if ((ret
) || !(p_drv
->attach
))
514 static int pcmcia_device_remove(struct device
* dev
)
516 struct pcmcia_device
*p_dev
;
517 struct pcmcia_driver
*p_drv
;
519 /* detach the "instance" */
520 p_dev
= to_pcmcia_dev(dev
);
521 p_drv
= to_pcmcia_drv(dev
->driver
);
524 if ((p_drv
->detach
) && (p_dev
->instance
)) {
525 p_drv
->detach(p_dev
->instance
);
526 /* from pcmcia_probe_device */
527 put_device(&p_dev
->dev
);
529 module_put(p_drv
->owner
);
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
;
547 if (!pccard_read_tuple(p_dev
->socket
, p_dev
->func
,
548 CISTPL_MANFID
, &manf_id
)) {
549 p_dev
->manf_id
= manf_id
.manf
;
550 p_dev
->card_id
= manf_id
.card
;
551 p_dev
->has_manf_id
= 1;
552 p_dev
->has_card_id
= 1;
555 if (!pccard_read_tuple(p_dev
->socket
, p_dev
->func
,
556 CISTPL_FUNCID
, &func_id
)) {
557 p_dev
->func_id
= func_id
.func
;
558 p_dev
->has_func_id
= 1;
560 /* rule of thumb: cards with no FUNCID, but with
561 * common memory device geometry information, are
562 * probably memory cards (from pcmcia-cs) */
563 cistpl_device_geo_t devgeo
;
564 if (!pccard_read_tuple(p_dev
->socket
, p_dev
->func
,
565 CISTPL_DEVICE_GEO
, &devgeo
)) {
566 ds_dbg(0, "mem device geometry probably means "
568 p_dev
->func_id
= CISTPL_FUNCID_MEMORY
;
569 p_dev
->has_func_id
= 1;
573 if (!pccard_read_tuple(p_dev
->socket
, p_dev
->func
, CISTPL_VERS_1
,
575 for (i
=0; i
< vers1
.ns
; i
++) {
579 tmp
= vers1
.str
+ vers1
.ofs
[i
];
581 length
= strlen(tmp
) + 1;
582 if ((length
< 3) || (length
> 255))
585 p_dev
->prod_id
[i
] = kmalloc(sizeof(char) * length
,
587 if (!p_dev
->prod_id
[i
])
590 p_dev
->prod_id
[i
] = strncpy(p_dev
->prod_id
[i
],
599 /* device_add_lock is needed to avoid double registration by cardmgr and kernel.
600 * Serializes pcmcia_device_add; will most likely be removed in future.
602 * While it has the caveat that adding new PCMCIA devices inside(!) device_register()
603 * won't work, this doesn't matter much at the moment: the driver core doesn't
606 static DECLARE_MUTEX(device_add_lock
);
608 static struct pcmcia_device
* pcmcia_device_add(struct pcmcia_bus_socket
*s
, unsigned int function
)
610 struct pcmcia_device
*p_dev
;
613 s
= pcmcia_get_bus_socket(s
);
617 down(&device_add_lock
);
619 /* max of 2 devices per card */
620 if (s
->device_count
== 2)
623 p_dev
= kmalloc(sizeof(struct pcmcia_device
), GFP_KERNEL
);
626 memset(p_dev
, 0, sizeof(struct pcmcia_device
));
628 p_dev
->socket
= s
->parent
;
629 p_dev
->device_no
= (s
->device_count
++);
630 p_dev
->func
= function
;
632 p_dev
->dev
.bus
= &pcmcia_bus_type
;
633 p_dev
->dev
.parent
= s
->parent
->dev
.dev
;
634 p_dev
->dev
.release
= pcmcia_release_dev
;
635 sprintf (p_dev
->dev
.bus_id
, "%d.%d", p_dev
->socket
->sock
, p_dev
->device_no
);
638 p_dev
->client
.client_magic
= CLIENT_MAGIC
;
639 p_dev
->client
.Socket
= s
->parent
;
640 p_dev
->client
.Function
= function
;
641 p_dev
->client
.state
= CLIENT_UNBOUND
;
643 /* Add to the list in pcmcia_bus_socket */
644 spin_lock_irqsave(&pcmcia_dev_list_lock
, flags
);
645 list_add_tail(&p_dev
->socket_device_list
, &s
->devices_list
);
646 spin_unlock_irqrestore(&pcmcia_dev_list_lock
, flags
);
648 pcmcia_device_query(p_dev
);
650 if (device_register(&p_dev
->dev
)) {
651 spin_lock_irqsave(&pcmcia_dev_list_lock
, flags
);
652 list_del(&p_dev
->socket_device_list
);
653 spin_unlock_irqrestore(&pcmcia_dev_list_lock
, flags
);
658 up(&device_add_lock
);
666 up(&device_add_lock
);
667 pcmcia_put_bus_socket(s
);
673 static int pcmcia_card_add(struct pcmcia_socket
*s
)
676 cistpl_longlink_mfc_t mfc
;
677 unsigned int no_funcs
, i
;
680 if (!(s
->resource_setup_done
))
681 return -EAGAIN
; /* try again, but later... */
683 pcmcia_validate_mem(s
);
684 ret
= pccard_validate_cis(s
, BIND_FN_ALL
, &cisinfo
);
685 if (ret
|| !cisinfo
.Chains
) {
686 ds_dbg(0, "invalid CIS or invalid resources\n");
690 if (!pccard_read_tuple(s
, BIND_FN_ALL
, CISTPL_LONGLINK_MFC
, &mfc
))
695 /* this doesn't handle multifunction devices on one pcmcia function
697 for (i
=0; i
< no_funcs
; i
++)
698 pcmcia_device_add(s
->pcmcia
, i
);
704 static void pcmcia_delayed_add_pseudo_device(void *data
)
706 struct pcmcia_bus_socket
*s
= data
;
707 pcmcia_device_add(s
, 0);
708 s
->device_add_pending
= 0;
711 static inline void pcmcia_add_pseudo_device(struct pcmcia_bus_socket
*s
)
713 if (!s
->device_add_pending
) {
714 schedule_work(&s
->device_add
);
715 s
->device_add_pending
= 1;
720 static int pcmcia_requery(struct device
*dev
, void * _data
)
722 struct pcmcia_device
*p_dev
= to_pcmcia_dev(dev
);
723 if (!p_dev
->dev
.driver
)
724 pcmcia_device_query(p_dev
);
729 static void pcmcia_bus_rescan(struct pcmcia_socket
*skt
)
734 /* must be called with skt_sem held */
735 spin_lock_irqsave(&pcmcia_dev_list_lock
, flags
);
736 if (list_empty(&skt
->pcmcia
->devices_list
))
738 spin_unlock_irqrestore(&pcmcia_dev_list_lock
, flags
);
740 /* if no devices were added for this socket yet because of
741 * missing resource information or other trouble, we need to
744 int ret
= pcmcia_card_add(skt
);
749 /* some device information might have changed because of a CIS
750 * update or because we can finally read it correctly... so
751 * determine it again, overwriting old values if necessary. */
752 bus_for_each_dev(&pcmcia_bus_type
, NULL
, NULL
, pcmcia_requery
);
754 /* we re-scan all devices, not just the ones connected to this
755 * socket. This does not matter, though. */
756 bus_rescan_devices(&pcmcia_bus_type
);
759 static inline int pcmcia_devmatch(struct pcmcia_device
*dev
,
760 struct pcmcia_device_id
*did
)
762 if (did
->match_flags
& PCMCIA_DEV_ID_MATCH_MANF_ID
) {
763 if ((!dev
->has_manf_id
) || (dev
->manf_id
!= did
->manf_id
))
767 if (did
->match_flags
& PCMCIA_DEV_ID_MATCH_CARD_ID
) {
768 if ((!dev
->has_card_id
) || (dev
->card_id
!= did
->card_id
))
772 if (did
->match_flags
& PCMCIA_DEV_ID_MATCH_FUNCTION
) {
773 if (dev
->func
!= did
->function
)
777 if (did
->match_flags
& PCMCIA_DEV_ID_MATCH_PROD_ID1
) {
778 if (!dev
->prod_id
[0])
780 if (strcmp(did
->prod_id
[0], dev
->prod_id
[0]))
784 if (did
->match_flags
& PCMCIA_DEV_ID_MATCH_PROD_ID2
) {
785 if (!dev
->prod_id
[1])
787 if (strcmp(did
->prod_id
[1], dev
->prod_id
[1]))
791 if (did
->match_flags
& PCMCIA_DEV_ID_MATCH_PROD_ID3
) {
792 if (!dev
->prod_id
[2])
794 if (strcmp(did
->prod_id
[2], dev
->prod_id
[2]))
798 if (did
->match_flags
& PCMCIA_DEV_ID_MATCH_PROD_ID4
) {
799 if (!dev
->prod_id
[3])
801 if (strcmp(did
->prod_id
[3], dev
->prod_id
[3]))
805 if (did
->match_flags
& PCMCIA_DEV_ID_MATCH_DEVICE_NO
) {
806 /* handle pseudo multifunction devices:
807 * there are at most two pseudo multifunction devices.
808 * if we're matching against the first, schedule a
809 * call which will then check whether there are two
810 * pseudo devices, and if not, add the second one.
812 if (dev
->device_no
== 0)
813 pcmcia_add_pseudo_device(dev
->socket
->pcmcia
);
815 if (dev
->device_no
!= did
->device_no
)
819 if (did
->match_flags
& PCMCIA_DEV_ID_MATCH_FUNC_ID
) {
820 if ((!dev
->has_func_id
) || (dev
->func_id
!= did
->func_id
))
823 /* if this is a pseudo-multi-function device,
824 * we need explicit matches */
825 if (did
->match_flags
& PCMCIA_DEV_ID_MATCH_DEVICE_NO
)
830 /* also, FUNC_ID matching needs to be activated by userspace
831 * after it has re-checked that there is no possible module
832 * with a prod_id/manf_id/card_id match.
834 if (!dev
->allow_func_id_match
)
838 if (did
->match_flags
& PCMCIA_DEV_ID_MATCH_FAKE_CIS
) {
839 if (!dev
->socket
->fake_cis
)
840 pcmcia_load_firmware(dev
, did
->cisfile
);
842 if (!dev
->socket
->fake_cis
)
846 if (did
->match_flags
& PCMCIA_DEV_ID_MATCH_ANONYMOUS
) {
851 if (dev
->has_manf_id
|| dev
->has_card_id
|| dev
->has_func_id
)
855 dev
->dev
.driver_data
= (void *) did
;
861 static int pcmcia_bus_match(struct device
* dev
, struct device_driver
* drv
) {
862 struct pcmcia_device
* p_dev
= to_pcmcia_dev(dev
);
863 struct pcmcia_driver
* p_drv
= to_pcmcia_drv(drv
);
864 struct pcmcia_device_id
*did
= p_drv
->id_table
;
866 /* matching by cardmgr */
867 if (p_dev
->cardmgr
== p_drv
)
870 while (did
&& did
->match_flags
) {
871 if (pcmcia_devmatch(p_dev
, did
))
879 #ifdef CONFIG_HOTPLUG
881 static int pcmcia_bus_hotplug(struct device
*dev
, char **envp
, int num_envp
,
882 char *buffer
, int buffer_size
)
884 struct pcmcia_device
*p_dev
;
886 u32 hash
[4] = { 0, 0, 0, 0};
891 p_dev
= to_pcmcia_dev(dev
);
893 /* calculate hashes */
894 for (i
=0; i
<4; i
++) {
895 if (!p_dev
->prod_id
[i
])
897 hash
[i
] = crc32(0, p_dev
->prod_id
[i
], strlen(p_dev
->prod_id
[i
]));
902 if (add_hotplug_env_var(envp
, num_envp
, &i
,
903 buffer
, buffer_size
, &length
,
905 p_dev
->socket
->sock
))
908 if (add_hotplug_env_var(envp
, num_envp
, &i
,
909 buffer
, buffer_size
, &length
,
914 if (add_hotplug_env_var(envp
, num_envp
, &i
,
915 buffer
, buffer_size
, &length
,
916 "MODALIAS=pcmcia:m%04Xc%04Xf%02Xfn%02Xpfn%02X"
917 "pa%08Xpb%08Xpc%08Xpd%08X",
918 p_dev
->has_manf_id
? p_dev
->manf_id
: 0,
919 p_dev
->has_card_id
? p_dev
->card_id
: 0,
920 p_dev
->has_func_id
? p_dev
->func_id
: 0,
936 static int pcmcia_bus_hotplug(struct device
*dev
, char **envp
, int num_envp
,
937 char *buffer
, int buffer_size
)
944 /************************ per-device sysfs output ***************************/
946 #define pcmcia_device_attr(field, test, format) \
947 static ssize_t field##_show (struct device *dev, struct device_attribute *attr, char *buf) \
949 struct pcmcia_device *p_dev = to_pcmcia_dev(dev); \
950 return p_dev->test ? sprintf (buf, format, p_dev->field) : -ENODEV; \
953 #define pcmcia_device_stringattr(name, field) \
954 static ssize_t name##_show (struct device *dev, struct device_attribute *attr, char *buf) \
956 struct pcmcia_device *p_dev = to_pcmcia_dev(dev); \
957 return p_dev->field ? sprintf (buf, "%s\n", p_dev->field) : -ENODEV; \
960 pcmcia_device_attr(func
, socket
, "0x%02x\n");
961 pcmcia_device_attr(func_id
, has_func_id
, "0x%02x\n");
962 pcmcia_device_attr(manf_id
, has_manf_id
, "0x%04x\n");
963 pcmcia_device_attr(card_id
, has_card_id
, "0x%04x\n");
964 pcmcia_device_stringattr(prod_id1
, prod_id
[0]);
965 pcmcia_device_stringattr(prod_id2
, prod_id
[1]);
966 pcmcia_device_stringattr(prod_id3
, prod_id
[2]);
967 pcmcia_device_stringattr(prod_id4
, prod_id
[3]);
970 static ssize_t
pcmcia_store_allow_func_id_match (struct device
* dev
, struct device_attribute
*attr
,
971 const char * buf
, size_t count
)
973 struct pcmcia_device
*p_dev
= to_pcmcia_dev(dev
);
977 down(&p_dev
->socket
->skt_sem
);
978 p_dev
->allow_func_id_match
= 1;
979 up(&p_dev
->socket
->skt_sem
);
981 bus_rescan_devices(&pcmcia_bus_type
);
986 static struct device_attribute pcmcia_dev_attrs
[] = {
987 __ATTR(function
, 0444, func_show
, NULL
),
995 __ATTR(allow_func_id_match
, 0200, NULL
, pcmcia_store_allow_func_id_match
),
1000 /*======================================================================
1002 These manage a ring buffer of events pending for one user process
1004 ======================================================================*/
1006 #ifdef CONFIG_PCMCIA_IOCTL
1008 static int queue_empty(user_info_t
*user
)
1010 return (user
->event_head
== user
->event_tail
);
1013 static event_t
get_queued_event(user_info_t
*user
)
1015 user
->event_tail
= (user
->event_tail
+1) % MAX_EVENTS
;
1016 return user
->event
[user
->event_tail
];
1019 static void queue_event(user_info_t
*user
, event_t event
)
1021 user
->event_head
= (user
->event_head
+1) % MAX_EVENTS
;
1022 if (user
->event_head
== user
->event_tail
)
1023 user
->event_tail
= (user
->event_tail
+1) % MAX_EVENTS
;
1024 user
->event
[user
->event_head
] = event
;
1027 static void handle_event(struct pcmcia_bus_socket
*s
, event_t event
)
1030 for (user
= s
->user
; user
; user
= user
->next
)
1031 queue_event(user
, event
);
1032 wake_up_interruptible(&s
->queue
);
1035 static inline void handle_event(struct pcmcia_bus_socket
*s
, event_t event
) { return; }
1036 static inline int handle_request(struct pcmcia_bus_socket
*s
, event_t event
) { return CS_SUCCESS
; }
1041 /*======================================================================
1043 The card status event handler.
1045 ======================================================================*/
1047 struct send_event_data
{
1048 struct pcmcia_socket
*skt
;
1053 static int send_event_callback(struct device
*dev
, void * _data
)
1055 struct pcmcia_device
*p_dev
= to_pcmcia_dev(dev
);
1056 struct send_event_data
*data
= _data
;
1058 /* we get called for all sockets, but may only pass the event
1059 * for drivers _on the affected socket_ */
1060 if (p_dev
->socket
!= data
->skt
)
1063 if (p_dev
->client
.state
& (CLIENT_UNBOUND
|CLIENT_STALE
))
1066 if (p_dev
->client
.EventMask
& data
->event
)
1067 return EVENT(&p_dev
->client
, data
->event
, data
->priority
);
1072 static int send_event(struct pcmcia_socket
*s
, event_t event
, int priority
)
1075 struct send_event_data
private;
1076 struct pcmcia_bus_socket
*skt
= pcmcia_get_bus_socket(s
->pcmcia
);
1082 private.event
= event
;
1083 private.priority
= priority
;
1085 ret
= bus_for_each_dev(&pcmcia_bus_type
, NULL
, &private, send_event_callback
);
1087 pcmcia_put_bus_socket(skt
);
1092 /* Normally, the event is passed to individual drivers after
1093 * informing userspace. Only for CS_EVENT_CARD_REMOVAL this
1094 * is inversed to maintain historic compatibility.
1097 static int ds_event(struct pcmcia_socket
*skt
, event_t event
, int priority
)
1099 struct pcmcia_bus_socket
*s
= skt
->pcmcia
;
1102 ds_dbg(1, "ds_event(0x%06x, %d, 0x%p)\n",
1103 event
, priority
, s
);
1107 case CS_EVENT_CARD_REMOVAL
:
1108 s
->state
&= ~DS_SOCKET_PRESENT
;
1109 send_event(skt
, event
, priority
);
1111 handle_event(s
, event
);
1114 case CS_EVENT_CARD_INSERTION
:
1115 s
->state
|= DS_SOCKET_PRESENT
;
1116 pcmcia_card_add(skt
);
1117 handle_event(s
, event
);
1120 case CS_EVENT_EJECTION_REQUEST
:
1121 ret
= send_event(skt
, event
, priority
);
1125 handle_event(s
, event
);
1126 send_event(skt
, event
, priority
);
1134 /*======================================================================
1136 bind_request() and bind_device() are merged by now. Register_client()
1137 is called right at the end of bind_request(), during the driver's
1138 ->attach() call. Individual descriptions:
1140 bind_request() connects a socket to a particular client driver.
1141 It looks up the specified device ID in the list of registered
1142 drivers, binds it to the socket, and tries to create an instance
1143 of the device. unbind_request() deletes a driver instance.
1145 Bind_device() associates a device driver with a particular socket.
1146 It is normally called by Driver Services after it has identified
1147 a newly inserted card. An instance of that driver will then be
1148 eligible to register as a client of this socket.
1150 Register_client() uses the dev_info_t handle to match the
1151 caller with a socket. The driver must have already been bound
1152 to a socket with bind_device() -- in fact, bind_device()
1153 allocates the client structure that will be used.
1155 ======================================================================*/
1157 #ifdef CONFIG_PCMCIA_IOCTL
1159 static int bind_request(struct pcmcia_bus_socket
*s
, bind_info_t
*bind_info
)
1161 struct pcmcia_driver
*p_drv
;
1162 struct pcmcia_device
*p_dev
;
1164 unsigned long flags
;
1166 s
= pcmcia_get_bus_socket(s
);
1170 ds_dbg(2, "bind_request(%d, '%s')\n", s
->parent
->sock
,
1171 (char *)bind_info
->dev_info
);
1173 p_drv
= get_pcmcia_driver(&bind_info
->dev_info
);
1179 if (!try_module_get(p_drv
->owner
)) {
1181 goto err_put_driver
;
1184 spin_lock_irqsave(&pcmcia_dev_list_lock
, flags
);
1185 list_for_each_entry(p_dev
, &s
->devices_list
, socket_device_list
) {
1186 if (p_dev
->func
== bind_info
->function
) {
1187 if ((p_dev
->dev
.driver
== &p_drv
->drv
)) {
1188 if (p_dev
->cardmgr
) {
1189 /* if there's already a device
1190 * registered, and it was registered
1191 * by userspace before, we need to
1192 * return the "instance". */
1193 spin_unlock_irqrestore(&pcmcia_dev_list_lock
, flags
);
1194 bind_info
->instance
= p_dev
->instance
;
1196 goto err_put_module
;
1198 /* the correct driver managed to bind
1199 * itself magically to the correct
1201 spin_unlock_irqrestore(&pcmcia_dev_list_lock
, flags
);
1202 p_dev
->cardmgr
= p_drv
;
1204 goto err_put_module
;
1206 } else if (!p_dev
->dev
.driver
) {
1207 /* there's already a device available where
1208 * no device has been bound to yet. So we don't
1209 * need to register a device! */
1210 spin_unlock_irqrestore(&pcmcia_dev_list_lock
, flags
);
1215 spin_unlock_irqrestore(&pcmcia_dev_list_lock
, flags
);
1217 p_dev
= pcmcia_device_add(s
, bind_info
->function
);
1220 goto err_put_module
;
1224 p_dev
->cardmgr
= p_drv
;
1226 /* if a driver is already running, we can abort */
1227 if (p_dev
->dev
.driver
)
1228 goto err_put_module
;
1231 * Prevent this racing with a card insertion.
1233 down(&s
->parent
->skt_sem
);
1234 bus_rescan_devices(&pcmcia_bus_type
);
1235 up(&s
->parent
->skt_sem
);
1237 /* check whether the driver indeed matched. I don't care if this
1238 * is racy or not, because it can only happen on cardmgr access
1241 if (!(p_dev
->dev
.driver
== &p_drv
->drv
))
1242 p_dev
->cardmgr
= NULL
;
1245 module_put(p_drv
->owner
);
1247 put_driver(&p_drv
->drv
);
1249 pcmcia_put_bus_socket(s
);
1252 } /* bind_request */
1257 int pcmcia_register_client(client_handle_t
*handle
, client_reg_t
*req
)
1259 client_t
*client
= NULL
;
1260 struct pcmcia_socket
*s
;
1261 struct pcmcia_bus_socket
*skt
= NULL
;
1262 struct pcmcia_device
*p_dev
= NULL
;
1264 /* Look for unbound client with matching dev_info */
1265 down_read(&pcmcia_socket_list_rwsem
);
1266 list_for_each_entry(s
, &pcmcia_socket_list
, socket_list
) {
1267 unsigned long flags
;
1269 if (s
->state
& SOCKET_CARDBUS
)
1275 skt
= pcmcia_get_bus_socket(skt
);
1278 spin_lock_irqsave(&pcmcia_dev_list_lock
, flags
);
1279 list_for_each_entry(p_dev
, &skt
->devices_list
, socket_device_list
) {
1280 struct pcmcia_driver
*p_drv
;
1281 p_dev
= pcmcia_get_dev(p_dev
);
1284 if (!(p_dev
->client
.state
& CLIENT_UNBOUND
) ||
1285 (!p_dev
->dev
.driver
)) {
1286 pcmcia_put_dev(p_dev
);
1289 p_drv
= to_pcmcia_drv(p_dev
->dev
.driver
);
1290 if (!strncmp(p_drv
->drv
.name
, (char *)req
->dev_info
, DEV_NAME_LEN
)) {
1291 client
= &p_dev
->client
;
1292 spin_unlock_irqrestore(&pcmcia_dev_list_lock
, flags
);
1295 pcmcia_put_dev(p_dev
);
1297 spin_unlock_irqrestore(&pcmcia_dev_list_lock
, flags
);
1298 pcmcia_put_bus_socket(skt
);
1301 up_read(&pcmcia_socket_list_rwsem
);
1302 if (!p_dev
|| !client
)
1305 pcmcia_put_bus_socket(skt
); /* safe, as we already hold a reference from bind_device */
1308 client
->state
&= ~CLIENT_UNBOUND
;
1310 client
->EventMask
= req
->EventMask
;
1311 client
->event_handler
= req
->event_handler
;
1312 client
->event_callback_args
= req
->event_callback_args
;
1313 client
->event_callback_args
.client_handle
= client
;
1315 if (s
->state
& SOCKET_CARDBUS
)
1316 client
->state
|= CLIENT_CARDBUS
;
1318 if ((!(s
->state
& SOCKET_CARDBUS
)) && (s
->functions
== 0) &&
1319 (client
->Function
!= BIND_FN_ALL
)) {
1320 cistpl_longlink_mfc_t mfc
;
1321 if (pccard_read_tuple(s
, client
->Function
, CISTPL_LONGLINK_MFC
, &mfc
)
1323 s
->functions
= mfc
.nfn
;
1326 s
->config
= kmalloc(sizeof(config_t
) * s
->functions
,
1329 goto out_no_resource
;
1330 memset(s
->config
, 0, sizeof(config_t
) * s
->functions
);
1333 ds_dbg(1, "register_client(): client 0x%p, dev %s\n",
1334 client
, p_dev
->dev
.bus_id
);
1335 if (client
->EventMask
& CS_EVENT_REGISTRATION_COMPLETE
)
1336 EVENT(client
, CS_EVENT_REGISTRATION_COMPLETE
, CS_EVENT_PRI_LOW
);
1338 if ((s
->state
& (SOCKET_PRESENT
|SOCKET_CARDBUS
)) == SOCKET_PRESENT
) {
1339 if (client
->EventMask
& CS_EVENT_CARD_INSERTION
)
1340 EVENT(client
, CS_EVENT_CARD_INSERTION
, CS_EVENT_PRI_LOW
);
1346 pcmcia_put_dev(p_dev
);
1347 return CS_OUT_OF_RESOURCE
;
1348 } /* register_client */
1349 EXPORT_SYMBOL(pcmcia_register_client
);
1352 /*====================================================================*/
1353 #ifdef CONFIG_PCMCIA_IOCTL
1355 extern struct pci_bus
*pcmcia_lookup_bus(struct pcmcia_socket
*s
);
1357 static int get_device_info(struct pcmcia_bus_socket
*s
, bind_info_t
*bind_info
, int first
)
1360 struct pcmcia_device
*p_dev
;
1361 unsigned long flags
;
1364 #ifdef CONFIG_CARDBUS
1366 * Some unbelievably ugly code to associate the PCI cardbus
1367 * device and its driver with the PCMCIA "bind" information.
1370 struct pci_bus
*bus
;
1372 bus
= pcmcia_lookup_bus(s
->parent
);
1374 struct list_head
*list
;
1375 struct pci_dev
*dev
= NULL
;
1377 list
= bus
->devices
.next
;
1378 while (list
!= &bus
->devices
) {
1379 struct pci_dev
*pdev
= pci_dev_b(list
);
1387 /* Try to handle "next" here some way? */
1389 if (dev
&& dev
->driver
) {
1390 strlcpy(bind_info
->name
, dev
->driver
->name
, DEV_NAME_LEN
);
1391 bind_info
->major
= 0;
1392 bind_info
->minor
= 0;
1393 bind_info
->next
= NULL
;
1400 spin_lock_irqsave(&pcmcia_dev_list_lock
, flags
);
1401 list_for_each_entry(p_dev
, &s
->devices_list
, socket_device_list
) {
1402 if (p_dev
->func
== bind_info
->function
) {
1403 p_dev
= pcmcia_get_dev(p_dev
);
1409 spin_unlock_irqrestore(&pcmcia_dev_list_lock
, flags
);
1413 spin_unlock_irqrestore(&pcmcia_dev_list_lock
, flags
);
1415 if ((!p_dev
->instance
) ||
1416 (p_dev
->instance
->state
& DEV_CONFIG_PENDING
)) {
1422 node
= p_dev
->instance
->dev
;
1424 for (node
= p_dev
->instance
->dev
; node
; node
= node
->next
)
1425 if (node
== bind_info
->next
)
1432 strlcpy(bind_info
->name
, node
->dev_name
, DEV_NAME_LEN
);
1433 bind_info
->major
= node
->major
;
1434 bind_info
->minor
= node
->minor
;
1435 bind_info
->next
= node
->next
;
1438 pcmcia_put_dev(p_dev
);
1440 } /* get_device_info */
1444 /*====================================================================*/
1446 /* unbind _all_ devices attached to a given pcmcia_bus_socket. The
1447 * drivers have been called with EVENT_CARD_REMOVAL before.
1449 static int unbind_request(struct pcmcia_bus_socket
*s
)
1451 struct pcmcia_device
*p_dev
;
1452 unsigned long flags
;
1454 ds_dbg(2, "unbind_request(%d)\n", s
->parent
->sock
);
1456 s
->device_count
= 0;
1459 /* unregister all pcmcia_devices registered with this socket*/
1460 spin_lock_irqsave(&pcmcia_dev_list_lock
, flags
);
1461 if (list_empty(&s
->devices_list
)) {
1462 spin_unlock_irqrestore(&pcmcia_dev_list_lock
, flags
);
1465 p_dev
= list_entry((&s
->devices_list
)->next
, struct pcmcia_device
, socket_device_list
);
1466 list_del(&p_dev
->socket_device_list
);
1467 p_dev
->client
.state
|= CLIENT_STALE
;
1468 spin_unlock_irqrestore(&pcmcia_dev_list_lock
, flags
);
1470 device_unregister(&p_dev
->dev
);
1474 } /* unbind_request */
1476 int pcmcia_deregister_client(client_handle_t handle
)
1478 struct pcmcia_socket
*s
;
1480 struct pcmcia_device
*p_dev
= handle_to_pdev(handle
);
1482 if (CHECK_HANDLE(handle
))
1483 return CS_BAD_HANDLE
;
1486 ds_dbg(1, "deregister_client(%p)\n", handle
);
1488 if (handle
->state
& (CLIENT_IRQ_REQ
|CLIENT_IO_REQ
|CLIENT_CONFIG_LOCKED
))
1490 for (i
= 0; i
< MAX_WIN
; i
++)
1491 if (handle
->state
& CLIENT_WIN_REQ(i
))
1494 if (handle
->state
& CLIENT_STALE
) {
1495 handle
->client_magic
= 0;
1496 handle
->state
&= ~CLIENT_STALE
;
1497 pcmcia_put_dev(p_dev
);
1499 handle
->state
= CLIENT_UNBOUND
;
1500 handle
->event_handler
= NULL
;
1505 printk(KERN_WARNING
"ds: deregister_client was called too early.\n");
1507 } /* deregister_client */
1508 EXPORT_SYMBOL(pcmcia_deregister_client
);
1511 /*======================================================================
1513 The user-mode PC Card device interface
1515 ======================================================================*/
1517 #ifdef CONFIG_PCMCIA_IOCTL
1519 static int ds_open(struct inode
*inode
, struct file
*file
)
1521 socket_t i
= iminor(inode
);
1522 struct pcmcia_bus_socket
*s
;
1525 ds_dbg(0, "ds_open(socket %d)\n", i
);
1527 s
= get_socket_info_by_nr(i
);
1530 s
= pcmcia_get_bus_socket(s
);
1534 if ((file
->f_flags
& O_ACCMODE
) != O_RDONLY
) {
1535 if (s
->state
& DS_SOCKET_BUSY
) {
1536 pcmcia_put_bus_socket(s
);
1540 s
->state
|= DS_SOCKET_BUSY
;
1543 user
= kmalloc(sizeof(user_info_t
), GFP_KERNEL
);
1545 pcmcia_put_bus_socket(s
);
1548 user
->event_tail
= user
->event_head
= 0;
1549 user
->next
= s
->user
;
1550 user
->user_magic
= USER_MAGIC
;
1553 file
->private_data
= user
;
1555 if (s
->state
& DS_SOCKET_PRESENT
)
1556 queue_event(user
, CS_EVENT_CARD_INSERTION
);
1560 /*====================================================================*/
1562 static int ds_release(struct inode
*inode
, struct file
*file
)
1564 struct pcmcia_bus_socket
*s
;
1565 user_info_t
*user
, **link
;
1567 ds_dbg(0, "ds_release(socket %d)\n", iminor(inode
));
1569 user
= file
->private_data
;
1570 if (CHECK_USER(user
))
1575 /* Unlink user data structure */
1576 if ((file
->f_flags
& O_ACCMODE
) != O_RDONLY
) {
1577 s
->state
&= ~DS_SOCKET_BUSY
;
1579 file
->private_data
= NULL
;
1580 for (link
= &s
->user
; *link
; link
= &(*link
)->next
)
1581 if (*link
== user
) break;
1585 user
->user_magic
= 0;
1587 pcmcia_put_bus_socket(s
);
1592 /*====================================================================*/
1594 static ssize_t
ds_read(struct file
*file
, char __user
*buf
,
1595 size_t count
, loff_t
*ppos
)
1597 struct pcmcia_bus_socket
*s
;
1601 ds_dbg(2, "ds_read(socket %d)\n", iminor(file
->f_dentry
->d_inode
));
1606 user
= file
->private_data
;
1607 if (CHECK_USER(user
))
1611 if (s
->state
& DS_SOCKET_DEAD
)
1614 ret
= wait_event_interruptible(s
->queue
, !queue_empty(user
));
1616 ret
= put_user(get_queued_event(user
), (int __user
*)buf
) ? -EFAULT
: 4;
1621 /*====================================================================*/
1623 static ssize_t
ds_write(struct file
*file
, const char __user
*buf
,
1624 size_t count
, loff_t
*ppos
)
1626 ds_dbg(2, "ds_write(socket %d)\n", iminor(file
->f_dentry
->d_inode
));
1630 if ((file
->f_flags
& O_ACCMODE
) == O_RDONLY
)
1636 /*====================================================================*/
1638 /* No kernel lock - fine */
1639 static u_int
ds_poll(struct file
*file
, poll_table
*wait
)
1641 struct pcmcia_bus_socket
*s
;
1644 ds_dbg(2, "ds_poll(socket %d)\n", iminor(file
->f_dentry
->d_inode
));
1646 user
= file
->private_data
;
1647 if (CHECK_USER(user
))
1651 * We don't check for a dead socket here since that
1652 * will send cardmgr into an endless spin.
1654 poll_wait(file
, &s
->queue
, wait
);
1655 if (!queue_empty(user
))
1656 return POLLIN
| POLLRDNORM
;
1660 /*====================================================================*/
1662 extern int pcmcia_adjust_resource_info(adjust_t
*adj
);
1664 static int ds_ioctl(struct inode
* inode
, struct file
* file
,
1665 u_int cmd
, u_long arg
)
1667 struct pcmcia_bus_socket
*s
;
1668 void __user
*uarg
= (char __user
*)arg
;
1671 ds_ioctl_arg_t
*buf
;
1674 ds_dbg(2, "ds_ioctl(socket %d, %#x, %#lx)\n", iminor(inode
), cmd
, arg
);
1676 user
= file
->private_data
;
1677 if (CHECK_USER(user
))
1681 if (s
->state
& DS_SOCKET_DEAD
)
1684 size
= (cmd
& IOCSIZE_MASK
) >> IOCSIZE_SHIFT
;
1685 if (size
> sizeof(ds_ioctl_arg_t
)) return -EINVAL
;
1687 /* Permission check */
1688 if (!(cmd
& IOC_OUT
) && !capable(CAP_SYS_ADMIN
))
1692 if (!access_ok(VERIFY_READ
, uarg
, size
)) {
1693 ds_dbg(3, "ds_ioctl(): verify_read = %d\n", -EFAULT
);
1697 if (cmd
& IOC_OUT
) {
1698 if (!access_ok(VERIFY_WRITE
, uarg
, size
)) {
1699 ds_dbg(3, "ds_ioctl(): verify_write = %d\n", -EFAULT
);
1703 buf
= kmalloc(sizeof(ds_ioctl_arg_t
), GFP_KERNEL
);
1709 if (cmd
& IOC_IN
) __copy_from_user((char *)buf
, uarg
, size
);
1712 case DS_ADJUST_RESOURCE_INFO
:
1713 ret
= pcmcia_adjust_resource_info(&buf
->adjust
);
1715 case DS_GET_CARD_SERVICES_INFO
:
1716 ret
= pcmcia_get_card_services_info(&buf
->servinfo
);
1718 case DS_GET_CONFIGURATION_INFO
:
1719 if (buf
->config
.Function
&&
1720 (buf
->config
.Function
>= s
->parent
->functions
))
1723 ret
= pccard_get_configuration_info(s
->parent
,
1724 buf
->config
.Function
, &buf
->config
);
1726 case DS_GET_FIRST_TUPLE
:
1727 down(&s
->parent
->skt_sem
);
1728 pcmcia_validate_mem(s
->parent
);
1729 up(&s
->parent
->skt_sem
);
1730 ret
= pccard_get_first_tuple(s
->parent
, BIND_FN_ALL
, &buf
->tuple
);
1732 case DS_GET_NEXT_TUPLE
:
1733 ret
= pccard_get_next_tuple(s
->parent
, BIND_FN_ALL
, &buf
->tuple
);
1735 case DS_GET_TUPLE_DATA
:
1736 buf
->tuple
.TupleData
= buf
->tuple_parse
.data
;
1737 buf
->tuple
.TupleDataMax
= sizeof(buf
->tuple_parse
.data
);
1738 ret
= pccard_get_tuple_data(s
->parent
, &buf
->tuple
);
1740 case DS_PARSE_TUPLE
:
1741 buf
->tuple
.TupleData
= buf
->tuple_parse
.data
;
1742 ret
= pccard_parse_tuple(&buf
->tuple
, &buf
->tuple_parse
.parse
);
1745 ret
= pccard_reset_card(s
->parent
);
1748 if (buf
->status
.Function
&&
1749 (buf
->status
.Function
>= s
->parent
->functions
))
1752 ret
= pccard_get_status(s
->parent
, buf
->status
.Function
, &buf
->status
);
1754 case DS_VALIDATE_CIS
:
1755 down(&s
->parent
->skt_sem
);
1756 pcmcia_validate_mem(s
->parent
);
1757 up(&s
->parent
->skt_sem
);
1758 ret
= pccard_validate_cis(s
->parent
, BIND_FN_ALL
, &buf
->cisinfo
);
1760 case DS_SUSPEND_CARD
:
1761 ret
= pcmcia_suspend_card(s
->parent
);
1763 case DS_RESUME_CARD
:
1764 ret
= pcmcia_resume_card(s
->parent
);
1767 err
= pcmcia_eject_card(s
->parent
);
1769 case DS_INSERT_CARD
:
1770 err
= pcmcia_insert_card(s
->parent
);
1772 case DS_ACCESS_CONFIGURATION_REGISTER
:
1773 if ((buf
->conf_reg
.Action
== CS_WRITE
) && !capable(CAP_SYS_ADMIN
)) {
1777 if (buf
->conf_reg
.Function
&&
1778 (buf
->conf_reg
.Function
>= s
->parent
->functions
))
1781 ret
= pccard_access_configuration_register(s
->parent
,
1782 buf
->conf_reg
.Function
, &buf
->conf_reg
);
1784 case DS_GET_FIRST_REGION
:
1785 case DS_GET_NEXT_REGION
:
1787 if (!capable(CAP_SYS_ADMIN
)) {
1791 static int printed
= 0;
1793 printk(KERN_WARNING
"2.6. kernels use pcmciamtd instead of memory_cs.c and do not require special\n");
1794 printk(KERN_WARNING
"MTD handling any more.\n");
1801 case DS_GET_FIRST_WINDOW
:
1802 ret
= pcmcia_get_window(s
->parent
, &buf
->win_info
.handle
, 0,
1803 &buf
->win_info
.window
);
1805 case DS_GET_NEXT_WINDOW
:
1806 ret
= pcmcia_get_window(s
->parent
, &buf
->win_info
.handle
,
1807 buf
->win_info
.handle
->index
+ 1, &buf
->win_info
.window
);
1809 case DS_GET_MEM_PAGE
:
1810 ret
= pcmcia_get_mem_page(buf
->win_info
.handle
,
1811 &buf
->win_info
.map
);
1813 case DS_REPLACE_CIS
:
1814 ret
= pcmcia_replace_cis(s
->parent
, &buf
->cisdump
);
1816 case DS_BIND_REQUEST
:
1817 if (!capable(CAP_SYS_ADMIN
)) {
1821 err
= bind_request(s
, &buf
->bind_info
);
1823 case DS_GET_DEVICE_INFO
:
1824 err
= get_device_info(s
, &buf
->bind_info
, 1);
1826 case DS_GET_NEXT_DEVICE
:
1827 err
= get_device_info(s
, &buf
->bind_info
, 0);
1829 case DS_UNBIND_REQUEST
:
1836 if ((err
== 0) && (ret
!= CS_SUCCESS
)) {
1837 ds_dbg(2, "ds_ioctl: ret = %d\n", ret
);
1839 case CS_BAD_SOCKET
: case CS_NO_CARD
:
1840 err
= -ENODEV
; break;
1841 case CS_BAD_ARGS
: case CS_BAD_ATTRIBUTE
: case CS_BAD_IRQ
:
1843 err
= -EINVAL
; break;
1845 err
= -EBUSY
; break;
1846 case CS_OUT_OF_RESOURCE
:
1847 err
= -ENOSPC
; break;
1848 case CS_NO_MORE_ITEMS
:
1849 err
= -ENODATA
; break;
1850 case CS_UNSUPPORTED_FUNCTION
:
1851 err
= -ENOSYS
; break;
1857 if (cmd
& IOC_OUT
) {
1858 if (__copy_to_user(uarg
, (char *)buf
, size
))
1867 /*====================================================================*/
1869 static struct file_operations ds_fops
= {
1870 .owner
= THIS_MODULE
,
1872 .release
= ds_release
,
1881 static int __devinit
pcmcia_bus_add_socket(struct class_device
*class_dev
)
1883 struct pcmcia_socket
*socket
= class_get_devdata(class_dev
);
1884 struct pcmcia_bus_socket
*s
;
1887 s
= kmalloc(sizeof(struct pcmcia_bus_socket
), GFP_KERNEL
);
1890 memset(s
, 0, sizeof(struct pcmcia_bus_socket
));
1892 /* get reference to parent socket */
1893 s
->parent
= pcmcia_get_socket(socket
);
1895 printk(KERN_ERR
"PCMCIA obtaining reference to socket %p failed\n", socket
);
1900 kref_init(&s
->refcount
);
1903 * Ugly. But we want to wait for the socket threads to have started up.
1904 * We really should let the drivers themselves drive some of this..
1908 init_waitqueue_head(&s
->queue
);
1909 INIT_LIST_HEAD(&s
->devices_list
);
1910 INIT_WORK(&s
->device_add
, pcmcia_delayed_add_pseudo_device
, s
);
1912 /* Set up hotline to Card Services */
1913 s
->callback
.owner
= THIS_MODULE
;
1914 s
->callback
.event
= &ds_event
;
1915 s
->callback
.requery
= &pcmcia_bus_rescan
;
1918 ret
= pccard_register_pcmcia(socket
, &s
->callback
);
1920 printk(KERN_ERR
"PCMCIA registration PCCard core failed for socket %p\n", socket
);
1921 pcmcia_put_bus_socket(s
);
1922 socket
->pcmcia
= NULL
;
1930 static void pcmcia_bus_remove_socket(struct class_device
*class_dev
)
1932 struct pcmcia_socket
*socket
= class_get_devdata(class_dev
);
1934 if (!socket
|| !socket
->pcmcia
)
1937 pccard_register_pcmcia(socket
, NULL
);
1939 socket
->pcmcia
->state
|= DS_SOCKET_DEAD
;
1940 pcmcia_put_bus_socket(socket
->pcmcia
);
1941 socket
->pcmcia
= NULL
;
1947 /* the pcmcia_bus_interface is used to handle pcmcia socket devices */
1948 static struct class_interface pcmcia_bus_interface
= {
1949 .class = &pcmcia_socket_class
,
1950 .add
= &pcmcia_bus_add_socket
,
1951 .remove
= &pcmcia_bus_remove_socket
,
1955 static struct bus_type pcmcia_bus_type
= {
1957 .hotplug
= pcmcia_bus_hotplug
,
1958 .match
= pcmcia_bus_match
,
1959 .dev_attrs
= pcmcia_dev_attrs
,
1963 static int __init
init_pcmcia_bus(void)
1965 #ifdef CONFIG_PCMCIA_IOCTL
1969 spin_lock_init(&pcmcia_dev_list_lock
);
1971 bus_register(&pcmcia_bus_type
);
1972 class_interface_register(&pcmcia_bus_interface
);
1974 #ifdef CONFIG_PCMCIA_IOCTL
1975 /* Set up character device for user mode clients */
1976 i
= register_chrdev(0, "pcmcia", &ds_fops
);
1978 printk(KERN_NOTICE
"unable to find a free device # for "
1979 "Driver Services (error=%d)\n", i
);
1983 #ifdef CONFIG_PROC_FS
1984 proc_pccard
= proc_mkdir("pccard", proc_bus
);
1986 create_proc_read_entry("drivers",0,proc_pccard
,proc_read_drivers
,NULL
);
1992 fs_initcall(init_pcmcia_bus
); /* one level after subsys_initcall so that
1993 * pcmcia_socket_class is already registered */
1996 static void __exit
exit_pcmcia_bus(void)
1998 class_interface_unregister(&pcmcia_bus_interface
);
2000 #ifdef CONFIG_PCMCIA_IOCTL
2001 #ifdef CONFIG_PROC_FS
2003 remove_proc_entry("drivers", proc_pccard
);
2004 remove_proc_entry("pccard", proc_bus
);
2007 if (major_dev
!= -1)
2008 unregister_chrdev(major_dev
, "pcmcia");
2011 bus_unregister(&pcmcia_bus_type
);
2013 module_exit(exit_pcmcia_bus
);
2017 /* helpers for backwards-compatible functions */
2018 #ifdef CONFIG_PCMCIA_IOCTL
2019 static struct pcmcia_bus_socket
* get_socket_info_by_nr(unsigned int nr
)
2021 struct pcmcia_socket
* s
= pcmcia_get_socket_by_nr(nr
);
2028 /* backwards-compatible accessing of driver --- by name! */
2030 static struct pcmcia_driver
* get_pcmcia_driver (dev_info_t
*dev_info
)
2032 struct device_driver
*drv
;
2033 struct pcmcia_driver
*p_drv
;
2035 drv
= driver_find((char *) dev_info
, &pcmcia_bus_type
);
2039 p_drv
= container_of(drv
, struct pcmcia_driver
, drv
);