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