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

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

Cease using devmap_get_phone() and devmap_hangup_phone() in drivers.

  • 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 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 printf(NAME ": Unable to register device %s.\n", "adb/kdb");
165 return rc;
166 }
167
168 adb_dev[2].devmap_handle = devmap_handle;
169 adb_dev[8].devmap_handle = devmap_handle;
170
171 rc = devmap_device_register("adb/mouse", &devmap_handle);
172 if (rc != EOK) {
173 printf(NAME ": Unable to register device %s.\n", "adb/mouse");
174 return rc;
175 }
176
177 adb_dev[9].devmap_handle = devmap_handle;
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)
192{
193 ipc_callid_t callid;
194 ipc_call_t call;
195 sysarg_t method;
196 devmap_handle_t dh;
197 int retval;
198 int dev_addr, i;
199
200 /* Get the device handle. */
201 dh = IPC_GET_ARG1(*icall);
202
203 /* Determine which disk device is the client connecting to. */
204 dev_addr = -1;
205 for (i = 0; i < ADB_MAX_ADDR; i++) {
206 if (adb_dev[i].devmap_handle == dh)
207 dev_addr = i;
208 }
209
210 if (dev_addr < 0) {
211 ipc_answer_0(iid, EINVAL);
212 return;
213 }
214
215 /* Answer the IPC_M_CONNECT_ME_TO call. */
216 ipc_answer_0(iid, EOK);
217
218 while (1) {
219 callid = async_get_call(&call);
220 method = IPC_GET_IMETHOD(call);
221 switch (method) {
222 case IPC_M_PHONE_HUNGUP:
223 /* The other side has hung up. */
224 ipc_answer_0(callid, EOK);
225 return;
226 case IPC_M_CONNECT_TO_ME:
227 if (adb_dev[dev_addr].client_phone != -1) {
228 retval = ELIMIT;
229 break;
230 }
231 adb_dev[dev_addr].client_phone = IPC_GET_ARG5(call);
232 /*
233 * A hack so that we send the data to the phone
234 * regardless of which address the device is on.
235 */
236 for (i = 0; i < ADB_MAX_ADDR; ++i) {
237 if (adb_dev[i].devmap_handle == dh) {
238 adb_dev[i].client_phone = IPC_GET_ARG5(call);
239 }
240 }
241 retval = 0;
242 break;
243 default:
244 retval = EINVAL;
245 break;
246 }
247 ipc_answer_0(callid, retval);
248 }
249}
250
251
252static int cuda_init(void)
253{
254 if (sysinfo_get_value("cuda.address.physical", &(instance->cuda_physical)) != EOK)
255 return -1;
256
257 if (sysinfo_get_value("cuda.address.kernel", &(instance->cuda_kernel)) != EOK)
258 return -1;
259
260 void *vaddr;
261 if (pio_enable((void *) instance->cuda_physical, sizeof(cuda_t), &vaddr) != 0)
262 return -1;
263
264 dev = vaddr;
265
266 instance->cuda = dev;
267 instance->xstate = cx_listen;
268 instance->bidx = 0;
269 instance->snd_bytes = 0;
270
271 fibril_mutex_initialize(&instance->dev_lock);
272
273 /* Disable all interrupts from CUDA. */
274 pio_write_8(&dev->ier, IER_CLR | ALL_INT);
275
276 cuda_irq_code.cmds[0].addr = (void *) &((cuda_t *) instance->cuda_kernel)->ifr;
277 async_set_interrupt_received(cuda_irq_handler);
278 ipc_register_irq(10, device_assign_devno(), 0, &cuda_irq_code);
279
280 /* Enable SR interrupt. */
281 pio_write_8(&dev->ier, TIP | TREQ);
282 pio_write_8(&dev->ier, IER_SET | SR_INT);
283
284 /* Enable ADB autopolling. */
285 cuda_autopoll_set(true);
286
287 return 0;
288}
289
290static void cuda_irq_handler(ipc_callid_t iid, ipc_call_t *call)
291{
292 uint8_t rbuf[CUDA_RCV_BUF_SIZE];
293 size_t len;
294 bool handle;
295
296 handle = false;
297 len = 0;
298
299 fibril_mutex_lock(&instance->dev_lock);
300
301 /* Lower IFR.SR_INT so that CUDA can generate next int by raising it. */
302 pio_write_8(&instance->cuda->ifr, SR_INT);
303
304 switch (instance->xstate) {
305 case cx_listen: cuda_irq_listen(); break;
306 case cx_receive: cuda_irq_receive(); break;
307 case cx_rcv_end: cuda_irq_rcv_end(rbuf, &len);
308 handle = true; break;
309 case cx_send_start: cuda_irq_send_start(); break;
310 case cx_send: cuda_irq_send(); break;
311 }
312
313 fibril_mutex_unlock(&instance->dev_lock);
314
315 /* Handle an incoming packet. */
316 if (handle)
317 cuda_packet_handle(rbuf, len);
318}
319
320/** Interrupt in listen state.
321 *
322 * Start packet reception.
323 */
324static void cuda_irq_listen(void)
325{
326 uint8_t b;
327
328 b = pio_read_8(&dev->b);
329
330 if ((b & TREQ) != 0) {
331 printf("cuda_irq_listen: no TREQ?!\n");
332 return;
333 }
334
335 pio_read_8(&dev->sr);
336 pio_write_8(&dev->b, pio_read_8(&dev->b) & ~TIP);
337 instance->xstate = cx_receive;
338}
339
340/** Interrupt in receive state.
341 *
342 * Receive next byte of packet.
343 */
344static void cuda_irq_receive(void)
345{
346 uint8_t b, data;
347
348 data = pio_read_8(&dev->sr);
349 if (instance->bidx < CUDA_RCV_BUF_SIZE)
350 instance->rcv_buf[instance->bidx++] = data;
351
352 b = pio_read_8(&dev->b);
353
354 if ((b & TREQ) == 0) {
355 pio_write_8(&dev->b, b ^ TACK);
356 } else {
357 pio_write_8(&dev->b, b | TACK | TIP);
358 instance->xstate = cx_rcv_end;
359 }
360}
361
362/** Interrupt in rcv_end state.
363 *
364 * Terminate packet reception. Either go back to listen state or start
365 * receiving another packet if CUDA has one for us.
366 */
367static void cuda_irq_rcv_end(void *buf, size_t *len)
368{
369 uint8_t data, b;
370
371 b = pio_read_8(&dev->b);
372 data = pio_read_8(&dev->sr);
373
374 if ((b & TREQ) == 0) {
375 instance->xstate = cx_receive;
376 pio_write_8(&dev->b, b & ~TIP);
377 } else {
378 instance->xstate = cx_listen;
379 cuda_send_start();
380 }
381
382 memcpy(buf, instance->rcv_buf, instance->bidx);
383 *len = instance->bidx;
384 instance->bidx = 0;
385}
386
387/** Interrupt in send_start state.
388 *
389 * Process result of sending first byte (and send second on success).
390 */
391static void cuda_irq_send_start(void)
392{
393 uint8_t b;
394
395 b = pio_read_8(&dev->b);
396
397 if ((b & TREQ) == 0) {
398 /* Collision */
399 pio_write_8(&dev->acr, pio_read_8(&dev->acr) & ~SR_OUT);
400 pio_read_8(&dev->sr);
401 pio_write_8(&dev->b, pio_read_8(&dev->b) | TIP | TACK);
402 instance->xstate = cx_listen;
403 return;
404 }
405
406 pio_write_8(&dev->sr, instance->snd_buf[1]);
407 pio_write_8(&dev->b, pio_read_8(&dev->b) ^ TACK);
408 instance->bidx = 2;
409
410 instance->xstate = cx_send;
411}
412
413/** Interrupt in send state.
414 *
415 * Send next byte or terminate transmission.
416 */
417static void cuda_irq_send(void)
418{
419 if (instance->bidx < instance->snd_bytes) {
420 /* Send next byte. */
421 pio_write_8(&dev->sr, instance->snd_buf[instance->bidx++]);
422 pio_write_8(&dev->b, pio_read_8(&dev->b) ^ TACK);
423 return;
424 }
425
426 /* End transfer. */
427 instance->snd_bytes = 0;
428 instance->bidx = 0;
429
430 pio_write_8(&dev->acr, pio_read_8(&dev->acr) & ~SR_OUT);
431 pio_read_8(&dev->sr);
432 pio_write_8(&dev->b, pio_read_8(&dev->b) | TACK | TIP);
433
434 instance->xstate = cx_listen;
435 /* TODO: Match reply with request. */
436}
437
438static void cuda_packet_handle(uint8_t *data, size_t len)
439{
440 if (data[0] != PT_ADB)
441 return;
442 if (len < 2)
443 return;
444
445 adb_packet_handle(data + 2, len - 2, (data[1] & 0x40) != 0);
446}
447
448static void adb_packet_handle(uint8_t *data, size_t size, bool autopoll)
449{
450 uint8_t dev_addr;
451 uint8_t reg_no;
452 uint16_t reg_val;
453 unsigned i;
454
455 dev_addr = data[0] >> 4;
456 reg_no = data[0] & 0x03;
457
458 if (size != 3) {
459 printf("unrecognized packet, size=%d\n", size);
460 for (i = 0; i < size; ++i) {
461 printf(" 0x%02x", data[i]);
462 }
463 putchar('\n');
464 return;
465 }
466
467 if (reg_no != 0) {
468 printf("unrecognized packet, size=%d\n", size);
469 for (i = 0; i < size; ++i) {
470 printf(" 0x%02x", data[i]);
471 }
472 putchar('\n');
473 return;
474 }
475
476 reg_val = ((uint16_t) data[1] << 8) | (uint16_t) data[2];
477
478 if (adb_dev[dev_addr].client_phone == -1)
479 return;
480
481 async_msg_1(adb_dev[dev_addr].client_phone, ADB_REG_NOTIF, reg_val);
482}
483
484static void cuda_autopoll_set(bool enable)
485{
486 instance->snd_buf[0] = PT_CUDA;
487 instance->snd_buf[1] = CPT_AUTOPOLL;
488 instance->snd_buf[2] = enable ? 0x01 : 0x00;
489 instance->snd_bytes = 3;
490 instance->bidx = 0;
491
492 cuda_send_start();
493}
494
495static void cuda_send_start(void)
496{
497 cuda_t *dev = instance->cuda;
498
499 assert(instance->xstate == cx_listen);
500
501 if (instance->snd_bytes == 0)
502 return;
503
504 /* Check for incoming data. */
505 if ((pio_read_8(&dev->b) & TREQ) == 0)
506 return;
507
508 pio_write_8(&dev->acr, pio_read_8(&dev->acr) | SR_OUT);
509 pio_write_8(&dev->sr, instance->snd_buf[0]);
510 pio_write_8(&dev->b, pio_read_8(&dev->b) & ~TIP);
511
512 instance->xstate = cx_send_start;
513}
514
515
516/** @}
517 */
Note: See TracBrowser for help on using the repository browser.