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