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