55
56
#define JTAGICE_BULK_EP_READ 0x82
56
57
#define JTAGICE_MAX_XFER 64
58
static int signalled, exiting;
59
static volatile sig_atomic_t signalled, exiting, ready;
59
60
static pid_t usb_kid;
62
* Various signal handlers for the USB daemon child.
64
static void sigtermhandler(int signo)
66
// give the pipes some time to flush before exiting
71
static void alarmhandler(int signo)
76
static void dummyhandler(int signo)
78
/* nothing to do, just abort the current read()/select() */
84
static void kill_daemon(void)
86
kill(usb_kid, SIGTERM);
90
* Signal handler for the parent (i.e. for AVaRICE).
92
static void inthandler(int signo)
96
kill(usb_kid, SIGTERM);
98
signal(signo, SIG_DFL);
99
kill(getpid(), signo);
102
static void childhandler(int signo)
109
#define PRINTERR(msg) write(fileno(stderr), msg, strlen(msg))
110
PRINTERR("USB daemon died\n");
115
* The USB daemon itself. Polls the USB device for data as long as
116
* there is room in the AVaRICE pipe. Polls the AVaRICE descriptor
117
* for data, and sends them to the USB device.
119
static void usb_daemon(usb_dev_handle *udev, int fd, int usb_interface)
123
signal(SIGALRM, alarmhandler);
124
signal(SIGTERM, sigtermhandler);
125
signal(SIGINT, sigtermhandler);
126
if (fcntl(fd, F_GETFL, &ioflags) != -1)
129
if (fcntl(fd, F_SETFL, &ioflags) != -1)
130
signal(SIGIO, dummyhandler);
139
* See if our parent has something to tell us, or room in the
140
* pipe to send something to.
148
if (select(fd + 1, &r, &w, NULL, &tv) > 0)
150
if (FD_ISSET(fd, &r))
152
char buf[JTAGICE_MAX_XFER];
155
if ((rv = read(fd, buf, JTAGICE_MAX_XFER)) > 0)
157
if (usb_bulk_write(udev, JTAGICE_BULK_EP_WRITE, buf,
161
fprintf(stderr, "USB bulk write error: %s\n",
167
if (rv < 0 && errno != EINTR && errno != EAGAIN)
169
fprintf(stderr, "read error from AVaRICE: %s\n",
174
if (FD_ISSET(fd, &w))
176
char buf[JTAGICE_MAX_XFER];
178
rv = usb_bulk_read(udev, JTAGICE_BULK_EP_READ, buf,
179
JTAGICE_MAX_XFER, 100);
180
if (rv == 0 || rv == -EINTR || rv == -EAGAIN || rv == -ETIMEDOUT)
185
fprintf(stderr, "USB bulk read error: %s\n",
189
if (write(fd, buf, rv) != rv)
191
fprintf(stderr, "short write to AVaRICE: %s\n",
198
(void)usb_release_interface(udev, usb_interface);
203
pid_t jtag::openUSB(const char *jtagDeviceName)
63
* Walk down all USB devices, and see whether we can find our emulator
66
static usb_dev_handle *opendev(const char *jtagDeviceName, emulator emu_type,
206
70
struct usb_bus *bus;
207
71
struct usb_device *dev;
208
72
usb_dev_handle *udev;
209
73
char *serno, *cp2;
79
case EMULATOR_JTAGICE:
80
pid = USB_DEVICE_JTAGICEMKII;
84
pid = USB_DEVICE_AVRDRAGON;
214
89
* The syntax for usb devices is defined as:
195
* Various signal handlers for the USB daemon child.
197
static void sigtermhandler(int signo)
199
// give the pipes some time to flush before exiting
204
static void alarmhandler(int signo)
209
static void usr1handler(int signo)
214
static void dummyhandler(int signo)
216
/* nothing to do, just abort the current read()/select() */
222
static void kill_daemon(void)
224
kill(usb_kid, SIGTERM);
228
* Signal handler for the parent (i.e. for AVaRICE).
230
static void inthandler(int signo)
234
kill(usb_kid, SIGTERM);
236
signal(signo, SIG_DFL);
237
kill(getpid(), signo);
240
static void childhandler(int signo)
247
#define PRINTERR(msg) write(fileno(stderr), msg, strlen(msg))
249
PRINTERR("USB daemon died\n");
254
* The USB daemon itself. Polls the USB device for data as long as
255
* there is room in the AVaRICE pipe. Polls the AVaRICE descriptor
256
* for data, and sends them to the USB device.
258
static void usb_daemon(usb_dev_handle *udev, int fd, int cfd, int usb_interface)
260
signal(SIGALRM, alarmhandler);
261
signal(SIGTERM, sigtermhandler);
262
signal(SIGINT, sigtermhandler);
267
if (fcntl(fd, F_GETFL, &ioflags) != -1)
270
if (fcntl(fd, F_SETFL, &ioflags) != -1)
271
signal(SIGIO, dummyhandler);
273
#endif /* defined(O_ASYNC) */
275
int highestfd = fd > cfd? fd: cfd;
276
bool polling = false;
283
bool do_read, clear_eps;
284
char buf[JTAGICE_MAX_XFER];
289
* See if our parent has something to tell us, or requests
305
if (!exiting && select(highestfd + 1, &r, NULL, NULL, &tv) > 0)
307
if (FD_ISSET(fd, &r))
309
if ((rv = read(fd, buf, JTAGICE_MAX_XFER)) > 0)
311
if (usb_bulk_write(udev, JTAGICE_BULK_EP_WRITE, buf,
315
fprintf(stderr, "USB bulk write error: %s\n",
321
if (rv < 0 && errno != EINTR && errno != EAGAIN)
323
fprintf(stderr, "read error from AVaRICE: %s\n",
328
if (FD_ISSET(cfd, &r))
330
char buf[JTAGICE_MAX_XFER];
333
if (FD_ISSET(cfd, &r))
335
if ((rv = read(cfd, cmd, 1)) > 0)
338
* Examine AVaRICE's command.
345
else if (cmd[0] == 'p')
349
else if (cmd[0] == 'c')
355
fprintf(stderr, "unknown command in USB_daemon: %c\n",
359
if (rv < 0 && errno != EINTR && errno != EAGAIN)
361
fprintf(stderr, "read error on control pipe from AVaRICE: %s\n",
371
usb_resetep(udev, JTAGICE_BULK_EP_READ);
372
usb_resetep(udev, JTAGICE_BULK_EP_WRITE);
375
if (!exiting && (do_read || polling))
377
rv = usb_bulk_read(udev, JTAGICE_BULK_EP_READ, buf,
378
JTAGICE_MAX_XFER, 500);
379
if (rv == 0 || rv == -EINTR || rv == -EAGAIN || rv == -ETIMEDOUT)
386
fprintf(stderr, "USB bulk read error: %s\n",
393
* We read a (partial) packet from USB. Return
394
* what we've got so far to AVaRICE, and examine
395
* the length field to see whether we have to
399
if (write(fd, buf, rv) != rv)
401
fprintf(stderr, "short write to AVaRICE: %s\n",
405
unsigned int pkt_len = (unsigned char)buf[3] +
406
((unsigned char)buf[4] << 8) + ((unsigned char)buf[5] << 16) +
407
((unsigned char)buf[6] << 24);
408
const unsigned int header_size = 8;
409
const unsigned int crc_size = 2;
410
pkt_len += header_size + crc_size;
412
/* OK, if there is more to read, do so. */
413
while (!exiting && pkt_len > 0)
415
rv = usb_bulk_read(udev, JTAGICE_BULK_EP_READ, buf,
416
pkt_len > JTAGICE_MAX_XFER? JTAGICE_MAX_XFER: pkt_len,
420
* Zero-length reads are not expected here,
421
* as we carefully examined the packet
422
* length from the header.
424
if (rv == -EINTR || rv == -EAGAIN || rv == -ETIMEDOUT)
432
"USB bulk read error in continuation block: %s\n",
436
if (write(fd, buf, rv) != rv)
438
fprintf(stderr, "short write to AVaRICE: %s\n",
449
pid_t jtag::openUSB(const char *jtagDeviceName)
453
int pype[2], cpipe[2];
454
usb_dev_handle *udev;
315
456
unixCheck(socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pype) == 0,
316
"cannot create pipe");
457
"cannot create pipe");
458
unixCheck(socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, cpipe) == 0,
459
"cannot create control pipe");
461
signal(SIGCHLD, childhandler);
462
signal(SIGUSR1, usr1handler);
318
463
switch ((p = fork()))
322
usb_daemon(udev, pype[1], usb_interface);
466
signal(SIGCHLD, SIG_DFL);
467
signal(SIGUSR1, SIG_DFL);
471
udev = opendev(jtagDeviceName, emu_type, usb_interface);
472
check(udev != NULL, "USB device not found");
473
kill(getppid(), SIGUSR1); // tell the parent we are ready to go
475
usb_daemon(udev, pype[0], cpipe[0], usb_interface);
477
(void)usb_release_interface(udev, usb_interface);
325
unixCheck(0, "Cannot fork");
483
unixCheck(-1, "Cannot fork");
332
493
atexit(kill_daemon);
333
494
signal(SIGTERM, inthandler);
334
495
signal(SIGINT, inthandler);
335
496
signal(SIGQUIT, inthandler);
336
signal(SIGCHLD, childhandler);
499
/* wait for child to become ready */ ;
500
signal(SIGUSR1, SIG_DFL);
339
503
#endif /* HAVE_LIBUSB */