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>
40 #include <linux/mutex.h>
42 #include <asm/uaccess.h>
45 #include <asm/machdep.h>
49 EXPORT_SYMBOL(adb_client_list);
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;
58 static struct adb_driver *adb_driver_list[] = {
59 #ifdef CONFIG_ADB_MACII
62 #ifdef CONFIG_ADB_MACIISI
65 #ifdef CONFIG_ADB_CUDA
71 #if defined(CONFIG_ADB_PMU) || defined(CONFIG_ADB_PMU68K)
74 #ifdef CONFIG_ADB_MACIO
80 static struct class *adb_dev_class;
82 static struct adb_driver *adb_controller;
83 BLOCKING_NOTIFIER_HEAD(adb_client_list);
84 static int adb_got_sleep;
85 static int adb_inited;
86 static DECLARE_MUTEX(adb_probe_mutex);
87 static int sleepy_trackpad;
88 static int autopoll_devs;
91 static int adb_scan_bus(void);
92 static int do_adb_reset_bus(void);
93 static void adbdev_init(void);
94 static int try_handler_change(int, int);
96 static struct adb_handler {
97 void (*handler)(unsigned char *, int, int);
104 * The adb_handler_mutex mutex protects all accesses to the original_address
105 * and handler_id fields of adb_handler[i] for all i, and changes to the
107 * Accesses to the handler field are protected by the adb_handler_lock
108 * rwlock. It is held across all calls to any handler, so that by the
109 * time adb_unregister returns, we know that the old handler isn't being
112 static DEFINE_MUTEX(adb_handler_mutex);
113 static DEFINE_RWLOCK(adb_handler_lock);
116 static void printADBreply(struct adb_request *req)
120 printk("adb reply (%d)", req->reply_len);
121 for(i = 0; i < req->reply_len; i++)
122 printk(" %x", req->reply[i]);
128 static int adb_scan_bus(void)
130 int i, highFree=0, noMovement;
132 struct adb_request req;
134 /* assumes adb_handler[] is all zeroes at this point */
135 for (i = 1; i < 16; i++) {
136 /* see if there is anything at address i */
137 adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
139 if (req.reply_len > 1)
140 /* one or more devices at this address */
141 adb_handler[i].original_address = i;
142 else if (i > highFree)
146 /* Note we reset noMovement to 0 each time we move a device */
147 for (noMovement = 1; noMovement < 2 && highFree > 0; noMovement++) {
148 for (i = 1; i < 16; i++) {
149 if (adb_handler[i].original_address == 0)
152 * Send a "talk register 3" command to address i
153 * to provoke a collision if there is more than
154 * one device at this address.
156 adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
159 * Move the device(s) which didn't detect a
160 * collision to address `highFree'. Hopefully
161 * this only moves one device.
163 adb_request(&req, NULL, ADBREQ_SYNC, 3,
164 (i<< 4) | 0xb, (highFree | 0x60), 0xfe);
166 * See if anybody actually moved. This is suggested
169 * http://developer.apple.com/technotes/hw/hw_01.html
171 adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
172 (highFree << 4) | 0xf);
173 if (req.reply_len <= 1) continue;
175 * Test whether there are any device(s) left
178 adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
180 if (req.reply_len > 1) {
182 * There are still one or more devices
183 * left at address i. Register the one(s)
184 * we moved to `highFree', and find a new
185 * value for highFree.
187 adb_handler[highFree].original_address =
188 adb_handler[i].original_address;
189 while (highFree > 0 &&
190 adb_handler[highFree].original_address)
199 * No devices left at address i; move the
200 * one(s) we moved to `highFree' back to i.
202 adb_request(&req, NULL, ADBREQ_SYNC, 3,
203 (highFree << 4) | 0xb,
209 /* Now fill in the handler_id field of the adb_handler entries. */
210 printk(KERN_DEBUG "adb devices:");
211 for (i = 1; i < 16; i++) {
212 if (adb_handler[i].original_address == 0)
214 adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
216 adb_handler[i].handler_id = req.reply[2];
217 printk(" [%d]: %d %x", i, adb_handler[i].original_address,
218 adb_handler[i].handler_id);
226 * This kernel task handles ADB probing. It dies once probing is
230 adb_probe_task(void *x)
232 printk(KERN_INFO "adb: starting probe task...\n");
234 printk(KERN_INFO "adb: finished probe task...\n");
236 up(&adb_probe_mutex);
242 __adb_probe_task(struct work_struct *bullshit)
244 kthread_run(adb_probe_task, NULL, "kadbprobe");
247 static DECLARE_WORK(adb_reset_work, __adb_probe_task);
252 if (__adb_probe_sync) {
257 down(&adb_probe_mutex);
258 schedule_work(&adb_reset_work);
264 * notify clients before sleep
266 static int adb_suspend(struct platform_device *dev, pm_message_t state)
269 /* We need to get a lock on the probe thread */
270 down(&adb_probe_mutex);
272 if (adb_controller->autopoll)
273 adb_controller->autopoll(0);
274 blocking_notifier_call_chain(&adb_client_list, ADB_MSG_POWERDOWN, NULL);
280 * reset bus after sleep
282 static int adb_resume(struct platform_device *dev)
285 up(&adb_probe_mutex);
290 #endif /* CONFIG_PM */
292 static 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 if (machine_is_compatible("AAPL,PowerBook1998") ||
326 machine_is_compatible("PowerBook1,1"))
328 #endif /* CONFIG_PPC */
336 device_initcall(adb_init);
339 do_adb_reset_bus(void)
343 if (adb_controller == NULL)
346 if (adb_controller->autopoll)
347 adb_controller->autopoll(0);
349 blocking_notifier_call_chain(&adb_client_list,
350 ADB_MSG_PRE_RESET, NULL);
352 if (sleepy_trackpad) {
353 /* Let the trackpad settle down */
357 mutex_lock(&adb_handler_mutex);
358 write_lock_irq(&adb_handler_lock);
359 memset(adb_handler, 0, sizeof(adb_handler));
360 write_unlock_irq(&adb_handler_lock);
362 /* That one is still a bit synchronous, oh well... */
363 if (adb_controller->reset_bus)
364 ret = adb_controller->reset_bus();
368 if (sleepy_trackpad) {
369 /* Let the trackpad settle down */
374 autopoll_devs = adb_scan_bus();
375 if (adb_controller->autopoll)
376 adb_controller->autopoll(autopoll_devs);
378 mutex_unlock(&adb_handler_mutex);
380 blocking_notifier_call_chain(&adb_client_list,
381 ADB_MSG_POST_RESET, NULL);
389 if ((adb_controller == NULL)||(adb_controller->poll == NULL))
391 adb_controller->poll();
394 static void adb_sync_req_done(struct adb_request *req)
396 struct completion *comp = req->arg;
402 adb_request(struct adb_request *req, void (*done)(struct adb_request *),
403 int flags, int nbytes, ...)
408 struct completion comp;
410 if ((adb_controller == NULL) || (adb_controller->send_request == NULL))
415 req->nbytes = nbytes+1;
417 req->reply_expected = flags & ADBREQ_REPLY;
418 req->data[0] = ADB_PACKET;
419 va_start(list, nbytes);
420 for (i = 0; i < nbytes; ++i)
421 req->data[i+1] = va_arg(list, int);
424 if (flags & ADBREQ_NOSEND)
427 /* Synchronous requests block using an on-stack completion */
428 if (flags & ADBREQ_SYNC) {
430 req->done = adb_sync_req_done;
432 init_completion(&comp);
435 rc = adb_controller->send_request(req, 0);
437 if ((flags & ADBREQ_SYNC) && !rc && !req->complete)
438 wait_for_completion(&comp);
443 /* Ultimately this should return the number of devices with
444 the given default id.
445 And it does it now ! Note: changed behaviour: This function
446 will now register if default_id _and_ handler_id both match
447 but handler_id can be left to 0 to match with default_id only.
448 When handler_id is set, this function will try to adjust
449 the handler_id id it doesn't match. */
451 adb_register(int default_id, int handler_id, struct adb_ids *ids,
452 void (*handler)(unsigned char *, int, int))
456 mutex_lock(&adb_handler_mutex);
458 for (i = 1; i < 16; i++) {
459 if ((adb_handler[i].original_address == default_id) &&
460 (!handler_id || (handler_id == adb_handler[i].handler_id) ||
461 try_handler_change(i, handler_id))) {
462 if (adb_handler[i].handler != 0) {
464 "Two handlers for ADB device %d\n",
468 write_lock_irq(&adb_handler_lock);
469 adb_handler[i].handler = handler;
470 write_unlock_irq(&adb_handler_lock);
471 ids->id[ids->nids++] = i;
474 mutex_unlock(&adb_handler_mutex);
479 adb_unregister(int index)
483 mutex_lock(&adb_handler_mutex);
484 write_lock_irq(&adb_handler_lock);
485 if (adb_handler[index].handler) {
486 while(adb_handler[index].busy) {
487 write_unlock_irq(&adb_handler_lock);
489 write_lock_irq(&adb_handler_lock);
492 adb_handler[index].handler = NULL;
494 write_unlock_irq(&adb_handler_lock);
495 mutex_unlock(&adb_handler_mutex);
500 adb_input(unsigned char *buf, int nb, int autopoll)
503 static int dump_adb_input = 0;
506 void (*handler)(unsigned char *, int, int);
508 /* We skip keystrokes and mouse moves when the sleep process
509 * has been started. We stop autopoll, but this is another security
515 if (dump_adb_input) {
516 printk(KERN_INFO "adb packet: ");
517 for (i = 0; i < nb; ++i)
518 printk(" %x", buf[i]);
519 printk(", id = %d\n", id);
521 write_lock_irqsave(&adb_handler_lock, flags);
522 handler = adb_handler[id].handler;
524 adb_handler[id].busy = 1;
525 write_unlock_irqrestore(&adb_handler_lock, flags);
526 if (handler != NULL) {
527 (*handler)(buf, nb, autopoll);
529 adb_handler[id].busy = 0;
534 /* Try to change handler to new_id. Will return 1 if successful. */
535 static int try_handler_change(int address, int new_id)
537 struct adb_request req;
539 if (adb_handler[address].handler_id == new_id)
541 adb_request(&req, NULL, ADBREQ_SYNC, 3,
542 ADB_WRITEREG(address, 3), address | 0x20, new_id);
543 adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
544 ADB_READREG(address, 3));
545 if (req.reply_len < 2)
547 if (req.reply[2] != new_id)
549 adb_handler[address].handler_id = req.reply[2];
555 adb_try_handler_change(int address, int new_id)
559 mutex_lock(&adb_handler_mutex);
560 ret = try_handler_change(address, new_id);
561 mutex_unlock(&adb_handler_mutex);
566 adb_get_infos(int address, int *original_address, int *handler_id)
568 mutex_lock(&adb_handler_mutex);
569 *original_address = adb_handler[address].original_address;
570 *handler_id = adb_handler[address].handler_id;
571 mutex_unlock(&adb_handler_mutex);
573 return (*original_address != 0);
578 * /dev/adb device driver.
581 #define ADB_MAJOR 56 /* major number for /dev/adb */
583 struct adbdev_state {
586 struct adb_request *completed;
587 wait_queue_head_t wait_queue;
591 static void adb_write_done(struct adb_request *req)
593 struct adbdev_state *state = (struct adbdev_state *) req->arg;
596 if (!req->complete) {
600 spin_lock_irqsave(&state->lock, flags);
601 atomic_dec(&state->n_pending);
604 if (atomic_read(&state->n_pending) == 0) {
605 spin_unlock_irqrestore(&state->lock, flags);
610 struct adb_request **ap = &state->completed;
615 wake_up_interruptible(&state->wait_queue);
617 spin_unlock_irqrestore(&state->lock, flags);
621 do_adb_query(struct adb_request *req)
627 case ADB_QUERY_GETDEVINFO:
630 mutex_lock(&adb_handler_mutex);
631 req->reply[0] = adb_handler[req->data[2]].original_address;
632 req->reply[1] = adb_handler[req->data[2]].handler_id;
633 mutex_unlock(&adb_handler_mutex);
643 static int adb_open(struct inode *inode, struct file *file)
645 struct adbdev_state *state;
649 if (iminor(inode) > 0 || adb_controller == NULL) {
653 state = kmalloc(sizeof(struct adbdev_state), GFP_KERNEL);
658 file->private_data = state;
659 spin_lock_init(&state->lock);
660 atomic_set(&state->n_pending, 0);
661 state->completed = NULL;
662 init_waitqueue_head(&state->wait_queue);
670 static int adb_release(struct inode *inode, struct file *file)
672 struct adbdev_state *state = file->private_data;
677 file->private_data = NULL;
678 spin_lock_irqsave(&state->lock, flags);
679 if (atomic_read(&state->n_pending) == 0
680 && state->completed == NULL) {
681 spin_unlock_irqrestore(&state->lock, flags);
685 spin_unlock_irqrestore(&state->lock, flags);
692 static ssize_t adb_read(struct file *file, char __user *buf,
693 size_t count, loff_t *ppos)
696 struct adbdev_state *state = file->private_data;
697 struct adb_request *req;
698 wait_queue_t wait = __WAITQUEUE_INITIALIZER(wait,current);
703 if (count > sizeof(req->reply))
704 count = sizeof(req->reply);
705 if (!access_ok(VERIFY_WRITE, buf, count))
709 spin_lock_irqsave(&state->lock, flags);
710 add_wait_queue(&state->wait_queue, &wait);
711 current->state = TASK_INTERRUPTIBLE;
714 req = state->completed;
716 state->completed = req->next;
717 else if (atomic_read(&state->n_pending) == 0)
719 if (req != NULL || ret != 0)
722 if (file->f_flags & O_NONBLOCK) {
726 if (signal_pending(current)) {
730 spin_unlock_irqrestore(&state->lock, flags);
732 spin_lock_irqsave(&state->lock, flags);
735 current->state = TASK_RUNNING;
736 remove_wait_queue(&state->wait_queue, &wait);
737 spin_unlock_irqrestore(&state->lock, flags);
742 ret = req->reply_len;
745 if (ret > 0 && copy_to_user(buf, req->reply, ret))
752 static ssize_t adb_write(struct file *file, const char __user *buf,
753 size_t count, loff_t *ppos)
756 struct adbdev_state *state = file->private_data;
757 struct adb_request *req;
759 if (count < 2 || count > sizeof(req->data))
761 if (adb_controller == NULL)
763 if (!access_ok(VERIFY_READ, buf, count))
766 req = kmalloc(sizeof(struct adb_request),
772 req->done = adb_write_done;
773 req->arg = (void *) state;
777 if (copy_from_user(req->data, buf, count))
780 atomic_inc(&state->n_pending);
782 /* If a probe is in progress or we are sleeping, wait for it to complete */
783 down(&adb_probe_mutex);
785 /* Queries are special requests sent to the ADB driver itself */
786 if (req->data[0] == ADB_QUERY) {
788 ret = do_adb_query(req);
791 up(&adb_probe_mutex);
793 /* Special case for ADB_BUSRESET request, all others are sent to
795 else if ((req->data[0] == ADB_PACKET)&&(count > 1)
796 &&(req->data[1] == ADB_BUSRESET)) {
797 ret = do_adb_reset_bus();
798 up(&adb_probe_mutex);
799 atomic_dec(&state->n_pending);
804 req->reply_expected = ((req->data[1] & 0xc) == 0xc);
805 if (adb_controller && adb_controller->send_request)
806 ret = adb_controller->send_request(req, 0);
809 up(&adb_probe_mutex);
813 atomic_dec(&state->n_pending);
823 static const struct file_operations adb_fops = {
824 .owner = THIS_MODULE,
829 .release = adb_release,
832 static struct platform_driver adb_pfdrv = {
837 .suspend = adb_suspend,
838 .resume = adb_resume,
842 static struct platform_device adb_pfdev = {
847 adb_dummy_probe(struct platform_device *dev)
849 if (dev == &adb_pfdev)
857 if (register_chrdev(ADB_MAJOR, "adb", &adb_fops)) {
858 printk(KERN_ERR "adb: unable to get major %d\n", ADB_MAJOR);
862 adb_dev_class = class_create(THIS_MODULE, "adb");
863 if (IS_ERR(adb_dev_class))
865 device_create_drvdata(adb_dev_class, NULL, MKDEV(ADB_MAJOR, 0), NULL,
868 platform_device_register(&adb_pfdev);
869 platform_driver_probe(&adb_pfdrv, adb_dummy_probe);