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/types.h>
20 #include <linux/errno.h>
21 #include <linux/kernel.h>
22 #include <linux/slab.h>
23 #include <linux/module.h>
26 #include <linux/sched.h>
27 #include <linux/smp_lock.h>
28 #include <linux/adb.h>
29 #include <linux/cuda.h>
30 #include <linux/pmu.h>
31 #include <linux/notifier.h>
32 #include <linux/wait.h>
33 #include <linux/init.h>
34 #include <linux/delay.h>
35 #include <linux/spinlock.h>
36 #include <linux/completion.h>
37 #include <linux/device.h>
38 #include <linux/kthread.h>
39 #include <linux/platform_device.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 DECLARE_MUTEX(adb_probe_mutex);
88 static int sleepy_trackpad;
89 static int autopoll_devs;
92 static int adb_scan_bus(void);
93 static int do_adb_reset_bus(void);
94 static void adbdev_init(void);
95 static int try_handler_change(int, int);
97 static struct adb_handler {
98 void (*handler)(unsigned char *, int, int);
105 * The adb_handler_sem mutex protects all accesses to the original_address
106 * and handler_id fields of adb_handler[i] for all i, and changes to the
108 * Accesses to the handler field are protected by the adb_handler_lock
109 * rwlock. It is held across all calls to any handler, so that by the
110 * time adb_unregister returns, we know that the old handler isn't being
113 static DECLARE_MUTEX(adb_handler_sem);
114 static DEFINE_RWLOCK(adb_handler_lock);
117 static void printADBreply(struct adb_request *req)
121 printk("adb reply (%d)", req->reply_len);
122 for(i = 0; i < req->reply_len; i++)
123 printk(" %x", req->reply[i]);
129 static int adb_scan_bus(void)
131 int i, highFree=0, noMovement;
133 struct adb_request req;
135 /* assumes adb_handler[] is all zeroes at this point */
136 for (i = 1; i < 16; i++) {
137 /* see if there is anything at address i */
138 adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
140 if (req.reply_len > 1)
141 /* one or more devices at this address */
142 adb_handler[i].original_address = i;
143 else if (i > highFree)
147 /* Note we reset noMovement to 0 each time we move a device */
148 for (noMovement = 1; noMovement < 2 && highFree > 0; noMovement++) {
149 for (i = 1; i < 16; i++) {
150 if (adb_handler[i].original_address == 0)
153 * Send a "talk register 3" command to address i
154 * to provoke a collision if there is more than
155 * one device at this address.
157 adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
160 * Move the device(s) which didn't detect a
161 * collision to address `highFree'. Hopefully
162 * this only moves one device.
164 adb_request(&req, NULL, ADBREQ_SYNC, 3,
165 (i<< 4) | 0xb, (highFree | 0x60), 0xfe);
167 * See if anybody actually moved. This is suggested
170 * http://developer.apple.com/technotes/hw/hw_01.html
172 adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
173 (highFree << 4) | 0xf);
174 if (req.reply_len <= 1) continue;
176 * Test whether there are any device(s) left
179 adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
181 if (req.reply_len > 1) {
183 * There are still one or more devices
184 * left at address i. Register the one(s)
185 * we moved to `highFree', and find a new
186 * value for highFree.
188 adb_handler[highFree].original_address =
189 adb_handler[i].original_address;
190 while (highFree > 0 &&
191 adb_handler[highFree].original_address)
200 * No devices left at address i; move the
201 * one(s) we moved to `highFree' back to i.
203 adb_request(&req, NULL, ADBREQ_SYNC, 3,
204 (highFree << 4) | 0xb,
210 /* Now fill in the handler_id field of the adb_handler entries. */
211 printk(KERN_DEBUG "adb devices:");
212 for (i = 1; i < 16; i++) {
213 if (adb_handler[i].original_address == 0)
215 adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
217 adb_handler[i].handler_id = req.reply[2];
218 printk(" [%d]: %d %x", i, adb_handler[i].original_address,
219 adb_handler[i].handler_id);
227 * This kernel task handles ADB probing. It dies once probing is
231 adb_probe_task(void *x)
233 printk(KERN_INFO "adb: starting probe task...\n");
235 printk(KERN_INFO "adb: finished probe task...\n");
237 up(&adb_probe_mutex);
243 __adb_probe_task(struct work_struct *bullshit)
245 kthread_run(adb_probe_task, NULL, "kadbprobe");
248 static DECLARE_WORK(adb_reset_work, __adb_probe_task);
253 if (__adb_probe_sync) {
258 down(&adb_probe_mutex);
259 schedule_work(&adb_reset_work);
265 * notify clients before sleep
267 static int adb_suspend(struct platform_device *dev, pm_message_t state)
270 /* We need to get a lock on the probe thread */
271 down(&adb_probe_mutex);
273 if (adb_controller->autopoll)
274 adb_controller->autopoll(0);
275 blocking_notifier_call_chain(&adb_client_list, ADB_MSG_POWERDOWN, NULL);
281 * reset bus after sleep
283 static int adb_resume(struct platform_device *dev)
286 up(&adb_probe_mutex);
291 #endif /* CONFIG_PM */
293 int __init adb_init(void)
295 struct adb_driver *driver;
299 if (!machine_is(chrp) && !machine_is(powermac))
303 if (!machine_is(powermac))
311 /* xmon may do early-init */
316 adb_controller = NULL;
319 while ((driver = adb_driver_list[i++]) != NULL) {
320 if (!driver->probe()) {
321 adb_controller = driver;
325 if ((adb_controller == NULL) || adb_controller->init()) {
326 printk(KERN_WARNING "Warning: no ADB interface detected\n");
327 adb_controller = NULL;
330 if (machine_is_compatible("AAPL,PowerBook1998") ||
331 machine_is_compatible("PowerBook1,1"))
333 #endif /* CONFIG_PPC */
341 __initcall(adb_init);
344 do_adb_reset_bus(void)
348 if (adb_controller == NULL)
351 if (adb_controller->autopoll)
352 adb_controller->autopoll(0);
354 blocking_notifier_call_chain(&adb_client_list,
355 ADB_MSG_PRE_RESET, NULL);
357 if (sleepy_trackpad) {
358 /* Let the trackpad settle down */
362 down(&adb_handler_sem);
363 write_lock_irq(&adb_handler_lock);
364 memset(adb_handler, 0, sizeof(adb_handler));
365 write_unlock_irq(&adb_handler_lock);
367 /* That one is still a bit synchronous, oh well... */
368 if (adb_controller->reset_bus)
369 ret = adb_controller->reset_bus();
373 if (sleepy_trackpad) {
374 /* Let the trackpad settle down */
379 autopoll_devs = adb_scan_bus();
380 if (adb_controller->autopoll)
381 adb_controller->autopoll(autopoll_devs);
383 up(&adb_handler_sem);
385 blocking_notifier_call_chain(&adb_client_list,
386 ADB_MSG_POST_RESET, NULL);
394 if ((adb_controller == NULL)||(adb_controller->poll == NULL))
396 adb_controller->poll();
399 static void adb_sync_req_done(struct adb_request *req)
401 struct completion *comp = req->arg;
407 adb_request(struct adb_request *req, void (*done)(struct adb_request *),
408 int flags, int nbytes, ...)
413 struct completion comp;
415 if ((adb_controller == NULL) || (adb_controller->send_request == NULL))
420 req->nbytes = nbytes+1;
422 req->reply_expected = flags & ADBREQ_REPLY;
423 req->data[0] = ADB_PACKET;
424 va_start(list, nbytes);
425 for (i = 0; i < nbytes; ++i)
426 req->data[i+1] = va_arg(list, int);
429 if (flags & ADBREQ_NOSEND)
432 /* Synchronous requests block using an on-stack completion */
433 if (flags & ADBREQ_SYNC) {
435 req->done = adb_sync_req_done;
437 init_completion(&comp);
440 rc = adb_controller->send_request(req, 0);
442 if ((flags & ADBREQ_SYNC) && !rc && !req->complete)
443 wait_for_completion(&comp);
448 /* Ultimately this should return the number of devices with
449 the given default id.
450 And it does it now ! Note: changed behaviour: This function
451 will now register if default_id _and_ handler_id both match
452 but handler_id can be left to 0 to match with default_id only.
453 When handler_id is set, this function will try to adjust
454 the handler_id id it doesn't match. */
456 adb_register(int default_id, int handler_id, struct adb_ids *ids,
457 void (*handler)(unsigned char *, int, int))
461 down(&adb_handler_sem);
463 for (i = 1; i < 16; i++) {
464 if ((adb_handler[i].original_address == default_id) &&
465 (!handler_id || (handler_id == adb_handler[i].handler_id) ||
466 try_handler_change(i, handler_id))) {
467 if (adb_handler[i].handler != 0) {
469 "Two handlers for ADB device %d\n",
473 write_lock_irq(&adb_handler_lock);
474 adb_handler[i].handler = handler;
475 write_unlock_irq(&adb_handler_lock);
476 ids->id[ids->nids++] = i;
479 up(&adb_handler_sem);
484 adb_unregister(int index)
488 down(&adb_handler_sem);
489 write_lock_irq(&adb_handler_lock);
490 if (adb_handler[index].handler) {
491 while(adb_handler[index].busy) {
492 write_unlock_irq(&adb_handler_lock);
494 write_lock_irq(&adb_handler_lock);
497 adb_handler[index].handler = NULL;
499 write_unlock_irq(&adb_handler_lock);
500 up(&adb_handler_sem);
505 adb_input(unsigned char *buf, int nb, int autopoll)
508 static int dump_adb_input = 0;
511 void (*handler)(unsigned char *, int, int);
513 /* We skip keystrokes and mouse moves when the sleep process
514 * has been started. We stop autopoll, but this is another security
520 if (dump_adb_input) {
521 printk(KERN_INFO "adb packet: ");
522 for (i = 0; i < nb; ++i)
523 printk(" %x", buf[i]);
524 printk(", id = %d\n", id);
526 write_lock_irqsave(&adb_handler_lock, flags);
527 handler = adb_handler[id].handler;
529 adb_handler[id].busy = 1;
530 write_unlock_irqrestore(&adb_handler_lock, flags);
531 if (handler != NULL) {
532 (*handler)(buf, nb, autopoll);
534 adb_handler[id].busy = 0;
539 /* Try to change handler to new_id. Will return 1 if successful. */
540 static int try_handler_change(int address, int new_id)
542 struct adb_request req;
544 if (adb_handler[address].handler_id == new_id)
546 adb_request(&req, NULL, ADBREQ_SYNC, 3,
547 ADB_WRITEREG(address, 3), address | 0x20, new_id);
548 adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
549 ADB_READREG(address, 3));
550 if (req.reply_len < 2)
552 if (req.reply[2] != new_id)
554 adb_handler[address].handler_id = req.reply[2];
560 adb_try_handler_change(int address, int new_id)
564 down(&adb_handler_sem);
565 ret = try_handler_change(address, new_id);
566 up(&adb_handler_sem);
571 adb_get_infos(int address, int *original_address, int *handler_id)
573 down(&adb_handler_sem);
574 *original_address = adb_handler[address].original_address;
575 *handler_id = adb_handler[address].handler_id;
576 up(&adb_handler_sem);
578 return (*original_address != 0);
583 * /dev/adb device driver.
586 #define ADB_MAJOR 56 /* major number for /dev/adb */
588 struct adbdev_state {
591 struct adb_request *completed;
592 wait_queue_head_t wait_queue;
596 static void adb_write_done(struct adb_request *req)
598 struct adbdev_state *state = (struct adbdev_state *) req->arg;
601 if (!req->complete) {
605 spin_lock_irqsave(&state->lock, flags);
606 atomic_dec(&state->n_pending);
609 if (atomic_read(&state->n_pending) == 0) {
610 spin_unlock_irqrestore(&state->lock, flags);
615 struct adb_request **ap = &state->completed;
620 wake_up_interruptible(&state->wait_queue);
622 spin_unlock_irqrestore(&state->lock, flags);
626 do_adb_query(struct adb_request *req)
632 case ADB_QUERY_GETDEVINFO:
635 down(&adb_handler_sem);
636 req->reply[0] = adb_handler[req->data[2]].original_address;
637 req->reply[1] = adb_handler[req->data[2]].handler_id;
638 up(&adb_handler_sem);
648 static int adb_open(struct inode *inode, struct file *file)
650 struct adbdev_state *state;
652 if (iminor(inode) > 0 || adb_controller == NULL)
654 state = kmalloc(sizeof(struct adbdev_state), GFP_KERNEL);
657 file->private_data = state;
658 spin_lock_init(&state->lock);
659 atomic_set(&state->n_pending, 0);
660 state->completed = NULL;
661 init_waitqueue_head(&state->wait_queue);
667 static int adb_release(struct inode *inode, struct file *file)
669 struct adbdev_state *state = file->private_data;
674 file->private_data = NULL;
675 spin_lock_irqsave(&state->lock, flags);
676 if (atomic_read(&state->n_pending) == 0
677 && state->completed == NULL) {
678 spin_unlock_irqrestore(&state->lock, flags);
682 spin_unlock_irqrestore(&state->lock, flags);
689 static ssize_t adb_read(struct file *file, char __user *buf,
690 size_t count, loff_t *ppos)
693 struct adbdev_state *state = file->private_data;
694 struct adb_request *req;
695 wait_queue_t wait = __WAITQUEUE_INITIALIZER(wait,current);
700 if (count > sizeof(req->reply))
701 count = sizeof(req->reply);
702 if (!access_ok(VERIFY_WRITE, buf, count))
706 spin_lock_irqsave(&state->lock, flags);
707 add_wait_queue(&state->wait_queue, &wait);
708 current->state = TASK_INTERRUPTIBLE;
711 req = state->completed;
713 state->completed = req->next;
714 else if (atomic_read(&state->n_pending) == 0)
716 if (req != NULL || ret != 0)
719 if (file->f_flags & O_NONBLOCK) {
723 if (signal_pending(current)) {
727 spin_unlock_irqrestore(&state->lock, flags);
729 spin_lock_irqsave(&state->lock, flags);
732 current->state = TASK_RUNNING;
733 remove_wait_queue(&state->wait_queue, &wait);
734 spin_unlock_irqrestore(&state->lock, flags);
739 ret = req->reply_len;
742 if (ret > 0 && copy_to_user(buf, req->reply, ret))
749 static ssize_t adb_write(struct file *file, const char __user *buf,
750 size_t count, loff_t *ppos)
753 struct adbdev_state *state = file->private_data;
754 struct adb_request *req;
756 if (count < 2 || count > sizeof(req->data))
758 if (adb_controller == NULL)
760 if (!access_ok(VERIFY_READ, buf, count))
763 req = kmalloc(sizeof(struct adb_request),
769 req->done = adb_write_done;
770 req->arg = (void *) state;
774 if (copy_from_user(req->data, buf, count))
777 atomic_inc(&state->n_pending);
779 /* If a probe is in progress or we are sleeping, wait for it to complete */
780 down(&adb_probe_mutex);
782 /* Queries are special requests sent to the ADB driver itself */
783 if (req->data[0] == ADB_QUERY) {
785 ret = do_adb_query(req);
788 up(&adb_probe_mutex);
790 /* Special case for ADB_BUSRESET request, all others are sent to
792 else if ((req->data[0] == ADB_PACKET)&&(count > 1)
793 &&(req->data[1] == ADB_BUSRESET)) {
794 ret = do_adb_reset_bus();
795 up(&adb_probe_mutex);
796 atomic_dec(&state->n_pending);
801 req->reply_expected = ((req->data[1] & 0xc) == 0xc);
802 if (adb_controller && adb_controller->send_request)
803 ret = adb_controller->send_request(req, 0);
806 up(&adb_probe_mutex);
810 atomic_dec(&state->n_pending);
820 static const struct file_operations adb_fops = {
821 .owner = THIS_MODULE,
826 .release = adb_release,
829 static struct platform_driver adb_pfdrv = {
834 .suspend = adb_suspend,
835 .resume = adb_resume,
839 static struct platform_device adb_pfdev = {
844 adb_dummy_probe(struct platform_device *dev)
846 if (dev == &adb_pfdev)
854 if (register_chrdev(ADB_MAJOR, "adb", &adb_fops)) {
855 printk(KERN_ERR "adb: unable to get major %d\n", ADB_MAJOR);
859 adb_dev_class = class_create(THIS_MODULE, "adb");
860 if (IS_ERR(adb_dev_class))
862 device_create(adb_dev_class, NULL, MKDEV(ADB_MAJOR, 0), "adb");
864 platform_device_register(&adb_pfdev);
865 platform_driver_probe(&adb_pfdrv, adb_dummy_probe);