]> sigrok.org Git - libsigrok.git/blame - hardware/zeroplus-logic-cube/gl_usb.c
sr: remove unused time/duration fields from datafeed packets
[libsigrok.git] / hardware / zeroplus-logic-cube / gl_usb.c
CommitLineData
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>
b7f09cf8 34#include "sigrok-internal.h"
a1bb33af
UH
35#include "gl_usb.h"
36
fed16f06
UH
37#define CTRL_IN (LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_IN | \
38 LIBUSB_RECIPIENT_INTERFACE)
39#define CTRL_OUT (LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_OUT | \
40 LIBUSB_RECIPIENT_INTERFACE)
41
42const int PACKET_CTRL_LEN = 2;
43const int PACKET_INT_LEN = 2;
44const int PACKET_BULK_LEN = 64;
45const int INTERFACE = 0;
46const int ENDPOINT_INT_IN = 0x81; /* Endpoint 0x81 address for IN */
47const int ENDPOINT_INT_OUT = 0x01; /* Endpoint 1 address for OUT */
48const int ENDPOINT_BULK_IN = 0x81; /* Endpoint 0x81 address for IN */
49const int ENDPOINT_BULK_OUT = 0x02; /* Endpoint 1 address for OUT */
50const int TIMEOUT = 5000; /* Timeout in ms */
a1bb33af
UH
51
52enum {
53 REQ_READBULK = 0x82,
54 REQ_WRITEADDR,
55 REQ_READDATA,
56 REQ_WRITEDATA,
57};
58
59static struct libusb_device_handle *g_devh = NULL;
60
61int gl_write_address(libusb_device_handle *devh, unsigned int address)
62{
fed16f06
UH
63 unsigned char packet[8] = { address & 0xFF };
64 int ret;
65
66 ret = libusb_control_transfer(devh, CTRL_OUT, 0xc, REQ_WRITEADDR,
67 0, packet, 1, TIMEOUT);
68 if (ret != 1)
a562c3a2 69 sr_err("%s: libusb_control_transfer returned %d\n",
6bb5c5fa 70 __func__, ret);
fed16f06 71 return ret;
a1bb33af
UH
72}
73
74int gl_write_data(libusb_device_handle *devh, unsigned int val)
75{
fed16f06
UH
76 unsigned char packet[8] = { val & 0xFF };
77 int ret;
78
79 ret = libusb_control_transfer(devh, CTRL_OUT, 0xc, REQ_WRITEDATA,
80 0, packet, 1, TIMEOUT);
81 if (ret != 1)
a562c3a2 82 sr_err("%s: libusb_control_transfer returned %d\n",
6bb5c5fa 83 __func__, ret);
fed16f06 84 return ret;
a1bb33af
UH
85}
86
87int gl_read_data(libusb_device_handle *devh)
88{
fed16f06
UH
89 unsigned char packet[8] = { 0 };
90 int ret;
91
92 ret = libusb_control_transfer(devh, CTRL_IN, 0xc, REQ_READDATA,
93 0, packet, 1, TIMEOUT);
94 if (ret != 1)
a562c3a2 95 sr_err("%s: libusb_control_transfer returned %d, val=%hhx\n",
6bb5c5fa 96 __func__, ret, packet[0]);
fed16f06 97 return (ret == 1) ? packet[0] : ret;
a1bb33af
UH
98}
99
100int gl_read_bulk(libusb_device_handle *devh, void *buffer, unsigned int size)
101{
fed16f06
UH
102 unsigned char packet[8] =
103 { 0, 0, 0, 0, size & 0xff, (size & 0xff00) >> 8,
104 (size & 0xff0000) >> 16, (size & 0xff000000) >> 24 };
105 int ret, transferred = 0;
106
107 ret = libusb_control_transfer(devh, CTRL_OUT, 0x4, REQ_READBULK,
108 0, packet, 8, TIMEOUT);
109 if (ret != 8)
a562c3a2 110 sr_err("%s: libusb_control_transfer returned %d\n",
6bb5c5fa 111 __func__, ret);
fed16f06
UH
112
113 ret = libusb_bulk_transfer(devh, ENDPOINT_BULK_IN, buffer, size,
114 &transferred, TIMEOUT);
115 if (ret < 0)
a562c3a2 116 sr_err("Bulk read error %d\n", ret);
a1bb33af
UH
117 return transferred;
118}
119
fed16f06
UH
120int gl_reg_write(libusb_device_handle *devh, unsigned int reg,
121 unsigned int val)
a1bb33af 122{
fed16f06
UH
123 int ret;
124
125 ret = gl_write_address(devh, reg);
126 if (ret < 0)
127 return ret;
128 ret = gl_write_data(devh, val);
129 return ret;
a1bb33af
UH
130}
131
132int gl_reg_read(libusb_device_handle *devh, unsigned int reg)
133{
fed16f06
UH
134 int ret;
135
136 ret = gl_write_address(devh, reg);
137 if (ret < 0)
138 return ret;
139 ret = gl_read_data(devh);
140 return ret;
a1bb33af
UH
141}
142
143int gl_open(int vid)
144{
fed16f06
UH
145 int ret;
146 struct libusb_device **devs;
147 struct libusb_device *dev;
148 size_t i = 0;
149 struct libusb_device_descriptor desc;
a1bb33af 150
fed16f06
UH
151 ret = libusb_init(NULL);
152 if (ret < 0)
a1bb33af
UH
153 return GL_ELIBUSB;
154
a1bb33af 155 if (libusb_get_device_list(NULL, &devs) < 0) {
fed16f06 156 ret = GL_EOPEN;
a1bb33af
UH
157 goto gl_open_error;
158 }
159
160 while ((dev = devs[i++]) != NULL) {
a1bb33af
UH
161 if (libusb_get_device_descriptor(dev, &desc) < 0)
162 break;
163
164 if (desc.idVendor == vid) {
165 if (libusb_open(dev, &g_devh) < 0)
166 g_devh = NULL;
167 break;
168 }
169 }
170
171 libusb_free_device_list(devs, 1);
172
173 if (!g_devh) {
fed16f06 174 ret = GL_EOPEN;
a1bb33af
UH
175 goto gl_open_error;
176 }
177
fed16f06
UH
178 ret = libusb_set_configuration(g_devh, 1);
179 if (ret < 0) {
180 ret = GL_ESETCONFIG;
a1bb33af
UH
181 goto gl_open_error;
182 }
183
fed16f06
UH
184 ret = libusb_claim_interface(g_devh, 0);
185 if (ret < 0) {
186 ret = GL_ECLAIM;
a1bb33af
UH
187 goto gl_open_error;
188 }
189
190 return GL_OK;
191
192gl_open_error:
193 gl_close();
fed16f06 194 return ret;
a1bb33af
UH
195}
196
197int gl_close(void)
198{
199 if (g_devh) {
200 libusb_release_interface(g_devh, 0);
201 libusb_reset_device(g_devh);
202 libusb_close(g_devh);
203 }
204 libusb_exit(NULL);
205
206 return 0;
207}