]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2017 Jan Luebbe <jluebbe@lasnet.de> | |
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 <string.h> | |
23 | #include "protocol.h" | |
24 | ||
25 | #define BUF_COUNT 512 | |
26 | #define BUF_SIZE (16 * 1024) | |
27 | #define BUF_TIMEOUT 1000 | |
28 | ||
29 | static const uint32_t scanopts[] = { | |
30 | SR_CONF_CONN, | |
31 | }; | |
32 | ||
33 | static const uint32_t drvopts[] = { | |
34 | SR_CONF_LOGIC_ANALYZER, | |
35 | }; | |
36 | ||
37 | static const uint32_t devopts[] = { | |
38 | SR_CONF_CONTINUOUS, | |
39 | SR_CONF_CONN | SR_CONF_GET, | |
40 | SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
41 | }; | |
42 | ||
43 | static const char *channel_names[] = { | |
44 | "0", "1", "2", "3", "4", "5", "6", "7", | |
45 | "8", "9", "10", "11", "12", "13", "14", "15", | |
46 | }; | |
47 | ||
48 | static const uint64_t samplerates[] = { | |
49 | SR_MHZ(1), | |
50 | SR_MHZ(2), | |
51 | SR_KHZ(2500), | |
52 | SR_MHZ(10), | |
53 | SR_MHZ(25), | |
54 | SR_MHZ(50), | |
55 | }; | |
56 | ||
57 | #define FW_HEADER_SIZE 7 | |
58 | #define FW_MAX_PART_SIZE (4 * 1024) | |
59 | ||
60 | static int upload_firmware(struct sr_context *ctx, libusb_device *dev, const char *name) | |
61 | { | |
62 | struct libusb_device_handle *hdl = NULL; | |
63 | unsigned char *firmware = NULL; | |
64 | int ret = SR_ERR; | |
65 | size_t fw_size, fw_offset = 0; | |
66 | uint32_t part_address = 0; | |
67 | uint16_t part_size = 0; | |
68 | uint8_t part_final = 0; | |
69 | ||
70 | firmware = sr_resource_load(ctx, SR_RESOURCE_FIRMWARE, | |
71 | name, &fw_size, 256 * 1024); | |
72 | if (!firmware) | |
73 | goto out; | |
74 | ||
75 | sr_info("Uploading firmware '%s'.", name); | |
76 | ||
77 | if (libusb_open(dev, &hdl) != 0) | |
78 | goto out; | |
79 | ||
80 | while ((fw_offset + FW_HEADER_SIZE) <= fw_size) { | |
81 | part_size = GUINT16_FROM_LE(*(uint16_t*)(firmware + fw_offset)); | |
82 | part_address = GUINT32_FROM_LE(*(uint32_t*)(firmware + fw_offset + 2)); | |
83 | part_final = *(uint8_t*)(firmware + fw_offset + 6); | |
84 | if (part_size > FW_MAX_PART_SIZE) { | |
85 | sr_err("Part too large (%d).", part_size); | |
86 | goto out; | |
87 | } | |
88 | fw_offset += FW_HEADER_SIZE; | |
89 | if ((fw_offset + part_size) > fw_size) { | |
90 | sr_err("Truncated firmware file."); | |
91 | goto out; | |
92 | } | |
93 | ret = libusb_control_transfer(hdl, LIBUSB_REQUEST_TYPE_VENDOR | | |
94 | LIBUSB_ENDPOINT_OUT, 0xa0, | |
95 | part_address & 0xffff, part_address >> 16, | |
96 | firmware + fw_offset, part_size, | |
97 | 100); | |
98 | if (ret < 0) { | |
99 | sr_err("Unable to send firmware to device: %s.", | |
100 | libusb_error_name(ret)); | |
101 | ret = SR_ERR; | |
102 | goto out; | |
103 | } | |
104 | if (part_size) | |
105 | sr_spew("Uploaded %d bytes.", part_size); | |
106 | else | |
107 | sr_info("Started firmware at 0x%x.", part_address); | |
108 | fw_offset += part_size; | |
109 | } | |
110 | ||
111 | if ((!part_final) || (part_size != 0)) { | |
112 | sr_err("Missing final part."); | |
113 | goto out; | |
114 | } | |
115 | ||
116 | ret = SR_OK; | |
117 | ||
118 | sr_info("Firmware upload done."); | |
119 | ||
120 | out: | |
121 | if (hdl) | |
122 | libusb_close(hdl); | |
123 | ||
124 | g_free(firmware); | |
125 | ||
126 | return ret; | |
127 | } | |
128 | ||
129 | static gboolean scan_firmware(libusb_device *dev) | |
130 | { | |
131 | struct libusb_device_descriptor des; | |
132 | struct libusb_device_handle *hdl; | |
133 | gboolean ret; | |
134 | unsigned char strdesc[64]; | |
135 | ||
136 | hdl = NULL; | |
137 | ret = FALSE; | |
138 | ||
139 | libusb_get_device_descriptor(dev, &des); | |
140 | ||
141 | if (libusb_open(dev, &hdl) != 0) | |
142 | goto out; | |
143 | ||
144 | if (libusb_get_string_descriptor_ascii(hdl, | |
145 | des.iManufacturer, strdesc, sizeof(strdesc)) < 0) | |
146 | goto out; | |
147 | if (strcmp((const char *)strdesc, "Saleae")) | |
148 | goto out; | |
149 | ||
150 | if (libusb_get_string_descriptor_ascii(hdl, | |
151 | des.iProduct, strdesc, sizeof(strdesc)) < 0) | |
152 | goto out; | |
153 | if (strcmp((const char *)strdesc, "Logic Pro")) | |
154 | goto out; | |
155 | ||
156 | ret = TRUE; | |
157 | ||
158 | out: | |
159 | if (hdl) | |
160 | libusb_close(hdl); | |
161 | ||
162 | return ret; | |
163 | } | |
164 | ||
165 | static GSList *scan(struct sr_dev_driver *di, GSList *options) | |
166 | { | |
167 | struct drv_context *drvc; | |
168 | struct dev_context *devc; | |
169 | struct sr_dev_inst *sdi; | |
170 | GSList *devices, *conn_devices; | |
171 | libusb_device **devlist; | |
172 | struct libusb_device_descriptor des; | |
173 | const char *conn; | |
174 | char connection_id[64]; | |
175 | gboolean fw_loaded = FALSE; | |
176 | ||
177 | devices = NULL; | |
178 | conn_devices = NULL; | |
179 | drvc = di->context; | |
180 | drvc->instances = NULL; | |
181 | ||
182 | conn = NULL; | |
183 | for (GSList *l = options; l; l = l->next) { | |
184 | struct sr_config *src = l->data; | |
185 | ||
186 | switch (src->key) { | |
187 | case SR_CONF_CONN: | |
188 | conn = g_variant_get_string(src->data, NULL); | |
189 | break; | |
190 | } | |
191 | } | |
192 | ||
193 | libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); | |
194 | for (unsigned int i = 0; devlist[i]; i++) { | |
195 | libusb_get_device_descriptor(devlist[i], &des); | |
196 | ||
197 | if (des.idVendor != 0x21a9 || des.idProduct != 0x1006) | |
198 | continue; | |
199 | ||
200 | if (!scan_firmware(devlist[i])) { | |
201 | const char *fwname; | |
202 | sr_info("Found a Logic Pro 16 device (no firmware loaded)."); | |
203 | fwname = "saleae-logicpro16-fx3.fw"; | |
204 | if (upload_firmware(drvc->sr_ctx, devlist[i], | |
205 | fwname) != SR_OK) { | |
206 | sr_err("Firmware upload failed, name %s.", fwname); | |
207 | continue; | |
208 | }; | |
209 | fw_loaded = TRUE; | |
210 | } | |
211 | ||
212 | } | |
213 | if (fw_loaded) { | |
214 | /* Give the device some time to come back and scan again */ | |
215 | libusb_free_device_list(devlist, 1); | |
216 | g_usleep(500 * 1000); | |
217 | libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); | |
218 | } | |
219 | if (conn) | |
220 | conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn); | |
221 | for (unsigned int i = 0; devlist[i]; i++) { | |
222 | if (conn_devices) { | |
223 | struct sr_usb_dev_inst *usb = NULL; | |
224 | GSList *l; | |
225 | ||
226 | for (l = conn_devices; l; l = l->next) { | |
227 | usb = l->data; | |
228 | if (usb->bus == libusb_get_bus_number(devlist[i]) | |
229 | && usb->address == libusb_get_device_address(devlist[i])) | |
230 | break; | |
231 | } | |
232 | if (!l) | |
233 | /* This device matched none of the ones that | |
234 | * matched the conn specification. */ | |
235 | continue; | |
236 | } | |
237 | ||
238 | libusb_get_device_descriptor(devlist[i], &des); | |
239 | ||
240 | if (des.idVendor != 0x21a9 || des.idProduct != 0x1006) | |
241 | continue; | |
242 | ||
243 | if (usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)) < 0) | |
244 | continue; | |
245 | ||
246 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); | |
247 | sdi->status = SR_ST_INITIALIZING; | |
248 | sdi->vendor = g_strdup("Saleae"); | |
249 | sdi->model = g_strdup("Logic Pro 16"); | |
250 | sdi->connection_id = g_strdup(connection_id); | |
251 | ||
252 | for (unsigned int j = 0; j < ARRAY_SIZE(channel_names); j++) | |
253 | sr_channel_new(sdi, j, SR_CHANNEL_LOGIC, TRUE, | |
254 | channel_names[j]); | |
255 | ||
256 | sr_dbg("Found a Logic Pro 16 device."); | |
257 | sdi->status = SR_ST_INACTIVE; | |
258 | sdi->inst_type = SR_INST_USB; | |
259 | sdi->conn = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]), | |
260 | libusb_get_device_address(devlist[i]), NULL); | |
261 | ||
262 | devc = g_malloc0(sizeof(struct dev_context)); | |
263 | sdi->priv = devc; | |
264 | devices = g_slist_append(devices, sdi); | |
265 | ||
266 | } | |
267 | g_slist_free_full(conn_devices, (GDestroyNotify)sr_usb_dev_inst_free); | |
268 | libusb_free_device_list(devlist, 1); | |
269 | ||
270 | return std_scan_complete(di, devices); | |
271 | } | |
272 | ||
273 | static int dev_open(struct sr_dev_inst *sdi) | |
274 | { | |
275 | struct sr_dev_driver *di = sdi->driver; | |
276 | struct drv_context *drvc = di->context; | |
277 | struct dev_context *devc = sdi->priv; | |
278 | struct sr_usb_dev_inst *usb = sdi->conn; | |
279 | int ret; | |
280 | ||
281 | if (sr_usb_open(drvc->sr_ctx->libusb_ctx, usb) != SR_OK) | |
282 | return SR_ERR; | |
283 | ||
284 | if ((ret = libusb_claim_interface(usb->devhdl, 0))) { | |
285 | sr_err("Failed to claim interface: %s.", libusb_error_name(ret)); | |
286 | return SR_ERR; | |
287 | } | |
288 | ||
289 | /* Configure default samplerate. */ | |
290 | if (devc->dig_samplerate == 0) | |
291 | devc->dig_samplerate = samplerates[3]; | |
292 | ||
293 | return saleae_logic_pro_init(sdi); | |
294 | } | |
295 | ||
296 | static int dev_close(struct sr_dev_inst *sdi) | |
297 | { | |
298 | sr_usb_close(sdi->conn); | |
299 | ||
300 | return SR_OK; | |
301 | } | |
302 | ||
303 | static int config_get(uint32_t key, GVariant **data, | |
304 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
305 | { | |
306 | struct sr_usb_dev_inst *usb; | |
307 | struct dev_context *devc; | |
308 | ||
309 | (void)cg; | |
310 | ||
311 | switch (key) { | |
312 | case SR_CONF_CONN: | |
313 | if (!sdi || !sdi->conn) | |
314 | return SR_ERR_ARG; | |
315 | usb = sdi->conn; | |
316 | *data = g_variant_new_printf("%d.%d", usb->bus, usb->address); | |
317 | break; | |
318 | case SR_CONF_SAMPLERATE: | |
319 | if (!sdi) | |
320 | return SR_ERR; | |
321 | devc = sdi->priv; | |
322 | *data = g_variant_new_uint64(devc->dig_samplerate); | |
323 | break; | |
324 | default: | |
325 | return SR_ERR_NA; | |
326 | } | |
327 | ||
328 | return SR_OK; | |
329 | } | |
330 | ||
331 | static int config_set(uint32_t key, GVariant *data, | |
332 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
333 | { | |
334 | struct dev_context *devc; | |
335 | ||
336 | (void)cg; | |
337 | ||
338 | devc = sdi->priv; | |
339 | ||
340 | switch (key) { | |
341 | case SR_CONF_SAMPLERATE: | |
342 | devc->dig_samplerate = g_variant_get_uint64(data); | |
343 | break; | |
344 | default: | |
345 | return SR_ERR_NA; | |
346 | } | |
347 | ||
348 | return SR_OK; | |
349 | } | |
350 | ||
351 | static int config_list(uint32_t key, GVariant **data, | |
352 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
353 | { | |
354 | switch (key) { | |
355 | case SR_CONF_SCAN_OPTIONS: | |
356 | case SR_CONF_DEVICE_OPTIONS: | |
357 | return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts); | |
358 | case SR_CONF_SAMPLERATE: | |
359 | *data = std_gvar_samplerates(ARRAY_AND_SIZE(samplerates)); | |
360 | break; | |
361 | default: | |
362 | return SR_ERR_NA; | |
363 | } | |
364 | ||
365 | return SR_OK; | |
366 | } | |
367 | ||
368 | static void dev_acquisition_abort(const struct sr_dev_inst *sdi) | |
369 | { | |
370 | struct dev_context *devc = sdi->priv; | |
371 | unsigned int i; | |
372 | ||
373 | for (i = 0; i < devc->num_transfers; i++) { | |
374 | if (devc->transfers[i]) | |
375 | libusb_cancel_transfer(devc->transfers[i]); | |
376 | } | |
377 | } | |
378 | ||
379 | static int dev_acquisition_handle(int fd, int revents, void *cb_data) | |
380 | { | |
381 | struct sr_dev_inst *sdi = cb_data; | |
382 | struct drv_context *drvc = sdi->driver->context; | |
383 | struct timeval tv = ALL_ZERO; | |
384 | ||
385 | (void)fd; | |
386 | ||
387 | libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv); | |
388 | ||
389 | /* Handle timeout */ | |
390 | if (!revents) | |
391 | sr_dev_acquisition_stop(sdi); | |
392 | ||
393 | return TRUE; | |
394 | } | |
395 | ||
396 | static int dev_acquisition_start(const struct sr_dev_inst *sdi) | |
397 | { | |
398 | struct dev_context *devc = sdi->priv; | |
399 | struct drv_context *drvc = sdi->driver->context; | |
400 | struct libusb_transfer *transfer; | |
401 | struct sr_usb_dev_inst *usb; | |
402 | uint8_t *buf; | |
403 | unsigned int i, ret; | |
404 | ||
405 | ret = saleae_logic_pro_prepare(sdi); | |
406 | if (ret != SR_OK) | |
407 | return ret; | |
408 | ||
409 | usb = sdi->conn; | |
410 | ||
411 | devc->conv_buffer = g_malloc(CONV_BUFFER_SIZE); | |
412 | ||
413 | devc->num_transfers = BUF_COUNT; | |
414 | devc->transfers = g_malloc0(sizeof(*devc->transfers) * BUF_COUNT); | |
415 | for (i = 0; i < devc->num_transfers; i++) { | |
416 | buf = g_malloc(BUF_SIZE); | |
417 | transfer = libusb_alloc_transfer(0); | |
418 | libusb_fill_bulk_transfer(transfer, usb->devhdl, | |
419 | 2 | LIBUSB_ENDPOINT_IN, buf, BUF_SIZE, | |
420 | saleae_logic_pro_receive_data, (void *)sdi, 0); | |
421 | if ((ret = libusb_submit_transfer(transfer)) != 0) { | |
422 | sr_err("Failed to submit transfer: %s.", | |
423 | libusb_error_name(ret)); | |
424 | libusb_free_transfer(transfer); | |
425 | g_free(buf); | |
426 | dev_acquisition_abort(sdi); | |
427 | return SR_ERR; | |
428 | } | |
429 | devc->transfers[i] = transfer; | |
430 | devc->submitted_transfers++; | |
431 | } | |
432 | ||
433 | usb_source_add(sdi->session, drvc->sr_ctx, BUF_TIMEOUT, dev_acquisition_handle, (void *)sdi); | |
434 | ||
435 | std_session_send_df_header(sdi); | |
436 | ||
437 | saleae_logic_pro_start(sdi); | |
438 | if (ret != SR_OK) | |
439 | return ret; | |
440 | ||
441 | return SR_OK; | |
442 | } | |
443 | ||
444 | static int dev_acquisition_stop(struct sr_dev_inst *sdi) | |
445 | { | |
446 | struct dev_context *devc = sdi->priv; | |
447 | struct drv_context *drvc = sdi->driver->context; | |
448 | ||
449 | saleae_logic_pro_stop(sdi); | |
450 | ||
451 | std_session_send_df_end(sdi); | |
452 | ||
453 | usb_source_remove(sdi->session, drvc->sr_ctx); | |
454 | ||
455 | g_free(devc->conv_buffer); | |
456 | ||
457 | return SR_OK; | |
458 | } | |
459 | ||
460 | static struct sr_dev_driver saleae_logic_pro_driver_info = { | |
461 | .name = "saleae-logic-pro", | |
462 | .longname = "Saleae Logic Pro", | |
463 | .api_version = 1, | |
464 | .init = std_init, | |
465 | .cleanup = std_cleanup, | |
466 | .scan = scan, | |
467 | .dev_list = std_dev_list, | |
468 | .dev_clear = std_dev_clear, | |
469 | .config_get = config_get, | |
470 | .config_set = config_set, | |
471 | .config_list = config_list, | |
472 | .dev_open = dev_open, | |
473 | .dev_close = dev_close, | |
474 | .dev_acquisition_start = dev_acquisition_start, | |
475 | .dev_acquisition_stop = dev_acquisition_stop, | |
476 | .context = NULL, | |
477 | }; | |
478 | SR_REGISTER_DEV_DRIVER(saleae_logic_pro_driver_info); |