libftdi Archives

Subject: (Nearly) Endless loops of USBDEVFS_REAPURBNDELAY

From: Uwe Bonnes <bon@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx>
To: libftdi@xxxxxxxxxxxxxxxxxxxxxxx
Date: Mon, 20 Apr 2009 18:14:48 +0200
Hello,

appended code reads out the JTAG Ids of devices connected to a FT2232 in
JTAG/MPSSE manner. On some runs, it returns nearly immediate, on other
runs, there is a 5 second delay. With strace, this is the start and the end
of 5 second loop:

    268 18746 1240243231.424154 ioctl(3, USBDEVFS_IOCTL, 0x7fffab4a3320) = -1 
ENODATA (No data available)
    269 18746 1240243231.424308 ioctl(3, USBDEVFS_SETCONFIGURATION, 
0x7fffab4a3334) = -1 EBUSY (Device or resource busy)
    270 18746 1240243231.425115 ioctl(3, USBDEVFS_CLAIMINTERFACE, 
0x7fffab4a332c) = 0
    271 18746 1240243231.425266 ioctl(3, USBDEVFS_CONTROL, 0x7fffab4a32f0) = 0
    272 18746 1240243231.425722 ioctl(3, USBDEVFS_CONTROL, 0x7fffab4a32c0) = 0
    273 18746 1240243231.426016 ioctl(3, USBDEVFS_CONTROL, 0x7fffab4a3470) = 0
    274 18746 1240243231.426266 ioctl(3, USBDEVFS_CONTROL, 0x7fffab4a3470) = 0
    275 18746 1240243231.426575 ioctl(3, USBDEVFS_CONTROL, 0x7fffab4a3460) = 0
    276 18746 1240243231.426761 ioctl(3, USBDEVFS_SUBMITURB, 0x7fffab4a33d0) = 0
    277 18746 1240243231.426844 ioctl(3, USBDEVFS_REAPURBNDELAY, 
0x7fffab4a3448) = -1 EAGAIN (Resource temporarily unavailable)
    278 18746 1240243231.426954 select(4, NULL, [3], NULL, {0, 1000}) = 1 (out 
[3], left {0, 1000})
    279 18746 1240243231.427083 ioctl(3, USBDEVFS_REAPURBNDELAY, 
0x7fffab4a3448) = 0
    280 18746 1240243231.427156 ioctl(3, USBDEVFS_SUBMITURB, 0x7fffab4a33d0) = 0
    281 18746 1240243231.427230 ioctl(3, USBDEVFS_REAPURBNDELAY, 
0x7fffab4a3448) = -1 EAGAIN (Resource temporarily unavailable)
    282 18746 1240243231.427314 select(4, NULL, [3], NULL, {0, 1000}) = 1 (out 
[3], left {0, 1000})
    283 18746 1240243231.427412 ioctl(3, USBDEVFS_REAPURBNDELAY, 
0x7fffab4a3448) = 0
    284 18746 1240243231.427483 ioctl(3, USBDEVFS_SUBMITURB, 0x7fffab4a33d0) = 0
    285 18746 1240243231.427556 ioctl(3, USBDEVFS_REAPURBNDELAY, 
0x7fffab4a3448) = -1 EAGAIN (Resource temporarily unavailable)
    286 18746 1240243231.427629 select(4, NULL, [3], NULL, {0, 1000}) = 1 (out 
[3], left {0, 1000})
    287 18746 1240243231.427756 ioctl(3, USBDEVFS_REAPURBNDELAY, 
0x7fffab4a3448) = 0
    288 18746 1240243231.427827 ioctl(3, USBDEVFS_SUBMITURB, 0x7fffab4a33d0) = 0
    289 18746 1240243231.427900 ioctl(3, USBDEVFS_REAPURBNDELAY, 
0x7fffab4a3448) = -1 EAGAIN (Resource temporarily unavailable)
    290 18746 1240243231.427985 select(4, NULL, [3], NULL, {0, 1000}) = 1 (out 
[3], left {0, 1000})
    291 18746 1240243231.428079 ioctl(3, USBDEVFS_REAPURBNDELAY, 
0x7fffab4a3448) = 0
    292 18746 1240243231.428154 ioctl(3, USBDEVFS_SUBMITURB, 0x7fffab4a33c0) = 0
    293 18746 1240243231.428238 ioctl(3, USBDEVFS_REAPURBNDELAY, 
0x7fffab4a3438) = -1 EAGAIN (Resource temporarily unavailable)
    294 18746 1240243231.428311 select(4, NULL, [3], NULL, {0, 1000}) = 0 
