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/adb.h>
28 #include <linux/cuda.h>
29 #include <linux/pmu.h>
30 #include <linux/notifier.h>
31 #include <linux/wait.h>
32 #include <linux/init.h>
33 #include <linux/delay.h>
34 #include <linux/spinlock.h>
35 #include <linux/completion.h>
36 #include <linux/device.h>
37 #include <linux/kthread.h>
38 #include <linux/platform_device.h>
39 #include <linux/mutex.h>
41 #include <linux/uaccess.h>
44 #include <asm/machdep.h>
48 EXPORT_SYMBOL(adb_client_list);
50 extern struct adb_driver via_macii_driver;
51 extern struct adb_driver via_maciisi_driver;
52 extern struct adb_driver via_cuda_driver;
53 extern struct adb_driver adb_iop_driver;
54 extern struct adb_driver via_pmu_driver;
55 extern struct adb_driver macio_adb_driver;
57 static DEFINE_MUTEX(adb_mutex);
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 DEFINE_SEMAPHORE(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)
198 * No devices left at address i; move the
199 * one(s) we moved to `highFree' back to i.
201 adb_request(&req, NULL, ADBREQ_SYNC, 3,
202 (highFree << 4) | 0xb,
208 /* Now fill in the handler_id field of the adb_handler entries. */
209 printk(KERN_DEBUG "adb devices:");
210 for (i = 1; i < 16; i++) {
211 if (adb_handler[i].original_address == 0)
213 adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
215 adb_handler[i].handler_id = req.reply[2];
216 printk(" [%d]: %d %x", i, adb_handler[i].original_address,
217 adb_handler[i].handler_id);
225 * This kernel task handles ADB probing. It dies once probing is
229 adb_probe_task(void *x)
231 printk(KERN_INFO "adb: starting probe task...\n");
233 printk(KERN_INFO "adb: finished probe task...\n");
235 up(&adb_probe_mutex);
241 __adb_probe_task(struct work_struct *bullshit)
243 kthread_run(adb_probe_task, NULL, "kadbprobe");
246 static DECLARE_WORK(adb_reset_work, __adb_probe_task);
251 if (__adb_probe_sync) {
256 down(&adb_probe_mutex);
257 schedule_work(&adb_reset_work);
263 * notify clients before sleep
265 static int __adb_suspend(struct platform_device *dev, pm_message_t state)
268 /* We need to get a lock on the probe thread */
269 down(&adb_probe_mutex);
271 if (adb_controller->autopoll)
272 adb_controller->autopoll(0);
273 blocking_notifier_call_chain(&adb_client_list, ADB_MSG_POWERDOWN, NULL);
278 static int adb_suspend(struct device *dev)
280 return __adb_suspend(to_platform_device(dev), PMSG_SUSPEND);
283 static int adb_freeze(struct device *dev)
285 return __adb_suspend(to_platform_device(dev), PMSG_FREEZE);
288 static int adb_poweroff(struct device *dev)
290 return __adb_suspend(to_platform_device(dev), PMSG_HIBERNATE);
294 * reset bus after sleep
296 static int __adb_resume(struct platform_device *dev)
299 up(&adb_probe_mutex);
305 static int adb_resume(struct device *dev)
307 return __adb_resume(to_platform_device(dev));
309 #endif /* CONFIG_PM */
311 static int __init adb_init(void)
313 struct adb_driver *driver;
317 if (!machine_is(chrp) && !machine_is(powermac))
325 /* xmon may do early-init */
330 adb_controller = NULL;
333 while ((driver = adb_driver_list[i++]) != NULL) {
334 if (!driver->probe()) {
335 adb_controller = driver;
339 if (adb_controller != NULL && adb_controller->init &&
340 adb_controller->init())
341 adb_controller = NULL;
342 if (adb_controller == NULL) {
343 printk(KERN_WARNING "Warning: no ADB interface detected\n");
346 if (of_machine_is_compatible("AAPL,PowerBook1998") ||
347 of_machine_is_compatible("PowerBook1,1"))
349 #endif /* CONFIG_PPC */
357 device_initcall(adb_init);
360 do_adb_reset_bus(void)
364 if (adb_controller == NULL)
367 if (adb_controller->autopoll)
368 adb_controller->autopoll(0);
370 blocking_notifier_call_chain(&adb_client_list,
371 ADB_MSG_PRE_RESET, NULL);
373 if (sleepy_trackpad) {
374 /* Let the trackpad settle down */
378 mutex_lock(&adb_handler_mutex);
379 write_lock_irq(&adb_handler_lock);
380 memset(adb_handler, 0, sizeof(adb_handler));
381 write_unlock_irq(&adb_handler_lock);
383 /* That one is still a bit synchronous, oh well... */
384 if (adb_controller->reset_bus)
385 ret = adb_controller->reset_bus();
389 if (sleepy_trackpad) {
390 /* Let the trackpad settle down */
395 autopoll_devs = adb_scan_bus();
396 if (adb_controller->autopoll)
397 adb_controller->autopoll(autopoll_devs);
399 mutex_unlock(&adb_handler_mutex);
401 blocking_notifier_call_chain(&adb_client_list,
402 ADB_MSG_POST_RESET, NULL);
410 if ((adb_controller == NULL)||(adb_controller->poll == NULL))
412 adb_controller->poll();
414 EXPORT_SYMBOL(adb_poll);
416 static void adb_sync_req_done(struct adb_request *req)
418 struct completion *comp = req->arg;
424 adb_request(struct adb_request *req, void (*done)(struct adb_request *),
425 int flags, int nbytes, ...)
430 struct completion comp;
432 if ((adb_controller == NULL) || (adb_controller->send_request == NULL))
437 req->nbytes = nbytes+1;
439 req->reply_expected = flags & ADBREQ_REPLY;
440 req->data[0] = ADB_PACKET;
441 va_start(list, nbytes);
442 for (i = 0; i < nbytes; ++i)
443 req->data[i+1] = va_arg(list, int);
446 if (flags & ADBREQ_NOSEND)
449 /* Synchronous requests block using an on-stack completion */
450 if (flags & ADBREQ_SYNC) {
452 req->done = adb_sync_req_done;
454 init_completion(&comp);
457 rc = adb_controller->send_request(req, 0);
459 if ((flags & ADBREQ_SYNC) && !rc && !req->complete)
460 wait_for_completion(&comp);
464 EXPORT_SYMBOL(adb_request);
466 /* Ultimately this should return the number of devices with
467 the given default id.
468 And it does it now ! Note: changed behaviour: This function
469 will now register if default_id _and_ handler_id both match
470 but handler_id can be left to 0 to match with default_id only.
471 When handler_id is set, this function will try to adjust
472 the handler_id id it doesn't match. */
474 adb_register(int default_id, int handler_id, struct adb_ids *ids,
475 void (*handler)(unsigned char *, int, int))
479 mutex_lock(&adb_handler_mutex);
481 for (i = 1; i < 16; i++) {
482 if ((adb_handler[i].original_address == default_id) &&
483 (!handler_id || (handler_id == adb_handler[i].handler_id) ||
484 try_handler_change(i, handler_id))) {
485 if (adb_handler[i].handler != 0) {
487 "Two handlers for ADB device %d\n",
491 write_lock_irq(&adb_handler_lock);
492 adb_handler[i].handler = handler;
493 write_unlock_irq(&adb_handler_lock);
494 ids->id[ids->nids++] = i;
497 mutex_unlock(&adb_handler_mutex);
500 EXPORT_SYMBOL(adb_register);
503 adb_unregister(int index)
507 mutex_lock(&adb_handler_mutex);
508 write_lock_irq(&adb_handler_lock);
509 if (adb_handler[index].handler) {
510 while(adb_handler[index].busy) {
511 write_unlock_irq(&adb_handler_lock);
513 write_lock_irq(&adb_handler_lock);
516 adb_handler[index].handler = NULL;
518 write_unlock_irq(&adb_handler_lock);
519 mutex_unlock(&adb_handler_mutex);
522 EXPORT_SYMBOL(adb_unregister);
525 adb_input(unsigned char *buf, int nb, int autopoll)
528 static int dump_adb_input;
531 void (*handler)(unsigned char *, int, int);
533 /* We skip keystrokes and mouse moves when the sleep process
534 * has been started. We stop autopoll, but this is another security
540 if (dump_adb_input) {
541 printk(KERN_INFO "adb packet: ");
542 for (i = 0; i < nb; ++i)
543 printk(" %x", buf[i]);
544 printk(", id = %d\n", id);
546 write_lock_irqsave(&adb_handler_lock, flags);
547 handler = adb_handler[id].handler;
549 adb_handler[id].busy = 1;
550 write_unlock_irqrestore(&adb_handler_lock, flags);
551 if (handler != NULL) {
552 (*handler)(buf, nb, autopoll);
554 adb_handler[id].busy = 0;
559 /* Try to change handler to new_id. Will return 1 if successful. */
560 static int try_handler_change(int address, int new_id)
562 struct adb_request req;
564 if (adb_handler[address].handler_id == new_id)
566 adb_request(&req, NULL, ADBREQ_SYNC, 3,
567 ADB_WRITEREG(address, 3), address | 0x20, new_id);
568 adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
569 ADB_READREG(address, 3));
570 if (req.reply_len < 2)
572 if (req.reply[2] != new_id)
574 adb_handler[address].handler_id = req.reply[2];
580 adb_try_handler_change(int address, int new_id)
584 mutex_lock(&adb_handler_mutex);
585 ret = try_handler_change(address, new_id);
586 mutex_unlock(&adb_handler_mutex);
589 EXPORT_SYMBOL(adb_try_handler_change);
592 adb_get_infos(int address, int *original_address, int *handler_id)
594 mutex_lock(&adb_handler_mutex);
595 *original_address = adb_handler[address].original_address;
596 *handler_id = adb_handler[address].handler_id;
597 mutex_unlock(&adb_handler_mutex);
599 return (*original_address != 0);
604 * /dev/adb device driver.
607 #define ADB_MAJOR 56 /* major number for /dev/adb */
609 struct adbdev_state {
612 struct adb_request *completed;
613 wait_queue_head_t wait_queue;
617 static void adb_write_done(struct adb_request *req)
619 struct adbdev_state *state = (struct adbdev_state *) req->arg;
622 if (!req->complete) {
626 spin_lock_irqsave(&state->lock, flags);
627 atomic_dec(&state->n_pending);
630 if (atomic_read(&state->n_pending) == 0) {
631 spin_unlock_irqrestore(&state->lock, flags);
636 struct adb_request **ap = &state->completed;
641 wake_up_interruptible(&state->wait_queue);
643 spin_unlock_irqrestore(&state->lock, flags);
647 do_adb_query(struct adb_request *req)
651 switch(req->data[1]) {
652 case ADB_QUERY_GETDEVINFO:
655 mutex_lock(&adb_handler_mutex);
656 req->reply[0] = adb_handler[req->data[2]].original_address;
657 req->reply[1] = adb_handler[req->data[2]].handler_id;
658 mutex_unlock(&adb_handler_mutex);
668 static int adb_open(struct inode *inode, struct file *file)
670 struct adbdev_state *state;
673 mutex_lock(&adb_mutex);
674 if (iminor(inode) > 0 || adb_controller == NULL) {
678 state = kmalloc(sizeof(struct adbdev_state), GFP_KERNEL);
683 file->private_data = state;
684 spin_lock_init(&state->lock);
685 atomic_set(&state->n_pending, 0);
686 state->completed = NULL;
687 init_waitqueue_head(&state->wait_queue);
691 mutex_unlock(&adb_mutex);
695 static int adb_release(struct inode *inode, struct file *file)
697 struct adbdev_state *state = file->private_data;
700 mutex_lock(&adb_mutex);
702 file->private_data = NULL;
703 spin_lock_irqsave(&state->lock, flags);
704 if (atomic_read(&state->n_pending) == 0
705 && state->completed == NULL) {
706 spin_unlock_irqrestore(&state->lock, flags);
710 spin_unlock_irqrestore(&state->lock, flags);
713 mutex_unlock(&adb_mutex);
717 static ssize_t adb_read(struct file *file, char __user *buf,
718 size_t count, loff_t *ppos)
721 struct adbdev_state *state = file->private_data;
722 struct adb_request *req;
723 DECLARE_WAITQUEUE(wait, current);
728 if (count > sizeof(req->reply))
729 count = sizeof(req->reply);
730 if (!access_ok(VERIFY_WRITE, buf, count))
734 spin_lock_irqsave(&state->lock, flags);
735 add_wait_queue(&state->wait_queue, &wait);
736 set_current_state(TASK_INTERRUPTIBLE);
739 req = state->completed;
741 state->completed = req->next;
742 else if (atomic_read(&state->n_pending) == 0)
744 if (req != NULL || ret != 0)
747 if (file->f_flags & O_NONBLOCK) {
751 if (signal_pending(current)) {
755 spin_unlock_irqrestore(&state->lock, flags);
757 spin_lock_irqsave(&state->lock, flags);
760 set_current_state(TASK_RUNNING);
761 remove_wait_queue(&state->wait_queue, &wait);
762 spin_unlock_irqrestore(&state->lock, flags);
767 ret = req->reply_len;
770 if (ret > 0 && copy_to_user(buf, req->reply, ret))
777 static ssize_t adb_write(struct file *file, const char __user *buf,
778 size_t count, loff_t *ppos)
781 struct adbdev_state *state = file->private_data;
782 struct adb_request *req;
784 if (count < 2 || count > sizeof(req->data))
786 if (adb_controller == NULL)
788 if (!access_ok(VERIFY_READ, buf, count))
791 req = kmalloc(sizeof(struct adb_request),
797 req->done = adb_write_done;
798 req->arg = (void *) state;
802 if (copy_from_user(req->data, buf, count))
805 atomic_inc(&state->n_pending);
807 /* If a probe is in progress or we are sleeping, wait for it to complete */
808 down(&adb_probe_mutex);
810 /* Queries are special requests sent to the ADB driver itself */
811 if (req->data[0] == ADB_QUERY) {
813 ret = do_adb_query(req);
816 up(&adb_probe_mutex);
818 /* Special case for ADB_BUSRESET request, all others are sent to
820 else if ((req->data[0] == ADB_PACKET) && (count > 1)
821 && (req->data[1] == ADB_BUSRESET)) {
822 ret = do_adb_reset_bus();
823 up(&adb_probe_mutex);
824 atomic_dec(&state->n_pending);
829 req->reply_expected = ((req->data[1] & 0xc) == 0xc);
830 if (adb_controller && adb_controller->send_request)
831 ret = adb_controller->send_request(req, 0);
834 up(&adb_probe_mutex);
838 atomic_dec(&state->n_pending);
848 static const struct file_operations adb_fops = {
849 .owner = THIS_MODULE,
854 .release = adb_release,
858 static const struct dev_pm_ops adb_dev_pm_ops = {
859 .suspend = adb_suspend,
860 .resume = adb_resume,
861 /* Hibernate hooks */
862 .freeze = adb_freeze,
864 .poweroff = adb_poweroff,
865 .restore = adb_resume,
869 static struct platform_driver adb_pfdrv = {
873 .pm = &adb_dev_pm_ops,
878 static struct platform_device adb_pfdev = {
883 adb_dummy_probe(struct platform_device *dev)
885 if (dev == &adb_pfdev)
893 if (register_chrdev(ADB_MAJOR, "adb", &adb_fops)) {
894 printk(KERN_ERR "adb: unable to get major %d\n", ADB_MAJOR);
898 adb_dev_class = class_create(THIS_MODULE, "adb");
899 if (IS_ERR(adb_dev_class))
901 device_create(adb_dev_class, NULL, MKDEV(ADB_MAJOR, 0), NULL, "adb");
903 platform_device_register(&adb_pfdev);
904 platform_driver_probe(&adb_pfdrv, adb_dummy_probe);