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