USB2.0 mass storage device

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
ehenkes
Member
Member
Posts: 124
Joined: Mon Mar 23, 2009 3:15 am
Location: Germany
Contact:

USB2.0 mass storage device

Post by ehenkes »

EHCI and USB 2.0 works. Based on control transfers, the OS can fetch descriptors and parse them. As next step I want to read sectors out of an attached USB stick. As far as I have seen, the usb stick belongs to the interface subclass "SCSI". Can you give me some advice how to implement the necessary SCSI support. How is this done as good practice?
TomT
Member
Member
Posts: 42
Joined: Sat Mar 15, 2008 7:20 am
Location: Wisconsin, USA
Contact:

Re: USB2.0 mass storage device

Post by TomT »

Here is my CBW CommandBlockWrapper for the SCSI Inquiry request.
take a look at tatOS/usb/inquiry.s and also other files in that directory.

See the /docs directory for a list of resources. I found these documents useful:
[6] "Working Draft American National Standard SCSI Block Commands (SBC-2)"
Nov 2004 and the "SCSI Primary Commands (SPC-2)"

Good luck,
TomT
http://code.google.com/p/tatos/


;Command Block Wrapper for SCSI Inquiry (31 bytes)
InquiryRequest:
dd 0x43425355 ;dCBWSignature
dd 0xaabbccdd ;dCBWTag (device will copy this into CSW)
dd 0x24 ;dCBWDataTransferLength (for tdData)
db 0x80 ;bmCBWFlags 0x80=Device2Host, 00=Host2Device
db 0 ;bCBWLun
db 6 ;bCBWCBLength (of CBWCB)
;CBWCB (16 bytes) see SCSI Inquiry Command
db 0x12 ;SCSI operation code
db 0 ;SCSI reserved
db 0 ;SCSI page or operation code
db 0 ;SCSI reserved
db 0x24 ;SCSI allocation length
db 0 ;SCSI control
times 15 db 0 ;USBmass CBWCB must be 16 bytes long (we add alittle extra 0)
User avatar
ehenkes
Member
Member
Posts: 124
Joined: Mon Mar 23, 2009 3:15 am
Location: Germany
Contact:

Re: USB2.0 mass storage device

Post by ehenkes »

@TomT:
thank you very much for this link to tatOS, great work! what ist the reason for this order of the commands in your OS? Where can I find the original spec for this procedure?

Code: Select all

	STDCALL mpdstr16,putmessage
	call SetAddress

	STDCALL mpdstr17,putmessage
	call SetConfiguration



	;now we start on the SCSI commands
	;the order of the commands and redundancy is important
	;we init the flash toggles here and then let prepareTDchain touch them only
	mov dword  [bulktogglein] ,0
	mov dword  [bulktoggleout],0

	STDCALL mpdstr18,putmessage
	call Inquiry

	STDCALL mpdstr19,putmessage
	call TestUnitReady

	STDCALL mpdstr20,putmessage
	call RequestSense

	STDCALL mpdstr19,putmessage
	call TestUnitReady

	STDCALL mpdstr20,putmessage
	call RequestSense

	STDCALL mpdstr21,putmessage
	call ReadCapacity

	;done-ready for read10/write10 
	STDCALL mpdstr22,putmessage
	mov dword [FLASHDRIVEREADY],1
@all:
Is there no Hobby-OS with USB2/SCSI for Mass Storage Devices (MSD) written in C? We have some code working, but with some errors, which we do not understand regarding the root causes. If somebody is interested in this area I invite him/her to help us. Control Transfers work great, it is the area of bulk transfers causing some trouble and instability.

Source Code: http://prettyos.svn.sourceforge.net/vie ... z?view=tar (comments English) please look at ehci.c and usb2.c
chat: irc.euirc.net #PrettyOS (we speak English)
forum: http://www.c-plusplus.de/forum/viewforu ... is-62.html (German)
User avatar
ehenkes
Member
Member
Posts: 124
Joined: Mon Mar 23, 2009 3:15 am
Location: Germany
Contact:

Re: USB2.0 mass storage device

Post by ehenkes »

Currently, the biggest problem is that we can carry out one SCSI command perfectly at a real PC (VMWare already works great) , but as soon as a second SCSI command follows, the asynchronous scheduler does not carry out the qH and qTD connected to the IN endpoint. As said, control transfers and the first SCSI command work great.

