blob: 96d8d25c209d7ea2f865505258179ad20ad205bb [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <pcmcia/cs_types.h>
36#include <pcmcia/ss.h>
37#include <pcmcia/cs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <pcmcia/cistpl.h>
39#include <pcmcia/cisreg.h>
40#include <pcmcia/ds.h>
41#include "cs_internal.h"
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44/* Module parameters */
45
46MODULE_AUTHOR("David Hinds <dahinds@users.sourceforge.net>");
Dominik Brodowski33519dd2005-06-27 16:28:53 -070047MODULE_DESCRIPTION("Linux Kernel Card Services");
Linus Torvalds1da177e2005-04-16 15:20:36 -070048MODULE_LICENSE("GPL");
49
50#define INT_MODULE_PARM(n, v) static int n = v; module_param(n, int, 0444)
51
52INT_MODULE_PARM(setup_delay, 10); /* centiseconds */
53INT_MODULE_PARM(resume_delay, 20); /* centiseconds */
54INT_MODULE_PARM(shutdown_delay, 3); /* centiseconds */
55INT_MODULE_PARM(vcc_settle, 40); /* centiseconds */
56INT_MODULE_PARM(reset_time, 10); /* usecs */
57INT_MODULE_PARM(unreset_delay, 10); /* centiseconds */
58INT_MODULE_PARM(unreset_check, 10); /* centiseconds */
59INT_MODULE_PARM(unreset_limit, 30); /* unreset_check's */
60
61/* Access speed for attribute memory windows */
62INT_MODULE_PARM(cis_speed, 300); /* ns */
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65socket_state_t dead_socket = {
66 .csc_mask = SS_DETECT,
67};
Dominik Brodowski33519dd2005-06-27 16:28:53 -070068EXPORT_SYMBOL(dead_socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70
71/* List of all sockets, protected by a rwsem */
72LIST_HEAD(pcmcia_socket_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073EXPORT_SYMBOL(pcmcia_socket_list);
Dominik Brodowski33519dd2005-06-27 16:28:53 -070074
75DECLARE_RWSEM(pcmcia_socket_list_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076EXPORT_SYMBOL(pcmcia_socket_list_rwsem);
77
78
Randy Dunlap78187862007-12-10 15:49:22 -080079/*
Dominik Brodowski33519dd2005-06-27 16:28:53 -070080 * Low-level PCMCIA socket drivers need to register with the PCCard
81 * core using pcmcia_register_socket.
82 *
83 * socket drivers are expected to use the following callbacks in their
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 * .drv struct:
85 * - pcmcia_socket_dev_suspend
86 * - pcmcia_socket_dev_resume
87 * These functions check for the appropriate struct pcmcia_soket arrays,
88 * and pass them to the low-level functions pcmcia_{suspend,resume}_socket
89 */
Rafael J. Wysocki9905d1b2009-11-03 10:54:58 +010090static int socket_early_resume(struct pcmcia_socket *skt);
91static int socket_late_resume(struct pcmcia_socket *skt);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092static int socket_resume(struct pcmcia_socket *skt);
93static int socket_suspend(struct pcmcia_socket *skt);
94
Rafael J. Wysocki9905d1b2009-11-03 10:54:58 +010095static void pcmcia_socket_dev_run(struct device *dev,
96 int (*cb)(struct pcmcia_socket *))
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
98 struct pcmcia_socket *socket;
99
100 down_read(&pcmcia_socket_list_rwsem);
101 list_for_each_entry(socket, &pcmcia_socket_list, socket_list) {
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200102 if (socket->dev.parent != dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 continue;
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100104 mutex_lock(&socket->skt_mutex);
Rafael J. Wysocki9905d1b2009-11-03 10:54:58 +0100105 cb(socket);
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100106 mutex_unlock(&socket->skt_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 }
108 up_read(&pcmcia_socket_list_rwsem);
Rafael J. Wysocki9905d1b2009-11-03 10:54:58 +0100109}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Rafael J. Wysocki9905d1b2009-11-03 10:54:58 +0100111int pcmcia_socket_dev_suspend(struct device *dev)
112{
113 pcmcia_socket_dev_run(dev, socket_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 return 0;
115}
116EXPORT_SYMBOL(pcmcia_socket_dev_suspend);
117
Rafael J. Wysocki9905d1b2009-11-03 10:54:58 +0100118void pcmcia_socket_dev_early_resume(struct device *dev)
119{
120 pcmcia_socket_dev_run(dev, socket_early_resume);
121}
122EXPORT_SYMBOL(pcmcia_socket_dev_early_resume);
123
124void pcmcia_socket_dev_late_resume(struct device *dev)
125{
126 pcmcia_socket_dev_run(dev, socket_late_resume);
127}
128EXPORT_SYMBOL(pcmcia_socket_dev_late_resume);
129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130int pcmcia_socket_dev_resume(struct device *dev)
131{
Rafael J. Wysocki9905d1b2009-11-03 10:54:58 +0100132 pcmcia_socket_dev_run(dev, socket_resume);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 return 0;
134}
135EXPORT_SYMBOL(pcmcia_socket_dev_resume);
136
137
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100138struct pcmcia_socket *pcmcia_get_socket(struct pcmcia_socket *skt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200140 struct device *dev = get_device(&skt->dev);
141 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 return NULL;
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200143 skt = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 if (!try_module_get(skt->owner)) {
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200145 put_device(&skt->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 return NULL;
147 }
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100148 return skt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149}
150EXPORT_SYMBOL(pcmcia_get_socket);
151
152
153void pcmcia_put_socket(struct pcmcia_socket *skt)
154{
155 module_put(skt->owner);
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200156 put_device(&skt->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157}
158EXPORT_SYMBOL(pcmcia_put_socket);
159
160
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200161static void pcmcia_release_socket(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200163 struct pcmcia_socket *socket = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165 complete(&socket->socket_released);
166}
167
168static int pccardd(void *__skt);
169
170/**
171 * pcmcia_register_socket - add a new pcmcia socket device
Randy Dunlap78187862007-12-10 15:49:22 -0800172 * @socket: the &socket to register
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 */
174int pcmcia_register_socket(struct pcmcia_socket *socket)
175{
Serge E. Hallyn1da9ab72006-06-14 08:01:26 -0500176 struct task_struct *tsk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 int ret;
178
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200179 if (!socket || !socket->ops || !socket->dev.parent || !socket->resource_ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 return -EINVAL;
181
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200182 dev_dbg(&socket->dev, "pcmcia_register_socket(0x%p)\n", socket->ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184 spin_lock_init(&socket->lock);
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 /* try to obtain a socket number [yes, it gets ugly if we
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700187 * register more than 2^sizeof(unsigned int) pcmcia
188 * sockets... but the socket number is deprecated
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 * anyways, so I don't care] */
190 down_write(&pcmcia_socket_list_rwsem);
191 if (list_empty(&pcmcia_socket_list))
192 socket->sock = 0;
193 else {
194 unsigned int found, i = 1;
195 struct pcmcia_socket *tmp;
196 do {
197 found = 1;
198 list_for_each_entry(tmp, &pcmcia_socket_list, socket_list) {
199 if (tmp->sock == i)
200 found = 0;
201 }
202 i++;
203 } while (!found);
204 socket->sock = i - 1;
205 }
206 list_add_tail(&socket->socket_list, &pcmcia_socket_list);
207 up_write(&pcmcia_socket_list_rwsem);
208
Russell King5bc6b682005-07-07 17:59:07 -0700209#ifndef CONFIG_CARDBUS
210 /*
211 * If we do not support Cardbus, ensure that
212 * the Cardbus socket capability is disabled.
213 */
214 socket->features &= ~SS_CAP_CARDBUS;
215#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217 /* set proper values in socket->dev */
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200218 dev_set_drvdata(&socket->dev, socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 socket->dev.class = &pcmcia_socket_class;
Kay Sievers25096982008-11-01 11:46:06 +0100220 dev_set_name(&socket->dev, "pcmcia_socket%u", socket->sock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222 /* base address = 0, map = 0 */
223 socket->cis_mem.flags = 0;
224 socket->cis_mem.speed = cis_speed;
225
226 INIT_LIST_HEAD(&socket->cis_cache);
227
228 init_completion(&socket->socket_released);
229 init_completion(&socket->thread_done);
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100230 mutex_init(&socket->skt_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 spin_lock_init(&socket->thread_lock);
232
Dominik Brodowskib1769452008-11-09 21:47:47 +0100233 if (socket->resource_ops->init) {
234 ret = socket->resource_ops->init(socket);
235 if (ret)
236 goto err;
237 }
238
Serge E. Hallyn1da9ab72006-06-14 08:01:26 -0500239 tsk = kthread_run(pccardd, socket, "pccardd");
240 if (IS_ERR(tsk)) {
241 ret = PTR_ERR(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 goto err;
Serge E. Hallyn1da9ab72006-06-14 08:01:26 -0500243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
245 wait_for_completion(&socket->thread_done);
Serge E. Hallyn1da9ab72006-06-14 08:01:26 -0500246 if (!socket->thread) {
Dominik Brodowski2e55bf62008-08-02 18:08:38 +0200247 dev_printk(KERN_WARNING, &socket->dev,
248 "PCMCIA: warning: socket thread did not start\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 return -EIO;
250 }
Serge E. Hallyn1da9ab72006-06-14 08:01:26 -0500251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 pcmcia_parse_events(socket, SS_DETECT);
253
Dominik Brodowski638bba52009-11-07 12:26:17 +0100254 /*
255 * Let's try to get the PCMCIA module for 16-bit PCMCIA support.
256 * If it fails, it doesn't matter -- we still have 32-bit CardBus
257 * support to offer, so this is not a failure mode.
258 */
259 request_module_nowait("pcmcia");
260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 return 0;
262
263 err:
264 down_write(&pcmcia_socket_list_rwsem);
265 list_del(&socket->socket_list);
266 up_write(&pcmcia_socket_list_rwsem);
267 return ret;
268} /* pcmcia_register_socket */
269EXPORT_SYMBOL(pcmcia_register_socket);
270
271
272/**
273 * pcmcia_unregister_socket - remove a pcmcia socket device
Randy Dunlap78187862007-12-10 15:49:22 -0800274 * @socket: the &socket to unregister
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 */
276void pcmcia_unregister_socket(struct pcmcia_socket *socket)
277{
278 if (!socket)
279 return;
280
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200281 dev_dbg(&socket->dev, "pcmcia_unregister_socket(0x%p)\n", socket->ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Christoph Hellwig0b402092007-04-22 13:55:36 +0100283 if (socket->thread)
Serge E. Hallyn1da9ab72006-06-14 08:01:26 -0500284 kthread_stop(socket->thread);
Christoph Hellwig0b402092007-04-22 13:55:36 +0100285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 release_cis_mem(socket);
287
288 /* remove from our own list */
289 down_write(&pcmcia_socket_list_rwsem);
290 list_del(&socket->socket_list);
291 up_write(&pcmcia_socket_list_rwsem);
292
293 /* wait for sysfs to drop all references */
294 release_resource_db(socket);
295 wait_for_completion(&socket->socket_released);
296} /* pcmcia_unregister_socket */
297EXPORT_SYMBOL(pcmcia_unregister_socket);
298
299
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100300struct pcmcia_socket *pcmcia_get_socket_by_nr(unsigned int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
302 struct pcmcia_socket *s;
303
304 down_read(&pcmcia_socket_list_rwsem);
305 list_for_each_entry(s, &pcmcia_socket_list, socket_list)
306 if (s->sock == nr) {
307 up_read(&pcmcia_socket_list_rwsem);
308 return s;
309 }
310 up_read(&pcmcia_socket_list_rwsem);
311
312 return NULL;
313
314}
315EXPORT_SYMBOL(pcmcia_get_socket_by_nr);
316
Randy Dunlap78187862007-12-10 15:49:22 -0800317/*
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700318 * The central event handler. Send_event() sends an event to the
319 * 16-bit subsystem, which then calls the relevant device drivers.
320 * Parse_events() interprets the event bits from
321 * a card status change report. Do_shutdown() handles the high
322 * priority stuff associated with a card removal.
323 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325/* NOTE: send_event needs to be called with skt->sem held. */
326
327static int send_event(struct pcmcia_socket *s, event_t event, int priority)
328{
329 int ret;
330
331 if (s->state & SOCKET_CARDBUS)
332 return 0;
333
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200334 dev_dbg(&s->dev, "send_event(event %d, pri %d, callback 0x%p)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 event, priority, s->callback);
336
337 if (!s->callback)
338 return 0;
339 if (!try_module_get(s->callback->owner))
340 return 0;
341
342 ret = s->callback->event(s, event, priority);
343
344 module_put(s->callback->owner);
345
346 return ret;
347}
348
349static void socket_remove_drivers(struct pcmcia_socket *skt)
350{
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200351 dev_dbg(&skt->dev, "remove_drivers\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
353 send_event(skt, CS_EVENT_CARD_REMOVAL, CS_EVENT_PRI_HIGH);
354}
355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356static int socket_reset(struct pcmcia_socket *skt)
357{
358 int status, i;
359
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200360 dev_dbg(&skt->dev, "reset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362 skt->socket.flags |= SS_OUTPUT_ENA | SS_RESET;
363 skt->ops->set_socket(skt, &skt->socket);
364 udelay((long)reset_time);
365
366 skt->socket.flags &= ~SS_RESET;
367 skt->ops->set_socket(skt, &skt->socket);
368
369 msleep(unreset_delay * 10);
370 for (i = 0; i < unreset_limit; i++) {
371 skt->ops->get_status(skt, &status);
372
373 if (!(status & SS_DETECT))
Dominik Brodowski3939c1ef2008-08-03 11:10:56 +0200374 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376 if (status & SS_READY)
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200377 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
379 msleep(unreset_check * 10);
380 }
381
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200382 dev_printk(KERN_ERR, &skt->dev, "time out after reset.\n");
Dominik Brodowski85671422008-08-03 11:15:45 +0200383 return -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384}
385
Randy Dunlap78187862007-12-10 15:49:22 -0800386/*
Daniel Ritz6e493882005-12-30 15:12:35 +0100387 * socket_setup() and socket_shutdown() are called by the main event handler
388 * when card insertion and removal events are received.
389 * socket_setup() turns on socket power and resets the socket, in two stages.
390 * socket_shutdown() unconfigures a socket and turns off socket power.
391 */
392static void socket_shutdown(struct pcmcia_socket *s)
393{
394 int status;
395
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200396 dev_dbg(&s->dev, "shutdown\n");
Daniel Ritz6e493882005-12-30 15:12:35 +0100397
398 socket_remove_drivers(s);
399 s->state &= SOCKET_INUSE | SOCKET_PRESENT;
400 msleep(shutdown_delay * 10);
401 s->state &= SOCKET_INUSE;
402
403 /* Blank out the socket state */
404 s->socket = dead_socket;
405 s->ops->init(s);
406 s->ops->set_socket(s, &s->socket);
407 s->irq.AssignedIRQ = s->irq.Config = 0;
408 s->lock_count = 0;
409 destroy_cis_cache(s);
Dominik Brodowski904e3772010-01-02 12:28:04 +0100410 kfree(s->fake_cis);
411 s->fake_cis = NULL;
Daniel Ritz6e493882005-12-30 15:12:35 +0100412#ifdef CONFIG_CARDBUS
413 cb_free(s);
414#endif
415 s->functions = 0;
Daniel Ritz6e493882005-12-30 15:12:35 +0100416
Daniel Ritzafb2c162007-07-31 00:38:08 -0700417 /* give socket some time to power down */
418 msleep(100);
419
Daniel Ritz6e493882005-12-30 15:12:35 +0100420 s->ops->get_status(s, &status);
421 if (status & SS_POWERON) {
Dominik Brodowski2e55bf62008-08-02 18:08:38 +0200422 dev_printk(KERN_ERR, &s->dev,
423 "*** DANGER *** unable to remove socket power\n");
Daniel Ritz6e493882005-12-30 15:12:35 +0100424 }
425
426 cs_socket_put(s);
427}
428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429static int socket_setup(struct pcmcia_socket *skt, int initial_delay)
430{
431 int status, i;
432
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200433 dev_dbg(&skt->dev, "setup\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
435 skt->ops->get_status(skt, &status);
436 if (!(status & SS_DETECT))
Dominik Brodowski3939c1ef2008-08-03 11:10:56 +0200437 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439 msleep(initial_delay * 10);
440
441 for (i = 0; i < 100; i++) {
442 skt->ops->get_status(skt, &status);
443 if (!(status & SS_DETECT))
Dominik Brodowski3939c1ef2008-08-03 11:10:56 +0200444 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446 if (!(status & SS_PENDING))
447 break;
448
449 msleep(100);
450 }
451
452 if (status & SS_PENDING) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200453 dev_printk(KERN_ERR, &skt->dev,
454 "voltage interrogation timed out.\n");
Dominik Brodowski85671422008-08-03 11:15:45 +0200455 return -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 }
457
458 if (status & SS_CARDBUS) {
Russell King5bc6b682005-07-07 17:59:07 -0700459 if (!(skt->features & SS_CAP_CARDBUS)) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200460 dev_printk(KERN_ERR, &skt->dev,
461 "cardbus cards are not supported.\n");
Dominik Brodowski610e2372008-08-03 11:58:53 +0200462 return -EINVAL;
Russell King5bc6b682005-07-07 17:59:07 -0700463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 skt->state |= SOCKET_CARDBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 }
466
467 /*
468 * Decode the card voltage requirements, and apply power to the card.
469 */
470 if (status & SS_3VCARD)
471 skt->socket.Vcc = skt->socket.Vpp = 33;
472 else if (!(status & SS_XVCARD))
473 skt->socket.Vcc = skt->socket.Vpp = 50;
474 else {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200475 dev_printk(KERN_ERR, &skt->dev, "unsupported voltage key.\n");
Dominik Brodowski610e2372008-08-03 11:58:53 +0200476 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 }
Daniel Ritzfa912bc2005-06-23 00:10:12 -0700478
479 if (skt->power_hook)
480 skt->power_hook(skt, HOOK_POWER_PRE);
481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 skt->socket.flags = 0;
483 skt->ops->set_socket(skt, &skt->socket);
484
485 /*
486 * Wait "vcc_settle" for the supply to stabilise.
487 */
488 msleep(vcc_settle * 10);
489
490 skt->ops->get_status(skt, &status);
491 if (!(status & SS_POWERON)) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200492 dev_printk(KERN_ERR, &skt->dev, "unable to apply power.\n");
Dominik Brodowski610e2372008-08-03 11:58:53 +0200493 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 }
495
Daniel Ritzfa912bc2005-06-23 00:10:12 -0700496 status = socket_reset(skt);
497
498 if (skt->power_hook)
499 skt->power_hook(skt, HOOK_POWER_POST);
500
501 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502}
503
504/*
505 * Handle card insertion. Setup the socket, reset the card,
506 * and then tell the rest of PCMCIA that a card is present.
507 */
508static int socket_insert(struct pcmcia_socket *skt)
509{
510 int ret;
511
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200512 dev_dbg(&skt->dev, "insert\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
514 if (!cs_socket_get(skt))
Dominik Brodowski3939c1ef2008-08-03 11:10:56 +0200515 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
517 ret = socket_setup(skt, setup_delay);
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200518 if (ret == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 skt->state |= SOCKET_PRESENT;
Dominik Brodowski807277c2005-11-12 23:34:06 +0100520
Dominik Brodowski2e55bf62008-08-02 18:08:38 +0200521 dev_printk(KERN_NOTICE, &skt->dev,
522 "pccard: %s card inserted into slot %d\n",
523 (skt->state & SOCKET_CARDBUS) ? "CardBus" : "PCMCIA",
524 skt->sock);
Dominik Brodowski807277c2005-11-12 23:34:06 +0100525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526#ifdef CONFIG_CARDBUS
527 if (skt->state & SOCKET_CARDBUS) {
528 cb_alloc(skt);
529 skt->state |= SOCKET_CARDBUS_CONFIG;
530 }
531#endif
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200532 dev_dbg(&skt->dev, "insert done\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
534 send_event(skt, CS_EVENT_CARD_INSERTION, CS_EVENT_PRI_LOW);
535 } else {
536 socket_shutdown(skt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 }
538
539 return ret;
540}
541
542static int socket_suspend(struct pcmcia_socket *skt)
543{
544 if (skt->state & SOCKET_SUSPEND)
Dominik Brodowskif9580952008-08-03 11:40:19 +0200545 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547 send_event(skt, CS_EVENT_PM_SUSPEND, CS_EVENT_PRI_LOW);
548 skt->socket = dead_socket;
549 skt->ops->set_socket(skt, &skt->socket);
550 if (skt->ops->suspend)
551 skt->ops->suspend(skt);
552 skt->state |= SOCKET_SUSPEND;
553
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200554 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555}
556
Rafael J. Wysocki9905d1b2009-11-03 10:54:58 +0100557static int socket_early_resume(struct pcmcia_socket *skt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 skt->socket = dead_socket;
560 skt->ops->init(skt);
561 skt->ops->set_socket(skt, &skt->socket);
Rafael J. Wysocki9905d1b2009-11-03 10:54:58 +0100562 if (skt->state & SOCKET_PRESENT)
563 skt->resume_status = socket_setup(skt, resume_delay);
564 return 0;
565}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Rafael J. Wysocki9905d1b2009-11-03 10:54:58 +0100567static int socket_late_resume(struct pcmcia_socket *skt)
568{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 if (!(skt->state & SOCKET_PRESENT)) {
570 skt->state &= ~SOCKET_SUSPEND;
571 return socket_insert(skt);
572 }
573
Rafael J. Wysocki9905d1b2009-11-03 10:54:58 +0100574 if (skt->resume_status == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 /*
576 * FIXME: need a better check here for cardbus cards.
577 */
578 if (verify_cis_cache(skt) != 0) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200579 dev_dbg(&skt->dev, "cis mismatch - different card\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 socket_remove_drivers(skt);
581 destroy_cis_cache(skt);
Dominik Brodowski904e3772010-01-02 12:28:04 +0100582 kfree(skt->fake_cis);
583 skt->fake_cis = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 /*
585 * Workaround: give DS time to schedule removal.
586 * Remove me once the 100ms delay is eliminated
587 * in ds.c
588 */
589 msleep(200);
590 send_event(skt, CS_EVENT_CARD_INSERTION, CS_EVENT_PRI_LOW);
591 } else {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200592 dev_dbg(&skt->dev, "cis matches cache\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 send_event(skt, CS_EVENT_PM_RESUME, CS_EVENT_PRI_LOW);
594 }
595 } else {
596 socket_shutdown(skt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 }
598
599 skt->state &= ~SOCKET_SUSPEND;
600
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200601 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602}
603
Rafael J. Wysocki9905d1b2009-11-03 10:54:58 +0100604/*
605 * Resume a socket. If a card is present, verify its CIS against
606 * our cached copy. If they are different, the card has been
607 * replaced, and we need to tell the drivers.
608 */
609static int socket_resume(struct pcmcia_socket *skt)
610{
611 if (!(skt->state & SOCKET_SUSPEND))
612 return -EBUSY;
613
614 socket_early_resume(skt);
615 return socket_late_resume(skt);
616}
617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618static void socket_remove(struct pcmcia_socket *skt)
619{
Dominik Brodowski2e55bf62008-08-02 18:08:38 +0200620 dev_printk(KERN_NOTICE, &skt->dev,
621 "pccard: card ejected from slot %d\n", skt->sock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 socket_shutdown(skt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623}
624
625/*
626 * Process a socket card detect status change.
627 *
628 * If we don't have a card already present, delay the detect event for
629 * about 20ms (to be on the safe side) before reading the socket status.
630 *
631 * Some i82365-based systems send multiple SS_DETECT events during card
632 * insertion, and the "card present" status bit seems to bounce. This
633 * will probably be true with GPIO-based card detection systems after
634 * the product has aged.
635 */
636static void socket_detect_change(struct pcmcia_socket *skt)
637{
638 if (!(skt->state & SOCKET_SUSPEND)) {
639 int status;
640
641 if (!(skt->state & SOCKET_PRESENT))
642 msleep(20);
643
644 skt->ops->get_status(skt, &status);
645 if ((skt->state & SOCKET_PRESENT) &&
646 !(status & SS_DETECT))
647 socket_remove(skt);
648 if (!(skt->state & SOCKET_PRESENT) &&
649 (status & SS_DETECT))
650 socket_insert(skt);
651 }
652}
653
654static int pccardd(void *__skt)
655{
656 struct pcmcia_socket *skt = __skt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 int ret;
658
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 skt->thread = current;
660 skt->socket = dead_socket;
661 skt->ops->init(skt);
662 skt->ops->set_socket(skt, &skt->socket);
663
664 /* register with the device core */
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200665 ret = device_register(&skt->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 if (ret) {
Dominik Brodowski2e55bf62008-08-02 18:08:38 +0200667 dev_printk(KERN_WARNING, &skt->dev,
668 "PCMCIA: unable to register socket\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 skt->thread = NULL;
Serge E. Hallyn1da9ab72006-06-14 08:01:26 -0500670 complete(&skt->thread_done);
671 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 }
David Brownell4356d732008-04-28 01:03:20 -0700673 ret = pccard_sysfs_add_socket(&skt->dev);
674 if (ret)
675 dev_warn(&skt->dev, "err %d adding socket attributes\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Daniel Ritzbf4de6f2005-09-09 13:03:23 -0700677 complete(&skt->thread_done);
678
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700679 set_freezable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 for (;;) {
681 unsigned long flags;
682 unsigned int events;
683
684 set_current_state(TASK_INTERRUPTIBLE);
685
686 spin_lock_irqsave(&skt->thread_lock, flags);
687 events = skt->thread_events;
688 skt->thread_events = 0;
689 spin_unlock_irqrestore(&skt->thread_lock, flags);
690
691 if (events) {
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100692 mutex_lock(&skt->skt_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 if (events & SS_DETECT)
694 socket_detect_change(skt);
695 if (events & SS_BATDEAD)
696 send_event(skt, CS_EVENT_BATTERY_DEAD, CS_EVENT_PRI_LOW);
697 if (events & SS_BATWARN)
698 send_event(skt, CS_EVENT_BATTERY_LOW, CS_EVENT_PRI_LOW);
699 if (events & SS_READY)
700 send_event(skt, CS_EVENT_READY_CHANGE, CS_EVENT_PRI_LOW);
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100701 mutex_unlock(&skt->skt_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 continue;
703 }
704
Serge E. Hallyn1da9ab72006-06-14 08:01:26 -0500705 if (kthread_should_stop())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 break;
Daniel Ritzbf4de6f2005-09-09 13:03:23 -0700707
708 schedule();
709 try_to_freeze();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 }
Steven Rostedt220ec022005-10-10 11:13:17 -0400711 /* make sure we are running before we exit */
712 set_current_state(TASK_RUNNING);
713
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 /* remove from the device core */
David Brownell4356d732008-04-28 01:03:20 -0700715 pccard_sysfs_remove_socket(&skt->dev);
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200716 device_unregister(&skt->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Serge E. Hallyn1da9ab72006-06-14 08:01:26 -0500718 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719}
720
721/*
722 * Yenta (at least) probes interrupts before registering the socket and
723 * starting the handler thread.
724 */
725void pcmcia_parse_events(struct pcmcia_socket *s, u_int events)
726{
Arjan van de Ven4b7a89a2006-06-30 10:31:13 +0200727 unsigned long flags;
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200728 dev_dbg(&s->dev, "parse_events: events %08x\n", events);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 if (s->thread) {
Arjan van de Ven4b7a89a2006-06-30 10:31:13 +0200730 spin_lock_irqsave(&s->thread_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 s->thread_events |= events;
Arjan van de Ven4b7a89a2006-06-30 10:31:13 +0200732 spin_unlock_irqrestore(&s->thread_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Christoph Hellwig0b402092007-04-22 13:55:36 +0100734 wake_up_process(s->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 }
736} /* pcmcia_parse_events */
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700737EXPORT_SYMBOL(pcmcia_parse_events);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
740/* register pcmcia_callback */
741int pccard_register_pcmcia(struct pcmcia_socket *s, struct pcmcia_callback *c)
742{
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100743 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100745 /* s->skt_mutex also protects s->callback */
746 mutex_lock(&s->skt_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
748 if (c) {
749 /* registration */
750 if (s->callback) {
751 ret = -EBUSY;
752 goto err;
753 }
754
755 s->callback = c;
756
757 if ((s->state & (SOCKET_PRESENT|SOCKET_CARDBUS)) == SOCKET_PRESENT)
758 send_event(s, CS_EVENT_CARD_INSERTION, CS_EVENT_PRI_LOW);
759 } else
760 s->callback = NULL;
761 err:
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100762 mutex_unlock(&s->skt_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
764 return ret;
765}
766EXPORT_SYMBOL(pccard_register_pcmcia);
767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700769/* I'm not sure which "reset" function this is supposed to use,
770 * but for now, it uses the low-level interface's reset, not the
771 * CIS register.
772 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Dominik Brodowski994917f2008-08-31 15:20:26 +0200774int pcmcia_reset_card(struct pcmcia_socket *skt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
776 int ret;
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700777
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200778 dev_dbg(&skt->dev, "resetting socket\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100780 mutex_lock(&skt->skt_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 do {
782 if (!(skt->state & SOCKET_PRESENT)) {
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200783 dev_dbg(&skt->dev, "can't reset, not present\n");
Dominik Brodowski3939c1ef2008-08-03 11:10:56 +0200784 ret = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 break;
786 }
787 if (skt->state & SOCKET_SUSPEND) {
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200788 dev_dbg(&skt->dev, "can't reset, suspended\n");
Dominik Brodowskif9580952008-08-03 11:40:19 +0200789 ret = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 break;
791 }
792 if (skt->state & SOCKET_CARDBUS) {
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200793 dev_dbg(&skt->dev, "can't reset, is cardbus\n");
Dominik Brodowskide6405e2008-08-03 10:47:59 +0200794 ret = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 break;
796 }
797
798 ret = send_event(skt, CS_EVENT_RESET_REQUEST, CS_EVENT_PRI_LOW);
799 if (ret == 0) {
800 send_event(skt, CS_EVENT_RESET_PHYSICAL, CS_EVENT_PRI_LOW);
Dominik Brodowski8e9e7932006-01-06 00:02:03 +0100801 if (skt->callback)
802 skt->callback->suspend(skt);
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200803 if (socket_reset(skt) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 send_event(skt, CS_EVENT_CARD_RESET, CS_EVENT_PRI_LOW);
Dominik Brodowski8e9e7932006-01-06 00:02:03 +0100805 if (skt->callback)
806 skt->callback->resume(skt);
807 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 }
809
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200810 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 } while (0);
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100812 mutex_unlock(&skt->skt_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
814 return ret;
815} /* reset_card */
Dominik Brodowski994917f2008-08-31 15:20:26 +0200816EXPORT_SYMBOL(pcmcia_reset_card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700819/* These shut down or wake up a socket. They are sort of user
820 * initiated versions of the APM suspend and resume actions.
821 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822int pcmcia_suspend_card(struct pcmcia_socket *skt)
823{
824 int ret;
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700825
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200826 dev_dbg(&skt->dev, "suspending socket\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100828 mutex_lock(&skt->skt_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 do {
830 if (!(skt->state & SOCKET_PRESENT)) {
Dominik Brodowski3939c1ef2008-08-03 11:10:56 +0200831 ret = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 break;
833 }
834 if (skt->state & SOCKET_CARDBUS) {
Dominik Brodowskide6405e2008-08-03 10:47:59 +0200835 ret = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 break;
837 }
Dominik Brodowski8e9e7932006-01-06 00:02:03 +0100838 if (skt->callback) {
839 ret = skt->callback->suspend(skt);
840 if (ret)
841 break;
842 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 ret = socket_suspend(skt);
844 } while (0);
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100845 mutex_unlock(&skt->skt_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
847 return ret;
848} /* suspend_card */
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700849EXPORT_SYMBOL(pcmcia_suspend_card);
850
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
852int pcmcia_resume_card(struct pcmcia_socket *skt)
853{
854 int ret;
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100855
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200856 dev_dbg(&skt->dev, "waking up socket\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100858 mutex_lock(&skt->skt_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 do {
860 if (!(skt->state & SOCKET_PRESENT)) {
Dominik Brodowski3939c1ef2008-08-03 11:10:56 +0200861 ret = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 break;
863 }
864 if (skt->state & SOCKET_CARDBUS) {
Dominik Brodowskide6405e2008-08-03 10:47:59 +0200865 ret = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 break;
867 }
868 ret = socket_resume(skt);
Dominik Brodowski8e9e7932006-01-06 00:02:03 +0100869 if (!ret && skt->callback)
870 skt->callback->resume(skt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 } while (0);
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100872 mutex_unlock(&skt->skt_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
874 return ret;
875} /* resume_card */
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700876EXPORT_SYMBOL(pcmcia_resume_card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700879/* These handle user requests to eject or insert a card. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880int pcmcia_eject_card(struct pcmcia_socket *skt)
881{
882 int ret;
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100883
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200884 dev_dbg(&skt->dev, "user eject request\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
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 = -ENODEV;
890 break;
891 }
892
893 ret = send_event(skt, CS_EVENT_EJECTION_REQUEST, CS_EVENT_PRI_LOW);
894 if (ret != 0) {
895 ret = -EINVAL;
896 break;
897 }
898
899 socket_remove(skt);
900 ret = 0;
901 } while (0);
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100902 mutex_unlock(&skt->skt_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
904 return ret;
905} /* eject_card */
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700906EXPORT_SYMBOL(pcmcia_eject_card);
907
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
909int pcmcia_insert_card(struct pcmcia_socket *skt)
910{
911 int ret;
912
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200913 dev_dbg(&skt->dev, "user insert request\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100915 mutex_lock(&skt->skt_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 do {
917 if (skt->state & SOCKET_PRESENT) {
918 ret = -EBUSY;
919 break;
920 }
Dominik Brodowski3939c1ef2008-08-03 11:10:56 +0200921 if (socket_insert(skt) == -ENODEV) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 ret = -ENODEV;
923 break;
924 }
925 ret = 0;
926 } while (0);
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100927 mutex_unlock(&skt->skt_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
929 return ret;
930} /* insert_card */
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700931EXPORT_SYMBOL(pcmcia_insert_card);
932
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
Kay Sievers7eff2e72007-08-14 15:15:12 +0200934static int pcmcia_socket_uevent(struct device *dev,
935 struct kobj_uevent_env *env)
Dominik Brodowski3ee13932005-06-27 16:28:05 -0700936{
937 struct pcmcia_socket *s = container_of(dev, struct pcmcia_socket, dev);
Dominik Brodowski3ee13932005-06-27 16:28:05 -0700938
Kay Sievers7eff2e72007-08-14 15:15:12 +0200939 if (add_uevent_var(env, "SOCKET_NO=%u", s->sock))
Dominik Brodowski3ee13932005-06-27 16:28:05 -0700940 return -ENOMEM;
941
Dominik Brodowski3ee13932005-06-27 16:28:05 -0700942 return 0;
943}
944
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Dominik Brodowski378a33a2005-06-27 16:28:54 -0700946static struct completion pcmcia_unload;
947
948static void pcmcia_release_socket_class(struct class *data)
949{
950 complete(&pcmcia_unload);
951}
952
953
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954struct class pcmcia_socket_class = {
955 .name = "pcmcia_socket",
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200956 .dev_uevent = pcmcia_socket_uevent,
957 .dev_release = pcmcia_release_socket,
Dominik Brodowski378a33a2005-06-27 16:28:54 -0700958 .class_release = pcmcia_release_socket_class,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959};
960EXPORT_SYMBOL(pcmcia_socket_class);
961
962
963static int __init init_pcmcia_cs(void)
964{
Dominik Brodowski378a33a2005-06-27 16:28:54 -0700965 init_completion(&pcmcia_unload);
David Brownell4356d732008-04-28 01:03:20 -0700966 return class_register(&pcmcia_socket_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967}
968
969static void __exit exit_pcmcia_cs(void)
970{
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700971 class_unregister(&pcmcia_socket_class);
Dominik Brodowski378a33a2005-06-27 16:28:54 -0700972 wait_for_completion(&pcmcia_unload);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973}
974
975subsys_initcall(init_pcmcia_cs);
976module_exit(exit_pcmcia_cs);
977