]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2017-2019 Gerhard Sittig <gerhard.sittig@gmx.net> | |
5 | * | |
6 | * This program is free software: you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation, either version 3 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
19 | ||
20 | #include <config.h> | |
21 | #include <glib.h> | |
22 | #include <libsigrok/libsigrok.h> | |
23 | #include "libsigrok-internal.h" | |
24 | #include "serial_hid.h" | |
25 | #include <string.h> | |
26 | ||
27 | #define LOG_PREFIX "serial-ch9325" | |
28 | ||
29 | #ifdef HAVE_SERIAL_COMM | |
30 | #ifdef HAVE_LIBHIDAPI | |
31 | ||
32 | /** | |
33 | * @file | |
34 | * | |
35 | * Support serial-over-HID, specifically the WCH CH9325 chip. | |
36 | */ | |
37 | ||
38 | #define CH9325_MAX_BYTES_PER_REQUEST 7 | |
39 | ||
40 | static const struct vid_pid_item vid_pid_items_ch9325[] = { | |
41 | { 0x1a86, 0xe008, }, /* CH9325 */ | |
42 | /* | |
43 | * Strictly speaking Hoitek HE2325U is a different chip, but | |
44 | * shares the programming model with WCH CH9325, and works | |
45 | * with the same support code. | |
46 | */ | |
47 | { 0x04fa, 0x2490, }, /* HE2325U */ | |
48 | ALL_ZERO | |
49 | }; | |
50 | ||
51 | static int ch9325_set_params(struct sr_serial_dev_inst *serial, | |
52 | int baudrate, int bits, int parity, int stopbits, | |
53 | int flowcontrol, int rts, int dtr) | |
54 | { | |
55 | uint8_t report[6]; | |
56 | int replen; | |
57 | int rc; | |
58 | GString *text; | |
59 | ||
60 | (void)parity; | |
61 | (void)stopbits; | |
62 | (void)flowcontrol; | |
63 | (void)rts; | |
64 | (void)dtr; | |
65 | ||
66 | /* | |
67 | * Setup bitrate and frame format. Report layout: | |
68 | * (@-1, length 1, report number) | |
69 | * @0, length 2, bitrate (little endian format) | |
70 | * @2, length 1, unknown (parity? stop bits?) | |
71 | * @3, length 1, unknown (parity? stop bits?) | |
72 | * @4, length 1, data bits (0: 5, 1: 6, etc, 3: 8) | |
73 | */ | |
74 | replen = 0; | |
75 | report[replen++] = 0; | |
76 | WL16(&report[replen], baudrate); | |
77 | replen += sizeof(uint16_t); | |
78 | report[replen++] = 0x00; | |
79 | report[replen++] = 0x00; | |
80 | report[replen++] = bits - 5; | |
81 | rc = ser_hid_hidapi_set_report(serial, report, replen); | |
82 | text = sr_hexdump_new(report, replen); | |
83 | sr_dbg("DBG: %s() report %s => rc %d", __func__, text->str, rc); | |
84 | sr_hexdump_free(text); | |
85 | if (rc < 0) | |
86 | return SR_ERR; | |
87 | if (rc != replen) | |
88 | return SR_ERR; | |
89 | ||
90 | return SR_OK; | |
91 | } | |
92 | ||
93 | static int ch9325_read_bytes(struct sr_serial_dev_inst *serial, | |
94 | uint8_t *data, int space, unsigned int timeout) | |
95 | { | |
96 | uint8_t buffer[1 + CH9325_MAX_BYTES_PER_REQUEST]; | |
97 | int rc; | |
98 | int count; | |
99 | ||
100 | /* | |
101 | * Check for available input data from the serial port. | |
102 | * Packet layout: | |
103 | * @0, length 1, number of bytes, OR-ed with 0xf0 | |
104 | * @1, length N, data bytes (up to 7 bytes) | |
105 | */ | |
106 | rc = ser_hid_hidapi_get_data(serial, 2, buffer, sizeof(buffer), timeout); | |
107 | if (rc < 0) | |
108 | return SR_ERR; | |
109 | if (rc == 0) | |
110 | return 0; | |
111 | sr_dbg("DBG: %s() got report len %d, 0x%02x.", __func__, rc, buffer[0]); | |
112 | ||
113 | /* Check the length spec, get the byte count. */ | |
114 | count = buffer[0]; | |
115 | if ((count & 0xf0) != 0xf0) | |
116 | return SR_ERR; | |
117 | count &= 0x0f; | |
118 | sr_dbg("DBG: %s(), got %d UART RX bytes.", __func__, count); | |
119 | if (count > space) | |
120 | return SR_ERR; | |
121 | ||
122 | /* Pass received data bytes and their count to the caller. */ | |
123 | memcpy(data, &buffer[1], count); | |
124 | return count; | |
125 | } | |
126 | ||
127 | static int ch9325_write_bytes(struct sr_serial_dev_inst *serial, | |
128 | const uint8_t *data, int size) | |
129 | { | |
130 | uint8_t buffer[1 + CH9325_MAX_BYTES_PER_REQUEST]; | |
131 | int rc; | |
132 | ||
133 | sr_dbg("DBG: %s() shall send UART TX data, len %d.", __func__, size); | |
134 | ||
135 | if (size < 1) | |
136 | return 0; | |
137 | if (size > CH9325_MAX_BYTES_PER_REQUEST) { | |
138 | size = CH9325_MAX_BYTES_PER_REQUEST; | |
139 | sr_dbg("DBG: %s() capping size to %d.", __func__, size); | |
140 | } | |
141 | ||
142 | /* | |
143 | * Packet layout to send serial data to the USB HID chip: | |
144 | * (@-1, length 1, report number) | |
145 | * @0, length 1, number of bytes, OR-ed with 0xf0 | |
146 | * @1, length N, data bytes (up to 7 bytes) | |
147 | */ | |
148 | buffer[0] = size; /* YES! TX is *without* 0xf0! */ | |
149 | memcpy(&buffer[1], data, size); | |
150 | rc = ser_hid_hidapi_set_data(serial, 2, buffer, sizeof(buffer), 0); | |
151 | if (rc < 0) | |
152 | return rc; | |
153 | if (rc == 0) | |
154 | return 0; | |
155 | return size; | |
156 | } | |
157 | ||
158 | static struct ser_hid_chip_functions chip_ch9325 = { | |
159 | .chipname = "ch9325", | |
160 | .chipdesc = "WCH CH9325", | |
161 | .vid_pid_items = vid_pid_items_ch9325, | |
162 | .max_bytes_per_request = CH9325_MAX_BYTES_PER_REQUEST, | |
163 | .set_params = ch9325_set_params, | |
164 | .read_bytes = ch9325_read_bytes, | |
165 | .write_bytes = ch9325_write_bytes, | |
166 | }; | |
167 | SR_PRIV struct ser_hid_chip_functions *ser_hid_chip_funcs_ch9325 = &chip_ch9325; | |
168 | ||
169 | #else | |
170 | ||
171 | SR_PRIV struct ser_hid_chip_functions *ser_hid_chip_funcs_ch9325 = NULL; | |
172 | ||
173 | #endif | |
174 | #endif |