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