blob: 28958101061f9a922cf4c79f7cd41c8ec3472e45 [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>
Paul Mackerrasc61dace2007-12-13 15:11:22 +110038#include <linux/kthread.h>
Geert Uytterhoevenfe2528b2008-02-03 16:49:09 +010039#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41#include <asm/uaccess.h>
42#include <asm/semaphore.h>
43#ifdef CONFIG_PPC
44#include <asm/prom.h>
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +110045#include <asm/machdep.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#endif
47
48
49EXPORT_SYMBOL(adb_controller);
50EXPORT_SYMBOL(adb_client_list);
51
52extern struct adb_driver via_macii_driver;
53extern struct adb_driver via_maciisi_driver;
54extern struct adb_driver via_cuda_driver;
55extern struct adb_driver adb_iop_driver;
56extern struct adb_driver via_pmu_driver;
57extern struct adb_driver macio_adb_driver;
58
59static struct adb_driver *adb_driver_list[] = {
60#ifdef CONFIG_ADB_MACII
61 &via_macii_driver,
62#endif
63#ifdef CONFIG_ADB_MACIISI
64 &via_maciisi_driver,
65#endif
66#ifdef CONFIG_ADB_CUDA
67 &via_cuda_driver,
68#endif
69#ifdef CONFIG_ADB_IOP
70 &adb_iop_driver,
71#endif
72#if defined(CONFIG_ADB_PMU) || defined(CONFIG_ADB_PMU68K)
73 &via_pmu_driver,
74#endif
75#ifdef CONFIG_ADB_MACIO
76 &macio_adb_driver,
77#endif
78 NULL
79};
80
gregkh@suse.de56b22932005-03-23 10:01:41 -080081static struct class *adb_dev_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83struct adb_driver *adb_controller;
Alan Sterne041c682006-03-27 01:16:30 -080084BLOCKING_NOTIFIER_HEAD(adb_client_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085static int adb_got_sleep;
86static int adb_inited;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087static DECLARE_MUTEX(adb_probe_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129static int adb_scan_bus(void)
130{
131 int i, highFree=0, noMovement;
132 int devmask = 0;
133 struct adb_request req;
134
135 /* assumes adb_handler[] is all zeroes at this point */
136 for (i = 1; i < 16; i++) {
137 /* see if there is anything at address i */
138 adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
139 (i << 4) | 0xf);
140 if (req.reply_len > 1)
141 /* one or more devices at this address */
142 adb_handler[i].original_address = i;
143 else if (i > highFree)
144 highFree = i;
145 }
146
147 /* Note we reset noMovement to 0 each time we move a device */
148 for (noMovement = 1; noMovement < 2 && highFree > 0; noMovement++) {
149 for (i = 1; i < 16; i++) {
150 if (adb_handler[i].original_address == 0)
151 continue;
152 /*
153 * Send a "talk register 3" command to address i
154 * to provoke a collision if there is more than
155 * one device at this address.
156 */
157 adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
158 (i << 4) | 0xf);
159 /*
160 * Move the device(s) which didn't detect a
161 * collision to address `highFree'. Hopefully
162 * this only moves one device.
163 */
164 adb_request(&req, NULL, ADBREQ_SYNC, 3,
165 (i<< 4) | 0xb, (highFree | 0x60), 0xfe);
166 /*
167 * See if anybody actually moved. This is suggested
168 * by HW TechNote 01:
169 *
170 * http://developer.apple.com/technotes/hw/hw_01.html
171 */
172 adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
173 (highFree << 4) | 0xf);
174 if (req.reply_len <= 1) continue;
175 /*
176 * Test whether there are any device(s) left
177 * at address i.
178 */
179 adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
180 (i << 4) | 0xf);
181 if (req.reply_len > 1) {
182 /*
183 * There are still one or more devices
184 * left at address i. Register the one(s)
185 * we moved to `highFree', and find a new
186 * value for highFree.
187 */
188 adb_handler[highFree].original_address =
189 adb_handler[i].original_address;
190 while (highFree > 0 &&
191 adb_handler[highFree].original_address)
192 highFree--;
193 if (highFree <= 0)
194 break;
195
196 noMovement = 0;
197 }
198 else {
199 /*
200 * No devices left at address i; move the
201 * one(s) we moved to `highFree' back to i.
202 */
203 adb_request(&req, NULL, ADBREQ_SYNC, 3,
204 (highFree << 4) | 0xb,
205 (i | 0x60), 0xfe);
206 }
207 }
208 }
209
210 /* Now fill in the handler_id field of the adb_handler entries. */
211 printk(KERN_DEBUG "adb devices:");
212 for (i = 1; i < 16; i++) {
213 if (adb_handler[i].original_address == 0)
214 continue;
215 adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
216 (i << 4) | 0xf);
217 adb_handler[i].handler_id = req.reply[2];
218 printk(" [%d]: %d %x", i, adb_handler[i].original_address,
219 adb_handler[i].handler_id);
220 devmask |= 1 << i;
221 }
222 printk("\n");
223 return devmask;
224}
225
226/*
227 * This kernel task handles ADB probing. It dies once probing is
228 * completed.
229 */
230static int
231adb_probe_task(void *x)
232{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 printk(KERN_INFO "adb: starting probe task...\n");
234 do_adb_reset_bus();
235 printk(KERN_INFO "adb: finished probe task...\n");
Oleg Nesterov1b6dd9b2007-07-15 23:41:29 -0700236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 up(&adb_probe_mutex);
Oleg Nesterov1b6dd9b2007-07-15 23:41:29 -0700238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 return 0;
240}
241
242static void
Al Viro3e577a82006-12-06 18:41:45 +0000243__adb_probe_task(struct work_struct *bullshit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
Paul Mackerrasc61dace2007-12-13 15:11:22 +1100245 kthread_run(adb_probe_task, NULL, "kadbprobe");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246}
247
Al Viro3e577a82006-12-06 18:41:45 +0000248static DECLARE_WORK(adb_reset_work, __adb_probe_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250int
251adb_reset_bus(void)
252{
253 if (__adb_probe_sync) {
254 do_adb_reset_bus();
255 return 0;
256 }
257
258 down(&adb_probe_mutex);
259 schedule_work(&adb_reset_work);
260 return 0;
261}
262
Johannes Bergc9f6d3d2007-12-12 01:21:25 +1100263#ifdef CONFIG_PM
264/*
265 * notify clients before sleep
266 */
267static int adb_suspend(struct platform_device *dev, pm_message_t state)
268{
269 adb_got_sleep = 1;
270 /* We need to get a lock on the probe thread */
271 down(&adb_probe_mutex);
272 /* Stop autopoll */
273 if (adb_controller->autopoll)
274 adb_controller->autopoll(0);
275 blocking_notifier_call_chain(&adb_client_list, ADB_MSG_POWERDOWN, NULL);
276
277 return 0;
278}
279
280/*
281 * reset bus after sleep
282 */
283static int adb_resume(struct platform_device *dev)
284{
285 adb_got_sleep = 0;
286 up(&adb_probe_mutex);
287 adb_reset_bus();
288
289 return 0;
290}
291#endif /* CONFIG_PM */
292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293int __init adb_init(void)
294{
295 struct adb_driver *driver;
296 int i;
297
298#ifdef CONFIG_PPC32
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +1100299 if (!machine_is(chrp) && !machine_is(powermac))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 return 0;
301#endif
302#ifdef CONFIG_MAC
303 if (!MACH_IS_MAC)
304 return 0;
305#endif
306
307 /* xmon may do early-init */
308 if (adb_inited)
309 return 0;
310 adb_inited = 1;
311
312 adb_controller = NULL;
313
314 i = 0;
315 while ((driver = adb_driver_list[i++]) != NULL) {
316 if (!driver->probe()) {
317 adb_controller = driver;
318 break;
319 }
320 }
321 if ((adb_controller == NULL) || adb_controller->init()) {
322 printk(KERN_WARNING "Warning: no ADB interface detected\n");
323 adb_controller = NULL;
324 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325#ifdef CONFIG_PPC
326 if (machine_is_compatible("AAPL,PowerBook1998") ||
327 machine_is_compatible("PowerBook1,1"))
328 sleepy_trackpad = 1;
329#endif /* CONFIG_PPC */
Johannes Bergc9f6d3d2007-12-12 01:21:25 +1100330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 adbdev_init();
332 adb_reset_bus();
333 }
334 return 0;
335}
336
337__initcall(adb_init);
338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339static int
340do_adb_reset_bus(void)
341{
Johannes Berg70b52b32007-03-19 11:53:55 +0100342 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344 if (adb_controller == NULL)
345 return -ENXIO;
346
347 if (adb_controller->autopoll)
348 adb_controller->autopoll(0);
349
Johannes Berg70b52b32007-03-19 11:53:55 +0100350 blocking_notifier_call_chain(&adb_client_list,
351 ADB_MSG_PRE_RESET, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
353 if (sleepy_trackpad) {
354 /* Let the trackpad settle down */
Paul Mackerrasc61dace2007-12-13 15:11:22 +1100355 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 }
357
358 down(&adb_handler_sem);
359 write_lock_irq(&adb_handler_lock);
360 memset(adb_handler, 0, sizeof(adb_handler));
361 write_unlock_irq(&adb_handler_lock);
362
363 /* That one is still a bit synchronous, oh well... */
364 if (adb_controller->reset_bus)
365 ret = adb_controller->reset_bus();
366 else
367 ret = 0;
368
369 if (sleepy_trackpad) {
370 /* Let the trackpad settle down */
Paul Mackerrasc61dace2007-12-13 15:11:22 +1100371 msleep(1500);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 }
373
374 if (!ret) {
375 autopoll_devs = adb_scan_bus();
376 if (adb_controller->autopoll)
377 adb_controller->autopoll(autopoll_devs);
378 }
379 up(&adb_handler_sem);
380
Johannes Berg70b52b32007-03-19 11:53:55 +0100381 blocking_notifier_call_chain(&adb_client_list,
382 ADB_MSG_POST_RESET, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
384 return ret;
385}
386
387void
388adb_poll(void)
389{
390 if ((adb_controller == NULL)||(adb_controller->poll == NULL))
391 return;
392 adb_controller->poll();
393}
394
Paul Mackerrasc61dace2007-12-13 15:11:22 +1100395static void adb_sync_req_done(struct adb_request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
Paul Mackerrasc61dace2007-12-13 15:11:22 +1100397 struct completion *comp = req->arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Paul Mackerrasc61dace2007-12-13 15:11:22 +1100399 complete(comp);
400}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
402int
403adb_request(struct adb_request *req, void (*done)(struct adb_request *),
404 int flags, int nbytes, ...)
405{
406 va_list list;
Paul Mackerrasc61dace2007-12-13 15:11:22 +1100407 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 int rc;
Paul Mackerrasc61dace2007-12-13 15:11:22 +1100409 struct completion comp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411 if ((adb_controller == NULL) || (adb_controller->send_request == NULL))
412 return -ENXIO;
413 if (nbytes < 1)
414 return -EINVAL;
Paul Mackerrasc61dace2007-12-13 15:11:22 +1100415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 req->nbytes = nbytes+1;
417 req->done = done;
418 req->reply_expected = flags & ADBREQ_REPLY;
419 req->data[0] = ADB_PACKET;
420 va_start(list, nbytes);
421 for (i = 0; i < nbytes; ++i)
422 req->data[i+1] = va_arg(list, int);
423 va_end(list);
424
425 if (flags & ADBREQ_NOSEND)
426 return 0;
427
Paul Mackerrasc61dace2007-12-13 15:11:22 +1100428 /* Synchronous requests block using an on-stack completion */
429 if (flags & ADBREQ_SYNC) {
430 WARN_ON(done);
431 req->done = adb_sync_req_done;
432 req->arg = &comp;
433 init_completion(&comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 }
435
Paul Mackerrasc61dace2007-12-13 15:11:22 +1100436 rc = adb_controller->send_request(req, 0);
437
438 if ((flags & ADBREQ_SYNC) && !rc && !req->complete)
439 wait_for_completion(&comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441 return rc;
442}
443
444 /* Ultimately this should return the number of devices with
445 the given default id.
446 And it does it now ! Note: changed behaviour: This function
447 will now register if default_id _and_ handler_id both match
448 but handler_id can be left to 0 to match with default_id only.
449 When handler_id is set, this function will try to adjust
450 the handler_id id it doesn't match. */
451int
452adb_register(int default_id, int handler_id, struct adb_ids *ids,
David Howells7d12e782006-10-05 14:55:46 +0100453 void (*handler)(unsigned char *, int, int))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
455 int i;
456
457 down(&adb_handler_sem);
458 ids->nids = 0;
459 for (i = 1; i < 16; i++) {
460 if ((adb_handler[i].original_address == default_id) &&
461 (!handler_id || (handler_id == adb_handler[i].handler_id) ||
462 try_handler_change(i, handler_id))) {
463 if (adb_handler[i].handler != 0) {
464 printk(KERN_ERR
465 "Two handlers for ADB device %d\n",
466 default_id);
467 continue;
468 }
469 write_lock_irq(&adb_handler_lock);
470 adb_handler[i].handler = handler;
471 write_unlock_irq(&adb_handler_lock);
472 ids->id[ids->nids++] = i;
473 }
474 }
475 up(&adb_handler_sem);
476 return ids->nids;
477}
478
479int
480adb_unregister(int index)
481{
482 int ret = -ENODEV;
483
484 down(&adb_handler_sem);
485 write_lock_irq(&adb_handler_lock);
486 if (adb_handler[index].handler) {
487 while(adb_handler[index].busy) {
488 write_unlock_irq(&adb_handler_lock);
489 yield();
490 write_lock_irq(&adb_handler_lock);
491 }
492 ret = 0;
493 adb_handler[index].handler = NULL;
494 }
495 write_unlock_irq(&adb_handler_lock);
496 up(&adb_handler_sem);
497 return ret;
498}
499
500void
David Howells7d12e782006-10-05 14:55:46 +0100501adb_input(unsigned char *buf, int nb, int autopoll)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
503 int i, id;
504 static int dump_adb_input = 0;
505 unsigned long flags;
506
David Howells7d12e782006-10-05 14:55:46 +0100507 void (*handler)(unsigned char *, int, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
509 /* We skip keystrokes and mouse moves when the sleep process
510 * has been started. We stop autopoll, but this is another security
511 */
512 if (adb_got_sleep)
513 return;
514
515 id = buf[0] >> 4;
516 if (dump_adb_input) {
517 printk(KERN_INFO "adb packet: ");
518 for (i = 0; i < nb; ++i)
519 printk(" %x", buf[i]);
520 printk(", id = %d\n", id);
521 }
522 write_lock_irqsave(&adb_handler_lock, flags);
523 handler = adb_handler[id].handler;
524 if (handler != NULL)
525 adb_handler[id].busy = 1;
526 write_unlock_irqrestore(&adb_handler_lock, flags);
527 if (handler != NULL) {
David Howells7d12e782006-10-05 14:55:46 +0100528 (*handler)(buf, nb, autopoll);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 wmb();
530 adb_handler[id].busy = 0;
531 }
532
533}
534
535/* Try to change handler to new_id. Will return 1 if successful. */
536static int try_handler_change(int address, int new_id)
537{
538 struct adb_request req;
539
540 if (adb_handler[address].handler_id == new_id)
541 return 1;
542 adb_request(&req, NULL, ADBREQ_SYNC, 3,
543 ADB_WRITEREG(address, 3), address | 0x20, new_id);
544 adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
545 ADB_READREG(address, 3));
546 if (req.reply_len < 2)
547 return 0;
548 if (req.reply[2] != new_id)
549 return 0;
550 adb_handler[address].handler_id = req.reply[2];
551
552 return 1;
553}
554
555int
556adb_try_handler_change(int address, int new_id)
557{
558 int ret;
559
560 down(&adb_handler_sem);
561 ret = try_handler_change(address, new_id);
562 up(&adb_handler_sem);
563 return ret;
564}
565
566int
567adb_get_infos(int address, int *original_address, int *handler_id)
568{
569 down(&adb_handler_sem);
570 *original_address = adb_handler[address].original_address;
571 *handler_id = adb_handler[address].handler_id;
572 up(&adb_handler_sem);
573
574 return (*original_address != 0);
575}
576
577
578/*
579 * /dev/adb device driver.
580 */
581
582#define ADB_MAJOR 56 /* major number for /dev/adb */
583
584struct adbdev_state {
585 spinlock_t lock;
586 atomic_t n_pending;
587 struct adb_request *completed;
588 wait_queue_head_t wait_queue;
589 int inuse;
590};
591
592static void adb_write_done(struct adb_request *req)
593{
594 struct adbdev_state *state = (struct adbdev_state *) req->arg;
595 unsigned long flags;
596
597 if (!req->complete) {
598 req->reply_len = 0;
599 req->complete = 1;
600 }
601 spin_lock_irqsave(&state->lock, flags);
602 atomic_dec(&state->n_pending);
603 if (!state->inuse) {
604 kfree(req);
605 if (atomic_read(&state->n_pending) == 0) {
606 spin_unlock_irqrestore(&state->lock, flags);
607 kfree(state);
608 return;
609 }
610 } else {
611 struct adb_request **ap = &state->completed;
612 while (*ap != NULL)
613 ap = &(*ap)->next;
614 req->next = NULL;
615 *ap = req;
616 wake_up_interruptible(&state->wait_queue);
617 }
618 spin_unlock_irqrestore(&state->lock, flags);
619}
620
621static int
622do_adb_query(struct adb_request *req)
623{
624 int ret = -EINVAL;
625
626 switch(req->data[1])
627 {
628 case ADB_QUERY_GETDEVINFO:
629 if (req->nbytes < 3)
630 break;
631 down(&adb_handler_sem);
632 req->reply[0] = adb_handler[req->data[2]].original_address;
633 req->reply[1] = adb_handler[req->data[2]].handler_id;
634 up(&adb_handler_sem);
635 req->complete = 1;
636 req->reply_len = 2;
637 adb_write_done(req);
638 ret = 0;
639 break;
640 }
641 return ret;
642}
643
644static int adb_open(struct inode *inode, struct file *file)
645{
646 struct adbdev_state *state;
647
648 if (iminor(inode) > 0 || adb_controller == NULL)
649 return -ENXIO;
650 state = kmalloc(sizeof(struct adbdev_state), GFP_KERNEL);
651 if (state == 0)
652 return -ENOMEM;
653 file->private_data = state;
654 spin_lock_init(&state->lock);
655 atomic_set(&state->n_pending, 0);
656 state->completed = NULL;
657 init_waitqueue_head(&state->wait_queue);
658 state->inuse = 1;
659
660 return 0;
661}
662
663static int adb_release(struct inode *inode, struct file *file)
664{
665 struct adbdev_state *state = file->private_data;
666 unsigned long flags;
667
668 lock_kernel();
669 if (state) {
670 file->private_data = NULL;
671 spin_lock_irqsave(&state->lock, flags);
672 if (atomic_read(&state->n_pending) == 0
673 && state->completed == NULL) {
674 spin_unlock_irqrestore(&state->lock, flags);
675 kfree(state);
676 } else {
677 state->inuse = 0;
678 spin_unlock_irqrestore(&state->lock, flags);
679 }
680 }
681 unlock_kernel();
682 return 0;
683}
684
685static ssize_t adb_read(struct file *file, char __user *buf,
686 size_t count, loff_t *ppos)
687{
688 int ret = 0;
689 struct adbdev_state *state = file->private_data;
690 struct adb_request *req;
691 wait_queue_t wait = __WAITQUEUE_INITIALIZER(wait,current);
692 unsigned long flags;
693
694 if (count < 2)
695 return -EINVAL;
696 if (count > sizeof(req->reply))
697 count = sizeof(req->reply);
698 if (!access_ok(VERIFY_WRITE, buf, count))
699 return -EFAULT;
700
701 req = NULL;
702 spin_lock_irqsave(&state->lock, flags);
703 add_wait_queue(&state->wait_queue, &wait);
704 current->state = TASK_INTERRUPTIBLE;
705
706 for (;;) {
707 req = state->completed;
708 if (req != NULL)
709 state->completed = req->next;
710 else if (atomic_read(&state->n_pending) == 0)
711 ret = -EIO;
712 if (req != NULL || ret != 0)
713 break;
714
715 if (file->f_flags & O_NONBLOCK) {
716 ret = -EAGAIN;
717 break;
718 }
719 if (signal_pending(current)) {
720 ret = -ERESTARTSYS;
721 break;
722 }
723 spin_unlock_irqrestore(&state->lock, flags);
724 schedule();
725 spin_lock_irqsave(&state->lock, flags);
726 }
727
728 current->state = TASK_RUNNING;
729 remove_wait_queue(&state->wait_queue, &wait);
730 spin_unlock_irqrestore(&state->lock, flags);
731
732 if (ret)
733 return ret;
734
735 ret = req->reply_len;
736 if (ret > count)
737 ret = count;
738 if (ret > 0 && copy_to_user(buf, req->reply, ret))
739 ret = -EFAULT;
740
741 kfree(req);
742 return ret;
743}
744
745static ssize_t adb_write(struct file *file, const char __user *buf,
746 size_t count, loff_t *ppos)
747{
748 int ret/*, i*/;
749 struct adbdev_state *state = file->private_data;
750 struct adb_request *req;
751
752 if (count < 2 || count > sizeof(req->data))
753 return -EINVAL;
754 if (adb_controller == NULL)
755 return -ENXIO;
756 if (!access_ok(VERIFY_READ, buf, count))
757 return -EFAULT;
758
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800759 req = kmalloc(sizeof(struct adb_request),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 GFP_KERNEL);
761 if (req == NULL)
762 return -ENOMEM;
763
764 req->nbytes = count;
765 req->done = adb_write_done;
766 req->arg = (void *) state;
767 req->complete = 0;
768
769 ret = -EFAULT;
770 if (copy_from_user(req->data, buf, count))
771 goto out;
772
773 atomic_inc(&state->n_pending);
774
775 /* If a probe is in progress or we are sleeping, wait for it to complete */
776 down(&adb_probe_mutex);
777
778 /* Queries are special requests sent to the ADB driver itself */
779 if (req->data[0] == ADB_QUERY) {
780 if (count > 1)
781 ret = do_adb_query(req);
782 else
783 ret = -EINVAL;
784 up(&adb_probe_mutex);
785 }
786 /* Special case for ADB_BUSRESET request, all others are sent to
787 the controller */
788 else if ((req->data[0] == ADB_PACKET)&&(count > 1)
789 &&(req->data[1] == ADB_BUSRESET)) {
790 ret = do_adb_reset_bus();
791 up(&adb_probe_mutex);
792 atomic_dec(&state->n_pending);
793 if (ret == 0)
794 ret = count;
795 goto out;
796 } else {
797 req->reply_expected = ((req->data[1] & 0xc) == 0xc);
798 if (adb_controller && adb_controller->send_request)
799 ret = adb_controller->send_request(req, 0);
800 else
801 ret = -ENXIO;
802 up(&adb_probe_mutex);
803 }
804
805 if (ret != 0) {
806 atomic_dec(&state->n_pending);
807 goto out;
808 }
809 return count;
810
811out:
812 kfree(req);
813 return ret;
814}
815
Arjan van de Venfa027c22007-02-12 00:55:33 -0800816static const struct file_operations adb_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 .owner = THIS_MODULE,
818 .llseek = no_llseek,
819 .read = adb_read,
820 .write = adb_write,
821 .open = adb_open,
822 .release = adb_release,
823};
824
Johannes Bergc9f6d3d2007-12-12 01:21:25 +1100825static struct platform_driver adb_pfdrv = {
826 .driver = {
827 .name = "adb",
828 },
829#ifdef CONFIG_PM
830 .suspend = adb_suspend,
831 .resume = adb_resume,
832#endif
833};
834
835static struct platform_device adb_pfdev = {
836 .name = "adb",
837};
838
839static int __init
840adb_dummy_probe(struct platform_device *dev)
841{
842 if (dev == &adb_pfdev)
843 return 0;
844 return -ENODEV;
845}
846
847static void __init
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848adbdev_init(void)
849{
850 if (register_chrdev(ADB_MAJOR, "adb", &adb_fops)) {
851 printk(KERN_ERR "adb: unable to get major %d\n", ADB_MAJOR);
852 return;
853 }
854
gregkh@suse.de56b22932005-03-23 10:01:41 -0800855 adb_dev_class = class_create(THIS_MODULE, "adb");
856 if (IS_ERR(adb_dev_class))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 return;
Tony Jones68db2bc2007-09-25 02:03:03 +0200858 device_create(adb_dev_class, NULL, MKDEV(ADB_MAJOR, 0), "adb");
Johannes Bergc9f6d3d2007-12-12 01:21:25 +1100859
860 platform_device_register(&adb_pfdev);
861 platform_driver_probe(&adb_pfdrv, adb_dummy_probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862}