]> sigrok.org Git - libsigrok.git/blame - src/ezusb.c
output/csv: use intermediate time_t var, silence compiler warning
[libsigrok.git] / src / ezusb.c
CommitLineData
a1bb33af 1/*
50985c20 2 * This file is part of the libsigrok project.
a1bb33af 3 *
c73d2ea4 4 * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com>
a1bb33af
UH
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
6ec6c43b 24#include <config.h>
a1bb33af
UH
25#include <libusb.h>
26#include <glib.h>
3bbd9849 27#include <glib/gstdio.h>
a1bb33af
UH
28#include <stdio.h>
29#include <errno.h>
30#include <string.h>
c1aae900 31#include <libsigrok/libsigrok.h>
45c59c8b 32#include "libsigrok-internal.h"
a1bb33af 33
3544f848 34#define LOG_PREFIX "ezusb"
d458a0ac 35
8e2d6c9d
DE
36#define FW_CHUNKSIZE (4 * 1024)
37
1a081ca6 38SR_PRIV int ezusb_reset(struct libusb_device_handle *hdl, int set_clear)
a1bb33af 39{
ebc34738 40 int ret;
a1bb33af
UH
41 unsigned char buf[1];
42
d458a0ac 43 sr_info("setting CPU reset mode %s...",
7b48d6e1 44 set_clear ? "on" : "off");
a1bb33af 45 buf[0] = set_clear ? 1 : 0;
ebc34738 46 ret = libusb_control_transfer(hdl, LIBUSB_REQUEST_TYPE_VENDOR, 0xa0,
9d2933fb 47 0xe600, 0x0000, buf, 1, 100);
ebc34738 48 if (ret < 0)
1740429d 49 sr_err("Unable to send control request: %s.",
d4928d71 50 libusb_error_name(ret));
a1bb33af 51
ebc34738 52 return ret;
a1bb33af
UH
53}
54
8e2d6c9d
DE
55SR_PRIV int ezusb_install_firmware(struct sr_context *ctx,
56 libusb_device_handle *hdl,
57 const char *name)
a1bb33af 58{
8e2d6c9d
DE
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)
6d754b6d 69 return SR_ERR;
8e2d6c9d
DE
70
71 sr_info("Uploading firmware '%s'.", name);
a1bb33af 72
6d754b6d
BV
73 result = SR_OK;
74 offset = 0;
8e2d6c9d
DE
75 while (offset < length) {
76 chunksize = MIN(length - offset, FW_CHUNKSIZE);
77
ebc34738 78 ret = libusb_control_transfer(hdl, LIBUSB_REQUEST_TYPE_VENDOR |
986f7270 79 LIBUSB_ENDPOINT_OUT, 0xa0, offset,
8e2d6c9d
DE
80 0x0000, firmware + offset,
81 chunksize, 100);
ebc34738 82 if (ret < 0) {
1740429d 83 sr_err("Unable to send firmware to device: %s.",
d4928d71 84 libusb_error_name(ret));
8e2d6c9d
DE
85 g_free(firmware);
86 return SR_ERR;
a1bb33af 87 }
8e2d6c9d 88 sr_info("Uploaded %zu bytes.", chunksize);
a1bb33af
UH
89 offset += chunksize;
90 }
8e2d6c9d
DE
91 g_free(firmware);
92
93 sr_info("Firmware upload done.");
a1bb33af
UH
94
95 return result;
96}
edf60d05 97
8e2d6c9d
DE
98SR_PRIV int ezusb_upload_firmware(struct sr_context *ctx, libusb_device *dev,
99 int configuration, const char *name)
edf60d05
UH
100{
101 struct libusb_device_handle *hdl;
ebc34738 102 int ret;
edf60d05 103
d458a0ac 104 sr_info("uploading firmware to device on %d.%d",
7b48d6e1 105 libusb_get_bus_number(dev), libusb_get_device_address(dev));
edf60d05 106
ebc34738 107 if ((ret = libusb_open(dev, &hdl)) < 0) {
1740429d 108 sr_err("failed to open device: %s.", libusb_error_name(ret));
6d754b6d 109 return SR_ERR;
edf60d05
UH
110 }
111
3f98bf70 112/*
f3f19d11 113 * The libusb Darwin backend is broken: it can report a kernel driver being
3f98bf70
BV
114 * active, but detaching it always returns an error.
115 */
116#if !defined(__APPLE__)
117 if (libusb_kernel_driver_active(hdl, 0) == 1) {
ebc34738 118 if ((ret = libusb_detach_kernel_driver(hdl, 0)) < 0) {
d4928d71
PS
119 sr_err("failed to detach kernel driver: %s",
120 libusb_error_name(ret));
6d754b6d 121 return SR_ERR;
e10d6e32
BV
122 }
123 }
bf3f06c9 124#endif
e10d6e32 125
ebc34738 126 if ((ret = libusb_set_configuration(hdl, configuration)) < 0) {
d4928d71
PS
127 sr_err("Unable to set configuration: %s",
128 libusb_error_name(ret));
6d754b6d 129 return SR_ERR;
edf60d05
UH
130 }
131
132 if ((ezusb_reset(hdl, 1)) < 0)
6d754b6d 133 return SR_ERR;
edf60d05 134
8e2d6c9d 135 if (ezusb_install_firmware(ctx, hdl, name) < 0)
6d754b6d 136 return SR_ERR;
edf60d05
UH
137
138 if ((ezusb_reset(hdl, 0)) < 0)
6d754b6d 139 return SR_ERR;
edf60d05
UH
140
141 libusb_close(hdl);
142
6d754b6d 143 return SR_OK;
edf60d05 144}