Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 449 lines (391 sloc) 10.742 kb
fc828ae piece of shit.
mellis authored
1 /*
2 * avrdude - A Downloader/Uploader for AVR device programmers
3 * Copyright (C) 2005,2006 Joerg Wunsch
4 * Copyright (C) 2006 David Moore
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 2 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, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 /* $Id: usb_libusb.c,v 1.11 2007/01/24 21:07:54 joerg_wunsch Exp $ */
22
23 /*
24 * USB interface via libusb for avrdude.
25 */
26
27 #include "ac_cfg.h"
28 #if defined(HAVE_LIBUSB)
29
30
31 #include <ctype.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <errno.h>
36 #include <sys/types.h>
37 #include <sys/time.h>
38
39 #include <usb.h>
40
41 #include "avrdude.h"
42 #include "serial.h"
43 #include "usbdevs.h"
44
45 static char usbbuf[USBDEV_MAX_XFER];
46 static int buflen = -1, bufptr;
47
48 static int usb_interface;
49
50 /*
51 * The "baud" parameter is meaningless for USB devices, so we reuse it
52 * to pass the desired USB device ID.
53 */
54 static void usbdev_open(char * port, long baud, union filedescriptor *fd)
55 {
56 char string[256];
57 char product[256];
58 struct usb_bus *bus;
59 struct usb_device *dev;
60 usb_dev_handle *udev;
61 char *serno, *cp2;
62 size_t x;
63
64 /*
65 * The syntax for usb devices is defined as:
66 *
67 * -P usb[:serialnumber]
68 *
69 * See if we've got a serial number passed here. The serial number
70 * might contain colons which we remove below, and we compare it
71 * right-to-left, so only the least significant nibbles need to be
72 * specified.
73 */
74 if ((serno = strchr(port, ':')) != NULL)
75 {
76 /* first, drop all colons there if any */
77 cp2 = ++serno;
78
79 while ((cp2 = strchr(cp2, ':')) != NULL)
80 {
81 x = strlen(cp2) - 1;
82 memmove(cp2, cp2 + 1, x);
83 cp2[x] = '\0';
84 }
85
86 if (strlen(serno) > 12)
87 {
88 fprintf(stderr,
89 "%s: usbdev_open(): invalid serial number \"%s\"\n",
90 progname, serno);
91 exit(1);
92 }
93 }
94
95 usb_init();
96
97 usb_find_busses();
98 usb_find_devices();
99
100 for (bus = usb_get_busses(); bus; bus = bus->next)
101 {
102 for (dev = bus->devices; dev; dev = dev->next)
103 {
104 udev = usb_open(dev);
105 if (udev)
106 {
107 if (dev->descriptor.idVendor == USB_VENDOR_ATMEL &&
108 dev->descriptor.idProduct == (unsigned short)baud)
109 {
110 /* yeah, we found something */
111 if (usb_get_string_simple(udev,
112 dev->descriptor.iSerialNumber,
113 string, sizeof(string)) < 0)
114 {
115 fprintf(stderr,
116 "%s: usb_open(): cannot read serial number \"%s\"\n",
117 progname, usb_strerror());
118 /*
119 * On some systems, libusb appears to have
120 * problems sending control messages. Catch the
121 * benign case where the user did not request a
122 * particular serial number, so we could
123 * continue anyway.
124 */
125 if (serno != NULL)
126 exit(1); /* no chance */
127 else
128 strcpy(string, "[unknown]");
129 }
130
131 if (usb_get_string_simple(udev,
132 dev->descriptor.iProduct,
133 product, sizeof(product)) < 0)
134 {
135 fprintf(stderr,
136 "%s: usb_open(): cannot read product name \"%s\"\n",
137 progname, usb_strerror());
138 strcpy(product, "[unnamed product]");
139 }
140
141 if (verbose)
142 fprintf(stderr,
143 "%s: usbdev_open(): Found %s, serno: %s\n",
144 progname, product, string);
145 if (serno != NULL)
146 {
147 /*
148 * See if the serial number requested by the
149 * user matches what we found, matching
150 * right-to-left.
151 */
152 x = strlen(string) - strlen(serno);
153 if (strcasecmp(string + x, serno) != 0)
154 {
155 if (verbose > 2)
156 fprintf(stderr,
157 "%s: usbdev_open(): serial number doesn't match\n",
158 progname);
159 usb_close(udev);
160 continue;
161 }
162 }
163
164 if (dev->config == NULL)
165 {
166 fprintf(stderr,
167 "%s: usbdev_open(): USB device has no configuration\n",
168 progname);
169 goto trynext;
170 }
171
172 if (usb_set_configuration(udev, dev->config[0].bConfigurationValue))
173 {
174 fprintf(stderr,
175 "%s: usbdev_open(): error setting configuration %d: %s\n",
176 progname, dev->config[0].bConfigurationValue,
177 usb_strerror());
178 goto trynext;
179 }
180
181 usb_interface = dev->config[0].interface[0].altsetting[0].bInterfaceNumber;
182 if (usb_claim_interface(udev, usb_interface))
183 {
184 fprintf(stderr,
185 "%s: usbdev_open(): error claiming interface %d: %s\n",
186 progname, usb_interface, usb_strerror());
187 goto trynext;
188 }
189
190 fd->pfd = udev;
191 return;
192 }
193 trynext:
194 usb_close(udev);
195 }
196 }
197 }
198
199 fprintf(stderr, "%s: usbdev_open(): did not find any%s USB device \"%s\"\n",
200 progname, serno? " (matching)": "", port);
201 exit(1);
202 }
203
204 static void usbdev_close(union filedescriptor *fd)
205 {
206 usb_dev_handle *udev = (usb_dev_handle *)fd->pfd;
207
208 (void)usb_release_interface(udev, usb_interface);
209
210 /*
211 * Without this reset, the AVRISP mkII seems to stall the second
212 * time we try to connect to it.
213 */
214 usb_reset(udev);
215
216 usb_close(udev);
217 }
218
219
220 static int usbdev_send(union filedescriptor *fd, unsigned char *bp, size_t mlen)
221 {
222 usb_dev_handle *udev = (usb_dev_handle *)fd->pfd;
223 int rv;
224 int i = mlen;
225 unsigned char * p = bp;
226 int tx_size;
227
228 /*
229 * Split the frame into multiple packets. It's important to make
230 * sure we finish with a short packet, or else the device won't know
231 * the frame is finished. For example, if we need to send 64 bytes,
232 * we must send a packet of length 64 followed by a packet of length
233 * 0.
234 */
235 do {
236 tx_size = (mlen < USBDEV_MAX_XFER)? mlen: USBDEV_MAX_XFER;
237 rv = usb_bulk_write(udev, USBDEV_BULK_EP_WRITE, (char *)bp, tx_size, 5000);
238 if (rv != tx_size)
239 {
240 fprintf(stderr, "%s: usbdev_send(): wrote %d out of %d bytes, err = %s\n",
241 progname, rv, tx_size, usb_strerror());
242 return -1;
243 }
244 bp += tx_size;
245 mlen -= tx_size;
246 } while (tx_size == USBDEV_MAX_XFER);
247
248 if (verbose > 3)
249 {
250 fprintf(stderr, "%s: Sent: ", progname);
251
252 while (i) {
253 unsigned char c = *p;
254 if (isprint(c)) {
255 fprintf(stderr, "%c ", c);
256 }
257 else {
258 fprintf(stderr, ". ");
259 }
260 fprintf(stderr, "[%02x] ", c);
261
262 p++;
263 i--;
264 }
265 fprintf(stderr, "\n");
266 }
267 return 0;
268 }
269
270 /*
271 * As calls to usb_bulk_read() result in exactly one USB request, we
272 * have to buffer the read results ourselves, so the single-char read
273 * requests performed by the upper layers will be handled. In order
274 * to do this, we maintain a private buffer of what we've got so far,
275 * and transparently issue another USB read request if the buffer is
276 * empty and more data are requested.
277 */
278 static int
279 usb_fill_buf(usb_dev_handle *udev)
280 {
281 int rv;
282
283 rv = usb_bulk_read(udev, USBDEV_BULK_EP_READ, usbbuf, USBDEV_MAX_XFER, 5000);
284 if (rv < 0)
285 {
286 if (verbose > 1)
287 fprintf(stderr, "%s: usb_fill_buf(): usb_bulk_read() error %s\n",
288 progname, usb_strerror());
289 return -1;
290 }
291
292 buflen = rv;
293 bufptr = 0;
294
295 return 0;
296 }
297
298 static int usbdev_recv(union filedescriptor *fd, unsigned char *buf, size_t nbytes)
299 {
300 usb_dev_handle *udev = (usb_dev_handle *)fd->pfd;
301 int i, amnt;
302 unsigned char * p = buf;
303
304 for (i = 0; nbytes > 0;)
305 {
306 if (buflen <= bufptr)
307 {
308 if (usb_fill_buf(udev) < 0)
309 return -1;
310 }
311 amnt = buflen - bufptr > nbytes? nbytes: buflen - bufptr;
312 memcpy(buf + i, usbbuf + bufptr, amnt);
313 bufptr += amnt;
314 nbytes -= amnt;
315 i += amnt;
316 }
317
318 if (verbose > 3)
319 {
320 fprintf(stderr, "%s: Recv: ", progname);
321
322 while (i) {
323 unsigned char c = *p;
324 if (isprint(c)) {
325 fprintf(stderr, "%c ", c);
326 }
327 else {
328 fprintf(stderr, ". ");
329 }
330 fprintf(stderr, "[%02x] ", c);
331
332 p++;
333 i--;
334 }
335 fprintf(stderr, "\n");
336 }
337
338 return 0;
339 }
340
341 /*
342 * This version of recv keeps reading packets until we receive a short
343 * packet. Then, the entire frame is assembled and returned to the
344 * user. The length will be unknown in advance, so we return the
345 * length as the return value of this function, or -1 in case of an
346 * error.
347 *
348 * This is used for the AVRISP mkII device.
349 */
350 static int usbdev_recv_frame(union filedescriptor *fd, unsigned char *buf, size_t nbytes)
351 {
352 usb_dev_handle *udev = (usb_dev_handle *)fd->pfd;
353 int rv, n;
354 int i;
355 unsigned char * p = buf;
356
357 n = 0;
358 do
359 {
360 rv = usb_bulk_read(udev, USBDEV_BULK_EP_READ, usbbuf,
361 USBDEV_MAX_XFER, 10000);
362 if (rv < 0)
363 {
364 if (verbose > 1)
365 fprintf(stderr, "%s: usbdev_recv_frame(): usb_bulk_read(): %s\n",
366 progname, usb_strerror());
367 return -1;
368 }
369
370 if (rv <= nbytes)
371 {
372 memcpy (buf, usbbuf, rv);
373 buf += rv;
374 }
375
376 n += rv;
377 nbytes -= rv;
378 }
379 while (rv == USBDEV_MAX_XFER);
380
381 if (nbytes < 0)
382 return -1;
383
384 if (verbose > 3)
385 {
386 i = n;
387 fprintf(stderr, "%s: Recv: ", progname);
388
389 while (i) {
390 unsigned char c = *p;
391 if (isprint(c)) {
392 fprintf(stderr, "%c ", c);
393 }
394 else {
395 fprintf(stderr, ". ");
396 }
397 fprintf(stderr, "[%02x] ", c);
398
399 p++;
400 i--;
401 }
402 fprintf(stderr, "\n");
403 }
404 return n;
405 }
406
407 static int usbdev_drain(union filedescriptor *fd, int display)
408 {
409 usb_dev_handle *udev = (usb_dev_handle *)fd->pfd;
410 int rv;
411
412 do {
413 rv = usb_bulk_read(udev, USBDEV_BULK_EP_READ, usbbuf, USBDEV_MAX_XFER, 100);
414 if (rv > 0 && verbose >= 4)
415 fprintf(stderr, "%s: usbdev_drain(): flushed %d characters\n",
416 progname, rv);
417 } while (rv > 0);
418
419 return 0;
420 }
421
422 /*
423 * Device descriptor for the JTAG ICE mkII.
424 */
425 struct serial_device usb_serdev =
426 {
427 .open = usbdev_open,
428 .close = usbdev_close,
429 .send = usbdev_send,
430 .recv = usbdev_recv,
431 .drain = usbdev_drain,
432 .flags = SERDEV_FL_NONE,
433 };
434
435 /*
436 * Device descriptor for the AVRISP mkII.
437 */
438 struct serial_device usb_serdev_frame =
439 {
440 .open = usbdev_open,
441 .close = usbdev_close,
442 .send = usbdev_send,
443 .recv = usbdev_recv_frame,
444 .drain = usbdev_drain,
445 .flags = SERDEV_FL_NONE,
446 };
447
448 #endif /* HAVE_LIBUSB */
Something went wrong with that request. Please try again.