~cosmos-door/ubuntu/quantal/avarice/fix-ftbfs-1058667

« back to all changes in this revision

Viewing changes to src/jtag2usb.cc

  • Committer: Bazaar Package Importer
  • Author(s): Shaun Jackman
  • Date: 2008-01-21 12:42:25 UTC
  • mfrom: (3.1.2 hardy)
  • Revision ID: james.westby@ubuntu.com-20080121124225-jpgg64fki5b2clqs
Tags: 2.7-2
* Add the AVR Dragon to the udev rules. Closes: #461986.
* Update the Debian policy to version 3.7.3.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
 *      avarice - The "avarice" program.
3
 
 *      Copyright (C) 2005 Joerg Wunsch
 
3
 *      Copyright (C) 2005, 2007 Joerg Wunsch
4
4
 *
5
5
 *      This program is free software; you can redistribute it and/or modify
6
6
 *      it under the terms of the GNU General Public License Version 2
18
18
 * This file implements the libusb-based USB connection to a JTAG ICE
19
19
 * mkII.
20
20
 *
21
 
 * $Id: jtag2usb.cc,v 1.4 2005/10/28 21:04:49 joerg_wunsch Exp $
 
21
 * $Id: jtag2usb.cc,v 1.9 2007/02/17 22:41:46 joerg_wunsch Exp $
22
22
 */
23
23
 
24
24
 
47
47
 
48
48
#define USB_VENDOR_ATMEL 1003
49
49
#define USB_DEVICE_JTAGICEMKII 0x2103
 
50
#define USB_DEVICE_AVRDRAGON   0x2107
50
51
/*
51
52
 * Should we query the endpoint number and max transfer size from USB?
52
53
 * After all, the JTAG ICE mkII docs document these values.
55
56
#define JTAGICE_BULK_EP_READ  0x82
56
57
#define JTAGICE_MAX_XFER 64
57
58
 
58
 
static int signalled, exiting;
 
59
static volatile sig_atomic_t signalled, exiting, ready;
59
60
static pid_t usb_kid;
60
61
 
61
62
/*
62
 
 * Various signal handlers for the USB daemon child.
63
 
 */
64
 
static void sigtermhandler(int signo)
65
 
{
66
 
  // give the pipes some time to flush before exiting
67
 
  exiting++;
68
 
  alarm(3);
69
 
}
70
 
 
71
 
static void alarmhandler(int signo)
72
 
{
73
 
  signalled++;
74
 
}
75
 
 
76
 
static void dummyhandler(int signo)
77
 
{
78
 
  /* nothing to do, just abort the current read()/select() */
79
 
}
80
 
 
81
 
/*
82
 
 * atexit() handler
83
 
 */
84
 
static void kill_daemon(void)
85
 
{
86
 
  kill(usb_kid, SIGTERM);
87
 
}
88
 
 
89
 
/*
90
 
 * Signal handler for the parent (i.e. for AVaRICE).
91
 
 */
92
 
static void inthandler(int signo)
93
 
{
94
 
  int status;
95
 
 
96
 
  kill(usb_kid, SIGTERM);
97
 
  (void)wait(&status);
98
 
  signal(signo, SIG_DFL);
99
 
  kill(getpid(), signo);
100
 
}
101
 
 
102
 
static void childhandler(int signo)
103
 
{
104
 
  int status;
105
 
  char b[500];
106
 
 
107
 
  (void)wait(&status);
108
 
 
109
 
#define PRINTERR(msg) write(fileno(stderr), msg, strlen(msg))
110
 
  PRINTERR("USB daemon died\n");
111
 
  _exit(1);
112
 
}
113
 
 
114
 
/*
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.
118
 
 */
119
 
static void usb_daemon(usb_dev_handle *udev, int fd, int usb_interface)
120
 
