Blame view

drivers/macintosh/adb.c 19.3 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  /*
   * Device driver for the Apple Desktop Bus
   * and the /dev/adb device on macintoshes.
   *
   * Copyright (C) 1996 Paul Mackerras.
   *
   * Modified to declare controllers as structures, added
   * client notification of bus reset and handles PowerBook
   * sleep, by Benjamin Herrenschmidt.
   *
   * To do:
   *
   * - /sys/bus/adb to list the devices and infos
   * - more /dev/adb to allow userland to receive the
   *   flow of auto-polling datas from a given device.
   * - move bus probe to a kernel thread
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
18
19
20
21
22
23
24
25
  #include <linux/types.h>
  #include <linux/errno.h>
  #include <linux/kernel.h>
  #include <linux/slab.h>
  #include <linux/module.h>
  #include <linux/fs.h>
  #include <linux/mm.h>
  #include <linux/sched.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
26
27
28
29
30
31
32
33
34
35
  #include <linux/adb.h>
  #include <linux/cuda.h>
  #include <linux/pmu.h>
  #include <linux/notifier.h>
  #include <linux/wait.h>
  #include <linux/init.h>
  #include <linux/delay.h>
  #include <linux/spinlock.h>
  #include <linux/completion.h>
  #include <linux/device.h>
c61dace9a   Paul Mackerras   [POWERPC] Convert...
36
  #include <linux/kthread.h>
fe2528b96   Geert Uytterhoeven   ADB: Add missing ...
37
  #include <linux/platform_device.h>
af3ce514a   Daniel Walker   [POWERPC] macinto...
38
  #include <linux/mutex.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
39
40
  
  #include <asm/uaccess.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
41
42
  #ifdef CONFIG_PPC
  #include <asm/prom.h>
e8222502e   Benjamin Herrenschmidt   [PATCH] powerpc: ...
43
  #include <asm/machdep.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
44
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
45
46
47
48
49
50
51
52
  EXPORT_SYMBOL(adb_client_list);
  
  extern struct adb_driver via_macii_driver;
  extern struct adb_driver via_maciisi_driver;
  extern struct adb_driver via_cuda_driver;
  extern struct adb_driver adb_iop_driver;
  extern struct adb_driver via_pmu_driver;
  extern struct adb_driver macio_adb_driver;
d851b6e04   Arnd Bergmann   mac: autoconvert ...
53
  static DEFINE_MUTEX(adb_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
  static struct adb_driver *adb_driver_list[] = {
  #ifdef CONFIG_ADB_MACII
  	&via_macii_driver,
  #endif
  #ifdef CONFIG_ADB_MACIISI
  	&via_maciisi_driver,
  #endif
  #ifdef CONFIG_ADB_CUDA
  	&via_cuda_driver,
  #endif
  #ifdef CONFIG_ADB_IOP
  	&adb_iop_driver,
  #endif
  #if defined(CONFIG_ADB_PMU) || defined(CONFIG_ADB_PMU68K)
  	&via_pmu_driver,
  #endif
  #ifdef CONFIG_ADB_MACIO
  	&macio_adb_driver,
  #endif
  	NULL
  };
56b229359   Greg Kroah-Hartman   [PATCH] class: co...
75
  static struct class *adb_dev_class;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
76

9b4a8dd2e   Adrian Bunk   drivers/macintosh...
77
  static struct adb_driver *adb_controller;
e041c6834   Alan Stern   [PATCH] Notifier ...
78
  BLOCKING_NOTIFIER_HEAD(adb_client_list);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
79
80
  static int adb_got_sleep;
  static int adb_inited;
8192b1f6b   Thomas Gleixner   drivers/macintosh...
81
  static DEFINE_SEMAPHORE(adb_probe_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
82
83
84
  static int sleepy_trackpad;
  static int autopoll_devs;
  int __adb_probe_sync;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
85
86
87
88
89
90
  static int adb_scan_bus(void);
  static int do_adb_reset_bus(void);
  static void adbdev_init(void);
  static int try_handler_change(int, int);
  
  static struct adb_handler {
7d12e780e   David Howells   IRQ: Maintain reg...
91
  	void (*handler)(unsigned char *, int, int);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
92
93
94
95
96
97
  	int original_address;
  	int handler_id;
  	int busy;
  } adb_handler[16];
  
  /*
af3ce514a   Daniel Walker   [POWERPC] macinto...
98
   * The adb_handler_mutex mutex protects all accesses to the original_address
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
99
100
101
102
103
104
105
   * and handler_id fields of adb_handler[i] for all i, and changes to the
   * handler field.
   * Accesses to the handler field are protected by the adb_handler_lock
   * rwlock.  It is held across all calls to any handler, so that by the
   * time adb_unregister returns, we know that the old handler isn't being
   * called.
   */
