source: mainline/uspace/drv/bus/adb/cuda_adb/cuda_adb.c@ 76f566d

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

Prefer handle over ID in naming handle variables

  • Property mode set to 100644
File size: 11.5 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 <assert.h>
40#include <ddf/driver.h>
41#include <ddf/log.h>
42#include <ddi.h>
43#include <errno.h>
44#include <ipc/adb.h>
45#include <libarch/ddi.h>
46#include <stdbool.h>
47#include <stddef.h>
48#include <stdint.h>
49#include <stdio.h>
50#include <stdlib.h>
51
52#include "cuda_adb.h"
53#include "cuda_hw.h"
54
55#define NAME "cuda_adb"
56
57static void cuda_dev_connection(cap_call_handle_t, ipc_call_t *, void *);
58static errno_t cuda_init(cuda_t *);
59static void cuda_irq_handler(ipc_call_t *, void *);
60
61static void cuda_irq_listen(cuda_t *);
62static void cuda_irq_receive(cuda_t *);
63static void cuda_irq_rcv_end(cuda_t *, void *, size_t *);
64static void cuda_irq_send_start(cuda_t *);
65static void cuda_irq_send(cuda_t *);
66
67static void cuda_packet_handle(cuda_t *, uint8_t *, size_t);
68static void cuda_send_start(cuda_t *);
69static void cuda_autopoll_set(cuda_t *, bool);
70
71static void adb_packet_handle(cuda_t *, uint8_t *, size_t, bool);
72
73static irq_pio_range_t cuda_ranges[] = {
74 {
75 .base = 0,
76 .size = sizeof(cuda_regs_t)
77 }
78};
79
80static irq_cmd_t cuda_cmds[] = {
81 {
82 .cmd = CMD_PIO_READ_8,
83 .addr = NULL,
84 .dstarg = 1
85 },
86 {
87 .cmd = CMD_AND,
88 .value = SR_INT,
89 .srcarg = 1,
90 .dstarg = 2
91 },
92 {
93 .cmd = CMD_PREDICATE,
94 .value = 1,
95 .srcarg = 2
96 },
97 {
98 .cmd = CMD_ACCEPT
99 }
100};
101
102
103static irq_code_t cuda_irq_code = {
104 sizeof(cuda_ranges) / sizeof(irq_pio_range_t),
105 cuda_ranges,
106 sizeof(cuda_cmds) / sizeof(irq_cmd_t),
107 cuda_cmds
108};
109
110static errno_t cuda_dev_create(cuda_t *cuda, const char *name, const char *id,
111 adb_dev_t **rdev)
112{
113 adb_dev_t *dev = NULL;
114 ddf_fun_t *fun;
115 errno_t rc;
116
117 fun = ddf_fun_create(cuda->dev, fun_inner, name);
118 if (fun == NULL) {
119 ddf_msg(LVL_ERROR, "Failed creating function '%s'.", name);
120 rc = ENOMEM;
121 goto error;
122 }
123
124 rc = ddf_fun_add_match_id(fun, id, 10);
125 if (rc != EOK) {
126 ddf_msg(LVL_ERROR, "Failed adding match ID.");
127 rc = ENOMEM;
128 goto error;
129 }
130
131 dev = ddf_fun_data_alloc(fun, sizeof(adb_dev_t));
132 if (dev == NULL) {
133 ddf_msg(LVL_ERROR, "Failed allocating memory for '%s'.", name);
134 rc = ENOMEM;
135 goto error;
136 }
137
138 dev->fun = fun;
139 list_append(&dev->lcuda, &cuda->devs);
140
141 ddf_fun_set_conn_handler(fun, cuda_dev_connection);
142
143 rc = ddf_fun_bind(fun);
144 if (rc != EOK) {
145 ddf_msg(LVL_ERROR, "Failed binding function '%s'.", name);
146 goto error;
147 }
148
149 *rdev = dev;
150 return EOK;
151error:
152 if (fun != NULL)
153 ddf_fun_destroy(fun);
154 return rc;
155}
156
157errno_t cuda_add(cuda_t *cuda, cuda_res_t *res)
158{
159 adb_dev_t *kbd = NULL;
160 adb_dev_t *mouse = NULL;
161 errno_t rc;
162
163 cuda->phys_base = res->base;
164
165 rc = cuda_dev_create(cuda, "kbd", "adb/keyboard", &kbd);
166 if (rc != EOK)
167 goto error;
168
169 rc = cuda_dev_create(cuda, "mouse", "adb/mouse", &mouse);
170 if (rc != EOK)
171 goto error;
172
173 cuda->addr_dev[2] = kbd;
174 cuda->addr_dev[8] = kbd;
175
176 cuda->addr_dev[9] = mouse;
177
178 rc = cuda_init(cuda);
179 if (rc != EOK) {
180 ddf_msg(LVL_ERROR, "Failed initializing CUDA hardware.");
181 return rc;
182 }
183
184 return EOK;
185error:
186 return rc;
187}
188
189errno_t cuda_remove(cuda_t *cuda)
190{
191 return ENOTSUP;
192}
193
194errno_t cuda_gone(cuda_t *cuda)
195{
196 return ENOTSUP;
197}
198
199/** Device connection handler */
200static void cuda_dev_connection(cap_call_handle_t icall_handle,
201 ipc_call_t *icall, void *arg)
202{
203 adb_dev_t *dev = (adb_dev_t *) ddf_fun_data_get((ddf_fun_t *) arg);
204 cap_call_handle_t chandle;
205 ipc_call_t call;
206 sysarg_t method;
207
208 /* Answer the IPC_M_CONNECT_ME_TO call. */
209 async_answer_0(icall_handle, EOK);
210
211 while (true) {
212 chandle = async_get_call(&call);
213 method = IPC_GET_IMETHOD(call);
214
215 if (!method) {
216 /* The other side has hung up. */
217 async_answer_0(chandle, EOK);
218 return;
219 }
220
221 async_sess_t *sess =
222 async_callback_receive_start(EXCHANGE_SERIALIZE, &call);
223 if (sess != NULL) {
224 dev->client_sess = sess;
225 async_answer_0(chandle, EOK);
226 } else {
227 async_answer_0(chandle, EINVAL);
228 }
229 }
230}
231
232static errno_t cuda_init(cuda_t *cuda)
233{
234 errno_t rc;
235
236 void *vaddr;
237 rc = pio_enable((void *) cuda->phys_base, sizeof(cuda_regs_t),
238 &vaddr);
239 if (rc != EOK)
240 return rc;
241
242 cuda->regs = vaddr;
243 cuda->xstate = cx_listen;
244 cuda->bidx = 0;
245 cuda->snd_bytes = 0;
246
247 fibril_mutex_initialize(&cuda->dev_lock);
248
249 /* Disable all interrupts from CUDA. */
250 pio_write_8(&cuda->regs->ier, IER_CLR | ALL_INT);
251
252 cuda_irq_code.ranges[0].base = (uintptr_t) cuda->phys_base;
253 cuda_irq_code.cmds[0].addr = (void *) &((cuda_regs_t *)
254 cuda->phys_base)->ifr;
255 async_irq_subscribe(10, cuda_irq_handler, cuda, &cuda_irq_code, NULL);
256
257 /* Enable SR interrupt. */
258 pio_write_8(&cuda->regs->ier, TIP | TREQ);
259 pio_write_8(&cuda->regs->ier, IER_SET | SR_INT);
260
261 /* Enable ADB autopolling. */
262 cuda_autopoll_set(cuda, true);
263
264 return EOK;
265}
266
267static void cuda_irq_handler(ipc_call_t *call, void *arg)
268{
269 uint8_t rbuf[CUDA_RCV_BUF_SIZE];
270 cuda_t *cuda = (cuda_t *)arg;
271 size_t len;
272 bool handle;
273
274 handle = false;
275 len = 0;
276
277 fibril_mutex_lock(&cuda->dev_lock);
278
279 switch (cuda->xstate) {
280 case cx_listen:
281 cuda_irq_listen(cuda);
282 break;
283 case cx_receive:
284 cuda_irq_receive(cuda);
285 break;
286 case cx_rcv_end:
287 cuda_irq_rcv_end(cuda, rbuf, &len);
288 handle = true;
289 break;
290 case cx_send_start:
291 cuda_irq_send_start(cuda);
292 break;
293 case cx_send:
294 cuda_irq_send(cuda);
295 break;
296 }
297
298 /* Lower IFR.SR_INT so that CUDA can generate next int by raising it. */
299 pio_write_8(&cuda->regs->ifr, SR_INT);
300
301 fibril_mutex_unlock(&cuda->dev_lock);
302
303 /* Handle an incoming packet. */
304 if (handle)
305 cuda_packet_handle(cuda, rbuf, len);
306}
307
308/** Interrupt in listen state.
309 *
310 * Start packet reception.
311 *
312 * @param cuda CUDA instance
313 */
314static void cuda_irq_listen(cuda_t *cuda)
315{
316 uint8_t b = pio_read_8(&cuda->regs->b);
317
318 if ((b & TREQ) != 0) {
319 ddf_msg(LVL_WARN, "cuda_irq_listen: no TREQ?!");
320 return;
321 }
322
323 pio_write_8(&cuda->regs->b, b & ~TIP);
324 cuda->xstate = cx_receive;
325}
326
327/** Interrupt in receive state.
328 *
329 * Receive next byte of packet.
330 *
331 * @param cuda CUDA instance
332 */
333static void cuda_irq_receive(cuda_t *cuda)
334{
335 uint8_t data = pio_read_8(&cuda->regs->sr);
336 if (cuda->bidx < CUDA_RCV_BUF_SIZE)
337 cuda->rcv_buf[cuda->bidx++] = data;
338
339 uint8_t b = pio_read_8(&cuda->regs->b);
340
341 if ((b & TREQ) == 0) {
342 pio_write_8(&cuda->regs->b, b ^ TACK);
343 } else {
344 pio_write_8(&cuda->regs->b, b | TACK | TIP);
345 cuda->xstate = cx_rcv_end;
346 }
347}
348
349/** Interrupt in rcv_end state.
350 *
351 * Terminate packet reception. Either go back to listen state or start
352 * receiving another packet if CUDA has one for us.
353 *
354 * @param cuda CUDA instance
355 * @param buf Buffer for storing received packet
356 * @param len Place to store length of received packet
357 */
358static void cuda_irq_rcv_end(cuda_t *cuda, void *buf, size_t *len)
359{
360 uint8_t b = pio_read_8(&cuda->regs->b);
361
362 if ((b & TREQ) == 0) {
363 cuda->xstate = cx_receive;
364 pio_write_8(&cuda->regs->b, b & ~TIP);
365 } else {
366 cuda->xstate = cx_listen;
367 cuda_send_start(cuda);
368 }
369
370 memcpy(buf, cuda->rcv_buf, cuda->bidx);
371 *len = cuda->bidx;
372 cuda->bidx = 0;
373}
374
375/** Interrupt in send_start state.
376 *
377 * Process result of sending first byte (and send second on success).
378 *
379 * @param cuda CUDA instance
380 */
381static void cuda_irq_send_start(cuda_t *cuda)
382{
383 uint8_t b;
384
385 b = pio_read_8(&cuda->regs->b);
386
387 if ((b & TREQ) == 0) {
388 /* Collision */
389 pio_write_8(&cuda->regs->acr, pio_read_8(&cuda->regs->acr) &
390 ~SR_OUT);
391 pio_read_8(&cuda->regs->sr);
392 pio_write_8(&cuda->regs->b, pio_read_8(&cuda->regs->b) |
393 TIP | TACK);
394 cuda->xstate = cx_listen;
395 return;
396 }
397
398 pio_write_8(&cuda->regs->sr, cuda->snd_buf[1]);
399 pio_write_8(&cuda->regs->b, pio_read_8(&cuda->regs->b) ^ TACK);
400 cuda->bidx = 2;
401
402 cuda->xstate = cx_send;
403}
404
405/** Interrupt in send state.
406 *
407 * Send next byte or terminate transmission.
408 *
409 * @param cuda CUDA instance
410 */
411static void cuda_irq_send(cuda_t *cuda)
412{
413 if (cuda->bidx < cuda->snd_bytes) {
414 /* Send next byte. */
415 pio_write_8(&cuda->regs->sr,
416 cuda->snd_buf[cuda->bidx++]);
417 pio_write_8(&cuda->regs->b, pio_read_8(&cuda->regs->b) ^ TACK);
418 return;
419 }
420
421 /* End transfer. */
422 cuda->snd_bytes = 0;
423 cuda->bidx = 0;
424
425 pio_write_8(&cuda->regs->acr, pio_read_8(&cuda->regs->acr) & ~SR_OUT);
426 pio_read_8(&cuda->regs->sr);
427 pio_write_8(&cuda->regs->b, pio_read_8(&cuda->regs->b) | TACK | TIP);
428
429 cuda->xstate = cx_listen;
430 /* TODO: Match reply with request. */
431}
432
433static void cuda_packet_handle(cuda_t *cuda, uint8_t *data, size_t len)
434{
435 if (data[0] != PT_ADB)
436 return;
437 if (len < 2)
438 return;
439
440 adb_packet_handle(cuda, data + 2, len - 2, (data[1] & 0x40) != 0);
441}
442
443static void adb_packet_handle(cuda_t *cuda, uint8_t *data, size_t size,
444 bool autopoll)
445{
446 uint8_t dev_addr;
447 uint8_t reg_no;
448 uint16_t reg_val;
449 adb_dev_t *dev;
450 unsigned i;
451
452 dev_addr = data[0] >> 4;
453 reg_no = data[0] & 0x03;
454
455 if (size != 3) {
456 ddf_msg(LVL_WARN, "Unrecognized packet, size=%zu", size);
457 for (i = 0; i < size; ++i) {
458 ddf_msg(LVL_WARN, " 0x%02x", data[i]);
459 }
460 return;
461 }
462
463 if (reg_no != 0) {
464 ddf_msg(LVL_WARN, "Unrecognized packet, size=%zu", size);
465 for (i = 0; i < size; ++i) {
466 ddf_msg(LVL_WARN, " 0x%02x", data[i]);
467 }
468 return;
469 }
470
471 reg_val = ((uint16_t) data[1] << 8) | (uint16_t) data[2];
472
473 ddf_msg(LVL_DEBUG, "Received ADB packet for device address %d",
474 dev_addr);
475 dev = cuda->addr_dev[dev_addr];
476 if (dev == NULL)
477 return;
478
479 async_exch_t *exch = async_exchange_begin(dev->client_sess);
480 async_msg_1(exch, ADB_REG_NOTIF, reg_val);
481 async_exchange_end(exch);
482}
483
484static void cuda_autopoll_set(cuda_t *cuda, bool enable)
485{
486 cuda->snd_buf[0] = PT_CUDA;
487 cuda->snd_buf[1] = CPT_AUTOPOLL;
488 cuda->snd_buf[2] = enable ? 0x01 : 0x00;
489 cuda->snd_bytes = 3;
490 cuda->bidx = 0;
491
492 cuda_send_start(cuda);
493}
494
495static void cuda_send_start(cuda_t *cuda)
496{
497 assert(cuda->xstate == cx_listen);
498
499 if (cuda->snd_bytes == 0)
500 return;
501
502 /* Check for incoming data. */
503 if ((pio_read_8(&cuda->regs->b) & TREQ) == 0)
504 return;
505
506 pio_write_8(&cuda->regs->acr, pio_read_8(&cuda->regs->acr) | SR_OUT);
507 pio_write_8(&cuda->regs->sr, cuda->snd_buf[0]);
508 pio_write_8(&cuda->regs->b, pio_read_8(&cuda->regs->b) & ~TIP);
509
510 cuda->xstate = cx_send_start;
511}
512
513/** @}
514 */
Note: See TracBrowser for help on using the repository browser.