1 /*
2 * cs.c -- Kernel Card Services - core services
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 */
14
15 #include <linux/module.h>
16 #include <linux/moduleparam.h>
17 #include <linux/init.h>
18 #include <linux/kernel.h>
19 #include <linux/string.h>
20 #include <linux/major.h>
21 #include <linux/errno.h>
22 #include <linux/slab.h>
23 #include <linux/mm.h>
24 #include <linux/interrupt.h>
25 #include <linux/timer.h>
26 #include <linux/ioport.h>
27 #include <linux/delay.h>
28 #include <linux/pm.h>
29 #include <linux/device.h>
30 #include <linux/kthread.h>
31 #include <linux/freezer.h>
32 #include <asm/irq.h>
33
34 #include <pcmcia/ss.h>
35 #include <pcmcia/cistpl.h>
36 #include <pcmcia/cisreg.h>
37 #include <pcmcia/ds.h>
38 #include "cs_internal.h"
39
40
41 /* Module parameters */
42
43 MODULE_AUTHOR("David Hinds <dahinds@users.sourceforge.net>");
44 MODULE_DESCRIPTION("Linux Kernel Card Services");
45 MODULE_LICENSE("GPL");
46
47 #define INT_MODULE_PARM(n, v) static int n = v; module_param(n, int, 0444)
48
49 INT_MODULE_PARM(setup_delay, 10); /* centiseconds */
50 INT_MODULE_PARM(resume_delay, 20); /* centiseconds */
51 INT_MODULE_PARM(shutdown_delay, 3); /* centiseconds */
52 INT_MODULE_PARM(vcc_settle, 40); /* centiseconds */
53 INT_MODULE_PARM(reset_time, 10); /* usecs */
54 INT_MODULE_PARM(unreset_delay, 10); /* centiseconds */
55 INT_MODULE_PARM(unreset_check, 10); /* centiseconds */
56 INT_MODULE_PARM(unreset_limit, 30); /* unreset_check's */
57
58 /* Access speed for attribute memory windows */
59 INT_MODULE_PARM(cis_speed, 300); /* ns */
60
61
62 socket_state_t dead_socket = {
63 .csc_mask = SS_DETECT,
64 };
65 EXPORT_SYMBOL(dead_socket);
66
67
68 /* List of all sockets, protected by a rwsem */
69 LIST_HEAD(pcmcia_socket_list);
70 EXPORT_SYMBOL(pcmcia_socket_list);
71
72 DECLARE_RWSEM(pcmcia_socket_list_rwsem);
73 EXPORT_SYMBOL(pcmcia_socket_list_rwsem);
74
75
pcmcia_get_socket(struct pcmcia_socket * skt)76 struct pcmcia_socket *pcmcia_get_socket(struct pcmcia_socket *skt)
77 {
78 struct device *dev = get_device(&skt->dev);
79 if (!dev)
80 return NULL;
81 return dev_get_drvdata(dev);
82 }
83 EXPORT_SYMBOL(pcmcia_get_socket);
84
85
pcmcia_put_socket(struct pcmcia_socket * skt)86 void pcmcia_put_socket(struct pcmcia_socket *skt)
87 {
88 put_device(&skt->dev);
89 }
90 EXPORT_SYMBOL(pcmcia_put_socket);
91
92
pcmcia_release_socket(struct device * dev)93 static void pcmcia_release_socket(struct device *dev)
94 {
95 struct pcmcia_socket *socket = dev_get_drvdata(dev);
96
97 complete(&socket->socket_released);
98 }
99
100 static int pccardd(void *__skt);
101
102 /**
103 * pcmcia_register_socket - add a new pcmcia socket device
104 * @socket: the &socket to register
105 */
pcmcia_register_socket(struct pcmcia_socket * socket)106 int pcmcia_register_socket(struct pcmcia_socket *socket)
107 {
108 struct task_struct *tsk;
109 int ret;
110
111 if (!socket || !socket->ops || !socket->dev.parent || !socket->resource_ops)
112 return -EINVAL;
113
114 dev_dbg(&socket->dev, "pcmcia_register_socket(0x%p)\n", socket->ops);
115
116 /* try to obtain a socket number [yes, it gets ugly if we
117 * register more than 2^sizeof(unsigned int) pcmcia
118 * sockets... but the socket number is deprecated
119 * anyways, so I don't care] */
120 down_write(&pcmcia_socket_list_rwsem);
121 if (list_empty(&pcmcia_socket_list))
122 socket->sock = 0;
123 else {
124 unsigned int found, i = 1;
125 struct pcmcia_socket *tmp;
126 do {
127 found = 1;
128 list_for_each_entry(tmp, &pcmcia_socket_list, socket_list) {
129 if (tmp->sock == i)
130 found = 0;
131 }
132 i++;
133 } while (!found);
134 socket->sock = i - 1;
135 }
136 list_add_tail(&socket->socket_list, &pcmcia_socket_list);
137 up_write(&pcmcia_socket_list_rwsem);
138
139 #ifndef CONFIG_CARDBUS
140 /*
141 * If we do not support Cardbus, ensure that
142 * the Cardbus socket capability is disabled.
143 */
144 socket->features &= ~SS_CAP_CARDBUS;
145 #endif
146
147 /* set proper values in socket->dev */
148 dev_set_drvdata(&socket->dev, socket);
149 socket->dev.class = &pcmcia_socket_class;
150 dev_set_name(&socket->dev, "pcmcia_socket%u", socket->sock);
151
152 /* base address = 0, map = 0 */
153 socket->cis_mem.flags = 0;
154 socket->cis_mem.speed = cis_speed;
155
156 INIT_LIST_HEAD(&socket->cis_cache);
157
158 init_completion(&socket->socket_released);
159 init_completion(&socket->thread_done);
160 mutex_init(&socket->skt_mutex);
161 mutex_init(&socket->ops_mutex);
162 spin_lock_init(&socket->thread_lock);
163
164 if (socket->resource_ops->init) {
165 mutex_lock(&socket->ops_mutex);
166 ret = socket->resource_ops->init(socket);
167 mutex_unlock(&socket->ops_mutex);
168 if (ret)
169 goto err;
170 }
171
172 tsk = kthread_run(pccardd, socket, "pccardd");
173 if (IS_ERR(tsk)) {
174 ret = PTR_ERR(tsk);
175 goto err;
176 }
177
178 wait_for_completion(&socket->thread_done);
179 if (!socket->thread) {
180 dev_warn(&socket->dev,
181 "PCMCIA: warning: socket thread did not start\n");
182 return -EIO;
183 }
184
185 pcmcia_parse_events(socket, SS_DETECT);
186
187 /*
188 * Let's try to get the PCMCIA module for 16-bit PCMCIA support.
189 * If it fails, it doesn't matter -- we still have 32-bit CardBus
190 * support to offer, so this is not a failure mode.
191 */
192 request_module_nowait("pcmcia");
193
194 return 0;
195
196 err:
197 down_write(&pcmcia_socket_list_rwsem);
198 list_del(&socket->socket_list);
199 up_write(&pcmcia_socket_list_rwsem);
200 return ret;
201 } /* pcmcia_register_socket */
202 EXPORT_SYMBOL(pcmcia_register_socket);
203
204
205 /**
206 * pcmcia_unregister_socket - remove a pcmcia socket device
207 * @socket: the &socket to unregister
208 */
pcmcia_unregister_socket(struct pcmcia_socket * socket)209 void pcmcia_unregister_socket(struct pcmcia_socket *socket)
210 {
211 if (!socket)
212 return;
213
214 dev_dbg(&socket->dev, "pcmcia_unregister_socket(0x%p)\n", socket->ops);
215
216 if (socket->thread)
217 kthread_stop(socket->thread);
218
219 /* remove from our own list */
220 down_write(&pcmcia_socket_list_rwsem);
221 list_del(&socket->socket_list);
222 up_write(&pcmcia_socket_list_rwsem);
223
224 /* wait for sysfs to drop all references */
225 if (socket->resource_ops->exit) {
226 mutex_lock(&socket->ops_mutex);
227 socket->resource_ops->exit(socket);
228 mutex_unlock(&socket->ops_mutex);
229 }
230 wait_for_completion(&socket->socket_released);
231 } /* pcmcia_unregister_socket */
232 EXPORT_SYMBOL(pcmcia_unregister_socket);
233
234
pcmcia_get_socket_by_nr(unsigned int nr)235 struct pcmcia_socket *pcmcia_get_socket_by_nr(unsigned int nr)
236 {
237 struct pcmcia_socket *s;
238
239 down_read(&pcmcia_socket_list_rwsem);
240 list_for_each_entry(s, &pcmcia_socket_list, socket_list)
241 if (s->sock == nr) {
242 up_read(&pcmcia_socket_list_rwsem);
243 return s;
244 }
245 up_read(&pcmcia_socket_list_rwsem);
246
247 return NULL;
248
249 }
250 EXPORT_SYMBOL(pcmcia_get_socket_by_nr);
251
socket_reset(struct pcmcia_socket * skt)252 static int socket_reset(struct pcmcia_socket *skt)
253 {
254 int status, i;
255
256 dev_dbg(&skt->dev, "reset\n");
257
258 skt->socket.flags |= SS_OUTPUT_ENA | SS_RESET;
259 skt->ops->set_socket(skt, &skt->socket);
260 udelay((long)reset_time);
261
262 skt->socket.flags &= ~SS_RESET;
263 skt->ops->set_socket(skt, &skt->socket);
264
265 msleep(unreset_delay * 10);
266 for (i = 0; i < unreset_limit; i++) {
267 skt->ops->get_status(skt, &status);
268
269 if (!(status & SS_DETECT))
270 return -ENODEV;
271
272 if (status & SS_READY)
273 return 0;
274
275 msleep(unreset_check * 10);
276 }
277
278 dev_err(&skt->dev, "time out after reset\n");
279 return -ETIMEDOUT;
280 }
281
282 /*
283 * socket_setup() and socket_shutdown() are called by the main event handler
284 * when card insertion and removal events are received.
285 * socket_setup() turns on socket power and resets the socket, in two stages.
286 * socket_shutdown() unconfigures a socket and turns off socket power.
287 */
socket_shutdown(struct pcmcia_socket * s)288 static void socket_shutdown(struct pcmcia_socket *s)
289 {
290 int status;
291
292 dev_dbg(&s->dev, "shutdown\n");
293
294 if (s->callback)
295 s->callback->remove(s);
296
297 mutex_lock(&s->ops_mutex);
298 s->state &= SOCKET_INUSE | SOCKET_PRESENT;
299 msleep(shutdown_delay * 10);
300 s->state &= SOCKET_INUSE;
301
302 /* Blank out the socket state */
303 s->socket = dead_socket;
304 s->ops->init(s);
305 s->ops->set_socket(s, &s->socket);
306 s->lock_count = 0;
307 kfree(s->fake_cis);
308 s->fake_cis = NULL;
309 s->functions = 0;
310
311 /* From here on we can be sure that only we (that is, the
312 * pccardd thread) accesses this socket, and all (16-bit)
313 * PCMCIA interactions are gone. Therefore, release
314 * ops_mutex so that we don't get a sysfs-related lockdep
315 * warning.
316 */
317 mutex_unlock(&s->ops_mutex);
318
319 #ifdef CONFIG_CARDBUS
320 cb_free(s);
321 #endif
322
323 /* give socket some time to power down */
324 msleep(100);
325
326 s->ops->get_status(s, &status);
327 if (status & SS_POWERON) {
328 dev_err(&s->dev,
329 "*** DANGER *** unable to remove socket power\n");
330 }
331
332 s->state &= ~SOCKET_INUSE;
333 }
334
socket_setup(struct pcmcia_socket * skt,int initial_delay)335 static int socket_setup(struct pcmcia_socket *skt, int initial_delay)
336 {
337 int status, i;
338
339 dev_dbg(&skt->dev, "setup\n");
340
341 skt->ops->get_status(skt, &status);
342 if (!(status & SS_DETECT))
343 return -ENODEV;
344
345 msleep(initial_delay * 10);
346
347 for (i = 0; i < 100; i++) {
348 skt->ops->get_status(skt, &status);
349 if (!(status & SS_DETECT))
350 return -ENODEV;
351
352 if (!(status & SS_PENDING))
353 break;
354
355 msleep(100);
356 }
357
358 if (status & SS_PENDING) {
359 dev_err(&skt->dev, "voltage interrogation timed out\n");
360 return -ETIMEDOUT;
361 }
362
363 if (status & SS_CARDBUS) {
364 if (!(skt->features & SS_CAP_CARDBUS)) {
365 dev_err(&skt->dev, "cardbus cards are not supported\n");
366 return -EINVAL;
367 }
368 skt->state |= SOCKET_CARDBUS;
369 } else
370 skt->state &= ~SOCKET_CARDBUS;
371
372 /*
373 * Decode the card voltage requirements, and apply power to the card.
374 */
375 if (status & SS_3VCARD)
376 skt->socket.Vcc = skt->socket.Vpp = 33;
377 else if (!(status & SS_XVCARD))
378 skt->socket.Vcc = skt->socket.Vpp = 50;
379 else {
380 dev_err(&skt->dev, "unsupported voltage key\n");
381 return -EIO;
382 }
383
384 if (skt->power_hook)
385 skt->power_hook(skt, HOOK_POWER_PRE);
386
387 skt->socket.flags = 0;
388 skt->ops->set_socket(skt, &skt->socket);
389
390 /*
391 * Wait "vcc_settle" for the supply to stabilise.
392 */
393 msleep(vcc_settle * 10);
394
395 skt->ops->get_status(skt, &status);
396 if (!(status & SS_POWERON)) {
397 dev_err(&skt->dev, "unable to apply power\n");
398 return -EIO;
399 }
400
401 status = socket_reset(skt);
402
403 if (skt->power_hook)
404 skt->power_hook(skt, HOOK_POWER_POST);
405
406 return status;
407 }
408
409 /*
410 * Handle card insertion. Setup the socket, reset the card,
411 * and then tell the rest of PCMCIA that a card is present.
412 */
socket_insert(struct pcmcia_socket * skt)413 static int socket_insert(struct pcmcia_socket *skt)
414 {
415 int ret;
416
417 dev_dbg(&skt->dev, "insert\n");
418
419 mutex_lock(&skt->ops_mutex);
420 if (skt->state & SOCKET_INUSE) {
421 mutex_unlock(&skt->ops_mutex);
422 return -EINVAL;
423 }
424 skt->state |= SOCKET_INUSE;
425
426 ret = socket_setup(skt, setup_delay);
427 if (ret == 0) {
428 skt->state |= SOCKET_PRESENT;
429
430 dev_notice(&skt->dev, "pccard: %s card inserted into slot %d\n",
431 (skt->state & SOCKET_CARDBUS) ? "CardBus" : "PCMCIA",
432 skt->sock);
433
434 #ifdef CONFIG_CARDBUS
435 if (skt->state & SOCKET_CARDBUS) {
436 cb_alloc(skt);
437 skt->state |= SOCKET_CARDBUS_CONFIG;
438 }
439 #endif
440 dev_dbg(&skt->dev, "insert done\n");
441 mutex_unlock(&skt->ops_mutex);
442
443 if (!(skt->state & SOCKET_CARDBUS) && (skt->callback))
444 skt->callback->add(skt);
445 } else {
446 mutex_unlock(&skt->ops_mutex);
447 socket_shutdown(skt);
448 }
449
450 return ret;
451 }
452
socket_suspend(struct pcmcia_socket * skt)453 static int socket_suspend(struct pcmcia_socket *skt)
454 {
455 if (skt->state & SOCKET_SUSPEND)
456 return -EBUSY;
457
458 mutex_lock(&skt->ops_mutex);
459 skt->suspended_state = skt->state;
460
461 skt->socket = dead_socket;
462 skt->ops->set_socket(skt, &skt->socket);
463 if (skt->ops->suspend)
464 skt->ops->suspend(skt);
465 skt->state |= SOCKET_SUSPEND;
466 mutex_unlock(&skt->ops_mutex);
467 return 0;
468 }
469
socket_early_resume(struct pcmcia_socket * skt)470 static int socket_early_resume(struct pcmcia_socket *skt)
471 {
472 mutex_lock(&skt->ops_mutex);
473 skt->socket = dead_socket;
474 skt->ops->init(skt);
475 skt->ops->set_socket(skt, &skt->socket);
476 if (skt->state & SOCKET_PRESENT)
477 skt->resume_status = socket_setup(skt, resume_delay);
478 mutex_unlock(&skt->ops_mutex);
479 return 0;
480 }
481
socket_late_resume(struct pcmcia_socket * skt)482 static int socket_late_resume(struct pcmcia_socket *skt)
483 {
484 int ret = 0;
485
486 mutex_lock(&skt->ops_mutex);
487 skt->state &= ~SOCKET_SUSPEND;
488 mutex_unlock(&skt->ops_mutex);
489
490 if (!(skt->state & SOCKET_PRESENT)) {
491 ret = socket_insert(skt);
492 if (ret == -ENODEV)
493 ret = 0;
494 return ret;
495 }
496
497 if (skt->resume_status) {
498 socket_shutdown(skt);
499 return 0;
500 }
501
502 if (skt->suspended_state != skt->state) {
503 dev_dbg(&skt->dev,
504 "suspend state 0x%x != resume state 0x%x\n",
505 skt->suspended_state, skt->state);
506
507 socket_shutdown(skt);
508 return socket_insert(skt);
509 }
510
511 if (!(skt->state & SOCKET_CARDBUS) && (skt->callback))
512 ret = skt->callback->early_resume(skt);
513 return ret;
514 }
515
516 /*
517 * Finalize the resume. In case of a cardbus socket, we have
518 * to rebind the devices as we can't be certain that it has been
519 * replaced, or not.
520 */
socket_complete_resume(struct pcmcia_socket * skt)521 static int socket_complete_resume(struct pcmcia_socket *skt)
522 {
523 int ret = 0;
524 #ifdef CONFIG_CARDBUS
525 if (skt->state & SOCKET_CARDBUS) {
526 /* We can't be sure the CardBus card is the same
527 * as the one previously inserted. Therefore, remove
528 * and re-add... */
529 cb_free(skt);
530 ret = cb_alloc(skt);
531 if (ret)
532 cb_free(skt);
533 }
534 #endif
535 return ret;
536 }
537
538 /*
539 * Resume a socket. If a card is present, verify its CIS against
540 * our cached copy. If they are different, the card has been
541 * replaced, and we need to tell the drivers.
542 */
socket_resume(struct pcmcia_socket * skt)543 static int socket_resume(struct pcmcia_socket *skt)
544 {
545 int err;
546 if (!(skt->state & SOCKET_SUSPEND))
547 return -EBUSY;
548
549 socket_early_resume(skt);
550 err = socket_late_resume(skt);
551 if (!err)
552 err = socket_complete_resume(skt);
553 return err;
554 }
555
socket_remove(struct pcmcia_socket * skt)556 static void socket_remove(struct pcmcia_socket *skt)
557 {
558 dev_notice(&skt->dev, "pccard: card ejected from slot %d\n", skt->sock);
559 socket_shutdown(skt);
560 }
561
562 /*
563 * Process a socket card detect status change.
564 *
565 * If we don't have a card already present, delay the detect event for
566 * about 20ms (to be on the safe side) before reading the socket status.
567 *
568 * Some i82365-based systems send multiple SS_DETECT events during card
569 * insertion, and the "card present" status bit seems to bounce. This
570 * will probably be true with GPIO-based card detection systems after
571 * the product has aged.
572 */
socket_detect_change(struct pcmcia_socket * skt)573 static void socket_detect_change(struct pcmcia_socket *skt)
574 {
575 if (!(skt->state & SOCKET_SUSPEND)) {
576 int status;
577
578 if (!(skt->state & SOCKET_PRESENT))
579 msleep(20);
580
581 skt->ops->get_status(skt, &status);
582 if ((skt->state & SOCKET_PRESENT) &&
583 !(status & SS_DETECT))
584 socket_remove(skt);
585 if (!(skt->state & SOCKET_PRESENT) &&
586 (status & SS_DETECT))
587 socket_insert(skt);
588 }
589 }
590
pccardd(void * __skt)591 static int pccardd(void *__skt)
592 {
593 struct pcmcia_socket *skt = __skt;
594 int ret;
595
596 skt->thread = current;
597 skt->socket = dead_socket;
598 skt->ops->init(skt);
599 skt->ops->set_socket(skt, &skt->socket);
600
601 /* register with the device core */
602 ret = device_register(&skt->dev);
603 if (ret) {
604 dev_warn(&skt->dev, "PCMCIA: unable to register socket\n");
605 skt->thread = NULL;
606 complete(&skt->thread_done);
607 return 0;
608 }
609 ret = pccard_sysfs_add_socket(&skt->dev);
610 if (ret)
611 dev_warn(&skt->dev, "err %d adding socket attributes\n", ret);
612
613 complete(&skt->thread_done);
614
615 /* wait for userspace to catch up */
616 msleep(250);
617
618 set_freezable();
619 for (;;) {
620 unsigned long flags;
621 unsigned int events;
622 unsigned int sysfs_events;
623
624 spin_lock_irqsave(&skt->thread_lock, flags);
625 events = skt->thread_events;
626 skt->thread_events = 0;
627 sysfs_events = skt->sysfs_events;
628 skt->sysfs_events = 0;
629 spin_unlock_irqrestore(&skt->thread_lock, flags);
630
631 mutex_lock(&skt->skt_mutex);
632 if (events & SS_DETECT)
633 socket_detect_change(skt);
634
635 if (sysfs_events) {
636 if (sysfs_events & PCMCIA_UEVENT_EJECT)
637 socket_remove(skt);
638 if (sysfs_events & PCMCIA_UEVENT_INSERT)
639 socket_insert(skt);
640 if ((sysfs_events & PCMCIA_UEVENT_SUSPEND) &&
641 !(skt->state & SOCKET_CARDBUS)) {
642 if (skt->callback)
643 ret = skt->callback->suspend(skt);
644 else
645 ret = 0;
646 if (!ret) {
647 socket_suspend(skt);
648 msleep(100);
649 }
650 }
651 if ((sysfs_events & PCMCIA_UEVENT_RESUME) &&
652 !(skt->state & SOCKET_CARDBUS)) {
653 ret = socket_resume(skt);
654 if (!ret && skt->callback)
655 skt->callback->resume(skt);
656 }
657 if ((sysfs_events & PCMCIA_UEVENT_REQUERY) &&
658 !(skt->state & SOCKET_CARDBUS)) {
659 if (!ret && skt->callback)
660 skt->callback->requery(skt);
661 }
662 }
663 mutex_unlock(&skt->skt_mutex);
664
665 if (events || sysfs_events)
666 continue;
667
668 set_current_state(TASK_INTERRUPTIBLE);
669 if (kthread_should_stop())
670 break;
671
672 schedule();
673
674 try_to_freeze();
675 }
676 /* make sure we are running before we exit */
677 __set_current_state(TASK_RUNNING);
678
679 /* shut down socket, if a device is still present */
680 if (skt->state & SOCKET_PRESENT) {
681 mutex_lock(&skt->skt_mutex);
682 socket_remove(skt);
683 mutex_unlock(&skt->skt_mutex);
684 }
685
686 /* remove from the device core */
687 pccard_sysfs_remove_socket(&skt->dev);
688 device_unregister(&skt->dev);
689
690 return 0;
691 }
692
693 /*
694 * Yenta (at least) probes interrupts before registering the socket and
695 * starting the handler thread.
696 */
pcmcia_parse_events(struct pcmcia_socket * s,u_int events)697 void pcmcia_parse_events(struct pcmcia_socket *s, u_int events)
698 {
699 unsigned long flags;
700 dev_dbg(&s->dev, "parse_events: events %08x\n", events);
701 if (s->thread) {
702 spin_lock_irqsave(&s->thread_lock, flags);
703 s->thread_events |= events;
704 spin_unlock_irqrestore(&s->thread_lock, flags);
705
706 wake_up_process(s->thread);
707 }
708 } /* pcmcia_parse_events */
709 EXPORT_SYMBOL(pcmcia_parse_events);
710
711 /**
712 * pcmcia_parse_uevents() - tell pccardd to issue manual commands
713 * @s: the PCMCIA socket we wan't to command
714 * @events: events to pass to pccardd
715 *
716 * userspace-issued insert, eject, suspend and resume commands must be
717 * handled by pccardd to avoid any sysfs-related deadlocks. Valid events
718 * are PCMCIA_UEVENT_EJECT (for eject), PCMCIA_UEVENT__INSERT (for insert),
719 * PCMCIA_UEVENT_RESUME (for resume), PCMCIA_UEVENT_SUSPEND (for suspend)
720 * and PCMCIA_UEVENT_REQUERY (for re-querying the PCMCIA card).
721 */
pcmcia_parse_uevents(struct pcmcia_socket * s,u_int events)722 void pcmcia_parse_uevents(struct pcmcia_socket *s, u_int events)
723 {
724 unsigned long flags;
725 dev_dbg(&s->dev, "parse_uevents: events %08x\n", events);
726 if (s->thread) {
727 spin_lock_irqsave(&s->thread_lock, flags);
728 s->sysfs_events |= events;
729 spin_unlock_irqrestore(&s->thread_lock, flags);
730
731 wake_up_process(s->thread);
732 }
733 }
734 EXPORT_SYMBOL(pcmcia_parse_uevents);
735
736
737 /* register pcmcia_callback */
pccard_register_pcmcia(struct pcmcia_socket * s,struct pcmcia_callback * c)738 int pccard_register_pcmcia(struct pcmcia_socket *s, struct pcmcia_callback *c)
739 {
740 int ret = 0;
741
742 /* s->skt_mutex also protects s->callback */
743 mutex_lock(&s->skt_mutex);
744
745 if (c) {
746 /* registration */
747 if (s->callback) {
748 ret = -EBUSY;
749 goto err;
750 }
751
752 s->callback = c;
753
754 if ((s->state & (SOCKET_PRESENT|SOCKET_CARDBUS)) == SOCKET_PRESENT)
755 s->callback->add(s);
756 } else
757 s->callback = NULL;
758 err:
759 mutex_unlock(&s->skt_mutex);
760
761 return ret;
762 }
763 EXPORT_SYMBOL(pccard_register_pcmcia);
764
765
766 /* I'm not sure which "reset" function this is supposed to use,
767 * but for now, it uses the low-level interface's reset, not the
768 * CIS register.
769 */
770
pcmcia_reset_card(struct pcmcia_socket * skt)771 int pcmcia_reset_card(struct pcmcia_socket *skt)
772 {
773 int ret;
774
775 dev_dbg(&skt->dev, "resetting socket\n");
776
777 mutex_lock(&skt->skt_mutex);
778 do {
779 if (!(skt->state & SOCKET_PRESENT)) {
780 dev_dbg(&skt->dev, "can't reset, not present\n");
781 ret = -ENODEV;
782 break;
783 }
784 if (skt->state & SOCKET_SUSPEND) {
785 dev_dbg(&skt->dev, "can't reset, suspended\n");
786 ret = -EBUSY;
787 break;
788 }
789 if (skt->state & SOCKET_CARDBUS) {
790 dev_dbg(&skt->dev, "can't reset, is cardbus\n");
791 ret = -EPERM;
792 break;
793 }
794
795 if (skt->callback)
796 skt->callback->suspend(skt);
797 mutex_lock(&skt->ops_mutex);
798 ret = socket_reset(skt);
799 mutex_unlock(&skt->ops_mutex);
800 if ((ret == 0) && (skt->callback))
801 skt->callback->resume(skt);
802
803 ret = 0;
804 } while (0);
805 mutex_unlock(&skt->skt_mutex);
806
807 return ret;
808 } /* reset_card */
809 EXPORT_SYMBOL(pcmcia_reset_card);
810
811
pcmcia_socket_uevent(struct device * dev,struct kobj_uevent_env * env)812 static int pcmcia_socket_uevent(struct device *dev,
813 struct kobj_uevent_env *env)
814 {
815 struct pcmcia_socket *s = container_of(dev, struct pcmcia_socket, dev);
816
817 if (add_uevent_var(env, "SOCKET_NO=%u", s->sock))
818 return -ENOMEM;
819
820 return 0;
821 }
822
823
824 static struct completion pcmcia_unload;
825
pcmcia_release_socket_class(struct class * data)826 static void pcmcia_release_socket_class(struct class *data)
827 {
828 complete(&pcmcia_unload);
829 }
830
831
832 #ifdef CONFIG_PM
833
__pcmcia_pm_op(struct device * dev,int (* callback)(struct pcmcia_socket * skt))834 static int __pcmcia_pm_op(struct device *dev,
835 int (*callback) (struct pcmcia_socket *skt))
836 {
837 struct pcmcia_socket *s = container_of(dev, struct pcmcia_socket, dev);
838 int ret;
839
840 mutex_lock(&s->skt_mutex);
841 ret = callback(s);
842 mutex_unlock(&s->skt_mutex);
843
844 return ret;
845 }
846
pcmcia_socket_dev_suspend_noirq(struct device * dev)847 static int pcmcia_socket_dev_suspend_noirq(struct device *dev)
848 {
849 return __pcmcia_pm_op(dev, socket_suspend);
850 }
851
pcmcia_socket_dev_resume_noirq(struct device * dev)852 static int pcmcia_socket_dev_resume_noirq(struct device *dev)
853 {
854 return __pcmcia_pm_op(dev, socket_early_resume);
855 }
856
pcmcia_socket_dev_resume(struct device * dev)857 static int __used pcmcia_socket_dev_resume(struct device *dev)
858 {
859 return __pcmcia_pm_op(dev, socket_late_resume);
860 }
861
pcmcia_socket_dev_complete(struct device * dev)862 static void __used pcmcia_socket_dev_complete(struct device *dev)
863 {
864 WARN(__pcmcia_pm_op(dev, socket_complete_resume),
865 "failed to complete resume");
866 }
867
868 static const struct dev_pm_ops pcmcia_socket_pm_ops = {
869 /* dev_resume may be called with IRQs enabled */
870 SET_SYSTEM_SLEEP_PM_OPS(NULL,
871 pcmcia_socket_dev_resume)
872
873 /* late suspend must be called with IRQs disabled */
874 .suspend_noirq = pcmcia_socket_dev_suspend_noirq,
875 .freeze_noirq = pcmcia_socket_dev_suspend_noirq,
876 .poweroff_noirq = pcmcia_socket_dev_suspend_noirq,
877
878 /* early resume must be called with IRQs disabled */
879 .resume_noirq = pcmcia_socket_dev_resume_noirq,
880 .thaw_noirq = pcmcia_socket_dev_resume_noirq,
881 .restore_noirq = pcmcia_socket_dev_resume_noirq,
882 .complete = pcmcia_socket_dev_complete,
883 };
884
885 #define PCMCIA_SOCKET_CLASS_PM_OPS (&pcmcia_socket_pm_ops)
886
887 #else /* CONFIG_PM */
888
889 #define PCMCIA_SOCKET_CLASS_PM_OPS NULL
890
891 #endif /* CONFIG_PM */
892
893 struct class pcmcia_socket_class = {
894 .name = "pcmcia_socket",
895 .dev_uevent = pcmcia_socket_uevent,
896 .dev_release = pcmcia_release_socket,
897 .class_release = pcmcia_release_socket_class,
898 .pm = PCMCIA_SOCKET_CLASS_PM_OPS,
899 };
900 EXPORT_SYMBOL(pcmcia_socket_class);
901
902
init_pcmcia_cs(void)903 static int __init init_pcmcia_cs(void)
904 {
905 init_completion(&pcmcia_unload);
906 return class_register(&pcmcia_socket_class);
907 }
908
exit_pcmcia_cs(void)909 static void __exit exit_pcmcia_cs(void)
910 {
911 class_unregister(&pcmcia_socket_class);
912 wait_for_completion(&pcmcia_unload);
913 }
914
915 subsys_initcall(init_pcmcia_cs);
916 module_exit(exit_pcmcia_cs);
917
918