1 /*	$OpenBSD: ezload.c,v 1.8 2005/04/14 19:07:02 damien Exp $ */
2 /*	$NetBSD: ezload.c,v 1.5 2002/07/11 21:14:25 augustss Exp $	*/
3 
4 /*
5  * Copyright (c) 2000 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by  Lennart Augustsson <lennart@augustsson.net>.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *        This product includes software developed by the NetBSD
22  *        Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/device.h>
44 #include <sys/malloc.h>
45 #include <sys/conf.h>
46 
47 #include <dev/usb/usb.h>
48 #include <dev/usb/usbdi.h>
49 #include <dev/usb/usbdi_util.h>
50 
51 #include <dev/usb/ezload.h>
52 
53 /*
54  * Vendor specific request code for Anchor Upload/Download
55  */
56 
57 /* This one is implemented in the core */
58 #define ANCHOR_LOAD_INTERNAL	0xA0
59 
60 /* This is the highest internal RAM address for the AN2131Q */
61 #define ANCHOR_MAX_INTERNAL_ADDRESS  0x1B3F
62 
63 /*
64  * EZ-USB Control and Status Register.  Bit 0 controls 8051 reset
65  */
66 #define ANCHOR_CPUCS_REG	0x7F92
67 #define  ANCHOR_RESET		0x01
68 
69 /*
70  * Although USB does not limit you here, the Anchor docs
71  * quote 64 as a limit, and Mato@activewireinc.com suggested
72  * to use 16.
73  */
74 #define ANCHOR_CHUNK 16
75 
76 /*
77  * This is a firmware loader for ezusb (Anchor) devices. When the firmware
78  * has been downloaded the device will simulate a disconnect and when it
79  * is next recognized by the USB software it will appear as another
80  * device.
81  */
82 
83 #ifdef USB_DEBUG
84 #define DPRINTF(x)	if (ezloaddebug) printf x
85 #define DPRINTFN(n,x)	if (ezloaddebug>(n)) printf x
86 int ezloaddebug = 0;
87 #else
88 #define DPRINTF(x)
89 #define DPRINTFN(n,x)
90 #endif
91 
92 usbd_status
ezload_reset(usbd_device_handle dev,int reset)93 ezload_reset(usbd_device_handle dev, int reset)
94 {
95 	usb_device_request_t req;
96 	uByte rst;
97 
98 	DPRINTF(("ezload_reset: reset=%d\n", reset));
99 
100 	rst = reset ? ANCHOR_RESET : 0;
101 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
102 	req.bRequest = ANCHOR_LOAD_INTERNAL;
103 	USETW(req.wValue, ANCHOR_CPUCS_REG);
104 	USETW(req.wIndex, 0);
105 	USETW(req.wLength, 1);
106 	return (usbd_do_request(dev, &req, &rst));
107 }
108 
109 usbd_status
ezload_download(usbd_device_handle dev,const char * name,const u_char * buf,size_t buflen)110 ezload_download(usbd_device_handle dev, const char *name, const u_char *buf,
111     size_t buflen)
112 {
113 	usb_device_request_t req;
114 	usbd_status err = 0;
115 	u_int8_t length;
116 	u_int16_t address;
117 	u_int len, offs;
118 
119 	for (;;) {
120 		length = *buf++;
121 		if (length == 0)
122 			break;
123 
124 		address = UGETW(buf); buf += 2;
125 #if 0
126 		if (address + length > ANCHOR_MAX_INTERNAL_ADDRESS)
127 			return (USBD_INVAL);
128 #endif
129 
130 		req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
131 		req.bRequest = ANCHOR_LOAD_INTERNAL;
132 		USETW(req.wIndex, 0);
133 		for (offs = 0; offs < length; offs += ANCHOR_CHUNK) {
134 			len = length - offs;
135 			if (len > ANCHOR_CHUNK)
136 				len = ANCHOR_CHUNK;
137 			USETW(req.wValue, address + offs);
138 			USETW(req.wLength, len);
139 			DPRINTFN(2,("ezload_download: addr=0x%x len=%d\n",
140 				    address + offs, len));
141 			err = usbd_do_request(dev, &req, (u_char *)buf);
142 			if (err)
143 				break;
144 
145 			buf += len;
146 		}
147 		if (err)
148 			break;
149 	}
150 
151 	return (err);
152 }
153 
154 usbd_status
ezload_downloads_and_reset(usbd_device_handle dev,char ** names)155 ezload_downloads_and_reset(usbd_device_handle dev, char **names)
156 {
157 	usbd_status err;
158 	size_t buflen;
159 	u_char *buf;
160 	int error;
161 
162 	/*(void)ezload_reset(dev, 1);*/
163 	err = ezload_reset(dev, 1);
164 	if (err)
165 		return (err);
166 	usbd_delay_ms(dev, 250);
167 
168 	while (*names != NULL) {
169 		error = loadfirmware(*names, &buf, &buflen);
170 		if (error)
171 			return (error);
172 
173 		err = ezload_download(dev, *names, buf, buflen);
174 		free(buf, M_DEVBUF);
175 		if (err)
176 			return (err);
177 		names++;
178 	}
179 	if (err)
180 		return (err);
181 	usbd_delay_ms(dev, 250);
182 	/*(void)ezload_reset(dev, 0);*/
183 	err = ezload_reset(dev, 0);
184 	usbd_delay_ms(dev, 250);
185 	return (err);
186 }
187