Re: Trouble loading firmware to USB device

From: Farhan Khan <farhan_at_farhan.codes>
Date: Tue, 26 Apr 2022 17:24:24 UTC
I mistakenly emailed Hans directly rather than copying the email list. Sorry, 
switched email clients :/ Keeping the full exchange below for one email round.

On Tuesday, April 26, 2022 3:01:44 AM EDT you wrote:
> On 4/26/22 07:42, Farhan Khan wrote:
> > On Monday, April 25, 2022 4:21:04 AM EDT you wrote:
> >> Hi,
> >> 
> >>>   status 0xeb023
> >>>   <OPEN|TRANSFERRING|STARTED|SHORT_XFER_OK|BDMA_ENABLE|BDMA_SETUP|CURR_D
> >>>   MA
> >>>   _SET|CAN_CANCEL_IMMED|DOING_CALLBACK|0>>
> >>> 
> >>> 12:25:07.785438 usbus3.3
> >>> SUBM-INTR-EP=00000083,SPD=HIGH,NFR=1,SLEN=0,IVAL=1
> >>> 
> >>>   frame[0] READ 1024 bytes
> >>>   flags 0xa <SHORT_XFER_OK|PIPE_BOF|0>
> >> 
> >> You are asking for 1024 bytes. Try asking for only 64 bytes instead,
> >> same as one wMaxPacket, before submitting the job.
> >> 
> >> --HPS
> > 
> > I believe my bufsize was already set to 64 (0x40). However, I do not see
> > where I am reading or writing to the device except when loading/unloading
> > the firmware. I just gave my code a read-through and do not see a
> > reference to 1024.
> > 
> > Regarding the 64-byte buffer, I went through OpenBSD's code base and when
> > they open pipes (I believe the same as usbd_transfer_setup(9)), they
> > allocate a 64- byte buffer and associate it with the Rx Interrupt pipe,
> > as follows:
> > 
> > ---START OPENBSD CODE---
> > 
> >      isize = UGETW(ed->wMaxPacketSize);
> >      if (isize == 0) {
> >      
> >          printf("%s: invalid Rx intr pipe descriptor\n",
> >          
> >              usc->usb_dev.dv_xname);
> >          
> >          goto fail;
> >      
> >      }
> >      usc->ibuf = malloc(isize, M_USBDEV, M_NOWAIT);
> >      if (usc->ibuf == NULL) {
> >      
> >          printf("%s: could not allocate Rx intr buffer\n",
> >          
> >              usc->usb_dev.dv_xname);
> >          
> >          goto fail;
> >      
> >      }
> >      usc->ibuflen = isize;
> >      error = usbd_open_pipe_intr(usc->sc_iface, AR_PIPE_RX_INTR,
> >      
> >          USBD_SHORT_XFER_OK, &usc->rx_intr_pipe, usc, usc->ibuf, isize,
> >          athn_usb_intr, USBD_DEFAULT_INTERVAL);
> > 
> > ---END OPENBSD CODE---
> > 
> > It appears to me like it is reading the size of the Max Packet size,
> > allocating that many bytes and setting that as the equivalent of bufsize.
> 
> Hi,
> 
> > So my question is, where am I reading 1024 bytes, such that I need to
> > change it to 64?
> 
> The "usbdump" says that you have setup a receive buffer of 1024 bytes.
> 
> Try adding some prints here:
> > 		printf("====USB_ST_SETUP       athn_usb_intr\n");
> > 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
> 
> Try to print usbd_xfer_max_len(xfer) .

printf("---------The length is %d\n", usbd_xfer_max_len(xfer));
This is now giving me a size of 64.
My usb_config bufsize is set to 64 (0x40).

> 
> Also try to "make clean cleandepend" your module.
I added this to my build script, which previously just ran `make clean`.

> 
> And verify using usbdump again.

This looks like what we want, I believe.
-----
     Endpoint 2
        bLength = 0x0007 
        bDescriptorType = 0x0005 
        bEndpointAddress = 0x0083  <IN>
        bmAttributes = 0x0003  <INTERRUPT>
        wMaxPacketSize = 0x0040 
        bInterval = 0x0001 
        bRefresh = 0x0000 
        bSynchAddress = 0x0000 

     Endpoint 3
        bLength = 0x0007 
        bDescriptorType = 0x0005 
        bEndpointAddress = 0x0004  <OUT>
        bmAttributes = 0x0003  <INTERRUPT>
        wMaxPacketSize = 0x0040 
        bInterval = 0x0001 
        bRefresh = 0x0000 
        bSynchAddress = 0x0000 

-----

> 
> Where is "" defined. I cannot find it!
I was following how rtwn did it because this driver has a potential PCI 
component, not a single USB stand alone driver. That value is defined in /usr/
src/sys/dev/athn/athnvar.h

```
enum {
	ATHN_TX_DATA,
	ATHN_RX_DATA,
	ATHN_RX_INTR,
	ATHN_TX_INTR,
	ATHN_N_TRANSFERS, // A semi-copy from RTWN_N_TRANSFER
};
```

Github link: https://github.com/khanzf/freebsd/blob/ar9271/sys/dev/athn/
athnvar.h#L25

> 
> --HPS

- Farhan