blob: 8c8caec3a72cc2b6bc4b6cf1af814b7674976eeb [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/irq.h>
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <pcmcia/ss.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <pcmcia/cistpl.h>
36#include <pcmcia/cisreg.h>
37#include <pcmcia/ds.h>
38#include "cs_internal.h"
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41/* Module parameters */
42
43MODULE_AUTHOR("David Hinds <dahinds@users.sourceforge.net>");
Dominik Brodowski33519dd2005-06-27 16:28:53 -070044MODULE_DESCRIPTION("Linux Kernel Card Services");
Linus Torvalds1da177e2005-04-16 15:20:36 -070045MODULE_LICENSE("GPL");
46
47#define INT_MODULE_PARM(n, v) static int n = v; module_param(n, int, 0444)
48
49INT_MODULE_PARM(setup_delay, 10); /* centiseconds */
50INT_MODULE_PARM(resume_delay, 20); /* centiseconds */
51INT_MODULE_PARM(shutdown_delay, 3); /* centiseconds */
52INT_MODULE_PARM(vcc_settle, 40); /* centiseconds */
53INT_MODULE_PARM(reset_time, 10); /* usecs */
54INT_MODULE_PARM(unreset_delay, 10); /* centiseconds */
55INT_MODULE_PARM(unreset_check, 10); /* centiseconds */
56INT_MODULE_PARM(unreset_limit, 30); /* unreset_check's */
57
58/* Access speed for attribute memory windows */
59INT_MODULE_PARM(cis_speed, 300); /* ns */
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62socket_state_t dead_socket = {
63 .csc_mask = SS_DETECT,
64};
Dominik Brodowski33519dd2005-06-27 16:28:53 -070065EXPORT_SYMBOL(dead_socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67
68/* List of all sockets, protected by a rwsem */
69LIST_HEAD(pcmcia_socket_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070EXPORT_SYMBOL(pcmcia_socket_list);
Dominik Brodowski33519dd2005-06-27 16:28:53 -070071
72DECLARE_RWSEM(pcmcia_socket_list_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073EXPORT_SYMBOL(pcmcia_socket_list_rwsem);
74
75
Dominik Brodowski9fea84f2009-12-07 22:11:45 +010076struct pcmcia_socket *pcmcia_get_socket(struct pcmcia_socket *skt)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077{
Greg Kroah-Hartman87373312006-09-12 17:00:10 +020078 struct device *dev = get_device(&skt->dev);
79 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 return NULL;
Dominik Brodowski593f010b2010-01-02 22:59:15 +010081 return dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082}
83EXPORT_SYMBOL(pcmcia_get_socket);
84
85
86void pcmcia_put_socket(struct pcmcia_socket *skt)
87{
Greg Kroah-Hartman87373312006-09-12 17:00:10 +020088 put_device(&skt->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089}
90EXPORT_SYMBOL(pcmcia_put_socket);
91
92
Greg Kroah-Hartman87373312006-09-12 17:00:10 +020093static void pcmcia_release_socket(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
Greg Kroah-Hartman87373312006-09-12 17:00:10 +020095 struct pcmcia_socket *socket = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97 complete(&socket->socket_released);
98}
99
100static int pccardd(void *__skt);
101
102/**
103 * pcmcia_register_socket - add a new pcmcia socket device
Randy Dunlap78187862007-12-10 15:49:22 -0800104 * @socket: the &socket to register
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 */
106int pcmcia_register_socket(struct pcmcia_socket *socket)
107{
Serge E. Hallyn1da9ab72006-06-14 08:01:26 -0500108 struct task_struct *tsk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 int ret;
110
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200111 if (!socket || !socket->ops || !socket->dev.parent || !socket->resource_ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 return -EINVAL;
113
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200114 dev_dbg(&socket->dev, "pcmcia_register_socket(0x%p)\n", socket->ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 /* try to obtain a socket number [yes, it gets ugly if we
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700117 * register more than 2^sizeof(unsigned int) pcmcia
118 * sockets... but the socket number is deprecated
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 * anyways, so I don't care] */
120 down_write(&pcmcia_socket_list_rwsem);
121 if (list_empty(&pcmcia_socket_list))
122 socket->sock = 0;
123 else {
124 unsigned int found, i = 1;
125 struct pcmcia_socket *tmp;
126 do {
127 found = 1;
128 list_for_each_entry(tmp, &pcmcia_socket_list, socket_list) {
129 if (tmp->sock == i)
130 found = 0;
131 }
132 i++;
133 } while (!found);
134 socket->sock = i - 1;
135 }
136 list_add_tail(&socket->socket_list, &pcmcia_socket_list);
137 up_write(&pcmcia_socket_list_rwsem);
138
Russell King5bc6b682005-07-07 17:59:07 -0700139#ifndef CONFIG_CARDBUS
140 /*
141 * If we do not support Cardbus, ensure that
142 * the Cardbus socket capability is disabled.
143 */
144 socket->features &= ~SS_CAP_CARDBUS;
145#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147 /* set proper values in socket->dev */
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200148 dev_set_drvdata(&socket->dev, socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 socket->dev.class = &pcmcia_socket_class;
Kay Sievers25096982008-11-01 11:46:06 +0100150 dev_set_name(&socket->dev, "pcmcia_socket%u", socket->sock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152 /* base address = 0, map = 0 */
153 socket->cis_mem.flags = 0;
154 socket->cis_mem.speed = cis_speed;
155
156 INIT_LIST_HEAD(&socket->cis_cache);
157
158 init_completion(&socket->socket_released);
159 init_completion(&socket->thread_done);
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100160 mutex_init(&socket->skt_mutex);
Dominik Brodowski6b8e0872010-01-12 21:42:51 +0100161 mutex_init(&socket->ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 spin_lock_init(&socket->thread_lock);
163
Dominik Brodowskib1769452008-11-09 21:47:47 +0100164 if (socket->resource_ops->init) {
Dominik Brodowskicfe5d802010-01-17 19:31:45 +0100165 mutex_lock(&socket->ops_mutex);
Dominik Brodowskib1769452008-11-09 21:47:47 +0100166 ret = socket->resource_ops->init(socket);
Dominik Brodowskicfe5d802010-01-17 19:31:45 +0100167 mutex_unlock(&socket->ops_mutex);
Dominik Brodowskib1769452008-11-09 21:47:47 +0100168 if (ret)
169 goto err;
170 }
171
Serge E. Hallyn1da9ab72006-06-14 08:01:26 -0500172 tsk = kthread_run(pccardd, socket, "pccardd");
173 if (IS_ERR(tsk)) {
174 ret = PTR_ERR(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 goto err;
Serge E. Hallyn1da9ab72006-06-14 08:01:26 -0500176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
178 wait_for_completion(&socket->thread_done);
Serge E. Hallyn1da9ab72006-06-14 08:01:26 -0500179 if (!socket->thread) {
Joe Perchesf2e6cf72014-10-10 09:12:47 -0700180 dev_warn(&socket->dev,
181 "PCMCIA: warning: socket thread did not start\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 return -EIO;
183 }
Serge E. Hallyn1da9ab72006-06-14 08:01:26 -0500184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 pcmcia_parse_events(socket, SS_DETECT);
186
Dominik Brodowski638bba52009-11-07 12:26:17 +0100187 /*
188 * Let's try to get the PCMCIA module for 16-bit PCMCIA support.
189 * If it fails, it doesn't matter -- we still have 32-bit CardBus
190 * support to offer, so this is not a failure mode.
191 */
192 request_module_nowait("pcmcia");
193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 return 0;
195
196 err:
197 down_write(&pcmcia_socket_list_rwsem);
198 list_del(&socket->socket_list);
199 up_write(&pcmcia_socket_list_rwsem);
200 return ret;
201} /* pcmcia_register_socket */
202EXPORT_SYMBOL(pcmcia_register_socket);
203
204
205/**
206 * pcmcia_unregister_socket - remove a pcmcia socket device
Randy Dunlap78187862007-12-10 15:49:22 -0800207 * @socket: the &socket to unregister
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 */
209void pcmcia_unregister_socket(struct pcmcia_socket *socket)
210{
211 if (!socket)
212 return;
213
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200214 dev_dbg(&socket->dev, "pcmcia_unregister_socket(0x%p)\n", socket->ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Christoph Hellwig0b402092007-04-22 13:55:36 +0100216 if (socket->thread)
Serge E. Hallyn1da9ab72006-06-14 08:01:26 -0500217 kthread_stop(socket->thread);
Christoph Hellwig0b402092007-04-22 13:55:36 +0100218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 /* remove from our own list */
220 down_write(&pcmcia_socket_list_rwsem);
221 list_del(&socket->socket_list);
222 up_write(&pcmcia_socket_list_rwsem);
223
224 /* wait for sysfs to drop all references */
Dominik Brodowskicfe5d802010-01-17 19:31:45 +0100225 if (socket->resource_ops->exit) {
226 mutex_lock(&socket->ops_mutex);
Dominik Brodowski385ee872010-01-06 11:23:58 +0100227 socket->resource_ops->exit(socket);
Dominik Brodowskicfe5d802010-01-17 19:31:45 +0100228 mutex_unlock(&socket->ops_mutex);
229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 wait_for_completion(&socket->socket_released);
231} /* pcmcia_unregister_socket */
232EXPORT_SYMBOL(pcmcia_unregister_socket);
233
234
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100235struct pcmcia_socket *pcmcia_get_socket_by_nr(unsigned int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
237 struct pcmcia_socket *s;
238
239 down_read(&pcmcia_socket_list_rwsem);
240 list_for_each_entry(s, &pcmcia_socket_list, socket_list)
241 if (s->sock == nr) {
242 up_read(&pcmcia_socket_list_rwsem);
243 return s;
244 }
245 up_read(&pcmcia_socket_list_rwsem);
246
247 return NULL;
248
249}
250EXPORT_SYMBOL(pcmcia_get_socket_by_nr);
251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252static int socket_reset(struct pcmcia_socket *skt)
253{
254 int status, i;
255
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200256 dev_dbg(&skt->dev, "reset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258 skt->socket.flags |= SS_OUTPUT_ENA | SS_RESET;
259 skt->ops->set_socket(skt, &skt->socket);
260 udelay((long)reset_time);
261
262 skt->socket.flags &= ~SS_RESET;
263 skt->ops->set_socket(skt, &skt->socket);
264
265 msleep(unreset_delay * 10);
266 for (i = 0; i < unreset_limit; i++) {
267 skt->ops->get_status(skt, &status);
268
269 if (!(status & SS_DETECT))
Dominik Brodowski3939c1ef2008-08-03 11:10:56 +0200270 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 if (status & SS_READY)
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200273 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
275 msleep(unreset_check * 10);
276 }
277
Joe Perchesf2e6cf72014-10-10 09:12:47 -0700278 dev_err(&skt->dev, "time out after reset\n");
Dominik Brodowski85671422008-08-03 11:15:45 +0200279 return -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280}
281
Randy Dunlap78187862007-12-10 15:49:22 -0800282/*
Daniel Ritz6e493882005-12-30 15:12:35 +0100283 * socket_setup() and socket_shutdown() are called by the main event handler
284 * when card insertion and removal events are received.
285 * socket_setup() turns on socket power and resets the socket, in two stages.
286 * socket_shutdown() unconfigures a socket and turns off socket power.
287 */
288static void socket_shutdown(struct pcmcia_socket *s)
289{
290 int status;
291
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200292 dev_dbg(&s->dev, "shutdown\n");
Daniel Ritz6e493882005-12-30 15:12:35 +0100293
Dominik Brodowski7b24e792010-07-11 10:26:53 +0200294 if (s->callback)
295 s->callback->remove(s);
Dominik Brodowski9e86749c2010-01-16 00:26:33 +0100296
297 mutex_lock(&s->ops_mutex);
Daniel Ritz6e493882005-12-30 15:12:35 +0100298 s->state &= SOCKET_INUSE | SOCKET_PRESENT;
299 msleep(shutdown_delay * 10);
300 s->state &= SOCKET_INUSE;
301
302 /* Blank out the socket state */
303 s->socket = dead_socket;
304 s->ops->init(s);
305 s->ops->set_socket(s, &s->socket);
Daniel Ritz6e493882005-12-30 15:12:35 +0100306 s->lock_count = 0;
Dominik Brodowski904e3772010-01-02 12:28:04 +0100307 kfree(s->fake_cis);
308 s->fake_cis = NULL;
Dominik Brodowskic3bfc962010-01-18 08:43:39 +0100309 s->functions = 0;
310
311 /* From here on we can be sure that only we (that is, the
312 * pccardd thread) accesses this socket, and all (16-bit)
313 * PCMCIA interactions are gone. Therefore, release
314 * ops_mutex so that we don't get a sysfs-related lockdep
315 * warning.
316 */
317 mutex_unlock(&s->ops_mutex);
318
Daniel Ritz6e493882005-12-30 15:12:35 +0100319#ifdef CONFIG_CARDBUS
320 cb_free(s);
321#endif
Daniel Ritz6e493882005-12-30 15:12:35 +0100322
Daniel Ritzafb2c162007-07-31 00:38:08 -0700323 /* give socket some time to power down */
324 msleep(100);
325
Daniel Ritz6e493882005-12-30 15:12:35 +0100326 s->ops->get_status(s, &status);
327 if (status & SS_POWERON) {
Joe Perchesf2e6cf72014-10-10 09:12:47 -0700328 dev_err(&s->dev,
329 "*** DANGER *** unable to remove socket power\n");
Daniel Ritz6e493882005-12-30 15:12:35 +0100330 }
331
Dominik Brodowski3970dd82010-01-02 23:19:45 +0100332 s->state &= ~SOCKET_INUSE;
Daniel Ritz6e493882005-12-30 15:12:35 +0100333}
334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335static int socket_setup(struct pcmcia_socket *skt, int initial_delay)
336{
337 int status, i;
338
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200339 dev_dbg(&skt->dev, "setup\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 skt->ops->get_status(skt, &status);
342 if (!(status & SS_DETECT))
Dominik Brodowski3939c1ef2008-08-03 11:10:56 +0200343 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
345 msleep(initial_delay * 10);
346
347 for (i = 0; i < 100; i++) {
348 skt->ops->get_status(skt, &status);
349 if (!(status & SS_DETECT))
Dominik Brodowski3939c1ef2008-08-03 11:10:56 +0200350 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352 if (!(status & SS_PENDING))
353 break;
354
355 msleep(100);
356 }
357
358 if (status & SS_PENDING) {
Joe Perchesf2e6cf72014-10-10 09:12:47 -0700359 dev_err(&skt->dev, "voltage interrogation timed out\n");
Dominik Brodowski85671422008-08-03 11:15:45 +0200360 return -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 }
362
363 if (status & SS_CARDBUS) {
Russell King5bc6b682005-07-07 17:59:07 -0700364 if (!(skt->features & SS_CAP_CARDBUS)) {
Joe Perchesf2e6cf72014-10-10 09:12:47 -0700365 dev_err(&skt->dev, "cardbus cards are not supported\n");
Dominik Brodowski610e2372008-08-03 11:58:53 +0200366 return -EINVAL;
Russell King5bc6b682005-07-07 17:59:07 -0700367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 skt->state |= SOCKET_CARDBUS;
Dominik Brodowski88b060d2010-01-02 14:14:23 +0100369 } else
370 skt->state &= ~SOCKET_CARDBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372 /*
373 * Decode the card voltage requirements, and apply power to the card.
374 */
375 if (status & SS_3VCARD)
376 skt->socket.Vcc = skt->socket.Vpp = 33;
377 else if (!(status & SS_XVCARD))
378 skt->socket.Vcc = skt->socket.Vpp = 50;
379 else {
Joe Perchesf2e6cf72014-10-10 09:12:47 -0700380 dev_err(&skt->dev, "unsupported voltage key\n");
Dominik Brodowski610e2372008-08-03 11:58:53 +0200381 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 }
Daniel Ritzfa912bc2005-06-23 00:10:12 -0700383
384 if (skt->power_hook)
385 skt->power_hook(skt, HOOK_POWER_PRE);
386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 skt->socket.flags = 0;
388 skt->ops->set_socket(skt, &skt->socket);
389
390 /*
391 * Wait "vcc_settle" for the supply to stabilise.
392 */
393 msleep(vcc_settle * 10);
394
395 skt->ops->get_status(skt, &status);
396 if (!(status & SS_POWERON)) {
Joe Perchesf2e6cf72014-10-10 09:12:47 -0700397 dev_err(&skt->dev, "unable to apply power\n");
Dominik Brodowski610e2372008-08-03 11:58:53 +0200398 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 }
400
Daniel Ritzfa912bc2005-06-23 00:10:12 -0700401 status = socket_reset(skt);
402
403 if (skt->power_hook)
404 skt->power_hook(skt, HOOK_POWER_POST);
405
406 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407}
408
409/*
410 * Handle card insertion. Setup the socket, reset the card,
411 * and then tell the rest of PCMCIA that a card is present.
412 */
413static int socket_insert(struct pcmcia_socket *skt)
414{
415 int ret;
416
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200417 dev_dbg(&skt->dev, "insert\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Dominik Brodowski9e86749c2010-01-16 00:26:33 +0100419 mutex_lock(&skt->ops_mutex);
Dominik Brodowskif971dbd2010-01-17 18:13:31 +0100420 if (skt->state & SOCKET_INUSE) {
421 mutex_unlock(&skt->ops_mutex);
422 return -EINVAL;
423 }
Dominik Brodowski3970dd82010-01-02 23:19:45 +0100424 skt->state |= SOCKET_INUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426 ret = socket_setup(skt, setup_delay);
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200427 if (ret == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 skt->state |= SOCKET_PRESENT;
Dominik Brodowski807277c2005-11-12 23:34:06 +0100429
Joe Perchesf2e6cf72014-10-10 09:12:47 -0700430 dev_notice(&skt->dev, "pccard: %s card inserted into slot %d\n",
Dominik Brodowski2e55bf62008-08-02 18:08:38 +0200431 (skt->state & SOCKET_CARDBUS) ? "CardBus" : "PCMCIA",
432 skt->sock);
Dominik Brodowski807277c2005-11-12 23:34:06 +0100433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434#ifdef CONFIG_CARDBUS
435 if (skt->state & SOCKET_CARDBUS) {
436 cb_alloc(skt);
437 skt->state |= SOCKET_CARDBUS_CONFIG;
438 }
439#endif
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200440 dev_dbg(&skt->dev, "insert done\n");
Dominik Brodowski9e86749c2010-01-16 00:26:33 +0100441 mutex_unlock(&skt->ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Dominik Brodowski7b24e792010-07-11 10:26:53 +0200443 if (!(skt->state & SOCKET_CARDBUS) && (skt->callback))
444 skt->callback->add(skt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 } else {
Dominik Brodowski9e86749c2010-01-16 00:26:33 +0100446 mutex_unlock(&skt->ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 socket_shutdown(skt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 }
449
450 return ret;
451}
452
453static int socket_suspend(struct pcmcia_socket *skt)
454{
Rafael J. Wysockidbdd0f52018-02-21 13:24:16 +0100455 if ((skt->state & SOCKET_SUSPEND) && !(skt->state & SOCKET_IN_RESUME))
Dominik Brodowskif9580952008-08-03 11:40:19 +0200456 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
Dominik Brodowski9e86749c2010-01-16 00:26:33 +0100458 mutex_lock(&skt->ops_mutex);
Rafael J. Wysockidbdd0f52018-02-21 13:24:16 +0100459 /* store state on first suspend, but not after spurious wakeups */
460 if (!(skt->state & SOCKET_IN_RESUME))
461 skt->suspended_state = skt->state;
Dominik Brodowski88b060d2010-01-02 14:14:23 +0100462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 skt->socket = dead_socket;
464 skt->ops->set_socket(skt, &skt->socket);
465 if (skt->ops->suspend)
466 skt->ops->suspend(skt);
467 skt->state |= SOCKET_SUSPEND;
Rafael J. Wysockidbdd0f52018-02-21 13:24:16 +0100468 skt->state &= ~SOCKET_IN_RESUME;
Dominik Brodowski9e86749c2010-01-16 00:26:33 +0100469 mutex_unlock(&skt->ops_mutex);
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200470 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471}
472
Rafael J. Wysocki9905d1b2009-11-03 10:54:58 +0100473static int socket_early_resume(struct pcmcia_socket *skt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
Dominik Brodowski9e86749c2010-01-16 00:26:33 +0100475 mutex_lock(&skt->ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 skt->socket = dead_socket;
477 skt->ops->init(skt);
478 skt->ops->set_socket(skt, &skt->socket);
Rafael J. Wysocki9905d1b2009-11-03 10:54:58 +0100479 if (skt->state & SOCKET_PRESENT)
480 skt->resume_status = socket_setup(skt, resume_delay);
Rafael J. Wysockidbdd0f52018-02-21 13:24:16 +0100481 skt->state |= SOCKET_IN_RESUME;
Dominik Brodowski9e86749c2010-01-16 00:26:33 +0100482 mutex_unlock(&skt->ops_mutex);
Rafael J. Wysocki9905d1b2009-11-03 10:54:58 +0100483 return 0;
484}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Rafael J. Wysocki9905d1b2009-11-03 10:54:58 +0100486static int socket_late_resume(struct pcmcia_socket *skt)
487{
Christian Lamparter42b5dd52013-02-21 16:42:06 -0800488 int ret = 0;
Dominik Brodowskid7646f72010-03-15 21:46:34 +0100489
Dominik Brodowski9e86749c2010-01-16 00:26:33 +0100490 mutex_lock(&skt->ops_mutex);
Rafael J. Wysockidbdd0f52018-02-21 13:24:16 +0100491 skt->state &= ~(SOCKET_SUSPEND | SOCKET_IN_RESUME);
Dominik Brodowski9e86749c2010-01-16 00:26:33 +0100492 mutex_unlock(&skt->ops_mutex);
Dominik Brodowski88b060d2010-01-02 14:14:23 +0100493
Dominik Brodowskid7646f72010-03-15 21:46:34 +0100494 if (!(skt->state & SOCKET_PRESENT)) {
495 ret = socket_insert(skt);
496 if (ret == -ENODEV)
497 ret = 0;
498 return ret;
499 }
Dominik Brodowski88b060d2010-01-02 14:14:23 +0100500
501 if (skt->resume_status) {
502 socket_shutdown(skt);
503 return 0;
504 }
505
506 if (skt->suspended_state != skt->state) {
507 dev_dbg(&skt->dev,
508 "suspend state 0x%x != resume state 0x%x\n",
509 skt->suspended_state, skt->state);
510
511 socket_shutdown(skt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 return socket_insert(skt);
513 }
514
Christian Lamparter42b5dd52013-02-21 16:42:06 -0800515 if (!(skt->state & SOCKET_CARDBUS) && (skt->callback))
516 ret = skt->callback->early_resume(skt);
517 return ret;
518}
519
520/*
521 * Finalize the resume. In case of a cardbus socket, we have
522 * to rebind the devices as we can't be certain that it has been
523 * replaced, or not.
524 */
525static int socket_complete_resume(struct pcmcia_socket *skt)
526{
527 int ret = 0;
Dominik Brodowski88b060d2010-01-02 14:14:23 +0100528#ifdef CONFIG_CARDBUS
529 if (skt->state & SOCKET_CARDBUS) {
530 /* We can't be sure the CardBus card is the same
531 * as the one previously inserted. Therefore, remove
532 * and re-add... */
533 cb_free(skt);
Christian Lamparter42b5dd52013-02-21 16:42:06 -0800534 ret = cb_alloc(skt);
535 if (ret)
536 cb_free(skt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 }
Dominik Brodowski88b060d2010-01-02 14:14:23 +0100538#endif
Christian Lamparter42b5dd52013-02-21 16:42:06 -0800539 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540}
541
Rafael J. Wysocki9905d1b2009-11-03 10:54:58 +0100542/*
543 * Resume a socket. If a card is present, verify its CIS against
544 * our cached copy. If they are different, the card has been
545 * replaced, and we need to tell the drivers.
546 */
547static int socket_resume(struct pcmcia_socket *skt)
548{
Christian Lamparter42b5dd52013-02-21 16:42:06 -0800549 int err;
Rafael J. Wysocki9905d1b2009-11-03 10:54:58 +0100550 if (!(skt->state & SOCKET_SUSPEND))
551 return -EBUSY;
552
553 socket_early_resume(skt);
Christian Lamparter42b5dd52013-02-21 16:42:06 -0800554 err = socket_late_resume(skt);
555 if (!err)
556 err = socket_complete_resume(skt);
557 return err;
Rafael J. Wysocki9905d1b2009-11-03 10:54:58 +0100558}
559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560static void socket_remove(struct pcmcia_socket *skt)
561{
Joe Perchesf2e6cf72014-10-10 09:12:47 -0700562 dev_notice(&skt->dev, "pccard: card ejected from slot %d\n", skt->sock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 socket_shutdown(skt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564}
565
566/*
567 * Process a socket card detect status change.
568 *
569 * If we don't have a card already present, delay the detect event for
570 * about 20ms (to be on the safe side) before reading the socket status.
571 *
572 * Some i82365-based systems send multiple SS_DETECT events during card
573 * insertion, and the "card present" status bit seems to bounce. This
574 * will probably be true with GPIO-based card detection systems after
575 * the product has aged.
576 */
577static void socket_detect_change(struct pcmcia_socket *skt)
578{
579 if (!(skt->state & SOCKET_SUSPEND)) {
580 int status;
581
582 if (!(skt->state & SOCKET_PRESENT))
583 msleep(20);
584
585 skt->ops->get_status(skt, &status);
586 if ((skt->state & SOCKET_PRESENT) &&
587 !(status & SS_DETECT))
588 socket_remove(skt);
589 if (!(skt->state & SOCKET_PRESENT) &&
590 (status & SS_DETECT))
591 socket_insert(skt);
592 }
593}
594
595static int pccardd(void *__skt)
596{
597 struct pcmcia_socket *skt = __skt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 int ret;
599
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 skt->thread = current;
601 skt->socket = dead_socket;
602 skt->ops->init(skt);
603 skt->ops->set_socket(skt, &skt->socket);
604
605 /* register with the device core */
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200606 ret = device_register(&skt->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 if (ret) {
Joe Perchesf2e6cf72014-10-10 09:12:47 -0700608 dev_warn(&skt->dev, "PCMCIA: unable to register socket\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 skt->thread = NULL;
Serge E. Hallyn1da9ab72006-06-14 08:01:26 -0500610 complete(&skt->thread_done);
611 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 }
David Brownell4356d732008-04-28 01:03:20 -0700613 ret = pccard_sysfs_add_socket(&skt->dev);
614 if (ret)
615 dev_warn(&skt->dev, "err %d adding socket attributes\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Daniel Ritzbf4de6f2005-09-09 13:03:23 -0700617 complete(&skt->thread_done);
618
Dominik Brodowskid801c1402010-02-17 11:02:22 +0100619 /* wait for userspace to catch up */
620 msleep(250);
621
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700622 set_freezable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 for (;;) {
624 unsigned long flags;
625 unsigned int events;
Dominik Brodowskif971dbd2010-01-17 18:13:31 +0100626 unsigned int sysfs_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 spin_lock_irqsave(&skt->thread_lock, flags);
629 events = skt->thread_events;
630 skt->thread_events = 0;
Dominik Brodowskif971dbd2010-01-17 18:13:31 +0100631 sysfs_events = skt->sysfs_events;
632 skt->sysfs_events = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 spin_unlock_irqrestore(&skt->thread_lock, flags);
634
Dominik Brodowskif971dbd2010-01-17 18:13:31 +0100635 mutex_lock(&skt->skt_mutex);
Dominik Brodowski216d7cd2010-07-11 10:04:59 +0200636 if (events & SS_DETECT)
637 socket_detect_change(skt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Dominik Brodowskif971dbd2010-01-17 18:13:31 +0100639 if (sysfs_events) {
640 if (sysfs_events & PCMCIA_UEVENT_EJECT)
641 socket_remove(skt);
642 if (sysfs_events & PCMCIA_UEVENT_INSERT)
643 socket_insert(skt);
Dominik Brodowskif971dbd2010-01-17 18:13:31 +0100644 if ((sysfs_events & PCMCIA_UEVENT_SUSPEND) &&
645 !(skt->state & SOCKET_CARDBUS)) {
646 if (skt->callback)
647 ret = skt->callback->suspend(skt);
648 else
649 ret = 0;
Dominik Brodowski43821242010-05-03 15:10:09 +0200650 if (!ret) {
Dominik Brodowskif971dbd2010-01-17 18:13:31 +0100651 socket_suspend(skt);
Dominik Brodowski43821242010-05-03 15:10:09 +0200652 msleep(100);
653 }
654 }
655 if ((sysfs_events & PCMCIA_UEVENT_RESUME) &&
656 !(skt->state & SOCKET_CARDBUS)) {
657 ret = socket_resume(skt);
658 if (!ret && skt->callback)
659 skt->callback->resume(skt);
Dominik Brodowskif971dbd2010-01-17 18:13:31 +0100660 }
Dominik Brodowskiaf461fc2010-01-17 19:30:53 +0100661 if ((sysfs_events & PCMCIA_UEVENT_REQUERY) &&
662 !(skt->state & SOCKET_CARDBUS)) {
663 if (!ret && skt->callback)
664 skt->callback->requery(skt);
665 }
Dominik Brodowskif971dbd2010-01-17 18:13:31 +0100666 }
667 mutex_unlock(&skt->skt_mutex);
668
669 if (events || sysfs_events)
670 continue;
671
Serge E. Hallyn1da9ab72006-06-14 08:01:26 -0500672 if (kthread_should_stop())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 break;
Daniel Ritzbf4de6f2005-09-09 13:03:23 -0700674
Dmitry Eremin-Solenikovd3046ba2015-03-30 14:33:45 +0300675 set_current_state(TASK_INTERRUPTIBLE);
676
Daniel Ritzbf4de6f2005-09-09 13:03:23 -0700677 schedule();
Dmitry Eremin-Solenikovd3046ba2015-03-30 14:33:45 +0300678
679 /* make sure we are running */
680 __set_current_state(TASK_RUNNING);
681
Daniel Ritzbf4de6f2005-09-09 13:03:23 -0700682 try_to_freeze();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 }
Steven Rostedt220ec022005-10-10 11:13:17 -0400684
Dominik Brodowski3970dd82010-01-02 23:19:45 +0100685 /* shut down socket, if a device is still present */
686 if (skt->state & SOCKET_PRESENT) {
687 mutex_lock(&skt->skt_mutex);
688 socket_remove(skt);
689 mutex_unlock(&skt->skt_mutex);
690 }
691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 /* remove from the device core */
David Brownell4356d732008-04-28 01:03:20 -0700693 pccard_sysfs_remove_socket(&skt->dev);
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200694 device_unregister(&skt->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Serge E. Hallyn1da9ab72006-06-14 08:01:26 -0500696 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697}
698
699/*
700 * Yenta (at least) probes interrupts before registering the socket and
701 * starting the handler thread.
702 */
703void pcmcia_parse_events(struct pcmcia_socket *s, u_int events)
704{
Arjan van de Ven4b7a89a2006-06-30 10:31:13 +0200705 unsigned long flags;
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200706 dev_dbg(&s->dev, "parse_events: events %08x\n", events);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 if (s->thread) {
Arjan van de Ven4b7a89a2006-06-30 10:31:13 +0200708 spin_lock_irqsave(&s->thread_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 s->thread_events |= events;
Arjan van de Ven4b7a89a2006-06-30 10:31:13 +0200710 spin_unlock_irqrestore(&s->thread_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Christoph Hellwig0b402092007-04-22 13:55:36 +0100712 wake_up_process(s->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 }
714} /* pcmcia_parse_events */
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700715EXPORT_SYMBOL(pcmcia_parse_events);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Dominik Brodowskif971dbd2010-01-17 18:13:31 +0100717/**
718 * pcmcia_parse_uevents() - tell pccardd to issue manual commands
719 * @s: the PCMCIA socket we wan't to command
720 * @events: events to pass to pccardd
721 *
722 * userspace-issued insert, eject, suspend and resume commands must be
723 * handled by pccardd to avoid any sysfs-related deadlocks. Valid events
724 * are PCMCIA_UEVENT_EJECT (for eject), PCMCIA_UEVENT__INSERT (for insert),
Dominik Brodowskiaf461fc2010-01-17 19:30:53 +0100725 * PCMCIA_UEVENT_RESUME (for resume), PCMCIA_UEVENT_SUSPEND (for suspend)
726 * and PCMCIA_UEVENT_REQUERY (for re-querying the PCMCIA card).
Dominik Brodowskif971dbd2010-01-17 18:13:31 +0100727 */
728void pcmcia_parse_uevents(struct pcmcia_socket *s, u_int events)
729{
730 unsigned long flags;
731 dev_dbg(&s->dev, "parse_uevents: events %08x\n", events);
732 if (s->thread) {
733 spin_lock_irqsave(&s->thread_lock, flags);
734 s->sysfs_events |= events;
735 spin_unlock_irqrestore(&s->thread_lock, flags);
736
737 wake_up_process(s->thread);
738 }
739}
740EXPORT_SYMBOL(pcmcia_parse_uevents);
741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
743/* register pcmcia_callback */
744int pccard_register_pcmcia(struct pcmcia_socket *s, struct pcmcia_callback *c)
745{
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100746 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100748 /* s->skt_mutex also protects s->callback */
749 mutex_lock(&s->skt_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
751 if (c) {
752 /* registration */
753 if (s->callback) {
754 ret = -EBUSY;
755 goto err;
756 }
757
758 s->callback = c;
759
760 if ((s->state & (SOCKET_PRESENT|SOCKET_CARDBUS)) == SOCKET_PRESENT)
Dominik Brodowski7b24e792010-07-11 10:26:53 +0200761 s->callback->add(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 } else
763 s->callback = NULL;
764 err:
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100765 mutex_unlock(&s->skt_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
767 return ret;
768}
769EXPORT_SYMBOL(pccard_register_pcmcia);
770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700772/* I'm not sure which "reset" function this is supposed to use,
773 * but for now, it uses the low-level interface's reset, not the
774 * CIS register.
775 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Dominik Brodowski994917f2008-08-31 15:20:26 +0200777int pcmcia_reset_card(struct pcmcia_socket *skt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
779 int ret;
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700780
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200781 dev_dbg(&skt->dev, "resetting socket\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100783 mutex_lock(&skt->skt_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 do {
785 if (!(skt->state & SOCKET_PRESENT)) {
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200786 dev_dbg(&skt->dev, "can't reset, not present\n");
Dominik Brodowski3939c1ef2008-08-03 11:10:56 +0200787 ret = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 break;
789 }
790 if (skt->state & SOCKET_SUSPEND) {
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200791 dev_dbg(&skt->dev, "can't reset, suspended\n");
Dominik Brodowskif9580952008-08-03 11:40:19 +0200792 ret = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 break;
794 }
795 if (skt->state & SOCKET_CARDBUS) {
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200796 dev_dbg(&skt->dev, "can't reset, is cardbus\n");
Dominik Brodowskide6405e2008-08-03 10:47:59 +0200797 ret = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 break;
799 }
800
Dominik Brodowski216d7cd2010-07-11 10:04:59 +0200801 if (skt->callback)
802 skt->callback->suspend(skt);
803 mutex_lock(&skt->ops_mutex);
804 ret = socket_reset(skt);
805 mutex_unlock(&skt->ops_mutex);
806 if ((ret == 0) && (skt->callback))
807 skt->callback->resume(skt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200809 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 } while (0);
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100811 mutex_unlock(&skt->skt_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
813 return ret;
814} /* reset_card */
Dominik Brodowski994917f2008-08-31 15:20:26 +0200815EXPORT_SYMBOL(pcmcia_reset_card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Kay Sievers7eff2e72007-08-14 15:15:12 +0200818static int pcmcia_socket_uevent(struct device *dev,
819 struct kobj_uevent_env *env)
Dominik Brodowski3ee13932005-06-27 16:28:05 -0700820{
821 struct pcmcia_socket *s = container_of(dev, struct pcmcia_socket, dev);
Dominik Brodowski3ee13932005-06-27 16:28:05 -0700822
Kay Sievers7eff2e72007-08-14 15:15:12 +0200823 if (add_uevent_var(env, "SOCKET_NO=%u", s->sock))
Dominik Brodowski3ee13932005-06-27 16:28:05 -0700824 return -ENOMEM;
825
Dominik Brodowski3ee13932005-06-27 16:28:05 -0700826 return 0;
827}
828
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
Dominik Brodowski378a33a2005-06-27 16:28:54 -0700830static struct completion pcmcia_unload;
831
832static void pcmcia_release_socket_class(struct class *data)
833{
834 complete(&pcmcia_unload);
835}
836
837
Dominik Brodowskid7646f72010-03-15 21:46:34 +0100838#ifdef CONFIG_PM
839
840static int __pcmcia_pm_op(struct device *dev,
841 int (*callback) (struct pcmcia_socket *skt))
842{
843 struct pcmcia_socket *s = container_of(dev, struct pcmcia_socket, dev);
844 int ret;
845
846 mutex_lock(&s->skt_mutex);
847 ret = callback(s);
848 mutex_unlock(&s->skt_mutex);
849
850 return ret;
851}
852
853static int pcmcia_socket_dev_suspend_noirq(struct device *dev)
854{
855 return __pcmcia_pm_op(dev, socket_suspend);
856}
857
858static int pcmcia_socket_dev_resume_noirq(struct device *dev)
859{
860 return __pcmcia_pm_op(dev, socket_early_resume);
861}
862
Maciej Żenczykowski3c7d2b72010-10-13 21:47:14 -0700863static int __used pcmcia_socket_dev_resume(struct device *dev)
Dominik Brodowskid7646f72010-03-15 21:46:34 +0100864{
865 return __pcmcia_pm_op(dev, socket_late_resume);
866}
867
Christian Lamparter42b5dd52013-02-21 16:42:06 -0800868static void __used pcmcia_socket_dev_complete(struct device *dev)
869{
870 WARN(__pcmcia_pm_op(dev, socket_complete_resume),
871 "failed to complete resume");
872}
873
Dominik Brodowskid7646f72010-03-15 21:46:34 +0100874static const struct dev_pm_ops pcmcia_socket_pm_ops = {
875 /* dev_resume may be called with IRQs enabled */
876 SET_SYSTEM_SLEEP_PM_OPS(NULL,
877 pcmcia_socket_dev_resume)
878
879 /* late suspend must be called with IRQs disabled */
880 .suspend_noirq = pcmcia_socket_dev_suspend_noirq,
881 .freeze_noirq = pcmcia_socket_dev_suspend_noirq,
882 .poweroff_noirq = pcmcia_socket_dev_suspend_noirq,
883
884 /* early resume must be called with IRQs disabled */
885 .resume_noirq = pcmcia_socket_dev_resume_noirq,
886 .thaw_noirq = pcmcia_socket_dev_resume_noirq,
887 .restore_noirq = pcmcia_socket_dev_resume_noirq,
Christian Lamparter42b5dd52013-02-21 16:42:06 -0800888 .complete = pcmcia_socket_dev_complete,
Dominik Brodowskid7646f72010-03-15 21:46:34 +0100889};
890
891#define PCMCIA_SOCKET_CLASS_PM_OPS (&pcmcia_socket_pm_ops)
892
893#else /* CONFIG_PM */
894
895#define PCMCIA_SOCKET_CLASS_PM_OPS NULL
896
897#endif /* CONFIG_PM */
898
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899struct class pcmcia_socket_class = {
900 .name = "pcmcia_socket",
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200901 .dev_uevent = pcmcia_socket_uevent,
902 .dev_release = pcmcia_release_socket,
Dominik Brodowski378a33a2005-06-27 16:28:54 -0700903 .class_release = pcmcia_release_socket_class,
Dominik Brodowskid7646f72010-03-15 21:46:34 +0100904 .pm = PCMCIA_SOCKET_CLASS_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905};
906EXPORT_SYMBOL(pcmcia_socket_class);
907
908
909static int __init init_pcmcia_cs(void)
910{
Dominik Brodowski378a33a2005-06-27 16:28:54 -0700911 init_completion(&pcmcia_unload);
David Brownell4356d732008-04-28 01:03:20 -0700912 return class_register(&pcmcia_socket_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913}
914
915static void __exit exit_pcmcia_cs(void)
916{
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700917 class_unregister(&pcmcia_socket_class);
Dominik Brodowski378a33a2005-06-27 16:28:54 -0700918 wait_for_completion(&pcmcia_unload);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919}
920
921subsys_initcall(init_pcmcia_cs);
922module_exit(exit_pcmcia_cs);
923