blob: 06e2cda4e07b8fe31e17f5c4c2013b163e105c2f [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#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/pci.h>
30#include <linux/device.h>
Serge E. Hallyn1da9ab72006-06-14 08:01:26 -050031#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#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;
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100114 mutex_lock(&socket->skt_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 socket_suspend(socket);
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100116 mutex_unlock(&socket->skt_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 }
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;
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100132 mutex_lock(&socket->skt_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 socket_resume(socket);
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100134 mutex_unlock(&socket->skt_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 }
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{
Serge E. Hallyn1da9ab72006-06-14 08:01:26 -0500180 struct task_struct *tsk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 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
Russell King5bc6b682005-07-07 17:59:07 -0700219#ifndef CONFIG_CARDBUS
220 /*
221 * If we do not support Cardbus, ensure that
222 * the Cardbus socket capability is disabled.
223 */
224 socket->features &= ~SS_CAP_CARDBUS;
225#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227 /* set proper values in socket->dev */
228 socket->dev.class_data = socket;
229 socket->dev.class = &pcmcia_socket_class;
230 snprintf(socket->dev.class_id, BUS_ID_SIZE, "pcmcia_socket%u", socket->sock);
231
232 /* base address = 0, map = 0 */
233 socket->cis_mem.flags = 0;
234 socket->cis_mem.speed = cis_speed;
235
236 INIT_LIST_HEAD(&socket->cis_cache);
237
238 init_completion(&socket->socket_released);
239 init_completion(&socket->thread_done);
240 init_waitqueue_head(&socket->thread_wait);
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100241 mutex_init(&socket->skt_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 spin_lock_init(&socket->thread_lock);
243
Serge E. Hallyn1da9ab72006-06-14 08:01:26 -0500244 tsk = kthread_run(pccardd, socket, "pccardd");
245 if (IS_ERR(tsk)) {
246 ret = PTR_ERR(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 goto err;
Serge E. Hallyn1da9ab72006-06-14 08:01:26 -0500248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250 wait_for_completion(&socket->thread_done);
Serge E. Hallyn1da9ab72006-06-14 08:01:26 -0500251 if (!socket->thread) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 printk(KERN_WARNING "PCMCIA: warning: socket thread for socket %p did not start\n", socket);
253 return -EIO;
254 }
Serge E. Hallyn1da9ab72006-06-14 08:01:26 -0500255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 pcmcia_parse_events(socket, SS_DETECT);
257
258 return 0;
259
260 err:
261 down_write(&pcmcia_socket_list_rwsem);
262 list_del(&socket->socket_list);
263 up_write(&pcmcia_socket_list_rwsem);
264 return ret;
265} /* pcmcia_register_socket */
266EXPORT_SYMBOL(pcmcia_register_socket);
267
268
269/**
270 * pcmcia_unregister_socket - remove a pcmcia socket device
271 */
272void pcmcia_unregister_socket(struct pcmcia_socket *socket)
273{
274 if (!socket)
275 return;
276
277 cs_dbg(socket, 0, "pcmcia_unregister_socket(0x%p)\n", socket->ops);
278
279 if (socket->thread) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 wake_up(&socket->thread_wait);
Serge E. Hallyn1da9ab72006-06-14 08:01:26 -0500281 kthread_stop(socket->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 }
283 release_cis_mem(socket);
284
285 /* remove from our own list */
286 down_write(&pcmcia_socket_list_rwsem);
287 list_del(&socket->socket_list);
288 up_write(&pcmcia_socket_list_rwsem);
289
290 /* wait for sysfs to drop all references */
291 release_resource_db(socket);
292 wait_for_completion(&socket->socket_released);
293} /* pcmcia_unregister_socket */
294EXPORT_SYMBOL(pcmcia_unregister_socket);
295
296
297struct pcmcia_socket * pcmcia_get_socket_by_nr(unsigned int nr)
298{
299 struct pcmcia_socket *s;
300
301 down_read(&pcmcia_socket_list_rwsem);
302 list_for_each_entry(s, &pcmcia_socket_list, socket_list)
303 if (s->sock == nr) {
304 up_read(&pcmcia_socket_list_rwsem);
305 return s;
306 }
307 up_read(&pcmcia_socket_list_rwsem);
308
309 return NULL;
310
311}
312EXPORT_SYMBOL(pcmcia_get_socket_by_nr);
313
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700314/**
315 * The central event handler. Send_event() sends an event to the
316 * 16-bit subsystem, which then calls the relevant device drivers.
317 * Parse_events() interprets the event bits from
318 * a card status change report. Do_shutdown() handles the high
319 * priority stuff associated with a card removal.
320 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
322/* NOTE: send_event needs to be called with skt->sem held. */
323
324static int send_event(struct pcmcia_socket *s, event_t event, int priority)
325{
326 int ret;
327
328 if (s->state & SOCKET_CARDBUS)
329 return 0;
330
331 cs_dbg(s, 1, "send_event(event %d, pri %d, callback 0x%p)\n",
332 event, priority, s->callback);
333
334 if (!s->callback)
335 return 0;
336 if (!try_module_get(s->callback->owner))
337 return 0;
338
339 ret = s->callback->event(s, event, priority);
340
341 module_put(s->callback->owner);
342
343 return ret;
344}
345
346static void socket_remove_drivers(struct pcmcia_socket *skt)
347{
348 cs_dbg(skt, 4, "remove_drivers\n");
349
350 send_event(skt, CS_EVENT_CARD_REMOVAL, CS_EVENT_PRI_HIGH);
351}
352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353static int socket_reset(struct pcmcia_socket *skt)
354{
355 int status, i;
356
357 cs_dbg(skt, 4, "reset\n");
358
359 skt->socket.flags |= SS_OUTPUT_ENA | SS_RESET;
360 skt->ops->set_socket(skt, &skt->socket);
361 udelay((long)reset_time);
362
363 skt->socket.flags &= ~SS_RESET;
364 skt->ops->set_socket(skt, &skt->socket);
365
366 msleep(unreset_delay * 10);
367 for (i = 0; i < unreset_limit; i++) {
368 skt->ops->get_status(skt, &status);
369
370 if (!(status & SS_DETECT))
371 return CS_NO_CARD;
372
373 if (status & SS_READY)
374 return CS_SUCCESS;
375
376 msleep(unreset_check * 10);
377 }
378
379 cs_err(skt, "time out after reset.\n");
380 return CS_GENERAL_FAILURE;
381}
382
Daniel Ritz6e493882005-12-30 15:12:35 +0100383/**
384 * socket_setup() and socket_shutdown() are called by the main event handler
385 * when card insertion and removal events are received.
386 * socket_setup() turns on socket power and resets the socket, in two stages.
387 * socket_shutdown() unconfigures a socket and turns off socket power.
388 */
389static void socket_shutdown(struct pcmcia_socket *s)
390{
391 int status;
392
393 cs_dbg(s, 4, "shutdown\n");
394
395 socket_remove_drivers(s);
396 s->state &= SOCKET_INUSE | SOCKET_PRESENT;
397 msleep(shutdown_delay * 10);
398 s->state &= SOCKET_INUSE;
399
400 /* Blank out the socket state */
401 s->socket = dead_socket;
402 s->ops->init(s);
403 s->ops->set_socket(s, &s->socket);
404 s->irq.AssignedIRQ = s->irq.Config = 0;
405 s->lock_count = 0;
406 destroy_cis_cache(s);
407#ifdef CONFIG_CARDBUS
408 cb_free(s);
409#endif
410 s->functions = 0;
Daniel Ritz6e493882005-12-30 15:12:35 +0100411
412 s->ops->get_status(s, &status);
413 if (status & SS_POWERON) {
414 printk(KERN_ERR "PCMCIA: socket %p: *** DANGER *** unable to remove socket power\n", s);
415 }
416
417 cs_socket_put(s);
418}
419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420static int socket_setup(struct pcmcia_socket *skt, int initial_delay)
421{
422 int status, i;
423
424 cs_dbg(skt, 4, "setup\n");
425
426 skt->ops->get_status(skt, &status);
427 if (!(status & SS_DETECT))
428 return CS_NO_CARD;
429
430 msleep(initial_delay * 10);
431
432 for (i = 0; i < 100; i++) {
433 skt->ops->get_status(skt, &status);
434 if (!(status & SS_DETECT))
435 return CS_NO_CARD;
436
437 if (!(status & SS_PENDING))
438 break;
439
440 msleep(100);
441 }
442
443 if (status & SS_PENDING) {
444 cs_err(skt, "voltage interrogation timed out.\n");
445 return CS_GENERAL_FAILURE;
446 }
447
448 if (status & SS_CARDBUS) {
Russell King5bc6b682005-07-07 17:59:07 -0700449 if (!(skt->features & SS_CAP_CARDBUS)) {
450 cs_err(skt, "cardbus cards are not supported.\n");
451 return CS_BAD_TYPE;
452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 skt->state |= SOCKET_CARDBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 }
455
456 /*
457 * Decode the card voltage requirements, and apply power to the card.
458 */
459 if (status & SS_3VCARD)
460 skt->socket.Vcc = skt->socket.Vpp = 33;
461 else if (!(status & SS_XVCARD))
462 skt->socket.Vcc = skt->socket.Vpp = 50;
463 else {
464 cs_err(skt, "unsupported voltage key.\n");
465 return CS_BAD_TYPE;
466 }
Daniel Ritzfa912bc2005-06-23 00:10:12 -0700467
468 if (skt->power_hook)
469 skt->power_hook(skt, HOOK_POWER_PRE);
470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 skt->socket.flags = 0;
472 skt->ops->set_socket(skt, &skt->socket);
473
474 /*
475 * Wait "vcc_settle" for the supply to stabilise.
476 */
477 msleep(vcc_settle * 10);
478
479 skt->ops->get_status(skt, &status);
480 if (!(status & SS_POWERON)) {
481 cs_err(skt, "unable to apply power.\n");
482 return CS_BAD_TYPE;
483 }
484
Daniel Ritzfa912bc2005-06-23 00:10:12 -0700485 status = socket_reset(skt);
486
487 if (skt->power_hook)
488 skt->power_hook(skt, HOOK_POWER_POST);
489
490 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491}
492
493/*
494 * Handle card insertion. Setup the socket, reset the card,
495 * and then tell the rest of PCMCIA that a card is present.
496 */
497static int socket_insert(struct pcmcia_socket *skt)
498{
499 int ret;
500
501 cs_dbg(skt, 4, "insert\n");
502
503 if (!cs_socket_get(skt))
504 return CS_NO_CARD;
505
506 ret = socket_setup(skt, setup_delay);
507 if (ret == CS_SUCCESS) {
508 skt->state |= SOCKET_PRESENT;
Dominik Brodowski807277c2005-11-12 23:34:06 +0100509
510 printk(KERN_NOTICE "pccard: %s card inserted into slot %d\n",
511 (skt->state & SOCKET_CARDBUS) ? "CardBus" : "PCMCIA",
512 skt->sock);
513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514#ifdef CONFIG_CARDBUS
515 if (skt->state & SOCKET_CARDBUS) {
516 cb_alloc(skt);
517 skt->state |= SOCKET_CARDBUS_CONFIG;
518 }
519#endif
520 cs_dbg(skt, 4, "insert done\n");
521
522 send_event(skt, CS_EVENT_CARD_INSERTION, CS_EVENT_PRI_LOW);
523 } else {
524 socket_shutdown(skt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 }
526
527 return ret;
528}
529
530static int socket_suspend(struct pcmcia_socket *skt)
531{
532 if (skt->state & SOCKET_SUSPEND)
533 return CS_IN_USE;
534
535 send_event(skt, CS_EVENT_PM_SUSPEND, CS_EVENT_PRI_LOW);
536 skt->socket = dead_socket;
537 skt->ops->set_socket(skt, &skt->socket);
538 if (skt->ops->suspend)
539 skt->ops->suspend(skt);
540 skt->state |= SOCKET_SUSPEND;
541
542 return CS_SUCCESS;
543}
544
545/*
546 * Resume a socket. If a card is present, verify its CIS against
547 * our cached copy. If they are different, the card has been
548 * replaced, and we need to tell the drivers.
549 */
550static int socket_resume(struct pcmcia_socket *skt)
551{
552 int ret;
553
554 if (!(skt->state & SOCKET_SUSPEND))
555 return CS_IN_USE;
556
557 skt->socket = dead_socket;
558 skt->ops->init(skt);
559 skt->ops->set_socket(skt, &skt->socket);
560
561 if (!(skt->state & SOCKET_PRESENT)) {
562 skt->state &= ~SOCKET_SUSPEND;
563 return socket_insert(skt);
564 }
565
566 ret = socket_setup(skt, resume_delay);
567 if (ret == CS_SUCCESS) {
568 /*
569 * FIXME: need a better check here for cardbus cards.
570 */
571 if (verify_cis_cache(skt) != 0) {
572 cs_dbg(skt, 4, "cis mismatch - different card\n");
573 socket_remove_drivers(skt);
574 destroy_cis_cache(skt);
575 /*
576 * Workaround: give DS time to schedule removal.
577 * Remove me once the 100ms delay is eliminated
578 * in ds.c
579 */
580 msleep(200);
581 send_event(skt, CS_EVENT_CARD_INSERTION, CS_EVENT_PRI_LOW);
582 } else {
583 cs_dbg(skt, 4, "cis matches cache\n");
584 send_event(skt, CS_EVENT_PM_RESUME, CS_EVENT_PRI_LOW);
585 }
586 } else {
587 socket_shutdown(skt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 }
589
590 skt->state &= ~SOCKET_SUSPEND;
591
592 return CS_SUCCESS;
593}
594
595static void socket_remove(struct pcmcia_socket *skt)
596{
Dominik Brodowski807277c2005-11-12 23:34:06 +0100597 printk(KERN_NOTICE "pccard: card ejected from slot %d\n", skt->sock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 socket_shutdown(skt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599}
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 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;
Serge E. Hallyn1da9ab72006-06-14 08:01:26 -0500647 complete(&skt->thread_done);
648 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
651 add_wait_queue(&skt->thread_wait, &wait);
Daniel Ritzbf4de6f2005-09-09 13:03:23 -0700652 complete(&skt->thread_done);
653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 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) {
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100666 mutex_lock(&skt->skt_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 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);
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100675 mutex_unlock(&skt->skt_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 continue;
677 }
678
Serge E. Hallyn1da9ab72006-06-14 08:01:26 -0500679 if (kthread_should_stop())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 break;
Daniel Ritzbf4de6f2005-09-09 13:03:23 -0700681
682 schedule();
683 try_to_freeze();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 }
Steven Rostedt220ec022005-10-10 11:13:17 -0400685 /* make sure we are running before we exit */
686 set_current_state(TASK_RUNNING);
687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 remove_wait_queue(&skt->thread_wait, &wait);
689
690 /* remove from the device core */
691 class_device_unregister(&skt->dev);
692
Serge E. Hallyn1da9ab72006-06-14 08:01:26 -0500693 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694}
695
696/*
697 * Yenta (at least) probes interrupts before registering the socket and
698 * starting the handler thread.
699 */
700void pcmcia_parse_events(struct pcmcia_socket *s, u_int events)
701{
702 cs_dbg(s, 4, "parse_events: events %08x\n", events);
703 if (s->thread) {
704 spin_lock(&s->thread_lock);
705 s->thread_events |= events;
706 spin_unlock(&s->thread_lock);
707
708 wake_up(&s->thread_wait);
709 }
710} /* pcmcia_parse_events */
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700711EXPORT_SYMBOL(pcmcia_parse_events);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
714/* register pcmcia_callback */
715int pccard_register_pcmcia(struct pcmcia_socket *s, struct pcmcia_callback *c)
716{
717 int ret = 0;
718
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100719 /* s->skt_mutex also protects s->callback */
720 mutex_lock(&s->skt_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
722 if (c) {
723 /* registration */
724 if (s->callback) {
725 ret = -EBUSY;
726 goto err;
727 }
728
729 s->callback = c;
730
731 if ((s->state & (SOCKET_PRESENT|SOCKET_CARDBUS)) == SOCKET_PRESENT)
732 send_event(s, CS_EVENT_CARD_INSERTION, CS_EVENT_PRI_LOW);
733 } else
734 s->callback = NULL;
735 err:
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100736 mutex_unlock(&s->skt_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
738 return ret;
739}
740EXPORT_SYMBOL(pccard_register_pcmcia);
741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700743/* I'm not sure which "reset" function this is supposed to use,
744 * but for now, it uses the low-level interface's reset, not the
745 * CIS register.
746 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
748int pccard_reset_card(struct pcmcia_socket *skt)
749{
750 int ret;
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 cs_dbg(skt, 1, "resetting socket\n");
753
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100754 mutex_lock(&skt->skt_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 do {
756 if (!(skt->state & SOCKET_PRESENT)) {
757 ret = CS_NO_CARD;
758 break;
759 }
760 if (skt->state & SOCKET_SUSPEND) {
761 ret = CS_IN_USE;
762 break;
763 }
764 if (skt->state & SOCKET_CARDBUS) {
765 ret = CS_UNSUPPORTED_FUNCTION;
766 break;
767 }
768
769 ret = send_event(skt, CS_EVENT_RESET_REQUEST, CS_EVENT_PRI_LOW);
770 if (ret == 0) {
771 send_event(skt, CS_EVENT_RESET_PHYSICAL, CS_EVENT_PRI_LOW);
Dominik Brodowski8e9e7932006-01-06 00:02:03 +0100772 if (skt->callback)
773 skt->callback->suspend(skt);
774 if (socket_reset(skt) == CS_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 send_event(skt, CS_EVENT_CARD_RESET, CS_EVENT_PRI_LOW);
Dominik Brodowski8e9e7932006-01-06 00:02:03 +0100776 if (skt->callback)
777 skt->callback->resume(skt);
778 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 }
780
781 ret = CS_SUCCESS;
782 } while (0);
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100783 mutex_unlock(&skt->skt_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
785 return ret;
786} /* reset_card */
787EXPORT_SYMBOL(pccard_reset_card);
788
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700790/* These shut down or wake up a socket. They are sort of user
791 * initiated versions of the APM suspend and resume actions.
792 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793int pcmcia_suspend_card(struct pcmcia_socket *skt)
794{
795 int ret;
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700796
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 cs_dbg(skt, 1, "suspending socket\n");
798
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100799 mutex_lock(&skt->skt_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 do {
801 if (!(skt->state & SOCKET_PRESENT)) {
802 ret = CS_NO_CARD;
803 break;
804 }
805 if (skt->state & SOCKET_CARDBUS) {
806 ret = CS_UNSUPPORTED_FUNCTION;
807 break;
808 }
Dominik Brodowski8e9e7932006-01-06 00:02:03 +0100809 if (skt->callback) {
810 ret = skt->callback->suspend(skt);
811 if (ret)
812 break;
813 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 ret = socket_suspend(skt);
815 } while (0);
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100816 mutex_unlock(&skt->skt_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
818 return ret;
819} /* suspend_card */
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700820EXPORT_SYMBOL(pcmcia_suspend_card);
821
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
823int pcmcia_resume_card(struct pcmcia_socket *skt)
824{
825 int ret;
826
827 cs_dbg(skt, 1, "waking up socket\n");
828
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100829 mutex_lock(&skt->skt_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 do {
831 if (!(skt->state & SOCKET_PRESENT)) {
832 ret = CS_NO_CARD;
833 break;
834 }
835 if (skt->state & SOCKET_CARDBUS) {
836 ret = CS_UNSUPPORTED_FUNCTION;
837 break;
838 }
839 ret = socket_resume(skt);
Dominik Brodowski8e9e7932006-01-06 00:02:03 +0100840 if (!ret && skt->callback)
841 skt->callback->resume(skt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 } while (0);
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100843 mutex_unlock(&skt->skt_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
845 return ret;
846} /* resume_card */
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700847EXPORT_SYMBOL(pcmcia_resume_card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700850/* These handle user requests to eject or insert a card. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851int pcmcia_eject_card(struct pcmcia_socket *skt)
852{
853 int ret;
854
855 cs_dbg(skt, 1, "user eject request\n");
856
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100857 mutex_lock(&skt->skt_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 do {
859 if (!(skt->state & SOCKET_PRESENT)) {
860 ret = -ENODEV;
861 break;
862 }
863
864 ret = send_event(skt, CS_EVENT_EJECTION_REQUEST, CS_EVENT_PRI_LOW);
865 if (ret != 0) {
866 ret = -EINVAL;
867 break;
868 }
869
870 socket_remove(skt);
871 ret = 0;
872 } while (0);
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100873 mutex_unlock(&skt->skt_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
875 return ret;
876} /* eject_card */
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700877EXPORT_SYMBOL(pcmcia_eject_card);
878
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
880int pcmcia_insert_card(struct pcmcia_socket *skt)
881{
882 int ret;
883
884 cs_dbg(skt, 1, "user insert request\n");
885
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100886 mutex_lock(&skt->skt_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 do {
888 if (skt->state & SOCKET_PRESENT) {
889 ret = -EBUSY;
890 break;
891 }
892 if (socket_insert(skt) == CS_NO_CARD) {
893 ret = -ENODEV;
894 break;
895 }
896 ret = 0;
897 } while (0);
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100898 mutex_unlock(&skt->skt_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
900 return ret;
901} /* insert_card */
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700902EXPORT_SYMBOL(pcmcia_insert_card);
903
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
Kay Sievers312c0042005-11-16 09:00:00 +0100905static int pcmcia_socket_uevent(struct class_device *dev, char **envp,
906 int num_envp, char *buffer, int buffer_size)
Dominik Brodowski3ee13932005-06-27 16:28:05 -0700907{
908 struct pcmcia_socket *s = container_of(dev, struct pcmcia_socket, dev);
909 int i = 0, length = 0;
910
Kay Sievers312c0042005-11-16 09:00:00 +0100911 if (add_uevent_var(envp, num_envp, &i, buffer, buffer_size,
912 &length, "SOCKET_NO=%u", s->sock))
Dominik Brodowski3ee13932005-06-27 16:28:05 -0700913 return -ENOMEM;
914
915 envp[i] = NULL;
916
917 return 0;
918}
919
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
Dominik Brodowski378a33a2005-06-27 16:28:54 -0700921static struct completion pcmcia_unload;
922
923static void pcmcia_release_socket_class(struct class *data)
924{
925 complete(&pcmcia_unload);
926}
927
928
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929struct class pcmcia_socket_class = {
930 .name = "pcmcia_socket",
Kay Sievers312c0042005-11-16 09:00:00 +0100931 .uevent = pcmcia_socket_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 .release = pcmcia_release_socket,
Dominik Brodowski378a33a2005-06-27 16:28:54 -0700933 .class_release = pcmcia_release_socket_class,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934};
935EXPORT_SYMBOL(pcmcia_socket_class);
936
937
938static int __init init_pcmcia_cs(void)
939{
Dominik Brodowski378a33a2005-06-27 16:28:54 -0700940 int ret;
941
942 init_completion(&pcmcia_unload);
943 ret = class_register(&pcmcia_socket_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 if (ret)
945 return (ret);
946 return class_interface_register(&pccard_sysfs_interface);
947}
948
949static void __exit exit_pcmcia_cs(void)
950{
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700951 class_interface_unregister(&pccard_sysfs_interface);
952 class_unregister(&pcmcia_socket_class);
Dominik Brodowski378a33a2005-06-27 16:28:54 -0700953
954 wait_for_completion(&pcmcia_unload);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955}
956
957subsys_initcall(init_pcmcia_cs);
958module_exit(exit_pcmcia_cs);
959