1 // SPDX-License-Identifier: GPL-2.0+
3 * hid.c -- HID Composite driver
7 * Copyright (C) 2010 Fabien Chouteau <fabien.chouteau@barco.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
16 #include <linux/kernel.h>
17 #include <linux/platform_device.h>
18 #include <linux/list.h>
19 #include <linux/module.h>
20 #include <linux/usb/composite.h>
21 #include <linux/usb/g_hid.h>
23 #define DRIVER_DESC "HID Gadget"
24 #define DRIVER_VERSION "2010/03/16"
28 /*-------------------------------------------------------------------------*/
30 #define HIDG_VENDOR_NUM 0x0525 /* XXX NetChip */
31 #define HIDG_PRODUCT_NUM 0xa4ac /* Linux-USB HID gadget */
33 /*-------------------------------------------------------------------------*/
35 struct hidg_func_node
{
36 struct usb_function_instance
*fi
;
37 struct usb_function
*f
;
38 struct list_head node
;
39 struct hidg_func_descriptor
*func
;
42 static LIST_HEAD(hidg_func_list
);
44 /*-------------------------------------------------------------------------*/
45 USB_GADGET_COMPOSITE_OPTIONS();
47 static struct usb_device_descriptor device_desc
= {
48 .bLength
= sizeof device_desc
,
49 .bDescriptorType
= USB_DT_DEVICE
,
51 /* .bcdUSB = DYNAMIC */
53 /* .bDeviceClass = USB_CLASS_COMM, */
54 /* .bDeviceSubClass = 0, */
55 /* .bDeviceProtocol = 0, */
56 .bDeviceClass
= USB_CLASS_PER_INTERFACE
,
59 /* .bMaxPacketSize0 = f(hardware) */
61 /* Vendor and product id can be overridden by module parameters. */
62 .idVendor
= cpu_to_le16(HIDG_VENDOR_NUM
),
63 .idProduct
= cpu_to_le16(HIDG_PRODUCT_NUM
),
64 /* .bcdDevice = f(hardware) */
65 /* .iManufacturer = DYNAMIC */
66 /* .iProduct = DYNAMIC */
67 /* NO SERIAL NUMBER */
68 .bNumConfigurations
= 1,
71 static const struct usb_descriptor_header
*otg_desc
[2];
73 /* string IDs are assigned dynamically */
74 static struct usb_string strings_dev
[] = {
75 [USB_GADGET_MANUFACTURER_IDX
].s
= "",
76 [USB_GADGET_PRODUCT_IDX
].s
= DRIVER_DESC
,
77 [USB_GADGET_SERIAL_IDX
].s
= "",
81 static struct usb_gadget_strings stringtab_dev
= {
82 .language
= 0x0409, /* en-us */
83 .strings
= strings_dev
,
86 static struct usb_gadget_strings
*dev_strings
[] = {
93 /****************************** Configurations ******************************/
95 static int do_config(struct usb_configuration
*c
)
97 struct hidg_func_node
*e
, *n
;
100 if (gadget_is_otg(c
->cdev
->gadget
)) {
101 c
->descriptors
= otg_desc
;
102 c
->bmAttributes
|= USB_CONFIG_ATT_WAKEUP
;
105 list_for_each_entry(e
, &hidg_func_list
, node
) {
106 e
->f
= usb_get_function(e
->fi
);
109 status
= usb_add_function(c
, e
->f
);
111 usb_put_function(e
->f
);
118 list_for_each_entry(n
, &hidg_func_list
, node
) {
121 usb_remove_function(c
, n
->f
);
122 usb_put_function(n
->f
);
127 static struct usb_configuration config_driver
= {
128 .label
= "HID Gadget",
129 .bConfigurationValue
= 1,
130 /* .iConfiguration = DYNAMIC */
131 .bmAttributes
= USB_CONFIG_ATT_SELFPOWER
,
134 /****************************** Gadget Bind ******************************/
136 static int hid_bind(struct usb_composite_dev
*cdev
)
138 struct usb_gadget
*gadget
= cdev
->gadget
;
139 struct list_head
*tmp
;
140 struct hidg_func_node
*n
, *m
;
141 struct f_hid_opts
*hid_opts
;
142 int status
, funcs
= 0;
144 list_for_each(tmp
, &hidg_func_list
)
150 list_for_each_entry(n
, &hidg_func_list
, node
) {
151 n
->fi
= usb_get_function_instance("hid");
153 status
= PTR_ERR(n
->fi
);
156 hid_opts
= container_of(n
->fi
, struct f_hid_opts
, func_inst
);
157 hid_opts
->subclass
= n
->func
->subclass
;
158 hid_opts
->protocol
= n
->func
->protocol
;
159 hid_opts
->report_length
= n
->func
->report_length
;
160 hid_opts
->report_desc_length
= n
->func
->report_desc_length
;
161 hid_opts
->report_desc
= n
->func
->report_desc
;
165 /* Allocate string descriptor numbers ... note that string
166 * contents can be overridden by the composite_dev glue.
169 status
= usb_string_ids_tab(cdev
, strings_dev
);
172 device_desc
.iManufacturer
= strings_dev
[USB_GADGET_MANUFACTURER_IDX
].id
;
173 device_desc
.iProduct
= strings_dev
[USB_GADGET_PRODUCT_IDX
].id
;
175 if (gadget_is_otg(gadget
) && !otg_desc
[0]) {
176 struct usb_descriptor_header
*usb_desc
;
178 usb_desc
= usb_otg_descriptor_alloc(gadget
);
181 usb_otg_descriptor_init(gadget
, usb_desc
);
182 otg_desc
[0] = usb_desc
;
186 /* register our configuration */
187 status
= usb_add_config(cdev
, &config_driver
, do_config
);
191 usb_composite_overwrite_options(cdev
, &coverwrite
);
192 dev_info(&gadget
->dev
, DRIVER_DESC
", version: " DRIVER_VERSION
"\n");
200 list_for_each_entry(m
, &hidg_func_list
, node
) {
203 usb_put_function_instance(m
->fi
);
208 static int hid_unbind(struct usb_composite_dev
*cdev
)
210 struct hidg_func_node
*n
;
212 list_for_each_entry(n
, &hidg_func_list
, node
) {
213 usb_put_function(n
->f
);
214 usb_put_function_instance(n
->fi
);
223 static int hidg_plat_driver_probe(struct platform_device
*pdev
)
225 struct hidg_func_descriptor
*func
= dev_get_platdata(&pdev
->dev
);
226 struct hidg_func_node
*entry
;
229 dev_err(&pdev
->dev
, "Platform data missing\n");
233 entry
= kzalloc(sizeof(*entry
), GFP_KERNEL
);
238 list_add_tail(&entry
->node
, &hidg_func_list
);
243 static int hidg_plat_driver_remove(struct platform_device
*pdev
)
245 struct hidg_func_node
*e
, *n
;
247 list_for_each_entry_safe(e
, n
, &hidg_func_list
, node
) {
256 /****************************** Some noise ******************************/
259 static struct usb_composite_driver hidg_driver
= {
262 .strings
= dev_strings
,
263 .max_speed
= USB_SPEED_HIGH
,
265 .unbind
= hid_unbind
,
268 static struct platform_driver hidg_plat_driver
= {
269 .remove
= hidg_plat_driver_remove
,
276 MODULE_DESCRIPTION(DRIVER_DESC
);
277 MODULE_AUTHOR("Fabien Chouteau, Peter Korsgaard");
278 MODULE_LICENSE("GPL");
280 static int __init
hidg_init(void)
284 status
= platform_driver_probe(&hidg_plat_driver
,
285 hidg_plat_driver_probe
);
289 status
= usb_composite_probe(&hidg_driver
);
291 platform_driver_unregister(&hidg_plat_driver
);
295 module_init(hidg_init
);
297 static void __exit
hidg_cleanup(void)
299 usb_composite_unregister(&hidg_driver
);
300 platform_driver_unregister(&hidg_plat_driver
);
302 module_exit(hidg_cleanup
);