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