{
121
 
  int ioflags;
122
 
 
123
 
  signal(SIGALRM, alarmhandler);
124
 
  signal(SIGTERM, sigtermhandler);
125
 
  signal(SIGINT, sigtermhandler);
126
 
  if (fcntl(fd, F_GETFL, &ioflags) != -1)
127
 
    {
128
 
      ioflags |= O_ASYNC;
129
 
      if (fcntl(fd, F_SETFL, &ioflags) != -1)
130
 
        signal(SIGIO, dummyhandler);
131
 
    }
132
 
 
133
 
  for (; !signalled;)
134
 
    {
135
 
      fd_set r, w;
136
 
      struct timeval tv;
137
 
 
138
 
      /*
139
 
       * See if our parent has something to tell us, or room in the
140
 
       * pipe to send something to.
141
 
       */
142
 
      FD_ZERO(&r);
143
 
      FD_ZERO(&w);
144
 
      FD_SET(fd, &r);
145
 
      FD_SET(fd, &w);
146
 
      tv.tv_sec = 0;
147
 
      tv.tv_usec = 0;
148
 
      if (select(fd + 1, &r, &w, NULL, &tv) > 0)
149
 
        {
150
 
          if (FD_ISSET(fd, &r))
151
 
            {
152
 
              char buf[JTAGICE_MAX_XFER];
153
 
              ssize_t rv;
154
 
 
155
 
              if ((rv = read(fd, buf, JTAGICE_MAX_XFER)) > 0)
156
 
                {
157
 
                  if (usb_bulk_write(udev, JTAGICE_BULK_EP_WRITE, buf,
158
 
                                     rv, 5000) !=
159
 
                      rv)
160
 
                    {
161
 
                      fprintf(stderr, "USB bulk write error: %s\n",
162
 
                              usb_strerror());
163
 
                      exit(1);
164
 
                    }
165
 
                  continue;
166
 
                }
167
 
              if (rv < 0 && errno != EINTR && errno != EAGAIN)
168
 
                {
169
 
                  fprintf(stderr, "read error from AVaRICE: %s\n",
170
 
                          strerror(errno));
171
 
                  exit(1);
172
 
                }
173
 
            }
174
 
          if (FD_ISSET(fd, &w))
175
 
            {
176
 
              char buf[JTAGICE_MAX_XFER];
177
 
              int rv;
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)
181
 
                continue;
182
 
              if (rv < 0)
183
 
                {
184
 
                  if (!exiting)
185
 
                    fprintf(stderr, "USB bulk read error: %s\n",
186
 
                            usb_strerror());
187
 
                  exit(1);
188
 
                }
189
 
              if (write(fd, buf, rv) != rv)
190
 
                {
191
 
                  fprintf(stderr, "short write to AVaRICE: %s\n",
192
 
                          strerror(errno));
193
 
                  exit(1);
194
 
                }
195
 
            }
196
 
        }
197
 
    }
198
 
  (void)usb_release_interface(udev, usb_interface);
199
 
  usb_close(udev);
200
 
  exit(0);
201
 
}
202
 
 
203
 
pid_t jtag::openUSB(const char *jtagDeviceName)
 
63
 * Walk down all USB devices, and see whether we can find our emulator
 
64
 * device.
 
65
 */
 
66
static usb_dev_handle *opendev(const char *jtagDeviceName, emulator emu_type,
 
67
                               int &usb_interface)
204
68
{
205
69
  char string[256];
206
70
  struct usb_bus *bus;
207
71
  struct usb_device *dev;
208
72
  usb_dev_handle *udev;
209
73
  char *serno, *cp2;
210
 
  int usb_interface;
 
74
  u_int16_t pid;
211
75
  size_t x;
212
76
 
 
77
  switch (emu_type)
 
78
    {
 
79
    case EMULATOR_JTAGICE:
 
80
      pid = USB_DEVICE_JTAGICEMKII;
 
81
      break;
 
82
 
 
83
    case EMULATOR_DRAGON:
 
84
      pid = USB_DEVICE_AVRDRAGON;
 
85
      break;
 
86
    }
 
87
 
213
88
  /*
214
89
   * The syntax for usb devices is defined as:
215
90
   *
250
125
          if (udev)
251
126
            {
252
127
              if (dev->descriptor.idVendor == USB_VENDOR_ATMEL &&
253
 
                  dev->descriptor.idProduct == USB_DEVICE_JTAGICEMKII)
 
128
                  dev->descriptor.idProduct == pid)
254
129
                {
255
130
                  /* yeah, we found something */
256
131
                  int rv = usb_get_string_simple(udev,
284
159
            }
285
160
        }
