]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/pcmcia/ds.c
[PATCH] pcmcia: device and driver matching
[mirror_ubuntu-bionic-kernel.git] / drivers / pcmcia / ds.c
CommitLineData
1da177e4
LT
1/*
2 * ds.c -- 16-bit PCMCIA core support
3 *
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.
7 *
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.
11 *
12 * (C) 1999 David A. Hinds
13 * (C) 2003 - 2004 Dominik Brodowski
14 */
15
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>
25#include <linux/mm.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>
840c2ac5 38#include <linux/crc32.h>
1da177e4
LT
39
40#include <asm/atomic.h>
41
42#define IN_CARD_SERVICES
43#include <pcmcia/version.h>
44#include <pcmcia/cs_types.h>
45#include <pcmcia/cs.h>
46#include <pcmcia/bulkmem.h>
47#include <pcmcia/cistpl.h>
48#include <pcmcia/ds.h>
49#include <pcmcia/ss.h>
50
51#include "cs_internal.h"
52
53/*====================================================================*/
54
55/* Module parameters */
56
57MODULE_AUTHOR("David Hinds <dahinds@users.sourceforge.net>");
58MODULE_DESCRIPTION("PCMCIA Driver Services");
59MODULE_LICENSE("GPL");
60
61#ifdef DEBUG
62int ds_pc_debug;
63
64module_param_named(pc_debug, ds_pc_debug, int, 0644);
65
66#define ds_dbg(lvl, fmt, arg...) do { \
67 if (ds_pc_debug > (lvl)) \
68 printk(KERN_DEBUG "ds: " fmt , ## arg); \
69} while (0)
70#else
71#define ds_dbg(lvl, fmt, arg...) do { } while (0)
72#endif
73
74/*====================================================================*/
75
76/* Device user information */
77#define MAX_EVENTS 32
78#define USER_MAGIC 0x7ea4
79#define CHECK_USER(u) \
80 (((u) == NULL) || ((u)->user_magic != USER_MAGIC))
81typedef struct user_info_t {
82 u_int user_magic;
83 int event_head, event_tail;
84 event_t event[MAX_EVENTS];
85 struct user_info_t *next;
86 struct pcmcia_bus_socket *socket;
87} user_info_t;
88
89/* Socket state information */
90struct pcmcia_bus_socket {
91 struct kref refcount;
92 struct pcmcia_callback callback;
93 int state;
94 user_info_t *user;
95 wait_queue_head_t queue;
96 struct pcmcia_socket *parent;
97
98 /* the PCMCIA devices connected to this socket (normally one, more
99 * for multifunction devices: */
100 struct list_head devices_list;
101 u8 device_count; /* the number of devices, used
102 * only internally and subject
103 * to incorrectness and change */
1ad275e3
DB
104
105 u8 device_add_pending;
106 struct work_struct device_add;
1da177e4
LT
107};
108static spinlock_t pcmcia_dev_list_lock;
109
110#define DS_SOCKET_PRESENT 0x01
111#define DS_SOCKET_BUSY 0x02
112#define DS_SOCKET_REMOVAL_PENDING 0x10
113#define DS_SOCKET_DEAD 0x80
114
115/*====================================================================*/
116
117static int major_dev = -1;
118
119static int unbind_request(struct pcmcia_bus_socket *s);
120
121/*====================================================================*/
122
123/* code which was in cs.c before */
124
125/* String tables for error messages */
126
127typedef struct lookup_t {
128 int key;
129 char *msg;
130} lookup_t;
131
132static const lookup_t error_table[] = {
133 { CS_SUCCESS, "Operation succeeded" },
134 { CS_BAD_ADAPTER, "Bad adapter" },
135 { CS_BAD_ATTRIBUTE, "Bad attribute", },
136 { CS_BAD_BASE, "Bad base address" },
137 { CS_BAD_EDC, "Bad EDC" },
138 { CS_BAD_IRQ, "Bad IRQ" },
139 { CS_BAD_OFFSET, "Bad offset" },
140 { CS_BAD_PAGE, "Bad page number" },
141 { CS_READ_FAILURE, "Read failure" },
142 { CS_BAD_SIZE, "Bad size" },
143 { CS_BAD_SOCKET, "Bad socket" },
144 { CS_BAD_TYPE, "Bad type" },
145 { CS_BAD_VCC, "Bad Vcc" },
146 { CS_BAD_VPP, "Bad Vpp" },
147 { CS_BAD_WINDOW, "Bad window" },
148 { CS_WRITE_FAILURE, "Write failure" },
149 { CS_NO_CARD, "No card present" },
150 { CS_UNSUPPORTED_FUNCTION, "Usupported function" },
151 { CS_UNSUPPORTED_MODE, "Unsupported mode" },
152 { CS_BAD_SPEED, "Bad speed" },
153 { CS_BUSY, "Resource busy" },
154 { CS_GENERAL_FAILURE, "General failure" },
155 { CS_WRITE_PROTECTED, "Write protected" },
156 { CS_BAD_ARG_LENGTH, "Bad argument length" },
157 { CS_BAD_ARGS, "Bad arguments" },
158 { CS_CONFIGURATION_LOCKED, "Configuration locked" },
159 { CS_IN_USE, "Resource in use" },
160 { CS_NO_MORE_ITEMS, "No more items" },
161 { CS_OUT_OF_RESOURCE, "Out of resource" },
162 { CS_BAD_HANDLE, "Bad handle" },
163 { CS_BAD_TUPLE, "Bad CIS tuple" }
164};
165
166
167static const lookup_t service_table[] = {
168 { AccessConfigurationRegister, "AccessConfigurationRegister" },
169 { AddSocketServices, "AddSocketServices" },
170 { AdjustResourceInfo, "AdjustResourceInfo" },
171 { CheckEraseQueue, "CheckEraseQueue" },
172 { CloseMemory, "CloseMemory" },
173 { DeregisterClient, "DeregisterClient" },
174 { DeregisterEraseQueue, "DeregisterEraseQueue" },
175 { GetCardServicesInfo, "GetCardServicesInfo" },
176 { GetClientInfo, "GetClientInfo" },
177 { GetConfigurationInfo, "GetConfigurationInfo" },
178 { GetEventMask, "GetEventMask" },
179 { GetFirstClient, "GetFirstClient" },
180 { GetFirstRegion, "GetFirstRegion" },
181 { GetFirstTuple, "GetFirstTuple" },
182 { GetNextClient, "GetNextClient" },
183 { GetNextRegion, "GetNextRegion" },
184 { GetNextTuple, "GetNextTuple" },
185 { GetStatus, "GetStatus" },
186 { GetTupleData, "GetTupleData" },
187 { MapMemPage, "MapMemPage" },
188 { ModifyConfiguration, "ModifyConfiguration" },
189 { ModifyWindow, "ModifyWindow" },
190 { OpenMemory, "OpenMemory" },
191 { ParseTuple, "ParseTuple" },
192 { ReadMemory, "ReadMemory" },
193 { RegisterClient, "RegisterClient" },
194 { RegisterEraseQueue, "RegisterEraseQueue" },
195 { RegisterMTD, "RegisterMTD" },
196 { ReleaseConfiguration, "ReleaseConfiguration" },
197 { ReleaseIO, "ReleaseIO" },
198 { ReleaseIRQ, "ReleaseIRQ" },
199 { ReleaseWindow, "ReleaseWindow" },
200 { RequestConfiguration, "RequestConfiguration" },
201 { RequestIO, "RequestIO" },
202 { RequestIRQ, "RequestIRQ" },
203 { RequestSocketMask, "RequestSocketMask" },
204 { RequestWindow, "RequestWindow" },
205 { ResetCard, "ResetCard" },
206 { SetEventMask, "SetEventMask" },
207 { ValidateCIS, "ValidateCIS" },
208 { WriteMemory, "WriteMemory" },
209 { BindDevice, "BindDevice" },
210 { BindMTD, "BindMTD" },
211 { ReportError, "ReportError" },
212 { SuspendCard, "SuspendCard" },
213 { ResumeCard, "ResumeCard" },
214 { EjectCard, "EjectCard" },
215 { InsertCard, "InsertCard" },
216 { ReplaceCIS, "ReplaceCIS" }
217};
218
219
220int pcmcia_report_error(client_handle_t handle, error_info_t *err)
221{
222 int i;
223 char *serv;
224
225 if (CHECK_HANDLE(handle))
226 printk(KERN_NOTICE);
227 else {
228 struct pcmcia_device *p_dev = handle_to_pdev(handle);
229 printk(KERN_NOTICE "%s: ", p_dev->dev.bus_id);
230 }
231
232 for (i = 0; i < ARRAY_SIZE(service_table); i++)
233 if (service_table[i].key == err->func)
234 break;
235 if (i < ARRAY_SIZE(service_table))
236 serv = service_table[i].msg;
237 else
238 serv = "Unknown service number";
239
240 for (i = 0; i < ARRAY_SIZE(error_table); i++)
241 if (error_table[i].key == err->retcode)
242 break;
243 if (i < ARRAY_SIZE(error_table))
244 printk("%s: %s\n", serv, error_table[i].msg);
245 else
246 printk("%s: Unknown error code %#x\n", serv, err->retcode);
247
248 return CS_SUCCESS;
249} /* report_error */
250EXPORT_SYMBOL(pcmcia_report_error);
251
252/* end of code which was in cs.c before */
253
254/*======================================================================*/
255
256void cs_error(client_handle_t handle, int func, int ret)
257{
258 error_info_t err = { func, ret };
259 pcmcia_report_error(handle, &err);
260}
261EXPORT_SYMBOL(cs_error);
262
263/*======================================================================*/
264
265static struct pcmcia_driver * get_pcmcia_driver (dev_info_t *dev_info);
266static struct pcmcia_bus_socket * get_socket_info_by_nr(unsigned int nr);
267
268static void pcmcia_release_bus_socket(struct kref *refcount)
269{
270 struct pcmcia_bus_socket *s = container_of(refcount, struct pcmcia_bus_socket, refcount);
271 pcmcia_put_socket(s->parent);
272 kfree(s);
273}
274
275static void pcmcia_put_bus_socket(struct pcmcia_bus_socket *s)
276{
277 kref_put(&s->refcount, pcmcia_release_bus_socket);
278}
279
280static struct pcmcia_bus_socket *pcmcia_get_bus_socket(struct pcmcia_bus_socket *s)
281{
282 kref_get(&s->refcount);
283 return (s);
284}
285
286/**
287 * pcmcia_register_driver - register a PCMCIA driver with the bus core
288 *
289 * Registers a PCMCIA driver with the PCMCIA bus core.
290 */
291static int pcmcia_device_probe(struct device *dev);
292static int pcmcia_device_remove(struct device * dev);
293
294int pcmcia_register_driver(struct pcmcia_driver *driver)
295{
296 if (!driver)
297 return -EINVAL;
298
299 /* initialize common fields */
300 driver->drv.bus = &pcmcia_bus_type;
301 driver->drv.owner = driver->owner;
302 driver->drv.probe = pcmcia_device_probe;
303 driver->drv.remove = pcmcia_device_remove;
304
305 return driver_register(&driver->drv);
306}
307EXPORT_SYMBOL(pcmcia_register_driver);
308
309/**
310 * pcmcia_unregister_driver - unregister a PCMCIA driver with the bus core
311 */
312void pcmcia_unregister_driver(struct pcmcia_driver *driver)
313{
314 driver_unregister(&driver->drv);
315}
316EXPORT_SYMBOL(pcmcia_unregister_driver);
317
318#ifdef CONFIG_PROC_FS
319static struct proc_dir_entry *proc_pccard = NULL;
320
321static int proc_read_drivers_callback(struct device_driver *driver, void *d)
322{
323 char **p = d;
324 struct pcmcia_driver *p_drv = container_of(driver,
325 struct pcmcia_driver, drv);
326
327 *p += sprintf(*p, "%-24.24s 1 %d\n", p_drv->drv.name,
328#ifdef CONFIG_MODULE_UNLOAD
329 (p_drv->owner) ? module_refcount(p_drv->owner) : 1
330#else
331 1
332#endif
333 );
334 d = (void *) p;
335
336 return 0;
337}
338
339static int proc_read_drivers(char *buf, char **start, off_t pos,
340 int count, int *eof, void *data)
341{
342 char *p = buf;
343
344 bus_for_each_drv(&pcmcia_bus_type, NULL,
345 (void *) &p, proc_read_drivers_callback);
346
347 return (p - buf);
348}
349#endif
350
351/* pcmcia_device handling */
352
353static struct pcmcia_device * pcmcia_get_dev(struct pcmcia_device *p_dev)
354{
355 struct device *tmp_dev;
356 tmp_dev = get_device(&p_dev->dev);
357 if (!tmp_dev)
358 return NULL;
359 return to_pcmcia_dev(tmp_dev);
360}
361
362static void pcmcia_put_dev(struct pcmcia_device *p_dev)
363{
364 if (p_dev)
365 put_device(&p_dev->dev);
366}
367
368static void pcmcia_release_dev(struct device *dev)
369{
370 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
371 ds_dbg(1, "releasing dev %p\n", p_dev);
372 pcmcia_put_bus_socket(p_dev->socket->pcmcia);
373 kfree(p_dev);
374}
375
376
377static int pcmcia_device_probe(struct device * dev)
378{
379 struct pcmcia_device *p_dev;
380 struct pcmcia_driver *p_drv;
381 int ret = 0;
382
383 dev = get_device(dev);
384 if (!dev)
385 return -ENODEV;
386
387 p_dev = to_pcmcia_dev(dev);
388 p_drv = to_pcmcia_drv(dev->driver);
389
390 if (!try_module_get(p_drv->owner)) {
391 ret = -EINVAL;
392 goto put_dev;
393 }
394
395 if (p_drv->attach) {
396 p_dev->instance = p_drv->attach();
397 if ((!p_dev->instance) || (p_dev->client.state & CLIENT_UNBOUND)) {
398 printk(KERN_NOTICE "ds: unable to create instance "
399 "of '%s'!\n", p_drv->drv.name);
400 ret = -EINVAL;
401 }
402 }
403
404 if (ret)
405 module_put(p_drv->owner);
406 put_dev:
407 if ((ret) || !(p_drv->attach))
408 put_device(dev);
409 return (ret);
410}
411
412
413static int pcmcia_device_remove(struct device * dev)
414{
415 struct pcmcia_device *p_dev;
416 struct pcmcia_driver *p_drv;
417
418 /* detach the "instance" */
419 p_dev = to_pcmcia_dev(dev);
420 p_drv = to_pcmcia_drv(dev->driver);
421
422 if (p_drv) {
423 if ((p_drv->detach) && (p_dev->instance)) {
424 p_drv->detach(p_dev->instance);
425 /* from pcmcia_probe_device */
426 put_device(&p_dev->dev);
427 }
428 module_put(p_drv->owner);
429 }
430
431 return 0;
432}
433
434
435
436/*
437 * pcmcia_device_query -- determine information about a pcmcia device
438 */
439static int pcmcia_device_query(struct pcmcia_device *p_dev)
440{
441 cistpl_manfid_t manf_id;
442 cistpl_funcid_t func_id;
443 cistpl_vers_1_t vers1;
444 unsigned int i;
445
446 if (!pccard_read_tuple(p_dev->socket, p_dev->func,
447 CISTPL_MANFID, &manf_id)) {
448 p_dev->manf_id = manf_id.manf;
449 p_dev->card_id = manf_id.card;
450 p_dev->has_manf_id = 1;
451 p_dev->has_card_id = 1;
452 }
453
454 if (!pccard_read_tuple(p_dev->socket, p_dev->func,
455 CISTPL_FUNCID, &func_id)) {
456 p_dev->func_id = func_id.func;
457 p_dev->has_func_id = 1;
458 } else {
459 /* rule of thumb: cards with no FUNCID, but with
460 * common memory device geometry information, are
461 * probably memory cards (from pcmcia-cs) */
462 cistpl_device_geo_t devgeo;
463 if (!pccard_read_tuple(p_dev->socket, p_dev->func,
464 CISTPL_DEVICE_GEO, &devgeo)) {
465 ds_dbg(0, "mem device geometry probably means "
466 "FUNCID_MEMORY\n");
467 p_dev->func_id = CISTPL_FUNCID_MEMORY;
468 p_dev->has_func_id = 1;
469 }
470 }
471
472 if (!pccard_read_tuple(p_dev->socket, p_dev->func, CISTPL_VERS_1,
473 &vers1)) {
474 for (i=0; i < vers1.ns; i++) {
475 char *tmp;
476 unsigned int length;
477
478 tmp = vers1.str + vers1.ofs[i];
479
480 length = strlen(tmp) + 1;
481 if ((length < 3) || (length > 255))
482 continue;
483
484 p_dev->prod_id[i] = kmalloc(sizeof(char) * length,
485 GFP_KERNEL);
486 if (!p_dev->prod_id[i])
487 continue;
488
489 p_dev->prod_id[i] = strncpy(p_dev->prod_id[i],
490 tmp, length);
491 }
492 }
493
494 return 0;
495}
496
497
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.
500 *
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
503 * support it either.
504 */
505static DECLARE_MUTEX(device_add_lock);
506
507static struct pcmcia_device * pcmcia_device_add(struct pcmcia_bus_socket *s, unsigned int function)
508{
509 struct pcmcia_device *p_dev;
510 unsigned long flags;
511
512 s = pcmcia_get_bus_socket(s);
513 if (!s)
514 return NULL;
515
516 down(&device_add_lock);
517
1ad275e3
DB
518 /* max of 2 devices per card */
519 if (s->device_count == 2)
520 goto err_put;
521
1da177e4
LT
522 p_dev = kmalloc(sizeof(struct pcmcia_device), GFP_KERNEL);
523 if (!p_dev)
524 goto err_put;
525 memset(p_dev, 0, sizeof(struct pcmcia_device));
526
527 p_dev->socket = s->parent;
528 p_dev->device_no = (s->device_count++);
529 p_dev->func = function;
530
531 p_dev->dev.bus = &pcmcia_bus_type;
532 p_dev->dev.parent = s->parent->dev.dev;
533 p_dev->dev.release = pcmcia_release_dev;
534 sprintf (p_dev->dev.bus_id, "%d.%d", p_dev->socket->sock, p_dev->device_no);
535
536 /* compat */
537 p_dev->client.client_magic = CLIENT_MAGIC;
538 p_dev->client.Socket = s->parent;
539 p_dev->client.Function = function;
540 p_dev->client.state = CLIENT_UNBOUND;
541
542 /* Add to the list in pcmcia_bus_socket */
543 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
544 list_add_tail(&p_dev->socket_device_list, &s->devices_list);
545 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
546
1ad275e3
DB
547 pcmcia_device_query(p_dev);
548
1da177e4
LT
549 if (device_register(&p_dev->dev)) {
550 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
551 list_del(&p_dev->socket_device_list);
552 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
553
554 goto err_free;
555 }
556
557 up(&device_add_lock);
558
559 return p_dev;
560
561 err_free:
562 kfree(p_dev);
563 s->device_count--;
564 err_put:
565 up(&device_add_lock);
566 pcmcia_put_bus_socket(s);
567
568 return NULL;
569}
570
571
572static int pcmcia_card_add(struct pcmcia_socket *s)
573{
574 cisinfo_t cisinfo;
575 cistpl_longlink_mfc_t mfc;
576 unsigned int no_funcs, i;
577 int ret = 0;
578
579 if (!(s->resource_setup_done))
580 return -EAGAIN; /* try again, but later... */
581
582 pcmcia_validate_mem(s);
583 ret = pccard_validate_cis(s, BIND_FN_ALL, &cisinfo);
584 if (ret || !cisinfo.Chains) {
585 ds_dbg(0, "invalid CIS or invalid resources\n");
586 return -ENODEV;
587 }
588
589 if (!pccard_read_tuple(s, BIND_FN_ALL, CISTPL_LONGLINK_MFC, &mfc))
590 no_funcs = mfc.nfn;
591 else
592 no_funcs = 1;
593
594 /* this doesn't handle multifunction devices on one pcmcia function
595 * yet. */
596 for (i=0; i < no_funcs; i++)
597 pcmcia_device_add(s->pcmcia, i);
598
599 return (ret);
600}
601
602
1ad275e3
DB
603static void pcmcia_delayed_add_pseudo_device(void *data)
604{
605 struct pcmcia_bus_socket *s = data;
606 pcmcia_device_add(s, 0);
607 s->device_add_pending = 0;
608}
609
610static inline void pcmcia_add_pseudo_device(struct pcmcia_bus_socket *s)
611{
612 if (!s->device_add_pending) {
613 schedule_work(&s->device_add);
614 s->device_add_pending = 1;
615 }
616 return;
617}
618
619
620static inline int pcmcia_devmatch(struct pcmcia_device *dev,
621 struct pcmcia_device_id *did)
622{
623 if (did->match_flags & PCMCIA_DEV_ID_MATCH_MANF_ID) {
624 if ((!dev->has_manf_id) || (dev->manf_id != did->manf_id))
625 return 0;
626 }
627
628 if (did->match_flags & PCMCIA_DEV_ID_MATCH_CARD_ID) {
629 if ((!dev->has_card_id) || (dev->card_id != did->card_id))
630 return 0;
631 }
632
633 if (did->match_flags & PCMCIA_DEV_ID_MATCH_FUNCTION) {
634 if (dev->func != did->function)
635 return 0;
636 }
637
638 if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID1) {
639 if (!dev->prod_id[0])
640 return 0;
641 if (strcmp(did->prod_id[0], dev->prod_id[0]))
642 return 0;
643 }
644
645 if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID2) {
646 if (!dev->prod_id[1])
647 return 0;
648 if (strcmp(did->prod_id[1], dev->prod_id[1]))
649 return 0;
650 }
651
652 if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID3) {
653 if (!dev->prod_id[2])
654 return 0;
655 if (strcmp(did->prod_id[2], dev->prod_id[2]))
656 return 0;
657 }
658
659 if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID4) {
660 if (!dev->prod_id[3])
661 return 0;
662 if (strcmp(did->prod_id[3], dev->prod_id[3]))
663 return 0;
664 }
665
666 if (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO) {
667 /* handle pseudo multifunction devices:
668 * there are at most two pseudo multifunction devices.
669 * if we're matching against the first, schedule a
670 * call which will then check whether there are two
671 * pseudo devices, and if not, add the second one.
672 */
673 if (dev->device_no == 0)
674 pcmcia_add_pseudo_device(dev->socket->pcmcia);
675
676 if (dev->device_no != did->device_no)
677 return 0;
678 }
679
680 if (did->match_flags & PCMCIA_DEV_ID_MATCH_FUNC_ID) {
681 if ((!dev->has_func_id) || (dev->func_id != did->func_id))
682 return 0;
683
684 /* if this is a pseudo-multi-function device,
685 * we need explicit matches */
686 if (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO)
687 return 0;
688 if (dev->device_no)
689 return 0;
690
691 /* also, FUNC_ID matching needs to be activated by userspace
692 * after it has re-checked that there is no possible module
693 * with a prod_id/manf_id/card_id match.
694 */
695 if (!dev->allow_func_id_match)
696 return 0;
697 }
698
699 dev->dev.driver_data = (void *) did;
700
701 return 1;
702}
703
704
1da177e4
LT
705static int pcmcia_bus_match(struct device * dev, struct device_driver * drv) {
706 struct pcmcia_device * p_dev = to_pcmcia_dev(dev);
707 struct pcmcia_driver * p_drv = to_pcmcia_drv(drv);
1ad275e3 708 struct pcmcia_device_id *did = p_drv->id_table;
1da177e4
LT
709
710 /* matching by cardmgr */
711 if (p_dev->cardmgr == p_drv)
712 return 1;
713
1ad275e3
DB
714 while (did && did->match_flags) {
715 if (pcmcia_devmatch(p_dev, did))
716 return 1;
717 did++;
718 }
719
1da177e4
LT
720 return 0;
721}
722
840c2ac5
DB
723#ifdef CONFIG_HOTPLUG
724
725static int pcmcia_bus_hotplug(struct device *dev, char **envp, int num_envp,
726 char *buffer, int buffer_size)
727{
728 struct pcmcia_device *p_dev;
729 int i, length = 0;
730 u32 hash[4] = { 0, 0, 0, 0};
731
732 if (!dev)
733 return -ENODEV;
734
735 p_dev = to_pcmcia_dev(dev);
736
737 /* calculate hashes */
738 for (i=0; i<4; i++) {
739 if (!p_dev->prod_id[i])
740 continue;
741 hash[i] = crc32(0, p_dev->prod_id[i], strlen(p_dev->prod_id[i]));
742 }
743
744 i = 0;
745
746 if (add_hotplug_env_var(envp, num_envp, &i,
747 buffer, buffer_size, &length,
748 "SOCKET_NO=%u",
749 p_dev->socket->sock))
750 return -ENOMEM;
751
752 if (add_hotplug_env_var(envp, num_envp, &i,
753 buffer, buffer_size, &length,
754 "DEVICE_NO=%02X",
755 p_dev->device_no))
756 return -ENOMEM;
757
758 if (add_hotplug_env_var(envp, num_envp, &i,
759 buffer, buffer_size, &length,
760 "MODALIAS=pcmcia:m%04Xc%04Xf%02Xfn%02Xpfn%02X"
761 "pa%08Xpb%08Xpc%08Xpd%08X",
762 p_dev->has_manf_id ? p_dev->manf_id : 0,
763 p_dev->has_card_id ? p_dev->card_id : 0,
764 p_dev->has_func_id ? p_dev->func_id : 0,
765 p_dev->func,
766 p_dev->device_no,
767 hash[0],
768 hash[1],
769 hash[2],
770 hash[3]))
771 return -ENOMEM;
772
773 envp[i] = NULL;
774
775 return 0;
776}
777
778#else
779
780static int pcmcia_bus_hotplug(struct device *dev, char **envp, int num_envp,
781 char *buffer, int buffer_size)
782{
783 return -ENODEV;
784}
785
786#endif
787
1da177e4
LT
788/************************ per-device sysfs output ***************************/
789
790#define pcmcia_device_attr(field, test, format) \
e404e274 791static ssize_t field##_show (struct device *dev, struct device_attribute *attr, char *buf) \
1da177e4
LT
792{ \
793 struct pcmcia_device *p_dev = to_pcmcia_dev(dev); \
794 return p_dev->test ? sprintf (buf, format, p_dev->field) : -ENODEV; \
795}
796
797#define pcmcia_device_stringattr(name, field) \
e404e274 798static ssize_t name##_show (struct device *dev, struct device_attribute *attr, char *buf) \
1da177e4
LT
799{ \
800 struct pcmcia_device *p_dev = to_pcmcia_dev(dev); \
801 return p_dev->field ? sprintf (buf, "%s\n", p_dev->field) : -ENODEV; \
802}
803
804pcmcia_device_attr(func, socket, "0x%02x\n");
805pcmcia_device_attr(func_id, has_func_id, "0x%02x\n");
806pcmcia_device_attr(manf_id, has_manf_id, "0x%04x\n");
807pcmcia_device_attr(card_id, has_card_id, "0x%04x\n");
808pcmcia_device_stringattr(prod_id1, prod_id[0]);
809pcmcia_device_stringattr(prod_id2, prod_id[1]);
810pcmcia_device_stringattr(prod_id3, prod_id[2]);
811pcmcia_device_stringattr(prod_id4, prod_id[3]);
812
813static struct device_attribute pcmcia_dev_attrs[] = {
814 __ATTR(function, 0444, func_show, NULL),
815 __ATTR_RO(func_id),
816 __ATTR_RO(manf_id),
817 __ATTR_RO(card_id),
818 __ATTR_RO(prod_id1),
819 __ATTR_RO(prod_id2),
820 __ATTR_RO(prod_id3),
821 __ATTR_RO(prod_id4),
822 __ATTR_NULL,
823};
824
825
826/*======================================================================
827
828 These manage a ring buffer of events pending for one user process
829
830======================================================================*/
831
832static int queue_empty(user_info_t *user)
833{
834 return (user->event_head == user->event_tail);
835}
836
837static event_t get_queued_event(user_info_t *user)
838{
839 user->event_tail = (user->event_tail+1) % MAX_EVENTS;
840 return user->event[user->event_tail];
841}
842
843static void queue_event(user_info_t *user, event_t event)
844{
845 user->event_head = (user->event_head+1) % MAX_EVENTS;
846 if (user->event_head == user->event_tail)
847 user->event_tail = (user->event_tail+1) % MAX_EVENTS;
848 user->event[user->event_head] = event;
849}
850
851static void handle_event(struct pcmcia_bus_socket *s, event_t event)
852{
853 user_info_t *user;
854 for (user = s->user; user; user = user->next)
855 queue_event(user, event);
856 wake_up_interruptible(&s->queue);
857}
858
859
860/*======================================================================
861
862 The card status event handler.
863
864======================================================================*/
865
866struct send_event_data {
867 struct pcmcia_socket *skt;
868 event_t event;
869 int priority;
870};
871
872static int send_event_callback(struct device *dev, void * _data)
873{
874 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
875 struct send_event_data *data = _data;
876
877 /* we get called for all sockets, but may only pass the event
878 * for drivers _on the affected socket_ */
879 if (p_dev->socket != data->skt)
880 return 0;
881
882 if (p_dev->client.state & (CLIENT_UNBOUND|CLIENT_STALE))
883 return 0;
884
885 if (p_dev->client.EventMask & data->event)
886 return EVENT(&p_dev->client, data->event, data->priority);
887
888 return 0;
889}
890
891static int send_event(struct pcmcia_socket *s, event_t event, int priority)
892{
893 int ret = 0;
894 struct send_event_data private;
895 struct pcmcia_bus_socket *skt = pcmcia_get_bus_socket(s->pcmcia);
896
897 if (!skt)
898 return 0;
899
900 private.skt = s;
901 private.event = event;
902 private.priority = priority;
903
904 ret = bus_for_each_dev(&pcmcia_bus_type, NULL, &private, send_event_callback);
905
906 pcmcia_put_bus_socket(skt);
907 return ret;
908} /* send_event */
909
910
911/* Normally, the event is passed to individual drivers after
912 * informing userspace. Only for CS_EVENT_CARD_REMOVAL this
913 * is inversed to maintain historic compatibility.
914 */
915
916static int ds_event(struct pcmcia_socket *skt, event_t event, int priority)
917{
918 struct pcmcia_bus_socket *s = skt->pcmcia;
919 int ret = 0;
920
921 ds_dbg(1, "ds_event(0x%06x, %d, 0x%p)\n",
922 event, priority, s);
923
924 switch (event) {
925
926 case CS_EVENT_CARD_REMOVAL:
927 s->state &= ~DS_SOCKET_PRESENT;
928 send_event(skt, event, priority);
929 unbind_request(s);
930 handle_event(s, event);
931 break;
932
933 case CS_EVENT_CARD_INSERTION:
934 s->state |= DS_SOCKET_PRESENT;
935 pcmcia_card_add(skt);
936 handle_event(s, event);
937 break;
938
939 case CS_EVENT_EJECTION_REQUEST:
940 ret = send_event(skt, event, priority);
941 break;
942
943 default:
944 handle_event(s, event);
945 send_event(skt, event, priority);
946 break;
947 }
948
949 return 0;
950} /* ds_event */
951
952
953/*======================================================================
954
955 bind_request() and bind_device() are merged by now. Register_client()
956 is called right at the end of bind_request(), during the driver's
957 ->attach() call. Individual descriptions:
958
959 bind_request() connects a socket to a particular client driver.
960 It looks up the specified device ID in the list of registered
961 drivers, binds it to the socket, and tries to create an instance
962 of the device. unbind_request() deletes a driver instance.
963
964 Bind_device() associates a device driver with a particular socket.
965 It is normally called by Driver Services after it has identified
966 a newly inserted card. An instance of that driver will then be
967 eligible to register as a client of this socket.
968
969 Register_client() uses the dev_info_t handle to match the
970 caller with a socket. The driver must have already been bound
971 to a socket with bind_device() -- in fact, bind_device()
972 allocates the client structure that will be used.
973
974======================================================================*/
975
976static int bind_request(struct pcmcia_bus_socket *s, bind_info_t *bind_info)
977{
978 struct pcmcia_driver *p_drv;
979 struct pcmcia_device *p_dev;
980 int ret = 0;
981 unsigned long flags;
982
983 s = pcmcia_get_bus_socket(s);
984 if (!s)
985 return -EINVAL;
986
987 ds_dbg(2, "bind_request(%d, '%s')\n", s->parent->sock,
988 (char *)bind_info->dev_info);
989
990 p_drv = get_pcmcia_driver(&bind_info->dev_info);
991 if (!p_drv) {
992 ret = -EINVAL;
993 goto err_put;
994 }
995
996 if (!try_module_get(p_drv->owner)) {
997 ret = -EINVAL;
998 goto err_put_driver;
999 }
1000
1001 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
1002 list_for_each_entry(p_dev, &s->devices_list, socket_device_list) {
1003 if (p_dev->func == bind_info->function) {
1004 if ((p_dev->dev.driver == &p_drv->drv)) {
1005 if (p_dev->cardmgr) {
1006 /* if there's already a device
1007 * registered, and it was registered
1008 * by userspace before, we need to
1009 * return the "instance". */
1010 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
1011 bind_info->instance = p_dev->instance;
1012 ret = -EBUSY;
1013 goto err_put_module;
1014 } else {
1015 /* the correct driver managed to bind
1016 * itself magically to the correct
1017 * device. */
1018 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
1019 p_dev->cardmgr = p_drv;
1020 ret = 0;
1021 goto err_put_module;
1022 }
1023 } else if (!p_dev->dev.driver) {
1024 /* there's already a device available where
1025 * no device has been bound to yet. So we don't
1026 * need to register a device! */
1027 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
1028 goto rescan;
1029 }
1030 }
1031 }
1032 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
1033
1034 p_dev = pcmcia_device_add(s, bind_info->function);
1035 if (!p_dev) {
1036 ret = -EIO;
1037 goto err_put_module;
1038 }
1039
1040rescan:
1041 p_dev->cardmgr = p_drv;
1042
1ad275e3
DB
1043 /* if a driver is already running, we can abort */
1044 if (p_dev->dev.driver)
1045 goto err_put_module;
1da177e4
LT
1046
1047 /*
1048 * Prevent this racing with a card insertion.
1049 */
1050 down(&s->parent->skt_sem);
1051 bus_rescan_devices(&pcmcia_bus_type);
1052 up(&s->parent->skt_sem);
1053
1054 /* check whether the driver indeed matched. I don't care if this
1055 * is racy or not, because it can only happen on cardmgr access
1056 * paths...
1057 */
1058 if (!(p_dev->dev.driver == &p_drv->drv))
1059 p_dev->cardmgr = NULL;
1060
1061 err_put_module:
1062 module_put(p_drv->owner);
1063 err_put_driver:
1064 put_driver(&p_drv->drv);
1065 err_put:
1066 pcmcia_put_bus_socket(s);
1067
1068 return (ret);
1069} /* bind_request */
1070
1071
1072int pcmcia_register_client(client_handle_t *handle, client_reg_t *req)
1073{
1074 client_t *client = NULL;
1075 struct pcmcia_socket *s;
1076 struct pcmcia_bus_socket *skt = NULL;
1077 struct pcmcia_device *p_dev = NULL;
1078
1079 /* Look for unbound client with matching dev_info */
1080 down_read(&pcmcia_socket_list_rwsem);
1081 list_for_each_entry(s, &pcmcia_socket_list, socket_list) {
1082 unsigned long flags;
1083
1084 if (s->state & SOCKET_CARDBUS)
1085 continue;
1086
1087 skt = s->pcmcia;
1088 if (!skt)
1089 continue;
1090 skt = pcmcia_get_bus_socket(skt);
1091 if (!skt)
1092 continue;
1093 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
1094 list_for_each_entry(p_dev, &skt->devices_list, socket_device_list) {
1095 struct pcmcia_driver *p_drv;
1096 p_dev = pcmcia_get_dev(p_dev);
1097 if (!p_dev)
1098 continue;
1099 if (!(p_dev->client.state & CLIENT_UNBOUND) ||
1100 (!p_dev->dev.driver)) {
1101 pcmcia_put_dev(p_dev);
1102 continue;
1103 }
1104 p_drv = to_pcmcia_drv(p_dev->dev.driver);
1105 if (!strncmp(p_drv->drv.name, (char *)req->dev_info, DEV_NAME_LEN)) {
1106 client = &p_dev->client;
1107 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
1108 goto found;
1109 }
1110 pcmcia_put_dev(p_dev);
1111 }
1112 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
1113 pcmcia_put_bus_socket(skt);
1114 }
1115 found:
1116 up_read(&pcmcia_socket_list_rwsem);
1117 if (!p_dev || !client)
1118 return -ENODEV;
1119
1120 pcmcia_put_bus_socket(skt); /* safe, as we already hold a reference from bind_device */
1121
1122 *handle = client;
1123 client->state &= ~CLIENT_UNBOUND;
1124 client->Socket = s;
1125 client->EventMask = req->EventMask;
1126 client->event_handler = req->event_handler;
1127 client->event_callback_args = req->event_callback_args;
1128 client->event_callback_args.client_handle = client;
1129
1130 if (s->state & SOCKET_CARDBUS)
1131 client->state |= CLIENT_CARDBUS;
1132
1133 if ((!(s->state & SOCKET_CARDBUS)) && (s->functions == 0) &&
1134 (client->Function != BIND_FN_ALL)) {
1135 cistpl_longlink_mfc_t mfc;
1136 if (pccard_read_tuple(s, client->Function, CISTPL_LONGLINK_MFC, &mfc)
1137 == CS_SUCCESS)
1138 s->functions = mfc.nfn;
1139 else
1140 s->functions = 1;
1141 s->config = kmalloc(sizeof(config_t) * s->functions,
1142 GFP_KERNEL);
1143 if (!s->config)
1144 goto out_no_resource;
1145 memset(s->config, 0, sizeof(config_t) * s->functions);
1146 }
1147
1148 ds_dbg(1, "register_client(): client 0x%p, dev %s\n",
1149 client, p_dev->dev.bus_id);
1150 if (client->EventMask & CS_EVENT_REGISTRATION_COMPLETE)
1151 EVENT(client, CS_EVENT_REGISTRATION_COMPLETE, CS_EVENT_PRI_LOW);
1152
1153 if ((s->state & (SOCKET_PRESENT|SOCKET_CARDBUS)) == SOCKET_PRESENT) {
1154 if (client->EventMask & CS_EVENT_CARD_INSERTION)
1155 EVENT(client, CS_EVENT_CARD_INSERTION, CS_EVENT_PRI_LOW);
1156 }
1157
1158 return CS_SUCCESS;
1159
1160 out_no_resource:
1161 pcmcia_put_dev(p_dev);
1162 return CS_OUT_OF_RESOURCE;
1163} /* register_client */
1164EXPORT_SYMBOL(pcmcia_register_client);
1165
1166
1167/*====================================================================*/
1168
1169extern struct pci_bus *pcmcia_lookup_bus(struct pcmcia_socket *s);
1170
1171static int get_device_info(struct pcmcia_bus_socket *s, bind_info_t *bind_info, int first)
1172{
1173 dev_node_t *node;
1174 struct pcmcia_device *p_dev;
1175 unsigned long flags;
1176 int ret = 0;
1177
1178#ifdef CONFIG_CARDBUS
1179 /*
1180 * Some unbelievably ugly code to associate the PCI cardbus
1181 * device and its driver with the PCMCIA "bind" information.
1182 */
1183 {
1184 struct pci_bus *bus;
1185
1186 bus = pcmcia_lookup_bus(s->parent);
1187 if (bus) {
1188 struct list_head *list;
1189 struct pci_dev *dev = NULL;
1190
1191 list = bus->devices.next;
1192 while (list != &bus->devices) {
1193 struct pci_dev *pdev = pci_dev_b(list);
1194 list = list->next;
1195
1196 if (first) {
1197 dev = pdev;
1198 break;
1199 }
1200
1201 /* Try to handle "next" here some way? */
1202 }
1203 if (dev && dev->driver) {
1204 strlcpy(bind_info->name, dev->driver->name, DEV_NAME_LEN);
1205 bind_info->major = 0;
1206 bind_info->minor = 0;
1207 bind_info->next = NULL;
1208 return 0;
1209 }
1210 }
1211 }
1212#endif
1213
1214 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
1215 list_for_each_entry(p_dev, &s->devices_list, socket_device_list) {
1216 if (p_dev->func == bind_info->function) {
1217 p_dev = pcmcia_get_dev(p_dev);
1218 if (!p_dev)
1219 continue;
1220 goto found;
1221 }
1222 }
1223 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
1224 return -ENODEV;
1225
1226 found:
1227 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
1228
1229 if ((!p_dev->instance) ||
1230 (p_dev->instance->state & DEV_CONFIG_PENDING)) {
1231 ret = -EAGAIN;
1232 goto err_put;
1233 }
1234
1235 if (first)
1236 node = p_dev->instance->dev;
1237 else
1238 for (node = p_dev->instance->dev; node; node = node->next)
1239 if (node == bind_info->next)
1240 break;
1241 if (!node) {
1242 ret = -ENODEV;
1243 goto err_put;
1244 }
1245
1246 strlcpy(bind_info->name, node->dev_name, DEV_NAME_LEN);
1247 bind_info->major = node->major;
1248 bind_info->minor = node->minor;
1249 bind_info->next = node->next;
1250
1251 err_put:
1252 pcmcia_put_dev(p_dev);
1253 return (ret);
1254} /* get_device_info */
1255
1256/*====================================================================*/
1257
1258/* unbind _all_ devices attached to a given pcmcia_bus_socket. The
1259 * drivers have been called with EVENT_CARD_REMOVAL before.
1260 */
1261static int unbind_request(struct pcmcia_bus_socket *s)
1262{
1263 struct pcmcia_device *p_dev;
1264 unsigned long flags;
1265
1266 ds_dbg(2, "unbind_request(%d)\n", s->parent->sock);
1267
1268 s->device_count = 0;
1269
1270 for (;;) {
1271 /* unregister all pcmcia_devices registered with this socket*/
1272 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
1273 if (list_empty(&s->devices_list)) {
1274 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
1275 return 0;
1276 }
1277 p_dev = list_entry((&s->devices_list)->next, struct pcmcia_device, socket_device_list);
1278 list_del(&p_dev->socket_device_list);
1279 p_dev->client.state |= CLIENT_STALE;
1280 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
1281
1282 device_unregister(&p_dev->dev);
1283 }
1284
1285 return 0;
1286} /* unbind_request */
1287
1288int pcmcia_deregister_client(client_handle_t handle)
1289{
1290 struct pcmcia_socket *s;
1291 int i;
1292 struct pcmcia_device *p_dev = handle_to_pdev(handle);
1293
1294 if (CHECK_HANDLE(handle))
1295 return CS_BAD_HANDLE;
1296
1297 s = SOCKET(handle);
1298 ds_dbg(1, "deregister_client(%p)\n", handle);
1299
1300 if (handle->state & (CLIENT_IRQ_REQ|CLIENT_IO_REQ|CLIENT_CONFIG_LOCKED))
1301 goto warn_out;
1302 for (i = 0; i < MAX_WIN; i++)
1303 if (handle->state & CLIENT_WIN_REQ(i))
1304 goto warn_out;
1305
1306 if (handle->state & CLIENT_STALE) {
1307 handle->client_magic = 0;
1308 handle->state &= ~CLIENT_STALE;
1309 pcmcia_put_dev(p_dev);
1310 } else {
1311 handle->state = CLIENT_UNBOUND;
1312 handle->event_handler = NULL;
1313 }
1314
1315 return CS_SUCCESS;
1316 warn_out:
1317 printk(KERN_WARNING "ds: deregister_client was called too early.\n");
1318 return CS_IN_USE;
1319} /* deregister_client */
1320EXPORT_SYMBOL(pcmcia_deregister_client);
1321
1322
1323/*======================================================================
1324
1325 The user-mode PC Card device interface
1326
1327======================================================================*/
1328
1329static int ds_open(struct inode *inode, struct file *file)
1330{
1331 socket_t i = iminor(inode);
1332 struct pcmcia_bus_socket *s;
1333 user_info_t *user;
1334
1335 ds_dbg(0, "ds_open(socket %d)\n", i);
1336
1337 s = get_socket_info_by_nr(i);
1338 if (!s)
1339 return -ENODEV;
1340 s = pcmcia_get_bus_socket(s);
1341 if (!s)
1342 return -ENODEV;
1343
1344 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
1345 if (s->state & DS_SOCKET_BUSY) {
1346 pcmcia_put_bus_socket(s);
1347 return -EBUSY;
1348 }
1349 else
1350 s->state |= DS_SOCKET_BUSY;
1351 }
1352
1353 user = kmalloc(sizeof(user_info_t), GFP_KERNEL);
1354 if (!user) {
1355 pcmcia_put_bus_socket(s);
1356 return -ENOMEM;
1357 }
1358 user->event_tail = user->event_head = 0;
1359 user->next = s->user;
1360 user->user_magic = USER_MAGIC;
1361 user->socket = s;
1362 s->user = user;
1363 file->private_data = user;
1364
1365 if (s->state & DS_SOCKET_PRESENT)
1366 queue_event(user, CS_EVENT_CARD_INSERTION);
1367 return 0;
1368} /* ds_open */
1369
1370/*====================================================================*/
1371
1372static int ds_release(struct inode *inode, struct file *file)
1373{
1374 struct pcmcia_bus_socket *s;
1375 user_info_t *user, **link;
1376
1377 ds_dbg(0, "ds_release(socket %d)\n", iminor(inode));
1378
1379 user = file->private_data;
1380 if (CHECK_USER(user))
1381 goto out;
1382
1383 s = user->socket;
1384
1385 /* Unlink user data structure */
1386 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
1387 s->state &= ~DS_SOCKET_BUSY;
1388 }
1389 file->private_data = NULL;
1390 for (link = &s->user; *link; link = &(*link)->next)
1391 if (*link == user) break;
1392 if (link == NULL)
1393 goto out;
1394 *link = user->next;
1395 user->user_magic = 0;
1396 kfree(user);
1397 pcmcia_put_bus_socket(s);
1398out:
1399 return 0;
1400} /* ds_release */
1401
1402/*====================================================================*/
1403
1404static ssize_t ds_read(struct file *file, char __user *buf,
1405 size_t count, loff_t *ppos)
1406{
1407 struct pcmcia_bus_socket *s;
1408 user_info_t *user;
1409 int ret;
1410
1411 ds_dbg(2, "ds_read(socket %d)\n", iminor(file->f_dentry->d_inode));
1412
1413 if (count < 4)
1414 return -EINVAL;
1415
1416 user = file->private_data;
1417 if (CHECK_USER(user))
1418 return -EIO;
1419
1420 s = user->socket;
1421 if (s->state & DS_SOCKET_DEAD)
1422 return -EIO;
1423
1424 ret = wait_event_interruptible(s->queue, !queue_empty(user));
1425 if (ret == 0)
1426 ret = put_user(get_queued_event(user), (int __user *)buf) ? -EFAULT : 4;
1427
1428 return ret;
1429} /* ds_read */
1430
1431/*====================================================================*/
1432
1433static ssize_t ds_write(struct file *file, const char __user *buf,
1434 size_t count, loff_t *ppos)
1435{
1436 ds_dbg(2, "ds_write(socket %d)\n", iminor(file->f_dentry->d_inode));
1437
1438 if (count != 4)
1439 return -EINVAL;
1440 if ((file->f_flags & O_ACCMODE) == O_RDONLY)
1441 return -EBADF;
1442
1443 return -EIO;
1444} /* ds_write */
1445
1446/*====================================================================*/
1447
1448/* No kernel lock - fine */
1449static u_int ds_poll(struct file *file, poll_table *wait)
1450{
1451 struct pcmcia_bus_socket *s;
1452 user_info_t *user;
1453
1454 ds_dbg(2, "ds_poll(socket %d)\n", iminor(file->f_dentry->d_inode));
1455
1456 user = file->private_data;
1457 if (CHECK_USER(user))
1458 return POLLERR;
1459 s = user->socket;
1460 /*
1461 * We don't check for a dead socket here since that
1462 * will send cardmgr into an endless spin.
1463 */
1464 poll_wait(file, &s->queue, wait);
1465 if (!queue_empty(user))
1466 return POLLIN | POLLRDNORM;
1467 return 0;
1468} /* ds_poll */
1469
1470/*====================================================================*/
1471
1472extern int pcmcia_adjust_resource_info(adjust_t *adj);
1473
1474static int ds_ioctl(struct inode * inode, struct file * file,
1475 u_int cmd, u_long arg)
1476{
1477 struct pcmcia_bus_socket *s;
1478 void __user *uarg = (char __user *)arg;
1479 u_int size;
1480 int ret, err;
1481 ds_ioctl_arg_t *buf;
1482 user_info_t *user;
1483
1484 ds_dbg(2, "ds_ioctl(socket %d, %#x, %#lx)\n", iminor(inode), cmd, arg);
1485
1486 user = file->private_data;
1487 if (CHECK_USER(user))
1488 return -EIO;
1489
1490 s = user->socket;
1491 if (s->state & DS_SOCKET_DEAD)
1492 return -EIO;
1493
1494 size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
1495 if (size > sizeof(ds_ioctl_arg_t)) return -EINVAL;
1496
1497 /* Permission check */
1498 if (!(cmd & IOC_OUT) && !capable(CAP_SYS_ADMIN))
1499 return -EPERM;
1500
1501 if (cmd & IOC_IN) {
1502 if (!access_ok(VERIFY_READ, uarg, size)) {
1503 ds_dbg(3, "ds_ioctl(): verify_read = %d\n", -EFAULT);
1504 return -EFAULT;
1505 }
1506 }
1507 if (cmd & IOC_OUT) {
1508 if (!access_ok(VERIFY_WRITE, uarg, size)) {
1509 ds_dbg(3, "ds_ioctl(): verify_write = %d\n", -EFAULT);
1510 return -EFAULT;
1511 }
1512 }
1513 buf = kmalloc(sizeof(ds_ioctl_arg_t), GFP_KERNEL);
1514 if (!buf)
1515 return -ENOMEM;
1516
1517 err = ret = 0;
1518
1519 if (cmd & IOC_IN) __copy_from_user((char *)buf, uarg, size);
1520
1521 switch (cmd) {
1522 case DS_ADJUST_RESOURCE_INFO:
1523 ret = pcmcia_adjust_resource_info(&buf->adjust);
1524 break;
1525 case DS_GET_CARD_SERVICES_INFO:
1526 ret = pcmcia_get_card_services_info(&buf->servinfo);
1527 break;
1528 case DS_GET_CONFIGURATION_INFO:
1529 if (buf->config.Function &&
1530 (buf->config.Function >= s->parent->functions))
1531 ret = CS_BAD_ARGS;
1532 else
1533 ret = pccard_get_configuration_info(s->parent,
1534 buf->config.Function, &buf->config);
1535 break;
1536 case DS_GET_FIRST_TUPLE:
1537 down(&s->parent->skt_sem);
1538 pcmcia_validate_mem(s->parent);
1539 up(&s->parent->skt_sem);
1540 ret = pccard_get_first_tuple(s->parent, BIND_FN_ALL, &buf->tuple);
1541 break;
1542 case DS_GET_NEXT_TUPLE:
1543 ret = pccard_get_next_tuple(s->parent, BIND_FN_ALL, &buf->tuple);
1544 break;
1545 case DS_GET_TUPLE_DATA:
1546 buf->tuple.TupleData = buf->tuple_parse.data;
1547 buf->tuple.TupleDataMax = sizeof(buf->tuple_parse.data);
1548 ret = pccard_get_tuple_data(s->parent, &buf->tuple);
1549 break;
1550 case DS_PARSE_TUPLE:
1551 buf->tuple.TupleData = buf->tuple_parse.data;
1552 ret = pccard_parse_tuple(&buf->tuple, &buf->tuple_parse.parse);
1553 break;
1554 case DS_RESET_CARD:
1555 ret = pccard_reset_card(s->parent);
1556 break;
1557 case DS_GET_STATUS:
1558 if (buf->status.Function &&
1559 (buf->status.Function >= s->parent->functions))
1560 ret = CS_BAD_ARGS;
1561 else
1562 ret = pccard_get_status(s->parent, buf->status.Function, &buf->status);
1563 break;
1564 case DS_VALIDATE_CIS:
1565 down(&s->parent->skt_sem);
1566 pcmcia_validate_mem(s->parent);
1567 up(&s->parent->skt_sem);
1568 ret = pccard_validate_cis(s->parent, BIND_FN_ALL, &buf->cisinfo);
1569 break;
1570 case DS_SUSPEND_CARD:
1571 ret = pcmcia_suspend_card(s->parent);
1572 break;
1573 case DS_RESUME_CARD:
1574 ret = pcmcia_resume_card(s->parent);
1575 break;
1576 case DS_EJECT_CARD:
1577 err = pcmcia_eject_card(s->parent);
1578 break;
1579 case DS_INSERT_CARD:
1580 err = pcmcia_insert_card(s->parent);
1581 break;
1582 case DS_ACCESS_CONFIGURATION_REGISTER:
1583 if ((buf->conf_reg.Action == CS_WRITE) && !capable(CAP_SYS_ADMIN)) {
1584 err = -EPERM;
1585 goto free_out;
1586 }
1587 if (buf->conf_reg.Function &&
1588 (buf->conf_reg.Function >= s->parent->functions))
1589 ret = CS_BAD_ARGS;
1590 else
1591 ret = pccard_access_configuration_register(s->parent,
1592 buf->conf_reg.Function, &buf->conf_reg);
1593 break;
1594 case DS_GET_FIRST_REGION:
1595 case DS_GET_NEXT_REGION:
1596 case DS_BIND_MTD:
1597 if (!capable(CAP_SYS_ADMIN)) {
1598 err = -EPERM;
1599 goto free_out;
1600 } else {
1601 static int printed = 0;
1602 if (!printed) {
1603 printk(KERN_WARNING "2.6. kernels use pcmciamtd instead of memory_cs.c and do not require special\n");
1604 printk(KERN_WARNING "MTD handling any more.\n");
1605 printed++;
1606 }
1607 }
1608 err = -EINVAL;
1609 goto free_out;
1610 break;
1611 case DS_GET_FIRST_WINDOW:
1612 ret = pcmcia_get_window(s->parent, &buf->win_info.handle, 0,
1613 &buf->win_info.window);
1614 break;
1615 case DS_GET_NEXT_WINDOW:
1616 ret = pcmcia_get_window(s->parent, &buf->win_info.handle,
1617 buf->win_info.handle->index + 1, &buf->win_info.window);
1618 break;
1619 case DS_GET_MEM_PAGE:
1620 ret = pcmcia_get_mem_page(buf->win_info.handle,
1621 &buf->win_info.map);
1622 break;
1623 case DS_REPLACE_CIS:
1624 ret = pcmcia_replace_cis(s->parent, &buf->cisdump);
1625 break;
1626 case DS_BIND_REQUEST:
1627 if (!capable(CAP_SYS_ADMIN)) {
1628 err = -EPERM;
1629 goto free_out;
1630 }
1631 err = bind_request(s, &buf->bind_info);
1632 break;
1633 case DS_GET_DEVICE_INFO:
1634 err = get_device_info(s, &buf->bind_info, 1);
1635 break;
1636 case DS_GET_NEXT_DEVICE:
1637 err = get_device_info(s, &buf->bind_info, 0);
1638 break;
1639 case DS_UNBIND_REQUEST:
1640 err = 0;
1641 break;
1642 default:
1643 err = -EINVAL;
1644 }
1645
1646 if ((err == 0) && (ret != CS_SUCCESS)) {
1647 ds_dbg(2, "ds_ioctl: ret = %d\n", ret);
1648 switch (ret) {
1649 case CS_BAD_SOCKET: case CS_NO_CARD:
1650 err = -ENODEV; break;
1651 case CS_BAD_ARGS: case CS_BAD_ATTRIBUTE: case CS_BAD_IRQ:
1652 case CS_BAD_TUPLE:
1653 err = -EINVAL; break;
1654 case CS_IN_USE:
1655 err = -EBUSY; break;
1656 case CS_OUT_OF_RESOURCE:
1657 err = -ENOSPC; break;
1658 case CS_NO_MORE_ITEMS:
1659 err = -ENODATA; break;
1660 case CS_UNSUPPORTED_FUNCTION:
1661 err = -ENOSYS; break;
1662 default:
1663 err = -EIO; break;
1664 }
1665 }
1666
1667 if (cmd & IOC_OUT) {
1668 if (__copy_to_user(uarg, (char *)buf, size))
1669 err = -EFAULT;
1670 }
1671
1672free_out:
1673 kfree(buf);
1674 return err;
1675} /* ds_ioctl */
1676
1677/*====================================================================*/
1678
1679static struct file_operations ds_fops = {
1680 .owner = THIS_MODULE,
1681 .open = ds_open,
1682 .release = ds_release,
1683 .ioctl = ds_ioctl,
1684 .read = ds_read,
1685 .write = ds_write,
1686 .poll = ds_poll,
1687};
1688
1689static int __devinit pcmcia_bus_add_socket(struct class_device *class_dev)
1690{
1691 struct pcmcia_socket *socket = class_get_devdata(class_dev);
1692 struct pcmcia_bus_socket *s;
1693 int ret;
1694
1695 s = kmalloc(sizeof(struct pcmcia_bus_socket), GFP_KERNEL);
1696 if(!s)
1697 return -ENOMEM;
1698 memset(s, 0, sizeof(struct pcmcia_bus_socket));
1699
1700 /* get reference to parent socket */
1701 s->parent = pcmcia_get_socket(socket);
1702 if (!s->parent) {
1703 printk(KERN_ERR "PCMCIA obtaining reference to socket %p failed\n", socket);
1704 kfree (s);
1705 return -ENODEV;
1706 }
1707
1708 kref_init(&s->refcount);
1709
1710 /*
1711 * Ugly. But we want to wait for the socket threads to have started up.
1712 * We really should let the drivers themselves drive some of this..
1713 */
1714 msleep(250);
1715
1716 init_waitqueue_head(&s->queue);
1717 INIT_LIST_HEAD(&s->devices_list);
1ad275e3 1718 INIT_WORK(&s->device_add, pcmcia_delayed_add_pseudo_device, s);
1da177e4
LT
1719
1720 /* Set up hotline to Card Services */
1721 s->callback.owner = THIS_MODULE;
1722 s->callback.event = &ds_event;
1723 s->callback.resources_done = &pcmcia_card_add;
1724 socket->pcmcia = s;
1725
1726 ret = pccard_register_pcmcia(socket, &s->callback);
1727 if (ret) {
1728 printk(KERN_ERR "PCMCIA registration PCCard core failed for socket %p\n", socket);
1729 pcmcia_put_bus_socket(s);
1730 socket->pcmcia = NULL;
1731 return (ret);
1732 }
1733
1734 return 0;
1735}
1736
1737
1738static void pcmcia_bus_remove_socket(struct class_device *class_dev)
1739{
1740 struct pcmcia_socket *socket = class_get_devdata(class_dev);
1741
1742 if (!socket || !socket->pcmcia)
1743 return;
1744
1745 pccard_register_pcmcia(socket, NULL);
1746
1747 socket->pcmcia->state |= DS_SOCKET_DEAD;
1748 pcmcia_put_bus_socket(socket->pcmcia);
1749 socket->pcmcia = NULL;
1750
1751 return;
1752}
1753
1754
1755/* the pcmcia_bus_interface is used to handle pcmcia socket devices */
1756static struct class_interface pcmcia_bus_interface = {
1757 .class = &pcmcia_socket_class,
1758 .add = &pcmcia_bus_add_socket,
1759 .remove = &pcmcia_bus_remove_socket,
1760};
1761
1762
1763struct bus_type pcmcia_bus_type = {
1764 .name = "pcmcia",
840c2ac5 1765 .hotplug = pcmcia_bus_hotplug,
1da177e4
LT
1766 .match = pcmcia_bus_match,
1767 .dev_attrs = pcmcia_dev_attrs,
1768};
1769EXPORT_SYMBOL(pcmcia_bus_type);
1770
1771
1772static int __init init_pcmcia_bus(void)
1773{
1774 int i;
1775
1776 spin_lock_init(&pcmcia_dev_list_lock);
1777
1778 bus_register(&pcmcia_bus_type);
1779 class_interface_register(&pcmcia_bus_interface);
1780
1781 /* Set up character device for user mode clients */
1782 i = register_chrdev(0, "pcmcia", &ds_fops);
afbf510d 1783 if (i < 0)
1da177e4 1784 printk(KERN_NOTICE "unable to find a free device # for "
afbf510d 1785 "Driver Services (error=%d)\n", i);
1da177e4
LT
1786 else
1787 major_dev = i;
1788
1789#ifdef CONFIG_PROC_FS
1790 proc_pccard = proc_mkdir("pccard", proc_bus);
1791 if (proc_pccard)
1792 create_proc_read_entry("drivers",0,proc_pccard,proc_read_drivers,NULL);
1793#endif
1794
1795 return 0;
1796}
1797fs_initcall(init_pcmcia_bus); /* one level after subsys_initcall so that
1798 * pcmcia_socket_class is already registered */
1799
1800
1801static void __exit exit_pcmcia_bus(void)
1802{
1803 class_interface_unregister(&pcmcia_bus_interface);
1804
1805#ifdef CONFIG_PROC_FS
1806 if (proc_pccard) {
1807 remove_proc_entry("drivers", proc_pccard);
1808 remove_proc_entry("pccard", proc_bus);
1809 }
1810#endif
1811 if (major_dev != -1)
1812 unregister_chrdev(major_dev, "pcmcia");
1813
1814 bus_unregister(&pcmcia_bus_type);
1815}
1816module_exit(exit_pcmcia_bus);
1817
1818
1819
1820/* helpers for backwards-compatible functions */
1821
1822static struct pcmcia_bus_socket * get_socket_info_by_nr(unsigned int nr)
1823{
1824 struct pcmcia_socket * s = pcmcia_get_socket_by_nr(nr);
1825 if (s && s->pcmcia)
1826 return s->pcmcia;
1827 else
1828 return NULL;
1829}
1830
1831/* backwards-compatible accessing of driver --- by name! */
1832
1833static struct pcmcia_driver * get_pcmcia_driver (dev_info_t *dev_info)
1834{
1835 struct device_driver *drv;
1836 struct pcmcia_driver *p_drv;
1837
1838 drv = driver_find((char *) dev_info, &pcmcia_bus_type);
1839 if (!drv)
1840 return NULL;
1841
1842 p_drv = container_of(drv, struct pcmcia_driver, drv);
1843
1844 return (p_drv);
1845}
1846
1847MODULE_ALIAS("ds");