(Timeout)
    295 18746 1240243231.431013 ioctl(3, USBDEVFS_REAPURBNDELAY, 
0x7fffab4a3438) = -1 EAGAIN (Resource temporarily unavailable)
    296 18746 1240243231.431088 select(4, NULL, [], NULL, {0, 1000}) = 0 
(Timeout)
    297 18746 1240243231.434930 ioctl(3, USBDEVFS_REAPURBNDELAY, 
0x7fffab4a3438) = -1 EAGAIN (Resource temporarily unavailable)
...
   2781 18746 1240243236.430945 ioctl(3, USBDEVFS_REAPURBNDELAY, 
0x7fffab4a3438) = -1 EAGAIN (Resource temporarily unavailable)
   2782 18746 1240243236.431017 ioctl(3, USBDEVFS_DISCARDURB, 0x7fffab4a33c0) = 0
   2783 18746 1240243236.431178 ioctl(3, USBDEVFS_REAPURB, 0x7fffab4a3438) = 0
   2784 18746 1240243236.431259 ioctl(3, USBDEVFS_SUBMITURB, 0x7fffab4a33c0) = 0
   2785 18746 1240243236.431343 ioctl(3, USBDEVFS_REAPURBNDELAY, 
0x7fffab4a3438) = 0
   2786 18746 1240243236.431474 ioctl(3, USBDEVFS_SUBMITURB, 0x7fffab4a33c0) = 0
   2787 18746 1240243236.431549 ioctl(3, USBDEVFS_REAPURBNDELAY, 
0x7fffab4a3438) = -1 EAGAIN (Resource temporarily unavailable)
   2788 18746 1240243236.431633 select(4, NULL, [3], NULL, {0, 1000}) = 1 (out 
[3], left {0, 1000})
   2789 18746 1240243236.431732 ioctl(3, USBDEVFS_REAPURBNDELAY, 
0x7fffab4a3438) = 0
   2790 18746 1240243236.431844 fstat(1, {st_mode=S_IFCHR|0620, 
st_rdev=makedev(136, 4), ...}) = 0
   2791 18746 1240243236.431966 mmap(NULL, 4096, PROT_READ|PROT_WRITE, 
MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fe7a34a0000
   2792 18746 1240243236.432074 write(1, "0xacb04049\n", 11) = 11
   2793 18746 1240243236.432536 ioctl(3, USBDEVFS_SUBMITURB, 0x7fffab4a33d0) = 0
   2794 18746 1240243236.432696 ioctl(3, USBDEVFS_REAPURBNDELAY, 
0x7fffab4a3448) = 0

 
USBDEVFS_REAPURBNDELAY is mentioned in libftdi in relation to asyncronous
calls. Are async calls active here?

There are other peoplen mentioning this kind of loop on the net, but
I didn't find a sensible answer. Has anybody here on the list some ideas?

> uname -a
Linux hertz 2.6.27.21-0.1-default #1 SMP 2009-03-31 14:50:44 +0200 x86_64 
x86_64 x86_64 GNU/Linux
> ldd test 
        linux-vdso.so.1 =>  (0x00007fff34bfe000)
        libftdi.so.1 => /usr/lib64/libftdi.so.1 (0x00007f9b2c691000)
        libusb-0.1.so.4 => /usr/lib64/libusb-0.1.so.4 (0x00007f9b2c488000)
        libc.so.6 => /lib64/libc.so.6 (0x00007f9b2c12f000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f9b2c897000)
> rpm -qfi /usr/lib64/libusb-0.1.so.4
Source RPM: libusb-0.1.12-136.11.src.rpm

Feel free to add the code to the samples directory.

Bye

-- 
Uwe Bonnes                bon@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Institut fuer Kernphysik  Schlossgartenstrasse 9  64289 Darmstadt
--------- Tel. 06151 162516 -------- Fax. 06151 164321 ----------
#include <ftdi.h>
#include <usb.h>
#include <stdio.h>

#define VENDOR 0x0403
#define PRODUCT 0x6010

/* Sampe libftdi code to read out the JTAG IDs of devices connected 
to the MPSSE (JTAG) pins of a FD2232
Compile like
gcc -g -o test main.c -lftdi -l usb
*/
int main(void)
{
  struct ftdi_context ftdi;
  unsigned long id;
  int read=0, to_read, last_read, retries = 0;
  unsigned char buf[256] = { SET_BITS_LOW, 0x08, 0x0b,
                                 TCK_DIVISOR,  0x01, 0x00 ,
                                 SET_BITS_HIGH, ~0x04, 0x04};
  ftdi_init(&ftdi);
  if (ftdi_usb_open_desc(&ftdi, VENDOR, PRODUCT, 0, 0) < 0)
    {
      printf("Can't open device\n");
      return 1;
    }
  ftdi_usb_reset(&ftdi);
  ftdi_set_interface(&ftdi, INTERFACE_A);
  ftdi_set_latency_timer(&ftdi, 1);
  ftdi_set_bitmode(&ftdi, 0xfb, BITMODE_MPSSE);
  buf[9] = SEND_IMMEDIATE;
  if (ftdi_write_data(&ftdi,buf, 10) != 10)
    printf("Write failed\n");
  /* Send 5 Clocks with TMS = 1 to reset the chain*/
  buf[0]= MPSSE_WRITE_TMS|MPSSE_LSB|MPSSE_BITMODE|MPSSE_WRITE_NEG;
  buf[1]= 5;
  buf[2]= 0x9f;
  if (ftdi_write_data(&ftdi,buf, 3) != 3)
    printf("Write1 failed\n");
  /* Send 4 Clocks with TMS = 0 1 0 0 to reach SHIFTDR*/
  buf[1]= 4;
  buf[2]= 0x82;
  if (ftdi_write_data(&ftdi,buf, 3) != 3)
    printf("Write1 failed\n");
  /* In SHIFTDR, clock 32 times to get the ID (last device first)*/
  do
    {
      buf[0] = MPSSE_DO_READ|MPSSE_DO_WRITE|MPSSE_LSB|MPSSE_WRITE_NEG;
      buf[1] = 3;
      buf[2] = 0;
      buf[3] = 0;
      buf[4] = 0;
      buf[5] = 0;
      buf[6] = 0;
      if (ftdi_write_data(&ftdi,buf, 7) != 7)
        printf("Write loop failed\n");
      to_read = 4;
      read = 0;
      last_read = ftdi_read_data(&ftdi, buf, to_read );
      if (last_read > 0)
        read += last_read;
      while (read <to_read)
        {
          retries++;
          last_read = ftdi_read_data(&ftdi, buf+read, to_read -read);
          if (last_read > 0)
            read += last_read;
        }
      id = 
        (unsigned long) buf[3] <<24 |
        (unsigned long) buf[2] <<16 |
        (unsigned long) buf[1] << 8 |
        (unsigned long) buf[0];
      if (id)
        printf("0x%08lx\n", id);
    } while( id !=0 && id!=0xffffffff);

  ftdi_usb_reset(&ftdi);
  ftdi_usb_close(&ftdi);
  ftdi_deinit(&ftdi);
  return 0;
}  
  
     

--
libftdi - see http://www.intra2net.com/en/developer/libftdi for details.
To unsubscribe send a mail to libftdi+unsubscribe@xxxxxxxxxxxxxxxxxxxxxxx   

Current Thread