blob: 29276bd28295007212a73e89c4bc44cb6f0a0a2a [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/device.h>
Serge E. Hallyn1da9ab72006-06-14 08:01:26 -050030#include <linux/kthread.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080031#include <linux/freezer.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
Randy Dunlap78187862007-12-10 15:49:22 -080092/*
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) {
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200112 if (socket->dev.parent != dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 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) {
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200130 if (socket->dev.parent != dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 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{
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200145 struct device *dev = get_device(&skt->dev);
146 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 return NULL;
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200148 skt = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 if (!try_module_get(skt->owner)) {
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200150 put_device(&skt->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 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);
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200161 put_device(&skt->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162}
163EXPORT_SYMBOL(pcmcia_put_socket);
164
165
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200166static void pcmcia_release_socket(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200168 struct pcmcia_socket *socket = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170 complete(&socket->socket_released);
171}
172
173static int pccardd(void *__skt);
174
175/**
176 * pcmcia_register_socket - add a new pcmcia socket device
Randy Dunlap78187862007-12-10 15:49:22 -0800177 * @socket: the &socket to register
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 */
179int pcmcia_register_socket(struct pcmcia_socket *socket)
180{
Serge E. Hallyn1da9ab72006-06-14 08:01:26 -0500181 struct task_struct *tsk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 int ret;
183
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200184 if (!socket || !socket->ops || !socket->dev.parent || !socket->resource_ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 return -EINVAL;
186
187 cs_dbg(socket, 0, "pcmcia_register_socket(0x%p)\n", socket->ops);
188
189 spin_lock_init(&socket->lock);
190
191 if (socket->resource_ops->init) {
192 ret = socket->resource_ops->init(socket);
193 if (ret)
194 return (ret);
195 }
196
197 /* try to obtain a socket number [yes, it gets ugly if we
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700198 * register more than 2^sizeof(unsigned int) pcmcia
199 * sockets... but the socket number is deprecated
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 * anyways, so I don't care] */
201 down_write(&pcmcia_socket_list_rwsem);
202 if (list_empty(&pcmcia_socket_list))
203 socket->sock = 0;
204 else {
205 unsigned int found, i = 1;
206 struct pcmcia_socket *tmp;
207 do {
208 found = 1;
209 list_for_each_entry(tmp, &pcmcia_socket_list, socket_list) {
210 if (tmp->sock == i)
211 found = 0;
212 }
213 i++;
214 } while (!found);
215 socket->sock = i - 1;
216 }
217 list_add_tail(&socket->socket_list, &pcmcia_socket_list);
218 up_write(&pcmcia_socket_list_rwsem);
219
Russell King5bc6b682005-07-07 17:59:07 -0700220#ifndef CONFIG_CARDBUS
221 /*
222 * If we do not support Cardbus, ensure that
223 * the Cardbus socket capability is disabled.
224 */
225 socket->features &= ~SS_CAP_CARDBUS;
226#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228 /* set proper values in socket->dev */
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200229 dev_set_drvdata(&socket->dev, socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 socket->dev.class = &pcmcia_socket_class;
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200231 snprintf(socket->dev.bus_id, BUS_ID_SIZE, "pcmcia_socket%u", socket->sock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233 /* base address = 0, map = 0 */
234 socket->cis_mem.flags = 0;
235 socket->cis_mem.speed = cis_speed;
236
237 INIT_LIST_HEAD(&socket->cis_cache);
238
239 init_completion(&socket->socket_released);
240 init_completion(&socket->thread_done);
241 init_waitqueue_head(&socket->thread_wait);
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100242 mutex_init(&socket->skt_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 spin_lock_init(&socket->thread_lock);
244
Serge E. Hallyn1da9ab72006-06-14 08:01:26 -0500245 tsk = kthread_run(pccardd, socket, "pccardd");
246 if (IS_ERR(tsk)) {
247 ret = PTR_ERR(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 goto err;
Serge E. Hallyn1da9ab72006-06-14 08:01:26 -0500249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251 wait_for_completion(&socket->thread_done);
Serge E. Hallyn1da9ab72006-06-14 08:01:26 -0500252 if (!socket->thread) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 printk(KERN_WARNING "PCMCIA: warning: socket thread for socket %p did not start\n", socket);
254 return -EIO;
255 }
Serge E. Hallyn1da9ab72006-06-14 08:01:26 -0500256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 pcmcia_parse_events(socket, SS_DETECT);
258
259 return 0;
260
261 err:
262 down_write(&pcmcia_socket_list_rwsem);
263 list_del(&socket->socket_list);
264 up_write(&pcmcia_socket_list_rwsem);
265 return ret;
266} /* pcmcia_register_socket */
267EXPORT_SYMBOL(pcmcia_register_socket);
268
269
270/**
271 * pcmcia_unregister_socket - remove a pcmcia socket device
Randy Dunlap78187862007-12-10 15:49:22 -0800272 * @socket: the &socket to unregister
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 */
274void pcmcia_unregister_socket(struct pcmcia_socket *socket)
275{
276 if (!socket)
277 return;
278
279 cs_dbg(socket, 0, "pcmcia_unregister_socket(0x%p)\n", socket->ops);
280
281 if (socket->thread) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 wake_up(&socket->thread_wait);
Serge E. Hallyn1da9ab72006-06-14 08:01:26 -0500283 kthread_stop(socket->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 }
285 release_cis_mem(socket);
286
287 /* remove from our own list */
288 down_write(&pcmcia_socket_list_rwsem);
289 list_del(&socket->socket_list);
290 up_write(&pcmcia_socket_list_rwsem);
291
292 /* wait for sysfs to drop all references */
293 release_resource_db(socket);
294 wait_for_completion(&socket->socket_released);
295} /* pcmcia_unregister_socket */
296EXPORT_SYMBOL(pcmcia_unregister_socket);
297
298
299struct pcmcia_socket * pcmcia_get_socket_by_nr(unsigned int nr)
300{
301 struct pcmcia_socket *s;
302
303 down_read(&pcmcia_socket_list_rwsem);
304 list_for_each_entry(s, &pcmcia_socket_list, socket_list)
305 if (s->sock == nr) {
306 up_read(&pcmcia_socket_list_rwsem);
307 return s;
308 }
309 up_read(&pcmcia_socket_list_rwsem);
310
311 return NULL;
312
313}
314EXPORT_SYMBOL(pcmcia_get_socket_by_nr);
315
Randy Dunlap78187862007-12-10 15:49:22 -0800316/*
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700317 * The central event handler. Send_event() sends an event to the
318 * 16-bit subsystem, which then calls the relevant device drivers.
319 * Parse_events() interprets the event bits from
320 * a card status change report. Do_shutdown() handles the high
321 * priority stuff associated with a card removal.
322 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324/* NOTE: send_event needs to be called with skt->sem held. */
325
326static int send_event(struct pcmcia_socket *s, event_t event, int priority)
327{
328 int ret;
329
330 if (s->state & SOCKET_CARDBUS)
331 return 0;
332
333 cs_dbg(s, 1, "send_event(event %d, pri %d, callback 0x%p)\n",
334 event, priority, s->callback);
335
336 if (!s->callback)
337 return 0;
338 if (!try_module_get(s->callback->owner))
339 return 0;
340
341 ret = s->callback->event(s, event, priority);
342
343 module_put(s->callback->owner);
344
345 return ret;
346}
347
348static void socket_remove_drivers(struct pcmcia_socket *skt)
349{
350 cs_dbg(skt, 4, "remove_drivers\n");
351
352 send_event(skt, CS_EVENT_CARD_REMOVAL, CS_EVENT_PRI_HIGH);
353}
354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355static int socket_reset(struct pcmcia_socket *skt)
356{
357 int status, i;
358
359 cs_dbg(skt, 4, "reset\n");
360
361 skt->socket.flags |= SS_OUTPUT_ENA | SS_RESET;
362 skt->ops->set_socket(skt, &skt->socket);
363 udelay((long)reset_time);
364
365 skt->socket.flags &= ~SS_RESET;
366 skt->ops->set_socket(skt, &skt->socket);
367
368 msleep(unreset_delay * 10);
369 for (i = 0; i < unreset_limit; i++) {
370 skt->ops->get_status(skt, &status);
371
372 if (!(status & SS_DETECT))
373 return CS_NO_CARD;
374
375 if (status & SS_READY)
376 return CS_SUCCESS;
377
378 msleep(unreset_check * 10);
379 }
380
381 cs_err(skt, "time out after reset.\n");
382 return CS_GENERAL_FAILURE;
383}
384
Randy Dunlap78187862007-12-10 15:49:22 -0800385/*
Daniel Ritz6e493882005-12-30 15:12:35 +0100386 * socket_setup() and socket_shutdown() are called by the main event handler
387 * when card insertion and removal events are received.
388 * socket_setup() turns on socket power and resets the socket, in two stages.
389 * socket_shutdown() unconfigures a socket and turns off socket power.
390 */
391static void socket_shutdown(struct pcmcia_socket *s)
392{
393 int status;
394
395 cs_dbg(s, 4, "shutdown\n");
396
397 socket_remove_drivers(s);
398 s->state &= SOCKET_INUSE | SOCKET_PRESENT;
399 msleep(shutdown_delay * 10);
400 s->state &= SOCKET_INUSE;
401
402 /* Blank out the socket state */
403 s->socket = dead_socket;
404 s->ops->init(s);
405 s->ops->set_socket(s, &s->socket);
406 s->irq.AssignedIRQ = s->irq.Config = 0;
407 s->lock_count = 0;
408 destroy_cis_cache(s);
409#ifdef CONFIG_CARDBUS
410 cb_free(s);
411#endif
412 s->functions = 0;
Daniel Ritz6e493882005-12-30 15:12:35 +0100413
Daniel Ritzafb2c162007-07-31 00:38:08 -0700414 /* give socket some time to power down */
415 msleep(100);
416
Daniel Ritz6e493882005-12-30 15:12:35 +0100417 s->ops->get_status(s, &status);
418 if (status & SS_POWERON) {
419 printk(KERN_ERR "PCMCIA: socket %p: *** DANGER *** unable to remove socket power\n", s);
420 }
421
422 cs_socket_put(s);
423}
424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425static int socket_setup(struct pcmcia_socket *skt, int initial_delay)
426{
427 int status, i;
428
429 cs_dbg(skt, 4, "setup\n");
430
431 skt->ops->get_status(skt, &status);
432 if (!(status & SS_DETECT))
433 return CS_NO_CARD;
434
435 msleep(initial_delay * 10);
436
437 for (i = 0; i < 100; i++) {
438 skt->ops->get_status(skt, &status);
439 if (!(status & SS_DETECT))
440 return CS_NO_CARD;
441
442 if (!(status & SS_PENDING))
443 break;
444
445 msleep(100);
446 }
447
448 if (status & SS_PENDING) {
449 cs_err(skt, "voltage interrogation timed out.\n");
450 return CS_GENERAL_FAILURE;
451 }
452
453 if (status & SS_CARDBUS) {
Russell King5bc6b682005-07-07 17:59:07 -0700454 if (!(skt->features & SS_CAP_CARDBUS)) {
455 cs_err(skt, "cardbus cards are not supported.\n");
456 return CS_BAD_TYPE;
457 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 skt->state |= SOCKET_CARDBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 }
460
461 /*
462 * Decode the card voltage requirements, and apply power to the card.
463 */
464 if (status & SS_3VCARD)
465 skt->socket.Vcc = skt->socket.Vpp = 33;
466 else if (!(status & SS_XVCARD))
467 skt->socket.Vcc = skt->socket.Vpp = 50;
468 else {
469 cs_err(skt, "unsupported voltage key.\n");
470 return CS_BAD_TYPE;
471 }
Daniel Ritzfa912bc2005-06-23 00:10:12 -0700472
473 if (skt->power_hook)
474 skt->power_hook(skt, HOOK_POWER_PRE);
475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 skt->socket.flags = 0;
477 skt->ops->set_socket(skt, &skt->socket);
478
479 /*
480 * Wait "vcc_settle" for the supply to stabilise.
481 */
482 msleep(vcc_settle * 10);
483
484 skt->ops->get_status(skt, &status);
485 if (!(status & SS_POWERON)) {
486 cs_err(skt, "unable to apply power.\n");
487 return CS_BAD_TYPE;
488 }
489
Daniel Ritzfa912bc2005-06-23 00:10:12 -0700490 status = socket_reset(skt);
491
492 if (skt->power_hook)
493 skt->power_hook(skt, HOOK_POWER_POST);
494
495 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496}
497
498/*
499 * Handle card insertion. Setup the socket, reset the card,
500 * and then tell the rest of PCMCIA that a card is present.
501 */
502static int socket_insert(struct pcmcia_socket *skt)
503{
504 int ret;
505
506 cs_dbg(skt, 4, "insert\n");
507
508 if (!cs_socket_get(skt))
509 return CS_NO_CARD;
510
511 ret = socket_setup(skt, setup_delay);
512 if (ret == CS_SUCCESS) {
513 skt->state |= SOCKET_PRESENT;
Dominik Brodowski807277c2005-11-12 23:34:06 +0100514
515 printk(KERN_NOTICE "pccard: %s card inserted into slot %d\n",
516 (skt->state & SOCKET_CARDBUS) ? "CardBus" : "PCMCIA",
517 skt->sock);
518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519#ifdef CONFIG_CARDBUS
520 if (skt->state & SOCKET_CARDBUS) {
521 cb_alloc(skt);
522 skt->state |= SOCKET_CARDBUS_CONFIG;
523 }
524#endif
525 cs_dbg(skt, 4, "insert done\n");
526
527 send_event(skt, CS_EVENT_CARD_INSERTION, CS_EVENT_PRI_LOW);
528 } else {
529 socket_shutdown(skt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 }
531
532 return ret;
533}
534
535static int socket_suspend(struct pcmcia_socket *skt)
536{
537 if (skt->state & SOCKET_SUSPEND)
538 return CS_IN_USE;
539
540 send_event(skt, CS_EVENT_PM_SUSPEND, CS_EVENT_PRI_LOW);
541 skt->socket = dead_socket;
542 skt->ops->set_socket(skt, &skt->socket);
543 if (skt->ops->suspend)
544 skt->ops->suspend(skt);
545 skt->state |= SOCKET_SUSPEND;
546
547 return CS_SUCCESS;
548}
549
550/*
551 * Resume a socket. If a card is present, verify its CIS against
552 * our cached copy. If they are different, the card has been
553 * replaced, and we need to tell the drivers.
554 */
555static int socket_resume(struct pcmcia_socket *skt)
556{
557 int ret;
558
559 if (!(skt->state & SOCKET_SUSPEND))
560 return CS_IN_USE;
561
562 skt->socket = dead_socket;
563 skt->ops->init(skt);
564 skt->ops->set_socket(skt, &skt->socket);
565
566 if (!(skt->state & SOCKET_PRESENT)) {
567 skt->state &= ~SOCKET_SUSPEND;
568 return socket_insert(skt);
569 }
570
571 ret = socket_setup(skt, resume_delay);
572 if (ret == CS_SUCCESS) {
573 /*
574 * FIXME: need a better check here for cardbus cards.
575 */
576 if (verify_cis_cache(skt) != 0) {
577 cs_dbg(skt, 4, "cis mismatch - different card\n");
578 socket_remove_drivers(skt);
579 destroy_cis_cache(skt);
580 /*
581 * Workaround: give DS time to schedule removal.
582 * Remove me once the 100ms delay is eliminated
583 * in ds.c
584 */
585 msleep(200);
586 send_event(skt, CS_EVENT_CARD_INSERTION, CS_EVENT_PRI_LOW);
587 } else {
588 cs_dbg(skt, 4, "cis matches cache\n");
589 send_event(skt, CS_EVENT_PM_RESUME, CS_EVENT_PRI_LOW);
590 }
591 } else {
592 socket_shutdown(skt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 }
594
595 skt->state &= ~SOCKET_SUSPEND;
596
597 return CS_SUCCESS;
598}
599
600static void socket_remove(struct pcmcia_socket *skt)
601{
Dominik Brodowski807277c2005-11-12 23:34:06 +0100602 printk(KERN_NOTICE "pccard: card ejected from slot %d\n", skt->sock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 socket_shutdown(skt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604}
605
606/*
607 * Process a socket card detect status change.
608 *
609 * If we don't have a card already present, delay the detect event for
610 * about 20ms (to be on the safe side) before reading the socket status.
611 *
612 * Some i82365-based systems send multiple SS_DETECT events during card
613 * insertion, and the "card present" status bit seems to bounce. This
614 * will probably be true with GPIO-based card detection systems after
615 * the product has aged.
616 */
617static void socket_detect_change(struct pcmcia_socket *skt)
618{
619 if (!(skt->state & SOCKET_SUSPEND)) {
620 int status;
621
622 if (!(skt->state & SOCKET_PRESENT))
623 msleep(20);
624
625 skt->ops->get_status(skt, &status);
626 if ((skt->state & SOCKET_PRESENT) &&
627 !(status & SS_DETECT))
628 socket_remove(skt);
629 if (!(skt->state & SOCKET_PRESENT) &&
630 (status & SS_DETECT))
631 socket_insert(skt);
632 }
633}
634
635static int pccardd(void *__skt)
636{
637 struct pcmcia_socket *skt = __skt;
638 DECLARE_WAITQUEUE(wait, current);
639 int ret;
640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 skt->thread = current;
642 skt->socket = dead_socket;
643 skt->ops->init(skt);
644 skt->ops->set_socket(skt, &skt->socket);
645
646 /* register with the device core */
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200647 ret = device_register(&skt->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 if (ret) {
649 printk(KERN_WARNING "PCMCIA: unable to register socket 0x%p\n",
650 skt);
651 skt->thread = NULL;
Serge E. Hallyn1da9ab72006-06-14 08:01:26 -0500652 complete(&skt->thread_done);
653 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 }
David Brownell4356d732008-04-28 01:03:20 -0700655 ret = pccard_sysfs_add_socket(&skt->dev);
656 if (ret)
657 dev_warn(&skt->dev, "err %d adding socket attributes\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
659 add_wait_queue(&skt->thread_wait, &wait);
Daniel Ritzbf4de6f2005-09-09 13:03:23 -0700660 complete(&skt->thread_done);
661
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700662 set_freezable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 for (;;) {
664 unsigned long flags;
665 unsigned int events;
666
667 set_current_state(TASK_INTERRUPTIBLE);
668
669 spin_lock_irqsave(&skt->thread_lock, flags);
670 events = skt->thread_events;
671 skt->thread_events = 0;
672 spin_unlock_irqrestore(&skt->thread_lock, flags);
673
674 if (events) {
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100675 mutex_lock(&skt->skt_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 if (events & SS_DETECT)
677 socket_detect_change(skt);
678 if (events & SS_BATDEAD)
679 send_event(skt, CS_EVENT_BATTERY_DEAD, CS_EVENT_PRI_LOW);
680 if (events & SS_BATWARN)
681 send_event(skt, CS_EVENT_BATTERY_LOW, CS_EVENT_PRI_LOW);
682 if (events & SS_READY)
683 send_event(skt, CS_EVENT_READY_CHANGE, CS_EVENT_PRI_LOW);
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100684 mutex_unlock(&skt->skt_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 continue;
686 }
687
Serge E. Hallyn1da9ab72006-06-14 08:01:26 -0500688 if (kthread_should_stop())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 break;
Daniel Ritzbf4de6f2005-09-09 13:03:23 -0700690
691 schedule();
692 try_to_freeze();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 }
Steven Rostedt220ec022005-10-10 11:13:17 -0400694 /* make sure we are running before we exit */
695 set_current_state(TASK_RUNNING);
696
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 remove_wait_queue(&skt->thread_wait, &wait);
698
699 /* remove from the device core */
David Brownell4356d732008-04-28 01:03:20 -0700700 pccard_sysfs_remove_socket(&skt->dev);
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200701 device_unregister(&skt->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Serge E. Hallyn1da9ab72006-06-14 08:01:26 -0500703 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704}
705
706/*
707 * Yenta (at least) probes interrupts before registering the socket and
708 * starting the handler thread.
709 */
710void pcmcia_parse_events(struct pcmcia_socket *s, u_int events)
711{
Arjan van de Ven4b7a89a2006-06-30 10:31:13 +0200712 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 cs_dbg(s, 4, "parse_events: events %08x\n", events);
714 if (s->thread) {
Arjan van de Ven4b7a89a2006-06-30 10:31:13 +0200715 spin_lock_irqsave(&s->thread_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 s->thread_events |= events;
Arjan van de Ven4b7a89a2006-06-30 10:31:13 +0200717 spin_unlock_irqrestore(&s->thread_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
719 wake_up(&s->thread_wait);
720 }
721} /* pcmcia_parse_events */
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700722EXPORT_SYMBOL(pcmcia_parse_events);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725/* register pcmcia_callback */
726int pccard_register_pcmcia(struct pcmcia_socket *s, struct pcmcia_callback *c)
727{
728 int ret = 0;
729
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100730 /* s->skt_mutex also protects s->callback */
731 mutex_lock(&s->skt_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
733 if (c) {
734 /* registration */
735 if (s->callback) {
736 ret = -EBUSY;
737 goto err;
738 }
739
740 s->callback = c;
741
742 if ((s->state & (SOCKET_PRESENT|SOCKET_CARDBUS)) == SOCKET_PRESENT)
743 send_event(s, CS_EVENT_CARD_INSERTION, CS_EVENT_PRI_LOW);
744 } else
745 s->callback = NULL;
746 err:
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100747 mutex_unlock(&s->skt_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
749 return ret;
750}
751EXPORT_SYMBOL(pccard_register_pcmcia);
752
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700754/* I'm not sure which "reset" function this is supposed to use,
755 * but for now, it uses the low-level interface's reset, not the
756 * CIS register.
757 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
759int pccard_reset_card(struct pcmcia_socket *skt)
760{
761 int ret;
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700762
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 cs_dbg(skt, 1, "resetting socket\n");
764
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100765 mutex_lock(&skt->skt_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 do {
767 if (!(skt->state & SOCKET_PRESENT)) {
768 ret = CS_NO_CARD;
769 break;
770 }
771 if (skt->state & SOCKET_SUSPEND) {
772 ret = CS_IN_USE;
773 break;
774 }
775 if (skt->state & SOCKET_CARDBUS) {
776 ret = CS_UNSUPPORTED_FUNCTION;
777 break;
778 }
779
780 ret = send_event(skt, CS_EVENT_RESET_REQUEST, CS_EVENT_PRI_LOW);
781 if (ret == 0) {
782 send_event(skt, CS_EVENT_RESET_PHYSICAL, CS_EVENT_PRI_LOW);
Dominik Brodowski8e9e7932006-01-06 00:02:03 +0100783 if (skt->callback)
784 skt->callback->suspend(skt);
785 if (socket_reset(skt) == CS_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 send_event(skt, CS_EVENT_CARD_RESET, CS_EVENT_PRI_LOW);
Dominik Brodowski8e9e7932006-01-06 00:02:03 +0100787 if (skt->callback)
788 skt->callback->resume(skt);
789 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 }
791
792 ret = CS_SUCCESS;
793 } while (0);
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100794 mutex_unlock(&skt->skt_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
796 return ret;
797} /* reset_card */
798EXPORT_SYMBOL(pccard_reset_card);
799
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700801/* These shut down or wake up a socket. They are sort of user
802 * initiated versions of the APM suspend and resume actions.
803 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804int pcmcia_suspend_card(struct pcmcia_socket *skt)
805{
806 int ret;
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 cs_dbg(skt, 1, "suspending socket\n");
809
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100810 mutex_lock(&skt->skt_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 do {
812 if (!(skt->state & SOCKET_PRESENT)) {
813 ret = CS_NO_CARD;
814 break;
815 }
816 if (skt->state & SOCKET_CARDBUS) {
817 ret = CS_UNSUPPORTED_FUNCTION;
818 break;
819 }
Dominik Brodowski8e9e7932006-01-06 00:02:03 +0100820 if (skt->callback) {
821 ret = skt->callback->suspend(skt);
822 if (ret)
823 break;
824 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 ret = socket_suspend(skt);
826 } while (0);
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100827 mutex_unlock(&skt->skt_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
829 return ret;
830} /* suspend_card */
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700831EXPORT_SYMBOL(pcmcia_suspend_card);
832
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
834int pcmcia_resume_card(struct pcmcia_socket *skt)
835{
836 int ret;
837
838 cs_dbg(skt, 1, "waking up socket\n");
839
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100840 mutex_lock(&skt->skt_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 do {
842 if (!(skt->state & SOCKET_PRESENT)) {
843 ret = CS_NO_CARD;
844 break;
845 }
846 if (skt->state & SOCKET_CARDBUS) {
847 ret = CS_UNSUPPORTED_FUNCTION;
848 break;
849 }
850 ret = socket_resume(skt);
Dominik Brodowski8e9e7932006-01-06 00:02:03 +0100851 if (!ret && skt->callback)
852 skt->callback->resume(skt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 } while (0);
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100854 mutex_unlock(&skt->skt_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
856 return ret;
857} /* resume_card */
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700858EXPORT_SYMBOL(pcmcia_resume_card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700861/* These handle user requests to eject or insert a card. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862int pcmcia_eject_card(struct pcmcia_socket *skt)
863{
864 int ret;
865
866 cs_dbg(skt, 1, "user eject request\n");
867
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100868 mutex_lock(&skt->skt_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 do {
870 if (!(skt->state & SOCKET_PRESENT)) {
871 ret = -ENODEV;
872 break;
873 }
874
875 ret = send_event(skt, CS_EVENT_EJECTION_REQUEST, CS_EVENT_PRI_LOW);
876 if (ret != 0) {
877 ret = -EINVAL;
878 break;
879 }
880
881 socket_remove(skt);
882 ret = 0;
883 } while (0);
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100884 mutex_unlock(&skt->skt_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
886 return ret;
887} /* eject_card */
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700888EXPORT_SYMBOL(pcmcia_eject_card);
889
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
891int pcmcia_insert_card(struct pcmcia_socket *skt)
892{
893 int ret;
894
895 cs_dbg(skt, 1, "user insert request\n");
896
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100897 mutex_lock(&skt->skt_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 do {
899 if (skt->state & SOCKET_PRESENT) {
900 ret = -EBUSY;
901 break;
902 }
903 if (socket_insert(skt) == CS_NO_CARD) {
904 ret = -ENODEV;
905 break;
906 }
907 ret = 0;
908 } while (0);
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100909 mutex_unlock(&skt->skt_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
911 return ret;
912} /* insert_card */
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700913EXPORT_SYMBOL(pcmcia_insert_card);
914
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Kay Sievers7eff2e72007-08-14 15:15:12 +0200916static int pcmcia_socket_uevent(struct device *dev,
917 struct kobj_uevent_env *env)
Dominik Brodowski3ee13932005-06-27 16:28:05 -0700918{
919 struct pcmcia_socket *s = container_of(dev, struct pcmcia_socket, dev);
Dominik Brodowski3ee13932005-06-27 16:28:05 -0700920
Kay Sievers7eff2e72007-08-14 15:15:12 +0200921 if (add_uevent_var(env, "SOCKET_NO=%u", s->sock))
Dominik Brodowski3ee13932005-06-27 16:28:05 -0700922 return -ENOMEM;
923
Dominik Brodowski3ee13932005-06-27 16:28:05 -0700924 return 0;
925}
926
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Dominik Brodowski378a33a2005-06-27 16:28:54 -0700928static struct completion pcmcia_unload;
929
930static void pcmcia_release_socket_class(struct class *data)
931{
932 complete(&pcmcia_unload);
933}
934
935
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936struct class pcmcia_socket_class = {
937 .name = "pcmcia_socket",
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200938 .dev_uevent = pcmcia_socket_uevent,
939 .dev_release = pcmcia_release_socket,
Dominik Brodowski378a33a2005-06-27 16:28:54 -0700940 .class_release = pcmcia_release_socket_class,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941};
942EXPORT_SYMBOL(pcmcia_socket_class);
943
944
945static int __init init_pcmcia_cs(void)
946{
Dominik Brodowski378a33a2005-06-27 16:28:54 -0700947 init_completion(&pcmcia_unload);
David Brownell4356d732008-04-28 01:03:20 -0700948 return class_register(&pcmcia_socket_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949}
950
951static void __exit exit_pcmcia_cs(void)
952{
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700953 class_unregister(&pcmcia_socket_class);
Dominik Brodowski378a33a2005-06-27 16:28:54 -0700954 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