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