转载 linux usb gadget hid configfs

https://wiki.tizen.org/USB/Linux_USB_Layers/Configfs_Composite_Gadget/Usage_eq._to_g_hid.ko

USB/Linux USB Layers/Configfs Composite Gadget/Usage eq. to g hid.ko

USB‎ | Linux USB Layers/Configfs Composite Gadget

Contents

 [hide

Owner

  • Andrzej Pietrasiewicz <andrzej.p (at) samsung.com> (KERNEL)

Code change log

v1

Guide

This series aims at integrating configfs into hid, the way it has been done for acm, ncm, ecm, eem, ecm subset, rndis, obex, phonet,

mass_storage, FunctionFS, loopback, sourcesink, uac1, uac2 and uvc. The hid function driver needs some data from userspace, which cannot

be known in advance, so it must be passed somehow to the kernel.

So far passing this data has been done in a rather tricky way: another (proablby out-of-tree) kernel module is required to provide

fake platform device(s), for which the g_hid happens to be a platform driver. The platform devices contain in their platform_data

the data to be passed to the hid function, and during platform drivers's probe the data is passed.

With configfs passing of the said data is straightforward: there is a report_desc attribute in hid function's directory, to which the

data is just written.

 

BACKWARD COMPATIBILITY

Please note that the old g_hid.ko is still available and works.

 

USING THE NEW "GADGET"

Please refer to this post:

http://www.spinics.net/lists/linux-usb/msg76388.html

for general information from Sebastian on how to use configfs-based gadgets (*).

With configfs the procedure is as follows, compared to the information mentioned above (*):

instead of mkdir functions/acm.ttyS1 do
mkdir functions/hid.<instance name>
e.g. mkdir functions/hid.usb0.

In the hid.usb0 directory there will be the following attributes:

	protocol	- HID protocol to use
	report_desc	- data to be used in HID reports, except data 	passed with /dev/hidg<X>
	report_length	- HID report length
	subclass	- HID subclass to use

For a keyboard protocol and subclass are 1, report_length is 8, while the report_desc is:

$ hd my_report_desc
00000000  05 01 09 06 a1 01 05 07  19 e0 29 e7 15 00 25 01  |..........)...%.|
00000010  75 01 95 08 81 02 95 01  75 08 81 03 95 05 75 01  |u.......u.....u.|
00000020  05 08 19 01 29 05 91 02  95 01 75 03 91 03 95 06  |....).....u.....|
00000030  75 08 15 00 25 65 05 07  19 00 29 65 81 00 c0     |u...%e....)e...|
0000003f

Such a sequence of bytes can be stored to the attribute with echo:

$ echo -ne \\x05\\x01\\x09\\x06\\xa1.....

Below is a script which creates a hid gadget:

$ modprobe libcomposite
$ mount none cfg -t configfs
$ mkdir cfg/usb_gadget/g1
$ cd cfg/usb_gadget/g1
$ mkdir configs/c.1
$ mkdir functions/hid.usb0
$ echo 1 > functions/hid.usb0/protocol
$ echo 1 > functions/hid.usb0/subclass
$ echo 8 > functions/hid.usb0/report_length
$ cat my_report_desc > functions/hid.usb0/report_desc
$ mkdir strings/0x409
$ mkdir configs/c.1/strings/0x409
$ echo 0xa4ac > idProduct
$ echo 0x0525 > idVendor
$ echo serial > strings/0x409/serialnumber
$ echo manufacturer > strings/0x409/manufacturer
$ echo HID Gadget > strings/0x409/product
$ echo "Conf 1" > configs/c.1/strings/0x409/configuration
$ echo 120 > configs/c.1/MaxPower
$ ln -s functions/hid.usb0 configs/c.1
$ echo 12480000.hsotg > UDC # choose your UDC from /sys/class/udc/*

TESTING THE FUNCTIONS

gadget)

- create the gadget

- connect the gadget to a host, preferably not the one used to control the gadget

- run a program which writes to /dev/hidg<N>, e.g.

a userspace program found in Documentation/usb/gadget_hid.txt:

$ ./hid_gadget_test /dev/hidg0 keyboard

host) - observe the keystrokes from the gadget

Logo

更多推荐