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