]> sigrok.org Git - libsigrok.git/blame - hardware/zeroplus-logic-cube/gl_usb.c
hardware: Call libusb_error_name() in all USB-related error messages
[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>
45c59c8b
BV
34#include "libsigrok.h"
35#include "libsigrok-internal.h"
a1bb33af
UH
36#include "gl_usb.h"
37
fed16f06
UH
38#define CTRL_IN (LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_IN | \
39 LIBUSB_RECIPIENT_INTERFACE)
40#define CTRL_OUT (LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_OUT | \
41 LIBUSB_RECIPIENT_INTERFACE)
ca070ed9 42#define EP1_BULK_IN (LIBUSB_ENDPOINT_IN | 1)
fed16f06 43
ca070ed9 44#define TIMEOUT 5000 /* Timeout in ms */
a1bb33af
UH
45
46enum {
47 REQ_READBULK = 0x82,
48 REQ_WRITEADDR,
49 REQ_READDATA,
50 REQ_WRITEDATA,
51};
52
ca070ed9 53static int gl_write_address(libusb_device_handle *devh, unsigned int address)
a1bb33af 54{
fed16f06
UH
55 unsigned char packet[8] = { address & 0xFF };
56 int ret;
57
58 ret = libusb_control_transfer(devh, CTRL_OUT, 0xc, REQ_WRITEADDR,
59 0, packet, 1, TIMEOUT);
60 if (ret != 1)
d4928d71 61 sr_err("zp: %s: %s.", __func__, libusb_error_name(ret));
fed16f06 62 return ret;
a1bb33af
UH
63}
64
ca070ed9 65static int gl_write_data(libusb_device_handle *devh, unsigned int val)
a1bb33af 66{
fed16f06
UH
67 unsigned char packet[8] = { val & 0xFF };
68 int ret;
69
70 ret = libusb_control_transfer(devh, CTRL_OUT, 0xc, REQ_WRITEDATA,
71 0, packet, 1, TIMEOUT);
72 if (ret != 1)
d4928d71 73 sr_err("zp: %s: %s.", __func__, libusb_error_name(ret));
fed16f06 74 return ret;
a1bb33af
UH
75}
76
ca070ed9 77static int gl_read_data(libusb_device_handle *devh)
a1bb33af 78{
fed16f06
UH
79 unsigned char packet[8] = { 0 };
80 int ret;
81
82 ret = libusb_control_transfer(devh, CTRL_IN, 0xc, REQ_READDATA,
83 0, packet, 1, TIMEOUT);
84 if (ret != 1)
d4928d71
PS
85 sr_err("zp: %s: %s, val=%hhx.", __func__,
86 libusb_error_name(ret), packet[0]);
fed16f06 87 return (ret == 1) ? packet[0] : ret;
a1bb33af
UH
88}
89
ca070ed9
UH
90SR_PRIV int gl_read_bulk(libusb_device_handle *devh, void *buffer,
91 unsigned int size)
a1bb33af 92{
fed16f06
UH
93 unsigned char packet[8] =
94 { 0, 0, 0, 0, size & 0xff, (size & 0xff00) >> 8,
95 (size & 0xff0000) >> 16, (size & 0xff000000) >> 24 };
96 int ret, transferred = 0;
97
98 ret = libusb_control_transfer(devh, CTRL_OUT, 0x4, REQ_READBULK,
99 0, packet, 8, TIMEOUT);
100 if (ret != 8)
d4928d71
PS
101 sr_err("zp: %s: libusb_control_transfer: %s.", __func__,
102 libusb_error_name(ret));
fed16f06 103
ca070ed9 104 ret = libusb_bulk_transfer(devh, EP1_BULK_IN, buffer, size,
fed16f06
UH
105 &transferred, TIMEOUT);
106 if (ret < 0)
d4928d71
PS
107 sr_err("zp: %s: libusb_bulk_transfer: %s.", __func__,
108 libusb_error_name(ret));
a1bb33af
UH
109 return transferred;
110}
111
ca070ed9 112SR_PRIV int gl_reg_write(libusb_device_handle *devh, unsigned int reg,
fed16f06 113 unsigned int val)
a1bb33af 114{
fed16f06
UH
115 int ret;
116
117 ret = gl_write_address(devh, reg);
118 if (ret < 0)
119 return ret;
120 ret = gl_write_data(devh, val);
121 return ret;
a1bb33af
UH
122}
123
ca070ed9 124SR_PRIV int gl_reg_read(libusb_device_handle *devh, unsigned int reg)
a1bb33af 125{
fed16f06
UH
126 int ret;
127
128 ret = gl_write_address(devh, reg);
129 if (ret < 0)
130 return ret;
131 ret = gl_read_data(devh);
132 return ret;
a1bb33af 133}