xref: /openbmc/linux/drivers/usb/gadget/function/f_hid.c (revision 57904291176fa16a981cefca5cbe1a0b50196792)
15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0+
200a2430fSAndrzej Pietrasiewicz /*
300a2430fSAndrzej Pietrasiewicz  * f_hid.c -- USB HID function driver
400a2430fSAndrzej Pietrasiewicz  *
500a2430fSAndrzej Pietrasiewicz  * Copyright (C) 2010 Fabien Chouteau <fabien.chouteau@barco.com>
600a2430fSAndrzej Pietrasiewicz  */
700a2430fSAndrzej Pietrasiewicz 
800a2430fSAndrzej Pietrasiewicz #include <linux/kernel.h>
900a2430fSAndrzej Pietrasiewicz #include <linux/module.h>
1000a2430fSAndrzej Pietrasiewicz #include <linux/hid.h>
11cb382536SAndrzej Pietrasiewicz #include <linux/idr.h>
1200a2430fSAndrzej Pietrasiewicz #include <linux/cdev.h>
1300a2430fSAndrzej Pietrasiewicz #include <linux/mutex.h>
1400a2430fSAndrzej Pietrasiewicz #include <linux/poll.h>
1500a2430fSAndrzej Pietrasiewicz #include <linux/uaccess.h>
1600a2430fSAndrzej Pietrasiewicz #include <linux/wait.h>
1700a2430fSAndrzej Pietrasiewicz #include <linux/sched.h>
1800a2430fSAndrzej Pietrasiewicz #include <linux/usb/g_hid.h>
1900a2430fSAndrzej Pietrasiewicz 
2000a2430fSAndrzej Pietrasiewicz #include "u_f.h"
21cb382536SAndrzej Pietrasiewicz #include "u_hid.h"
22cb382536SAndrzej Pietrasiewicz 
23cb382536SAndrzej Pietrasiewicz #define HIDG_MINORS	4
2400a2430fSAndrzej Pietrasiewicz 
2500a2430fSAndrzej Pietrasiewicz static int major, minors;
2699f2d956SIvan Orlov 
2799f2d956SIvan Orlov static const struct class hidg_class = {
2899f2d956SIvan Orlov 	.name = "hidg",
2999f2d956SIvan Orlov };
3099f2d956SIvan Orlov 
31cb382536SAndrzej Pietrasiewicz static DEFINE_IDA(hidg_ida);
32cb382536SAndrzej Pietrasiewicz static DEFINE_MUTEX(hidg_ida_lock); /* protects access to hidg_ida */
3300a2430fSAndrzej Pietrasiewicz 
3400a2430fSAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/
3500a2430fSAndrzej Pietrasiewicz /*                            HID gadget struct                            */
3600a2430fSAndrzej Pietrasiewicz 
3700a2430fSAndrzej Pietrasiewicz struct f_hidg_req_list {
3800a2430fSAndrzej Pietrasiewicz 	struct usb_request	*req;
3900a2430fSAndrzej Pietrasiewicz 	unsigned int		pos;
4000a2430fSAndrzej Pietrasiewicz 	struct list_head 	list;
4100a2430fSAndrzej Pietrasiewicz };
4200a2430fSAndrzej Pietrasiewicz 
4300a2430fSAndrzej Pietrasiewicz struct f_hidg {
4400a2430fSAndrzej Pietrasiewicz 	/* configuration */
4500a2430fSAndrzej Pietrasiewicz 	unsigned char			bInterfaceSubClass;
4600a2430fSAndrzej Pietrasiewicz 	unsigned char			bInterfaceProtocol;
47b3c4ec71SAbdulhadi Mohamed 	unsigned char			protocol;
48afcff6dcSMaxim Devaev 	unsigned char			idle;
4900a2430fSAndrzej Pietrasiewicz 	unsigned short			report_desc_length;
5000a2430fSAndrzej Pietrasiewicz 	char				*report_desc;
5100a2430fSAndrzej Pietrasiewicz 	unsigned short			report_length;
52d7428bc2SMaxim Devaev 	/*
53d7428bc2SMaxim Devaev 	 * use_out_ep - if true, the OUT Endpoint (interrupt out method)
54d7428bc2SMaxim Devaev 	 *              will be used to receive reports from the host
55d7428bc2SMaxim Devaev 	 *              using functions with the "intout" suffix.
56d7428bc2SMaxim Devaev 	 *              Otherwise, the OUT Endpoint will not be configured
57d7428bc2SMaxim Devaev 	 *              and the SETUP/SET_REPORT method ("ssreport" suffix)
58d7428bc2SMaxim Devaev 	 *              will be used to receive reports.
59d7428bc2SMaxim Devaev 	 */
60d7428bc2SMaxim Devaev 	bool				use_out_ep;
6100a2430fSAndrzej Pietrasiewicz 
6200a2430fSAndrzej Pietrasiewicz 	/* recv report */
6333e4c1a9SKrzysztof Opasiak 	spinlock_t			read_spinlock;
6400a2430fSAndrzej Pietrasiewicz 	wait_queue_head_t		read_queue;
65d7428bc2SMaxim Devaev 	/* recv report - interrupt out only (use_out_ep == 1) */
66d7428bc2SMaxim Devaev 	struct list_head		completed_out_req;
6700a2430fSAndrzej Pietrasiewicz 	unsigned int			qlen;
68d7428bc2SMaxim Devaev 	/* recv report - setup set_report only (use_out_ep == 0) */
69d7428bc2SMaxim Devaev 	char				*set_report_buf;
70d7428bc2SMaxim Devaev 	unsigned int			set_report_length;
7100a2430fSAndrzej Pietrasiewicz 
7200a2430fSAndrzej Pietrasiewicz 	/* send report */
7333e4c1a9SKrzysztof Opasiak 	spinlock_t			write_spinlock;
7400a2430fSAndrzej Pietrasiewicz 	bool				write_pending;
7500a2430fSAndrzej Pietrasiewicz 	wait_queue_head_t		write_queue;
7600a2430fSAndrzej Pietrasiewicz 	struct usb_request		*req;
7700a2430fSAndrzej Pietrasiewicz 
7889ff3dfaSJohn Keeping 	struct device			dev;
7900a2430fSAndrzej Pietrasiewicz 	struct cdev			cdev;
8000a2430fSAndrzej Pietrasiewicz 	struct usb_function		func;
8100a2430fSAndrzej Pietrasiewicz 
8200a2430fSAndrzej Pietrasiewicz 	struct usb_ep			*in_ep;
8300a2430fSAndrzej Pietrasiewicz 	struct usb_ep			*out_ep;
8400a2430fSAndrzej Pietrasiewicz };
8500a2430fSAndrzej Pietrasiewicz 
func_to_hidg(struct usb_function * f)8600a2430fSAndrzej Pietrasiewicz static inline struct f_hidg *func_to_hidg(struct usb_function *f)
8700a2430fSAndrzej Pietrasiewicz {
8800a2430fSAndrzej Pietrasiewicz 	return container_of(f, struct f_hidg, func);
8900a2430fSAndrzej Pietrasiewicz }
9000a2430fSAndrzej Pietrasiewicz 
hidg_release(struct device * dev)9189ff3dfaSJohn Keeping static void hidg_release(struct device *dev)
9289ff3dfaSJohn Keeping {
9389ff3dfaSJohn Keeping 	struct f_hidg *hidg = container_of(dev, struct f_hidg, dev);
9489ff3dfaSJohn Keeping 
95d9828961SKonstantin Aladyshev 	kfree(hidg->report_desc);
9689ff3dfaSJohn Keeping 	kfree(hidg->set_report_buf);
9789ff3dfaSJohn Keeping 	kfree(hidg);
9889ff3dfaSJohn Keeping }
9989ff3dfaSJohn Keeping 
10000a2430fSAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/
10100a2430fSAndrzej Pietrasiewicz /*                           Static descriptors                            */
10200a2430fSAndrzej Pietrasiewicz 
10300a2430fSAndrzej Pietrasiewicz static struct usb_interface_descriptor hidg_interface_desc = {
10400a2430fSAndrzej Pietrasiewicz 	.bLength		= sizeof hidg_interface_desc,
10500a2430fSAndrzej Pietrasiewicz 	.bDescriptorType	= USB_DT_INTERFACE,
10600a2430fSAndrzej Pietrasiewicz 	/* .bInterfaceNumber	= DYNAMIC */
10700a2430fSAndrzej Pietrasiewicz 	.bAlternateSetting	= 0,
108d7428bc2SMaxim Devaev 	/* .bNumEndpoints	= DYNAMIC (depends on use_out_ep) */
10900a2430fSAndrzej Pietrasiewicz 	.bInterfaceClass	= USB_CLASS_HID,
11000a2430fSAndrzej Pietrasiewicz 	/* .bInterfaceSubClass	= DYNAMIC */
11100a2430fSAndrzej Pietrasiewicz 	/* .bInterfaceProtocol	= DYNAMIC */
11200a2430fSAndrzej Pietrasiewicz 	/* .iInterface		= DYNAMIC */
11300a2430fSAndrzej Pietrasiewicz };
11400a2430fSAndrzej Pietrasiewicz 
11500a2430fSAndrzej Pietrasiewicz static struct hid_descriptor hidg_desc = {
11600a2430fSAndrzej Pietrasiewicz 	.bLength			= sizeof hidg_desc,
11700a2430fSAndrzej Pietrasiewicz 	.bDescriptorType		= HID_DT_HID,
11833cb46c4SRuslan Bilovol 	.bcdHID				= cpu_to_le16(0x0101),
11900a2430fSAndrzej Pietrasiewicz 	.bCountryCode			= 0x00,
12000a2430fSAndrzej Pietrasiewicz 	.bNumDescriptors		= 0x1,
12100a2430fSAndrzej Pietrasiewicz 	/*.desc[0].bDescriptorType	= DYNAMIC */
12200a2430fSAndrzej Pietrasiewicz 	/*.desc[0].wDescriptorLenght	= DYNAMIC */
12300a2430fSAndrzej Pietrasiewicz };
12400a2430fSAndrzej Pietrasiewicz 
125dbf499cfSJanusz Dziedzic /* Super-Speed Support */
126dbf499cfSJanusz Dziedzic 
127dbf499cfSJanusz Dziedzic static struct usb_endpoint_descriptor hidg_ss_in_ep_desc = {
128dbf499cfSJanusz Dziedzic 	.bLength		= USB_DT_ENDPOINT_SIZE,
129dbf499cfSJanusz Dziedzic 	.bDescriptorType	= USB_DT_ENDPOINT,
130dbf499cfSJanusz Dziedzic 	.bEndpointAddress	= USB_DIR_IN,
131dbf499cfSJanusz Dziedzic 	.bmAttributes		= USB_ENDPOINT_XFER_INT,
132dbf499cfSJanusz Dziedzic 	/*.wMaxPacketSize	= DYNAMIC */
133dbf499cfSJanusz Dziedzic 	.bInterval		= 4, /* FIXME: Add this field in the
134dbf499cfSJanusz Dziedzic 				      * HID gadget configuration?
135dbf499cfSJanusz Dziedzic 				      * (struct hidg_func_descriptor)
136dbf499cfSJanusz Dziedzic 				      */
137dbf499cfSJanusz Dziedzic };
138dbf499cfSJanusz Dziedzic 
139dbf499cfSJanusz Dziedzic static struct usb_ss_ep_comp_descriptor hidg_ss_in_comp_desc = {
140dbf499cfSJanusz Dziedzic 	.bLength                = sizeof(hidg_ss_in_comp_desc),
141dbf499cfSJanusz Dziedzic 	.bDescriptorType        = USB_DT_SS_ENDPOINT_COMP,
142dbf499cfSJanusz Dziedzic 
143dbf499cfSJanusz Dziedzic 	/* .bMaxBurst           = 0, */
144dbf499cfSJanusz Dziedzic 	/* .bmAttributes        = 0, */
145dbf499cfSJanusz Dziedzic 	/* .wBytesPerInterval   = DYNAMIC */
146dbf499cfSJanusz Dziedzic };
147dbf499cfSJanusz Dziedzic 
148dbf499cfSJanusz Dziedzic static struct usb_endpoint_descriptor hidg_ss_out_ep_desc = {
149dbf499cfSJanusz Dziedzic 	.bLength		= USB_DT_ENDPOINT_SIZE,
150dbf499cfSJanusz Dziedzic 	.bDescriptorType	= USB_DT_ENDPOINT,
151dbf499cfSJanusz Dziedzic 	.bEndpointAddress	= USB_DIR_OUT,
152dbf499cfSJanusz Dziedzic 	.bmAttributes		= USB_ENDPOINT_XFER_INT,
153dbf499cfSJanusz Dziedzic 	/*.wMaxPacketSize	= DYNAMIC */
154dbf499cfSJanusz Dziedzic 	.bInterval		= 4, /* FIXME: Add this field in the
155dbf499cfSJanusz Dziedzic 				      * HID gadget configuration?
156dbf499cfSJanusz Dziedzic 				      * (struct hidg_func_descriptor)
157dbf499cfSJanusz Dziedzic 				      */
158dbf499cfSJanusz Dziedzic };
159dbf499cfSJanusz Dziedzic 
160dbf499cfSJanusz Dziedzic static struct usb_ss_ep_comp_descriptor hidg_ss_out_comp_desc = {
161dbf499cfSJanusz Dziedzic 	.bLength                = sizeof(hidg_ss_out_comp_desc),
162dbf499cfSJanusz Dziedzic 	.bDescriptorType        = USB_DT_SS_ENDPOINT_COMP,
163dbf499cfSJanusz Dziedzic 
164dbf499cfSJanusz Dziedzic 	/* .bMaxBurst           = 0, */
165dbf499cfSJanusz Dziedzic 	/* .bmAttributes        = 0, */
166dbf499cfSJanusz Dziedzic 	/* .wBytesPerInterval   = DYNAMIC */
167dbf499cfSJanusz Dziedzic };
168dbf499cfSJanusz Dziedzic 
169d7428bc2SMaxim Devaev static struct usb_descriptor_header *hidg_ss_descriptors_intout[] = {
170dbf499cfSJanusz Dziedzic 	(struct usb_descriptor_header *)&hidg_interface_desc,
171dbf499cfSJanusz Dziedzic 	(struct usb_descriptor_header *)&hidg_desc,
172dbf499cfSJanusz Dziedzic 	(struct usb_descriptor_header *)&hidg_ss_in_ep_desc,
173dbf499cfSJanusz Dziedzic 	(struct usb_descriptor_header *)&hidg_ss_in_comp_desc,
174dbf499cfSJanusz Dziedzic 	(struct usb_descriptor_header *)&hidg_ss_out_ep_desc,
175dbf499cfSJanusz Dziedzic 	(struct usb_descriptor_header *)&hidg_ss_out_comp_desc,
176dbf499cfSJanusz Dziedzic 	NULL,
177dbf499cfSJanusz Dziedzic };
178dbf499cfSJanusz Dziedzic 
179d7428bc2SMaxim Devaev static struct usb_descriptor_header *hidg_ss_descriptors_ssreport[] = {
180d7428bc2SMaxim Devaev 	(struct usb_descriptor_header *)&hidg_interface_desc,
181d7428bc2SMaxim Devaev 	(struct usb_descriptor_header *)&hidg_desc,
182d7428bc2SMaxim Devaev 	(struct usb_descriptor_header *)&hidg_ss_in_ep_desc,
183d7428bc2SMaxim Devaev 	(struct usb_descriptor_header *)&hidg_ss_in_comp_desc,
184d7428bc2SMaxim Devaev 	NULL,
185d7428bc2SMaxim Devaev };
186d7428bc2SMaxim Devaev 
18700a2430fSAndrzej Pietrasiewicz /* High-Speed Support */
18800a2430fSAndrzej Pietrasiewicz 
18900a2430fSAndrzej Pietrasiewicz static struct usb_endpoint_descriptor hidg_hs_in_ep_desc = {
19000a2430fSAndrzej Pietrasiewicz 	.bLength		= USB_DT_ENDPOINT_SIZE,
19100a2430fSAndrzej Pietrasiewicz 	.bDescriptorType	= USB_DT_ENDPOINT,
19200a2430fSAndrzej Pietrasiewicz 	.bEndpointAddress	= USB_DIR_IN,
19300a2430fSAndrzej Pietrasiewicz 	.bmAttributes		= USB_ENDPOINT_XFER_INT,
19400a2430fSAndrzej Pietrasiewicz 	/*.wMaxPacketSize	= DYNAMIC */
19500a2430fSAndrzej Pietrasiewicz 	.bInterval		= 4, /* FIXME: Add this field in the
19600a2430fSAndrzej Pietrasiewicz 				      * HID gadget configuration?
19700a2430fSAndrzej Pietrasiewicz 				      * (struct hidg_func_descriptor)
19800a2430fSAndrzej Pietrasiewicz 				      */
19900a2430fSAndrzej Pietrasiewicz };
20000a2430fSAndrzej Pietrasiewicz 
20100a2430fSAndrzej Pietrasiewicz static struct usb_endpoint_descriptor hidg_hs_out_ep_desc = {
20200a2430fSAndrzej Pietrasiewicz 	.bLength		= USB_DT_ENDPOINT_SIZE,
20300a2430fSAndrzej Pietrasiewicz 	.bDescriptorType	= USB_DT_ENDPOINT,
20400a2430fSAndrzej Pietrasiewicz 	.bEndpointAddress	= USB_DIR_OUT,
20500a2430fSAndrzej Pietrasiewicz 	.bmAttributes		= USB_ENDPOINT_XFER_INT,
20600a2430fSAndrzej Pietrasiewicz 	/*.wMaxPacketSize	= DYNAMIC */
20700a2430fSAndrzej Pietrasiewicz 	.bInterval		= 4, /* FIXME: Add this field in the
20800a2430fSAndrzej Pietrasiewicz 				      * HID gadget configuration?
20900a2430fSAndrzej Pietrasiewicz 				      * (struct hidg_func_descriptor)
21000a2430fSAndrzej Pietrasiewicz 				      */
21100a2430fSAndrzej Pietrasiewicz };
21200a2430fSAndrzej Pietrasiewicz 
213d7428bc2SMaxim Devaev static struct usb_descriptor_header *hidg_hs_descriptors_intout[] = {
21400a2430fSAndrzej Pietrasiewicz 	(struct usb_descriptor_header *)&hidg_interface_desc,
21500a2430fSAndrzej Pietrasiewicz 	(struct usb_descriptor_header *)&hidg_desc,
21600a2430fSAndrzej Pietrasiewicz 	(struct usb_descriptor_header *)&hidg_hs_in_ep_desc,
21700a2430fSAndrzej Pietrasiewicz 	(struct usb_descriptor_header *)&hidg_hs_out_ep_desc,
21800a2430fSAndrzej Pietrasiewicz 	NULL,
21900a2430fSAndrzej Pietrasiewicz };
22000a2430fSAndrzej Pietrasiewicz 
221d7428bc2SMaxim Devaev static struct usb_descriptor_header *hidg_hs_descriptors_ssreport[] = {
222d7428bc2SMaxim Devaev 	(struct usb_descriptor_header *)&hidg_interface_desc,
223d7428bc2SMaxim Devaev 	(struct usb_descriptor_header *)&hidg_desc,
224d7428bc2SMaxim Devaev 	(struct usb_descriptor_header *)&hidg_hs_in_ep_desc,
225d7428bc2SMaxim Devaev 	NULL,
226d7428bc2SMaxim Devaev };
227d7428bc2SMaxim Devaev 
22800a2430fSAndrzej Pietrasiewicz /* Full-Speed Support */
22900a2430fSAndrzej Pietrasiewicz 
23000a2430fSAndrzej Pietrasiewicz static struct usb_endpoint_descriptor hidg_fs_in_ep_desc = {
23100a2430fSAndrzej Pietrasiewicz 	.bLength		= USB_DT_ENDPOINT_SIZE,
23200a2430fSAndrzej Pietrasiewicz 	.bDescriptorType	= USB_DT_ENDPOINT,
23300a2430fSAndrzej Pietrasiewicz 	.bEndpointAddress	= USB_DIR_IN,
23400a2430fSAndrzej Pietrasiewicz 	.bmAttributes		= USB_ENDPOINT_XFER_INT,
23500a2430fSAndrzej Pietrasiewicz 	/*.wMaxPacketSize	= DYNAMIC */
23600a2430fSAndrzej Pietrasiewicz 	.bInterval		= 10, /* FIXME: Add this field in the
23700a2430fSAndrzej Pietrasiewicz 				       * HID gadget configuration?
23800a2430fSAndrzej Pietrasiewicz 				       * (struct hidg_func_descriptor)
23900a2430fSAndrzej Pietrasiewicz 				       */
24000a2430fSAndrzej Pietrasiewicz };
24100a2430fSAndrzej Pietrasiewicz 
24200a2430fSAndrzej Pietrasiewicz static struct usb_endpoint_descriptor hidg_fs_out_ep_desc = {
24300a2430fSAndrzej Pietrasiewicz 	.bLength		= USB_DT_ENDPOINT_SIZE,
24400a2430fSAndrzej Pietrasiewicz 	.bDescriptorType	= USB_DT_ENDPOINT,
24500a2430fSAndrzej Pietrasiewicz 	.bEndpointAddress	= USB_DIR_OUT,
24600a2430fSAndrzej Pietrasiewicz 	.bmAttributes		= USB_ENDPOINT_XFER_INT,
24700a2430fSAndrzej Pietrasiewicz 	/*.wMaxPacketSize	= DYNAMIC */
24800a2430fSAndrzej Pietrasiewicz 	.bInterval		= 10, /* FIXME: Add this field in the
24900a2430fSAndrzej Pietrasiewicz 				       * HID gadget configuration?
25000a2430fSAndrzej Pietrasiewicz 				       * (struct hidg_func_descriptor)
25100a2430fSAndrzej Pietrasiewicz 				       */
25200a2430fSAndrzej Pietrasiewicz };
25300a2430fSAndrzej Pietrasiewicz 
254d7428bc2SMaxim Devaev static struct usb_descriptor_header *hidg_fs_descriptors_intout[] = {
25500a2430fSAndrzej Pietrasiewicz 	(struct usb_descriptor_header *)&hidg_interface_desc,
25600a2430fSAndrzej Pietrasiewicz 	(struct usb_descriptor_header *)&hidg_desc,
25700a2430fSAndrzej Pietrasiewicz 	(struct usb_descriptor_header *)&hidg_fs_in_ep_desc,
25800a2430fSAndrzej Pietrasiewicz 	(struct usb_descriptor_header *)&hidg_fs_out_ep_desc,
25900a2430fSAndrzej Pietrasiewicz 	NULL,
26000a2430fSAndrzej Pietrasiewicz };
26100a2430fSAndrzej Pietrasiewicz 
262d7428bc2SMaxim Devaev static struct usb_descriptor_header *hidg_fs_descriptors_ssreport[] = {
263d7428bc2SMaxim Devaev 	(struct usb_descriptor_header *)&hidg_interface_desc,
264d7428bc2SMaxim Devaev 	(struct usb_descriptor_header *)&hidg_desc,
265d7428bc2SMaxim Devaev 	(struct usb_descriptor_header *)&hidg_fs_in_ep_desc,
266d7428bc2SMaxim Devaev 	NULL,
267d7428bc2SMaxim Devaev };
268d7428bc2SMaxim Devaev 
26900a2430fSAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/
270cb382536SAndrzej Pietrasiewicz /*                                 Strings                                 */
271cb382536SAndrzej Pietrasiewicz 
272cb382536SAndrzej Pietrasiewicz #define CT_FUNC_HID_IDX	0
273cb382536SAndrzej Pietrasiewicz 
274cb382536SAndrzej Pietrasiewicz static struct usb_string ct_func_string_defs[] = {
275cb382536SAndrzej Pietrasiewicz 	[CT_FUNC_HID_IDX].s	= "HID Interface",
276cb382536SAndrzej Pietrasiewicz 	{},			/* end of list */
277cb382536SAndrzej Pietrasiewicz };
278cb382536SAndrzej Pietrasiewicz 
279cb382536SAndrzej Pietrasiewicz static struct usb_gadget_strings ct_func_string_table = {
280cb382536SAndrzej Pietrasiewicz 	.language	= 0x0409,	/* en-US */
281cb382536SAndrzej Pietrasiewicz 	.strings	= ct_func_string_defs,
282cb382536SAndrzej Pietrasiewicz };
283cb382536SAndrzej Pietrasiewicz 
284cb382536SAndrzej Pietrasiewicz static struct usb_gadget_strings *ct_func_strings[] = {
285cb382536SAndrzej Pietrasiewicz 	&ct_func_string_table,
286cb382536SAndrzej Pietrasiewicz 	NULL,
287cb382536SAndrzej Pietrasiewicz };
288cb382536SAndrzej Pietrasiewicz 
289cb382536SAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/
29000a2430fSAndrzej Pietrasiewicz /*                              Char Device                                */
29100a2430fSAndrzej Pietrasiewicz 
f_hidg_intout_read(struct file * file,char __user * buffer,size_t count,loff_t * ptr)292d7428bc2SMaxim Devaev static ssize_t f_hidg_intout_read(struct file *file, char __user *buffer,
29300a2430fSAndrzej Pietrasiewicz 				  size_t count, loff_t *ptr)
29400a2430fSAndrzej Pietrasiewicz {
29500a2430fSAndrzej Pietrasiewicz 	struct f_hidg *hidg = file->private_data;
29600a2430fSAndrzej Pietrasiewicz 	struct f_hidg_req_list *list;
29700a2430fSAndrzej Pietrasiewicz 	struct usb_request *req;
29800a2430fSAndrzej Pietrasiewicz 	unsigned long flags;
29900a2430fSAndrzej Pietrasiewicz 	int ret;
30000a2430fSAndrzej Pietrasiewicz 
30100a2430fSAndrzej Pietrasiewicz 	if (!count)
30200a2430fSAndrzej Pietrasiewicz 		return 0;
30300a2430fSAndrzej Pietrasiewicz 
30433e4c1a9SKrzysztof Opasiak 	spin_lock_irqsave(&hidg->read_spinlock, flags);
30500a2430fSAndrzej Pietrasiewicz 
306d7428bc2SMaxim Devaev #define READ_COND_INTOUT (!list_empty(&hidg->completed_out_req))
30700a2430fSAndrzej Pietrasiewicz 
30800a2430fSAndrzej Pietrasiewicz 	/* wait for at least one buffer to complete */
309d7428bc2SMaxim Devaev 	while (!READ_COND_INTOUT) {
31033e4c1a9SKrzysztof Opasiak 		spin_unlock_irqrestore(&hidg->read_spinlock, flags);
31100a2430fSAndrzej Pietrasiewicz 		if (file->f_flags & O_NONBLOCK)
31200a2430fSAndrzej Pietrasiewicz 			return -EAGAIN;
31300a2430fSAndrzej Pietrasiewicz 
314d7428bc2SMaxim Devaev 		if (wait_event_interruptible(hidg->read_queue, READ_COND_INTOUT))
31500a2430fSAndrzej Pietrasiewicz 			return -ERESTARTSYS;
31600a2430fSAndrzej Pietrasiewicz 
31733e4c1a9SKrzysztof Opasiak 		spin_lock_irqsave(&hidg->read_spinlock, flags);
31800a2430fSAndrzej Pietrasiewicz 	}
31900a2430fSAndrzej Pietrasiewicz 
32000a2430fSAndrzej Pietrasiewicz 	/* pick the first one */
32100a2430fSAndrzej Pietrasiewicz 	list = list_first_entry(&hidg->completed_out_req,
32200a2430fSAndrzej Pietrasiewicz 				struct f_hidg_req_list, list);
323aa65d11aSKrzysztof Opasiak 
324aa65d11aSKrzysztof Opasiak 	/*
325aa65d11aSKrzysztof Opasiak 	 * Remove this from list to protect it from beign free()
326aa65d11aSKrzysztof Opasiak 	 * while host disables our function
327aa65d11aSKrzysztof Opasiak 	 */
328aa65d11aSKrzysztof Opasiak 	list_del(&list->list);
329aa65d11aSKrzysztof Opasiak 
33000a2430fSAndrzej Pietrasiewicz 	req = list->req;
33100a2430fSAndrzej Pietrasiewicz 	count = min_t(unsigned int, count, req->actual - list->pos);
33233e4c1a9SKrzysztof Opasiak 	spin_unlock_irqrestore(&hidg->read_spinlock, flags);
33300a2430fSAndrzej Pietrasiewicz 
33400a2430fSAndrzej Pietrasiewicz 	/* copy to user outside spinlock */
33500a2430fSAndrzej Pietrasiewicz 	count -= copy_to_user(buffer, req->buf + list->pos, count);
33600a2430fSAndrzej Pietrasiewicz 	list->pos += count;
33700a2430fSAndrzej Pietrasiewicz 
33800a2430fSAndrzej Pietrasiewicz 	/*
33900a2430fSAndrzej Pietrasiewicz 	 * if this request is completely handled and transfered to
34000a2430fSAndrzej Pietrasiewicz 	 * userspace, remove its entry from the list and requeue it
34100a2430fSAndrzej Pietrasiewicz 	 * again. Otherwise, we will revisit it again upon the next
34200a2430fSAndrzej Pietrasiewicz 	 * call, taking into account its current read position.
34300a2430fSAndrzej Pietrasiewicz 	 */
34400a2430fSAndrzej Pietrasiewicz 	if (list->pos == req->actual) {
34500a2430fSAndrzej Pietrasiewicz 		kfree(list);
34600a2430fSAndrzej Pietrasiewicz 
34700a2430fSAndrzej Pietrasiewicz 		req->length = hidg->report_length;
34800a2430fSAndrzej Pietrasiewicz 		ret = usb_ep_queue(hidg->out_ep, req, GFP_KERNEL);
349aa65d11aSKrzysztof Opasiak 		if (ret < 0) {
350aa65d11aSKrzysztof Opasiak 			free_ep_req(hidg->out_ep, req);
35100a2430fSAndrzej Pietrasiewicz 			return ret;
35200a2430fSAndrzej Pietrasiewicz 		}
353aa65d11aSKrzysztof Opasiak 	} else {
35433e4c1a9SKrzysztof Opasiak 		spin_lock_irqsave(&hidg->read_spinlock, flags);
355aa65d11aSKrzysztof Opasiak 		list_add(&list->list, &hidg->completed_out_req);
35633e4c1a9SKrzysztof Opasiak 		spin_unlock_irqrestore(&hidg->read_spinlock, flags);
357aa65d11aSKrzysztof Opasiak 
358aa65d11aSKrzysztof Opasiak 		wake_up(&hidg->read_queue);
359aa65d11aSKrzysztof Opasiak 	}
36000a2430fSAndrzej Pietrasiewicz 
36100a2430fSAndrzej Pietrasiewicz 	return count;
36200a2430fSAndrzej Pietrasiewicz }
36300a2430fSAndrzej Pietrasiewicz 
364d7428bc2SMaxim Devaev #define READ_COND_SSREPORT (hidg->set_report_buf != NULL)
365d7428bc2SMaxim Devaev 
f_hidg_ssreport_read(struct file * file,char __user * buffer,size_t count,loff_t * ptr)366d7428bc2SMaxim Devaev static ssize_t f_hidg_ssreport_read(struct file *file, char __user *buffer,
367d7428bc2SMaxim Devaev 				    size_t count, loff_t *ptr)
368d7428bc2SMaxim Devaev {
369d7428bc2SMaxim Devaev 	struct f_hidg *hidg = file->private_data;
370d7428bc2SMaxim Devaev 	char *tmp_buf = NULL;
371d7428bc2SMaxim Devaev 	unsigned long flags;
372d7428bc2SMaxim Devaev 
373d7428bc2SMaxim Devaev 	if (!count)
374d7428bc2SMaxim Devaev 		return 0;
375d7428bc2SMaxim Devaev 
376d7428bc2SMaxim Devaev 	spin_lock_irqsave(&hidg->read_spinlock, flags);
377d7428bc2SMaxim Devaev 
378d7428bc2SMaxim Devaev 	while (!READ_COND_SSREPORT) {
379d7428bc2SMaxim Devaev 		spin_unlock_irqrestore(&hidg->read_spinlock, flags);
380d7428bc2SMaxim Devaev 		if (file->f_flags & O_NONBLOCK)
381d7428bc2SMaxim Devaev 			return -EAGAIN;
382d7428bc2SMaxim Devaev 
383d7428bc2SMaxim Devaev 		if (wait_event_interruptible(hidg->read_queue, READ_COND_SSREPORT))
384d7428bc2SMaxim Devaev 			return -ERESTARTSYS;
385d7428bc2SMaxim Devaev 
386d7428bc2SMaxim Devaev 		spin_lock_irqsave(&hidg->read_spinlock, flags);
387d7428bc2SMaxim Devaev 	}
388d7428bc2SMaxim Devaev 
389d7428bc2SMaxim Devaev 	count = min_t(unsigned int, count, hidg->set_report_length);
390d7428bc2SMaxim Devaev 	tmp_buf = hidg->set_report_buf;
391d7428bc2SMaxim Devaev 	hidg->set_report_buf = NULL;
392d7428bc2SMaxim Devaev 
393d7428bc2SMaxim Devaev 	spin_unlock_irqrestore(&hidg->read_spinlock, flags);
394d7428bc2SMaxim Devaev 
395d7428bc2SMaxim Devaev 	if (tmp_buf != NULL) {
396d7428bc2SMaxim Devaev 		count -= copy_to_user(buffer, tmp_buf, count);
397d7428bc2SMaxim Devaev 		kfree(tmp_buf);
398d7428bc2SMaxim Devaev 	} else {
399d7428bc2SMaxim Devaev 		count = -ENOMEM;
400d7428bc2SMaxim Devaev 	}
401d7428bc2SMaxim Devaev 
402d7428bc2SMaxim Devaev 	wake_up(&hidg->read_queue);
403d7428bc2SMaxim Devaev 
404d7428bc2SMaxim Devaev 	return count;
405d7428bc2SMaxim Devaev }
406d7428bc2SMaxim Devaev 
f_hidg_read(struct file * file,char __user * buffer,size_t count,loff_t * ptr)407d7428bc2SMaxim Devaev static ssize_t f_hidg_read(struct file *file, char __user *buffer,
408d7428bc2SMaxim Devaev 			   size_t count, loff_t *ptr)
409d7428bc2SMaxim Devaev {
410d7428bc2SMaxim Devaev 	struct f_hidg *hidg = file->private_data;
411d7428bc2SMaxim Devaev 
412d7428bc2SMaxim Devaev 	if (hidg->use_out_ep)
413d7428bc2SMaxim Devaev 		return f_hidg_intout_read(file, buffer, count, ptr);
414d7428bc2SMaxim Devaev 	else
415d7428bc2SMaxim Devaev 		return f_hidg_ssreport_read(file, buffer, count, ptr);
416d7428bc2SMaxim Devaev }
417d7428bc2SMaxim Devaev 
f_hidg_req_complete(struct usb_ep * ep,struct usb_request * req)41800a2430fSAndrzej Pietrasiewicz static void f_hidg_req_complete(struct usb_ep *ep, struct usb_request *req)
41900a2430fSAndrzej Pietrasiewicz {
42000a2430fSAndrzej Pietrasiewicz 	struct f_hidg *hidg = (struct f_hidg *)ep->driver_data;
42133e4c1a9SKrzysztof Opasiak 	unsigned long flags;
42200a2430fSAndrzej Pietrasiewicz 
42300a2430fSAndrzej Pietrasiewicz 	if (req->status != 0) {
42400a2430fSAndrzej Pietrasiewicz 		ERROR(hidg->func.config->cdev,
42500a2430fSAndrzej Pietrasiewicz 			"End Point Request ERROR: %d\n", req->status);
42600a2430fSAndrzej Pietrasiewicz 	}
42700a2430fSAndrzej Pietrasiewicz 
42833e4c1a9SKrzysztof Opasiak 	spin_lock_irqsave(&hidg->write_spinlock, flags);
42900a2430fSAndrzej Pietrasiewicz 	hidg->write_pending = 0;
43033e4c1a9SKrzysztof Opasiak 	spin_unlock_irqrestore(&hidg->write_spinlock, flags);
43100a2430fSAndrzej Pietrasiewicz 	wake_up(&hidg->write_queue);
43200a2430fSAndrzej Pietrasiewicz }
43300a2430fSAndrzej Pietrasiewicz 
f_hidg_write(struct file * file,const char __user * buffer,size_t count,loff_t * offp)43400a2430fSAndrzej Pietrasiewicz static ssize_t f_hidg_write(struct file *file, const char __user *buffer,
43500a2430fSAndrzej Pietrasiewicz 			    size_t count, loff_t *offp)
43600a2430fSAndrzej Pietrasiewicz {
43700a2430fSAndrzej Pietrasiewicz 	struct f_hidg *hidg  = file->private_data;
438749494b6SKrzysztof Opasiak 	struct usb_request *req;
43933e4c1a9SKrzysztof Opasiak 	unsigned long flags;
44000a2430fSAndrzej Pietrasiewicz 	ssize_t status = -ENOMEM;
44100a2430fSAndrzej Pietrasiewicz 
44233e4c1a9SKrzysztof Opasiak 	spin_lock_irqsave(&hidg->write_spinlock, flags);
44300a2430fSAndrzej Pietrasiewicz 
4442867652eSPhil Elwell 	if (!hidg->req) {
4452867652eSPhil Elwell 		spin_unlock_irqrestore(&hidg->write_spinlock, flags);
4462867652eSPhil Elwell 		return -ESHUTDOWN;
4472867652eSPhil Elwell 	}
4482867652eSPhil Elwell 
44900a2430fSAndrzej Pietrasiewicz #define WRITE_COND (!hidg->write_pending)
450749494b6SKrzysztof Opasiak try_again:
45100a2430fSAndrzej Pietrasiewicz 	/* write queue */
45200a2430fSAndrzej Pietrasiewicz 	while (!WRITE_COND) {
45333e4c1a9SKrzysztof Opasiak 		spin_unlock_irqrestore(&hidg->write_spinlock, flags);
45400a2430fSAndrzej Pietrasiewicz 		if (file->f_flags & O_NONBLOCK)
45500a2430fSAndrzej Pietrasiewicz 			return -EAGAIN;
45600a2430fSAndrzej Pietrasiewicz 
45700a2430fSAndrzej Pietrasiewicz 		if (wait_event_interruptible_exclusive(
45800a2430fSAndrzej Pietrasiewicz 				hidg->write_queue, WRITE_COND))
45900a2430fSAndrzej Pietrasiewicz 			return -ERESTARTSYS;
46000a2430fSAndrzej Pietrasiewicz 
46133e4c1a9SKrzysztof Opasiak 		spin_lock_irqsave(&hidg->write_spinlock, flags);
46200a2430fSAndrzej Pietrasiewicz 	}
46300a2430fSAndrzej Pietrasiewicz 
46433e4c1a9SKrzysztof Opasiak 	hidg->write_pending = 1;
465749494b6SKrzysztof Opasiak 	req = hidg->req;
46600a2430fSAndrzej Pietrasiewicz 	count  = min_t(unsigned, count, hidg->report_length);
46733e4c1a9SKrzysztof Opasiak 
46833e4c1a9SKrzysztof Opasiak 	spin_unlock_irqrestore(&hidg->write_spinlock, flags);
46900a2430fSAndrzej Pietrasiewicz 
4702867652eSPhil Elwell 	if (!req) {
4712867652eSPhil Elwell 		ERROR(hidg->func.config->cdev, "hidg->req is NULL\n");
4722867652eSPhil Elwell 		status = -ESHUTDOWN;
4732867652eSPhil Elwell 		goto release_write_pending;
4742867652eSPhil Elwell 	}
4752867652eSPhil Elwell 
4762867652eSPhil Elwell 	status = copy_from_user(req->buf, buffer, count);
47700a2430fSAndrzej Pietrasiewicz 	if (status != 0) {
47800a2430fSAndrzej Pietrasiewicz 		ERROR(hidg->func.config->cdev,
47900a2430fSAndrzej Pietrasiewicz 			"copy_from_user error\n");
48033e4c1a9SKrzysztof Opasiak 		status = -EINVAL;
48133e4c1a9SKrzysztof Opasiak 		goto release_write_pending;
48200a2430fSAndrzej Pietrasiewicz 	}
48300a2430fSAndrzej Pietrasiewicz 
484749494b6SKrzysztof Opasiak 	spin_lock_irqsave(&hidg->write_spinlock, flags);
485749494b6SKrzysztof Opasiak 
48625cd9721SKrzysztof Opasiak 	/* when our function has been disabled by host */
487749494b6SKrzysztof Opasiak 	if (!hidg->req) {
48825cd9721SKrzysztof Opasiak 		free_ep_req(hidg->in_ep, req);
489749494b6SKrzysztof Opasiak 		/*
490749494b6SKrzysztof Opasiak 		 * TODO
491749494b6SKrzysztof Opasiak 		 * Should we fail with error here?
492749494b6SKrzysztof Opasiak 		 */
493749494b6SKrzysztof Opasiak 		goto try_again;
494749494b6SKrzysztof Opasiak 	}
495749494b6SKrzysztof Opasiak 
496749494b6SKrzysztof Opasiak 	req->status   = 0;
497749494b6SKrzysztof Opasiak 	req->zero     = 0;
498749494b6SKrzysztof Opasiak 	req->length   = count;
499749494b6SKrzysztof Opasiak 	req->complete = f_hidg_req_complete;
500749494b6SKrzysztof Opasiak 	req->context  = hidg;
50100a2430fSAndrzej Pietrasiewicz 
502072684e8SRadoslav Gerganov 	spin_unlock_irqrestore(&hidg->write_spinlock, flags);
503072684e8SRadoslav Gerganov 
5042867652eSPhil Elwell 	if (!hidg->in_ep->enabled) {
5052867652eSPhil Elwell 		ERROR(hidg->func.config->cdev, "in_ep is disabled\n");
5062867652eSPhil Elwell 		status = -ESHUTDOWN;
507072684e8SRadoslav Gerganov 		goto release_write_pending;
50800a2430fSAndrzej Pietrasiewicz 	}
50900a2430fSAndrzej Pietrasiewicz 
5102867652eSPhil Elwell 	status = usb_ep_queue(hidg->in_ep, req, GFP_ATOMIC);
5112867652eSPhil Elwell 	if (status < 0)
5122867652eSPhil Elwell 		goto release_write_pending;
5132867652eSPhil Elwell 	else
5142867652eSPhil Elwell 		status = count;
5152867652eSPhil Elwell 
51633e4c1a9SKrzysztof Opasiak 	return status;
51733e4c1a9SKrzysztof Opasiak release_write_pending:
51833e4c1a9SKrzysztof Opasiak 	spin_lock_irqsave(&hidg->write_spinlock, flags);
51933e4c1a9SKrzysztof Opasiak 	hidg->write_pending = 0;
52033e4c1a9SKrzysztof Opasiak 	spin_unlock_irqrestore(&hidg->write_spinlock, flags);
52133e4c1a9SKrzysztof Opasiak 
52233e4c1a9SKrzysztof Opasiak 	wake_up(&hidg->write_queue);
52300a2430fSAndrzej Pietrasiewicz 
52400a2430fSAndrzej Pietrasiewicz 	return status;
52500a2430fSAndrzej Pietrasiewicz }
52600a2430fSAndrzej Pietrasiewicz 
f_hidg_poll(struct file * file,poll_table * wait)527afc9a42bSAl Viro static __poll_t f_hidg_poll(struct file *file, poll_table *wait)
52800a2430fSAndrzej Pietrasiewicz {
52900a2430fSAndrzej Pietrasiewicz 	struct f_hidg	*hidg  = file->private_data;
530afc9a42bSAl Viro 	__poll_t	ret = 0;
53100a2430fSAndrzej Pietrasiewicz 
53200a2430fSAndrzej Pietrasiewicz 	poll_wait(file, &hidg->read_queue, wait);
53300a2430fSAndrzej Pietrasiewicz 	poll_wait(file, &hidg->write_queue, wait);
53400a2430fSAndrzej Pietrasiewicz 
53500a2430fSAndrzej Pietrasiewicz 	if (WRITE_COND)
536a9a08845SLinus Torvalds 		ret |= EPOLLOUT | EPOLLWRNORM;
53700a2430fSAndrzej Pietrasiewicz 
538d7428bc2SMaxim Devaev 	if (hidg->use_out_ep) {
539d7428bc2SMaxim Devaev 		if (READ_COND_INTOUT)
540a9a08845SLinus Torvalds 			ret |= EPOLLIN | EPOLLRDNORM;
541d7428bc2SMaxim Devaev 	} else {
542d7428bc2SMaxim Devaev 		if (READ_COND_SSREPORT)
543d7428bc2SMaxim Devaev 			ret |= EPOLLIN | EPOLLRDNORM;
544d7428bc2SMaxim Devaev 	}
54500a2430fSAndrzej Pietrasiewicz 
54600a2430fSAndrzej Pietrasiewicz 	return ret;
54700a2430fSAndrzej Pietrasiewicz }
54800a2430fSAndrzej Pietrasiewicz 
54900a2430fSAndrzej Pietrasiewicz #undef WRITE_COND
550d7428bc2SMaxim Devaev #undef READ_COND_SSREPORT
551d7428bc2SMaxim Devaev #undef READ_COND_INTOUT
55200a2430fSAndrzej Pietrasiewicz 
f_hidg_release(struct inode * inode,struct file * fd)55300a2430fSAndrzej Pietrasiewicz static int f_hidg_release(struct inode *inode, struct file *fd)
55400a2430fSAndrzej Pietrasiewicz {
55500a2430fSAndrzej Pietrasiewicz 	fd->private_data = NULL;
55600a2430fSAndrzej Pietrasiewicz 	return 0;
55700a2430fSAndrzej Pietrasiewicz }
55800a2430fSAndrzej Pietrasiewicz 
f_hidg_open(struct inode * inode,struct file * fd)55900a2430fSAndrzej Pietrasiewicz static int f_hidg_open(struct inode *inode, struct file *fd)
56000a2430fSAndrzej Pietrasiewicz {
56100a2430fSAndrzej Pietrasiewicz 	struct f_hidg *hidg =
56200a2430fSAndrzej Pietrasiewicz 		container_of(inode->i_cdev, struct f_hidg, cdev);
56300a2430fSAndrzej Pietrasiewicz 
56400a2430fSAndrzej Pietrasiewicz 	fd->private_data = hidg;
56500a2430fSAndrzej Pietrasiewicz 
56600a2430fSAndrzej Pietrasiewicz 	return 0;
56700a2430fSAndrzej Pietrasiewicz }
56800a2430fSAndrzej Pietrasiewicz 
56900a2430fSAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/
57000a2430fSAndrzej Pietrasiewicz /*                                usb_function                             */
57100a2430fSAndrzej Pietrasiewicz 
hidg_alloc_ep_req(struct usb_ep * ep,unsigned length)57200a2430fSAndrzej Pietrasiewicz static inline struct usb_request *hidg_alloc_ep_req(struct usb_ep *ep,
57300a2430fSAndrzej Pietrasiewicz 						    unsigned length)
57400a2430fSAndrzej Pietrasiewicz {
575aadbe812SFelipe F. Tonello 	return alloc_ep_req(ep, length);
57600a2430fSAndrzej Pietrasiewicz }
57700a2430fSAndrzej Pietrasiewicz 
hidg_intout_complete(struct usb_ep * ep,struct usb_request * req)578d7428bc2SMaxim Devaev static void hidg_intout_complete(struct usb_ep *ep, struct usb_request *req)
57900a2430fSAndrzej Pietrasiewicz {
58000a2430fSAndrzej Pietrasiewicz 	struct f_hidg *hidg = (struct f_hidg *) req->context;
58120d2ca95SKrzysztof Opasiak 	struct usb_composite_dev *cdev = hidg->func.config->cdev;
58200a2430fSAndrzej Pietrasiewicz 	struct f_hidg_req_list *req_list;
58300a2430fSAndrzej Pietrasiewicz 	unsigned long flags;
58400a2430fSAndrzej Pietrasiewicz 
58520d2ca95SKrzysztof Opasiak 	switch (req->status) {
58620d2ca95SKrzysztof Opasiak 	case 0:
58700a2430fSAndrzej Pietrasiewicz 		req_list = kzalloc(sizeof(*req_list), GFP_ATOMIC);
58820d2ca95SKrzysztof Opasiak 		if (!req_list) {
58920d2ca95SKrzysztof Opasiak 			ERROR(cdev, "Unable to allocate mem for req_list\n");
59020d2ca95SKrzysztof Opasiak 			goto free_req;
59120d2ca95SKrzysztof Opasiak 		}
59200a2430fSAndrzej Pietrasiewicz 
59300a2430fSAndrzej Pietrasiewicz 		req_list->req = req;
59400a2430fSAndrzej Pietrasiewicz 
59533e4c1a9SKrzysztof Opasiak 		spin_lock_irqsave(&hidg->read_spinlock, flags);
59600a2430fSAndrzej Pietrasiewicz 		list_add_tail(&req_list->list, &hidg->completed_out_req);
59733e4c1a9SKrzysztof Opasiak 		spin_unlock_irqrestore(&hidg->read_spinlock, flags);
59800a2430fSAndrzej Pietrasiewicz 
59900a2430fSAndrzej Pietrasiewicz 		wake_up(&hidg->read_queue);
60020d2ca95SKrzysztof Opasiak 		break;
60120d2ca95SKrzysztof Opasiak 	default:
60220d2ca95SKrzysztof Opasiak 		ERROR(cdev, "Set report failed %d\n", req->status);
603a74005abSGustavo A. R. Silva 		fallthrough;
60420d2ca95SKrzysztof Opasiak 	case -ECONNABORTED:		/* hardware forced ep reset */
60520d2ca95SKrzysztof Opasiak 	case -ECONNRESET:		/* request dequeued */
60620d2ca95SKrzysztof Opasiak 	case -ESHUTDOWN:		/* disconnect from host */
60720d2ca95SKrzysztof Opasiak free_req:
60820d2ca95SKrzysztof Opasiak 		free_ep_req(ep, req);
60920d2ca95SKrzysztof Opasiak 		return;
61020d2ca95SKrzysztof Opasiak 	}
61100a2430fSAndrzej Pietrasiewicz }
61200a2430fSAndrzej Pietrasiewicz 
hidg_ssreport_complete(struct usb_ep * ep,struct usb_request * req)613d7428bc2SMaxim Devaev static void hidg_ssreport_complete(struct usb_ep *ep, struct usb_request *req)
614d7428bc2SMaxim Devaev {
615d7428bc2SMaxim Devaev 	struct f_hidg *hidg = (struct f_hidg *)req->context;
616d7428bc2SMaxim Devaev 	struct usb_composite_dev *cdev = hidg->func.config->cdev;
617d7428bc2SMaxim Devaev 	char *new_buf = NULL;
618d7428bc2SMaxim Devaev 	unsigned long flags;
619d7428bc2SMaxim Devaev 
620d7428bc2SMaxim Devaev 	if (req->status != 0 || req->buf == NULL || req->actual == 0) {
621d7428bc2SMaxim Devaev 		ERROR(cdev,
622d7428bc2SMaxim Devaev 		      "%s FAILED: status=%d, buf=%p, actual=%d\n",
623d7428bc2SMaxim Devaev 		      __func__, req->status, req->buf, req->actual);
624d7428bc2SMaxim Devaev 		return;
625d7428bc2SMaxim Devaev 	}
626d7428bc2SMaxim Devaev 
627d7428bc2SMaxim Devaev 	spin_lock_irqsave(&hidg->read_spinlock, flags);
628d7428bc2SMaxim Devaev 
629d7428bc2SMaxim Devaev 	new_buf = krealloc(hidg->set_report_buf, req->actual, GFP_ATOMIC);
630d7428bc2SMaxim Devaev 	if (new_buf == NULL) {
631d7428bc2SMaxim Devaev 		spin_unlock_irqrestore(&hidg->read_spinlock, flags);
632d7428bc2SMaxim Devaev 		return;
633d7428bc2SMaxim Devaev 	}
634d7428bc2SMaxim Devaev 	hidg->set_report_buf = new_buf;
635d7428bc2SMaxim Devaev 
636d7428bc2SMaxim Devaev 	hidg->set_report_length = req->actual;
637d7428bc2SMaxim Devaev 	memcpy(hidg->set_report_buf, req->buf, req->actual);
638d7428bc2SMaxim Devaev 
639d7428bc2SMaxim Devaev 	spin_unlock_irqrestore(&hidg->read_spinlock, flags);
640d7428bc2SMaxim Devaev 
641d7428bc2SMaxim Devaev 	wake_up(&hidg->read_queue);
642d7428bc2SMaxim Devaev }
643d7428bc2SMaxim Devaev 
hidg_setup(struct usb_function * f,const struct usb_ctrlrequest * ctrl)64400a2430fSAndrzej Pietrasiewicz static int hidg_setup(struct usb_function *f,
64500a2430fSAndrzej Pietrasiewicz 		const struct usb_ctrlrequest *ctrl)
64600a2430fSAndrzej Pietrasiewicz {
64700a2430fSAndrzej Pietrasiewicz 	struct f_hidg			*hidg = func_to_hidg(f);
64800a2430fSAndrzej Pietrasiewicz 	struct usb_composite_dev	*cdev = f->config->cdev;
64900a2430fSAndrzej Pietrasiewicz 	struct usb_request		*req  = cdev->req;
65000a2430fSAndrzej Pietrasiewicz 	int status = 0;
65100a2430fSAndrzej Pietrasiewicz 	__u16 value, length;
65200a2430fSAndrzej Pietrasiewicz 
65300a2430fSAndrzej Pietrasiewicz 	value	= __le16_to_cpu(ctrl->wValue);
65400a2430fSAndrzej Pietrasiewicz 	length	= __le16_to_cpu(ctrl->wLength);
65500a2430fSAndrzej Pietrasiewicz 
656c9b3bde0SJulia Lawall 	VDBG(cdev,
657c9b3bde0SJulia Lawall 	     "%s crtl_request : bRequestType:0x%x bRequest:0x%x Value:0x%x\n",
658c9b3bde0SJulia Lawall 	     __func__, ctrl->bRequestType, ctrl->bRequest, value);
65900a2430fSAndrzej Pietrasiewicz 
66000a2430fSAndrzej Pietrasiewicz 	switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
66100a2430fSAndrzej Pietrasiewicz 	case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
66200a2430fSAndrzej Pietrasiewicz 		  | HID_REQ_GET_REPORT):
66300a2430fSAndrzej Pietrasiewicz 		VDBG(cdev, "get_report\n");
66400a2430fSAndrzej Pietrasiewicz 
66500a2430fSAndrzej Pietrasiewicz 		/* send an empty report */
66600a2430fSAndrzej Pietrasiewicz 		length = min_t(unsigned, length, hidg->report_length);
66700a2430fSAndrzej Pietrasiewicz 		memset(req->buf, 0x0, length);
66800a2430fSAndrzej Pietrasiewicz 
66900a2430fSAndrzej Pietrasiewicz 		goto respond;
67000a2430fSAndrzej Pietrasiewicz 		break;
67100a2430fSAndrzej Pietrasiewicz 
67200a2430fSAndrzej Pietrasiewicz 	case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
67300a2430fSAndrzej Pietrasiewicz 		  | HID_REQ_GET_PROTOCOL):
67400a2430fSAndrzej Pietrasiewicz 		VDBG(cdev, "get_protocol\n");
675b3c4ec71SAbdulhadi Mohamed 		length = min_t(unsigned int, length, 1);
676b3c4ec71SAbdulhadi Mohamed 		((u8 *) req->buf)[0] = hidg->protocol;
677b3c4ec71SAbdulhadi Mohamed 		goto respond;
67800a2430fSAndrzej Pietrasiewicz 		break;
67900a2430fSAndrzej Pietrasiewicz 
680afcff6dcSMaxim Devaev 	case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
681afcff6dcSMaxim Devaev 		  | HID_REQ_GET_IDLE):
682afcff6dcSMaxim Devaev 		VDBG(cdev, "get_idle\n");
683afcff6dcSMaxim Devaev 		length = min_t(unsigned int, length, 1);
684afcff6dcSMaxim Devaev 		((u8 *) req->buf)[0] = hidg->idle;
685afcff6dcSMaxim Devaev 		goto respond;
686afcff6dcSMaxim Devaev 		break;
687afcff6dcSMaxim Devaev 
68800a2430fSAndrzej Pietrasiewicz 	case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
68900a2430fSAndrzej Pietrasiewicz 		  | HID_REQ_SET_REPORT):
6906774def6SMasanari Iida 		VDBG(cdev, "set_report | wLength=%d\n", ctrl->wLength);
691d7428bc2SMaxim Devaev 		if (hidg->use_out_ep)
69200a2430fSAndrzej Pietrasiewicz 			goto stall;
693d7428bc2SMaxim Devaev 		req->complete = hidg_ssreport_complete;
694d7428bc2SMaxim Devaev 		req->context  = hidg;
695d7428bc2SMaxim Devaev 		goto respond;
69600a2430fSAndrzej Pietrasiewicz 		break;
69700a2430fSAndrzej Pietrasiewicz 
69800a2430fSAndrzej Pietrasiewicz 	case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
69900a2430fSAndrzej Pietrasiewicz 		  | HID_REQ_SET_PROTOCOL):
70000a2430fSAndrzej Pietrasiewicz 		VDBG(cdev, "set_protocol\n");
701b3c4ec71SAbdulhadi Mohamed 		if (value > HID_REPORT_PROTOCOL)
702b3c4ec71SAbdulhadi Mohamed 			goto stall;
703b3c4ec71SAbdulhadi Mohamed 		length = 0;
704b3c4ec71SAbdulhadi Mohamed 		/*
705b3c4ec71SAbdulhadi Mohamed 		 * We assume that programs implementing the Boot protocol
706b3c4ec71SAbdulhadi Mohamed 		 * are also compatible with the Report Protocol
707b3c4ec71SAbdulhadi Mohamed 		 */
708b3c4ec71SAbdulhadi Mohamed 		if (hidg->bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT) {
709b3c4ec71SAbdulhadi Mohamed 			hidg->protocol = value;
710b3c4ec71SAbdulhadi Mohamed 			goto respond;
711b3c4ec71SAbdulhadi Mohamed 		}
71200a2430fSAndrzej Pietrasiewicz 		goto stall;
71300a2430fSAndrzej Pietrasiewicz 		break;
71400a2430fSAndrzej Pietrasiewicz 
715afcff6dcSMaxim Devaev 	case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
716afcff6dcSMaxim Devaev 		  | HID_REQ_SET_IDLE):
717afcff6dcSMaxim Devaev 		VDBG(cdev, "set_idle\n");
718afcff6dcSMaxim Devaev 		length = 0;
719fa20badaSMaxim Devaev 		hidg->idle = value >> 8;
720afcff6dcSMaxim Devaev 		goto respond;
721afcff6dcSMaxim Devaev 		break;
722afcff6dcSMaxim Devaev 
72300a2430fSAndrzej Pietrasiewicz 	case ((USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_INTERFACE) << 8
72400a2430fSAndrzej Pietrasiewicz 		  | USB_REQ_GET_DESCRIPTOR):
72500a2430fSAndrzej Pietrasiewicz 		switch (value >> 8) {
72600a2430fSAndrzej Pietrasiewicz 		case HID_DT_HID:
727f286d487SKrzysztof Opasiak 		{
728f286d487SKrzysztof Opasiak 			struct hid_descriptor hidg_desc_copy = hidg_desc;
729f286d487SKrzysztof Opasiak 
73000a2430fSAndrzej Pietrasiewicz 			VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: HID\n");
731f286d487SKrzysztof Opasiak 			hidg_desc_copy.desc[0].bDescriptorType = HID_DT_REPORT;
732f286d487SKrzysztof Opasiak 			hidg_desc_copy.desc[0].wDescriptorLength =
733f286d487SKrzysztof Opasiak 				cpu_to_le16(hidg->report_desc_length);
734f286d487SKrzysztof Opasiak 
73500a2430fSAndrzej Pietrasiewicz 			length = min_t(unsigned short, length,
736f286d487SKrzysztof Opasiak 						   hidg_desc_copy.bLength);
737f286d487SKrzysztof Opasiak 			memcpy(req->buf, &hidg_desc_copy, length);
73800a2430fSAndrzej Pietrasiewicz 			goto respond;
73900a2430fSAndrzej Pietrasiewicz 			break;
740f286d487SKrzysztof Opasiak 		}
74100a2430fSAndrzej Pietrasiewicz 		case HID_DT_REPORT:
74200a2430fSAndrzej Pietrasiewicz 			VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: REPORT\n");
74300a2430fSAndrzej Pietrasiewicz 			length = min_t(unsigned short, length,
74400a2430fSAndrzej Pietrasiewicz 						   hidg->report_desc_length);
74500a2430fSAndrzej Pietrasiewicz 			memcpy(req->buf, hidg->report_desc, length);
74600a2430fSAndrzej Pietrasiewicz 			goto respond;
74700a2430fSAndrzej Pietrasiewicz 			break;
74800a2430fSAndrzej Pietrasiewicz 
74900a2430fSAndrzej Pietrasiewicz 		default:
75000a2430fSAndrzej Pietrasiewicz 			VDBG(cdev, "Unknown descriptor request 0x%x\n",
75100a2430fSAndrzej Pietrasiewicz 				 value >> 8);
75200a2430fSAndrzej Pietrasiewicz 			goto stall;
75300a2430fSAndrzej Pietrasiewicz 			break;
75400a2430fSAndrzej Pietrasiewicz 		}
75500a2430fSAndrzej Pietrasiewicz 		break;
75600a2430fSAndrzej Pietrasiewicz 
75700a2430fSAndrzej Pietrasiewicz 	default:
75800a2430fSAndrzej Pietrasiewicz 		VDBG(cdev, "Unknown request 0x%x\n",
75900a2430fSAndrzej Pietrasiewicz 			 ctrl->bRequest);
76000a2430fSAndrzej Pietrasiewicz 		goto stall;
76100a2430fSAndrzej Pietrasiewicz 		break;
76200a2430fSAndrzej Pietrasiewicz 	}
76300a2430fSAndrzej Pietrasiewicz 
76400a2430fSAndrzej Pietrasiewicz stall:
76500a2430fSAndrzej Pietrasiewicz 	return -EOPNOTSUPP;
76600a2430fSAndrzej Pietrasiewicz 
76700a2430fSAndrzej Pietrasiewicz respond:
76800a2430fSAndrzej Pietrasiewicz 	req->zero = 0;
76900a2430fSAndrzej Pietrasiewicz 	req->length = length;
77000a2430fSAndrzej Pietrasiewicz 	status = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
77100a2430fSAndrzej Pietrasiewicz 	if (status < 0)
77200a2430fSAndrzej Pietrasiewicz 		ERROR(cdev, "usb_ep_queue error on ep0 %d\n", value);
77300a2430fSAndrzej Pietrasiewicz 	return status;
77400a2430fSAndrzej Pietrasiewicz }
77500a2430fSAndrzej Pietrasiewicz 
hidg_disable(struct usb_function * f)77600a2430fSAndrzej Pietrasiewicz static void hidg_disable(struct usb_function *f)
77700a2430fSAndrzej Pietrasiewicz {
77800a2430fSAndrzej Pietrasiewicz 	struct f_hidg *hidg = func_to_hidg(f);
77900a2430fSAndrzej Pietrasiewicz 	struct f_hidg_req_list *list, *next;
780aa65d11aSKrzysztof Opasiak 	unsigned long flags;
78100a2430fSAndrzej Pietrasiewicz 
78200a2430fSAndrzej Pietrasiewicz 	usb_ep_disable(hidg->in_ep);
783d7428bc2SMaxim Devaev 
784d7428bc2SMaxim Devaev 	if (hidg->out_ep) {
78500a2430fSAndrzej Pietrasiewicz 		usb_ep_disable(hidg->out_ep);
78600a2430fSAndrzej Pietrasiewicz 
78733e4c1a9SKrzysztof Opasiak 		spin_lock_irqsave(&hidg->read_spinlock, flags);
78800a2430fSAndrzej Pietrasiewicz 		list_for_each_entry_safe(list, next, &hidg->completed_out_req, list) {
789aa65d11aSKrzysztof Opasiak 			free_ep_req(hidg->out_ep, list->req);
79000a2430fSAndrzej Pietrasiewicz 			list_del(&list->list);
79100a2430fSAndrzej Pietrasiewicz 			kfree(list);
79200a2430fSAndrzej Pietrasiewicz 		}
79333e4c1a9SKrzysztof Opasiak 		spin_unlock_irqrestore(&hidg->read_spinlock, flags);
794d7428bc2SMaxim Devaev 	}
795749494b6SKrzysztof Opasiak 
796749494b6SKrzysztof Opasiak 	spin_lock_irqsave(&hidg->write_spinlock, flags);
797749494b6SKrzysztof Opasiak 	if (!hidg->write_pending) {
798749494b6SKrzysztof Opasiak 		free_ep_req(hidg->in_ep, hidg->req);
799749494b6SKrzysztof Opasiak 		hidg->write_pending = 1;
800749494b6SKrzysztof Opasiak 	}
801749494b6SKrzysztof Opasiak 
802749494b6SKrzysztof Opasiak 	hidg->req = NULL;
803749494b6SKrzysztof Opasiak 	spin_unlock_irqrestore(&hidg->write_spinlock, flags);
80400a2430fSAndrzej Pietrasiewicz }
80500a2430fSAndrzej Pietrasiewicz 
hidg_set_alt(struct usb_function * f,unsigned intf,unsigned alt)80600a2430fSAndrzej Pietrasiewicz static int hidg_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
80700a2430fSAndrzej Pietrasiewicz {
80800a2430fSAndrzej Pietrasiewicz 	struct usb_composite_dev		*cdev = f->config->cdev;
80900a2430fSAndrzej Pietrasiewicz 	struct f_hidg				*hidg = func_to_hidg(f);
810749494b6SKrzysztof Opasiak 	struct usb_request			*req_in = NULL;
811749494b6SKrzysztof Opasiak 	unsigned long				flags;
81200a2430fSAndrzej Pietrasiewicz 	int i, status = 0;
81300a2430fSAndrzej Pietrasiewicz 
81400a2430fSAndrzej Pietrasiewicz 	VDBG(cdev, "hidg_set_alt intf:%d alt:%d\n", intf, alt);
81500a2430fSAndrzej Pietrasiewicz 
81600a2430fSAndrzej Pietrasiewicz 	if (hidg->in_ep != NULL) {
81700a2430fSAndrzej Pietrasiewicz 		/* restart endpoint */
81800a2430fSAndrzej Pietrasiewicz 		usb_ep_disable(hidg->in_ep);
81900a2430fSAndrzej Pietrasiewicz 
82000a2430fSAndrzej Pietrasiewicz 		status = config_ep_by_speed(f->config->cdev->gadget, f,
82100a2430fSAndrzej Pietrasiewicz 					    hidg->in_ep);
82200a2430fSAndrzej Pietrasiewicz 		if (status) {
82300a2430fSAndrzej Pietrasiewicz 			ERROR(cdev, "config_ep_by_speed FAILED!\n");
82400a2430fSAndrzej Pietrasiewicz 			goto fail;
82500a2430fSAndrzej Pietrasiewicz 		}
82600a2430fSAndrzej Pietrasiewicz 		status = usb_ep_enable(hidg->in_ep);
82700a2430fSAndrzej Pietrasiewicz 		if (status < 0) {
82800a2430fSAndrzej Pietrasiewicz 			ERROR(cdev, "Enable IN endpoint FAILED!\n");
82900a2430fSAndrzej Pietrasiewicz 			goto fail;
83000a2430fSAndrzej Pietrasiewicz 		}
83100a2430fSAndrzej Pietrasiewicz 		hidg->in_ep->driver_data = hidg;
832749494b6SKrzysztof Opasiak 
833749494b6SKrzysztof Opasiak 		req_in = hidg_alloc_ep_req(hidg->in_ep, hidg->report_length);
834749494b6SKrzysztof Opasiak 		if (!req_in) {
835749494b6SKrzysztof Opasiak 			status = -ENOMEM;
836749494b6SKrzysztof Opasiak 			goto disable_ep_in;
837749494b6SKrzysztof Opasiak 		}
83800a2430fSAndrzej Pietrasiewicz 	}
83900a2430fSAndrzej Pietrasiewicz 
840d7428bc2SMaxim Devaev 	if (hidg->use_out_ep && hidg->out_ep != NULL) {
84100a2430fSAndrzej Pietrasiewicz 		/* restart endpoint */
84200a2430fSAndrzej Pietrasiewicz 		usb_ep_disable(hidg->out_ep);
84300a2430fSAndrzej Pietrasiewicz 
84400a2430fSAndrzej Pietrasiewicz 		status = config_ep_by_speed(f->config->cdev->gadget, f,
84500a2430fSAndrzej Pietrasiewicz 					    hidg->out_ep);
84600a2430fSAndrzej Pietrasiewicz 		if (status) {
84700a2430fSAndrzej Pietrasiewicz 			ERROR(cdev, "config_ep_by_speed FAILED!\n");
848749494b6SKrzysztof Opasiak 			goto free_req_in;
84900a2430fSAndrzej Pietrasiewicz 		}
85000a2430fSAndrzej Pietrasiewicz 		status = usb_ep_enable(hidg->out_ep);
85100a2430fSAndrzej Pietrasiewicz 		if (status < 0) {
85243aef5c2SDavid Lechner 			ERROR(cdev, "Enable OUT endpoint FAILED!\n");
853749494b6SKrzysztof Opasiak 			goto free_req_in;
85400a2430fSAndrzej Pietrasiewicz 		}
85500a2430fSAndrzej Pietrasiewicz 		hidg->out_ep->driver_data = hidg;
85600a2430fSAndrzej Pietrasiewicz 
85700a2430fSAndrzej Pietrasiewicz 		/*
85800a2430fSAndrzej Pietrasiewicz 		 * allocate a bunch of read buffers and queue them all at once.
85900a2430fSAndrzej Pietrasiewicz 		 */
86000a2430fSAndrzej Pietrasiewicz 		for (i = 0; i < hidg->qlen && status == 0; i++) {
86100a2430fSAndrzej Pietrasiewicz 			struct usb_request *req =
86200a2430fSAndrzej Pietrasiewicz 					hidg_alloc_ep_req(hidg->out_ep,
86300a2430fSAndrzej Pietrasiewicz 							  hidg->report_length);
86400a2430fSAndrzej Pietrasiewicz 			if (req) {
865d7428bc2SMaxim Devaev 				req->complete = hidg_intout_complete;
86600a2430fSAndrzej Pietrasiewicz 				req->context  = hidg;
86700a2430fSAndrzej Pietrasiewicz 				status = usb_ep_queue(hidg->out_ep, req,
86800a2430fSAndrzej Pietrasiewicz 						      GFP_ATOMIC);
869749494b6SKrzysztof Opasiak 				if (status) {
87000a2430fSAndrzej Pietrasiewicz 					ERROR(cdev, "%s queue req --> %d\n",
87100a2430fSAndrzej Pietrasiewicz 						hidg->out_ep->name, status);
872749494b6SKrzysztof Opasiak 					free_ep_req(hidg->out_ep, req);
873749494b6SKrzysztof Opasiak 				}
87400a2430fSAndrzej Pietrasiewicz 			} else {
87500a2430fSAndrzej Pietrasiewicz 				status = -ENOMEM;
876749494b6SKrzysztof Opasiak 				goto disable_out_ep;
87700a2430fSAndrzej Pietrasiewicz 			}
87800a2430fSAndrzej Pietrasiewicz 		}
87900a2430fSAndrzej Pietrasiewicz 	}
88000a2430fSAndrzej Pietrasiewicz 
881749494b6SKrzysztof Opasiak 	if (hidg->in_ep != NULL) {
882749494b6SKrzysztof Opasiak 		spin_lock_irqsave(&hidg->write_spinlock, flags);
883749494b6SKrzysztof Opasiak 		hidg->req = req_in;
884749494b6SKrzysztof Opasiak 		hidg->write_pending = 0;
885749494b6SKrzysztof Opasiak 		spin_unlock_irqrestore(&hidg->write_spinlock, flags);
886749494b6SKrzysztof Opasiak 
887749494b6SKrzysztof Opasiak 		wake_up(&hidg->write_queue);
888749494b6SKrzysztof Opasiak 	}
889749494b6SKrzysztof Opasiak 	return 0;
890749494b6SKrzysztof Opasiak disable_out_ep:
891d7428bc2SMaxim Devaev 	if (hidg->out_ep)
892749494b6SKrzysztof Opasiak 		usb_ep_disable(hidg->out_ep);
893749494b6SKrzysztof Opasiak free_req_in:
894749494b6SKrzysztof Opasiak 	if (req_in)
895749494b6SKrzysztof Opasiak 		free_ep_req(hidg->in_ep, req_in);
896749494b6SKrzysztof Opasiak 
897749494b6SKrzysztof Opasiak disable_ep_in:
898749494b6SKrzysztof Opasiak 	if (hidg->in_ep)
899749494b6SKrzysztof Opasiak 		usb_ep_disable(hidg->in_ep);
900749494b6SKrzysztof Opasiak 
90100a2430fSAndrzej Pietrasiewicz fail:
90200a2430fSAndrzej Pietrasiewicz 	return status;
90300a2430fSAndrzej Pietrasiewicz }
90400a2430fSAndrzej Pietrasiewicz 
9057a3cc461SLad, Prabhakar static const struct file_operations f_hidg_fops = {
90600a2430fSAndrzej Pietrasiewicz 	.owner		= THIS_MODULE,
90700a2430fSAndrzej Pietrasiewicz 	.open		= f_hidg_open,
90800a2430fSAndrzej Pietrasiewicz 	.release	= f_hidg_release,
90900a2430fSAndrzej Pietrasiewicz 	.write		= f_hidg_write,
91000a2430fSAndrzej Pietrasiewicz 	.read		= f_hidg_read,
91100a2430fSAndrzej Pietrasiewicz 	.poll		= f_hidg_poll,
91200a2430fSAndrzej Pietrasiewicz 	.llseek		= noop_llseek,
91300a2430fSAndrzej Pietrasiewicz };
91400a2430fSAndrzej Pietrasiewicz 
hidg_bind(struct usb_configuration * c,struct usb_function * f)915cb382536SAndrzej Pietrasiewicz static int hidg_bind(struct usb_configuration *c, struct usb_function *f)
91600a2430fSAndrzej Pietrasiewicz {
91700a2430fSAndrzej Pietrasiewicz 	struct usb_ep		*ep;
91800a2430fSAndrzej Pietrasiewicz 	struct f_hidg		*hidg = func_to_hidg(f);
9195ca8d3ecSAndrzej Pietrasiewicz 	struct usb_string	*us;
92000a2430fSAndrzej Pietrasiewicz 	int			status;
92100a2430fSAndrzej Pietrasiewicz 
922cb382536SAndrzej Pietrasiewicz 	/* maybe allocate device-global string IDs, and patch descriptors */
9235ca8d3ecSAndrzej Pietrasiewicz 	us = usb_gstrings_attach(c->cdev, ct_func_strings,
9245ca8d3ecSAndrzej Pietrasiewicz 				 ARRAY_SIZE(ct_func_string_defs));
9255ca8d3ecSAndrzej Pietrasiewicz 	if (IS_ERR(us))
9265ca8d3ecSAndrzej Pietrasiewicz 		return PTR_ERR(us);
9275ca8d3ecSAndrzej Pietrasiewicz 	hidg_interface_desc.iInterface = us[CT_FUNC_HID_IDX].id;
928cb382536SAndrzej Pietrasiewicz 
92900a2430fSAndrzej Pietrasiewicz 	/* allocate instance-specific interface IDs, and patch descriptors */
93000a2430fSAndrzej Pietrasiewicz 	status = usb_interface_id(c, f);
93100a2430fSAndrzej Pietrasiewicz 	if (status < 0)
93200a2430fSAndrzej Pietrasiewicz 		goto fail;
93300a2430fSAndrzej Pietrasiewicz 	hidg_interface_desc.bInterfaceNumber = status;
93400a2430fSAndrzej Pietrasiewicz 
93500a2430fSAndrzej Pietrasiewicz 	/* allocate instance-specific endpoints */
93600a2430fSAndrzej Pietrasiewicz 	status = -ENODEV;
93700a2430fSAndrzej Pietrasiewicz 	ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_in_ep_desc);
93800a2430fSAndrzej Pietrasiewicz 	if (!ep)
93900a2430fSAndrzej Pietrasiewicz 		goto fail;
94000a2430fSAndrzej Pietrasiewicz 	hidg->in_ep = ep;
94100a2430fSAndrzej Pietrasiewicz 
942d7428bc2SMaxim Devaev 	hidg->out_ep = NULL;
943d7428bc2SMaxim Devaev 	if (hidg->use_out_ep) {
94400a2430fSAndrzej Pietrasiewicz 		ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_out_ep_desc);
94500a2430fSAndrzej Pietrasiewicz 		if (!ep)
94600a2430fSAndrzej Pietrasiewicz 			goto fail;
94700a2430fSAndrzej Pietrasiewicz 		hidg->out_ep = ep;
948d7428bc2SMaxim Devaev 	}
949d7428bc2SMaxim Devaev 
950d7428bc2SMaxim Devaev 	/* used only if use_out_ep == 1 */
951d7428bc2SMaxim Devaev 	hidg->set_report_buf = NULL;
95200a2430fSAndrzej Pietrasiewicz 
95300a2430fSAndrzej Pietrasiewicz 	/* set descriptor dynamic values */
95400a2430fSAndrzej Pietrasiewicz 	hidg_interface_desc.bInterfaceSubClass = hidg->bInterfaceSubClass;
95500a2430fSAndrzej Pietrasiewicz 	hidg_interface_desc.bInterfaceProtocol = hidg->bInterfaceProtocol;
956d7428bc2SMaxim Devaev 	hidg_interface_desc.bNumEndpoints = hidg->use_out_ep ? 2 : 1;
957b3c4ec71SAbdulhadi Mohamed 	hidg->protocol = HID_REPORT_PROTOCOL;
958afcff6dcSMaxim Devaev 	hidg->idle = 1;
959dbf499cfSJanusz Dziedzic 	hidg_ss_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
960dbf499cfSJanusz Dziedzic 	hidg_ss_in_comp_desc.wBytesPerInterval =
961dbf499cfSJanusz Dziedzic 				cpu_to_le16(hidg->report_length);
96200a2430fSAndrzej Pietrasiewicz 	hidg_hs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
96300a2430fSAndrzej Pietrasiewicz 	hidg_fs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
964dbf499cfSJanusz Dziedzic 	hidg_ss_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
965dbf499cfSJanusz Dziedzic 	hidg_ss_out_comp_desc.wBytesPerInterval =
966dbf499cfSJanusz Dziedzic 				cpu_to_le16(hidg->report_length);
96700a2430fSAndrzej Pietrasiewicz 	hidg_hs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
96800a2430fSAndrzej Pietrasiewicz 	hidg_fs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
969f286d487SKrzysztof Opasiak 	/*
970f286d487SKrzysztof Opasiak 	 * We can use hidg_desc struct here but we should not relay
971f286d487SKrzysztof Opasiak 	 * that its content won't change after returning from this function.
972f286d487SKrzysztof Opasiak 	 */
97300a2430fSAndrzej Pietrasiewicz 	hidg_desc.desc[0].bDescriptorType = HID_DT_REPORT;
97400a2430fSAndrzej Pietrasiewicz 	hidg_desc.desc[0].wDescriptorLength =
97500a2430fSAndrzej Pietrasiewicz 		cpu_to_le16(hidg->report_desc_length);
97600a2430fSAndrzej Pietrasiewicz 
97700a2430fSAndrzej Pietrasiewicz 	hidg_hs_in_ep_desc.bEndpointAddress =
97800a2430fSAndrzej Pietrasiewicz 		hidg_fs_in_ep_desc.bEndpointAddress;
97900a2430fSAndrzej Pietrasiewicz 	hidg_hs_out_ep_desc.bEndpointAddress =
98000a2430fSAndrzej Pietrasiewicz 		hidg_fs_out_ep_desc.bEndpointAddress;
98100a2430fSAndrzej Pietrasiewicz 
982dbf499cfSJanusz Dziedzic 	hidg_ss_in_ep_desc.bEndpointAddress =
983dbf499cfSJanusz Dziedzic 		hidg_fs_in_ep_desc.bEndpointAddress;
984dbf499cfSJanusz Dziedzic 	hidg_ss_out_ep_desc.bEndpointAddress =
985dbf499cfSJanusz Dziedzic 		hidg_fs_out_ep_desc.bEndpointAddress;
986dbf499cfSJanusz Dziedzic 
987d7428bc2SMaxim Devaev 	if (hidg->use_out_ep)
988d7428bc2SMaxim Devaev 		status = usb_assign_descriptors(f,
989d7428bc2SMaxim Devaev 			hidg_fs_descriptors_intout,
990d7428bc2SMaxim Devaev 			hidg_hs_descriptors_intout,
991d7428bc2SMaxim Devaev 			hidg_ss_descriptors_intout,
992d7428bc2SMaxim Devaev 			hidg_ss_descriptors_intout);
993d7428bc2SMaxim Devaev 	else
994d7428bc2SMaxim Devaev 		status = usb_assign_descriptors(f,
995d7428bc2SMaxim Devaev 			hidg_fs_descriptors_ssreport,
996d7428bc2SMaxim Devaev 			hidg_hs_descriptors_ssreport,
997d7428bc2SMaxim Devaev 			hidg_ss_descriptors_ssreport,
998d7428bc2SMaxim Devaev 			hidg_ss_descriptors_ssreport);
999d7428bc2SMaxim Devaev 
100000a2430fSAndrzej Pietrasiewicz 	if (status)
100100a2430fSAndrzej Pietrasiewicz 		goto fail;
100200a2430fSAndrzej Pietrasiewicz 
100333e4c1a9SKrzysztof Opasiak 	spin_lock_init(&hidg->write_spinlock);
1004749494b6SKrzysztof Opasiak 	hidg->write_pending = 1;
1005749494b6SKrzysztof Opasiak 	hidg->req = NULL;
100633e4c1a9SKrzysztof Opasiak 	spin_lock_init(&hidg->read_spinlock);
100700a2430fSAndrzej Pietrasiewicz 	init_waitqueue_head(&hidg->write_queue);
100800a2430fSAndrzej Pietrasiewicz 	init_waitqueue_head(&hidg->read_queue);
100900a2430fSAndrzej Pietrasiewicz 	INIT_LIST_HEAD(&hidg->completed_out_req);
101000a2430fSAndrzej Pietrasiewicz 
101100a2430fSAndrzej Pietrasiewicz 	/* create char device */
101200a2430fSAndrzej Pietrasiewicz 	cdev_init(&hidg->cdev, &f_hidg_fops);
101389ff3dfaSJohn Keeping 	status = cdev_device_add(&hidg->cdev, &hidg->dev);
101400a2430fSAndrzej Pietrasiewicz 	if (status)
1015d12a8727SPavitrakumar Managutte 		goto fail_free_descs;
101600a2430fSAndrzej Pietrasiewicz 
101700a2430fSAndrzej Pietrasiewicz 	return 0;
1018d12a8727SPavitrakumar Managutte fail_free_descs:
1019d12a8727SPavitrakumar Managutte 	usb_free_all_descriptors(f);
102000a2430fSAndrzej Pietrasiewicz fail:
102100a2430fSAndrzej Pietrasiewicz 	ERROR(f->config->cdev, "hidg_bind FAILED\n");
102214794d71SFelipe F. Tonello 	if (hidg->req != NULL)
102314794d71SFelipe F. Tonello 		free_ep_req(hidg->in_ep, hidg->req);
102400a2430fSAndrzej Pietrasiewicz 
102500a2430fSAndrzej Pietrasiewicz 	return status;
102600a2430fSAndrzej Pietrasiewicz }
102700a2430fSAndrzej Pietrasiewicz 
hidg_get_minor(void)1028cb382536SAndrzej Pietrasiewicz static inline int hidg_get_minor(void)
1029cb382536SAndrzej Pietrasiewicz {
1030cb382536SAndrzej Pietrasiewicz 	int ret;
1031cb382536SAndrzej Pietrasiewicz 
1032*a94a5600SChristophe JAILLET 	ret = ida_alloc(&hidg_ida, GFP_KERNEL);
1033774cf72fSAndrzej Pietrasiewicz 	if (ret >= HIDG_MINORS) {
1034*a94a5600SChristophe JAILLET 		ida_free(&hidg_ida, ret);
1035774cf72fSAndrzej Pietrasiewicz 		ret = -ENODEV;
1036774cf72fSAndrzej Pietrasiewicz 	}
1037cb382536SAndrzej Pietrasiewicz 
1038cb382536SAndrzej Pietrasiewicz 	return ret;
1039cb382536SAndrzej Pietrasiewicz }
1040cb382536SAndrzej Pietrasiewicz 
to_f_hid_opts(struct config_item * item)104121a9476aSAndrzej Pietrasiewicz static inline struct f_hid_opts *to_f_hid_opts(struct config_item *item)
104221a9476aSAndrzej Pietrasiewicz {
104321a9476aSAndrzej Pietrasiewicz 	return container_of(to_config_group(item), struct f_hid_opts,
104421a9476aSAndrzej Pietrasiewicz 			    func_inst.group);
104521a9476aSAndrzej Pietrasiewicz }
104621a9476aSAndrzej Pietrasiewicz 
hid_attr_release(struct config_item * item)104721a9476aSAndrzej Pietrasiewicz static void hid_attr_release(struct config_item *item)
104821a9476aSAndrzej Pietrasiewicz {
104921a9476aSAndrzej Pietrasiewicz 	struct f_hid_opts *opts = to_f_hid_opts(item);
105021a9476aSAndrzej Pietrasiewicz 
105121a9476aSAndrzej Pietrasiewicz 	usb_put_function_instance(&opts->func_inst);
105221a9476aSAndrzej Pietrasiewicz }
105321a9476aSAndrzej Pietrasiewicz 
105421a9476aSAndrzej Pietrasiewicz static struct configfs_item_operations hidg_item_ops = {
105521a9476aSAndrzej Pietrasiewicz 	.release	= hid_attr_release,
105621a9476aSAndrzej Pietrasiewicz };
105721a9476aSAndrzej Pietrasiewicz 
105821a9476aSAndrzej Pietrasiewicz #define F_HID_OPT(name, prec, limit)					\
1059da4e527cSChristoph Hellwig static ssize_t f_hid_opts_##name##_show(struct config_item *item, char *page)\
106021a9476aSAndrzej Pietrasiewicz {									\
1061da4e527cSChristoph Hellwig 	struct f_hid_opts *opts = to_f_hid_opts(item);			\
106221a9476aSAndrzej Pietrasiewicz 	int result;							\
106321a9476aSAndrzej Pietrasiewicz 									\
106421a9476aSAndrzej Pietrasiewicz 	mutex_lock(&opts->lock);					\
106521a9476aSAndrzej Pietrasiewicz 	result = sprintf(page, "%d\n", opts->name);			\
106621a9476aSAndrzej Pietrasiewicz 	mutex_unlock(&opts->lock);					\
106721a9476aSAndrzej Pietrasiewicz 									\
106821a9476aSAndrzej Pietrasiewicz 	return result;							\
106921a9476aSAndrzej Pietrasiewicz }									\
107021a9476aSAndrzej Pietrasiewicz 									\
1071da4e527cSChristoph Hellwig static ssize_t f_hid_opts_##name##_store(struct config_item *item,	\
107221a9476aSAndrzej Pietrasiewicz 					 const char *page, size_t len)	\
107321a9476aSAndrzej Pietrasiewicz {									\
1074da4e527cSChristoph Hellwig 	struct f_hid_opts *opts = to_f_hid_opts(item);			\
107521a9476aSAndrzej Pietrasiewicz 	int ret;							\
107621a9476aSAndrzej Pietrasiewicz 	u##prec num;							\
107721a9476aSAndrzej Pietrasiewicz 									\
107821a9476aSAndrzej Pietrasiewicz 	mutex_lock(&opts->lock);					\
107921a9476aSAndrzej Pietrasiewicz 	if (opts->refcnt) {						\
108021a9476aSAndrzej Pietrasiewicz 		ret = -EBUSY;						\
108121a9476aSAndrzej Pietrasiewicz 		goto end;						\
108221a9476aSAndrzej Pietrasiewicz 	}								\
108321a9476aSAndrzej Pietrasiewicz 									\
108421a9476aSAndrzej Pietrasiewicz 	ret = kstrtou##prec(page, 0, &num);				\
108521a9476aSAndrzej Pietrasiewicz 	if (ret)							\
108621a9476aSAndrzej Pietrasiewicz 		goto end;						\
108721a9476aSAndrzej Pietrasiewicz 									\
108821a9476aSAndrzej Pietrasiewicz 	if (num > limit) {						\
108921a9476aSAndrzej Pietrasiewicz 		ret = -EINVAL;						\
109021a9476aSAndrzej Pietrasiewicz 		goto end;						\
109121a9476aSAndrzej Pietrasiewicz 	}								\
109221a9476aSAndrzej Pietrasiewicz 	opts->name = num;						\
109321a9476aSAndrzej Pietrasiewicz 	ret = len;							\
109421a9476aSAndrzej Pietrasiewicz 									\
109521a9476aSAndrzej Pietrasiewicz end:									\
109621a9476aSAndrzej Pietrasiewicz 	mutex_unlock(&opts->lock);					\
109721a9476aSAndrzej Pietrasiewicz 	return ret;							\
109821a9476aSAndrzej Pietrasiewicz }									\
109921a9476aSAndrzej Pietrasiewicz 									\
1100da4e527cSChristoph Hellwig CONFIGFS_ATTR(f_hid_opts_, name)
110121a9476aSAndrzej Pietrasiewicz 
110221a9476aSAndrzej Pietrasiewicz F_HID_OPT(subclass, 8, 255);
110321a9476aSAndrzej Pietrasiewicz F_HID_OPT(protocol, 8, 255);
1104d7428bc2SMaxim Devaev F_HID_OPT(no_out_endpoint, 8, 1);
110539a2ac27SAndrzej Pietrasiewicz F_HID_OPT(report_length, 16, 65535);
110621a9476aSAndrzej Pietrasiewicz 
f_hid_opts_report_desc_show(struct config_item * item,char * page)1107da4e527cSChristoph Hellwig static ssize_t f_hid_opts_report_desc_show(struct config_item *item, char *page)
110821a9476aSAndrzej Pietrasiewicz {
1109da4e527cSChristoph Hellwig 	struct f_hid_opts *opts = to_f_hid_opts(item);
111021a9476aSAndrzej Pietrasiewicz 	int result;
111121a9476aSAndrzej Pietrasiewicz 
111221a9476aSAndrzej Pietrasiewicz 	mutex_lock(&opts->lock);
111321a9476aSAndrzej Pietrasiewicz 	result = opts->report_desc_length;
111421a9476aSAndrzej Pietrasiewicz 	memcpy(page, opts->report_desc, opts->report_desc_length);
111521a9476aSAndrzej Pietrasiewicz 	mutex_unlock(&opts->lock);
111621a9476aSAndrzej Pietrasiewicz 
111721a9476aSAndrzej Pietrasiewicz 	return result;
111821a9476aSAndrzej Pietrasiewicz }
111921a9476aSAndrzej Pietrasiewicz 
f_hid_opts_report_desc_store(struct config_item * item,const char * page,size_t len)1120da4e527cSChristoph Hellwig static ssize_t f_hid_opts_report_desc_store(struct config_item *item,
112121a9476aSAndrzej Pietrasiewicz 					    const char *page, size_t len)
112221a9476aSAndrzej Pietrasiewicz {
1123da4e527cSChristoph Hellwig 	struct f_hid_opts *opts = to_f_hid_opts(item);
112421a9476aSAndrzej Pietrasiewicz 	int ret = -EBUSY;
112521a9476aSAndrzej Pietrasiewicz 	char *d;
112621a9476aSAndrzej Pietrasiewicz 
112721a9476aSAndrzej Pietrasiewicz 	mutex_lock(&opts->lock);
112821a9476aSAndrzej Pietrasiewicz 
112921a9476aSAndrzej Pietrasiewicz 	if (opts->refcnt)
113021a9476aSAndrzej Pietrasiewicz 		goto end;
113121a9476aSAndrzej Pietrasiewicz 	if (len > PAGE_SIZE) {
113221a9476aSAndrzej Pietrasiewicz 		ret = -ENOSPC;
113321a9476aSAndrzej Pietrasiewicz 		goto end;
113421a9476aSAndrzej Pietrasiewicz 	}
113521a9476aSAndrzej Pietrasiewicz 	d = kmemdup(page, len, GFP_KERNEL);
113621a9476aSAndrzej Pietrasiewicz 	if (!d) {
113721a9476aSAndrzej Pietrasiewicz 		ret = -ENOMEM;
113821a9476aSAndrzej Pietrasiewicz 		goto end;
113921a9476aSAndrzej Pietrasiewicz 	}
114021a9476aSAndrzej Pietrasiewicz 	kfree(opts->report_desc);
114121a9476aSAndrzej Pietrasiewicz 	opts->report_desc = d;
114221a9476aSAndrzej Pietrasiewicz 	opts->report_desc_length = len;
114321a9476aSAndrzej Pietrasiewicz 	opts->report_desc_alloc = true;
114421a9476aSAndrzej Pietrasiewicz 	ret = len;
114521a9476aSAndrzej Pietrasiewicz end:
114621a9476aSAndrzej Pietrasiewicz 	mutex_unlock(&opts->lock);
114721a9476aSAndrzej Pietrasiewicz 	return ret;
114821a9476aSAndrzej Pietrasiewicz }
114921a9476aSAndrzej Pietrasiewicz 
1150da4e527cSChristoph Hellwig CONFIGFS_ATTR(f_hid_opts_, report_desc);
115121a9476aSAndrzej Pietrasiewicz 
f_hid_opts_dev_show(struct config_item * item,char * page)1152ed6fe1f5SJohannes Berg static ssize_t f_hid_opts_dev_show(struct config_item *item, char *page)
1153ed6fe1f5SJohannes Berg {
1154ed6fe1f5SJohannes Berg 	struct f_hid_opts *opts = to_f_hid_opts(item);
1155ed6fe1f5SJohannes Berg 
1156ed6fe1f5SJohannes Berg 	return sprintf(page, "%d:%d\n", major, opts->minor);
1157ed6fe1f5SJohannes Berg }
1158ed6fe1f5SJohannes Berg 
1159ed6fe1f5SJohannes Berg CONFIGFS_ATTR_RO(f_hid_opts_, dev);
1160ed6fe1f5SJohannes Berg 
116121a9476aSAndrzej Pietrasiewicz static struct configfs_attribute *hid_attrs[] = {
1162da4e527cSChristoph Hellwig 	&f_hid_opts_attr_subclass,
1163da4e527cSChristoph Hellwig 	&f_hid_opts_attr_protocol,
1164d7428bc2SMaxim Devaev 	&f_hid_opts_attr_no_out_endpoint,
1165da4e527cSChristoph Hellwig 	&f_hid_opts_attr_report_length,
1166da4e527cSChristoph Hellwig 	&f_hid_opts_attr_report_desc,
1167ed6fe1f5SJohannes Berg 	&f_hid_opts_attr_dev,
116821a9476aSAndrzej Pietrasiewicz 	NULL,
116921a9476aSAndrzej Pietrasiewicz };
117021a9476aSAndrzej Pietrasiewicz 
117197363902SBhumika Goyal static const struct config_item_type hid_func_type = {
117221a9476aSAndrzej Pietrasiewicz 	.ct_item_ops	= &hidg_item_ops,
117321a9476aSAndrzej Pietrasiewicz 	.ct_attrs	= hid_attrs,
117421a9476aSAndrzej Pietrasiewicz 	.ct_owner	= THIS_MODULE,
117521a9476aSAndrzej Pietrasiewicz };
117621a9476aSAndrzej Pietrasiewicz 
hidg_put_minor(int minor)1177cb382536SAndrzej Pietrasiewicz static inline void hidg_put_minor(int minor)
1178cb382536SAndrzej Pietrasiewicz {
1179*a94a5600SChristophe JAILLET 	ida_free(&hidg_ida, minor);
1180cb382536SAndrzej Pietrasiewicz }
1181cb382536SAndrzej Pietrasiewicz 
hidg_free_inst(struct usb_function_instance * f)1182cb382536SAndrzej Pietrasiewicz static void hidg_free_inst(struct usb_function_instance *f)
1183cb382536SAndrzej Pietrasiewicz {
1184cb382536SAndrzej Pietrasiewicz 	struct f_hid_opts *opts;
1185cb382536SAndrzej Pietrasiewicz 
1186cb382536SAndrzej Pietrasiewicz 	opts = container_of(f, struct f_hid_opts, func_inst);
1187cb382536SAndrzej Pietrasiewicz 
1188cb382536SAndrzej Pietrasiewicz 	mutex_lock(&hidg_ida_lock);
1189cb382536SAndrzej Pietrasiewicz 
1190cb382536SAndrzej Pietrasiewicz 	hidg_put_minor(opts->minor);
119199c49407SMatthew Wilcox 	if (ida_is_empty(&hidg_ida))
1192cb382536SAndrzej Pietrasiewicz 		ghid_cleanup();
1193cb382536SAndrzej Pietrasiewicz 
1194cb382536SAndrzej Pietrasiewicz 	mutex_unlock(&hidg_ida_lock);
1195cb382536SAndrzej Pietrasiewicz 
1196cb382536SAndrzej Pietrasiewicz 	if (opts->report_desc_alloc)
1197cb382536SAndrzej Pietrasiewicz 		kfree(opts->report_desc);
1198cb382536SAndrzej Pietrasiewicz 
1199cb382536SAndrzej Pietrasiewicz 	kfree(opts);
1200cb382536SAndrzej Pietrasiewicz }
1201cb382536SAndrzej Pietrasiewicz 
hidg_alloc_inst(void)1202cb382536SAndrzej Pietrasiewicz static struct usb_function_instance *hidg_alloc_inst(void)
1203cb382536SAndrzej Pietrasiewicz {
1204cb382536SAndrzej Pietrasiewicz 	struct f_hid_opts *opts;
1205cb382536SAndrzej Pietrasiewicz 	struct usb_function_instance *ret;
1206cb382536SAndrzej Pietrasiewicz 	int status = 0;
1207cb382536SAndrzej Pietrasiewicz 
1208cb382536SAndrzej Pietrasiewicz 	opts = kzalloc(sizeof(*opts), GFP_KERNEL);
1209cb382536SAndrzej Pietrasiewicz 	if (!opts)
1210cb382536SAndrzej Pietrasiewicz 		return ERR_PTR(-ENOMEM);
121121a9476aSAndrzej Pietrasiewicz 	mutex_init(&opts->lock);
1212cb382536SAndrzej Pietrasiewicz 	opts->func_inst.free_func_inst = hidg_free_inst;
1213cb382536SAndrzej Pietrasiewicz 	ret = &opts->func_inst;
1214cb382536SAndrzej Pietrasiewicz 
1215cb382536SAndrzej Pietrasiewicz 	mutex_lock(&hidg_ida_lock);
1216cb382536SAndrzej Pietrasiewicz 
121799c49407SMatthew Wilcox 	if (ida_is_empty(&hidg_ida)) {
1218cb382536SAndrzej Pietrasiewicz 		status = ghid_setup(NULL, HIDG_MINORS);
1219cb382536SAndrzej Pietrasiewicz 		if (status)  {
1220cb382536SAndrzej Pietrasiewicz 			ret = ERR_PTR(status);
1221cb382536SAndrzej Pietrasiewicz 			kfree(opts);
1222cb382536SAndrzej Pietrasiewicz 			goto unlock;
1223cb382536SAndrzej Pietrasiewicz 		}
1224cb382536SAndrzej Pietrasiewicz 	}
1225cb382536SAndrzej Pietrasiewicz 
1226cb382536SAndrzej Pietrasiewicz 	opts->minor = hidg_get_minor();
1227cb382536SAndrzej Pietrasiewicz 	if (opts->minor < 0) {
1228cb382536SAndrzej Pietrasiewicz 		ret = ERR_PTR(opts->minor);
1229cb382536SAndrzej Pietrasiewicz 		kfree(opts);
123099c49407SMatthew Wilcox 		if (ida_is_empty(&hidg_ida))
1231cb382536SAndrzej Pietrasiewicz 			ghid_cleanup();
1232828f6148SDan Carpenter 		goto unlock;
1233cb382536SAndrzej Pietrasiewicz 	}
123421a9476aSAndrzej Pietrasiewicz 	config_group_init_type_name(&opts->func_inst.group, "", &hid_func_type);
1235cb382536SAndrzej Pietrasiewicz 
1236cb382536SAndrzej Pietrasiewicz unlock:
1237cb382536SAndrzej Pietrasiewicz 	mutex_unlock(&hidg_ida_lock);
1238cb382536SAndrzej Pietrasiewicz 	return ret;
1239cb382536SAndrzej Pietrasiewicz }
1240cb382536SAndrzej Pietrasiewicz 
hidg_free(struct usb_function * f)1241cb382536SAndrzej Pietrasiewicz static void hidg_free(struct usb_function *f)
1242cb382536SAndrzej Pietrasiewicz {
1243cb382536SAndrzej Pietrasiewicz 	struct f_hidg *hidg;
124421a9476aSAndrzej Pietrasiewicz 	struct f_hid_opts *opts;
1245cb382536SAndrzej Pietrasiewicz 
1246cb382536SAndrzej Pietrasiewicz 	hidg = func_to_hidg(f);
124721a9476aSAndrzej Pietrasiewicz 	opts = container_of(f->fi, struct f_hid_opts, func_inst);
124889ff3dfaSJohn Keeping 	put_device(&hidg->dev);
124921a9476aSAndrzej Pietrasiewicz 	mutex_lock(&opts->lock);
125021a9476aSAndrzej Pietrasiewicz 	--opts->refcnt;
125121a9476aSAndrzej Pietrasiewicz 	mutex_unlock(&opts->lock);
1252cb382536SAndrzej Pietrasiewicz }
1253cb382536SAndrzej Pietrasiewicz 
hidg_unbind(struct usb_configuration * c,struct usb_function * f)125400a2430fSAndrzej Pietrasiewicz static void hidg_unbind(struct usb_configuration *c, struct usb_function *f)
125500a2430fSAndrzej Pietrasiewicz {
125600a2430fSAndrzej Pietrasiewicz 	struct f_hidg *hidg = func_to_hidg(f);
125700a2430fSAndrzej Pietrasiewicz 
125889ff3dfaSJohn Keeping 	cdev_device_del(&hidg->cdev, &hidg->dev);
125900a2430fSAndrzej Pietrasiewicz 
126000a2430fSAndrzej Pietrasiewicz 	usb_free_all_descriptors(f);
126100a2430fSAndrzej Pietrasiewicz }
126200a2430fSAndrzej Pietrasiewicz 
hidg_alloc(struct usb_function_instance * fi)12630fc57ea0SFengguang Wu static struct usb_function *hidg_alloc(struct usb_function_instance *fi)
126400a2430fSAndrzej Pietrasiewicz {
126500a2430fSAndrzej Pietrasiewicz 	struct f_hidg *hidg;
1266cb382536SAndrzej Pietrasiewicz 	struct f_hid_opts *opts;
126789ff3dfaSJohn Keeping 	int ret;
126800a2430fSAndrzej Pietrasiewicz 
126900a2430fSAndrzej Pietrasiewicz 	/* allocate and initialize one new instance */
1270cb382536SAndrzej Pietrasiewicz 	hidg = kzalloc(sizeof(*hidg), GFP_KERNEL);
127100a2430fSAndrzej Pietrasiewicz 	if (!hidg)
1272cb382536SAndrzej Pietrasiewicz 		return ERR_PTR(-ENOMEM);
127300a2430fSAndrzej Pietrasiewicz 
1274cb382536SAndrzej Pietrasiewicz 	opts = container_of(fi, struct f_hid_opts, func_inst);
1275cb382536SAndrzej Pietrasiewicz 
127621a9476aSAndrzej Pietrasiewicz 	mutex_lock(&opts->lock);
127721a9476aSAndrzej Pietrasiewicz 
127889ff3dfaSJohn Keeping 	device_initialize(&hidg->dev);
127989ff3dfaSJohn Keeping 	hidg->dev.release = hidg_release;
128099f2d956SIvan Orlov 	hidg->dev.class = &hidg_class;
128189ff3dfaSJohn Keeping 	hidg->dev.devt = MKDEV(major, opts->minor);
128289ff3dfaSJohn Keeping 	ret = dev_set_name(&hidg->dev, "hidg%d", opts->minor);
1283944fe915SJohn Keeping 	if (ret)
1284944fe915SJohn Keeping 		goto err_unlock;
128589ff3dfaSJohn Keeping 
1286cb382536SAndrzej Pietrasiewicz 	hidg->bInterfaceSubClass = opts->subclass;
1287cb382536SAndrzej Pietrasiewicz 	hidg->bInterfaceProtocol = opts->protocol;
1288cb382536SAndrzej Pietrasiewicz 	hidg->report_length = opts->report_length;
1289cb382536SAndrzej Pietrasiewicz 	hidg->report_desc_length = opts->report_desc_length;
1290cb382536SAndrzej Pietrasiewicz 	if (opts->report_desc) {
1291d9828961SKonstantin Aladyshev 		hidg->report_desc = kmemdup(opts->report_desc,
1292cb382536SAndrzej Pietrasiewicz 					    opts->report_desc_length,
129300a2430fSAndrzej Pietrasiewicz 					    GFP_KERNEL);
129400a2430fSAndrzej Pietrasiewicz 		if (!hidg->report_desc) {
1295944fe915SJohn Keeping 			ret = -ENOMEM;
1296944fe915SJohn Keeping 			goto err_put_device;
1297cb382536SAndrzej Pietrasiewicz 		}
129800a2430fSAndrzej Pietrasiewicz 	}
1299d7428bc2SMaxim Devaev 	hidg->use_out_ep = !opts->no_out_endpoint;
130000a2430fSAndrzej Pietrasiewicz 
1301944fe915SJohn Keeping 	++opts->refcnt;
130221a9476aSAndrzej Pietrasiewicz 	mutex_unlock(&opts->lock);
130321a9476aSAndrzej Pietrasiewicz 
130400a2430fSAndrzej Pietrasiewicz 	hidg->func.name    = "hid";
130500a2430fSAndrzej Pietrasiewicz 	hidg->func.bind    = hidg_bind;
130600a2430fSAndrzej Pietrasiewicz 	hidg->func.unbind  = hidg_unbind;
130700a2430fSAndrzej Pietrasiewicz 	hidg->func.set_alt = hidg_set_alt;
130800a2430fSAndrzej Pietrasiewicz 	hidg->func.disable = hidg_disable;
130900a2430fSAndrzej Pietrasiewicz 	hidg->func.setup   = hidg_setup;
1310cb382536SAndrzej Pietrasiewicz 	hidg->func.free_func = hidg_free;
131100a2430fSAndrzej Pietrasiewicz 
131229a812e4SWei Ming Chen 	/* this could be made configurable at some point */
131300a2430fSAndrzej Pietrasiewicz 	hidg->qlen	   = 4;
131400a2430fSAndrzej Pietrasiewicz 
1315cb382536SAndrzej Pietrasiewicz 	return &hidg->func;
1316944fe915SJohn Keeping 
1317944fe915SJohn Keeping err_put_device:
1318944fe915SJohn Keeping 	put_device(&hidg->dev);
1319944fe915SJohn Keeping err_unlock:
1320944fe915SJohn Keeping 	mutex_unlock(&opts->lock);
1321944fe915SJohn Keeping 	return ERR_PTR(ret);
132200a2430fSAndrzej Pietrasiewicz }
132300a2430fSAndrzej Pietrasiewicz 
1324cb382536SAndrzej Pietrasiewicz DECLARE_USB_FUNCTION_INIT(hid, hidg_alloc_inst, hidg_alloc);
1325cb382536SAndrzej Pietrasiewicz MODULE_LICENSE("GPL");
1326cb382536SAndrzej Pietrasiewicz MODULE_AUTHOR("Fabien Chouteau");
1327cb382536SAndrzej Pietrasiewicz 
ghid_setup(struct usb_gadget * g,int count)1328cb382536SAndrzej Pietrasiewicz int ghid_setup(struct usb_gadget *g, int count)
132900a2430fSAndrzej Pietrasiewicz {
133000a2430fSAndrzej Pietrasiewicz 	int status;
133100a2430fSAndrzej Pietrasiewicz 	dev_t dev;
133200a2430fSAndrzej Pietrasiewicz 
133399f2d956SIvan Orlov 	status = class_register(&hidg_class);
133499f2d956SIvan Orlov 	if (status)
13350448d38cSDan Carpenter 		return status;
133600a2430fSAndrzej Pietrasiewicz 
133700a2430fSAndrzej Pietrasiewicz 	status = alloc_chrdev_region(&dev, 0, count, "hidg");
13380448d38cSDan Carpenter 	if (status) {
133999f2d956SIvan Orlov 		class_unregister(&hidg_class);
134000a2430fSAndrzej Pietrasiewicz 		return status;
134100a2430fSAndrzej Pietrasiewicz 	}
134200a2430fSAndrzej Pietrasiewicz 
13430448d38cSDan Carpenter 	major = MAJOR(dev);
13440448d38cSDan Carpenter 	minors = count;
13450448d38cSDan Carpenter 
13460448d38cSDan Carpenter 	return 0;
134700a2430fSAndrzej Pietrasiewicz }
134800a2430fSAndrzej Pietrasiewicz 
ghid_cleanup(void)134900a2430fSAndrzej Pietrasiewicz void ghid_cleanup(void)
135000a2430fSAndrzej Pietrasiewicz {
135100a2430fSAndrzej Pietrasiewicz 	if (major) {
135200a2430fSAndrzej Pietrasiewicz 		unregister_chrdev_region(MKDEV(major, 0), minors);
135300a2430fSAndrzej Pietrasiewicz 		major = minors = 0;
135400a2430fSAndrzej Pietrasiewicz 	}
135500a2430fSAndrzej Pietrasiewicz 
135699f2d956SIvan Orlov 	class_unregister(&hidg_class);
135700a2430fSAndrzej Pietrasiewicz }
1358