]> sigrok.org Git - libsigrok.git/blame - src/hardware/chromium-twinkie/api.c
CHROMIUM: Add driver for Chromium Twinkie dongle
[libsigrok.git] / src / hardware / chromium-twinkie / api.c
CommitLineData
744e43bc
VP
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright 2014 Google, Inc
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 2 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>
21#include <libusb.h>
22#include <stdlib.h>
23#include <string.h>
24#include <math.h>
25#include <libsigrok/libsigrok.h>
26#include "libsigrok-internal.h"
27#include "protocol.h"
28
29#define TWINKIE_VID 0x18d1
30#define TWINKIE_PID 0x500a
31
32#define USB_INTERFACE 1
33#define USB_CONFIGURATION 1
34
35#define MAX_RENUM_DELAY_MS 3000
36#define NUM_SIMUL_TRANSFERS 32
37
38#define SAMPLE_RATE SR_KHZ(2400)
39
40static const int32_t hwopts[] = {
41 SR_CONF_CONN,
42};
43
44static const int32_t hwcaps[] = {
45 SR_CONF_LOGIC_ANALYZER,
46 SR_CONF_CONTINUOUS,
47 SR_CONF_CONN | SR_CONF_GET,
48 SR_CONF_SAMPLERATE | SR_CONF_GET,
49 SR_CONF_LIMIT_SAMPLES | SR_CONF_SET,
50};
51
52static const struct chan {
53 char *name;
54 int type;
55} chan_defs[] = {
56 {"CC1", SR_CHANNEL_LOGIC},
57 {"CC2", SR_CHANNEL_LOGIC},
58
59 {NULL, 0}
60};
61
62static GSList *scan(struct sr_dev_driver *di, GSList *options)
63{
64 struct drv_context *drvc;
65 struct dev_context *devc;
66 struct sr_dev_inst *sdi;
67 struct sr_usb_dev_inst *usb;
68 struct sr_channel *ch;
69 struct sr_config *src;
70 GSList *l, *devices, *conn_devices;
71 struct libusb_device_descriptor des;
72 libusb_device **devlist;
73 int ret, i, j;
74 const char *conn;
75 char connection_id[64];
76
77 drvc = di->context;
78
79 conn = NULL;
80 for (l = options; l; l = l->next) {
81 src = l->data;
82 switch (src->key) {
83 case SR_CONF_CONN:
84 conn = g_variant_get_string(src->data, NULL);
85 break;
86 }
87 }
88 if (conn)
89 conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn);
90 else
91 conn_devices = NULL;
92
93 /* Find all Twinkie devices */
94 devices = NULL;
95 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
96 for (i = 0; devlist[i]; i++) {
97 if (conn) {
98 usb = NULL;
99 for (l = conn_devices; l; l = l->next) {
100 usb = l->data;
101 if (usb->bus == libusb_get_bus_number(devlist[i])
102 && usb->address == libusb_get_device_address(devlist[i]))
103 break;
104 }
105 if (!l)
106 /* This device matched none of the ones that
107 * matched the conn specification. */
108 continue;
109 }
110
111 if ((ret = libusb_get_device_descriptor(devlist[i], &des)) != 0) {
112 sr_warn("Failed to get device descriptor: %s.",
113 libusb_error_name(ret));
114 continue;
115 }
116
117 if (des.idVendor != TWINKIE_VID || des.idProduct != TWINKIE_PID)
118 continue;
119
120 usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
121
122 sdi = g_malloc0(sizeof(struct sr_dev_inst));
123 sdi->status = SR_ST_INITIALIZING;
124 sdi->vendor = g_strdup("Chromium");
125 sdi->model = g_strdup("Twinkie");
126 sdi->driver = di;
127 sdi->connection_id = g_strdup(connection_id);
128
129 for (j = 0; chan_defs[j].name; j++)
130 if (!(ch = sr_channel_new(sdi, j, chan_defs[j].type,
131 TRUE, chan_defs[j].name)))
132 return NULL;
133
134 if (!(devc = g_try_malloc0(sizeof(struct dev_context))))
135 return NULL;
136 sdi->priv = devc;
137 drvc->instances = g_slist_append(drvc->instances, sdi);
138 devices = g_slist_append(devices, sdi);
139
140 sr_dbg("Found a Twinkie dongle.");
141 sdi->status = SR_ST_INACTIVE;
142 sdi->inst_type = SR_INST_USB;
143 sdi->conn = sr_usb_dev_inst_new(
144 libusb_get_bus_number(devlist[i]),
145 libusb_get_device_address(devlist[i]), NULL);
146 }
147 libusb_free_device_list(devlist, 1);
148 g_slist_free_full(conn_devices, (GDestroyNotify)sr_usb_dev_inst_free);
149
150 return devices;
151}
152
153static int twinkie_dev_open(struct sr_dev_inst *sdi)
154{
155 struct sr_dev_driver *di;
156 libusb_device **devlist;
157 struct sr_usb_dev_inst *usb;
158 struct libusb_device_descriptor des;
159 struct drv_context *drvc;
160 int ret, i, device_count;
161 char connection_id[64];
162
163 di = sdi->driver;
164 drvc = di->context;
165 usb = sdi->conn;
166
167 if (sdi->status == SR_ST_ACTIVE)
168 /* Device is already in use. */
169 return SR_ERR;
170
171 device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
172 if (device_count < 0) {
173 sr_err("Failed to get device list: %s.",
174 libusb_error_name(device_count));
175 return SR_ERR;
176 }
177
178 for (i = 0; i < device_count; i++) {
179 if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
180 sr_err("Failed to get device descriptor: %s.",
181 libusb_error_name(ret));
182 continue;
183 }
184
185 if (des.idVendor != TWINKIE_VID || des.idProduct != TWINKIE_PID)
186 continue;
187
188 if ((sdi->status == SR_ST_INITIALIZING) ||
189 (sdi->status == SR_ST_INACTIVE)) {
190 /*
191 * Check device by its physical USB bus/port address.
192 */
193 usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
194 if (strcmp(sdi->connection_id, connection_id))
195 /* This is not the one. */
196 continue;
197 }
198
199 if (!(ret = libusb_open(devlist[i], &usb->devhdl))) {
200 if (usb->address == 0xff)
201 /*
202 * First time we touch this device after FW
203 * upload, so we don't know the address yet.
204 */
205 usb->address = libusb_get_device_address(devlist[i]);
206 } else {
207 sr_err("Failed to open device: %s.",
208 libusb_error_name(ret));
209 break;
210 }
211
212 ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE);
213 if (ret == LIBUSB_ERROR_BUSY) {
214 sr_err("Unable to claim USB interface. Another "
215 "program or driver has already claimed it.");
216 break;
217 } else if (ret == LIBUSB_ERROR_NO_DEVICE) {
218 sr_err("Device has been disconnected.");
219 break;
220 } else if (ret != 0) {
221 sr_err("Unable to claim interface: %s.",
222 libusb_error_name(ret));
223 break;
224 }
225
226 if ((ret = twinkie_init_device(sdi)) != SR_OK) {
227 sr_err("Failed to init device.");
228 break;
229 }
230
231 sdi->status = SR_ST_ACTIVE;
232 sr_info("Opened device %d.%d, interface %d.",
233 usb->bus, usb->address, USB_INTERFACE);
234
235 break;
236 }
237 libusb_free_device_list(devlist, 1);
238
239 if (sdi->status != SR_ST_ACTIVE) {
240 if (usb->devhdl) {
241 libusb_release_interface(usb->devhdl, USB_INTERFACE);
242 libusb_close(usb->devhdl);
243 usb->devhdl = NULL;
244 }
245 return SR_ERR;
246 }
247
248 return SR_OK;
249}
250
251static int dev_open(struct sr_dev_inst *sdi)
252{
253 int ret;
254
255 ret = twinkie_dev_open(sdi);
256 if (ret != SR_OK) {
257 sr_err("Unable to open device.");
258 return SR_ERR;
259 }
260
261 return SR_OK;
262}
263
264static int dev_close(struct sr_dev_inst *sdi)
265{
266 struct sr_usb_dev_inst *usb;
267
268 usb = sdi->conn;
269 if (usb->devhdl == NULL)
270 return SR_ERR;
271
272 sr_info("Closing device %d.%d interface %d.",
273 usb->bus, usb->address, USB_INTERFACE);
274 libusb_release_interface(usb->devhdl, USB_INTERFACE);
275 libusb_close(usb->devhdl);
276 usb->devhdl = NULL;
277 sdi->status = SR_ST_INACTIVE;
278
279 return SR_OK;
280}
281
282static int dev_clear(const struct sr_dev_driver *di)
283{
284 return std_dev_clear(di, NULL);
285}
286
287static int config_get(uint32_t key, GVariant **data,
288 const struct sr_dev_inst *sdi,
289 const struct sr_channel_group *cg)
290{
291 struct sr_usb_dev_inst *usb;
292 char str[128];
293 int ret;
294
295 (void)cg;
296
297 ret = SR_OK;
298 switch (key) {
299 case SR_CONF_CONN:
300 if (!sdi || !sdi->conn)
301 return SR_ERR_ARG;
302 usb = sdi->conn;
303 if (usb->address == 255)
304 /* Device still needs to re-enumerate after firmware
305 * upload, so we don't know its (future) address. */
306 return SR_ERR;
307 snprintf(str, 128, "%d.%d", usb->bus, usb->address);
308 *data = g_variant_new_string(str);
309 break;
310 case SR_CONF_SAMPLERATE:
311 *data = g_variant_new_uint64(SAMPLE_RATE);
312 break;
313 default:
314 return SR_ERR_NA;
315 }
316
317 return ret;
318}
319
320static int config_set(uint32_t key, GVariant *data,
321 const struct sr_dev_inst *sdi,
322 const struct sr_channel_group *cg)
323{
324 struct dev_context *devc;
325 int ret;
326
327 (void)cg;
328
329 if (sdi->status != SR_ST_ACTIVE)
330 return SR_ERR_DEV_CLOSED;
331
332 devc = sdi->priv;
333
334 ret = SR_OK;
335 switch (key) {
336 case SR_CONF_LIMIT_SAMPLES:
337 devc->limit_samples = g_variant_get_uint64(data);
338 break;
339 default:
340 ret = SR_ERR_NA;
341 }
342
343 return ret;
344}
345
346static int config_list(uint32_t key, GVariant **data,
347 const struct sr_dev_inst *sdi,
348 const struct sr_channel_group *cg)
349{
350 int ret;
351
352 (void)sdi;
353 (void)cg;
354
355 ret = SR_OK;
356 switch (key) {
357 case SR_CONF_SCAN_OPTIONS:
358 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
359 hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t));
360 break;
361 case SR_CONF_DEVICE_OPTIONS:
362 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
363 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
364 break;
365 default:
366 return SR_ERR_NA;
367 }
368
369 return ret;
370}
371
372static void abort_acquisition(struct dev_context *devc)
373{
374 int i;
375
376 devc->sent_samples = -1;
377
378 for (i = devc->num_transfers - 1; i >= 0; i--) {
379 if (devc->transfers[i])
380 libusb_cancel_transfer(devc->transfers[i]);
381 }
382}
383
384static int receive_data(int fd, int revents, void *cb_data)
385{
386 struct timeval tv;
387 struct dev_context *devc;
388 struct drv_context *drvc;
389 const struct sr_dev_inst *sdi;
390 struct sr_dev_driver *di;
391
392 (void)fd;
393 (void)revents;
394
395 sdi = cb_data;
396 di = sdi->driver;
397 drvc = di->context;
398 devc = sdi->priv;
399
400 tv.tv_sec = tv.tv_usec = 0;
401 libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv);
402
403 if (devc->sent_samples == -2) {
404 abort_acquisition(devc);
405 }
406
407 return TRUE;
408}
409
410static int dev_acquisition_start(const struct sr_dev_inst *sdi)
411{
412 struct sr_dev_driver *di = sdi->driver;
413 struct dev_context *devc;
414 struct drv_context *drvc;
415 struct sr_usb_dev_inst *usb;
416 struct libusb_transfer *transfer;
417 unsigned int i, timeout, num_transfers;
418 int ret;
419 unsigned char *buf;
420 size_t size, convsize;
421
422 if (sdi->status != SR_ST_ACTIVE)
423 return SR_ERR_DEV_CLOSED;
424
425 drvc = di->context;
426 devc = sdi->priv;
427 usb = sdi->conn;
428
429 devc->sent_samples = 0;
430 /* reset per-CC context */
431 memset(devc->cc, 0, sizeof(devc->cc));
432
433 timeout = 1000;
434 num_transfers = 10;
435 size = 10*1024;
436 convsize = size * 8 * 256 /* largest size : only rollbacks/no edges */;
437 devc->submitted_transfers = 0;
438
439 devc->convbuffer_size = convsize;
440 if (!(devc->convbuffer = g_try_malloc(convsize))) {
441 sr_err("Conversion buffer malloc failed.");
442 return SR_ERR_MALLOC;
443 }
444 memset(devc->convbuffer, 0, devc->convbuffer_size);
445
446 devc->transfers = g_try_malloc0(sizeof(*devc->transfers) * num_transfers);
447 if (!devc->transfers) {
448 sr_err("USB transfers malloc failed.");
449 g_free(devc->convbuffer);
450 return SR_ERR_MALLOC;
451 }
452
453 devc->num_transfers = num_transfers;
454 for (i = 0; i < num_transfers; i++) {
455 if (!(buf = g_try_malloc(size))) {
456 sr_err("USB transfer buffer malloc failed.");
457 if (devc->submitted_transfers)
458 abort_acquisition(devc);
459 else {
460 g_free(devc->transfers);
461 g_free(devc->convbuffer);
462 }
463 return SR_ERR_MALLOC;
464 }
465 transfer = libusb_alloc_transfer(0);
466 libusb_fill_bulk_transfer(transfer, usb->devhdl,
467 3 | LIBUSB_ENDPOINT_IN, buf, size,
468 twinkie_receive_transfer, (void *)sdi, timeout);
469 if ((ret = libusb_submit_transfer(transfer)) != 0) {
470 sr_err("Failed to submit transfer: %s.",
471 libusb_error_name(ret));
472 libusb_free_transfer(transfer);
473 g_free(buf);
474 abort_acquisition(devc);
475 return SR_ERR;
476 }
477 devc->transfers[i] = transfer;
478 devc->submitted_transfers++;
479 }
480
481 devc->ctx = drvc->sr_ctx;
482
483 usb_source_add(sdi->session, devc->ctx, timeout, receive_data,
484 (void *)sdi);
485
486 /* Send header packet to the session bus. */
487 std_session_send_df_header(sdi);
488
489 if ((ret = twinkie_start_acquisition(sdi)) != SR_OK) {
490 abort_acquisition(devc);
491 return ret;
492 }
493
494 return SR_OK;
495}
496
497static int dev_acquisition_stop(struct sr_dev_inst *sdi)
498{
499 if (sdi->status != SR_ST_ACTIVE)
500 return SR_ERR_DEV_CLOSED;
501
502 abort_acquisition(sdi->priv);
503
504 return SR_OK;
505}
506
507static struct sr_dev_driver chromium_twinkie_driver_info = {
508 .name = "chromium-twinkie",
509 .longname = "Chromium Twinkie",
510 .api_version = 1,
511 .init = std_init,
512 .cleanup = std_cleanup,
513 .scan = scan,
514 .dev_list = std_dev_list,
515 .dev_clear = dev_clear,
516 .config_get = config_get,
517 .config_set = config_set,
518 .config_list = config_list,
519 .dev_open = dev_open,
520 .dev_close = dev_close,
521 .dev_acquisition_start = dev_acquisition_start,
522 .dev_acquisition_stop = dev_acquisition_stop,
523};
524SR_REGISTER_DEV_DRIVER(chromium_twinkie_driver_info);