]> sigrok.org Git - libsigrok.git/blob - hardware/zeroplus-logic-cube/gl_usb.c
sr: sr_err() et. al. don't need trailing \n.
[libsigrok.git] / hardware / zeroplus-logic-cube / gl_usb.c
1 /*
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
32 #include <libusb.h>
33 #include <stdio.h>
34 #include "sigrok.h"
35 #include "sigrok-internal.h"
36 #include "gl_usb.h"
37
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)
42 #define EP1_BULK_IN     (LIBUSB_ENDPOINT_IN | 1)
43
44 #define TIMEOUT         5000    /* Timeout in ms */
45
46 enum {
47         REQ_READBULK = 0x82,
48         REQ_WRITEADDR,
49         REQ_READDATA,
50         REQ_WRITEDATA,
51 };
52
53 static int gl_write_address(libusb_device_handle *devh, unsigned int address)
54 {
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)
61                 sr_err("zp: %s: libusb_control_transfer returned %d.",
62                        __func__, ret);
63         return ret;
64 }
65
66 static int gl_write_data(libusb_device_handle *devh, unsigned int val)
67 {
68         unsigned char packet[8] = { val & 0xFF };
69         int ret;
70
71         ret = libusb_control_transfer(devh, CTRL_OUT, 0xc, REQ_WRITEDATA,
72                                       0, packet, 1, TIMEOUT);
73         if (ret != 1)
74                 sr_err("zp: %s: libusb_control_transfer returned %d.",
75                        __func__, ret);
76         return ret;
77 }
78
79 static int gl_read_data(libusb_device_handle *devh)
80 {
81         unsigned char packet[8] = { 0 };
82         int ret;
83
84         ret = libusb_control_transfer(devh, CTRL_IN, 0xc, REQ_READDATA,
85                                       0, packet, 1, TIMEOUT);
86         if (ret != 1)
87                 sr_err("zp: %s: libusb_control_transfer returned %d, "
88                        "val=%hhx.", __func__, ret, packet[0]);
89         return (ret == 1) ? packet[0] : ret;
90 }
91
92 SR_PRIV int gl_read_bulk(libusb_device_handle *devh, void *buffer,
93                          unsigned int size)
94 {
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,
101                                       0, packet, 8, TIMEOUT);
102         if (ret != 8)
103                 sr_err("zp: %s: libusb_control_transfer returned %d.",
104                        __func__, ret);
105
106         ret = libusb_bulk_transfer(devh, EP1_BULK_IN, buffer, size,
107                                    &transferred, TIMEOUT);
108         if (ret < 0)
109                 sr_err("zp: Bulk read error %d.", ret);
110         return transferred;
111 }
112
113 SR_PRIV int gl_reg_write(libusb_device_handle *devh, unsigned int reg,
114                  unsigned int val)
115 {
116         int ret;
117
118         ret = gl_write_address(devh, reg);
119         if (ret < 0)
120                 return ret;
121         ret = gl_write_data(devh, val);
122         return ret;
123 }
124
125 SR_PRIV int gl_reg_read(libusb_device_handle *devh, unsigned int reg)
126 {
127         int ret;
128
129         ret = gl_write_address(devh, reg);
130         if (ret < 0)
131                 return ret;
132         ret = gl_read_data(devh);
133         return ret;
134 }