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