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>
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/device.h>
30 #include <linux/kthread.h>
31 #include <linux/freezer.h>
32 #include <asm/system.h>
33 #include <asm/irq.h>
34 
35 #include <pcmcia/ss.h>
36 #include <pcmcia/cistpl.h>
37 #include <pcmcia/cisreg.h>
38 #include <pcmcia/ds.h>
39 #include "cs_internal.h"
40 
41 
42 /* Module parameters */
43 
44 MODULE_AUTHOR("David Hinds <dahinds@users.sourceforge.net>");
45 MODULE_DESCRIPTION("Linux Kernel Card Services");
46 MODULE_LICENSE("GPL");
47 
48 #define INT_MODULE_PARM(n, v) static int n = v; module_param(n, int, 0444)
49 
50 INT_MODULE_PARM(setup_delay,	10);		/* centiseconds */
51 INT_MODULE_PARM(resume_delay,	20);		/* centiseconds */
52 INT_MODULE_PARM(shutdown_delay,	3);		/* centiseconds */
53 INT_MODULE_PARM(vcc_settle,	40);		/* centiseconds */
54 INT_MODULE_PARM(reset_time,	10);		/* usecs */
55 INT_MODULE_PARM(unreset_delay,	10);		/* centiseconds */
56 INT_MODULE_PARM(unreset_check,	10);		/* centiseconds */
57 INT_MODULE_PARM(unreset_limit,	30);		/* unreset_check's */
58 
59 /* Access speed for attribute memory windows */
60 INT_MODULE_PARM(cis_speed,	300);		/* ns */
61 
62 
63 socket_state_t dead_socket = {
64 	.csc_mask	= SS_DETECT,
65 };
66 EXPORT_SYMBOL(dead_socket);
67 
68 
69 /* List of all sockets, protected by a rwsem */
70 LIST_HEAD(pcmcia_socket_list);
71 EXPORT_SYMBOL(pcmcia_socket_list);
72 
73 DECLARE_RWSEM(pcmcia_socket_list_rwsem);
74 EXPORT_SYMBOL(pcmcia_socket_list_rwsem);
75 
76 
pcmcia_get_socket(struct pcmcia_socket * skt)77 struct pcmcia_socket *pcmcia_get_socket(struct pcmcia_socket *skt)
78 {
79 	struct device *dev = get_device(&skt->dev);
80 	if (!dev)
81 		return NULL;
82 	return dev_get_drvdata(dev);
83 }
84 EXPORT_SYMBOL(pcmcia_get_socket);
85 
86 
pcmcia_put_socket(struct pcmcia_socket * skt)87 void pcmcia_put_socket(struct pcmcia_socket *skt)
88 {
89 	put_device(&skt->dev);
90 }
91 EXPORT_SYMBOL(pcmcia_put_socket);
92 
93 
pcmcia_release_socket(struct device * dev)94 static void pcmcia_release_socket(struct device *dev)
95 {
96 	struct pcmcia_socket *socket = dev_get_drvdata(dev);
97 
98 	complete(&socket->socket_released);
99 }
100 
101 static int pccardd(void *__skt);
102 
103 /**
104  * pcmcia_register_socket - add a new pcmcia socket device
105  * @socket: the &socket to register
106  */
pcmcia_register_socket(struct pcmcia_socket * socket)107 int pcmcia_register_socket(struct pcmcia_socket *socket)
108 {
109 	struct task_struct *tsk;
110 	int ret;
111 
112 	if (!socket || !socket->ops || !socket->dev.parent || !socket->resource_ops)
113 		return -EINVAL;
114 
115 	dev_dbg(&socket->dev, "pcmcia_register_socket(0x%p)\n", socket->ops);
116 
117 	/* try to obtain a socket number [yes, it gets ugly if we
118 	 * register more than 2^sizeof(unsigned int) pcmcia
119 	 * sockets... but the socket number is deprecated
120 	 * anyways, so I don't care] */
121 	down_write(&pcmcia_socket_list_rwsem);
122 	if (list_empty(&pcmcia_socket_list))
123 		socket->sock = 0;
124 	else {
125 		unsigned int found, i = 1;
126 		struct pcmcia_socket *tmp;
127 		do {
128 			found = 1;
129 			list_for_each_entry(tmp, &pcmcia_socket_list, socket_list) {
130 				if (tmp->sock == i)
131 					found = 0;
132 			}
133 			i++;
134 		} while (!found);
135 		socket->sock = i - 1;
136 	}
137 	list_add_tail(&socket->socket_list, &pcmcia_socket_list);
138 	up_write(&pcmcia_socket_list_rwsem);
139 
140 #ifndef CONFIG_CARDBUS
141 	/*
142 	 * If we do not support Cardbus, ensure that
143 	 * the Cardbus socket capability is disabled.
144 	 */
145 	socket->features &= ~SS_CAP_CARDBUS;
146 #endif
147 
148 	/* set proper values in socket->dev */
149 	dev_set_drvdata(&socket->dev, socket);
150 	socket->dev.class = &pcmcia_socket_class;
151 	dev_set_name(&socket->dev, "pcmcia_socket%u", socket->sock);
152 
153 	/* base address = 0, map = 0 */
154 	socket->cis_mem.flags = 0;
155 	socket->cis_mem.speed = cis_speed;
156 
157 	INIT_LIST_HEAD(&socket->cis_cache);
158 
159 	init_completion(&socket->socket_released);
160 	init_completion(&socket->thread_done);
161 	mutex_init(&socket->skt_mutex);
162 	mutex_init(&socket->ops_mutex);
163 	spin_lock_init(&socket->thread_lock);
164 
165 	if (socket->resource_ops->init) {
166 		mutex_lock(&socket->ops_mutex);
167 		ret = socket->resource_ops->init(socket);
168 		mutex_unlock(&socket->ops_mutex);
169 		if (ret)
170 			goto err;
171 	}
172 
173 	tsk = kthread_run(pccardd, socket, "pccardd");
174 	if (IS_ERR(tsk)) {
175 		ret = PTR_ERR(tsk);
176 		goto err;
177 	}
178 
179 	wait_for_completion(&socket->thread_done);
180 	if (!socket->thread) {
181 		dev_printk(KERN_WARNING, &socket->dev,
182 			   "PCMCIA: warning: socket thread did not start\n");
183 		return -EIO;
184 	}
185 
186 	pcmcia_parse_events(socket, SS_DETECT);
187 
188 	/*
189 	 * Let's try to get the PCMCIA module for 16-bit PCMCIA support.
190 	 * If it fails, it doesn't matter -- we still have 32-bit CardBus
191 	 * support to offer, so this is not a failure mode.
192 	 */
193 	request_module_nowait("pcmcia");
194 
195 	return 0;
196 
197  err:
198 	down_write(&pcmcia_socket_list_rwsem);
199 	list_del(&socket->socket_list);
200 	up_write(&pcmcia_socket_list_rwsem);
201 	return ret;
202 } /* pcmcia_register_socket */
203 EXPORT_SYMBOL(pcmcia_register_socket);
204 
205 
206 /**
207  * pcmcia_unregister_socket - remove a pcmcia socket device
208  * @socket: the &socket to unregister
209  */
pcmcia_unregister_socket(struct pcmcia_socket * socket)210 void pcmcia_unregister_socket(struct pcmcia_socket *socket)
211 {
212 	if (!socket)
213 		return;
214 
215 	dev_dbg(&socket->dev, "pcmcia_unregister_socket(0x%p)\n", socket->ops);
216 
217 	if (socket->thread)
218 		kthread_stop(socket->thread);
219 
220 	/* remove from our own list */
221 	down_write(&pcmcia_socket_list_rwsem);
222 	list_del(&socket->socket_list);
223 	up_write(&pcmcia_socket_list_rwsem);
224 
225 	/* wait for sysfs to drop all references */
226 	if (socket->resource_ops->exit) {
227 		mutex_lock(&socket->ops_mutex);
228 		socket->resource_ops->exit(socket);
229 		mutex_unlock(&socket->ops_mutex);
230 	}
231 	wait_for_completion(&socket->socket_released);
232 } /* pcmcia_unregister_socket */
233 EXPORT_SYMBOL(pcmcia_unregister_socket);
234 
235 
pcmcia_get_socket_by_nr(unsigned int nr)236 struct pcmcia_socket *pcmcia_get_socket_by_nr(unsigned int nr)
237 {
238 	struct pcmcia_socket *s;
239 
240 	down_read(&pcmcia_socket_list_rwsem);
241 	list_for_each_entry(s, &pcmcia_socket_list, socket_list)
242 		if (s->sock == nr) {
243 			up_read(&pcmcia_socket_list_rwsem);
244 			return s;
245 		}
246 	up_read(&pcmcia_socket_list_rwsem);
247 
248 	return NULL;
249 
250 }
251 EXPORT_SYMBOL(pcmcia_get_socket_by_nr);
252 
socket_reset(struct pcmcia_socket * skt)253 static int socket_reset(struct pcmcia_socket *skt)
254 {
255 	int status, i;
256 
257 	dev_dbg(&skt->dev, "reset\n");
258 
259 	skt->socket.flags |= SS_OUTPUT_ENA | SS_RESET;
260 	skt->ops->set_socket(skt, &skt->socket);
261 	udelay((long)reset_time);
262 
263 	skt->socket.flags &= ~SS_RESET;
264 	skt->ops->set_socket(skt, &skt->socket);
265 
266 	msleep(unreset_delay * 10);
267 	for (i = 0; i < unreset_limit; i++) {
268 		skt->ops->get_status(skt, &status);
269 
270 		if (!(status & SS_DETECT))
271 			return -ENODEV;
272 
273 		if (status & SS_READY)
274 			return 0;
275 
276 		msleep(unreset_check * 10);
277 	}
278 
279 	dev_printk(KERN_ERR, &skt->dev, "time out after reset.\n");
280 	return -ETIMEDOUT;
281 }
282 
283 /*
284  * socket_setup() and socket_shutdown() are called by the main event handler
285  * when card insertion and removal events are received.
286  * socket_setup() turns on socket power and resets the socket, in two stages.
287  * socket_shutdown() unconfigures a socket and turns off socket power.
288  */
socket_shutdown(struct pcmcia_socket * s)289 static void socket_shutdown(struct pcmcia_socket *s)
290 {
291 	int status;
292 
293 	dev_dbg(&s->dev, "shutdown\n");
294 
295 	if (s->callback)
296 		s->callback->remove(s);
297 
298 	mutex_lock(&s->ops_mutex);
299 	s->state &= SOCKET_INUSE | SOCKET_PRESENT;
300 	msleep(shutdown_delay * 10);
301 	s->state &= SOCKET_INUSE;
302 
303 	/* Blank out the socket state */
304 	s->socket = dead_socket;
305 	s->ops->init(s);
306 	s->ops->set_socket(s, &s->socket);
307 	s->lock_count = 0;
308 	kfree(s->fake_cis);
309 	s->fake_cis = NULL;
310 	s->functions = 0;
311 
312 	/* From here on we can be sure that only we (that is, the
313 	 * pccardd thread) accesses this socket, and all (16-bit)
314 	 * PCMCIA interactions are gone. Therefore, release
315 	 * ops_mutex so that we don't get a sysfs-related lockdep
316 	 * warning.
317 	 */
318 	mutex_unlock(&s->ops_mutex);
319 
320 #ifdef CONFIG_CARDBUS
321 	cb_free(s);
322 #endif
323 
324 	/* give socket some time to power down */
325 	msleep(100);
326 
327 	s->ops->get_status(s, &status);
328 	if (status & SS_POWERON) {
329 		dev_printk(KERN_ERR, &s->dev,
330 			   "*** DANGER *** unable to remove socket power\n");
331 	}
332 
333 	s->state &= ~SOCKET_INUSE;
334 }
335 
socket_setup(struct pcmcia_socket * skt,int initial_delay)336 static int socket_setup(struct pcmcia_socket *skt, int initial_delay)
337 {
338 	int status, i;
339 
340 	dev_dbg(&skt->dev, "setup\n");
341 
342 	skt->ops->get_status(skt, &status);
343 	if (!(status & SS_DETECT))
344 		return -ENODEV;
345 
346 	msleep(initial_delay * 10);
347 
348 	for (i = 0; i < 100; i++) {
349 		skt->ops->get_status(skt, &status);
350 		if (!(status & SS_DETECT))
351 			return -ENODEV;
352 
353 		if (!(status & SS_PENDING))
354 			break;
355 
356 		msleep(100);
357 	}
358 
359 	if (status & SS_PENDING) {
360 		dev_printk(KERN_ERR, &skt->dev,
361 			   "voltage interrogation timed out.\n");
362 		return -ETIMEDOUT;
363 	}
364 
365 	if (status & SS_CARDBUS) {
366 		if (!(skt->features & SS_CAP_CARDBUS)) {
367 			dev_printk(KERN_ERR, &skt->dev,
368 				"cardbus cards are not supported.\n");
369 			return -EINVAL;
370 		}
371 		skt->state |= SOCKET_CARDBUS;
372 	} else
373 		skt->state &= ~SOCKET_CARDBUS;
374 
375 	/*
376 	 * Decode the card voltage requirements, and apply power to the card.
377 	 */
378 	if (status & SS_3VCARD)
379 		skt->socket.Vcc = skt->socket.Vpp = 33;
380 	else if (!(status & SS_XVCARD))
381 		skt->socket.Vcc = skt->socket.Vpp = 50;
382 	else {
383 		dev_printk(KERN_ERR, &skt->dev, "unsupported voltage key.\n");
384 		return -EIO;
385 	}
386 
387 	if (skt->power_hook)
388 		skt->power_hook(skt, HOOK_POWER_PRE);
389 
390 	skt->socket.flags = 0;
391 	skt->ops->set_socket(skt, &skt->socket);
392 
393 	/*
394 	 * Wait "vcc_settle" for the supply to stabilise.
395 	 */
396 	msleep(vcc_settle * 10);
397 
398 	skt->ops->get_status(skt, &status);
399 	if (!(status & SS_POWERON)) {
400 		dev_printk(KERN_ERR, &skt->dev, "unable to apply power.\n");
401 		return -EIO;
402 	}
403 
404 	status = socket_reset(skt);
405 
406 	if (skt->power_hook)
407 		skt->power_hook(skt, HOOK_POWER_POST);
408 
409 	return status;
410 }
411 
412 /*
413  * Handle card insertion.  Setup the socket, reset the card,
414  * and then tell the rest of PCMCIA that a card is present.
415  */
socket_insert(struct pcmcia_socket * skt)416 static int socket_insert(struct pcmcia_socket *skt)
417 {
418 	int ret;
419 
420 	dev_dbg(&skt->dev, "insert\n");
421 
422 	mutex_lock(&skt->ops_mutex);
423 	if (skt->state & SOCKET_INUSE) {
424 		mutex_unlock(&skt->ops_mutex);
425 		return -EINVAL;
426 	}
427 	skt->state |= SOCKET_INUSE;
428 
429 	ret = socket_setup(skt, setup_delay);
430 	if (ret == 0) {
431 		skt->state |= SOCKET_PRESENT;
432 
433 		dev_printk(KERN_NOTICE, &skt->dev,
434 			   "pccard: %s card inserted into slot %d\n",
435 			   (skt->state & SOCKET_CARDBUS) ? "CardBus" : "PCMCIA",
436 			   skt->sock);
437 
438 #ifdef CONFIG_CARDBUS
439 		if (skt->state & SOCKET_CARDBUS) {
440 			cb_alloc(skt);
441 			skt->state |= SOCKET_CARDBUS_CONFIG;
442 		}
443 #endif
444 		dev_dbg(&skt->dev, "insert done\n");
445 		mutex_unlock(&skt->ops_mutex);
446 
447 		if (!(skt->state & SOCKET_CARDBUS) && (skt->callback))
448 			skt->callback->add(skt);
449 	} else {
450 		mutex_unlock(&skt->ops_mutex);
451 		socket_shutdown(skt);
452 	}
453 
454 	return ret;
455 }
456 
socket_suspend(struct pcmcia_socket * skt)457 static int socket_suspend(struct pcmcia_socket *skt)
458 {
459 	if (skt->state & SOCKET_SUSPEND)
460 		return -EBUSY;
461 
462 	mutex_lock(&skt->ops_mutex);
463 	skt->suspended_state = skt->state;
464 
465 	skt->socket = dead_socket;
466 	skt->ops->set_socket(skt, &skt->socket);
467 	if (skt->ops->suspend)
468 		skt->ops->suspend(skt);
469 	skt->state |= SOCKET_SUSPEND;
470 	mutex_unlock(&skt->ops_mutex);
471 	return 0;
472 }
473 
socket_early_resume(struct pcmcia_socket * skt)474 static int socket_early_resume(struct pcmcia_socket *skt)
475 {
476 	mutex_lock(&skt->ops_mutex);
477 	skt->socket = dead_socket;
478 	skt->ops->init(skt);
479 	skt->ops->set_socket(skt, &skt->socket);
480 	if (skt->state & SOCKET_PRESENT)
481 		skt->resume_status = socket_setup(skt, resume_delay);
482 	mutex_unlock(&skt->ops_mutex);
483 	return 0;
484 }
485 
socket_late_resume(struct pcmcia_socket * skt)486 static int socket_late_resume(struct pcmcia_socket *skt)
487 {
488 	int ret;
489 
490 	mutex_lock(&skt->ops_mutex);
491 	skt->state &= ~SOCKET_SUSPEND;
492 	mutex_unlock(&skt->ops_mutex);
493 
494 	if (!(skt->state & SOCKET_PRESENT)) {
495 		ret = socket_insert(skt);
496 		if (ret == -ENODEV)
497 			ret = 0;
498 		return ret;
499 	}
500 
501 	if (skt->resume_status) {
502 		socket_shutdown(skt);
503 		return 0;
504 	}
505 
506 	if (skt->suspended_state != skt->state) {
507 		dev_dbg(&skt->dev,
508 			"suspend state 0x%x != resume state 0x%x\n",
509 			skt->suspended_state, skt->state);
510 
511 		socket_shutdown(skt);
512 		return socket_insert(skt);
513 	}
514 
515 #ifdef CONFIG_CARDBUS
516 	if (skt->state & SOCKET_CARDBUS) {
517 		/* We can't be sure the CardBus card is the same
518 		 * as the one previously inserted. Therefore, remove
519 		 * and re-add... */
520 		cb_free(skt);
521 		cb_alloc(skt);
522 		return 0;
523 	}
524 #endif
525 	if (!(skt->state & SOCKET_CARDBUS) && (skt->callback))
526 		skt->callback->early_resume(skt);
527 	return 0;
528 }
529 
530 /*
531  * Resume a socket.  If a card is present, verify its CIS against
532  * our cached copy.  If they are different, the card has been
533  * replaced, and we need to tell the drivers.
534  */
socket_resume(struct pcmcia_socket * skt)535 static int socket_resume(struct pcmcia_socket *skt)
536 {
537 	if (!(skt->state & SOCKET_SUSPEND))
538 		return -EBUSY;
539 
540 	socket_early_resume(skt);
541 	return socket_late_resume(skt);
542 }
543 
socket_remove(struct pcmcia_socket * skt)544 static void socket_remove(struct pcmcia_socket *skt)
545 {
546 	dev_printk(KERN_NOTICE, &skt->dev,
547 		   "pccard: card ejected from slot %d\n", skt->sock);
548 	socket_shutdown(skt);
549 }
550 
551 /*
552  * Process a socket card detect status change.
553  *
554  * If we don't have a card already present, delay the detect event for
555  * about 20ms (to be on the safe side) before reading the socket status.
556  *
557  * Some i82365-based systems send multiple SS_DETECT events during card
558  * insertion, and the "card present" status bit seems to bounce.  This
559  * will probably be true with GPIO-based card detection systems after
560  * the product has aged.
561  */
socket_detect_change(struct pcmcia_socket * skt)562 static void socket_detect_change(struct pcmcia_socket *skt)
563 {
564 	if (!(skt->state & SOCKET_SUSPEND)) {
565 		int status;
566 
567 		if (!(skt->state & SOCKET_PRESENT))
568 			msleep(20);
569 
570 		skt->ops->get_status(skt, &status);
571 		if ((skt->state & SOCKET_PRESENT) &&
572 		     !(status & SS_DETECT))
573 			socket_remove(skt);
574 		if (!(skt->state & SOCKET_PRESENT) &&
575 		    (status & SS_DETECT))
576 			socket_insert(skt);
577 	}
578 }
579 
pccardd(void * __skt)580 static int pccardd(void *__skt)
581 {
582 	struct pcmcia_socket *skt = __skt;
583 	int ret;
584 
585 	skt->thread = current;
586 	skt->socket = dead_socket;
587 	skt->ops->init(skt);
588 	skt->ops->set_socket(skt, &skt->socket);
589 
590 	/* register with the device core */
591 	ret = device_register(&skt->dev);
592 	if (ret) {
593 		dev_printk(KERN_WARNING, &skt->dev,
594 			   "PCMCIA: unable to register socket\n");
595 		skt->thread = NULL;
596 		complete(&skt->thread_done);
597 		return 0;
598 	}
599 	ret = pccard_sysfs_add_socket(&skt->dev);
600 	if (ret)
601 		dev_warn(&skt->dev, "err %d adding socket attributes\n", ret);
602 
603 	complete(&skt->thread_done);
604 
605 	/* wait for userspace to catch up */
606 	msleep(250);
607 
608 	set_freezable();
609 	for (;;) {
610 		unsigned long flags;
611 		unsigned int events;
612 		unsigned int sysfs_events;
613 
614 		set_current_state(TASK_INTERRUPTIBLE);
615 
616 		spin_lock_irqsave(&skt->thread_lock, flags);
617 		events = skt->thread_events;
618 		skt->thread_events = 0;
619 		sysfs_events = skt->sysfs_events;
620 		skt->sysfs_events = 0;
621 		spin_unlock_irqrestore(&skt->thread_lock, flags);
622 
623 		mutex_lock(&skt->skt_mutex);
624 		if (events & SS_DETECT)
625 			socket_detect_change(skt);
626 
627 		if (sysfs_events) {
628 			if (sysfs_events & PCMCIA_UEVENT_EJECT)
629 				socket_remove(skt);
630 			if (sysfs_events & PCMCIA_UEVENT_INSERT)
631 				socket_insert(skt);
632 			if ((sysfs_events & PCMCIA_UEVENT_SUSPEND) &&
633 				!(skt->state & SOCKET_CARDBUS)) {
634 				if (skt->callback)
635 					ret = skt->callback->suspend(skt);
636 				else
637 					ret = 0;
638 				if (!ret) {
639 					socket_suspend(skt);
640 					msleep(100);
641 				}
642 			}
643 			if ((sysfs_events & PCMCIA_UEVENT_RESUME) &&
644 				!(skt->state & SOCKET_CARDBUS)) {
645 				ret = socket_resume(skt);
646 				if (!ret && skt->callback)
647 					skt->callback->resume(skt);
648 			}
649 			if ((sysfs_events & PCMCIA_UEVENT_REQUERY) &&
650 				!(skt->state & SOCKET_CARDBUS)) {
651 				if (!ret && skt->callback)
652 					skt->callback->requery(skt);
653 			}
654 		}
655 		mutex_unlock(&skt->skt_mutex);
656 
657 		if (events || sysfs_events)
658 			continue;
659 
660 		if (kthread_should_stop())
661 			break;
662 
663 		schedule();
664 		try_to_freeze();
665 	}
666 	/* make sure we are running before we exit */
667 	set_current_state(TASK_RUNNING);
668 
669 	/* shut down socket, if a device is still present */
670 	if (skt->state & SOCKET_PRESENT) {
671 		mutex_lock(&skt->skt_mutex);
672 		socket_remove(skt);
673 		mutex_unlock(&skt->skt_mutex);
674 	}
675 
676 	/* remove from the device core */
677 	pccard_sysfs_remove_socket(&skt->dev);
678 	device_unregister(&skt->dev);
679 
680 	return 0;
681 }
682 
683 /*
684  * Yenta (at least) probes interrupts before registering the socket and
685  * starting the handler thread.
686  */
pcmcia_parse_events(struct pcmcia_socket * s,u_int events)687 void pcmcia_parse_events(struct pcmcia_socket *s, u_int events)
688 {
689 	unsigned long flags;
690 	dev_dbg(&s->dev, "parse_events: events %08x\n", events);
691 	if (s->thread) {
692 		spin_lock_irqsave(&s->thread_lock, flags);
693 		s->thread_events |= events;
694 		spin_unlock_irqrestore(&s->thread_lock, flags);
695 
696 		wake_up_process(s->thread);
697 	}
698 } /* pcmcia_parse_events */
699 EXPORT_SYMBOL(pcmcia_parse_events);
700 
701 /**
702  * pcmcia_parse_uevents() - tell pccardd to issue manual commands
703  * @s:		the PCMCIA socket we wan't to command
704  * @events:	events to pass to pccardd
705  *
706  * userspace-issued insert, eject, suspend and resume commands must be
707  * handled by pccardd to avoid any sysfs-related deadlocks. Valid events
708  * are PCMCIA_UEVENT_EJECT (for eject), PCMCIA_UEVENT__INSERT (for insert),
709  * PCMCIA_UEVENT_RESUME (for resume), PCMCIA_UEVENT_SUSPEND (for suspend)
710  * and PCMCIA_UEVENT_REQUERY (for re-querying the PCMCIA card).
711  */
pcmcia_parse_uevents(struct pcmcia_socket * s,u_int events)712 void pcmcia_parse_uevents(struct pcmcia_socket *s, u_int events)
713 {
714 	unsigned long flags;
715 	dev_dbg(&s->dev, "parse_uevents: events %08x\n", events);
716 	if (s->thread) {
717 		spin_lock_irqsave(&s->thread_lock, flags);
718 		s->sysfs_events |= events;
719 		spin_unlock_irqrestore(&s->thread_lock, flags);
720 
721 		wake_up_process(s->thread);
722 	}
723 }
724 EXPORT_SYMBOL(pcmcia_parse_uevents);
725 
726 
727 /* register pcmcia_callback */
pccard_register_pcmcia(struct pcmcia_socket * s,struct pcmcia_callback * c)728 int pccard_register_pcmcia(struct pcmcia_socket *s, struct pcmcia_callback *c)
729 {
730 	int ret = 0;
731 
732 	/* s->skt_mutex also protects s->callback */
733 	mutex_lock(&s->skt_mutex);
734 
735 	if (c) {
736 		/* registration */
737 		if (s->callback) {
738 			ret = -EBUSY;
739 			goto err;
740 		}
741 
742 		s->callback = c;
743 
744 		if ((s->state & (SOCKET_PRESENT|SOCKET_CARDBUS)) == SOCKET_PRESENT)
745 			s->callback->add(s);
746 	} else
747 		s->callback = NULL;
748  err:
749 	mutex_unlock(&s->skt_mutex);
750 
751 	return ret;
752 }
753 EXPORT_SYMBOL(pccard_register_pcmcia);
754 
755 
756 /* I'm not sure which "reset" function this is supposed to use,
757  * but for now, it uses the low-level interface's reset, not the
758  * CIS register.
759  */
760 
pcmcia_reset_card(struct pcmcia_socket * skt)761 int pcmcia_reset_card(struct pcmcia_socket *skt)
762 {
763 	int ret;
764 
765 	dev_dbg(&skt->dev, "resetting socket\n");
766 
767 	mutex_lock(&skt->skt_mutex);
768 	do {
769 		if (!(skt->state & SOCKET_PRESENT)) {
770 			dev_dbg(&skt->dev, "can't reset, not present\n");
771 			ret = -ENODEV;
772 			break;
773 		}
774 		if (skt->state & SOCKET_SUSPEND) {
775 			dev_dbg(&skt->dev, "can't reset, suspended\n");
776 			ret = -EBUSY;
777 			break;
778 		}
779 		if (skt->state & SOCKET_CARDBUS) {
780 			dev_dbg(&skt->dev, "can't reset, is cardbus\n");
781 			ret = -EPERM;
782 			break;
783 		}
784 
785 		if (skt->callback)
786 			skt->callback->suspend(skt);
787 		mutex_lock(&skt->ops_mutex);
788 		ret = socket_reset(skt);
789 		mutex_unlock(&skt->ops_mutex);
790 		if ((ret == 0) && (skt->callback))
791 			skt->callback->resume(skt);
792 
793 		ret = 0;
794 	} while (0);
795 	mutex_unlock(&skt->skt_mutex);
796 
797 	return ret;
798 } /* reset_card */
799 EXPORT_SYMBOL(pcmcia_reset_card);
800 
801 
pcmcia_socket_uevent(struct device * dev,struct kobj_uevent_env * env)802 static int pcmcia_socket_uevent(struct device *dev,
803 				struct kobj_uevent_env *env)
804 {
805 	struct pcmcia_socket *s = container_of(dev, struct pcmcia_socket, dev);
806 
807 	if (add_uevent_var(env, "SOCKET_NO=%u", s->sock))
808 		return -ENOMEM;
809 
810 	return 0;
811 }
812 
813 
814 static struct completion pcmcia_unload;
815 
pcmcia_release_socket_class(struct class * data)816 static void pcmcia_release_socket_class(struct class *data)
817 {
818 	complete(&pcmcia_unload);
819 }
820 
821 
822 #ifdef CONFIG_PM
823 
__pcmcia_pm_op(struct device * dev,int (* callback)(struct pcmcia_socket * skt))824 static int __pcmcia_pm_op(struct device *dev,
825 			  int (*callback) (struct pcmcia_socket *skt))
826 {
827 	struct pcmcia_socket *s = container_of(dev, struct pcmcia_socket, dev);
828 	int ret;
829 
830 	mutex_lock(&s->skt_mutex);
831 	ret = callback(s);
832 	mutex_unlock(&s->skt_mutex);
833 
834 	return ret;
835 }
836 
pcmcia_socket_dev_suspend_noirq(struct device * dev)837 static int pcmcia_socket_dev_suspend_noirq(struct device *dev)
838 {
839 	return __pcmcia_pm_op(dev, socket_suspend);
840 }
841 
pcmcia_socket_dev_resume_noirq(struct device * dev)842 static int pcmcia_socket_dev_resume_noirq(struct device *dev)
843 {
844 	return __pcmcia_pm_op(dev, socket_early_resume);
845 }
846 
pcmcia_socket_dev_resume(struct device * dev)847 static int __used pcmcia_socket_dev_resume(struct device *dev)
848 {
849 	return __pcmcia_pm_op(dev, socket_late_resume);
850 }
851 
852 static const struct dev_pm_ops pcmcia_socket_pm_ops = {
853 	/* dev_resume may be called with IRQs enabled */
854 	SET_SYSTEM_SLEEP_PM_OPS(NULL,
855 				pcmcia_socket_dev_resume)
856 
857 	/* late suspend must be called with IRQs disabled */
858 	.suspend_noirq = pcmcia_socket_dev_suspend_noirq,
859 	.freeze_noirq = pcmcia_socket_dev_suspend_noirq,
860 	.poweroff_noirq = pcmcia_socket_dev_suspend_noirq,
861 
862 	/* early resume must be called with IRQs disabled */
863 	.resume_noirq = pcmcia_socket_dev_resume_noirq,
864 	.thaw_noirq = pcmcia_socket_dev_resume_noirq,
865 	.restore_noirq = pcmcia_socket_dev_resume_noirq,
866 };
867 
868 #define PCMCIA_SOCKET_CLASS_PM_OPS (&pcmcia_socket_pm_ops)
869 
870 #else /* CONFIG_PM */
871 
872 #define PCMCIA_SOCKET_CLASS_PM_OPS NULL
873 
874 #endif /* CONFIG_PM */
875 
876 struct class pcmcia_socket_class = {
877 	.name = "pcmcia_socket",
878 	.dev_uevent = pcmcia_socket_uevent,
879 	.dev_release = pcmcia_release_socket,
880 	.class_release = pcmcia_release_socket_class,
881 	.pm = PCMCIA_SOCKET_CLASS_PM_OPS,
882 };
883 EXPORT_SYMBOL(pcmcia_socket_class);
884 
885 
init_pcmcia_cs(void)886 static int __init init_pcmcia_cs(void)
887 {
888 	init_completion(&pcmcia_unload);
889 	return class_register(&pcmcia_socket_class);
890 }
891 
exit_pcmcia_cs(void)892 static void __exit exit_pcmcia_cs(void)
893 {
894 	class_unregister(&pcmcia_socket_class);
895 	wait_for_completion(&pcmcia_unload);
896 }
897 
898 subsys_initcall(init_pcmcia_cs);
899 module_exit(exit_pcmcia_cs);
900 
901