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