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