Import 2.1.118
[davej-history.git] / drivers / macintosh / adb.c
blob4c9a67cc0f1dfbf106dfb55cea9192b3a8d774a8
1 /*
2 * Device driver for the Apple Desktop Bus
3 * and the /dev/adb device on macintoshes.
5 * Copyright (C) 1996 Paul Mackerras.
6 */
7 #include <linux/types.h>
8 #include <linux/errno.h>
9 #include <linux/kernel.h>
10 #include <linux/malloc.h>
11 #include <linux/fs.h>
12 #include <linux/mm.h>
13 #include <linux/sched.h>
14 #include <asm/prom.h>
15 #include <asm/adb.h>
16 #include <asm/cuda.h>
17 #include <asm/pmu.h>
18 #include <asm/uaccess.h>
19 #include <asm/hydra.h>
20 #include <asm/init.h>
22 enum adb_hw adb_hardware = ADB_NONE;
23 int (*adb_send_request)(struct adb_request *req, int sync);
24 int (*adb_autopoll)(int on);
25 static void adb_scan_bus(void);
27 static struct adb_handler {
28 void (*handler)(unsigned char *, int, struct pt_regs *, int);
29 int original_address;
30 int handler_id;
31 } adb_handler[16];
33 __openfirmware
35 static int adb_nodev(void)
37 return -1;
40 #if 0
41 static void printADBreply(struct adb_request *req)
43 int i;
45 printk("adb reply (%d)", req->reply_len);
46 for(i = 0; i < req->reply_len; i++)
47 printk(" %x", req->reply[i]);
48 printk("\n");
51 #endif
53 static void adb_scan_bus(void)
55 int i, highFree=0, noMovement;
56 struct adb_request req;
58 /* reset ADB bus */
59 /*adb_request(&req, NULL, ADBREQ_SYNC, 1, 0);*/
61 /* assumes adb_handler[] is all zeroes at this point */
62 for (i = 1; i < 16; i++) {
63 /* see if there is anything at address i */
64 adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
65 (i << 4) | 0xf);
66 if (req.reply_len > 1)
67 /* one or more devices at this address */
68 adb_handler[i].original_address = i;
69 else if (i > highFree)
70 highFree = i;
73 /* Note we reset noMovement to 0 each time we move a device */
74 for (noMovement = 1; noMovement < 2 && highFree > 0; noMovement++) {
75 for (i = 1; i < 16; i++) {
76 if (adb_handler[i].original_address == 0)
77 continue;
79 * Send a "talk register 3" command to address i
80 * to provoke a collision if there is more than
81 * one device at this address.
83 adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
84 (i << 4) | 0xf);
86 * Move the device(s) which didn't detect a
87 * collision to address `highFree'. Hopefully
88 * this only moves one device.
90 adb_request(&req, NULL, ADBREQ_SYNC, 3,
91 (i<< 4) | 0xb, (highFree | 0x60), 0xfe);
93 * Test whether there are any device(s) left
94 * at address i.
96 adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
97 (i << 4) | 0xf);
98 if (req.reply_len > 1) {
100 * There are still one or more devices
101 * left at address i. Register the one(s)
102 * we moved to `highFree', and find a new
103 * value for highFree.
105 adb_handler[highFree].original_address =
106 adb_handler[i].original_address;
107 while (highFree > 0 &&
108 adb_handler[highFree].original_address)
109 highFree--;
110 if (highFree <= 0)
111 break;
113 noMovement = 0;
115 else {
117 * No devices left at address i; move the
118 * one(s) we moved to `highFree' back to i.
120 adb_request(&req, NULL, ADBREQ_SYNC, 3,
121 (highFree << 4) | 0xb,
122 (i | 0x60), 0xfe);
127 /* Now fill in the handler_id field of the adb_handler entries. */
128 printk(KERN_DEBUG "adb devices:");
129 for (i = 1; i < 16; i++) {
130 if (adb_handler[i].original_address == 0)
131 continue;
132 adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
133 (i << 4) | 0xf);
134 adb_handler[i].handler_id = req.reply[2];
135 printk(" [%d]: %d %x", i, adb_handler[i].original_address,
136 adb_handler[i].handler_id);
138 printk("\n");
141 void adb_init(void)
143 adb_send_request = (void *) adb_nodev;
144 adb_autopoll = (void *) adb_nodev;
145 if ( (_machine != _MACH_chrp) && (_machine != _MACH_Pmac) )
146 return;
147 via_cuda_init();
148 via_pmu_init();
149 macio_adb_init();
150 if (adb_hardware == ADB_NONE)
151 printk(KERN_WARNING "Warning: no ADB interface detected\n");
152 else {
153 adb_scan_bus();
154 adb_autopoll(1);
159 adb_request(struct adb_request *req, void (*done)(struct adb_request *),
160 int flags, int nbytes, ...)
162 va_list list;
163 int i;
164 struct adb_request sreq;
166 if (req == NULL) {
167 req = &sreq;
168 flags |= ADBREQ_SYNC;
170 req->nbytes = nbytes;
171 req->done = done;
172 req->reply_expected = flags & ADBREQ_REPLY;
173 va_start(list, nbytes);
174 for (i = 0; i < nbytes; ++i)
175 req->data[i] = va_arg(list, int);
176 va_end(list);
177 return adb_send_request(req, flags & ADBREQ_SYNC);
180 /* Ultimately this should return the number of devices with
181 the given default id. */
183 adb_register(int default_id, int handler_id, struct adb_ids *ids,
184 void (*handler)(unsigned char *, int, struct pt_regs *, int))
186 int i;
188 ids->nids = 0;
189 for (i = 1; i < 16; i++) {
190 if ((adb_handler[i].original_address == default_id) ||
191 (adb_handler[i].handler_id == handler_id)) {
192 if (adb_handler[i].handler != 0) {
193 printk(KERN_ERR
194 "Two handlers for ADB device %d\n",
195 default_id);
196 return 0;
198 adb_handler[i].handler = handler;
199 ids->id[ids->nids++] = i;
202 return 1;
205 void
206 adb_input(unsigned char *buf, int nb, struct pt_regs *regs, int autopoll)
208 int i, id;
209 static int dump_adb_input = 0;
211 id = buf[0] >> 4;
212 if (dump_adb_input) {
213 printk(KERN_INFO "adb packet: ");
214 for (i = 0; i < nb; ++i)
215 printk(" %x", buf[i]);
216 printk(", id = %d\n", id);
218 if (adb_handler[id].handler != 0) {
219 (*adb_handler[id].handler)(buf, nb, regs, autopoll);
224 * /dev/adb device driver.
227 #define ADB_MAJOR 56 /* major number for /dev/adb */
229 extern void adbdev_init(void);
231 struct adbdev_state {
232 spinlock_t lock;
233 atomic_t n_pending;
234 struct adb_request *completed;
235 struct wait_queue *wait_queue;
236 int inuse;
239 static void adb_write_done(struct adb_request *req)
241 struct adbdev_state *state = (struct adbdev_state *) req->arg;
242 unsigned long flags;
244 if (!req->complete) {
245 req->reply_len = 0;
246 req->complete = 1;
248 spin_lock_irqsave(&state->lock, flags);
249 atomic_dec(&state->n_pending);
250 if (!state->inuse) {
251 kfree(req);
252 if (atomic_read(&state->n_pending) == 0) {
253 spin_unlock_irqrestore(&state->lock, flags);
254 kfree(state);
255 return;
257 } else {
258 struct adb_request **ap = &state->completed;
259 while (*ap != NULL)
260 ap = &(*ap)->next;
261 req->next = NULL;
262 *ap = req;
263 wake_up_interruptible(&state->wait_queue);
265 spin_unlock_irqrestore(&state->lock, flags);
268 static int adb_open(struct inode *inode, struct file *file)
270 struct adbdev_state *state;
272 if (MINOR(inode->i_rdev) > 0 || adb_hardware == ADB_NONE)
273 return -ENXIO;
274 state = kmalloc(sizeof(struct adbdev_state), GFP_KERNEL);
275 if (state == 0)
276 return -ENOMEM;
277 file->private_data = state;
278 spin_lock_init(&state->lock);
279 atomic_set(&state->n_pending, 0);
280 state->completed = NULL;
281 state->wait_queue = NULL;
282 state->inuse = 1;
284 return 0;
287 static int adb_release(struct inode *inode, struct file *file)
289 struct adbdev_state *state = file->private_data;
290 unsigned long flags;
292 if (state) {
293 file->private_data = NULL;
294 spin_lock_irqsave(&state->lock, flags);
295 if (atomic_read(&state->n_pending) == 0
296 && state->completed == NULL) {
297 spin_unlock_irqrestore(&state->lock, flags);
298 kfree(state);
299 } else {
300 state->inuse = 0;
301 spin_unlock_irqrestore(&state->lock, flags);
304 return 0;
307 static long long adb_lseek(struct file *file, loff_t offset, int origin)
309 return -ESPIPE;
312 static ssize_t adb_read(struct file *file, char *buf,
313 size_t count, loff_t *ppos)
315 int ret;
316 struct adbdev_state *state = file->private_data;
317 struct adb_request *req;
318 struct wait_queue wait = { current, NULL };
319 unsigned long flags;
321 if (count < 2)
322 return -EINVAL;
323 if (count > sizeof(req->reply))
324 count = sizeof(req->reply);
325 ret = verify_area(VERIFY_WRITE, buf, count);
326 if (ret)
327 return ret;
329 req = NULL;
330 add_wait_queue(&state->wait_queue, &wait);
331 current->state = TASK_INTERRUPTIBLE;
333 for (;;) {
334 spin_lock_irqsave(&state->lock, flags);
335 req = state->completed;
336 if (req != NULL)
337 state->completed = req->next;
338 else if (atomic_read(&state->n_pending) == 0)
339 ret = -EIO;
340 spin_unlock_irqrestore(&state->lock, flags);
341 if (req != NULL || ret != 0)
342 break;
344 if (file->f_flags & O_NONBLOCK) {
345 ret = -EAGAIN;
346 break;
348 if (signal_pending(current)) {
349 ret = -ERESTARTSYS;
350 break;
352 schedule();
355 current->state = TASK_RUNNING;
356 remove_wait_queue(&state->wait_queue, &wait);
358 if (ret)
359 return ret;
361 ret = req->reply_len;
362 if (ret > count)
363 ret = count;
364 if (ret > 0 && copy_to_user(buf, req->reply, ret))
365 ret = -EFAULT;
367 kfree(req);
368 return ret;
371 static ssize_t adb_write(struct file *file, const char *buf,
372 size_t count, loff_t *ppos)
374 int ret, i;
375 struct adbdev_state *state = file->private_data;
376 struct adb_request *req;
378 if (count < 2 || count > sizeof(req->data))
379 return -EINVAL;
380 ret = verify_area(VERIFY_READ, buf, count);
381 if (ret)
382 return ret;
384 req = (struct adb_request *) kmalloc(sizeof(struct adb_request),
385 GFP_KERNEL);
386 if (req == NULL)
387 return -ENOMEM;
389 req->nbytes = count;
390 req->done = adb_write_done;
391 req->arg = (void *) state;
392 req->complete = 0;
394 ret = -EFAULT;
395 if (copy_from_user(req->data, buf, count))
396 goto out;
398 atomic_inc(&state->n_pending);
399 switch (adb_hardware) {
400 case ADB_NONE:
401 ret = -ENXIO;
402 break;
403 case ADB_VIACUDA:
404 req->reply_expected = 1;
405 ret = cuda_send_request(req);
406 break;
407 case ADB_VIAPMU:
408 if (req->data[0] != ADB_PACKET) {
409 ret = pmu_send_request(req);
410 break;
412 /* else fall through */
413 default:
414 ret = -EINVAL;
415 if (req->data[0] != ADB_PACKET)
416 break;
417 for (i = 0; i < req->nbytes-1; ++i)
418 req->data[i] = req->data[i+1];
419 req->nbytes--;
420 req->reply_expected = ((req->data[0] & 0xc) == 0xc);
421 ret = adb_send_request(req, 0);
422 break;
425 if (ret != 0) {
426 atomic_dec(&state->n_pending);
427 goto out;
429 return count;
431 out:
432 kfree(req);
433 return ret;
436 static struct file_operations adb_fops = {
437 adb_lseek,
438 adb_read,
439 adb_write,
440 NULL, /* no readdir */
441 NULL, /* no poll yet */
442 NULL, /* no ioctl yet */
443 NULL, /* no mmap */
444 adb_open,
445 NULL, /* flush */
446 adb_release
449 void adbdev_init()
451 if ( (_machine != _MACH_chrp) && (_machine != _MACH_Pmac) )
452 return;
453 if (register_chrdev(ADB_MAJOR, "adb", &adb_fops))
454 printk(KERN_ERR "adb: unable to get major %d\n", ADB_MAJOR);