source: mainline/uspace/srv/hw/bus/cuda_adb/cuda_adb.c@ 94868e1

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 94868e1 was 943aaf1b, checked in by Jiri Svoboda <jiri@…>, 8 years ago

Eliminate global variables in CUDA driver.

  • Property mode set to 100644
File size: 11.7 KB
Line 
1/*
2 * Copyright (c) 2010 Jiri Svoboda
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup genarch
30 * @{
31 */
32/** @file VIA-CUDA Apple Desktop Bus driver
33 *
34 * Note: We should really do a full bus scan at the beginning and resolve
35 * address conflicts. Also we should consider the handler ID in r3. Instead
36 * we just assume a keyboard at address 2 or 8 and a mouse at address 9.
37 */
38
39#include <stdio.h>
40#include <stdlib.h>
41#include <stddef.h>
42#include <stdint.h>
43#include <stdbool.h>
44#include <ddi.h>
45#include <libarch/ddi.h>
46#include <loc.h>
47#include <sysinfo.h>
48#include <errno.h>
49#include <ipc/adb.h>
50#include <assert.h>
51#include "cuda_adb.h"
52#include "cuda_hw.h"
53
54#define NAME "cuda_adb"
55
56static void cuda_connection(ipc_callid_t, ipc_call_t *, void *);
57static int cuda_init(cuda_instance_t *);
58static void cuda_irq_handler(ipc_callid_t, ipc_call_t *, void *);
59
60static void cuda_irq_listen(cuda_instance_t *);
61static void cuda_irq_receive(cuda_instance_t *);
62static void cuda_irq_rcv_end(cuda_instance_t *, void *, size_t *);
63static void cuda_irq_send_start(cuda_instance_t *);
64static void cuda_irq_send(cuda_instance_t *);
65
66static void cuda_packet_handle(cuda_instance_t *, uint8_t *, size_t);
67static void cuda_send_start(cuda_instance_t *);
68static void cuda_autopoll_set(cuda_instance_t *, bool);
69
70static void adb_packet_handle(cuda_instance_t *, uint8_t *, size_t, bool);
71
72static irq_pio_range_t cuda_ranges[] = {
73 {
74 .base = 0,
75 .size = sizeof(cuda_t)
76 }
77};
78
79static irq_cmd_t cuda_cmds[] = {
80 {
81 .cmd = CMD_PIO_READ_8,
82 .addr = NULL,
83 .dstarg = 1
84 },
85 {
86 .cmd = CMD_AND,
87 .value = SR_INT,
88 .srcarg = 1,
89 .dstarg = 2
90 },
91 {
92 .cmd = CMD_PREDICATE,
93 .value = 1,
94 .srcarg = 2
95 },
96 {
97 .cmd = CMD_ACCEPT
98 }
99};
100
101
102static irq_code_t cuda_irq_code = {
103 sizeof(cuda_ranges) / sizeof(irq_pio_range_t),
104 cuda_ranges,
105 sizeof(cuda_cmds) / sizeof(irq_cmd_t),
106 cuda_cmds
107};
108
109int main(int argc, char *argv[])
110{
111 service_id_t service_id;
112 cuda_instance_t cinst;
113 int rc;
114 int i;
115
116 printf(NAME ": VIA-CUDA Apple Desktop Bus driver\n");
117
118 for (i = 0; i < ADB_MAX_ADDR; ++i) {
119 cinst.adb_dev[i].client_sess = NULL;
120 cinst.adb_dev[i].service_id = 0;
121 }
122
123 async_set_fallback_port_handler(cuda_connection, &cinst);
124 rc = loc_server_register(NAME);
125 if (rc < 0) {
126 printf(NAME ": Unable to register server.\n");
127 return rc;
128 }
129
130 rc = loc_service_register("adb/kbd", &service_id);
131 if (rc != EOK) {
132 printf(NAME ": Unable to register service %s.\n", "adb/kbd");
133 return rc;
134 }
135
136 cinst.adb_dev[2].service_id = service_id;
137 cinst.adb_dev[8].service_id = service_id;
138
139 rc = loc_service_register("adb/mouse", &service_id);
140 if (rc != EOK) {
141 printf(NAME ": Unable to register service %s.\n", "adb/mouse");
142 return rc;
143 }
144
145 cinst.adb_dev[9].service_id = service_id;
146
147 if (cuda_init(&cinst) < 0) {
148 printf("cuda_init() failed\n");
149 return 1;
150 }
151
152 task_retval(0);
153 async_manager();
154
155 return 0;
156}
157
158/** Character device connection handler */
159static void cuda_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
160{
161 ipc_callid_t callid;
162 ipc_call_t call;
163 sysarg_t method;
164 service_id_t dsid;
165 cuda_instance_t *cuda = (cuda_instance_t *) arg;
166 int dev_addr, i;
167
168 /* Get the device handle. */
169 dsid = IPC_GET_ARG2(*icall);
170
171 /* Determine which disk device is the client connecting to. */
172 dev_addr = -1;
173 for (i = 0; i < ADB_MAX_ADDR; i++) {
174 if (cuda->adb_dev[i].service_id == dsid)
175 dev_addr = i;
176 }
177
178 if (dev_addr < 0) {
179 async_answer_0(iid, EINVAL);
180 return;
181 }
182
183 /* Answer the IPC_M_CONNECT_ME_TO call. */
184 async_answer_0(iid, EOK);
185
186 while (true) {
187 callid = async_get_call(&call);
188 method = IPC_GET_IMETHOD(call);
189
190 if (!method) {
191 /* The other side has hung up. */
192 async_answer_0(callid, EOK);
193 return;
194 }
195
196 async_sess_t *sess =
197 async_callback_receive_start(EXCHANGE_SERIALIZE, &call);
198 if (sess != NULL) {
199 if (cuda->adb_dev[dev_addr].client_sess == NULL) {
200 cuda->adb_dev[dev_addr].client_sess = sess;
201
202 /*
203 * A hack so that we send the data to the session
204 * regardless of which address the device is on.
205 */
206 for (i = 0; i < ADB_MAX_ADDR; ++i) {
207 if (cuda->adb_dev[i].service_id == dsid)
208 cuda->adb_dev[i].client_sess = sess;
209 }
210
211 async_answer_0(callid, EOK);
212 } else
213 async_answer_0(callid, ELIMIT);
214 } else
215 async_answer_0(callid, EINVAL);
216 }
217}
218
219static int cuda_init(cuda_instance_t *cuda)
220{
221 if (sysinfo_get_value("cuda.address.physical", &(cuda->cuda_physical)) != EOK)
222 return -1;
223
224 void *vaddr;
225 if (pio_enable((void *) cuda->cuda_physical, sizeof(cuda_t), &vaddr) != 0)
226 return -1;
227
228 cuda->regs = vaddr;
229 cuda->xstate = cx_listen;
230 cuda->bidx = 0;
231 cuda->snd_bytes = 0;
232
233 fibril_mutex_initialize(&cuda->dev_lock);
234
235 /* Disable all interrupts from CUDA. */
236 pio_write_8(&cuda->regs->ier, IER_CLR | ALL_INT);
237
238 cuda_irq_code.ranges[0].base = (uintptr_t) cuda->cuda_physical;
239 cuda_irq_code.cmds[0].addr = (void *) &((cuda_t *) cuda->cuda_physical)->ifr;
240 async_irq_subscribe(10, cuda_irq_handler, cuda, &cuda_irq_code);
241
242 /* Enable SR interrupt. */
243 pio_write_8(&cuda->regs->ier, TIP | TREQ);
244 pio_write_8(&cuda->regs->ier, IER_SET | SR_INT);
245
246 /* Enable ADB autopolling. */
247 cuda_autopoll_set(cuda, true);
248
249 return 0;
250}
251
252static void cuda_irq_handler(ipc_callid_t iid, ipc_call_t *call, void *arg)
253{
254 uint8_t rbuf[CUDA_RCV_BUF_SIZE];
255 cuda_instance_t *cuda = (cuda_instance_t *)arg;
256 size_t len;
257 bool handle;
258
259 handle = false;
260 len = 0;
261
262 fibril_mutex_lock(&cuda->dev_lock);
263
264 switch (cuda->xstate) {
265 case cx_listen:
266 cuda_irq_listen(cuda);
267 break;
268 case cx_receive:
269 cuda_irq_receive(cuda);
270 break;
271 case cx_rcv_end:
272 cuda_irq_rcv_end(cuda, rbuf, &len);
273 handle = true;
274 break;
275 case cx_send_start:
276 cuda_irq_send_start(cuda);
277 break;
278 case cx_send:
279 cuda_irq_send(cuda);
280 break;
281 }
282
283 /* Lower IFR.SR_INT so that CUDA can generate next int by raising it. */
284 pio_write_8(&cuda->regs->ifr, SR_INT);
285
286 fibril_mutex_unlock(&cuda->dev_lock);
287
288 /* Handle an incoming packet. */
289 if (handle)
290 cuda_packet_handle(cuda, rbuf, len);
291}
292
293/** Interrupt in listen state.
294 *
295 * Start packet reception.
296 *
297 * @param cuda CUDA instance
298 */
299static void cuda_irq_listen(cuda_instance_t *cuda)
300{
301 uint8_t b = pio_read_8(&cuda->regs->b);
302
303 if ((b & TREQ) != 0) {
304 printf("cuda_irq_listen: no TREQ?!\n");
305 return;
306 }
307
308 pio_write_8(&cuda->regs->b, b & ~TIP);
309 cuda->xstate = cx_receive;
310}
311
312/** Interrupt in receive state.
313 *
314 * Receive next byte of packet.
315 *
316 * @param cuda CUDA instance
317 */
318static void cuda_irq_receive(cuda_instance_t *cuda)
319{
320 uint8_t data = pio_read_8(&cuda->regs->sr);
321 if (cuda->bidx < CUDA_RCV_BUF_SIZE)
322 cuda->rcv_buf[cuda->bidx++] = data;
323
324 uint8_t b = pio_read_8(&cuda->regs->b);
325
326 if ((b & TREQ) == 0) {
327 pio_write_8(&cuda->regs->b, b ^ TACK);
328 } else {
329 pio_write_8(&cuda->regs->b, b | TACK | TIP);
330 cuda->xstate = cx_rcv_end;
331 }
332}
333
334/** Interrupt in rcv_end state.
335 *
336 * Terminate packet reception. Either go back to listen state or start
337 * receiving another packet if CUDA has one for us.
338 *
339 * @param cuda CUDA instance
340 * @param buf Buffer for storing received packet
341 * @param len Place to store length of received packet
342 */
343static void cuda_irq_rcv_end(cuda_instance_t *cuda, void *buf, size_t *len)
344{
345 uint8_t b = pio_read_8(&cuda->regs->b);
346
347 if ((b & TREQ) == 0) {
348 cuda->xstate = cx_receive;
349 pio_write_8(&cuda->regs->b, b & ~TIP);
350 } else {
351 cuda->xstate = cx_listen;
352 cuda_send_start(cuda);
353 }
354
355 memcpy(buf, cuda->rcv_buf, cuda->bidx);
356 *len = cuda->bidx;
357 cuda->bidx = 0;
358}
359
360/** Interrupt in send_start state.
361 *
362 * Process result of sending first byte (and send second on success).
363 *
364 * @param cuda CUDA instance
365 */
366static void cuda_irq_send_start(cuda_instance_t *cuda)
367{
368 uint8_t b;
369
370 b = pio_read_8(&cuda->regs->b);
371
372 if ((b & TREQ) == 0) {
373 /* Collision */
374 pio_write_8(&cuda->regs->acr, pio_read_8(&cuda->regs->acr) &
375 ~SR_OUT);
376 pio_read_8(&cuda->regs->sr);
377 pio_write_8(&cuda->regs->b, pio_read_8(&cuda->regs->b) |
378 TIP | TACK);
379 cuda->xstate = cx_listen;
380 return;
381 }
382
383 pio_write_8(&cuda->regs->sr, cuda->snd_buf[1]);
384 pio_write_8(&cuda->regs->b, pio_read_8(&cuda->regs->b) ^ TACK);
385 cuda->bidx = 2;
386
387 cuda->xstate = cx_send;
388}
389
390/** Interrupt in send state.
391 *
392 * Send next byte or terminate transmission.
393 *
394 * @param cuda CUDA instance
395 */
396static void cuda_irq_send(cuda_instance_t *cuda)
397{
398 if (cuda->bidx < cuda->snd_bytes) {
399 /* Send next byte. */
400 pio_write_8(&cuda->regs->sr,
401 cuda->snd_buf[cuda->bidx++]);
402 pio_write_8(&cuda->regs->b, pio_read_8(&cuda->regs->b) ^ TACK);
403 return;
404 }
405
406 /* End transfer. */
407 cuda->snd_bytes = 0;
408 cuda->bidx = 0;
409
410 pio_write_8(&cuda->regs->acr, pio_read_8(&cuda->regs->acr) & ~SR_OUT);
411 pio_read_8(&cuda->regs->sr);
412 pio_write_8(&cuda->regs->b, pio_read_8(&cuda->regs->b) | TACK | TIP);
413
414 cuda->xstate = cx_listen;
415 /* TODO: Match reply with request. */
416}
417
418static void cuda_packet_handle(cuda_instance_t *cuda, uint8_t *data, size_t len)
419{
420 if (data[0] != PT_ADB)
421 return;
422 if (len < 2)
423 return;
424
425 adb_packet_handle(cuda, data + 2, len - 2, (data[1] & 0x40) != 0);
426}
427
428static void adb_packet_handle(cuda_instance_t *cuda, uint8_t *data,
429 size_t size, bool autopoll)
430{
431 uint8_t dev_addr;
432 uint8_t reg_no;
433 uint16_t reg_val;
434 unsigned i;
435
436 dev_addr = data[0] >> 4;
437 reg_no = data[0] & 0x03;
438
439 if (size != 3) {
440 printf("unrecognized packet, size=%zu\n", size);
441 for (i = 0; i < size; ++i) {
442 printf(" 0x%02x", data[i]);
443 }
444 putchar('\n');
445 return;
446 }
447
448 if (reg_no != 0) {
449 printf("unrecognized packet, size=%zu\n", size);
450 for (i = 0; i < size; ++i) {
451 printf(" 0x%02x", data[i]);
452 }
453 putchar('\n');
454 return;
455 }
456
457 reg_val = ((uint16_t) data[1] << 8) | (uint16_t) data[2];
458
459 if (cuda->adb_dev[dev_addr].client_sess == NULL)
460 return;
461
462 async_exch_t *exch =
463 async_exchange_begin(cuda->adb_dev[dev_addr].client_sess);
464 async_msg_1(exch, ADB_REG_NOTIF, reg_val);
465 async_exchange_end(exch);
466}
467
468static void cuda_autopoll_set(cuda_instance_t *cuda, bool enable)
469{
470 cuda->snd_buf[0] = PT_CUDA;
471 cuda->snd_buf[1] = CPT_AUTOPOLL;
472 cuda->snd_buf[2] = enable ? 0x01 : 0x00;
473 cuda->snd_bytes = 3;
474 cuda->bidx = 0;
475
476 cuda_send_start(cuda);
477}
478
479static void cuda_send_start(cuda_instance_t *cuda)
480{
481 assert(cuda->xstate == cx_listen);
482
483 if (cuda->snd_bytes == 0)
484 return;
485
486 /* Check for incoming data. */
487 if ((pio_read_8(&cuda->regs->b) & TREQ) == 0)
488 return;
489
490 pio_write_8(&cuda->regs->acr, pio_read_8(&cuda->regs->acr) | SR_OUT);
491 pio_write_8(&cuda->regs->sr, cuda->snd_buf[0]);
492 pio_write_8(&cuda->regs->b, pio_read_8(&cuda->regs->b) & ~TIP);
493
494 cuda->xstate = cx_send_start;
495}
496
497/** @}
498 */
Note: See TracBrowser for help on using the repository browser.