CPU: Introduce ARCH_HAS_CPU_AUTOPROBE and X86 parts
[deliverable/linux.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
7b24e798 13 * (C) 2003 - 2010 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>
5a0e3ad6 27#include <linux/slab.h>
1da177e4 28
1da177e4
LT
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
39MODULE_AUTHOR("David Hinds <dahinds@users.sourceforge.net>");
40MODULE_DESCRIPTION("PCMCIA Driver Services");
41MODULE_LICENSE("GPL");
42
1da177e4 43
1da177e4
LT
44/*====================================================================*/
45
23a83bfe
DB
46static void pcmcia_check_driver(struct pcmcia_driver *p_drv)
47{
e9fb13bf 48 const struct pcmcia_device_id *did = p_drv->id_table;
23a83bfe
DB
49 unsigned int i;
50 u32 hash;
51
f8cfa618 52 if (!p_drv->probe || !p_drv->remove)
ba5bb6b5 53 printk(KERN_DEBUG "pcmcia: %s lacks a requisite callback "
2e9b981a 54 "function\n", p_drv->name);
1e212f36 55
23a83bfe 56 while (did && did->match_flags) {
9fea84f4 57 for (i = 0; i < 4; i++) {
23a83bfe
DB
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 "
2e9b981a 67 "be 0x%x\n", p_drv->name, did->prod_id[i],
23a83bfe 68 did->prod_id_hash[i], hash);
5085cb26
DB
69 printk(KERN_DEBUG "pcmcia: see "
70 "Documentation/pcmcia/devicetable.txt for "
71 "details\n");
23a83bfe
DB
72 }
73 did++;
74 }
75
76 return;
77}
78
daa9517d 79
1da177e4
LT
80/*======================================================================*/
81
1da177e4 82
6179b556
BW
83struct 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 */
97static ssize_t
98pcmcia_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};
9fea84f4 105 int fields = 0;
6179b556
BW
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
6179b556
BW
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
3f565232 126 mutex_lock(&pdrv->dynids.lock);
b4b3d7bb 127 list_add_tail(&dynid->node, &pdrv->dynids.list);
3f565232 128 mutex_unlock(&pdrv->dynids.lock);
6179b556 129
cef9bc56 130 retval = driver_attach(&pdrv->drv);
6179b556
BW
131
132 if (retval)
133 return retval;
134 return count;
135}
136static DRIVER_ATTR(new_id, S_IWUSR, NULL, pcmcia_store_new_id);
137
138static void
139pcmcia_free_dynids(struct pcmcia_driver *drv)
140{
141 struct pcmcia_dynid *dynid, *n;
142
3f565232 143 mutex_lock(&drv->dynids.lock);
6179b556
BW
144 list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
145 list_del(&dynid->node);
146 kfree(dynid);
147 }
3f565232 148 mutex_unlock(&drv->dynids.lock);
6179b556
BW
149}
150
151static int
152pcmcia_create_newid_file(struct pcmcia_driver *drv)
153{
154 int error = 0;
155 if (drv->probe != NULL)
2344c6de 156 error = driver_create_file(&drv->drv, &driver_attr_new_id);
6179b556
BW
157 return error;
158}
159
160
1da177e4
LT
161/**
162 * pcmcia_register_driver - register a PCMCIA driver with the bus core
78187865 163 * @driver: the &driver being registered
1da177e4
LT
164 *
165 * Registers a PCMCIA driver with the PCMCIA bus core.
166 */
1da177e4
LT
167int pcmcia_register_driver(struct pcmcia_driver *driver)
168{
6179b556
BW
169 int error;
170
1da177e4
LT
171 if (!driver)
172 return -EINVAL;
173
23a83bfe
DB
174 pcmcia_check_driver(driver);
175
1da177e4
LT
176 /* initialize common fields */
177 driver->drv.bus = &pcmcia_bus_type;
178 driver->drv.owner = driver->owner;
2e9b981a 179 driver->drv.name = driver->name;
3f565232 180 mutex_init(&driver->dynids.lock);
6179b556 181 INIT_LIST_HEAD(&driver->dynids.list);
1da177e4 182
2e9b981a 183 pr_debug("registering driver %s\n", driver->name);
d9d9ea01 184
6179b556
BW
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;
1da177e4
LT
194}
195EXPORT_SYMBOL(pcmcia_register_driver);
196
197/**
198 * pcmcia_unregister_driver - unregister a PCMCIA driver with the bus core
78187865 199 * @driver: the &driver being unregistered
1da177e4
LT
200 */
201void pcmcia_unregister_driver(struct pcmcia_driver *driver)
202{
2e9b981a 203 pr_debug("unregistering driver %s\n", driver->name);
1da177e4 204 driver_unregister(&driver->drv);
6179b556 205 pcmcia_free_dynids(driver);
1da177e4
LT
206}
207EXPORT_SYMBOL(pcmcia_unregister_driver);
208
1da177e4
LT
209
210/* pcmcia_device handling */
211
5716d415 212static struct pcmcia_device *pcmcia_get_dev(struct pcmcia_device *p_dev)
1da177e4
LT
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
5716d415 221static void pcmcia_put_dev(struct pcmcia_device *p_dev)
1da177e4
LT
222{
223 if (p_dev)
224 put_device(&p_dev->dev);
225}
226
360b65b9
DB
227static void pcmcia_release_function(struct kref *ref)
228{
229 struct config_t *c = container_of(ref, struct config_t, ref);
d50dbec3 230 pr_debug("releasing config_t\n");
360b65b9
DB
231 kfree(c);
232}
233
1da177e4
LT
234static void pcmcia_release_dev(struct device *dev)
235{
236 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
44961a03 237 int i;
d50dbec3 238 dev_dbg(dev, "releasing device\n");
dc109497 239 pcmcia_put_socket(p_dev->socket);
44961a03
DB
240 for (i = 0; i < 4; i++)
241 kfree(p_dev->prod_id[i]);
bd65a685 242 kfree(p_dev->devname);
360b65b9 243 kref_put(&p_dev->function_config->ref, pcmcia_release_function);
1da177e4
LT
244 kfree(p_dev);
245}
246
247
9fea84f4 248static int pcmcia_device_probe(struct device *dev)
1da177e4
LT
249{
250 struct pcmcia_device *p_dev;
251 struct pcmcia_driver *p_drv;
f8cfa618 252 struct pcmcia_socket *s;
af2b3b50 253 cistpl_config_t cis_config;
1da177e4
LT
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);
f8cfa618 262 s = p_dev->socket;
1da177e4 263
2e9b981a 264 dev_dbg(dev, "trying to bind to %s\n", p_drv->name);
d9d9ea01 265
360b65b9
DB
266 if ((!p_drv->probe) || (!p_dev->function_config) ||
267 (!try_module_get(p_drv->owner))) {
1da177e4
LT
268 ret = -EINVAL;
269 goto put_dev;
270 }
271
af2b3b50
DB
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) {
7feabb64
DB
276 p_dev->config_base = cis_config.base;
277 p_dev->config_regs = cis_config.rmask[0];
1cc745d1
DB
278 dev_dbg(dev, "base %x, regs %x", p_dev->config_base,
279 p_dev->config_regs);
af2b3b50 280 } else {
ac449d6e
DB
281 dev_printk(KERN_INFO, dev,
282 "pcmcia: could not parse base and rmask0 of CIS\n");
7feabb64
DB
283 p_dev->config_base = 0;
284 p_dev->config_regs = 0;
af2b3b50
DB
285 }
286
f8cfa618 287 ret = p_drv->probe(p_dev);
d9d9ea01 288 if (ret) {
d50dbec3 289 dev_dbg(dev, "binding to %s failed with %d\n",
2e9b981a 290 p_drv->name, ret);
82d56e6d 291 goto put_module;
d9d9ea01 292 }
2e9b981a 293 dev_dbg(dev, "%s bound: Vpp %d.%d, idx %x, IRQ %d", p_drv->name,
1cc745d1
DB
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]);
82d56e6d 298
00ce99ff 299 mutex_lock(&s->ops_mutex);
ce3f9d71 300 if ((s->pcmcia_pfc) &&
82d56e6d 301 (p_dev->socket->device_count == 1) && (p_dev->device_no == 0))
aa584ca4 302 pcmcia_parse_uevents(s, PCMCIA_UEVENT_REQUERY);
00ce99ff 303 mutex_unlock(&s->ops_mutex);
f8cfa618 304
cec5eb7b 305put_module:
1da177e4
LT
306 if (ret)
307 module_put(p_drv->owner);
cec5eb7b 308put_dev:
f8cfa618 309 if (ret)
1da177e4 310 put_device(dev);
9fea84f4 311 return ret;
1da177e4
LT
312}
313
314
d6ff5a85
DB
315/*
316 * Removes a PCMCIA card from the device tree and socket list.
317 */
318static void pcmcia_card_remove(struct pcmcia_socket *s, struct pcmcia_device *leftover)
319{
320 struct pcmcia_device *p_dev;
321 struct pcmcia_device *tmp;
d6ff5a85 322
d50dbec3 323 dev_dbg(leftover ? &leftover->dev : &s->dev,
ac449d6e
DB
324 "pcmcia_card_remove(%d) %s\n", s->sock,
325 leftover ? leftover->devname : "");
d6ff5a85 326
00ce99ff 327 mutex_lock(&s->ops_mutex);
d6ff5a85
DB
328 if (!leftover)
329 s->device_count = 0;
330 else
331 s->device_count = 1;
00ce99ff 332 mutex_unlock(&s->ops_mutex);
d6ff5a85
DB
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
00ce99ff 339 mutex_lock(&s->ops_mutex);
d6ff5a85 340 list_del(&p_dev->socket_device_list);
00ce99ff 341 mutex_unlock(&s->ops_mutex);
d6ff5a85 342
d50dbec3 343 dev_dbg(&p_dev->dev, "unregistering device\n");
d6ff5a85
DB
344 device_unregister(&p_dev->dev);
345 }
346
347 return;
348}
349
9fea84f4 350static int pcmcia_device_remove(struct device *dev)
1da177e4
LT
351{
352 struct pcmcia_device *p_dev;
353 struct pcmcia_driver *p_drv;
cc3b4866 354 int i;
1da177e4 355
1da177e4
LT
356 p_dev = to_pcmcia_dev(dev);
357 p_drv = to_pcmcia_drv(dev->driver);
d6ff5a85 358
d50dbec3 359 dev_dbg(dev, "removing device\n");
d9d9ea01 360
d6ff5a85
DB
361 /* If we're removing the primary module driving a
362 * pseudo multi-function card, we need to unbind
363 * all devices
364 */
ce3f9d71 365 if ((p_dev->socket->pcmcia_pfc) &&
e6e4f397 366 (p_dev->socket->device_count > 0) &&
d6ff5a85
DB
367 (p_dev->device_no == 0))
368 pcmcia_card_remove(p_dev->socket, p_dev);
369
370 /* detach the "instance" */
f3990715
DB
371 if (!p_drv)
372 return 0;
1da177e4 373
f3990715 374 if (p_drv->remove)
9fea84f4 375 p_drv->remove(p_dev);
cc3b4866 376
f3990715 377 /* check for proper unloading */
e2d40963 378 if (p_dev->_irq || p_dev->_io || p_dev->_locked)
ac449d6e
DB
379 dev_printk(KERN_INFO, dev,
380 "pcmcia: driver %s did not release config properly\n",
2e9b981a 381 p_drv->name);
cc3b4866 382
f3990715 383 for (i = 0; i < MAX_WIN; i++)
e2d40963 384 if (p_dev->_win & CLIENT_WIN_REQ(i))
ac449d6e
DB
385 dev_printk(KERN_INFO, dev,
386 "pcmcia: driver %s did not release window properly\n",
2e9b981a 387 p_drv->name);
cc3b4866 388
f3990715
DB
389 /* references from pcmcia_probe_device */
390 pcmcia_put_dev(p_dev);
391 module_put(p_drv->owner);
1da177e4
LT
392
393 return 0;
394}
395
396
1da177e4
LT
397/*
398 * pcmcia_device_query -- determine information about a pcmcia device
399 */
400static int pcmcia_device_query(struct pcmcia_device *p_dev)
401{
402 cistpl_manfid_t manf_id;
403 cistpl_funcid_t func_id;
76fa82fb 404 cistpl_vers_1_t *vers1;
1da177e4
LT
405 unsigned int i;
406
76fa82fb
IM
407 vers1 = kmalloc(sizeof(*vers1), GFP_KERNEL);
408 if (!vers1)
409 return -ENOMEM;
410
84897fc0 411 if (!pccard_read_tuple(p_dev->socket, BIND_FN_ALL,
1da177e4 412 CISTPL_MANFID, &manf_id)) {
af461fc1 413 mutex_lock(&p_dev->socket->ops_mutex);
1da177e4
LT
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;
af461fc1 418 mutex_unlock(&p_dev->socket->ops_mutex);
1da177e4
LT
419 }
420
421 if (!pccard_read_tuple(p_dev->socket, p_dev->func,
422 CISTPL_FUNCID, &func_id)) {
af461fc1 423 mutex_lock(&p_dev->socket->ops_mutex);
1da177e4
LT
424 p_dev->func_id = func_id.func;
425 p_dev->has_func_id = 1;
af461fc1 426 mutex_unlock(&p_dev->socket->ops_mutex);
1da177e4
LT
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) */
76fa82fb
IM
431 cistpl_device_geo_t *devgeo;
432
433 devgeo = kmalloc(sizeof(*devgeo), GFP_KERNEL);
434 if (!devgeo) {
435 kfree(vers1);
436 return -ENOMEM;
437 }
1da177e4 438 if (!pccard_read_tuple(p_dev->socket, p_dev->func,
76fa82fb 439 CISTPL_DEVICE_GEO, devgeo)) {
d50dbec3 440 dev_dbg(&p_dev->dev,
ac449d6e
DB
441 "mem device geometry probably means "
442 "FUNCID_MEMORY\n");
af461fc1 443 mutex_lock(&p_dev->socket->ops_mutex);
1da177e4
LT
444 p_dev->func_id = CISTPL_FUNCID_MEMORY;
445 p_dev->has_func_id = 1;
af461fc1 446 mutex_unlock(&p_dev->socket->ops_mutex);
1da177e4 447 }
76fa82fb 448 kfree(devgeo);
1da177e4
LT
449 }
450
84897fc0 451 if (!pccard_read_tuple(p_dev->socket, BIND_FN_ALL, CISTPL_VERS_1,
76fa82fb 452 vers1)) {
af461fc1 453 mutex_lock(&p_dev->socket->ops_mutex);
c5e09528 454 for (i = 0; i < min_t(unsigned int, 4, vers1->ns); i++) {
1da177e4
LT
455 char *tmp;
456 unsigned int length;
44961a03 457 char *new;
1da177e4 458
76fa82fb 459 tmp = vers1->str + vers1->ofs[i];
1da177e4
LT
460
461 length = strlen(tmp) + 1;
6e3d4f25 462 if ((length < 2) || (length > 255))
1da177e4
LT
463 continue;
464
44961a03
DB
465 new = kmalloc(sizeof(char) * length, GFP_KERNEL);
466 if (!new)
1da177e4
LT
467 continue;
468
44961a03
DB
469 new = strncpy(new, tmp, length);
470
471 tmp = p_dev->prod_id[i];
472 p_dev->prod_id[i] = new;
473 kfree(tmp);
1da177e4 474 }
af461fc1 475 mutex_unlock(&p_dev->socket->ops_mutex);
1da177e4
LT
476 }
477
76fa82fb 478 kfree(vers1);
1da177e4
LT
479 return 0;
480}
481
482
5716d415
DB
483static struct pcmcia_device *pcmcia_device_add(struct pcmcia_socket *s,
484 unsigned int function)
1da177e4 485{
360b65b9 486 struct pcmcia_device *p_dev, *tmp_dev;
44961a03 487 int i;
1da177e4 488
dc109497 489 s = pcmcia_get_socket(s);
1da177e4
LT
490 if (!s)
491 return NULL;
492
d50dbec3 493 pr_debug("adding device to %d, function %d\n", s->sock, function);
d9d9ea01 494
8084b372 495 p_dev = kzalloc(sizeof(struct pcmcia_device), GFP_KERNEL);
1da177e4
LT
496 if (!p_dev)
497 goto err_put;
1da177e4 498
00ce99ff 499 mutex_lock(&s->ops_mutex);
1da177e4 500 p_dev->device_no = (s->device_count++);
00ce99ff 501 mutex_unlock(&s->ops_mutex);
e6e4f397 502
7d7ba8d3
DB
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)
e6e4f397
DB
509 goto err_free;
510
511 p_dev->socket = s;
1da177e4
LT
512 p_dev->func = function;
513
514 p_dev->dev.bus = &pcmcia_bus_type;
87373318 515 p_dev->dev.parent = s->dev.parent;
1da177e4 516 p_dev->dev.release = pcmcia_release_dev;
43d9f7fd
JB
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;
25096986
KS
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));
bd65a685
BG
524 if (!p_dev->devname)
525 goto err_free;
d50dbec3 526 dev_dbg(&p_dev->dev, "devname is %s\n", p_dev->devname);
1da177e4 527
00ce99ff 528 mutex_lock(&s->ops_mutex);
360b65b9
DB
529
530 /*
531 * p_dev->function_config must be the same for all card functions.
a60f22c4
DB
532 * Note that this is serialized by ops_mutex, so that only one
533 * such struct will be created.
360b65b9 534 */
9fea84f4
DB
535 list_for_each_entry(tmp_dev, &s->devices_list, socket_device_list)
536 if (p_dev->func == tmp_dev->func) {
360b65b9 537 p_dev->function_config = tmp_dev->function_config;
3e879f61 538 p_dev->irq = tmp_dev->irq;
360b65b9
DB
539 kref_get(&p_dev->function_config->ref);
540 }
541
542 /* Add to the list in pcmcia_bus_socket */
6171b88b 543 list_add(&p_dev->socket_device_list, &s->devices_list);
360b65b9 544
6f0f38c4
DB
545 if (pcmcia_setup_irq(p_dev))
546 dev_warn(&p_dev->dev,
547 "IRQ setup failed -- device might not work\n");
1da177e4 548
360b65b9 549 if (!p_dev->function_config) {
2ce4905e 550 config_t *c;
d50dbec3 551 dev_dbg(&p_dev->dev, "creating config_t\n");
2ce4905e
DB
552 c = kzalloc(sizeof(struct config_t), GFP_KERNEL);
553 if (!c) {
6f0f38c4 554 mutex_unlock(&s->ops_mutex);
360b65b9 555 goto err_unreg;
6f0f38c4 556 }
2ce4905e
DB
557 p_dev->function_config = c;
558 kref_init(&c->ref);
559 for (i = 0; i < MAX_IO_WIN; i++) {
ad0c7be2 560 c->io[i].name = p_dev->devname;
2ce4905e
DB
561 c->io[i].flags = IORESOURCE_IO;
562 }
0ca724d3 563 for (i = 0; i< MAX_WIN; i++) {
ad0c7be2 564 c->mem[i].name = p_dev->devname;
0ca724d3
DB
565 c->mem[i].flags = IORESOURCE_MEM;
566 }
360b65b9 567 }
2ce4905e
DB
568 for (i = 0; i < MAX_IO_WIN; i++)
569 p_dev->resource[i] = &p_dev->function_config->io[i];
0ca724d3
DB
570 for (; i < (MAX_IO_WIN + MAX_WIN); i++)
571 p_dev->resource[i] = &p_dev->function_config->mem[i-MAX_IO_WIN];
2ce4905e 572
6f0f38c4 573 mutex_unlock(&s->ops_mutex);
360b65b9 574
ac449d6e 575 dev_printk(KERN_NOTICE, &p_dev->dev,
6f0f38c4 576 "pcmcia: registering new device %s (IRQ: %d)\n",
eb14120f 577 p_dev->devname, p_dev->irq);
807277cb 578
1ad275e3
DB
579 pcmcia_device_query(p_dev);
580
360b65b9
DB
581 if (device_register(&p_dev->dev))
582 goto err_unreg;
1da177e4 583
1da177e4
LT
584 return p_dev;
585
360b65b9 586 err_unreg:
00ce99ff 587 mutex_lock(&s->ops_mutex);
360b65b9 588 list_del(&p_dev->socket_device_list);
00ce99ff 589 mutex_unlock(&s->ops_mutex);
360b65b9 590
1da177e4 591 err_free:
00ce99ff 592 mutex_lock(&s->ops_mutex);
e6e4f397 593 s->device_count--;
00ce99ff 594 mutex_unlock(&s->ops_mutex);
e6e4f397 595
44961a03
DB
596 for (i = 0; i < 4; i++)
597 kfree(p_dev->prod_id[i]);
bd65a685 598 kfree(p_dev->devname);
1da177e4 599 kfree(p_dev);
1da177e4 600 err_put:
dc109497 601 pcmcia_put_socket(s);
1da177e4
LT
602
603 return NULL;
604}
605
606
607static int pcmcia_card_add(struct pcmcia_socket *s)
608{
1da177e4 609 cistpl_longlink_mfc_t mfc;
c5081d5f 610 unsigned int no_funcs, i, no_chains;
cfe5d809 611 int ret = -EAGAIN;
1da177e4 612
cfe5d809 613 mutex_lock(&s->ops_mutex);
d9d9ea01 614 if (!(s->resource_setup_done)) {
d50dbec3 615 dev_dbg(&s->dev,
ac449d6e 616 "no resources available, delaying card_add\n");
cfe5d809 617 mutex_unlock(&s->ops_mutex);
1da177e4 618 return -EAGAIN; /* try again, but later... */
d9d9ea01 619 }
1da177e4 620
d9d9ea01 621 if (pcmcia_validate_mem(s)) {
d50dbec3 622 dev_dbg(&s->dev, "validating mem resources failed, "
d9d9ea01 623 "delaying card_add\n");
cfe5d809 624 mutex_unlock(&s->ops_mutex);
de75914e 625 return -EAGAIN; /* try again, but later... */
d9d9ea01 626 }
cfe5d809 627 mutex_unlock(&s->ops_mutex);
de75914e 628
84897fc0 629 ret = pccard_validate_cis(s, &no_chains);
c5081d5f 630 if (ret || !no_chains) {
d50dbec3 631 dev_dbg(&s->dev, "invalid CIS or invalid resources\n");
1da177e4
LT
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;
1d2c9042 639 s->functions = no_funcs;
1da177e4 640
9fea84f4 641 for (i = 0; i < no_funcs; i++)
dc109497 642 pcmcia_device_add(s, i);
1da177e4 643
9fea84f4 644 return ret;
1da177e4
LT
645}
646
647
af461fc1 648static int pcmcia_requery_callback(struct device *dev, void * _data)
ff1fa9ef 649{
e2f0b534 650 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
d9d9ea01 651 if (!p_dev->dev.driver) {
d50dbec3 652 dev_dbg(dev, "update device information\n");
e2f0b534 653 pcmcia_device_query(p_dev);
d9d9ea01 654 }
e2f0b534
DB
655
656 return 0;
657}
658
aa584ca4 659
af461fc1 660static void pcmcia_requery(struct pcmcia_socket *s)
e2f0b534 661{
04de0816 662 int has_pfc;
4ae1cbf1 663
af461fc1
DB
664 if (s->functions == 0) {
665 pcmcia_card_add(s);
666 return;
e2f0b534
DB
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. */
af461fc1
DB
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;
b1095afe
DB
688 if (old_funcs != new_funcs) {
689 /* we need to re-start */
af461fc1 690 pcmcia_card_remove(s, NULL);
b83156b5 691 s->functions = 0;
af461fc1 692 pcmcia_card_add(s);
af461fc1
DB
693 }
694 }
e2f0b534 695
aa584ca4
DB
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);
ce3f9d71 700 has_pfc = s->pcmcia_pfc;
aa584ca4
DB
701 mutex_unlock(&s->ops_mutex);
702 if (has_pfc)
703 pcmcia_device_add(s, 0);
704
e2f0b534
DB
705 /* we re-scan all devices, not just the ones connected to this
706 * socket. This does not matter, though. */
af461fc1
DB
707 if (bus_rescan_devices(&pcmcia_bus_type))
708 dev_warn(&s->dev, "rescanning the bus failed\n");
ff1fa9ef 709}
1ad275e3 710
af461fc1 711
1d2c9042
DB
712#ifdef CONFIG_PCMCIA_LOAD_CIS
713
714/**
715 * pcmcia_load_firmware - load CIS from userspace if device-provided is broken
78187865
RD
716 * @dev: the pcmcia device which needs a CIS override
717 * @filename: requested filename in /lib/firmware/
1d2c9042
DB
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 */
723static int pcmcia_load_firmware(struct pcmcia_device *dev, char * filename)
724{
725 struct pcmcia_socket *s = dev->socket;
726 const struct firmware *fw;
1d2c9042 727 int ret = -ENOMEM;
b1095afe
DB
728 cistpl_longlink_mfc_t mfc;
729 int old_funcs, new_funcs = 1;
1d2c9042
DB
730
731 if (!filename)
732 return -EINVAL;
733
d50dbec3 734 dev_dbg(&dev->dev, "trying to load CIS file %s\n", filename);
1d2c9042 735
ed62acec 736 if (request_firmware(&fw, filename, &dev->dev) == 0) {
d9d9ea01
DB
737 if (fw->size >= CISTPL_MAX_CIS_SIZE) {
738 ret = -EINVAL;
ac449d6e
DB
739 dev_printk(KERN_ERR, &dev->dev,
740 "pcmcia: CIS override is too big\n");
1d2c9042 741 goto release;
d9d9ea01 742 }
1d2c9042 743
53efec95 744 if (!pcmcia_replace_cis(s, fw->data, fw->size))
1d2c9042 745 ret = 0;
d9d9ea01 746 else {
ac449d6e
DB
747 dev_printk(KERN_ERR, &dev->dev,
748 "pcmcia: CIS override failed\n");
d9d9ea01
DB
749 goto release;
750 }
751
b1095afe
DB
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;
1d2c9042
DB
760
761 /* update information */
762 pcmcia_device_query(dev);
763
aa584ca4
DB
764 /* requery (as number of functions might have changed) */
765 pcmcia_parse_uevents(s, PCMCIA_UEVENT_REQUERY);
1d2c9042
DB
766 }
767 release:
768 release_firmware(fw);
769
9fea84f4 770 return ret;
1d2c9042
DB
771}
772
773#else /* !CONFIG_PCMCIA_LOAD_CIS */
774
775static inline int pcmcia_load_firmware(struct pcmcia_device *dev, char * filename)
776{
777 return -ENODEV;
778}
779
780#endif
781
782
1ad275e3 783static inline int pcmcia_devmatch(struct pcmcia_device *dev,
e9fb13bf 784 const struct pcmcia_device_id *did)
1ad275e3
DB
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) {
83bf6f11 830 dev_dbg(&dev->dev, "this is a pseudo-multi-function device\n");
aa584ca4 831 mutex_lock(&dev->socket->ops_mutex);
ce3f9d71 832 dev->socket->pcmcia_pfc = 1;
aa584ca4 833 mutex_unlock(&dev->socket->ops_mutex);
83bf6f11
AK
834 if (dev->device_no != did->device_no)
835 return 0;
1ad275e3
DB
836 }
837
838 if (did->match_flags & PCMCIA_DEV_ID_MATCH_FUNC_ID) {
94a819f8
DB
839 int ret;
840
1ad275e3
DB
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 */
ce3f9d71 846 if (dev->socket->pcmcia_pfc)
1ad275e3
DB
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 */
94a819f8
DB
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");
1ad275e3 862 return 0;
94a819f8 863 }
1ad275e3
DB
864 }
865
ea7b3882 866 if (did->match_flags & PCMCIA_DEV_ID_MATCH_FAKE_CIS) {
d50dbec3 867 dev_dbg(&dev->dev, "device needs a fake CIS\n");
daa9517d 868 if (!dev->socket->fake_cis)
b1095afe
DB
869 if (pcmcia_load_firmware(dev, did->cisfile))
870 return 0;
ea7b3882
DB
871 }
872
f602ff7e
DB
873 if (did->match_flags & PCMCIA_DEV_ID_MATCH_ANONYMOUS) {
874 int i;
9fea84f4 875 for (i = 0; i < 4; i++)
f602ff7e
DB
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
1ad275e3
DB
882 return 1;
883}
884
885
9fea84f4
DB
886static 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);
e9fb13bf 890 const struct pcmcia_device_id *did = p_drv->id_table;
6179b556
BW
891 struct pcmcia_dynid *dynid;
892
893 /* match dynamic devices first */
3f565232 894 mutex_lock(&p_drv->dynids.lock);
6179b556 895 list_for_each_entry(dynid, &p_drv->dynids.list, node) {
d50dbec3 896 dev_dbg(dev, "trying to match to %s\n", drv->name);
6179b556 897 if (pcmcia_devmatch(p_dev, &dynid->id)) {
d50dbec3 898 dev_dbg(dev, "matched to %s\n", drv->name);
3f565232 899 mutex_unlock(&p_drv->dynids.lock);
6179b556
BW
900 return 1;
901 }
902 }
3f565232 903 mutex_unlock(&p_drv->dynids.lock);
1da177e4 904
1ad275e3 905 while (did && did->match_flags) {
d50dbec3 906 dev_dbg(dev, "trying to match to %s\n", drv->name);
d9d9ea01 907 if (pcmcia_devmatch(p_dev, did)) {
d50dbec3 908 dev_dbg(dev, "matched to %s\n", drv->name);
1ad275e3 909 return 1;
d9d9ea01 910 }
1ad275e3
DB
911 did++;
912 }
913
1da177e4
LT
914 return 0;
915}
916
840c2ac5
DB
917#ifdef CONFIG_HOTPLUG
918
7eff2e7a 919static int pcmcia_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
840c2ac5
DB
920{
921 struct pcmcia_device *p_dev;
7eff2e7a 922 int i;
840c2ac5
DB
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 */
9fea84f4 931 for (i = 0; i < 4; i++) {
840c2ac5
DB
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
7eff2e7a 937 if (add_uevent_var(env, "SOCKET_NO=%u", p_dev->socket->sock))
840c2ac5
DB
938 return -ENOMEM;
939
7eff2e7a 940 if (add_uevent_var(env, "DEVICE_NO=%02X", p_dev->device_no))
840c2ac5
DB
941 return -ENOMEM;
942
7eff2e7a 943 if (add_uevent_var(env, "MODALIAS=pcmcia:m%04Xc%04Xf%02Xfn%02Xpfn%02X"
312c004d
KS
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]))
840c2ac5
DB
954 return -ENOMEM;
955
840c2ac5
DB
956 return 0;
957}
958
959#else
960
7eff2e7a 961static int pcmcia_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
840c2ac5
DB
962{
963 return -ENODEV;
964}
965
966#endif
967
3f8df781
AS
968/************************ runtime PM support ***************************/
969
970static int pcmcia_dev_suspend(struct device *dev, pm_message_t state);
971static int pcmcia_dev_resume(struct device *dev);
972
973static int runtime_suspend(struct device *dev)
974{
975 int rc;
976
8e9394ce 977 device_lock(dev);
3f8df781 978 rc = pcmcia_dev_suspend(dev, PMSG_SUSPEND);
8e9394ce 979 device_unlock(dev);
3f8df781
AS
980 return rc;
981}
982
933a838a 983static int runtime_resume(struct device *dev)
3f8df781
AS
984{
985 int rc;
986
8e9394ce 987 device_lock(dev);
3f8df781 988 rc = pcmcia_dev_resume(dev);
8e9394ce 989 device_unlock(dev);
933a838a 990 return rc;
3f8df781
AS
991}
992
1da177e4
LT
993/************************ per-device sysfs output ***************************/
994
995#define pcmcia_device_attr(field, test, format) \
e404e274 996static ssize_t field##_show (struct device *dev, struct device_attribute *attr, char *buf) \
1da177e4
LT
997{ \
998 struct pcmcia_device *p_dev = to_pcmcia_dev(dev); \
9fea84f4 999 return p_dev->test ? sprintf(buf, format, p_dev->field) : -ENODEV; \
1da177e4
LT
1000}
1001
1002#define pcmcia_device_stringattr(name, field) \
e404e274 1003static ssize_t name##_show (struct device *dev, struct device_attribute *attr, char *buf) \
1da177e4
LT
1004{ \
1005 struct pcmcia_device *p_dev = to_pcmcia_dev(dev); \
9fea84f4 1006 return p_dev->field ? sprintf(buf, "%s\n", p_dev->field) : -ENODEV; \
1da177e4
LT
1007}
1008
1009pcmcia_device_attr(func, socket, "0x%02x\n");
1010pcmcia_device_attr(func_id, has_func_id, "0x%02x\n");
1011pcmcia_device_attr(manf_id, has_manf_id, "0x%04x\n");
1012pcmcia_device_attr(card_id, has_card_id, "0x%04x\n");
1013pcmcia_device_stringattr(prod_id1, prod_id[0]);
1014pcmcia_device_stringattr(prod_id2, prod_id[1]);
1015pcmcia_device_stringattr(prod_id3, prod_id[2]);
1016pcmcia_device_stringattr(prod_id4, prod_id[3]);
1017
8f677ea0
DB
1018static 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}
db1019ca
DB
1030
1031static 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
f6fbe01a 1035 if (p_dev->suspended)
db1019ca
DB
1036 return sprintf(buf, "off\n");
1037 else
1038 return sprintf(buf, "on\n");
1039}
1040
1041static 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
9fea84f4
DB
1047 if (!count)
1048 return -EINVAL;
db1019ca 1049
f6fbe01a 1050 if ((!p_dev->suspended) && !strncmp(buf, "off", 3))
3f8df781 1051 ret = runtime_suspend(dev);
f6fbe01a 1052 else if (p_dev->suspended && !strncmp(buf, "on", 2))
933a838a 1053 ret = runtime_resume(dev);
db1019ca
DB
1054
1055 return ret ? ret : count;
1056}
1057
1058
3704511b 1059static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
3248ff43
DB
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 */
9fea84f4 1066 for (i = 0; i < 4; i++) {
3248ff43
DB
1067 if (!p_dev->prod_id[i])
1068 continue;
9fea84f4
DB
1069 hash[i] = crc32(0, p_dev->prod_id[i],
1070 strlen(p_dev->prod_id[i]));
3248ff43
DB
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}
a5b55778 1080
3248ff43
DB
1081static ssize_t pcmcia_store_allow_func_id_match(struct device *dev,
1082 struct device_attribute *attr, const char *buf, size_t count)
a5b55778
DB
1083{
1084 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
db1019ca
DB
1085
1086 if (!count)
1087 return -EINVAL;
a5b55778 1088
94a819f8 1089 mutex_lock(&p_dev->socket->ops_mutex);
a5b55778 1090 p_dev->allow_func_id_match = 1;
94a819f8 1091 mutex_unlock(&p_dev->socket->ops_mutex);
af461fc1 1092 pcmcia_parse_uevents(p_dev->socket, PCMCIA_UEVENT_REQUERY);
a5b55778
DB
1093
1094 return count;
1095}
1096
1da177e4
LT
1097static struct device_attribute pcmcia_dev_attrs[] = {
1098 __ATTR(function, 0444, func_show, NULL),
db1019ca 1099 __ATTR(pm_state, 0644, pcmcia_show_pm_state, pcmcia_store_pm_state),
8f677ea0 1100 __ATTR(resources, 0444, pcmcia_show_resources, NULL),
1da177e4
LT
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),
3248ff43 1108 __ATTR_RO(modalias),
a5b55778 1109 __ATTR(allow_func_id_match, 0200, NULL, pcmcia_store_allow_func_id_match),
1da177e4
LT
1110 __ATTR_NULL,
1111};
1112
8e9e793d
DB
1113/* PM support, also needed for reset */
1114
9fea84f4 1115static int pcmcia_dev_suspend(struct device *dev, pm_message_t state)
8e9e793d
DB
1116{
1117 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1118 struct pcmcia_driver *p_drv = NULL;
f6fbe01a 1119 int ret = 0;
8e9e793d 1120
94a819f8
DB
1121 mutex_lock(&p_dev->socket->ops_mutex);
1122 if (p_dev->suspended) {
1123 mutex_unlock(&p_dev->socket->ops_mutex);
d6b4fa6d 1124 return 0;
94a819f8
DB
1125 }
1126 p_dev->suspended = 1;
1127 mutex_unlock(&p_dev->socket->ops_mutex);
d6b4fa6d 1128
d50dbec3 1129 dev_dbg(dev, "suspending\n");
d9d9ea01 1130
8e9e793d
DB
1131 if (dev->driver)
1132 p_drv = to_pcmcia_drv(dev->driver);
1133
e2d40963
DB
1134 if (!p_drv)
1135 goto out;
1136
1137 if (p_drv->suspend) {
8661bb5b 1138 ret = p_drv->suspend(p_dev);
d9d9ea01 1139 if (ret) {
ac449d6e
DB
1140 dev_printk(KERN_ERR, dev,
1141 "pcmcia: device %s (driver %s) did "
1142 "not want to go to sleep (%d)\n",
2e9b981a 1143 p_dev->devname, p_drv->name, ret);
94a819f8
DB
1144 mutex_lock(&p_dev->socket->ops_mutex);
1145 p_dev->suspended = 0;
1146 mutex_unlock(&p_dev->socket->ops_mutex);
f6fbe01a 1147 goto out;
d9d9ea01 1148 }
8661bb5b 1149 }
8e9e793d 1150
d9d9ea01 1151 if (p_dev->device_no == p_dev->func) {
d50dbec3 1152 dev_dbg(dev, "releasing configuration\n");
e2d40963 1153 pcmcia_release_configuration(p_dev);
d9d9ea01 1154 }
e2d40963 1155
f6fbe01a 1156 out:
f6fbe01a 1157 return ret;
8e9e793d
DB
1158}
1159
1160
9fea84f4 1161static int pcmcia_dev_resume(struct device *dev)
8e9e793d
DB
1162{
1163 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
9fea84f4 1164 struct pcmcia_driver *p_drv = NULL;
f6fbe01a 1165 int ret = 0;
8e9e793d 1166
94a819f8
DB
1167 mutex_lock(&p_dev->socket->ops_mutex);
1168 if (!p_dev->suspended) {
1169 mutex_unlock(&p_dev->socket->ops_mutex);
d6b4fa6d 1170 return 0;
94a819f8
DB
1171 }
1172 p_dev->suspended = 0;
1173 mutex_unlock(&p_dev->socket->ops_mutex);
d6b4fa6d 1174
d50dbec3 1175 dev_dbg(dev, "resuming\n");
d9d9ea01 1176
8e9e793d
DB
1177 if (dev->driver)
1178 p_drv = to_pcmcia_drv(dev->driver);
1179
e2d40963
DB
1180 if (!p_drv)
1181 goto out;
1182
1183 if (p_dev->device_no == p_dev->func) {
d50dbec3 1184 dev_dbg(dev, "requesting configuration\n");
1ac71e5a 1185 ret = pcmcia_enable_device(p_dev);
e2d40963
DB
1186 if (ret)
1187 goto out;
8661bb5b 1188 }
8e9e793d 1189
e2d40963
DB
1190 if (p_drv->resume)
1191 ret = p_drv->resume(p_dev);
1192
f6fbe01a 1193 out:
f6fbe01a 1194 return ret;
8e9e793d
DB
1195}
1196
1197
1198static 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
3f8df781 1203 if (p_dev->socket != skt || p_dev->suspended)
8e9e793d
DB
1204 return 0;
1205
3f8df781 1206 return runtime_suspend(dev);
8e9e793d
DB
1207}
1208
1209static 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
3f8df781 1214 if (p_dev->socket != skt || !p_dev->suspended)
8e9e793d
DB
1215 return 0;
1216
3f8df781 1217 runtime_resume(dev);
8e9e793d
DB
1218
1219 return 0;
1220}
1221
1222static int pcmcia_bus_resume(struct pcmcia_socket *skt)
1223{
d50dbec3 1224 dev_dbg(&skt->dev, "resuming socket %d\n", skt->sock);
8e9e793d
DB
1225 bus_for_each_dev(&pcmcia_bus_type, NULL, skt, pcmcia_bus_resume_callback);
1226 return 0;
1227}
1228
1229static int pcmcia_bus_suspend(struct pcmcia_socket *skt)
1230{
d50dbec3 1231 dev_dbg(&skt->dev, "suspending socket %d\n", skt->sock);
8e9e793d
DB
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
7b24e798
DB
1240static int pcmcia_bus_remove(struct pcmcia_socket *skt)
1241{
1242 atomic_set(&skt->present, 0);
1243 pcmcia_card_remove(skt, NULL);
1da177e4 1244
7b24e798
DB
1245 mutex_lock(&skt->ops_mutex);
1246 destroy_cis_cache(skt);
1247 pcmcia_cleanup_irq(skt);
1248 mutex_unlock(&skt->ops_mutex);
1da177e4 1249
7b24e798
DB
1250 return 0;
1251}
9fea84f4 1252
7b24e798
DB
1253static int pcmcia_bus_add(struct pcmcia_socket *skt)
1254{
1255 atomic_set(&skt->present, 1);
1da177e4 1256
7b24e798 1257 mutex_lock(&skt->ops_mutex);
ce3f9d71 1258 skt->pcmcia_pfc = 0;
7b24e798
DB
1259 destroy_cis_cache(skt); /* to be on the safe side... */
1260 mutex_unlock(&skt->ops_mutex);
1da177e4 1261
7b24e798 1262 pcmcia_card_add(skt);
1da177e4 1263
7b24e798
DB
1264 return 0;
1265}
1617406a 1266
7b24e798
DB
1267static 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 }
1da177e4 1273
7b24e798 1274 dev_dbg(&skt->dev, "cis mismatch - different card\n");
f8cfa618 1275
7b24e798
DB
1276 /* first, remove the card */
1277 pcmcia_bus_remove(skt);
88b060d6 1278
7b24e798
DB
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);
1da177e4 1285
7b24e798
DB
1286 /* now, add the new card */
1287 pcmcia_bus_add(skt);
1288 return 0;
1289}
dc109497 1290
1da177e4 1291
04de0816
DB
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 */
9fea84f4 1299struct pcmcia_device *pcmcia_dev_present(struct pcmcia_device *_p_dev)
9940ec36
DB
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
04de0816
DB
1308 if (atomic_read(&p_dev->socket->present) != 0)
1309 ret = p_dev;
9940ec36 1310
9940ec36
DB
1311 pcmcia_put_dev(p_dev);
1312 return ret;
1313}
1314EXPORT_SYMBOL(pcmcia_dev_present);
1315
1316
90c6cdd1
DB
1317static struct pcmcia_callback pcmcia_bus_callback = {
1318 .owner = THIS_MODULE,
7b24e798
DB
1319 .add = pcmcia_bus_add,
1320 .remove = pcmcia_bus_remove,
af461fc1 1321 .requery = pcmcia_requery,
6e7b51a7 1322 .validate = pccard_validate_cis,
8e9e793d 1323 .suspend = pcmcia_bus_suspend,
7b24e798 1324 .early_resume = pcmcia_bus_early_resume,
8e9e793d 1325 .resume = pcmcia_bus_resume,
90c6cdd1
DB
1326};
1327
87373318 1328static int __devinit pcmcia_bus_add_socket(struct device *dev,
d8539d81 1329 struct class_interface *class_intf)
1da177e4 1330{
87373318 1331 struct pcmcia_socket *socket = dev_get_drvdata(dev);
1da177e4
LT
1332 int ret;
1333
dc109497
DB
1334 socket = pcmcia_get_socket(socket);
1335 if (!socket) {
ac449d6e
DB
1336 dev_printk(KERN_ERR, dev,
1337 "PCMCIA obtaining reference to socket failed\n");
1da177e4
LT
1338 return -ENODEV;
1339 }
1340
6e7b51a7
DB
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
dc109497 1348 INIT_LIST_HEAD(&socket->devices_list);
ce3f9d71 1349 socket->pcmcia_pfc = 0;
dc109497 1350 socket->device_count = 0;
e4f1ac21 1351 atomic_set(&socket->present, 0);
1da177e4 1352
90c6cdd1 1353 ret = pccard_register_pcmcia(socket, &pcmcia_bus_callback);
1da177e4 1354 if (ret) {
ac449d6e 1355 dev_printk(KERN_ERR, dev, "PCMCIA registration failed\n");
dc109497 1356 pcmcia_put_socket(socket);
9fea84f4 1357 return ret;
1da177e4
LT
1358 }
1359
1360 return 0;
1361}
1362
87373318 1363static void pcmcia_bus_remove_socket(struct device *dev,
d8539d81 1364 struct class_interface *class_intf)
1da177e4 1365{
87373318 1366 struct pcmcia_socket *socket = dev_get_drvdata(dev);
1da177e4 1367
dc109497 1368 if (!socket)
1da177e4
LT
1369 return;
1370
1371 pccard_register_pcmcia(socket, NULL);
1372
dfbc9e9d 1373 /* unregister any unbound devices */
8e4d9dcb 1374 mutex_lock(&socket->skt_mutex);
dfbc9e9d 1375 pcmcia_card_remove(socket, NULL);
180c33ee 1376 release_cis_mem(socket);
8e4d9dcb 1377 mutex_unlock(&socket->skt_mutex);
dfbc9e9d 1378
6e7b51a7
DB
1379 sysfs_remove_bin_file(&dev->kobj, &pccard_cis_attr);
1380
dc109497 1381 pcmcia_put_socket(socket);
1da177e4
LT
1382
1383 return;
1384}
1385
1386
1387/* the pcmcia_bus_interface is used to handle pcmcia socket devices */
ed49f5d0 1388static struct class_interface pcmcia_bus_interface __refdata = {
1da177e4 1389 .class = &pcmcia_socket_class,
87373318
GKH
1390 .add_dev = &pcmcia_bus_add_socket,
1391 .remove_dev = &pcmcia_bus_remove_socket,
1da177e4
LT
1392};
1393
1394
e7a480d2 1395struct bus_type pcmcia_bus_type = {
1da177e4 1396 .name = "pcmcia",
312c004d 1397 .uevent = pcmcia_bus_uevent,
1da177e4
LT
1398 .match = pcmcia_bus_match,
1399 .dev_attrs = pcmcia_dev_attrs,
1d0baa3a
RK
1400 .probe = pcmcia_device_probe,
1401 .remove = pcmcia_device_remove,
8e9e793d
DB
1402 .suspend = pcmcia_dev_suspend,
1403 .resume = pcmcia_dev_resume,
1da177e4 1404};
1da177e4
LT
1405
1406
1407static int __init init_pcmcia_bus(void)
1408{
ace7d477
RD
1409 int ret;
1410
ace7d477
RD
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 }
1da177e4 1423
1da177e4
LT
1424 return 0;
1425}
9fea84f4 1426fs_initcall(init_pcmcia_bus); /* one level after subsys_initcall so that
1da177e4
LT
1427 * pcmcia_socket_class is already registered */
1428
1429
1430static void __exit exit_pcmcia_bus(void)
1431{
e7a480d2 1432 class_interface_unregister(&pcmcia_bus_interface);
1da177e4
LT
1433
1434 bus_unregister(&pcmcia_bus_type);
1435}
1436module_exit(exit_pcmcia_bus);
1437
1438
1da177e4 1439MODULE_ALIAS("ds");
This page took 0.756235 seconds and 5 git commands to generate.