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 * - /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
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>
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>
41 #include <asm/uaccess.h>
42 #include <asm/semaphore.h>
45 #include <asm/machdep.h>
49 EXPORT_SYMBOL(adb_controller);
50 EXPORT_SYMBOL(adb_client_list);
52 extern struct adb_driver via_macii_driver;
53 extern struct adb_driver via_maciisi_driver;
54 extern struct adb_driver via_cuda_driver;
55 extern struct adb_driver adb_iop_driver;
56 extern struct adb_driver via_pmu_driver;
57 extern struct adb_driver macio_adb_driver;
59 static struct adb_driver *adb_driver_list[] = {
60 #ifdef CONFIG_ADB_MACII
63 #ifdef CONFIG_ADB_MACIISI
66 #ifdef CONFIG_ADB_CUDA
72 #if defined(CONFIG_ADB_PMU) || defined(CONFIG_ADB_PMU68K)
75 #ifdef CONFIG_ADB_MACIO
81 static struct class *adb_dev_class;
83 struct adb_driver *adb_controller;
84 BLOCKING_NOTIFIER_HEAD(adb_client_list);
85 static int adb_got_sleep;
86 static int adb_inited;
87 static pid_t adb_probe_task_pid;
88 static DECLARE_MUTEX(adb_probe_mutex);
89 static struct completion adb_probe_task_comp;
90 static int sleepy_trackpad;
91 static int autopoll_devs;
95 static int adb_notify_sleep(struct pmu_sleep_notifier *self, int when);
96 static struct pmu_sleep_notifier adb_sleep_notifier = {
102 static int adb_scan_bus(void);
103 static int do_adb_reset_bus(void);
104 static void adbdev_init(void);
105 static int try_handler_change(int, int);
107 static struct adb_handler {
108 void (*handler)(unsigned char *, int, struct pt_regs *, int);
109 int original_address;
115 * The adb_handler_sem mutex protects all accesses to the original_address
116 * and handler_id fields of adb_handler[i] for all i, and changes to the
118 * Accesses to the handler field are protected by the adb_handler_lock
119 * rwlock. It is held across all calls to any handler, so that by the
120 * time adb_unregister returns, we know that the old handler isn't being
123 static DECLARE_MUTEX(adb_handler_sem);
124 static DEFINE_RWLOCK(adb_handler_lock);
127 static void printADBreply(struct adb_request *req)
131 printk("adb reply (%d)", req->reply_len);
132 for(i = 0; i < req->reply_len; i++)
133 printk(" %x", req->reply[i]);
140 static __inline__ void adb_wait_ms(unsigned int ms)
142 if (current->pid && adb_probe_task_pid &&
143 adb_probe_task_pid == current->pid)
149 static int adb_scan_bus(void)
151 int i, highFree=0, noMovement;
153 struct adb_request req;
155 /* assumes adb_handler[] is all zeroes at this point */
156 for (i = 1; i < 16; i++) {
157 /* see if there is anything at address i */
158 adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
160 if (req.reply_len > 1)
161 /* one or more devices at this address */
162 adb_handler[i].original_address = i;
163 else if (i > highFree)
167 /* Note we reset noMovement to 0 each time we move a device */
168 for (noMovement = 1; noMovement < 2 && highFree > 0; noMovement++) {
169 for (i = 1; i < 16; i++) {
170 if (adb_handler[i].original_address == 0)
173 * Send a "talk register 3" command to address i
174 * to provoke a collision if there is more than
175 * one device at this address.
177 adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
180 * Move the device(s) which didn't detect a
181 * collision to address `highFree'. Hopefully
182 * this only moves one device.
184 adb_request(&req, NULL, ADBREQ_SYNC, 3,
185 (i<< 4) | 0xb, (highFree | 0x60), 0xfe);
187 * See if anybody actually moved. This is suggested
190 * http://developer.apple.com/technotes/hw/hw_01.html
192 adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
193 (highFree << 4) | 0xf);
194 if (req.reply_len <= 1) continue;
196 * Test whether there are any device(s) left
199 adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
201 if (req.reply_len > 1) {
203 * There are still one or more devices
204 * left at address i. Register the one(s)
205 * we moved to `highFree', and find a new
206 * value for highFree.
208 adb_handler[highFree].original_address =
209 adb_handler[i].original_address;
210 while (highFree > 0 &&
211 adb_handler[highFree].original_address)
220 * No devices left at address i; move the
221 * one(s) we moved to `highFree' back to i.
223 adb_request(&req, NULL, ADBREQ_SYNC, 3,
224 (highFree << 4) | 0xb,
230 /* Now fill in the handler_id field of the adb_handler entries. */
231 printk(KERN_DEBUG "adb devices:");
232 for (i = 1; i < 16; i++) {
233 if (adb_handler[i].original_address == 0)
235 adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
237 adb_handler[i].handler_id = req.reply[2];
238 printk(" [%d]: %d %x", i, adb_handler[i].original_address,
239 adb_handler[i].handler_id);
247 * This kernel task handles ADB probing. It dies once probing is
251 adb_probe_task(void *x)
255 strcpy(current->comm, "kadbprobe");
257 sigfillset(&blocked);
258 sigprocmask(SIG_BLOCK, &blocked, NULL);
259 flush_signals(current);
261 printk(KERN_INFO "adb: starting probe task...\n");
263 printk(KERN_INFO "adb: finished probe task...\n");
265 adb_probe_task_pid = 0;
266 up(&adb_probe_mutex);
272 __adb_probe_task(void *data)
274 adb_probe_task_pid = kernel_thread(adb_probe_task, NULL, SIGCHLD | CLONE_KERNEL);
277 static DECLARE_WORK(adb_reset_work, __adb_probe_task, NULL);
282 if (__adb_probe_sync) {
287 down(&adb_probe_mutex);
288 schedule_work(&adb_reset_work);
292 int __init adb_init(void)
294 struct adb_driver *driver;
298 if (!machine_is(chrp) && !machine_is(powermac))
306 /* xmon may do early-init */
311 adb_controller = NULL;
314 while ((driver = adb_driver_list[i++]) != NULL) {
315 if (!driver->probe()) {
316 adb_controller = driver;
320 if ((adb_controller == NULL) || adb_controller->init()) {
321 printk(KERN_WARNING "Warning: no ADB interface detected\n");
322 adb_controller = NULL;
325 pmu_register_sleep_notifier(&adb_sleep_notifier);
326 #endif /* CONFIG_PM */
328 if (machine_is_compatible("AAPL,PowerBook1998") ||
329 machine_is_compatible("PowerBook1,1"))
331 #endif /* CONFIG_PPC */
332 init_completion(&adb_probe_task_comp);
339 __initcall(adb_init);
343 * notify clients before sleep and reset bus afterwards
346 adb_notify_sleep(struct pmu_sleep_notifier *self, int when)
351 case PBOOK_SLEEP_REQUEST:
353 /* We need to get a lock on the probe thread */
354 down(&adb_probe_mutex);
356 if (adb_controller->autopoll)
357 adb_controller->autopoll(0);
358 ret = blocking_notifier_call_chain(&adb_client_list,
359 ADB_MSG_POWERDOWN, NULL);
360 if (ret & NOTIFY_STOP_MASK) {
361 up(&adb_probe_mutex);
362 return PBOOK_SLEEP_REFUSE;
365 case PBOOK_SLEEP_REJECT:
368 up(&adb_probe_mutex);
373 case PBOOK_SLEEP_NOW:
377 up(&adb_probe_mutex);
381 return PBOOK_SLEEP_OK;
383 #endif /* CONFIG_PM */
386 do_adb_reset_bus(void)
390 if (adb_controller == NULL)
393 if (adb_controller->autopoll)
394 adb_controller->autopoll(0);
396 nret = blocking_notifier_call_chain(&adb_client_list,
397 ADB_MSG_PRE_RESET, NULL);
398 if (nret & NOTIFY_STOP_MASK) {
399 if (adb_controller->autopoll)
400 adb_controller->autopoll(autopoll_devs);
404 if (sleepy_trackpad) {
405 /* Let the trackpad settle down */
409 down(&adb_handler_sem);
410 write_lock_irq(&adb_handler_lock);
411 memset(adb_handler, 0, sizeof(adb_handler));
412 write_unlock_irq(&adb_handler_lock);
414 /* That one is still a bit synchronous, oh well... */
415 if (adb_controller->reset_bus)
416 ret = adb_controller->reset_bus();
420 if (sleepy_trackpad) {
421 /* Let the trackpad settle down */
426 autopoll_devs = adb_scan_bus();
427 if (adb_controller->autopoll)
428 adb_controller->autopoll(autopoll_devs);
430 up(&adb_handler_sem);
432 nret = blocking_notifier_call_chain(&adb_client_list,
433 ADB_MSG_POST_RESET, NULL);
434 if (nret & NOTIFY_STOP_MASK)
443 if ((adb_controller == NULL)||(adb_controller->poll == NULL))
445 adb_controller->poll();
449 adb_probe_wakeup(struct adb_request *req)
451 complete(&adb_probe_task_comp);
454 /* Static request used during probe */
455 static struct adb_request adb_sreq;
456 static unsigned long adb_sreq_lock; // Use semaphore ! */
459 adb_request(struct adb_request *req, void (*done)(struct adb_request *),
460 int flags, int nbytes, ...)
466 if ((adb_controller == NULL) || (adb_controller->send_request == NULL))
470 if (req == NULL && (flags & ADBREQ_NOSEND))
474 if (test_and_set_bit(0,&adb_sreq_lock)) {
475 printk("adb.c: Warning: contention on static request !\n");
479 flags |= ADBREQ_SYNC;
483 req->nbytes = nbytes+1;
485 req->reply_expected = flags & ADBREQ_REPLY;
486 req->data[0] = ADB_PACKET;
487 va_start(list, nbytes);
488 for (i = 0; i < nbytes; ++i)
489 req->data[i+1] = va_arg(list, int);
492 if (flags & ADBREQ_NOSEND)
495 /* Synchronous requests send from the probe thread cause it to
496 * block. Beware that the "done" callback will be overriden !
498 if ((flags & ADBREQ_SYNC) &&
499 (current->pid && adb_probe_task_pid &&
500 adb_probe_task_pid == current->pid)) {
501 req->done = adb_probe_wakeup;
502 rc = adb_controller->send_request(req, 0);
503 if (rc || req->complete)
505 wait_for_completion(&adb_probe_task_comp);
510 rc = adb_controller->send_request(req, flags & ADBREQ_SYNC);
513 clear_bit(0, &adb_sreq_lock);
518 /* Ultimately this should return the number of devices with
519 the given default id.
520 And it does it now ! Note: changed behaviour: This function
521 will now register if default_id _and_ handler_id both match
522 but handler_id can be left to 0 to match with default_id only.
523 When handler_id is set, this function will try to adjust
524 the handler_id id it doesn't match. */
526 adb_register(int default_id, int handler_id, struct adb_ids *ids,
527 void (*handler)(unsigned char *, int, struct pt_regs *, int))
531 down(&adb_handler_sem);
533 for (i = 1; i < 16; i++) {
534 if ((adb_handler[i].original_address == default_id) &&
535 (!handler_id || (handler_id == adb_handler[i].handler_id) ||
536 try_handler_change(i, handler_id))) {
537 if (adb_handler[i].handler != 0) {
539 "Two handlers for ADB device %d\n",
543 write_lock_irq(&adb_handler_lock);
544 adb_handler[i].handler = handler;
545 write_unlock_irq(&adb_handler_lock);
546 ids->id[ids->nids++] = i;
549 up(&adb_handler_sem);
554 adb_unregister(int index)
558 down(&adb_handler_sem);
559 write_lock_irq(&adb_handler_lock);
560 if (adb_handler[index].handler) {
561 while(adb_handler[index].busy) {
562 write_unlock_irq(&adb_handler_lock);
564 write_lock_irq(&adb_handler_lock);
567 adb_handler[index].handler = NULL;
569 write_unlock_irq(&adb_handler_lock);
570 up(&adb_handler_sem);
575 adb_input(unsigned char *buf, int nb, struct pt_regs *regs, int autopoll)
578 static int dump_adb_input = 0;
581 void (*handler)(unsigned char *, int, struct pt_regs *, int);
583 /* We skip keystrokes and mouse moves when the sleep process
584 * has been started. We stop autopoll, but this is another security
590 if (dump_adb_input) {
591 printk(KERN_INFO "adb packet: ");
592 for (i = 0; i < nb; ++i)
593 printk(" %x", buf[i]);
594 printk(", id = %d\n", id);
596 write_lock_irqsave(&adb_handler_lock, flags);
597 handler = adb_handler[id].handler;
599 adb_handler[id].busy = 1;
600 write_unlock_irqrestore(&adb_handler_lock, flags);
601 if (handler != NULL) {
602 (*handler)(buf, nb, regs, autopoll);
604 adb_handler[id].busy = 0;
609 /* Try to change handler to new_id. Will return 1 if successful. */
610 static int try_handler_change(int address, int new_id)
612 struct adb_request req;
614 if (adb_handler[address].handler_id == new_id)
616 adb_request(&req, NULL, ADBREQ_SYNC, 3,
617 ADB_WRITEREG(address, 3), address | 0x20, new_id);
618 adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
619 ADB_READREG(address, 3));
620 if (req.reply_len < 2)
622 if (req.reply[2] != new_id)
624 adb_handler[address].handler_id = req.reply[2];
630 adb_try_handler_change(int address, int new_id)
634 down(&adb_handler_sem);
635 ret = try_handler_change(address, new_id);
636 up(&adb_handler_sem);
641 adb_get_infos(int address, int *original_address, int *handler_id)
643 down(&adb_handler_sem);
644 *original_address = adb_handler[address].original_address;
645 *handler_id = adb_handler[address].handler_id;
646 up(&adb_handler_sem);
648 return (*original_address != 0);
653 * /dev/adb device driver.
656 #define ADB_MAJOR 56 /* major number for /dev/adb */
658 struct adbdev_state {
661 struct adb_request *completed;
662 wait_queue_head_t wait_queue;
666 static void adb_write_done(struct adb_request *req)
668 struct adbdev_state *state = (struct adbdev_state *) req->arg;
671 if (!req->complete) {
675 spin_lock_irqsave(&state->lock, flags);
676 atomic_dec(&state->n_pending);
679 if (atomic_read(&state->n_pending) == 0) {
680 spin_unlock_irqrestore(&state->lock, flags);
685 struct adb_request **ap = &state->completed;
690 wake_up_interruptible(&state->wait_queue);
692 spin_unlock_irqrestore(&state->lock, flags);
696 do_adb_query(struct adb_request *req)
702 case ADB_QUERY_GETDEVINFO:
705 down(&adb_handler_sem);
706 req->reply[0] = adb_handler[req->data[2]].original_address;
707 req->reply[1] = adb_handler[req->data[2]].handler_id;
708 up(&adb_handler_sem);
718 static int adb_open(struct inode *inode, struct file *file)
720 struct adbdev_state *state;
722 if (iminor(inode) > 0 || adb_controller == NULL)
724 state = kmalloc(sizeof(struct adbdev_state), GFP_KERNEL);
727 file->private_data = state;
728 spin_lock_init(&state->lock);
729 atomic_set(&state->n_pending, 0);
730 state->completed = NULL;
731 init_waitqueue_head(&state->wait_queue);
737 static int adb_release(struct inode *inode, struct file *file)
739 struct adbdev_state *state = file->private_data;
744 file->private_data = NULL;
745 spin_lock_irqsave(&state->lock, flags);
746 if (atomic_read(&state->n_pending) == 0
747 && state->completed == NULL) {
748 spin_unlock_irqrestore(&state->lock, flags);
752 spin_unlock_irqrestore(&state->lock, flags);
759 static ssize_t adb_read(struct file *file, char __user *buf,
760 size_t count, loff_t *ppos)
763 struct adbdev_state *state = file->private_data;
764 struct adb_request *req;
765 wait_queue_t wait = __WAITQUEUE_INITIALIZER(wait,current);
770 if (count > sizeof(req->reply))
771 count = sizeof(req->reply);
772 if (!access_ok(VERIFY_WRITE, buf, count))
776 spin_lock_irqsave(&state->lock, flags);
777 add_wait_queue(&state->wait_queue, &wait);
778 current->state = TASK_INTERRUPTIBLE;
781 req = state->completed;
783 state->completed = req->next;
784 else if (atomic_read(&state->n_pending) == 0)
786 if (req != NULL || ret != 0)
789 if (file->f_flags & O_NONBLOCK) {
793 if (signal_pending(current)) {
797 spin_unlock_irqrestore(&state->lock, flags);
799 spin_lock_irqsave(&state->lock, flags);
802 current->state = TASK_RUNNING;
803 remove_wait_queue(&state->wait_queue, &wait);
804 spin_unlock_irqrestore(&state->lock, flags);
809 ret = req->reply_len;
812 if (ret > 0 && copy_to_user(buf, req->reply, ret))
819 static ssize_t adb_write(struct file *file, const char __user *buf,
820 size_t count, loff_t *ppos)
823 struct adbdev_state *state = file->private_data;
824 struct adb_request *req;
826 if (count < 2 || count > sizeof(req->data))
828 if (adb_controller == NULL)
830 if (!access_ok(VERIFY_READ, buf, count))
833 req = (struct adb_request *) kmalloc(sizeof(struct adb_request),
839 req->done = adb_write_done;
840 req->arg = (void *) state;
844 if (copy_from_user(req->data, buf, count))
847 atomic_inc(&state->n_pending);
849 /* If a probe is in progress or we are sleeping, wait for it to complete */
850 down(&adb_probe_mutex);
852 /* Queries are special requests sent to the ADB driver itself */
853 if (req->data[0] == ADB_QUERY) {
855 ret = do_adb_query(req);
858 up(&adb_probe_mutex);
860 /* Special case for ADB_BUSRESET request, all others are sent to
862 else if ((req->data[0] == ADB_PACKET)&&(count > 1)
863 &&(req->data[1] == ADB_BUSRESET)) {
864 ret = do_adb_reset_bus();
865 up(&adb_probe_mutex);
866 atomic_dec(&state->n_pending);
871 req->reply_expected = ((req->data[1] & 0xc) == 0xc);
872 if (adb_controller && adb_controller->send_request)
873 ret = adb_controller->send_request(req, 0);
876 up(&adb_probe_mutex);
880 atomic_dec(&state->n_pending);
890 static struct file_operations adb_fops = {
891 .owner = THIS_MODULE,
896 .release = adb_release,
902 if (register_chrdev(ADB_MAJOR, "adb", &adb_fops)) {
903 printk(KERN_ERR "adb: unable to get major %d\n", ADB_MAJOR);
907 devfs_mk_cdev(MKDEV(ADB_MAJOR, 0), S_IFCHR | S_IRUSR | S_IWUSR, "adb");
909 adb_dev_class = class_create(THIS_MODULE, "adb");
910 if (IS_ERR(adb_dev_class))
912 class_device_create(adb_dev_class, NULL, MKDEV(ADB_MAJOR, 0), NULL, "adb");