source: mainline/uspace/srv/hw/bus/cuda_adb/cuda_adb.c@ 36e9cd1

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 36e9cd1 was 36e9cd1, checked in by Martin Decky <martin@…>, 15 years ago

silence compiler warnings (no change in actual functionality)

  • Property mode set to 100644
File size: 11.3 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 <sys/types.h>
42#include <bool.h>
43#include <ddi.h>
44#include <libarch/ddi.h>
45#include <devmap.h>
46#include <sysinfo.h>
47#include <errno.h>
48#include <ipc/adb.h>
49#include <async.h>
50#include <assert.h>
51#include "cuda_adb.h"
52
53#define NAME "cuda_adb"
54
55static void cuda_connection(ipc_callid_t iid, ipc_call_t *icall);
56static int cuda_init(void);
57static void cuda_irq_handler(ipc_callid_t iid, ipc_call_t *call);
58
59static void cuda_irq_listen(void);
60static void cuda_irq_receive(void);
61static void cuda_irq_rcv_end(void *buf, size_t *len);
62static void cuda_irq_send_start(void);
63static void cuda_irq_send(void);
64
65static void cuda_packet_handle(uint8_t *buf, size_t len);
66static void cuda_send_start(void);
67static void cuda_autopoll_set(bool enable);
68
69static void adb_packet_handle(uint8_t *data, size_t size, bool autopoll);
70
71
72/** B register fields */
73enum {
74 TREQ = 0x08,
75 TACK = 0x10,
76 TIP = 0x20
77};
78
79/** IER register fields */
80enum {
81 IER_CLR = 0x00,
82 IER_SET = 0x80,
83
84 SR_INT = 0x04,
85 ALL_INT = 0x7f
86};
87
88/** ACR register fields */
89enum {
90 SR_OUT = 0x10
91};
92
93/** Packet types */
94enum {
95 PT_ADB = 0x00,
96 PT_CUDA = 0x01
97};
98
99/** CUDA packet types */
100enum {
101 CPT_AUTOPOLL = 0x01
102};
103
104enum {
105 ADB_MAX_ADDR = 16
106};
107
108static irq_cmd_t cuda_cmds[] = {
109 {
110 .cmd = CMD_PIO_READ_8,
111 .addr = NULL, /* will be patched in run-time */
112 .dstarg = 1
113 },
114 {
115 .cmd = CMD_BTEST,
116 .value = SR_INT,
117 .srcarg = 1,
118 .dstarg = 2
119 },
120 {
121 .cmd = CMD_PREDICATE,
122 .value = 1,
123 .srcarg = 2
124 },
125 {
126 .cmd = CMD_ACCEPT
127 }
128};
129
130
131static irq_code_t cuda_irq_code = {
132 sizeof(cuda_cmds) / sizeof(irq_cmd_t),
133 cuda_cmds
134};
135
136static cuda_instance_t cinst;
137
138static cuda_instance_t *instance = &cinst;
139static cuda_t *dev;
140
141static adb_dev_t adb_dev[ADB_MAX_ADDR];
142
143int main(int argc, char *argv[])
144{
145 dev_handle_t dev_handle;
146 int rc;
147 int i;
148
149 printf(NAME ": VIA-CUDA Apple Desktop Bus driver\n");
150
151 for (i = 0; i < ADB_MAX_ADDR; ++i) {
152 adb_dev[i].client_phone = -1;
153 adb_dev[i].dev_handle = 0;
154 }
155
156 rc = devmap_driver_register(NAME, cuda_connection);
157 if (rc < 0) {
158 printf(NAME ": Unable to register driver.\n");
159 return rc;
160 }
161
162 rc = devmap_device_register("adb/kbd", &dev_handle);
163 if (rc != EOK) {
164 devmap_hangup_phone(DEVMAP_DRIVER);
165 printf(NAME ": Unable to register device %s.\n", "adb/kdb");
166 return rc;
167 }
168
169 adb_dev[2].dev_handle = dev_handle;
170 adb_dev[8].dev_handle = dev_handle;
171
172 rc = devmap_device_register("adb/mouse", &dev_handle);
173 if (rc != EOK) {
174 devmap_hangup_phone(DEVMAP_DRIVER);
175 printf(NAME ": Unable to register device %s.\n", "adb/mouse");
176 return rc;
177 }
178
179 adb_dev[9].dev_handle = dev_handle;
180
181 if (cuda_init() < 0) {
182 printf("cuda_init() failed\n");
183 return 1;
184 }
185
186 task_retval(0);
187 async_manager();
188
189 return 0;
190}
191
192/** Character device connection handler */
193static void cuda_connection(ipc_callid_t iid, ipc_call_t *icall)
194{
195 ipc_callid_t callid;
196 ipc_call_t call;
197 ipcarg_t method;
198 dev_handle_t dh;
199 int retval;
200 int dev_addr, i;
201
202 /* Get the device handle. */
203 dh = IPC_GET_ARG1(*icall);
204
205 /* Determine which disk device is the client connecting to. */
206 dev_addr = -1;
207 for (i = 0; i < ADB_MAX_ADDR; i++) {
208 if (adb_dev[i].dev_handle == dh)
209 dev_addr = i;
210 }
211
212 if (dev_addr < 0) {
213 ipc_answer_0(iid, EINVAL);
214 return;
215 }
216
217 /* Answer the IPC_M_CONNECT_ME_TO call. */
218 ipc_answer_0(iid, EOK);
219
220 while (1) {
221 callid = async_get_call(&call);
222 method = IPC_GET_METHOD(call);
223 switch (method) {
224 case IPC_M_PHONE_HUNGUP:
225 /* The other side has hung up. */
226 ipc_answer_0(callid, EOK);
227 return;
228 case IPC_M_CONNECT_TO_ME:
229 if (adb_dev[dev_addr].client_phone != -1) {
230 retval = ELIMIT;
231 break;
232 }
233 adb_dev[dev_addr].client_phone = IPC_GET_ARG5(call);
234 /*
235 * A hack so that we send the data to the phone
236 * regardless of which address the device is on.
237 */
238 for (i = 0; i < ADB_MAX_ADDR; ++i) {
239 if (adb_dev[i].dev_handle == dh) {
240 adb_dev[i].client_phone = IPC_GET_ARG5(call);
241 }
242 }
243 retval = 0;
244 break;
245 default:
246 retval = EINVAL;
247 break;
248 }
249 ipc_answer_0(callid, retval);
250 }
251}
252
253
254static int cuda_init(void)
255{
256 void *vaddr;
257
258 instance->cuda_physical = sysinfo_value("cuda.address.physical");
259 instance->cuda_kernel = sysinfo_value("cuda.address.kernel");
260
261 if (pio_enable((void *) instance->cuda_physical, sizeof(cuda_t), &vaddr) != 0)
262 return -1;
263 dev = vaddr;
264
265 instance->cuda = dev;
266 instance->xstate = cx_listen;
267 instance->bidx = 0;
268 instance->snd_bytes = 0;
269
270 fibril_mutex_initialize(&instance->dev_lock);
271
272 /* Disable all interrupts from CUDA. */
273 pio_write_8(&dev->ier, IER_CLR | ALL_INT);
274
275 cuda_irq_code.cmds[0].addr = (void *) &((cuda_t *) instance->cuda_kernel)->ifr;
276 async_set_interrupt_received(cuda_irq_handler);
277 ipc_register_irq(10, device_assign_devno(), 0, &cuda_irq_code);
278
279 /* Enable SR interrupt. */
280 pio_write_8(&dev->ier, TIP | TREQ);
281 pio_write_8(&dev->ier, IER_SET | SR_INT);
282
283 /* Enable ADB autopolling. */
284 cuda_autopoll_set(true);
285
286 return 0;
287}
288
289static void cuda_irq_handler(ipc_callid_t iid, ipc_call_t *call)
290{
291 uint8_t rbuf[CUDA_RCV_BUF_SIZE];
292 size_t len;
293 bool handle;
294
295 handle = false;
296 len = 0;
297
298 fibril_mutex_lock(&instance->dev_lock);
299
300 /* Lower IFR.SR_INT so that CUDA can generate next int by raising it. */
301 pio_write_8(&instance->cuda->ifr, SR_INT);
302
303 switch (instance->xstate) {
304 case cx_listen: cuda_irq_listen(); break;
305 case cx_receive: cuda_irq_receive(); break;
306 case cx_rcv_end: cuda_irq_rcv_end(rbuf, &len);
307 handle = true; break;
308 case cx_send_start: cuda_irq_send_start(); break;
309 case cx_send: cuda_irq_send(); break;
310 }
311
312 fibril_mutex_unlock(&instance->dev_lock);
313
314 /* Handle an incoming packet. */
315 if (handle)
316 cuda_packet_handle(rbuf, len);
317}
318
319/** Interrupt in listen state.
320 *
321 * Start packet reception.
322 */
323static void cuda_irq_listen(void)
324{
325 uint8_t b;
326
327 b = pio_read_8(&dev->b);
328
329 if ((b & TREQ) != 0) {
330 printf("cuda_irq_listen: no TREQ?!\n");
331 return;
332 }
333
334 pio_read_8(&dev->sr);
335 pio_write_8(&dev->b, pio_read_8(&dev->b) & ~TIP);
336 instance->xstate = cx_receive;
337}
338
339/** Interrupt in receive state.
340 *
341 * Receive next byte of packet.
342 */
343static void cuda_irq_receive(void)
344{
345 uint8_t b, data;
346
347 data = pio_read_8(&dev->sr);
348 if (instance->bidx < CUDA_RCV_BUF_SIZE)
349 instance->rcv_buf[instance->bidx++] = data;
350
351 b = pio_read_8(&dev->b);
352
353 if ((b & TREQ) == 0) {
354 pio_write_8(&dev->b, b ^ TACK);
355 } else {
356 pio_write_8(&dev->b, b | TACK | TIP);
357 instance->xstate = cx_rcv_end;
358 }
359}
360
361/** Interrupt in rcv_end state.
362 *
363 * Terminate packet reception. Either go back to listen state or start
364 * receiving another packet if CUDA has one for us.
365 */
366static void cuda_irq_rcv_end(void *buf, size_t *len)
367{
368 uint8_t data, b;
369
370 b = pio_read_8(&dev->b);
371 data = pio_read_8(&dev->sr);
372
373 if ((b & TREQ) == 0) {
374 instance->xstate = cx_receive;
375 pio_write_8(&dev->b, b & ~TIP);
376 } else {
377 instance->xstate = cx_listen;
378 cuda_send_start();
379 }
380
381 memcpy(buf, instance->rcv_buf, instance->bidx);
382 *len = instance->bidx;
383 instance->bidx = 0;
384}
385
386/** Interrupt in send_start state.
387 *
388 * Process result of sending first byte (and send second on success).
389 */
390static void cuda_irq_send_start(void)
391{
392 uint8_t b;
393
394 b = pio_read_8(&dev->b);
395
396 if ((b & TREQ) == 0) {
397 /* Collision */
398 pio_write_8(&dev->acr, pio_read_8(&dev->acr) & ~SR_OUT);
399 pio_read_8(&dev->sr);
400 pio_write_8(&dev->b, pio_read_8(&dev->b) | TIP | TACK);
401 instance->xstate = cx_listen;
402 return;
403 }
404
405 pio_write_8(&dev->sr, instance->snd_buf[1]);
406 pio_write_8(&dev->b, pio_read_8(&dev->b) ^ TACK);
407 instance->bidx = 2;
408
409 instance->xstate = cx_send;
410}
411
412/** Interrupt in send state.
413 *
414 * Send next byte or terminate transmission.
415 */
416static void cuda_irq_send(void)
417{
418 if (instance->bidx < instance->snd_bytes) {
419 /* Send next byte. */
420 pio_write_8(&dev->sr, instance->snd_buf[instance->bidx++]);
421 pio_write_8(&dev->b, pio_read_8(&dev->b) ^ TACK);
422 return;
423 }
424
425 /* End transfer. */
426 instance->snd_bytes = 0;
427 instance->bidx = 0;
428
429 pio_write_8(&dev->acr, pio_read_8(&dev->acr) & ~SR_OUT);
430 pio_read_8(&dev->sr);
431 pio_write_8(&dev->b, pio_read_8(&dev->b) | TACK | TIP);
432
433 instance->xstate = cx_listen;
434 /* TODO: Match reply with request. */
435}
436
437static void cuda_packet_handle(uint8_t *data, size_t len)
438{
439 if (data[0] != PT_ADB)
440 return;
441 if (len < 2)
442 return;
443
444 adb_packet_handle(data + 2, len - 2, (data[1] & 0x40) != 0);
445}
446
447static void adb_packet_handle(uint8_t *data, size_t size, bool autopoll)
448{
449 uint8_t dev_addr;
450 uint8_t reg_no;
451 uint16_t reg_val;
452 unsigned i;
453
454 dev_addr = data[0] >> 4;
455 reg_no = data[0] & 0x03;
456
457 if (size != 3) {
458 printf("unrecognized packet, size=%d\n", size);
459 for (i = 0; i < size; ++i) {
460 printf(" 0x%02x", data[i]);
461 }
462 putchar('\n');
463 return;
464 }
465
466 if (reg_no != 0) {
467 printf("unrecognized packet, size=%d\n", size);
468 for (i = 0; i < size; ++i) {
469 printf(" 0x%02x", data[i]);
470 }
471 putchar('\n');
472 return;
473 }
474
475 reg_val = ((uint16_t) data[1] << 8) | (uint16_t) data[2];
476
477 if (adb_dev[dev_addr].client_phone == -1)
478 return;
479
480 async_msg_1(adb_dev[dev_addr].client_phone, ADB_REG_NOTIF, reg_val);
481}
482
483static void cuda_autopoll_set(bool enable)
484{
485 instance->snd_buf[0] = PT_CUDA;
486 instance->snd_buf[1] = CPT_AUTOPOLL;
487 instance->snd_buf[2] = enable ? 0x01 : 0x00;
488 instance->snd_bytes = 3;
489 instance->bidx = 0;
490
491 cuda_send_start();
492}
493
494static void cuda_send_start(void)
495{
496 cuda_t *dev = instance->cuda;
497
498 assert(instance->xstate == cx_listen);
499
500 if (instance->snd_bytes == 0)
501 return;
502
503 /* Check for incoming data. */
504 if ((pio_read_8(&dev->b) & TREQ) == 0)
505 return;
506
507 pio_write_8(&dev->acr, pio_read_8(&dev->acr) | SR_OUT);
508 pio_write_8(&dev->sr, instance->snd_buf[0]);
509 pio_write_8(&dev->b, pio_read_8(&dev->b) & ~TIP);
510
511 instance->xstate = cx_send_start;
512}
513
514
515/** @}
516 */
Note: See TracBrowser for help on using the repository browser.