xref: /openbmc/linux/drivers/usb/gadget/function/f_hid.c (revision 89ff3dfac604614287ad5aad9370c3f984ea3f4b)
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;
2600a2430fSAndrzej Pietrasiewicz static struct class *hidg_class;
27cb382536SAndrzej Pietrasiewicz static DEFINE_IDA(hidg_ida);
28cb382536SAndrzej Pietrasiewicz static DEFINE_MUTEX(hidg_ida_lock); /* protects access to hidg_ida */
2900a2430fSAndrzej Pietrasiewicz 
3000a2430fSAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/
3100a2430fSAndrzej Pietrasiewicz /*                            HID gadget struct                            */
3200a2430fSAndrzej Pietrasiewicz 
3300a2430fSAndrzej Pietrasiewicz struct f_hidg_req_list {
3400a2430fSAndrzej Pietrasiewicz 	struct usb_request	*req;
3500a2430fSAndrzej Pietrasiewicz 	unsigned int		pos;
3600a2430fSAndrzej Pietrasiewicz 	struct list_head 	list;
3700a2430fSAndrzej Pietrasiewicz };
3800a2430fSAndrzej Pietrasiewicz 
3900a2430fSAndrzej Pietrasiewicz struct f_hidg {
4000a2430fSAndrzej Pietrasiewicz 	/* configuration */
4100a2430fSAndrzej Pietrasiewicz 	unsigned char			bInterfaceSubClass;
4200a2430fSAndrzej Pietrasiewicz 	unsigned char			bInterfaceProtocol;
43b3c4ec71SAbdulhadi Mohamed 	unsigned char			protocol;
44afcff6dcSMaxim Devaev 	unsigned char			idle;
4500a2430fSAndrzej Pietrasiewicz 	unsigned short			report_desc_length;
4600a2430fSAndrzej Pietrasiewicz 	char				*report_desc;
4700a2430fSAndrzej Pietrasiewicz 	unsigned short			report_length;
48d7428bc2SMaxim Devaev 	/*
49d7428bc2SMaxim Devaev 	 * use_out_ep - if true, the OUT Endpoint (interrupt out method)
50d7428bc2SMaxim Devaev 	 *              will be used to receive reports from the host
51d7428bc2SMaxim Devaev 	 *              using functions with the "intout" suffix.
52d7428bc2SMaxim Devaev 	 *              Otherwise, the OUT Endpoint will not be configured
53d7428bc2SMaxim Devaev 	 *              and the SETUP/SET_REPORT method ("ssreport" suffix)
54d7428bc2SMaxim Devaev 	 *              will be used to receive reports.
55d7428bc2SMaxim Devaev 	 */
56d7428bc2SMaxim Devaev 	bool				use_out_ep;
5700a2430fSAndrzej Pietrasiewicz 
5800a2430fSAndrzej Pietrasiewicz 	/* recv report */
5933e4c1a9SKrzysztof Opasiak 	spinlock_t			read_spinlock;
6000a2430fSAndrzej Pietrasiewicz 	wait_queue_head_t		read_queue;
61d7428bc2SMaxim Devaev 	/* recv report - interrupt out only (use_out_ep == 1) */
62d7428bc2SMaxim Devaev 	struct list_head		completed_out_req;
6300a2430fSAndrzej Pietrasiewicz 	unsigned int			qlen;
64d7428bc2SMaxim Devaev 	/* recv report - setup set_report only (use_out_ep == 0) */
65d7428bc2SMaxim Devaev 	char				*set_report_buf;
66d7428bc2SMaxim Devaev 	unsigned int			set_report_length;
6700a2430fSAndrzej Pietrasiewicz 
6800a2430fSAndrzej Pietrasiewicz 	/* send report */
6933e4c1a9SKrzysztof Opasiak 	spinlock_t			write_spinlock;
7000a2430fSAndrzej Pietrasiewicz 	bool				write_pending;
7100a2430fSAndrzej Pietrasiewicz 	wait_queue_head_t		write_queue;
7200a2430fSAndrzej Pietrasiewicz 	struct usb_request		*req;
7300a2430fSAndrzej Pietrasiewicz 
74*89ff3dfaSJohn Keeping 	struct device			dev;
7500a2430fSAndrzej Pietrasiewicz 	struct cdev			cdev;
7600a2430fSAndrzej Pietrasiewicz 	struct usb_function		func;
7700a2430fSAndrzej Pietrasiewicz 
7800a2430fSAndrzej Pietrasiewicz 	struct usb_ep			*in_ep;
7900a2430fSAndrzej Pietrasiewicz 	struct usb_ep			*out_ep;
8000a2430fSAndrzej Pietrasiewicz };
8100a2430fSAndrzej Pietrasiewicz 
8200a2430fSAndrzej Pietrasiewicz static inline struct f_hidg *func_to_hidg(struct usb_function *f)
8300a2430fSAndrzej Pietrasiewicz {
8400a2430fSAndrzej Pietrasiewicz 	return container_of(f, struct f_hidg, func);
8500a2430fSAndrzej Pietrasiewicz }
8600a2430fSAndrzej Pietrasiewicz 
87*89ff3dfaSJohn Keeping static void hidg_release(struct device *dev)
88*89ff3dfaSJohn Keeping {
89*89ff3dfaSJohn Keeping 	struct f_hidg *hidg = container_of(dev, struct f_hidg, dev);
90*89ff3dfaSJohn Keeping 
91*89ff3dfaSJohn Keeping 	kfree(hidg->set_report_buf);
92*89ff3dfaSJohn Keeping 	kfree(hidg);
93*89ff3dfaSJohn Keeping }
94*89ff3dfaSJohn Keeping 
9500a2430fSAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/
9600a2430fSAndrzej Pietrasiewicz /*                           Static descriptors                            */
9700a2430fSAndrzej Pietrasiewicz 
9800a2430fSAndrzej Pietrasiewicz static struct usb_interface_descriptor hidg_interface_desc = {
9900a2430fSAndrzej Pietrasiewicz 	.bLength		= sizeof hidg_interface_desc,
10000a2430fSAndrzej Pietrasiewicz 	.bDescriptorType	= USB_DT_INTERFACE,
10100a2430fSAndrzej Pietrasiewicz 	/* .bInterfaceNumber	= DYNAMIC */
10200a2430fSAndrzej Pietrasiewicz 	.bAlternateSetting	= 0,
103d7428bc2SMaxim Devaev 	/* .bNumEndpoints	= DYNAMIC (depends on use_out_ep) */
10400a2430fSAndrzej Pietrasiewicz 	.bInterfaceClass	= USB_CLASS_HID,
10500a2430fSAndrzej Pietrasiewicz 	/* .bInterfaceSubClass	= DYNAMIC */
10600a2430fSAndrzej Pietrasiewicz 	/* .bInterfaceProtocol	= DYNAMIC */
10700a2430fSAndrzej Pietrasiewicz 	/* .iInterface		= DYNAMIC */
10800a2430fSAndrzej Pietrasiewicz };
10900a2430fSAndrzej Pietrasiewicz 
11000a2430fSAndrzej Pietrasiewicz static struct hid_descriptor hidg_desc = {
11100a2430fSAndrzej Pietrasiewicz 	.bLength			= sizeof hidg_desc,
11200a2430fSAndrzej Pietrasiewicz 	.bDescriptorType		= HID_DT_HID,
11333cb46c4SRuslan Bilovol 	.bcdHID				= cpu_to_le16(0x0101),
11400a2430fSAndrzej Pietrasiewicz 	.bCountryCode			= 0x00,
11500a2430fSAndrzej Pietrasiewicz 	.bNumDescriptors		= 0x1,
11600a2430fSAndrzej Pietrasiewicz 	/*.desc[0].bDescriptorType	= DYNAMIC */
11700a2430fSAndrzej Pietrasiewicz 	/*.desc[0].wDescriptorLenght	= DYNAMIC */
11800a2430fSAndrzej Pietrasiewicz };
11900a2430fSAndrzej Pietrasiewicz 
120dbf499cfSJanusz Dziedzic /* Super-Speed Support */
121dbf499cfSJanusz Dziedzic 
122dbf499cfSJanusz Dziedzic static struct usb_endpoint_descriptor hidg_ss_in_ep_desc = {
123dbf499cfSJanusz Dziedzic 	.bLength		= USB_DT_ENDPOINT_SIZE,
124dbf499cfSJanusz Dziedzic 	.bDescriptorType	= USB_DT_ENDPOINT,
125dbf499cfSJanusz Dziedzic 	.bEndpointAddress	= USB_DIR_IN,
126dbf499cfSJanusz Dziedzic 	.bmAttributes		= USB_ENDPOINT_XFER_INT,
127dbf499cfSJanusz Dziedzic 	/*.wMaxPacketSize	= DYNAMIC */
128dbf499cfSJanusz Dziedzic 	.bInterval		= 4, /* FIXME: Add this field in the
129dbf499cfSJanusz Dziedzic 				      * HID gadget configuration?
130dbf499cfSJanusz Dziedzic 				      * (struct hidg_func_descriptor)
131dbf499cfSJanusz Dziedzic 				      */
132dbf499cfSJanusz Dziedzic };
133dbf499cfSJanusz Dziedzic 
134dbf499cfSJanusz Dziedzic static struct usb_ss_ep_comp_descriptor hidg_ss_in_comp_desc = {
135dbf499cfSJanusz Dziedzic 	.bLength                = sizeof(hidg_ss_in_comp_desc),
136dbf499cfSJanusz Dziedzic 	.bDescriptorType        = USB_DT_SS_ENDPOINT_COMP,
137dbf499cfSJanusz Dziedzic 
138dbf499cfSJanusz Dziedzic 	/* .bMaxBurst           = 0, */
139dbf499cfSJanusz Dziedzic 	/* .bmAttributes        = 0, */
140dbf499cfSJanusz Dziedzic 	/* .wBytesPerInterval   = DYNAMIC */
141dbf499cfSJanusz Dziedzic };
142dbf499cfSJanusz Dziedzic 
143dbf499cfSJanusz Dziedzic static struct usb_endpoint_descriptor hidg_ss_out_ep_desc = {
144dbf499cfSJanusz Dziedzic 	.bLength		= USB_DT_ENDPOINT_SIZE,
145dbf499cfSJanusz Dziedzic 	.bDescriptorType	= USB_DT_ENDPOINT,
146dbf499cfSJanusz Dziedzic 	.bEndpointAddress	= USB_DIR_OUT,
147dbf499cfSJanusz Dziedzic 	.bmAttributes		= USB_ENDPOINT_XFER_INT,
148dbf499cfSJanusz Dziedzic 	/*.wMaxPacketSize	= DYNAMIC */
149dbf499cfSJanusz Dziedzic 	.bInterval		= 4, /* FIXME: Add this field in the
150dbf499cfSJanusz Dziedzic 				      * HID gadget configuration?
151dbf499cfSJanusz Dziedzic 				      * (struct hidg_func_descriptor)
152dbf499cfSJanusz Dziedzic 				      */
153dbf499cfSJanusz Dziedzic };
154dbf499cfSJanusz Dziedzic 
155dbf499cfSJanusz Dziedzic static struct usb_ss_ep_comp_descriptor hidg_ss_out_comp_desc = {
156dbf499cfSJanusz Dziedzic 	.bLength                = sizeof(hidg_ss_out_comp_desc),
157dbf499cfSJanusz Dziedzic 	.bDescriptorType        = USB_DT_SS_ENDPOINT_COMP,
158dbf499cfSJanusz Dziedzic 
159dbf499cfSJanusz Dziedzic 	/* .bMaxBurst           = 0, */
160dbf499cfSJanusz Dziedzic 	/* .bmAttributes        = 0, */
161dbf499cfSJanusz Dziedzic 	/* .wBytesPerInterval   = DYNAMIC */
162dbf499cfSJanusz Dziedzic };
163dbf499cfSJanusz Dziedzic 
164d7428bc2SMaxim Devaev static struct usb_descriptor_header *hidg_ss_descriptors_intout[] = {
165dbf499cfSJanusz Dziedzic 	(struct usb_descriptor_header *)&hidg_interface_desc,
166dbf499cfSJanusz Dziedzic 	(struct usb_descriptor_header *)&hidg_desc,
167dbf499cfSJanusz Dziedzic 	(struct usb_descriptor_header *)&hidg_ss_in_ep_desc,
168dbf499cfSJanusz Dziedzic 	(struct usb_descriptor_header *)&hidg_ss_in_comp_desc,
169dbf499cfSJanusz Dziedzic 	(struct usb_descriptor_header *)&hidg_ss_out_ep_desc,
170dbf499cfSJanusz Dziedzic 	(struct usb_descriptor_header *)&hidg_ss_out_comp_desc,
171dbf499cfSJanusz Dziedzic 	NULL,
172dbf499cfSJanusz Dziedzic };
173dbf499cfSJanusz Dziedzic 
174d7428bc2SMaxim Devaev static struct usb_descriptor_header *hidg_ss_descriptors_ssreport[] = {
175d7428bc2SMaxim Devaev 	(struct usb_descriptor_header *)&hidg_interface_desc,
176d7428bc2SMaxim Devaev 	(struct usb_descriptor_header *)&hidg_desc,
177d7428bc2SMaxim Devaev 	(struct usb_descriptor_header *)&hidg_ss_in_ep_desc,
178d7428bc2SMaxim Devaev 	(struct usb_descriptor_header *)&hidg_ss_in_comp_desc,
179d7428bc2SMaxim Devaev 	NULL,
180d7428bc2SMaxim Devaev };
181d7428bc2SMaxim Devaev 
18200a2430fSAndrzej Pietrasiewicz /* High-Speed Support */
18300a2430fSAndrzej Pietrasiewicz 
18400a2430fSAndrzej Pietrasiewicz static struct usb_endpoint_descriptor hidg_hs_in_ep_desc = {
18500a2430fSAndrzej Pietrasiewicz 	.bLength		= USB_DT_ENDPOINT_SIZE,
18600a2430fSAndrzej Pietrasiewicz 	.bDescriptorType	= USB_DT_ENDPOINT,
18700a2430fSAndrzej Pietrasiewicz 	.bEndpointAddress	= USB_DIR_IN,
18800a2430fSAndrzej Pietrasiewicz 	.bmAttributes		= USB_ENDPOINT_XFER_INT,
18900a2430fSAndrzej Pietrasiewicz 	/*.wMaxPacketSize	= DYNAMIC */
19000a2430fSAndrzej Pietrasiewicz 	.bInterval		= 4, /* FIXME: Add this field in the
19100a2430fSAndrzej Pietrasiewicz 				      * HID gadget configuration?
19200a2430fSAndrzej Pietrasiewicz 				      * (struct hidg_func_descriptor)
19300a2430fSAndrzej Pietrasiewicz 				      */
19400a2430fSAndrzej Pietrasiewicz };
19500a2430fSAndrzej Pietrasiewicz 
19600a2430fSAndrzej Pietrasiewicz static struct usb_endpoint_descriptor hidg_hs_out_ep_desc = {
19700a2430fSAndrzej Pietrasiewicz 	.bLength		= USB_DT_ENDPOINT_SIZE,
19800a2430fSAndrzej Pietrasiewicz 	.bDescriptorType	= USB_DT_ENDPOINT,
19900a2430fSAndrzej Pietrasiewicz 	.bEndpointAddress	= USB_DIR_OUT,
20000a2430fSAndrzej Pietrasiewicz 	.bmAttributes		= USB_ENDPOINT_XFER_INT,
20100a2430fSAndrzej Pietrasiewicz 	/*.wMaxPacketSize	= DYNAMIC */
20200a2430fSAndrzej Pietrasiewicz 	.bInterval		= 4, /* FIXME: Add this field in the
20300a2430fSAndrzej Pietrasiewicz 				      * HID gadget configuration?
20400a2430fSAndrzej Pietrasiewicz 				      * (struct hidg_func_descriptor)
20500a2430fSAndrzej Pietrasiewicz 				      */
20600a2430fSAndrzej Pietrasiewicz };
20700a2430fSAndrzej Pietrasiewicz 
208d7428bc2SMaxim Devaev static struct usb_descriptor_header *hidg_hs_descriptors_intout[] = {
20900a2430fSAndrzej Pietrasiewicz 	(struct usb_descriptor_header *)&hidg_interface_desc,
21000a2430fSAndrzej Pietrasiewicz 	(struct usb_descriptor_header *)&hidg_desc,
21100a2430fSAndrzej Pietrasiewicz 	(struct usb_descriptor_header *)&hidg_hs_in_ep_desc,
21200a2430fSAndrzej Pietrasiewicz 	(struct usb_descriptor_header *)&hidg_hs_out_ep_desc,
21300a2430fSAndrzej Pietrasiewicz 	NULL,
21400a2430fSAndrzej Pietrasiewicz };
21500a2430fSAndrzej Pietrasiewicz 
216d7428bc2SMaxim Devaev static struct usb_descriptor_header *hidg_hs_descriptors_ssreport[] = {
217d7428bc2SMaxim Devaev 	(struct usb_descriptor_header *)&hidg_interface_desc,
218d7428bc2SMaxim Devaev 	(struct usb_descriptor_header *)&hidg_desc,
219d7428bc2SMaxim Devaev 	(struct usb_descriptor_header *)&hidg_hs_in_ep_desc,
220d7428bc2SMaxim Devaev 	NULL,
221d7428bc2SMaxim Devaev };
222d7428bc2SMaxim Devaev 
22300a2430fSAndrzej Pietrasiewicz /* Full-Speed Support */
22400a2430fSAndrzej Pietrasiewicz 
22500a2430fSAndrzej Pietrasiewicz static struct usb_endpoint_descriptor hidg_fs_in_ep_desc = {
22600a2430fSAndrzej Pietrasiewicz 	.bLength		= USB_DT_ENDPOINT_SIZE,
22700a2430fSAndrzej Pietrasiewicz 	.bDescriptorType	= USB_DT_ENDPOINT,
22800a2430fSAndrzej Pietrasiewicz 	.bEndpointAddress	= USB_DIR_IN,
22900a2430fSAndrzej Pietrasiewicz 	.bmAttributes		= USB_ENDPOINT_XFER_INT,
23000a2430fSAndrzej Pietrasiewicz 	/*.wMaxPacketSize	= DYNAMIC */
23100a2430fSAndrzej Pietrasiewicz 	.bInterval		= 10, /* FIXME: Add this field in the
23200a2430fSAndrzej Pietrasiewicz 				       * HID gadget configuration?
23300a2430fSAndrzej Pietrasiewicz 				       * (struct hidg_func_descriptor)
23400a2430fSAndrzej Pietrasiewicz 				       */
23500a2430fSAndrzej Pietrasiewicz };
23600a2430fSAndrzej Pietrasiewicz 
23700a2430fSAndrzej Pietrasiewicz static struct usb_endpoint_descriptor hidg_fs_out_ep_desc = {
23800a2430fSAndrzej Pietrasiewicz 	.bLength		= USB_DT_ENDPOINT_SIZE,
23900a2430fSAndrzej Pietrasiewicz 	.bDescriptorType	= USB_DT_ENDPOINT,
24000a2430fSAndrzej Pietrasiewicz 	.bEndpointAddress	= USB_DIR_OUT,
24100a2430fSAndrzej Pietrasiewicz 	.bmAttributes		= USB_ENDPOINT_XFER_INT,
24200a2430fSAndrzej Pietrasiewicz 	/*.wMaxPacketSize	= DYNAMIC */
24300a2430fSAndrzej Pietrasiewicz 	.bInterval		= 10, /* FIXME: Add this field in the
24400a2430fSAndrzej Pietrasiewicz 				       * HID gadget configuration?
24500a2430fSAndrzej Pietrasiewicz 				       * (struct hidg_func_descriptor)
24600a2430fSAndrzej Pietrasiewicz 				       */
24700a2430fSAndrzej Pietrasiewicz };
24800a2430fSAndrzej Pietrasiewicz 
249d7428bc2SMaxim Devaev static struct usb_descriptor_header *hidg_fs_descriptors_intout[] = {
25000a2430fSAndrzej Pietrasiewicz 	(struct usb_descriptor_header *)&hidg_interface_desc,
25100a2430fSAndrzej Pietrasiewicz 	(struct usb_descriptor_header *)&hidg_desc,
25200a2430fSAndrzej Pietrasiewicz 	(struct usb_descriptor_header *)&hidg_fs_in_ep_desc,
25300a2430fSAndrzej Pietrasiewicz 	(struct usb_descriptor_header *)&hidg_fs_out_ep_desc,
25400a2430fSAndrzej Pietrasiewicz 	NULL,
25500a2430fSAndrzej Pietrasiewicz };
25600a2430fSAndrzej Pietrasiewicz 
257d7428bc2SMaxim Devaev static struct usb_descriptor_header *hidg_fs_descriptors_ssreport[] = {
258d7428bc2SMaxim Devaev 	(struct usb_descriptor_header *)&hidg_interface_desc,
259d7428bc2SMaxim Devaev 	(struct usb_descriptor_header *)&hidg_desc,
260d7428bc2SMaxim Devaev 	(struct usb_descriptor_header *)&hidg_fs_in_ep_desc,
261d7428bc2SMaxim Devaev 	NULL,
262d7428bc2SMaxim Devaev };
263d7428bc2SMaxim Devaev 
26400a2430fSAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/
265cb382536SAndrzej Pietrasiewicz /*                                 Strings                                 */
266cb382536SAndrzej Pietrasiewicz 
267cb382536SAndrzej Pietrasiewicz #define CT_FUNC_HID_IDX	0
268cb382536SAndrzej Pietrasiewicz 
269cb382536SAndrzej Pietrasiewicz static struct usb_string ct_func_string_defs[] = {
270cb382536SAndrzej Pietrasiewicz 	[CT_FUNC_HID_IDX].s	= "HID Interface",
271cb382536SAndrzej Pietrasiewicz 	{},			/* end of list */
272cb382536SAndrzej Pietrasiewicz };
273cb382536SAndrzej Pietrasiewicz 
274cb382536SAndrzej Pietrasiewicz static struct usb_gadget_strings ct_func_string_table = {
275cb382536SAndrzej Pietrasiewicz 	.language	= 0x0409,	/* en-US */
276cb382536SAndrzej Pietrasiewicz 	.strings	= ct_func_string_defs,
277cb382536SAndrzej Pietrasiewicz };
278cb382536SAndrzej Pietrasiewicz 
279cb382536SAndrzej Pietrasiewicz static struct usb_gadget_strings *ct_func_strings[] = {
280cb382536SAndrzej Pietrasiewicz 	&ct_func_string_table,
281cb382536SAndrzej Pietrasiewicz 	NULL,
282cb382536SAndrzej Pietrasiewicz };
283cb382536SAndrzej Pietrasiewicz 
284cb382536SAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/
28500a2430fSAndrzej Pietrasiewicz /*                              Char Device                                */
28600a2430fSAndrzej Pietrasiewicz 
287d7428bc2SMaxim Devaev static ssize_t f_hidg_intout_read(struct file *file, char __user *buffer,
28800a2430fSAndrzej Pietrasiewicz 				  size_t count, loff_t *ptr)
28900a2430fSAndrzej Pietrasiewicz {
29000a2430fSAndrzej Pietrasiewicz 	struct f_hidg *hidg = file->private_data;
29100a2430fSAndrzej Pietrasiewicz 	struct f_hidg_req_list *list;
29200a2430fSAndrzej Pietrasiewicz 	struct usb_request *req;
29300a2430fSAndrzej Pietrasiewicz 	unsigned long flags;
29400a2430fSAndrzej Pietrasiewicz 	int ret;
29500a2430fSAndrzej Pietrasiewicz 
29600a2430fSAndrzej Pietrasiewicz 	if (!count)
29700a2430fSAndrzej Pietrasiewicz 		return 0;
29800a2430fSAndrzej Pietrasiewicz 
29933e4c1a9SKrzysztof Opasiak 	spin_lock_irqsave(&hidg->read_spinlock, flags);
30000a2430fSAndrzej Pietrasiewicz 
301d7428bc2SMaxim Devaev #define READ_COND_INTOUT (!list_empty(&hidg->completed_out_req))
30200a2430fSAndrzej Pietrasiewicz 
30300a2430fSAndrzej Pietrasiewicz 	/* wait for at least one buffer to complete */
304d7428bc2SMaxim Devaev 	while (!READ_COND_INTOUT) {
30533e4c1a9SKrzysztof Opasiak 		spin_unlock_irqrestore(&hidg->read_spinlock, flags);
30600a2430fSAndrzej Pietrasiewicz 		if (file->f_flags & O_NONBLOCK)
30700a2430fSAndrzej Pietrasiewicz 			return -EAGAIN;
30800a2430fSAndrzej Pietrasiewicz 
309d7428bc2SMaxim Devaev 		if (wait_event_interruptible(hidg->read_queue, READ_COND_INTOUT))
31000a2430fSAndrzej Pietrasiewicz 			return -ERESTARTSYS;
31100a2430fSAndrzej Pietrasiewicz 
31233e4c1a9SKrzysztof Opasiak 		spin_lock_irqsave(&hidg->read_spinlock, flags);
31300a2430fSAndrzej Pietrasiewicz 	}
31400a2430fSAndrzej Pietrasiewicz 
31500a2430fSAndrzej Pietrasiewicz 	/* pick the first one */
31600a2430fSAndrzej Pietrasiewicz 	list = list_first_entry(&hidg->completed_out_req,
31700a2430fSAndrzej Pietrasiewicz 				struct f_hidg_req_list, list);
318aa65d11aSKrzysztof Opasiak 
319aa65d11aSKrzysztof Opasiak 	/*
320aa65d11aSKrzysztof Opasiak 	 * Remove this from list to protect it from beign free()
321aa65d11aSKrzysztof Opasiak 	 * while host disables our function
322aa65d11aSKrzysztof Opasiak 	 */
323aa65d11aSKrzysztof Opasiak 	list_del(&list->list);
324aa65d11aSKrzysztof Opasiak 
32500a2430fSAndrzej Pietrasiewicz 	req = list->req;
32600a2430fSAndrzej Pietrasiewicz 	count = min_t(unsigned int, count, req->actual - list->pos);
32733e4c1a9SKrzysztof Opasiak 	spin_unlock_irqrestore(&hidg->read_spinlock, flags);
32800a2430fSAndrzej Pietrasiewicz 
32900a2430fSAndrzej Pietrasiewicz 	/* copy to user outside spinlock */
33000a2430fSAndrzej Pietrasiewicz 	count -= copy_to_user(buffer, req->buf + list->pos, count);
33100a2430fSAndrzej Pietrasiewicz 	list->pos += count;
33200a2430fSAndrzej Pietrasiewicz 
33300a2430fSAndrzej Pietrasiewicz 	/*
33400a2430fSAndrzej Pietrasiewicz 	 * if this request is completely handled and transfered to
33500a2430fSAndrzej Pietrasiewicz 	 * userspace, remove its entry from the list and requeue it
33600a2430fSAndrzej Pietrasiewicz 	 * again. Otherwise, we will revisit it again upon the next
33700a2430fSAndrzej Pietrasiewicz 	 * call, taking into account its current read position.
33800a2430fSAndrzej Pietrasiewicz 	 */
33900a2430fSAndrzej Pietrasiewicz 	if (list->pos == req->actual) {
34000a2430fSAndrzej Pietrasiewicz 		kfree(list);
34100a2430fSAndrzej Pietrasiewicz 
34200a2430fSAndrzej Pietrasiewicz 		req->length = hidg->report_length;
34300a2430fSAndrzej Pietrasiewicz 		ret = usb_ep_queue(hidg->out_ep, req, GFP_KERNEL);
344aa65d11aSKrzysztof Opasiak 		if (ret < 0) {
345aa65d11aSKrzysztof Opasiak 			free_ep_req(hidg->out_ep, req);
34600a2430fSAndrzej Pietrasiewicz 			return ret;
34700a2430fSAndrzej Pietrasiewicz 		}
348aa65d11aSKrzysztof Opasiak 	} else {
34933e4c1a9SKrzysztof Opasiak 		spin_lock_irqsave(&hidg->read_spinlock, flags);
350aa65d11aSKrzysztof Opasiak 		list_add(&list->list, &hidg->completed_out_req);
35133e4c1a9SKrzysztof Opasiak 		spin_unlock_irqrestore(&hidg->read_spinlock, flags);
352aa65d11aSKrzysztof Opasiak 
353aa65d11aSKrzysztof Opasiak 		wake_up(&hidg->read_queue);
354aa65d11aSKrzysztof Opasiak 	}
35500a2430fSAndrzej Pietrasiewicz 
35600a2430fSAndrzej Pietrasiewicz 	return count;
35700a2430fSAndrzej Pietrasiewicz }
35800a2430fSAndrzej Pietrasiewicz 
359d7428bc2SMaxim Devaev #define READ_COND_SSREPORT (hidg->set_report_buf != NULL)
360d7428bc2SMaxim Devaev 
361d7428bc2SMaxim Devaev static ssize_t f_hidg_ssreport_read(struct file *file, char __user *buffer,
362d7428bc2SMaxim Devaev 				    size_t count, loff_t *ptr)
363d7428bc2SMaxim Devaev {
364d7428bc2SMaxim Devaev 	struct f_hidg *hidg = file->private_data;
365d7428bc2SMaxim Devaev 	char *tmp_buf = NULL;
366d7428bc2SMaxim Devaev 	unsigned long flags;
367d7428bc2SMaxim Devaev 
368d7428bc2SMaxim Devaev 	if (!count)
369d7428bc2SMaxim Devaev 		return 0;
370d7428bc2SMaxim Devaev 
371d7428bc2SMaxim Devaev 	spin_lock_irqsave(&hidg->read_spinlock, flags);
372d7428bc2SMaxim Devaev 
373d7428bc2SMaxim Devaev 	while (!READ_COND_SSREPORT) {
374d7428bc2SMaxim Devaev 		spin_unlock_irqrestore(&hidg->read_spinlock, flags);
375d7428bc2SMaxim Devaev 		if (file->f_flags & O_NONBLOCK)
376d7428bc2SMaxim Devaev 			return -EAGAIN;
377d7428bc2SMaxim Devaev 
378d7428bc2SMaxim Devaev 		if (wait_event_interruptible(hidg->read_queue, READ_COND_SSREPORT))
379d7428bc2SMaxim Devaev 			return -ERESTARTSYS;
380d7428bc2SMaxim Devaev 
381d7428bc2SMaxim Devaev 		spin_lock_irqsave(&hidg->read_spinlock, flags);
382d7428bc2SMaxim Devaev 	}
383d7428bc2SMaxim Devaev 
384d7428bc2SMaxim Devaev 	count = min_t(unsigned int, count, hidg->set_report_length);
385d7428bc2SMaxim Devaev 	tmp_buf = hidg->set_report_buf;
386d7428bc2SMaxim Devaev 	hidg->set_report_buf = NULL;
387d7428bc2SMaxim Devaev 
388d7428bc2SMaxim Devaev 	spin_unlock_irqrestore(&hidg->read_spinlock, flags);
389d7428bc2SMaxim Devaev 
390d7428bc2SMaxim Devaev 	if (tmp_buf != NULL) {
391d7428bc2SMaxim Devaev 		count -= copy_to_user(buffer, tmp_buf, count);
392d7428bc2SMaxim Devaev 		kfree(tmp_buf);
393d7428bc2SMaxim Devaev 	} else {
394d7428bc2SMaxim Devaev 		count = -ENOMEM;
395d7428bc2SMaxim Devaev 	}
396d7428bc2SMaxim Devaev 
397d7428bc2SMaxim Devaev 	wake_up(&hidg->read_queue);
398d7428bc2SMaxim Devaev 
399d7428bc2SMaxim Devaev 	return count;
400d7428bc2SMaxim Devaev }
401d7428bc2SMaxim Devaev 
402d7428bc2SMaxim Devaev static ssize_t f_hidg_read(struct file *file, char __user *buffer,
403d7428bc2SMaxim Devaev 			   size_t count, loff_t *ptr)
404d7428bc2SMaxim Devaev {
405d7428bc2SMaxim Devaev 	struct f_hidg *hidg = file->private_data;
406d7428bc2SMaxim Devaev 
407d7428bc2SMaxim Devaev 	if (hidg->use_out_ep)
408d7428bc2SMaxim Devaev 		return f_hidg_intout_read(file, buffer, count, ptr);
409d7428bc2SMaxim Devaev 	else
410d7428bc2SMaxim Devaev 		return f_hidg_ssreport_read(file, buffer, count, ptr);
411d7428bc2SMaxim Devaev }
412d7428bc2SMaxim Devaev 
41300a2430fSAndrzej Pietrasiewicz static void f_hidg_req_complete(struct usb_ep *ep, struct usb_request *req)
41400a2430fSAndrzej Pietrasiewicz {
41500a2430fSAndrzej Pietrasiewicz 	struct f_hidg *hidg = (struct f_hidg *)ep->driver_data;
41633e4c1a9SKrzysztof Opasiak 	unsigned long flags;
41700a2430fSAndrzej Pietrasiewicz 
41800a2430fSAndrzej Pietrasiewicz 	if (req->status != 0) {
41900a2430fSAndrzej Pietrasiewicz 		ERROR(hidg->func.config->cdev,
42000a2430fSAndrzej Pietrasiewicz 			"End Point Request ERROR: %d\n", req->status);
42100a2430fSAndrzej Pietrasiewicz 	}
42200a2430fSAndrzej Pietrasiewicz 
42333e4c1a9SKrzysztof Opasiak 	spin_lock_irqsave(&hidg->write_spinlock, flags);
42400a2430fSAndrzej Pietrasiewicz 	hidg->write_pending = 0;
42533e4c1a9SKrzysztof Opasiak 	spin_unlock_irqrestore(&hidg->write_spinlock, flags);
42600a2430fSAndrzej Pietrasiewicz 	wake_up(&hidg->write_queue);
42700a2430fSAndrzej Pietrasiewicz }
42800a2430fSAndrzej Pietrasiewicz 
42900a2430fSAndrzej Pietrasiewicz static ssize_t f_hidg_write(struct file *file, const char __user *buffer,
43000a2430fSAndrzej Pietrasiewicz 			    size_t count, loff_t *offp)
43100a2430fSAndrzej Pietrasiewicz {
43200a2430fSAndrzej Pietrasiewicz 	struct f_hidg *hidg  = file->private_data;
433749494b6SKrzysztof Opasiak 	struct usb_request *req;
43433e4c1a9SKrzysztof Opasiak 	unsigned long flags;
43500a2430fSAndrzej Pietrasiewicz 	ssize_t status = -ENOMEM;
43600a2430fSAndrzej Pietrasiewicz 
43733e4c1a9SKrzysztof Opasiak 	spin_lock_irqsave(&hidg->write_spinlock, flags);
43800a2430fSAndrzej Pietrasiewicz 
4392867652eSPhil Elwell 	if (!hidg->req) {
4402867652eSPhil Elwell 		spin_unlock_irqrestore(&hidg->write_spinlock, flags);
4412867652eSPhil Elwell 		return -ESHUTDOWN;
4422867652eSPhil Elwell 	}
4432867652eSPhil Elwell 
44400a2430fSAndrzej Pietrasiewicz #define WRITE_COND (!hidg->write_pending)
445749494b6SKrzysztof Opasiak try_again:
44600a2430fSAndrzej Pietrasiewicz 	/* write queue */
44700a2430fSAndrzej Pietrasiewicz 	while (!WRITE_COND) {
44833e4c1a9SKrzysztof Opasiak 		spin_unlock_irqrestore(&hidg->write_spinlock, flags);
44900a2430fSAndrzej Pietrasiewicz 		if (file->f_flags & O_NONBLOCK)
45000a2430fSAndrzej Pietrasiewicz 			return -EAGAIN;
45100a2430fSAndrzej Pietrasiewicz 
45200a2430fSAndrzej Pietrasiewicz 		if (wait_event_interruptible_exclusive(
45300a2430fSAndrzej Pietrasiewicz 				hidg->write_queue, WRITE_COND))
45400a2430fSAndrzej Pietrasiewicz 			return -ERESTARTSYS;
45500a2430fSAndrzej Pietrasiewicz 
45633e4c1a9SKrzysztof Opasiak 		spin_lock_irqsave(&hidg->write_spinlock, flags);
45700a2430fSAndrzej Pietrasiewicz 	}
45800a2430fSAndrzej Pietrasiewicz 
45933e4c1a9SKrzysztof Opasiak 	hidg->write_pending = 1;
460749494b6SKrzysztof Opasiak 	req = hidg->req;
46100a2430fSAndrzej Pietrasiewicz 	count  = min_t(unsigned, count, hidg->report_length);
46233e4c1a9SKrzysztof Opasiak 
46333e4c1a9SKrzysztof Opasiak 	spin_unlock_irqrestore(&hidg->write_spinlock, flags);
46400a2430fSAndrzej Pietrasiewicz 
4652867652eSPhil Elwell 	if (!req) {
4662867652eSPhil Elwell 		ERROR(hidg->func.config->cdev, "hidg->req is NULL\n");
4672867652eSPhil Elwell 		status = -ESHUTDOWN;
4682867652eSPhil Elwell 		goto release_write_pending;
4692867652eSPhil Elwell 	}
4702867652eSPhil Elwell 
4712867652eSPhil Elwell 	status = copy_from_user(req->buf, buffer, count);
47200a2430fSAndrzej Pietrasiewicz 	if (status != 0) {
47300a2430fSAndrzej Pietrasiewicz 		ERROR(hidg->func.config->cdev,
47400a2430fSAndrzej Pietrasiewicz 			"copy_from_user error\n");
47533e4c1a9SKrzysztof Opasiak 		status = -EINVAL;
47633e4c1a9SKrzysztof Opasiak 		goto release_write_pending;
47700a2430fSAndrzej Pietrasiewicz 	}
47800a2430fSAndrzej Pietrasiewicz 
479749494b6SKrzysztof Opasiak 	spin_lock_irqsave(&hidg->write_spinlock, flags);
480749494b6SKrzysztof Opasiak 
48125cd9721SKrzysztof Opasiak 	/* when our function has been disabled by host */
482749494b6SKrzysztof Opasiak 	if (!hidg->req) {
48325cd9721SKrzysztof Opasiak 		free_ep_req(hidg->in_ep, req);
484749494b6SKrzysztof Opasiak 		/*
485749494b6SKrzysztof Opasiak 		 * TODO
486749494b6SKrzysztof Opasiak 		 * Should we fail with error here?
487749494b6SKrzysztof Opasiak 		 */
488749494b6SKrzysztof Opasiak 		goto try_again;
489749494b6SKrzysztof Opasiak 	}
490749494b6SKrzysztof Opasiak 
491749494b6SKrzysztof Opasiak 	req->status   = 0;
492749494b6SKrzysztof Opasiak 	req->zero     = 0;
493749494b6SKrzysztof Opasiak 	req->length   = count;
494749494b6SKrzysztof Opasiak 	req->complete = f_hidg_req_complete;
495749494b6SKrzysztof Opasiak 	req->context  = hidg;
49600a2430fSAndrzej Pietrasiewicz 
497072684e8SRadoslav Gerganov 	spin_unlock_irqrestore(&hidg->write_spinlock, flags);
498072684e8SRadoslav Gerganov 
4992867652eSPhil Elwell 	if (!hidg->in_ep->enabled) {
5002867652eSPhil Elwell 		ERROR(hidg->func.config->cdev, "in_ep is disabled\n");
5012867652eSPhil Elwell 		status = -ESHUTDOWN;
502072684e8SRadoslav Gerganov 		goto release_write_pending;
50300a2430fSAndrzej Pietrasiewicz 	}
50400a2430fSAndrzej Pietrasiewicz 
5052867652eSPhil Elwell 	status = usb_ep_queue(hidg->in_ep, req, GFP_ATOMIC);
5062867652eSPhil Elwell 	if (status < 0)
5072867652eSPhil Elwell 		goto release_write_pending;
5082867652eSPhil Elwell 	else
5092867652eSPhil Elwell 		status = count;
5102867652eSPhil Elwell 
51133e4c1a9SKrzysztof Opasiak 	return status;
51233e4c1a9SKrzysztof Opasiak release_write_pending:
51333e4c1a9SKrzysztof Opasiak 	spin_lock_irqsave(&hidg->write_spinlock, flags);
51433e4c1a9SKrzysztof Opasiak 	hidg->write_pending = 0;
51533e4c1a9SKrzysztof Opasiak 	spin_unlock_irqrestore(&hidg->write_spinlock, flags);
51633e4c1a9SKrzysztof Opasiak 
51733e4c1a9SKrzysztof Opasiak 	wake_up(&hidg->write_queue);
51800a2430fSAndrzej Pietrasiewicz 
51900a2430fSAndrzej Pietrasiewicz 	return status;
52000a2430fSAndrzej Pietrasiewicz }
52100a2430fSAndrzej Pietrasiewicz 
522afc9a42bSAl Viro static __poll_t f_hidg_poll(struct file *file, poll_table *wait)
52300a2430fSAndrzej Pietrasiewicz {
52400a2430fSAndrzej Pietrasiewicz 	struct f_hidg	*hidg  = file->private_data;
525afc9a42bSAl Viro 	__poll_t	ret = 0;
52600a2430fSAndrzej Pietrasiewicz 
52700a2430fSAndrzej Pietrasiewicz 	poll_wait(file, &hidg->read_queue, wait);
52800a2430fSAndrzej Pietrasiewicz 	poll_wait(file, &hidg->write_queue, wait);
52900a2430fSAndrzej Pietrasiewicz 
53000a2430fSAndrzej Pietrasiewicz 	if (WRITE_COND)
531a9a08845SLinus Torvalds 		ret |= EPOLLOUT | EPOLLWRNORM;
53200a2430fSAndrzej Pietrasiewicz 
533d7428bc2SMaxim Devaev 	if (hidg->use_out_ep) {
534d7428bc2SMaxim Devaev 		if (READ_COND_INTOUT)
535a9a08845SLinus Torvalds 			ret |= EPOLLIN | EPOLLRDNORM;
536d7428bc2SMaxim Devaev 	} else {
537d7428bc2SMaxim Devaev 		if (READ_COND_SSREPORT)
538d7428bc2SMaxim Devaev 			ret |= EPOLLIN | EPOLLRDNORM;
539d7428bc2SMaxim Devaev 	}
54000a2430fSAndrzej Pietrasiewicz 
54100a2430fSAndrzej Pietrasiewicz 	return ret;
54200a2430fSAndrzej Pietrasiewicz }
54300a2430fSAndrzej Pietrasiewicz 
54400a2430fSAndrzej Pietrasiewicz #undef WRITE_COND
545d7428bc2SMaxim Devaev #undef READ_COND_SSREPORT
546d7428bc2SMaxim Devaev #undef READ_COND_INTOUT
54700a2430fSAndrzej Pietrasiewicz 
54800a2430fSAndrzej Pietrasiewicz static int f_hidg_release(struct inode *inode, struct file *fd)
54900a2430fSAndrzej Pietrasiewicz {
55000a2430fSAndrzej Pietrasiewicz 	fd->private_data = NULL;
55100a2430fSAndrzej Pietrasiewicz 	return 0;
55200a2430fSAndrzej Pietrasiewicz }
55300a2430fSAndrzej Pietrasiewicz 
55400a2430fSAndrzej Pietrasiewicz static int f_hidg_open(struct inode *inode, struct file *fd)
55500a2430fSAndrzej Pietrasiewicz {
55600a2430fSAndrzej Pietrasiewicz 	struct f_hidg *hidg =
55700a2430fSAndrzej Pietrasiewicz 		container_of(inode->i_cdev, struct f_hidg, cdev);
55800a2430fSAndrzej Pietrasiewicz 
55900a2430fSAndrzej Pietrasiewicz 	fd->private_data = hidg;
56000a2430fSAndrzej Pietrasiewicz 
56100a2430fSAndrzej Pietrasiewicz 	return 0;
56200a2430fSAndrzej Pietrasiewicz }
56300a2430fSAndrzej Pietrasiewicz 
56400a2430fSAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/
56500a2430fSAndrzej Pietrasiewicz /*                                usb_function                             */
56600a2430fSAndrzej Pietrasiewicz 
56700a2430fSAndrzej Pietrasiewicz static inline struct usb_request *hidg_alloc_ep_req(struct usb_ep *ep,
56800a2430fSAndrzej Pietrasiewicz 						    unsigned length)
56900a2430fSAndrzej Pietrasiewicz {
570aadbe812SFelipe F. Tonello 	return alloc_ep_req(ep, length);
57100a2430fSAndrzej Pietrasiewicz }
57200a2430fSAndrzej Pietrasiewicz 
573d7428bc2SMaxim Devaev static void hidg_intout_complete(struct usb_ep *ep, struct usb_request *req)
57400a2430fSAndrzej Pietrasiewicz {
57500a2430fSAndrzej Pietrasiewicz 	struct f_hidg *hidg = (struct f_hidg *) req->context;
57620d2ca95SKrzysztof Opasiak 	struct usb_composite_dev *cdev = hidg->func.config->cdev;
57700a2430fSAndrzej Pietrasiewicz 	struct f_hidg_req_list *req_list;
57800a2430fSAndrzej Pietrasiewicz 	unsigned long flags;
57900a2430fSAndrzej Pietrasiewicz 
58020d2ca95SKrzysztof Opasiak 	switch (req->status) {
58120d2ca95SKrzysztof Opasiak 	case 0:
58200a2430fSAndrzej Pietrasiewicz 		req_list = kzalloc(sizeof(*req_list), GFP_ATOMIC);
58320d2ca95SKrzysztof Opasiak 		if (!req_list) {
58420d2ca95SKrzysztof Opasiak 			ERROR(cdev, "Unable to allocate mem for req_list\n");
58520d2ca95SKrzysztof Opasiak 			goto free_req;
58620d2ca95SKrzysztof Opasiak 		}
58700a2430fSAndrzej Pietrasiewicz 
58800a2430fSAndrzej Pietrasiewicz 		req_list->req = req;
58900a2430fSAndrzej Pietrasiewicz 
59033e4c1a9SKrzysztof Opasiak 		spin_lock_irqsave(&hidg->read_spinlock, flags);
59100a2430fSAndrzej Pietrasiewicz 		list_add_tail(&req_list->list, &hidg->completed_out_req);
59233e4c1a9SKrzysztof Opasiak 		spin_unlock_irqrestore(&hidg->read_spinlock, flags);
59300a2430fSAndrzej Pietrasiewicz 
59400a2430fSAndrzej Pietrasiewicz 		wake_up(&hidg->read_queue);
59520d2ca95SKrzysztof Opasiak 		break;
59620d2ca95SKrzysztof Opasiak 	default:
59720d2ca95SKrzysztof Opasiak 		ERROR(cdev, "Set report failed %d\n", req->status);
598a74005abSGustavo A. R. Silva 		fallthrough;
59920d2ca95SKrzysztof Opasiak 	case -ECONNABORTED:		/* hardware forced ep reset */
60020d2ca95SKrzysztof Opasiak 	case -ECONNRESET:		/* request dequeued */
60120d2ca95SKrzysztof Opasiak 	case -ESHUTDOWN:		/* disconnect from host */
60220d2ca95SKrzysztof Opasiak free_req:
60320d2ca95SKrzysztof Opasiak 		free_ep_req(ep, req);
60420d2ca95SKrzysztof Opasiak 		return;
60520d2ca95SKrzysztof Opasiak 	}
60600a2430fSAndrzej Pietrasiewicz }
60700a2430fSAndrzej Pietrasiewicz 
608d7428bc2SMaxim Devaev static void hidg_ssreport_complete(struct usb_ep *ep, struct usb_request *req)
609d7428bc2SMaxim Devaev {
610d7428bc2SMaxim Devaev 	struct f_hidg *hidg = (struct f_hidg *)req->context;
611d7428bc2SMaxim Devaev 	struct usb_composite_dev *cdev = hidg->func.config->cdev;
612d7428bc2SMaxim Devaev 	char *new_buf = NULL;
613d7428bc2SMaxim Devaev 	unsigned long flags;
614d7428bc2SMaxim Devaev 
615d7428bc2SMaxim Devaev 	if (req->status != 0 || req->buf == NULL || req->actual == 0) {
616d7428bc2SMaxim Devaev 		ERROR(cdev,
617d7428bc2SMaxim Devaev 		      "%s FAILED: status=%d, buf=%p, actual=%d\n",
618d7428bc2SMaxim Devaev 		      __func__, req->status, req->buf, req->actual);
619d7428bc2SMaxim Devaev 		return;
620d7428bc2SMaxim Devaev 	}
621d7428bc2SMaxim Devaev 
622d7428bc2SMaxim Devaev 	spin_lock_irqsave(&hidg->read_spinlock, flags);
623d7428bc2SMaxim Devaev 
624d7428bc2SMaxim Devaev 	new_buf = krealloc(hidg->set_report_buf, req->actual, GFP_ATOMIC);
625d7428bc2SMaxim Devaev 	if (new_buf == NULL) {
626d7428bc2SMaxim Devaev 		spin_unlock_irqrestore(&hidg->read_spinlock, flags);
627d7428bc2SMaxim Devaev 		return;
628d7428bc2SMaxim Devaev 	}
629d7428bc2SMaxim Devaev 	hidg->set_report_buf = new_buf;
630d7428bc2SMaxim Devaev 
631d7428bc2SMaxim Devaev 	hidg->set_report_length = req->actual;
632d7428bc2SMaxim Devaev 	memcpy(hidg->set_report_buf, req->buf, req->actual);
633d7428bc2SMaxim Devaev 
634d7428bc2SMaxim Devaev 	spin_unlock_irqrestore(&hidg->read_spinlock, flags);
635d7428bc2SMaxim Devaev 
636d7428bc2SMaxim Devaev 	wake_up(&hidg->read_queue);
637d7428bc2SMaxim Devaev }
638d7428bc2SMaxim Devaev 
63900a2430fSAndrzej Pietrasiewicz static int hidg_setup(struct usb_function *f,
64000a2430fSAndrzej Pietrasiewicz 		const struct usb_ctrlrequest *ctrl)
64100a2430fSAndrzej Pietrasiewicz {
64200a2430fSAndrzej Pietrasiewicz 	struct f_hidg			*hidg = func_to_hidg(f);
64300a2430fSAndrzej Pietrasiewicz 	struct usb_composite_dev	*cdev = f->config->cdev;
64400a2430fSAndrzej Pietrasiewicz 	struct usb_request		*req  = cdev->req;
64500a2430fSAndrzej Pietrasiewicz 	int status = 0;
64600a2430fSAndrzej Pietrasiewicz 	__u16 value, length;
64700a2430fSAndrzej Pietrasiewicz 
64800a2430fSAndrzej Pietrasiewicz 	value	= __le16_to_cpu(ctrl->wValue);
64900a2430fSAndrzej Pietrasiewicz 	length	= __le16_to_cpu(ctrl->wLength);
65000a2430fSAndrzej Pietrasiewicz 
651c9b3bde0SJulia Lawall 	VDBG(cdev,
652c9b3bde0SJulia Lawall 	     "%s crtl_request : bRequestType:0x%x bRequest:0x%x Value:0x%x\n",
653c9b3bde0SJulia Lawall 	     __func__, ctrl->bRequestType, ctrl->bRequest, value);
65400a2430fSAndrzej Pietrasiewicz 
65500a2430fSAndrzej Pietrasiewicz 	switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
65600a2430fSAndrzej Pietrasiewicz 	case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
65700a2430fSAndrzej Pietrasiewicz 		  | HID_REQ_GET_REPORT):
65800a2430fSAndrzej Pietrasiewicz 		VDBG(cdev, "get_report\n");
65900a2430fSAndrzej Pietrasiewicz 
66000a2430fSAndrzej Pietrasiewicz 		/* send an empty report */
66100a2430fSAndrzej Pietrasiewicz 		length = min_t(unsigned, length, hidg->report_length);
66200a2430fSAndrzej Pietrasiewicz 		memset(req->buf, 0x0, length);
66300a2430fSAndrzej Pietrasiewicz 
66400a2430fSAndrzej Pietrasiewicz 		goto respond;
66500a2430fSAndrzej Pietrasiewicz 		break;
66600a2430fSAndrzej Pietrasiewicz 
66700a2430fSAndrzej Pietrasiewicz 	case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
66800a2430fSAndrzej Pietrasiewicz 		  | HID_REQ_GET_PROTOCOL):
66900a2430fSAndrzej Pietrasiewicz 		VDBG(cdev, "get_protocol\n");
670b3c4ec71SAbdulhadi Mohamed 		length = min_t(unsigned int, length, 1);
671b3c4ec71SAbdulhadi Mohamed 		((u8 *) req->buf)[0] = hidg->protocol;
672b3c4ec71SAbdulhadi Mohamed 		goto respond;
67300a2430fSAndrzej Pietrasiewicz 		break;
67400a2430fSAndrzej Pietrasiewicz 
675afcff6dcSMaxim Devaev 	case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
676afcff6dcSMaxim Devaev 		  | HID_REQ_GET_IDLE):
677afcff6dcSMaxim Devaev 		VDBG(cdev, "get_idle\n");
678afcff6dcSMaxim Devaev 		length = min_t(unsigned int, length, 1);
679afcff6dcSMaxim Devaev 		((u8 *) req->buf)[0] = hidg->idle;
680afcff6dcSMaxim Devaev 		goto respond;
681afcff6dcSMaxim Devaev 		break;
682afcff6dcSMaxim Devaev 
68300a2430fSAndrzej Pietrasiewicz 	case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
68400a2430fSAndrzej Pietrasiewicz 		  | HID_REQ_SET_REPORT):
6856774def6SMasanari Iida 		VDBG(cdev, "set_report | wLength=%d\n", ctrl->wLength);
686d7428bc2SMaxim Devaev 		if (hidg->use_out_ep)
68700a2430fSAndrzej Pietrasiewicz 			goto stall;
688d7428bc2SMaxim Devaev 		req->complete = hidg_ssreport_complete;
689d7428bc2SMaxim Devaev 		req->context  = hidg;
690d7428bc2SMaxim Devaev 		goto respond;
69100a2430fSAndrzej Pietrasiewicz 		break;
69200a2430fSAndrzej Pietrasiewicz 
69300a2430fSAndrzej Pietrasiewicz 	case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
69400a2430fSAndrzej Pietrasiewicz 		  | HID_REQ_SET_PROTOCOL):
69500a2430fSAndrzej Pietrasiewicz 		VDBG(cdev, "set_protocol\n");
696b3c4ec71SAbdulhadi Mohamed 		if (value > HID_REPORT_PROTOCOL)
697b3c4ec71SAbdulhadi Mohamed 			goto stall;
698b3c4ec71SAbdulhadi Mohamed 		length = 0;
699b3c4ec71SAbdulhadi Mohamed 		/*
700b3c4ec71SAbdulhadi Mohamed 		 * We assume that programs implementing the Boot protocol
701b3c4ec71SAbdulhadi Mohamed 		 * are also compatible with the Report Protocol
702b3c4ec71SAbdulhadi Mohamed 		 */
703b3c4ec71SAbdulhadi Mohamed 		if (hidg->bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT) {
704b3c4ec71SAbdulhadi Mohamed 			hidg->protocol = value;
705b3c4ec71SAbdulhadi Mohamed 			goto respond;
706b3c4ec71SAbdulhadi Mohamed 		}
70700a2430fSAndrzej Pietrasiewicz 		goto stall;
70800a2430fSAndrzej Pietrasiewicz 		break;
70900a2430fSAndrzej Pietrasiewicz 
710afcff6dcSMaxim Devaev 	case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
711afcff6dcSMaxim Devaev 		  | HID_REQ_SET_IDLE):
712afcff6dcSMaxim Devaev 		VDBG(cdev, "set_idle\n");
713afcff6dcSMaxim Devaev 		length = 0;
714fa20badaSMaxim Devaev 		hidg->idle = value >> 8;
715afcff6dcSMaxim Devaev 		goto respond;
716afcff6dcSMaxim Devaev 		break;
717afcff6dcSMaxim Devaev 
71800a2430fSAndrzej Pietrasiewicz 	case ((USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_INTERFACE) << 8
71900a2430fSAndrzej Pietrasiewicz 		  | USB_REQ_GET_DESCRIPTOR):
72000a2430fSAndrzej Pietrasiewicz 		switch (value >> 8) {
72100a2430fSAndrzej Pietrasiewicz 		case HID_DT_HID:
722f286d487SKrzysztof Opasiak 		{
723f286d487SKrzysztof Opasiak 			struct hid_descriptor hidg_desc_copy = hidg_desc;
724f286d487SKrzysztof Opasiak 
72500a2430fSAndrzej Pietrasiewicz 			VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: HID\n");
726f286d487SKrzysztof Opasiak 			hidg_desc_copy.desc[0].bDescriptorType = HID_DT_REPORT;
727f286d487SKrzysztof Opasiak 			hidg_desc_copy.desc[0].wDescriptorLength =
728f286d487SKrzysztof Opasiak 				cpu_to_le16(hidg->report_desc_length);
729f286d487SKrzysztof Opasiak 
73000a2430fSAndrzej Pietrasiewicz 			length = min_t(unsigned short, length,
731f286d487SKrzysztof Opasiak 						   hidg_desc_copy.bLength);
732f286d487SKrzysztof Opasiak 			memcpy(req->buf, &hidg_desc_copy, length);
73300a2430fSAndrzej Pietrasiewicz 			goto respond;
73400a2430fSAndrzej Pietrasiewicz 			break;
735f286d487SKrzysztof Opasiak 		}
73600a2430fSAndrzej Pietrasiewicz 		case HID_DT_REPORT:
73700a2430fSAndrzej Pietrasiewicz 			VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: REPORT\n");
73800a2430fSAndrzej Pietrasiewicz 			length = min_t(unsigned short, length,
73900a2430fSAndrzej Pietrasiewicz 						   hidg->report_desc_length);
74000a2430fSAndrzej Pietrasiewicz 			memcpy(req->buf, hidg->report_desc, length);
74100a2430fSAndrzej Pietrasiewicz 			goto respond;
74200a2430fSAndrzej Pietrasiewicz 			break;
74300a2430fSAndrzej Pietrasiewicz 
74400a2430fSAndrzej Pietrasiewicz 		default:
74500a2430fSAndrzej Pietrasiewicz 			VDBG(cdev, "Unknown descriptor request 0x%x\n",
74600a2430fSAndrzej Pietrasiewicz 				 value >> 8);
74700a2430fSAndrzej Pietrasiewicz 			goto stall;
74800a2430fSAndrzej Pietrasiewicz 			break;
74900a2430fSAndrzej Pietrasiewicz 		}
75000a2430fSAndrzej Pietrasiewicz 		break;
75100a2430fSAndrzej Pietrasiewicz 
75200a2430fSAndrzej Pietrasiewicz 	default:
75300a2430fSAndrzej Pietrasiewicz 		VDBG(cdev, "Unknown request 0x%x\n",
75400a2430fSAndrzej Pietrasiewicz 			 ctrl->bRequest);
75500a2430fSAndrzej Pietrasiewicz 		goto stall;
75600a2430fSAndrzej Pietrasiewicz 		break;
75700a2430fSAndrzej Pietrasiewicz 	}
75800a2430fSAndrzej Pietrasiewicz 
75900a2430fSAndrzej Pietrasiewicz stall:
76000a2430fSAndrzej Pietrasiewicz 	return -EOPNOTSUPP;
76100a2430fSAndrzej Pietrasiewicz 
76200a2430fSAndrzej Pietrasiewicz respond:
76300a2430fSAndrzej Pietrasiewicz 	req->zero = 0;
76400a2430fSAndrzej Pietrasiewicz 	req->length = length;
76500a2430fSAndrzej Pietrasiewicz 	status = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
76600a2430fSAndrzej Pietrasiewicz 	if (status < 0)
76700a2430fSAndrzej Pietrasiewicz 		ERROR(cdev, "usb_ep_queue error on ep0 %d\n", value);
76800a2430fSAndrzej Pietrasiewicz 	return status;
76900a2430fSAndrzej Pietrasiewicz }
77000a2430fSAndrzej Pietrasiewicz 
77100a2430fSAndrzej Pietrasiewicz static void hidg_disable(struct usb_function *f)
77200a2430fSAndrzej Pietrasiewicz {
77300a2430fSAndrzej Pietrasiewicz 	struct f_hidg *hidg = func_to_hidg(f);
77400a2430fSAndrzej Pietrasiewicz 	struct f_hidg_req_list *list, *next;
775aa65d11aSKrzysztof Opasiak 	unsigned long flags;
77600a2430fSAndrzej Pietrasiewicz 
77700a2430fSAndrzej Pietrasiewicz 	usb_ep_disable(hidg->in_ep);
778d7428bc2SMaxim Devaev 
779d7428bc2SMaxim Devaev 	if (hidg->out_ep) {
78000a2430fSAndrzej Pietrasiewicz 		usb_ep_disable(hidg->out_ep);
78100a2430fSAndrzej Pietrasiewicz 
78233e4c1a9SKrzysztof Opasiak 		spin_lock_irqsave(&hidg->read_spinlock, flags);
78300a2430fSAndrzej Pietrasiewicz 		list_for_each_entry_safe(list, next, &hidg->completed_out_req, list) {
784aa65d11aSKrzysztof Opasiak 			free_ep_req(hidg->out_ep, list->req);
78500a2430fSAndrzej Pietrasiewicz 			list_del(&list->list);
78600a2430fSAndrzej Pietrasiewicz 			kfree(list);
78700a2430fSAndrzej Pietrasiewicz 		}
78833e4c1a9SKrzysztof Opasiak 		spin_unlock_irqrestore(&hidg->read_spinlock, flags);
789d7428bc2SMaxim Devaev 	}
790749494b6SKrzysztof Opasiak 
791749494b6SKrzysztof Opasiak 	spin_lock_irqsave(&hidg->write_spinlock, flags);
792749494b6SKrzysztof Opasiak 	if (!hidg->write_pending) {
793749494b6SKrzysztof Opasiak 		free_ep_req(hidg->in_ep, hidg->req);
794749494b6SKrzysztof Opasiak 		hidg->write_pending = 1;
795749494b6SKrzysztof Opasiak 	}
796749494b6SKrzysztof Opasiak 
797749494b6SKrzysztof Opasiak 	hidg->req = NULL;
798749494b6SKrzysztof Opasiak 	spin_unlock_irqrestore(&hidg->write_spinlock, flags);
79900a2430fSAndrzej Pietrasiewicz }
80000a2430fSAndrzej Pietrasiewicz 
80100a2430fSAndrzej Pietrasiewicz static int hidg_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
80200a2430fSAndrzej Pietrasiewicz {
80300a2430fSAndrzej Pietrasiewicz 	struct usb_composite_dev		*cdev = f->config->cdev;
80400a2430fSAndrzej Pietrasiewicz 	struct f_hidg				*hidg = func_to_hidg(f);
805749494b6SKrzysztof Opasiak 	struct usb_request			*req_in = NULL;
806749494b6SKrzysztof Opasiak 	unsigned long				flags;
80700a2430fSAndrzej Pietrasiewicz 	int i, status = 0;
80800a2430fSAndrzej Pietrasiewicz 
80900a2430fSAndrzej Pietrasiewicz 	VDBG(cdev, "hidg_set_alt intf:%d alt:%d\n", intf, alt);
81000a2430fSAndrzej Pietrasiewicz 
81100a2430fSAndrzej Pietrasiewicz 	if (hidg->in_ep != NULL) {
81200a2430fSAndrzej Pietrasiewicz 		/* restart endpoint */
81300a2430fSAndrzej Pietrasiewicz 		usb_ep_disable(hidg->in_ep);
81400a2430fSAndrzej Pietrasiewicz 
81500a2430fSAndrzej Pietrasiewicz 		status = config_ep_by_speed(f->config->cdev->gadget, f,
81600a2430fSAndrzej Pietrasiewicz 					    hidg->in_ep);
81700a2430fSAndrzej Pietrasiewicz 		if (status) {
81800a2430fSAndrzej Pietrasiewicz 			ERROR(cdev, "config_ep_by_speed FAILED!\n");
81900a2430fSAndrzej Pietrasiewicz 			goto fail;
82000a2430fSAndrzej Pietrasiewicz 		}
82100a2430fSAndrzej Pietrasiewicz 		status = usb_ep_enable(hidg->in_ep);
82200a2430fSAndrzej Pietrasiewicz 		if (status < 0) {
82300a2430fSAndrzej Pietrasiewicz 			ERROR(cdev, "Enable IN endpoint FAILED!\n");
82400a2430fSAndrzej Pietrasiewicz 			goto fail;
82500a2430fSAndrzej Pietrasiewicz 		}
82600a2430fSAndrzej Pietrasiewicz 		hidg->in_ep->driver_data = hidg;
827749494b6SKrzysztof Opasiak 
828749494b6SKrzysztof Opasiak 		req_in = hidg_alloc_ep_req(hidg->in_ep, hidg->report_length);
829749494b6SKrzysztof Opasiak 		if (!req_in) {
830749494b6SKrzysztof Opasiak 			status = -ENOMEM;
831749494b6SKrzysztof Opasiak 			goto disable_ep_in;
832749494b6SKrzysztof Opasiak 		}
83300a2430fSAndrzej Pietrasiewicz 	}
83400a2430fSAndrzej Pietrasiewicz 
835d7428bc2SMaxim Devaev 	if (hidg->use_out_ep && hidg->out_ep != NULL) {
83600a2430fSAndrzej Pietrasiewicz 		/* restart endpoint */
83700a2430fSAndrzej Pietrasiewicz 		usb_ep_disable(hidg->out_ep);
83800a2430fSAndrzej Pietrasiewicz 
83900a2430fSAndrzej Pietrasiewicz 		status = config_ep_by_speed(f->config->cdev->gadget, f,
84000a2430fSAndrzej Pietrasiewicz 					    hidg->out_ep);
84100a2430fSAndrzej Pietrasiewicz 		if (status) {
84200a2430fSAndrzej Pietrasiewicz 			ERROR(cdev, "config_ep_by_speed FAILED!\n");
843749494b6SKrzysztof Opasiak 			goto free_req_in;
84400a2430fSAndrzej Pietrasiewicz 		}
84500a2430fSAndrzej Pietrasiewicz 		status = usb_ep_enable(hidg->out_ep);
84600a2430fSAndrzej Pietrasiewicz 		if (status < 0) {
84743aef5c2SDavid Lechner 			ERROR(cdev, "Enable OUT endpoint FAILED!\n");
848749494b6SKrzysztof Opasiak 			goto free_req_in;
84900a2430fSAndrzej Pietrasiewicz 		}
85000a2430fSAndrzej Pietrasiewicz 		hidg->out_ep->driver_data = hidg;
85100a2430fSAndrzej Pietrasiewicz 
85200a2430fSAndrzej Pietrasiewicz 		/*
85300a2430fSAndrzej Pietrasiewicz 		 * allocate a bunch of read buffers and queue them all at once.
85400a2430fSAndrzej Pietrasiewicz 		 */
85500a2430fSAndrzej Pietrasiewicz 		for (i = 0; i < hidg->qlen && status == 0; i++) {
85600a2430fSAndrzej Pietrasiewicz 			struct usb_request *req =
85700a2430fSAndrzej Pietrasiewicz 					hidg_alloc_ep_req(hidg->out_ep,
85800a2430fSAndrzej Pietrasiewicz 							  hidg->report_length);
85900a2430fSAndrzej Pietrasiewicz 			if (req) {
860d7428bc2SMaxim Devaev 				req->complete = hidg_intout_complete;
86100a2430fSAndrzej Pietrasiewicz 				req->context  = hidg;
86200a2430fSAndrzej Pietrasiewicz 				status = usb_ep_queue(hidg->out_ep, req,
86300a2430fSAndrzej Pietrasiewicz 						      GFP_ATOMIC);
864749494b6SKrzysztof Opasiak 				if (status) {
86500a2430fSAndrzej Pietrasiewicz 					ERROR(cdev, "%s queue req --> %d\n",
86600a2430fSAndrzej Pietrasiewicz 						hidg->out_ep->name, status);
867749494b6SKrzysztof Opasiak 					free_ep_req(hidg->out_ep, req);
868749494b6SKrzysztof Opasiak 				}
86900a2430fSAndrzej Pietrasiewicz 			} else {
87000a2430fSAndrzej Pietrasiewicz 				status = -ENOMEM;
871749494b6SKrzysztof Opasiak 				goto disable_out_ep;
87200a2430fSAndrzej Pietrasiewicz 			}
87300a2430fSAndrzej Pietrasiewicz 		}
87400a2430fSAndrzej Pietrasiewicz 	}
87500a2430fSAndrzej Pietrasiewicz 
876749494b6SKrzysztof Opasiak 	if (hidg->in_ep != NULL) {
877749494b6SKrzysztof Opasiak 		spin_lock_irqsave(&hidg->write_spinlock, flags);
878749494b6SKrzysztof Opasiak 		hidg->req = req_in;
879749494b6SKrzysztof Opasiak 		hidg->write_pending = 0;
880749494b6SKrzysztof Opasiak 		spin_unlock_irqrestore(&hidg->write_spinlock, flags);
881749494b6SKrzysztof Opasiak 
882749494b6SKrzysztof Opasiak 		wake_up(&hidg->write_queue);
883749494b6SKrzysztof Opasiak 	}
884749494b6SKrzysztof Opasiak 	return 0;
885749494b6SKrzysztof Opasiak disable_out_ep:
886d7428bc2SMaxim Devaev 	if (hidg->out_ep)
887749494b6SKrzysztof Opasiak 		usb_ep_disable(hidg->out_ep);
888749494b6SKrzysztof Opasiak free_req_in:
889749494b6SKrzysztof Opasiak 	if (req_in)
890749494b6SKrzysztof Opasiak 		free_ep_req(hidg->in_ep, req_in);
891749494b6SKrzysztof Opasiak 
892749494b6SKrzysztof Opasiak disable_ep_in:
893749494b6SKrzysztof Opasiak 	if (hidg->in_ep)
894749494b6SKrzysztof Opasiak 		usb_ep_disable(hidg->in_ep);
895749494b6SKrzysztof Opasiak 
89600a2430fSAndrzej Pietrasiewicz fail:
89700a2430fSAndrzej Pietrasiewicz 	return status;
89800a2430fSAndrzej Pietrasiewicz }
89900a2430fSAndrzej Pietrasiewicz 
9007a3cc461SLad, Prabhakar static const struct file_operations f_hidg_fops = {
90100a2430fSAndrzej Pietrasiewicz 	.owner		= THIS_MODULE,
90200a2430fSAndrzej Pietrasiewicz 	.open		= f_hidg_open,
90300a2430fSAndrzej Pietrasiewicz 	.release	= f_hidg_release,
90400a2430fSAndrzej Pietrasiewicz 	.write		= f_hidg_write,
90500a2430fSAndrzej Pietrasiewicz 	.read		= f_hidg_read,
90600a2430fSAndrzej Pietrasiewicz 	.poll		= f_hidg_poll,
90700a2430fSAndrzej Pietrasiewicz 	.llseek		= noop_llseek,
90800a2430fSAndrzej Pietrasiewicz };
90900a2430fSAndrzej Pietrasiewicz 
910cb382536SAndrzej Pietrasiewicz static int hidg_bind(struct usb_configuration *c, struct usb_function *f)
91100a2430fSAndrzej Pietrasiewicz {
91200a2430fSAndrzej Pietrasiewicz 	struct usb_ep		*ep;
91300a2430fSAndrzej Pietrasiewicz 	struct f_hidg		*hidg = func_to_hidg(f);
9145ca8d3ecSAndrzej Pietrasiewicz 	struct usb_string	*us;
91500a2430fSAndrzej Pietrasiewicz 	int			status;
91600a2430fSAndrzej Pietrasiewicz 
917cb382536SAndrzej Pietrasiewicz 	/* maybe allocate device-global string IDs, and patch descriptors */
9185ca8d3ecSAndrzej Pietrasiewicz 	us = usb_gstrings_attach(c->cdev, ct_func_strings,
9195ca8d3ecSAndrzej Pietrasiewicz 				 ARRAY_SIZE(ct_func_string_defs));
9205ca8d3ecSAndrzej Pietrasiewicz 	if (IS_ERR(us))
9215ca8d3ecSAndrzej Pietrasiewicz 		return PTR_ERR(us);
9225ca8d3ecSAndrzej Pietrasiewicz 	hidg_interface_desc.iInterface = us[CT_FUNC_HID_IDX].id;
923cb382536SAndrzej Pietrasiewicz 
92400a2430fSAndrzej Pietrasiewicz 	/* allocate instance-specific interface IDs, and patch descriptors */
92500a2430fSAndrzej Pietrasiewicz 	status = usb_interface_id(c, f);
92600a2430fSAndrzej Pietrasiewicz 	if (status < 0)
92700a2430fSAndrzej Pietrasiewicz 		goto fail;
92800a2430fSAndrzej Pietrasiewicz 	hidg_interface_desc.bInterfaceNumber = status;
92900a2430fSAndrzej Pietrasiewicz 
93000a2430fSAndrzej Pietrasiewicz 	/* allocate instance-specific endpoints */
93100a2430fSAndrzej Pietrasiewicz 	status = -ENODEV;
93200a2430fSAndrzej Pietrasiewicz 	ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_in_ep_desc);
93300a2430fSAndrzej Pietrasiewicz 	if (!ep)
93400a2430fSAndrzej Pietrasiewicz 		goto fail;
93500a2430fSAndrzej Pietrasiewicz 	hidg->in_ep = ep;
93600a2430fSAndrzej Pietrasiewicz 
937d7428bc2SMaxim Devaev 	hidg->out_ep = NULL;
938d7428bc2SMaxim Devaev 	if (hidg->use_out_ep) {
93900a2430fSAndrzej Pietrasiewicz 		ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_out_ep_desc);
94000a2430fSAndrzej Pietrasiewicz 		if (!ep)
94100a2430fSAndrzej Pietrasiewicz 			goto fail;
94200a2430fSAndrzej Pietrasiewicz 		hidg->out_ep = ep;
943d7428bc2SMaxim Devaev 	}
944d7428bc2SMaxim Devaev 
945d7428bc2SMaxim Devaev 	/* used only if use_out_ep == 1 */
946d7428bc2SMaxim Devaev 	hidg->set_report_buf = NULL;
94700a2430fSAndrzej Pietrasiewicz 
94800a2430fSAndrzej Pietrasiewicz 	/* set descriptor dynamic values */
94900a2430fSAndrzej Pietrasiewicz 	hidg_interface_desc.bInterfaceSubClass = hidg->bInterfaceSubClass;
95000a2430fSAndrzej Pietrasiewicz 	hidg_interface_desc.bInterfaceProtocol = hidg->bInterfaceProtocol;
951d7428bc2SMaxim Devaev 	hidg_interface_desc.bNumEndpoints = hidg->use_out_ep ? 2 : 1;
952b3c4ec71SAbdulhadi Mohamed 	hidg->protocol = HID_REPORT_PROTOCOL;
953afcff6dcSMaxim Devaev 	hidg->idle = 1;
954dbf499cfSJanusz Dziedzic 	hidg_ss_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
955dbf499cfSJanusz Dziedzic 	hidg_ss_in_comp_desc.wBytesPerInterval =
956dbf499cfSJanusz Dziedzic 				cpu_to_le16(hidg->report_length);
95700a2430fSAndrzej Pietrasiewicz 	hidg_hs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
95800a2430fSAndrzej Pietrasiewicz 	hidg_fs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
959dbf499cfSJanusz Dziedzic 	hidg_ss_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
960dbf499cfSJanusz Dziedzic 	hidg_ss_out_comp_desc.wBytesPerInterval =
961dbf499cfSJanusz Dziedzic 				cpu_to_le16(hidg->report_length);
96200a2430fSAndrzej Pietrasiewicz 	hidg_hs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
96300a2430fSAndrzej Pietrasiewicz 	hidg_fs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
964f286d487SKrzysztof Opasiak 	/*
965f286d487SKrzysztof Opasiak 	 * We can use hidg_desc struct here but we should not relay
966f286d487SKrzysztof Opasiak 	 * that its content won't change after returning from this function.
967f286d487SKrzysztof Opasiak 	 */
96800a2430fSAndrzej Pietrasiewicz 	hidg_desc.desc[0].bDescriptorType = HID_DT_REPORT;
96900a2430fSAndrzej Pietrasiewicz 	hidg_desc.desc[0].wDescriptorLength =
97000a2430fSAndrzej Pietrasiewicz 		cpu_to_le16(hidg->report_desc_length);
97100a2430fSAndrzej Pietrasiewicz 
97200a2430fSAndrzej Pietrasiewicz 	hidg_hs_in_ep_desc.bEndpointAddress =
97300a2430fSAndrzej Pietrasiewicz 		hidg_fs_in_ep_desc.bEndpointAddress;
97400a2430fSAndrzej Pietrasiewicz 	hidg_hs_out_ep_desc.bEndpointAddress =
97500a2430fSAndrzej Pietrasiewicz 		hidg_fs_out_ep_desc.bEndpointAddress;
97600a2430fSAndrzej Pietrasiewicz 
977dbf499cfSJanusz Dziedzic 	hidg_ss_in_ep_desc.bEndpointAddress =
978dbf499cfSJanusz Dziedzic 		hidg_fs_in_ep_desc.bEndpointAddress;
979dbf499cfSJanusz Dziedzic 	hidg_ss_out_ep_desc.bEndpointAddress =
980dbf499cfSJanusz Dziedzic 		hidg_fs_out_ep_desc.bEndpointAddress;
981dbf499cfSJanusz Dziedzic 
982d7428bc2SMaxim Devaev 	if (hidg->use_out_ep)
983d7428bc2SMaxim Devaev 		status = usb_assign_descriptors(f,
984d7428bc2SMaxim Devaev 			hidg_fs_descriptors_intout,
985d7428bc2SMaxim Devaev 			hidg_hs_descriptors_intout,
986d7428bc2SMaxim Devaev 			hidg_ss_descriptors_intout,
987d7428bc2SMaxim Devaev 			hidg_ss_descriptors_intout);
988d7428bc2SMaxim Devaev 	else
989d7428bc2SMaxim Devaev 		status = usb_assign_descriptors(f,
990d7428bc2SMaxim Devaev 			hidg_fs_descriptors_ssreport,
991d7428bc2SMaxim Devaev 			hidg_hs_descriptors_ssreport,
992d7428bc2SMaxim Devaev 			hidg_ss_descriptors_ssreport,
993d7428bc2SMaxim Devaev 			hidg_ss_descriptors_ssreport);
994d7428bc2SMaxim Devaev 
99500a2430fSAndrzej Pietrasiewicz 	if (status)
99600a2430fSAndrzej Pietrasiewicz 		goto fail;
99700a2430fSAndrzej Pietrasiewicz 
99833e4c1a9SKrzysztof Opasiak 	spin_lock_init(&hidg->write_spinlock);
999749494b6SKrzysztof Opasiak 	hidg->write_pending = 1;
1000749494b6SKrzysztof Opasiak 	hidg->req = NULL;
100133e4c1a9SKrzysztof Opasiak 	spin_lock_init(&hidg->read_spinlock);
100200a2430fSAndrzej Pietrasiewicz 	init_waitqueue_head(&hidg->write_queue);
100300a2430fSAndrzej Pietrasiewicz 	init_waitqueue_head(&hidg->read_queue);
100400a2430fSAndrzej Pietrasiewicz 	INIT_LIST_HEAD(&hidg->completed_out_req);
100500a2430fSAndrzej Pietrasiewicz 
100600a2430fSAndrzej Pietrasiewicz 	/* create char device */
100700a2430fSAndrzej Pietrasiewicz 	cdev_init(&hidg->cdev, &f_hidg_fops);
1008*89ff3dfaSJohn Keeping 	status = cdev_device_add(&hidg->cdev, &hidg->dev);
100900a2430fSAndrzej Pietrasiewicz 	if (status)
1010d12a8727SPavitrakumar Managutte 		goto fail_free_descs;
101100a2430fSAndrzej Pietrasiewicz 
101200a2430fSAndrzej Pietrasiewicz 	return 0;
1013d12a8727SPavitrakumar Managutte fail_free_descs:
1014d12a8727SPavitrakumar Managutte 	usb_free_all_descriptors(f);
101500a2430fSAndrzej Pietrasiewicz fail:
101600a2430fSAndrzej Pietrasiewicz 	ERROR(f->config->cdev, "hidg_bind FAILED\n");
101714794d71SFelipe F. Tonello 	if (hidg->req != NULL)
101814794d71SFelipe F. Tonello 		free_ep_req(hidg->in_ep, hidg->req);
101900a2430fSAndrzej Pietrasiewicz 
102000a2430fSAndrzej Pietrasiewicz 	return status;
102100a2430fSAndrzej Pietrasiewicz }
102200a2430fSAndrzej Pietrasiewicz 
1023cb382536SAndrzej Pietrasiewicz static inline int hidg_get_minor(void)
1024cb382536SAndrzej Pietrasiewicz {
1025cb382536SAndrzej Pietrasiewicz 	int ret;
1026cb382536SAndrzej Pietrasiewicz 
1027cb382536SAndrzej Pietrasiewicz 	ret = ida_simple_get(&hidg_ida, 0, 0, GFP_KERNEL);
1028774cf72fSAndrzej Pietrasiewicz 	if (ret >= HIDG_MINORS) {
1029774cf72fSAndrzej Pietrasiewicz 		ida_simple_remove(&hidg_ida, ret);
1030774cf72fSAndrzej Pietrasiewicz 		ret = -ENODEV;
1031774cf72fSAndrzej Pietrasiewicz 	}
1032cb382536SAndrzej Pietrasiewicz 
1033cb382536SAndrzej Pietrasiewicz 	return ret;
1034cb382536SAndrzej Pietrasiewicz }
1035cb382536SAndrzej Pietrasiewicz 
103621a9476aSAndrzej Pietrasiewicz static inline struct f_hid_opts *to_f_hid_opts(struct config_item *item)
103721a9476aSAndrzej Pietrasiewicz {
103821a9476aSAndrzej Pietrasiewicz 	return container_of(to_config_group(item), struct f_hid_opts,
103921a9476aSAndrzej Pietrasiewicz 			    func_inst.group);
104021a9476aSAndrzej Pietrasiewicz }
104121a9476aSAndrzej Pietrasiewicz 
104221a9476aSAndrzej Pietrasiewicz static void hid_attr_release(struct config_item *item)
104321a9476aSAndrzej Pietrasiewicz {
104421a9476aSAndrzej Pietrasiewicz 	struct f_hid_opts *opts = to_f_hid_opts(item);
104521a9476aSAndrzej Pietrasiewicz 
104621a9476aSAndrzej Pietrasiewicz 	usb_put_function_instance(&opts->func_inst);
104721a9476aSAndrzej Pietrasiewicz }
104821a9476aSAndrzej Pietrasiewicz 
104921a9476aSAndrzej Pietrasiewicz static struct configfs_item_operations hidg_item_ops = {
105021a9476aSAndrzej Pietrasiewicz 	.release	= hid_attr_release,
105121a9476aSAndrzej Pietrasiewicz };
105221a9476aSAndrzej Pietrasiewicz 
105321a9476aSAndrzej Pietrasiewicz #define F_HID_OPT(name, prec, limit)					\
1054da4e527cSChristoph Hellwig static ssize_t f_hid_opts_##name##_show(struct config_item *item, char *page)\
105521a9476aSAndrzej Pietrasiewicz {									\
1056da4e527cSChristoph Hellwig 	struct f_hid_opts *opts = to_f_hid_opts(item);			\
105721a9476aSAndrzej Pietrasiewicz 	int result;							\
105821a9476aSAndrzej Pietrasiewicz 									\
105921a9476aSAndrzej Pietrasiewicz 	mutex_lock(&opts->lock);					\
106021a9476aSAndrzej Pietrasiewicz 	result = sprintf(page, "%d\n", opts->name);			\
106121a9476aSAndrzej Pietrasiewicz 	mutex_unlock(&opts->lock);					\
106221a9476aSAndrzej Pietrasiewicz 									\
106321a9476aSAndrzej Pietrasiewicz 	return result;							\
106421a9476aSAndrzej Pietrasiewicz }									\
106521a9476aSAndrzej Pietrasiewicz 									\
1066da4e527cSChristoph Hellwig static ssize_t f_hid_opts_##name##_store(struct config_item *item,	\
106721a9476aSAndrzej Pietrasiewicz 					 const char *page, size_t len)	\
106821a9476aSAndrzej Pietrasiewicz {									\
1069da4e527cSChristoph Hellwig 	struct f_hid_opts *opts = to_f_hid_opts(item);			\
107021a9476aSAndrzej Pietrasiewicz 	int ret;							\
107121a9476aSAndrzej Pietrasiewicz 	u##prec num;							\
107221a9476aSAndrzej Pietrasiewicz 									\
107321a9476aSAndrzej Pietrasiewicz 	mutex_lock(&opts->lock);					\
107421a9476aSAndrzej Pietrasiewicz 	if (opts->refcnt) {						\
107521a9476aSAndrzej Pietrasiewicz 		ret = -EBUSY;						\
107621a9476aSAndrzej Pietrasiewicz 		goto end;						\
107721a9476aSAndrzej Pietrasiewicz 	}								\
107821a9476aSAndrzej Pietrasiewicz 									\
107921a9476aSAndrzej Pietrasiewicz 	ret = kstrtou##prec(page, 0, &num);				\
108021a9476aSAndrzej Pietrasiewicz 	if (ret)							\
108121a9476aSAndrzej Pietrasiewicz 		goto end;						\
108221a9476aSAndrzej Pietrasiewicz 									\
108321a9476aSAndrzej Pietrasiewicz 	if (num > limit) {						\
108421a9476aSAndrzej Pietrasiewicz 		ret = -EINVAL;						\
108521a9476aSAndrzej Pietrasiewicz 		goto end;						\
108621a9476aSAndrzej Pietrasiewicz 	}								\
108721a9476aSAndrzej Pietrasiewicz 	opts->name = num;						\
108821a9476aSAndrzej Pietrasiewicz 	ret = len;							\
108921a9476aSAndrzej Pietrasiewicz 									\
109021a9476aSAndrzej Pietrasiewicz end:									\
109121a9476aSAndrzej Pietrasiewicz 	mutex_unlock(&opts->lock);					\
109221a9476aSAndrzej Pietrasiewicz 	return ret;							\
109321a9476aSAndrzej Pietrasiewicz }									\
109421a9476aSAndrzej Pietrasiewicz 									\
1095da4e527cSChristoph Hellwig CONFIGFS_ATTR(f_hid_opts_, name)
109621a9476aSAndrzej Pietrasiewicz 
109721a9476aSAndrzej Pietrasiewicz F_HID_OPT(subclass, 8, 255);
109821a9476aSAndrzej Pietrasiewicz F_HID_OPT(protocol, 8, 255);
1099d7428bc2SMaxim Devaev F_HID_OPT(no_out_endpoint, 8, 1);
110039a2ac27SAndrzej Pietrasiewicz F_HID_OPT(report_length, 16, 65535);
110121a9476aSAndrzej Pietrasiewicz 
1102da4e527cSChristoph Hellwig static ssize_t f_hid_opts_report_desc_show(struct config_item *item, char *page)
110321a9476aSAndrzej Pietrasiewicz {
1104da4e527cSChristoph Hellwig 	struct f_hid_opts *opts = to_f_hid_opts(item);
110521a9476aSAndrzej Pietrasiewicz 	int result;
110621a9476aSAndrzej Pietrasiewicz 
110721a9476aSAndrzej Pietrasiewicz 	mutex_lock(&opts->lock);
110821a9476aSAndrzej Pietrasiewicz 	result = opts->report_desc_length;
110921a9476aSAndrzej Pietrasiewicz 	memcpy(page, opts->report_desc, opts->report_desc_length);
111021a9476aSAndrzej Pietrasiewicz 	mutex_unlock(&opts->lock);
111121a9476aSAndrzej Pietrasiewicz 
111221a9476aSAndrzej Pietrasiewicz 	return result;
111321a9476aSAndrzej Pietrasiewicz }
111421a9476aSAndrzej Pietrasiewicz 
1115da4e527cSChristoph Hellwig static ssize_t f_hid_opts_report_desc_store(struct config_item *item,
111621a9476aSAndrzej Pietrasiewicz 					    const char *page, size_t len)
111721a9476aSAndrzej Pietrasiewicz {
1118da4e527cSChristoph Hellwig 	struct f_hid_opts *opts = to_f_hid_opts(item);
111921a9476aSAndrzej Pietrasiewicz 	int ret = -EBUSY;
112021a9476aSAndrzej Pietrasiewicz 	char *d;
112121a9476aSAndrzej Pietrasiewicz 
112221a9476aSAndrzej Pietrasiewicz 	mutex_lock(&opts->lock);
112321a9476aSAndrzej Pietrasiewicz 
112421a9476aSAndrzej Pietrasiewicz 	if (opts->refcnt)
112521a9476aSAndrzej Pietrasiewicz 		goto end;
112621a9476aSAndrzej Pietrasiewicz 	if (len > PAGE_SIZE) {
112721a9476aSAndrzej Pietrasiewicz 		ret = -ENOSPC;
112821a9476aSAndrzej Pietrasiewicz 		goto end;
112921a9476aSAndrzej Pietrasiewicz 	}
113021a9476aSAndrzej Pietrasiewicz 	d = kmemdup(page, len, GFP_KERNEL);
113121a9476aSAndrzej Pietrasiewicz 	if (!d) {
113221a9476aSAndrzej Pietrasiewicz 		ret = -ENOMEM;
113321a9476aSAndrzej Pietrasiewicz 		goto end;
113421a9476aSAndrzej Pietrasiewicz 	}
113521a9476aSAndrzej Pietrasiewicz 	kfree(opts->report_desc);
113621a9476aSAndrzej Pietrasiewicz 	opts->report_desc = d;
113721a9476aSAndrzej Pietrasiewicz 	opts->report_desc_length = len;
113821a9476aSAndrzej Pietrasiewicz 	opts->report_desc_alloc = true;
113921a9476aSAndrzej Pietrasiewicz 	ret = len;
114021a9476aSAndrzej Pietrasiewicz end:
114121a9476aSAndrzej Pietrasiewicz 	mutex_unlock(&opts->lock);
114221a9476aSAndrzej Pietrasiewicz 	return ret;
114321a9476aSAndrzej Pietrasiewicz }
114421a9476aSAndrzej Pietrasiewicz 
1145da4e527cSChristoph Hellwig CONFIGFS_ATTR(f_hid_opts_, report_desc);
114621a9476aSAndrzej Pietrasiewicz 
1147ed6fe1f5SJohannes Berg static ssize_t f_hid_opts_dev_show(struct config_item *item, char *page)
1148ed6fe1f5SJohannes Berg {
1149ed6fe1f5SJohannes Berg 	struct f_hid_opts *opts = to_f_hid_opts(item);
1150ed6fe1f5SJohannes Berg 
1151ed6fe1f5SJohannes Berg 	return sprintf(page, "%d:%d\n", major, opts->minor);
1152ed6fe1f5SJohannes Berg }
1153ed6fe1f5SJohannes Berg 
1154ed6fe1f5SJohannes Berg CONFIGFS_ATTR_RO(f_hid_opts_, dev);
1155ed6fe1f5SJohannes Berg 
115621a9476aSAndrzej Pietrasiewicz static struct configfs_attribute *hid_attrs[] = {
1157da4e527cSChristoph Hellwig 	&f_hid_opts_attr_subclass,
1158da4e527cSChristoph Hellwig 	&f_hid_opts_attr_protocol,
1159d7428bc2SMaxim Devaev 	&f_hid_opts_attr_no_out_endpoint,
1160da4e527cSChristoph Hellwig 	&f_hid_opts_attr_report_length,
1161da4e527cSChristoph Hellwig 	&f_hid_opts_attr_report_desc,
1162ed6fe1f5SJohannes Berg 	&f_hid_opts_attr_dev,
116321a9476aSAndrzej Pietrasiewicz 	NULL,
116421a9476aSAndrzej Pietrasiewicz };
116521a9476aSAndrzej Pietrasiewicz 
116697363902SBhumika Goyal static const struct config_item_type hid_func_type = {
116721a9476aSAndrzej Pietrasiewicz 	.ct_item_ops	= &hidg_item_ops,
116821a9476aSAndrzej Pietrasiewicz 	.ct_attrs	= hid_attrs,
116921a9476aSAndrzej Pietrasiewicz 	.ct_owner	= THIS_MODULE,
117021a9476aSAndrzej Pietrasiewicz };
117121a9476aSAndrzej Pietrasiewicz 
1172cb382536SAndrzej Pietrasiewicz static inline void hidg_put_minor(int minor)
1173cb382536SAndrzej Pietrasiewicz {
1174cb382536SAndrzej Pietrasiewicz 	ida_simple_remove(&hidg_ida, minor);
1175cb382536SAndrzej Pietrasiewicz }
1176cb382536SAndrzej Pietrasiewicz 
1177cb382536SAndrzej Pietrasiewicz static void hidg_free_inst(struct usb_function_instance *f)
1178cb382536SAndrzej Pietrasiewicz {
1179cb382536SAndrzej Pietrasiewicz 	struct f_hid_opts *opts;
1180cb382536SAndrzej Pietrasiewicz 
1181cb382536SAndrzej Pietrasiewicz 	opts = container_of(f, struct f_hid_opts, func_inst);
1182cb382536SAndrzej Pietrasiewicz 
1183cb382536SAndrzej Pietrasiewicz 	mutex_lock(&hidg_ida_lock);
1184cb382536SAndrzej Pietrasiewicz 
1185cb382536SAndrzej Pietrasiewicz 	hidg_put_minor(opts->minor);
118699c49407SMatthew Wilcox 	if (ida_is_empty(&hidg_ida))
1187cb382536SAndrzej Pietrasiewicz 		ghid_cleanup();
1188cb382536SAndrzej Pietrasiewicz 
1189cb382536SAndrzej Pietrasiewicz 	mutex_unlock(&hidg_ida_lock);
1190cb382536SAndrzej Pietrasiewicz 
1191cb382536SAndrzej Pietrasiewicz 	if (opts->report_desc_alloc)
1192cb382536SAndrzej Pietrasiewicz 		kfree(opts->report_desc);
1193cb382536SAndrzej Pietrasiewicz 
1194cb382536SAndrzej Pietrasiewicz 	kfree(opts);
1195cb382536SAndrzej Pietrasiewicz }
1196cb382536SAndrzej Pietrasiewicz 
1197cb382536SAndrzej Pietrasiewicz static struct usb_function_instance *hidg_alloc_inst(void)
1198cb382536SAndrzej Pietrasiewicz {
1199cb382536SAndrzej Pietrasiewicz 	struct f_hid_opts *opts;
1200cb382536SAndrzej Pietrasiewicz 	struct usb_function_instance *ret;
1201cb382536SAndrzej Pietrasiewicz 	int status = 0;
1202cb382536SAndrzej Pietrasiewicz 
1203cb382536SAndrzej Pietrasiewicz 	opts = kzalloc(sizeof(*opts), GFP_KERNEL);
1204cb382536SAndrzej Pietrasiewicz 	if (!opts)
1205cb382536SAndrzej Pietrasiewicz 		return ERR_PTR(-ENOMEM);
120621a9476aSAndrzej Pietrasiewicz 	mutex_init(&opts->lock);
1207cb382536SAndrzej Pietrasiewicz 	opts->func_inst.free_func_inst = hidg_free_inst;
1208cb382536SAndrzej Pietrasiewicz 	ret = &opts->func_inst;
1209cb382536SAndrzej Pietrasiewicz 
1210cb382536SAndrzej Pietrasiewicz 	mutex_lock(&hidg_ida_lock);
1211cb382536SAndrzej Pietrasiewicz 
121299c49407SMatthew Wilcox 	if (ida_is_empty(&hidg_ida)) {
1213cb382536SAndrzej Pietrasiewicz 		status = ghid_setup(NULL, HIDG_MINORS);
1214cb382536SAndrzej Pietrasiewicz 		if (status)  {
1215cb382536SAndrzej Pietrasiewicz 			ret = ERR_PTR(status);
1216cb382536SAndrzej Pietrasiewicz 			kfree(opts);
1217cb382536SAndrzej Pietrasiewicz 			goto unlock;
1218cb382536SAndrzej Pietrasiewicz 		}
1219cb382536SAndrzej Pietrasiewicz 	}
1220cb382536SAndrzej Pietrasiewicz 
1221cb382536SAndrzej Pietrasiewicz 	opts->minor = hidg_get_minor();
1222cb382536SAndrzej Pietrasiewicz 	if (opts->minor < 0) {
1223cb382536SAndrzej Pietrasiewicz 		ret = ERR_PTR(opts->minor);
1224cb382536SAndrzej Pietrasiewicz 		kfree(opts);
122599c49407SMatthew Wilcox 		if (ida_is_empty(&hidg_ida))
1226cb382536SAndrzej Pietrasiewicz 			ghid_cleanup();
1227828f6148SDan Carpenter 		goto unlock;
1228cb382536SAndrzej Pietrasiewicz 	}
122921a9476aSAndrzej Pietrasiewicz 	config_group_init_type_name(&opts->func_inst.group, "", &hid_func_type);
1230cb382536SAndrzej Pietrasiewicz 
1231cb382536SAndrzej Pietrasiewicz unlock:
1232cb382536SAndrzej Pietrasiewicz 	mutex_unlock(&hidg_ida_lock);
1233cb382536SAndrzej Pietrasiewicz 	return ret;
1234cb382536SAndrzej Pietrasiewicz }
1235cb382536SAndrzej Pietrasiewicz 
1236cb382536SAndrzej Pietrasiewicz static void hidg_free(struct usb_function *f)
1237cb382536SAndrzej Pietrasiewicz {
1238cb382536SAndrzej Pietrasiewicz 	struct f_hidg *hidg;
123921a9476aSAndrzej Pietrasiewicz 	struct f_hid_opts *opts;
1240cb382536SAndrzej Pietrasiewicz 
1241cb382536SAndrzej Pietrasiewicz 	hidg = func_to_hidg(f);
124221a9476aSAndrzej Pietrasiewicz 	opts = container_of(f->fi, struct f_hid_opts, func_inst);
1243*89ff3dfaSJohn Keeping 	put_device(&hidg->dev);
124421a9476aSAndrzej Pietrasiewicz 	mutex_lock(&opts->lock);
124521a9476aSAndrzej Pietrasiewicz 	--opts->refcnt;
124621a9476aSAndrzej Pietrasiewicz 	mutex_unlock(&opts->lock);
1247cb382536SAndrzej Pietrasiewicz }
1248cb382536SAndrzej Pietrasiewicz 
124900a2430fSAndrzej Pietrasiewicz static void hidg_unbind(struct usb_configuration *c, struct usb_function *f)
125000a2430fSAndrzej Pietrasiewicz {
125100a2430fSAndrzej Pietrasiewicz 	struct f_hidg *hidg = func_to_hidg(f);
125200a2430fSAndrzej Pietrasiewicz 
1253*89ff3dfaSJohn Keeping 	cdev_device_del(&hidg->cdev, &hidg->dev);
125400a2430fSAndrzej Pietrasiewicz 
125500a2430fSAndrzej Pietrasiewicz 	usb_free_all_descriptors(f);
125600a2430fSAndrzej Pietrasiewicz }
125700a2430fSAndrzej Pietrasiewicz 
12580fc57ea0SFengguang Wu static struct usb_function *hidg_alloc(struct usb_function_instance *fi)
125900a2430fSAndrzej Pietrasiewicz {
126000a2430fSAndrzej Pietrasiewicz 	struct f_hidg *hidg;
1261cb382536SAndrzej Pietrasiewicz 	struct f_hid_opts *opts;
1262*89ff3dfaSJohn Keeping 	int ret;
126300a2430fSAndrzej Pietrasiewicz 
126400a2430fSAndrzej Pietrasiewicz 	/* allocate and initialize one new instance */
1265cb382536SAndrzej Pietrasiewicz 	hidg = kzalloc(sizeof(*hidg), GFP_KERNEL);
126600a2430fSAndrzej Pietrasiewicz 	if (!hidg)
1267cb382536SAndrzej Pietrasiewicz 		return ERR_PTR(-ENOMEM);
126800a2430fSAndrzej Pietrasiewicz 
1269cb382536SAndrzej Pietrasiewicz 	opts = container_of(fi, struct f_hid_opts, func_inst);
1270cb382536SAndrzej Pietrasiewicz 
127121a9476aSAndrzej Pietrasiewicz 	mutex_lock(&opts->lock);
127221a9476aSAndrzej Pietrasiewicz 	++opts->refcnt;
127321a9476aSAndrzej Pietrasiewicz 
1274*89ff3dfaSJohn Keeping 	device_initialize(&hidg->dev);
1275*89ff3dfaSJohn Keeping 	hidg->dev.release = hidg_release;
1276*89ff3dfaSJohn Keeping 	hidg->dev.class = hidg_class;
1277*89ff3dfaSJohn Keeping 	hidg->dev.devt = MKDEV(major, opts->minor);
1278*89ff3dfaSJohn Keeping 	ret = dev_set_name(&hidg->dev, "hidg%d", opts->minor);
1279*89ff3dfaSJohn Keeping 	if (ret) {
1280*89ff3dfaSJohn Keeping 		--opts->refcnt;
1281*89ff3dfaSJohn Keeping 		mutex_unlock(&opts->lock);
1282*89ff3dfaSJohn Keeping 		return ERR_PTR(ret);
1283*89ff3dfaSJohn Keeping 	}
1284*89ff3dfaSJohn Keeping 
1285cb382536SAndrzej Pietrasiewicz 	hidg->bInterfaceSubClass = opts->subclass;
1286cb382536SAndrzej Pietrasiewicz 	hidg->bInterfaceProtocol = opts->protocol;
1287cb382536SAndrzej Pietrasiewicz 	hidg->report_length = opts->report_length;
1288cb382536SAndrzej Pietrasiewicz 	hidg->report_desc_length = opts->report_desc_length;
1289cb382536SAndrzej Pietrasiewicz 	if (opts->report_desc) {
1290*89ff3dfaSJohn Keeping 		hidg->report_desc = devm_kmemdup(&hidg->dev, opts->report_desc,
1291cb382536SAndrzej Pietrasiewicz 						 opts->report_desc_length,
129200a2430fSAndrzej Pietrasiewicz 						 GFP_KERNEL);
129300a2430fSAndrzej Pietrasiewicz 		if (!hidg->report_desc) {
1294*89ff3dfaSJohn Keeping 			put_device(&hidg->dev);
129521a9476aSAndrzej Pietrasiewicz 			mutex_unlock(&opts->lock);
1296cb382536SAndrzej Pietrasiewicz 			return ERR_PTR(-ENOMEM);
1297cb382536SAndrzej Pietrasiewicz 		}
129800a2430fSAndrzej Pietrasiewicz 	}
1299d7428bc2SMaxim Devaev 	hidg->use_out_ep = !opts->no_out_endpoint;
130000a2430fSAndrzej Pietrasiewicz 
130121a9476aSAndrzej Pietrasiewicz 	mutex_unlock(&opts->lock);
130221a9476aSAndrzej Pietrasiewicz 
130300a2430fSAndrzej Pietrasiewicz 	hidg->func.name    = "hid";
130400a2430fSAndrzej Pietrasiewicz 	hidg->func.bind    = hidg_bind;
130500a2430fSAndrzej Pietrasiewicz 	hidg->func.unbind  = hidg_unbind;
130600a2430fSAndrzej Pietrasiewicz 	hidg->func.set_alt = hidg_set_alt;
130700a2430fSAndrzej Pietrasiewicz 	hidg->func.disable = hidg_disable;
130800a2430fSAndrzej Pietrasiewicz 	hidg->func.setup   = hidg_setup;
1309cb382536SAndrzej Pietrasiewicz 	hidg->func.free_func = hidg_free;
131000a2430fSAndrzej Pietrasiewicz 
131129a812e4SWei Ming Chen 	/* this could be made configurable at some point */
131200a2430fSAndrzej Pietrasiewicz 	hidg->qlen	   = 4;
131300a2430fSAndrzej Pietrasiewicz 
1314cb382536SAndrzej Pietrasiewicz 	return &hidg->func;
131500a2430fSAndrzej Pietrasiewicz }
131600a2430fSAndrzej Pietrasiewicz 
1317cb382536SAndrzej Pietrasiewicz DECLARE_USB_FUNCTION_INIT(hid, hidg_alloc_inst, hidg_alloc);
1318cb382536SAndrzej Pietrasiewicz MODULE_LICENSE("GPL");
1319cb382536SAndrzej Pietrasiewicz MODULE_AUTHOR("Fabien Chouteau");
1320cb382536SAndrzej Pietrasiewicz 
1321cb382536SAndrzej Pietrasiewicz int ghid_setup(struct usb_gadget *g, int count)
132200a2430fSAndrzej Pietrasiewicz {
132300a2430fSAndrzej Pietrasiewicz 	int status;
132400a2430fSAndrzej Pietrasiewicz 	dev_t dev;
132500a2430fSAndrzej Pietrasiewicz 
132600a2430fSAndrzej Pietrasiewicz 	hidg_class = class_create(THIS_MODULE, "hidg");
132706529407SAndrzej Pietrasiewicz 	if (IS_ERR(hidg_class)) {
13280448d38cSDan Carpenter 		status = PTR_ERR(hidg_class);
132906529407SAndrzej Pietrasiewicz 		hidg_class = NULL;
13300448d38cSDan Carpenter 		return status;
133100a2430fSAndrzej Pietrasiewicz 	}
133200a2430fSAndrzej Pietrasiewicz 
133300a2430fSAndrzej Pietrasiewicz 	status = alloc_chrdev_region(&dev, 0, count, "hidg");
13340448d38cSDan Carpenter 	if (status) {
13350448d38cSDan Carpenter 		class_destroy(hidg_class);
13360448d38cSDan Carpenter 		hidg_class = NULL;
133700a2430fSAndrzej Pietrasiewicz 		return status;
133800a2430fSAndrzej Pietrasiewicz 	}
133900a2430fSAndrzej Pietrasiewicz 
13400448d38cSDan Carpenter 	major = MAJOR(dev);
13410448d38cSDan Carpenter 	minors = count;
13420448d38cSDan Carpenter 
13430448d38cSDan Carpenter 	return 0;
134400a2430fSAndrzej Pietrasiewicz }
134500a2430fSAndrzej Pietrasiewicz 
134600a2430fSAndrzej Pietrasiewicz void ghid_cleanup(void)
134700a2430fSAndrzej Pietrasiewicz {
134800a2430fSAndrzej Pietrasiewicz 	if (major) {
134900a2430fSAndrzej Pietrasiewicz 		unregister_chrdev_region(MKDEV(major, 0), minors);
135000a2430fSAndrzej Pietrasiewicz 		major = minors = 0;
135100a2430fSAndrzej Pietrasiewicz 	}
135200a2430fSAndrzej Pietrasiewicz 
135300a2430fSAndrzej Pietrasiewicz 	class_destroy(hidg_class);
135400a2430fSAndrzej Pietrasiewicz 	hidg_class = NULL;
135500a2430fSAndrzej Pietrasiewicz }
1356