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