1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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>
25#include <linux/fs.h>
26#include <linux/mm.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>
41
42#include <linux/uaccess.h>
43#ifdef CONFIG_PPC
44#include <asm/prom.h>
45#include <asm/machdep.h>
46#endif
47
48
49EXPORT_SYMBOL(adb_client_list);
50
51extern struct adb_driver via_macii_driver;
52extern struct adb_driver via_cuda_driver;
53extern struct adb_driver adb_iop_driver;
54extern struct adb_driver via_pmu_driver;
55extern struct adb_driver macio_adb_driver;
56
57static DEFINE_MUTEX(adb_mutex);
58static struct adb_driver *adb_driver_list[] = {
59#ifdef CONFIG_ADB_MACII
60 &via_macii_driver,
61#endif
62#ifdef CONFIG_ADB_CUDA
63 &via_cuda_driver,
64#endif
65#ifdef CONFIG_ADB_IOP
66 &adb_iop_driver,
67#endif
68#if defined(CONFIG_ADB_PMU) || defined(CONFIG_ADB_PMU68K)
69 &via_pmu_driver,
70#endif
71#ifdef CONFIG_ADB_MACIO
72 &macio_adb_driver,
73#endif
74 NULL
75};
76
77static struct class *adb_dev_class;
78
79static struct adb_driver *adb_controller;
80BLOCKING_NOTIFIER_HEAD(adb_client_list);
81static int adb_got_sleep;
82static int adb_inited;
83static DEFINE_SEMAPHORE(adb_probe_mutex);
84static int sleepy_trackpad;
85static int autopoll_devs;
86int __adb_probe_sync;
87
88static int adb_scan_bus(void);
89static int do_adb_reset_bus(void);
90static void adbdev_init(void);
91static int try_handler_change(int, int);
92
93static struct adb_handler {
94 void (*handler)(unsigned char *, int, int);
95 int original_address;
96 int handler_id;
97 int busy;
98} adb_handler[16];
99
100
101
102
103
104
105
106
107
108
109static DEFINE_MUTEX(adb_handler_mutex);
110static DEFINE_RWLOCK(adb_handler_lock);
111
112#if 0
113static void printADBreply(struct adb_request *req)
114{
115 int i;
116
117 printk("adb reply (%d)", req->reply_len);
118 for(i = 0; i < req->reply_len; i++)
119 printk(" %x", req->reply[i]);
120 printk("\n");
121
122}
123#endif
124
125static int adb_scan_bus(void)
126{
127 int i, highFree=0, noMovement;
128 int devmask = 0;
129 struct adb_request req;
130
131
132 for (i = 1; i < 16; i++) {
133
134 adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
135 (i << 4) | 0xf);
136 if (req.reply_len > 1)
137
138 adb_handler[i].original_address = i;
139 else if (i > highFree)
140 highFree = i;
141 }
142
143
144 for (noMovement = 1; noMovement < 2 && highFree > 0; noMovement++) {
145 for (i = 1; i < 16; i++) {
146 if (adb_handler[i].original_address == 0)
147 continue;
148
149
150
151
152
153 adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
154 (i << 4) | 0xf);
155
156
157
158
159
160 adb_request(&req, NULL, ADBREQ_SYNC, 3,
161 (i<< 4) | 0xb, (highFree | 0x60), 0xfe);
162
163
164
165
166
167
168 adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
169 (highFree << 4) | 0xf);
170 if (req.reply_len <= 1) continue;
171
172
173
174
175 adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
176 (i << 4) | 0xf);
177 if (req.reply_len > 1) {
178
179
180
181
182
183
184 adb_handler[highFree].original_address =
185 adb_handler[i].original_address;
186 while (highFree > 0 &&
187 adb_handler[highFree].original_address)
188 highFree--;
189 if (highFree <= 0)
190 break;
191
192 noMovement = 0;
193 } else {
194
195
196
197
198 adb_request(&req, NULL, ADBREQ_SYNC, 3,
199 (highFree << 4) | 0xb,
200 (i | 0x60), 0xfe);
201 }
202 }
203 }
204
205
206 printk(KERN_DEBUG "adb devices:");
207 for (i = 1; i < 16; i++) {
208 if (adb_handler[i].original_address == 0)
209 continue;
210 adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
211 (i << 4) | 0xf);
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);
215 devmask |= 1 << i;
216 }
217 printk("\n");
218 return devmask;
219}
220
221
222
223
224
225static int
226adb_probe_task(void *x)
227{
228 printk(KERN_INFO "adb: starting probe task...\n");
229 do_adb_reset_bus();
230 printk(KERN_INFO "adb: finished probe task...\n");
231
232 up(&adb_probe_mutex);
233
234 return 0;
235}
236
237static void
238__adb_probe_task(struct work_struct *bullshit)
239{
240 kthread_run(adb_probe_task, NULL, "kadbprobe");
241}
242
243static DECLARE_WORK(adb_reset_work, __adb_probe_task);
244
245int
246adb_reset_bus(void)
247{
248 if (__adb_probe_sync) {
249 do_adb_reset_bus();
250 return 0;
251 }
252
253 down(&adb_probe_mutex);
254 schedule_work(&adb_reset_work);
255 return 0;
256}
257
258#ifdef CONFIG_PM
259
260
261
262static int __adb_suspend(struct platform_device *dev, pm_message_t state)
263{
264 adb_got_sleep = 1;
265
266 down(&adb_probe_mutex);
267
268 if (adb_controller->autopoll)
269 adb_controller->autopoll(0);
270 blocking_notifier_call_chain(&adb_client_list, ADB_MSG_POWERDOWN, NULL);
271
272 return 0;
273}
274
275static int adb_suspend(struct device *dev)
276{
277 return __adb_suspend(to_platform_device(dev), PMSG_SUSPEND);
278}
279
280static int adb_freeze(struct device *dev)
281{
282 return __adb_suspend(to_platform_device(dev), PMSG_FREEZE);
283}
284
285static int adb_poweroff(struct device *dev)
286{
287 return __adb_suspend(to_platform_device(dev), PMSG_HIBERNATE);
288}
289
290
291
292
293static int __adb_resume(struct platform_device *dev)
294{
295 adb_got_sleep = 0;
296 up(&adb_probe_mutex);
297 adb_reset_bus();
298
299 return 0;
300}
301
302static int adb_resume(struct device *dev)
303{
304 return __adb_resume(to_platform_device(dev));
305}
306#endif
307
308static int __init adb_init(void)
309{
310 struct adb_driver *driver;
311 int i;
312
313#ifdef CONFIG_PPC32
314 if (!machine_is(chrp) && !machine_is(powermac))
315 return 0;
316#endif
317#ifdef CONFIG_MAC
318 if (!MACH_IS_MAC)
319 return 0;
320#endif
321
322
323 if (adb_inited)
324 return 0;
325 adb_inited = 1;
326
327 adb_controller = NULL;
328
329 i = 0;
330 while ((driver = adb_driver_list[i++]) != NULL) {
331 if (!driver->probe()) {
332 adb_controller = driver;
333 break;
334 }
335 }
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");
341 } else {
342#ifdef CONFIG_PPC
343 if (of_machine_is_compatible("AAPL,PowerBook1998") ||
344 of_machine_is_compatible("PowerBook1,1"))
345 sleepy_trackpad = 1;
346#endif
347
348 adbdev_init();
349 adb_reset_bus();
350 }
351 return 0;
352}
353
354device_initcall(adb_init);
355
356static int
357do_adb_reset_bus(void)
358{
359 int ret;
360
361 if (adb_controller == NULL)
362 return -ENXIO;
363
364 if (adb_controller->autopoll)
365 adb_controller->autopoll(0);
366
367 blocking_notifier_call_chain(&adb_client_list,
368 ADB_MSG_PRE_RESET, NULL);
369
370 if (sleepy_trackpad) {
371
372 msleep(500);
373 }
374
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);
379
380
381 if (adb_controller->reset_bus)
382 ret = adb_controller->reset_bus();
383 else
384 ret = 0;
385
386 if (sleepy_trackpad) {
387
388 msleep(1500);
389 }
390
391 if (!ret) {
392 autopoll_devs = adb_scan_bus();
393 if (adb_controller->autopoll)
394 adb_controller->autopoll(autopoll_devs);
395 }
396 mutex_unlock(&adb_handler_mutex);
397
398 blocking_notifier_call_chain(&adb_client_list,
399 ADB_MSG_POST_RESET, NULL);
400
401 return ret;
402}
403
404void
405adb_poll(void)
406{
407 if ((adb_controller == NULL)||(adb_controller->poll == NULL))
408 return;
409 adb_controller->poll();
410}
411EXPORT_SYMBOL(adb_poll);
412
413static void adb_sync_req_done(struct adb_request *req)
414{
415 struct completion *comp = req->arg;
416
417 complete(comp);
418}
419
420int
421adb_request(struct adb_request *req, void (*done)(struct adb_request *),
422 int flags, int nbytes, ...)
423{
424 va_list list;
425 int i;
426 int rc;
427 struct completion comp;
428
429 if ((adb_controller == NULL) || (adb_controller->send_request == NULL))
430 return -ENXIO;
431 if (nbytes < 1)
432 return -EINVAL;
433
434 req->nbytes = nbytes+1;
435 req->done = done;
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);
441 va_end(list);
442
443 if (flags & ADBREQ_NOSEND)
444 return 0;
445
446
447 if (flags & ADBREQ_SYNC) {
448 WARN_ON(done);
449 req->done = adb_sync_req_done;
450 req->arg = ∁
451 init_completion(&comp);
452 }
453
454 rc = adb_controller->send_request(req, 0);
455
456 if ((flags & ADBREQ_SYNC) && !rc && !req->complete)
457 wait_for_completion(&comp);
458
459 return rc;
460}
461EXPORT_SYMBOL(adb_request);
462
463
464
465
466
467
468
469
470int
471adb_register(int default_id, int handler_id, struct adb_ids *ids,
472 void (*handler)(unsigned char *, int, int))
473{
474 int i;
475
476 mutex_lock(&adb_handler_mutex);
477 ids->nids = 0;
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) {
483 printk(KERN_ERR
484 "Two handlers for ADB device %d\n",
485 default_id);
486 continue;
487 }
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;
492 }
493 }
494 mutex_unlock(&adb_handler_mutex);
495 return ids->nids;
496}
497EXPORT_SYMBOL(adb_register);
498
499int
500adb_unregister(int index)
501{
502 int ret = -ENODEV;
503
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);
509 yield();
510 write_lock_irq(&adb_handler_lock);
511 }
512 ret = 0;
513 adb_handler[index].handler = NULL;
514 }
515 write_unlock_irq(&adb_handler_lock);
516 mutex_unlock(&adb_handler_mutex);
517 return ret;
518}
519EXPORT_SYMBOL(adb_unregister);
520
521void
522adb_input(unsigned char *buf, int nb, int autopoll)
523{
524 int i, id;
525 static int dump_adb_input;
526 unsigned long flags;
527
528 void (*handler)(unsigned char *, int, int);
529
530
531
532
533 if (adb_got_sleep)
534 return;
535
536 id = buf[0] >> 4;
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);
542 }
543 write_lock_irqsave(&adb_handler_lock, flags);
544 handler = adb_handler[id].handler;
545 if (handler != NULL)
546 adb_handler[id].busy = 1;
547 write_unlock_irqrestore(&adb_handler_lock, flags);
548 if (handler != NULL) {
549 (*handler)(buf, nb, autopoll);
550 wmb();
551 adb_handler[id].busy = 0;
552 }
553
554}
555
556
557static int try_handler_change(int address, int new_id)
558{
559 struct adb_request req;
560
561 if (adb_handler[address].handler_id == new_id)
562 return 1;
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)
568 return 0;
569 if (req.reply[2] != new_id)
570 return 0;
571 adb_handler[address].handler_id = req.reply[2];
572
573 return 1;
574}
575
576int
577adb_try_handler_change(int address, int new_id)
578{
579 int ret;
580
581 mutex_lock(&adb_handler_mutex);
582 ret = try_handler_change(address, new_id);
583 mutex_unlock(&adb_handler_mutex);
584 return ret;
585}
586EXPORT_SYMBOL(adb_try_handler_change);
587
588int
589adb_get_infos(int address, int *original_address, int *handler_id)
590{
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);
595
596 return (*original_address != 0);
597}
598
599
600
601
602
603
604#define ADB_MAJOR 56
605
606struct adbdev_state {
607 spinlock_t lock;
608 atomic_t n_pending;
609 struct adb_request *completed;
610 wait_queue_head_t wait_queue;
611 int inuse;
612};
613
614static void adb_write_done(struct adb_request *req)
615{
616 struct adbdev_state *state = (struct adbdev_state *) req->arg;
617 unsigned long flags;
618
619 if (!req->complete) {
620 req->reply_len = 0;
621 req->complete = 1;
622 }
623 spin_lock_irqsave(&state->lock, flags);
624 atomic_dec(&state->n_pending);
625 if (!state->inuse) {
626 kfree(req);
627 if (atomic_read(&state->n_pending) == 0) {
628 spin_unlock_irqrestore(&state->lock, flags);
629 kfree(state);
630 return;
631 }
632 } else {
633 struct adb_request **ap = &state->completed;
634 while (*ap != NULL)
635 ap = &(*ap)->next;
636 req->next = NULL;
637 *ap = req;
638 wake_up_interruptible(&state->wait_queue);
639 }
640 spin_unlock_irqrestore(&state->lock, flags);
641}
642
643static int
644do_adb_query(struct adb_request *req)
645{
646 int ret = -EINVAL;
647
648 switch(req->data[1]) {
649 case ADB_QUERY_GETDEVINFO:
650 if (req->nbytes < 3)
651 break;
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);
656 req->complete = 1;
657 req->reply_len = 2;
658 adb_write_done(req);
659 ret = 0;
660 break;
661 }
662 return ret;
663}
664
665static int adb_open(struct inode *inode, struct file *file)
666{
667 struct adbdev_state *state;
668 int ret = 0;
669
670 mutex_lock(&adb_mutex);
671 if (iminor(inode) > 0 || adb_controller == NULL) {
672 ret = -ENXIO;
673 goto out;
674 }
675 state = kmalloc(sizeof(struct adbdev_state), GFP_KERNEL);
676 if (state == 0) {
677 ret = -ENOMEM;
678 goto out;
679 }
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);
685 state->inuse = 1;
686
687out:
688 mutex_unlock(&adb_mutex);
689 return ret;
690}
691
692static int adb_release(struct inode *inode, struct file *file)
693{
694 struct adbdev_state *state = file->private_data;
695 unsigned long flags;
696
697 mutex_lock(&adb_mutex);
698 if (state) {
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);
704 kfree(state);
705 } else {
706 state->inuse = 0;
707 spin_unlock_irqrestore(&state->lock, flags);
708 }
709 }
710 mutex_unlock(&adb_mutex);
711 return 0;
712}
713
714static ssize_t adb_read(struct file *file, char __user *buf,
715 size_t count, loff_t *ppos)
716{
717 int ret = 0;
718 struct adbdev_state *state = file->private_data;
719 struct adb_request *req;
720 DECLARE_WAITQUEUE(wait, current);
721 unsigned long flags;
722
723 if (count < 2)
724 return -EINVAL;
725 if (count > sizeof(req->reply))
726 count = sizeof(req->reply);
727
728 req = NULL;
729 spin_lock_irqsave(&state->lock, flags);
730 add_wait_queue(&state->wait_queue, &wait);
731 set_current_state(TASK_INTERRUPTIBLE);
732
733 for (;;) {
734 req = state->completed;
735 if (req != NULL)
736 state->completed = req->next;
737 else if (atomic_read(&state->n_pending) == 0)
738 ret = -EIO;
739 if (req != NULL || ret != 0)
740 break;
741
742 if (file->f_flags & O_NONBLOCK) {
743 ret = -EAGAIN;
744 break;
745 }
746 if (signal_pending(current)) {
747 ret = -ERESTARTSYS;
748 break;
749 }
750 spin_unlock_irqrestore(&state->lock, flags);
751 schedule();
752 spin_lock_irqsave(&state->lock, flags);
753 }
754
755 set_current_state(TASK_RUNNING);
756 remove_wait_queue(&state->wait_queue, &wait);
757 spin_unlock_irqrestore(&state->lock, flags);
758
759 if (ret)
760 return ret;
761
762 ret = req->reply_len;
763 if (ret > count)
764 ret = count;
765 if (ret > 0 && copy_to_user(buf, req->reply, ret))
766 ret = -EFAULT;
767
768 kfree(req);
769 return ret;
770}
771
772static ssize_t adb_write(struct file *file, const char __user *buf,
773 size_t count, loff_t *ppos)
774{
775 int ret;
776 struct adbdev_state *state = file->private_data;
777 struct adb_request *req;
778
779 if (count < 2 || count > sizeof(req->data))
780 return -EINVAL;
781 if (adb_controller == NULL)
782 return -ENXIO;
783
784 req = kmalloc(sizeof(struct adb_request),
785 GFP_KERNEL);
786 if (req == NULL)
787 return -ENOMEM;
788
789 req->nbytes = count;
790 req->done = adb_write_done;
791 req->arg = (void *) state;
792 req->complete = 0;
793
794 ret = -EFAULT;
795 if (copy_from_user(req->data, buf, count))
796 goto out;
797
798 atomic_inc(&state->n_pending);
799
800
801 down(&adb_probe_mutex);
802
803
804 if (req->data[0] == ADB_QUERY) {
805 if (count > 1)
806 ret = do_adb_query(req);
807 else
808 ret = -EINVAL;
809 up(&adb_probe_mutex);
810 }
811
812
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);
818 if (ret == 0)
819 ret = count;
820 goto out;
821 } else {
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);
825 else
826 ret = -ENXIO;
827 up(&adb_probe_mutex);
828 }
829
830 if (ret != 0) {
831 atomic_dec(&state->n_pending);
832 goto out;
833 }
834 return count;
835
836out:
837 kfree(req);
838 return ret;
839}
840
841static const struct file_operations adb_fops = {
842 .owner = THIS_MODULE,
843 .llseek = no_llseek,
844 .read = adb_read,
845 .write = adb_write,
846 .open = adb_open,
847 .release = adb_release,
848};
849
850#ifdef CONFIG_PM
851static const struct dev_pm_ops adb_dev_pm_ops = {
852 .suspend = adb_suspend,
853 .resume = adb_resume,
854
855 .freeze = adb_freeze,
856 .thaw = adb_resume,
857 .poweroff = adb_poweroff,
858 .restore = adb_resume,
859};
860#endif
861
862static struct platform_driver adb_pfdrv = {
863 .driver = {
864 .name = "adb",
865#ifdef CONFIG_PM
866 .pm = &adb_dev_pm_ops,
867#endif
868 },
869};
870
871static struct platform_device adb_pfdev = {
872 .name = "adb",
873};
874
875static int __init
876adb_dummy_probe(struct platform_device *dev)
877{
878 if (dev == &adb_pfdev)
879 return 0;
880 return -ENODEV;
881}
882
883static void __init
884adbdev_init(void)
885{
886 if (register_chrdev(ADB_MAJOR, "adb", &adb_fops)) {
887 printk(KERN_ERR "adb: unable to get major %d\n", ADB_MAJOR);
888 return;
889 }
890
891 adb_dev_class = class_create(THIS_MODULE, "adb");
892 if (IS_ERR(adb_dev_class))
893 return;
894 device_create(adb_dev_class, NULL, MKDEV(ADB_MAJOR, 0), NULL, "adb");
895
896 platform_device_register(&adb_pfdev);
897 platform_driver_probe(&adb_pfdrv, adb_dummy_probe);
898}
899