blob: 75ed866e6953944c871dd46bfc94f4a54da997bd [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
Dominik Brodowski9fea84f2009-12-07 22:11:45 +010079struct pcmcia_socket *pcmcia_get_socket(struct pcmcia_socket *skt)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080{
Greg Kroah-Hartman87373312006-09-12 17:00:10 +020081 struct device *dev = get_device(&skt->dev);
82 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 return NULL;
Dominik Brodowski593f010b2010-01-02 22:59:15 +010084 return dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085}
86EXPORT_SYMBOL(pcmcia_get_socket);
87
88
89void pcmcia_put_socket(struct pcmcia_socket *skt)
90{
Greg Kroah-Hartman87373312006-09-12 17:00:10 +020091 put_device(&skt->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092}
93EXPORT_SYMBOL(pcmcia_put_socket);
94
95
Greg Kroah-Hartman87373312006-09-12 17:00:10 +020096static void pcmcia_release_socket(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
Greg Kroah-Hartman87373312006-09-12 17:00:10 +020098 struct pcmcia_socket *socket = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100 complete(&socket->socket_released);
101}
102
103static int pccardd(void *__skt);
104
105/**
106 * pcmcia_register_socket - add a new pcmcia socket device
Randy Dunlap78187862007-12-10 15:49:22 -0800107 * @socket: the &socket to register
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 */
109int pcmcia_register_socket(struct pcmcia_socket *socket)
110{
Serge E. Hallyn1da9ab72006-06-14 08:01:26 -0500111 struct task_struct *tsk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 int ret;
113
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200114 if (!socket || !socket->ops || !socket->dev.parent || !socket->resource_ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 return -EINVAL;
116
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200117 dev_dbg(&socket->dev, "pcmcia_register_socket(0x%p)\n", socket->ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 /* try to obtain a socket number [yes, it gets ugly if we
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700120 * register more than 2^sizeof(unsigned int) pcmcia
121 * sockets... but the socket number is deprecated
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 * anyways, so I don't care] */
123 down_write(&pcmcia_socket_list_rwsem);
124 if (list_empty(&pcmcia_socket_list))
125 socket->sock = 0;
126 else {
127 unsigned int found, i = 1;
128 struct pcmcia_socket *tmp;
129 do {
130 found = 1;
131 list_for_each_entry(tmp, &pcmcia_socket_list, socket_list) {
132 if (tmp->sock == i)
133 found = 0;
134 }
135 i++;
136 } while (!found);
137 socket->sock = i - 1;
138 }
139 list_add_tail(&socket->socket_list, &pcmcia_socket_list);
140 up_write(&pcmcia_socket_list_rwsem);
141
Russell King5bc6b682005-07-07 17:59:07 -0700142#ifndef CONFIG_CARDBUS
143 /*
144 * If we do not support Cardbus, ensure that
145 * the Cardbus socket capability is disabled.
146 */
147 socket->features &= ~SS_CAP_CARDBUS;
148#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150 /* set proper values in socket->dev */
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200151 dev_set_drvdata(&socket->dev, socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 socket->dev.class = &pcmcia_socket_class;
Kay Sievers25096982008-11-01 11:46:06 +0100153 dev_set_name(&socket->dev, "pcmcia_socket%u", socket->sock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155 /* base address = 0, map = 0 */
156 socket->cis_mem.flags = 0;
157 socket->cis_mem.speed = cis_speed;
158
159 INIT_LIST_HEAD(&socket->cis_cache);
160
161 init_completion(&socket->socket_released);
162 init_completion(&socket->thread_done);
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100163 mutex_init(&socket->skt_mutex);
Dominik Brodowski6b8e0872010-01-12 21:42:51 +0100164 mutex_init(&socket->ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 spin_lock_init(&socket->thread_lock);
166
Dominik Brodowskib1769452008-11-09 21:47:47 +0100167 if (socket->resource_ops->init) {
Dominik Brodowskicfe5d802010-01-17 19:31:45 +0100168 mutex_lock(&socket->ops_mutex);
Dominik Brodowskib1769452008-11-09 21:47:47 +0100169 ret = socket->resource_ops->init(socket);
Dominik Brodowskicfe5d802010-01-17 19:31:45 +0100170 mutex_unlock(&socket->ops_mutex);
Dominik Brodowskib1769452008-11-09 21:47:47 +0100171 if (ret)
172 goto err;
173 }
174
Serge E. Hallyn1da9ab72006-06-14 08:01:26 -0500175 tsk = kthread_run(pccardd, socket, "pccardd");
176 if (IS_ERR(tsk)) {
177 ret = PTR_ERR(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 goto err;
Serge E. Hallyn1da9ab72006-06-14 08:01:26 -0500179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181 wait_for_completion(&socket->thread_done);
Serge E. Hallyn1da9ab72006-06-14 08:01:26 -0500182 if (!socket->thread) {
Dominik Brodowski2e55bf62008-08-02 18:08:38 +0200183 dev_printk(KERN_WARNING, &socket->dev,
184 "PCMCIA: warning: socket thread did not start\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 return -EIO;
186 }
Serge E. Hallyn1da9ab72006-06-14 08:01:26 -0500187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 pcmcia_parse_events(socket, SS_DETECT);
189
Dominik Brodowski638bba52009-11-07 12:26:17 +0100190 /*
191 * Let's try to get the PCMCIA module for 16-bit PCMCIA support.
192 * If it fails, it doesn't matter -- we still have 32-bit CardBus
193 * support to offer, so this is not a failure mode.
194 */
195 request_module_nowait("pcmcia");
196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 return 0;
198
199 err:
200 down_write(&pcmcia_socket_list_rwsem);
201 list_del(&socket->socket_list);
202 up_write(&pcmcia_socket_list_rwsem);
203 return ret;
204} /* pcmcia_register_socket */
205EXPORT_SYMBOL(pcmcia_register_socket);
206
207
208/**
209 * pcmcia_unregister_socket - remove a pcmcia socket device
Randy Dunlap78187862007-12-10 15:49:22 -0800210 * @socket: the &socket to unregister
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 */
212void pcmcia_unregister_socket(struct pcmcia_socket *socket)
213{
214 if (!socket)
215 return;
216
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200217 dev_dbg(&socket->dev, "pcmcia_unregister_socket(0x%p)\n", socket->ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Christoph Hellwig0b402092007-04-22 13:55:36 +0100219 if (socket->thread)
Serge E. Hallyn1da9ab72006-06-14 08:01:26 -0500220 kthread_stop(socket->thread);
Christoph Hellwig0b402092007-04-22 13:55:36 +0100221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 /* remove from our own list */
223 down_write(&pcmcia_socket_list_rwsem);
224 list_del(&socket->socket_list);
225 up_write(&pcmcia_socket_list_rwsem);
226
227 /* wait for sysfs to drop all references */
Dominik Brodowskicfe5d802010-01-17 19:31:45 +0100228 if (socket->resource_ops->exit) {
229 mutex_lock(&socket->ops_mutex);
Dominik Brodowski385ee872010-01-06 11:23:58 +0100230 socket->resource_ops->exit(socket);
Dominik Brodowskicfe5d802010-01-17 19:31:45 +0100231 mutex_unlock(&socket->ops_mutex);
232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 wait_for_completion(&socket->socket_released);
234} /* pcmcia_unregister_socket */
235EXPORT_SYMBOL(pcmcia_unregister_socket);
236
237
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100238struct pcmcia_socket *pcmcia_get_socket_by_nr(unsigned int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
240 struct pcmcia_socket *s;
241
242 down_read(&pcmcia_socket_list_rwsem);
243 list_for_each_entry(s, &pcmcia_socket_list, socket_list)
244 if (s->sock == nr) {
245 up_read(&pcmcia_socket_list_rwsem);
246 return s;
247 }
248 up_read(&pcmcia_socket_list_rwsem);
249
250 return NULL;
251
252}
253EXPORT_SYMBOL(pcmcia_get_socket_by_nr);
254
Randy Dunlap78187862007-12-10 15:49:22 -0800255/*
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700256 * The central event handler. Send_event() sends an event to the
257 * 16-bit subsystem, which then calls the relevant device drivers.
258 * Parse_events() interprets the event bits from
259 * a card status change report. Do_shutdown() handles the high
260 * priority stuff associated with a card removal.
261 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263/* NOTE: send_event needs to be called with skt->sem held. */
264
265static int send_event(struct pcmcia_socket *s, event_t event, int priority)
266{
267 int ret;
268
Dominik Brodowski88b060d2010-01-02 14:14:23 +0100269 if ((s->state & SOCKET_CARDBUS) && (event != CS_EVENT_CARD_REMOVAL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 return 0;
271
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200272 dev_dbg(&s->dev, "send_event(event %d, pri %d, callback 0x%p)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 event, priority, s->callback);
274
275 if (!s->callback)
276 return 0;
277 if (!try_module_get(s->callback->owner))
278 return 0;
279
280 ret = s->callback->event(s, event, priority);
281
282 module_put(s->callback->owner);
283
284 return ret;
285}
286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287static int socket_reset(struct pcmcia_socket *skt)
288{
289 int status, i;
290
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200291 dev_dbg(&skt->dev, "reset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
293 skt->socket.flags |= SS_OUTPUT_ENA | SS_RESET;
294 skt->ops->set_socket(skt, &skt->socket);
295 udelay((long)reset_time);
296
297 skt->socket.flags &= ~SS_RESET;
298 skt->ops->set_socket(skt, &skt->socket);
299
300 msleep(unreset_delay * 10);
301 for (i = 0; i < unreset_limit; i++) {
302 skt->ops->get_status(skt, &status);
303
304 if (!(status & SS_DETECT))
Dominik Brodowski3939c1ef2008-08-03 11:10:56 +0200305 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
307 if (status & SS_READY)
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200308 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
310 msleep(unreset_check * 10);
311 }
312
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200313 dev_printk(KERN_ERR, &skt->dev, "time out after reset.\n");
Dominik Brodowski85671422008-08-03 11:15:45 +0200314 return -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315}
316
Randy Dunlap78187862007-12-10 15:49:22 -0800317/*
Daniel Ritz6e493882005-12-30 15:12:35 +0100318 * socket_setup() and socket_shutdown() are called by the main event handler
319 * when card insertion and removal events are received.
320 * socket_setup() turns on socket power and resets the socket, in two stages.
321 * socket_shutdown() unconfigures a socket and turns off socket power.
322 */
323static void socket_shutdown(struct pcmcia_socket *s)
324{
325 int status;
326
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200327 dev_dbg(&s->dev, "shutdown\n");
Daniel Ritz6e493882005-12-30 15:12:35 +0100328
Dominik Brodowski88b060d2010-01-02 14:14:23 +0100329 send_event(s, CS_EVENT_CARD_REMOVAL, CS_EVENT_PRI_HIGH);
Dominik Brodowski9e86749c2010-01-16 00:26:33 +0100330
331 mutex_lock(&s->ops_mutex);
Daniel Ritz6e493882005-12-30 15:12:35 +0100332 s->state &= SOCKET_INUSE | SOCKET_PRESENT;
333 msleep(shutdown_delay * 10);
334 s->state &= SOCKET_INUSE;
335
336 /* Blank out the socket state */
337 s->socket = dead_socket;
338 s->ops->init(s);
339 s->ops->set_socket(s, &s->socket);
340 s->irq.AssignedIRQ = s->irq.Config = 0;
341 s->lock_count = 0;
Dominik Brodowski904e3772010-01-02 12:28:04 +0100342 kfree(s->fake_cis);
343 s->fake_cis = NULL;
Dominik Brodowskic3bfc962010-01-18 08:43:39 +0100344 s->functions = 0;
345
346 /* From here on we can be sure that only we (that is, the
347 * pccardd thread) accesses this socket, and all (16-bit)
348 * PCMCIA interactions are gone. Therefore, release
349 * ops_mutex so that we don't get a sysfs-related lockdep
350 * warning.
351 */
352 mutex_unlock(&s->ops_mutex);
353
Daniel Ritz6e493882005-12-30 15:12:35 +0100354#ifdef CONFIG_CARDBUS
355 cb_free(s);
356#endif
Daniel Ritz6e493882005-12-30 15:12:35 +0100357
Daniel Ritzafb2c162007-07-31 00:38:08 -0700358 /* give socket some time to power down */
359 msleep(100);
360
Daniel Ritz6e493882005-12-30 15:12:35 +0100361 s->ops->get_status(s, &status);
362 if (status & SS_POWERON) {
Dominik Brodowski2e55bf62008-08-02 18:08:38 +0200363 dev_printk(KERN_ERR, &s->dev,
364 "*** DANGER *** unable to remove socket power\n");
Daniel Ritz6e493882005-12-30 15:12:35 +0100365 }
366
Dominik Brodowski3970dd82010-01-02 23:19:45 +0100367 s->state &= ~SOCKET_INUSE;
Daniel Ritz6e493882005-12-30 15:12:35 +0100368}
369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370static int socket_setup(struct pcmcia_socket *skt, int initial_delay)
371{
372 int status, i;
373
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200374 dev_dbg(&skt->dev, "setup\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376 skt->ops->get_status(skt, &status);
377 if (!(status & SS_DETECT))
Dominik Brodowski3939c1ef2008-08-03 11:10:56 +0200378 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
380 msleep(initial_delay * 10);
381
382 for (i = 0; i < 100; i++) {
383 skt->ops->get_status(skt, &status);
384 if (!(status & SS_DETECT))
Dominik Brodowski3939c1ef2008-08-03 11:10:56 +0200385 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387 if (!(status & SS_PENDING))
388 break;
389
390 msleep(100);
391 }
392
393 if (status & SS_PENDING) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200394 dev_printk(KERN_ERR, &skt->dev,
395 "voltage interrogation timed out.\n");
Dominik Brodowski85671422008-08-03 11:15:45 +0200396 return -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 }
398
399 if (status & SS_CARDBUS) {
Russell King5bc6b682005-07-07 17:59:07 -0700400 if (!(skt->features & SS_CAP_CARDBUS)) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200401 dev_printk(KERN_ERR, &skt->dev,
402 "cardbus cards are not supported.\n");
Dominik Brodowski610e2372008-08-03 11:58:53 +0200403 return -EINVAL;
Russell King5bc6b682005-07-07 17:59:07 -0700404 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 skt->state |= SOCKET_CARDBUS;
Dominik Brodowski88b060d2010-01-02 14:14:23 +0100406 } else
407 skt->state &= ~SOCKET_CARDBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409 /*
410 * Decode the card voltage requirements, and apply power to the card.
411 */
412 if (status & SS_3VCARD)
413 skt->socket.Vcc = skt->socket.Vpp = 33;
414 else if (!(status & SS_XVCARD))
415 skt->socket.Vcc = skt->socket.Vpp = 50;
416 else {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200417 dev_printk(KERN_ERR, &skt->dev, "unsupported voltage key.\n");
Dominik Brodowski610e2372008-08-03 11:58:53 +0200418 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 }
Daniel Ritzfa912bc2005-06-23 00:10:12 -0700420
421 if (skt->power_hook)
422 skt->power_hook(skt, HOOK_POWER_PRE);
423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 skt->socket.flags = 0;
425 skt->ops->set_socket(skt, &skt->socket);
426
427 /*
428 * Wait "vcc_settle" for the supply to stabilise.
429 */
430 msleep(vcc_settle * 10);
431
432 skt->ops->get_status(skt, &status);
433 if (!(status & SS_POWERON)) {
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200434 dev_printk(KERN_ERR, &skt->dev, "unable to apply power.\n");
Dominik Brodowski610e2372008-08-03 11:58:53 +0200435 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 }
437
Daniel Ritzfa912bc2005-06-23 00:10:12 -0700438 status = socket_reset(skt);
439
440 if (skt->power_hook)
441 skt->power_hook(skt, HOOK_POWER_POST);
442
443 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444}
445
446/*
447 * Handle card insertion. Setup the socket, reset the card,
448 * and then tell the rest of PCMCIA that a card is present.
449 */
450static int socket_insert(struct pcmcia_socket *skt)
451{
452 int ret;
453
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200454 dev_dbg(&skt->dev, "insert\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Dominik Brodowski9e86749c2010-01-16 00:26:33 +0100456 mutex_lock(&skt->ops_mutex);
Dominik Brodowskif971dbd2010-01-17 18:13:31 +0100457 if (skt->state & SOCKET_INUSE) {
458 mutex_unlock(&skt->ops_mutex);
459 return -EINVAL;
460 }
Dominik Brodowski3970dd82010-01-02 23:19:45 +0100461 skt->state |= SOCKET_INUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
463 ret = socket_setup(skt, setup_delay);
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200464 if (ret == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 skt->state |= SOCKET_PRESENT;
Dominik Brodowski807277c2005-11-12 23:34:06 +0100466
Dominik Brodowski2e55bf62008-08-02 18:08:38 +0200467 dev_printk(KERN_NOTICE, &skt->dev,
468 "pccard: %s card inserted into slot %d\n",
469 (skt->state & SOCKET_CARDBUS) ? "CardBus" : "PCMCIA",
470 skt->sock);
Dominik Brodowski807277c2005-11-12 23:34:06 +0100471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472#ifdef CONFIG_CARDBUS
473 if (skt->state & SOCKET_CARDBUS) {
474 cb_alloc(skt);
475 skt->state |= SOCKET_CARDBUS_CONFIG;
476 }
477#endif
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200478 dev_dbg(&skt->dev, "insert done\n");
Dominik Brodowski9e86749c2010-01-16 00:26:33 +0100479 mutex_unlock(&skt->ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
481 send_event(skt, CS_EVENT_CARD_INSERTION, CS_EVENT_PRI_LOW);
482 } else {
Dominik Brodowski9e86749c2010-01-16 00:26:33 +0100483 mutex_unlock(&skt->ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 socket_shutdown(skt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 }
486
487 return ret;
488}
489
490static int socket_suspend(struct pcmcia_socket *skt)
491{
492 if (skt->state & SOCKET_SUSPEND)
Dominik Brodowskif9580952008-08-03 11:40:19 +0200493 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Dominik Brodowski9e86749c2010-01-16 00:26:33 +0100495 mutex_lock(&skt->ops_mutex);
Dominik Brodowski88b060d2010-01-02 14:14:23 +0100496 skt->suspended_state = skt->state;
497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 send_event(skt, CS_EVENT_PM_SUSPEND, CS_EVENT_PRI_LOW);
499 skt->socket = dead_socket;
500 skt->ops->set_socket(skt, &skt->socket);
501 if (skt->ops->suspend)
502 skt->ops->suspend(skt);
503 skt->state |= SOCKET_SUSPEND;
Dominik Brodowski9e86749c2010-01-16 00:26:33 +0100504 mutex_unlock(&skt->ops_mutex);
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200505 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506}
507
Rafael J. Wysocki9905d1b2009-11-03 10:54:58 +0100508static int socket_early_resume(struct pcmcia_socket *skt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
Dominik Brodowski9e86749c2010-01-16 00:26:33 +0100510 mutex_lock(&skt->ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 skt->socket = dead_socket;
512 skt->ops->init(skt);
513 skt->ops->set_socket(skt, &skt->socket);
Rafael J. Wysocki9905d1b2009-11-03 10:54:58 +0100514 if (skt->state & SOCKET_PRESENT)
515 skt->resume_status = socket_setup(skt, resume_delay);
Dominik Brodowski9e86749c2010-01-16 00:26:33 +0100516 mutex_unlock(&skt->ops_mutex);
Rafael J. Wysocki9905d1b2009-11-03 10:54:58 +0100517 return 0;
518}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Rafael J. Wysocki9905d1b2009-11-03 10:54:58 +0100520static int socket_late_resume(struct pcmcia_socket *skt)
521{
Dominik Brodowskid7646f72010-03-15 21:46:34 +0100522 int ret;
523
Dominik Brodowski9e86749c2010-01-16 00:26:33 +0100524 mutex_lock(&skt->ops_mutex);
Dominik Brodowski88b060d2010-01-02 14:14:23 +0100525 skt->state &= ~SOCKET_SUSPEND;
Dominik Brodowski9e86749c2010-01-16 00:26:33 +0100526 mutex_unlock(&skt->ops_mutex);
Dominik Brodowski88b060d2010-01-02 14:14:23 +0100527
Dominik Brodowskid7646f72010-03-15 21:46:34 +0100528 if (!(skt->state & SOCKET_PRESENT)) {
529 ret = socket_insert(skt);
530 if (ret == -ENODEV)
531 ret = 0;
532 return ret;
533 }
Dominik Brodowski88b060d2010-01-02 14:14:23 +0100534
535 if (skt->resume_status) {
536 socket_shutdown(skt);
537 return 0;
538 }
539
540 if (skt->suspended_state != skt->state) {
541 dev_dbg(&skt->dev,
542 "suspend state 0x%x != resume state 0x%x\n",
543 skt->suspended_state, skt->state);
544
545 socket_shutdown(skt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 return socket_insert(skt);
547 }
548
Dominik Brodowski88b060d2010-01-02 14:14:23 +0100549#ifdef CONFIG_CARDBUS
550 if (skt->state & SOCKET_CARDBUS) {
551 /* We can't be sure the CardBus card is the same
552 * as the one previously inserted. Therefore, remove
553 * and re-add... */
554 cb_free(skt);
555 cb_alloc(skt);
556 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 }
Dominik Brodowski88b060d2010-01-02 14:14:23 +0100558#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Dominik Brodowski88b060d2010-01-02 14:14:23 +0100560 send_event(skt, CS_EVENT_PM_RESUME, CS_EVENT_PRI_LOW);
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200561 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562}
563
Rafael J. Wysocki9905d1b2009-11-03 10:54:58 +0100564/*
565 * Resume a socket. If a card is present, verify its CIS against
566 * our cached copy. If they are different, the card has been
567 * replaced, and we need to tell the drivers.
568 */
569static int socket_resume(struct pcmcia_socket *skt)
570{
571 if (!(skt->state & SOCKET_SUSPEND))
572 return -EBUSY;
573
574 socket_early_resume(skt);
575 return socket_late_resume(skt);
576}
577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578static void socket_remove(struct pcmcia_socket *skt)
579{
Dominik Brodowski2e55bf62008-08-02 18:08:38 +0200580 dev_printk(KERN_NOTICE, &skt->dev,
581 "pccard: card ejected from slot %d\n", skt->sock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 socket_shutdown(skt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583}
584
585/*
586 * Process a socket card detect status change.
587 *
588 * If we don't have a card already present, delay the detect event for
589 * about 20ms (to be on the safe side) before reading the socket status.
590 *
591 * Some i82365-based systems send multiple SS_DETECT events during card
592 * insertion, and the "card present" status bit seems to bounce. This
593 * will probably be true with GPIO-based card detection systems after
594 * the product has aged.
595 */
596static void socket_detect_change(struct pcmcia_socket *skt)
597{
598 if (!(skt->state & SOCKET_SUSPEND)) {
599 int status;
600
601 if (!(skt->state & SOCKET_PRESENT))
602 msleep(20);
603
604 skt->ops->get_status(skt, &status);
605 if ((skt->state & SOCKET_PRESENT) &&
606 !(status & SS_DETECT))
607 socket_remove(skt);
608 if (!(skt->state & SOCKET_PRESENT) &&
609 (status & SS_DETECT))
610 socket_insert(skt);
611 }
612}
613
614static int pccardd(void *__skt)
615{
616 struct pcmcia_socket *skt = __skt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 int ret;
618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 skt->thread = current;
620 skt->socket = dead_socket;
621 skt->ops->init(skt);
622 skt->ops->set_socket(skt, &skt->socket);
623
624 /* register with the device core */
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200625 ret = device_register(&skt->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 if (ret) {
Dominik Brodowski2e55bf62008-08-02 18:08:38 +0200627 dev_printk(KERN_WARNING, &skt->dev,
628 "PCMCIA: unable to register socket\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 skt->thread = NULL;
Serge E. Hallyn1da9ab72006-06-14 08:01:26 -0500630 complete(&skt->thread_done);
631 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 }
David Brownell4356d732008-04-28 01:03:20 -0700633 ret = pccard_sysfs_add_socket(&skt->dev);
634 if (ret)
635 dev_warn(&skt->dev, "err %d adding socket attributes\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Daniel Ritzbf4de6f2005-09-09 13:03:23 -0700637 complete(&skt->thread_done);
638
Dominik Brodowskid801c1402010-02-17 11:02:22 +0100639 /* wait for userspace to catch up */
640 msleep(250);
641
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700642 set_freezable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 for (;;) {
644 unsigned long flags;
645 unsigned int events;
Dominik Brodowskif971dbd2010-01-17 18:13:31 +0100646 unsigned int sysfs_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
648 set_current_state(TASK_INTERRUPTIBLE);
649
650 spin_lock_irqsave(&skt->thread_lock, flags);
651 events = skt->thread_events;
652 skt->thread_events = 0;
Dominik Brodowskif971dbd2010-01-17 18:13:31 +0100653 sysfs_events = skt->sysfs_events;
654 skt->sysfs_events = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 spin_unlock_irqrestore(&skt->thread_lock, flags);
656
Dominik Brodowskif971dbd2010-01-17 18:13:31 +0100657 mutex_lock(&skt->skt_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 if (events) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 if (events & SS_DETECT)
660 socket_detect_change(skt);
661 if (events & SS_BATDEAD)
662 send_event(skt, CS_EVENT_BATTERY_DEAD, CS_EVENT_PRI_LOW);
663 if (events & SS_BATWARN)
664 send_event(skt, CS_EVENT_BATTERY_LOW, CS_EVENT_PRI_LOW);
665 if (events & SS_READY)
666 send_event(skt, CS_EVENT_READY_CHANGE, CS_EVENT_PRI_LOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 }
668
Dominik Brodowskif971dbd2010-01-17 18:13:31 +0100669 if (sysfs_events) {
670 if (sysfs_events & PCMCIA_UEVENT_EJECT)
671 socket_remove(skt);
672 if (sysfs_events & PCMCIA_UEVENT_INSERT)
673 socket_insert(skt);
674 if ((sysfs_events & PCMCIA_UEVENT_RESUME) &&
675 !(skt->state & SOCKET_CARDBUS)) {
676 ret = socket_resume(skt);
677 if (!ret && skt->callback)
678 skt->callback->resume(skt);
679 }
680 if ((sysfs_events & PCMCIA_UEVENT_SUSPEND) &&
681 !(skt->state & SOCKET_CARDBUS)) {
682 if (skt->callback)
683 ret = skt->callback->suspend(skt);
684 else
685 ret = 0;
686 if (!ret)
687 socket_suspend(skt);
688 }
Dominik Brodowskiaf461fc2010-01-17 19:30:53 +0100689 if ((sysfs_events & PCMCIA_UEVENT_REQUERY) &&
690 !(skt->state & SOCKET_CARDBUS)) {
691 if (!ret && skt->callback)
692 skt->callback->requery(skt);
693 }
Dominik Brodowskif971dbd2010-01-17 18:13:31 +0100694 }
695 mutex_unlock(&skt->skt_mutex);
696
697 if (events || sysfs_events)
698 continue;
699
Serge E. Hallyn1da9ab72006-06-14 08:01:26 -0500700 if (kthread_should_stop())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 break;
Daniel Ritzbf4de6f2005-09-09 13:03:23 -0700702
703 schedule();
704 try_to_freeze();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 }
Steven Rostedt220ec022005-10-10 11:13:17 -0400706 /* make sure we are running before we exit */
707 set_current_state(TASK_RUNNING);
708
Dominik Brodowski3970dd82010-01-02 23:19:45 +0100709 /* shut down socket, if a device is still present */
710 if (skt->state & SOCKET_PRESENT) {
711 mutex_lock(&skt->skt_mutex);
712 socket_remove(skt);
713 mutex_unlock(&skt->skt_mutex);
714 }
715
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 /* remove from the device core */
David Brownell4356d732008-04-28 01:03:20 -0700717 pccard_sysfs_remove_socket(&skt->dev);
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200718 device_unregister(&skt->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Serge E. Hallyn1da9ab72006-06-14 08:01:26 -0500720 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721}
722
723/*
724 * Yenta (at least) probes interrupts before registering the socket and
725 * starting the handler thread.
726 */
727void pcmcia_parse_events(struct pcmcia_socket *s, u_int events)
728{
Arjan van de Ven4b7a89a2006-06-30 10:31:13 +0200729 unsigned long flags;
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200730 dev_dbg(&s->dev, "parse_events: events %08x\n", events);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 if (s->thread) {
Arjan van de Ven4b7a89a2006-06-30 10:31:13 +0200732 spin_lock_irqsave(&s->thread_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 s->thread_events |= events;
Arjan van de Ven4b7a89a2006-06-30 10:31:13 +0200734 spin_unlock_irqrestore(&s->thread_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Christoph Hellwig0b402092007-04-22 13:55:36 +0100736 wake_up_process(s->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 }
738} /* pcmcia_parse_events */
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700739EXPORT_SYMBOL(pcmcia_parse_events);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
Dominik Brodowskif971dbd2010-01-17 18:13:31 +0100741/**
742 * pcmcia_parse_uevents() - tell pccardd to issue manual commands
743 * @s: the PCMCIA socket we wan't to command
744 * @events: events to pass to pccardd
745 *
746 * userspace-issued insert, eject, suspend and resume commands must be
747 * handled by pccardd to avoid any sysfs-related deadlocks. Valid events
748 * are PCMCIA_UEVENT_EJECT (for eject), PCMCIA_UEVENT__INSERT (for insert),
Dominik Brodowskiaf461fc2010-01-17 19:30:53 +0100749 * PCMCIA_UEVENT_RESUME (for resume), PCMCIA_UEVENT_SUSPEND (for suspend)
750 * and PCMCIA_UEVENT_REQUERY (for re-querying the PCMCIA card).
Dominik Brodowskif971dbd2010-01-17 18:13:31 +0100751 */
752void pcmcia_parse_uevents(struct pcmcia_socket *s, u_int events)
753{
754 unsigned long flags;
755 dev_dbg(&s->dev, "parse_uevents: events %08x\n", events);
756 if (s->thread) {
757 spin_lock_irqsave(&s->thread_lock, flags);
758 s->sysfs_events |= events;
759 spin_unlock_irqrestore(&s->thread_lock, flags);
760
761 wake_up_process(s->thread);
762 }
763}
764EXPORT_SYMBOL(pcmcia_parse_uevents);
765
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
767/* register pcmcia_callback */
768int pccard_register_pcmcia(struct pcmcia_socket *s, struct pcmcia_callback *c)
769{
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100770 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100772 /* s->skt_mutex also protects s->callback */
773 mutex_lock(&s->skt_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
775 if (c) {
776 /* registration */
777 if (s->callback) {
778 ret = -EBUSY;
779 goto err;
780 }
781
782 s->callback = c;
783
784 if ((s->state & (SOCKET_PRESENT|SOCKET_CARDBUS)) == SOCKET_PRESENT)
785 send_event(s, CS_EVENT_CARD_INSERTION, CS_EVENT_PRI_LOW);
786 } else
787 s->callback = NULL;
788 err:
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100789 mutex_unlock(&s->skt_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791 return ret;
792}
793EXPORT_SYMBOL(pccard_register_pcmcia);
794
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700796/* I'm not sure which "reset" function this is supposed to use,
797 * but for now, it uses the low-level interface's reset, not the
798 * CIS register.
799 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
Dominik Brodowski994917f2008-08-31 15:20:26 +0200801int pcmcia_reset_card(struct pcmcia_socket *skt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
803 int ret;
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700804
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200805 dev_dbg(&skt->dev, "resetting socket\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100807 mutex_lock(&skt->skt_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 do {
809 if (!(skt->state & SOCKET_PRESENT)) {
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200810 dev_dbg(&skt->dev, "can't reset, not present\n");
Dominik Brodowski3939c1ef2008-08-03 11:10:56 +0200811 ret = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 break;
813 }
814 if (skt->state & SOCKET_SUSPEND) {
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200815 dev_dbg(&skt->dev, "can't reset, suspended\n");
Dominik Brodowskif9580952008-08-03 11:40:19 +0200816 ret = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 break;
818 }
819 if (skt->state & SOCKET_CARDBUS) {
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200820 dev_dbg(&skt->dev, "can't reset, is cardbus\n");
Dominik Brodowskide6405e2008-08-03 10:47:59 +0200821 ret = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 break;
823 }
824
825 ret = send_event(skt, CS_EVENT_RESET_REQUEST, CS_EVENT_PRI_LOW);
826 if (ret == 0) {
827 send_event(skt, CS_EVENT_RESET_PHYSICAL, CS_EVENT_PRI_LOW);
Dominik Brodowski8e9e7932006-01-06 00:02:03 +0100828 if (skt->callback)
829 skt->callback->suspend(skt);
Dominik Brodowski9e86749c2010-01-16 00:26:33 +0100830 mutex_lock(&skt->ops_mutex);
831 ret = socket_reset(skt);
832 mutex_unlock(&skt->ops_mutex);
833 if (ret == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 send_event(skt, CS_EVENT_CARD_RESET, CS_EVENT_PRI_LOW);
Dominik Brodowski8e9e7932006-01-06 00:02:03 +0100835 if (skt->callback)
836 skt->callback->resume(skt);
837 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 }
839
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200840 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 } while (0);
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100842 mutex_unlock(&skt->skt_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
844 return ret;
845} /* reset_card */
Dominik Brodowski994917f2008-08-31 15:20:26 +0200846EXPORT_SYMBOL(pcmcia_reset_card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Kay Sievers7eff2e72007-08-14 15:15:12 +0200849static int pcmcia_socket_uevent(struct device *dev,
850 struct kobj_uevent_env *env)
Dominik Brodowski3ee13932005-06-27 16:28:05 -0700851{
852 struct pcmcia_socket *s = container_of(dev, struct pcmcia_socket, dev);
Dominik Brodowski3ee13932005-06-27 16:28:05 -0700853
Kay Sievers7eff2e72007-08-14 15:15:12 +0200854 if (add_uevent_var(env, "SOCKET_NO=%u", s->sock))
Dominik Brodowski3ee13932005-06-27 16:28:05 -0700855 return -ENOMEM;
856
Dominik Brodowski3ee13932005-06-27 16:28:05 -0700857 return 0;
858}
859
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
Dominik Brodowski378a33a2005-06-27 16:28:54 -0700861static struct completion pcmcia_unload;
862
863static void pcmcia_release_socket_class(struct class *data)
864{
865 complete(&pcmcia_unload);
866}
867
868
Dominik Brodowskid7646f72010-03-15 21:46:34 +0100869#ifdef CONFIG_PM
870
871static int __pcmcia_pm_op(struct device *dev,
872 int (*callback) (struct pcmcia_socket *skt))
873{
874 struct pcmcia_socket *s = container_of(dev, struct pcmcia_socket, dev);
875 int ret;
876
877 mutex_lock(&s->skt_mutex);
878 ret = callback(s);
879 mutex_unlock(&s->skt_mutex);
880
881 return ret;
882}
883
884static int pcmcia_socket_dev_suspend_noirq(struct device *dev)
885{
886 return __pcmcia_pm_op(dev, socket_suspend);
887}
888
889static int pcmcia_socket_dev_resume_noirq(struct device *dev)
890{
891 return __pcmcia_pm_op(dev, socket_early_resume);
892}
893
894static int pcmcia_socket_dev_resume(struct device *dev)
895{
896 return __pcmcia_pm_op(dev, socket_late_resume);
897}
898
899static const struct dev_pm_ops pcmcia_socket_pm_ops = {
900 /* dev_resume may be called with IRQs enabled */
901 SET_SYSTEM_SLEEP_PM_OPS(NULL,
902 pcmcia_socket_dev_resume)
903
904 /* late suspend must be called with IRQs disabled */
905 .suspend_noirq = pcmcia_socket_dev_suspend_noirq,
906 .freeze_noirq = pcmcia_socket_dev_suspend_noirq,
907 .poweroff_noirq = pcmcia_socket_dev_suspend_noirq,
908
909 /* early resume must be called with IRQs disabled */
910 .resume_noirq = pcmcia_socket_dev_resume_noirq,
911 .thaw_noirq = pcmcia_socket_dev_resume_noirq,
912 .restore_noirq = pcmcia_socket_dev_resume_noirq,
913};
914
915#define PCMCIA_SOCKET_CLASS_PM_OPS (&pcmcia_socket_pm_ops)
916
917#else /* CONFIG_PM */
918
919#define PCMCIA_SOCKET_CLASS_PM_OPS NULL
920
921#endif /* CONFIG_PM */
922
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923struct class pcmcia_socket_class = {
924 .name = "pcmcia_socket",
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200925 .dev_uevent = pcmcia_socket_uevent,
926 .dev_release = pcmcia_release_socket,
Dominik Brodowski378a33a2005-06-27 16:28:54 -0700927 .class_release = pcmcia_release_socket_class,
Dominik Brodowskid7646f72010-03-15 21:46:34 +0100928 .pm = PCMCIA_SOCKET_CLASS_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929};
930EXPORT_SYMBOL(pcmcia_socket_class);
931
932
933static int __init init_pcmcia_cs(void)
934{
Dominik Brodowski378a33a2005-06-27 16:28:54 -0700935 init_completion(&pcmcia_unload);
David Brownell4356d732008-04-28 01:03:20 -0700936 return class_register(&pcmcia_socket_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937}
938
939static void __exit exit_pcmcia_cs(void)
940{
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700941 class_unregister(&pcmcia_socket_class);
Dominik Brodowski378a33a2005-06-27 16:28:54 -0700942 wait_for_completion(&pcmcia_unload);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943}
944
945subsys_initcall(init_pcmcia_cs);
946module_exit(exit_pcmcia_cs);
947