1/*
2 * f_hid.c -- USB HID function driver
3 *
4 * Copyright (C) 2010 Fabien Chouteau <fabien.chouteau@barco.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 */
11
12#include <linux/kernel.h>
13#include <linux/module.h>
14#include <linux/hid.h>
15#include <linux/cdev.h>
16#include <linux/mutex.h>
17#include <linux/poll.h>
18#include <linux/uaccess.h>
19#include <linux/wait.h>
20#include <linux/sched.h>
21#include <linux/usb/g_hid.h>
22
23#include "u_f.h"
24
25static int major, minors;
26static struct class *hidg_class;
27
28/*-------------------------------------------------------------------------*/
29/*                            HID gadget struct                            */
30
31struct f_hidg_req_list {
32	struct usb_request	*req;
33	unsigned int		pos;
34	struct list_head 	list;
35};
36
37struct f_hidg {
38	/* configuration */
39	unsigned char			bInterfaceSubClass;
40	unsigned char			bInterfaceProtocol;
41	unsigned short			report_desc_length;
42	char				*report_desc;
43	unsigned short			report_length;
44
45	/* recv report */
46	struct list_head		completed_out_req;
47	spinlock_t			spinlock;
48	wait_queue_head_t		read_queue;
49	unsigned int			qlen;
50
51	/* send report */
52	struct mutex			lock;
53	bool				write_pending;
54	wait_queue_head_t		write_queue;
55	struct usb_request		*req;
56
57	int				minor;
58	struct cdev			cdev;
59	struct usb_function		func;
60
61	struct usb_ep			*in_ep;
62	struct usb_ep			*out_ep;
63};
64
65static inline struct f_hidg *func_to_hidg(struct usb_function *f)
66{
67	return container_of(f, struct f_hidg, func);
68}
69
70/*-------------------------------------------------------------------------*/
71/*                           Static descriptors                            */
72
73static struct usb_interface_descriptor hidg_interface_desc = {
74	.bLength		= sizeof hidg_interface_desc,
75	.bDescriptorType	= USB_DT_INTERFACE,
76	/* .bInterfaceNumber	= DYNAMIC */
77	.bAlternateSetting	= 0,
78	.bNumEndpoints		= 2,
79	.bInterfaceClass	= USB_CLASS_HID,
80	/* .bInterfaceSubClass	= DYNAMIC */
81	/* .bInterfaceProtocol	= DYNAMIC */
82	/* .iInterface		= DYNAMIC */
83};
84
85static struct hid_descriptor hidg_desc = {
86	.bLength			= sizeof hidg_desc,
87	.bDescriptorType		= HID_DT_HID,
88	.bcdHID				= 0x0101,
89	.bCountryCode			= 0x00,
90	.bNumDescriptors		= 0x1,
91	/*.desc[0].bDescriptorType	= DYNAMIC */
92	/*.desc[0].wDescriptorLenght	= DYNAMIC */
93};
94
95/* High-Speed Support */
96
97static struct usb_endpoint_descriptor hidg_hs_in_ep_desc = {
98	.bLength		= USB_DT_ENDPOINT_SIZE,
99	.bDescriptorType	= USB_DT_ENDPOINT,
100	.bEndpointAddress	= USB_DIR_IN,
101	.bmAttributes		= USB_ENDPOINT_XFER_INT,
102	/*.wMaxPacketSize	= DYNAMIC */
103	.bInterval		= 4, /* FIXME: Add this field in the
104				      * HID gadget configuration?
105				      * (struct hidg_func_descriptor)
106				      */
107};
108
109static struct usb_endpoint_descriptor hidg_hs_out_ep_desc = {
110	.bLength		= USB_DT_ENDPOINT_SIZE,
111	.bDescriptorType	= USB_DT_ENDPOINT,
112	.bEndpointAddress	= USB_DIR_OUT,
113	.bmAttributes		= USB_ENDPOINT_XFER_INT,
114	/*.wMaxPacketSize	= DYNAMIC */
115	.bInterval		= 4, /* FIXME: Add this field in the
116				      * HID gadget configuration?
117				      * (struct hidg_func_descriptor)
118				      */
119};
120
121static struct usb_descriptor_header *hidg_hs_descriptors[] = {
122	(struct usb_descriptor_header *)&hidg_interface_desc,
123	(struct usb_descriptor_header *)&hidg_desc,
124	(struct usb_descriptor_header *)&hidg_hs_in_ep_desc,
125	(struct usb_descriptor_header *)&hidg_hs_out_ep_desc,
126	NULL,
127};
128
129/* Full-Speed Support */
130
131static struct usb_endpoint_descriptor hidg_fs_in_ep_desc = {
132	.bLength		= USB_DT_ENDPOINT_SIZE,
133	.bDescriptorType	= USB_DT_ENDPOINT,
134	.bEndpointAddress	= USB_DIR_IN,
135	.bmAttributes		= USB_ENDPOINT_XFER_INT,
136	/*.wMaxPacketSize	= DYNAMIC */
137	.bInterval		= 10, /* FIXME: Add this field in the
138				       * HID gadget configuration?
139				       * (struct hidg_func_descriptor)
140				       */
141};
142
143static struct usb_endpoint_descriptor hidg_fs_out_ep_desc = {
144	.bLength		= USB_DT_ENDPOINT_SIZE,
145	.bDescriptorType	= USB_DT_ENDPOINT,
146	.bEndpointAddress	= USB_DIR_OUT,
147	.bmAttributes		= USB_ENDPOINT_XFER_INT,
148	/*.wMaxPacketSize	= DYNAMIC */
149	.bInterval		= 10, /* FIXME: Add this field in the
150				       * HID gadget configuration?
151				       * (struct hidg_func_descriptor)
152				       */
153};
154
155static struct usb_descriptor_header *hidg_fs_descriptors[] = {
156	(struct usb_descriptor_header *)&hidg_interface_desc,
157	(struct usb_descriptor_header *)&hidg_desc,
158	(struct usb_descriptor_header *)&hidg_fs_in_ep_desc,
159	(struct usb_descriptor_header *)&hidg_fs_out_ep_desc,
160	NULL,
161};
162
163/*-------------------------------------------------------------------------*/
164/*                              Char Device                                */
165
166static ssize_t f_hidg_read(struct file *file, char __user *buffer,
167			size_t count, loff_t *ptr)
168{
169	struct f_hidg *hidg = file->private_data;
170	struct f_hidg_req_list *list;
171	struct usb_request *req;
172	unsigned long flags;
173	int ret;
174
175	if (!count)
176		return 0;
177
178	if (!access_ok(VERIFY_WRITE, buffer, count))
179		return -EFAULT;
180
181	spin_lock_irqsave(&hidg->spinlock, flags);
182
183#define READ_COND (!list_empty(&hidg->completed_out_req))
184
185	/* wait for at least one buffer to complete */
186	while (!READ_COND) {
187		spin_unlock_irqrestore(&hidg->spinlock, flags);
188		if (file->f_flags & O_NONBLOCK)
189			return -EAGAIN;
190
191		if (wait_event_interruptible(hidg->read_queue, READ_COND))
192			return -ERESTARTSYS;
193
194		spin_lock_irqsave(&hidg->spinlock, flags);
195	}
196
197	/* pick the first one */
198	list = list_first_entry(&hidg->completed_out_req,
199				struct f_hidg_req_list, list);
200	req = list->req;
201	count = min_t(unsigned int, count, req->actual - list->pos);
202	spin_unlock_irqrestore(&hidg->spinlock, flags);
203
204	/* copy to user outside spinlock */
205	count -= copy_to_user(buffer, req->buf + list->pos, count);
206	list->pos += count;
207
208	/*
209	 * if this request is completely handled and transfered to
210	 * userspace, remove its entry from the list and requeue it
211	 * again. Otherwise, we will revisit it again upon the next
212	 * call, taking into account its current read position.
213	 */
214	if (list->pos == req->actual) {
215		spin_lock_irqsave(&hidg->spinlock, flags);
216		list_del(&list->list);
217		kfree(list);
218		spin_unlock_irqrestore(&hidg->spinlock, flags);
219
220		req->length = hidg->report_length;
221		ret = usb_ep_queue(hidg->out_ep, req, GFP_KERNEL);
222		if (ret < 0)
223			return ret;
224	}
225
226	return count;
227}
228
229static void f_hidg_req_complete(struct usb_ep *ep, struct usb_request *req)
230{
231	struct f_hidg *hidg = (struct f_hidg *)ep->driver_data;
232
233	if (req->status != 0) {
234		ERROR(hidg->func.config->cdev,
235			"End Point Request ERROR: %d\n", req->status);
236	}
237
238	hidg->write_pending = 0;
239	wake_up(&hidg->write_queue);
240}
241
242static ssize_t f_hidg_write(struct file *file, const char __user *buffer,
243			    size_t count, loff_t *offp)
244{
245	struct f_hidg *hidg  = file->private_data;
246	ssize_t status = -ENOMEM;
247
248	if (!access_ok(VERIFY_READ, buffer, count))
249		return -EFAULT;
250
251	mutex_lock(&hidg->lock);
252
253#define WRITE_COND (!hidg->write_pending)
254
255	/* write queue */
256	while (!WRITE_COND) {
257		mutex_unlock(&hidg->lock);
258		if (file->f_flags & O_NONBLOCK)
259			return -EAGAIN;
260
261		if (wait_event_interruptible_exclusive(
262				hidg->write_queue, WRITE_COND))
263			return -ERESTARTSYS;
264
265		mutex_lock(&hidg->lock);
266	}
267
268	count  = min_t(unsigned, count, hidg->report_length);
269	status = copy_from_user(hidg->req->buf, buffer, count);
270
271	if (status != 0) {
272		ERROR(hidg->func.config->cdev,
273			"copy_from_user error\n");
274		mutex_unlock(&hidg->lock);
275		return -EINVAL;
276	}
277
278	hidg->req->status   = 0;
279	hidg->req->zero     = 0;
280	hidg->req->length   = count;
281	hidg->req->complete = f_hidg_req_complete;
282	hidg->req->context  = hidg;
283	hidg->write_pending = 1;
284
285	status = usb_ep_queue(hidg->in_ep, hidg->req, GFP_ATOMIC);
286	if (status < 0) {
287		ERROR(hidg->func.config->cdev,
288			"usb_ep_queue error on int endpoint %zd\n", status);
289		hidg->write_pending = 0;
290		wake_up(&hidg->write_queue);
291	} else {
292		status = count;
293	}
294
295	mutex_unlock(&hidg->lock);
296
297	return status;
298}
299
300static unsigned int f_hidg_poll(struct file *file, poll_table *wait)
301{
302	struct f_hidg	*hidg  = file->private_data;
303	unsigned int	ret = 0;
304
305	poll_wait(file, &hidg->read_queue, wait);
306	poll_wait(file, &hidg->write_queue, wait);
307
308	if (WRITE_COND)
309		ret |= POLLOUT | POLLWRNORM;
310
311	if (READ_COND)
312		ret |= POLLIN | POLLRDNORM;
313
314	return ret;
315}
316
317#undef WRITE_COND
318#undef READ_COND
319
320static int f_hidg_release(struct inode *inode, struct file *fd)
321{
322	fd->private_data = NULL;
323	return 0;
324}
325
326static int f_hidg_open(struct inode *inode, struct file *fd)
327{
328	struct f_hidg *hidg =
329		container_of(inode->i_cdev, struct f_hidg, cdev);
330
331	fd->private_data = hidg;
332
333	return 0;
334}
335
336/*-------------------------------------------------------------------------*/
337/*                                usb_function                             */
338
339static inline struct usb_request *hidg_alloc_ep_req(struct usb_ep *ep,
340						    unsigned length)
341{
342	return alloc_ep_req(ep, length, length);
343}
344
345static void hidg_set_report_complete(struct usb_ep *ep, struct usb_request *req)
346{
347	struct f_hidg *hidg = (struct f_hidg *) req->context;
348	struct f_hidg_req_list *req_list;
349	unsigned long flags;
350
351	req_list = kzalloc(sizeof(*req_list), GFP_ATOMIC);
352	if (!req_list)
353		return;
354
355	req_list->req = req;
356
357	spin_lock_irqsave(&hidg->spinlock, flags);
358	list_add_tail(&req_list->list, &hidg->completed_out_req);
359	spin_unlock_irqrestore(&hidg->spinlock, flags);
360
361	wake_up(&hidg->read_queue);
362}
363
364static int hidg_setup(struct usb_function *f,
365		const struct usb_ctrlrequest *ctrl)
366{
367	struct f_hidg			*hidg = func_to_hidg(f);
368	struct usb_composite_dev	*cdev = f->config->cdev;
369	struct usb_request		*req  = cdev->req;
370	int status = 0;
371	__u16 value, length;
372
373	value	= __le16_to_cpu(ctrl->wValue);
374	length	= __le16_to_cpu(ctrl->wLength);
375
376	VDBG(cdev,
377	     "%s crtl_request : bRequestType:0x%x bRequest:0x%x Value:0x%x\n",
378	     __func__, ctrl->bRequestType, ctrl->bRequest, value);
379
380	switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
381	case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
382		  | HID_REQ_GET_REPORT):
383		VDBG(cdev, "get_report\n");
384
385		/* send an empty report */
386		length = min_t(unsigned, length, hidg->report_length);
387		memset(req->buf, 0x0, length);
388
389		goto respond;
390		break;
391
392	case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
393		  | HID_REQ_GET_PROTOCOL):
394		VDBG(cdev, "get_protocol\n");
395		goto stall;
396		break;
397
398	case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
399		  | HID_REQ_SET_REPORT):
400		VDBG(cdev, "set_report | wLenght=%d\n", ctrl->wLength);
401		goto stall;
402		break;
403
404	case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
405		  | HID_REQ_SET_PROTOCOL):
406		VDBG(cdev, "set_protocol\n");
407		goto stall;
408		break;
409
410	case ((USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_INTERFACE) << 8
411		  | USB_REQ_GET_DESCRIPTOR):
412		switch (value >> 8) {
413		case HID_DT_HID:
414			VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: HID\n");
415			length = min_t(unsigned short, length,
416						   hidg_desc.bLength);
417			memcpy(req->buf, &hidg_desc, length);
418			goto respond;
419			break;
420		case HID_DT_REPORT:
421			VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: REPORT\n");
422			length = min_t(unsigned short, length,
423						   hidg->report_desc_length);
424			memcpy(req->buf, hidg->report_desc, length);
425			goto respond;
426			break;
427
428		default:
429			VDBG(cdev, "Unknown descriptor request 0x%x\n",
430				 value >> 8);
431			goto stall;
432			break;
433		}
434		break;
435
436	default:
437		VDBG(cdev, "Unknown request 0x%x\n",
438			 ctrl->bRequest);
439		goto stall;
440		break;
441	}
442
443stall:
444	return -EOPNOTSUPP;
445
446respond:
447	req->zero = 0;
448	req->length = length;
449	status = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
450	if (status < 0)
451		ERROR(cdev, "usb_ep_queue error on ep0 %d\n", value);
452	return status;
453}
454
455static void hidg_disable(struct usb_function *f)
456{
457	struct f_hidg *hidg = func_to_hidg(f);
458	struct f_hidg_req_list *list, *next;
459
460	usb_ep_disable(hidg->in_ep);
461	hidg->in_ep->driver_data = NULL;
462
463	usb_ep_disable(hidg->out_ep);
464	hidg->out_ep->driver_data = NULL;
465
466	list_for_each_entry_safe(list, next, &hidg->completed_out_req, list) {
467		list_del(&list->list);
468		kfree(list);
469	}
470}
471
472static int hidg_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
473{
474	struct usb_composite_dev		*cdev = f->config->cdev;
475	struct f_hidg				*hidg = func_to_hidg(f);
476	int i, status = 0;
477
478	VDBG(cdev, "hidg_set_alt intf:%d alt:%d\n", intf, alt);
479
480	if (hidg->in_ep != NULL) {
481		/* restart endpoint */
482		if (hidg->in_ep->driver_data != NULL)
483			usb_ep_disable(hidg->in_ep);
484
485		status = config_ep_by_speed(f->config->cdev->gadget, f,
486					    hidg->in_ep);
487		if (status) {
488			ERROR(cdev, "config_ep_by_speed FAILED!\n");
489			goto fail;
490		}
491		status = usb_ep_enable(hidg->in_ep);
492		if (status < 0) {
493			ERROR(cdev, "Enable IN endpoint FAILED!\n");
494			goto fail;
495		}
496		hidg->in_ep->driver_data = hidg;
497	}
498
499
500	if (hidg->out_ep != NULL) {
501		/* restart endpoint */
502		if (hidg->out_ep->driver_data != NULL)
503			usb_ep_disable(hidg->out_ep);
504
505		status = config_ep_by_speed(f->config->cdev->gadget, f,
506					    hidg->out_ep);
507		if (status) {
508			ERROR(cdev, "config_ep_by_speed FAILED!\n");
509			goto fail;
510		}
511		status = usb_ep_enable(hidg->out_ep);
512		if (status < 0) {
513			ERROR(cdev, "Enable IN endpoint FAILED!\n");
514			goto fail;
515		}
516		hidg->out_ep->driver_data = hidg;
517
518		/*
519		 * allocate a bunch of read buffers and queue them all at once.
520		 */
521		for (i = 0; i < hidg->qlen && status == 0; i++) {
522			struct usb_request *req =
523					hidg_alloc_ep_req(hidg->out_ep,
524							  hidg->report_length);
525			if (req) {
526				req->complete = hidg_set_report_complete;
527				req->context  = hidg;
528				status = usb_ep_queue(hidg->out_ep, req,
529						      GFP_ATOMIC);
530				if (status)
531					ERROR(cdev, "%s queue req --> %d\n",
532						hidg->out_ep->name, status);
533			} else {
534				usb_ep_disable(hidg->out_ep);
535				hidg->out_ep->driver_data = NULL;
536				status = -ENOMEM;
537				goto fail;
538			}
539		}
540	}
541
542fail:
543	return status;
544}
545
546const struct file_operations f_hidg_fops = {
547	.owner		= THIS_MODULE,
548	.open		= f_hidg_open,
549	.release	= f_hidg_release,
550	.write		= f_hidg_write,
551	.read		= f_hidg_read,
552	.poll		= f_hidg_poll,
553	.llseek		= noop_llseek,
554};
555
556static int __init hidg_bind(struct usb_configuration *c, struct usb_function *f)
557{
558	struct usb_ep		*ep;
559	struct f_hidg		*hidg = func_to_hidg(f);
560	int			status;
561	dev_t			dev;
562
563	/* allocate instance-specific interface IDs, and patch descriptors */
564	status = usb_interface_id(c, f);
565	if (status < 0)
566		goto fail;
567	hidg_interface_desc.bInterfaceNumber = status;
568
569	/* allocate instance-specific endpoints */
570	status = -ENODEV;
571	ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_in_ep_desc);
572	if (!ep)
573		goto fail;
574	ep->driver_data = c->cdev;	/* claim */
575	hidg->in_ep = ep;
576
577	ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_out_ep_desc);
578	if (!ep)
579		goto fail;
580	ep->driver_data = c->cdev;	/* claim */
581	hidg->out_ep = ep;
582
583	/* preallocate request and buffer */
584	status = -ENOMEM;
585	hidg->req = usb_ep_alloc_request(hidg->in_ep, GFP_KERNEL);
586	if (!hidg->req)
587		goto fail;
588
589	hidg->req->buf = kmalloc(hidg->report_length, GFP_KERNEL);
590	if (!hidg->req->buf)
591		goto fail;
592
593	/* set descriptor dynamic values */
594	hidg_interface_desc.bInterfaceSubClass = hidg->bInterfaceSubClass;
595	hidg_interface_desc.bInterfaceProtocol = hidg->bInterfaceProtocol;
596	hidg_hs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
597	hidg_fs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
598	hidg_hs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
599	hidg_fs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
600	hidg_desc.desc[0].bDescriptorType = HID_DT_REPORT;
601	hidg_desc.desc[0].wDescriptorLength =
602		cpu_to_le16(hidg->report_desc_length);
603
604	hidg_hs_in_ep_desc.bEndpointAddress =
605		hidg_fs_in_ep_desc.bEndpointAddress;
606	hidg_hs_out_ep_desc.bEndpointAddress =
607		hidg_fs_out_ep_desc.bEndpointAddress;
608
609	status = usb_assign_descriptors(f, hidg_fs_descriptors,
610			hidg_hs_descriptors, NULL);
611	if (status)
612		goto fail;
613
614	mutex_init(&hidg->lock);
615	spin_lock_init(&hidg->spinlock);
616	init_waitqueue_head(&hidg->write_queue);
617	init_waitqueue_head(&hidg->read_queue);
618	INIT_LIST_HEAD(&hidg->completed_out_req);
619
620	/* create char device */
621	cdev_init(&hidg->cdev, &f_hidg_fops);
622	dev = MKDEV(major, hidg->minor);
623	status = cdev_add(&hidg->cdev, dev, 1);
624	if (status)
625		goto fail_free_descs;
626
627	device_create(hidg_class, NULL, dev, NULL, "%s%d", "hidg", hidg->minor);
628
629	return 0;
630
631fail_free_descs:
632	usb_free_all_descriptors(f);
633fail:
634	ERROR(f->config->cdev, "hidg_bind FAILED\n");
635	if (hidg->req != NULL) {
636		kfree(hidg->req->buf);
637		if (hidg->in_ep != NULL)
638			usb_ep_free_request(hidg->in_ep, hidg->req);
639	}
640
641	return status;
642}
643
644static void hidg_unbind(struct usb_configuration *c, struct usb_function *f)
645{
646	struct f_hidg *hidg = func_to_hidg(f);
647
648	device_destroy(hidg_class, MKDEV(major, hidg->minor));
649	cdev_del(&hidg->cdev);
650
651	/* disable/free request and end point */
652	usb_ep_disable(hidg->in_ep);
653	usb_ep_dequeue(hidg->in_ep, hidg->req);
654	kfree(hidg->req->buf);
655	usb_ep_free_request(hidg->in_ep, hidg->req);
656
657	usb_free_all_descriptors(f);
658
659	kfree(hidg->report_desc);
660	kfree(hidg);
661}
662
663/*-------------------------------------------------------------------------*/
664/*                                 Strings                                 */
665
666#define CT_FUNC_HID_IDX	0
667
668static struct usb_string ct_func_string_defs[] = {
669	[CT_FUNC_HID_IDX].s	= "HID Interface",
670	{},			/* end of list */
671};
672
673static struct usb_gadget_strings ct_func_string_table = {
674	.language	= 0x0409,	/* en-US */
675	.strings	= ct_func_string_defs,
676};
677
678static struct usb_gadget_strings *ct_func_strings[] = {
679	&ct_func_string_table,
680	NULL,
681};
682
683/*-------------------------------------------------------------------------*/
684/*                             usb_configuration                           */
685
686int __init hidg_bind_config(struct usb_configuration *c,
687			    struct hidg_func_descriptor *fdesc, int index)
688{
689	struct f_hidg *hidg;
690	int status;
691
692	if (index >= minors)
693		return -ENOENT;
694
695	/* maybe allocate device-global string IDs, and patch descriptors */
696	if (ct_func_string_defs[CT_FUNC_HID_IDX].id == 0) {
697		status = usb_string_id(c->cdev);
698		if (status < 0)
699			return status;
700		ct_func_string_defs[CT_FUNC_HID_IDX].id = status;
701		hidg_interface_desc.iInterface = status;
702	}
703
704	/* allocate and initialize one new instance */
705	hidg = kzalloc(sizeof *hidg, GFP_KERNEL);
706	if (!hidg)
707		return -ENOMEM;
708
709	hidg->minor = index;
710	hidg->bInterfaceSubClass = fdesc->subclass;
711	hidg->bInterfaceProtocol = fdesc->protocol;
712	hidg->report_length = fdesc->report_length;
713	hidg->report_desc_length = fdesc->report_desc_length;
714	hidg->report_desc = kmemdup(fdesc->report_desc,
715				    fdesc->report_desc_length,
716				    GFP_KERNEL);
717	if (!hidg->report_desc) {
718		kfree(hidg);
719		return -ENOMEM;
720	}
721
722	hidg->func.name    = "hid";
723	hidg->func.strings = ct_func_strings;
724	hidg->func.bind    = hidg_bind;
725	hidg->func.unbind  = hidg_unbind;
726	hidg->func.set_alt = hidg_set_alt;
727	hidg->func.disable = hidg_disable;
728	hidg->func.setup   = hidg_setup;
729
730	/* this could me made configurable at some point */
731	hidg->qlen	   = 4;
732
733	status = usb_add_function(c, &hidg->func);
734	if (status)
735		kfree(hidg);
736
737	return status;
738}
739
740int __init ghid_setup(struct usb_gadget *g, int count)
741{
742	int status;
743	dev_t dev;
744
745	hidg_class = class_create(THIS_MODULE, "hidg");
746
747	status = alloc_chrdev_region(&dev, 0, count, "hidg");
748	if (!status) {
749		major = MAJOR(dev);
750		minors = count;
751	}
752
753	return status;
754}
755
756void ghid_cleanup(void)
757{
758	if (major) {
759		unregister_chrdev_region(MKDEV(major, 0), minors);
760		major = minors = 0;
761	}
762
763	class_destroy(hidg_class);
764	hidg_class = NULL;
765}
766