blob: 45cffbf285c2e2b4f54169a44a436150f05d27a2 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <pcmcia/cs_types.h>
37#include <pcmcia/ss.h>
38#include <pcmcia/cs.h>
39#include <pcmcia/bulkmem.h>
40#include <pcmcia/cistpl.h>
41#include <pcmcia/cisreg.h>
42#include <pcmcia/ds.h>
43#include "cs_internal.h"
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46/* Module parameters */
47
48MODULE_AUTHOR("David Hinds <dahinds@users.sourceforge.net>");
Dominik Brodowski33519dd2005-06-27 16:28:53 -070049MODULE_DESCRIPTION("Linux Kernel Card Services");
Linus Torvalds1da177e2005-04-16 15:20:36 -070050MODULE_LICENSE("GPL");
51
52#define INT_MODULE_PARM(n, v) static int n = v; module_param(n, int, 0444)
53
54INT_MODULE_PARM(setup_delay, 10); /* centiseconds */
55INT_MODULE_PARM(resume_delay, 20); /* centiseconds */
56INT_MODULE_PARM(shutdown_delay, 3); /* centiseconds */
57INT_MODULE_PARM(vcc_settle, 40); /* centiseconds */
58INT_MODULE_PARM(reset_time, 10); /* usecs */
59INT_MODULE_PARM(unreset_delay, 10); /* centiseconds */
60INT_MODULE_PARM(unreset_check, 10); /* centiseconds */
61INT_MODULE_PARM(unreset_limit, 30); /* unreset_check's */
62
63/* Access speed for attribute memory windows */
64INT_MODULE_PARM(cis_speed, 300); /* ns */
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#ifdef DEBUG
67static int pc_debug;
68
69module_param(pc_debug, int, 0644);
70
71int cs_debug_level(int level)
72{
73 return pc_debug > level;
74}
75#endif
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78socket_state_t dead_socket = {
79 .csc_mask = SS_DETECT,
80};
Dominik Brodowski33519dd2005-06-27 16:28:53 -070081EXPORT_SYMBOL(dead_socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83
84/* List of all sockets, protected by a rwsem */
85LIST_HEAD(pcmcia_socket_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086EXPORT_SYMBOL(pcmcia_socket_list);
Dominik Brodowski33519dd2005-06-27 16:28:53 -070087
88DECLARE_RWSEM(pcmcia_socket_list_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089EXPORT_SYMBOL(pcmcia_socket_list_rwsem);
90
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092/**
Dominik Brodowski33519dd2005-06-27 16:28:53 -070093 * Low-level PCMCIA socket drivers need to register with the PCCard
94 * core using pcmcia_register_socket.
95 *
96 * socket drivers are expected to use the following callbacks in their
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 * .drv struct:
98 * - pcmcia_socket_dev_suspend
99 * - pcmcia_socket_dev_resume
100 * These functions check for the appropriate struct pcmcia_soket arrays,
101 * and pass them to the low-level functions pcmcia_{suspend,resume}_socket
102 */
103static int socket_resume(struct pcmcia_socket *skt);
104static int socket_suspend(struct pcmcia_socket *skt);
105
106int pcmcia_socket_dev_suspend(struct device *dev, pm_message_t state)
107{
108 struct pcmcia_socket *socket;
109
110 down_read(&pcmcia_socket_list_rwsem);
111 list_for_each_entry(socket, &pcmcia_socket_list, socket_list) {
112 if (socket->dev.dev != dev)
113 continue;
114 down(&socket->skt_sem);
115 socket_suspend(socket);
116 up(&socket->skt_sem);
117 }
118 up_read(&pcmcia_socket_list_rwsem);
119
120 return 0;
121}
122EXPORT_SYMBOL(pcmcia_socket_dev_suspend);
123
124int pcmcia_socket_dev_resume(struct device *dev)
125{
126 struct pcmcia_socket *socket;
127
128 down_read(&pcmcia_socket_list_rwsem);
129 list_for_each_entry(socket, &pcmcia_socket_list, socket_list) {
130 if (socket->dev.dev != dev)
131 continue;
132 down(&socket->skt_sem);
133 socket_resume(socket);
134 up(&socket->skt_sem);
135 }
136 up_read(&pcmcia_socket_list_rwsem);
137
138 return 0;
139}
140EXPORT_SYMBOL(pcmcia_socket_dev_resume);
141
142
143struct pcmcia_socket * pcmcia_get_socket(struct pcmcia_socket *skt)
144{
145 struct class_device *cl_dev = class_device_get(&skt->dev);
146 if (!cl_dev)
147 return NULL;
148 skt = class_get_devdata(cl_dev);
149 if (!try_module_get(skt->owner)) {
150 class_device_put(&skt->dev);
151 return NULL;
152 }
153 return (skt);
154}
155EXPORT_SYMBOL(pcmcia_get_socket);
156
157
158void pcmcia_put_socket(struct pcmcia_socket *skt)
159{
160 module_put(skt->owner);
161 class_device_put(&skt->dev);
162}
163EXPORT_SYMBOL(pcmcia_put_socket);
164
165
166static void pcmcia_release_socket(struct class_device *class_dev)
167{
168 struct pcmcia_socket *socket = class_get_devdata(class_dev);
169
170 complete(&socket->socket_released);
171}
172
173static int pccardd(void *__skt);
174
175/**
176 * pcmcia_register_socket - add a new pcmcia socket device
177 */
178int pcmcia_register_socket(struct pcmcia_socket *socket)
179{
180 int ret;
181
182 if (!socket || !socket->ops || !socket->dev.dev || !socket->resource_ops)
183 return -EINVAL;
184
185 cs_dbg(socket, 0, "pcmcia_register_socket(0x%p)\n", socket->ops);
186
187 spin_lock_init(&socket->lock);
188
189 if (socket->resource_ops->init) {
190 ret = socket->resource_ops->init(socket);
191 if (ret)
192 return (ret);
193 }
194
195 /* try to obtain a socket number [yes, it gets ugly if we
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700196 * register more than 2^sizeof(unsigned int) pcmcia
197 * sockets... but the socket number is deprecated
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 * anyways, so I don't care] */
199 down_write(&pcmcia_socket_list_rwsem);
200 if (list_empty(&pcmcia_socket_list))
201 socket->sock = 0;
202 else {
203 unsigned int found, i = 1;
204 struct pcmcia_socket *tmp;
205 do {
206 found = 1;
207 list_for_each_entry(tmp, &pcmcia_socket_list, socket_list) {
208 if (tmp->sock == i)
209 found = 0;
210 }
211 i++;
212 } while (!found);
213 socket->sock = i - 1;
214 }
215 list_add_tail(&socket->socket_list, &pcmcia_socket_list);
216 up_write(&pcmcia_socket_list_rwsem);
217
Russell King5bc6b682005-07-07 17:59:07 -0700218#ifndef CONFIG_CARDBUS
219 /*
220 * If we do not support Cardbus, ensure that
221 * the Cardbus socket capability is disabled.
222 */
223 socket->features &= ~SS_CAP_CARDBUS;
224#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226 /* set proper values in socket->dev */
227 socket->dev.class_data = socket;
228 socket->dev.class = &pcmcia_socket_class;
229 snprintf(socket->dev.class_id, BUS_ID_SIZE, "pcmcia_socket%u", socket->sock);
230
231 /* base address = 0, map = 0 */
232 socket->cis_mem.flags = 0;
233 socket->cis_mem.speed = cis_speed;
234
235 INIT_LIST_HEAD(&socket->cis_cache);
236
237 init_completion(&socket->socket_released);
238 init_completion(&socket->thread_done);
239 init_waitqueue_head(&socket->thread_wait);
240 init_MUTEX(&socket->skt_sem);
241 spin_lock_init(&socket->thread_lock);
242
243 ret = kernel_thread(pccardd, socket, CLONE_KERNEL);
244 if (ret < 0)
245 goto err;
246
247 wait_for_completion(&socket->thread_done);
248 if(!socket->thread) {
249 printk(KERN_WARNING "PCMCIA: warning: socket thread for socket %p did not start\n", socket);
250 return -EIO;
251 }
252 pcmcia_parse_events(socket, SS_DETECT);
253
254 return 0;
255
256 err:
257 down_write(&pcmcia_socket_list_rwsem);
258 list_del(&socket->socket_list);
259 up_write(&pcmcia_socket_list_rwsem);
260 return ret;
261} /* pcmcia_register_socket */
262EXPORT_SYMBOL(pcmcia_register_socket);
263
264
265/**
266 * pcmcia_unregister_socket - remove a pcmcia socket device
267 */
268void pcmcia_unregister_socket(struct pcmcia_socket *socket)
269{
270 if (!socket)
271 return;
272
273 cs_dbg(socket, 0, "pcmcia_unregister_socket(0x%p)\n", socket->ops);
274
275 if (socket->thread) {
276 init_completion(&socket->thread_done);
277 socket->thread = NULL;
278 wake_up(&socket->thread_wait);
279 wait_for_completion(&socket->thread_done);
280 }
281 release_cis_mem(socket);
282
283 /* remove from our own list */
284 down_write(&pcmcia_socket_list_rwsem);
285 list_del(&socket->socket_list);
286 up_write(&pcmcia_socket_list_rwsem);
287
288 /* wait for sysfs to drop all references */
289 release_resource_db(socket);
290 wait_for_completion(&socket->socket_released);
291} /* pcmcia_unregister_socket */
292EXPORT_SYMBOL(pcmcia_unregister_socket);
293
294
295struct pcmcia_socket * pcmcia_get_socket_by_nr(unsigned int nr)
296{
297 struct pcmcia_socket *s;
298
299 down_read(&pcmcia_socket_list_rwsem);
300 list_for_each_entry(s, &pcmcia_socket_list, socket_list)
301 if (s->sock == nr) {
302 up_read(&pcmcia_socket_list_rwsem);
303 return s;
304 }
305 up_read(&pcmcia_socket_list_rwsem);
306
307 return NULL;
308
309}
310EXPORT_SYMBOL(pcmcia_get_socket_by_nr);
311
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700312/**
313 * The central event handler. Send_event() sends an event to the
314 * 16-bit subsystem, which then calls the relevant device drivers.
315 * Parse_events() interprets the event bits from
316 * a card status change report. Do_shutdown() handles the high
317 * priority stuff associated with a card removal.
318 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
320/* NOTE: send_event needs to be called with skt->sem held. */
321
322static int send_event(struct pcmcia_socket *s, event_t event, int priority)
323{
324 int ret;
325
326 if (s->state & SOCKET_CARDBUS)
327 return 0;
328
329 cs_dbg(s, 1, "send_event(event %d, pri %d, callback 0x%p)\n",
330 event, priority, s->callback);
331
332 if (!s->callback)
333 return 0;
334 if (!try_module_get(s->callback->owner))
335 return 0;
336
337 ret = s->callback->event(s, event, priority);
338
339 module_put(s->callback->owner);
340
341 return ret;
342}
343
344static void socket_remove_drivers(struct pcmcia_socket *skt)
345{
346 cs_dbg(skt, 4, "remove_drivers\n");
347
348 send_event(skt, CS_EVENT_CARD_REMOVAL, CS_EVENT_PRI_HIGH);
349}
350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351static int socket_reset(struct pcmcia_socket *skt)
352{
353 int status, i;
354
355 cs_dbg(skt, 4, "reset\n");
356
357 skt->socket.flags |= SS_OUTPUT_ENA | SS_RESET;
358 skt->ops->set_socket(skt, &skt->socket);
359 udelay((long)reset_time);
360
361 skt->socket.flags &= ~SS_RESET;
362 skt->ops->set_socket(skt, &skt->socket);
363
364 msleep(unreset_delay * 10);
365 for (i = 0; i < unreset_limit; i++) {
366 skt->ops->get_status(skt, &status);
367
368 if (!(status & SS_DETECT))
369 return CS_NO_CARD;
370
371 if (status & SS_READY)
372 return CS_SUCCESS;
373
374 msleep(unreset_check * 10);
375 }
376
377 cs_err(skt, "time out after reset.\n");
378 return CS_GENERAL_FAILURE;
379}
380
Daniel Ritz6e493882005-12-30 15:12:35 +0100381/**
382 * socket_setup() and socket_shutdown() are called by the main event handler
383 * when card insertion and removal events are received.
384 * socket_setup() turns on socket power and resets the socket, in two stages.
385 * socket_shutdown() unconfigures a socket and turns off socket power.
386 */
387static void socket_shutdown(struct pcmcia_socket *s)
388{
389 int status;
390
391 cs_dbg(s, 4, "shutdown\n");
392
393 socket_remove_drivers(s);
394 s->state &= SOCKET_INUSE | SOCKET_PRESENT;
395 msleep(shutdown_delay * 10);
396 s->state &= SOCKET_INUSE;
397
398 /* Blank out the socket state */
399 s->socket = dead_socket;
400 s->ops->init(s);
401 s->ops->set_socket(s, &s->socket);
402 s->irq.AssignedIRQ = s->irq.Config = 0;
403 s->lock_count = 0;
404 destroy_cis_cache(s);
405#ifdef CONFIG_CARDBUS
406 cb_free(s);
407#endif
408 s->functions = 0;
Daniel Ritz6e493882005-12-30 15:12:35 +0100409
410 s->ops->get_status(s, &status);
411 if (status & SS_POWERON) {
412 printk(KERN_ERR "PCMCIA: socket %p: *** DANGER *** unable to remove socket power\n", s);
413 }
414
415 cs_socket_put(s);
416}
417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418static int socket_setup(struct pcmcia_socket *skt, int initial_delay)
419{
420 int status, i;
421
422 cs_dbg(skt, 4, "setup\n");
423
424 skt->ops->get_status(skt, &status);
425 if (!(status & SS_DETECT))
426 return CS_NO_CARD;
427
428 msleep(initial_delay * 10);
429
430 for (i = 0; i < 100; i++) {
431 skt->ops->get_status(skt, &status);
432 if (!(status & SS_DETECT))
433 return CS_NO_CARD;
434
435 if (!(status & SS_PENDING))
436 break;
437
438 msleep(100);
439 }
440
441 if (status & SS_PENDING) {
442 cs_err(skt, "voltage interrogation timed out.\n");
443 return CS_GENERAL_FAILURE;
444 }
445
446 if (status & SS_CARDBUS) {
Russell King5bc6b682005-07-07 17:59:07 -0700447 if (!(skt->features & SS_CAP_CARDBUS)) {
448 cs_err(skt, "cardbus cards are not supported.\n");
449 return CS_BAD_TYPE;
450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 skt->state |= SOCKET_CARDBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 }
453
454 /*
455 * Decode the card voltage requirements, and apply power to the card.
456 */
457 if (status & SS_3VCARD)
458 skt->socket.Vcc = skt->socket.Vpp = 33;
459 else if (!(status & SS_XVCARD))
460 skt->socket.Vcc = skt->socket.Vpp = 50;
461 else {
462 cs_err(skt, "unsupported voltage key.\n");
463 return CS_BAD_TYPE;
464 }
Daniel Ritzfa912bc2005-06-23 00:10:12 -0700465
466 if (skt->power_hook)
467 skt->power_hook(skt, HOOK_POWER_PRE);
468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 skt->socket.flags = 0;
470 skt->ops->set_socket(skt, &skt->socket);
471
472 /*
473 * Wait "vcc_settle" for the supply to stabilise.
474 */
475 msleep(vcc_settle * 10);
476
477 skt->ops->get_status(skt, &status);
478 if (!(status & SS_POWERON)) {
479 cs_err(skt, "unable to apply power.\n");
480 return CS_BAD_TYPE;
481 }
482
Daniel Ritzfa912bc2005-06-23 00:10:12 -0700483 status = socket_reset(skt);
484
485 if (skt->power_hook)
486 skt->power_hook(skt, HOOK_POWER_POST);
487
488 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489}
490
491/*
492 * Handle card insertion. Setup the socket, reset the card,
493 * and then tell the rest of PCMCIA that a card is present.
494 */
495static int socket_insert(struct pcmcia_socket *skt)
496{
497 int ret;
498
499 cs_dbg(skt, 4, "insert\n");
500
501 if (!cs_socket_get(skt))
502 return CS_NO_CARD;
503
504 ret = socket_setup(skt, setup_delay);
505 if (ret == CS_SUCCESS) {
506 skt->state |= SOCKET_PRESENT;
Dominik Brodowski807277c2005-11-12 23:34:06 +0100507
508 printk(KERN_NOTICE "pccard: %s card inserted into slot %d\n",
509 (skt->state & SOCKET_CARDBUS) ? "CardBus" : "PCMCIA",
510 skt->sock);
511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512#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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 }
524
525 return ret;
526}
527
528static int socket_suspend(struct pcmcia_socket *skt)
529{
530 if (skt->state & SOCKET_SUSPEND)
531 return CS_IN_USE;
532
533 send_event(skt, CS_EVENT_PM_SUSPEND, CS_EVENT_PRI_LOW);
534 skt->socket = dead_socket;
535 skt->ops->set_socket(skt, &skt->socket);
536 if (skt->ops->suspend)
537 skt->ops->suspend(skt);
538 skt->state |= SOCKET_SUSPEND;
539
540 return CS_SUCCESS;
541}
542
543/*
544 * Resume a socket. If a card is present, verify its CIS against
545 * our cached copy. If they are different, the card has been
546 * replaced, and we need to tell the drivers.
547 */
548static int socket_resume(struct pcmcia_socket *skt)
549{
550 int ret;
551
552 if (!(skt->state & SOCKET_SUSPEND))
553 return CS_IN_USE;
554
555 skt->socket = dead_socket;
556 skt->ops->init(skt);
557 skt->ops->set_socket(skt, &skt->socket);
558
559 if (!(skt->state & SOCKET_PRESENT)) {
560 skt->state &= ~SOCKET_SUSPEND;
561 return socket_insert(skt);
562 }
563
564 ret = socket_setup(skt, resume_delay);
565 if (ret == CS_SUCCESS) {
566 /*
567 * FIXME: need a better check here for cardbus cards.
568 */
569 if (verify_cis_cache(skt) != 0) {
570 cs_dbg(skt, 4, "cis mismatch - different card\n");
571 socket_remove_drivers(skt);
572 destroy_cis_cache(skt);
573 /*
574 * Workaround: give DS time to schedule removal.
575 * Remove me once the 100ms delay is eliminated
576 * in ds.c
577 */
578 msleep(200);
579 send_event(skt, CS_EVENT_CARD_INSERTION, CS_EVENT_PRI_LOW);
580 } else {
581 cs_dbg(skt, 4, "cis matches cache\n");
582 send_event(skt, CS_EVENT_PM_RESUME, CS_EVENT_PRI_LOW);
583 }
584 } else {
585 socket_shutdown(skt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 }
587
588 skt->state &= ~SOCKET_SUSPEND;
589
590 return CS_SUCCESS;
591}
592
593static void socket_remove(struct pcmcia_socket *skt)
594{
Dominik Brodowski807277c2005-11-12 23:34:06 +0100595 printk(KERN_NOTICE "pccard: card ejected from slot %d\n", skt->sock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 socket_shutdown(skt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597}
598
599/*
600 * Process a socket card detect status change.
601 *
602 * If we don't have a card already present, delay the detect event for
603 * about 20ms (to be on the safe side) before reading the socket status.
604 *
605 * Some i82365-based systems send multiple SS_DETECT events during card
606 * insertion, and the "card present" status bit seems to bounce. This
607 * will probably be true with GPIO-based card detection systems after
608 * the product has aged.
609 */
610static void socket_detect_change(struct pcmcia_socket *skt)
611{
612 if (!(skt->state & SOCKET_SUSPEND)) {
613 int status;
614
615 if (!(skt->state & SOCKET_PRESENT))
616 msleep(20);
617
618 skt->ops->get_status(skt, &status);
619 if ((skt->state & SOCKET_PRESENT) &&
620 !(status & SS_DETECT))
621 socket_remove(skt);
622 if (!(skt->state & SOCKET_PRESENT) &&
623 (status & SS_DETECT))
624 socket_insert(skt);
625 }
626}
627
628static int pccardd(void *__skt)
629{
630 struct pcmcia_socket *skt = __skt;
631 DECLARE_WAITQUEUE(wait, current);
632 int ret;
633
634 daemonize("pccardd");
635
636 skt->thread = current;
637 skt->socket = dead_socket;
638 skt->ops->init(skt);
639 skt->ops->set_socket(skt, &skt->socket);
640
641 /* register with the device core */
642 ret = class_device_register(&skt->dev);
643 if (ret) {
644 printk(KERN_WARNING "PCMCIA: unable to register socket 0x%p\n",
645 skt);
646 skt->thread = NULL;
647 complete_and_exit(&skt->thread_done, 0);
648 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
650 add_wait_queue(&skt->thread_wait, &wait);
Daniel Ritzbf4de6f2005-09-09 13:03:23 -0700651 complete(&skt->thread_done);
652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 for (;;) {
654 unsigned long flags;
655 unsigned int events;
656
657 set_current_state(TASK_INTERRUPTIBLE);
658
659 spin_lock_irqsave(&skt->thread_lock, flags);
660 events = skt->thread_events;
661 skt->thread_events = 0;
662 spin_unlock_irqrestore(&skt->thread_lock, flags);
663
664 if (events) {
665 down(&skt->skt_sem);
666 if (events & SS_DETECT)
667 socket_detect_change(skt);
668 if (events & SS_BATDEAD)
669 send_event(skt, CS_EVENT_BATTERY_DEAD, CS_EVENT_PRI_LOW);
670 if (events & SS_BATWARN)
671 send_event(skt, CS_EVENT_BATTERY_LOW, CS_EVENT_PRI_LOW);
672 if (events & SS_READY)
673 send_event(skt, CS_EVENT_READY_CHANGE, CS_EVENT_PRI_LOW);
674 up(&skt->skt_sem);
675 continue;
676 }
677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 if (!skt->thread)
679 break;
Daniel Ritzbf4de6f2005-09-09 13:03:23 -0700680
681 schedule();
682 try_to_freeze();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 }
Steven Rostedt220ec022005-10-10 11:13:17 -0400684 /* make sure we are running before we exit */
685 set_current_state(TASK_RUNNING);
686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 remove_wait_queue(&skt->thread_wait, &wait);
688
689 /* remove from the device core */
690 class_device_unregister(&skt->dev);
691
692 complete_and_exit(&skt->thread_done, 0);
693}
694
695/*
696 * Yenta (at least) probes interrupts before registering the socket and
697 * starting the handler thread.
698 */
699void pcmcia_parse_events(struct pcmcia_socket *s, u_int events)
700{
701 cs_dbg(s, 4, "parse_events: events %08x\n", events);
702 if (s->thread) {
703 spin_lock(&s->thread_lock);
704 s->thread_events |= events;
705 spin_unlock(&s->thread_lock);
706
707 wake_up(&s->thread_wait);
708 }
709} /* pcmcia_parse_events */
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700710EXPORT_SYMBOL(pcmcia_parse_events);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
713/* register pcmcia_callback */
714int pccard_register_pcmcia(struct pcmcia_socket *s, struct pcmcia_callback *c)
715{
716 int ret = 0;
717
718 /* s->skt_sem also protects s->callback */
719 down(&s->skt_sem);
720
721 if (c) {
722 /* registration */
723 if (s->callback) {
724 ret = -EBUSY;
725 goto err;
726 }
727
728 s->callback = c;
729
730 if ((s->state & (SOCKET_PRESENT|SOCKET_CARDBUS)) == SOCKET_PRESENT)
731 send_event(s, CS_EVENT_CARD_INSERTION, CS_EVENT_PRI_LOW);
732 } else
733 s->callback = NULL;
734 err:
735 up(&s->skt_sem);
736
737 return ret;
738}
739EXPORT_SYMBOL(pccard_register_pcmcia);
740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700742/* I'm not sure which "reset" function this is supposed to use,
743 * but for now, it uses the low-level interface's reset, not the
744 * CIS register.
745 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
747int pccard_reset_card(struct pcmcia_socket *skt)
748{
749 int ret;
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700750
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 cs_dbg(skt, 1, "resetting socket\n");
752
753 down(&skt->skt_sem);
754 do {
755 if (!(skt->state & SOCKET_PRESENT)) {
756 ret = CS_NO_CARD;
757 break;
758 }
759 if (skt->state & SOCKET_SUSPEND) {
760 ret = CS_IN_USE;
761 break;
762 }
763 if (skt->state & SOCKET_CARDBUS) {
764 ret = CS_UNSUPPORTED_FUNCTION;
765 break;
766 }
767
768 ret = send_event(skt, CS_EVENT_RESET_REQUEST, CS_EVENT_PRI_LOW);
769 if (ret == 0) {
770 send_event(skt, CS_EVENT_RESET_PHYSICAL, CS_EVENT_PRI_LOW);
Dominik Brodowski8e9e7932006-01-06 00:02:03 +0100771 if (skt->callback)
772 skt->callback->suspend(skt);
773 if (socket_reset(skt) == CS_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 send_event(skt, CS_EVENT_CARD_RESET, CS_EVENT_PRI_LOW);
Dominik Brodowski8e9e7932006-01-06 00:02:03 +0100775 if (skt->callback)
776 skt->callback->resume(skt);
777 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 }
779
780 ret = CS_SUCCESS;
781 } while (0);
782 up(&skt->skt_sem);
783
784 return ret;
785} /* reset_card */
786EXPORT_SYMBOL(pccard_reset_card);
787
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700789/* These shut down or wake up a socket. They are sort of user
790 * initiated versions of the APM suspend and resume actions.
791 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792int pcmcia_suspend_card(struct pcmcia_socket *skt)
793{
794 int ret;
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700795
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 cs_dbg(skt, 1, "suspending socket\n");
797
798 down(&skt->skt_sem);
799 do {
800 if (!(skt->state & SOCKET_PRESENT)) {
801 ret = CS_NO_CARD;
802 break;
803 }
804 if (skt->state & SOCKET_CARDBUS) {
805 ret = CS_UNSUPPORTED_FUNCTION;
806 break;
807 }
Dominik Brodowski8e9e7932006-01-06 00:02:03 +0100808 if (skt->callback) {
809 ret = skt->callback->suspend(skt);
810 if (ret)
811 break;
812 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 ret = socket_suspend(skt);
814 } while (0);
815 up(&skt->skt_sem);
816
817 return ret;
818} /* suspend_card */
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700819EXPORT_SYMBOL(pcmcia_suspend_card);
820
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
822int pcmcia_resume_card(struct pcmcia_socket *skt)
823{
824 int ret;
825
826 cs_dbg(skt, 1, "waking up socket\n");
827
828 down(&skt->skt_sem);
829 do {
830 if (!(skt->state & SOCKET_PRESENT)) {
831 ret = CS_NO_CARD;
832 break;
833 }
834 if (skt->state & SOCKET_CARDBUS) {
835 ret = CS_UNSUPPORTED_FUNCTION;
836 break;
837 }
838 ret = socket_resume(skt);
Dominik Brodowski8e9e7932006-01-06 00:02:03 +0100839 if (!ret && skt->callback)
840 skt->callback->resume(skt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 } while (0);
842 up(&skt->skt_sem);
843
844 return ret;
845} /* resume_card */
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700846EXPORT_SYMBOL(pcmcia_resume_card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700849/* These handle user requests to eject or insert a card. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850int pcmcia_eject_card(struct pcmcia_socket *skt)
851{
852 int ret;
853
854 cs_dbg(skt, 1, "user eject request\n");
855
856 down(&skt->skt_sem);
857 do {
858 if (!(skt->state & SOCKET_PRESENT)) {
859 ret = -ENODEV;
860 break;
861 }
862
863 ret = send_event(skt, CS_EVENT_EJECTION_REQUEST, CS_EVENT_PRI_LOW);
864 if (ret != 0) {
865 ret = -EINVAL;
866 break;
867 }
868
869 socket_remove(skt);
870 ret = 0;
871 } while (0);
872 up(&skt->skt_sem);
873
874 return ret;
875} /* eject_card */
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700876EXPORT_SYMBOL(pcmcia_eject_card);
877
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
879int pcmcia_insert_card(struct pcmcia_socket *skt)
880{
881 int ret;
882
883 cs_dbg(skt, 1, "user insert request\n");
884
885 down(&skt->skt_sem);
886 do {
887 if (skt->state & SOCKET_PRESENT) {
888 ret = -EBUSY;
889 break;
890 }
891 if (socket_insert(skt) == CS_NO_CARD) {
892 ret = -ENODEV;
893 break;
894 }
895 ret = 0;
896 } while (0);
897 up(&skt->skt_sem);
898
899 return ret;
900} /* insert_card */
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700901EXPORT_SYMBOL(pcmcia_insert_card);
902
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
Kay Sievers312c0042005-11-16 09:00:00 +0100904static int pcmcia_socket_uevent(struct class_device *dev, char **envp,
905 int num_envp, char *buffer, int buffer_size)
Dominik Brodowski3ee13932005-06-27 16:28:05 -0700906{
907 struct pcmcia_socket *s = container_of(dev, struct pcmcia_socket, dev);
908 int i = 0, length = 0;
909
Kay Sievers312c0042005-11-16 09:00:00 +0100910 if (add_uevent_var(envp, num_envp, &i, buffer, buffer_size,
911 &length, "SOCKET_NO=%u", s->sock))
Dominik Brodowski3ee13932005-06-27 16:28:05 -0700912 return -ENOMEM;
913
914 envp[i] = NULL;
915
916 return 0;
917}
918
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
Dominik Brodowski378a33a2005-06-27 16:28:54 -0700920static struct completion pcmcia_unload;
921
922static void pcmcia_release_socket_class(struct class *data)
923{
924 complete(&pcmcia_unload);
925}
926
927
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928struct class pcmcia_socket_class = {
929 .name = "pcmcia_socket",
Kay Sievers312c0042005-11-16 09:00:00 +0100930 .uevent = pcmcia_socket_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 .release = pcmcia_release_socket,
Dominik Brodowski378a33a2005-06-27 16:28:54 -0700932 .class_release = pcmcia_release_socket_class,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933};
934EXPORT_SYMBOL(pcmcia_socket_class);
935
936
937static int __init init_pcmcia_cs(void)
938{
Dominik Brodowski378a33a2005-06-27 16:28:54 -0700939 int ret;
940
941 init_completion(&pcmcia_unload);
942 ret = class_register(&pcmcia_socket_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 if (ret)
944 return (ret);
945 return class_interface_register(&pccard_sysfs_interface);
946}
947
948static void __exit exit_pcmcia_cs(void)
949{
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700950 class_interface_unregister(&pccard_sysfs_interface);
951 class_unregister(&pcmcia_socket_class);
Dominik Brodowski378a33a2005-06-27 16:28:54 -0700952
953 wait_for_completion(&pcmcia_unload);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954}
955
956subsys_initcall(init_pcmcia_cs);
957module_exit(exit_pcmcia_cs);
958