source: mainline/uspace/srv/hw/bus/cuda_adb/cuda_adb.c@ 5da7199

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

remove the obsolete async API

  • Property mode set to 100644
File size: 11.4 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 <loc.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, void *arg);
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 service_id_t service_id;
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_sess = NULL;
153 adb_dev[i].service_id = 0;
154 }
155
156 rc = loc_server_register(NAME, cuda_connection);
157 if (rc < 0) {
158 printf(NAME ": Unable to register server.\n");
159 return rc;
160 }
161
162 rc = loc_service_register("adb/kbd", &service_id);
163 if (rc != EOK) {
164 printf(NAME ": Unable to register service %s.\n", "adb/kdb");
165 return rc;
166 }
167
168 adb_dev[2].service_id = service_id;
169 adb_dev[8].service_id = service_id;
170
171 rc = loc_service_register("adb/mouse", &service_id);
172 if (rc != EOK) {
173 printf(NAME ": Unable to register servise %s.\n", "adb/mouse");
174 return rc;
175 }
176
177 adb_dev[9].service_id = service_id;
178
179 if (cuda_init() < 0) {
180 printf("cuda_init() failed\n");
181 return 1;
182 }
183
184 task_retval(0);
185 async_manager();
186
187 return 0;
188}
189
190/** Character device connection handler */
191static void cuda_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
192{
193 ipc_callid_t callid;
194 ipc_call_t call;
195 sysarg_t method;
196 service_id_t dsid;
197 int dev_addr, i;
198
199 /* Get the device handle. */
200 dsid = IPC_GET_ARG1(*icall);
201
202 /* Determine which disk device is the client connecting to. */
203 dev_addr = -1;
204 for (i = 0; i < ADB_MAX_ADDR; i++) {
205 if (adb_dev[i].service_id == dsid)
206 dev_addr = i;
207 }
208
209 if (dev_addr < 0) {
210 async_answer_0(iid, EINVAL);
211 return;
212 }
213
214 /* Answer the IPC_M_CONNECT_ME_TO call. */
215 async_answer_0(iid, EOK);
216
217 while (true) {
218 callid = async_get_call(&call);
219 method = IPC_GET_IMETHOD(call);
220
221 if (!method) {
222 /* The other side has hung up. */
223 async_answer_0(callid, EOK);
224 return;
225 }
226
227 async_sess_t *sess =
228 async_callback_receive_start(EXCHANGE_SERIALIZE, &call);
229 if (sess != NULL) {
230 if (adb_dev[dev_addr].client_sess == NULL) {
231 adb_dev[dev_addr].client_sess = sess;
232
233 /*
234 * A hack so that we send the data to the session
235 * regardless of which address the device is on.
236 */
237 for (i = 0; i < ADB_MAX_ADDR; ++i) {
238 if (adb_dev[i].service_id == dsid)
239 adb_dev[i].client_sess = sess;
240 }
241
242 async_answer_0(callid, EOK);
243 } else
244 async_answer_0(callid, ELIMIT);
245 } else
246 async_answer_0(callid, EINVAL);
247 }
248}
249
250
251static int cuda_init(void)
252{
253 if (sysinfo_get_value("cuda.address.physical", &(instance->cuda_physical)) != EOK)
254 return -1;
255
256 if (sysinfo_get_value("cuda.address.kernel", &(instance->cuda_kernel)) != EOK)
257 return -1;
258
259 void *vaddr;
260 if (pio_enable((void *) instance->cuda_physical, sizeof(cuda_t), &vaddr) != 0)
261 return -1;
262
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 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 b;
369
370 b = pio_read_8(&dev->b);
371 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_sess == NULL)
478 return;
479
480 async_exch_t *exch =
481 async_exchange_begin(adb_dev[dev_addr].client_sess);
482 async_msg_1(exch, ADB_REG_NOTIF, reg_val);
483 async_exchange_end(exch);
484}
485
486static void cuda_autopoll_set(bool enable)
487{
488 instance->snd_buf[0] = PT_CUDA;
489 instance->snd_buf[1] = CPT_AUTOPOLL;
490 instance->snd_buf[2] = enable ? 0x01 : 0x00;
491 instance->snd_bytes = 3;
492 instance->bidx = 0;
493
494 cuda_send_start();
495}
496
497static void cuda_send_start(void)
498{
499 cuda_t *dev = instance->cuda;
500
501 assert(instance->xstate == cx_listen);
502
503 if (instance->snd_bytes == 0)
504 return;
505
506 /* Check for incoming data. */
507 if ((pio_read_8(&dev->b) & TREQ) == 0)
508 return;
509
510 pio_write_8(&dev->acr, pio_read_8(&dev->acr) | SR_OUT);
511 pio_write_8(&dev->sr, instance->snd_buf[0]);
512 pio_write_8(&dev->b, pio_read_8(&dev->b) & ~TIP);
513
514 instance->xstate = cx_send_start;
515}
516
517
518/** @}
519 */
Note: See TracBrowser for help on using the repository browser.