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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c69646f8 was 16dc887, checked in by Jiri Svoboda <jiri@…>, 14 years ago

Merge Location service.

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