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