blob: 7b892f4a8e8cdccc981e4a8f29dc2cac30af9b4e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Device driver for the Apple Desktop Bus
3 * and the /dev/adb device on macintoshes.
4 *
5 * Copyright (C) 1996 Paul Mackerras.
6 *
7 * Modified to declare controllers as structures, added
8 * client notification of bus reset and handles PowerBook
9 * sleep, by Benjamin Herrenschmidt.
10 *
11 * To do:
12 *
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
17 */
18
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/types.h>
20#include <linux/errno.h>
21#include <linux/kernel.h>
22#include <linux/slab.h>
23#include <linux/module.h>
24#include <linux/fs.h>
25#include <linux/mm.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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39#include <asm/uaccess.h>
40#include <asm/semaphore.h>
41#ifdef CONFIG_PPC
42#include <asm/prom.h>
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +110043#include <asm/machdep.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#endif
45
46
47EXPORT_SYMBOL(adb_controller);
48EXPORT_SYMBOL(adb_client_list);
49
50extern struct adb_driver via_macii_driver;
51extern struct adb_driver via_maciisi_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 struct adb_driver *adb_driver_list[] = {
58#ifdef CONFIG_ADB_MACII
59 &via_macii_driver,
60#endif
61#ifdef CONFIG_ADB_MACIISI
62 &via_maciisi_driver,
63#endif
64#ifdef CONFIG_ADB_CUDA
65 &via_cuda_driver,
66#endif
67#ifdef CONFIG_ADB_IOP
68 &adb_iop_driver,
69#endif
70#if defined(CONFIG_ADB_PMU) || defined(CONFIG_ADB_PMU68K)
71 &via_pmu_driver,
72#endif
73#ifdef CONFIG_ADB_MACIO
74 &macio_adb_driver,
75#endif
76 NULL
77};
78
gregkh@suse.de56b22932005-03-23 10:01:41 -080079static struct class *adb_dev_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81struct adb_driver *adb_controller;
Alan Sterne041c682006-03-27 01:16:30 -080082BLOCKING_NOTIFIER_HEAD(adb_client_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083static int adb_got_sleep;
84static int adb_inited;
85static pid_t adb_probe_task_pid;
86static DECLARE_MUTEX(adb_probe_mutex);
87static struct completion adb_probe_task_comp;
88static int sleepy_trackpad;
89static int autopoll_devs;
90int __adb_probe_sync;
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092static int adb_scan_bus(void);
93static int do_adb_reset_bus(void);
94static void adbdev_init(void);
95static int try_handler_change(int, int);
96
97static struct adb_handler {
David Howells7d12e782006-10-05 14:55:46 +010098 void (*handler)(unsigned char *, int, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 int original_address;
100 int handler_id;
101 int busy;
102} adb_handler[16];
103
104/*
105 * The adb_handler_sem mutex protects all accesses to the original_address
106 * and handler_id fields of adb_handler[i] for all i, and changes to the
107 * handler field.
108 * Accesses to the handler field are protected by the adb_handler_lock
109 * rwlock. It is held across all calls to any handler, so that by the
110 * time adb_unregister returns, we know that the old handler isn't being
111 * called.
112 */
113static DECLARE_MUTEX(adb_handler_sem);
114static DEFINE_RWLOCK(adb_handler_lock);
115
116#if 0
117static void printADBreply(struct adb_request *req)
118{
119 int i;
120
121 printk("adb reply (%d)", req->reply_len);
122 for(i = 0; i < req->reply_len; i++)
123 printk(" %x", req->reply[i]);
124 printk("\n");
125
126}
127#endif
128
129
130static __inline__ void adb_wait_ms(unsigned int ms)
131{
132 if (current->pid && adb_probe_task_pid &&
133 adb_probe_task_pid == current->pid)
134 msleep(ms);
135 else
136 mdelay(ms);
137}
138
139static int adb_scan_bus(void)
140{
141 int i, highFree=0, noMovement;
142 int devmask = 0;
143 struct adb_request req;
144
145 /* assumes adb_handler[] is all zeroes at this point */
146 for (i = 1; i < 16; i++) {
147 /* see if there is anything at address i */
148 adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
149 (i << 4) | 0xf);
150 if (req.reply_len > 1)
151 /* one or more devices at this address */
152 adb_handler[i].original_address = i;
153 else if (i > highFree)
154 highFree = i;
155 }
156
157 /* Note we reset noMovement to 0 each time we move a device */
158 for (noMovement = 1; noMovement < 2 && highFree > 0; noMovement++) {
159 for (i = 1; i < 16; i++) {
160 if (adb_handler[i].original_address == 0)
161 continue;
162 /*
163 * Send a "talk register 3" command to address i
164 * to provoke a collision if there is more than
165 * one device at this address.
166 */
167 adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
168 (i << 4) | 0xf);
169 /*
170 * Move the device(s) which didn't detect a
171 * collision to address `highFree'. Hopefully
172 * this only moves one device.
173 */
174 adb_request(&req, NULL, ADBREQ_SYNC, 3,
175 (i<< 4) | 0xb, (highFree | 0x60), 0xfe);
176 /*
177 * See if anybody actually moved. This is suggested
178 * by HW TechNote 01:
179 *
180 * http://developer.apple.com/technotes/hw/hw_01.html
181 */
182 adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
183 (highFree << 4) | 0xf);
184 if (req.reply_len <= 1) continue;
185 /*
186 * Test whether there are any device(s) left
187 * at address i.
188 */
189 adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
190 (i << 4) | 0xf);
191 if (req.reply_len > 1) {
192 /*
193 * There are still one or more devices
194 * left at address i. Register the one(s)
195 * we moved to `highFree', and find a new
196 * value for highFree.
197 */
198 adb_handler[highFree].original_address =
199 adb_handler[i].original_address;
200 while (highFree > 0 &&
201 adb_handler[highFree].original_address)
202 highFree--;
203 if (highFree <= 0)
204 break;
205
206 noMovement = 0;
207 }
208 else {
209 /*
210 * No devices left at address i; move the
211 * one(s) we moved to `highFree' back to i.
212 */
213 adb_request(&req, NULL, ADBREQ_SYNC, 3,
214 (highFree << 4) | 0xb,
215 (i | 0x60), 0xfe);
216 }
217 }
218 }
219
220 /* Now fill in the handler_id field of the adb_handler entries. */
221 printk(KERN_DEBUG "adb devices:");
222 for (i = 1; i < 16; i++) {
223 if (adb_handler[i].original_address == 0)
224 continue;
225 adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
226 (i << 4) | 0xf);
227 adb_handler[i].handler_id = req.reply[2];
228 printk(" [%d]: %d %x", i, adb_handler[i].original_address,
229 adb_handler[i].handler_id);
230 devmask |= 1 << i;
231 }
232 printk("\n");
233 return devmask;
234}
235
236/*
237 * This kernel task handles ADB probing. It dies once probing is
238 * completed.
239 */
240static int
241adb_probe_task(void *x)
242{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 strcpy(current->comm, "kadbprobe");
244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 printk(KERN_INFO "adb: starting probe task...\n");
246 do_adb_reset_bus();
247 printk(KERN_INFO "adb: finished probe task...\n");
Oleg Nesterov1b6dd9b2007-07-15 23:41:29 -0700248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 adb_probe_task_pid = 0;
250 up(&adb_probe_mutex);
Oleg Nesterov1b6dd9b2007-07-15 23:41:29 -0700251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 return 0;
253}
254
255static void
Al Viro3e577a82006-12-06 18:41:45 +0000256__adb_probe_task(struct work_struct *bullshit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
258 adb_probe_task_pid = kernel_thread(adb_probe_task, NULL, SIGCHLD | CLONE_KERNEL);
259}
260
Al Viro3e577a82006-12-06 18:41:45 +0000261static DECLARE_WORK(adb_reset_work, __adb_probe_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263int
264adb_reset_bus(void)
265{
266 if (__adb_probe_sync) {
267 do_adb_reset_bus();
268 return 0;
269 }
270
271 down(&adb_probe_mutex);
272 schedule_work(&adb_reset_work);
273 return 0;
274}
275
Johannes Bergc9f6d3d2007-12-12 01:21:25 +1100276#ifdef CONFIG_PM
277/*
278 * notify clients before sleep
279 */
280static int adb_suspend(struct platform_device *dev, pm_message_t state)
281{
282 adb_got_sleep = 1;
283 /* We need to get a lock on the probe thread */
284 down(&adb_probe_mutex);
285 /* Stop autopoll */
286 if (adb_controller->autopoll)
287 adb_controller->autopoll(0);
288 blocking_notifier_call_chain(&adb_client_list, ADB_MSG_POWERDOWN, NULL);
289
290 return 0;
291}
292
293/*
294 * reset bus after sleep
295 */
296static int adb_resume(struct platform_device *dev)
297{
298 adb_got_sleep = 0;
299 up(&adb_probe_mutex);
300 adb_reset_bus();
301
302 return 0;
303}
304#endif /* CONFIG_PM */
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306int __init adb_init(void)
307{
308 struct adb_driver *driver;
309 int i;
310
311#ifdef CONFIG_PPC32
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +1100312 if (!machine_is(chrp) && !machine_is(powermac))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 return 0;
314#endif
315#ifdef CONFIG_MAC
316 if (!MACH_IS_MAC)
317 return 0;
318#endif
319
320 /* xmon may do early-init */
321 if (adb_inited)
322 return 0;
323 adb_inited = 1;
324
325 adb_controller = NULL;
326
327 i = 0;
328 while ((driver = adb_driver_list[i++]) != NULL) {
329 if (!driver->probe()) {
330 adb_controller = driver;
331 break;
332 }
333 }
334 if ((adb_controller == NULL) || adb_controller->init()) {
335 printk(KERN_WARNING "Warning: no ADB interface detected\n");
336 adb_controller = NULL;
337 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338#ifdef CONFIG_PPC
339 if (machine_is_compatible("AAPL,PowerBook1998") ||
340 machine_is_compatible("PowerBook1,1"))
341 sleepy_trackpad = 1;
342#endif /* CONFIG_PPC */
Johannes Bergc9f6d3d2007-12-12 01:21:25 +1100343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 init_completion(&adb_probe_task_comp);
345 adbdev_init();
346 adb_reset_bus();
347 }
348 return 0;
349}
350
351__initcall(adb_init);
352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353static int
354do_adb_reset_bus(void)
355{
Johannes Berg70b52b32007-03-19 11:53:55 +0100356 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
358 if (adb_controller == NULL)
359 return -ENXIO;
360
361 if (adb_controller->autopoll)
362 adb_controller->autopoll(0);
363
Johannes Berg70b52b32007-03-19 11:53:55 +0100364 blocking_notifier_call_chain(&adb_client_list,
365 ADB_MSG_PRE_RESET, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367 if (sleepy_trackpad) {
368 /* Let the trackpad settle down */
369 adb_wait_ms(500);
370 }
371
372 down(&adb_handler_sem);
373 write_lock_irq(&adb_handler_lock);
374 memset(adb_handler, 0, sizeof(adb_handler));
375 write_unlock_irq(&adb_handler_lock);
376
377 /* That one is still a bit synchronous, oh well... */
378 if (adb_controller->reset_bus)
379 ret = adb_controller->reset_bus();
380 else
381 ret = 0;
382
383 if (sleepy_trackpad) {
384 /* Let the trackpad settle down */
385 adb_wait_ms(1500);
386 }
387
388 if (!ret) {
389 autopoll_devs = adb_scan_bus();
390 if (adb_controller->autopoll)
391 adb_controller->autopoll(autopoll_devs);
392 }
393 up(&adb_handler_sem);
394
Johannes Berg70b52b32007-03-19 11:53:55 +0100395 blocking_notifier_call_chain(&adb_client_list,
396 ADB_MSG_POST_RESET, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
398 return ret;
399}
400
401void
402adb_poll(void)
403{
404 if ((adb_controller == NULL)||(adb_controller->poll == NULL))
405 return;
406 adb_controller->poll();
407}
408
409static void
410adb_probe_wakeup(struct adb_request *req)
411{
412 complete(&adb_probe_task_comp);
413}
414
415/* Static request used during probe */
416static struct adb_request adb_sreq;
417static unsigned long adb_sreq_lock; // Use semaphore ! */
418
419int
420adb_request(struct adb_request *req, void (*done)(struct adb_request *),
421 int flags, int nbytes, ...)
422{
423 va_list list;
424 int i, use_sreq;
425 int rc;
426
427 if ((adb_controller == NULL) || (adb_controller->send_request == NULL))
428 return -ENXIO;
429 if (nbytes < 1)
430 return -EINVAL;
431 if (req == NULL && (flags & ADBREQ_NOSEND))
432 return -EINVAL;
433
434 if (req == NULL) {
435 if (test_and_set_bit(0,&adb_sreq_lock)) {
436 printk("adb.c: Warning: contention on static request !\n");
437 return -EPERM;
438 }
439 req = &adb_sreq;
440 flags |= ADBREQ_SYNC;
441 use_sreq = 1;
442 } else
443 use_sreq = 0;
444 req->nbytes = nbytes+1;
445 req->done = done;
446 req->reply_expected = flags & ADBREQ_REPLY;
447 req->data[0] = ADB_PACKET;
448 va_start(list, nbytes);
449 for (i = 0; i < nbytes; ++i)
450 req->data[i+1] = va_arg(list, int);
451 va_end(list);
452
453 if (flags & ADBREQ_NOSEND)
454 return 0;
455
456 /* Synchronous requests send from the probe thread cause it to
457 * block. Beware that the "done" callback will be overriden !
458 */
459 if ((flags & ADBREQ_SYNC) &&
460 (current->pid && adb_probe_task_pid &&
461 adb_probe_task_pid == current->pid)) {
462 req->done = adb_probe_wakeup;
463 rc = adb_controller->send_request(req, 0);
464 if (rc || req->complete)
465 goto bail;
466 wait_for_completion(&adb_probe_task_comp);
467 rc = 0;
468 goto bail;
469 }
470
471 rc = adb_controller->send_request(req, flags & ADBREQ_SYNC);
472bail:
473 if (use_sreq)
474 clear_bit(0, &adb_sreq_lock);
475
476 return rc;
477}
478
479 /* Ultimately this should return the number of devices with
480 the given default id.
481 And it does it now ! Note: changed behaviour: This function
482 will now register if default_id _and_ handler_id both match
483 but handler_id can be left to 0 to match with default_id only.
484 When handler_id is set, this function will try to adjust
485 the handler_id id it doesn't match. */
486int
487adb_register(int default_id, int handler_id, struct adb_ids *ids,
David Howells7d12e782006-10-05 14:55:46 +0100488 void (*handler)(unsigned char *, int, int))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
490 int i;
491
492 down(&adb_handler_sem);
493 ids->nids = 0;
494 for (i = 1; i < 16; i++) {
495 if ((adb_handler[i].original_address == default_id) &&
496 (!handler_id || (handler_id == adb_handler[i].handler_id) ||
497 try_handler_change(i, handler_id))) {
498 if (adb_handler[i].handler != 0) {
499 printk(KERN_ERR
500 "Two handlers for ADB device %d\n",
501 default_id);
502 continue;
503 }
504 write_lock_irq(&adb_handler_lock);
505 adb_handler[i].handler = handler;
506 write_unlock_irq(&adb_handler_lock);
507 ids->id[ids->nids++] = i;
508 }
509 }
510 up(&adb_handler_sem);
511 return ids->nids;
512}
513
514int
515adb_unregister(int index)
516{
517 int ret = -ENODEV;
518
519 down(&adb_handler_sem);
520 write_lock_irq(&adb_handler_lock);
521 if (adb_handler[index].handler) {
522 while(adb_handler[index].busy) {
523 write_unlock_irq(&adb_handler_lock);
524 yield();
525 write_lock_irq(&adb_handler_lock);
526 }
527 ret = 0;
528 adb_handler[index].handler = NULL;
529 }
530 write_unlock_irq(&adb_handler_lock);
531 up(&adb_handler_sem);
532 return ret;
533}
534
535void
David Howells7d12e782006-10-05 14:55:46 +0100536adb_input(unsigned char *buf, int nb, int autopoll)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537{
538 int i, id;
539 static int dump_adb_input = 0;
540 unsigned long flags;
541
David Howells7d12e782006-10-05 14:55:46 +0100542 void (*handler)(unsigned char *, int, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
544 /* We skip keystrokes and mouse moves when the sleep process
545 * has been started. We stop autopoll, but this is another security
546 */
547 if (adb_got_sleep)
548 return;
549
550 id = buf[0] >> 4;
551 if (dump_adb_input) {
552 printk(KERN_INFO "adb packet: ");
553 for (i = 0; i < nb; ++i)
554 printk(" %x", buf[i]);
555 printk(", id = %d\n", id);
556 }
557 write_lock_irqsave(&adb_handler_lock, flags);
558 handler = adb_handler[id].handler;
559 if (handler != NULL)
560 adb_handler[id].busy = 1;
561 write_unlock_irqrestore(&adb_handler_lock, flags);
562 if (handler != NULL) {
David Howells7d12e782006-10-05 14:55:46 +0100563 (*handler)(buf, nb, autopoll);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 wmb();
565 adb_handler[id].busy = 0;
566 }
567
568}
569
570/* Try to change handler to new_id. Will return 1 if successful. */
571static int try_handler_change(int address, int new_id)
572{
573 struct adb_request req;
574
575 if (adb_handler[address].handler_id == new_id)
576 return 1;
577 adb_request(&req, NULL, ADBREQ_SYNC, 3,
578 ADB_WRITEREG(address, 3), address | 0x20, new_id);
579 adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
580 ADB_READREG(address, 3));
581 if (req.reply_len < 2)
582 return 0;
583 if (req.reply[2] != new_id)
584 return 0;
585 adb_handler[address].handler_id = req.reply[2];
586
587 return 1;
588}
589
590int
591adb_try_handler_change(int address, int new_id)
592{
593 int ret;
594
595 down(&adb_handler_sem);
596 ret = try_handler_change(address, new_id);
597 up(&adb_handler_sem);
598 return ret;
599}
600
601int
602adb_get_infos(int address, int *original_address, int *handler_id)
603{
604 down(&adb_handler_sem);
605 *original_address = adb_handler[address].original_address;
606 *handler_id = adb_handler[address].handler_id;
607 up(&adb_handler_sem);
608
609 return (*original_address != 0);
610}
611
612
613/*
614 * /dev/adb device driver.
615 */
616
617#define ADB_MAJOR 56 /* major number for /dev/adb */
618
619struct adbdev_state {
620 spinlock_t lock;
621 atomic_t n_pending;
622 struct adb_request *completed;
623 wait_queue_head_t wait_queue;
624 int inuse;
625};
626
627static void adb_write_done(struct adb_request *req)
628{
629 struct adbdev_state *state = (struct adbdev_state *) req->arg;
630 unsigned long flags;
631
632 if (!req->complete) {
633 req->reply_len = 0;
634 req->complete = 1;
635 }
636 spin_lock_irqsave(&state->lock, flags);
637 atomic_dec(&state->n_pending);
638 if (!state->inuse) {
639 kfree(req);
640 if (atomic_read(&state->n_pending) == 0) {
641 spin_unlock_irqrestore(&state->lock, flags);
642 kfree(state);
643 return;
644 }
645 } else {
646 struct adb_request **ap = &state->completed;
647 while (*ap != NULL)
648 ap = &(*ap)->next;
649 req->next = NULL;
650 *ap = req;
651 wake_up_interruptible(&state->wait_queue);
652 }
653 spin_unlock_irqrestore(&state->lock, flags);
654}
655
656static int
657do_adb_query(struct adb_request *req)
658{
659 int ret = -EINVAL;
660
661 switch(req->data[1])
662 {
663 case ADB_QUERY_GETDEVINFO:
664 if (req->nbytes < 3)
665 break;
666 down(&adb_handler_sem);
667 req->reply[0] = adb_handler[req->data[2]].original_address;
668 req->reply[1] = adb_handler[req->data[2]].handler_id;
669 up(&adb_handler_sem);
670 req->complete = 1;
671 req->reply_len = 2;
672 adb_write_done(req);
673 ret = 0;
674 break;
675 }
676 return ret;
677}
678
679static int adb_open(struct inode *inode, struct file *file)
680{
681 struct adbdev_state *state;
682
683 if (iminor(inode) > 0 || adb_controller == NULL)
684 return -ENXIO;
685 state = kmalloc(sizeof(struct adbdev_state), GFP_KERNEL);
686 if (state == 0)
687 return -ENOMEM;
688 file->private_data = state;
689 spin_lock_init(&state->lock);
690 atomic_set(&state->n_pending, 0);
691 state->completed = NULL;
692 init_waitqueue_head(&state->wait_queue);
693 state->inuse = 1;
694
695 return 0;
696}
697
698static int adb_release(struct inode *inode, struct file *file)
699{
700 struct adbdev_state *state = file->private_data;
701 unsigned long flags;
702
703 lock_kernel();
704 if (state) {
705 file->private_data = NULL;
706 spin_lock_irqsave(&state->lock, flags);
707 if (atomic_read(&state->n_pending) == 0
708 && state->completed == NULL) {
709 spin_unlock_irqrestore(&state->lock, flags);
710 kfree(state);
711 } else {
712 state->inuse = 0;
713 spin_unlock_irqrestore(&state->lock, flags);
714 }
715 }
716 unlock_kernel();
717 return 0;
718}
719
720static ssize_t adb_read(struct file *file, char __user *buf,
721 size_t count, loff_t *ppos)
722{
723 int ret = 0;
724 struct adbdev_state *state = file->private_data;
725 struct adb_request *req;
726 wait_queue_t wait = __WAITQUEUE_INITIALIZER(wait,current);
727 unsigned long flags;
728
729 if (count < 2)
730 return -EINVAL;
731 if (count > sizeof(req->reply))
732 count = sizeof(req->reply);
733 if (!access_ok(VERIFY_WRITE, buf, count))
734 return -EFAULT;
735
736 req = NULL;
737 spin_lock_irqsave(&state->lock, flags);
738 add_wait_queue(&state->wait_queue, &wait);
739 current->state = TASK_INTERRUPTIBLE;
740
741 for (;;) {
742 req = state->completed;
743 if (req != NULL)
744 state->completed = req->next;
745 else if (atomic_read(&state->n_pending) == 0)
746 ret = -EIO;
747 if (req != NULL || ret != 0)
748 break;
749
750 if (file->f_flags & O_NONBLOCK) {
751 ret = -EAGAIN;
752 break;
753 }
754 if (signal_pending(current)) {
755 ret = -ERESTARTSYS;
756 break;
757 }
758 spin_unlock_irqrestore(&state->lock, flags);
759 schedule();
760 spin_lock_irqsave(&state->lock, flags);
761 }
762
763 current->state = TASK_RUNNING;
764 remove_wait_queue(&state->wait_queue, &wait);
765 spin_unlock_irqrestore(&state->lock, flags);
766
767 if (ret)
768 return ret;
769
770 ret = req->reply_len;
771 if (ret > count)
772 ret = count;
773 if (ret > 0 && copy_to_user(buf, req->reply, ret))
774 ret = -EFAULT;
775
776 kfree(req);
777 return ret;
778}
779
780static ssize_t adb_write(struct file *file, const char __user *buf,
781 size_t count, loff_t *ppos)
782{
783 int ret/*, i*/;
784 struct adbdev_state *state = file->private_data;
785 struct adb_request *req;
786
787 if (count < 2 || count > sizeof(req->data))
788 return -EINVAL;
789 if (adb_controller == NULL)
790 return -ENXIO;
791 if (!access_ok(VERIFY_READ, buf, count))
792 return -EFAULT;
793
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800794 req = kmalloc(sizeof(struct adb_request),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 GFP_KERNEL);
796 if (req == NULL)
797 return -ENOMEM;
798
799 req->nbytes = count;
800 req->done = adb_write_done;
801 req->arg = (void *) state;
802 req->complete = 0;
803
804 ret = -EFAULT;
805 if (copy_from_user(req->data, buf, count))
806 goto out;
807
808 atomic_inc(&state->n_pending);
809
810 /* If a probe is in progress or we are sleeping, wait for it to complete */
811 down(&adb_probe_mutex);
812
813 /* Queries are special requests sent to the ADB driver itself */
814 if (req->data[0] == ADB_QUERY) {
815 if (count > 1)
816 ret = do_adb_query(req);
817 else
818 ret = -EINVAL;
819 up(&adb_probe_mutex);
820 }
821 /* Special case for ADB_BUSRESET request, all others are sent to
822 the controller */
823 else if ((req->data[0] == ADB_PACKET)&&(count > 1)
824 &&(req->data[1] == ADB_BUSRESET)) {
825 ret = do_adb_reset_bus();
826 up(&adb_probe_mutex);
827 atomic_dec(&state->n_pending);
828 if (ret == 0)
829 ret = count;
830 goto out;
831 } else {
832 req->reply_expected = ((req->data[1] & 0xc) == 0xc);
833 if (adb_controller && adb_controller->send_request)
834 ret = adb_controller->send_request(req, 0);
835 else
836 ret = -ENXIO;
837 up(&adb_probe_mutex);
838 }
839
840 if (ret != 0) {
841 atomic_dec(&state->n_pending);
842 goto out;
843 }
844 return count;
845
846out:
847 kfree(req);
848 return ret;
849}
850
Arjan van de Venfa027c22007-02-12 00:55:33 -0800851static const struct file_operations adb_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 .owner = THIS_MODULE,
853 .llseek = no_llseek,
854 .read = adb_read,
855 .write = adb_write,
856 .open = adb_open,
857 .release = adb_release,
858};
859
Johannes Bergc9f6d3d2007-12-12 01:21:25 +1100860static struct platform_driver adb_pfdrv = {
861 .driver = {
862 .name = "adb",
863 },
864#ifdef CONFIG_PM
865 .suspend = adb_suspend,
866 .resume = adb_resume,
867#endif
868};
869
870static struct platform_device adb_pfdev = {
871 .name = "adb",
872};
873
874static int __init
875adb_dummy_probe(struct platform_device *dev)
876{
877 if (dev == &adb_pfdev)
878 return 0;
879 return -ENODEV;
880}
881
882static void __init
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883adbdev_init(void)
884{
885 if (register_chrdev(ADB_MAJOR, "adb", &adb_fops)) {
886 printk(KERN_ERR "adb: unable to get major %d\n", ADB_MAJOR);
887 return;
888 }
889
gregkh@suse.de56b22932005-03-23 10:01:41 -0800890 adb_dev_class = class_create(THIS_MODULE, "adb");
891 if (IS_ERR(adb_dev_class))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 return;
Greg Kroah-Hartman53f46542005-10-27 22:25:43 -0700893 class_device_create(adb_dev_class, NULL, MKDEV(ADB_MAJOR, 0), NULL, "adb");
Johannes Bergc9f6d3d2007-12-12 01:21:25 +1100894
895 platform_device_register(&adb_pfdev);
896 platform_driver_probe(&adb_pfdrv, adb_dummy_probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897}