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