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