]> sigrok.org Git - libsigrok.git/blame - hardware/zeroplus-logic-cube/gl_usb.c
sr: Make more symbols private via static/SR_PRIV.
[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>
1a081ca6 34#include "sigrok.h"
b7f09cf8 35#include "sigrok-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
53static struct libusb_device_handle *g_devh = NULL;
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,
61 0, packet, 1, TIMEOUT);
62 if (ret != 1)
a562c3a2 63 sr_err("%s: libusb_control_transfer returned %d\n",
6bb5c5fa 64 __func__, ret);
fed16f06 65 return ret;
a1bb33af
UH
66}
67
ca070ed9 68static int gl_write_data(libusb_device_handle *devh, unsigned int val)
a1bb33af 69{
fed16f06
UH
70 unsigned char packet[8] = { val & 0xFF };
71 int ret;
72
73 ret = libusb_control_transfer(devh, CTRL_OUT, 0xc, REQ_WRITEDATA,
74 0, packet, 1, TIMEOUT);
75 if (ret != 1)
a562c3a2 76 sr_err("%s: libusb_control_transfer returned %d\n",
6bb5c5fa 77 __func__, ret);
fed16f06 78 return ret;
a1bb33af
UH
79}
80
ca070ed9 81static int gl_read_data(libusb_device_handle *devh)
a1bb33af 82{
fed16f06
UH
83 unsigned char packet[8] = { 0 };
84 int ret;
85
86 ret = libusb_control_transfer(devh, CTRL_IN, 0xc, REQ_READDATA,
87 0, packet, 1, TIMEOUT);
88 if (ret != 1)
a562c3a2 89 sr_err("%s: libusb_control_transfer returned %d, val=%hhx\n",
6bb5c5fa 90 __func__, ret, packet[0]);
fed16f06 91 return (ret == 1) ? packet[0] : ret;
a1bb33af
UH
92}
93
ca070ed9
UH
94SR_PRIV int gl_read_bulk(libusb_device_handle *devh, void *buffer,
95 unsigned int size)
a1bb33af 96{
fed16f06
UH
97 unsigned char packet[8] =
98 { 0, 0, 0, 0, size & 0xff, (size & 0xff00) >> 8,
99 (size & 0xff0000) >> 16, (size & 0xff000000) >> 24 };
100 int ret, transferred = 0;
101
102 ret = libusb_control_transfer(devh, CTRL_OUT, 0x4, REQ_READBULK,
103 0, packet, 8, TIMEOUT);
104 if (ret != 8)
a562c3a2 105 sr_err("%s: libusb_control_transfer returned %d\n",
6bb5c5fa 106 __func__, ret);
fed16f06 107
ca070ed9 108 ret = libusb_bulk_transfer(devh, EP1_BULK_IN, buffer, size,
fed16f06
UH
109 &transferred, TIMEOUT);
110 if (ret < 0)
a562c3a2 111 sr_err("Bulk read error %d\n", ret);
a1bb33af
UH
112 return transferred;
113}
114
ca070ed9 115SR_PRIV int gl_reg_write(libusb_device_handle *devh, unsigned int reg,
fed16f06 116 unsigned int val)
a1bb33af 117{
fed16f06
UH
118 int ret;
119
120 ret = gl_write_address(devh, reg);
121 if (ret < 0)
122 return ret;
123 ret = gl_write_data(devh, val);
124 return ret;
a1bb33af
UH
125}
126
ca070ed9 127SR_PRIV int gl_reg_read(libusb_device_handle *devh, unsigned int reg)
a1bb33af 128{
fed16f06
UH
129 int ret;
130
131 ret = gl_write_address(devh, reg);
132 if (ret < 0)
133 return ret;
134 ret = gl_read_data(devh);
135 return ret;
a1bb33af 136}