1 // SPDX-License-Identifier: GPL-2.0+
3 * hid.c -- HID Composite driver
7 * Copyright (C) 2010 Fabien Chouteau <fabien.chouteau@barco.com>
11 #include <linux/kernel.h>
12 #include <linux/platform_device.h>
13 #include <linux/list.h>
14 #include <linux/module.h>
15 #include <linux/usb/composite.h>
16 #include <linux/usb/g_hid.h>
18 #define DRIVER_DESC "HID Gadget"
19 #define DRIVER_VERSION "2010/03/16"
23 /*-------------------------------------------------------------------------*/
25 #define HIDG_VENDOR_NUM 0x0525 /* XXX NetChip */
26 #define HIDG_PRODUCT_NUM 0xa4ac /* Linux-USB HID gadget */
28 /*-------------------------------------------------------------------------*/
30 struct hidg_func_node
{
31 struct usb_function_instance
*fi
;
32 struct usb_function
*f
;
33 struct list_head node
;
34 struct hidg_func_descriptor
*func
;
37 static LIST_HEAD(hidg_func_list
);
39 /*-------------------------------------------------------------------------*/
40 USB_GADGET_COMPOSITE_OPTIONS();
42 static struct usb_device_descriptor device_desc
= {
43 .bLength
= sizeof device_desc
,
44 .bDescriptorType
= USB_DT_DEVICE
,
46 /* .bcdUSB = DYNAMIC */
48 /* .bDeviceClass = USB_CLASS_COMM, */
49 /* .bDeviceSubClass = 0, */
50 /* .bDeviceProtocol = 0, */
51 .bDeviceClass
= USB_CLASS_PER_INTERFACE
,
54 /* .bMaxPacketSize0 = f(hardware) */
56 /* Vendor and product id can be overridden by module parameters. */
57 .idVendor
= cpu_to_le16(HIDG_VENDOR_NUM
),
58 .idProduct
= cpu_to_le16(HIDG_PRODUCT_NUM
),
59 /* .bcdDevice = f(hardware) */
60 /* .iManufacturer = DYNAMIC */
61 /* .iProduct = DYNAMIC */
62 /* NO SERIAL NUMBER */
63 .bNumConfigurations
= 1,
66 static const struct usb_descriptor_header
*otg_desc
[2];
68 /* string IDs are assigned dynamically */
69 static struct usb_string strings_dev
[] = {
70 [USB_GADGET_MANUFACTURER_IDX
].s
= "",
71 [USB_GADGET_PRODUCT_IDX
].s
= DRIVER_DESC
,
72 [USB_GADGET_SERIAL_IDX
].s
= "",
76 static struct usb_gadget_strings stringtab_dev
= {
77 .language
= 0x0409, /* en-us */
78 .strings
= strings_dev
,
81 static struct usb_gadget_strings
*dev_strings
[] = {
88 /****************************** Configurations ******************************/
90 static int do_config(struct usb_configuration
*c
)
92 struct hidg_func_node
*e
, *n
;
95 if (gadget_is_otg(c
->cdev
->gadget
)) {
96 c
->descriptors
= otg_desc
;
97 c
->bmAttributes
|= USB_CONFIG_ATT_WAKEUP
;
100 list_for_each_entry(e
, &hidg_func_list
, node
) {
101 e
->f
= usb_get_function(e
->fi
);
104 status
= usb_add_function(c
, e
->f
);
106 usb_put_function(e
->f
);
113 list_for_each_entry(n
, &hidg_func_list
, node
) {
116 usb_remove_function(c
, n
->f
);
117 usb_put_function(n
->f
);
122 static struct usb_configuration config_driver
= {
123 .label
= "HID Gadget",
124 .bConfigurationValue
= 1,
125 /* .iConfiguration = DYNAMIC */
126 .bmAttributes
= USB_CONFIG_ATT_SELFPOWER
,
129 /****************************** Gadget Bind ******************************/
131 static int hid_bind(struct usb_composite_dev
*cdev
)
133 struct usb_gadget
*gadget
= cdev
->gadget
;
134 struct list_head
*tmp
;
135 struct hidg_func_node
*n
, *m
;
136 struct f_hid_opts
*hid_opts
;
137 int status
, funcs
= 0;
139 list_for_each(tmp
, &hidg_func_list
)
145 list_for_each_entry(n
, &hidg_func_list
, node
) {
146 n
->fi
= usb_get_function_instance("hid");
148 status
= PTR_ERR(n
->fi
);
151 hid_opts
= container_of(n
->fi
, struct f_hid_opts
, func_inst
);
152 hid_opts
->subclass
= n
->func
->subclass
;
153 hid_opts
->protocol
= n
->func
->protocol
;
154 hid_opts
->report_length
= n
->func
->report_length
;
155 hid_opts
->report_desc_length
= n
->func
->report_desc_length
;
156 hid_opts
->report_desc
= n
->func
->report_desc
;
160 /* Allocate string descriptor numbers ... note that string
161 * contents can be overridden by the composite_dev glue.
164 status
= usb_string_ids_tab(cdev
, strings_dev
);
167 device_desc
.iManufacturer
= strings_dev
[USB_GADGET_MANUFACTURER_IDX
].id
;
168 device_desc
.iProduct
= strings_dev
[USB_GADGET_PRODUCT_IDX
].id
;
170 if (gadget_is_otg(gadget
) && !otg_desc
[0]) {
171 struct usb_descriptor_header
*usb_desc
;
173 usb_desc
= usb_otg_descriptor_alloc(gadget
);
176 usb_otg_descriptor_init(gadget
, usb_desc
);
177 otg_desc
[0] = usb_desc
;
181 /* register our configuration */
182 status
= usb_add_config(cdev
, &config_driver
, do_config
);
186 usb_composite_overwrite_options(cdev
, &coverwrite
);
187 dev_info(&gadget
->dev
, DRIVER_DESC
", version: " DRIVER_VERSION
"\n");
195 list_for_each_entry(m
, &hidg_func_list
, node
) {
198 usb_put_function_instance(m
->fi
);
203 static int hid_unbind(struct usb_composite_dev
*cdev
)
205 struct hidg_func_node
*n
;
207 list_for_each_entry(n
, &hidg_func_list
, node
) {
208 usb_put_function(n
->f
);
209 usb_put_function_instance(n
->fi
);
218 static int hidg_plat_driver_probe(struct platform_device
*pdev
)
220 struct hidg_func_descriptor
*func
= dev_get_platdata(&pdev
->dev
);
221 struct hidg_func_node
*entry
;
224 dev_err(&pdev
->dev
, "Platform data missing\n");
228 entry
= kzalloc(sizeof(*entry
), GFP_KERNEL
);
233 list_add_tail(&entry
->node
, &hidg_func_list
);
238 static int hidg_plat_driver_remove(struct platform_device
*pdev
)
240 struct hidg_func_node
*e
, *n
;
242 list_for_each_entry_safe(e
, n
, &hidg_func_list
, node
) {
251 /****************************** Some noise ******************************/
254 static struct usb_composite_driver hidg_driver
= {
257 .strings
= dev_strings
,
258 .max_speed
= USB_SPEED_HIGH
,
260 .unbind
= hid_unbind
,
263 static struct platform_driver hidg_plat_driver
= {
264 .remove
= hidg_plat_driver_remove
,
271 MODULE_DESCRIPTION(DRIVER_DESC
);
272 MODULE_AUTHOR("Fabien Chouteau, Peter Korsgaard");
273 MODULE_LICENSE("GPL");
275 static int __init
hidg_init(void)
279 status
= platform_driver_probe(&hidg_plat_driver
,
280 hidg_plat_driver_probe
);
284 status
= usb_composite_probe(&hidg_driver
);
286 platform_driver_unregister(&hidg_plat_driver
);
290 module_init(hidg_init
);
292 static void __exit
hidg_cleanup(void)
294 usb_composite_unregister(&hidg_driver
);
295 platform_driver_unregister(&hidg_plat_driver
);
297 module_exit(hidg_cleanup
);