xref: /openbmc/linux/drivers/usb/gadget/function/f_hid.c (revision 828f6148e89ec051c2540400773655c0174ccaa3)
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;
5300a2430fSAndrzej Pietrasiewicz 	spinlock_t			spinlock;
5400a2430fSAndrzej Pietrasiewicz 	wait_queue_head_t		read_queue;
5500a2430fSAndrzej Pietrasiewicz 	unsigned int			qlen;
5600a2430fSAndrzej Pietrasiewicz 
5700a2430fSAndrzej Pietrasiewicz 	/* send report */
5800a2430fSAndrzej Pietrasiewicz 	struct mutex			lock;
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 
10100a2430fSAndrzej Pietrasiewicz /* High-Speed Support */
10200a2430fSAndrzej Pietrasiewicz 
10300a2430fSAndrzej Pietrasiewicz static struct usb_endpoint_descriptor hidg_hs_in_ep_desc = {
10400a2430fSAndrzej Pietrasiewicz 	.bLength		= USB_DT_ENDPOINT_SIZE,
10500a2430fSAndrzej Pietrasiewicz 	.bDescriptorType	= USB_DT_ENDPOINT,
10600a2430fSAndrzej Pietrasiewicz 	.bEndpointAddress	= USB_DIR_IN,
10700a2430fSAndrzej Pietrasiewicz 	.bmAttributes		= USB_ENDPOINT_XFER_INT,
10800a2430fSAndrzej Pietrasiewicz 	/*.wMaxPacketSize	= DYNAMIC */
10900a2430fSAndrzej Pietrasiewicz 	.bInterval		= 4, /* FIXME: Add this field in the
11000a2430fSAndrzej Pietrasiewicz 				      * HID gadget configuration?
11100a2430fSAndrzej Pietrasiewicz 				      * (struct hidg_func_descriptor)
11200a2430fSAndrzej Pietrasiewicz 				      */
11300a2430fSAndrzej Pietrasiewicz };
11400a2430fSAndrzej Pietrasiewicz 
11500a2430fSAndrzej Pietrasiewicz static struct usb_endpoint_descriptor hidg_hs_out_ep_desc = {
11600a2430fSAndrzej Pietrasiewicz 	.bLength		= USB_DT_ENDPOINT_SIZE,
11700a2430fSAndrzej Pietrasiewicz 	.bDescriptorType	= USB_DT_ENDPOINT,
11800a2430fSAndrzej Pietrasiewicz 	.bEndpointAddress	= USB_DIR_OUT,
11900a2430fSAndrzej Pietrasiewicz 	.bmAttributes		= USB_ENDPOINT_XFER_INT,
12000a2430fSAndrzej Pietrasiewicz 	/*.wMaxPacketSize	= DYNAMIC */
12100a2430fSAndrzej Pietrasiewicz 	.bInterval		= 4, /* FIXME: Add this field in the
12200a2430fSAndrzej Pietrasiewicz 				      * HID gadget configuration?
12300a2430fSAndrzej Pietrasiewicz 				      * (struct hidg_func_descriptor)
12400a2430fSAndrzej Pietrasiewicz 				      */
12500a2430fSAndrzej Pietrasiewicz };
12600a2430fSAndrzej Pietrasiewicz 
12700a2430fSAndrzej Pietrasiewicz static struct usb_descriptor_header *hidg_hs_descriptors[] = {
12800a2430fSAndrzej Pietrasiewicz 	(struct usb_descriptor_header *)&hidg_interface_desc,
12900a2430fSAndrzej Pietrasiewicz 	(struct usb_descriptor_header *)&hidg_desc,
13000a2430fSAndrzej Pietrasiewicz 	(struct usb_descriptor_header *)&hidg_hs_in_ep_desc,
13100a2430fSAndrzej Pietrasiewicz 	(struct usb_descriptor_header *)&hidg_hs_out_ep_desc,
13200a2430fSAndrzej Pietrasiewicz 	NULL,
13300a2430fSAndrzej Pietrasiewicz };
13400a2430fSAndrzej Pietrasiewicz 
13500a2430fSAndrzej Pietrasiewicz /* Full-Speed Support */
13600a2430fSAndrzej Pietrasiewicz 
13700a2430fSAndrzej Pietrasiewicz static struct usb_endpoint_descriptor hidg_fs_in_ep_desc = {
13800a2430fSAndrzej Pietrasiewicz 	.bLength		= USB_DT_ENDPOINT_SIZE,
13900a2430fSAndrzej Pietrasiewicz 	.bDescriptorType	= USB_DT_ENDPOINT,
14000a2430fSAndrzej Pietrasiewicz 	.bEndpointAddress	= USB_DIR_IN,
14100a2430fSAndrzej Pietrasiewicz 	.bmAttributes		= USB_ENDPOINT_XFER_INT,
14200a2430fSAndrzej Pietrasiewicz 	/*.wMaxPacketSize	= DYNAMIC */
14300a2430fSAndrzej Pietrasiewicz 	.bInterval		= 10, /* FIXME: Add this field in the
14400a2430fSAndrzej Pietrasiewicz 				       * HID gadget configuration?
14500a2430fSAndrzej Pietrasiewicz 				       * (struct hidg_func_descriptor)
14600a2430fSAndrzej Pietrasiewicz 				       */
14700a2430fSAndrzej Pietrasiewicz };
14800a2430fSAndrzej Pietrasiewicz 
14900a2430fSAndrzej Pietrasiewicz static struct usb_endpoint_descriptor hidg_fs_out_ep_desc = {
15000a2430fSAndrzej Pietrasiewicz 	.bLength		= USB_DT_ENDPOINT_SIZE,
15100a2430fSAndrzej Pietrasiewicz 	.bDescriptorType	= USB_DT_ENDPOINT,
15200a2430fSAndrzej Pietrasiewicz 	.bEndpointAddress	= USB_DIR_OUT,
15300a2430fSAndrzej Pietrasiewicz 	.bmAttributes		= USB_ENDPOINT_XFER_INT,
15400a2430fSAndrzej Pietrasiewicz 	/*.wMaxPacketSize	= DYNAMIC */
15500a2430fSAndrzej Pietrasiewicz 	.bInterval		= 10, /* FIXME: Add this field in the
15600a2430fSAndrzej Pietrasiewicz 				       * HID gadget configuration?
15700a2430fSAndrzej Pietrasiewicz 				       * (struct hidg_func_descriptor)
15800a2430fSAndrzej Pietrasiewicz 				       */
15900a2430fSAndrzej Pietrasiewicz };
16000a2430fSAndrzej Pietrasiewicz 
16100a2430fSAndrzej Pietrasiewicz static struct usb_descriptor_header *hidg_fs_descriptors[] = {
16200a2430fSAndrzej Pietrasiewicz 	(struct usb_descriptor_header *)&hidg_interface_desc,
16300a2430fSAndrzej Pietrasiewicz 	(struct usb_descriptor_header *)&hidg_desc,
16400a2430fSAndrzej Pietrasiewicz 	(struct usb_descriptor_header *)&hidg_fs_in_ep_desc,
16500a2430fSAndrzej Pietrasiewicz 	(struct usb_descriptor_header *)&hidg_fs_out_ep_desc,
16600a2430fSAndrzej Pietrasiewicz 	NULL,
16700a2430fSAndrzej Pietrasiewicz };
16800a2430fSAndrzej Pietrasiewicz 
16900a2430fSAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/
170cb382536SAndrzej Pietrasiewicz /*                                 Strings                                 */
171cb382536SAndrzej Pietrasiewicz 
172cb382536SAndrzej Pietrasiewicz #define CT_FUNC_HID_IDX	0
173cb382536SAndrzej Pietrasiewicz 
174cb382536SAndrzej Pietrasiewicz static struct usb_string ct_func_string_defs[] = {
175cb382536SAndrzej Pietrasiewicz 	[CT_FUNC_HID_IDX].s	= "HID Interface",
176cb382536SAndrzej Pietrasiewicz 	{},			/* end of list */
177cb382536SAndrzej Pietrasiewicz };
178cb382536SAndrzej Pietrasiewicz 
179cb382536SAndrzej Pietrasiewicz static struct usb_gadget_strings ct_func_string_table = {
180cb382536SAndrzej Pietrasiewicz 	.language	= 0x0409,	/* en-US */
181cb382536SAndrzej Pietrasiewicz 	.strings	= ct_func_string_defs,
182cb382536SAndrzej Pietrasiewicz };
183cb382536SAndrzej Pietrasiewicz 
184cb382536SAndrzej Pietrasiewicz static struct usb_gadget_strings *ct_func_strings[] = {
185cb382536SAndrzej Pietrasiewicz 	&ct_func_string_table,
186cb382536SAndrzej Pietrasiewicz 	NULL,
187cb382536SAndrzej Pietrasiewicz };
188cb382536SAndrzej Pietrasiewicz 
189cb382536SAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/
19000a2430fSAndrzej Pietrasiewicz /*                              Char Device                                */
19100a2430fSAndrzej Pietrasiewicz 
19200a2430fSAndrzej Pietrasiewicz static ssize_t f_hidg_read(struct file *file, char __user *buffer,
19300a2430fSAndrzej Pietrasiewicz 			size_t count, loff_t *ptr)
19400a2430fSAndrzej Pietrasiewicz {
19500a2430fSAndrzej Pietrasiewicz 	struct f_hidg *hidg = file->private_data;
19600a2430fSAndrzej Pietrasiewicz 	struct f_hidg_req_list *list;
19700a2430fSAndrzej Pietrasiewicz 	struct usb_request *req;
19800a2430fSAndrzej Pietrasiewicz 	unsigned long flags;
19900a2430fSAndrzej Pietrasiewicz 	int ret;
20000a2430fSAndrzej Pietrasiewicz 
20100a2430fSAndrzej Pietrasiewicz 	if (!count)
20200a2430fSAndrzej Pietrasiewicz 		return 0;
20300a2430fSAndrzej Pietrasiewicz 
20400a2430fSAndrzej Pietrasiewicz 	if (!access_ok(VERIFY_WRITE, buffer, count))
20500a2430fSAndrzej Pietrasiewicz 		return -EFAULT;
20600a2430fSAndrzej Pietrasiewicz 
20700a2430fSAndrzej Pietrasiewicz 	spin_lock_irqsave(&hidg->spinlock, flags);
20800a2430fSAndrzej Pietrasiewicz 
20900a2430fSAndrzej Pietrasiewicz #define READ_COND (!list_empty(&hidg->completed_out_req))
21000a2430fSAndrzej Pietrasiewicz 
21100a2430fSAndrzej Pietrasiewicz 	/* wait for at least one buffer to complete */
21200a2430fSAndrzej Pietrasiewicz 	while (!READ_COND) {
21300a2430fSAndrzej Pietrasiewicz 		spin_unlock_irqrestore(&hidg->spinlock, flags);
21400a2430fSAndrzej Pietrasiewicz 		if (file->f_flags & O_NONBLOCK)
21500a2430fSAndrzej Pietrasiewicz 			return -EAGAIN;
21600a2430fSAndrzej Pietrasiewicz 
21700a2430fSAndrzej Pietrasiewicz 		if (wait_event_interruptible(hidg->read_queue, READ_COND))
21800a2430fSAndrzej Pietrasiewicz 			return -ERESTARTSYS;
21900a2430fSAndrzej Pietrasiewicz 
22000a2430fSAndrzej Pietrasiewicz 		spin_lock_irqsave(&hidg->spinlock, flags);
22100a2430fSAndrzej Pietrasiewicz 	}
22200a2430fSAndrzej Pietrasiewicz 
22300a2430fSAndrzej Pietrasiewicz 	/* pick the first one */
22400a2430fSAndrzej Pietrasiewicz 	list = list_first_entry(&hidg->completed_out_req,
22500a2430fSAndrzej Pietrasiewicz 				struct f_hidg_req_list, list);
22600a2430fSAndrzej Pietrasiewicz 	req = list->req;
22700a2430fSAndrzej Pietrasiewicz 	count = min_t(unsigned int, count, req->actual - list->pos);
22800a2430fSAndrzej Pietrasiewicz 	spin_unlock_irqrestore(&hidg->spinlock, flags);
22900a2430fSAndrzej Pietrasiewicz 
23000a2430fSAndrzej Pietrasiewicz 	/* copy to user outside spinlock */
23100a2430fSAndrzej Pietrasiewicz 	count -= copy_to_user(buffer, req->buf + list->pos, count);
23200a2430fSAndrzej Pietrasiewicz 	list->pos += count;
23300a2430fSAndrzej Pietrasiewicz 
23400a2430fSAndrzej Pietrasiewicz 	/*
23500a2430fSAndrzej Pietrasiewicz 	 * if this request is completely handled and transfered to
23600a2430fSAndrzej Pietrasiewicz 	 * userspace, remove its entry from the list and requeue it
23700a2430fSAndrzej Pietrasiewicz 	 * again. Otherwise, we will revisit it again upon the next
23800a2430fSAndrzej Pietrasiewicz 	 * call, taking into account its current read position.
23900a2430fSAndrzej Pietrasiewicz 	 */
24000a2430fSAndrzej Pietrasiewicz 	if (list->pos == req->actual) {
24100a2430fSAndrzej Pietrasiewicz 		spin_lock_irqsave(&hidg->spinlock, flags);
24200a2430fSAndrzej Pietrasiewicz 		list_del(&list->list);
24300a2430fSAndrzej Pietrasiewicz 		kfree(list);
24400a2430fSAndrzej Pietrasiewicz 		spin_unlock_irqrestore(&hidg->spinlock, flags);
24500a2430fSAndrzej Pietrasiewicz 
24600a2430fSAndrzej Pietrasiewicz 		req->length = hidg->report_length;
24700a2430fSAndrzej Pietrasiewicz 		ret = usb_ep_queue(hidg->out_ep, req, GFP_KERNEL);
24800a2430fSAndrzej Pietrasiewicz 		if (ret < 0)
24900a2430fSAndrzej Pietrasiewicz 			return ret;
25000a2430fSAndrzej Pietrasiewicz 	}
25100a2430fSAndrzej Pietrasiewicz 
25200a2430fSAndrzej Pietrasiewicz 	return count;
25300a2430fSAndrzej Pietrasiewicz }
25400a2430fSAndrzej Pietrasiewicz 
25500a2430fSAndrzej Pietrasiewicz static void f_hidg_req_complete(struct usb_ep *ep, struct usb_request *req)
25600a2430fSAndrzej Pietrasiewicz {
25700a2430fSAndrzej Pietrasiewicz 	struct f_hidg *hidg = (struct f_hidg *)ep->driver_data;
25800a2430fSAndrzej Pietrasiewicz 
25900a2430fSAndrzej Pietrasiewicz 	if (req->status != 0) {
26000a2430fSAndrzej Pietrasiewicz 		ERROR(hidg->func.config->cdev,
26100a2430fSAndrzej Pietrasiewicz 			"End Point Request ERROR: %d\n", req->status);
26200a2430fSAndrzej Pietrasiewicz 	}
26300a2430fSAndrzej Pietrasiewicz 
26400a2430fSAndrzej Pietrasiewicz 	hidg->write_pending = 0;
26500a2430fSAndrzej Pietrasiewicz 	wake_up(&hidg->write_queue);
26600a2430fSAndrzej Pietrasiewicz }
26700a2430fSAndrzej Pietrasiewicz 
26800a2430fSAndrzej Pietrasiewicz static ssize_t f_hidg_write(struct file *file, const char __user *buffer,
26900a2430fSAndrzej Pietrasiewicz 			    size_t count, loff_t *offp)
27000a2430fSAndrzej Pietrasiewicz {
27100a2430fSAndrzej Pietrasiewicz 	struct f_hidg *hidg  = file->private_data;
27200a2430fSAndrzej Pietrasiewicz 	ssize_t status = -ENOMEM;
27300a2430fSAndrzej Pietrasiewicz 
27400a2430fSAndrzej Pietrasiewicz 	if (!access_ok(VERIFY_READ, buffer, count))
27500a2430fSAndrzej Pietrasiewicz 		return -EFAULT;
27600a2430fSAndrzej Pietrasiewicz 
27700a2430fSAndrzej Pietrasiewicz 	mutex_lock(&hidg->lock);
27800a2430fSAndrzej Pietrasiewicz 
27900a2430fSAndrzej Pietrasiewicz #define WRITE_COND (!hidg->write_pending)
28000a2430fSAndrzej Pietrasiewicz 
28100a2430fSAndrzej Pietrasiewicz 	/* write queue */
28200a2430fSAndrzej Pietrasiewicz 	while (!WRITE_COND) {
28300a2430fSAndrzej Pietrasiewicz 		mutex_unlock(&hidg->lock);
28400a2430fSAndrzej Pietrasiewicz 		if (file->f_flags & O_NONBLOCK)
28500a2430fSAndrzej Pietrasiewicz 			return -EAGAIN;
28600a2430fSAndrzej Pietrasiewicz 
28700a2430fSAndrzej Pietrasiewicz 		if (wait_event_interruptible_exclusive(
28800a2430fSAndrzej Pietrasiewicz 				hidg->write_queue, WRITE_COND))
28900a2430fSAndrzej Pietrasiewicz 			return -ERESTARTSYS;
29000a2430fSAndrzej Pietrasiewicz 
29100a2430fSAndrzej Pietrasiewicz 		mutex_lock(&hidg->lock);
29200a2430fSAndrzej Pietrasiewicz 	}
29300a2430fSAndrzej Pietrasiewicz 
29400a2430fSAndrzej Pietrasiewicz 	count  = min_t(unsigned, count, hidg->report_length);
29500a2430fSAndrzej Pietrasiewicz 	status = copy_from_user(hidg->req->buf, buffer, count);
29600a2430fSAndrzej Pietrasiewicz 
29700a2430fSAndrzej Pietrasiewicz 	if (status != 0) {
29800a2430fSAndrzej Pietrasiewicz 		ERROR(hidg->func.config->cdev,
29900a2430fSAndrzej Pietrasiewicz 			"copy_from_user error\n");
30000a2430fSAndrzej Pietrasiewicz 		mutex_unlock(&hidg->lock);
30100a2430fSAndrzej Pietrasiewicz 		return -EINVAL;
30200a2430fSAndrzej Pietrasiewicz 	}
30300a2430fSAndrzej Pietrasiewicz 
30400a2430fSAndrzej Pietrasiewicz 	hidg->req->status   = 0;
30500a2430fSAndrzej Pietrasiewicz 	hidg->req->zero     = 0;
30600a2430fSAndrzej Pietrasiewicz 	hidg->req->length   = count;
30700a2430fSAndrzej Pietrasiewicz 	hidg->req->complete = f_hidg_req_complete;
30800a2430fSAndrzej Pietrasiewicz 	hidg->req->context  = hidg;
30900a2430fSAndrzej Pietrasiewicz 	hidg->write_pending = 1;
31000a2430fSAndrzej Pietrasiewicz 
31100a2430fSAndrzej Pietrasiewicz 	status = usb_ep_queue(hidg->in_ep, hidg->req, GFP_ATOMIC);
31200a2430fSAndrzej Pietrasiewicz 	if (status < 0) {
31300a2430fSAndrzej Pietrasiewicz 		ERROR(hidg->func.config->cdev,
31400a2430fSAndrzej Pietrasiewicz 			"usb_ep_queue error on int endpoint %zd\n", status);
31500a2430fSAndrzej Pietrasiewicz 		hidg->write_pending = 0;
31600a2430fSAndrzej Pietrasiewicz 		wake_up(&hidg->write_queue);
31700a2430fSAndrzej Pietrasiewicz 	} else {
31800a2430fSAndrzej Pietrasiewicz 		status = count;
31900a2430fSAndrzej Pietrasiewicz 	}
32000a2430fSAndrzej Pietrasiewicz 
32100a2430fSAndrzej Pietrasiewicz 	mutex_unlock(&hidg->lock);
32200a2430fSAndrzej Pietrasiewicz 
32300a2430fSAndrzej Pietrasiewicz 	return status;
32400a2430fSAndrzej Pietrasiewicz }
32500a2430fSAndrzej Pietrasiewicz 
32600a2430fSAndrzej Pietrasiewicz static unsigned int f_hidg_poll(struct file *file, poll_table *wait)
32700a2430fSAndrzej Pietrasiewicz {
32800a2430fSAndrzej Pietrasiewicz 	struct f_hidg	*hidg  = file->private_data;
32900a2430fSAndrzej Pietrasiewicz 	unsigned int	ret = 0;
33000a2430fSAndrzej Pietrasiewicz 
33100a2430fSAndrzej Pietrasiewicz 	poll_wait(file, &hidg->read_queue, wait);
33200a2430fSAndrzej Pietrasiewicz 	poll_wait(file, &hidg->write_queue, wait);
33300a2430fSAndrzej Pietrasiewicz 
33400a2430fSAndrzej Pietrasiewicz 	if (WRITE_COND)
33500a2430fSAndrzej Pietrasiewicz 		ret |= POLLOUT | POLLWRNORM;
33600a2430fSAndrzej Pietrasiewicz 
33700a2430fSAndrzej Pietrasiewicz 	if (READ_COND)
33800a2430fSAndrzej Pietrasiewicz 		ret |= POLLIN | POLLRDNORM;
33900a2430fSAndrzej Pietrasiewicz 
34000a2430fSAndrzej Pietrasiewicz 	return ret;
34100a2430fSAndrzej Pietrasiewicz }
34200a2430fSAndrzej Pietrasiewicz 
34300a2430fSAndrzej Pietrasiewicz #undef WRITE_COND
34400a2430fSAndrzej Pietrasiewicz #undef READ_COND
34500a2430fSAndrzej Pietrasiewicz 
34600a2430fSAndrzej Pietrasiewicz static int f_hidg_release(struct inode *inode, struct file *fd)
34700a2430fSAndrzej Pietrasiewicz {
34800a2430fSAndrzej Pietrasiewicz 	fd->private_data = NULL;
34900a2430fSAndrzej Pietrasiewicz 	return 0;
35000a2430fSAndrzej Pietrasiewicz }
35100a2430fSAndrzej Pietrasiewicz 
35200a2430fSAndrzej Pietrasiewicz static int f_hidg_open(struct inode *inode, struct file *fd)
35300a2430fSAndrzej Pietrasiewicz {
35400a2430fSAndrzej Pietrasiewicz 	struct f_hidg *hidg =
35500a2430fSAndrzej Pietrasiewicz 		container_of(inode->i_cdev, struct f_hidg, cdev);
35600a2430fSAndrzej Pietrasiewicz 
35700a2430fSAndrzej Pietrasiewicz 	fd->private_data = hidg;
35800a2430fSAndrzej Pietrasiewicz 
35900a2430fSAndrzej Pietrasiewicz 	return 0;
36000a2430fSAndrzej Pietrasiewicz }
36100a2430fSAndrzej Pietrasiewicz 
36200a2430fSAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/
36300a2430fSAndrzej Pietrasiewicz /*                                usb_function                             */
36400a2430fSAndrzej Pietrasiewicz 
36500a2430fSAndrzej Pietrasiewicz static inline struct usb_request *hidg_alloc_ep_req(struct usb_ep *ep,
36600a2430fSAndrzej Pietrasiewicz 						    unsigned length)
36700a2430fSAndrzej Pietrasiewicz {
36800a2430fSAndrzej Pietrasiewicz 	return alloc_ep_req(ep, length, length);
36900a2430fSAndrzej Pietrasiewicz }
37000a2430fSAndrzej Pietrasiewicz 
37100a2430fSAndrzej Pietrasiewicz static void hidg_set_report_complete(struct usb_ep *ep, struct usb_request *req)
37200a2430fSAndrzej Pietrasiewicz {
37300a2430fSAndrzej Pietrasiewicz 	struct f_hidg *hidg = (struct f_hidg *) req->context;
37400a2430fSAndrzej Pietrasiewicz 	struct f_hidg_req_list *req_list;
37500a2430fSAndrzej Pietrasiewicz 	unsigned long flags;
37600a2430fSAndrzej Pietrasiewicz 
37700a2430fSAndrzej Pietrasiewicz 	req_list = kzalloc(sizeof(*req_list), GFP_ATOMIC);
37800a2430fSAndrzej Pietrasiewicz 	if (!req_list)
37900a2430fSAndrzej Pietrasiewicz 		return;
38000a2430fSAndrzej Pietrasiewicz 
38100a2430fSAndrzej Pietrasiewicz 	req_list->req = req;
38200a2430fSAndrzej Pietrasiewicz 
38300a2430fSAndrzej Pietrasiewicz 	spin_lock_irqsave(&hidg->spinlock, flags);
38400a2430fSAndrzej Pietrasiewicz 	list_add_tail(&req_list->list, &hidg->completed_out_req);
38500a2430fSAndrzej Pietrasiewicz 	spin_unlock_irqrestore(&hidg->spinlock, flags);
38600a2430fSAndrzej Pietrasiewicz 
38700a2430fSAndrzej Pietrasiewicz 	wake_up(&hidg->read_queue);
38800a2430fSAndrzej Pietrasiewicz }
38900a2430fSAndrzej Pietrasiewicz 
39000a2430fSAndrzej Pietrasiewicz static int hidg_setup(struct usb_function *f,
39100a2430fSAndrzej Pietrasiewicz 		const struct usb_ctrlrequest *ctrl)
39200a2430fSAndrzej Pietrasiewicz {
39300a2430fSAndrzej Pietrasiewicz 	struct f_hidg			*hidg = func_to_hidg(f);
39400a2430fSAndrzej Pietrasiewicz 	struct usb_composite_dev	*cdev = f->config->cdev;
39500a2430fSAndrzej Pietrasiewicz 	struct usb_request		*req  = cdev->req;
39600a2430fSAndrzej Pietrasiewicz 	int status = 0;
39700a2430fSAndrzej Pietrasiewicz 	__u16 value, length;
39800a2430fSAndrzej Pietrasiewicz 
39900a2430fSAndrzej Pietrasiewicz 	value	= __le16_to_cpu(ctrl->wValue);
40000a2430fSAndrzej Pietrasiewicz 	length	= __le16_to_cpu(ctrl->wLength);
40100a2430fSAndrzej Pietrasiewicz 
40200a2430fSAndrzej Pietrasiewicz 	VDBG(cdev, "hid_setup crtl_request : bRequestType:0x%x bRequest:0x%x "
40300a2430fSAndrzej Pietrasiewicz 		"Value:0x%x\n", ctrl->bRequestType, ctrl->bRequest, value);
40400a2430fSAndrzej Pietrasiewicz 
40500a2430fSAndrzej Pietrasiewicz 	switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
40600a2430fSAndrzej Pietrasiewicz 	case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
40700a2430fSAndrzej Pietrasiewicz 		  | HID_REQ_GET_REPORT):
40800a2430fSAndrzej Pietrasiewicz 		VDBG(cdev, "get_report\n");
40900a2430fSAndrzej Pietrasiewicz 
41000a2430fSAndrzej Pietrasiewicz 		/* send an empty report */
41100a2430fSAndrzej Pietrasiewicz 		length = min_t(unsigned, length, hidg->report_length);
41200a2430fSAndrzej Pietrasiewicz 		memset(req->buf, 0x0, length);
41300a2430fSAndrzej Pietrasiewicz 
41400a2430fSAndrzej Pietrasiewicz 		goto respond;
41500a2430fSAndrzej Pietrasiewicz 		break;
41600a2430fSAndrzej Pietrasiewicz 
41700a2430fSAndrzej Pietrasiewicz 	case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
41800a2430fSAndrzej Pietrasiewicz 		  | HID_REQ_GET_PROTOCOL):
41900a2430fSAndrzej Pietrasiewicz 		VDBG(cdev, "get_protocol\n");
42000a2430fSAndrzej Pietrasiewicz 		goto stall;
42100a2430fSAndrzej Pietrasiewicz 		break;
42200a2430fSAndrzej Pietrasiewicz 
42300a2430fSAndrzej Pietrasiewicz 	case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
42400a2430fSAndrzej Pietrasiewicz 		  | HID_REQ_SET_REPORT):
42500a2430fSAndrzej Pietrasiewicz 		VDBG(cdev, "set_report | wLenght=%d\n", ctrl->wLength);
42600a2430fSAndrzej Pietrasiewicz 		goto stall;
42700a2430fSAndrzej Pietrasiewicz 		break;
42800a2430fSAndrzej Pietrasiewicz 
42900a2430fSAndrzej Pietrasiewicz 	case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
43000a2430fSAndrzej Pietrasiewicz 		  | HID_REQ_SET_PROTOCOL):
43100a2430fSAndrzej Pietrasiewicz 		VDBG(cdev, "set_protocol\n");
43200a2430fSAndrzej Pietrasiewicz 		goto stall;
43300a2430fSAndrzej Pietrasiewicz 		break;
43400a2430fSAndrzej Pietrasiewicz 
43500a2430fSAndrzej Pietrasiewicz 	case ((USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_INTERFACE) << 8
43600a2430fSAndrzej Pietrasiewicz 		  | USB_REQ_GET_DESCRIPTOR):
43700a2430fSAndrzej Pietrasiewicz 		switch (value >> 8) {
43800a2430fSAndrzej Pietrasiewicz 		case HID_DT_HID:
43900a2430fSAndrzej Pietrasiewicz 			VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: HID\n");
44000a2430fSAndrzej Pietrasiewicz 			length = min_t(unsigned short, length,
44100a2430fSAndrzej Pietrasiewicz 						   hidg_desc.bLength);
44200a2430fSAndrzej Pietrasiewicz 			memcpy(req->buf, &hidg_desc, length);
44300a2430fSAndrzej Pietrasiewicz 			goto respond;
44400a2430fSAndrzej Pietrasiewicz 			break;
44500a2430fSAndrzej Pietrasiewicz 		case HID_DT_REPORT:
44600a2430fSAndrzej Pietrasiewicz 			VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: REPORT\n");
44700a2430fSAndrzej Pietrasiewicz 			length = min_t(unsigned short, length,
44800a2430fSAndrzej Pietrasiewicz 						   hidg->report_desc_length);
44900a2430fSAndrzej Pietrasiewicz 			memcpy(req->buf, hidg->report_desc, length);
45000a2430fSAndrzej Pietrasiewicz 			goto respond;
45100a2430fSAndrzej Pietrasiewicz 			break;
45200a2430fSAndrzej Pietrasiewicz 
45300a2430fSAndrzej Pietrasiewicz 		default:
45400a2430fSAndrzej Pietrasiewicz 			VDBG(cdev, "Unknown descriptor request 0x%x\n",
45500a2430fSAndrzej Pietrasiewicz 				 value >> 8);
45600a2430fSAndrzej Pietrasiewicz 			goto stall;
45700a2430fSAndrzej Pietrasiewicz 			break;
45800a2430fSAndrzej Pietrasiewicz 		}
45900a2430fSAndrzej Pietrasiewicz 		break;
46000a2430fSAndrzej Pietrasiewicz 
46100a2430fSAndrzej Pietrasiewicz 	default:
46200a2430fSAndrzej Pietrasiewicz 		VDBG(cdev, "Unknown request 0x%x\n",
46300a2430fSAndrzej Pietrasiewicz 			 ctrl->bRequest);
46400a2430fSAndrzej Pietrasiewicz 		goto stall;
46500a2430fSAndrzej Pietrasiewicz 		break;
46600a2430fSAndrzej Pietrasiewicz 	}
46700a2430fSAndrzej Pietrasiewicz 
46800a2430fSAndrzej Pietrasiewicz stall:
46900a2430fSAndrzej Pietrasiewicz 	return -EOPNOTSUPP;
47000a2430fSAndrzej Pietrasiewicz 
47100a2430fSAndrzej Pietrasiewicz respond:
47200a2430fSAndrzej Pietrasiewicz 	req->zero = 0;
47300a2430fSAndrzej Pietrasiewicz 	req->length = length;
47400a2430fSAndrzej Pietrasiewicz 	status = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
47500a2430fSAndrzej Pietrasiewicz 	if (status < 0)
47600a2430fSAndrzej Pietrasiewicz 		ERROR(cdev, "usb_ep_queue error on ep0 %d\n", value);
47700a2430fSAndrzej Pietrasiewicz 	return status;
47800a2430fSAndrzej Pietrasiewicz }
47900a2430fSAndrzej Pietrasiewicz 
48000a2430fSAndrzej Pietrasiewicz static void hidg_disable(struct usb_function *f)
48100a2430fSAndrzej Pietrasiewicz {
48200a2430fSAndrzej Pietrasiewicz 	struct f_hidg *hidg = func_to_hidg(f);
48300a2430fSAndrzej Pietrasiewicz 	struct f_hidg_req_list *list, *next;
48400a2430fSAndrzej Pietrasiewicz 
48500a2430fSAndrzej Pietrasiewicz 	usb_ep_disable(hidg->in_ep);
48600a2430fSAndrzej Pietrasiewicz 	hidg->in_ep->driver_data = NULL;
48700a2430fSAndrzej Pietrasiewicz 
48800a2430fSAndrzej Pietrasiewicz 	usb_ep_disable(hidg->out_ep);
48900a2430fSAndrzej Pietrasiewicz 	hidg->out_ep->driver_data = NULL;
49000a2430fSAndrzej Pietrasiewicz 
49100a2430fSAndrzej Pietrasiewicz 	list_for_each_entry_safe(list, next, &hidg->completed_out_req, list) {
49200a2430fSAndrzej Pietrasiewicz 		list_del(&list->list);
49300a2430fSAndrzej Pietrasiewicz 		kfree(list);
49400a2430fSAndrzej Pietrasiewicz 	}
49500a2430fSAndrzej Pietrasiewicz }
49600a2430fSAndrzej Pietrasiewicz 
49700a2430fSAndrzej Pietrasiewicz static int hidg_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
49800a2430fSAndrzej Pietrasiewicz {
49900a2430fSAndrzej Pietrasiewicz 	struct usb_composite_dev		*cdev = f->config->cdev;
50000a2430fSAndrzej Pietrasiewicz 	struct f_hidg				*hidg = func_to_hidg(f);
50100a2430fSAndrzej Pietrasiewicz 	int i, status = 0;
50200a2430fSAndrzej Pietrasiewicz 
50300a2430fSAndrzej Pietrasiewicz 	VDBG(cdev, "hidg_set_alt intf:%d alt:%d\n", intf, alt);
50400a2430fSAndrzej Pietrasiewicz 
50500a2430fSAndrzej Pietrasiewicz 	if (hidg->in_ep != NULL) {
50600a2430fSAndrzej Pietrasiewicz 		/* restart endpoint */
50700a2430fSAndrzej Pietrasiewicz 		if (hidg->in_ep->driver_data != NULL)
50800a2430fSAndrzej Pietrasiewicz 			usb_ep_disable(hidg->in_ep);
50900a2430fSAndrzej Pietrasiewicz 
51000a2430fSAndrzej Pietrasiewicz 		status = config_ep_by_speed(f->config->cdev->gadget, f,
51100a2430fSAndrzej Pietrasiewicz 					    hidg->in_ep);
51200a2430fSAndrzej Pietrasiewicz 		if (status) {
51300a2430fSAndrzej Pietrasiewicz 			ERROR(cdev, "config_ep_by_speed FAILED!\n");
51400a2430fSAndrzej Pietrasiewicz 			goto fail;
51500a2430fSAndrzej Pietrasiewicz 		}
51600a2430fSAndrzej Pietrasiewicz 		status = usb_ep_enable(hidg->in_ep);
51700a2430fSAndrzej Pietrasiewicz 		if (status < 0) {
51800a2430fSAndrzej Pietrasiewicz 			ERROR(cdev, "Enable IN endpoint FAILED!\n");
51900a2430fSAndrzej Pietrasiewicz 			goto fail;
52000a2430fSAndrzej Pietrasiewicz 		}
52100a2430fSAndrzej Pietrasiewicz 		hidg->in_ep->driver_data = hidg;
52200a2430fSAndrzej Pietrasiewicz 	}
52300a2430fSAndrzej Pietrasiewicz 
52400a2430fSAndrzej Pietrasiewicz 
52500a2430fSAndrzej Pietrasiewicz 	if (hidg->out_ep != NULL) {
52600a2430fSAndrzej Pietrasiewicz 		/* restart endpoint */
52700a2430fSAndrzej Pietrasiewicz 		if (hidg->out_ep->driver_data != NULL)
52800a2430fSAndrzej Pietrasiewicz 			usb_ep_disable(hidg->out_ep);
52900a2430fSAndrzej Pietrasiewicz 
53000a2430fSAndrzej Pietrasiewicz 		status = config_ep_by_speed(f->config->cdev->gadget, f,
53100a2430fSAndrzej Pietrasiewicz 					    hidg->out_ep);
53200a2430fSAndrzej Pietrasiewicz 		if (status) {
53300a2430fSAndrzej Pietrasiewicz 			ERROR(cdev, "config_ep_by_speed FAILED!\n");
53400a2430fSAndrzej Pietrasiewicz 			goto fail;
53500a2430fSAndrzej Pietrasiewicz 		}
53600a2430fSAndrzej Pietrasiewicz 		status = usb_ep_enable(hidg->out_ep);
53700a2430fSAndrzej Pietrasiewicz 		if (status < 0) {
53800a2430fSAndrzej Pietrasiewicz 			ERROR(cdev, "Enable IN endpoint FAILED!\n");
53900a2430fSAndrzej Pietrasiewicz 			goto fail;
54000a2430fSAndrzej Pietrasiewicz 		}
54100a2430fSAndrzej Pietrasiewicz 		hidg->out_ep->driver_data = hidg;
54200a2430fSAndrzej Pietrasiewicz 
54300a2430fSAndrzej Pietrasiewicz 		/*
54400a2430fSAndrzej Pietrasiewicz 		 * allocate a bunch of read buffers and queue them all at once.
54500a2430fSAndrzej Pietrasiewicz 		 */
54600a2430fSAndrzej Pietrasiewicz 		for (i = 0; i < hidg->qlen && status == 0; i++) {
54700a2430fSAndrzej Pietrasiewicz 			struct usb_request *req =
54800a2430fSAndrzej Pietrasiewicz 					hidg_alloc_ep_req(hidg->out_ep,
54900a2430fSAndrzej Pietrasiewicz 							  hidg->report_length);
55000a2430fSAndrzej Pietrasiewicz 			if (req) {
55100a2430fSAndrzej Pietrasiewicz 				req->complete = hidg_set_report_complete;
55200a2430fSAndrzej Pietrasiewicz 				req->context  = hidg;
55300a2430fSAndrzej Pietrasiewicz 				status = usb_ep_queue(hidg->out_ep, req,
55400a2430fSAndrzej Pietrasiewicz 						      GFP_ATOMIC);
55500a2430fSAndrzej Pietrasiewicz 				if (status)
55600a2430fSAndrzej Pietrasiewicz 					ERROR(cdev, "%s queue req --> %d\n",
55700a2430fSAndrzej Pietrasiewicz 						hidg->out_ep->name, status);
55800a2430fSAndrzej Pietrasiewicz 			} else {
55900a2430fSAndrzej Pietrasiewicz 				usb_ep_disable(hidg->out_ep);
56000a2430fSAndrzej Pietrasiewicz 				hidg->out_ep->driver_data = NULL;
56100a2430fSAndrzej Pietrasiewicz 				status = -ENOMEM;
56200a2430fSAndrzej Pietrasiewicz 				goto fail;
56300a2430fSAndrzej Pietrasiewicz 			}
56400a2430fSAndrzej Pietrasiewicz 		}
56500a2430fSAndrzej Pietrasiewicz 	}
56600a2430fSAndrzej Pietrasiewicz 
56700a2430fSAndrzej Pietrasiewicz fail:
56800a2430fSAndrzej Pietrasiewicz 	return status;
56900a2430fSAndrzej Pietrasiewicz }
57000a2430fSAndrzej Pietrasiewicz 
57100a2430fSAndrzej Pietrasiewicz const struct file_operations f_hidg_fops = {
57200a2430fSAndrzej Pietrasiewicz 	.owner		= THIS_MODULE,
57300a2430fSAndrzej Pietrasiewicz 	.open		= f_hidg_open,
57400a2430fSAndrzej Pietrasiewicz 	.release	= f_hidg_release,
57500a2430fSAndrzej Pietrasiewicz 	.write		= f_hidg_write,
57600a2430fSAndrzej Pietrasiewicz 	.read		= f_hidg_read,
57700a2430fSAndrzej Pietrasiewicz 	.poll		= f_hidg_poll,
57800a2430fSAndrzej Pietrasiewicz 	.llseek		= noop_llseek,
57900a2430fSAndrzej Pietrasiewicz };
58000a2430fSAndrzej Pietrasiewicz 
581cb382536SAndrzej Pietrasiewicz static int hidg_bind(struct usb_configuration *c, struct usb_function *f)
58200a2430fSAndrzej Pietrasiewicz {
58300a2430fSAndrzej Pietrasiewicz 	struct usb_ep		*ep;
58400a2430fSAndrzej Pietrasiewicz 	struct f_hidg		*hidg = func_to_hidg(f);
5855ca8d3ecSAndrzej Pietrasiewicz 	struct usb_string	*us;
58663406087SAndrzej Pietrasiewicz 	struct device		*device;
58700a2430fSAndrzej Pietrasiewicz 	int			status;
58800a2430fSAndrzej Pietrasiewicz 	dev_t			dev;
58900a2430fSAndrzej Pietrasiewicz 
590cb382536SAndrzej Pietrasiewicz 	/* maybe allocate device-global string IDs, and patch descriptors */
5915ca8d3ecSAndrzej Pietrasiewicz 	us = usb_gstrings_attach(c->cdev, ct_func_strings,
5925ca8d3ecSAndrzej Pietrasiewicz 				 ARRAY_SIZE(ct_func_string_defs));
5935ca8d3ecSAndrzej Pietrasiewicz 	if (IS_ERR(us))
5945ca8d3ecSAndrzej Pietrasiewicz 		return PTR_ERR(us);
5955ca8d3ecSAndrzej Pietrasiewicz 	hidg_interface_desc.iInterface = us[CT_FUNC_HID_IDX].id;
596cb382536SAndrzej Pietrasiewicz 
59700a2430fSAndrzej Pietrasiewicz 	/* allocate instance-specific interface IDs, and patch descriptors */
59800a2430fSAndrzej Pietrasiewicz 	status = usb_interface_id(c, f);
59900a2430fSAndrzej Pietrasiewicz 	if (status < 0)
60000a2430fSAndrzej Pietrasiewicz 		goto fail;
60100a2430fSAndrzej Pietrasiewicz 	hidg_interface_desc.bInterfaceNumber = status;
60200a2430fSAndrzej Pietrasiewicz 
60300a2430fSAndrzej Pietrasiewicz 	/* allocate instance-specific endpoints */
60400a2430fSAndrzej Pietrasiewicz 	status = -ENODEV;
60500a2430fSAndrzej Pietrasiewicz 	ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_in_ep_desc);
60600a2430fSAndrzej Pietrasiewicz 	if (!ep)
60700a2430fSAndrzej Pietrasiewicz 		goto fail;
60800a2430fSAndrzej Pietrasiewicz 	ep->driver_data = c->cdev;	/* claim */
60900a2430fSAndrzej Pietrasiewicz 	hidg->in_ep = ep;
61000a2430fSAndrzej Pietrasiewicz 
61100a2430fSAndrzej Pietrasiewicz 	ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_out_ep_desc);
61200a2430fSAndrzej Pietrasiewicz 	if (!ep)
61300a2430fSAndrzej Pietrasiewicz 		goto fail;
61400a2430fSAndrzej Pietrasiewicz 	ep->driver_data = c->cdev;	/* claim */
61500a2430fSAndrzej Pietrasiewicz 	hidg->out_ep = ep;
61600a2430fSAndrzej Pietrasiewicz 
61700a2430fSAndrzej Pietrasiewicz 	/* preallocate request and buffer */
61800a2430fSAndrzej Pietrasiewicz 	status = -ENOMEM;
61900a2430fSAndrzej Pietrasiewicz 	hidg->req = usb_ep_alloc_request(hidg->in_ep, GFP_KERNEL);
62000a2430fSAndrzej Pietrasiewicz 	if (!hidg->req)
62100a2430fSAndrzej Pietrasiewicz 		goto fail;
62200a2430fSAndrzej Pietrasiewicz 
62300a2430fSAndrzej Pietrasiewicz 	hidg->req->buf = kmalloc(hidg->report_length, GFP_KERNEL);
62400a2430fSAndrzej Pietrasiewicz 	if (!hidg->req->buf)
62500a2430fSAndrzej Pietrasiewicz 		goto fail;
62600a2430fSAndrzej Pietrasiewicz 
62700a2430fSAndrzej Pietrasiewicz 	/* set descriptor dynamic values */
62800a2430fSAndrzej Pietrasiewicz 	hidg_interface_desc.bInterfaceSubClass = hidg->bInterfaceSubClass;
62900a2430fSAndrzej Pietrasiewicz 	hidg_interface_desc.bInterfaceProtocol = hidg->bInterfaceProtocol;
63000a2430fSAndrzej Pietrasiewicz 	hidg_hs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
63100a2430fSAndrzej Pietrasiewicz 	hidg_fs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
63200a2430fSAndrzej Pietrasiewicz 	hidg_hs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
63300a2430fSAndrzej Pietrasiewicz 	hidg_fs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
63400a2430fSAndrzej Pietrasiewicz 	hidg_desc.desc[0].bDescriptorType = HID_DT_REPORT;
63500a2430fSAndrzej Pietrasiewicz 	hidg_desc.desc[0].wDescriptorLength =
63600a2430fSAndrzej Pietrasiewicz 		cpu_to_le16(hidg->report_desc_length);
63700a2430fSAndrzej Pietrasiewicz 
63800a2430fSAndrzej Pietrasiewicz 	hidg_hs_in_ep_desc.bEndpointAddress =
63900a2430fSAndrzej Pietrasiewicz 		hidg_fs_in_ep_desc.bEndpointAddress;
64000a2430fSAndrzej Pietrasiewicz 	hidg_hs_out_ep_desc.bEndpointAddress =
64100a2430fSAndrzej Pietrasiewicz 		hidg_fs_out_ep_desc.bEndpointAddress;
64200a2430fSAndrzej Pietrasiewicz 
64300a2430fSAndrzej Pietrasiewicz 	status = usb_assign_descriptors(f, hidg_fs_descriptors,
64400a2430fSAndrzej Pietrasiewicz 			hidg_hs_descriptors, NULL);
64500a2430fSAndrzej Pietrasiewicz 	if (status)
64600a2430fSAndrzej Pietrasiewicz 		goto fail;
64700a2430fSAndrzej Pietrasiewicz 
64800a2430fSAndrzej Pietrasiewicz 	mutex_init(&hidg->lock);
64900a2430fSAndrzej Pietrasiewicz 	spin_lock_init(&hidg->spinlock);
65000a2430fSAndrzej Pietrasiewicz 	init_waitqueue_head(&hidg->write_queue);
65100a2430fSAndrzej Pietrasiewicz 	init_waitqueue_head(&hidg->read_queue);
65200a2430fSAndrzej Pietrasiewicz 	INIT_LIST_HEAD(&hidg->completed_out_req);
65300a2430fSAndrzej Pietrasiewicz 
65400a2430fSAndrzej Pietrasiewicz 	/* create char device */
65500a2430fSAndrzej Pietrasiewicz 	cdev_init(&hidg->cdev, &f_hidg_fops);
65600a2430fSAndrzej Pietrasiewicz 	dev = MKDEV(major, hidg->minor);
65700a2430fSAndrzej Pietrasiewicz 	status = cdev_add(&hidg->cdev, dev, 1);
65800a2430fSAndrzej Pietrasiewicz 	if (status)
659d12a8727SPavitrakumar Managutte 		goto fail_free_descs;
66000a2430fSAndrzej Pietrasiewicz 
66163406087SAndrzej Pietrasiewicz 	device = device_create(hidg_class, NULL, dev, NULL,
66263406087SAndrzej Pietrasiewicz 			       "%s%d", "hidg", hidg->minor);
66363406087SAndrzej Pietrasiewicz 	if (IS_ERR(device)) {
66463406087SAndrzej Pietrasiewicz 		status = PTR_ERR(device);
66563406087SAndrzej Pietrasiewicz 		goto del;
66663406087SAndrzej Pietrasiewicz 	}
66700a2430fSAndrzej Pietrasiewicz 
66800a2430fSAndrzej Pietrasiewicz 	return 0;
66963406087SAndrzej Pietrasiewicz del:
67063406087SAndrzej Pietrasiewicz 	cdev_del(&hidg->cdev);
671d12a8727SPavitrakumar Managutte fail_free_descs:
672d12a8727SPavitrakumar Managutte 	usb_free_all_descriptors(f);
67300a2430fSAndrzej Pietrasiewicz fail:
67400a2430fSAndrzej Pietrasiewicz 	ERROR(f->config->cdev, "hidg_bind FAILED\n");
67500a2430fSAndrzej Pietrasiewicz 	if (hidg->req != NULL) {
67600a2430fSAndrzej Pietrasiewicz 		kfree(hidg->req->buf);
67700a2430fSAndrzej Pietrasiewicz 		if (hidg->in_ep != NULL)
67800a2430fSAndrzej Pietrasiewicz 			usb_ep_free_request(hidg->in_ep, hidg->req);
67900a2430fSAndrzej Pietrasiewicz 	}
68000a2430fSAndrzej Pietrasiewicz 
68100a2430fSAndrzej Pietrasiewicz 	return status;
68200a2430fSAndrzej Pietrasiewicz }
68300a2430fSAndrzej Pietrasiewicz 
684cb382536SAndrzej Pietrasiewicz static inline int hidg_get_minor(void)
685cb382536SAndrzej Pietrasiewicz {
686cb382536SAndrzej Pietrasiewicz 	int ret;
687cb382536SAndrzej Pietrasiewicz 
688cb382536SAndrzej Pietrasiewicz 	ret = ida_simple_get(&hidg_ida, 0, 0, GFP_KERNEL);
689cb382536SAndrzej Pietrasiewicz 
690cb382536SAndrzej Pietrasiewicz 	return ret;
691cb382536SAndrzej Pietrasiewicz }
692cb382536SAndrzej Pietrasiewicz 
69321a9476aSAndrzej Pietrasiewicz static inline struct f_hid_opts *to_f_hid_opts(struct config_item *item)
69421a9476aSAndrzej Pietrasiewicz {
69521a9476aSAndrzej Pietrasiewicz 	return container_of(to_config_group(item), struct f_hid_opts,
69621a9476aSAndrzej Pietrasiewicz 			    func_inst.group);
69721a9476aSAndrzej Pietrasiewicz }
69821a9476aSAndrzej Pietrasiewicz 
69921a9476aSAndrzej Pietrasiewicz CONFIGFS_ATTR_STRUCT(f_hid_opts);
70021a9476aSAndrzej Pietrasiewicz CONFIGFS_ATTR_OPS(f_hid_opts);
70121a9476aSAndrzej Pietrasiewicz 
70221a9476aSAndrzej Pietrasiewicz static void hid_attr_release(struct config_item *item)
70321a9476aSAndrzej Pietrasiewicz {
70421a9476aSAndrzej Pietrasiewicz 	struct f_hid_opts *opts = to_f_hid_opts(item);
70521a9476aSAndrzej Pietrasiewicz 
70621a9476aSAndrzej Pietrasiewicz 	usb_put_function_instance(&opts->func_inst);
70721a9476aSAndrzej Pietrasiewicz }
70821a9476aSAndrzej Pietrasiewicz 
70921a9476aSAndrzej Pietrasiewicz static struct configfs_item_operations hidg_item_ops = {
71021a9476aSAndrzej Pietrasiewicz 	.release	= hid_attr_release,
71121a9476aSAndrzej Pietrasiewicz 	.show_attribute	= f_hid_opts_attr_show,
71221a9476aSAndrzej Pietrasiewicz 	.store_attribute = f_hid_opts_attr_store,
71321a9476aSAndrzej Pietrasiewicz };
71421a9476aSAndrzej Pietrasiewicz 
71521a9476aSAndrzej Pietrasiewicz #define F_HID_OPT(name, prec, limit)					\
71621a9476aSAndrzej Pietrasiewicz static ssize_t f_hid_opts_##name##_show(struct f_hid_opts *opts, char *page)\
71721a9476aSAndrzej Pietrasiewicz {									\
71821a9476aSAndrzej Pietrasiewicz 	int result;							\
71921a9476aSAndrzej Pietrasiewicz 									\
72021a9476aSAndrzej Pietrasiewicz 	mutex_lock(&opts->lock);					\
72121a9476aSAndrzej Pietrasiewicz 	result = sprintf(page, "%d\n", opts->name);			\
72221a9476aSAndrzej Pietrasiewicz 	mutex_unlock(&opts->lock);					\
72321a9476aSAndrzej Pietrasiewicz 									\
72421a9476aSAndrzej Pietrasiewicz 	return result;							\
72521a9476aSAndrzej Pietrasiewicz }									\
72621a9476aSAndrzej Pietrasiewicz 									\
72721a9476aSAndrzej Pietrasiewicz static ssize_t f_hid_opts_##name##_store(struct f_hid_opts *opts,	\
72821a9476aSAndrzej Pietrasiewicz 					 const char *page, size_t len)	\
72921a9476aSAndrzej Pietrasiewicz {									\
73021a9476aSAndrzej Pietrasiewicz 	int ret;							\
73121a9476aSAndrzej Pietrasiewicz 	u##prec num;							\
73221a9476aSAndrzej Pietrasiewicz 									\
73321a9476aSAndrzej Pietrasiewicz 	mutex_lock(&opts->lock);					\
73421a9476aSAndrzej Pietrasiewicz 	if (opts->refcnt) {						\
73521a9476aSAndrzej Pietrasiewicz 		ret = -EBUSY;						\
73621a9476aSAndrzej Pietrasiewicz 		goto end;						\
73721a9476aSAndrzej Pietrasiewicz 	}								\
73821a9476aSAndrzej Pietrasiewicz 									\
73921a9476aSAndrzej Pietrasiewicz 	ret = kstrtou##prec(page, 0, &num);				\
74021a9476aSAndrzej Pietrasiewicz 	if (ret)							\
74121a9476aSAndrzej Pietrasiewicz 		goto end;						\
74221a9476aSAndrzej Pietrasiewicz 									\
74321a9476aSAndrzej Pietrasiewicz 	if (num > limit) {						\
74421a9476aSAndrzej Pietrasiewicz 		ret = -EINVAL;						\
74521a9476aSAndrzej Pietrasiewicz 		goto end;						\
74621a9476aSAndrzej Pietrasiewicz 	}								\
74721a9476aSAndrzej Pietrasiewicz 	opts->name = num;						\
74821a9476aSAndrzej Pietrasiewicz 	ret = len;							\
74921a9476aSAndrzej Pietrasiewicz 									\
75021a9476aSAndrzej Pietrasiewicz end:									\
75121a9476aSAndrzej Pietrasiewicz 	mutex_unlock(&opts->lock);					\
75221a9476aSAndrzej Pietrasiewicz 	return ret;							\
75321a9476aSAndrzej Pietrasiewicz }									\
75421a9476aSAndrzej Pietrasiewicz 									\
75521a9476aSAndrzej Pietrasiewicz static struct f_hid_opts_attribute f_hid_opts_##name =			\
75621a9476aSAndrzej Pietrasiewicz 	__CONFIGFS_ATTR(name, S_IRUGO | S_IWUSR, f_hid_opts_##name##_show,\
75721a9476aSAndrzej Pietrasiewicz 			f_hid_opts_##name##_store)
75821a9476aSAndrzej Pietrasiewicz 
75921a9476aSAndrzej Pietrasiewicz F_HID_OPT(subclass, 8, 255);
76021a9476aSAndrzej Pietrasiewicz F_HID_OPT(protocol, 8, 255);
76121a9476aSAndrzej Pietrasiewicz F_HID_OPT(report_length, 16, 65536);
76221a9476aSAndrzej Pietrasiewicz 
76321a9476aSAndrzej Pietrasiewicz static ssize_t f_hid_opts_report_desc_show(struct f_hid_opts *opts, char *page)
76421a9476aSAndrzej Pietrasiewicz {
76521a9476aSAndrzej Pietrasiewicz 	int result;
76621a9476aSAndrzej Pietrasiewicz 
76721a9476aSAndrzej Pietrasiewicz 	mutex_lock(&opts->lock);
76821a9476aSAndrzej Pietrasiewicz 	result = opts->report_desc_length;
76921a9476aSAndrzej Pietrasiewicz 	memcpy(page, opts->report_desc, opts->report_desc_length);
77021a9476aSAndrzej Pietrasiewicz 	mutex_unlock(&opts->lock);
77121a9476aSAndrzej Pietrasiewicz 
77221a9476aSAndrzej Pietrasiewicz 	return result;
77321a9476aSAndrzej Pietrasiewicz }
77421a9476aSAndrzej Pietrasiewicz 
77521a9476aSAndrzej Pietrasiewicz static ssize_t f_hid_opts_report_desc_store(struct f_hid_opts *opts,
77621a9476aSAndrzej Pietrasiewicz 					    const char *page, size_t len)
77721a9476aSAndrzej Pietrasiewicz {
77821a9476aSAndrzej Pietrasiewicz 	int ret = -EBUSY;
77921a9476aSAndrzej Pietrasiewicz 	char *d;
78021a9476aSAndrzej Pietrasiewicz 
78121a9476aSAndrzej Pietrasiewicz 	mutex_lock(&opts->lock);
78221a9476aSAndrzej Pietrasiewicz 
78321a9476aSAndrzej Pietrasiewicz 	if (opts->refcnt)
78421a9476aSAndrzej Pietrasiewicz 		goto end;
78521a9476aSAndrzej Pietrasiewicz 	if (len > PAGE_SIZE) {
78621a9476aSAndrzej Pietrasiewicz 		ret = -ENOSPC;
78721a9476aSAndrzej Pietrasiewicz 		goto end;
78821a9476aSAndrzej Pietrasiewicz 	}
78921a9476aSAndrzej Pietrasiewicz 	d = kmemdup(page, len, GFP_KERNEL);
79021a9476aSAndrzej Pietrasiewicz 	if (!d) {
79121a9476aSAndrzej Pietrasiewicz 		ret = -ENOMEM;
79221a9476aSAndrzej Pietrasiewicz 		goto end;
79321a9476aSAndrzej Pietrasiewicz 	}
79421a9476aSAndrzej Pietrasiewicz 	kfree(opts->report_desc);
79521a9476aSAndrzej Pietrasiewicz 	opts->report_desc = d;
79621a9476aSAndrzej Pietrasiewicz 	opts->report_desc_length = len;
79721a9476aSAndrzej Pietrasiewicz 	opts->report_desc_alloc = true;
79821a9476aSAndrzej Pietrasiewicz 	ret = len;
79921a9476aSAndrzej Pietrasiewicz end:
80021a9476aSAndrzej Pietrasiewicz 	mutex_unlock(&opts->lock);
80121a9476aSAndrzej Pietrasiewicz 	return ret;
80221a9476aSAndrzej Pietrasiewicz }
80321a9476aSAndrzej Pietrasiewicz 
80421a9476aSAndrzej Pietrasiewicz static struct f_hid_opts_attribute f_hid_opts_report_desc =
80521a9476aSAndrzej Pietrasiewicz 	__CONFIGFS_ATTR(report_desc, S_IRUGO | S_IWUSR,
80621a9476aSAndrzej Pietrasiewicz 			f_hid_opts_report_desc_show,
80721a9476aSAndrzej Pietrasiewicz 			f_hid_opts_report_desc_store);
80821a9476aSAndrzej Pietrasiewicz 
80921a9476aSAndrzej Pietrasiewicz static struct configfs_attribute *hid_attrs[] = {
81021a9476aSAndrzej Pietrasiewicz 	&f_hid_opts_subclass.attr,
81121a9476aSAndrzej Pietrasiewicz 	&f_hid_opts_protocol.attr,
81221a9476aSAndrzej Pietrasiewicz 	&f_hid_opts_report_length.attr,
81321a9476aSAndrzej Pietrasiewicz 	&f_hid_opts_report_desc.attr,
81421a9476aSAndrzej Pietrasiewicz 	NULL,
81521a9476aSAndrzej Pietrasiewicz };
81621a9476aSAndrzej Pietrasiewicz 
81721a9476aSAndrzej Pietrasiewicz static struct config_item_type hid_func_type = {
81821a9476aSAndrzej Pietrasiewicz 	.ct_item_ops	= &hidg_item_ops,
81921a9476aSAndrzej Pietrasiewicz 	.ct_attrs	= hid_attrs,
82021a9476aSAndrzej Pietrasiewicz 	.ct_owner	= THIS_MODULE,
82121a9476aSAndrzej Pietrasiewicz };
82221a9476aSAndrzej Pietrasiewicz 
823cb382536SAndrzej Pietrasiewicz static inline void hidg_put_minor(int minor)
824cb382536SAndrzej Pietrasiewicz {
825cb382536SAndrzej Pietrasiewicz 	ida_simple_remove(&hidg_ida, minor);
826cb382536SAndrzej Pietrasiewicz }
827cb382536SAndrzej Pietrasiewicz 
828cb382536SAndrzej Pietrasiewicz static void hidg_free_inst(struct usb_function_instance *f)
829cb382536SAndrzej Pietrasiewicz {
830cb382536SAndrzej Pietrasiewicz 	struct f_hid_opts *opts;
831cb382536SAndrzej Pietrasiewicz 
832cb382536SAndrzej Pietrasiewicz 	opts = container_of(f, struct f_hid_opts, func_inst);
833cb382536SAndrzej Pietrasiewicz 
834cb382536SAndrzej Pietrasiewicz 	mutex_lock(&hidg_ida_lock);
835cb382536SAndrzej Pietrasiewicz 
836cb382536SAndrzej Pietrasiewicz 	hidg_put_minor(opts->minor);
837cb382536SAndrzej Pietrasiewicz 	if (idr_is_empty(&hidg_ida.idr))
838cb382536SAndrzej Pietrasiewicz 		ghid_cleanup();
839cb382536SAndrzej Pietrasiewicz 
840cb382536SAndrzej Pietrasiewicz 	mutex_unlock(&hidg_ida_lock);
841cb382536SAndrzej Pietrasiewicz 
842cb382536SAndrzej Pietrasiewicz 	if (opts->report_desc_alloc)
843cb382536SAndrzej Pietrasiewicz 		kfree(opts->report_desc);
844cb382536SAndrzej Pietrasiewicz 
845cb382536SAndrzej Pietrasiewicz 	kfree(opts);
846cb382536SAndrzej Pietrasiewicz }
847cb382536SAndrzej Pietrasiewicz 
848cb382536SAndrzej Pietrasiewicz static struct usb_function_instance *hidg_alloc_inst(void)
849cb382536SAndrzej Pietrasiewicz {
850cb382536SAndrzej Pietrasiewicz 	struct f_hid_opts *opts;
851cb382536SAndrzej Pietrasiewicz 	struct usb_function_instance *ret;
852cb382536SAndrzej Pietrasiewicz 	int status = 0;
853cb382536SAndrzej Pietrasiewicz 
854cb382536SAndrzej Pietrasiewicz 	opts = kzalloc(sizeof(*opts), GFP_KERNEL);
855cb382536SAndrzej Pietrasiewicz 	if (!opts)
856cb382536SAndrzej Pietrasiewicz 		return ERR_PTR(-ENOMEM);
85721a9476aSAndrzej Pietrasiewicz 	mutex_init(&opts->lock);
858cb382536SAndrzej Pietrasiewicz 	opts->func_inst.free_func_inst = hidg_free_inst;
859cb382536SAndrzej Pietrasiewicz 	ret = &opts->func_inst;
860cb382536SAndrzej Pietrasiewicz 
861cb382536SAndrzej Pietrasiewicz 	mutex_lock(&hidg_ida_lock);
862cb382536SAndrzej Pietrasiewicz 
863cb382536SAndrzej Pietrasiewicz 	if (idr_is_empty(&hidg_ida.idr)) {
864cb382536SAndrzej Pietrasiewicz 		status = ghid_setup(NULL, HIDG_MINORS);
865cb382536SAndrzej Pietrasiewicz 		if (status)  {
866cb382536SAndrzej Pietrasiewicz 			ret = ERR_PTR(status);
867cb382536SAndrzej Pietrasiewicz 			kfree(opts);
868cb382536SAndrzej Pietrasiewicz 			goto unlock;
869cb382536SAndrzej Pietrasiewicz 		}
870cb382536SAndrzej Pietrasiewicz 	}
871cb382536SAndrzej Pietrasiewicz 
872cb382536SAndrzej Pietrasiewicz 	opts->minor = hidg_get_minor();
873cb382536SAndrzej Pietrasiewicz 	if (opts->minor < 0) {
874cb382536SAndrzej Pietrasiewicz 		ret = ERR_PTR(opts->minor);
875cb382536SAndrzej Pietrasiewicz 		kfree(opts);
876cb382536SAndrzej Pietrasiewicz 		if (idr_is_empty(&hidg_ida.idr))
877cb382536SAndrzej Pietrasiewicz 			ghid_cleanup();
878*828f6148SDan Carpenter 		goto unlock;
879cb382536SAndrzej Pietrasiewicz 	}
88021a9476aSAndrzej Pietrasiewicz 	config_group_init_type_name(&opts->func_inst.group, "", &hid_func_type);
881cb382536SAndrzej Pietrasiewicz 
882cb382536SAndrzej Pietrasiewicz unlock:
883cb382536SAndrzej Pietrasiewicz 	mutex_unlock(&hidg_ida_lock);
884cb382536SAndrzej Pietrasiewicz 	return ret;
885cb382536SAndrzej Pietrasiewicz }
886cb382536SAndrzej Pietrasiewicz 
887cb382536SAndrzej Pietrasiewicz static void hidg_free(struct usb_function *f)
888cb382536SAndrzej Pietrasiewicz {
889cb382536SAndrzej Pietrasiewicz 	struct f_hidg *hidg;
89021a9476aSAndrzej Pietrasiewicz 	struct f_hid_opts *opts;
891cb382536SAndrzej Pietrasiewicz 
892cb382536SAndrzej Pietrasiewicz 	hidg = func_to_hidg(f);
89321a9476aSAndrzej Pietrasiewicz 	opts = container_of(f->fi, struct f_hid_opts, func_inst);
894cb382536SAndrzej Pietrasiewicz 	kfree(hidg->report_desc);
895cb382536SAndrzej Pietrasiewicz 	kfree(hidg);
89621a9476aSAndrzej Pietrasiewicz 	mutex_lock(&opts->lock);
89721a9476aSAndrzej Pietrasiewicz 	--opts->refcnt;
89821a9476aSAndrzej Pietrasiewicz 	mutex_unlock(&opts->lock);
899cb382536SAndrzej Pietrasiewicz }
900cb382536SAndrzej Pietrasiewicz 
901cb382536SAndrzej Pietrasiewicz static void hidg_unbind(struct usb_configuration *c, struct usb_function *f)
902cb382536SAndrzej Pietrasiewicz {
903cb382536SAndrzej Pietrasiewicz 	struct f_hidg *hidg = func_to_hidg(f);
904cb382536SAndrzej Pietrasiewicz 
905cb382536SAndrzej Pietrasiewicz 	device_destroy(hidg_class, MKDEV(major, hidg->minor));
906cb382536SAndrzej Pietrasiewicz 	cdev_del(&hidg->cdev);
907cb382536SAndrzej Pietrasiewicz 
908cb382536SAndrzej Pietrasiewicz 	/* disable/free request and end point */
909cb382536SAndrzej Pietrasiewicz 	usb_ep_disable(hidg->in_ep);
910cb382536SAndrzej Pietrasiewicz 	usb_ep_dequeue(hidg->in_ep, hidg->req);
911cb382536SAndrzej Pietrasiewicz 	kfree(hidg->req->buf);
912cb382536SAndrzej Pietrasiewicz 	usb_ep_free_request(hidg->in_ep, hidg->req);
913cb382536SAndrzej Pietrasiewicz 
914cb382536SAndrzej Pietrasiewicz 	usb_free_all_descriptors(f);
915cb382536SAndrzej Pietrasiewicz }
916cb382536SAndrzej Pietrasiewicz 
9170fc57ea0SFengguang Wu static struct usb_function *hidg_alloc(struct usb_function_instance *fi)
918cb382536SAndrzej Pietrasiewicz {
919cb382536SAndrzej Pietrasiewicz 	struct f_hidg *hidg;
920cb382536SAndrzej Pietrasiewicz 	struct f_hid_opts *opts;
921cb382536SAndrzej Pietrasiewicz 
922cb382536SAndrzej Pietrasiewicz 	/* allocate and initialize one new instance */
923cb382536SAndrzej Pietrasiewicz 	hidg = kzalloc(sizeof(*hidg), GFP_KERNEL);
924cb382536SAndrzej Pietrasiewicz 	if (!hidg)
925cb382536SAndrzej Pietrasiewicz 		return ERR_PTR(-ENOMEM);
926cb382536SAndrzej Pietrasiewicz 
927cb382536SAndrzej Pietrasiewicz 	opts = container_of(fi, struct f_hid_opts, func_inst);
928cb382536SAndrzej Pietrasiewicz 
92921a9476aSAndrzej Pietrasiewicz 	mutex_lock(&opts->lock);
93021a9476aSAndrzej Pietrasiewicz 	++opts->refcnt;
93121a9476aSAndrzej Pietrasiewicz 
932cb382536SAndrzej Pietrasiewicz 	hidg->minor = opts->minor;
933cb382536SAndrzej Pietrasiewicz 	hidg->bInterfaceSubClass = opts->subclass;
934cb382536SAndrzej Pietrasiewicz 	hidg->bInterfaceProtocol = opts->protocol;
935cb382536SAndrzej Pietrasiewicz 	hidg->report_length = opts->report_length;
936cb382536SAndrzej Pietrasiewicz 	hidg->report_desc_length = opts->report_desc_length;
937cb382536SAndrzej Pietrasiewicz 	if (opts->report_desc) {
938cb382536SAndrzej Pietrasiewicz 		hidg->report_desc = kmemdup(opts->report_desc,
939cb382536SAndrzej Pietrasiewicz 					    opts->report_desc_length,
940cb382536SAndrzej Pietrasiewicz 					    GFP_KERNEL);
941cb382536SAndrzej Pietrasiewicz 		if (!hidg->report_desc) {
942cb382536SAndrzej Pietrasiewicz 			kfree(hidg);
94321a9476aSAndrzej Pietrasiewicz 			mutex_unlock(&opts->lock);
944cb382536SAndrzej Pietrasiewicz 			return ERR_PTR(-ENOMEM);
945cb382536SAndrzej Pietrasiewicz 		}
946cb382536SAndrzej Pietrasiewicz 	}
947cb382536SAndrzej Pietrasiewicz 
94821a9476aSAndrzej Pietrasiewicz 	mutex_unlock(&opts->lock);
94921a9476aSAndrzej Pietrasiewicz 
950cb382536SAndrzej Pietrasiewicz 	hidg->func.name    = "hid";
951cb382536SAndrzej Pietrasiewicz 	hidg->func.bind    = hidg_bind;
952cb382536SAndrzej Pietrasiewicz 	hidg->func.unbind  = hidg_unbind;
953cb382536SAndrzej Pietrasiewicz 	hidg->func.set_alt = hidg_set_alt;
954cb382536SAndrzej Pietrasiewicz 	hidg->func.disable = hidg_disable;
955cb382536SAndrzej Pietrasiewicz 	hidg->func.setup   = hidg_setup;
956cb382536SAndrzej Pietrasiewicz 	hidg->func.free_func = hidg_free;
957cb382536SAndrzej Pietrasiewicz 
958cb382536SAndrzej Pietrasiewicz 	/* this could me made configurable at some point */
959cb382536SAndrzej Pietrasiewicz 	hidg->qlen	   = 4;
960cb382536SAndrzej Pietrasiewicz 
961cb382536SAndrzej Pietrasiewicz 	return &hidg->func;
962cb382536SAndrzej Pietrasiewicz }
963cb382536SAndrzej Pietrasiewicz 
964cb382536SAndrzej Pietrasiewicz DECLARE_USB_FUNCTION_INIT(hid, hidg_alloc_inst, hidg_alloc);
965cb382536SAndrzej Pietrasiewicz MODULE_LICENSE("GPL");
966cb382536SAndrzej Pietrasiewicz MODULE_AUTHOR("Fabien Chouteau");
967cb382536SAndrzej Pietrasiewicz 
968cb382536SAndrzej Pietrasiewicz int ghid_setup(struct usb_gadget *g, int count)
96900a2430fSAndrzej Pietrasiewicz {
97000a2430fSAndrzej Pietrasiewicz 	int status;
97100a2430fSAndrzej Pietrasiewicz 	dev_t dev;
97200a2430fSAndrzej Pietrasiewicz 
97300a2430fSAndrzej Pietrasiewicz 	hidg_class = class_create(THIS_MODULE, "hidg");
97406529407SAndrzej Pietrasiewicz 	if (IS_ERR(hidg_class)) {
97506529407SAndrzej Pietrasiewicz 		hidg_class = NULL;
97606529407SAndrzej Pietrasiewicz 		return PTR_ERR(hidg_class);
97706529407SAndrzej Pietrasiewicz 	}
97800a2430fSAndrzej Pietrasiewicz 
97900a2430fSAndrzej Pietrasiewicz 	status = alloc_chrdev_region(&dev, 0, count, "hidg");
98000a2430fSAndrzej Pietrasiewicz 	if (!status) {
98100a2430fSAndrzej Pietrasiewicz 		major = MAJOR(dev);
98200a2430fSAndrzej Pietrasiewicz 		minors = count;
98300a2430fSAndrzej Pietrasiewicz 	}
98400a2430fSAndrzej Pietrasiewicz 
98500a2430fSAndrzej Pietrasiewicz 	return status;
98600a2430fSAndrzej Pietrasiewicz }
98700a2430fSAndrzej Pietrasiewicz 
98800a2430fSAndrzej Pietrasiewicz void ghid_cleanup(void)
98900a2430fSAndrzej Pietrasiewicz {
99000a2430fSAndrzej Pietrasiewicz 	if (major) {
99100a2430fSAndrzej Pietrasiewicz 		unregister_chrdev_region(MKDEV(major, 0), minors);
99200a2430fSAndrzej Pietrasiewicz 		major = minors = 0;
99300a2430fSAndrzej Pietrasiewicz 	}
99400a2430fSAndrzej Pietrasiewicz 
99500a2430fSAndrzej Pietrasiewicz 	class_destroy(hidg_class);
99600a2430fSAndrzej Pietrasiewicz 	hidg_class = NULL;
99700a2430fSAndrzej Pietrasiewicz }
998