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