1 /*
2  * f_loopback.c - USB peripheral loopback configuration driver
3  *
4  * Copyright (C) 2003-2008 David Brownell
5  * Copyright (C) 2008 by Nokia Corporation
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  */
12 
13 /* #define VERBOSE_DEBUG */
14 
15 #include <linux/slab.h>
16 #include <linux/kernel.h>
17 #include <linux/device.h>
18 #include <linux/module.h>
19 #include <linux/err.h>
20 #include <linux/usb/composite.h>
21 
22 #include "g_zero.h"
23 #include "u_f.h"
24 
25 /*
26  * LOOPBACK FUNCTION ... a testing vehicle for USB peripherals,
27  *
28  * This takes messages of various sizes written OUT to a device, and loops
29  * them back so they can be read IN from it.  It has been used by certain
30  * test applications.  It supports limited testing of data queueing logic.
31  *
32  *
33  * This is currently packaged as a configuration driver, which can't be
34  * combined with other functions to make composite devices.  However, it
35  * can be combined with other independent configurations.
36  */
37 struct f_loopback {
38 	struct usb_function	function;
39 
40 	struct usb_ep		*in_ep;
41 	struct usb_ep		*out_ep;
42 };
43 
44 static inline struct f_loopback *func_to_loop(struct usb_function *f)
45 {
46 	return container_of(f, struct f_loopback, function);
47 }
48 
49 static unsigned qlen;
50 static unsigned buflen;
51 
52 /*-------------------------------------------------------------------------*/
53 
54 static struct usb_interface_descriptor loopback_intf = {
55 	.bLength =		sizeof loopback_intf,
56 	.bDescriptorType =	USB_DT_INTERFACE,
57 
58 	.bNumEndpoints =	2,
59 	.bInterfaceClass =	USB_CLASS_VENDOR_SPEC,
60 	/* .iInterface = DYNAMIC */
61 };
62 
63 /* full speed support: */
64 
65 static struct usb_endpoint_descriptor fs_loop_source_desc = {
66 	.bLength =		USB_DT_ENDPOINT_SIZE,
67 	.bDescriptorType =	USB_DT_ENDPOINT,
68 
69 	.bEndpointAddress =	USB_DIR_IN,
70 	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
71 };
72 
73 static struct usb_endpoint_descriptor fs_loop_sink_desc = {
74 	.bLength =		USB_DT_ENDPOINT_SIZE,
75 	.bDescriptorType =	USB_DT_ENDPOINT,
76 
77 	.bEndpointAddress =	USB_DIR_OUT,
78 	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
79 };
80 
81 static struct usb_descriptor_header *fs_loopback_descs[] = {
82 	(struct usb_descriptor_header *) &loopback_intf,
83 	(struct usb_descriptor_header *) &fs_loop_sink_desc,
84 	(struct usb_descriptor_header *) &fs_loop_source_desc,
85 	NULL,
86 };
87 
88 /* high speed support: */
89 
90 static struct usb_endpoint_descriptor hs_loop_source_desc = {
91 	.bLength =		USB_DT_ENDPOINT_SIZE,
92 	.bDescriptorType =	USB_DT_ENDPOINT,
93 
94 	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
95 	.wMaxPacketSize =	cpu_to_le16(512),
96 };
97 
98 static struct usb_endpoint_descriptor hs_loop_sink_desc = {
99 	.bLength =		USB_DT_ENDPOINT_SIZE,
100 	.bDescriptorType =	USB_DT_ENDPOINT,
101 
102 	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
103 	.wMaxPacketSize =	cpu_to_le16(512),
104 };
105 
106 static struct usb_descriptor_header *hs_loopback_descs[] = {
107 	(struct usb_descriptor_header *) &loopback_intf,
108 	(struct usb_descriptor_header *) &hs_loop_source_desc,
109 	(struct usb_descriptor_header *) &hs_loop_sink_desc,
110 	NULL,
111 };
112 
113 /* super speed support: */
114 
115 static struct usb_endpoint_descriptor ss_loop_source_desc = {
116 	.bLength =		USB_DT_ENDPOINT_SIZE,
117 	.bDescriptorType =	USB_DT_ENDPOINT,
118 
119 	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
120 	.wMaxPacketSize =	cpu_to_le16(1024),
121 };
122 
123 static struct usb_ss_ep_comp_descriptor ss_loop_source_comp_desc = {
124 	.bLength =		USB_DT_SS_EP_COMP_SIZE,
125 	.bDescriptorType =	USB_DT_SS_ENDPOINT_COMP,
126 	.bMaxBurst =		0,
127 	.bmAttributes =		0,
128 	.wBytesPerInterval =	0,
129 };
130 
131 static struct usb_endpoint_descriptor ss_loop_sink_desc = {
132 	.bLength =		USB_DT_ENDPOINT_SIZE,
133 	.bDescriptorType =	USB_DT_ENDPOINT,
134 
135 	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
136 	.wMaxPacketSize =	cpu_to_le16(1024),
137 };
138 
139 static struct usb_ss_ep_comp_descriptor ss_loop_sink_comp_desc = {
140 	.bLength =		USB_DT_SS_EP_COMP_SIZE,
141 	.bDescriptorType =	USB_DT_SS_ENDPOINT_COMP,
142 	.bMaxBurst =		0,
143 	.bmAttributes =		0,
144 	.wBytesPerInterval =	0,
145 };
146 
147 static struct usb_descriptor_header *ss_loopback_descs[] = {
148 	(struct usb_descriptor_header *) &loopback_intf,
149 	(struct usb_descriptor_header *) &ss_loop_source_desc,
150 	(struct usb_descriptor_header *) &ss_loop_source_comp_desc,
151 	(struct usb_descriptor_header *) &ss_loop_sink_desc,
152 	(struct usb_descriptor_header *) &ss_loop_sink_comp_desc,
153 	NULL,
154 };
155 
156 /* function-specific strings: */
157 
158 static struct usb_string strings_loopback[] = {
159 	[0].s = "loop input to output",
160 	{  }			/* end of list */
161 };
162 
163 static struct usb_gadget_strings stringtab_loop = {
164 	.language	= 0x0409,	/* en-us */
165 	.strings	= strings_loopback,
166 };
167 
168 static struct usb_gadget_strings *loopback_strings[] = {
169 	&stringtab_loop,
170 	NULL,
171 };
172 
173 /*-------------------------------------------------------------------------*/
174 
175 static int loopback_bind(struct usb_configuration *c, struct usb_function *f)
176 {
177 	struct usb_composite_dev *cdev = c->cdev;
178 	struct f_loopback	*loop = func_to_loop(f);
179 	int			id;
180 	int ret;
181 
182 	/* allocate interface ID(s) */
183 	id = usb_interface_id(c, f);
184 	if (id < 0)
185 		return id;
186 	loopback_intf.bInterfaceNumber = id;
187 
188 	id = usb_string_id(cdev);
189 	if (id < 0)
190 		return id;
191 	strings_loopback[0].id = id;
192 	loopback_intf.iInterface = id;
193 
194 	/* allocate endpoints */
195 
196 	loop->in_ep = usb_ep_autoconfig(cdev->gadget, &fs_loop_source_desc);
197 	if (!loop->in_ep) {
198 autoconf_fail:
199 		ERROR(cdev, "%s: can't autoconfigure on %s\n",
200 			f->name, cdev->gadget->name);
201 		return -ENODEV;
202 	}
203 	loop->in_ep->driver_data = cdev;	/* claim */
204 
205 	loop->out_ep = usb_ep_autoconfig(cdev->gadget, &fs_loop_sink_desc);
206 	if (!loop->out_ep)
207 		goto autoconf_fail;
208 	loop->out_ep->driver_data = cdev;	/* claim */
209 
210 	/* support high speed hardware */
211 	hs_loop_source_desc.bEndpointAddress =
212 		fs_loop_source_desc.bEndpointAddress;
213 	hs_loop_sink_desc.bEndpointAddress = fs_loop_sink_desc.bEndpointAddress;
214 
215 	/* support super speed hardware */
216 	ss_loop_source_desc.bEndpointAddress =
217 		fs_loop_source_desc.bEndpointAddress;
218 	ss_loop_sink_desc.bEndpointAddress = fs_loop_sink_desc.bEndpointAddress;
219 
220 	ret = usb_assign_descriptors(f, fs_loopback_descs, hs_loopback_descs,
221 			ss_loopback_descs);
222 	if (ret)
223 		return ret;
224 
225 	DBG(cdev, "%s speed %s: IN/%s, OUT/%s\n",
226 	    (gadget_is_superspeed(c->cdev->gadget) ? "super" :
227 	     (gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full")),
228 			f->name, loop->in_ep->name, loop->out_ep->name);
229 	return 0;
230 }
231 
232 static void lb_free_func(struct usb_function *f)
233 {
234 	struct f_lb_opts *opts;
235 
236 	opts = container_of(f->fi, struct f_lb_opts, func_inst);
237 
238 	mutex_lock(&opts->lock);
239 	opts->refcnt--;
240 	mutex_unlock(&opts->lock);
241 
242 	usb_free_all_descriptors(f);
243 	kfree(func_to_loop(f));
244 }
245 
246 static void loopback_complete(struct usb_ep *ep, struct usb_request *req)
247 {
248 	struct f_loopback	*loop = ep->driver_data;
249 	struct usb_composite_dev *cdev = loop->function.config->cdev;
250 	int			status = req->status;
251 
252 	switch (status) {
253 
254 	case 0:				/* normal completion? */
255 		if (ep == loop->out_ep) {
256 			/* loop this OUT packet back IN to the host */
257 			req->zero = (req->actual < req->length);
258 			req->length = req->actual;
259 			status = usb_ep_queue(loop->in_ep, req, GFP_ATOMIC);
260 			if (status == 0)
261 				return;
262 
263 			/* "should never get here" */
264 			ERROR(cdev, "can't loop %s to %s: %d\n",
265 				ep->name, loop->in_ep->name,
266 				status);
267 		}
268 
269 		/* queue the buffer for some later OUT packet */
270 		req->length = buflen;
271 		status = usb_ep_queue(loop->out_ep, req, GFP_ATOMIC);
272 		if (status == 0)
273 			return;
274 
275 		/* "should never get here" */
276 		/* FALLTHROUGH */
277 
278 	default:
279 		ERROR(cdev, "%s loop complete --> %d, %d/%d\n", ep->name,
280 				status, req->actual, req->length);
281 		/* FALLTHROUGH */
282 
283 	/* NOTE:  since this driver doesn't maintain an explicit record
284 	 * of requests it submitted (just maintains qlen count), we
285 	 * rely on the hardware driver to clean up on disconnect or
286 	 * endpoint disable.
287 	 */
288 	case -ECONNABORTED:		/* hardware forced ep reset */
289 	case -ECONNRESET:		/* request dequeued */
290 	case -ESHUTDOWN:		/* disconnect from host */
291 		free_ep_req(ep, req);
292 		return;
293 	}
294 }
295 
296 static void disable_loopback(struct f_loopback *loop)
297 {
298 	struct usb_composite_dev	*cdev;
299 
300 	cdev = loop->function.config->cdev;
301 	disable_endpoints(cdev, loop->in_ep, loop->out_ep, NULL, NULL, NULL,
302 			NULL);
303 	VDBG(cdev, "%s disabled\n", loop->function.name);
304 }
305 
306 static inline struct usb_request *lb_alloc_ep_req(struct usb_ep *ep, int len)
307 {
308 	return alloc_ep_req(ep, len, buflen);
309 }
310 
311 static int
312 enable_loopback(struct usb_composite_dev *cdev, struct f_loopback *loop)
313 {
314 	int					result = 0;
315 	struct usb_ep				*ep;
316 	struct usb_request			*req;
317 	unsigned				i;
318 
319 	/* one endpoint writes data back IN to the host */
320 	ep = loop->in_ep;
321 	result = config_ep_by_speed(cdev->gadget, &(loop->function), ep);
322 	if (result)
323 		return result;
324 	result = usb_ep_enable(ep);
325 	if (result < 0)
326 		return result;
327 	ep->driver_data = loop;
328 
329 	/* one endpoint just reads OUT packets */
330 	ep = loop->out_ep;
331 	result = config_ep_by_speed(cdev->gadget, &(loop->function), ep);
332 	if (result)
333 		goto fail0;
334 
335 	result = usb_ep_enable(ep);
336 	if (result < 0) {
337 fail0:
338 		ep = loop->in_ep;
339 		usb_ep_disable(ep);
340 		ep->driver_data = NULL;
341 		return result;
342 	}
343 	ep->driver_data = loop;
344 
345 	/* allocate a bunch of read buffers and queue them all at once.
346 	 * we buffer at most 'qlen' transfers; fewer if any need more
347 	 * than 'buflen' bytes each.
348 	 */
349 	for (i = 0; i < qlen && result == 0; i++) {
350 		req = lb_alloc_ep_req(ep, 0);
351 		if (req) {
352 			req->complete = loopback_complete;
353 			result = usb_ep_queue(ep, req, GFP_ATOMIC);
354 			if (result)
355 				ERROR(cdev, "%s queue req --> %d\n",
356 						ep->name, result);
357 		} else {
358 			usb_ep_disable(ep);
359 			ep->driver_data = NULL;
360 			result = -ENOMEM;
361 			goto fail0;
362 		}
363 	}
364 
365 	DBG(cdev, "%s enabled\n", loop->function.name);
366 	return result;
367 }
368 
369 static int loopback_set_alt(struct usb_function *f,
370 		unsigned intf, unsigned alt)
371 {
372 	struct f_loopback	*loop = func_to_loop(f);
373 	struct usb_composite_dev *cdev = f->config->cdev;
374 
375 	/* we know alt is zero */
376 	if (loop->in_ep->driver_data)
377 		disable_loopback(loop);
378 	return enable_loopback(cdev, loop);
379 }
380 
381 static void loopback_disable(struct usb_function *f)
382 {
383 	struct f_loopback	*loop = func_to_loop(f);
384 
385 	disable_loopback(loop);
386 }
387 
388 static struct usb_function *loopback_alloc(struct usb_function_instance *fi)
389 {
390 	struct f_loopback	*loop;
391 	struct f_lb_opts	*lb_opts;
392 
393 	loop = kzalloc(sizeof *loop, GFP_KERNEL);
394 	if (!loop)
395 		return ERR_PTR(-ENOMEM);
396 
397 	lb_opts = container_of(fi, struct f_lb_opts, func_inst);
398 
399 	mutex_lock(&lb_opts->lock);
400 	lb_opts->refcnt++;
401 	mutex_unlock(&lb_opts->lock);
402 
403 	buflen = lb_opts->bulk_buflen;
404 	qlen = lb_opts->qlen;
405 	if (!qlen)
406 		qlen = 32;
407 
408 	loop->function.name = "loopback";
409 	loop->function.bind = loopback_bind;
410 	loop->function.set_alt = loopback_set_alt;
411 	loop->function.disable = loopback_disable;
412 	loop->function.strings = loopback_strings;
413 
414 	loop->function.free_func = lb_free_func;
415 
416 	return &loop->function;
417 }
418 
419 static inline struct f_lb_opts *to_f_lb_opts(struct config_item *item)
420 {
421 	return container_of(to_config_group(item), struct f_lb_opts,
422 			    func_inst.group);
423 }
424 
425 CONFIGFS_ATTR_STRUCT(f_lb_opts);
426 CONFIGFS_ATTR_OPS(f_lb_opts);
427 
428 static void lb_attr_release(struct config_item *item)
429 {
430 	struct f_lb_opts *lb_opts = to_f_lb_opts(item);
431 
432 	usb_put_function_instance(&lb_opts->func_inst);
433 }
434 
435 static struct configfs_item_operations lb_item_ops = {
436 	.release		= lb_attr_release,
437 	.show_attribute		= f_lb_opts_attr_show,
438 	.store_attribute	= f_lb_opts_attr_store,
439 };
440 
441 static ssize_t f_lb_opts_qlen_show(struct f_lb_opts *opts, char *page)
442 {
443 	int result;
444 
445 	mutex_lock(&opts->lock);
446 	result = sprintf(page, "%d", opts->qlen);
447 	mutex_unlock(&opts->lock);
448 
449 	return result;
450 }
451 
452 static ssize_t f_lb_opts_qlen_store(struct f_lb_opts *opts,
453 				    const char *page, size_t len)
454 {
455 	int ret;
456 	u32 num;
457 
458 	mutex_lock(&opts->lock);
459 	if (opts->refcnt) {
460 		ret = -EBUSY;
461 		goto end;
462 	}
463 
464 	ret = kstrtou32(page, 0, &num);
465 	if (ret)
466 		goto end;
467 
468 	opts->qlen = num;
469 	ret = len;
470 end:
471 	mutex_unlock(&opts->lock);
472 	return ret;
473 }
474 
475 static struct f_lb_opts_attribute f_lb_opts_qlen =
476 	__CONFIGFS_ATTR(qlen, S_IRUGO | S_IWUSR,
477 			f_lb_opts_qlen_show,
478 			f_lb_opts_qlen_store);
479 
480 static ssize_t f_lb_opts_bulk_buflen_show(struct f_lb_opts *opts, char *page)
481 {
482 	int result;
483 
484 	mutex_lock(&opts->lock);
485 	result = sprintf(page, "%d", opts->bulk_buflen);
486 	mutex_unlock(&opts->lock);
487 
488 	return result;
489 }
490 
491 static ssize_t f_lb_opts_bulk_buflen_store(struct f_lb_opts *opts,
492 				    const char *page, size_t len)
493 {
494 	int ret;
495 	u32 num;
496 
497 	mutex_lock(&opts->lock);
498 	if (opts->refcnt) {
499 		ret = -EBUSY;
500 		goto end;
501 	}
502 
503 	ret = kstrtou32(page, 0, &num);
504 	if (ret)
505 		goto end;
506 
507 	opts->bulk_buflen = num;
508 	ret = len;
509 end:
510 	mutex_unlock(&opts->lock);
511 	return ret;
512 }
513 
514 static struct f_lb_opts_attribute f_lb_opts_bulk_buflen =
515 	__CONFIGFS_ATTR(buflen, S_IRUGO | S_IWUSR,
516 			f_lb_opts_bulk_buflen_show,
517 			f_lb_opts_bulk_buflen_store);
518 
519 static struct configfs_attribute *lb_attrs[] = {
520 	&f_lb_opts_qlen.attr,
521 	&f_lb_opts_bulk_buflen.attr,
522 	NULL,
523 };
524 
525 static struct config_item_type lb_func_type = {
526 	.ct_item_ops    = &lb_item_ops,
527 	.ct_attrs	= lb_attrs,
528 	.ct_owner       = THIS_MODULE,
529 };
530 
531 static void lb_free_instance(struct usb_function_instance *fi)
532 {
533 	struct f_lb_opts *lb_opts;
534 
535 	lb_opts = container_of(fi, struct f_lb_opts, func_inst);
536 	kfree(lb_opts);
537 }
538 
539 static struct usb_function_instance *loopback_alloc_instance(void)
540 {
541 	struct f_lb_opts *lb_opts;
542 
543 	lb_opts = kzalloc(sizeof(*lb_opts), GFP_KERNEL);
544 	if (!lb_opts)
545 		return ERR_PTR(-ENOMEM);
546 	mutex_init(&lb_opts->lock);
547 	lb_opts->func_inst.free_func_inst = lb_free_instance;
548 	lb_opts->bulk_buflen = GZERO_BULK_BUFLEN;
549 	lb_opts->qlen = GZERO_QLEN;
550 
551 	config_group_init_type_name(&lb_opts->func_inst.group, "",
552 				    &lb_func_type);
553 
554 	return  &lb_opts->func_inst;
555 }
556 DECLARE_USB_FUNCTION(Loopback, loopback_alloc_instance, loopback_alloc);
557 
558 int __init lb_modinit(void)
559 {
560 	int ret;
561 
562 	ret = usb_function_register(&Loopbackusb_func);
563 	if (ret)
564 		return ret;
565 	return ret;
566 }
567 void __exit lb_modexit(void)
568 {
569 	usb_function_unregister(&Loopbackusb_func);
570 }
571 
572 MODULE_LICENSE("GPL");
573