xref: /openbmc/linux/drivers/usb/gadget/function/f_hid.c (revision 072684e8c58d17e853f8e8b9f6d9ce2e58d2b036)
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;
4400a2430fSAndrzej Pietrasiewicz 	unsigned short			report_desc_length;
4500a2430fSAndrzej Pietrasiewicz 	char				*report_desc;
4600a2430fSAndrzej Pietrasiewicz 	unsigned short			report_length;
4700a2430fSAndrzej Pietrasiewicz 
4800a2430fSAndrzej Pietrasiewicz 	/* recv report */
4900a2430fSAndrzej Pietrasiewicz 	struct list_head		completed_out_req;
5033e4c1a9SKrzysztof Opasiak 	spinlock_t			read_spinlock;
5100a2430fSAndrzej Pietrasiewicz 	wait_queue_head_t		read_queue;
5200a2430fSAndrzej Pietrasiewicz 	unsigned int			qlen;
5300a2430fSAndrzej Pietrasiewicz 
5400a2430fSAndrzej Pietrasiewicz 	/* send report */
5533e4c1a9SKrzysztof Opasiak 	spinlock_t			write_spinlock;
5600a2430fSAndrzej Pietrasiewicz 	bool				write_pending;
5700a2430fSAndrzej Pietrasiewicz 	wait_queue_head_t		write_queue;
5800a2430fSAndrzej Pietrasiewicz 	struct usb_request		*req;
5900a2430fSAndrzej Pietrasiewicz 
6000a2430fSAndrzej Pietrasiewicz 	int				minor;
6100a2430fSAndrzej Pietrasiewicz 	struct cdev			cdev;
6200a2430fSAndrzej Pietrasiewicz 	struct usb_function		func;
6300a2430fSAndrzej Pietrasiewicz 
6400a2430fSAndrzej Pietrasiewicz 	struct usb_ep			*in_ep;
6500a2430fSAndrzej Pietrasiewicz 	struct usb_ep			*out_ep;
6600a2430fSAndrzej Pietrasiewicz };
6700a2430fSAndrzej Pietrasiewicz 
6800a2430fSAndrzej Pietrasiewicz static inline struct f_hidg *func_to_hidg(struct usb_function *f)
6900a2430fSAndrzej Pietrasiewicz {
7000a2430fSAndrzej Pietrasiewicz 	return container_of(f, struct f_hidg, func);
7100a2430fSAndrzej Pietrasiewicz }
7200a2430fSAndrzej Pietrasiewicz 
7300a2430fSAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/
7400a2430fSAndrzej Pietrasiewicz /*                           Static descriptors                            */
7500a2430fSAndrzej Pietrasiewicz 
7600a2430fSAndrzej Pietrasiewicz static struct usb_interface_descriptor hidg_interface_desc = {
7700a2430fSAndrzej Pietrasiewicz 	.bLength		= sizeof hidg_interface_desc,
7800a2430fSAndrzej Pietrasiewicz 	.bDescriptorType	= USB_DT_INTERFACE,
7900a2430fSAndrzej Pietrasiewicz 	/* .bInterfaceNumber	= DYNAMIC */
8000a2430fSAndrzej Pietrasiewicz 	.bAlternateSetting	= 0,
8100a2430fSAndrzej Pietrasiewicz 	.bNumEndpoints		= 2,
8200a2430fSAndrzej Pietrasiewicz 	.bInterfaceClass	= USB_CLASS_HID,
8300a2430fSAndrzej Pietrasiewicz 	/* .bInterfaceSubClass	= DYNAMIC */
8400a2430fSAndrzej Pietrasiewicz 	/* .bInterfaceProtocol	= DYNAMIC */
8500a2430fSAndrzej Pietrasiewicz 	/* .iInterface		= DYNAMIC */
8600a2430fSAndrzej Pietrasiewicz };
8700a2430fSAndrzej Pietrasiewicz 
8800a2430fSAndrzej Pietrasiewicz static struct hid_descriptor hidg_desc = {
8900a2430fSAndrzej Pietrasiewicz 	.bLength			= sizeof hidg_desc,
9000a2430fSAndrzej Pietrasiewicz 	.bDescriptorType		= HID_DT_HID,
9100a2430fSAndrzej Pietrasiewicz 	.bcdHID				= 0x0101,
9200a2430fSAndrzej Pietrasiewicz 	.bCountryCode			= 0x00,
9300a2430fSAndrzej Pietrasiewicz 	.bNumDescriptors		= 0x1,
9400a2430fSAndrzej Pietrasiewicz 	/*.desc[0].bDescriptorType	= DYNAMIC */
9500a2430fSAndrzej Pietrasiewicz 	/*.desc[0].wDescriptorLenght	= DYNAMIC */
9600a2430fSAndrzej Pietrasiewicz };
9700a2430fSAndrzej Pietrasiewicz 
98dbf499cfSJanusz Dziedzic /* Super-Speed Support */
99dbf499cfSJanusz Dziedzic 
100dbf499cfSJanusz Dziedzic static struct usb_endpoint_descriptor hidg_ss_in_ep_desc = {
101dbf499cfSJanusz Dziedzic 	.bLength		= USB_DT_ENDPOINT_SIZE,
102dbf499cfSJanusz Dziedzic 	.bDescriptorType	= USB_DT_ENDPOINT,
103dbf499cfSJanusz Dziedzic 	.bEndpointAddress	= USB_DIR_IN,
104dbf499cfSJanusz Dziedzic 	.bmAttributes		= USB_ENDPOINT_XFER_INT,
105dbf499cfSJanusz Dziedzic 	/*.wMaxPacketSize	= DYNAMIC */
106dbf499cfSJanusz Dziedzic 	.bInterval		= 4, /* FIXME: Add this field in the
107dbf499cfSJanusz Dziedzic 				      * HID gadget configuration?
108dbf499cfSJanusz Dziedzic 				      * (struct hidg_func_descriptor)
109dbf499cfSJanusz Dziedzic 				      */
110dbf499cfSJanusz Dziedzic };
111dbf499cfSJanusz Dziedzic 
112dbf499cfSJanusz Dziedzic static struct usb_ss_ep_comp_descriptor hidg_ss_in_comp_desc = {
113dbf499cfSJanusz Dziedzic 	.bLength                = sizeof(hidg_ss_in_comp_desc),
114dbf499cfSJanusz Dziedzic 	.bDescriptorType        = USB_DT_SS_ENDPOINT_COMP,
115dbf499cfSJanusz Dziedzic 
116dbf499cfSJanusz Dziedzic 	/* .bMaxBurst           = 0, */
117dbf499cfSJanusz Dziedzic 	/* .bmAttributes        = 0, */
118dbf499cfSJanusz Dziedzic 	/* .wBytesPerInterval   = DYNAMIC */
119dbf499cfSJanusz Dziedzic };
120dbf499cfSJanusz Dziedzic 
121dbf499cfSJanusz Dziedzic static struct usb_endpoint_descriptor hidg_ss_out_ep_desc = {
122dbf499cfSJanusz Dziedzic 	.bLength		= USB_DT_ENDPOINT_SIZE,
123dbf499cfSJanusz Dziedzic 	.bDescriptorType	= USB_DT_ENDPOINT,
124dbf499cfSJanusz Dziedzic 	.bEndpointAddress	= USB_DIR_OUT,
125dbf499cfSJanusz Dziedzic 	.bmAttributes		= USB_ENDPOINT_XFER_INT,
126dbf499cfSJanusz Dziedzic 	/*.wMaxPacketSize	= DYNAMIC */
127dbf499cfSJanusz Dziedzic 	.bInterval		= 4, /* FIXME: Add this field in the
128dbf499cfSJanusz Dziedzic 				      * HID gadget configuration?
129dbf499cfSJanusz Dziedzic 				      * (struct hidg_func_descriptor)
130dbf499cfSJanusz Dziedzic 				      */
131dbf499cfSJanusz Dziedzic };
132dbf499cfSJanusz Dziedzic 
133dbf499cfSJanusz Dziedzic static struct usb_ss_ep_comp_descriptor hidg_ss_out_comp_desc = {
134dbf499cfSJanusz Dziedzic 	.bLength                = sizeof(hidg_ss_out_comp_desc),
135dbf499cfSJanusz Dziedzic 	.bDescriptorType        = USB_DT_SS_ENDPOINT_COMP,
136dbf499cfSJanusz Dziedzic 
137dbf499cfSJanusz Dziedzic 	/* .bMaxBurst           = 0, */
138dbf499cfSJanusz Dziedzic 	/* .bmAttributes        = 0, */
139dbf499cfSJanusz Dziedzic 	/* .wBytesPerInterval   = DYNAMIC */
140dbf499cfSJanusz Dziedzic };
141dbf499cfSJanusz Dziedzic 
142dbf499cfSJanusz Dziedzic static struct usb_descriptor_header *hidg_ss_descriptors[] = {
143dbf499cfSJanusz Dziedzic 	(struct usb_descriptor_header *)&hidg_interface_desc,
144dbf499cfSJanusz Dziedzic 	(struct usb_descriptor_header *)&hidg_desc,
145dbf499cfSJanusz Dziedzic 	(struct usb_descriptor_header *)&hidg_ss_in_ep_desc,
146dbf499cfSJanusz Dziedzic 	(struct usb_descriptor_header *)&hidg_ss_in_comp_desc,
147dbf499cfSJanusz Dziedzic 	(struct usb_descriptor_header *)&hidg_ss_out_ep_desc,
148dbf499cfSJanusz Dziedzic 	(struct usb_descriptor_header *)&hidg_ss_out_comp_desc,
149dbf499cfSJanusz Dziedzic 	NULL,
150dbf499cfSJanusz Dziedzic };
151dbf499cfSJanusz Dziedzic 
15200a2430fSAndrzej Pietrasiewicz /* High-Speed Support */
15300a2430fSAndrzej Pietrasiewicz 
15400a2430fSAndrzej Pietrasiewicz static struct usb_endpoint_descriptor hidg_hs_in_ep_desc = {
15500a2430fSAndrzej Pietrasiewicz 	.bLength		= USB_DT_ENDPOINT_SIZE,
15600a2430fSAndrzej Pietrasiewicz 	.bDescriptorType	= USB_DT_ENDPOINT,
15700a2430fSAndrzej Pietrasiewicz 	.bEndpointAddress	= USB_DIR_IN,
15800a2430fSAndrzej Pietrasiewicz 	.bmAttributes		= USB_ENDPOINT_XFER_INT,
15900a2430fSAndrzej Pietrasiewicz 	/*.wMaxPacketSize	= DYNAMIC */
16000a2430fSAndrzej Pietrasiewicz 	.bInterval		= 4, /* FIXME: Add this field in the
16100a2430fSAndrzej Pietrasiewicz 				      * HID gadget configuration?
16200a2430fSAndrzej Pietrasiewicz 				      * (struct hidg_func_descriptor)
16300a2430fSAndrzej Pietrasiewicz 				      */
16400a2430fSAndrzej Pietrasiewicz };
16500a2430fSAndrzej Pietrasiewicz 
16600a2430fSAndrzej Pietrasiewicz static struct usb_endpoint_descriptor hidg_hs_out_ep_desc = {
16700a2430fSAndrzej Pietrasiewicz 	.bLength		= USB_DT_ENDPOINT_SIZE,
16800a2430fSAndrzej Pietrasiewicz 	.bDescriptorType	= USB_DT_ENDPOINT,
16900a2430fSAndrzej Pietrasiewicz 	.bEndpointAddress	= USB_DIR_OUT,
17000a2430fSAndrzej Pietrasiewicz 	.bmAttributes		= USB_ENDPOINT_XFER_INT,
17100a2430fSAndrzej Pietrasiewicz 	/*.wMaxPacketSize	= DYNAMIC */
17200a2430fSAndrzej Pietrasiewicz 	.bInterval		= 4, /* FIXME: Add this field in the
17300a2430fSAndrzej Pietrasiewicz 				      * HID gadget configuration?
17400a2430fSAndrzej Pietrasiewicz 				      * (struct hidg_func_descriptor)
17500a2430fSAndrzej Pietrasiewicz 				      */
17600a2430fSAndrzej Pietrasiewicz };
17700a2430fSAndrzej Pietrasiewicz 
17800a2430fSAndrzej Pietrasiewicz static struct usb_descriptor_header *hidg_hs_descriptors[] = {
17900a2430fSAndrzej Pietrasiewicz 	(struct usb_descriptor_header *)&hidg_interface_desc,
18000a2430fSAndrzej Pietrasiewicz 	(struct usb_descriptor_header *)&hidg_desc,
18100a2430fSAndrzej Pietrasiewicz 	(struct usb_descriptor_header *)&hidg_hs_in_ep_desc,
18200a2430fSAndrzej Pietrasiewicz 	(struct usb_descriptor_header *)&hidg_hs_out_ep_desc,
18300a2430fSAndrzej Pietrasiewicz 	NULL,
18400a2430fSAndrzej Pietrasiewicz };
18500a2430fSAndrzej Pietrasiewicz 
18600a2430fSAndrzej Pietrasiewicz /* Full-Speed Support */
18700a2430fSAndrzej Pietrasiewicz 
18800a2430fSAndrzej Pietrasiewicz static struct usb_endpoint_descriptor hidg_fs_in_ep_desc = {
18900a2430fSAndrzej Pietrasiewicz 	.bLength		= USB_DT_ENDPOINT_SIZE,
19000a2430fSAndrzej Pietrasiewicz 	.bDescriptorType	= USB_DT_ENDPOINT,
19100a2430fSAndrzej Pietrasiewicz 	.bEndpointAddress	= USB_DIR_IN,
19200a2430fSAndrzej Pietrasiewicz 	.bmAttributes		= USB_ENDPOINT_XFER_INT,
19300a2430fSAndrzej Pietrasiewicz 	/*.wMaxPacketSize	= DYNAMIC */
19400a2430fSAndrzej Pietrasiewicz 	.bInterval		= 10, /* FIXME: Add this field in the
19500a2430fSAndrzej Pietrasiewicz 				       * HID gadget configuration?
19600a2430fSAndrzej Pietrasiewicz 				       * (struct hidg_func_descriptor)
19700a2430fSAndrzej Pietrasiewicz 				       */
19800a2430fSAndrzej Pietrasiewicz };
19900a2430fSAndrzej Pietrasiewicz 
20000a2430fSAndrzej Pietrasiewicz static struct usb_endpoint_descriptor hidg_fs_out_ep_desc = {
20100a2430fSAndrzej Pietrasiewicz 	.bLength		= USB_DT_ENDPOINT_SIZE,
20200a2430fSAndrzej Pietrasiewicz 	.bDescriptorType	= USB_DT_ENDPOINT,
20300a2430fSAndrzej Pietrasiewicz 	.bEndpointAddress	= USB_DIR_OUT,
20400a2430fSAndrzej Pietrasiewicz 	.bmAttributes		= USB_ENDPOINT_XFER_INT,
20500a2430fSAndrzej Pietrasiewicz 	/*.wMaxPacketSize	= DYNAMIC */
20600a2430fSAndrzej Pietrasiewicz 	.bInterval		= 10, /* FIXME: Add this field in the
20700a2430fSAndrzej Pietrasiewicz 				       * HID gadget configuration?
20800a2430fSAndrzej Pietrasiewicz 				       * (struct hidg_func_descriptor)
20900a2430fSAndrzej Pietrasiewicz 				       */
21000a2430fSAndrzej Pietrasiewicz };
21100a2430fSAndrzej Pietrasiewicz 
21200a2430fSAndrzej Pietrasiewicz static struct usb_descriptor_header *hidg_fs_descriptors[] = {
21300a2430fSAndrzej Pietrasiewicz 	(struct usb_descriptor_header *)&hidg_interface_desc,
21400a2430fSAndrzej Pietrasiewicz 	(struct usb_descriptor_header *)&hidg_desc,
21500a2430fSAndrzej Pietrasiewicz 	(struct usb_descriptor_header *)&hidg_fs_in_ep_desc,
21600a2430fSAndrzej Pietrasiewicz 	(struct usb_descriptor_header *)&hidg_fs_out_ep_desc,
21700a2430fSAndrzej Pietrasiewicz 	NULL,
21800a2430fSAndrzej Pietrasiewicz };
21900a2430fSAndrzej Pietrasiewicz 
22000a2430fSAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/
221cb382536SAndrzej Pietrasiewicz /*                                 Strings                                 */
222cb382536SAndrzej Pietrasiewicz 
223cb382536SAndrzej Pietrasiewicz #define CT_FUNC_HID_IDX	0
224cb382536SAndrzej Pietrasiewicz 
225cb382536SAndrzej Pietrasiewicz static struct usb_string ct_func_string_defs[] = {
226cb382536SAndrzej Pietrasiewicz 	[CT_FUNC_HID_IDX].s	= "HID Interface",
227cb382536SAndrzej Pietrasiewicz 	{},			/* end of list */
228cb382536SAndrzej Pietrasiewicz };
229cb382536SAndrzej Pietrasiewicz 
230cb382536SAndrzej Pietrasiewicz static struct usb_gadget_strings ct_func_string_table = {
231cb382536SAndrzej Pietrasiewicz 	.language	= 0x0409,	/* en-US */
232cb382536SAndrzej Pietrasiewicz 	.strings	= ct_func_string_defs,
233cb382536SAndrzej Pietrasiewicz };
234cb382536SAndrzej Pietrasiewicz 
235cb382536SAndrzej Pietrasiewicz static struct usb_gadget_strings *ct_func_strings[] = {
236cb382536SAndrzej Pietrasiewicz 	&ct_func_string_table,
237cb382536SAndrzej Pietrasiewicz 	NULL,
238cb382536SAndrzej Pietrasiewicz };
239cb382536SAndrzej Pietrasiewicz 
240cb382536SAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/
24100a2430fSAndrzej Pietrasiewicz /*                              Char Device                                */
24200a2430fSAndrzej Pietrasiewicz 
24300a2430fSAndrzej Pietrasiewicz static ssize_t f_hidg_read(struct file *file, char __user *buffer,
24400a2430fSAndrzej Pietrasiewicz 			size_t count, loff_t *ptr)
24500a2430fSAndrzej Pietrasiewicz {
24600a2430fSAndrzej Pietrasiewicz 	struct f_hidg *hidg = file->private_data;
24700a2430fSAndrzej Pietrasiewicz 	struct f_hidg_req_list *list;
24800a2430fSAndrzej Pietrasiewicz 	struct usb_request *req;
24900a2430fSAndrzej Pietrasiewicz 	unsigned long flags;
25000a2430fSAndrzej Pietrasiewicz 	int ret;
25100a2430fSAndrzej Pietrasiewicz 
25200a2430fSAndrzej Pietrasiewicz 	if (!count)
25300a2430fSAndrzej Pietrasiewicz 		return 0;
25400a2430fSAndrzej Pietrasiewicz 
25596d4f267SLinus Torvalds 	if (!access_ok(buffer, count))
25600a2430fSAndrzej Pietrasiewicz 		return -EFAULT;
25700a2430fSAndrzej Pietrasiewicz 
25833e4c1a9SKrzysztof Opasiak 	spin_lock_irqsave(&hidg->read_spinlock, flags);
25900a2430fSAndrzej Pietrasiewicz 
26000a2430fSAndrzej Pietrasiewicz #define READ_COND (!list_empty(&hidg->completed_out_req))
26100a2430fSAndrzej Pietrasiewicz 
26200a2430fSAndrzej Pietrasiewicz 	/* wait for at least one buffer to complete */
26300a2430fSAndrzej Pietrasiewicz 	while (!READ_COND) {
26433e4c1a9SKrzysztof Opasiak 		spin_unlock_irqrestore(&hidg->read_spinlock, flags);
26500a2430fSAndrzej Pietrasiewicz 		if (file->f_flags & O_NONBLOCK)
26600a2430fSAndrzej Pietrasiewicz 			return -EAGAIN;
26700a2430fSAndrzej Pietrasiewicz 
26800a2430fSAndrzej Pietrasiewicz 		if (wait_event_interruptible(hidg->read_queue, READ_COND))
26900a2430fSAndrzej Pietrasiewicz 			return -ERESTARTSYS;
27000a2430fSAndrzej Pietrasiewicz 
27133e4c1a9SKrzysztof Opasiak 		spin_lock_irqsave(&hidg->read_spinlock, flags);
27200a2430fSAndrzej Pietrasiewicz 	}
27300a2430fSAndrzej Pietrasiewicz 
27400a2430fSAndrzej Pietrasiewicz 	/* pick the first one */
27500a2430fSAndrzej Pietrasiewicz 	list = list_first_entry(&hidg->completed_out_req,
27600a2430fSAndrzej Pietrasiewicz 				struct f_hidg_req_list, list);
277aa65d11aSKrzysztof Opasiak 
278aa65d11aSKrzysztof Opasiak 	/*
279aa65d11aSKrzysztof Opasiak 	 * Remove this from list to protect it from beign free()
280aa65d11aSKrzysztof Opasiak 	 * while host disables our function
281aa65d11aSKrzysztof Opasiak 	 */
282aa65d11aSKrzysztof Opasiak 	list_del(&list->list);
283aa65d11aSKrzysztof Opasiak 
28400a2430fSAndrzej Pietrasiewicz 	req = list->req;
28500a2430fSAndrzej Pietrasiewicz 	count = min_t(unsigned int, count, req->actual - list->pos);
28633e4c1a9SKrzysztof Opasiak 	spin_unlock_irqrestore(&hidg->read_spinlock, flags);
28700a2430fSAndrzej Pietrasiewicz 
28800a2430fSAndrzej Pietrasiewicz 	/* copy to user outside spinlock */
28900a2430fSAndrzej Pietrasiewicz 	count -= copy_to_user(buffer, req->buf + list->pos, count);
29000a2430fSAndrzej Pietrasiewicz 	list->pos += count;
29100a2430fSAndrzej Pietrasiewicz 
29200a2430fSAndrzej Pietrasiewicz 	/*
29300a2430fSAndrzej Pietrasiewicz 	 * if this request is completely handled and transfered to
29400a2430fSAndrzej Pietrasiewicz 	 * userspace, remove its entry from the list and requeue it
29500a2430fSAndrzej Pietrasiewicz 	 * again. Otherwise, we will revisit it again upon the next
29600a2430fSAndrzej Pietrasiewicz 	 * call, taking into account its current read position.
29700a2430fSAndrzej Pietrasiewicz 	 */
29800a2430fSAndrzej Pietrasiewicz 	if (list->pos == req->actual) {
29900a2430fSAndrzej Pietrasiewicz 		kfree(list);
30000a2430fSAndrzej Pietrasiewicz 
30100a2430fSAndrzej Pietrasiewicz 		req->length = hidg->report_length;
30200a2430fSAndrzej Pietrasiewicz 		ret = usb_ep_queue(hidg->out_ep, req, GFP_KERNEL);
303aa65d11aSKrzysztof Opasiak 		if (ret < 0) {
304aa65d11aSKrzysztof Opasiak 			free_ep_req(hidg->out_ep, req);
30500a2430fSAndrzej Pietrasiewicz 			return ret;
30600a2430fSAndrzej Pietrasiewicz 		}
307aa65d11aSKrzysztof Opasiak 	} else {
30833e4c1a9SKrzysztof Opasiak 		spin_lock_irqsave(&hidg->read_spinlock, flags);
309aa65d11aSKrzysztof Opasiak 		list_add(&list->list, &hidg->completed_out_req);
31033e4c1a9SKrzysztof Opasiak 		spin_unlock_irqrestore(&hidg->read_spinlock, flags);
311aa65d11aSKrzysztof Opasiak 
312aa65d11aSKrzysztof Opasiak 		wake_up(&hidg->read_queue);
313aa65d11aSKrzysztof Opasiak 	}
31400a2430fSAndrzej Pietrasiewicz 
31500a2430fSAndrzej Pietrasiewicz 	return count;
31600a2430fSAndrzej Pietrasiewicz }
31700a2430fSAndrzej Pietrasiewicz 
31800a2430fSAndrzej Pietrasiewicz static void f_hidg_req_complete(struct usb_ep *ep, struct usb_request *req)
31900a2430fSAndrzej Pietrasiewicz {
32000a2430fSAndrzej Pietrasiewicz 	struct f_hidg *hidg = (struct f_hidg *)ep->driver_data;
32133e4c1a9SKrzysztof Opasiak 	unsigned long flags;
32200a2430fSAndrzej Pietrasiewicz 
32300a2430fSAndrzej Pietrasiewicz 	if (req->status != 0) {
32400a2430fSAndrzej Pietrasiewicz 		ERROR(hidg->func.config->cdev,
32500a2430fSAndrzej Pietrasiewicz 			"End Point Request ERROR: %d\n", req->status);
32600a2430fSAndrzej Pietrasiewicz 	}
32700a2430fSAndrzej Pietrasiewicz 
32833e4c1a9SKrzysztof Opasiak 	spin_lock_irqsave(&hidg->write_spinlock, flags);
32900a2430fSAndrzej Pietrasiewicz 	hidg->write_pending = 0;
33033e4c1a9SKrzysztof Opasiak 	spin_unlock_irqrestore(&hidg->write_spinlock, flags);
33100a2430fSAndrzej Pietrasiewicz 	wake_up(&hidg->write_queue);
33200a2430fSAndrzej Pietrasiewicz }
33300a2430fSAndrzej Pietrasiewicz 
33400a2430fSAndrzej Pietrasiewicz static ssize_t f_hidg_write(struct file *file, const char __user *buffer,
33500a2430fSAndrzej Pietrasiewicz 			    size_t count, loff_t *offp)
33600a2430fSAndrzej Pietrasiewicz {
33700a2430fSAndrzej Pietrasiewicz 	struct f_hidg *hidg  = file->private_data;
338749494b6SKrzysztof Opasiak 	struct usb_request *req;
33933e4c1a9SKrzysztof Opasiak 	unsigned long flags;
34000a2430fSAndrzej Pietrasiewicz 	ssize_t status = -ENOMEM;
34100a2430fSAndrzej Pietrasiewicz 
34296d4f267SLinus Torvalds 	if (!access_ok(buffer, count))
34300a2430fSAndrzej Pietrasiewicz 		return -EFAULT;
34400a2430fSAndrzej Pietrasiewicz 
34533e4c1a9SKrzysztof Opasiak 	spin_lock_irqsave(&hidg->write_spinlock, flags);
34600a2430fSAndrzej Pietrasiewicz 
34700a2430fSAndrzej Pietrasiewicz #define WRITE_COND (!hidg->write_pending)
348749494b6SKrzysztof Opasiak try_again:
34900a2430fSAndrzej Pietrasiewicz 	/* write queue */
35000a2430fSAndrzej Pietrasiewicz 	while (!WRITE_COND) {
35133e4c1a9SKrzysztof Opasiak 		spin_unlock_irqrestore(&hidg->write_spinlock, flags);
35200a2430fSAndrzej Pietrasiewicz 		if (file->f_flags & O_NONBLOCK)
35300a2430fSAndrzej Pietrasiewicz 			return -EAGAIN;
35400a2430fSAndrzej Pietrasiewicz 
35500a2430fSAndrzej Pietrasiewicz 		if (wait_event_interruptible_exclusive(
35600a2430fSAndrzej Pietrasiewicz 				hidg->write_queue, WRITE_COND))
35700a2430fSAndrzej Pietrasiewicz 			return -ERESTARTSYS;
35800a2430fSAndrzej Pietrasiewicz 
35933e4c1a9SKrzysztof Opasiak 		spin_lock_irqsave(&hidg->write_spinlock, flags);
36000a2430fSAndrzej Pietrasiewicz 	}
36100a2430fSAndrzej Pietrasiewicz 
36233e4c1a9SKrzysztof Opasiak 	hidg->write_pending = 1;
363749494b6SKrzysztof Opasiak 	req = hidg->req;
36400a2430fSAndrzej Pietrasiewicz 	count  = min_t(unsigned, count, hidg->report_length);
36533e4c1a9SKrzysztof Opasiak 
36633e4c1a9SKrzysztof Opasiak 	spin_unlock_irqrestore(&hidg->write_spinlock, flags);
36725cd9721SKrzysztof Opasiak 	status = copy_from_user(req->buf, buffer, count);
36800a2430fSAndrzej Pietrasiewicz 
36900a2430fSAndrzej Pietrasiewicz 	if (status != 0) {
37000a2430fSAndrzej Pietrasiewicz 		ERROR(hidg->func.config->cdev,
37100a2430fSAndrzej Pietrasiewicz 			"copy_from_user error\n");
37233e4c1a9SKrzysztof Opasiak 		status = -EINVAL;
37333e4c1a9SKrzysztof Opasiak 		goto release_write_pending;
37400a2430fSAndrzej Pietrasiewicz 	}
37500a2430fSAndrzej Pietrasiewicz 
376749494b6SKrzysztof Opasiak 	spin_lock_irqsave(&hidg->write_spinlock, flags);
377749494b6SKrzysztof Opasiak 
37825cd9721SKrzysztof Opasiak 	/* when our function has been disabled by host */
379749494b6SKrzysztof Opasiak 	if (!hidg->req) {
38025cd9721SKrzysztof Opasiak 		free_ep_req(hidg->in_ep, req);
381749494b6SKrzysztof Opasiak 		/*
382749494b6SKrzysztof Opasiak 		 * TODO
383749494b6SKrzysztof Opasiak 		 * Should we fail with error here?
384749494b6SKrzysztof Opasiak 		 */
385749494b6SKrzysztof Opasiak 		goto try_again;
386749494b6SKrzysztof Opasiak 	}
387749494b6SKrzysztof Opasiak 
388749494b6SKrzysztof Opasiak 	req->status   = 0;
389749494b6SKrzysztof Opasiak 	req->zero     = 0;
390749494b6SKrzysztof Opasiak 	req->length   = count;
391749494b6SKrzysztof Opasiak 	req->complete = f_hidg_req_complete;
392749494b6SKrzysztof Opasiak 	req->context  = hidg;
39300a2430fSAndrzej Pietrasiewicz 
394*072684e8SRadoslav Gerganov 	spin_unlock_irqrestore(&hidg->write_spinlock, flags);
395*072684e8SRadoslav Gerganov 
39625cd9721SKrzysztof Opasiak 	status = usb_ep_queue(hidg->in_ep, req, GFP_ATOMIC);
39700a2430fSAndrzej Pietrasiewicz 	if (status < 0) {
39800a2430fSAndrzej Pietrasiewicz 		ERROR(hidg->func.config->cdev,
39900a2430fSAndrzej Pietrasiewicz 			"usb_ep_queue error on int endpoint %zd\n", status);
400*072684e8SRadoslav Gerganov 		goto release_write_pending;
40100a2430fSAndrzej Pietrasiewicz 	} else {
40200a2430fSAndrzej Pietrasiewicz 		status = count;
40300a2430fSAndrzej Pietrasiewicz 	}
40400a2430fSAndrzej Pietrasiewicz 
40533e4c1a9SKrzysztof Opasiak 	return status;
40633e4c1a9SKrzysztof Opasiak release_write_pending:
40733e4c1a9SKrzysztof Opasiak 	spin_lock_irqsave(&hidg->write_spinlock, flags);
40833e4c1a9SKrzysztof Opasiak 	hidg->write_pending = 0;
40933e4c1a9SKrzysztof Opasiak 	spin_unlock_irqrestore(&hidg->write_spinlock, flags);
41033e4c1a9SKrzysztof Opasiak 
41133e4c1a9SKrzysztof Opasiak 	wake_up(&hidg->write_queue);
41200a2430fSAndrzej Pietrasiewicz 
41300a2430fSAndrzej Pietrasiewicz 	return status;
41400a2430fSAndrzej Pietrasiewicz }
41500a2430fSAndrzej Pietrasiewicz 
416afc9a42bSAl Viro static __poll_t f_hidg_poll(struct file *file, poll_table *wait)
41700a2430fSAndrzej Pietrasiewicz {
41800a2430fSAndrzej Pietrasiewicz 	struct f_hidg	*hidg  = file->private_data;
419afc9a42bSAl Viro 	__poll_t	ret = 0;
42000a2430fSAndrzej Pietrasiewicz 
42100a2430fSAndrzej Pietrasiewicz 	poll_wait(file, &hidg->read_queue, wait);
42200a2430fSAndrzej Pietrasiewicz 	poll_wait(file, &hidg->write_queue, wait);
42300a2430fSAndrzej Pietrasiewicz 
42400a2430fSAndrzej Pietrasiewicz 	if (WRITE_COND)
425a9a08845SLinus Torvalds 		ret |= EPOLLOUT | EPOLLWRNORM;
42600a2430fSAndrzej Pietrasiewicz 
42700a2430fSAndrzej Pietrasiewicz 	if (READ_COND)
428a9a08845SLinus Torvalds 		ret |= EPOLLIN | EPOLLRDNORM;
42900a2430fSAndrzej Pietrasiewicz 
43000a2430fSAndrzej Pietrasiewicz 	return ret;
43100a2430fSAndrzej Pietrasiewicz }
43200a2430fSAndrzej Pietrasiewicz 
43300a2430fSAndrzej Pietrasiewicz #undef WRITE_COND
43400a2430fSAndrzej Pietrasiewicz #undef READ_COND
43500a2430fSAndrzej Pietrasiewicz 
43600a2430fSAndrzej Pietrasiewicz static int f_hidg_release(struct inode *inode, struct file *fd)
43700a2430fSAndrzej Pietrasiewicz {
43800a2430fSAndrzej Pietrasiewicz 	fd->private_data = NULL;
43900a2430fSAndrzej Pietrasiewicz 	return 0;
44000a2430fSAndrzej Pietrasiewicz }
44100a2430fSAndrzej Pietrasiewicz 
44200a2430fSAndrzej Pietrasiewicz static int f_hidg_open(struct inode *inode, struct file *fd)
44300a2430fSAndrzej Pietrasiewicz {
44400a2430fSAndrzej Pietrasiewicz 	struct f_hidg *hidg =
44500a2430fSAndrzej Pietrasiewicz 		container_of(inode->i_cdev, struct f_hidg, cdev);
44600a2430fSAndrzej Pietrasiewicz 
44700a2430fSAndrzej Pietrasiewicz 	fd->private_data = hidg;
44800a2430fSAndrzej Pietrasiewicz 
44900a2430fSAndrzej Pietrasiewicz 	return 0;
45000a2430fSAndrzej Pietrasiewicz }
45100a2430fSAndrzej Pietrasiewicz 
45200a2430fSAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/
45300a2430fSAndrzej Pietrasiewicz /*                                usb_function                             */
45400a2430fSAndrzej Pietrasiewicz 
45500a2430fSAndrzej Pietrasiewicz static inline struct usb_request *hidg_alloc_ep_req(struct usb_ep *ep,
45600a2430fSAndrzej Pietrasiewicz 						    unsigned length)
45700a2430fSAndrzej Pietrasiewicz {
458aadbe812SFelipe F. Tonello 	return alloc_ep_req(ep, length);
45900a2430fSAndrzej Pietrasiewicz }
46000a2430fSAndrzej Pietrasiewicz 
46100a2430fSAndrzej Pietrasiewicz static void hidg_set_report_complete(struct usb_ep *ep, struct usb_request *req)
46200a2430fSAndrzej Pietrasiewicz {
46300a2430fSAndrzej Pietrasiewicz 	struct f_hidg *hidg = (struct f_hidg *) req->context;
46420d2ca95SKrzysztof Opasiak 	struct usb_composite_dev *cdev = hidg->func.config->cdev;
46500a2430fSAndrzej Pietrasiewicz 	struct f_hidg_req_list *req_list;
46600a2430fSAndrzej Pietrasiewicz 	unsigned long flags;
46700a2430fSAndrzej Pietrasiewicz 
46820d2ca95SKrzysztof Opasiak 	switch (req->status) {
46920d2ca95SKrzysztof Opasiak 	case 0:
47000a2430fSAndrzej Pietrasiewicz 		req_list = kzalloc(sizeof(*req_list), GFP_ATOMIC);
47120d2ca95SKrzysztof Opasiak 		if (!req_list) {
47220d2ca95SKrzysztof Opasiak 			ERROR(cdev, "Unable to allocate mem for req_list\n");
47320d2ca95SKrzysztof Opasiak 			goto free_req;
47420d2ca95SKrzysztof Opasiak 		}
47500a2430fSAndrzej Pietrasiewicz 
47600a2430fSAndrzej Pietrasiewicz 		req_list->req = req;
47700a2430fSAndrzej Pietrasiewicz 
47833e4c1a9SKrzysztof Opasiak 		spin_lock_irqsave(&hidg->read_spinlock, flags);
47900a2430fSAndrzej Pietrasiewicz 		list_add_tail(&req_list->list, &hidg->completed_out_req);
48033e4c1a9SKrzysztof Opasiak 		spin_unlock_irqrestore(&hidg->read_spinlock, flags);
48100a2430fSAndrzej Pietrasiewicz 
48200a2430fSAndrzej Pietrasiewicz 		wake_up(&hidg->read_queue);
48320d2ca95SKrzysztof Opasiak 		break;
48420d2ca95SKrzysztof Opasiak 	default:
48520d2ca95SKrzysztof Opasiak 		ERROR(cdev, "Set report failed %d\n", req->status);
48620d2ca95SKrzysztof Opasiak 		/* FALLTHROUGH */
48720d2ca95SKrzysztof Opasiak 	case -ECONNABORTED:		/* hardware forced ep reset */
48820d2ca95SKrzysztof Opasiak 	case -ECONNRESET:		/* request dequeued */
48920d2ca95SKrzysztof Opasiak 	case -ESHUTDOWN:		/* disconnect from host */
49020d2ca95SKrzysztof Opasiak free_req:
49120d2ca95SKrzysztof Opasiak 		free_ep_req(ep, req);
49220d2ca95SKrzysztof Opasiak 		return;
49320d2ca95SKrzysztof Opasiak 	}
49400a2430fSAndrzej Pietrasiewicz }
49500a2430fSAndrzej Pietrasiewicz 
49600a2430fSAndrzej Pietrasiewicz static int hidg_setup(struct usb_function *f,
49700a2430fSAndrzej Pietrasiewicz 		const struct usb_ctrlrequest *ctrl)
49800a2430fSAndrzej Pietrasiewicz {
49900a2430fSAndrzej Pietrasiewicz 	struct f_hidg			*hidg = func_to_hidg(f);
50000a2430fSAndrzej Pietrasiewicz 	struct usb_composite_dev	*cdev = f->config->cdev;
50100a2430fSAndrzej Pietrasiewicz 	struct usb_request		*req  = cdev->req;
50200a2430fSAndrzej Pietrasiewicz 	int status = 0;
50300a2430fSAndrzej Pietrasiewicz 	__u16 value, length;
50400a2430fSAndrzej Pietrasiewicz 
50500a2430fSAndrzej Pietrasiewicz 	value	= __le16_to_cpu(ctrl->wValue);
50600a2430fSAndrzej Pietrasiewicz 	length	= __le16_to_cpu(ctrl->wLength);
50700a2430fSAndrzej Pietrasiewicz 
508c9b3bde0SJulia Lawall 	VDBG(cdev,
509c9b3bde0SJulia Lawall 	     "%s crtl_request : bRequestType:0x%x bRequest:0x%x Value:0x%x\n",
510c9b3bde0SJulia Lawall 	     __func__, ctrl->bRequestType, ctrl->bRequest, value);
51100a2430fSAndrzej Pietrasiewicz 
51200a2430fSAndrzej Pietrasiewicz 	switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
51300a2430fSAndrzej Pietrasiewicz 	case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
51400a2430fSAndrzej Pietrasiewicz 		  | HID_REQ_GET_REPORT):
51500a2430fSAndrzej Pietrasiewicz 		VDBG(cdev, "get_report\n");
51600a2430fSAndrzej Pietrasiewicz 
51700a2430fSAndrzej Pietrasiewicz 		/* send an empty report */
51800a2430fSAndrzej Pietrasiewicz 		length = min_t(unsigned, length, hidg->report_length);
51900a2430fSAndrzej Pietrasiewicz 		memset(req->buf, 0x0, length);
52000a2430fSAndrzej Pietrasiewicz 
52100a2430fSAndrzej Pietrasiewicz 		goto respond;
52200a2430fSAndrzej Pietrasiewicz 		break;
52300a2430fSAndrzej Pietrasiewicz 
52400a2430fSAndrzej Pietrasiewicz 	case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
52500a2430fSAndrzej Pietrasiewicz 		  | HID_REQ_GET_PROTOCOL):
52600a2430fSAndrzej Pietrasiewicz 		VDBG(cdev, "get_protocol\n");
527b3c4ec71SAbdulhadi Mohamed 		length = min_t(unsigned int, length, 1);
528b3c4ec71SAbdulhadi Mohamed 		((u8 *) req->buf)[0] = hidg->protocol;
529b3c4ec71SAbdulhadi Mohamed 		goto respond;
53000a2430fSAndrzej Pietrasiewicz 		break;
53100a2430fSAndrzej Pietrasiewicz 
53200a2430fSAndrzej Pietrasiewicz 	case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
53300a2430fSAndrzej Pietrasiewicz 		  | HID_REQ_SET_REPORT):
5346774def6SMasanari Iida 		VDBG(cdev, "set_report | wLength=%d\n", ctrl->wLength);
53500a2430fSAndrzej Pietrasiewicz 		goto stall;
53600a2430fSAndrzej Pietrasiewicz 		break;
53700a2430fSAndrzej Pietrasiewicz 
53800a2430fSAndrzej Pietrasiewicz 	case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
53900a2430fSAndrzej Pietrasiewicz 		  | HID_REQ_SET_PROTOCOL):
54000a2430fSAndrzej Pietrasiewicz 		VDBG(cdev, "set_protocol\n");
541b3c4ec71SAbdulhadi Mohamed 		if (value > HID_REPORT_PROTOCOL)
542b3c4ec71SAbdulhadi Mohamed 			goto stall;
543b3c4ec71SAbdulhadi Mohamed 		length = 0;
544b3c4ec71SAbdulhadi Mohamed 		/*
545b3c4ec71SAbdulhadi Mohamed 		 * We assume that programs implementing the Boot protocol
546b3c4ec71SAbdulhadi Mohamed 		 * are also compatible with the Report Protocol
547b3c4ec71SAbdulhadi Mohamed 		 */
548b3c4ec71SAbdulhadi Mohamed 		if (hidg->bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT) {
549b3c4ec71SAbdulhadi Mohamed 			hidg->protocol = value;
550b3c4ec71SAbdulhadi Mohamed 			goto respond;
551b3c4ec71SAbdulhadi Mohamed 		}
55200a2430fSAndrzej Pietrasiewicz 		goto stall;
55300a2430fSAndrzej Pietrasiewicz 		break;
55400a2430fSAndrzej Pietrasiewicz 
55500a2430fSAndrzej Pietrasiewicz 	case ((USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_INTERFACE) << 8
55600a2430fSAndrzej Pietrasiewicz 		  | USB_REQ_GET_DESCRIPTOR):
55700a2430fSAndrzej Pietrasiewicz 		switch (value >> 8) {
55800a2430fSAndrzej Pietrasiewicz 		case HID_DT_HID:
559f286d487SKrzysztof Opasiak 		{
560f286d487SKrzysztof Opasiak 			struct hid_descriptor hidg_desc_copy = hidg_desc;
561f286d487SKrzysztof Opasiak 
56200a2430fSAndrzej Pietrasiewicz 			VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: HID\n");
563f286d487SKrzysztof Opasiak 			hidg_desc_copy.desc[0].bDescriptorType = HID_DT_REPORT;
564f286d487SKrzysztof Opasiak 			hidg_desc_copy.desc[0].wDescriptorLength =
565f286d487SKrzysztof Opasiak 				cpu_to_le16(hidg->report_desc_length);
566f286d487SKrzysztof Opasiak 
56700a2430fSAndrzej Pietrasiewicz 			length = min_t(unsigned short, length,
568f286d487SKrzysztof Opasiak 						   hidg_desc_copy.bLength);
569f286d487SKrzysztof Opasiak 			memcpy(req->buf, &hidg_desc_copy, length);
57000a2430fSAndrzej Pietrasiewicz 			goto respond;
57100a2430fSAndrzej Pietrasiewicz 			break;
572f286d487SKrzysztof Opasiak 		}
57300a2430fSAndrzej Pietrasiewicz 		case HID_DT_REPORT:
57400a2430fSAndrzej Pietrasiewicz 			VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: REPORT\n");
57500a2430fSAndrzej Pietrasiewicz 			length = min_t(unsigned short, length,
57600a2430fSAndrzej Pietrasiewicz 						   hidg->report_desc_length);
57700a2430fSAndrzej Pietrasiewicz 			memcpy(req->buf, hidg->report_desc, length);
57800a2430fSAndrzej Pietrasiewicz 			goto respond;
57900a2430fSAndrzej Pietrasiewicz 			break;
58000a2430fSAndrzej Pietrasiewicz 
58100a2430fSAndrzej Pietrasiewicz 		default:
58200a2430fSAndrzej Pietrasiewicz 			VDBG(cdev, "Unknown descriptor request 0x%x\n",
58300a2430fSAndrzej Pietrasiewicz 				 value >> 8);
58400a2430fSAndrzej Pietrasiewicz 			goto stall;
58500a2430fSAndrzej Pietrasiewicz 			break;
58600a2430fSAndrzej Pietrasiewicz 		}
58700a2430fSAndrzej Pietrasiewicz 		break;
58800a2430fSAndrzej Pietrasiewicz 
58900a2430fSAndrzej Pietrasiewicz 	default:
59000a2430fSAndrzej Pietrasiewicz 		VDBG(cdev, "Unknown request 0x%x\n",
59100a2430fSAndrzej Pietrasiewicz 			 ctrl->bRequest);
59200a2430fSAndrzej Pietrasiewicz 		goto stall;
59300a2430fSAndrzej Pietrasiewicz 		break;
59400a2430fSAndrzej Pietrasiewicz 	}
59500a2430fSAndrzej Pietrasiewicz 
59600a2430fSAndrzej Pietrasiewicz stall:
59700a2430fSAndrzej Pietrasiewicz 	return -EOPNOTSUPP;
59800a2430fSAndrzej Pietrasiewicz 
59900a2430fSAndrzej Pietrasiewicz respond:
60000a2430fSAndrzej Pietrasiewicz 	req->zero = 0;
60100a2430fSAndrzej Pietrasiewicz 	req->length = length;
60200a2430fSAndrzej Pietrasiewicz 	status = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
60300a2430fSAndrzej Pietrasiewicz 	if (status < 0)
60400a2430fSAndrzej Pietrasiewicz 		ERROR(cdev, "usb_ep_queue error on ep0 %d\n", value);
60500a2430fSAndrzej Pietrasiewicz 	return status;
60600a2430fSAndrzej Pietrasiewicz }
60700a2430fSAndrzej Pietrasiewicz 
60800a2430fSAndrzej Pietrasiewicz static void hidg_disable(struct usb_function *f)
60900a2430fSAndrzej Pietrasiewicz {
61000a2430fSAndrzej Pietrasiewicz 	struct f_hidg *hidg = func_to_hidg(f);
61100a2430fSAndrzej Pietrasiewicz 	struct f_hidg_req_list *list, *next;
612aa65d11aSKrzysztof Opasiak 	unsigned long flags;
61300a2430fSAndrzej Pietrasiewicz 
61400a2430fSAndrzej Pietrasiewicz 	usb_ep_disable(hidg->in_ep);
61500a2430fSAndrzej Pietrasiewicz 	usb_ep_disable(hidg->out_ep);
61600a2430fSAndrzej Pietrasiewicz 
61733e4c1a9SKrzysztof Opasiak 	spin_lock_irqsave(&hidg->read_spinlock, flags);
61800a2430fSAndrzej Pietrasiewicz 	list_for_each_entry_safe(list, next, &hidg->completed_out_req, list) {
619aa65d11aSKrzysztof Opasiak 		free_ep_req(hidg->out_ep, list->req);
62000a2430fSAndrzej Pietrasiewicz 		list_del(&list->list);
62100a2430fSAndrzej Pietrasiewicz 		kfree(list);
62200a2430fSAndrzej Pietrasiewicz 	}
62333e4c1a9SKrzysztof Opasiak 	spin_unlock_irqrestore(&hidg->read_spinlock, flags);
624749494b6SKrzysztof Opasiak 
625749494b6SKrzysztof Opasiak 	spin_lock_irqsave(&hidg->write_spinlock, flags);
626749494b6SKrzysztof Opasiak 	if (!hidg->write_pending) {
627749494b6SKrzysztof Opasiak 		free_ep_req(hidg->in_ep, hidg->req);
628749494b6SKrzysztof Opasiak 		hidg->write_pending = 1;
629749494b6SKrzysztof Opasiak 	}
630749494b6SKrzysztof Opasiak 
631749494b6SKrzysztof Opasiak 	hidg->req = NULL;
632749494b6SKrzysztof Opasiak 	spin_unlock_irqrestore(&hidg->write_spinlock, flags);
63300a2430fSAndrzej Pietrasiewicz }
63400a2430fSAndrzej Pietrasiewicz 
63500a2430fSAndrzej Pietrasiewicz static int hidg_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
63600a2430fSAndrzej Pietrasiewicz {
63700a2430fSAndrzej Pietrasiewicz 	struct usb_composite_dev		*cdev = f->config->cdev;
63800a2430fSAndrzej Pietrasiewicz 	struct f_hidg				*hidg = func_to_hidg(f);
639749494b6SKrzysztof Opasiak 	struct usb_request			*req_in = NULL;
640749494b6SKrzysztof Opasiak 	unsigned long				flags;
64100a2430fSAndrzej Pietrasiewicz 	int i, status = 0;
64200a2430fSAndrzej Pietrasiewicz 
64300a2430fSAndrzej Pietrasiewicz 	VDBG(cdev, "hidg_set_alt intf:%d alt:%d\n", intf, alt);
64400a2430fSAndrzej Pietrasiewicz 
64500a2430fSAndrzej Pietrasiewicz 	if (hidg->in_ep != NULL) {
64600a2430fSAndrzej Pietrasiewicz 		/* restart endpoint */
64700a2430fSAndrzej Pietrasiewicz 		usb_ep_disable(hidg->in_ep);
64800a2430fSAndrzej Pietrasiewicz 
64900a2430fSAndrzej Pietrasiewicz 		status = config_ep_by_speed(f->config->cdev->gadget, f,
65000a2430fSAndrzej Pietrasiewicz 					    hidg->in_ep);
65100a2430fSAndrzej Pietrasiewicz 		if (status) {
65200a2430fSAndrzej Pietrasiewicz 			ERROR(cdev, "config_ep_by_speed FAILED!\n");
65300a2430fSAndrzej Pietrasiewicz 			goto fail;
65400a2430fSAndrzej Pietrasiewicz 		}
65500a2430fSAndrzej Pietrasiewicz 		status = usb_ep_enable(hidg->in_ep);
65600a2430fSAndrzej Pietrasiewicz 		if (status < 0) {
65700a2430fSAndrzej Pietrasiewicz 			ERROR(cdev, "Enable IN endpoint FAILED!\n");
65800a2430fSAndrzej Pietrasiewicz 			goto fail;
65900a2430fSAndrzej Pietrasiewicz 		}
66000a2430fSAndrzej Pietrasiewicz 		hidg->in_ep->driver_data = hidg;
661749494b6SKrzysztof Opasiak 
662749494b6SKrzysztof Opasiak 		req_in = hidg_alloc_ep_req(hidg->in_ep, hidg->report_length);
663749494b6SKrzysztof Opasiak 		if (!req_in) {
664749494b6SKrzysztof Opasiak 			status = -ENOMEM;
665749494b6SKrzysztof Opasiak 			goto disable_ep_in;
666749494b6SKrzysztof Opasiak 		}
66700a2430fSAndrzej Pietrasiewicz 	}
66800a2430fSAndrzej Pietrasiewicz 
66900a2430fSAndrzej Pietrasiewicz 
67000a2430fSAndrzej Pietrasiewicz 	if (hidg->out_ep != NULL) {
67100a2430fSAndrzej Pietrasiewicz 		/* restart endpoint */
67200a2430fSAndrzej Pietrasiewicz 		usb_ep_disable(hidg->out_ep);
67300a2430fSAndrzej Pietrasiewicz 
67400a2430fSAndrzej Pietrasiewicz 		status = config_ep_by_speed(f->config->cdev->gadget, f,
67500a2430fSAndrzej Pietrasiewicz 					    hidg->out_ep);
67600a2430fSAndrzej Pietrasiewicz 		if (status) {
67700a2430fSAndrzej Pietrasiewicz 			ERROR(cdev, "config_ep_by_speed FAILED!\n");
678749494b6SKrzysztof Opasiak 			goto free_req_in;
67900a2430fSAndrzej Pietrasiewicz 		}
68000a2430fSAndrzej Pietrasiewicz 		status = usb_ep_enable(hidg->out_ep);
68100a2430fSAndrzej Pietrasiewicz 		if (status < 0) {
68243aef5c2SDavid Lechner 			ERROR(cdev, "Enable OUT endpoint FAILED!\n");
683749494b6SKrzysztof Opasiak 			goto free_req_in;
68400a2430fSAndrzej Pietrasiewicz 		}
68500a2430fSAndrzej Pietrasiewicz 		hidg->out_ep->driver_data = hidg;
68600a2430fSAndrzej Pietrasiewicz 
68700a2430fSAndrzej Pietrasiewicz 		/*
68800a2430fSAndrzej Pietrasiewicz 		 * allocate a bunch of read buffers and queue them all at once.
68900a2430fSAndrzej Pietrasiewicz 		 */
69000a2430fSAndrzej Pietrasiewicz 		for (i = 0; i < hidg->qlen && status == 0; i++) {
69100a2430fSAndrzej Pietrasiewicz 			struct usb_request *req =
69200a2430fSAndrzej Pietrasiewicz 					hidg_alloc_ep_req(hidg->out_ep,
69300a2430fSAndrzej Pietrasiewicz 							  hidg->report_length);
69400a2430fSAndrzej Pietrasiewicz 			if (req) {
69500a2430fSAndrzej Pietrasiewicz 				req->complete = hidg_set_report_complete;
69600a2430fSAndrzej Pietrasiewicz 				req->context  = hidg;
69700a2430fSAndrzej Pietrasiewicz 				status = usb_ep_queue(hidg->out_ep, req,
69800a2430fSAndrzej Pietrasiewicz 						      GFP_ATOMIC);
699749494b6SKrzysztof Opasiak 				if (status) {
70000a2430fSAndrzej Pietrasiewicz 					ERROR(cdev, "%s queue req --> %d\n",
70100a2430fSAndrzej Pietrasiewicz 						hidg->out_ep->name, status);
702749494b6SKrzysztof Opasiak 					free_ep_req(hidg->out_ep, req);
703749494b6SKrzysztof Opasiak 				}
70400a2430fSAndrzej Pietrasiewicz 			} else {
70500a2430fSAndrzej Pietrasiewicz 				status = -ENOMEM;
706749494b6SKrzysztof Opasiak 				goto disable_out_ep;
70700a2430fSAndrzej Pietrasiewicz 			}
70800a2430fSAndrzej Pietrasiewicz 		}
70900a2430fSAndrzej Pietrasiewicz 	}
71000a2430fSAndrzej Pietrasiewicz 
711749494b6SKrzysztof Opasiak 	if (hidg->in_ep != NULL) {
712749494b6SKrzysztof Opasiak 		spin_lock_irqsave(&hidg->write_spinlock, flags);
713749494b6SKrzysztof Opasiak 		hidg->req = req_in;
714749494b6SKrzysztof Opasiak 		hidg->write_pending = 0;
715749494b6SKrzysztof Opasiak 		spin_unlock_irqrestore(&hidg->write_spinlock, flags);
716749494b6SKrzysztof Opasiak 
717749494b6SKrzysztof Opasiak 		wake_up(&hidg->write_queue);
718749494b6SKrzysztof Opasiak 	}
719749494b6SKrzysztof Opasiak 	return 0;
720749494b6SKrzysztof Opasiak disable_out_ep:
721749494b6SKrzysztof Opasiak 	usb_ep_disable(hidg->out_ep);
722749494b6SKrzysztof Opasiak free_req_in:
723749494b6SKrzysztof Opasiak 	if (req_in)
724749494b6SKrzysztof Opasiak 		free_ep_req(hidg->in_ep, req_in);
725749494b6SKrzysztof Opasiak 
726749494b6SKrzysztof Opasiak disable_ep_in:
727749494b6SKrzysztof Opasiak 	if (hidg->in_ep)
728749494b6SKrzysztof Opasiak 		usb_ep_disable(hidg->in_ep);
729749494b6SKrzysztof Opasiak 
73000a2430fSAndrzej Pietrasiewicz fail:
73100a2430fSAndrzej Pietrasiewicz 	return status;
73200a2430fSAndrzej Pietrasiewicz }
73300a2430fSAndrzej Pietrasiewicz 
7347a3cc461SLad, Prabhakar static const struct file_operations f_hidg_fops = {
73500a2430fSAndrzej Pietrasiewicz 	.owner		= THIS_MODULE,
73600a2430fSAndrzej Pietrasiewicz 	.open		= f_hidg_open,
73700a2430fSAndrzej Pietrasiewicz 	.release	= f_hidg_release,
73800a2430fSAndrzej Pietrasiewicz 	.write		= f_hidg_write,
73900a2430fSAndrzej Pietrasiewicz 	.read		= f_hidg_read,
74000a2430fSAndrzej Pietrasiewicz 	.poll		= f_hidg_poll,
74100a2430fSAndrzej Pietrasiewicz 	.llseek		= noop_llseek,
74200a2430fSAndrzej Pietrasiewicz };
74300a2430fSAndrzej Pietrasiewicz 
744cb382536SAndrzej Pietrasiewicz static int hidg_bind(struct usb_configuration *c, struct usb_function *f)
74500a2430fSAndrzej Pietrasiewicz {
74600a2430fSAndrzej Pietrasiewicz 	struct usb_ep		*ep;
74700a2430fSAndrzej Pietrasiewicz 	struct f_hidg		*hidg = func_to_hidg(f);
7485ca8d3ecSAndrzej Pietrasiewicz 	struct usb_string	*us;
74963406087SAndrzej Pietrasiewicz 	struct device		*device;
75000a2430fSAndrzej Pietrasiewicz 	int			status;
75100a2430fSAndrzej Pietrasiewicz 	dev_t			dev;
75200a2430fSAndrzej Pietrasiewicz 
753cb382536SAndrzej Pietrasiewicz 	/* maybe allocate device-global string IDs, and patch descriptors */
7545ca8d3ecSAndrzej Pietrasiewicz 	us = usb_gstrings_attach(c->cdev, ct_func_strings,
7555ca8d3ecSAndrzej Pietrasiewicz 				 ARRAY_SIZE(ct_func_string_defs));
7565ca8d3ecSAndrzej Pietrasiewicz 	if (IS_ERR(us))
7575ca8d3ecSAndrzej Pietrasiewicz 		return PTR_ERR(us);
7585ca8d3ecSAndrzej Pietrasiewicz 	hidg_interface_desc.iInterface = us[CT_FUNC_HID_IDX].id;
759cb382536SAndrzej Pietrasiewicz 
76000a2430fSAndrzej Pietrasiewicz 	/* allocate instance-specific interface IDs, and patch descriptors */
76100a2430fSAndrzej Pietrasiewicz 	status = usb_interface_id(c, f);
76200a2430fSAndrzej Pietrasiewicz 	if (status < 0)
76300a2430fSAndrzej Pietrasiewicz 		goto fail;
76400a2430fSAndrzej Pietrasiewicz 	hidg_interface_desc.bInterfaceNumber = status;
76500a2430fSAndrzej Pietrasiewicz 
76600a2430fSAndrzej Pietrasiewicz 	/* allocate instance-specific endpoints */
76700a2430fSAndrzej Pietrasiewicz 	status = -ENODEV;
76800a2430fSAndrzej Pietrasiewicz 	ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_in_ep_desc);
76900a2430fSAndrzej Pietrasiewicz 	if (!ep)
77000a2430fSAndrzej Pietrasiewicz 		goto fail;
77100a2430fSAndrzej Pietrasiewicz 	hidg->in_ep = ep;
77200a2430fSAndrzej Pietrasiewicz 
77300a2430fSAndrzej Pietrasiewicz 	ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_out_ep_desc);
77400a2430fSAndrzej Pietrasiewicz 	if (!ep)
77500a2430fSAndrzej Pietrasiewicz 		goto fail;
77600a2430fSAndrzej Pietrasiewicz 	hidg->out_ep = ep;
77700a2430fSAndrzej Pietrasiewicz 
77800a2430fSAndrzej Pietrasiewicz 	/* set descriptor dynamic values */
77900a2430fSAndrzej Pietrasiewicz 	hidg_interface_desc.bInterfaceSubClass = hidg->bInterfaceSubClass;
78000a2430fSAndrzej Pietrasiewicz 	hidg_interface_desc.bInterfaceProtocol = hidg->bInterfaceProtocol;
781b3c4ec71SAbdulhadi Mohamed 	hidg->protocol = HID_REPORT_PROTOCOL;
782dbf499cfSJanusz Dziedzic 	hidg_ss_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
783dbf499cfSJanusz Dziedzic 	hidg_ss_in_comp_desc.wBytesPerInterval =
784dbf499cfSJanusz Dziedzic 				cpu_to_le16(hidg->report_length);
78500a2430fSAndrzej Pietrasiewicz 	hidg_hs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
78600a2430fSAndrzej Pietrasiewicz 	hidg_fs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
787dbf499cfSJanusz Dziedzic 	hidg_ss_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
788dbf499cfSJanusz Dziedzic 	hidg_ss_out_comp_desc.wBytesPerInterval =
789dbf499cfSJanusz Dziedzic 				cpu_to_le16(hidg->report_length);
79000a2430fSAndrzej Pietrasiewicz 	hidg_hs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
79100a2430fSAndrzej Pietrasiewicz 	hidg_fs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
792f286d487SKrzysztof Opasiak 	/*
793f286d487SKrzysztof Opasiak 	 * We can use hidg_desc struct here but we should not relay
794f286d487SKrzysztof Opasiak 	 * that its content won't change after returning from this function.
795f286d487SKrzysztof Opasiak 	 */
79600a2430fSAndrzej Pietrasiewicz 	hidg_desc.desc[0].bDescriptorType = HID_DT_REPORT;
79700a2430fSAndrzej Pietrasiewicz 	hidg_desc.desc[0].wDescriptorLength =
79800a2430fSAndrzej Pietrasiewicz 		cpu_to_le16(hidg->report_desc_length);
79900a2430fSAndrzej Pietrasiewicz 
80000a2430fSAndrzej Pietrasiewicz 	hidg_hs_in_ep_desc.bEndpointAddress =
80100a2430fSAndrzej Pietrasiewicz 		hidg_fs_in_ep_desc.bEndpointAddress;
80200a2430fSAndrzej Pietrasiewicz 	hidg_hs_out_ep_desc.bEndpointAddress =
80300a2430fSAndrzej Pietrasiewicz 		hidg_fs_out_ep_desc.bEndpointAddress;
80400a2430fSAndrzej Pietrasiewicz 
805dbf499cfSJanusz Dziedzic 	hidg_ss_in_ep_desc.bEndpointAddress =
806dbf499cfSJanusz Dziedzic 		hidg_fs_in_ep_desc.bEndpointAddress;
807dbf499cfSJanusz Dziedzic 	hidg_ss_out_ep_desc.bEndpointAddress =
808dbf499cfSJanusz Dziedzic 		hidg_fs_out_ep_desc.bEndpointAddress;
809dbf499cfSJanusz Dziedzic 
81000a2430fSAndrzej Pietrasiewicz 	status = usb_assign_descriptors(f, hidg_fs_descriptors,
811dbf499cfSJanusz Dziedzic 			hidg_hs_descriptors, hidg_ss_descriptors, NULL);
81200a2430fSAndrzej Pietrasiewicz 	if (status)
81300a2430fSAndrzej Pietrasiewicz 		goto fail;
81400a2430fSAndrzej Pietrasiewicz 
81533e4c1a9SKrzysztof Opasiak 	spin_lock_init(&hidg->write_spinlock);
816749494b6SKrzysztof Opasiak 	hidg->write_pending = 1;
817749494b6SKrzysztof Opasiak 	hidg->req = NULL;
81833e4c1a9SKrzysztof Opasiak 	spin_lock_init(&hidg->read_spinlock);
81900a2430fSAndrzej Pietrasiewicz 	init_waitqueue_head(&hidg->write_queue);
82000a2430fSAndrzej Pietrasiewicz 	init_waitqueue_head(&hidg->read_queue);
82100a2430fSAndrzej Pietrasiewicz 	INIT_LIST_HEAD(&hidg->completed_out_req);
82200a2430fSAndrzej Pietrasiewicz 
82300a2430fSAndrzej Pietrasiewicz 	/* create char device */
82400a2430fSAndrzej Pietrasiewicz 	cdev_init(&hidg->cdev, &f_hidg_fops);
82500a2430fSAndrzej Pietrasiewicz 	dev = MKDEV(major, hidg->minor);
82600a2430fSAndrzej Pietrasiewicz 	status = cdev_add(&hidg->cdev, dev, 1);
82700a2430fSAndrzej Pietrasiewicz 	if (status)
828d12a8727SPavitrakumar Managutte 		goto fail_free_descs;
82900a2430fSAndrzej Pietrasiewicz 
83063406087SAndrzej Pietrasiewicz 	device = device_create(hidg_class, NULL, dev, NULL,
83163406087SAndrzej Pietrasiewicz 			       "%s%d", "hidg", hidg->minor);
83263406087SAndrzej Pietrasiewicz 	if (IS_ERR(device)) {
83363406087SAndrzej Pietrasiewicz 		status = PTR_ERR(device);
83463406087SAndrzej Pietrasiewicz 		goto del;
83563406087SAndrzej Pietrasiewicz 	}
83600a2430fSAndrzej Pietrasiewicz 
83700a2430fSAndrzej Pietrasiewicz 	return 0;
83863406087SAndrzej Pietrasiewicz del:
83963406087SAndrzej Pietrasiewicz 	cdev_del(&hidg->cdev);
840d12a8727SPavitrakumar Managutte fail_free_descs:
841d12a8727SPavitrakumar Managutte 	usb_free_all_descriptors(f);
84200a2430fSAndrzej Pietrasiewicz fail:
84300a2430fSAndrzej Pietrasiewicz 	ERROR(f->config->cdev, "hidg_bind FAILED\n");
84414794d71SFelipe F. Tonello 	if (hidg->req != NULL)
84514794d71SFelipe F. Tonello 		free_ep_req(hidg->in_ep, hidg->req);
84600a2430fSAndrzej Pietrasiewicz 
84700a2430fSAndrzej Pietrasiewicz 	return status;
84800a2430fSAndrzej Pietrasiewicz }
84900a2430fSAndrzej Pietrasiewicz 
850cb382536SAndrzej Pietrasiewicz static inline int hidg_get_minor(void)
851cb382536SAndrzej Pietrasiewicz {
852cb382536SAndrzej Pietrasiewicz 	int ret;
853cb382536SAndrzej Pietrasiewicz 
854cb382536SAndrzej Pietrasiewicz 	ret = ida_simple_get(&hidg_ida, 0, 0, GFP_KERNEL);
855774cf72fSAndrzej Pietrasiewicz 	if (ret >= HIDG_MINORS) {
856774cf72fSAndrzej Pietrasiewicz 		ida_simple_remove(&hidg_ida, ret);
857774cf72fSAndrzej Pietrasiewicz 		ret = -ENODEV;
858774cf72fSAndrzej Pietrasiewicz 	}
859cb382536SAndrzej Pietrasiewicz 
860cb382536SAndrzej Pietrasiewicz 	return ret;
861cb382536SAndrzej Pietrasiewicz }
862cb382536SAndrzej Pietrasiewicz 
86321a9476aSAndrzej Pietrasiewicz static inline struct f_hid_opts *to_f_hid_opts(struct config_item *item)
86421a9476aSAndrzej Pietrasiewicz {
86521a9476aSAndrzej Pietrasiewicz 	return container_of(to_config_group(item), struct f_hid_opts,
86621a9476aSAndrzej Pietrasiewicz 			    func_inst.group);
86721a9476aSAndrzej Pietrasiewicz }
86821a9476aSAndrzej Pietrasiewicz 
86921a9476aSAndrzej Pietrasiewicz static void hid_attr_release(struct config_item *item)
87021a9476aSAndrzej Pietrasiewicz {
87121a9476aSAndrzej Pietrasiewicz 	struct f_hid_opts *opts = to_f_hid_opts(item);
87221a9476aSAndrzej Pietrasiewicz 
87321a9476aSAndrzej Pietrasiewicz 	usb_put_function_instance(&opts->func_inst);
87421a9476aSAndrzej Pietrasiewicz }
87521a9476aSAndrzej Pietrasiewicz 
87621a9476aSAndrzej Pietrasiewicz static struct configfs_item_operations hidg_item_ops = {
87721a9476aSAndrzej Pietrasiewicz 	.release	= hid_attr_release,
87821a9476aSAndrzej Pietrasiewicz };
87921a9476aSAndrzej Pietrasiewicz 
88021a9476aSAndrzej Pietrasiewicz #define F_HID_OPT(name, prec, limit)					\
881da4e527cSChristoph Hellwig static ssize_t f_hid_opts_##name##_show(struct config_item *item, char *page)\
88221a9476aSAndrzej Pietrasiewicz {									\
883da4e527cSChristoph Hellwig 	struct f_hid_opts *opts = to_f_hid_opts(item);			\
88421a9476aSAndrzej Pietrasiewicz 	int result;							\
88521a9476aSAndrzej Pietrasiewicz 									\
88621a9476aSAndrzej Pietrasiewicz 	mutex_lock(&opts->lock);					\
88721a9476aSAndrzej Pietrasiewicz 	result = sprintf(page, "%d\n", opts->name);			\
88821a9476aSAndrzej Pietrasiewicz 	mutex_unlock(&opts->lock);					\
88921a9476aSAndrzej Pietrasiewicz 									\
89021a9476aSAndrzej Pietrasiewicz 	return result;							\
89121a9476aSAndrzej Pietrasiewicz }									\
89221a9476aSAndrzej Pietrasiewicz 									\
893da4e527cSChristoph Hellwig static ssize_t f_hid_opts_##name##_store(struct config_item *item,	\
89421a9476aSAndrzej Pietrasiewicz 					 const char *page, size_t len)	\
89521a9476aSAndrzej Pietrasiewicz {									\
896da4e527cSChristoph Hellwig 	struct f_hid_opts *opts = to_f_hid_opts(item);			\
89721a9476aSAndrzej Pietrasiewicz 	int ret;							\
89821a9476aSAndrzej Pietrasiewicz 	u##prec num;							\
89921a9476aSAndrzej Pietrasiewicz 									\
90021a9476aSAndrzej Pietrasiewicz 	mutex_lock(&opts->lock);					\
90121a9476aSAndrzej Pietrasiewicz 	if (opts->refcnt) {						\
90221a9476aSAndrzej Pietrasiewicz 		ret = -EBUSY;						\
90321a9476aSAndrzej Pietrasiewicz 		goto end;						\
90421a9476aSAndrzej Pietrasiewicz 	}								\
90521a9476aSAndrzej Pietrasiewicz 									\
90621a9476aSAndrzej Pietrasiewicz 	ret = kstrtou##prec(page, 0, &num);				\
90721a9476aSAndrzej Pietrasiewicz 	if (ret)							\
90821a9476aSAndrzej Pietrasiewicz 		goto end;						\
90921a9476aSAndrzej Pietrasiewicz 									\
91021a9476aSAndrzej Pietrasiewicz 	if (num > limit) {						\
91121a9476aSAndrzej Pietrasiewicz 		ret = -EINVAL;						\
91221a9476aSAndrzej Pietrasiewicz 		goto end;						\
91321a9476aSAndrzej Pietrasiewicz 	}								\
91421a9476aSAndrzej Pietrasiewicz 	opts->name = num;						\
91521a9476aSAndrzej Pietrasiewicz 	ret = len;							\
91621a9476aSAndrzej Pietrasiewicz 									\
91721a9476aSAndrzej Pietrasiewicz end:									\
91821a9476aSAndrzej Pietrasiewicz 	mutex_unlock(&opts->lock);					\
91921a9476aSAndrzej Pietrasiewicz 	return ret;							\
92021a9476aSAndrzej Pietrasiewicz }									\
92121a9476aSAndrzej Pietrasiewicz 									\
922da4e527cSChristoph Hellwig CONFIGFS_ATTR(f_hid_opts_, name)
92321a9476aSAndrzej Pietrasiewicz 
92421a9476aSAndrzej Pietrasiewicz F_HID_OPT(subclass, 8, 255);
92521a9476aSAndrzej Pietrasiewicz F_HID_OPT(protocol, 8, 255);
92639a2ac27SAndrzej Pietrasiewicz F_HID_OPT(report_length, 16, 65535);
92721a9476aSAndrzej Pietrasiewicz 
928da4e527cSChristoph Hellwig static ssize_t f_hid_opts_report_desc_show(struct config_item *item, char *page)
92921a9476aSAndrzej Pietrasiewicz {
930da4e527cSChristoph Hellwig 	struct f_hid_opts *opts = to_f_hid_opts(item);
93121a9476aSAndrzej Pietrasiewicz 	int result;
93221a9476aSAndrzej Pietrasiewicz 
93321a9476aSAndrzej Pietrasiewicz 	mutex_lock(&opts->lock);
93421a9476aSAndrzej Pietrasiewicz 	result = opts->report_desc_length;
93521a9476aSAndrzej Pietrasiewicz 	memcpy(page, opts->report_desc, opts->report_desc_length);
93621a9476aSAndrzej Pietrasiewicz 	mutex_unlock(&opts->lock);
93721a9476aSAndrzej Pietrasiewicz 
93821a9476aSAndrzej Pietrasiewicz 	return result;
93921a9476aSAndrzej Pietrasiewicz }
94021a9476aSAndrzej Pietrasiewicz 
941da4e527cSChristoph Hellwig static ssize_t f_hid_opts_report_desc_store(struct config_item *item,
94221a9476aSAndrzej Pietrasiewicz 					    const char *page, size_t len)
94321a9476aSAndrzej Pietrasiewicz {
944da4e527cSChristoph Hellwig 	struct f_hid_opts *opts = to_f_hid_opts(item);
94521a9476aSAndrzej Pietrasiewicz 	int ret = -EBUSY;
94621a9476aSAndrzej Pietrasiewicz 	char *d;
94721a9476aSAndrzej Pietrasiewicz 
94821a9476aSAndrzej Pietrasiewicz 	mutex_lock(&opts->lock);
94921a9476aSAndrzej Pietrasiewicz 
95021a9476aSAndrzej Pietrasiewicz 	if (opts->refcnt)
95121a9476aSAndrzej Pietrasiewicz 		goto end;
95221a9476aSAndrzej Pietrasiewicz 	if (len > PAGE_SIZE) {
95321a9476aSAndrzej Pietrasiewicz 		ret = -ENOSPC;
95421a9476aSAndrzej Pietrasiewicz 		goto end;
95521a9476aSAndrzej Pietrasiewicz 	}
95621a9476aSAndrzej Pietrasiewicz 	d = kmemdup(page, len, GFP_KERNEL);
95721a9476aSAndrzej Pietrasiewicz 	if (!d) {
95821a9476aSAndrzej Pietrasiewicz 		ret = -ENOMEM;
95921a9476aSAndrzej Pietrasiewicz 		goto end;
96021a9476aSAndrzej Pietrasiewicz 	}
96121a9476aSAndrzej Pietrasiewicz 	kfree(opts->report_desc);
96221a9476aSAndrzej Pietrasiewicz 	opts->report_desc = d;
96321a9476aSAndrzej Pietrasiewicz 	opts->report_desc_length = len;
96421a9476aSAndrzej Pietrasiewicz 	opts->report_desc_alloc = true;
96521a9476aSAndrzej Pietrasiewicz 	ret = len;
96621a9476aSAndrzej Pietrasiewicz end:
96721a9476aSAndrzej Pietrasiewicz 	mutex_unlock(&opts->lock);
96821a9476aSAndrzej Pietrasiewicz 	return ret;
96921a9476aSAndrzej Pietrasiewicz }
97021a9476aSAndrzej Pietrasiewicz 
971da4e527cSChristoph Hellwig CONFIGFS_ATTR(f_hid_opts_, report_desc);
97221a9476aSAndrzej Pietrasiewicz 
973ed6fe1f5SJohannes Berg static ssize_t f_hid_opts_dev_show(struct config_item *item, char *page)
974ed6fe1f5SJohannes Berg {
975ed6fe1f5SJohannes Berg 	struct f_hid_opts *opts = to_f_hid_opts(item);
976ed6fe1f5SJohannes Berg 
977ed6fe1f5SJohannes Berg 	return sprintf(page, "%d:%d\n", major, opts->minor);
978ed6fe1f5SJohannes Berg }
979ed6fe1f5SJohannes Berg 
980ed6fe1f5SJohannes Berg CONFIGFS_ATTR_RO(f_hid_opts_, dev);
981ed6fe1f5SJohannes Berg 
98221a9476aSAndrzej Pietrasiewicz static struct configfs_attribute *hid_attrs[] = {
983da4e527cSChristoph Hellwig 	&f_hid_opts_attr_subclass,
984da4e527cSChristoph Hellwig 	&f_hid_opts_attr_protocol,
985da4e527cSChristoph Hellwig 	&f_hid_opts_attr_report_length,
986da4e527cSChristoph Hellwig 	&f_hid_opts_attr_report_desc,
987ed6fe1f5SJohannes Berg 	&f_hid_opts_attr_dev,
98821a9476aSAndrzej Pietrasiewicz 	NULL,
98921a9476aSAndrzej Pietrasiewicz };
99021a9476aSAndrzej Pietrasiewicz 
99197363902SBhumika Goyal static const struct config_item_type hid_func_type = {
99221a9476aSAndrzej Pietrasiewicz 	.ct_item_ops	= &hidg_item_ops,
99321a9476aSAndrzej Pietrasiewicz 	.ct_attrs	= hid_attrs,
99421a9476aSAndrzej Pietrasiewicz 	.ct_owner	= THIS_MODULE,
99521a9476aSAndrzej Pietrasiewicz };
99621a9476aSAndrzej Pietrasiewicz 
997cb382536SAndrzej Pietrasiewicz static inline void hidg_put_minor(int minor)
998cb382536SAndrzej Pietrasiewicz {
999cb382536SAndrzej Pietrasiewicz 	ida_simple_remove(&hidg_ida, minor);
1000cb382536SAndrzej Pietrasiewicz }
1001cb382536SAndrzej Pietrasiewicz 
1002cb382536SAndrzej Pietrasiewicz static void hidg_free_inst(struct usb_function_instance *f)
1003cb382536SAndrzej Pietrasiewicz {
1004cb382536SAndrzej Pietrasiewicz 	struct f_hid_opts *opts;
1005cb382536SAndrzej Pietrasiewicz 
1006cb382536SAndrzej Pietrasiewicz 	opts = container_of(f, struct f_hid_opts, func_inst);
1007cb382536SAndrzej Pietrasiewicz 
1008cb382536SAndrzej Pietrasiewicz 	mutex_lock(&hidg_ida_lock);
1009cb382536SAndrzej Pietrasiewicz 
1010cb382536SAndrzej Pietrasiewicz 	hidg_put_minor(opts->minor);
101199c49407SMatthew Wilcox 	if (ida_is_empty(&hidg_ida))
1012cb382536SAndrzej Pietrasiewicz 		ghid_cleanup();
1013cb382536SAndrzej Pietrasiewicz 
1014cb382536SAndrzej Pietrasiewicz 	mutex_unlock(&hidg_ida_lock);
1015cb382536SAndrzej Pietrasiewicz 
1016cb382536SAndrzej Pietrasiewicz 	if (opts->report_desc_alloc)
1017cb382536SAndrzej Pietrasiewicz 		kfree(opts->report_desc);
1018cb382536SAndrzej Pietrasiewicz 
1019cb382536SAndrzej Pietrasiewicz 	kfree(opts);
1020cb382536SAndrzej Pietrasiewicz }
1021cb382536SAndrzej Pietrasiewicz 
1022cb382536SAndrzej Pietrasiewicz static struct usb_function_instance *hidg_alloc_inst(void)
1023cb382536SAndrzej Pietrasiewicz {
1024cb382536SAndrzej Pietrasiewicz 	struct f_hid_opts *opts;
1025cb382536SAndrzej Pietrasiewicz 	struct usb_function_instance *ret;
1026cb382536SAndrzej Pietrasiewicz 	int status = 0;
1027cb382536SAndrzej Pietrasiewicz 
1028cb382536SAndrzej Pietrasiewicz 	opts = kzalloc(sizeof(*opts), GFP_KERNEL);
1029cb382536SAndrzej Pietrasiewicz 	if (!opts)
1030cb382536SAndrzej Pietrasiewicz 		return ERR_PTR(-ENOMEM);
103121a9476aSAndrzej Pietrasiewicz 	mutex_init(&opts->lock);
1032cb382536SAndrzej Pietrasiewicz 	opts->func_inst.free_func_inst = hidg_free_inst;
1033cb382536SAndrzej Pietrasiewicz 	ret = &opts->func_inst;
1034cb382536SAndrzej Pietrasiewicz 
1035cb382536SAndrzej Pietrasiewicz 	mutex_lock(&hidg_ida_lock);
1036cb382536SAndrzej Pietrasiewicz 
103799c49407SMatthew Wilcox 	if (ida_is_empty(&hidg_ida)) {
1038cb382536SAndrzej Pietrasiewicz 		status = ghid_setup(NULL, HIDG_MINORS);
1039cb382536SAndrzej Pietrasiewicz 		if (status)  {
1040cb382536SAndrzej Pietrasiewicz 			ret = ERR_PTR(status);
1041cb382536SAndrzej Pietrasiewicz 			kfree(opts);
1042cb382536SAndrzej Pietrasiewicz 			goto unlock;
1043cb382536SAndrzej Pietrasiewicz 		}
1044cb382536SAndrzej Pietrasiewicz 	}
1045cb382536SAndrzej Pietrasiewicz 
1046cb382536SAndrzej Pietrasiewicz 	opts->minor = hidg_get_minor();
1047cb382536SAndrzej Pietrasiewicz 	if (opts->minor < 0) {
1048cb382536SAndrzej Pietrasiewicz 		ret = ERR_PTR(opts->minor);
1049cb382536SAndrzej Pietrasiewicz 		kfree(opts);
105099c49407SMatthew Wilcox 		if (ida_is_empty(&hidg_ida))
1051cb382536SAndrzej Pietrasiewicz 			ghid_cleanup();
1052828f6148SDan Carpenter 		goto unlock;
1053cb382536SAndrzej Pietrasiewicz 	}
105421a9476aSAndrzej Pietrasiewicz 	config_group_init_type_name(&opts->func_inst.group, "", &hid_func_type);
1055cb382536SAndrzej Pietrasiewicz 
1056cb382536SAndrzej Pietrasiewicz unlock:
1057cb382536SAndrzej Pietrasiewicz 	mutex_unlock(&hidg_ida_lock);
1058cb382536SAndrzej Pietrasiewicz 	return ret;
1059cb382536SAndrzej Pietrasiewicz }
1060cb382536SAndrzej Pietrasiewicz 
1061cb382536SAndrzej Pietrasiewicz static void hidg_free(struct usb_function *f)
1062cb382536SAndrzej Pietrasiewicz {
1063cb382536SAndrzej Pietrasiewicz 	struct f_hidg *hidg;
106421a9476aSAndrzej Pietrasiewicz 	struct f_hid_opts *opts;
1065cb382536SAndrzej Pietrasiewicz 
1066cb382536SAndrzej Pietrasiewicz 	hidg = func_to_hidg(f);
106721a9476aSAndrzej Pietrasiewicz 	opts = container_of(f->fi, struct f_hid_opts, func_inst);
1068cb382536SAndrzej Pietrasiewicz 	kfree(hidg->report_desc);
1069cb382536SAndrzej Pietrasiewicz 	kfree(hidg);
107021a9476aSAndrzej Pietrasiewicz 	mutex_lock(&opts->lock);
107121a9476aSAndrzej Pietrasiewicz 	--opts->refcnt;
107221a9476aSAndrzej Pietrasiewicz 	mutex_unlock(&opts->lock);
1073cb382536SAndrzej Pietrasiewicz }
1074cb382536SAndrzej Pietrasiewicz 
107500a2430fSAndrzej Pietrasiewicz static void hidg_unbind(struct usb_configuration *c, struct usb_function *f)
107600a2430fSAndrzej Pietrasiewicz {
107700a2430fSAndrzej Pietrasiewicz 	struct f_hidg *hidg = func_to_hidg(f);
107800a2430fSAndrzej Pietrasiewicz 
107900a2430fSAndrzej Pietrasiewicz 	device_destroy(hidg_class, MKDEV(major, hidg->minor));
108000a2430fSAndrzej Pietrasiewicz 	cdev_del(&hidg->cdev);
108100a2430fSAndrzej Pietrasiewicz 
108200a2430fSAndrzej Pietrasiewicz 	usb_free_all_descriptors(f);
108300a2430fSAndrzej Pietrasiewicz }
108400a2430fSAndrzej Pietrasiewicz 
10850fc57ea0SFengguang Wu static struct usb_function *hidg_alloc(struct usb_function_instance *fi)
108600a2430fSAndrzej Pietrasiewicz {
108700a2430fSAndrzej Pietrasiewicz 	struct f_hidg *hidg;
1088cb382536SAndrzej Pietrasiewicz 	struct f_hid_opts *opts;
108900a2430fSAndrzej Pietrasiewicz 
109000a2430fSAndrzej Pietrasiewicz 	/* allocate and initialize one new instance */
1091cb382536SAndrzej Pietrasiewicz 	hidg = kzalloc(sizeof(*hidg), GFP_KERNEL);
109200a2430fSAndrzej Pietrasiewicz 	if (!hidg)
1093cb382536SAndrzej Pietrasiewicz 		return ERR_PTR(-ENOMEM);
109400a2430fSAndrzej Pietrasiewicz 
1095cb382536SAndrzej Pietrasiewicz 	opts = container_of(fi, struct f_hid_opts, func_inst);
1096cb382536SAndrzej Pietrasiewicz 
109721a9476aSAndrzej Pietrasiewicz 	mutex_lock(&opts->lock);
109821a9476aSAndrzej Pietrasiewicz 	++opts->refcnt;
109921a9476aSAndrzej Pietrasiewicz 
1100cb382536SAndrzej Pietrasiewicz 	hidg->minor = opts->minor;
1101cb382536SAndrzej Pietrasiewicz 	hidg->bInterfaceSubClass = opts->subclass;
1102cb382536SAndrzej Pietrasiewicz 	hidg->bInterfaceProtocol = opts->protocol;
1103cb382536SAndrzej Pietrasiewicz 	hidg->report_length = opts->report_length;
1104cb382536SAndrzej Pietrasiewicz 	hidg->report_desc_length = opts->report_desc_length;
1105cb382536SAndrzej Pietrasiewicz 	if (opts->report_desc) {
1106cb382536SAndrzej Pietrasiewicz 		hidg->report_desc = kmemdup(opts->report_desc,
1107cb382536SAndrzej Pietrasiewicz 					    opts->report_desc_length,
110800a2430fSAndrzej Pietrasiewicz 					    GFP_KERNEL);
110900a2430fSAndrzej Pietrasiewicz 		if (!hidg->report_desc) {
111000a2430fSAndrzej Pietrasiewicz 			kfree(hidg);
111121a9476aSAndrzej Pietrasiewicz 			mutex_unlock(&opts->lock);
1112cb382536SAndrzej Pietrasiewicz 			return ERR_PTR(-ENOMEM);
1113cb382536SAndrzej Pietrasiewicz 		}
111400a2430fSAndrzej Pietrasiewicz 	}
111500a2430fSAndrzej Pietrasiewicz 
111621a9476aSAndrzej Pietrasiewicz 	mutex_unlock(&opts->lock);
111721a9476aSAndrzej Pietrasiewicz 
111800a2430fSAndrzej Pietrasiewicz 	hidg->func.name    = "hid";
111900a2430fSAndrzej Pietrasiewicz 	hidg->func.bind    = hidg_bind;
112000a2430fSAndrzej Pietrasiewicz 	hidg->func.unbind  = hidg_unbind;
112100a2430fSAndrzej Pietrasiewicz 	hidg->func.set_alt = hidg_set_alt;
112200a2430fSAndrzej Pietrasiewicz 	hidg->func.disable = hidg_disable;
112300a2430fSAndrzej Pietrasiewicz 	hidg->func.setup   = hidg_setup;
1124cb382536SAndrzej Pietrasiewicz 	hidg->func.free_func = hidg_free;
112500a2430fSAndrzej Pietrasiewicz 
112600a2430fSAndrzej Pietrasiewicz 	/* this could me made configurable at some point */
112700a2430fSAndrzej Pietrasiewicz 	hidg->qlen	   = 4;
112800a2430fSAndrzej Pietrasiewicz 
1129cb382536SAndrzej Pietrasiewicz 	return &hidg->func;
113000a2430fSAndrzej Pietrasiewicz }
113100a2430fSAndrzej Pietrasiewicz 
1132cb382536SAndrzej Pietrasiewicz DECLARE_USB_FUNCTION_INIT(hid, hidg_alloc_inst, hidg_alloc);
1133cb382536SAndrzej Pietrasiewicz MODULE_LICENSE("GPL");
1134cb382536SAndrzej Pietrasiewicz MODULE_AUTHOR("Fabien Chouteau");
1135cb382536SAndrzej Pietrasiewicz 
1136cb382536SAndrzej Pietrasiewicz int ghid_setup(struct usb_gadget *g, int count)
113700a2430fSAndrzej Pietrasiewicz {
113800a2430fSAndrzej Pietrasiewicz 	int status;
113900a2430fSAndrzej Pietrasiewicz 	dev_t dev;
114000a2430fSAndrzej Pietrasiewicz 
114100a2430fSAndrzej Pietrasiewicz 	hidg_class = class_create(THIS_MODULE, "hidg");
114206529407SAndrzej Pietrasiewicz 	if (IS_ERR(hidg_class)) {
11430448d38cSDan Carpenter 		status = PTR_ERR(hidg_class);
114406529407SAndrzej Pietrasiewicz 		hidg_class = NULL;
11450448d38cSDan Carpenter 		return status;
114600a2430fSAndrzej Pietrasiewicz 	}
114700a2430fSAndrzej Pietrasiewicz 
114800a2430fSAndrzej Pietrasiewicz 	status = alloc_chrdev_region(&dev, 0, count, "hidg");
11490448d38cSDan Carpenter 	if (status) {
11500448d38cSDan Carpenter 		class_destroy(hidg_class);
11510448d38cSDan Carpenter 		hidg_class = NULL;
115200a2430fSAndrzej Pietrasiewicz 		return status;
115300a2430fSAndrzej Pietrasiewicz 	}
115400a2430fSAndrzej Pietrasiewicz 
11550448d38cSDan Carpenter 	major = MAJOR(dev);
11560448d38cSDan Carpenter 	minors = count;
11570448d38cSDan Carpenter 
11580448d38cSDan Carpenter 	return 0;
115900a2430fSAndrzej Pietrasiewicz }
116000a2430fSAndrzej Pietrasiewicz 
116100a2430fSAndrzej Pietrasiewicz void ghid_cleanup(void)
116200a2430fSAndrzej Pietrasiewicz {
116300a2430fSAndrzej Pietrasiewicz 	if (major) {
116400a2430fSAndrzej Pietrasiewicz 		unregister_chrdev_region(MKDEV(major, 0), minors);
116500a2430fSAndrzej Pietrasiewicz 		major = minors = 0;
116600a2430fSAndrzej Pietrasiewicz 	}
116700a2430fSAndrzej Pietrasiewicz 
116800a2430fSAndrzej Pietrasiewicz 	class_destroy(hidg_class);
116900a2430fSAndrzej Pietrasiewicz 	hidg_class = NULL;
117000a2430fSAndrzej Pietrasiewicz }
1171