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