]>
Commit | Line | Data |
---|---|---|
5fd54ace | 1 | // SPDX-License-Identifier: GPL-2.0+ |
a400cadc DB |
2 | /* |
3 | * f_sourcesink.c - USB peripheral source/sink configuration driver | |
4 | * | |
5 | * Copyright (C) 2003-2008 David Brownell | |
6 | * Copyright (C) 2008 by Nokia Corporation | |
a400cadc DB |
7 | */ |
8 | ||
9 | /* #define VERBOSE_DEBUG */ | |
10 | ||
5a0e3ad6 | 11 | #include <linux/slab.h> |
a400cadc | 12 | #include <linux/kernel.h> |
a400cadc | 13 | #include <linux/device.h> |
6eb0de82 | 14 | #include <linux/module.h> |
cf9a08ae SAS |
15 | #include <linux/usb/composite.h> |
16 | #include <linux/err.h> | |
a400cadc DB |
17 | |
18 | #include "g_zero.h" | |
1efd54ea | 19 | #include "u_f.h" |
a400cadc | 20 | |
a400cadc DB |
21 | /* |
22 | * SOURCE/SINK FUNCTION ... a primary testing vehicle for USB peripheral | |
23 | * controller drivers. | |
24 | * | |
25 | * This just sinks bulk packets OUT to the peripheral and sources them IN | |
26 | * to the host, optionally with specific data patterns for integrity tests. | |
27 | * As such it supports basic functionality and load tests. | |
28 | * | |
29 | * In terms of control messaging, this supports all the standard requests | |
30 | * plus two that support control-OUT tests. If the optional "autoresume" | |
31 | * mode is enabled, it provides good functional coverage for the "USBCV" | |
32 | * test harness from USB-IF. | |
a400cadc DB |
33 | */ |
34 | struct f_sourcesink { | |
35 | struct usb_function function; | |
36 | ||
37 | struct usb_ep *in_ep; | |
38 | struct usb_ep *out_ep; | |
b4036ccd PZ |
39 | struct usb_ep *iso_in_ep; |
40 | struct usb_ep *iso_out_ep; | |
41 | int cur_alt; | |
897ee0e8 RB |
42 | |
43 | unsigned pattern; | |
44 | unsigned isoc_interval; | |
45 | unsigned isoc_maxpacket; | |
46 | unsigned isoc_mult; | |
47 | unsigned isoc_maxburst; | |
48 | unsigned buflen; | |
0d6c3d96 PC |
49 | unsigned bulk_qlen; |
50 | unsigned iso_qlen; | |
a400cadc DB |
51 | }; |
52 | ||
53 | static inline struct f_sourcesink *func_to_ss(struct usb_function *f) | |
54 | { | |
55 | return container_of(f, struct f_sourcesink, function); | |
56 | } | |
57 | ||
a400cadc DB |
58 | /*-------------------------------------------------------------------------*/ |
59 | ||
b4036ccd PZ |
60 | static struct usb_interface_descriptor source_sink_intf_alt0 = { |
61 | .bLength = USB_DT_INTERFACE_SIZE, | |
a400cadc DB |
62 | .bDescriptorType = USB_DT_INTERFACE, |
63 | ||
b4036ccd | 64 | .bAlternateSetting = 0, |
a400cadc DB |
65 | .bNumEndpoints = 2, |
66 | .bInterfaceClass = USB_CLASS_VENDOR_SPEC, | |
b4036ccd PZ |
67 | /* .iInterface = DYNAMIC */ |
68 | }; | |
69 | ||
70 | static struct usb_interface_descriptor source_sink_intf_alt1 = { | |
71 | .bLength = USB_DT_INTERFACE_SIZE, | |
72 | .bDescriptorType = USB_DT_INTERFACE, | |
73 | ||
74 | .bAlternateSetting = 1, | |
75 | .bNumEndpoints = 4, | |
76 | .bInterfaceClass = USB_CLASS_VENDOR_SPEC, | |
77 | /* .iInterface = DYNAMIC */ | |
a400cadc DB |
78 | }; |
79 | ||
80 | /* full speed support: */ | |
81 | ||
82 | static struct usb_endpoint_descriptor fs_source_desc = { | |
83 | .bLength = USB_DT_ENDPOINT_SIZE, | |
84 | .bDescriptorType = USB_DT_ENDPOINT, | |
85 | ||
86 | .bEndpointAddress = USB_DIR_IN, | |
87 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
88 | }; | |
89 | ||
90 | static struct usb_endpoint_descriptor fs_sink_desc = { | |
91 | .bLength = USB_DT_ENDPOINT_SIZE, | |
92 | .bDescriptorType = USB_DT_ENDPOINT, | |
93 | ||
94 | .bEndpointAddress = USB_DIR_OUT, | |
95 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
96 | }; | |
97 | ||
b4036ccd PZ |
98 | static struct usb_endpoint_descriptor fs_iso_source_desc = { |
99 | .bLength = USB_DT_ENDPOINT_SIZE, | |
100 | .bDescriptorType = USB_DT_ENDPOINT, | |
101 | ||
102 | .bEndpointAddress = USB_DIR_IN, | |
103 | .bmAttributes = USB_ENDPOINT_XFER_ISOC, | |
104 | .wMaxPacketSize = cpu_to_le16(1023), | |
105 | .bInterval = 4, | |
106 | }; | |
107 | ||
108 | static struct usb_endpoint_descriptor fs_iso_sink_desc = { | |
109 | .bLength = USB_DT_ENDPOINT_SIZE, | |
110 | .bDescriptorType = USB_DT_ENDPOINT, | |
111 | ||
112 | .bEndpointAddress = USB_DIR_OUT, | |
113 | .bmAttributes = USB_ENDPOINT_XFER_ISOC, | |
114 | .wMaxPacketSize = cpu_to_le16(1023), | |
115 | .bInterval = 4, | |
116 | }; | |
117 | ||
a400cadc | 118 | static struct usb_descriptor_header *fs_source_sink_descs[] = { |
b4036ccd | 119 | (struct usb_descriptor_header *) &source_sink_intf_alt0, |
a400cadc DB |
120 | (struct usb_descriptor_header *) &fs_sink_desc, |
121 | (struct usb_descriptor_header *) &fs_source_desc, | |
b4036ccd PZ |
122 | (struct usb_descriptor_header *) &source_sink_intf_alt1, |
123 | #define FS_ALT_IFC_1_OFFSET 3 | |
124 | (struct usb_descriptor_header *) &fs_sink_desc, | |
125 | (struct usb_descriptor_header *) &fs_source_desc, | |
126 | (struct usb_descriptor_header *) &fs_iso_sink_desc, | |
127 | (struct usb_descriptor_header *) &fs_iso_source_desc, | |
a400cadc DB |
128 | NULL, |
129 | }; | |
130 | ||
131 | /* high speed support: */ | |
132 | ||
133 | static struct usb_endpoint_descriptor hs_source_desc = { | |
134 | .bLength = USB_DT_ENDPOINT_SIZE, | |
135 | .bDescriptorType = USB_DT_ENDPOINT, | |
136 | ||
137 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
551509d2 | 138 | .wMaxPacketSize = cpu_to_le16(512), |
a400cadc DB |
139 | }; |
140 | ||
141 | static struct usb_endpoint_descriptor hs_sink_desc = { | |
142 | .bLength = USB_DT_ENDPOINT_SIZE, | |
143 | .bDescriptorType = USB_DT_ENDPOINT, | |
144 | ||
145 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
551509d2 | 146 | .wMaxPacketSize = cpu_to_le16(512), |
a400cadc DB |
147 | }; |
148 | ||
b4036ccd PZ |
149 | static struct usb_endpoint_descriptor hs_iso_source_desc = { |
150 | .bLength = USB_DT_ENDPOINT_SIZE, | |
151 | .bDescriptorType = USB_DT_ENDPOINT, | |
152 | ||
153 | .bmAttributes = USB_ENDPOINT_XFER_ISOC, | |
154 | .wMaxPacketSize = cpu_to_le16(1024), | |
155 | .bInterval = 4, | |
156 | }; | |
157 | ||
158 | static struct usb_endpoint_descriptor hs_iso_sink_desc = { | |
159 | .bLength = USB_DT_ENDPOINT_SIZE, | |
160 | .bDescriptorType = USB_DT_ENDPOINT, | |
161 | ||
162 | .bmAttributes = USB_ENDPOINT_XFER_ISOC, | |
163 | .wMaxPacketSize = cpu_to_le16(1024), | |
164 | .bInterval = 4, | |
165 | }; | |
166 | ||
a400cadc | 167 | static struct usb_descriptor_header *hs_source_sink_descs[] = { |
b4036ccd | 168 | (struct usb_descriptor_header *) &source_sink_intf_alt0, |
a400cadc DB |
169 | (struct usb_descriptor_header *) &hs_source_desc, |
170 | (struct usb_descriptor_header *) &hs_sink_desc, | |
b4036ccd PZ |
171 | (struct usb_descriptor_header *) &source_sink_intf_alt1, |
172 | #define HS_ALT_IFC_1_OFFSET 3 | |
173 | (struct usb_descriptor_header *) &hs_source_desc, | |
174 | (struct usb_descriptor_header *) &hs_sink_desc, | |
175 | (struct usb_descriptor_header *) &hs_iso_source_desc, | |
176 | (struct usb_descriptor_header *) &hs_iso_sink_desc, | |
a400cadc DB |
177 | NULL, |
178 | }; | |
179 | ||
57c97c02 AB |
180 | /* super speed support: */ |
181 | ||
182 | static struct usb_endpoint_descriptor ss_source_desc = { | |
183 | .bLength = USB_DT_ENDPOINT_SIZE, | |
184 | .bDescriptorType = USB_DT_ENDPOINT, | |
185 | ||
186 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
187 | .wMaxPacketSize = cpu_to_le16(1024), | |
188 | }; | |
189 | ||
4a5ee77c | 190 | static struct usb_ss_ep_comp_descriptor ss_source_comp_desc = { |
57c97c02 AB |
191 | .bLength = USB_DT_SS_EP_COMP_SIZE, |
192 | .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, | |
b4036ccd | 193 | |
57c97c02 AB |
194 | .bMaxBurst = 0, |
195 | .bmAttributes = 0, | |
196 | .wBytesPerInterval = 0, | |
197 | }; | |
198 | ||
199 | static struct usb_endpoint_descriptor ss_sink_desc = { | |
200 | .bLength = USB_DT_ENDPOINT_SIZE, | |
201 | .bDescriptorType = USB_DT_ENDPOINT, | |
202 | ||
203 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
204 | .wMaxPacketSize = cpu_to_le16(1024), | |
205 | }; | |
206 | ||
4a5ee77c | 207 | static struct usb_ss_ep_comp_descriptor ss_sink_comp_desc = { |
57c97c02 AB |
208 | .bLength = USB_DT_SS_EP_COMP_SIZE, |
209 | .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, | |
b4036ccd | 210 | |
57c97c02 AB |
211 | .bMaxBurst = 0, |
212 | .bmAttributes = 0, | |
213 | .wBytesPerInterval = 0, | |
214 | }; | |
215 | ||
b4036ccd PZ |
216 | static struct usb_endpoint_descriptor ss_iso_source_desc = { |
217 | .bLength = USB_DT_ENDPOINT_SIZE, | |
218 | .bDescriptorType = USB_DT_ENDPOINT, | |
219 | ||
220 | .bmAttributes = USB_ENDPOINT_XFER_ISOC, | |
221 | .wMaxPacketSize = cpu_to_le16(1024), | |
222 | .bInterval = 4, | |
223 | }; | |
224 | ||
4a5ee77c | 225 | static struct usb_ss_ep_comp_descriptor ss_iso_source_comp_desc = { |
b4036ccd PZ |
226 | .bLength = USB_DT_SS_EP_COMP_SIZE, |
227 | .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, | |
228 | ||
229 | .bMaxBurst = 0, | |
230 | .bmAttributes = 0, | |
231 | .wBytesPerInterval = cpu_to_le16(1024), | |
232 | }; | |
233 | ||
234 | static struct usb_endpoint_descriptor ss_iso_sink_desc = { | |
235 | .bLength = USB_DT_ENDPOINT_SIZE, | |
236 | .bDescriptorType = USB_DT_ENDPOINT, | |
237 | ||
238 | .bmAttributes = USB_ENDPOINT_XFER_ISOC, | |
239 | .wMaxPacketSize = cpu_to_le16(1024), | |
240 | .bInterval = 4, | |
241 | }; | |
242 | ||
4a5ee77c | 243 | static struct usb_ss_ep_comp_descriptor ss_iso_sink_comp_desc = { |
b4036ccd PZ |
244 | .bLength = USB_DT_SS_EP_COMP_SIZE, |
245 | .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, | |
246 | ||
247 | .bMaxBurst = 0, | |
248 | .bmAttributes = 0, | |
249 | .wBytesPerInterval = cpu_to_le16(1024), | |
250 | }; | |
251 | ||
57c97c02 | 252 | static struct usb_descriptor_header *ss_source_sink_descs[] = { |
b4036ccd | 253 | (struct usb_descriptor_header *) &source_sink_intf_alt0, |
57c97c02 AB |
254 | (struct usb_descriptor_header *) &ss_source_desc, |
255 | (struct usb_descriptor_header *) &ss_source_comp_desc, | |
256 | (struct usb_descriptor_header *) &ss_sink_desc, | |
257 | (struct usb_descriptor_header *) &ss_sink_comp_desc, | |
b4036ccd PZ |
258 | (struct usb_descriptor_header *) &source_sink_intf_alt1, |
259 | #define SS_ALT_IFC_1_OFFSET 5 | |
260 | (struct usb_descriptor_header *) &ss_source_desc, | |
261 | (struct usb_descriptor_header *) &ss_source_comp_desc, | |
262 | (struct usb_descriptor_header *) &ss_sink_desc, | |
263 | (struct usb_descriptor_header *) &ss_sink_comp_desc, | |
264 | (struct usb_descriptor_header *) &ss_iso_source_desc, | |
265 | (struct usb_descriptor_header *) &ss_iso_source_comp_desc, | |
266 | (struct usb_descriptor_header *) &ss_iso_sink_desc, | |
267 | (struct usb_descriptor_header *) &ss_iso_sink_comp_desc, | |
57c97c02 AB |
268 | NULL, |
269 | }; | |
270 | ||
a400cadc DB |
271 | /* function-specific strings: */ |
272 | ||
273 | static struct usb_string strings_sourcesink[] = { | |
274 | [0].s = "source and sink data", | |
275 | { } /* end of list */ | |
276 | }; | |
277 | ||
278 | static struct usb_gadget_strings stringtab_sourcesink = { | |
279 | .language = 0x0409, /* en-us */ | |
280 | .strings = strings_sourcesink, | |
281 | }; | |
282 | ||
283 | static struct usb_gadget_strings *sourcesink_strings[] = { | |
284 | &stringtab_sourcesink, | |
285 | NULL, | |
286 | }; | |
287 | ||
288 | /*-------------------------------------------------------------------------*/ | |
289 | ||
1efd54ea | 290 | static inline struct usb_request *ss_alloc_ep_req(struct usb_ep *ep, int len) |
cf9a08ae | 291 | { |
aadbe812 | 292 | return alloc_ep_req(ep, len); |
cf9a08ae SAS |
293 | } |
294 | ||
cf9a08ae SAS |
295 | static void disable_ep(struct usb_composite_dev *cdev, struct usb_ep *ep) |
296 | { | |
297 | int value; | |
298 | ||
dbd2badf RB |
299 | value = usb_ep_disable(ep); |
300 | if (value < 0) | |
301 | DBG(cdev, "disable %s --> %d\n", ep->name, value); | |
cf9a08ae SAS |
302 | } |
303 | ||
304 | void disable_endpoints(struct usb_composite_dev *cdev, | |
305 | struct usb_ep *in, struct usb_ep *out, | |
2c247804 | 306 | struct usb_ep *iso_in, struct usb_ep *iso_out) |
cf9a08ae SAS |
307 | { |
308 | disable_ep(cdev, in); | |
309 | disable_ep(cdev, out); | |
310 | if (iso_in) | |
311 | disable_ep(cdev, iso_in); | |
312 | if (iso_out) | |
313 | disable_ep(cdev, iso_out); | |
314 | } | |
315 | ||
316 | static int | |
a400cadc DB |
317 | sourcesink_bind(struct usb_configuration *c, struct usb_function *f) |
318 | { | |
319 | struct usb_composite_dev *cdev = c->cdev; | |
320 | struct f_sourcesink *ss = func_to_ss(f); | |
321 | int id; | |
10287bae | 322 | int ret; |
a400cadc DB |
323 | |
324 | /* allocate interface ID(s) */ | |
325 | id = usb_interface_id(c, f); | |
326 | if (id < 0) | |
327 | return id; | |
b4036ccd PZ |
328 | source_sink_intf_alt0.bInterfaceNumber = id; |
329 | source_sink_intf_alt1.bInterfaceNumber = id; | |
a400cadc | 330 | |
b4036ccd | 331 | /* allocate bulk endpoints */ |
a400cadc DB |
332 | ss->in_ep = usb_ep_autoconfig(cdev->gadget, &fs_source_desc); |
333 | if (!ss->in_ep) { | |
334 | autoconf_fail: | |
335 | ERROR(cdev, "%s: can't autoconfigure on %s\n", | |
336 | f->name, cdev->gadget->name); | |
337 | return -ENODEV; | |
338 | } | |
a400cadc DB |
339 | |
340 | ss->out_ep = usb_ep_autoconfig(cdev->gadget, &fs_sink_desc); | |
341 | if (!ss->out_ep) | |
342 | goto autoconf_fail; | |
a400cadc | 343 | |
b4036ccd | 344 | /* sanity check the isoc module parameters */ |
897ee0e8 RB |
345 | if (ss->isoc_interval < 1) |
346 | ss->isoc_interval = 1; | |
347 | if (ss->isoc_interval > 16) | |
348 | ss->isoc_interval = 16; | |
349 | if (ss->isoc_mult > 2) | |
350 | ss->isoc_mult = 2; | |
351 | if (ss->isoc_maxburst > 15) | |
352 | ss->isoc_maxburst = 15; | |
b4036ccd PZ |
353 | |
354 | /* fill in the FS isoc descriptors from the module parameters */ | |
897ee0e8 RB |
355 | fs_iso_source_desc.wMaxPacketSize = ss->isoc_maxpacket > 1023 ? |
356 | 1023 : ss->isoc_maxpacket; | |
357 | fs_iso_source_desc.bInterval = ss->isoc_interval; | |
358 | fs_iso_sink_desc.wMaxPacketSize = ss->isoc_maxpacket > 1023 ? | |
359 | 1023 : ss->isoc_maxpacket; | |
360 | fs_iso_sink_desc.bInterval = ss->isoc_interval; | |
b4036ccd PZ |
361 | |
362 | /* allocate iso endpoints */ | |
363 | ss->iso_in_ep = usb_ep_autoconfig(cdev->gadget, &fs_iso_source_desc); | |
364 | if (!ss->iso_in_ep) | |
365 | goto no_iso; | |
b4036ccd PZ |
366 | |
367 | ss->iso_out_ep = usb_ep_autoconfig(cdev->gadget, &fs_iso_sink_desc); | |
dbd2badf RB |
368 | if (!ss->iso_out_ep) { |
369 | usb_ep_autoconfig_release(ss->iso_in_ep); | |
b4036ccd PZ |
370 | ss->iso_in_ep = NULL; |
371 | no_iso: | |
372 | /* | |
373 | * We still want to work even if the UDC doesn't have isoc | |
374 | * endpoints, so null out the alt interface that contains | |
375 | * them and continue. | |
376 | */ | |
377 | fs_source_sink_descs[FS_ALT_IFC_1_OFFSET] = NULL; | |
378 | hs_source_sink_descs[HS_ALT_IFC_1_OFFSET] = NULL; | |
379 | ss_source_sink_descs[SS_ALT_IFC_1_OFFSET] = NULL; | |
380 | } | |
381 | ||
897ee0e8 RB |
382 | if (ss->isoc_maxpacket > 1024) |
383 | ss->isoc_maxpacket = 1024; | |
b4036ccd | 384 | |
a400cadc | 385 | /* support high speed hardware */ |
10287bae SAS |
386 | hs_source_desc.bEndpointAddress = fs_source_desc.bEndpointAddress; |
387 | hs_sink_desc.bEndpointAddress = fs_sink_desc.bEndpointAddress; | |
388 | ||
389 | /* | |
2c247804 FB |
390 | * Fill in the HS isoc descriptors from the module parameters. |
391 | * We assume that the user knows what they are doing and won't | |
392 | * give parameters that their UDC doesn't support. | |
10287bae | 393 | */ |
897ee0e8 RB |
394 | hs_iso_source_desc.wMaxPacketSize = ss->isoc_maxpacket; |
395 | hs_iso_source_desc.wMaxPacketSize |= ss->isoc_mult << 11; | |
396 | hs_iso_source_desc.bInterval = ss->isoc_interval; | |
10287bae SAS |
397 | hs_iso_source_desc.bEndpointAddress = |
398 | fs_iso_source_desc.bEndpointAddress; | |
399 | ||
897ee0e8 RB |
400 | hs_iso_sink_desc.wMaxPacketSize = ss->isoc_maxpacket; |
401 | hs_iso_sink_desc.wMaxPacketSize |= ss->isoc_mult << 11; | |
402 | hs_iso_sink_desc.bInterval = ss->isoc_interval; | |
10287bae | 403 | hs_iso_sink_desc.bEndpointAddress = fs_iso_sink_desc.bEndpointAddress; |
a400cadc | 404 | |
57c97c02 | 405 | /* support super speed hardware */ |
10287bae SAS |
406 | ss_source_desc.bEndpointAddress = |
407 | fs_source_desc.bEndpointAddress; | |
408 | ss_sink_desc.bEndpointAddress = | |
409 | fs_sink_desc.bEndpointAddress; | |
410 | ||
411 | /* | |
2c247804 FB |
412 | * Fill in the SS isoc descriptors from the module parameters. |
413 | * We assume that the user knows what they are doing and won't | |
414 | * give parameters that their UDC doesn't support. | |
10287bae | 415 | */ |
897ee0e8 RB |
416 | ss_iso_source_desc.wMaxPacketSize = ss->isoc_maxpacket; |
417 | ss_iso_source_desc.bInterval = ss->isoc_interval; | |
418 | ss_iso_source_comp_desc.bmAttributes = ss->isoc_mult; | |
419 | ss_iso_source_comp_desc.bMaxBurst = ss->isoc_maxburst; | |
420 | ss_iso_source_comp_desc.wBytesPerInterval = ss->isoc_maxpacket * | |
421 | (ss->isoc_mult + 1) * (ss->isoc_maxburst + 1); | |
10287bae SAS |
422 | ss_iso_source_desc.bEndpointAddress = |
423 | fs_iso_source_desc.bEndpointAddress; | |
424 | ||
897ee0e8 RB |
425 | ss_iso_sink_desc.wMaxPacketSize = ss->isoc_maxpacket; |
426 | ss_iso_sink_desc.bInterval = ss->isoc_interval; | |
427 | ss_iso_sink_comp_desc.bmAttributes = ss->isoc_mult; | |
428 | ss_iso_sink_comp_desc.bMaxBurst = ss->isoc_maxburst; | |
429 | ss_iso_sink_comp_desc.wBytesPerInterval = ss->isoc_maxpacket * | |
430 | (ss->isoc_mult + 1) * (ss->isoc_maxburst + 1); | |
10287bae SAS |
431 | ss_iso_sink_desc.bEndpointAddress = fs_iso_sink_desc.bEndpointAddress; |
432 | ||
433 | ret = usb_assign_descriptors(f, fs_source_sink_descs, | |
eaef50c7 | 434 | hs_source_sink_descs, ss_source_sink_descs, NULL); |
10287bae SAS |
435 | if (ret) |
436 | return ret; | |
57c97c02 | 437 | |
2c247804 | 438 | DBG(cdev, "%s speed %s: IN/%s, OUT/%s, ISO-IN/%s, ISO-OUT/%s\n", |
57c97c02 AB |
439 | (gadget_is_superspeed(c->cdev->gadget) ? "super" : |
440 | (gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full")), | |
b4036ccd PZ |
441 | f->name, ss->in_ep->name, ss->out_ep->name, |
442 | ss->iso_in_ep ? ss->iso_in_ep->name : "<none>", | |
2c247804 | 443 | ss->iso_out_ep ? ss->iso_out_ep->name : "<none>"); |
a400cadc DB |
444 | return 0; |
445 | } | |
446 | ||
447 | static void | |
cf9a08ae | 448 | sourcesink_free_func(struct usb_function *f) |
a400cadc | 449 | { |
25d80151 AP |
450 | struct f_ss_opts *opts; |
451 | ||
452 | opts = container_of(f->fi, struct f_ss_opts, func_inst); | |
453 | ||
454 | mutex_lock(&opts->lock); | |
455 | opts->refcnt--; | |
456 | mutex_unlock(&opts->lock); | |
457 | ||
10287bae | 458 | usb_free_all_descriptors(f); |
a400cadc DB |
459 | kfree(func_to_ss(f)); |
460 | } | |
461 | ||
462 | /* optionally require specific source/sink data patterns */ | |
463 | static int check_read_data(struct f_sourcesink *ss, struct usb_request *req) | |
464 | { | |
465 | unsigned i; | |
466 | u8 *buf = req->buf; | |
467 | struct usb_composite_dev *cdev = ss->function.config->cdev; | |
04683909 | 468 | int max_packet_size = le16_to_cpu(ss->out_ep->desc->wMaxPacketSize); |
a400cadc | 469 | |
897ee0e8 | 470 | if (ss->pattern == 2) |
b4036ccd PZ |
471 | return 0; |
472 | ||
a400cadc | 473 | for (i = 0; i < req->actual; i++, buf++) { |
897ee0e8 | 474 | switch (ss->pattern) { |
a400cadc DB |
475 | |
476 | /* all-zeroes has no synchronization issues */ | |
477 | case 0: | |
478 | if (*buf == 0) | |
479 | continue; | |
480 | break; | |
481 | ||
482 | /* "mod63" stays in sync with short-terminated transfers, | |
483 | * OR otherwise when host and gadget agree on how large | |
484 | * each usb transfer request should be. Resync is done | |
485 | * with set_interface or set_config. (We *WANT* it to | |
486 | * get quickly out of sync if controllers or their drivers | |
b4036ccd | 487 | * stutter for any reason, including buffer duplication...) |
a400cadc DB |
488 | */ |
489 | case 1: | |
04683909 | 490 | if (*buf == (u8)((i % max_packet_size) % 63)) |
a400cadc DB |
491 | continue; |
492 | break; | |
493 | } | |
494 | ERROR(cdev, "bad OUT byte, buf[%d] = %d\n", i, *buf); | |
495 | usb_ep_set_halt(ss->out_ep); | |
496 | return -EINVAL; | |
497 | } | |
498 | return 0; | |
499 | } | |
500 | ||
501 | static void reinit_write_data(struct usb_ep *ep, struct usb_request *req) | |
502 | { | |
503 | unsigned i; | |
504 | u8 *buf = req->buf; | |
04683909 | 505 | int max_packet_size = le16_to_cpu(ep->desc->wMaxPacketSize); |
897ee0e8 | 506 | struct f_sourcesink *ss = ep->driver_data; |
a400cadc | 507 | |
897ee0e8 | 508 | switch (ss->pattern) { |
a400cadc DB |
509 | case 0: |
510 | memset(req->buf, 0, req->length); | |
511 | break; | |
512 | case 1: | |
513 | for (i = 0; i < req->length; i++) | |
04683909 | 514 | *buf++ = (u8) ((i % max_packet_size) % 63); |
a400cadc | 515 | break; |
b4036ccd PZ |
516 | case 2: |
517 | break; | |
a400cadc DB |
518 | } |
519 | } | |
520 | ||
521 | static void source_sink_complete(struct usb_ep *ep, struct usb_request *req) | |
522 | { | |
b4036ccd PZ |
523 | struct usb_composite_dev *cdev; |
524 | struct f_sourcesink *ss = ep->driver_data; | |
525 | int status = req->status; | |
526 | ||
527 | /* driver_data will be null if ep has been disabled */ | |
528 | if (!ss) | |
529 | return; | |
530 | ||
531 | cdev = ss->function.config->cdev; | |
a400cadc DB |
532 | |
533 | switch (status) { | |
534 | ||
535 | case 0: /* normal completion? */ | |
536 | if (ep == ss->out_ep) { | |
537 | check_read_data(ss, req); | |
897ee0e8 | 538 | if (ss->pattern != 2) |
b4036ccd | 539 | memset(req->buf, 0x55, req->length); |
32c9cf22 | 540 | } |
a400cadc DB |
541 | break; |
542 | ||
543 | /* this endpoint is normally active while we're configured */ | |
544 | case -ECONNABORTED: /* hardware forced ep reset */ | |
545 | case -ECONNRESET: /* request dequeued */ | |
546 | case -ESHUTDOWN: /* disconnect from host */ | |
547 | VDBG(cdev, "%s gone (%d), %d/%d\n", ep->name, status, | |
548 | req->actual, req->length); | |
549 | if (ep == ss->out_ep) | |
550 | check_read_data(ss, req); | |
551 | free_ep_req(ep, req); | |
552 | return; | |
553 | ||
554 | case -EOVERFLOW: /* buffer overrun on read means that | |
555 | * we didn't provide a big enough | |
556 | * buffer. | |
557 | */ | |
558 | default: | |
559 | #if 1 | |
560 | DBG(cdev, "%s complete --> %d, %d/%d\n", ep->name, | |
561 | status, req->actual, req->length); | |
562 | #endif | |
563 | case -EREMOTEIO: /* short read */ | |
564 | break; | |
565 | } | |
566 | ||
567 | status = usb_ep_queue(ep, req, GFP_ATOMIC); | |
568 | if (status) { | |
569 | ERROR(cdev, "kill %s: resubmit %d bytes --> %d\n", | |
570 | ep->name, req->length, status); | |
571 | usb_ep_set_halt(ep); | |
572 | /* FIXME recover later ... somehow */ | |
573 | } | |
574 | } | |
575 | ||
b4036ccd | 576 | static int source_sink_start_ep(struct f_sourcesink *ss, bool is_in, |
2c247804 | 577 | bool is_iso, int speed) |
a400cadc DB |
578 | { |
579 | struct usb_ep *ep; | |
580 | struct usb_request *req; | |
0d6c3d96 PC |
581 | int i, size, qlen, status = 0; |
582 | ||
583 | if (is_iso) { | |
584 | switch (speed) { | |
585 | case USB_SPEED_SUPER: | |
586 | size = ss->isoc_maxpacket * | |
587 | (ss->isoc_mult + 1) * | |
588 | (ss->isoc_maxburst + 1); | |
589 | break; | |
590 | case USB_SPEED_HIGH: | |
591 | size = ss->isoc_maxpacket * (ss->isoc_mult + 1); | |
592 | break; | |
593 | default: | |
594 | size = ss->isoc_maxpacket > 1023 ? | |
595 | 1023 : ss->isoc_maxpacket; | |
596 | break; | |
b4036ccd | 597 | } |
0d6c3d96 PC |
598 | ep = is_in ? ss->iso_in_ep : ss->iso_out_ep; |
599 | qlen = ss->iso_qlen; | |
600 | } else { | |
601 | ep = is_in ? ss->in_ep : ss->out_ep; | |
602 | qlen = ss->bulk_qlen; | |
aadbe812 | 603 | size = ss->buflen; |
0d6c3d96 | 604 | } |
a400cadc | 605 | |
0d6c3d96 PC |
606 | for (i = 0; i < qlen; i++) { |
607 | req = ss_alloc_ep_req(ep, size); | |
b4036ccd PZ |
608 | if (!req) |
609 | return -ENOMEM; | |
a400cadc | 610 | |
b4036ccd PZ |
611 | req->complete = source_sink_complete; |
612 | if (is_in) | |
613 | reinit_write_data(ep, req); | |
897ee0e8 | 614 | else if (ss->pattern != 2) |
b4036ccd | 615 | memset(req->buf, 0x55, req->length); |
a400cadc | 616 | |
b4036ccd PZ |
617 | status = usb_ep_queue(ep, req, GFP_ATOMIC); |
618 | if (status) { | |
619 | struct usb_composite_dev *cdev; | |
a400cadc | 620 | |
b4036ccd PZ |
621 | cdev = ss->function.config->cdev; |
622 | ERROR(cdev, "start %s%s %s --> %d\n", | |
2c247804 FB |
623 | is_iso ? "ISO-" : "", is_in ? "IN" : "OUT", |
624 | ep->name, status); | |
b4036ccd | 625 | free_ep_req(ep, req); |
fa4dce20 | 626 | return status; |
b4036ccd | 627 | } |
a400cadc DB |
628 | } |
629 | ||
630 | return status; | |
631 | } | |
632 | ||
633 | static void disable_source_sink(struct f_sourcesink *ss) | |
634 | { | |
635 | struct usb_composite_dev *cdev; | |
636 | ||
637 | cdev = ss->function.config->cdev; | |
b4036ccd | 638 | disable_endpoints(cdev, ss->in_ep, ss->out_ep, ss->iso_in_ep, |
2c247804 | 639 | ss->iso_out_ep); |
a400cadc DB |
640 | VDBG(cdev, "%s disabled\n", ss->function.name); |
641 | } | |
642 | ||
643 | static int | |
b4036ccd PZ |
644 | enable_source_sink(struct usb_composite_dev *cdev, struct f_sourcesink *ss, |
645 | int alt) | |
a400cadc DB |
646 | { |
647 | int result = 0; | |
b4036ccd | 648 | int speed = cdev->gadget->speed; |
a400cadc DB |
649 | struct usb_ep *ep; |
650 | ||
b4036ccd | 651 | /* one bulk endpoint writes (sources) zeroes IN (to the host) */ |
a400cadc | 652 | ep = ss->in_ep; |
ea2a1df7 TB |
653 | result = config_ep_by_speed(cdev->gadget, &(ss->function), ep); |
654 | if (result) | |
655 | return result; | |
72c973dd | 656 | result = usb_ep_enable(ep); |
a400cadc DB |
657 | if (result < 0) |
658 | return result; | |
659 | ep->driver_data = ss; | |
660 | ||
2c247804 | 661 | result = source_sink_start_ep(ss, true, false, speed); |
a400cadc DB |
662 | if (result < 0) { |
663 | fail: | |
664 | ep = ss->in_ep; | |
665 | usb_ep_disable(ep); | |
a400cadc DB |
666 | return result; |
667 | } | |
668 | ||
b4036ccd | 669 | /* one bulk endpoint reads (sinks) anything OUT (from the host) */ |
a400cadc | 670 | ep = ss->out_ep; |
ea2a1df7 TB |
671 | result = config_ep_by_speed(cdev->gadget, &(ss->function), ep); |
672 | if (result) | |
673 | goto fail; | |
72c973dd | 674 | result = usb_ep_enable(ep); |
a400cadc DB |
675 | if (result < 0) |
676 | goto fail; | |
677 | ep->driver_data = ss; | |
678 | ||
2c247804 | 679 | result = source_sink_start_ep(ss, false, false, speed); |
a400cadc | 680 | if (result < 0) { |
b4036ccd PZ |
681 | fail2: |
682 | ep = ss->out_ep; | |
a400cadc | 683 | usb_ep_disable(ep); |
a400cadc DB |
684 | goto fail; |
685 | } | |
686 | ||
b4036ccd PZ |
687 | if (alt == 0) |
688 | goto out; | |
689 | ||
690 | /* one iso endpoint writes (sources) zeroes IN (to the host) */ | |
691 | ep = ss->iso_in_ep; | |
692 | if (ep) { | |
693 | result = config_ep_by_speed(cdev->gadget, &(ss->function), ep); | |
694 | if (result) | |
695 | goto fail2; | |
696 | result = usb_ep_enable(ep); | |
697 | if (result < 0) | |
698 | goto fail2; | |
699 | ep->driver_data = ss; | |
700 | ||
2c247804 | 701 | result = source_sink_start_ep(ss, true, true, speed); |
b4036ccd PZ |
702 | if (result < 0) { |
703 | fail3: | |
704 | ep = ss->iso_in_ep; | |
dbd2badf | 705 | if (ep) |
b4036ccd | 706 | usb_ep_disable(ep); |
b4036ccd PZ |
707 | goto fail2; |
708 | } | |
709 | } | |
710 | ||
711 | /* one iso endpoint reads (sinks) anything OUT (from the host) */ | |
712 | ep = ss->iso_out_ep; | |
713 | if (ep) { | |
714 | result = config_ep_by_speed(cdev->gadget, &(ss->function), ep); | |
715 | if (result) | |
716 | goto fail3; | |
717 | result = usb_ep_enable(ep); | |
718 | if (result < 0) | |
719 | goto fail3; | |
720 | ep->driver_data = ss; | |
721 | ||
2c247804 | 722 | result = source_sink_start_ep(ss, false, true, speed); |
b4036ccd PZ |
723 | if (result < 0) { |
724 | usb_ep_disable(ep); | |
b4036ccd PZ |
725 | goto fail3; |
726 | } | |
727 | } | |
728 | out: | |
729 | ss->cur_alt = alt; | |
730 | ||
731 | DBG(cdev, "%s enabled, alt intf %d\n", ss->function.name, alt); | |
a400cadc DB |
732 | return result; |
733 | } | |
734 | ||
735 | static int sourcesink_set_alt(struct usb_function *f, | |
736 | unsigned intf, unsigned alt) | |
737 | { | |
b4036ccd PZ |
738 | struct f_sourcesink *ss = func_to_ss(f); |
739 | struct usb_composite_dev *cdev = f->config->cdev; | |
a400cadc | 740 | |
dbd2badf | 741 | disable_source_sink(ss); |
b4036ccd PZ |
742 | return enable_source_sink(cdev, ss, alt); |
743 | } | |
744 | ||
745 | static int sourcesink_get_alt(struct usb_function *f, unsigned intf) | |
746 | { | |
747 | struct f_sourcesink *ss = func_to_ss(f); | |
748 | ||
749 | return ss->cur_alt; | |
a400cadc DB |
750 | } |
751 | ||
752 | static void sourcesink_disable(struct usb_function *f) | |
753 | { | |
754 | struct f_sourcesink *ss = func_to_ss(f); | |
755 | ||
756 | disable_source_sink(ss); | |
757 | } | |
758 | ||
a400cadc | 759 | /*-------------------------------------------------------------------------*/ |
cf9a08ae | 760 | |
544aca39 | 761 | static int sourcesink_setup(struct usb_function *f, |
a400cadc DB |
762 | const struct usb_ctrlrequest *ctrl) |
763 | { | |
544aca39 | 764 | struct usb_configuration *c = f->config; |
a400cadc DB |
765 | struct usb_request *req = c->cdev->req; |
766 | int value = -EOPNOTSUPP; | |
767 | u16 w_index = le16_to_cpu(ctrl->wIndex); | |
768 | u16 w_value = le16_to_cpu(ctrl->wValue); | |
769 | u16 w_length = le16_to_cpu(ctrl->wLength); | |
770 | ||
e13f17ff | 771 | req->length = USB_COMP_EP0_BUFSIZ; |
5030ec73 | 772 | |
a400cadc DB |
773 | /* composite driver infrastructure handles everything except |
774 | * the two control test requests. | |
775 | */ | |
776 | switch (ctrl->bRequest) { | |
777 | ||
778 | /* | |
779 | * These are the same vendor-specific requests supported by | |
780 | * Intel's USB 2.0 compliance test devices. We exceed that | |
781 | * device spec by allowing multiple-packet requests. | |
782 | * | |
783 | * NOTE: the Control-OUT data stays in req->buf ... better | |
784 | * would be copying it into a scratch buffer, so that other | |
785 | * requests may safely intervene. | |
786 | */ | |
787 | case 0x5b: /* control WRITE test -- fill the buffer */ | |
788 | if (ctrl->bRequestType != (USB_DIR_OUT|USB_TYPE_VENDOR)) | |
789 | goto unknown; | |
790 | if (w_value || w_index) | |
791 | break; | |
792 | /* just read that many bytes into the buffer */ | |
793 | if (w_length > req->length) | |
794 | break; | |
795 | value = w_length; | |
796 | break; | |
797 | case 0x5c: /* control READ test -- return the buffer */ | |
798 | if (ctrl->bRequestType != (USB_DIR_IN|USB_TYPE_VENDOR)) | |
799 | goto unknown; | |
800 | if (w_value || w_index) | |
801 | break; | |
802 | /* expect those bytes are still in the buffer; send back */ | |
803 | if (w_length > req->length) | |
804 | break; | |
805 | value = w_length; | |
806 | break; | |
807 | ||
808 | default: | |
809 | unknown: | |
810 | VDBG(c->cdev, | |
811 | "unknown control req%02x.%02x v%04x i%04x l%d\n", | |
812 | ctrl->bRequestType, ctrl->bRequest, | |
813 | w_value, w_index, w_length); | |
814 | } | |
815 | ||
816 | /* respond with data transfer or status phase? */ | |
817 | if (value >= 0) { | |
818 | VDBG(c->cdev, "source/sink req%02x.%02x v%04x i%04x l%d\n", | |
819 | ctrl->bRequestType, ctrl->bRequest, | |
820 | w_value, w_index, w_length); | |
821 | req->zero = 0; | |
822 | req->length = value; | |
823 | value = usb_ep_queue(c->cdev->gadget->ep0, req, GFP_ATOMIC); | |
824 | if (value < 0) | |
b4036ccd | 825 | ERROR(c->cdev, "source/sink response, err %d\n", |
a400cadc DB |
826 | value); |
827 | } | |
828 | ||
829 | /* device either stalls (value < 0) or reports success */ | |
830 | return value; | |
831 | } | |
832 | ||
cf9a08ae SAS |
833 | static struct usb_function *source_sink_alloc_func( |
834 | struct usb_function_instance *fi) | |
544aca39 | 835 | { |
cf9a08ae SAS |
836 | struct f_sourcesink *ss; |
837 | struct f_ss_opts *ss_opts; | |
544aca39 SAS |
838 | |
839 | ss = kzalloc(sizeof(*ss), GFP_KERNEL); | |
840 | if (!ss) | |
1045431f | 841 | return ERR_PTR(-ENOMEM); |
544aca39 | 842 | |
cf9a08ae | 843 | ss_opts = container_of(fi, struct f_ss_opts, func_inst); |
25d80151 AP |
844 | |
845 | mutex_lock(&ss_opts->lock); | |
846 | ss_opts->refcnt++; | |
847 | mutex_unlock(&ss_opts->lock); | |
848 | ||
897ee0e8 RB |
849 | ss->pattern = ss_opts->pattern; |
850 | ss->isoc_interval = ss_opts->isoc_interval; | |
851 | ss->isoc_maxpacket = ss_opts->isoc_maxpacket; | |
852 | ss->isoc_mult = ss_opts->isoc_mult; | |
853 | ss->isoc_maxburst = ss_opts->isoc_maxburst; | |
854 | ss->buflen = ss_opts->bulk_buflen; | |
0d6c3d96 PC |
855 | ss->bulk_qlen = ss_opts->bulk_qlen; |
856 | ss->iso_qlen = ss_opts->iso_qlen; | |
544aca39 SAS |
857 | |
858 | ss->function.name = "source/sink"; | |
859 | ss->function.bind = sourcesink_bind; | |
544aca39 SAS |
860 | ss->function.set_alt = sourcesink_set_alt; |
861 | ss->function.get_alt = sourcesink_get_alt; | |
862 | ss->function.disable = sourcesink_disable; | |
863 | ss->function.setup = sourcesink_setup; | |
cf9a08ae | 864 | ss->function.strings = sourcesink_strings; |
544aca39 | 865 | |
cf9a08ae SAS |
866 | ss->function.free_func = sourcesink_free_func; |
867 | ||
868 | return &ss->function; | |
544aca39 SAS |
869 | } |
870 | ||
25d80151 AP |
871 | static inline struct f_ss_opts *to_f_ss_opts(struct config_item *item) |
872 | { | |
873 | return container_of(to_config_group(item), struct f_ss_opts, | |
874 | func_inst.group); | |
875 | } | |
876 | ||
25d80151 AP |
877 | static void ss_attr_release(struct config_item *item) |
878 | { | |
879 | struct f_ss_opts *ss_opts = to_f_ss_opts(item); | |
880 | ||
881 | usb_put_function_instance(&ss_opts->func_inst); | |
882 | } | |
883 | ||
884 | static struct configfs_item_operations ss_item_ops = { | |
885 | .release = ss_attr_release, | |
25d80151 AP |
886 | }; |
887 | ||
208e61ac | 888 | static ssize_t f_ss_opts_pattern_show(struct config_item *item, char *page) |
25d80151 | 889 | { |
208e61ac | 890 | struct f_ss_opts *opts = to_f_ss_opts(item); |
25d80151 AP |
891 | int result; |
892 | ||
893 | mutex_lock(&opts->lock); | |
fa50bff6 | 894 | result = sprintf(page, "%u\n", opts->pattern); |
25d80151 AP |
895 | mutex_unlock(&opts->lock); |
896 | ||
897 | return result; | |
898 | } | |
899 | ||
208e61ac | 900 | static ssize_t f_ss_opts_pattern_store(struct config_item *item, |
25d80151 AP |
901 | const char *page, size_t len) |
902 | { | |
208e61ac | 903 | struct f_ss_opts *opts = to_f_ss_opts(item); |
25d80151 AP |
904 | int ret; |
905 | u8 num; | |
906 | ||
907 | mutex_lock(&opts->lock); | |
908 | if (opts->refcnt) { | |
909 | ret = -EBUSY; | |
910 | goto end; | |
911 | } | |
912 | ||
913 | ret = kstrtou8(page, 0, &num); | |
914 | if (ret) | |
915 | goto end; | |
916 | ||
917 | if (num != 0 && num != 1 && num != 2) { | |
918 | ret = -EINVAL; | |
919 | goto end; | |
920 | } | |
921 | ||
922 | opts->pattern = num; | |
923 | ret = len; | |
924 | end: | |
925 | mutex_unlock(&opts->lock); | |
926 | return ret; | |
927 | } | |
928 | ||
208e61ac | 929 | CONFIGFS_ATTR(f_ss_opts_, pattern); |
25d80151 | 930 | |
208e61ac | 931 | static ssize_t f_ss_opts_isoc_interval_show(struct config_item *item, char *page) |
25d80151 | 932 | { |
208e61ac | 933 | struct f_ss_opts *opts = to_f_ss_opts(item); |
25d80151 AP |
934 | int result; |
935 | ||
936 | mutex_lock(&opts->lock); | |
fa50bff6 | 937 | result = sprintf(page, "%u\n", opts->isoc_interval); |
25d80151 AP |
938 | mutex_unlock(&opts->lock); |
939 | ||
940 | return result; | |
941 | } | |
942 | ||
208e61ac | 943 | static ssize_t f_ss_opts_isoc_interval_store(struct config_item *item, |
25d80151 AP |
944 | const char *page, size_t len) |
945 | { | |
208e61ac | 946 | struct f_ss_opts *opts = to_f_ss_opts(item); |
25d80151 AP |
947 | int ret; |
948 | u8 num; | |
949 | ||
950 | mutex_lock(&opts->lock); | |
951 | if (opts->refcnt) { | |
952 | ret = -EBUSY; | |
953 | goto end; | |
954 | } | |
955 | ||
956 | ret = kstrtou8(page, 0, &num); | |
957 | if (ret) | |
958 | goto end; | |
959 | ||
960 | if (num > 16) { | |
961 | ret = -EINVAL; | |
962 | goto end; | |
963 | } | |
964 | ||
965 | opts->isoc_interval = num; | |
966 | ret = len; | |
967 | end: | |
968 | mutex_unlock(&opts->lock); | |
969 | return ret; | |
970 | } | |
971 | ||
208e61ac | 972 | CONFIGFS_ATTR(f_ss_opts_, isoc_interval); |
25d80151 | 973 | |
208e61ac | 974 | static ssize_t f_ss_opts_isoc_maxpacket_show(struct config_item *item, char *page) |
25d80151 | 975 | { |
208e61ac | 976 | struct f_ss_opts *opts = to_f_ss_opts(item); |
25d80151 AP |
977 | int result; |
978 | ||
979 | mutex_lock(&opts->lock); | |
fa50bff6 | 980 | result = sprintf(page, "%u\n", opts->isoc_maxpacket); |
25d80151 AP |
981 | mutex_unlock(&opts->lock); |
982 | ||
983 | return result; | |
984 | } | |
985 | ||
208e61ac | 986 | static ssize_t f_ss_opts_isoc_maxpacket_store(struct config_item *item, |
25d80151 AP |
987 | const char *page, size_t len) |
988 | { | |
208e61ac | 989 | struct f_ss_opts *opts = to_f_ss_opts(item); |
25d80151 AP |
990 | int ret; |
991 | u16 num; | |
992 | ||
993 | mutex_lock(&opts->lock); | |
994 | if (opts->refcnt) { | |
995 | ret = -EBUSY; | |
996 | goto end; | |
997 | } | |
998 | ||
999 | ret = kstrtou16(page, 0, &num); | |
1000 | if (ret) | |
1001 | goto end; | |
1002 | ||
1003 | if (num > 1024) { | |
1004 | ret = -EINVAL; | |
1005 | goto end; | |
1006 | } | |
1007 | ||
1008 | opts->isoc_maxpacket = num; | |
1009 | ret = len; | |
1010 | end: | |
1011 | mutex_unlock(&opts->lock); | |
1012 | return ret; | |
1013 | } | |
1014 | ||
208e61ac | 1015 | CONFIGFS_ATTR(f_ss_opts_, isoc_maxpacket); |
25d80151 | 1016 | |
208e61ac | 1017 | static ssize_t f_ss_opts_isoc_mult_show(struct config_item *item, char *page) |
25d80151 | 1018 | { |
208e61ac | 1019 | struct f_ss_opts *opts = to_f_ss_opts(item); |
25d80151 AP |
1020 | int result; |
1021 | ||
1022 | mutex_lock(&opts->lock); | |
fa50bff6 | 1023 | result = sprintf(page, "%u\n", opts->isoc_mult); |
25d80151 AP |
1024 | mutex_unlock(&opts->lock); |
1025 | ||
1026 | return result; | |
1027 | } | |
1028 | ||
208e61ac | 1029 | static ssize_t f_ss_opts_isoc_mult_store(struct config_item *item, |
25d80151 AP |
1030 | const char *page, size_t len) |
1031 | { | |
208e61ac | 1032 | struct f_ss_opts *opts = to_f_ss_opts(item); |
25d80151 AP |
1033 | int ret; |
1034 | u8 num; | |
1035 | ||
1036 | mutex_lock(&opts->lock); | |
1037 | if (opts->refcnt) { | |
1038 | ret = -EBUSY; | |
1039 | goto end; | |
1040 | } | |
1041 | ||
1042 | ret = kstrtou8(page, 0, &num); | |
1043 | if (ret) | |
1044 | goto end; | |
1045 | ||
1046 | if (num > 2) { | |
1047 | ret = -EINVAL; | |
1048 | goto end; | |
1049 | } | |
1050 | ||
1051 | opts->isoc_mult = num; | |
1052 | ret = len; | |
1053 | end: | |
1054 | mutex_unlock(&opts->lock); | |
1055 | return ret; | |
1056 | } | |
1057 | ||
208e61ac | 1058 | CONFIGFS_ATTR(f_ss_opts_, isoc_mult); |
25d80151 | 1059 | |
208e61ac | 1060 | static ssize_t f_ss_opts_isoc_maxburst_show(struct config_item *item, char *page) |
25d80151 | 1061 | { |
208e61ac | 1062 | struct f_ss_opts *opts = to_f_ss_opts(item); |
25d80151 AP |
1063 | int result; |
1064 | ||
1065 | mutex_lock(&opts->lock); | |
fa50bff6 | 1066 | result = sprintf(page, "%u\n", opts->isoc_maxburst); |
25d80151 AP |
1067 | mutex_unlock(&opts->lock); |
1068 | ||
1069 | return result; | |
1070 | } | |
1071 | ||
208e61ac | 1072 | static ssize_t f_ss_opts_isoc_maxburst_store(struct config_item *item, |
25d80151 AP |
1073 | const char *page, size_t len) |
1074 | { | |
208e61ac | 1075 | struct f_ss_opts *opts = to_f_ss_opts(item); |
25d80151 AP |
1076 | int ret; |
1077 | u8 num; | |
1078 | ||
1079 | mutex_lock(&opts->lock); | |
1080 | if (opts->refcnt) { | |
1081 | ret = -EBUSY; | |
1082 | goto end; | |
1083 | } | |
1084 | ||
1085 | ret = kstrtou8(page, 0, &num); | |
1086 | if (ret) | |
1087 | goto end; | |
1088 | ||
1089 | if (num > 15) { | |
1090 | ret = -EINVAL; | |
1091 | goto end; | |
1092 | } | |
1093 | ||
1094 | opts->isoc_maxburst = num; | |
1095 | ret = len; | |
1096 | end: | |
1097 | mutex_unlock(&opts->lock); | |
1098 | return ret; | |
1099 | } | |
1100 | ||
208e61ac | 1101 | CONFIGFS_ATTR(f_ss_opts_, isoc_maxburst); |
25d80151 | 1102 | |
208e61ac | 1103 | static ssize_t f_ss_opts_bulk_buflen_show(struct config_item *item, char *page) |
25d80151 | 1104 | { |
208e61ac | 1105 | struct f_ss_opts *opts = to_f_ss_opts(item); |
25d80151 AP |
1106 | int result; |
1107 | ||
1108 | mutex_lock(&opts->lock); | |
fa50bff6 | 1109 | result = sprintf(page, "%u\n", opts->bulk_buflen); |
25d80151 AP |
1110 | mutex_unlock(&opts->lock); |
1111 | ||
1112 | return result; | |
1113 | } | |
1114 | ||
208e61ac | 1115 | static ssize_t f_ss_opts_bulk_buflen_store(struct config_item *item, |
25d80151 AP |
1116 | const char *page, size_t len) |
1117 | { | |
208e61ac | 1118 | struct f_ss_opts *opts = to_f_ss_opts(item); |
25d80151 AP |
1119 | int ret; |
1120 | u32 num; | |
1121 | ||
1122 | mutex_lock(&opts->lock); | |
1123 | if (opts->refcnt) { | |
1124 | ret = -EBUSY; | |
1125 | goto end; | |
1126 | } | |
1127 | ||
1128 | ret = kstrtou32(page, 0, &num); | |
1129 | if (ret) | |
1130 | goto end; | |
1131 | ||
1132 | opts->bulk_buflen = num; | |
1133 | ret = len; | |
1134 | end: | |
1135 | mutex_unlock(&opts->lock); | |
1136 | return ret; | |
1137 | } | |
1138 | ||
208e61ac | 1139 | CONFIGFS_ATTR(f_ss_opts_, bulk_buflen); |
25d80151 | 1140 | |
0d6c3d96 PC |
1141 | static ssize_t f_ss_opts_bulk_qlen_show(struct config_item *item, char *page) |
1142 | { | |
1143 | struct f_ss_opts *opts = to_f_ss_opts(item); | |
1144 | int result; | |
1145 | ||
1146 | mutex_lock(&opts->lock); | |
1147 | result = sprintf(page, "%u\n", opts->bulk_qlen); | |
1148 | mutex_unlock(&opts->lock); | |
1149 | ||
1150 | return result; | |
1151 | } | |
1152 | ||
1153 | static ssize_t f_ss_opts_bulk_qlen_store(struct config_item *item, | |
1154 | const char *page, size_t len) | |
1155 | { | |
1156 | struct f_ss_opts *opts = to_f_ss_opts(item); | |
1157 | int ret; | |
1158 | u32 num; | |
1159 | ||
1160 | mutex_lock(&opts->lock); | |
1161 | if (opts->refcnt) { | |
1162 | ret = -EBUSY; | |
1163 | goto end; | |
1164 | } | |
1165 | ||
1166 | ret = kstrtou32(page, 0, &num); | |
1167 | if (ret) | |
1168 | goto end; | |
1169 | ||
1170 | opts->bulk_qlen = num; | |
1171 | ret = len; | |
1172 | end: | |
1173 | mutex_unlock(&opts->lock); | |
1174 | return ret; | |
1175 | } | |
1176 | ||
1177 | CONFIGFS_ATTR(f_ss_opts_, bulk_qlen); | |
1178 | ||
1179 | static ssize_t f_ss_opts_iso_qlen_show(struct config_item *item, char *page) | |
1180 | { | |
1181 | struct f_ss_opts *opts = to_f_ss_opts(item); | |
1182 | int result; | |
1183 | ||
1184 | mutex_lock(&opts->lock); | |
1185 | result = sprintf(page, "%u\n", opts->iso_qlen); | |
1186 | mutex_unlock(&opts->lock); | |
1187 | ||
1188 | return result; | |
1189 | } | |
1190 | ||
1191 | static ssize_t f_ss_opts_iso_qlen_store(struct config_item *item, | |
1192 | const char *page, size_t len) | |
1193 | { | |
1194 | struct f_ss_opts *opts = to_f_ss_opts(item); | |
1195 | int ret; | |
1196 | u32 num; | |
1197 | ||
1198 | mutex_lock(&opts->lock); | |
1199 | if (opts->refcnt) { | |
1200 | ret = -EBUSY; | |
1201 | goto end; | |
1202 | } | |
1203 | ||
1204 | ret = kstrtou32(page, 0, &num); | |
1205 | if (ret) | |
1206 | goto end; | |
1207 | ||
1208 | opts->iso_qlen = num; | |
1209 | ret = len; | |
1210 | end: | |
1211 | mutex_unlock(&opts->lock); | |
1212 | return ret; | |
1213 | } | |
1214 | ||
1215 | CONFIGFS_ATTR(f_ss_opts_, iso_qlen); | |
1216 | ||
25d80151 | 1217 | static struct configfs_attribute *ss_attrs[] = { |
208e61ac CH |
1218 | &f_ss_opts_attr_pattern, |
1219 | &f_ss_opts_attr_isoc_interval, | |
1220 | &f_ss_opts_attr_isoc_maxpacket, | |
1221 | &f_ss_opts_attr_isoc_mult, | |
1222 | &f_ss_opts_attr_isoc_maxburst, | |
1223 | &f_ss_opts_attr_bulk_buflen, | |
0d6c3d96 PC |
1224 | &f_ss_opts_attr_bulk_qlen, |
1225 | &f_ss_opts_attr_iso_qlen, | |
25d80151 AP |
1226 | NULL, |
1227 | }; | |
1228 | ||
97363902 | 1229 | static const struct config_item_type ss_func_type = { |
25d80151 AP |
1230 | .ct_item_ops = &ss_item_ops, |
1231 | .ct_attrs = ss_attrs, | |
1232 | .ct_owner = THIS_MODULE, | |
1233 | }; | |
1234 | ||
9890e330 | 1235 | static void source_sink_free_instance(struct usb_function_instance *fi) |
544aca39 | 1236 | { |
cf9a08ae SAS |
1237 | struct f_ss_opts *ss_opts; |
1238 | ||
1239 | ss_opts = container_of(fi, struct f_ss_opts, func_inst); | |
1240 | kfree(ss_opts); | |
544aca39 | 1241 | } |
cf9a08ae SAS |
1242 | |
1243 | static struct usb_function_instance *source_sink_alloc_inst(void) | |
1244 | { | |
1245 | struct f_ss_opts *ss_opts; | |
1246 | ||
1247 | ss_opts = kzalloc(sizeof(*ss_opts), GFP_KERNEL); | |
1248 | if (!ss_opts) | |
1249 | return ERR_PTR(-ENOMEM); | |
25d80151 | 1250 | mutex_init(&ss_opts->lock); |
9890e330 | 1251 | ss_opts->func_inst.free_func_inst = source_sink_free_instance; |
25d80151 AP |
1252 | ss_opts->isoc_interval = GZERO_ISOC_INTERVAL; |
1253 | ss_opts->isoc_maxpacket = GZERO_ISOC_MAXPACKET; | |
1254 | ss_opts->bulk_buflen = GZERO_BULK_BUFLEN; | |
0d6c3d96 PC |
1255 | ss_opts->bulk_qlen = GZERO_SS_BULK_QLEN; |
1256 | ss_opts->iso_qlen = GZERO_SS_ISO_QLEN; | |
25d80151 AP |
1257 | |
1258 | config_group_init_type_name(&ss_opts->func_inst.group, "", | |
1259 | &ss_func_type); | |
1260 | ||
cf9a08ae SAS |
1261 | return &ss_opts->func_inst; |
1262 | } | |
1263 | DECLARE_USB_FUNCTION(SourceSink, source_sink_alloc_inst, | |
1264 | source_sink_alloc_func); | |
1265 | ||
1266 | static int __init sslb_modinit(void) | |
1267 | { | |
1268 | int ret; | |
1269 | ||
1270 | ret = usb_function_register(&SourceSinkusb_func); | |
1271 | if (ret) | |
1272 | return ret; | |
1273 | ret = lb_modinit(); | |
1274 | if (ret) | |
1275 | usb_function_unregister(&SourceSinkusb_func); | |
1276 | return ret; | |
1277 | } | |
1278 | static void __exit sslb_modexit(void) | |
1279 | { | |
1280 | usb_function_unregister(&SourceSinkusb_func); | |
1281 | lb_modexit(); | |
1282 | } | |
1283 | module_init(sslb_modinit); | |
1284 | module_exit(sslb_modexit); | |
1285 | ||
1286 | MODULE_LICENSE("GPL"); |