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