]> sigrok.org Git - libsigrok.git/blob - hardware/fx2lafw/fx2lafw.c
fx2lafw: Implemented firmware upload
[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 struct fx2lafw_device* fx2lafw_device_new(void)
145 {
146         struct fx2lafw_device *fx2lafw;
147
148         if (!(fx2lafw = g_try_malloc0(sizeof(struct fx2lafw_device)))) {
149                 sr_err("fx2lafw: %s: fx2lafw_device malloc failed", __func__);
150                 return NULL;
151         }
152
153         return fx2lafw;
154 }
155
156 /*
157  * API callbacks
158  */
159
160 static int hw_init(const char *deviceinfo)
161 {
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;
167         int err;
168         int devcnt = 0;
169         int i, j;
170
171         /* Avoid compiler warnings. */
172         (void)deviceinfo;
173
174         if (libusb_init(&usb_context) != 0) {
175                 sr_warn("Failed to initialize USB.");
176                 return 0;
177         }
178
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++) {
182
183                 if ((err = libusb_get_device_descriptor(
184                         devlist[i], &des)) != 0) {
185                         sr_warn("failed to get device descriptor: %d", err);
186                         continue;
187                 }
188
189                 fx2lafw_prof = NULL;
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];
194                         }
195                 }
196
197                 /* Skip if the device was not found */
198                 if(!fx2lafw_prof)
199                         continue;
200
201                 sdi = sr_dev_inst_new(devcnt, SR_ST_INITIALIZING,
202                         fx2lafw_prof->vendor, fx2lafw_prof->model,
203                         fx2lafw_prof->model_version);
204                 if(!sdi)
205                         return 0;
206
207                 ctx = fx2lafw_device_new();
208                 ctx->profile = fx2lafw_prof;
209                 sdi->priv = ctx;
210                 device_instances = g_slist_append(dev_insts, sdi);
211
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);
219                 } else {
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);
223                         else
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);
228                 }
229
230                 devcnt++;
231         }
232         libusb_free_device_list(devlist, 1);
233
234         return devcnt;
235 }
236
237 static int hw_dev_open(int device_index)
238 {
239         (void)device_index;
240         return SR_OK;
241 }
242
243 static int hw_dev_close(int device_index)
244 {
245         (void)device_index;
246         return SR_OK;
247 }
248
249 static int hw_cleanup(void)
250 {
251         GSList *l;
252         struct sr_dev_inst *sdi;
253
254         for(l = device_instances; l; l = l->next) {
255                 sdi = l->data;
256                 sr_dev_inst_free(sdi);
257         }
258
259         g_slist_free(device_instances);
260         device_instances = NULL;
261
262         if(usb_context)
263                 libusb_exit(usb_context);
264         usb_context = NULL;
265
266         return SR_OK;
267 }
268
269 static void *hw_dev_info_get(int device_index, int device_info_id)
270 {
271         struct sr_dev_inst *sdi;
272         struct fx2lafw_device *ctx;
273
274         if (!(sdi = sr_dev_inst_get(dev_insts, device_index)))
275                 return NULL;
276         ctx = sdi->priv;
277
278         switch (device_info_id) {
279         case SR_DI_INST:
280                 return sdi;
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;
289         }
290
291         return NULL;
292 }
293
294 static int hw_dev_status_get(int device_index)
295 {
296         const struct sr_dev_inst *const sdi =
297                 sr_dev_inst_get(dev_insts, device_index);
298
299         if (!sdi)
300                 return SR_ST_NOT_FOUND;
301
302         return sdi->status;
303 }
304
305 static int *hw_hwcap_get_all(void)
306 {
307         return fx2lafw_capabilities;
308 }
309
310 static int hw_dev_config_set(int dev_index, int capability, void *value)
311 {
312         (void)dev_index;
313         (void)capability;
314         (void)value;
315         return SR_OK;
316 }
317
318 static int hw_dev_acquisition_start(int dev_index, gpointer session_data)
319 {
320         (void)dev_index;
321         (void)session_data;
322         return SR_OK;
323 }
324
325 /* This stops acquisition on ALL devices, ignoring device_index. */
326 static int hw_dev_acquisition_stop(int dev_index, gpointer session_data)
327 {
328         (void)dev_index;
329         (void)session_data;
330         return SR_OK;
331 }
332
333 SR_PRIV struct sr_dev_plugin fx2lafw_plugin_info = {
334         .name = "fx2lafw",
335         .longname = "fx2lafw",
336         .api_version = 1,
337         .init = hw_init,
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,
347 };