blob: f70197154a36256ce59dcaf31d22faa09cb5fcaf [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * cs.c -- Kernel Card Services - core services
4 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * The initial developer of the original code is David A. Hinds
6 * <dahinds@users.sourceforge.net>. Portions created by David A. Hinds
7 * are Copyright (C) 1999 David A. Hinds. All Rights Reserved.
8 *
9 * (C) 1999 David A. Hinds
10 */
11
12#include <linux/module.h>
13#include <linux/moduleparam.h>
14#include <linux/init.h>
15#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/string.h>
17#include <linux/major.h>
18#include <linux/errno.h>
19#include <linux/slab.h>
20#include <linux/mm.h>
21#include <linux/interrupt.h>
22#include <linux/timer.h>
23#include <linux/ioport.h>
24#include <linux/delay.h>
25#include <linux/pm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/device.h>
Serge E. Hallyn1da9ab72006-06-14 08:01:26 -050027#include <linux/kthread.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080028#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <asm/irq.h>
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <pcmcia/ss.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <pcmcia/cistpl.h>
33#include <pcmcia/cisreg.h>
34#include <pcmcia/ds.h>
35#include "cs_internal.h"
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38/* Module parameters */
39
40MODULE_AUTHOR("David Hinds <dahinds@users.sourceforge.net>");
Dominik Brodowski33519dd2005-06-27 16:28:53 -070041MODULE_DESCRIPTION("Linux Kernel Card Services");
Linus Torvalds1da177e2005-04-16 15:20:36 -070042MODULE_LICENSE("GPL");
43
44#define INT_MODULE_PARM(n, v) static int n = v; module_param(n, int, 0444)
45
46INT_MODULE_PARM(setup_delay, 10); /* centiseconds */
47INT_MODULE_PARM(resume_delay, 20); /* centiseconds */
48INT_MODULE_PARM(shutdown_delay, 3); /* centiseconds */
49INT_MODULE_PARM(vcc_settle, 40); /* centiseconds */
50INT_MODULE_PARM(reset_time, 10); /* usecs */
51INT_MODULE_PARM(unreset_delay, 10); /* centiseconds */
52INT_MODULE_PARM(unreset_check, 10); /* centiseconds */
53INT_MODULE_PARM(unreset_limit, 30); /* unreset_check's */
54
55/* Access speed for attribute memory windows */
56INT_MODULE_PARM(cis_speed, 300); /* ns */
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59socket_state_t dead_socket = {
60 .csc_mask = SS_DETECT,
61};
Dominik Brodowski33519dd2005-06-27 16:28:53 -070062EXPORT_SYMBOL(dead_socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64
65/* List of all sockets, protected by a rwsem */
66LIST_HEAD(pcmcia_socket_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067EXPORT_SYMBOL(pcmcia_socket_list);
Dominik Brodowski33519dd2005-06-27 16:28:53 -070068
69DECLARE_RWSEM(pcmcia_socket_list_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070EXPORT_SYMBOL(pcmcia_socket_list_rwsem);
71
72
Dominik Brodowski9fea84f2009-12-07 22:11:45 +010073struct pcmcia_socket *pcmcia_get_socket(struct pcmcia_socket *skt)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
Greg Kroah-Hartman87373312006-09-12 17:00:10 +020075 struct device *dev = get_device(&skt->dev);
76 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 return NULL;
Dominik Brodowski593f010b2010-01-02 22:59:15 +010078 return dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079}
80EXPORT_SYMBOL(pcmcia_get_socket);
81
82
83void pcmcia_put_socket(struct pcmcia_socket *skt)
84{
Greg Kroah-Hartman87373312006-09-12 17:00:10 +020085 put_device(&skt->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086}
87EXPORT_SYMBOL(pcmcia_put_socket);
88
89
Greg Kroah-Hartman87373312006-09-12 17:00:10 +020090static void pcmcia_release_socket(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
Greg Kroah-Hartman87373312006-09-12 17:00:10 +020092 struct pcmcia_socket *socket = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94 complete(&socket->socket_released);
95}
96
97static int pccardd(void *__skt);
98
99/**
100 * pcmcia_register_socket - add a new pcmcia socket device
Randy Dunlap78187862007-12-10 15:49:22 -0800101 * @socket: the &socket to register
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 */
103int pcmcia_register_socket(struct pcmcia_socket *socket)
104{
Serge E. Hallyn1da9ab72006-06-14 08:01:26 -0500105 struct task_struct *tsk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 int ret;
107
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200108 if (!socket || !socket->ops || !socket->dev.parent || !socket->resource_ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 return -EINVAL;
110
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200111 dev_dbg(&socket->dev, "pcmcia_register_socket(0x%p)\n", socket->ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 /* try to obtain a socket number [yes, it gets ugly if we
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700114 * register more than 2^sizeof(unsigned int) pcmcia
115 * sockets... but the socket number is deprecated
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 * anyways, so I don't care] */
117 down_write(&pcmcia_socket_list_rwsem);
118 if (list_empty(&pcmcia_socket_list))
119 socket->sock = 0;
120 else {
121 unsigned int found, i = 1;
122 struct pcmcia_socket *tmp;
123 do {
124 found = 1;
125 list_for_each_entry(tmp, &pcmcia_socket_list, socket_list) {
126 if (tmp->sock == i)
127 found = 0;
128 }
129 i++;
130 } while (!found);
131 socket->sock = i - 1;
132 }
133 list_add_tail(&socket->socket_list, &pcmcia_socket_list);
134 up_write(&pcmcia_socket_list_rwsem);
135
Russell King5bc6b682005-07-07 17:59:07 -0700136#ifndef CONFIG_CARDBUS
137 /*
138 * If we do not support Cardbus, ensure that
139 * the Cardbus socket capability is disabled.
140 */
141 socket->features &= ~SS_CAP_CARDBUS;
142#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144 /* set proper values in socket->dev */
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200145 dev_set_drvdata(&socket->dev, socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 socket->dev.class = &pcmcia_socket_class;
Kay Sievers25096982008-11-01 11:46:06 +0100147 dev_set_name(&socket->dev, "pcmcia_socket%u", socket->sock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149 /* base address = 0, map = 0 */
150 socket->cis_mem.flags = 0;
151 socket->cis_mem.speed = cis_speed;
152
153 INIT_LIST_HEAD(&socket->cis_cache);
154
155 init_completion(&socket->socket_released);
156 init_completion(&socket->thread_done);
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100157 mutex_init(&socket->skt_mutex);
Dominik Brodowski6b8e0872010-01-12 21:42:51 +0100158 mutex_init(&socket->ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 spin_lock_init(&socket->thread_lock);
160
Dominik Brodowskib1769452008-11-09 21:47:47 +0100161 if (socket->resource_ops->init) {
Dominik Brodowskicfe5d802010-01-17 19:31:45 +0100162 mutex_lock(&socket->ops_mutex);
Dominik Brodowskib1769452008-11-09 21:47:47 +0100163 ret = socket->resource_ops->init(socket);
Dominik Brodowskicfe5d802010-01-17 19:31:45 +0100164 mutex_unlock(&socket->ops_mutex);
Dominik Brodowskib1769452008-11-09 21:47:47 +0100165 if (ret)
166 goto err;
167 }
168
Serge E. Hallyn1da9ab72006-06-14 08:01:26 -0500169 tsk = kthread_run(pccardd, socket, "pccardd");
170 if (IS_ERR(tsk)) {
171 ret = PTR_ERR(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 goto err;
Serge E. Hallyn1da9ab72006-06-14 08:01:26 -0500173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175 wait_for_completion(&socket->thread_done);
Serge E. Hallyn1da9ab72006-06-14 08:01:26 -0500176 if (!socket->thread) {
Joe Perchesf2e6cf72014-10-10 09:12:47 -0700177 dev_warn(&socket->dev,
178 "PCMCIA: warning: socket thread did not start\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 return -EIO;
180 }
Serge E. Hallyn1da9ab72006-06-14 08:01:26 -0500181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 pcmcia_parse_events(socket, SS_DETECT);
183
Dominik Brodowski638bba52009-11-07 12:26:17 +0100184 /*
185 * Let's try to get the PCMCIA module for 16-bit PCMCIA support.
186 * If it fails, it doesn't matter -- we still have 32-bit CardBus
187 * support to offer, so this is not a failure mode.
188 */
189 request_module_nowait("pcmcia");
190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 return 0;
192
193 err:
194 down_write(&pcmcia_socket_list_rwsem);
195 list_del(&socket->socket_list);
196 up_write(&pcmcia_socket_list_rwsem);
197 return ret;
198} /* pcmcia_register_socket */
199EXPORT_SYMBOL(pcmcia_register_socket);
200
201
202/**
203 * pcmcia_unregister_socket - remove a pcmcia socket device
Randy Dunlap78187862007-12-10 15:49:22 -0800204 * @socket: the &socket to unregister
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 */
206void pcmcia_unregister_socket(struct pcmcia_socket *socket)
207{
208 if (!socket)
209 return;
210
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200211 dev_dbg(&socket->dev, "pcmcia_unregister_socket(0x%p)\n", socket->ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Christoph Hellwig0b402092007-04-22 13:55:36 +0100213 if (socket->thread)
Serge E. Hallyn1da9ab72006-06-14 08:01:26 -0500214 kthread_stop(socket->thread);
Christoph Hellwig0b402092007-04-22 13:55:36 +0100215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 /* remove from our own list */
217 down_write(&pcmcia_socket_list_rwsem);
218 list_del(&socket->socket_list);
219 up_write(&pcmcia_socket_list_rwsem);
220
221 /* wait for sysfs to drop all references */
Dominik Brodowskicfe5d802010-01-17 19:31:45 +0100222 if (socket->resource_ops->exit) {
223 mutex_lock(&socket->ops_mutex);
Dominik Brodowski385ee872010-01-06 11:23:58 +0100224 socket->resource_ops->exit(socket);
Dominik Brodowskicfe5d802010-01-17 19:31:45 +0100225 mutex_unlock(&socket->ops_mutex);
226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 wait_for_completion(&socket->socket_released);
228} /* pcmcia_unregister_socket */
229EXPORT_SYMBOL(pcmcia_unregister_socket);
230
231
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100232struct pcmcia_socket *pcmcia_get_socket_by_nr(unsigned int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
234 struct pcmcia_socket *s;
235
236 down_read(&pcmcia_socket_list_rwsem);
237 list_for_each_entry(s, &pcmcia_socket_list, socket_list)
238 if (s->sock == nr) {
239 up_read(&pcmcia_socket_list_rwsem);
240 return s;
241 }
242 up_read(&pcmcia_socket_list_rwsem);
243
244 return NULL;
245
246}
247EXPORT_SYMBOL(pcmcia_get_socket_by_nr);
248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249static int socket_reset(struct pcmcia_socket *skt)
250{
251 int status, i;
252
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200253 dev_dbg(&skt->dev, "reset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255 skt->socket.flags |= SS_OUTPUT_ENA | SS_RESET;
256 skt->ops->set_socket(skt, &skt->socket);
257 udelay((long)reset_time);
258
259 skt->socket.flags &= ~SS_RESET;
260 skt->ops->set_socket(skt, &skt->socket);
261
262 msleep(unreset_delay * 10);
263 for (i = 0; i < unreset_limit; i++) {
264 skt->ops->get_status(skt, &status);
265
266 if (!(status & SS_DETECT))
Dominik Brodowski3939c1ef2008-08-03 11:10:56 +0200267 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269 if (status & SS_READY)
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200270 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 msleep(unreset_check * 10);
273 }
274
Joe Perchesf2e6cf72014-10-10 09:12:47 -0700275 dev_err(&skt->dev, "time out after reset\n");
Dominik Brodowski85671422008-08-03 11:15:45 +0200276 return -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277}
278
Randy Dunlap78187862007-12-10 15:49:22 -0800279/*
Daniel Ritz6e493882005-12-30 15:12:35 +0100280 * socket_setup() and socket_shutdown() are called by the main event handler
281 * when card insertion and removal events are received.
282 * socket_setup() turns on socket power and resets the socket, in two stages.
283 * socket_shutdown() unconfigures a socket and turns off socket power.
284 */
285static void socket_shutdown(struct pcmcia_socket *s)
286{
287 int status;
288
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200289 dev_dbg(&s->dev, "shutdown\n");
Daniel Ritz6e493882005-12-30 15:12:35 +0100290
Dominik Brodowski7b24e792010-07-11 10:26:53 +0200291 if (s->callback)
292 s->callback->remove(s);
Dominik Brodowski9e86749c2010-01-16 00:26:33 +0100293
294 mutex_lock(&s->ops_mutex);
Daniel Ritz6e493882005-12-30 15:12:35 +0100295 s->state &= SOCKET_INUSE | SOCKET_PRESENT;
296 msleep(shutdown_delay * 10);
297 s->state &= SOCKET_INUSE;
298
299 /* Blank out the socket state */
300 s->socket = dead_socket;
301 s->ops->init(s);
302 s->ops->set_socket(s, &s->socket);
Daniel Ritz6e493882005-12-30 15:12:35 +0100303 s->lock_count = 0;
Dominik Brodowski904e3772010-01-02 12:28:04 +0100304 kfree(s->fake_cis);
305 s->fake_cis = NULL;
Dominik Brodowskic3bfc962010-01-18 08:43:39 +0100306 s->functions = 0;
307
308 /* From here on we can be sure that only we (that is, the
309 * pccardd thread) accesses this socket, and all (16-bit)
310 * PCMCIA interactions are gone. Therefore, release
311 * ops_mutex so that we don't get a sysfs-related lockdep
312 * warning.
313 */
314 mutex_unlock(&s->ops_mutex);
315
Daniel Ritz6e493882005-12-30 15:12:35 +0100316#ifdef CONFIG_CARDBUS
317 cb_free(s);
318#endif
Daniel Ritz6e493882005-12-30 15:12:35 +0100319
Daniel Ritzafb2c162007-07-31 00:38:08 -0700320 /* give socket some time to power down */
321 msleep(100);
322
Daniel Ritz6e493882005-12-30 15:12:35 +0100323 s->ops->get_status(s, &status);
324 if (status & SS_POWERON) {
Joe Perchesf2e6cf72014-10-10 09:12:47 -0700325 dev_err(&s->dev,
326 "*** DANGER *** unable to remove socket power\n");
Daniel Ritz6e493882005-12-30 15:12:35 +0100327 }
328
Dominik Brodowski3970dd82010-01-02 23:19:45 +0100329 s->state &= ~SOCKET_INUSE;
Daniel Ritz6e493882005-12-30 15:12:35 +0100330}
331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332static int socket_setup(struct pcmcia_socket *skt, int initial_delay)
333{
334 int status, i;
335
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200336 dev_dbg(&skt->dev, "setup\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338 skt->ops->get_status(skt, &status);
339 if (!(status & SS_DETECT))
Dominik Brodowski3939c1ef2008-08-03 11:10:56 +0200340 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342 msleep(initial_delay * 10);
343
344 for (i = 0; i < 100; i++) {
345 skt->ops->get_status(skt, &status);
346 if (!(status & SS_DETECT))
Dominik Brodowski3939c1ef2008-08-03 11:10:56 +0200347 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
349 if (!(status & SS_PENDING))
350 break;
351
352 msleep(100);
353 }
354
355 if (status & SS_PENDING) {
Joe Perchesf2e6cf72014-10-10 09:12:47 -0700356 dev_err(&skt->dev, "voltage interrogation timed out\n");
Dominik Brodowski85671422008-08-03 11:15:45 +0200357 return -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 }
359
360 if (status & SS_CARDBUS) {
Russell King5bc6b682005-07-07 17:59:07 -0700361 if (!(skt->features & SS_CAP_CARDBUS)) {
Joe Perchesf2e6cf72014-10-10 09:12:47 -0700362 dev_err(&skt->dev, "cardbus cards are not supported\n");
Dominik Brodowski610e2372008-08-03 11:58:53 +0200363 return -EINVAL;
Russell King5bc6b682005-07-07 17:59:07 -0700364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 skt->state |= SOCKET_CARDBUS;
Dominik Brodowski88b060d2010-01-02 14:14:23 +0100366 } else
367 skt->state &= ~SOCKET_CARDBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369 /*
370 * Decode the card voltage requirements, and apply power to the card.
371 */
372 if (status & SS_3VCARD)
373 skt->socket.Vcc = skt->socket.Vpp = 33;
374 else if (!(status & SS_XVCARD))
375 skt->socket.Vcc = skt->socket.Vpp = 50;
376 else {
Joe Perchesf2e6cf72014-10-10 09:12:47 -0700377 dev_err(&skt->dev, "unsupported voltage key\n");
Dominik Brodowski610e2372008-08-03 11:58:53 +0200378 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 }
Daniel Ritzfa912bc2005-06-23 00:10:12 -0700380
381 if (skt->power_hook)
382 skt->power_hook(skt, HOOK_POWER_PRE);
383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 skt->socket.flags = 0;
385 skt->ops->set_socket(skt, &skt->socket);
386
387 /*
388 * Wait "vcc_settle" for the supply to stabilise.
389 */
390 msleep(vcc_settle * 10);
391
392 skt->ops->get_status(skt, &status);
393 if (!(status & SS_POWERON)) {
Joe Perchesf2e6cf72014-10-10 09:12:47 -0700394 dev_err(&skt->dev, "unable to apply power\n");
Dominik Brodowski610e2372008-08-03 11:58:53 +0200395 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 }
397
Daniel Ritzfa912bc2005-06-23 00:10:12 -0700398 status = socket_reset(skt);
399
400 if (skt->power_hook)
401 skt->power_hook(skt, HOOK_POWER_POST);
402
403 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404}
405
406/*
407 * Handle card insertion. Setup the socket, reset the card,
408 * and then tell the rest of PCMCIA that a card is present.
409 */
410static int socket_insert(struct pcmcia_socket *skt)
411{
412 int ret;
413
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200414 dev_dbg(&skt->dev, "insert\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Dominik Brodowski9e86749c2010-01-16 00:26:33 +0100416 mutex_lock(&skt->ops_mutex);
Dominik Brodowskif971dbd2010-01-17 18:13:31 +0100417 if (skt->state & SOCKET_INUSE) {
418 mutex_unlock(&skt->ops_mutex);
419 return -EINVAL;
420 }
Dominik Brodowski3970dd82010-01-02 23:19:45 +0100421 skt->state |= SOCKET_INUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
423 ret = socket_setup(skt, setup_delay);
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200424 if (ret == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 skt->state |= SOCKET_PRESENT;
Dominik Brodowski807277c2005-11-12 23:34:06 +0100426
Joe Perchesf2e6cf72014-10-10 09:12:47 -0700427 dev_notice(&skt->dev, "pccard: %s card inserted into slot %d\n",
Dominik Brodowski2e55bf62008-08-02 18:08:38 +0200428 (skt->state & SOCKET_CARDBUS) ? "CardBus" : "PCMCIA",
429 skt->sock);
Dominik Brodowski807277c2005-11-12 23:34:06 +0100430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431#ifdef CONFIG_CARDBUS
432 if (skt->state & SOCKET_CARDBUS) {
433 cb_alloc(skt);
434 skt->state |= SOCKET_CARDBUS_CONFIG;
435 }
436#endif
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200437 dev_dbg(&skt->dev, "insert done\n");
Dominik Brodowski9e86749c2010-01-16 00:26:33 +0100438 mutex_unlock(&skt->ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Dominik Brodowski7b24e792010-07-11 10:26:53 +0200440 if (!(skt->state & SOCKET_CARDBUS) && (skt->callback))
441 skt->callback->add(skt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 } else {
Dominik Brodowski9e86749c2010-01-16 00:26:33 +0100443 mutex_unlock(&skt->ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 socket_shutdown(skt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 }
446
447 return ret;
448}
449
450static int socket_suspend(struct pcmcia_socket *skt)
451{
Rafael J. Wysockidbdd0f52018-02-21 13:24:16 +0100452 if ((skt->state & SOCKET_SUSPEND) && !(skt->state & SOCKET_IN_RESUME))
Dominik Brodowskif9580952008-08-03 11:40:19 +0200453 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Dominik Brodowski9e86749c2010-01-16 00:26:33 +0100455 mutex_lock(&skt->ops_mutex);
Rafael J. Wysockidbdd0f52018-02-21 13:24:16 +0100456 /* store state on first suspend, but not after spurious wakeups */
457 if (!(skt->state & SOCKET_IN_RESUME))
458 skt->suspended_state = skt->state;
Dominik Brodowski88b060d2010-01-02 14:14:23 +0100459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 skt->socket = dead_socket;
461 skt->ops->set_socket(skt, &skt->socket);
462 if (skt->ops->suspend)
463 skt->ops->suspend(skt);
464 skt->state |= SOCKET_SUSPEND;
Rafael J. Wysockidbdd0f52018-02-21 13:24:16 +0100465 skt->state &= ~SOCKET_IN_RESUME;
Dominik Brodowski9e86749c2010-01-16 00:26:33 +0100466 mutex_unlock(&skt->ops_mutex);
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200467 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468}
469
Rafael J. Wysocki9905d1b2009-11-03 10:54:58 +0100470static int socket_early_resume(struct pcmcia_socket *skt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
Dominik Brodowski9e86749c2010-01-16 00:26:33 +0100472 mutex_lock(&skt->ops_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 skt->socket = dead_socket;
474 skt->ops->init(skt);
475 skt->ops->set_socket(skt, &skt->socket);
Rafael J. Wysocki9905d1b2009-11-03 10:54:58 +0100476 if (skt->state & SOCKET_PRESENT)
477 skt->resume_status = socket_setup(skt, resume_delay);
Rafael J. Wysockidbdd0f52018-02-21 13:24:16 +0100478 skt->state |= SOCKET_IN_RESUME;
Dominik Brodowski9e86749c2010-01-16 00:26:33 +0100479 mutex_unlock(&skt->ops_mutex);
Rafael J. Wysocki9905d1b2009-11-03 10:54:58 +0100480 return 0;
481}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Rafael J. Wysocki9905d1b2009-11-03 10:54:58 +0100483static int socket_late_resume(struct pcmcia_socket *skt)
484{
Christian Lamparter42b5dd52013-02-21 16:42:06 -0800485 int ret = 0;
Dominik Brodowskid7646f72010-03-15 21:46:34 +0100486
Dominik Brodowski9e86749c2010-01-16 00:26:33 +0100487 mutex_lock(&skt->ops_mutex);
Rafael J. Wysockidbdd0f52018-02-21 13:24:16 +0100488 skt->state &= ~(SOCKET_SUSPEND | SOCKET_IN_RESUME);
Dominik Brodowski9e86749c2010-01-16 00:26:33 +0100489 mutex_unlock(&skt->ops_mutex);
Dominik Brodowski88b060d2010-01-02 14:14:23 +0100490
Dominik Brodowskid7646f72010-03-15 21:46:34 +0100491 if (!(skt->state & SOCKET_PRESENT)) {
492 ret = socket_insert(skt);
493 if (ret == -ENODEV)
494 ret = 0;
495 return ret;
496 }
Dominik Brodowski88b060d2010-01-02 14:14:23 +0100497
498 if (skt->resume_status) {
499 socket_shutdown(skt);
500 return 0;
501 }
502
503 if (skt->suspended_state != skt->state) {
504 dev_dbg(&skt->dev,
505 "suspend state 0x%x != resume state 0x%x\n",
506 skt->suspended_state, skt->state);
507
508 socket_shutdown(skt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 return socket_insert(skt);
510 }
511
Christian Lamparter42b5dd52013-02-21 16:42:06 -0800512 if (!(skt->state & SOCKET_CARDBUS) && (skt->callback))
513 ret = skt->callback->early_resume(skt);
514 return ret;
515}
516
517/*
518 * Finalize the resume. In case of a cardbus socket, we have
519 * to rebind the devices as we can't be certain that it has been
520 * replaced, or not.
521 */
522static int socket_complete_resume(struct pcmcia_socket *skt)
523{
524 int ret = 0;
Dominik Brodowski88b060d2010-01-02 14:14:23 +0100525#ifdef CONFIG_CARDBUS
526 if (skt->state & SOCKET_CARDBUS) {
527 /* We can't be sure the CardBus card is the same
528 * as the one previously inserted. Therefore, remove
529 * and re-add... */
530 cb_free(skt);
Christian Lamparter42b5dd52013-02-21 16:42:06 -0800531 ret = cb_alloc(skt);
532 if (ret)
533 cb_free(skt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 }
Dominik Brodowski88b060d2010-01-02 14:14:23 +0100535#endif
Christian Lamparter42b5dd52013-02-21 16:42:06 -0800536 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537}
538
Rafael J. Wysocki9905d1b2009-11-03 10:54:58 +0100539/*
540 * Resume a socket. If a card is present, verify its CIS against
541 * our cached copy. If they are different, the card has been
542 * replaced, and we need to tell the drivers.
543 */
544static int socket_resume(struct pcmcia_socket *skt)
545{
Christian Lamparter42b5dd52013-02-21 16:42:06 -0800546 int err;
Rafael J. Wysocki9905d1b2009-11-03 10:54:58 +0100547 if (!(skt->state & SOCKET_SUSPEND))
548 return -EBUSY;
549
550 socket_early_resume(skt);
Christian Lamparter42b5dd52013-02-21 16:42:06 -0800551 err = socket_late_resume(skt);
552 if (!err)
553 err = socket_complete_resume(skt);
554 return err;
Rafael J. Wysocki9905d1b2009-11-03 10:54:58 +0100555}
556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557static void socket_remove(struct pcmcia_socket *skt)
558{
Joe Perchesf2e6cf72014-10-10 09:12:47 -0700559 dev_notice(&skt->dev, "pccard: card ejected from slot %d\n", skt->sock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 socket_shutdown(skt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561}
562
563/*
564 * Process a socket card detect status change.
565 *
566 * If we don't have a card already present, delay the detect event for
567 * about 20ms (to be on the safe side) before reading the socket status.
568 *
569 * Some i82365-based systems send multiple SS_DETECT events during card
570 * insertion, and the "card present" status bit seems to bounce. This
571 * will probably be true with GPIO-based card detection systems after
572 * the product has aged.
573 */
574static void socket_detect_change(struct pcmcia_socket *skt)
575{
576 if (!(skt->state & SOCKET_SUSPEND)) {
577 int status;
578
579 if (!(skt->state & SOCKET_PRESENT))
580 msleep(20);
581
582 skt->ops->get_status(skt, &status);
583 if ((skt->state & SOCKET_PRESENT) &&
584 !(status & SS_DETECT))
585 socket_remove(skt);
586 if (!(skt->state & SOCKET_PRESENT) &&
587 (status & SS_DETECT))
588 socket_insert(skt);
589 }
590}
591
592static int pccardd(void *__skt)
593{
594 struct pcmcia_socket *skt = __skt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 int ret;
596
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 skt->thread = current;
598 skt->socket = dead_socket;
599 skt->ops->init(skt);
600 skt->ops->set_socket(skt, &skt->socket);
601
602 /* register with the device core */
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200603 ret = device_register(&skt->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 if (ret) {
Joe Perchesf2e6cf72014-10-10 09:12:47 -0700605 dev_warn(&skt->dev, "PCMCIA: unable to register socket\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 skt->thread = NULL;
Serge E. Hallyn1da9ab72006-06-14 08:01:26 -0500607 complete(&skt->thread_done);
608 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 }
David Brownell4356d732008-04-28 01:03:20 -0700610 ret = pccard_sysfs_add_socket(&skt->dev);
611 if (ret)
612 dev_warn(&skt->dev, "err %d adding socket attributes\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Daniel Ritzbf4de6f2005-09-09 13:03:23 -0700614 complete(&skt->thread_done);
615
Dominik Brodowskid801c1402010-02-17 11:02:22 +0100616 /* wait for userspace to catch up */
617 msleep(250);
618
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700619 set_freezable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 for (;;) {
621 unsigned long flags;
622 unsigned int events;
Dominik Brodowskif971dbd2010-01-17 18:13:31 +0100623 unsigned int sysfs_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 spin_lock_irqsave(&skt->thread_lock, flags);
626 events = skt->thread_events;
627 skt->thread_events = 0;
Dominik Brodowskif971dbd2010-01-17 18:13:31 +0100628 sysfs_events = skt->sysfs_events;
629 skt->sysfs_events = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 spin_unlock_irqrestore(&skt->thread_lock, flags);
631
Dominik Brodowskif971dbd2010-01-17 18:13:31 +0100632 mutex_lock(&skt->skt_mutex);
Dominik Brodowski216d7cd2010-07-11 10:04:59 +0200633 if (events & SS_DETECT)
634 socket_detect_change(skt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Dominik Brodowskif971dbd2010-01-17 18:13:31 +0100636 if (sysfs_events) {
637 if (sysfs_events & PCMCIA_UEVENT_EJECT)
638 socket_remove(skt);
639 if (sysfs_events & PCMCIA_UEVENT_INSERT)
640 socket_insert(skt);
Dominik Brodowskif971dbd2010-01-17 18:13:31 +0100641 if ((sysfs_events & PCMCIA_UEVENT_SUSPEND) &&
642 !(skt->state & SOCKET_CARDBUS)) {
643 if (skt->callback)
644 ret = skt->callback->suspend(skt);
645 else
646 ret = 0;
Dominik Brodowski43821242010-05-03 15:10:09 +0200647 if (!ret) {
Dominik Brodowskif971dbd2010-01-17 18:13:31 +0100648 socket_suspend(skt);
Dominik Brodowski43821242010-05-03 15:10:09 +0200649 msleep(100);
650 }
651 }
652 if ((sysfs_events & PCMCIA_UEVENT_RESUME) &&
653 !(skt->state & SOCKET_CARDBUS)) {
654 ret = socket_resume(skt);
655 if (!ret && skt->callback)
656 skt->callback->resume(skt);
Dominik Brodowskif971dbd2010-01-17 18:13:31 +0100657 }
Dominik Brodowskiaf461fc2010-01-17 19:30:53 +0100658 if ((sysfs_events & PCMCIA_UEVENT_REQUERY) &&
659 !(skt->state & SOCKET_CARDBUS)) {
660 if (!ret && skt->callback)
661 skt->callback->requery(skt);
662 }
Dominik Brodowskif971dbd2010-01-17 18:13:31 +0100663 }
664 mutex_unlock(&skt->skt_mutex);
665
666 if (events || sysfs_events)
667 continue;
668
Dominik Brodowskifbb34852022-01-09 10:02:51 +0100669 set_current_state(TASK_INTERRUPTIBLE);
Serge E. Hallyn1da9ab72006-06-14 08:01:26 -0500670 if (kthread_should_stop())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 break;
Daniel Ritzbf4de6f2005-09-09 13:03:23 -0700672
673 schedule();
Dmitry Eremin-Solenikovd3046ba2015-03-30 14:33:45 +0300674
Daniel Ritzbf4de6f2005-09-09 13:03:23 -0700675 try_to_freeze();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 }
Dominik Brodowskifbb34852022-01-09 10:02:51 +0100677 /* make sure we are running before we exit */
678 __set_current_state(TASK_RUNNING);
Steven Rostedt220ec022005-10-10 11:13:17 -0400679
Dominik Brodowski3970dd82010-01-02 23:19:45 +0100680 /* shut down socket, if a device is still present */
681 if (skt->state & SOCKET_PRESENT) {
682 mutex_lock(&skt->skt_mutex);
683 socket_remove(skt);
684 mutex_unlock(&skt->skt_mutex);
685 }
686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 /* remove from the device core */
David Brownell4356d732008-04-28 01:03:20 -0700688 pccard_sysfs_remove_socket(&skt->dev);
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200689 device_unregister(&skt->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Serge E. Hallyn1da9ab72006-06-14 08:01:26 -0500691 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692}
693
694/*
695 * Yenta (at least) probes interrupts before registering the socket and
696 * starting the handler thread.
697 */
698void pcmcia_parse_events(struct pcmcia_socket *s, u_int events)
699{
Arjan van de Ven4b7a89a2006-06-30 10:31:13 +0200700 unsigned long flags;
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200701 dev_dbg(&s->dev, "parse_events: events %08x\n", events);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 if (s->thread) {
Arjan van de Ven4b7a89a2006-06-30 10:31:13 +0200703 spin_lock_irqsave(&s->thread_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 s->thread_events |= events;
Arjan van de Ven4b7a89a2006-06-30 10:31:13 +0200705 spin_unlock_irqrestore(&s->thread_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Christoph Hellwig0b402092007-04-22 13:55:36 +0100707 wake_up_process(s->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 }
709} /* pcmcia_parse_events */
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700710EXPORT_SYMBOL(pcmcia_parse_events);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Dominik Brodowskif971dbd2010-01-17 18:13:31 +0100712/**
713 * pcmcia_parse_uevents() - tell pccardd to issue manual commands
714 * @s: the PCMCIA socket we wan't to command
715 * @events: events to pass to pccardd
716 *
717 * userspace-issued insert, eject, suspend and resume commands must be
718 * handled by pccardd to avoid any sysfs-related deadlocks. Valid events
719 * are PCMCIA_UEVENT_EJECT (for eject), PCMCIA_UEVENT__INSERT (for insert),
Dominik Brodowskiaf461fc2010-01-17 19:30:53 +0100720 * PCMCIA_UEVENT_RESUME (for resume), PCMCIA_UEVENT_SUSPEND (for suspend)
721 * and PCMCIA_UEVENT_REQUERY (for re-querying the PCMCIA card).
Dominik Brodowskif971dbd2010-01-17 18:13:31 +0100722 */
723void pcmcia_parse_uevents(struct pcmcia_socket *s, u_int events)
724{
725 unsigned long flags;
726 dev_dbg(&s->dev, "parse_uevents: events %08x\n", events);
727 if (s->thread) {
728 spin_lock_irqsave(&s->thread_lock, flags);
729 s->sysfs_events |= events;
730 spin_unlock_irqrestore(&s->thread_lock, flags);
731
732 wake_up_process(s->thread);
733 }
734}
735EXPORT_SYMBOL(pcmcia_parse_uevents);
736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
738/* register pcmcia_callback */
739int pccard_register_pcmcia(struct pcmcia_socket *s, struct pcmcia_callback *c)
740{
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100741 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100743 /* s->skt_mutex also protects s->callback */
744 mutex_lock(&s->skt_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
746 if (c) {
747 /* registration */
748 if (s->callback) {
749 ret = -EBUSY;
750 goto err;
751 }
752
753 s->callback = c;
754
755 if ((s->state & (SOCKET_PRESENT|SOCKET_CARDBUS)) == SOCKET_PRESENT)
Dominik Brodowski7b24e792010-07-11 10:26:53 +0200756 s->callback->add(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 } else
758 s->callback = NULL;
759 err:
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100760 mutex_unlock(&s->skt_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
762 return ret;
763}
764EXPORT_SYMBOL(pccard_register_pcmcia);
765
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700767/* I'm not sure which "reset" function this is supposed to use,
768 * but for now, it uses the low-level interface's reset, not the
769 * CIS register.
770 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Dominik Brodowski994917f2008-08-31 15:20:26 +0200772int pcmcia_reset_card(struct pcmcia_socket *skt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773{
774 int ret;
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700775
Dominik Brodowskid50dbec2009-10-23 12:51:28 +0200776 dev_dbg(&skt->dev, "resetting socket\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100778 mutex_lock(&skt->skt_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 do {
780 if (!(skt->state & SOCKET_PRESENT)) {
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200781 dev_dbg(&skt->dev, "can't reset, not present\n");
Dominik Brodowski3939c1ef2008-08-03 11:10:56 +0200782 ret = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 break;
784 }
785 if (skt->state & SOCKET_SUSPEND) {
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200786 dev_dbg(&skt->dev, "can't reset, suspended\n");
Dominik Brodowskif9580952008-08-03 11:40:19 +0200787 ret = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 break;
789 }
790 if (skt->state & SOCKET_CARDBUS) {
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200791 dev_dbg(&skt->dev, "can't reset, is cardbus\n");
Dominik Brodowskide6405e2008-08-03 10:47:59 +0200792 ret = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 break;
794 }
795
Dominik Brodowski216d7cd2010-07-11 10:04:59 +0200796 if (skt->callback)
797 skt->callback->suspend(skt);
798 mutex_lock(&skt->ops_mutex);
799 ret = socket_reset(skt);
800 mutex_unlock(&skt->ops_mutex);
801 if ((ret == 0) && (skt->callback))
802 skt->callback->resume(skt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200804 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 } while (0);
Dominik Brodowski7fe908d2006-01-10 21:20:36 +0100806 mutex_unlock(&skt->skt_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
808 return ret;
809} /* reset_card */
Dominik Brodowski994917f2008-08-31 15:20:26 +0200810EXPORT_SYMBOL(pcmcia_reset_card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Kay Sievers7eff2e72007-08-14 15:15:12 +0200813static int pcmcia_socket_uevent(struct device *dev,
814 struct kobj_uevent_env *env)
Dominik Brodowski3ee13932005-06-27 16:28:05 -0700815{
816 struct pcmcia_socket *s = container_of(dev, struct pcmcia_socket, dev);
Dominik Brodowski3ee13932005-06-27 16:28:05 -0700817
Kay Sievers7eff2e72007-08-14 15:15:12 +0200818 if (add_uevent_var(env, "SOCKET_NO=%u", s->sock))
Dominik Brodowski3ee13932005-06-27 16:28:05 -0700819 return -ENOMEM;
820
Dominik Brodowski3ee13932005-06-27 16:28:05 -0700821 return 0;
822}
823
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Dominik Brodowski378a33a2005-06-27 16:28:54 -0700825static struct completion pcmcia_unload;
826
827static void pcmcia_release_socket_class(struct class *data)
828{
829 complete(&pcmcia_unload);
830}
831
832
Dominik Brodowskid7646f72010-03-15 21:46:34 +0100833#ifdef CONFIG_PM
834
835static int __pcmcia_pm_op(struct device *dev,
836 int (*callback) (struct pcmcia_socket *skt))
837{
838 struct pcmcia_socket *s = container_of(dev, struct pcmcia_socket, dev);
839 int ret;
840
841 mutex_lock(&s->skt_mutex);
842 ret = callback(s);
843 mutex_unlock(&s->skt_mutex);
844
845 return ret;
846}
847
848static int pcmcia_socket_dev_suspend_noirq(struct device *dev)
849{
850 return __pcmcia_pm_op(dev, socket_suspend);
851}
852
853static int pcmcia_socket_dev_resume_noirq(struct device *dev)
854{
855 return __pcmcia_pm_op(dev, socket_early_resume);
856}
857
Maciej Żenczykowski3c7d2b72010-10-13 21:47:14 -0700858static int __used pcmcia_socket_dev_resume(struct device *dev)
Dominik Brodowskid7646f72010-03-15 21:46:34 +0100859{
860 return __pcmcia_pm_op(dev, socket_late_resume);
861}
862
Christian Lamparter42b5dd52013-02-21 16:42:06 -0800863static void __used pcmcia_socket_dev_complete(struct device *dev)
864{
865 WARN(__pcmcia_pm_op(dev, socket_complete_resume),
866 "failed to complete resume");
867}
868
Dominik Brodowskid7646f72010-03-15 21:46:34 +0100869static const struct dev_pm_ops pcmcia_socket_pm_ops = {
870 /* dev_resume may be called with IRQs enabled */
871 SET_SYSTEM_SLEEP_PM_OPS(NULL,
872 pcmcia_socket_dev_resume)
873
874 /* late suspend must be called with IRQs disabled */
875 .suspend_noirq = pcmcia_socket_dev_suspend_noirq,
876 .freeze_noirq = pcmcia_socket_dev_suspend_noirq,
877 .poweroff_noirq = pcmcia_socket_dev_suspend_noirq,
878
879 /* early resume must be called with IRQs disabled */
880 .resume_noirq = pcmcia_socket_dev_resume_noirq,
881 .thaw_noirq = pcmcia_socket_dev_resume_noirq,
882 .restore_noirq = pcmcia_socket_dev_resume_noirq,
Christian Lamparter42b5dd52013-02-21 16:42:06 -0800883 .complete = pcmcia_socket_dev_complete,
Dominik Brodowskid7646f72010-03-15 21:46:34 +0100884};
885
886#define PCMCIA_SOCKET_CLASS_PM_OPS (&pcmcia_socket_pm_ops)
887
888#else /* CONFIG_PM */
889
890#define PCMCIA_SOCKET_CLASS_PM_OPS NULL
891
892#endif /* CONFIG_PM */
893
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894struct class pcmcia_socket_class = {
895 .name = "pcmcia_socket",
Greg Kroah-Hartman87373312006-09-12 17:00:10 +0200896 .dev_uevent = pcmcia_socket_uevent,
897 .dev_release = pcmcia_release_socket,
Dominik Brodowski378a33a2005-06-27 16:28:54 -0700898 .class_release = pcmcia_release_socket_class,
Dominik Brodowskid7646f72010-03-15 21:46:34 +0100899 .pm = PCMCIA_SOCKET_CLASS_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900};
901EXPORT_SYMBOL(pcmcia_socket_class);
902
903
904static int __init init_pcmcia_cs(void)
905{
Dominik Brodowski378a33a2005-06-27 16:28:54 -0700906 init_completion(&pcmcia_unload);
David Brownell4356d732008-04-28 01:03:20 -0700907 return class_register(&pcmcia_socket_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908}
909
910static void __exit exit_pcmcia_cs(void)
911{
Dominik Brodowski33519dd2005-06-27 16:28:53 -0700912 class_unregister(&pcmcia_socket_class);
Dominik Brodowski378a33a2005-06-27 16:28:54 -0700913 wait_for_completion(&pcmcia_unload);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914}
915
916subsys_initcall(init_pcmcia_cs);
917module_exit(exit_pcmcia_cs);
918