Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Device driver for the Cuda and Egret system controllers found on PowerMacs
0004  * and 68k Macs.
0005  *
0006  * The Cuda or Egret is a 6805 microcontroller interfaced to the 6522 VIA.
0007  * This MCU controls system power, Parameter RAM, Real Time Clock and the
0008  * Apple Desktop Bus (ADB) that connects to the keyboard and mouse.
0009  *
0010  * Copyright (C) 1996 Paul Mackerras.
0011  */
0012 #include <linux/stdarg.h>
0013 #include <linux/types.h>
0014 #include <linux/errno.h>
0015 #include <linux/kernel.h>
0016 #include <linux/delay.h>
0017 #include <linux/adb.h>
0018 #include <linux/cuda.h>
0019 #include <linux/spinlock.h>
0020 #include <linux/interrupt.h>
0021 #include <linux/of_address.h>
0022 #include <linux/of_irq.h>
0023 
0024 #ifdef CONFIG_PPC
0025 #include <asm/machdep.h>
0026 #include <asm/pmac_feature.h>
0027 #else
0028 #include <asm/macintosh.h>
0029 #include <asm/macints.h>
0030 #include <asm/mac_via.h>
0031 #endif
0032 #include <asm/io.h>
0033 #include <linux/init.h>
0034 
0035 static volatile unsigned char __iomem *via;
0036 static DEFINE_SPINLOCK(cuda_lock);
0037 
0038 /* VIA registers - spaced 0x200 bytes apart */
0039 #define RS      0x200       /* skip between registers */
0040 #define B       0       /* B-side data */
0041 #define A       RS      /* A-side data */
0042 #define DIRB        (2*RS)      /* B-side direction (1=output) */
0043 #define DIRA        (3*RS)      /* A-side direction (1=output) */
0044 #define T1CL        (4*RS)      /* Timer 1 ctr/latch (low 8 bits) */
0045 #define T1CH        (5*RS)      /* Timer 1 counter (high 8 bits) */
0046 #define T1LL        (6*RS)      /* Timer 1 latch (low 8 bits) */
0047 #define T1LH        (7*RS)      /* Timer 1 latch (high 8 bits) */
0048 #define T2CL        (8*RS)      /* Timer 2 ctr/latch (low 8 bits) */
0049 #define T2CH        (9*RS)      /* Timer 2 counter (high 8 bits) */
0050 #define SR      (10*RS)     /* Shift register */
0051 #define ACR     (11*RS)     /* Auxiliary control register */
0052 #define PCR     (12*RS)     /* Peripheral control register */
0053 #define IFR     (13*RS)     /* Interrupt flag register */
0054 #define IER     (14*RS)     /* Interrupt enable register */
0055 #define ANH     (15*RS)     /* A-side data, no handshake */
0056 
0057 /*
0058  * When the Cuda design replaced the Egret, some signal names and
0059  * logic sense changed. They all serve the same purposes, however.
0060  *
0061  *   VIA pin       |  Egret pin
0062  * ----------------+------------------------------------------
0063  *   PB3 (input)   |  Transceiver session   (active low)
0064  *   PB4 (output)  |  VIA full              (active high)
0065  *   PB5 (output)  |  System session        (active high)
0066  *
0067  *   VIA pin       |  Cuda pin
0068  * ----------------+------------------------------------------
0069  *   PB3 (input)   |  Transfer request      (active low)
0070  *   PB4 (output)  |  Byte acknowledge      (active low)
0071  *   PB5 (output)  |  Transfer in progress  (active low)
0072  */
0073 
0074 /* Bits in Port B data register */
0075 #define TREQ        0x08        /* Transfer request */
0076 #define TACK        0x10        /* Transfer acknowledge */
0077 #define TIP     0x20        /* Transfer in progress */
0078 
0079 /* Bits in ACR */
0080 #define SR_CTRL     0x1c        /* Shift register control bits */
0081 #define SR_EXT      0x0c        /* Shift on external clock */
0082 #define SR_OUT      0x10        /* Shift out if 1 */
0083 
0084 /* Bits in IFR and IER */
0085 #define IER_SET     0x80        /* set bits in IER */
0086 #define IER_CLR     0       /* clear bits in IER */
0087 #define SR_INT      0x04        /* Shift register full/empty */
0088 
0089 /* Duration of byte acknowledgement pulse (us) */
0090 #define EGRET_TACK_ASSERTED_DELAY   300
0091 #define EGRET_TACK_NEGATED_DELAY    400
0092 
0093 /* Interval from interrupt to start of session (us) */
0094 #define EGRET_SESSION_DELAY     450
0095 
0096 #ifdef CONFIG_PPC
0097 #define mcu_is_egret    false
0098 #else
0099 static bool mcu_is_egret;
0100 #endif
0101 
0102 static inline bool TREQ_asserted(u8 portb)
0103 {
0104     return !(portb & TREQ);
0105 }
0106 
0107 static inline void assert_TIP(void)
0108 {
0109     if (mcu_is_egret) {
0110         udelay(EGRET_SESSION_DELAY);
0111         out_8(&via[B], in_8(&via[B]) | TIP);
0112     } else
0113         out_8(&via[B], in_8(&via[B]) & ~TIP);
0114 }
0115 
0116 static inline void assert_TIP_and_TACK(void)
0117 {
0118     if (mcu_is_egret) {
0119         udelay(EGRET_SESSION_DELAY);
0120         out_8(&via[B], in_8(&via[B]) | TIP | TACK);
0121     } else
0122         out_8(&via[B], in_8(&via[B]) & ~(TIP | TACK));
0123 }
0124 
0125 static inline void assert_TACK(void)
0126 {
0127     if (mcu_is_egret) {
0128         udelay(EGRET_TACK_NEGATED_DELAY);
0129         out_8(&via[B], in_8(&via[B]) | TACK);
0130     } else
0131         out_8(&via[B], in_8(&via[B]) & ~TACK);
0132 }
0133 
0134 static inline void toggle_TACK(void)
0135 {
0136     out_8(&via[B], in_8(&via[B]) ^ TACK);
0137 }
0138 
0139 static inline void negate_TACK(void)
0140 {
0141     if (mcu_is_egret) {
0142         udelay(EGRET_TACK_ASSERTED_DELAY);
0143         out_8(&via[B], in_8(&via[B]) & ~TACK);
0144     } else
0145         out_8(&via[B], in_8(&via[B]) | TACK);
0146 }
0147 
0148 static inline void negate_TIP_and_TACK(void)
0149 {
0150     if (mcu_is_egret) {
0151         udelay(EGRET_TACK_ASSERTED_DELAY);
0152         out_8(&via[B], in_8(&via[B]) & ~(TIP | TACK));
0153     } else
0154         out_8(&via[B], in_8(&via[B]) | TIP | TACK);
0155 }
0156 
0157 static enum cuda_state {
0158     idle,
0159     sent_first_byte,
0160     sending,
0161     reading,
0162     read_done,
0163     awaiting_reply
0164 } cuda_state;
0165 
0166 static struct adb_request *current_req;
0167 static struct adb_request *last_req;
0168 static unsigned char cuda_rbuf[16];
0169 static unsigned char *reply_ptr;
0170 static int reading_reply;
0171 static int data_index;
0172 static int cuda_irq;
0173 #ifdef CONFIG_PPC
0174 static struct device_node *vias;
0175 #endif
0176 static int cuda_fully_inited;
0177 
0178 #ifdef CONFIG_ADB
0179 static int cuda_probe(void);
0180 static int cuda_send_request(struct adb_request *req, int sync);
0181 static int cuda_adb_autopoll(int devs);
0182 static int cuda_reset_adb_bus(void);
0183 #endif /* CONFIG_ADB */
0184 
0185 static int cuda_init_via(void);
0186 static void cuda_start(void);
0187 static irqreturn_t cuda_interrupt(int irq, void *arg);
0188 static void cuda_input(unsigned char *buf, int nb);
0189 void cuda_poll(void);
0190 static int cuda_write(struct adb_request *req);
0191 
0192 int cuda_request(struct adb_request *req,
0193          void (*done)(struct adb_request *), int nbytes, ...);
0194 
0195 #ifdef CONFIG_ADB
0196 struct adb_driver via_cuda_driver = {
0197     .name         = "CUDA",
0198     .probe        = cuda_probe,
0199     .send_request = cuda_send_request,
0200     .autopoll     = cuda_adb_autopoll,
0201     .poll         = cuda_poll,
0202     .reset_bus    = cuda_reset_adb_bus,
0203 };
0204 #endif /* CONFIG_ADB */
0205 
0206 #ifdef CONFIG_MAC
0207 int __init find_via_cuda(void)
0208 {
0209     struct adb_request req;
0210     int err;
0211 
0212     if (macintosh_config->adb_type != MAC_ADB_CUDA &&
0213         macintosh_config->adb_type != MAC_ADB_EGRET)
0214     return 0;
0215 
0216     via = via1;
0217     cuda_state = idle;
0218     mcu_is_egret = macintosh_config->adb_type == MAC_ADB_EGRET;
0219 
0220     err = cuda_init_via();
0221     if (err) {
0222     printk(KERN_ERR "cuda_init_via() failed\n");
0223     via = NULL;
0224     return 0;
0225     }
0226 
0227     /* enable autopoll */
0228     cuda_request(&req, NULL, 3, CUDA_PACKET, CUDA_AUTOPOLL, 1);
0229     while (!req.complete)
0230     cuda_poll();
0231 
0232     return 1;
0233 }
0234 #else
0235 int __init find_via_cuda(void)
0236 {
0237     struct adb_request req;
0238     phys_addr_t taddr;
0239     const u32 *reg;
0240     int err;
0241 
0242     if (vias)
0243     return 1;
0244     vias = of_find_node_by_name(NULL, "via-cuda");
0245     if (!vias)
0246     return 0;
0247 
0248     reg = of_get_property(vias, "reg", NULL);
0249     if (reg == NULL) {
0250         printk(KERN_ERR "via-cuda: No \"reg\" property !\n");
0251         goto fail;
0252     }
0253     taddr = of_translate_address(vias, reg);
0254     if (taddr == 0) {
0255         printk(KERN_ERR "via-cuda: Can't translate address !\n");
0256         goto fail;
0257     }
0258     via = ioremap(taddr, 0x2000);
0259     if (via == NULL) {
0260         printk(KERN_ERR "via-cuda: Can't map address !\n");
0261         goto fail;
0262     }
0263 
0264     cuda_state = idle;
0265     sys_ctrler = SYS_CTRLER_CUDA;
0266 
0267     err = cuda_init_via();
0268     if (err) {
0269     printk(KERN_ERR "cuda_init_via() failed\n");
0270     via = NULL;
0271     return 0;
0272     }
0273 
0274     /* Clear and enable interrupts, but only on PPC. On 68K it's done  */
0275     /* for us by the main VIA driver in arch/m68k/mac/via.c        */
0276 
0277     out_8(&via[IFR], 0x7f); /* clear interrupts by writing 1s */
0278     out_8(&via[IER], IER_SET|SR_INT); /* enable interrupt from SR */
0279 
0280     /* enable autopoll */
0281     cuda_request(&req, NULL, 3, CUDA_PACKET, CUDA_AUTOPOLL, 1);
0282     while (!req.complete)
0283     cuda_poll();
0284 
0285     return 1;
0286 
0287  fail:
0288     of_node_put(vias);
0289     vias = NULL;
0290     return 0;
0291 }
0292 #endif /* !defined CONFIG_MAC */
0293 
0294 static int __init via_cuda_start(void)
0295 {
0296     if (via == NULL)
0297     return -ENODEV;
0298 
0299 #ifdef CONFIG_MAC
0300     cuda_irq = IRQ_MAC_ADB;
0301 #else
0302     cuda_irq = irq_of_parse_and_map(vias, 0);
0303     if (!cuda_irq) {
0304     printk(KERN_ERR "via-cuda: can't map interrupts for %pOF\n",
0305            vias);
0306     return -ENODEV;
0307     }
0308 #endif
0309 
0310     if (request_irq(cuda_irq, cuda_interrupt, 0, "ADB", cuda_interrupt)) {
0311     printk(KERN_ERR "via-cuda: can't request irq %d\n", cuda_irq);
0312     return -EAGAIN;
0313     }
0314 
0315     pr_info("Macintosh Cuda and Egret driver.\n");
0316 
0317     cuda_fully_inited = 1;
0318     return 0;
0319 }
0320 
0321 device_initcall(via_cuda_start);
0322 
0323 #ifdef CONFIG_ADB
0324 static int
0325 cuda_probe(void)
0326 {
0327 #ifdef CONFIG_PPC
0328     if (sys_ctrler != SYS_CTRLER_CUDA)
0329     return -ENODEV;
0330 #else
0331     if (macintosh_config->adb_type != MAC_ADB_CUDA &&
0332         macintosh_config->adb_type != MAC_ADB_EGRET)
0333     return -ENODEV;
0334 #endif
0335     if (via == NULL)
0336     return -ENODEV;
0337     return 0;
0338 }
0339 #endif /* CONFIG_ADB */
0340 
0341 static int __init sync_egret(void)
0342 {
0343     if (TREQ_asserted(in_8(&via[B]))) {
0344         /* Complete the inbound transfer */
0345         assert_TIP_and_TACK();
0346         while (1) {
0347             negate_TACK();
0348             mdelay(1);
0349             (void)in_8(&via[SR]);
0350             assert_TACK();
0351             if (!TREQ_asserted(in_8(&via[B])))
0352                 break;
0353         }
0354         negate_TIP_and_TACK();
0355     } else if (in_8(&via[B]) & TIP) {
0356         /* Terminate the outbound transfer */
0357         negate_TACK();
0358         assert_TACK();
0359         mdelay(1);
0360         negate_TIP_and_TACK();
0361     }
0362     /* Clear shift register interrupt */
0363     if (in_8(&via[IFR]) & SR_INT)
0364         (void)in_8(&via[SR]);
0365     return 0;
0366 }
0367 
0368 #define WAIT_FOR(cond, what)                    \
0369     do {                                                        \
0370         int x;                          \
0371     for (x = 1000; !(cond); --x) {              \
0372         if (x == 0) {                   \
0373         pr_err("Timeout waiting for " what "\n");   \
0374         return -ENXIO;                  \
0375         }                           \
0376         udelay(100);                    \
0377     }                           \
0378     } while (0)
0379 
0380 static int
0381 __init cuda_init_via(void)
0382 {
0383 #ifdef CONFIG_PPC
0384     out_8(&via[IER], 0x7f);                 /* disable interrupts from VIA */
0385     (void)in_8(&via[IER]);
0386 #else
0387     out_8(&via[IER], SR_INT);                   /* disable SR interrupt from VIA */
0388 #endif
0389 
0390     out_8(&via[DIRB], (in_8(&via[DIRB]) | TACK | TIP) & ~TREQ); /* TACK & TIP out */
0391     out_8(&via[ACR], (in_8(&via[ACR]) & ~SR_CTRL) | SR_EXT);    /* SR data in */
0392     (void)in_8(&via[SR]);                   /* clear any left-over data */
0393 
0394     if (mcu_is_egret)
0395     return sync_egret();
0396 
0397     negate_TIP_and_TACK();
0398 
0399     /* delay 4ms and then clear any pending interrupt */
0400     mdelay(4);
0401     (void)in_8(&via[SR]);
0402     out_8(&via[IFR], SR_INT);
0403 
0404     /* sync with the CUDA - assert TACK without TIP */
0405     assert_TACK();
0406 
0407     /* wait for the CUDA to assert TREQ in response */
0408     WAIT_FOR(TREQ_asserted(in_8(&via[B])), "CUDA response to sync");
0409 
0410     /* wait for the interrupt and then clear it */
0411     WAIT_FOR(in_8(&via[IFR]) & SR_INT, "CUDA response to sync (2)");
0412     (void)in_8(&via[SR]);
0413     out_8(&via[IFR], SR_INT);
0414 
0415     /* finish the sync by negating TACK */
0416     negate_TACK();
0417 
0418     /* wait for the CUDA to negate TREQ and the corresponding interrupt */
0419     WAIT_FOR(!TREQ_asserted(in_8(&via[B])), "CUDA response to sync (3)");
0420     WAIT_FOR(in_8(&via[IFR]) & SR_INT, "CUDA response to sync (4)");
0421     (void)in_8(&via[SR]);
0422     out_8(&via[IFR], SR_INT);
0423 
0424     return 0;
0425 }
0426 
0427 #ifdef CONFIG_ADB
0428 /* Send an ADB command */
0429 static int
0430 cuda_send_request(struct adb_request *req, int sync)
0431 {
0432     int i;
0433 
0434     if ((via == NULL) || !cuda_fully_inited) {
0435     req->complete = 1;
0436     return -ENXIO;
0437     }
0438   
0439     req->reply_expected = 1;
0440 
0441     i = cuda_write(req);
0442     if (i)
0443     return i;
0444 
0445     if (sync) {
0446     while (!req->complete)
0447         cuda_poll();
0448     }
0449     return 0;
0450 }
0451 
0452 
0453 /* Enable/disable autopolling */
0454 static int
0455 cuda_adb_autopoll(int devs)
0456 {
0457     struct adb_request req;
0458 
0459     if ((via == NULL) || !cuda_fully_inited)
0460     return -ENXIO;
0461 
0462     cuda_request(&req, NULL, 3, CUDA_PACKET, CUDA_AUTOPOLL, (devs? 1: 0));
0463     while (!req.complete)
0464     cuda_poll();
0465     return 0;
0466 }
0467 
0468 /* Reset adb bus - how do we do this?? */
0469 static int
0470 cuda_reset_adb_bus(void)
0471 {
0472     struct adb_request req;
0473 
0474     if ((via == NULL) || !cuda_fully_inited)
0475     return -ENXIO;
0476 
0477     cuda_request(&req, NULL, 2, ADB_PACKET, 0);     /* maybe? */
0478     while (!req.complete)
0479     cuda_poll();
0480     return 0;
0481 }
0482 #endif /* CONFIG_ADB */
0483 
0484 /* Construct and send a cuda request */
0485 int
0486 cuda_request(struct adb_request *req, void (*done)(struct adb_request *),
0487          int nbytes, ...)
0488 {
0489     va_list list;
0490     int i;
0491 
0492     if (via == NULL) {
0493     req->complete = 1;
0494     return -ENXIO;
0495     }
0496 
0497     req->nbytes = nbytes;
0498     req->done = done;
0499     va_start(list, nbytes);
0500     for (i = 0; i < nbytes; ++i)
0501     req->data[i] = va_arg(list, int);
0502     va_end(list);
0503     req->reply_expected = 1;
0504     return cuda_write(req);
0505 }
0506 EXPORT_SYMBOL(cuda_request);
0507 
0508 static int
0509 cuda_write(struct adb_request *req)
0510 {
0511     unsigned long flags;
0512 
0513     if (req->nbytes < 2 || req->data[0] > CUDA_PACKET) {
0514     req->complete = 1;
0515     return -EINVAL;
0516     }
0517     req->next = NULL;
0518     req->sent = 0;
0519     req->complete = 0;
0520     req->reply_len = 0;
0521 
0522     spin_lock_irqsave(&cuda_lock, flags);
0523     if (current_req) {
0524     last_req->next = req;
0525     last_req = req;
0526     } else {
0527     current_req = req;
0528     last_req = req;
0529     if (cuda_state == idle)
0530         cuda_start();
0531     }
0532     spin_unlock_irqrestore(&cuda_lock, flags);
0533 
0534     return 0;
0535 }
0536 
0537 static void
0538 cuda_start(void)
0539 {
0540     /* assert cuda_state == idle */
0541     if (current_req == NULL)
0542     return;
0543     data_index = 0;
0544     if (TREQ_asserted(in_8(&via[B])))
0545     return;         /* a byte is coming in from the CUDA */
0546 
0547     /* set the shift register to shift out and send a byte */
0548     out_8(&via[ACR], in_8(&via[ACR]) | SR_OUT);
0549     out_8(&via[SR], current_req->data[data_index++]);
0550     if (mcu_is_egret)
0551     assert_TIP_and_TACK();
0552     else
0553     assert_TIP();
0554     cuda_state = sent_first_byte;
0555 }
0556 
0557 void
0558 cuda_poll(void)
0559 {
0560     cuda_interrupt(0, NULL);
0561 }
0562 EXPORT_SYMBOL(cuda_poll);
0563 
0564 #define ARRAY_FULL(a, p)    ((p) - (a) == ARRAY_SIZE(a))
0565 
0566 static irqreturn_t
0567 cuda_interrupt(int irq, void *arg)
0568 {
0569     unsigned long flags;
0570     u8 status;
0571     struct adb_request *req = NULL;
0572     unsigned char ibuf[16];
0573     int ibuf_len = 0;
0574     int complete = 0;
0575     bool full;
0576     
0577     spin_lock_irqsave(&cuda_lock, flags);
0578 
0579     /* On powermacs, this handler is registered for the VIA IRQ. But they use
0580      * just the shift register IRQ -- other VIA interrupt sources are disabled.
0581      * On m68k macs, the VIA IRQ sources are dispatched individually. Unless
0582      * we are polling, the shift register IRQ flag has already been cleared.
0583      */
0584 
0585 #ifdef CONFIG_MAC
0586     if (!arg)
0587 #endif
0588     {
0589         if ((in_8(&via[IFR]) & SR_INT) == 0) {
0590             spin_unlock_irqrestore(&cuda_lock, flags);
0591             return IRQ_NONE;
0592         } else {
0593             out_8(&via[IFR], SR_INT);
0594         }
0595     }
0596 
0597     status = in_8(&via[B]) & (TIP | TACK | TREQ);
0598 
0599     switch (cuda_state) {
0600     case idle:
0601     /* System controller has unsolicited data for us */
0602     (void)in_8(&via[SR]);
0603 idle_state:
0604     assert_TIP();
0605     cuda_state = reading;
0606     reply_ptr = cuda_rbuf;
0607     reading_reply = 0;
0608     break;
0609 
0610     case awaiting_reply:
0611     /* System controller has reply data for us */
0612     (void)in_8(&via[SR]);
0613     assert_TIP();
0614     cuda_state = reading;
0615     reply_ptr = current_req->reply;
0616     reading_reply = 1;
0617     break;
0618 
0619     case sent_first_byte:
0620     if (TREQ_asserted(status)) {
0621         /* collision */
0622         out_8(&via[ACR], in_8(&via[ACR]) & ~SR_OUT);
0623         (void)in_8(&via[SR]);
0624         negate_TIP_and_TACK();
0625         cuda_state = idle;
0626         /* Egret does not raise an "aborted" interrupt */
0627         if (mcu_is_egret)
0628         goto idle_state;
0629     } else {
0630         out_8(&via[SR], current_req->data[data_index++]);
0631         toggle_TACK();
0632         if (mcu_is_egret)
0633         assert_TACK();
0634         cuda_state = sending;
0635     }
0636     break;
0637 
0638     case sending:
0639     req = current_req;
0640     if (data_index >= req->nbytes) {
0641         out_8(&via[ACR], in_8(&via[ACR]) & ~SR_OUT);
0642         (void)in_8(&via[SR]);
0643         negate_TIP_and_TACK();
0644         req->sent = 1;
0645         if (req->reply_expected) {
0646         cuda_state = awaiting_reply;
0647         } else {
0648         current_req = req->next;
0649         complete = 1;
0650         /* not sure about this */
0651         cuda_state = idle;
0652         cuda_start();
0653         }
0654     } else {
0655         out_8(&via[SR], req->data[data_index++]);
0656         toggle_TACK();
0657         if (mcu_is_egret)
0658         assert_TACK();
0659     }
0660     break;
0661 
0662     case reading:
0663     full = reading_reply ? ARRAY_FULL(current_req->reply, reply_ptr)
0664                          : ARRAY_FULL(cuda_rbuf, reply_ptr);
0665     if (full)
0666         (void)in_8(&via[SR]);
0667     else
0668         *reply_ptr++ = in_8(&via[SR]);
0669     if (!TREQ_asserted(status) || full) {
0670         if (mcu_is_egret)
0671         assert_TACK();
0672         /* that's all folks */
0673         negate_TIP_and_TACK();
0674         cuda_state = read_done;
0675         /* Egret does not raise a "read done" interrupt */
0676         if (mcu_is_egret)
0677         goto read_done_state;
0678     } else {
0679         toggle_TACK();
0680         if (mcu_is_egret)
0681         negate_TACK();
0682     }
0683     break;
0684 
0685     case read_done:
0686     (void)in_8(&via[SR]);
0687 read_done_state:
0688     if (reading_reply) {
0689         req = current_req;
0690         req->reply_len = reply_ptr - req->reply;
0691         if (req->data[0] == ADB_PACKET) {
0692         /* Have to adjust the reply from ADB commands */
0693         if (req->reply_len <= 2 || (req->reply[1] & 2) != 0) {
0694             /* the 0x2 bit indicates no response */
0695             req->reply_len = 0;
0696         } else {
0697             /* leave just the command and result bytes in the reply */
0698             req->reply_len -= 2;
0699             memmove(req->reply, req->reply + 2, req->reply_len);
0700         }
0701         }
0702         current_req = req->next;
0703         complete = 1;
0704         reading_reply = 0;
0705     } else {
0706         /* This is tricky. We must break the spinlock to call
0707          * cuda_input. However, doing so means we might get
0708          * re-entered from another CPU getting an interrupt
0709          * or calling cuda_poll(). I ended up using the stack
0710          * (it's only for 16 bytes) and moving the actual
0711          * call to cuda_input to outside of the lock.
0712          */
0713         ibuf_len = reply_ptr - cuda_rbuf;
0714         memcpy(ibuf, cuda_rbuf, ibuf_len);
0715     }
0716     reply_ptr = cuda_rbuf;
0717     cuda_state = idle;
0718     cuda_start();
0719     if (cuda_state == idle && TREQ_asserted(in_8(&via[B]))) {
0720         assert_TIP();
0721         cuda_state = reading;
0722     }
0723     break;
0724 
0725     default:
0726     pr_err("cuda_interrupt: unknown cuda_state %d?\n", cuda_state);
0727     }
0728     spin_unlock_irqrestore(&cuda_lock, flags);
0729     if (complete && req) {
0730         void (*done)(struct adb_request *) = req->done;
0731         mb();
0732         req->complete = 1;
0733         /* Here, we assume that if the request has a done member, the
0734          * struct request will survive to setting req->complete to 1
0735          */
0736         if (done)
0737         (*done)(req);
0738     }
0739     if (ibuf_len)
0740     cuda_input(ibuf, ibuf_len);
0741     return IRQ_HANDLED;
0742 }
0743 
0744 static void
0745 cuda_input(unsigned char *buf, int nb)
0746 {
0747     switch (buf[0]) {
0748     case ADB_PACKET:
0749 #ifdef CONFIG_XMON
0750     if (nb == 5 && buf[2] == 0x2c) {
0751         extern int xmon_wants_key, xmon_adb_keycode;
0752         if (xmon_wants_key) {
0753         xmon_adb_keycode = buf[3];
0754         return;
0755         }
0756     }
0757 #endif /* CONFIG_XMON */
0758 #ifdef CONFIG_ADB
0759     adb_input(buf+2, nb-2, buf[1] & 0x40);
0760 #endif /* CONFIG_ADB */
0761     break;
0762 
0763     case TIMER_PACKET:
0764     /* Egret sends these periodically. Might be useful as a 'heartbeat'
0765      * to trigger a recovery for the VIA shift register errata.
0766      */
0767     break;
0768 
0769     default:
0770     print_hex_dump(KERN_INFO, "cuda_input: ", DUMP_PREFIX_NONE, 32, 1,
0771                    buf, nb, false);
0772     }
0773 }
0774 
0775 /* Offset between Unix time (1970-based) and Mac time (1904-based) */
0776 #define RTC_OFFSET  2082844800
0777 
0778 time64_t cuda_get_time(void)
0779 {
0780     struct adb_request req;
0781     u32 now;
0782 
0783     if (cuda_request(&req, NULL, 2, CUDA_PACKET, CUDA_GET_TIME) < 0)
0784         return 0;
0785     while (!req.complete)
0786         cuda_poll();
0787     if (req.reply_len != 7)
0788         pr_err("%s: got %d byte reply\n", __func__, req.reply_len);
0789     now = (req.reply[3] << 24) + (req.reply[4] << 16) +
0790           (req.reply[5] << 8) + req.reply[6];
0791     return (time64_t)now - RTC_OFFSET;
0792 }
0793 
0794 int cuda_set_rtc_time(struct rtc_time *tm)
0795 {
0796     u32 now;
0797     struct adb_request req;
0798 
0799     now = lower_32_bits(rtc_tm_to_time64(tm) + RTC_OFFSET);
0800     if (cuda_request(&req, NULL, 6, CUDA_PACKET, CUDA_SET_TIME,
0801                      now >> 24, now >> 16, now >> 8, now) < 0)
0802         return -ENXIO;
0803     while (!req.complete)
0804         cuda_poll();
0805     if ((req.reply_len != 3) && (req.reply_len != 7))
0806         pr_err("%s: got %d byte reply\n", __func__, req.reply_len);
0807     return 0;
0808 }