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