Author Topic: Send custom commands/data to USB mass-storage device  (Read 16757 times)

nazekimi

  • Member
  • ***
  • Posts: 3
Send custom commands/data to USB mass-storage device
« on: June 12, 2014, 10:38:00 pm »
Hello Jan,

I am starting a project in which I copy data to a mass-storage device and also allow the host
and the device to convey status, version and other info between them.
The device is a Linux gadget device on an iMX processor and the host will be a Win7 PC
running a host application developed in C#

I can see it there are 3 ways to achieve this. Although I still lack the knowledge if all of them
are really possible.(in order of priority)
1. Device is build as only a usb mass-storage gadget. Then somehow use a Windows API to send data to the device.
    This might be the smartest way to do it but hard to implement. It might need a bit more customization/programming
    on both the host-side and device-side. It also seems hard to get information on how to go about this method.
2. Build the device as a composite gadget(serial port + mass-storage) then handle data communication via serial port.
    This method will be easier to program on both sides but the problem is it probably needs some customize usb driver.(or does it?)
    I have no experience in making any Windows drivers and have no idea where to start here.
3. Usb mass-storage only build and implement communication via file reading on both sides.
    May not require any research and probably the easiest, but it is not elegant. The device has to continue on polling
    on the "shared file" for status updates and communication will go slower because of file access.

Any information and opinion regarding method #1 and #2 would be much appreciated!
Or if there is another method I could use to communicate with a usb mass-storage device.

Best Regards,
nazekimi
« Last Edit: June 12, 2014, 10:46:00 pm by nazekimi »

Barry Twycross

  • Frequent Contributor
  • ****
  • Posts: 263
Re: Send custom commands/data to USB mass-storage device
« Reply #1 on: June 13, 2014, 12:34:25 pm »
I wouldn't recommend #3. There are significant tricky issues about arbitrating for the media to allow both ends to access it. Also writing files is significantly difficult, reading them is none to easy. A Mass storage device only needs to know about reading and writing blocks, it doesn't know anything about what files are in those blocks. I have used this method, we use it for the host to update the device, the device has to be able to find a single file and read the contents, not a very difficult thing, but significantly more difficult than just being a block device. We also send files back the host using virtual files. I write directory entries for blocks which are beyond the end of the physical media, when requests are made for those blocks I get the needed data from somewhere else.

#2 may be doable. I've not made a composite  MSC/CDC device yet, I've been avoiding that. Our device is either MSC or CDC depending on how the user plugs it in. Do you actually need the MSC portion? Could you do it all via CDC? CDC is about the simplest protocol you can use.

#1 is relatively easy. I've done this by implementing both custom device requests and custom SCSI commands. If you already have a working MSC device (the linux gadget), then this should be pretty trivial to impliment on the device. I've never done the Windows end of this, that may be the trickiest part. Its pretty trivial to access the device from a Mac host.


Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: Send custom commands/data to USB mass-storage device
« Reply #2 on: June 13, 2014, 05:19:09 pm »
Agree with Barry that mass-storage only is easiest, but if you need the device to provide real-time status, you can't use the mass-storage file system without the complications of #3.

CDC needs a vendor-provided INF file, and it will need to be signed for 64-bit Windows.

You could use HID or WinUSB without a vendor-provided INF but will need to provide an application.




nazekimi

  • Member
  • ***
  • Posts: 3
Re: Send custom commands/data to USB mass-storage device
« Reply #3 on: June 14, 2014, 12:04:25 am »
Thank you for the replies.

Yes indeed, I was hoping to really implement #1.
The Linux part will be quite easy for me but I don't have an idea how to do it from the Windows App side.
When you say HID, does that mean that the device doesn't have to be an interface device(mouse, pen, etc) to be able communicate with it?
And for WinUSB, I read somehow that it can act as a filter driver for the device, is this possible?
I really need the Mass-storage function, as the main function of the host is transferring files to the device.
The device will later access these file via its file its file system.
If I use either HID and WinUSB will the still be detected as mass-storage and viewable via explorer?

Best Regards

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: Send custom commands/data to USB mass-storage device
« Reply #4 on: June 14, 2014, 10:33:56 am »
You could do a composite mass-storage + HID or WinUSB device.

The HID would be a "generic" HID. I have example code here:

janaxelson.com/hidpage.htm

WinUSB info here:

http://janaxelson.com/winusb.htm

Tsuneo

  • Frequent Contributor
  • ****
  • Posts: 145
Re: Send custom commands/data to USB mass-storage device
« Reply #5 on: June 15, 2014, 08:14:29 am »
Quote
Yes indeed, I was hoping to really implement #1.
...I don't have an idea how to do it from the Windows App side.

You may apply IOCTL_SCSI_PASS_THROUGH on your Windows application (user mode).
http://msdn.microsoft.com/en-us/library/windows/hardware/ff560519

You’ll find an example of this IOCTL, "SCSI Pass-Through Interface Tool", in Windows Driver Kit (WDK) 8.1 Samples
http://code.msdn.microsoft.com/Windows-8-Driver-Samples-5e1aa62e

Tsuneo

nazekimi

  • Member
  • ***
  • Posts: 3
Re: Send custom commands/data to USB mass-storage device
« Reply #6 on: June 16, 2014, 08:09:11 pm »
You could do a composite mass-storage + HID or WinUSB device.

The HID would be a "generic" HID. I have example code here:

http://janaxelson.com/hidpage.htm

WinUSB info here:

http://janaxelson.com/winusb.htm


You may apply IOCTL_SCSI_PASS_THROUGH on your Windows application (user mode).
http://msdn.microsoft.com/en-us/library/windows/hardware/ff560519

You’ll find an example of this IOCTL, "SCSI Pass-Through Interface Tool", in Windows Driver Kit (WDK) 8.1 Samples
http://code.msdn.microsoft.com/Windows-8-Driver-Samples-5e1aa62e

Tsuneo

Thank you Jan and Tsuneo

I will try your suggestion and see which way would work best.
I will report later on how it work.

Best Regards
nazekimi