xref: /openbmc/linux/drivers/usb/gadget/function/f_hid.c (revision da4e527cd8850712bb705f4c41f0839705ab7c98)
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 
402c9b3bde0SJulia Lawall 	VDBG(cdev,
403c9b3bde0SJulia Lawall 	     "%s crtl_request : bRequestType:0x%x bRequest:0x%x Value:0x%x\n",
404c9b3bde0SJulia Lawall 	     __func__, ctrl->bRequestType, ctrl->bRequest, value);
40500a2430fSAndrzej Pietrasiewicz 
40600a2430fSAndrzej Pietrasiewicz 	switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
40700a2430fSAndrzej Pietrasiewicz 	case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
40800a2430fSAndrzej Pietrasiewicz 		  | HID_REQ_GET_REPORT):
40900a2430fSAndrzej Pietrasiewicz 		VDBG(cdev, "get_report\n");
41000a2430fSAndrzej Pietrasiewicz 
41100a2430fSAndrzej Pietrasiewicz 		/* send an empty report */
41200a2430fSAndrzej Pietrasiewicz 		length = min_t(unsigned, length, hidg->report_length);
41300a2430fSAndrzej Pietrasiewicz 		memset(req->buf, 0x0, length);
41400a2430fSAndrzej Pietrasiewicz 
41500a2430fSAndrzej Pietrasiewicz 		goto respond;
41600a2430fSAndrzej Pietrasiewicz 		break;
41700a2430fSAndrzej Pietrasiewicz 
41800a2430fSAndrzej Pietrasiewicz 	case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
41900a2430fSAndrzej Pietrasiewicz 		  | HID_REQ_GET_PROTOCOL):
42000a2430fSAndrzej Pietrasiewicz 		VDBG(cdev, "get_protocol\n");
42100a2430fSAndrzej Pietrasiewicz 		goto stall;
42200a2430fSAndrzej Pietrasiewicz 		break;
42300a2430fSAndrzej Pietrasiewicz 
42400a2430fSAndrzej Pietrasiewicz 	case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
42500a2430fSAndrzej Pietrasiewicz 		  | HID_REQ_SET_REPORT):
4266774def6SMasanari Iida 		VDBG(cdev, "set_report | wLength=%d\n", ctrl->wLength);
42700a2430fSAndrzej Pietrasiewicz 		goto stall;
42800a2430fSAndrzej Pietrasiewicz 		break;
42900a2430fSAndrzej Pietrasiewicz 
43000a2430fSAndrzej Pietrasiewicz 	case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
43100a2430fSAndrzej Pietrasiewicz 		  | HID_REQ_SET_PROTOCOL):
43200a2430fSAndrzej Pietrasiewicz 		VDBG(cdev, "set_protocol\n");
43300a2430fSAndrzej Pietrasiewicz 		goto stall;
43400a2430fSAndrzej Pietrasiewicz 		break;
43500a2430fSAndrzej Pietrasiewicz 
43600a2430fSAndrzej Pietrasiewicz 	case ((USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_INTERFACE) << 8
43700a2430fSAndrzej Pietrasiewicz 		  | USB_REQ_GET_DESCRIPTOR):
43800a2430fSAndrzej Pietrasiewicz 		switch (value >> 8) {
43900a2430fSAndrzej Pietrasiewicz 		case HID_DT_HID:
440f286d487SKrzysztof Opasiak 		{
441f286d487SKrzysztof Opasiak 			struct hid_descriptor hidg_desc_copy = hidg_desc;
442f286d487SKrzysztof Opasiak 
44300a2430fSAndrzej Pietrasiewicz 			VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: HID\n");
444f286d487SKrzysztof Opasiak 			hidg_desc_copy.desc[0].bDescriptorType = HID_DT_REPORT;
445f286d487SKrzysztof Opasiak 			hidg_desc_copy.desc[0].wDescriptorLength =
446f286d487SKrzysztof Opasiak 				cpu_to_le16(hidg->report_desc_length);
447f286d487SKrzysztof Opasiak 
44800a2430fSAndrzej Pietrasiewicz 			length = min_t(unsigned short, length,
449f286d487SKrzysztof Opasiak 						   hidg_desc_copy.bLength);
450f286d487SKrzysztof Opasiak 			memcpy(req->buf, &hidg_desc_copy, length);
45100a2430fSAndrzej Pietrasiewicz 			goto respond;
45200a2430fSAndrzej Pietrasiewicz 			break;
453f286d487SKrzysztof Opasiak 		}
45400a2430fSAndrzej Pietrasiewicz 		case HID_DT_REPORT:
45500a2430fSAndrzej Pietrasiewicz 			VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: REPORT\n");
45600a2430fSAndrzej Pietrasiewicz 			length = min_t(unsigned short, length,
45700a2430fSAndrzej Pietrasiewicz 						   hidg->report_desc_length);
45800a2430fSAndrzej Pietrasiewicz 			memcpy(req->buf, hidg->report_desc, length);
45900a2430fSAndrzej Pietrasiewicz 			goto respond;
46000a2430fSAndrzej Pietrasiewicz 			break;
46100a2430fSAndrzej Pietrasiewicz 
46200a2430fSAndrzej Pietrasiewicz 		default:
46300a2430fSAndrzej Pietrasiewicz 			VDBG(cdev, "Unknown descriptor request 0x%x\n",
46400a2430fSAndrzej Pietrasiewicz 				 value >> 8);
46500a2430fSAndrzej Pietrasiewicz 			goto stall;
46600a2430fSAndrzej Pietrasiewicz 			break;
46700a2430fSAndrzej Pietrasiewicz 		}
46800a2430fSAndrzej Pietrasiewicz 		break;
46900a2430fSAndrzej Pietrasiewicz 
47000a2430fSAndrzej Pietrasiewicz 	default:
47100a2430fSAndrzej Pietrasiewicz 		VDBG(cdev, "Unknown request 0x%x\n",
47200a2430fSAndrzej Pietrasiewicz 			 ctrl->bRequest);
47300a2430fSAndrzej Pietrasiewicz 		goto stall;
47400a2430fSAndrzej Pietrasiewicz 		break;
47500a2430fSAndrzej Pietrasiewicz 	}
47600a2430fSAndrzej Pietrasiewicz 
47700a2430fSAndrzej Pietrasiewicz stall:
47800a2430fSAndrzej Pietrasiewicz 	return -EOPNOTSUPP;
47900a2430fSAndrzej Pietrasiewicz 
48000a2430fSAndrzej Pietrasiewicz respond:
48100a2430fSAndrzej Pietrasiewicz 	req->zero = 0;
48200a2430fSAndrzej Pietrasiewicz 	req->length = length;
48300a2430fSAndrzej Pietrasiewicz 	status = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
48400a2430fSAndrzej Pietrasiewicz 	if (status < 0)
48500a2430fSAndrzej Pietrasiewicz 		ERROR(cdev, "usb_ep_queue error on ep0 %d\n", value);
48600a2430fSAndrzej Pietrasiewicz 	return status;
48700a2430fSAndrzej Pietrasiewicz }
48800a2430fSAndrzej Pietrasiewicz 
48900a2430fSAndrzej Pietrasiewicz static void hidg_disable(struct usb_function *f)
49000a2430fSAndrzej Pietrasiewicz {
49100a2430fSAndrzej Pietrasiewicz 	struct f_hidg *hidg = func_to_hidg(f);
49200a2430fSAndrzej Pietrasiewicz 	struct f_hidg_req_list *list, *next;
49300a2430fSAndrzej Pietrasiewicz 
49400a2430fSAndrzej Pietrasiewicz 	usb_ep_disable(hidg->in_ep);
49500a2430fSAndrzej Pietrasiewicz 	hidg->in_ep->driver_data = NULL;
49600a2430fSAndrzej Pietrasiewicz 
49700a2430fSAndrzej Pietrasiewicz 	usb_ep_disable(hidg->out_ep);
49800a2430fSAndrzej Pietrasiewicz 	hidg->out_ep->driver_data = NULL;
49900a2430fSAndrzej Pietrasiewicz 
50000a2430fSAndrzej Pietrasiewicz 	list_for_each_entry_safe(list, next, &hidg->completed_out_req, list) {
50100a2430fSAndrzej Pietrasiewicz 		list_del(&list->list);
50200a2430fSAndrzej Pietrasiewicz 		kfree(list);
50300a2430fSAndrzej Pietrasiewicz 	}
50400a2430fSAndrzej Pietrasiewicz }
50500a2430fSAndrzej Pietrasiewicz 
50600a2430fSAndrzej Pietrasiewicz static int hidg_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
50700a2430fSAndrzej Pietrasiewicz {
50800a2430fSAndrzej Pietrasiewicz 	struct usb_composite_dev		*cdev = f->config->cdev;
50900a2430fSAndrzej Pietrasiewicz 	struct f_hidg				*hidg = func_to_hidg(f);
51000a2430fSAndrzej Pietrasiewicz 	int i, status = 0;
51100a2430fSAndrzej Pietrasiewicz 
51200a2430fSAndrzej Pietrasiewicz 	VDBG(cdev, "hidg_set_alt intf:%d alt:%d\n", intf, alt);
51300a2430fSAndrzej Pietrasiewicz 
51400a2430fSAndrzej Pietrasiewicz 	if (hidg->in_ep != NULL) {
51500a2430fSAndrzej Pietrasiewicz 		/* restart endpoint */
51600a2430fSAndrzej Pietrasiewicz 		if (hidg->in_ep->driver_data != NULL)
51700a2430fSAndrzej Pietrasiewicz 			usb_ep_disable(hidg->in_ep);
51800a2430fSAndrzej Pietrasiewicz 
51900a2430fSAndrzej Pietrasiewicz 		status = config_ep_by_speed(f->config->cdev->gadget, f,
52000a2430fSAndrzej Pietrasiewicz 					    hidg->in_ep);
52100a2430fSAndrzej Pietrasiewicz 		if (status) {
52200a2430fSAndrzej Pietrasiewicz 			ERROR(cdev, "config_ep_by_speed FAILED!\n");
52300a2430fSAndrzej Pietrasiewicz 			goto fail;
52400a2430fSAndrzej Pietrasiewicz 		}
52500a2430fSAndrzej Pietrasiewicz 		status = usb_ep_enable(hidg->in_ep);
52600a2430fSAndrzej Pietrasiewicz 		if (status < 0) {
52700a2430fSAndrzej Pietrasiewicz 			ERROR(cdev, "Enable IN endpoint FAILED!\n");
52800a2430fSAndrzej Pietrasiewicz 			goto fail;
52900a2430fSAndrzej Pietrasiewicz 		}
53000a2430fSAndrzej Pietrasiewicz 		hidg->in_ep->driver_data = hidg;
53100a2430fSAndrzej Pietrasiewicz 	}
53200a2430fSAndrzej Pietrasiewicz 
53300a2430fSAndrzej Pietrasiewicz 
53400a2430fSAndrzej Pietrasiewicz 	if (hidg->out_ep != NULL) {
53500a2430fSAndrzej Pietrasiewicz 		/* restart endpoint */
53600a2430fSAndrzej Pietrasiewicz 		if (hidg->out_ep->driver_data != NULL)
53700a2430fSAndrzej Pietrasiewicz 			usb_ep_disable(hidg->out_ep);
53800a2430fSAndrzej Pietrasiewicz 
53900a2430fSAndrzej Pietrasiewicz 		status = config_ep_by_speed(f->config->cdev->gadget, f,
54000a2430fSAndrzej Pietrasiewicz 					    hidg->out_ep);
54100a2430fSAndrzej Pietrasiewicz 		if (status) {
54200a2430fSAndrzej Pietrasiewicz 			ERROR(cdev, "config_ep_by_speed FAILED!\n");
54300a2430fSAndrzej Pietrasiewicz 			goto fail;
54400a2430fSAndrzej Pietrasiewicz 		}
54500a2430fSAndrzej Pietrasiewicz 		status = usb_ep_enable(hidg->out_ep);
54600a2430fSAndrzej Pietrasiewicz 		if (status < 0) {
54700a2430fSAndrzej Pietrasiewicz 			ERROR(cdev, "Enable IN endpoint FAILED!\n");
54800a2430fSAndrzej Pietrasiewicz 			goto fail;
54900a2430fSAndrzej Pietrasiewicz 		}
55000a2430fSAndrzej Pietrasiewicz 		hidg->out_ep->driver_data = hidg;
55100a2430fSAndrzej Pietrasiewicz 
55200a2430fSAndrzej Pietrasiewicz 		/*
55300a2430fSAndrzej Pietrasiewicz 		 * allocate a bunch of read buffers and queue them all at once.
55400a2430fSAndrzej Pietrasiewicz 		 */
55500a2430fSAndrzej Pietrasiewicz 		for (i = 0; i < hidg->qlen && status == 0; i++) {
55600a2430fSAndrzej Pietrasiewicz 			struct usb_request *req =
55700a2430fSAndrzej Pietrasiewicz 					hidg_alloc_ep_req(hidg->out_ep,
55800a2430fSAndrzej Pietrasiewicz 							  hidg->report_length);
55900a2430fSAndrzej Pietrasiewicz 			if (req) {
56000a2430fSAndrzej Pietrasiewicz 				req->complete = hidg_set_report_complete;
56100a2430fSAndrzej Pietrasiewicz 				req->context  = hidg;
56200a2430fSAndrzej Pietrasiewicz 				status = usb_ep_queue(hidg->out_ep, req,
56300a2430fSAndrzej Pietrasiewicz 						      GFP_ATOMIC);
56400a2430fSAndrzej Pietrasiewicz 				if (status)
56500a2430fSAndrzej Pietrasiewicz 					ERROR(cdev, "%s queue req --> %d\n",
56600a2430fSAndrzej Pietrasiewicz 						hidg->out_ep->name, status);
56700a2430fSAndrzej Pietrasiewicz 			} else {
56800a2430fSAndrzej Pietrasiewicz 				usb_ep_disable(hidg->out_ep);
56900a2430fSAndrzej Pietrasiewicz 				hidg->out_ep->driver_data = NULL;
57000a2430fSAndrzej Pietrasiewicz 				status = -ENOMEM;
57100a2430fSAndrzej Pietrasiewicz 				goto fail;
57200a2430fSAndrzej Pietrasiewicz 			}
57300a2430fSAndrzej Pietrasiewicz 		}
57400a2430fSAndrzej Pietrasiewicz 	}
57500a2430fSAndrzej Pietrasiewicz 
57600a2430fSAndrzej Pietrasiewicz fail:
57700a2430fSAndrzej Pietrasiewicz 	return status;
57800a2430fSAndrzej Pietrasiewicz }
57900a2430fSAndrzej Pietrasiewicz 
5807a3cc461SLad, Prabhakar static const struct file_operations f_hidg_fops = {
58100a2430fSAndrzej Pietrasiewicz 	.owner		= THIS_MODULE,
58200a2430fSAndrzej Pietrasiewicz 	.open		= f_hidg_open,
58300a2430fSAndrzej Pietrasiewicz 	.release	= f_hidg_release,
58400a2430fSAndrzej Pietrasiewicz 	.write		= f_hidg_write,
58500a2430fSAndrzej Pietrasiewicz 	.read		= f_hidg_read,
58600a2430fSAndrzej Pietrasiewicz 	.poll		= f_hidg_poll,
58700a2430fSAndrzej Pietrasiewicz 	.llseek		= noop_llseek,
58800a2430fSAndrzej Pietrasiewicz };
58900a2430fSAndrzej Pietrasiewicz 
590cb382536SAndrzej Pietrasiewicz static int hidg_bind(struct usb_configuration *c, struct usb_function *f)
59100a2430fSAndrzej Pietrasiewicz {
59200a2430fSAndrzej Pietrasiewicz 	struct usb_ep		*ep;
59300a2430fSAndrzej Pietrasiewicz 	struct f_hidg		*hidg = func_to_hidg(f);
5945ca8d3ecSAndrzej Pietrasiewicz 	struct usb_string	*us;
59563406087SAndrzej Pietrasiewicz 	struct device		*device;
59600a2430fSAndrzej Pietrasiewicz 	int			status;
59700a2430fSAndrzej Pietrasiewicz 	dev_t			dev;
59800a2430fSAndrzej Pietrasiewicz 
599cb382536SAndrzej Pietrasiewicz 	/* maybe allocate device-global string IDs, and patch descriptors */
6005ca8d3ecSAndrzej Pietrasiewicz 	us = usb_gstrings_attach(c->cdev, ct_func_strings,
6015ca8d3ecSAndrzej Pietrasiewicz 				 ARRAY_SIZE(ct_func_string_defs));
6025ca8d3ecSAndrzej Pietrasiewicz 	if (IS_ERR(us))
6035ca8d3ecSAndrzej Pietrasiewicz 		return PTR_ERR(us);
6045ca8d3ecSAndrzej Pietrasiewicz 	hidg_interface_desc.iInterface = us[CT_FUNC_HID_IDX].id;
605cb382536SAndrzej Pietrasiewicz 
60600a2430fSAndrzej Pietrasiewicz 	/* allocate instance-specific interface IDs, and patch descriptors */
60700a2430fSAndrzej Pietrasiewicz 	status = usb_interface_id(c, f);
60800a2430fSAndrzej Pietrasiewicz 	if (status < 0)
60900a2430fSAndrzej Pietrasiewicz 		goto fail;
61000a2430fSAndrzej Pietrasiewicz 	hidg_interface_desc.bInterfaceNumber = status;
61100a2430fSAndrzej Pietrasiewicz 
61200a2430fSAndrzej Pietrasiewicz 	/* allocate instance-specific endpoints */
61300a2430fSAndrzej Pietrasiewicz 	status = -ENODEV;
61400a2430fSAndrzej Pietrasiewicz 	ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_in_ep_desc);
61500a2430fSAndrzej Pietrasiewicz 	if (!ep)
61600a2430fSAndrzej Pietrasiewicz 		goto fail;
61700a2430fSAndrzej Pietrasiewicz 	ep->driver_data = c->cdev;	/* claim */
61800a2430fSAndrzej Pietrasiewicz 	hidg->in_ep = ep;
61900a2430fSAndrzej Pietrasiewicz 
62000a2430fSAndrzej Pietrasiewicz 	ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_out_ep_desc);
62100a2430fSAndrzej Pietrasiewicz 	if (!ep)
62200a2430fSAndrzej Pietrasiewicz 		goto fail;
62300a2430fSAndrzej Pietrasiewicz 	ep->driver_data = c->cdev;	/* claim */
62400a2430fSAndrzej Pietrasiewicz 	hidg->out_ep = ep;
62500a2430fSAndrzej Pietrasiewicz 
62600a2430fSAndrzej Pietrasiewicz 	/* preallocate request and buffer */
62700a2430fSAndrzej Pietrasiewicz 	status = -ENOMEM;
62800a2430fSAndrzej Pietrasiewicz 	hidg->req = usb_ep_alloc_request(hidg->in_ep, GFP_KERNEL);
62900a2430fSAndrzej Pietrasiewicz 	if (!hidg->req)
63000a2430fSAndrzej Pietrasiewicz 		goto fail;
63100a2430fSAndrzej Pietrasiewicz 
63200a2430fSAndrzej Pietrasiewicz 	hidg->req->buf = kmalloc(hidg->report_length, GFP_KERNEL);
63300a2430fSAndrzej Pietrasiewicz 	if (!hidg->req->buf)
63400a2430fSAndrzej Pietrasiewicz 		goto fail;
63500a2430fSAndrzej Pietrasiewicz 
63600a2430fSAndrzej Pietrasiewicz 	/* set descriptor dynamic values */
63700a2430fSAndrzej Pietrasiewicz 	hidg_interface_desc.bInterfaceSubClass = hidg->bInterfaceSubClass;
63800a2430fSAndrzej Pietrasiewicz 	hidg_interface_desc.bInterfaceProtocol = hidg->bInterfaceProtocol;
63900a2430fSAndrzej Pietrasiewicz 	hidg_hs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
64000a2430fSAndrzej Pietrasiewicz 	hidg_fs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
64100a2430fSAndrzej Pietrasiewicz 	hidg_hs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
64200a2430fSAndrzej Pietrasiewicz 	hidg_fs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
643f286d487SKrzysztof Opasiak 	/*
644f286d487SKrzysztof Opasiak 	 * We can use hidg_desc struct here but we should not relay
645f286d487SKrzysztof Opasiak 	 * that its content won't change after returning from this function.
646f286d487SKrzysztof Opasiak 	 */
64700a2430fSAndrzej Pietrasiewicz 	hidg_desc.desc[0].bDescriptorType = HID_DT_REPORT;
64800a2430fSAndrzej Pietrasiewicz 	hidg_desc.desc[0].wDescriptorLength =
64900a2430fSAndrzej Pietrasiewicz 		cpu_to_le16(hidg->report_desc_length);
65000a2430fSAndrzej Pietrasiewicz 
65100a2430fSAndrzej Pietrasiewicz 	hidg_hs_in_ep_desc.bEndpointAddress =
65200a2430fSAndrzej Pietrasiewicz 		hidg_fs_in_ep_desc.bEndpointAddress;
65300a2430fSAndrzej Pietrasiewicz 	hidg_hs_out_ep_desc.bEndpointAddress =
65400a2430fSAndrzej Pietrasiewicz 		hidg_fs_out_ep_desc.bEndpointAddress;
65500a2430fSAndrzej Pietrasiewicz 
65600a2430fSAndrzej Pietrasiewicz 	status = usb_assign_descriptors(f, hidg_fs_descriptors,
65700a2430fSAndrzej Pietrasiewicz 			hidg_hs_descriptors, NULL);
65800a2430fSAndrzej Pietrasiewicz 	if (status)
65900a2430fSAndrzej Pietrasiewicz 		goto fail;
66000a2430fSAndrzej Pietrasiewicz 
66100a2430fSAndrzej Pietrasiewicz 	mutex_init(&hidg->lock);
66200a2430fSAndrzej Pietrasiewicz 	spin_lock_init(&hidg->spinlock);
66300a2430fSAndrzej Pietrasiewicz 	init_waitqueue_head(&hidg->write_queue);
66400a2430fSAndrzej Pietrasiewicz 	init_waitqueue_head(&hidg->read_queue);
66500a2430fSAndrzej Pietrasiewicz 	INIT_LIST_HEAD(&hidg->completed_out_req);
66600a2430fSAndrzej Pietrasiewicz 
66700a2430fSAndrzej Pietrasiewicz 	/* create char device */
66800a2430fSAndrzej Pietrasiewicz 	cdev_init(&hidg->cdev, &f_hidg_fops);
66900a2430fSAndrzej Pietrasiewicz 	dev = MKDEV(major, hidg->minor);
67000a2430fSAndrzej Pietrasiewicz 	status = cdev_add(&hidg->cdev, dev, 1);
67100a2430fSAndrzej Pietrasiewicz 	if (status)
672d12a8727SPavitrakumar Managutte 		goto fail_free_descs;
67300a2430fSAndrzej Pietrasiewicz 
67463406087SAndrzej Pietrasiewicz 	device = device_create(hidg_class, NULL, dev, NULL,
67563406087SAndrzej Pietrasiewicz 			       "%s%d", "hidg", hidg->minor);
67663406087SAndrzej Pietrasiewicz 	if (IS_ERR(device)) {
67763406087SAndrzej Pietrasiewicz 		status = PTR_ERR(device);
67863406087SAndrzej Pietrasiewicz 		goto del;
67963406087SAndrzej Pietrasiewicz 	}
68000a2430fSAndrzej Pietrasiewicz 
68100a2430fSAndrzej Pietrasiewicz 	return 0;
68263406087SAndrzej Pietrasiewicz del:
68363406087SAndrzej Pietrasiewicz 	cdev_del(&hidg->cdev);
684d12a8727SPavitrakumar Managutte fail_free_descs:
685d12a8727SPavitrakumar Managutte 	usb_free_all_descriptors(f);
68600a2430fSAndrzej Pietrasiewicz fail:
68700a2430fSAndrzej Pietrasiewicz 	ERROR(f->config->cdev, "hidg_bind FAILED\n");
68800a2430fSAndrzej Pietrasiewicz 	if (hidg->req != NULL) {
68900a2430fSAndrzej Pietrasiewicz 		kfree(hidg->req->buf);
69000a2430fSAndrzej Pietrasiewicz 		if (hidg->in_ep != NULL)
69100a2430fSAndrzej Pietrasiewicz 			usb_ep_free_request(hidg->in_ep, hidg->req);
69200a2430fSAndrzej Pietrasiewicz 	}
69300a2430fSAndrzej Pietrasiewicz 
69400a2430fSAndrzej Pietrasiewicz 	return status;
69500a2430fSAndrzej Pietrasiewicz }
69600a2430fSAndrzej Pietrasiewicz 
697cb382536SAndrzej Pietrasiewicz static inline int hidg_get_minor(void)
698cb382536SAndrzej Pietrasiewicz {
699cb382536SAndrzej Pietrasiewicz 	int ret;
700cb382536SAndrzej Pietrasiewicz 
701cb382536SAndrzej Pietrasiewicz 	ret = ida_simple_get(&hidg_ida, 0, 0, GFP_KERNEL);
702774cf72fSAndrzej Pietrasiewicz 	if (ret >= HIDG_MINORS) {
703774cf72fSAndrzej Pietrasiewicz 		ida_simple_remove(&hidg_ida, ret);
704774cf72fSAndrzej Pietrasiewicz 		ret = -ENODEV;
705774cf72fSAndrzej Pietrasiewicz 	}
706cb382536SAndrzej Pietrasiewicz 
707cb382536SAndrzej Pietrasiewicz 	return ret;
708cb382536SAndrzej Pietrasiewicz }
709cb382536SAndrzej Pietrasiewicz 
71021a9476aSAndrzej Pietrasiewicz static inline struct f_hid_opts *to_f_hid_opts(struct config_item *item)
71121a9476aSAndrzej Pietrasiewicz {
71221a9476aSAndrzej Pietrasiewicz 	return container_of(to_config_group(item), struct f_hid_opts,
71321a9476aSAndrzej Pietrasiewicz 			    func_inst.group);
71421a9476aSAndrzej Pietrasiewicz }
71521a9476aSAndrzej Pietrasiewicz 
71621a9476aSAndrzej Pietrasiewicz static void hid_attr_release(struct config_item *item)
71721a9476aSAndrzej Pietrasiewicz {
71821a9476aSAndrzej Pietrasiewicz 	struct f_hid_opts *opts = to_f_hid_opts(item);
71921a9476aSAndrzej Pietrasiewicz 
72021a9476aSAndrzej Pietrasiewicz 	usb_put_function_instance(&opts->func_inst);
72121a9476aSAndrzej Pietrasiewicz }
72221a9476aSAndrzej Pietrasiewicz 
72321a9476aSAndrzej Pietrasiewicz static struct configfs_item_operations hidg_item_ops = {
72421a9476aSAndrzej Pietrasiewicz 	.release	= hid_attr_release,
72521a9476aSAndrzej Pietrasiewicz };
72621a9476aSAndrzej Pietrasiewicz 
72721a9476aSAndrzej Pietrasiewicz #define F_HID_OPT(name, prec, limit)					\
728*da4e527cSChristoph Hellwig static ssize_t f_hid_opts_##name##_show(struct config_item *item, char *page)\
72921a9476aSAndrzej Pietrasiewicz {									\
730*da4e527cSChristoph Hellwig 	struct f_hid_opts *opts = to_f_hid_opts(item);			\
73121a9476aSAndrzej Pietrasiewicz 	int result;							\
73221a9476aSAndrzej Pietrasiewicz 									\
73321a9476aSAndrzej Pietrasiewicz 	mutex_lock(&opts->lock);					\
73421a9476aSAndrzej Pietrasiewicz 	result = sprintf(page, "%d\n", opts->name);			\
73521a9476aSAndrzej Pietrasiewicz 	mutex_unlock(&opts->lock);					\
73621a9476aSAndrzej Pietrasiewicz 									\
73721a9476aSAndrzej Pietrasiewicz 	return result;							\
73821a9476aSAndrzej Pietrasiewicz }									\
73921a9476aSAndrzej Pietrasiewicz 									\
740*da4e527cSChristoph Hellwig static ssize_t f_hid_opts_##name##_store(struct config_item *item,	\
74121a9476aSAndrzej Pietrasiewicz 					 const char *page, size_t len)	\
74221a9476aSAndrzej Pietrasiewicz {									\
743*da4e527cSChristoph Hellwig 	struct f_hid_opts *opts = to_f_hid_opts(item);			\
74421a9476aSAndrzej Pietrasiewicz 	int ret;							\
74521a9476aSAndrzej Pietrasiewicz 	u##prec num;							\
74621a9476aSAndrzej Pietrasiewicz 									\
74721a9476aSAndrzej Pietrasiewicz 	mutex_lock(&opts->lock);					\
74821a9476aSAndrzej Pietrasiewicz 	if (opts->refcnt) {						\
74921a9476aSAndrzej Pietrasiewicz 		ret = -EBUSY;						\
75021a9476aSAndrzej Pietrasiewicz 		goto end;						\
75121a9476aSAndrzej Pietrasiewicz 	}								\
75221a9476aSAndrzej Pietrasiewicz 									\
75321a9476aSAndrzej Pietrasiewicz 	ret = kstrtou##prec(page, 0, &num);				\
75421a9476aSAndrzej Pietrasiewicz 	if (ret)							\
75521a9476aSAndrzej Pietrasiewicz 		goto end;						\
75621a9476aSAndrzej Pietrasiewicz 									\
75721a9476aSAndrzej Pietrasiewicz 	if (num > limit) {						\
75821a9476aSAndrzej Pietrasiewicz 		ret = -EINVAL;						\
75921a9476aSAndrzej Pietrasiewicz 		goto end;						\
76021a9476aSAndrzej Pietrasiewicz 	}								\
76121a9476aSAndrzej Pietrasiewicz 	opts->name = num;						\
76221a9476aSAndrzej Pietrasiewicz 	ret = len;							\
76321a9476aSAndrzej Pietrasiewicz 									\
76421a9476aSAndrzej Pietrasiewicz end:									\
76521a9476aSAndrzej Pietrasiewicz 	mutex_unlock(&opts->lock);					\
76621a9476aSAndrzej Pietrasiewicz 	return ret;							\
76721a9476aSAndrzej Pietrasiewicz }									\
76821a9476aSAndrzej Pietrasiewicz 									\
769*da4e527cSChristoph Hellwig CONFIGFS_ATTR(f_hid_opts_, name)
77021a9476aSAndrzej Pietrasiewicz 
77121a9476aSAndrzej Pietrasiewicz F_HID_OPT(subclass, 8, 255);
77221a9476aSAndrzej Pietrasiewicz F_HID_OPT(protocol, 8, 255);
77339a2ac27SAndrzej Pietrasiewicz F_HID_OPT(report_length, 16, 65535);
77421a9476aSAndrzej Pietrasiewicz 
775*da4e527cSChristoph Hellwig static ssize_t f_hid_opts_report_desc_show(struct config_item *item, char *page)
77621a9476aSAndrzej Pietrasiewicz {
777*da4e527cSChristoph Hellwig 	struct f_hid_opts *opts = to_f_hid_opts(item);
77821a9476aSAndrzej Pietrasiewicz 	int result;
77921a9476aSAndrzej Pietrasiewicz 
78021a9476aSAndrzej Pietrasiewicz 	mutex_lock(&opts->lock);
78121a9476aSAndrzej Pietrasiewicz 	result = opts->report_desc_length;
78221a9476aSAndrzej Pietrasiewicz 	memcpy(page, opts->report_desc, opts->report_desc_length);
78321a9476aSAndrzej Pietrasiewicz 	mutex_unlock(&opts->lock);
78421a9476aSAndrzej Pietrasiewicz 
78521a9476aSAndrzej Pietrasiewicz 	return result;
78621a9476aSAndrzej Pietrasiewicz }
78721a9476aSAndrzej Pietrasiewicz 
788*da4e527cSChristoph Hellwig static ssize_t f_hid_opts_report_desc_store(struct config_item *item,
78921a9476aSAndrzej Pietrasiewicz 					    const char *page, size_t len)
79021a9476aSAndrzej Pietrasiewicz {
791*da4e527cSChristoph Hellwig 	struct f_hid_opts *opts = to_f_hid_opts(item);
79221a9476aSAndrzej Pietrasiewicz 	int ret = -EBUSY;
79321a9476aSAndrzej Pietrasiewicz 	char *d;
79421a9476aSAndrzej Pietrasiewicz 
79521a9476aSAndrzej Pietrasiewicz 	mutex_lock(&opts->lock);
79621a9476aSAndrzej Pietrasiewicz 
79721a9476aSAndrzej Pietrasiewicz 	if (opts->refcnt)
79821a9476aSAndrzej Pietrasiewicz 		goto end;
79921a9476aSAndrzej Pietrasiewicz 	if (len > PAGE_SIZE) {
80021a9476aSAndrzej Pietrasiewicz 		ret = -ENOSPC;
80121a9476aSAndrzej Pietrasiewicz 		goto end;
80221a9476aSAndrzej Pietrasiewicz 	}
80321a9476aSAndrzej Pietrasiewicz 	d = kmemdup(page, len, GFP_KERNEL);
80421a9476aSAndrzej Pietrasiewicz 	if (!d) {
80521a9476aSAndrzej Pietrasiewicz 		ret = -ENOMEM;
80621a9476aSAndrzej Pietrasiewicz 		goto end;
80721a9476aSAndrzej Pietrasiewicz 	}
80821a9476aSAndrzej Pietrasiewicz 	kfree(opts->report_desc);
80921a9476aSAndrzej Pietrasiewicz 	opts->report_desc = d;
81021a9476aSAndrzej Pietrasiewicz 	opts->report_desc_length = len;
81121a9476aSAndrzej Pietrasiewicz 	opts->report_desc_alloc = true;
81221a9476aSAndrzej Pietrasiewicz 	ret = len;
81321a9476aSAndrzej Pietrasiewicz end:
81421a9476aSAndrzej Pietrasiewicz 	mutex_unlock(&opts->lock);
81521a9476aSAndrzej Pietrasiewicz 	return ret;
81621a9476aSAndrzej Pietrasiewicz }
81721a9476aSAndrzej Pietrasiewicz 
818*da4e527cSChristoph Hellwig CONFIGFS_ATTR(f_hid_opts_, report_desc);
81921a9476aSAndrzej Pietrasiewicz 
82021a9476aSAndrzej Pietrasiewicz static struct configfs_attribute *hid_attrs[] = {
821*da4e527cSChristoph Hellwig 	&f_hid_opts_attr_subclass,
822*da4e527cSChristoph Hellwig 	&f_hid_opts_attr_protocol,
823*da4e527cSChristoph Hellwig 	&f_hid_opts_attr_report_length,
824*da4e527cSChristoph Hellwig 	&f_hid_opts_attr_report_desc,
82521a9476aSAndrzej Pietrasiewicz 	NULL,
82621a9476aSAndrzej Pietrasiewicz };
82721a9476aSAndrzej Pietrasiewicz 
82821a9476aSAndrzej Pietrasiewicz static struct config_item_type hid_func_type = {
82921a9476aSAndrzej Pietrasiewicz 	.ct_item_ops	= &hidg_item_ops,
83021a9476aSAndrzej Pietrasiewicz 	.ct_attrs	= hid_attrs,
83121a9476aSAndrzej Pietrasiewicz 	.ct_owner	= THIS_MODULE,
83221a9476aSAndrzej Pietrasiewicz };
83321a9476aSAndrzej Pietrasiewicz 
834cb382536SAndrzej Pietrasiewicz static inline void hidg_put_minor(int minor)
835cb382536SAndrzej Pietrasiewicz {
836cb382536SAndrzej Pietrasiewicz 	ida_simple_remove(&hidg_ida, minor);
837cb382536SAndrzej Pietrasiewicz }
838cb382536SAndrzej Pietrasiewicz 
839cb382536SAndrzej Pietrasiewicz static void hidg_free_inst(struct usb_function_instance *f)
840cb382536SAndrzej Pietrasiewicz {
841cb382536SAndrzej Pietrasiewicz 	struct f_hid_opts *opts;
842cb382536SAndrzej Pietrasiewicz 
843cb382536SAndrzej Pietrasiewicz 	opts = container_of(f, struct f_hid_opts, func_inst);
844cb382536SAndrzej Pietrasiewicz 
845cb382536SAndrzej Pietrasiewicz 	mutex_lock(&hidg_ida_lock);
846cb382536SAndrzej Pietrasiewicz 
847cb382536SAndrzej Pietrasiewicz 	hidg_put_minor(opts->minor);
848cb382536SAndrzej Pietrasiewicz 	if (idr_is_empty(&hidg_ida.idr))
849cb382536SAndrzej Pietrasiewicz 		ghid_cleanup();
850cb382536SAndrzej Pietrasiewicz 
851cb382536SAndrzej Pietrasiewicz 	mutex_unlock(&hidg_ida_lock);
852cb382536SAndrzej Pietrasiewicz 
853cb382536SAndrzej Pietrasiewicz 	if (opts->report_desc_alloc)
854cb382536SAndrzej Pietrasiewicz 		kfree(opts->report_desc);
855cb382536SAndrzej Pietrasiewicz 
856cb382536SAndrzej Pietrasiewicz 	kfree(opts);
857cb382536SAndrzej Pietrasiewicz }
858cb382536SAndrzej Pietrasiewicz 
859cb382536SAndrzej Pietrasiewicz static struct usb_function_instance *hidg_alloc_inst(void)
860cb382536SAndrzej Pietrasiewicz {
861cb382536SAndrzej Pietrasiewicz 	struct f_hid_opts *opts;
862cb382536SAndrzej Pietrasiewicz 	struct usb_function_instance *ret;
863cb382536SAndrzej Pietrasiewicz 	int status = 0;
864cb382536SAndrzej Pietrasiewicz 
865cb382536SAndrzej Pietrasiewicz 	opts = kzalloc(sizeof(*opts), GFP_KERNEL);
866cb382536SAndrzej Pietrasiewicz 	if (!opts)
867cb382536SAndrzej Pietrasiewicz 		return ERR_PTR(-ENOMEM);
86821a9476aSAndrzej Pietrasiewicz 	mutex_init(&opts->lock);
869cb382536SAndrzej Pietrasiewicz 	opts->func_inst.free_func_inst = hidg_free_inst;
870cb382536SAndrzej Pietrasiewicz 	ret = &opts->func_inst;
871cb382536SAndrzej Pietrasiewicz 
872cb382536SAndrzej Pietrasiewicz 	mutex_lock(&hidg_ida_lock);
873cb382536SAndrzej Pietrasiewicz 
874cb382536SAndrzej Pietrasiewicz 	if (idr_is_empty(&hidg_ida.idr)) {
875cb382536SAndrzej Pietrasiewicz 		status = ghid_setup(NULL, HIDG_MINORS);
876cb382536SAndrzej Pietrasiewicz 		if (status)  {
877cb382536SAndrzej Pietrasiewicz 			ret = ERR_PTR(status);
878cb382536SAndrzej Pietrasiewicz 			kfree(opts);
879cb382536SAndrzej Pietrasiewicz 			goto unlock;
880cb382536SAndrzej Pietrasiewicz 		}
881cb382536SAndrzej Pietrasiewicz 	}
882cb382536SAndrzej Pietrasiewicz 
883cb382536SAndrzej Pietrasiewicz 	opts->minor = hidg_get_minor();
884cb382536SAndrzej Pietrasiewicz 	if (opts->minor < 0) {
885cb382536SAndrzej Pietrasiewicz 		ret = ERR_PTR(opts->minor);
886cb382536SAndrzej Pietrasiewicz 		kfree(opts);
887cb382536SAndrzej Pietrasiewicz 		if (idr_is_empty(&hidg_ida.idr))
888cb382536SAndrzej Pietrasiewicz 			ghid_cleanup();
889828f6148SDan Carpenter 		goto unlock;
890cb382536SAndrzej Pietrasiewicz 	}
89121a9476aSAndrzej Pietrasiewicz 	config_group_init_type_name(&opts->func_inst.group, "", &hid_func_type);
892cb382536SAndrzej Pietrasiewicz 
893cb382536SAndrzej Pietrasiewicz unlock:
894cb382536SAndrzej Pietrasiewicz 	mutex_unlock(&hidg_ida_lock);
895cb382536SAndrzej Pietrasiewicz 	return ret;
896cb382536SAndrzej Pietrasiewicz }
897cb382536SAndrzej Pietrasiewicz 
898cb382536SAndrzej Pietrasiewicz static void hidg_free(struct usb_function *f)
899cb382536SAndrzej Pietrasiewicz {
900cb382536SAndrzej Pietrasiewicz 	struct f_hidg *hidg;
90121a9476aSAndrzej Pietrasiewicz 	struct f_hid_opts *opts;
902cb382536SAndrzej Pietrasiewicz 
903cb382536SAndrzej Pietrasiewicz 	hidg = func_to_hidg(f);
90421a9476aSAndrzej Pietrasiewicz 	opts = container_of(f->fi, struct f_hid_opts, func_inst);
905cb382536SAndrzej Pietrasiewicz 	kfree(hidg->report_desc);
906cb382536SAndrzej Pietrasiewicz 	kfree(hidg);
90721a9476aSAndrzej Pietrasiewicz 	mutex_lock(&opts->lock);
90821a9476aSAndrzej Pietrasiewicz 	--opts->refcnt;
90921a9476aSAndrzej Pietrasiewicz 	mutex_unlock(&opts->lock);
910cb382536SAndrzej Pietrasiewicz }
911cb382536SAndrzej Pietrasiewicz 
91200a2430fSAndrzej Pietrasiewicz static void hidg_unbind(struct usb_configuration *c, struct usb_function *f)
91300a2430fSAndrzej Pietrasiewicz {
91400a2430fSAndrzej Pietrasiewicz 	struct f_hidg *hidg = func_to_hidg(f);
91500a2430fSAndrzej Pietrasiewicz 
91600a2430fSAndrzej Pietrasiewicz 	device_destroy(hidg_class, MKDEV(major, hidg->minor));
91700a2430fSAndrzej Pietrasiewicz 	cdev_del(&hidg->cdev);
91800a2430fSAndrzej Pietrasiewicz 
91900a2430fSAndrzej Pietrasiewicz 	/* disable/free request and end point */
92000a2430fSAndrzej Pietrasiewicz 	usb_ep_disable(hidg->in_ep);
92100a2430fSAndrzej Pietrasiewicz 	kfree(hidg->req->buf);
92200a2430fSAndrzej Pietrasiewicz 	usb_ep_free_request(hidg->in_ep, hidg->req);
92300a2430fSAndrzej Pietrasiewicz 
92400a2430fSAndrzej Pietrasiewicz 	usb_free_all_descriptors(f);
92500a2430fSAndrzej Pietrasiewicz }
92600a2430fSAndrzej Pietrasiewicz 
9270fc57ea0SFengguang Wu static struct usb_function *hidg_alloc(struct usb_function_instance *fi)
92800a2430fSAndrzej Pietrasiewicz {
92900a2430fSAndrzej Pietrasiewicz 	struct f_hidg *hidg;
930cb382536SAndrzej Pietrasiewicz 	struct f_hid_opts *opts;
93100a2430fSAndrzej Pietrasiewicz 
93200a2430fSAndrzej Pietrasiewicz 	/* allocate and initialize one new instance */
933cb382536SAndrzej Pietrasiewicz 	hidg = kzalloc(sizeof(*hidg), GFP_KERNEL);
93400a2430fSAndrzej Pietrasiewicz 	if (!hidg)
935cb382536SAndrzej Pietrasiewicz 		return ERR_PTR(-ENOMEM);
93600a2430fSAndrzej Pietrasiewicz 
937cb382536SAndrzej Pietrasiewicz 	opts = container_of(fi, struct f_hid_opts, func_inst);
938cb382536SAndrzej Pietrasiewicz 
93921a9476aSAndrzej Pietrasiewicz 	mutex_lock(&opts->lock);
94021a9476aSAndrzej Pietrasiewicz 	++opts->refcnt;
94121a9476aSAndrzej Pietrasiewicz 
942cb382536SAndrzej Pietrasiewicz 	hidg->minor = opts->minor;
943cb382536SAndrzej Pietrasiewicz 	hidg->bInterfaceSubClass = opts->subclass;
944cb382536SAndrzej Pietrasiewicz 	hidg->bInterfaceProtocol = opts->protocol;
945cb382536SAndrzej Pietrasiewicz 	hidg->report_length = opts->report_length;
946cb382536SAndrzej Pietrasiewicz 	hidg->report_desc_length = opts->report_desc_length;
947cb382536SAndrzej Pietrasiewicz 	if (opts->report_desc) {
948cb382536SAndrzej Pietrasiewicz 		hidg->report_desc = kmemdup(opts->report_desc,
949cb382536SAndrzej Pietrasiewicz 					    opts->report_desc_length,
95000a2430fSAndrzej Pietrasiewicz 					    GFP_KERNEL);
95100a2430fSAndrzej Pietrasiewicz 		if (!hidg->report_desc) {
95200a2430fSAndrzej Pietrasiewicz 			kfree(hidg);
95321a9476aSAndrzej Pietrasiewicz 			mutex_unlock(&opts->lock);
954cb382536SAndrzej Pietrasiewicz 			return ERR_PTR(-ENOMEM);
955cb382536SAndrzej Pietrasiewicz 		}
95600a2430fSAndrzej Pietrasiewicz 	}
95700a2430fSAndrzej Pietrasiewicz 
95821a9476aSAndrzej Pietrasiewicz 	mutex_unlock(&opts->lock);
95921a9476aSAndrzej Pietrasiewicz 
96000a2430fSAndrzej Pietrasiewicz 	hidg->func.name    = "hid";
96100a2430fSAndrzej Pietrasiewicz 	hidg->func.bind    = hidg_bind;
96200a2430fSAndrzej Pietrasiewicz 	hidg->func.unbind  = hidg_unbind;
96300a2430fSAndrzej Pietrasiewicz 	hidg->func.set_alt = hidg_set_alt;
96400a2430fSAndrzej Pietrasiewicz 	hidg->func.disable = hidg_disable;
96500a2430fSAndrzej Pietrasiewicz 	hidg->func.setup   = hidg_setup;
966cb382536SAndrzej Pietrasiewicz 	hidg->func.free_func = hidg_free;
96700a2430fSAndrzej Pietrasiewicz 
96800a2430fSAndrzej Pietrasiewicz 	/* this could me made configurable at some point */
96900a2430fSAndrzej Pietrasiewicz 	hidg->qlen	   = 4;
97000a2430fSAndrzej Pietrasiewicz 
971cb382536SAndrzej Pietrasiewicz 	return &hidg->func;
97200a2430fSAndrzej Pietrasiewicz }
97300a2430fSAndrzej Pietrasiewicz 
974cb382536SAndrzej Pietrasiewicz DECLARE_USB_FUNCTION_INIT(hid, hidg_alloc_inst, hidg_alloc);
975cb382536SAndrzej Pietrasiewicz MODULE_LICENSE("GPL");
976cb382536SAndrzej Pietrasiewicz MODULE_AUTHOR("Fabien Chouteau");
977cb382536SAndrzej Pietrasiewicz 
978cb382536SAndrzej Pietrasiewicz int ghid_setup(struct usb_gadget *g, int count)
97900a2430fSAndrzej Pietrasiewicz {
98000a2430fSAndrzej Pietrasiewicz 	int status;
98100a2430fSAndrzej Pietrasiewicz 	dev_t dev;
98200a2430fSAndrzej Pietrasiewicz 
98300a2430fSAndrzej Pietrasiewicz 	hidg_class = class_create(THIS_MODULE, "hidg");
98406529407SAndrzej Pietrasiewicz 	if (IS_ERR(hidg_class)) {
9850448d38cSDan Carpenter 		status = PTR_ERR(hidg_class);
98606529407SAndrzej Pietrasiewicz 		hidg_class = NULL;
9870448d38cSDan Carpenter 		return status;
98800a2430fSAndrzej Pietrasiewicz 	}
98900a2430fSAndrzej Pietrasiewicz 
99000a2430fSAndrzej Pietrasiewicz 	status = alloc_chrdev_region(&dev, 0, count, "hidg");
9910448d38cSDan Carpenter 	if (status) {
9920448d38cSDan Carpenter 		class_destroy(hidg_class);
9930448d38cSDan Carpenter 		hidg_class = NULL;
99400a2430fSAndrzej Pietrasiewicz 		return status;
99500a2430fSAndrzej Pietrasiewicz 	}
99600a2430fSAndrzej Pietrasiewicz 
9970448d38cSDan Carpenter 	major = MAJOR(dev);
9980448d38cSDan Carpenter 	minors = count;
9990448d38cSDan Carpenter 
10000448d38cSDan Carpenter 	return 0;
100100a2430fSAndrzej Pietrasiewicz }
100200a2430fSAndrzej Pietrasiewicz 
100300a2430fSAndrzej Pietrasiewicz void ghid_cleanup(void)
100400a2430fSAndrzej Pietrasiewicz {
100500a2430fSAndrzej Pietrasiewicz 	if (major) {
100600a2430fSAndrzej Pietrasiewicz 		unregister_chrdev_region(MKDEV(major, 0), minors);
100700a2430fSAndrzej Pietrasiewicz 		major = minors = 0;
100800a2430fSAndrzej Pietrasiewicz 	}
100900a2430fSAndrzej Pietrasiewicz 
101000a2430fSAndrzej Pietrasiewicz 	class_destroy(hidg_class);
101100a2430fSAndrzej Pietrasiewicz 	hidg_class = NULL;
101200a2430fSAndrzej Pietrasiewicz }
1013