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