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