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