blob: 9e9c56758a0897ab5c7e7b0dfc14ac690e1bb043 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/adb.h>
28#include <linux/cuda.h>
29#include <linux/pmu.h>
30#include <linux/notifier.h>
31#include <linux/wait.h>
32#include <linux/init.h>
33#include <linux/delay.h>
34#include <linux/spinlock.h>
35#include <linux/completion.h>
36#include <linux/device.h>
Paul Mackerrasc61dace2007-12-13 15:11:22 +110037#include <linux/kthread.h>
Geert Uytterhoevenfe2528b2008-02-03 16:49:09 +010038#include <linux/platform_device.h>
Daniel Walkeraf3ce512008-05-03 06:34:03 +100039#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Brandon Stewart74e7cd42014-01-27 19:43:17 -060041#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#ifdef CONFIG_PPC
43#include <asm/prom.h>
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +110044#include <asm/machdep.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#endif
46
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048EXPORT_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
Arnd Bergmannd851b6e2010-06-02 14:28:52 +020057static DEFINE_MUTEX(adb_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058static struct adb_driver *adb_driver_list[] = {
59#ifdef CONFIG_ADB_MACII
60 &via_macii_driver,
61#endif
62#ifdef CONFIG_ADB_MACIISI
63 &via_maciisi_driver,
64#endif
65#ifdef CONFIG_ADB_CUDA
66 &via_cuda_driver,
67#endif
68#ifdef CONFIG_ADB_IOP
69 &adb_iop_driver,
70#endif
71#if defined(CONFIG_ADB_PMU) || defined(CONFIG_ADB_PMU68K)
72 &via_pmu_driver,
73#endif
74#ifdef CONFIG_ADB_MACIO
75 &macio_adb_driver,
76#endif
77 NULL
78};
79
gregkh@suse.de56b22932005-03-23 10:01:41 -080080static struct class *adb_dev_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Adrian Bunk9b4a8dd2008-06-24 03:46:57 +100082static struct adb_driver *adb_controller;
Alan Sterne041c682006-03-27 01:16:30 -080083BLOCKING_NOTIFIER_HEAD(adb_client_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084static int adb_got_sleep;
85static int adb_inited;
Thomas Gleixner8192b1f2010-09-07 14:33:40 +000086static DEFINE_SEMAPHORE(adb_probe_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087static int sleepy_trackpad;
88static int autopoll_devs;
89int __adb_probe_sync;
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091static int adb_scan_bus(void);
92static int do_adb_reset_bus(void);
93static void adbdev_init(void);
94static int try_handler_change(int, int);
95
96static struct adb_handler {
David Howells7d12e782006-10-05 14:55:46 +010097 void (*handler)(unsigned char *, int, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 int original_address;
99 int handler_id;
100 int busy;
101} adb_handler[16];
102
103/*
Daniel Walkeraf3ce512008-05-03 06:34:03 +1000104 * The adb_handler_mutex mutex protects all accesses to the original_address
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 * and handler_id fields of adb_handler[i] for all i, and changes to the
106 * handler field.
107 * Accesses to the handler field are protected by the adb_handler_lock
108 * rwlock. It is held across all calls to any handler, so that by the
109 * time adb_unregister returns, we know that the old handler isn't being
110 * called.
111 */
Daniel Walkeraf3ce512008-05-03 06:34:03 +1000112static DEFINE_MUTEX(adb_handler_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113static DEFINE_RWLOCK(adb_handler_lock);
114
115#if 0
116static void printADBreply(struct adb_request *req)
117{
118 int i;
119
120 printk("adb reply (%d)", req->reply_len);
121 for(i = 0; i < req->reply_len; i++)
122 printk(" %x", req->reply[i]);
123 printk("\n");
124
125}
126#endif
127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128static int adb_scan_bus(void)
129{
130 int i, highFree=0, noMovement;
131 int devmask = 0;
132 struct adb_request req;
133
134 /* assumes adb_handler[] is all zeroes at this point */
135 for (i = 1; i < 16; i++) {
136 /* see if there is anything at address i */
137 adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
138 (i << 4) | 0xf);
139 if (req.reply_len > 1)
140 /* one or more devices at this address */
141 adb_handler[i].original_address = i;
142 else if (i > highFree)
143 highFree = i;
144 }
145
146 /* Note we reset noMovement to 0 each time we move a device */
147 for (noMovement = 1; noMovement < 2 && highFree > 0; noMovement++) {
148 for (i = 1; i < 16; i++) {
149 if (adb_handler[i].original_address == 0)
150 continue;
151 /*
152 * Send a "talk register 3" command to address i
153 * to provoke a collision if there is more than
154 * one device at this address.
155 */
156 adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
157 (i << 4) | 0xf);
158 /*
159 * Move the device(s) which didn't detect a
160 * collision to address `highFree'. Hopefully
161 * this only moves one device.
162 */
163 adb_request(&req, NULL, ADBREQ_SYNC, 3,
164 (i<< 4) | 0xb, (highFree | 0x60), 0xfe);
165 /*
166 * See if anybody actually moved. This is suggested
167 * by HW TechNote 01:
168 *
169 * http://developer.apple.com/technotes/hw/hw_01.html
170 */
171 adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
172 (highFree << 4) | 0xf);
173 if (req.reply_len <= 1) continue;
174 /*
175 * Test whether there are any device(s) left
176 * at address i.
177 */
178 adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
179 (i << 4) | 0xf);
180 if (req.reply_len > 1) {
181 /*
182 * There are still one or more devices
183 * left at address i. Register the one(s)
184 * we moved to `highFree', and find a new
185 * value for highFree.
186 */
187 adb_handler[highFree].original_address =
188 adb_handler[i].original_address;
189 while (highFree > 0 &&
190 adb_handler[highFree].original_address)
191 highFree--;
192 if (highFree <= 0)
193 break;
194
195 noMovement = 0;
Brandon Stewart74e7cd42014-01-27 19:43:17 -0600196 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 /*
198 * No devices left at address i; move the
199 * one(s) we moved to `highFree' back to i.
200 */
201 adb_request(&req, NULL, ADBREQ_SYNC, 3,
202 (highFree << 4) | 0xb,
203 (i | 0x60), 0xfe);
204 }
205 }
206 }
207
208 /* Now fill in the handler_id field of the adb_handler entries. */
209 printk(KERN_DEBUG "adb devices:");
210 for (i = 1; i < 16; i++) {
211 if (adb_handler[i].original_address == 0)
212 continue;
213 adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
214 (i << 4) | 0xf);
215 adb_handler[i].handler_id = req.reply[2];
216 printk(" [%d]: %d %x", i, adb_handler[i].original_address,
217 adb_handler[i].handler_id);
218 devmask |= 1 << i;
219 }
220 printk("\n");
221 return devmask;
222}
223
224/*
225 * This kernel task handles ADB probing. It dies once probing is
226 * completed.
227 */
228static int
229adb_probe_task(void *x)
230{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 printk(KERN_INFO "adb: starting probe task...\n");
232 do_adb_reset_bus();
233 printk(KERN_INFO "adb: finished probe task...\n");
Oleg Nesterov1b6dd9b2007-07-15 23:41:29 -0700234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 up(&adb_probe_mutex);
Oleg Nesterov1b6dd9b2007-07-15 23:41:29 -0700236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 return 0;
238}
239
240static void
Al Viro3e577a82006-12-06 18:41:45 +0000241__adb_probe_task(struct work_struct *bullshit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
Paul Mackerrasc61dace2007-12-13 15:11:22 +1100243 kthread_run(adb_probe_task, NULL, "kadbprobe");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244}
245
Al Viro3e577a82006-12-06 18:41:45 +0000246static DECLARE_WORK(adb_reset_work, __adb_probe_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248int
249adb_reset_bus(void)
250{
251 if (__adb_probe_sync) {
252 do_adb_reset_bus();
253 return 0;
254 }
255
256 down(&adb_probe_mutex);
257 schedule_work(&adb_reset_work);
258 return 0;
259}
260
Johannes Bergc9f6d3d2007-12-12 01:21:25 +1100261#ifdef CONFIG_PM
262/*
263 * notify clients before sleep
264 */
Shuah Khan639291f22014-02-10 09:12:27 -0700265static int __adb_suspend(struct platform_device *dev, pm_message_t state)
Johannes Bergc9f6d3d2007-12-12 01:21:25 +1100266{
267 adb_got_sleep = 1;
268 /* We need to get a lock on the probe thread */
269 down(&adb_probe_mutex);
270 /* Stop autopoll */
271 if (adb_controller->autopoll)
272 adb_controller->autopoll(0);
273 blocking_notifier_call_chain(&adb_client_list, ADB_MSG_POWERDOWN, NULL);
274
275 return 0;
276}
277
Shuah Khan639291f22014-02-10 09:12:27 -0700278static int adb_suspend(struct device *dev)
279{
280 return __adb_suspend(to_platform_device(dev), PMSG_SUSPEND);
281}
282
283static int adb_freeze(struct device *dev)
284{
285 return __adb_suspend(to_platform_device(dev), PMSG_FREEZE);
286}
287
288static int adb_poweroff(struct device *dev)
289{
290 return __adb_suspend(to_platform_device(dev), PMSG_HIBERNATE);
291}
292
Johannes Bergc9f6d3d2007-12-12 01:21:25 +1100293/*
294 * reset bus after sleep
295 */
Shuah Khan639291f22014-02-10 09:12:27 -0700296static int __adb_resume(struct platform_device *dev)
Johannes Bergc9f6d3d2007-12-12 01:21:25 +1100297{
298 adb_got_sleep = 0;
299 up(&adb_probe_mutex);
300 adb_reset_bus();
301
302 return 0;
303}
Shuah Khan639291f22014-02-10 09:12:27 -0700304
305static int adb_resume(struct device *dev)
306{
307 return __adb_resume(to_platform_device(dev));
308}
Johannes Bergc9f6d3d2007-12-12 01:21:25 +1100309#endif /* CONFIG_PM */
310
Adrian Bunk9b4a8dd2008-06-24 03:46:57 +1000311static int __init adb_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312{
313 struct adb_driver *driver;
314 int i;
315
316#ifdef CONFIG_PPC32
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +1100317 if (!machine_is(chrp) && !machine_is(powermac))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 return 0;
319#endif
320#ifdef CONFIG_MAC
321 if (!MACH_IS_MAC)
322 return 0;
323#endif
324
325 /* xmon may do early-init */
326 if (adb_inited)
327 return 0;
328 adb_inited = 1;
329
330 adb_controller = NULL;
331
332 i = 0;
333 while ((driver = adb_driver_list[i++]) != NULL) {
334 if (!driver->probe()) {
335 adb_controller = driver;
336 break;
337 }
338 }
Finn Thain18814ee2009-11-17 20:03:05 +1100339 if (adb_controller != NULL && adb_controller->init &&
340 adb_controller->init())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 adb_controller = NULL;
Finn Thain18814ee2009-11-17 20:03:05 +1100342 if (adb_controller == NULL) {
343 printk(KERN_WARNING "Warning: no ADB interface detected\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345#ifdef CONFIG_PPC
Grant Likely71a157e2010-02-01 21:34:14 -0700346 if (of_machine_is_compatible("AAPL,PowerBook1998") ||
347 of_machine_is_compatible("PowerBook1,1"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 sleepy_trackpad = 1;
349#endif /* CONFIG_PPC */
Johannes Bergc9f6d3d2007-12-12 01:21:25 +1100350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 adbdev_init();
352 adb_reset_bus();
353 }
354 return 0;
355}
356
Robert P. J. Dayfaa5b9d2008-05-15 09:12:53 +1000357device_initcall(adb_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359static int
360do_adb_reset_bus(void)
361{
Johannes Berg70b52b32007-03-19 11:53:55 +0100362 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
364 if (adb_controller == NULL)
365 return -ENXIO;
366
367 if (adb_controller->autopoll)
368 adb_controller->autopoll(0);
369
Johannes Berg70b52b32007-03-19 11:53:55 +0100370 blocking_notifier_call_chain(&adb_client_list,
371 ADB_MSG_PRE_RESET, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373 if (sleepy_trackpad) {
374 /* Let the trackpad settle down */
Paul Mackerrasc61dace2007-12-13 15:11:22 +1100375 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 }
377
Daniel Walkeraf3ce512008-05-03 06:34:03 +1000378 mutex_lock(&adb_handler_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 write_lock_irq(&adb_handler_lock);
380 memset(adb_handler, 0, sizeof(adb_handler));
381 write_unlock_irq(&adb_handler_lock);
382
383 /* That one is still a bit synchronous, oh well... */
384 if (adb_controller->reset_bus)
385 ret = adb_controller->reset_bus();
386 else
387 ret = 0;
388
389 if (sleepy_trackpad) {
390 /* Let the trackpad settle down */
Paul Mackerrasc61dace2007-12-13 15:11:22 +1100391 msleep(1500);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 }
393
394 if (!ret) {
395 autopoll_devs = adb_scan_bus();
396 if (adb_controller->autopoll)
397 adb_controller->autopoll(autopoll_devs);
398 }
Daniel Walkeraf3ce512008-05-03 06:34:03 +1000399 mutex_unlock(&adb_handler_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Johannes Berg70b52b32007-03-19 11:53:55 +0100401 blocking_notifier_call_chain(&adb_client_list,
402 ADB_MSG_POST_RESET, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404 return ret;
405}
406
407void
408adb_poll(void)
409{
410 if ((adb_controller == NULL)||(adb_controller->poll == NULL))
411 return;
412 adb_controller->poll();
413}
414
Paul Mackerrasc61dace2007-12-13 15:11:22 +1100415static void adb_sync_req_done(struct adb_request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416{
Paul Mackerrasc61dace2007-12-13 15:11:22 +1100417 struct completion *comp = req->arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Paul Mackerrasc61dace2007-12-13 15:11:22 +1100419 complete(comp);
420}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
422int
423adb_request(struct adb_request *req, void (*done)(struct adb_request *),
424 int flags, int nbytes, ...)
425{
426 va_list list;
Paul Mackerrasc61dace2007-12-13 15:11:22 +1100427 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 int rc;
Paul Mackerrasc61dace2007-12-13 15:11:22 +1100429 struct completion comp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431 if ((adb_controller == NULL) || (adb_controller->send_request == NULL))
432 return -ENXIO;
433 if (nbytes < 1)
434 return -EINVAL;
Paul Mackerrasc61dace2007-12-13 15:11:22 +1100435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 req->nbytes = nbytes+1;
437 req->done = done;
438 req->reply_expected = flags & ADBREQ_REPLY;
439 req->data[0] = ADB_PACKET;
440 va_start(list, nbytes);
441 for (i = 0; i < nbytes; ++i)
442 req->data[i+1] = va_arg(list, int);
443 va_end(list);
444
445 if (flags & ADBREQ_NOSEND)
446 return 0;
447
Paul Mackerrasc61dace2007-12-13 15:11:22 +1100448 /* Synchronous requests block using an on-stack completion */
449 if (flags & ADBREQ_SYNC) {
450 WARN_ON(done);
451 req->done = adb_sync_req_done;
452 req->arg = &comp;
453 init_completion(&comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 }
455
Paul Mackerrasc61dace2007-12-13 15:11:22 +1100456 rc = adb_controller->send_request(req, 0);
457
458 if ((flags & ADBREQ_SYNC) && !rc && !req->complete)
459 wait_for_completion(&comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
461 return rc;
462}
463
464 /* Ultimately this should return the number of devices with
465 the given default id.
466 And it does it now ! Note: changed behaviour: This function
467 will now register if default_id _and_ handler_id both match
468 but handler_id can be left to 0 to match with default_id only.
469 When handler_id is set, this function will try to adjust
470 the handler_id id it doesn't match. */
471int
472adb_register(int default_id, int handler_id, struct adb_ids *ids,
David Howells7d12e782006-10-05 14:55:46 +0100473 void (*handler)(unsigned char *, int, int))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
475 int i;
476
Daniel Walkeraf3ce512008-05-03 06:34:03 +1000477 mutex_lock(&adb_handler_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 ids->nids = 0;
479 for (i = 1; i < 16; i++) {
480 if ((adb_handler[i].original_address == default_id) &&
481 (!handler_id || (handler_id == adb_handler[i].handler_id) ||
482 try_handler_change(i, handler_id))) {
483 if (adb_handler[i].handler != 0) {
484 printk(KERN_ERR
485 "Two handlers for ADB device %d\n",
486 default_id);
487 continue;
488 }
489 write_lock_irq(&adb_handler_lock);
490 adb_handler[i].handler = handler;
491 write_unlock_irq(&adb_handler_lock);
492 ids->id[ids->nids++] = i;
493 }
494 }
Daniel Walkeraf3ce512008-05-03 06:34:03 +1000495 mutex_unlock(&adb_handler_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 return ids->nids;
497}
498
499int
500adb_unregister(int index)
501{
502 int ret = -ENODEV;
503
Daniel Walkeraf3ce512008-05-03 06:34:03 +1000504 mutex_lock(&adb_handler_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 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);
Daniel Walkeraf3ce512008-05-03 06:34:03 +1000516 mutex_unlock(&adb_handler_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 return ret;
518}
519
520void
David Howells7d12e782006-10-05 14:55:46 +0100521adb_input(unsigned char *buf, int nb, int autopoll)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
523 int i, id;
Brandon Stewart74e7cd42014-01-27 19:43:17 -0600524 static int dump_adb_input;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 unsigned long flags;
526
David Howells7d12e782006-10-05 14:55:46 +0100527 void (*handler)(unsigned char *, int, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
529 /* We skip keystrokes and mouse moves when the sleep process
530 * has been started. We stop autopoll, but this is another security
531 */
532 if (adb_got_sleep)
533 return;
534
535 id = buf[0] >> 4;
536 if (dump_adb_input) {
537 printk(KERN_INFO "adb packet: ");
538 for (i = 0; i < nb; ++i)
539 printk(" %x", buf[i]);
540 printk(", id = %d\n", id);
541 }
542 write_lock_irqsave(&adb_handler_lock, flags);
543 handler = adb_handler[id].handler;
544 if (handler != NULL)
545 adb_handler[id].busy = 1;
546 write_unlock_irqrestore(&adb_handler_lock, flags);
547 if (handler != NULL) {
David Howells7d12e782006-10-05 14:55:46 +0100548 (*handler)(buf, nb, autopoll);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 wmb();
550 adb_handler[id].busy = 0;
551 }
552
553}
554
555/* Try to change handler to new_id. Will return 1 if successful. */
556static int try_handler_change(int address, int new_id)
557{
558 struct adb_request req;
559
560 if (adb_handler[address].handler_id == new_id)
561 return 1;
562 adb_request(&req, NULL, ADBREQ_SYNC, 3,
563 ADB_WRITEREG(address, 3), address | 0x20, new_id);
564 adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
565 ADB_READREG(address, 3));
566 if (req.reply_len < 2)
567 return 0;
568 if (req.reply[2] != new_id)
569 return 0;
570 adb_handler[address].handler_id = req.reply[2];
571
572 return 1;
573}
574
575int
576adb_try_handler_change(int address, int new_id)
577{
578 int ret;
579
Daniel Walkeraf3ce512008-05-03 06:34:03 +1000580 mutex_lock(&adb_handler_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 ret = try_handler_change(address, new_id);
Daniel Walkeraf3ce512008-05-03 06:34:03 +1000582 mutex_unlock(&adb_handler_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 return ret;
584}
585
586int
587adb_get_infos(int address, int *original_address, int *handler_id)
588{
Daniel Walkeraf3ce512008-05-03 06:34:03 +1000589 mutex_lock(&adb_handler_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 *original_address = adb_handler[address].original_address;
591 *handler_id = adb_handler[address].handler_id;
Daniel Walkeraf3ce512008-05-03 06:34:03 +1000592 mutex_unlock(&adb_handler_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594 return (*original_address != 0);
595}
596
597
598/*
599 * /dev/adb device driver.
600 */
601
602#define ADB_MAJOR 56 /* major number for /dev/adb */
603
604struct adbdev_state {
605 spinlock_t lock;
606 atomic_t n_pending;
607 struct adb_request *completed;
608 wait_queue_head_t wait_queue;
609 int inuse;
610};
611
612static void adb_write_done(struct adb_request *req)
613{
614 struct adbdev_state *state = (struct adbdev_state *) req->arg;
615 unsigned long flags;
616
617 if (!req->complete) {
618 req->reply_len = 0;
619 req->complete = 1;
620 }
621 spin_lock_irqsave(&state->lock, flags);
622 atomic_dec(&state->n_pending);
623 if (!state->inuse) {
624 kfree(req);
625 if (atomic_read(&state->n_pending) == 0) {
626 spin_unlock_irqrestore(&state->lock, flags);
627 kfree(state);
628 return;
629 }
630 } else {
631 struct adb_request **ap = &state->completed;
632 while (*ap != NULL)
633 ap = &(*ap)->next;
634 req->next = NULL;
635 *ap = req;
636 wake_up_interruptible(&state->wait_queue);
637 }
638 spin_unlock_irqrestore(&state->lock, flags);
639}
640
641static int
642do_adb_query(struct adb_request *req)
643{
644 int ret = -EINVAL;
645
Brandon Stewart74e7cd42014-01-27 19:43:17 -0600646 switch(req->data[1]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 case ADB_QUERY_GETDEVINFO:
648 if (req->nbytes < 3)
649 break;
Daniel Walkeraf3ce512008-05-03 06:34:03 +1000650 mutex_lock(&adb_handler_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 req->reply[0] = adb_handler[req->data[2]].original_address;
652 req->reply[1] = adb_handler[req->data[2]].handler_id;
Daniel Walkeraf3ce512008-05-03 06:34:03 +1000653 mutex_unlock(&adb_handler_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 req->complete = 1;
655 req->reply_len = 2;
656 adb_write_done(req);
657 ret = 0;
658 break;
659 }
660 return ret;
661}
662
663static int adb_open(struct inode *inode, struct file *file)
664{
665 struct adbdev_state *state;
Jonathan Corbet26ce4f02008-05-16 14:19:56 -0600666 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Arnd Bergmannd851b6e2010-06-02 14:28:52 +0200668 mutex_lock(&adb_mutex);
Jonathan Corbet26ce4f02008-05-16 14:19:56 -0600669 if (iminor(inode) > 0 || adb_controller == NULL) {
670 ret = -ENXIO;
671 goto out;
672 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 state = kmalloc(sizeof(struct adbdev_state), GFP_KERNEL);
Jonathan Corbet26ce4f02008-05-16 14:19:56 -0600674 if (state == 0) {
675 ret = -ENOMEM;
676 goto out;
677 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 file->private_data = state;
679 spin_lock_init(&state->lock);
680 atomic_set(&state->n_pending, 0);
681 state->completed = NULL;
682 init_waitqueue_head(&state->wait_queue);
683 state->inuse = 1;
684
Jonathan Corbet26ce4f02008-05-16 14:19:56 -0600685out:
Arnd Bergmannd851b6e2010-06-02 14:28:52 +0200686 mutex_unlock(&adb_mutex);
Jonathan Corbet26ce4f02008-05-16 14:19:56 -0600687 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688}
689
690static int adb_release(struct inode *inode, struct file *file)
691{
692 struct adbdev_state *state = file->private_data;
693 unsigned long flags;
694
Arnd Bergmannd851b6e2010-06-02 14:28:52 +0200695 mutex_lock(&adb_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 if (state) {
697 file->private_data = NULL;
698 spin_lock_irqsave(&state->lock, flags);
699 if (atomic_read(&state->n_pending) == 0
700 && state->completed == NULL) {
701 spin_unlock_irqrestore(&state->lock, flags);
702 kfree(state);
703 } else {
704 state->inuse = 0;
705 spin_unlock_irqrestore(&state->lock, flags);
706 }
707 }
Arnd Bergmannd851b6e2010-06-02 14:28:52 +0200708 mutex_unlock(&adb_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 return 0;
710}
711
712static ssize_t adb_read(struct file *file, char __user *buf,
713 size_t count, loff_t *ppos)
714{
715 int ret = 0;
716 struct adbdev_state *state = file->private_data;
717 struct adb_request *req;
Brandon Stewart74e7cd42014-01-27 19:43:17 -0600718 DECLARE_WAITQUEUE(wait, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 unsigned long flags;
720
721 if (count < 2)
722 return -EINVAL;
723 if (count > sizeof(req->reply))
724 count = sizeof(req->reply);
725 if (!access_ok(VERIFY_WRITE, buf, count))
726 return -EFAULT;
727
728 req = NULL;
729 spin_lock_irqsave(&state->lock, flags);
730 add_wait_queue(&state->wait_queue, &wait);
majianpeng64f8c1352012-02-03 14:35:59 +0000731 set_current_state(TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
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
majianpeng64f8c1352012-02-03 14:35:59 +0000755 set_current_state(TASK_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 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/*, i*/;
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 if (!access_ok(VERIFY_READ, buf, count))
784 return -EFAULT;
785
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800786 req = kmalloc(sizeof(struct adb_request),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 GFP_KERNEL);
788 if (req == NULL)
789 return -ENOMEM;
790
791 req->nbytes = count;
792 req->done = adb_write_done;
793 req->arg = (void *) state;
794 req->complete = 0;
795
796 ret = -EFAULT;
797 if (copy_from_user(req->data, buf, count))
798 goto out;
799
800 atomic_inc(&state->n_pending);
801
802 /* If a probe is in progress or we are sleeping, wait for it to complete */
803 down(&adb_probe_mutex);
804
805 /* Queries are special requests sent to the ADB driver itself */
806 if (req->data[0] == ADB_QUERY) {
807 if (count > 1)
808 ret = do_adb_query(req);
809 else
810 ret = -EINVAL;
811 up(&adb_probe_mutex);
812 }
813 /* Special case for ADB_BUSRESET request, all others are sent to
814 the controller */
Brandon Stewart74e7cd42014-01-27 19:43:17 -0600815 else if ((req->data[0] == ADB_PACKET) && (count > 1)
816 && (req->data[1] == ADB_BUSRESET)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 ret = do_adb_reset_bus();
818 up(&adb_probe_mutex);
819 atomic_dec(&state->n_pending);
820 if (ret == 0)
821 ret = count;
822 goto out;
823 } else {
824 req->reply_expected = ((req->data[1] & 0xc) == 0xc);
825 if (adb_controller && adb_controller->send_request)
826 ret = adb_controller->send_request(req, 0);
827 else
828 ret = -ENXIO;
829 up(&adb_probe_mutex);
830 }
831
832 if (ret != 0) {
833 atomic_dec(&state->n_pending);
834 goto out;
835 }
836 return count;
837
838out:
839 kfree(req);
840 return ret;
841}
842
Arjan van de Venfa027c22007-02-12 00:55:33 -0800843static const struct file_operations adb_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 .owner = THIS_MODULE,
845 .llseek = no_llseek,
846 .read = adb_read,
847 .write = adb_write,
848 .open = adb_open,
849 .release = adb_release,
850};
851
Shuah Khan639291f22014-02-10 09:12:27 -0700852#ifdef CONFIG_PM
853static const struct dev_pm_ops adb_dev_pm_ops = {
854 .suspend = adb_suspend,
855 .resume = adb_resume,
856 /* Hibernate hooks */
857 .freeze = adb_freeze,
858 .thaw = adb_resume,
859 .poweroff = adb_poweroff,
860 .restore = adb_resume,
861};
862#endif
863
Johannes Bergc9f6d3d2007-12-12 01:21:25 +1100864static struct platform_driver adb_pfdrv = {
865 .driver = {
866 .name = "adb",
Johannes Bergc9f6d3d2007-12-12 01:21:25 +1100867#ifdef CONFIG_PM
Shuah Khan639291f22014-02-10 09:12:27 -0700868 .pm = &adb_dev_pm_ops,
Johannes Bergc9f6d3d2007-12-12 01:21:25 +1100869#endif
Shuah Khan639291f22014-02-10 09:12:27 -0700870 },
Johannes Bergc9f6d3d2007-12-12 01:21:25 +1100871};
872
873static struct platform_device adb_pfdev = {
874 .name = "adb",
875};
876
877static int __init
878adb_dummy_probe(struct platform_device *dev)
879{
880 if (dev == &adb_pfdev)
881 return 0;
882 return -ENODEV;
883}
884
885static void __init
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886adbdev_init(void)
887{
888 if (register_chrdev(ADB_MAJOR, "adb", &adb_fops)) {
889 printk(KERN_ERR "adb: unable to get major %d\n", ADB_MAJOR);
890 return;
891 }
892
gregkh@suse.de56b22932005-03-23 10:01:41 -0800893 adb_dev_class = class_create(THIS_MODULE, "adb");
894 if (IS_ERR(adb_dev_class))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 return;
Greg Kroah-Hartmana9b12612008-07-21 20:03:34 -0700896 device_create(adb_dev_class, NULL, MKDEV(ADB_MAJOR, 0), NULL, "adb");
Johannes Bergc9f6d3d2007-12-12 01:21:25 +1100897
898 platform_device_register(&adb_pfdev);
899 platform_driver_probe(&adb_pfdrv, adb_dummy_probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900}