blob: 0ff4d6ec8b770eb0b7a43dbe68efb96a3718d9e5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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/config.h>
20#include <linux/string.h>
21#include <linux/major.h>
22#include <linux/errno.h>
23#include <linux/slab.h>
24#include <linux/mm.h>
25#include <linux/interrupt.h>
26#include <linux/timer.h>
27#include <linux/ioport.h>
28#include <linux/delay.h>
29#include <linux/pm.h>
30#include <linux/pci.h>
31#include <linux/device.h>
32#include <asm/system.h>
33#include <asm/irq.h>
34
35#define IN_CARD_SERVICES
36#include <pcmcia/version.h>
37#include <pcmcia/cs_types.h>
38#include <pcmcia/ss.h>
39#include <pcmcia/cs.h>
40#include <pcmcia/bulkmem.h>
41#include <pcmcia/cistpl.h>
42#include <pcmcia/cisreg.h>
43#include <pcmcia/ds.h>
44#include "cs_internal.h"
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47/* Module parameters */
48
49MODULE_AUTHOR("David Hinds <dahinds@users.sourceforge.net>");
Dominik Brodowski33519dd2005-06-27 16:28:53 -070050MODULE_DESCRIPTION("Linux Kernel Card Services");
Linus Torvalds1da177e2005-04-16 15:20:36 -070051MODULE_LICENSE("GPL");
52
53#define INT_MODULE_PARM(n, v) static int n = v; module_param(n, int, 0444)
54
55INT_MODULE_PARM(setup_delay, 10); /* centiseconds */
56INT_MODULE_PARM(resume_delay, 20); /* centiseconds */
57INT_MODULE_PARM(shutdown_delay, 3); /* centiseconds */
58INT_MODULE_PARM(vcc_settle, 40); /* centiseconds */
59INT_MODULE_PARM(reset_time, 10); /* usecs */
60INT_MODULE_PARM(unreset_delay, 10); /* centiseconds */
61INT_MODULE_PARM(unreset_check, 10); /* centiseconds */
62INT_MODULE_PARM(unreset_limit, 30); /* unreset_check's */
63
64/* Access speed for attribute memory windows */
65INT_MODULE_PARM(cis_speed, 300); /* ns */
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#ifdef DEBUG
68static int pc_debug;
69
70module_param(pc_debug, int, 0644);
71
72int cs_debug_level(int level)
73{
74 return pc_debug > level;
75}
76#endif
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79socket_state_t dead_socket = {
80 .csc_mask = SS_DETECT,
81};
Dominik Brodowski33519dd2005-06-27 16:28:53 -070082EXPORT_SYMBOL(dead_socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84
85/* List of all sockets, protected by a rwsem */
86LIST_HEAD(pcmcia_socket_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087EXPORT_SYMBOL(pcmcia_socket_list);
Dominik Brodowski33519dd2005-06-27 16:28:53 -070088
89DECLARE_RWSEM(pcmcia_socket_list_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090EXPORT_SYMBOL(pcmcia_socket_list_rwsem);
91
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093/**
Dominik Brodowski33519dd2005-06-27 16:28:53 -070094 * Low-level PCMCIA socket drivers need to register with the PCCard
95 * core using pcmcia_register_socket.
96 *
97 * socket drivers are expected to use the following callbacks in their
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 * .drv struct:
99 * - pcmcia_socket_dev_suspend
100 * - pcmcia_socket_dev_resume
101 * These functions check for the appropriate struct pcmcia_soket arrays,
102 * and pass them to the low-level functions pcmcia_{suspend,resume}_socket
103 */
104static int socket_resume(struct pcmcia_socket *skt);
105static int socket_suspend(struct pcmcia_socket *skt);
106
107int pcmcia_socket_dev_suspend(struct device *dev, pm_message_t state)
108{
109 struct pcmcia_socket *socket;
110
111 down_read(&pcmcia_socket_list_rwsem);
112 list_for_each_entry(socket, &pcmcia_socket_list, socket_list) {
113 if (socket->dev.dev != dev)
114 continue;
115 down(&socket->skt_sem);
116 socket_suspend(socket);
117 up(&socket->skt_sem);
118 }
119 up_read(&pcmcia_socket_list_rwsem);
120
121 return 0;
122}
123EXPORT_SYMBOL(pcmcia_socket_dev_suspend);
124
125int pcmcia_socket_dev_resume(struct device *dev)
126{
127 struct pcmcia_socket *socket;
128
129 down_read(&pcmcia_socket_list_rwsem);
130 list_for_each_entry(socket, &pcmcia_socket_list, socket_list) {
131 if (socket->dev.dev != dev)
132 continue;
133 down(&socket->skt_sem);
134 socket_resume(socket);
135 up(&socket->skt_sem);
136 }
137 up_read(&pcmcia_socket_list_rwsem);
138
139 return 0;
140}
141EXPORT_SYMBOL(pcmcia_socket_dev_resume);
142
143
144struct pcmcia_socket * pcmcia_get_socket(struct pcmcia_socket *skt)
145{
146 struct class_device *cl_dev = class_device_get(&skt->dev);
147 if (!cl_dev)
148 return NULL;
149 skt = class_get_devdata(cl_dev);
150 if (!try_module_get(skt->owner)) {
151 class_device_put(&skt->dev);
152 return NULL;
153 }
154 return (skt);
155}
156EXPORT_SYMBOL(pcmcia_get_socket);
157
158
159void pcmcia_put_socket(struct pcmcia_socket *skt)
160{
161 module_put(skt->owner);
162 class_device_put(&skt->dev);
163}
164EXPORT_SYMBOL(pcmcia_put_socket);
165
166
167static void pcmcia_release_socket(struct class_device *class_dev)
168{
169 struct pcmcia_socket *socket = class_get_devdata(class_dev);
170
171 complete(&socket->socket_released);
172}
173
174static int pccardd(void *__skt);
175
176/**
177 * pcmcia_register_socket - add a new pcmcia socket device
178 */
179int pcmcia_register_socket(struct pcmcia_socket *socket)
180{
181 int ret;
182
183 if (!socket || !socket->ops || !socket->dev.dev || !socket->resource_ops)
184 return -EINVAL;
185
186 cs_dbg(socket, 0, "pcmcia_register_socket(0x%p)\n", socket->ops);
187
188 spin_lock_init(&socket->lock);
189
190 if (socket->resource_ops->init) {
191 ret = socket->resource_ops->init(socket);
192 if (ret)
193 return (ret);
194 }
195
196 /* try to obtain a socket number [yes, it gets ugly if we
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700197 * register more than 2^sizeof(unsigned int) pcmcia
198 * sockets... but the socket number is deprecated
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 * anyways, so I don't care] */
200 down_write(&pcmcia_socket_list_rwsem);
201 if (list_empty(&pcmcia_socket_list))
202 socket->sock = 0;
203 else {
204 unsigned int found, i = 1;
205 struct pcmcia_socket *tmp;
206 do {
207 found = 1;
208 list_for_each_entry(tmp, &pcmcia_socket_list, socket_list) {
209 if (tmp->sock == i)
210 found = 0;
211 }
212 i++;
213 } while (!found);
214 socket->sock = i - 1;
215 }
216 list_add_tail(&socket->socket_list, &pcmcia_socket_list);
217 up_write(&pcmcia_socket_list_rwsem);
218
219
220 /* set proper values in socket->dev */
221 socket->dev.class_data = socket;
222 socket->dev.class = &pcmcia_socket_class;
223 snprintf(socket->dev.class_id, BUS_ID_SIZE, "pcmcia_socket%u", socket->sock);
224
225 /* base address = 0, map = 0 */
226 socket->cis_mem.flags = 0;
227 socket->cis_mem.speed = cis_speed;
228
229 INIT_LIST_HEAD(&socket->cis_cache);
230
231 init_completion(&socket->socket_released);
232 init_completion(&socket->thread_done);
233 init_waitqueue_head(&socket->thread_wait);
234 init_MUTEX(&socket->skt_sem);
235 spin_lock_init(&socket->thread_lock);
236
237 ret = kernel_thread(pccardd, socket, CLONE_KERNEL);
238 if (ret < 0)
239 goto err;
240
241 wait_for_completion(&socket->thread_done);
242 if(!socket->thread) {
243 printk(KERN_WARNING "PCMCIA: warning: socket thread for socket %p did not start\n", socket);
244 return -EIO;
245 }
246 pcmcia_parse_events(socket, SS_DETECT);
247
248 return 0;
249
250 err:
251 down_write(&pcmcia_socket_list_rwsem);
252 list_del(&socket->socket_list);
253 up_write(&pcmcia_socket_list_rwsem);
254 return ret;
255} /* pcmcia_register_socket */
256EXPORT_SYMBOL(pcmcia_register_socket);
257
258
259/**
260 * pcmcia_unregister_socket - remove a pcmcia socket device
261 */
262void pcmcia_unregister_socket(struct pcmcia_socket *socket)
263{
264 if (!socket)
265 return;
266
267 cs_dbg(socket, 0, "pcmcia_unregister_socket(0x%p)\n", socket->ops);
268
269 if (socket->thread) {
270 init_completion(&socket->thread_done);
271 socket->thread = NULL;
272 wake_up(&socket->thread_wait);
273 wait_for_completion(&socket->thread_done);
274 }
275 release_cis_mem(socket);
276
277 /* remove from our own list */
278 down_write(&pcmcia_socket_list_rwsem);
279 list_del(&socket->socket_list);
280 up_write(&pcmcia_socket_list_rwsem);
281
282 /* wait for sysfs to drop all references */
283 release_resource_db(socket);
284 wait_for_completion(&socket->socket_released);
285} /* pcmcia_unregister_socket */
286EXPORT_SYMBOL(pcmcia_unregister_socket);
287
288
289struct pcmcia_socket * pcmcia_get_socket_by_nr(unsigned int nr)
290{
291 struct pcmcia_socket *s;
292
293 down_read(&pcmcia_socket_list_rwsem);
294 list_for_each_entry(s, &pcmcia_socket_list, socket_list)
295 if (s->sock == nr) {
296 up_read(&pcmcia_socket_list_rwsem);
297 return s;
298 }
299 up_read(&pcmcia_socket_list_rwsem);
300
301 return NULL;
302
303}
304EXPORT_SYMBOL(pcmcia_get_socket_by_nr);
305
306
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700307/**
308 * socket_setup() and shutdown_socket() are called by the main event
309 * handler when card insertion and removal events are received.
310 * socket_setup() turns on socket power and resets the socket, in two stages.
311 * shutdown_socket() unconfigures a socket and turns off socket power.
312 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313static void shutdown_socket(struct pcmcia_socket *s)
314{
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700315 cs_dbg(s, 1, "shutdown_socket\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700317 /* Blank out the socket state */
318 s->socket = dead_socket;
319 s->ops->init(s);
320 s->ops->set_socket(s, &s->socket);
321 s->irq.AssignedIRQ = s->irq.Config = 0;
322 s->lock_count = 0;
323 destroy_cis_cache(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324#ifdef CONFIG_CARDBUS
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700325 cb_free(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326#endif
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700327 s->functions = 0;
328 if (s->config) {
329 kfree(s->config);
330 s->config = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 }
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700332
333 {
334 int status;
335 s->ops->get_status(s, &status);
336 if (status & SS_POWERON) {
337 printk(KERN_ERR "PCMCIA: socket %p: *** DANGER *** unable to remove socket power\n", s);
338 }
339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340} /* shutdown_socket */
341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700343/**
344 * The central event handler. Send_event() sends an event to the
345 * 16-bit subsystem, which then calls the relevant device drivers.
346 * Parse_events() interprets the event bits from
347 * a card status change report. Do_shutdown() handles the high
348 * priority stuff associated with a card removal.
349 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
351/* NOTE: send_event needs to be called with skt->sem held. */
352
353static int send_event(struct pcmcia_socket *s, event_t event, int priority)
354{
355 int ret;
356
357 if (s->state & SOCKET_CARDBUS)
358 return 0;
359
360 cs_dbg(s, 1, "send_event(event %d, pri %d, callback 0x%p)\n",
361 event, priority, s->callback);
362
363 if (!s->callback)
364 return 0;
365 if (!try_module_get(s->callback->owner))
366 return 0;
367
368 ret = s->callback->event(s, event, priority);
369
370 module_put(s->callback->owner);
371
372 return ret;
373}
374
375static void socket_remove_drivers(struct pcmcia_socket *skt)
376{
377 cs_dbg(skt, 4, "remove_drivers\n");
378
379 send_event(skt, CS_EVENT_CARD_REMOVAL, CS_EVENT_PRI_HIGH);
380}
381
382static void socket_shutdown(struct pcmcia_socket *skt)
383{
384 cs_dbg(skt, 4, "shutdown\n");
385
386 socket_remove_drivers(skt);
387 skt->state &= SOCKET_INUSE|SOCKET_PRESENT;
388 msleep(shutdown_delay * 10);
389 skt->state &= SOCKET_INUSE;
390 shutdown_socket(skt);
391}
392
393static int socket_reset(struct pcmcia_socket *skt)
394{
395 int status, i;
396
397 cs_dbg(skt, 4, "reset\n");
398
399 skt->socket.flags |= SS_OUTPUT_ENA | SS_RESET;
400 skt->ops->set_socket(skt, &skt->socket);
401 udelay((long)reset_time);
402
403 skt->socket.flags &= ~SS_RESET;
404 skt->ops->set_socket(skt, &skt->socket);
405
406 msleep(unreset_delay * 10);
407 for (i = 0; i < unreset_limit; i++) {
408 skt->ops->get_status(skt, &status);
409
410 if (!(status & SS_DETECT))
411 return CS_NO_CARD;
412
413 if (status & SS_READY)
414 return CS_SUCCESS;
415
416 msleep(unreset_check * 10);
417 }
418
419 cs_err(skt, "time out after reset.\n");
420 return CS_GENERAL_FAILURE;
421}
422
423static int socket_setup(struct pcmcia_socket *skt, int initial_delay)
424{
425 int status, i;
426
427 cs_dbg(skt, 4, "setup\n");
428
429 skt->ops->get_status(skt, &status);
430 if (!(status & SS_DETECT))
431 return CS_NO_CARD;
432
433 msleep(initial_delay * 10);
434
435 for (i = 0; i < 100; i++) {
436 skt->ops->get_status(skt, &status);
437 if (!(status & SS_DETECT))
438 return CS_NO_CARD;
439
440 if (!(status & SS_PENDING))
441 break;
442
443 msleep(100);
444 }
445
446 if (status & SS_PENDING) {
447 cs_err(skt, "voltage interrogation timed out.\n");
448 return CS_GENERAL_FAILURE;
449 }
450
451 if (status & SS_CARDBUS) {
452 skt->state |= SOCKET_CARDBUS;
453#ifndef CONFIG_CARDBUS
454 cs_err(skt, "cardbus cards are not supported.\n");
455 return CS_BAD_TYPE;
456#endif
457 }
458
459 /*
460 * Decode the card voltage requirements, and apply power to the card.
461 */
462 if (status & SS_3VCARD)
463 skt->socket.Vcc = skt->socket.Vpp = 33;
464 else if (!(status & SS_XVCARD))
465 skt->socket.Vcc = skt->socket.Vpp = 50;
466 else {
467 cs_err(skt, "unsupported voltage key.\n");
468 return CS_BAD_TYPE;
469 }
Daniel Ritzfa912bc2005-06-23 00:10:12 -0700470
471 if (skt->power_hook)
472 skt->power_hook(skt, HOOK_POWER_PRE);
473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 skt->socket.flags = 0;
475 skt->ops->set_socket(skt, &skt->socket);
476
477 /*
478 * Wait "vcc_settle" for the supply to stabilise.
479 */
480 msleep(vcc_settle * 10);
481
482 skt->ops->get_status(skt, &status);
483 if (!(status & SS_POWERON)) {
484 cs_err(skt, "unable to apply power.\n");
485 return CS_BAD_TYPE;
486 }
487
Daniel Ritzfa912bc2005-06-23 00:10:12 -0700488 status = socket_reset(skt);
489
490 if (skt->power_hook)
491 skt->power_hook(skt, HOOK_POWER_POST);
492
493 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494}
495
496/*
497 * Handle card insertion. Setup the socket, reset the card,
498 * and then tell the rest of PCMCIA that a card is present.
499 */
500static int socket_insert(struct pcmcia_socket *skt)
501{
502 int ret;
503
504 cs_dbg(skt, 4, "insert\n");
505
506 if (!cs_socket_get(skt))
507 return CS_NO_CARD;
508
509 ret = socket_setup(skt, setup_delay);
510 if (ret == CS_SUCCESS) {
511 skt->state |= SOCKET_PRESENT;
512#ifdef CONFIG_CARDBUS
513 if (skt->state & SOCKET_CARDBUS) {
514 cb_alloc(skt);
515 skt->state |= SOCKET_CARDBUS_CONFIG;
516 }
517#endif
518 cs_dbg(skt, 4, "insert done\n");
519
520 send_event(skt, CS_EVENT_CARD_INSERTION, CS_EVENT_PRI_LOW);
521 } else {
522 socket_shutdown(skt);
523 cs_socket_put(skt);
524 }
525
526 return ret;
527}
528
529static int socket_suspend(struct pcmcia_socket *skt)
530{
531 if (skt->state & SOCKET_SUSPEND)
532 return CS_IN_USE;
533
534 send_event(skt, CS_EVENT_PM_SUSPEND, CS_EVENT_PRI_LOW);
535 skt->socket = dead_socket;
536 skt->ops->set_socket(skt, &skt->socket);
537 if (skt->ops->suspend)
538 skt->ops->suspend(skt);
539 skt->state |= SOCKET_SUSPEND;
540
541 return CS_SUCCESS;
542}
543
544/*
545 * Resume a socket. If a card is present, verify its CIS against
546 * our cached copy. If they are different, the card has been
547 * replaced, and we need to tell the drivers.
548 */
549static int socket_resume(struct pcmcia_socket *skt)
550{
551 int ret;
552
553 if (!(skt->state & SOCKET_SUSPEND))
554 return CS_IN_USE;
555
556 skt->socket = dead_socket;
557 skt->ops->init(skt);
558 skt->ops->set_socket(skt, &skt->socket);
559
560 if (!(skt->state & SOCKET_PRESENT)) {
561 skt->state &= ~SOCKET_SUSPEND;
562 return socket_insert(skt);
563 }
564
565 ret = socket_setup(skt, resume_delay);
566 if (ret == CS_SUCCESS) {
567 /*
568 * FIXME: need a better check here for cardbus cards.
569 */
570 if (verify_cis_cache(skt) != 0) {
571 cs_dbg(skt, 4, "cis mismatch - different card\n");
572 socket_remove_drivers(skt);
573 destroy_cis_cache(skt);
574 /*
575 * Workaround: give DS time to schedule removal.
576 * Remove me once the 100ms delay is eliminated
577 * in ds.c
578 */
579 msleep(200);
580 send_event(skt, CS_EVENT_CARD_INSERTION, CS_EVENT_PRI_LOW);
581 } else {
582 cs_dbg(skt, 4, "cis matches cache\n");
583 send_event(skt, CS_EVENT_PM_RESUME, CS_EVENT_PRI_LOW);
584 }
585 } else {
586 socket_shutdown(skt);
587 cs_socket_put(skt);
588 }
589
590 skt->state &= ~SOCKET_SUSPEND;
591
592 return CS_SUCCESS;
593}
594
595static void socket_remove(struct pcmcia_socket *skt)
596{
597 socket_shutdown(skt);
598 cs_socket_put(skt);
599}
600
601/*
602 * Process a socket card detect status change.
603 *
604 * If we don't have a card already present, delay the detect event for
605 * about 20ms (to be on the safe side) before reading the socket status.
606 *
607 * Some i82365-based systems send multiple SS_DETECT events during card
608 * insertion, and the "card present" status bit seems to bounce. This
609 * will probably be true with GPIO-based card detection systems after
610 * the product has aged.
611 */
612static void socket_detect_change(struct pcmcia_socket *skt)
613{
614 if (!(skt->state & SOCKET_SUSPEND)) {
615 int status;
616
617 if (!(skt->state & SOCKET_PRESENT))
618 msleep(20);
619
620 skt->ops->get_status(skt, &status);
621 if ((skt->state & SOCKET_PRESENT) &&
622 !(status & SS_DETECT))
623 socket_remove(skt);
624 if (!(skt->state & SOCKET_PRESENT) &&
625 (status & SS_DETECT))
626 socket_insert(skt);
627 }
628}
629
630static int pccardd(void *__skt)
631{
632 struct pcmcia_socket *skt = __skt;
633 DECLARE_WAITQUEUE(wait, current);
634 int ret;
635
636 daemonize("pccardd");
637
638 skt->thread = current;
639 skt->socket = dead_socket;
640 skt->ops->init(skt);
641 skt->ops->set_socket(skt, &skt->socket);
642
643 /* register with the device core */
644 ret = class_device_register(&skt->dev);
645 if (ret) {
646 printk(KERN_WARNING "PCMCIA: unable to register socket 0x%p\n",
647 skt);
648 skt->thread = NULL;
649 complete_and_exit(&skt->thread_done, 0);
650 }
651 complete(&skt->thread_done);
652
653 add_wait_queue(&skt->thread_wait, &wait);
654 for (;;) {
655 unsigned long flags;
656 unsigned int events;
657
658 set_current_state(TASK_INTERRUPTIBLE);
659
660 spin_lock_irqsave(&skt->thread_lock, flags);
661 events = skt->thread_events;
662 skt->thread_events = 0;
663 spin_unlock_irqrestore(&skt->thread_lock, flags);
664
665 if (events) {
666 down(&skt->skt_sem);
667 if (events & SS_DETECT)
668 socket_detect_change(skt);
669 if (events & SS_BATDEAD)
670 send_event(skt, CS_EVENT_BATTERY_DEAD, CS_EVENT_PRI_LOW);
671 if (events & SS_BATWARN)
672 send_event(skt, CS_EVENT_BATTERY_LOW, CS_EVENT_PRI_LOW);
673 if (events & SS_READY)
674 send_event(skt, CS_EVENT_READY_CHANGE, CS_EVENT_PRI_LOW);
675 up(&skt->skt_sem);
676 continue;
677 }
678
679 schedule();
Christoph Lameter3e1d1d22005-06-24 23:13:50 -0700680 try_to_freeze();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
682 if (!skt->thread)
683 break;
684 }
685 remove_wait_queue(&skt->thread_wait, &wait);
686
687 /* remove from the device core */
688 class_device_unregister(&skt->dev);
689
690 complete_and_exit(&skt->thread_done, 0);
691}
692
693/*
694 * Yenta (at least) probes interrupts before registering the socket and
695 * starting the handler thread.
696 */
697void pcmcia_parse_events(struct pcmcia_socket *s, u_int events)
698{
699 cs_dbg(s, 4, "parse_events: events %08x\n", events);
700 if (s->thread) {
701 spin_lock(&s->thread_lock);
702 s->thread_events |= events;
703 spin_unlock(&s->thread_lock);
704
705 wake_up(&s->thread_wait);
706 }
707} /* pcmcia_parse_events */
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700708EXPORT_SYMBOL(pcmcia_parse_events);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
711/* register pcmcia_callback */
712int pccard_register_pcmcia(struct pcmcia_socket *s, struct pcmcia_callback *c)
713{
714 int ret = 0;
715
716 /* s->skt_sem also protects s->callback */
717 down(&s->skt_sem);
718
719 if (c) {
720 /* registration */
721 if (s->callback) {
722 ret = -EBUSY;
723 goto err;
724 }
725
726 s->callback = c;
727
728 if ((s->state & (SOCKET_PRESENT|SOCKET_CARDBUS)) == SOCKET_PRESENT)
729 send_event(s, CS_EVENT_CARD_INSERTION, CS_EVENT_PRI_LOW);
730 } else
731 s->callback = NULL;
732 err:
733 up(&s->skt_sem);
734
735 return ret;
736}
737EXPORT_SYMBOL(pccard_register_pcmcia);
738
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700740/* I'm not sure which "reset" function this is supposed to use,
741 * but for now, it uses the low-level interface's reset, not the
742 * CIS register.
743 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
745int pccard_reset_card(struct pcmcia_socket *skt)
746{
747 int ret;
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700748
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 cs_dbg(skt, 1, "resetting socket\n");
750
751 down(&skt->skt_sem);
752 do {
753 if (!(skt->state & SOCKET_PRESENT)) {
754 ret = CS_NO_CARD;
755 break;
756 }
757 if (skt->state & SOCKET_SUSPEND) {
758 ret = CS_IN_USE;
759 break;
760 }
761 if (skt->state & SOCKET_CARDBUS) {
762 ret = CS_UNSUPPORTED_FUNCTION;
763 break;
764 }
765
766 ret = send_event(skt, CS_EVENT_RESET_REQUEST, CS_EVENT_PRI_LOW);
767 if (ret == 0) {
768 send_event(skt, CS_EVENT_RESET_PHYSICAL, CS_EVENT_PRI_LOW);
769 if (socket_reset(skt) == CS_SUCCESS)
770 send_event(skt, CS_EVENT_CARD_RESET, CS_EVENT_PRI_LOW);
771 }
772
773 ret = CS_SUCCESS;
774 } while (0);
775 up(&skt->skt_sem);
776
777 return ret;
778} /* reset_card */
779EXPORT_SYMBOL(pccard_reset_card);
780
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700782/* These shut down or wake up a socket. They are sort of user
783 * initiated versions of the APM suspend and resume actions.
784 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785int pcmcia_suspend_card(struct pcmcia_socket *skt)
786{
787 int ret;
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700788
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 cs_dbg(skt, 1, "suspending socket\n");
790
791 down(&skt->skt_sem);
792 do {
793 if (!(skt->state & SOCKET_PRESENT)) {
794 ret = CS_NO_CARD;
795 break;
796 }
797 if (skt->state & SOCKET_CARDBUS) {
798 ret = CS_UNSUPPORTED_FUNCTION;
799 break;
800 }
801 ret = socket_suspend(skt);
802 } while (0);
803 up(&skt->skt_sem);
804
805 return ret;
806} /* suspend_card */
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700807EXPORT_SYMBOL(pcmcia_suspend_card);
808
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
810int pcmcia_resume_card(struct pcmcia_socket *skt)
811{
812 int ret;
813
814 cs_dbg(skt, 1, "waking up socket\n");
815
816 down(&skt->skt_sem);
817 do {
818 if (!(skt->state & SOCKET_PRESENT)) {
819 ret = CS_NO_CARD;
820 break;
821 }
822 if (skt->state & SOCKET_CARDBUS) {
823 ret = CS_UNSUPPORTED_FUNCTION;
824 break;
825 }
826 ret = socket_resume(skt);
827 } while (0);
828 up(&skt->skt_sem);
829
830 return ret;
831} /* resume_card */
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700832EXPORT_SYMBOL(pcmcia_resume_card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700835/* These handle user requests to eject or insert a card. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836int pcmcia_eject_card(struct pcmcia_socket *skt)
837{
838 int ret;
839
840 cs_dbg(skt, 1, "user eject request\n");
841
842 down(&skt->skt_sem);
843 do {
844 if (!(skt->state & SOCKET_PRESENT)) {
845 ret = -ENODEV;
846 break;
847 }
848
849 ret = send_event(skt, CS_EVENT_EJECTION_REQUEST, CS_EVENT_PRI_LOW);
850 if (ret != 0) {
851 ret = -EINVAL;
852 break;
853 }
854
855 socket_remove(skt);
856 ret = 0;
857 } while (0);
858 up(&skt->skt_sem);
859
860 return ret;
861} /* eject_card */
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700862EXPORT_SYMBOL(pcmcia_eject_card);
863
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
865int pcmcia_insert_card(struct pcmcia_socket *skt)
866{
867 int ret;
868
869 cs_dbg(skt, 1, "user insert request\n");
870
871 down(&skt->skt_sem);
872 do {
873 if (skt->state & SOCKET_PRESENT) {
874 ret = -EBUSY;
875 break;
876 }
877 if (socket_insert(skt) == CS_NO_CARD) {
878 ret = -ENODEV;
879 break;
880 }
881 ret = 0;
882 } while (0);
883 up(&skt->skt_sem);
884
885 return ret;
886} /* insert_card */
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700887EXPORT_SYMBOL(pcmcia_insert_card);
888
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
Dominik Brodowski3ee13932005-06-27 16:28:05 -0700890static int pcmcia_socket_hotplug(struct class_device *dev, char **envp,
891 int num_envp, char *buffer, int buffer_size)
892{
893 struct pcmcia_socket *s = container_of(dev, struct pcmcia_socket, dev);
894 int i = 0, length = 0;
895
896 if (add_hotplug_env_var(envp, num_envp, &i, buffer, buffer_size,
897 &length, "SOCKET_NO=%u", s->sock))
898 return -ENOMEM;
899
900 envp[i] = NULL;
901
902 return 0;
903}
904
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
906struct class pcmcia_socket_class = {
907 .name = "pcmcia_socket",
Dominik Brodowski3ee13932005-06-27 16:28:05 -0700908 .hotplug = pcmcia_socket_hotplug,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 .release = pcmcia_release_socket,
910};
911EXPORT_SYMBOL(pcmcia_socket_class);
912
913
914static int __init init_pcmcia_cs(void)
915{
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700916 int ret = class_register(&pcmcia_socket_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 if (ret)
918 return (ret);
919 return class_interface_register(&pccard_sysfs_interface);
920}
921
922static void __exit exit_pcmcia_cs(void)
923{
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700924 class_interface_unregister(&pccard_sysfs_interface);
925 class_unregister(&pcmcia_socket_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926}
927
928subsys_initcall(init_pcmcia_cs);
929module_exit(exit_pcmcia_cs);
930