[PATCH] pcmcia: move event handler
[safe/jmp/linux-2.6] / drivers / pcmcia / ds.c
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 - 2005      Dominik Brodowski
14  */
15
16 #include <linux/config.h>
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/init.h>
20 #include <linux/errno.h>
21 #include <linux/list.h>
22 #include <linux/delay.h>
23 #include <linux/workqueue.h>
24 #include <linux/crc32.h>
25 #include <linux/firmware.h>
26
27 #define IN_CARD_SERVICES
28 #include <pcmcia/cs_types.h>
29 #include <pcmcia/cs.h>
30 #include <pcmcia/cistpl.h>
31 #include <pcmcia/ds.h>
32 #include <pcmcia/ss.h>
33
34 #include "cs_internal.h"
35 #include "ds_internal.h"
36
37 /*====================================================================*/
38
39 /* Module parameters */
40
41 MODULE_AUTHOR("David Hinds <dahinds@users.sourceforge.net>");
42 MODULE_DESCRIPTION("PCMCIA Driver Services");
43 MODULE_LICENSE("GPL");
44
45 #ifdef DEBUG
46 int ds_pc_debug;
47
48 module_param_named(pc_debug, ds_pc_debug, int, 0644);
49
50 #define ds_dbg(lvl, fmt, arg...) do {                           \
51         if (ds_pc_debug > (lvl))                                        \
52                 printk(KERN_DEBUG "ds: " fmt , ## arg);         \
53 } while (0)
54 #else
55 #define ds_dbg(lvl, fmt, arg...) do { } while (0)
56 #endif
57
58 spinlock_t pcmcia_dev_list_lock;
59
60 static int unbind_request(struct pcmcia_socket *s);
61
62 /*====================================================================*/
63
64 /* code which was in cs.c before */
65
66 /* String tables for error messages */
67
68 typedef struct lookup_t {
69     int key;
70     char *msg;
71 } lookup_t;
72
73 static const lookup_t error_table[] = {
74     { CS_SUCCESS,               "Operation succeeded" },
75     { CS_BAD_ADAPTER,           "Bad adapter" },
76     { CS_BAD_ATTRIBUTE,         "Bad attribute", },
77     { CS_BAD_BASE,              "Bad base address" },
78     { CS_BAD_EDC,               "Bad EDC" },
79     { CS_BAD_IRQ,               "Bad IRQ" },
80     { CS_BAD_OFFSET,            "Bad offset" },
81     { CS_BAD_PAGE,              "Bad page number" },
82     { CS_READ_FAILURE,          "Read failure" },
83     { CS_BAD_SIZE,              "Bad size" },
84     { CS_BAD_SOCKET,            "Bad socket" },
85     { CS_BAD_TYPE,              "Bad type" },
86     { CS_BAD_VCC,               "Bad Vcc" },
87     { CS_BAD_VPP,               "Bad Vpp" },
88     { CS_BAD_WINDOW,            "Bad window" },
89     { CS_WRITE_FAILURE,         "Write failure" },
90     { CS_NO_CARD,               "No card present" },
91     { CS_UNSUPPORTED_FUNCTION,  "Usupported function" },
92     { CS_UNSUPPORTED_MODE,      "Unsupported mode" },
93     { CS_BAD_SPEED,             "Bad speed" },
94     { CS_BUSY,                  "Resource busy" },
95     { CS_GENERAL_FAILURE,       "General failure" },
96     { CS_WRITE_PROTECTED,       "Write protected" },
97     { CS_BAD_ARG_LENGTH,        "Bad argument length" },
98     { CS_BAD_ARGS,              "Bad arguments" },
99     { CS_CONFIGURATION_LOCKED,  "Configuration locked" },
100     { CS_IN_USE,                "Resource in use" },
101     { CS_NO_MORE_ITEMS,         "No more items" },
102     { CS_OUT_OF_RESOURCE,       "Out of resource" },
103     { CS_BAD_HANDLE,            "Bad handle" },
104     { CS_BAD_TUPLE,             "Bad CIS tuple" }
105 };
106
107
108 static const lookup_t service_table[] = {
109     { AccessConfigurationRegister,      "AccessConfigurationRegister" },
110     { AddSocketServices,                "AddSocketServices" },
111     { AdjustResourceInfo,               "AdjustResourceInfo" },
112     { CheckEraseQueue,                  "CheckEraseQueue" },
113     { CloseMemory,                      "CloseMemory" },
114     { DeregisterClient,                 "DeregisterClient" },
115     { DeregisterEraseQueue,             "DeregisterEraseQueue" },
116     { GetCardServicesInfo,              "GetCardServicesInfo" },
117     { GetClientInfo,                    "GetClientInfo" },
118     { GetConfigurationInfo,             "GetConfigurationInfo" },
119     { GetEventMask,                     "GetEventMask" },
120     { GetFirstClient,                   "GetFirstClient" },
121     { GetFirstRegion,                   "GetFirstRegion" },
122     { GetFirstTuple,                    "GetFirstTuple" },
123     { GetNextClient,                    "GetNextClient" },
124     { GetNextRegion,                    "GetNextRegion" },
125     { GetNextTuple,                     "GetNextTuple" },
126     { GetStatus,                        "GetStatus" },
127     { GetTupleData,                     "GetTupleData" },
128     { MapMemPage,                       "MapMemPage" },
129     { ModifyConfiguration,              "ModifyConfiguration" },
130     { ModifyWindow,                     "ModifyWindow" },
131     { OpenMemory,                       "OpenMemory" },
132     { ParseTuple,                       "ParseTuple" },
133     { ReadMemory,                       "ReadMemory" },
134     { RegisterClient,                   "RegisterClient" },
135     { RegisterEraseQueue,               "RegisterEraseQueue" },
136     { RegisterMTD,                      "RegisterMTD" },
137     { ReleaseConfiguration,             "ReleaseConfiguration" },
138     { ReleaseIO,                        "ReleaseIO" },
139     { ReleaseIRQ,                       "ReleaseIRQ" },
140     { ReleaseWindow,                    "ReleaseWindow" },
141     { RequestConfiguration,             "RequestConfiguration" },
142     { RequestIO,                        "RequestIO" },
143     { RequestIRQ,                       "RequestIRQ" },
144     { RequestSocketMask,                "RequestSocketMask" },
145     { RequestWindow,                    "RequestWindow" },
146     { ResetCard,                        "ResetCard" },
147     { SetEventMask,                     "SetEventMask" },
148     { ValidateCIS,                      "ValidateCIS" },
149     { WriteMemory,                      "WriteMemory" },
150     { BindDevice,                       "BindDevice" },
151     { BindMTD,                          "BindMTD" },
152     { ReportError,                      "ReportError" },
153     { SuspendCard,                      "SuspendCard" },
154     { ResumeCard,                       "ResumeCard" },
155     { EjectCard,                        "EjectCard" },
156     { InsertCard,                       "InsertCard" },
157     { ReplaceCIS,                       "ReplaceCIS" }
158 };
159
160
161 static int pcmcia_report_error(client_handle_t handle, error_info_t *err)
162 {
163         int i;
164         char *serv;
165
166         if (CHECK_HANDLE(handle))
167                 printk(KERN_NOTICE);
168         else {
169                 struct pcmcia_device *p_dev = handle_to_pdev(handle);
170                 printk(KERN_NOTICE "%s: ", p_dev->dev.bus_id);
171         }
172
173         for (i = 0; i < ARRAY_SIZE(service_table); i++)
174                 if (service_table[i].key == err->func)
175                         break;
176         if (i < ARRAY_SIZE(service_table))
177                 serv = service_table[i].msg;
178         else
179                 serv = "Unknown service number";
180
181         for (i = 0; i < ARRAY_SIZE(error_table); i++)
182                 if (error_table[i].key == err->retcode)
183                         break;
184         if (i < ARRAY_SIZE(error_table))
185                 printk("%s: %s\n", serv, error_table[i].msg);
186         else
187                 printk("%s: Unknown error code %#x\n", serv, err->retcode);
188
189         return CS_SUCCESS;
190 } /* report_error */
191
192 /* end of code which was in cs.c before */
193
194 /*======================================================================*/
195
196 void cs_error(client_handle_t handle, int func, int ret)
197 {
198         error_info_t err = { func, ret };
199         pcmcia_report_error(handle, &err);
200 }
201 EXPORT_SYMBOL(cs_error);
202
203
204 static void pcmcia_check_driver(struct pcmcia_driver *p_drv)
205 {
206         struct pcmcia_device_id *did = p_drv->id_table;
207         unsigned int i;
208         u32 hash;
209
210         if (!p_drv->attach || !p_drv->event || !p_drv->detach)
211                 printk(KERN_DEBUG "pcmcia: %s does misses a callback function",
212                        p_drv->drv.name);
213
214         while (did && did->match_flags) {
215                 for (i=0; i<4; i++) {
216                         if (!did->prod_id[i])
217                                 continue;
218
219                         hash = crc32(0, did->prod_id[i], strlen(did->prod_id[i]));
220                         if (hash == did->prod_id_hash[i])
221                                 continue;
222
223                         printk(KERN_DEBUG "pcmcia: %s: invalid hash for "
224                                "product string \"%s\": is 0x%x, should "
225                                "be 0x%x\n", p_drv->drv.name, did->prod_id[i],
226                                did->prod_id_hash[i], hash);
227                         printk(KERN_DEBUG "pcmcia: see "
228                                 "Documentation/pcmcia/devicetable.txt for "
229                                 "details\n");
230                 }
231                 did++;
232         }
233
234         return;
235 }
236
237
238 #ifdef CONFIG_PCMCIA_LOAD_CIS
239
240 /**
241  * pcmcia_load_firmware - load CIS from userspace if device-provided is broken
242  * @dev - the pcmcia device which needs a CIS override
243  * @filename - requested filename in /lib/firmware/cis/
244  *
245  * This uses the in-kernel firmware loading mechanism to use a "fake CIS" if
246  * the one provided by the card is broken. The firmware files reside in
247  * /lib/firmware/cis/ in userspace.
248  */
249 static int pcmcia_load_firmware(struct pcmcia_device *dev, char * filename)
250 {
251         struct pcmcia_socket *s = dev->socket;
252         const struct firmware *fw;
253         char path[20];
254         int ret=-ENOMEM;
255         cisdump_t *cis;
256
257         if (!filename)
258                 return -EINVAL;
259
260         ds_dbg(1, "trying to load firmware %s\n", filename);
261
262         if (strlen(filename) > 14)
263                 return -EINVAL;
264
265         snprintf(path, 20, "%s", filename);
266
267         if (request_firmware(&fw, path, &dev->dev) == 0) {
268                 if (fw->size >= CISTPL_MAX_CIS_SIZE)
269                         goto release;
270
271                 cis = kmalloc(sizeof(cisdump_t), GFP_KERNEL);
272                 if (!cis)
273                         goto release;
274
275                 memset(cis, 0, sizeof(cisdump_t));
276
277                 cis->Length = fw->size + 1;
278                 memcpy(cis->Data, fw->data, fw->size);
279
280                 if (!pcmcia_replace_cis(s, cis))
281                         ret = 0;
282         }
283  release:
284         release_firmware(fw);
285
286         return (ret);
287 }
288
289 #else /* !CONFIG_PCMCIA_LOAD_CIS */
290
291 static inline int pcmcia_load_firmware(struct pcmcia_device *dev, char * filename)
292 {
293         return -ENODEV;
294 }
295
296 #endif
297
298
299 /*======================================================================*/
300
301
302 /**
303  * pcmcia_register_driver - register a PCMCIA driver with the bus core
304  *
305  * Registers a PCMCIA driver with the PCMCIA bus core.
306  */
307 static int pcmcia_device_probe(struct device *dev);
308 static int pcmcia_device_remove(struct device * dev);
309
310 int pcmcia_register_driver(struct pcmcia_driver *driver)
311 {
312         if (!driver)
313                 return -EINVAL;
314
315         pcmcia_check_driver(driver);
316
317         /* initialize common fields */
318         driver->drv.bus = &pcmcia_bus_type;
319         driver->drv.owner = driver->owner;
320         driver->drv.probe = pcmcia_device_probe;
321         driver->drv.remove = pcmcia_device_remove;
322
323         return driver_register(&driver->drv);
324 }
325 EXPORT_SYMBOL(pcmcia_register_driver);
326
327 /**
328  * pcmcia_unregister_driver - unregister a PCMCIA driver with the bus core
329  */
330 void pcmcia_unregister_driver(struct pcmcia_driver *driver)
331 {
332         driver_unregister(&driver->drv);
333 }
334 EXPORT_SYMBOL(pcmcia_unregister_driver);
335
336
337 /* pcmcia_device handling */
338
339 struct pcmcia_device * pcmcia_get_dev(struct pcmcia_device *p_dev)
340 {
341         struct device *tmp_dev;
342         tmp_dev = get_device(&p_dev->dev);
343         if (!tmp_dev)
344                 return NULL;
345         return to_pcmcia_dev(tmp_dev);
346 }
347
348 void pcmcia_put_dev(struct pcmcia_device *p_dev)
349 {
350         if (p_dev)
351                 put_device(&p_dev->dev);
352 }
353
354 static void pcmcia_release_dev(struct device *dev)
355 {
356         struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
357         ds_dbg(1, "releasing dev %p\n", p_dev);
358         pcmcia_put_socket(p_dev->socket);
359         kfree(p_dev);
360 }
361
362
363 static int pcmcia_device_probe(struct device * dev)
364 {
365         struct pcmcia_device *p_dev;
366         struct pcmcia_driver *p_drv;
367         int ret = 0;
368
369         dev = get_device(dev);
370         if (!dev)
371                 return -ENODEV;
372
373         p_dev = to_pcmcia_dev(dev);
374         p_drv = to_pcmcia_drv(dev->driver);
375
376         if (!try_module_get(p_drv->owner)) {
377                 ret = -EINVAL;
378                 goto put_dev;
379         }
380
381         if (p_drv->attach) {
382                 p_dev->instance = p_drv->attach();
383                 if ((!p_dev->instance) || (p_dev->client.state & CLIENT_UNBOUND)) {
384                         printk(KERN_NOTICE "ds: unable to create instance "
385                                "of '%s'!\n", p_drv->drv.name);
386                         ret = -EINVAL;
387                 }
388         }
389
390         if (ret)
391                 module_put(p_drv->owner);
392  put_dev:
393         if ((ret) || !(p_drv->attach))
394                 put_device(dev);
395         return (ret);
396 }
397
398
399 static int pcmcia_device_remove(struct device * dev)
400 {
401         struct pcmcia_device *p_dev;
402         struct pcmcia_driver *p_drv;
403
404         /* detach the "instance" */
405         p_dev = to_pcmcia_dev(dev);
406         p_drv = to_pcmcia_drv(dev->driver);
407
408         if (p_drv) {
409                 if ((p_drv->detach) && (p_dev->instance)) {
410                         p_drv->detach(p_dev->instance);
411                         /* from pcmcia_probe_device */
412                         put_device(&p_dev->dev);
413                 }
414                 module_put(p_drv->owner);
415         }
416
417         return 0;
418 }
419
420
421
422 /*
423  * pcmcia_device_query -- determine information about a pcmcia device
424  */
425 static int pcmcia_device_query(struct pcmcia_device *p_dev)
426 {
427         cistpl_manfid_t manf_id;
428         cistpl_funcid_t func_id;
429         cistpl_vers_1_t vers1;
430         unsigned int i;
431
432         if (!pccard_read_tuple(p_dev->socket, p_dev->func,
433                                CISTPL_MANFID, &manf_id)) {
434                 p_dev->manf_id = manf_id.manf;
435                 p_dev->card_id = manf_id.card;
436                 p_dev->has_manf_id = 1;
437                 p_dev->has_card_id = 1;
438         }
439
440         if (!pccard_read_tuple(p_dev->socket, p_dev->func,
441                                CISTPL_FUNCID, &func_id)) {
442                 p_dev->func_id = func_id.func;
443                 p_dev->has_func_id = 1;
444         } else {
445                 /* rule of thumb: cards with no FUNCID, but with
446                  * common memory device geometry information, are
447                  * probably memory cards (from pcmcia-cs) */
448                 cistpl_device_geo_t devgeo;
449                 if (!pccard_read_tuple(p_dev->socket, p_dev->func,
450                                       CISTPL_DEVICE_GEO, &devgeo)) {
451                         ds_dbg(0, "mem device geometry probably means "
452                                "FUNCID_MEMORY\n");
453                         p_dev->func_id = CISTPL_FUNCID_MEMORY;
454                         p_dev->has_func_id = 1;
455                 }
456         }
457
458         if (!pccard_read_tuple(p_dev->socket, p_dev->func, CISTPL_VERS_1,
459                                &vers1)) {
460                 for (i=0; i < vers1.ns; i++) {
461                         char *tmp;
462                         unsigned int length;
463
464                         tmp = vers1.str + vers1.ofs[i];
465
466                         length = strlen(tmp) + 1;
467                         if ((length < 3) || (length > 255))
468                                 continue;
469
470                         p_dev->prod_id[i] = kmalloc(sizeof(char) * length,
471                                                     GFP_KERNEL);
472                         if (!p_dev->prod_id[i])
473                                 continue;
474
475                         p_dev->prod_id[i] = strncpy(p_dev->prod_id[i],
476                                                     tmp, length);
477                 }
478         }
479
480         return 0;
481 }
482
483
484 /* device_add_lock is needed to avoid double registration by cardmgr and kernel.
485  * Serializes pcmcia_device_add; will most likely be removed in future.
486  *
487  * While it has the caveat that adding new PCMCIA devices inside(!) device_register()
488  * won't work, this doesn't matter much at the moment: the driver core doesn't
489  * support it either.
490  */
491 static DECLARE_MUTEX(device_add_lock);
492
493 struct pcmcia_device * pcmcia_device_add(struct pcmcia_socket *s, unsigned int function)
494 {
495         struct pcmcia_device *p_dev;
496         unsigned long flags;
497
498         s = pcmcia_get_socket(s);
499         if (!s)
500                 return NULL;
501
502         down(&device_add_lock);
503
504         /* max of 2 devices per card */
505         if (s->device_count == 2)
506                 goto err_put;
507
508         p_dev = kmalloc(sizeof(struct pcmcia_device), GFP_KERNEL);
509         if (!p_dev)
510                 goto err_put;
511         memset(p_dev, 0, sizeof(struct pcmcia_device));
512
513         p_dev->socket = s;
514         p_dev->device_no = (s->device_count++);
515         p_dev->func   = function;
516
517         p_dev->dev.bus = &pcmcia_bus_type;
518         p_dev->dev.parent = s->dev.dev;
519         p_dev->dev.release = pcmcia_release_dev;
520         sprintf (p_dev->dev.bus_id, "%d.%d", p_dev->socket->sock, p_dev->device_no);
521
522         /* compat */
523         p_dev->client.client_magic = CLIENT_MAGIC;
524         p_dev->client.Socket = s;
525         p_dev->client.Function = function;
526         p_dev->client.state = CLIENT_UNBOUND;
527
528         /* Add to the list in pcmcia_bus_socket */
529         spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
530         list_add_tail(&p_dev->socket_device_list, &s->devices_list);
531         spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
532
533         pcmcia_device_query(p_dev);
534
535         if (device_register(&p_dev->dev)) {
536                 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
537                 list_del(&p_dev->socket_device_list);
538                 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
539
540                 goto err_free;
541        }
542
543         up(&device_add_lock);
544
545         return p_dev;
546
547  err_free:
548         kfree(p_dev);
549         s->device_count--;
550  err_put:
551         up(&device_add_lock);
552         pcmcia_put_socket(s);
553
554         return NULL;
555 }
556
557
558 static int pcmcia_card_add(struct pcmcia_socket *s)
559 {
560         cisinfo_t cisinfo;
561         cistpl_longlink_mfc_t mfc;
562         unsigned int no_funcs, i;
563         int ret = 0;
564
565         if (!(s->resource_setup_done))
566                 return -EAGAIN; /* try again, but later... */
567
568         pcmcia_validate_mem(s);
569         ret = pccard_validate_cis(s, BIND_FN_ALL, &cisinfo);
570         if (ret || !cisinfo.Chains) {
571                 ds_dbg(0, "invalid CIS or invalid resources\n");
572                 return -ENODEV;
573         }
574
575         if (!pccard_read_tuple(s, BIND_FN_ALL, CISTPL_LONGLINK_MFC, &mfc))
576                 no_funcs = mfc.nfn;
577         else
578                 no_funcs = 1;
579
580         /* this doesn't handle multifunction devices on one pcmcia function
581          * yet. */
582         for (i=0; i < no_funcs; i++)
583                 pcmcia_device_add(s, i);
584
585         return (ret);
586 }
587
588
589 static void pcmcia_delayed_add_pseudo_device(void *data)
590 {
591         struct pcmcia_socket *s = data;
592         pcmcia_device_add(s, 0);
593         s->pcmcia_state.device_add_pending = 0;
594 }
595
596 static inline void pcmcia_add_pseudo_device(struct pcmcia_socket *s)
597 {
598         if (!s->pcmcia_state.device_add_pending) {
599                 schedule_work(&s->device_add);
600                 s->pcmcia_state.device_add_pending = 1;
601         }
602         return;
603 }
604
605 static int pcmcia_requery(struct device *dev, void * _data)
606 {
607         struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
608         if (!p_dev->dev.driver)
609                 pcmcia_device_query(p_dev);
610
611         return 0;
612 }
613
614 static void pcmcia_bus_rescan(struct pcmcia_socket *skt)
615 {
616         int no_devices=0;
617         unsigned long flags;
618
619         /* must be called with skt_sem held */
620         spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
621         if (list_empty(&skt->devices_list))
622                 no_devices=1;
623         spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
624
625         /* if no devices were added for this socket yet because of
626          * missing resource information or other trouble, we need to
627          * do this now. */
628         if (no_devices) {
629                 int ret = pcmcia_card_add(skt);
630                 if (ret)
631                         return;
632         }
633
634         /* some device information might have changed because of a CIS
635          * update or because we can finally read it correctly... so
636          * determine it again, overwriting old values if necessary. */
637         bus_for_each_dev(&pcmcia_bus_type, NULL, NULL, pcmcia_requery);
638
639         /* we re-scan all devices, not just the ones connected to this
640          * socket. This does not matter, though. */
641         bus_rescan_devices(&pcmcia_bus_type);
642 }
643
644 static inline int pcmcia_devmatch(struct pcmcia_device *dev,
645                                   struct pcmcia_device_id *did)
646 {
647         if (did->match_flags & PCMCIA_DEV_ID_MATCH_MANF_ID) {
648                 if ((!dev->has_manf_id) || (dev->manf_id != did->manf_id))
649                         return 0;
650         }
651
652         if (did->match_flags & PCMCIA_DEV_ID_MATCH_CARD_ID) {
653                 if ((!dev->has_card_id) || (dev->card_id != did->card_id))
654                         return 0;
655         }
656
657         if (did->match_flags & PCMCIA_DEV_ID_MATCH_FUNCTION) {
658                 if (dev->func != did->function)
659                         return 0;
660         }
661
662         if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID1) {
663                 if (!dev->prod_id[0])
664                         return 0;
665                 if (strcmp(did->prod_id[0], dev->prod_id[0]))
666                         return 0;
667         }
668
669         if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID2) {
670                 if (!dev->prod_id[1])
671                         return 0;
672                 if (strcmp(did->prod_id[1], dev->prod_id[1]))
673                         return 0;
674         }
675
676         if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID3) {
677                 if (!dev->prod_id[2])
678                         return 0;
679                 if (strcmp(did->prod_id[2], dev->prod_id[2]))
680                         return 0;
681         }
682
683         if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID4) {
684                 if (!dev->prod_id[3])
685                         return 0;
686                 if (strcmp(did->prod_id[3], dev->prod_id[3]))
687                         return 0;
688         }
689
690         if (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO) {
691                 /* handle pseudo multifunction devices:
692                  * there are at most two pseudo multifunction devices.
693                  * if we're matching against the first, schedule a
694                  * call which will then check whether there are two
695                  * pseudo devices, and if not, add the second one.
696                  */
697                 if (dev->device_no == 0)
698                         pcmcia_add_pseudo_device(dev->socket);
699
700                 if (dev->device_no != did->device_no)
701                         return 0;
702         }
703
704         if (did->match_flags & PCMCIA_DEV_ID_MATCH_FUNC_ID) {
705                 if ((!dev->has_func_id) || (dev->func_id != did->func_id))
706                         return 0;
707
708                 /* if this is a pseudo-multi-function device,
709                  * we need explicit matches */
710                 if (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO)
711                         return 0;
712                 if (dev->device_no)
713                         return 0;
714
715                 /* also, FUNC_ID matching needs to be activated by userspace
716                  * after it has re-checked that there is no possible module
717                  * with a prod_id/manf_id/card_id match.
718                  */
719                 if (!dev->allow_func_id_match)
720                         return 0;
721         }
722
723         if (did->match_flags & PCMCIA_DEV_ID_MATCH_FAKE_CIS) {
724                 if (!dev->socket->fake_cis)
725                         pcmcia_load_firmware(dev, did->cisfile);
726
727                 if (!dev->socket->fake_cis)
728                         return 0;
729         }
730
731         if (did->match_flags & PCMCIA_DEV_ID_MATCH_ANONYMOUS) {
732                 int i;
733                 for (i=0; i<4; i++)
734                         if (dev->prod_id[i])
735                                 return 0;
736                 if (dev->has_manf_id || dev->has_card_id || dev->has_func_id)
737                         return 0;
738         }
739
740         dev->dev.driver_data = (void *) did;
741
742         return 1;
743 }
744
745
746 static int pcmcia_bus_match(struct device * dev, struct device_driver * drv) {
747         struct pcmcia_device * p_dev = to_pcmcia_dev(dev);
748         struct pcmcia_driver * p_drv = to_pcmcia_drv(drv);
749         struct pcmcia_device_id *did = p_drv->id_table;
750
751         /* matching by cardmgr */
752         if (p_dev->cardmgr == p_drv)
753                 return 1;
754
755         while (did && did->match_flags) {
756                 if (pcmcia_devmatch(p_dev, did))
757                         return 1;
758                 did++;
759         }
760
761         return 0;
762 }
763
764 #ifdef CONFIG_HOTPLUG
765
766 static int pcmcia_bus_hotplug(struct device *dev, char **envp, int num_envp,
767                               char *buffer, int buffer_size)
768 {
769         struct pcmcia_device *p_dev;
770         int i, length = 0;
771         u32 hash[4] = { 0, 0, 0, 0};
772
773         if (!dev)
774                 return -ENODEV;
775
776         p_dev = to_pcmcia_dev(dev);
777
778         /* calculate hashes */
779         for (i=0; i<4; i++) {
780                 if (!p_dev->prod_id[i])
781                         continue;
782                 hash[i] = crc32(0, p_dev->prod_id[i], strlen(p_dev->prod_id[i]));
783         }
784
785         i = 0;
786
787         if (add_hotplug_env_var(envp, num_envp, &i,
788                                 buffer, buffer_size, &length,
789                                 "SOCKET_NO=%u",
790                                 p_dev->socket->sock))
791                 return -ENOMEM;
792
793         if (add_hotplug_env_var(envp, num_envp, &i,
794                                 buffer, buffer_size, &length,
795                                 "DEVICE_NO=%02X",
796                                 p_dev->device_no))
797                 return -ENOMEM;
798
799         if (add_hotplug_env_var(envp, num_envp, &i,
800                                 buffer, buffer_size, &length,
801                                 "MODALIAS=pcmcia:m%04Xc%04Xf%02Xfn%02Xpfn%02X"
802                                 "pa%08Xpb%08Xpc%08Xpd%08X",
803                                 p_dev->has_manf_id ? p_dev->manf_id : 0,
804                                 p_dev->has_card_id ? p_dev->card_id : 0,
805                                 p_dev->has_func_id ? p_dev->func_id : 0,
806                                 p_dev->func,
807                                 p_dev->device_no,
808                                 hash[0],
809                                 hash[1],
810                                 hash[2],
811                                 hash[3]))
812                 return -ENOMEM;
813
814         envp[i] = NULL;
815
816         return 0;
817 }
818
819 #else
820
821 static int pcmcia_bus_hotplug(struct device *dev, char **envp, int num_envp,
822                               char *buffer, int buffer_size)
823 {
824         return -ENODEV;
825 }
826
827 #endif
828
829 /************************ per-device sysfs output ***************************/
830
831 #define pcmcia_device_attr(field, test, format)                         \
832 static ssize_t field##_show (struct device *dev, struct device_attribute *attr, char *buf)              \
833 {                                                                       \
834         struct pcmcia_device *p_dev = to_pcmcia_dev(dev);               \
835         return p_dev->test ? sprintf (buf, format, p_dev->field) : -ENODEV; \
836 }
837
838 #define pcmcia_device_stringattr(name, field)                                   \
839 static ssize_t name##_show (struct device *dev, struct device_attribute *attr, char *buf)               \
840 {                                                                       \
841         struct pcmcia_device *p_dev = to_pcmcia_dev(dev);               \
842         return p_dev->field ? sprintf (buf, "%s\n", p_dev->field) : -ENODEV; \
843 }
844
845 pcmcia_device_attr(func, socket, "0x%02x\n");
846 pcmcia_device_attr(func_id, has_func_id, "0x%02x\n");
847 pcmcia_device_attr(manf_id, has_manf_id, "0x%04x\n");
848 pcmcia_device_attr(card_id, has_card_id, "0x%04x\n");
849 pcmcia_device_stringattr(prod_id1, prod_id[0]);
850 pcmcia_device_stringattr(prod_id2, prod_id[1]);
851 pcmcia_device_stringattr(prod_id3, prod_id[2]);
852 pcmcia_device_stringattr(prod_id4, prod_id[3]);
853
854 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
855 {
856         struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
857         int i;
858         u32 hash[4] = { 0, 0, 0, 0};
859
860         /* calculate hashes */
861         for (i=0; i<4; i++) {
862                 if (!p_dev->prod_id[i])
863                         continue;
864                 hash[i] = crc32(0,p_dev->prod_id[i],strlen(p_dev->prod_id[i]));
865         }
866         return sprintf(buf, "pcmcia:m%04Xc%04Xf%02Xfn%02Xpfn%02X"
867                                 "pa%08Xpb%08Xpc%08Xpd%08X\n",
868                                 p_dev->has_manf_id ? p_dev->manf_id : 0,
869                                 p_dev->has_card_id ? p_dev->card_id : 0,
870                                 p_dev->has_func_id ? p_dev->func_id : 0,
871                                 p_dev->func, p_dev->device_no,
872                                 hash[0], hash[1], hash[2], hash[3]);
873 }
874
875 static ssize_t pcmcia_store_allow_func_id_match(struct device *dev,
876                 struct device_attribute *attr, const char *buf, size_t count)
877 {
878         struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
879         if (!count)
880                 return -EINVAL;
881
882         down(&p_dev->socket->skt_sem);
883         p_dev->allow_func_id_match = 1;
884         up(&p_dev->socket->skt_sem);
885
886         bus_rescan_devices(&pcmcia_bus_type);
887
888         return count;
889 }
890
891 static struct device_attribute pcmcia_dev_attrs[] = {
892         __ATTR(function, 0444, func_show, NULL),
893         __ATTR_RO(func_id),
894         __ATTR_RO(manf_id),
895         __ATTR_RO(card_id),
896         __ATTR_RO(prod_id1),
897         __ATTR_RO(prod_id2),
898         __ATTR_RO(prod_id3),
899         __ATTR_RO(prod_id4),
900         __ATTR_RO(modalias),
901         __ATTR(allow_func_id_match, 0200, NULL, pcmcia_store_allow_func_id_match),
902         __ATTR_NULL,
903 };
904
905
906 /*======================================================================
907
908     The card status event handler.
909     
910 ======================================================================*/
911
912 struct send_event_data {
913         struct pcmcia_socket *skt;
914         event_t event;
915         int priority;
916 };
917
918 static int send_event_callback(struct device *dev, void * _data)
919 {
920         struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
921         struct pcmcia_driver *p_drv;
922         struct send_event_data *data = _data;
923
924         /* we get called for all sockets, but may only pass the event
925          * for drivers _on the affected socket_ */
926         if (p_dev->socket != data->skt)
927                 return 0;
928
929         p_drv = to_pcmcia_drv(p_dev->dev.driver);
930         if (!p_drv)
931                 return 0;
932
933         if (p_dev->client.state & (CLIENT_UNBOUND|CLIENT_STALE))
934                 return 0;
935
936         if (p_drv->event)
937                 return p_drv->event(data->event, data->priority,
938                                     &p_dev->event_callback_args);
939
940         return 0;
941 }
942
943 static int send_event(struct pcmcia_socket *s, event_t event, int priority)
944 {
945         struct send_event_data private;
946
947         private.skt = s;
948         private.event = event;
949         private.priority = priority;
950
951         return bus_for_each_dev(&pcmcia_bus_type, NULL, &private, send_event_callback);
952 } /* send_event */
953
954
955 /* Normally, the event is passed to individual drivers after
956  * informing userspace. Only for CS_EVENT_CARD_REMOVAL this
957  * is inversed to maintain historic compatibility.
958  */
959
960 static int ds_event(struct pcmcia_socket *skt, event_t event, int priority)
961 {
962         struct pcmcia_socket *s = pcmcia_get_socket(skt);
963         int ret = 0;
964
965         ds_dbg(1, "ds_event(0x%06x, %d, 0x%p)\n",
966                event, priority, skt);
967     
968         switch (event) {
969
970         case CS_EVENT_CARD_REMOVAL:
971                 s->pcmcia_state.present = 0;
972                 send_event(skt, event, priority);
973                 unbind_request(skt);
974                 handle_event(skt, event);
975                 break;
976         
977         case CS_EVENT_CARD_INSERTION:
978                 s->pcmcia_state.present = 1;
979                 pcmcia_card_add(skt);
980                 handle_event(skt, event);
981                 break;
982
983         case CS_EVENT_EJECTION_REQUEST:
984                 ret = send_event(skt, event, priority);
985                 break;
986
987         default:
988                 handle_event(skt, event);
989                 send_event(skt, event, priority);
990                 break;
991     }
992
993     pcmcia_put_socket(s);
994
995     return 0;
996 } /* ds_event */
997
998
999
1000 int pcmcia_register_client(client_handle_t *handle, client_reg_t *req)
1001 {
1002         client_t *client = NULL;
1003         struct pcmcia_socket *s = NULL;
1004         struct pcmcia_device *p_dev = NULL;
1005         struct pcmcia_driver *p_drv = NULL;
1006
1007         /* Look for unbound client with matching dev_info */
1008         down_read(&pcmcia_socket_list_rwsem);
1009         list_for_each_entry(s, &pcmcia_socket_list, socket_list) {
1010                 unsigned long flags;
1011
1012                 if (s->state & SOCKET_CARDBUS)
1013                         continue;
1014
1015                 s = pcmcia_get_socket(s);
1016                 if (!s)
1017                         continue;
1018                 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
1019                 list_for_each_entry(p_dev, &s->devices_list, socket_device_list) {
1020                         p_dev = pcmcia_get_dev(p_dev);
1021                         if (!p_dev)
1022                                 continue;
1023                         if (!(p_dev->client.state & CLIENT_UNBOUND) ||
1024                             (!p_dev->dev.driver)) {
1025                                 pcmcia_put_dev(p_dev);
1026                                 continue;
1027                         }
1028                         p_drv = to_pcmcia_drv(p_dev->dev.driver);
1029                         if (!strncmp(p_drv->drv.name, (char *)req->dev_info, DEV_NAME_LEN)) {
1030                                 client = &p_dev->client;
1031                                 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
1032                                 goto found;
1033                         }
1034                         pcmcia_put_dev(p_dev);
1035                 }
1036                 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
1037                 pcmcia_put_socket(s);
1038         }
1039  found:
1040         up_read(&pcmcia_socket_list_rwsem);
1041         if (!p_dev || !client)
1042                 return -ENODEV;
1043
1044         pcmcia_put_socket(s); /* safe, as we already hold a reference from bind_device */
1045
1046         *handle = client;
1047         client->state &= ~CLIENT_UNBOUND;
1048         client->Socket = s;
1049         p_dev->event_callback_args = req->event_callback_args;
1050         p_dev->event_callback_args.client_handle = client;
1051
1052
1053         if (s->state & SOCKET_CARDBUS)
1054                 client->state |= CLIENT_CARDBUS;
1055
1056         if ((!(s->state & SOCKET_CARDBUS)) && (s->functions == 0) &&
1057             (client->Function != BIND_FN_ALL)) {
1058                 cistpl_longlink_mfc_t mfc;
1059                 if (pccard_read_tuple(s, client->Function, CISTPL_LONGLINK_MFC, &mfc)
1060                     == CS_SUCCESS)
1061                         s->functions = mfc.nfn;
1062                 else
1063                         s->functions = 1;
1064                 s->config = kmalloc(sizeof(config_t) * s->functions,
1065                                     GFP_KERNEL);
1066                 if (!s->config)
1067                         goto out_no_resource;
1068                 memset(s->config, 0, sizeof(config_t) * s->functions);
1069         }
1070
1071         ds_dbg(1, "register_client(): client 0x%p, dev %s\n",
1072                client, p_dev->dev.bus_id);
1073
1074         if ((s->state & (SOCKET_PRESENT|SOCKET_CARDBUS)) == SOCKET_PRESENT) {
1075                 if (p_drv->event)
1076                         p_drv->event(CS_EVENT_CARD_INSERTION, CS_EVENT_PRI_LOW,
1077                                      &p_dev->event_callback_args);
1078
1079         }
1080
1081         return CS_SUCCESS;
1082
1083  out_no_resource:
1084         pcmcia_put_dev(p_dev);
1085         return CS_OUT_OF_RESOURCE;
1086 } /* register_client */
1087 EXPORT_SYMBOL(pcmcia_register_client);
1088
1089
1090 /* unbind _all_ devices attached to a given pcmcia_bus_socket. The
1091  * drivers have been called with EVENT_CARD_REMOVAL before.
1092  */
1093 static int unbind_request(struct pcmcia_socket *s)
1094 {
1095         struct pcmcia_device    *p_dev;
1096         unsigned long           flags;
1097
1098         ds_dbg(2, "unbind_request(%d)\n", s->sock);
1099
1100         s->device_count = 0;
1101
1102         for (;;) {
1103                 /* unregister all pcmcia_devices registered with this socket*/
1104                 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
1105                 if (list_empty(&s->devices_list)) {
1106                         spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
1107                         return 0;
1108                 }
1109                 p_dev = list_entry((&s->devices_list)->next, struct pcmcia_device, socket_device_list);
1110                 list_del(&p_dev->socket_device_list);
1111                 p_dev->client.state |= CLIENT_STALE;
1112                 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
1113
1114                 device_unregister(&p_dev->dev);
1115         }
1116
1117         return 0;
1118 } /* unbind_request */
1119
1120 int pcmcia_deregister_client(client_handle_t handle)
1121 {
1122         struct pcmcia_socket *s;
1123         int i;
1124         struct pcmcia_device *p_dev = handle_to_pdev(handle);
1125
1126         if (CHECK_HANDLE(handle))
1127                 return CS_BAD_HANDLE;
1128
1129         s = SOCKET(handle);
1130         ds_dbg(1, "deregister_client(%p)\n", handle);
1131
1132         if (handle->state & (CLIENT_IRQ_REQ|CLIENT_IO_REQ|CLIENT_CONFIG_LOCKED))
1133                 goto warn_out;
1134         for (i = 0; i < MAX_WIN; i++)
1135                 if (handle->state & CLIENT_WIN_REQ(i))
1136                         goto warn_out;
1137
1138         if (handle->state & CLIENT_STALE) {
1139                 handle->client_magic = 0;
1140                 handle->state &= ~CLIENT_STALE;
1141                 pcmcia_put_dev(p_dev);
1142         } else {
1143                 handle->state = CLIENT_UNBOUND;
1144         }
1145
1146         return CS_SUCCESS;
1147  warn_out:
1148         printk(KERN_WARNING "ds: deregister_client was called too early.\n");
1149         return CS_IN_USE;
1150 } /* deregister_client */
1151 EXPORT_SYMBOL(pcmcia_deregister_client);
1152
1153 static struct pcmcia_callback pcmcia_bus_callback = {
1154         .owner = THIS_MODULE,
1155         .event = ds_event,
1156         .requery = pcmcia_bus_rescan,
1157 };
1158
1159 static int __devinit pcmcia_bus_add_socket(struct class_device *class_dev)
1160 {
1161         struct pcmcia_socket *socket = class_get_devdata(class_dev);
1162         int ret;
1163
1164         socket = pcmcia_get_socket(socket);
1165         if (!socket) {
1166                 printk(KERN_ERR "PCMCIA obtaining reference to socket %p failed\n", socket);
1167                 return -ENODEV;
1168         }
1169
1170         /*
1171          * Ugly. But we want to wait for the socket threads to have started up.
1172          * We really should let the drivers themselves drive some of this..
1173          */
1174         msleep(250);
1175
1176 #ifdef CONFIG_PCMCIA_IOCTL
1177         init_waitqueue_head(&socket->queue);
1178 #endif
1179         INIT_LIST_HEAD(&socket->devices_list);
1180         INIT_WORK(&socket->device_add, pcmcia_delayed_add_pseudo_device, socket);
1181         memset(&socket->pcmcia_state, 0, sizeof(u8));
1182         socket->device_count = 0;
1183
1184         ret = pccard_register_pcmcia(socket, &pcmcia_bus_callback);
1185         if (ret) {
1186                 printk(KERN_ERR "PCMCIA registration PCCard core failed for socket %p\n", socket);
1187                 pcmcia_put_socket(socket);
1188                 return (ret);
1189         }
1190
1191         return 0;
1192 }
1193
1194 static void pcmcia_bus_remove_socket(struct class_device *class_dev)
1195 {
1196         struct pcmcia_socket *socket = class_get_devdata(class_dev);
1197
1198         if (!socket)
1199                 return;
1200
1201         socket->pcmcia_state.dead = 1;
1202         pccard_register_pcmcia(socket, NULL);
1203
1204         pcmcia_put_socket(socket);
1205
1206         return;
1207 }
1208
1209
1210 /* the pcmcia_bus_interface is used to handle pcmcia socket devices */
1211 static struct class_interface pcmcia_bus_interface = {
1212         .class = &pcmcia_socket_class,
1213         .add = &pcmcia_bus_add_socket,
1214         .remove = &pcmcia_bus_remove_socket,
1215 };
1216
1217
1218 struct bus_type pcmcia_bus_type = {
1219         .name = "pcmcia",
1220         .hotplug = pcmcia_bus_hotplug,
1221         .match = pcmcia_bus_match,
1222         .dev_attrs = pcmcia_dev_attrs,
1223 };
1224
1225
1226 static int __init init_pcmcia_bus(void)
1227 {
1228         spin_lock_init(&pcmcia_dev_list_lock);
1229
1230         bus_register(&pcmcia_bus_type);
1231         class_interface_register(&pcmcia_bus_interface);
1232
1233         pcmcia_setup_ioctl();
1234
1235         return 0;
1236 }
1237 fs_initcall(init_pcmcia_bus); /* one level after subsys_initcall so that 
1238                                * pcmcia_socket_class is already registered */
1239
1240
1241 static void __exit exit_pcmcia_bus(void)
1242 {
1243         pcmcia_cleanup_ioctl();
1244
1245         class_interface_unregister(&pcmcia_bus_interface);
1246
1247         bus_unregister(&pcmcia_bus_type);
1248 }
1249 module_exit(exit_pcmcia_bus);
1250
1251
1252 MODULE_ALIAS("ds");