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