Root/tools/lib/atusb.c

Source at commit e2b2df2e315c6b0338aed53dffd1dce9cae8196e created 9 years 13 days ago.
By Werner Almesberger, Made the libatspi API driver-agnostic.
1/*
2 * lib/atusb.c - ATSPI 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 "atspi/ep0.h"
19#include "atspi/usb-ids.h"
20
21#include "driver.h"
22
23
24#define FROM_DEV ATSPI_FROM_DEV(0)
25#define TO_DEV ATSPI_TO_DEV(0)
26
27
28/* ----- error handling ---------------------------------------------------- */
29
30
31static int error;
32
33
34int atusb_error(void)
35{
36    return error;
37}
38
39
40int atusb_clear_error(void)
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, ATSPI_RESET, 0, 0, NULL, 0, 1000);
87    if (res < 0) {
88        fprintf(stderr, "ATSPI_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, ATSPI_RF_RESET, 0, 0, NULL, 0, 1000);
104    if (res < 0) {
105        fprintf(stderr, "ATSPI_RF_RESET: %d\n", res);
106        error = 1;
107    }
108}
109
110
111/* ----- register access --------------------------------------------------- */
112
113
114static void atusb_reg_write(void *dsc, uint8_t reg, uint8_t value)
115{
116    usb_dev_handle *dev = dsc;
117    int res;
118
119    if (error)
120        return;
121
122    res = usb_control_msg(dev, TO_DEV, ATSPI_REG_WRITE, value, reg,
123        NULL, 0, 1000);
124    if (res < 0) {
125        fprintf(stderr, "ATSPI_REG_WRITE: %d\n", res);
126        error = 1;
127    }
128}
129
130
131static uint8_t atusb_reg_read(void *dsc, uint8_t reg)
132{
133    usb_dev_handle *dev = dsc;
134    uint8_t value = 0;
135    int res;
136
137    if (error)
138        return 0;
139
140    res = usb_control_msg(dev, FROM_DEV, ATSPI_REG_READ, 0, reg,
141        (void *) &value, 1, 1000);
142    if (res < 0) {
143        fprintf(stderr, "ATSPI_REG_READ: %d\n", res);
144        error = 1;
145    }
146    return value;
147}
148
149
150/* ----- frame buffer access ----------------------------------------------- */
151
152
153static void atusb_buf_write(void *dsc, const void *buf, int size)
154{
155    usb_dev_handle *dev = dsc;
156    int res;
157
158    if (error)
159        return;
160
161    res = usb_control_msg(dev, TO_DEV, ATSPI_BUF_WRITE, 0, 0,
162        (void *) buf, size, 1000);
163    if (res < 0) {
164        fprintf(stderr, "ATSPI_BUF_WRITE: %d\n", res);
165        error = 1;
166    }
167
168}
169
170
171static int atusb_buf_read(void *dsc, void *buf, int size)
172{
173    usb_dev_handle *dev = dsc;
174    int res;
175
176    if (error)
177        return -1;
178
179    res = usb_control_msg(dev, FROM_DEV, ATSPI_BUF_READ, 0, 0,
180        buf, size, 1000);
181    if (res < 0) {
182        fprintf(stderr, "ATSPI_BUF_READ: %d\n", res);
183        error = 1;
184    }
185
186    return res;
187}
188
189
190/* ----- driver interface -------------------------------------------------- */
191
192
193struct atspi_driver atusb_driver = {
194    .name = "atusb",
195    .open = atusb_open,
196    .close = atusb_close,
197    .reset = atusb_reset,
198    .reset_rf = atusb_reset_rf,
199    .reg_write = atusb_reg_write,
200    .reg_read = atusb_reg_read,
201    .buf_write = atusb_buf_write,
202    .buf_read = atusb_buf_read,
203};
204

Archive Download this file



interactive