Subject: Re: USB stack needs early review (Re: Someone should fix our USB stack...)
To: ITOH Yasufumi <itohy@netbsd.org>
From: Hans Petter Selasky <hselasky@c2i.net>
List: tech-kern
Date: 03/17/2007 11:44:30
Hi ITOH,

On Saturday 17 March 2007 07:05, ITOH Yasufumi wrote:
> Hello,
>
> Jason Thorpe writes:
> > ...like the FreeBSD guys did:
> >
> > http://lists.freebsd.org/pipermail/cvs-src/2006-May/064310.html
>
> Is anybody working on this?

Yes, these problems have already been solved. The only solution I see is that 
we change the USB API, due to the problems you experience, like sleeping in 
non-sleepable contexts. All memory needs to be pre-allocated in a separate 
context.

>
> I'm doing some work and have troubles.
> Modifying allocm/freem with bus_dma(9) functions (FreeBSD directly
> replaced them but I decided to separate DMA related code),
> doesn't work since our bus_dma(9) requires thread context.
> Calling bus_dmamap_unmap() causes panic: lockmgr: no context.
>
> I assume allocm() can sleep (always called with context).  It is
> actually called by async functions and they need to be fixed.
>
> I rewrote freem() without context to delay unmap until next
> allocm/freem with context.
> This isn't stable for now....  Is this design reasonable?
> This causes unmap delayed (possibly) forever.  Is it OK?
> I first rewrote usb_transfer_complete() to use usb_add_task(),
> but that didn't work during cold boot and the performance was
> affected.
>
> - -
>
> If you like to use this, please review attached code.
> Currently OHCI only, since my test machine has only OHCIs.
>
> This applies to NetBSD 4.99.13 (before caddr_t removal).
>
> Stated based on FreeBSD version, excluding
>  - removal of portability code
>
> Patch most NetBSD changes, excluding
>  - memory "reserve"
>  - caddr_t removal, expecting it will soon be reverted :D
>  - volatiles in DMA structure, since it should not be needed
>    with proper bus_dma_sync(9)s
>
> Move all DMA related code to usb_mem.[ch]
> (add usb_alloc_buffer_dma(), usb_free_buffer_dma(), etc.).
>
> Allocate DMA buffer per host interface, not globally.
>  advantage:	Buffers can be freed on detaching host interface.
> 		Activity of a host interface does not affect others.
>  disadvantages:	It possibly consumes more memory.
>
> ohci.c
>  - free resources on detach (but some leaks exist)
>  - add lots of bus_dmamap_sync() operations
>  - simplify the code of loading std chain
>
> To do
>  - ehci/uhci/slhci
>  - non-DMA version of usb_mem.c for slhci
>  - files/files.usb changes
>  - make async functions really async (may change APIs)

The latest sources can be gotten here:

svn --username anonsvn --password anonsvn \
      checkout svn://svn.turbocat.net/i4b
#
# The following commands will
# install the driver on FreeBSD:
#
cd i4b/trunk/i4b/FreeBSD.usb
make S=../src package
make install

And it supports NetBSD, FreeBSD, EHCI, OHCI and UHCI.

One of the things left is to write an emulation layer of the old USB API using 
the new USB API.

See the README.TXT file in the root directory for instructions on how to 
install the driver on NetBSD.

--HPS