1 /*
2  * Copyright (c) 2002,03 Bernd Walter Computer Technology
3  * http://www.bwct.de
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the author nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * Thanks to FTDI for providing me the programming specs.
29  *
30  * $URL: https://devel.bwct.de/svn/projects/usb/fdti232/eeprom/trunk/ftdi-eeprom.c $
31  * $Date: 2003-02-16 05:02:35 +0100 (Sun, 16 Feb 2003) $
32  * $Author: ticso $
33  * $Rev: 309 $
34  */
35 
36 #include <sys/ioctl.h>
37 #include <sys/param.h>
38 #include <sys/types.h>
39 #include <errno.h>
40 #include <fcntl.h>
41 #include <stdio.h>
42 #include <unistd.h>
43 #include <stdlib.h>
44 #include <stdio.h>
45 #include <string.h>
46 #include <limits.h>
47 #ifdef __DragonFly__
48 #include <bus/u4b/usb.h>
49 #include <bus/u4b/usb_ioctl.h>
50 #else
51 #include <dev/usb/usb.h>
52 #include <dev/usb/usb_ioctl.h>
53 #endif
54 
55 #include "ftdi-eeprom.h"
56 
57 static int fd;
58 static int do_write;
59 
60 /*
61  * ftdi-eeprom: programm to write 93c46 EEPROM devices on
62  * FTDI usb chips over usb
63  */
64 int
main(int argc,char * argv[])65 main(int argc, char *argv[])
66 {
67 	int ch;
68 	static char *devname;
69 	static int addr;
70 	union {
71 		u_int8_t *d8;
72 		u_int16_t *d16;
73 	} data;
74 	int wptr;
75 	u_int16_t vendor_id, product_id, release_number;
76 	int bus_powered, remote_wakeup;
77 	u_int8_t current;
78 	char* manufacturer_string;
79 	char* product_string;
80 	char* serial_string;
81 	u_int16_t checksum, tmpsum;
82 
83 	while ((ch = getopt(argc, argv, "wa:f:")) != -1)
84 		switch (ch) {
85 		case 'a':
86 			addr = strtol(optarg, NULL, 0);
87 			break;
88 		case 'f':
89 			devname = optarg;
90 			break;
91 		case 'w':
92 			do_write = 1;
93 			break;
94 		default:
95 			usage();
96 			/* NOTREACHED */
97 		}
98 	argc -= optind;
99 	argv += optind;
100 
101 	if (devname == NULL) {
102 		usage();
103 		/* NOTREACHED */
104 	}
105 
106 	fd = open(devname, O_RDWR);
107 	if (fd < 0) {
108 		fprintf(stderr, "Failed to open device: %s %s\n", devname,
109 		    strerror(errno));
110 		exit(1);
111 	}
112 
113 	data.d8 = (u_int8_t*)malloc(128);
114 	if (data.d8 == NULL) {
115 		fprintf(stderr, "failed to malloc 128 Bytes\n");
116 		exit(1);
117 	}
118 	bzero(data.d8, 128);
119 
120 	if (do_write) {
121 		if (argc != 9) {
122 			free(data.d8);
123 			usage();
124 			/* NOTREACHED */
125 		}
126 		vendor_id = strtol(argv[0], NULL, 0);
127 		product_id = strtol(argv[1], NULL, 0);
128 		release_number = strtol(argv[2], NULL, 0);
129 		bus_powered = (strcmp(argv[3], "1") == 0) ? 1 : 0;
130 		remote_wakeup = (strcmp(argv[4], "1") == 0) ? 1 : 0;
131 		current = strtol(argv[5], NULL, 0) / 2;
132 		manufacturer_string = argv[6];
133 		product_string = argv[7];
134 		serial_string = argv[8];
135 		data.d16[0x2/2] = vendor_id;
136 		data.d16[0x4/2] = product_id;
137 		data.d16[0x6/2] = release_number;
138 		data.d8[0x8 ^ END8] = 0x80;
139 		data.d8[0x8 ^ END8] |= (bus_powered) ? 0 : 0x40;
140 		data.d8[0x8 ^ END8] |= (remote_wakeup) ? 0x20 : 0;
141 		data.d8[0x9 ^ END8] = current;
142 		wptr = 0x14;
143 		data.d8[0xe ^ END8] = wptr + 0x80;
144 		data.d8[0xf ^ END8] = strlen(manufacturer_string) * 2 + 2;
145 		data.d8[wptr++ ^ END8] = data.d8[0xf ^ END8];
146 		data.d8[wptr++ ^ END8] = 0x03;
147 		while (*manufacturer_string != '\0') {
148 			data.d16[wptr/2] = *(manufacturer_string++);
149 			wptr += 2;
150 		}
151 		data.d8[0x10 ^ END8] = wptr + 0x80;
152 		data.d8[0x11 ^ END8] = strlen(product_string) * 2 + 2;
153 		data.d8[wptr++ ^ END8] = data.d8[0x11 ^ END8];
154 		data.d8[wptr++ ^ END8] = 0x03;
155 		while (*product_string != '\0') {
156 			data.d16[wptr/2] = *(product_string++);
157 			wptr += 2;
158 		}
159 		data.d8[0x12 ^ END8] = wptr + 0x80;
160 		data.d8[0x13 ^ END8] = strlen(serial_string) * 2 + 2;
161 		data.d8[wptr++ ^ END8] = data.d8[0x13 ^ END8];
162 		data.d8[wptr++ ^ END8] = 0x03;
163 		while (*serial_string != '\0') {
164 			data.d16[wptr/2] = *(serial_string++);
165 			wptr += 2;
166 		}
167 
168 		checksum = 0xaaaa;
169 		for (wptr = 0; wptr < 0x3f; wptr++) {
170 			writecell(wptr, data.d16[wptr], addr);
171 			tmpsum = data.d16[wptr] ^ checksum;
172 			checksum = tmpsum << 1;
173 			if (tmpsum & 0x8000)
174 				checksum |= 0x01;
175 		}
176 		writecell(wptr, checksum, addr);
177 	}
178 
179 	free(data.d8);
180 	close(fd);
181 	return 0;
182 }
183 
184 int
writecell(int num,u_int16_t val,int addr)185 writecell (int num, u_int16_t val, int addr) {
186 	struct usb_ctl_request request;
187 	int res;
188 
189 	request.ucr_addr = addr;
190 	request.ucr_request.bmRequestType = 0x40;
191 	request.ucr_request.bRequest = 0x91;
192 	request.ucr_request.wValue[0] = val & 0xff;
193 	request.ucr_request.wValue[1] = val >> 8;
194 	request.ucr_request.wIndex[0] = num & 0xff;
195 	request.ucr_request.wIndex[1] = num >> 8;
196 	request.ucr_request.wLength[0] = 0x00;
197 	request.ucr_request.wLength[1] = 0x00;
198 	request.ucr_data = NULL;
199 	request.ucr_flags = 0;
200 	request.ucr_actlen = 0;
201 	if (addr)
202 		res = ioctl(fd, USB_REQUEST, &request);
203 	else
204 		res = ioctl(fd, USB_DO_REQUEST, &request);
205 	if (res < 0) {
206 		fprintf(stderr, "failed to write value %d into cell %d %s\n",
207 		    val, num, strerror(errno));
208 	}
209 	return res;
210 }
211 
212 void
usage()213 usage()
214 {
215 
216 	fprintf(stderr, "ftdi-eeprom V1.0\n");
217 	fprintf(stderr, "(c) 2002,03 by Bernd Walter Computer Technology\n");
218 	fprintf(stderr, "All rights reserved.\n");
219 	fprintf(stderr,
220 	    "usage: ftdi-eeprom -f dev [-a addr] -w vendor_id product_id \\\n"
221 	    "       release_number bus_powered remote_wakeup current \\\n"
222 	    "       manufacturer_string product_string serial_string\n");
223 	exit(1);
224 }
225 
226