]> sigrok.org Git - libsigrok.git/blame_incremental - src/ezusb.c
output/csv: use intermediate time_t var, silence compiler warning
[libsigrok.git] / src / ezusb.c
... / ...
CommitLineData
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com>
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/*
21 * Helper functions for the Cypress EZ-USB / FX2 series chips.
22 */
23
24#include <config.h>
25#include <libusb.h>
26#include <glib.h>
27#include <glib/gstdio.h>
28#include <stdio.h>
29#include <errno.h>
30#include <string.h>
31#include <libsigrok/libsigrok.h>
32#include "libsigrok-internal.h"
33
34#define LOG_PREFIX "ezusb"
35
36#define FW_CHUNKSIZE (4 * 1024)
37
38SR_PRIV int ezusb_reset(struct libusb_device_handle *hdl, int set_clear)
39{
40 int ret;
41 unsigned char buf[1];
42
43 sr_info("setting CPU reset mode %s...",
44 set_clear ? "on" : "off");
45 buf[0] = set_clear ? 1 : 0;
46 ret = libusb_control_transfer(hdl, LIBUSB_REQUEST_TYPE_VENDOR, 0xa0,
47 0xe600, 0x0000, buf, 1, 100);
48 if (ret < 0)
49 sr_err("Unable to send control request: %s.",
50 libusb_error_name(ret));
51
52 return ret;
53}
54
55SR_PRIV int ezusb_install_firmware(struct sr_context *ctx,
56 libusb_device_handle *hdl,
57 const char *name)
58{
59 unsigned char *firmware;
60 size_t length, offset, chunksize;
61 int ret, result;
62
63 /* Max size is 64 kiB since the value field of the setup packet,
64 * which holds the firmware offset, is only 16 bit wide.
65 */
66 firmware = sr_resource_load(ctx, SR_RESOURCE_FIRMWARE,
67 name, &length, 1 << 16);
68 if (!firmware)
69 return SR_ERR;
70
71 sr_info("Uploading firmware '%s'.", name);
72
73 result = SR_OK;
74 offset = 0;
75 while (offset < length) {
76 chunksize = MIN(length - offset, FW_CHUNKSIZE);
77
78 ret = libusb_control_transfer(hdl, LIBUSB_REQUEST_TYPE_VENDOR |
79 LIBUSB_ENDPOINT_OUT, 0xa0, offset,
80 0x0000, firmware + offset,
81 chunksize, 100);
82 if (ret < 0) {
83 sr_err("Unable to send firmware to device: %s.",
84 libusb_error_name(ret));
85 g_free(firmware);
86 return SR_ERR;
87 }
88 sr_info("Uploaded %zu bytes.", chunksize);
89 offset += chunksize;
90 }
91 g_free(firmware);
92
93 sr_info("Firmware upload done.");
94
95 return result;
96}
97
98SR_PRIV int ezusb_upload_firmware(struct sr_context *ctx, libusb_device *dev,
99 int configuration, const char *name)
100{
101 struct libusb_device_handle *hdl;
102 int ret;
103
104 sr_info("uploading firmware to device on %d.%d",
105 libusb_get_bus_number(dev), libusb_get_device_address(dev));
106
107 if ((ret = libusb_open(dev, &hdl)) < 0) {
108 sr_err("failed to open device: %s.", libusb_error_name(ret));
109 return SR_ERR;
110 }
111
112/*
113 * The libusb Darwin backend is broken: it can report a kernel driver being
114 * active, but detaching it always returns an error.
115 */
116#if !defined(__APPLE__)
117 if (libusb_kernel_driver_active(hdl, 0) == 1) {
118 if ((ret = libusb_detach_kernel_driver(hdl, 0)) < 0) {
119 sr_err("failed to detach kernel driver: %s",
120 libusb_error_name(ret));
121 return SR_ERR;
122 }
123 }
124#endif
125
126 if ((ret = libusb_set_configuration(hdl, configuration)) < 0) {
127 sr_err("Unable to set configuration: %s",
128 libusb_error_name(ret));
129 return SR_ERR;
130 }
131
132 if ((ezusb_reset(hdl, 1)) < 0)
133 return SR_ERR;
134
135 if (ezusb_install_firmware(ctx, hdl, name) < 0)
136 return SR_ERR;
137
138 if ((ezusb_reset(hdl, 0)) < 0)
139 return SR_ERR;
140
141 libusb_close(hdl);
142
143 return SR_OK;
144}