Ok. I didn't make 2.4.0 in 2000. Tough. I tried, but we had some
[davej-history.git] / drivers / macintosh / adb-iop.c
blobcf0504e0e233f08f6ec8b8cac61d8c9154268108
1 /*
2 * I/O Processor (IOP) ADB Driver
3 * Written and (C) 1999 by Joshua M. Thompson (funaho@jurai.org)
4 * Based on via-cuda.c by Paul Mackerras.
6 * 1999-07-01 (jmt) - First implementation for new driver architecture.
8 * 1999-07-31 (jmt) - First working version.
10 * TODO:
12 * o Implement SRQ handling.
15 #include <linux/types.h>
16 #include <linux/kernel.h>
17 #include <linux/mm.h>
18 #include <linux/delay.h>
19 #include <linux/init.h>
20 #include <linux/proc_fs.h>
22 #include <asm/bootinfo.h>
23 #include <asm/macintosh.h>
24 #include <asm/macints.h>
25 #include <asm/mac_iop.h>
26 #include <asm/mac_oss.h>
27 #include <asm/adb_iop.h>
29 #include <linux/adb.h>
31 /*#define DEBUG_ADB_IOP*/
33 extern void iop_ism_irq(int, void *, struct pt_regs *);
35 static struct adb_request *current_req;
36 static struct adb_request *last_req;
37 #if 0
38 static unsigned char reply_buff[16];
39 static unsigned char *reply_ptr;
40 #endif
42 static enum adb_iop_state {
43 idle,
44 sending,
45 awaiting_reply
46 } adb_iop_state;
48 static void adb_iop_start(void);
49 static int adb_iop_probe(void);
50 static int adb_iop_init(void);
51 static int adb_iop_send_request(struct adb_request *, int);
52 static int adb_iop_write(struct adb_request *);
53 static int adb_iop_autopoll(int);
54 static void adb_iop_poll(void);
55 static int adb_iop_reset_bus(void);
57 struct adb_driver adb_iop_driver = {
58 "ISM IOP",
59 adb_iop_probe,
60 adb_iop_init,
61 adb_iop_send_request,
62 adb_iop_autopoll,
63 adb_iop_poll,
64 adb_iop_reset_bus
67 static void adb_iop_end_req(struct adb_request *req, int state)
69 req->complete = 1;
70 current_req = req->next;
71 if (req->done) (*req->done)(req);
72 adb_iop_state = state;
76 * Completion routine for ADB commands sent to the IOP.
78 * This will be called when a packet has been successfully sent.
81 static void adb_iop_complete(struct iop_msg *msg, struct pt_regs *regs)
83 struct adb_request *req;
84 uint flags;
86 save_flags(flags);
87 cli();
89 req = current_req;
90 if ((adb_iop_state == sending) && req && req->reply_expected) {
91 adb_iop_state = awaiting_reply;
94 restore_flags(flags);
98 * Listen for ADB messages from the IOP.
100 * This will be called when unsolicited messages (usually replies to TALK
101 * commands or autopoll packets) are received.
104 static void adb_iop_listen(struct iop_msg *msg, struct pt_regs *regs)
106 struct adb_iopmsg *amsg = (struct adb_iopmsg *) msg->message;
107 struct adb_request *req;
108 uint flags;
110 save_flags(flags);
111 cli();
113 req = current_req;
115 #ifdef DEBUG_ADB_IOP
116 printk("adb_iop_listen: rcvd packet, %d bytes: %02X %02X",
117 (uint) amsg->count + 2, (uint) amsg->flags, (uint) amsg->cmd);
118 i = 0;
119 while (i < amsg->count) {
120 printk(" %02X", (uint) amsg->data[i++]);
122 printk("\n");
123 #endif
125 /* Handle a timeout. Timeout packets seem to occur even after */
126 /* we've gotten a valid reply to a TALK, so I'm assuming that */
127 /* a "timeout" is actually more like an "end-of-data" signal. */
128 /* We need to send back a timeout packet to the IOP to shut */
129 /* it up, plus complete the current request, if any. */
131 if (amsg->flags & ADB_IOP_TIMEOUT) {
132 msg->reply[0] = ADB_IOP_TIMEOUT | ADB_IOP_AUTOPOLL;
133 msg->reply[1] = 0;
134 msg->reply[2] = 0;
135 if (req && (adb_iop_state != idle)) {
136 adb_iop_end_req(req, idle);
138 } else {
139 /* TODO: is it possible for more tha one chunk of data */
140 /* to arrive before the timeout? If so we need to */
141 /* use reply_ptr here like the other drivers do. */
142 if ((adb_iop_state == awaiting_reply) &&
143 (amsg->flags & ADB_IOP_EXPLICIT)) {
144 req->reply_len = amsg->count + 1;
145 memcpy(req->reply, &amsg->cmd, req->reply_len);
146 } else {
147 adb_input(&amsg->cmd, amsg->count + 1, regs,
148 amsg->flags & ADB_IOP_AUTOPOLL);
150 memcpy(msg->reply, msg->message, IOP_MSG_LEN);
152 iop_complete_message(msg);
153 restore_flags(flags);
157 * Start sending an ADB packet, IOP style
159 * There isn't much to do other than hand the packet over to the IOP
160 * after encapsulating it in an adb_iopmsg.
163 static void adb_iop_start(void)
165 unsigned long flags;
166 struct adb_request *req;
167 struct adb_iopmsg amsg;
169 /* get the packet to send */
170 req = current_req;
171 if (!req) return;
173 save_flags(flags);
174 cli();
176 #ifdef DEBUG_ADB_IOP
177 printk("adb_iop_start: sending packet, %d bytes:", req->nbytes);
178 for (i = 0 ; i < req->nbytes ; i++)
179 printk(" %02X", (uint) req->data[i]);
180 printk("\n");
181 #endif
183 /* The IOP takes MacII-style packets, so */
184 /* strip the initial ADB_PACKET byte. */
186 amsg.flags = ADB_IOP_EXPLICIT;
187 amsg.count = req->nbytes - 2;
189 /* amsg.data immediately follows amsg.cmd, effectively making */
190 /* amsg.cmd a pointer to the beginning of a full ADB packet. */
191 memcpy(&amsg.cmd, req->data + 1, req->nbytes - 1);
193 req->sent = 1;
194 adb_iop_state = sending;
195 restore_flags(flags);
197 /* Now send it. The IOP manager will call adb_iop_complete */
198 /* when the packet has been sent. */
200 iop_send_message(ADB_IOP, ADB_CHAN, req,
201 sizeof(amsg), (__u8 *) &amsg, adb_iop_complete);
204 int adb_iop_probe(void)
206 if (!iop_ism_present) return -ENODEV;
207 return 0;
210 int adb_iop_init(void)
212 printk("adb: IOP ISM driver v0.4 for Unified ADB.\n");
213 iop_listen(ADB_IOP, ADB_CHAN, adb_iop_listen, "ADB");
214 return 0;
217 int adb_iop_send_request(struct adb_request *req, int sync)
219 int err;
221 err = adb_iop_write(req);
222 if (err) return err;
224 if (sync) {
225 while (!req->complete) adb_iop_poll();
227 return 0;
230 static int adb_iop_write(struct adb_request *req)
232 unsigned long flags;
234 if ((req->nbytes < 2) || (req->data[0] != ADB_PACKET)) {
235 req->complete = 1;
236 return -EINVAL;
239 save_flags(flags);
240 cli();
242 req->next = 0;
243 req->sent = 0;
244 req->complete = 0;
245 req->reply_len = 0;
247 if (current_req != 0) {
248 last_req->next = req;
249 last_req = req;
250 } else {
251 current_req = req;
252 last_req = req;
255 restore_flags(flags);
256 if (adb_iop_state == idle) adb_iop_start();
257 return 0;
260 int adb_iop_autopoll(int devs)
262 /* TODO: how do we enable/disable autopoll? */
263 return 0;
266 void adb_iop_poll(void)
268 if (adb_iop_state == idle) adb_iop_start();
269 iop_ism_irq(0, (void *) ADB_IOP, NULL);
272 int adb_iop_reset_bus(void)
274 struct adb_request req;
276 req.reply_expected = 0;
277 req.nbytes = 2;
278 req.data[0] = ADB_PACKET;
279 req.data[1] = 0; /* RESET */
280 adb_iop_write(&req);
281 while (!req.complete) adb_iop_poll();
282 return 0;