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