]>
Commit | Line | Data |
---|---|---|
a1bb33af UH |
1 | /* |
2 | * This file is part of the sigrok project. | |
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 | ||
24 | #include <libusb.h> | |
25 | #include <glib.h> | |
3bbd9849 | 26 | #include <glib/gstdio.h> |
a1bb33af UH |
27 | #include <stdio.h> |
28 | #include <errno.h> | |
29 | #include <string.h> | |
45c59c8b BV |
30 | #include "libsigrok.h" |
31 | #include "libsigrok-internal.h" | |
a1bb33af | 32 | |
d458a0ac AG |
33 | /* Message logging helpers with driver-specific prefix string. */ |
34 | #define DRIVER_LOG_DOMAIN "ezusb: " | |
35 | #define sr_log(l, s, args...) sr_log(l, DRIVER_LOG_DOMAIN s, ## args) | |
36 | #define sr_spew(s, args...) sr_spew(DRIVER_LOG_DOMAIN s, ## args) | |
37 | #define sr_dbg(s, args...) sr_dbg(DRIVER_LOG_DOMAIN s, ## args) | |
38 | #define sr_info(s, args...) sr_info(DRIVER_LOG_DOMAIN s, ## args) | |
39 | #define sr_warn(s, args...) sr_warn(DRIVER_LOG_DOMAIN s, ## args) | |
40 | #define sr_err(s, args...) sr_err(DRIVER_LOG_DOMAIN s, ## args) | |
41 | ||
1a081ca6 | 42 | SR_PRIV int ezusb_reset(struct libusb_device_handle *hdl, int set_clear) |
a1bb33af | 43 | { |
ebc34738 | 44 | int ret; |
a1bb33af UH |
45 | unsigned char buf[1]; |
46 | ||
d458a0ac | 47 | sr_info("setting CPU reset mode %s...", |
7b48d6e1 | 48 | set_clear ? "on" : "off"); |
a1bb33af | 49 | buf[0] = set_clear ? 1 : 0; |
ebc34738 | 50 | ret = libusb_control_transfer(hdl, LIBUSB_REQUEST_TYPE_VENDOR, 0xa0, |
9d2933fb | 51 | 0xe600, 0x0000, buf, 1, 100); |
ebc34738 | 52 | if (ret < 0) |
1740429d | 53 | sr_err("Unable to send control request: %s.", |
d4928d71 | 54 | libusb_error_name(ret)); |
a1bb33af | 55 | |
ebc34738 | 56 | return ret; |
a1bb33af UH |
57 | } |
58 | ||
1a081ca6 UH |
59 | SR_PRIV int ezusb_install_firmware(libusb_device_handle *hdl, |
60 | const char *filename) | |
a1bb33af UH |
61 | { |
62 | FILE *fw; | |
ebc34738 | 63 | int offset, chunksize, ret, result; |
a1bb33af UH |
64 | unsigned char buf[4096]; |
65 | ||
d458a0ac | 66 | sr_info("Uploading firmware at %s", filename); |
868d8cef | 67 | if ((fw = g_fopen(filename, "rb")) == NULL) { |
d458a0ac | 68 | sr_err("Unable to open firmware file %s for reading: %s", |
133a37bf | 69 | filename, strerror(errno)); |
6d754b6d | 70 | return SR_ERR; |
a1bb33af UH |
71 | } |
72 | ||
6d754b6d BV |
73 | result = SR_OK; |
74 | offset = 0; | |
9d2933fb | 75 | while (1) { |
a1bb33af | 76 | chunksize = fread(buf, 1, 4096, fw); |
9d2933fb | 77 | if (chunksize == 0) |
a1bb33af | 78 | break; |
ebc34738 | 79 | ret = libusb_control_transfer(hdl, LIBUSB_REQUEST_TYPE_VENDOR | |
986f7270 UH |
80 | LIBUSB_ENDPOINT_OUT, 0xa0, offset, |
81 | 0x0000, buf, chunksize, 100); | |
ebc34738 | 82 | if (ret < 0) { |
1740429d | 83 | sr_err("Unable to send firmware to device: %s.", |
d4928d71 | 84 | libusb_error_name(ret)); |
6d754b6d | 85 | result = SR_ERR; |
a1bb33af UH |
86 | break; |
87 | } | |
d458a0ac | 88 | sr_info("Uploaded %d bytes", chunksize); |
a1bb33af UH |
89 | offset += chunksize; |
90 | } | |
91 | fclose(fw); | |
d458a0ac | 92 | sr_info("Firmware upload done"); |
a1bb33af UH |
93 | |
94 | return result; | |
95 | } | |
edf60d05 | 96 | |
1a081ca6 UH |
97 | SR_PRIV int ezusb_upload_firmware(libusb_device *dev, int configuration, |
98 | const char *filename) | |
edf60d05 UH |
99 | { |
100 | struct libusb_device_handle *hdl; | |
ebc34738 | 101 | int ret; |
edf60d05 | 102 | |
d458a0ac | 103 | sr_info("uploading firmware to device on %d.%d", |
7b48d6e1 | 104 | libusb_get_bus_number(dev), libusb_get_device_address(dev)); |
edf60d05 | 105 | |
ebc34738 | 106 | if ((ret = libusb_open(dev, &hdl)) < 0) { |
1740429d | 107 | sr_err("failed to open device: %s.", libusb_error_name(ret)); |
6d754b6d | 108 | return SR_ERR; |
edf60d05 UH |
109 | } |
110 | ||
3f98bf70 BV |
111 | /* |
112 | * The libusbx darwin backend is broken: it can report a kernel driver being | |
113 | * active, but detaching it always returns an error. | |
114 | */ | |
115 | #if !defined(__APPLE__) | |
116 | if (libusb_kernel_driver_active(hdl, 0) == 1) { | |
ebc34738 | 117 | if ((ret = libusb_detach_kernel_driver(hdl, 0)) < 0) { |
d4928d71 PS |
118 | sr_err("failed to detach kernel driver: %s", |
119 | libusb_error_name(ret)); | |
6d754b6d | 120 | return SR_ERR; |
e10d6e32 BV |
121 | } |
122 | } | |
bf3f06c9 | 123 | #endif |
e10d6e32 | 124 | |
ebc34738 | 125 | if ((ret = libusb_set_configuration(hdl, configuration)) < 0) { |
d4928d71 PS |
126 | sr_err("Unable to set configuration: %s", |
127 | libusb_error_name(ret)); | |
6d754b6d | 128 | return SR_ERR; |
edf60d05 UH |
129 | } |
130 | ||
131 | if ((ezusb_reset(hdl, 1)) < 0) | |
6d754b6d | 132 | return SR_ERR; |
edf60d05 | 133 | |
6d754b6d BV |
134 | if (ezusb_install_firmware(hdl, filename) < 0) |
135 | return SR_ERR; | |
edf60d05 UH |
136 | |
137 | if ((ezusb_reset(hdl, 0)) < 0) | |
6d754b6d | 138 | return SR_ERR; |
edf60d05 UH |
139 | |
140 | libusb_close(hdl); | |
141 | ||
6d754b6d | 142 | return SR_OK; |
edf60d05 | 143 | } |