http://prettyos.svn.sourceforge.net/vie ... z?view=tar

Does anybody have a clear sequence how to manage the async scheduler?

Problem solved. :D

But I would appreciate to communicate to other people working on this complex area.

EDIT: currently, our EHCI / USB 2.0 / MSD/SCSI driver works well. Mainly handshake and toggle problems during bulk transfer.
rdos
Member
Member
Posts: 3311
Joined: Wed Oct 01, 2008 1:55 pm

Re: USB2.0 mass storage device

Post by rdos »

I have plans to implement this in the "near future". I have working USB-stacks for UHCI and OHCI, and experimental for EHCI, but I found the mass-storage class to lack in documentation (the SCSI part). I also have the FAT-file systems implemented and relatively bug-free under ATA/IDE.

Links to "easy-to-follow" code for how the SCSI part works would be appreciated. The rest seems to be well described in mass storage class in the USB documentation.
eddyb
Member
Member
Posts: 248
Joined: Fri Aug 01, 2008 7:52 am

Re: USB2.0 mass storage device

Post by eddyb »

rdos wrote:I have plans to implement this in the "near future". I have working USB-stacks for UHCI and OHCI, and experimental for EHCI, but I found the mass-storage class to lack in documentation (the SCSI part). I also have the FAT-file systems implemented and relatively bug-free under ATA/IDE.

Links to "easy-to-follow" code for how the SCSI part works would be appreciated. The rest seems to be well described in mass storage class in the USB documentation.
Wikipedia is your friend: http://en.wikipedia.org/wiki/SCSI_command . Have fun :D .
User avatar
ehenkes
Member
Member
Posts: 124
Joined: Mon Mar 23, 2009 3:15 am
Location: Germany
Contact:

Re: USB2.0 mass storage device

Post by ehenkes »

The most important thing is that inside the SCSI command MSB comes first! :)
Try the sequence: inquiry - test unit ready - request sense - test unit ready - request sense - read capacity - read ...
No handshakes (HC does the ACK alone), switch data toggle per endpoint after each packet transfer or use setconfig(1) before each transfer, if you want to start with toggle 0. :wink:

There is a spec for usb MSD: "Universal Serial Bus Mass Storage Class Bulk-Only Transport" Rev. 1.0, September 31, 1999
http://www.usb.org/developers/devclass_ ... ulk_10.pdf

Have fun! :D


Links to "easy-to-follow" code for how the SCSI part works would be appreciated <--- http://prettyos.svn.sourceforge.net/vie ... usb2_msd.c :)

I would appreciate a co-worker or at least an experience exchange on this complex topic.
rdos
Member
Member
Posts: 3311
Joined: Wed Oct 01, 2008 1:55 pm

Re: USB2.0 mass storage device

Post by rdos »

OK, thanks for the info. I will get back to this at some point, but it might take a few months. I'll code it in assembly, so it will probably not be easy-to-read code. Currently, I have USB-code for various serial to USB adapters, as this is what I currently need for our professional payment terminal.
User avatar
ehenkes
Member
Member
Posts: 124
Joined: Mon Mar 23, 2009 3:15 am
Location: Germany
Contact:

Re: USB2.0 mass storage device

Post by ehenkes »

Some people think that "test unit ready" is not necessary. The device has to be "ready". :)

What does that mean, if "Request Sense" shows "Sense data are not SCSI compliant" in the "Valid" information?
Last edited by ehenkes on Mon May 24, 2010 9:11 am, edited 1 time in total.
User avatar
ehenkes
Member
Member
Posts: 124
Joined: Mon Mar 23, 2009 3:15 am
Location: Germany
Contact:

Re: USB2.0 mass storage device

Post by ehenkes »

User avatar
ehenkes
Member
Member
Posts: 124
Joined: Mon Mar 23, 2009 3:15 am
Location: Germany
Contact:

Re: USB2.0 mass storage device

Post by ehenkes »

I can recommend Jan Axelson's book "usb mass storage". Above all, here you can meet the author and other developers in this new forum:
http://www.lvr.com/forum/index.php?topic=14.0 :D
Post Reply