2 * Device driver for the Apple Desktop Bus
3 * and the /dev/adb device on macintoshes.
5 * Copyright (C) 1996 Paul Mackerras.
7 * Modified to declare controllers as structures, added
8 * client notification of bus reset and handles PowerBook
9 * sleep, by Benjamin Herrenschmidt.
13 * - /proc/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
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>
26 #include <linux/devfs_fs_kernel.h>
28 #include <linux/sched.h>
29 #include <linux/smp_lock.h>
30 #include <linux/adb.h>
31 #include <linux/cuda.h>
32 #include <linux/pmu.h>
33 #include <linux/notifier.h>
34 #include <linux/wait.h>
35 #include <linux/init.h>
36 #include <linux/delay.h>
37 #include <linux/spinlock.h>
38 #include <linux/completion.h>
39 #include <asm/uaccess.h>
40 #include <asm/semaphore.h>
46 EXPORT_SYMBOL(adb_controller);
47 EXPORT_SYMBOL(adb_client_list);
49 extern struct adb_driver via_macii_driver;
50 extern struct adb_driver via_maciisi_driver;
51 extern struct adb_driver via_cuda_driver;
52 extern struct adb_driver adb_iop_driver;
53 extern struct adb_driver via_pmu_driver;
54 extern struct adb_driver macio_adb_driver;
56 static struct adb_driver *adb_driver_list[] = {
57 #ifdef CONFIG_ADB_MACII
60 #ifdef CONFIG_ADB_MACIISI
63 #ifdef CONFIG_ADB_CUDA
69 #if defined(CONFIG_ADB_PMU) || defined(CONFIG_ADB_PMU68K)
72 #ifdef CONFIG_ADB_MACIO
78 struct adb_driver *adb_controller;
79 struct notifier_block *adb_client_list = NULL;
80 static int adb_got_sleep;
81 static int adb_inited;
82 static pid_t adb_probe_task_pid;
83 static DECLARE_MUTEX(adb_probe_mutex);
84 static struct completion adb_probe_task_comp;
85 static int sleepy_trackpad;
86 static int autopoll_devs;
89 #ifdef CONFIG_PMAC_PBOOK
90 static int adb_notify_sleep(struct pmu_sleep_notifier *self, int when);
91 static struct pmu_sleep_notifier adb_sleep_notifier = {
97 static int adb_scan_bus(void);
98 static int do_adb_reset_bus(void);
99 static void adbdev_init(void);
100 static int try_handler_change(int, int);
102 static struct adb_handler {
103 void (*handler)(unsigned char *, int, struct pt_regs *, int);
104 int original_address;
110 * The adb_handler_sem mutex protects all accesses to the original_address
111 * and handler_id fields of adb_handler[i] for all i, and changes to the
113 * Accesses to the handler field are protected by the adb_handler_lock
114 * rwlock. It is held across all calls to any handler, so that by the
115 * time adb_unregister returns, we know that the old handler isn't being
118 static DECLARE_MUTEX(adb_handler_sem);
119 static rwlock_t adb_handler_lock = RW_LOCK_UNLOCKED;
122 static void printADBreply(struct adb_request *req)
126 printk("adb reply (%d)", req->reply_len);
127 for(i = 0; i < req->reply_len; i++)
128 printk(" %x", req->reply[i]);
135 static __inline__ void adb_wait_ms(unsigned int ms)
137 if (current->pid && adb_probe_task_pid &&
138 adb_probe_task_pid == current->pid) {
139 set_task_state(current, TASK_UNINTERRUPTIBLE);
140 schedule_timeout(1 + ms * HZ / 1000);
145 static int adb_scan_bus(void)
147 int i, highFree=0, noMovement;
149 struct adb_request req;
151 /* assumes adb_handler[] is all zeroes at this point */
152 for (i = 1; i < 16; i++) {
153 /* see if there is anything at address i */
154 adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
156 if (req.reply_len > 1)
157 /* one or more devices at this address */
158 adb_handler[i].original_address = i;
159 else if (i > highFree)
163 /* Note we reset noMovement to 0 each time we move a device */
164 for (noMovement = 1; noMovement < 2 && highFree > 0; noMovement++) {
165 for (i = 1; i < 16; i++) {
166 if (adb_handler[i].original_address == 0)
169 * Send a "talk register 3" command to address i
170 * to provoke a collision if there is more than
171 * one device at this address.
173 adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
176 * Move the device(s) which didn't detect a
177 * collision to address `highFree'. Hopefully
178 * this only moves one device.
180 adb_request(&req, NULL, ADBREQ_SYNC, 3,
181 (i<< 4) | 0xb, (highFree | 0x60), 0xfe);
183 * See if anybody actually moved. This is suggested
186 * http://developer.apple.com/technotes/hw/hw_01.html
188 adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
189 (highFree << 4) | 0xf);
190 if (req.reply_len <= 1) continue;
192 * Test whether there are any device(s) left
195 adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
197 if (req.reply_len > 1) {
199 * There are still one or more devices
200 * left at address i. Register the one(s)
201 * we moved to `highFree', and find a new
202 * value for highFree.
204 adb_handler[highFree].original_address =
205 adb_handler[i].original_address;
206 while (highFree > 0 &&
207 adb_handler[highFree].original_address)
216 * No devices left at address i; move the
217 * one(s) we moved to `highFree' back to i.
219 adb_request(&req, NULL, ADBREQ_SYNC, 3,
220 (highFree << 4) | 0xb,
226 /* Now fill in the handler_id field of the adb_handler entries. */
227 printk(KERN_DEBUG "adb devices:");
228 for (i = 1; i < 16; i++) {
229 if (adb_handler[i].original_address == 0)
231 adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
233 adb_handler[i].handler_id = req.reply[2];
234 printk(" [%d]: %d %x", i, adb_handler[i].original_address,
235 adb_handler[i].handler_id);
243 * This kernel task handles ADB probing. It dies once probing is
247 adb_probe_task(void *x)
251 strcpy(current->comm, "kadbprobe");
253 sigfillset(&blocked);
254 sigprocmask(SIG_BLOCK, &blocked, NULL);
255 flush_signals(current);
257 printk(KERN_INFO "adb: starting probe task...\n");
259 printk(KERN_INFO "adb: finished probe task...\n");
261 adb_probe_task_pid = 0;
262 up(&adb_probe_mutex);
268 __adb_probe_task(void *data)
270 adb_probe_task_pid = kernel_thread(adb_probe_task, NULL, SIGCHLD | CLONE_KERNEL);
273 static DECLARE_WORK(adb_reset_work, __adb_probe_task, NULL);
278 if (__adb_probe_sync) {
283 down(&adb_probe_mutex);
284 schedule_work(&adb_reset_work);
288 int __init adb_init(void)
290 struct adb_driver *driver;
294 if ( (_machine != _MACH_chrp) && (_machine != _MACH_Pmac) )
302 /* xmon may do early-init */
307 adb_controller = NULL;
310 while ((driver = adb_driver_list[i++]) != NULL) {
311 if (!driver->probe()) {
312 adb_controller = driver;
316 if ((adb_controller == NULL) || adb_controller->init()) {
317 printk(KERN_WARNING "Warning: no ADB interface detected\n");
318 adb_controller = NULL;
320 #ifdef CONFIG_PMAC_PBOOK
321 pmu_register_sleep_notifier(&adb_sleep_notifier);
322 #endif /* CONFIG_PMAC_PBOOK */
324 if (machine_is_compatible("AAPL,PowerBook1998") ||
325 machine_is_compatible("PowerBook1,1"))
327 #endif /* CONFIG_PPC */
328 init_completion(&adb_probe_task_comp);
335 __initcall(adb_init);
337 #ifdef CONFIG_PMAC_PBOOK
339 * notify clients before sleep and reset bus afterwards
342 adb_notify_sleep(struct pmu_sleep_notifier *self, int when)
347 case PBOOK_SLEEP_REQUEST:
349 /* We need to get a lock on the probe thread */
350 down(&adb_probe_mutex);
352 if (adb_controller->autopoll)
353 adb_controller->autopoll(0);
354 ret = notifier_call_chain(&adb_client_list, ADB_MSG_POWERDOWN, NULL);
355 if (ret & NOTIFY_STOP_MASK) {
356 up(&adb_probe_mutex);
357 return PBOOK_SLEEP_REFUSE;
360 case PBOOK_SLEEP_REJECT:
363 up(&adb_probe_mutex);
368 case PBOOK_SLEEP_NOW:
372 up(&adb_probe_mutex);
376 return PBOOK_SLEEP_OK;
378 #endif /* CONFIG_PMAC_PBOOK */
381 do_adb_reset_bus(void)
385 if (adb_controller == NULL)
388 if (adb_controller->autopoll)
389 adb_controller->autopoll(0);
391 nret = notifier_call_chain(&adb_client_list, ADB_MSG_PRE_RESET, NULL);
392 if (nret & NOTIFY_STOP_MASK) {
393 if (adb_controller->autopoll)
394 adb_controller->autopoll(autopoll_devs);
398 if (sleepy_trackpad) {
399 /* Let the trackpad settle down */
403 down(&adb_handler_sem);
404 write_lock_irq(&adb_handler_lock);
405 memset(adb_handler, 0, sizeof(adb_handler));
406 write_unlock_irq(&adb_handler_lock);
408 /* That one is still a bit synchronous, oh well... */
409 if (adb_controller->reset_bus)
410 ret = adb_controller->reset_bus();
414 if (sleepy_trackpad) {
415 /* Let the trackpad settle down */
420 autopoll_devs = adb_scan_bus();
421 if (adb_controller->autopoll)
422 adb_controller->autopoll(autopoll_devs);
424 up(&adb_handler_sem);
426 nret = notifier_call_chain(&adb_client_list, ADB_MSG_POST_RESET, NULL);
427 if (nret & NOTIFY_STOP_MASK)
436 if ((adb_controller == NULL)||(adb_controller->poll == NULL))
438 adb_controller->poll();
442 adb_probe_wakeup(struct adb_request *req)
444 complete(&adb_probe_task_comp);
447 /* Static request used during probe */
448 static struct adb_request adb_sreq;
449 static unsigned long adb_sreq_lock; // Use semaphore ! */
452 adb_request(struct adb_request *req, void (*done)(struct adb_request *),
453 int flags, int nbytes, ...)
459 if ((adb_controller == NULL) || (adb_controller->send_request == NULL))
463 if (req == NULL && (flags & ADBREQ_NOSEND))
467 if (test_and_set_bit(0,&adb_sreq_lock)) {
468 printk("adb.c: Warning: contention on static request !\n");
472 flags |= ADBREQ_SYNC;
476 req->nbytes = nbytes+1;
478 req->reply_expected = flags & ADBREQ_REPLY;
479 req->data[0] = ADB_PACKET;
480 va_start(list, nbytes);
481 for (i = 0; i < nbytes; ++i)
482 req->data[i+1] = va_arg(list, int);
485 if (flags & ADBREQ_NOSEND)
488 /* Synchronous requests send from the probe thread cause it to
489 * block. Beware that the "done" callback will be overriden !
491 if ((flags & ADBREQ_SYNC) &&
492 (current->pid && adb_probe_task_pid &&
493 adb_probe_task_pid == current->pid)) {
494 req->done = adb_probe_wakeup;
495 rc = adb_controller->send_request(req, 0);
496 if (rc || req->complete)
498 wait_for_completion(&adb_probe_task_comp);
503 rc = adb_controller->send_request(req, flags & ADBREQ_SYNC);
506 clear_bit(0, &adb_sreq_lock);
511 /* Ultimately this should return the number of devices with
512 the given default id.
513 And it does it now ! Note: changed behaviour: This function
514 will now register if default_id _and_ handler_id both match
515 but handler_id can be left to 0 to match with default_id only.
516 When handler_id is set, this function will try to adjust
517 the handler_id id it doesn't match. */
519 adb_register(int default_id, int handler_id, struct adb_ids *ids,
520 void (*handler)(unsigned char *, int, struct pt_regs *, int))
524 down(&adb_handler_sem);
526 for (i = 1; i < 16; i++) {
527 if ((adb_handler[i].original_address == default_id) &&
528 (!handler_id || (handler_id == adb_handler[i].handler_id) ||
529 try_handler_change(i, handler_id))) {
530 if (adb_handler[i].handler != 0) {
532 "Two handlers for ADB device %d\n",
536 write_lock_irq(&adb_handler_lock);
537 adb_handler[i].handler = handler;
538 write_unlock_irq(&adb_handler_lock);
539 ids->id[ids->nids++] = i;
542 up(&adb_handler_sem);
547 adb_unregister(int index)
551 down(&adb_handler_sem);
552 write_lock_irq(&adb_handler_lock);
553 if (adb_handler[index].handler) {
554 while(adb_handler[index].busy) {
555 write_unlock_irq(&adb_handler_lock);
557 write_lock_irq(&adb_handler_lock);
560 adb_handler[index].handler = 0;
562 write_unlock_irq(&adb_handler_lock);
563 up(&adb_handler_sem);
568 adb_input(unsigned char *buf, int nb, struct pt_regs *regs, int autopoll)
571 static int dump_adb_input = 0;
574 void (*handler)(unsigned char *, int, struct pt_regs *, int);
576 /* We skip keystrokes and mouse moves when the sleep process
577 * has been started. We stop autopoll, but this is another security
583 if (dump_adb_input) {
584 printk(KERN_INFO "adb packet: ");
585 for (i = 0; i < nb; ++i)
586 printk(" %x", buf[i]);
587 printk(", id = %d\n", id);
589 write_lock_irqsave(&adb_handler_lock, flags);
590 handler = adb_handler[id].handler;
592 adb_handler[id].busy = 1;
593 write_unlock_irqrestore(&adb_handler_lock, flags);
594 if (handler != NULL) {
595 (*handler)(buf, nb, regs, autopoll);
597 adb_handler[id].busy = 0;
602 /* Try to change handler to new_id. Will return 1 if successful. */
603 static int try_handler_change(int address, int new_id)
605 struct adb_request req;
607 if (adb_handler[address].handler_id == new_id)
609 adb_request(&req, NULL, ADBREQ_SYNC, 3,
610 ADB_WRITEREG(address, 3), address | 0x20, new_id);
611 adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
612 ADB_READREG(address, 3));
613 if (req.reply_len < 2)
615 if (req.reply[2] != new_id)
617 adb_handler[address].handler_id = req.reply[2];
623 adb_try_handler_change(int address, int new_id)
627 down(&adb_handler_sem);
628 ret = try_handler_change(address, new_id);
629 up(&adb_handler_sem);
634 adb_get_infos(int address, int *original_address, int *handler_id)
636 down(&adb_handler_sem);
637 *original_address = adb_handler[address].original_address;
638 *handler_id = adb_handler[address].handler_id;
639 up(&adb_handler_sem);
641 return (*original_address != 0);
646 * /dev/adb device driver.
649 #define ADB_MAJOR 56 /* major number for /dev/adb */
651 struct adbdev_state {
654 struct adb_request *completed;
655 wait_queue_head_t wait_queue;
659 static void adb_write_done(struct adb_request *req)
661 struct adbdev_state *state = (struct adbdev_state *) req->arg;
664 if (!req->complete) {
668 spin_lock_irqsave(&state->lock, flags);
669 atomic_dec(&state->n_pending);
672 if (atomic_read(&state->n_pending) == 0) {
673 spin_unlock_irqrestore(&state->lock, flags);
678 struct adb_request **ap = &state->completed;
683 wake_up_interruptible(&state->wait_queue);
685 spin_unlock_irqrestore(&state->lock, flags);
689 do_adb_query(struct adb_request *req)
695 case ADB_QUERY_GETDEVINFO:
698 down(&adb_handler_sem);
699 req->reply[0] = adb_handler[req->data[2]].original_address;
700 req->reply[1] = adb_handler[req->data[2]].handler_id;
701 up(&adb_handler_sem);
711 static int adb_open(struct inode *inode, struct file *file)
713 struct adbdev_state *state;
715 if (iminor(inode) > 0 || adb_controller == NULL)
717 state = kmalloc(sizeof(struct adbdev_state), GFP_KERNEL);
720 file->private_data = state;
721 spin_lock_init(&state->lock);
722 atomic_set(&state->n_pending, 0);
723 state->completed = NULL;
724 init_waitqueue_head(&state->wait_queue);
730 static int adb_release(struct inode *inode, struct file *file)
732 struct adbdev_state *state = file->private_data;
737 file->private_data = NULL;
738 spin_lock_irqsave(&state->lock, flags);
739 if (atomic_read(&state->n_pending) == 0
740 && state->completed == NULL) {
741 spin_unlock_irqrestore(&state->lock, flags);
745 spin_unlock_irqrestore(&state->lock, flags);
752 static ssize_t adb_read(struct file *file, char __user *buf,
753 size_t count, loff_t *ppos)
756 struct adbdev_state *state = file->private_data;
757 struct adb_request *req;
758 wait_queue_t wait = __WAITQUEUE_INITIALIZER(wait,current);
763 if (count > sizeof(req->reply))
764 count = sizeof(req->reply);
765 ret = verify_area(VERIFY_WRITE, buf, count);
770 spin_lock_irqsave(&state->lock, flags);
771 add_wait_queue(&state->wait_queue, &wait);
772 current->state = TASK_INTERRUPTIBLE;
775 req = state->completed;
777 state->completed = req->next;
778 else if (atomic_read(&state->n_pending) == 0)
780 if (req != NULL || ret != 0)
783 if (file->f_flags & O_NONBLOCK) {
787 if (signal_pending(current)) {
791 spin_unlock_irqrestore(&state->lock, flags);
793 spin_lock_irqsave(&state->lock, flags);
796 current->state = TASK_RUNNING;
797 remove_wait_queue(&state->wait_queue, &wait);
798 spin_unlock_irqrestore(&state->lock, flags);
803 ret = req->reply_len;
806 if (ret > 0 && copy_to_user(buf, req->reply, ret))
813 static ssize_t adb_write(struct file *file, const char __user *buf,
814 size_t count, loff_t *ppos)
817 struct adbdev_state *state = file->private_data;
818 struct adb_request *req;
820 if (count < 2 || count > sizeof(req->data))
822 if (adb_controller == NULL)
824 ret = verify_area(VERIFY_READ, buf, count);
828 req = (struct adb_request *) kmalloc(sizeof(struct adb_request),
834 req->done = adb_write_done;
835 req->arg = (void *) state;
839 if (copy_from_user(req->data, buf, count))
842 atomic_inc(&state->n_pending);
844 /* If a probe is in progress or we are sleeping, wait for it to complete */
845 down(&adb_probe_mutex);
847 /* Queries are special requests sent to the ADB driver itself */
848 if (req->data[0] == ADB_QUERY) {
850 ret = do_adb_query(req);
853 up(&adb_probe_mutex);
855 /* Special case for ADB_BUSRESET request, all others are sent to
857 else if ((req->data[0] == ADB_PACKET)&&(count > 1)
858 &&(req->data[1] == ADB_BUSRESET)) {
859 ret = do_adb_reset_bus();
860 up(&adb_probe_mutex);
861 atomic_dec(&state->n_pending);
866 req->reply_expected = ((req->data[1] & 0xc) == 0xc);
867 if (adb_controller && adb_controller->send_request)
868 ret = adb_controller->send_request(req, 0);
871 up(&adb_probe_mutex);
875 atomic_dec(&state->n_pending);
885 static struct file_operations adb_fops = {
890 .release = adb_release,
896 if (register_chrdev(ADB_MAJOR, "adb", &adb_fops))
897 printk(KERN_ERR "adb: unable to get major %d\n", ADB_MAJOR);
899 devfs_mk_cdev(MKDEV(ADB_MAJOR, 0),
900 S_IFCHR | S_IRUSR | S_IWUSR, "adb");