286
161
    }
287
 
  unixCheck(found? 0: -1,
288
 
            "did not find any%s USB device \"%s\"",
289
 
            serno? " (matching)": "", jtagDeviceName);
 
162
  if (!found)
 
163
  {
 
164
    printf("did not find any%s USB device \"%s\"\n",
 
165
           serno? " (matching)": "", jtagDeviceName);
 
166
    return NULL;
 
167
  }
290
168
 
291
169
  if (dev->config == NULL)
292
170
  {
293
171
      statusOut("USB device has no configuration\n");
294
172
    fail:
295
173
      usb_close(udev);
296
 
      exit(EXIT_FAILURE);
 
174
      return NULL;
297
175
  }
298
176
  if (usb_set_configuration(udev, dev->config[0].bConfigurationValue))
299
177
  {
310
188
      goto fail;
311
189
  }
312
190
 
 
191
  return udev;
 
192
}
 
193
 
 
194
/*
 
195
 * Various signal handlers for the USB daemon child.
 
196
 */
 
197
static void sigtermhandler(int signo)
 
198
{
 
199
  // give the pipes some time to flush before exiting
 
200
  exiting++;
 
201
  alarm(1);
 
202
}
 
203
 
 
204
static void alarmhandler(int signo)
 
205
{
 
206
  signalled++;
 
207
}
 
208
 
 
209
static void usr1handler(int signo)
 
210
{
 
211
  ready++;
 
212
}
 
213
 
 
214
static void dummyhandler(int signo)
 
215
{
 
216
  /* nothing to do, just abort the current read()/select() */
 
217
}
 
218
 
 
219
/*
 
220
 * atexit() handler
 
221
 */
 
222
static void kill_daemon(void)
 
223
{
 
224
  kill(usb_kid, SIGTERM);
 
225
}
 
226
 
 
227
/*
 
228
 * Signal handler for the parent (i.e. for AVaRICE).
 
229
 */
 
230
static void inthandler(int signo)
 
231
{
 
232
  int status;
 
233
 
 
234
  kill(usb_kid, SIGTERM);
 
235
  (void)wait(&status);
 
236
  signal(signo, SIG_DFL);
 
237
  kill(getpid(), signo);
 
238
}
 
239
 
 
240
static void childhandler(int signo)
 
241
{
 
242
  int status;
 
243
  char b[500];
 
244
 
 
245
  (void)wait(&status);
 
246
 
 
247
#define PRINTERR(msg) write(fileno(stderr), msg, strlen(msg))
 
248
  if (ready)
 
249
    PRINTERR("USB daemon died\n");
 
250
  _exit(1);
 
251
}
 
252
 
 
253
/*
 
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.
 
257
 */
 
258
static void usb_daemon(usb_dev_handle *udev, int fd, int cfd, int usb_interface)
 
