]>
Commit | Line | Data |
---|---|---|
a1bb33af | 1 | /* |
fed16f06 UH |
2 | * This file is part of the sigrok project. |
3 | * | |
4 | * Copyright (C) 2010 Sven Peter <sven@fail0verflow.com> | |
5 | * Copyright (C) 2010 Haxx Enterprises <bushing@gmail.com> | |
6 | * All rights reserved. | |
7 | * | |
8 | * Redistribution and use in source and binary forms, with or | |
9 | * without modification, are permitted provided that the following | |
10 | * conditions are met: | |
11 | * | |
12 | * * Redistributions of source code must retain the above copyright notice, | |
13 | * this list of conditions and the following disclaimer. | |
14 | * | |
15 | * * Redistributions in binary form must reproduce the above copyright notice, | |
16 | * this list of conditions and the following disclaimer in the documentation | |
17 | * and/or other materials provided with the distribution. | |
18 | * | |
19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | |
23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | |
29 | * THE POSSIBILITY OF SUCH DAMAGE. | |
30 | */ | |
31 | ||
a1bb33af UH |
32 | #include <libusb.h> |
33 | #include <stdio.h> | |
34 | #include "gl_usb.h" | |
35 | ||
fed16f06 UH |
36 | #define CTRL_IN (LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_IN | \ |
37 | LIBUSB_RECIPIENT_INTERFACE) | |
38 | #define CTRL_OUT (LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_OUT | \ | |
39 | LIBUSB_RECIPIENT_INTERFACE) | |
40 | ||
41 | const int PACKET_CTRL_LEN = 2; | |
42 | const int PACKET_INT_LEN = 2; | |
43 | const int PACKET_BULK_LEN = 64; | |
44 | const int INTERFACE = 0; | |
45 | const int ENDPOINT_INT_IN = 0x81; /* Endpoint 0x81 address for IN */ | |
46 | const int ENDPOINT_INT_OUT = 0x01; /* Endpoint 1 address for OUT */ | |
47 | const int ENDPOINT_BULK_IN = 0x81; /* Endpoint 0x81 address for IN */ | |
48 | const int ENDPOINT_BULK_OUT = 0x02; /* Endpoint 1 address for OUT */ | |
49 | const int TIMEOUT = 5000; /* Timeout in ms */ | |
a1bb33af UH |
50 | |
51 | enum { | |
52 | REQ_READBULK = 0x82, | |
53 | REQ_WRITEADDR, | |
54 | REQ_READDATA, | |
55 | REQ_WRITEDATA, | |
56 | }; | |
57 | ||
58 | static struct libusb_device_handle *g_devh = NULL; | |
59 | ||
60 | int gl_write_address(libusb_device_handle *devh, unsigned int address) | |
61 | { | |
fed16f06 UH |
62 | unsigned char packet[8] = { address & 0xFF }; |
63 | int ret; | |
64 | ||
65 | ret = libusb_control_transfer(devh, CTRL_OUT, 0xc, REQ_WRITEADDR, | |
66 | 0, packet, 1, TIMEOUT); | |
67 | if (ret != 1) | |
68 | printf("%s: libusb_control_transfer returned %d\n", | |
69 | __FUNCTION__, ret); | |
70 | return ret; | |
a1bb33af UH |
71 | } |
72 | ||
73 | int gl_write_data(libusb_device_handle *devh, unsigned int val) | |
74 | { | |
fed16f06 UH |
75 | unsigned char packet[8] = { val & 0xFF }; |
76 | int ret; | |
77 | ||
78 | ret = libusb_control_transfer(devh, CTRL_OUT, 0xc, REQ_WRITEDATA, | |
79 | 0, packet, 1, TIMEOUT); | |
80 | if (ret != 1) | |
81 | printf("%s: libusb_control_transfer returned %d\n", | |
82 | __FUNCTION__, ret); | |
83 | return ret; | |
a1bb33af UH |
84 | } |
85 | ||
86 | int gl_read_data(libusb_device_handle *devh) | |
87 | { | |
fed16f06 UH |
88 | unsigned char packet[8] = { 0 }; |
89 | int ret; | |
90 | ||
91 | ret = libusb_control_transfer(devh, CTRL_IN, 0xc, REQ_READDATA, | |
92 | 0, packet, 1, TIMEOUT); | |
93 | if (ret != 1) | |
94 | printf("%s: libusb_control_transfer returned %d, val=%hhx\n", | |
95 | __FUNCTION__, ret, packet[0]); | |
96 | return (ret == 1) ? packet[0] : ret; | |
a1bb33af UH |
97 | } |
98 | ||
99 | int gl_read_bulk(libusb_device_handle *devh, void *buffer, unsigned int size) | |
100 | { | |
fed16f06 UH |
101 | unsigned char packet[8] = |
102 | { 0, 0, 0, 0, size & 0xff, (size & 0xff00) >> 8, | |
103 | (size & 0xff0000) >> 16, (size & 0xff000000) >> 24 }; | |
104 | int ret, transferred = 0; | |
105 | ||
106 | ret = libusb_control_transfer(devh, CTRL_OUT, 0x4, REQ_READBULK, | |
107 | 0, packet, 8, TIMEOUT); | |
108 | if (ret != 8) | |
109 | printf("%s: libusb_control_transfer returned %d\n", | |
110 | __FUNCTION__, ret); | |
111 | ||
112 | ret = libusb_bulk_transfer(devh, ENDPOINT_BULK_IN, buffer, size, | |
113 | &transferred, TIMEOUT); | |
114 | if (ret < 0) | |
115 | fprintf(stderr, "Bulk read error %d\n", ret); | |
a1bb33af UH |
116 | return transferred; |
117 | } | |
118 | ||
fed16f06 UH |
119 | int gl_reg_write(libusb_device_handle *devh, unsigned int reg, |
120 | unsigned int val) | |
a1bb33af | 121 | { |
fed16f06 UH |
122 | int ret; |
123 | ||
124 | ret = gl_write_address(devh, reg); | |
125 | if (ret < 0) | |
126 | return ret; | |
127 | ret = gl_write_data(devh, val); | |
128 | return ret; | |
a1bb33af UH |
129 | } |
130 | ||
131 | int gl_reg_read(libusb_device_handle *devh, unsigned int reg) | |
132 | { | |
fed16f06 UH |
133 | int ret; |
134 | ||
135 | ret = gl_write_address(devh, reg); | |
136 | if (ret < 0) | |
137 | return ret; | |
138 | ret = gl_read_data(devh); | |
139 | return ret; | |
a1bb33af UH |
140 | } |
141 | ||
142 | int gl_open(int vid) | |
143 | { | |
fed16f06 UH |
144 | int ret; |
145 | struct libusb_device **devs; | |
146 | struct libusb_device *dev; | |
147 | size_t i = 0; | |
148 | struct libusb_device_descriptor desc; | |
a1bb33af | 149 | |
fed16f06 UH |
150 | ret = libusb_init(NULL); |
151 | if (ret < 0) | |
a1bb33af UH |
152 | return GL_ELIBUSB; |
153 | ||
a1bb33af | 154 | if (libusb_get_device_list(NULL, &devs) < 0) { |
fed16f06 | 155 | ret = GL_EOPEN; |
a1bb33af UH |
156 | goto gl_open_error; |
157 | } | |
158 | ||
159 | while ((dev = devs[i++]) != NULL) { | |
a1bb33af UH |
160 | if (libusb_get_device_descriptor(dev, &desc) < 0) |
161 | break; | |
162 | ||
163 | if (desc.idVendor == vid) { | |
164 | if (libusb_open(dev, &g_devh) < 0) | |
165 | g_devh = NULL; | |
166 | break; | |
167 | } | |
168 | } | |
169 | ||
170 | libusb_free_device_list(devs, 1); | |
171 | ||
172 | if (!g_devh) { | |
fed16f06 | 173 | ret = GL_EOPEN; |
a1bb33af UH |
174 | goto gl_open_error; |
175 | } | |
176 | ||
fed16f06 UH |
177 | ret = libusb_set_configuration(g_devh, 1); |
178 | if (ret < 0) { | |
179 | ret = GL_ESETCONFIG; | |
a1bb33af UH |
180 | goto gl_open_error; |
181 | } | |
182 | ||
fed16f06 UH |
183 | ret = libusb_claim_interface(g_devh, 0); |
184 | if (ret < 0) { | |
185 | ret = GL_ECLAIM; | |
a1bb33af UH |
186 | goto gl_open_error; |
187 | } | |
188 | ||
189 | return GL_OK; | |
190 | ||
191 | gl_open_error: | |
192 | gl_close(); | |
fed16f06 | 193 | return ret; |
a1bb33af UH |
194 | } |
195 | ||
196 | int gl_close(void) | |
197 | { | |
198 | if (g_devh) { | |
199 | libusb_release_interface(g_devh, 0); | |
200 | libusb_reset_device(g_devh); | |
201 | libusb_close(g_devh); | |
202 | } | |
203 | libusb_exit(NULL); | |
204 | ||
205 | return 0; | |
206 | } |