]> sigrok.org Git - libsigrok.git/blame - src/ezusb.c
resource: Move sr_file_get_size() to resource.c
[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
1a081ca6 36SR_PRIV int ezusb_reset(struct libusb_device_handle *hdl, int set_clear)
a1bb33af 37{
ebc34738 38 int ret;
a1bb33af
UH
39 unsigned char buf[1];
40
d458a0ac 41 sr_info("setting CPU reset mode %s...",
7b48d6e1 42 set_clear ? "on" : "off");
a1bb33af 43 buf[0] = set_clear ? 1 : 0;
ebc34738 44 ret = libusb_control_transfer(hdl, LIBUSB_REQUEST_TYPE_VENDOR, 0xa0,
9d2933fb 45 0xe600, 0x0000, buf, 1, 100);
ebc34738 46 if (ret < 0)
1740429d 47 sr_err("Unable to send control request: %s.",
d4928d71 48 libusb_error_name(ret));
a1bb33af 49
ebc34738 50 return ret;
a1bb33af
UH
51}
52
1a081ca6
UH
53SR_PRIV int ezusb_install_firmware(libusb_device_handle *hdl,
54 const char *filename)
a1bb33af
UH
55{
56 FILE *fw;
ebc34738 57 int offset, chunksize, ret, result;
a1bb33af
UH
58 unsigned char buf[4096];
59
d458a0ac 60 sr_info("Uploading firmware at %s", filename);
98fec29e 61 if (!(fw = g_fopen(filename, "rb"))) {
d458a0ac 62 sr_err("Unable to open firmware file %s for reading: %s",
7237e912 63 filename, g_strerror(errno));
6d754b6d 64 return SR_ERR;
a1bb33af
UH
65 }
66
6d754b6d
BV
67 result = SR_OK;
68 offset = 0;
9d2933fb 69 while (1) {
a1bb33af 70 chunksize = fread(buf, 1, 4096, fw);
9d2933fb 71 if (chunksize == 0)
a1bb33af 72 break;
ebc34738 73 ret = libusb_control_transfer(hdl, LIBUSB_REQUEST_TYPE_VENDOR |
986f7270
UH
74 LIBUSB_ENDPOINT_OUT, 0xa0, offset,
75 0x0000, buf, chunksize, 100);
ebc34738 76 if (ret < 0) {
1740429d 77 sr_err("Unable to send firmware to device: %s.",
d4928d71 78 libusb_error_name(ret));
6d754b6d 79 result = SR_ERR;
a1bb33af
UH
80 break;
81 }
d458a0ac 82 sr_info("Uploaded %d bytes", chunksize);
a1bb33af
UH
83 offset += chunksize;
84 }
85 fclose(fw);
d458a0ac 86 sr_info("Firmware upload done");
a1bb33af
UH
87
88 return result;
89}
edf60d05 90
1a081ca6
UH
91SR_PRIV int ezusb_upload_firmware(libusb_device *dev, int configuration,
92 const char *filename)
edf60d05
UH
93{
94 struct libusb_device_handle *hdl;
ebc34738 95 int ret;
edf60d05 96
d458a0ac 97 sr_info("uploading firmware to device on %d.%d",
7b48d6e1 98 libusb_get_bus_number(dev), libusb_get_device_address(dev));
edf60d05 99
ebc34738 100 if ((ret = libusb_open(dev, &hdl)) < 0) {
1740429d 101 sr_err("failed to open device: %s.", libusb_error_name(ret));
6d754b6d 102 return SR_ERR;
edf60d05
UH
103 }
104
3f98bf70 105/*
f3f19d11 106 * The libusb Darwin backend is broken: it can report a kernel driver being
3f98bf70
BV
107 * active, but detaching it always returns an error.
108 */
109#if !defined(__APPLE__)
110 if (libusb_kernel_driver_active(hdl, 0) == 1) {
ebc34738 111 if ((ret = libusb_detach_kernel_driver(hdl, 0)) < 0) {
d4928d71
PS
112 sr_err("failed to detach kernel driver: %s",
113 libusb_error_name(ret));
6d754b6d 114 return SR_ERR;
e10d6e32
BV
115 }
116 }
bf3f06c9 117#endif
e10d6e32 118
ebc34738 119 if ((ret = libusb_set_configuration(hdl, configuration)) < 0) {
d4928d71
PS
120 sr_err("Unable to set configuration: %s",
121 libusb_error_name(ret));
6d754b6d 122 return SR_ERR;
edf60d05
UH
123 }
124
125 if ((ezusb_reset(hdl, 1)) < 0)
6d754b6d 126 return SR_ERR;
edf60d05 127
6d754b6d
BV
128 if (ezusb_install_firmware(hdl, filename) < 0)
129 return SR_ERR;
edf60d05
UH
130
131 if ((ezusb_reset(hdl, 0)) < 0)
6d754b6d 132 return SR_ERR;
edf60d05
UH
133
134 libusb_close(hdl);
135
6d754b6d 136 return SR_OK;
edf60d05 137}