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