]> sigrok.org Git - libsigrok.git/blob - hardware/saleae-logic16/api.c
saleae-logic16: Reworked scan/open to handle FW upload.
[libsigrok.git] / hardware / saleae-logic16 / api.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2013 Marcus Comstedt <marcus@mc.pp.se>
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 <glib.h>
21 #include <libusb.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include "libsigrok.h"
25 #include "libsigrok-internal.h"
26 #include "protocol.h"
27
28 #define LOGIC16_VID 0x21a9
29 #define LOGIC16_PID 0x1001
30 #define NUM_PROBES  16
31
32 #define USB_INTERFACE           0
33 #define USB_CONFIGURATION       1
34 #define FX2_FIRMWARE            FIRMWARE_DIR "/saleae-logic16-fx2.fw"
35
36 #define MAX_RENUM_DELAY_MS      3000
37
38
39 SR_PRIV struct sr_dev_driver saleae_logic16_driver_info;
40 static struct sr_dev_driver *di = &saleae_logic16_driver_info;
41
42 static const char *probe_names[NUM_PROBES + 1] = {
43         "0", "1", "2", "3", "4", "5", "6", "7", "8",
44         "9", "10", "11", "12", "13", "14", "15",
45         NULL,
46 };
47
48 static int init(struct sr_context *sr_ctx)
49 {
50         return std_init(sr_ctx, di, LOG_PREFIX);
51 }
52
53 static gboolean check_conf_profile(libusb_device *dev)
54 {
55         struct libusb_device_descriptor des;
56         struct libusb_device_handle *hdl;
57         gboolean ret;
58         unsigned char strdesc[64];
59
60         hdl = NULL;
61         ret = FALSE;
62         while (!ret) {
63                 /* Assume the FW has not been loaded, unless proven wrong. */
64                 if (libusb_get_device_descriptor(dev, &des) != 0)
65                         break;
66
67                 if (libusb_open(dev, &hdl) != 0)
68                         break;
69
70                 if (libusb_get_string_descriptor_ascii(hdl,
71                     des.iManufacturer, strdesc, sizeof(strdesc)) < 0)
72                         break;
73                 if (strcmp((const char *)strdesc, "Saleae LLC"))
74                         break;
75
76                 if (libusb_get_string_descriptor_ascii(hdl,
77                                 des.iProduct, strdesc, sizeof(strdesc)) < 0)
78                         break;
79                 if (strcmp((const char *)strdesc, "Logic S/16"))
80                         break;
81
82                 /* If we made it here, it must be a configured Logic16. */
83                 ret = TRUE;
84         }
85         if (hdl)
86                 libusb_close(hdl);
87
88         return ret;
89 }
90
91 static GSList *scan(GSList *options)
92 {
93         struct drv_context *drvc;
94         struct dev_context *devc;
95         struct sr_dev_inst *sdi;
96         struct sr_usb_dev_inst *usb;
97         struct sr_probe *probe;
98         struct sr_config *src;
99         GSList *l, *devices, *conn_devices;
100         struct libusb_device_descriptor des;
101         libusb_device **devlist;
102         int devcnt, ret, i, j;
103         const char *conn;
104
105         drvc = di->priv;
106
107         conn = NULL;
108         for (l = options; l; l = l->next) {
109                 src = l->data;
110                 switch (src->key) {
111                 case SR_CONF_CONN:
112                         conn = g_variant_get_string(src->data, NULL);
113                         break;
114                 }
115         }
116         if (conn)
117                 conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn);
118         else
119                 conn_devices = NULL;
120
121         /* Find all Logic16 devices and upload firmware to them. */
122         devices = NULL;
123         libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
124         for (i = 0; devlist[i]; i++) {
125                 if (conn) {
126                         usb = NULL;
127                         for (l = conn_devices; l; l = l->next) {
128                                 usb = l->data;
129                                 if (usb->bus == libusb_get_bus_number(devlist[i])
130                                     && usb->address == libusb_get_device_address(devlist[i]))
131                                         break;
132                         }
133                         if (!l)
134                                 /* This device matched none of the ones that
135                                  * matched the conn specification. */
136                                 continue;
137                 }
138
139                 if ((ret = libusb_get_device_descriptor( devlist[i], &des)) != 0) {
140                         sr_warn("Failed to get device descriptor: %s.",
141                                 libusb_error_name(ret));
142                         continue;
143                 }
144
145                 if (des.idVendor != LOGIC16_VID || des.idProduct != LOGIC16_PID)
146                         continue;
147
148                 devcnt = g_slist_length(drvc->instances);
149                 sdi = sr_dev_inst_new(devcnt, SR_ST_INITIALIZING,
150                                       "Saleae", "Logic16", NULL);
151                 if (!sdi)
152                         return NULL;
153                 sdi->driver = di;
154
155                 for (j = 0; probe_names[j]; j++) {
156                         if (!(probe = sr_probe_new(j, SR_PROBE_LOGIC, TRUE,
157                                                    probe_names[j])))
158                                 return NULL;
159                         sdi->probes = g_slist_append(sdi->probes, probe);
160                 }
161
162                 if (!(devc = g_try_malloc0(sizeof(struct dev_context))))
163                         return NULL;
164                 sdi->priv = devc;
165                 drvc->instances = g_slist_append(drvc->instances, sdi);
166                 devices = g_slist_append(devices, sdi);
167
168                 if (check_conf_profile(devlist[i])) {
169                         /* Already has the firmware, so fix the new address. */
170                         sr_dbg("Found a Logic16 device.");
171                         sdi->status = SR_ST_INACTIVE;
172                         sdi->inst_type = SR_INST_USB;
173                         sdi->conn = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]),
174                                                         libusb_get_device_address(devlist[i]), NULL);
175                 } else {
176                         if (ezusb_upload_firmware(devlist[i], USB_CONFIGURATION,
177                                                   FX2_FIRMWARE) == SR_OK)
178                                 /* Store when this device's FW was updated. */
179                                 devc->fw_updated = g_get_monotonic_time();
180                         else
181                                 sr_err("Firmware upload failed for "
182                                        "device %d.", devcnt);
183                         sdi->inst_type = SR_INST_USB;
184                         sdi->conn = sr_usb_dev_inst_new (libusb_get_bus_number(devlist[i]),
185                                                          0xff, NULL);
186                 }
187         }
188         libusb_free_device_list(devlist, 1);
189         g_slist_free_full(conn_devices, (GDestroyNotify)sr_usb_dev_inst_free);
190
191         return devices;
192 }
193
194 static GSList *dev_list(void)
195 {
196         struct drv_context *drvc;
197
198         drvc = di->priv;
199
200         return drvc->instances;
201 }
202
203 static int dev_clear(void)
204 {
205         return std_dev_clear(di, NULL);
206 }
207
208 static int logic16_dev_open(struct sr_dev_inst *sdi)
209 {
210         libusb_device **devlist;
211         struct sr_usb_dev_inst *usb;
212         struct libusb_device_descriptor des;
213         struct dev_context *devc;
214         struct drv_context *drvc;
215         int ret, skip, i, device_count;
216
217         drvc = di->priv;
218         devc = sdi->priv;
219         usb = sdi->conn;
220
221         if (sdi->status == SR_ST_ACTIVE)
222                 /* Device is already in use. */
223                 return SR_ERR;
224
225         skip = 0;
226         device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
227         if (device_count < 0) {
228                 sr_err("Failed to get device list: %s.",
229                        libusb_error_name(device_count));
230                 return SR_ERR;
231         }
232
233         for (i = 0; i < device_count; i++) {
234                 if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
235                         sr_err("Failed to get device descriptor: %s.",
236                                libusb_error_name(ret));
237                         continue;
238                 }
239
240                 if (des.idVendor != LOGIC16_VID
241                     || des.idProduct != LOGIC16_PID)
242                         continue;
243
244                 if (sdi->status == SR_ST_INITIALIZING) {
245                         if (skip != sdi->index) {
246                                 /* Skip devices of this type that aren't the one we want. */
247                                 skip += 1;
248                                 continue;
249                         }
250                 } else if (sdi->status == SR_ST_INACTIVE) {
251                         /*
252                          * This device is fully enumerated, so we need to find
253                          * this device by vendor, product, bus and address.
254                          */
255                         if (libusb_get_bus_number(devlist[i]) != usb->bus
256                                 || libusb_get_device_address(devlist[i]) != usb->address)
257                                 /* This is not the one. */
258                                 continue;
259                 }
260
261                 if (!(ret = libusb_open(devlist[i], &usb->devhdl))) {
262                         if (usb->address == 0xff)
263                                 /*
264                                  * First time we touch this device after FW
265                                  * upload, so we don't know the address yet.
266                                  */
267                                 usb->address = libusb_get_device_address(devlist[i]);
268                 } else {
269                         sr_err("Failed to open device: %s.",
270                                libusb_error_name(ret));
271                         break;
272                 }
273
274                 sdi->status = SR_ST_ACTIVE;
275                 sr_info("Opened device %d on %d.%d, "
276                         "interface %d.",
277                         sdi->index, usb->bus, usb->address,
278                         USB_INTERFACE);
279
280                 break;
281         }
282         libusb_free_device_list(devlist, 1);
283
284         if (sdi->status != SR_ST_ACTIVE)
285                 return SR_ERR;
286
287         return SR_OK;
288 }
289
290 static int dev_open(struct sr_dev_inst *sdi)
291 {
292         struct sr_usb_dev_inst *usb;
293         struct dev_context *devc;
294         int ret;
295         int64_t timediff_us, timediff_ms;
296
297         devc = sdi->priv;
298         usb = sdi->conn;
299
300         /*
301          * If the firmware was recently uploaded, wait up to MAX_RENUM_DELAY_MS
302          * milliseconds for the FX2 to renumerate.
303          */
304         ret = SR_ERR;
305         if (devc->fw_updated > 0) {
306                 sr_info("Waiting for device to reset.");
307                 /* Takes >= 300ms for the FX2 to be gone from the USB bus. */
308                 g_usleep(300 * 1000);
309                 timediff_ms = 0;
310                 while (timediff_ms < MAX_RENUM_DELAY_MS) {
311                         if ((ret = logic16_dev_open(sdi)) == SR_OK)
312                                 break;
313                         g_usleep(100 * 1000);
314
315                         timediff_us = g_get_monotonic_time() - devc->fw_updated;
316                         timediff_ms = timediff_us / 1000;
317                         sr_spew("Waited %" PRIi64 "ms.", timediff_ms);
318                 }
319                 if (ret != SR_OK) {
320                         sr_err("Device failed to renumerate.");
321                         return SR_ERR;
322                 }
323                 sr_info("Device came back after %" PRIi64 "ms.", timediff_ms);
324         } else {
325                 sr_info("Firmware upload was not needed.");
326                 ret = logic16_dev_open(sdi);
327         }
328
329         if (ret != SR_OK) {
330                 sr_err("Unable to open device.");
331                 return SR_ERR;
332         }
333
334         ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE);
335         if (ret != 0) {
336                 switch(ret) {
337                 case LIBUSB_ERROR_BUSY:
338                         sr_err("Unable to claim USB interface. Another "
339                                "program or driver has already claimed it.");
340                         break;
341                 case LIBUSB_ERROR_NO_DEVICE:
342                         sr_err("Device has been disconnected.");
343                         break;
344                 default:
345                         sr_err("Unable to claim interface: %s.",
346                                libusb_error_name(ret));
347                         break;
348                 }
349
350                 return SR_ERR;
351         }
352
353         if (devc->cur_samplerate == 0) {
354                 /* Samplerate hasn't been set; default to the slowest one. */
355                 devc->cur_samplerate = 500000;
356         }
357
358         return SR_OK;
359 }
360
361 static int dev_close(struct sr_dev_inst *sdi)
362 {
363         struct sr_usb_dev_inst *usb;
364
365         usb = sdi->conn;
366         if (usb->devhdl == NULL)
367                 return SR_ERR;
368
369         sr_info("Closing device %d on %d.%d interface %d.",
370                 sdi->index, usb->bus, usb->address, USB_INTERFACE);
371         libusb_release_interface(usb->devhdl, USB_INTERFACE);
372         libusb_close(usb->devhdl);
373         usb->devhdl = NULL;
374         sdi->status = SR_ST_INACTIVE;
375
376         return SR_OK;
377 }
378
379 static int cleanup(void)
380 {
381         int ret;
382         struct drv_context *drvc;
383
384         if (!(drvc = di->priv))
385                 /* Can get called on an unused driver, doesn't matter. */
386                 return SR_OK;
387
388         ret = dev_clear();
389         g_free(drvc);
390         di->priv = NULL;
391
392         return ret;
393 }
394
395 static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi)
396 {
397         int ret;
398
399         (void)sdi;
400         (void)data;
401
402         ret = SR_OK;
403         switch (key) {
404         /* TODO */
405         default:
406                 return SR_ERR_NA;
407         }
408
409         return ret;
410 }
411
412 static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi)
413 {
414         int ret;
415
416         (void)data;
417
418         if (sdi->status != SR_ST_ACTIVE)
419                 return SR_ERR_DEV_CLOSED;
420
421         ret = SR_OK;
422         switch (key) {
423         /* TODO */
424         default:
425                 ret = SR_ERR_NA;
426         }
427
428         return ret;
429 }
430
431 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi)
432 {
433         int ret;
434
435         (void)sdi;
436         (void)data;
437
438         ret = SR_OK;
439         switch (key) {
440         /* TODO */
441         default:
442                 return SR_ERR_NA;
443         }
444
445         return ret;
446 }
447
448 static int dev_acquisition_start(const struct sr_dev_inst *sdi,
449                                     void *cb_data)
450 {
451         (void)sdi;
452         (void)cb_data;
453
454         if (sdi->status != SR_ST_ACTIVE)
455                 return SR_ERR_DEV_CLOSED;
456
457         /* TODO: configure hardware, reset acquisition state, set up
458          * callbacks and send header packet. */
459
460         return SR_OK;
461 }
462
463 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
464 {
465         (void)cb_data;
466
467         if (sdi->status != SR_ST_ACTIVE)
468                 return SR_ERR_DEV_CLOSED;
469
470         /* TODO: stop acquisition. */
471
472         return SR_OK;
473 }
474
475 SR_PRIV struct sr_dev_driver saleae_logic16_driver_info = {
476         .name = "saleae-logic16",
477         .longname = "Saleae Logic16",
478         .api_version = 1,
479         .init = init,
480         .cleanup = cleanup,
481         .scan = scan,
482         .dev_list = dev_list,
483         .dev_clear = dev_clear,
484         .config_get = config_get,
485         .config_set = config_set,
486         .config_list = config_list,
487         .dev_open = dev_open,
488         .dev_close = dev_close,
489         .dev_acquisition_start = dev_acquisition_start,
490         .dev_acquisition_stop = dev_acquisition_stop,
491         .priv = NULL,
492 };