259
{
 
260
  signal(SIGALRM, alarmhandler);
 
261
  signal(SIGTERM, sigtermhandler);
 
262
  signal(SIGINT, sigtermhandler);
 
263
 
 
264
#if defined(O_ASYNC)
 
265
  int ioflags;
 
266
 
 
267
  if (fcntl(fd, F_GETFL, &ioflags) != -1)
 
268
    {
 
269
      ioflags |= O_ASYNC;
 
270
      if (fcntl(fd, F_SETFL, &ioflags) != -1)
 
271
        signal(SIGIO, dummyhandler);
 
272
    }
 
273
#endif /* defined(O_ASYNC) */
 
274
 
 
275
  int highestfd = fd > cfd? fd: cfd;
 
276
  bool polling = false;
 
277
 
 
278
  for (; !signalled;)
 
279
    {
 
280
      fd_set r;
 
281
      struct timeval tv;
 
282
      int rv;
 
283
      bool do_read, clear_eps;
 
284
      char buf[JTAGICE_MAX_XFER];
 
285
 
 
286
      do_read = false;
 
287
      clear_eps = false;
 
288
      /*
 
289
       * See if our parent has something to tell us, or requests
 
290
       * something from us.
 
291
       */
 
292
      FD_ZERO(&r);
 
293
      FD_SET(fd, &r);
 
294
      FD_SET(cfd, &r);
 
295
      if (polling)
 
296
        {
 
297
          tv.tv_sec = 0;
 
298
          tv.tv_usec = 100000;
 
299
        }
 
300
      else
 
301
        {
 
302
          tv.tv_sec = 1;
 
303
          tv.tv_usec = 0;
 
304
        }
 
305
      if (!exiting && select(highestfd + 1, &r, NULL, NULL, &tv) > 0)
 
306
        {
 
307
          if (FD_ISSET(fd, &r))
 
308
            {
 
309
              if ((rv = read(fd, buf, JTAGICE_MAX_XFER)) > 0)
 
310
                {
 
311
                  if (usb_bulk_write(udev, JTAGICE_BULK_EP_WRITE, buf,
 
312
                                     rv, 5000) !=
 
313
                      rv)
 
314
                    {
 
315
                      fprintf(stderr, "USB bulk write error: %s\n",
 
316
                              usb_strerror());
 
317
                      exit(1);
 
318
                    }
 
319
                  continue;
 
320
                }
 
321
              if (rv < 0 && errno != EINTR && errno != EAGAIN)
 
322
                {
 
323
                  fprintf(stderr, "read error from AVaRICE: %s\n",
 
324
                          strerror(errno));
 
325
                  exit(1);
 
326
                }
 
327
            }
 
328
          if (FD_ISSET(cfd, &r))
 
329
            {
 
330
              char buf[JTAGICE_MAX_XFER];
 
331
              char cmd[1];
 
332
 
 
333
              if (FD_ISSET(cfd, &r))
 
334
                {
 
335
                  if ((rv = read(cfd, cmd, 1)) > 0)
 
336
                    {
 
337
                      /*
 
338
                       * Examine AVaRICE's command.
 
339
                       */
 
340
                      if (cmd[0] == 'r')
 
341
                        {
 
342
                          polling = false;
 
343
                          do_read = true;
 
344
                        }
 
345
                      else if (cmd[0] == 'p')
 
346
                        {
 
347
                          polling = true;
 
348
                        }
 
349
                      else if (cmd[0] == 'c')
 
350
                        {
 
351
                          clear_eps = true;
 
352
                        }
 
353
                      else
 
354
                        {
 
355
                          fprintf(stderr, "unknown command in USB_daemon: %c\n",
 
356
                                  cmd[0]);
 
357
                        }
 
358
                    }
 
359
                  if (rv < 0 && errno != EINTR && errno != EAGAIN)
 
360
                    {
 
361
                      fprintf(stderr, "read error on control pipe from AVaRICE: %s\n",
 
362
                              strerror(errno));
 
363
                      exit(1);
 
364
                    }
 
365
                }
 
366
            }
 
367
        }
 
368
 
 
369
      if (clear_eps)
 
370
        {
 
371
          usb_resetep(udev, JTAGICE_BULK_EP_READ);
 
372
          usb_resetep(udev, JTAGICE_BULK_EP_WRITE);
 
373
        }
 
374
 
 
375
      if (!exiting && (do_read || polling))
 
376
        {
 
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)
 
380
            {
 
381
              /* OK, try again */
 
382
            }
 
383
          else if (rv < 0)
 
384
            {
 
385
              if (!exiting)
 
386
                fprintf(stderr, "USB bulk read error: %s\n",
 
387
                        usb_strerror());
 
388
              exit(1);
 
389
            }
 
390
          else
 
391
            {
 
392
              /*
 
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
 
396
               * expect more.
 
397
               */
 
398
              polling = false;
 
399
              if (write(fd, buf, rv) != rv)
 
400
                {
 
401
                  fprintf(stderr, "short write to AVaRICE: %s\n",
 
402
                          strerror(errno));
 
403
                  exit(1);
 
404
                }
 
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;
 
411
              pkt_len -= rv;
 
412
              /* OK, if there is more to read, do so. */
 
413
              while (!exiting && pkt_len > 0)
 
414
                {
 
415
                  rv = usb_bulk_read(udev, JTAGICE_BULK_EP_READ, buf,
 
416
                                     pkt_len > JTAGICE_MAX_XFER? JTAGICE_MAX_XFER: pkt_len,
 
417
                                     100);
 
418
 
 
419
                  /*
 
420
                   * Zero-length reads are not expected here,
 
421
                   * as we carefully examined the packet
 
422
                   * length from the header.
 
423
                   */
 
424
                  if (rv == -EINTR || rv == -EAGAIN || rv == -ETIMEDOUT)
 
425
                    {
 
426
                      continue;
 
427
                    }
 
428
                  if (rv <= 0)
 
429
                    {
 
430
                      if (!exiting)
 
431
                        fprintf(stderr,
 
432
                                "USB bulk read error in continuation block: %s\n",
 
433
                                usb_strerror());
 
434
                      exit(1);
 
435
                    }
 
436
                  if (write(fd, buf, rv) != rv)
 
437
                    {
 
438
                      fprintf(stderr, "short write to AVaRICE: %s\n",
 
439
                              strerror(errno));
 
440
                      exit(1);
 
441
                    }
 
442
                  pkt_len -= rv;
 
443
                }
 
444
            }
 
445
        }
 
446
    }
 
