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