xref: /openbmc/linux/drivers/macintosh/adb.c (revision 1da177e4)
1 /*
2  * Device driver for the Apple Desktop Bus
3  * and the /dev/adb device on macintoshes.
4  *
5  * Copyright (C) 1996 Paul Mackerras.
6  *
7  * Modified to declare controllers as structures, added
8  * client notification of bus reset and handles PowerBook
9  * sleep, by Benjamin Herrenschmidt.
10  *
11  * To do:
12  *
13  * - /sys/bus/adb to list the devices and infos
14  * - more /dev/adb to allow userland to receive the
15  *   flow of auto-polling datas from a given device.
16  * - move bus probe to a kernel thread
17  */
18 
19 #include <linux/config.h>
20 #include <linux/types.h>
21 #include <linux/errno.h>
22 #include <linux/kernel.h>
23 #include <linux/slab.h>
24 #include <linux/module.h>
25 #include <linux/fs.h>
26 #include <linux/mm.h>
27 #include <linux/sched.h>
28 #include <linux/smp_lock.h>
29 #include <linux/adb.h>
30 #include <linux/cuda.h>
31 #include <linux/pmu.h>
32 #include <linux/notifier.h>
33 #include <linux/wait.h>
34 #include <linux/init.h>
35 #include <linux/delay.h>
36 #include <linux/spinlock.h>
37 #include <linux/completion.h>
38 #include <linux/device.h>
39 #include <linux/devfs_fs_kernel.h>
40 
41 #include <asm/uaccess.h>
42 #include <asm/semaphore.h>
43 #ifdef CONFIG_PPC
44 #include <asm/prom.h>
45 #endif
46 
47 
48 EXPORT_SYMBOL(adb_controller);
49 EXPORT_SYMBOL(adb_client_list);
50 
51 extern struct adb_driver via_macii_driver;
52 extern struct adb_driver via_maciisi_driver;
53 extern struct adb_driver via_cuda_driver;
54 extern struct adb_driver adb_iop_driver;
55 extern struct adb_driver via_pmu_driver;
56 extern struct adb_driver macio_adb_driver;
57 
58 static struct adb_driver *adb_driver_list[] = {
59 #ifdef CONFIG_ADB_MACII
60 	&via_macii_driver,
61 #endif
62 #ifdef CONFIG_ADB_MACIISI
63 	&via_maciisi_driver,
64 #endif
65 #ifdef CONFIG_ADB_CUDA
66 	&via_cuda_driver,
67 #endif
68 #ifdef CONFIG_ADB_IOP
69 	&adb_iop_driver,
70 #endif
71 #if defined(CONFIG_ADB_PMU) || defined(CONFIG_ADB_PMU68K)
72 	&via_pmu_driver,
73 #endif
74 #ifdef CONFIG_ADB_MACIO
75 	&macio_adb_driver,
76 #endif
77 	NULL
78 };
79 
80 static struct class_simple *adb_dev_class;
81 
82 struct adb_driver *adb_controller;
83 struct notifier_block *adb_client_list = NULL;
84 static int adb_got_sleep;
85 static int adb_inited;
86 static pid_t adb_probe_task_pid;
87 static DECLARE_MUTEX(adb_probe_mutex);
88 static struct completion adb_probe_task_comp;
89 static int sleepy_trackpad;
90 static int autopoll_devs;
91 int __adb_probe_sync;
92 
93 #ifdef CONFIG_PMAC_PBOOK
94 static int adb_notify_sleep(struct pmu_sleep_notifier *self, int when);
95 static struct pmu_sleep_notifier adb_sleep_notifier = {
96 	adb_notify_sleep,
97 	SLEEP_LEVEL_ADB,
98 };
99 #endif
100 
101 static int adb_scan_bus(void);
102 static int do_adb_reset_bus(void);
103 static void adbdev_init(void);
104 static int try_handler_change(int, int);
105 
106 static struct adb_handler {
107 	void (*handler)(unsigned char *, int, struct pt_regs *, int);
108 	int original_address;
109 	int handler_id;
110 	int busy;
111 } adb_handler[16];
112 
113 /*
114  * The adb_handler_sem mutex protects all accesses to the original_address
115  * and handler_id fields of adb_handler[i] for all i, and changes to the
116  * handler field.
117  * Accesses to the handler field are protected by the adb_handler_lock
118  * rwlock.  It is held across all calls to any handler, so that by the
119  * time adb_unregister returns, we know that the old handler isn't being
120  * called.
121  */
122 static DECLARE_MUTEX(adb_handler_sem);
123 static DEFINE_RWLOCK(adb_handler_lock);
124 
125 #if 0
126 static void printADBreply(struct adb_request *req)
127 {
128         int i;
129 
130         printk("adb reply (%d)", req->reply_len);
131         for(i = 0; i < req->reply_len; i++)
132                 printk(" %x", req->reply[i]);
133         printk("\n");
134 
135 }
136 #endif
137 
138 
139 static __inline__ void adb_wait_ms(unsigned int ms)
140 {
141 	if (current->pid && adb_probe_task_pid &&
142 	  adb_probe_task_pid == current->pid)
143 		msleep(ms);
144 	else
145 		mdelay(ms);
146 }
147 
148 static int adb_scan_bus(void)
149 {
150 	int i, highFree=0, noMovement;
151 	int devmask = 0;
152 	struct adb_request req;
153 
154 	/* assumes adb_handler[] is all zeroes at this point */
155 	for (i = 1; i < 16; i++) {
156 		/* see if there is anything at address i */
157 		adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
158                             (i << 4) | 0xf);
159 		if (req.reply_len > 1)
160 			/* one or more devices at this address */
161 			adb_handler[i].original_address = i;
162 		else if (i > highFree)
163 			highFree = i;
164 	}
165 
166 	/* Note we reset noMovement to 0 each time we move a device */
167 	for (noMovement = 1; noMovement < 2 && highFree > 0; noMovement++) {
168 		for (i = 1; i < 16; i++) {
169 			if (adb_handler[i].original_address == 0)
170 				continue;
171 			/*
172 			 * Send a "talk register 3" command to address i
173 			 * to provoke a collision if there is more than
174 			 * one device at this address.
175 			 */
176 			adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
177 				    (i << 4) | 0xf);
178 			/*
179 			 * Move the device(s) which didn't detect a
180 			 * collision to address `highFree'.  Hopefully
181 			 * this only moves one device.
182 			 */
183 			adb_request(&req, NULL, ADBREQ_SYNC, 3,
184 				    (i<< 4) | 0xb, (highFree | 0x60), 0xfe);
185 			/*
186 			 * See if anybody actually moved. This is suggested
187 			 * by HW TechNote 01:
188 			 *
189 			 * http://developer.apple.com/technotes/hw/hw_01.html
190 			 */
191 			adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
192 				    (highFree << 4) | 0xf);
193 			if (req.reply_len <= 1) continue;
194 			/*
195 			 * Test whether there are any device(s) left
196 			 * at address i.
197 			 */
198 			adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
199 				    (i << 4) | 0xf);
200 			if (req.reply_len > 1) {
201 				/*
202 				 * There are still one or more devices
203 				 * left at address i.  Register the one(s)
204 				 * we moved to `highFree', and find a new
205 				 * value for highFree.
206 				 */
207 				adb_handler[highFree].original_address =
208 					adb_handler[i].original_address;
209 				while (highFree > 0 &&
210 				       adb_handler[highFree].original_address)
211 					highFree--;
212 				if (highFree <= 0)
213 					break;
214 
215 				noMovement = 0;
216 			}
217 			else {
218 				/*
219 				 * No devices left at address i; move the
220 				 * one(s) we moved to `highFree' back to i.
221 				 */
222 				adb_request(&req, NULL, ADBREQ_SYNC, 3,
223 					    (highFree << 4) | 0xb,
224 					    (i | 0x60), 0xfe);
225 			}
226 		}
227 	}
228 
229 	/* Now fill in the handler_id field of the adb_handler entries. */
230 	printk(KERN_DEBUG "adb devices:");
231 	for (i = 1; i < 16; i++) {
232 		if (adb_handler[i].original_address == 0)
233 			continue;
234 		adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
235 			    (i << 4) | 0xf);
236 		adb_handler[i].handler_id = req.reply[2];
237 		printk(" [%d]: %d %x", i, adb_handler[i].original_address,
238 		       adb_handler[i].handler_id);
239 		devmask |= 1 << i;
240 	}
241 	printk("\n");
242 	return devmask;
243 }
244 
245 /*
246  * This kernel task handles ADB probing. It dies once probing is
247  * completed.
248  */
249 static int
250 adb_probe_task(void *x)
251 {
252 	sigset_t blocked;
253 
254 	strcpy(current->comm, "kadbprobe");
255 
256 	sigfillset(&blocked);
257 	sigprocmask(SIG_BLOCK, &blocked, NULL);
258 	flush_signals(current);
259 
260 	printk(KERN_INFO "adb: starting probe task...\n");
261 	do_adb_reset_bus();
262 	printk(KERN_INFO "adb: finished probe task...\n");
263 
264 	adb_probe_task_pid = 0;
265 	up(&adb_probe_mutex);
266 
267 	return 0;
268 }
269 
270 static void
271 __adb_probe_task(void *data)
272 {
273 	adb_probe_task_pid = kernel_thread(adb_probe_task, NULL, SIGCHLD | CLONE_KERNEL);
274 }
275 
276 static DECLARE_WORK(adb_reset_work, __adb_probe_task, NULL);
277 
278 int
279 adb_reset_bus(void)
280 {
281 	if (__adb_probe_sync) {
282 		do_adb_reset_bus();
283 		return 0;
284 	}
285 
286 	down(&adb_probe_mutex);
287 	schedule_work(&adb_reset_work);
288 	return 0;
289 }
290 
291 int __init adb_init(void)
292 {
293 	struct adb_driver *driver;
294 	int i;
295 
296 #ifdef CONFIG_PPC32
297 	if ( (_machine != _MACH_chrp) && (_machine != _MACH_Pmac) )
298 		return 0;
299 #endif
300 #ifdef CONFIG_MAC
301 	if (!MACH_IS_MAC)
302 		return 0;
303 #endif
304 
305 	/* xmon may do early-init */
306 	if (adb_inited)
307 		return 0;
308 	adb_inited = 1;
309 
310 	adb_controller = NULL;
311 
312 	i = 0;
313 	while ((driver = adb_driver_list[i++]) != NULL) {
314 		if (!driver->probe()) {
315 			adb_controller = driver;
316 			break;
317 		}
318 	}
319 	if ((adb_controller == NULL) || adb_controller->init()) {
320 		printk(KERN_WARNING "Warning: no ADB interface detected\n");
321 		adb_controller = NULL;
322 	} else {
323 #ifdef CONFIG_PMAC_PBOOK
324 		pmu_register_sleep_notifier(&adb_sleep_notifier);
325 #endif /* CONFIG_PMAC_PBOOK */
326 #ifdef CONFIG_PPC
327 		if (machine_is_compatible("AAPL,PowerBook1998") ||
328 			machine_is_compatible("PowerBook1,1"))
329 			sleepy_trackpad = 1;
330 #endif /* CONFIG_PPC */
331 		init_completion(&adb_probe_task_comp);
332 		adbdev_init();
333 		adb_reset_bus();
334 	}
335 	return 0;
336 }
337 
338 __initcall(adb_init);
339 
340 #ifdef CONFIG_PMAC_PBOOK
341 /*
342  * notify clients before sleep and reset bus afterwards
343  */
344 int
345 adb_notify_sleep(struct pmu_sleep_notifier *self, int when)
346 {
347 	int ret;
348 
349 	switch (when) {
350 	case PBOOK_SLEEP_REQUEST:
351 		adb_got_sleep = 1;
352 		/* We need to get a lock on the probe thread */
353 		down(&adb_probe_mutex);
354 		/* Stop autopoll */
355 		if (adb_controller->autopoll)
356 			adb_controller->autopoll(0);
357 		ret = notifier_call_chain(&adb_client_list, ADB_MSG_POWERDOWN, NULL);
358 		if (ret & NOTIFY_STOP_MASK) {
359 			up(&adb_probe_mutex);
360 			return PBOOK_SLEEP_REFUSE;
361 		}
362 		break;
363 	case PBOOK_SLEEP_REJECT:
364 		if (adb_got_sleep) {
365 			adb_got_sleep = 0;
366 			up(&adb_probe_mutex);
367 			adb_reset_bus();
368 		}
369 		break;
370 
371 	case PBOOK_SLEEP_NOW:
372 		break;
373 	case PBOOK_WAKE:
374 		adb_got_sleep = 0;
375 		up(&adb_probe_mutex);
376 		adb_reset_bus();
377 		break;
378 	}
379 	return PBOOK_SLEEP_OK;
380 }
381 #endif /* CONFIG_PMAC_PBOOK */
382 
383 static int
384 do_adb_reset_bus(void)
385 {
386 	int ret, nret;
387 
388 	if (adb_controller == NULL)
389 		return -ENXIO;
390 
391 	if (adb_controller->autopoll)
392 		adb_controller->autopoll(0);
393 
394 	nret = notifier_call_chain(&adb_client_list, ADB_MSG_PRE_RESET, NULL);
395 	if (nret & NOTIFY_STOP_MASK) {
396 		if (adb_controller->autopoll)
397 			adb_controller->autopoll(autopoll_devs);
398 		return -EBUSY;
399 	}
400 
401 	if (sleepy_trackpad) {
402 		/* Let the trackpad settle down */
403 		adb_wait_ms(500);
404 	}
405 
406 	down(&adb_handler_sem);
407 	write_lock_irq(&adb_handler_lock);
408 	memset(adb_handler, 0, sizeof(adb_handler));
409 	write_unlock_irq(&adb_handler_lock);
410 
411 	/* That one is still a bit synchronous, oh well... */
412 	if (adb_controller->reset_bus)
413 		ret = adb_controller->reset_bus();
414 	else
415 		ret = 0;
416 
417 	if (sleepy_trackpad) {
418 		/* Let the trackpad settle down */
419 		adb_wait_ms(1500);
420 	}
421 
422 	if (!ret) {
423 		autopoll_devs = adb_scan_bus();
424 		if (adb_controller->autopoll)
425 			adb_controller->autopoll(autopoll_devs);
426 	}
427 	up(&adb_handler_sem);
428 
429 	nret = notifier_call_chain(&adb_client_list, ADB_MSG_POST_RESET, NULL);
430 	if (nret & NOTIFY_STOP_MASK)
431 		return -EBUSY;
432 
433 	return ret;
434 }
435 
436 void
437 adb_poll(void)
438 {
439 	if ((adb_controller == NULL)||(adb_controller->poll == NULL))
440 		return;
441 	adb_controller->poll();
442 }
443 
444 static void
445 adb_probe_wakeup(struct adb_request *req)
446 {
447 	complete(&adb_probe_task_comp);
448 }
449 
450 /* Static request used during probe */
451 static struct adb_request adb_sreq;
452 static unsigned long adb_sreq_lock; // Use semaphore ! */
453 
454 int
455 adb_request(struct adb_request *req, void (*done)(struct adb_request *),
456 	    int flags, int nbytes, ...)
457 {
458 	va_list list;
459 	int i, use_sreq;
460 	int rc;
461 
462 	if ((adb_controller == NULL) || (adb_controller->send_request == NULL))
463 		return -ENXIO;
464 	if (nbytes < 1)
465 		return -EINVAL;
466 	if (req == NULL && (flags & ADBREQ_NOSEND))
467 		return -EINVAL;
468 
469 	if (req == NULL) {
470 		if (test_and_set_bit(0,&adb_sreq_lock)) {
471 			printk("adb.c: Warning: contention on static request !\n");
472 			return -EPERM;
473 		}
474 		req = &adb_sreq;
475 		flags |= ADBREQ_SYNC;
476 		use_sreq = 1;
477 	} else
478 		use_sreq = 0;
479 	req->nbytes = nbytes+1;
480 	req->done = done;
481 	req->reply_expected = flags & ADBREQ_REPLY;
482 	req->data[0] = ADB_PACKET;
483 	va_start(list, nbytes);
484 	for (i = 0; i < nbytes; ++i)
485 		req->data[i+1] = va_arg(list, int);
486 	va_end(list);
487 
488 	if (flags & ADBREQ_NOSEND)
489 		return 0;
490 
491 	/* Synchronous requests send from the probe thread cause it to
492 	 * block. Beware that the "done" callback will be overriden !
493 	 */
494 	if ((flags & ADBREQ_SYNC) &&
495 	    (current->pid && adb_probe_task_pid &&
496 	    adb_probe_task_pid == current->pid)) {
497 		req->done = adb_probe_wakeup;
498 		rc = adb_controller->send_request(req, 0);
499 		if (rc || req->complete)
500 			goto bail;
501 		wait_for_completion(&adb_probe_task_comp);
502 		rc = 0;
503 		goto bail;
504 	}
505 
506 	rc = adb_controller->send_request(req, flags & ADBREQ_SYNC);
507 bail:
508 	if (use_sreq)
509 		clear_bit(0, &adb_sreq_lock);
510 
511 	return rc;
512 }
513 
514  /* Ultimately this should return the number of devices with
515     the given default id.
516     And it does it now ! Note: changed behaviour: This function
517     will now register if default_id _and_ handler_id both match
518     but handler_id can be left to 0 to match with default_id only.
519     When handler_id is set, this function will try to adjust
520     the handler_id id it doesn't match. */
521 int
522 adb_register(int default_id, int handler_id, struct adb_ids *ids,
523 	     void (*handler)(unsigned char *, int, struct pt_regs *, int))
524 {
525 	int i;
526 
527 	down(&adb_handler_sem);
528 	ids->nids = 0;
529 	for (i = 1; i < 16; i++) {
530 		if ((adb_handler[i].original_address == default_id) &&
531 		    (!handler_id || (handler_id == adb_handler[i].handler_id) ||
532 		    try_handler_change(i, handler_id))) {
533 			if (adb_handler[i].handler != 0) {
534 				printk(KERN_ERR
535 				       "Two handlers for ADB device %d\n",
536 				       default_id);
537 				continue;
538 			}
539 			write_lock_irq(&adb_handler_lock);
540 			adb_handler[i].handler = handler;
541 			write_unlock_irq(&adb_handler_lock);
542 			ids->id[ids->nids++] = i;
543 		}
544 	}
545 	up(&adb_handler_sem);
546 	return ids->nids;
547 }
548 
549 int
550 adb_unregister(int index)
551 {
552 	int ret = -ENODEV;
553 
554 	down(&adb_handler_sem);
555 	write_lock_irq(&adb_handler_lock);
556 	if (adb_handler[index].handler) {
557 		while(adb_handler[index].busy) {
558 			write_unlock_irq(&adb_handler_lock);
559 			yield();
560 			write_lock_irq(&adb_handler_lock);
561 		}
562 		ret = 0;
563 		adb_handler[index].handler = NULL;
564 	}
565 	write_unlock_irq(&adb_handler_lock);
566 	up(&adb_handler_sem);
567 	return ret;
568 }
569 
570 void
571 adb_input(unsigned char *buf, int nb, struct pt_regs *regs, int autopoll)
572 {
573 	int i, id;
574 	static int dump_adb_input = 0;
575 	unsigned long flags;
576 
577 	void (*handler)(unsigned char *, int, struct pt_regs *, int);
578 
579 	/* We skip keystrokes and mouse moves when the sleep process
580 	 * has been started. We stop autopoll, but this is another security
581 	 */
582 	if (adb_got_sleep)
583 		return;
584 
585 	id = buf[0] >> 4;
586 	if (dump_adb_input) {
587 		printk(KERN_INFO "adb packet: ");
588 		for (i = 0; i < nb; ++i)
589 			printk(" %x", buf[i]);
590 		printk(", id = %d\n", id);
591 	}
592 	write_lock_irqsave(&adb_handler_lock, flags);
593 	handler = adb_handler[id].handler;
594 	if (handler != NULL)
595 		adb_handler[id].busy = 1;
596 	write_unlock_irqrestore(&adb_handler_lock, flags);
597 	if (handler != NULL) {
598 		(*handler)(buf, nb, regs, autopoll);
599 		wmb();
600 		adb_handler[id].busy = 0;
601 	}
602 
603 }
604 
605 /* Try to change handler to new_id. Will return 1 if successful. */
606 static int try_handler_change(int address, int new_id)
607 {
608 	struct adb_request req;
609 
610 	if (adb_handler[address].handler_id == new_id)
611 	    return 1;
612 	adb_request(&req, NULL, ADBREQ_SYNC, 3,
613 	    ADB_WRITEREG(address, 3), address | 0x20, new_id);
614 	adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
615 	    ADB_READREG(address, 3));
616 	if (req.reply_len < 2)
617 	    return 0;
618 	if (req.reply[2] != new_id)
619 	    return 0;
620 	adb_handler[address].handler_id = req.reply[2];
621 
622 	return 1;
623 }
624 
625 int
626 adb_try_handler_change(int address, int new_id)
627 {
628 	int ret;
629 
630 	down(&adb_handler_sem);
631 	ret = try_handler_change(address, new_id);
632 	up(&adb_handler_sem);
633 	return ret;
634 }
635 
636 int
637 adb_get_infos(int address, int *original_address, int *handler_id)
638 {
639 	down(&adb_handler_sem);
640 	*original_address = adb_handler[address].original_address;
641 	*handler_id = adb_handler[address].handler_id;
642 	up(&adb_handler_sem);
643 
644 	return (*original_address != 0);
645 }
646 
647 
648 /*
649  * /dev/adb device driver.
650  */
651 
652 #define ADB_MAJOR	56	/* major number for /dev/adb */
653 
654 struct adbdev_state {
655 	spinlock_t	lock;
656 	atomic_t	n_pending;
657 	struct adb_request *completed;
658   	wait_queue_head_t wait_queue;
659 	int		inuse;
660 };
661 
662 static void adb_write_done(struct adb_request *req)
663 {
664 	struct adbdev_state *state = (struct adbdev_state *) req->arg;
665 	unsigned long flags;
666 
667 	if (!req->complete) {
668 		req->reply_len = 0;
669 		req->complete = 1;
670 	}
671 	spin_lock_irqsave(&state->lock, flags);
672 	atomic_dec(&state->n_pending);
673 	if (!state->inuse) {
674 		kfree(req);
675 		if (atomic_read(&state->n_pending) == 0) {
676 			spin_unlock_irqrestore(&state->lock, flags);
677 			kfree(state);
678 			return;
679 		}
680 	} else {
681 		struct adb_request **ap = &state->completed;
682 		while (*ap != NULL)
683 			ap = &(*ap)->next;
684 		req->next = NULL;
685 		*ap = req;
686 		wake_up_interruptible(&state->wait_queue);
687 	}
688 	spin_unlock_irqrestore(&state->lock, flags);
689 }
690 
691 static int
692 do_adb_query(struct adb_request *req)
693 {
694 	int	ret = -EINVAL;
695 
696 	switch(req->data[1])
697 	{
698 	case ADB_QUERY_GETDEVINFO:
699 		if (req->nbytes < 3)
700 			break;
701 		down(&adb_handler_sem);
702 		req->reply[0] = adb_handler[req->data[2]].original_address;
703 		req->reply[1] = adb_handler[req->data[2]].handler_id;
704 		up(&adb_handler_sem);
705 		req->complete = 1;
706 		req->reply_len = 2;
707 		adb_write_done(req);
708 		ret = 0;
709 		break;
710 	}
711 	return ret;
712 }
713 
714 static int adb_open(struct inode *inode, struct file *file)
715 {
716 	struct adbdev_state *state;
717 
718 	if (iminor(inode) > 0 || adb_controller == NULL)
719 		return -ENXIO;
720 	state = kmalloc(sizeof(struct adbdev_state), GFP_KERNEL);
721 	if (state == 0)
722 		return -ENOMEM;
723 	file->private_data = state;
724 	spin_lock_init(&state->lock);
725 	atomic_set(&state->n_pending, 0);
726 	state->completed = NULL;
727 	init_waitqueue_head(&state->wait_queue);
728 	state->inuse = 1;
729 
730 	return 0;
731 }
732 
733 static int adb_release(struct inode *inode, struct file *file)
734 {
735 	struct adbdev_state *state = file->private_data;
736 	unsigned long flags;
737 
738 	lock_kernel();
739 	if (state) {
740 		file->private_data = NULL;
741 		spin_lock_irqsave(&state->lock, flags);
742 		if (atomic_read(&state->n_pending) == 0
743 		    && state->completed == NULL) {
744 			spin_unlock_irqrestore(&state->lock, flags);
745 			kfree(state);
746 		} else {
747 			state->inuse = 0;
748 			spin_unlock_irqrestore(&state->lock, flags);
749 		}
750 	}
751 	unlock_kernel();
752 	return 0;
753 }
754 
755 static ssize_t adb_read(struct file *file, char __user *buf,
756 			size_t count, loff_t *ppos)
757 {
758 	int ret = 0;
759 	struct adbdev_state *state = file->private_data;
760 	struct adb_request *req;
761 	wait_queue_t wait = __WAITQUEUE_INITIALIZER(wait,current);
762 	unsigned long flags;
763 
764 	if (count < 2)
765 		return -EINVAL;
766 	if (count > sizeof(req->reply))
767 		count = sizeof(req->reply);
768 	if (!access_ok(VERIFY_WRITE, buf, count))
769 		return -EFAULT;
770 
771 	req = NULL;
772 	spin_lock_irqsave(&state->lock, flags);
773 	add_wait_queue(&state->wait_queue, &wait);
774 	current->state = TASK_INTERRUPTIBLE;
775 
776 	for (;;) {
777 		req = state->completed;
778 		if (req != NULL)
779 			state->completed = req->next;
780 		else if (atomic_read(&state->n_pending) == 0)
781 			ret = -EIO;
782 		if (req != NULL || ret != 0)
783 			break;
784 
785 		if (file->f_flags & O_NONBLOCK) {
786 			ret = -EAGAIN;
787 			break;
788 		}
789 		if (signal_pending(current)) {
790 			ret = -ERESTARTSYS;
791 			break;
792 		}
793 		spin_unlock_irqrestore(&state->lock, flags);
794 		schedule();
795 		spin_lock_irqsave(&state->lock, flags);
796 	}
797 
798 	current->state = TASK_RUNNING;
799 	remove_wait_queue(&state->wait_queue, &wait);
800 	spin_unlock_irqrestore(&state->lock, flags);
801 
802 	if (ret)
803 		return ret;
804 
805 	ret = req->reply_len;
806 	if (ret > count)
807 		ret = count;
808 	if (ret > 0 && copy_to_user(buf, req->reply, ret))
809 		ret = -EFAULT;
810 
811 	kfree(req);
812 	return ret;
813 }
814 
815 static ssize_t adb_write(struct file *file, const char __user *buf,
816 			 size_t count, loff_t *ppos)
817 {
818 	int ret/*, i*/;
819 	struct adbdev_state *state = file->private_data;
820 	struct adb_request *req;
821 
822 	if (count < 2 || count > sizeof(req->data))
823 		return -EINVAL;
824 	if (adb_controller == NULL)
825 		return -ENXIO;
826 	if (!access_ok(VERIFY_READ, buf, count))
827 		return -EFAULT;
828 
829 	req = (struct adb_request *) kmalloc(sizeof(struct adb_request),
830 					     GFP_KERNEL);
831 	if (req == NULL)
832 		return -ENOMEM;
833 
834 	req->nbytes = count;
835 	req->done = adb_write_done;
836 	req->arg = (void *) state;
837 	req->complete = 0;
838 
839 	ret = -EFAULT;
840 	if (copy_from_user(req->data, buf, count))
841 		goto out;
842 
843 	atomic_inc(&state->n_pending);
844 
845 	/* If a probe is in progress or we are sleeping, wait for it to complete */
846 	down(&adb_probe_mutex);
847 
848 	/* Queries are special requests sent to the ADB driver itself */
849 	if (req->data[0] == ADB_QUERY) {
850 		if (count > 1)
851 			ret = do_adb_query(req);
852 		else
853 			ret = -EINVAL;
854 		up(&adb_probe_mutex);
855 	}
856 	/* Special case for ADB_BUSRESET request, all others are sent to
857 	   the controller */
858 	else if ((req->data[0] == ADB_PACKET)&&(count > 1)
859 		&&(req->data[1] == ADB_BUSRESET)) {
860 		ret = do_adb_reset_bus();
861 		up(&adb_probe_mutex);
862 		atomic_dec(&state->n_pending);
863 		if (ret == 0)
864 			ret = count;
865 		goto out;
866 	} else {
867 		req->reply_expected = ((req->data[1] & 0xc) == 0xc);
868 		if (adb_controller && adb_controller->send_request)
869 			ret = adb_controller->send_request(req, 0);
870 		else
871 			ret = -ENXIO;
872 		up(&adb_probe_mutex);
873 	}
874 
875 	if (ret != 0) {
876 		atomic_dec(&state->n_pending);
877 		goto out;
878 	}
879 	return count;
880 
881 out:
882 	kfree(req);
883 	return ret;
884 }
885 
886 static struct file_operations adb_fops = {
887 	.owner		= THIS_MODULE,
888 	.llseek		= no_llseek,
889 	.read		= adb_read,
890 	.write		= adb_write,
891 	.open		= adb_open,
892 	.release	= adb_release,
893 };
894 
895 static void
896 adbdev_init(void)
897 {
898 	if (register_chrdev(ADB_MAJOR, "adb", &adb_fops)) {
899 		printk(KERN_ERR "adb: unable to get major %d\n", ADB_MAJOR);
900 		return;
901 	}
902 
903 	devfs_mk_cdev(MKDEV(ADB_MAJOR, 0), S_IFCHR | S_IRUSR | S_IWUSR, "adb");
904 
905 	adb_dev_class = class_simple_create(THIS_MODULE, "adb");
906 	if (IS_ERR(adb_dev_class)) {
907 		return;
908 	}
909 	class_simple_device_add(adb_dev_class, MKDEV(ADB_MAJOR, 0), NULL, "adb");
910 }
911