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