xairy / raw-gadget

USB Raw Gadget — a low-level interface for the Linux USB Gadget subsystem
350 stars 37 forks source link

Add an example for emulating a USB printer #17

Closed AristoChen closed 2 years ago

AristoChen commented 2 years ago

Hi,

I tried to combine tests/gadget.c and examples/keyboard.c together to emulate a USB printer, hope that I made it right, let me know if I did anything wrong.

thanks!

xairy commented 2 years ago

Hi! Could you clarify in the comment in the file header how complete printer emulation is? I assume it's not able to do anything meaningful like the keyboard? This is fine, but I would like to see this documented. Thanks!

AristoChen commented 2 years ago

Could you clarify in the comment in the file header how complete printer emulation is?

Is there any prefered format for documenting the emulation? or simply just copy the log genereted like the following to the header of printer.c?

event: control, length: 8
  bRequestType: 0x80 (IN), bRequest: 0x6, wValue: 0x100, wIndex: 0x0, wLength: 64
  type = USB_TYPE_STANDARD
  req = USB_REQ_GET_DESCRIPTOR
  desc = USB_DT_DEVICE
ep0: transferred 18 bytes (in)
event: control, length: 8
  bRequestType: 0x80 (IN), bRequest: 0x6, wValue: 0x100, wIndex: 0x0, wLength: 18
  type = USB_TYPE_STANDARD
  req = USB_REQ_GET_DESCRIPTOR
  desc = USB_DT_DEVICE
ep0: transferred 18 bytes (in)
event: control, length: 8
  bRequestType: 0x80 (IN), bRequest: 0x6, wValue: 0x200, wIndex: 0x0, wLength: 9
  type = USB_TYPE_STANDARD
  req = USB_REQ_GET_DESCRIPTOR
  desc = USB_DT_CONFIG
config->wTotalLength: 32
ep0: transferred 9 bytes (in)
event: control, length: 8
  bRequestType: 0x80 (IN), bRequest: 0x6, wValue: 0x200, wIndex: 0x0, wLength: 32
  type = USB_TYPE_STANDARD
  req = USB_REQ_GET_DESCRIPTOR
  desc = USB_DT_CONFIG
config->wTotalLength: 32
ep0: transferred 32 bytes (in)
event: control, length: 8
  bRequestType: 0x80 (IN), bRequest: 0x6, wValue: 0x300, wIndex: 0x0, wLength: 255
  type = USB_TYPE_STANDARD
  req = USB_REQ_GET_DESCRIPTOR
  desc = USB_DT_STRING
ep0: transferred 4 bytes (in)
event: control, length: 8
  bRequestType: 0x80 (IN), bRequest: 0x6, wValue: 0x301, wIndex: 0x409, wLength: 255
  type = USB_TYPE_STANDARD
  req = USB_REQ_GET_DESCRIPTOR
  desc = USB_DT_STRING
ep0: transferred 4 bytes (in)
event: control, length: 8
  bRequestType: 0x80 (IN), bRequest: 0x6, wValue: 0x302, wIndex: 0x409, wLength: 255
  type = USB_TYPE_STANDARD
  req = USB_REQ_GET_DESCRIPTOR
  desc = USB_DT_STRING
ep0: transferred 4 bytes (in)
event: control, length: 8
  bRequestType: 0x0 (OUT), bRequest: 0x9, wValue: 0x1, wIndex: 0x0, wLength: 0
  type = USB_TYPE_STANDARD
  req = USB_REQ_SET_CONFIGURATION
bulk_out: ep = #1
bulk_in: ep = #0
bulk_out_thread: created
bulk_in_thread: created
ep0: transferred 0 bytes (out)
event: control, length: 8
  bRequestType: 0x80 (IN), bRequest: 0x6, wValue: 0x303, wIndex: 0x409, wLength: 255
  type = USB_TYPE_STANDARD
  req = USB_REQ_GET_DESCRIPTOR
  desc = USB_DT_STRING
ep0: transferred 4 bytes (in)
event: control, length: 8
  bRequestType: 0x80 (IN), bRequest: 0x6, wValue: 0x304, wIndex: 0x409, wLength: 255
  type = USB_TYPE_STANDARD
  req = USB_REQ_GET_DESCRIPTOR
  desc = USB_DT_STRING
ep0: transferred 4 bytes (in)
event: control, length: 8
  bRequestType: 0xa1 (IN), bRequest: 0x0, wValue: 0x0, wIndex: 0x0, wLength: 1023
  type = USB_TYPE_CLASS
  req = GET_DEVICE_ID
ep0: transferred 43 bytes (in)

I assume it's not able to do anything meaningful like the keyboard?

Currently it can be recognized as a printer on my laptop(Ubuntu 20.04, with kernel version 5.11.0-46-generic), so I can read and write with /dev/usb/lp0 if the usblp kernel module is loaded.

xairy commented 2 years ago

Is there any prefered format for documenting the emulation? or simply just copy the log genereted like the following to the header of printer.c?

Not really. No need to be super thorough and technical: this is just an example after all.

Currently it can be recognized as a printer on my laptop(Ubuntu 20.04, with kernel version 5.11.0-46-generic), so I can read and write with /dev/usb/lp0 if the usblp kernel module is loaded.

Just put this into the header comment ^. Something like:

"This example emulates a USB printer. The code handles enumeration and allows a Linux host to recognize a printer device (enabling it to read from and write to /dev/usb/lp0 if the usblp kernel module is loaded)."

The exact wording is up to you.

Thanks!

AristoChen commented 2 years ago

updated both printer.c and README.md, let me know if there is anything else need to be done

thanks!

xairy commented 2 years ago

Awesome, thanks!