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