]> sigrok.org Git - libsigrok.git/blob - hardware/common/ezusb.c
MinGW: Use "b" in all fopen() calls.
[libsigrok.git] / hardware / common / ezusb.c
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
24 #include <libusb.h>
25 #include <glib.h>
26 #include <stdio.h>
27 #include <errno.h>
28 #include <string.h>
29 #include "config.h"
30
31 int ezusb_reset(struct libusb_device_handle *hdl, int set_clear)
32 {
33         int err;
34         unsigned char buf[1];
35
36         g_message("setting CPU reset mode %s...", set_clear ? "on" : "off");
37         buf[0] = set_clear ? 1 : 0;
38         err = libusb_control_transfer(hdl, LIBUSB_REQUEST_TYPE_VENDOR, 0xa0,
39                                       0xe600, 0x0000, buf, 1, 100);
40         if (err < 0)
41                 g_warning("Unable to send control request: %d", err);
42
43         return err;
44 }
45
46 int ezusb_install_firmware(libusb_device_handle *hdl, const char *filename)
47 {
48         FILE *fw;
49         int offset, chunksize, err, result;
50         unsigned char buf[4096];
51
52         g_message("Uploading firmware at %s", filename);
53         if ((fw = fopen(filename, "rb")) == NULL) {
54                 g_warning("Unable to open firmware file %s for reading: %s",
55                           filename, strerror(errno));
56                 return 1;
57         }
58
59         result = offset = 0;
60         while (1) {
61                 chunksize = fread(buf, 1, 4096, fw);
62                 if (chunksize == 0)
63                         break;
64                 err = libusb_control_transfer(hdl, LIBUSB_REQUEST_TYPE_VENDOR |
65                                               LIBUSB_ENDPOINT_OUT, 0xa0, offset,
66                                               0x0000, buf, chunksize, 100);
67                 if (err < 0) {
68                         g_warning("Unable to send firmware to device: %d", err);
69                         result = 1;
70                         break;
71                 }
72                 g_message("Uploaded %d bytes", chunksize);
73                 offset += chunksize;
74         }
75         fclose(fw);
76         g_message("Firmware upload done");
77
78         return result;
79 }
80
81 int ezusb_upload_firmware(libusb_device *dev, int configuration,
82                           const char *filename)
83 {
84         struct libusb_device_handle *hdl;
85         int err;
86
87         g_message("uploading firmware to device on %d.%d",
88                   libusb_get_bus_number(dev), libusb_get_device_address(dev));
89
90         err = libusb_open(dev, &hdl);
91         if (err != 0) {
92                 g_warning("failed to open device: %d", err);
93                 return 1;
94         }
95
96         err = libusb_set_configuration(hdl, configuration);
97         if (err != 0) {
98                 g_warning("Unable to set configuration: %d", err);
99                 return 1;
100         }
101
102         if ((ezusb_reset(hdl, 1)) < 0)
103                 return 1;
104
105         if (ezusb_install_firmware(hdl, filename) != 0)
106                 return 1;
107
108         if ((ezusb_reset(hdl, 0)) < 0)
109                 return 1;
110
111         libusb_close(hdl);
112
113         return 0;
114 }