Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge tag 'usb-for-v4.13' of git://git.kernel.org/pub/scm/linux/kernel/git/balbi/usb into usb-testing

Felipe writes:

usb: changes for v4.13 merge window

This time around we have a total of 57 non-merge commits. A list of
most important changes follows:

- Improvements to dwc3 tracing interface
- Initial dual-role support for dwc3
- Improvements to how we handle DMA resources in dwc3
- A new f_uac1 implementation which much more flexible
- Removal of AVR32 bits
- Improvements to f_mass_storage driver

+5127 -2073
+8 -8
Documentation/ABI/stable/sysfs-class-udc
··· 55 55 Indicates the maximum USB speed supported by this port. 56 56 Users: 57 57 58 - What: /sys/class/udc/<udc>/maximum_speed 59 - Date: June 2011 60 - KernelVersion: 3.1 61 - Contact: Felipe Balbi <balbi@kernel.org> 62 - Description: 63 - Indicates the maximum USB speed supported by this port. 64 - Users: 65 - 66 58 What: /sys/class/udc/<udc>/soft_connect 67 59 Date: June 2011 68 60 KernelVersion: 3.1 ··· 82 90 'reconnecting', 'unauthenticated', 'default', 'addressed', 83 91 'configured', and 'suspended'; however not all USB Device 84 92 Controllers support reporting all states. 93 + Users: 94 + 95 + What: /sys/class/udc/<udc>/function 96 + Date: June 2017 97 + KernelVersion: 4.13 98 + Contact: Felipe Balbi <balbi@kernel.org> 99 + Description: 100 + Prints out name of currently running USB Gadget Driver. 85 101 Users:
+10 -8
Documentation/ABI/testing/configfs-usb-gadget-uac1
··· 1 1 What: /config/usb-gadget/gadget/functions/uac1.name 2 - Date: Sep 2014 3 - KernelVersion: 3.18 2 + Date: June 2017 3 + KernelVersion: 4.14 4 4 Description: 5 5 The attributes: 6 6 7 - audio_buf_size - audio buffer size 8 - fn_cap - capture pcm device file name 9 - fn_cntl - control device file name 10 - fn_play - playback pcm device file name 11 - req_buf_size - ISO OUT endpoint request buffer size 12 - req_count - ISO OUT endpoint request count 7 + c_chmask - capture channel mask 8 + c_srate - capture sampling rate 9 + c_ssize - capture sample size (bytes) 10 + p_chmask - playback channel mask 11 + p_srate - playback sampling rate 12 + p_ssize - playback sample size (bytes) 13 + req_number - the number of pre-allocated request 14 + for both capture and playback
+12
Documentation/ABI/testing/configfs-usb-gadget-uac1_legacy
··· 1 + What: /config/usb-gadget/gadget/functions/uac1_legacy.name 2 + Date: Sep 2014 3 + KernelVersion: 3.18 4 + Description: 5 + The attributes: 6 + 7 + audio_buf_size - audio buffer size 8 + fn_cap - capture pcm device file name 9 + fn_cntl - control device file name 10 + fn_play - playback pcm device file name 11 + req_buf_size - ISO OUT endpoint request buffer size 12 + req_count - ISO OUT endpoint request count
+2
Documentation/devicetree/bindings/usb/dwc3.txt
··· 45 45 a free-running PHY clock. 46 46 - snps,dis-del-phy-power-chg-quirk: when set core will change PHY power 47 47 from P0 to P1/P2/P3 without delay. 48 + - snps,dis-tx-ipgap-linecheck-quirk: when set, disable u2mac linestate check 49 + during HS transmit. 48 50 - snps,is-utmi-l1-suspend: true when DWC3 asserts output signal 49 51 utmi_l1_suspend_n, false when asserts utmi_sleep_n 50 52 - snps,hird-threshold: HIRD threshold
+21
Documentation/devicetree/bindings/usb/iproc-udc.txt
··· 1 + Broadcom IPROC USB Device controller. 2 + 3 + The device node is used for UDCs integrated into Broadcom's 4 + iProc family (Northstar2, Cygnus) of SoCs'. The UDC is based 5 + on Synopsys Designware Cores AHB Subsystem Device Controller 6 + IP. 7 + 8 + Required properties: 9 + - compatible: Add the compatibility strings for supported platforms. 10 + For Broadcom NS2 platform, add "brcm,ns2-udc","brcm,iproc-udc". 11 + For Broadcom Cygnus platform, add "brcm,cygnus-udc", "brcm,iproc-udc". 12 + - reg: Offset and length of UDC register set 13 + - interrupts: description of interrupt line 14 + - phys: phandle to phy node. 15 + 16 + Example: 17 + udc_dwc: usb@664e0000 { 18 + compatible = "brcm,ns2-udc", "brcm,iproc-udc"; 19 + reg = <0x664e0000 0x2000>; 20 + interrupts = <GIC_SPI 424 IRQ_TYPE_LEVEL_HIGH>; 21 + phys = <&usbdrd_phy>;
+712
Documentation/driver-api/usb/dwc3.rst
··· 1 + =============================================================== 2 + Synopsys DesignWare Core SuperSpeed USB 3.0 Controller 3 + =============================================================== 4 + 5 + :Author: Felipe Balbi <felipe.balbi@linux.intel.com> 6 + :Date: April 2017 7 + 8 + Introduction 9 + ============ 10 + 11 + The *Synopsys DesignWare Core SuperSpeed USB 3.0 Controller* 12 + (hereinafter referred to as *DWC3*) is a USB SuperSpeed compliant 13 + controller which can be configured in one of 4 ways: 14 + 15 + 1. Peripheral-only configuration 16 + 2. Host-only configuration 17 + 3. Dual-Role configuration 18 + 4. Hub configuration 19 + 20 + Linux currently supports several versions of this controller. In all 21 + likelyhood, the version in your SoC is already supported. At the time 22 + of this writing, known tested versions range from 2.02a to 3.10a. As a 23 + rule of thumb, anything above 2.02a should work reliably well. 24 + 25 + Currently, we have many known users for this driver. In alphabetical 26 + order: 27 + 28 + 1. Cavium 29 + 2. Intel Corporation 30 + 3. Qualcomm 31 + 4. Rockchip 32 + 5. ST 33 + 6. Samsung 34 + 7. Texas Instruments 35 + 8. Xilinx 36 + 37 + Summary of Features 38 + ====================== 39 + 40 + For details about features supported by your version of DWC3, consult 41 + your IP team and/or *Synopsys DesignWare Core SuperSpeed USB 3.0 42 + Controller Databook*. Following is a list of features supported by the 43 + driver at the time of this writing: 44 + 45 + 1. Up to 16 bidirectional endpoints (including the control 46 + pipe - ep0) 47 + 2. Flexible endpoint configuration 48 + 3. Simultaneous IN and OUT transfer support 49 + 4. Scatter-list support 50 + 5. Up to 256 TRBs [#trb]_ per endpoint 51 + 6. Support for all transfer types (*Control*, *Bulk*, 52 + *Interrupt*, and *Isochronous*) 53 + 7. SuperSpeed Bulk Streams 54 + 8. Link Power Management 55 + 9. Trace Events for debugging 56 + 10. DebugFS [#debugfs]_ interface 57 + 58 + These features have all been exercised with many of the **in-tree** 59 + gadget drivers. We have verified both *ConfigFS* [#configfs]_ and 60 + legacy gadget drivers. 61 + 62 + Driver Design 63 + ============== 64 + 65 + The DWC3 driver sits on the *drivers/usb/dwc3/* directory. All files 66 + related to this driver are in this one directory. This makes it easy 67 + for new-comers to read the code and understand how it behaves. 68 + 69 + Because of DWC3's configuration flexibility, the driver is a little 70 + complex in some places but it should be rather straightforward to 71 + understand. 72 + 73 + The biggest part of the driver refers to the Gadget API. 74 + 75 + Known Limitations 76 + =================== 77 + 78 + Like any other HW, DWC3 has its own set of limitations. To avoid 79 + constant questions about such problems, we decided to document them 80 + here and have a single location to where we could point users. 81 + 82 + OUT Transfer Size Requirements 83 + --------------------------------- 84 + 85 + According to Synopsys Databook, all OUT transfer TRBs [#trb]_ must 86 + have their *size* field set to a value which is integer divisible by 87 + the endpoint's *wMaxPacketSize*. This means that *e.g.* in order to 88 + receive a Mass Storage *CBW* [#cbw]_, req->length must either be set 89 + to a value that's divisible by *wMaxPacketSize* (1024 on SuperSpeed, 90 + 512 on HighSpeed, etc), or DWC3 driver must add a Chained TRB pointing 91 + to a throw-away buffer for the remaining length. Without this, OUT 92 + transfers will **NOT** start. 93 + 94 + Note that as of this writing, this won't be a problem because DWC3 is 95 + fully capable of appending a chained TRB for the remaining length and 96 + completely hide this detail from the gadget driver. It's still worth 97 + mentioning because this seems to be the largest source of queries 98 + about DWC3 and *non-working transfers*. 99 + 100 + TRB Ring Size Limitation 101 + ------------------------- 102 + 103 + We, currently, have a hard limit of 256 TRBs [#trb]_ per endpoint, 104 + with the last TRB being a Link TRB [#link_trb]_ pointing back to the 105 + first. This limit is arbitrary but it has the benefit of adding up to 106 + exactly 4096 bytes, or 1 Page. 107 + 108 + DWC3 driver will try its best to cope with more than 255 requests and, 109 + for the most part, it should work normally. However this is not 110 + something that has been exercised very frequently. If you experience 111 + any problems, see section **Reporting Bugs** below. 112 + 113 + Reporting Bugs 114 + ================ 115 + 116 + Whenever you encounter a problem with DWC3, first and foremost you 117 + should make sure that: 118 + 119 + 1. You're running latest tag from `Linus' tree`_ 120 + 2. You can reproduce the error without any out-of-tree changes 121 + to DWC3 122 + 3. You have checked that it's not a fault on the host machine 123 + 124 + After all these are verified, then here's how to capture enough 125 + information so we can be of any help to you. 126 + 127 + Required Information 128 + --------------------- 129 + 130 + DWC3 relies exclusively on Trace Events for debugging. Everything is 131 + exposed there, with some extra bits being exposed to DebugFS 132 + [#debugfs]_. 133 + 134 + In order to capture DWC3's Trace Events you should run the following 135 + commands **before** plugging the USB cable to a host machine: 136 + 137 + .. code-block:: sh 138 + 139 + # mkdir -p /d 140 + # mkdir -p /t 141 + # mount -t debugfs none /d 142 + # mount -t tracefs none /t 143 + # echo 81920 > /t/buffer_size_kb 144 + # echo 1 > /t/events/dwc3/enable 145 + 146 + After this is done, you can connect your USB cable and reproduce the 147 + problem. As soon as the fault is reproduced, make a copy of files 148 + ``trace`` and ``regdump``, like so: 149 + 150 + .. code-block:: sh 151 + 152 + # cp /t/trace /root/trace.txt 153 + # cat /d/*dwc3*/regdump > /root/regdump.txt 154 + 155 + Make sure to compress ``trace.txt`` and ``regdump.txt`` in a tarball 156 + and email it to `me`_ with `linux-usb`_ in Cc. If you want to be extra 157 + sure that I'll help you, write your subject line in the following 158 + format: 159 + 160 + **[BUG REPORT] usb: dwc3: Bug while doing XYZ** 161 + 162 + On the email body, make sure to detail what you doing, which gadget 163 + driver you were using, how to reproduce the problem, what SoC you're 164 + using, which OS (and its version) was running on the Host machine. 165 + 166 + With all this information, we should be able to understand what's 167 + going on and be helpful to you. 168 + 169 + Debugging 170 + =========== 171 + 172 + First and foremost a disclaimer:: 173 + 174 + DISCLAIMER: The information available on DebugFS and/or TraceFS can 175 + change at any time at any Major Linux Kernel Release. If writing 176 + scripts, do **NOT** assume information to be available in the 177 + current format. 178 + 179 + With that out of the way, let's carry on. 180 + 181 + If you're willing to debug your own problem, you deserve a round of 182 + applause :-) 183 + 184 + Anyway, there isn't much to say here other than Trace Events will be 185 + really helpful in figuring out issues with DWC3. Also, access to 186 + Synopsys Databook will be **really** valuable in this case. 187 + 188 + A USB Sniffer can be helpful at times but it's not entirely required, 189 + there's a lot that can be understood without looking at the wire. 190 + 191 + Feel free to email `me`_ and Cc `linux-usb`_ if you need any help. 192 + 193 + ``DebugFS`` 194 + ------------- 195 + 196 + ``DebugFS`` is very good for gathering snapshots of what's going on 197 + with DWC3 and/or any endpoint. 198 + 199 + On DWC3's ``DebugFS`` directory, you will find the following files and 200 + directories: 201 + 202 + ``ep[0..15]{in,out}/`` 203 + ``link_state`` 204 + ``regdump`` 205 + ``testmode`` 206 + 207 + ``link_state`` 208 + `````````````` 209 + 210 + When read, ``link_state`` will print out one of ``U0``, ``U1``, 211 + ``U2``, ``U3``, ``SS.Disabled``, ``RX.Detect``, ``SS.Inactive``, 212 + ``Polling``, ``Recovery``, ``Hot Reset``, ``Compliance``, 213 + ``Loopback``, ``Reset``, ``Resume`` or ``UNKNOWN link state``. 214 + 215 + This file can also be written to in order to force link to one of the 216 + states above. 217 + 218 + ``regdump`` 219 + ````````````` 220 + 221 + File name is self-explanatory. When read, ``regdump`` will print out a 222 + register dump of DWC3. Note that this file can be grepped to find the 223 + information you want. 224 + 225 + ``testmode`` 226 + `````````````` 227 + 228 + When read, ``testmode`` will print out a name of one of the specified 229 + USB 2.0 Testmodes (``test_j``, ``test_k``, ``test_se0_nak``, 230 + ``test_packet``, ``test_force_enable``) or the string ``no test`` in 231 + case no tests are currently being executed. 232 + 233 + In order to start any of these test modes, the same strings can be 234 + written to the file and DWC3 will enter the requested test mode. 235 + 236 + 237 + ``ep[0..15]{in,out}`` 238 + `````````````````````` 239 + 240 + For each endpoint we expose one directory following the naming 241 + convention ``ep$num$dir`` *(ep0in, ep0out, ep1in, ...)*. Inside each 242 + of these directories you will find the following files: 243 + 244 + ``descriptor_fetch_queue`` 245 + ``event_queue`` 246 + ``rx_fifo_queue`` 247 + ``rx_info_queue`` 248 + ``rx_request_queue`` 249 + ``transfer_type`` 250 + ``trb_ring`` 251 + ``tx_fifo_queue`` 252 + ``tx_request_queue`` 253 + 254 + With access to Synopsys Databook, you can decode the information on 255 + them. 256 + 257 + ``transfer_type`` 258 + ~~~~~~~~~~~~~~~~~~ 259 + 260 + When read, ``transfer_type`` will print out one of ``control``, 261 + ``bulk``, ``interrupt`` or ``isochronous`` depending on what the 262 + endpoint descriptor says. If the endpoint hasn't been enabled yet, it 263 + will print ``--``. 264 + 265 + ``trb_ring`` 266 + ~~~~~~~~~~~~~ 267 + 268 + When read, ``trb_ring`` will print out details about all TRBs on the 269 + ring. It will also tell you where our enqueue and dequeue pointers are 270 + located in the ring: 271 + 272 + .. code-block:: sh 273 + 274 + buffer_addr,size,type,ioc,isp_imi,csp,chn,lst,hwo 275 + 000000002c754000,481,normal,1,0,1,0,0,0 276 + 000000002c75c000,481,normal,1,0,1,0,0,0 277 + 000000002c780000,481,normal,1,0,1,0,0,0 278 + 000000002c788000,481,normal,1,0,1,0,0,0 279 + 000000002c78c000,481,normal,1,0,1,0,0,0 280 + 000000002c754000,481,normal,1,0,1,0,0,0 281 + 000000002c75c000,481,normal,1,0,1,0,0,0 282 + 000000002c784000,481,normal,1,0,1,0,0,0 283 + 000000002c788000,481,normal,1,0,1,0,0,0 284 + 000000002c78c000,481,normal,1,0,1,0,0,0 285 + 000000002c790000,481,normal,1,0,1,0,0,0 286 + 000000002c758000,481,normal,1,0,1,0,0,0 287 + 000000002c780000,481,normal,1,0,1,0,0,0 288 + 000000002c788000,481,normal,1,0,1,0,0,0 289 + 000000002c790000,481,normal,1,0,1,0,0,0 290 + 000000002c758000,481,normal,1,0,1,0,0,0 291 + 000000002c780000,481,normal,1,0,1,0,0,0 292 + 000000002c784000,481,normal,1,0,1,0,0,0 293 + 000000002c788000,481,normal,1,0,1,0,0,0 294 + 000000002c78c000,481,normal,1,0,1,0,0,0 295 + 000000002c754000,481,normal,1,0,1,0,0,0 296 + 000000002c758000,481,normal,1,0,1,0,0,0 297 + 000000002c780000,481,normal,1,0,1,0,0,0 298 + 000000002c784000,481,normal,1,0,1,0,0,0 299 + 000000002c78c000,481,normal,1,0,1,0,0,0 300 + 000000002c790000,481,normal,1,0,1,0,0,0 301 + 000000002c758000,481,normal,1,0,1,0,0,0 302 + 000000002c780000,481,normal,1,0,1,0,0,0 303 + 000000002c788000,481,normal,1,0,1,0,0,0 304 + 000000002c790000,481,normal,1,0,1,0,0,0 305 + 000000002c758000,481,normal,1,0,1,0,0,0 306 + 000000002c780000,481,normal,1,0,1,0,0,0 307 + 000000002c788000,481,normal,1,0,1,0,0,0 308 + 000000002c790000,481,normal,1,0,1,0,0,0 309 + 000000002c758000,481,normal,1,0,1,0,0,0 310 + 000000002c780000,481,normal,1,0,1,0,0,0 311 + 000000002c788000,481,normal,1,0,1,0,0,0 312 + 000000002c790000,481,normal,1,0,1,0,0,0 313 + 000000002c758000,481,normal,1,0,1,0,0,0 314 + 000000002c780000,481,normal,1,0,1,0,0,0 315 + 000000002c788000,481,normal,1,0,1,0,0,0 316 + 000000002c790000,481,normal,1,0,1,0,0,0 317 + 000000002c758000,481,normal,1,0,1,0,0,0 318 + 000000002c780000,481,normal,1,0,1,0,0,0 319 + 000000002c788000,481,normal,1,0,1,0,0,0 320 + 000000002c790000,481,normal,1,0,1,0,0,0 321 + 000000002c758000,481,normal,1,0,1,0,0,0 322 + 000000002c780000,481,normal,1,0,1,0,0,0 323 + 000000002c788000,481,normal,1,0,1,0,0,0 324 + 000000002c790000,481,normal,1,0,1,0,0,0 325 + 000000002c758000,481,normal,1,0,1,0,0,0 326 + 000000002c780000,481,normal,1,0,1,0,0,0 327 + 000000002c788000,481,normal,1,0,1,0,0,0 328 + 000000002c790000,481,normal,1,0,1,0,0,0 329 + 000000002c758000,481,normal,1,0,1,0,0,0 330 + 000000002c780000,481,normal,1,0,1,0,0,0 331 + 000000002c78c000,481,normal,1,0,1,0,0,0 332 + 000000002c784000,481,normal,1,0,1,0,0,0 333 + 000000002c788000,481,normal,1,0,1,0,0,0 334 + 000000002c78c000,481,normal,1,0,1,0,0,0 335 + 000000002c754000,481,normal,1,0,1,0,0,0 336 + 000000002c758000,481,normal,1,0,1,0,0,0 337 + 000000002c780000,481,normal,1,0,1,0,0,0 338 + 000000002c788000,481,normal,1,0,1,0,0,0 339 + 000000002c790000,481,normal,1,0,1,0,0,0 340 + 000000002c758000,481,normal,1,0,1,0,0,0 341 + 000000002c780000,481,normal,1,0,1,0,0,0 342 + 000000002c758000,481,normal,1,0,1,0,0,0 343 + 000000002c780000,481,normal,1,0,1,0,0,0 344 + 000000002c78c000,481,normal,1,0,1,0,0,0 345 + 000000002c75c000,481,normal,1,0,1,0,0,0 346 + 000000002c78c000,481,normal,1,0,1,0,0,0 347 + 000000002c780000,481,normal,1,0,1,0,0,0 348 + 000000002c754000,481,normal,1,0,1,0,0,0 349 + 000000002c788000,481,normal,1,0,1,0,0,0 350 + 000000002c754000,481,normal,1,0,1,0,0,0 351 + 000000002c780000,481,normal,1,0,1,0,0,0 352 + 000000002c788000,481,normal,1,0,1,0,0,0 353 + 000000002c78c000,481,normal,1,0,1,0,0,0 354 + 000000002c790000,481,normal,1,0,1,0,0,0 355 + 000000002c754000,481,normal,1,0,1,0,0,0 356 + 000000002c758000,481,normal,1,0,1,0,0,0 357 + 000000002c75c000,481,normal,1,0,1,0,0,0 358 + 000000002c780000,481,normal,1,0,1,0,0,0 359 + 000000002c784000,481,normal,1,0,1,0,0,0 360 + 000000002c788000,481,normal,1,0,1,0,0,0 361 + 000000002c78c000,481,normal,1,0,1,0,0,0 362 + 000000002c790000,481,normal,1,0,1,0,0,0 363 + 000000002c754000,481,normal,1,0,1,0,0,0 364 + 000000002c758000,481,normal,1,0,1,0,0,0 365 + 000000002c75c000,512,normal,1,0,1,0,0,1 D 366 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 E 367 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 368 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 369 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 370 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 371 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 372 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 373 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 374 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 375 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 376 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 377 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 378 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 379 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 380 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 381 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 382 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 383 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 384 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 385 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 386 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 387 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 388 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 389 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 390 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 391 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 392 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 393 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 394 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 395 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 396 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 397 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 398 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 399 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 400 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 401 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 402 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 403 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 404 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 405 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 406 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 407 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 408 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 409 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 410 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 411 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 412 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 413 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 414 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 415 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 416 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 417 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 418 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 419 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 420 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 421 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 422 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 423 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 424 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 425 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 426 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 427 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 428 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 429 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 430 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 431 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 432 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 433 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 434 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 435 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 436 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 437 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 438 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 439 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 440 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 441 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 442 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 443 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 444 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 445 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 446 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 447 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 448 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 449 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 450 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 451 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 452 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 453 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 454 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 455 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 456 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 457 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 458 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 459 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 460 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 461 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 462 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 463 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 464 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 465 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 466 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 467 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 468 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 469 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 470 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 471 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 472 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 473 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 474 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 475 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 476 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 477 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 478 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 479 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 480 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 481 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 482 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 483 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 484 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 485 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 486 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 487 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 488 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 489 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 490 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 491 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 492 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 493 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 494 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 495 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 496 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 497 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 498 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 499 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 500 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 501 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 502 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 503 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 504 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 505 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 506 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 507 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 508 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 509 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 510 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 511 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 512 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 513 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 514 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 515 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 516 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 517 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 518 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 519 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 520 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 521 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 522 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 523 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 524 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 525 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 526 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 527 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 528 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 529 + 0000000000000000,0,UNKNOWN,0,0,0,0,0,0 530 + 00000000381ab000,0,link,0,0,0,0,0,1 531 + 532 + 533 + Trace Events 534 + ------------- 535 + 536 + DWC3 also provides several trace events which help us gathering 537 + information about the behavior of the driver during runtime. 538 + 539 + In order to use these events, you must enable ``CONFIG_FTRACE`` in 540 + your kernel config. 541 + 542 + For details about how enable DWC3 events, see section **Reporting 543 + Bugs**. 544 + 545 + The following subsections will give details about each Event Class and 546 + each Event defined by DWC3. 547 + 548 + MMIO 549 + ``````` 550 + 551 + It is sometimes useful to look at every MMIO access when looking for 552 + bugs. Because of that, DWC3 offers two Trace Events (one for 553 + dwc3_readl() and one for dwc3_writel()). ``TP_printk`` follows:: 554 + 555 + TP_printk("addr %p value %08x", __entry->base + __entry->offset, 556 + __entry->value) 557 + 558 + Interrupt Events 559 + ```````````````` 560 + 561 + Every IRQ event can be logged and decoded into a human readable 562 + string. Because every event will be different, we don't give an 563 + example other than the ``TP_printk`` format used:: 564 + 565 + TP_printk("event (%08x): %s", __entry->event, 566 + dwc3_decode_event(__entry->event, __entry->ep0state)) 567 + 568 + Control Request 569 + ````````````````` 570 + 571 + Every USB Control Request can be logged to the trace buffer. The 572 + output format is:: 573 + 574 + TP_printk("%s", dwc3_decode_ctrl(__entry->bRequestType, 575 + __entry->bRequest, __entry->wValue, 576 + __entry->wIndex, __entry->wLength) 577 + ) 578 + 579 + Note that Standard Control Requests will be decoded into 580 + human-readable strings with their respective arguments. Class and 581 + Vendor requests will be printed out a sequence of 8 bytes in hex 582 + format. 583 + 584 + Lifetime of a ``struct usb_request`` 585 + ``````````````````````````````````````` 586 + 587 + The entire lifetime of a ``struct usb_request`` can be tracked on the 588 + trace buffer. We have one event for each of allocation, free, 589 + queueing, dequeueing, and giveback. Output format is:: 590 + 591 + TP_printk("%s: req %p length %u/%u %s%s%s ==> %d", 592 + __get_str(name), __entry->req, __entry->actual, __entry->length, 593 + __entry->zero ? "Z" : "z", 594 + __entry->short_not_ok ? "S" : "s", 595 + __entry->no_interrupt ? "i" : "I", 596 + __entry->status 597 + ) 598 + 599 + Generic Commands 600 + ```````````````````` 601 + 602 + We can log and decode every Generic Command with its completion 603 + code. Format is:: 604 + 605 + TP_printk("cmd '%s' [%x] param %08x --> status: %s", 606 + dwc3_gadget_generic_cmd_string(__entry->cmd), 607 + __entry->cmd, __entry->param, 608 + dwc3_gadget_generic_cmd_status_string(__entry->status) 609 + ) 610 + 611 + Endpoint Commands 612 + ```````````````````` 613 + 614 + Endpoints commands can also be logged together with completion 615 + code. Format is:: 616 + 617 + TP_printk("%s: cmd '%s' [%d] params %08x %08x %08x --> status: %s", 618 + __get_str(name), dwc3_gadget_ep_cmd_string(__entry->cmd), 619 + __entry->cmd, __entry->param0, 620 + __entry->param1, __entry->param2, 621 + dwc3_ep_cmd_status_string(__entry->cmd_status) 622 + ) 623 + 624 + Lifetime of a ``TRB`` 625 + `````````````````````` 626 + 627 + A ``TRB`` Lifetime is simple. We are either preparing a ``TRB`` or 628 + completing it. With these two events, we can see how a ``TRB`` changes 629 + over time. Format is:: 630 + 631 + TP_printk("%s: %d/%d trb %p buf %08x%08x size %s%d ctrl %08x (%c%c%c%c:%c%c:%s)", 632 + __get_str(name), __entry->queued, __entry->allocated, 633 + __entry->trb, __entry->bph, __entry->bpl, 634 + ({char *s; 635 + int pcm = ((__entry->size >> 24) & 3) + 1; 636 + switch (__entry->type) { 637 + case USB_ENDPOINT_XFER_INT: 638 + case USB_ENDPOINT_XFER_ISOC: 639 + switch (pcm) { 640 + case 1: 641 + s = "1x "; 642 + break; 643 + case 2: 644 + s = "2x "; 645 + break; 646 + case 3: 647 + s = "3x "; 648 + break; 649 + } 650 + default: 651 + s = ""; 652 + } s; }), 653 + DWC3_TRB_SIZE_LENGTH(__entry->size), __entry->ctrl, 654 + __entry->ctrl & DWC3_TRB_CTRL_HWO ? 'H' : 'h', 655 + __entry->ctrl & DWC3_TRB_CTRL_LST ? 'L' : 'l', 656 + __entry->ctrl & DWC3_TRB_CTRL_CHN ? 'C' : 'c', 657 + __entry->ctrl & DWC3_TRB_CTRL_CSP ? 'S' : 's', 658 + __entry->ctrl & DWC3_TRB_CTRL_ISP_IMI ? 'S' : 's', 659 + __entry->ctrl & DWC3_TRB_CTRL_IOC ? 'C' : 'c', 660 + dwc3_trb_type_string(DWC3_TRBCTL_TYPE(__entry->ctrl)) 661 + ) 662 + 663 + Lifetime of an Endpoint 664 + ``````````````````````` 665 + 666 + And endpoint's lifetime is summarized with enable and disable 667 + operations, both of which can be traced. Format is:: 668 + 669 + TP_printk("%s: mps %d/%d streams %d burst %d ring %d/%d flags %c:%c%c%c%c%c:%c:%c", 670 + __get_str(name), __entry->maxpacket, 671 + __entry->maxpacket_limit, __entry->max_streams, 672 + __entry->maxburst, __entry->trb_enqueue, 673 + __entry->trb_dequeue, 674 + __entry->flags & DWC3_EP_ENABLED ? 'E' : 'e', 675 + __entry->flags & DWC3_EP_STALL ? 'S' : 's', 676 + __entry->flags & DWC3_EP_WEDGE ? 'W' : 'w', 677 + __entry->flags & DWC3_EP_BUSY ? 'B' : 'b', 678 + __entry->flags & DWC3_EP_PENDING_REQUEST ? 'P' : 'p', 679 + __entry->flags & DWC3_EP_MISSED_ISOC ? 'M' : 'm', 680 + __entry->flags & DWC3_EP_END_TRANSFER_PENDING ? 'E' : 'e', 681 + __entry->direction ? '<' : '>' 682 + ) 683 + 684 + 685 + Structures, Methods and Definitions 686 + ==================================== 687 + 688 + .. kernel-doc:: drivers/usb/dwc3/core.h 689 + :doc: main data structures 690 + :internal: 691 + 692 + .. kernel-doc:: drivers/usb/dwc3/gadget.h 693 + :doc: gadget-only helpers 694 + :internal: 695 + 696 + .. kernel-doc:: drivers/usb/dwc3/gadget.c 697 + :doc: gadget-side implementation 698 + :internal: 699 + 700 + .. kernel-doc:: drivers/usb/dwc3/core.c 701 + :doc: core driver (probe, PM, etc) 702 + :internal: 703 + 704 + .. [#trb] Transfer Request Block 705 + .. [#link_trb] Transfer Request Block pointing to another Transfer 706 + Request Block. 707 + .. [#debugfs] The Debug File System 708 + .. [#configfs] The Config File System 709 + .. [#cbw] Command Block Wrapper 710 + .. _Linus' tree: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/ 711 + .. _me: felipe.balbi@linux.intel.com 712 + .. _linux-usb: linux-usb@vger.kernel.org
+3
Documentation/driver-api/usb/index.rst
··· 16 16 persist 17 17 error-codes 18 18 writing_usb_driver 19 + dwc3 19 20 writing_musb_glue_layer 21 + typec 22 + usb3-debug-port 20 23 21 24 .. only:: subproject and html 22 25
+49 -4
Documentation/usb/gadget-testing.txt
··· 16 16 13. RNDIS function 17 17 14. SERIAL function 18 18 15. SOURCESINK function 19 - 16. UAC1 function 19 + 16. UAC1 function (legacy implementation) 20 20 17. UAC2 function 21 21 18. UVC function 22 22 19. PRINTER function 23 + 20. UAC1 function (new API) 23 24 24 25 25 26 1. ACM function ··· 590 589 host: test-usb (tools/usb/testusb.c) 591 590 592 591 593 - 16. UAC1 function 592 + 16. UAC1 function (legacy implementation) 594 593 ================= 595 594 596 - The function is provided by usb_f_uac1.ko module. 595 + The function is provided by usb_f_uac1_legacy.ko module. 597 596 598 597 Function-specific configfs interface 599 598 ------------------------------------ 600 599 601 - The function name to use when creating the function directory is "uac1". 600 + The function name to use when creating the function directory 601 + is "uac1_legacy". 602 602 The uac1 function provides these attributes in its function directory: 603 603 604 604 audio_buf_size - audio buffer size ··· 774 772 775 773 More advanced testing can be done with the prn_example 776 774 described in Documentation/usb/gadget-printer.txt. 775 + 776 + 777 + 20. UAC1 function (virtual ALSA card, using u_audio API) 778 + ================= 779 + 780 + The function is provided by usb_f_uac1.ko module. 781 + It will create a virtual ALSA card and the audio streams are simply 782 + sinked to and sourced from it. 783 + 784 + Function-specific configfs interface 785 + ------------------------------------ 786 + 787 + The function name to use when creating the function directory is "uac1". 788 + The uac1 function provides these attributes in its function directory: 789 + 790 + c_chmask - capture channel mask 791 + c_srate - capture sampling rate 792 + c_ssize - capture sample size (bytes) 793 + p_chmask - playback channel mask 794 + p_srate - playback sampling rate 795 + p_ssize - playback sample size (bytes) 796 + req_number - the number of pre-allocated request for both capture 797 + and playback 798 + 799 + The attributes have sane default values. 800 + 801 + Testing the UAC1 function 802 + ------------------------- 803 + 804 + device: run the gadget 805 + host: aplay -l # should list our USB Audio Gadget 806 + 807 + This function does not require real hardware support, it just 808 + sends a stream of audio data to/from the host. In order to 809 + actually hear something at the device side, a command similar 810 + to this must be used at the device side: 811 + 812 + $ arecord -f dat -t wav -D hw:2,0 | aplay -D hw:0,0 & 813 + 814 + e.g.: 815 + 816 + $ arecord -f dat -t wav -D hw:CARD=UAC1Gadget,DEV=0 | \ 817 + aplay -D default:CARD=OdroidU3
Documentation/usb/typec.rst Documentation/driver-api/usb/typec.rst
Documentation/usb/usb3-debug-port.rst Documentation/driver-api/usb/usb3-debug-port.rst
+46 -11
drivers/usb/dwc3/core.c
··· 151 151 switch (dwc->desired_dr_role) { 152 152 case DWC3_GCTL_PRTCAP_HOST: 153 153 ret = dwc3_host_init(dwc); 154 - if (ret) 154 + if (ret) { 155 155 dev_err(dwc->dev, "failed to initialize host\n"); 156 + } else { 157 + if (dwc->usb2_phy) 158 + otg_set_vbus(dwc->usb2_phy->otg, true); 159 + if (dwc->usb2_generic_phy) 160 + phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_HOST); 161 + 162 + } 156 163 break; 157 164 case DWC3_GCTL_PRTCAP_DEVICE: 158 165 dwc3_event_buffers_setup(dwc); 166 + 167 + if (dwc->usb2_phy) 168 + otg_set_vbus(dwc->usb2_phy->otg, false); 169 + if (dwc->usb2_generic_phy) 170 + phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_DEVICE); 171 + 159 172 ret = dwc3_gadget_init(dwc); 160 173 if (ret) 161 174 dev_err(dwc->dev, "failed to initialize peripheral\n"); ··· 734 721 dwc3_writel(dwc->regs, DWC3_GCTL, reg); 735 722 } 736 723 724 + static int dwc3_core_get_phy(struct dwc3 *dwc); 725 + 737 726 /** 738 727 * dwc3_core_init - Low-level initialization of DWC3 Core 739 728 * @dwc: Pointer to our controller context structure ··· 771 756 goto err0; 772 757 773 758 ret = dwc3_phy_setup(dwc); 759 + if (ret) 760 + goto err0; 761 + 762 + ret = dwc3_core_get_phy(dwc); 774 763 if (ret) 775 764 goto err0; 776 765 ··· 815 796 dwc3_writel(dwc->regs, DWC3_GUCTL2, reg); 816 797 } 817 798 818 - /* 819 - * Enable hardware control of sending remote wakeup in HS when 820 - * the device is in the L1 state. 821 - */ 822 - if (dwc->revision >= DWC3_REVISION_290A) { 799 + if (dwc->revision >= DWC3_REVISION_250A) { 823 800 reg = dwc3_readl(dwc->regs, DWC3_GUCTL1); 824 - reg |= DWC3_GUCTL1_DEV_L1_EXIT_BY_HW; 801 + 802 + /* 803 + * Enable hardware control of sending remote wakeup 804 + * in HS when the device is in the L1 state. 805 + */ 806 + if (dwc->revision >= DWC3_REVISION_290A) 807 + reg |= DWC3_GUCTL1_DEV_L1_EXIT_BY_HW; 808 + 809 + if (dwc->dis_tx_ipgap_linecheck_quirk) 810 + reg |= DWC3_GUCTL1_TX_IPGAP_LINECHECK_DIS; 811 + 825 812 dwc3_writel(dwc->regs, DWC3_GUCTL1, reg); 826 813 } 827 814 ··· 928 903 switch (dwc->dr_mode) { 929 904 case USB_DR_MODE_PERIPHERAL: 930 905 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_DEVICE); 906 + 907 + if (dwc->usb2_phy) 908 + otg_set_vbus(dwc->usb2_phy->otg, false); 909 + if (dwc->usb2_generic_phy) 910 + phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_DEVICE); 911 + 931 912 ret = dwc3_gadget_init(dwc); 932 913 if (ret) { 933 914 if (ret != -EPROBE_DEFER) ··· 943 912 break; 944 913 case USB_DR_MODE_HOST: 945 914 dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_HOST); 915 + 916 + if (dwc->usb2_phy) 917 + otg_set_vbus(dwc->usb2_phy->otg, true); 918 + if (dwc->usb2_generic_phy) 919 + phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_HOST); 920 + 946 921 ret = dwc3_host_init(dwc); 947 922 if (ret) { 948 923 if (ret != -EPROBE_DEFER) ··· 1060 1023 "snps,dis-u2-freeclk-exists-quirk"); 1061 1024 dwc->dis_del_phy_power_chg_quirk = device_property_read_bool(dev, 1062 1025 "snps,dis-del-phy-power-chg-quirk"); 1026 + dwc->dis_tx_ipgap_linecheck_quirk = device_property_read_bool(dev, 1027 + "snps,dis-tx-ipgap-linecheck-quirk"); 1063 1028 1064 1029 dwc->tx_de_emphasis_quirk = device_property_read_bool(dev, 1065 1030 "snps,tx_de_emphasis_quirk"); ··· 1186 1147 1187 1148 platform_set_drvdata(pdev, dwc); 1188 1149 dwc3_cache_hwparams(dwc); 1189 - 1190 - ret = dwc3_core_get_phy(dwc); 1191 - if (ret) 1192 - goto err0; 1193 1150 1194 1151 spin_lock_init(&dwc->lock); 1195 1152
+28 -20
drivers/usb/dwc3/core.h
··· 1 - /** 1 + /* 2 2 * core.h - DesignWare USB3 DRD Core Header 3 3 * 4 4 * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com ··· 204 204 #define DWC3_GCTL_DSBLCLKGTNG BIT(0) 205 205 206 206 /* Global User Control 1 Register */ 207 + #define DWC3_GUCTL1_TX_IPGAP_LINECHECK_DIS BIT(28) 207 208 #define DWC3_GUCTL1_DEV_L1_EXIT_BY_HW BIT(24) 208 209 209 210 /* Global USB2 PHY Configuration Register */ ··· 523 522 * @trb_pool_dma: dma address of @trb_pool 524 523 * @trb_enqueue: enqueue 'pointer' into TRB array 525 524 * @trb_dequeue: dequeue 'pointer' into TRB array 526 - * @desc: usb_endpoint_descriptor pointer 527 525 * @dwc: pointer to DWC controller 528 526 * @saved_state: ep state saved during hibernation 529 527 * @flags: endpoint flags (wedged, stalled, ...) ··· 664 664 * @bpl: DW0-3 665 665 * @bph: DW4-7 666 666 * @size: DW8-B 667 - * @trl: DWC-F 667 + * @ctrl: DWC-F 668 668 */ 669 669 struct dwc3_trb { 670 670 u32 bpl; ··· 674 674 } __packed; 675 675 676 676 /** 677 - * dwc3_hwparams - copy of HWPARAMS registers 678 - * @hwparams0 - GHWPARAMS0 679 - * @hwparams1 - GHWPARAMS1 680 - * @hwparams2 - GHWPARAMS2 681 - * @hwparams3 - GHWPARAMS3 682 - * @hwparams4 - GHWPARAMS4 683 - * @hwparams5 - GHWPARAMS5 684 - * @hwparams6 - GHWPARAMS6 685 - * @hwparams7 - GHWPARAMS7 686 - * @hwparams8 - GHWPARAMS8 677 + * struct dwc3_hwparams - copy of HWPARAMS registers 678 + * @hwparams0: GHWPARAMS0 679 + * @hwparams1: GHWPARAMS1 680 + * @hwparams2: GHWPARAMS2 681 + * @hwparams3: GHWPARAMS3 682 + * @hwparams4: GHWPARAMS4 683 + * @hwparams5: GHWPARAMS5 684 + * @hwparams6: GHWPARAMS6 685 + * @hwparams7: GHWPARAMS7 686 + * @hwparams8: GHWPARAMS8 687 687 */ 688 688 struct dwc3_hwparams { 689 689 u32 hwparams0; ··· 730 730 * @unaligned: true for OUT endpoints with length not divisible by maxp 731 731 * @direction: IN or OUT direction flag 732 732 * @mapped: true when request has been dma-mapped 733 - * @queued: true when request has been queued to HW 733 + * @started: request is started 734 + * @zero: wants a ZLP 734 735 */ 735 736 struct dwc3_request { 736 737 struct usb_request request; ··· 762 761 763 762 /** 764 763 * struct dwc3 - representation of our controller 765 - * @drd_work - workqueue used for role swapping 764 + * @drd_work: workqueue used for role swapping 766 765 * @ep0_trb: trb which is used for the ctrl_req 766 + * @bounce: address of bounce buffer 767 + * @scratchbuf: address of scratch buffer 767 768 * @setup_buf: used while precessing STD USB requests 768 - * @ep0_trb: dma address of ep0_trb 769 + * @ep0_trb_addr: dma address of @ep0_trb 770 + * @bounce_addr: dma address of @bounce 769 771 * @ep0_usb_req: dummy req used while handling STD USB requests 770 772 * @scratch_addr: dma address of scratchbuf 771 773 * @ep0_in_setup: one control transfer is completed and enter setup phase 772 774 * @lock: for synchronizing 773 775 * @dev: pointer to our struct device 776 + * @sysdev: pointer to the DMA-capable device 774 777 * @xhci: pointer to our xHCI child 775 - * @event_buffer_list: a list of event buffers 778 + * @xhci_resources: struct resources for our @xhci child 779 + * @ev_buf: struct dwc3_event_buffer pointer 780 + * @eps: endpoint array 776 781 * @gadget: device side representation of the peripheral controller 777 782 * @gadget_driver: pointer to the gadget driver 778 783 * @regs: base address for our registers ··· 802 795 * @usb2_generic_phy: pointer to USB2 PHY 803 796 * @usb3_generic_phy: pointer to USB3 PHY 804 797 * @ulpi: pointer to ulpi interface 805 - * @dcfg: saved contents of DCFG register 806 - * @gctl: saved contents of GCTL register 807 798 * @isoch_delay: wValue from Set Isochronous Delay request; 808 799 * @u2sel: parameter from Set SEL request. 809 800 * @u2pel: parameter from Set SEL request. ··· 835 830 * @pending_events: true when we have pending IRQs to be handled 836 831 * @pullups_connected: true when Run/Stop bit is set 837 832 * @setup_packet_pending: true when there's a Setup Packet in FIFO. Workaround 838 - * @start_config_issued: true when StartConfig command has been issued 839 833 * @three_stage_setup: set if we perform a three phase setup 840 834 * @usb3_lpm_capable: set if hadrware supports Link Power Management 841 835 * @disable_scramble_quirk: set if we enable the disable scramble quirk ··· 849 845 * @dis_u2_susphy_quirk: set if we disable usb2 suspend phy 850 846 * @dis_enblslpm_quirk: set if we clear enblslpm in GUSB2PHYCFG, 851 847 * disabling the suspend signal to the PHY. 848 + * @dis_rxdet_inp3_quirk: set if we disable Rx.Detect in P3 852 849 * @dis_u2_freeclk_exists_quirk : set if we clear u2_freeclk_exists 853 850 * in GUSB2PHYCFG, specify that USB2 PHY doesn't 854 851 * provide a free-running PHY clock. 855 852 * @dis_del_phy_power_chg_quirk: set if we disable delay phy power 856 853 * change quirk. 854 + * @dis_tx_ipgap_linecheck_quirk: set if we disable u2mac linestate 855 + * check during HS transmit. 857 856 * @tx_de_emphasis_quirk: set if we enable Tx de-emphasis quirk 858 857 * @tx_de_emphasis: Tx de-emphasis value 859 858 * 0 - -6dB de-emphasis ··· 1011 1004 unsigned dis_rxdet_inp3_quirk:1; 1012 1005 unsigned dis_u2_freeclk_exists_quirk:1; 1013 1006 unsigned dis_del_phy_power_chg_quirk:1; 1007 + unsigned dis_tx_ipgap_linecheck_quirk:1; 1014 1008 1015 1009 unsigned tx_de_emphasis_quirk:1; 1016 1010 unsigned tx_de_emphasis:2;
+240 -7
drivers/usb/dwc3/debug.h
··· 173 173 * @event: the event code 174 174 */ 175 175 static inline const char * 176 - dwc3_gadget_event_string(const struct dwc3_event_devt *event) 176 + dwc3_gadget_event_string(char *str, const struct dwc3_event_devt *event) 177 177 { 178 - static char str[256]; 179 178 enum dwc3_link_state state = event->event_info & DWC3_LINK_STATE_MASK; 180 179 181 180 switch (event->type) { ··· 222 223 return str; 223 224 } 224 225 226 + static inline void dwc3_decode_get_status(__u8 t, __u16 i, __u16 l, char *str) 227 + { 228 + switch (t & USB_RECIP_MASK) { 229 + case USB_RECIP_INTERFACE: 230 + sprintf(str, "Get Interface Status(Intf = %d, Length = %d)", 231 + i, l); 232 + break; 233 + case USB_RECIP_ENDPOINT: 234 + sprintf(str, "Get Endpoint Status(ep%d%s)", 235 + i & ~USB_DIR_IN, 236 + i & USB_DIR_IN ? "in" : "out"); 237 + break; 238 + } 239 + } 240 + 241 + static inline void dwc3_decode_set_clear_feature(__u8 t, __u8 b, __u16 v, 242 + __u16 i, char *str) 243 + { 244 + switch (t & USB_RECIP_MASK) { 245 + case USB_RECIP_DEVICE: 246 + sprintf(str, "%s Device Feature(%s%s)", 247 + b == USB_REQ_CLEAR_FEATURE ? "Clear" : "Set", 248 + ({char *s; 249 + switch (v) { 250 + case USB_DEVICE_SELF_POWERED: 251 + s = "Self Powered"; 252 + break; 253 + case USB_DEVICE_REMOTE_WAKEUP: 254 + s = "Remote Wakeup"; 255 + break; 256 + case USB_DEVICE_TEST_MODE: 257 + s = "Test Mode"; 258 + break; 259 + default: 260 + s = "UNKNOWN"; 261 + } s; }), 262 + v == USB_DEVICE_TEST_MODE ? 263 + ({ char *s; 264 + switch (i) { 265 + case TEST_J: 266 + s = ": TEST_J"; 267 + break; 268 + case TEST_K: 269 + s = ": TEST_K"; 270 + break; 271 + case TEST_SE0_NAK: 272 + s = ": TEST_SE0_NAK"; 273 + break; 274 + case TEST_PACKET: 275 + s = ": TEST_PACKET"; 276 + break; 277 + case TEST_FORCE_EN: 278 + s = ": TEST_FORCE_EN"; 279 + break; 280 + default: 281 + s = ": UNKNOWN"; 282 + } s; }) : ""); 283 + break; 284 + case USB_RECIP_INTERFACE: 285 + sprintf(str, "%s Interface Feature(%s)", 286 + b == USB_REQ_CLEAR_FEATURE ? "Clear" : "Set", 287 + v == USB_INTRF_FUNC_SUSPEND ? 288 + "Function Suspend" : "UNKNOWN"); 289 + break; 290 + case USB_RECIP_ENDPOINT: 291 + sprintf(str, "%s Endpoint Feature(%s ep%d%s)", 292 + b == USB_REQ_CLEAR_FEATURE ? "Clear" : "Set", 293 + v == USB_ENDPOINT_HALT ? "Halt" : "UNKNOWN", 294 + i & ~USB_DIR_IN, 295 + i & USB_DIR_IN ? "in" : "out"); 296 + break; 297 + } 298 + } 299 + 300 + static inline void dwc3_decode_set_address(__u16 v, char *str) 301 + { 302 + sprintf(str, "Set Address(Addr = %02x)", v); 303 + } 304 + 305 + static inline void dwc3_decode_get_set_descriptor(__u8 t, __u8 b, __u16 v, 306 + __u16 i, __u16 l, char *str) 307 + { 308 + sprintf(str, "%s %s Descriptor(Index = %d, Length = %d)", 309 + b == USB_REQ_GET_DESCRIPTOR ? "Get" : "Set", 310 + ({ char *s; 311 + switch (v >> 8) { 312 + case USB_DT_DEVICE: 313 + s = "Device"; 314 + break; 315 + case USB_DT_CONFIG: 316 + s = "Configuration"; 317 + break; 318 + case USB_DT_STRING: 319 + s = "String"; 320 + break; 321 + case USB_DT_INTERFACE: 322 + s = "Interface"; 323 + break; 324 + case USB_DT_ENDPOINT: 325 + s = "Endpoint"; 326 + break; 327 + case USB_DT_DEVICE_QUALIFIER: 328 + s = "Device Qualifier"; 329 + break; 330 + case USB_DT_OTHER_SPEED_CONFIG: 331 + s = "Other Speed Config"; 332 + break; 333 + case USB_DT_INTERFACE_POWER: 334 + s = "Interface Power"; 335 + break; 336 + case USB_DT_OTG: 337 + s = "OTG"; 338 + break; 339 + case USB_DT_DEBUG: 340 + s = "Debug"; 341 + break; 342 + case USB_DT_INTERFACE_ASSOCIATION: 343 + s = "Interface Association"; 344 + break; 345 + case USB_DT_BOS: 346 + s = "BOS"; 347 + break; 348 + case USB_DT_DEVICE_CAPABILITY: 349 + s = "Device Capability"; 350 + break; 351 + case USB_DT_PIPE_USAGE: 352 + s = "Pipe Usage"; 353 + break; 354 + case USB_DT_SS_ENDPOINT_COMP: 355 + s = "SS Endpoint Companion"; 356 + break; 357 + case USB_DT_SSP_ISOC_ENDPOINT_COMP: 358 + s = "SSP Isochronous Endpoint Companion"; 359 + break; 360 + default: 361 + s = "UNKNOWN"; 362 + break; 363 + } s; }), v & 0xff, l); 364 + } 365 + 366 + 367 + static inline void dwc3_decode_get_configuration(__u16 l, char *str) 368 + { 369 + sprintf(str, "Get Configuration(Length = %d)", l); 370 + } 371 + 372 + static inline void dwc3_decode_set_configuration(__u8 v, char *str) 373 + { 374 + sprintf(str, "Set Configuration(Config = %d)", v); 375 + } 376 + 377 + static inline void dwc3_decode_get_intf(__u16 i, __u16 l, char *str) 378 + { 379 + sprintf(str, "Get Interface(Intf = %d, Length = %d)", i, l); 380 + } 381 + 382 + static inline void dwc3_decode_set_intf(__u8 v, __u16 i, char *str) 383 + { 384 + sprintf(str, "Set Interface(Intf = %d, Alt.Setting = %d)", i, v); 385 + } 386 + 387 + static inline void dwc3_decode_synch_frame(__u16 i, __u16 l, char *str) 388 + { 389 + sprintf(str, "Synch Frame(Endpoint = %d, Length = %d)", i, l); 390 + } 391 + 392 + static inline void dwc3_decode_set_sel(__u16 l, char *str) 393 + { 394 + sprintf(str, "Set SEL(Length = %d)", l); 395 + } 396 + 397 + static inline void dwc3_decode_set_isoch_delay(__u8 v, char *str) 398 + { 399 + sprintf(str, "Set Isochronous Delay(Delay = %d ns)", v); 400 + } 401 + 402 + /** 403 + * dwc3_decode_ctrl - returns a string represetion of ctrl request 404 + */ 405 + static inline const char *dwc3_decode_ctrl(char *str, __u8 bRequestType, 406 + __u8 bRequest, __u16 wValue, __u16 wIndex, __u16 wLength) 407 + { 408 + switch (bRequest) { 409 + case USB_REQ_GET_STATUS: 410 + dwc3_decode_get_status(bRequestType, wIndex, wLength, str); 411 + break; 412 + case USB_REQ_CLEAR_FEATURE: 413 + case USB_REQ_SET_FEATURE: 414 + dwc3_decode_set_clear_feature(bRequestType, bRequest, wValue, 415 + wIndex, str); 416 + break; 417 + case USB_REQ_SET_ADDRESS: 418 + dwc3_decode_set_address(wValue, str); 419 + break; 420 + case USB_REQ_GET_DESCRIPTOR: 421 + case USB_REQ_SET_DESCRIPTOR: 422 + dwc3_decode_get_set_descriptor(bRequestType, bRequest, wValue, 423 + wIndex, wLength, str); 424 + break; 425 + case USB_REQ_GET_CONFIGURATION: 426 + dwc3_decode_get_configuration(wLength, str); 427 + break; 428 + case USB_REQ_SET_CONFIGURATION: 429 + dwc3_decode_set_configuration(wValue, str); 430 + break; 431 + case USB_REQ_GET_INTERFACE: 432 + dwc3_decode_get_intf(wIndex, wLength, str); 433 + break; 434 + case USB_REQ_SET_INTERFACE: 435 + dwc3_decode_set_intf(wValue, wIndex, str); 436 + break; 437 + case USB_REQ_SYNCH_FRAME: 438 + dwc3_decode_synch_frame(wIndex, wLength, str); 439 + break; 440 + case USB_REQ_SET_SEL: 441 + dwc3_decode_set_sel(wLength, str); 442 + break; 443 + case USB_REQ_SET_ISOCH_DELAY: 444 + dwc3_decode_set_isoch_delay(wValue, str); 445 + break; 446 + default: 447 + sprintf(str, "%02x %02x %02x %02x %02x %02x %02x %02x", 448 + bRequestType, bRequest, 449 + cpu_to_le16(wValue) & 0xff, 450 + cpu_to_le16(wValue) >> 8, 451 + cpu_to_le16(wIndex) & 0xff, 452 + cpu_to_le16(wIndex) >> 8, 453 + cpu_to_le16(wLength) & 0xff, 454 + cpu_to_le16(wLength) >> 8); 455 + } 456 + 457 + return str; 458 + } 459 + 225 460 /** 226 461 * dwc3_ep_event_string - returns event name 227 462 * @event: then event code 228 463 */ 229 464 static inline const char * 230 - dwc3_ep_event_string(const struct dwc3_event_depevt *event, u32 ep0state) 465 + dwc3_ep_event_string(char *str, const struct dwc3_event_depevt *event, 466 + u32 ep0state) 231 467 { 232 468 u8 epnum = event->endpoint_number; 233 - static char str[256]; 234 469 size_t len; 235 470 int status; 236 471 int ret; ··· 565 332 } 566 333 } 567 334 568 - static inline const char *dwc3_decode_event(u32 event, u32 ep0state) 335 + static inline const char *dwc3_decode_event(char *str, u32 event, u32 ep0state) 569 336 { 570 337 const union dwc3_event evt = (union dwc3_event) event; 571 338 572 339 if (evt.type.is_devspec) 573 - return dwc3_gadget_event_string(&evt.devt); 340 + return dwc3_gadget_event_string(str, &evt.devt); 574 341 else 575 - return dwc3_ep_event_string(&evt.depevt, ep0state); 342 + return dwc3_ep_event_string(str, &evt.depevt, ep0state); 576 343 } 577 344 578 345 static inline const char *dwc3_ep_cmd_status_string(int status)
+4 -5
drivers/usb/dwc3/debugfs.c
··· 653 653 goto out; 654 654 } 655 655 656 - seq_printf(s, "enqueue pointer %d\n", dep->trb_enqueue); 657 - seq_printf(s, "dequeue pointer %d\n", dep->trb_dequeue); 658 - seq_printf(s, "\n--------------------------------------------------\n\n"); 659 656 seq_printf(s, "buffer_addr,size,type,ioc,isp_imi,csp,chn,lst,hwo\n"); 660 657 661 658 for (i = 0; i < DWC3_TRB_NUM; i++) { 662 659 struct dwc3_trb *trb = &dep->trb_pool[i]; 663 660 unsigned int type = DWC3_TRBCTL_TYPE(trb->ctrl); 664 661 665 - seq_printf(s, "%08x%08x,%d,%s,%d,%d,%d,%d,%d,%d\n", 662 + seq_printf(s, "%08x%08x,%d,%s,%d,%d,%d,%d,%d,%d %c%c\n", 666 663 trb->bph, trb->bpl, trb->size, 667 664 dwc3_trb_type_string(type), 668 665 !!(trb->ctrl & DWC3_TRB_CTRL_IOC), ··· 667 670 !!(trb->ctrl & DWC3_TRB_CTRL_CSP), 668 671 !!(trb->ctrl & DWC3_TRB_CTRL_CHN), 669 672 !!(trb->ctrl & DWC3_TRB_CTRL_LST), 670 - !!(trb->ctrl & DWC3_TRB_CTRL_HWO)); 673 + !!(trb->ctrl & DWC3_TRB_CTRL_HWO), 674 + dep->trb_enqueue == i ? 'E' : ' ', 675 + dep->trb_dequeue == i ? 'D' : ' '); 671 676 } 672 677 673 678 out:
+10 -3
drivers/usb/dwc3/dwc3-exynos.c
··· 125 125 dev_err(dev, "couldn't get clock\n"); 126 126 return -EINVAL; 127 127 } 128 - clk_prepare_enable(exynos->clk); 128 + ret = clk_prepare_enable(exynos->clk); 129 + if (ret) 130 + return ret; 129 131 130 132 exynos->susp_clk = devm_clk_get(dev, "usbdrd30_susp_clk"); 131 133 if (IS_ERR(exynos->susp_clk)) 132 134 exynos->susp_clk = NULL; 133 - clk_prepare_enable(exynos->susp_clk); 135 + ret = clk_prepare_enable(exynos->susp_clk); 136 + if (ret) 137 + goto susp_clk_err; 134 138 135 139 if (of_device_is_compatible(node, "samsung,exynos7-dwusb3")) { 136 140 exynos->axius_clk = devm_clk_get(dev, "usbdrd30_axius_clk"); ··· 143 139 ret = -ENODEV; 144 140 goto axius_clk_err; 145 141 } 146 - clk_prepare_enable(exynos->axius_clk); 142 + ret = clk_prepare_enable(exynos->axius_clk); 143 + if (ret) 144 + goto axius_clk_err; 147 145 } else { 148 146 exynos->axius_clk = NULL; 149 147 } ··· 203 197 clk_disable_unprepare(exynos->axius_clk); 204 198 axius_clk_err: 205 199 clk_disable_unprepare(exynos->susp_clk); 200 + susp_clk_err: 206 201 clk_disable_unprepare(exynos->clk); 207 202 return ret; 208 203 }
+1 -1
drivers/usb/dwc3/dwc3-st.c
··· 230 230 231 231 dwc3_data->syscfg_reg_off = res->start; 232 232 233 - dev_vdbg(&pdev->dev, "glue-logic addr 0x%p, syscfg-reg offset 0x%x\n", 233 + dev_vdbg(&pdev->dev, "glue-logic addr 0x%pK, syscfg-reg offset 0x%x\n", 234 234 dwc3_data->glue_base, dwc3_data->syscfg_reg_off); 235 235 236 236 dwc3_data->rstc_pwrdn =
+7 -1
drivers/usb/dwc3/ep0.c
··· 1 - /** 1 + /* 2 2 * ep0.c - DesignWare USB3 DRD Controller Endpoint 0 Handling 3 3 * 4 4 * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com ··· 319 319 { 320 320 struct dwc3_ep *dep; 321 321 u32 recip; 322 + u32 value; 322 323 u32 reg; 323 324 u16 usb_status = 0; 324 325 __le16 *response_pkt; 326 + 327 + /* We don't support PTM_STATUS */ 328 + value = le16_to_cpu(ctrl->wValue); 329 + if (value != 0) 330 + return -EINVAL; 325 331 326 332 recip = ctrl->bRequestType & USB_RECIP_MASK; 327 333 switch (recip) {
+173 -126
drivers/usb/dwc3/gadget.c
··· 1 - /** 1 + /* 2 2 * gadget.c - DesignWare USB3 DRD Controller Gadget Framework Link 3 3 * 4 4 * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com ··· 36 36 #include "io.h" 37 37 38 38 /** 39 - * dwc3_gadget_set_test_mode - Enables USB2 Test Modes 39 + * dwc3_gadget_set_test_mode - enables usb2 test modes 40 40 * @dwc: pointer to our context structure 41 41 * @mode: the mode to set (J, K SE0 NAK, Force Enable) 42 42 * 43 - * Caller should take care of locking. This function will 44 - * return 0 on success or -EINVAL if wrong Test Selector 45 - * is passed 43 + * Caller should take care of locking. This function will return 0 on 44 + * success or -EINVAL if wrong Test Selector is passed. 46 45 */ 47 46 int dwc3_gadget_set_test_mode(struct dwc3 *dwc, int mode) 48 47 { ··· 68 69 } 69 70 70 71 /** 71 - * dwc3_gadget_get_link_state - Gets current state of USB Link 72 + * dwc3_gadget_get_link_state - gets current state of usb link 72 73 * @dwc: pointer to our context structure 73 74 * 74 75 * Caller should take care of locking. This function will ··· 84 85 } 85 86 86 87 /** 87 - * dwc3_gadget_set_link_state - Sets USB Link to a particular State 88 + * dwc3_gadget_set_link_state - sets usb link to a particular state 88 89 * @dwc: pointer to our context structure 89 90 * @state: the state to put link into 90 91 * ··· 142 143 } 143 144 144 145 /** 145 - * dwc3_ep_inc_trb() - Increment a TRB index. 146 - * @index - Pointer to the TRB index to increment. 146 + * dwc3_ep_inc_trb - increment a trb index. 147 + * @index: Pointer to the TRB index to increment. 147 148 * 148 149 * The index should never point to the link TRB. After incrementing, 149 150 * if it is point to the link TRB, wrap around to the beginning. The ··· 156 157 *index = 0; 157 158 } 158 159 160 + /** 161 + * dwc3_ep_inc_enq - increment endpoint's enqueue pointer 162 + * @dep: The endpoint whose enqueue pointer we're incrementing 163 + */ 159 164 static void dwc3_ep_inc_enq(struct dwc3_ep *dep) 160 165 { 161 166 dwc3_ep_inc_trb(&dep->trb_enqueue); 162 167 } 163 168 169 + /** 170 + * dwc3_ep_inc_deq - increment endpoint's dequeue pointer 171 + * @dep: The endpoint whose enqueue pointer we're incrementing 172 + */ 164 173 static void dwc3_ep_inc_deq(struct dwc3_ep *dep) 165 174 { 166 175 dwc3_ep_inc_trb(&dep->trb_dequeue); 167 176 } 168 177 178 + /** 179 + * dwc3_gadget_giveback - call struct usb_request's ->complete callback 180 + * @dep: The endpoint to whom the request belongs to 181 + * @req: The request we're giving back 182 + * @status: completion code for the request 183 + * 184 + * Must be called with controller's lock held and interrupts disabled. This 185 + * function will unmap @req and call its ->complete() callback to notify upper 186 + * layers that it has completed. 187 + */ 169 188 void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req, 170 189 int status) 171 190 { ··· 210 193 pm_runtime_put(dwc->dev); 211 194 } 212 195 196 + /** 197 + * dwc3_send_gadget_generic_command - issue a generic command for the controller 198 + * @dwc: pointer to the controller context 199 + * @cmd: the command to be issued 200 + * @param: command parameter 201 + * 202 + * Caller should take care of locking. Issue @cmd with a given @param to @dwc 203 + * and wait for its completion. 204 + */ 213 205 int dwc3_send_gadget_generic_command(struct dwc3 *dwc, unsigned cmd, u32 param) 214 206 { 215 207 u32 timeout = 500; ··· 251 225 252 226 static int __dwc3_gadget_wakeup(struct dwc3 *dwc); 253 227 228 + /** 229 + * dwc3_send_gadget_ep_cmd - issue an endpoint command 230 + * @dep: the endpoint to which the command is going to be issued 231 + * @cmd: the command to be issued 232 + * @params: parameters to the command 233 + * 234 + * Caller should handle locking. This function will issue @cmd with given 235 + * @params to @dep and wait for its completion. 236 + */ 254 237 int dwc3_send_gadget_ep_cmd(struct dwc3_ep *dep, unsigned cmd, 255 238 struct dwc3_gadget_ep_cmd_params *params) 256 239 { ··· 457 422 static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep); 458 423 459 424 /** 460 - * dwc3_gadget_start_config - Configure EP resources 425 + * dwc3_gadget_start_config - configure ep resources 461 426 * @dwc: pointer to our controller context structure 462 427 * @dep: endpoint that is being enabled 463 428 * 464 - * The assignment of transfer resources cannot perfectly follow the 465 - * data book due to the fact that the controller driver does not have 466 - * all knowledge of the configuration in advance. It is given this 467 - * information piecemeal by the composite gadget framework after every 468 - * SET_CONFIGURATION and SET_INTERFACE. Trying to follow the databook 469 - * programming model in this scenario can cause errors. For two 470 - * reasons: 429 + * Issue a %DWC3_DEPCMD_DEPSTARTCFG command to @dep. After the command's 430 + * completion, it will set Transfer Resource for all available endpoints. 471 431 * 472 - * 1) The databook says to do DEPSTARTCFG for every SET_CONFIGURATION 473 - * and SET_INTERFACE (8.1.5). This is incorrect in the scenario of 474 - * multiple interfaces. 432 + * The assignment of transfer resources cannot perfectly follow the data book 433 + * due to the fact that the controller driver does not have all knowledge of the 434 + * configuration in advance. It is given this information piecemeal by the 435 + * composite gadget framework after every SET_CONFIGURATION and 436 + * SET_INTERFACE. Trying to follow the databook programming model in this 437 + * scenario can cause errors. For two reasons: 475 438 * 476 - * 2) The databook does not mention doing more DEPXFERCFG for new 439 + * 1) The databook says to do %DWC3_DEPCMD_DEPSTARTCFG for every 440 + * %USB_REQ_SET_CONFIGURATION and %USB_REQ_SET_INTERFACE (8.1.5). This is 441 + * incorrect in the scenario of multiple interfaces. 442 + * 443 + * 2) The databook does not mention doing more %DWC3_DEPCMD_DEPXFERCFG for new 477 444 * endpoint on alt setting (8.1.6). 478 445 * 479 446 * The following simplified method is used instead: 480 447 * 481 - * All hardware endpoints can be assigned a transfer resource and this 482 - * setting will stay persistent until either a core reset or 483 - * hibernation. So whenever we do a DEPSTARTCFG(0) we can go ahead and 484 - * do DEPXFERCFG for every hardware endpoint as well. We are 448 + * All hardware endpoints can be assigned a transfer resource and this setting 449 + * will stay persistent until either a core reset or hibernation. So whenever we 450 + * do a %DWC3_DEPCMD_DEPSTARTCFG(0) we can go ahead and do 451 + * %DWC3_DEPCMD_DEPXFERCFG for every hardware endpoint as well. We are 485 452 * guaranteed that there are as many transfer resources as endpoints. 486 453 * 487 - * This function is called for each endpoint when it is being enabled 488 - * but is triggered only when called for EP0-out, which always happens 489 - * first, and which should only happen in one of the above conditions. 454 + * This function is called for each endpoint when it is being enabled but is 455 + * triggered only when called for EP0-out, which always happens first, and which 456 + * should only happen in one of the above conditions. 490 457 */ 491 458 static int dwc3_gadget_start_config(struct dwc3 *dwc, struct dwc3_ep *dep) 492 459 { ··· 606 569 } 607 570 608 571 /** 609 - * __dwc3_gadget_ep_enable - Initializes a HW endpoint 572 + * __dwc3_gadget_ep_enable - initializes a hw endpoint 610 573 * @dep: endpoint to be initialized 611 - * @desc: USB Endpoint Descriptor 574 + * @modify: if true, modify existing endpoint configuration 575 + * @restore: if true, restore endpoint configuration from scratch buffer 612 576 * 613 - * Caller should take care of locking 577 + * Caller should take care of locking. Execute all necessary commands to 578 + * initialize a HW endpoint so it can be used by a gadget driver. 614 579 */ 615 580 static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep, 616 581 bool modify, bool restore) ··· 724 685 } 725 686 726 687 /** 727 - * __dwc3_gadget_ep_disable - Disables a HW endpoint 688 + * __dwc3_gadget_ep_disable - disables a hw endpoint 728 689 * @dep: the endpoint to disable 729 690 * 730 - * This function also removes requests which are currently processed ny the 731 - * hardware and those which are not yet scheduled. 691 + * This function undoes what __dwc3_gadget_ep_enable did and also removes 692 + * requests which are currently being processed by the hardware and those which 693 + * are not yet scheduled. 694 + * 732 695 * Caller should take care of locking. 733 696 */ 734 697 static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep) ··· 973 932 } 974 933 975 934 /** 976 - * dwc3_ep_prev_trb() - Returns the previous TRB in the ring 935 + * dwc3_ep_prev_trb - returns the previous TRB in the ring 977 936 * @dep: The endpoint with the TRB ring 978 937 * @index: The index of the current TRB in the ring 979 938 * ··· 994 953 static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep) 995 954 { 996 955 struct dwc3_trb *tmp; 997 - struct dwc3 *dwc = dep->dwc; 998 956 u8 trbs_left; 999 957 1000 958 /* ··· 1005 965 */ 1006 966 if (dep->trb_enqueue == dep->trb_dequeue) { 1007 967 tmp = dwc3_ep_prev_trb(dep, dep->trb_enqueue); 1008 - if (dev_WARN_ONCE(dwc->dev, tmp->ctrl & DWC3_TRB_CTRL_HWO, 1009 - "%s No TRBS left\n", dep->name)) 968 + if (tmp->ctrl & DWC3_TRB_CTRL_HWO) 1010 969 return 0; 1011 970 1012 971 return DWC3_TRB_NUM - 1; ··· 1140 1101 } 1141 1102 1142 1103 list_for_each_entry_safe(req, n, &dep->pending_list, list) { 1104 + struct dwc3 *dwc = dep->dwc; 1105 + int ret; 1106 + 1107 + ret = usb_gadget_map_request_by_dev(dwc->sysdev, &req->request, 1108 + dep->direction); 1109 + if (ret) 1110 + return; 1111 + 1112 + req->sg = req->request.sg; 1113 + req->num_pending_sgs = req->request.num_mapped_sgs; 1114 + 1143 1115 if (req->num_pending_sgs > 0) 1144 1116 dwc3_prepare_one_trb_sg(dep, req); 1145 1117 else ··· 1257 1207 static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req) 1258 1208 { 1259 1209 struct dwc3 *dwc = dep->dwc; 1260 - int ret; 1210 + int ret = 0; 1261 1211 1262 1212 if (!dep->endpoint.desc) { 1263 1213 dev_err(dwc->dev, "%s: can't queue to disabled endpoint\n", ··· 1265 1215 return -ESHUTDOWN; 1266 1216 } 1267 1217 1268 - if (WARN(req->dep != dep, "request %p belongs to '%s'\n", 1269 - &req->request, req->dep->name)) { 1270 - dev_err(dwc->dev, "%s: request %p belongs to '%s'\n", 1271 - dep->name, &req->request, req->dep->name); 1218 + if (WARN(req->dep != dep, "request %pK belongs to '%s'\n", 1219 + &req->request, req->dep->name)) 1272 1220 return -EINVAL; 1273 - } 1274 1221 1275 1222 pm_runtime_get(dwc->dev); 1276 1223 ··· 1277 1230 req->epnum = dep->number; 1278 1231 1279 1232 trace_dwc3_ep_queue(req); 1280 - 1281 - ret = usb_gadget_map_request_by_dev(dwc->sysdev, &req->request, 1282 - dep->direction); 1283 - if (ret) 1284 - return ret; 1285 - 1286 - req->sg = req->request.sg; 1287 - req->num_pending_sgs = req->request.num_mapped_sgs; 1288 1233 1289 1234 list_add_tail(&req->list, &dep->pending_list); 1290 1235 ··· 1435 1396 } 1436 1397 goto out1; 1437 1398 } 1438 - dev_err(dwc->dev, "request %p was not queued to %s\n", 1399 + dev_err(dwc->dev, "request %pK was not queued to %s\n", 1439 1400 request, ep->name); 1440 1401 ret = -EINVAL; 1441 1402 goto out0; ··· 1780 1741 static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc); 1781 1742 1782 1743 /** 1783 - * dwc3_gadget_setup_nump - Calculate and initialize NUMP field of DCFG 1784 - * dwc: pointer to our context structure 1744 + * dwc3_gadget_setup_nump - calculate and initialize NUMP field of %DWC3_DCFG 1745 + * @dwc: pointer to our context structure 1785 1746 * 1786 1747 * The following looks like complex but it's actually very simple. In order to 1787 1748 * calculate the number of packets we can burst at once on OUT transfers, we're ··· 1836 1797 } else if (dwc3_has_imod(dwc)) { 1837 1798 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), 0); 1838 1799 } 1839 - 1840 - reg = dwc3_readl(dwc->regs, DWC3_DCFG); 1841 - reg &= ~(DWC3_DCFG_SPEED_MASK); 1842 - 1843 - /** 1844 - * WORKAROUND: DWC3 revision < 2.20a have an issue 1845 - * which would cause metastability state on Run/Stop 1846 - * bit if we try to force the IP to USB2-only mode. 1847 - * 1848 - * Because of that, we cannot configure the IP to any 1849 - * speed other than the SuperSpeed 1850 - * 1851 - * Refers to: 1852 - * 1853 - * STAR#9000525659: Clock Domain Crossing on DCTL in 1854 - * USB 2.0 Mode 1855 - */ 1856 - if (dwc->revision < DWC3_REVISION_220A) { 1857 - reg |= DWC3_DCFG_SUPERSPEED; 1858 - } else { 1859 - switch (dwc->maximum_speed) { 1860 - case USB_SPEED_LOW: 1861 - reg |= DWC3_DCFG_LOWSPEED; 1862 - break; 1863 - case USB_SPEED_FULL: 1864 - reg |= DWC3_DCFG_FULLSPEED; 1865 - break; 1866 - case USB_SPEED_HIGH: 1867 - reg |= DWC3_DCFG_HIGHSPEED; 1868 - break; 1869 - case USB_SPEED_SUPER_PLUS: 1870 - reg |= DWC3_DCFG_SUPERSPEED_PLUS; 1871 - break; 1872 - default: 1873 - dev_err(dwc->dev, "invalid dwc->maximum_speed (%d)\n", 1874 - dwc->maximum_speed); 1875 - /* fall through */ 1876 - case USB_SPEED_SUPER: 1877 - reg |= DWC3_DCFG_SUPERSPEED; 1878 - break; 1879 - } 1880 - } 1881 - dwc3_writel(dwc->regs, DWC3_DCFG, reg); 1882 1800 1883 1801 /* 1884 1802 * We are telling dwc3 that we want to use DCFG.NUMP as ACK TP's NUMP ··· 1968 1972 return 0; 1969 1973 } 1970 1974 1975 + static void dwc3_gadget_set_speed(struct usb_gadget *g, 1976 + enum usb_device_speed speed) 1977 + { 1978 + struct dwc3 *dwc = gadget_to_dwc(g); 1979 + unsigned long flags; 1980 + u32 reg; 1981 + 1982 + spin_lock_irqsave(&dwc->lock, flags); 1983 + reg = dwc3_readl(dwc->regs, DWC3_DCFG); 1984 + reg &= ~(DWC3_DCFG_SPEED_MASK); 1985 + 1986 + /* 1987 + * WORKAROUND: DWC3 revision < 2.20a have an issue 1988 + * which would cause metastability state on Run/Stop 1989 + * bit if we try to force the IP to USB2-only mode. 1990 + * 1991 + * Because of that, we cannot configure the IP to any 1992 + * speed other than the SuperSpeed 1993 + * 1994 + * Refers to: 1995 + * 1996 + * STAR#9000525659: Clock Domain Crossing on DCTL in 1997 + * USB 2.0 Mode 1998 + */ 1999 + if (dwc->revision < DWC3_REVISION_220A) { 2000 + reg |= DWC3_DCFG_SUPERSPEED; 2001 + } else { 2002 + switch (speed) { 2003 + case USB_SPEED_LOW: 2004 + reg |= DWC3_DCFG_LOWSPEED; 2005 + break; 2006 + case USB_SPEED_FULL: 2007 + reg |= DWC3_DCFG_FULLSPEED; 2008 + break; 2009 + case USB_SPEED_HIGH: 2010 + reg |= DWC3_DCFG_HIGHSPEED; 2011 + break; 2012 + case USB_SPEED_SUPER: 2013 + reg |= DWC3_DCFG_SUPERSPEED; 2014 + break; 2015 + case USB_SPEED_SUPER_PLUS: 2016 + reg |= DWC3_DCFG_SUPERSPEED_PLUS; 2017 + break; 2018 + default: 2019 + dev_err(dwc->dev, "invalid speed (%d)\n", speed); 2020 + 2021 + if (dwc->revision & DWC3_REVISION_IS_DWC31) 2022 + reg |= DWC3_DCFG_SUPERSPEED_PLUS; 2023 + else 2024 + reg |= DWC3_DCFG_SUPERSPEED; 2025 + } 2026 + } 2027 + dwc3_writel(dwc->regs, DWC3_DCFG, reg); 2028 + 2029 + spin_unlock_irqrestore(&dwc->lock, flags); 2030 + } 2031 + 1971 2032 static const struct usb_gadget_ops dwc3_gadget_ops = { 1972 2033 .get_frame = dwc3_gadget_get_frame, 1973 2034 .wakeup = dwc3_gadget_wakeup, ··· 2032 1979 .pullup = dwc3_gadget_pullup, 2033 1980 .udc_start = dwc3_gadget_start, 2034 1981 .udc_stop = dwc3_gadget_stop, 1982 + .udc_set_speed = dwc3_gadget_set_speed, 2035 1983 }; 2036 1984 2037 1985 /* -------------------------------------------------------------------------- */ 2038 1986 2039 - static int dwc3_gadget_init_endpoints(struct dwc3 *dwc, u8 num) 1987 + static int dwc3_gadget_init_endpoints(struct dwc3 *dwc, u8 total) 2040 1988 { 2041 1989 struct dwc3_ep *dep; 2042 1990 u8 epnum; 2043 1991 2044 1992 INIT_LIST_HEAD(&dwc->gadget.ep_list); 2045 1993 2046 - for (epnum = 0; epnum < num; epnum++) { 1994 + for (epnum = 0; epnum < total; epnum++) { 2047 1995 bool direction = epnum & 1; 1996 + u8 num = epnum >> 1; 2048 1997 2049 1998 dep = kzalloc(sizeof(*dep), GFP_KERNEL); 2050 1999 if (!dep) ··· 2058 2003 dep->regs = dwc->regs + DWC3_DEP_BASE(epnum); 2059 2004 dwc->eps[epnum] = dep; 2060 2005 2061 - snprintf(dep->name, sizeof(dep->name), "ep%d%s", epnum >> 1, 2006 + snprintf(dep->name, sizeof(dep->name), "ep%u%s", num, 2062 2007 direction ? "in" : "out"); 2063 2008 2064 2009 dep->endpoint.name = dep->name; ··· 2070 2015 2071 2016 spin_lock_init(&dep->lock); 2072 2017 2073 - if (epnum == 0 || epnum == 1) { 2018 + if (num == 0) { 2074 2019 usb_ep_set_maxpacket_limit(&dep->endpoint, 512); 2075 2020 dep->endpoint.maxburst = 1; 2076 2021 dep->endpoint.ops = &dwc3_gadget_ep0_ops; 2077 - if (!epnum) 2022 + if (!direction) 2078 2023 dwc->gadget.ep0 = &dep->endpoint; 2079 2024 } else if (direction) { 2080 2025 int mdwidth; 2026 + int kbytes; 2081 2027 int size; 2082 2028 int ret; 2083 - int num; 2084 2029 2085 2030 mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0); 2086 2031 /* MDWIDTH is represented in bits, we need it in bytes */ 2087 2032 mdwidth /= 8; 2088 2033 2089 - size = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(epnum >> 1)); 2034 + size = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(num)); 2090 2035 size = DWC3_GTXFIFOSIZ_TXFDEF(size); 2091 2036 2092 2037 /* FIFO Depth is in MDWDITH bytes. Multiply */ 2093 2038 size *= mdwidth; 2094 2039 2095 - num = size / 1024; 2096 - if (num == 0) 2097 - num = 1; 2040 + kbytes = size / 1024; 2041 + if (kbytes == 0) 2042 + kbytes = 1; 2098 2043 2099 2044 /* 2100 - * FIFO sizes account an extra MDWIDTH * (num + 1) bytes for 2045 + * FIFO sizes account an extra MDWIDTH * (kbytes + 1) bytes for 2101 2046 * internal overhead. We don't really know how these are used, 2102 2047 * but documentation say it exists. 2103 2048 */ 2104 - size -= mdwidth * (num + 1); 2105 - size /= num; 2049 + size -= mdwidth * (kbytes + 1); 2050 + size /= kbytes; 2106 2051 2107 2052 usb_ep_set_maxpacket_limit(&dep->endpoint, size); 2108 2053 ··· 2128 2073 return ret; 2129 2074 } 2130 2075 2131 - if (epnum == 0 || epnum == 1) { 2076 + if (num == 0) { 2132 2077 dep->endpoint.caps.type_control = true; 2133 2078 } else { 2134 2079 dep->endpoint.caps.type_iso = true; ··· 2926 2871 { 2927 2872 unsigned int is_ss = evtinfo & BIT(4); 2928 2873 2929 - /** 2874 + /* 2930 2875 * WORKAROUND: DWC3 revison 2.20a with hibernation support 2931 2876 * have a known issue which can cause USB CV TD.9.23 to fail 2932 2877 * randomly. ··· 2998 2943 { 2999 2944 trace_dwc3_event(event->raw, dwc); 3000 2945 3001 - /* Endpoint IRQ, handle it and return early */ 3002 - if (event->type.is_devspec == 0) { 3003 - /* depevt */ 3004 - return dwc3_endpoint_interrupt(dwc, &event->depevt); 3005 - } 3006 - 3007 - switch (event->type.type) { 3008 - case DWC3_EVENT_TYPE_DEV: 2946 + if (!event->type.is_devspec) 2947 + dwc3_endpoint_interrupt(dwc, &event->depevt); 2948 + else if (event->type.type == DWC3_EVENT_TYPE_DEV) 3009 2949 dwc3_gadget_interrupt(dwc, &event->devt); 3010 - break; 3011 - /* REVISIT what to do with Carkit and I2C events ? */ 3012 - default: 2950 + else 3013 2951 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw); 3014 - } 3015 2952 } 3016 2953 3017 2954 static irqreturn_t dwc3_process_event_buf(struct dwc3_event_buffer *evt) ··· 3157 3110 } 3158 3111 3159 3112 /** 3160 - * dwc3_gadget_init - Initializes gadget related registers 3113 + * dwc3_gadget_init - initializes gadget related registers 3161 3114 * @dwc: pointer to our controller context structure 3162 3115 * 3163 3116 * Returns 0 on success otherwise negative errno.
+18 -4
drivers/usb/dwc3/gadget.h
··· 1 - /** 1 + /* 2 2 * gadget.h - DesignWare USB3 DRD Gadget Header 3 3 * 4 4 * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com ··· 60 60 61 61 #define to_dwc3_request(r) (container_of(r, struct dwc3_request, request)) 62 62 63 + /** 64 + * next_request - gets the next request on the given list 65 + * @list: the request list to operate on 66 + * 67 + * Caller should take care of locking. This function return %NULL or the first 68 + * request available on @list. 69 + */ 63 70 static inline struct dwc3_request *next_request(struct list_head *list) 64 71 { 65 72 return list_first_entry_or_null(list, struct dwc3_request, list); 66 73 } 67 74 75 + /** 76 + * dwc3_gadget_move_started_request - move @req to the started_list 77 + * @req: the request to be moved 78 + * 79 + * Caller should take care of locking. This function will move @req from its 80 + * current list to the endpoint's started_list. 81 + */ 68 82 static inline void dwc3_gadget_move_started_request(struct dwc3_request *req) 69 83 { 70 84 struct dwc3_ep *dep = req->dep; ··· 101 87 102 88 /** 103 89 * dwc3_gadget_ep_get_transfer_index - Gets transfer index from HW 104 - * @dwc: DesignWare USB3 Pointer 105 - * @number: DWC endpoint number 90 + * @dep: dwc3 endpoint 106 91 * 107 - * Caller should take care of locking 92 + * Caller should take care of locking. Returns the transfer resource 93 + * index for a given endpoint. 108 94 */ 109 95 static inline u32 dwc3_gadget_ep_get_transfer_index(struct dwc3_ep *dep) 110 96 {
+15 -13
drivers/usb/dwc3/trace.h
··· 60 60 TP_STRUCT__entry( 61 61 __field(u32, event) 62 62 __field(u32, ep0state) 63 + __dynamic_array(char, str, DWC3_MSG_MAX) 63 64 ), 64 65 TP_fast_assign( 65 66 __entry->event = event; 66 67 __entry->ep0state = dwc->ep0state; 67 68 ), 68 69 TP_printk("event (%08x): %s", __entry->event, 69 - dwc3_decode_event(__entry->event, __entry->ep0state)) 70 + dwc3_decode_event(__get_str(str), __entry->event, 71 + __entry->ep0state)) 70 72 ); 71 73 72 74 DEFINE_EVENT(dwc3_log_event, dwc3_event, ··· 85 83 __field(__u16, wValue) 86 84 __field(__u16, wIndex) 87 85 __field(__u16, wLength) 86 + __dynamic_array(char, str, DWC3_MSG_MAX) 88 87 ), 89 88 TP_fast_assign( 90 89 __entry->bRequestType = ctrl->bRequestType; ··· 94 91 __entry->wIndex = le16_to_cpu(ctrl->wIndex); 95 92 __entry->wLength = le16_to_cpu(ctrl->wLength); 96 93 ), 97 - TP_printk("bRequestType %02x bRequest %02x wValue %04x wIndex %04x wLength %d", 98 - __entry->bRequestType, __entry->bRequest, 99 - __entry->wValue, __entry->wIndex, 100 - __entry->wLength 94 + TP_printk("%s", dwc3_decode_ctrl(__get_str(str), __entry->bRequestType, 95 + __entry->bRequest, __entry->wValue, 96 + __entry->wIndex, __entry->wLength) 101 97 ) 102 98 ); 103 99 ··· 109 107 TP_PROTO(struct dwc3_request *req), 110 108 TP_ARGS(req), 111 109 TP_STRUCT__entry( 112 - __dynamic_array(char, name, DWC3_MSG_MAX) 110 + __string(name, req->dep->name) 113 111 __field(struct dwc3_request *, req) 114 112 __field(unsigned, actual) 115 113 __field(unsigned, length) ··· 119 117 __field(int, no_interrupt) 120 118 ), 121 119 TP_fast_assign( 122 - snprintf(__get_str(name), DWC3_MSG_MAX, "%s", req->dep->name); 120 + __assign_str(name, req->dep->name); 123 121 __entry->req = req; 124 122 __entry->actual = req->request.actual; 125 123 __entry->length = req->request.length; ··· 192 190 struct dwc3_gadget_ep_cmd_params *params, int cmd_status), 193 191 TP_ARGS(dep, cmd, params, cmd_status), 194 192 TP_STRUCT__entry( 195 - __dynamic_array(char, name, DWC3_MSG_MAX) 193 + __string(name, dep->name) 196 194 __field(unsigned int, cmd) 197 195 __field(u32, param0) 198 196 __field(u32, param1) ··· 200 198 __field(int, cmd_status) 201 199 ), 202 200 TP_fast_assign( 203 - snprintf(__get_str(name), DWC3_MSG_MAX, "%s", dep->name); 201 + __assign_str(name, dep->name); 204 202 __entry->cmd = cmd; 205 203 __entry->param0 = params->param0; 206 204 __entry->param1 = params->param1; ··· 225 223 TP_PROTO(struct dwc3_ep *dep, struct dwc3_trb *trb), 226 224 TP_ARGS(dep, trb), 227 225 TP_STRUCT__entry( 228 - __dynamic_array(char, name, DWC3_MSG_MAX) 226 + __string(name, dep->name) 229 227 __field(struct dwc3_trb *, trb) 230 228 __field(u32, allocated) 231 229 __field(u32, queued) ··· 236 234 __field(u32, type) 237 235 ), 238 236 TP_fast_assign( 239 - snprintf(__get_str(name), DWC3_MSG_MAX, "%s", dep->name); 237 + __assign_str(name, dep->name); 240 238 __entry->trb = trb; 241 239 __entry->allocated = dep->allocated_requests; 242 240 __entry->queued = dep->queued_requests; ··· 293 291 TP_PROTO(struct dwc3_ep *dep), 294 292 TP_ARGS(dep), 295 293 TP_STRUCT__entry( 296 - __dynamic_array(char, name, DWC3_MSG_MAX) 294 + __string(name, dep->name) 297 295 __field(unsigned, maxpacket) 298 296 __field(unsigned, maxpacket_limit) 299 297 __field(unsigned, max_streams) ··· 304 302 __field(u8, trb_dequeue) 305 303 ), 306 304 TP_fast_assign( 307 - snprintf(__get_str(name), DWC3_MSG_MAX, "%s", dep->name); 305 + __assign_str(name, dep->name); 308 306 __entry->maxpacket = dep->endpoint.maxpacket; 309 307 __entry->maxpacket_limit = dep->endpoint.maxpacket_limit; 310 308 __entry->max_streams = dep->endpoint.max_streams;
+12
drivers/usb/dwc3/ulpi.c
··· 41 41 u32 reg; 42 42 int ret; 43 43 44 + reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0)); 45 + if (reg & DWC3_GUSB2PHYCFG_SUSPHY) { 46 + reg &= ~DWC3_GUSB2PHYCFG_SUSPHY; 47 + dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg); 48 + } 49 + 44 50 reg = DWC3_GUSB2PHYACC_NEWREGREQ | DWC3_ULPI_ADDR(addr); 45 51 dwc3_writel(dwc->regs, DWC3_GUSB2PHYACC(0), reg); 46 52 ··· 63 57 { 64 58 struct dwc3 *dwc = dev_get_drvdata(dev); 65 59 u32 reg; 60 + 61 + reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0)); 62 + if (reg & DWC3_GUSB2PHYCFG_SUSPHY) { 63 + reg &= ~DWC3_GUSB2PHYCFG_SUSPHY; 64 + dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg); 65 + } 66 66 67 67 reg = DWC3_GUSB2PHYACC_NEWREGREQ | DWC3_ULPI_ADDR(addr); 68 68 reg |= DWC3_GUSB2PHYACC_WRITE | val;
+28 -3
drivers/usb/gadget/Kconfig
··· 41 41 don't have this kind of hardware (except maybe inside Linux PDAs). 42 42 43 43 For more information, see <http://www.linux-usb.org/gadget> and 44 - the kernel DocBook documentation for this API. 44 + the kernel documentation for this API. 45 45 46 46 if USB_GADGET 47 47 ··· 158 158 config USB_U_ETHER 159 159 tristate 160 160 161 + config USB_U_AUDIO 162 + tristate 163 + 161 164 config USB_F_SERIAL 162 165 tristate 163 166 ··· 192 189 tristate 193 190 194 191 config USB_F_UAC1 192 + tristate 193 + 194 + config USB_F_UAC1_LEGACY 195 195 tristate 196 196 197 197 config USB_F_UAC2 ··· 374 368 depends on SND 375 369 select USB_LIBCOMPOSITE 376 370 select SND_PCM 371 + select USB_U_AUDIO 377 372 select USB_F_UAC1 378 373 help 379 374 This Audio function implements 1 AudioControl interface, 380 375 1 AudioStreaming Interface each for USB-OUT and USB-IN. 381 - This driver requires a real Audio codec to be present 382 - on the device. 376 + This driver doesn't expect any real Audio codec to be present 377 + on the device - the audio streams are simply sinked to and 378 + sourced from a virtual ALSA sound card created. The user-space 379 + application may choose to do whatever it wants with the data 380 + received from the USB Host and choose to provide whatever it 381 + wants as audio data to the USB Host. 382 + 383 + config USB_CONFIGFS_F_UAC1_LEGACY 384 + bool "Audio Class 1.0 (legacy implementation)" 385 + depends on USB_CONFIGFS 386 + depends on SND 387 + select USB_LIBCOMPOSITE 388 + select SND_PCM 389 + select USB_F_UAC1_LEGACY 390 + help 391 + This Audio function implements 1 AudioControl interface, 392 + 1 AudioStreaming Interface each for USB-OUT and USB-IN. 393 + This is a legacy driver and requires a real Audio codec 394 + to be present on the device. 383 395 384 396 config USB_CONFIGFS_F_UAC2 385 397 bool "Audio Class 2.0" ··· 405 381 depends on SND 406 382 select USB_LIBCOMPOSITE 407 383 select SND_PCM 384 + select USB_U_AUDIO 408 385 select USB_F_UAC2 409 386 help 410 387 This Audio function is compatible with USB Audio Class
+33 -24
drivers/usb/gadget/composite.c
··· 610 610 static int bos_desc(struct usb_composite_dev *cdev) 611 611 { 612 612 struct usb_ext_cap_descriptor *usb_ext; 613 - struct usb_ss_cap_descriptor *ss_cap; 614 613 struct usb_dcd_config_params dcd_config_params; 615 614 struct usb_bos_descriptor *bos = cdev->req->buf; 616 615 ··· 635 636 * The Superspeed USB Capability descriptor shall be implemented by all 636 637 * SuperSpeed devices. 637 638 */ 638 - ss_cap = cdev->req->buf + le16_to_cpu(bos->wTotalLength); 639 - bos->bNumDeviceCaps++; 640 - le16_add_cpu(&bos->wTotalLength, USB_DT_USB_SS_CAP_SIZE); 641 - ss_cap->bLength = USB_DT_USB_SS_CAP_SIZE; 642 - ss_cap->bDescriptorType = USB_DT_DEVICE_CAPABILITY; 643 - ss_cap->bDevCapabilityType = USB_SS_CAP_TYPE; 644 - ss_cap->bmAttributes = 0; /* LTM is not supported yet */ 645 - ss_cap->wSpeedSupported = cpu_to_le16(USB_LOW_SPEED_OPERATION | 646 - USB_FULL_SPEED_OPERATION | 647 - USB_HIGH_SPEED_OPERATION | 648 - USB_5GBPS_OPERATION); 649 - ss_cap->bFunctionalitySupport = USB_LOW_SPEED_OPERATION; 639 + if (gadget_is_superspeed(cdev->gadget)) { 640 + struct usb_ss_cap_descriptor *ss_cap; 650 641 651 - /* Get Controller configuration */ 652 - if (cdev->gadget->ops->get_config_params) 653 - cdev->gadget->ops->get_config_params(&dcd_config_params); 654 - else { 655 - dcd_config_params.bU1devExitLat = USB_DEFAULT_U1_DEV_EXIT_LAT; 656 - dcd_config_params.bU2DevExitLat = 657 - cpu_to_le16(USB_DEFAULT_U2_DEV_EXIT_LAT); 642 + ss_cap = cdev->req->buf + le16_to_cpu(bos->wTotalLength); 643 + bos->bNumDeviceCaps++; 644 + le16_add_cpu(&bos->wTotalLength, USB_DT_USB_SS_CAP_SIZE); 645 + ss_cap->bLength = USB_DT_USB_SS_CAP_SIZE; 646 + ss_cap->bDescriptorType = USB_DT_DEVICE_CAPABILITY; 647 + ss_cap->bDevCapabilityType = USB_SS_CAP_TYPE; 648 + ss_cap->bmAttributes = 0; /* LTM is not supported yet */ 649 + ss_cap->wSpeedSupported = cpu_to_le16(USB_LOW_SPEED_OPERATION | 650 + USB_FULL_SPEED_OPERATION | 651 + USB_HIGH_SPEED_OPERATION | 652 + USB_5GBPS_OPERATION); 653 + ss_cap->bFunctionalitySupport = USB_LOW_SPEED_OPERATION; 654 + 655 + /* Get Controller configuration */ 656 + if (cdev->gadget->ops->get_config_params) { 657 + cdev->gadget->ops->get_config_params( 658 + &dcd_config_params); 659 + } else { 660 + dcd_config_params.bU1devExitLat = 661 + USB_DEFAULT_U1_DEV_EXIT_LAT; 662 + dcd_config_params.bU2DevExitLat = 663 + cpu_to_le16(USB_DEFAULT_U2_DEV_EXIT_LAT); 664 + } 665 + ss_cap->bU1devExitLat = dcd_config_params.bU1devExitLat; 666 + ss_cap->bU2DevExitLat = dcd_config_params.bU2DevExitLat; 658 667 } 659 - ss_cap->bU1devExitLat = dcd_config_params.bU1devExitLat; 660 - ss_cap->bU2DevExitLat = dcd_config_params.bU2DevExitLat; 661 668 662 669 /* The SuperSpeedPlus USB Device Capability descriptor */ 663 670 if (gadget_is_superspeed_plus(cdev->gadget)) { ··· 1607 1602 cdev->desc.bcdUSB = cpu_to_le16(0x0210); 1608 1603 } 1609 1604 } else { 1610 - cdev->desc.bcdUSB = cpu_to_le16(0x0200); 1605 + if (gadget->lpm_capable) 1606 + cdev->desc.bcdUSB = cpu_to_le16(0x0201); 1607 + else 1608 + cdev->desc.bcdUSB = cpu_to_le16(0x0200); 1611 1609 } 1612 1610 1613 1611 value = min(w_length, (u16) sizeof cdev->desc); ··· 1641 1633 value = min(w_length, (u16) value); 1642 1634 break; 1643 1635 case USB_DT_BOS: 1644 - if (gadget_is_superspeed(gadget)) { 1636 + if (gadget_is_superspeed(gadget) || 1637 + gadget->lpm_capable) { 1645 1638 value = bos_desc(cdev); 1646 1639 value = min(w_length, (u16) value); 1647 1640 }
+9 -5
drivers/usb/gadget/configfs.c
··· 738 738 739 739 static ssize_t os_desc_use_show(struct config_item *item, char *page) 740 740 { 741 - return sprintf(page, "%d", 741 + return sprintf(page, "%d\n", 742 742 os_desc_item_to_gadget_info(item)->use_os_desc); 743 743 } 744 744 ··· 762 762 763 763 static ssize_t os_desc_b_vendor_code_show(struct config_item *item, char *page) 764 764 { 765 - return sprintf(page, "%d", 765 + return sprintf(page, "0x%02x\n", 766 766 os_desc_item_to_gadget_info(item)->b_vendor_code); 767 767 } 768 768 ··· 787 787 static ssize_t os_desc_qw_sign_show(struct config_item *item, char *page) 788 788 { 789 789 struct gadget_info *gi = os_desc_item_to_gadget_info(item); 790 + int res; 790 791 791 - memcpy(page, gi->qw_sign, OS_STRING_QW_SIGN_LEN); 792 - return OS_STRING_QW_SIGN_LEN; 792 + res = utf16s_to_utf8s((wchar_t *) gi->qw_sign, OS_STRING_QW_SIGN_LEN, 793 + UTF16_LITTLE_ENDIAN, page, PAGE_SIZE - 1); 794 + page[res++] = '\n'; 795 + 796 + return res; 793 797 } 794 798 795 799 static ssize_t os_desc_qw_sign_store(struct config_item *item, const char *page, ··· 904 900 905 901 static ssize_t ext_prop_type_show(struct config_item *item, char *page) 906 902 { 907 - return sprintf(page, "%d", to_usb_os_desc_ext_prop(item)->type); 903 + return sprintf(page, "%d\n", to_usb_os_desc_ext_prop(item)->type); 908 904 } 909 905 910 906 static ssize_t ext_prop_type_store(struct config_item *item,
+4 -1
drivers/usb/gadget/function/Makefile
··· 32 32 obj-$(CONFIG_USB_F_MASS_STORAGE)+= usb_f_mass_storage.o 33 33 usb_f_fs-y := f_fs.o 34 34 obj-$(CONFIG_USB_F_FS) += usb_f_fs.o 35 - usb_f_uac1-y := f_uac1.o u_uac1.o 35 + obj-$(CONFIG_USB_U_AUDIO) += u_audio.o 36 + usb_f_uac1-y := f_uac1.o 36 37 obj-$(CONFIG_USB_F_UAC1) += usb_f_uac1.o 38 + usb_f_uac1_legacy-y := f_uac1_legacy.o u_uac1_legacy.o 39 + obj-$(CONFIG_USB_F_UAC1_LEGACY) += usb_f_uac1_legacy.o 37 40 usb_f_uac2-y := f_uac2.o 38 41 obj-$(CONFIG_USB_F_UAC2) += usb_f_uac2.o 39 42 usb_f_uvc-y := f_uvc.o uvc_queue.o uvc_v4l2.o uvc_video.o uvc_configfs.o
+63 -47
drivers/usb/gadget/function/f_fs.c
··· 127 127 struct ffs_epfile { 128 128 /* Protects ep->ep and ep->req. */ 129 129 struct mutex mutex; 130 - wait_queue_head_t wait; 131 130 132 131 struct ffs_data *ffs; 133 132 struct ffs_ep *ep; /* P: ffs->eps_lock */ ··· 888 889 if (file->f_flags & O_NONBLOCK) 889 890 return -EAGAIN; 890 891 891 - ret = wait_event_interruptible(epfile->wait, (ep = epfile->ep)); 892 + ret = wait_event_interruptible( 893 + epfile->ffs->wait, (ep = epfile->ep)); 892 894 if (ret) 893 895 return -EINTR; 894 896 } ··· 1189 1189 unsigned long value) 1190 1190 { 1191 1191 struct ffs_epfile *epfile = file->private_data; 1192 + struct ffs_ep *ep; 1192 1193 int ret; 1193 1194 1194 1195 ENTER(); ··· 1197 1196 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE)) 1198 1197 return -ENODEV; 1199 1198 1199 + /* Wait for endpoint to be enabled */ 1200 + ep = epfile->ep; 1201 + if (!ep) { 1202 + if (file->f_flags & O_NONBLOCK) 1203 + return -EAGAIN; 1204 + 1205 + ret = wait_event_interruptible( 1206 + epfile->ffs->wait, (ep = epfile->ep)); 1207 + if (ret) 1208 + return -EINTR; 1209 + } 1210 + 1200 1211 spin_lock_irq(&epfile->ffs->eps_lock); 1201 - if (likely(epfile->ep)) { 1202 - switch (code) { 1203 - case FUNCTIONFS_FIFO_STATUS: 1204 - ret = usb_ep_fifo_status(epfile->ep->ep); 1205 - break; 1206 - case FUNCTIONFS_FIFO_FLUSH: 1207 - usb_ep_fifo_flush(epfile->ep->ep); 1208 - ret = 0; 1209 - break; 1210 - case FUNCTIONFS_CLEAR_HALT: 1211 - ret = usb_ep_clear_halt(epfile->ep->ep); 1212 - break; 1213 - case FUNCTIONFS_ENDPOINT_REVMAP: 1214 - ret = epfile->ep->num; 1215 - break; 1216 - case FUNCTIONFS_ENDPOINT_DESC: 1217 - { 1218 - int desc_idx; 1219 - struct usb_endpoint_descriptor *desc; 1220 1212 1221 - switch (epfile->ffs->gadget->speed) { 1222 - case USB_SPEED_SUPER: 1223 - desc_idx = 2; 1224 - break; 1225 - case USB_SPEED_HIGH: 1226 - desc_idx = 1; 1227 - break; 1228 - default: 1229 - desc_idx = 0; 1230 - } 1231 - desc = epfile->ep->descs[desc_idx]; 1213 + /* In the meantime, endpoint got disabled or changed. */ 1214 + if (epfile->ep != ep) { 1215 + spin_unlock_irq(&epfile->ffs->eps_lock); 1216 + return -ESHUTDOWN; 1217 + } 1232 1218 1233 - spin_unlock_irq(&epfile->ffs->eps_lock); 1234 - ret = copy_to_user((void *)value, desc, desc->bLength); 1235 - if (ret) 1236 - ret = -EFAULT; 1237 - return ret; 1238 - } 1219 + switch (code) { 1220 + case FUNCTIONFS_FIFO_STATUS: 1221 + ret = usb_ep_fifo_status(epfile->ep->ep); 1222 + break; 1223 + case FUNCTIONFS_FIFO_FLUSH: 1224 + usb_ep_fifo_flush(epfile->ep->ep); 1225 + ret = 0; 1226 + break; 1227 + case FUNCTIONFS_CLEAR_HALT: 1228 + ret = usb_ep_clear_halt(epfile->ep->ep); 1229 + break; 1230 + case FUNCTIONFS_ENDPOINT_REVMAP: 1231 + ret = epfile->ep->num; 1232 + break; 1233 + case FUNCTIONFS_ENDPOINT_DESC: 1234 + { 1235 + int desc_idx; 1236 + struct usb_endpoint_descriptor *desc; 1237 + 1238 + switch (epfile->ffs->gadget->speed) { 1239 + case USB_SPEED_SUPER: 1240 + desc_idx = 2; 1241 + break; 1242 + case USB_SPEED_HIGH: 1243 + desc_idx = 1; 1244 + break; 1239 1245 default: 1240 - ret = -ENOTTY; 1246 + desc_idx = 0; 1241 1247 } 1242 - } else { 1243 - ret = -ENODEV; 1248 + desc = epfile->ep->descs[desc_idx]; 1249 + 1250 + spin_unlock_irq(&epfile->ffs->eps_lock); 1251 + ret = copy_to_user((void *)value, desc, desc->bLength); 1252 + if (ret) 1253 + ret = -EFAULT; 1254 + return ret; 1255 + } 1256 + default: 1257 + ret = -ENOTTY; 1244 1258 } 1245 1259 spin_unlock_irq(&epfile->ffs->eps_lock); 1246 1260 ··· 1609 1593 pr_info("%s(): freeing\n", __func__); 1610 1594 ffs_data_clear(ffs); 1611 1595 BUG_ON(waitqueue_active(&ffs->ev.waitq) || 1612 - waitqueue_active(&ffs->ep0req_completion.wait)); 1596 + waitqueue_active(&ffs->ep0req_completion.wait) || 1597 + waitqueue_active(&ffs->wait)); 1613 1598 kfree(ffs->dev_name); 1614 1599 kfree(ffs); 1615 1600 } ··· 1657 1640 mutex_init(&ffs->mutex); 1658 1641 spin_lock_init(&ffs->eps_lock); 1659 1642 init_waitqueue_head(&ffs->ev.waitq); 1643 + init_waitqueue_head(&ffs->wait); 1660 1644 init_completion(&ffs->ep0req_completion); 1661 1645 1662 1646 /* XXX REVISIT need to update it in some places, or do we? */ ··· 1779 1761 for (i = 1; i <= count; ++i, ++epfile) { 1780 1762 epfile->ffs = ffs; 1781 1763 mutex_init(&epfile->mutex); 1782 - init_waitqueue_head(&epfile->wait); 1783 1764 if (ffs->user_flags & FUNCTIONFS_VIRTUAL_ADDR) 1784 1765 sprintf(epfile->name, "ep%02x", ffs->eps_addrmap[i]); 1785 1766 else ··· 1803 1786 ENTER(); 1804 1787 1805 1788 for (; count; --count, ++epfile) { 1806 - BUG_ON(mutex_is_locked(&epfile->mutex) || 1807 - waitqueue_active(&epfile->wait)); 1789 + BUG_ON(mutex_is_locked(&epfile->mutex)); 1808 1790 if (epfile->dentry) { 1809 1791 d_delete(epfile->dentry); 1810 1792 dput(epfile->dentry); ··· 1890 1874 break; 1891 1875 } 1892 1876 1893 - wake_up(&epfile->wait); 1894 - 1895 1877 ++ep; 1896 1878 ++epfile; 1897 1879 } 1880 + 1881 + wake_up_interruptible(&ffs->wait); 1898 1882 spin_unlock_irqrestore(&func->ffs->eps_lock, flags); 1899 1883 1900 1884 return ret;
+183 -265
drivers/usb/gadget/function/f_mass_storage.c
··· 260 260 struct usb_gadget *gadget; 261 261 struct usb_composite_dev *cdev; 262 262 struct fsg_dev *fsg, *new_fsg; 263 + wait_queue_head_t io_wait; 263 264 wait_queue_head_t fsg_wait; 264 265 265 266 /* filesem protects: backing files in use */ 266 267 struct rw_semaphore filesem; 267 268 268 - /* lock protects: state, all the req_busy's */ 269 + /* lock protects: state and thread_task */ 269 270 spinlock_t lock; 270 271 271 272 struct usb_ep *ep0; /* Copy of gadget->ep0 */ ··· 304 303 unsigned int running:1; 305 304 unsigned int sysfs:1; 306 305 307 - int thread_wakeup_needed; 308 306 struct completion thread_notifier; 309 307 struct task_struct *thread_task; 310 308 ··· 355 355 356 356 static int exception_in_progress(struct fsg_common *common) 357 357 { 358 - return common->state > FSG_STATE_IDLE; 358 + return common->state > FSG_STATE_NORMAL; 359 359 } 360 360 361 361 /* Make bulk-out requests be divisible by the maxpacket size */ ··· 392 392 /*-------------------------------------------------------------------------*/ 393 393 394 394 /* These routines may be called in process context or in_irq */ 395 - 396 - /* Caller must hold fsg->lock */ 397 - static void wakeup_thread(struct fsg_common *common) 398 - { 399 - /* 400 - * Ensure the reading of thread_wakeup_needed 401 - * and the writing of bh->state are completed 402 - */ 403 - smp_mb(); 404 - /* Tell the main thread that something has happened */ 405 - common->thread_wakeup_needed = 1; 406 - if (common->thread_task) 407 - wake_up_process(common->thread_task); 408 - } 409 395 410 396 static void raise_exception(struct fsg_common *common, enum fsg_state new_state) 411 397 { ··· 446 460 if (req->status == -ECONNRESET) /* Request was cancelled */ 447 461 usb_ep_fifo_flush(ep); 448 462 449 - /* Hold the lock while we update the request and buffer states */ 450 - smp_wmb(); 451 - spin_lock(&common->lock); 452 - bh->inreq_busy = 0; 453 - bh->state = BUF_STATE_EMPTY; 454 - wakeup_thread(common); 455 - spin_unlock(&common->lock); 463 + /* Synchronize with the smp_load_acquire() in sleep_thread() */ 464 + smp_store_release(&bh->state, BUF_STATE_EMPTY); 465 + wake_up(&common->io_wait); 456 466 } 457 467 458 468 static void bulk_out_complete(struct usb_ep *ep, struct usb_request *req) ··· 463 481 if (req->status == -ECONNRESET) /* Request was cancelled */ 464 482 usb_ep_fifo_flush(ep); 465 483 466 - /* Hold the lock while we update the request and buffer states */ 467 - smp_wmb(); 468 - spin_lock(&common->lock); 469 - bh->outreq_busy = 0; 470 - bh->state = BUF_STATE_FULL; 471 - wakeup_thread(common); 472 - spin_unlock(&common->lock); 484 + /* Synchronize with the smp_load_acquire() in sleep_thread() */ 485 + smp_store_release(&bh->state, BUF_STATE_FULL); 486 + wake_up(&common->io_wait); 473 487 } 474 488 475 489 static int _fsg_common_get_max_lun(struct fsg_common *common) ··· 510 532 * and reinitialize our state. 511 533 */ 512 534 DBG(fsg, "bulk reset request\n"); 513 - raise_exception(fsg->common, FSG_STATE_RESET); 535 + raise_exception(fsg->common, FSG_STATE_PROTOCOL_RESET); 514 536 return USB_GADGET_DELAYED_STATUS; 515 537 516 538 case US_BULK_GET_MAX_LUN: ··· 541 563 /* All the following routines run in process context */ 542 564 543 565 /* Use this for bulk or interrupt transfers, not ep0 */ 544 - static void start_transfer(struct fsg_dev *fsg, struct usb_ep *ep, 545 - struct usb_request *req, int *pbusy, 546 - enum fsg_buffer_state *state) 566 + static int start_transfer(struct fsg_dev *fsg, struct usb_ep *ep, 567 + struct usb_request *req) 547 568 { 548 569 int rc; 549 570 550 571 if (ep == fsg->bulk_in) 551 572 dump_msg(fsg, "bulk-in", req->buf, req->length); 552 573 553 - spin_lock_irq(&fsg->common->lock); 554 - *pbusy = 1; 555 - *state = BUF_STATE_BUSY; 556 - spin_unlock_irq(&fsg->common->lock); 557 - 558 574 rc = usb_ep_queue(ep, req, GFP_KERNEL); 559 - if (rc == 0) 560 - return; /* All good, we're done */ 575 + if (rc) { 561 576 562 - *pbusy = 0; 563 - *state = BUF_STATE_EMPTY; 577 + /* We can't do much more than wait for a reset */ 578 + req->status = rc; 564 579 565 - /* We can't do much more than wait for a reset */ 566 - 567 - /* 568 - * Note: currently the net2280 driver fails zero-length 569 - * submissions if DMA is enabled. 570 - */ 571 - if (rc != -ESHUTDOWN && !(rc == -EOPNOTSUPP && req->length == 0)) 572 - WARNING(fsg, "error in submission: %s --> %d\n", ep->name, rc); 580 + /* 581 + * Note: currently the net2280 driver fails zero-length 582 + * submissions if DMA is enabled. 583 + */ 584 + if (rc != -ESHUTDOWN && 585 + !(rc == -EOPNOTSUPP && req->length == 0)) 586 + WARNING(fsg, "error in submission: %s --> %d\n", 587 + ep->name, rc); 588 + } 589 + return rc; 573 590 } 574 591 575 592 static bool start_in_transfer(struct fsg_common *common, struct fsg_buffhd *bh) 576 593 { 577 594 if (!fsg_is_set(common)) 578 595 return false; 579 - start_transfer(common->fsg, common->fsg->bulk_in, 580 - bh->inreq, &bh->inreq_busy, &bh->state); 596 + bh->state = BUF_STATE_SENDING; 597 + if (start_transfer(common->fsg, common->fsg->bulk_in, bh->inreq)) 598 + bh->state = BUF_STATE_EMPTY; 581 599 return true; 582 600 } 583 601 ··· 581 607 { 582 608 if (!fsg_is_set(common)) 583 609 return false; 584 - start_transfer(common->fsg, common->fsg->bulk_out, 585 - bh->outreq, &bh->outreq_busy, &bh->state); 610 + bh->state = BUF_STATE_RECEIVING; 611 + if (start_transfer(common->fsg, common->fsg->bulk_out, bh->outreq)) 612 + bh->state = BUF_STATE_FULL; 586 613 return true; 587 614 } 588 615 589 - static int sleep_thread(struct fsg_common *common, bool can_freeze) 616 + static int sleep_thread(struct fsg_common *common, bool can_freeze, 617 + struct fsg_buffhd *bh) 590 618 { 591 - int rc = 0; 619 + int rc; 592 620 593 - /* Wait until a signal arrives or we are woken up */ 594 - for (;;) { 595 - if (can_freeze) 596 - try_to_freeze(); 597 - set_current_state(TASK_INTERRUPTIBLE); 598 - if (signal_pending(current)) { 599 - rc = -EINTR; 600 - break; 601 - } 602 - if (common->thread_wakeup_needed) 603 - break; 604 - schedule(); 605 - } 606 - __set_current_state(TASK_RUNNING); 607 - common->thread_wakeup_needed = 0; 608 - 609 - /* 610 - * Ensure the writing of thread_wakeup_needed 611 - * and the reading of bh->state are completed 612 - */ 613 - smp_mb(); 614 - return rc; 621 + /* Wait until a signal arrives or bh is no longer busy */ 622 + if (can_freeze) 623 + /* 624 + * synchronize with the smp_store_release(&bh->state) in 625 + * bulk_in_complete() or bulk_out_complete() 626 + */ 627 + rc = wait_event_freezable(common->io_wait, 628 + bh && smp_load_acquire(&bh->state) >= 629 + BUF_STATE_EMPTY); 630 + else 631 + rc = wait_event_interruptible(common->io_wait, 632 + bh && smp_load_acquire(&bh->state) >= 633 + BUF_STATE_EMPTY); 634 + return rc ? -EINTR : 0; 615 635 } 616 636 617 637 ··· 665 697 666 698 /* Wait for the next buffer to become available */ 667 699 bh = common->next_buffhd_to_fill; 668 - while (bh->state != BUF_STATE_EMPTY) { 669 - rc = sleep_thread(common, false); 670 - if (rc) 671 - return rc; 672 - } 700 + rc = sleep_thread(common, false, bh); 701 + if (rc) 702 + return rc; 673 703 674 704 /* 675 705 * If we were asked to read past the end of file, ··· 844 878 bh = common->next_buffhd_to_drain; 845 879 if (bh->state == BUF_STATE_EMPTY && !get_some_more) 846 880 break; /* We stopped early */ 847 - if (bh->state == BUF_STATE_FULL) { 848 - smp_rmb(); 849 - common->next_buffhd_to_drain = bh->next; 850 - bh->state = BUF_STATE_EMPTY; 851 881 852 - /* Did something go wrong with the transfer? */ 853 - if (bh->outreq->status != 0) { 854 - curlun->sense_data = SS_COMMUNICATION_FAILURE; 855 - curlun->sense_data_info = 856 - file_offset >> curlun->blkbits; 857 - curlun->info_valid = 1; 858 - break; 859 - } 860 - 861 - amount = bh->outreq->actual; 862 - if (curlun->file_length - file_offset < amount) { 863 - LERROR(curlun, 864 - "write %u @ %llu beyond end %llu\n", 865 - amount, (unsigned long long)file_offset, 866 - (unsigned long long)curlun->file_length); 867 - amount = curlun->file_length - file_offset; 868 - } 869 - 870 - /* Don't accept excess data. The spec doesn't say 871 - * what to do in this case. We'll ignore the error. 872 - */ 873 - amount = min(amount, bh->bulk_out_intended_length); 874 - 875 - /* Don't write a partial block */ 876 - amount = round_down(amount, curlun->blksize); 877 - if (amount == 0) 878 - goto empty_write; 879 - 880 - /* Perform the write */ 881 - file_offset_tmp = file_offset; 882 - nwritten = vfs_write(curlun->filp, 883 - (char __user *)bh->buf, 884 - amount, &file_offset_tmp); 885 - VLDBG(curlun, "file write %u @ %llu -> %d\n", amount, 886 - (unsigned long long)file_offset, (int)nwritten); 887 - if (signal_pending(current)) 888 - return -EINTR; /* Interrupted! */ 889 - 890 - if (nwritten < 0) { 891 - LDBG(curlun, "error in file write: %d\n", 892 - (int)nwritten); 893 - nwritten = 0; 894 - } else if (nwritten < amount) { 895 - LDBG(curlun, "partial file write: %d/%u\n", 896 - (int)nwritten, amount); 897 - nwritten = round_down(nwritten, curlun->blksize); 898 - } 899 - file_offset += nwritten; 900 - amount_left_to_write -= nwritten; 901 - common->residue -= nwritten; 902 - 903 - /* If an error occurred, report it and its position */ 904 - if (nwritten < amount) { 905 - curlun->sense_data = SS_WRITE_ERROR; 906 - curlun->sense_data_info = 907 - file_offset >> curlun->blkbits; 908 - curlun->info_valid = 1; 909 - break; 910 - } 911 - 912 - empty_write: 913 - /* Did the host decide to stop early? */ 914 - if (bh->outreq->actual < bh->bulk_out_intended_length) { 915 - common->short_packet_received = 1; 916 - break; 917 - } 918 - continue; 919 - } 920 - 921 - /* Wait for something to happen */ 922 - rc = sleep_thread(common, false); 882 + /* Wait for the data to be received */ 883 + rc = sleep_thread(common, false, bh); 923 884 if (rc) 924 885 return rc; 886 + 887 + common->next_buffhd_to_drain = bh->next; 888 + bh->state = BUF_STATE_EMPTY; 889 + 890 + /* Did something go wrong with the transfer? */ 891 + if (bh->outreq->status != 0) { 892 + curlun->sense_data = SS_COMMUNICATION_FAILURE; 893 + curlun->sense_data_info = 894 + file_offset >> curlun->blkbits; 895 + curlun->info_valid = 1; 896 + break; 897 + } 898 + 899 + amount = bh->outreq->actual; 900 + if (curlun->file_length - file_offset < amount) { 901 + LERROR(curlun, "write %u @ %llu beyond end %llu\n", 902 + amount, (unsigned long long)file_offset, 903 + (unsigned long long)curlun->file_length); 904 + amount = curlun->file_length - file_offset; 905 + } 906 + 907 + /* 908 + * Don't accept excess data. The spec doesn't say 909 + * what to do in this case. We'll ignore the error. 910 + */ 911 + amount = min(amount, bh->bulk_out_intended_length); 912 + 913 + /* Don't write a partial block */ 914 + amount = round_down(amount, curlun->blksize); 915 + if (amount == 0) 916 + goto empty_write; 917 + 918 + /* Perform the write */ 919 + file_offset_tmp = file_offset; 920 + nwritten = vfs_write(curlun->filp, (char __user *)bh->buf, 921 + amount, &file_offset_tmp); 922 + VLDBG(curlun, "file write %u @ %llu -> %d\n", amount, 923 + (unsigned long long)file_offset, (int)nwritten); 924 + if (signal_pending(current)) 925 + return -EINTR; /* Interrupted! */ 926 + 927 + if (nwritten < 0) { 928 + LDBG(curlun, "error in file write: %d\n", 929 + (int) nwritten); 930 + nwritten = 0; 931 + } else if (nwritten < amount) { 932 + LDBG(curlun, "partial file write: %d/%u\n", 933 + (int) nwritten, amount); 934 + nwritten = round_down(nwritten, curlun->blksize); 935 + } 936 + file_offset += nwritten; 937 + amount_left_to_write -= nwritten; 938 + common->residue -= nwritten; 939 + 940 + /* If an error occurred, report it and its position */ 941 + if (nwritten < amount) { 942 + curlun->sense_data = SS_WRITE_ERROR; 943 + curlun->sense_data_info = 944 + file_offset >> curlun->blkbits; 945 + curlun->info_valid = 1; 946 + break; 947 + } 948 + 949 + empty_write: 950 + /* Did the host decide to stop early? */ 951 + if (bh->outreq->actual < bh->bulk_out_intended_length) { 952 + common->short_packet_received = 1; 953 + break; 954 + } 925 955 } 926 956 927 957 return -EIO; /* No default reply */ ··· 1442 1480 1443 1481 static int throw_away_data(struct fsg_common *common) 1444 1482 { 1445 - struct fsg_buffhd *bh; 1483 + struct fsg_buffhd *bh, *bh2; 1446 1484 u32 amount; 1447 1485 int rc; 1448 1486 ··· 1450 1488 bh->state != BUF_STATE_EMPTY || common->usb_amount_left > 0; 1451 1489 bh = common->next_buffhd_to_drain) { 1452 1490 1453 - /* Throw away the data in a filled buffer */ 1454 - if (bh->state == BUF_STATE_FULL) { 1455 - smp_rmb(); 1456 - bh->state = BUF_STATE_EMPTY; 1457 - common->next_buffhd_to_drain = bh->next; 1458 - 1459 - /* A short packet or an error ends everything */ 1460 - if (bh->outreq->actual < bh->bulk_out_intended_length || 1461 - bh->outreq->status != 0) { 1462 - raise_exception(common, 1463 - FSG_STATE_ABORT_BULK_OUT); 1464 - return -EINTR; 1465 - } 1466 - continue; 1467 - } 1468 - 1469 1491 /* Try to submit another request if we need one */ 1470 - bh = common->next_buffhd_to_fill; 1471 - if (bh->state == BUF_STATE_EMPTY 1472 - && common->usb_amount_left > 0) { 1492 + bh2 = common->next_buffhd_to_fill; 1493 + if (bh2->state == BUF_STATE_EMPTY && 1494 + common->usb_amount_left > 0) { 1473 1495 amount = min(common->usb_amount_left, FSG_BUFLEN); 1474 1496 1475 1497 /* ··· 1461 1515 * equal to the buffer size, which is divisible by 1462 1516 * the bulk-out maxpacket size. 1463 1517 */ 1464 - set_bulk_out_req_length(common, bh, amount); 1465 - if (!start_out_transfer(common, bh)) 1518 + set_bulk_out_req_length(common, bh2, amount); 1519 + if (!start_out_transfer(common, bh2)) 1466 1520 /* Dunno what to do if common->fsg is NULL */ 1467 1521 return -EIO; 1468 - common->next_buffhd_to_fill = bh->next; 1522 + common->next_buffhd_to_fill = bh2->next; 1469 1523 common->usb_amount_left -= amount; 1470 1524 continue; 1471 1525 } 1472 1526 1473 - /* Otherwise wait for something to happen */ 1474 - rc = sleep_thread(common, true); 1527 + /* Wait for the data to be received */ 1528 + rc = sleep_thread(common, false, bh); 1475 1529 if (rc) 1476 1530 return rc; 1531 + 1532 + /* Throw away the data in a filled buffer */ 1533 + bh->state = BUF_STATE_EMPTY; 1534 + common->next_buffhd_to_drain = bh->next; 1535 + 1536 + /* A short packet or an error ends everything */ 1537 + if (bh->outreq->actual < bh->bulk_out_intended_length || 1538 + bh->outreq->status != 0) { 1539 + raise_exception(common, FSG_STATE_ABORT_BULK_OUT); 1540 + return -EINTR; 1541 + } 1477 1542 } 1478 1543 return 0; 1479 1544 } ··· 1591 1634 return rc; 1592 1635 } 1593 1636 1594 - static int send_status(struct fsg_common *common) 1637 + static void send_status(struct fsg_common *common) 1595 1638 { 1596 1639 struct fsg_lun *curlun = common->curlun; 1597 1640 struct fsg_buffhd *bh; ··· 1602 1645 1603 1646 /* Wait for the next buffer to become available */ 1604 1647 bh = common->next_buffhd_to_fill; 1605 - while (bh->state != BUF_STATE_EMPTY) { 1606 - rc = sleep_thread(common, true); 1607 - if (rc) 1608 - return rc; 1609 - } 1648 + rc = sleep_thread(common, false, bh); 1649 + if (rc) 1650 + return; 1610 1651 1611 1652 if (curlun) { 1612 1653 sd = curlun->sense_data; ··· 1638 1683 bh->inreq->zero = 0; 1639 1684 if (!start_in_transfer(common, bh)) 1640 1685 /* Don't know what to do if common->fsg is NULL */ 1641 - return -EIO; 1686 + return; 1642 1687 1643 1688 common->next_buffhd_to_fill = bh->next; 1644 - return 0; 1689 + return; 1645 1690 } 1646 1691 1647 1692 ··· 1803 1848 /* Wait for the next buffer to become available for data or status */ 1804 1849 bh = common->next_buffhd_to_fill; 1805 1850 common->next_buffhd_to_drain = bh; 1806 - while (bh->state != BUF_STATE_EMPTY) { 1807 - rc = sleep_thread(common, true); 1808 - if (rc) 1809 - return rc; 1810 - } 1851 + rc = sleep_thread(common, false, bh); 1852 + if (rc) 1853 + return rc; 1854 + 1811 1855 common->phase_error = 0; 1812 1856 common->short_packet_received = 0; 1813 1857 ··· 2149 2195 2150 2196 /* Wait for the next buffer to become available */ 2151 2197 bh = common->next_buffhd_to_fill; 2152 - while (bh->state != BUF_STATE_EMPTY) { 2153 - rc = sleep_thread(common, true); 2154 - if (rc) 2155 - return rc; 2156 - } 2198 + rc = sleep_thread(common, true, bh); 2199 + if (rc) 2200 + return rc; 2157 2201 2158 2202 /* Queue a request to read a Bulk-only CBW */ 2159 2203 set_bulk_out_req_length(common, bh, US_BULK_CB_WRAP_LEN); ··· 2166 2214 */ 2167 2215 2168 2216 /* Wait for the CBW to arrive */ 2169 - while (bh->state != BUF_STATE_FULL) { 2170 - rc = sleep_thread(common, true); 2171 - if (rc) 2172 - return rc; 2173 - } 2174 - smp_rmb(); 2217 + rc = sleep_thread(common, true, bh); 2218 + if (rc) 2219 + return rc; 2220 + 2175 2221 rc = fsg_is_set(common) ? received_cbw(common->fsg, bh) : -EIO; 2176 2222 bh->state = BUF_STATE_EMPTY; 2177 2223 ··· 2321 2371 if (!sig) 2322 2372 break; 2323 2373 if (sig != SIGUSR1) { 2374 + spin_lock_irq(&common->lock); 2324 2375 if (common->state < FSG_STATE_EXIT) 2325 2376 DBG(common, "Main thread exiting on signal\n"); 2326 - raise_exception(common, FSG_STATE_EXIT); 2377 + common->state = FSG_STATE_EXIT; 2378 + spin_unlock_irq(&common->lock); 2327 2379 } 2328 2380 } 2329 2381 ··· 2333 2381 if (likely(common->fsg)) { 2334 2382 for (i = 0; i < common->fsg_num_buffers; ++i) { 2335 2383 bh = &common->buffhds[i]; 2336 - if (bh->inreq_busy) 2384 + if (bh->state == BUF_STATE_SENDING) 2337 2385 usb_ep_dequeue(common->fsg->bulk_in, bh->inreq); 2338 - if (bh->outreq_busy) 2386 + if (bh->state == BUF_STATE_RECEIVING) 2339 2387 usb_ep_dequeue(common->fsg->bulk_out, 2340 2388 bh->outreq); 2341 - } 2342 2389 2343 - /* Wait until everything is idle */ 2344 - for (;;) { 2345 - int num_active = 0; 2346 - for (i = 0; i < common->fsg_num_buffers; ++i) { 2347 - bh = &common->buffhds[i]; 2348 - num_active += bh->inreq_busy + bh->outreq_busy; 2349 - } 2350 - if (num_active == 0) 2351 - break; 2352 - if (sleep_thread(common, true)) 2390 + /* Wait for a transfer to become idle */ 2391 + if (sleep_thread(common, false, bh)) 2353 2392 return; 2354 2393 } 2355 2394 ··· 2365 2422 common->next_buffhd_to_drain = &common->buffhds[0]; 2366 2423 exception_req_tag = common->exception_req_tag; 2367 2424 old_state = common->state; 2425 + common->state = FSG_STATE_NORMAL; 2368 2426 2369 - if (old_state == FSG_STATE_ABORT_BULK_OUT) 2370 - common->state = FSG_STATE_STATUS_PHASE; 2371 - else { 2427 + if (old_state != FSG_STATE_ABORT_BULK_OUT) { 2372 2428 for (i = 0; i < ARRAY_SIZE(common->luns); ++i) { 2373 2429 curlun = common->luns[i]; 2374 2430 if (!curlun) ··· 2378 2436 curlun->sense_data_info = 0; 2379 2437 curlun->info_valid = 0; 2380 2438 } 2381 - common->state = FSG_STATE_IDLE; 2382 2439 } 2383 2440 spin_unlock_irq(&common->lock); 2384 2441 2385 2442 /* Carry out any extra actions required for the exception */ 2386 2443 switch (old_state) { 2387 - case FSG_STATE_ABORT_BULK_OUT: 2388 - send_status(common); 2389 - spin_lock_irq(&common->lock); 2390 - if (common->state == FSG_STATE_STATUS_PHASE) 2391 - common->state = FSG_STATE_IDLE; 2392 - spin_unlock_irq(&common->lock); 2444 + case FSG_STATE_NORMAL: 2393 2445 break; 2394 2446 2395 - case FSG_STATE_RESET: 2447 + case FSG_STATE_ABORT_BULK_OUT: 2448 + send_status(common); 2449 + break; 2450 + 2451 + case FSG_STATE_PROTOCOL_RESET: 2396 2452 /* 2397 2453 * In case we were forced against our will to halt a 2398 2454 * bulk endpoint, clear the halt now. (The SuperH UDC ··· 2423 2483 break; 2424 2484 2425 2485 case FSG_STATE_EXIT: 2426 - case FSG_STATE_TERMINATED: 2427 2486 do_set_interface(common, NULL); /* Free resources */ 2428 2487 spin_lock_irq(&common->lock); 2429 2488 common->state = FSG_STATE_TERMINATED; /* Stop the thread */ 2430 2489 spin_unlock_irq(&common->lock); 2431 2490 break; 2432 2491 2433 - case FSG_STATE_INTERFACE_CHANGE: 2434 - case FSG_STATE_DISCONNECT: 2435 - case FSG_STATE_COMMAND_PHASE: 2436 - case FSG_STATE_DATA_PHASE: 2437 - case FSG_STATE_STATUS_PHASE: 2438 - case FSG_STATE_IDLE: 2492 + case FSG_STATE_TERMINATED: 2439 2493 break; 2440 2494 } 2441 2495 } ··· 2468 2534 } 2469 2535 2470 2536 if (!common->running) { 2471 - sleep_thread(common, true); 2537 + sleep_thread(common, true, NULL); 2472 2538 continue; 2473 2539 } 2474 2540 2475 - if (get_next_command(common)) 2541 + if (get_next_command(common) || exception_in_progress(common)) 2476 2542 continue; 2477 - 2478 - spin_lock_irq(&common->lock); 2479 - if (!exception_in_progress(common)) 2480 - common->state = FSG_STATE_DATA_PHASE; 2481 - spin_unlock_irq(&common->lock); 2482 - 2483 - if (do_scsi_command(common) || finish_reply(common)) 2543 + if (do_scsi_command(common) || exception_in_progress(common)) 2484 2544 continue; 2485 - 2486 - spin_lock_irq(&common->lock); 2487 - if (!exception_in_progress(common)) 2488 - common->state = FSG_STATE_STATUS_PHASE; 2489 - spin_unlock_irq(&common->lock); 2490 - 2491 - if (send_status(common)) 2545 + if (finish_reply(common) || exception_in_progress(common)) 2492 2546 continue; 2493 - 2494 - spin_lock_irq(&common->lock); 2495 - if (!exception_in_progress(common)) 2496 - common->state = FSG_STATE_IDLE; 2497 - spin_unlock_irq(&common->lock); 2547 + send_status(common); 2498 2548 } 2499 2549 2500 2550 spin_lock_irq(&common->lock); ··· 2598 2680 spin_lock_init(&common->lock); 2599 2681 kref_init(&common->ref); 2600 2682 init_completion(&common->thread_notifier); 2683 + init_waitqueue_head(&common->io_wait); 2601 2684 init_waitqueue_head(&common->fsg_wait); 2602 2685 common->state = FSG_STATE_TERMINATED; 2603 2686 memset(common->luns, 0, sizeof(common->luns)); ··· 2900 2981 if (common->state != FSG_STATE_TERMINATED) { 2901 2982 raise_exception(common, FSG_STATE_EXIT); 2902 2983 wait_for_completion(&common->thread_notifier); 2903 - common->thread_task = NULL; 2904 2984 } 2905 2985 2906 2986 for (i = 0; i < ARRAY_SIZE(common->luns); ++i) { ··· 2948 3030 } 2949 3031 2950 3032 if (!common->thread_task) { 2951 - common->state = FSG_STATE_IDLE; 3033 + common->state = FSG_STATE_NORMAL; 2952 3034 common->thread_task = 2953 3035 kthread_create(fsg_main_thread, common, "file-storage"); 2954 3036 if (IS_ERR(common->thread_task)) { 2955 - int ret = PTR_ERR(common->thread_task); 3037 + ret = PTR_ERR(common->thread_task); 2956 3038 common->thread_task = NULL; 2957 3039 common->state = FSG_STATE_TERMINATED; 2958 3040 return ret;
+366 -546
drivers/usb/gadget/function/f_uac1.c
··· 1 1 /* 2 - * f_audio.c -- USB Audio class function driver 3 - * 4 - * Copyright (C) 2008 Bryan Wu <cooloney@kernel.org> 5 - * Copyright (C) 2008 Analog Devices, Inc 2 + * f_uac1.c -- USB Audio Class 1.0 Function (using u_audio API) 6 3 * 7 - * Enter bugs at http://blackfin.uclinux.org/ 4 + * Copyright (C) 2016 Ruslan Bilovol <ruslan.bilovol@gmail.com> 8 5 * 9 - * Licensed under the GPL-2 or later. 6 + * This driver doesn't expect any real Audio codec to be present 7 + * on the device - the audio streams are simply sinked to and 8 + * sourced from a virtual ALSA sound card created. 9 + * 10 + * This file is based on f_uac1.c which is 11 + * Copyright (C) 2008 Bryan Wu <cooloney@kernel.org> 12 + * Copyright (C) 2008 Analog Devices, Inc 13 + * 14 + * This program is free software; you can redistribute it and/or modify 15 + * it under the terms of the GNU General Public License as published by 16 + * the Free Software Foundation; either version 2 of the License, or 17 + * (at your option) any later version. 10 18 */ 11 19 12 - #include <linux/slab.h> 13 - #include <linux/kernel.h> 20 + #include <linux/usb/audio.h> 14 21 #include <linux/module.h> 15 - #include <linux/device.h> 16 - #include <linux/atomic.h> 17 22 23 + #include "u_audio.h" 18 24 #include "u_uac1.h" 19 25 20 - static int generic_set_cmd(struct usb_audio_control *con, u8 cmd, int value); 21 - static int generic_get_cmd(struct usb_audio_control *con, u8 cmd); 26 + struct f_uac1 { 27 + struct g_audio g_audio; 28 + u8 ac_intf, as_in_intf, as_out_intf; 29 + u8 ac_alt, as_in_alt, as_out_alt; /* needed for get_alt() */ 30 + }; 31 + 32 + static inline struct f_uac1 *func_to_uac1(struct usb_function *f) 33 + { 34 + return container_of(f, struct f_uac1, g_audio.func); 35 + } 22 36 23 37 /* 24 38 * DESCRIPTORS ... most are static, but strings and full ··· 40 26 */ 41 27 42 28 /* 43 - * We have two interfaces- AudioControl and AudioStreaming 44 - * TODO: only supcard playback currently 29 + * We have three interfaces - one AudioControl and two AudioStreaming 30 + * 31 + * The driver implements a simple UAC_1 topology. 32 + * USB-OUT -> IT_1 -> OT_2 -> ALSA_Capture 33 + * ALSA_Playback -> IT_3 -> OT_4 -> USB-IN 45 34 */ 46 - #define F_AUDIO_AC_INTERFACE 0 47 - #define F_AUDIO_AS_INTERFACE 1 48 - #define F_AUDIO_NUM_INTERFACES 1 35 + #define F_AUDIO_AC_INTERFACE 0 36 + #define F_AUDIO_AS_OUT_INTERFACE 1 37 + #define F_AUDIO_AS_IN_INTERFACE 2 38 + /* Number of streaming interfaces */ 39 + #define F_AUDIO_NUM_INTERFACES 2 49 40 50 41 /* B.3.1 Standard AC Interface Descriptor */ 51 42 static struct usb_interface_descriptor ac_interface_desc = { ··· 65 46 * The number of AudioStreaming and MIDIStreaming interfaces 66 47 * in the Audio Interface Collection 67 48 */ 68 - DECLARE_UAC_AC_HEADER_DESCRIPTOR(1); 49 + DECLARE_UAC_AC_HEADER_DESCRIPTOR(2); 69 50 70 51 #define UAC_DT_AC_HEADER_LENGTH UAC_DT_AC_HEADER_SIZE(F_AUDIO_NUM_INTERFACES) 71 - /* 1 input terminal, 1 output terminal and 1 feature unit */ 72 - #define UAC_DT_TOTAL_LENGTH (UAC_DT_AC_HEADER_LENGTH + UAC_DT_INPUT_TERMINAL_SIZE \ 73 - + UAC_DT_OUTPUT_TERMINAL_SIZE + UAC_DT_FEATURE_UNIT_SIZE(0)) 52 + /* 2 input terminals and 2 output terminals */ 53 + #define UAC_DT_TOTAL_LENGTH (UAC_DT_AC_HEADER_LENGTH \ 54 + + 2*UAC_DT_INPUT_TERMINAL_SIZE + 2*UAC_DT_OUTPUT_TERMINAL_SIZE) 74 55 /* B.3.2 Class-Specific AC Interface Descriptor */ 75 - static struct uac1_ac_header_descriptor_1 ac_header_desc = { 56 + static struct uac1_ac_header_descriptor_2 ac_header_desc = { 76 57 .bLength = UAC_DT_AC_HEADER_LENGTH, 77 58 .bDescriptorType = USB_DT_CS_INTERFACE, 78 59 .bDescriptorSubtype = UAC_HEADER, 79 - .bcdADC = __constant_cpu_to_le16(0x0100), 80 - .wTotalLength = __constant_cpu_to_le16(UAC_DT_TOTAL_LENGTH), 60 + .bcdADC = cpu_to_le16(0x0100), 61 + .wTotalLength = cpu_to_le16(UAC_DT_TOTAL_LENGTH), 81 62 .bInCollection = F_AUDIO_NUM_INTERFACES, 82 63 .baInterfaceNr = { 83 - /* Interface number of the first AudioStream interface */ 64 + /* Interface number of the AudioStream interfaces */ 84 65 [0] = 1, 66 + [1] = 2, 85 67 } 86 68 }; 87 69 88 - #define INPUT_TERMINAL_ID 1 89 - static struct uac_input_terminal_descriptor input_terminal_desc = { 70 + #define USB_OUT_IT_ID 1 71 + static struct uac_input_terminal_descriptor usb_out_it_desc = { 90 72 .bLength = UAC_DT_INPUT_TERMINAL_SIZE, 91 73 .bDescriptorType = USB_DT_CS_INTERFACE, 92 74 .bDescriptorSubtype = UAC_INPUT_TERMINAL, 93 - .bTerminalID = INPUT_TERMINAL_ID, 75 + .bTerminalID = USB_OUT_IT_ID, 94 76 .wTerminalType = UAC_TERMINAL_STREAMING, 95 77 .bAssocTerminal = 0, 96 78 .wChannelConfig = 0x3, 97 79 }; 98 80 99 - DECLARE_UAC_FEATURE_UNIT_DESCRIPTOR(0); 100 - 101 - #define FEATURE_UNIT_ID 2 102 - static struct uac_feature_unit_descriptor_0 feature_unit_desc = { 103 - .bLength = UAC_DT_FEATURE_UNIT_SIZE(0), 104 - .bDescriptorType = USB_DT_CS_INTERFACE, 105 - .bDescriptorSubtype = UAC_FEATURE_UNIT, 106 - .bUnitID = FEATURE_UNIT_ID, 107 - .bSourceID = INPUT_TERMINAL_ID, 108 - .bControlSize = 2, 109 - .bmaControls[0] = (UAC_FU_MUTE | UAC_FU_VOLUME), 110 - }; 111 - 112 - static struct usb_audio_control mute_control = { 113 - .list = LIST_HEAD_INIT(mute_control.list), 114 - .name = "Mute Control", 115 - .type = UAC_FU_MUTE, 116 - /* Todo: add real Mute control code */ 117 - .set = generic_set_cmd, 118 - .get = generic_get_cmd, 119 - }; 120 - 121 - static struct usb_audio_control volume_control = { 122 - .list = LIST_HEAD_INIT(volume_control.list), 123 - .name = "Volume Control", 124 - .type = UAC_FU_VOLUME, 125 - /* Todo: add real Volume control code */ 126 - .set = generic_set_cmd, 127 - .get = generic_get_cmd, 128 - }; 129 - 130 - static struct usb_audio_control_selector feature_unit = { 131 - .list = LIST_HEAD_INIT(feature_unit.list), 132 - .id = FEATURE_UNIT_ID, 133 - .name = "Mute & Volume Control", 134 - .type = UAC_FEATURE_UNIT, 135 - .desc = (struct usb_descriptor_header *)&feature_unit_desc, 136 - }; 137 - 138 - #define OUTPUT_TERMINAL_ID 3 139 - static struct uac1_output_terminal_descriptor output_terminal_desc = { 81 + #define IO_OUT_OT_ID 2 82 + static struct uac1_output_terminal_descriptor io_out_ot_desc = { 140 83 .bLength = UAC_DT_OUTPUT_TERMINAL_SIZE, 141 84 .bDescriptorType = USB_DT_CS_INTERFACE, 142 85 .bDescriptorSubtype = UAC_OUTPUT_TERMINAL, 143 - .bTerminalID = OUTPUT_TERMINAL_ID, 86 + .bTerminalID = IO_OUT_OT_ID, 144 87 .wTerminalType = UAC_OUTPUT_TERMINAL_SPEAKER, 145 - .bAssocTerminal = FEATURE_UNIT_ID, 146 - .bSourceID = FEATURE_UNIT_ID, 88 + .bAssocTerminal = 0, 89 + .bSourceID = USB_OUT_IT_ID, 90 + }; 91 + 92 + #define IO_IN_IT_ID 3 93 + static struct uac_input_terminal_descriptor io_in_it_desc = { 94 + .bLength = UAC_DT_INPUT_TERMINAL_SIZE, 95 + .bDescriptorType = USB_DT_CS_INTERFACE, 96 + .bDescriptorSubtype = UAC_INPUT_TERMINAL, 97 + .bTerminalID = IO_IN_IT_ID, 98 + .wTerminalType = UAC_INPUT_TERMINAL_MICROPHONE, 99 + .bAssocTerminal = 0, 100 + .wChannelConfig = 0x3, 101 + }; 102 + 103 + #define USB_IN_OT_ID 4 104 + static struct uac1_output_terminal_descriptor usb_in_ot_desc = { 105 + .bLength = UAC_DT_OUTPUT_TERMINAL_SIZE, 106 + .bDescriptorType = USB_DT_CS_INTERFACE, 107 + .bDescriptorSubtype = UAC_OUTPUT_TERMINAL, 108 + .bTerminalID = USB_IN_OT_ID, 109 + .wTerminalType = UAC_TERMINAL_STREAMING, 110 + .bAssocTerminal = 0, 111 + .bSourceID = IO_IN_IT_ID, 147 112 }; 148 113 149 114 /* B.4.1 Standard AS Interface Descriptor */ 150 - static struct usb_interface_descriptor as_interface_alt_0_desc = { 115 + static struct usb_interface_descriptor as_out_interface_alt_0_desc = { 151 116 .bLength = USB_DT_INTERFACE_SIZE, 152 117 .bDescriptorType = USB_DT_INTERFACE, 153 118 .bAlternateSetting = 0, ··· 140 137 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING, 141 138 }; 142 139 143 - static struct usb_interface_descriptor as_interface_alt_1_desc = { 140 + static struct usb_interface_descriptor as_out_interface_alt_1_desc = { 141 + .bLength = USB_DT_INTERFACE_SIZE, 142 + .bDescriptorType = USB_DT_INTERFACE, 143 + .bAlternateSetting = 1, 144 + .bNumEndpoints = 1, 145 + .bInterfaceClass = USB_CLASS_AUDIO, 146 + .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING, 147 + }; 148 + 149 + static struct usb_interface_descriptor as_in_interface_alt_0_desc = { 150 + .bLength = USB_DT_INTERFACE_SIZE, 151 + .bDescriptorType = USB_DT_INTERFACE, 152 + .bAlternateSetting = 0, 153 + .bNumEndpoints = 0, 154 + .bInterfaceClass = USB_CLASS_AUDIO, 155 + .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING, 156 + }; 157 + 158 + static struct usb_interface_descriptor as_in_interface_alt_1_desc = { 144 159 .bLength = USB_DT_INTERFACE_SIZE, 145 160 .bDescriptorType = USB_DT_INTERFACE, 146 161 .bAlternateSetting = 1, ··· 168 147 }; 169 148 170 149 /* B.4.2 Class-Specific AS Interface Descriptor */ 171 - static struct uac1_as_header_descriptor as_header_desc = { 150 + static struct uac1_as_header_descriptor as_out_header_desc = { 172 151 .bLength = UAC_DT_AS_HEADER_SIZE, 173 152 .bDescriptorType = USB_DT_CS_INTERFACE, 174 153 .bDescriptorSubtype = UAC_AS_GENERAL, 175 - .bTerminalLink = INPUT_TERMINAL_ID, 154 + .bTerminalLink = USB_OUT_IT_ID, 155 + .bDelay = 1, 156 + .wFormatTag = UAC_FORMAT_TYPE_I_PCM, 157 + }; 158 + 159 + static struct uac1_as_header_descriptor as_in_header_desc = { 160 + .bLength = UAC_DT_AS_HEADER_SIZE, 161 + .bDescriptorType = USB_DT_CS_INTERFACE, 162 + .bDescriptorSubtype = UAC_AS_GENERAL, 163 + .bTerminalLink = USB_IN_OT_ID, 176 164 .bDelay = 1, 177 165 .wFormatTag = UAC_FORMAT_TYPE_I_PCM, 178 166 }; 179 167 180 168 DECLARE_UAC_FORMAT_TYPE_I_DISCRETE_DESC(1); 181 169 182 - static struct uac_format_type_i_discrete_descriptor_1 as_type_i_desc = { 170 + static struct uac_format_type_i_discrete_descriptor_1 as_out_type_i_desc = { 183 171 .bLength = UAC_FORMAT_TYPE_I_DISCRETE_DESC_SIZE(1), 184 172 .bDescriptorType = USB_DT_CS_INTERFACE, 185 173 .bDescriptorSubtype = UAC_FORMAT_TYPE, ··· 214 184 .bLength = UAC_ISO_ENDPOINT_DESC_SIZE, 215 185 .bDescriptorType = USB_DT_CS_ENDPOINT, 216 186 .bDescriptorSubtype = UAC_EP_GENERAL, 217 - .bmAttributes = 1, 187 + .bmAttributes = 1, 218 188 .bLockDelayUnits = 1, 219 - .wLockDelay = __constant_cpu_to_le16(1), 189 + .wLockDelay = cpu_to_le16(1), 190 + }; 191 + 192 + static struct uac_format_type_i_discrete_descriptor_1 as_in_type_i_desc = { 193 + .bLength = UAC_FORMAT_TYPE_I_DISCRETE_DESC_SIZE(1), 194 + .bDescriptorType = USB_DT_CS_INTERFACE, 195 + .bDescriptorSubtype = UAC_FORMAT_TYPE, 196 + .bFormatType = UAC_FORMAT_TYPE_I, 197 + .bSubframeSize = 2, 198 + .bBitResolution = 16, 199 + .bSamFreqType = 1, 200 + }; 201 + 202 + /* Standard ISO OUT Endpoint Descriptor */ 203 + static struct usb_endpoint_descriptor as_in_ep_desc = { 204 + .bLength = USB_DT_ENDPOINT_AUDIO_SIZE, 205 + .bDescriptorType = USB_DT_ENDPOINT, 206 + .bEndpointAddress = USB_DIR_IN, 207 + .bmAttributes = USB_ENDPOINT_SYNC_ASYNC 208 + | USB_ENDPOINT_XFER_ISOC, 209 + .wMaxPacketSize = cpu_to_le16(UAC1_OUT_EP_MAX_PACKET_SIZE), 210 + .bInterval = 4, 211 + }; 212 + 213 + /* Class-specific AS ISO OUT Endpoint Descriptor */ 214 + static struct uac_iso_endpoint_descriptor as_iso_in_desc = { 215 + .bLength = UAC_ISO_ENDPOINT_DESC_SIZE, 216 + .bDescriptorType = USB_DT_CS_ENDPOINT, 217 + .bDescriptorSubtype = UAC_EP_GENERAL, 218 + .bmAttributes = 1, 219 + .bLockDelayUnits = 0, 220 + .wLockDelay = 0, 220 221 }; 221 222 222 223 static struct usb_descriptor_header *f_audio_desc[] = { 223 224 (struct usb_descriptor_header *)&ac_interface_desc, 224 225 (struct usb_descriptor_header *)&ac_header_desc, 225 226 226 - (struct usb_descriptor_header *)&input_terminal_desc, 227 - (struct usb_descriptor_header *)&output_terminal_desc, 228 - (struct usb_descriptor_header *)&feature_unit_desc, 227 + (struct usb_descriptor_header *)&usb_out_it_desc, 228 + (struct usb_descriptor_header *)&io_out_ot_desc, 229 + (struct usb_descriptor_header *)&io_in_it_desc, 230 + (struct usb_descriptor_header *)&usb_in_ot_desc, 229 231 230 - (struct usb_descriptor_header *)&as_interface_alt_0_desc, 231 - (struct usb_descriptor_header *)&as_interface_alt_1_desc, 232 - (struct usb_descriptor_header *)&as_header_desc, 232 + (struct usb_descriptor_header *)&as_out_interface_alt_0_desc, 233 + (struct usb_descriptor_header *)&as_out_interface_alt_1_desc, 234 + (struct usb_descriptor_header *)&as_out_header_desc, 233 235 234 - (struct usb_descriptor_header *)&as_type_i_desc, 236 + (struct usb_descriptor_header *)&as_out_type_i_desc, 235 237 236 238 (struct usb_descriptor_header *)&as_out_ep_desc, 237 239 (struct usb_descriptor_header *)&as_iso_out_desc, 240 + 241 + (struct usb_descriptor_header *)&as_in_interface_alt_0_desc, 242 + (struct usb_descriptor_header *)&as_in_interface_alt_1_desc, 243 + (struct usb_descriptor_header *)&as_in_header_desc, 244 + 245 + (struct usb_descriptor_header *)&as_in_type_i_desc, 246 + 247 + (struct usb_descriptor_header *)&as_in_ep_desc, 248 + (struct usb_descriptor_header *)&as_iso_in_desc, 238 249 NULL, 239 250 }; 240 251 241 252 enum { 242 253 STR_AC_IF, 243 - STR_INPUT_TERMINAL, 244 - STR_INPUT_TERMINAL_CH_NAMES, 245 - STR_FEAT_DESC_0, 246 - STR_OUTPUT_TERMINAL, 247 - STR_AS_IF_ALT0, 248 - STR_AS_IF_ALT1, 254 + STR_USB_OUT_IT, 255 + STR_USB_OUT_IT_CH_NAMES, 256 + STR_IO_OUT_OT, 257 + STR_IO_IN_IT, 258 + STR_IO_IN_IT_CH_NAMES, 259 + STR_USB_IN_OT, 260 + STR_AS_OUT_IF_ALT0, 261 + STR_AS_OUT_IF_ALT1, 262 + STR_AS_IN_IF_ALT0, 263 + STR_AS_IN_IF_ALT1, 249 264 }; 250 265 251 266 static struct usb_string strings_uac1[] = { 252 267 [STR_AC_IF].s = "AC Interface", 253 - [STR_INPUT_TERMINAL].s = "Input terminal", 254 - [STR_INPUT_TERMINAL_CH_NAMES].s = "Channels", 255 - [STR_FEAT_DESC_0].s = "Volume control & mute", 256 - [STR_OUTPUT_TERMINAL].s = "Output terminal", 257 - [STR_AS_IF_ALT0].s = "AS Interface", 258 - [STR_AS_IF_ALT1].s = "AS Interface", 268 + [STR_USB_OUT_IT].s = "Playback Input terminal", 269 + [STR_USB_OUT_IT_CH_NAMES].s = "Playback Channels", 270 + [STR_IO_OUT_OT].s = "Playback Output terminal", 271 + [STR_IO_IN_IT].s = "Capture Input terminal", 272 + [STR_IO_IN_IT_CH_NAMES].s = "Capture Channels", 273 + [STR_USB_IN_OT].s = "Capture Output terminal", 274 + [STR_AS_OUT_IF_ALT0].s = "Playback Inactive", 275 + [STR_AS_OUT_IF_ALT1].s = "Playback Active", 276 + [STR_AS_IN_IF_ALT0].s = "Capture Inactive", 277 + [STR_AS_IN_IF_ALT1].s = "Capture Active", 259 278 { }, 260 279 }; 261 280 ··· 321 242 /* 322 243 * This function is an ALSA sound card following USB Audio Class Spec 1.0. 323 244 */ 324 - 325 - /*-------------------------------------------------------------------------*/ 326 - struct f_audio_buf { 327 - u8 *buf; 328 - int actual; 329 - struct list_head list; 330 - }; 331 - 332 - static struct f_audio_buf *f_audio_buffer_alloc(int buf_size) 333 - { 334 - struct f_audio_buf *copy_buf; 335 - 336 - copy_buf = kzalloc(sizeof *copy_buf, GFP_ATOMIC); 337 - if (!copy_buf) 338 - return ERR_PTR(-ENOMEM); 339 - 340 - copy_buf->buf = kzalloc(buf_size, GFP_ATOMIC); 341 - if (!copy_buf->buf) { 342 - kfree(copy_buf); 343 - return ERR_PTR(-ENOMEM); 344 - } 345 - 346 - return copy_buf; 347 - } 348 - 349 - static void f_audio_buffer_free(struct f_audio_buf *audio_buf) 350 - { 351 - kfree(audio_buf->buf); 352 - kfree(audio_buf); 353 - } 354 - /*-------------------------------------------------------------------------*/ 355 - 356 - struct f_audio { 357 - struct gaudio card; 358 - 359 - /* endpoints handle full and/or high speeds */ 360 - struct usb_ep *out_ep; 361 - 362 - spinlock_t lock; 363 - struct f_audio_buf *copy_buf; 364 - struct work_struct playback_work; 365 - struct list_head play_queue; 366 - 367 - /* Control Set command */ 368 - struct list_head cs; 369 - u8 set_cmd; 370 - struct usb_audio_control *set_con; 371 - }; 372 - 373 - static inline struct f_audio *func_to_audio(struct usb_function *f) 374 - { 375 - return container_of(f, struct f_audio, card.func); 376 - } 377 - 378 - /*-------------------------------------------------------------------------*/ 379 - 380 - static void f_audio_playback_work(struct work_struct *data) 381 - { 382 - struct f_audio *audio = container_of(data, struct f_audio, 383 - playback_work); 384 - struct f_audio_buf *play_buf; 385 - 386 - spin_lock_irq(&audio->lock); 387 - if (list_empty(&audio->play_queue)) { 388 - spin_unlock_irq(&audio->lock); 389 - return; 390 - } 391 - play_buf = list_first_entry(&audio->play_queue, 392 - struct f_audio_buf, list); 393 - list_del(&play_buf->list); 394 - spin_unlock_irq(&audio->lock); 395 - 396 - u_audio_playback(&audio->card, play_buf->buf, play_buf->actual); 397 - f_audio_buffer_free(play_buf); 398 - } 399 - 400 - static int f_audio_out_ep_complete(struct usb_ep *ep, struct usb_request *req) 401 - { 402 - struct f_audio *audio = req->context; 403 - struct usb_composite_dev *cdev = audio->card.func.config->cdev; 404 - struct f_audio_buf *copy_buf = audio->copy_buf; 405 - struct f_uac1_opts *opts; 406 - int audio_buf_size; 407 - int err; 408 - 409 - opts = container_of(audio->card.func.fi, struct f_uac1_opts, 410 - func_inst); 411 - audio_buf_size = opts->audio_buf_size; 412 - 413 - if (!copy_buf) 414 - return -EINVAL; 415 - 416 - /* Copy buffer is full, add it to the play_queue */ 417 - if (audio_buf_size - copy_buf->actual < req->actual) { 418 - list_add_tail(&copy_buf->list, &audio->play_queue); 419 - schedule_work(&audio->playback_work); 420 - copy_buf = f_audio_buffer_alloc(audio_buf_size); 421 - if (IS_ERR(copy_buf)) 422 - return -ENOMEM; 423 - } 424 - 425 - memcpy(copy_buf->buf + copy_buf->actual, req->buf, req->actual); 426 - copy_buf->actual += req->actual; 427 - audio->copy_buf = copy_buf; 428 - 429 - err = usb_ep_queue(ep, req, GFP_ATOMIC); 430 - if (err) 431 - ERROR(cdev, "%s queue req: %d\n", ep->name, err); 432 - 433 - return 0; 434 - 435 - } 436 - 437 - static void f_audio_complete(struct usb_ep *ep, struct usb_request *req) 438 - { 439 - struct f_audio *audio = req->context; 440 - int status = req->status; 441 - u32 data = 0; 442 - struct usb_ep *out_ep = audio->out_ep; 443 - 444 - switch (status) { 445 - 446 - case 0: /* normal completion? */ 447 - if (ep == out_ep) 448 - f_audio_out_ep_complete(ep, req); 449 - else if (audio->set_con) { 450 - memcpy(&data, req->buf, req->length); 451 - audio->set_con->set(audio->set_con, audio->set_cmd, 452 - le16_to_cpu(data)); 453 - audio->set_con = NULL; 454 - } 455 - break; 456 - default: 457 - break; 458 - } 459 - } 460 - 461 - static int audio_set_intf_req(struct usb_function *f, 462 - const struct usb_ctrlrequest *ctrl) 463 - { 464 - struct f_audio *audio = func_to_audio(f); 465 - struct usb_composite_dev *cdev = f->config->cdev; 466 - struct usb_request *req = cdev->req; 467 - u8 id = ((le16_to_cpu(ctrl->wIndex) >> 8) & 0xFF); 468 - u16 len = le16_to_cpu(ctrl->wLength); 469 - u16 w_value = le16_to_cpu(ctrl->wValue); 470 - u8 con_sel = (w_value >> 8) & 0xFF; 471 - u8 cmd = (ctrl->bRequest & 0x0F); 472 - struct usb_audio_control_selector *cs; 473 - struct usb_audio_control *con; 474 - 475 - DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, entity %d\n", 476 - ctrl->bRequest, w_value, len, id); 477 - 478 - list_for_each_entry(cs, &audio->cs, list) { 479 - if (cs->id == id) { 480 - list_for_each_entry(con, &cs->control, list) { 481 - if (con->type == con_sel) { 482 - audio->set_con = con; 483 - break; 484 - } 485 - } 486 - break; 487 - } 488 - } 489 - 490 - audio->set_cmd = cmd; 491 - req->context = audio; 492 - req->complete = f_audio_complete; 493 - 494 - return len; 495 - } 496 - 497 - static int audio_get_intf_req(struct usb_function *f, 498 - const struct usb_ctrlrequest *ctrl) 499 - { 500 - struct f_audio *audio = func_to_audio(f); 501 - struct usb_composite_dev *cdev = f->config->cdev; 502 - struct usb_request *req = cdev->req; 503 - int value = -EOPNOTSUPP; 504 - u8 id = ((le16_to_cpu(ctrl->wIndex) >> 8) & 0xFF); 505 - u16 len = le16_to_cpu(ctrl->wLength); 506 - u16 w_value = le16_to_cpu(ctrl->wValue); 507 - u8 con_sel = (w_value >> 8) & 0xFF; 508 - u8 cmd = (ctrl->bRequest & 0x0F); 509 - struct usb_audio_control_selector *cs; 510 - struct usb_audio_control *con; 511 - 512 - DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, entity %d\n", 513 - ctrl->bRequest, w_value, len, id); 514 - 515 - list_for_each_entry(cs, &audio->cs, list) { 516 - if (cs->id == id) { 517 - list_for_each_entry(con, &cs->control, list) { 518 - if (con->type == con_sel && con->get) { 519 - value = con->get(con, cmd); 520 - break; 521 - } 522 - } 523 - break; 524 - } 525 - } 526 - 527 - req->context = audio; 528 - req->complete = f_audio_complete; 529 - len = min_t(size_t, sizeof(value), len); 530 - memcpy(req->buf, &value, len); 531 - 532 - return len; 533 - } 534 245 535 246 static int audio_set_endpoint_req(struct usb_function *f, 536 247 const struct usb_ctrlrequest *ctrl) ··· 400 531 * activation uses set_alt(). 401 532 */ 402 533 switch (ctrl->bRequestType) { 403 - case USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE: 404 - value = audio_set_intf_req(f, ctrl); 405 - break; 406 - 407 - case USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE: 408 - value = audio_get_intf_req(f, ctrl); 409 - break; 410 - 411 534 case USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_ENDPOINT: 412 535 value = audio_set_endpoint_req(f, ctrl); 413 536 break; ··· 432 571 433 572 static int f_audio_set_alt(struct usb_function *f, unsigned intf, unsigned alt) 434 573 { 435 - struct f_audio *audio = func_to_audio(f); 436 574 struct usb_composite_dev *cdev = f->config->cdev; 437 - struct usb_ep *out_ep = audio->out_ep; 438 - struct usb_request *req; 439 - struct f_uac1_opts *opts; 440 - int req_buf_size, req_count, audio_buf_size; 441 - int i = 0, err = 0; 575 + struct usb_gadget *gadget = cdev->gadget; 576 + struct device *dev = &gadget->dev; 577 + struct f_uac1 *uac1 = func_to_uac1(f); 578 + int ret = 0; 442 579 443 - DBG(cdev, "intf %d, alt %d\n", intf, alt); 444 - 445 - opts = container_of(f->fi, struct f_uac1_opts, func_inst); 446 - req_buf_size = opts->req_buf_size; 447 - req_count = opts->req_count; 448 - audio_buf_size = opts->audio_buf_size; 449 - 450 - if (intf == 1) { 451 - if (alt == 1) { 452 - err = config_ep_by_speed(cdev->gadget, f, out_ep); 453 - if (err) 454 - return err; 455 - 456 - usb_ep_enable(out_ep); 457 - audio->copy_buf = f_audio_buffer_alloc(audio_buf_size); 458 - if (IS_ERR(audio->copy_buf)) 459 - return -ENOMEM; 460 - 461 - /* 462 - * allocate a bunch of read buffers 463 - * and queue them all at once. 464 - */ 465 - for (i = 0; i < req_count && err == 0; i++) { 466 - req = usb_ep_alloc_request(out_ep, GFP_ATOMIC); 467 - if (req) { 468 - req->buf = kzalloc(req_buf_size, 469 - GFP_ATOMIC); 470 - if (req->buf) { 471 - req->length = req_buf_size; 472 - req->context = audio; 473 - req->complete = 474 - f_audio_complete; 475 - err = usb_ep_queue(out_ep, 476 - req, GFP_ATOMIC); 477 - if (err) 478 - ERROR(cdev, 479 - "%s queue req: %d\n", 480 - out_ep->name, err); 481 - } else 482 - err = -ENOMEM; 483 - } else 484 - err = -ENOMEM; 485 - } 486 - 487 - } else { 488 - struct f_audio_buf *copy_buf = audio->copy_buf; 489 - if (copy_buf) { 490 - list_add_tail(&copy_buf->list, 491 - &audio->play_queue); 492 - schedule_work(&audio->playback_work); 493 - } 494 - } 580 + /* No i/f has more than 2 alt settings */ 581 + if (alt > 1) { 582 + dev_err(dev, "%s:%d Error!\n", __func__, __LINE__); 583 + return -EINVAL; 495 584 } 496 585 497 - return err; 586 + if (intf == uac1->ac_intf) { 587 + /* Control I/f has only 1 AltSetting - 0 */ 588 + if (alt) { 589 + dev_err(dev, "%s:%d Error!\n", __func__, __LINE__); 590 + return -EINVAL; 591 + } 592 + return 0; 593 + } 594 + 595 + if (intf == uac1->as_out_intf) { 596 + uac1->as_out_alt = alt; 597 + 598 + if (alt) 599 + ret = u_audio_start_capture(&uac1->g_audio); 600 + else 601 + u_audio_stop_capture(&uac1->g_audio); 602 + } else if (intf == uac1->as_in_intf) { 603 + uac1->as_in_alt = alt; 604 + 605 + if (alt) 606 + ret = u_audio_start_playback(&uac1->g_audio); 607 + else 608 + u_audio_stop_playback(&uac1->g_audio); 609 + } else { 610 + dev_err(dev, "%s:%d Error!\n", __func__, __LINE__); 611 + return -EINVAL; 612 + } 613 + 614 + return ret; 498 615 } 616 + 617 + static int f_audio_get_alt(struct usb_function *f, unsigned intf) 618 + { 619 + struct usb_composite_dev *cdev = f->config->cdev; 620 + struct usb_gadget *gadget = cdev->gadget; 621 + struct device *dev = &gadget->dev; 622 + struct f_uac1 *uac1 = func_to_uac1(f); 623 + 624 + if (intf == uac1->ac_intf) 625 + return uac1->ac_alt; 626 + else if (intf == uac1->as_out_intf) 627 + return uac1->as_out_alt; 628 + else if (intf == uac1->as_in_intf) 629 + return uac1->as_in_alt; 630 + else 631 + dev_err(dev, "%s:%d Invalid Interface %d!\n", 632 + __func__, __LINE__, intf); 633 + 634 + return -EINVAL; 635 + } 636 + 499 637 500 638 static void f_audio_disable(struct usb_function *f) 501 639 { 502 - return; 640 + struct f_uac1 *uac1 = func_to_uac1(f); 641 + 642 + uac1->as_out_alt = 0; 643 + uac1->as_in_alt = 0; 644 + 645 + u_audio_stop_capture(&uac1->g_audio); 503 646 } 504 647 505 648 /*-------------------------------------------------------------------------*/ 506 649 507 - static void f_audio_build_desc(struct f_audio *audio) 508 - { 509 - struct gaudio *card = &audio->card; 510 - u8 *sam_freq; 511 - int rate; 512 - 513 - /* Set channel numbers */ 514 - input_terminal_desc.bNrChannels = u_audio_get_playback_channels(card); 515 - as_type_i_desc.bNrChannels = u_audio_get_playback_channels(card); 516 - 517 - /* Set sample rates */ 518 - rate = u_audio_get_playback_rate(card); 519 - sam_freq = as_type_i_desc.tSamFreq[0]; 520 - memcpy(sam_freq, &rate, 3); 521 - 522 - /* Todo: Set Sample bits and other parameters */ 523 - 524 - return; 525 - } 526 - 527 650 /* audio function driver setup/binding */ 528 - static int 529 - f_audio_bind(struct usb_configuration *c, struct usb_function *f) 651 + static int f_audio_bind(struct usb_configuration *c, struct usb_function *f) 530 652 { 531 - struct usb_composite_dev *cdev = c->cdev; 532 - struct f_audio *audio = func_to_audio(f); 533 - struct usb_string *us; 534 - int status; 535 - struct usb_ep *ep = NULL; 536 - struct f_uac1_opts *audio_opts; 653 + struct usb_composite_dev *cdev = c->cdev; 654 + struct usb_gadget *gadget = cdev->gadget; 655 + struct f_uac1 *uac1 = func_to_uac1(f); 656 + struct g_audio *audio = func_to_g_audio(f); 657 + struct f_uac1_opts *audio_opts; 658 + struct usb_ep *ep = NULL; 659 + struct usb_string *us; 660 + u8 *sam_freq; 661 + int rate; 662 + int status; 537 663 538 664 audio_opts = container_of(f->fi, struct f_uac1_opts, func_inst); 539 - audio->card.gadget = c->cdev->gadget; 540 - /* set up ASLA audio devices */ 541 - if (!audio_opts->bound) { 542 - status = gaudio_setup(&audio->card); 543 - if (status < 0) 544 - return status; 545 - audio_opts->bound = true; 546 - } 665 + 547 666 us = usb_gstrings_attach(cdev, uac1_strings, ARRAY_SIZE(strings_uac1)); 548 667 if (IS_ERR(us)) 549 668 return PTR_ERR(us); 550 669 ac_interface_desc.iInterface = us[STR_AC_IF].id; 551 - input_terminal_desc.iTerminal = us[STR_INPUT_TERMINAL].id; 552 - input_terminal_desc.iChannelNames = us[STR_INPUT_TERMINAL_CH_NAMES].id; 553 - feature_unit_desc.iFeature = us[STR_FEAT_DESC_0].id; 554 - output_terminal_desc.iTerminal = us[STR_OUTPUT_TERMINAL].id; 555 - as_interface_alt_0_desc.iInterface = us[STR_AS_IF_ALT0].id; 556 - as_interface_alt_1_desc.iInterface = us[STR_AS_IF_ALT1].id; 670 + usb_out_it_desc.iTerminal = us[STR_USB_OUT_IT].id; 671 + usb_out_it_desc.iChannelNames = us[STR_USB_OUT_IT_CH_NAMES].id; 672 + io_out_ot_desc.iTerminal = us[STR_IO_OUT_OT].id; 673 + as_out_interface_alt_0_desc.iInterface = us[STR_AS_OUT_IF_ALT0].id; 674 + as_out_interface_alt_1_desc.iInterface = us[STR_AS_OUT_IF_ALT1].id; 675 + io_in_it_desc.iTerminal = us[STR_IO_IN_IT].id; 676 + io_in_it_desc.iChannelNames = us[STR_IO_IN_IT_CH_NAMES].id; 677 + usb_in_ot_desc.iTerminal = us[STR_USB_IN_OT].id; 678 + as_in_interface_alt_0_desc.iInterface = us[STR_AS_IN_IF_ALT0].id; 679 + as_in_interface_alt_1_desc.iInterface = us[STR_AS_IN_IF_ALT1].id; 557 680 681 + /* Set channel numbers */ 682 + usb_out_it_desc.bNrChannels = num_channels(audio_opts->c_chmask); 683 + usb_out_it_desc.wChannelConfig = cpu_to_le16(audio_opts->c_chmask); 684 + as_out_type_i_desc.bNrChannels = num_channels(audio_opts->c_chmask); 685 + as_out_type_i_desc.bSubframeSize = audio_opts->c_ssize; 686 + as_out_type_i_desc.bBitResolution = audio_opts->c_ssize * 8; 687 + io_in_it_desc.bNrChannels = num_channels(audio_opts->p_chmask); 688 + io_in_it_desc.wChannelConfig = cpu_to_le16(audio_opts->p_chmask); 689 + as_in_type_i_desc.bNrChannels = num_channels(audio_opts->p_chmask); 690 + as_in_type_i_desc.bSubframeSize = audio_opts->p_ssize; 691 + as_in_type_i_desc.bBitResolution = audio_opts->p_ssize * 8; 558 692 559 - f_audio_build_desc(audio); 693 + /* Set sample rates */ 694 + rate = audio_opts->c_srate; 695 + sam_freq = as_out_type_i_desc.tSamFreq[0]; 696 + memcpy(sam_freq, &rate, 3); 697 + rate = audio_opts->p_srate; 698 + sam_freq = as_in_type_i_desc.tSamFreq[0]; 699 + memcpy(sam_freq, &rate, 3); 560 700 561 701 /* allocate instance-specific interface IDs, and patch descriptors */ 562 702 status = usb_interface_id(c, f); 563 703 if (status < 0) 564 704 goto fail; 565 705 ac_interface_desc.bInterfaceNumber = status; 706 + uac1->ac_intf = status; 707 + uac1->ac_alt = 0; 566 708 567 709 status = usb_interface_id(c, f); 568 710 if (status < 0) 569 711 goto fail; 570 - as_interface_alt_0_desc.bInterfaceNumber = status; 571 - as_interface_alt_1_desc.bInterfaceNumber = status; 712 + as_out_interface_alt_0_desc.bInterfaceNumber = status; 713 + as_out_interface_alt_1_desc.bInterfaceNumber = status; 714 + uac1->as_out_intf = status; 715 + uac1->as_out_alt = 0; 716 + 717 + status = usb_interface_id(c, f); 718 + if (status < 0) 719 + goto fail; 720 + as_in_interface_alt_0_desc.bInterfaceNumber = status; 721 + as_in_interface_alt_1_desc.bInterfaceNumber = status; 722 + uac1->as_in_intf = status; 723 + uac1->as_in_alt = 0; 724 + 725 + audio->gadget = gadget; 572 726 573 727 status = -ENODEV; 574 728 ··· 594 718 audio->out_ep = ep; 595 719 audio->out_ep->desc = &as_out_ep_desc; 596 720 597 - status = -ENOMEM; 721 + ep = usb_ep_autoconfig(cdev->gadget, &as_in_ep_desc); 722 + if (!ep) 723 + goto fail; 724 + audio->in_ep = ep; 725 + audio->in_ep->desc = &as_in_ep_desc; 598 726 599 727 /* copy descriptors, and track endpoint copies */ 600 728 status = usb_assign_descriptors(f, f_audio_desc, f_audio_desc, NULL, 601 729 NULL); 602 730 if (status) 603 731 goto fail; 732 + 733 + audio->out_ep_maxpsize = as_out_ep_desc.wMaxPacketSize; 734 + audio->in_ep_maxpsize = as_in_ep_desc.wMaxPacketSize; 735 + audio->params.c_chmask = audio_opts->c_chmask; 736 + audio->params.c_srate = audio_opts->c_srate; 737 + audio->params.c_ssize = audio_opts->c_ssize; 738 + audio->params.p_chmask = audio_opts->p_chmask; 739 + audio->params.p_srate = audio_opts->p_srate; 740 + audio->params.p_ssize = audio_opts->p_ssize; 741 + audio->params.req_number = audio_opts->req_number; 742 + 743 + status = g_audio_setup(audio, "UAC1_PCM", "UAC1_Gadget"); 744 + if (status) 745 + goto err_card_register; 746 + 604 747 return 0; 605 748 749 + err_card_register: 750 + usb_free_all_descriptors(f); 606 751 fail: 607 - gaudio_cleanup(&audio->card); 608 752 return status; 609 753 } 610 754 611 755 /*-------------------------------------------------------------------------*/ 612 - 613 - static int generic_set_cmd(struct usb_audio_control *con, u8 cmd, int value) 614 - { 615 - con->data[cmd] = value; 616 - 617 - return 0; 618 - } 619 - 620 - static int generic_get_cmd(struct usb_audio_control *con, u8 cmd) 621 - { 622 - return con->data[cmd]; 623 - } 624 - 625 - /* Todo: add more control selecotor dynamically */ 626 - static int control_selector_init(struct f_audio *audio) 627 - { 628 - INIT_LIST_HEAD(&audio->cs); 629 - list_add(&feature_unit.list, &audio->cs); 630 - 631 - INIT_LIST_HEAD(&feature_unit.control); 632 - list_add(&mute_control.list, &feature_unit.control); 633 - list_add(&volume_control.list, &feature_unit.control); 634 - 635 - volume_control.data[UAC__CUR] = 0xffc0; 636 - volume_control.data[UAC__MIN] = 0xe3a0; 637 - volume_control.data[UAC__MAX] = 0xfff0; 638 - volume_control.data[UAC__RES] = 0x0030; 639 - 640 - return 0; 641 - } 642 756 643 757 static inline struct f_uac1_opts *to_f_uac1_opts(struct config_item *item) 644 758 { ··· 647 781 .release = f_uac1_attr_release, 648 782 }; 649 783 650 - #define UAC1_INT_ATTRIBUTE(name) \ 651 - static ssize_t f_uac1_opts_##name##_show(struct config_item *item, \ 652 - char *page) \ 784 + #define UAC1_ATTRIBUTE(name) \ 785 + static ssize_t f_uac1_opts_##name##_show( \ 786 + struct config_item *item, \ 787 + char *page) \ 653 788 { \ 654 789 struct f_uac1_opts *opts = to_f_uac1_opts(item); \ 655 790 int result; \ ··· 662 795 return result; \ 663 796 } \ 664 797 \ 665 - static ssize_t f_uac1_opts_##name##_store(struct config_item *item, \ 798 + static ssize_t f_uac1_opts_##name##_store( \ 799 + struct config_item *item, \ 666 800 const char *page, size_t len) \ 667 801 { \ 668 802 struct f_uac1_opts *opts = to_f_uac1_opts(item); \ ··· 690 822 \ 691 823 CONFIGFS_ATTR(f_uac1_opts_, name) 692 824 693 - UAC1_INT_ATTRIBUTE(req_buf_size); 694 - UAC1_INT_ATTRIBUTE(req_count); 695 - UAC1_INT_ATTRIBUTE(audio_buf_size); 696 - 697 - #define UAC1_STR_ATTRIBUTE(name) \ 698 - static ssize_t f_uac1_opts_##name##_show(struct config_item *item, \ 699 - char *page) \ 700 - { \ 701 - struct f_uac1_opts *opts = to_f_uac1_opts(item); \ 702 - int result; \ 703 - \ 704 - mutex_lock(&opts->lock); \ 705 - result = sprintf(page, "%s\n", opts->name); \ 706 - mutex_unlock(&opts->lock); \ 707 - \ 708 - return result; \ 709 - } \ 710 - \ 711 - static ssize_t f_uac1_opts_##name##_store(struct config_item *item, \ 712 - const char *page, size_t len) \ 713 - { \ 714 - struct f_uac1_opts *opts = to_f_uac1_opts(item); \ 715 - int ret = -EBUSY; \ 716 - char *tmp; \ 717 - \ 718 - mutex_lock(&opts->lock); \ 719 - if (opts->refcnt) \ 720 - goto end; \ 721 - \ 722 - tmp = kstrndup(page, len, GFP_KERNEL); \ 723 - if (tmp) { \ 724 - ret = -ENOMEM; \ 725 - goto end; \ 726 - } \ 727 - if (opts->name##_alloc) \ 728 - kfree(opts->name); \ 729 - opts->name##_alloc = true; \ 730 - opts->name = tmp; \ 731 - ret = len; \ 732 - \ 733 - end: \ 734 - mutex_unlock(&opts->lock); \ 735 - return ret; \ 736 - } \ 737 - \ 738 - CONFIGFS_ATTR(f_uac1_opts_, name) 739 - 740 - UAC1_STR_ATTRIBUTE(fn_play); 741 - UAC1_STR_ATTRIBUTE(fn_cap); 742 - UAC1_STR_ATTRIBUTE(fn_cntl); 825 + UAC1_ATTRIBUTE(c_chmask); 826 + UAC1_ATTRIBUTE(c_srate); 827 + UAC1_ATTRIBUTE(c_ssize); 828 + UAC1_ATTRIBUTE(p_chmask); 829 + UAC1_ATTRIBUTE(p_srate); 830 + UAC1_ATTRIBUTE(p_ssize); 831 + UAC1_ATTRIBUTE(req_number); 743 832 744 833 static struct configfs_attribute *f_uac1_attrs[] = { 745 - &f_uac1_opts_attr_req_buf_size, 746 - &f_uac1_opts_attr_req_count, 747 - &f_uac1_opts_attr_audio_buf_size, 748 - &f_uac1_opts_attr_fn_play, 749 - &f_uac1_opts_attr_fn_cap, 750 - &f_uac1_opts_attr_fn_cntl, 834 + &f_uac1_opts_attr_c_chmask, 835 + &f_uac1_opts_attr_c_srate, 836 + &f_uac1_opts_attr_c_ssize, 837 + &f_uac1_opts_attr_p_chmask, 838 + &f_uac1_opts_attr_p_srate, 839 + &f_uac1_opts_attr_p_ssize, 840 + &f_uac1_opts_attr_req_number, 751 841 NULL, 752 842 }; 753 843 ··· 720 894 struct f_uac1_opts *opts; 721 895 722 896 opts = container_of(f, struct f_uac1_opts, func_inst); 723 - if (opts->fn_play_alloc) 724 - kfree(opts->fn_play); 725 - if (opts->fn_cap_alloc) 726 - kfree(opts->fn_cap); 727 - if (opts->fn_cntl_alloc) 728 - kfree(opts->fn_cntl); 729 897 kfree(opts); 730 898 } 731 899 ··· 737 917 config_group_init_type_name(&opts->func_inst.group, "", 738 918 &f_uac1_func_type); 739 919 740 - opts->req_buf_size = UAC1_OUT_EP_MAX_PACKET_SIZE; 741 - opts->req_count = UAC1_REQ_COUNT; 742 - opts->audio_buf_size = UAC1_AUDIO_BUF_SIZE; 743 - opts->fn_play = FILE_PCM_PLAYBACK; 744 - opts->fn_cap = FILE_PCM_CAPTURE; 745 - opts->fn_cntl = FILE_CONTROL; 920 + opts->c_chmask = UAC1_DEF_CCHMASK; 921 + opts->c_srate = UAC1_DEF_CSRATE; 922 + opts->c_ssize = UAC1_DEF_CSSIZE; 923 + opts->p_chmask = UAC1_DEF_PCHMASK; 924 + opts->p_srate = UAC1_DEF_PSRATE; 925 + opts->p_ssize = UAC1_DEF_PSSIZE; 926 + opts->req_number = UAC1_DEF_REQ_NUM; 746 927 return &opts->func_inst; 747 928 } 748 929 749 930 static void f_audio_free(struct usb_function *f) 750 931 { 751 - struct f_audio *audio = func_to_audio(f); 932 + struct g_audio *audio; 752 933 struct f_uac1_opts *opts; 753 934 754 - gaudio_cleanup(&audio->card); 935 + audio = func_to_g_audio(f); 755 936 opts = container_of(f->fi, struct f_uac1_opts, func_inst); 756 937 kfree(audio); 757 938 mutex_lock(&opts->lock); ··· 762 941 763 942 static void f_audio_unbind(struct usb_configuration *c, struct usb_function *f) 764 943 { 944 + struct g_audio *audio = func_to_g_audio(f); 945 + 946 + g_audio_cleanup(audio); 765 947 usb_free_all_descriptors(f); 948 + 949 + audio->gadget = NULL; 766 950 } 767 951 768 952 static struct usb_function *f_audio_alloc(struct usb_function_instance *fi) 769 953 { 770 - struct f_audio *audio; 954 + struct f_uac1 *uac1; 771 955 struct f_uac1_opts *opts; 772 956 773 957 /* allocate and initialize one new instance */ 774 - audio = kzalloc(sizeof(*audio), GFP_KERNEL); 775 - if (!audio) 958 + uac1 = kzalloc(sizeof(*uac1), GFP_KERNEL); 959 + if (!uac1) 776 960 return ERR_PTR(-ENOMEM); 777 - 778 - audio->card.func.name = "g_audio"; 779 961 780 962 opts = container_of(fi, struct f_uac1_opts, func_inst); 781 963 mutex_lock(&opts->lock); 782 964 ++opts->refcnt; 783 965 mutex_unlock(&opts->lock); 784 - INIT_LIST_HEAD(&audio->play_queue); 785 - spin_lock_init(&audio->lock); 786 966 787 - audio->card.func.bind = f_audio_bind; 788 - audio->card.func.unbind = f_audio_unbind; 789 - audio->card.func.set_alt = f_audio_set_alt; 790 - audio->card.func.setup = f_audio_setup; 791 - audio->card.func.disable = f_audio_disable; 792 - audio->card.func.free_func = f_audio_free; 967 + uac1->g_audio.func.name = "uac1_func"; 968 + uac1->g_audio.func.bind = f_audio_bind; 969 + uac1->g_audio.func.unbind = f_audio_unbind; 970 + uac1->g_audio.func.set_alt = f_audio_set_alt; 971 + uac1->g_audio.func.get_alt = f_audio_get_alt; 972 + uac1->g_audio.func.setup = f_audio_setup; 973 + uac1->g_audio.func.disable = f_audio_disable; 974 + uac1->g_audio.func.free_func = f_audio_free; 793 975 794 - control_selector_init(audio); 795 - 796 - INIT_WORK(&audio->playback_work, f_audio_playback_work); 797 - 798 - return &audio->card.func; 976 + return &uac1->g_audio.func; 799 977 } 800 978 801 979 DECLARE_USB_FUNCTION_INIT(uac1, f_audio_alloc_inst, f_audio_alloc); 802 980 MODULE_LICENSE("GPL"); 803 - MODULE_AUTHOR("Bryan Wu"); 981 + MODULE_AUTHOR("Ruslan Bilovol");
+1021
drivers/usb/gadget/function/f_uac1_legacy.c
··· 1 + /* 2 + * f_audio.c -- USB Audio class function driver 3 + * 4 + * Copyright (C) 2008 Bryan Wu <cooloney@kernel.org> 5 + * Copyright (C) 2008 Analog Devices, Inc 6 + * 7 + * Enter bugs at http://blackfin.uclinux.org/ 8 + * 9 + * Licensed under the GPL-2 or later. 10 + */ 11 + 12 + #include <linux/slab.h> 13 + #include <linux/kernel.h> 14 + #include <linux/module.h> 15 + #include <linux/device.h> 16 + #include <linux/atomic.h> 17 + 18 + #include "u_uac1_legacy.h" 19 + 20 + static int generic_set_cmd(struct usb_audio_control *con, u8 cmd, int value); 21 + static int generic_get_cmd(struct usb_audio_control *con, u8 cmd); 22 + 23 + /* 24 + * DESCRIPTORS ... most are static, but strings and full 25 + * configuration descriptors are built on demand. 26 + */ 27 + 28 + /* 29 + * We have two interfaces- AudioControl and AudioStreaming 30 + * TODO: only supcard playback currently 31 + */ 32 + #define F_AUDIO_AC_INTERFACE 0 33 + #define F_AUDIO_AS_INTERFACE 1 34 + #define F_AUDIO_NUM_INTERFACES 1 35 + 36 + /* B.3.1 Standard AC Interface Descriptor */ 37 + static struct usb_interface_descriptor ac_interface_desc = { 38 + .bLength = USB_DT_INTERFACE_SIZE, 39 + .bDescriptorType = USB_DT_INTERFACE, 40 + .bNumEndpoints = 0, 41 + .bInterfaceClass = USB_CLASS_AUDIO, 42 + .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL, 43 + }; 44 + 45 + /* 46 + * The number of AudioStreaming and MIDIStreaming interfaces 47 + * in the Audio Interface Collection 48 + */ 49 + DECLARE_UAC_AC_HEADER_DESCRIPTOR(1); 50 + 51 + #define UAC_DT_AC_HEADER_LENGTH UAC_DT_AC_HEADER_SIZE(F_AUDIO_NUM_INTERFACES) 52 + /* 1 input terminal, 1 output terminal and 1 feature unit */ 53 + #define UAC_DT_TOTAL_LENGTH (UAC_DT_AC_HEADER_LENGTH + UAC_DT_INPUT_TERMINAL_SIZE \ 54 + + UAC_DT_OUTPUT_TERMINAL_SIZE + UAC_DT_FEATURE_UNIT_SIZE(0)) 55 + /* B.3.2 Class-Specific AC Interface Descriptor */ 56 + static struct uac1_ac_header_descriptor_1 ac_header_desc = { 57 + .bLength = UAC_DT_AC_HEADER_LENGTH, 58 + .bDescriptorType = USB_DT_CS_INTERFACE, 59 + .bDescriptorSubtype = UAC_HEADER, 60 + .bcdADC = __constant_cpu_to_le16(0x0100), 61 + .wTotalLength = __constant_cpu_to_le16(UAC_DT_TOTAL_LENGTH), 62 + .bInCollection = F_AUDIO_NUM_INTERFACES, 63 + .baInterfaceNr = { 64 + /* Interface number of the first AudioStream interface */ 65 + [0] = 1, 66 + } 67 + }; 68 + 69 + #define INPUT_TERMINAL_ID 1 70 + static struct uac_input_terminal_descriptor input_terminal_desc = { 71 + .bLength = UAC_DT_INPUT_TERMINAL_SIZE, 72 + .bDescriptorType = USB_DT_CS_INTERFACE, 73 + .bDescriptorSubtype = UAC_INPUT_TERMINAL, 74 + .bTerminalID = INPUT_TERMINAL_ID, 75 + .wTerminalType = UAC_TERMINAL_STREAMING, 76 + .bAssocTerminal = 0, 77 + .wChannelConfig = 0x3, 78 + }; 79 + 80 + DECLARE_UAC_FEATURE_UNIT_DESCRIPTOR(0); 81 + 82 + #define FEATURE_UNIT_ID 2 83 + static struct uac_feature_unit_descriptor_0 feature_unit_desc = { 84 + .bLength = UAC_DT_FEATURE_UNIT_SIZE(0), 85 + .bDescriptorType = USB_DT_CS_INTERFACE, 86 + .bDescriptorSubtype = UAC_FEATURE_UNIT, 87 + .bUnitID = FEATURE_UNIT_ID, 88 + .bSourceID = INPUT_TERMINAL_ID, 89 + .bControlSize = 2, 90 + .bmaControls[0] = (UAC_FU_MUTE | UAC_FU_VOLUME), 91 + }; 92 + 93 + static struct usb_audio_control mute_control = { 94 + .list = LIST_HEAD_INIT(mute_control.list), 95 + .name = "Mute Control", 96 + .type = UAC_FU_MUTE, 97 + /* Todo: add real Mute control code */ 98 + .set = generic_set_cmd, 99 + .get = generic_get_cmd, 100 + }; 101 + 102 + static struct usb_audio_control volume_control = { 103 + .list = LIST_HEAD_INIT(volume_control.list), 104 + .name = "Volume Control", 105 + .type = UAC_FU_VOLUME, 106 + /* Todo: add real Volume control code */ 107 + .set = generic_set_cmd, 108 + .get = generic_get_cmd, 109 + }; 110 + 111 + static struct usb_audio_control_selector feature_unit = { 112 + .list = LIST_HEAD_INIT(feature_unit.list), 113 + .id = FEATURE_UNIT_ID, 114 + .name = "Mute & Volume Control", 115 + .type = UAC_FEATURE_UNIT, 116 + .desc = (struct usb_descriptor_header *)&feature_unit_desc, 117 + }; 118 + 119 + #define OUTPUT_TERMINAL_ID 3 120 + static struct uac1_output_terminal_descriptor output_terminal_desc = { 121 + .bLength = UAC_DT_OUTPUT_TERMINAL_SIZE, 122 + .bDescriptorType = USB_DT_CS_INTERFACE, 123 + .bDescriptorSubtype = UAC_OUTPUT_TERMINAL, 124 + .bTerminalID = OUTPUT_TERMINAL_ID, 125 + .wTerminalType = UAC_OUTPUT_TERMINAL_SPEAKER, 126 + .bAssocTerminal = FEATURE_UNIT_ID, 127 + .bSourceID = FEATURE_UNIT_ID, 128 + }; 129 + 130 + /* B.4.1 Standard AS Interface Descriptor */ 131 + static struct usb_interface_descriptor as_interface_alt_0_desc = { 132 + .bLength = USB_DT_INTERFACE_SIZE, 133 + .bDescriptorType = USB_DT_INTERFACE, 134 + .bAlternateSetting = 0, 135 + .bNumEndpoints = 0, 136 + .bInterfaceClass = USB_CLASS_AUDIO, 137 + .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING, 138 + }; 139 + 140 + static struct usb_interface_descriptor as_interface_alt_1_desc = { 141 + .bLength = USB_DT_INTERFACE_SIZE, 142 + .bDescriptorType = USB_DT_INTERFACE, 143 + .bAlternateSetting = 1, 144 + .bNumEndpoints = 1, 145 + .bInterfaceClass = USB_CLASS_AUDIO, 146 + .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING, 147 + }; 148 + 149 + /* B.4.2 Class-Specific AS Interface Descriptor */ 150 + static struct uac1_as_header_descriptor as_header_desc = { 151 + .bLength = UAC_DT_AS_HEADER_SIZE, 152 + .bDescriptorType = USB_DT_CS_INTERFACE, 153 + .bDescriptorSubtype = UAC_AS_GENERAL, 154 + .bTerminalLink = INPUT_TERMINAL_ID, 155 + .bDelay = 1, 156 + .wFormatTag = UAC_FORMAT_TYPE_I_PCM, 157 + }; 158 + 159 + DECLARE_UAC_FORMAT_TYPE_I_DISCRETE_DESC(1); 160 + 161 + static struct uac_format_type_i_discrete_descriptor_1 as_type_i_desc = { 162 + .bLength = UAC_FORMAT_TYPE_I_DISCRETE_DESC_SIZE(1), 163 + .bDescriptorType = USB_DT_CS_INTERFACE, 164 + .bDescriptorSubtype = UAC_FORMAT_TYPE, 165 + .bFormatType = UAC_FORMAT_TYPE_I, 166 + .bSubframeSize = 2, 167 + .bBitResolution = 16, 168 + .bSamFreqType = 1, 169 + }; 170 + 171 + /* Standard ISO OUT Endpoint Descriptor */ 172 + static struct usb_endpoint_descriptor as_out_ep_desc = { 173 + .bLength = USB_DT_ENDPOINT_AUDIO_SIZE, 174 + .bDescriptorType = USB_DT_ENDPOINT, 175 + .bEndpointAddress = USB_DIR_OUT, 176 + .bmAttributes = USB_ENDPOINT_SYNC_ADAPTIVE 177 + | USB_ENDPOINT_XFER_ISOC, 178 + .wMaxPacketSize = cpu_to_le16(UAC1_OUT_EP_MAX_PACKET_SIZE), 179 + .bInterval = 4, 180 + }; 181 + 182 + /* Class-specific AS ISO OUT Endpoint Descriptor */ 183 + static struct uac_iso_endpoint_descriptor as_iso_out_desc = { 184 + .bLength = UAC_ISO_ENDPOINT_DESC_SIZE, 185 + .bDescriptorType = USB_DT_CS_ENDPOINT, 186 + .bDescriptorSubtype = UAC_EP_GENERAL, 187 + .bmAttributes = 1, 188 + .bLockDelayUnits = 1, 189 + .wLockDelay = __constant_cpu_to_le16(1), 190 + }; 191 + 192 + static struct usb_descriptor_header *f_audio_desc[] = { 193 + (struct usb_descriptor_header *)&ac_interface_desc, 194 + (struct usb_descriptor_header *)&ac_header_desc, 195 + 196 + (struct usb_descriptor_header *)&input_terminal_desc, 197 + (struct usb_descriptor_header *)&output_terminal_desc, 198 + (struct usb_descriptor_header *)&feature_unit_desc, 199 + 200 + (struct usb_descriptor_header *)&as_interface_alt_0_desc, 201 + (struct usb_descriptor_header *)&as_interface_alt_1_desc, 202 + (struct usb_descriptor_header *)&as_header_desc, 203 + 204 + (struct usb_descriptor_header *)&as_type_i_desc, 205 + 206 + (struct usb_descriptor_header *)&as_out_ep_desc, 207 + (struct usb_descriptor_header *)&as_iso_out_desc, 208 + NULL, 209 + }; 210 + 211 + enum { 212 + STR_AC_IF, 213 + STR_INPUT_TERMINAL, 214 + STR_INPUT_TERMINAL_CH_NAMES, 215 + STR_FEAT_DESC_0, 216 + STR_OUTPUT_TERMINAL, 217 + STR_AS_IF_ALT0, 218 + STR_AS_IF_ALT1, 219 + }; 220 + 221 + static struct usb_string strings_uac1[] = { 222 + [STR_AC_IF].s = "AC Interface", 223 + [STR_INPUT_TERMINAL].s = "Input terminal", 224 + [STR_INPUT_TERMINAL_CH_NAMES].s = "Channels", 225 + [STR_FEAT_DESC_0].s = "Volume control & mute", 226 + [STR_OUTPUT_TERMINAL].s = "Output terminal", 227 + [STR_AS_IF_ALT0].s = "AS Interface", 228 + [STR_AS_IF_ALT1].s = "AS Interface", 229 + { }, 230 + }; 231 + 232 + static struct usb_gadget_strings str_uac1 = { 233 + .language = 0x0409, /* en-us */ 234 + .strings = strings_uac1, 235 + }; 236 + 237 + static struct usb_gadget_strings *uac1_strings[] = { 238 + &str_uac1, 239 + NULL, 240 + }; 241 + 242 + /* 243 + * This function is an ALSA sound card following USB Audio Class Spec 1.0. 244 + */ 245 + 246 + /*-------------------------------------------------------------------------*/ 247 + struct f_audio_buf { 248 + u8 *buf; 249 + int actual; 250 + struct list_head list; 251 + }; 252 + 253 + static struct f_audio_buf *f_audio_buffer_alloc(int buf_size) 254 + { 255 + struct f_audio_buf *copy_buf; 256 + 257 + copy_buf = kzalloc(sizeof *copy_buf, GFP_ATOMIC); 258 + if (!copy_buf) 259 + return ERR_PTR(-ENOMEM); 260 + 261 + copy_buf->buf = kzalloc(buf_size, GFP_ATOMIC); 262 + if (!copy_buf->buf) { 263 + kfree(copy_buf); 264 + return ERR_PTR(-ENOMEM); 265 + } 266 + 267 + return copy_buf; 268 + } 269 + 270 + static void f_audio_buffer_free(struct f_audio_buf *audio_buf) 271 + { 272 + kfree(audio_buf->buf); 273 + kfree(audio_buf); 274 + } 275 + /*-------------------------------------------------------------------------*/ 276 + 277 + struct f_audio { 278 + struct gaudio card; 279 + 280 + u8 ac_intf, ac_alt; 281 + u8 as_intf, as_alt; 282 + 283 + /* endpoints handle full and/or high speeds */ 284 + struct usb_ep *out_ep; 285 + 286 + spinlock_t lock; 287 + struct f_audio_buf *copy_buf; 288 + struct work_struct playback_work; 289 + struct list_head play_queue; 290 + 291 + /* Control Set command */ 292 + struct list_head cs; 293 + u8 set_cmd; 294 + struct usb_audio_control *set_con; 295 + }; 296 + 297 + static inline struct f_audio *func_to_audio(struct usb_function *f) 298 + { 299 + return container_of(f, struct f_audio, card.func); 300 + } 301 + 302 + /*-------------------------------------------------------------------------*/ 303 + 304 + static void f_audio_playback_work(struct work_struct *data) 305 + { 306 + struct f_audio *audio = container_of(data, struct f_audio, 307 + playback_work); 308 + struct f_audio_buf *play_buf; 309 + 310 + spin_lock_irq(&audio->lock); 311 + if (list_empty(&audio->play_queue)) { 312 + spin_unlock_irq(&audio->lock); 313 + return; 314 + } 315 + play_buf = list_first_entry(&audio->play_queue, 316 + struct f_audio_buf, list); 317 + list_del(&play_buf->list); 318 + spin_unlock_irq(&audio->lock); 319 + 320 + u_audio_playback(&audio->card, play_buf->buf, play_buf->actual); 321 + f_audio_buffer_free(play_buf); 322 + } 323 + 324 + static int f_audio_out_ep_complete(struct usb_ep *ep, struct usb_request *req) 325 + { 326 + struct f_audio *audio = req->context; 327 + struct usb_composite_dev *cdev = audio->card.func.config->cdev; 328 + struct f_audio_buf *copy_buf = audio->copy_buf; 329 + struct f_uac1_legacy_opts *opts; 330 + int audio_buf_size; 331 + int err; 332 + 333 + opts = container_of(audio->card.func.fi, struct f_uac1_legacy_opts, 334 + func_inst); 335 + audio_buf_size = opts->audio_buf_size; 336 + 337 + if (!copy_buf) 338 + return -EINVAL; 339 + 340 + /* Copy buffer is full, add it to the play_queue */ 341 + if (audio_buf_size - copy_buf->actual < req->actual) { 342 + list_add_tail(&copy_buf->list, &audio->play_queue); 343 + schedule_work(&audio->playback_work); 344 + copy_buf = f_audio_buffer_alloc(audio_buf_size); 345 + if (IS_ERR(copy_buf)) 346 + return -ENOMEM; 347 + } 348 + 349 + memcpy(copy_buf->buf + copy_buf->actual, req->buf, req->actual); 350 + copy_buf->actual += req->actual; 351 + audio->copy_buf = copy_buf; 352 + 353 + err = usb_ep_queue(ep, req, GFP_ATOMIC); 354 + if (err) 355 + ERROR(cdev, "%s queue req: %d\n", ep->name, err); 356 + 357 + return 0; 358 + 359 + } 360 + 361 + static void f_audio_complete(struct usb_ep *ep, struct usb_request *req) 362 + { 363 + struct f_audio *audio = req->context; 364 + int status = req->status; 365 + u32 data = 0; 366 + struct usb_ep *out_ep = audio->out_ep; 367 + 368 + switch (status) { 369 + 370 + case 0: /* normal completion? */ 371 + if (ep == out_ep) 372 + f_audio_out_ep_complete(ep, req); 373 + else if (audio->set_con) { 374 + memcpy(&data, req->buf, req->length); 375 + audio->set_con->set(audio->set_con, audio->set_cmd, 376 + le16_to_cpu(data)); 377 + audio->set_con = NULL; 378 + } 379 + break; 380 + default: 381 + break; 382 + } 383 + } 384 + 385 + static int audio_set_intf_req(struct usb_function *f, 386 + const struct usb_ctrlrequest *ctrl) 387 + { 388 + struct f_audio *audio = func_to_audio(f); 389 + struct usb_composite_dev *cdev = f->config->cdev; 390 + struct usb_request *req = cdev->req; 391 + u8 id = ((le16_to_cpu(ctrl->wIndex) >> 8) & 0xFF); 392 + u16 len = le16_to_cpu(ctrl->wLength); 393 + u16 w_value = le16_to_cpu(ctrl->wValue); 394 + u8 con_sel = (w_value >> 8) & 0xFF; 395 + u8 cmd = (ctrl->bRequest & 0x0F); 396 + struct usb_audio_control_selector *cs; 397 + struct usb_audio_control *con; 398 + 399 + DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, entity %d\n", 400 + ctrl->bRequest, w_value, len, id); 401 + 402 + list_for_each_entry(cs, &audio->cs, list) { 403 + if (cs->id == id) { 404 + list_for_each_entry(con, &cs->control, list) { 405 + if (con->type == con_sel) { 406 + audio->set_con = con; 407 + break; 408 + } 409 + } 410 + break; 411 + } 412 + } 413 + 414 + audio->set_cmd = cmd; 415 + req->context = audio; 416 + req->complete = f_audio_complete; 417 + 418 + return len; 419 + } 420 + 421 + static int audio_get_intf_req(struct usb_function *f, 422 + const struct usb_ctrlrequest *ctrl) 423 + { 424 + struct f_audio *audio = func_to_audio(f); 425 + struct usb_composite_dev *cdev = f->config->cdev; 426 + struct usb_request *req = cdev->req; 427 + int value = -EOPNOTSUPP; 428 + u8 id = ((le16_to_cpu(ctrl->wIndex) >> 8) & 0xFF); 429 + u16 len = le16_to_cpu(ctrl->wLength); 430 + u16 w_value = le16_to_cpu(ctrl->wValue); 431 + u8 con_sel = (w_value >> 8) & 0xFF; 432 + u8 cmd = (ctrl->bRequest & 0x0F); 433 + struct usb_audio_control_selector *cs; 434 + struct usb_audio_control *con; 435 + 436 + DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, entity %d\n", 437 + ctrl->bRequest, w_value, len, id); 438 + 439 + list_for_each_entry(cs, &audio->cs, list) { 440 + if (cs->id == id) { 441 + list_for_each_entry(con, &cs->control, list) { 442 + if (con->type == con_sel && con->get) { 443 + value = con->get(con, cmd); 444 + break; 445 + } 446 + } 447 + break; 448 + } 449 + } 450 + 451 + req->context = audio; 452 + req->complete = f_audio_complete; 453 + len = min_t(size_t, sizeof(value), len); 454 + memcpy(req->buf, &value, len); 455 + 456 + return len; 457 + } 458 + 459 + static int audio_set_endpoint_req(struct usb_function *f, 460 + const struct usb_ctrlrequest *ctrl) 461 + { 462 + struct usb_composite_dev *cdev = f->config->cdev; 463 + int value = -EOPNOTSUPP; 464 + u16 ep = le16_to_cpu(ctrl->wIndex); 465 + u16 len = le16_to_cpu(ctrl->wLength); 466 + u16 w_value = le16_to_cpu(ctrl->wValue); 467 + 468 + DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, endpoint %d\n", 469 + ctrl->bRequest, w_value, len, ep); 470 + 471 + switch (ctrl->bRequest) { 472 + case UAC_SET_CUR: 473 + value = len; 474 + break; 475 + 476 + case UAC_SET_MIN: 477 + break; 478 + 479 + case UAC_SET_MAX: 480 + break; 481 + 482 + case UAC_SET_RES: 483 + break; 484 + 485 + case UAC_SET_MEM: 486 + break; 487 + 488 + default: 489 + break; 490 + } 491 + 492 + return value; 493 + } 494 + 495 + static int audio_get_endpoint_req(struct usb_function *f, 496 + const struct usb_ctrlrequest *ctrl) 497 + { 498 + struct usb_composite_dev *cdev = f->config->cdev; 499 + int value = -EOPNOTSUPP; 500 + u8 ep = ((le16_to_cpu(ctrl->wIndex) >> 8) & 0xFF); 501 + u16 len = le16_to_cpu(ctrl->wLength); 502 + u16 w_value = le16_to_cpu(ctrl->wValue); 503 + 504 + DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, endpoint %d\n", 505 + ctrl->bRequest, w_value, len, ep); 506 + 507 + switch (ctrl->bRequest) { 508 + case UAC_GET_CUR: 509 + case UAC_GET_MIN: 510 + case UAC_GET_MAX: 511 + case UAC_GET_RES: 512 + value = len; 513 + break; 514 + case UAC_GET_MEM: 515 + break; 516 + default: 517 + break; 518 + } 519 + 520 + return value; 521 + } 522 + 523 + static int 524 + f_audio_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl) 525 + { 526 + struct usb_composite_dev *cdev = f->config->cdev; 527 + struct usb_request *req = cdev->req; 528 + int value = -EOPNOTSUPP; 529 + u16 w_index = le16_to_cpu(ctrl->wIndex); 530 + u16 w_value = le16_to_cpu(ctrl->wValue); 531 + u16 w_length = le16_to_cpu(ctrl->wLength); 532 + 533 + /* composite driver infrastructure handles everything; interface 534 + * activation uses set_alt(). 535 + */ 536 + switch (ctrl->bRequestType) { 537 + case USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE: 538 + value = audio_set_intf_req(f, ctrl); 539 + break; 540 + 541 + case USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE: 542 + value = audio_get_intf_req(f, ctrl); 543 + break; 544 + 545 + case USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_ENDPOINT: 546 + value = audio_set_endpoint_req(f, ctrl); 547 + break; 548 + 549 + case USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT: 550 + value = audio_get_endpoint_req(f, ctrl); 551 + break; 552 + 553 + default: 554 + ERROR(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n", 555 + ctrl->bRequestType, ctrl->bRequest, 556 + w_value, w_index, w_length); 557 + } 558 + 559 + /* respond with data transfer or status phase? */ 560 + if (value >= 0) { 561 + DBG(cdev, "audio req%02x.%02x v%04x i%04x l%d\n", 562 + ctrl->bRequestType, ctrl->bRequest, 563 + w_value, w_index, w_length); 564 + req->zero = 0; 565 + req->length = value; 566 + value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC); 567 + if (value < 0) 568 + ERROR(cdev, "audio response on err %d\n", value); 569 + } 570 + 571 + /* device either stalls (value < 0) or reports success */ 572 + return value; 573 + } 574 + 575 + static int f_audio_set_alt(struct usb_function *f, unsigned intf, unsigned alt) 576 + { 577 + struct f_audio *audio = func_to_audio(f); 578 + struct usb_composite_dev *cdev = f->config->cdev; 579 + struct usb_ep *out_ep = audio->out_ep; 580 + struct usb_request *req; 581 + struct f_uac1_legacy_opts *opts; 582 + int req_buf_size, req_count, audio_buf_size; 583 + int i = 0, err = 0; 584 + 585 + DBG(cdev, "intf %d, alt %d\n", intf, alt); 586 + 587 + opts = container_of(f->fi, struct f_uac1_legacy_opts, func_inst); 588 + req_buf_size = opts->req_buf_size; 589 + req_count = opts->req_count; 590 + audio_buf_size = opts->audio_buf_size; 591 + 592 + /* No i/f has more than 2 alt settings */ 593 + if (alt > 1) { 594 + ERROR(cdev, "%s:%d Error!\n", __func__, __LINE__); 595 + return -EINVAL; 596 + } 597 + 598 + if (intf == audio->ac_intf) { 599 + /* Control I/f has only 1 AltSetting - 0 */ 600 + if (alt) { 601 + ERROR(cdev, "%s:%d Error!\n", __func__, __LINE__); 602 + return -EINVAL; 603 + } 604 + return 0; 605 + } else if (intf == audio->as_intf) { 606 + if (alt == 1) { 607 + err = config_ep_by_speed(cdev->gadget, f, out_ep); 608 + if (err) 609 + return err; 610 + 611 + usb_ep_enable(out_ep); 612 + audio->copy_buf = f_audio_buffer_alloc(audio_buf_size); 613 + if (IS_ERR(audio->copy_buf)) 614 + return -ENOMEM; 615 + 616 + /* 617 + * allocate a bunch of read buffers 618 + * and queue them all at once. 619 + */ 620 + for (i = 0; i < req_count && err == 0; i++) { 621 + req = usb_ep_alloc_request(out_ep, GFP_ATOMIC); 622 + if (req) { 623 + req->buf = kzalloc(req_buf_size, 624 + GFP_ATOMIC); 625 + if (req->buf) { 626 + req->length = req_buf_size; 627 + req->context = audio; 628 + req->complete = 629 + f_audio_complete; 630 + err = usb_ep_queue(out_ep, 631 + req, GFP_ATOMIC); 632 + if (err) 633 + ERROR(cdev, 634 + "%s queue req: %d\n", 635 + out_ep->name, err); 636 + } else 637 + err = -ENOMEM; 638 + } else 639 + err = -ENOMEM; 640 + } 641 + 642 + } else { 643 + struct f_audio_buf *copy_buf = audio->copy_buf; 644 + if (copy_buf) { 645 + list_add_tail(&copy_buf->list, 646 + &audio->play_queue); 647 + schedule_work(&audio->playback_work); 648 + } 649 + } 650 + audio->as_alt = alt; 651 + } 652 + 653 + return err; 654 + } 655 + 656 + static int f_audio_get_alt(struct usb_function *f, unsigned intf) 657 + { 658 + struct f_audio *audio = func_to_audio(f); 659 + struct usb_composite_dev *cdev = f->config->cdev; 660 + 661 + if (intf == audio->ac_intf) 662 + return audio->ac_alt; 663 + else if (intf == audio->as_intf) 664 + return audio->as_alt; 665 + else 666 + ERROR(cdev, "%s:%d Invalid Interface %d!\n", 667 + __func__, __LINE__, intf); 668 + 669 + return -EINVAL; 670 + } 671 + 672 + static void f_audio_disable(struct usb_function *f) 673 + { 674 + return; 675 + } 676 + 677 + /*-------------------------------------------------------------------------*/ 678 + 679 + static void f_audio_build_desc(struct f_audio *audio) 680 + { 681 + struct gaudio *card = &audio->card; 682 + u8 *sam_freq; 683 + int rate; 684 + 685 + /* Set channel numbers */ 686 + input_terminal_desc.bNrChannels = u_audio_get_playback_channels(card); 687 + as_type_i_desc.bNrChannels = u_audio_get_playback_channels(card); 688 + 689 + /* Set sample rates */ 690 + rate = u_audio_get_playback_rate(card); 691 + sam_freq = as_type_i_desc.tSamFreq[0]; 692 + memcpy(sam_freq, &rate, 3); 693 + 694 + /* Todo: Set Sample bits and other parameters */ 695 + 696 + return; 697 + } 698 + 699 + /* audio function driver setup/binding */ 700 + static int 701 + f_audio_bind(struct usb_configuration *c, struct usb_function *f) 702 + { 703 + struct usb_composite_dev *cdev = c->cdev; 704 + struct f_audio *audio = func_to_audio(f); 705 + struct usb_string *us; 706 + int status; 707 + struct usb_ep *ep = NULL; 708 + struct f_uac1_legacy_opts *audio_opts; 709 + 710 + audio_opts = container_of(f->fi, struct f_uac1_legacy_opts, func_inst); 711 + audio->card.gadget = c->cdev->gadget; 712 + /* set up ASLA audio devices */ 713 + if (!audio_opts->bound) { 714 + status = gaudio_setup(&audio->card); 715 + if (status < 0) 716 + return status; 717 + audio_opts->bound = true; 718 + } 719 + us = usb_gstrings_attach(cdev, uac1_strings, ARRAY_SIZE(strings_uac1)); 720 + if (IS_ERR(us)) 721 + return PTR_ERR(us); 722 + ac_interface_desc.iInterface = us[STR_AC_IF].id; 723 + input_terminal_desc.iTerminal = us[STR_INPUT_TERMINAL].id; 724 + input_terminal_desc.iChannelNames = us[STR_INPUT_TERMINAL_CH_NAMES].id; 725 + feature_unit_desc.iFeature = us[STR_FEAT_DESC_0].id; 726 + output_terminal_desc.iTerminal = us[STR_OUTPUT_TERMINAL].id; 727 + as_interface_alt_0_desc.iInterface = us[STR_AS_IF_ALT0].id; 728 + as_interface_alt_1_desc.iInterface = us[STR_AS_IF_ALT1].id; 729 + 730 + 731 + f_audio_build_desc(audio); 732 + 733 + /* allocate instance-specific interface IDs, and patch descriptors */ 734 + status = usb_interface_id(c, f); 735 + if (status < 0) 736 + goto fail; 737 + ac_interface_desc.bInterfaceNumber = status; 738 + audio->ac_intf = status; 739 + audio->ac_alt = 0; 740 + 741 + status = usb_interface_id(c, f); 742 + if (status < 0) 743 + goto fail; 744 + as_interface_alt_0_desc.bInterfaceNumber = status; 745 + as_interface_alt_1_desc.bInterfaceNumber = status; 746 + audio->as_intf = status; 747 + audio->as_alt = 0; 748 + 749 + status = -ENODEV; 750 + 751 + /* allocate instance-specific endpoints */ 752 + ep = usb_ep_autoconfig(cdev->gadget, &as_out_ep_desc); 753 + if (!ep) 754 + goto fail; 755 + audio->out_ep = ep; 756 + audio->out_ep->desc = &as_out_ep_desc; 757 + 758 + status = -ENOMEM; 759 + 760 + /* copy descriptors, and track endpoint copies */ 761 + status = usb_assign_descriptors(f, f_audio_desc, f_audio_desc, NULL, 762 + NULL); 763 + if (status) 764 + goto fail; 765 + return 0; 766 + 767 + fail: 768 + gaudio_cleanup(&audio->card); 769 + return status; 770 + } 771 + 772 + /*-------------------------------------------------------------------------*/ 773 + 774 + static int generic_set_cmd(struct usb_audio_control *con, u8 cmd, int value) 775 + { 776 + con->data[cmd] = value; 777 + 778 + return 0; 779 + } 780 + 781 + static int generic_get_cmd(struct usb_audio_control *con, u8 cmd) 782 + { 783 + return con->data[cmd]; 784 + } 785 + 786 + /* Todo: add more control selecotor dynamically */ 787 + static int control_selector_init(struct f_audio *audio) 788 + { 789 + INIT_LIST_HEAD(&audio->cs); 790 + list_add(&feature_unit.list, &audio->cs); 791 + 792 + INIT_LIST_HEAD(&feature_unit.control); 793 + list_add(&mute_control.list, &feature_unit.control); 794 + list_add(&volume_control.list, &feature_unit.control); 795 + 796 + volume_control.data[UAC__CUR] = 0xffc0; 797 + volume_control.data[UAC__MIN] = 0xe3a0; 798 + volume_control.data[UAC__MAX] = 0xfff0; 799 + volume_control.data[UAC__RES] = 0x0030; 800 + 801 + return 0; 802 + } 803 + 804 + static inline 805 + struct f_uac1_legacy_opts *to_f_uac1_opts(struct config_item *item) 806 + { 807 + return container_of(to_config_group(item), struct f_uac1_legacy_opts, 808 + func_inst.group); 809 + } 810 + 811 + static void f_uac1_attr_release(struct config_item *item) 812 + { 813 + struct f_uac1_legacy_opts *opts = to_f_uac1_opts(item); 814 + 815 + usb_put_function_instance(&opts->func_inst); 816 + } 817 + 818 + static struct configfs_item_operations f_uac1_item_ops = { 819 + .release = f_uac1_attr_release, 820 + }; 821 + 822 + #define UAC1_INT_ATTRIBUTE(name) \ 823 + static ssize_t f_uac1_opts_##name##_show(struct config_item *item, \ 824 + char *page) \ 825 + { \ 826 + struct f_uac1_legacy_opts *opts = to_f_uac1_opts(item); \ 827 + int result; \ 828 + \ 829 + mutex_lock(&opts->lock); \ 830 + result = sprintf(page, "%u\n", opts->name); \ 831 + mutex_unlock(&opts->lock); \ 832 + \ 833 + return result; \ 834 + } \ 835 + \ 836 + static ssize_t f_uac1_opts_##name##_store(struct config_item *item, \ 837 + const char *page, size_t len) \ 838 + { \ 839 + struct f_uac1_legacy_opts *opts = to_f_uac1_opts(item); \ 840 + int ret; \ 841 + u32 num; \ 842 + \ 843 + mutex_lock(&opts->lock); \ 844 + if (opts->refcnt) { \ 845 + ret = -EBUSY; \ 846 + goto end; \ 847 + } \ 848 + \ 849 + ret = kstrtou32(page, 0, &num); \ 850 + if (ret) \ 851 + goto end; \ 852 + \ 853 + opts->name = num; \ 854 + ret = len; \ 855 + \ 856 + end: \ 857 + mutex_unlock(&opts->lock); \ 858 + return ret; \ 859 + } \ 860 + \ 861 + CONFIGFS_ATTR(f_uac1_opts_, name) 862 + 863 + UAC1_INT_ATTRIBUTE(req_buf_size); 864 + UAC1_INT_ATTRIBUTE(req_count); 865 + UAC1_INT_ATTRIBUTE(audio_buf_size); 866 + 867 + #define UAC1_STR_ATTRIBUTE(name) \ 868 + static ssize_t f_uac1_opts_##name##_show(struct config_item *item, \ 869 + char *page) \ 870 + { \ 871 + struct f_uac1_legacy_opts *opts = to_f_uac1_opts(item); \ 872 + int result; \ 873 + \ 874 + mutex_lock(&opts->lock); \ 875 + result = sprintf(page, "%s\n", opts->name); \ 876 + mutex_unlock(&opts->lock); \ 877 + \ 878 + return result; \ 879 + } \ 880 + \ 881 + static ssize_t f_uac1_opts_##name##_store(struct config_item *item, \ 882 + const char *page, size_t len) \ 883 + { \ 884 + struct f_uac1_legacy_opts *opts = to_f_uac1_opts(item); \ 885 + int ret = -EBUSY; \ 886 + char *tmp; \ 887 + \ 888 + mutex_lock(&opts->lock); \ 889 + if (opts->refcnt) \ 890 + goto end; \ 891 + \ 892 + tmp = kstrndup(page, len, GFP_KERNEL); \ 893 + if (tmp) { \ 894 + ret = -ENOMEM; \ 895 + goto end; \ 896 + } \ 897 + if (opts->name##_alloc) \ 898 + kfree(opts->name); \ 899 + opts->name##_alloc = true; \ 900 + opts->name = tmp; \ 901 + ret = len; \ 902 + \ 903 + end: \ 904 + mutex_unlock(&opts->lock); \ 905 + return ret; \ 906 + } \ 907 + \ 908 + CONFIGFS_ATTR(f_uac1_opts_, name) 909 + 910 + UAC1_STR_ATTRIBUTE(fn_play); 911 + UAC1_STR_ATTRIBUTE(fn_cap); 912 + UAC1_STR_ATTRIBUTE(fn_cntl); 913 + 914 + static struct configfs_attribute *f_uac1_attrs[] = { 915 + &f_uac1_opts_attr_req_buf_size, 916 + &f_uac1_opts_attr_req_count, 917 + &f_uac1_opts_attr_audio_buf_size, 918 + &f_uac1_opts_attr_fn_play, 919 + &f_uac1_opts_attr_fn_cap, 920 + &f_uac1_opts_attr_fn_cntl, 921 + NULL, 922 + }; 923 + 924 + static struct config_item_type f_uac1_func_type = { 925 + .ct_item_ops = &f_uac1_item_ops, 926 + .ct_attrs = f_uac1_attrs, 927 + .ct_owner = THIS_MODULE, 928 + }; 929 + 930 + static void f_audio_free_inst(struct usb_function_instance *f) 931 + { 932 + struct f_uac1_legacy_opts *opts; 933 + 934 + opts = container_of(f, struct f_uac1_legacy_opts, func_inst); 935 + if (opts->fn_play_alloc) 936 + kfree(opts->fn_play); 937 + if (opts->fn_cap_alloc) 938 + kfree(opts->fn_cap); 939 + if (opts->fn_cntl_alloc) 940 + kfree(opts->fn_cntl); 941 + kfree(opts); 942 + } 943 + 944 + static struct usb_function_instance *f_audio_alloc_inst(void) 945 + { 946 + struct f_uac1_legacy_opts *opts; 947 + 948 + opts = kzalloc(sizeof(*opts), GFP_KERNEL); 949 + if (!opts) 950 + return ERR_PTR(-ENOMEM); 951 + 952 + mutex_init(&opts->lock); 953 + opts->func_inst.free_func_inst = f_audio_free_inst; 954 + 955 + config_group_init_type_name(&opts->func_inst.group, "", 956 + &f_uac1_func_type); 957 + 958 + opts->req_buf_size = UAC1_OUT_EP_MAX_PACKET_SIZE; 959 + opts->req_count = UAC1_REQ_COUNT; 960 + opts->audio_buf_size = UAC1_AUDIO_BUF_SIZE; 961 + opts->fn_play = FILE_PCM_PLAYBACK; 962 + opts->fn_cap = FILE_PCM_CAPTURE; 963 + opts->fn_cntl = FILE_CONTROL; 964 + return &opts->func_inst; 965 + } 966 + 967 + static void f_audio_free(struct usb_function *f) 968 + { 969 + struct f_audio *audio = func_to_audio(f); 970 + struct f_uac1_legacy_opts *opts; 971 + 972 + gaudio_cleanup(&audio->card); 973 + opts = container_of(f->fi, struct f_uac1_legacy_opts, func_inst); 974 + kfree(audio); 975 + mutex_lock(&opts->lock); 976 + --opts->refcnt; 977 + mutex_unlock(&opts->lock); 978 + } 979 + 980 + static void f_audio_unbind(struct usb_configuration *c, struct usb_function *f) 981 + { 982 + usb_free_all_descriptors(f); 983 + } 984 + 985 + static struct usb_function *f_audio_alloc(struct usb_function_instance *fi) 986 + { 987 + struct f_audio *audio; 988 + struct f_uac1_legacy_opts *opts; 989 + 990 + /* allocate and initialize one new instance */ 991 + audio = kzalloc(sizeof(*audio), GFP_KERNEL); 992 + if (!audio) 993 + return ERR_PTR(-ENOMEM); 994 + 995 + audio->card.func.name = "g_audio"; 996 + 997 + opts = container_of(fi, struct f_uac1_legacy_opts, func_inst); 998 + mutex_lock(&opts->lock); 999 + ++opts->refcnt; 1000 + mutex_unlock(&opts->lock); 1001 + INIT_LIST_HEAD(&audio->play_queue); 1002 + spin_lock_init(&audio->lock); 1003 + 1004 + audio->card.func.bind = f_audio_bind; 1005 + audio->card.func.unbind = f_audio_unbind; 1006 + audio->card.func.set_alt = f_audio_set_alt; 1007 + audio->card.func.get_alt = f_audio_get_alt; 1008 + audio->card.func.setup = f_audio_setup; 1009 + audio->card.func.disable = f_audio_disable; 1010 + audio->card.func.free_func = f_audio_free; 1011 + 1012 + control_selector_init(audio); 1013 + 1014 + INIT_WORK(&audio->playback_work, f_audio_playback_work); 1015 + 1016 + return &audio->card.func; 1017 + } 1018 + 1019 + DECLARE_USB_FUNCTION_INIT(uac1_legacy, f_audio_alloc_inst, f_audio_alloc); 1020 + MODULE_LICENSE("GPL"); 1021 + MODULE_AUTHOR("Bryan Wu");
+100 -705
drivers/usb/gadget/function/f_uac2.c
··· 13 13 14 14 #include <linux/usb/audio.h> 15 15 #include <linux/usb/audio-v2.h> 16 - #include <linux/platform_device.h> 17 16 #include <linux/module.h> 18 17 19 - #include <sound/core.h> 20 - #include <sound/pcm.h> 21 - #include <sound/pcm_params.h> 22 - 18 + #include "u_audio.h" 23 19 #include "u_uac2.h" 24 20 25 21 /* ··· 47 51 #define UNFLW_CTRL 8 48 52 #define OVFLW_CTRL 10 49 53 50 - static const char *uac2_name = "snd_uac2"; 51 - 52 - struct uac2_req { 53 - struct uac2_rtd_params *pp; /* parent param */ 54 - struct usb_request *req; 54 + struct f_uac2 { 55 + struct g_audio g_audio; 56 + u8 ac_intf, as_in_intf, as_out_intf; 57 + u8 ac_alt, as_in_alt, as_out_alt; /* needed for get_alt() */ 55 58 }; 56 59 57 - struct uac2_rtd_params { 58 - struct snd_uac2_chip *uac2; /* parent chip */ 59 - bool ep_enabled; /* if the ep is enabled */ 60 - /* Size of the ring buffer */ 61 - size_t dma_bytes; 62 - unsigned char *dma_area; 63 - 64 - struct snd_pcm_substream *ss; 65 - 66 - /* Ring buffer */ 67 - ssize_t hw_ptr; 68 - 69 - void *rbuf; 70 - 71 - size_t period_size; 72 - 73 - unsigned max_psize; 74 - struct uac2_req *ureq; 75 - 76 - spinlock_t lock; 77 - }; 78 - 79 - struct snd_uac2_chip { 80 - struct platform_device pdev; 81 - struct platform_driver pdrv; 82 - 83 - struct uac2_rtd_params p_prm; 84 - struct uac2_rtd_params c_prm; 85 - 86 - struct snd_card *card; 87 - struct snd_pcm *pcm; 88 - 89 - /* timekeeping for the playback endpoint */ 90 - unsigned int p_interval; 91 - unsigned int p_residue; 92 - 93 - /* pre-calculated values for playback iso completion */ 94 - unsigned int p_pktsize; 95 - unsigned int p_pktsize_residue; 96 - unsigned int p_framesize; 97 - }; 98 - 99 - #define BUFF_SIZE_MAX (PAGE_SIZE * 16) 100 - #define PRD_SIZE_MAX PAGE_SIZE 101 - #define MIN_PERIODS 4 102 - 103 - static struct snd_pcm_hardware uac2_pcm_hardware = { 104 - .info = SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER 105 - | SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID 106 - | SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME, 107 - .rates = SNDRV_PCM_RATE_CONTINUOUS, 108 - .periods_max = BUFF_SIZE_MAX / PRD_SIZE_MAX, 109 - .buffer_bytes_max = BUFF_SIZE_MAX, 110 - .period_bytes_max = PRD_SIZE_MAX, 111 - .periods_min = MIN_PERIODS, 112 - }; 113 - 114 - struct audio_dev { 115 - u8 ac_intf, ac_alt; 116 - u8 as_out_intf, as_out_alt; 117 - u8 as_in_intf, as_in_alt; 118 - 119 - struct usb_ep *in_ep, *out_ep; 120 - struct usb_function func; 121 - 122 - /* The ALSA Sound Card it represents on the USB-Client side */ 123 - struct snd_uac2_chip uac2; 124 - }; 125 - 126 - static inline 127 - struct audio_dev *func_to_agdev(struct usb_function *f) 60 + static inline struct f_uac2 *func_to_uac2(struct usb_function *f) 128 61 { 129 - return container_of(f, struct audio_dev, func); 62 + return container_of(f, struct f_uac2, g_audio.func); 130 63 } 131 64 132 65 static inline 133 - struct audio_dev *uac2_to_agdev(struct snd_uac2_chip *u) 134 - { 135 - return container_of(u, struct audio_dev, uac2); 136 - } 137 - 138 - static inline 139 - struct snd_uac2_chip *pdev_to_uac2(struct platform_device *p) 140 - { 141 - return container_of(p, struct snd_uac2_chip, pdev); 142 - } 143 - 144 - static inline 145 - struct f_uac2_opts *agdev_to_uac2_opts(struct audio_dev *agdev) 66 + struct f_uac2_opts *g_audio_to_uac2_opts(struct g_audio *agdev) 146 67 { 147 68 return container_of(agdev->func.fi, struct f_uac2_opts, func_inst); 148 69 } 149 - 150 - static inline 151 - uint num_channels(uint chanmask) 152 - { 153 - uint num = 0; 154 - 155 - while (chanmask) { 156 - num += (chanmask & 1); 157 - chanmask >>= 1; 158 - } 159 - 160 - return num; 161 - } 162 - 163 - static void 164 - agdev_iso_complete(struct usb_ep *ep, struct usb_request *req) 165 - { 166 - unsigned pending; 167 - unsigned long flags; 168 - unsigned int hw_ptr; 169 - bool update_alsa = false; 170 - int status = req->status; 171 - struct uac2_req *ur = req->context; 172 - struct snd_pcm_substream *substream; 173 - struct uac2_rtd_params *prm = ur->pp; 174 - struct snd_uac2_chip *uac2 = prm->uac2; 175 - 176 - /* i/f shutting down */ 177 - if (!prm->ep_enabled || req->status == -ESHUTDOWN) 178 - return; 179 - 180 - /* 181 - * We can't really do much about bad xfers. 182 - * Afterall, the ISOCH xfers could fail legitimately. 183 - */ 184 - if (status) 185 - pr_debug("%s: iso_complete status(%d) %d/%d\n", 186 - __func__, status, req->actual, req->length); 187 - 188 - substream = prm->ss; 189 - 190 - /* Do nothing if ALSA isn't active */ 191 - if (!substream) 192 - goto exit; 193 - 194 - spin_lock_irqsave(&prm->lock, flags); 195 - 196 - if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 197 - /* 198 - * For each IN packet, take the quotient of the current data 199 - * rate and the endpoint's interval as the base packet size. 200 - * If there is a residue from this division, add it to the 201 - * residue accumulator. 202 - */ 203 - req->length = uac2->p_pktsize; 204 - uac2->p_residue += uac2->p_pktsize_residue; 205 - 206 - /* 207 - * Whenever there are more bytes in the accumulator than we 208 - * need to add one more sample frame, increase this packet's 209 - * size and decrease the accumulator. 210 - */ 211 - if (uac2->p_residue / uac2->p_interval >= uac2->p_framesize) { 212 - req->length += uac2->p_framesize; 213 - uac2->p_residue -= uac2->p_framesize * 214 - uac2->p_interval; 215 - } 216 - 217 - req->actual = req->length; 218 - } 219 - 220 - pending = prm->hw_ptr % prm->period_size; 221 - pending += req->actual; 222 - if (pending >= prm->period_size) 223 - update_alsa = true; 224 - 225 - hw_ptr = prm->hw_ptr; 226 - prm->hw_ptr = (prm->hw_ptr + req->actual) % prm->dma_bytes; 227 - 228 - spin_unlock_irqrestore(&prm->lock, flags); 229 - 230 - /* Pack USB load in ALSA ring buffer */ 231 - pending = prm->dma_bytes - hw_ptr; 232 - 233 - if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 234 - if (unlikely(pending < req->actual)) { 235 - memcpy(req->buf, prm->dma_area + hw_ptr, pending); 236 - memcpy(req->buf + pending, prm->dma_area, 237 - req->actual - pending); 238 - } else { 239 - memcpy(req->buf, prm->dma_area + hw_ptr, req->actual); 240 - } 241 - } else { 242 - if (unlikely(pending < req->actual)) { 243 - memcpy(prm->dma_area + hw_ptr, req->buf, pending); 244 - memcpy(prm->dma_area, req->buf + pending, 245 - req->actual - pending); 246 - } else { 247 - memcpy(prm->dma_area + hw_ptr, req->buf, req->actual); 248 - } 249 - } 250 - 251 - exit: 252 - if (usb_ep_queue(ep, req, GFP_ATOMIC)) 253 - dev_err(&uac2->pdev.dev, "%d Error!\n", __LINE__); 254 - 255 - if (update_alsa) 256 - snd_pcm_period_elapsed(substream); 257 - 258 - return; 259 - } 260 - 261 - static int 262 - uac2_pcm_trigger(struct snd_pcm_substream *substream, int cmd) 263 - { 264 - struct snd_uac2_chip *uac2 = snd_pcm_substream_chip(substream); 265 - struct audio_dev *agdev = uac2_to_agdev(uac2); 266 - struct f_uac2_opts *uac2_opts = agdev_to_uac2_opts(agdev); 267 - struct uac2_rtd_params *prm; 268 - unsigned long flags; 269 - int err = 0; 270 - 271 - if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 272 - prm = &uac2->p_prm; 273 - else 274 - prm = &uac2->c_prm; 275 - 276 - spin_lock_irqsave(&prm->lock, flags); 277 - 278 - /* Reset */ 279 - prm->hw_ptr = 0; 280 - 281 - switch (cmd) { 282 - case SNDRV_PCM_TRIGGER_START: 283 - case SNDRV_PCM_TRIGGER_RESUME: 284 - prm->ss = substream; 285 - break; 286 - case SNDRV_PCM_TRIGGER_STOP: 287 - case SNDRV_PCM_TRIGGER_SUSPEND: 288 - prm->ss = NULL; 289 - break; 290 - default: 291 - err = -EINVAL; 292 - } 293 - 294 - spin_unlock_irqrestore(&prm->lock, flags); 295 - 296 - /* Clear buffer after Play stops */ 297 - if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && !prm->ss) 298 - memset(prm->rbuf, 0, prm->max_psize * uac2_opts->req_number); 299 - 300 - return err; 301 - } 302 - 303 - static snd_pcm_uframes_t uac2_pcm_pointer(struct snd_pcm_substream *substream) 304 - { 305 - struct snd_uac2_chip *uac2 = snd_pcm_substream_chip(substream); 306 - struct uac2_rtd_params *prm; 307 - 308 - if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 309 - prm = &uac2->p_prm; 310 - else 311 - prm = &uac2->c_prm; 312 - 313 - return bytes_to_frames(substream->runtime, prm->hw_ptr); 314 - } 315 - 316 - static int uac2_pcm_hw_params(struct snd_pcm_substream *substream, 317 - struct snd_pcm_hw_params *hw_params) 318 - { 319 - struct snd_uac2_chip *uac2 = snd_pcm_substream_chip(substream); 320 - struct uac2_rtd_params *prm; 321 - int err; 322 - 323 - if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 324 - prm = &uac2->p_prm; 325 - else 326 - prm = &uac2->c_prm; 327 - 328 - err = snd_pcm_lib_malloc_pages(substream, 329 - params_buffer_bytes(hw_params)); 330 - if (err >= 0) { 331 - prm->dma_bytes = substream->runtime->dma_bytes; 332 - prm->dma_area = substream->runtime->dma_area; 333 - prm->period_size = params_period_bytes(hw_params); 334 - } 335 - 336 - return err; 337 - } 338 - 339 - static int uac2_pcm_hw_free(struct snd_pcm_substream *substream) 340 - { 341 - struct snd_uac2_chip *uac2 = snd_pcm_substream_chip(substream); 342 - struct uac2_rtd_params *prm; 343 - 344 - if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 345 - prm = &uac2->p_prm; 346 - else 347 - prm = &uac2->c_prm; 348 - 349 - prm->dma_area = NULL; 350 - prm->dma_bytes = 0; 351 - prm->period_size = 0; 352 - 353 - return snd_pcm_lib_free_pages(substream); 354 - } 355 - 356 - static int uac2_pcm_open(struct snd_pcm_substream *substream) 357 - { 358 - struct snd_uac2_chip *uac2 = snd_pcm_substream_chip(substream); 359 - struct snd_pcm_runtime *runtime = substream->runtime; 360 - struct audio_dev *audio_dev; 361 - struct f_uac2_opts *opts; 362 - int p_ssize, c_ssize; 363 - int p_srate, c_srate; 364 - int p_chmask, c_chmask; 365 - 366 - audio_dev = uac2_to_agdev(uac2); 367 - opts = container_of(audio_dev->func.fi, struct f_uac2_opts, func_inst); 368 - p_ssize = opts->p_ssize; 369 - c_ssize = opts->c_ssize; 370 - p_srate = opts->p_srate; 371 - c_srate = opts->c_srate; 372 - p_chmask = opts->p_chmask; 373 - c_chmask = opts->c_chmask; 374 - uac2->p_residue = 0; 375 - 376 - runtime->hw = uac2_pcm_hardware; 377 - 378 - if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 379 - spin_lock_init(&uac2->p_prm.lock); 380 - runtime->hw.rate_min = p_srate; 381 - switch (p_ssize) { 382 - case 3: 383 - runtime->hw.formats = SNDRV_PCM_FMTBIT_S24_3LE; 384 - break; 385 - case 4: 386 - runtime->hw.formats = SNDRV_PCM_FMTBIT_S32_LE; 387 - break; 388 - default: 389 - runtime->hw.formats = SNDRV_PCM_FMTBIT_S16_LE; 390 - break; 391 - } 392 - runtime->hw.channels_min = num_channels(p_chmask); 393 - runtime->hw.period_bytes_min = 2 * uac2->p_prm.max_psize 394 - / runtime->hw.periods_min; 395 - } else { 396 - spin_lock_init(&uac2->c_prm.lock); 397 - runtime->hw.rate_min = c_srate; 398 - switch (c_ssize) { 399 - case 3: 400 - runtime->hw.formats = SNDRV_PCM_FMTBIT_S24_3LE; 401 - break; 402 - case 4: 403 - runtime->hw.formats = SNDRV_PCM_FMTBIT_S32_LE; 404 - break; 405 - default: 406 - runtime->hw.formats = SNDRV_PCM_FMTBIT_S16_LE; 407 - break; 408 - } 409 - runtime->hw.channels_min = num_channels(c_chmask); 410 - runtime->hw.period_bytes_min = 2 * uac2->c_prm.max_psize 411 - / runtime->hw.periods_min; 412 - } 413 - 414 - runtime->hw.rate_max = runtime->hw.rate_min; 415 - runtime->hw.channels_max = runtime->hw.channels_min; 416 - 417 - snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS); 418 - 419 - return 0; 420 - } 421 - 422 - /* ALSA cries without these function pointers */ 423 - static int uac2_pcm_null(struct snd_pcm_substream *substream) 424 - { 425 - return 0; 426 - } 427 - 428 - static struct snd_pcm_ops uac2_pcm_ops = { 429 - .open = uac2_pcm_open, 430 - .close = uac2_pcm_null, 431 - .ioctl = snd_pcm_lib_ioctl, 432 - .hw_params = uac2_pcm_hw_params, 433 - .hw_free = uac2_pcm_hw_free, 434 - .trigger = uac2_pcm_trigger, 435 - .pointer = uac2_pcm_pointer, 436 - .prepare = uac2_pcm_null, 437 - }; 438 - 439 - static int snd_uac2_probe(struct platform_device *pdev) 440 - { 441 - struct snd_uac2_chip *uac2 = pdev_to_uac2(pdev); 442 - struct snd_card *card; 443 - struct snd_pcm *pcm; 444 - struct audio_dev *audio_dev; 445 - struct f_uac2_opts *opts; 446 - int err; 447 - int p_chmask, c_chmask; 448 - 449 - audio_dev = uac2_to_agdev(uac2); 450 - opts = container_of(audio_dev->func.fi, struct f_uac2_opts, func_inst); 451 - p_chmask = opts->p_chmask; 452 - c_chmask = opts->c_chmask; 453 - 454 - /* Choose any slot, with no id */ 455 - err = snd_card_new(&pdev->dev, -1, NULL, THIS_MODULE, 0, &card); 456 - if (err < 0) 457 - return err; 458 - 459 - uac2->card = card; 460 - 461 - /* 462 - * Create first PCM device 463 - * Create a substream only for non-zero channel streams 464 - */ 465 - err = snd_pcm_new(uac2->card, "UAC2 PCM", 0, 466 - p_chmask ? 1 : 0, c_chmask ? 1 : 0, &pcm); 467 - if (err < 0) 468 - goto snd_fail; 469 - 470 - strcpy(pcm->name, "UAC2 PCM"); 471 - pcm->private_data = uac2; 472 - 473 - uac2->pcm = pcm; 474 - 475 - snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &uac2_pcm_ops); 476 - snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &uac2_pcm_ops); 477 - 478 - strcpy(card->driver, "UAC2_Gadget"); 479 - strcpy(card->shortname, "UAC2_Gadget"); 480 - sprintf(card->longname, "UAC2_Gadget %i", pdev->id); 481 - 482 - snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS, 483 - snd_dma_continuous_data(GFP_KERNEL), 0, BUFF_SIZE_MAX); 484 - 485 - err = snd_card_register(card); 486 - if (!err) { 487 - platform_set_drvdata(pdev, card); 488 - return 0; 489 - } 490 - 491 - snd_fail: 492 - snd_card_free(card); 493 - 494 - uac2->pcm = NULL; 495 - uac2->card = NULL; 496 - 497 - return err; 498 - } 499 - 500 - static int snd_uac2_remove(struct platform_device *pdev) 501 - { 502 - struct snd_card *card = platform_get_drvdata(pdev); 503 - 504 - if (card) 505 - return snd_card_free(card); 506 - 507 - return 0; 508 - } 509 - 510 - static void snd_uac2_release(struct device *dev) 511 - { 512 - dev_dbg(dev, "releasing '%s'\n", dev_name(dev)); 513 - } 514 - 515 - static int alsa_uac2_init(struct audio_dev *agdev) 516 - { 517 - struct snd_uac2_chip *uac2 = &agdev->uac2; 518 - int err; 519 - 520 - uac2->pdrv.probe = snd_uac2_probe; 521 - uac2->pdrv.remove = snd_uac2_remove; 522 - uac2->pdrv.driver.name = uac2_name; 523 - 524 - uac2->pdev.id = 0; 525 - uac2->pdev.name = uac2_name; 526 - uac2->pdev.dev.release = snd_uac2_release; 527 - 528 - /* Register snd_uac2 driver */ 529 - err = platform_driver_register(&uac2->pdrv); 530 - if (err) 531 - return err; 532 - 533 - /* Register snd_uac2 device */ 534 - err = platform_device_register(&uac2->pdev); 535 - if (err) 536 - platform_driver_unregister(&uac2->pdrv); 537 - 538 - return err; 539 - } 540 - 541 - static void alsa_uac2_exit(struct audio_dev *agdev) 542 - { 543 - struct snd_uac2_chip *uac2 = &agdev->uac2; 544 - 545 - platform_driver_unregister(&uac2->pdrv); 546 - platform_device_unregister(&uac2->pdev); 547 - } 548 - 549 70 550 71 /* --------- USB Function Interface ------------- */ 551 72 ··· 451 938 __u32 dRES; 452 939 } __packed; 453 940 454 - static inline void 455 - free_ep(struct uac2_rtd_params *prm, struct usb_ep *ep) 456 - { 457 - struct snd_uac2_chip *uac2 = prm->uac2; 458 - struct audio_dev *agdev = uac2_to_agdev(uac2); 459 - struct f_uac2_opts *uac2_opts = agdev_to_uac2_opts(agdev); 460 - int i; 461 - 462 - if (!prm->ep_enabled) 463 - return; 464 - 465 - prm->ep_enabled = false; 466 - 467 - for (i = 0; i < uac2_opts->req_number; i++) { 468 - if (prm->ureq[i].req) { 469 - usb_ep_dequeue(ep, prm->ureq[i].req); 470 - usb_ep_free_request(ep, prm->ureq[i].req); 471 - prm->ureq[i].req = NULL; 472 - } 473 - } 474 - 475 - if (usb_ep_disable(ep)) 476 - dev_err(&uac2->pdev.dev, 477 - "%s:%d Error!\n", __func__, __LINE__); 478 - } 479 - 480 941 static void set_ep_max_packet_size(const struct f_uac2_opts *uac2_opts, 481 942 struct usb_endpoint_descriptor *ep_desc, 482 943 unsigned int factor, bool is_playback) ··· 477 990 static int 478 991 afunc_bind(struct usb_configuration *cfg, struct usb_function *fn) 479 992 { 480 - struct audio_dev *agdev = func_to_agdev(fn); 481 - struct snd_uac2_chip *uac2 = &agdev->uac2; 993 + struct f_uac2 *uac2 = func_to_uac2(fn); 994 + struct g_audio *agdev = func_to_g_audio(fn); 482 995 struct usb_composite_dev *cdev = cfg->cdev; 483 996 struct usb_gadget *gadget = cdev->gadget; 484 - struct device *dev = &uac2->pdev.dev; 485 - struct uac2_rtd_params *prm; 997 + struct device *dev = &gadget->dev; 486 998 struct f_uac2_opts *uac2_opts; 487 999 struct usb_string *us; 488 1000 int ret; ··· 528 1042 return ret; 529 1043 } 530 1044 std_ac_if_desc.bInterfaceNumber = ret; 531 - agdev->ac_intf = ret; 532 - agdev->ac_alt = 0; 1045 + uac2->ac_intf = ret; 1046 + uac2->ac_alt = 0; 533 1047 534 1048 ret = usb_interface_id(cfg, fn); 535 1049 if (ret < 0) { ··· 538 1052 } 539 1053 std_as_out_if0_desc.bInterfaceNumber = ret; 540 1054 std_as_out_if1_desc.bInterfaceNumber = ret; 541 - agdev->as_out_intf = ret; 542 - agdev->as_out_alt = 0; 1055 + uac2->as_out_intf = ret; 1056 + uac2->as_out_alt = 0; 543 1057 544 1058 ret = usb_interface_id(cfg, fn); 545 1059 if (ret < 0) { ··· 548 1062 } 549 1063 std_as_in_if0_desc.bInterfaceNumber = ret; 550 1064 std_as_in_if1_desc.bInterfaceNumber = ret; 551 - agdev->as_in_intf = ret; 552 - agdev->as_in_alt = 0; 1065 + uac2->as_in_intf = ret; 1066 + uac2->as_in_alt = 0; 1067 + 1068 + /* Calculate wMaxPacketSize according to audio bandwidth */ 1069 + set_ep_max_packet_size(uac2_opts, &fs_epin_desc, 1000, true); 1070 + set_ep_max_packet_size(uac2_opts, &fs_epout_desc, 1000, false); 1071 + set_ep_max_packet_size(uac2_opts, &hs_epin_desc, 8000, true); 1072 + set_ep_max_packet_size(uac2_opts, &hs_epout_desc, 8000, false); 553 1073 554 1074 agdev->out_ep = usb_ep_autoconfig(gadget, &fs_epout_desc); 555 1075 if (!agdev->out_ep) { ··· 569 1077 return ret; 570 1078 } 571 1079 572 - uac2->p_prm.uac2 = uac2; 573 - uac2->c_prm.uac2 = uac2; 574 - 575 - /* Calculate wMaxPacketSize according to audio bandwidth */ 576 - set_ep_max_packet_size(uac2_opts, &fs_epin_desc, 1000, true); 577 - set_ep_max_packet_size(uac2_opts, &fs_epout_desc, 1000, false); 578 - set_ep_max_packet_size(uac2_opts, &hs_epin_desc, 8000, true); 579 - set_ep_max_packet_size(uac2_opts, &hs_epout_desc, 8000, false); 1080 + agdev->in_ep_maxpsize = max(fs_epin_desc.wMaxPacketSize, 1081 + hs_epin_desc.wMaxPacketSize); 1082 + agdev->out_ep_maxpsize = max(fs_epout_desc.wMaxPacketSize, 1083 + hs_epout_desc.wMaxPacketSize); 580 1084 581 1085 hs_epout_desc.bEndpointAddress = fs_epout_desc.bEndpointAddress; 582 1086 hs_epin_desc.bEndpointAddress = fs_epin_desc.bEndpointAddress; ··· 582 1094 if (ret) 583 1095 return ret; 584 1096 585 - prm = &agdev->uac2.c_prm; 586 - prm->max_psize = hs_epout_desc.wMaxPacketSize; 587 - prm->ureq = kcalloc(uac2_opts->req_number, sizeof(struct uac2_req), 588 - GFP_KERNEL); 589 - if (!prm->ureq) { 590 - ret = -ENOMEM; 591 - goto err_free_descs; 592 - } 593 - prm->rbuf = kcalloc(uac2_opts->req_number, prm->max_psize, GFP_KERNEL); 594 - if (!prm->rbuf) { 595 - prm->max_psize = 0; 596 - ret = -ENOMEM; 597 - goto err_free_descs; 598 - } 1097 + agdev->gadget = gadget; 599 1098 600 - prm = &agdev->uac2.p_prm; 601 - prm->max_psize = hs_epin_desc.wMaxPacketSize; 602 - prm->ureq = kcalloc(uac2_opts->req_number, sizeof(struct uac2_req), 603 - GFP_KERNEL); 604 - if (!prm->ureq) { 605 - ret = -ENOMEM; 606 - goto err_free_descs; 607 - } 608 - prm->rbuf = kcalloc(uac2_opts->req_number, prm->max_psize, GFP_KERNEL); 609 - if (!prm->rbuf) { 610 - prm->max_psize = 0; 611 - ret = -ENOMEM; 612 - goto err_no_memory; 613 - } 614 - 615 - ret = alsa_uac2_init(agdev); 1099 + agdev->params.p_chmask = uac2_opts->p_chmask; 1100 + agdev->params.p_srate = uac2_opts->p_srate; 1101 + agdev->params.p_ssize = uac2_opts->p_ssize; 1102 + agdev->params.c_chmask = uac2_opts->c_chmask; 1103 + agdev->params.c_srate = uac2_opts->c_srate; 1104 + agdev->params.c_ssize = uac2_opts->c_ssize; 1105 + agdev->params.req_number = uac2_opts->req_number; 1106 + ret = g_audio_setup(agdev, "UAC2 PCM", "UAC2_Gadget"); 616 1107 if (ret) 617 - goto err_no_memory; 1108 + goto err_free_descs; 618 1109 return 0; 619 1110 620 - err_no_memory: 621 - kfree(agdev->uac2.p_prm.ureq); 622 - kfree(agdev->uac2.c_prm.ureq); 623 - kfree(agdev->uac2.p_prm.rbuf); 624 - kfree(agdev->uac2.c_prm.rbuf); 625 1111 err_free_descs: 626 1112 usb_free_all_descriptors(fn); 1113 + agdev->gadget = NULL; 627 1114 return ret; 628 1115 } 629 1116 ··· 606 1143 afunc_set_alt(struct usb_function *fn, unsigned intf, unsigned alt) 607 1144 { 608 1145 struct usb_composite_dev *cdev = fn->config->cdev; 609 - struct audio_dev *agdev = func_to_agdev(fn); 610 - struct f_uac2_opts *opts = agdev_to_uac2_opts(agdev); 611 - struct snd_uac2_chip *uac2 = &agdev->uac2; 1146 + struct f_uac2 *uac2 = func_to_uac2(fn); 612 1147 struct usb_gadget *gadget = cdev->gadget; 613 - struct device *dev = &uac2->pdev.dev; 614 - struct usb_request *req; 615 - struct usb_ep *ep; 616 - struct uac2_rtd_params *prm; 617 - int req_len, i; 1148 + struct device *dev = &gadget->dev; 1149 + int ret = 0; 618 1150 619 1151 /* No i/f has more than 2 alt settings */ 620 1152 if (alt > 1) { ··· 617 1159 return -EINVAL; 618 1160 } 619 1161 620 - if (intf == agdev->ac_intf) { 1162 + if (intf == uac2->ac_intf) { 621 1163 /* Control I/f has only 1 AltSetting - 0 */ 622 1164 if (alt) { 623 1165 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__); ··· 626 1168 return 0; 627 1169 } 628 1170 629 - if (intf == agdev->as_out_intf) { 630 - ep = agdev->out_ep; 631 - prm = &uac2->c_prm; 632 - config_ep_by_speed(gadget, fn, ep); 633 - agdev->as_out_alt = alt; 634 - req_len = prm->max_psize; 635 - } else if (intf == agdev->as_in_intf) { 636 - unsigned int factor, rate; 637 - struct usb_endpoint_descriptor *ep_desc; 1171 + if (intf == uac2->as_out_intf) { 1172 + uac2->as_out_alt = alt; 638 1173 639 - ep = agdev->in_ep; 640 - prm = &uac2->p_prm; 641 - config_ep_by_speed(gadget, fn, ep); 642 - agdev->as_in_alt = alt; 643 - 644 - /* pre-calculate the playback endpoint's interval */ 645 - if (gadget->speed == USB_SPEED_FULL) { 646 - ep_desc = &fs_epin_desc; 647 - factor = 1000; 648 - } else { 649 - ep_desc = &hs_epin_desc; 650 - factor = 8000; 651 - } 652 - 653 - /* pre-compute some values for iso_complete() */ 654 - uac2->p_framesize = opts->p_ssize * 655 - num_channels(opts->p_chmask); 656 - rate = opts->p_srate * uac2->p_framesize; 657 - uac2->p_interval = factor / (1 << (ep_desc->bInterval - 1)); 658 - uac2->p_pktsize = min_t(unsigned int, rate / uac2->p_interval, 659 - prm->max_psize); 660 - 661 - if (uac2->p_pktsize < prm->max_psize) 662 - uac2->p_pktsize_residue = rate % uac2->p_interval; 1174 + if (alt) 1175 + ret = u_audio_start_capture(&uac2->g_audio); 663 1176 else 664 - uac2->p_pktsize_residue = 0; 1177 + u_audio_stop_capture(&uac2->g_audio); 1178 + } else if (intf == uac2->as_in_intf) { 1179 + uac2->as_in_alt = alt; 665 1180 666 - req_len = uac2->p_pktsize; 667 - uac2->p_residue = 0; 1181 + if (alt) 1182 + ret = u_audio_start_playback(&uac2->g_audio); 1183 + else 1184 + u_audio_stop_playback(&uac2->g_audio); 668 1185 } else { 669 1186 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__); 670 1187 return -EINVAL; 671 1188 } 672 1189 673 - if (alt == 0) { 674 - free_ep(prm, ep); 675 - return 0; 676 - } 677 - 678 - prm->ep_enabled = true; 679 - usb_ep_enable(ep); 680 - 681 - for (i = 0; i < opts->req_number; i++) { 682 - if (!prm->ureq[i].req) { 683 - req = usb_ep_alloc_request(ep, GFP_ATOMIC); 684 - if (req == NULL) 685 - return -ENOMEM; 686 - 687 - prm->ureq[i].req = req; 688 - prm->ureq[i].pp = prm; 689 - 690 - req->zero = 0; 691 - req->context = &prm->ureq[i]; 692 - req->length = req_len; 693 - req->complete = agdev_iso_complete; 694 - req->buf = prm->rbuf + i * prm->max_psize; 695 - } 696 - 697 - if (usb_ep_queue(ep, prm->ureq[i].req, GFP_ATOMIC)) 698 - dev_err(dev, "%s:%d Error!\n", __func__, __LINE__); 699 - } 700 - 701 - return 0; 1190 + return ret; 702 1191 } 703 1192 704 1193 static int 705 1194 afunc_get_alt(struct usb_function *fn, unsigned intf) 706 1195 { 707 - struct audio_dev *agdev = func_to_agdev(fn); 708 - struct snd_uac2_chip *uac2 = &agdev->uac2; 1196 + struct f_uac2 *uac2 = func_to_uac2(fn); 1197 + struct g_audio *agdev = func_to_g_audio(fn); 709 1198 710 - if (intf == agdev->ac_intf) 711 - return agdev->ac_alt; 712 - else if (intf == agdev->as_out_intf) 713 - return agdev->as_out_alt; 714 - else if (intf == agdev->as_in_intf) 715 - return agdev->as_in_alt; 1199 + if (intf == uac2->ac_intf) 1200 + return uac2->ac_alt; 1201 + else if (intf == uac2->as_out_intf) 1202 + return uac2->as_out_alt; 1203 + else if (intf == uac2->as_in_intf) 1204 + return uac2->as_in_alt; 716 1205 else 717 - dev_err(&uac2->pdev.dev, 1206 + dev_err(&agdev->gadget->dev, 718 1207 "%s:%d Invalid Interface %d!\n", 719 1208 __func__, __LINE__, intf); 720 1209 ··· 671 1266 static void 672 1267 afunc_disable(struct usb_function *fn) 673 1268 { 674 - struct audio_dev *agdev = func_to_agdev(fn); 675 - struct snd_uac2_chip *uac2 = &agdev->uac2; 1269 + struct f_uac2 *uac2 = func_to_uac2(fn); 676 1270 677 - free_ep(&uac2->p_prm, agdev->in_ep); 678 - agdev->as_in_alt = 0; 679 - 680 - free_ep(&uac2->c_prm, agdev->out_ep); 681 - agdev->as_out_alt = 0; 1271 + uac2->as_in_alt = 0; 1272 + uac2->as_out_alt = 0; 1273 + u_audio_stop_capture(&uac2->g_audio); 1274 + u_audio_stop_playback(&uac2->g_audio); 682 1275 } 683 1276 684 1277 static int 685 1278 in_rq_cur(struct usb_function *fn, const struct usb_ctrlrequest *cr) 686 1279 { 687 1280 struct usb_request *req = fn->config->cdev->req; 688 - struct audio_dev *agdev = func_to_agdev(fn); 689 - struct snd_uac2_chip *uac2 = &agdev->uac2; 1281 + struct g_audio *agdev = func_to_g_audio(fn); 690 1282 struct f_uac2_opts *opts; 691 1283 u16 w_length = le16_to_cpu(cr->wLength); 692 1284 u16 w_index = le16_to_cpu(cr->wIndex); ··· 693 1291 int value = -EOPNOTSUPP; 694 1292 int p_srate, c_srate; 695 1293 696 - opts = agdev_to_uac2_opts(agdev); 1294 + opts = g_audio_to_uac2_opts(agdev); 697 1295 p_srate = opts->p_srate; 698 1296 c_srate = opts->c_srate; 699 1297 ··· 712 1310 *(u8 *)req->buf = 1; 713 1311 value = min_t(unsigned, w_length, 1); 714 1312 } else { 715 - dev_err(&uac2->pdev.dev, 1313 + dev_err(&agdev->gadget->dev, 716 1314 "%s:%d control_selector=%d TODO!\n", 717 1315 __func__, __LINE__, control_selector); 718 1316 } ··· 724 1322 in_rq_range(struct usb_function *fn, const struct usb_ctrlrequest *cr) 725 1323 { 726 1324 struct usb_request *req = fn->config->cdev->req; 727 - struct audio_dev *agdev = func_to_agdev(fn); 728 - struct snd_uac2_chip *uac2 = &agdev->uac2; 1325 + struct g_audio *agdev = func_to_g_audio(fn); 729 1326 struct f_uac2_opts *opts; 730 1327 u16 w_length = le16_to_cpu(cr->wLength); 731 1328 u16 w_index = le16_to_cpu(cr->wIndex); ··· 735 1334 int value = -EOPNOTSUPP; 736 1335 int p_srate, c_srate; 737 1336 738 - opts = agdev_to_uac2_opts(agdev); 1337 + opts = g_audio_to_uac2_opts(agdev); 739 1338 p_srate = opts->p_srate; 740 1339 c_srate = opts->c_srate; 741 1340 ··· 754 1353 value = min_t(unsigned, w_length, sizeof r); 755 1354 memcpy(req->buf, &r, value); 756 1355 } else { 757 - dev_err(&uac2->pdev.dev, 1356 + dev_err(&agdev->gadget->dev, 758 1357 "%s:%d control_selector=%d TODO!\n", 759 1358 __func__, __LINE__, control_selector); 760 1359 } ··· 789 1388 static int 790 1389 setup_rq_inf(struct usb_function *fn, const struct usb_ctrlrequest *cr) 791 1390 { 792 - struct audio_dev *agdev = func_to_agdev(fn); 793 - struct snd_uac2_chip *uac2 = &agdev->uac2; 1391 + struct f_uac2 *uac2 = func_to_uac2(fn); 1392 + struct g_audio *agdev = func_to_g_audio(fn); 794 1393 u16 w_index = le16_to_cpu(cr->wIndex); 795 1394 u8 intf = w_index & 0xff; 796 1395 797 - if (intf != agdev->ac_intf) { 798 - dev_err(&uac2->pdev.dev, 1396 + if (intf != uac2->ac_intf) { 1397 + dev_err(&agdev->gadget->dev, 799 1398 "%s:%d Error!\n", __func__, __LINE__); 800 1399 return -EOPNOTSUPP; 801 1400 } ··· 812 1411 afunc_setup(struct usb_function *fn, const struct usb_ctrlrequest *cr) 813 1412 { 814 1413 struct usb_composite_dev *cdev = fn->config->cdev; 815 - struct audio_dev *agdev = func_to_agdev(fn); 816 - struct snd_uac2_chip *uac2 = &agdev->uac2; 1414 + struct g_audio *agdev = func_to_g_audio(fn); 817 1415 struct usb_request *req = cdev->req; 818 1416 u16 w_length = le16_to_cpu(cr->wLength); 819 1417 int value = -EOPNOTSUPP; ··· 824 1424 if ((cr->bRequestType & USB_RECIP_MASK) == USB_RECIP_INTERFACE) 825 1425 value = setup_rq_inf(fn, cr); 826 1426 else 827 - dev_err(&uac2->pdev.dev, "%s:%d Error!\n", __func__, __LINE__); 1427 + dev_err(&agdev->gadget->dev, "%s:%d Error!\n", 1428 + __func__, __LINE__); 828 1429 829 1430 if (value >= 0) { 830 1431 req->length = value; 831 1432 req->zero = value < w_length; 832 1433 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC); 833 1434 if (value < 0) { 834 - dev_err(&uac2->pdev.dev, 1435 + dev_err(&agdev->gadget->dev, 835 1436 "%s:%d Error!\n", __func__, __LINE__); 836 1437 req->status = 0; 837 1438 } ··· 958 1557 959 1558 static void afunc_free(struct usb_function *f) 960 1559 { 961 - struct audio_dev *agdev; 1560 + struct g_audio *agdev; 962 1561 struct f_uac2_opts *opts; 963 1562 964 - agdev = func_to_agdev(f); 1563 + agdev = func_to_g_audio(f); 965 1564 opts = container_of(f->fi, struct f_uac2_opts, func_inst); 966 1565 kfree(agdev); 967 1566 mutex_lock(&opts->lock); ··· 971 1570 972 1571 static void afunc_unbind(struct usb_configuration *c, struct usb_function *f) 973 1572 { 974 - struct audio_dev *agdev = func_to_agdev(f); 975 - struct uac2_rtd_params *prm; 1573 + struct g_audio *agdev = func_to_g_audio(f); 976 1574 977 - alsa_uac2_exit(agdev); 978 - 979 - prm = &agdev->uac2.p_prm; 980 - kfree(prm->rbuf); 981 - 982 - prm = &agdev->uac2.c_prm; 983 - kfree(prm->rbuf); 984 - kfree(prm->ureq); 1575 + g_audio_cleanup(agdev); 985 1576 usb_free_all_descriptors(f); 1577 + 1578 + agdev->gadget = NULL; 986 1579 } 987 1580 988 1581 static struct usb_function *afunc_alloc(struct usb_function_instance *fi) 989 1582 { 990 - struct audio_dev *agdev; 1583 + struct f_uac2 *uac2; 991 1584 struct f_uac2_opts *opts; 992 1585 993 - agdev = kzalloc(sizeof(*agdev), GFP_KERNEL); 994 - if (agdev == NULL) 1586 + uac2 = kzalloc(sizeof(*uac2), GFP_KERNEL); 1587 + if (uac2 == NULL) 995 1588 return ERR_PTR(-ENOMEM); 996 1589 997 1590 opts = container_of(fi, struct f_uac2_opts, func_inst); ··· 993 1598 ++opts->refcnt; 994 1599 mutex_unlock(&opts->lock); 995 1600 996 - agdev->func.name = "uac2_func"; 997 - agdev->func.bind = afunc_bind; 998 - agdev->func.unbind = afunc_unbind; 999 - agdev->func.set_alt = afunc_set_alt; 1000 - agdev->func.get_alt = afunc_get_alt; 1001 - agdev->func.disable = afunc_disable; 1002 - agdev->func.setup = afunc_setup; 1003 - agdev->func.free_func = afunc_free; 1601 + uac2->g_audio.func.name = "uac2_func"; 1602 + uac2->g_audio.func.bind = afunc_bind; 1603 + uac2->g_audio.func.unbind = afunc_unbind; 1604 + uac2->g_audio.func.set_alt = afunc_set_alt; 1605 + uac2->g_audio.func.get_alt = afunc_get_alt; 1606 + uac2->g_audio.func.disable = afunc_disable; 1607 + uac2->g_audio.func.setup = afunc_setup; 1608 + uac2->g_audio.func.free_func = afunc_free; 1004 1609 1005 - return &agdev->func; 1610 + return &uac2->g_audio.func; 1006 1611 } 1007 1612 1008 1613 DECLARE_USB_FUNCTION_INIT(uac2, afunc_alloc_inst, afunc_alloc);
+5 -13
drivers/usb/gadget/function/storage_common.h
··· 133 133 #define FSG_MAX_LUNS 16 134 134 135 135 enum fsg_buffer_state { 136 + BUF_STATE_SENDING = -2, 137 + BUF_STATE_RECEIVING, 136 138 BUF_STATE_EMPTY = 0, 137 - BUF_STATE_FULL, 138 - BUF_STATE_BUSY 139 + BUF_STATE_FULL 139 140 }; 140 141 141 142 struct fsg_buffhd { ··· 152 151 unsigned int bulk_out_intended_length; 153 152 154 153 struct usb_request *inreq; 155 - int inreq_busy; 156 154 struct usb_request *outreq; 157 - int outreq_busy; 158 155 }; 159 156 160 157 enum fsg_state { 161 - /* This one isn't used anywhere */ 162 - FSG_STATE_COMMAND_PHASE = -10, 163 - FSG_STATE_DATA_PHASE, 164 - FSG_STATE_STATUS_PHASE, 165 - 166 - FSG_STATE_IDLE = 0, 158 + FSG_STATE_NORMAL, 167 159 FSG_STATE_ABORT_BULK_OUT, 168 - FSG_STATE_RESET, 169 - FSG_STATE_INTERFACE_CHANGE, 160 + FSG_STATE_PROTOCOL_RESET, 170 161 FSG_STATE_CONFIG_CHANGE, 171 - FSG_STATE_DISCONNECT, 172 162 FSG_STATE_EXIT, 173 163 FSG_STATE_TERMINATED 174 164 };
+662
drivers/usb/gadget/function/u_audio.c
··· 1 + /* 2 + * u_audio.c -- interface to USB gadget "ALSA sound card" utilities 3 + * 4 + * Copyright (C) 2016 5 + * Author: Ruslan Bilovol <ruslan.bilovol@gmail.com> 6 + * 7 + * Sound card implementation was cut-and-pasted with changes 8 + * from f_uac2.c and has: 9 + * Copyright (C) 2011 10 + * Yadwinder Singh (yadi.brar01@gmail.com) 11 + * Jaswinder Singh (jaswinder.singh@linaro.org) 12 + * 13 + * This program is free software; you can redistribute it and/or modify 14 + * it under the terms of the GNU General Public License as published by 15 + * the Free Software Foundation; either version 2 of the License, or 16 + * (at your option) any later version. 17 + * 18 + * This program is distributed in the hope that it will be useful, 19 + * but WITHOUT ANY WARRANTY; without even the implied warranty of 20 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 21 + * GNU General Public License for more details. 22 + */ 23 + 24 + #include <linux/module.h> 25 + #include <sound/core.h> 26 + #include <sound/pcm.h> 27 + #include <sound/pcm_params.h> 28 + 29 + #include "u_audio.h" 30 + 31 + #define BUFF_SIZE_MAX (PAGE_SIZE * 16) 32 + #define PRD_SIZE_MAX PAGE_SIZE 33 + #define MIN_PERIODS 4 34 + 35 + struct uac_req { 36 + struct uac_rtd_params *pp; /* parent param */ 37 + struct usb_request *req; 38 + }; 39 + 40 + /* Runtime data params for one stream */ 41 + struct uac_rtd_params { 42 + struct snd_uac_chip *uac; /* parent chip */ 43 + bool ep_enabled; /* if the ep is enabled */ 44 + /* Size of the ring buffer */ 45 + size_t dma_bytes; 46 + unsigned char *dma_area; 47 + 48 + struct snd_pcm_substream *ss; 49 + 50 + /* Ring buffer */ 51 + ssize_t hw_ptr; 52 + 53 + void *rbuf; 54 + 55 + size_t period_size; 56 + 57 + unsigned max_psize; /* MaxPacketSize of endpoint */ 58 + struct uac_req *ureq; 59 + 60 + spinlock_t lock; 61 + }; 62 + 63 + struct snd_uac_chip { 64 + struct g_audio *audio_dev; 65 + 66 + struct uac_rtd_params p_prm; 67 + struct uac_rtd_params c_prm; 68 + 69 + struct snd_card *card; 70 + struct snd_pcm *pcm; 71 + 72 + /* timekeeping for the playback endpoint */ 73 + unsigned int p_interval; 74 + unsigned int p_residue; 75 + 76 + /* pre-calculated values for playback iso completion */ 77 + unsigned int p_pktsize; 78 + unsigned int p_pktsize_residue; 79 + unsigned int p_framesize; 80 + }; 81 + 82 + static struct snd_pcm_hardware uac_pcm_hardware = { 83 + .info = SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER 84 + | SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID 85 + | SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME, 86 + .rates = SNDRV_PCM_RATE_CONTINUOUS, 87 + .periods_max = BUFF_SIZE_MAX / PRD_SIZE_MAX, 88 + .buffer_bytes_max = BUFF_SIZE_MAX, 89 + .period_bytes_max = PRD_SIZE_MAX, 90 + .periods_min = MIN_PERIODS, 91 + }; 92 + 93 + static void u_audio_iso_complete(struct usb_ep *ep, struct usb_request *req) 94 + { 95 + unsigned pending; 96 + unsigned long flags; 97 + unsigned int hw_ptr; 98 + bool update_alsa = false; 99 + int status = req->status; 100 + struct uac_req *ur = req->context; 101 + struct snd_pcm_substream *substream; 102 + struct uac_rtd_params *prm = ur->pp; 103 + struct snd_uac_chip *uac = prm->uac; 104 + 105 + /* i/f shutting down */ 106 + if (!prm->ep_enabled || req->status == -ESHUTDOWN) 107 + return; 108 + 109 + /* 110 + * We can't really do much about bad xfers. 111 + * Afterall, the ISOCH xfers could fail legitimately. 112 + */ 113 + if (status) 114 + pr_debug("%s: iso_complete status(%d) %d/%d\n", 115 + __func__, status, req->actual, req->length); 116 + 117 + substream = prm->ss; 118 + 119 + /* Do nothing if ALSA isn't active */ 120 + if (!substream) 121 + goto exit; 122 + 123 + spin_lock_irqsave(&prm->lock, flags); 124 + 125 + if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 126 + /* 127 + * For each IN packet, take the quotient of the current data 128 + * rate and the endpoint's interval as the base packet size. 129 + * If there is a residue from this division, add it to the 130 + * residue accumulator. 131 + */ 132 + req->length = uac->p_pktsize; 133 + uac->p_residue += uac->p_pktsize_residue; 134 + 135 + /* 136 + * Whenever there are more bytes in the accumulator than we 137 + * need to add one more sample frame, increase this packet's 138 + * size and decrease the accumulator. 139 + */ 140 + if (uac->p_residue / uac->p_interval >= uac->p_framesize) { 141 + req->length += uac->p_framesize; 142 + uac->p_residue -= uac->p_framesize * 143 + uac->p_interval; 144 + } 145 + 146 + req->actual = req->length; 147 + } 148 + 149 + pending = prm->hw_ptr % prm->period_size; 150 + pending += req->actual; 151 + if (pending >= prm->period_size) 152 + update_alsa = true; 153 + 154 + hw_ptr = prm->hw_ptr; 155 + prm->hw_ptr = (prm->hw_ptr + req->actual) % prm->dma_bytes; 156 + 157 + spin_unlock_irqrestore(&prm->lock, flags); 158 + 159 + /* Pack USB load in ALSA ring buffer */ 160 + pending = prm->dma_bytes - hw_ptr; 161 + 162 + if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 163 + if (unlikely(pending < req->actual)) { 164 + memcpy(req->buf, prm->dma_area + hw_ptr, pending); 165 + memcpy(req->buf + pending, prm->dma_area, 166 + req->actual - pending); 167 + } else { 168 + memcpy(req->buf, prm->dma_area + hw_ptr, req->actual); 169 + } 170 + } else { 171 + if (unlikely(pending < req->actual)) { 172 + memcpy(prm->dma_area + hw_ptr, req->buf, pending); 173 + memcpy(prm->dma_area, req->buf + pending, 174 + req->actual - pending); 175 + } else { 176 + memcpy(prm->dma_area + hw_ptr, req->buf, req->actual); 177 + } 178 + } 179 + 180 + exit: 181 + if (usb_ep_queue(ep, req, GFP_ATOMIC)) 182 + dev_err(uac->card->dev, "%d Error!\n", __LINE__); 183 + 184 + if (update_alsa) 185 + snd_pcm_period_elapsed(substream); 186 + } 187 + 188 + static int uac_pcm_trigger(struct snd_pcm_substream *substream, int cmd) 189 + { 190 + struct snd_uac_chip *uac = snd_pcm_substream_chip(substream); 191 + struct uac_rtd_params *prm; 192 + struct g_audio *audio_dev; 193 + struct uac_params *params; 194 + unsigned long flags; 195 + int err = 0; 196 + 197 + audio_dev = uac->audio_dev; 198 + params = &audio_dev->params; 199 + 200 + if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 201 + prm = &uac->p_prm; 202 + else 203 + prm = &uac->c_prm; 204 + 205 + spin_lock_irqsave(&prm->lock, flags); 206 + 207 + /* Reset */ 208 + prm->hw_ptr = 0; 209 + 210 + switch (cmd) { 211 + case SNDRV_PCM_TRIGGER_START: 212 + case SNDRV_PCM_TRIGGER_RESUME: 213 + prm->ss = substream; 214 + break; 215 + case SNDRV_PCM_TRIGGER_STOP: 216 + case SNDRV_PCM_TRIGGER_SUSPEND: 217 + prm->ss = NULL; 218 + break; 219 + default: 220 + err = -EINVAL; 221 + } 222 + 223 + spin_unlock_irqrestore(&prm->lock, flags); 224 + 225 + /* Clear buffer after Play stops */ 226 + if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && !prm->ss) 227 + memset(prm->rbuf, 0, prm->max_psize * params->req_number); 228 + 229 + return err; 230 + } 231 + 232 + static snd_pcm_uframes_t uac_pcm_pointer(struct snd_pcm_substream *substream) 233 + { 234 + struct snd_uac_chip *uac = snd_pcm_substream_chip(substream); 235 + struct uac_rtd_params *prm; 236 + 237 + if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 238 + prm = &uac->p_prm; 239 + else 240 + prm = &uac->c_prm; 241 + 242 + return bytes_to_frames(substream->runtime, prm->hw_ptr); 243 + } 244 + 245 + static int uac_pcm_hw_params(struct snd_pcm_substream *substream, 246 + struct snd_pcm_hw_params *hw_params) 247 + { 248 + struct snd_uac_chip *uac = snd_pcm_substream_chip(substream); 249 + struct uac_rtd_params *prm; 250 + int err; 251 + 252 + if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 253 + prm = &uac->p_prm; 254 + else 255 + prm = &uac->c_prm; 256 + 257 + err = snd_pcm_lib_malloc_pages(substream, 258 + params_buffer_bytes(hw_params)); 259 + if (err >= 0) { 260 + prm->dma_bytes = substream->runtime->dma_bytes; 261 + prm->dma_area = substream->runtime->dma_area; 262 + prm->period_size = params_period_bytes(hw_params); 263 + } 264 + 265 + return err; 266 + } 267 + 268 + static int uac_pcm_hw_free(struct snd_pcm_substream *substream) 269 + { 270 + struct snd_uac_chip *uac = snd_pcm_substream_chip(substream); 271 + struct uac_rtd_params *prm; 272 + 273 + if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 274 + prm = &uac->p_prm; 275 + else 276 + prm = &uac->c_prm; 277 + 278 + prm->dma_area = NULL; 279 + prm->dma_bytes = 0; 280 + prm->period_size = 0; 281 + 282 + return snd_pcm_lib_free_pages(substream); 283 + } 284 + 285 + static int uac_pcm_open(struct snd_pcm_substream *substream) 286 + { 287 + struct snd_uac_chip *uac = snd_pcm_substream_chip(substream); 288 + struct snd_pcm_runtime *runtime = substream->runtime; 289 + struct g_audio *audio_dev; 290 + struct uac_params *params; 291 + int p_ssize, c_ssize; 292 + int p_srate, c_srate; 293 + int p_chmask, c_chmask; 294 + 295 + audio_dev = uac->audio_dev; 296 + params = &audio_dev->params; 297 + p_ssize = params->p_ssize; 298 + c_ssize = params->c_ssize; 299 + p_srate = params->p_srate; 300 + c_srate = params->c_srate; 301 + p_chmask = params->p_chmask; 302 + c_chmask = params->c_chmask; 303 + uac->p_residue = 0; 304 + 305 + runtime->hw = uac_pcm_hardware; 306 + 307 + if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 308 + spin_lock_init(&uac->p_prm.lock); 309 + runtime->hw.rate_min = p_srate; 310 + switch (p_ssize) { 311 + case 3: 312 + runtime->hw.formats = SNDRV_PCM_FMTBIT_S24_3LE; 313 + break; 314 + case 4: 315 + runtime->hw.formats = SNDRV_PCM_FMTBIT_S32_LE; 316 + break; 317 + default: 318 + runtime->hw.formats = SNDRV_PCM_FMTBIT_S16_LE; 319 + break; 320 + } 321 + runtime->hw.channels_min = num_channels(p_chmask); 322 + runtime->hw.period_bytes_min = 2 * uac->p_prm.max_psize 323 + / runtime->hw.periods_min; 324 + } else { 325 + spin_lock_init(&uac->c_prm.lock); 326 + runtime->hw.rate_min = c_srate; 327 + switch (c_ssize) { 328 + case 3: 329 + runtime->hw.formats = SNDRV_PCM_FMTBIT_S24_3LE; 330 + break; 331 + case 4: 332 + runtime->hw.formats = SNDRV_PCM_FMTBIT_S32_LE; 333 + break; 334 + default: 335 + runtime->hw.formats = SNDRV_PCM_FMTBIT_S16_LE; 336 + break; 337 + } 338 + runtime->hw.channels_min = num_channels(c_chmask); 339 + runtime->hw.period_bytes_min = 2 * uac->c_prm.max_psize 340 + / runtime->hw.periods_min; 341 + } 342 + 343 + runtime->hw.rate_max = runtime->hw.rate_min; 344 + runtime->hw.channels_max = runtime->hw.channels_min; 345 + 346 + snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS); 347 + 348 + return 0; 349 + } 350 + 351 + /* ALSA cries without these function pointers */ 352 + static int uac_pcm_null(struct snd_pcm_substream *substream) 353 + { 354 + return 0; 355 + } 356 + 357 + static struct snd_pcm_ops uac_pcm_ops = { 358 + .open = uac_pcm_open, 359 + .close = uac_pcm_null, 360 + .ioctl = snd_pcm_lib_ioctl, 361 + .hw_params = uac_pcm_hw_params, 362 + .hw_free = uac_pcm_hw_free, 363 + .trigger = uac_pcm_trigger, 364 + .pointer = uac_pcm_pointer, 365 + .prepare = uac_pcm_null, 366 + }; 367 + 368 + static inline void free_ep(struct uac_rtd_params *prm, struct usb_ep *ep) 369 + { 370 + struct snd_uac_chip *uac = prm->uac; 371 + struct g_audio *audio_dev; 372 + struct uac_params *params; 373 + int i; 374 + 375 + if (!prm->ep_enabled) 376 + return; 377 + 378 + prm->ep_enabled = false; 379 + 380 + audio_dev = uac->audio_dev; 381 + params = &audio_dev->params; 382 + 383 + for (i = 0; i < params->req_number; i++) { 384 + if (prm->ureq[i].req) { 385 + usb_ep_dequeue(ep, prm->ureq[i].req); 386 + usb_ep_free_request(ep, prm->ureq[i].req); 387 + prm->ureq[i].req = NULL; 388 + } 389 + } 390 + 391 + if (usb_ep_disable(ep)) 392 + dev_err(uac->card->dev, "%s:%d Error!\n", __func__, __LINE__); 393 + } 394 + 395 + 396 + int u_audio_start_capture(struct g_audio *audio_dev) 397 + { 398 + struct snd_uac_chip *uac = audio_dev->uac; 399 + struct usb_gadget *gadget = audio_dev->gadget; 400 + struct device *dev = &gadget->dev; 401 + struct usb_request *req; 402 + struct usb_ep *ep; 403 + struct uac_rtd_params *prm; 404 + struct uac_params *params = &audio_dev->params; 405 + int req_len, i; 406 + 407 + ep = audio_dev->out_ep; 408 + prm = &uac->c_prm; 409 + config_ep_by_speed(gadget, &audio_dev->func, ep); 410 + req_len = prm->max_psize; 411 + 412 + prm->ep_enabled = true; 413 + usb_ep_enable(ep); 414 + 415 + for (i = 0; i < params->req_number; i++) { 416 + if (!prm->ureq[i].req) { 417 + req = usb_ep_alloc_request(ep, GFP_ATOMIC); 418 + if (req == NULL) 419 + return -ENOMEM; 420 + 421 + prm->ureq[i].req = req; 422 + prm->ureq[i].pp = prm; 423 + 424 + req->zero = 0; 425 + req->context = &prm->ureq[i]; 426 + req->length = req_len; 427 + req->complete = u_audio_iso_complete; 428 + req->buf = prm->rbuf + i * prm->max_psize; 429 + } 430 + 431 + if (usb_ep_queue(ep, prm->ureq[i].req, GFP_ATOMIC)) 432 + dev_err(dev, "%s:%d Error!\n", __func__, __LINE__); 433 + } 434 + 435 + return 0; 436 + } 437 + EXPORT_SYMBOL_GPL(u_audio_start_capture); 438 + 439 + void u_audio_stop_capture(struct g_audio *audio_dev) 440 + { 441 + struct snd_uac_chip *uac = audio_dev->uac; 442 + 443 + free_ep(&uac->c_prm, audio_dev->out_ep); 444 + } 445 + EXPORT_SYMBOL_GPL(u_audio_stop_capture); 446 + 447 + int u_audio_start_playback(struct g_audio *audio_dev) 448 + { 449 + struct snd_uac_chip *uac = audio_dev->uac; 450 + struct usb_gadget *gadget = audio_dev->gadget; 451 + struct device *dev = &gadget->dev; 452 + struct usb_request *req; 453 + struct usb_ep *ep; 454 + struct uac_rtd_params *prm; 455 + struct uac_params *params = &audio_dev->params; 456 + unsigned int factor, rate; 457 + const struct usb_endpoint_descriptor *ep_desc; 458 + int req_len, i; 459 + 460 + ep = audio_dev->in_ep; 461 + prm = &uac->p_prm; 462 + config_ep_by_speed(gadget, &audio_dev->func, ep); 463 + 464 + ep_desc = ep->desc; 465 + 466 + /* pre-calculate the playback endpoint's interval */ 467 + if (gadget->speed == USB_SPEED_FULL) 468 + factor = 1000; 469 + else 470 + factor = 8000; 471 + 472 + /* pre-compute some values for iso_complete() */ 473 + uac->p_framesize = params->p_ssize * 474 + num_channels(params->p_chmask); 475 + rate = params->p_srate * uac->p_framesize; 476 + uac->p_interval = factor / (1 << (ep_desc->bInterval - 1)); 477 + uac->p_pktsize = min_t(unsigned int, rate / uac->p_interval, 478 + prm->max_psize); 479 + 480 + if (uac->p_pktsize < prm->max_psize) 481 + uac->p_pktsize_residue = rate % uac->p_interval; 482 + else 483 + uac->p_pktsize_residue = 0; 484 + 485 + req_len = uac->p_pktsize; 486 + uac->p_residue = 0; 487 + 488 + prm->ep_enabled = true; 489 + usb_ep_enable(ep); 490 + 491 + for (i = 0; i < params->req_number; i++) { 492 + if (!prm->ureq[i].req) { 493 + req = usb_ep_alloc_request(ep, GFP_ATOMIC); 494 + if (req == NULL) 495 + return -ENOMEM; 496 + 497 + prm->ureq[i].req = req; 498 + prm->ureq[i].pp = prm; 499 + 500 + req->zero = 0; 501 + req->context = &prm->ureq[i]; 502 + req->length = req_len; 503 + req->complete = u_audio_iso_complete; 504 + req->buf = prm->rbuf + i * prm->max_psize; 505 + } 506 + 507 + if (usb_ep_queue(ep, prm->ureq[i].req, GFP_ATOMIC)) 508 + dev_err(dev, "%s:%d Error!\n", __func__, __LINE__); 509 + } 510 + 511 + return 0; 512 + } 513 + EXPORT_SYMBOL_GPL(u_audio_start_playback); 514 + 515 + void u_audio_stop_playback(struct g_audio *audio_dev) 516 + { 517 + struct snd_uac_chip *uac = audio_dev->uac; 518 + 519 + free_ep(&uac->p_prm, audio_dev->in_ep); 520 + } 521 + EXPORT_SYMBOL_GPL(u_audio_stop_playback); 522 + 523 + int g_audio_setup(struct g_audio *g_audio, const char *pcm_name, 524 + const char *card_name) 525 + { 526 + struct snd_uac_chip *uac; 527 + struct snd_card *card; 528 + struct snd_pcm *pcm; 529 + struct uac_params *params; 530 + int p_chmask, c_chmask; 531 + int err; 532 + 533 + if (!g_audio) 534 + return -EINVAL; 535 + 536 + uac = kzalloc(sizeof(*uac), GFP_KERNEL); 537 + if (!uac) 538 + return -ENOMEM; 539 + g_audio->uac = uac; 540 + uac->audio_dev = g_audio; 541 + 542 + params = &g_audio->params; 543 + p_chmask = params->p_chmask; 544 + c_chmask = params->c_chmask; 545 + 546 + if (c_chmask) { 547 + struct uac_rtd_params *prm = &uac->c_prm; 548 + 549 + uac->c_prm.uac = uac; 550 + prm->max_psize = g_audio->out_ep_maxpsize; 551 + 552 + prm->ureq = kcalloc(params->req_number, sizeof(struct uac_req), 553 + GFP_KERNEL); 554 + if (!prm->ureq) { 555 + err = -ENOMEM; 556 + goto fail; 557 + } 558 + 559 + prm->rbuf = kcalloc(params->req_number, prm->max_psize, 560 + GFP_KERNEL); 561 + if (!prm->rbuf) { 562 + prm->max_psize = 0; 563 + err = -ENOMEM; 564 + goto fail; 565 + } 566 + } 567 + 568 + if (p_chmask) { 569 + struct uac_rtd_params *prm = &uac->p_prm; 570 + 571 + uac->p_prm.uac = uac; 572 + prm->max_psize = g_audio->in_ep_maxpsize; 573 + 574 + prm->ureq = kcalloc(params->req_number, sizeof(struct uac_req), 575 + GFP_KERNEL); 576 + if (!prm->ureq) { 577 + err = -ENOMEM; 578 + goto fail; 579 + } 580 + 581 + prm->rbuf = kcalloc(params->req_number, prm->max_psize, 582 + GFP_KERNEL); 583 + if (!prm->rbuf) { 584 + prm->max_psize = 0; 585 + err = -ENOMEM; 586 + goto fail; 587 + } 588 + } 589 + 590 + /* Choose any slot, with no id */ 591 + err = snd_card_new(&g_audio->gadget->dev, 592 + -1, NULL, THIS_MODULE, 0, &card); 593 + if (err < 0) 594 + goto fail; 595 + 596 + uac->card = card; 597 + 598 + /* 599 + * Create first PCM device 600 + * Create a substream only for non-zero channel streams 601 + */ 602 + err = snd_pcm_new(uac->card, pcm_name, 0, 603 + p_chmask ? 1 : 0, c_chmask ? 1 : 0, &pcm); 604 + if (err < 0) 605 + goto snd_fail; 606 + 607 + strcpy(pcm->name, pcm_name); 608 + pcm->private_data = uac; 609 + uac->pcm = pcm; 610 + 611 + snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &uac_pcm_ops); 612 + snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &uac_pcm_ops); 613 + 614 + strcpy(card->driver, card_name); 615 + strcpy(card->shortname, card_name); 616 + sprintf(card->longname, "%s %i", card_name, card->dev->id); 617 + 618 + snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS, 619 + snd_dma_continuous_data(GFP_KERNEL), 0, BUFF_SIZE_MAX); 620 + 621 + err = snd_card_register(card); 622 + 623 + if (!err) 624 + return 0; 625 + 626 + snd_fail: 627 + snd_card_free(card); 628 + fail: 629 + kfree(uac->p_prm.ureq); 630 + kfree(uac->c_prm.ureq); 631 + kfree(uac->p_prm.rbuf); 632 + kfree(uac->c_prm.rbuf); 633 + kfree(uac); 634 + 635 + return err; 636 + } 637 + EXPORT_SYMBOL_GPL(g_audio_setup); 638 + 639 + void g_audio_cleanup(struct g_audio *g_audio) 640 + { 641 + struct snd_uac_chip *uac; 642 + struct snd_card *card; 643 + 644 + if (!g_audio || !g_audio->uac) 645 + return; 646 + 647 + uac = g_audio->uac; 648 + card = uac->card; 649 + if (card) 650 + snd_card_free(card); 651 + 652 + kfree(uac->p_prm.ureq); 653 + kfree(uac->c_prm.ureq); 654 + kfree(uac->p_prm.rbuf); 655 + kfree(uac->c_prm.rbuf); 656 + kfree(uac); 657 + } 658 + EXPORT_SYMBOL_GPL(g_audio_cleanup); 659 + 660 + MODULE_LICENSE("GPL"); 661 + MODULE_DESCRIPTION("USB gadget \"ALSA sound card\" utilities"); 662 + MODULE_AUTHOR("Ruslan Bilovol");
+95
drivers/usb/gadget/function/u_audio.h
··· 1 + /* 2 + * u_audio.h -- interface to USB gadget "ALSA sound card" utilities 3 + * 4 + * Copyright (C) 2016 5 + * Author: Ruslan Bilovol <ruslan.bilovol@gmail.com> 6 + * 7 + * This program is free software; you can redistribute it and/or modify 8 + * it under the terms of the GNU General Public License as published by 9 + * the Free Software Foundation; either version 2 of the License, or 10 + * (at your option) any later version. 11 + * 12 + * This program is distributed in the hope that it will be useful, 13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 + * GNU General Public License for more details. 16 + * 17 + */ 18 + 19 + #ifndef __U_AUDIO_H 20 + #define __U_AUDIO_H 21 + 22 + #include <linux/usb/composite.h> 23 + 24 + struct uac_params { 25 + /* playback */ 26 + int p_chmask; /* channel mask */ 27 + int p_srate; /* rate in Hz */ 28 + int p_ssize; /* sample size */ 29 + 30 + /* capture */ 31 + int c_chmask; /* channel mask */ 32 + int c_srate; /* rate in Hz */ 33 + int c_ssize; /* sample size */ 34 + 35 + int req_number; /* number of preallocated requests */ 36 + }; 37 + 38 + struct g_audio { 39 + struct usb_function func; 40 + struct usb_gadget *gadget; 41 + 42 + struct usb_ep *in_ep; 43 + struct usb_ep *out_ep; 44 + 45 + /* Max packet size for all in_ep possible speeds */ 46 + unsigned int in_ep_maxpsize; 47 + /* Max packet size for all out_ep possible speeds */ 48 + unsigned int out_ep_maxpsize; 49 + 50 + /* The ALSA Sound Card it represents on the USB-Client side */ 51 + struct snd_uac_chip *uac; 52 + 53 + struct uac_params params; 54 + }; 55 + 56 + static inline struct g_audio *func_to_g_audio(struct usb_function *f) 57 + { 58 + return container_of(f, struct g_audio, func); 59 + } 60 + 61 + static inline uint num_channels(uint chanmask) 62 + { 63 + uint num = 0; 64 + 65 + while (chanmask) { 66 + num += (chanmask & 1); 67 + chanmask >>= 1; 68 + } 69 + 70 + return num; 71 + } 72 + 73 + /* 74 + * g_audio_setup - initialize one virtual ALSA sound card 75 + * @g_audio: struct with filled params, in_ep_maxpsize, out_ep_maxpsize 76 + * @pcm_name: the id string for a PCM instance of this sound card 77 + * @card_name: name of this soundcard 78 + * 79 + * This sets up the single virtual ALSA sound card that may be exported by a 80 + * gadget driver using this framework. 81 + * 82 + * Context: may sleep 83 + * 84 + * Returns zero on success, or a negative error on failure. 85 + */ 86 + int g_audio_setup(struct g_audio *g_audio, const char *pcm_name, 87 + const char *card_name); 88 + void g_audio_cleanup(struct g_audio *g_audio); 89 + 90 + int u_audio_start_capture(struct g_audio *g_audio); 91 + void u_audio_stop_capture(struct g_audio *g_audio); 92 + int u_audio_start_playback(struct g_audio *g_audio); 93 + void u_audio_stop_playback(struct g_audio *g_audio); 94 + 95 + #endif /* __U_AUDIO_H */
+3
drivers/usb/gadget/function/u_fs.h
··· 216 216 #define FFS_FL_CALL_CLOSED_CALLBACK 0 217 217 #define FFS_FL_BOUND 1 218 218 219 + /* For waking up blocked threads when function is enabled. */ 220 + wait_queue_head_t wait; 221 + 219 222 /* Active function */ 220 223 struct ffs_function *func; 221 224
+4 -3
drivers/usb/gadget/function/u_uac1.c drivers/usb/gadget/function/u_uac1_legacy.c
··· 18 18 #include <linux/random.h> 19 19 #include <linux/syscalls.h> 20 20 21 - #include "u_uac1.h" 21 + #include "u_uac1_legacy.h" 22 22 23 23 /* 24 24 * This component encapsulates the ALSA devices for USB audio gadget ··· 205 205 { 206 206 struct snd_pcm_file *pcm_file; 207 207 struct gaudio_snd_dev *snd; 208 - struct f_uac1_opts *opts; 208 + struct f_uac1_legacy_opts *opts; 209 209 char *fn_play, *fn_cap, *fn_cntl; 210 210 211 - opts = container_of(card->func.fi, struct f_uac1_opts, func_inst); 211 + opts = container_of(card->func.fi, struct f_uac1_legacy_opts, 212 + func_inst); 212 213 fn_play = opts->fn_play; 213 214 fn_cap = opts->fn_cap; 214 215 fn_cntl = opts->fn_cntl;
+23 -64
drivers/usb/gadget/function/u_uac1.h
··· 1 1 /* 2 - * u_uac1.h -- interface to USB gadget "ALSA AUDIO" utilities 2 + * u_uac1.h - Utility definitions for UAC1 function 3 3 * 4 - * Copyright (C) 2008 Bryan Wu <cooloney@kernel.org> 5 - * Copyright (C) 2008 Analog Devices, Inc 4 + * Copyright (C) 2016 Ruslan Bilovol <ruslan.bilovol@gmail.com> 6 5 * 7 - * Enter bugs at http://blackfin.uclinux.org/ 8 - * 9 - * Licensed under the GPL-2 or later. 6 + * This program is free software; you can redistribute it and/or modify 7 + * it under the terms of the GNU General Public License version 2 as 8 + * published by the Free Software Foundation. 10 9 */ 11 10 12 - #ifndef __U_AUDIO_H 13 - #define __U_AUDIO_H 11 + #ifndef __U_UAC1_H 12 + #define __U_UAC1_H 14 13 15 - #include <linux/device.h> 16 - #include <linux/err.h> 17 - #include <linux/usb/audio.h> 18 14 #include <linux/usb/composite.h> 19 15 20 - #include <sound/core.h> 21 - #include <sound/pcm.h> 22 - #include <sound/pcm_params.h> 23 - 24 - #define FILE_PCM_PLAYBACK "/dev/snd/pcmC0D0p" 25 - #define FILE_PCM_CAPTURE "/dev/snd/pcmC0D0c" 26 - #define FILE_CONTROL "/dev/snd/controlC0" 27 - 28 16 #define UAC1_OUT_EP_MAX_PACKET_SIZE 200 29 - #define UAC1_REQ_COUNT 256 30 - #define UAC1_AUDIO_BUF_SIZE 48000 17 + #define UAC1_DEF_CCHMASK 0x3 18 + #define UAC1_DEF_CSRATE 48000 19 + #define UAC1_DEF_CSSIZE 2 20 + #define UAC1_DEF_PCHMASK 0x3 21 + #define UAC1_DEF_PSRATE 48000 22 + #define UAC1_DEF_PSSIZE 2 23 + #define UAC1_DEF_REQ_NUM 2 31 24 32 - /* 33 - * This represents the USB side of an audio card device, managed by a USB 34 - * function which provides control and stream interfaces. 35 - */ 36 - 37 - struct gaudio_snd_dev { 38 - struct gaudio *card; 39 - struct file *filp; 40 - struct snd_pcm_substream *substream; 41 - int access; 42 - int format; 43 - int channels; 44 - int rate; 45 - }; 46 - 47 - struct gaudio { 48 - struct usb_function func; 49 - struct usb_gadget *gadget; 50 - 51 - /* ALSA sound device interfaces */ 52 - struct gaudio_snd_dev control; 53 - struct gaudio_snd_dev playback; 54 - struct gaudio_snd_dev capture; 55 - 56 - /* TODO */ 57 - }; 58 25 59 26 struct f_uac1_opts { 60 27 struct usb_function_instance func_inst; 61 - int req_buf_size; 62 - int req_count; 63 - int audio_buf_size; 64 - char *fn_play; 65 - char *fn_cap; 66 - char *fn_cntl; 28 + int c_chmask; 29 + int c_srate; 30 + int c_ssize; 31 + int p_chmask; 32 + int p_srate; 33 + int p_ssize; 34 + int req_number; 67 35 unsigned bound:1; 68 - unsigned fn_play_alloc:1; 69 - unsigned fn_cap_alloc:1; 70 - unsigned fn_cntl_alloc:1; 36 + 71 37 struct mutex lock; 72 38 int refcnt; 73 39 }; 74 40 75 - int gaudio_setup(struct gaudio *card); 76 - void gaudio_cleanup(struct gaudio *the_card); 77 - 78 - size_t u_audio_playback(struct gaudio *card, void *buf, size_t count); 79 - int u_audio_get_playback_channels(struct gaudio *card); 80 - int u_audio_get_playback_rate(struct gaudio *card); 81 - 82 - #endif /* __U_AUDIO_H */ 41 + #endif /* __U_UAC1_H */
+82
drivers/usb/gadget/function/u_uac1_legacy.h
··· 1 + /* 2 + * u_uac1.h -- interface to USB gadget "ALSA AUDIO" utilities 3 + * 4 + * Copyright (C) 2008 Bryan Wu <cooloney@kernel.org> 5 + * Copyright (C) 2008 Analog Devices, Inc 6 + * 7 + * Enter bugs at http://blackfin.uclinux.org/ 8 + * 9 + * Licensed under the GPL-2 or later. 10 + */ 11 + 12 + #ifndef __U_UAC1_LEGACY_H 13 + #define __U_UAC1_LEGACY_H 14 + 15 + #include <linux/device.h> 16 + #include <linux/err.h> 17 + #include <linux/usb/audio.h> 18 + #include <linux/usb/composite.h> 19 + 20 + #include <sound/core.h> 21 + #include <sound/pcm.h> 22 + #include <sound/pcm_params.h> 23 + 24 + #define FILE_PCM_PLAYBACK "/dev/snd/pcmC0D0p" 25 + #define FILE_PCM_CAPTURE "/dev/snd/pcmC0D0c" 26 + #define FILE_CONTROL "/dev/snd/controlC0" 27 + 28 + #define UAC1_OUT_EP_MAX_PACKET_SIZE 200 29 + #define UAC1_REQ_COUNT 256 30 + #define UAC1_AUDIO_BUF_SIZE 48000 31 + 32 + /* 33 + * This represents the USB side of an audio card device, managed by a USB 34 + * function which provides control and stream interfaces. 35 + */ 36 + 37 + struct gaudio_snd_dev { 38 + struct gaudio *card; 39 + struct file *filp; 40 + struct snd_pcm_substream *substream; 41 + int access; 42 + int format; 43 + int channels; 44 + int rate; 45 + }; 46 + 47 + struct gaudio { 48 + struct usb_function func; 49 + struct usb_gadget *gadget; 50 + 51 + /* ALSA sound device interfaces */ 52 + struct gaudio_snd_dev control; 53 + struct gaudio_snd_dev playback; 54 + struct gaudio_snd_dev capture; 55 + 56 + /* TODO */ 57 + }; 58 + 59 + struct f_uac1_legacy_opts { 60 + struct usb_function_instance func_inst; 61 + int req_buf_size; 62 + int req_count; 63 + int audio_buf_size; 64 + char *fn_play; 65 + char *fn_cap; 66 + char *fn_cntl; 67 + unsigned bound:1; 68 + unsigned fn_play_alloc:1; 69 + unsigned fn_cap_alloc:1; 70 + unsigned fn_cntl_alloc:1; 71 + struct mutex lock; 72 + int refcnt; 73 + }; 74 + 75 + int gaudio_setup(struct gaudio *card); 76 + void gaudio_cleanup(struct gaudio *the_card); 77 + 78 + size_t u_audio_playback(struct gaudio *card, void *buf, size_t count); 79 + int u_audio_get_playback_channels(struct gaudio *card); 80 + int u_audio_get_playback_rate(struct gaudio *card); 81 + 82 + #endif /* __U_UAC1_LEGACY_H */
+12 -3
drivers/usb/gadget/legacy/Kconfig
··· 54 54 depends on SND 55 55 select USB_LIBCOMPOSITE 56 56 select SND_PCM 57 - select USB_F_UAC1 if GADGET_UAC1 57 + select USB_F_UAC1 if (GADGET_UAC1 && !GADGET_UAC1_LEGACY) 58 + select USB_F_UAC1_LEGACY if (GADGET_UAC1 && GADGET_UAC1_LEGACY) 58 59 select USB_F_UAC2 if !GADGET_UAC1 60 + select USB_U_AUDIO if (USB_F_UAC2 || USB_F_UAC1) 59 61 help 60 62 This Gadget Audio driver is compatible with USB Audio Class 61 63 specification 2.0. It implements 1 AudioControl interface, ··· 75 73 dynamically linked module called "g_audio". 76 74 77 75 config GADGET_UAC1 78 - bool "UAC 1.0 (Legacy)" 76 + bool "UAC 1.0" 79 77 depends on USB_AUDIO 80 78 help 81 - If you instead want older UAC Spec-1.0 driver that also has audio 79 + If you instead want older USB Audio Class specification 1.0 support 80 + with similar driver capabilities. 81 + 82 + config GADGET_UAC1_LEGACY 83 + bool "UAC 1.0 (Legacy)" 84 + depends on GADGET_UAC1 85 + help 86 + If you instead want legacy UAC Spec-1.0 driver that also has audio 82 87 paths hardwired to the Audio codec chip on-board and doesn't work 83 88 without one. 84 89
+54 -1
drivers/usb/gadget/legacy/audio.c
··· 53 53 module_param(c_ssize, uint, S_IRUGO); 54 54 MODULE_PARM_DESC(c_ssize, "Capture Sample Size(bytes)"); 55 55 #else 56 + #ifndef CONFIG_GADGET_UAC1_LEGACY 56 57 #include "u_uac1.h" 58 + 59 + /* Playback(USB-IN) Default Stereo - Fl/Fr */ 60 + static int p_chmask = UAC1_DEF_PCHMASK; 61 + module_param(p_chmask, uint, S_IRUGO); 62 + MODULE_PARM_DESC(p_chmask, "Playback Channel Mask"); 63 + 64 + /* Playback Default 48 KHz */ 65 + static int p_srate = UAC1_DEF_PSRATE; 66 + module_param(p_srate, uint, S_IRUGO); 67 + MODULE_PARM_DESC(p_srate, "Playback Sampling Rate"); 68 + 69 + /* Playback Default 16bits/sample */ 70 + static int p_ssize = UAC1_DEF_PSSIZE; 71 + module_param(p_ssize, uint, S_IRUGO); 72 + MODULE_PARM_DESC(p_ssize, "Playback Sample Size(bytes)"); 73 + 74 + /* Capture(USB-OUT) Default Stereo - Fl/Fr */ 75 + static int c_chmask = UAC1_DEF_CCHMASK; 76 + module_param(c_chmask, uint, S_IRUGO); 77 + MODULE_PARM_DESC(c_chmask, "Capture Channel Mask"); 78 + 79 + /* Capture Default 48 KHz */ 80 + static int c_srate = UAC1_DEF_CSRATE; 81 + module_param(c_srate, uint, S_IRUGO); 82 + MODULE_PARM_DESC(c_srate, "Capture Sampling Rate"); 83 + 84 + /* Capture Default 16bits/sample */ 85 + static int c_ssize = UAC1_DEF_CSSIZE; 86 + module_param(c_ssize, uint, S_IRUGO); 87 + MODULE_PARM_DESC(c_ssize, "Capture Sample Size(bytes)"); 88 + #else /* CONFIG_GADGET_UAC1_LEGACY */ 89 + #include "u_uac1_legacy.h" 57 90 58 91 static char *fn_play = FILE_PCM_PLAYBACK; 59 92 module_param(fn_play, charp, S_IRUGO); ··· 111 78 static int audio_buf_size = UAC1_AUDIO_BUF_SIZE; 112 79 module_param(audio_buf_size, int, S_IRUGO); 113 80 MODULE_PARM_DESC(audio_buf_size, "Audio buffer size"); 81 + #endif /* CONFIG_GADGET_UAC1_LEGACY */ 114 82 #endif 115 83 116 84 /* string IDs are assigned dynamically */ ··· 159 125 160 126 /* .bcdUSB = DYNAMIC */ 161 127 162 - #ifdef CONFIG_GADGET_UAC1 128 + #ifdef CONFIG_GADGET_UAC1_LEGACY 163 129 .bDeviceClass = USB_CLASS_PER_INTERFACE, 164 130 .bDeviceSubClass = 0, 165 131 .bDeviceProtocol = 0, ··· 241 207 #ifndef CONFIG_GADGET_UAC1 242 208 struct f_uac2_opts *uac2_opts; 243 209 #else 210 + #ifndef CONFIG_GADGET_UAC1_LEGACY 244 211 struct f_uac1_opts *uac1_opts; 212 + #else 213 + struct f_uac1_legacy_opts *uac1_opts; 214 + #endif 245 215 #endif 246 216 int status; 247 217 ··· 254 216 if (IS_ERR(fi_uac2)) 255 217 return PTR_ERR(fi_uac2); 256 218 #else 219 + #ifndef CONFIG_GADGET_UAC1_LEGACY 257 220 fi_uac1 = usb_get_function_instance("uac1"); 221 + #else 222 + fi_uac1 = usb_get_function_instance("uac1_legacy"); 223 + #endif 258 224 if (IS_ERR(fi_uac1)) 259 225 return PTR_ERR(fi_uac1); 260 226 #endif ··· 273 231 uac2_opts->c_ssize = c_ssize; 274 232 uac2_opts->req_number = UAC2_DEF_REQ_NUM; 275 233 #else 234 + #ifndef CONFIG_GADGET_UAC1_LEGACY 276 235 uac1_opts = container_of(fi_uac1, struct f_uac1_opts, func_inst); 236 + uac1_opts->p_chmask = p_chmask; 237 + uac1_opts->p_srate = p_srate; 238 + uac1_opts->p_ssize = p_ssize; 239 + uac1_opts->c_chmask = c_chmask; 240 + uac1_opts->c_srate = c_srate; 241 + uac1_opts->c_ssize = c_ssize; 242 + uac1_opts->req_number = UAC1_DEF_REQ_NUM; 243 + #else /* CONFIG_GADGET_UAC1_LEGACY */ 244 + uac1_opts = container_of(fi_uac1, struct f_uac1_legacy_opts, func_inst); 277 245 uac1_opts->fn_play = fn_play; 278 246 uac1_opts->fn_cap = fn_cap; 279 247 uac1_opts->fn_cntl = fn_cntl; 280 248 uac1_opts->req_buf_size = req_buf_size; 281 249 uac1_opts->req_count = req_count; 282 250 uac1_opts->audio_buf_size = audio_buf_size; 251 + #endif /* CONFIG_GADGET_UAC1_LEGACY */ 283 252 #endif 284 253 285 254 status = usb_string_ids_tab(cdev, strings_dev);
+6 -2
drivers/usb/gadget/legacy/mass_storage.c
··· 210 210 usb_composite_overwrite_options(cdev, &coverwrite); 211 211 dev_info(&cdev->gadget->dev, 212 212 DRIVER_DESC ", version: " DRIVER_VERSION "\n"); 213 - set_bit(0, &msg_registered); 214 213 return 0; 215 214 216 215 fail_otg_desc: ··· 256 257 257 258 static int __init msg_init(void) 258 259 { 259 - return usb_composite_probe(&msg_driver); 260 + int ret; 261 + 262 + ret = usb_composite_probe(&msg_driver); 263 + set_bit(0, &msg_registered); 264 + 265 + return ret; 260 266 } 261 267 module_init(msg_init); 262 268
+16 -2
drivers/usb/gadget/udc/Kconfig
··· 55 55 56 56 config USB_ATMEL_USBA 57 57 tristate "Atmel USBA" 58 - depends on ((AVR32 && !OF) || ARCH_AT91) 58 + depends on ARCH_AT91 59 59 help 60 60 USBA is the integrated high-speed USB Device controller on 61 61 the AT32AP700x, some AT91SAM9 and AT91CAP9 processors from Atmel. ··· 256 256 controller, which support super speed USB peripheral. 257 257 258 258 config USB_SNP_CORE 259 - depends on USB_AMD5536UDC 259 + depends on (USB_AMD5536UDC || USB_SNP_UDC_PLAT) 260 260 tristate 261 261 help 262 262 This enables core driver support for Synopsys USB 2.0 Device ··· 269 269 This IP is different to the High Speed OTG IP that can be enabled 270 270 by selecting USB_DWC2 or USB_DWC3 options. 271 271 272 + config USB_SNP_UDC_PLAT 273 + tristate "Synopsys USB 2.0 Device controller" 274 + depends on (USB_GADGET && OF) 275 + select USB_GADGET_DUALSPEED 276 + select USB_SNP_CORE 277 + default ARCH_BCM_IPROC 278 + help 279 + This adds Platform Device support for Synopsys Designware core 280 + AHB subsystem USB2.0 Device Controller (UDC). 281 + 282 + This driver works with UDCs integrated into Broadcom's Northstar2 283 + and Cygnus SoCs. 284 + 285 + If unsure, say N. 272 286 # 273 287 # Controllers available in both integrated and discrete versions 274 288 #
+2 -1
drivers/usb/gadget/udc/Makefile
··· 10 10 obj-$(CONFIG_USB_DUMMY_HCD) += dummy_hcd.o 11 11 obj-$(CONFIG_USB_NET2272) += net2272.o 12 12 obj-$(CONFIG_USB_NET2280) += net2280.o 13 - obj-$(CONFIG_USB_SNP_CORE) += amd5536udc.o 13 + obj-$(CONFIG_USB_SNP_CORE) += snps_udc_core.o 14 14 obj-$(CONFIG_USB_AMD5536UDC) += amd5536udc_pci.o 15 15 obj-$(CONFIG_USB_PXA25X) += pxa25x_udc.o 16 16 obj-$(CONFIG_USB_PXA27X) += pxa27x_udc.o ··· 37 37 obj-$(CONFIG_USB_MV_U3D) += mv_u3d_core.o 38 38 obj-$(CONFIG_USB_GR_UDC) += gr_udc.o 39 39 obj-$(CONFIG_USB_GADGET_XILINX) += udc-xilinx.o 40 + obj-$(CONFIG_USB_SNP_UDC_PLAT) += snps_udc_plat.o 40 41 obj-$(CONFIG_USB_BDC_UDC) += bdc/
+48 -32
drivers/usb/gadget/udc/amd5536udc.c drivers/usb/gadget/udc/snps_udc_core.c
··· 41 41 #include "amd5536udc.h" 42 42 43 43 static void udc_tasklet_disconnect(unsigned long); 44 - static void empty_req_queue(struct udc_ep *); 45 44 static void udc_setup_endpoints(struct udc *dev); 46 45 static void udc_soft_reset(struct udc *dev); 47 46 static struct udc_request *udc_alloc_bna_dummy(struct udc_ep *ep); ··· 208 209 if (use_dma && use_dma_ppb && !use_dma_ppb_du) { 209 210 DBG(dev, "DMA mode = PPBNDU (packet per buffer " 210 211 "WITHOUT desc. update)\n"); 211 - dev_info(&dev->pdev->dev, "DMA mode (%s)\n", "PPBNDU"); 212 + dev_info(dev->dev, "DMA mode (%s)\n", "PPBNDU"); 212 213 } else if (use_dma && use_dma_ppb && use_dma_ppb_du) { 213 214 DBG(dev, "DMA mode = PPBDU (packet per buffer " 214 215 "WITH desc. update)\n"); 215 - dev_info(&dev->pdev->dev, "DMA mode (%s)\n", "PPBDU"); 216 + dev_info(dev->dev, "DMA mode (%s)\n", "PPBDU"); 216 217 } 217 218 if (use_dma && use_dma_bufferfill_mode) { 218 219 DBG(dev, "DMA mode = BF (buffer fill mode)\n"); 219 - dev_info(&dev->pdev->dev, "DMA mode (%s)\n", "BF"); 220 + dev_info(dev->dev, "DMA mode (%s)\n", "BF"); 220 221 } 221 222 if (!use_dma) 222 - dev_info(&dev->pdev->dev, "FIFO mode\n"); 223 + dev_info(dev->dev, "FIFO mode\n"); 223 224 DBG(dev, "-------------------------------------------------------\n"); 224 225 } 225 226 ··· 1243 1244 } 1244 1245 1245 1246 /* Empty request queue of an endpoint; caller holds spinlock */ 1246 - static void empty_req_queue(struct udc_ep *ep) 1247 + void empty_req_queue(struct udc_ep *ep) 1247 1248 { 1248 1249 struct udc_request *req; 1249 1250 ··· 1255 1256 complete_req(ep, req, -ESHUTDOWN); 1256 1257 } 1257 1258 } 1259 + EXPORT_SYMBOL_GPL(empty_req_queue); 1258 1260 1259 1261 /* Dequeues a request packet, called by gadget driver */ 1260 1262 static int udc_dequeue(struct usb_ep *usbep, struct usb_request *usbreq) ··· 1623 1623 /* Bringup after Connect event, initial bringup to be ready for ep0 events */ 1624 1624 static void usb_connect(struct udc *dev) 1625 1625 { 1626 + /* Return if already connected */ 1627 + if (dev->connected) 1628 + return; 1626 1629 1627 - dev_info(&dev->pdev->dev, "USB Connect\n"); 1630 + dev_info(dev->dev, "USB Connect\n"); 1628 1631 1629 1632 dev->connected = 1; 1630 1633 ··· 1644 1641 */ 1645 1642 static void usb_disconnect(struct udc *dev) 1646 1643 { 1644 + /* Return if already disconnected */ 1645 + if (!dev->connected) 1646 + return; 1647 1647 1648 - dev_info(&dev->pdev->dev, "USB Disconnect\n"); 1648 + dev_info(dev->dev, "USB Disconnect\n"); 1649 1649 1650 1650 dev->connected = 0; 1651 1651 ··· 1721 1715 /* device int. status reset */ 1722 1716 writel(UDC_DEV_MSK_DISABLE, &dev->regs->irqsts); 1723 1717 1724 - spin_lock_irqsave(&udc_irq_spinlock, flags); 1725 - writel(AMD_BIT(UDC_DEVCFG_SOFTRESET), &dev->regs->cfg); 1726 - readl(&dev->regs->cfg); 1727 - spin_unlock_irqrestore(&udc_irq_spinlock, flags); 1728 - 1718 + /* Don't do this for Broadcom UDC since this is a reserved 1719 + * bit. 1720 + */ 1721 + if (dev->chiprev != UDC_BCM_REV) { 1722 + spin_lock_irqsave(&udc_irq_spinlock, flags); 1723 + writel(AMD_BIT(UDC_DEVCFG_SOFTRESET), &dev->regs->cfg); 1724 + readl(&dev->regs->cfg); 1725 + spin_unlock_irqrestore(&udc_irq_spinlock, flags); 1726 + } 1729 1727 } 1730 1728 1731 1729 /* RDE timer callback to set RDE bit */ ··· 2116 2106 } 2117 2107 /* HE event ? */ 2118 2108 if (tmp & AMD_BIT(UDC_EPSTS_HE)) { 2119 - dev_err(&dev->pdev->dev, "HE ep%dout occurred\n", ep->num); 2109 + dev_err(dev->dev, "HE ep%dout occurred\n", ep->num); 2120 2110 2121 2111 /* clear HE */ 2122 2112 writel(tmp | AMD_BIT(UDC_EPSTS_HE), &ep->regs->sts); ··· 2315 2305 if (use_dma) { 2316 2306 /* BNA ? */ 2317 2307 if (epsts & AMD_BIT(UDC_EPSTS_BNA)) { 2318 - dev_err(&dev->pdev->dev, 2308 + dev_err(dev->dev, 2319 2309 "BNA ep%din occurred - DESPTR = %08lx\n", 2320 2310 ep->num, 2321 2311 (unsigned long) readl(&ep->regs->desptr)); ··· 2328 2318 } 2329 2319 /* HE event ? */ 2330 2320 if (epsts & AMD_BIT(UDC_EPSTS_HE)) { 2331 - dev_err(&dev->pdev->dev, 2321 + dev_err(dev->dev, 2332 2322 "HE ep%dn occurred - DESPTR = %08lx\n", 2333 2323 ep->num, (unsigned long) readl(&ep->regs->desptr)); 2334 2324 ··· 2966 2956 2967 2957 /* link up all endpoints */ 2968 2958 udc_setup_endpoints(dev); 2969 - dev_info(&dev->pdev->dev, "Connect: %s\n", 2959 + dev_info(dev->dev, "Connect: %s\n", 2970 2960 usb_speed_string(dev->gadget.speed)); 2971 2961 2972 2962 /* init ep 0 */ ··· 3107 3097 } 3108 3098 3109 3099 /* DMA setup */ 3110 - dev->data_requests = dma_pool_create("data_requests", NULL, 3100 + dev->data_requests = dma_pool_create("data_requests", dev->dev, 3111 3101 sizeof(struct udc_data_dma), 0, 0); 3112 3102 if (!dev->data_requests) { 3113 3103 DBG(dev, "can't get request data pool\n"); ··· 3118 3108 dev->ep[UDC_EP0IN_IX].dma = &dev->regs->ctl; 3119 3109 3120 3110 /* dma desc for setup data */ 3121 - dev->stp_requests = dma_pool_create("setup requests", NULL, 3111 + dev->stp_requests = dma_pool_create("setup requests", dev->dev, 3122 3112 sizeof(struct udc_stp_dma), 0, 0); 3123 3113 if (!dev->stp_requests) { 3124 3114 DBG(dev, "can't get stp request pool\n"); ··· 3178 3168 /* init registers, interrupts, ... */ 3179 3169 startup_registers(dev); 3180 3170 3181 - dev_info(&dev->pdev->dev, "%s\n", mod_desc); 3171 + dev_info(dev->dev, "%s\n", mod_desc); 3182 3172 3183 3173 snprintf(tmp, sizeof(tmp), "%d", dev->irq); 3184 - dev_info(&dev->pdev->dev, 3185 - "irq %s, pci mem %08lx, chip rev %02x(Geode5536 %s)\n", 3186 - tmp, dev->phys_addr, dev->chiprev, 3187 - (dev->chiprev == UDC_HSA0_REV) ? "A0" : "B1"); 3188 - strcpy(tmp, UDC_DRIVER_VERSION_STRING); 3189 - if (dev->chiprev == UDC_HSA0_REV) { 3190 - dev_err(&dev->pdev->dev, "chip revision is A0; too old\n"); 3191 - retval = -ENODEV; 3192 - goto finished; 3174 + 3175 + /* Print this device info for AMD chips only*/ 3176 + if (dev->chiprev == UDC_HSA0_REV || 3177 + dev->chiprev == UDC_HSB1_REV) { 3178 + dev_info(dev->dev, "irq %s, pci mem %08lx, chip rev %02x(Geode5536 %s)\n", 3179 + tmp, dev->phys_addr, dev->chiprev, 3180 + (dev->chiprev == UDC_HSA0_REV) ? 3181 + "A0" : "B1"); 3182 + strcpy(tmp, UDC_DRIVER_VERSION_STRING); 3183 + if (dev->chiprev == UDC_HSA0_REV) { 3184 + dev_err(dev->dev, "chip revision is A0; too old\n"); 3185 + retval = -ENODEV; 3186 + goto finished; 3187 + } 3188 + dev_info(dev->dev, 3189 + "driver version: %s(for Geode5536 B1)\n", tmp); 3193 3190 } 3194 - dev_info(&dev->pdev->dev, 3195 - "driver version: %s(for Geode5536 B1)\n", tmp); 3191 + 3196 3192 udc = dev; 3197 3193 3198 - retval = usb_add_gadget_udc_release(&udc->pdev->dev, &dev->gadget, 3194 + retval = usb_add_gadget_udc_release(udc->dev, &dev->gadget, 3199 3195 gadget_release); 3200 3196 if (retval) 3201 3197 goto finished;
+17 -1
drivers/usb/gadget/udc/amd5536udc.h
··· 16 16 /* debug control */ 17 17 /* #define UDC_VERBOSE */ 18 18 19 + #include <linux/extcon.h> 19 20 #include <linux/usb/ch9.h> 20 21 #include <linux/usb/gadget.h> 21 22 ··· 28 27 /* Hs AMD5536 chip rev. */ 29 28 #define UDC_HSA0_REV 1 30 29 #define UDC_HSB1_REV 2 30 + 31 + /* Broadcom chip rev. */ 32 + #define UDC_BCM_REV 10 31 33 32 34 /* 33 35 * SETUP usb commands ··· 116 112 #define UDC_DEVCTL_BRLEN_MASK 0x00ff0000 117 113 #define UDC_DEVCTL_BRLEN_OFS 16 118 114 115 + #define UDC_DEVCTL_SRX_FLUSH 14 119 116 #define UDC_DEVCTL_CSR_DONE 13 120 117 #define UDC_DEVCTL_DEVNAK 12 121 118 #define UDC_DEVCTL_SD 10 ··· 568 563 u16 cur_config; 569 564 u16 cur_intf; 570 565 u16 cur_alt; 566 + 567 + /* for platform device and extcon support */ 568 + struct device *dev; 569 + struct phy *udc_phy; 570 + struct extcon_dev *edev; 571 + struct extcon_specific_cable_nb extcon_nb; 572 + struct notifier_block nb; 573 + struct delayed_work drd_work; 574 + struct workqueue_struct *drd_wq; 575 + u32 conn_type; 571 576 }; 572 577 573 578 #define to_amd5536_udc(g) (container_of((g), struct udc, gadget)) ··· 593 578 int udc_mask_unused_interrupts(struct udc *dev); 594 579 irqreturn_t udc_irq(int irq, void *pdev); 595 580 void gadget_release(struct device *pdev); 581 + void empty_req_queue(struct udc_ep *ep); 596 582 void udc_basic_init(struct udc *dev); 597 583 void free_dma_pools(struct udc *dev); 598 584 int init_dma_pools(struct udc *dev); ··· 655 639 656 640 /* debug macros ------------------------------------------------------------*/ 657 641 658 - #define DBG(udc , args...) dev_dbg(&(udc)->pdev->dev, args) 642 + #define DBG(udc , args...) dev_dbg(udc->dev, args) 659 643 660 644 #ifdef UDC_VERBOSE 661 645 #define VDBG DBG
+1
drivers/usb/gadget/udc/amd5536udc_pci.c
··· 168 168 dev->phys_addr = resource; 169 169 dev->irq = pdev->irq; 170 170 dev->pdev = pdev; 171 + dev->dev = &pdev->dev; 171 172 172 173 /* general probing */ 173 174 if (udc_probe(dev)) {
+2 -2
drivers/usb/gadget/udc/atmel_usba_udc.c
··· 152 152 153 153 spin_lock_irq(&udc->lock); 154 154 for (i = 0; i < inode->i_size / 4; i++) 155 - data[i] = usba_io_readl(udc->regs + i * 4); 155 + data[i] = readl_relaxed(udc->regs + i * 4); 156 156 spin_unlock_irq(&udc->lock); 157 157 158 158 file->private_data = data; ··· 1369 1369 if (crq->wLength != cpu_to_le16(sizeof(status))) 1370 1370 goto stall; 1371 1371 ep->state = DATA_STAGE_IN; 1372 - usba_io_writew(status, ep->fifo); 1372 + writew_relaxed(status, ep->fifo); 1373 1373 usba_ep_writel(ep, SET_STA, USBA_TX_PK_RDY); 1374 1374 break; 1375 1375 }
+6 -21
drivers/usb/gadget/udc/atmel_usba_udc.h
··· 43 43 #define USBA_REMOTE_WAKE_UP (1 << 10) 44 44 #define USBA_PULLD_DIS (1 << 11) 45 45 46 - #if defined(CONFIG_AVR32) 47 - #define USBA_ENABLE_MASK USBA_EN_USBA 48 - #define USBA_DISABLE_MASK 0 49 - #elif defined(CONFIG_ARCH_AT91) 50 46 #define USBA_ENABLE_MASK (USBA_EN_USBA | USBA_PULLD_DIS) 51 47 #define USBA_DISABLE_MASK USBA_DETACH 52 - #endif /* CONFIG_ARCH_AT91 */ 53 48 54 49 /* Bitfields in FNUM */ 55 50 #define USBA_MICRO_FRAME_NUM_OFFSET 0 ··· 186 191 | USBA_BF(name, value)) 187 192 188 193 /* Register access macros */ 189 - #ifdef CONFIG_AVR32 190 - #define usba_io_readl __raw_readl 191 - #define usba_io_writel __raw_writel 192 - #define usba_io_writew __raw_writew 193 - #else 194 - #define usba_io_readl readl_relaxed 195 - #define usba_io_writel writel_relaxed 196 - #define usba_io_writew writew_relaxed 197 - #endif 198 - 199 194 #define usba_readl(udc, reg) \ 200 - usba_io_readl((udc)->regs + USBA_##reg) 195 + readl_relaxed((udc)->regs + USBA_##reg) 201 196 #define usba_writel(udc, reg, value) \ 202 - usba_io_writel((value), (udc)->regs + USBA_##reg) 197 + writel_relaxed((value), (udc)->regs + USBA_##reg) 203 198 #define usba_ep_readl(ep, reg) \ 204 - usba_io_readl((ep)->ep_regs + USBA_EPT_##reg) 199 + readl_relaxed((ep)->ep_regs + USBA_EPT_##reg) 205 200 #define usba_ep_writel(ep, reg, value) \ 206 - usba_io_writel((value), (ep)->ep_regs + USBA_EPT_##reg) 201 + writel_relaxed((value), (ep)->ep_regs + USBA_EPT_##reg) 207 202 #define usba_dma_readl(ep, reg) \ 208 - usba_io_readl((ep)->dma_regs + USBA_DMA_##reg) 203 + readl_relaxed((ep)->dma_regs + USBA_DMA_##reg) 209 204 #define usba_dma_writel(ep, reg, value) \ 210 - usba_io_writel((value), (ep)->dma_regs + USBA_DMA_##reg) 205 + writel_relaxed((value), (ep)->dma_regs + USBA_DMA_##reg) 211 206 212 207 /* Calculate base address for a given endpoint or DMA controller */ 213 208 #define USBA_EPT_BASE(x) (0x100 + (x) * 0x20)
+1 -1
drivers/usb/gadget/udc/bdc/bdc_core.c
··· 475 475 bdc->dev = dev; 476 476 dev_dbg(bdc->dev, "bdc->regs: %p irq=%d\n", bdc->regs, bdc->irq); 477 477 478 - temp = bdc_readl(bdc->regs, BDC_BDCSC); 478 + temp = bdc_readl(bdc->regs, BDC_BDCCAP1); 479 479 if ((temp & BDC_P64) && 480 480 !dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64))) { 481 481 dev_dbg(bdc->dev, "Using 64-bit address\n");
+34 -3
drivers/usb/gadget/udc/core.c
··· 140 140 goto out; 141 141 142 142 ret = ep->ops->disable(ep); 143 - if (ret) { 144 - ret = ret; 143 + if (ret) 145 144 goto out; 146 - } 147 145 148 146 ep->enabled = false; 149 147 ··· 1065 1067 } 1066 1068 1067 1069 /** 1070 + * usb_gadget_udc_set_speed - tells usb device controller speed supported by 1071 + * current driver 1072 + * @udc: The device we want to set maximum speed 1073 + * @speed: The maximum speed to allowed to run 1074 + * 1075 + * This call is issued by the UDC Class driver before calling 1076 + * usb_gadget_udc_start() in order to make sure that we don't try to 1077 + * connect on speeds the gadget driver doesn't support. 1078 + */ 1079 + static inline void usb_gadget_udc_set_speed(struct usb_udc *udc, 1080 + enum usb_device_speed speed) 1081 + { 1082 + if (udc->gadget->ops->udc_set_speed) 1083 + udc->gadget->ops->udc_set_speed(udc->gadget, speed); 1084 + } 1085 + 1086 + /** 1068 1087 * usb_udc_release - release the usb_udc struct 1069 1088 * @dev: the dev member within usb_udc 1070 1089 * ··· 1314 1299 udc->dev.driver = &driver->driver; 1315 1300 udc->gadget->dev.driver = &driver->driver; 1316 1301 1302 + if (driver->max_speed < udc->gadget->max_speed) 1303 + usb_gadget_udc_set_speed(udc, driver->max_speed); 1304 + 1317 1305 ret = driver->bind(udc->gadget, driver); 1318 1306 if (ret) 1319 1307 goto err1; ··· 1469 1451 } 1470 1452 static DEVICE_ATTR_RO(state); 1471 1453 1454 + static ssize_t function_show(struct device *dev, struct device_attribute *attr, 1455 + char *buf) 1456 + { 1457 + struct usb_udc *udc = container_of(dev, struct usb_udc, dev); 1458 + struct usb_gadget_driver *drv = udc->driver; 1459 + 1460 + if (!drv || !drv->function) 1461 + return 0; 1462 + return scnprintf(buf, PAGE_SIZE, "%s\n", drv->function); 1463 + } 1464 + static DEVICE_ATTR_RO(function); 1465 + 1472 1466 #define USB_UDC_SPEED_ATTR(name, param) \ 1473 1467 ssize_t name##_show(struct device *dev, \ 1474 1468 struct device_attribute *attr, char *buf) \ ··· 1516 1486 &dev_attr_srp.attr, 1517 1487 &dev_attr_soft_connect.attr, 1518 1488 &dev_attr_state.attr, 1489 + &dev_attr_function.attr, 1519 1490 &dev_attr_current_speed.attr, 1520 1491 &dev_attr_maximum_speed.attr, 1521 1492
+23 -16
drivers/usb/gadget/udc/dummy_hcd.c
··· 881 881 unsigned long flags; 882 882 883 883 dum = gadget_dev_to_dummy(&_gadget->dev); 884 - 885 - if (value && dum->driver) { 886 - if (mod_data.is_super_speed) 887 - dum->gadget.speed = dum->driver->max_speed; 888 - else if (mod_data.is_high_speed) 889 - dum->gadget.speed = min_t(u8, USB_SPEED_HIGH, 890 - dum->driver->max_speed); 891 - else 892 - dum->gadget.speed = USB_SPEED_FULL; 893 - dummy_udc_update_ep0(dum); 894 - 895 - if (dum->gadget.speed < dum->driver->max_speed) 896 - dev_dbg(udc_dev(dum), "This device can perform faster" 897 - " if you connect it to a %s port...\n", 898 - usb_speed_string(dum->driver->max_speed)); 899 - } 900 884 dum_hcd = gadget_to_dummy_hcd(_gadget); 901 885 902 886 spin_lock_irqsave(&dum->lock, flags); ··· 890 906 891 907 usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd)); 892 908 return 0; 909 + } 910 + 911 + static void dummy_udc_set_speed(struct usb_gadget *_gadget, 912 + enum usb_device_speed speed) 913 + { 914 + struct dummy *dum; 915 + 916 + dum = gadget_dev_to_dummy(&_gadget->dev); 917 + 918 + if (mod_data.is_super_speed) 919 + dum->gadget.speed = min_t(u8, USB_SPEED_SUPER, speed); 920 + else if (mod_data.is_high_speed) 921 + dum->gadget.speed = min_t(u8, USB_SPEED_HIGH, speed); 922 + else 923 + dum->gadget.speed = USB_SPEED_FULL; 924 + 925 + dummy_udc_update_ep0(dum); 926 + 927 + if (dum->gadget.speed < speed) 928 + dev_dbg(udc_dev(dum), "This device can perform faster" 929 + " if you connect it to a %s port...\n", 930 + usb_speed_string(speed)); 893 931 } 894 932 895 933 static int dummy_udc_start(struct usb_gadget *g, ··· 925 919 .pullup = dummy_pullup, 926 920 .udc_start = dummy_udc_start, 927 921 .udc_stop = dummy_udc_stop, 922 + .udc_set_speed = dummy_udc_set_speed, 928 923 }; 929 924 930 925 /*-------------------------------------------------------------------------*/
+6 -3
drivers/usb/gadget/udc/mv_udc_core.c
··· 960 960 .fifo_flush = mv_ep_fifo_flush, /* flush fifo */ 961 961 }; 962 962 963 - static void udc_clock_enable(struct mv_udc *udc) 963 + static int udc_clock_enable(struct mv_udc *udc) 964 964 { 965 - clk_prepare_enable(udc->clk); 965 + return clk_prepare_enable(udc->clk); 966 966 } 967 967 968 968 static void udc_clock_disable(struct mv_udc *udc) ··· 1070 1070 return 0; 1071 1071 1072 1072 dev_dbg(&udc->dev->dev, "enable udc\n"); 1073 - udc_clock_enable(udc); 1073 + retval = udc_clock_enable(udc); 1074 + if (retval) 1075 + return retval; 1076 + 1074 1077 if (udc->pdata->phy_init) { 1075 1078 retval = udc->pdata->phy_init(udc->phy_regs); 1076 1079 if (retval) {
+3 -2
drivers/usb/gadget/udc/net2280.c
··· 3566 3566 BUG_ON(dev->driver); 3567 3567 3568 3568 /* then clean up the resources we allocated during probe() */ 3569 - net2280_led_shutdown(dev); 3570 3569 if (dev->requests) { 3571 3570 int i; 3572 3571 for (i = 1; i < 5; i++) { ··· 3580 3581 free_irq(pdev->irq, dev); 3581 3582 if (dev->quirks & PLX_PCIE) 3582 3583 pci_disable_msi(pdev); 3583 - if (dev->regs) 3584 + if (dev->regs) { 3585 + net2280_led_shutdown(dev); 3584 3586 iounmap(dev->regs); 3587 + } 3585 3588 if (dev->region) 3586 3589 release_mem_region(pci_resource_start(pdev, 0), 3587 3590 pci_resource_len(pdev, 0));
+392
drivers/usb/gadget/udc/renesas_usb3.c
··· 9 9 */ 10 10 11 11 #include <linux/delay.h> 12 + #include <linux/dma-mapping.h> 12 13 #include <linux/err.h> 13 14 #include <linux/extcon.h> 14 15 #include <linux/interrupt.h> ··· 28 27 #define USB3_AXI_INT_ENA 0x00c 29 28 #define USB3_DMA_INT_STA 0x010 30 29 #define USB3_DMA_INT_ENA 0x014 30 + #define USB3_DMA_CH0_CON(n) (0x030 + ((n) - 1) * 0x10) /* n = 1 to 4 */ 31 + #define USB3_DMA_CH0_PRD_ADR(n) (0x034 + ((n) - 1) * 0x10) /* n = 1 to 4 */ 31 32 #define USB3_USB_COM_CON 0x200 32 33 #define USB3_USB20_CON 0x204 33 34 #define USB3_USB30_CON 0x208 ··· 67 64 /* AXI_INT_ENA and AXI_INT_STA */ 68 65 #define AXI_INT_DMAINT BIT(31) 69 66 #define AXI_INT_EPCINT BIT(30) 67 + /* PRD's n = from 1 to 4 */ 68 + #define AXI_INT_PRDEN_CLR_STA_SHIFT(n) (16 + (n) - 1) 69 + #define AXI_INT_PRDERR_STA_SHIFT(n) (0 + (n) - 1) 70 + #define AXI_INT_PRDEN_CLR_STA(n) (1 << AXI_INT_PRDEN_CLR_STA_SHIFT(n)) 71 + #define AXI_INT_PRDERR_STA(n) (1 << AXI_INT_PRDERR_STA_SHIFT(n)) 72 + 73 + /* DMA_INT_ENA and DMA_INT_STA */ 74 + #define DMA_INT(n) BIT(n) 75 + 76 + /* DMA_CH0_CONn */ 77 + #define DMA_CON_PIPE_DIR BIT(15) /* 1: In Transfer */ 78 + #define DMA_CON_PIPE_NO_SHIFT 8 79 + #define DMA_CON_PIPE_NO_MASK GENMASK(12, DMA_CON_PIPE_NO_SHIFT) 80 + #define DMA_COM_PIPE_NO(n) (((n) << DMA_CON_PIPE_NO_SHIFT) & \ 81 + DMA_CON_PIPE_NO_MASK) 82 + #define DMA_CON_PRD_EN BIT(0) 70 83 71 84 /* LCLKSEL */ 72 85 #define LCLKSEL_LSEL BIT(18) ··· 250 231 #define USB3_EP0_BUF_SIZE 8 251 232 #define USB3_MAX_NUM_PIPES 30 252 233 #define USB3_WAIT_US 3 234 + #define USB3_DMA_NUM_SETTING_AREA 4 235 + /* 236 + * To avoid double-meaning of "0" (xferred 65536 bytes or received zlp if 237 + * buffer size is 65536), this driver uses the maximum size per a entry is 238 + * 32768 bytes. 239 + */ 240 + #define USB3_DMA_MAX_XFER_SIZE 32768 241 + #define USB3_DMA_PRD_SIZE 4096 253 242 254 243 struct renesas_usb3; 244 + 245 + /* Physical Region Descriptor Table */ 246 + struct renesas_usb3_prd { 247 + u32 word1; 248 + #define USB3_PRD1_E BIT(30) /* the end of chain */ 249 + #define USB3_PRD1_U BIT(29) /* completion of transfer */ 250 + #define USB3_PRD1_D BIT(28) /* Error occurred */ 251 + #define USB3_PRD1_INT BIT(27) /* Interrupt occurred */ 252 + #define USB3_PRD1_LST BIT(26) /* Last Packet */ 253 + #define USB3_PRD1_B_INC BIT(24) 254 + #define USB3_PRD1_MPS_8 0 255 + #define USB3_PRD1_MPS_16 BIT(21) 256 + #define USB3_PRD1_MPS_32 BIT(22) 257 + #define USB3_PRD1_MPS_64 (BIT(22) | BIT(21)) 258 + #define USB3_PRD1_MPS_512 BIT(23) 259 + #define USB3_PRD1_MPS_1024 (BIT(23) | BIT(21)) 260 + #define USB3_PRD1_MPS_RESERVED (BIT(23) | BIT(22) | BIT(21)) 261 + #define USB3_PRD1_SIZE_MASK GENMASK(15, 0) 262 + 263 + u32 bap; 264 + }; 265 + #define USB3_DMA_NUM_PRD_ENTRIES (USB3_DMA_PRD_SIZE / \ 266 + sizeof(struct renesas_usb3_prd)) 267 + #define USB3_DMA_MAX_XFER_SIZE_ALL_PRDS (USB3_DMA_PRD_SIZE / \ 268 + sizeof(struct renesas_usb3_prd) * \ 269 + USB3_DMA_MAX_XFER_SIZE) 270 + 271 + struct renesas_usb3_dma { 272 + struct renesas_usb3_prd *prd; 273 + dma_addr_t prd_dma; 274 + int num; /* Setting area number (from 1 to 4) */ 275 + bool used; 276 + }; 277 + 255 278 struct renesas_usb3_request { 256 279 struct usb_request req; 257 280 struct list_head queue; ··· 303 242 struct renesas_usb3_ep { 304 243 struct usb_ep ep; 305 244 struct renesas_usb3 *usb3; 245 + struct renesas_usb3_dma *dma; 306 246 int num; 307 247 char ep_name[USB3_EP_NAME_SIZE]; 308 248 struct list_head queue; ··· 331 269 332 270 struct renesas_usb3_ep *usb3_ep; 333 271 int num_usb3_eps; 272 + 273 + struct renesas_usb3_dma dma[USB3_DMA_NUM_SETTING_AREA]; 334 274 335 275 spinlock_t lock; 336 276 int disabled_count; ··· 362 298 (i) < (usb3)->num_usb3_eps; \ 363 299 (i)++, usb3_ep = usb3_get_ep(usb3, (i))) 364 300 301 + #define usb3_get_dma(usb3, i) (&(usb3)->dma[i]) 302 + #define usb3_for_each_dma(usb3, dma, i) \ 303 + for ((i) = 0, dma = usb3_get_dma((usb3), (i)); \ 304 + (i) < USB3_DMA_NUM_SETTING_AREA; \ 305 + (i)++, dma = usb3_get_dma((usb3), (i))) 306 + 365 307 static const char udc_name[] = "renesas_usb3"; 308 + 309 + static bool use_dma = 1; 310 + module_param(use_dma, bool, 0644); 311 + MODULE_PARM_DESC(use_dma, "use dedicated DMAC"); 366 312 367 313 static void usb3_write(struct renesas_usb3 *usb3, u32 data, u32 offs) 368 314 { ··· 1133 1059 usb3_p0_xfer(usb3_ep, usb3_req); 1134 1060 } 1135 1061 1062 + static void usb3_enable_dma_pipen(struct renesas_usb3 *usb3) 1063 + { 1064 + usb3_set_bit(usb3, PN_CON_DATAIF_EN, USB3_PN_CON); 1065 + } 1066 + 1067 + static void usb3_disable_dma_pipen(struct renesas_usb3 *usb3) 1068 + { 1069 + usb3_clear_bit(usb3, PN_CON_DATAIF_EN, USB3_PN_CON); 1070 + } 1071 + 1072 + static void usb3_enable_dma_irq(struct renesas_usb3 *usb3, int num) 1073 + { 1074 + usb3_set_bit(usb3, DMA_INT(num), USB3_DMA_INT_ENA); 1075 + } 1076 + 1077 + static void usb3_disable_dma_irq(struct renesas_usb3 *usb3, int num) 1078 + { 1079 + usb3_clear_bit(usb3, DMA_INT(num), USB3_DMA_INT_ENA); 1080 + } 1081 + 1082 + static u32 usb3_dma_mps_to_prd_word1(struct renesas_usb3_ep *usb3_ep) 1083 + { 1084 + switch (usb3_ep->ep.maxpacket) { 1085 + case 8: 1086 + return USB3_PRD1_MPS_8; 1087 + case 16: 1088 + return USB3_PRD1_MPS_16; 1089 + case 32: 1090 + return USB3_PRD1_MPS_32; 1091 + case 64: 1092 + return USB3_PRD1_MPS_64; 1093 + case 512: 1094 + return USB3_PRD1_MPS_512; 1095 + case 1024: 1096 + return USB3_PRD1_MPS_1024; 1097 + default: 1098 + return USB3_PRD1_MPS_RESERVED; 1099 + } 1100 + } 1101 + 1102 + static bool usb3_dma_get_setting_area(struct renesas_usb3_ep *usb3_ep, 1103 + struct renesas_usb3_request *usb3_req) 1104 + { 1105 + struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep); 1106 + struct renesas_usb3_dma *dma; 1107 + int i; 1108 + bool ret = false; 1109 + 1110 + if (usb3_req->req.length > USB3_DMA_MAX_XFER_SIZE_ALL_PRDS) { 1111 + dev_dbg(usb3_to_dev(usb3), "%s: the length is too big (%d)\n", 1112 + __func__, usb3_req->req.length); 1113 + return false; 1114 + } 1115 + 1116 + /* The driver doesn't handle zero-length packet via dmac */ 1117 + if (!usb3_req->req.length) 1118 + return false; 1119 + 1120 + if (usb3_dma_mps_to_prd_word1(usb3_ep) == USB3_PRD1_MPS_RESERVED) 1121 + return false; 1122 + 1123 + usb3_for_each_dma(usb3, dma, i) { 1124 + if (dma->used) 1125 + continue; 1126 + 1127 + if (usb_gadget_map_request(&usb3->gadget, &usb3_req->req, 1128 + usb3_ep->dir_in) < 0) 1129 + break; 1130 + 1131 + dma->used = true; 1132 + usb3_ep->dma = dma; 1133 + ret = true; 1134 + break; 1135 + } 1136 + 1137 + return ret; 1138 + } 1139 + 1140 + static void usb3_dma_put_setting_area(struct renesas_usb3_ep *usb3_ep, 1141 + struct renesas_usb3_request *usb3_req) 1142 + { 1143 + struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep); 1144 + int i; 1145 + struct renesas_usb3_dma *dma; 1146 + 1147 + usb3_for_each_dma(usb3, dma, i) { 1148 + if (usb3_ep->dma == dma) { 1149 + usb_gadget_unmap_request(&usb3->gadget, &usb3_req->req, 1150 + usb3_ep->dir_in); 1151 + dma->used = false; 1152 + usb3_ep->dma = NULL; 1153 + break; 1154 + } 1155 + } 1156 + } 1157 + 1158 + static void usb3_dma_fill_prd(struct renesas_usb3_ep *usb3_ep, 1159 + struct renesas_usb3_request *usb3_req) 1160 + { 1161 + struct renesas_usb3_prd *cur_prd = usb3_ep->dma->prd; 1162 + u32 remain = usb3_req->req.length; 1163 + u32 dma = usb3_req->req.dma; 1164 + u32 len; 1165 + int i = 0; 1166 + 1167 + do { 1168 + len = min_t(u32, remain, USB3_DMA_MAX_XFER_SIZE) & 1169 + USB3_PRD1_SIZE_MASK; 1170 + cur_prd->word1 = usb3_dma_mps_to_prd_word1(usb3_ep) | 1171 + USB3_PRD1_B_INC | len; 1172 + cur_prd->bap = dma; 1173 + remain -= len; 1174 + dma += len; 1175 + if (!remain || (i + 1) < USB3_DMA_NUM_PRD_ENTRIES) 1176 + break; 1177 + 1178 + cur_prd++; 1179 + i++; 1180 + } while (1); 1181 + 1182 + cur_prd->word1 |= USB3_PRD1_E | USB3_PRD1_INT; 1183 + if (usb3_ep->dir_in) 1184 + cur_prd->word1 |= USB3_PRD1_LST; 1185 + } 1186 + 1187 + static void usb3_dma_kick_prd(struct renesas_usb3_ep *usb3_ep) 1188 + { 1189 + struct renesas_usb3_dma *dma = usb3_ep->dma; 1190 + struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep); 1191 + u32 dma_con = DMA_COM_PIPE_NO(usb3_ep->num) | DMA_CON_PRD_EN; 1192 + 1193 + if (usb3_ep->dir_in) 1194 + dma_con |= DMA_CON_PIPE_DIR; 1195 + 1196 + wmb(); /* prd entries should be in system memory here */ 1197 + 1198 + usb3_write(usb3, 1 << usb3_ep->num, USB3_DMA_INT_STA); 1199 + usb3_write(usb3, AXI_INT_PRDEN_CLR_STA(dma->num) | 1200 + AXI_INT_PRDERR_STA(dma->num), USB3_AXI_INT_STA); 1201 + 1202 + usb3_write(usb3, dma->prd_dma, USB3_DMA_CH0_PRD_ADR(dma->num)); 1203 + usb3_write(usb3, dma_con, USB3_DMA_CH0_CON(dma->num)); 1204 + usb3_enable_dma_irq(usb3, usb3_ep->num); 1205 + } 1206 + 1207 + static void usb3_dma_stop_prd(struct renesas_usb3_ep *usb3_ep) 1208 + { 1209 + struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep); 1210 + struct renesas_usb3_dma *dma = usb3_ep->dma; 1211 + 1212 + usb3_disable_dma_irq(usb3, usb3_ep->num); 1213 + usb3_write(usb3, 0, USB3_DMA_CH0_CON(dma->num)); 1214 + } 1215 + 1216 + static int usb3_dma_update_status(struct renesas_usb3_ep *usb3_ep, 1217 + struct renesas_usb3_request *usb3_req) 1218 + { 1219 + struct renesas_usb3_prd *cur_prd = usb3_ep->dma->prd; 1220 + struct usb_request *req = &usb3_req->req; 1221 + u32 remain, len; 1222 + int i = 0; 1223 + int status = 0; 1224 + 1225 + rmb(); /* The controller updated prd entries */ 1226 + 1227 + do { 1228 + if (cur_prd->word1 & USB3_PRD1_D) 1229 + status = -EIO; 1230 + if (cur_prd->word1 & USB3_PRD1_E) 1231 + len = req->length % USB3_DMA_MAX_XFER_SIZE; 1232 + else 1233 + len = USB3_DMA_MAX_XFER_SIZE; 1234 + remain = cur_prd->word1 & USB3_PRD1_SIZE_MASK; 1235 + req->actual += len - remain; 1236 + 1237 + if (cur_prd->word1 & USB3_PRD1_E || 1238 + (i + 1) < USB3_DMA_NUM_PRD_ENTRIES) 1239 + break; 1240 + 1241 + cur_prd++; 1242 + i++; 1243 + } while (1); 1244 + 1245 + return status; 1246 + } 1247 + 1248 + static bool usb3_dma_try_start(struct renesas_usb3_ep *usb3_ep, 1249 + struct renesas_usb3_request *usb3_req) 1250 + { 1251 + struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep); 1252 + 1253 + if (!use_dma) 1254 + return false; 1255 + 1256 + if (usb3_dma_get_setting_area(usb3_ep, usb3_req)) { 1257 + usb3_pn_stop(usb3); 1258 + usb3_enable_dma_pipen(usb3); 1259 + usb3_dma_fill_prd(usb3_ep, usb3_req); 1260 + usb3_dma_kick_prd(usb3_ep); 1261 + usb3_pn_start(usb3); 1262 + return true; 1263 + } 1264 + 1265 + return false; 1266 + } 1267 + 1268 + static int usb3_dma_try_stop(struct renesas_usb3_ep *usb3_ep, 1269 + struct renesas_usb3_request *usb3_req) 1270 + { 1271 + struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep); 1272 + unsigned long flags; 1273 + int status = 0; 1274 + 1275 + spin_lock_irqsave(&usb3->lock, flags); 1276 + if (!usb3_ep->dma) 1277 + goto out; 1278 + 1279 + if (!usb3_pn_change(usb3, usb3_ep->num)) 1280 + usb3_disable_dma_pipen(usb3); 1281 + usb3_dma_stop_prd(usb3_ep); 1282 + status = usb3_dma_update_status(usb3_ep, usb3_req); 1283 + usb3_dma_put_setting_area(usb3_ep, usb3_req); 1284 + 1285 + out: 1286 + spin_unlock_irqrestore(&usb3->lock, flags); 1287 + return status; 1288 + } 1289 + 1290 + static int renesas_usb3_dma_free_prd(struct renesas_usb3 *usb3, 1291 + struct device *dev) 1292 + { 1293 + int i; 1294 + struct renesas_usb3_dma *dma; 1295 + 1296 + usb3_for_each_dma(usb3, dma, i) { 1297 + if (dma->prd) { 1298 + dma_free_coherent(dev, USB3_DMA_MAX_XFER_SIZE, 1299 + dma->prd, dma->prd_dma); 1300 + dma->prd = NULL; 1301 + } 1302 + } 1303 + 1304 + return 0; 1305 + } 1306 + 1307 + static int renesas_usb3_dma_alloc_prd(struct renesas_usb3 *usb3, 1308 + struct device *dev) 1309 + { 1310 + int i; 1311 + struct renesas_usb3_dma *dma; 1312 + 1313 + if (!use_dma) 1314 + return 0; 1315 + 1316 + usb3_for_each_dma(usb3, dma, i) { 1317 + dma->prd = dma_alloc_coherent(dev, USB3_DMA_PRD_SIZE, 1318 + &dma->prd_dma, GFP_KERNEL); 1319 + if (!dma->prd) { 1320 + renesas_usb3_dma_free_prd(usb3, dev); 1321 + return -ENOMEM; 1322 + } 1323 + dma->num = i + 1; 1324 + } 1325 + 1326 + return 0; 1327 + } 1328 + 1136 1329 static void usb3_start_pipen(struct renesas_usb3_ep *usb3_ep, 1137 1330 struct renesas_usb3_request *usb3_req) 1138 1331 { ··· 1419 1078 goto out; 1420 1079 1421 1080 usb3_ep->started = true; 1081 + 1082 + if (usb3_dma_try_start(usb3_ep, usb3_req)) 1083 + goto out; 1084 + 1422 1085 usb3_pn_start(usb3); 1423 1086 1424 1087 if (usb3_ep->dir_in) { ··· 1948 1603 } 1949 1604 } 1950 1605 1606 + static void usb3_irq_dma_int(struct renesas_usb3 *usb3, u32 dma_sta) 1607 + { 1608 + struct renesas_usb3_ep *usb3_ep; 1609 + struct renesas_usb3_request *usb3_req; 1610 + int i, status; 1611 + 1612 + for (i = 0; i < usb3->num_usb3_eps; i++) { 1613 + if (!(dma_sta & DMA_INT(i))) 1614 + continue; 1615 + 1616 + usb3_ep = usb3_get_ep(usb3, i); 1617 + if (!(usb3_read(usb3, USB3_AXI_INT_STA) & 1618 + AXI_INT_PRDEN_CLR_STA(usb3_ep->dma->num))) 1619 + continue; 1620 + 1621 + usb3_req = usb3_get_request(usb3_ep); 1622 + status = usb3_dma_try_stop(usb3_ep, usb3_req); 1623 + usb3_request_done_pipen(usb3, usb3_ep, usb3_req, status); 1624 + } 1625 + } 1626 + 1627 + static void usb3_irq_dma(struct renesas_usb3 *usb3) 1628 + { 1629 + u32 dma_sta = usb3_read(usb3, USB3_DMA_INT_STA); 1630 + 1631 + dma_sta &= usb3_read(usb3, USB3_DMA_INT_ENA); 1632 + if (dma_sta) { 1633 + usb3_write(usb3, dma_sta, USB3_DMA_INT_STA); 1634 + usb3_irq_dma_int(usb3, dma_sta); 1635 + } 1636 + } 1637 + 1951 1638 static irqreturn_t renesas_usb3_irq(int irq, void *_usb3) 1952 1639 { 1953 1640 struct renesas_usb3 *usb3 = _usb3; 1954 1641 irqreturn_t ret = IRQ_NONE; 1955 1642 u32 axi_int_sta = usb3_read(usb3, USB3_AXI_INT_STA); 1643 + 1644 + if (axi_int_sta & AXI_INT_DMAINT) { 1645 + usb3_irq_dma(usb3); 1646 + ret = IRQ_HANDLED; 1647 + } 1956 1648 1957 1649 if (axi_int_sta & AXI_INT_EPCINT) { 1958 1650 usb3_irq_epc(usb3); ··· 2090 1708 usb3_req = usb3_get_request(usb3_ep); 2091 1709 if (!usb3_req) 2092 1710 break; 1711 + usb3_dma_try_stop(usb3_ep, usb3_req); 2093 1712 usb3_request_done(usb3_ep, usb3_req, -ESHUTDOWN); 2094 1713 } while (1); 2095 1714 ··· 2138 1755 dev_dbg(usb3_to_dev(usb3), "ep_dequeue: ep%2d, %u\n", usb3_ep->num, 2139 1756 _req->length); 2140 1757 1758 + usb3_dma_try_stop(usb3_ep, usb3_req); 2141 1759 usb3_request_done_pipen(usb3, usb3_ep, usb3_req, -ECONNRESET); 2142 1760 2143 1761 return 0; ··· 2301 1917 device_remove_file(&pdev->dev, &dev_attr_role); 2302 1918 2303 1919 usb_del_gadget_udc(&usb3->gadget); 1920 + renesas_usb3_dma_free_prd(usb3, &pdev->dev); 2304 1921 2305 1922 __renesas_usb3_ep_free_request(usb3->ep0_req); 2306 1923 ··· 2496 2111 if (!usb3->ep0_req) 2497 2112 return -ENOMEM; 2498 2113 2114 + ret = renesas_usb3_dma_alloc_prd(usb3, &pdev->dev); 2115 + if (ret < 0) 2116 + goto err_alloc_prd; 2117 + 2499 2118 ret = usb_add_gadget_udc(&pdev->dev, &usb3->gadget); 2500 2119 if (ret < 0) 2501 2120 goto err_add_udc; ··· 2518 2129 usb_del_gadget_udc(&usb3->gadget); 2519 2130 2520 2131 err_add_udc: 2132 + renesas_usb3_dma_free_prd(usb3, &pdev->dev); 2133 + 2134 + err_alloc_prd: 2521 2135 __renesas_usb3_ep_free_request(usb3->ep0_req); 2522 2136 2523 2137 return ret;
+344
drivers/usb/gadget/udc/snps_udc_plat.c
··· 1 + /* 2 + * snps_udc_plat.c - Synopsys UDC Platform Driver 3 + * 4 + * Copyright (C) 2016 Broadcom 5 + * 6 + * This program is free software; you can redistribute it and/or 7 + * modify it under the terms of the GNU General Public License as 8 + * published by the Free Software Foundation version 2. 9 + * 10 + * This program is distributed "as is" WITHOUT ANY WARRANTY of any 11 + * kind, whether express or implied; without even the implied warranty 12 + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 + * GNU General Public License for more details. 14 + */ 15 + 16 + #include <linux/extcon.h> 17 + #include <linux/of_address.h> 18 + #include <linux/of_irq.h> 19 + #include <linux/of_gpio.h> 20 + #include <linux/platform_device.h> 21 + #include <linux/phy/phy.h> 22 + #include <linux/module.h> 23 + #include <linux/dmapool.h> 24 + #include <linux/interrupt.h> 25 + #include <linux/moduleparam.h> 26 + #include "amd5536udc.h" 27 + 28 + /* description */ 29 + #define UDC_MOD_DESCRIPTION "Synopsys UDC platform driver" 30 + 31 + void start_udc(struct udc *udc) 32 + { 33 + if (udc->driver) { 34 + dev_info(udc->dev, "Connecting...\n"); 35 + udc_enable_dev_setup_interrupts(udc); 36 + udc_basic_init(udc); 37 + udc->connected = 1; 38 + } 39 + } 40 + 41 + void stop_udc(struct udc *udc) 42 + { 43 + int tmp; 44 + u32 reg; 45 + 46 + spin_lock(&udc->lock); 47 + 48 + /* Flush the receieve fifo */ 49 + reg = readl(&udc->regs->ctl); 50 + reg |= AMD_BIT(UDC_DEVCTL_SRX_FLUSH); 51 + writel(reg, &udc->regs->ctl); 52 + 53 + reg = readl(&udc->regs->ctl); 54 + reg &= ~(AMD_BIT(UDC_DEVCTL_SRX_FLUSH)); 55 + writel(reg, &udc->regs->ctl); 56 + dev_dbg(udc->dev, "ep rx queue flushed\n"); 57 + 58 + /* Mask interrupts. Required more so when the 59 + * UDC is connected to a DRD phy. 60 + */ 61 + udc_mask_unused_interrupts(udc); 62 + 63 + /* Disconnect gadget driver */ 64 + if (udc->driver) { 65 + spin_unlock(&udc->lock); 66 + udc->driver->disconnect(&udc->gadget); 67 + spin_lock(&udc->lock); 68 + 69 + /* empty queues */ 70 + for (tmp = 0; tmp < UDC_EP_NUM; tmp++) 71 + empty_req_queue(&udc->ep[tmp]); 72 + } 73 + udc->connected = 0; 74 + 75 + spin_unlock(&udc->lock); 76 + dev_info(udc->dev, "Device disconnected\n"); 77 + } 78 + 79 + void udc_drd_work(struct work_struct *work) 80 + { 81 + struct udc *udc; 82 + 83 + udc = container_of(to_delayed_work(work), 84 + struct udc, drd_work); 85 + 86 + if (udc->conn_type) { 87 + dev_dbg(udc->dev, "idle -> device\n"); 88 + start_udc(udc); 89 + } else { 90 + dev_dbg(udc->dev, "device -> idle\n"); 91 + stop_udc(udc); 92 + } 93 + } 94 + 95 + static int usbd_connect_notify(struct notifier_block *self, 96 + unsigned long event, void *ptr) 97 + { 98 + struct udc *udc = container_of(self, struct udc, nb); 99 + 100 + dev_dbg(udc->dev, "%s: event: %lu\n", __func__, event); 101 + 102 + udc->conn_type = event; 103 + 104 + schedule_delayed_work(&udc->drd_work, 0); 105 + 106 + return NOTIFY_OK; 107 + } 108 + 109 + static int udc_plat_probe(struct platform_device *pdev) 110 + { 111 + struct device *dev = &pdev->dev; 112 + struct resource *res; 113 + struct udc *udc; 114 + int ret; 115 + 116 + udc = devm_kzalloc(dev, sizeof(*udc), GFP_KERNEL); 117 + if (!udc) 118 + return -ENOMEM; 119 + 120 + spin_lock_init(&udc->lock); 121 + udc->dev = dev; 122 + 123 + res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 124 + udc->virt_addr = devm_ioremap_resource(dev, res); 125 + if (IS_ERR(udc->regs)) 126 + return PTR_ERR(udc->regs); 127 + 128 + /* udc csr registers base */ 129 + udc->csr = udc->virt_addr + UDC_CSR_ADDR; 130 + 131 + /* dev registers base */ 132 + udc->regs = udc->virt_addr + UDC_DEVCFG_ADDR; 133 + 134 + /* ep registers base */ 135 + udc->ep_regs = udc->virt_addr + UDC_EPREGS_ADDR; 136 + 137 + /* fifo's base */ 138 + udc->rxfifo = (u32 __iomem *)(udc->virt_addr + UDC_RXFIFO_ADDR); 139 + udc->txfifo = (u32 __iomem *)(udc->virt_addr + UDC_TXFIFO_ADDR); 140 + 141 + udc->phys_addr = (unsigned long)res->start; 142 + 143 + udc->irq = irq_of_parse_and_map(dev->of_node, 0); 144 + if (udc->irq <= 0) { 145 + dev_err(dev, "Can't parse and map interrupt\n"); 146 + return -EINVAL; 147 + } 148 + 149 + udc->udc_phy = devm_of_phy_get_by_index(dev, dev->of_node, 0); 150 + if (IS_ERR(udc->udc_phy)) { 151 + dev_err(dev, "Failed to obtain phy from device tree\n"); 152 + return PTR_ERR(udc->udc_phy); 153 + } 154 + 155 + ret = phy_init(udc->udc_phy); 156 + if (ret) { 157 + dev_err(dev, "UDC phy init failed"); 158 + return ret; 159 + } 160 + 161 + ret = phy_power_on(udc->udc_phy); 162 + if (ret) { 163 + dev_err(dev, "UDC phy power on failed"); 164 + phy_exit(udc->udc_phy); 165 + return ret; 166 + } 167 + 168 + /* Register for extcon if supported */ 169 + if (of_get_property(dev->of_node, "extcon", NULL)) { 170 + udc->edev = extcon_get_edev_by_phandle(dev, 0); 171 + if (IS_ERR(udc->edev)) { 172 + if (PTR_ERR(udc->edev) == -EPROBE_DEFER) 173 + return -EPROBE_DEFER; 174 + dev_err(dev, "Invalid or missing extcon\n"); 175 + ret = PTR_ERR(udc->edev); 176 + goto exit_phy; 177 + } 178 + 179 + udc->nb.notifier_call = usbd_connect_notify; 180 + ret = extcon_register_notifier(udc->edev, EXTCON_USB, 181 + &udc->nb); 182 + if (ret < 0) { 183 + dev_err(dev, "Can't register extcon device\n"); 184 + goto exit_phy; 185 + } 186 + 187 + ret = extcon_get_cable_state_(udc->edev, EXTCON_USB); 188 + if (ret < 0) { 189 + dev_err(dev, "Can't get cable state\n"); 190 + goto exit_extcon; 191 + } else if (ret) { 192 + udc->conn_type = ret; 193 + } 194 + INIT_DELAYED_WORK(&udc->drd_work, udc_drd_work); 195 + } 196 + 197 + /* init dma pools */ 198 + if (use_dma) { 199 + ret = init_dma_pools(udc); 200 + if (ret != 0) 201 + goto exit_extcon; 202 + } 203 + 204 + ret = devm_request_irq(dev, udc->irq, udc_irq, IRQF_SHARED, 205 + "snps-udc", udc); 206 + if (ret < 0) { 207 + dev_err(dev, "Request irq %d failed for UDC\n", udc->irq); 208 + goto exit_dma; 209 + } 210 + 211 + platform_set_drvdata(pdev, udc); 212 + udc->chiprev = UDC_BCM_REV; 213 + 214 + if (udc_probe(udc)) { 215 + ret = -ENODEV; 216 + goto exit_dma; 217 + } 218 + dev_info(dev, "Synopsys UDC platform driver probe successful\n"); 219 + 220 + return 0; 221 + 222 + exit_dma: 223 + if (use_dma) 224 + free_dma_pools(udc); 225 + exit_extcon: 226 + if (udc->edev) 227 + extcon_unregister_notifier(udc->edev, EXTCON_USB, &udc->nb); 228 + exit_phy: 229 + if (udc->udc_phy) { 230 + phy_power_off(udc->udc_phy); 231 + phy_exit(udc->udc_phy); 232 + } 233 + return ret; 234 + } 235 + 236 + static int udc_plat_remove(struct platform_device *pdev) 237 + { 238 + struct udc *dev; 239 + 240 + dev = platform_get_drvdata(pdev); 241 + 242 + usb_del_gadget_udc(&dev->gadget); 243 + /* gadget driver must not be registered */ 244 + if (WARN_ON(dev->driver)) 245 + return 0; 246 + 247 + /* dma pool cleanup */ 248 + free_dma_pools(dev); 249 + 250 + udc_remove(dev); 251 + 252 + platform_set_drvdata(pdev, NULL); 253 + 254 + if (dev->drd_wq) { 255 + flush_workqueue(dev->drd_wq); 256 + destroy_workqueue(dev->drd_wq); 257 + } 258 + 259 + phy_power_off(dev->udc_phy); 260 + phy_exit(dev->udc_phy); 261 + extcon_unregister_notifier(dev->edev, EXTCON_USB, &dev->nb); 262 + 263 + dev_info(&pdev->dev, "Synopsys UDC platform driver removed\n"); 264 + 265 + return 0; 266 + } 267 + 268 + #ifdef CONFIG_PM_SLEEP 269 + static int udc_plat_suspend(struct device *dev) 270 + { 271 + struct udc *udc; 272 + 273 + udc = dev_get_drvdata(dev); 274 + stop_udc(udc); 275 + 276 + if (extcon_get_cable_state_(udc->edev, EXTCON_USB) > 0) { 277 + dev_dbg(udc->dev, "device -> idle\n"); 278 + stop_udc(udc); 279 + } 280 + phy_power_off(udc->udc_phy); 281 + phy_exit(udc->udc_phy); 282 + 283 + return 0; 284 + } 285 + 286 + static int udc_plat_resume(struct device *dev) 287 + { 288 + struct udc *udc; 289 + int ret; 290 + 291 + udc = dev_get_drvdata(dev); 292 + 293 + ret = phy_init(udc->udc_phy); 294 + if (ret) { 295 + dev_err(udc->dev, "UDC phy init failure"); 296 + return ret; 297 + } 298 + 299 + ret = phy_power_on(udc->udc_phy); 300 + if (ret) { 301 + dev_err(udc->dev, "UDC phy power on failure"); 302 + phy_exit(udc->udc_phy); 303 + return ret; 304 + } 305 + 306 + if (extcon_get_cable_state_(udc->edev, EXTCON_USB) > 0) { 307 + dev_dbg(udc->dev, "idle -> device\n"); 308 + start_udc(udc); 309 + } 310 + 311 + return 0; 312 + } 313 + static const struct dev_pm_ops udc_plat_pm_ops = { 314 + .suspend = udc_plat_suspend, 315 + .resume = udc_plat_resume, 316 + }; 317 + #endif 318 + 319 + #if defined(CONFIG_OF) 320 + static const struct of_device_id of_udc_match[] = { 321 + { .compatible = "brcm,ns2-udc", }, 322 + { .compatible = "brcm,cygnus-udc", }, 323 + { .compatible = "brcm,iproc-udc", }, 324 + { } 325 + }; 326 + MODULE_DEVICE_TABLE(of, of_udc_match); 327 + #endif 328 + 329 + static struct platform_driver udc_plat_driver = { 330 + .probe = udc_plat_probe, 331 + .remove = udc_plat_remove, 332 + .driver = { 333 + .name = "snps-udc-plat", 334 + .of_match_table = of_match_ptr(of_udc_match), 335 + #ifdef CONFIG_PM_SLEEP 336 + .pm = &udc_plat_pm_ops, 337 + #endif 338 + }, 339 + }; 340 + module_platform_driver(udc_plat_driver); 341 + 342 + MODULE_DESCRIPTION(UDC_MOD_DESCRIPTION); 343 + MODULE_AUTHOR("Broadcom"); 344 + MODULE_LICENSE("GPL v2");
+1 -1
drivers/usb/gadget/udc/udc-xilinx.c
··· 1151 1151 break; 1152 1152 } 1153 1153 if (&req->usb_req != _req) { 1154 - spin_unlock_irqrestore(&ep->udc->lock, flags); 1154 + spin_unlock_irqrestore(&udc->lock, flags); 1155 1155 return -EINVAL; 1156 1156 } 1157 1157 xudc_done(ep, req, -ECONNRESET);
+3 -3
drivers/usb/phy/Kconfig
··· 4 4 menu "USB Physical Layer drivers" 5 5 6 6 config USB_PHY 7 + select EXTCON 7 8 def_bool n 8 9 9 10 # ··· 110 109 111 110 config TAHVO_USB 112 111 tristate "Tahvo USB transceiver driver" 113 - depends on MFD_RETU && EXTCON 112 + depends on MFD_RETU 114 113 depends on USB_GADGET || !USB_GADGET # if USB_GADGET=m, this can't be 'y' 115 114 select USB_PHY 116 115 help ··· 142 141 depends on (USB || USB_GADGET) && (ARCH_QCOM || COMPILE_TEST) 143 142 depends on USB_GADGET || !USB_GADGET # if USB_GADGET=m, this can't be 'y' 144 143 depends on RESET_CONTROLLER 145 - depends on EXTCON 146 144 select USB_PHY 147 145 help 148 146 Enable this to support the USB OTG transceiver on Qualcomm chips. It ··· 155 155 config USB_QCOM_8X16_PHY 156 156 tristate "Qualcomm APQ8016/MSM8916 on-chip USB PHY controller support" 157 157 depends on ARCH_QCOM || COMPILE_TEST 158 - depends on RESET_CONTROLLER && EXTCON 158 + depends on RESET_CONTROLLER 159 159 select USB_PHY 160 160 select USB_ULPI_VIEWPORT 161 161 help
+18 -67
drivers/usb/phy/phy-msm-usb.c
··· 146 146 }; 147 147 148 148 /** 149 - * struct msm_usb_cable - structure for exteternal connector cable 150 - * state tracking 151 - * @nb: hold event notification callback 152 - * @conn: used for notification registration 153 - */ 154 - struct msm_usb_cable { 155 - struct notifier_block nb; 156 - struct extcon_dev *extcon; 157 - }; 158 - 159 - /** 160 149 * struct msm_otg: OTG driver data. Shared by HCD and DCD. 161 150 * @otg: USB OTG Transceiver structure. 162 151 * @pdata: otg device platform data. ··· 203 214 int vdd_levels[3]; 204 215 205 216 bool manual_pullup; 206 - 207 - struct msm_usb_cable vbus; 208 - struct msm_usb_cable id; 209 217 210 218 struct gpio_desc *switch_gpio; 211 219 struct notifier_block reboot; ··· 1598 1612 static int msm_otg_vbus_notifier(struct notifier_block *nb, unsigned long event, 1599 1613 void *ptr) 1600 1614 { 1601 - struct msm_usb_cable *vbus = container_of(nb, struct msm_usb_cable, nb); 1602 - struct msm_otg *motg = container_of(vbus, struct msm_otg, vbus); 1615 + struct usb_phy *usb_phy = container_of(nb, struct usb_phy, vbus_nb); 1616 + struct msm_otg *motg = container_of(usb_phy, struct msm_otg, phy); 1603 1617 1604 1618 if (event) 1605 1619 set_bit(B_SESS_VLD, &motg->inputs); ··· 1622 1636 static int msm_otg_id_notifier(struct notifier_block *nb, unsigned long event, 1623 1637 void *ptr) 1624 1638 { 1625 - struct msm_usb_cable *id = container_of(nb, struct msm_usb_cable, nb); 1626 - struct msm_otg *motg = container_of(id, struct msm_otg, id); 1639 + struct usb_phy *usb_phy = container_of(nb, struct usb_phy, id_nb); 1640 + struct msm_otg *motg = container_of(usb_phy, struct msm_otg, phy); 1627 1641 1628 1642 if (event) 1629 1643 clear_bit(ID, &motg->inputs); ··· 1638 1652 static int msm_otg_read_dt(struct platform_device *pdev, struct msm_otg *motg) 1639 1653 { 1640 1654 struct msm_otg_platform_data *pdata; 1641 - struct extcon_dev *ext_id, *ext_vbus; 1642 1655 struct device_node *node = pdev->dev.of_node; 1643 1656 struct property *prop; 1644 1657 int len, ret, words; ··· 1692 1707 GPIOD_OUT_LOW); 1693 1708 if (IS_ERR(motg->switch_gpio)) 1694 1709 return PTR_ERR(motg->switch_gpio); 1695 - 1696 - ext_id = ERR_PTR(-ENODEV); 1697 - ext_vbus = ERR_PTR(-ENODEV); 1698 - if (of_property_read_bool(node, "extcon")) { 1699 - 1700 - /* Each one of them is not mandatory */ 1701 - ext_vbus = extcon_get_edev_by_phandle(&pdev->dev, 0); 1702 - if (IS_ERR(ext_vbus) && PTR_ERR(ext_vbus) != -ENODEV) 1703 - return PTR_ERR(ext_vbus); 1704 - 1705 - ext_id = extcon_get_edev_by_phandle(&pdev->dev, 1); 1706 - if (IS_ERR(ext_id) && PTR_ERR(ext_id) != -ENODEV) 1707 - return PTR_ERR(ext_id); 1708 - } 1709 - 1710 - if (!IS_ERR(ext_vbus)) { 1711 - motg->vbus.extcon = ext_vbus; 1712 - motg->vbus.nb.notifier_call = msm_otg_vbus_notifier; 1713 - ret = devm_extcon_register_notifier(&pdev->dev, ext_vbus, 1714 - EXTCON_USB, &motg->vbus.nb); 1715 - if (ret < 0) { 1716 - dev_err(&pdev->dev, "register VBUS notifier failed\n"); 1717 - return ret; 1718 - } 1719 - 1720 - ret = extcon_get_state(ext_vbus, EXTCON_USB); 1721 - if (ret) 1722 - set_bit(B_SESS_VLD, &motg->inputs); 1723 - else 1724 - clear_bit(B_SESS_VLD, &motg->inputs); 1725 - } 1726 - 1727 - if (!IS_ERR(ext_id)) { 1728 - motg->id.extcon = ext_id; 1729 - motg->id.nb.notifier_call = msm_otg_id_notifier; 1730 - ret = devm_extcon_register_notifier(&pdev->dev, ext_id, 1731 - EXTCON_USB_HOST, &motg->id.nb); 1732 - if (ret < 0) { 1733 - dev_err(&pdev->dev, "register ID notifier failed\n"); 1734 - return ret; 1735 - } 1736 - 1737 - ret = extcon_get_state(ext_id, EXTCON_USB_HOST); 1738 - if (ret) 1739 - clear_bit(ID, &motg->inputs); 1740 - else 1741 - set_bit(ID, &motg->inputs); 1742 - } 1743 1710 1744 1711 prop = of_find_property(node, "qcom,phy-init-sequence", &len); 1745 1712 if (!prop || !len) ··· 1869 1932 phy->init = msm_phy_init; 1870 1933 phy->notify_disconnect = msm_phy_notify_disconnect; 1871 1934 phy->type = USB_PHY_TYPE_USB2; 1935 + phy->vbus_nb.notifier_call = msm_otg_vbus_notifier; 1936 + phy->id_nb.notifier_call = msm_otg_id_notifier; 1872 1937 1873 1938 phy->io_ops = &msm_otg_io_ops; 1874 1939 ··· 1885 1946 dev_err(&pdev->dev, "usb_add_phy failed\n"); 1886 1947 goto disable_ldo; 1887 1948 } 1949 + 1950 + ret = extcon_get_state(phy->edev, EXTCON_USB); 1951 + if (ret) 1952 + set_bit(B_SESS_VLD, &motg->inputs); 1953 + else 1954 + clear_bit(B_SESS_VLD, &motg->inputs); 1955 + 1956 + ret = extcon_get_state(phy->id_edev, EXTCON_USB_HOST); 1957 + if (ret) 1958 + clear_bit(ID, &motg->inputs); 1959 + else 1960 + set_bit(ID, &motg->inputs); 1888 1961 1889 1962 platform_set_drvdata(pdev, motg); 1890 1963 device_init_wakeup(&pdev->dev, 1);
+5 -15
drivers/usb/phy/phy-qcom-8x16-usb.c
··· 69 69 70 70 struct reset_control *phy_reset; 71 71 72 - struct extcon_dev *vbus_edev; 73 - struct notifier_block vbus_notify; 74 - 75 72 struct gpio_desc *switch_gpio; 76 73 struct notifier_block reboot_notify; 77 74 }; ··· 128 131 static int phy_8x16_vbus_notify(struct notifier_block *nb, unsigned long event, 129 132 void *ptr) 130 133 { 131 - struct phy_8x16 *qphy = container_of(nb, struct phy_8x16, vbus_notify); 134 + struct usb_phy *usb_phy = container_of(nb, struct usb_phy, vbus_nb); 135 + struct phy_8x16 *qphy = container_of(usb_phy, struct phy_8x16, phy); 132 136 133 137 if (event) 134 138 phy_8x16_vbus_on(qphy); ··· 185 187 val = ULPI_PWR_OTG_COMP_DISABLE; 186 188 usb_phy_io_write(phy, val, ULPI_SET(ULPI_PWR_CLK_MNG_REG)); 187 189 188 - state = extcon_get_state(qphy->vbus_edev, EXTCON_USB); 190 + state = extcon_get_state(qphy->phy.edev, EXTCON_USB); 189 191 if (state) 190 192 phy_8x16_vbus_on(qphy); 191 193 else ··· 287 289 phy->io_priv = qphy->regs + HSPHY_ULPI_VIEWPORT; 288 290 phy->io_ops = &ulpi_viewport_access_ops; 289 291 phy->type = USB_PHY_TYPE_USB2; 292 + phy->vbus_nb.notifier_call = phy_8x16_vbus_notify; 293 + phy->id_nb.notifier_call = NULL; 290 294 291 295 ret = phy_8x16_read_devicetree(qphy); 292 296 if (ret < 0) 293 297 return ret; 294 - 295 - qphy->vbus_edev = extcon_get_edev_by_phandle(phy->dev, 0); 296 - if (IS_ERR(qphy->vbus_edev)) 297 - return PTR_ERR(qphy->vbus_edev); 298 298 299 299 ret = clk_set_rate(qphy->core_clk, INT_MAX); 300 300 if (ret < 0) ··· 310 314 qphy->regulator); 311 315 if (WARN_ON(ret)) 312 316 goto off_clks; 313 - 314 - qphy->vbus_notify.notifier_call = phy_8x16_vbus_notify; 315 - ret = devm_extcon_register_notifier(&pdev->dev, qphy->vbus_edev, 316 - EXTCON_USB, &qphy->vbus_notify); 317 - if (ret < 0) 318 - goto off_power; 319 317 320 318 ret = usb_add_phy_dev(&qphy->phy); 321 319 if (ret)
+57
drivers/usb/phy/phy.c
··· 100 100 return *phy == match_data; 101 101 } 102 102 103 + static int usb_add_extcon(struct usb_phy *x) 104 + { 105 + int ret; 106 + 107 + if (of_property_read_bool(x->dev->of_node, "extcon")) { 108 + x->edev = extcon_get_edev_by_phandle(x->dev, 0); 109 + if (IS_ERR(x->edev)) 110 + return PTR_ERR(x->edev); 111 + 112 + x->id_edev = extcon_get_edev_by_phandle(x->dev, 1); 113 + if (IS_ERR(x->id_edev)) { 114 + x->id_edev = NULL; 115 + dev_info(x->dev, "No separate ID extcon device\n"); 116 + } 117 + 118 + if (x->vbus_nb.notifier_call) { 119 + ret = devm_extcon_register_notifier(x->dev, x->edev, 120 + EXTCON_USB, 121 + &x->vbus_nb); 122 + if (ret < 0) { 123 + dev_err(x->dev, 124 + "register VBUS notifier failed\n"); 125 + return ret; 126 + } 127 + } 128 + 129 + if (x->id_nb.notifier_call) { 130 + struct extcon_dev *id_ext; 131 + 132 + if (x->id_edev) 133 + id_ext = x->id_edev; 134 + else 135 + id_ext = x->edev; 136 + 137 + ret = devm_extcon_register_notifier(x->dev, id_ext, 138 + EXTCON_USB_HOST, 139 + &x->id_nb); 140 + if (ret < 0) { 141 + dev_err(x->dev, 142 + "register ID notifier failed\n"); 143 + return ret; 144 + } 145 + } 146 + } 147 + 148 + return 0; 149 + } 150 + 103 151 /** 104 152 * devm_usb_get_phy - find the USB PHY 105 153 * @dev - device that requests this phy ··· 436 388 return -EINVAL; 437 389 } 438 390 391 + ret = usb_add_extcon(x); 392 + if (ret) 393 + return ret; 394 + 439 395 ATOMIC_INIT_NOTIFIER_HEAD(&x->notifier); 440 396 441 397 spin_lock_irqsave(&phy_lock, flags); ··· 474 422 { 475 423 struct usb_phy_bind *phy_bind; 476 424 unsigned long flags; 425 + int ret; 477 426 478 427 if (!x->dev) { 479 428 dev_err(x->dev, "no device provided for PHY\n"); 480 429 return -EINVAL; 481 430 } 431 + 432 + ret = usb_add_extcon(x); 433 + if (ret) 434 + return ret; 482 435 483 436 ATOMIC_INIT_NOTIFIER_HEAD(&x->notifier); 484 437
+4
include/linux/usb/gadget.h
··· 304 304 int (*udc_start)(struct usb_gadget *, 305 305 struct usb_gadget_driver *); 306 306 int (*udc_stop)(struct usb_gadget *); 307 + void (*udc_set_speed)(struct usb_gadget *, enum usb_device_speed); 307 308 struct usb_ep *(*match_ep)(struct usb_gadget *, 308 309 struct usb_endpoint_descriptor *, 309 310 struct usb_ss_ep_comp_descriptor *); ··· 353 352 * @deactivated: True if gadget is deactivated - in deactivated state it cannot 354 353 * be connected. 355 354 * @connected: True if gadget is connected. 355 + * @lpm_capable: If the gadget max_speed is FULL or HIGH, this flag 356 + * indicates that it supports LPM as per the LPM ECN & errata. 356 357 * 357 358 * Gadgets have a mostly-portable "gadget driver" implementing device 358 359 * functions, handling all usb configurations and interfaces. Gadget ··· 407 404 unsigned is_selfpowered:1; 408 405 unsigned deactivated:1; 409 406 unsigned connected:1; 407 + unsigned lpm_capable:1; 410 408 }; 411 409 #define work_to_gadget(w) (container_of((w), struct usb_gadget, work)) 412 410
+7
include/linux/usb/phy.h
··· 9 9 #ifndef __LINUX_USB_PHY_H 10 10 #define __LINUX_USB_PHY_H 11 11 12 + #include <linux/extcon.h> 12 13 #include <linux/notifier.h> 13 14 #include <linux/usb.h> 14 15 ··· 85 84 struct device *io_dev; 86 85 struct usb_phy_io_ops *io_ops; 87 86 void __iomem *io_priv; 87 + 88 + /* to support extcon device */ 89 + struct extcon_dev *edev; 90 + struct extcon_dev *id_edev; 91 + struct notifier_block vbus_nb; 92 + struct notifier_block id_nb; 88 93 89 94 /* for notification of usb_phy_events */ 90 95 struct atomic_notifier_head notifier;
+4 -3
include/uapi/linux/usb/functionfs.h
··· 275 275 #define FUNCTIONFS_INTERFACE_REVMAP _IO('g', 128) 276 276 277 277 /* 278 - * Returns real bEndpointAddress of an endpoint. If function is not 279 - * active returns -ENODEV. 278 + * Returns real bEndpointAddress of an endpoint. If endpoint shuts down 279 + * during the call, returns -ESHUTDOWN. 280 280 */ 281 281 #define FUNCTIONFS_ENDPOINT_REVMAP _IO('g', 129) 282 282 283 283 /* 284 - * Returns endpoint descriptor. If function is not active returns -ENODEV. 284 + * Returns endpoint descriptor. If endpoint shuts down during the call, 285 + * returns -ESHUTDOWN. 285 286 */ 286 287 #define FUNCTIONFS_ENDPOINT_DESC _IOR('g', 130, \ 287 288 struct usb_endpoint_descriptor)
+9 -7
tools/usb/testusb.c
··· 387 387 /* pick defaults that works with all speeds, without short packets. 388 388 * 389 389 * Best per-frame data rates: 390 - * high speed, bulk 512 * 13 * 8 = 53248 391 - * interrupt 1024 * 3 * 8 = 24576 392 - * full speed, bulk/intr 64 * 19 = 1216 393 - * interrupt 64 * 1 = 64 394 - * low speed, interrupt 8 * 1 = 8 390 + * super speed,bulk 1024 * 16 * 8 = 131072 391 + * interrupt 1024 * 3 * 8 = 24576 392 + * high speed, bulk 512 * 13 * 8 = 53248 393 + * interrupt 1024 * 3 * 8 = 24576 394 + * full speed, bulk/intr 64 * 19 = 1216 395 + * interrupt 64 * 1 = 64 396 + * low speed, interrupt 8 * 1 = 8 395 397 */ 396 398 param.iterations = 1000; 397 399 param.length = 1024; 398 - param.vary = 512; 400 + param.vary = 1024; 399 401 param.sglen = 32; 400 402 401 403 /* for easy use when hotplugging */ ··· 459 457 "\t-c iterations default 1000\n" 460 458 "\t-s transfer length default 1024\n" 461 459 "\t-g sglen default 32\n" 462 - "\t-v vary default 512\n", 460 + "\t-v vary default 1024\n", 463 461 argv[0]); 464 462 return 1; 465 463 }