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