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