af3ce514a   Daniel Walker   [POWERPC] macinto...
106
  static DEFINE_MUTEX(adb_handler_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
  static DEFINE_RWLOCK(adb_handler_lock);
  
  #if 0
  static void printADBreply(struct adb_request *req)
  {
          int i;
  
          printk("adb reply (%d)", req->reply_len);
          for(i = 0; i < req->reply_len; i++)
                  printk(" %x", req->reply[i]);
          printk("
  ");
  
  }
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
  static int adb_scan_bus(void)
  {
  	int i, highFree=0, noMovement;
  	int devmask = 0;
  	struct adb_request req;
  	
  	/* assumes adb_handler[] is all zeroes at this point */
  	for (i = 1; i < 16; i++) {
  		/* see if there is anything at address i */
  		adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
                              (i << 4) | 0xf);
  		if (req.reply_len > 1)
  			/* one or more devices at this address */
  			adb_handler[i].original_address = i;
  		else if (i > highFree)
  			highFree = i;
  	}
  
  	/* Note we reset noMovement to 0 each time we move a device */
  	for (noMovement = 1; noMovement < 2 && highFree > 0; noMovement++) {
  		for (i = 1; i < 16; i++) {
  			if (adb_handler[i].original_address == 0)
  				continue;
  			/*
  			 * Send a "talk register 3" command to address i
  			 * to provoke a collision if there is more than
  			 * one device at this address.
  			 */
  			adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
  				    (i << 4) | 0xf);
  			/*
  			 * Move the device(s) which didn't detect a
  			 * collision to address `highFree'.  Hopefully
  			 * this only moves one device.
  			 */
  			adb_request(&req, NULL, ADBREQ_SYNC, 3,
  				    (i<< 4) | 0xb, (highFree | 0x60), 0xfe);
  			/*
  			 * See if anybody actually moved. This is suggested
  			 * by HW TechNote 01:
  			 *
  			 * http://developer.apple.com/technotes/hw/hw_01.html
  			 */
  			adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
  				    (highFree << 4) | 0xf);
  			if (req.reply_len <= 1) continue;
  			/*
  			 * Test whether there are any device(s) left
  			 * at address i.
  			 */
  			adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
  				    (i << 4) | 0xf);
  			if (req.reply_len > 1) {
  				/*
  				 * There are still one or more devices
  				 * left at address i.  Register the one(s)
  				 * we moved to `highFree', and find a new
  				 * value for highFree.
  				 */
  				adb_handler[highFree].original_address =
  					adb_handler[i].original_address;
  				while (highFree > 0 &&
  				       adb_handler[highFree].original_address)
  					highFree--;
  				if (highFree <= 0)
  					break;
  
  				noMovement = 0;
  			}
  			else {
  				/*
  				 * No devices left at address i; move the
  				 * one(s) we moved to `highFree' back to i.
  				 */
  				adb_request(&req, NULL, ADBREQ_SYNC, 3,
  					    (highFree << 4) | 0xb,
  					    (i | 0x60), 0xfe);
  			}
  		}	
  	}
  
  	/* Now fill in the handler_id field of the adb_handler entries. */
  	printk(KERN_DEBUG "adb devices:");
  	for (i = 1; i < 16; i++) {
  		if (adb_handler[i].original_address == 0)
  			continue;
  		adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
  			    (i << 4) | 0xf);
  		adb_handler[i].handler_id = req.reply[2];
  		printk(" [%d]: %d %x", i, adb_handler[i].original_address,
  		       adb_handler[i].handler_id);
  		devmask |= 1 << i;
  	}
  	printk("
  ");
  	return devmask;
  }
  
  /*
   * This kernel task handles ADB probing. It dies once probing is
   * completed.
   */
  static int
  adb_probe_task(void *x)
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
227
228
229
230
231
  	printk(KERN_INFO "adb: starting probe task...
  ");
  	do_adb_reset_bus();
  	printk(KERN_INFO "adb: finished probe task...
  ");
1b6dd9baa   Oleg Nesterov   adb_probe_task: r...
232

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
233
  	up(&adb_probe_mutex);
1b6dd9baa   Oleg Nesterov   adb_probe_task: r...
234

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
235
236
237
238
  	return 0;
  }
  
  static void
3e577a80e   Al Viro   [PATCH] drivers/{...
239
  __adb_probe_task(struct work_struct *bullshit)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
240
  {
c61dace9a   Paul Mackerras   [POWERPC] Convert...
241
  	kthread_run(adb_probe_task, NULL, "kadbprobe");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
242
  }
3e577a80e   Al Viro   [PATCH] drivers/{...
243
  static DECLARE_WORK(adb_reset_work, __adb_probe_task);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
244
245
246
247
248
249
250
251
252
253
254
255
256
  
  int
  adb_reset_bus(void)
  {
  	if (__adb_probe_sync) {
  		do_adb_reset_bus();
  		return 0;
  	}
  
  	down(&adb_probe_mutex);
  	schedule_work(&adb_reset_work);
  	return 0;
  }
c9f6d3d5c   Johannes Berg   [POWERPC] adb: Re...
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
  #ifdef CONFIG_PM
  /*
   * notify clients before sleep
   */
  static int adb_suspend(struct platform_device *dev, pm_message_t state)
  {
  	adb_got_sleep = 1;
  	/* We need to get a lock on the probe thread */
  	down(&adb_probe_mutex);
  	/* Stop autopoll */
  	if (adb_controller->autopoll)
  		adb_controller->autopoll(0);
  	blocking_notifier_call_chain(&adb_client_list, ADB_MSG_POWERDOWN, NULL);
  
  	return 0;
  }
  
  /*
   * reset bus after sleep
   */
  static int adb_resume(struct platform_device *dev)
  {
  	adb_got_sleep = 0;
  	up(&adb_probe_mutex);
  	adb_reset_bus();
  
  	return 0;
  }
  #endif /* CONFIG_PM */
9b4a8dd2e   Adrian Bunk   drivers/macintosh...
286
  static int __init adb_init(void)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
287
288
289
290
291
  {
  	struct adb_driver *driver;
  	int i;
  
  #ifdef CONFIG_PPC32
e8222502e   Benjamin Herrenschmidt   [PATCH] powerpc: ...
292
  	if (!machine_is(chrp) && !machine_is(powermac))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
  		return 0;
  #endif
  #ifdef CONFIG_MAC
  	if (!MACH_IS_MAC)
  		return 0;
  #endif
  
  	/* xmon may do early-init */
  	if (adb_inited)
  		return 0;
  	adb_inited = 1;
  		
  	adb_controller = NULL;
  
  	i = 0;
  	while ((driver = adb_driver_list[i++]) != NULL) {
  		if (!driver->probe()) {
  			adb_controller = driver;
  			break;
  		}
  	}
18814ee84   Finn Thain   mac68k: start CUD...
314
315
  	if (adb_controller != NULL && adb_controller->init &&
  	    adb_controller->init())
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
316
  		adb_controller = NULL;
18814ee84   Finn Thain   mac68k: start CUD...
317
318
319
  	if (adb_controller == NULL) {
  		printk(KERN_WARNING "Warning: no ADB interface detected
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
320
  	} else {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
321
  #ifdef CONFIG_PPC
71a157e8e   Grant Likely   of: add 'of_' pre...
322
323
  		if (of_machine_is_compatible("AAPL,PowerBook1998") ||
  			of_machine_is_compatible("PowerBook1,1"))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
324
325
  			sleepy_trackpad = 1;
  #endif /* CONFIG_PPC */
c9f6d3d5c   Johannes Berg   [POWERPC] adb: Re...
326

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
327
328
329
330
331
  		adbdev_init();
  		adb_reset_bus();
  	}
  	return 0;
  }
faa5b9daa   Robert P. J. Day   [POWERPC] macinto...
332
  device_initcall(adb_init);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
333

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
334
335
336
  static int
  do_adb_reset_bus(void)
  {
70b52b386   Johannes Berg   [POWERPC] powerma...
337
  	int ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
338
339
340
341
342
343
  	
  	if (adb_controller == NULL)
  		return -ENXIO;
  		
  	if (adb_controller->autopoll)
  		adb_controller->autopoll(0);
70b52b386   Johannes Berg   [POWERPC] powerma...
344
345
  	blocking_notifier_call_chain(&adb_client_list,
  		ADB_MSG_PRE_RESET, NULL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
346
347
348
  
  	if (sleepy_trackpad) {
  		/* Let the trackpad settle down */
c61dace9a   Paul Mackerras   [POWERPC] Convert...
349
  		msleep(500);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
350
  	}
af3ce514a   Daniel Walker   [POWERPC] macinto...
351
  	mutex_lock(&adb_handler_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
352
353
354
355
356
357
358
359
360
361
362
363
  	write_lock_irq(&adb_handler_lock);
  	memset(adb_handler, 0, sizeof(adb_handler));
  	write_unlock_irq(&adb_handler_lock);
  
  	/* That one is still a bit synchronous, oh well... */
  	if (adb_controller->reset_bus)
  		ret = adb_controller->reset_bus();
  	else
  		ret = 0;
  
  	if (sleepy_trackpad) {
  		/* Let the trackpad settle down */
c61dace9a   Paul Mackerras   [POWERPC] Convert...
364
  		msleep(1500);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
365
366
367
368
369
370
371
  	}
  
  	if (!ret) {
  		autopoll_devs = adb_scan_bus();
  		if (adb_controller->autopoll)
  			adb_controller->autopoll(autopoll_devs);
  	}
af3ce514a   Daniel Walker   [POWERPC] macinto...
372
  	mutex_unlock(&adb_handler_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
373

70b52b386   Johannes Berg   [POWERPC] powerma...
374
375
  	blocking_notifier_call_chain(&adb_client_list,
  		ADB_MSG_POST_RESET, NULL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
376
377
378
379
380
381
382
383
384
385
386
  	
  	return ret;
  }
  
  void
  adb_poll(void)
  {
  	if ((adb_controller == NULL)||(adb_controller->poll == NULL))
  		return;
  	adb_controller->poll();
  }
c61dace9a   Paul Mackerras   [POWERPC] Convert...
387
  static void adb_sync_req_done(struct adb_request *req)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
388
  {
c61dace9a   Paul Mackerras   [POWERPC] Convert...
389
  	struct completion *comp = req->arg;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
390

c61dace9a   Paul Mackerras   [POWERPC] Convert...
391
392
  	complete(comp);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
393
394
395
396
397
398
  
  int
  adb_request(struct adb_request *req, void (*done)(struct adb_request *),
  	    int flags, int nbytes, ...)
  {
  	va_list list;
c61dace9a   Paul Mackerras   [POWERPC] Convert...
399
  	int i;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
400
  	int rc;
c61dace9a   Paul Mackerras   [POWERPC] Convert...
401
  	struct completion comp;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
402
403
404
405
406
  
  	if ((adb_controller == NULL) || (adb_controller->send_request == NULL))
  		return -ENXIO;
  	if (nbytes < 1)
  		return -EINVAL;
c61dace9a   Paul Mackerras   [POWERPC] Convert...
407

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
408
409
410
411
412
413
414
415
416
417
418
  	req->nbytes = nbytes+1;
  	req->done = done;
  	req->reply_expected = flags & ADBREQ_REPLY;
  	req->data[0] = ADB_PACKET;
  	va_start(list, nbytes);
  	for (i = 0; i < nbytes; ++i)
  		req->data[i+1] = va_arg(list, int);
  	va_end(list);
  
  	if (flags & ADBREQ_NOSEND)
  		return 0;
c61dace9a   Paul Mackerras   [POWERPC] Convert...
419
420
421
422
423
424
  	/* Synchronous requests block using an on-stack completion */
  	if (flags & ADBREQ_SYNC) {
  		WARN_ON(done);
  		req->done = adb_sync_req_done;
  		req->arg = &comp;
  		init_completion(&comp);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
425
  	}
c61dace9a   Paul Mackerras   [POWERPC] Convert...
426
427
428
429
  	rc = adb_controller->send_request(req, 0);
  
  	if ((flags & ADBREQ_SYNC) && !rc && !req->complete)
  		wait_for_completion(&comp);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
430
431
432
433
434
435
436
437
438
439
440
441
442
  
  	return rc;
  }
  
   /* Ultimately this should return the number of devices with
      the given default id.
      And it does it now ! Note: changed behaviour: This function
      will now register if default_id _and_ handler_id both match
      but handler_id can be left to 0 to match with default_id only.
      When handler_id is set, this function will try to adjust
      the handler_id id it doesn't match. */
  int
  adb_register(int default_id, int handler_id, struct adb_ids *ids,
7d12e780e   David Howells   IRQ: Maintain reg...
443
  	     void (*handler)(unsigned char *, int, int))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
444
445
  {
  	int i;
af3ce514a   Daniel Walker   [POWERPC] macinto...
446
  	mutex_lock(&adb_handler_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
  	ids->nids = 0;
  	for (i = 1; i < 16; i++) {
  		if ((adb_handler[i].original_address == default_id) &&
  		    (!handler_id || (handler_id == adb_handler[i].handler_id) || 
  		    try_handler_change(i, handler_id))) {
  			if (adb_handler[i].handler != 0) {
  				printk(KERN_ERR
  				       "Two handlers for ADB device %d
  ",
  				       default_id);
  				continue;
  			}
  			write_lock_irq(&adb_handler_lock);
  			adb_handler[i].handler = handler;
  			write_unlock_irq(&adb_handler_lock);
  			ids->id[ids->nids++] = i;
  		}
  	}
af3ce514a   Daniel Walker   [POWERPC] macinto...
465
  	mutex_unlock(&adb_handler_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
466
467
468
469
470
471
472
  	return ids->nids;
  }
  
  int
  adb_unregister(int index)
  {
  	int ret = -ENODEV;
af3ce514a   Daniel Walker   [POWERPC] macinto...
473
  	mutex_lock(&adb_handler_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
474
475
476
477
478
479
480
481
482
483
484
  	write_lock_irq(&adb_handler_lock);
  	if (adb_handler[index].handler) {
  		while(adb_handler[index].busy) {
  			write_unlock_irq(&adb_handler_lock);
  			yield();
  			write_lock_irq(&adb_handler_lock);
  		}
  		ret = 0;
  		adb_handler[index].handler = NULL;
  	}
  	write_unlock_irq(&adb_handler_lock);
af3ce514a   Daniel Walker   [POWERPC] macinto...
485
  	mutex_unlock(&adb_handler_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
486
487
488
489
  	return ret;
  }
  
  void
7d12e780e   David Howells   IRQ: Maintain reg...
490
  adb_input(unsigned char *buf, int nb, int autopoll)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
491
492
493
494
495
  {
  	int i, id;
  	static int dump_adb_input = 0;
  	unsigned long flags;
  	
7d12e780e   David Howells   IRQ: Maintain reg...
496
  	void (*handler)(unsigned char *, int, int);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
  
  	/* We skip keystrokes and mouse moves when the sleep process
  	 * has been started. We stop autopoll, but this is another security
  	 */
  	if (adb_got_sleep)
  		return;
  		
  	id = buf[0] >> 4;
  	if (dump_adb_input) {
  		printk(KERN_INFO "adb packet: ");
  		for (i = 0; i < nb; ++i)
  			printk(" %x", buf[i]);
  		printk(", id = %d
  ", id);
  	}
  	write_lock_irqsave(&adb_handler_lock, flags);
  	handler = adb_handler[id].handler;
  	if (handler != NULL)
  		adb_handler[id].busy = 1;
  	write_unlock_irqrestore(&adb_handler_lock, flags);
  	if (handler != NULL) {
7d12e780e   David Howells   IRQ: Maintain reg...
518
  		(*handler)(buf, nb, autopoll);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
  		wmb();
  		adb_handler[id].busy = 0;
  	}
  		
  }
  
  /* Try to change handler to new_id. Will return 1 if successful. */
  static int try_handler_change(int address, int new_id)
  {
  	struct adb_request req;
  
  	if (adb_handler[address].handler_id == new_id)
  	    return 1;
  	adb_request(&req, NULL, ADBREQ_SYNC, 3,
  	    ADB_WRITEREG(address, 3), address | 0x20, new_id);
  	adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
  	    ADB_READREG(address, 3));
  	if (req.reply_len < 2)
  	    return 0;
  	if (req.reply[2] != new_id)
  	    return 0;
  	adb_handler[address].handler_id = req.reply[2];
  
  	return 1;
  }
  
  int
  adb_try_handler_change(int address, int new_id)
  {
  	int ret;
af3ce514a   Daniel Walker   [POWERPC] macinto...
549
  	mutex_lock(&adb_handler_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
550
  	ret = try_handler_change(address, new_id);
af3ce514a   Daniel Walker   [POWERPC] macinto...
551
  	mutex_unlock(&adb_handler_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
552
553
554
555
556
557
  	return ret;
  }
  
  int
  adb_get_infos(int address, int *original_address, int *handler_id)
  {
af3ce514a   Daniel Walker   [POWERPC] macinto...
558
  	mutex_lock(&adb_handler_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
559
560
  	*original_address = adb_handler[address].original_address;
  	*handler_id = adb_handler[address].handler_id;
af3ce514a   Daniel Walker   [POWERPC] macinto...
561
  	mutex_unlock(&adb_handler_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
  
  	return (*original_address != 0);
  }
  
  
  /*
   * /dev/adb device driver.
   */
  
  #define ADB_MAJOR	56	/* major number for /dev/adb */
  
  struct adbdev_state {
  	spinlock_t	lock;
  	atomic_t	n_pending;
  	struct adb_request *completed;
    	wait_queue_head_t wait_queue;
  	int		inuse;
  };
  
  static void adb_write_done(struct adb_request *req)
  {
  	struct adbdev_state *state = (struct adbdev_state *) req->arg;
  	unsigned long flags;
  
  	if (!req->complete) {
  		req->reply_len = 0;
  		req->complete = 1;
  	}
  	spin_lock_irqsave(&state->lock, flags);
  	atomic_dec(&state->n_pending);
  	if (!state->inuse) {
  		kfree(req);
  		if (atomic_read(&state->n_pending) == 0) {
  			spin_unlock_irqrestore(&state->lock, flags);
  			kfree(state);
  			return;
  		}
  	} else {
  		struct adb_request **ap = &state->completed;
  		while (*ap != NULL)
  			ap = &(*ap)->next;
  		req->next = NULL;
  		*ap = req;
  		wake_up_interruptible(&state->wait_queue);
  	}
  	spin_unlock_irqrestore(&state->lock, flags);
  }
  
  static int
  do_adb_query(struct adb_request *req)
  {
  	int	ret = -EINVAL;
  
  	switch(req->data[1])
  	{
  	case ADB_QUERY_GETDEVINFO:
  		if (req->nbytes < 3)
  			break;
af3ce514a   Daniel Walker   [POWERPC] macinto...
620
  		mutex_lock(&adb_handler_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
621
622
  		req->reply[0] = adb_handler[req->data[2]].original_address;
  		req->reply[1] = adb_handler[req->data[2]].handler_id;
af3ce514a   Daniel Walker   [POWERPC] macinto...
623
  		mutex_unlock(&adb_handler_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
624
625
626
627
628
629
630
631
632
633
634
635
  		req->complete = 1;
  		req->reply_len = 2;
  		adb_write_done(req);
  		ret = 0;
  		break;
  	}
  	return ret;
  }
  
  static int adb_open(struct inode *inode, struct file *file)
  {
  	struct adbdev_state *state;
26ce4f068   Jonathan Corbet   adb: BKL pushdown
636
  	int ret = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
637

d851b6e04   Arnd Bergmann   mac: autoconvert ...
638
  	mutex_lock(&adb_mutex);
26ce4f068   Jonathan Corbet   adb: BKL pushdown
639
640
641
642
  	if (iminor(inode) > 0 || adb_controller == NULL) {
  		ret = -ENXIO;
  		goto out;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
643
  	state = kmalloc(sizeof(struct adbdev_state), GFP_KERNEL);
26ce4f068   Jonathan Corbet   adb: BKL pushdown
644
645
646
647
  	if (state == 0) {
  		ret = -ENOMEM;
  		goto out;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
648
649
650
651
652
653
  	file->private_data = state;
  	spin_lock_init(&state->lock);
  	atomic_set(&state->n_pending, 0);
  	state->completed = NULL;
  	init_waitqueue_head(&state->wait_queue);
  	state->inuse = 1;
26ce4f068   Jonathan Corbet   adb: BKL pushdown
654
  out:
d851b6e04   Arnd Bergmann   mac: autoconvert ...
655
  	mutex_unlock(&adb_mutex);
26ce4f068   Jonathan Corbet   adb: BKL pushdown
656
  	return ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
657
658
659
660
661
662
  }
  
  static int adb_release(struct inode *inode, struct file *file)
  {
  	struct adbdev_state *state = file->private_data;
  	unsigned long flags;
d851b6e04   Arnd Bergmann   mac: autoconvert ...
663
  	mutex_lock(&adb_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
664
665
666
667
668
669
670
671
672
673
674
675
  	if (state) {
  		file->private_data = NULL;
  		spin_lock_irqsave(&state->lock, flags);
  		if (atomic_read(&state->n_pending) == 0
  		    && state->completed == NULL) {
  			spin_unlock_irqrestore(&state->lock, flags);
  			kfree(state);
  		} else {
  			state->inuse = 0;
  			spin_unlock_irqrestore(&state->lock, flags);
  		}
  	}
d851b6e04   Arnd Bergmann   mac: autoconvert ...
676
  	mutex_unlock(&adb_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
  	return 0;
  }
  
  static ssize_t adb_read(struct file *file, char __user *buf,
  			size_t count, loff_t *ppos)
  {
  	int ret = 0;
  	struct adbdev_state *state = file->private_data;
  	struct adb_request *req;
  	wait_queue_t wait = __WAITQUEUE_INITIALIZER(wait,current);
  	unsigned long flags;
  
  	if (count < 2)
  		return -EINVAL;
  	if (count > sizeof(req->reply))
  		count = sizeof(req->reply);
  	if (!access_ok(VERIFY_WRITE, buf, count))
  		return -EFAULT;
  
  	req = NULL;
  	spin_lock_irqsave(&state->lock, flags);
  	add_wait_queue(&state->wait_queue, &wait);
  	current->state = TASK_INTERRUPTIBLE;
  
  	for (;;) {
  		req = state->completed;
  		if (req != NULL)
  			state->completed = req->next;
  		else if (atomic_read(&state->n_pending) == 0)
  			ret = -EIO;
  		if (req != NULL || ret != 0)
  			break;
  		
  		if (file->f_flags & O_NONBLOCK) {
  			ret = -EAGAIN;
  			break;
  		}
  		if (signal_pending(current)) {
  			ret = -ERESTARTSYS;
  			break;
  		}
  		spin_unlock_irqrestore(&state->lock, flags);
  		schedule();
  		spin_lock_irqsave(&state->lock, flags);
  	}
  
  	current->state = TASK_RUNNING;
  	remove_wait_queue(&state->wait_queue, &wait);
  	spin_unlock_irqrestore(&state->lock, flags);
  	
  	if (ret)
  		return ret;
  
  	ret = req->reply_len;
  	if (ret > count)
  		ret = count;
  	if (ret > 0 && copy_to_user(buf, req->reply, ret))
  		ret = -EFAULT;
  
  	kfree(req);
  	return ret;
  }
  
  static ssize_t adb_write(struct file *file, const char __user *buf,
  			 size_t count, loff_t *ppos)
  {
  	int ret/*, i*/;
  	struct adbdev_state *state = file->private_data;
  	struct adb_request *req;
  
  	if (count < 2 || count > sizeof(req->data))
  		return -EINVAL;
  	if (adb_controller == NULL)
  		return -ENXIO;
  	if (!access_ok(VERIFY_READ, buf, count))
  		return -EFAULT;
5cbded585   Robert P. J. Day   [PATCH] getting r...
753
  	req = kmalloc(sizeof(struct adb_request),
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
  					     GFP_KERNEL);
  	if (req == NULL)
  		return -ENOMEM;
  
  	req->nbytes = count;
  	req->done = adb_write_done;
  	req->arg = (void *) state;
  	req->complete = 0;
  	
  	ret = -EFAULT;
  	if (copy_from_user(req->data, buf, count))
  		goto out;
  
  	atomic_inc(&state->n_pending);
  
  	/* If a probe is in progress or we are sleeping, wait for it to complete */
  	down(&adb_probe_mutex);
  
  	/* Queries are special requests sent to the ADB driver itself */
  	if (req->data[0] == ADB_QUERY) {
  		if (count > 1)
  			ret = do_adb_query(req);
  		else
  			ret = -EINVAL;
  		up(&adb_probe_mutex);
  	}
  	/* Special case for ADB_BUSRESET request, all others are sent to
  	   the controller */
  	else if ((req->data[0] == ADB_PACKET)&&(count > 1)
  		&&(req->data[1] == ADB_BUSRESET)) {
  		ret = do_adb_reset_bus();
  		up(&adb_probe_mutex);
  		atomic_dec(&state->n_pending);
  		if (ret == 0)
  			ret = count;
  		goto out;
  	} else {	
  		req->reply_expected = ((req->data[1] & 0xc) == 0xc);
  		if (adb_controller && adb_controller->send_request)
  			ret = adb_controller->send_request(req, 0);
  		else
  			ret = -ENXIO;
  		up(&adb_probe_mutex);
  	}
  
  	if (ret != 0) {
  		atomic_dec(&state->n_pending);
  		goto out;
  	}
  	return count;
  
  out:
  	kfree(req);
  	return ret;
  }
fa027c2a0   Arjan van de Ven   [PATCH] mark stru...
809
  static const struct file_operations adb_fops = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
810
811
812
813
814
815
816
  	.owner		= THIS_MODULE,
  	.llseek		= no_llseek,
  	.read		= adb_read,
  	.write		= adb_write,
  	.open		= adb_open,
  	.release	= adb_release,
  };
c9f6d3d5c   Johannes Berg   [POWERPC] adb: Re...
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
  static struct platform_driver adb_pfdrv = {
  	.driver = {
  		.name = "adb",
  	},
  #ifdef CONFIG_PM
  	.suspend = adb_suspend,
  	.resume = adb_resume,
  #endif
  };
  
  static struct platform_device adb_pfdev = {
  	.name = "adb",
  };
  
  static int __init
  adb_dummy_probe(struct platform_device *dev)
  {
  	if (dev == &adb_pfdev)
  		return 0;
  	return -ENODEV;
  }
  
  static void __init
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
840
841
842
843
844
845
846
  adbdev_init(void)
  {
  	if (register_chrdev(ADB_MAJOR, "adb", &adb_fops)) {
  		printk(KERN_ERR "adb: unable to get major %d
  ", ADB_MAJOR);
  		return;
  	}
56b229359   Greg Kroah-Hartman   [PATCH] class: co...
847
848
  	adb_dev_class = class_create(THIS_MODULE, "adb");
  	if (IS_ERR(adb_dev_class))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
849
  		return;
a9b12619f   Greg Kroah-Hartman   device create: mi...
850
  	device_create(adb_dev_class, NULL, MKDEV(ADB_MAJOR, 0), NULL, "adb");
c9f6d3d5c   Johannes Berg   [POWERPC] adb: Re...
851
852
853
  
  	platform_device_register(&adb_pfdev);
  	platform_driver_probe(&adb_pfdrv, adb_dummy_probe);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
854
  }