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