]>
Commit | Line | Data |
---|---|---|
a1bb33af UH |
1 | /* |
2 | * This file is part of the sigrok project. | |
3 | * | |
4 | * Copyright (C) 2010 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 | ||
b08024a8 UH |
24 | #include <sigrok.h> |
25 | #include <sigrok-internal.h> | |
a1bb33af UH |
26 | #include <libusb.h> |
27 | #include <glib.h> | |
3bbd9849 | 28 | #include <glib/gstdio.h> |
a1bb33af UH |
29 | #include <stdio.h> |
30 | #include <errno.h> | |
31 | #include <string.h> | |
32 | #include "config.h" | |
33 | ||
6d754b6d | 34 | |
a1bb33af UH |
35 | int ezusb_reset(struct libusb_device_handle *hdl, int set_clear) |
36 | { | |
37 | int err; | |
38 | unsigned char buf[1]; | |
39 | ||
b08024a8 | 40 | sr_info("setting CPU reset mode %s...", set_clear ? "on" : "off"); |
a1bb33af UH |
41 | buf[0] = set_clear ? 1 : 0; |
42 | err = libusb_control_transfer(hdl, LIBUSB_REQUEST_TYPE_VENDOR, 0xa0, | |
9d2933fb UH |
43 | 0xe600, 0x0000, buf, 1, 100); |
44 | if (err < 0) | |
b08024a8 | 45 | sr_warn("Unable to send control request: %d", err); |
a1bb33af UH |
46 | |
47 | return err; | |
48 | } | |
49 | ||
a1bb33af UH |
50 | int ezusb_install_firmware(libusb_device_handle *hdl, const char *filename) |
51 | { | |
52 | FILE *fw; | |
53 | int offset, chunksize, err, result; | |
54 | unsigned char buf[4096]; | |
55 | ||
b08024a8 | 56 | sr_info("Uploading firmware at %s", filename); |
868d8cef | 57 | if ((fw = g_fopen(filename, "rb")) == NULL) { |
b08024a8 UH |
58 | sr_warn("Unable to open firmware file %s for reading: %s", |
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; |
9d2933fb | 69 | err = libusb_control_transfer(hdl, LIBUSB_REQUEST_TYPE_VENDOR | |
986f7270 UH |
70 | LIBUSB_ENDPOINT_OUT, 0xa0, offset, |
71 | 0x0000, buf, chunksize, 100); | |
9d2933fb | 72 | if (err < 0) { |
b08024a8 | 73 | sr_warn("Unable to send firmware to device: %d", err); |
6d754b6d | 74 | result = SR_ERR; |
a1bb33af UH |
75 | break; |
76 | } | |
b08024a8 | 77 | sr_info("Uploaded %d bytes", chunksize); |
a1bb33af UH |
78 | offset += chunksize; |
79 | } | |
80 | fclose(fw); | |
b08024a8 | 81 | sr_info("Firmware upload done"); |
a1bb33af UH |
82 | |
83 | return result; | |
84 | } | |
edf60d05 UH |
85 | |
86 | int ezusb_upload_firmware(libusb_device *dev, int configuration, | |
87 | const char *filename) | |
88 | { | |
89 | struct libusb_device_handle *hdl; | |
90 | int err; | |
91 | ||
b08024a8 | 92 | sr_info("uploading firmware to device on %d.%d", |
edf60d05 UH |
93 | libusb_get_bus_number(dev), libusb_get_device_address(dev)); |
94 | ||
6d754b6d | 95 | if ((err = libusb_open(dev, &hdl)) < 0) { |
b08024a8 | 96 | sr_warn("failed to open device: %d", err); |
6d754b6d | 97 | return SR_ERR; |
edf60d05 UH |
98 | } |
99 | ||
e10d6e32 | 100 | if (libusb_kernel_driver_active(hdl, 0)) { |
6d754b6d BV |
101 | if ((err = libusb_detach_kernel_driver(hdl, 0)) < 0) { |
102 | sr_warn("failed to detach kernel driver: %d", err); | |
103 | return SR_ERR; | |
e10d6e32 BV |
104 | } |
105 | } | |
106 | ||
6d754b6d | 107 | if ((err = libusb_set_configuration(hdl, configuration)) < 0) { |
b08024a8 | 108 | sr_warn("Unable to set configuration: %d", err); |
6d754b6d | 109 | return SR_ERR; |
edf60d05 UH |
110 | } |
111 | ||
112 | if ((ezusb_reset(hdl, 1)) < 0) | |
6d754b6d | 113 | return SR_ERR; |
edf60d05 | 114 | |
6d754b6d BV |
115 | if (ezusb_install_firmware(hdl, filename) < 0) |
116 | return SR_ERR; | |
edf60d05 UH |
117 | |
118 | if ((ezusb_reset(hdl, 0)) < 0) | |
6d754b6d | 119 | return SR_ERR; |
edf60d05 UH |
120 | |
121 | libusb_close(hdl); | |
122 | ||
6d754b6d | 123 | return SR_OK; |
edf60d05 | 124 | } |