source: mainline/uspace/srv/hw/bus/cuda_adb/cuda_adb.c@ 991f645

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 991f645 was 991f645, checked in by Jakub Jermar <jakub@…>, 15 years ago

Rename dev_handle_t to devmap_handle_t and make it explicitly clear that
dev_handle_t is a handle understood by devmap.

  • 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 <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 devmap_handle_t devmap_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].devmap_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", &devmap_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].devmap_handle = devmap_handle;
170 adb_dev[8].devmap_handle = devmap_handle;
171
172 rc = devmap_device_register("adb/mouse", &devmap_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].devmap_handle = devmap_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 devmap_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].devmap_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].devmap_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 if (sysinfo_get_value("cuda.address.physical", &(instance->cuda_physical)) != EOK)
257 return -1;
258
259 if (sysinfo_get_value("cuda.address.kernel", &(instance->cuda_kernel)) != EOK)
260 return -1;
261
262 void *vaddr;
263 if (pio_enable((void *) instance->cuda_physical, sizeof(cuda_t), &vaddr) != 0)
264 return -1;
265
266 dev = vaddr;
267
268 instance->cuda = dev;
269 instance->xstate = cx_listen;
270 instance->bidx = 0;
271 instance->snd_bytes = 0;
272
273 fibril_mutex_initialize(&instance->dev_lock);
274
275 /* Disable all interrupts from CUDA. */
276 pio_write_8(&dev->ier, IER_CLR | ALL_INT);
277
278 cuda_irq_code.cmds[0].addr = (void *) &((cuda_t *) instance->cuda_kernel)->ifr;
279 async_set_interrupt_received(cuda_irq_handler);
280 ipc_register_irq(10, device_assign_devno(), 0, &cuda_irq_code);
281
282 /* Enable SR interrupt. */
283 pio_write_8(&dev->ier, TIP | TREQ);
284 pio_write_8(&dev->ier, IER_SET | SR_INT);
285
286 /* Enable ADB autopolling. */
287 cuda_autopoll_set(true);
288
289 return 0;
290}
291
292static void cuda_irq_handler(ipc_callid_t iid, ipc_call_t *call)
293{
294 uint8_t rbuf[CUDA_RCV_BUF_SIZE];
295 size_t len;
296 bool handle;
297
298 handle = false;
299 len = 0;
300
301 fibril_mutex_lock(&instance->dev_lock);
302
303 /* Lower IFR.SR_INT so that CUDA can generate next int by raising it. */
304 pio_write_8(&instance->cuda->ifr, SR_INT);
305
306 switch (instance->xstate) {
307 case cx_listen: cuda_irq_listen(); break;
308 case cx_receive: cuda_irq_receive(); break;
309 case cx_rcv_end: cuda_irq_rcv_end(rbuf, &len);
310 handle = true; break;
311 case cx_send_start: cuda_irq_send_start(); break;
312 case cx_send: cuda_irq_send(); break;
313 }
314
315 fibril_mutex_unlock(&instance->dev_lock);
316
317 /* Handle an incoming packet. */
318 if (handle)
319 cuda_packet_handle(rbuf, len);
320}
321
322/** Interrupt in listen state.
323 *
324 * Start packet reception.
325 */
326static void cuda_irq_listen(void)
327{
328 uint8_t b;
329
330 b = pio_read_8(&dev->b);
331
332 if ((b & TREQ) != 0) {
333 printf("cuda_irq_listen: no TREQ?!\n");
334 return;
335 }
336
337 pio_read_8(&dev->sr);
338 pio_write_8(&dev->b, pio_read_8(&dev->b) & ~TIP);
339 instance->xstate = cx_receive;
340}
341
342/** Interrupt in receive state.
343 *
344 * Receive next byte of packet.
345 */
346static void cuda_irq_receive(void)
347{
348 uint8_t b, data;
349
350 data = pio_read_8(&dev->sr);
351 if (instance->bidx < CUDA_RCV_BUF_SIZE)
352 instance->rcv_buf[instance->bidx++] = data;
353
354 b = pio_read_8(&dev->b);
355
356 if ((b & TREQ) == 0) {
357 pio_write_8(&dev->b, b ^ TACK);
358 } else {
359 pio_write_8(&dev->b, b | TACK | TIP);
360 instance->xstate = cx_rcv_end;
361 }
362}
363
364/** Interrupt in rcv_end state.
365 *
366 * Terminate packet reception. Either go back to listen state or start
367 * receiving another packet if CUDA has one for us.
368 */
369static void cuda_irq_rcv_end(void *buf, size_t *len)
370{
371 uint8_t data, b;
372
373 b = pio_read_8(&dev->b);
374 data = pio_read_8(&dev->sr);
375
376 if ((b & TREQ) == 0) {
377 instance->xstate = cx_receive;
378 pio_write_8(&dev->b, b & ~TIP);
379 } else {
380 instance->xstate = cx_listen;
381 cuda_send_start();
382 }
383
384 memcpy(buf, instance->rcv_buf, instance->bidx);
385 *len = instance->bidx;
386 instance->bidx = 0;
387}
388
389/** Interrupt in send_start state.
390 *
391 * Process result of sending first byte (and send second on success).
392 */
393static void cuda_irq_send_start(void)
394{
395 uint8_t b;
396
397 b = pio_read_8(&dev->b);
398
399 if ((b & TREQ) == 0) {
400 /* Collision */
401 pio_write_8(&dev->acr, pio_read_8(&dev->acr) & ~SR_OUT);
402 pio_read_8(&dev->sr);
403 pio_write_8(&dev->b, pio_read_8(&dev->b) | TIP | TACK);
404 instance->xstate = cx_listen;
405 return;
406 }
407
408 pio_write_8(&dev->sr, instance->snd_buf[1]);
409 pio_write_8(&dev->b, pio_read_8(&dev->b) ^ TACK);
410 instance->bidx = 2;
411
412 instance->xstate = cx_send;
413}
414
415/** Interrupt in send state.
416 *
417 * Send next byte or terminate transmission.
418 */
419static void cuda_irq_send(void)
420{
421 if (instance->bidx < instance->snd_bytes) {
422 /* Send next byte. */
423 pio_write_8(&dev->sr, instance->snd_buf[instance->bidx++]);
424 pio_write_8(&dev->b, pio_read_8(&dev->b) ^ TACK);
425 return;
426 }
427
428 /* End transfer. */
429 instance->snd_bytes = 0;
430 instance->bidx = 0;
431
432 pio_write_8(&dev->acr, pio_read_8(&dev->acr) & ~SR_OUT);
433 pio_read_8(&dev->sr);
434 pio_write_8(&dev->b, pio_read_8(&dev->b) | TACK | TIP);
435
436 instance->xstate = cx_listen;
437 /* TODO: Match reply with request. */
438}
439
440static void cuda_packet_handle(uint8_t *data, size_t len)
441{
442 if (data[0] != PT_ADB)
443 return;
444 if (len < 2)
445 return;
446
447 adb_packet_handle(data + 2, len - 2, (data[1] & 0x40) != 0);
448}
449
450static void adb_packet_handle(uint8_t *data, size_t size, bool autopoll)
451{
452 uint8_t dev_addr;
453 uint8_t reg_no;
454 uint16_t reg_val;
455 unsigned i;
456
457 dev_addr = data[0] >> 4;
458 reg_no = data[0] & 0x03;
459
460 if (size != 3) {
461 printf("unrecognized packet, size=%d\n", size);
462 for (i = 0; i < size; ++i) {
463 printf(" 0x%02x", data[i]);
464 }
465 putchar('\n');
466 return;
467 }
468
469 if (reg_no != 0) {
470 printf("unrecognized packet, size=%d\n", size);
471 for (i = 0; i < size; ++i) {
472 printf(" 0x%02x", data[i]);
473 }
474 putchar('\n');
475 return;
476 }
477
478 reg_val = ((uint16_t) data[1] << 8) | (uint16_t) data[2];
479
480 if (adb_dev[dev_addr].client_phone == -1)
481 return;
482
483 async_msg_1(adb_dev[dev_addr].client_phone, ADB_REG_NOTIF, reg_val);
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.