]>
Commit | Line | Data |
---|---|---|
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 | ||
a1bb33af | 32 | #include <stdio.h> |
6d116114 | 33 | #include <libusb.h> |
45c59c8b BV |
34 | #include "libsigrok.h" |
35 | #include "libsigrok-internal.h" | |
a1bb33af | 36 | #include "gl_usb.h" |
6d116114 | 37 | #include "protocol.h" |
a1bb33af | 38 | |
fed16f06 UH |
39 | #define CTRL_IN (LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_IN | \ |
40 | LIBUSB_RECIPIENT_INTERFACE) | |
41 | #define CTRL_OUT (LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_OUT | \ | |
42 | LIBUSB_RECIPIENT_INTERFACE) | |
ca070ed9 | 43 | #define EP1_BULK_IN (LIBUSB_ENDPOINT_IN | 1) |
fed16f06 | 44 | |
ca070ed9 | 45 | #define TIMEOUT 5000 /* Timeout in ms */ |
a1bb33af UH |
46 | |
47 | enum { | |
48 | REQ_READBULK = 0x82, | |
49 | REQ_WRITEADDR, | |
50 | REQ_READDATA, | |
51 | REQ_WRITEDATA, | |
52 | }; | |
53 | ||
ca070ed9 | 54 | static int gl_write_address(libusb_device_handle *devh, unsigned int address) |
a1bb33af | 55 | { |
fed16f06 UH |
56 | unsigned char packet[8] = { address & 0xFF }; |
57 | int ret; | |
58 | ||
59 | ret = libusb_control_transfer(devh, CTRL_OUT, 0xc, REQ_WRITEADDR, | |
60 | 0, packet, 1, TIMEOUT); | |
61 | if (ret != 1) | |
6d116114 | 62 | sr_err("%s: %s.", __func__, libusb_error_name(ret)); |
fed16f06 | 63 | return ret; |
a1bb33af UH |
64 | } |
65 | ||
ca070ed9 | 66 | static int gl_write_data(libusb_device_handle *devh, unsigned int val) |
a1bb33af | 67 | { |
fed16f06 UH |
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) | |
6d116114 | 74 | sr_err("%s: %s.", __func__, libusb_error_name(ret)); |
fed16f06 | 75 | return ret; |
a1bb33af UH |
76 | } |
77 | ||
ca070ed9 | 78 | static int gl_read_data(libusb_device_handle *devh) |
a1bb33af | 79 | { |
fed16f06 UH |
80 | unsigned char packet[8] = { 0 }; |
81 | int ret; | |
82 | ||
83 | ret = libusb_control_transfer(devh, CTRL_IN, 0xc, REQ_READDATA, | |
84 | 0, packet, 1, TIMEOUT); | |
85 | if (ret != 1) | |
6d116114 | 86 | sr_err("%s: %s, val=%hhx.", __func__, |
d4928d71 | 87 | libusb_error_name(ret), packet[0]); |
fed16f06 | 88 | return (ret == 1) ? packet[0] : ret; |
a1bb33af UH |
89 | } |
90 | ||
ca070ed9 UH |
91 | SR_PRIV int gl_read_bulk(libusb_device_handle *devh, void *buffer, |
92 | unsigned int size) | |
a1bb33af | 93 | { |
fed16f06 UH |
94 | unsigned char packet[8] = |
95 | { 0, 0, 0, 0, size & 0xff, (size & 0xff00) >> 8, | |
96 | (size & 0xff0000) >> 16, (size & 0xff000000) >> 24 }; | |
97 | int ret, transferred = 0; | |
98 | ||
99 | ret = libusb_control_transfer(devh, CTRL_OUT, 0x4, REQ_READBULK, | |
100 | 0, packet, 8, TIMEOUT); | |
101 | if (ret != 8) | |
6d116114 | 102 | sr_err("%s: libusb_control_transfer: %s.", __func__, |
d4928d71 | 103 | libusb_error_name(ret)); |
fed16f06 | 104 | |
ca070ed9 | 105 | ret = libusb_bulk_transfer(devh, EP1_BULK_IN, buffer, size, |
fed16f06 UH |
106 | &transferred, TIMEOUT); |
107 | if (ret < 0) | |
6d116114 | 108 | sr_err("%s: libusb_bulk_transfer: %s.", __func__, |
d4928d71 | 109 | libusb_error_name(ret)); |
a1bb33af UH |
110 | return transferred; |
111 | } | |
112 | ||
ca070ed9 | 113 | SR_PRIV int gl_reg_write(libusb_device_handle *devh, unsigned int reg, |
fed16f06 | 114 | unsigned int val) |
a1bb33af | 115 | { |
fed16f06 UH |
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; | |
a1bb33af UH |
123 | } |
124 | ||
ca070ed9 | 125 | SR_PRIV int gl_reg_read(libusb_device_handle *devh, unsigned int reg) |
a1bb33af | 126 | { |
fed16f06 UH |
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; | |
a1bb33af | 134 | } |