3ccf4091e8775bd6e568dfcdf0afa43759acd3d3
[linux-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 - 2006      Dominik Brodowski
14  */
15
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/errno.h>
20 #include <linux/list.h>
21 #include <linux/delay.h>
22 #include <linux/workqueue.h>
23 #include <linux/crc32.h>
24 #include <linux/firmware.h>
25 #include <linux/kref.h>
26 #include <linux/dma-mapping.h>
27
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 CONFIG_PCMCIA_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 #define ds_dev_dbg(lvl, dev, fmt, arg...) do {                          \
55         if (ds_pc_debug > (lvl))                                        \
56                 dev_printk(KERN_DEBUG, dev, "ds: " fmt , ## arg);       \
57 } while (0)
58 #else
59 #define ds_dbg(lvl, fmt, arg...) do { } while (0)
60 #define ds_dev_dbg(lvl, dev, fmt, arg...) do { } while (0)
61 #endif
62
63 spinlock_t pcmcia_dev_list_lock;
64
65 /*====================================================================*/
66
67 /* code which was in cs.c before */
68
69 /* String tables for error messages */
70
71 typedef struct lookup_t {
72     int key;
73     char *msg;
74 } lookup_t;
75
76 static const lookup_t error_table[] = {
77     { 0,                        "Operation succeeded" },
78     { -EIO,                     "Input/Output error" },
79     { -ENODEV,                  "No card present" },
80     { -EINVAL,                  "Bad parameter" },
81     { CS_BAD_ARGS,              "Bad arguments" },
82     { -EACCES,                  "Configuration locked" },
83     { -EBUSY,                   "Resource in use" },
84     { -ENOSPC,                  "No more items" },
85     { -ENOMEM,                  "Out of resource" },
86     { CS_BAD_TUPLE,             "Bad CIS tuple" }
87 };
88
89
90 static const lookup_t service_table[] = {
91     { AccessConfigurationRegister,      "AccessConfigurationRegister" },
92     { AddSocketServices,                "AddSocketServices" },
93     { AdjustResourceInfo,               "AdjustResourceInfo" },
94     { CheckEraseQueue,                  "CheckEraseQueue" },
95     { CloseMemory,                      "CloseMemory" },
96     { DeregisterClient,                 "DeregisterClient" },
97     { DeregisterEraseQueue,             "DeregisterEraseQueue" },
98     { GetCardServicesInfo,              "GetCardServicesInfo" },
99     { GetClientInfo,                    "GetClientInfo" },
100     { GetConfigurationInfo,             "GetConfigurationInfo" },
101     { GetEventMask,                     "GetEventMask" },
102     { GetFirstClient,                   "GetFirstClient" },
103     { GetFirstRegion,                   "GetFirstRegion" },
104     { GetFirstTuple,                    "GetFirstTuple" },
105     { GetNextClient,                    "GetNextClient" },
106     { GetNextRegion,                    "GetNextRegion" },
107     { GetNextTuple,                     "GetNextTuple" },
108     { GetStatus,                        "GetStatus" },
109     { GetTupleData,                     "GetTupleData" },
110     { MapMemPage,                       "MapMemPage" },
111     { ModifyConfiguration,              "ModifyConfiguration" },
112     { ModifyWindow,                     "ModifyWindow" },
113     { OpenMemory,                       "OpenMemory" },
114     { ParseTuple,                       "ParseTuple" },
115     { ReadMemory,                       "ReadMemory" },
116     { RegisterClient,                   "RegisterClient" },
117     { RegisterEraseQueue,               "RegisterEraseQueue" },
118     { RegisterMTD,                      "RegisterMTD" },
119     { ReleaseConfiguration,             "ReleaseConfiguration" },
120     { ReleaseIO,                        "ReleaseIO" },
121     { ReleaseIRQ,                       "ReleaseIRQ" },
122     { ReleaseWindow,                    "ReleaseWindow" },
123     { RequestConfiguration,             "RequestConfiguration" },
124     { RequestIO,                        "RequestIO" },
125     { RequestIRQ,                       "RequestIRQ" },
126     { RequestSocketMask,                "RequestSocketMask" },
127     { RequestWindow,                    "RequestWindow" },
128     { ResetCard,                        "ResetCard" },
129     { SetEventMask,                     "SetEventMask" },
130     { ValidateCIS,                      "ValidateCIS" },
131     { WriteMemory,                      "WriteMemory" },
132     { BindDevice,                       "BindDevice" },
133     { BindMTD,                          "BindMTD" },
134     { ReportError,                      "ReportError" },
135     { SuspendCard,                      "SuspendCard" },
136     { ResumeCard,                       "ResumeCard" },
137     { EjectCard,                        "EjectCard" },
138     { InsertCard,                       "InsertCard" },
139     { ReplaceCIS,                       "ReplaceCIS" }
140 };
141
142
143 static int pcmcia_report_error(struct pcmcia_device *p_dev, error_info_t *err)
144 {
145         int i;
146         char *serv;
147
148         if (!p_dev)
149                 printk(KERN_NOTICE);
150         else
151                 printk(KERN_NOTICE "%s: ", p_dev->dev.bus_id);
152
153         for (i = 0; i < ARRAY_SIZE(service_table); i++)
154                 if (service_table[i].key == err->func)
155                         break;
156         if (i < ARRAY_SIZE(service_table))
157                 serv = service_table[i].msg;
158         else
159                 serv = "Unknown service number";
160
161         for (i = 0; i < ARRAY_SIZE(error_table); i++)
162                 if (error_table[i].key == err->retcode)
163                         break;
164         if (i < ARRAY_SIZE(error_table))
165                 printk("%s: %s\n", serv, error_table[i].msg);
166         else
167                 printk("%s: Unknown error code %#x\n", serv, err->retcode);
168
169         return 0;
170 } /* report_error */
171
172 /* end of code which was in cs.c before */
173
174 /*======================================================================*/
175
176 void cs_error(struct pcmcia_device *p_dev, int func, int ret)
177 {
178         error_info_t err = { func, ret };
179         pcmcia_report_error(p_dev, &err);
180 }
181 EXPORT_SYMBOL(cs_error);
182
183
184 static void pcmcia_check_driver(struct pcmcia_driver *p_drv)
185 {
186         struct pcmcia_device_id *did = p_drv->id_table;
187         unsigned int i;
188         u32 hash;
189
190         if (!p_drv->probe || !p_drv->remove)
191                 printk(KERN_DEBUG "pcmcia: %s lacks a requisite callback "
192                        "function\n", p_drv->drv.name);
193
194         while (did && did->match_flags) {
195                 for (i=0; i<4; i++) {
196                         if (!did->prod_id[i])
197                                 continue;
198
199                         hash = crc32(0, did->prod_id[i], strlen(did->prod_id[i]));
200                         if (hash == did->prod_id_hash[i])
201                                 continue;
202
203                         printk(KERN_DEBUG "pcmcia: %s: invalid hash for "
204                                "product string \"%s\": is 0x%x, should "
205                                "be 0x%x\n", p_drv->drv.name, did->prod_id[i],
206                                did->prod_id_hash[i], hash);
207                         printk(KERN_DEBUG "pcmcia: see "
208                                 "Documentation/pcmcia/devicetable.txt for "
209                                 "details\n");
210                 }
211                 did++;
212         }
213
214         return;
215 }
216
217
218 /*======================================================================*/
219
220
221 struct pcmcia_dynid {
222         struct list_head                node;
223         struct pcmcia_device_id         id;
224 };
225
226 /**
227  * pcmcia_store_new_id - add a new PCMCIA device ID to this driver and re-probe devices
228  * @driver: target device driver
229  * @buf: buffer for scanning device ID data
230  * @count: input size
231  *
232  * Adds a new dynamic PCMCIA device ID to this driver,
233  * and causes the driver to probe for all devices again.
234  */
235 static ssize_t
236 pcmcia_store_new_id(struct device_driver *driver, const char *buf, size_t count)
237 {
238         struct pcmcia_dynid *dynid;
239         struct pcmcia_driver *pdrv = to_pcmcia_drv(driver);
240         __u16 match_flags, manf_id, card_id;
241         __u8 func_id, function, device_no;
242         __u32 prod_id_hash[4] = {0, 0, 0, 0};
243         int fields=0;
244         int retval = 0;
245
246         fields = sscanf(buf, "%hx %hx %hx %hhx %hhx %hhx %x %x %x %x",
247                         &match_flags, &manf_id, &card_id, &func_id, &function, &device_no,
248                         &prod_id_hash[0], &prod_id_hash[1], &prod_id_hash[2], &prod_id_hash[3]);
249         if (fields < 6)
250                 return -EINVAL;
251
252         dynid = kzalloc(sizeof(struct pcmcia_dynid), GFP_KERNEL);
253         if (!dynid)
254                 return -ENOMEM;
255
256         INIT_LIST_HEAD(&dynid->node);
257         dynid->id.match_flags = match_flags;
258         dynid->id.manf_id = manf_id;
259         dynid->id.card_id = card_id;
260         dynid->id.func_id = func_id;
261         dynid->id.function = function;
262         dynid->id.device_no = device_no;
263         memcpy(dynid->id.prod_id_hash, prod_id_hash, sizeof(__u32) * 4);
264
265         spin_lock(&pdrv->dynids.lock);
266         list_add_tail(&pdrv->dynids.list, &dynid->node);
267         spin_unlock(&pdrv->dynids.lock);
268
269         if (get_driver(&pdrv->drv)) {
270                 retval = driver_attach(&pdrv->drv);
271                 put_driver(&pdrv->drv);
272         }
273
274         if (retval)
275                 return retval;
276         return count;
277 }
278 static DRIVER_ATTR(new_id, S_IWUSR, NULL, pcmcia_store_new_id);
279
280 static void
281 pcmcia_free_dynids(struct pcmcia_driver *drv)
282 {
283         struct pcmcia_dynid *dynid, *n;
284
285         spin_lock(&drv->dynids.lock);
286         list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
287                 list_del(&dynid->node);
288                 kfree(dynid);
289         }
290         spin_unlock(&drv->dynids.lock);
291 }
292
293 static int
294 pcmcia_create_newid_file(struct pcmcia_driver *drv)
295 {
296         int error = 0;
297         if (drv->probe != NULL)
298                 error = driver_create_file(&drv->drv, &driver_attr_new_id);
299         return error;
300 }
301
302
303 /**
304  * pcmcia_register_driver - register a PCMCIA driver with the bus core
305  * @driver: the &driver being registered
306  *
307  * Registers a PCMCIA driver with the PCMCIA bus core.
308  */
309 int pcmcia_register_driver(struct pcmcia_driver *driver)
310 {
311         int error;
312
313         if (!driver)
314                 return -EINVAL;
315
316         pcmcia_check_driver(driver);
317
318         /* initialize common fields */
319         driver->drv.bus = &pcmcia_bus_type;
320         driver->drv.owner = driver->owner;
321         spin_lock_init(&driver->dynids.lock);
322         INIT_LIST_HEAD(&driver->dynids.list);
323
324         ds_dbg(3, "registering driver %s\n", driver->drv.name);
325
326         error = driver_register(&driver->drv);
327         if (error < 0)
328                 return error;
329
330         error = pcmcia_create_newid_file(driver);
331         if (error)
332                 driver_unregister(&driver->drv);
333
334         return error;
335 }
336 EXPORT_SYMBOL(pcmcia_register_driver);
337
338 /**
339  * pcmcia_unregister_driver - unregister a PCMCIA driver with the bus core
340  * @driver: the &driver being unregistered
341  */
342 void pcmcia_unregister_driver(struct pcmcia_driver *driver)
343 {
344         ds_dbg(3, "unregistering driver %s\n", driver->drv.name);
345         driver_unregister(&driver->drv);
346         pcmcia_free_dynids(driver);
347 }
348 EXPORT_SYMBOL(pcmcia_unregister_driver);
349
350
351 /* pcmcia_device handling */
352
353 struct pcmcia_device * pcmcia_get_dev(struct pcmcia_device *p_dev)
354 {
355         struct device *tmp_dev;
356         tmp_dev = get_device(&p_dev->dev);
357         if (!tmp_dev)
358                 return NULL;
359         return to_pcmcia_dev(tmp_dev);
360 }
361
362 void pcmcia_put_dev(struct pcmcia_device *p_dev)
363 {
364         if (p_dev)
365                 put_device(&p_dev->dev);
366 }
367
368 static void pcmcia_release_function(struct kref *ref)
369 {
370         struct config_t *c = container_of(ref, struct config_t, ref);
371         ds_dbg(1, "releasing config_t\n");
372         kfree(c);
373 }
374
375 static void pcmcia_release_dev(struct device *dev)
376 {
377         struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
378         ds_dev_dbg(1, dev, "releasing device\n");
379         pcmcia_put_socket(p_dev->socket);
380         kfree(p_dev->devname);
381         kref_put(&p_dev->function_config->ref, pcmcia_release_function);
382         kfree(p_dev);
383 }
384
385 static void pcmcia_add_device_later(struct pcmcia_socket *s, int mfc)
386 {
387         if (!s->pcmcia_state.device_add_pending) {
388                 ds_dev_dbg(1, &s->dev, "scheduling to add %s secondary"
389                        " device to %d\n", mfc ? "mfc" : "pfc", s->sock);
390                 s->pcmcia_state.device_add_pending = 1;
391                 s->pcmcia_state.mfc_pfc = mfc;
392                 schedule_work(&s->device_add);
393         }
394         return;
395 }
396
397 static int pcmcia_device_probe(struct device * dev)
398 {
399         struct pcmcia_device *p_dev;
400         struct pcmcia_driver *p_drv;
401         struct pcmcia_device_id *did;
402         struct pcmcia_socket *s;
403         cistpl_config_t cis_config;
404         int ret = 0;
405
406         dev = get_device(dev);
407         if (!dev)
408                 return -ENODEV;
409
410         p_dev = to_pcmcia_dev(dev);
411         p_drv = to_pcmcia_drv(dev->driver);
412         s = p_dev->socket;
413
414         ds_dev_dbg(1, dev, "trying to bind to %s\n", p_drv->drv.name);
415
416         if ((!p_drv->probe) || (!p_dev->function_config) ||
417             (!try_module_get(p_drv->owner))) {
418                 ret = -EINVAL;
419                 goto put_dev;
420         }
421
422         /* set up some more device information */
423         ret = pccard_read_tuple(p_dev->socket, p_dev->func, CISTPL_CONFIG,
424                                 &cis_config);
425         if (!ret) {
426                 p_dev->conf.ConfigBase = cis_config.base;
427                 p_dev->conf.Present = cis_config.rmask[0];
428         } else {
429                 dev_printk(KERN_INFO, dev,
430                            "pcmcia: could not parse base and rmask0 of CIS\n");
431                 p_dev->conf.ConfigBase = 0;
432                 p_dev->conf.Present = 0;
433         }
434
435         ret = p_drv->probe(p_dev);
436         if (ret) {
437                 ds_dev_dbg(1, dev, "binding to %s failed with %d\n",
438                            p_drv->drv.name, ret);
439                 goto put_module;
440         }
441
442         /* handle pseudo multifunction devices:
443          * there are at most two pseudo multifunction devices.
444          * if we're matching against the first, schedule a
445          * call which will then check whether there are two
446          * pseudo devices, and if not, add the second one.
447          */
448         did = p_dev->dev.driver_data;
449         if (did && (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO) &&
450             (p_dev->socket->device_count == 1) && (p_dev->device_no == 0))
451                 pcmcia_add_device_later(p_dev->socket, 0);
452
453  put_module:
454         if (ret)
455                 module_put(p_drv->owner);
456  put_dev:
457         if (ret)
458                 put_device(dev);
459         return (ret);
460 }
461
462
463 /*
464  * Removes a PCMCIA card from the device tree and socket list.
465  */
466 static void pcmcia_card_remove(struct pcmcia_socket *s, struct pcmcia_device *leftover)
467 {
468         struct pcmcia_device    *p_dev;
469         struct pcmcia_device    *tmp;
470         unsigned long           flags;
471
472         ds_dev_dbg(2, leftover ? &leftover->dev : &s->dev,
473                    "pcmcia_card_remove(%d) %s\n", s->sock,
474                    leftover ? leftover->devname : "");
475
476         if (!leftover)
477                 s->device_count = 0;
478         else
479                 s->device_count = 1;
480
481         /* unregister all pcmcia_devices registered with this socket, except leftover */
482         list_for_each_entry_safe(p_dev, tmp, &s->devices_list, socket_device_list) {
483                 if (p_dev == leftover)
484                         continue;
485
486                 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
487                 list_del(&p_dev->socket_device_list);
488                 p_dev->_removed=1;
489                 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
490
491                 ds_dev_dbg(2, &p_dev->dev, "unregistering device\n");
492                 device_unregister(&p_dev->dev);
493         }
494
495         return;
496 }
497
498 static int pcmcia_device_remove(struct device * dev)
499 {
500         struct pcmcia_device *p_dev;
501         struct pcmcia_driver *p_drv;
502         struct pcmcia_device_id *did;
503         int i;
504
505         p_dev = to_pcmcia_dev(dev);
506         p_drv = to_pcmcia_drv(dev->driver);
507
508         ds_dev_dbg(1, dev, "removing device\n");
509
510         /* If we're removing the primary module driving a
511          * pseudo multi-function card, we need to unbind
512          * all devices
513          */
514         did = p_dev->dev.driver_data;
515         if (did && (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO) &&
516             (p_dev->socket->device_count != 0) &&
517             (p_dev->device_no == 0))
518                 pcmcia_card_remove(p_dev->socket, p_dev);
519
520         /* detach the "instance" */
521         if (!p_drv)
522                 return 0;
523
524         if (p_drv->remove)
525                 p_drv->remove(p_dev);
526
527         p_dev->dev_node = NULL;
528
529         /* check for proper unloading */
530         if (p_dev->_irq || p_dev->_io || p_dev->_locked)
531                 dev_printk(KERN_INFO, dev,
532                         "pcmcia: driver %s did not release config properly\n",
533                         p_drv->drv.name);
534
535         for (i = 0; i < MAX_WIN; i++)
536                 if (p_dev->_win & CLIENT_WIN_REQ(i))
537                         dev_printk(KERN_INFO, dev,
538                           "pcmcia: driver %s did not release window properly\n",
539                            p_drv->drv.name);
540
541         /* references from pcmcia_probe_device */
542         pcmcia_put_dev(p_dev);
543         module_put(p_drv->owner);
544
545         return 0;
546 }
547
548
549 /*
550  * pcmcia_device_query -- determine information about a pcmcia device
551  */
552 static int pcmcia_device_query(struct pcmcia_device *p_dev)
553 {
554         cistpl_manfid_t manf_id;
555         cistpl_funcid_t func_id;
556         cistpl_vers_1_t *vers1;
557         unsigned int i;
558
559         vers1 = kmalloc(sizeof(*vers1), GFP_KERNEL);
560         if (!vers1)
561                 return -ENOMEM;
562
563         if (!pccard_read_tuple(p_dev->socket, p_dev->func,
564                                CISTPL_MANFID, &manf_id)) {
565                 p_dev->manf_id = manf_id.manf;
566                 p_dev->card_id = manf_id.card;
567                 p_dev->has_manf_id = 1;
568                 p_dev->has_card_id = 1;
569         }
570
571         if (!pccard_read_tuple(p_dev->socket, p_dev->func,
572                                CISTPL_FUNCID, &func_id)) {
573                 p_dev->func_id = func_id.func;
574                 p_dev->has_func_id = 1;
575         } else {
576                 /* rule of thumb: cards with no FUNCID, but with
577                  * common memory device geometry information, are
578                  * probably memory cards (from pcmcia-cs) */
579                 cistpl_device_geo_t *devgeo;
580
581                 devgeo = kmalloc(sizeof(*devgeo), GFP_KERNEL);
582                 if (!devgeo) {
583                         kfree(vers1);
584                         return -ENOMEM;
585                 }
586                 if (!pccard_read_tuple(p_dev->socket, p_dev->func,
587                                       CISTPL_DEVICE_GEO, devgeo)) {
588                         ds_dev_dbg(0, &p_dev->dev,
589                                    "mem device geometry probably means "
590                                    "FUNCID_MEMORY\n");
591                         p_dev->func_id = CISTPL_FUNCID_MEMORY;
592                         p_dev->has_func_id = 1;
593                 }
594                 kfree(devgeo);
595         }
596
597         if (!pccard_read_tuple(p_dev->socket, p_dev->func, CISTPL_VERS_1,
598                                vers1)) {
599                 for (i=0; i < vers1->ns; i++) {
600                         char *tmp;
601                         unsigned int length;
602
603                         tmp = vers1->str + vers1->ofs[i];
604
605                         length = strlen(tmp) + 1;
606                         if ((length < 2) || (length > 255))
607                                 continue;
608
609                         p_dev->prod_id[i] = kmalloc(sizeof(char) * length,
610                                                     GFP_KERNEL);
611                         if (!p_dev->prod_id[i])
612                                 continue;
613
614                         p_dev->prod_id[i] = strncpy(p_dev->prod_id[i],
615                                                     tmp, length);
616                 }
617         }
618
619         kfree(vers1);
620         return 0;
621 }
622
623
624 /* device_add_lock is needed to avoid double registration by cardmgr and kernel.
625  * Serializes pcmcia_device_add; will most likely be removed in future.
626  *
627  * While it has the caveat that adding new PCMCIA devices inside(!) device_register()
628  * won't work, this doesn't matter much at the moment: the driver core doesn't
629  * support it either.
630  */
631 static DEFINE_MUTEX(device_add_lock);
632
633 struct pcmcia_device * pcmcia_device_add(struct pcmcia_socket *s, unsigned int function)
634 {
635         struct pcmcia_device *p_dev, *tmp_dev;
636         unsigned long flags;
637         int bus_id_len;
638
639         s = pcmcia_get_socket(s);
640         if (!s)
641                 return NULL;
642
643         mutex_lock(&device_add_lock);
644
645         ds_dbg(3, "adding device to %d, function %d\n", s->sock, function);
646
647         /* max of 4 devices per card */
648         if (s->device_count == 4)
649                 goto err_put;
650
651         p_dev = kzalloc(sizeof(struct pcmcia_device), GFP_KERNEL);
652         if (!p_dev)
653                 goto err_put;
654
655         p_dev->socket = s;
656         p_dev->device_no = (s->device_count++);
657         p_dev->func   = function;
658
659         p_dev->dev.bus = &pcmcia_bus_type;
660         p_dev->dev.parent = s->dev.parent;
661         p_dev->dev.release = pcmcia_release_dev;
662         /* by default don't allow DMA */
663         p_dev->dma_mask = DMA_MASK_NONE;
664         p_dev->dev.dma_mask = &p_dev->dma_mask;
665         bus_id_len = sprintf (p_dev->dev.bus_id, "%d.%d", p_dev->socket->sock, p_dev->device_no);
666
667         p_dev->devname = kmalloc(6 + bus_id_len + 1, GFP_KERNEL);
668         if (!p_dev->devname)
669                 goto err_free;
670         sprintf (p_dev->devname, "pcmcia%s", p_dev->dev.bus_id);
671         ds_dev_dbg(3, &p_dev->dev, "devname is %s\n", p_dev->devname);
672
673         spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
674
675         /*
676          * p_dev->function_config must be the same for all card functions.
677          * Note that this is serialized by the device_add_lock, so that
678          * only one such struct will be created.
679          */
680         list_for_each_entry(tmp_dev, &s->devices_list, socket_device_list)
681                 if (p_dev->func == tmp_dev->func) {
682                         p_dev->function_config = tmp_dev->function_config;
683                         kref_get(&p_dev->function_config->ref);
684                 }
685
686         /* Add to the list in pcmcia_bus_socket */
687         list_add(&p_dev->socket_device_list, &s->devices_list);
688
689         spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
690
691         if (!p_dev->function_config) {
692                 ds_dev_dbg(3, &p_dev->dev, "creating config_t\n");
693                 p_dev->function_config = kzalloc(sizeof(struct config_t),
694                                                  GFP_KERNEL);
695                 if (!p_dev->function_config)
696                         goto err_unreg;
697                 kref_init(&p_dev->function_config->ref);
698         }
699
700         dev_printk(KERN_NOTICE, &p_dev->dev,
701                    "pcmcia: registering new device %s\n",
702                    p_dev->devname);
703
704         pcmcia_device_query(p_dev);
705
706         if (device_register(&p_dev->dev))
707                 goto err_unreg;
708
709         mutex_unlock(&device_add_lock);
710
711         return p_dev;
712
713  err_unreg:
714         spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
715         list_del(&p_dev->socket_device_list);
716         spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
717
718  err_free:
719         kfree(p_dev->devname);
720         kfree(p_dev);
721         s->device_count--;
722  err_put:
723         mutex_unlock(&device_add_lock);
724         pcmcia_put_socket(s);
725
726         return NULL;
727 }
728
729
730 static int pcmcia_card_add(struct pcmcia_socket *s)
731 {
732         cistpl_longlink_mfc_t mfc;
733         unsigned int no_funcs, i, no_chains;
734         int ret = 0;
735
736         if (!(s->resource_setup_done)) {
737                 ds_dev_dbg(3, &s->dev,
738                            "no resources available, delaying card_add\n");
739                 return -EAGAIN; /* try again, but later... */
740         }
741
742         if (pcmcia_validate_mem(s)) {
743                 ds_dev_dbg(3, &s->dev, "validating mem resources failed, "
744                        "delaying card_add\n");
745                 return -EAGAIN; /* try again, but later... */
746         }
747
748         ret = pccard_validate_cis(s, BIND_FN_ALL, &no_chains);
749         if (ret || !no_chains) {
750                 ds_dev_dbg(0, &s->dev, "invalid CIS or invalid resources\n");
751                 return -ENODEV;
752         }
753
754         if (!pccard_read_tuple(s, BIND_FN_ALL, CISTPL_LONGLINK_MFC, &mfc))
755                 no_funcs = mfc.nfn;
756         else
757                 no_funcs = 1;
758         s->functions = no_funcs;
759
760         for (i=0; i < no_funcs; i++)
761                 pcmcia_device_add(s, i);
762
763         return (ret);
764 }
765
766
767 static void pcmcia_delayed_add_device(struct work_struct *work)
768 {
769         struct pcmcia_socket *s =
770                 container_of(work, struct pcmcia_socket, device_add);
771         ds_dev_dbg(1, &s->dev, "adding additional device to %d\n", s->sock);
772         pcmcia_device_add(s, s->pcmcia_state.mfc_pfc);
773         s->pcmcia_state.device_add_pending = 0;
774         s->pcmcia_state.mfc_pfc = 0;
775 }
776
777 static int pcmcia_requery(struct device *dev, void * _data)
778 {
779         struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
780         if (!p_dev->dev.driver) {
781                 ds_dev_dbg(1, dev, "update device information\n");
782                 pcmcia_device_query(p_dev);
783         }
784
785         return 0;
786 }
787
788 static void pcmcia_bus_rescan(struct pcmcia_socket *skt, int new_cis)
789 {
790         int no_devices = 0;
791         int ret = 0;
792         unsigned long flags;
793
794         /* must be called with skt_mutex held */
795         ds_dev_dbg(0, &skt->dev, "re-scanning socket %d\n", skt->sock);
796
797         spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
798         if (list_empty(&skt->devices_list))
799                 no_devices = 1;
800         spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
801
802         /* If this is because of a CIS override, start over */
803         if (new_cis && !no_devices)
804                 pcmcia_card_remove(skt, NULL);
805
806         /* if no devices were added for this socket yet because of
807          * missing resource information or other trouble, we need to
808          * do this now. */
809         if (no_devices || new_cis) {
810                 ret = pcmcia_card_add(skt);
811                 if (ret)
812                         return;
813         }
814
815         /* some device information might have changed because of a CIS
816          * update or because we can finally read it correctly... so
817          * determine it again, overwriting old values if necessary. */
818         bus_for_each_dev(&pcmcia_bus_type, NULL, NULL, pcmcia_requery);
819
820         /* we re-scan all devices, not just the ones connected to this
821          * socket. This does not matter, though. */
822         ret = bus_rescan_devices(&pcmcia_bus_type);
823         if (ret)
824                 printk(KERN_INFO "pcmcia: bus_rescan_devices failed\n");
825 }
826
827 #ifdef CONFIG_PCMCIA_LOAD_CIS
828
829 /**
830  * pcmcia_load_firmware - load CIS from userspace if device-provided is broken
831  * @dev: the pcmcia device which needs a CIS override
832  * @filename: requested filename in /lib/firmware/
833  *
834  * This uses the in-kernel firmware loading mechanism to use a "fake CIS" if
835  * the one provided by the card is broken. The firmware files reside in
836  * /lib/firmware/ in userspace.
837  */
838 static int pcmcia_load_firmware(struct pcmcia_device *dev, char * filename)
839 {
840         struct pcmcia_socket *s = dev->socket;
841         const struct firmware *fw;
842         char path[FIRMWARE_NAME_MAX];
843         int ret = -ENOMEM;
844         int no_funcs;
845         int old_funcs;
846         cistpl_longlink_mfc_t mfc;
847
848         if (!filename)
849                 return -EINVAL;
850
851         ds_dev_dbg(1, &dev->dev, "trying to load CIS file %s\n", filename);
852
853         if (strlen(filename) > (FIRMWARE_NAME_MAX - 1)) {
854                 dev_printk(KERN_WARNING, &dev->dev,
855                            "pcmcia: CIS filename is too long [%s]\n",
856                            filename);
857                 return -EINVAL;
858         }
859
860         snprintf(path, sizeof(path), "%s", filename);
861
862         if (request_firmware(&fw, path, &dev->dev) == 0) {
863                 if (fw->size >= CISTPL_MAX_CIS_SIZE) {
864                         ret = -EINVAL;
865                         dev_printk(KERN_ERR, &dev->dev,
866                                    "pcmcia: CIS override is too big\n");
867                         goto release;
868                 }
869
870                 if (!pcmcia_replace_cis(s, fw->data, fw->size))
871                         ret = 0;
872                 else {
873                         dev_printk(KERN_ERR, &dev->dev,
874                                    "pcmcia: CIS override failed\n");
875                         goto release;
876                 }
877
878
879                 /* update information */
880                 pcmcia_device_query(dev);
881
882                 /* does this cis override add or remove functions? */
883                 old_funcs = s->functions;
884
885                 if (!pccard_read_tuple(s, BIND_FN_ALL, CISTPL_LONGLINK_MFC, &mfc))
886                         no_funcs = mfc.nfn;
887                 else
888                         no_funcs = 1;
889                 s->functions = no_funcs;
890
891                 if (old_funcs > no_funcs)
892                         pcmcia_card_remove(s, dev);
893                 else if (no_funcs > old_funcs)
894                         pcmcia_add_device_later(s, 1);
895         }
896  release:
897         release_firmware(fw);
898
899         return (ret);
900 }
901
902 #else /* !CONFIG_PCMCIA_LOAD_CIS */
903
904 static inline int pcmcia_load_firmware(struct pcmcia_device *dev, char * filename)
905 {
906         return -ENODEV;
907 }
908
909 #endif
910
911
912 static inline int pcmcia_devmatch(struct pcmcia_device *dev,
913                                   struct pcmcia_device_id *did)
914 {
915         if (did->match_flags & PCMCIA_DEV_ID_MATCH_MANF_ID) {
916                 if ((!dev->has_manf_id) || (dev->manf_id != did->manf_id))
917                         return 0;
918         }
919
920         if (did->match_flags & PCMCIA_DEV_ID_MATCH_CARD_ID) {
921                 if ((!dev->has_card_id) || (dev->card_id != did->card_id))
922                         return 0;
923         }
924
925         if (did->match_flags & PCMCIA_DEV_ID_MATCH_FUNCTION) {
926                 if (dev->func != did->function)
927                         return 0;
928         }
929
930         if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID1) {
931                 if (!dev->prod_id[0])
932                         return 0;
933                 if (strcmp(did->prod_id[0], dev->prod_id[0]))
934                         return 0;
935         }
936
937         if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID2) {
938                 if (!dev->prod_id[1])
939                         return 0;
940                 if (strcmp(did->prod_id[1], dev->prod_id[1]))
941                         return 0;
942         }
943
944         if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID3) {
945                 if (!dev->prod_id[2])
946                         return 0;
947                 if (strcmp(did->prod_id[2], dev->prod_id[2]))
948                         return 0;
949         }
950
951         if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID4) {
952                 if (!dev->prod_id[3])
953                         return 0;
954                 if (strcmp(did->prod_id[3], dev->prod_id[3]))
955                         return 0;
956         }
957
958         if (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO) {
959                 if (dev->device_no != did->device_no)
960                         return 0;
961         }
962
963         if (did->match_flags & PCMCIA_DEV_ID_MATCH_FUNC_ID) {
964                 if ((!dev->has_func_id) || (dev->func_id != did->func_id))
965                         return 0;
966
967                 /* if this is a pseudo-multi-function device,
968                  * we need explicit matches */
969                 if (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO)
970                         return 0;
971                 if (dev->device_no)
972                         return 0;
973
974                 /* also, FUNC_ID matching needs to be activated by userspace
975                  * after it has re-checked that there is no possible module
976                  * with a prod_id/manf_id/card_id match.
977                  */
978                 ds_dev_dbg(0, &dev->dev,
979                         "skipping FUNC_ID match until userspace interaction\n");
980                 if (!dev->allow_func_id_match)
981                         return 0;
982         }
983
984         if (did->match_flags & PCMCIA_DEV_ID_MATCH_FAKE_CIS) {
985                 ds_dev_dbg(0, &dev->dev, "device needs a fake CIS\n");
986                 if (!dev->socket->fake_cis)
987                         pcmcia_load_firmware(dev, did->cisfile);
988
989                 if (!dev->socket->fake_cis)
990                         return 0;
991         }
992
993         if (did->match_flags & PCMCIA_DEV_ID_MATCH_ANONYMOUS) {
994                 int i;
995                 for (i=0; i<4; i++)
996                         if (dev->prod_id[i])
997                                 return 0;
998                 if (dev->has_manf_id || dev->has_card_id || dev->has_func_id)
999                         return 0;
1000         }
1001
1002         dev->dev.driver_data = (void *) did;
1003
1004         return 1;
1005 }
1006
1007
1008 static int pcmcia_bus_match(struct device * dev, struct device_driver * drv) {
1009         struct pcmcia_device * p_dev = to_pcmcia_dev(dev);
1010         struct pcmcia_driver * p_drv = to_pcmcia_drv(drv);
1011         struct pcmcia_device_id *did = p_drv->id_table;
1012         struct pcmcia_dynid *dynid;
1013
1014         /* match dynamic devices first */
1015         spin_lock(&p_drv->dynids.lock);
1016         list_for_each_entry(dynid, &p_drv->dynids.list, node) {
1017                 ds_dev_dbg(3, dev, "trying to match to %s\n", drv->name);
1018                 if (pcmcia_devmatch(p_dev, &dynid->id)) {
1019                         ds_dev_dbg(0, dev, "matched to %s\n", drv->name);
1020                         spin_unlock(&p_drv->dynids.lock);
1021                         return 1;
1022                 }
1023         }
1024         spin_unlock(&p_drv->dynids.lock);
1025
1026 #ifdef CONFIG_PCMCIA_IOCTL
1027         /* matching by cardmgr */
1028         if (p_dev->cardmgr == p_drv) {
1029                 ds_dev_dbg(0, dev, "cardmgr matched to %s\n", drv->name);
1030                 return 1;
1031         }
1032 #endif
1033
1034         while (did && did->match_flags) {
1035                 ds_dev_dbg(3, dev, "trying to match to %s\n", drv->name);
1036                 if (pcmcia_devmatch(p_dev, did)) {
1037                         ds_dev_dbg(0, dev, "matched to %s\n", drv->name);
1038                         return 1;
1039                 }
1040                 did++;
1041         }
1042
1043         return 0;
1044 }
1045
1046 #ifdef CONFIG_HOTPLUG
1047
1048 static int pcmcia_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
1049 {
1050         struct pcmcia_device *p_dev;
1051         int i;
1052         u32 hash[4] = { 0, 0, 0, 0};
1053
1054         if (!dev)
1055                 return -ENODEV;
1056
1057         p_dev = to_pcmcia_dev(dev);
1058
1059         /* calculate hashes */
1060         for (i=0; i<4; i++) {
1061                 if (!p_dev->prod_id[i])
1062                         continue;
1063                 hash[i] = crc32(0, p_dev->prod_id[i], strlen(p_dev->prod_id[i]));
1064         }
1065
1066         if (add_uevent_var(env, "SOCKET_NO=%u", p_dev->socket->sock))
1067                 return -ENOMEM;
1068
1069         if (add_uevent_var(env, "DEVICE_NO=%02X", p_dev->device_no))
1070                 return -ENOMEM;
1071
1072         if (add_uevent_var(env, "MODALIAS=pcmcia:m%04Xc%04Xf%02Xfn%02Xpfn%02X"
1073                            "pa%08Xpb%08Xpc%08Xpd%08X",
1074                            p_dev->has_manf_id ? p_dev->manf_id : 0,
1075                            p_dev->has_card_id ? p_dev->card_id : 0,
1076                            p_dev->has_func_id ? p_dev->func_id : 0,
1077                            p_dev->func,
1078                            p_dev->device_no,
1079                            hash[0],
1080                            hash[1],
1081                            hash[2],
1082                            hash[3]))
1083                 return -ENOMEM;
1084
1085         return 0;
1086 }
1087
1088 #else
1089
1090 static int pcmcia_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
1091 {
1092         return -ENODEV;
1093 }
1094
1095 #endif
1096
1097 /************************ runtime PM support ***************************/
1098
1099 static int pcmcia_dev_suspend(struct device *dev, pm_message_t state);
1100 static int pcmcia_dev_resume(struct device *dev);
1101
1102 static int runtime_suspend(struct device *dev)
1103 {
1104         int rc;
1105
1106         down(&dev->sem);
1107         rc = pcmcia_dev_suspend(dev, PMSG_SUSPEND);
1108         up(&dev->sem);
1109         return rc;
1110 }
1111
1112 static void runtime_resume(struct device *dev)
1113 {
1114         int rc;
1115
1116         down(&dev->sem);
1117         rc = pcmcia_dev_resume(dev);
1118         up(&dev->sem);
1119 }
1120
1121 /************************ per-device sysfs output ***************************/
1122
1123 #define pcmcia_device_attr(field, test, format)                         \
1124 static ssize_t field##_show (struct device *dev, struct device_attribute *attr, char *buf)              \
1125 {                                                                       \
1126         struct pcmcia_device *p_dev = to_pcmcia_dev(dev);               \
1127         return p_dev->test ? sprintf (buf, format, p_dev->field) : -ENODEV; \
1128 }
1129
1130 #define pcmcia_device_stringattr(name, field)                                   \
1131 static ssize_t name##_show (struct device *dev, struct device_attribute *attr, char *buf)               \
1132 {                                                                       \
1133         struct pcmcia_device *p_dev = to_pcmcia_dev(dev);               \
1134         return p_dev->field ? sprintf (buf, "%s\n", p_dev->field) : -ENODEV; \
1135 }
1136
1137 pcmcia_device_attr(func, socket, "0x%02x\n");
1138 pcmcia_device_attr(func_id, has_func_id, "0x%02x\n");
1139 pcmcia_device_attr(manf_id, has_manf_id, "0x%04x\n");
1140 pcmcia_device_attr(card_id, has_card_id, "0x%04x\n");
1141 pcmcia_device_stringattr(prod_id1, prod_id[0]);
1142 pcmcia_device_stringattr(prod_id2, prod_id[1]);
1143 pcmcia_device_stringattr(prod_id3, prod_id[2]);
1144 pcmcia_device_stringattr(prod_id4, prod_id[3]);
1145
1146
1147 static ssize_t pcmcia_show_pm_state(struct device *dev, struct device_attribute *attr, char *buf)
1148 {
1149         struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1150
1151         if (p_dev->suspended)
1152                 return sprintf(buf, "off\n");
1153         else
1154                 return sprintf(buf, "on\n");
1155 }
1156
1157 static ssize_t pcmcia_store_pm_state(struct device *dev, struct device_attribute *attr,
1158                                      const char *buf, size_t count)
1159 {
1160         struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1161         int ret = 0;
1162
1163         if (!count)
1164                 return -EINVAL;
1165
1166         if ((!p_dev->suspended) && !strncmp(buf, "off", 3))
1167                 ret = runtime_suspend(dev);
1168         else if (p_dev->suspended && !strncmp(buf, "on", 2))
1169                 runtime_resume(dev);
1170
1171         return ret ? ret : count;
1172 }
1173
1174
1175 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
1176 {
1177         struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1178         int i;
1179         u32 hash[4] = { 0, 0, 0, 0};
1180
1181         /* calculate hashes */
1182         for (i=0; i<4; i++) {
1183                 if (!p_dev->prod_id[i])
1184                         continue;
1185                 hash[i] = crc32(0,p_dev->prod_id[i],strlen(p_dev->prod_id[i]));
1186         }
1187         return sprintf(buf, "pcmcia:m%04Xc%04Xf%02Xfn%02Xpfn%02X"
1188                                 "pa%08Xpb%08Xpc%08Xpd%08X\n",
1189                                 p_dev->has_manf_id ? p_dev->manf_id : 0,
1190                                 p_dev->has_card_id ? p_dev->card_id : 0,
1191                                 p_dev->has_func_id ? p_dev->func_id : 0,
1192                                 p_dev->func, p_dev->device_no,
1193                                 hash[0], hash[1], hash[2], hash[3]);
1194 }
1195
1196 static ssize_t pcmcia_store_allow_func_id_match(struct device *dev,
1197                 struct device_attribute *attr, const char *buf, size_t count)
1198 {
1199         struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1200         int ret;
1201
1202         if (!count)
1203                 return -EINVAL;
1204
1205         mutex_lock(&p_dev->socket->skt_mutex);
1206         p_dev->allow_func_id_match = 1;
1207         mutex_unlock(&p_dev->socket->skt_mutex);
1208
1209         ret = bus_rescan_devices(&pcmcia_bus_type);
1210         if (ret)
1211                 printk(KERN_INFO "pcmcia: bus_rescan_devices failed after "
1212                        "allowing func_id matches\n");
1213
1214         return count;
1215 }
1216
1217 static struct device_attribute pcmcia_dev_attrs[] = {
1218         __ATTR(function, 0444, func_show, NULL),
1219         __ATTR(pm_state, 0644, pcmcia_show_pm_state, pcmcia_store_pm_state),
1220         __ATTR_RO(func_id),
1221         __ATTR_RO(manf_id),
1222         __ATTR_RO(card_id),
1223         __ATTR_RO(prod_id1),
1224         __ATTR_RO(prod_id2),
1225         __ATTR_RO(prod_id3),
1226         __ATTR_RO(prod_id4),
1227         __ATTR_RO(modalias),
1228         __ATTR(allow_func_id_match, 0200, NULL, pcmcia_store_allow_func_id_match),
1229         __ATTR_NULL,
1230 };
1231
1232 /* PM support, also needed for reset */
1233
1234 static int pcmcia_dev_suspend(struct device * dev, pm_message_t state)
1235 {
1236         struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1237         struct pcmcia_driver *p_drv = NULL;
1238         int ret = 0;
1239
1240         if (p_dev->suspended)
1241                 return 0;
1242
1243         ds_dev_dbg(2, dev, "suspending\n");
1244
1245         if (dev->driver)
1246                 p_drv = to_pcmcia_drv(dev->driver);
1247
1248         if (!p_drv)
1249                 goto out;
1250
1251         if (p_drv->suspend) {
1252                 ret = p_drv->suspend(p_dev);
1253                 if (ret) {
1254                         dev_printk(KERN_ERR, dev,
1255                                    "pcmcia: device %s (driver %s) did "
1256                                    "not want to go to sleep (%d)\n",
1257                                    p_dev->devname, p_drv->drv.name, ret);
1258                         goto out;
1259                 }
1260         }
1261
1262         if (p_dev->device_no == p_dev->func) {
1263                 ds_dev_dbg(2, dev, "releasing configuration\n");
1264                 pcmcia_release_configuration(p_dev);
1265         }
1266
1267  out:
1268         if (!ret)
1269                 p_dev->suspended = 1;
1270         return ret;
1271 }
1272
1273
1274 static int pcmcia_dev_resume(struct device * dev)
1275 {
1276         struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1277         struct pcmcia_driver *p_drv = NULL;
1278         int ret = 0;
1279
1280         if (!p_dev->suspended)
1281                 return 0;
1282
1283         ds_dev_dbg(2, dev, "resuming\n");
1284
1285         if (dev->driver)
1286                 p_drv = to_pcmcia_drv(dev->driver);
1287
1288         if (!p_drv)
1289                 goto out;
1290
1291         if (p_dev->device_no == p_dev->func) {
1292                 ds_dev_dbg(2, dev, "requesting configuration\n");
1293                 ret = pcmcia_request_configuration(p_dev, &p_dev->conf);
1294                 if (ret)
1295                         goto out;
1296         }
1297
1298         if (p_drv->resume)
1299                 ret = p_drv->resume(p_dev);
1300
1301  out:
1302         if (!ret)
1303                 p_dev->suspended = 0;
1304         return ret;
1305 }
1306
1307
1308 static int pcmcia_bus_suspend_callback(struct device *dev, void * _data)
1309 {
1310         struct pcmcia_socket *skt = _data;
1311         struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1312
1313         if (p_dev->socket != skt || p_dev->suspended)
1314                 return 0;
1315
1316         return runtime_suspend(dev);
1317 }
1318
1319 static int pcmcia_bus_resume_callback(struct device *dev, void * _data)
1320 {
1321         struct pcmcia_socket *skt = _data;
1322         struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1323
1324         if (p_dev->socket != skt || !p_dev->suspended)
1325                 return 0;
1326
1327         runtime_resume(dev);
1328
1329         return 0;
1330 }
1331
1332 static int pcmcia_bus_resume(struct pcmcia_socket *skt)
1333 {
1334         ds_dev_dbg(2, &skt->dev, "resuming socket %d\n", skt->sock);
1335         bus_for_each_dev(&pcmcia_bus_type, NULL, skt, pcmcia_bus_resume_callback);
1336         return 0;
1337 }
1338
1339 static int pcmcia_bus_suspend(struct pcmcia_socket *skt)
1340 {
1341         ds_dev_dbg(2, &skt->dev, "suspending socket %d\n", skt->sock);
1342         if (bus_for_each_dev(&pcmcia_bus_type, NULL, skt,
1343                              pcmcia_bus_suspend_callback)) {
1344                 pcmcia_bus_resume(skt);
1345                 return -EIO;
1346         }
1347         return 0;
1348 }
1349
1350
1351 /*======================================================================
1352
1353     The card status event handler.
1354     
1355 ======================================================================*/
1356
1357 /* Normally, the event is passed to individual drivers after
1358  * informing userspace. Only for CS_EVENT_CARD_REMOVAL this
1359  * is inversed to maintain historic compatibility.
1360  */
1361
1362 static int ds_event(struct pcmcia_socket *skt, event_t event, int priority)
1363 {
1364         struct pcmcia_socket *s = pcmcia_get_socket(skt);
1365
1366         if (!s) {
1367                 dev_printk(KERN_ERR, &skt->dev,
1368                            "PCMCIA obtaining reference to socket "      \
1369                            "failed, event 0x%x lost!\n", event);
1370                 return -ENODEV;
1371         }
1372
1373         ds_dev_dbg(1, &skt->dev, "ds_event(0x%06x, %d, 0x%p)\n",
1374                    event, priority, skt);
1375
1376         switch (event) {
1377         case CS_EVENT_CARD_REMOVAL:
1378                 s->pcmcia_state.present = 0;
1379                 pcmcia_card_remove(skt, NULL);
1380                 handle_event(skt, event);
1381                 break;
1382
1383         case CS_EVENT_CARD_INSERTION:
1384                 s->pcmcia_state.present = 1;
1385                 pcmcia_card_add(skt);
1386                 handle_event(skt, event);
1387                 break;
1388
1389         case CS_EVENT_EJECTION_REQUEST:
1390                 break;
1391
1392         case CS_EVENT_PM_SUSPEND:
1393         case CS_EVENT_PM_RESUME:
1394         case CS_EVENT_RESET_PHYSICAL:
1395         case CS_EVENT_CARD_RESET:
1396         default:
1397                 handle_event(skt, event);
1398                 break;
1399     }
1400
1401     pcmcia_put_socket(s);
1402
1403     return 0;
1404 } /* ds_event */
1405
1406
1407 struct pcmcia_device * pcmcia_dev_present(struct pcmcia_device *_p_dev)
1408 {
1409         struct pcmcia_device *p_dev;
1410         struct pcmcia_device *ret = NULL;
1411
1412         p_dev = pcmcia_get_dev(_p_dev);
1413         if (!p_dev)
1414                 return NULL;
1415
1416         if (!p_dev->socket->pcmcia_state.present)
1417                 goto out;
1418
1419         if (p_dev->_removed)
1420                 goto out;
1421
1422         if (p_dev->suspended)
1423                 goto out;
1424
1425         ret = p_dev;
1426  out:
1427         pcmcia_put_dev(p_dev);
1428         return ret;
1429 }
1430 EXPORT_SYMBOL(pcmcia_dev_present);
1431
1432
1433 static struct pcmcia_callback pcmcia_bus_callback = {
1434         .owner = THIS_MODULE,
1435         .event = ds_event,
1436         .requery = pcmcia_bus_rescan,
1437         .suspend = pcmcia_bus_suspend,
1438         .resume = pcmcia_bus_resume,
1439 };
1440
1441 static int __devinit pcmcia_bus_add_socket(struct device *dev,
1442                                            struct class_interface *class_intf)
1443 {
1444         struct pcmcia_socket *socket = dev_get_drvdata(dev);
1445         int ret;
1446
1447         socket = pcmcia_get_socket(socket);
1448         if (!socket) {
1449                 dev_printk(KERN_ERR, dev,
1450                            "PCMCIA obtaining reference to socket failed\n");
1451                 return -ENODEV;
1452         }
1453
1454         /*
1455          * Ugly. But we want to wait for the socket threads to have started up.
1456          * We really should let the drivers themselves drive some of this..
1457          */
1458         msleep(250);
1459
1460 #ifdef CONFIG_PCMCIA_IOCTL
1461         init_waitqueue_head(&socket->queue);
1462 #endif
1463         INIT_LIST_HEAD(&socket->devices_list);
1464         INIT_WORK(&socket->device_add, pcmcia_delayed_add_device);
1465         memset(&socket->pcmcia_state, 0, sizeof(u8));
1466         socket->device_count = 0;
1467
1468         ret = pccard_register_pcmcia(socket, &pcmcia_bus_callback);
1469         if (ret) {
1470                 dev_printk(KERN_ERR, dev, "PCMCIA registration failed\n");
1471                 pcmcia_put_socket(socket);
1472                 return (ret);
1473         }
1474
1475         return 0;
1476 }
1477
1478 static void pcmcia_bus_remove_socket(struct device *dev,
1479                                      struct class_interface *class_intf)
1480 {
1481         struct pcmcia_socket *socket = dev_get_drvdata(dev);
1482
1483         if (!socket)
1484                 return;
1485
1486         socket->pcmcia_state.dead = 1;
1487         pccard_register_pcmcia(socket, NULL);
1488
1489         /* unregister any unbound devices */
1490         mutex_lock(&socket->skt_mutex);
1491         pcmcia_card_remove(socket, NULL);
1492         mutex_unlock(&socket->skt_mutex);
1493
1494         pcmcia_put_socket(socket);
1495
1496         return;
1497 }
1498
1499
1500 /* the pcmcia_bus_interface is used to handle pcmcia socket devices */
1501 static struct class_interface pcmcia_bus_interface __refdata = {
1502         .class = &pcmcia_socket_class,
1503         .add_dev = &pcmcia_bus_add_socket,
1504         .remove_dev = &pcmcia_bus_remove_socket,
1505 };
1506
1507
1508 struct bus_type pcmcia_bus_type = {
1509         .name = "pcmcia",
1510         .uevent = pcmcia_bus_uevent,
1511         .match = pcmcia_bus_match,
1512         .dev_attrs = pcmcia_dev_attrs,
1513         .probe = pcmcia_device_probe,
1514         .remove = pcmcia_device_remove,
1515         .suspend = pcmcia_dev_suspend,
1516         .resume = pcmcia_dev_resume,
1517 };
1518
1519
1520 static int __init init_pcmcia_bus(void)
1521 {
1522         int ret;
1523
1524         spin_lock_init(&pcmcia_dev_list_lock);
1525
1526         ret = bus_register(&pcmcia_bus_type);
1527         if (ret < 0) {
1528                 printk(KERN_WARNING "pcmcia: bus_register error: %d\n", ret);
1529                 return ret;
1530         }
1531         ret = class_interface_register(&pcmcia_bus_interface);
1532         if (ret < 0) {
1533                 printk(KERN_WARNING
1534                         "pcmcia: class_interface_register error: %d\n", ret);
1535                 bus_unregister(&pcmcia_bus_type);
1536                 return ret;
1537         }
1538
1539         pcmcia_setup_ioctl();
1540
1541         return 0;
1542 }
1543 fs_initcall(init_pcmcia_bus); /* one level after subsys_initcall so that 
1544                                * pcmcia_socket_class is already registered */
1545
1546
1547 static void __exit exit_pcmcia_bus(void)
1548 {
1549         pcmcia_cleanup_ioctl();
1550
1551         class_interface_unregister(&pcmcia_bus_interface);
1552
1553         bus_unregister(&pcmcia_bus_type);
1554 }
1555 module_exit(exit_pcmcia_bus);
1556
1557
1558 MODULE_ALIAS("ds");