blob: b9d8f05300582d267a1878b3f151b27af013570e [file] [log] [blame]
David Brownelle5760fd2008-06-19 17:55:35 -07001/*
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.
David Brownelle5760fd2008-06-19 17:55:35 -070011 */
12
13/* #define VERBOSE_DEBUG */
14
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
David Brownelle5760fd2008-06-19 17:55:35 -070016#include <linux/kernel.h>
David Brownelle5760fd2008-06-19 17:55:35 -070017#include <linux/device.h>
Sebastian Andrzej Siewiorcf9a08a2012-12-23 21:10:01 +010018#include <linux/module.h>
19#include <linux/err.h>
20#include <linux/usb/composite.h>
David Brownelle5760fd2008-06-19 17:55:35 -070021
22#include "g_zero.h"
Andrzej Pietrasiewicz1efd54e2013-11-07 08:41:26 +010023#include "u_f.h"
David Brownelle5760fd2008-06-19 17:55:35 -070024
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.
David Brownelle5760fd2008-06-19 17:55:35 -070031 */
32struct f_loopback {
33 struct usb_function function;
34
35 struct usb_ep *in_ep;
36 struct usb_ep *out_ep;
37};
38
39static inline struct f_loopback *func_to_loop(struct usb_function *f)
40{
41 return container_of(f, struct f_loopback, function);
42}
43
Sebastian Andrzej Siewiorcf9a08a2012-12-23 21:10:01 +010044static unsigned qlen;
45static unsigned buflen;
David Brownelle5760fd2008-06-19 17:55:35 -070046
47/*-------------------------------------------------------------------------*/
48
49static 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
David Brownell7e75bc02008-08-18 17:41:31 -070060static struct usb_endpoint_descriptor fs_loop_source_desc = {
David Brownelle5760fd2008-06-19 17:55:35 -070061 .bLength = USB_DT_ENDPOINT_SIZE,
62 .bDescriptorType = USB_DT_ENDPOINT,
63
64 .bEndpointAddress = USB_DIR_IN,
65 .bmAttributes = USB_ENDPOINT_XFER_BULK,
66};
67
David Brownell7e75bc02008-08-18 17:41:31 -070068static struct usb_endpoint_descriptor fs_loop_sink_desc = {
David Brownelle5760fd2008-06-19 17:55:35 -070069 .bLength = USB_DT_ENDPOINT_SIZE,
70 .bDescriptorType = USB_DT_ENDPOINT,
71
72 .bEndpointAddress = USB_DIR_OUT,
73 .bmAttributes = USB_ENDPOINT_XFER_BULK,
74};
75
76static struct usb_descriptor_header *fs_loopback_descs[] = {
77 (struct usb_descriptor_header *) &loopback_intf,
David Brownell7e75bc02008-08-18 17:41:31 -070078 (struct usb_descriptor_header *) &fs_loop_sink_desc,
79 (struct usb_descriptor_header *) &fs_loop_source_desc,
David Brownelle5760fd2008-06-19 17:55:35 -070080 NULL,
81};
82
83/* high speed support: */
84
David Brownell7e75bc02008-08-18 17:41:31 -070085static struct usb_endpoint_descriptor hs_loop_source_desc = {
David Brownelle5760fd2008-06-19 17:55:35 -070086 .bLength = USB_DT_ENDPOINT_SIZE,
87 .bDescriptorType = USB_DT_ENDPOINT,
88
89 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Harvey Harrison551509d2009-02-11 14:11:36 -080090 .wMaxPacketSize = cpu_to_le16(512),
David Brownelle5760fd2008-06-19 17:55:35 -070091};
92
David Brownell7e75bc02008-08-18 17:41:31 -070093static struct usb_endpoint_descriptor hs_loop_sink_desc = {
David Brownelle5760fd2008-06-19 17:55:35 -070094 .bLength = USB_DT_ENDPOINT_SIZE,
95 .bDescriptorType = USB_DT_ENDPOINT,
96
97 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Harvey Harrison551509d2009-02-11 14:11:36 -080098 .wMaxPacketSize = cpu_to_le16(512),
David Brownelle5760fd2008-06-19 17:55:35 -070099};
100
101static struct usb_descriptor_header *hs_loopback_descs[] = {
102 (struct usb_descriptor_header *) &loopback_intf,
David Brownell7e75bc02008-08-18 17:41:31 -0700103 (struct usb_descriptor_header *) &hs_loop_source_desc,
104 (struct usb_descriptor_header *) &hs_loop_sink_desc,
David Brownelle5760fd2008-06-19 17:55:35 -0700105 NULL,
106};
107
Amit Blay57c97c02011-07-03 17:29:31 +0300108/* super speed support: */
109
110static 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
Jingoo Han80024182013-12-16 18:40:49 +0900118static struct usb_ss_ep_comp_descriptor ss_loop_source_comp_desc = {
Amit Blay57c97c02011-07-03 17:29:31 +0300119 .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
126static 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
Jingoo Han80024182013-12-16 18:40:49 +0900134static struct usb_ss_ep_comp_descriptor ss_loop_sink_comp_desc = {
Amit Blay57c97c02011-07-03 17:29:31 +0300135 .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
142static 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
David Brownelle5760fd2008-06-19 17:55:35 -0700151/* function-specific strings: */
152
153static struct usb_string strings_loopback[] = {
154 [0].s = "loop input to output",
155 { } /* end of list */
156};
157
158static struct usb_gadget_strings stringtab_loop = {
159 .language = 0x0409, /* en-us */
160 .strings = strings_loopback,
161};
162
163static struct usb_gadget_strings *loopback_strings[] = {
164 &stringtab_loop,
165 NULL,
166};
167
168/*-------------------------------------------------------------------------*/
169
Sebastian Andrzej Siewiorcf9a08a2012-12-23 21:10:01 +0100170static int loopback_bind(struct usb_configuration *c, struct usb_function *f)
David Brownelle5760fd2008-06-19 17:55:35 -0700171{
172 struct usb_composite_dev *cdev = c->cdev;
173 struct f_loopback *loop = func_to_loop(f);
174 int id;
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200175 int ret;
David Brownelle5760fd2008-06-19 17:55:35 -0700176
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
Sebastian Andrzej Siewior78f46f02012-12-23 21:09:59 +0100183 id = usb_string_id(cdev);
184 if (id < 0)
185 return id;
186 strings_loopback[0].id = id;
187 loopback_intf.iInterface = id;
188
David Brownelle5760fd2008-06-19 17:55:35 -0700189 /* allocate endpoints */
190
David Brownell7e75bc02008-08-18 17:41:31 -0700191 loop->in_ep = usb_ep_autoconfig(cdev->gadget, &fs_loop_source_desc);
David Brownelle5760fd2008-06-19 17:55:35 -0700192 if (!loop->in_ep) {
193autoconf_fail:
194 ERROR(cdev, "%s: can't autoconfigure on %s\n",
195 f->name, cdev->gadget->name);
196 return -ENODEV;
197 }
198 loop->in_ep->driver_data = cdev; /* claim */
199
David Brownell7e75bc02008-08-18 17:41:31 -0700200 loop->out_ep = usb_ep_autoconfig(cdev->gadget, &fs_loop_sink_desc);
David Brownelle5760fd2008-06-19 17:55:35 -0700201 if (!loop->out_ep)
202 goto autoconf_fail;
203 loop->out_ep->driver_data = cdev; /* claim */
204
205 /* support high speed hardware */
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200206 hs_loop_source_desc.bEndpointAddress =
207 fs_loop_source_desc.bEndpointAddress;
208 hs_loop_sink_desc.bEndpointAddress = fs_loop_sink_desc.bEndpointAddress;
David Brownelle5760fd2008-06-19 17:55:35 -0700209
Amit Blay57c97c02011-07-03 17:29:31 +0300210 /* support super speed hardware */
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200211 ss_loop_source_desc.bEndpointAddress =
212 fs_loop_source_desc.bEndpointAddress;
213 ss_loop_sink_desc.bEndpointAddress = fs_loop_sink_desc.bEndpointAddress;
214
215 ret = usb_assign_descriptors(f, fs_loopback_descs, hs_loopback_descs,
216 ss_loopback_descs);
217 if (ret)
218 return ret;
Amit Blay57c97c02011-07-03 17:29:31 +0300219
David Brownelle5760fd2008-06-19 17:55:35 -0700220 DBG(cdev, "%s speed %s: IN/%s, OUT/%s\n",
Amit Blay57c97c02011-07-03 17:29:31 +0300221 (gadget_is_superspeed(c->cdev->gadget) ? "super" :
222 (gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full")),
David Brownelle5760fd2008-06-19 17:55:35 -0700223 f->name, loop->in_ep->name, loop->out_ep->name);
224 return 0;
225}
226
Sebastian Andrzej Siewiorcf9a08a2012-12-23 21:10:01 +0100227static void lb_free_func(struct usb_function *f)
David Brownelle5760fd2008-06-19 17:55:35 -0700228{
Andrzej Pietrasiewiczc0501f42013-11-07 08:41:27 +0100229 struct f_lb_opts *opts;
230
231 opts = container_of(f->fi, struct f_lb_opts, func_inst);
232
233 mutex_lock(&opts->lock);
234 opts->refcnt--;
235 mutex_unlock(&opts->lock);
236
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200237 usb_free_all_descriptors(f);
David Brownelle5760fd2008-06-19 17:55:35 -0700238 kfree(func_to_loop(f));
239}
240
241static void loopback_complete(struct usb_ep *ep, struct usb_request *req)
242{
243 struct f_loopback *loop = ep->driver_data;
244 struct usb_composite_dev *cdev = loop->function.config->cdev;
245 int status = req->status;
246
247 switch (status) {
248
249 case 0: /* normal completion? */
250 if (ep == loop->out_ep) {
David Brownelle5760fd2008-06-19 17:55:35 -0700251 req->zero = (req->actual < req->length);
252 req->length = req->actual;
David Brownelle5760fd2008-06-19 17:55:35 -0700253 }
254
255 /* queue the buffer for some later OUT packet */
256 req->length = buflen;
Felipe Balbie0857ce2014-10-13 15:30:52 -0500257 status = usb_ep_queue(ep, req, GFP_ATOMIC);
David Brownelle5760fd2008-06-19 17:55:35 -0700258 if (status == 0)
259 return;
260
261 /* "should never get here" */
262 /* FALLTHROUGH */
263
264 default:
265 ERROR(cdev, "%s loop complete --> %d, %d/%d\n", ep->name,
266 status, req->actual, req->length);
267 /* FALLTHROUGH */
268
269 /* NOTE: since this driver doesn't maintain an explicit record
270 * of requests it submitted (just maintains qlen count), we
271 * rely on the hardware driver to clean up on disconnect or
272 * endpoint disable.
273 */
274 case -ECONNABORTED: /* hardware forced ep reset */
275 case -ECONNRESET: /* request dequeued */
276 case -ESHUTDOWN: /* disconnect from host */
277 free_ep_req(ep, req);
278 return;
279 }
280}
281
282static void disable_loopback(struct f_loopback *loop)
283{
284 struct usb_composite_dev *cdev;
285
286 cdev = loop->function.config->cdev;
Felipe Balbi2c247802015-03-11 10:00:05 -0500287 disable_endpoints(cdev, loop->in_ep, loop->out_ep, NULL, NULL);
David Brownelle5760fd2008-06-19 17:55:35 -0700288 VDBG(cdev, "%s disabled\n", loop->function.name);
289}
290
Andrzej Pietrasiewicz1efd54e2013-11-07 08:41:26 +0100291static inline struct usb_request *lb_alloc_ep_req(struct usb_ep *ep, int len)
292{
293 return alloc_ep_req(ep, len, buflen);
294}
295
Felipe Balbie0857ce2014-10-13 15:30:52 -0500296static int enable_endpoint(struct usb_composite_dev *cdev, struct f_loopback *loop,
297 struct usb_ep *ep)
David Brownelle5760fd2008-06-19 17:55:35 -0700298{
David Brownelle5760fd2008-06-19 17:55:35 -0700299 struct usb_request *req;
300 unsigned i;
Felipe Balbie0857ce2014-10-13 15:30:52 -0500301 int result;
David Brownelle5760fd2008-06-19 17:55:35 -0700302
Felipe Balbie0857ce2014-10-13 15:30:52 -0500303 /*
304 * one endpoint writes data back IN to the host while another endpoint
305 * just reads OUT packets
306 */
Tatyana Brokhmanea2a1df2011-06-28 16:33:50 +0300307 result = config_ep_by_speed(cdev->gadget, &(loop->function), ep);
308 if (result)
309 goto fail0;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300310 result = usb_ep_enable(ep);
Felipe Balbie0857ce2014-10-13 15:30:52 -0500311 if (result < 0)
312 goto fail0;
David Brownelle5760fd2008-06-19 17:55:35 -0700313 ep->driver_data = loop;
314
Felipe Balbie0857ce2014-10-13 15:30:52 -0500315 /*
316 * allocate a bunch of read buffers and queue them all at once.
David Brownelle5760fd2008-06-19 17:55:35 -0700317 * we buffer at most 'qlen' transfers; fewer if any need more
318 * than 'buflen' bytes each.
319 */
320 for (i = 0; i < qlen && result == 0; i++) {
Andrzej Pietrasiewicz1efd54e2013-11-07 08:41:26 +0100321 req = lb_alloc_ep_req(ep, 0);
Felipe Balbie0857ce2014-10-13 15:30:52 -0500322 if (!req)
323 goto fail1;
324
325 req->complete = loopback_complete;
326 result = usb_ep_queue(ep, req, GFP_ATOMIC);
327 if (result) {
328 ERROR(cdev, "%s queue req --> %d\n",
329 ep->name, result);
330 goto fail1;
David Brownelle5760fd2008-06-19 17:55:35 -0700331 }
332 }
333
Felipe Balbie0857ce2014-10-13 15:30:52 -0500334 return 0;
335
336fail1:
337 usb_ep_disable(ep);
338
339fail0:
340 return result;
341}
342
343static int
344enable_loopback(struct usb_composite_dev *cdev, struct f_loopback *loop)
345{
346 int result = 0;
347
348 result = enable_endpoint(cdev, loop, loop->in_ep);
349 if (result)
350 return result;
351
352 result = enable_endpoint(cdev, loop, loop->out_ep);
353 if (result)
354 return result;
355
David Brownelle5760fd2008-06-19 17:55:35 -0700356 DBG(cdev, "%s enabled\n", loop->function.name);
357 return result;
358}
359
360static int loopback_set_alt(struct usb_function *f,
361 unsigned intf, unsigned alt)
362{
363 struct f_loopback *loop = func_to_loop(f);
364 struct usb_composite_dev *cdev = f->config->cdev;
365
366 /* we know alt is zero */
367 if (loop->in_ep->driver_data)
368 disable_loopback(loop);
369 return enable_loopback(cdev, loop);
370}
371
372static void loopback_disable(struct usb_function *f)
373{
374 struct f_loopback *loop = func_to_loop(f);
375
376 disable_loopback(loop);
377}
378
Sebastian Andrzej Siewiorcf9a08a2012-12-23 21:10:01 +0100379static struct usb_function *loopback_alloc(struct usb_function_instance *fi)
David Brownelle5760fd2008-06-19 17:55:35 -0700380{
381 struct f_loopback *loop;
Sebastian Andrzej Siewiorcf9a08a2012-12-23 21:10:01 +0100382 struct f_lb_opts *lb_opts;
David Brownelle5760fd2008-06-19 17:55:35 -0700383
384 loop = kzalloc(sizeof *loop, GFP_KERNEL);
385 if (!loop)
Sebastian Andrzej Siewiorcf9a08a2012-12-23 21:10:01 +0100386 return ERR_PTR(-ENOMEM);
387
388 lb_opts = container_of(fi, struct f_lb_opts, func_inst);
Andrzej Pietrasiewiczc0501f42013-11-07 08:41:27 +0100389
390 mutex_lock(&lb_opts->lock);
391 lb_opts->refcnt++;
392 mutex_unlock(&lb_opts->lock);
393
Sebastian Andrzej Siewiorcf9a08a2012-12-23 21:10:01 +0100394 buflen = lb_opts->bulk_buflen;
395 qlen = lb_opts->qlen;
396 if (!qlen)
397 qlen = 32;
David Brownelle5760fd2008-06-19 17:55:35 -0700398
399 loop->function.name = "loopback";
David Brownelle5760fd2008-06-19 17:55:35 -0700400 loop->function.bind = loopback_bind;
David Brownelle5760fd2008-06-19 17:55:35 -0700401 loop->function.set_alt = loopback_set_alt;
402 loop->function.disable = loopback_disable;
Sebastian Andrzej Siewiorcf9a08a2012-12-23 21:10:01 +0100403 loop->function.strings = loopback_strings;
David Brownelle5760fd2008-06-19 17:55:35 -0700404
Sebastian Andrzej Siewiorcf9a08a2012-12-23 21:10:01 +0100405 loop->function.free_func = lb_free_func;
406
407 return &loop->function;
David Brownelle5760fd2008-06-19 17:55:35 -0700408}
Sebastian Andrzej Siewiorcf9a08a2012-12-23 21:10:01 +0100409
Andrzej Pietrasiewiczc0501f42013-11-07 08:41:27 +0100410static inline struct f_lb_opts *to_f_lb_opts(struct config_item *item)
411{
412 return container_of(to_config_group(item), struct f_lb_opts,
413 func_inst.group);
414}
415
Andrzej Pietrasiewiczc0501f42013-11-07 08:41:27 +0100416static 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
423static struct configfs_item_operations lb_item_ops = {
424 .release = lb_attr_release,
Andrzej Pietrasiewiczc0501f42013-11-07 08:41:27 +0100425};
426
Christoph Hellwig75ab2252015-10-03 15:32:43 +0200427static ssize_t f_lb_opts_qlen_show(struct config_item *item, char *page)
Andrzej Pietrasiewiczc0501f42013-11-07 08:41:27 +0100428{
Christoph Hellwig75ab2252015-10-03 15:32:43 +0200429 struct f_lb_opts *opts = to_f_lb_opts(item);
Andrzej Pietrasiewiczc0501f42013-11-07 08:41:27 +0100430 int result;
431
432 mutex_lock(&opts->lock);
433 result = sprintf(page, "%d", opts->qlen);
434 mutex_unlock(&opts->lock);
435
436 return result;
437}
438
Christoph Hellwig75ab2252015-10-03 15:32:43 +0200439static ssize_t f_lb_opts_qlen_store(struct config_item *item,
Andrzej Pietrasiewiczc0501f42013-11-07 08:41:27 +0100440 const char *page, size_t len)
441{
Christoph Hellwig75ab2252015-10-03 15:32:43 +0200442 struct f_lb_opts *opts = to_f_lb_opts(item);
Andrzej Pietrasiewiczc0501f42013-11-07 08:41:27 +0100443 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;
458end:
459 mutex_unlock(&opts->lock);
460 return ret;
461}
462
Christoph Hellwig75ab2252015-10-03 15:32:43 +0200463CONFIGFS_ATTR(f_lb_opts_, qlen);
Andrzej Pietrasiewiczc0501f42013-11-07 08:41:27 +0100464
Christoph Hellwig75ab2252015-10-03 15:32:43 +0200465static ssize_t f_lb_opts_bulk_buflen_show(struct config_item *item, char *page)
Andrzej Pietrasiewiczc0501f42013-11-07 08:41:27 +0100466{
Christoph Hellwig75ab2252015-10-03 15:32:43 +0200467 struct f_lb_opts *opts = to_f_lb_opts(item);
Andrzej Pietrasiewiczc0501f42013-11-07 08:41:27 +0100468 int result;
469
470 mutex_lock(&opts->lock);
471 result = sprintf(page, "%d", opts->bulk_buflen);
472 mutex_unlock(&opts->lock);
473
474 return result;
475}
476
Christoph Hellwig75ab2252015-10-03 15:32:43 +0200477static ssize_t f_lb_opts_bulk_buflen_store(struct config_item *item,
Andrzej Pietrasiewiczc0501f42013-11-07 08:41:27 +0100478 const char *page, size_t len)
479{
Christoph Hellwig75ab2252015-10-03 15:32:43 +0200480 struct f_lb_opts *opts = to_f_lb_opts(item);
Andrzej Pietrasiewiczc0501f42013-11-07 08:41:27 +0100481 int ret;
482 u32 num;
483
484 mutex_lock(&opts->lock);
485 if (opts->refcnt) {
486 ret = -EBUSY;
487 goto end;
488 }
489
490 ret = kstrtou32(page, 0, &num);
491 if (ret)
492 goto end;
493
494 opts->bulk_buflen = num;
495 ret = len;
496end:
497 mutex_unlock(&opts->lock);
498 return ret;
499}
500
Christoph Hellwig75ab2252015-10-03 15:32:43 +0200501CONFIGFS_ATTR(f_lb_opts_, bulk_buflen);
Andrzej Pietrasiewiczc0501f42013-11-07 08:41:27 +0100502
503static struct configfs_attribute *lb_attrs[] = {
Christoph Hellwig75ab2252015-10-03 15:32:43 +0200504 &f_lb_opts_attr_qlen,
505 &f_lb_opts_attr_bulk_buflen,
Andrzej Pietrasiewiczc0501f42013-11-07 08:41:27 +0100506 NULL,
507};
508
509static struct config_item_type lb_func_type = {
510 .ct_item_ops = &lb_item_ops,
511 .ct_attrs = lb_attrs,
512 .ct_owner = THIS_MODULE,
513};
514
Sebastian Andrzej Siewiorcf9a08a2012-12-23 21:10:01 +0100515static void lb_free_instance(struct usb_function_instance *fi)
516{
517 struct f_lb_opts *lb_opts;
518
519 lb_opts = container_of(fi, struct f_lb_opts, func_inst);
520 kfree(lb_opts);
521}
522
523static struct usb_function_instance *loopback_alloc_instance(void)
524{
525 struct f_lb_opts *lb_opts;
526
527 lb_opts = kzalloc(sizeof(*lb_opts), GFP_KERNEL);
528 if (!lb_opts)
529 return ERR_PTR(-ENOMEM);
Andrzej Pietrasiewiczc0501f42013-11-07 08:41:27 +0100530 mutex_init(&lb_opts->lock);
Sebastian Andrzej Siewiorcf9a08a2012-12-23 21:10:01 +0100531 lb_opts->func_inst.free_func_inst = lb_free_instance;
Andrzej Pietrasiewiczc0501f42013-11-07 08:41:27 +0100532 lb_opts->bulk_buflen = GZERO_BULK_BUFLEN;
533 lb_opts->qlen = GZERO_QLEN;
534
535 config_group_init_type_name(&lb_opts->func_inst.group, "",
536 &lb_func_type);
537
Sebastian Andrzej Siewiorcf9a08a2012-12-23 21:10:01 +0100538 return &lb_opts->func_inst;
539}
540DECLARE_USB_FUNCTION(Loopback, loopback_alloc_instance, loopback_alloc);
541
542int __init lb_modinit(void)
543{
544 int ret;
545
546 ret = usb_function_register(&Loopbackusb_func);
547 if (ret)
548 return ret;
549 return ret;
550}
551void __exit lb_modexit(void)
552{
553 usb_function_unregister(&Loopbackusb_func);
554}
555
556MODULE_LICENSE("GPL");