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