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