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