Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <linux/device.h> |
Serge E. Hallyn | 1da9ab7 | 2006-06-14 08:01:26 -0500 | [diff] [blame] | 30 | #include <linux/kthread.h> |
Nigel Cunningham | 7dfb710 | 2006-12-06 20:34:23 -0800 | [diff] [blame] | 31 | #include <linux/freezer.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include <asm/irq.h> |
| 33 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include <pcmcia/ss.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include <pcmcia/cistpl.h> |
| 36 | #include <pcmcia/cisreg.h> |
| 37 | #include <pcmcia/ds.h> |
| 38 | #include "cs_internal.h" |
| 39 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | |
| 41 | /* Module parameters */ |
| 42 | |
| 43 | MODULE_AUTHOR("David Hinds <dahinds@users.sourceforge.net>"); |
Dominik Brodowski | 33519dd | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 44 | MODULE_DESCRIPTION("Linux Kernel Card Services"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | MODULE_LICENSE("GPL"); |
| 46 | |
| 47 | #define INT_MODULE_PARM(n, v) static int n = v; module_param(n, int, 0444) |
| 48 | |
| 49 | INT_MODULE_PARM(setup_delay, 10); /* centiseconds */ |
| 50 | INT_MODULE_PARM(resume_delay, 20); /* centiseconds */ |
| 51 | INT_MODULE_PARM(shutdown_delay, 3); /* centiseconds */ |
| 52 | INT_MODULE_PARM(vcc_settle, 40); /* centiseconds */ |
| 53 | INT_MODULE_PARM(reset_time, 10); /* usecs */ |
| 54 | INT_MODULE_PARM(unreset_delay, 10); /* centiseconds */ |
| 55 | INT_MODULE_PARM(unreset_check, 10); /* centiseconds */ |
| 56 | INT_MODULE_PARM(unreset_limit, 30); /* unreset_check's */ |
| 57 | |
| 58 | /* Access speed for attribute memory windows */ |
| 59 | INT_MODULE_PARM(cis_speed, 300); /* ns */ |
| 60 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | |
| 62 | socket_state_t dead_socket = { |
| 63 | .csc_mask = SS_DETECT, |
| 64 | }; |
Dominik Brodowski | 33519dd | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 65 | EXPORT_SYMBOL(dead_socket); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | |
| 67 | |
| 68 | /* List of all sockets, protected by a rwsem */ |
| 69 | LIST_HEAD(pcmcia_socket_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | EXPORT_SYMBOL(pcmcia_socket_list); |
Dominik Brodowski | 33519dd | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 71 | |
| 72 | DECLARE_RWSEM(pcmcia_socket_list_rwsem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | EXPORT_SYMBOL(pcmcia_socket_list_rwsem); |
| 74 | |
| 75 | |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 76 | struct pcmcia_socket *pcmcia_get_socket(struct pcmcia_socket *skt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | { |
Greg Kroah-Hartman | 8737331 | 2006-09-12 17:00:10 +0200 | [diff] [blame] | 78 | struct device *dev = get_device(&skt->dev); |
| 79 | if (!dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | return NULL; |
Dominik Brodowski | 593f010b | 2010-01-02 22:59:15 +0100 | [diff] [blame] | 81 | return dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | } |
| 83 | EXPORT_SYMBOL(pcmcia_get_socket); |
| 84 | |
| 85 | |
| 86 | void pcmcia_put_socket(struct pcmcia_socket *skt) |
| 87 | { |
Greg Kroah-Hartman | 8737331 | 2006-09-12 17:00:10 +0200 | [diff] [blame] | 88 | put_device(&skt->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | } |
| 90 | EXPORT_SYMBOL(pcmcia_put_socket); |
| 91 | |
| 92 | |
Greg Kroah-Hartman | 8737331 | 2006-09-12 17:00:10 +0200 | [diff] [blame] | 93 | static void pcmcia_release_socket(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | { |
Greg Kroah-Hartman | 8737331 | 2006-09-12 17:00:10 +0200 | [diff] [blame] | 95 | struct pcmcia_socket *socket = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | |
| 97 | complete(&socket->socket_released); |
| 98 | } |
| 99 | |
| 100 | static int pccardd(void *__skt); |
| 101 | |
| 102 | /** |
| 103 | * pcmcia_register_socket - add a new pcmcia socket device |
Randy Dunlap | 7818786 | 2007-12-10 15:49:22 -0800 | [diff] [blame] | 104 | * @socket: the &socket to register |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | */ |
| 106 | int pcmcia_register_socket(struct pcmcia_socket *socket) |
| 107 | { |
Serge E. Hallyn | 1da9ab7 | 2006-06-14 08:01:26 -0500 | [diff] [blame] | 108 | struct task_struct *tsk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | int ret; |
| 110 | |
Greg Kroah-Hartman | 8737331 | 2006-09-12 17:00:10 +0200 | [diff] [blame] | 111 | if (!socket || !socket->ops || !socket->dev.parent || !socket->resource_ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | return -EINVAL; |
| 113 | |
Dominik Brodowski | d50dbec | 2009-10-23 12:51:28 +0200 | [diff] [blame] | 114 | dev_dbg(&socket->dev, "pcmcia_register_socket(0x%p)\n", socket->ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | /* try to obtain a socket number [yes, it gets ugly if we |
Dominik Brodowski | 33519dd | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 117 | * register more than 2^sizeof(unsigned int) pcmcia |
| 118 | * sockets... but the socket number is deprecated |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | * 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 King | 5bc6b68 | 2005-07-07 17:59:07 -0700 | [diff] [blame] | 139 | #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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | |
| 147 | /* set proper values in socket->dev */ |
Greg Kroah-Hartman | 8737331 | 2006-09-12 17:00:10 +0200 | [diff] [blame] | 148 | dev_set_drvdata(&socket->dev, socket); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | socket->dev.class = &pcmcia_socket_class; |
Kay Sievers | 2509698 | 2008-11-01 11:46:06 +0100 | [diff] [blame] | 150 | dev_set_name(&socket->dev, "pcmcia_socket%u", socket->sock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | |
| 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 Brodowski | 7fe908d | 2006-01-10 21:20:36 +0100 | [diff] [blame] | 160 | mutex_init(&socket->skt_mutex); |
Dominik Brodowski | 6b8e087 | 2010-01-12 21:42:51 +0100 | [diff] [blame] | 161 | mutex_init(&socket->ops_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | spin_lock_init(&socket->thread_lock); |
| 163 | |
Dominik Brodowski | b176945 | 2008-11-09 21:47:47 +0100 | [diff] [blame] | 164 | if (socket->resource_ops->init) { |
Dominik Brodowski | cfe5d80 | 2010-01-17 19:31:45 +0100 | [diff] [blame] | 165 | mutex_lock(&socket->ops_mutex); |
Dominik Brodowski | b176945 | 2008-11-09 21:47:47 +0100 | [diff] [blame] | 166 | ret = socket->resource_ops->init(socket); |
Dominik Brodowski | cfe5d80 | 2010-01-17 19:31:45 +0100 | [diff] [blame] | 167 | mutex_unlock(&socket->ops_mutex); |
Dominik Brodowski | b176945 | 2008-11-09 21:47:47 +0100 | [diff] [blame] | 168 | if (ret) |
| 169 | goto err; |
| 170 | } |
| 171 | |
Serge E. Hallyn | 1da9ab7 | 2006-06-14 08:01:26 -0500 | [diff] [blame] | 172 | tsk = kthread_run(pccardd, socket, "pccardd"); |
| 173 | if (IS_ERR(tsk)) { |
| 174 | ret = PTR_ERR(tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | goto err; |
Serge E. Hallyn | 1da9ab7 | 2006-06-14 08:01:26 -0500 | [diff] [blame] | 176 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | |
| 178 | wait_for_completion(&socket->thread_done); |
Serge E. Hallyn | 1da9ab7 | 2006-06-14 08:01:26 -0500 | [diff] [blame] | 179 | if (!socket->thread) { |
Dominik Brodowski | 2e55bf6 | 2008-08-02 18:08:38 +0200 | [diff] [blame] | 180 | dev_printk(KERN_WARNING, &socket->dev, |
| 181 | "PCMCIA: warning: socket thread did not start\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | return -EIO; |
| 183 | } |
Serge E. Hallyn | 1da9ab7 | 2006-06-14 08:01:26 -0500 | [diff] [blame] | 184 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | pcmcia_parse_events(socket, SS_DETECT); |
| 186 | |
Dominik Brodowski | 638bba5 | 2009-11-07 12:26:17 +0100 | [diff] [blame] | 187 | /* |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | 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 */ |
| 202 | EXPORT_SYMBOL(pcmcia_register_socket); |
| 203 | |
| 204 | |
| 205 | /** |
| 206 | * pcmcia_unregister_socket - remove a pcmcia socket device |
Randy Dunlap | 7818786 | 2007-12-10 15:49:22 -0800 | [diff] [blame] | 207 | * @socket: the &socket to unregister |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | */ |
| 209 | void pcmcia_unregister_socket(struct pcmcia_socket *socket) |
| 210 | { |
| 211 | if (!socket) |
| 212 | return; |
| 213 | |
Dominik Brodowski | d50dbec | 2009-10-23 12:51:28 +0200 | [diff] [blame] | 214 | dev_dbg(&socket->dev, "pcmcia_unregister_socket(0x%p)\n", socket->ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | |
Christoph Hellwig | 0b40209 | 2007-04-22 13:55:36 +0100 | [diff] [blame] | 216 | if (socket->thread) |
Serge E. Hallyn | 1da9ab7 | 2006-06-14 08:01:26 -0500 | [diff] [blame] | 217 | kthread_stop(socket->thread); |
Christoph Hellwig | 0b40209 | 2007-04-22 13:55:36 +0100 | [diff] [blame] | 218 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | /* 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 Brodowski | cfe5d80 | 2010-01-17 19:31:45 +0100 | [diff] [blame] | 225 | if (socket->resource_ops->exit) { |
| 226 | mutex_lock(&socket->ops_mutex); |
Dominik Brodowski | 385ee87 | 2010-01-06 11:23:58 +0100 | [diff] [blame] | 227 | socket->resource_ops->exit(socket); |
Dominik Brodowski | cfe5d80 | 2010-01-17 19:31:45 +0100 | [diff] [blame] | 228 | mutex_unlock(&socket->ops_mutex); |
| 229 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | wait_for_completion(&socket->socket_released); |
| 231 | } /* pcmcia_unregister_socket */ |
| 232 | EXPORT_SYMBOL(pcmcia_unregister_socket); |
| 233 | |
| 234 | |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 235 | struct pcmcia_socket *pcmcia_get_socket_by_nr(unsigned int nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | { |
| 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 | } |
| 250 | EXPORT_SYMBOL(pcmcia_get_socket_by_nr); |
| 251 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | static int socket_reset(struct pcmcia_socket *skt) |
| 253 | { |
| 254 | int status, i; |
| 255 | |
Dominik Brodowski | d50dbec | 2009-10-23 12:51:28 +0200 | [diff] [blame] | 256 | dev_dbg(&skt->dev, "reset\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | |
| 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 Brodowski | 3939c1ef | 2008-08-03 11:10:56 +0200 | [diff] [blame] | 270 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | |
| 272 | if (status & SS_READY) |
Dominik Brodowski | 4c89e88 | 2008-08-03 10:07:45 +0200 | [diff] [blame] | 273 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | |
| 275 | msleep(unreset_check * 10); |
| 276 | } |
| 277 | |
Dominik Brodowski | d50dbec | 2009-10-23 12:51:28 +0200 | [diff] [blame] | 278 | dev_printk(KERN_ERR, &skt->dev, "time out after reset.\n"); |
Dominik Brodowski | 8567142 | 2008-08-03 11:15:45 +0200 | [diff] [blame] | 279 | return -ETIMEDOUT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | } |
| 281 | |
Randy Dunlap | 7818786 | 2007-12-10 15:49:22 -0800 | [diff] [blame] | 282 | /* |
Daniel Ritz | 6e49388 | 2005-12-30 15:12:35 +0100 | [diff] [blame] | 283 | * 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 | */ |
| 288 | static void socket_shutdown(struct pcmcia_socket *s) |
| 289 | { |
| 290 | int status; |
| 291 | |
Dominik Brodowski | d50dbec | 2009-10-23 12:51:28 +0200 | [diff] [blame] | 292 | dev_dbg(&s->dev, "shutdown\n"); |
Daniel Ritz | 6e49388 | 2005-12-30 15:12:35 +0100 | [diff] [blame] | 293 | |
Dominik Brodowski | 7b24e79 | 2010-07-11 10:26:53 +0200 | [diff] [blame] | 294 | if (s->callback) |
| 295 | s->callback->remove(s); |
Dominik Brodowski | 9e86749c | 2010-01-16 00:26:33 +0100 | [diff] [blame] | 296 | |
| 297 | mutex_lock(&s->ops_mutex); |
Daniel Ritz | 6e49388 | 2005-12-30 15:12:35 +0100 | [diff] [blame] | 298 | 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 Ritz | 6e49388 | 2005-12-30 15:12:35 +0100 | [diff] [blame] | 306 | s->lock_count = 0; |
Dominik Brodowski | 904e377 | 2010-01-02 12:28:04 +0100 | [diff] [blame] | 307 | kfree(s->fake_cis); |
| 308 | s->fake_cis = NULL; |
Dominik Brodowski | c3bfc96 | 2010-01-18 08:43:39 +0100 | [diff] [blame] | 309 | 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 Ritz | 6e49388 | 2005-12-30 15:12:35 +0100 | [diff] [blame] | 319 | #ifdef CONFIG_CARDBUS |
| 320 | cb_free(s); |
| 321 | #endif |
Daniel Ritz | 6e49388 | 2005-12-30 15:12:35 +0100 | [diff] [blame] | 322 | |
Daniel Ritz | afb2c16 | 2007-07-31 00:38:08 -0700 | [diff] [blame] | 323 | /* give socket some time to power down */ |
| 324 | msleep(100); |
| 325 | |
Daniel Ritz | 6e49388 | 2005-12-30 15:12:35 +0100 | [diff] [blame] | 326 | s->ops->get_status(s, &status); |
| 327 | if (status & SS_POWERON) { |
Dominik Brodowski | 2e55bf6 | 2008-08-02 18:08:38 +0200 | [diff] [blame] | 328 | dev_printk(KERN_ERR, &s->dev, |
| 329 | "*** DANGER *** unable to remove socket power\n"); |
Daniel Ritz | 6e49388 | 2005-12-30 15:12:35 +0100 | [diff] [blame] | 330 | } |
| 331 | |
Dominik Brodowski | 3970dd8 | 2010-01-02 23:19:45 +0100 | [diff] [blame] | 332 | s->state &= ~SOCKET_INUSE; |
Daniel Ritz | 6e49388 | 2005-12-30 15:12:35 +0100 | [diff] [blame] | 333 | } |
| 334 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | static int socket_setup(struct pcmcia_socket *skt, int initial_delay) |
| 336 | { |
| 337 | int status, i; |
| 338 | |
Dominik Brodowski | d50dbec | 2009-10-23 12:51:28 +0200 | [diff] [blame] | 339 | dev_dbg(&skt->dev, "setup\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | |
| 341 | skt->ops->get_status(skt, &status); |
| 342 | if (!(status & SS_DETECT)) |
Dominik Brodowski | 3939c1ef | 2008-08-03 11:10:56 +0200 | [diff] [blame] | 343 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | |
| 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 Brodowski | 3939c1ef | 2008-08-03 11:10:56 +0200 | [diff] [blame] | 350 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | |
| 352 | if (!(status & SS_PENDING)) |
| 353 | break; |
| 354 | |
| 355 | msleep(100); |
| 356 | } |
| 357 | |
| 358 | if (status & SS_PENDING) { |
Dominik Brodowski | d50dbec | 2009-10-23 12:51:28 +0200 | [diff] [blame] | 359 | dev_printk(KERN_ERR, &skt->dev, |
| 360 | "voltage interrogation timed out.\n"); |
Dominik Brodowski | 8567142 | 2008-08-03 11:15:45 +0200 | [diff] [blame] | 361 | return -ETIMEDOUT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | if (status & SS_CARDBUS) { |
Russell King | 5bc6b68 | 2005-07-07 17:59:07 -0700 | [diff] [blame] | 365 | if (!(skt->features & SS_CAP_CARDBUS)) { |
Dominik Brodowski | d50dbec | 2009-10-23 12:51:28 +0200 | [diff] [blame] | 366 | dev_printk(KERN_ERR, &skt->dev, |
| 367 | "cardbus cards are not supported.\n"); |
Dominik Brodowski | 610e237 | 2008-08-03 11:58:53 +0200 | [diff] [blame] | 368 | return -EINVAL; |
Russell King | 5bc6b68 | 2005-07-07 17:59:07 -0700 | [diff] [blame] | 369 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | skt->state |= SOCKET_CARDBUS; |
Dominik Brodowski | 88b060d | 2010-01-02 14:14:23 +0100 | [diff] [blame] | 371 | } else |
| 372 | skt->state &= ~SOCKET_CARDBUS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | |
| 374 | /* |
| 375 | * Decode the card voltage requirements, and apply power to the card. |
| 376 | */ |
| 377 | if (status & SS_3VCARD) |
| 378 | skt->socket.Vcc = skt->socket.Vpp = 33; |
| 379 | else if (!(status & SS_XVCARD)) |
| 380 | skt->socket.Vcc = skt->socket.Vpp = 50; |
| 381 | else { |
Dominik Brodowski | d50dbec | 2009-10-23 12:51:28 +0200 | [diff] [blame] | 382 | dev_printk(KERN_ERR, &skt->dev, "unsupported voltage key.\n"); |
Dominik Brodowski | 610e237 | 2008-08-03 11:58:53 +0200 | [diff] [blame] | 383 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | } |
Daniel Ritz | fa912bc | 2005-06-23 00:10:12 -0700 | [diff] [blame] | 385 | |
| 386 | if (skt->power_hook) |
| 387 | skt->power_hook(skt, HOOK_POWER_PRE); |
| 388 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | skt->socket.flags = 0; |
| 390 | skt->ops->set_socket(skt, &skt->socket); |
| 391 | |
| 392 | /* |
| 393 | * Wait "vcc_settle" for the supply to stabilise. |
| 394 | */ |
| 395 | msleep(vcc_settle * 10); |
| 396 | |
| 397 | skt->ops->get_status(skt, &status); |
| 398 | if (!(status & SS_POWERON)) { |
Dominik Brodowski | d50dbec | 2009-10-23 12:51:28 +0200 | [diff] [blame] | 399 | dev_printk(KERN_ERR, &skt->dev, "unable to apply power.\n"); |
Dominik Brodowski | 610e237 | 2008-08-03 11:58:53 +0200 | [diff] [blame] | 400 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | } |
| 402 | |
Daniel Ritz | fa912bc | 2005-06-23 00:10:12 -0700 | [diff] [blame] | 403 | status = socket_reset(skt); |
| 404 | |
| 405 | if (skt->power_hook) |
| 406 | skt->power_hook(skt, HOOK_POWER_POST); |
| 407 | |
| 408 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | /* |
| 412 | * Handle card insertion. Setup the socket, reset the card, |
| 413 | * and then tell the rest of PCMCIA that a card is present. |
| 414 | */ |
| 415 | static int socket_insert(struct pcmcia_socket *skt) |
| 416 | { |
| 417 | int ret; |
| 418 | |
Dominik Brodowski | d50dbec | 2009-10-23 12:51:28 +0200 | [diff] [blame] | 419 | dev_dbg(&skt->dev, "insert\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | |
Dominik Brodowski | 9e86749c | 2010-01-16 00:26:33 +0100 | [diff] [blame] | 421 | mutex_lock(&skt->ops_mutex); |
Dominik Brodowski | f971dbd | 2010-01-17 18:13:31 +0100 | [diff] [blame] | 422 | if (skt->state & SOCKET_INUSE) { |
| 423 | mutex_unlock(&skt->ops_mutex); |
| 424 | return -EINVAL; |
| 425 | } |
Dominik Brodowski | 3970dd8 | 2010-01-02 23:19:45 +0100 | [diff] [blame] | 426 | skt->state |= SOCKET_INUSE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | |
| 428 | ret = socket_setup(skt, setup_delay); |
Dominik Brodowski | 4c89e88 | 2008-08-03 10:07:45 +0200 | [diff] [blame] | 429 | if (ret == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | skt->state |= SOCKET_PRESENT; |
Dominik Brodowski | 807277c | 2005-11-12 23:34:06 +0100 | [diff] [blame] | 431 | |
Dominik Brodowski | 2e55bf6 | 2008-08-02 18:08:38 +0200 | [diff] [blame] | 432 | dev_printk(KERN_NOTICE, &skt->dev, |
| 433 | "pccard: %s card inserted into slot %d\n", |
| 434 | (skt->state & SOCKET_CARDBUS) ? "CardBus" : "PCMCIA", |
| 435 | skt->sock); |
Dominik Brodowski | 807277c | 2005-11-12 23:34:06 +0100 | [diff] [blame] | 436 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | #ifdef CONFIG_CARDBUS |
| 438 | if (skt->state & SOCKET_CARDBUS) { |
| 439 | cb_alloc(skt); |
| 440 | skt->state |= SOCKET_CARDBUS_CONFIG; |
| 441 | } |
| 442 | #endif |
Dominik Brodowski | d50dbec | 2009-10-23 12:51:28 +0200 | [diff] [blame] | 443 | dev_dbg(&skt->dev, "insert done\n"); |
Dominik Brodowski | 9e86749c | 2010-01-16 00:26:33 +0100 | [diff] [blame] | 444 | mutex_unlock(&skt->ops_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | |
Dominik Brodowski | 7b24e79 | 2010-07-11 10:26:53 +0200 | [diff] [blame] | 446 | if (!(skt->state & SOCKET_CARDBUS) && (skt->callback)) |
| 447 | skt->callback->add(skt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | } else { |
Dominik Brodowski | 9e86749c | 2010-01-16 00:26:33 +0100 | [diff] [blame] | 449 | mutex_unlock(&skt->ops_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | socket_shutdown(skt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | return ret; |
| 454 | } |
| 455 | |
| 456 | static int socket_suspend(struct pcmcia_socket *skt) |
| 457 | { |
| 458 | if (skt->state & SOCKET_SUSPEND) |
Dominik Brodowski | f958095 | 2008-08-03 11:40:19 +0200 | [diff] [blame] | 459 | return -EBUSY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | |
Dominik Brodowski | 9e86749c | 2010-01-16 00:26:33 +0100 | [diff] [blame] | 461 | mutex_lock(&skt->ops_mutex); |
Dominik Brodowski | 88b060d | 2010-01-02 14:14:23 +0100 | [diff] [blame] | 462 | skt->suspended_state = skt->state; |
| 463 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | skt->socket = dead_socket; |
| 465 | skt->ops->set_socket(skt, &skt->socket); |
| 466 | if (skt->ops->suspend) |
| 467 | skt->ops->suspend(skt); |
| 468 | skt->state |= SOCKET_SUSPEND; |
Dominik Brodowski | 9e86749c | 2010-01-16 00:26:33 +0100 | [diff] [blame] | 469 | mutex_unlock(&skt->ops_mutex); |
Dominik Brodowski | 4c89e88 | 2008-08-03 10:07:45 +0200 | [diff] [blame] | 470 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | } |
| 472 | |
Rafael J. Wysocki | 9905d1b | 2009-11-03 10:54:58 +0100 | [diff] [blame] | 473 | static int socket_early_resume(struct pcmcia_socket *skt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | { |
Dominik Brodowski | 9e86749c | 2010-01-16 00:26:33 +0100 | [diff] [blame] | 475 | mutex_lock(&skt->ops_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | skt->socket = dead_socket; |
| 477 | skt->ops->init(skt); |
| 478 | skt->ops->set_socket(skt, &skt->socket); |
Rafael J. Wysocki | 9905d1b | 2009-11-03 10:54:58 +0100 | [diff] [blame] | 479 | if (skt->state & SOCKET_PRESENT) |
| 480 | skt->resume_status = socket_setup(skt, resume_delay); |
Dominik Brodowski | 9e86749c | 2010-01-16 00:26:33 +0100 | [diff] [blame] | 481 | mutex_unlock(&skt->ops_mutex); |
Rafael J. Wysocki | 9905d1b | 2009-11-03 10:54:58 +0100 | [diff] [blame] | 482 | return 0; |
| 483 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | |
Rafael J. Wysocki | 9905d1b | 2009-11-03 10:54:58 +0100 | [diff] [blame] | 485 | static int socket_late_resume(struct pcmcia_socket *skt) |
| 486 | { |
Christian Lamparter | 42b5dd5 | 2013-02-21 16:42:06 -0800 | [diff] [blame] | 487 | int ret = 0; |
Dominik Brodowski | d7646f7 | 2010-03-15 21:46:34 +0100 | [diff] [blame] | 488 | |
Dominik Brodowski | 9e86749c | 2010-01-16 00:26:33 +0100 | [diff] [blame] | 489 | mutex_lock(&skt->ops_mutex); |
Dominik Brodowski | 88b060d | 2010-01-02 14:14:23 +0100 | [diff] [blame] | 490 | skt->state &= ~SOCKET_SUSPEND; |
Dominik Brodowski | 9e86749c | 2010-01-16 00:26:33 +0100 | [diff] [blame] | 491 | mutex_unlock(&skt->ops_mutex); |
Dominik Brodowski | 88b060d | 2010-01-02 14:14:23 +0100 | [diff] [blame] | 492 | |
Dominik Brodowski | d7646f7 | 2010-03-15 21:46:34 +0100 | [diff] [blame] | 493 | if (!(skt->state & SOCKET_PRESENT)) { |
| 494 | ret = socket_insert(skt); |
| 495 | if (ret == -ENODEV) |
| 496 | ret = 0; |
| 497 | return ret; |
| 498 | } |
Dominik Brodowski | 88b060d | 2010-01-02 14:14:23 +0100 | [diff] [blame] | 499 | |
| 500 | if (skt->resume_status) { |
| 501 | socket_shutdown(skt); |
| 502 | return 0; |
| 503 | } |
| 504 | |
| 505 | if (skt->suspended_state != skt->state) { |
| 506 | dev_dbg(&skt->dev, |
| 507 | "suspend state 0x%x != resume state 0x%x\n", |
| 508 | skt->suspended_state, skt->state); |
| 509 | |
| 510 | socket_shutdown(skt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | return socket_insert(skt); |
| 512 | } |
| 513 | |
Christian Lamparter | 42b5dd5 | 2013-02-21 16:42:06 -0800 | [diff] [blame] | 514 | if (!(skt->state & SOCKET_CARDBUS) && (skt->callback)) |
| 515 | ret = skt->callback->early_resume(skt); |
| 516 | return ret; |
| 517 | } |
| 518 | |
| 519 | /* |
| 520 | * Finalize the resume. In case of a cardbus socket, we have |
| 521 | * to rebind the devices as we can't be certain that it has been |
| 522 | * replaced, or not. |
| 523 | */ |
| 524 | static int socket_complete_resume(struct pcmcia_socket *skt) |
| 525 | { |
| 526 | int ret = 0; |
Dominik Brodowski | 88b060d | 2010-01-02 14:14:23 +0100 | [diff] [blame] | 527 | #ifdef CONFIG_CARDBUS |
| 528 | if (skt->state & SOCKET_CARDBUS) { |
| 529 | /* We can't be sure the CardBus card is the same |
| 530 | * as the one previously inserted. Therefore, remove |
| 531 | * and re-add... */ |
| 532 | cb_free(skt); |
Christian Lamparter | 42b5dd5 | 2013-02-21 16:42:06 -0800 | [diff] [blame] | 533 | ret = cb_alloc(skt); |
| 534 | if (ret) |
| 535 | cb_free(skt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | } |
Dominik Brodowski | 88b060d | 2010-01-02 14:14:23 +0100 | [diff] [blame] | 537 | #endif |
Christian Lamparter | 42b5dd5 | 2013-02-21 16:42:06 -0800 | [diff] [blame] | 538 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | } |
| 540 | |
Rafael J. Wysocki | 9905d1b | 2009-11-03 10:54:58 +0100 | [diff] [blame] | 541 | /* |
| 542 | * Resume a socket. If a card is present, verify its CIS against |
| 543 | * our cached copy. If they are different, the card has been |
| 544 | * replaced, and we need to tell the drivers. |
| 545 | */ |
| 546 | static int socket_resume(struct pcmcia_socket *skt) |
| 547 | { |
Christian Lamparter | 42b5dd5 | 2013-02-21 16:42:06 -0800 | [diff] [blame] | 548 | int err; |
Rafael J. Wysocki | 9905d1b | 2009-11-03 10:54:58 +0100 | [diff] [blame] | 549 | if (!(skt->state & SOCKET_SUSPEND)) |
| 550 | return -EBUSY; |
| 551 | |
| 552 | socket_early_resume(skt); |
Christian Lamparter | 42b5dd5 | 2013-02-21 16:42:06 -0800 | [diff] [blame] | 553 | err = socket_late_resume(skt); |
| 554 | if (!err) |
| 555 | err = socket_complete_resume(skt); |
| 556 | return err; |
Rafael J. Wysocki | 9905d1b | 2009-11-03 10:54:58 +0100 | [diff] [blame] | 557 | } |
| 558 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | static void socket_remove(struct pcmcia_socket *skt) |
| 560 | { |
Dominik Brodowski | 2e55bf6 | 2008-08-02 18:08:38 +0200 | [diff] [blame] | 561 | dev_printk(KERN_NOTICE, &skt->dev, |
| 562 | "pccard: card ejected from slot %d\n", skt->sock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | socket_shutdown(skt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | } |
| 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 | */ |
| 577 | static 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 | |
| 595 | static int pccardd(void *__skt) |
| 596 | { |
| 597 | struct pcmcia_socket *skt = __skt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | int ret; |
| 599 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | 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-Hartman | 8737331 | 2006-09-12 17:00:10 +0200 | [diff] [blame] | 606 | ret = device_register(&skt->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | if (ret) { |
Dominik Brodowski | 2e55bf6 | 2008-08-02 18:08:38 +0200 | [diff] [blame] | 608 | dev_printk(KERN_WARNING, &skt->dev, |
| 609 | "PCMCIA: unable to register socket\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | skt->thread = NULL; |
Serge E. Hallyn | 1da9ab7 | 2006-06-14 08:01:26 -0500 | [diff] [blame] | 611 | complete(&skt->thread_done); |
| 612 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | } |
David Brownell | 4356d73 | 2008-04-28 01:03:20 -0700 | [diff] [blame] | 614 | ret = pccard_sysfs_add_socket(&skt->dev); |
| 615 | if (ret) |
| 616 | dev_warn(&skt->dev, "err %d adding socket attributes\n", ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | |
Daniel Ritz | bf4de6f | 2005-09-09 13:03:23 -0700 | [diff] [blame] | 618 | complete(&skt->thread_done); |
| 619 | |
Dominik Brodowski | d801c140 | 2010-02-17 11:02:22 +0100 | [diff] [blame] | 620 | /* wait for userspace to catch up */ |
| 621 | msleep(250); |
| 622 | |
Rafael J. Wysocki | 8314418 | 2007-07-17 04:03:35 -0700 | [diff] [blame] | 623 | set_freezable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | for (;;) { |
| 625 | unsigned long flags; |
| 626 | unsigned int events; |
Dominik Brodowski | f971dbd | 2010-01-17 18:13:31 +0100 | [diff] [blame] | 627 | unsigned int sysfs_events; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | spin_lock_irqsave(&skt->thread_lock, flags); |
| 630 | events = skt->thread_events; |
| 631 | skt->thread_events = 0; |
Dominik Brodowski | f971dbd | 2010-01-17 18:13:31 +0100 | [diff] [blame] | 632 | sysfs_events = skt->sysfs_events; |
| 633 | skt->sysfs_events = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | spin_unlock_irqrestore(&skt->thread_lock, flags); |
| 635 | |
Dominik Brodowski | f971dbd | 2010-01-17 18:13:31 +0100 | [diff] [blame] | 636 | mutex_lock(&skt->skt_mutex); |
Dominik Brodowski | 216d7cd | 2010-07-11 10:04:59 +0200 | [diff] [blame] | 637 | if (events & SS_DETECT) |
| 638 | socket_detect_change(skt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | |
Dominik Brodowski | f971dbd | 2010-01-17 18:13:31 +0100 | [diff] [blame] | 640 | if (sysfs_events) { |
| 641 | if (sysfs_events & PCMCIA_UEVENT_EJECT) |
| 642 | socket_remove(skt); |
| 643 | if (sysfs_events & PCMCIA_UEVENT_INSERT) |
| 644 | socket_insert(skt); |
Dominik Brodowski | f971dbd | 2010-01-17 18:13:31 +0100 | [diff] [blame] | 645 | if ((sysfs_events & PCMCIA_UEVENT_SUSPEND) && |
| 646 | !(skt->state & SOCKET_CARDBUS)) { |
| 647 | if (skt->callback) |
| 648 | ret = skt->callback->suspend(skt); |
| 649 | else |
| 650 | ret = 0; |
Dominik Brodowski | 4382124 | 2010-05-03 15:10:09 +0200 | [diff] [blame] | 651 | if (!ret) { |
Dominik Brodowski | f971dbd | 2010-01-17 18:13:31 +0100 | [diff] [blame] | 652 | socket_suspend(skt); |
Dominik Brodowski | 4382124 | 2010-05-03 15:10:09 +0200 | [diff] [blame] | 653 | msleep(100); |
| 654 | } |
| 655 | } |
| 656 | if ((sysfs_events & PCMCIA_UEVENT_RESUME) && |
| 657 | !(skt->state & SOCKET_CARDBUS)) { |
| 658 | ret = socket_resume(skt); |
| 659 | if (!ret && skt->callback) |
| 660 | skt->callback->resume(skt); |
Dominik Brodowski | f971dbd | 2010-01-17 18:13:31 +0100 | [diff] [blame] | 661 | } |
Dominik Brodowski | af461fc | 2010-01-17 19:30:53 +0100 | [diff] [blame] | 662 | if ((sysfs_events & PCMCIA_UEVENT_REQUERY) && |
| 663 | !(skt->state & SOCKET_CARDBUS)) { |
| 664 | if (!ret && skt->callback) |
| 665 | skt->callback->requery(skt); |
| 666 | } |
Dominik Brodowski | f971dbd | 2010-01-17 18:13:31 +0100 | [diff] [blame] | 667 | } |
| 668 | mutex_unlock(&skt->skt_mutex); |
| 669 | |
| 670 | if (events || sysfs_events) |
| 671 | continue; |
| 672 | |
Serge E. Hallyn | 1da9ab7 | 2006-06-14 08:01:26 -0500 | [diff] [blame] | 673 | if (kthread_should_stop()) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | break; |
Daniel Ritz | bf4de6f | 2005-09-09 13:03:23 -0700 | [diff] [blame] | 675 | |
Dmitry Eremin-Solenikov | d3046ba | 2015-03-30 14:33:45 +0300 | [diff] [blame^] | 676 | set_current_state(TASK_INTERRUPTIBLE); |
| 677 | |
Daniel Ritz | bf4de6f | 2005-09-09 13:03:23 -0700 | [diff] [blame] | 678 | schedule(); |
Dmitry Eremin-Solenikov | d3046ba | 2015-03-30 14:33:45 +0300 | [diff] [blame^] | 679 | |
| 680 | /* make sure we are running */ |
| 681 | __set_current_state(TASK_RUNNING); |
| 682 | |
Daniel Ritz | bf4de6f | 2005-09-09 13:03:23 -0700 | [diff] [blame] | 683 | try_to_freeze(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | } |
Steven Rostedt | 220ec02 | 2005-10-10 11:13:17 -0400 | [diff] [blame] | 685 | |
Dominik Brodowski | 3970dd8 | 2010-01-02 23:19:45 +0100 | [diff] [blame] | 686 | /* shut down socket, if a device is still present */ |
| 687 | if (skt->state & SOCKET_PRESENT) { |
| 688 | mutex_lock(&skt->skt_mutex); |
| 689 | socket_remove(skt); |
| 690 | mutex_unlock(&skt->skt_mutex); |
| 691 | } |
| 692 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | /* remove from the device core */ |
David Brownell | 4356d73 | 2008-04-28 01:03:20 -0700 | [diff] [blame] | 694 | pccard_sysfs_remove_socket(&skt->dev); |
Greg Kroah-Hartman | 8737331 | 2006-09-12 17:00:10 +0200 | [diff] [blame] | 695 | device_unregister(&skt->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | |
Serge E. Hallyn | 1da9ab7 | 2006-06-14 08:01:26 -0500 | [diff] [blame] | 697 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | /* |
| 701 | * Yenta (at least) probes interrupts before registering the socket and |
| 702 | * starting the handler thread. |
| 703 | */ |
| 704 | void pcmcia_parse_events(struct pcmcia_socket *s, u_int events) |
| 705 | { |
Arjan van de Ven | 4b7a89a | 2006-06-30 10:31:13 +0200 | [diff] [blame] | 706 | unsigned long flags; |
Dominik Brodowski | d50dbec | 2009-10-23 12:51:28 +0200 | [diff] [blame] | 707 | dev_dbg(&s->dev, "parse_events: events %08x\n", events); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | if (s->thread) { |
Arjan van de Ven | 4b7a89a | 2006-06-30 10:31:13 +0200 | [diff] [blame] | 709 | spin_lock_irqsave(&s->thread_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | s->thread_events |= events; |
Arjan van de Ven | 4b7a89a | 2006-06-30 10:31:13 +0200 | [diff] [blame] | 711 | spin_unlock_irqrestore(&s->thread_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | |
Christoph Hellwig | 0b40209 | 2007-04-22 13:55:36 +0100 | [diff] [blame] | 713 | wake_up_process(s->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | } |
| 715 | } /* pcmcia_parse_events */ |
Dominik Brodowski | 33519dd | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 716 | EXPORT_SYMBOL(pcmcia_parse_events); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | |
Dominik Brodowski | f971dbd | 2010-01-17 18:13:31 +0100 | [diff] [blame] | 718 | /** |
| 719 | * pcmcia_parse_uevents() - tell pccardd to issue manual commands |
| 720 | * @s: the PCMCIA socket we wan't to command |
| 721 | * @events: events to pass to pccardd |
| 722 | * |
| 723 | * userspace-issued insert, eject, suspend and resume commands must be |
| 724 | * handled by pccardd to avoid any sysfs-related deadlocks. Valid events |
| 725 | * are PCMCIA_UEVENT_EJECT (for eject), PCMCIA_UEVENT__INSERT (for insert), |
Dominik Brodowski | af461fc | 2010-01-17 19:30:53 +0100 | [diff] [blame] | 726 | * PCMCIA_UEVENT_RESUME (for resume), PCMCIA_UEVENT_SUSPEND (for suspend) |
| 727 | * and PCMCIA_UEVENT_REQUERY (for re-querying the PCMCIA card). |
Dominik Brodowski | f971dbd | 2010-01-17 18:13:31 +0100 | [diff] [blame] | 728 | */ |
| 729 | void pcmcia_parse_uevents(struct pcmcia_socket *s, u_int events) |
| 730 | { |
| 731 | unsigned long flags; |
| 732 | dev_dbg(&s->dev, "parse_uevents: events %08x\n", events); |
| 733 | if (s->thread) { |
| 734 | spin_lock_irqsave(&s->thread_lock, flags); |
| 735 | s->sysfs_events |= events; |
| 736 | spin_unlock_irqrestore(&s->thread_lock, flags); |
| 737 | |
| 738 | wake_up_process(s->thread); |
| 739 | } |
| 740 | } |
| 741 | EXPORT_SYMBOL(pcmcia_parse_uevents); |
| 742 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | |
| 744 | /* register pcmcia_callback */ |
| 745 | int pccard_register_pcmcia(struct pcmcia_socket *s, struct pcmcia_callback *c) |
| 746 | { |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 747 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | |
Dominik Brodowski | 7fe908d | 2006-01-10 21:20:36 +0100 | [diff] [blame] | 749 | /* s->skt_mutex also protects s->callback */ |
| 750 | mutex_lock(&s->skt_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | |
| 752 | if (c) { |
| 753 | /* registration */ |
| 754 | if (s->callback) { |
| 755 | ret = -EBUSY; |
| 756 | goto err; |
| 757 | } |
| 758 | |
| 759 | s->callback = c; |
| 760 | |
| 761 | if ((s->state & (SOCKET_PRESENT|SOCKET_CARDBUS)) == SOCKET_PRESENT) |
Dominik Brodowski | 7b24e79 | 2010-07-11 10:26:53 +0200 | [diff] [blame] | 762 | s->callback->add(s); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | } else |
| 764 | s->callback = NULL; |
| 765 | err: |
Dominik Brodowski | 7fe908d | 2006-01-10 21:20:36 +0100 | [diff] [blame] | 766 | mutex_unlock(&s->skt_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | |
| 768 | return ret; |
| 769 | } |
| 770 | EXPORT_SYMBOL(pccard_register_pcmcia); |
| 771 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | |
Dominik Brodowski | 33519dd | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 773 | /* I'm not sure which "reset" function this is supposed to use, |
| 774 | * but for now, it uses the low-level interface's reset, not the |
| 775 | * CIS register. |
| 776 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | |
Dominik Brodowski | 994917f | 2008-08-31 15:20:26 +0200 | [diff] [blame] | 778 | int pcmcia_reset_card(struct pcmcia_socket *skt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | { |
| 780 | int ret; |
Dominik Brodowski | 33519dd | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 781 | |
Dominik Brodowski | d50dbec | 2009-10-23 12:51:28 +0200 | [diff] [blame] | 782 | dev_dbg(&skt->dev, "resetting socket\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | |
Dominik Brodowski | 7fe908d | 2006-01-10 21:20:36 +0100 | [diff] [blame] | 784 | mutex_lock(&skt->skt_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | do { |
| 786 | if (!(skt->state & SOCKET_PRESENT)) { |
Dominik Brodowski | 6d9a299 | 2009-10-24 12:20:18 +0200 | [diff] [blame] | 787 | dev_dbg(&skt->dev, "can't reset, not present\n"); |
Dominik Brodowski | 3939c1ef | 2008-08-03 11:10:56 +0200 | [diff] [blame] | 788 | ret = -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | break; |
| 790 | } |
| 791 | if (skt->state & SOCKET_SUSPEND) { |
Dominik Brodowski | 6d9a299 | 2009-10-24 12:20:18 +0200 | [diff] [blame] | 792 | dev_dbg(&skt->dev, "can't reset, suspended\n"); |
Dominik Brodowski | f958095 | 2008-08-03 11:40:19 +0200 | [diff] [blame] | 793 | ret = -EBUSY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | break; |
| 795 | } |
| 796 | if (skt->state & SOCKET_CARDBUS) { |
Dominik Brodowski | 6d9a299 | 2009-10-24 12:20:18 +0200 | [diff] [blame] | 797 | dev_dbg(&skt->dev, "can't reset, is cardbus\n"); |
Dominik Brodowski | de6405e | 2008-08-03 10:47:59 +0200 | [diff] [blame] | 798 | ret = -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | break; |
| 800 | } |
| 801 | |
Dominik Brodowski | 216d7cd | 2010-07-11 10:04:59 +0200 | [diff] [blame] | 802 | if (skt->callback) |
| 803 | skt->callback->suspend(skt); |
| 804 | mutex_lock(&skt->ops_mutex); |
| 805 | ret = socket_reset(skt); |
| 806 | mutex_unlock(&skt->ops_mutex); |
| 807 | if ((ret == 0) && (skt->callback)) |
| 808 | skt->callback->resume(skt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | |
Dominik Brodowski | 4c89e88 | 2008-08-03 10:07:45 +0200 | [diff] [blame] | 810 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | } while (0); |
Dominik Brodowski | 7fe908d | 2006-01-10 21:20:36 +0100 | [diff] [blame] | 812 | mutex_unlock(&skt->skt_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | |
| 814 | return ret; |
| 815 | } /* reset_card */ |
Dominik Brodowski | 994917f | 2008-08-31 15:20:26 +0200 | [diff] [blame] | 816 | EXPORT_SYMBOL(pcmcia_reset_card); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 819 | static int pcmcia_socket_uevent(struct device *dev, |
| 820 | struct kobj_uevent_env *env) |
Dominik Brodowski | 3ee1393 | 2005-06-27 16:28:05 -0700 | [diff] [blame] | 821 | { |
| 822 | struct pcmcia_socket *s = container_of(dev, struct pcmcia_socket, dev); |
Dominik Brodowski | 3ee1393 | 2005-06-27 16:28:05 -0700 | [diff] [blame] | 823 | |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 824 | if (add_uevent_var(env, "SOCKET_NO=%u", s->sock)) |
Dominik Brodowski | 3ee1393 | 2005-06-27 16:28:05 -0700 | [diff] [blame] | 825 | return -ENOMEM; |
| 826 | |
Dominik Brodowski | 3ee1393 | 2005-06-27 16:28:05 -0700 | [diff] [blame] | 827 | return 0; |
| 828 | } |
| 829 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | |
Dominik Brodowski | 378a33a | 2005-06-27 16:28:54 -0700 | [diff] [blame] | 831 | static struct completion pcmcia_unload; |
| 832 | |
| 833 | static void pcmcia_release_socket_class(struct class *data) |
| 834 | { |
| 835 | complete(&pcmcia_unload); |
| 836 | } |
| 837 | |
| 838 | |
Dominik Brodowski | d7646f7 | 2010-03-15 21:46:34 +0100 | [diff] [blame] | 839 | #ifdef CONFIG_PM |
| 840 | |
| 841 | static int __pcmcia_pm_op(struct device *dev, |
| 842 | int (*callback) (struct pcmcia_socket *skt)) |
| 843 | { |
| 844 | struct pcmcia_socket *s = container_of(dev, struct pcmcia_socket, dev); |
| 845 | int ret; |
| 846 | |
| 847 | mutex_lock(&s->skt_mutex); |
| 848 | ret = callback(s); |
| 849 | mutex_unlock(&s->skt_mutex); |
| 850 | |
| 851 | return ret; |
| 852 | } |
| 853 | |
| 854 | static int pcmcia_socket_dev_suspend_noirq(struct device *dev) |
| 855 | { |
| 856 | return __pcmcia_pm_op(dev, socket_suspend); |
| 857 | } |
| 858 | |
| 859 | static int pcmcia_socket_dev_resume_noirq(struct device *dev) |
| 860 | { |
| 861 | return __pcmcia_pm_op(dev, socket_early_resume); |
| 862 | } |
| 863 | |
Maciej Żenczykowski | 3c7d2b7 | 2010-10-13 21:47:14 -0700 | [diff] [blame] | 864 | static int __used pcmcia_socket_dev_resume(struct device *dev) |
Dominik Brodowski | d7646f7 | 2010-03-15 21:46:34 +0100 | [diff] [blame] | 865 | { |
| 866 | return __pcmcia_pm_op(dev, socket_late_resume); |
| 867 | } |
| 868 | |
Christian Lamparter | 42b5dd5 | 2013-02-21 16:42:06 -0800 | [diff] [blame] | 869 | static void __used pcmcia_socket_dev_complete(struct device *dev) |
| 870 | { |
| 871 | WARN(__pcmcia_pm_op(dev, socket_complete_resume), |
| 872 | "failed to complete resume"); |
| 873 | } |
| 874 | |
Dominik Brodowski | d7646f7 | 2010-03-15 21:46:34 +0100 | [diff] [blame] | 875 | static const struct dev_pm_ops pcmcia_socket_pm_ops = { |
| 876 | /* dev_resume may be called with IRQs enabled */ |
| 877 | SET_SYSTEM_SLEEP_PM_OPS(NULL, |
| 878 | pcmcia_socket_dev_resume) |
| 879 | |
| 880 | /* late suspend must be called with IRQs disabled */ |
| 881 | .suspend_noirq = pcmcia_socket_dev_suspend_noirq, |
| 882 | .freeze_noirq = pcmcia_socket_dev_suspend_noirq, |
| 883 | .poweroff_noirq = pcmcia_socket_dev_suspend_noirq, |
| 884 | |
| 885 | /* early resume must be called with IRQs disabled */ |
| 886 | .resume_noirq = pcmcia_socket_dev_resume_noirq, |
| 887 | .thaw_noirq = pcmcia_socket_dev_resume_noirq, |
| 888 | .restore_noirq = pcmcia_socket_dev_resume_noirq, |
Christian Lamparter | 42b5dd5 | 2013-02-21 16:42:06 -0800 | [diff] [blame] | 889 | .complete = pcmcia_socket_dev_complete, |
Dominik Brodowski | d7646f7 | 2010-03-15 21:46:34 +0100 | [diff] [blame] | 890 | }; |
| 891 | |
| 892 | #define PCMCIA_SOCKET_CLASS_PM_OPS (&pcmcia_socket_pm_ops) |
| 893 | |
| 894 | #else /* CONFIG_PM */ |
| 895 | |
| 896 | #define PCMCIA_SOCKET_CLASS_PM_OPS NULL |
| 897 | |
| 898 | #endif /* CONFIG_PM */ |
| 899 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | struct class pcmcia_socket_class = { |
| 901 | .name = "pcmcia_socket", |
Greg Kroah-Hartman | 8737331 | 2006-09-12 17:00:10 +0200 | [diff] [blame] | 902 | .dev_uevent = pcmcia_socket_uevent, |
| 903 | .dev_release = pcmcia_release_socket, |
Dominik Brodowski | 378a33a | 2005-06-27 16:28:54 -0700 | [diff] [blame] | 904 | .class_release = pcmcia_release_socket_class, |
Dominik Brodowski | d7646f7 | 2010-03-15 21:46:34 +0100 | [diff] [blame] | 905 | .pm = PCMCIA_SOCKET_CLASS_PM_OPS, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | }; |
| 907 | EXPORT_SYMBOL(pcmcia_socket_class); |
| 908 | |
| 909 | |
| 910 | static int __init init_pcmcia_cs(void) |
| 911 | { |
Dominik Brodowski | 378a33a | 2005-06-27 16:28:54 -0700 | [diff] [blame] | 912 | init_completion(&pcmcia_unload); |
David Brownell | 4356d73 | 2008-04-28 01:03:20 -0700 | [diff] [blame] | 913 | return class_register(&pcmcia_socket_class); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | } |
| 915 | |
| 916 | static void __exit exit_pcmcia_cs(void) |
| 917 | { |
Dominik Brodowski | 33519dd | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 918 | class_unregister(&pcmcia_socket_class); |
Dominik Brodowski | 378a33a | 2005-06-27 16:28:54 -0700 | [diff] [blame] | 919 | wait_for_completion(&pcmcia_unload); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 920 | } |
| 921 | |
| 922 | subsys_initcall(init_pcmcia_cs); |
| 923 | module_exit(exit_pcmcia_cs); |
| 924 | |