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