blob: 044b4642705458d9c91b4b0c39b5a109a38a24dc [file] [log] [blame]
Uwe Hermanna1bb33a2010-04-02 20:18:27 +02001/*
Uwe Hermann50985c22013-04-23 22:24:30 +02002 * This file is part of the libsigrok project.
Uwe Hermanna1bb33a2010-04-02 20:18:27 +02003 *
Bert Vermeulenc73d2ea2012-02-13 14:31:51 +01004 * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com>
Uwe Hermanna1bb33a2010-04-02 20:18:27 +02005 *
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>
Uwe Hermann3bbd9842011-02-06 02:14:57 +010026#include <glib/gstdio.h>
Uwe Hermanna1bb33a2010-04-02 20:18:27 +020027#include <stdio.h>
28#include <errno.h>
29#include <string.h>
Bert Vermeulen45c59c82012-07-05 00:55:07 +020030#include "libsigrok.h"
31#include "libsigrok-internal.h"
Uwe Hermanna1bb33a2010-04-02 20:18:27 +020032
Martin Ling3544f842013-12-23 03:38:35 +000033#define LOG_PREFIX "ezusb"
Alexandru Gagniucd458a0a2012-12-04 13:31:49 -060034
Uwe Hermann1a081ca2012-02-01 23:40:35 +010035SR_PRIV int ezusb_reset(struct libusb_device_handle *hdl, int set_clear)
Uwe Hermanna1bb33a2010-04-02 20:18:27 +020036{
Uwe Hermannebc34732012-03-20 17:51:18 +010037 int ret;
Uwe Hermanna1bb33a2010-04-02 20:18:27 +020038 unsigned char buf[1];
39
Alexandru Gagniucd458a0a2012-12-04 13:31:49 -060040 sr_info("setting CPU reset mode %s...",
Uwe Hermann7b48d6e2012-02-17 19:40:01 +010041 set_clear ? "on" : "off");
Uwe Hermanna1bb33a2010-04-02 20:18:27 +020042 buf[0] = set_clear ? 1 : 0;
Uwe Hermannebc34732012-03-20 17:51:18 +010043 ret = libusb_control_transfer(hdl, LIBUSB_REQUEST_TYPE_VENDOR, 0xa0,
Uwe Hermann9d2933f2010-04-09 20:36:29 +020044 0xe600, 0x0000, buf, 1, 100);
Uwe Hermannebc34732012-03-20 17:51:18 +010045 if (ret < 0)
Peter Stuge17404292012-12-05 01:23:49 +010046 sr_err("Unable to send control request: %s.",
Peter Stuged4928d72012-12-04 21:11:25 +010047 libusb_error_name(ret));
Uwe Hermanna1bb33a2010-04-02 20:18:27 +020048
Uwe Hermannebc34732012-03-20 17:51:18 +010049 return ret;
Uwe Hermanna1bb33a2010-04-02 20:18:27 +020050}
51
Uwe Hermann1a081ca2012-02-01 23:40:35 +010052SR_PRIV int ezusb_install_firmware(libusb_device_handle *hdl,
53 const char *filename)
Uwe Hermanna1bb33a2010-04-02 20:18:27 +020054{
55 FILE *fw;
Uwe Hermannebc34732012-03-20 17:51:18 +010056 int offset, chunksize, ret, result;
Uwe Hermanna1bb33a2010-04-02 20:18:27 +020057 unsigned char buf[4096];
58
Alexandru Gagniucd458a0a2012-12-04 13:31:49 -060059 sr_info("Uploading firmware at %s", filename);
Uwe Hermann868d8ce2011-02-05 20:03:17 +010060 if ((fw = g_fopen(filename, "rb")) == NULL) {
Alexandru Gagniucd458a0a2012-12-04 13:31:49 -060061 sr_err("Unable to open firmware file %s for reading: %s",
Uwe Hermann133a37b2012-02-11 20:06:46 +010062 filename, strerror(errno));
Bert Vermeulen6d754b62011-06-12 18:04:19 +020063 return SR_ERR;
Uwe Hermanna1bb33a2010-04-02 20:18:27 +020064 }
65
Bert Vermeulen6d754b62011-06-12 18:04:19 +020066 result = SR_OK;
67 offset = 0;
Uwe Hermann9d2933f2010-04-09 20:36:29 +020068 while (1) {
Uwe Hermanna1bb33a2010-04-02 20:18:27 +020069 chunksize = fread(buf, 1, 4096, fw);
Uwe Hermann9d2933f2010-04-09 20:36:29 +020070 if (chunksize == 0)
Uwe Hermanna1bb33a2010-04-02 20:18:27 +020071 break;
Uwe Hermannebc34732012-03-20 17:51:18 +010072 ret = libusb_control_transfer(hdl, LIBUSB_REQUEST_TYPE_VENDOR |
Uwe Hermann986f7272010-04-15 20:55:57 +020073 LIBUSB_ENDPOINT_OUT, 0xa0, offset,
74 0x0000, buf, chunksize, 100);
Uwe Hermannebc34732012-03-20 17:51:18 +010075 if (ret < 0) {
Peter Stuge17404292012-12-05 01:23:49 +010076 sr_err("Unable to send firmware to device: %s.",
Peter Stuged4928d72012-12-04 21:11:25 +010077 libusb_error_name(ret));
Bert Vermeulen6d754b62011-06-12 18:04:19 +020078 result = SR_ERR;
Uwe Hermanna1bb33a2010-04-02 20:18:27 +020079 break;
80 }
Alexandru Gagniucd458a0a2012-12-04 13:31:49 -060081 sr_info("Uploaded %d bytes", chunksize);
Uwe Hermanna1bb33a2010-04-02 20:18:27 +020082 offset += chunksize;
83 }
84 fclose(fw);
Alexandru Gagniucd458a0a2012-12-04 13:31:49 -060085 sr_info("Firmware upload done");
Uwe Hermanna1bb33a2010-04-02 20:18:27 +020086
87 return result;
88}
Uwe Hermannedf60d02010-04-09 20:44:51 +020089
Uwe Hermann1a081ca2012-02-01 23:40:35 +010090SR_PRIV int ezusb_upload_firmware(libusb_device *dev, int configuration,
91 const char *filename)
Uwe Hermannedf60d02010-04-09 20:44:51 +020092{
93 struct libusb_device_handle *hdl;
Uwe Hermannebc34732012-03-20 17:51:18 +010094 int ret;
Uwe Hermannedf60d02010-04-09 20:44:51 +020095
Alexandru Gagniucd458a0a2012-12-04 13:31:49 -060096 sr_info("uploading firmware to device on %d.%d",
Uwe Hermann7b48d6e2012-02-17 19:40:01 +010097 libusb_get_bus_number(dev), libusb_get_device_address(dev));
Uwe Hermannedf60d02010-04-09 20:44:51 +020098
Uwe Hermannebc34732012-03-20 17:51:18 +010099 if ((ret = libusb_open(dev, &hdl)) < 0) {
Peter Stuge17404292012-12-05 01:23:49 +0100100 sr_err("failed to open device: %s.", libusb_error_name(ret));
Bert Vermeulen6d754b62011-06-12 18:04:19 +0200101 return SR_ERR;
Uwe Hermannedf60d02010-04-09 20:44:51 +0200102 }
103
Bert Vermeulen3f98bf72013-04-07 14:14:16 +0200104/*
105 * The libusbx darwin backend is broken: it can report a kernel driver being
106 * active, but detaching it always returns an error.
107 */
108#if !defined(__APPLE__)
109 if (libusb_kernel_driver_active(hdl, 0) == 1) {
Uwe Hermannebc34732012-03-20 17:51:18 +0100110 if ((ret = libusb_detach_kernel_driver(hdl, 0)) < 0) {
Peter Stuged4928d72012-12-04 21:11:25 +0100111 sr_err("failed to detach kernel driver: %s",
112 libusb_error_name(ret));
Bert Vermeulen6d754b62011-06-12 18:04:19 +0200113 return SR_ERR;
Bert Vermeulene10d6e32011-06-04 23:20:00 +0200114 }
115 }
Uwe Hermannbf3f06c2012-02-11 00:20:23 +0100116#endif
Bert Vermeulene10d6e32011-06-04 23:20:00 +0200117
Uwe Hermannebc34732012-03-20 17:51:18 +0100118 if ((ret = libusb_set_configuration(hdl, configuration)) < 0) {
Peter Stuged4928d72012-12-04 21:11:25 +0100119 sr_err("Unable to set configuration: %s",
120 libusb_error_name(ret));
Bert Vermeulen6d754b62011-06-12 18:04:19 +0200121 return SR_ERR;
Uwe Hermannedf60d02010-04-09 20:44:51 +0200122 }
123
124 if ((ezusb_reset(hdl, 1)) < 0)
Bert Vermeulen6d754b62011-06-12 18:04:19 +0200125 return SR_ERR;
Uwe Hermannedf60d02010-04-09 20:44:51 +0200126
Bert Vermeulen6d754b62011-06-12 18:04:19 +0200127 if (ezusb_install_firmware(hdl, filename) < 0)
128 return SR_ERR;
Uwe Hermannedf60d02010-04-09 20:44:51 +0200129
130 if ((ezusb_reset(hdl, 0)) < 0)
Bert Vermeulen6d754b62011-06-12 18:04:19 +0200131 return SR_ERR;
Uwe Hermannedf60d02010-04-09 20:44:51 +0200132
133 libusb_close(hdl);
134
Bert Vermeulen6d754b62011-06-12 18:04:19 +0200135 return SR_OK;
Uwe Hermannedf60d02010-04-09 20:44:51 +0200136}