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