]> sigrok.org Git - libsigrok.git/blame - src/hardware/zeroplus-logic-cube/gl_usb.c
Build: Include <config.h> first in all source files
[libsigrok.git] / src / hardware / zeroplus-logic-cube / gl_usb.c
CommitLineData
a1bb33af 1/*
50985c20 2 * This file is part of the libsigrok project.
fed16f06
UH
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
6ec6c43b 32#include <config.h>
a1bb33af 33#include <stdio.h>
6d116114 34#include <libusb.h>
c1aae900 35#include <libsigrok/libsigrok.h>
45c59c8b 36#include "libsigrok-internal.h"
a1bb33af 37#include "gl_usb.h"
6d116114 38#include "protocol.h"
a1bb33af 39
fed16f06
UH
40#define CTRL_IN (LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_IN | \
41 LIBUSB_RECIPIENT_INTERFACE)
42#define CTRL_OUT (LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_OUT | \
43 LIBUSB_RECIPIENT_INTERFACE)
ca070ed9 44#define EP1_BULK_IN (LIBUSB_ENDPOINT_IN | 1)
fed16f06 45
1a46cc62 46#define TIMEOUT_MS (5 * 1000)
a1bb33af
UH
47
48enum {
49 REQ_READBULK = 0x82,
50 REQ_WRITEADDR,
51 REQ_READDATA,
52 REQ_WRITEDATA,
53};
54
ca070ed9 55static int gl_write_address(libusb_device_handle *devh, unsigned int address)
a1bb33af 56{
fed16f06
UH
57 unsigned char packet[8] = { address & 0xFF };
58 int ret;
59
60 ret = libusb_control_transfer(devh, CTRL_OUT, 0xc, REQ_WRITEADDR,
1a46cc62 61 0, packet, 1, TIMEOUT_MS);
fed16f06 62 if (ret != 1)
6d116114 63 sr_err("%s: %s.", __func__, libusb_error_name(ret));
fed16f06 64 return ret;
a1bb33af
UH
65}
66
ca070ed9 67static int gl_write_data(libusb_device_handle *devh, unsigned int val)
a1bb33af 68{
fed16f06
UH
69 unsigned char packet[8] = { val & 0xFF };
70 int ret;
71
72 ret = libusb_control_transfer(devh, CTRL_OUT, 0xc, REQ_WRITEDATA,
1a46cc62 73 0, packet, 1, TIMEOUT_MS);
fed16f06 74 if (ret != 1)
6d116114 75 sr_err("%s: %s.", __func__, libusb_error_name(ret));
fed16f06 76 return ret;
a1bb33af
UH
77}
78
ca070ed9 79static int gl_read_data(libusb_device_handle *devh)
a1bb33af 80{
fed16f06
UH
81 unsigned char packet[8] = { 0 };
82 int ret;
83
84 ret = libusb_control_transfer(devh, CTRL_IN, 0xc, REQ_READDATA,
1a46cc62 85 0, packet, 1, TIMEOUT_MS);
fed16f06 86 if (ret != 1)
6d116114 87 sr_err("%s: %s, val=%hhx.", __func__,
d4928d71 88 libusb_error_name(ret), packet[0]);
fed16f06 89 return (ret == 1) ? packet[0] : ret;
a1bb33af
UH
90}
91
ca070ed9
UH
92SR_PRIV int gl_read_bulk(libusb_device_handle *devh, void *buffer,
93 unsigned int size)
a1bb33af 94{
fed16f06
UH
95 unsigned char packet[8] =
96 { 0, 0, 0, 0, size & 0xff, (size & 0xff00) >> 8,
97 (size & 0xff0000) >> 16, (size & 0xff000000) >> 24 };
98 int ret, transferred = 0;
99
100 ret = libusb_control_transfer(devh, CTRL_OUT, 0x4, REQ_READBULK,
1a46cc62 101 0, packet, 8, TIMEOUT_MS);
fed16f06 102 if (ret != 8)
6d116114 103 sr_err("%s: libusb_control_transfer: %s.", __func__,
d4928d71 104 libusb_error_name(ret));
fed16f06 105
ca070ed9 106 ret = libusb_bulk_transfer(devh, EP1_BULK_IN, buffer, size,
1a46cc62 107 &transferred, TIMEOUT_MS);
fed16f06 108 if (ret < 0)
6d116114 109 sr_err("%s: libusb_bulk_transfer: %s.", __func__,
d4928d71 110 libusb_error_name(ret));
a1bb33af
UH
111 return transferred;
112}
113
ca070ed9 114SR_PRIV int gl_reg_write(libusb_device_handle *devh, unsigned int reg,
fed16f06 115 unsigned int val)
a1bb33af 116{
fed16f06
UH
117 int ret;
118
119 ret = gl_write_address(devh, reg);
120 if (ret < 0)
121 return ret;
122 ret = gl_write_data(devh, val);
123 return ret;
a1bb33af
UH
124}
125
ca070ed9 126SR_PRIV int gl_reg_read(libusb_device_handle *devh, unsigned int reg)
a1bb33af 127{
fed16f06
UH
128 int ret;
129
130 ret = gl_write_address(devh, reg);
131 if (ret < 0)
132 return ret;
133 ret = gl_read_data(devh);
134 return ret;
a1bb33af 135}
aad03192
RD
136
137SR_PRIV int gl_reg_read_buf(libusb_device_handle *devh, unsigned int reg,
138 unsigned char *buf, unsigned int len)
139{
140 int ret;
141 unsigned int i;
142
143 ret = gl_write_address(devh, reg);
144 if (ret < 0)
145 return ret;
146 for (i = 0; i < len; i++) {
147 ret = gl_read_data(devh);
148 if (ret < 0)
149 return ret;
150 buf[i] = ret;
151 }
152 return 0;
153}