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>
43 #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
71 #ifdef CONFIG_ADB_MACIO
77 static const struct class adb_dev_class
= {
81 static struct adb_driver
*adb_controller
;
82 BLOCKING_NOTIFIER_HEAD(adb_client_list
);
83 static int adb_got_sleep
;
84 static int adb_inited
;
85 static DEFINE_SEMAPHORE(adb_probe_mutex
, 1);
86 static int sleepy_trackpad
;
87 static int autopoll_devs
;
90 static int adb_scan_bus(void);
91 static int do_adb_reset_bus(void);
92 static void adbdev_init(void);
93 static int try_handler_change(int, int);
95 static struct adb_handler
{
96 void (*handler
)(unsigned char *, int, int);
103 * The adb_handler_mutex mutex protects all accesses to the original_address
104 * and handler_id fields of adb_handler[i] for all i, and changes to the
106 * Accesses to the handler field are protected by the adb_handler_lock
107 * rwlock. It is held across all calls to any handler, so that by the
108 * time adb_unregister returns, we know that the old handler isn't being
111 static DEFINE_MUTEX(adb_handler_mutex
);
112 static DEFINE_RWLOCK(adb_handler_lock
);
115 static void printADBreply(struct adb_request
*req
)
119 printk("adb reply (%d)", req
->reply_len
);
120 for(i
= 0; i
< req
->reply_len
; i
++)
121 printk(" %x", req
->reply
[i
]);
127 static int adb_scan_bus(void)
129 int i
, highFree
=0, noMovement
;
131 struct adb_request req
;
133 /* assumes adb_handler[] is all zeroes at this point */
134 for (i
= 1; i
< 16; i
++) {
135 /* see if there is anything at address i */
136 adb_request(&req
, NULL
, ADBREQ_SYNC
| ADBREQ_REPLY
, 1,
138 if (req
.reply_len
> 1)
139 /* one or more devices at this address */
140 adb_handler
[i
].original_address
= i
;
141 else if (i
> highFree
)
145 /* Note we reset noMovement to 0 each time we move a device */
146 for (noMovement
= 1; noMovement
< 2 && highFree
> 0; noMovement
++) {
147 for (i
= 1; i
< 16; i
++) {
148 if (adb_handler
[i
].original_address
== 0)
151 * Send a "talk register 3" command to address i
152 * to provoke a collision if there is more than
153 * one device at this address.
155 adb_request(&req
, NULL
, ADBREQ_SYNC
| ADBREQ_REPLY
, 1,
158 * Move the device(s) which didn't detect a
159 * collision to address `highFree'. Hopefully
160 * this only moves one device.
162 adb_request(&req
, NULL
, ADBREQ_SYNC
, 3,
163 (i
<< 4) | 0xb, (highFree
| 0x60), 0xfe);
165 * See if anybody actually moved. This is suggested
168 * https://developer.apple.com/technotes/hw/hw_01.html
170 adb_request(&req
, NULL
, ADBREQ_SYNC
| ADBREQ_REPLY
, 1,
171 (highFree
<< 4) | 0xf);
172 if (req
.reply_len
<= 1) continue;
174 * Test whether there are any device(s) left
177 adb_request(&req
, NULL
, ADBREQ_SYNC
| ADBREQ_REPLY
, 1,
179 if (req
.reply_len
> 1) {
181 * There are still one or more devices
182 * left at address i. Register the one(s)
183 * we moved to `highFree', and find a new
184 * value for highFree.
186 adb_handler
[highFree
].original_address
=
187 adb_handler
[i
].original_address
;
188 while (highFree
> 0 &&
189 adb_handler
[highFree
].original_address
)
197 * No devices left at address i; move the
198 * one(s) we moved to `highFree' back to i.
200 adb_request(&req
, NULL
, ADBREQ_SYNC
, 3,
201 (highFree
<< 4) | 0xb,
207 /* Now fill in the handler_id field of the adb_handler entries. */
208 for (i
= 1; i
< 16; i
++) {
209 if (adb_handler
[i
].original_address
== 0)
211 adb_request(&req
, NULL
, ADBREQ_SYNC
| ADBREQ_REPLY
, 1,
213 adb_handler
[i
].handler_id
= req
.reply
[2];
214 printk(KERN_DEBUG
"adb device [%d]: %d 0x%X\n", i
,
215 adb_handler
[i
].original_address
,
216 adb_handler
[i
].handler_id
);
223 * This kernel task handles ADB probing. It dies once probing is
227 adb_probe_task(void *x
)
229 pr_debug("adb: starting probe task...\n");
231 pr_debug("adb: finished probe task...\n");
233 up(&adb_probe_mutex
);
239 __adb_probe_task(struct work_struct
*bullshit
)
241 kthread_run(adb_probe_task
, NULL
, "kadbprobe");
244 static DECLARE_WORK(adb_reset_work
, __adb_probe_task
);
249 if (__adb_probe_sync
) {
254 down(&adb_probe_mutex
);
255 schedule_work(&adb_reset_work
);
261 * notify clients before sleep
263 static int __adb_suspend(struct platform_device
*dev
, pm_message_t state
)
266 /* We need to get a lock on the probe thread */
267 down(&adb_probe_mutex
);
269 if (adb_controller
->autopoll
)
270 adb_controller
->autopoll(0);
271 blocking_notifier_call_chain(&adb_client_list
, ADB_MSG_POWERDOWN
, NULL
);
276 static int adb_suspend(struct device
*dev
)
278 return __adb_suspend(to_platform_device(dev
), PMSG_SUSPEND
);
281 static int adb_freeze(struct device
*dev
)
283 return __adb_suspend(to_platform_device(dev
), PMSG_FREEZE
);
286 static int adb_poweroff(struct device
*dev
)
288 return __adb_suspend(to_platform_device(dev
), PMSG_HIBERNATE
);
292 * reset bus after sleep
294 static int __adb_resume(struct platform_device
*dev
)
297 up(&adb_probe_mutex
);
303 static int adb_resume(struct device
*dev
)
305 return __adb_resume(to_platform_device(dev
));
307 #endif /* CONFIG_PM */
309 static int __init
adb_init(void)
311 struct adb_driver
*driver
;
315 if (!machine_is(chrp
) && !machine_is(powermac
))
323 /* xmon may do early-init */
328 adb_controller
= NULL
;
331 while ((driver
= adb_driver_list
[i
++]) != NULL
) {
332 if (!driver
->probe()) {
333 adb_controller
= driver
;
337 if (adb_controller
!= NULL
&& adb_controller
->init
&&
338 adb_controller
->init())
339 adb_controller
= NULL
;
340 if (adb_controller
== NULL
) {
341 pr_warn("Warning: no ADB interface detected\n");
344 if (of_machine_is_compatible("AAPL,PowerBook1998") ||
345 of_machine_is_compatible("PowerBook1,1"))
347 #endif /* CONFIG_PPC */
355 device_initcall(adb_init
);
358 do_adb_reset_bus(void)
362 if (adb_controller
== NULL
)
365 if (adb_controller
->autopoll
)
366 adb_controller
->autopoll(0);
368 blocking_notifier_call_chain(&adb_client_list
,
369 ADB_MSG_PRE_RESET
, NULL
);
371 if (sleepy_trackpad
) {
372 /* Let the trackpad settle down */
376 mutex_lock(&adb_handler_mutex
);
377 write_lock_irq(&adb_handler_lock
);
378 memset(adb_handler
, 0, sizeof(adb_handler
));
379 write_unlock_irq(&adb_handler_lock
);
381 /* That one is still a bit synchronous, oh well... */
382 if (adb_controller
->reset_bus
)
383 ret
= adb_controller
->reset_bus();
387 if (sleepy_trackpad
) {
388 /* Let the trackpad settle down */
393 autopoll_devs
= adb_scan_bus();
394 if (adb_controller
->autopoll
)
395 adb_controller
->autopoll(autopoll_devs
);
397 mutex_unlock(&adb_handler_mutex
);
399 blocking_notifier_call_chain(&adb_client_list
,
400 ADB_MSG_POST_RESET
, NULL
);
408 if ((adb_controller
== NULL
)||(adb_controller
->poll
== NULL
))
410 adb_controller
->poll();
412 EXPORT_SYMBOL(adb_poll
);
414 static void adb_sync_req_done(struct adb_request
*req
)
416 struct completion
*comp
= req
->arg
;
422 adb_request(struct adb_request
*req
, void (*done
)(struct adb_request
*),
423 int flags
, int nbytes
, ...)
428 struct completion comp
;
430 if ((adb_controller
== NULL
) || (adb_controller
->send_request
== NULL
))
435 req
->nbytes
= nbytes
+1;
437 req
->reply_expected
= flags
& ADBREQ_REPLY
;
438 req
->data
[0] = ADB_PACKET
;
439 va_start(list
, nbytes
);
440 for (i
= 0; i
< nbytes
; ++i
)
441 req
->data
[i
+1] = va_arg(list
, int);
444 if (flags
& ADBREQ_NOSEND
)
447 /* Synchronous requests block using an on-stack completion */
448 if (flags
& ADBREQ_SYNC
) {
450 req
->done
= adb_sync_req_done
;
452 init_completion(&comp
);
455 rc
= adb_controller
->send_request(req
, 0);
457 if ((flags
& ADBREQ_SYNC
) && !rc
&& !req
->complete
)
458 wait_for_completion(&comp
);
462 EXPORT_SYMBOL(adb_request
);
464 /* Ultimately this should return the number of devices with
465 the given default id.
466 And it does it now ! Note: changed behaviour: This function
467 will now register if default_id _and_ handler_id both match
468 but handler_id can be left to 0 to match with default_id only.
469 When handler_id is set, this function will try to adjust
470 the handler_id id it doesn't match. */
472 adb_register(int default_id
, int handler_id
, struct adb_ids
*ids
,
473 void (*handler
)(unsigned char *, int, int))
477 mutex_lock(&adb_handler_mutex
);
479 for (i
= 1; i
< 16; i
++) {
480 if ((adb_handler
[i
].original_address
== default_id
) &&
481 (!handler_id
|| (handler_id
== adb_handler
[i
].handler_id
) ||
482 try_handler_change(i
, handler_id
))) {
483 if (adb_handler
[i
].handler
) {
484 pr_err("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 pr_info("adb packet: ");
539 for (i
= 0; i
< nb
; ++i
)
540 pr_cont(" %x", buf
[i
]);
541 pr_cont(", 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
);
585 pr_debug("adb handler change: [%d] 0x%X\n", address
, new_id
);
588 EXPORT_SYMBOL(adb_try_handler_change
);
591 adb_get_infos(int address
, int *original_address
, int *handler_id
)
593 mutex_lock(&adb_handler_mutex
);
594 *original_address
= adb_handler
[address
].original_address
;
595 *handler_id
= adb_handler
[address
].handler_id
;
596 mutex_unlock(&adb_handler_mutex
);
598 return (*original_address
!= 0);
603 * /dev/adb device driver.
606 #define ADB_MAJOR 56 /* major number for /dev/adb */
608 struct adbdev_state
{
611 struct adb_request
*completed
;
612 wait_queue_head_t wait_queue
;
616 static void adb_write_done(struct adb_request
*req
)
618 struct adbdev_state
*state
= (struct adbdev_state
*) req
->arg
;
621 if (!req
->complete
) {
625 spin_lock_irqsave(&state
->lock
, flags
);
626 atomic_dec(&state
->n_pending
);
629 if (atomic_read(&state
->n_pending
) == 0) {
630 spin_unlock_irqrestore(&state
->lock
, flags
);
635 struct adb_request
**ap
= &state
->completed
;
640 wake_up_interruptible(&state
->wait_queue
);
642 spin_unlock_irqrestore(&state
->lock
, flags
);
646 do_adb_query(struct adb_request
*req
)
650 switch(req
->data
[1]) {
651 case ADB_QUERY_GETDEVINFO
:
652 if (req
->nbytes
< 3 || req
->data
[2] >= 16)
654 mutex_lock(&adb_handler_mutex
);
655 req
->reply
[0] = adb_handler
[req
->data
[2]].original_address
;
656 req
->reply
[1] = adb_handler
[req
->data
[2]].handler_id
;
657 mutex_unlock(&adb_handler_mutex
);
667 static int adb_open(struct inode
*inode
, struct file
*file
)
669 struct adbdev_state
*state
;
672 mutex_lock(&adb_mutex
);
673 if (iminor(inode
) > 0 || adb_controller
== NULL
) {
677 state
= kmalloc(sizeof(struct adbdev_state
), GFP_KERNEL
);
682 file
->private_data
= state
;
683 spin_lock_init(&state
->lock
);
684 atomic_set(&state
->n_pending
, 0);
685 state
->completed
= NULL
;
686 init_waitqueue_head(&state
->wait_queue
);
690 mutex_unlock(&adb_mutex
);
694 static int adb_release(struct inode
*inode
, struct file
*file
)
696 struct adbdev_state
*state
= file
->private_data
;
699 mutex_lock(&adb_mutex
);
701 file
->private_data
= NULL
;
702 spin_lock_irqsave(&state
->lock
, flags
);
703 if (atomic_read(&state
->n_pending
) == 0
704 && state
->completed
== NULL
) {
705 spin_unlock_irqrestore(&state
->lock
, flags
);
709 spin_unlock_irqrestore(&state
->lock
, flags
);
712 mutex_unlock(&adb_mutex
);
716 static ssize_t
adb_read(struct file
*file
, char __user
*buf
,
717 size_t count
, loff_t
*ppos
)
720 struct adbdev_state
*state
= file
->private_data
;
721 struct adb_request
*req
;
722 DECLARE_WAITQUEUE(wait
, current
);
727 if (count
> sizeof(req
->reply
))
728 count
= sizeof(req
->reply
);
731 spin_lock_irqsave(&state
->lock
, flags
);
732 add_wait_queue(&state
->wait_queue
, &wait
);
733 set_current_state(TASK_INTERRUPTIBLE
);
736 req
= state
->completed
;
738 state
->completed
= req
->next
;
739 else if (atomic_read(&state
->n_pending
) == 0)
741 if (req
!= NULL
|| ret
!= 0)
744 if (file
->f_flags
& O_NONBLOCK
) {
748 if (signal_pending(current
)) {
752 spin_unlock_irqrestore(&state
->lock
, flags
);
754 spin_lock_irqsave(&state
->lock
, flags
);
757 set_current_state(TASK_RUNNING
);
758 remove_wait_queue(&state
->wait_queue
, &wait
);
759 spin_unlock_irqrestore(&state
->lock
, flags
);
764 ret
= req
->reply_len
;
767 if (ret
> 0 && copy_to_user(buf
, req
->reply
, ret
))
774 static ssize_t
adb_write(struct file
*file
, const char __user
*buf
,
775 size_t count
, loff_t
*ppos
)
778 struct adbdev_state
*state
= file
->private_data
;
779 struct adb_request
*req
;
781 if (count
< 2 || count
> sizeof(req
->data
))
783 if (adb_controller
== NULL
)
786 req
= kmalloc(sizeof(struct adb_request
),
792 req
->done
= adb_write_done
;
793 req
->arg
= (void *) state
;
797 if (copy_from_user(req
->data
, buf
, count
))
800 atomic_inc(&state
->n_pending
);
802 /* If a probe is in progress or we are sleeping, wait for it to complete */
803 down(&adb_probe_mutex
);
805 /* Queries are special requests sent to the ADB driver itself */
806 if (req
->data
[0] == ADB_QUERY
) {
808 ret
= do_adb_query(req
);
811 up(&adb_probe_mutex
);
813 /* Special case for ADB_BUSRESET request, all others are sent to
815 else if ((req
->data
[0] == ADB_PACKET
) && (count
> 1)
816 && (req
->data
[1] == ADB_BUSRESET
)) {
817 ret
= do_adb_reset_bus();
818 up(&adb_probe_mutex
);
819 atomic_dec(&state
->n_pending
);
824 req
->reply_expected
= ((req
->data
[1] & 0xc) == 0xc);
825 if (adb_controller
&& adb_controller
->send_request
)
826 ret
= adb_controller
->send_request(req
, 0);
829 up(&adb_probe_mutex
);
833 atomic_dec(&state
->n_pending
);
843 static const struct file_operations adb_fops
= {
844 .owner
= THIS_MODULE
,
848 .release
= adb_release
,
852 static const struct dev_pm_ops adb_dev_pm_ops
= {
853 .suspend
= adb_suspend
,
854 .resume
= adb_resume
,
855 /* Hibernate hooks */
856 .freeze
= adb_freeze
,
858 .poweroff
= adb_poweroff
,
859 .restore
= adb_resume
,
863 static struct platform_driver adb_pfdrv
= {
867 .pm
= &adb_dev_pm_ops
,
872 static struct platform_device adb_pfdev
= {
877 adb_dummy_probe(struct platform_device
*dev
)
879 if (dev
== &adb_pfdev
)
887 if (register_chrdev(ADB_MAJOR
, "adb", &adb_fops
)) {
888 pr_err("adb: unable to get major %d\n", ADB_MAJOR
);
892 if (class_register(&adb_dev_class
))
895 device_create(&adb_dev_class
, NULL
, MKDEV(ADB_MAJOR
, 0), NULL
, "adb");
897 platform_device_register(&adb_pfdev
);
898 platform_driver_probe(&adb_pfdrv
, adb_dummy_probe
);