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