]> sigrok.org Git - libsigrok.git/blob - hardware/fx2lafw/fx2lafw.c
fx2lafw: Implemented hw_dev_open
[libsigrok.git] / hardware / fx2lafw / fx2lafw.c
1 /*
2  * This file is part of the sigrok project.
3  *
4  * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
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 <stdio.h>
21 #include <stdlib.h>
22 #include <stdbool.h>
23 #include <inttypes.h>
24 #include <glib.h>
25 #include <libusb.h>
26
27 #include "config.h"
28 #include "sigrok.h"
29 #include "sigrok-internal.h"
30 #include "fx2lafw.h"
31
32 static struct fx2lafw_profile supported_fx2[] = {
33         /* USBee AX */
34         { 0x08a9, 0x0014, "CWAV", "USBee AX", NULL, 8 },
35         { 0, 0, 0, 0, 0, 0 }
36 };
37
38 static int fx2lafw_capabilities[] = {
39         SR_HWCAP_LOGIC_ANALYZER,
40         SR_HWCAP_SAMPLERATE,
41
42         /* These are really implemented in the driver, not the hardware. */
43         SR_HWCAP_LIMIT_SAMPLES,
44         SR_HWCAP_CONTINUOUS,
45         0
46 };
47
48 static const char *fx2lafw_probe_names[] = {
49         "D0",
50         "D1",
51         "D2",
52         "D3",
53         "D4",
54         "D5",
55         "D6",
56         "D7",
57         NULL
58 };
59
60 static uint64_t fx2lafw_supported_samplerates[] = {
61         SR_MHZ(1),
62         SR_MHZ(2),
63         SR_MHZ(3),
64         SR_MHZ(4),
65         SR_MHZ(6),
66         SR_MHZ(8),
67         SR_MHZ(12),
68         SR_MHZ(16),
69         SR_MHZ(24)
70 };
71
72 static struct sr_samplerates fx2lafw_samplerates = {
73         SR_MHZ(1),
74         SR_MHZ(24),
75         SR_HZ(0),
76         fx2lafw_supported_samplerates
77 };
78
79 static GSList *dev_insts = NULL;
80 static libusb_context *usb_context = NULL;
81
82 /**
83  * Check the USB configuration to determine if this is an fx2lafw device.
84  *
85  * @return true if the device's configuration profile match fx2lafw
86  *         configuration, flase otherwise.
87  */
88 static bool check_conf_profile(libusb_device *dev)
89 {
90         struct libusb_device_descriptor des;
91         struct libusb_config_descriptor *conf_dsc = NULL;
92         const struct libusb_interface_descriptor *intf_dsc;
93         bool ret = false;
94
95         while (!ret) {
96                 /* Assume it's not a Saleae Logic unless proven wrong. */
97                 ret = 0;
98
99                 if (libusb_get_device_descriptor(dev, &des) != 0)
100                         break;
101
102                 if (des.bNumConfigurations != 1)
103                         /* Need exactly 1 configuration. */
104                         break;
105
106                 if (libusb_get_config_descriptor(dev, 0, &conf_dsc) != 0)
107                         break;
108
109                 if (conf_dsc->bNumInterfaces != 1)
110                         /* Need exactly 1 interface. */
111                         break;
112
113                 if (conf_dsc->interface[0].num_altsetting != 1)
114                         /* Need just one alternate setting. */
115                         break;
116
117                 intf_dsc = &(conf_dsc->interface[0].altsetting[0]);
118                 if (intf_dsc->bNumEndpoints != 3)
119                         /* Need exactly 3 end points. */
120                         break;
121
122                 if ((intf_dsc->endpoint[0].bEndpointAddress & 0x8f) !=
123                     (1 | LIBUSB_ENDPOINT_OUT))
124                         /* The first endpoint should be 1 (outbound). */
125                         break;
126
127                 if ((intf_dsc->endpoint[1].bEndpointAddress & 0x8f) !=
128                     (2 | LIBUSB_ENDPOINT_IN))
129                         /* The second endpoint should be 2 (inbound). */
130                         break;
131
132                 /* TODO: Check the debug channel... */
133
134                 /* If we made it here, it must be an fx2lafw. */
135                 ret = true;
136         }
137
138         if (conf_dsc)
139                 libusb_free_config_descriptor(conf_dsc);
140
141         return ret;
142 }
143
144 static int fx2lafw_open_dev(int dev_index)
145 {
146         libusb_device **devlist;
147         struct libusb_device_descriptor des;
148         struct sr_dev_inst *sdi;
149         struct fx2lafw_device *ctx;
150         int err, skip, i;
151
152         if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
153                 return SR_ERR;
154         ctx = sdi->priv;
155
156         if (sdi->status == SR_ST_ACTIVE)
157                 /* already in use */
158                 return SR_ERR;
159
160         skip = 0;
161         libusb_get_device_list(usb_context, &devlist);
162         for (i = 0; devlist[i]; i++) {
163                 if ((err = libusb_get_device_descriptor(devlist[i], &des))) {
164                         sr_err("fx2lafw: failed to get device descriptor: %d", err);
165                         continue;
166                 }
167
168                 if (des.idVendor != FIRMWARE_VID
169                     || des.idProduct != FIRMWARE_PID)
170                         continue;
171
172                 if (sdi->status == SR_ST_INITIALIZING) {
173                         if (skip != dev_index) {
174                                 /* Skip devices of this type that aren't the one we want. */
175                                 skip += 1;
176                                 continue;
177                         }
178                 } else if (sdi->status == SR_ST_INACTIVE) {
179                         /*
180                          * This device is fully enumerated, so we need to find
181                          * this device by vendor, product, bus and address.
182                          */
183                         if (libusb_get_bus_number(devlist[i]) != ctx->usb->bus
184                                 || libusb_get_device_address(devlist[i]) != ctx->usb->address)
185                                 /* this is not the one */
186                                 continue;
187                 }
188
189                 if (!(err = libusb_open(devlist[i], &ctx->usb->devhdl))) {
190                         if (ctx->usb->address == 0xff)
191                                 /*
192                                  * first time we touch this device after firmware upload,
193                                  * so we don't know the address yet.
194                                  */
195                                 ctx->usb->address = libusb_get_device_address(devlist[i]);
196
197                         sdi->status = SR_ST_ACTIVE;
198                         sr_info("fx2lafw: opened device %d on %d.%d interface %d",
199                                 sdi->index, ctx->usb->bus,
200                                 ctx->usb->address, USB_INTERFACE);
201                 } else {
202                         sr_err("fx2lafw: failed to open device: %d", err);
203                 }
204
205                 /* if we made it here, we handled the device one way or another */
206                 break;
207         }
208         libusb_free_device_list(devlist, 1);
209
210         if (sdi->status != SR_ST_ACTIVE)
211                 return SR_ERR;
212
213         return SR_OK;
214 }
215
216 static struct fx2lafw_device* fx2lafw_device_new(void)
217 {
218         struct fx2lafw_device *fx2lafw;
219
220         if (!(fx2lafw = g_try_malloc0(sizeof(struct fx2lafw_device)))) {
221                 sr_err("fx2lafw: %s: fx2lafw_device malloc failed", __func__);
222                 return NULL;
223         }
224
225         return fx2lafw;
226 }
227
228 /*
229  * API callbacks
230  */
231
232 static int hw_init(const char *deviceinfo)
233 {
234         struct sr_dev_inst *sdi;
235         struct libusb_device_descriptor des;
236         struct fx2lafw_profile *fx2lafw_prof;
237         struct fx2lafw_device *ctx;
238         libusb_device **devlist;
239         int err;
240         int devcnt = 0;
241         int i, j;
242
243         /* Avoid compiler warnings. */
244         (void)deviceinfo;
245
246         if (libusb_init(&usb_context) != 0) {
247                 sr_warn("Failed to initialize USB.");
248                 return 0;
249         }
250
251         /* Find all fx2lafw compatible devices and upload firware to all of them. */
252         libusb_get_device_list(usb_context, &devlist);
253         for (i = 0; devlist[i]; i++) {
254
255                 if ((err = libusb_get_device_descriptor(
256                         devlist[i], &des)) != 0) {
257                         sr_warn("failed to get device descriptor: %d", err);
258                         continue;
259                 }
260
261                 fx2lafw_prof = NULL;
262                 for (j = 0; supported_fx2[j].vid; j++) {
263                         if (des.idVendor == supported_fx2[j].vid &&
264                                 des.idProduct == supported_fx2[j].pid) {
265                                 fx2lafw_prof = &supported_fx2[j];
266                         }
267                 }
268
269                 /* Skip if the device was not found */
270                 if(!fx2lafw_prof)
271                         continue;
272
273                 sdi = sr_dev_inst_new(devcnt, SR_ST_INITIALIZING,
274                         fx2lafw_prof->vendor, fx2lafw_prof->model,
275                         fx2lafw_prof->model_version);
276                 if(!sdi)
277                         return 0;
278
279                 ctx = fx2lafw_device_new();
280                 ctx->profile = fx2lafw_prof;
281                 sdi->priv = ctx;
282                 device_instances = g_slist_append(dev_insts, sdi);
283
284                 if (check_conf_profile(devlist[i])) {
285                         /* Already has the firmware, so fix the new address. */
286                         sr_dbg("fx2lafw: Found a fx2lafw device.");
287                         sdi->status = SR_ST_INACTIVE;
288                         ctx->usb = sr_usb_dev_inst_new
289                             (libusb_get_bus_number(devlist[i]),
290                              libusb_get_device_address(devlist[i]), NULL);
291                 } else {
292                         if (ezusb_upload_firmware(devlist[i], USB_CONFIGURATION, FIRMWARE) == SR_OK)
293                                 /* Remember when the firmware on this device was updated */
294                                 g_get_current_time(&ctx->fw_updated);
295                         else
296                                 sr_err("fx2lafw: firmware upload failed for "
297                                        "device %d", devcnt);
298                         ctx->usb = sr_usb_dev_inst_new
299                                 (libusb_get_bus_number(devlist[i]), 0xff, NULL);
300                 }
301
302                 devcnt++;
303         }
304         libusb_free_device_list(devlist, 1);
305
306         return devcnt;
307 }
308
309 static int hw_dev_open(int device_index)
310 {
311         GTimeVal cur_time;
312         struct sr_dev_inst *sdi;
313         struct fx2lafw_device *ctx;
314         int timediff, err;
315
316         if (!(sdi = sr_dev_inst_get(dev_insts, device_index)))
317                 return SR_ERR;
318         ctx = sdi->priv;
319
320         /*
321          * if the firmware was recently uploaded, wait up to MAX_RENUM_DELAY ms
322          * for the FX2 to renumerate
323          */
324         err = 0;
325         if (GTV_TO_MSEC(ctx->fw_updated) > 0) {
326                 sr_info("fx2lafw: waiting for device to reset");
327                 /* takes at least 300ms for the FX2 to be gone from the USB bus */
328                 g_usleep(300 * 1000);
329                 timediff = 0;
330                 while (timediff < MAX_RENUM_DELAY) {
331                         if ((err = fx2lafw_open_dev(device_index)) == SR_OK)
332                                 break;
333                         g_usleep(100 * 1000);
334                         g_get_current_time(&cur_time);
335                         timediff = GTV_TO_MSEC(cur_time) - GTV_TO_MSEC(ctx->fw_updated);
336                 }
337                 sr_info("fx2lafw: device came back after %d ms", timediff);
338         } else {
339                 err = fx2lafw_open_dev(device_index);
340         }
341
342         if (err != SR_OK) {
343                 sr_err("fx2lafw: unable to open device");
344                 return SR_ERR;
345         }
346         ctx = sdi->priv;
347
348         err = libusb_claim_interface(ctx->usb->devhdl, USB_INTERFACE);
349         if (err != 0) {
350                 sr_err("fx2lafw: Unable to claim interface: %d", err);
351                 return SR_ERR;
352         }
353
354         return SR_OK;
355 }
356
357 static int hw_dev_close(int device_index)
358 {
359         (void)device_index;
360         return SR_OK;
361 }
362
363 static int hw_cleanup(void)
364 {
365         GSList *l;
366         struct sr_dev_inst *sdi;
367
368         for(l = device_instances; l; l = l->next) {
369                 sdi = l->data;
370                 sr_dev_inst_free(sdi);
371         }
372
373         g_slist_free(device_instances);
374         device_instances = NULL;
375
376         if(usb_context)
377                 libusb_exit(usb_context);
378         usb_context = NULL;
379
380         return SR_OK;
381 }
382
383 static void *hw_dev_info_get(int device_index, int device_info_id)
384 {
385         struct sr_dev_inst *sdi;
386         struct fx2lafw_device *ctx;
387
388         if (!(sdi = sr_dev_inst_get(dev_insts, device_index)))
389                 return NULL;
390         ctx = sdi->priv;
391
392         switch (device_info_id) {
393         case SR_DI_INST:
394                 return sdi;
395         case SR_DI_NUM_PROBES:
396                 return GINT_TO_POINTER(ctx->profile->num_probes);
397         case SR_DI_PROBE_NAMES:
398                 return fx2lafw_probe_names;
399         case SR_DI_SAMPLERATES:
400                 return &fx2lafw_samplerates;
401         case SR_DI_TRIGGER_TYPES:
402                 return TRIGGER_TYPES;
403         }
404
405         return NULL;
406 }
407
408 static int hw_dev_status_get(int device_index)
409 {
410         const struct sr_dev_inst *const sdi =
411                 sr_dev_inst_get(dev_insts, device_index);
412
413         if (!sdi)
414                 return SR_ST_NOT_FOUND;
415
416         return sdi->status;
417 }
418
419 static int *hw_hwcap_get_all(void)
420 {
421         return fx2lafw_capabilities;
422 }
423
424 static int hw_dev_config_set(int dev_index, int capability, void *value)
425 {
426         (void)dev_index;
427         (void)capability;
428         (void)value;
429         return SR_OK;
430 }
431
432 static int hw_dev_acquisition_start(int dev_index, gpointer session_data)
433 {
434         (void)dev_index;
435         (void)session_data;
436         return SR_OK;
437 }
438
439 /* This stops acquisition on ALL devices, ignoring device_index. */
440 static int hw_dev_acquisition_stop(int dev_index, gpointer session_data)
441 {
442         (void)dev_index;
443         (void)session_data;
444         return SR_OK;
445 }
446
447 SR_PRIV struct sr_dev_plugin fx2lafw_plugin_info = {
448         .name = "fx2lafw",
449         .longname = "fx2lafw",
450         .api_version = 1,
451         .init = hw_init,
452         .cleanup = hw_cleanup,
453         .dev_open = hw_dev_open,
454         .dev_close = hw_dev_close,
455         .dev_info_get = hw_dev_info_get,
456         .dev_status_get = hw_dev_status_get,
457         .hwcap_get_all = hw_hwcap_get_all,
458         .dev_config_set = hw_dev_config_set,
459         .dev_acquisition_start = hw_dev_acquisition_start,
460         .dev_acquisition_stop = hw_dev_acquisition_stop,
461 };