Root/tools/lib/atusb.c

Source at commit 96b6a50b3a1f9f889e1a59631bc17836511aaf06 created 8 years 11 months ago.
By Werner Almesberger, The Great ATSPI Renaming, part 2: rename ATSPI_* identifiers to ATUSB_*
1/*
2 * lib/atusb.c - ATUSB access functions library (USB version)
3 *
4 * Written 2010 by Werner Almesberger
5 * Copyright 2010 Werner Almesberger
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 */
12
13
14#include <stdio.h>
15#include <usb.h>
16
17#include "f32xbase/usb.h"
18#include "atusb/ep0.h"
19#include "atusb/usb-ids.h"
20
21#include "driver.h"
22
23
24#define FROM_DEV ATUSB_FROM_DEV(0)
25#define TO_DEV ATUSB_TO_DEV(0)
26
27
28/* ----- error handling ---------------------------------------------------- */
29
30
31static int error;
32
33
34static int atusb_error(void *dsc)
35{
36    return error;
37}
38
39
40static int atusb_clear_error(void *dsc)
41{
42    int ret;
43
44    ret = error;
45    error = 0;
46    return ret;
47}
48
49
50/* ----- open/close -------------------------------------------------------- */
51
52
53static void *atusb_open(void)
54{
55    usb_dev_handle *dev;
56
57    dev = open_usb(USB_VENDOR, USB_PRODUCT);
58    if (dev) {
59        error = 0;
60    } else {
61        fprintf(stderr, ":-(\n");
62        error = 1;
63    }
64    return dev;
65}
66
67
68static void atusb_close(void *dsc)
69{
70    /* to do */
71}
72
73
74/* ----- device mode ------------------------------------------------------- */
75
76
77static void atusb_reset(void *dsc)
78{
79    usb_dev_handle *dev = dsc;
80    int res;
81
82    if (error)
83        return;
84
85    res =
86        usb_control_msg(dev, TO_DEV, ATUSB_RESET, 0, 0, NULL, 0, 1000);
87    if (res < 0) {
88        fprintf(stderr, "ATUSB_RESET: %d\n", res);
89        error = 1;
90    }
91}
92
93
94static void atusb_reset_rf(void *dsc)
95{
96    usb_dev_handle *dev = dsc;
97    int res;
98
99    if (error)
100        return;
101
102    res =
103        usb_control_msg(dev, TO_DEV, ATUSB_RF_RESET, 0, 0, NULL, 0, 1000);
104    if (res < 0) {
105        fprintf(stderr, "ATUSB_RF_RESET: %d\n", res);
106        error = 1;
107    }
108}
109
110
111static void atusb_test_mode(void *dsc)
112{
113    usb_dev_handle *dev = dsc;
114    int res;
115
116    if (error)
117        return;
118
119    res =
120        usb_control_msg(dev, TO_DEV, ATUSB_TEST, 0, 0, NULL, 0, 1000);
121    if (res < 0) {
122        fprintf(stderr, "ATUSB_TEST: %d\n", res);
123        error = 1;
124    }
125}
126
127
128/* ----- register access --------------------------------------------------- */
129
130
131static void atusb_reg_write(void *dsc, uint8_t reg, uint8_t value)
132{
133    usb_dev_handle *dev = dsc;
134    int res;
135
136    if (error)
137        return;
138
139    res = usb_control_msg(dev, TO_DEV, ATUSB_REG_WRITE, value, reg,
140        NULL, 0, 1000);
141    if (res < 0) {
142        fprintf(stderr, "ATUSB_REG_WRITE: %d\n", res);
143        error = 1;
144    }
145}
146
147
148static uint8_t atusb_reg_read(void *dsc, uint8_t reg)
149{
150    usb_dev_handle *dev = dsc;
151    uint8_t value = 0;
152    int res;
153
154    if (error)
155        return 0;
156
157    res = usb_control_msg(dev, FROM_DEV, ATUSB_REG_READ, 0, reg,
158        (void *) &value, 1, 1000);
159    if (res < 0) {
160        fprintf(stderr, "ATUSB_REG_READ: %d\n", res);
161        error = 1;
162    }
163    return value;
164}
165
166
167/* ----- frame buffer access ----------------------------------------------- */
168
169
170static void atusb_buf_write(void *dsc, const void *buf, int size)
171{
172    usb_dev_handle *dev = dsc;
173    int res;
174
175    if (error)
176        return;
177
178    res = usb_control_msg(dev, TO_DEV, ATUSB_BUF_WRITE, 0, 0,
179        (void *) buf, size, 1000);
180    if (res < 0) {
181        fprintf(stderr, "ATUSB_BUF_WRITE: %d\n", res);
182        error = 1;
183    }
184
185}
186
187
188static int atusb_buf_read(void *dsc, void *buf, int size)
189{
190    usb_dev_handle *dev = dsc;
191    int res;
192
193    if (error)
194        return -1;
195
196    res = usb_control_msg(dev, FROM_DEV, ATUSB_BUF_READ, 0, 0,
197        buf, size, 1000);
198    if (res < 0) {
199        fprintf(stderr, "ATUSB_BUF_READ: %d\n", res);
200        error = 1;
201    }
202
203    return res;
204}
205
206
207/* ----- RF interrupt ------------------------------------------------------ */
208
209
210static int atusd_interrupt(void *dsc)
211{
212    usb_dev_handle *dev = dsc;
213    uint8_t buf;
214    int res;
215
216    if (error)
217        return -1;
218    
219    res = usb_control_msg(dev, FROM_DEV, ATUSB_POLL_INT, 0, 0,
220        (void *) &buf, 1, 1000);
221    if (res < 0) {
222        fprintf(stderr, "ATUSB_POLL_INT: %d\n", res);
223        error = 1;
224    }
225
226    return buf;
227}
228
229
230/* ----- driver interface -------------------------------------------------- */
231
232
233struct atspi_driver atusb_driver = {
234    .name = "USB",
235    .open = atusb_open,
236    .close = atusb_close,
237    .error = atusb_error,
238    .clear_error = atusb_clear_error,
239    .reset = atusb_reset,
240    .reset_rf = atusb_reset_rf,
241    .test_mode = atusb_test_mode,
242    .reg_write = atusb_reg_write,
243    .reg_read = atusb_reg_read,
244    .buf_write = atusb_buf_write,
245    .buf_read = atusb_buf_read,
246    .interrupt = atusd_interrupt,
247};
248

Archive Download this file



interactive