summaryrefslogtreecommitdiff
path: root/sys/dev/usb/ezload.c
blob: 061e675a225dde2761a898c51a45a8d4cfc860d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*	$OpenBSD: ezload.c,v 1.8 2005/04/14 19:07:02 damien Exp $ */
/*	$NetBSD: ezload.c,v 1.5 2002/07/11 21:14:25 augustss Exp $	*/

/*
 * Copyright (c) 2000 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by  Lennart Augustsson <lennart@augustsson.net>.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *        This product includes software developed by the NetBSD
 *        Foundation, Inc. and its contributors.
 * 4. Neither the name of The NetBSD Foundation nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/device.h>
#include <sys/malloc.h>
#include <sys/conf.h>

#include <dev/usb/usb.h>
#include <dev/usb/usbdi.h>
#include <dev/usb/usbdi_util.h>

#include <dev/usb/ezload.h>

/*
 * Vendor specific request code for Anchor Upload/Download
 */

/* This one is implemented in the core */
#define ANCHOR_LOAD_INTERNAL	0xA0

/* This is the highest internal RAM address for the AN2131Q */
#define ANCHOR_MAX_INTERNAL_ADDRESS  0x1B3F

/*
 * EZ-USB Control and Status Register.  Bit 0 controls 8051 reset
 */
#define ANCHOR_CPUCS_REG	0x7F92
#define  ANCHOR_RESET		0x01

/*
 * Although USB does not limit you here, the Anchor docs
 * quote 64 as a limit, and Mato@activewireinc.com suggested
 * to use 16.
 */
#define ANCHOR_CHUNK 16

/*
 * This is a firmware loader for ezusb (Anchor) devices. When the firmware
 * has been downloaded the device will simulate a disconnect and when it
 * is next recognized by the USB software it will appear as another
 * device.
 */

#ifdef USB_DEBUG
#define DPRINTF(x)	if (ezloaddebug) printf x
#define DPRINTFN(n,x)	if (ezloaddebug>(n)) printf x
int ezloaddebug = 0;
#else
#define DPRINTF(x)
#define DPRINTFN(n,x)
#endif

usbd_status
ezload_reset(usbd_device_handle dev, int reset)
{
	usb_device_request_t req;
	uByte rst;

	DPRINTF(("ezload_reset: reset=%d\n", reset));

	rst = reset ? ANCHOR_RESET : 0;
	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
	req.bRequest = ANCHOR_LOAD_INTERNAL;
	USETW(req.wValue, ANCHOR_CPUCS_REG);
	USETW(req.wIndex, 0);
	USETW(req.wLength, 1);
	return (usbd_do_request(dev, &req, &rst));
}

usbd_status
ezload_download(usbd_device_handle dev, const char *name, const u_char *buf,
    size_t buflen)
{
	usb_device_request_t req;
	usbd_status err = 0;
	u_int8_t length;
	u_int16_t address;
	u_int len, offs;

	for (;;) {
		length = *buf++;
		if (length == 0)
			break;

		address = UGETW(buf); buf += 2;
#if 0
		if (address + length > ANCHOR_MAX_INTERNAL_ADDRESS)
			return (USBD_INVAL);
#endif

		req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
		req.bRequest = ANCHOR_LOAD_INTERNAL;
		USETW(req.wIndex, 0);
		for (offs = 0; offs < length; offs += ANCHOR_CHUNK) {
			len = length - offs;
			if (len > ANCHOR_CHUNK)
				len = ANCHOR_CHUNK;
			USETW(req.wValue, address + offs);
			USETW(req.wLength, len);
			DPRINTFN(2,("ezload_download: addr=0x%x len=%d\n",
				    address + offs, len));
			err = usbd_do_request(dev, &req, (u_char *)buf);
			if (err)
				break;

			buf += len;
		}
		if (err)
			break;
	}

	return (err);
}

usbd_status
ezload_downloads_and_reset(usbd_device_handle dev, char **names)
{
	usbd_status err;
	size_t buflen;
	u_char *buf;
	int error;

	/*(void)ezload_reset(dev, 1);*/
	err = ezload_reset(dev, 1);
	if (err)
		return (err);
	usbd_delay_ms(dev, 250);

	while (*names != NULL) {
		error = loadfirmware(*names, &buf, &buflen);
		if (error)
			return (error);

		err = ezload_download(dev, *names, buf, buflen);
		free(buf, M_DEVBUF);
		if (err)
			return (err);
		names++;
	}
	if (err)
		return (err);
	usbd_delay_ms(dev, 250);
	/*(void)ezload_reset(dev, 0);*/
	err = ezload_reset(dev, 0);
	usbd_delay_ms(dev, 250);
	return (err);
}