2 * This file is part of the sigrok project.
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
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.
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.
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/>.
29 #include "sigrok-internal.h"
32 static struct fx2lafw_profile supported_fx2[] = {
34 { 0x08a9, 0x0014, "CWAV", "USBee AX", NULL, 8 },
38 static int fx2lafw_capabilities[] = {
39 SR_HWCAP_LOGIC_ANALYZER,
42 /* These are really implemented in the driver, not the hardware. */
43 SR_HWCAP_LIMIT_SAMPLES,
48 static const char *fx2lafw_probe_names[] = {
60 static uint64_t fx2lafw_supported_samplerates[] = {
72 static struct sr_samplerates fx2lafw_samplerates = {
76 fx2lafw_supported_samplerates
79 static GSList *dev_insts = NULL;
80 static libusb_context *usb_context = NULL;
83 * Check the USB configuration to determine if this is an fx2lafw device.
85 * @return true if the device's configuration profile match fx2lafw
86 * configuration, flase otherwise.
88 static bool check_conf_profile(libusb_device *dev)
90 struct libusb_device_descriptor des;
91 struct libusb_config_descriptor *conf_dsc = NULL;
92 const struct libusb_interface_descriptor *intf_dsc;
96 /* Assume it's not a Saleae Logic unless proven wrong. */
99 if (libusb_get_device_descriptor(dev, &des) != 0)
102 if (des.bNumConfigurations != 1)
103 /* Need exactly 1 configuration. */
106 if (libusb_get_config_descriptor(dev, 0, &conf_dsc) != 0)
109 if (conf_dsc->bNumInterfaces != 1)
110 /* Need exactly 1 interface. */
113 if (conf_dsc->interface[0].num_altsetting != 1)
114 /* Need just one alternate setting. */
117 intf_dsc = &(conf_dsc->interface[0].altsetting[0]);
118 if (intf_dsc->bNumEndpoints != 3)
119 /* Need exactly 3 end points. */
122 if ((intf_dsc->endpoint[0].bEndpointAddress & 0x8f) !=
123 (1 | LIBUSB_ENDPOINT_OUT))
124 /* The first endpoint should be 1 (outbound). */
127 if ((intf_dsc->endpoint[1].bEndpointAddress & 0x8f) !=
128 (2 | LIBUSB_ENDPOINT_IN))
129 /* The second endpoint should be 2 (inbound). */
132 /* TODO: Check the debug channel... */
134 /* If we made it here, it must be an fx2lafw. */
139 libusb_free_config_descriptor(conf_dsc);
144 static struct fx2lafw_device* fx2lafw_device_new(void)
146 struct fx2lafw_device *fx2lafw;
148 if (!(fx2lafw = g_try_malloc0(sizeof(struct fx2lafw_device)))) {
149 sr_err("fx2lafw: %s: fx2lafw_device malloc failed", __func__);
160 static int hw_init(const char *deviceinfo)
162 struct sr_dev_inst *sdi;
163 struct libusb_device_descriptor des;
164 struct fx2lafw_profile *fx2lafw_prof;
165 struct fx2lafw_device *ctx;
166 libusb_device **devlist;
171 /* Avoid compiler warnings. */
174 if (libusb_init(&usb_context) != 0) {
175 sr_warn("Failed to initialize USB.");
179 /* Find all fx2lafw compatible devices and upload firware to all of them. */
180 libusb_get_device_list(usb_context, &devlist);
181 for (i = 0; devlist[i]; i++) {
183 if ((err = libusb_get_device_descriptor(
184 devlist[i], &des)) != 0) {
185 sr_warn("failed to get device descriptor: %d", err);
190 for (j = 0; supported_fx2[j].vid; j++) {
191 if (des.idVendor == supported_fx2[j].vid &&
192 des.idProduct == supported_fx2[j].pid) {
193 fx2lafw_prof = &supported_fx2[j];
197 /* Skip if the device was not found */
201 sdi = sr_dev_inst_new(devcnt, SR_ST_INITIALIZING,
202 fx2lafw_prof->vendor, fx2lafw_prof->model,
203 fx2lafw_prof->model_version);
207 ctx = fx2lafw_device_new();
208 ctx->profile = fx2lafw_prof;
210 device_instances = g_slist_append(dev_insts, sdi);
212 if (check_conf_profile(devlist[i])) {
213 /* Already has the firmware, so fix the new address. */
214 sr_dbg("fx2lafw: Found a fx2lafw device.");
215 sdi->status = SR_ST_INACTIVE;
216 ctx->usb = sr_usb_dev_inst_new
217 (libusb_get_bus_number(devlist[i]),
218 libusb_get_device_address(devlist[i]), NULL);
220 if (ezusb_upload_firmware(devlist[i], USB_CONFIGURATION, FIRMWARE) == SR_OK)
221 /* Remember when the firmware on this device was updated */
222 g_get_current_time(&ctx->fw_updated);
224 sr_err("fx2lafw: firmware upload failed for "
225 "device %d", devcnt);
226 ctx->usb = sr_usb_dev_inst_new
227 (libusb_get_bus_number(devlist[i]), 0xff, NULL);
232 libusb_free_device_list(devlist, 1);
237 static int hw_dev_open(int device_index)
243 static int hw_dev_close(int device_index)
249 static int hw_cleanup(void)
252 struct sr_dev_inst *sdi;
254 for(l = device_instances; l; l = l->next) {
256 sr_dev_inst_free(sdi);
259 g_slist_free(device_instances);
260 device_instances = NULL;
263 libusb_exit(usb_context);
269 static void *hw_dev_info_get(int device_index, int device_info_id)
271 struct sr_dev_inst *sdi;
272 struct fx2lafw_device *ctx;
274 if (!(sdi = sr_dev_inst_get(dev_insts, device_index)))
278 switch (device_info_id) {
281 case SR_DI_NUM_PROBES:
282 return GINT_TO_POINTER(ctx->profile->num_probes);
283 case SR_DI_PROBE_NAMES:
284 return fx2lafw_probe_names;
285 case SR_DI_SAMPLERATES:
286 return &fx2lafw_samplerates;
287 case SR_DI_TRIGGER_TYPES:
288 return TRIGGER_TYPES;
294 static int hw_dev_status_get(int device_index)
296 const struct sr_dev_inst *const sdi =
297 sr_dev_inst_get(dev_insts, device_index);
300 return SR_ST_NOT_FOUND;
305 static int *hw_hwcap_get_all(void)
307 return fx2lafw_capabilities;
310 static int hw_dev_config_set(int dev_index, int capability, void *value)
318 static int hw_dev_acquisition_start(int dev_index, gpointer session_data)
325 /* This stops acquisition on ALL devices, ignoring device_index. */
326 static int hw_dev_acquisition_stop(int dev_index, gpointer session_data)
333 SR_PRIV struct sr_dev_plugin fx2lafw_plugin_info = {
335 .longname = "fx2lafw",
338 .cleanup = hw_cleanup,
339 .dev_open = hw_dev_open,
340 .dev_close = hw_dev_close,
341 .dev_info_get = hw_dev_info_get,
342 .dev_status_get = hw_dev_status_get,
343 .hwcap_get_all = hw_hwcap_get_all,
344 .dev_config_set = hw_dev_config_set,
345 .dev_acquisition_start = hw_dev_acquisition_start,
346 .dev_acquisition_stop = hw_dev_acquisition_stop,