HID Gadget on Colibri imx7

I would like to use/create a HID gadget on a Colibri imx7D. I can find a few posts here and there online but it seems to me to be contradictory and/or dated. One post talks about adding the descriptor and an init function to to kernel sources under drivers->usb->hid.c. It seems on a modern kernel, the hid.c is in a directory called legacy which makes me think that maybe that is not the current best practice. Another guide says those two items need to be added to the platform drivers with no real concrete explanation.

Can anyone tell me the current recommended best practice if I want my module to be able to emulate a keyboard over the OTG port? I don’t mind recompiling kernels/changing devices/etc/etc. I would just like some concrete guidance on how to go about it. I keep thinking I am missing something because it should just not be this hard…

Kernel documentation for HID gadget in the 4.9 kernel (kernel used for imx7 2.8 releases):
https://elixir.bootlin.com/linux/v4.9/source/Documentation/usb/gadget_hid.txt

I’ve found that documentation, this is one of the points above that is not clear. The docs say:

g_hid is a platform driver, so to use it you need to add
struct platform_device(s) to your platform code defining the
HID function descriptors you want to use - E.G. something
like:

But that statement assumes the reader knows what “platform code” means or more importantly, where it is. I understand what is meant by “platform code” but where, specifically, is it. I suspect it is …/arch/arm/mach-imx/… but there are a lot of source files there. Where would those snippets go? Does it matter what source file it goes in? Can I for example throw it in devices/paltform-imx-dma.c or devices/devices.c?

You probably want to to use configfs rather then platform device, our bsp is using libusbg(https://github.com/libusbg/libusbg) to configure usb gadget via configfs.

Configuration with configfs

	Instead of adding fake platform devices and drivers in order to pass
	some data to the kernel, if HID is a part of a gadget composed with
	configfs the hidg_func_descriptor.report_desc is passed to the kernel
	by writing the appropriate stream of bytes to a configfs attribute.

Lets play devils advocate, lets assume that I do actually want to do this as a platform device (and this is as much for just understanding than anything else). Would it be sufficient to add the descriptor and init function in one of the source files under arch/arm/mach-imx/…? Are those the ‘platform’ devices and drivers?

There are platform devices created under arch/arm/mach-imx in the old days. I am not sure if that would still work nowadays and is definitely not the recommended way today.

Using configfs from user space is the recommended way to create HID gadgets nowadays.

There is some documentation about configfs on our developer website:
https://developer.toradex.com/knowledge-base/usb-device-mode-(linux)#Linux

The patchset which introduced HID Gadget configfs support has some more information on how to use HID Gadget with configfs:
https://www.spinics.net/lists/linux-usb/msg115328.html