xref: /openbmc/linux/drivers/usb/gadget/function/f_hid.c (revision 5ca8d3ec9970f4798e68bd21a9d44db3d0ff4da7)
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);
585*5ca8d3ecSAndrzej 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 */
591*5ca8d3ecSAndrzej Pietrasiewicz 	us = usb_gstrings_attach(c->cdev, ct_func_strings,
592*5ca8d3ecSAndrzej Pietrasiewicz 				 ARRAY_SIZE(ct_func_string_defs));
593*5ca8d3ecSAndrzej Pietrasiewicz 	if (IS_ERR(us))
594*5ca8d3ecSAndrzej Pietrasiewicz 		return PTR_ERR(us);
595*5ca8d3ecSAndrzej 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 
693cb382536SAndrzej Pietrasiewicz static inline void hidg_put_minor(int minor)
694cb382536SAndrzej Pietrasiewicz {
695cb382536SAndrzej Pietrasiewicz 	ida_simple_remove(&hidg_ida, minor);
696cb382536SAndrzej Pietrasiewicz }
697cb382536SAndrzej Pietrasiewicz 
698cb382536SAndrzej Pietrasiewicz static void hidg_free_inst(struct usb_function_instance *f)
699cb382536SAndrzej Pietrasiewicz {
700cb382536SAndrzej Pietrasiewicz 	struct f_hid_opts *opts;
701cb382536SAndrzej Pietrasiewicz 
702cb382536SAndrzej Pietrasiewicz 	opts = container_of(f, struct f_hid_opts, func_inst);
703cb382536SAndrzej Pietrasiewicz 
704cb382536SAndrzej Pietrasiewicz 	mutex_lock(&hidg_ida_lock);
705cb382536SAndrzej Pietrasiewicz 
706cb382536SAndrzej Pietrasiewicz 	hidg_put_minor(opts->minor);
707cb382536SAndrzej Pietrasiewicz 	if (idr_is_empty(&hidg_ida.idr))
708cb382536SAndrzej Pietrasiewicz 		ghid_cleanup();
709cb382536SAndrzej Pietrasiewicz 
710cb382536SAndrzej Pietrasiewicz 	mutex_unlock(&hidg_ida_lock);
711cb382536SAndrzej Pietrasiewicz 
712cb382536SAndrzej Pietrasiewicz 	if (opts->report_desc_alloc)
713cb382536SAndrzej Pietrasiewicz 		kfree(opts->report_desc);
714cb382536SAndrzej Pietrasiewicz 
715cb382536SAndrzej Pietrasiewicz 	kfree(opts);
716cb382536SAndrzej Pietrasiewicz }
717cb382536SAndrzej Pietrasiewicz 
718cb382536SAndrzej Pietrasiewicz static struct usb_function_instance *hidg_alloc_inst(void)
719cb382536SAndrzej Pietrasiewicz {
720cb382536SAndrzej Pietrasiewicz 	struct f_hid_opts *opts;
721cb382536SAndrzej Pietrasiewicz 	struct usb_function_instance *ret;
722cb382536SAndrzej Pietrasiewicz 	int status = 0;
723cb382536SAndrzej Pietrasiewicz 
724cb382536SAndrzej Pietrasiewicz 	opts = kzalloc(sizeof(*opts), GFP_KERNEL);
725cb382536SAndrzej Pietrasiewicz 	if (!opts)
726cb382536SAndrzej Pietrasiewicz 		return ERR_PTR(-ENOMEM);
727cb382536SAndrzej Pietrasiewicz 
728cb382536SAndrzej Pietrasiewicz 	opts->func_inst.free_func_inst = hidg_free_inst;
729cb382536SAndrzej Pietrasiewicz 	ret = &opts->func_inst;
730cb382536SAndrzej Pietrasiewicz 
731cb382536SAndrzej Pietrasiewicz 	mutex_lock(&hidg_ida_lock);
732cb382536SAndrzej Pietrasiewicz 
733cb382536SAndrzej Pietrasiewicz 	if (idr_is_empty(&hidg_ida.idr)) {
734cb382536SAndrzej Pietrasiewicz 		status = ghid_setup(NULL, HIDG_MINORS);
735cb382536SAndrzej Pietrasiewicz 		if (status)  {
736cb382536SAndrzej Pietrasiewicz 			ret = ERR_PTR(status);
737cb382536SAndrzej Pietrasiewicz 			kfree(opts);
738cb382536SAndrzej Pietrasiewicz 			goto unlock;
739cb382536SAndrzej Pietrasiewicz 		}
740cb382536SAndrzej Pietrasiewicz 	}
741cb382536SAndrzej Pietrasiewicz 
742cb382536SAndrzej Pietrasiewicz 	opts->minor = hidg_get_minor();
743cb382536SAndrzej Pietrasiewicz 	if (opts->minor < 0) {
744cb382536SAndrzej Pietrasiewicz 		ret = ERR_PTR(opts->minor);
745cb382536SAndrzej Pietrasiewicz 		kfree(opts);
746cb382536SAndrzej Pietrasiewicz 		if (idr_is_empty(&hidg_ida.idr))
747cb382536SAndrzej Pietrasiewicz 			ghid_cleanup();
748cb382536SAndrzej Pietrasiewicz 	}
749cb382536SAndrzej Pietrasiewicz 
750cb382536SAndrzej Pietrasiewicz unlock:
751cb382536SAndrzej Pietrasiewicz 	mutex_unlock(&hidg_ida_lock);
752cb382536SAndrzej Pietrasiewicz 	return ret;
753cb382536SAndrzej Pietrasiewicz }
754cb382536SAndrzej Pietrasiewicz 
755cb382536SAndrzej Pietrasiewicz static void hidg_free(struct usb_function *f)
756cb382536SAndrzej Pietrasiewicz {
757cb382536SAndrzej Pietrasiewicz 	struct f_hidg *hidg;
758cb382536SAndrzej Pietrasiewicz 
759cb382536SAndrzej Pietrasiewicz 	hidg = func_to_hidg(f);
760cb382536SAndrzej Pietrasiewicz 	kfree(hidg->report_desc);
761cb382536SAndrzej Pietrasiewicz 	kfree(hidg);
762cb382536SAndrzej Pietrasiewicz }
763cb382536SAndrzej Pietrasiewicz 
764cb382536SAndrzej Pietrasiewicz static void hidg_unbind(struct usb_configuration *c, struct usb_function *f)
765cb382536SAndrzej Pietrasiewicz {
766cb382536SAndrzej Pietrasiewicz 	struct f_hidg *hidg = func_to_hidg(f);
767cb382536SAndrzej Pietrasiewicz 
768cb382536SAndrzej Pietrasiewicz 	device_destroy(hidg_class, MKDEV(major, hidg->minor));
769cb382536SAndrzej Pietrasiewicz 	cdev_del(&hidg->cdev);
770cb382536SAndrzej Pietrasiewicz 
771cb382536SAndrzej Pietrasiewicz 	/* disable/free request and end point */
772cb382536SAndrzej Pietrasiewicz 	usb_ep_disable(hidg->in_ep);
773cb382536SAndrzej Pietrasiewicz 	usb_ep_dequeue(hidg->in_ep, hidg->req);
774cb382536SAndrzej Pietrasiewicz 	kfree(hidg->req->buf);
775cb382536SAndrzej Pietrasiewicz 	usb_ep_free_request(hidg->in_ep, hidg->req);
776cb382536SAndrzej Pietrasiewicz 
777cb382536SAndrzej Pietrasiewicz 	usb_free_all_descriptors(f);
778cb382536SAndrzej Pietrasiewicz }
779cb382536SAndrzej Pietrasiewicz 
780cb382536SAndrzej Pietrasiewicz struct usb_function *hidg_alloc(struct usb_function_instance *fi)
781cb382536SAndrzej Pietrasiewicz {
782cb382536SAndrzej Pietrasiewicz 	struct f_hidg *hidg;
783cb382536SAndrzej Pietrasiewicz 	struct f_hid_opts *opts;
784cb382536SAndrzej Pietrasiewicz 
785cb382536SAndrzej Pietrasiewicz 	/* allocate and initialize one new instance */
786cb382536SAndrzej Pietrasiewicz 	hidg = kzalloc(sizeof(*hidg), GFP_KERNEL);
787cb382536SAndrzej Pietrasiewicz 	if (!hidg)
788cb382536SAndrzej Pietrasiewicz 		return ERR_PTR(-ENOMEM);
789cb382536SAndrzej Pietrasiewicz 
790cb382536SAndrzej Pietrasiewicz 	opts = container_of(fi, struct f_hid_opts, func_inst);
791cb382536SAndrzej Pietrasiewicz 
792cb382536SAndrzej Pietrasiewicz 	hidg->minor = opts->minor;
793cb382536SAndrzej Pietrasiewicz 	hidg->bInterfaceSubClass = opts->subclass;
794cb382536SAndrzej Pietrasiewicz 	hidg->bInterfaceProtocol = opts->protocol;
795cb382536SAndrzej Pietrasiewicz 	hidg->report_length = opts->report_length;
796cb382536SAndrzej Pietrasiewicz 	hidg->report_desc_length = opts->report_desc_length;
797cb382536SAndrzej Pietrasiewicz 	if (opts->report_desc) {
798cb382536SAndrzej Pietrasiewicz 		hidg->report_desc = kmemdup(opts->report_desc,
799cb382536SAndrzej Pietrasiewicz 					    opts->report_desc_length,
800cb382536SAndrzej Pietrasiewicz 					    GFP_KERNEL);
801cb382536SAndrzej Pietrasiewicz 		if (!hidg->report_desc) {
802cb382536SAndrzej Pietrasiewicz 			kfree(hidg);
803cb382536SAndrzej Pietrasiewicz 			return ERR_PTR(-ENOMEM);
804cb382536SAndrzej Pietrasiewicz 		}
805cb382536SAndrzej Pietrasiewicz 	}
806cb382536SAndrzej Pietrasiewicz 
807cb382536SAndrzej Pietrasiewicz 	hidg->func.name    = "hid";
808cb382536SAndrzej Pietrasiewicz 	hidg->func.bind    = hidg_bind;
809cb382536SAndrzej Pietrasiewicz 	hidg->func.unbind  = hidg_unbind;
810cb382536SAndrzej Pietrasiewicz 	hidg->func.set_alt = hidg_set_alt;
811cb382536SAndrzej Pietrasiewicz 	hidg->func.disable = hidg_disable;
812cb382536SAndrzej Pietrasiewicz 	hidg->func.setup   = hidg_setup;
813cb382536SAndrzej Pietrasiewicz 	hidg->func.free_func = hidg_free;
814cb382536SAndrzej Pietrasiewicz 
815cb382536SAndrzej Pietrasiewicz 	/* this could me made configurable at some point */
816cb382536SAndrzej Pietrasiewicz 	hidg->qlen	   = 4;
817cb382536SAndrzej Pietrasiewicz 
818cb382536SAndrzej Pietrasiewicz 	return &hidg->func;
819cb382536SAndrzej Pietrasiewicz }
820cb382536SAndrzej Pietrasiewicz 
821cb382536SAndrzej Pietrasiewicz DECLARE_USB_FUNCTION_INIT(hid, hidg_alloc_inst, hidg_alloc);
822cb382536SAndrzej Pietrasiewicz MODULE_LICENSE("GPL");
823cb382536SAndrzej Pietrasiewicz MODULE_AUTHOR("Fabien Chouteau");
824cb382536SAndrzej Pietrasiewicz 
825cb382536SAndrzej Pietrasiewicz int ghid_setup(struct usb_gadget *g, int count)
82600a2430fSAndrzej Pietrasiewicz {
82700a2430fSAndrzej Pietrasiewicz 	int status;
82800a2430fSAndrzej Pietrasiewicz 	dev_t dev;
82900a2430fSAndrzej Pietrasiewicz 
83000a2430fSAndrzej Pietrasiewicz 	hidg_class = class_create(THIS_MODULE, "hidg");
83106529407SAndrzej Pietrasiewicz 	if (IS_ERR(hidg_class)) {
83206529407SAndrzej Pietrasiewicz 		hidg_class = NULL;
83306529407SAndrzej Pietrasiewicz 		return PTR_ERR(hidg_class);
83406529407SAndrzej Pietrasiewicz 	}
83500a2430fSAndrzej Pietrasiewicz 
83600a2430fSAndrzej Pietrasiewicz 	status = alloc_chrdev_region(&dev, 0, count, "hidg");
83700a2430fSAndrzej Pietrasiewicz 	if (!status) {
83800a2430fSAndrzej Pietrasiewicz 		major = MAJOR(dev);
83900a2430fSAndrzej Pietrasiewicz 		minors = count;
84000a2430fSAndrzej Pietrasiewicz 	}
84100a2430fSAndrzej Pietrasiewicz 
84200a2430fSAndrzej Pietrasiewicz 	return status;
84300a2430fSAndrzej Pietrasiewicz }
84400a2430fSAndrzej Pietrasiewicz 
84500a2430fSAndrzej Pietrasiewicz void ghid_cleanup(void)
84600a2430fSAndrzej Pietrasiewicz {
84700a2430fSAndrzej Pietrasiewicz 	if (major) {
84800a2430fSAndrzej Pietrasiewicz 		unregister_chrdev_region(MKDEV(major, 0), minors);
84900a2430fSAndrzej Pietrasiewicz 		major = minors = 0;
85000a2430fSAndrzej Pietrasiewicz 	}
85100a2430fSAndrzej Pietrasiewicz 
85200a2430fSAndrzej Pietrasiewicz 	class_destroy(hidg_class);
85300a2430fSAndrzej Pietrasiewicz 	hidg_class = NULL;
85400a2430fSAndrzej Pietrasiewicz }
855