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