447
}
 
448
 
 
449
pid_t jtag::openUSB(const char *jtagDeviceName)
 
450
{
 
451
  int usb_interface;
313
452
  pid_t p;
314
 
  int pype[2];
 
453
  int pype[2], cpipe[2];
 
454
  usb_dev_handle *udev;
 
455
 
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");
317
460
 
 
461
  signal(SIGCHLD, childhandler);
 
462
  signal(SIGUSR1, usr1handler);
318
463
  switch ((p = fork()))
319
464
    {
320
465
    case 0:
321
 
      close(pype[0]);
322
 
      usb_daemon(udev, pype[1], usb_interface);
 
466
      signal(SIGCHLD, SIG_DFL);
 
467
      signal(SIGUSR1, SIG_DFL);
 
468
      close(pype[1]);
 
469
      close(cpipe[1]);
 
470
 
 
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
 
474
 
 
475
      usb_daemon(udev, pype[0], cpipe[0], usb_interface);
 
476
 
 
477
      (void)usb_release_interface(udev, usb_interface);
 
478
      usb_close(udev);
 
479
      exit(0);
323
480
      break;
 
481
 
324
482
    case -1:
325
 
      unixCheck(0, "Cannot fork");
 
483
      unixCheck(-1, "Cannot fork");
326
484
      break;
 
485
 
327
486
    default:
328
 
      close(pype[1]);
329
 
      jtagBox = pype[0];
 
487
      close(pype[0]);
 
488
      close(cpipe[0]);
 
489
      jtagBox = pype[1];
 
490
      ctrlPipe = cpipe[1];
330
491
      usb_kid = p;
331
492
    }
332
493
  atexit(kill_daemon);
333
494
  signal(SIGTERM, inthandler);
334
495
  signal(SIGINT, inthandler);
335
496
  signal(SIGQUIT, inthandler);
336
 
  signal(SIGCHLD, childhandler);
 
497
 
 
498
  while (!ready)
 
499
    /* wait for child to become ready */ ;
 
500
  signal(SIGUSR1, SIG_DFL);
337
501
}
338
502
 
339
503
#endif /* HAVE_LIBUSB */