blob: dd51df5a6ec06f35a314883692135ed73e79c1ac [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Device driver for the via-cuda on Apple Powermacs.
3 *
4 * The VIA (versatile interface adapter) interfaces to the CUDA,
5 * a 6805 microprocessor core which controls the ADB (Apple Desktop
6 * Bus) which connects to the keyboard and mouse. The CUDA also
7 * controls system power and the RTC (real time clock) chip.
8 *
9 * Copyright (C) 1996 Paul Mackerras.
10 */
11#include <stdarg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/types.h>
13#include <linux/errno.h>
14#include <linux/kernel.h>
15#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/adb.h>
17#include <linux/cuda.h>
18#include <linux/spinlock.h>
19#include <linux/interrupt.h>
20#ifdef CONFIG_PPC
21#include <asm/prom.h>
22#include <asm/machdep.h>
23#else
24#include <asm/macintosh.h>
25#include <asm/macints.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <asm/mac_via.h>
27#endif
28#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/init.h>
30
31static volatile unsigned char __iomem *via;
32static DEFINE_SPINLOCK(cuda_lock);
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034/* VIA registers - spaced 0x200 bytes apart */
35#define RS 0x200 /* skip between registers */
36#define B 0 /* B-side data */
37#define A RS /* A-side data */
38#define DIRB (2*RS) /* B-side direction (1=output) */
39#define DIRA (3*RS) /* A-side direction (1=output) */
40#define T1CL (4*RS) /* Timer 1 ctr/latch (low 8 bits) */
41#define T1CH (5*RS) /* Timer 1 counter (high 8 bits) */
42#define T1LL (6*RS) /* Timer 1 latch (low 8 bits) */
43#define T1LH (7*RS) /* Timer 1 latch (high 8 bits) */
44#define T2CL (8*RS) /* Timer 2 ctr/latch (low 8 bits) */
45#define T2CH (9*RS) /* Timer 2 counter (high 8 bits) */
46#define SR (10*RS) /* Shift register */
47#define ACR (11*RS) /* Auxiliary control register */
48#define PCR (12*RS) /* Peripheral control register */
49#define IFR (13*RS) /* Interrupt flag register */
50#define IER (14*RS) /* Interrupt enable register */
51#define ANH (15*RS) /* A-side data, no handshake */
52
53/* Bits in B data register: all active low */
54#define TREQ 0x08 /* Transfer request (input) */
55#define TACK 0x10 /* Transfer acknowledge (output) */
56#define TIP 0x20 /* Transfer in progress (output) */
57
58/* Bits in ACR */
59#define SR_CTRL 0x1c /* Shift register control bits */
60#define SR_EXT 0x0c /* Shift on external clock */
61#define SR_OUT 0x10 /* Shift out if 1 */
62
63/* Bits in IFR and IER */
64#define IER_SET 0x80 /* set bits in IER */
65#define IER_CLR 0 /* clear bits in IER */
66#define SR_INT 0x04 /* Shift register full/empty */
67
68static enum cuda_state {
69 idle,
70 sent_first_byte,
71 sending,
72 reading,
73 read_done,
74 awaiting_reply
75} cuda_state;
76
77static struct adb_request *current_req;
78static struct adb_request *last_req;
79static unsigned char cuda_rbuf[16];
80static unsigned char *reply_ptr;
81static int reading_reply;
82static int data_index;
Finn Thain0251c382007-05-01 22:33:00 +020083static int cuda_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#ifdef CONFIG_PPC
85static struct device_node *vias;
86#endif
Olaf Hering87275852007-02-10 21:35:12 +010087static int cuda_fully_inited;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89#ifdef CONFIG_ADB
90static int cuda_probe(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091static int cuda_send_request(struct adb_request *req, int sync);
92static int cuda_adb_autopoll(int devs);
93static int cuda_reset_adb_bus(void);
94#endif /* CONFIG_ADB */
95
96static int cuda_init_via(void);
97static void cuda_start(void);
David Howells7d12e782006-10-05 14:55:46 +010098static irqreturn_t cuda_interrupt(int irq, void *arg);
99static void cuda_input(unsigned char *buf, int nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100void cuda_poll(void);
101static int cuda_write(struct adb_request *req);
102
103int cuda_request(struct adb_request *req,
104 void (*done)(struct adb_request *), int nbytes, ...);
105
106#ifdef CONFIG_ADB
107struct adb_driver via_cuda_driver = {
Finn Thain18814ee2009-11-17 20:03:05 +1100108 .name = "CUDA",
109 .probe = cuda_probe,
110 .send_request = cuda_send_request,
111 .autopoll = cuda_adb_autopoll,
112 .poll = cuda_poll,
113 .reset_bus = cuda_reset_adb_bus,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114};
115#endif /* CONFIG_ADB */
116
Finn Thain18814ee2009-11-17 20:03:05 +1100117#ifdef CONFIG_MAC
118int __init find_via_cuda(void)
119{
120 struct adb_request req;
121 int err;
122
123 if (macintosh_config->adb_type != MAC_ADB_CUDA)
124 return 0;
125
126 via = via1;
127 cuda_state = idle;
128
129 err = cuda_init_via();
130 if (err) {
131 printk(KERN_ERR "cuda_init_via() failed\n");
132 via = NULL;
133 return 0;
134 }
135
136 /* enable autopoll */
137 cuda_request(&req, NULL, 3, CUDA_PACKET, CUDA_AUTOPOLL, 1);
138 while (!req.complete)
139 cuda_poll();
140
141 return 1;
142}
143#else
Benjamin Herrenschmidt51d30822005-11-23 17:57:25 +1100144int __init find_via_cuda(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 struct adb_request req;
Benjamin Herrenschmidt51d30822005-11-23 17:57:25 +1100147 phys_addr_t taddr;
Jeremy Kerr018a3d12006-07-12 15:40:29 +1000148 const u32 *reg;
Benjamin Herrenschmidt51d30822005-11-23 17:57:25 +1100149 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151 if (vias != 0)
152 return 1;
Benjamin Herrenschmidt51d30822005-11-23 17:57:25 +1100153 vias = of_find_node_by_name(NULL, "via-cuda");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 if (vias == 0)
155 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Stephen Rothwell01b27262007-04-27 13:41:15 +1000157 reg = of_get_property(vias, "reg", NULL);
Benjamin Herrenschmidt51d30822005-11-23 17:57:25 +1100158 if (reg == NULL) {
159 printk(KERN_ERR "via-cuda: No \"reg\" property !\n");
160 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 }
Benjamin Herrenschmidt51d30822005-11-23 17:57:25 +1100162 taddr = of_translate_address(vias, reg);
163 if (taddr == 0) {
164 printk(KERN_ERR "via-cuda: Can't translate address !\n");
165 goto fail;
166 }
167 via = ioremap(taddr, 0x2000);
168 if (via == NULL) {
169 printk(KERN_ERR "via-cuda: Can't map address !\n");
170 goto fail;
171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173 cuda_state = idle;
174 sys_ctrler = SYS_CTRLER_CUDA;
175
176 err = cuda_init_via();
177 if (err) {
178 printk(KERN_ERR "cuda_init_via() failed\n");
179 via = NULL;
180 return 0;
181 }
182
183 /* Clear and enable interrupts, but only on PPC. On 68K it's done */
184 /* for us by the main VIA driver in arch/m68k/mac/via.c */
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 out_8(&via[IFR], 0x7f); /* clear interrupts by writing 1s */
187 out_8(&via[IER], IER_SET|SR_INT); /* enable interrupt from SR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189 /* enable autopoll */
190 cuda_request(&req, NULL, 3, CUDA_PACKET, CUDA_AUTOPOLL, 1);
191 while (!req.complete)
192 cuda_poll();
193
194 return 1;
Benjamin Herrenschmidt51d30822005-11-23 17:57:25 +1100195
196 fail:
197 of_node_put(vias);
198 vias = NULL;
199 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200}
Finn Thain18814ee2009-11-17 20:03:05 +1100201#endif /* !defined CONFIG_MAC */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203static int __init via_cuda_start(void)
204{
205 if (via == NULL)
206 return -ENODEV;
207
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000208#ifdef CONFIG_MAC
Finn Thain0251c382007-05-01 22:33:00 +0200209 cuda_irq = IRQ_MAC_ADB;
Finn Thain18814ee2009-11-17 20:03:05 +1100210#else
Finn Thain0251c382007-05-01 22:33:00 +0200211 cuda_irq = irq_of_parse_and_map(vias, 0);
Michael Ellermanef24ba72016-09-06 21:53:24 +1000212 if (!cuda_irq) {
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000213 printk(KERN_ERR "via-cuda: can't map interrupts for %s\n",
214 vias->full_name);
215 return -ENODEV;
216 }
Finn Thain18814ee2009-11-17 20:03:05 +1100217#endif
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000218
Finn Thain0251c382007-05-01 22:33:00 +0200219 if (request_irq(cuda_irq, cuda_interrupt, 0, "ADB", cuda_interrupt)) {
220 printk(KERN_ERR "via-cuda: can't request irq %d\n", cuda_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 return -EAGAIN;
222 }
223
Finn Thain523717d2016-12-31 19:56:26 -0500224 pr_info("Macintosh CUDA driver v0.5 for Unified ADB.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226 cuda_fully_inited = 1;
227 return 0;
228}
229
230device_initcall(via_cuda_start);
231
232#ifdef CONFIG_ADB
233static int
234cuda_probe(void)
235{
236#ifdef CONFIG_PPC
237 if (sys_ctrler != SYS_CTRLER_CUDA)
238 return -ENODEV;
239#else
240 if (macintosh_config->adb_type != MAC_ADB_CUDA)
241 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 if (via == NULL)
244 return -ENODEV;
245 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246}
247#endif /* CONFIG_ADB */
248
249#define WAIT_FOR(cond, what) \
250 do { \
251 int x; \
252 for (x = 1000; !(cond); --x) { \
253 if (x == 0) { \
Finn Thain523717d2016-12-31 19:56:26 -0500254 pr_err("Timeout waiting for " what "\n"); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 return -ENXIO; \
256 } \
257 udelay(100); \
258 } \
259 } while (0)
260
261static int
Geert Uytterhoeven330dae12013-06-30 12:03:23 +0200262__init cuda_init_via(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
264 out_8(&via[DIRB], (in_8(&via[DIRB]) | TACK | TIP) & ~TREQ); /* TACK & TIP out */
265 out_8(&via[B], in_8(&via[B]) | TACK | TIP); /* negate them */
266 out_8(&via[ACR] ,(in_8(&via[ACR]) & ~SR_CTRL) | SR_EXT); /* SR data in */
267 (void)in_8(&via[SR]); /* clear any left-over data */
Finn Thain0251c382007-05-01 22:33:00 +0200268#ifdef CONFIG_PPC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 out_8(&via[IER], 0x7f); /* disable interrupts from VIA */
270 (void)in_8(&via[IER]);
Finn Thain0251c382007-05-01 22:33:00 +0200271#else
272 out_8(&via[IER], SR_INT); /* disable SR interrupt from VIA */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273#endif
274
275 /* delay 4ms and then clear any pending interrupt */
276 mdelay(4);
277 (void)in_8(&via[SR]);
Finn Thain0251c382007-05-01 22:33:00 +0200278 out_8(&via[IFR], SR_INT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280 /* sync with the CUDA - assert TACK without TIP */
281 out_8(&via[B], in_8(&via[B]) & ~TACK);
282
283 /* wait for the CUDA to assert TREQ in response */
284 WAIT_FOR((in_8(&via[B]) & TREQ) == 0, "CUDA response to sync");
285
286 /* wait for the interrupt and then clear it */
287 WAIT_FOR(in_8(&via[IFR]) & SR_INT, "CUDA response to sync (2)");
288 (void)in_8(&via[SR]);
Finn Thain0251c382007-05-01 22:33:00 +0200289 out_8(&via[IFR], SR_INT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291 /* finish the sync by negating TACK */
292 out_8(&via[B], in_8(&via[B]) | TACK);
293
294 /* wait for the CUDA to negate TREQ and the corresponding interrupt */
295 WAIT_FOR(in_8(&via[B]) & TREQ, "CUDA response to sync (3)");
296 WAIT_FOR(in_8(&via[IFR]) & SR_INT, "CUDA response to sync (4)");
297 (void)in_8(&via[SR]);
Finn Thain0251c382007-05-01 22:33:00 +0200298 out_8(&via[IFR], SR_INT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 out_8(&via[B], in_8(&via[B]) | TIP); /* should be unnecessary */
300
301 return 0;
302}
303
304#ifdef CONFIG_ADB
305/* Send an ADB command */
306static int
307cuda_send_request(struct adb_request *req, int sync)
308{
309 int i;
310
311 if ((via == NULL) || !cuda_fully_inited) {
312 req->complete = 1;
313 return -ENXIO;
314 }
315
316 req->reply_expected = 1;
317
318 i = cuda_write(req);
319 if (i)
320 return i;
321
322 if (sync) {
323 while (!req->complete)
324 cuda_poll();
325 }
326 return 0;
327}
328
329
330/* Enable/disable autopolling */
331static int
332cuda_adb_autopoll(int devs)
333{
334 struct adb_request req;
335
336 if ((via == NULL) || !cuda_fully_inited)
337 return -ENXIO;
338
339 cuda_request(&req, NULL, 3, CUDA_PACKET, CUDA_AUTOPOLL, (devs? 1: 0));
340 while (!req.complete)
341 cuda_poll();
342 return 0;
343}
344
345/* Reset adb bus - how do we do this?? */
346static int
347cuda_reset_adb_bus(void)
348{
349 struct adb_request req;
350
351 if ((via == NULL) || !cuda_fully_inited)
352 return -ENXIO;
353
354 cuda_request(&req, NULL, 2, ADB_PACKET, 0); /* maybe? */
355 while (!req.complete)
356 cuda_poll();
357 return 0;
358}
359#endif /* CONFIG_ADB */
Finn Thain523717d2016-12-31 19:56:26 -0500360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361/* Construct and send a cuda request */
362int
363cuda_request(struct adb_request *req, void (*done)(struct adb_request *),
364 int nbytes, ...)
365{
366 va_list list;
367 int i;
368
369 if (via == NULL) {
370 req->complete = 1;
371 return -ENXIO;
372 }
373
374 req->nbytes = nbytes;
375 req->done = done;
376 va_start(list, nbytes);
377 for (i = 0; i < nbytes; ++i)
378 req->data[i] = va_arg(list, int);
379 va_end(list);
380 req->reply_expected = 1;
381 return cuda_write(req);
382}
Anton Blanchard4a1b08e2014-08-20 08:00:01 +1000383EXPORT_SYMBOL(cuda_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
385static int
386cuda_write(struct adb_request *req)
387{
388 unsigned long flags;
389
390 if (req->nbytes < 2 || req->data[0] > CUDA_PACKET) {
391 req->complete = 1;
392 return -EINVAL;
393 }
394 req->next = NULL;
395 req->sent = 0;
396 req->complete = 0;
397 req->reply_len = 0;
398
399 spin_lock_irqsave(&cuda_lock, flags);
400 if (current_req != 0) {
401 last_req->next = req;
402 last_req = req;
403 } else {
404 current_req = req;
405 last_req = req;
406 if (cuda_state == idle)
407 cuda_start();
408 }
409 spin_unlock_irqrestore(&cuda_lock, flags);
410
411 return 0;
412}
413
414static void
415cuda_start(void)
416{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 /* assert cuda_state == idle */
Finn Thain06d7e992016-12-31 19:56:26 -0500418 if (current_req == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 return;
420 if ((in_8(&via[B]) & TREQ) == 0)
421 return; /* a byte is coming in from the CUDA */
422
423 /* set the shift register to shift out and send a byte */
424 out_8(&via[ACR], in_8(&via[ACR]) | SR_OUT);
Finn Thain06d7e992016-12-31 19:56:26 -0500425 out_8(&via[SR], current_req->data[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 out_8(&via[B], in_8(&via[B]) & ~TIP);
427 cuda_state = sent_first_byte;
428}
429
430void
431cuda_poll(void)
432{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 /* cuda_interrupt only takes a normal lock, we disable
434 * interrupts here to avoid re-entering and thus deadlocking.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 */
Finn Thain18814ee2009-11-17 20:03:05 +1100436 if (cuda_irq)
437 disable_irq(cuda_irq);
Olof Johansson49f19ce2006-10-05 20:31:10 -0500438 cuda_interrupt(0, NULL);
Finn Thain18814ee2009-11-17 20:03:05 +1100439 if (cuda_irq)
440 enable_irq(cuda_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441}
Anton Blanchard4a1b08e2014-08-20 08:00:01 +1000442EXPORT_SYMBOL(cuda_poll);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +0100445cuda_interrupt(int irq, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
447 int status;
448 struct adb_request *req = NULL;
449 unsigned char ibuf[16];
450 int ibuf_len = 0;
451 int complete = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453 spin_lock(&cuda_lock);
454
Finn Thain18814ee2009-11-17 20:03:05 +1100455 /* On powermacs, this handler is registered for the VIA IRQ. But they use
Finn Thain0251c382007-05-01 22:33:00 +0200456 * just the shift register IRQ -- other VIA interrupt sources are disabled.
457 * On m68k macs, the VIA IRQ sources are dispatched individually. Unless
458 * we are polling, the shift register IRQ flag has already been cleared.
459 */
460
461#ifdef CONFIG_MAC
462 if (!arg)
463#endif
464 {
465 if ((in_8(&via[IFR]) & SR_INT) == 0) {
466 spin_unlock(&cuda_lock);
467 return IRQ_NONE;
468 } else {
469 out_8(&via[IFR], SR_INT);
470 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 }
472
473 status = (~in_8(&via[B]) & (TIP|TREQ)) | (in_8(&via[ACR]) & SR_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 switch (cuda_state) {
475 case idle:
476 /* CUDA has sent us the first byte of data - unsolicited */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 (void)in_8(&via[SR]);
478 out_8(&via[B], in_8(&via[B]) & ~TIP);
479 cuda_state = reading;
480 reply_ptr = cuda_rbuf;
481 reading_reply = 0;
482 break;
483
484 case awaiting_reply:
485 /* CUDA has sent us the first byte of data of a reply */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 (void)in_8(&via[SR]);
487 out_8(&via[B], in_8(&via[B]) & ~TIP);
488 cuda_state = reading;
489 reply_ptr = current_req->reply;
490 reading_reply = 1;
491 break;
492
493 case sent_first_byte:
494 if (status == TREQ + TIP + SR_OUT) {
495 /* collision */
496 out_8(&via[ACR], in_8(&via[ACR]) & ~SR_OUT);
497 (void)in_8(&via[SR]);
498 out_8(&via[B], in_8(&via[B]) | TIP | TACK);
499 cuda_state = idle;
500 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 out_8(&via[SR], current_req->data[1]);
502 out_8(&via[B], in_8(&via[B]) ^ TACK);
503 data_index = 2;
504 cuda_state = sending;
505 }
506 break;
507
508 case sending:
509 req = current_req;
510 if (data_index >= req->nbytes) {
511 out_8(&via[ACR], in_8(&via[ACR]) & ~SR_OUT);
512 (void)in_8(&via[SR]);
513 out_8(&via[B], in_8(&via[B]) | TACK | TIP);
514 req->sent = 1;
515 if (req->reply_expected) {
516 cuda_state = awaiting_reply;
517 } else {
518 current_req = req->next;
519 complete = 1;
520 /* not sure about this */
521 cuda_state = idle;
522 cuda_start();
523 }
524 } else {
525 out_8(&via[SR], req->data[data_index++]);
526 out_8(&via[B], in_8(&via[B]) ^ TACK);
527 }
528 break;
529
530 case reading:
531 *reply_ptr++ = in_8(&via[SR]);
532 if (status == TIP) {
533 /* that's all folks */
534 out_8(&via[B], in_8(&via[B]) | TACK | TIP);
535 cuda_state = read_done;
536 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 out_8(&via[B], in_8(&via[B]) ^ TACK);
538 }
539 break;
540
541 case read_done:
542 (void)in_8(&via[SR]);
543 if (reading_reply) {
544 req = current_req;
545 req->reply_len = reply_ptr - req->reply;
546 if (req->data[0] == ADB_PACKET) {
547 /* Have to adjust the reply from ADB commands */
548 if (req->reply_len <= 2 || (req->reply[1] & 2) != 0) {
549 /* the 0x2 bit indicates no response */
550 req->reply_len = 0;
551 } else {
552 /* leave just the command and result bytes in the reply */
553 req->reply_len -= 2;
554 memmove(req->reply, req->reply + 2, req->reply_len);
555 }
556 }
557 current_req = req->next;
558 complete = 1;
559 } else {
560 /* This is tricky. We must break the spinlock to call
561 * cuda_input. However, doing so means we might get
562 * re-entered from another CPU getting an interrupt
563 * or calling cuda_poll(). I ended up using the stack
564 * (it's only for 16 bytes) and moving the actual
565 * call to cuda_input to outside of the lock.
566 */
567 ibuf_len = reply_ptr - cuda_rbuf;
568 memcpy(ibuf, cuda_rbuf, ibuf_len);
569 }
570 if (status == TREQ) {
571 out_8(&via[B], in_8(&via[B]) & ~TIP);
572 cuda_state = reading;
573 reply_ptr = cuda_rbuf;
574 reading_reply = 0;
575 } else {
576 cuda_state = idle;
577 cuda_start();
578 }
579 break;
580
581 default:
Finn Thain523717d2016-12-31 19:56:26 -0500582 pr_err("cuda_interrupt: unknown cuda_state %d?\n", cuda_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 }
584 spin_unlock(&cuda_lock);
585 if (complete && req) {
586 void (*done)(struct adb_request *) = req->done;
587 mb();
588 req->complete = 1;
589 /* Here, we assume that if the request has a done member, the
590 * struct request will survive to setting req->complete to 1
591 */
592 if (done)
593 (*done)(req);
594 }
595 if (ibuf_len)
David Howells7d12e782006-10-05 14:55:46 +0100596 cuda_input(ibuf, ibuf_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 return IRQ_HANDLED;
598}
599
600static void
David Howells7d12e782006-10-05 14:55:46 +0100601cuda_input(unsigned char *buf, int nb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 switch (buf[0]) {
604 case ADB_PACKET:
605#ifdef CONFIG_XMON
606 if (nb == 5 && buf[2] == 0x2c) {
607 extern int xmon_wants_key, xmon_adb_keycode;
608 if (xmon_wants_key) {
609 xmon_adb_keycode = buf[3];
610 return;
611 }
612 }
613#endif /* CONFIG_XMON */
614#ifdef CONFIG_ADB
David Howells7d12e782006-10-05 14:55:46 +0100615 adb_input(buf+2, nb-2, buf[1] & 0x40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616#endif /* CONFIG_ADB */
617 break;
618
619 default:
Finn Thain523717d2016-12-31 19:56:26 -0500620 print_hex_dump(KERN_INFO, "cuda_input: ", DUMP_PREFIX_NONE, 32, 1,
621 buf, nb, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 }
623}