xref: /openbmc/linux/drivers/usb/gadget/function/f_hid.c (revision 33e4c1a9987a1fc3b42c3b534100b5b006d55c61)
100a2430fSAndrzej Pietrasiewicz /*
200a2430fSAndrzej Pietrasiewicz  * f_hid.c -- USB HID function driver
300a2430fSAndrzej Pietrasiewicz  *
400a2430fSAndrzej Pietrasiewicz  * Copyright (C) 2010 Fabien Chouteau <fabien.chouteau@barco.com>
500a2430fSAndrzej Pietrasiewicz  *
600a2430fSAndrzej Pietrasiewicz  * This program is free software; you can redistribute it and/or modify
700a2430fSAndrzej Pietrasiewicz  * it under the terms of the GNU General Public License as published by
800a2430fSAndrzej Pietrasiewicz  * the Free Software Foundation; either version 2 of the License, or
900a2430fSAndrzej Pietrasiewicz  * (at your option) any later version.
1000a2430fSAndrzej Pietrasiewicz  */
1100a2430fSAndrzej Pietrasiewicz 
1200a2430fSAndrzej Pietrasiewicz #include <linux/kernel.h>
1300a2430fSAndrzej Pietrasiewicz #include <linux/module.h>
1400a2430fSAndrzej Pietrasiewicz #include <linux/hid.h>
15cb382536SAndrzej Pietrasiewicz #include <linux/idr.h>
1600a2430fSAndrzej Pietrasiewicz #include <linux/cdev.h>
1700a2430fSAndrzej Pietrasiewicz #include <linux/mutex.h>
1800a2430fSAndrzej Pietrasiewicz #include <linux/poll.h>
1900a2430fSAndrzej Pietrasiewicz #include <linux/uaccess.h>
2000a2430fSAndrzej Pietrasiewicz #include <linux/wait.h>
2100a2430fSAndrzej Pietrasiewicz #include <linux/sched.h>
2200a2430fSAndrzej Pietrasiewicz #include <linux/usb/g_hid.h>
2300a2430fSAndrzej Pietrasiewicz 
2400a2430fSAndrzej Pietrasiewicz #include "u_f.h"
25cb382536SAndrzej Pietrasiewicz #include "u_hid.h"
26cb382536SAndrzej Pietrasiewicz 
27cb382536SAndrzej Pietrasiewicz #define HIDG_MINORS	4
2800a2430fSAndrzej Pietrasiewicz 
2900a2430fSAndrzej Pietrasiewicz static int major, minors;
3000a2430fSAndrzej Pietrasiewicz static struct class *hidg_class;
31cb382536SAndrzej Pietrasiewicz static DEFINE_IDA(hidg_ida);
32cb382536SAndrzej Pietrasiewicz static DEFINE_MUTEX(hidg_ida_lock); /* protects access to hidg_ida */
3300a2430fSAndrzej Pietrasiewicz 
3400a2430fSAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/
3500a2430fSAndrzej Pietrasiewicz /*                            HID gadget struct                            */
3600a2430fSAndrzej Pietrasiewicz 
3700a2430fSAndrzej Pietrasiewicz struct f_hidg_req_list {
3800a2430fSAndrzej Pietrasiewicz 	struct usb_request	*req;
3900a2430fSAndrzej Pietrasiewicz 	unsigned int		pos;
4000a2430fSAndrzej Pietrasiewicz 	struct list_head 	list;
4100a2430fSAndrzej Pietrasiewicz };
4200a2430fSAndrzej Pietrasiewicz 
4300a2430fSAndrzej Pietrasiewicz struct f_hidg {
4400a2430fSAndrzej Pietrasiewicz 	/* configuration */
4500a2430fSAndrzej Pietrasiewicz 	unsigned char			bInterfaceSubClass;
4600a2430fSAndrzej Pietrasiewicz 	unsigned char			bInterfaceProtocol;
4700a2430fSAndrzej Pietrasiewicz 	unsigned short			report_desc_length;
4800a2430fSAndrzej Pietrasiewicz 	char				*report_desc;
4900a2430fSAndrzej Pietrasiewicz 	unsigned short			report_length;
5000a2430fSAndrzej Pietrasiewicz 
5100a2430fSAndrzej Pietrasiewicz 	/* recv report */
5200a2430fSAndrzej Pietrasiewicz 	struct list_head		completed_out_req;
53*33e4c1a9SKrzysztof Opasiak 	spinlock_t			read_spinlock;
5400a2430fSAndrzej Pietrasiewicz 	wait_queue_head_t		read_queue;
5500a2430fSAndrzej Pietrasiewicz 	unsigned int			qlen;
5600a2430fSAndrzej Pietrasiewicz 
5700a2430fSAndrzej Pietrasiewicz 	/* send report */
58*33e4c1a9SKrzysztof Opasiak 	spinlock_t			write_spinlock;
5900a2430fSAndrzej Pietrasiewicz 	bool				write_pending;
6000a2430fSAndrzej Pietrasiewicz 	wait_queue_head_t		write_queue;
6100a2430fSAndrzej Pietrasiewicz 	struct usb_request		*req;
6200a2430fSAndrzej Pietrasiewicz 
6300a2430fSAndrzej Pietrasiewicz 	int				minor;
6400a2430fSAndrzej Pietrasiewicz 	struct cdev			cdev;
6500a2430fSAndrzej Pietrasiewicz 	struct usb_function		func;
6600a2430fSAndrzej Pietrasiewicz 
6700a2430fSAndrzej Pietrasiewicz 	struct usb_ep			*in_ep;
6800a2430fSAndrzej Pietrasiewicz 	struct usb_ep			*out_ep;
6900a2430fSAndrzej Pietrasiewicz };
7000a2430fSAndrzej Pietrasiewicz 
7100a2430fSAndrzej Pietrasiewicz static inline struct f_hidg *func_to_hidg(struct usb_function *f)
7200a2430fSAndrzej Pietrasiewicz {
7300a2430fSAndrzej Pietrasiewicz 	return container_of(f, struct f_hidg, func);
7400a2430fSAndrzej Pietrasiewicz }
7500a2430fSAndrzej Pietrasiewicz 
7600a2430fSAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/
7700a2430fSAndrzej Pietrasiewicz /*                           Static descriptors                            */
7800a2430fSAndrzej Pietrasiewicz 
7900a2430fSAndrzej Pietrasiewicz static struct usb_interface_descriptor hidg_interface_desc = {
8000a2430fSAndrzej Pietrasiewicz 	.bLength		= sizeof hidg_interface_desc,
8100a2430fSAndrzej Pietrasiewicz 	.bDescriptorType	= USB_DT_INTERFACE,
8200a2430fSAndrzej Pietrasiewicz 	/* .bInterfaceNumber	= DYNAMIC */
8300a2430fSAndrzej Pietrasiewicz 	.bAlternateSetting	= 0,
8400a2430fSAndrzej Pietrasiewicz 	.bNumEndpoints		= 2,
8500a2430fSAndrzej Pietrasiewicz 	.bInterfaceClass	= USB_CLASS_HID,
8600a2430fSAndrzej Pietrasiewicz 	/* .bInterfaceSubClass	= DYNAMIC */
8700a2430fSAndrzej Pietrasiewicz 	/* .bInterfaceProtocol	= DYNAMIC */
8800a2430fSAndrzej Pietrasiewicz 	/* .iInterface		= DYNAMIC */
8900a2430fSAndrzej Pietrasiewicz };
9000a2430fSAndrzej Pietrasiewicz 
9100a2430fSAndrzej Pietrasiewicz static struct hid_descriptor hidg_desc = {
9200a2430fSAndrzej Pietrasiewicz 	.bLength			= sizeof hidg_desc,
9300a2430fSAndrzej Pietrasiewicz 	.bDescriptorType		= HID_DT_HID,
9400a2430fSAndrzej Pietrasiewicz 	.bcdHID				= 0x0101,
9500a2430fSAndrzej Pietrasiewicz 	.bCountryCode			= 0x00,
9600a2430fSAndrzej Pietrasiewicz 	.bNumDescriptors		= 0x1,
9700a2430fSAndrzej Pietrasiewicz 	/*.desc[0].bDescriptorType	= DYNAMIC */
9800a2430fSAndrzej Pietrasiewicz 	/*.desc[0].wDescriptorLenght	= DYNAMIC */
9900a2430fSAndrzej Pietrasiewicz };
10000a2430fSAndrzej Pietrasiewicz 
101dbf499cfSJanusz Dziedzic /* Super-Speed Support */
102dbf499cfSJanusz Dziedzic 
103dbf499cfSJanusz Dziedzic static struct usb_endpoint_descriptor hidg_ss_in_ep_desc = {
104dbf499cfSJanusz Dziedzic 	.bLength		= USB_DT_ENDPOINT_SIZE,
105dbf499cfSJanusz Dziedzic 	.bDescriptorType	= USB_DT_ENDPOINT,
106dbf499cfSJanusz Dziedzic 	.bEndpointAddress	= USB_DIR_IN,
107dbf499cfSJanusz Dziedzic 	.bmAttributes		= USB_ENDPOINT_XFER_INT,
108dbf499cfSJanusz Dziedzic 	/*.wMaxPacketSize	= DYNAMIC */
109dbf499cfSJanusz Dziedzic 	.bInterval		= 4, /* FIXME: Add this field in the
110dbf499cfSJanusz Dziedzic 				      * HID gadget configuration?
111dbf499cfSJanusz Dziedzic 				      * (struct hidg_func_descriptor)
112dbf499cfSJanusz Dziedzic 				      */
113dbf499cfSJanusz Dziedzic };
114dbf499cfSJanusz Dziedzic 
115dbf499cfSJanusz Dziedzic static struct usb_ss_ep_comp_descriptor hidg_ss_in_comp_desc = {
116dbf499cfSJanusz Dziedzic 	.bLength                = sizeof(hidg_ss_in_comp_desc),
117dbf499cfSJanusz Dziedzic 	.bDescriptorType        = USB_DT_SS_ENDPOINT_COMP,
118dbf499cfSJanusz Dziedzic 
119dbf499cfSJanusz Dziedzic 	/* .bMaxBurst           = 0, */
120dbf499cfSJanusz Dziedzic 	/* .bmAttributes        = 0, */
121dbf499cfSJanusz Dziedzic 	/* .wBytesPerInterval   = DYNAMIC */
122dbf499cfSJanusz Dziedzic };
123dbf499cfSJanusz Dziedzic 
124dbf499cfSJanusz Dziedzic static struct usb_endpoint_descriptor hidg_ss_out_ep_desc = {
125dbf499cfSJanusz Dziedzic 	.bLength		= USB_DT_ENDPOINT_SIZE,
126dbf499cfSJanusz Dziedzic 	.bDescriptorType	= USB_DT_ENDPOINT,
127dbf499cfSJanusz Dziedzic 	.bEndpointAddress	= USB_DIR_OUT,
128dbf499cfSJanusz Dziedzic 	.bmAttributes		= USB_ENDPOINT_XFER_INT,
129dbf499cfSJanusz Dziedzic 	/*.wMaxPacketSize	= DYNAMIC */
130dbf499cfSJanusz Dziedzic 	.bInterval		= 4, /* FIXME: Add this field in the
131dbf499cfSJanusz Dziedzic 				      * HID gadget configuration?
132dbf499cfSJanusz Dziedzic 				      * (struct hidg_func_descriptor)
133dbf499cfSJanusz Dziedzic 				      */
134dbf499cfSJanusz Dziedzic };
135dbf499cfSJanusz Dziedzic 
136dbf499cfSJanusz Dziedzic static struct usb_ss_ep_comp_descriptor hidg_ss_out_comp_desc = {
137dbf499cfSJanusz Dziedzic 	.bLength                = sizeof(hidg_ss_out_comp_desc),
138dbf499cfSJanusz Dziedzic 	.bDescriptorType        = USB_DT_SS_ENDPOINT_COMP,
139dbf499cfSJanusz Dziedzic 
140dbf499cfSJanusz Dziedzic 	/* .bMaxBurst           = 0, */
141dbf499cfSJanusz Dziedzic 	/* .bmAttributes        = 0, */
142dbf499cfSJanusz Dziedzic 	/* .wBytesPerInterval   = DYNAMIC */
143dbf499cfSJanusz Dziedzic };
144dbf499cfSJanusz Dziedzic 
145dbf499cfSJanusz Dziedzic static struct usb_descriptor_header *hidg_ss_descriptors[] = {
146dbf499cfSJanusz Dziedzic 	(struct usb_descriptor_header *)&hidg_interface_desc,
147dbf499cfSJanusz Dziedzic 	(struct usb_descriptor_header *)&hidg_desc,
148dbf499cfSJanusz Dziedzic 	(struct usb_descriptor_header *)&hidg_ss_in_ep_desc,
149dbf499cfSJanusz Dziedzic 	(struct usb_descriptor_header *)&hidg_ss_in_comp_desc,
150dbf499cfSJanusz Dziedzic 	(struct usb_descriptor_header *)&hidg_ss_out_ep_desc,
151dbf499cfSJanusz Dziedzic 	(struct usb_descriptor_header *)&hidg_ss_out_comp_desc,
152dbf499cfSJanusz Dziedzic 	NULL,
153dbf499cfSJanusz Dziedzic };
154dbf499cfSJanusz Dziedzic 
15500a2430fSAndrzej Pietrasiewicz /* High-Speed Support */
15600a2430fSAndrzej Pietrasiewicz 
15700a2430fSAndrzej Pietrasiewicz static struct usb_endpoint_descriptor hidg_hs_in_ep_desc = {
15800a2430fSAndrzej Pietrasiewicz 	.bLength		= USB_DT_ENDPOINT_SIZE,
15900a2430fSAndrzej Pietrasiewicz 	.bDescriptorType	= USB_DT_ENDPOINT,
16000a2430fSAndrzej Pietrasiewicz 	.bEndpointAddress	= USB_DIR_IN,
16100a2430fSAndrzej Pietrasiewicz 	.bmAttributes		= USB_ENDPOINT_XFER_INT,
16200a2430fSAndrzej Pietrasiewicz 	/*.wMaxPacketSize	= DYNAMIC */
16300a2430fSAndrzej Pietrasiewicz 	.bInterval		= 4, /* FIXME: Add this field in the
16400a2430fSAndrzej Pietrasiewicz 				      * HID gadget configuration?
16500a2430fSAndrzej Pietrasiewicz 				      * (struct hidg_func_descriptor)
16600a2430fSAndrzej Pietrasiewicz 				      */
16700a2430fSAndrzej Pietrasiewicz };
16800a2430fSAndrzej Pietrasiewicz 
16900a2430fSAndrzej Pietrasiewicz static struct usb_endpoint_descriptor hidg_hs_out_ep_desc = {
17000a2430fSAndrzej Pietrasiewicz 	.bLength		= USB_DT_ENDPOINT_SIZE,
17100a2430fSAndrzej Pietrasiewicz 	.bDescriptorType	= USB_DT_ENDPOINT,
17200a2430fSAndrzej Pietrasiewicz 	.bEndpointAddress	= USB_DIR_OUT,
17300a2430fSAndrzej Pietrasiewicz 	.bmAttributes		= USB_ENDPOINT_XFER_INT,
17400a2430fSAndrzej Pietrasiewicz 	/*.wMaxPacketSize	= DYNAMIC */
17500a2430fSAndrzej Pietrasiewicz 	.bInterval		= 4, /* FIXME: Add this field in the
17600a2430fSAndrzej Pietrasiewicz 				      * HID gadget configuration?
17700a2430fSAndrzej Pietrasiewicz 				      * (struct hidg_func_descriptor)
17800a2430fSAndrzej Pietrasiewicz 				      */
17900a2430fSAndrzej Pietrasiewicz };
18000a2430fSAndrzej Pietrasiewicz 
18100a2430fSAndrzej Pietrasiewicz static struct usb_descriptor_header *hidg_hs_descriptors[] = {
18200a2430fSAndrzej Pietrasiewicz 	(struct usb_descriptor_header *)&hidg_interface_desc,
18300a2430fSAndrzej Pietrasiewicz 	(struct usb_descriptor_header *)&hidg_desc,
18400a2430fSAndrzej Pietrasiewicz 	(struct usb_descriptor_header *)&hidg_hs_in_ep_desc,
18500a2430fSAndrzej Pietrasiewicz 	(struct usb_descriptor_header *)&hidg_hs_out_ep_desc,
18600a2430fSAndrzej Pietrasiewicz 	NULL,
18700a2430fSAndrzej Pietrasiewicz };
18800a2430fSAndrzej Pietrasiewicz 
18900a2430fSAndrzej Pietrasiewicz /* Full-Speed Support */
19000a2430fSAndrzej Pietrasiewicz 
19100a2430fSAndrzej Pietrasiewicz static struct usb_endpoint_descriptor hidg_fs_in_ep_desc = {
19200a2430fSAndrzej Pietrasiewicz 	.bLength		= USB_DT_ENDPOINT_SIZE,
19300a2430fSAndrzej Pietrasiewicz 	.bDescriptorType	= USB_DT_ENDPOINT,
19400a2430fSAndrzej Pietrasiewicz 	.bEndpointAddress	= USB_DIR_IN,
19500a2430fSAndrzej Pietrasiewicz 	.bmAttributes		= USB_ENDPOINT_XFER_INT,
19600a2430fSAndrzej Pietrasiewicz 	/*.wMaxPacketSize	= DYNAMIC */
19700a2430fSAndrzej Pietrasiewicz 	.bInterval		= 10, /* FIXME: Add this field in the
19800a2430fSAndrzej Pietrasiewicz 				       * HID gadget configuration?
19900a2430fSAndrzej Pietrasiewicz 				       * (struct hidg_func_descriptor)
20000a2430fSAndrzej Pietrasiewicz 				       */
20100a2430fSAndrzej Pietrasiewicz };
20200a2430fSAndrzej Pietrasiewicz 
20300a2430fSAndrzej Pietrasiewicz static struct usb_endpoint_descriptor hidg_fs_out_ep_desc = {
20400a2430fSAndrzej Pietrasiewicz 	.bLength		= USB_DT_ENDPOINT_SIZE,
20500a2430fSAndrzej Pietrasiewicz 	.bDescriptorType	= USB_DT_ENDPOINT,
20600a2430fSAndrzej Pietrasiewicz 	.bEndpointAddress	= USB_DIR_OUT,
20700a2430fSAndrzej Pietrasiewicz 	.bmAttributes		= USB_ENDPOINT_XFER_INT,
20800a2430fSAndrzej Pietrasiewicz 	/*.wMaxPacketSize	= DYNAMIC */
20900a2430fSAndrzej Pietrasiewicz 	.bInterval		= 10, /* FIXME: Add this field in the
21000a2430fSAndrzej Pietrasiewicz 				       * HID gadget configuration?
21100a2430fSAndrzej Pietrasiewicz 				       * (struct hidg_func_descriptor)
21200a2430fSAndrzej Pietrasiewicz 				       */
21300a2430fSAndrzej Pietrasiewicz };
21400a2430fSAndrzej Pietrasiewicz 
21500a2430fSAndrzej Pietrasiewicz static struct usb_descriptor_header *hidg_fs_descriptors[] = {
21600a2430fSAndrzej Pietrasiewicz 	(struct usb_descriptor_header *)&hidg_interface_desc,
21700a2430fSAndrzej Pietrasiewicz 	(struct usb_descriptor_header *)&hidg_desc,
21800a2430fSAndrzej Pietrasiewicz 	(struct usb_descriptor_header *)&hidg_fs_in_ep_desc,
21900a2430fSAndrzej Pietrasiewicz 	(struct usb_descriptor_header *)&hidg_fs_out_ep_desc,
22000a2430fSAndrzej Pietrasiewicz 	NULL,
22100a2430fSAndrzej Pietrasiewicz };
22200a2430fSAndrzej Pietrasiewicz 
22300a2430fSAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/
224cb382536SAndrzej Pietrasiewicz /*                                 Strings                                 */
225cb382536SAndrzej Pietrasiewicz 
226cb382536SAndrzej Pietrasiewicz #define CT_FUNC_HID_IDX	0
227cb382536SAndrzej Pietrasiewicz 
228cb382536SAndrzej Pietrasiewicz static struct usb_string ct_func_string_defs[] = {
229cb382536SAndrzej Pietrasiewicz 	[CT_FUNC_HID_IDX].s	= "HID Interface",
230cb382536SAndrzej Pietrasiewicz 	{},			/* end of list */
231cb382536SAndrzej Pietrasiewicz };
232cb382536SAndrzej Pietrasiewicz 
233cb382536SAndrzej Pietrasiewicz static struct usb_gadget_strings ct_func_string_table = {
234cb382536SAndrzej Pietrasiewicz 	.language	= 0x0409,	/* en-US */
235cb382536SAndrzej Pietrasiewicz 	.strings	= ct_func_string_defs,
236cb382536SAndrzej Pietrasiewicz };
237cb382536SAndrzej Pietrasiewicz 
238cb382536SAndrzej Pietrasiewicz static struct usb_gadget_strings *ct_func_strings[] = {
239cb382536SAndrzej Pietrasiewicz 	&ct_func_string_table,
240cb382536SAndrzej Pietrasiewicz 	NULL,
241cb382536SAndrzej Pietrasiewicz };
242cb382536SAndrzej Pietrasiewicz 
243cb382536SAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/
24400a2430fSAndrzej Pietrasiewicz /*                              Char Device                                */
24500a2430fSAndrzej Pietrasiewicz 
24600a2430fSAndrzej Pietrasiewicz static ssize_t f_hidg_read(struct file *file, char __user *buffer,
24700a2430fSAndrzej Pietrasiewicz 			size_t count, loff_t *ptr)
24800a2430fSAndrzej Pietrasiewicz {
24900a2430fSAndrzej Pietrasiewicz 	struct f_hidg *hidg = file->private_data;
25000a2430fSAndrzej Pietrasiewicz 	struct f_hidg_req_list *list;
25100a2430fSAndrzej Pietrasiewicz 	struct usb_request *req;
25200a2430fSAndrzej Pietrasiewicz 	unsigned long flags;
25300a2430fSAndrzej Pietrasiewicz 	int ret;
25400a2430fSAndrzej Pietrasiewicz 
25500a2430fSAndrzej Pietrasiewicz 	if (!count)
25600a2430fSAndrzej Pietrasiewicz 		return 0;
25700a2430fSAndrzej Pietrasiewicz 
25800a2430fSAndrzej Pietrasiewicz 	if (!access_ok(VERIFY_WRITE, buffer, count))
25900a2430fSAndrzej Pietrasiewicz 		return -EFAULT;
26000a2430fSAndrzej Pietrasiewicz 
261*33e4c1a9SKrzysztof Opasiak 	spin_lock_irqsave(&hidg->read_spinlock, flags);
26200a2430fSAndrzej Pietrasiewicz 
26300a2430fSAndrzej Pietrasiewicz #define READ_COND (!list_empty(&hidg->completed_out_req))
26400a2430fSAndrzej Pietrasiewicz 
26500a2430fSAndrzej Pietrasiewicz 	/* wait for at least one buffer to complete */
26600a2430fSAndrzej Pietrasiewicz 	while (!READ_COND) {
267*33e4c1a9SKrzysztof Opasiak 		spin_unlock_irqrestore(&hidg->read_spinlock, flags);
26800a2430fSAndrzej Pietrasiewicz 		if (file->f_flags & O_NONBLOCK)
26900a2430fSAndrzej Pietrasiewicz 			return -EAGAIN;
27000a2430fSAndrzej Pietrasiewicz 
27100a2430fSAndrzej Pietrasiewicz 		if (wait_event_interruptible(hidg->read_queue, READ_COND))
27200a2430fSAndrzej Pietrasiewicz 			return -ERESTARTSYS;
27300a2430fSAndrzej Pietrasiewicz 
274*33e4c1a9SKrzysztof Opasiak 		spin_lock_irqsave(&hidg->read_spinlock, flags);
27500a2430fSAndrzej Pietrasiewicz 	}
27600a2430fSAndrzej Pietrasiewicz 
27700a2430fSAndrzej Pietrasiewicz 	/* pick the first one */
27800a2430fSAndrzej Pietrasiewicz 	list = list_first_entry(&hidg->completed_out_req,
27900a2430fSAndrzej Pietrasiewicz 				struct f_hidg_req_list, list);
280aa65d11aSKrzysztof Opasiak 
281aa65d11aSKrzysztof Opasiak 	/*
282aa65d11aSKrzysztof Opasiak 	 * Remove this from list to protect it from beign free()
283aa65d11aSKrzysztof Opasiak 	 * while host disables our function
284aa65d11aSKrzysztof Opasiak 	 */
285aa65d11aSKrzysztof Opasiak 	list_del(&list->list);
286aa65d11aSKrzysztof Opasiak 
28700a2430fSAndrzej Pietrasiewicz 	req = list->req;
28800a2430fSAndrzej Pietrasiewicz 	count = min_t(unsigned int, count, req->actual - list->pos);
289*33e4c1a9SKrzysztof Opasiak 	spin_unlock_irqrestore(&hidg->read_spinlock, flags);
29000a2430fSAndrzej Pietrasiewicz 
29100a2430fSAndrzej Pietrasiewicz 	/* copy to user outside spinlock */
29200a2430fSAndrzej Pietrasiewicz 	count -= copy_to_user(buffer, req->buf + list->pos, count);
29300a2430fSAndrzej Pietrasiewicz 	list->pos += count;
29400a2430fSAndrzej Pietrasiewicz 
29500a2430fSAndrzej Pietrasiewicz 	/*
29600a2430fSAndrzej Pietrasiewicz 	 * if this request is completely handled and transfered to
29700a2430fSAndrzej Pietrasiewicz 	 * userspace, remove its entry from the list and requeue it
29800a2430fSAndrzej Pietrasiewicz 	 * again. Otherwise, we will revisit it again upon the next
29900a2430fSAndrzej Pietrasiewicz 	 * call, taking into account its current read position.
30000a2430fSAndrzej Pietrasiewicz 	 */
30100a2430fSAndrzej Pietrasiewicz 	if (list->pos == req->actual) {
30200a2430fSAndrzej Pietrasiewicz 		kfree(list);
30300a2430fSAndrzej Pietrasiewicz 
30400a2430fSAndrzej Pietrasiewicz 		req->length = hidg->report_length;
30500a2430fSAndrzej Pietrasiewicz 		ret = usb_ep_queue(hidg->out_ep, req, GFP_KERNEL);
306aa65d11aSKrzysztof Opasiak 		if (ret < 0) {
307aa65d11aSKrzysztof Opasiak 			free_ep_req(hidg->out_ep, req);
30800a2430fSAndrzej Pietrasiewicz 			return ret;
30900a2430fSAndrzej Pietrasiewicz 		}
310aa65d11aSKrzysztof Opasiak 	} else {
311*33e4c1a9SKrzysztof Opasiak 		spin_lock_irqsave(&hidg->read_spinlock, flags);
312aa65d11aSKrzysztof Opasiak 		list_add(&list->list, &hidg->completed_out_req);
313*33e4c1a9SKrzysztof Opasiak 		spin_unlock_irqrestore(&hidg->read_spinlock, flags);
314aa65d11aSKrzysztof Opasiak 
315aa65d11aSKrzysztof Opasiak 		wake_up(&hidg->read_queue);
316aa65d11aSKrzysztof Opasiak 	}
31700a2430fSAndrzej Pietrasiewicz 
31800a2430fSAndrzej Pietrasiewicz 	return count;
31900a2430fSAndrzej Pietrasiewicz }
32000a2430fSAndrzej Pietrasiewicz 
32100a2430fSAndrzej Pietrasiewicz static void f_hidg_req_complete(struct usb_ep *ep, struct usb_request *req)
32200a2430fSAndrzej Pietrasiewicz {
32300a2430fSAndrzej Pietrasiewicz 	struct f_hidg *hidg = (struct f_hidg *)ep->driver_data;
324*33e4c1a9SKrzysztof Opasiak 	unsigned long flags;
32500a2430fSAndrzej Pietrasiewicz 
32600a2430fSAndrzej Pietrasiewicz 	if (req->status != 0) {
32700a2430fSAndrzej Pietrasiewicz 		ERROR(hidg->func.config->cdev,
32800a2430fSAndrzej Pietrasiewicz 			"End Point Request ERROR: %d\n", req->status);
32900a2430fSAndrzej Pietrasiewicz 	}
33000a2430fSAndrzej Pietrasiewicz 
331*33e4c1a9SKrzysztof Opasiak 	spin_lock_irqsave(&hidg->write_spinlock, flags);
33200a2430fSAndrzej Pietrasiewicz 	hidg->write_pending = 0;
333*33e4c1a9SKrzysztof Opasiak 	spin_unlock_irqrestore(&hidg->write_spinlock, flags);
33400a2430fSAndrzej Pietrasiewicz 	wake_up(&hidg->write_queue);
33500a2430fSAndrzej Pietrasiewicz }
33600a2430fSAndrzej Pietrasiewicz 
33700a2430fSAndrzej Pietrasiewicz static ssize_t f_hidg_write(struct file *file, const char __user *buffer,
33800a2430fSAndrzej Pietrasiewicz 			    size_t count, loff_t *offp)
33900a2430fSAndrzej Pietrasiewicz {
34000a2430fSAndrzej Pietrasiewicz 	struct f_hidg *hidg  = file->private_data;
341*33e4c1a9SKrzysztof Opasiak 	unsigned long flags;
34200a2430fSAndrzej Pietrasiewicz 	ssize_t status = -ENOMEM;
34300a2430fSAndrzej Pietrasiewicz 
34400a2430fSAndrzej Pietrasiewicz 	if (!access_ok(VERIFY_READ, buffer, count))
34500a2430fSAndrzej Pietrasiewicz 		return -EFAULT;
34600a2430fSAndrzej Pietrasiewicz 
347*33e4c1a9SKrzysztof Opasiak 	spin_lock_irqsave(&hidg->write_spinlock, flags);
34800a2430fSAndrzej Pietrasiewicz 
34900a2430fSAndrzej Pietrasiewicz #define WRITE_COND (!hidg->write_pending)
35000a2430fSAndrzej Pietrasiewicz 
35100a2430fSAndrzej Pietrasiewicz 	/* write queue */
35200a2430fSAndrzej Pietrasiewicz 	while (!WRITE_COND) {
353*33e4c1a9SKrzysztof Opasiak 		spin_unlock_irqrestore(&hidg->write_spinlock, flags);
35400a2430fSAndrzej Pietrasiewicz 		if (file->f_flags & O_NONBLOCK)
35500a2430fSAndrzej Pietrasiewicz 			return -EAGAIN;
35600a2430fSAndrzej Pietrasiewicz 
35700a2430fSAndrzej Pietrasiewicz 		if (wait_event_interruptible_exclusive(
35800a2430fSAndrzej Pietrasiewicz 				hidg->write_queue, WRITE_COND))
35900a2430fSAndrzej Pietrasiewicz 			return -ERESTARTSYS;
36000a2430fSAndrzej Pietrasiewicz 
361*33e4c1a9SKrzysztof Opasiak 		spin_lock_irqsave(&hidg->write_spinlock, flags);
36200a2430fSAndrzej Pietrasiewicz 	}
36300a2430fSAndrzej Pietrasiewicz 
364*33e4c1a9SKrzysztof Opasiak 	hidg->write_pending = 1;
36500a2430fSAndrzej Pietrasiewicz 	count  = min_t(unsigned, count, hidg->report_length);
366*33e4c1a9SKrzysztof Opasiak 
367*33e4c1a9SKrzysztof Opasiak 	spin_unlock_irqrestore(&hidg->write_spinlock, flags);
36800a2430fSAndrzej Pietrasiewicz 	status = copy_from_user(hidg->req->buf, buffer, count);
36900a2430fSAndrzej Pietrasiewicz 
37000a2430fSAndrzej Pietrasiewicz 	if (status != 0) {
37100a2430fSAndrzej Pietrasiewicz 		ERROR(hidg->func.config->cdev,
37200a2430fSAndrzej Pietrasiewicz 			"copy_from_user error\n");
373*33e4c1a9SKrzysztof Opasiak 		status = -EINVAL;
374*33e4c1a9SKrzysztof Opasiak 		goto release_write_pending;
37500a2430fSAndrzej Pietrasiewicz 	}
37600a2430fSAndrzej Pietrasiewicz 
37700a2430fSAndrzej Pietrasiewicz 	hidg->req->status   = 0;
37800a2430fSAndrzej Pietrasiewicz 	hidg->req->zero     = 0;
37900a2430fSAndrzej Pietrasiewicz 	hidg->req->length   = count;
38000a2430fSAndrzej Pietrasiewicz 	hidg->req->complete = f_hidg_req_complete;
38100a2430fSAndrzej Pietrasiewicz 	hidg->req->context  = hidg;
38200a2430fSAndrzej Pietrasiewicz 
38300a2430fSAndrzej Pietrasiewicz 	status = usb_ep_queue(hidg->in_ep, hidg->req, GFP_ATOMIC);
38400a2430fSAndrzej Pietrasiewicz 	if (status < 0) {
38500a2430fSAndrzej Pietrasiewicz 		ERROR(hidg->func.config->cdev,
38600a2430fSAndrzej Pietrasiewicz 			"usb_ep_queue error on int endpoint %zd\n", status);
387*33e4c1a9SKrzysztof Opasiak 		goto release_write_pending;
38800a2430fSAndrzej Pietrasiewicz 	} else {
38900a2430fSAndrzej Pietrasiewicz 		status = count;
39000a2430fSAndrzej Pietrasiewicz 	}
39100a2430fSAndrzej Pietrasiewicz 
392*33e4c1a9SKrzysztof Opasiak 	return status;
393*33e4c1a9SKrzysztof Opasiak release_write_pending:
394*33e4c1a9SKrzysztof Opasiak 	spin_lock_irqsave(&hidg->write_spinlock, flags);
395*33e4c1a9SKrzysztof Opasiak 	hidg->write_pending = 0;
396*33e4c1a9SKrzysztof Opasiak 	spin_unlock_irqrestore(&hidg->write_spinlock, flags);
397*33e4c1a9SKrzysztof Opasiak 
398*33e4c1a9SKrzysztof Opasiak 	wake_up(&hidg->write_queue);
39900a2430fSAndrzej Pietrasiewicz 
40000a2430fSAndrzej Pietrasiewicz 	return status;
40100a2430fSAndrzej Pietrasiewicz }
40200a2430fSAndrzej Pietrasiewicz 
40300a2430fSAndrzej Pietrasiewicz static unsigned int f_hidg_poll(struct file *file, poll_table *wait)
40400a2430fSAndrzej Pietrasiewicz {
40500a2430fSAndrzej Pietrasiewicz 	struct f_hidg	*hidg  = file->private_data;
40600a2430fSAndrzej Pietrasiewicz 	unsigned int	ret = 0;
40700a2430fSAndrzej Pietrasiewicz 
40800a2430fSAndrzej Pietrasiewicz 	poll_wait(file, &hidg->read_queue, wait);
40900a2430fSAndrzej Pietrasiewicz 	poll_wait(file, &hidg->write_queue, wait);
41000a2430fSAndrzej Pietrasiewicz 
41100a2430fSAndrzej Pietrasiewicz 	if (WRITE_COND)
41200a2430fSAndrzej Pietrasiewicz 		ret |= POLLOUT | POLLWRNORM;
41300a2430fSAndrzej Pietrasiewicz 
41400a2430fSAndrzej Pietrasiewicz 	if (READ_COND)
41500a2430fSAndrzej Pietrasiewicz 		ret |= POLLIN | POLLRDNORM;
41600a2430fSAndrzej Pietrasiewicz 
41700a2430fSAndrzej Pietrasiewicz 	return ret;
41800a2430fSAndrzej Pietrasiewicz }
41900a2430fSAndrzej Pietrasiewicz 
42000a2430fSAndrzej Pietrasiewicz #undef WRITE_COND
42100a2430fSAndrzej Pietrasiewicz #undef READ_COND
42200a2430fSAndrzej Pietrasiewicz 
42300a2430fSAndrzej Pietrasiewicz static int f_hidg_release(struct inode *inode, struct file *fd)
42400a2430fSAndrzej Pietrasiewicz {
42500a2430fSAndrzej Pietrasiewicz 	fd->private_data = NULL;
42600a2430fSAndrzej Pietrasiewicz 	return 0;
42700a2430fSAndrzej Pietrasiewicz }
42800a2430fSAndrzej Pietrasiewicz 
42900a2430fSAndrzej Pietrasiewicz static int f_hidg_open(struct inode *inode, struct file *fd)
43000a2430fSAndrzej Pietrasiewicz {
43100a2430fSAndrzej Pietrasiewicz 	struct f_hidg *hidg =
43200a2430fSAndrzej Pietrasiewicz 		container_of(inode->i_cdev, struct f_hidg, cdev);
43300a2430fSAndrzej Pietrasiewicz 
43400a2430fSAndrzej Pietrasiewicz 	fd->private_data = hidg;
43500a2430fSAndrzej Pietrasiewicz 
43600a2430fSAndrzej Pietrasiewicz 	return 0;
43700a2430fSAndrzej Pietrasiewicz }
43800a2430fSAndrzej Pietrasiewicz 
43900a2430fSAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/
44000a2430fSAndrzej Pietrasiewicz /*                                usb_function                             */
44100a2430fSAndrzej Pietrasiewicz 
44200a2430fSAndrzej Pietrasiewicz static inline struct usb_request *hidg_alloc_ep_req(struct usb_ep *ep,
44300a2430fSAndrzej Pietrasiewicz 						    unsigned length)
44400a2430fSAndrzej Pietrasiewicz {
445aadbe812SFelipe F. Tonello 	return alloc_ep_req(ep, length);
44600a2430fSAndrzej Pietrasiewicz }
44700a2430fSAndrzej Pietrasiewicz 
44800a2430fSAndrzej Pietrasiewicz static void hidg_set_report_complete(struct usb_ep *ep, struct usb_request *req)
44900a2430fSAndrzej Pietrasiewicz {
45000a2430fSAndrzej Pietrasiewicz 	struct f_hidg *hidg = (struct f_hidg *) req->context;
45120d2ca95SKrzysztof Opasiak 	struct usb_composite_dev *cdev = hidg->func.config->cdev;
45200a2430fSAndrzej Pietrasiewicz 	struct f_hidg_req_list *req_list;
45300a2430fSAndrzej Pietrasiewicz 	unsigned long flags;
45400a2430fSAndrzej Pietrasiewicz 
45520d2ca95SKrzysztof Opasiak 	switch (req->status) {
45620d2ca95SKrzysztof Opasiak 	case 0:
45700a2430fSAndrzej Pietrasiewicz 		req_list = kzalloc(sizeof(*req_list), GFP_ATOMIC);
45820d2ca95SKrzysztof Opasiak 		if (!req_list) {
45920d2ca95SKrzysztof Opasiak 			ERROR(cdev, "Unable to allocate mem for req_list\n");
46020d2ca95SKrzysztof Opasiak 			goto free_req;
46120d2ca95SKrzysztof Opasiak 		}
46200a2430fSAndrzej Pietrasiewicz 
46300a2430fSAndrzej Pietrasiewicz 		req_list->req = req;
46400a2430fSAndrzej Pietrasiewicz 
465*33e4c1a9SKrzysztof Opasiak 		spin_lock_irqsave(&hidg->read_spinlock, flags);
46600a2430fSAndrzej Pietrasiewicz 		list_add_tail(&req_list->list, &hidg->completed_out_req);
467*33e4c1a9SKrzysztof Opasiak 		spin_unlock_irqrestore(&hidg->read_spinlock, flags);
46800a2430fSAndrzej Pietrasiewicz 
46900a2430fSAndrzej Pietrasiewicz 		wake_up(&hidg->read_queue);
47020d2ca95SKrzysztof Opasiak 		break;
47120d2ca95SKrzysztof Opasiak 	default:
47220d2ca95SKrzysztof Opasiak 		ERROR(cdev, "Set report failed %d\n", req->status);
47320d2ca95SKrzysztof Opasiak 		/* FALLTHROUGH */
47420d2ca95SKrzysztof Opasiak 	case -ECONNABORTED:		/* hardware forced ep reset */
47520d2ca95SKrzysztof Opasiak 	case -ECONNRESET:		/* request dequeued */
47620d2ca95SKrzysztof Opasiak 	case -ESHUTDOWN:		/* disconnect from host */
47720d2ca95SKrzysztof Opasiak free_req:
47820d2ca95SKrzysztof Opasiak 		free_ep_req(ep, req);
47920d2ca95SKrzysztof Opasiak 		return;
48020d2ca95SKrzysztof Opasiak 	}
48100a2430fSAndrzej Pietrasiewicz }
48200a2430fSAndrzej Pietrasiewicz 
48300a2430fSAndrzej Pietrasiewicz static int hidg_setup(struct usb_function *f,
48400a2430fSAndrzej Pietrasiewicz 		const struct usb_ctrlrequest *ctrl)
48500a2430fSAndrzej Pietrasiewicz {
48600a2430fSAndrzej Pietrasiewicz 	struct f_hidg			*hidg = func_to_hidg(f);
48700a2430fSAndrzej Pietrasiewicz 	struct usb_composite_dev	*cdev = f->config->cdev;
48800a2430fSAndrzej Pietrasiewicz 	struct usb_request		*req  = cdev->req;
48900a2430fSAndrzej Pietrasiewicz 	int status = 0;
49000a2430fSAndrzej Pietrasiewicz 	__u16 value, length;
49100a2430fSAndrzej Pietrasiewicz 
49200a2430fSAndrzej Pietrasiewicz 	value	= __le16_to_cpu(ctrl->wValue);
49300a2430fSAndrzej Pietrasiewicz 	length	= __le16_to_cpu(ctrl->wLength);
49400a2430fSAndrzej Pietrasiewicz 
495c9b3bde0SJulia Lawall 	VDBG(cdev,
496c9b3bde0SJulia Lawall 	     "%s crtl_request : bRequestType:0x%x bRequest:0x%x Value:0x%x\n",
497c9b3bde0SJulia Lawall 	     __func__, ctrl->bRequestType, ctrl->bRequest, value);
49800a2430fSAndrzej Pietrasiewicz 
49900a2430fSAndrzej Pietrasiewicz 	switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
50000a2430fSAndrzej Pietrasiewicz 	case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
50100a2430fSAndrzej Pietrasiewicz 		  | HID_REQ_GET_REPORT):
50200a2430fSAndrzej Pietrasiewicz 		VDBG(cdev, "get_report\n");
50300a2430fSAndrzej Pietrasiewicz 
50400a2430fSAndrzej Pietrasiewicz 		/* send an empty report */
50500a2430fSAndrzej Pietrasiewicz 		length = min_t(unsigned, length, hidg->report_length);
50600a2430fSAndrzej Pietrasiewicz 		memset(req->buf, 0x0, length);
50700a2430fSAndrzej Pietrasiewicz 
50800a2430fSAndrzej Pietrasiewicz 		goto respond;
50900a2430fSAndrzej Pietrasiewicz 		break;
51000a2430fSAndrzej Pietrasiewicz 
51100a2430fSAndrzej Pietrasiewicz 	case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
51200a2430fSAndrzej Pietrasiewicz 		  | HID_REQ_GET_PROTOCOL):
51300a2430fSAndrzej Pietrasiewicz 		VDBG(cdev, "get_protocol\n");
51400a2430fSAndrzej Pietrasiewicz 		goto stall;
51500a2430fSAndrzej Pietrasiewicz 		break;
51600a2430fSAndrzej Pietrasiewicz 
51700a2430fSAndrzej Pietrasiewicz 	case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
51800a2430fSAndrzej Pietrasiewicz 		  | HID_REQ_SET_REPORT):
5196774def6SMasanari Iida 		VDBG(cdev, "set_report | wLength=%d\n", ctrl->wLength);
52000a2430fSAndrzej Pietrasiewicz 		goto stall;
52100a2430fSAndrzej Pietrasiewicz 		break;
52200a2430fSAndrzej Pietrasiewicz 
52300a2430fSAndrzej Pietrasiewicz 	case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
52400a2430fSAndrzej Pietrasiewicz 		  | HID_REQ_SET_PROTOCOL):
52500a2430fSAndrzej Pietrasiewicz 		VDBG(cdev, "set_protocol\n");
52600a2430fSAndrzej Pietrasiewicz 		goto stall;
52700a2430fSAndrzej Pietrasiewicz 		break;
52800a2430fSAndrzej Pietrasiewicz 
52900a2430fSAndrzej Pietrasiewicz 	case ((USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_INTERFACE) << 8
53000a2430fSAndrzej Pietrasiewicz 		  | USB_REQ_GET_DESCRIPTOR):
53100a2430fSAndrzej Pietrasiewicz 		switch (value >> 8) {
53200a2430fSAndrzej Pietrasiewicz 		case HID_DT_HID:
533f286d487SKrzysztof Opasiak 		{
534f286d487SKrzysztof Opasiak 			struct hid_descriptor hidg_desc_copy = hidg_desc;
535f286d487SKrzysztof Opasiak 
53600a2430fSAndrzej Pietrasiewicz 			VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: HID\n");
537f286d487SKrzysztof Opasiak 			hidg_desc_copy.desc[0].bDescriptorType = HID_DT_REPORT;
538f286d487SKrzysztof Opasiak 			hidg_desc_copy.desc[0].wDescriptorLength =
539f286d487SKrzysztof Opasiak 				cpu_to_le16(hidg->report_desc_length);
540f286d487SKrzysztof Opasiak 
54100a2430fSAndrzej Pietrasiewicz 			length = min_t(unsigned short, length,
542f286d487SKrzysztof Opasiak 						   hidg_desc_copy.bLength);
543f286d487SKrzysztof Opasiak 			memcpy(req->buf, &hidg_desc_copy, length);
54400a2430fSAndrzej Pietrasiewicz 			goto respond;
54500a2430fSAndrzej Pietrasiewicz 			break;
546f286d487SKrzysztof Opasiak 		}
54700a2430fSAndrzej Pietrasiewicz 		case HID_DT_REPORT:
54800a2430fSAndrzej Pietrasiewicz 			VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: REPORT\n");
54900a2430fSAndrzej Pietrasiewicz 			length = min_t(unsigned short, length,
55000a2430fSAndrzej Pietrasiewicz 						   hidg->report_desc_length);
55100a2430fSAndrzej Pietrasiewicz 			memcpy(req->buf, hidg->report_desc, length);
55200a2430fSAndrzej Pietrasiewicz 			goto respond;
55300a2430fSAndrzej Pietrasiewicz 			break;
55400a2430fSAndrzej Pietrasiewicz 
55500a2430fSAndrzej Pietrasiewicz 		default:
55600a2430fSAndrzej Pietrasiewicz 			VDBG(cdev, "Unknown descriptor request 0x%x\n",
55700a2430fSAndrzej Pietrasiewicz 				 value >> 8);
55800a2430fSAndrzej Pietrasiewicz 			goto stall;
55900a2430fSAndrzej Pietrasiewicz 			break;
56000a2430fSAndrzej Pietrasiewicz 		}
56100a2430fSAndrzej Pietrasiewicz 		break;
56200a2430fSAndrzej Pietrasiewicz 
56300a2430fSAndrzej Pietrasiewicz 	default:
56400a2430fSAndrzej Pietrasiewicz 		VDBG(cdev, "Unknown request 0x%x\n",
56500a2430fSAndrzej Pietrasiewicz 			 ctrl->bRequest);
56600a2430fSAndrzej Pietrasiewicz 		goto stall;
56700a2430fSAndrzej Pietrasiewicz 		break;
56800a2430fSAndrzej Pietrasiewicz 	}
56900a2430fSAndrzej Pietrasiewicz 
57000a2430fSAndrzej Pietrasiewicz stall:
57100a2430fSAndrzej Pietrasiewicz 	return -EOPNOTSUPP;
57200a2430fSAndrzej Pietrasiewicz 
57300a2430fSAndrzej Pietrasiewicz respond:
57400a2430fSAndrzej Pietrasiewicz 	req->zero = 0;
57500a2430fSAndrzej Pietrasiewicz 	req->length = length;
57600a2430fSAndrzej Pietrasiewicz 	status = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
57700a2430fSAndrzej Pietrasiewicz 	if (status < 0)
57800a2430fSAndrzej Pietrasiewicz 		ERROR(cdev, "usb_ep_queue error on ep0 %d\n", value);
57900a2430fSAndrzej Pietrasiewicz 	return status;
58000a2430fSAndrzej Pietrasiewicz }
58100a2430fSAndrzej Pietrasiewicz 
58200a2430fSAndrzej Pietrasiewicz static void hidg_disable(struct usb_function *f)
58300a2430fSAndrzej Pietrasiewicz {
58400a2430fSAndrzej Pietrasiewicz 	struct f_hidg *hidg = func_to_hidg(f);
58500a2430fSAndrzej Pietrasiewicz 	struct f_hidg_req_list *list, *next;
586aa65d11aSKrzysztof Opasiak 	unsigned long flags;
58700a2430fSAndrzej Pietrasiewicz 
58800a2430fSAndrzej Pietrasiewicz 	usb_ep_disable(hidg->in_ep);
58900a2430fSAndrzej Pietrasiewicz 	usb_ep_disable(hidg->out_ep);
59000a2430fSAndrzej Pietrasiewicz 
591*33e4c1a9SKrzysztof Opasiak 	spin_lock_irqsave(&hidg->read_spinlock, flags);
59200a2430fSAndrzej Pietrasiewicz 	list_for_each_entry_safe(list, next, &hidg->completed_out_req, list) {
593aa65d11aSKrzysztof Opasiak 		free_ep_req(hidg->out_ep, list->req);
59400a2430fSAndrzej Pietrasiewicz 		list_del(&list->list);
59500a2430fSAndrzej Pietrasiewicz 		kfree(list);
59600a2430fSAndrzej Pietrasiewicz 	}
597*33e4c1a9SKrzysztof Opasiak 	spin_unlock_irqrestore(&hidg->read_spinlock, flags);
59800a2430fSAndrzej Pietrasiewicz }
59900a2430fSAndrzej Pietrasiewicz 
60000a2430fSAndrzej Pietrasiewicz static int hidg_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
60100a2430fSAndrzej Pietrasiewicz {
60200a2430fSAndrzej Pietrasiewicz 	struct usb_composite_dev		*cdev = f->config->cdev;
60300a2430fSAndrzej Pietrasiewicz 	struct f_hidg				*hidg = func_to_hidg(f);
60400a2430fSAndrzej Pietrasiewicz 	int i, status = 0;
60500a2430fSAndrzej Pietrasiewicz 
60600a2430fSAndrzej Pietrasiewicz 	VDBG(cdev, "hidg_set_alt intf:%d alt:%d\n", intf, alt);
60700a2430fSAndrzej Pietrasiewicz 
60800a2430fSAndrzej Pietrasiewicz 	if (hidg->in_ep != NULL) {
60900a2430fSAndrzej Pietrasiewicz 		/* restart endpoint */
61000a2430fSAndrzej Pietrasiewicz 		usb_ep_disable(hidg->in_ep);
61100a2430fSAndrzej Pietrasiewicz 
61200a2430fSAndrzej Pietrasiewicz 		status = config_ep_by_speed(f->config->cdev->gadget, f,
61300a2430fSAndrzej Pietrasiewicz 					    hidg->in_ep);
61400a2430fSAndrzej Pietrasiewicz 		if (status) {
61500a2430fSAndrzej Pietrasiewicz 			ERROR(cdev, "config_ep_by_speed FAILED!\n");
61600a2430fSAndrzej Pietrasiewicz 			goto fail;
61700a2430fSAndrzej Pietrasiewicz 		}
61800a2430fSAndrzej Pietrasiewicz 		status = usb_ep_enable(hidg->in_ep);
61900a2430fSAndrzej Pietrasiewicz 		if (status < 0) {
62000a2430fSAndrzej Pietrasiewicz 			ERROR(cdev, "Enable IN endpoint FAILED!\n");
62100a2430fSAndrzej Pietrasiewicz 			goto fail;
62200a2430fSAndrzej Pietrasiewicz 		}
62300a2430fSAndrzej Pietrasiewicz 		hidg->in_ep->driver_data = hidg;
62400a2430fSAndrzej Pietrasiewicz 	}
62500a2430fSAndrzej Pietrasiewicz 
62600a2430fSAndrzej Pietrasiewicz 
62700a2430fSAndrzej Pietrasiewicz 	if (hidg->out_ep != NULL) {
62800a2430fSAndrzej Pietrasiewicz 		/* restart endpoint */
62900a2430fSAndrzej Pietrasiewicz 		usb_ep_disable(hidg->out_ep);
63000a2430fSAndrzej Pietrasiewicz 
63100a2430fSAndrzej Pietrasiewicz 		status = config_ep_by_speed(f->config->cdev->gadget, f,
63200a2430fSAndrzej Pietrasiewicz 					    hidg->out_ep);
63300a2430fSAndrzej Pietrasiewicz 		if (status) {
63400a2430fSAndrzej Pietrasiewicz 			ERROR(cdev, "config_ep_by_speed FAILED!\n");
63500a2430fSAndrzej Pietrasiewicz 			goto fail;
63600a2430fSAndrzej Pietrasiewicz 		}
63700a2430fSAndrzej Pietrasiewicz 		status = usb_ep_enable(hidg->out_ep);
63800a2430fSAndrzej Pietrasiewicz 		if (status < 0) {
63943aef5c2SDavid Lechner 			ERROR(cdev, "Enable OUT endpoint FAILED!\n");
64000a2430fSAndrzej Pietrasiewicz 			goto fail;
64100a2430fSAndrzej Pietrasiewicz 		}
64200a2430fSAndrzej Pietrasiewicz 		hidg->out_ep->driver_data = hidg;
64300a2430fSAndrzej Pietrasiewicz 
64400a2430fSAndrzej Pietrasiewicz 		/*
64500a2430fSAndrzej Pietrasiewicz 		 * allocate a bunch of read buffers and queue them all at once.
64600a2430fSAndrzej Pietrasiewicz 		 */
64700a2430fSAndrzej Pietrasiewicz 		for (i = 0; i < hidg->qlen && status == 0; i++) {
64800a2430fSAndrzej Pietrasiewicz 			struct usb_request *req =
64900a2430fSAndrzej Pietrasiewicz 					hidg_alloc_ep_req(hidg->out_ep,
65000a2430fSAndrzej Pietrasiewicz 							  hidg->report_length);
65100a2430fSAndrzej Pietrasiewicz 			if (req) {
65200a2430fSAndrzej Pietrasiewicz 				req->complete = hidg_set_report_complete;
65300a2430fSAndrzej Pietrasiewicz 				req->context  = hidg;
65400a2430fSAndrzej Pietrasiewicz 				status = usb_ep_queue(hidg->out_ep, req,
65500a2430fSAndrzej Pietrasiewicz 						      GFP_ATOMIC);
65600a2430fSAndrzej Pietrasiewicz 				if (status)
65700a2430fSAndrzej Pietrasiewicz 					ERROR(cdev, "%s queue req --> %d\n",
65800a2430fSAndrzej Pietrasiewicz 						hidg->out_ep->name, status);
65900a2430fSAndrzej Pietrasiewicz 			} else {
66000a2430fSAndrzej Pietrasiewicz 				usb_ep_disable(hidg->out_ep);
66100a2430fSAndrzej Pietrasiewicz 				status = -ENOMEM;
66200a2430fSAndrzej Pietrasiewicz 				goto fail;
66300a2430fSAndrzej Pietrasiewicz 			}
66400a2430fSAndrzej Pietrasiewicz 		}
66500a2430fSAndrzej Pietrasiewicz 	}
66600a2430fSAndrzej Pietrasiewicz 
66700a2430fSAndrzej Pietrasiewicz fail:
66800a2430fSAndrzej Pietrasiewicz 	return status;
66900a2430fSAndrzej Pietrasiewicz }
67000a2430fSAndrzej Pietrasiewicz 
6717a3cc461SLad, Prabhakar static const struct file_operations f_hidg_fops = {
67200a2430fSAndrzej Pietrasiewicz 	.owner		= THIS_MODULE,
67300a2430fSAndrzej Pietrasiewicz 	.open		= f_hidg_open,
67400a2430fSAndrzej Pietrasiewicz 	.release	= f_hidg_release,
67500a2430fSAndrzej Pietrasiewicz 	.write		= f_hidg_write,
67600a2430fSAndrzej Pietrasiewicz 	.read		= f_hidg_read,
67700a2430fSAndrzej Pietrasiewicz 	.poll		= f_hidg_poll,
67800a2430fSAndrzej Pietrasiewicz 	.llseek		= noop_llseek,
67900a2430fSAndrzej Pietrasiewicz };
68000a2430fSAndrzej Pietrasiewicz 
681cb382536SAndrzej Pietrasiewicz static int hidg_bind(struct usb_configuration *c, struct usb_function *f)
68200a2430fSAndrzej Pietrasiewicz {
68300a2430fSAndrzej Pietrasiewicz 	struct usb_ep		*ep;
68400a2430fSAndrzej Pietrasiewicz 	struct f_hidg		*hidg = func_to_hidg(f);
6855ca8d3ecSAndrzej Pietrasiewicz 	struct usb_string	*us;
68663406087SAndrzej Pietrasiewicz 	struct device		*device;
68700a2430fSAndrzej Pietrasiewicz 	int			status;
68800a2430fSAndrzej Pietrasiewicz 	dev_t			dev;
68900a2430fSAndrzej Pietrasiewicz 
690cb382536SAndrzej Pietrasiewicz 	/* maybe allocate device-global string IDs, and patch descriptors */
6915ca8d3ecSAndrzej Pietrasiewicz 	us = usb_gstrings_attach(c->cdev, ct_func_strings,
6925ca8d3ecSAndrzej Pietrasiewicz 				 ARRAY_SIZE(ct_func_string_defs));
6935ca8d3ecSAndrzej Pietrasiewicz 	if (IS_ERR(us))
6945ca8d3ecSAndrzej Pietrasiewicz 		return PTR_ERR(us);
6955ca8d3ecSAndrzej Pietrasiewicz 	hidg_interface_desc.iInterface = us[CT_FUNC_HID_IDX].id;
696cb382536SAndrzej Pietrasiewicz 
69700a2430fSAndrzej Pietrasiewicz 	/* allocate instance-specific interface IDs, and patch descriptors */
69800a2430fSAndrzej Pietrasiewicz 	status = usb_interface_id(c, f);
69900a2430fSAndrzej Pietrasiewicz 	if (status < 0)
70000a2430fSAndrzej Pietrasiewicz 		goto fail;
70100a2430fSAndrzej Pietrasiewicz 	hidg_interface_desc.bInterfaceNumber = status;
70200a2430fSAndrzej Pietrasiewicz 
70300a2430fSAndrzej Pietrasiewicz 	/* allocate instance-specific endpoints */
70400a2430fSAndrzej Pietrasiewicz 	status = -ENODEV;
70500a2430fSAndrzej Pietrasiewicz 	ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_in_ep_desc);
70600a2430fSAndrzej Pietrasiewicz 	if (!ep)
70700a2430fSAndrzej Pietrasiewicz 		goto fail;
70800a2430fSAndrzej Pietrasiewicz 	hidg->in_ep = ep;
70900a2430fSAndrzej Pietrasiewicz 
71000a2430fSAndrzej Pietrasiewicz 	ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_out_ep_desc);
71100a2430fSAndrzej Pietrasiewicz 	if (!ep)
71200a2430fSAndrzej Pietrasiewicz 		goto fail;
71300a2430fSAndrzej Pietrasiewicz 	hidg->out_ep = ep;
71400a2430fSAndrzej Pietrasiewicz 
71500a2430fSAndrzej Pietrasiewicz 	/* preallocate request and buffer */
71600a2430fSAndrzej Pietrasiewicz 	status = -ENOMEM;
717ba1582f2SFelipe F. Tonello 	hidg->req = alloc_ep_req(hidg->in_ep, hidg->report_length);
71800a2430fSAndrzej Pietrasiewicz 	if (!hidg->req)
71900a2430fSAndrzej Pietrasiewicz 		goto fail;
72000a2430fSAndrzej Pietrasiewicz 
72100a2430fSAndrzej Pietrasiewicz 	/* set descriptor dynamic values */
72200a2430fSAndrzej Pietrasiewicz 	hidg_interface_desc.bInterfaceSubClass = hidg->bInterfaceSubClass;
72300a2430fSAndrzej Pietrasiewicz 	hidg_interface_desc.bInterfaceProtocol = hidg->bInterfaceProtocol;
724dbf499cfSJanusz Dziedzic 	hidg_ss_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
725dbf499cfSJanusz Dziedzic 	hidg_ss_in_comp_desc.wBytesPerInterval =
726dbf499cfSJanusz Dziedzic 				cpu_to_le16(hidg->report_length);
72700a2430fSAndrzej Pietrasiewicz 	hidg_hs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
72800a2430fSAndrzej Pietrasiewicz 	hidg_fs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
729dbf499cfSJanusz Dziedzic 	hidg_ss_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
730dbf499cfSJanusz Dziedzic 	hidg_ss_out_comp_desc.wBytesPerInterval =
731dbf499cfSJanusz Dziedzic 				cpu_to_le16(hidg->report_length);
73200a2430fSAndrzej Pietrasiewicz 	hidg_hs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
73300a2430fSAndrzej Pietrasiewicz 	hidg_fs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
734f286d487SKrzysztof Opasiak 	/*
735f286d487SKrzysztof Opasiak 	 * We can use hidg_desc struct here but we should not relay
736f286d487SKrzysztof Opasiak 	 * that its content won't change after returning from this function.
737f286d487SKrzysztof Opasiak 	 */
73800a2430fSAndrzej Pietrasiewicz 	hidg_desc.desc[0].bDescriptorType = HID_DT_REPORT;
73900a2430fSAndrzej Pietrasiewicz 	hidg_desc.desc[0].wDescriptorLength =
74000a2430fSAndrzej Pietrasiewicz 		cpu_to_le16(hidg->report_desc_length);
74100a2430fSAndrzej Pietrasiewicz 
74200a2430fSAndrzej Pietrasiewicz 	hidg_hs_in_ep_desc.bEndpointAddress =
74300a2430fSAndrzej Pietrasiewicz 		hidg_fs_in_ep_desc.bEndpointAddress;
74400a2430fSAndrzej Pietrasiewicz 	hidg_hs_out_ep_desc.bEndpointAddress =
74500a2430fSAndrzej Pietrasiewicz 		hidg_fs_out_ep_desc.bEndpointAddress;
74600a2430fSAndrzej Pietrasiewicz 
747dbf499cfSJanusz Dziedzic 	hidg_ss_in_ep_desc.bEndpointAddress =
748dbf499cfSJanusz Dziedzic 		hidg_fs_in_ep_desc.bEndpointAddress;
749dbf499cfSJanusz Dziedzic 	hidg_ss_out_ep_desc.bEndpointAddress =
750dbf499cfSJanusz Dziedzic 		hidg_fs_out_ep_desc.bEndpointAddress;
751dbf499cfSJanusz Dziedzic 
75200a2430fSAndrzej Pietrasiewicz 	status = usb_assign_descriptors(f, hidg_fs_descriptors,
753dbf499cfSJanusz Dziedzic 			hidg_hs_descriptors, hidg_ss_descriptors, NULL);
75400a2430fSAndrzej Pietrasiewicz 	if (status)
75500a2430fSAndrzej Pietrasiewicz 		goto fail;
75600a2430fSAndrzej Pietrasiewicz 
757*33e4c1a9SKrzysztof Opasiak 	spin_lock_init(&hidg->write_spinlock);
758*33e4c1a9SKrzysztof Opasiak 	spin_lock_init(&hidg->read_spinlock);
75900a2430fSAndrzej Pietrasiewicz 	init_waitqueue_head(&hidg->write_queue);
76000a2430fSAndrzej Pietrasiewicz 	init_waitqueue_head(&hidg->read_queue);
76100a2430fSAndrzej Pietrasiewicz 	INIT_LIST_HEAD(&hidg->completed_out_req);
76200a2430fSAndrzej Pietrasiewicz 
76300a2430fSAndrzej Pietrasiewicz 	/* create char device */
76400a2430fSAndrzej Pietrasiewicz 	cdev_init(&hidg->cdev, &f_hidg_fops);
76500a2430fSAndrzej Pietrasiewicz 	dev = MKDEV(major, hidg->minor);
76600a2430fSAndrzej Pietrasiewicz 	status = cdev_add(&hidg->cdev, dev, 1);
76700a2430fSAndrzej Pietrasiewicz 	if (status)
768d12a8727SPavitrakumar Managutte 		goto fail_free_descs;
76900a2430fSAndrzej Pietrasiewicz 
77063406087SAndrzej Pietrasiewicz 	device = device_create(hidg_class, NULL, dev, NULL,
77163406087SAndrzej Pietrasiewicz 			       "%s%d", "hidg", hidg->minor);
77263406087SAndrzej Pietrasiewicz 	if (IS_ERR(device)) {
77363406087SAndrzej Pietrasiewicz 		status = PTR_ERR(device);
77463406087SAndrzej Pietrasiewicz 		goto del;
77563406087SAndrzej Pietrasiewicz 	}
77600a2430fSAndrzej Pietrasiewicz 
77700a2430fSAndrzej Pietrasiewicz 	return 0;
77863406087SAndrzej Pietrasiewicz del:
77963406087SAndrzej Pietrasiewicz 	cdev_del(&hidg->cdev);
780d12a8727SPavitrakumar Managutte fail_free_descs:
781d12a8727SPavitrakumar Managutte 	usb_free_all_descriptors(f);
78200a2430fSAndrzej Pietrasiewicz fail:
78300a2430fSAndrzej Pietrasiewicz 	ERROR(f->config->cdev, "hidg_bind FAILED\n");
78414794d71SFelipe F. Tonello 	if (hidg->req != NULL)
78514794d71SFelipe F. Tonello 		free_ep_req(hidg->in_ep, hidg->req);
78600a2430fSAndrzej Pietrasiewicz 
78700a2430fSAndrzej Pietrasiewicz 	return status;
78800a2430fSAndrzej Pietrasiewicz }
78900a2430fSAndrzej Pietrasiewicz 
790cb382536SAndrzej Pietrasiewicz static inline int hidg_get_minor(void)
791cb382536SAndrzej Pietrasiewicz {
792cb382536SAndrzej Pietrasiewicz 	int ret;
793cb382536SAndrzej Pietrasiewicz 
794cb382536SAndrzej Pietrasiewicz 	ret = ida_simple_get(&hidg_ida, 0, 0, GFP_KERNEL);
795774cf72fSAndrzej Pietrasiewicz 	if (ret >= HIDG_MINORS) {
796774cf72fSAndrzej Pietrasiewicz 		ida_simple_remove(&hidg_ida, ret);
797774cf72fSAndrzej Pietrasiewicz 		ret = -ENODEV;
798774cf72fSAndrzej Pietrasiewicz 	}
799cb382536SAndrzej Pietrasiewicz 
800cb382536SAndrzej Pietrasiewicz 	return ret;
801cb382536SAndrzej Pietrasiewicz }
802cb382536SAndrzej Pietrasiewicz 
80321a9476aSAndrzej Pietrasiewicz static inline struct f_hid_opts *to_f_hid_opts(struct config_item *item)
80421a9476aSAndrzej Pietrasiewicz {
80521a9476aSAndrzej Pietrasiewicz 	return container_of(to_config_group(item), struct f_hid_opts,
80621a9476aSAndrzej Pietrasiewicz 			    func_inst.group);
80721a9476aSAndrzej Pietrasiewicz }
80821a9476aSAndrzej Pietrasiewicz 
80921a9476aSAndrzej Pietrasiewicz static void hid_attr_release(struct config_item *item)
81021a9476aSAndrzej Pietrasiewicz {
81121a9476aSAndrzej Pietrasiewicz 	struct f_hid_opts *opts = to_f_hid_opts(item);
81221a9476aSAndrzej Pietrasiewicz 
81321a9476aSAndrzej Pietrasiewicz 	usb_put_function_instance(&opts->func_inst);
81421a9476aSAndrzej Pietrasiewicz }
81521a9476aSAndrzej Pietrasiewicz 
81621a9476aSAndrzej Pietrasiewicz static struct configfs_item_operations hidg_item_ops = {
81721a9476aSAndrzej Pietrasiewicz 	.release	= hid_attr_release,
81821a9476aSAndrzej Pietrasiewicz };
81921a9476aSAndrzej Pietrasiewicz 
82021a9476aSAndrzej Pietrasiewicz #define F_HID_OPT(name, prec, limit)					\
821da4e527cSChristoph Hellwig static ssize_t f_hid_opts_##name##_show(struct config_item *item, char *page)\
82221a9476aSAndrzej Pietrasiewicz {									\
823da4e527cSChristoph Hellwig 	struct f_hid_opts *opts = to_f_hid_opts(item);			\
82421a9476aSAndrzej Pietrasiewicz 	int result;							\
82521a9476aSAndrzej Pietrasiewicz 									\
82621a9476aSAndrzej Pietrasiewicz 	mutex_lock(&opts->lock);					\
82721a9476aSAndrzej Pietrasiewicz 	result = sprintf(page, "%d\n", opts->name);			\
82821a9476aSAndrzej Pietrasiewicz 	mutex_unlock(&opts->lock);					\
82921a9476aSAndrzej Pietrasiewicz 									\
83021a9476aSAndrzej Pietrasiewicz 	return result;							\
83121a9476aSAndrzej Pietrasiewicz }									\
83221a9476aSAndrzej Pietrasiewicz 									\
833da4e527cSChristoph Hellwig static ssize_t f_hid_opts_##name##_store(struct config_item *item,	\
83421a9476aSAndrzej Pietrasiewicz 					 const char *page, size_t len)	\
83521a9476aSAndrzej Pietrasiewicz {									\
836da4e527cSChristoph Hellwig 	struct f_hid_opts *opts = to_f_hid_opts(item);			\
83721a9476aSAndrzej Pietrasiewicz 	int ret;							\
83821a9476aSAndrzej Pietrasiewicz 	u##prec num;							\
83921a9476aSAndrzej Pietrasiewicz 									\
84021a9476aSAndrzej Pietrasiewicz 	mutex_lock(&opts->lock);					\
84121a9476aSAndrzej Pietrasiewicz 	if (opts->refcnt) {						\
84221a9476aSAndrzej Pietrasiewicz 		ret = -EBUSY;						\
84321a9476aSAndrzej Pietrasiewicz 		goto end;						\
84421a9476aSAndrzej Pietrasiewicz 	}								\
84521a9476aSAndrzej Pietrasiewicz 									\
84621a9476aSAndrzej Pietrasiewicz 	ret = kstrtou##prec(page, 0, &num);				\
84721a9476aSAndrzej Pietrasiewicz 	if (ret)							\
84821a9476aSAndrzej Pietrasiewicz 		goto end;						\
84921a9476aSAndrzej Pietrasiewicz 									\
85021a9476aSAndrzej Pietrasiewicz 	if (num > limit) {						\
85121a9476aSAndrzej Pietrasiewicz 		ret = -EINVAL;						\
85221a9476aSAndrzej Pietrasiewicz 		goto end;						\
85321a9476aSAndrzej Pietrasiewicz 	}								\
85421a9476aSAndrzej Pietrasiewicz 	opts->name = num;						\
85521a9476aSAndrzej Pietrasiewicz 	ret = len;							\
85621a9476aSAndrzej Pietrasiewicz 									\
85721a9476aSAndrzej Pietrasiewicz end:									\
85821a9476aSAndrzej Pietrasiewicz 	mutex_unlock(&opts->lock);					\
85921a9476aSAndrzej Pietrasiewicz 	return ret;							\
86021a9476aSAndrzej Pietrasiewicz }									\
86121a9476aSAndrzej Pietrasiewicz 									\
862da4e527cSChristoph Hellwig CONFIGFS_ATTR(f_hid_opts_, name)
86321a9476aSAndrzej Pietrasiewicz 
86421a9476aSAndrzej Pietrasiewicz F_HID_OPT(subclass, 8, 255);
86521a9476aSAndrzej Pietrasiewicz F_HID_OPT(protocol, 8, 255);
86639a2ac27SAndrzej Pietrasiewicz F_HID_OPT(report_length, 16, 65535);
86721a9476aSAndrzej Pietrasiewicz 
868da4e527cSChristoph Hellwig static ssize_t f_hid_opts_report_desc_show(struct config_item *item, char *page)
86921a9476aSAndrzej Pietrasiewicz {
870da4e527cSChristoph Hellwig 	struct f_hid_opts *opts = to_f_hid_opts(item);
87121a9476aSAndrzej Pietrasiewicz 	int result;
87221a9476aSAndrzej Pietrasiewicz 
87321a9476aSAndrzej Pietrasiewicz 	mutex_lock(&opts->lock);
87421a9476aSAndrzej Pietrasiewicz 	result = opts->report_desc_length;
87521a9476aSAndrzej Pietrasiewicz 	memcpy(page, opts->report_desc, opts->report_desc_length);
87621a9476aSAndrzej Pietrasiewicz 	mutex_unlock(&opts->lock);
87721a9476aSAndrzej Pietrasiewicz 
87821a9476aSAndrzej Pietrasiewicz 	return result;
87921a9476aSAndrzej Pietrasiewicz }
88021a9476aSAndrzej Pietrasiewicz 
881da4e527cSChristoph Hellwig static ssize_t f_hid_opts_report_desc_store(struct config_item *item,
88221a9476aSAndrzej Pietrasiewicz 					    const char *page, size_t len)
88321a9476aSAndrzej Pietrasiewicz {
884da4e527cSChristoph Hellwig 	struct f_hid_opts *opts = to_f_hid_opts(item);
88521a9476aSAndrzej Pietrasiewicz 	int ret = -EBUSY;
88621a9476aSAndrzej Pietrasiewicz 	char *d;
88721a9476aSAndrzej Pietrasiewicz 
88821a9476aSAndrzej Pietrasiewicz 	mutex_lock(&opts->lock);
88921a9476aSAndrzej Pietrasiewicz 
89021a9476aSAndrzej Pietrasiewicz 	if (opts->refcnt)
89121a9476aSAndrzej Pietrasiewicz 		goto end;
89221a9476aSAndrzej Pietrasiewicz 	if (len > PAGE_SIZE) {
89321a9476aSAndrzej Pietrasiewicz 		ret = -ENOSPC;
89421a9476aSAndrzej Pietrasiewicz 		goto end;
89521a9476aSAndrzej Pietrasiewicz 	}
89621a9476aSAndrzej Pietrasiewicz 	d = kmemdup(page, len, GFP_KERNEL);
89721a9476aSAndrzej Pietrasiewicz 	if (!d) {
89821a9476aSAndrzej Pietrasiewicz 		ret = -ENOMEM;
89921a9476aSAndrzej Pietrasiewicz 		goto end;
90021a9476aSAndrzej Pietrasiewicz 	}
90121a9476aSAndrzej Pietrasiewicz 	kfree(opts->report_desc);
90221a9476aSAndrzej Pietrasiewicz 	opts->report_desc = d;
90321a9476aSAndrzej Pietrasiewicz 	opts->report_desc_length = len;
90421a9476aSAndrzej Pietrasiewicz 	opts->report_desc_alloc = true;
90521a9476aSAndrzej Pietrasiewicz 	ret = len;
90621a9476aSAndrzej Pietrasiewicz end:
90721a9476aSAndrzej Pietrasiewicz 	mutex_unlock(&opts->lock);
90821a9476aSAndrzej Pietrasiewicz 	return ret;
90921a9476aSAndrzej Pietrasiewicz }
91021a9476aSAndrzej Pietrasiewicz 
911da4e527cSChristoph Hellwig CONFIGFS_ATTR(f_hid_opts_, report_desc);
91221a9476aSAndrzej Pietrasiewicz 
913ed6fe1f5SJohannes Berg static ssize_t f_hid_opts_dev_show(struct config_item *item, char *page)
914ed6fe1f5SJohannes Berg {
915ed6fe1f5SJohannes Berg 	struct f_hid_opts *opts = to_f_hid_opts(item);
916ed6fe1f5SJohannes Berg 
917ed6fe1f5SJohannes Berg 	return sprintf(page, "%d:%d\n", major, opts->minor);
918ed6fe1f5SJohannes Berg }
919ed6fe1f5SJohannes Berg 
920ed6fe1f5SJohannes Berg CONFIGFS_ATTR_RO(f_hid_opts_, dev);
921ed6fe1f5SJohannes Berg 
92221a9476aSAndrzej Pietrasiewicz static struct configfs_attribute *hid_attrs[] = {
923da4e527cSChristoph Hellwig 	&f_hid_opts_attr_subclass,
924da4e527cSChristoph Hellwig 	&f_hid_opts_attr_protocol,
925da4e527cSChristoph Hellwig 	&f_hid_opts_attr_report_length,
926da4e527cSChristoph Hellwig 	&f_hid_opts_attr_report_desc,
927ed6fe1f5SJohannes Berg 	&f_hid_opts_attr_dev,
92821a9476aSAndrzej Pietrasiewicz 	NULL,
92921a9476aSAndrzej Pietrasiewicz };
93021a9476aSAndrzej Pietrasiewicz 
93121a9476aSAndrzej Pietrasiewicz static struct config_item_type hid_func_type = {
93221a9476aSAndrzej Pietrasiewicz 	.ct_item_ops	= &hidg_item_ops,
93321a9476aSAndrzej Pietrasiewicz 	.ct_attrs	= hid_attrs,
93421a9476aSAndrzej Pietrasiewicz 	.ct_owner	= THIS_MODULE,
93521a9476aSAndrzej Pietrasiewicz };
93621a9476aSAndrzej Pietrasiewicz 
937cb382536SAndrzej Pietrasiewicz static inline void hidg_put_minor(int minor)
938cb382536SAndrzej Pietrasiewicz {
939cb382536SAndrzej Pietrasiewicz 	ida_simple_remove(&hidg_ida, minor);
940cb382536SAndrzej Pietrasiewicz }
941cb382536SAndrzej Pietrasiewicz 
942cb382536SAndrzej Pietrasiewicz static void hidg_free_inst(struct usb_function_instance *f)
943cb382536SAndrzej Pietrasiewicz {
944cb382536SAndrzej Pietrasiewicz 	struct f_hid_opts *opts;
945cb382536SAndrzej Pietrasiewicz 
946cb382536SAndrzej Pietrasiewicz 	opts = container_of(f, struct f_hid_opts, func_inst);
947cb382536SAndrzej Pietrasiewicz 
948cb382536SAndrzej Pietrasiewicz 	mutex_lock(&hidg_ida_lock);
949cb382536SAndrzej Pietrasiewicz 
950cb382536SAndrzej Pietrasiewicz 	hidg_put_minor(opts->minor);
95199c49407SMatthew Wilcox 	if (ida_is_empty(&hidg_ida))
952cb382536SAndrzej Pietrasiewicz 		ghid_cleanup();
953cb382536SAndrzej Pietrasiewicz 
954cb382536SAndrzej Pietrasiewicz 	mutex_unlock(&hidg_ida_lock);
955cb382536SAndrzej Pietrasiewicz 
956cb382536SAndrzej Pietrasiewicz 	if (opts->report_desc_alloc)
957cb382536SAndrzej Pietrasiewicz 		kfree(opts->report_desc);
958cb382536SAndrzej Pietrasiewicz 
959cb382536SAndrzej Pietrasiewicz 	kfree(opts);
960cb382536SAndrzej Pietrasiewicz }
961cb382536SAndrzej Pietrasiewicz 
962cb382536SAndrzej Pietrasiewicz static struct usb_function_instance *hidg_alloc_inst(void)
963cb382536SAndrzej Pietrasiewicz {
964cb382536SAndrzej Pietrasiewicz 	struct f_hid_opts *opts;
965cb382536SAndrzej Pietrasiewicz 	struct usb_function_instance *ret;
966cb382536SAndrzej Pietrasiewicz 	int status = 0;
967cb382536SAndrzej Pietrasiewicz 
968cb382536SAndrzej Pietrasiewicz 	opts = kzalloc(sizeof(*opts), GFP_KERNEL);
969cb382536SAndrzej Pietrasiewicz 	if (!opts)
970cb382536SAndrzej Pietrasiewicz 		return ERR_PTR(-ENOMEM);
97121a9476aSAndrzej Pietrasiewicz 	mutex_init(&opts->lock);
972cb382536SAndrzej Pietrasiewicz 	opts->func_inst.free_func_inst = hidg_free_inst;
973cb382536SAndrzej Pietrasiewicz 	ret = &opts->func_inst;
974cb382536SAndrzej Pietrasiewicz 
975cb382536SAndrzej Pietrasiewicz 	mutex_lock(&hidg_ida_lock);
976cb382536SAndrzej Pietrasiewicz 
97799c49407SMatthew Wilcox 	if (ida_is_empty(&hidg_ida)) {
978cb382536SAndrzej Pietrasiewicz 		status = ghid_setup(NULL, HIDG_MINORS);
979cb382536SAndrzej Pietrasiewicz 		if (status)  {
980cb382536SAndrzej Pietrasiewicz 			ret = ERR_PTR(status);
981cb382536SAndrzej Pietrasiewicz 			kfree(opts);
982cb382536SAndrzej Pietrasiewicz 			goto unlock;
983cb382536SAndrzej Pietrasiewicz 		}
984cb382536SAndrzej Pietrasiewicz 	}
985cb382536SAndrzej Pietrasiewicz 
986cb382536SAndrzej Pietrasiewicz 	opts->minor = hidg_get_minor();
987cb382536SAndrzej Pietrasiewicz 	if (opts->minor < 0) {
988cb382536SAndrzej Pietrasiewicz 		ret = ERR_PTR(opts->minor);
989cb382536SAndrzej Pietrasiewicz 		kfree(opts);
99099c49407SMatthew Wilcox 		if (ida_is_empty(&hidg_ida))
991cb382536SAndrzej Pietrasiewicz 			ghid_cleanup();
992828f6148SDan Carpenter 		goto unlock;
993cb382536SAndrzej Pietrasiewicz 	}
99421a9476aSAndrzej Pietrasiewicz 	config_group_init_type_name(&opts->func_inst.group, "", &hid_func_type);
995cb382536SAndrzej Pietrasiewicz 
996cb382536SAndrzej Pietrasiewicz unlock:
997cb382536SAndrzej Pietrasiewicz 	mutex_unlock(&hidg_ida_lock);
998cb382536SAndrzej Pietrasiewicz 	return ret;
999cb382536SAndrzej Pietrasiewicz }
1000cb382536SAndrzej Pietrasiewicz 
1001cb382536SAndrzej Pietrasiewicz static void hidg_free(struct usb_function *f)
1002cb382536SAndrzej Pietrasiewicz {
1003cb382536SAndrzej Pietrasiewicz 	struct f_hidg *hidg;
100421a9476aSAndrzej Pietrasiewicz 	struct f_hid_opts *opts;
1005cb382536SAndrzej Pietrasiewicz 
1006cb382536SAndrzej Pietrasiewicz 	hidg = func_to_hidg(f);
100721a9476aSAndrzej Pietrasiewicz 	opts = container_of(f->fi, struct f_hid_opts, func_inst);
1008cb382536SAndrzej Pietrasiewicz 	kfree(hidg->report_desc);
1009cb382536SAndrzej Pietrasiewicz 	kfree(hidg);
101021a9476aSAndrzej Pietrasiewicz 	mutex_lock(&opts->lock);
101121a9476aSAndrzej Pietrasiewicz 	--opts->refcnt;
101221a9476aSAndrzej Pietrasiewicz 	mutex_unlock(&opts->lock);
1013cb382536SAndrzej Pietrasiewicz }
1014cb382536SAndrzej Pietrasiewicz 
101500a2430fSAndrzej Pietrasiewicz static void hidg_unbind(struct usb_configuration *c, struct usb_function *f)
101600a2430fSAndrzej Pietrasiewicz {
101700a2430fSAndrzej Pietrasiewicz 	struct f_hidg *hidg = func_to_hidg(f);
101800a2430fSAndrzej Pietrasiewicz 
101900a2430fSAndrzej Pietrasiewicz 	device_destroy(hidg_class, MKDEV(major, hidg->minor));
102000a2430fSAndrzej Pietrasiewicz 	cdev_del(&hidg->cdev);
102100a2430fSAndrzej Pietrasiewicz 
102200a2430fSAndrzej Pietrasiewicz 	/* disable/free request and end point */
102300a2430fSAndrzej Pietrasiewicz 	usb_ep_disable(hidg->in_ep);
102414794d71SFelipe F. Tonello 	free_ep_req(hidg->in_ep, hidg->req);
102500a2430fSAndrzej Pietrasiewicz 
102600a2430fSAndrzej Pietrasiewicz 	usb_free_all_descriptors(f);
102700a2430fSAndrzej Pietrasiewicz }
102800a2430fSAndrzej Pietrasiewicz 
10290fc57ea0SFengguang Wu static struct usb_function *hidg_alloc(struct usb_function_instance *fi)
103000a2430fSAndrzej Pietrasiewicz {
103100a2430fSAndrzej Pietrasiewicz 	struct f_hidg *hidg;
1032cb382536SAndrzej Pietrasiewicz 	struct f_hid_opts *opts;
103300a2430fSAndrzej Pietrasiewicz 
103400a2430fSAndrzej Pietrasiewicz 	/* allocate and initialize one new instance */
1035cb382536SAndrzej Pietrasiewicz 	hidg = kzalloc(sizeof(*hidg), GFP_KERNEL);
103600a2430fSAndrzej Pietrasiewicz 	if (!hidg)
1037cb382536SAndrzej Pietrasiewicz 		return ERR_PTR(-ENOMEM);
103800a2430fSAndrzej Pietrasiewicz 
1039cb382536SAndrzej Pietrasiewicz 	opts = container_of(fi, struct f_hid_opts, func_inst);
1040cb382536SAndrzej Pietrasiewicz 
104121a9476aSAndrzej Pietrasiewicz 	mutex_lock(&opts->lock);
104221a9476aSAndrzej Pietrasiewicz 	++opts->refcnt;
104321a9476aSAndrzej Pietrasiewicz 
1044cb382536SAndrzej Pietrasiewicz 	hidg->minor = opts->minor;
1045cb382536SAndrzej Pietrasiewicz 	hidg->bInterfaceSubClass = opts->subclass;
1046cb382536SAndrzej Pietrasiewicz 	hidg->bInterfaceProtocol = opts->protocol;
1047cb382536SAndrzej Pietrasiewicz 	hidg->report_length = opts->report_length;
1048cb382536SAndrzej Pietrasiewicz 	hidg->report_desc_length = opts->report_desc_length;
1049cb382536SAndrzej Pietrasiewicz 	if (opts->report_desc) {
1050cb382536SAndrzej Pietrasiewicz 		hidg->report_desc = kmemdup(opts->report_desc,
1051cb382536SAndrzej Pietrasiewicz 					    opts->report_desc_length,
105200a2430fSAndrzej Pietrasiewicz 					    GFP_KERNEL);
105300a2430fSAndrzej Pietrasiewicz 		if (!hidg->report_desc) {
105400a2430fSAndrzej Pietrasiewicz 			kfree(hidg);
105521a9476aSAndrzej Pietrasiewicz 			mutex_unlock(&opts->lock);
1056cb382536SAndrzej Pietrasiewicz 			return ERR_PTR(-ENOMEM);
1057cb382536SAndrzej Pietrasiewicz 		}
105800a2430fSAndrzej Pietrasiewicz 	}
105900a2430fSAndrzej Pietrasiewicz 
106021a9476aSAndrzej Pietrasiewicz 	mutex_unlock(&opts->lock);
106121a9476aSAndrzej Pietrasiewicz 
106200a2430fSAndrzej Pietrasiewicz 	hidg->func.name    = "hid";
106300a2430fSAndrzej Pietrasiewicz 	hidg->func.bind    = hidg_bind;
106400a2430fSAndrzej Pietrasiewicz 	hidg->func.unbind  = hidg_unbind;
106500a2430fSAndrzej Pietrasiewicz 	hidg->func.set_alt = hidg_set_alt;
106600a2430fSAndrzej Pietrasiewicz 	hidg->func.disable = hidg_disable;
106700a2430fSAndrzej Pietrasiewicz 	hidg->func.setup   = hidg_setup;
1068cb382536SAndrzej Pietrasiewicz 	hidg->func.free_func = hidg_free;
106900a2430fSAndrzej Pietrasiewicz 
107000a2430fSAndrzej Pietrasiewicz 	/* this could me made configurable at some point */
107100a2430fSAndrzej Pietrasiewicz 	hidg->qlen	   = 4;
107200a2430fSAndrzej Pietrasiewicz 
1073cb382536SAndrzej Pietrasiewicz 	return &hidg->func;
107400a2430fSAndrzej Pietrasiewicz }
107500a2430fSAndrzej Pietrasiewicz 
1076cb382536SAndrzej Pietrasiewicz DECLARE_USB_FUNCTION_INIT(hid, hidg_alloc_inst, hidg_alloc);
1077cb382536SAndrzej Pietrasiewicz MODULE_LICENSE("GPL");
1078cb382536SAndrzej Pietrasiewicz MODULE_AUTHOR("Fabien Chouteau");
1079cb382536SAndrzej Pietrasiewicz 
1080cb382536SAndrzej Pietrasiewicz int ghid_setup(struct usb_gadget *g, int count)
108100a2430fSAndrzej Pietrasiewicz {
108200a2430fSAndrzej Pietrasiewicz 	int status;
108300a2430fSAndrzej Pietrasiewicz 	dev_t dev;
108400a2430fSAndrzej Pietrasiewicz 
108500a2430fSAndrzej Pietrasiewicz 	hidg_class = class_create(THIS_MODULE, "hidg");
108606529407SAndrzej Pietrasiewicz 	if (IS_ERR(hidg_class)) {
10870448d38cSDan Carpenter 		status = PTR_ERR(hidg_class);
108806529407SAndrzej Pietrasiewicz 		hidg_class = NULL;
10890448d38cSDan Carpenter 		return status;
109000a2430fSAndrzej Pietrasiewicz 	}
109100a2430fSAndrzej Pietrasiewicz 
109200a2430fSAndrzej Pietrasiewicz 	status = alloc_chrdev_region(&dev, 0, count, "hidg");
10930448d38cSDan Carpenter 	if (status) {
10940448d38cSDan Carpenter 		class_destroy(hidg_class);
10950448d38cSDan Carpenter 		hidg_class = NULL;
109600a2430fSAndrzej Pietrasiewicz 		return status;
109700a2430fSAndrzej Pietrasiewicz 	}
109800a2430fSAndrzej Pietrasiewicz 
10990448d38cSDan Carpenter 	major = MAJOR(dev);
11000448d38cSDan Carpenter 	minors = count;
11010448d38cSDan Carpenter 
11020448d38cSDan Carpenter 	return 0;
110300a2430fSAndrzej Pietrasiewicz }
110400a2430fSAndrzej Pietrasiewicz 
110500a2430fSAndrzej Pietrasiewicz void ghid_cleanup(void)
110600a2430fSAndrzej Pietrasiewicz {
110700a2430fSAndrzej Pietrasiewicz 	if (major) {
110800a2430fSAndrzej Pietrasiewicz 		unregister_chrdev_region(MKDEV(major, 0), minors);
110900a2430fSAndrzej Pietrasiewicz 		major = minors = 0;
111000a2430fSAndrzej Pietrasiewicz 	}
111100a2430fSAndrzej Pietrasiewicz 
111200a2430fSAndrzej Pietrasiewicz 	class_destroy(hidg_class);
111300a2430fSAndrzej Pietrasiewicz 	hidg_class = NULL;
111400a2430fSAndrzej Pietrasiewicz }
1115