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