-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcore.c
More file actions
239 lines (202 loc) · 6.58 KB
/
core.c
File metadata and controls
239 lines (202 loc) · 6.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
* libusbserial
*
* Copyright (C) 2019-2025 Anton Prozorov <prozanton@gmail.com>
* Copyright (c) 2014-2015 Felix Hädicke
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*/
#include "libusbserial.h"
#include "config.h"
#include "driver.h"
#include "internal.h"
#include <assert.h>
#include <stdlib.h>
extern struct usbserial_driver *usbserial_driver_ftdi;
extern struct usbserial_driver *usbserial_driver_silabs;
extern struct usbserial_driver *usbserial_driver_ch34x;
extern struct usbserial_driver *usbserial_driver_pl2303;
extern struct usbserial_driver *usbserial_driver_cdc;
static const struct usbserial_driver* find_driver_for_usb_device(uint16_t vid, uint16_t pid, uint8_t class, uint8_t subclass)
{
const struct usbserial_driver* drivers[] =
{
usbserial_driver_ftdi,
usbserial_driver_silabs,
usbserial_driver_ch34x,
usbserial_driver_pl2303,
usbserial_driver_cdc,
NULL
};
const struct usbserial_driver **driver = drivers;
while (*driver)
{
if ((*driver)->check_supported_by_vid_pid && (*driver)->check_supported_by_vid_pid(vid, pid))
return *driver;
if ((*driver)->check_supported_by_class && (*driver)->check_supported_by_class(class, subclass))
return *driver;
driver++;
}
return NULL;
}
int usbserial_is_device_supported(uint16_t vid, uint16_t pid, uint8_t class, uint8_t subclass)
{
return !!find_driver_for_usb_device(vid, pid, class, subclass);
}
const char* usbserial_get_device_name(uint16_t vid, uint16_t pid, uint8_t class, uint8_t subclass)
{
const struct usbserial_driver* driver = find_driver_for_usb_device(vid, pid, class, subclass);
if (!driver)
return NULL;
return driver->get_device_name(vid, pid, class, subclass);
}
const char* usbserial_get_device_name2(struct usbserial_port *port)
{
if (!port)
return NULL;
return port->driver->get_device_name(
port->usb_dev_desc.idVendor, port->usb_dev_desc.idProduct,
port->usb_dev_desc.bDeviceClass, port->usb_dev_desc.bDeviceSubClass);
}
unsigned int usbserial_get_ports_count(uint16_t vid, uint16_t pid, uint8_t class, uint8_t subclass)
{
const struct usbserial_driver* driver = find_driver_for_usb_device(vid, pid, class, subclass);
if (!driver)
return 0;
return driver->get_ports_count(vid, pid);
}
int usbserial_port_init(
struct usbserial_port **out_port,
libusb_device_handle *usb_dev_hdl,
unsigned int port_idx,
usbserial_cb_read_fn cb_read,
usbserial_cb_error_fn cb_read_error,
void* cb_user_data)
{
int ret;
libusb_device *usb_dev;
const struct usbserial_driver *driver;
struct usbserial_port *port = 0;
struct libusb_device_descriptor usb_dev_desc;
int mutex_init = 0;
if (!out_port || !usb_dev_hdl)
return USBSERIAL_ERROR_INVALID_PARAMETER;
*out_port = NULL;
usb_dev = libusb_get_device(usb_dev_hdl);
if (!usb_dev)
return USBSERIAL_ERROR_NO_SUCH_DEVICE;
ret = libusb_get_device_descriptor(usb_dev, &usb_dev_desc);
if (ret)
goto failed;
driver = find_driver_for_usb_device(usb_dev_desc.idVendor, usb_dev_desc.idProduct,
usb_dev_desc.bDeviceClass, usb_dev_desc.bDeviceSubClass);
if (!driver)
{
ret = USBSERIAL_ERROR_UNSUPPORTED_DEVICE;
goto failed;
}
port = (struct usbserial_port*)calloc(1, sizeof(struct usbserial_port));
if (!port)
{
ret = USBSERIAL_ERROR_RESOURCE_ALLOC_FAILED;
goto failed;
}
if (pthread_mutex_init(&port->mutex, NULL))
{
ret = USBSERIAL_ERROR_RESOURCE_ALLOC_FAILED;
goto failed;
}
mutex_init = 1;
if (pthread_mutex_lock(&port->mutex))
{
ret = USBSERIAL_ERROR_RESOURCE_ALLOC_FAILED;
goto failed;
}
port->driver = driver;
port->usb_dev = usb_dev;
port->usb_dev_hdl = usb_dev_hdl;
port->usb_dev_desc = usb_dev_desc;
port->port_idx = port_idx;
port->cb_read = cb_read;
port->cb_read_error = cb_read_error;
port->cb_user_data = cb_user_data;
if (pthread_mutex_unlock(&port->mutex))
{
ret = USBSERIAL_ERROR_RESOURCE_ALLOC_FAILED;
goto failed;
}
for (int inf = 0; inf < usb_dev_desc.bNumConfigurations; inf++)
{
if (libusb_kernel_driver_active(port->usb_dev_hdl, inf) == 1)
libusb_detach_kernel_driver(port->usb_dev_hdl, inf);
}
ret = driver->port_init(port);
if (ret)
goto failed;
*out_port = port;
return 0;
failed:
assert(0 != ret);
if (port)
{
if (mutex_init)
pthread_mutex_destroy(&port->mutex);
free(port);
}
return ret;
}
int usbserial_port_deinit(struct usbserial_port *port)
{
int ret;
if (!port)
return USBSERIAL_ERROR_INVALID_PARAMETER;
ret = port->driver->port_deinit(port);
free(port);
return ret;
}
int usbserial_port_set_config(struct usbserial_port *port, const struct usbserial_config *config)
{
if (!port || !config)
return USBSERIAL_ERROR_INVALID_PARAMETER;
return port->driver->port_set_config(port, config);
}
int usbserial_start_reader(struct usbserial_port *port)
{
if (!port)
return USBSERIAL_ERROR_INVALID_PARAMETER;
if (!port->cb_read)
return USBSERIAL_ERROR_ILLEGAL_STATE;
return port->driver->start_reader(port);
}
int usbserial_stop_reader(struct usbserial_port *port)
{
if (!port)
return USBSERIAL_ERROR_INVALID_PARAMETER;
return port->driver->stop_reader(port);
}
int usbserial_write(struct usbserial_port *port, const void *data, size_t bytes_count)
{
if (!port)
return USBSERIAL_ERROR_INVALID_PARAMETER;
return port->driver->write(port, data, bytes_count);
}
int usbserial_purge(struct usbserial_port *port, int rx, int tx)
{
if (!port)
return USBSERIAL_ERROR_INVALID_PARAMETER;
return port->driver->purge(port, rx, tx);
}
int usbserial_set_dtr_rts(struct usbserial_port *port, int dtr, int rts)
{
if (!port)
return USBSERIAL_ERROR_INVALID_PARAMETER;
return port->driver->set_dtr_rts(port, dtr, rts);
}