1 // SPDX-License-Identifier: GPL-2.0
3 * Device driver for the Apple Desktop Bus
4 * and the /dev/adb device on macintoshes.
6 * Copyright (C) 1996 Paul Mackerras.
8 * Modified to declare controllers as structures, added
9 * client notification of bus reset and handles PowerBook
10 * sleep, by Benjamin Herrenschmidt.
14 * - /sys/bus/adb to list the devices and infos
15 * - more /dev/adb to allow userland to receive the
16 * flow of auto-polling datas from a given device.
17 * - move bus probe to a kernel thread
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/signal.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 <linux/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_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_CUDA
68 #if defined(CONFIG_ADB_PMU) || defined(CONFIG_ADB_PMU68K)
71 #ifdef CONFIG_ADB_MACIO
77 static struct class *adb_dev_class;
79 static struct adb_driver *adb_controller;
80 BLOCKING_NOTIFIER_HEAD(adb_client_list);
81 static int adb_got_sleep;
82 static int adb_inited;
83 static DEFINE_SEMAPHORE(adb_probe_mutex);
84 static int sleepy_trackpad;
85 static int autopoll_devs;
88 static int adb_scan_bus(void);
89 static int do_adb_reset_bus(void);
90 static void adbdev_init(void);
91 static int try_handler_change(int, int);
93 static struct adb_handler {
94 void (*handler)(unsigned char *, int, int);
101 * The adb_handler_mutex mutex protects all accesses to the original_address
102 * and handler_id fields of adb_handler[i] for all i, and changes to the
104 * Accesses to the handler field are protected by the adb_handler_lock
105 * rwlock. It is held across all calls to any handler, so that by the
106 * time adb_unregister returns, we know that the old handler isn't being
109 static DEFINE_MUTEX(adb_handler_mutex);
110 static DEFINE_RWLOCK(adb_handler_lock);
113 static void printADBreply(struct adb_request *req)
117 printk("adb reply (%d)", req->reply_len);
118 for(i = 0; i < req->reply_len; i++)
119 printk(" %x", req->reply[i]);
125 static int adb_scan_bus(void)
127 int i, highFree=0, noMovement;
129 struct adb_request req;
131 /* assumes adb_handler[] is all zeroes at this point */
132 for (i = 1; i < 16; i++) {
133 /* see if there is anything at address i */
134 adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
136 if (req.reply_len > 1)
137 /* one or more devices at this address */
138 adb_handler[i].original_address = i;
139 else if (i > highFree)
143 /* Note we reset noMovement to 0 each time we move a device */
144 for (noMovement = 1; noMovement < 2 && highFree > 0; noMovement++) {
145 for (i = 1; i < 16; i++) {
146 if (adb_handler[i].original_address == 0)
149 * Send a "talk register 3" command to address i
150 * to provoke a collision if there is more than
151 * one device at this address.
153 adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
156 * Move the device(s) which didn't detect a
157 * collision to address `highFree'. Hopefully
158 * this only moves one device.
160 adb_request(&req, NULL, ADBREQ_SYNC, 3,
161 (i<< 4) | 0xb, (highFree | 0x60), 0xfe);
163 * See if anybody actually moved. This is suggested
166 * http://developer.apple.com/technotes/hw/hw_01.html
168 adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
169 (highFree << 4) | 0xf);
170 if (req.reply_len <= 1) continue;
172 * Test whether there are any device(s) left
175 adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
177 if (req.reply_len > 1) {
179 * There are still one or more devices
180 * left at address i. Register the one(s)
181 * we moved to `highFree', and find a new
182 * value for highFree.
184 adb_handler[highFree].original_address =
185 adb_handler[i].original_address;
186 while (highFree > 0 &&
187 adb_handler[highFree].original_address)
195 * No devices left at address i; move the
196 * one(s) we moved to `highFree' back to i.
198 adb_request(&req, NULL, ADBREQ_SYNC, 3,
199 (highFree << 4) | 0xb,
205 /* Now fill in the handler_id field of the adb_handler entries. */
206 printk(KERN_DEBUG "adb devices:");
207 for (i = 1; i < 16; i++) {
208 if (adb_handler[i].original_address == 0)
210 adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
212 adb_handler[i].handler_id = req.reply[2];
213 printk(" [%d]: %d %x", i, adb_handler[i].original_address,
214 adb_handler[i].handler_id);
222 * This kernel task handles ADB probing. It dies once probing is
226 adb_probe_task(void *x)
228 printk(KERN_INFO "adb: starting probe task...\n");
230 printk(KERN_INFO "adb: finished probe task...\n");
232 up(&adb_probe_mutex);
238 __adb_probe_task(struct work_struct *bullshit)
240 kthread_run(adb_probe_task, NULL, "kadbprobe");
243 static DECLARE_WORK(adb_reset_work, __adb_probe_task);
248 if (__adb_probe_sync) {
253 down(&adb_probe_mutex);
254 schedule_work(&adb_reset_work);
260 * notify clients before sleep
262 static int __adb_suspend(struct platform_device *dev, pm_message_t state)
265 /* We need to get a lock on the probe thread */
266 down(&adb_probe_mutex);
268 if (adb_controller->autopoll)
269 adb_controller->autopoll(0);
270 blocking_notifier_call_chain(&adb_client_list, ADB_MSG_POWERDOWN, NULL);
275 static int adb_suspend(struct device *dev)
277 return __adb_suspend(to_platform_device(dev), PMSG_SUSPEND);
280 static int adb_freeze(struct device *dev)
282 return __adb_suspend(to_platform_device(dev), PMSG_FREEZE);
285 static int adb_poweroff(struct device *dev)
287 return __adb_suspend(to_platform_device(dev), PMSG_HIBERNATE);
291 * reset bus after sleep
293 static int __adb_resume(struct platform_device *dev)
296 up(&adb_probe_mutex);
302 static int adb_resume(struct device *dev)
304 return __adb_resume(to_platform_device(dev));
306 #endif /* CONFIG_PM */
308 static int __init adb_init(void)
310 struct adb_driver *driver;
314 if (!machine_is(chrp) && !machine_is(powermac))
322 /* xmon may do early-init */
327 adb_controller = NULL;
330 while ((driver = adb_driver_list[i++]) != NULL) {
331 if (!driver->probe()) {
332 adb_controller = driver;
336 if (adb_controller != NULL && adb_controller->init &&
337 adb_controller->init())
338 adb_controller = NULL;
339 if (adb_controller == NULL) {
340 printk(KERN_WARNING "Warning: no ADB interface detected\n");
343 if (of_machine_is_compatible("AAPL,PowerBook1998") ||
344 of_machine_is_compatible("PowerBook1,1"))
346 #endif /* CONFIG_PPC */
354 device_initcall(adb_init);
357 do_adb_reset_bus(void)
361 if (adb_controller == NULL)
364 if (adb_controller->autopoll)
365 adb_controller->autopoll(0);
367 blocking_notifier_call_chain(&adb_client_list,
368 ADB_MSG_PRE_RESET, NULL);
370 if (sleepy_trackpad) {
371 /* Let the trackpad settle down */
375 mutex_lock(&adb_handler_mutex);
376 write_lock_irq(&adb_handler_lock);
377 memset(adb_handler, 0, sizeof(adb_handler));
378 write_unlock_irq(&adb_handler_lock);
380 /* That one is still a bit synchronous, oh well... */
381 if (adb_controller->reset_bus)
382 ret = adb_controller->reset_bus();
386 if (sleepy_trackpad) {
387 /* Let the trackpad settle down */
392 autopoll_devs = adb_scan_bus();
393 if (adb_controller->autopoll)
394 adb_controller->autopoll(autopoll_devs);
396 mutex_unlock(&adb_handler_mutex);
398 blocking_notifier_call_chain(&adb_client_list,
399 ADB_MSG_POST_RESET, NULL);
407 if ((adb_controller == NULL)||(adb_controller->poll == NULL))
409 adb_controller->poll();
411 EXPORT_SYMBOL(adb_poll);
413 static void adb_sync_req_done(struct adb_request *req)
415 struct completion *comp = req->arg;
421 adb_request(struct adb_request *req, void (*done)(struct adb_request *),
422 int flags, int nbytes, ...)
427 struct completion comp;
429 if ((adb_controller == NULL) || (adb_controller->send_request == NULL))
434 req->nbytes = nbytes+1;
436 req->reply_expected = flags & ADBREQ_REPLY;
437 req->data[0] = ADB_PACKET;
438 va_start(list, nbytes);
439 for (i = 0; i < nbytes; ++i)
440 req->data[i+1] = va_arg(list, int);
443 if (flags & ADBREQ_NOSEND)
446 /* Synchronous requests block using an on-stack completion */
447 if (flags & ADBREQ_SYNC) {
449 req->done = adb_sync_req_done;
451 init_completion(&comp);
454 rc = adb_controller->send_request(req, 0);
456 if ((flags & ADBREQ_SYNC) && !rc && !req->complete)
457 wait_for_completion(&comp);
461 EXPORT_SYMBOL(adb_request);
463 /* Ultimately this should return the number of devices with
464 the given default id.
465 And it does it now ! Note: changed behaviour: This function
466 will now register if default_id _and_ handler_id both match
467 but handler_id can be left to 0 to match with default_id only.
468 When handler_id is set, this function will try to adjust
469 the handler_id id it doesn't match. */
471 adb_register(int default_id, int handler_id, struct adb_ids *ids,
472 void (*handler)(unsigned char *, int, int))
476 mutex_lock(&adb_handler_mutex);
478 for (i = 1; i < 16; i++) {
479 if ((adb_handler[i].original_address == default_id) &&
480 (!handler_id || (handler_id == adb_handler[i].handler_id) ||
481 try_handler_change(i, handler_id))) {
482 if (adb_handler[i].handler != 0) {
484 "Two handlers for ADB device %d\n",
488 write_lock_irq(&adb_handler_lock);
489 adb_handler[i].handler = handler;
490 write_unlock_irq(&adb_handler_lock);
491 ids->id[ids->nids++] = i;
494 mutex_unlock(&adb_handler_mutex);
497 EXPORT_SYMBOL(adb_register);
500 adb_unregister(int index)
504 mutex_lock(&adb_handler_mutex);
505 write_lock_irq(&adb_handler_lock);
506 if (adb_handler[index].handler) {
507 while(adb_handler[index].busy) {
508 write_unlock_irq(&adb_handler_lock);
510 write_lock_irq(&adb_handler_lock);
513 adb_handler[index].handler = NULL;
515 write_unlock_irq(&adb_handler_lock);
516 mutex_unlock(&adb_handler_mutex);
519 EXPORT_SYMBOL(adb_unregister);
522 adb_input(unsigned char *buf, int nb, int autopoll)
525 static int dump_adb_input;
528 void (*handler)(unsigned char *, int, int);
530 /* We skip keystrokes and mouse moves when the sleep process
531 * has been started. We stop autopoll, but this is another security
537 if (dump_adb_input) {
538 printk(KERN_INFO "adb packet: ");
539 for (i = 0; i < nb; ++i)
540 printk(" %x", buf[i]);
541 printk(", id = %d\n", id);
543 write_lock_irqsave(&adb_handler_lock, flags);
544 handler = adb_handler[id].handler;
546 adb_handler[id].busy = 1;
547 write_unlock_irqrestore(&adb_handler_lock, flags);
548 if (handler != NULL) {
549 (*handler)(buf, nb, autopoll);
551 adb_handler[id].busy = 0;
556 /* Try to change handler to new_id. Will return 1 if successful. */
557 static int try_handler_change(int address, int new_id)
559 struct adb_request req;
561 if (adb_handler[address].handler_id == new_id)
563 adb_request(&req, NULL, ADBREQ_SYNC, 3,
564 ADB_WRITEREG(address, 3), address | 0x20, new_id);
565 adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
566 ADB_READREG(address, 3));
567 if (req.reply_len < 2)
569 if (req.reply[2] != new_id)
571 adb_handler[address].handler_id = req.reply[2];
577 adb_try_handler_change(int address, int new_id)
581 mutex_lock(&adb_handler_mutex);
582 ret = try_handler_change(address, new_id);
583 mutex_unlock(&adb_handler_mutex);
586 EXPORT_SYMBOL(adb_try_handler_change);
589 adb_get_infos(int address, int *original_address, int *handler_id)
591 mutex_lock(&adb_handler_mutex);
592 *original_address = adb_handler[address].original_address;
593 *handler_id = adb_handler[address].handler_id;
594 mutex_unlock(&adb_handler_mutex);
596 return (*original_address != 0);
601 * /dev/adb device driver.
604 #define ADB_MAJOR 56 /* major number for /dev/adb */
606 struct adbdev_state {
609 struct adb_request *completed;
610 wait_queue_head_t wait_queue;
614 static void adb_write_done(struct adb_request *req)
616 struct adbdev_state *state = (struct adbdev_state *) req->arg;
619 if (!req->complete) {
623 spin_lock_irqsave(&state->lock, flags);
624 atomic_dec(&state->n_pending);
627 if (atomic_read(&state->n_pending) == 0) {
628 spin_unlock_irqrestore(&state->lock, flags);
633 struct adb_request **ap = &state->completed;
638 wake_up_interruptible(&state->wait_queue);
640 spin_unlock_irqrestore(&state->lock, flags);
644 do_adb_query(struct adb_request *req)
648 switch(req->data[1]) {
649 case ADB_QUERY_GETDEVINFO:
652 mutex_lock(&adb_handler_mutex);
653 req->reply[0] = adb_handler[req->data[2]].original_address;
654 req->reply[1] = adb_handler[req->data[2]].handler_id;
655 mutex_unlock(&adb_handler_mutex);
665 static int adb_open(struct inode *inode, struct file *file)
667 struct adbdev_state *state;
670 mutex_lock(&adb_mutex);
671 if (iminor(inode) > 0 || adb_controller == NULL) {
675 state = kmalloc(sizeof(struct adbdev_state), GFP_KERNEL);
680 file->private_data = state;
681 spin_lock_init(&state->lock);
682 atomic_set(&state->n_pending, 0);
683 state->completed = NULL;
684 init_waitqueue_head(&state->wait_queue);
688 mutex_unlock(&adb_mutex);
692 static int adb_release(struct inode *inode, struct file *file)
694 struct adbdev_state *state = file->private_data;
697 mutex_lock(&adb_mutex);
699 file->private_data = NULL;
700 spin_lock_irqsave(&state->lock, flags);
701 if (atomic_read(&state->n_pending) == 0
702 && state->completed == NULL) {
703 spin_unlock_irqrestore(&state->lock, flags);
707 spin_unlock_irqrestore(&state->lock, flags);
710 mutex_unlock(&adb_mutex);
714 static ssize_t adb_read(struct file *file, char __user *buf,
715 size_t count, loff_t *ppos)
718 struct adbdev_state *state = file->private_data;
719 struct adb_request *req;
720 DECLARE_WAITQUEUE(wait, current);
725 if (count > sizeof(req->reply))
726 count = sizeof(req->reply);
729 spin_lock_irqsave(&state->lock, flags);
730 add_wait_queue(&state->wait_queue, &wait);
731 set_current_state(TASK_INTERRUPTIBLE);
734 req = state->completed;
736 state->completed = req->next;
737 else if (atomic_read(&state->n_pending) == 0)
739 if (req != NULL || ret != 0)
742 if (file->f_flags & O_NONBLOCK) {
746 if (signal_pending(current)) {
750 spin_unlock_irqrestore(&state->lock, flags);
752 spin_lock_irqsave(&state->lock, flags);
755 set_current_state(TASK_RUNNING);
756 remove_wait_queue(&state->wait_queue, &wait);
757 spin_unlock_irqrestore(&state->lock, flags);
762 ret = req->reply_len;
765 if (ret > 0 && copy_to_user(buf, req->reply, ret))
772 static ssize_t adb_write(struct file *file, const char __user *buf,
773 size_t count, loff_t *ppos)
776 struct adbdev_state *state = file->private_data;
777 struct adb_request *req;
779 if (count < 2 || count > sizeof(req->data))
781 if (adb_controller == NULL)
784 req = kmalloc(sizeof(struct adb_request),
790 req->done = adb_write_done;
791 req->arg = (void *) state;
795 if (copy_from_user(req->data, buf, count))
798 atomic_inc(&state->n_pending);
800 /* If a probe is in progress or we are sleeping, wait for it to complete */
801 down(&adb_probe_mutex);
803 /* Queries are special requests sent to the ADB driver itself */
804 if (req->data[0] == ADB_QUERY) {
806 ret = do_adb_query(req);
809 up(&adb_probe_mutex);
811 /* Special case for ADB_BUSRESET request, all others are sent to
813 else if ((req->data[0] == ADB_PACKET) && (count > 1)
814 && (req->data[1] == ADB_BUSRESET)) {
815 ret = do_adb_reset_bus();
816 up(&adb_probe_mutex);
817 atomic_dec(&state->n_pending);
822 req->reply_expected = ((req->data[1] & 0xc) == 0xc);
823 if (adb_controller && adb_controller->send_request)
824 ret = adb_controller->send_request(req, 0);
827 up(&adb_probe_mutex);
831 atomic_dec(&state->n_pending);
841 static const struct file_operations adb_fops = {
842 .owner = THIS_MODULE,
847 .release = adb_release,
851 static const struct dev_pm_ops adb_dev_pm_ops = {
852 .suspend = adb_suspend,
853 .resume = adb_resume,
854 /* Hibernate hooks */
855 .freeze = adb_freeze,
857 .poweroff = adb_poweroff,
858 .restore = adb_resume,
862 static struct platform_driver adb_pfdrv = {
866 .pm = &adb_dev_pm_ops,
871 static struct platform_device adb_pfdev = {
876 adb_dummy_probe(struct platform_device *dev)
878 if (dev == &adb_pfdev)
886 if (register_chrdev(ADB_MAJOR, "adb", &adb_fops)) {
887 printk(KERN_ERR "adb: unable to get major %d\n", ADB_MAJOR);
891 adb_dev_class = class_create(THIS_MODULE, "adb");
892 if (IS_ERR(adb_dev_class))
894 device_create(adb_dev_class, NULL, MKDEV(ADB_MAJOR, 0), NULL, "adb");
896 platform_device_register(&adb_pfdev);
897 platform_driver_probe(&adb_pfdrv, adb_dummy_probe);