]>
Commit | Line | Data |
---|---|---|
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) | |
42 | ||
43 | const int PACKET_CTRL_LEN = 2; | |
44 | const int PACKET_INT_LEN = 2; | |
45 | const int PACKET_BULK_LEN = 64; | |
46 | const int INTERFACE = 0; | |
47 | const int ENDPOINT_INT_IN = 0x81; /* Endpoint 0x81 address for IN */ | |
48 | const int ENDPOINT_INT_OUT = 0x01; /* Endpoint 1 address for OUT */ | |
49 | const int ENDPOINT_BULK_IN = 0x81; /* Endpoint 0x81 address for IN */ | |
50 | const int ENDPOINT_BULK_OUT = 0x02; /* Endpoint 1 address for OUT */ | |
51 | const int TIMEOUT = 5000; /* Timeout in ms */ | |
a1bb33af UH |
52 | |
53 | enum { | |
54 | REQ_READBULK = 0x82, | |
55 | REQ_WRITEADDR, | |
56 | REQ_READDATA, | |
57 | REQ_WRITEDATA, | |
58 | }; | |
59 | ||
60 | static struct libusb_device_handle *g_devh = NULL; | |
61 | ||
62 | int gl_write_address(libusb_device_handle *devh, unsigned int address) | |
63 | { | |
fed16f06 UH |
64 | unsigned char packet[8] = { address & 0xFF }; |
65 | int ret; | |
66 | ||
67 | ret = libusb_control_transfer(devh, CTRL_OUT, 0xc, REQ_WRITEADDR, | |
68 | 0, packet, 1, TIMEOUT); | |
69 | if (ret != 1) | |
a562c3a2 | 70 | sr_err("%s: libusb_control_transfer returned %d\n", |
6bb5c5fa | 71 | __func__, ret); |
fed16f06 | 72 | return ret; |
a1bb33af UH |
73 | } |
74 | ||
75 | int gl_write_data(libusb_device_handle *devh, unsigned int val) | |
76 | { | |
fed16f06 UH |
77 | unsigned char packet[8] = { val & 0xFF }; |
78 | int ret; | |
79 | ||
80 | ret = libusb_control_transfer(devh, CTRL_OUT, 0xc, REQ_WRITEDATA, | |
81 | 0, packet, 1, TIMEOUT); | |
82 | if (ret != 1) | |
a562c3a2 | 83 | sr_err("%s: libusb_control_transfer returned %d\n", |
6bb5c5fa | 84 | __func__, ret); |
fed16f06 | 85 | return ret; |
a1bb33af UH |
86 | } |
87 | ||
88 | int gl_read_data(libusb_device_handle *devh) | |
89 | { | |
fed16f06 UH |
90 | unsigned char packet[8] = { 0 }; |
91 | int ret; | |
92 | ||
93 | ret = libusb_control_transfer(devh, CTRL_IN, 0xc, REQ_READDATA, | |
94 | 0, packet, 1, TIMEOUT); | |
95 | if (ret != 1) | |
a562c3a2 | 96 | sr_err("%s: libusb_control_transfer returned %d, val=%hhx\n", |
6bb5c5fa | 97 | __func__, ret, packet[0]); |
fed16f06 | 98 | return (ret == 1) ? packet[0] : ret; |
a1bb33af UH |
99 | } |
100 | ||
101 | int gl_read_bulk(libusb_device_handle *devh, void *buffer, unsigned int size) | |
102 | { | |
fed16f06 UH |
103 | unsigned char packet[8] = |
104 | { 0, 0, 0, 0, size & 0xff, (size & 0xff00) >> 8, | |
105 | (size & 0xff0000) >> 16, (size & 0xff000000) >> 24 }; | |
106 | int ret, transferred = 0; | |
107 | ||
108 | ret = libusb_control_transfer(devh, CTRL_OUT, 0x4, REQ_READBULK, | |
109 | 0, packet, 8, TIMEOUT); | |
110 | if (ret != 8) | |
a562c3a2 | 111 | sr_err("%s: libusb_control_transfer returned %d\n", |
6bb5c5fa | 112 | __func__, ret); |
fed16f06 UH |
113 | |
114 | ret = libusb_bulk_transfer(devh, ENDPOINT_BULK_IN, buffer, size, | |
115 | &transferred, TIMEOUT); | |
116 | if (ret < 0) | |
a562c3a2 | 117 | sr_err("Bulk read error %d\n", ret); |
a1bb33af UH |
118 | return transferred; |
119 | } | |
120 | ||
fed16f06 UH |
121 | int gl_reg_write(libusb_device_handle *devh, unsigned int reg, |
122 | unsigned int val) | |
a1bb33af | 123 | { |
fed16f06 UH |
124 | int ret; |
125 | ||
126 | ret = gl_write_address(devh, reg); | |
127 | if (ret < 0) | |
128 | return ret; | |
129 | ret = gl_write_data(devh, val); | |
130 | return ret; | |
a1bb33af UH |
131 | } |
132 | ||
133 | int gl_reg_read(libusb_device_handle *devh, unsigned int reg) | |
134 | { | |
fed16f06 UH |
135 | int ret; |
136 | ||
137 | ret = gl_write_address(devh, reg); | |
138 | if (ret < 0) | |
139 | return ret; | |
140 | ret = gl_read_data(devh); | |
141 | return ret; | |
a1bb33af | 142 | } |