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