]>
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> | |
b7f09cf8 UH |
30 | #include "sigrok.h" |
31 | #include "sigrok-internal.h" | |
a1bb33af | 32 | |
1a081ca6 | 33 | SR_PRIV int ezusb_reset(struct libusb_device_handle *hdl, int set_clear) |
a1bb33af | 34 | { |
ebc34738 | 35 | int ret; |
a1bb33af UH |
36 | unsigned char buf[1]; |
37 | ||
7b48d6e1 UH |
38 | sr_info("ezusb: setting CPU reset mode %s...", |
39 | set_clear ? "on" : "off"); | |
a1bb33af | 40 | buf[0] = set_clear ? 1 : 0; |
ebc34738 | 41 | ret = libusb_control_transfer(hdl, LIBUSB_REQUEST_TYPE_VENDOR, 0xa0, |
9d2933fb | 42 | 0xe600, 0x0000, buf, 1, 100); |
ebc34738 UH |
43 | if (ret < 0) |
44 | sr_err("ezusb: Unable to send control request: %d", ret); | |
a1bb33af | 45 | |
ebc34738 | 46 | return ret; |
a1bb33af UH |
47 | } |
48 | ||
1a081ca6 UH |
49 | SR_PRIV int ezusb_install_firmware(libusb_device_handle *hdl, |
50 | const char *filename) | |
a1bb33af UH |
51 | { |
52 | FILE *fw; | |
ebc34738 | 53 | int offset, chunksize, ret, result; |
a1bb33af UH |
54 | unsigned char buf[4096]; |
55 | ||
7b48d6e1 | 56 | sr_info("ezusb: Uploading firmware at %s", filename); |
868d8cef | 57 | if ((fw = g_fopen(filename, "rb")) == NULL) { |
7b48d6e1 | 58 | sr_err("ezusb: Unable to open firmware file %s for reading: %s", |
133a37bf | 59 | filename, strerror(errno)); |
6d754b6d | 60 | return SR_ERR; |
a1bb33af UH |
61 | } |
62 | ||
6d754b6d BV |
63 | result = SR_OK; |
64 | offset = 0; | |
9d2933fb | 65 | while (1) { |
a1bb33af | 66 | chunksize = fread(buf, 1, 4096, fw); |
9d2933fb | 67 | if (chunksize == 0) |
a1bb33af | 68 | break; |
ebc34738 | 69 | ret = libusb_control_transfer(hdl, LIBUSB_REQUEST_TYPE_VENDOR | |
986f7270 UH |
70 | LIBUSB_ENDPOINT_OUT, 0xa0, offset, |
71 | 0x0000, buf, chunksize, 100); | |
ebc34738 | 72 | if (ret < 0) { |
7b48d6e1 | 73 | sr_err("ezusb: Unable to send firmware to device: %d", |
ebc34738 | 74 | ret); |
6d754b6d | 75 | result = SR_ERR; |
a1bb33af UH |
76 | break; |
77 | } | |
7b48d6e1 | 78 | sr_info("ezusb: Uploaded %d bytes", chunksize); |
a1bb33af UH |
79 | offset += chunksize; |
80 | } | |
81 | fclose(fw); | |
7b48d6e1 | 82 | sr_info("ezusb: Firmware upload done"); |
a1bb33af UH |
83 | |
84 | return result; | |
85 | } | |
edf60d05 | 86 | |
1a081ca6 UH |
87 | SR_PRIV int ezusb_upload_firmware(libusb_device *dev, int configuration, |
88 | const char *filename) | |
edf60d05 UH |
89 | { |
90 | struct libusb_device_handle *hdl; | |
ebc34738 | 91 | int ret; |
edf60d05 | 92 | |
7b48d6e1 UH |
93 | sr_info("ezusb: uploading firmware to device on %d.%d", |
94 | libusb_get_bus_number(dev), libusb_get_device_address(dev)); | |
edf60d05 | 95 | |
ebc34738 UH |
96 | if ((ret = libusb_open(dev, &hdl)) < 0) { |
97 | sr_err("ezusb: failed to open device: %d", ret); | |
6d754b6d | 98 | return SR_ERR; |
edf60d05 UH |
99 | } |
100 | ||
bf3f06c9 UH |
101 | /* Neither Windows/MinGW nor Darwin/Mac support these libusb-1.0 calls. */ |
102 | #if !defined(_WIN32) && !defined(__APPLE__) | |
e10d6e32 | 103 | if (libusb_kernel_driver_active(hdl, 0)) { |
ebc34738 UH |
104 | if ((ret = libusb_detach_kernel_driver(hdl, 0)) < 0) { |
105 | sr_err("ezusb: failed to detach kernel driver: %d", ret); | |
6d754b6d | 106 | return SR_ERR; |
e10d6e32 BV |
107 | } |
108 | } | |
bf3f06c9 | 109 | #endif |
e10d6e32 | 110 | |
ebc34738 UH |
111 | if ((ret = libusb_set_configuration(hdl, configuration)) < 0) { |
112 | sr_err("ezusb: Unable to set configuration: %d", ret); | |
6d754b6d | 113 | return SR_ERR; |
edf60d05 UH |
114 | } |
115 | ||
116 | if ((ezusb_reset(hdl, 1)) < 0) | |
6d754b6d | 117 | return SR_ERR; |
edf60d05 | 118 | |
6d754b6d BV |
119 | if (ezusb_install_firmware(hdl, filename) < 0) |
120 | return SR_ERR; | |
edf60d05 UH |
121 | |
122 | if ((ezusb_reset(hdl, 0)) < 0) | |
6d754b6d | 123 | return SR_ERR; |
edf60d05 UH |
124 | |
125 | libusb_close(hdl); | |
126 | ||
6d754b6d | 127 | return SR_OK; |
edf60d05 | 128 | } |