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