Root/atrf/tools/lib/atspi.c

Source at commit 73043a52ea70185b5dcf037ffa07abaf94a36134 created 10 years 30 days ago.
By Werner Almesberger, Finished and tested TX/RX. (LQ doesn't work yet, the rest does.)
1/*
2 * lib/atspi.c - ATSPI access functions library
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 "atspi.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 atspi_error(void)
35{
36    return error;
37}
38
39
40int atspi_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
53usb_dev_handle *atspi_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
68void atspi_close(usb_dev_handle *dev)
69{
70    /* to do */
71}
72
73
74/* ----- device mode ------------------------------------------------------- */
75
76
77void atspi_reset(usb_dev_handle *dev)
78{
79    int res;
80
81    if (error)
82        return;
83
84    res =
85        usb_control_msg(dev, TO_DEV, ATSPI_RESET, 0, 0, NULL, 0, 1000);
86    if (res < 0) {
87        fprintf(stderr, "ATSPI_RESET: %d\n", res);
88        error = 1;
89    }
90}
91
92
93void atspi_reset_rf(usb_dev_handle *dev)
94{
95    int res;
96
97    if (error)
98        return;
99
100    res =
101        usb_control_msg(dev, TO_DEV, ATSPI_RF_RESET, 0, 0, NULL, 0, 1000);
102    if (res < 0) {
103        fprintf(stderr, "ATSPI_RF_RESET: %d\n", res);
104        error = 1;
105    }
106}
107
108
109/* ----- register access --------------------------------------------------- */
110
111
112void atspi_reg_write(usb_dev_handle *dev, uint8_t reg, uint8_t value)
113{
114    int res;
115
116    if (error)
117        return;
118
119    res = usb_control_msg(dev, TO_DEV, ATSPI_REG_WRITE, value, reg,
120        NULL, 0, 1000);
121    if (res < 0) {
122        fprintf(stderr, "ATSPI_REG_WRITE: %d\n", res);
123        error = 1;
124    }
125}
126
127
128uint8_t atspi_reg_read(usb_dev_handle *dev, uint8_t reg)
129{
130    uint8_t value = 0;
131    int res;
132
133    if (error)
134        return 0;
135
136    res = usb_control_msg(dev, FROM_DEV, ATSPI_REG_READ, 0, reg,
137        (void *) &value, 1, 1000);
138    if (res < 0) {
139        fprintf(stderr, "ATSPI_REG_READ: %d\n", res);
140        error = 1;
141    }
142    return value;
143}
144
145
146/* ----- frame buffer access ----------------------------------------------- */
147
148
149void atspi_buf_write(usb_dev_handle *dev, const void *buf, int size)
150{
151    int res;
152
153    if (error)
154        return;
155
156    res = usb_control_msg(dev, TO_DEV, ATSPI_BUF_WRITE, 0, 0,
157        (void *) buf, size, 1000);
158    if (res < 0) {
159        fprintf(stderr, "ATSPI_BUF_WRITE: %d\n", res);
160        error = 1;
161    }
162
163}
164
165
166int atspi_buf_read(usb_dev_handle *dev, void *buf, int size)
167{
168    int res;
169
170    if (error)
171        return -1;
172
173    res = usb_control_msg(dev, FROM_DEV, ATSPI_BUF_READ, 0, 0,
174        buf, size, 1000);
175    if (res < 0) {
176        fprintf(stderr, "ATSPI_BUF_READ: %d\n", res);
177        error = 1;
178    }
179
180    return res;
181}
182

Archive Download this file



interactive