kernel: bump 4.14 to 4.14.176
[openwrt/openwrt.git] / target / linux / brcm2708 / patches-4.14 / 950-0037-Add-dwc_otg-driver.patch
1 From 451dab6b675762f8889979b04ee3e529eee915e8 Mon Sep 17 00:00:00 2001
2 From: popcornmix <popcornmix@gmail.com>
3 Date: Wed, 1 May 2013 19:46:17 +0100
4 Subject: [PATCH 037/454] Add dwc_otg driver
5 MIME-Version: 1.0
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
8
9 Signed-off-by: popcornmix <popcornmix@gmail.com>
10
11 usb: dwc: fix lockdep false positive
12
13 Signed-off-by: Kari Suvanto <karis79@gmail.com>
14
15 usb: dwc: fix inconsistent lock state
16
17 Signed-off-by: Kari Suvanto <karis79@gmail.com>
18
19 Add FIQ patch to dwc_otg driver. Enable with dwc_otg.fiq_fix_enable=1. Should give about 10% more ARM performance.
20 Thanks to Gordon and Costas
21
22 Avoid dynamic memory allocation for channel lock in USB driver. Thanks ddv2005.
23
24 Add NAK holdoff scheme. Enabled by default, disable with dwc_otg.nak_holdoff_enable=0. Thanks gsh
25
26 Make sure we wait for the reset to finish
27
28 dwc_otg: fix bug in dwc_otg_hcd.c resulting in silent kernel
29 memory corruption, escalating to OOPS under high USB load.
30
31 dwc_otg: Fix unsafe access of QTD during URB enqueue
32
33 In dwc_otg_hcd_urb_enqueue during qtd creation, it was possible that the
34 transaction could complete almost immediately after the qtd was assigned
35 to a host channel during URB enqueue, which meant the qtd pointer was no
36 longer valid having been completed and removed. Usually, this resulted in
37 an OOPS during URB submission. By predetermining whether transactions
38 need to be queued or not, this unsafe pointer access is avoided.
39
40 This bug was only evident on the Pi model A where a device was attached
41 that had no periodic endpoints (e.g. USB pendrive or some wlan devices).
42
43 dwc_otg: Fix incorrect URB allocation error handling
44
45 If the memory allocation for a dwc_otg_urb failed, the kernel would OOPS
46 because for some reason a member of the *unallocated* struct was set to
47 zero. Error handling changed to fail correctly.
48
49 dwc_otg: fix potential use-after-free case in interrupt handler
50
51 If a transaction had previously aborted, certain interrupts are
52 enabled to track error counts and reset where necessary. On IN
53 endpoints the host generates an ACK interrupt near-simultaneously
54 with completion of transfer. In the case where this transfer had
55 previously had an error, this results in a use-after-free on
56 the QTD memory space with a 1-byte length being overwritten to
57 0x00.
58
59 dwc_otg: add handling of SPLIT transaction data toggle errors
60
61 Previously a data toggle error on packets from a USB1.1 device behind
62 a TT would result in the Pi locking up as the driver never handled
63 the associated interrupt. Patch adds basic retry mechanism and
64 interrupt acknowledgement to cater for either a chance toggle error or
65 for devices that have a broken initial toggle state (FT8U232/FT232BM).
66
67 dwc_otg: implement tasklet for returning URBs to usbcore hcd layer
68
69 The dwc_otg driver interrupt handler for transfer completion will spend
70 a very long time with interrupts disabled when a URB is completed -
71 this is because usb_hcd_giveback_urb is called from within the handler
72 which for a USB device driver with complicated processing (e.g. webcam)
73 will take an exorbitant amount of time to complete. This results in
74 missed completion interrupts for other USB packets which lead to them
75 being dropped due to microframe overruns.
76
77 This patch splits returning the URB to the usb hcd layer into a
78 high-priority tasklet. This will have most benefit for isochronous IN
79 transfers but will also have incidental benefit where multiple periodic
80 devices are active at once.
81
82 dwc_otg: fix NAK holdoff and allow on split transactions only
83
84 This corrects a bug where if a single active non-periodic endpoint
85 had at least one transaction in its qh, on frnum == MAX_FRNUM the qh
86 would get skipped and never get queued again. This would result in
87 a silent device until error detection (automatic or otherwise) would
88 either reset the device or flush and requeue the URBs.
89
90 Additionally the NAK holdoff was enabled for all transactions - this
91 would potentially stall a HS endpoint for 1ms if a previous error state
92 enabled this interrupt and the next response was a NAK. Fix so that
93 only split transactions get held off.
94
95 dwc_otg: Call usb_hcd_unlink_urb_from_ep with lock held in completion handler
96
97 usb_hcd_unlink_urb_from_ep must be called with the HCD lock held. Calling it
98 asynchronously in the tasklet was not safe (regression in
99 c4564d4a1a0a9b10d4419e48239f5d99e88d2667).
100
101 This change unlinks it from the endpoint prior to queueing it for handling in
102 the tasklet, and also adds a check to ensure the urb is OK to be unlinked
103 before doing so.
104
105 NULL pointer dereference kernel oopses had been observed in usb_hcd_giveback_urb
106 when a USB device was unplugged/replugged during data transfer. This effect
107 was reproduced using automated USB port power control, hundreds of replug
108 events were performed during active transfers to confirm that the problem was
109 eliminated.
110
111 USB fix using a FIQ to implement split transactions
112
113 This commit adds a FIQ implementaion that schedules
114 the split transactions using a FIQ so we don't get
115 held off by the interrupt latency of Linux
116
117 dwc_otg: fix device attributes and avoid kernel warnings on boot
118
119 dcw_otg: avoid logging function that can cause panics
120
121 See: https://github.com/raspberrypi/firmware/issues/21
122 Thanks to cleverca22 for fix
123
124 dwc_otg: mask correct interrupts after transaction error recovery
125
126 The dwc_otg driver will unmask certain interrupts on a transaction
127 that previously halted in the error state in order to reset the
128 QTD error count. The various fine-grained interrupt handlers do not
129 consider that other interrupts besides themselves were unmasked.
130
131 By disabling the two other interrupts only ever enabled in DMA mode
132 for this purpose, we can avoid unnecessary function calls in the
133 IRQ handler. This will also prevent an unneccesary FIQ interrupt
134 from being generated if the FIQ is enabled.
135
136 dwc_otg: fiq: prevent FIQ thrash and incorrect state passing to IRQ
137
138 In the case of a transaction to a device that had previously aborted
139 due to an error, several interrupts are enabled to reset the error
140 count when a device responds. This has the side-effect of making the
141 FIQ thrash because the hardware will generate multiple instances of
142 a NAK on an IN bulk/interrupt endpoint and multiple instances of ACK
143 on an OUT bulk/interrupt endpoint. Make the FIQ mask and clear the
144 associated interrupts.
145
146 Additionally, on non-split transactions make sure that only unmasked
147 interrupts are cleared. This caused a hard-to-trigger but serious
148 race condition when you had the combination of an endpoint awaiting
149 error recovery and a transaction completed on an endpoint - due to
150 the sequencing and timing of interrupts generated by the dwc_otg core,
151 it was possible to confuse the IRQ handler.
152
153 Fix function tracing
154
155 dwc_otg: whitespace cleanup in dwc_otg_urb_enqueue
156
157 dwc_otg: prevent OOPSes during device disconnects
158
159 The dwc_otg_urb_enqueue function is thread-unsafe. In particular the
160 access of urb->hcpriv, usb_hcd_link_urb_to_ep, dwc_otg_urb->qtd and
161 friends does not occur within a critical section and so if a device
162 was unplugged during activity there was a high chance that the
163 usbcore hub_thread would try to disable the endpoint with partially-
164 formed entries in the URB queue. This would result in BUG() or null
165 pointer dereferences.
166
167 Fix so that access of urb->hcpriv, enqueuing to the hardware and
168 adding to usbcore endpoint URB lists is contained within a single
169 critical section.
170
171 dwc_otg: prevent BUG() in TT allocation if hub address is > 16
172
173 A fixed-size array is used to track TT allocation. This was
174 previously set to 16 which caused a crash because
175 dwc_otg_hcd_allocate_port would read past the end of the array.
176
177 This was hit if a hub was plugged in which enumerated as addr > 16,
178 due to previous device resets or unplugs.
179
180 Also add #ifdef FIQ_DEBUG around hcd->hub_port_alloc[], which grows
181 to a large size if 128 hub addresses are supported. This field is
182 for debug only for tracking which frame an allocate happened in.
183
184 dwc_otg: make channel halts with unknown state less damaging
185
186 If the IRQ received a channel halt interrupt through the FIQ
187 with no other bits set, the IRQ would not release the host
188 channel and never complete the URB.
189
190 Add catchall handling to treat as a transaction error and retry.
191
192 dwc_otg: fiq_split: use TTs with more granularity
193
194 This fixes certain issues with split transaction scheduling.
195
196 - Isochronous multi-packet OUT transactions now hog the TT until
197 they are completed - this prevents hubs aborting transactions
198 if they get a periodic start-split out-of-order
199 - Don't perform TT allocation on non-periodic endpoints - this
200 allows simultaneous use of the TT's bulk/control and periodic
201 transaction buffers
202
203 This commit will mainly affect USB audio playback.
204
205 dwc_otg: fix potential sleep while atomic during urb enqueue
206
207 Fixes a regression introduced with eb1b482a. Kmalloc called from
208 dwc_otg_hcd_qtd_add / dwc_otg_hcd_qtd_create did not always have
209 the GPF_ATOMIC flag set. Force this flag when inside the larger
210 critical section.
211
212 dwc_otg: make fiq_split_enable imply fiq_fix_enable
213
214 Failing to set up the FIQ correctly would result in
215 "IRQ 32: nobody cared" errors in dmesg.
216
217 dwc_otg: prevent crashes on host port disconnects
218
219 Fix several issues resulting in crashes or inconsistent state
220 if a Model A root port was disconnected.
221
222 - Clean up queue heads properly in kill_urbs_in_qh_list by
223 removing the empty QHs from the schedule lists
224 - Set the halt status properly to prevent IRQ handlers from
225 using freed memory
226 - Add fiq_split related cleanup for saved registers
227 - Make microframe scheduling reclaim host channels if
228 active during a disconnect
229 - Abort URBs with -ESHUTDOWN status response, informing
230 device drivers so they respond in a more correct fashion
231 and don't try to resubmit URBs
232 - Prevent IRQ handlers from attempting to handle channel
233 interrupts if the associated URB was dequeued (and the
234 driver state was cleared)
235
236 dwc_otg: prevent leaking URBs during enqueue
237
238 A dwc_otg_urb would get leaked if the HCD enqueue function
239 failed for any reason. Free the URB at the appropriate points.
240
241 dwc_otg: Enable NAK holdoff for control split transactions
242
243 Certain low-speed devices take a very long time to complete a
244 data or status stage of a control transaction, producing NAK
245 responses until they complete internal processing - the USB2.0
246 spec limit is up to 500mS. This causes the same type of interrupt
247 storm as seen with USB-serial dongles prior to c8edb238.
248
249 In certain circumstances, usually while booting, this interrupt
250 storm could cause SD card timeouts.
251
252 dwc_otg: Fix for occasional lockup on boot when doing a USB reset
253
254 dwc_otg: Don't issue traffic to LS devices in FS mode
255
256 Issuing low-speed packets when the root port is in full-speed mode
257 causes the root port to stop responding. Explicitly fail when
258 enqueuing URBs to a LS endpoint on a FS bus.
259
260 Fix ARM architecture issue with local_irq_restore()
261
262 If local_fiq_enable() is called before a local_irq_restore(flags) where
263 the flags variable has the F bit set, the FIQ will be erroneously disabled.
264
265 Fixup arch_local_irq_restore to avoid trampling the F bit in CPSR.
266
267 Also fix some of the hacks previously implemented for previous dwc_otg
268 incarnations.
269
270 dwc_otg: fiq_fsm: Base commit for driver rewrite
271
272 This commit removes the previous FIQ fixes entirely and adds fiq_fsm.
273
274 This rewrite features much more complete support for split transactions
275 and takes into account several OTG hardware bugs. High-speed
276 isochronous transactions are also capable of being performed by fiq_fsm.
277
278 All driver options have been removed and replaced with:
279 - dwc_otg.fiq_enable (bool)
280 - dwc_otg.fiq_fsm_enable (bool)
281 - dwc_otg.fiq_fsm_mask (bitmask)
282 - dwc_otg.nak_holdoff (unsigned int)
283
284 Defaults are specified such that fiq_fsm behaves similarly to the
285 previously implemented FIQ fixes.
286
287 fiq_fsm: Push error recovery into the FIQ when fiq_fsm is used
288
289 If the transfer associated with a QTD failed due to a bus error, the HCD
290 would retry the transfer up to 3 times (implementing the USB2.0
291 three-strikes retry in software).
292
293 Due to the masking mechanism used by fiq_fsm, it is only possible to pass
294 a single interrupt through to the HCD per-transfer.
295
296 In this instance host channels would fall off the radar because the error
297 reset would function, but the subsequent channel halt would be lost.
298
299 Push the error count reset into the FIQ handler.
300
301 fiq_fsm: Implement timeout mechanism
302
303 For full-speed endpoints with a large packet size, interrupt latency
304 runs the risk of the FIQ starting a transaction too late in a full-speed
305 frame. If the device is still transmitting data when EOF2 for the
306 downstream frame occurs, the hub will disable the port. This change is
307 not reflected in the hub status endpoint and the device becomes
308 unresponsive.
309
310 Prevent high-bandwidth transactions from being started too late in a
311 frame. The mechanism is not guaranteed: a combination of bit stuffing
312 and hub latency may still result in a device overrunning.
313
314 fiq_fsm: fix bounce buffer utilisation for Isochronous OUT
315
316 Multi-packet isochronous OUT transactions were subject to a few bounday
317 bugs. Fix them.
318
319 Audio playback is now much more robust: however, an issue stands with
320 devices that have adaptive sinks - ALSA plays samples too fast.
321
322 dwc_otg: Return full-speed frame numbers in HS mode
323
324 The frame counter increments on every *microframe* in high-speed mode.
325 Most device drivers expect this number to be in full-speed frames - this
326 caused considerable confusion to e.g. snd_usb_audio which uses the
327 frame counter to estimate the number of samples played.
328
329 fiq_fsm: save PID on completion of interrupt OUT transfers
330
331 Also add edge case handling for interrupt transports.
332
333 Note that for periodic split IN, data toggles are unimplemented in the
334 OTG host hardware - it unconditionally accepts any PID.
335
336 fiq_fsm: add missing case for fiq_fsm_tt_in_use()
337
338 Certain combinations of bitrate and endpoint activity could
339 result in a periodic transaction erroneously getting started
340 while the previous Isochronous OUT was still active.
341
342 fiq_fsm: clear hcintmsk for aborted transactions
343
344 Prevents the FIQ from erroneously handling interrupts
345 on a timed out channel.
346
347 fiq_fsm: enable by default
348
349 fiq_fsm: fix dequeues for non-periodic split transactions
350
351 If a dequeue happened between the SSPLIT and CSPLIT phases of the
352 transaction, the HCD would never receive an interrupt.
353
354 fiq_fsm: Disable by default
355
356 fiq_fsm: Handle HC babble errors
357
358 The HCTSIZ transfer size field raises a babble interrupt if
359 the counter wraps. Handle the resulting interrupt in this case.
360
361 dwc_otg: fix interrupt registration for fiq_enable=0
362
363 Additionally make the module parameter conditional for wherever
364 hcd->fiq_state is touched.
365
366 fiq_fsm: Enable by default
367
368 dwc_otg: Fix various issues with root port and transaction errors
369
370 Process the host port interrupts correctly (and don't trample them).
371 Root port hotplug now functional again.
372
373 Fix a few thinkos with the transaction error passthrough for fiq_fsm.
374
375 fiq_fsm: Implement hack for Split Interrupt transactions
376
377 Hubs aren't too picky about which endpoint we send Control type split
378 transactions to. By treating Interrupt transfers as Control, it is
379 possible to use the non-periodic queue in the OTG core as well as the
380 non-periodic FIFOs in the hub itself. This massively reduces the
381 microframe exclusivity/contention that periodic split transactions
382 otherwise have to enforce.
383
384 It goes without saying that this is a fairly egregious USB specification
385 violation, but it works.
386
387 Original idea by Hans Petter Selasky @ FreeBSD.org.
388
389 dwc_otg: FIQ support on SMP. Set up FIQ stack and handler on Core 0 only.
390
391 dwc_otg: introduce fiq_fsm_spin(un|)lock()
392
393 SMP safety for the FIQ relies on register read-modify write cycles being
394 completed in the correct order. Several places in the DWC code modify
395 registers also touched by the FIQ. Protect these by a bare-bones lock
396 mechanism.
397
398 This also makes it possible to run the FIQ and IRQ handlers on different
399 cores.
400
401 fiq_fsm: fix build on bcm2708 and bcm2709 platforms
402
403 dwc_otg: put some barriers back where they should be for UP
404
405 bcm2709/dwc_otg: Setup FIQ on core 1 if >1 core active
406
407 dwc_otg: fixup read-modify-write in critical paths
408
409 Be more careful about read-modify-write on registers that the FIQ
410 also touches.
411
412 Guard fiq_fsm_spin_lock with fiq_enable check
413
414 fiq_fsm: Falling out of the state machine isn't fatal
415
416 This edge case can be hit if the port is disabled while the FIQ is
417 in the middle of a transaction. Make the effects less severe.
418
419 Also get rid of the useless return value.
420
421 squash: dwc_otg: Allow to build without SMP
422
423 usb: core: make overcurrent messages more prominent
424
425 Hub overcurrent messages are more serious than "debug". Increase loglevel.
426
427 usb: dwc_otg: Don't use dma_to_virt()
428
429 Commit 6ce0d20 changes dma_to_virt() which breaks this driver.
430 Open code the old dma_to_virt() implementation to work around this.
431
432 Limit the use of __bus_to_virt() to cases where transfer_buffer_length
433 is set and transfer_buffer is not set. This is done to increase the
434 chance that this driver will also work on ARCH_BCM2835.
435
436 transfer_buffer should not be NULL if the length is set, but the
437 comment in the code indicates that there are situations where this
438 might happen. drivers/usb/isp1760/isp1760-hcd.c also has a similar
439 comment pointing to a possible: 'usb storage / SCSI bug'.
440
441 Signed-off-by: Noralf Trønnes <noralf@tronnes.org>
442
443 dwc_otg: Fix crash when fiq_enable=0
444
445 dwc_otg: fiq_fsm: Make high-speed isochronous strided transfers work properly
446
447 Certain low-bandwidth high-speed USB devices (specialist audio devices,
448 compressed-frame webcams) have packet intervals > 1 microframe.
449
450 Stride these transfers in the FIQ by using the start-of-frame interrupt
451 to restart the channel at the right time.
452
453 dwc_otg: Force host mode to fix incorrect compute module boards
454
455 dwc_otg: Add ARCH_BCM2835 support
456
457 Signed-off-by: Noralf Trønnes <noralf@tronnes.org>
458
459 dwc_otg: Simplify FIQ irq number code
460
461 Dropping ATAGS means we can simplify the FIQ irq number code.
462 Also add error checking on the returned irq number.
463
464 Signed-off-by: Noralf Trønnes <noralf@tronnes.org>
465
466 dwc_otg: Remove duplicate gadget probe/unregister function
467
468 dwc_otg: Properly set the HFIR
469
470 Douglas Anderson reported:
471
472 According to the most up to date version of the dwc2 databook, the FRINT
473 field of the HFIR register should be programmed to:
474 * 125 us * (PHY clock freq for HS) - 1
475 * 1000 us * (PHY clock freq for FS/LS) - 1
476
477 This is opposed to older versions of the doc that claimed it should be:
478 * 125 us * (PHY clock freq for HS)
479 * 1000 us * (PHY clock freq for FS/LS)
480
481 and reported lower timing jitter on a USB analyser
482
483 dcw_otg: trim xfer length when buffer larger than allocated size is received
484
485 dwc_otg: Don't free qh align buffers in atomic context
486
487 dwc_otg: Enable the hack for Split Interrupt transactions by default
488
489 dwc_otg.fiq_fsm_mask=0xF has long been a suggestion for users with audio stutters or other USB bandwidth issues.
490 So far we are aware of many success stories but no failure caused by this setting.
491 Make it a default to learn more.
492
493 See: https://www.raspberrypi.org/forums/viewtopic.php?f=28&t=70437
494
495 Signed-off-by: popcornmix <popcornmix@gmail.com>
496
497 dwc_otg: Use kzalloc when suitable
498
499 dwc_otg: Pass struct device to dma_alloc*()
500
501 This makes it possible to get the bus address from Device Tree.
502
503 Signed-off-by: Noralf Trønnes <noralf@tronnes.org>
504
505 dwc_otg: fix summarize urb->actual_length for isochronous transfers
506
507 Kernel does not copy input data of ISO transfers to userspace
508 if actual_length is set only in ISO transfers and not summarized
509 in urb->actual_length. Fixes raspberrypi/linux#903
510
511 fiq_fsm: Use correct states when starting isoc OUT transfers
512
513 In fiq_fsm_start_next_periodic() if an isochronous OUT transfer
514 was selected, no regard was given as to whether this was a single-packet
515 transfer or a multi-packet staged transfer.
516
517 For single-packet transfers, this had the effect of repeatedly sending
518 OUT packets with bogus data and lengths.
519
520 Eventually if the channel was repeatedly enabled enough times, this
521 would lock up the OTG core and no further bus transfers would happen.
522
523 Set the FSM state up properly if we select a single-packet transfer.
524
525 Fixes https://github.com/raspberrypi/linux/issues/1842
526
527 dwc_otg: make nak_holdoff work as intended with empty queues
528
529 If URBs reading from non-periodic split endpoints were dequeued and
530 the last transfer from the endpoint was a NAK handshake, the resulting
531 qh->nak_frame value was stale which would result in unnecessarily long
532 polling intervals for the first subsequent transfer with a fresh URB.
533
534 Fixup qh->nak_frame in dwc_otg_hcd_urb_dequeue and also guard against
535 a case where a single URB is submitted to the endpoint, a NAK was
536 received on the transfer immediately prior to receiving data and the
537 device subsequently resubmits another URB past the qh->nak_frame interval.
538
539 Fixes https://github.com/raspberrypi/linux/issues/1709
540
541 dwc_otg: fix split transaction data toggle handling around dequeues
542
543 See https://github.com/raspberrypi/linux/issues/1709
544
545 Fix several issues regarding endpoint state when URBs are dequeued
546 - If the HCD is disconnected, flush FIQ-enabled channels properly
547 - Save the data toggle state for bulk endpoints if the last transfer
548 from an endpoint where URBs were dequeued returned a data packet
549 - Reset hc->start_pkt_count properly in assign_and_init_hc()
550
551 dwc_otg: fix several potential crash sources
552
553 On root port disconnect events, the host driver state is cleared and
554 in-progress host channels are forcibly stopped. This doesn't play
555 well with the FIQ running in the background, so:
556 - Guard the disconnect callback with both the host spinlock and FIQ
557 spinlock
558 - Move qtd dereference in dwc_otg_handle_hc_fsm() after the early-out
559 so we don't dereference a qtd that has gone away
560 - Turn catch-all BUG()s in dwc_otg_handle_hc_fsm() into warnings.
561
562 dwc_otg: delete hcd->channel_lock
563
564 The lock serves no purpose as it is only held while the HCD spinlock
565 is already being held.
566
567 dwc_otg: remove unnecessary dma-mode channel halts on disconnect interrupt
568
569 Host channels are already halted in kill_urbs_in_qh_list() with the
570 subsequent interrupt processing behaving as if the URB was dequeued
571 via HCD callback.
572
573 There's no need to clobber the host channel registers a second time
574 as this exposes races between the driver and host channel resulting
575 in hcd->free_hc_list becoming corrupted.
576
577 dwcotg: Allow to build without FIQ on ARM64
578
579 Signed-off-by: popcornmix <popcornmix@gmail.com>
580
581 dwc_otg: make periodic scheduling behave properly for FS buses
582
583 If the root port is in full-speed mode, transfer times at 12mbit/s
584 would be calculated but matched against high-speed quotas.
585
586 Reinitialise hcd->frame_usecs[i] on each port enable event so that
587 full-speed bandwidth can be tracked sensibly.
588
589 Also, don't bother using the FIQ for transfers when in full-speed
590 mode - at the slower bus speed, interrupt frequency is reduced by
591 an order of magnitude.
592
593 Related issue: https://github.com/raspberrypi/linux/issues/2020
594
595 dwc_otg: fiq_fsm: Make isochronous compatibility checks work properly
596
597 Get rid of the spammy printk and local pointer mangling.
598 Also, there is a nominal benefit for using fiq_fsm for isochronous
599 transfers in FS mode (~1.1k IRQs per second vs 2.1k IRQs per second)
600 so remove the root port speed check.
601
602 dwc_otg: add module parameter int_ep_interval_min
603
604 Add a module parameter (defaulting to ignored) that clamps the polling rate
605 of high-speed Interrupt endpoints to a minimum microframe interval.
606
607 The parameter is modifiable at runtime as it is used when activating new
608 endpoints (such as on device connect).
609
610 dwc_otg: fiq_fsm: Add non-periodic TT exclusivity constraints
611
612 Certain hub types do not discriminate between pipe direction (IN or OUT)
613 when considering non-periodic transfers. Therefore these hubs get confused
614 if multiple transfers are issued in different directions with the same
615 device address and endpoint number.
616
617 Constrain queuing non-periodic split transactions so they are performed
618 serially in such cases.
619
620 Related: https://github.com/raspberrypi/linux/issues/2024
621
622 dwc_otg: Fixup change to DRIVER_ATTR interface
623
624 dwc_otg: Fix compilation warnings
625
626 Signed-off-by: Phil Elwell <phil@raspberrypi.org>
627
628 USB_DWCOTG: Disable building dwc_otg as a module (#2265)
629
630 When dwc_otg is built as a module, build will fail with the following
631 error:
632
633 ERROR: "DWC_TASK_HI_SCHEDULE" [drivers/usb/host/dwc_otg/dwc_otg.ko] undefined!
634 scripts/Makefile.modpost:91: recipe for target '__modpost' failed
635 make[1]: *** [__modpost] Error 1
636 Makefile:1199: recipe for target 'modules' failed
637 make: *** [modules] Error 2
638
639 Even if the error is solved by including the missing
640 DWC_TASK_HI_SCHEDULE function, the kernel will panic when loading
641 dwc_otg.
642
643 As a workaround, simply prevent user from building dwc_otg as a module
644 as the current kernel does not support it.
645
646 See: https://github.com/raspberrypi/linux/issues/2258
647
648 Signed-off-by: Malik Olivier Boussejra <malik@boussejra.com>
649 ---
650 arch/arm/include/asm/irqflags.h | 16 +-
651 arch/arm/kernel/fiqasm.S | 4 +
652 drivers/usb/Makefile | 1 +
653 drivers/usb/core/generic.c | 1 +
654 drivers/usb/core/hub.c | 2 +-
655 drivers/usb/core/message.c | 79 +
656 drivers/usb/core/otg_whitelist.h | 114 +-
657 drivers/usb/gadget/file_storage.c | 3676 +++++++++
658 drivers/usb/host/Kconfig | 10 +
659 drivers/usb/host/Makefile | 2 +
660 drivers/usb/host/dwc_common_port/Makefile | 58 +
661 .../usb/host/dwc_common_port/Makefile.fbsd | 17 +
662 .../usb/host/dwc_common_port/Makefile.linux | 49 +
663 drivers/usb/host/dwc_common_port/changes.txt | 174 +
664 .../usb/host/dwc_common_port/doc/doxygen.cfg | 270 +
665 drivers/usb/host/dwc_common_port/dwc_cc.c | 532 ++
666 drivers/usb/host/dwc_common_port/dwc_cc.h | 224 +
667 .../host/dwc_common_port/dwc_common_fbsd.c | 1308 +++
668 .../host/dwc_common_port/dwc_common_linux.c | 1418 ++++
669 .../host/dwc_common_port/dwc_common_nbsd.c | 1275 +++
670 drivers/usb/host/dwc_common_port/dwc_crypto.c | 308 +
671 drivers/usb/host/dwc_common_port/dwc_crypto.h | 111 +
672 drivers/usb/host/dwc_common_port/dwc_dh.c | 291 +
673 drivers/usb/host/dwc_common_port/dwc_dh.h | 106 +
674 drivers/usb/host/dwc_common_port/dwc_list.h | 594 ++
675 drivers/usb/host/dwc_common_port/dwc_mem.c | 245 +
676 drivers/usb/host/dwc_common_port/dwc_modpow.c | 636 ++
677 drivers/usb/host/dwc_common_port/dwc_modpow.h | 34 +
678 .../usb/host/dwc_common_port/dwc_notifier.c | 319 +
679 .../usb/host/dwc_common_port/dwc_notifier.h | 122 +
680 drivers/usb/host/dwc_common_port/dwc_os.h | 1276 +++
681 drivers/usb/host/dwc_common_port/usb.h | 946 +++
682 drivers/usb/host/dwc_otg/Makefile | 82 +
683 drivers/usb/host/dwc_otg/doc/doxygen.cfg | 224 +
684 drivers/usb/host/dwc_otg/dummy_audio.c | 1574 ++++
685 drivers/usb/host/dwc_otg/dwc_cfi_common.h | 142 +
686 drivers/usb/host/dwc_otg/dwc_otg_adp.c | 854 ++
687 drivers/usb/host/dwc_otg/dwc_otg_adp.h | 80 +
688 drivers/usb/host/dwc_otg/dwc_otg_attr.c | 1212 +++
689 drivers/usb/host/dwc_otg/dwc_otg_attr.h | 89 +
690 drivers/usb/host/dwc_otg/dwc_otg_cfi.c | 1876 +++++
691 drivers/usb/host/dwc_otg/dwc_otg_cfi.h | 320 +
692 drivers/usb/host/dwc_otg/dwc_otg_cil.c | 7141 +++++++++++++++++
693 drivers/usb/host/dwc_otg/dwc_otg_cil.h | 1464 ++++
694 drivers/usb/host/dwc_otg/dwc_otg_cil_intr.c | 1596 ++++
695 drivers/usb/host/dwc_otg/dwc_otg_core_if.h | 705 ++
696 drivers/usb/host/dwc_otg/dwc_otg_dbg.h | 117 +
697 drivers/usb/host/dwc_otg/dwc_otg_driver.c | 1760 ++++
698 drivers/usb/host/dwc_otg/dwc_otg_driver.h | 86 +
699 drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.c | 1389 ++++
700 drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.h | 372 +
701 drivers/usb/host/dwc_otg/dwc_otg_fiq_stub.S | 80 +
702 drivers/usb/host/dwc_otg/dwc_otg_hcd.c | 4283 ++++++++++
703 drivers/usb/host/dwc_otg/dwc_otg_hcd.h | 870 ++
704 drivers/usb/host/dwc_otg/dwc_otg_hcd_ddma.c | 1134 +++
705 drivers/usb/host/dwc_otg/dwc_otg_hcd_if.h | 417 +
706 drivers/usb/host/dwc_otg/dwc_otg_hcd_intr.c | 2752 +++++++
707 drivers/usb/host/dwc_otg/dwc_otg_hcd_linux.c | 1007 +++
708 drivers/usb/host/dwc_otg/dwc_otg_hcd_queue.c | 971 +++
709 drivers/usb/host/dwc_otg/dwc_otg_os_dep.h | 188 +
710 drivers/usb/host/dwc_otg/dwc_otg_pcd.c | 2725 +++++++
711 drivers/usb/host/dwc_otg/dwc_otg_pcd.h | 273 +
712 drivers/usb/host/dwc_otg/dwc_otg_pcd_if.h | 361 +
713 drivers/usb/host/dwc_otg/dwc_otg_pcd_intr.c | 5148 ++++++++++++
714 drivers/usb/host/dwc_otg/dwc_otg_pcd_linux.c | 1280 +++
715 drivers/usb/host/dwc_otg/dwc_otg_regs.h | 2550 ++++++
716 drivers/usb/host/dwc_otg/test/Makefile | 16 +
717 drivers/usb/host/dwc_otg/test/dwc_otg_test.pm | 337 +
718 .../usb/host/dwc_otg/test/test_mod_param.pl | 133 +
719 drivers/usb/host/dwc_otg/test/test_sysfs.pl | 193 +
720 70 files changed, 60003 insertions(+), 16 deletions(-)
721 create mode 100644 drivers/usb/gadget/file_storage.c
722 create mode 100644 drivers/usb/host/dwc_common_port/Makefile
723 create mode 100644 drivers/usb/host/dwc_common_port/Makefile.fbsd
724 create mode 100644 drivers/usb/host/dwc_common_port/Makefile.linux
725 create mode 100644 drivers/usb/host/dwc_common_port/changes.txt
726 create mode 100644 drivers/usb/host/dwc_common_port/doc/doxygen.cfg
727 create mode 100644 drivers/usb/host/dwc_common_port/dwc_cc.c
728 create mode 100644 drivers/usb/host/dwc_common_port/dwc_cc.h
729 create mode 100644 drivers/usb/host/dwc_common_port/dwc_common_fbsd.c
730 create mode 100644 drivers/usb/host/dwc_common_port/dwc_common_linux.c
731 create mode 100644 drivers/usb/host/dwc_common_port/dwc_common_nbsd.c
732 create mode 100644 drivers/usb/host/dwc_common_port/dwc_crypto.c
733 create mode 100644 drivers/usb/host/dwc_common_port/dwc_crypto.h
734 create mode 100644 drivers/usb/host/dwc_common_port/dwc_dh.c
735 create mode 100644 drivers/usb/host/dwc_common_port/dwc_dh.h
736 create mode 100644 drivers/usb/host/dwc_common_port/dwc_list.h
737 create mode 100644 drivers/usb/host/dwc_common_port/dwc_mem.c
738 create mode 100644 drivers/usb/host/dwc_common_port/dwc_modpow.c
739 create mode 100644 drivers/usb/host/dwc_common_port/dwc_modpow.h
740 create mode 100644 drivers/usb/host/dwc_common_port/dwc_notifier.c
741 create mode 100644 drivers/usb/host/dwc_common_port/dwc_notifier.h
742 create mode 100644 drivers/usb/host/dwc_common_port/dwc_os.h
743 create mode 100644 drivers/usb/host/dwc_common_port/usb.h
744 create mode 100644 drivers/usb/host/dwc_otg/Makefile
745 create mode 100644 drivers/usb/host/dwc_otg/doc/doxygen.cfg
746 create mode 100644 drivers/usb/host/dwc_otg/dummy_audio.c
747 create mode 100644 drivers/usb/host/dwc_otg/dwc_cfi_common.h
748 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_adp.c
749 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_adp.h
750 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_attr.c
751 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_attr.h
752 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_cfi.c
753 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_cfi.h
754 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_cil.c
755 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_cil.h
756 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_cil_intr.c
757 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_core_if.h
758 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_dbg.h
759 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_driver.c
760 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_driver.h
761 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.c
762 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.h
763 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_fiq_stub.S
764 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_hcd.c
765 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_hcd.h
766 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_hcd_ddma.c
767 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_hcd_if.h
768 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_hcd_intr.c
769 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_hcd_linux.c
770 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_hcd_queue.c
771 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_os_dep.h
772 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_pcd.c
773 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_pcd.h
774 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_pcd_if.h
775 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_pcd_intr.c
776 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_pcd_linux.c
777 create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_regs.h
778 create mode 100644 drivers/usb/host/dwc_otg/test/Makefile
779 create mode 100644 drivers/usb/host/dwc_otg/test/dwc_otg_test.pm
780 create mode 100644 drivers/usb/host/dwc_otg/test/test_mod_param.pl
781 create mode 100644 drivers/usb/host/dwc_otg/test/test_sysfs.pl
782
783 --- a/arch/arm/include/asm/irqflags.h
784 +++ b/arch/arm/include/asm/irqflags.h
785 @@ -163,13 +163,23 @@ static inline unsigned long arch_local_s
786 }
787
788 /*
789 - * restore saved IRQ & FIQ state
790 + * restore saved IRQ state
791 */
792 #define arch_local_irq_restore arch_local_irq_restore
793 static inline void arch_local_irq_restore(unsigned long flags)
794 {
795 - asm volatile(
796 - " msr " IRQMASK_REG_NAME_W ", %0 @ local_irq_restore"
797 + unsigned long temp = 0;
798 + flags &= ~(1 << 6);
799 + asm volatile (
800 + " mrs %0, cpsr"
801 + : "=r" (temp)
802 + :
803 + : "memory", "cc");
804 + /* Preserve FIQ bit */
805 + temp &= (1 << 6);
806 + flags = flags | temp;
807 + asm volatile (
808 + " msr cpsr_c, %0 @ local_irq_restore"
809 :
810 : "r" (flags)
811 : "memory", "cc");
812 --- a/arch/arm/kernel/fiqasm.S
813 +++ b/arch/arm/kernel/fiqasm.S
814 @@ -47,3 +47,7 @@ ENTRY(__get_fiq_regs)
815 mov r0, r0 @ avoid hazard prior to ARMv4
816 ret lr
817 ENDPROC(__get_fiq_regs)
818 +
819 +ENTRY(__FIQ_Branch)
820 + mov pc, r8
821 +ENDPROC(__FIQ_Branch)
822 --- a/drivers/usb/Makefile
823 +++ b/drivers/usb/Makefile
824 @@ -8,6 +8,7 @@
825 obj-$(CONFIG_USB) += core/
826 obj-$(CONFIG_USB_SUPPORT) += phy/
827
828 +obj-$(CONFIG_USB_DWCOTG) += host/
829 obj-$(CONFIG_USB_DWC3) += dwc3/
830 obj-$(CONFIG_USB_DWC2) += dwc2/
831 obj-$(CONFIG_USB_ISP1760) += isp1760/
832 --- a/drivers/usb/core/generic.c
833 +++ b/drivers/usb/core/generic.c
834 @@ -154,6 +154,7 @@ int usb_choose_configuration(struct usb_
835 dev_warn(&udev->dev,
836 "no configuration chosen from %d choice%s\n",
837 num_configs, plural(num_configs));
838 + dev_warn(&udev->dev, "No support over %dmA\n", udev->bus_mA);
839 }
840 return i;
841 }
842 --- a/drivers/usb/core/hub.c
843 +++ b/drivers/usb/core/hub.c
844 @@ -5116,7 +5116,7 @@ static void port_event(struct usb_hub *h
845 if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
846 u16 status = 0, unused;
847
848 - dev_dbg(&port_dev->dev, "over-current change\n");
849 + dev_notice(&port_dev->dev, "over-current change\n");
850 usb_clear_port_feature(hdev, port1,
851 USB_PORT_FEAT_C_OVER_CURRENT);
852 msleep(100); /* Cool down */
853 --- a/drivers/usb/core/message.c
854 +++ b/drivers/usb/core/message.c
855 @@ -1925,6 +1925,85 @@ free_interfaces:
856 if (cp->string == NULL &&
857 !(dev->quirks & USB_QUIRK_CONFIG_INTF_STRINGS))
858 cp->string = usb_cache_string(dev, cp->desc.iConfiguration);
859 +/* Uncomment this define to enable the HS Electrical Test support */
860 +#define DWC_HS_ELECT_TST 1
861 +#ifdef DWC_HS_ELECT_TST
862 + /* Here we implement the HS Electrical Test support. The
863 + * tester uses a vendor ID of 0x1A0A to indicate we should
864 + * run a special test sequence. The product ID tells us
865 + * which sequence to run. We invoke the test sequence by
866 + * sending a non-standard SetFeature command to our root
867 + * hub port. Our dwc_otg_hcd_hub_control() routine will
868 + * recognize the command and perform the desired test
869 + * sequence.
870 + */
871 + if (dev->descriptor.idVendor == 0x1A0A) {
872 + /* HSOTG Electrical Test */
873 + dev_warn(&dev->dev, "VID from HSOTG Electrical Test Fixture\n");
874 +
875 + if (dev->bus && dev->bus->root_hub) {
876 + struct usb_device *hdev = dev->bus->root_hub;
877 + dev_warn(&dev->dev, "Got PID 0x%x\n", dev->descriptor.idProduct);
878 +
879 + switch (dev->descriptor.idProduct) {
880 + case 0x0101: /* TEST_SE0_NAK */
881 + dev_warn(&dev->dev, "TEST_SE0_NAK\n");
882 + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
883 + USB_REQ_SET_FEATURE, USB_RT_PORT,
884 + USB_PORT_FEAT_TEST, 0x300, NULL, 0, HZ);
885 + break;
886 +
887 + case 0x0102: /* TEST_J */
888 + dev_warn(&dev->dev, "TEST_J\n");
889 + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
890 + USB_REQ_SET_FEATURE, USB_RT_PORT,
891 + USB_PORT_FEAT_TEST, 0x100, NULL, 0, HZ);
892 + break;
893 +
894 + case 0x0103: /* TEST_K */
895 + dev_warn(&dev->dev, "TEST_K\n");
896 + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
897 + USB_REQ_SET_FEATURE, USB_RT_PORT,
898 + USB_PORT_FEAT_TEST, 0x200, NULL, 0, HZ);
899 + break;
900 +
901 + case 0x0104: /* TEST_PACKET */
902 + dev_warn(&dev->dev, "TEST_PACKET\n");
903 + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
904 + USB_REQ_SET_FEATURE, USB_RT_PORT,
905 + USB_PORT_FEAT_TEST, 0x400, NULL, 0, HZ);
906 + break;
907 +
908 + case 0x0105: /* TEST_FORCE_ENABLE */
909 + dev_warn(&dev->dev, "TEST_FORCE_ENABLE\n");
910 + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
911 + USB_REQ_SET_FEATURE, USB_RT_PORT,
912 + USB_PORT_FEAT_TEST, 0x500, NULL, 0, HZ);
913 + break;
914 +
915 + case 0x0106: /* HS_HOST_PORT_SUSPEND_RESUME */
916 + dev_warn(&dev->dev, "HS_HOST_PORT_SUSPEND_RESUME\n");
917 + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
918 + USB_REQ_SET_FEATURE, USB_RT_PORT,
919 + USB_PORT_FEAT_TEST, 0x600, NULL, 0, 40 * HZ);
920 + break;
921 +
922 + case 0x0107: /* SINGLE_STEP_GET_DEVICE_DESCRIPTOR setup */
923 + dev_warn(&dev->dev, "SINGLE_STEP_GET_DEVICE_DESCRIPTOR setup\n");
924 + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
925 + USB_REQ_SET_FEATURE, USB_RT_PORT,
926 + USB_PORT_FEAT_TEST, 0x700, NULL, 0, 40 * HZ);
927 + break;
928 +
929 + case 0x0108: /* SINGLE_STEP_GET_DEVICE_DESCRIPTOR execute */
930 + dev_warn(&dev->dev, "SINGLE_STEP_GET_DEVICE_DESCRIPTOR execute\n");
931 + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
932 + USB_REQ_SET_FEATURE, USB_RT_PORT,
933 + USB_PORT_FEAT_TEST, 0x800, NULL, 0, 40 * HZ);
934 + }
935 + }
936 + }
937 +#endif /* DWC_HS_ELECT_TST */
938
939 /* Now that the interfaces are installed, re-enable LPM. */
940 usb_unlocked_enable_lpm(dev);
941 --- a/drivers/usb/core/otg_whitelist.h
942 +++ b/drivers/usb/core/otg_whitelist.h
943 @@ -19,33 +19,82 @@
944 static struct usb_device_id whitelist_table[] = {
945
946 /* hubs are optional in OTG, but very handy ... */
947 +#define CERT_WITHOUT_HUBS
948 +#if defined(CERT_WITHOUT_HUBS)
949 +{ USB_DEVICE( 0x0000, 0x0000 ), }, /* Root HUB Only*/
950 +#else
951 { USB_DEVICE_INFO(USB_CLASS_HUB, 0, 0), },
952 { USB_DEVICE_INFO(USB_CLASS_HUB, 0, 1), },
953 +{ USB_DEVICE_INFO(USB_CLASS_HUB, 0, 2), },
954 +#endif
955
956 #ifdef CONFIG_USB_PRINTER /* ignoring nonstatic linkage! */
957 /* FIXME actually, printers are NOT supposed to use device classes;
958 * they're supposed to use interface classes...
959 */
960 -{ USB_DEVICE_INFO(7, 1, 1) },
961 -{ USB_DEVICE_INFO(7, 1, 2) },
962 -{ USB_DEVICE_INFO(7, 1, 3) },
963 +//{ USB_DEVICE_INFO(7, 1, 1) },
964 +//{ USB_DEVICE_INFO(7, 1, 2) },
965 +//{ USB_DEVICE_INFO(7, 1, 3) },
966 #endif
967
968 #ifdef CONFIG_USB_NET_CDCETHER
969 /* Linux-USB CDC Ethernet gadget */
970 -{ USB_DEVICE(0x0525, 0xa4a1), },
971 +//{ USB_DEVICE(0x0525, 0xa4a1), },
972 /* Linux-USB CDC Ethernet + RNDIS gadget */
973 -{ USB_DEVICE(0x0525, 0xa4a2), },
974 +//{ USB_DEVICE(0x0525, 0xa4a2), },
975 #endif
976
977 #if IS_ENABLED(CONFIG_USB_TEST)
978 /* gadget zero, for testing */
979 -{ USB_DEVICE(0x0525, 0xa4a0), },
980 +//{ USB_DEVICE(0x0525, 0xa4a0), },
981 #endif
982
983 +/* OPT Tester */
984 +{ USB_DEVICE( 0x1a0a, 0x0101 ), }, /* TEST_SE0_NAK */
985 +{ USB_DEVICE( 0x1a0a, 0x0102 ), }, /* Test_J */
986 +{ USB_DEVICE( 0x1a0a, 0x0103 ), }, /* Test_K */
987 +{ USB_DEVICE( 0x1a0a, 0x0104 ), }, /* Test_PACKET */
988 +{ USB_DEVICE( 0x1a0a, 0x0105 ), }, /* Test_FORCE_ENABLE */
989 +{ USB_DEVICE( 0x1a0a, 0x0106 ), }, /* HS_PORT_SUSPEND_RESUME */
990 +{ USB_DEVICE( 0x1a0a, 0x0107 ), }, /* SINGLE_STEP_GET_DESCRIPTOR setup */
991 +{ USB_DEVICE( 0x1a0a, 0x0108 ), }, /* SINGLE_STEP_GET_DESCRIPTOR execute */
992 +
993 +/* Sony cameras */
994 +{ USB_DEVICE_VER(0x054c,0x0010,0x0410, 0x0500), },
995 +
996 +/* Memory Devices */
997 +//{ USB_DEVICE( 0x0781, 0x5150 ), }, /* SanDisk */
998 +//{ USB_DEVICE( 0x05DC, 0x0080 ), }, /* Lexar */
999 +//{ USB_DEVICE( 0x4146, 0x9281 ), }, /* IOMEGA */
1000 +//{ USB_DEVICE( 0x067b, 0x2507 ), }, /* Hammer 20GB External HD */
1001 +{ USB_DEVICE( 0x0EA0, 0x2168 ), }, /* Ours Technology Inc. (BUFFALO ClipDrive)*/
1002 +//{ USB_DEVICE( 0x0457, 0x0150 ), }, /* Silicon Integrated Systems Corp. */
1003 +
1004 +/* HP Printers */
1005 +//{ USB_DEVICE( 0x03F0, 0x1102 ), }, /* HP Photosmart 245 */
1006 +//{ USB_DEVICE( 0x03F0, 0x1302 ), }, /* HP Photosmart 370 Series */
1007 +
1008 +/* Speakers */
1009 +//{ USB_DEVICE( 0x0499, 0x3002 ), }, /* YAMAHA YST-MS35D USB Speakers */
1010 +//{ USB_DEVICE( 0x0672, 0x1041 ), }, /* Labtec USB Headset */
1011 +
1012 { } /* Terminating entry */
1013 };
1014
1015 +static inline void report_errors(struct usb_device *dev)
1016 +{
1017 + /* OTG MESSAGE: report errors here, customize to match your product */
1018 + dev_info(&dev->dev, "device Vendor:%04x Product:%04x is not supported\n",
1019 + le16_to_cpu(dev->descriptor.idVendor),
1020 + le16_to_cpu(dev->descriptor.idProduct));
1021 + if (USB_CLASS_HUB == dev->descriptor.bDeviceClass){
1022 + dev_printk(KERN_CRIT, &dev->dev, "Unsupported Hub Topology\n");
1023 + } else {
1024 + dev_printk(KERN_CRIT, &dev->dev, "Attached Device is not Supported\n");
1025 + }
1026 +}
1027 +
1028 +
1029 static int is_targeted(struct usb_device *dev)
1030 {
1031 struct usb_device_id *id = whitelist_table;
1032 @@ -95,16 +144,57 @@ static int is_targeted(struct usb_device
1033 continue;
1034
1035 return 1;
1036 - }
1037 + /* NOTE: can't use usb_match_id() since interface caches
1038 + * aren't set up yet. this is cut/paste from that code.
1039 + */
1040 + for (id = whitelist_table; id->match_flags; id++) {
1041 +#ifdef DEBUG
1042 + dev_dbg(&dev->dev,
1043 + "ID: V:%04x P:%04x DC:%04x SC:%04x PR:%04x \n",
1044 + id->idVendor,
1045 + id->idProduct,
1046 + id->bDeviceClass,
1047 + id->bDeviceSubClass,
1048 + id->bDeviceProtocol);
1049 +#endif
1050
1051 - /* add other match criteria here ... */
1052 + if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
1053 + id->idVendor != le16_to_cpu(dev->descriptor.idVendor))
1054 + continue;
1055 +
1056 + if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
1057 + id->idProduct != le16_to_cpu(dev->descriptor.idProduct))
1058 + continue;
1059 +
1060 + /* No need to test id->bcdDevice_lo != 0, since 0 is never
1061 + greater than any unsigned number. */
1062 + if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
1063 + (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice)))
1064 + continue;
1065 +
1066 + if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
1067 + (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice)))
1068 + continue;
1069 +
1070 + if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
1071 + (id->bDeviceClass != dev->descriptor.bDeviceClass))
1072 + continue;
1073 +
1074 + if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
1075 + (id->bDeviceSubClass != dev->descriptor.bDeviceSubClass))
1076 + continue;
1077 +
1078 + if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
1079 + (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
1080 + continue;
1081
1082 + return 1;
1083 + }
1084 + }
1085
1086 - /* OTG MESSAGE: report errors here, customize to match your product */
1087 - dev_err(&dev->dev, "device v%04x p%04x is not supported\n",
1088 - le16_to_cpu(dev->descriptor.idVendor),
1089 - le16_to_cpu(dev->descriptor.idProduct));
1090 + /* add other match criteria here ... */
1091
1092 + report_errors(dev);
1093 return 0;
1094 }
1095
1096 --- /dev/null
1097 +++ b/drivers/usb/gadget/file_storage.c
1098 @@ -0,0 +1,3676 @@
1099 +/*
1100 + * file_storage.c -- File-backed USB Storage Gadget, for USB development
1101 + *
1102 + * Copyright (C) 2003-2008 Alan Stern
1103 + * All rights reserved.
1104 + *
1105 + * Redistribution and use in source and binary forms, with or without
1106 + * modification, are permitted provided that the following conditions
1107 + * are met:
1108 + * 1. Redistributions of source code must retain the above copyright
1109 + * notice, this list of conditions, and the following disclaimer,
1110 + * without modification.
1111 + * 2. Redistributions in binary form must reproduce the above copyright
1112 + * notice, this list of conditions and the following disclaimer in the
1113 + * documentation and/or other materials provided with the distribution.
1114 + * 3. The names of the above-listed copyright holders may not be used
1115 + * to endorse or promote products derived from this software without
1116 + * specific prior written permission.
1117 + *
1118 + * ALTERNATIVELY, this software may be distributed under the terms of the
1119 + * GNU General Public License ("GPL") as published by the Free Software
1120 + * Foundation, either version 2 of that License or (at your option) any
1121 + * later version.
1122 + *
1123 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
1124 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
1125 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1126 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
1127 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
1128 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
1129 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
1130 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
1131 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
1132 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
1133 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1134 + */
1135 +
1136 +
1137 +/*
1138 + * The File-backed Storage Gadget acts as a USB Mass Storage device,
1139 + * appearing to the host as a disk drive or as a CD-ROM drive. In addition
1140 + * to providing an example of a genuinely useful gadget driver for a USB
1141 + * device, it also illustrates a technique of double-buffering for increased
1142 + * throughput. Last but not least, it gives an easy way to probe the
1143 + * behavior of the Mass Storage drivers in a USB host.
1144 + *
1145 + * Backing storage is provided by a regular file or a block device, specified
1146 + * by the "file" module parameter. Access can be limited to read-only by
1147 + * setting the optional "ro" module parameter. (For CD-ROM emulation,
1148 + * access is always read-only.) The gadget will indicate that it has
1149 + * removable media if the optional "removable" module parameter is set.
1150 + *
1151 + * The gadget supports the Control-Bulk (CB), Control-Bulk-Interrupt (CBI),
1152 + * and Bulk-Only (also known as Bulk-Bulk-Bulk or BBB) transports, selected
1153 + * by the optional "transport" module parameter. It also supports the
1154 + * following protocols: RBC (0x01), ATAPI or SFF-8020i (0x02), QIC-157 (0c03),
1155 + * UFI (0x04), SFF-8070i (0x05), and transparent SCSI (0x06), selected by
1156 + * the optional "protocol" module parameter. In addition, the default
1157 + * Vendor ID, Product ID, release number and serial number can be overridden.
1158 + *
1159 + * There is support for multiple logical units (LUNs), each of which has
1160 + * its own backing file. The number of LUNs can be set using the optional
1161 + * "luns" module parameter (anywhere from 1 to 8), and the corresponding
1162 + * files are specified using comma-separated lists for "file" and "ro".
1163 + * The default number of LUNs is taken from the number of "file" elements;
1164 + * it is 1 if "file" is not given. If "removable" is not set then a backing
1165 + * file must be specified for each LUN. If it is set, then an unspecified
1166 + * or empty backing filename means the LUN's medium is not loaded. Ideally
1167 + * each LUN would be settable independently as a disk drive or a CD-ROM
1168 + * drive, but currently all LUNs have to be the same type. The CD-ROM
1169 + * emulation includes a single data track and no audio tracks; hence there
1170 + * need be only one backing file per LUN.
1171 + *
1172 + * Requirements are modest; only a bulk-in and a bulk-out endpoint are
1173 + * needed (an interrupt-out endpoint is also needed for CBI). The memory
1174 + * requirement amounts to two 16K buffers, size configurable by a parameter.
1175 + * Support is included for both full-speed and high-speed operation.
1176 + *
1177 + * Note that the driver is slightly non-portable in that it assumes a
1178 + * single memory/DMA buffer will be useable for bulk-in, bulk-out, and
1179 + * interrupt-in endpoints. With most device controllers this isn't an
1180 + * issue, but there may be some with hardware restrictions that prevent
1181 + * a buffer from being used by more than one endpoint.
1182 + *
1183 + * Module options:
1184 + *
1185 + * file=filename[,filename...]
1186 + * Required if "removable" is not set, names of
1187 + * the files or block devices used for
1188 + * backing storage
1189 + * serial=HHHH... Required serial number (string of hex chars)
1190 + * ro=b[,b...] Default false, booleans for read-only access
1191 + * removable Default false, boolean for removable media
1192 + * luns=N Default N = number of filenames, number of
1193 + * LUNs to support
1194 + * nofua=b[,b...] Default false, booleans for ignore FUA flag
1195 + * in SCSI WRITE(10,12) commands
1196 + * stall Default determined according to the type of
1197 + * USB device controller (usually true),
1198 + * boolean to permit the driver to halt
1199 + * bulk endpoints
1200 + * cdrom Default false, boolean for whether to emulate
1201 + * a CD-ROM drive
1202 + * transport=XXX Default BBB, transport name (CB, CBI, or BBB)
1203 + * protocol=YYY Default SCSI, protocol name (RBC, 8020 or
1204 + * ATAPI, QIC, UFI, 8070, or SCSI;
1205 + * also 1 - 6)
1206 + * vendor=0xVVVV Default 0x0525 (NetChip), USB Vendor ID
1207 + * product=0xPPPP Default 0xa4a5 (FSG), USB Product ID
1208 + * release=0xRRRR Override the USB release number (bcdDevice)
1209 + * buflen=N Default N=16384, buffer size used (will be
1210 + * rounded down to a multiple of
1211 + * PAGE_CACHE_SIZE)
1212 + *
1213 + * If CONFIG_USB_FILE_STORAGE_TEST is not set, only the "file", "serial", "ro",
1214 + * "removable", "luns", "nofua", "stall", and "cdrom" options are available;
1215 + * default values are used for everything else.
1216 + *
1217 + * The pathnames of the backing files and the ro settings are available in
1218 + * the attribute files "file", "nofua", and "ro" in the lun<n> subdirectory of
1219 + * the gadget's sysfs directory. If the "removable" option is set, writing to
1220 + * these files will simulate ejecting/loading the medium (writing an empty
1221 + * line means eject) and adjusting a write-enable tab. Changes to the ro
1222 + * setting are not allowed when the medium is loaded or if CD-ROM emulation
1223 + * is being used.
1224 + *
1225 + * This gadget driver is heavily based on "Gadget Zero" by David Brownell.
1226 + * The driver's SCSI command interface was based on the "Information
1227 + * technology - Small Computer System Interface - 2" document from
1228 + * X3T9.2 Project 375D, Revision 10L, 7-SEP-93, available at
1229 + * <http://www.t10.org/ftp/t10/drafts/s2/s2-r10l.pdf>. The single exception
1230 + * is opcode 0x23 (READ FORMAT CAPACITIES), which was based on the
1231 + * "Universal Serial Bus Mass Storage Class UFI Command Specification"
1232 + * document, Revision 1.0, December 14, 1998, available at
1233 + * <http://www.usb.org/developers/devclass_docs/usbmass-ufi10.pdf>.
1234 + */
1235 +
1236 +
1237 +/*
1238 + * Driver Design
1239 + *
1240 + * The FSG driver is fairly straightforward. There is a main kernel
1241 + * thread that handles most of the work. Interrupt routines field
1242 + * callbacks from the controller driver: bulk- and interrupt-request
1243 + * completion notifications, endpoint-0 events, and disconnect events.
1244 + * Completion events are passed to the main thread by wakeup calls. Many
1245 + * ep0 requests are handled at interrupt time, but SetInterface,
1246 + * SetConfiguration, and device reset requests are forwarded to the
1247 + * thread in the form of "exceptions" using SIGUSR1 signals (since they
1248 + * should interrupt any ongoing file I/O operations).
1249 + *
1250 + * The thread's main routine implements the standard command/data/status
1251 + * parts of a SCSI interaction. It and its subroutines are full of tests
1252 + * for pending signals/exceptions -- all this polling is necessary since
1253 + * the kernel has no setjmp/longjmp equivalents. (Maybe this is an
1254 + * indication that the driver really wants to be running in userspace.)
1255 + * An important point is that so long as the thread is alive it keeps an
1256 + * open reference to the backing file. This will prevent unmounting
1257 + * the backing file's underlying filesystem and could cause problems
1258 + * during system shutdown, for example. To prevent such problems, the
1259 + * thread catches INT, TERM, and KILL signals and converts them into
1260 + * an EXIT exception.
1261 + *
1262 + * In normal operation the main thread is started during the gadget's
1263 + * fsg_bind() callback and stopped during fsg_unbind(). But it can also
1264 + * exit when it receives a signal, and there's no point leaving the
1265 + * gadget running when the thread is dead. So just before the thread
1266 + * exits, it deregisters the gadget driver. This makes things a little
1267 + * tricky: The driver is deregistered at two places, and the exiting
1268 + * thread can indirectly call fsg_unbind() which in turn can tell the
1269 + * thread to exit. The first problem is resolved through the use of the
1270 + * REGISTERED atomic bitflag; the driver will only be deregistered once.
1271 + * The second problem is resolved by having fsg_unbind() check
1272 + * fsg->state; it won't try to stop the thread if the state is already
1273 + * FSG_STATE_TERMINATED.
1274 + *
1275 + * To provide maximum throughput, the driver uses a circular pipeline of
1276 + * buffer heads (struct fsg_buffhd). In principle the pipeline can be
1277 + * arbitrarily long; in practice the benefits don't justify having more
1278 + * than 2 stages (i.e., double buffering). But it helps to think of the
1279 + * pipeline as being a long one. Each buffer head contains a bulk-in and
1280 + * a bulk-out request pointer (since the buffer can be used for both
1281 + * output and input -- directions always are given from the host's
1282 + * point of view) as well as a pointer to the buffer and various state
1283 + * variables.
1284 + *
1285 + * Use of the pipeline follows a simple protocol. There is a variable
1286 + * (fsg->next_buffhd_to_fill) that points to the next buffer head to use.
1287 + * At any time that buffer head may still be in use from an earlier
1288 + * request, so each buffer head has a state variable indicating whether
1289 + * it is EMPTY, FULL, or BUSY. Typical use involves waiting for the
1290 + * buffer head to be EMPTY, filling the buffer either by file I/O or by
1291 + * USB I/O (during which the buffer head is BUSY), and marking the buffer
1292 + * head FULL when the I/O is complete. Then the buffer will be emptied
1293 + * (again possibly by USB I/O, during which it is marked BUSY) and
1294 + * finally marked EMPTY again (possibly by a completion routine).
1295 + *
1296 + * A module parameter tells the driver to avoid stalling the bulk
1297 + * endpoints wherever the transport specification allows. This is
1298 + * necessary for some UDCs like the SuperH, which cannot reliably clear a
1299 + * halt on a bulk endpoint. However, under certain circumstances the
1300 + * Bulk-only specification requires a stall. In such cases the driver
1301 + * will halt the endpoint and set a flag indicating that it should clear
1302 + * the halt in software during the next device reset. Hopefully this
1303 + * will permit everything to work correctly. Furthermore, although the
1304 + * specification allows the bulk-out endpoint to halt when the host sends
1305 + * too much data, implementing this would cause an unavoidable race.
1306 + * The driver will always use the "no-stall" approach for OUT transfers.
1307 + *
1308 + * One subtle point concerns sending status-stage responses for ep0
1309 + * requests. Some of these requests, such as device reset, can involve
1310 + * interrupting an ongoing file I/O operation, which might take an
1311 + * arbitrarily long time. During that delay the host might give up on
1312 + * the original ep0 request and issue a new one. When that happens the
1313 + * driver should not notify the host about completion of the original
1314 + * request, as the host will no longer be waiting for it. So the driver
1315 + * assigns to each ep0 request a unique tag, and it keeps track of the
1316 + * tag value of the request associated with a long-running exception
1317 + * (device-reset, interface-change, or configuration-change). When the
1318 + * exception handler is finished, the status-stage response is submitted
1319 + * only if the current ep0 request tag is equal to the exception request
1320 + * tag. Thus only the most recently received ep0 request will get a
1321 + * status-stage response.
1322 + *
1323 + * Warning: This driver source file is too long. It ought to be split up
1324 + * into a header file plus about 3 separate .c files, to handle the details
1325 + * of the Gadget, USB Mass Storage, and SCSI protocols.
1326 + */
1327 +
1328 +
1329 +/* #define VERBOSE_DEBUG */
1330 +/* #define DUMP_MSGS */
1331 +
1332 +
1333 +#include <linux/blkdev.h>
1334 +#include <linux/completion.h>
1335 +#include <linux/dcache.h>
1336 +#include <linux/delay.h>
1337 +#include <linux/device.h>
1338 +#include <linux/fcntl.h>
1339 +#include <linux/file.h>
1340 +#include <linux/fs.h>
1341 +#include <linux/kref.h>
1342 +#include <linux/kthread.h>
1343 +#include <linux/limits.h>
1344 +#include <linux/module.h>
1345 +#include <linux/rwsem.h>
1346 +#include <linux/slab.h>
1347 +#include <linux/spinlock.h>
1348 +#include <linux/string.h>
1349 +#include <linux/freezer.h>
1350 +#include <linux/utsname.h>
1351 +
1352 +#include <linux/usb/ch9.h>
1353 +#include <linux/usb/gadget.h>
1354 +
1355 +#include "gadget_chips.h"
1356 +
1357 +
1358 +
1359 +/*
1360 + * Kbuild is not very cooperative with respect to linking separately
1361 + * compiled library objects into one module. So for now we won't use
1362 + * separate compilation ... ensuring init/exit sections work to shrink
1363 + * the runtime footprint, and giving us at least some parts of what
1364 + * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
1365 + */
1366 +#include "usbstring.c"
1367 +#include "config.c"
1368 +#include "epautoconf.c"
1369 +
1370 +/*-------------------------------------------------------------------------*/
1371 +
1372 +#define DRIVER_DESC "File-backed Storage Gadget"
1373 +#define DRIVER_NAME "g_file_storage"
1374 +#define DRIVER_VERSION "1 September 2010"
1375 +
1376 +static char fsg_string_manufacturer[64];
1377 +static const char fsg_string_product[] = DRIVER_DESC;
1378 +static const char fsg_string_config[] = "Self-powered";
1379 +static const char fsg_string_interface[] = "Mass Storage";
1380 +
1381 +
1382 +#include "storage_common.c"
1383 +
1384 +
1385 +MODULE_DESCRIPTION(DRIVER_DESC);
1386 +MODULE_AUTHOR("Alan Stern");
1387 +MODULE_LICENSE("Dual BSD/GPL");
1388 +
1389 +/*
1390 + * This driver assumes self-powered hardware and has no way for users to
1391 + * trigger remote wakeup. It uses autoconfiguration to select endpoints
1392 + * and endpoint addresses.
1393 + */
1394 +
1395 +
1396 +/*-------------------------------------------------------------------------*/
1397 +
1398 +
1399 +/* Encapsulate the module parameter settings */
1400 +
1401 +static struct {
1402 + char *file[FSG_MAX_LUNS];
1403 + char *serial;
1404 + bool ro[FSG_MAX_LUNS];
1405 + bool nofua[FSG_MAX_LUNS];
1406 + unsigned int num_filenames;
1407 + unsigned int num_ros;
1408 + unsigned int num_nofuas;
1409 + unsigned int nluns;
1410 +
1411 + bool removable;
1412 + bool can_stall;
1413 + bool cdrom;
1414 +
1415 + char *transport_parm;
1416 + char *protocol_parm;
1417 + unsigned short vendor;
1418 + unsigned short product;
1419 + unsigned short release;
1420 + unsigned int buflen;
1421 +
1422 + int transport_type;
1423 + char *transport_name;
1424 + int protocol_type;
1425 + char *protocol_name;
1426 +
1427 +} mod_data = { // Default values
1428 + .transport_parm = "BBB",
1429 + .protocol_parm = "SCSI",
1430 + .removable = 0,
1431 + .can_stall = 1,
1432 + .cdrom = 0,
1433 + .vendor = FSG_VENDOR_ID,
1434 + .product = FSG_PRODUCT_ID,
1435 + .release = 0xffff, // Use controller chip type
1436 + .buflen = 16384,
1437 + };
1438 +
1439 +
1440 +module_param_array_named(file, mod_data.file, charp, &mod_data.num_filenames,
1441 + S_IRUGO);
1442 +MODULE_PARM_DESC(file, "names of backing files or devices");
1443 +
1444 +module_param_named(serial, mod_data.serial, charp, S_IRUGO);
1445 +MODULE_PARM_DESC(serial, "USB serial number");
1446 +
1447 +module_param_array_named(ro, mod_data.ro, bool, &mod_data.num_ros, S_IRUGO);
1448 +MODULE_PARM_DESC(ro, "true to force read-only");
1449 +
1450 +module_param_array_named(nofua, mod_data.nofua, bool, &mod_data.num_nofuas,
1451 + S_IRUGO);
1452 +MODULE_PARM_DESC(nofua, "true to ignore SCSI WRITE(10,12) FUA bit");
1453 +
1454 +module_param_named(luns, mod_data.nluns, uint, S_IRUGO);
1455 +MODULE_PARM_DESC(luns, "number of LUNs");
1456 +
1457 +module_param_named(removable, mod_data.removable, bool, S_IRUGO);
1458 +MODULE_PARM_DESC(removable, "true to simulate removable media");
1459 +
1460 +module_param_named(stall, mod_data.can_stall, bool, S_IRUGO);
1461 +MODULE_PARM_DESC(stall, "false to prevent bulk stalls");
1462 +
1463 +module_param_named(cdrom, mod_data.cdrom, bool, S_IRUGO);
1464 +MODULE_PARM_DESC(cdrom, "true to emulate cdrom instead of disk");
1465 +
1466 +/* In the non-TEST version, only the module parameters listed above
1467 + * are available. */
1468 +#ifdef CONFIG_USB_FILE_STORAGE_TEST
1469 +
1470 +module_param_named(transport, mod_data.transport_parm, charp, S_IRUGO);
1471 +MODULE_PARM_DESC(transport, "type of transport (BBB, CBI, or CB)");
1472 +
1473 +module_param_named(protocol, mod_data.protocol_parm, charp, S_IRUGO);
1474 +MODULE_PARM_DESC(protocol, "type of protocol (RBC, 8020, QIC, UFI, "
1475 + "8070, or SCSI)");
1476 +
1477 +module_param_named(vendor, mod_data.vendor, ushort, S_IRUGO);
1478 +MODULE_PARM_DESC(vendor, "USB Vendor ID");
1479 +
1480 +module_param_named(product, mod_data.product, ushort, S_IRUGO);
1481 +MODULE_PARM_DESC(product, "USB Product ID");
1482 +
1483 +module_param_named(release, mod_data.release, ushort, S_IRUGO);
1484 +MODULE_PARM_DESC(release, "USB release number");
1485 +
1486 +module_param_named(buflen, mod_data.buflen, uint, S_IRUGO);
1487 +MODULE_PARM_DESC(buflen, "I/O buffer size");
1488 +
1489 +#endif /* CONFIG_USB_FILE_STORAGE_TEST */
1490 +
1491 +
1492 +/*
1493 + * These definitions will permit the compiler to avoid generating code for
1494 + * parts of the driver that aren't used in the non-TEST version. Even gcc
1495 + * can recognize when a test of a constant expression yields a dead code
1496 + * path.
1497 + */
1498 +
1499 +#ifdef CONFIG_USB_FILE_STORAGE_TEST
1500 +
1501 +#define transport_is_bbb() (mod_data.transport_type == USB_PR_BULK)
1502 +#define transport_is_cbi() (mod_data.transport_type == USB_PR_CBI)
1503 +#define protocol_is_scsi() (mod_data.protocol_type == USB_SC_SCSI)
1504 +
1505 +#else
1506 +
1507 +#define transport_is_bbb() 1
1508 +#define transport_is_cbi() 0
1509 +#define protocol_is_scsi() 1
1510 +
1511 +#endif /* CONFIG_USB_FILE_STORAGE_TEST */
1512 +
1513 +
1514 +/*-------------------------------------------------------------------------*/
1515 +
1516 +
1517 +struct fsg_dev {
1518 + /* lock protects: state, all the req_busy's, and cbbuf_cmnd */
1519 + spinlock_t lock;
1520 + struct usb_gadget *gadget;
1521 +
1522 + /* filesem protects: backing files in use */
1523 + struct rw_semaphore filesem;
1524 +
1525 + /* reference counting: wait until all LUNs are released */
1526 + struct kref ref;
1527 +
1528 + struct usb_ep *ep0; // Handy copy of gadget->ep0
1529 + struct usb_request *ep0req; // For control responses
1530 + unsigned int ep0_req_tag;
1531 + const char *ep0req_name;
1532 +
1533 + struct usb_request *intreq; // For interrupt responses
1534 + int intreq_busy;
1535 + struct fsg_buffhd *intr_buffhd;
1536 +
1537 + unsigned int bulk_out_maxpacket;
1538 + enum fsg_state state; // For exception handling
1539 + unsigned int exception_req_tag;
1540 +
1541 + u8 config, new_config;
1542 +
1543 + unsigned int running : 1;
1544 + unsigned int bulk_in_enabled : 1;
1545 + unsigned int bulk_out_enabled : 1;
1546 + unsigned int intr_in_enabled : 1;
1547 + unsigned int phase_error : 1;
1548 + unsigned int short_packet_received : 1;
1549 + unsigned int bad_lun_okay : 1;
1550 +
1551 + unsigned long atomic_bitflags;
1552 +#define REGISTERED 0
1553 +#define IGNORE_BULK_OUT 1
1554 +#define SUSPENDED 2
1555 +
1556 + struct usb_ep *bulk_in;
1557 + struct usb_ep *bulk_out;
1558 + struct usb_ep *intr_in;
1559 +
1560 + struct fsg_buffhd *next_buffhd_to_fill;
1561 + struct fsg_buffhd *next_buffhd_to_drain;
1562 +
1563 + int thread_wakeup_needed;
1564 + struct completion thread_notifier;
1565 + struct task_struct *thread_task;
1566 +
1567 + int cmnd_size;
1568 + u8 cmnd[MAX_COMMAND_SIZE];
1569 + enum data_direction data_dir;
1570 + u32 data_size;
1571 + u32 data_size_from_cmnd;
1572 + u32 tag;
1573 + unsigned int lun;
1574 + u32 residue;
1575 + u32 usb_amount_left;
1576 +
1577 + /* The CB protocol offers no way for a host to know when a command
1578 + * has completed. As a result the next command may arrive early,
1579 + * and we will still have to handle it. For that reason we need
1580 + * a buffer to store new commands when using CB (or CBI, which
1581 + * does not oblige a host to wait for command completion either). */
1582 + int cbbuf_cmnd_size;
1583 + u8 cbbuf_cmnd[MAX_COMMAND_SIZE];
1584 +
1585 + unsigned int nluns;
1586 + struct fsg_lun *luns;
1587 + struct fsg_lun *curlun;
1588 + /* Must be the last entry */
1589 + struct fsg_buffhd buffhds[];
1590 +};
1591 +
1592 +typedef void (*fsg_routine_t)(struct fsg_dev *);
1593 +
1594 +static int exception_in_progress(struct fsg_dev *fsg)
1595 +{
1596 + return (fsg->state > FSG_STATE_IDLE);
1597 +}
1598 +
1599 +/* Make bulk-out requests be divisible by the maxpacket size */
1600 +static void set_bulk_out_req_length(struct fsg_dev *fsg,
1601 + struct fsg_buffhd *bh, unsigned int length)
1602 +{
1603 + unsigned int rem;
1604 +
1605 + bh->bulk_out_intended_length = length;
1606 + rem = length % fsg->bulk_out_maxpacket;
1607 + if (rem > 0)
1608 + length += fsg->bulk_out_maxpacket - rem;
1609 + bh->outreq->length = length;
1610 +}
1611 +
1612 +static struct fsg_dev *the_fsg;
1613 +static struct usb_gadget_driver fsg_driver;
1614 +
1615 +
1616 +/*-------------------------------------------------------------------------*/
1617 +
1618 +static int fsg_set_halt(struct fsg_dev *fsg, struct usb_ep *ep)
1619 +{
1620 + const char *name;
1621 +
1622 + if (ep == fsg->bulk_in)
1623 + name = "bulk-in";
1624 + else if (ep == fsg->bulk_out)
1625 + name = "bulk-out";
1626 + else
1627 + name = ep->name;
1628 + DBG(fsg, "%s set halt\n", name);
1629 + return usb_ep_set_halt(ep);
1630 +}
1631 +
1632 +
1633 +/*-------------------------------------------------------------------------*/
1634 +
1635 +/*
1636 + * DESCRIPTORS ... most are static, but strings and (full) configuration
1637 + * descriptors are built on demand. Also the (static) config and interface
1638 + * descriptors are adjusted during fsg_bind().
1639 + */
1640 +
1641 +/* There is only one configuration. */
1642 +#define CONFIG_VALUE 1
1643 +
1644 +static struct usb_device_descriptor
1645 +device_desc = {
1646 + .bLength = sizeof device_desc,
1647 + .bDescriptorType = USB_DT_DEVICE,
1648 +
1649 + .bcdUSB = cpu_to_le16(0x0200),
1650 + .bDeviceClass = USB_CLASS_PER_INTERFACE,
1651 +
1652 + /* The next three values can be overridden by module parameters */
1653 + .idVendor = cpu_to_le16(FSG_VENDOR_ID),
1654 + .idProduct = cpu_to_le16(FSG_PRODUCT_ID),
1655 + .bcdDevice = cpu_to_le16(0xffff),
1656 +
1657 + .iManufacturer = FSG_STRING_MANUFACTURER,
1658 + .iProduct = FSG_STRING_PRODUCT,
1659 + .iSerialNumber = FSG_STRING_SERIAL,
1660 + .bNumConfigurations = 1,
1661 +};
1662 +
1663 +static struct usb_config_descriptor
1664 +config_desc = {
1665 + .bLength = sizeof config_desc,
1666 + .bDescriptorType = USB_DT_CONFIG,
1667 +
1668 + /* wTotalLength computed by usb_gadget_config_buf() */
1669 + .bNumInterfaces = 1,
1670 + .bConfigurationValue = CONFIG_VALUE,
1671 + .iConfiguration = FSG_STRING_CONFIG,
1672 + .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
1673 + .bMaxPower = CONFIG_USB_GADGET_VBUS_DRAW / 2,
1674 +};
1675 +
1676 +
1677 +static struct usb_qualifier_descriptor
1678 +dev_qualifier = {
1679 + .bLength = sizeof dev_qualifier,
1680 + .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
1681 +
1682 + .bcdUSB = cpu_to_le16(0x0200),
1683 + .bDeviceClass = USB_CLASS_PER_INTERFACE,
1684 +
1685 + .bNumConfigurations = 1,
1686 +};
1687 +
1688 +static int populate_bos(struct fsg_dev *fsg, u8 *buf)
1689 +{
1690 + memcpy(buf, &fsg_bos_desc, USB_DT_BOS_SIZE);
1691 + buf += USB_DT_BOS_SIZE;
1692 +
1693 + memcpy(buf, &fsg_ext_cap_desc, USB_DT_USB_EXT_CAP_SIZE);
1694 + buf += USB_DT_USB_EXT_CAP_SIZE;
1695 +
1696 + memcpy(buf, &fsg_ss_cap_desc, USB_DT_USB_SS_CAP_SIZE);
1697 +
1698 + return USB_DT_BOS_SIZE + USB_DT_USB_SS_CAP_SIZE
1699 + + USB_DT_USB_EXT_CAP_SIZE;
1700 +}
1701 +
1702 +/*
1703 + * Config descriptors must agree with the code that sets configurations
1704 + * and with code managing interfaces and their altsettings. They must
1705 + * also handle different speeds and other-speed requests.
1706 + */
1707 +static int populate_config_buf(struct usb_gadget *gadget,
1708 + u8 *buf, u8 type, unsigned index)
1709 +{
1710 + enum usb_device_speed speed = gadget->speed;
1711 + int len;
1712 + const struct usb_descriptor_header **function;
1713 +
1714 + if (index > 0)
1715 + return -EINVAL;
1716 +
1717 + if (gadget_is_dualspeed(gadget) && type == USB_DT_OTHER_SPEED_CONFIG)
1718 + speed = (USB_SPEED_FULL + USB_SPEED_HIGH) - speed;
1719 + function = gadget_is_dualspeed(gadget) && speed == USB_SPEED_HIGH
1720 + ? (const struct usb_descriptor_header **)fsg_hs_function
1721 + : (const struct usb_descriptor_header **)fsg_fs_function;
1722 +
1723 + /* for now, don't advertise srp-only devices */
1724 + if (!gadget_is_otg(gadget))
1725 + function++;
1726 +
1727 + len = usb_gadget_config_buf(&config_desc, buf, EP0_BUFSIZE, function);
1728 + ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
1729 + return len;
1730 +}
1731 +
1732 +
1733 +/*-------------------------------------------------------------------------*/
1734 +
1735 +/* These routines may be called in process context or in_irq */
1736 +
1737 +/* Caller must hold fsg->lock */
1738 +static void wakeup_thread(struct fsg_dev *fsg)
1739 +{
1740 + /* Tell the main thread that something has happened */
1741 + fsg->thread_wakeup_needed = 1;
1742 + if (fsg->thread_task)
1743 + wake_up_process(fsg->thread_task);
1744 +}
1745 +
1746 +
1747 +static void raise_exception(struct fsg_dev *fsg, enum fsg_state new_state)
1748 +{
1749 + unsigned long flags;
1750 +
1751 + /* Do nothing if a higher-priority exception is already in progress.
1752 + * If a lower-or-equal priority exception is in progress, preempt it
1753 + * and notify the main thread by sending it a signal. */
1754 + spin_lock_irqsave(&fsg->lock, flags);
1755 + if (fsg->state <= new_state) {
1756 + fsg->exception_req_tag = fsg->ep0_req_tag;
1757 + fsg->state = new_state;
1758 + if (fsg->thread_task)
1759 + send_sig_info(SIGUSR1, SEND_SIG_FORCED,
1760 + fsg->thread_task);
1761 + }
1762 + spin_unlock_irqrestore(&fsg->lock, flags);
1763 +}
1764 +
1765 +
1766 +/*-------------------------------------------------------------------------*/
1767 +
1768 +/* The disconnect callback and ep0 routines. These always run in_irq,
1769 + * except that ep0_queue() is called in the main thread to acknowledge
1770 + * completion of various requests: set config, set interface, and
1771 + * Bulk-only device reset. */
1772 +
1773 +static void fsg_disconnect(struct usb_gadget *gadget)
1774 +{
1775 + struct fsg_dev *fsg = get_gadget_data(gadget);
1776 +
1777 + DBG(fsg, "disconnect or port reset\n");
1778 + raise_exception(fsg, FSG_STATE_DISCONNECT);
1779 +}
1780 +
1781 +
1782 +static int ep0_queue(struct fsg_dev *fsg)
1783 +{
1784 + int rc;
1785 +
1786 + rc = usb_ep_queue(fsg->ep0, fsg->ep0req, GFP_ATOMIC);
1787 + if (rc != 0 && rc != -ESHUTDOWN) {
1788 +
1789 + /* We can't do much more than wait for a reset */
1790 + WARNING(fsg, "error in submission: %s --> %d\n",
1791 + fsg->ep0->name, rc);
1792 + }
1793 + return rc;
1794 +}
1795 +
1796 +static void ep0_complete(struct usb_ep *ep, struct usb_request *req)
1797 +{
1798 + struct fsg_dev *fsg = ep->driver_data;
1799 +
1800 + if (req->actual > 0)
1801 + dump_msg(fsg, fsg->ep0req_name, req->buf, req->actual);
1802 + if (req->status || req->actual != req->length)
1803 + DBG(fsg, "%s --> %d, %u/%u\n", __func__,
1804 + req->status, req->actual, req->length);
1805 + if (req->status == -ECONNRESET) // Request was cancelled
1806 + usb_ep_fifo_flush(ep);
1807 +
1808 + if (req->status == 0 && req->context)
1809 + ((fsg_routine_t) (req->context))(fsg);
1810 +}
1811 +
1812 +
1813 +/*-------------------------------------------------------------------------*/
1814 +
1815 +/* Bulk and interrupt endpoint completion handlers.
1816 + * These always run in_irq. */
1817 +
1818 +static void bulk_in_complete(struct usb_ep *ep, struct usb_request *req)
1819 +{
1820 + struct fsg_dev *fsg = ep->driver_data;
1821 + struct fsg_buffhd *bh = req->context;
1822 +
1823 + if (req->status || req->actual != req->length)
1824 + DBG(fsg, "%s --> %d, %u/%u\n", __func__,
1825 + req->status, req->actual, req->length);
1826 + if (req->status == -ECONNRESET) // Request was cancelled
1827 + usb_ep_fifo_flush(ep);
1828 +
1829 + /* Hold the lock while we update the request and buffer states */
1830 + smp_wmb();
1831 + spin_lock(&fsg->lock);
1832 + bh->inreq_busy = 0;
1833 + bh->state = BUF_STATE_EMPTY;
1834 + wakeup_thread(fsg);
1835 + spin_unlock(&fsg->lock);
1836 +}
1837 +
1838 +static void bulk_out_complete(struct usb_ep *ep, struct usb_request *req)
1839 +{
1840 + struct fsg_dev *fsg = ep->driver_data;
1841 + struct fsg_buffhd *bh = req->context;
1842 +
1843 + dump_msg(fsg, "bulk-out", req->buf, req->actual);
1844 + if (req->status || req->actual != bh->bulk_out_intended_length)
1845 + DBG(fsg, "%s --> %d, %u/%u\n", __func__,
1846 + req->status, req->actual,
1847 + bh->bulk_out_intended_length);
1848 + if (req->status == -ECONNRESET) // Request was cancelled
1849 + usb_ep_fifo_flush(ep);
1850 +
1851 + /* Hold the lock while we update the request and buffer states */
1852 + smp_wmb();
1853 + spin_lock(&fsg->lock);
1854 + bh->outreq_busy = 0;
1855 + bh->state = BUF_STATE_FULL;
1856 + wakeup_thread(fsg);
1857 + spin_unlock(&fsg->lock);
1858 +}
1859 +
1860 +
1861 +#ifdef CONFIG_USB_FILE_STORAGE_TEST
1862 +static void intr_in_complete(struct usb_ep *ep, struct usb_request *req)
1863 +{
1864 + struct fsg_dev *fsg = ep->driver_data;
1865 + struct fsg_buffhd *bh = req->context;
1866 +
1867 + if (req->status || req->actual != req->length)
1868 + DBG(fsg, "%s --> %d, %u/%u\n", __func__,
1869 + req->status, req->actual, req->length);
1870 + if (req->status == -ECONNRESET) // Request was cancelled
1871 + usb_ep_fifo_flush(ep);
1872 +
1873 + /* Hold the lock while we update the request and buffer states */
1874 + smp_wmb();
1875 + spin_lock(&fsg->lock);
1876 + fsg->intreq_busy = 0;
1877 + bh->state = BUF_STATE_EMPTY;
1878 + wakeup_thread(fsg);
1879 + spin_unlock(&fsg->lock);
1880 +}
1881 +
1882 +#else
1883 +static void intr_in_complete(struct usb_ep *ep, struct usb_request *req)
1884 +{}
1885 +#endif /* CONFIG_USB_FILE_STORAGE_TEST */
1886 +
1887 +
1888 +/*-------------------------------------------------------------------------*/
1889 +
1890 +/* Ep0 class-specific handlers. These always run in_irq. */
1891 +
1892 +#ifdef CONFIG_USB_FILE_STORAGE_TEST
1893 +static void received_cbi_adsc(struct fsg_dev *fsg, struct fsg_buffhd *bh)
1894 +{
1895 + struct usb_request *req = fsg->ep0req;
1896 + static u8 cbi_reset_cmnd[6] = {
1897 + SEND_DIAGNOSTIC, 4, 0xff, 0xff, 0xff, 0xff};
1898 +
1899 + /* Error in command transfer? */
1900 + if (req->status || req->length != req->actual ||
1901 + req->actual < 6 || req->actual > MAX_COMMAND_SIZE) {
1902 +
1903 + /* Not all controllers allow a protocol stall after
1904 + * receiving control-out data, but we'll try anyway. */
1905 + fsg_set_halt(fsg, fsg->ep0);
1906 + return; // Wait for reset
1907 + }
1908 +
1909 + /* Is it the special reset command? */
1910 + if (req->actual >= sizeof cbi_reset_cmnd &&
1911 + memcmp(req->buf, cbi_reset_cmnd,
1912 + sizeof cbi_reset_cmnd) == 0) {
1913 +
1914 + /* Raise an exception to stop the current operation
1915 + * and reinitialize our state. */
1916 + DBG(fsg, "cbi reset request\n");
1917 + raise_exception(fsg, FSG_STATE_RESET);
1918 + return;
1919 + }
1920 +
1921 + VDBG(fsg, "CB[I] accept device-specific command\n");
1922 + spin_lock(&fsg->lock);
1923 +
1924 + /* Save the command for later */
1925 + if (fsg->cbbuf_cmnd_size)
1926 + WARNING(fsg, "CB[I] overwriting previous command\n");
1927 + fsg->cbbuf_cmnd_size = req->actual;
1928 + memcpy(fsg->cbbuf_cmnd, req->buf, fsg->cbbuf_cmnd_size);
1929 +
1930 + wakeup_thread(fsg);
1931 + spin_unlock(&fsg->lock);
1932 +}
1933 +
1934 +#else
1935 +static void received_cbi_adsc(struct fsg_dev *fsg, struct fsg_buffhd *bh)
1936 +{}
1937 +#endif /* CONFIG_USB_FILE_STORAGE_TEST */
1938 +
1939 +
1940 +static int class_setup_req(struct fsg_dev *fsg,
1941 + const struct usb_ctrlrequest *ctrl)
1942 +{
1943 + struct usb_request *req = fsg->ep0req;
1944 + int value = -EOPNOTSUPP;
1945 + u16 w_index = le16_to_cpu(ctrl->wIndex);
1946 + u16 w_value = le16_to_cpu(ctrl->wValue);
1947 + u16 w_length = le16_to_cpu(ctrl->wLength);
1948 +
1949 + if (!fsg->config)
1950 + return value;
1951 +
1952 + /* Handle Bulk-only class-specific requests */
1953 + if (transport_is_bbb()) {
1954 + switch (ctrl->bRequest) {
1955 +
1956 + case US_BULK_RESET_REQUEST:
1957 + if (ctrl->bRequestType != (USB_DIR_OUT |
1958 + USB_TYPE_CLASS | USB_RECIP_INTERFACE))
1959 + break;
1960 + if (w_index != 0 || w_value != 0 || w_length != 0) {
1961 + value = -EDOM;
1962 + break;
1963 + }
1964 +
1965 + /* Raise an exception to stop the current operation
1966 + * and reinitialize our state. */
1967 + DBG(fsg, "bulk reset request\n");
1968 + raise_exception(fsg, FSG_STATE_RESET);
1969 + value = DELAYED_STATUS;
1970 + break;
1971 +
1972 + case US_BULK_GET_MAX_LUN:
1973 + if (ctrl->bRequestType != (USB_DIR_IN |
1974 + USB_TYPE_CLASS | USB_RECIP_INTERFACE))
1975 + break;
1976 + if (w_index != 0 || w_value != 0 || w_length != 1) {
1977 + value = -EDOM;
1978 + break;
1979 + }
1980 + VDBG(fsg, "get max LUN\n");
1981 + *(u8 *) req->buf = fsg->nluns - 1;
1982 + value = 1;
1983 + break;
1984 + }
1985 + }
1986 +
1987 + /* Handle CBI class-specific requests */
1988 + else {
1989 + switch (ctrl->bRequest) {
1990 +
1991 + case USB_CBI_ADSC_REQUEST:
1992 + if (ctrl->bRequestType != (USB_DIR_OUT |
1993 + USB_TYPE_CLASS | USB_RECIP_INTERFACE))
1994 + break;
1995 + if (w_index != 0 || w_value != 0) {
1996 + value = -EDOM;
1997 + break;
1998 + }
1999 + if (w_length > MAX_COMMAND_SIZE) {
2000 + value = -EOVERFLOW;
2001 + break;
2002 + }
2003 + value = w_length;
2004 + fsg->ep0req->context = received_cbi_adsc;
2005 + break;
2006 + }
2007 + }
2008 +
2009 + if (value == -EOPNOTSUPP)
2010 + VDBG(fsg,
2011 + "unknown class-specific control req "
2012 + "%02x.%02x v%04x i%04x l%u\n",
2013 + ctrl->bRequestType, ctrl->bRequest,
2014 + le16_to_cpu(ctrl->wValue), w_index, w_length);
2015 + return value;
2016 +}
2017 +
2018 +
2019 +/*-------------------------------------------------------------------------*/
2020 +
2021 +/* Ep0 standard request handlers. These always run in_irq. */
2022 +
2023 +static int standard_setup_req(struct fsg_dev *fsg,
2024 + const struct usb_ctrlrequest *ctrl)
2025 +{
2026 + struct usb_request *req = fsg->ep0req;
2027 + int value = -EOPNOTSUPP;
2028 + u16 w_index = le16_to_cpu(ctrl->wIndex);
2029 + u16 w_value = le16_to_cpu(ctrl->wValue);
2030 +
2031 + /* Usually this just stores reply data in the pre-allocated ep0 buffer,
2032 + * but config change events will also reconfigure hardware. */
2033 + switch (ctrl->bRequest) {
2034 +
2035 + case USB_REQ_GET_DESCRIPTOR:
2036 + if (ctrl->bRequestType != (USB_DIR_IN | USB_TYPE_STANDARD |
2037 + USB_RECIP_DEVICE))
2038 + break;
2039 + switch (w_value >> 8) {
2040 +
2041 + case USB_DT_DEVICE:
2042 + VDBG(fsg, "get device descriptor\n");
2043 + device_desc.bMaxPacketSize0 = fsg->ep0->maxpacket;
2044 + value = sizeof device_desc;
2045 + memcpy(req->buf, &device_desc, value);
2046 + break;
2047 + case USB_DT_DEVICE_QUALIFIER:
2048 + VDBG(fsg, "get device qualifier\n");
2049 + if (!gadget_is_dualspeed(fsg->gadget) ||
2050 + fsg->gadget->speed == USB_SPEED_SUPER)
2051 + break;
2052 + /*
2053 + * Assume ep0 uses the same maxpacket value for both
2054 + * speeds
2055 + */
2056 + dev_qualifier.bMaxPacketSize0 = fsg->ep0->maxpacket;
2057 + value = sizeof dev_qualifier;
2058 + memcpy(req->buf, &dev_qualifier, value);
2059 + break;
2060 +
2061 + case USB_DT_OTHER_SPEED_CONFIG:
2062 + VDBG(fsg, "get other-speed config descriptor\n");
2063 + if (!gadget_is_dualspeed(fsg->gadget) ||
2064 + fsg->gadget->speed == USB_SPEED_SUPER)
2065 + break;
2066 + goto get_config;
2067 + case USB_DT_CONFIG:
2068 + VDBG(fsg, "get configuration descriptor\n");
2069 +get_config:
2070 + value = populate_config_buf(fsg->gadget,
2071 + req->buf,
2072 + w_value >> 8,
2073 + w_value & 0xff);
2074 + break;
2075 +
2076 + case USB_DT_STRING:
2077 + VDBG(fsg, "get string descriptor\n");
2078 +
2079 + /* wIndex == language code */
2080 + value = usb_gadget_get_string(&fsg_stringtab,
2081 + w_value & 0xff, req->buf);
2082 + break;
2083 +
2084 + case USB_DT_BOS:
2085 + VDBG(fsg, "get bos descriptor\n");
2086 +
2087 + if (gadget_is_superspeed(fsg->gadget))
2088 + value = populate_bos(fsg, req->buf);
2089 + break;
2090 + }
2091 +
2092 + break;
2093 +
2094 + /* One config, two speeds */
2095 + case USB_REQ_SET_CONFIGURATION:
2096 + if (ctrl->bRequestType != (USB_DIR_OUT | USB_TYPE_STANDARD |
2097 + USB_RECIP_DEVICE))
2098 + break;
2099 + VDBG(fsg, "set configuration\n");
2100 + if (w_value == CONFIG_VALUE || w_value == 0) {
2101 + fsg->new_config = w_value;
2102 +
2103 + /* Raise an exception to wipe out previous transaction
2104 + * state (queued bufs, etc) and set the new config. */
2105 + raise_exception(fsg, FSG_STATE_CONFIG_CHANGE);
2106 + value = DELAYED_STATUS;
2107 + }
2108 + break;
2109 + case USB_REQ_GET_CONFIGURATION:
2110 + if (ctrl->bRequestType != (USB_DIR_IN | USB_TYPE_STANDARD |
2111 + USB_RECIP_DEVICE))
2112 + break;
2113 + VDBG(fsg, "get configuration\n");
2114 + *(u8 *) req->buf = fsg->config;
2115 + value = 1;
2116 + break;
2117 +
2118 + case USB_REQ_SET_INTERFACE:
2119 + if (ctrl->bRequestType != (USB_DIR_OUT| USB_TYPE_STANDARD |
2120 + USB_RECIP_INTERFACE))
2121 + break;
2122 + if (fsg->config && w_index == 0) {
2123 +
2124 + /* Raise an exception to wipe out previous transaction
2125 + * state (queued bufs, etc) and install the new
2126 + * interface altsetting. */
2127 + raise_exception(fsg, FSG_STATE_INTERFACE_CHANGE);
2128 + value = DELAYED_STATUS;
2129 + }
2130 + break;
2131 + case USB_REQ_GET_INTERFACE:
2132 + if (ctrl->bRequestType != (USB_DIR_IN | USB_TYPE_STANDARD |
2133 + USB_RECIP_INTERFACE))
2134 + break;
2135 + if (!fsg->config)
2136 + break;
2137 + if (w_index != 0) {
2138 + value = -EDOM;
2139 + break;
2140 + }
2141 + VDBG(fsg, "get interface\n");
2142 + *(u8 *) req->buf = 0;
2143 + value = 1;
2144 + break;
2145 +
2146 + default:
2147 + VDBG(fsg,
2148 + "unknown control req %02x.%02x v%04x i%04x l%u\n",
2149 + ctrl->bRequestType, ctrl->bRequest,
2150 + w_value, w_index, le16_to_cpu(ctrl->wLength));
2151 + }
2152 +
2153 + return value;
2154 +}
2155 +
2156 +
2157 +static int fsg_setup(struct usb_gadget *gadget,
2158 + const struct usb_ctrlrequest *ctrl)
2159 +{
2160 + struct fsg_dev *fsg = get_gadget_data(gadget);
2161 + int rc;
2162 + int w_length = le16_to_cpu(ctrl->wLength);
2163 +
2164 + ++fsg->ep0_req_tag; // Record arrival of a new request
2165 + fsg->ep0req->context = NULL;
2166 + fsg->ep0req->length = 0;
2167 + dump_msg(fsg, "ep0-setup", (u8 *) ctrl, sizeof(*ctrl));
2168 +
2169 + if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_CLASS)
2170 + rc = class_setup_req(fsg, ctrl);
2171 + else
2172 + rc = standard_setup_req(fsg, ctrl);
2173 +
2174 + /* Respond with data/status or defer until later? */
2175 + if (rc >= 0 && rc != DELAYED_STATUS) {
2176 + rc = min(rc, w_length);
2177 + fsg->ep0req->length = rc;
2178 + fsg->ep0req->zero = rc < w_length;
2179 + fsg->ep0req_name = (ctrl->bRequestType & USB_DIR_IN ?
2180 + "ep0-in" : "ep0-out");
2181 + rc = ep0_queue(fsg);
2182 + }
2183 +
2184 + /* Device either stalls (rc < 0) or reports success */
2185 + return rc;
2186 +}
2187 +
2188 +
2189 +/*-------------------------------------------------------------------------*/
2190 +
2191 +/* All the following routines run in process context */
2192 +
2193 +
2194 +/* Use this for bulk or interrupt transfers, not ep0 */
2195 +static void start_transfer(struct fsg_dev *fsg, struct usb_ep *ep,
2196 + struct usb_request *req, int *pbusy,
2197 + enum fsg_buffer_state *state)
2198 +{
2199 + int rc;
2200 +
2201 + if (ep == fsg->bulk_in)
2202 + dump_msg(fsg, "bulk-in", req->buf, req->length);
2203 + else if (ep == fsg->intr_in)
2204 + dump_msg(fsg, "intr-in", req->buf, req->length);
2205 +
2206 + spin_lock_irq(&fsg->lock);
2207 + *pbusy = 1;
2208 + *state = BUF_STATE_BUSY;
2209 + spin_unlock_irq(&fsg->lock);
2210 + rc = usb_ep_queue(ep, req, GFP_KERNEL);
2211 + if (rc != 0) {
2212 + *pbusy = 0;
2213 + *state = BUF_STATE_EMPTY;
2214 +
2215 + /* We can't do much more than wait for a reset */
2216 +
2217 + /* Note: currently the net2280 driver fails zero-length
2218 + * submissions if DMA is enabled. */
2219 + if (rc != -ESHUTDOWN && !(rc == -EOPNOTSUPP &&
2220 + req->length == 0))
2221 + WARNING(fsg, "error in submission: %s --> %d\n",
2222 + ep->name, rc);
2223 + }
2224 +}
2225 +
2226 +
2227 +static int sleep_thread(struct fsg_dev *fsg)
2228 +{
2229 + int rc = 0;
2230 +
2231 + /* Wait until a signal arrives or we are woken up */
2232 + for (;;) {
2233 + try_to_freeze();
2234 + set_current_state(TASK_INTERRUPTIBLE);
2235 + if (signal_pending(current)) {
2236 + rc = -EINTR;
2237 + break;
2238 + }
2239 + if (fsg->thread_wakeup_needed)
2240 + break;
2241 + schedule();
2242 + }
2243 + __set_current_state(TASK_RUNNING);
2244 + fsg->thread_wakeup_needed = 0;
2245 + return rc;
2246 +}
2247 +
2248 +
2249 +/*-------------------------------------------------------------------------*/
2250 +
2251 +static int do_read(struct fsg_dev *fsg)
2252 +{
2253 + struct fsg_lun *curlun = fsg->curlun;
2254 + u32 lba;
2255 + struct fsg_buffhd *bh;
2256 + int rc;
2257 + u32 amount_left;
2258 + loff_t file_offset, file_offset_tmp;
2259 + unsigned int amount;
2260 + ssize_t nread;
2261 +
2262 + /* Get the starting Logical Block Address and check that it's
2263 + * not too big */
2264 + if (fsg->cmnd[0] == READ_6)
2265 + lba = get_unaligned_be24(&fsg->cmnd[1]);
2266 + else {
2267 + lba = get_unaligned_be32(&fsg->cmnd[2]);
2268 +
2269 + /* We allow DPO (Disable Page Out = don't save data in the
2270 + * cache) and FUA (Force Unit Access = don't read from the
2271 + * cache), but we don't implement them. */
2272 + if ((fsg->cmnd[1] & ~0x18) != 0) {
2273 + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
2274 + return -EINVAL;
2275 + }
2276 + }
2277 + if (lba >= curlun->num_sectors) {
2278 + curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
2279 + return -EINVAL;
2280 + }
2281 + file_offset = ((loff_t) lba) << curlun->blkbits;
2282 +
2283 + /* Carry out the file reads */
2284 + amount_left = fsg->data_size_from_cmnd;
2285 + if (unlikely(amount_left == 0))
2286 + return -EIO; // No default reply
2287 +
2288 + for (;;) {
2289 +
2290 + /* Figure out how much we need to read:
2291 + * Try to read the remaining amount.
2292 + * But don't read more than the buffer size.
2293 + * And don't try to read past the end of the file.
2294 + */
2295 + amount = min((unsigned int) amount_left, mod_data.buflen);
2296 + amount = min((loff_t) amount,
2297 + curlun->file_length - file_offset);
2298 +
2299 + /* Wait for the next buffer to become available */
2300 + bh = fsg->next_buffhd_to_fill;
2301 + while (bh->state != BUF_STATE_EMPTY) {
2302 + rc = sleep_thread(fsg);
2303 + if (rc)
2304 + return rc;
2305 + }
2306 +
2307 + /* If we were asked to read past the end of file,
2308 + * end with an empty buffer. */
2309 + if (amount == 0) {
2310 + curlun->sense_data =
2311 + SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
2312 + curlun->sense_data_info = file_offset >> curlun->blkbits;
2313 + curlun->info_valid = 1;
2314 + bh->inreq->length = 0;
2315 + bh->state = BUF_STATE_FULL;
2316 + break;
2317 + }
2318 +
2319 + /* Perform the read */
2320 + file_offset_tmp = file_offset;
2321 + nread = vfs_read(curlun->filp,
2322 + (char __user *) bh->buf,
2323 + amount, &file_offset_tmp);
2324 + VLDBG(curlun, "file read %u @ %llu -> %d\n", amount,
2325 + (unsigned long long) file_offset,
2326 + (int) nread);
2327 + if (signal_pending(current))
2328 + return -EINTR;
2329 +
2330 + if (nread < 0) {
2331 + LDBG(curlun, "error in file read: %d\n",
2332 + (int) nread);
2333 + nread = 0;
2334 + } else if (nread < amount) {
2335 + LDBG(curlun, "partial file read: %d/%u\n",
2336 + (int) nread, amount);
2337 + nread = round_down(nread, curlun->blksize);
2338 + }
2339 + file_offset += nread;
2340 + amount_left -= nread;
2341 + fsg->residue -= nread;
2342 +
2343 + /* Except at the end of the transfer, nread will be
2344 + * equal to the buffer size, which is divisible by the
2345 + * bulk-in maxpacket size.
2346 + */
2347 + bh->inreq->length = nread;
2348 + bh->state = BUF_STATE_FULL;
2349 +
2350 + /* If an error occurred, report it and its position */
2351 + if (nread < amount) {
2352 + curlun->sense_data = SS_UNRECOVERED_READ_ERROR;
2353 + curlun->sense_data_info = file_offset >> curlun->blkbits;
2354 + curlun->info_valid = 1;
2355 + break;
2356 + }
2357 +
2358 + if (amount_left == 0)
2359 + break; // No more left to read
2360 +
2361 + /* Send this buffer and go read some more */
2362 + bh->inreq->zero = 0;
2363 + start_transfer(fsg, fsg->bulk_in, bh->inreq,
2364 + &bh->inreq_busy, &bh->state);
2365 + fsg->next_buffhd_to_fill = bh->next;
2366 + }
2367 +
2368 + return -EIO; // No default reply
2369 +}
2370 +
2371 +
2372 +/*-------------------------------------------------------------------------*/
2373 +
2374 +static int do_write(struct fsg_dev *fsg)
2375 +{
2376 + struct fsg_lun *curlun = fsg->curlun;
2377 + u32 lba;
2378 + struct fsg_buffhd *bh;
2379 + int get_some_more;
2380 + u32 amount_left_to_req, amount_left_to_write;
2381 + loff_t usb_offset, file_offset, file_offset_tmp;
2382 + unsigned int amount;
2383 + ssize_t nwritten;
2384 + int rc;
2385 +
2386 + if (curlun->ro) {
2387 + curlun->sense_data = SS_WRITE_PROTECTED;
2388 + return -EINVAL;
2389 + }
2390 + spin_lock(&curlun->filp->f_lock);
2391 + curlun->filp->f_flags &= ~O_SYNC; // Default is not to wait
2392 + spin_unlock(&curlun->filp->f_lock);
2393 +
2394 + /* Get the starting Logical Block Address and check that it's
2395 + * not too big */
2396 + if (fsg->cmnd[0] == WRITE_6)
2397 + lba = get_unaligned_be24(&fsg->cmnd[1]);
2398 + else {
2399 + lba = get_unaligned_be32(&fsg->cmnd[2]);
2400 +
2401 + /* We allow DPO (Disable Page Out = don't save data in the
2402 + * cache) and FUA (Force Unit Access = write directly to the
2403 + * medium). We don't implement DPO; we implement FUA by
2404 + * performing synchronous output. */
2405 + if ((fsg->cmnd[1] & ~0x18) != 0) {
2406 + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
2407 + return -EINVAL;
2408 + }
2409 + /* FUA */
2410 + if (!curlun->nofua && (fsg->cmnd[1] & 0x08)) {
2411 + spin_lock(&curlun->filp->f_lock);
2412 + curlun->filp->f_flags |= O_DSYNC;
2413 + spin_unlock(&curlun->filp->f_lock);
2414 + }
2415 + }
2416 + if (lba >= curlun->num_sectors) {
2417 + curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
2418 + return -EINVAL;
2419 + }
2420 +
2421 + /* Carry out the file writes */
2422 + get_some_more = 1;
2423 + file_offset = usb_offset = ((loff_t) lba) << curlun->blkbits;
2424 + amount_left_to_req = amount_left_to_write = fsg->data_size_from_cmnd;
2425 +
2426 + while (amount_left_to_write > 0) {
2427 +
2428 + /* Queue a request for more data from the host */
2429 + bh = fsg->next_buffhd_to_fill;
2430 + if (bh->state == BUF_STATE_EMPTY && get_some_more) {
2431 +
2432 + /* Figure out how much we want to get:
2433 + * Try to get the remaining amount,
2434 + * but not more than the buffer size.
2435 + */
2436 + amount = min(amount_left_to_req, mod_data.buflen);
2437 +
2438 + /* Beyond the end of the backing file? */
2439 + if (usb_offset >= curlun->file_length) {
2440 + get_some_more = 0;
2441 + curlun->sense_data =
2442 + SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
2443 + curlun->sense_data_info = usb_offset >> curlun->blkbits;
2444 + curlun->info_valid = 1;
2445 + continue;
2446 + }
2447 +
2448 + /* Get the next buffer */
2449 + usb_offset += amount;
2450 + fsg->usb_amount_left -= amount;
2451 + amount_left_to_req -= amount;
2452 + if (amount_left_to_req == 0)
2453 + get_some_more = 0;
2454 +
2455 + /* Except at the end of the transfer, amount will be
2456 + * equal to the buffer size, which is divisible by
2457 + * the bulk-out maxpacket size.
2458 + */
2459 + set_bulk_out_req_length(fsg, bh, amount);
2460 + start_transfer(fsg, fsg->bulk_out, bh->outreq,
2461 + &bh->outreq_busy, &bh->state);
2462 + fsg->next_buffhd_to_fill = bh->next;
2463 + continue;
2464 + }
2465 +
2466 + /* Write the received data to the backing file */
2467 + bh = fsg->next_buffhd_to_drain;
2468 + if (bh->state == BUF_STATE_EMPTY && !get_some_more)
2469 + break; // We stopped early
2470 + if (bh->state == BUF_STATE_FULL) {
2471 + smp_rmb();
2472 + fsg->next_buffhd_to_drain = bh->next;
2473 + bh->state = BUF_STATE_EMPTY;
2474 +
2475 + /* Did something go wrong with the transfer? */
2476 + if (bh->outreq->status != 0) {
2477 + curlun->sense_data = SS_COMMUNICATION_FAILURE;
2478 + curlun->sense_data_info = file_offset >> curlun->blkbits;
2479 + curlun->info_valid = 1;
2480 + break;
2481 + }
2482 +
2483 + amount = bh->outreq->actual;
2484 + if (curlun->file_length - file_offset < amount) {
2485 + LERROR(curlun,
2486 + "write %u @ %llu beyond end %llu\n",
2487 + amount, (unsigned long long) file_offset,
2488 + (unsigned long long) curlun->file_length);
2489 + amount = curlun->file_length - file_offset;
2490 + }
2491 +
2492 + /* Don't accept excess data. The spec doesn't say
2493 + * what to do in this case. We'll ignore the error.
2494 + */
2495 + amount = min(amount, bh->bulk_out_intended_length);
2496 +
2497 + /* Don't write a partial block */
2498 + amount = round_down(amount, curlun->blksize);
2499 + if (amount == 0)
2500 + goto empty_write;
2501 +
2502 + /* Perform the write */
2503 + file_offset_tmp = file_offset;
2504 + nwritten = vfs_write(curlun->filp,
2505 + (char __user *) bh->buf,
2506 + amount, &file_offset_tmp);
2507 + VLDBG(curlun, "file write %u @ %llu -> %d\n", amount,
2508 + (unsigned long long) file_offset,
2509 + (int) nwritten);
2510 + if (signal_pending(current))
2511 + return -EINTR; // Interrupted!
2512 +
2513 + if (nwritten < 0) {
2514 + LDBG(curlun, "error in file write: %d\n",
2515 + (int) nwritten);
2516 + nwritten = 0;
2517 + } else if (nwritten < amount) {
2518 + LDBG(curlun, "partial file write: %d/%u\n",
2519 + (int) nwritten, amount);
2520 + nwritten = round_down(nwritten, curlun->blksize);
2521 + }
2522 + file_offset += nwritten;
2523 + amount_left_to_write -= nwritten;
2524 + fsg->residue -= nwritten;
2525 +
2526 + /* If an error occurred, report it and its position */
2527 + if (nwritten < amount) {
2528 + curlun->sense_data = SS_WRITE_ERROR;
2529 + curlun->sense_data_info = file_offset >> curlun->blkbits;
2530 + curlun->info_valid = 1;
2531 + break;
2532 + }
2533 +
2534 + empty_write:
2535 + /* Did the host decide to stop early? */
2536 + if (bh->outreq->actual < bh->bulk_out_intended_length) {
2537 + fsg->short_packet_received = 1;
2538 + break;
2539 + }
2540 + continue;
2541 + }
2542 +
2543 + /* Wait for something to happen */
2544 + rc = sleep_thread(fsg);
2545 + if (rc)
2546 + return rc;
2547 + }
2548 +
2549 + return -EIO; // No default reply
2550 +}
2551 +
2552 +
2553 +/*-------------------------------------------------------------------------*/
2554 +
2555 +static int do_synchronize_cache(struct fsg_dev *fsg)
2556 +{
2557 + struct fsg_lun *curlun = fsg->curlun;
2558 + int rc;
2559 +
2560 + /* We ignore the requested LBA and write out all file's
2561 + * dirty data buffers. */
2562 + rc = fsg_lun_fsync_sub(curlun);
2563 + if (rc)
2564 + curlun->sense_data = SS_WRITE_ERROR;
2565 + return 0;
2566 +}
2567 +
2568 +
2569 +/*-------------------------------------------------------------------------*/
2570 +
2571 +static void invalidate_sub(struct fsg_lun *curlun)
2572 +{
2573 + struct file *filp = curlun->filp;
2574 + struct inode *inode = filp->f_path.dentry->d_inode;
2575 + unsigned long rc;
2576 +
2577 + rc = invalidate_mapping_pages(inode->i_mapping, 0, -1);
2578 + VLDBG(curlun, "invalidate_mapping_pages -> %ld\n", rc);
2579 +}
2580 +
2581 +static int do_verify(struct fsg_dev *fsg)
2582 +{
2583 + struct fsg_lun *curlun = fsg->curlun;
2584 + u32 lba;
2585 + u32 verification_length;
2586 + struct fsg_buffhd *bh = fsg->next_buffhd_to_fill;
2587 + loff_t file_offset, file_offset_tmp;
2588 + u32 amount_left;
2589 + unsigned int amount;
2590 + ssize_t nread;
2591 +
2592 + /* Get the starting Logical Block Address and check that it's
2593 + * not too big */
2594 + lba = get_unaligned_be32(&fsg->cmnd[2]);
2595 + if (lba >= curlun->num_sectors) {
2596 + curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
2597 + return -EINVAL;
2598 + }
2599 +
2600 + /* We allow DPO (Disable Page Out = don't save data in the
2601 + * cache) but we don't implement it. */
2602 + if ((fsg->cmnd[1] & ~0x10) != 0) {
2603 + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
2604 + return -EINVAL;
2605 + }
2606 +
2607 + verification_length = get_unaligned_be16(&fsg->cmnd[7]);
2608 + if (unlikely(verification_length == 0))
2609 + return -EIO; // No default reply
2610 +
2611 + /* Prepare to carry out the file verify */
2612 + amount_left = verification_length << curlun->blkbits;
2613 + file_offset = ((loff_t) lba) << curlun->blkbits;
2614 +
2615 + /* Write out all the dirty buffers before invalidating them */
2616 + fsg_lun_fsync_sub(curlun);
2617 + if (signal_pending(current))
2618 + return -EINTR;
2619 +
2620 + invalidate_sub(curlun);
2621 + if (signal_pending(current))
2622 + return -EINTR;
2623 +
2624 + /* Just try to read the requested blocks */
2625 + while (amount_left > 0) {
2626 +
2627 + /* Figure out how much we need to read:
2628 + * Try to read the remaining amount, but not more than
2629 + * the buffer size.
2630 + * And don't try to read past the end of the file.
2631 + */
2632 + amount = min((unsigned int) amount_left, mod_data.buflen);
2633 + amount = min((loff_t) amount,
2634 + curlun->file_length - file_offset);
2635 + if (amount == 0) {
2636 + curlun->sense_data =
2637 + SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
2638 + curlun->sense_data_info = file_offset >> curlun->blkbits;
2639 + curlun->info_valid = 1;
2640 + break;
2641 + }
2642 +
2643 + /* Perform the read */
2644 + file_offset_tmp = file_offset;
2645 + nread = vfs_read(curlun->filp,
2646 + (char __user *) bh->buf,
2647 + amount, &file_offset_tmp);
2648 + VLDBG(curlun, "file read %u @ %llu -> %d\n", amount,
2649 + (unsigned long long) file_offset,
2650 + (int) nread);
2651 + if (signal_pending(current))
2652 + return -EINTR;
2653 +
2654 + if (nread < 0) {
2655 + LDBG(curlun, "error in file verify: %d\n",
2656 + (int) nread);
2657 + nread = 0;
2658 + } else if (nread < amount) {
2659 + LDBG(curlun, "partial file verify: %d/%u\n",
2660 + (int) nread, amount);
2661 + nread = round_down(nread, curlun->blksize);
2662 + }
2663 + if (nread == 0) {
2664 + curlun->sense_data = SS_UNRECOVERED_READ_ERROR;
2665 + curlun->sense_data_info = file_offset >> curlun->blkbits;
2666 + curlun->info_valid = 1;
2667 + break;
2668 + }
2669 + file_offset += nread;
2670 + amount_left -= nread;
2671 + }
2672 + return 0;
2673 +}
2674 +
2675 +
2676 +/*-------------------------------------------------------------------------*/
2677 +
2678 +static int do_inquiry(struct fsg_dev *fsg, struct fsg_buffhd *bh)
2679 +{
2680 + u8 *buf = (u8 *) bh->buf;
2681 +
2682 + static char vendor_id[] = "Linux ";
2683 + static char product_disk_id[] = "File-Stor Gadget";
2684 + static char product_cdrom_id[] = "File-CD Gadget ";
2685 +
2686 + if (!fsg->curlun) { // Unsupported LUNs are okay
2687 + fsg->bad_lun_okay = 1;
2688 + memset(buf, 0, 36);
2689 + buf[0] = 0x7f; // Unsupported, no device-type
2690 + buf[4] = 31; // Additional length
2691 + return 36;
2692 + }
2693 +
2694 + memset(buf, 0, 8);
2695 + buf[0] = (mod_data.cdrom ? TYPE_ROM : TYPE_DISK);
2696 + if (mod_data.removable)
2697 + buf[1] = 0x80;
2698 + buf[2] = 2; // ANSI SCSI level 2
2699 + buf[3] = 2; // SCSI-2 INQUIRY data format
2700 + buf[4] = 31; // Additional length
2701 + // No special options
2702 + sprintf(buf + 8, "%-8s%-16s%04x", vendor_id,
2703 + (mod_data.cdrom ? product_cdrom_id :
2704 + product_disk_id),
2705 + mod_data.release);
2706 + return 36;
2707 +}
2708 +
2709 +
2710 +static int do_request_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh)
2711 +{
2712 + struct fsg_lun *curlun = fsg->curlun;
2713 + u8 *buf = (u8 *) bh->buf;
2714 + u32 sd, sdinfo;
2715 + int valid;
2716 +
2717 + /*
2718 + * From the SCSI-2 spec., section 7.9 (Unit attention condition):
2719 + *
2720 + * If a REQUEST SENSE command is received from an initiator
2721 + * with a pending unit attention condition (before the target
2722 + * generates the contingent allegiance condition), then the
2723 + * target shall either:
2724 + * a) report any pending sense data and preserve the unit
2725 + * attention condition on the logical unit, or,
2726 + * b) report the unit attention condition, may discard any
2727 + * pending sense data, and clear the unit attention
2728 + * condition on the logical unit for that initiator.
2729 + *
2730 + * FSG normally uses option a); enable this code to use option b).
2731 + */
2732 +#if 0
2733 + if (curlun && curlun->unit_attention_data != SS_NO_SENSE) {
2734 + curlun->sense_data = curlun->unit_attention_data;
2735 + curlun->unit_attention_data = SS_NO_SENSE;
2736 + }
2737 +#endif
2738 +
2739 + if (!curlun) { // Unsupported LUNs are okay
2740 + fsg->bad_lun_okay = 1;
2741 + sd = SS_LOGICAL_UNIT_NOT_SUPPORTED;
2742 + sdinfo = 0;
2743 + valid = 0;
2744 + } else {
2745 + sd = curlun->sense_data;
2746 + sdinfo = curlun->sense_data_info;
2747 + valid = curlun->info_valid << 7;
2748 + curlun->sense_data = SS_NO_SENSE;
2749 + curlun->sense_data_info = 0;
2750 + curlun->info_valid = 0;
2751 + }
2752 +
2753 + memset(buf, 0, 18);
2754 + buf[0] = valid | 0x70; // Valid, current error
2755 + buf[2] = SK(sd);
2756 + put_unaligned_be32(sdinfo, &buf[3]); /* Sense information */
2757 + buf[7] = 18 - 8; // Additional sense length
2758 + buf[12] = ASC(sd);
2759 + buf[13] = ASCQ(sd);
2760 + return 18;
2761 +}
2762 +
2763 +
2764 +static int do_read_capacity(struct fsg_dev *fsg, struct fsg_buffhd *bh)
2765 +{
2766 + struct fsg_lun *curlun = fsg->curlun;
2767 + u32 lba = get_unaligned_be32(&fsg->cmnd[2]);
2768 + int pmi = fsg->cmnd[8];
2769 + u8 *buf = (u8 *) bh->buf;
2770 +
2771 + /* Check the PMI and LBA fields */
2772 + if (pmi > 1 || (pmi == 0 && lba != 0)) {
2773 + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
2774 + return -EINVAL;
2775 + }
2776 +
2777 + put_unaligned_be32(curlun->num_sectors - 1, &buf[0]);
2778 + /* Max logical block */
2779 + put_unaligned_be32(curlun->blksize, &buf[4]); /* Block length */
2780 + return 8;
2781 +}
2782 +
2783 +
2784 +static int do_read_header(struct fsg_dev *fsg, struct fsg_buffhd *bh)
2785 +{
2786 + struct fsg_lun *curlun = fsg->curlun;
2787 + int msf = fsg->cmnd[1] & 0x02;
2788 + u32 lba = get_unaligned_be32(&fsg->cmnd[2]);
2789 + u8 *buf = (u8 *) bh->buf;
2790 +
2791 + if ((fsg->cmnd[1] & ~0x02) != 0) { /* Mask away MSF */
2792 + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
2793 + return -EINVAL;
2794 + }
2795 + if (lba >= curlun->num_sectors) {
2796 + curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
2797 + return -EINVAL;
2798 + }
2799 +
2800 + memset(buf, 0, 8);
2801 + buf[0] = 0x01; /* 2048 bytes of user data, rest is EC */
2802 + store_cdrom_address(&buf[4], msf, lba);
2803 + return 8;
2804 +}
2805 +
2806 +
2807 +static int do_read_toc(struct fsg_dev *fsg, struct fsg_buffhd *bh)
2808 +{
2809 + struct fsg_lun *curlun = fsg->curlun;
2810 + int msf = fsg->cmnd[1] & 0x02;
2811 + int start_track = fsg->cmnd[6];
2812 + u8 *buf = (u8 *) bh->buf;
2813 +
2814 + if ((fsg->cmnd[1] & ~0x02) != 0 || /* Mask away MSF */
2815 + start_track > 1) {
2816 + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
2817 + return -EINVAL;
2818 + }
2819 +
2820 + memset(buf, 0, 20);
2821 + buf[1] = (20-2); /* TOC data length */
2822 + buf[2] = 1; /* First track number */
2823 + buf[3] = 1; /* Last track number */
2824 + buf[5] = 0x16; /* Data track, copying allowed */
2825 + buf[6] = 0x01; /* Only track is number 1 */
2826 + store_cdrom_address(&buf[8], msf, 0);
2827 +
2828 + buf[13] = 0x16; /* Lead-out track is data */
2829 + buf[14] = 0xAA; /* Lead-out track number */
2830 + store_cdrom_address(&buf[16], msf, curlun->num_sectors);
2831 + return 20;
2832 +}
2833 +
2834 +
2835 +static int do_mode_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh)
2836 +{
2837 + struct fsg_lun *curlun = fsg->curlun;
2838 + int mscmnd = fsg->cmnd[0];
2839 + u8 *buf = (u8 *) bh->buf;
2840 + u8 *buf0 = buf;
2841 + int pc, page_code;
2842 + int changeable_values, all_pages;
2843 + int valid_page = 0;
2844 + int len, limit;
2845 +
2846 + if ((fsg->cmnd[1] & ~0x08) != 0) { // Mask away DBD
2847 + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
2848 + return -EINVAL;
2849 + }
2850 + pc = fsg->cmnd[2] >> 6;
2851 + page_code = fsg->cmnd[2] & 0x3f;
2852 + if (pc == 3) {
2853 + curlun->sense_data = SS_SAVING_PARAMETERS_NOT_SUPPORTED;
2854 + return -EINVAL;
2855 + }
2856 + changeable_values = (pc == 1);
2857 + all_pages = (page_code == 0x3f);
2858 +
2859 + /* Write the mode parameter header. Fixed values are: default
2860 + * medium type, no cache control (DPOFUA), and no block descriptors.
2861 + * The only variable value is the WriteProtect bit. We will fill in
2862 + * the mode data length later. */
2863 + memset(buf, 0, 8);
2864 + if (mscmnd == MODE_SENSE) {
2865 + buf[2] = (curlun->ro ? 0x80 : 0x00); // WP, DPOFUA
2866 + buf += 4;
2867 + limit = 255;
2868 + } else { // MODE_SENSE_10
2869 + buf[3] = (curlun->ro ? 0x80 : 0x00); // WP, DPOFUA
2870 + buf += 8;
2871 + limit = 65535; // Should really be mod_data.buflen
2872 + }
2873 +
2874 + /* No block descriptors */
2875 +
2876 + /* The mode pages, in numerical order. The only page we support
2877 + * is the Caching page. */
2878 + if (page_code == 0x08 || all_pages) {
2879 + valid_page = 1;
2880 + buf[0] = 0x08; // Page code
2881 + buf[1] = 10; // Page length
2882 + memset(buf+2, 0, 10); // None of the fields are changeable
2883 +
2884 + if (!changeable_values) {
2885 + buf[2] = 0x04; // Write cache enable,
2886 + // Read cache not disabled
2887 + // No cache retention priorities
2888 + put_unaligned_be16(0xffff, &buf[4]);
2889 + /* Don't disable prefetch */
2890 + /* Minimum prefetch = 0 */
2891 + put_unaligned_be16(0xffff, &buf[8]);
2892 + /* Maximum prefetch */
2893 + put_unaligned_be16(0xffff, &buf[10]);
2894 + /* Maximum prefetch ceiling */
2895 + }
2896 + buf += 12;
2897 + }
2898 +
2899 + /* Check that a valid page was requested and the mode data length
2900 + * isn't too long. */
2901 + len = buf - buf0;
2902 + if (!valid_page || len > limit) {
2903 + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
2904 + return -EINVAL;
2905 + }
2906 +
2907 + /* Store the mode data length */
2908 + if (mscmnd == MODE_SENSE)
2909 + buf0[0] = len - 1;
2910 + else
2911 + put_unaligned_be16(len - 2, buf0);
2912 + return len;
2913 +}
2914 +
2915 +
2916 +static int do_start_stop(struct fsg_dev *fsg)
2917 +{
2918 + struct fsg_lun *curlun = fsg->curlun;
2919 + int loej, start;
2920 +
2921 + if (!mod_data.removable) {
2922 + curlun->sense_data = SS_INVALID_COMMAND;
2923 + return -EINVAL;
2924 + }
2925 +
2926 + // int immed = fsg->cmnd[1] & 0x01;
2927 + loej = fsg->cmnd[4] & 0x02;
2928 + start = fsg->cmnd[4] & 0x01;
2929 +
2930 +#ifdef CONFIG_USB_FILE_STORAGE_TEST
2931 + if ((fsg->cmnd[1] & ~0x01) != 0 || // Mask away Immed
2932 + (fsg->cmnd[4] & ~0x03) != 0) { // Mask LoEj, Start
2933 + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
2934 + return -EINVAL;
2935 + }
2936 +
2937 + if (!start) {
2938 +
2939 + /* Are we allowed to unload the media? */
2940 + if (curlun->prevent_medium_removal) {
2941 + LDBG(curlun, "unload attempt prevented\n");
2942 + curlun->sense_data = SS_MEDIUM_REMOVAL_PREVENTED;
2943 + return -EINVAL;
2944 + }
2945 + if (loej) { // Simulate an unload/eject
2946 + up_read(&fsg->filesem);
2947 + down_write(&fsg->filesem);
2948 + fsg_lun_close(curlun);
2949 + up_write(&fsg->filesem);
2950 + down_read(&fsg->filesem);
2951 + }
2952 + } else {
2953 +
2954 + /* Our emulation doesn't support mounting; the medium is
2955 + * available for use as soon as it is loaded. */
2956 + if (!fsg_lun_is_open(curlun)) {
2957 + curlun->sense_data = SS_MEDIUM_NOT_PRESENT;
2958 + return -EINVAL;
2959 + }
2960 + }
2961 +#endif
2962 + return 0;
2963 +}
2964 +
2965 +
2966 +static int do_prevent_allow(struct fsg_dev *fsg)
2967 +{
2968 + struct fsg_lun *curlun = fsg->curlun;
2969 + int prevent;
2970 +
2971 + if (!mod_data.removable) {
2972 + curlun->sense_data = SS_INVALID_COMMAND;
2973 + return -EINVAL;
2974 + }
2975 +
2976 + prevent = fsg->cmnd[4] & 0x01;
2977 + if ((fsg->cmnd[4] & ~0x01) != 0) { // Mask away Prevent
2978 + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
2979 + return -EINVAL;
2980 + }
2981 +
2982 + if (curlun->prevent_medium_removal && !prevent)
2983 + fsg_lun_fsync_sub(curlun);
2984 + curlun->prevent_medium_removal = prevent;
2985 + return 0;
2986 +}
2987 +
2988 +
2989 +static int do_read_format_capacities(struct fsg_dev *fsg,
2990 + struct fsg_buffhd *bh)
2991 +{
2992 + struct fsg_lun *curlun = fsg->curlun;
2993 + u8 *buf = (u8 *) bh->buf;
2994 +
2995 + buf[0] = buf[1] = buf[2] = 0;
2996 + buf[3] = 8; // Only the Current/Maximum Capacity Descriptor
2997 + buf += 4;
2998 +
2999 + put_unaligned_be32(curlun->num_sectors, &buf[0]);
3000 + /* Number of blocks */
3001 + put_unaligned_be32(curlun->blksize, &buf[4]); /* Block length */
3002 + buf[4] = 0x02; /* Current capacity */
3003 + return 12;
3004 +}
3005 +
3006 +
3007 +static int do_mode_select(struct fsg_dev *fsg, struct fsg_buffhd *bh)
3008 +{
3009 + struct fsg_lun *curlun = fsg->curlun;
3010 +
3011 + /* We don't support MODE SELECT */
3012 + curlun->sense_data = SS_INVALID_COMMAND;
3013 + return -EINVAL;
3014 +}
3015 +
3016 +
3017 +/*-------------------------------------------------------------------------*/
3018 +
3019 +static int halt_bulk_in_endpoint(struct fsg_dev *fsg)
3020 +{
3021 + int rc;
3022 +
3023 + rc = fsg_set_halt(fsg, fsg->bulk_in);
3024 + if (rc == -EAGAIN)
3025 + VDBG(fsg, "delayed bulk-in endpoint halt\n");
3026 + while (rc != 0) {
3027 + if (rc != -EAGAIN) {
3028 + WARNING(fsg, "usb_ep_set_halt -> %d\n", rc);
3029 + rc = 0;
3030 + break;
3031 + }
3032 +
3033 + /* Wait for a short time and then try again */
3034 + if (msleep_interruptible(100) != 0)
3035 + return -EINTR;
3036 + rc = usb_ep_set_halt(fsg->bulk_in);
3037 + }
3038 + return rc;
3039 +}
3040 +
3041 +static int wedge_bulk_in_endpoint(struct fsg_dev *fsg)
3042 +{
3043 + int rc;
3044 +
3045 + DBG(fsg, "bulk-in set wedge\n");
3046 + rc = usb_ep_set_wedge(fsg->bulk_in);
3047 + if (rc == -EAGAIN)
3048 + VDBG(fsg, "delayed bulk-in endpoint wedge\n");
3049 + while (rc != 0) {
3050 + if (rc != -EAGAIN) {
3051 + WARNING(fsg, "usb_ep_set_wedge -> %d\n", rc);
3052 + rc = 0;
3053 + break;
3054 + }
3055 +
3056 + /* Wait for a short time and then try again */
3057 + if (msleep_interruptible(100) != 0)
3058 + return -EINTR;
3059 + rc = usb_ep_set_wedge(fsg->bulk_in);
3060 + }
3061 + return rc;
3062 +}
3063 +
3064 +static int throw_away_data(struct fsg_dev *fsg)
3065 +{
3066 + struct fsg_buffhd *bh;
3067 + u32 amount;
3068 + int rc;
3069 +
3070 + while ((bh = fsg->next_buffhd_to_drain)->state != BUF_STATE_EMPTY ||
3071 + fsg->usb_amount_left > 0) {
3072 +
3073 + /* Throw away the data in a filled buffer */
3074 + if (bh->state == BUF_STATE_FULL) {
3075 + smp_rmb();
3076 + bh->state = BUF_STATE_EMPTY;
3077 + fsg->next_buffhd_to_drain = bh->next;
3078 +
3079 + /* A short packet or an error ends everything */
3080 + if (bh->outreq->actual < bh->bulk_out_intended_length ||
3081 + bh->outreq->status != 0) {
3082 + raise_exception(fsg, FSG_STATE_ABORT_BULK_OUT);
3083 + return -EINTR;
3084 + }
3085 + continue;
3086 + }
3087 +
3088 + /* Try to submit another request if we need one */
3089 + bh = fsg->next_buffhd_to_fill;
3090 + if (bh->state == BUF_STATE_EMPTY && fsg->usb_amount_left > 0) {
3091 + amount = min(fsg->usb_amount_left,
3092 + (u32) mod_data.buflen);
3093 +
3094 + /* Except at the end of the transfer, amount will be
3095 + * equal to the buffer size, which is divisible by
3096 + * the bulk-out maxpacket size.
3097 + */
3098 + set_bulk_out_req_length(fsg, bh, amount);
3099 + start_transfer(fsg, fsg->bulk_out, bh->outreq,
3100 + &bh->outreq_busy, &bh->state);
3101 + fsg->next_buffhd_to_fill = bh->next;
3102 + fsg->usb_amount_left -= amount;
3103 + continue;
3104 + }
3105 +
3106 + /* Otherwise wait for something to happen */
3107 + rc = sleep_thread(fsg);
3108 + if (rc)
3109 + return rc;
3110 + }
3111 + return 0;
3112 +}
3113 +
3114 +
3115 +static int finish_reply(struct fsg_dev *fsg)
3116 +{
3117 + struct fsg_buffhd *bh = fsg->next_buffhd_to_fill;
3118 + int rc = 0;
3119 +
3120 + switch (fsg->data_dir) {
3121 + case DATA_DIR_NONE:
3122 + break; // Nothing to send
3123 +
3124 + /* If we don't know whether the host wants to read or write,
3125 + * this must be CB or CBI with an unknown command. We mustn't
3126 + * try to send or receive any data. So stall both bulk pipes
3127 + * if we can and wait for a reset. */
3128 + case DATA_DIR_UNKNOWN:
3129 + if (mod_data.can_stall) {
3130 + fsg_set_halt(fsg, fsg->bulk_out);
3131 + rc = halt_bulk_in_endpoint(fsg);
3132 + }
3133 + break;
3134 +
3135 + /* All but the last buffer of data must have already been sent */
3136 + case DATA_DIR_TO_HOST:
3137 + if (fsg->data_size == 0)
3138 + ; // Nothing to send
3139 +
3140 + /* If there's no residue, simply send the last buffer */
3141 + else if (fsg->residue == 0) {
3142 + bh->inreq->zero = 0;
3143 + start_transfer(fsg, fsg->bulk_in, bh->inreq,
3144 + &bh->inreq_busy, &bh->state);
3145 + fsg->next_buffhd_to_fill = bh->next;
3146 + }
3147 +
3148 + /* There is a residue. For CB and CBI, simply mark the end
3149 + * of the data with a short packet. However, if we are
3150 + * allowed to stall, there was no data at all (residue ==
3151 + * data_size), and the command failed (invalid LUN or
3152 + * sense data is set), then halt the bulk-in endpoint
3153 + * instead. */
3154 + else if (!transport_is_bbb()) {
3155 + if (mod_data.can_stall &&
3156 + fsg->residue == fsg->data_size &&
3157 + (!fsg->curlun || fsg->curlun->sense_data != SS_NO_SENSE)) {
3158 + bh->state = BUF_STATE_EMPTY;
3159 + rc = halt_bulk_in_endpoint(fsg);
3160 + } else {
3161 + bh->inreq->zero = 1;
3162 + start_transfer(fsg, fsg->bulk_in, bh->inreq,
3163 + &bh->inreq_busy, &bh->state);
3164 + fsg->next_buffhd_to_fill = bh->next;
3165 + }
3166 + }
3167 +
3168 + /*
3169 + * For Bulk-only, mark the end of the data with a short
3170 + * packet. If we are allowed to stall, halt the bulk-in
3171 + * endpoint. (Note: This violates the Bulk-Only Transport
3172 + * specification, which requires us to pad the data if we
3173 + * don't halt the endpoint. Presumably nobody will mind.)
3174 + */
3175 + else {
3176 + bh->inreq->zero = 1;
3177 + start_transfer(fsg, fsg->bulk_in, bh->inreq,
3178 + &bh->inreq_busy, &bh->state);
3179 + fsg->next_buffhd_to_fill = bh->next;
3180 + if (mod_data.can_stall)
3181 + rc = halt_bulk_in_endpoint(fsg);
3182 + }
3183 + break;
3184 +
3185 + /* We have processed all we want from the data the host has sent.
3186 + * There may still be outstanding bulk-out requests. */
3187 + case DATA_DIR_FROM_HOST:
3188 + if (fsg->residue == 0)
3189 + ; // Nothing to receive
3190 +
3191 + /* Did the host stop sending unexpectedly early? */
3192 + else if (fsg->short_packet_received) {
3193 + raise_exception(fsg, FSG_STATE_ABORT_BULK_OUT);
3194 + rc = -EINTR;
3195 + }
3196 +
3197 + /* We haven't processed all the incoming data. Even though
3198 + * we may be allowed to stall, doing so would cause a race.
3199 + * The controller may already have ACK'ed all the remaining
3200 + * bulk-out packets, in which case the host wouldn't see a
3201 + * STALL. Not realizing the endpoint was halted, it wouldn't
3202 + * clear the halt -- leading to problems later on. */
3203 +#if 0
3204 + else if (mod_data.can_stall) {
3205 + fsg_set_halt(fsg, fsg->bulk_out);
3206 + raise_exception(fsg, FSG_STATE_ABORT_BULK_OUT);
3207 + rc = -EINTR;
3208 + }
3209 +#endif
3210 +
3211 + /* We can't stall. Read in the excess data and throw it
3212 + * all away. */
3213 + else
3214 + rc = throw_away_data(fsg);
3215 + break;
3216 + }
3217 + return rc;
3218 +}
3219 +
3220 +
3221 +static int send_status(struct fsg_dev *fsg)
3222 +{
3223 + struct fsg_lun *curlun = fsg->curlun;
3224 + struct fsg_buffhd *bh;
3225 + int rc;
3226 + u8 status = US_BULK_STAT_OK;
3227 + u32 sd, sdinfo = 0;
3228 +
3229 + /* Wait for the next buffer to become available */
3230 + bh = fsg->next_buffhd_to_fill;
3231 + while (bh->state != BUF_STATE_EMPTY) {
3232 + rc = sleep_thread(fsg);
3233 + if (rc)
3234 + return rc;
3235 + }
3236 +
3237 + if (curlun) {
3238 + sd = curlun->sense_data;
3239 + sdinfo = curlun->sense_data_info;
3240 + } else if (fsg->bad_lun_okay)
3241 + sd = SS_NO_SENSE;
3242 + else
3243 + sd = SS_LOGICAL_UNIT_NOT_SUPPORTED;
3244 +
3245 + if (fsg->phase_error) {
3246 + DBG(fsg, "sending phase-error status\n");
3247 + status = US_BULK_STAT_PHASE;
3248 + sd = SS_INVALID_COMMAND;
3249 + } else if (sd != SS_NO_SENSE) {
3250 + DBG(fsg, "sending command-failure status\n");
3251 + status = US_BULK_STAT_FAIL;
3252 + VDBG(fsg, " sense data: SK x%02x, ASC x%02x, ASCQ x%02x;"
3253 + " info x%x\n",
3254 + SK(sd), ASC(sd), ASCQ(sd), sdinfo);
3255 + }
3256 +
3257 + if (transport_is_bbb()) {
3258 + struct bulk_cs_wrap *csw = bh->buf;
3259 +
3260 + /* Store and send the Bulk-only CSW */
3261 + csw->Signature = cpu_to_le32(US_BULK_CS_SIGN);
3262 + csw->Tag = fsg->tag;
3263 + csw->Residue = cpu_to_le32(fsg->residue);
3264 + csw->Status = status;
3265 +
3266 + bh->inreq->length = US_BULK_CS_WRAP_LEN;
3267 + bh->inreq->zero = 0;
3268 + start_transfer(fsg, fsg->bulk_in, bh->inreq,
3269 + &bh->inreq_busy, &bh->state);
3270 +
3271 + } else if (mod_data.transport_type == USB_PR_CB) {
3272 +
3273 + /* Control-Bulk transport has no status phase! */
3274 + return 0;
3275 +
3276 + } else { // USB_PR_CBI
3277 + struct interrupt_data *buf = bh->buf;
3278 +
3279 + /* Store and send the Interrupt data. UFI sends the ASC
3280 + * and ASCQ bytes. Everything else sends a Type (which
3281 + * is always 0) and the status Value. */
3282 + if (mod_data.protocol_type == USB_SC_UFI) {
3283 + buf->bType = ASC(sd);
3284 + buf->bValue = ASCQ(sd);
3285 + } else {
3286 + buf->bType = 0;
3287 + buf->bValue = status;
3288 + }
3289 + fsg->intreq->length = CBI_INTERRUPT_DATA_LEN;
3290 +
3291 + fsg->intr_buffhd = bh; // Point to the right buffhd
3292 + fsg->intreq->buf = bh->inreq->buf;
3293 + fsg->intreq->context = bh;
3294 + start_transfer(fsg, fsg->intr_in, fsg->intreq,
3295 + &fsg->intreq_busy, &bh->state);
3296 + }
3297 +
3298 + fsg->next_buffhd_to_fill = bh->next;
3299 + return 0;
3300 +}
3301 +
3302 +
3303 +/*-------------------------------------------------------------------------*/
3304 +
3305 +/* Check whether the command is properly formed and whether its data size
3306 + * and direction agree with the values we already have. */
3307 +static int check_command(struct fsg_dev *fsg, int cmnd_size,
3308 + enum data_direction data_dir, unsigned int mask,
3309 + int needs_medium, const char *name)
3310 +{
3311 + int i;
3312 + int lun = fsg->cmnd[1] >> 5;
3313 + static const char dirletter[4] = {'u', 'o', 'i', 'n'};
3314 + char hdlen[20];
3315 + struct fsg_lun *curlun;
3316 +
3317 + /* Adjust the expected cmnd_size for protocol encapsulation padding.
3318 + * Transparent SCSI doesn't pad. */
3319 + if (protocol_is_scsi())
3320 + ;
3321 +
3322 + /* There's some disagreement as to whether RBC pads commands or not.
3323 + * We'll play it safe and accept either form. */
3324 + else if (mod_data.protocol_type == USB_SC_RBC) {
3325 + if (fsg->cmnd_size == 12)
3326 + cmnd_size = 12;
3327 +
3328 + /* All the other protocols pad to 12 bytes */
3329 + } else
3330 + cmnd_size = 12;
3331 +
3332 + hdlen[0] = 0;
3333 + if (fsg->data_dir != DATA_DIR_UNKNOWN)
3334 + sprintf(hdlen, ", H%c=%u", dirletter[(int) fsg->data_dir],
3335 + fsg->data_size);
3336 + VDBG(fsg, "SCSI command: %s; Dc=%d, D%c=%u; Hc=%d%s\n",
3337 + name, cmnd_size, dirletter[(int) data_dir],
3338 + fsg->data_size_from_cmnd, fsg->cmnd_size, hdlen);
3339 +
3340 + /* We can't reply at all until we know the correct data direction
3341 + * and size. */
3342 + if (fsg->data_size_from_cmnd == 0)
3343 + data_dir = DATA_DIR_NONE;
3344 + if (fsg->data_dir == DATA_DIR_UNKNOWN) { // CB or CBI
3345 + fsg->data_dir = data_dir;
3346 + fsg->data_size = fsg->data_size_from_cmnd;
3347 +
3348 + } else { // Bulk-only
3349 + if (fsg->data_size < fsg->data_size_from_cmnd) {
3350 +
3351 + /* Host data size < Device data size is a phase error.
3352 + * Carry out the command, but only transfer as much
3353 + * as we are allowed. */
3354 + fsg->data_size_from_cmnd = fsg->data_size;
3355 + fsg->phase_error = 1;
3356 + }
3357 + }
3358 + fsg->residue = fsg->usb_amount_left = fsg->data_size;
3359 +
3360 + /* Conflicting data directions is a phase error */
3361 + if (fsg->data_dir != data_dir && fsg->data_size_from_cmnd > 0) {
3362 + fsg->phase_error = 1;
3363 + return -EINVAL;
3364 + }
3365 +
3366 + /* Verify the length of the command itself */
3367 + if (cmnd_size != fsg->cmnd_size) {
3368 +
3369 + /* Special case workaround: There are plenty of buggy SCSI
3370 + * implementations. Many have issues with cbw->Length
3371 + * field passing a wrong command size. For those cases we
3372 + * always try to work around the problem by using the length
3373 + * sent by the host side provided it is at least as large
3374 + * as the correct command length.
3375 + * Examples of such cases would be MS-Windows, which issues
3376 + * REQUEST SENSE with cbw->Length == 12 where it should
3377 + * be 6, and xbox360 issuing INQUIRY, TEST UNIT READY and
3378 + * REQUEST SENSE with cbw->Length == 10 where it should
3379 + * be 6 as well.
3380 + */
3381 + if (cmnd_size <= fsg->cmnd_size) {
3382 + DBG(fsg, "%s is buggy! Expected length %d "
3383 + "but we got %d\n", name,
3384 + cmnd_size, fsg->cmnd_size);
3385 + cmnd_size = fsg->cmnd_size;
3386 + } else {
3387 + fsg->phase_error = 1;
3388 + return -EINVAL;
3389 + }
3390 + }
3391 +
3392 + /* Check that the LUN values are consistent */
3393 + if (transport_is_bbb()) {
3394 + if (fsg->lun != lun)
3395 + DBG(fsg, "using LUN %d from CBW, "
3396 + "not LUN %d from CDB\n",
3397 + fsg->lun, lun);
3398 + }
3399 +
3400 + /* Check the LUN */
3401 + curlun = fsg->curlun;
3402 + if (curlun) {
3403 + if (fsg->cmnd[0] != REQUEST_SENSE) {
3404 + curlun->sense_data = SS_NO_SENSE;
3405 + curlun->sense_data_info = 0;
3406 + curlun->info_valid = 0;
3407 + }
3408 + } else {
3409 + fsg->bad_lun_okay = 0;
3410 +
3411 + /* INQUIRY and REQUEST SENSE commands are explicitly allowed
3412 + * to use unsupported LUNs; all others may not. */
3413 + if (fsg->cmnd[0] != INQUIRY &&
3414 + fsg->cmnd[0] != REQUEST_SENSE) {
3415 + DBG(fsg, "unsupported LUN %d\n", fsg->lun);
3416 + return -EINVAL;
3417 + }
3418 + }
3419 +
3420 + /* If a unit attention condition exists, only INQUIRY and
3421 + * REQUEST SENSE commands are allowed; anything else must fail. */
3422 + if (curlun && curlun->unit_attention_data != SS_NO_SENSE &&
3423 + fsg->cmnd[0] != INQUIRY &&
3424 + fsg->cmnd[0] != REQUEST_SENSE) {
3425 + curlun->sense_data = curlun->unit_attention_data;
3426 + curlun->unit_attention_data = SS_NO_SENSE;
3427 + return -EINVAL;
3428 + }
3429 +
3430 + /* Check that only command bytes listed in the mask are non-zero */
3431 + fsg->cmnd[1] &= 0x1f; // Mask away the LUN
3432 + for (i = 1; i < cmnd_size; ++i) {
3433 + if (fsg->cmnd[i] && !(mask & (1 << i))) {
3434 + if (curlun)
3435 + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
3436 + return -EINVAL;
3437 + }
3438 + }
3439 +
3440 + /* If the medium isn't mounted and the command needs to access
3441 + * it, return an error. */
3442 + if (curlun && !fsg_lun_is_open(curlun) && needs_medium) {
3443 + curlun->sense_data = SS_MEDIUM_NOT_PRESENT;
3444 + return -EINVAL;
3445 + }
3446 +
3447 + return 0;
3448 +}
3449 +
3450 +/* wrapper of check_command for data size in blocks handling */
3451 +static int check_command_size_in_blocks(struct fsg_dev *fsg, int cmnd_size,
3452 + enum data_direction data_dir, unsigned int mask,
3453 + int needs_medium, const char *name)
3454 +{
3455 + if (fsg->curlun)
3456 + fsg->data_size_from_cmnd <<= fsg->curlun->blkbits;
3457 + return check_command(fsg, cmnd_size, data_dir,
3458 + mask, needs_medium, name);
3459 +}
3460 +
3461 +static int do_scsi_command(struct fsg_dev *fsg)
3462 +{
3463 + struct fsg_buffhd *bh;
3464 + int rc;
3465 + int reply = -EINVAL;
3466 + int i;
3467 + static char unknown[16];
3468 +
3469 + dump_cdb(fsg);
3470 +
3471 + /* Wait for the next buffer to become available for data or status */
3472 + bh = fsg->next_buffhd_to_drain = fsg->next_buffhd_to_fill;
3473 + while (bh->state != BUF_STATE_EMPTY) {
3474 + rc = sleep_thread(fsg);
3475 + if (rc)
3476 + return rc;
3477 + }
3478 + fsg->phase_error = 0;
3479 + fsg->short_packet_received = 0;
3480 +
3481 + down_read(&fsg->filesem); // We're using the backing file
3482 + switch (fsg->cmnd[0]) {
3483 +
3484 + case INQUIRY:
3485 + fsg->data_size_from_cmnd = fsg->cmnd[4];
3486 + if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
3487 + (1<<4), 0,
3488 + "INQUIRY")) == 0)
3489 + reply = do_inquiry(fsg, bh);
3490 + break;
3491 +
3492 + case MODE_SELECT:
3493 + fsg->data_size_from_cmnd = fsg->cmnd[4];
3494 + if ((reply = check_command(fsg, 6, DATA_DIR_FROM_HOST,
3495 + (1<<1) | (1<<4), 0,
3496 + "MODE SELECT(6)")) == 0)
3497 + reply = do_mode_select(fsg, bh);
3498 + break;
3499 +
3500 + case MODE_SELECT_10:
3501 + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
3502 + if ((reply = check_command(fsg, 10, DATA_DIR_FROM_HOST,
3503 + (1<<1) | (3<<7), 0,
3504 + "MODE SELECT(10)")) == 0)
3505 + reply = do_mode_select(fsg, bh);
3506 + break;
3507 +
3508 + case MODE_SENSE:
3509 + fsg->data_size_from_cmnd = fsg->cmnd[4];
3510 + if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
3511 + (1<<1) | (1<<2) | (1<<4), 0,
3512 + "MODE SENSE(6)")) == 0)
3513 + reply = do_mode_sense(fsg, bh);
3514 + break;
3515 +
3516 + case MODE_SENSE_10:
3517 + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
3518 + if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
3519 + (1<<1) | (1<<2) | (3<<7), 0,
3520 + "MODE SENSE(10)")) == 0)
3521 + reply = do_mode_sense(fsg, bh);
3522 + break;
3523 +
3524 + case ALLOW_MEDIUM_REMOVAL:
3525 + fsg->data_size_from_cmnd = 0;
3526 + if ((reply = check_command(fsg, 6, DATA_DIR_NONE,
3527 + (1<<4), 0,
3528 + "PREVENT-ALLOW MEDIUM REMOVAL")) == 0)
3529 + reply = do_prevent_allow(fsg);
3530 + break;
3531 +
3532 + case READ_6:
3533 + i = fsg->cmnd[4];
3534 + fsg->data_size_from_cmnd = (i == 0) ? 256 : i;
3535 + if ((reply = check_command_size_in_blocks(fsg, 6,
3536 + DATA_DIR_TO_HOST,
3537 + (7<<1) | (1<<4), 1,
3538 + "READ(6)")) == 0)
3539 + reply = do_read(fsg);
3540 + break;
3541 +
3542 + case READ_10:
3543 + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
3544 + if ((reply = check_command_size_in_blocks(fsg, 10,
3545 + DATA_DIR_TO_HOST,
3546 + (1<<1) | (0xf<<2) | (3<<7), 1,
3547 + "READ(10)")) == 0)
3548 + reply = do_read(fsg);
3549 + break;
3550 +
3551 + case READ_12:
3552 + fsg->data_size_from_cmnd = get_unaligned_be32(&fsg->cmnd[6]);
3553 + if ((reply = check_command_size_in_blocks(fsg, 12,
3554 + DATA_DIR_TO_HOST,
3555 + (1<<1) | (0xf<<2) | (0xf<<6), 1,
3556 + "READ(12)")) == 0)
3557 + reply = do_read(fsg);
3558 + break;
3559 +
3560 + case READ_CAPACITY:
3561 + fsg->data_size_from_cmnd = 8;
3562 + if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
3563 + (0xf<<2) | (1<<8), 1,
3564 + "READ CAPACITY")) == 0)
3565 + reply = do_read_capacity(fsg, bh);
3566 + break;
3567 +
3568 + case READ_HEADER:
3569 + if (!mod_data.cdrom)
3570 + goto unknown_cmnd;
3571 + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
3572 + if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
3573 + (3<<7) | (0x1f<<1), 1,
3574 + "READ HEADER")) == 0)
3575 + reply = do_read_header(fsg, bh);
3576 + break;
3577 +
3578 + case READ_TOC:
3579 + if (!mod_data.cdrom)
3580 + goto unknown_cmnd;
3581 + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
3582 + if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
3583 + (7<<6) | (1<<1), 1,
3584 + "READ TOC")) == 0)
3585 + reply = do_read_toc(fsg, bh);
3586 + break;
3587 +
3588 + case READ_FORMAT_CAPACITIES:
3589 + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
3590 + if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
3591 + (3<<7), 1,
3592 + "READ FORMAT CAPACITIES")) == 0)
3593 + reply = do_read_format_capacities(fsg, bh);
3594 + break;
3595 +
3596 + case REQUEST_SENSE:
3597 + fsg->data_size_from_cmnd = fsg->cmnd[4];
3598 + if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
3599 + (1<<4), 0,
3600 + "REQUEST SENSE")) == 0)
3601 + reply = do_request_sense(fsg, bh);
3602 + break;
3603 +
3604 + case START_STOP:
3605 + fsg->data_size_from_cmnd = 0;
3606 + if ((reply = check_command(fsg, 6, DATA_DIR_NONE,
3607 + (1<<1) | (1<<4), 0,
3608 + "START-STOP UNIT")) == 0)
3609 + reply = do_start_stop(fsg);
3610 + break;
3611 +
3612 + case SYNCHRONIZE_CACHE:
3613 + fsg->data_size_from_cmnd = 0;
3614 + if ((reply = check_command(fsg, 10, DATA_DIR_NONE,
3615 + (0xf<<2) | (3<<7), 1,
3616 + "SYNCHRONIZE CACHE")) == 0)
3617 + reply = do_synchronize_cache(fsg);
3618 + break;
3619 +
3620 + case TEST_UNIT_READY:
3621 + fsg->data_size_from_cmnd = 0;
3622 + reply = check_command(fsg, 6, DATA_DIR_NONE,
3623 + 0, 1,
3624 + "TEST UNIT READY");
3625 + break;
3626 +
3627 + /* Although optional, this command is used by MS-Windows. We
3628 + * support a minimal version: BytChk must be 0. */
3629 + case VERIFY:
3630 + fsg->data_size_from_cmnd = 0;
3631 + if ((reply = check_command(fsg, 10, DATA_DIR_NONE,
3632 + (1<<1) | (0xf<<2) | (3<<7), 1,
3633 + "VERIFY")) == 0)
3634 + reply = do_verify(fsg);
3635 + break;
3636 +
3637 + case WRITE_6:
3638 + i = fsg->cmnd[4];
3639 + fsg->data_size_from_cmnd = (i == 0) ? 256 : i;
3640 + if ((reply = check_command_size_in_blocks(fsg, 6,
3641 + DATA_DIR_FROM_HOST,
3642 + (7<<1) | (1<<4), 1,
3643 + "WRITE(6)")) == 0)
3644 + reply = do_write(fsg);
3645 + break;
3646 +
3647 + case WRITE_10:
3648 + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
3649 + if ((reply = check_command_size_in_blocks(fsg, 10,
3650 + DATA_DIR_FROM_HOST,
3651 + (1<<1) | (0xf<<2) | (3<<7), 1,
3652 + "WRITE(10)")) == 0)
3653 + reply = do_write(fsg);
3654 + break;
3655 +
3656 + case WRITE_12:
3657 + fsg->data_size_from_cmnd = get_unaligned_be32(&fsg->cmnd[6]);
3658 + if ((reply = check_command_size_in_blocks(fsg, 12,
3659 + DATA_DIR_FROM_HOST,
3660 + (1<<1) | (0xf<<2) | (0xf<<6), 1,
3661 + "WRITE(12)")) == 0)
3662 + reply = do_write(fsg);
3663 + break;
3664 +
3665 + /* Some mandatory commands that we recognize but don't implement.
3666 + * They don't mean much in this setting. It's left as an exercise
3667 + * for anyone interested to implement RESERVE and RELEASE in terms
3668 + * of Posix locks. */
3669 + case FORMAT_UNIT:
3670 + case RELEASE:
3671 + case RESERVE:
3672 + case SEND_DIAGNOSTIC:
3673 + // Fall through
3674 +
3675 + default:
3676 + unknown_cmnd:
3677 + fsg->data_size_from_cmnd = 0;
3678 + sprintf(unknown, "Unknown x%02x", fsg->cmnd[0]);
3679 + if ((reply = check_command(fsg, fsg->cmnd_size,
3680 + DATA_DIR_UNKNOWN, ~0, 0, unknown)) == 0) {
3681 + fsg->curlun->sense_data = SS_INVALID_COMMAND;
3682 + reply = -EINVAL;
3683 + }
3684 + break;
3685 + }
3686 + up_read(&fsg->filesem);
3687 +
3688 + if (reply == -EINTR || signal_pending(current))
3689 + return -EINTR;
3690 +
3691 + /* Set up the single reply buffer for finish_reply() */
3692 + if (reply == -EINVAL)
3693 + reply = 0; // Error reply length
3694 + if (reply >= 0 && fsg->data_dir == DATA_DIR_TO_HOST) {
3695 + reply = min((u32) reply, fsg->data_size_from_cmnd);
3696 + bh->inreq->length = reply;
3697 + bh->state = BUF_STATE_FULL;
3698 + fsg->residue -= reply;
3699 + } // Otherwise it's already set
3700 +
3701 + return 0;
3702 +}
3703 +
3704 +
3705 +/*-------------------------------------------------------------------------*/
3706 +
3707 +static int received_cbw(struct fsg_dev *fsg, struct fsg_buffhd *bh)
3708 +{
3709 + struct usb_request *req = bh->outreq;
3710 + struct bulk_cb_wrap *cbw = req->buf;
3711 +
3712 + /* Was this a real packet? Should it be ignored? */
3713 + if (req->status || test_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags))
3714 + return -EINVAL;
3715 +
3716 + /* Is the CBW valid? */
3717 + if (req->actual != US_BULK_CB_WRAP_LEN ||
3718 + cbw->Signature != cpu_to_le32(
3719 + US_BULK_CB_SIGN)) {
3720 + DBG(fsg, "invalid CBW: len %u sig 0x%x\n",
3721 + req->actual,
3722 + le32_to_cpu(cbw->Signature));
3723 +
3724 + /* The Bulk-only spec says we MUST stall the IN endpoint
3725 + * (6.6.1), so it's unavoidable. It also says we must
3726 + * retain this state until the next reset, but there's
3727 + * no way to tell the controller driver it should ignore
3728 + * Clear-Feature(HALT) requests.
3729 + *
3730 + * We aren't required to halt the OUT endpoint; instead
3731 + * we can simply accept and discard any data received
3732 + * until the next reset. */
3733 + wedge_bulk_in_endpoint(fsg);
3734 + set_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags);
3735 + return -EINVAL;
3736 + }
3737 +
3738 + /* Is the CBW meaningful? */
3739 + if (cbw->Lun >= FSG_MAX_LUNS || cbw->Flags & ~US_BULK_FLAG_IN ||
3740 + cbw->Length <= 0 || cbw->Length > MAX_COMMAND_SIZE) {
3741 + DBG(fsg, "non-meaningful CBW: lun = %u, flags = 0x%x, "
3742 + "cmdlen %u\n",
3743 + cbw->Lun, cbw->Flags, cbw->Length);
3744 +
3745 + /* We can do anything we want here, so let's stall the
3746 + * bulk pipes if we are allowed to. */
3747 + if (mod_data.can_stall) {
3748 + fsg_set_halt(fsg, fsg->bulk_out);
3749 + halt_bulk_in_endpoint(fsg);
3750 + }
3751 + return -EINVAL;
3752 + }
3753 +
3754 + /* Save the command for later */
3755 + fsg->cmnd_size = cbw->Length;
3756 + memcpy(fsg->cmnd, cbw->CDB, fsg->cmnd_size);
3757 + if (cbw->Flags & US_BULK_FLAG_IN)
3758 + fsg->data_dir = DATA_DIR_TO_HOST;
3759 + else
3760 + fsg->data_dir = DATA_DIR_FROM_HOST;
3761 + fsg->data_size = le32_to_cpu(cbw->DataTransferLength);
3762 + if (fsg->data_size == 0)
3763 + fsg->data_dir = DATA_DIR_NONE;
3764 + fsg->lun = cbw->Lun;
3765 + fsg->tag = cbw->Tag;
3766 + return 0;
3767 +}
3768 +
3769 +
3770 +static int get_next_command(struct fsg_dev *fsg)
3771 +{
3772 + struct fsg_buffhd *bh;
3773 + int rc = 0;
3774 +
3775 + if (transport_is_bbb()) {
3776 +
3777 + /* Wait for the next buffer to become available */
3778 + bh = fsg->next_buffhd_to_fill;
3779 + while (bh->state != BUF_STATE_EMPTY) {
3780 + rc = sleep_thread(fsg);
3781 + if (rc)
3782 + return rc;
3783 + }
3784 +
3785 + /* Queue a request to read a Bulk-only CBW */
3786 + set_bulk_out_req_length(fsg, bh, US_BULK_CB_WRAP_LEN);
3787 + start_transfer(fsg, fsg->bulk_out, bh->outreq,
3788 + &bh->outreq_busy, &bh->state);
3789 +
3790 + /* We will drain the buffer in software, which means we
3791 + * can reuse it for the next filling. No need to advance
3792 + * next_buffhd_to_fill. */
3793 +
3794 + /* Wait for the CBW to arrive */
3795 + while (bh->state != BUF_STATE_FULL) {
3796 + rc = sleep_thread(fsg);
3797 + if (rc)
3798 + return rc;
3799 + }
3800 + smp_rmb();
3801 + rc = received_cbw(fsg, bh);
3802 + bh->state = BUF_STATE_EMPTY;
3803 +
3804 + } else { // USB_PR_CB or USB_PR_CBI
3805 +
3806 + /* Wait for the next command to arrive */
3807 + while (fsg->cbbuf_cmnd_size == 0) {
3808 + rc = sleep_thread(fsg);
3809 + if (rc)
3810 + return rc;
3811 + }
3812 +
3813 + /* Is the previous status interrupt request still busy?
3814 + * The host is allowed to skip reading the status,
3815 + * so we must cancel it. */
3816 + if (fsg->intreq_busy)
3817 + usb_ep_dequeue(fsg->intr_in, fsg->intreq);
3818 +
3819 + /* Copy the command and mark the buffer empty */
3820 + fsg->data_dir = DATA_DIR_UNKNOWN;
3821 + spin_lock_irq(&fsg->lock);
3822 + fsg->cmnd_size = fsg->cbbuf_cmnd_size;
3823 + memcpy(fsg->cmnd, fsg->cbbuf_cmnd, fsg->cmnd_size);
3824 + fsg->cbbuf_cmnd_size = 0;
3825 + spin_unlock_irq(&fsg->lock);
3826 +
3827 + /* Use LUN from the command */
3828 + fsg->lun = fsg->cmnd[1] >> 5;
3829 + }
3830 +
3831 + /* Update current lun */
3832 + if (fsg->lun >= 0 && fsg->lun < fsg->nluns)
3833 + fsg->curlun = &fsg->luns[fsg->lun];
3834 + else
3835 + fsg->curlun = NULL;
3836 +
3837 + return rc;
3838 +}
3839 +
3840 +
3841 +/*-------------------------------------------------------------------------*/
3842 +
3843 +static int enable_endpoint(struct fsg_dev *fsg, struct usb_ep *ep,
3844 + const struct usb_endpoint_descriptor *d)
3845 +{
3846 + int rc;
3847 +
3848 + ep->driver_data = fsg;
3849 + ep->desc = d;
3850 + rc = usb_ep_enable(ep);
3851 + if (rc)
3852 + ERROR(fsg, "can't enable %s, result %d\n", ep->name, rc);
3853 + return rc;
3854 +}
3855 +
3856 +static int alloc_request(struct fsg_dev *fsg, struct usb_ep *ep,
3857 + struct usb_request **preq)
3858 +{
3859 + *preq = usb_ep_alloc_request(ep, GFP_ATOMIC);
3860 + if (*preq)
3861 + return 0;
3862 + ERROR(fsg, "can't allocate request for %s\n", ep->name);
3863 + return -ENOMEM;
3864 +}
3865 +
3866 +/*
3867 + * Reset interface setting and re-init endpoint state (toggle etc).
3868 + * Call with altsetting < 0 to disable the interface. The only other
3869 + * available altsetting is 0, which enables the interface.
3870 + */
3871 +static int do_set_interface(struct fsg_dev *fsg, int altsetting)
3872 +{
3873 + int rc = 0;
3874 + int i;
3875 + const struct usb_endpoint_descriptor *d;
3876 +
3877 + if (fsg->running)
3878 + DBG(fsg, "reset interface\n");
3879 +
3880 +reset:
3881 + /* Deallocate the requests */
3882 + for (i = 0; i < fsg_num_buffers; ++i) {
3883 + struct fsg_buffhd *bh = &fsg->buffhds[i];
3884 +
3885 + if (bh->inreq) {
3886 + usb_ep_free_request(fsg->bulk_in, bh->inreq);
3887 + bh->inreq = NULL;
3888 + }
3889 + if (bh->outreq) {
3890 + usb_ep_free_request(fsg->bulk_out, bh->outreq);
3891 + bh->outreq = NULL;
3892 + }
3893 + }
3894 + if (fsg->intreq) {
3895 + usb_ep_free_request(fsg->intr_in, fsg->intreq);
3896 + fsg->intreq = NULL;
3897 + }
3898 +
3899 + /* Disable the endpoints */
3900 + if (fsg->bulk_in_enabled) {
3901 + usb_ep_disable(fsg->bulk_in);
3902 + fsg->bulk_in_enabled = 0;
3903 + }
3904 + if (fsg->bulk_out_enabled) {
3905 + usb_ep_disable(fsg->bulk_out);
3906 + fsg->bulk_out_enabled = 0;
3907 + }
3908 + if (fsg->intr_in_enabled) {
3909 + usb_ep_disable(fsg->intr_in);
3910 + fsg->intr_in_enabled = 0;
3911 + }
3912 +
3913 + fsg->running = 0;
3914 + if (altsetting < 0 || rc != 0)
3915 + return rc;
3916 +
3917 + DBG(fsg, "set interface %d\n", altsetting);
3918 +
3919 + /* Enable the endpoints */
3920 + d = fsg_ep_desc(fsg->gadget,
3921 + &fsg_fs_bulk_in_desc, &fsg_hs_bulk_in_desc,
3922 + &fsg_ss_bulk_in_desc);
3923 + if ((rc = enable_endpoint(fsg, fsg->bulk_in, d)) != 0)
3924 + goto reset;
3925 + fsg->bulk_in_enabled = 1;
3926 +
3927 + d = fsg_ep_desc(fsg->gadget,
3928 + &fsg_fs_bulk_out_desc, &fsg_hs_bulk_out_desc,
3929 + &fsg_ss_bulk_out_desc);
3930 + if ((rc = enable_endpoint(fsg, fsg->bulk_out, d)) != 0)
3931 + goto reset;
3932 + fsg->bulk_out_enabled = 1;
3933 + fsg->bulk_out_maxpacket = usb_endpoint_maxp(d);
3934 + clear_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags);
3935 +
3936 + if (transport_is_cbi()) {
3937 + d = fsg_ep_desc(fsg->gadget,
3938 + &fsg_fs_intr_in_desc, &fsg_hs_intr_in_desc,
3939 + &fsg_ss_intr_in_desc);
3940 + if ((rc = enable_endpoint(fsg, fsg->intr_in, d)) != 0)
3941 + goto reset;
3942 + fsg->intr_in_enabled = 1;
3943 + }
3944 +
3945 + /* Allocate the requests */
3946 + for (i = 0; i < fsg_num_buffers; ++i) {
3947 + struct fsg_buffhd *bh = &fsg->buffhds[i];
3948 +
3949 + if ((rc = alloc_request(fsg, fsg->bulk_in, &bh->inreq)) != 0)
3950 + goto reset;
3951 + if ((rc = alloc_request(fsg, fsg->bulk_out, &bh->outreq)) != 0)
3952 + goto reset;
3953 + bh->inreq->buf = bh->outreq->buf = bh->buf;
3954 + bh->inreq->context = bh->outreq->context = bh;
3955 + bh->inreq->complete = bulk_in_complete;
3956 + bh->outreq->complete = bulk_out_complete;
3957 + }
3958 + if (transport_is_cbi()) {
3959 + if ((rc = alloc_request(fsg, fsg->intr_in, &fsg->intreq)) != 0)
3960 + goto reset;
3961 + fsg->intreq->complete = intr_in_complete;
3962 + }
3963 +
3964 + fsg->running = 1;
3965 + for (i = 0; i < fsg->nluns; ++i)
3966 + fsg->luns[i].unit_attention_data = SS_RESET_OCCURRED;
3967 + return rc;
3968 +}
3969 +
3970 +
3971 +/*
3972 + * Change our operational configuration. This code must agree with the code
3973 + * that returns config descriptors, and with interface altsetting code.
3974 + *
3975 + * It's also responsible for power management interactions. Some
3976 + * configurations might not work with our current power sources.
3977 + * For now we just assume the gadget is always self-powered.
3978 + */
3979 +static int do_set_config(struct fsg_dev *fsg, u8 new_config)
3980 +{
3981 + int rc = 0;
3982 +
3983 + /* Disable the single interface */
3984 + if (fsg->config != 0) {
3985 + DBG(fsg, "reset config\n");
3986 + fsg->config = 0;
3987 + rc = do_set_interface(fsg, -1);
3988 + }
3989 +
3990 + /* Enable the interface */
3991 + if (new_config != 0) {
3992 + fsg->config = new_config;
3993 + if ((rc = do_set_interface(fsg, 0)) != 0)
3994 + fsg->config = 0; // Reset on errors
3995 + else
3996 + INFO(fsg, "%s config #%d\n",
3997 + usb_speed_string(fsg->gadget->speed),
3998 + fsg->config);
3999 + }
4000 + return rc;
4001 +}
4002 +
4003 +
4004 +/*-------------------------------------------------------------------------*/
4005 +
4006 +static void handle_exception(struct fsg_dev *fsg)
4007 +{
4008 + siginfo_t info;
4009 + int sig;
4010 + int i;
4011 + int num_active;
4012 + struct fsg_buffhd *bh;
4013 + enum fsg_state old_state;
4014 + u8 new_config;
4015 + struct fsg_lun *curlun;
4016 + unsigned int exception_req_tag;
4017 + int rc;
4018 +
4019 + /* Clear the existing signals. Anything but SIGUSR1 is converted
4020 + * into a high-priority EXIT exception. */
4021 + for (;;) {
4022 + sig = dequeue_signal_lock(current, &current->blocked, &info);
4023 + if (!sig)
4024 + break;
4025 + if (sig != SIGUSR1) {
4026 + if (fsg->state < FSG_STATE_EXIT)
4027 + DBG(fsg, "Main thread exiting on signal\n");
4028 + raise_exception(fsg, FSG_STATE_EXIT);
4029 + }
4030 + }
4031 +
4032 + /* Cancel all the pending transfers */
4033 + if (fsg->intreq_busy)
4034 + usb_ep_dequeue(fsg->intr_in, fsg->intreq);
4035 + for (i = 0; i < fsg_num_buffers; ++i) {
4036 + bh = &fsg->buffhds[i];
4037 + if (bh->inreq_busy)
4038 + usb_ep_dequeue(fsg->bulk_in, bh->inreq);
4039 + if (bh->outreq_busy)
4040 + usb_ep_dequeue(fsg->bulk_out, bh->outreq);
4041 + }
4042 +
4043 + /* Wait until everything is idle */
4044 + for (;;) {
4045 + num_active = fsg->intreq_busy;
4046 + for (i = 0; i < fsg_num_buffers; ++i) {
4047 + bh = &fsg->buffhds[i];
4048 + num_active += bh->inreq_busy + bh->outreq_busy;
4049 + }
4050 + if (num_active == 0)
4051 + break;
4052 + if (sleep_thread(fsg))
4053 + return;
4054 + }
4055 +
4056 + /* Clear out the controller's fifos */
4057 + if (fsg->bulk_in_enabled)
4058 + usb_ep_fifo_flush(fsg->bulk_in);
4059 + if (fsg->bulk_out_enabled)
4060 + usb_ep_fifo_flush(fsg->bulk_out);
4061 + if (fsg->intr_in_enabled)
4062 + usb_ep_fifo_flush(fsg->intr_in);
4063 +
4064 + /* Reset the I/O buffer states and pointers, the SCSI
4065 + * state, and the exception. Then invoke the handler. */
4066 + spin_lock_irq(&fsg->lock);
4067 +
4068 + for (i = 0; i < fsg_num_buffers; ++i) {
4069 + bh = &fsg->buffhds[i];
4070 + bh->state = BUF_STATE_EMPTY;
4071 + }
4072 + fsg->next_buffhd_to_fill = fsg->next_buffhd_to_drain =
4073 + &fsg->buffhds[0];
4074 +
4075 + exception_req_tag = fsg->exception_req_tag;
4076 + new_config = fsg->new_config;
4077 + old_state = fsg->state;
4078 +
4079 + if (old_state == FSG_STATE_ABORT_BULK_OUT)
4080 + fsg->state = FSG_STATE_STATUS_PHASE;
4081 + else {
4082 + for (i = 0; i < fsg->nluns; ++i) {
4083 + curlun = &fsg->luns[i];
4084 + curlun->prevent_medium_removal = 0;
4085 + curlun->sense_data = curlun->unit_attention_data =
4086 + SS_NO_SENSE;
4087 + curlun->sense_data_info = 0;
4088 + curlun->info_valid = 0;
4089 + }
4090 + fsg->state = FSG_STATE_IDLE;
4091 + }
4092 + spin_unlock_irq(&fsg->lock);
4093 +
4094 + /* Carry out any extra actions required for the exception */
4095 + switch (old_state) {
4096 + default:
4097 + break;
4098 +
4099 + case FSG_STATE_ABORT_BULK_OUT:
4100 + send_status(fsg);
4101 + spin_lock_irq(&fsg->lock);
4102 + if (fsg->state == FSG_STATE_STATUS_PHASE)
4103 + fsg->state = FSG_STATE_IDLE;
4104 + spin_unlock_irq(&fsg->lock);
4105 + break;
4106 +
4107 + case FSG_STATE_RESET:
4108 + /* In case we were forced against our will to halt a
4109 + * bulk endpoint, clear the halt now. (The SuperH UDC
4110 + * requires this.) */
4111 + if (test_and_clear_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags))
4112 + usb_ep_clear_halt(fsg->bulk_in);
4113 +
4114 + if (transport_is_bbb()) {
4115 + if (fsg->ep0_req_tag == exception_req_tag)
4116 + ep0_queue(fsg); // Complete the status stage
4117 +
4118 + } else if (transport_is_cbi())
4119 + send_status(fsg); // Status by interrupt pipe
4120 +
4121 + /* Technically this should go here, but it would only be
4122 + * a waste of time. Ditto for the INTERFACE_CHANGE and
4123 + * CONFIG_CHANGE cases. */
4124 + // for (i = 0; i < fsg->nluns; ++i)
4125 + // fsg->luns[i].unit_attention_data = SS_RESET_OCCURRED;
4126 + break;
4127 +
4128 + case FSG_STATE_INTERFACE_CHANGE:
4129 + rc = do_set_interface(fsg, 0);
4130 + if (fsg->ep0_req_tag != exception_req_tag)
4131 + break;
4132 + if (rc != 0) // STALL on errors
4133 + fsg_set_halt(fsg, fsg->ep0);
4134 + else // Complete the status stage
4135 + ep0_queue(fsg);
4136 + break;
4137 +
4138 + case FSG_STATE_CONFIG_CHANGE:
4139 + rc = do_set_config(fsg, new_config);
4140 + if (fsg->ep0_req_tag != exception_req_tag)
4141 + break;
4142 + if (rc != 0) // STALL on errors
4143 + fsg_set_halt(fsg, fsg->ep0);
4144 + else // Complete the status stage
4145 + ep0_queue(fsg);
4146 + break;
4147 +
4148 + case FSG_STATE_DISCONNECT:
4149 + for (i = 0; i < fsg->nluns; ++i)
4150 + fsg_lun_fsync_sub(fsg->luns + i);
4151 + do_set_config(fsg, 0); // Unconfigured state
4152 + break;
4153 +
4154 + case FSG_STATE_EXIT:
4155 + case FSG_STATE_TERMINATED:
4156 + do_set_config(fsg, 0); // Free resources
4157 + spin_lock_irq(&fsg->lock);
4158 + fsg->state = FSG_STATE_TERMINATED; // Stop the thread
4159 + spin_unlock_irq(&fsg->lock);
4160 + break;
4161 + }
4162 +}
4163 +
4164 +
4165 +/*-------------------------------------------------------------------------*/
4166 +
4167 +static int fsg_main_thread(void *fsg_)
4168 +{
4169 + struct fsg_dev *fsg = fsg_;
4170 +
4171 + /* Allow the thread to be killed by a signal, but set the signal mask
4172 + * to block everything but INT, TERM, KILL, and USR1. */
4173 + allow_signal(SIGINT);
4174 + allow_signal(SIGTERM);
4175 + allow_signal(SIGKILL);
4176 + allow_signal(SIGUSR1);
4177 +
4178 + /* Allow the thread to be frozen */
4179 + set_freezable();
4180 +
4181 + /* Arrange for userspace references to be interpreted as kernel
4182 + * pointers. That way we can pass a kernel pointer to a routine
4183 + * that expects a __user pointer and it will work okay. */
4184 + set_fs(get_ds());
4185 +
4186 + /* The main loop */
4187 + while (fsg->state != FSG_STATE_TERMINATED) {
4188 + if (exception_in_progress(fsg) || signal_pending(current)) {
4189 + handle_exception(fsg);
4190 + continue;
4191 + }
4192 +
4193 + if (!fsg->running) {
4194 + sleep_thread(fsg);
4195 + continue;
4196 + }
4197 +
4198 + if (get_next_command(fsg))
4199 + continue;
4200 +
4201 + spin_lock_irq(&fsg->lock);
4202 + if (!exception_in_progress(fsg))
4203 + fsg->state = FSG_STATE_DATA_PHASE;
4204 + spin_unlock_irq(&fsg->lock);
4205 +
4206 + if (do_scsi_command(fsg) || finish_reply(fsg))
4207 + continue;
4208 +
4209 + spin_lock_irq(&fsg->lock);
4210 + if (!exception_in_progress(fsg))
4211 + fsg->state = FSG_STATE_STATUS_PHASE;
4212 + spin_unlock_irq(&fsg->lock);
4213 +
4214 + if (send_status(fsg))
4215 + continue;
4216 +
4217 + spin_lock_irq(&fsg->lock);
4218 + if (!exception_in_progress(fsg))
4219 + fsg->state = FSG_STATE_IDLE;
4220 + spin_unlock_irq(&fsg->lock);
4221 + }
4222 +
4223 + spin_lock_irq(&fsg->lock);
4224 + fsg->thread_task = NULL;
4225 + spin_unlock_irq(&fsg->lock);
4226 +
4227 + /* If we are exiting because of a signal, unregister the
4228 + * gadget driver. */
4229 + if (test_and_clear_bit(REGISTERED, &fsg->atomic_bitflags))
4230 + usb_gadget_unregister_driver(&fsg_driver);
4231 +
4232 + /* Let the unbind and cleanup routines know the thread has exited */
4233 + complete_and_exit(&fsg->thread_notifier, 0);
4234 +}
4235 +
4236 +
4237 +/*-------------------------------------------------------------------------*/
4238 +
4239 +
4240 +/* The write permissions and store_xxx pointers are set in fsg_bind() */
4241 +static DEVICE_ATTR(ro, 0444, fsg_show_ro, NULL);
4242 +static DEVICE_ATTR(nofua, 0644, fsg_show_nofua, NULL);
4243 +static DEVICE_ATTR(file, 0444, fsg_show_file, NULL);
4244 +
4245 +
4246 +/*-------------------------------------------------------------------------*/
4247 +
4248 +static void fsg_release(struct kref *ref)
4249 +{
4250 + struct fsg_dev *fsg = container_of(ref, struct fsg_dev, ref);
4251 +
4252 + kfree(fsg->luns);
4253 + kfree(fsg);
4254 +}
4255 +
4256 +static void lun_release(struct device *dev)
4257 +{
4258 + struct rw_semaphore *filesem = dev_get_drvdata(dev);
4259 + struct fsg_dev *fsg =
4260 + container_of(filesem, struct fsg_dev, filesem);
4261 +
4262 + kref_put(&fsg->ref, fsg_release);
4263 +}
4264 +
4265 +static void /* __init_or_exit */ fsg_unbind(struct usb_gadget *gadget)
4266 +{
4267 + struct fsg_dev *fsg = get_gadget_data(gadget);
4268 + int i;
4269 + struct fsg_lun *curlun;
4270 + struct usb_request *req = fsg->ep0req;
4271 +
4272 + DBG(fsg, "unbind\n");
4273 + clear_bit(REGISTERED, &fsg->atomic_bitflags);
4274 +
4275 + /* If the thread isn't already dead, tell it to exit now */
4276 + if (fsg->state != FSG_STATE_TERMINATED) {
4277 + raise_exception(fsg, FSG_STATE_EXIT);
4278 + wait_for_completion(&fsg->thread_notifier);
4279 +
4280 + /* The cleanup routine waits for this completion also */
4281 + complete(&fsg->thread_notifier);
4282 + }
4283 +
4284 + /* Unregister the sysfs attribute files and the LUNs */
4285 + for (i = 0; i < fsg->nluns; ++i) {
4286 + curlun = &fsg->luns[i];
4287 + if (curlun->registered) {
4288 + device_remove_file(&curlun->dev, &dev_attr_nofua);
4289 + device_remove_file(&curlun->dev, &dev_attr_ro);
4290 + device_remove_file(&curlun->dev, &dev_attr_file);
4291 + fsg_lun_close(curlun);
4292 + device_unregister(&curlun->dev);
4293 + curlun->registered = 0;
4294 + }
4295 + }
4296 +
4297 + /* Free the data buffers */
4298 + for (i = 0; i < fsg_num_buffers; ++i)
4299 + kfree(fsg->buffhds[i].buf);
4300 +
4301 + /* Free the request and buffer for endpoint 0 */
4302 + if (req) {
4303 + kfree(req->buf);
4304 + usb_ep_free_request(fsg->ep0, req);
4305 + }
4306 +
4307 + set_gadget_data(gadget, NULL);
4308 +}
4309 +
4310 +
4311 +static int __init check_parameters(struct fsg_dev *fsg)
4312 +{
4313 + int prot;
4314 + int gcnum;
4315 +
4316 + /* Store the default values */
4317 + mod_data.transport_type = USB_PR_BULK;
4318 + mod_data.transport_name = "Bulk-only";
4319 + mod_data.protocol_type = USB_SC_SCSI;
4320 + mod_data.protocol_name = "Transparent SCSI";
4321 +
4322 + /* Some peripheral controllers are known not to be able to
4323 + * halt bulk endpoints correctly. If one of them is present,
4324 + * disable stalls.
4325 + */
4326 + if (gadget_is_at91(fsg->gadget))
4327 + mod_data.can_stall = 0;
4328 +
4329 + if (mod_data.release == 0xffff) { // Parameter wasn't set
4330 + gcnum = usb_gadget_controller_number(fsg->gadget);
4331 + if (gcnum >= 0)
4332 + mod_data.release = 0x0300 + gcnum;
4333 + else {
4334 + WARNING(fsg, "controller '%s' not recognized\n",
4335 + fsg->gadget->name);
4336 + mod_data.release = 0x0399;
4337 + }
4338 + }
4339 +
4340 + prot = simple_strtol(mod_data.protocol_parm, NULL, 0);
4341 +
4342 +#ifdef CONFIG_USB_FILE_STORAGE_TEST
4343 + if (strnicmp(mod_data.transport_parm, "BBB", 10) == 0) {
4344 + ; // Use default setting
4345 + } else if (strnicmp(mod_data.transport_parm, "CB", 10) == 0) {
4346 + mod_data.transport_type = USB_PR_CB;
4347 + mod_data.transport_name = "Control-Bulk";
4348 + } else if (strnicmp(mod_data.transport_parm, "CBI", 10) == 0) {
4349 + mod_data.transport_type = USB_PR_CBI;
4350 + mod_data.transport_name = "Control-Bulk-Interrupt";
4351 + } else {
4352 + ERROR(fsg, "invalid transport: %s\n", mod_data.transport_parm);
4353 + return -EINVAL;
4354 + }
4355 +
4356 + if (strnicmp(mod_data.protocol_parm, "SCSI", 10) == 0 ||
4357 + prot == USB_SC_SCSI) {
4358 + ; // Use default setting
4359 + } else if (strnicmp(mod_data.protocol_parm, "RBC", 10) == 0 ||
4360 + prot == USB_SC_RBC) {
4361 + mod_data.protocol_type = USB_SC_RBC;
4362 + mod_data.protocol_name = "RBC";
4363 + } else if (strnicmp(mod_data.protocol_parm, "8020", 4) == 0 ||
4364 + strnicmp(mod_data.protocol_parm, "ATAPI", 10) == 0 ||
4365 + prot == USB_SC_8020) {
4366 + mod_data.protocol_type = USB_SC_8020;
4367 + mod_data.protocol_name = "8020i (ATAPI)";
4368 + } else if (strnicmp(mod_data.protocol_parm, "QIC", 3) == 0 ||
4369 + prot == USB_SC_QIC) {
4370 + mod_data.protocol_type = USB_SC_QIC;
4371 + mod_data.protocol_name = "QIC-157";
4372 + } else if (strnicmp(mod_data.protocol_parm, "UFI", 10) == 0 ||
4373 + prot == USB_SC_UFI) {
4374 + mod_data.protocol_type = USB_SC_UFI;
4375 + mod_data.protocol_name = "UFI";
4376 + } else if (strnicmp(mod_data.protocol_parm, "8070", 4) == 0 ||
4377 + prot == USB_SC_8070) {
4378 + mod_data.protocol_type = USB_SC_8070;
4379 + mod_data.protocol_name = "8070i";
4380 + } else {
4381 + ERROR(fsg, "invalid protocol: %s\n", mod_data.protocol_parm);
4382 + return -EINVAL;
4383 + }
4384 +
4385 + mod_data.buflen &= PAGE_CACHE_MASK;
4386 + if (mod_data.buflen <= 0) {
4387 + ERROR(fsg, "invalid buflen\n");
4388 + return -ETOOSMALL;
4389 + }
4390 +
4391 +#endif /* CONFIG_USB_FILE_STORAGE_TEST */
4392 +
4393 + /* Serial string handling.
4394 + * On a real device, the serial string would be loaded
4395 + * from permanent storage. */
4396 + if (mod_data.serial) {
4397 + const char *ch;
4398 + unsigned len = 0;
4399 +
4400 + /* Sanity check :
4401 + * The CB[I] specification limits the serial string to
4402 + * 12 uppercase hexadecimal characters.
4403 + * BBB need at least 12 uppercase hexadecimal characters,
4404 + * with a maximum of 126. */
4405 + for (ch = mod_data.serial; *ch; ++ch) {
4406 + ++len;
4407 + if ((*ch < '0' || *ch > '9') &&
4408 + (*ch < 'A' || *ch > 'F')) { /* not uppercase hex */
4409 + WARNING(fsg,
4410 + "Invalid serial string character: %c\n",
4411 + *ch);
4412 + goto no_serial;
4413 + }
4414 + }
4415 + if (len > 126 ||
4416 + (mod_data.transport_type == USB_PR_BULK && len < 12) ||
4417 + (mod_data.transport_type != USB_PR_BULK && len > 12)) {
4418 + WARNING(fsg, "Invalid serial string length!\n");
4419 + goto no_serial;
4420 + }
4421 + fsg_strings[FSG_STRING_SERIAL - 1].s = mod_data.serial;
4422 + } else {
4423 + WARNING(fsg, "No serial-number string provided!\n");
4424 + no_serial:
4425 + device_desc.iSerialNumber = 0;
4426 + }
4427 +
4428 + return 0;
4429 +}
4430 +
4431 +
4432 +static int __init fsg_bind(struct usb_gadget *gadget)
4433 +{
4434 + struct fsg_dev *fsg = the_fsg;
4435 + int rc;
4436 + int i;
4437 + struct fsg_lun *curlun;
4438 + struct usb_ep *ep;
4439 + struct usb_request *req;
4440 + char *pathbuf, *p;
4441 +
4442 + fsg->gadget = gadget;
4443 + set_gadget_data(gadget, fsg);
4444 + fsg->ep0 = gadget->ep0;
4445 + fsg->ep0->driver_data = fsg;
4446 +
4447 + if ((rc = check_parameters(fsg)) != 0)
4448 + goto out;
4449 +
4450 + if (mod_data.removable) { // Enable the store_xxx attributes
4451 + dev_attr_file.attr.mode = 0644;
4452 + dev_attr_file.store = fsg_store_file;
4453 + if (!mod_data.cdrom) {
4454 + dev_attr_ro.attr.mode = 0644;
4455 + dev_attr_ro.store = fsg_store_ro;
4456 + }
4457 + }
4458 +
4459 + /* Only for removable media? */
4460 + dev_attr_nofua.attr.mode = 0644;
4461 + dev_attr_nofua.store = fsg_store_nofua;
4462 +
4463 + /* Find out how many LUNs there should be */
4464 + i = mod_data.nluns;
4465 + if (i == 0)
4466 + i = max(mod_data.num_filenames, 1u);
4467 + if (i > FSG_MAX_LUNS) {
4468 + ERROR(fsg, "invalid number of LUNs: %d\n", i);
4469 + rc = -EINVAL;
4470 + goto out;
4471 + }
4472 +
4473 + /* Create the LUNs, open their backing files, and register the
4474 + * LUN devices in sysfs. */
4475 + fsg->luns = kzalloc(i * sizeof(struct fsg_lun), GFP_KERNEL);
4476 + if (!fsg->luns) {
4477 + rc = -ENOMEM;
4478 + goto out;
4479 + }
4480 + fsg->nluns = i;
4481 +
4482 + for (i = 0; i < fsg->nluns; ++i) {
4483 + curlun = &fsg->luns[i];
4484 + curlun->cdrom = !!mod_data.cdrom;
4485 + curlun->ro = mod_data.cdrom || mod_data.ro[i];
4486 + curlun->initially_ro = curlun->ro;
4487 + curlun->removable = mod_data.removable;
4488 + curlun->nofua = mod_data.nofua[i];
4489 + curlun->dev.release = lun_release;
4490 + curlun->dev.parent = &gadget->dev;
4491 + curlun->dev.driver = &fsg_driver.driver;
4492 + dev_set_drvdata(&curlun->dev, &fsg->filesem);
4493 + dev_set_name(&curlun->dev,"%s-lun%d",
4494 + dev_name(&gadget->dev), i);
4495 +
4496 + kref_get(&fsg->ref);
4497 + rc = device_register(&curlun->dev);
4498 + if (rc) {
4499 + INFO(fsg, "failed to register LUN%d: %d\n", i, rc);
4500 + put_device(&curlun->dev);
4501 + goto out;
4502 + }
4503 + curlun->registered = 1;
4504 +
4505 + rc = device_create_file(&curlun->dev, &dev_attr_ro);
4506 + if (rc)
4507 + goto out;
4508 + rc = device_create_file(&curlun->dev, &dev_attr_nofua);
4509 + if (rc)
4510 + goto out;
4511 + rc = device_create_file(&curlun->dev, &dev_attr_file);
4512 + if (rc)
4513 + goto out;
4514 +
4515 + if (mod_data.file[i] && *mod_data.file[i]) {
4516 + rc = fsg_lun_open(curlun, mod_data.file[i]);
4517 + if (rc)
4518 + goto out;
4519 + } else if (!mod_data.removable) {
4520 + ERROR(fsg, "no file given for LUN%d\n", i);
4521 + rc = -EINVAL;
4522 + goto out;
4523 + }
4524 + }
4525 +
4526 + /* Find all the endpoints we will use */
4527 + usb_ep_autoconfig_reset(gadget);
4528 + ep = usb_ep_autoconfig(gadget, &fsg_fs_bulk_in_desc);
4529 + if (!ep)
4530 + goto autoconf_fail;
4531 + ep->driver_data = fsg; // claim the endpoint
4532 + fsg->bulk_in = ep;
4533 +
4534 + ep = usb_ep_autoconfig(gadget, &fsg_fs_bulk_out_desc);
4535 + if (!ep)
4536 + goto autoconf_fail;
4537 + ep->driver_data = fsg; // claim the endpoint
4538 + fsg->bulk_out = ep;
4539 +
4540 + if (transport_is_cbi()) {
4541 + ep = usb_ep_autoconfig(gadget, &fsg_fs_intr_in_desc);
4542 + if (!ep)
4543 + goto autoconf_fail;
4544 + ep->driver_data = fsg; // claim the endpoint
4545 + fsg->intr_in = ep;
4546 + }
4547 +
4548 + /* Fix up the descriptors */
4549 + device_desc.idVendor = cpu_to_le16(mod_data.vendor);
4550 + device_desc.idProduct = cpu_to_le16(mod_data.product);
4551 + device_desc.bcdDevice = cpu_to_le16(mod_data.release);
4552 +
4553 + i = (transport_is_cbi() ? 3 : 2); // Number of endpoints
4554 + fsg_intf_desc.bNumEndpoints = i;
4555 + fsg_intf_desc.bInterfaceSubClass = mod_data.protocol_type;
4556 + fsg_intf_desc.bInterfaceProtocol = mod_data.transport_type;
4557 + fsg_fs_function[i + FSG_FS_FUNCTION_PRE_EP_ENTRIES] = NULL;
4558 +
4559 + if (gadget_is_dualspeed(gadget)) {
4560 + fsg_hs_function[i + FSG_HS_FUNCTION_PRE_EP_ENTRIES] = NULL;
4561 +
4562 + /* Assume endpoint addresses are the same for both speeds */
4563 + fsg_hs_bulk_in_desc.bEndpointAddress =
4564 + fsg_fs_bulk_in_desc.bEndpointAddress;
4565 + fsg_hs_bulk_out_desc.bEndpointAddress =
4566 + fsg_fs_bulk_out_desc.bEndpointAddress;
4567 + fsg_hs_intr_in_desc.bEndpointAddress =
4568 + fsg_fs_intr_in_desc.bEndpointAddress;
4569 + }
4570 +
4571 + if (gadget_is_superspeed(gadget)) {
4572 + unsigned max_burst;
4573 +
4574 + fsg_ss_function[i + FSG_SS_FUNCTION_PRE_EP_ENTRIES] = NULL;
4575 +
4576 + /* Calculate bMaxBurst, we know packet size is 1024 */
4577 + max_burst = min_t(unsigned, mod_data.buflen / 1024, 15);
4578 +
4579 + /* Assume endpoint addresses are the same for both speeds */
4580 + fsg_ss_bulk_in_desc.bEndpointAddress =
4581 + fsg_fs_bulk_in_desc.bEndpointAddress;
4582 + fsg_ss_bulk_in_comp_desc.bMaxBurst = max_burst;
4583 +
4584 + fsg_ss_bulk_out_desc.bEndpointAddress =
4585 + fsg_fs_bulk_out_desc.bEndpointAddress;
4586 + fsg_ss_bulk_out_comp_desc.bMaxBurst = max_burst;
4587 + }
4588 +
4589 + if (gadget_is_otg(gadget))
4590 + fsg_otg_desc.bmAttributes |= USB_OTG_HNP;
4591 +
4592 + rc = -ENOMEM;
4593 +
4594 + /* Allocate the request and buffer for endpoint 0 */
4595 + fsg->ep0req = req = usb_ep_alloc_request(fsg->ep0, GFP_KERNEL);
4596 + if (!req)
4597 + goto out;
4598 + req->buf = kmalloc(EP0_BUFSIZE, GFP_KERNEL);
4599 + if (!req->buf)
4600 + goto out;
4601 + req->complete = ep0_complete;
4602 +
4603 + /* Allocate the data buffers */
4604 + for (i = 0; i < fsg_num_buffers; ++i) {
4605 + struct fsg_buffhd *bh = &fsg->buffhds[i];
4606 +
4607 + /* Allocate for the bulk-in endpoint. We assume that
4608 + * the buffer will also work with the bulk-out (and
4609 + * interrupt-in) endpoint. */
4610 + bh->buf = kmalloc(mod_data.buflen, GFP_KERNEL);
4611 + if (!bh->buf)
4612 + goto out;
4613 + bh->next = bh + 1;
4614 + }
4615 + fsg->buffhds[fsg_num_buffers - 1].next = &fsg->buffhds[0];
4616 +
4617 + /* This should reflect the actual gadget power source */
4618 + usb_gadget_set_selfpowered(gadget);
4619 +
4620 + snprintf(fsg_string_manufacturer, sizeof fsg_string_manufacturer,
4621 + "%s %s with %s",
4622 + init_utsname()->sysname, init_utsname()->release,
4623 + gadget->name);
4624 +
4625 + fsg->thread_task = kthread_create(fsg_main_thread, fsg,
4626 + "file-storage-gadget");
4627 + if (IS_ERR(fsg->thread_task)) {
4628 + rc = PTR_ERR(fsg->thread_task);
4629 + goto out;
4630 + }
4631 +
4632 + INFO(fsg, DRIVER_DESC ", version: " DRIVER_VERSION "\n");
4633 + INFO(fsg, "NOTE: This driver is deprecated. "
4634 + "Consider using g_mass_storage instead.\n");
4635 + INFO(fsg, "Number of LUNs=%d\n", fsg->nluns);
4636 +
4637 + pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
4638 + for (i = 0; i < fsg->nluns; ++i) {
4639 + curlun = &fsg->luns[i];
4640 + if (fsg_lun_is_open(curlun)) {
4641 + p = NULL;
4642 + if (pathbuf) {
4643 + p = d_path(&curlun->filp->f_path,
4644 + pathbuf, PATH_MAX);
4645 + if (IS_ERR(p))
4646 + p = NULL;
4647 + }
4648 + LINFO(curlun, "ro=%d, nofua=%d, file: %s\n",
4649 + curlun->ro, curlun->nofua, (p ? p : "(error)"));
4650 + }
4651 + }
4652 + kfree(pathbuf);
4653 +
4654 + DBG(fsg, "transport=%s (x%02x)\n",
4655 + mod_data.transport_name, mod_data.transport_type);
4656 + DBG(fsg, "protocol=%s (x%02x)\n",
4657 + mod_data.protocol_name, mod_data.protocol_type);
4658 + DBG(fsg, "VendorID=x%04x, ProductID=x%04x, Release=x%04x\n",
4659 + mod_data.vendor, mod_data.product, mod_data.release);
4660 + DBG(fsg, "removable=%d, stall=%d, cdrom=%d, buflen=%u\n",
4661 + mod_data.removable, mod_data.can_stall,
4662 + mod_data.cdrom, mod_data.buflen);
4663 + DBG(fsg, "I/O thread pid: %d\n", task_pid_nr(fsg->thread_task));
4664 +
4665 + set_bit(REGISTERED, &fsg->atomic_bitflags);
4666 +
4667 + /* Tell the thread to start working */
4668 + wake_up_process(fsg->thread_task);
4669 + return 0;
4670 +
4671 +autoconf_fail:
4672 + ERROR(fsg, "unable to autoconfigure all endpoints\n");
4673 + rc = -ENOTSUPP;
4674 +
4675 +out:
4676 + fsg->state = FSG_STATE_TERMINATED; // The thread is dead
4677 + fsg_unbind(gadget);
4678 + complete(&fsg->thread_notifier);
4679 + return rc;
4680 +}
4681 +
4682 +
4683 +/*-------------------------------------------------------------------------*/
4684 +
4685 +static void fsg_suspend(struct usb_gadget *gadget)
4686 +{
4687 + struct fsg_dev *fsg = get_gadget_data(gadget);
4688 +
4689 + DBG(fsg, "suspend\n");
4690 + set_bit(SUSPENDED, &fsg->atomic_bitflags);
4691 +}
4692 +
4693 +static void fsg_resume(struct usb_gadget *gadget)
4694 +{
4695 + struct fsg_dev *fsg = get_gadget_data(gadget);
4696 +
4697 + DBG(fsg, "resume\n");
4698 + clear_bit(SUSPENDED, &fsg->atomic_bitflags);
4699 +}
4700 +
4701 +
4702 +/*-------------------------------------------------------------------------*/
4703 +
4704 +static struct usb_gadget_driver fsg_driver = {
4705 + .max_speed = USB_SPEED_SUPER,
4706 + .function = (char *) fsg_string_product,
4707 + .unbind = fsg_unbind,
4708 + .disconnect = fsg_disconnect,
4709 + .setup = fsg_setup,
4710 + .suspend = fsg_suspend,
4711 + .resume = fsg_resume,
4712 +
4713 + .driver = {
4714 + .name = DRIVER_NAME,
4715 + .owner = THIS_MODULE,
4716 + // .release = ...
4717 + // .suspend = ...
4718 + // .resume = ...
4719 + },
4720 +};
4721 +
4722 +
4723 +static int __init fsg_alloc(void)
4724 +{
4725 + struct fsg_dev *fsg;
4726 +
4727 + fsg = kzalloc(sizeof *fsg +
4728 + fsg_num_buffers * sizeof *(fsg->buffhds), GFP_KERNEL);
4729 +
4730 + if (!fsg)
4731 + return -ENOMEM;
4732 + spin_lock_init(&fsg->lock);
4733 + init_rwsem(&fsg->filesem);
4734 + kref_init(&fsg->ref);
4735 + init_completion(&fsg->thread_notifier);
4736 +
4737 + the_fsg = fsg;
4738 + return 0;
4739 +}
4740 +
4741 +
4742 +static int __init fsg_init(void)
4743 +{
4744 + int rc;
4745 + struct fsg_dev *fsg;
4746 +
4747 + rc = fsg_num_buffers_validate();
4748 + if (rc != 0)
4749 + return rc;
4750 +
4751 + if ((rc = fsg_alloc()) != 0)
4752 + return rc;
4753 + fsg = the_fsg;
4754 + if ((rc = usb_gadget_probe_driver(&fsg_driver, fsg_bind)) != 0)
4755 + kref_put(&fsg->ref, fsg_release);
4756 + return rc;
4757 +}
4758 +module_init(fsg_init);
4759 +
4760 +
4761 +static void __exit fsg_cleanup(void)
4762 +{
4763 + struct fsg_dev *fsg = the_fsg;
4764 +
4765 + /* Unregister the driver iff the thread hasn't already done so */
4766 + if (test_and_clear_bit(REGISTERED, &fsg->atomic_bitflags))
4767 + usb_gadget_unregister_driver(&fsg_driver);
4768 +
4769 + /* Wait for the thread to finish up */
4770 + wait_for_completion(&fsg->thread_notifier);
4771 +
4772 + kref_put(&fsg->ref, fsg_release);
4773 +}
4774 +module_exit(fsg_cleanup);
4775 --- a/drivers/usb/host/Kconfig
4776 +++ b/drivers/usb/host/Kconfig
4777 @@ -763,6 +763,16 @@ config USB_HWA_HCD
4778 To compile this driver a module, choose M here: the module
4779 will be called "hwa-hc".
4780
4781 +config USB_DWCOTG
4782 + bool "Synopsis DWC host support"
4783 + depends on USB && (FIQ || ARM64)
4784 + help
4785 + The Synopsis DWC controller is a dual-role
4786 + host/peripheral/OTG ("On The Go") USB controllers.
4787 +
4788 + Enable this option to support this IP in host controller mode.
4789 + If unsure, say N.
4790 +
4791 config USB_IMX21_HCD
4792 tristate "i.MX21 HCD support"
4793 depends on ARM && ARCH_MXC
4794 --- a/drivers/usb/host/Makefile
4795 +++ b/drivers/usb/host/Makefile
4796 @@ -73,6 +73,8 @@ obj-$(CONFIG_USB_SL811_CS) += sl811_cs.o
4797 obj-$(CONFIG_USB_U132_HCD) += u132-hcd.o
4798 obj-$(CONFIG_USB_R8A66597_HCD) += r8a66597-hcd.o
4799 obj-$(CONFIG_USB_HWA_HCD) += hwa-hc.o
4800 +
4801 +obj-$(CONFIG_USB_DWCOTG) += dwc_otg/ dwc_common_port/
4802 obj-$(CONFIG_USB_IMX21_HCD) += imx21-hcd.o
4803 obj-$(CONFIG_USB_FSL_USB2) += fsl-mph-dr-of.o
4804 obj-$(CONFIG_USB_EHCI_FSL) += fsl-mph-dr-of.o
4805 --- /dev/null
4806 +++ b/drivers/usb/host/dwc_common_port/Makefile
4807 @@ -0,0 +1,58 @@
4808 +#
4809 +# Makefile for DWC_common library
4810 +#
4811 +
4812 +ifneq ($(KERNELRELEASE),)
4813 +
4814 +ccflags-y += -DDWC_LINUX
4815 +#ccflags-y += -DDEBUG
4816 +#ccflags-y += -DDWC_DEBUG_REGS
4817 +#ccflags-y += -DDWC_DEBUG_MEMORY
4818 +
4819 +ccflags-y += -DDWC_LIBMODULE
4820 +ccflags-y += -DDWC_CCLIB
4821 +#ccflags-y += -DDWC_CRYPTOLIB
4822 +ccflags-y += -DDWC_NOTIFYLIB
4823 +ccflags-y += -DDWC_UTFLIB
4824 +
4825 +obj-$(CONFIG_USB_DWCOTG) += dwc_common_port_lib.o
4826 +dwc_common_port_lib-objs := dwc_cc.o dwc_modpow.o dwc_dh.o \
4827 + dwc_crypto.o dwc_notifier.o \
4828 + dwc_common_linux.o dwc_mem.o
4829 +
4830 +kernrelwd := $(subst ., ,$(KERNELRELEASE))
4831 +kernrel3 := $(word 1,$(kernrelwd)).$(word 2,$(kernrelwd)).$(word 3,$(kernrelwd))
4832 +
4833 +ifneq ($(kernrel3),2.6.20)
4834 +# grayg - I only know that we use ccflags-y in 2.6.31 actually
4835 +ccflags-y += $(CPPFLAGS)
4836 +endif
4837 +
4838 +else
4839 +
4840 +#ifeq ($(KDIR),)
4841 +#$(error Must give "KDIR=/path/to/kernel/source" on command line or in environment)
4842 +#endif
4843 +
4844 +ifeq ($(ARCH),)
4845 +$(error Must give "ARCH=<arch>" on command line or in environment. Also, if \
4846 + cross-compiling, must give "CROSS_COMPILE=/path/to/compiler/plus/tool-prefix-")
4847 +endif
4848 +
4849 +ifeq ($(DOXYGEN),)
4850 +DOXYGEN := doxygen
4851 +endif
4852 +
4853 +default:
4854 + $(MAKE) -C$(KDIR) M=$(PWD) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) modules
4855 +
4856 +docs: $(wildcard *.[hc]) doc/doxygen.cfg
4857 + $(DOXYGEN) doc/doxygen.cfg
4858 +
4859 +tags: $(wildcard *.[hc])
4860 + $(CTAGS) -e $(wildcard *.[hc]) $(wildcard linux/*.[hc]) $(wildcard $(KDIR)/include/linux/usb*.h)
4861 +
4862 +endif
4863 +
4864 +clean:
4865 + rm -rf *.o *.ko .*.cmd *.mod.c .*.o.d .*.o.tmp modules.order Module.markers Module.symvers .tmp_versions/
4866 --- /dev/null
4867 +++ b/drivers/usb/host/dwc_common_port/Makefile.fbsd
4868 @@ -0,0 +1,17 @@
4869 +CFLAGS += -I/sys/i386/compile/GENERIC -I/sys/i386/include -I/usr/include
4870 +CFLAGS += -DDWC_FREEBSD
4871 +CFLAGS += -DDEBUG
4872 +#CFLAGS += -DDWC_DEBUG_REGS
4873 +#CFLAGS += -DDWC_DEBUG_MEMORY
4874 +
4875 +#CFLAGS += -DDWC_LIBMODULE
4876 +#CFLAGS += -DDWC_CCLIB
4877 +#CFLAGS += -DDWC_CRYPTOLIB
4878 +#CFLAGS += -DDWC_NOTIFYLIB
4879 +#CFLAGS += -DDWC_UTFLIB
4880 +
4881 +KMOD = dwc_common_port_lib
4882 +SRCS = dwc_cc.c dwc_modpow.c dwc_dh.c dwc_crypto.c dwc_notifier.c \
4883 + dwc_common_fbsd.c dwc_mem.c
4884 +
4885 +.include <bsd.kmod.mk>
4886 --- /dev/null
4887 +++ b/drivers/usb/host/dwc_common_port/Makefile.linux
4888 @@ -0,0 +1,49 @@
4889 +#
4890 +# Makefile for DWC_common library
4891 +#
4892 +ifneq ($(KERNELRELEASE),)
4893 +
4894 +ccflags-y += -DDWC_LINUX
4895 +#ccflags-y += -DDEBUG
4896 +#ccflags-y += -DDWC_DEBUG_REGS
4897 +#ccflags-y += -DDWC_DEBUG_MEMORY
4898 +
4899 +ccflags-y += -DDWC_LIBMODULE
4900 +ccflags-y += -DDWC_CCLIB
4901 +ccflags-y += -DDWC_CRYPTOLIB
4902 +ccflags-y += -DDWC_NOTIFYLIB
4903 +ccflags-y += -DDWC_UTFLIB
4904 +
4905 +obj-m := dwc_common_port_lib.o
4906 +dwc_common_port_lib-objs := dwc_cc.o dwc_modpow.o dwc_dh.o \
4907 + dwc_crypto.o dwc_notifier.o \
4908 + dwc_common_linux.o dwc_mem.o
4909 +
4910 +else
4911 +
4912 +ifeq ($(KDIR),)
4913 +$(error Must give "KDIR=/path/to/kernel/source" on command line or in environment)
4914 +endif
4915 +
4916 +ifeq ($(ARCH),)
4917 +$(error Must give "ARCH=<arch>" on command line or in environment. Also, if \
4918 + cross-compiling, must give "CROSS_COMPILE=/path/to/compiler/plus/tool-prefix-")
4919 +endif
4920 +
4921 +ifeq ($(DOXYGEN),)
4922 +DOXYGEN := doxygen
4923 +endif
4924 +
4925 +default:
4926 + $(MAKE) -C$(KDIR) M=$(PWD) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) modules
4927 +
4928 +docs: $(wildcard *.[hc]) doc/doxygen.cfg
4929 + $(DOXYGEN) doc/doxygen.cfg
4930 +
4931 +tags: $(wildcard *.[hc])
4932 + $(CTAGS) -e $(wildcard *.[hc]) $(wildcard linux/*.[hc]) $(wildcard $(KDIR)/include/linux/usb*.h)
4933 +
4934 +endif
4935 +
4936 +clean:
4937 + rm -rf *.o *.ko .*.cmd *.mod.c .*.o.d .*.o.tmp modules.order Module.markers Module.symvers .tmp_versions/
4938 --- /dev/null
4939 +++ b/drivers/usb/host/dwc_common_port/changes.txt
4940 @@ -0,0 +1,174 @@
4941 +
4942 +dwc_read_reg32() and friends now take an additional parameter, a pointer to an
4943 +IO context struct. The IO context struct should live in an os-dependent struct
4944 +in your driver. As an example, the dwc_usb3 driver has an os-dependent struct
4945 +named 'os_dep' embedded in the main device struct. So there these calls look
4946 +like this:
4947 +
4948 + dwc_read_reg32(&usb3_dev->os_dep.ioctx, &pcd->dev_global_regs->dcfg);
4949 +
4950 + dwc_write_reg32(&usb3_dev->os_dep.ioctx,
4951 + &pcd->dev_global_regs->dcfg, 0);
4952 +
4953 +Note that for the existing Linux driver ports, it is not necessary to actually
4954 +define the 'ioctx' member in the os-dependent struct. Since Linux does not
4955 +require an IO context, its macros for dwc_read_reg32() and friends do not
4956 +use the context pointer, so it is optimized away by the compiler. But it is
4957 +necessary to add the pointer parameter to all of the call sites, to be ready
4958 +for any future ports (such as FreeBSD) which do require an IO context.
4959 +
4960 +
4961 +Similarly, dwc_alloc(), dwc_alloc_atomic(), dwc_strdup(), and dwc_free() now
4962 +take an additional parameter, a pointer to a memory context. Examples:
4963 +
4964 + addr = dwc_alloc(&usb3_dev->os_dep.memctx, size);
4965 +
4966 + dwc_free(&usb3_dev->os_dep.memctx, addr);
4967 +
4968 +Again, for the Linux ports, it is not necessary to actually define the memctx
4969 +member, but it is necessary to add the pointer parameter to all of the call
4970 +sites.
4971 +
4972 +
4973 +Same for dwc_dma_alloc() and dwc_dma_free(). Examples:
4974 +
4975 + virt_addr = dwc_dma_alloc(&usb3_dev->os_dep.dmactx, size, &phys_addr);
4976 +
4977 + dwc_dma_free(&usb3_dev->os_dep.dmactx, size, virt_addr, phys_addr);
4978 +
4979 +
4980 +Same for dwc_mutex_alloc() and dwc_mutex_free(). Examples:
4981 +
4982 + mutex = dwc_mutex_alloc(&usb3_dev->os_dep.mtxctx);
4983 +
4984 + dwc_mutex_free(&usb3_dev->os_dep.mtxctx, mutex);
4985 +
4986 +
4987 +Same for dwc_spinlock_alloc() and dwc_spinlock_free(). Examples:
4988 +
4989 + lock = dwc_spinlock_alloc(&usb3_dev->osdep.splctx);
4990 +
4991 + dwc_spinlock_free(&usb3_dev->osdep.splctx, lock);
4992 +
4993 +
4994 +Same for dwc_timer_alloc(). Example:
4995 +
4996 + timer = dwc_timer_alloc(&usb3_dev->os_dep.tmrctx, "dwc_usb3_tmr1",
4997 + cb_func, cb_data);
4998 +
4999 +
5000 +Same for dwc_waitq_alloc(). Example:
5001 +
5002 + waitq = dwc_waitq_alloc(&usb3_dev->os_dep.wtqctx);
5003 +
5004 +
5005 +Same for dwc_thread_run(). Example:
5006 +
5007 + thread = dwc_thread_run(&usb3_dev->os_dep.thdctx, func,
5008 + "dwc_usb3_thd1", data);
5009 +
5010 +
5011 +Same for dwc_workq_alloc(). Example:
5012 +
5013 + workq = dwc_workq_alloc(&usb3_dev->osdep.wkqctx, "dwc_usb3_wkq1");
5014 +
5015 +
5016 +Same for dwc_task_alloc(). Example:
5017 +
5018 + task = dwc_task_alloc(&usb3_dev->os_dep.tskctx, "dwc_usb3_tsk1",
5019 + cb_func, cb_data);
5020 +
5021 +
5022 +In addition to the context pointer additions, a few core functions have had
5023 +other changes made to their parameters:
5024 +
5025 +The 'flags' parameter to dwc_spinlock_irqsave() and dwc_spinunlock_irqrestore()
5026 +has been changed from a uint64_t to a dwc_irqflags_t.
5027 +
5028 +dwc_thread_should_stop() now takes a 'dwc_thread_t *' parameter, because the
5029 +FreeBSD equivalent of that function requires it.
5030 +
5031 +And, in addition to the context pointer, dwc_task_alloc() also adds a
5032 +'char *name' parameter, to be consistent with dwc_thread_run() and
5033 +dwc_workq_alloc(), and because the FreeBSD equivalent of that function
5034 +requires a unique name.
5035 +
5036 +
5037 +Here is a complete list of the core functions that now take a pointer to a
5038 +context as their first parameter:
5039 +
5040 + dwc_read_reg32
5041 + dwc_read_reg64
5042 + dwc_write_reg32
5043 + dwc_write_reg64
5044 + dwc_modify_reg32
5045 + dwc_modify_reg64
5046 + dwc_alloc
5047 + dwc_alloc_atomic
5048 + dwc_strdup
5049 + dwc_free
5050 + dwc_dma_alloc
5051 + dwc_dma_free
5052 + dwc_mutex_alloc
5053 + dwc_mutex_free
5054 + dwc_spinlock_alloc
5055 + dwc_spinlock_free
5056 + dwc_timer_alloc
5057 + dwc_waitq_alloc
5058 + dwc_thread_run
5059 + dwc_workq_alloc
5060 + dwc_task_alloc Also adds a 'char *name' as its 2nd parameter
5061 +
5062 +And here are the core functions that have other changes to their parameters:
5063 +
5064 + dwc_spinlock_irqsave 'flags' param is now a 'dwc_irqflags_t *'
5065 + dwc_spinunlock_irqrestore 'flags' param is now a 'dwc_irqflags_t'
5066 + dwc_thread_should_stop Adds a 'dwc_thread_t *' parameter
5067 +
5068 +
5069 +
5070 +The changes to the core functions also require some of the other library
5071 +functions to change:
5072 +
5073 + dwc_cc_if_alloc() and dwc_cc_if_free() now take a 'void *memctx'
5074 + (for memory allocation) as the 1st param and a 'void *mtxctx'
5075 + (for mutex allocation) as the 2nd param.
5076 +
5077 + dwc_cc_clear(), dwc_cc_add(), dwc_cc_change(), dwc_cc_remove(),
5078 + dwc_cc_data_for_save(), and dwc_cc_restore_from_data() now take a
5079 + 'void *memctx' as the 1st param.
5080 +
5081 + dwc_dh_modpow(), dwc_dh_pk(), and dwc_dh_derive_keys() now take a
5082 + 'void *memctx' as the 1st param.
5083 +
5084 + dwc_modpow() now takes a 'void *memctx' as the 1st param.
5085 +
5086 + dwc_alloc_notification_manager() now takes a 'void *memctx' as the
5087 + 1st param and a 'void *wkqctx' (for work queue allocation) as the 2nd
5088 + param, and also now returns an integer value that is non-zero if
5089 + allocation of its data structures or work queue fails.
5090 +
5091 + dwc_register_notifier() now takes a 'void *memctx' as the 1st param.
5092 +
5093 + dwc_memory_debug_start() now takes a 'void *mem_ctx' as the first
5094 + param, and also now returns an integer value that is non-zero if
5095 + allocation of its data structures fails.
5096 +
5097 +
5098 +
5099 +Other miscellaneous changes:
5100 +
5101 +The DEBUG_MEMORY and DEBUG_REGS #define's have been renamed to
5102 +DWC_DEBUG_MEMORY and DWC_DEBUG_REGS.
5103 +
5104 +The following #define's have been added to allow selectively compiling library
5105 +features:
5106 +
5107 + DWC_CCLIB
5108 + DWC_CRYPTOLIB
5109 + DWC_NOTIFYLIB
5110 + DWC_UTFLIB
5111 +
5112 +A DWC_LIBMODULE #define has also been added. If this is not defined, then the
5113 +module code in dwc_common_linux.c is not compiled in. This allows linking the
5114 +library code directly into a driver module, instead of as a standalone module.
5115 --- /dev/null
5116 +++ b/drivers/usb/host/dwc_common_port/doc/doxygen.cfg
5117 @@ -0,0 +1,270 @@
5118 +# Doxyfile 1.4.5
5119 +
5120 +#---------------------------------------------------------------------------
5121 +# Project related configuration options
5122 +#---------------------------------------------------------------------------
5123 +PROJECT_NAME = "Synopsys DWC Portability and Common Library for UWB"
5124 +PROJECT_NUMBER =
5125 +OUTPUT_DIRECTORY = doc
5126 +CREATE_SUBDIRS = NO
5127 +OUTPUT_LANGUAGE = English
5128 +BRIEF_MEMBER_DESC = YES
5129 +REPEAT_BRIEF = YES
5130 +ABBREVIATE_BRIEF = "The $name class" \
5131 + "The $name widget" \
5132 + "The $name file" \
5133 + is \
5134 + provides \
5135 + specifies \
5136 + contains \
5137 + represents \
5138 + a \
5139 + an \
5140 + the
5141 +ALWAYS_DETAILED_SEC = YES
5142 +INLINE_INHERITED_MEMB = NO
5143 +FULL_PATH_NAMES = NO
5144 +STRIP_FROM_PATH = ..
5145 +STRIP_FROM_INC_PATH =
5146 +SHORT_NAMES = NO
5147 +JAVADOC_AUTOBRIEF = YES
5148 +MULTILINE_CPP_IS_BRIEF = NO
5149 +DETAILS_AT_TOP = YES
5150 +INHERIT_DOCS = YES
5151 +SEPARATE_MEMBER_PAGES = NO
5152 +TAB_SIZE = 8
5153 +ALIASES =
5154 +OPTIMIZE_OUTPUT_FOR_C = YES
5155 +OPTIMIZE_OUTPUT_JAVA = NO
5156 +BUILTIN_STL_SUPPORT = NO
5157 +DISTRIBUTE_GROUP_DOC = NO
5158 +SUBGROUPING = NO
5159 +#---------------------------------------------------------------------------
5160 +# Build related configuration options
5161 +#---------------------------------------------------------------------------
5162 +EXTRACT_ALL = NO
5163 +EXTRACT_PRIVATE = NO
5164 +EXTRACT_STATIC = YES
5165 +EXTRACT_LOCAL_CLASSES = NO
5166 +EXTRACT_LOCAL_METHODS = NO
5167 +HIDE_UNDOC_MEMBERS = NO
5168 +HIDE_UNDOC_CLASSES = NO
5169 +HIDE_FRIEND_COMPOUNDS = NO
5170 +HIDE_IN_BODY_DOCS = NO
5171 +INTERNAL_DOCS = NO
5172 +CASE_SENSE_NAMES = YES
5173 +HIDE_SCOPE_NAMES = NO
5174 +SHOW_INCLUDE_FILES = NO
5175 +INLINE_INFO = YES
5176 +SORT_MEMBER_DOCS = NO
5177 +SORT_BRIEF_DOCS = NO
5178 +SORT_BY_SCOPE_NAME = NO
5179 +GENERATE_TODOLIST = YES
5180 +GENERATE_TESTLIST = YES
5181 +GENERATE_BUGLIST = YES
5182 +GENERATE_DEPRECATEDLIST= YES
5183 +ENABLED_SECTIONS =
5184 +MAX_INITIALIZER_LINES = 30
5185 +SHOW_USED_FILES = YES
5186 +SHOW_DIRECTORIES = YES
5187 +FILE_VERSION_FILTER =
5188 +#---------------------------------------------------------------------------
5189 +# configuration options related to warning and progress messages
5190 +#---------------------------------------------------------------------------
5191 +QUIET = YES
5192 +WARNINGS = YES
5193 +WARN_IF_UNDOCUMENTED = NO
5194 +WARN_IF_DOC_ERROR = YES
5195 +WARN_NO_PARAMDOC = YES
5196 +WARN_FORMAT = "$file:$line: $text"
5197 +WARN_LOGFILE =
5198 +#---------------------------------------------------------------------------
5199 +# configuration options related to the input files
5200 +#---------------------------------------------------------------------------
5201 +INPUT = .
5202 +FILE_PATTERNS = *.c \
5203 + *.cc \
5204 + *.cxx \
5205 + *.cpp \
5206 + *.c++ \
5207 + *.d \
5208 + *.java \
5209 + *.ii \
5210 + *.ixx \
5211 + *.ipp \
5212 + *.i++ \
5213 + *.inl \
5214 + *.h \
5215 + *.hh \
5216 + *.hxx \
5217 + *.hpp \
5218 + *.h++ \
5219 + *.idl \
5220 + *.odl \
5221 + *.cs \
5222 + *.php \
5223 + *.php3 \
5224 + *.inc \
5225 + *.m \
5226 + *.mm \
5227 + *.dox \
5228 + *.py \
5229 + *.C \
5230 + *.CC \
5231 + *.C++ \
5232 + *.II \
5233 + *.I++ \
5234 + *.H \
5235 + *.HH \
5236 + *.H++ \
5237 + *.CS \
5238 + *.PHP \
5239 + *.PHP3 \
5240 + *.M \
5241 + *.MM \
5242 + *.PY
5243 +RECURSIVE = NO
5244 +EXCLUDE =
5245 +EXCLUDE_SYMLINKS = NO
5246 +EXCLUDE_PATTERNS =
5247 +EXAMPLE_PATH =
5248 +EXAMPLE_PATTERNS = *
5249 +EXAMPLE_RECURSIVE = NO
5250 +IMAGE_PATH =
5251 +INPUT_FILTER =
5252 +FILTER_PATTERNS =
5253 +FILTER_SOURCE_FILES = NO
5254 +#---------------------------------------------------------------------------
5255 +# configuration options related to source browsing
5256 +#---------------------------------------------------------------------------
5257 +SOURCE_BROWSER = NO
5258 +INLINE_SOURCES = NO
5259 +STRIP_CODE_COMMENTS = YES
5260 +REFERENCED_BY_RELATION = YES
5261 +REFERENCES_RELATION = YES
5262 +USE_HTAGS = NO
5263 +VERBATIM_HEADERS = NO
5264 +#---------------------------------------------------------------------------
5265 +# configuration options related to the alphabetical class index
5266 +#---------------------------------------------------------------------------
5267 +ALPHABETICAL_INDEX = NO
5268 +COLS_IN_ALPHA_INDEX = 5
5269 +IGNORE_PREFIX =
5270 +#---------------------------------------------------------------------------
5271 +# configuration options related to the HTML output
5272 +#---------------------------------------------------------------------------
5273 +GENERATE_HTML = YES
5274 +HTML_OUTPUT = html
5275 +HTML_FILE_EXTENSION = .html
5276 +HTML_HEADER =
5277 +HTML_FOOTER =
5278 +HTML_STYLESHEET =
5279 +HTML_ALIGN_MEMBERS = YES
5280 +GENERATE_HTMLHELP = NO
5281 +CHM_FILE =
5282 +HHC_LOCATION =
5283 +GENERATE_CHI = NO
5284 +BINARY_TOC = NO
5285 +TOC_EXPAND = NO
5286 +DISABLE_INDEX = NO
5287 +ENUM_VALUES_PER_LINE = 4
5288 +GENERATE_TREEVIEW = YES
5289 +TREEVIEW_WIDTH = 250
5290 +#---------------------------------------------------------------------------
5291 +# configuration options related to the LaTeX output
5292 +#---------------------------------------------------------------------------
5293 +GENERATE_LATEX = NO
5294 +LATEX_OUTPUT = latex
5295 +LATEX_CMD_NAME = latex
5296 +MAKEINDEX_CMD_NAME = makeindex
5297 +COMPACT_LATEX = NO
5298 +PAPER_TYPE = a4wide
5299 +EXTRA_PACKAGES =
5300 +LATEX_HEADER =
5301 +PDF_HYPERLINKS = NO
5302 +USE_PDFLATEX = NO
5303 +LATEX_BATCHMODE = NO
5304 +LATEX_HIDE_INDICES = NO
5305 +#---------------------------------------------------------------------------
5306 +# configuration options related to the RTF output
5307 +#---------------------------------------------------------------------------
5308 +GENERATE_RTF = NO
5309 +RTF_OUTPUT = rtf
5310 +COMPACT_RTF = NO
5311 +RTF_HYPERLINKS = NO
5312 +RTF_STYLESHEET_FILE =
5313 +RTF_EXTENSIONS_FILE =
5314 +#---------------------------------------------------------------------------
5315 +# configuration options related to the man page output
5316 +#---------------------------------------------------------------------------
5317 +GENERATE_MAN = NO
5318 +MAN_OUTPUT = man
5319 +MAN_EXTENSION = .3
5320 +MAN_LINKS = NO
5321 +#---------------------------------------------------------------------------
5322 +# configuration options related to the XML output
5323 +#---------------------------------------------------------------------------
5324 +GENERATE_XML = NO
5325 +XML_OUTPUT = xml
5326 +XML_SCHEMA =
5327 +XML_DTD =
5328 +XML_PROGRAMLISTING = YES
5329 +#---------------------------------------------------------------------------
5330 +# configuration options for the AutoGen Definitions output
5331 +#---------------------------------------------------------------------------
5332 +GENERATE_AUTOGEN_DEF = NO
5333 +#---------------------------------------------------------------------------
5334 +# configuration options related to the Perl module output
5335 +#---------------------------------------------------------------------------
5336 +GENERATE_PERLMOD = NO
5337 +PERLMOD_LATEX = NO
5338 +PERLMOD_PRETTY = YES
5339 +PERLMOD_MAKEVAR_PREFIX =
5340 +#---------------------------------------------------------------------------
5341 +# Configuration options related to the preprocessor
5342 +#---------------------------------------------------------------------------
5343 +ENABLE_PREPROCESSING = YES
5344 +MACRO_EXPANSION = NO
5345 +EXPAND_ONLY_PREDEF = NO
5346 +SEARCH_INCLUDES = YES
5347 +INCLUDE_PATH =
5348 +INCLUDE_FILE_PATTERNS =
5349 +PREDEFINED = DEBUG DEBUG_MEMORY
5350 +EXPAND_AS_DEFINED =
5351 +SKIP_FUNCTION_MACROS = YES
5352 +#---------------------------------------------------------------------------
5353 +# Configuration::additions related to external references
5354 +#---------------------------------------------------------------------------
5355 +TAGFILES =
5356 +GENERATE_TAGFILE =
5357 +ALLEXTERNALS = NO
5358 +EXTERNAL_GROUPS = YES
5359 +PERL_PATH = /usr/bin/perl
5360 +#---------------------------------------------------------------------------
5361 +# Configuration options related to the dot tool
5362 +#---------------------------------------------------------------------------
5363 +CLASS_DIAGRAMS = YES
5364 +HIDE_UNDOC_RELATIONS = YES
5365 +HAVE_DOT = NO
5366 +CLASS_GRAPH = YES
5367 +COLLABORATION_GRAPH = YES
5368 +GROUP_GRAPHS = YES
5369 +UML_LOOK = NO
5370 +TEMPLATE_RELATIONS = NO
5371 +INCLUDE_GRAPH = NO
5372 +INCLUDED_BY_GRAPH = YES
5373 +CALL_GRAPH = NO
5374 +GRAPHICAL_HIERARCHY = YES
5375 +DIRECTORY_GRAPH = YES
5376 +DOT_IMAGE_FORMAT = png
5377 +DOT_PATH =
5378 +DOTFILE_DIRS =
5379 +MAX_DOT_GRAPH_DEPTH = 1000
5380 +DOT_TRANSPARENT = NO
5381 +DOT_MULTI_TARGETS = NO
5382 +GENERATE_LEGEND = YES
5383 +DOT_CLEANUP = YES
5384 +#---------------------------------------------------------------------------
5385 +# Configuration::additions related to the search engine
5386 +#---------------------------------------------------------------------------
5387 +SEARCHENGINE = NO
5388 --- /dev/null
5389 +++ b/drivers/usb/host/dwc_common_port/dwc_cc.c
5390 @@ -0,0 +1,532 @@
5391 +/* =========================================================================
5392 + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_cc.c $
5393 + * $Revision: #4 $
5394 + * $Date: 2010/11/04 $
5395 + * $Change: 1621692 $
5396 + *
5397 + * Synopsys Portability Library Software and documentation
5398 + * (hereinafter, "Software") is an Unsupported proprietary work of
5399 + * Synopsys, Inc. unless otherwise expressly agreed to in writing
5400 + * between Synopsys and you.
5401 + *
5402 + * The Software IS NOT an item of Licensed Software or Licensed Product
5403 + * under any End User Software License Agreement or Agreement for
5404 + * Licensed Product with Synopsys or any supplement thereto. You are
5405 + * permitted to use and redistribute this Software in source and binary
5406 + * forms, with or without modification, provided that redistributions
5407 + * of source code must retain this notice. You may not view, use,
5408 + * disclose, copy or distribute this file or any information contained
5409 + * herein except pursuant to this license grant from Synopsys. If you
5410 + * do not agree with this notice, including the disclaimer below, then
5411 + * you are not authorized to use the Software.
5412 + *
5413 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
5414 + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5415 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
5416 + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
5417 + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
5418 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
5419 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
5420 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
5421 + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
5422 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
5423 + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
5424 + * DAMAGE.
5425 + * ========================================================================= */
5426 +#ifdef DWC_CCLIB
5427 +
5428 +#include "dwc_cc.h"
5429 +
5430 +typedef struct dwc_cc
5431 +{
5432 + uint32_t uid;
5433 + uint8_t chid[16];
5434 + uint8_t cdid[16];
5435 + uint8_t ck[16];
5436 + uint8_t *name;
5437 + uint8_t length;
5438 + DWC_CIRCLEQ_ENTRY(dwc_cc) list_entry;
5439 +} dwc_cc_t;
5440 +
5441 +DWC_CIRCLEQ_HEAD(context_list, dwc_cc);
5442 +
5443 +/** The main structure for CC management. */
5444 +struct dwc_cc_if
5445 +{
5446 + dwc_mutex_t *mutex;
5447 + char *filename;
5448 +
5449 + unsigned is_host:1;
5450 +
5451 + dwc_notifier_t *notifier;
5452 +
5453 + struct context_list list;
5454 +};
5455 +
5456 +#ifdef DEBUG
5457 +static inline void dump_bytes(char *name, uint8_t *bytes, int len)
5458 +{
5459 + int i;
5460 + DWC_PRINTF("%s: ", name);
5461 + for (i=0; i<len; i++) {
5462 + DWC_PRINTF("%02x ", bytes[i]);
5463 + }
5464 + DWC_PRINTF("\n");
5465 +}
5466 +#else
5467 +#define dump_bytes(x...)
5468 +#endif
5469 +
5470 +static dwc_cc_t *alloc_cc(void *mem_ctx, uint8_t *name, uint32_t length)
5471 +{
5472 + dwc_cc_t *cc = dwc_alloc(mem_ctx, sizeof(dwc_cc_t));
5473 + if (!cc) {
5474 + return NULL;
5475 + }
5476 + DWC_MEMSET(cc, 0, sizeof(dwc_cc_t));
5477 +
5478 + if (name) {
5479 + cc->length = length;
5480 + cc->name = dwc_alloc(mem_ctx, length);
5481 + if (!cc->name) {
5482 + dwc_free(mem_ctx, cc);
5483 + return NULL;
5484 + }
5485 +
5486 + DWC_MEMCPY(cc->name, name, length);
5487 + }
5488 +
5489 + return cc;
5490 +}
5491 +
5492 +static void free_cc(void *mem_ctx, dwc_cc_t *cc)
5493 +{
5494 + if (cc->name) {
5495 + dwc_free(mem_ctx, cc->name);
5496 + }
5497 + dwc_free(mem_ctx, cc);
5498 +}
5499 +
5500 +static uint32_t next_uid(dwc_cc_if_t *cc_if)
5501 +{
5502 + uint32_t uid = 0;
5503 + dwc_cc_t *cc;
5504 + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
5505 + if (cc->uid > uid) {
5506 + uid = cc->uid;
5507 + }
5508 + }
5509 +
5510 + if (uid == 0) {
5511 + uid = 255;
5512 + }
5513 +
5514 + return uid + 1;
5515 +}
5516 +
5517 +static dwc_cc_t *cc_find(dwc_cc_if_t *cc_if, uint32_t uid)
5518 +{
5519 + dwc_cc_t *cc;
5520 + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
5521 + if (cc->uid == uid) {
5522 + return cc;
5523 + }
5524 + }
5525 + return NULL;
5526 +}
5527 +
5528 +static unsigned int cc_data_size(dwc_cc_if_t *cc_if)
5529 +{
5530 + unsigned int size = 0;
5531 + dwc_cc_t *cc;
5532 + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
5533 + size += (48 + 1);
5534 + if (cc->name) {
5535 + size += cc->length;
5536 + }
5537 + }
5538 + return size;
5539 +}
5540 +
5541 +static uint32_t cc_match_chid(dwc_cc_if_t *cc_if, uint8_t *chid)
5542 +{
5543 + uint32_t uid = 0;
5544 + dwc_cc_t *cc;
5545 +
5546 + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
5547 + if (DWC_MEMCMP(cc->chid, chid, 16) == 0) {
5548 + uid = cc->uid;
5549 + break;
5550 + }
5551 + }
5552 + return uid;
5553 +}
5554 +static uint32_t cc_match_cdid(dwc_cc_if_t *cc_if, uint8_t *cdid)
5555 +{
5556 + uint32_t uid = 0;
5557 + dwc_cc_t *cc;
5558 +
5559 + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
5560 + if (DWC_MEMCMP(cc->cdid, cdid, 16) == 0) {
5561 + uid = cc->uid;
5562 + break;
5563 + }
5564 + }
5565 + return uid;
5566 +}
5567 +
5568 +/* Internal cc_add */
5569 +static int32_t cc_add(void *mem_ctx, dwc_cc_if_t *cc_if, uint8_t *chid,
5570 + uint8_t *cdid, uint8_t *ck, uint8_t *name, uint8_t length)
5571 +{
5572 + dwc_cc_t *cc;
5573 + uint32_t uid;
5574 +
5575 + if (cc_if->is_host) {
5576 + uid = cc_match_cdid(cc_if, cdid);
5577 + }
5578 + else {
5579 + uid = cc_match_chid(cc_if, chid);
5580 + }
5581 +
5582 + if (uid) {
5583 + DWC_DEBUGC("Replacing previous connection context id=%d name=%p name_len=%d", uid, name, length);
5584 + cc = cc_find(cc_if, uid);
5585 + }
5586 + else {
5587 + cc = alloc_cc(mem_ctx, name, length);
5588 + cc->uid = next_uid(cc_if);
5589 + DWC_CIRCLEQ_INSERT_TAIL(&cc_if->list, cc, list_entry);
5590 + }
5591 +
5592 + DWC_MEMCPY(&(cc->chid[0]), chid, 16);
5593 + DWC_MEMCPY(&(cc->cdid[0]), cdid, 16);
5594 + DWC_MEMCPY(&(cc->ck[0]), ck, 16);
5595 +
5596 + DWC_DEBUGC("Added connection context id=%d name=%p name_len=%d", cc->uid, name, length);
5597 + dump_bytes("CHID", cc->chid, 16);
5598 + dump_bytes("CDID", cc->cdid, 16);
5599 + dump_bytes("CK", cc->ck, 16);
5600 + return cc->uid;
5601 +}
5602 +
5603 +/* Internal cc_clear */
5604 +static void cc_clear(void *mem_ctx, dwc_cc_if_t *cc_if)
5605 +{
5606 + while (!DWC_CIRCLEQ_EMPTY(&cc_if->list)) {
5607 + dwc_cc_t *cc = DWC_CIRCLEQ_FIRST(&cc_if->list);
5608 + DWC_CIRCLEQ_REMOVE_INIT(&cc_if->list, cc, list_entry);
5609 + free_cc(mem_ctx, cc);
5610 + }
5611 +}
5612 +
5613 +dwc_cc_if_t *dwc_cc_if_alloc(void *mem_ctx, void *mtx_ctx,
5614 + dwc_notifier_t *notifier, unsigned is_host)
5615 +{
5616 + dwc_cc_if_t *cc_if = NULL;
5617 +
5618 + /* Allocate a common_cc_if structure */
5619 + cc_if = dwc_alloc(mem_ctx, sizeof(dwc_cc_if_t));
5620 +
5621 + if (!cc_if)
5622 + return NULL;
5623 +
5624 +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
5625 + DWC_MUTEX_ALLOC_LINUX_DEBUG(cc_if->mutex);
5626 +#else
5627 + cc_if->mutex = dwc_mutex_alloc(mtx_ctx);
5628 +#endif
5629 + if (!cc_if->mutex) {
5630 + dwc_free(mem_ctx, cc_if);
5631 + return NULL;
5632 + }
5633 +
5634 + DWC_CIRCLEQ_INIT(&cc_if->list);
5635 + cc_if->is_host = is_host;
5636 + cc_if->notifier = notifier;
5637 + return cc_if;
5638 +}
5639 +
5640 +void dwc_cc_if_free(void *mem_ctx, void *mtx_ctx, dwc_cc_if_t *cc_if)
5641 +{
5642 +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
5643 + DWC_MUTEX_FREE(cc_if->mutex);
5644 +#else
5645 + dwc_mutex_free(mtx_ctx, cc_if->mutex);
5646 +#endif
5647 + cc_clear(mem_ctx, cc_if);
5648 + dwc_free(mem_ctx, cc_if);
5649 +}
5650 +
5651 +static void cc_changed(dwc_cc_if_t *cc_if)
5652 +{
5653 + if (cc_if->notifier) {
5654 + dwc_notify(cc_if->notifier, DWC_CC_LIST_CHANGED_NOTIFICATION, cc_if);
5655 + }
5656 +}
5657 +
5658 +void dwc_cc_clear(void *mem_ctx, dwc_cc_if_t *cc_if)
5659 +{
5660 + DWC_MUTEX_LOCK(cc_if->mutex);
5661 + cc_clear(mem_ctx, cc_if);
5662 + DWC_MUTEX_UNLOCK(cc_if->mutex);
5663 + cc_changed(cc_if);
5664 +}
5665 +
5666 +int32_t dwc_cc_add(void *mem_ctx, dwc_cc_if_t *cc_if, uint8_t *chid,
5667 + uint8_t *cdid, uint8_t *ck, uint8_t *name, uint8_t length)
5668 +{
5669 + uint32_t uid;
5670 +
5671 + DWC_MUTEX_LOCK(cc_if->mutex);
5672 + uid = cc_add(mem_ctx, cc_if, chid, cdid, ck, name, length);
5673 + DWC_MUTEX_UNLOCK(cc_if->mutex);
5674 + cc_changed(cc_if);
5675 +
5676 + return uid;
5677 +}
5678 +
5679 +void dwc_cc_change(void *mem_ctx, dwc_cc_if_t *cc_if, int32_t id, uint8_t *chid,
5680 + uint8_t *cdid, uint8_t *ck, uint8_t *name, uint8_t length)
5681 +{
5682 + dwc_cc_t* cc;
5683 +
5684 + DWC_DEBUGC("Change connection context %d", id);
5685 +
5686 + DWC_MUTEX_LOCK(cc_if->mutex);
5687 + cc = cc_find(cc_if, id);
5688 + if (!cc) {
5689 + DWC_ERROR("Uid %d not found in cc list\n", id);
5690 + DWC_MUTEX_UNLOCK(cc_if->mutex);
5691 + return;
5692 + }
5693 +
5694 + if (chid) {
5695 + DWC_MEMCPY(&(cc->chid[0]), chid, 16);
5696 + }
5697 + if (cdid) {
5698 + DWC_MEMCPY(&(cc->cdid[0]), cdid, 16);
5699 + }
5700 + if (ck) {
5701 + DWC_MEMCPY(&(cc->ck[0]), ck, 16);
5702 + }
5703 +
5704 + if (name) {
5705 + if (cc->name) {
5706 + dwc_free(mem_ctx, cc->name);
5707 + }
5708 + cc->name = dwc_alloc(mem_ctx, length);
5709 + if (!cc->name) {
5710 + DWC_ERROR("Out of memory in dwc_cc_change()\n");
5711 + DWC_MUTEX_UNLOCK(cc_if->mutex);
5712 + return;
5713 + }
5714 + cc->length = length;
5715 + DWC_MEMCPY(cc->name, name, length);
5716 + }
5717 +
5718 + DWC_MUTEX_UNLOCK(cc_if->mutex);
5719 +
5720 + cc_changed(cc_if);
5721 +
5722 + DWC_DEBUGC("Changed connection context id=%d\n", id);
5723 + dump_bytes("New CHID", cc->chid, 16);
5724 + dump_bytes("New CDID", cc->cdid, 16);
5725 + dump_bytes("New CK", cc->ck, 16);
5726 +}
5727 +
5728 +void dwc_cc_remove(void *mem_ctx, dwc_cc_if_t *cc_if, int32_t id)
5729 +{
5730 + dwc_cc_t *cc;
5731 +
5732 + DWC_DEBUGC("Removing connection context %d", id);
5733 +
5734 + DWC_MUTEX_LOCK(cc_if->mutex);
5735 + cc = cc_find(cc_if, id);
5736 + if (!cc) {
5737 + DWC_ERROR("Uid %d not found in cc list\n", id);
5738 + DWC_MUTEX_UNLOCK(cc_if->mutex);
5739 + return;
5740 + }
5741 +
5742 + DWC_CIRCLEQ_REMOVE_INIT(&cc_if->list, cc, list_entry);
5743 + DWC_MUTEX_UNLOCK(cc_if->mutex);
5744 + free_cc(mem_ctx, cc);
5745 +
5746 + cc_changed(cc_if);
5747 +}
5748 +
5749 +uint8_t *dwc_cc_data_for_save(void *mem_ctx, dwc_cc_if_t *cc_if, unsigned int *length)
5750 +{
5751 + uint8_t *buf, *x;
5752 + uint8_t zero = 0;
5753 + dwc_cc_t *cc;
5754 +
5755 + DWC_MUTEX_LOCK(cc_if->mutex);
5756 + *length = cc_data_size(cc_if);
5757 + if (!(*length)) {
5758 + DWC_MUTEX_UNLOCK(cc_if->mutex);
5759 + return NULL;
5760 + }
5761 +
5762 + DWC_DEBUGC("Creating data for saving (length=%d)", *length);
5763 +
5764 + buf = dwc_alloc(mem_ctx, *length);
5765 + if (!buf) {
5766 + *length = 0;
5767 + DWC_MUTEX_UNLOCK(cc_if->mutex);
5768 + return NULL;
5769 + }
5770 +
5771 + x = buf;
5772 + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
5773 + DWC_MEMCPY(x, cc->chid, 16);
5774 + x += 16;
5775 + DWC_MEMCPY(x, cc->cdid, 16);
5776 + x += 16;
5777 + DWC_MEMCPY(x, cc->ck, 16);
5778 + x += 16;
5779 + if (cc->name) {
5780 + DWC_MEMCPY(x, &cc->length, 1);
5781 + x += 1;
5782 + DWC_MEMCPY(x, cc->name, cc->length);
5783 + x += cc->length;
5784 + }
5785 + else {
5786 + DWC_MEMCPY(x, &zero, 1);
5787 + x += 1;
5788 + }
5789 + }
5790 + DWC_MUTEX_UNLOCK(cc_if->mutex);
5791 +
5792 + return buf;
5793 +}
5794 +
5795 +void dwc_cc_restore_from_data(void *mem_ctx, dwc_cc_if_t *cc_if, uint8_t *data, uint32_t length)
5796 +{
5797 + uint8_t name_length;
5798 + uint8_t *name;
5799 + uint8_t *chid;
5800 + uint8_t *cdid;
5801 + uint8_t *ck;
5802 + uint32_t i = 0;
5803 +
5804 + DWC_MUTEX_LOCK(cc_if->mutex);
5805 + cc_clear(mem_ctx, cc_if);
5806 +
5807 + while (i < length) {
5808 + chid = &data[i];
5809 + i += 16;
5810 + cdid = &data[i];
5811 + i += 16;
5812 + ck = &data[i];
5813 + i += 16;
5814 +
5815 + name_length = data[i];
5816 + i ++;
5817 +
5818 + if (name_length) {
5819 + name = &data[i];
5820 + i += name_length;
5821 + }
5822 + else {
5823 + name = NULL;
5824 + }
5825 +
5826 + /* check to see if we haven't overflown the buffer */
5827 + if (i > length) {
5828 + DWC_ERROR("Data format error while attempting to load CCs "
5829 + "(nlen=%d, iter=%d, buflen=%d).\n", name_length, i, length);
5830 + break;
5831 + }
5832 +
5833 + cc_add(mem_ctx, cc_if, chid, cdid, ck, name, name_length);
5834 + }
5835 + DWC_MUTEX_UNLOCK(cc_if->mutex);
5836 +
5837 + cc_changed(cc_if);
5838 +}
5839 +
5840 +uint32_t dwc_cc_match_chid(dwc_cc_if_t *cc_if, uint8_t *chid)
5841 +{
5842 + uint32_t uid = 0;
5843 +
5844 + DWC_MUTEX_LOCK(cc_if->mutex);
5845 + uid = cc_match_chid(cc_if, chid);
5846 + DWC_MUTEX_UNLOCK(cc_if->mutex);
5847 + return uid;
5848 +}
5849 +uint32_t dwc_cc_match_cdid(dwc_cc_if_t *cc_if, uint8_t *cdid)
5850 +{
5851 + uint32_t uid = 0;
5852 +
5853 + DWC_MUTEX_LOCK(cc_if->mutex);
5854 + uid = cc_match_cdid(cc_if, cdid);
5855 + DWC_MUTEX_UNLOCK(cc_if->mutex);
5856 + return uid;
5857 +}
5858 +
5859 +uint8_t *dwc_cc_ck(dwc_cc_if_t *cc_if, int32_t id)
5860 +{
5861 + uint8_t *ck = NULL;
5862 + dwc_cc_t *cc;
5863 +
5864 + DWC_MUTEX_LOCK(cc_if->mutex);
5865 + cc = cc_find(cc_if, id);
5866 + if (cc) {
5867 + ck = cc->ck;
5868 + }
5869 + DWC_MUTEX_UNLOCK(cc_if->mutex);
5870 +
5871 + return ck;
5872 +
5873 +}
5874 +
5875 +uint8_t *dwc_cc_chid(dwc_cc_if_t *cc_if, int32_t id)
5876 +{
5877 + uint8_t *retval = NULL;
5878 + dwc_cc_t *cc;
5879 +
5880 + DWC_MUTEX_LOCK(cc_if->mutex);
5881 + cc = cc_find(cc_if, id);
5882 + if (cc) {
5883 + retval = cc->chid;
5884 + }
5885 + DWC_MUTEX_UNLOCK(cc_if->mutex);
5886 +
5887 + return retval;
5888 +}
5889 +
5890 +uint8_t *dwc_cc_cdid(dwc_cc_if_t *cc_if, int32_t id)
5891 +{
5892 + uint8_t *retval = NULL;
5893 + dwc_cc_t *cc;
5894 +
5895 + DWC_MUTEX_LOCK(cc_if->mutex);
5896 + cc = cc_find(cc_if, id);
5897 + if (cc) {
5898 + retval = cc->cdid;
5899 + }
5900 + DWC_MUTEX_UNLOCK(cc_if->mutex);
5901 +
5902 + return retval;
5903 +}
5904 +
5905 +uint8_t *dwc_cc_name(dwc_cc_if_t *cc_if, int32_t id, uint8_t *length)
5906 +{
5907 + uint8_t *retval = NULL;
5908 + dwc_cc_t *cc;
5909 +
5910 + DWC_MUTEX_LOCK(cc_if->mutex);
5911 + *length = 0;
5912 + cc = cc_find(cc_if, id);
5913 + if (cc) {
5914 + *length = cc->length;
5915 + retval = cc->name;
5916 + }
5917 + DWC_MUTEX_UNLOCK(cc_if->mutex);
5918 +
5919 + return retval;
5920 +}
5921 +
5922 +#endif /* DWC_CCLIB */
5923 --- /dev/null
5924 +++ b/drivers/usb/host/dwc_common_port/dwc_cc.h
5925 @@ -0,0 +1,224 @@
5926 +/* =========================================================================
5927 + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_cc.h $
5928 + * $Revision: #4 $
5929 + * $Date: 2010/09/28 $
5930 + * $Change: 1596182 $
5931 + *
5932 + * Synopsys Portability Library Software and documentation
5933 + * (hereinafter, "Software") is an Unsupported proprietary work of
5934 + * Synopsys, Inc. unless otherwise expressly agreed to in writing
5935 + * between Synopsys and you.
5936 + *
5937 + * The Software IS NOT an item of Licensed Software or Licensed Product
5938 + * under any End User Software License Agreement or Agreement for
5939 + * Licensed Product with Synopsys or any supplement thereto. You are
5940 + * permitted to use and redistribute this Software in source and binary
5941 + * forms, with or without modification, provided that redistributions
5942 + * of source code must retain this notice. You may not view, use,
5943 + * disclose, copy or distribute this file or any information contained
5944 + * herein except pursuant to this license grant from Synopsys. If you
5945 + * do not agree with this notice, including the disclaimer below, then
5946 + * you are not authorized to use the Software.
5947 + *
5948 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
5949 + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5950 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
5951 + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
5952 + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
5953 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
5954 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
5955 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
5956 + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
5957 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
5958 + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
5959 + * DAMAGE.
5960 + * ========================================================================= */
5961 +#ifndef _DWC_CC_H_
5962 +#define _DWC_CC_H_
5963 +
5964 +#ifdef __cplusplus
5965 +extern "C" {
5966 +#endif
5967 +
5968 +/** @file
5969 + *
5970 + * This file defines the Context Context library.
5971 + *
5972 + * The main data structure is dwc_cc_if_t which is returned by either the
5973 + * dwc_cc_if_alloc function or returned by the module to the user via a provided
5974 + * function. The data structure is opaque and should only be manipulated via the
5975 + * functions provied in this API.
5976 + *
5977 + * It manages a list of connection contexts and operations can be performed to
5978 + * add, remove, query, search, and change, those contexts. Additionally,
5979 + * a dwc_notifier_t object can be requested from the manager so that
5980 + * the user can be notified whenever the context list has changed.
5981 + */
5982 +
5983 +#include "dwc_os.h"
5984 +#include "dwc_list.h"
5985 +#include "dwc_notifier.h"
5986 +
5987 +
5988 +/* Notifications */
5989 +#define DWC_CC_LIST_CHANGED_NOTIFICATION "DWC_CC_LIST_CHANGED_NOTIFICATION"
5990 +
5991 +struct dwc_cc_if;
5992 +typedef struct dwc_cc_if dwc_cc_if_t;
5993 +
5994 +
5995 +/** @name Connection Context Operations */
5996 +/** @{ */
5997 +
5998 +/** This function allocates memory for a dwc_cc_if_t structure, initializes
5999 + * fields to default values, and returns a pointer to the structure or NULL on
6000 + * error. */
6001 +extern dwc_cc_if_t *dwc_cc_if_alloc(void *mem_ctx, void *mtx_ctx,
6002 + dwc_notifier_t *notifier, unsigned is_host);
6003 +
6004 +/** Frees the memory for the specified CC structure allocated from
6005 + * dwc_cc_if_alloc(). */
6006 +extern void dwc_cc_if_free(void *mem_ctx, void *mtx_ctx, dwc_cc_if_t *cc_if);
6007 +
6008 +/** Removes all contexts from the connection context list */
6009 +extern void dwc_cc_clear(void *mem_ctx, dwc_cc_if_t *cc_if);
6010 +
6011 +/** Adds a connection context (CHID, CK, CDID, Name) to the connection context list.
6012 + * If a CHID already exists, the CK and name are overwritten. Statistics are
6013 + * not overwritten.
6014 + *
6015 + * @param cc_if The cc_if structure.
6016 + * @param chid A pointer to the 16-byte CHID. This value will be copied.
6017 + * @param ck A pointer to the 16-byte CK. This value will be copied.
6018 + * @param cdid A pointer to the 16-byte CDID. This value will be copied.
6019 + * @param name An optional host friendly name as defined in the association model
6020 + * spec. Must be a UTF16-LE unicode string. Can be NULL to indicated no name.
6021 + * @param length The length othe unicode string.
6022 + * @return A unique identifier used to refer to this context that is valid for
6023 + * as long as this context is still in the list. */
6024 +extern int32_t dwc_cc_add(void *mem_ctx, dwc_cc_if_t *cc_if, uint8_t *chid,
6025 + uint8_t *cdid, uint8_t *ck, uint8_t *name,
6026 + uint8_t length);
6027 +
6028 +/** Changes the CHID, CK, CDID, or Name values of a connection context in the
6029 + * list, preserving any accumulated statistics. This would typically be called
6030 + * if the host decideds to change the context with a SET_CONNECTION request.
6031 + *
6032 + * @param cc_if The cc_if structure.
6033 + * @param id The identifier of the connection context.
6034 + * @param chid A pointer to the 16-byte CHID. This value will be copied. NULL
6035 + * indicates no change.
6036 + * @param cdid A pointer to the 16-byte CDID. This value will be copied. NULL
6037 + * indicates no change.
6038 + * @param ck A pointer to the 16-byte CK. This value will be copied. NULL
6039 + * indicates no change.
6040 + * @param name Host friendly name UTF16-LE. NULL indicates no change.
6041 + * @param length Length of name. */
6042 +extern void dwc_cc_change(void *mem_ctx, dwc_cc_if_t *cc_if, int32_t id,
6043 + uint8_t *chid, uint8_t *cdid, uint8_t *ck,
6044 + uint8_t *name, uint8_t length);
6045 +
6046 +/** Remove the specified connection context.
6047 + * @param cc_if The cc_if structure.
6048 + * @param id The identifier of the connection context to remove. */
6049 +extern void dwc_cc_remove(void *mem_ctx, dwc_cc_if_t *cc_if, int32_t id);
6050 +
6051 +/** Get a binary block of data for the connection context list and attributes.
6052 + * This data can be used by the OS specific driver to save the connection
6053 + * context list into non-volatile memory.
6054 + *
6055 + * @param cc_if The cc_if structure.
6056 + * @param length Return the length of the data buffer.
6057 + * @return A pointer to the data buffer. The memory for this buffer should be
6058 + * freed with DWC_FREE() after use. */
6059 +extern uint8_t *dwc_cc_data_for_save(void *mem_ctx, dwc_cc_if_t *cc_if,
6060 + unsigned int *length);
6061 +
6062 +/** Restore the connection context list from the binary data that was previously
6063 + * returned from a call to dwc_cc_data_for_save. This can be used by the OS specific
6064 + * driver to load a connection context list from non-volatile memory.
6065 + *
6066 + * @param cc_if The cc_if structure.
6067 + * @param data The data bytes as returned from dwc_cc_data_for_save.
6068 + * @param length The length of the data. */
6069 +extern void dwc_cc_restore_from_data(void *mem_ctx, dwc_cc_if_t *cc_if,
6070 + uint8_t *data, unsigned int length);
6071 +
6072 +/** Find the connection context from the specified CHID.
6073 + *
6074 + * @param cc_if The cc_if structure.
6075 + * @param chid A pointer to the CHID data.
6076 + * @return A non-zero identifier of the connection context if the CHID matches.
6077 + * Otherwise returns 0. */
6078 +extern uint32_t dwc_cc_match_chid(dwc_cc_if_t *cc_if, uint8_t *chid);
6079 +
6080 +/** Find the connection context from the specified CDID.
6081 + *
6082 + * @param cc_if The cc_if structure.
6083 + * @param cdid A pointer to the CDID data.
6084 + * @return A non-zero identifier of the connection context if the CHID matches.
6085 + * Otherwise returns 0. */
6086 +extern uint32_t dwc_cc_match_cdid(dwc_cc_if_t *cc_if, uint8_t *cdid);
6087 +
6088 +/** Retrieve the CK from the specified connection context.
6089 + *
6090 + * @param cc_if The cc_if structure.
6091 + * @param id The identifier of the connection context.
6092 + * @return A pointer to the CK data. The memory does not need to be freed. */
6093 +extern uint8_t *dwc_cc_ck(dwc_cc_if_t *cc_if, int32_t id);
6094 +
6095 +/** Retrieve the CHID from the specified connection context.
6096 + *
6097 + * @param cc_if The cc_if structure.
6098 + * @param id The identifier of the connection context.
6099 + * @return A pointer to the CHID data. The memory does not need to be freed. */
6100 +extern uint8_t *dwc_cc_chid(dwc_cc_if_t *cc_if, int32_t id);
6101 +
6102 +/** Retrieve the CDID from the specified connection context.
6103 + *
6104 + * @param cc_if The cc_if structure.
6105 + * @param id The identifier of the connection context.
6106 + * @return A pointer to the CDID data. The memory does not need to be freed. */
6107 +extern uint8_t *dwc_cc_cdid(dwc_cc_if_t *cc_if, int32_t id);
6108 +
6109 +extern uint8_t *dwc_cc_name(dwc_cc_if_t *cc_if, int32_t id, uint8_t *length);
6110 +
6111 +/** Checks a buffer for non-zero.
6112 + * @param id A pointer to a 16 byte buffer.
6113 + * @return true if the 16 byte value is non-zero. */
6114 +static inline unsigned dwc_assoc_is_not_zero_id(uint8_t *id) {
6115 + int i;
6116 + for (i=0; i<16; i++) {
6117 + if (id[i]) return 1;
6118 + }
6119 + return 0;
6120 +}
6121 +
6122 +/** Checks a buffer for zero.
6123 + * @param id A pointer to a 16 byte buffer.
6124 + * @return true if the 16 byte value is zero. */
6125 +static inline unsigned dwc_assoc_is_zero_id(uint8_t *id) {
6126 + return !dwc_assoc_is_not_zero_id(id);
6127 +}
6128 +
6129 +/** Prints an ASCII representation for the 16-byte chid, cdid, or ck, into
6130 + * buffer. */
6131 +static inline int dwc_print_id_string(char *buffer, uint8_t *id) {
6132 + char *ptr = buffer;
6133 + int i;
6134 + for (i=0; i<16; i++) {
6135 + ptr += DWC_SPRINTF(ptr, "%02x", id[i]);
6136 + if (i < 15) {
6137 + ptr += DWC_SPRINTF(ptr, " ");
6138 + }
6139 + }
6140 + return ptr - buffer;
6141 +}
6142 +
6143 +/** @} */
6144 +
6145 +#ifdef __cplusplus
6146 +}
6147 +#endif
6148 +
6149 +#endif /* _DWC_CC_H_ */
6150 --- /dev/null
6151 +++ b/drivers/usb/host/dwc_common_port/dwc_common_fbsd.c
6152 @@ -0,0 +1,1308 @@
6153 +#include "dwc_os.h"
6154 +#include "dwc_list.h"
6155 +
6156 +#ifdef DWC_CCLIB
6157 +# include "dwc_cc.h"
6158 +#endif
6159 +
6160 +#ifdef DWC_CRYPTOLIB
6161 +# include "dwc_modpow.h"
6162 +# include "dwc_dh.h"
6163 +# include "dwc_crypto.h"
6164 +#endif
6165 +
6166 +#ifdef DWC_NOTIFYLIB
6167 +# include "dwc_notifier.h"
6168 +#endif
6169 +
6170 +/* OS-Level Implementations */
6171 +
6172 +/* This is the FreeBSD 7.0 kernel implementation of the DWC platform library. */
6173 +
6174 +
6175 +/* MISC */
6176 +
6177 +void *DWC_MEMSET(void *dest, uint8_t byte, uint32_t size)
6178 +{
6179 + return memset(dest, byte, size);
6180 +}
6181 +
6182 +void *DWC_MEMCPY(void *dest, void const *src, uint32_t size)
6183 +{
6184 + return memcpy(dest, src, size);
6185 +}
6186 +
6187 +void *DWC_MEMMOVE(void *dest, void *src, uint32_t size)
6188 +{
6189 + bcopy(src, dest, size);
6190 + return dest;
6191 +}
6192 +
6193 +int DWC_MEMCMP(void *m1, void *m2, uint32_t size)
6194 +{
6195 + return memcmp(m1, m2, size);
6196 +}
6197 +
6198 +int DWC_STRNCMP(void *s1, void *s2, uint32_t size)
6199 +{
6200 + return strncmp(s1, s2, size);
6201 +}
6202 +
6203 +int DWC_STRCMP(void *s1, void *s2)
6204 +{
6205 + return strcmp(s1, s2);
6206 +}
6207 +
6208 +int DWC_STRLEN(char const *str)
6209 +{
6210 + return strlen(str);
6211 +}
6212 +
6213 +char *DWC_STRCPY(char *to, char const *from)
6214 +{
6215 + return strcpy(to, from);
6216 +}
6217 +
6218 +char *DWC_STRDUP(char const *str)
6219 +{
6220 + int len = DWC_STRLEN(str) + 1;
6221 + char *new = DWC_ALLOC_ATOMIC(len);
6222 +
6223 + if (!new) {
6224 + return NULL;
6225 + }
6226 +
6227 + DWC_MEMCPY(new, str, len);
6228 + return new;
6229 +}
6230 +
6231 +int DWC_ATOI(char *str, int32_t *value)
6232 +{
6233 + char *end = NULL;
6234 +
6235 + *value = strtol(str, &end, 0);
6236 + if (*end == '\0') {
6237 + return 0;
6238 + }
6239 +
6240 + return -1;
6241 +}
6242 +
6243 +int DWC_ATOUI(char *str, uint32_t *value)
6244 +{
6245 + char *end = NULL;
6246 +
6247 + *value = strtoul(str, &end, 0);
6248 + if (*end == '\0') {
6249 + return 0;
6250 + }
6251 +
6252 + return -1;
6253 +}
6254 +
6255 +
6256 +#ifdef DWC_UTFLIB
6257 +/* From usbstring.c */
6258 +
6259 +int DWC_UTF8_TO_UTF16LE(uint8_t const *s, uint16_t *cp, unsigned len)
6260 +{
6261 + int count = 0;
6262 + u8 c;
6263 + u16 uchar;
6264 +
6265 + /* this insists on correct encodings, though not minimal ones.
6266 + * BUT it currently rejects legit 4-byte UTF-8 code points,
6267 + * which need surrogate pairs. (Unicode 3.1 can use them.)
6268 + */
6269 + while (len != 0 && (c = (u8) *s++) != 0) {
6270 + if (unlikely(c & 0x80)) {
6271 + // 2-byte sequence:
6272 + // 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx
6273 + if ((c & 0xe0) == 0xc0) {
6274 + uchar = (c & 0x1f) << 6;
6275 +
6276 + c = (u8) *s++;
6277 + if ((c & 0xc0) != 0xc0)
6278 + goto fail;
6279 + c &= 0x3f;
6280 + uchar |= c;
6281 +
6282 + // 3-byte sequence (most CJKV characters):
6283 + // zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx
6284 + } else if ((c & 0xf0) == 0xe0) {
6285 + uchar = (c & 0x0f) << 12;
6286 +
6287 + c = (u8) *s++;
6288 + if ((c & 0xc0) != 0xc0)
6289 + goto fail;
6290 + c &= 0x3f;
6291 + uchar |= c << 6;
6292 +
6293 + c = (u8) *s++;
6294 + if ((c & 0xc0) != 0xc0)
6295 + goto fail;
6296 + c &= 0x3f;
6297 + uchar |= c;
6298 +
6299 + /* no bogus surrogates */
6300 + if (0xd800 <= uchar && uchar <= 0xdfff)
6301 + goto fail;
6302 +
6303 + // 4-byte sequence (surrogate pairs, currently rare):
6304 + // 11101110wwwwzzzzyy + 110111yyyyxxxxxx
6305 + // = 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx
6306 + // (uuuuu = wwww + 1)
6307 + // FIXME accept the surrogate code points (only)
6308 + } else
6309 + goto fail;
6310 + } else
6311 + uchar = c;
6312 + put_unaligned (cpu_to_le16 (uchar), cp++);
6313 + count++;
6314 + len--;
6315 + }
6316 + return count;
6317 +fail:
6318 + return -1;
6319 +}
6320 +
6321 +#endif /* DWC_UTFLIB */
6322 +
6323 +
6324 +/* dwc_debug.h */
6325 +
6326 +dwc_bool_t DWC_IN_IRQ(void)
6327 +{
6328 +// return in_irq();
6329 + return 0;
6330 +}
6331 +
6332 +dwc_bool_t DWC_IN_BH(void)
6333 +{
6334 +// return in_softirq();
6335 + return 0;
6336 +}
6337 +
6338 +void DWC_VPRINTF(char *format, va_list args)
6339 +{
6340 + vprintf(format, args);
6341 +}
6342 +
6343 +int DWC_VSNPRINTF(char *str, int size, char *format, va_list args)
6344 +{
6345 + return vsnprintf(str, size, format, args);
6346 +}
6347 +
6348 +void DWC_PRINTF(char *format, ...)
6349 +{
6350 + va_list args;
6351 +
6352 + va_start(args, format);
6353 + DWC_VPRINTF(format, args);
6354 + va_end(args);
6355 +}
6356 +
6357 +int DWC_SPRINTF(char *buffer, char *format, ...)
6358 +{
6359 + int retval;
6360 + va_list args;
6361 +
6362 + va_start(args, format);
6363 + retval = vsprintf(buffer, format, args);
6364 + va_end(args);
6365 + return retval;
6366 +}
6367 +
6368 +int DWC_SNPRINTF(char *buffer, int size, char *format, ...)
6369 +{
6370 + int retval;
6371 + va_list args;
6372 +
6373 + va_start(args, format);
6374 + retval = vsnprintf(buffer, size, format, args);
6375 + va_end(args);
6376 + return retval;
6377 +}
6378 +
6379 +void __DWC_WARN(char *format, ...)
6380 +{
6381 + va_list args;
6382 +
6383 + va_start(args, format);
6384 + DWC_VPRINTF(format, args);
6385 + va_end(args);
6386 +}
6387 +
6388 +void __DWC_ERROR(char *format, ...)
6389 +{
6390 + va_list args;
6391 +
6392 + va_start(args, format);
6393 + DWC_VPRINTF(format, args);
6394 + va_end(args);
6395 +}
6396 +
6397 +void DWC_EXCEPTION(char *format, ...)
6398 +{
6399 + va_list args;
6400 +
6401 + va_start(args, format);
6402 + DWC_VPRINTF(format, args);
6403 + va_end(args);
6404 +// BUG_ON(1); ???
6405 +}
6406 +
6407 +#ifdef DEBUG
6408 +void __DWC_DEBUG(char *format, ...)
6409 +{
6410 + va_list args;
6411 +
6412 + va_start(args, format);
6413 + DWC_VPRINTF(format, args);
6414 + va_end(args);
6415 +}
6416 +#endif
6417 +
6418 +
6419 +/* dwc_mem.h */
6420 +
6421 +#if 0
6422 +dwc_pool_t *DWC_DMA_POOL_CREATE(uint32_t size,
6423 + uint32_t align,
6424 + uint32_t alloc)
6425 +{
6426 + struct dma_pool *pool = dma_pool_create("Pool", NULL,
6427 + size, align, alloc);
6428 + return (dwc_pool_t *)pool;
6429 +}
6430 +
6431 +void DWC_DMA_POOL_DESTROY(dwc_pool_t *pool)
6432 +{
6433 + dma_pool_destroy((struct dma_pool *)pool);
6434 +}
6435 +
6436 +void *DWC_DMA_POOL_ALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
6437 +{
6438 +// return dma_pool_alloc((struct dma_pool *)pool, GFP_KERNEL, dma_addr);
6439 + return dma_pool_alloc((struct dma_pool *)pool, M_WAITOK, dma_addr);
6440 +}
6441 +
6442 +void *DWC_DMA_POOL_ZALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
6443 +{
6444 + void *vaddr = DWC_DMA_POOL_ALLOC(pool, dma_addr);
6445 + memset(..);
6446 +}
6447 +
6448 +void DWC_DMA_POOL_FREE(dwc_pool_t *pool, void *vaddr, void *daddr)
6449 +{
6450 + dma_pool_free(pool, vaddr, daddr);
6451 +}
6452 +#endif
6453 +
6454 +static void dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
6455 +{
6456 + if (error)
6457 + return;
6458 + *(bus_addr_t *)arg = segs[0].ds_addr;
6459 +}
6460 +
6461 +void *__DWC_DMA_ALLOC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr)
6462 +{
6463 + dwc_dmactx_t *dma = (dwc_dmactx_t *)dma_ctx;
6464 + int error;
6465 +
6466 + error = bus_dma_tag_create(
6467 +#if __FreeBSD_version >= 700000
6468 + bus_get_dma_tag(dma->dev), /* parent */
6469 +#else
6470 + NULL, /* parent */
6471 +#endif
6472 + 4, 0, /* alignment, bounds */
6473 + BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
6474 + BUS_SPACE_MAXADDR, /* highaddr */
6475 + NULL, NULL, /* filter, filterarg */
6476 + size, /* maxsize */
6477 + 1, /* nsegments */
6478 + size, /* maxsegsize */
6479 + 0, /* flags */
6480 + NULL, /* lockfunc */
6481 + NULL, /* lockarg */
6482 + &dma->dma_tag);
6483 + if (error) {
6484 + device_printf(dma->dev, "%s: bus_dma_tag_create failed: %d\n",
6485 + __func__, error);
6486 + goto fail_0;
6487 + }
6488 +
6489 + error = bus_dmamem_alloc(dma->dma_tag, &dma->dma_vaddr,
6490 + BUS_DMA_NOWAIT | BUS_DMA_COHERENT, &dma->dma_map);
6491 + if (error) {
6492 + device_printf(dma->dev, "%s: bus_dmamem_alloc(%ju) failed: %d\n",
6493 + __func__, (uintmax_t)size, error);
6494 + goto fail_1;
6495 + }
6496 +
6497 + dma->dma_paddr = 0;
6498 + error = bus_dmamap_load(dma->dma_tag, dma->dma_map, dma->dma_vaddr, size,
6499 + dmamap_cb, &dma->dma_paddr, BUS_DMA_NOWAIT);
6500 + if (error || dma->dma_paddr == 0) {
6501 + device_printf(dma->dev, "%s: bus_dmamap_load failed: %d\n",
6502 + __func__, error);
6503 + goto fail_2;
6504 + }
6505 +
6506 + *dma_addr = dma->dma_paddr;
6507 + return dma->dma_vaddr;
6508 +
6509 +fail_2:
6510 + bus_dmamap_unload(dma->dma_tag, dma->dma_map);
6511 +fail_1:
6512 + bus_dmamem_free(dma->dma_tag, dma->dma_vaddr, dma->dma_map);
6513 + bus_dma_tag_destroy(dma->dma_tag);
6514 +fail_0:
6515 + dma->dma_map = NULL;
6516 + dma->dma_tag = NULL;
6517 +
6518 + return NULL;
6519 +}
6520 +
6521 +void __DWC_DMA_FREE(void *dma_ctx, uint32_t size, void *virt_addr, dwc_dma_t dma_addr)
6522 +{
6523 + dwc_dmactx_t *dma = (dwc_dmactx_t *)dma_ctx;
6524 +
6525 + if (dma->dma_tag == NULL)
6526 + return;
6527 + if (dma->dma_map != NULL) {
6528 + bus_dmamap_sync(dma->dma_tag, dma->dma_map,
6529 + BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
6530 + bus_dmamap_unload(dma->dma_tag, dma->dma_map);
6531 + bus_dmamem_free(dma->dma_tag, dma->dma_vaddr, dma->dma_map);
6532 + dma->dma_map = NULL;
6533 + }
6534 +
6535 + bus_dma_tag_destroy(dma->dma_tag);
6536 + dma->dma_tag = NULL;
6537 +}
6538 +
6539 +void *__DWC_ALLOC(void *mem_ctx, uint32_t size)
6540 +{
6541 + return malloc(size, M_DEVBUF, M_WAITOK | M_ZERO);
6542 +}
6543 +
6544 +void *__DWC_ALLOC_ATOMIC(void *mem_ctx, uint32_t size)
6545 +{
6546 + return malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO);
6547 +}
6548 +
6549 +void __DWC_FREE(void *mem_ctx, void *addr)
6550 +{
6551 + free(addr, M_DEVBUF);
6552 +}
6553 +
6554 +
6555 +#ifdef DWC_CRYPTOLIB
6556 +/* dwc_crypto.h */
6557 +
6558 +void DWC_RANDOM_BYTES(uint8_t *buffer, uint32_t length)
6559 +{
6560 + get_random_bytes(buffer, length);
6561 +}
6562 +
6563 +int DWC_AES_CBC(uint8_t *message, uint32_t messagelen, uint8_t *key, uint32_t keylen, uint8_t iv[16], uint8_t *out)
6564 +{
6565 + struct crypto_blkcipher *tfm;
6566 + struct blkcipher_desc desc;
6567 + struct scatterlist sgd;
6568 + struct scatterlist sgs;
6569 +
6570 + tfm = crypto_alloc_blkcipher("cbc(aes)", 0, CRYPTO_ALG_ASYNC);
6571 + if (tfm == NULL) {
6572 + printk("failed to load transform for aes CBC\n");
6573 + return -1;
6574 + }
6575 +
6576 + crypto_blkcipher_setkey(tfm, key, keylen);
6577 + crypto_blkcipher_set_iv(tfm, iv, 16);
6578 +
6579 + sg_init_one(&sgd, out, messagelen);
6580 + sg_init_one(&sgs, message, messagelen);
6581 +
6582 + desc.tfm = tfm;
6583 + desc.flags = 0;
6584 +
6585 + if (crypto_blkcipher_encrypt(&desc, &sgd, &sgs, messagelen)) {
6586 + crypto_free_blkcipher(tfm);
6587 + DWC_ERROR("AES CBC encryption failed");
6588 + return -1;
6589 + }
6590 +
6591 + crypto_free_blkcipher(tfm);
6592 + return 0;
6593 +}
6594 +
6595 +int DWC_SHA256(uint8_t *message, uint32_t len, uint8_t *out)
6596 +{
6597 + struct crypto_hash *tfm;
6598 + struct hash_desc desc;
6599 + struct scatterlist sg;
6600 +
6601 + tfm = crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC);
6602 + if (IS_ERR(tfm)) {
6603 + DWC_ERROR("Failed to load transform for sha256: %ld", PTR_ERR(tfm));
6604 + return 0;
6605 + }
6606 + desc.tfm = tfm;
6607 + desc.flags = 0;
6608 +
6609 + sg_init_one(&sg, message, len);
6610 + crypto_hash_digest(&desc, &sg, len, out);
6611 + crypto_free_hash(tfm);
6612 +
6613 + return 1;
6614 +}
6615 +
6616 +int DWC_HMAC_SHA256(uint8_t *message, uint32_t messagelen,
6617 + uint8_t *key, uint32_t keylen, uint8_t *out)
6618 +{
6619 + struct crypto_hash *tfm;
6620 + struct hash_desc desc;
6621 + struct scatterlist sg;
6622 +
6623 + tfm = crypto_alloc_hash("hmac(sha256)", 0, CRYPTO_ALG_ASYNC);
6624 + if (IS_ERR(tfm)) {
6625 + DWC_ERROR("Failed to load transform for hmac(sha256): %ld", PTR_ERR(tfm));
6626 + return 0;
6627 + }
6628 + desc.tfm = tfm;
6629 + desc.flags = 0;
6630 +
6631 + sg_init_one(&sg, message, messagelen);
6632 + crypto_hash_setkey(tfm, key, keylen);
6633 + crypto_hash_digest(&desc, &sg, messagelen, out);
6634 + crypto_free_hash(tfm);
6635 +
6636 + return 1;
6637 +}
6638 +
6639 +#endif /* DWC_CRYPTOLIB */
6640 +
6641 +
6642 +/* Byte Ordering Conversions */
6643 +
6644 +uint32_t DWC_CPU_TO_LE32(uint32_t *p)
6645 +{
6646 +#ifdef __LITTLE_ENDIAN
6647 + return *p;
6648 +#else
6649 + uint8_t *u_p = (uint8_t *)p;
6650 +
6651 + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
6652 +#endif
6653 +}
6654 +
6655 +uint32_t DWC_CPU_TO_BE32(uint32_t *p)
6656 +{
6657 +#ifdef __BIG_ENDIAN
6658 + return *p;
6659 +#else
6660 + uint8_t *u_p = (uint8_t *)p;
6661 +
6662 + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
6663 +#endif
6664 +}
6665 +
6666 +uint32_t DWC_LE32_TO_CPU(uint32_t *p)
6667 +{
6668 +#ifdef __LITTLE_ENDIAN
6669 + return *p;
6670 +#else
6671 + uint8_t *u_p = (uint8_t *)p;
6672 +
6673 + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
6674 +#endif
6675 +}
6676 +
6677 +uint32_t DWC_BE32_TO_CPU(uint32_t *p)
6678 +{
6679 +#ifdef __BIG_ENDIAN
6680 + return *p;
6681 +#else
6682 + uint8_t *u_p = (uint8_t *)p;
6683 +
6684 + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
6685 +#endif
6686 +}
6687 +
6688 +uint16_t DWC_CPU_TO_LE16(uint16_t *p)
6689 +{
6690 +#ifdef __LITTLE_ENDIAN
6691 + return *p;
6692 +#else
6693 + uint8_t *u_p = (uint8_t *)p;
6694 + return (u_p[1] | (u_p[0] << 8));
6695 +#endif
6696 +}
6697 +
6698 +uint16_t DWC_CPU_TO_BE16(uint16_t *p)
6699 +{
6700 +#ifdef __BIG_ENDIAN
6701 + return *p;
6702 +#else
6703 + uint8_t *u_p = (uint8_t *)p;
6704 + return (u_p[1] | (u_p[0] << 8));
6705 +#endif
6706 +}
6707 +
6708 +uint16_t DWC_LE16_TO_CPU(uint16_t *p)
6709 +{
6710 +#ifdef __LITTLE_ENDIAN
6711 + return *p;
6712 +#else
6713 + uint8_t *u_p = (uint8_t *)p;
6714 + return (u_p[1] | (u_p[0] << 8));
6715 +#endif
6716 +}
6717 +
6718 +uint16_t DWC_BE16_TO_CPU(uint16_t *p)
6719 +{
6720 +#ifdef __BIG_ENDIAN
6721 + return *p;
6722 +#else
6723 + uint8_t *u_p = (uint8_t *)p;
6724 + return (u_p[1] | (u_p[0] << 8));
6725 +#endif
6726 +}
6727 +
6728 +
6729 +/* Registers */
6730 +
6731 +uint32_t DWC_READ_REG32(void *io_ctx, uint32_t volatile *reg)
6732 +{
6733 + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
6734 + bus_size_t ior = (bus_size_t)reg;
6735 +
6736 + return bus_space_read_4(io->iot, io->ioh, ior);
6737 +}
6738 +
6739 +#if 0
6740 +uint64_t DWC_READ_REG64(void *io_ctx, uint64_t volatile *reg)
6741 +{
6742 + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
6743 + bus_size_t ior = (bus_size_t)reg;
6744 +
6745 + return bus_space_read_8(io->iot, io->ioh, ior);
6746 +}
6747 +#endif
6748 +
6749 +void DWC_WRITE_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t value)
6750 +{
6751 + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
6752 + bus_size_t ior = (bus_size_t)reg;
6753 +
6754 + bus_space_write_4(io->iot, io->ioh, ior, value);
6755 +}
6756 +
6757 +#if 0
6758 +void DWC_WRITE_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t value)
6759 +{
6760 + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
6761 + bus_size_t ior = (bus_size_t)reg;
6762 +
6763 + bus_space_write_8(io->iot, io->ioh, ior, value);
6764 +}
6765 +#endif
6766 +
6767 +void DWC_MODIFY_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t clear_mask,
6768 + uint32_t set_mask)
6769 +{
6770 + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
6771 + bus_size_t ior = (bus_size_t)reg;
6772 +
6773 + bus_space_write_4(io->iot, io->ioh, ior,
6774 + (bus_space_read_4(io->iot, io->ioh, ior) &
6775 + ~clear_mask) | set_mask);
6776 +}
6777 +
6778 +#if 0
6779 +void DWC_MODIFY_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t clear_mask,
6780 + uint64_t set_mask)
6781 +{
6782 + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
6783 + bus_size_t ior = (bus_size_t)reg;
6784 +
6785 + bus_space_write_8(io->iot, io->ioh, ior,
6786 + (bus_space_read_8(io->iot, io->ioh, ior) &
6787 + ~clear_mask) | set_mask);
6788 +}
6789 +#endif
6790 +
6791 +
6792 +/* Locking */
6793 +
6794 +dwc_spinlock_t *DWC_SPINLOCK_ALLOC(void)
6795 +{
6796 + struct mtx *sl = DWC_ALLOC(sizeof(*sl));
6797 +
6798 + if (!sl) {
6799 + DWC_ERROR("Cannot allocate memory for spinlock");
6800 + return NULL;
6801 + }
6802 +
6803 + mtx_init(sl, "dw3spn", NULL, MTX_SPIN);
6804 + return (dwc_spinlock_t *)sl;
6805 +}
6806 +
6807 +void DWC_SPINLOCK_FREE(dwc_spinlock_t *lock)
6808 +{
6809 + struct mtx *sl = (struct mtx *)lock;
6810 +
6811 + mtx_destroy(sl);
6812 + DWC_FREE(sl);
6813 +}
6814 +
6815 +void DWC_SPINLOCK(dwc_spinlock_t *lock)
6816 +{
6817 + mtx_lock_spin((struct mtx *)lock); // ???
6818 +}
6819 +
6820 +void DWC_SPINUNLOCK(dwc_spinlock_t *lock)
6821 +{
6822 + mtx_unlock_spin((struct mtx *)lock); // ???
6823 +}
6824 +
6825 +void DWC_SPINLOCK_IRQSAVE(dwc_spinlock_t *lock, dwc_irqflags_t *flags)
6826 +{
6827 + mtx_lock_spin((struct mtx *)lock);
6828 +}
6829 +
6830 +void DWC_SPINUNLOCK_IRQRESTORE(dwc_spinlock_t *lock, dwc_irqflags_t flags)
6831 +{
6832 + mtx_unlock_spin((struct mtx *)lock);
6833 +}
6834 +
6835 +dwc_mutex_t *DWC_MUTEX_ALLOC(void)
6836 +{
6837 + struct mtx *m;
6838 + dwc_mutex_t *mutex = (dwc_mutex_t *)DWC_ALLOC(sizeof(struct mtx));
6839 +
6840 + if (!mutex) {
6841 + DWC_ERROR("Cannot allocate memory for mutex");
6842 + return NULL;
6843 + }
6844 +
6845 + m = (struct mtx *)mutex;
6846 + mtx_init(m, "dw3mtx", NULL, MTX_DEF);
6847 + return mutex;
6848 +}
6849 +
6850 +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
6851 +#else
6852 +void DWC_MUTEX_FREE(dwc_mutex_t *mutex)
6853 +{
6854 + mtx_destroy((struct mtx *)mutex);
6855 + DWC_FREE(mutex);
6856 +}
6857 +#endif
6858 +
6859 +void DWC_MUTEX_LOCK(dwc_mutex_t *mutex)
6860 +{
6861 + struct mtx *m = (struct mtx *)mutex;
6862 +
6863 + mtx_lock(m);
6864 +}
6865 +
6866 +int DWC_MUTEX_TRYLOCK(dwc_mutex_t *mutex)
6867 +{
6868 + struct mtx *m = (struct mtx *)mutex;
6869 +
6870 + return mtx_trylock(m);
6871 +}
6872 +
6873 +void DWC_MUTEX_UNLOCK(dwc_mutex_t *mutex)
6874 +{
6875 + struct mtx *m = (struct mtx *)mutex;
6876 +
6877 + mtx_unlock(m);
6878 +}
6879 +
6880 +
6881 +/* Timing */
6882 +
6883 +void DWC_UDELAY(uint32_t usecs)
6884 +{
6885 + DELAY(usecs);
6886 +}
6887 +
6888 +void DWC_MDELAY(uint32_t msecs)
6889 +{
6890 + do {
6891 + DELAY(1000);
6892 + } while (--msecs);
6893 +}
6894 +
6895 +void DWC_MSLEEP(uint32_t msecs)
6896 +{
6897 + struct timeval tv;
6898 +
6899 + tv.tv_sec = msecs / 1000;
6900 + tv.tv_usec = (msecs - tv.tv_sec * 1000) * 1000;
6901 + pause("dw3slp", tvtohz(&tv));
6902 +}
6903 +
6904 +uint32_t DWC_TIME(void)
6905 +{
6906 + struct timeval tv;
6907 +
6908 + microuptime(&tv); // or getmicrouptime? (less precise, but faster)
6909 + return tv.tv_sec * 1000 + tv.tv_usec / 1000;
6910 +}
6911 +
6912 +
6913 +/* Timers */
6914 +
6915 +struct dwc_timer {
6916 + struct callout t;
6917 + char *name;
6918 + dwc_spinlock_t *lock;
6919 + dwc_timer_callback_t cb;
6920 + void *data;
6921 +};
6922 +
6923 +dwc_timer_t *DWC_TIMER_ALLOC(char *name, dwc_timer_callback_t cb, void *data)
6924 +{
6925 + dwc_timer_t *t = DWC_ALLOC(sizeof(*t));
6926 +
6927 + if (!t) {
6928 + DWC_ERROR("Cannot allocate memory for timer");
6929 + return NULL;
6930 + }
6931 +
6932 + callout_init(&t->t, 1);
6933 +
6934 + t->name = DWC_STRDUP(name);
6935 + if (!t->name) {
6936 + DWC_ERROR("Cannot allocate memory for timer->name");
6937 + goto no_name;
6938 + }
6939 +
6940 + t->lock = DWC_SPINLOCK_ALLOC();
6941 + if (!t->lock) {
6942 + DWC_ERROR("Cannot allocate memory for lock");
6943 + goto no_lock;
6944 + }
6945 +
6946 + t->cb = cb;
6947 + t->data = data;
6948 +
6949 + return t;
6950 +
6951 + no_lock:
6952 + DWC_FREE(t->name);
6953 + no_name:
6954 + DWC_FREE(t);
6955 +
6956 + return NULL;
6957 +}
6958 +
6959 +void DWC_TIMER_FREE(dwc_timer_t *timer)
6960 +{
6961 + callout_stop(&timer->t);
6962 + DWC_SPINLOCK_FREE(timer->lock);
6963 + DWC_FREE(timer->name);
6964 + DWC_FREE(timer);
6965 +}
6966 +
6967 +void DWC_TIMER_SCHEDULE(dwc_timer_t *timer, uint32_t time)
6968 +{
6969 + struct timeval tv;
6970 +
6971 + tv.tv_sec = time / 1000;
6972 + tv.tv_usec = (time - tv.tv_sec * 1000) * 1000;
6973 + callout_reset(&timer->t, tvtohz(&tv), timer->cb, timer->data);
6974 +}
6975 +
6976 +void DWC_TIMER_CANCEL(dwc_timer_t *timer)
6977 +{
6978 + callout_stop(&timer->t);
6979 +}
6980 +
6981 +
6982 +/* Wait Queues */
6983 +
6984 +struct dwc_waitq {
6985 + struct mtx lock;
6986 + int abort;
6987 +};
6988 +
6989 +dwc_waitq_t *DWC_WAITQ_ALLOC(void)
6990 +{
6991 + dwc_waitq_t *wq = DWC_ALLOC(sizeof(*wq));
6992 +
6993 + if (!wq) {
6994 + DWC_ERROR("Cannot allocate memory for waitqueue");
6995 + return NULL;
6996 + }
6997 +
6998 + mtx_init(&wq->lock, "dw3wtq", NULL, MTX_DEF);
6999 + wq->abort = 0;
7000 +
7001 + return wq;
7002 +}
7003 +
7004 +void DWC_WAITQ_FREE(dwc_waitq_t *wq)
7005 +{
7006 + mtx_destroy(&wq->lock);
7007 + DWC_FREE(wq);
7008 +}
7009 +
7010 +int32_t DWC_WAITQ_WAIT(dwc_waitq_t *wq, dwc_waitq_condition_t cond, void *data)
7011 +{
7012 +// intrmask_t ipl;
7013 + int result = 0;
7014 +
7015 + mtx_lock(&wq->lock);
7016 +// ipl = splbio();
7017 +
7018 + /* Skip the sleep if already aborted or triggered */
7019 + if (!wq->abort && !cond(data)) {
7020 +// splx(ipl);
7021 + result = msleep(wq, &wq->lock, PCATCH, "dw3wat", 0); // infinite timeout
7022 +// ipl = splbio();
7023 + }
7024 +
7025 + if (result == ERESTART) { // signaled - restart
7026 + result = -DWC_E_RESTART;
7027 +
7028 + } else if (result == EINTR) { // signaled - interrupt
7029 + result = -DWC_E_ABORT;
7030 +
7031 + } else if (wq->abort) {
7032 + result = -DWC_E_ABORT;
7033 +
7034 + } else {
7035 + result = 0;
7036 + }
7037 +
7038 + wq->abort = 0;
7039 +// splx(ipl);
7040 + mtx_unlock(&wq->lock);
7041 + return result;
7042 +}
7043 +
7044 +int32_t DWC_WAITQ_WAIT_TIMEOUT(dwc_waitq_t *wq, dwc_waitq_condition_t cond,
7045 + void *data, int32_t msecs)
7046 +{
7047 + struct timeval tv, tv1, tv2;
7048 +// intrmask_t ipl;
7049 + int result = 0;
7050 +
7051 + tv.tv_sec = msecs / 1000;
7052 + tv.tv_usec = (msecs - tv.tv_sec * 1000) * 1000;
7053 +
7054 + mtx_lock(&wq->lock);
7055 +// ipl = splbio();
7056 +
7057 + /* Skip the sleep if already aborted or triggered */
7058 + if (!wq->abort && !cond(data)) {
7059 +// splx(ipl);
7060 + getmicrouptime(&tv1);
7061 + result = msleep(wq, &wq->lock, PCATCH, "dw3wto", tvtohz(&tv));
7062 + getmicrouptime(&tv2);
7063 +// ipl = splbio();
7064 + }
7065 +
7066 + if (result == 0) { // awoken
7067 + if (wq->abort) {
7068 + result = -DWC_E_ABORT;
7069 + } else {
7070 + tv2.tv_usec -= tv1.tv_usec;
7071 + if (tv2.tv_usec < 0) {
7072 + tv2.tv_usec += 1000000;
7073 + tv2.tv_sec--;
7074 + }
7075 +
7076 + tv2.tv_sec -= tv1.tv_sec;
7077 + result = tv2.tv_sec * 1000 + tv2.tv_usec / 1000;
7078 + result = msecs - result;
7079 + if (result <= 0)
7080 + result = 1;
7081 + }
7082 + } else if (result == ERESTART) { // signaled - restart
7083 + result = -DWC_E_RESTART;
7084 +
7085 + } else if (result == EINTR) { // signaled - interrupt
7086 + result = -DWC_E_ABORT;
7087 +
7088 + } else { // timed out
7089 + result = -DWC_E_TIMEOUT;
7090 + }
7091 +
7092 + wq->abort = 0;
7093 +// splx(ipl);
7094 + mtx_unlock(&wq->lock);
7095 + return result;
7096 +}
7097 +
7098 +void DWC_WAITQ_TRIGGER(dwc_waitq_t *wq)
7099 +{
7100 + wakeup(wq);
7101 +}
7102 +
7103 +void DWC_WAITQ_ABORT(dwc_waitq_t *wq)
7104 +{
7105 +// intrmask_t ipl;
7106 +
7107 + mtx_lock(&wq->lock);
7108 +// ipl = splbio();
7109 + wq->abort = 1;
7110 + wakeup(wq);
7111 +// splx(ipl);
7112 + mtx_unlock(&wq->lock);
7113 +}
7114 +
7115 +
7116 +/* Threading */
7117 +
7118 +struct dwc_thread {
7119 + struct proc *proc;
7120 + int abort;
7121 +};
7122 +
7123 +dwc_thread_t *DWC_THREAD_RUN(dwc_thread_function_t func, char *name, void *data)
7124 +{
7125 + int retval;
7126 + dwc_thread_t *thread = DWC_ALLOC(sizeof(*thread));
7127 +
7128 + if (!thread) {
7129 + return NULL;
7130 + }
7131 +
7132 + thread->abort = 0;
7133 + retval = kthread_create((void (*)(void *))func, data, &thread->proc,
7134 + RFPROC | RFNOWAIT, 0, "%s", name);
7135 + if (retval) {
7136 + DWC_FREE(thread);
7137 + return NULL;
7138 + }
7139 +
7140 + return thread;
7141 +}
7142 +
7143 +int DWC_THREAD_STOP(dwc_thread_t *thread)
7144 +{
7145 + int retval;
7146 +
7147 + thread->abort = 1;
7148 + retval = tsleep(&thread->abort, 0, "dw3stp", 60 * hz);
7149 +
7150 + if (retval == 0) {
7151 + /* DWC_THREAD_EXIT() will free the thread struct */
7152 + return 0;
7153 + }
7154 +
7155 + /* NOTE: We leak the thread struct if thread doesn't die */
7156 +
7157 + if (retval == EWOULDBLOCK) {
7158 + return -DWC_E_TIMEOUT;
7159 + }
7160 +
7161 + return -DWC_E_UNKNOWN;
7162 +}
7163 +
7164 +dwc_bool_t DWC_THREAD_SHOULD_STOP(dwc_thread_t *thread)
7165 +{
7166 + return thread->abort;
7167 +}
7168 +
7169 +void DWC_THREAD_EXIT(dwc_thread_t *thread)
7170 +{
7171 + wakeup(&thread->abort);
7172 + DWC_FREE(thread);
7173 + kthread_exit(0);
7174 +}
7175 +
7176 +
7177 +/* tasklets
7178 + - Runs in interrupt context (cannot sleep)
7179 + - Each tasklet runs on a single CPU [ How can we ensure this on FreeBSD? Does it matter? ]
7180 + - Different tasklets can be running simultaneously on different CPUs [ shouldn't matter ]
7181 + */
7182 +struct dwc_tasklet {
7183 + struct task t;
7184 + dwc_tasklet_callback_t cb;
7185 + void *data;
7186 +};
7187 +
7188 +static void tasklet_callback(void *data, int pending) // what to do with pending ???
7189 +{
7190 + dwc_tasklet_t *task = (dwc_tasklet_t *)data;
7191 +
7192 + task->cb(task->data);
7193 +}
7194 +
7195 +dwc_tasklet_t *DWC_TASK_ALLOC(char *name, dwc_tasklet_callback_t cb, void *data)
7196 +{
7197 + dwc_tasklet_t *task = DWC_ALLOC(sizeof(*task));
7198 +
7199 + if (task) {
7200 + task->cb = cb;
7201 + task->data = data;
7202 + TASK_INIT(&task->t, 0, tasklet_callback, task);
7203 + } else {
7204 + DWC_ERROR("Cannot allocate memory for tasklet");
7205 + }
7206 +
7207 + return task;
7208 +}
7209 +
7210 +void DWC_TASK_FREE(dwc_tasklet_t *task)
7211 +{
7212 + taskqueue_drain(taskqueue_fast, &task->t); // ???
7213 + DWC_FREE(task);
7214 +}
7215 +
7216 +void DWC_TASK_SCHEDULE(dwc_tasklet_t *task)
7217 +{
7218 + /* Uses predefined system queue */
7219 + taskqueue_enqueue_fast(taskqueue_fast, &task->t);
7220 +}
7221 +
7222 +
7223 +/* workqueues
7224 + - Runs in process context (can sleep)
7225 + */
7226 +typedef struct work_container {
7227 + dwc_work_callback_t cb;
7228 + void *data;
7229 + dwc_workq_t *wq;
7230 + char *name;
7231 + int hz;
7232 +
7233 +#ifdef DEBUG
7234 + DWC_CIRCLEQ_ENTRY(work_container) entry;
7235 +#endif
7236 + struct task task;
7237 +} work_container_t;
7238 +
7239 +#ifdef DEBUG
7240 +DWC_CIRCLEQ_HEAD(work_container_queue, work_container);
7241 +#endif
7242 +
7243 +struct dwc_workq {
7244 + struct taskqueue *taskq;
7245 + dwc_spinlock_t *lock;
7246 + dwc_waitq_t *waitq;
7247 + int pending;
7248 +
7249 +#ifdef DEBUG
7250 + struct work_container_queue entries;
7251 +#endif
7252 +};
7253 +
7254 +static void do_work(void *data, int pending) // what to do with pending ???
7255 +{
7256 + work_container_t *container = (work_container_t *)data;
7257 + dwc_workq_t *wq = container->wq;
7258 + dwc_irqflags_t flags;
7259 +
7260 + if (container->hz) {
7261 + pause("dw3wrk", container->hz);
7262 + }
7263 +
7264 + container->cb(container->data);
7265 + DWC_DEBUG("Work done: %s, container=%p", container->name, container);
7266 +
7267 + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
7268 +
7269 +#ifdef DEBUG
7270 + DWC_CIRCLEQ_REMOVE(&wq->entries, container, entry);
7271 +#endif
7272 + if (container->name)
7273 + DWC_FREE(container->name);
7274 + DWC_FREE(container);
7275 + wq->pending--;
7276 + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
7277 + DWC_WAITQ_TRIGGER(wq->waitq);
7278 +}
7279 +
7280 +static int work_done(void *data)
7281 +{
7282 + dwc_workq_t *workq = (dwc_workq_t *)data;
7283 +
7284 + return workq->pending == 0;
7285 +}
7286 +
7287 +int DWC_WORKQ_WAIT_WORK_DONE(dwc_workq_t *workq, int timeout)
7288 +{
7289 + return DWC_WAITQ_WAIT_TIMEOUT(workq->waitq, work_done, workq, timeout);
7290 +}
7291 +
7292 +dwc_workq_t *DWC_WORKQ_ALLOC(char *name)
7293 +{
7294 + dwc_workq_t *wq = DWC_ALLOC(sizeof(*wq));
7295 +
7296 + if (!wq) {
7297 + DWC_ERROR("Cannot allocate memory for workqueue");
7298 + return NULL;
7299 + }
7300 +
7301 + wq->taskq = taskqueue_create(name, M_NOWAIT, taskqueue_thread_enqueue, &wq->taskq);
7302 + if (!wq->taskq) {
7303 + DWC_ERROR("Cannot allocate memory for taskqueue");
7304 + goto no_taskq;
7305 + }
7306 +
7307 + wq->pending = 0;
7308 +
7309 + wq->lock = DWC_SPINLOCK_ALLOC();
7310 + if (!wq->lock) {
7311 + DWC_ERROR("Cannot allocate memory for spinlock");
7312 + goto no_lock;
7313 + }
7314 +
7315 + wq->waitq = DWC_WAITQ_ALLOC();
7316 + if (!wq->waitq) {
7317 + DWC_ERROR("Cannot allocate memory for waitqueue");
7318 + goto no_waitq;
7319 + }
7320 +
7321 + taskqueue_start_threads(&wq->taskq, 1, PWAIT, "%s taskq", "dw3tsk");
7322 +
7323 +#ifdef DEBUG
7324 + DWC_CIRCLEQ_INIT(&wq->entries);
7325 +#endif
7326 + return wq;
7327 +
7328 + no_waitq:
7329 + DWC_SPINLOCK_FREE(wq->lock);
7330 + no_lock:
7331 + taskqueue_free(wq->taskq);
7332 + no_taskq:
7333 + DWC_FREE(wq);
7334 +
7335 + return NULL;
7336 +}
7337 +
7338 +void DWC_WORKQ_FREE(dwc_workq_t *wq)
7339 +{
7340 +#ifdef DEBUG
7341 + dwc_irqflags_t flags;
7342 +
7343 + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
7344 +
7345 + if (wq->pending != 0) {
7346 + struct work_container *container;
7347 +
7348 + DWC_ERROR("Destroying work queue with pending work");
7349 +
7350 + DWC_CIRCLEQ_FOREACH(container, &wq->entries, entry) {
7351 + DWC_ERROR("Work %s still pending", container->name);
7352 + }
7353 + }
7354 +
7355 + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
7356 +#endif
7357 + DWC_WAITQ_FREE(wq->waitq);
7358 + DWC_SPINLOCK_FREE(wq->lock);
7359 + taskqueue_free(wq->taskq);
7360 + DWC_FREE(wq);
7361 +}
7362 +
7363 +void DWC_WORKQ_SCHEDULE(dwc_workq_t *wq, dwc_work_callback_t cb, void *data,
7364 + char *format, ...)
7365 +{
7366 + dwc_irqflags_t flags;
7367 + work_container_t *container;
7368 + static char name[128];
7369 + va_list args;
7370 +
7371 + va_start(args, format);
7372 + DWC_VSNPRINTF(name, 128, format, args);
7373 + va_end(args);
7374 +
7375 + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
7376 + wq->pending++;
7377 + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
7378 + DWC_WAITQ_TRIGGER(wq->waitq);
7379 +
7380 + container = DWC_ALLOC_ATOMIC(sizeof(*container));
7381 + if (!container) {
7382 + DWC_ERROR("Cannot allocate memory for container");
7383 + return;
7384 + }
7385 +
7386 + container->name = DWC_STRDUP(name);
7387 + if (!container->name) {
7388 + DWC_ERROR("Cannot allocate memory for container->name");
7389 + DWC_FREE(container);
7390 + return;
7391 + }
7392 +
7393 + container->cb = cb;
7394 + container->data = data;
7395 + container->wq = wq;
7396 + container->hz = 0;
7397 +
7398 + DWC_DEBUG("Queueing work: %s, container=%p", container->name, container);
7399 +
7400 + TASK_INIT(&container->task, 0, do_work, container);
7401 +
7402 +#ifdef DEBUG
7403 + DWC_CIRCLEQ_INSERT_TAIL(&wq->entries, container, entry);
7404 +#endif
7405 + taskqueue_enqueue_fast(wq->taskq, &container->task);
7406 +}
7407 +
7408 +void DWC_WORKQ_SCHEDULE_DELAYED(dwc_workq_t *wq, dwc_work_callback_t cb,
7409 + void *data, uint32_t time, char *format, ...)
7410 +{
7411 + dwc_irqflags_t flags;
7412 + work_container_t *container;
7413 + static char name[128];
7414 + struct timeval tv;
7415 + va_list args;
7416 +
7417 + va_start(args, format);
7418 + DWC_VSNPRINTF(name, 128, format, args);
7419 + va_end(args);
7420 +
7421 + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
7422 + wq->pending++;
7423 + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
7424 + DWC_WAITQ_TRIGGER(wq->waitq);
7425 +
7426 + container = DWC_ALLOC_ATOMIC(sizeof(*container));
7427 + if (!container) {
7428 + DWC_ERROR("Cannot allocate memory for container");
7429 + return;
7430 + }
7431 +
7432 + container->name = DWC_STRDUP(name);
7433 + if (!container->name) {
7434 + DWC_ERROR("Cannot allocate memory for container->name");
7435 + DWC_FREE(container);
7436 + return;
7437 + }
7438 +
7439 + container->cb = cb;
7440 + container->data = data;
7441 + container->wq = wq;
7442 +
7443 + tv.tv_sec = time / 1000;
7444 + tv.tv_usec = (time - tv.tv_sec * 1000) * 1000;
7445 + container->hz = tvtohz(&tv);
7446 +
7447 + DWC_DEBUG("Queueing work: %s, container=%p", container->name, container);
7448 +
7449 + TASK_INIT(&container->task, 0, do_work, container);
7450 +
7451 +#ifdef DEBUG
7452 + DWC_CIRCLEQ_INSERT_TAIL(&wq->entries, container, entry);
7453 +#endif
7454 + taskqueue_enqueue_fast(wq->taskq, &container->task);
7455 +}
7456 +
7457 +int DWC_WORKQ_PENDING(dwc_workq_t *wq)
7458 +{
7459 + return wq->pending;
7460 +}
7461 --- /dev/null
7462 +++ b/drivers/usb/host/dwc_common_port/dwc_common_linux.c
7463 @@ -0,0 +1,1418 @@
7464 +#include <linux/kernel.h>
7465 +#include <linux/init.h>
7466 +#include <linux/module.h>
7467 +#include <linux/kthread.h>
7468 +
7469 +#ifdef DWC_CCLIB
7470 +# include "dwc_cc.h"
7471 +#endif
7472 +
7473 +#ifdef DWC_CRYPTOLIB
7474 +# include "dwc_modpow.h"
7475 +# include "dwc_dh.h"
7476 +# include "dwc_crypto.h"
7477 +#endif
7478 +
7479 +#ifdef DWC_NOTIFYLIB
7480 +# include "dwc_notifier.h"
7481 +#endif
7482 +
7483 +/* OS-Level Implementations */
7484 +
7485 +/* This is the Linux kernel implementation of the DWC platform library. */
7486 +#include <linux/moduleparam.h>
7487 +#include <linux/ctype.h>
7488 +#include <linux/crypto.h>
7489 +#include <linux/delay.h>
7490 +#include <linux/device.h>
7491 +#include <linux/dma-mapping.h>
7492 +#include <linux/cdev.h>
7493 +#include <linux/errno.h>
7494 +#include <linux/interrupt.h>
7495 +#include <linux/jiffies.h>
7496 +#include <linux/list.h>
7497 +#include <linux/pci.h>
7498 +#include <linux/random.h>
7499 +#include <linux/scatterlist.h>
7500 +#include <linux/slab.h>
7501 +#include <linux/stat.h>
7502 +#include <linux/string.h>
7503 +#include <linux/timer.h>
7504 +#include <linux/usb.h>
7505 +
7506 +#include <linux/version.h>
7507 +
7508 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
7509 +# include <linux/usb/gadget.h>
7510 +#else
7511 +# include <linux/usb_gadget.h>
7512 +#endif
7513 +
7514 +#include <asm/io.h>
7515 +#include <asm/page.h>
7516 +#include <asm/uaccess.h>
7517 +#include <asm/unaligned.h>
7518 +
7519 +#include "dwc_os.h"
7520 +#include "dwc_list.h"
7521 +
7522 +
7523 +/* MISC */
7524 +
7525 +void *DWC_MEMSET(void *dest, uint8_t byte, uint32_t size)
7526 +{
7527 + return memset(dest, byte, size);
7528 +}
7529 +
7530 +void *DWC_MEMCPY(void *dest, void const *src, uint32_t size)
7531 +{
7532 + return memcpy(dest, src, size);
7533 +}
7534 +
7535 +void *DWC_MEMMOVE(void *dest, void *src, uint32_t size)
7536 +{
7537 + return memmove(dest, src, size);
7538 +}
7539 +
7540 +int DWC_MEMCMP(void *m1, void *m2, uint32_t size)
7541 +{
7542 + return memcmp(m1, m2, size);
7543 +}
7544 +
7545 +int DWC_STRNCMP(void *s1, void *s2, uint32_t size)
7546 +{
7547 + return strncmp(s1, s2, size);
7548 +}
7549 +
7550 +int DWC_STRCMP(void *s1, void *s2)
7551 +{
7552 + return strcmp(s1, s2);
7553 +}
7554 +
7555 +int DWC_STRLEN(char const *str)
7556 +{
7557 + return strlen(str);
7558 +}
7559 +
7560 +char *DWC_STRCPY(char *to, char const *from)
7561 +{
7562 + return strcpy(to, from);
7563 +}
7564 +
7565 +char *DWC_STRDUP(char const *str)
7566 +{
7567 + int len = DWC_STRLEN(str) + 1;
7568 + char *new = DWC_ALLOC_ATOMIC(len);
7569 +
7570 + if (!new) {
7571 + return NULL;
7572 + }
7573 +
7574 + DWC_MEMCPY(new, str, len);
7575 + return new;
7576 +}
7577 +
7578 +int DWC_ATOI(const char *str, int32_t *value)
7579 +{
7580 + char *end = NULL;
7581 +
7582 + *value = simple_strtol(str, &end, 0);
7583 + if (*end == '\0') {
7584 + return 0;
7585 + }
7586 +
7587 + return -1;
7588 +}
7589 +
7590 +int DWC_ATOUI(const char *str, uint32_t *value)
7591 +{
7592 + char *end = NULL;
7593 +
7594 + *value = simple_strtoul(str, &end, 0);
7595 + if (*end == '\0') {
7596 + return 0;
7597 + }
7598 +
7599 + return -1;
7600 +}
7601 +
7602 +
7603 +#ifdef DWC_UTFLIB
7604 +/* From usbstring.c */
7605 +
7606 +int DWC_UTF8_TO_UTF16LE(uint8_t const *s, uint16_t *cp, unsigned len)
7607 +{
7608 + int count = 0;
7609 + u8 c;
7610 + u16 uchar;
7611 +
7612 + /* this insists on correct encodings, though not minimal ones.
7613 + * BUT it currently rejects legit 4-byte UTF-8 code points,
7614 + * which need surrogate pairs. (Unicode 3.1 can use them.)
7615 + */
7616 + while (len != 0 && (c = (u8) *s++) != 0) {
7617 + if (unlikely(c & 0x80)) {
7618 + // 2-byte sequence:
7619 + // 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx
7620 + if ((c & 0xe0) == 0xc0) {
7621 + uchar = (c & 0x1f) << 6;
7622 +
7623 + c = (u8) *s++;
7624 + if ((c & 0xc0) != 0xc0)
7625 + goto fail;
7626 + c &= 0x3f;
7627 + uchar |= c;
7628 +
7629 + // 3-byte sequence (most CJKV characters):
7630 + // zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx
7631 + } else if ((c & 0xf0) == 0xe0) {
7632 + uchar = (c & 0x0f) << 12;
7633 +
7634 + c = (u8) *s++;
7635 + if ((c & 0xc0) != 0xc0)
7636 + goto fail;
7637 + c &= 0x3f;
7638 + uchar |= c << 6;
7639 +
7640 + c = (u8) *s++;
7641 + if ((c & 0xc0) != 0xc0)
7642 + goto fail;
7643 + c &= 0x3f;
7644 + uchar |= c;
7645 +
7646 + /* no bogus surrogates */
7647 + if (0xd800 <= uchar && uchar <= 0xdfff)
7648 + goto fail;
7649 +
7650 + // 4-byte sequence (surrogate pairs, currently rare):
7651 + // 11101110wwwwzzzzyy + 110111yyyyxxxxxx
7652 + // = 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx
7653 + // (uuuuu = wwww + 1)
7654 + // FIXME accept the surrogate code points (only)
7655 + } else
7656 + goto fail;
7657 + } else
7658 + uchar = c;
7659 + put_unaligned (cpu_to_le16 (uchar), cp++);
7660 + count++;
7661 + len--;
7662 + }
7663 + return count;
7664 +fail:
7665 + return -1;
7666 +}
7667 +#endif /* DWC_UTFLIB */
7668 +
7669 +
7670 +/* dwc_debug.h */
7671 +
7672 +dwc_bool_t DWC_IN_IRQ(void)
7673 +{
7674 + return in_irq();
7675 +}
7676 +
7677 +dwc_bool_t DWC_IN_BH(void)
7678 +{
7679 + return in_softirq();
7680 +}
7681 +
7682 +void DWC_VPRINTF(char *format, va_list args)
7683 +{
7684 + vprintk(format, args);
7685 +}
7686 +
7687 +int DWC_VSNPRINTF(char *str, int size, char *format, va_list args)
7688 +{
7689 + return vsnprintf(str, size, format, args);
7690 +}
7691 +
7692 +void DWC_PRINTF(char *format, ...)
7693 +{
7694 + va_list args;
7695 +
7696 + va_start(args, format);
7697 + DWC_VPRINTF(format, args);
7698 + va_end(args);
7699 +}
7700 +
7701 +int DWC_SPRINTF(char *buffer, char *format, ...)
7702 +{
7703 + int retval;
7704 + va_list args;
7705 +
7706 + va_start(args, format);
7707 + retval = vsprintf(buffer, format, args);
7708 + va_end(args);
7709 + return retval;
7710 +}
7711 +
7712 +int DWC_SNPRINTF(char *buffer, int size, char *format, ...)
7713 +{
7714 + int retval;
7715 + va_list args;
7716 +
7717 + va_start(args, format);
7718 + retval = vsnprintf(buffer, size, format, args);
7719 + va_end(args);
7720 + return retval;
7721 +}
7722 +
7723 +void __DWC_WARN(char *format, ...)
7724 +{
7725 + va_list args;
7726 +
7727 + va_start(args, format);
7728 + DWC_PRINTF(KERN_WARNING);
7729 + DWC_VPRINTF(format, args);
7730 + va_end(args);
7731 +}
7732 +
7733 +void __DWC_ERROR(char *format, ...)
7734 +{
7735 + va_list args;
7736 +
7737 + va_start(args, format);
7738 + DWC_PRINTF(KERN_ERR);
7739 + DWC_VPRINTF(format, args);
7740 + va_end(args);
7741 +}
7742 +
7743 +void DWC_EXCEPTION(char *format, ...)
7744 +{
7745 + va_list args;
7746 +
7747 + va_start(args, format);
7748 + DWC_PRINTF(KERN_ERR);
7749 + DWC_VPRINTF(format, args);
7750 + va_end(args);
7751 + BUG_ON(1);
7752 +}
7753 +
7754 +#ifdef DEBUG
7755 +void __DWC_DEBUG(char *format, ...)
7756 +{
7757 + va_list args;
7758 +
7759 + va_start(args, format);
7760 + DWC_PRINTF(KERN_DEBUG);
7761 + DWC_VPRINTF(format, args);
7762 + va_end(args);
7763 +}
7764 +#endif
7765 +
7766 +
7767 +/* dwc_mem.h */
7768 +
7769 +#if 0
7770 +dwc_pool_t *DWC_DMA_POOL_CREATE(uint32_t size,
7771 + uint32_t align,
7772 + uint32_t alloc)
7773 +{
7774 + struct dma_pool *pool = dma_pool_create("Pool", NULL,
7775 + size, align, alloc);
7776 + return (dwc_pool_t *)pool;
7777 +}
7778 +
7779 +void DWC_DMA_POOL_DESTROY(dwc_pool_t *pool)
7780 +{
7781 + dma_pool_destroy((struct dma_pool *)pool);
7782 +}
7783 +
7784 +void *DWC_DMA_POOL_ALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
7785 +{
7786 + return dma_pool_alloc((struct dma_pool *)pool, GFP_KERNEL, dma_addr);
7787 +}
7788 +
7789 +void *DWC_DMA_POOL_ZALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
7790 +{
7791 + void *vaddr = DWC_DMA_POOL_ALLOC(pool, dma_addr);
7792 + memset(..);
7793 +}
7794 +
7795 +void DWC_DMA_POOL_FREE(dwc_pool_t *pool, void *vaddr, void *daddr)
7796 +{
7797 + dma_pool_free(pool, vaddr, daddr);
7798 +}
7799 +#endif
7800 +
7801 +void *__DWC_DMA_ALLOC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr)
7802 +{
7803 + return dma_zalloc_coherent(dma_ctx, size, dma_addr, GFP_KERNEL | GFP_DMA32);
7804 +}
7805 +
7806 +void *__DWC_DMA_ALLOC_ATOMIC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr)
7807 +{
7808 + return dma_zalloc_coherent(dma_ctx, size, dma_addr, GFP_ATOMIC);
7809 +}
7810 +
7811 +void __DWC_DMA_FREE(void *dma_ctx, uint32_t size, void *virt_addr, dwc_dma_t dma_addr)
7812 +{
7813 + dma_free_coherent(dma_ctx, size, virt_addr, dma_addr);
7814 +}
7815 +
7816 +void *__DWC_ALLOC(void *mem_ctx, uint32_t size)
7817 +{
7818 + return kzalloc(size, GFP_KERNEL);
7819 +}
7820 +
7821 +void *__DWC_ALLOC_ATOMIC(void *mem_ctx, uint32_t size)
7822 +{
7823 + return kzalloc(size, GFP_ATOMIC);
7824 +}
7825 +
7826 +void __DWC_FREE(void *mem_ctx, void *addr)
7827 +{
7828 + kfree(addr);
7829 +}
7830 +
7831 +
7832 +#ifdef DWC_CRYPTOLIB
7833 +/* dwc_crypto.h */
7834 +
7835 +void DWC_RANDOM_BYTES(uint8_t *buffer, uint32_t length)
7836 +{
7837 + get_random_bytes(buffer, length);
7838 +}
7839 +
7840 +int DWC_AES_CBC(uint8_t *message, uint32_t messagelen, uint8_t *key, uint32_t keylen, uint8_t iv[16], uint8_t *out)
7841 +{
7842 + struct crypto_blkcipher *tfm;
7843 + struct blkcipher_desc desc;
7844 + struct scatterlist sgd;
7845 + struct scatterlist sgs;
7846 +
7847 + tfm = crypto_alloc_blkcipher("cbc(aes)", 0, CRYPTO_ALG_ASYNC);
7848 + if (tfm == NULL) {
7849 + printk("failed to load transform for aes CBC\n");
7850 + return -1;
7851 + }
7852 +
7853 + crypto_blkcipher_setkey(tfm, key, keylen);
7854 + crypto_blkcipher_set_iv(tfm, iv, 16);
7855 +
7856 + sg_init_one(&sgd, out, messagelen);
7857 + sg_init_one(&sgs, message, messagelen);
7858 +
7859 + desc.tfm = tfm;
7860 + desc.flags = 0;
7861 +
7862 + if (crypto_blkcipher_encrypt(&desc, &sgd, &sgs, messagelen)) {
7863 + crypto_free_blkcipher(tfm);
7864 + DWC_ERROR("AES CBC encryption failed");
7865 + return -1;
7866 + }
7867 +
7868 + crypto_free_blkcipher(tfm);
7869 + return 0;
7870 +}
7871 +
7872 +int DWC_SHA256(uint8_t *message, uint32_t len, uint8_t *out)
7873 +{
7874 + struct crypto_hash *tfm;
7875 + struct hash_desc desc;
7876 + struct scatterlist sg;
7877 +
7878 + tfm = crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC);
7879 + if (IS_ERR(tfm)) {
7880 + DWC_ERROR("Failed to load transform for sha256: %ld\n", PTR_ERR(tfm));
7881 + return 0;
7882 + }
7883 + desc.tfm = tfm;
7884 + desc.flags = 0;
7885 +
7886 + sg_init_one(&sg, message, len);
7887 + crypto_hash_digest(&desc, &sg, len, out);
7888 + crypto_free_hash(tfm);
7889 +
7890 + return 1;
7891 +}
7892 +
7893 +int DWC_HMAC_SHA256(uint8_t *message, uint32_t messagelen,
7894 + uint8_t *key, uint32_t keylen, uint8_t *out)
7895 +{
7896 + struct crypto_hash *tfm;
7897 + struct hash_desc desc;
7898 + struct scatterlist sg;
7899 +
7900 + tfm = crypto_alloc_hash("hmac(sha256)", 0, CRYPTO_ALG_ASYNC);
7901 + if (IS_ERR(tfm)) {
7902 + DWC_ERROR("Failed to load transform for hmac(sha256): %ld\n", PTR_ERR(tfm));
7903 + return 0;
7904 + }
7905 + desc.tfm = tfm;
7906 + desc.flags = 0;
7907 +
7908 + sg_init_one(&sg, message, messagelen);
7909 + crypto_hash_setkey(tfm, key, keylen);
7910 + crypto_hash_digest(&desc, &sg, messagelen, out);
7911 + crypto_free_hash(tfm);
7912 +
7913 + return 1;
7914 +}
7915 +#endif /* DWC_CRYPTOLIB */
7916 +
7917 +
7918 +/* Byte Ordering Conversions */
7919 +
7920 +uint32_t DWC_CPU_TO_LE32(uint32_t *p)
7921 +{
7922 +#ifdef __LITTLE_ENDIAN
7923 + return *p;
7924 +#else
7925 + uint8_t *u_p = (uint8_t *)p;
7926 +
7927 + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
7928 +#endif
7929 +}
7930 +
7931 +uint32_t DWC_CPU_TO_BE32(uint32_t *p)
7932 +{
7933 +#ifdef __BIG_ENDIAN
7934 + return *p;
7935 +#else
7936 + uint8_t *u_p = (uint8_t *)p;
7937 +
7938 + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
7939 +#endif
7940 +}
7941 +
7942 +uint32_t DWC_LE32_TO_CPU(uint32_t *p)
7943 +{
7944 +#ifdef __LITTLE_ENDIAN
7945 + return *p;
7946 +#else
7947 + uint8_t *u_p = (uint8_t *)p;
7948 +
7949 + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
7950 +#endif
7951 +}
7952 +
7953 +uint32_t DWC_BE32_TO_CPU(uint32_t *p)
7954 +{
7955 +#ifdef __BIG_ENDIAN
7956 + return *p;
7957 +#else
7958 + uint8_t *u_p = (uint8_t *)p;
7959 +
7960 + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
7961 +#endif
7962 +}
7963 +
7964 +uint16_t DWC_CPU_TO_LE16(uint16_t *p)
7965 +{
7966 +#ifdef __LITTLE_ENDIAN
7967 + return *p;
7968 +#else
7969 + uint8_t *u_p = (uint8_t *)p;
7970 + return (u_p[1] | (u_p[0] << 8));
7971 +#endif
7972 +}
7973 +
7974 +uint16_t DWC_CPU_TO_BE16(uint16_t *p)
7975 +{
7976 +#ifdef __BIG_ENDIAN
7977 + return *p;
7978 +#else
7979 + uint8_t *u_p = (uint8_t *)p;
7980 + return (u_p[1] | (u_p[0] << 8));
7981 +#endif
7982 +}
7983 +
7984 +uint16_t DWC_LE16_TO_CPU(uint16_t *p)
7985 +{
7986 +#ifdef __LITTLE_ENDIAN
7987 + return *p;
7988 +#else
7989 + uint8_t *u_p = (uint8_t *)p;
7990 + return (u_p[1] | (u_p[0] << 8));
7991 +#endif
7992 +}
7993 +
7994 +uint16_t DWC_BE16_TO_CPU(uint16_t *p)
7995 +{
7996 +#ifdef __BIG_ENDIAN
7997 + return *p;
7998 +#else
7999 + uint8_t *u_p = (uint8_t *)p;
8000 + return (u_p[1] | (u_p[0] << 8));
8001 +#endif
8002 +}
8003 +
8004 +
8005 +/* Registers */
8006 +
8007 +uint32_t DWC_READ_REG32(uint32_t volatile *reg)
8008 +{
8009 + return readl(reg);
8010 +}
8011 +
8012 +#if 0
8013 +uint64_t DWC_READ_REG64(uint64_t volatile *reg)
8014 +{
8015 +}
8016 +#endif
8017 +
8018 +void DWC_WRITE_REG32(uint32_t volatile *reg, uint32_t value)
8019 +{
8020 + writel(value, reg);
8021 +}
8022 +
8023 +#if 0
8024 +void DWC_WRITE_REG64(uint64_t volatile *reg, uint64_t value)
8025 +{
8026 +}
8027 +#endif
8028 +
8029 +void DWC_MODIFY_REG32(uint32_t volatile *reg, uint32_t clear_mask, uint32_t set_mask)
8030 +{
8031 + writel((readl(reg) & ~clear_mask) | set_mask, reg);
8032 +}
8033 +
8034 +#if 0
8035 +void DWC_MODIFY_REG64(uint64_t volatile *reg, uint64_t clear_mask, uint64_t set_mask)
8036 +{
8037 +}
8038 +#endif
8039 +
8040 +
8041 +/* Locking */
8042 +
8043 +dwc_spinlock_t *DWC_SPINLOCK_ALLOC(void)
8044 +{
8045 + spinlock_t *sl = (spinlock_t *)1;
8046 +
8047 +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
8048 + sl = DWC_ALLOC(sizeof(*sl));
8049 + if (!sl) {
8050 + DWC_ERROR("Cannot allocate memory for spinlock\n");
8051 + return NULL;
8052 + }
8053 +
8054 + spin_lock_init(sl);
8055 +#endif
8056 + return (dwc_spinlock_t *)sl;
8057 +}
8058 +
8059 +void DWC_SPINLOCK_FREE(dwc_spinlock_t *lock)
8060 +{
8061 +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
8062 + DWC_FREE(lock);
8063 +#endif
8064 +}
8065 +
8066 +void DWC_SPINLOCK(dwc_spinlock_t *lock)
8067 +{
8068 +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
8069 + spin_lock((spinlock_t *)lock);
8070 +#endif
8071 +}
8072 +
8073 +void DWC_SPINUNLOCK(dwc_spinlock_t *lock)
8074 +{
8075 +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
8076 + spin_unlock((spinlock_t *)lock);
8077 +#endif
8078 +}
8079 +
8080 +void DWC_SPINLOCK_IRQSAVE(dwc_spinlock_t *lock, dwc_irqflags_t *flags)
8081 +{
8082 + dwc_irqflags_t f;
8083 +
8084 +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
8085 + spin_lock_irqsave((spinlock_t *)lock, f);
8086 +#else
8087 + local_irq_save(f);
8088 +#endif
8089 + *flags = f;
8090 +}
8091 +
8092 +void DWC_SPINUNLOCK_IRQRESTORE(dwc_spinlock_t *lock, dwc_irqflags_t flags)
8093 +{
8094 +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
8095 + spin_unlock_irqrestore((spinlock_t *)lock, flags);
8096 +#else
8097 + local_irq_restore(flags);
8098 +#endif
8099 +}
8100 +
8101 +dwc_mutex_t *DWC_MUTEX_ALLOC(void)
8102 +{
8103 + struct mutex *m;
8104 + dwc_mutex_t *mutex = (dwc_mutex_t *)DWC_ALLOC(sizeof(struct mutex));
8105 +
8106 + if (!mutex) {
8107 + DWC_ERROR("Cannot allocate memory for mutex\n");
8108 + return NULL;
8109 + }
8110 +
8111 + m = (struct mutex *)mutex;
8112 + mutex_init(m);
8113 + return mutex;
8114 +}
8115 +
8116 +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
8117 +#else
8118 +void DWC_MUTEX_FREE(dwc_mutex_t *mutex)
8119 +{
8120 + mutex_destroy((struct mutex *)mutex);
8121 + DWC_FREE(mutex);
8122 +}
8123 +#endif
8124 +
8125 +void DWC_MUTEX_LOCK(dwc_mutex_t *mutex)
8126 +{
8127 + struct mutex *m = (struct mutex *)mutex;
8128 + mutex_lock(m);
8129 +}
8130 +
8131 +int DWC_MUTEX_TRYLOCK(dwc_mutex_t *mutex)
8132 +{
8133 + struct mutex *m = (struct mutex *)mutex;
8134 + return mutex_trylock(m);
8135 +}
8136 +
8137 +void DWC_MUTEX_UNLOCK(dwc_mutex_t *mutex)
8138 +{
8139 + struct mutex *m = (struct mutex *)mutex;
8140 + mutex_unlock(m);
8141 +}
8142 +
8143 +
8144 +/* Timing */
8145 +
8146 +void DWC_UDELAY(uint32_t usecs)
8147 +{
8148 + udelay(usecs);
8149 +}
8150 +
8151 +void DWC_MDELAY(uint32_t msecs)
8152 +{
8153 + mdelay(msecs);
8154 +}
8155 +
8156 +void DWC_MSLEEP(uint32_t msecs)
8157 +{
8158 + msleep(msecs);
8159 +}
8160 +
8161 +uint32_t DWC_TIME(void)
8162 +{
8163 + return jiffies_to_msecs(jiffies);
8164 +}
8165 +
8166 +
8167 +/* Timers */
8168 +
8169 +struct dwc_timer {
8170 + struct timer_list *t;
8171 + char *name;
8172 + dwc_timer_callback_t cb;
8173 + void *data;
8174 + uint8_t scheduled;
8175 + dwc_spinlock_t *lock;
8176 +};
8177 +
8178 +static void timer_callback(unsigned long data)
8179 +{
8180 + dwc_timer_t *timer = (dwc_timer_t *)data;
8181 + dwc_irqflags_t flags;
8182 +
8183 + DWC_SPINLOCK_IRQSAVE(timer->lock, &flags);
8184 + timer->scheduled = 0;
8185 + DWC_SPINUNLOCK_IRQRESTORE(timer->lock, flags);
8186 + DWC_DEBUGC("Timer %s callback", timer->name);
8187 + timer->cb(timer->data);
8188 +}
8189 +
8190 +dwc_timer_t *DWC_TIMER_ALLOC(char *name, dwc_timer_callback_t cb, void *data)
8191 +{
8192 + dwc_timer_t *t = DWC_ALLOC(sizeof(*t));
8193 +
8194 + if (!t) {
8195 + DWC_ERROR("Cannot allocate memory for timer");
8196 + return NULL;
8197 + }
8198 +
8199 + t->t = DWC_ALLOC(sizeof(*t->t));
8200 + if (!t->t) {
8201 + DWC_ERROR("Cannot allocate memory for timer->t");
8202 + goto no_timer;
8203 + }
8204 +
8205 + t->name = DWC_STRDUP(name);
8206 + if (!t->name) {
8207 + DWC_ERROR("Cannot allocate memory for timer->name");
8208 + goto no_name;
8209 + }
8210 +
8211 +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_SPINLOCK))
8212 + DWC_SPINLOCK_ALLOC_LINUX_DEBUG(t->lock);
8213 +#else
8214 + t->lock = DWC_SPINLOCK_ALLOC();
8215 +#endif
8216 + if (!t->lock) {
8217 + DWC_ERROR("Cannot allocate memory for lock");
8218 + goto no_lock;
8219 + }
8220 +
8221 + t->scheduled = 0;
8222 + t->t->expires = jiffies;
8223 + setup_timer(t->t, timer_callback, (unsigned long)t);
8224 +
8225 + t->cb = cb;
8226 + t->data = data;
8227 +
8228 + return t;
8229 +
8230 + no_lock:
8231 + DWC_FREE(t->name);
8232 + no_name:
8233 + DWC_FREE(t->t);
8234 + no_timer:
8235 + DWC_FREE(t);
8236 + return NULL;
8237 +}
8238 +
8239 +void DWC_TIMER_FREE(dwc_timer_t *timer)
8240 +{
8241 + dwc_irqflags_t flags;
8242 +
8243 + DWC_SPINLOCK_IRQSAVE(timer->lock, &flags);
8244 +
8245 + if (timer->scheduled) {
8246 + del_timer(timer->t);
8247 + timer->scheduled = 0;
8248 + }
8249 +
8250 + DWC_SPINUNLOCK_IRQRESTORE(timer->lock, flags);
8251 + DWC_SPINLOCK_FREE(timer->lock);
8252 + DWC_FREE(timer->t);
8253 + DWC_FREE(timer->name);
8254 + DWC_FREE(timer);
8255 +}
8256 +
8257 +void DWC_TIMER_SCHEDULE(dwc_timer_t *timer, uint32_t time)
8258 +{
8259 + dwc_irqflags_t flags;
8260 +
8261 + DWC_SPINLOCK_IRQSAVE(timer->lock, &flags);
8262 +
8263 + if (!timer->scheduled) {
8264 + timer->scheduled = 1;
8265 + DWC_DEBUGC("Scheduling timer %s to expire in +%d msec", timer->name, time);
8266 + timer->t->expires = jiffies + msecs_to_jiffies(time);
8267 + add_timer(timer->t);
8268 + } else {
8269 + DWC_DEBUGC("Modifying timer %s to expire in +%d msec", timer->name, time);
8270 + mod_timer(timer->t, jiffies + msecs_to_jiffies(time));
8271 + }
8272 +
8273 + DWC_SPINUNLOCK_IRQRESTORE(timer->lock, flags);
8274 +}
8275 +
8276 +void DWC_TIMER_CANCEL(dwc_timer_t *timer)
8277 +{
8278 + del_timer(timer->t);
8279 +}
8280 +
8281 +
8282 +/* Wait Queues */
8283 +
8284 +struct dwc_waitq {
8285 + wait_queue_head_t queue;
8286 + int abort;
8287 +};
8288 +
8289 +dwc_waitq_t *DWC_WAITQ_ALLOC(void)
8290 +{
8291 + dwc_waitq_t *wq = DWC_ALLOC(sizeof(*wq));
8292 +
8293 + if (!wq) {
8294 + DWC_ERROR("Cannot allocate memory for waitqueue\n");
8295 + return NULL;
8296 + }
8297 +
8298 + init_waitqueue_head(&wq->queue);
8299 + wq->abort = 0;
8300 + return wq;
8301 +}
8302 +
8303 +void DWC_WAITQ_FREE(dwc_waitq_t *wq)
8304 +{
8305 + DWC_FREE(wq);
8306 +}
8307 +
8308 +int32_t DWC_WAITQ_WAIT(dwc_waitq_t *wq, dwc_waitq_condition_t cond, void *data)
8309 +{
8310 + int result = wait_event_interruptible(wq->queue,
8311 + cond(data) || wq->abort);
8312 + if (result == -ERESTARTSYS) {
8313 + wq->abort = 0;
8314 + return -DWC_E_RESTART;
8315 + }
8316 +
8317 + if (wq->abort == 1) {
8318 + wq->abort = 0;
8319 + return -DWC_E_ABORT;
8320 + }
8321 +
8322 + wq->abort = 0;
8323 +
8324 + if (result == 0) {
8325 + return 0;
8326 + }
8327 +
8328 + return -DWC_E_UNKNOWN;
8329 +}
8330 +
8331 +int32_t DWC_WAITQ_WAIT_TIMEOUT(dwc_waitq_t *wq, dwc_waitq_condition_t cond,
8332 + void *data, int32_t msecs)
8333 +{
8334 + int32_t tmsecs;
8335 + int result = wait_event_interruptible_timeout(wq->queue,
8336 + cond(data) || wq->abort,
8337 + msecs_to_jiffies(msecs));
8338 + if (result == -ERESTARTSYS) {
8339 + wq->abort = 0;
8340 + return -DWC_E_RESTART;
8341 + }
8342 +
8343 + if (wq->abort == 1) {
8344 + wq->abort = 0;
8345 + return -DWC_E_ABORT;
8346 + }
8347 +
8348 + wq->abort = 0;
8349 +
8350 + if (result > 0) {
8351 + tmsecs = jiffies_to_msecs(result);
8352 + if (!tmsecs) {
8353 + return 1;
8354 + }
8355 +
8356 + return tmsecs;
8357 + }
8358 +
8359 + if (result == 0) {
8360 + return -DWC_E_TIMEOUT;
8361 + }
8362 +
8363 + return -DWC_E_UNKNOWN;
8364 +}
8365 +
8366 +void DWC_WAITQ_TRIGGER(dwc_waitq_t *wq)
8367 +{
8368 + wq->abort = 0;
8369 + wake_up_interruptible(&wq->queue);
8370 +}
8371 +
8372 +void DWC_WAITQ_ABORT(dwc_waitq_t *wq)
8373 +{
8374 + wq->abort = 1;
8375 + wake_up_interruptible(&wq->queue);
8376 +}
8377 +
8378 +
8379 +/* Threading */
8380 +
8381 +dwc_thread_t *DWC_THREAD_RUN(dwc_thread_function_t func, char *name, void *data)
8382 +{
8383 + struct task_struct *thread = kthread_run(func, data, name);
8384 +
8385 + if (thread == ERR_PTR(-ENOMEM)) {
8386 + return NULL;
8387 + }
8388 +
8389 + return (dwc_thread_t *)thread;
8390 +}
8391 +
8392 +int DWC_THREAD_STOP(dwc_thread_t *thread)
8393 +{
8394 + return kthread_stop((struct task_struct *)thread);
8395 +}
8396 +
8397 +dwc_bool_t DWC_THREAD_SHOULD_STOP(void)
8398 +{
8399 + return kthread_should_stop();
8400 +}
8401 +
8402 +
8403 +/* tasklets
8404 + - run in interrupt context (cannot sleep)
8405 + - each tasklet runs on a single CPU
8406 + - different tasklets can be running simultaneously on different CPUs
8407 + */
8408 +struct dwc_tasklet {
8409 + struct tasklet_struct t;
8410 + dwc_tasklet_callback_t cb;
8411 + void *data;
8412 +};
8413 +
8414 +static void tasklet_callback(unsigned long data)
8415 +{
8416 + dwc_tasklet_t *t = (dwc_tasklet_t *)data;
8417 + t->cb(t->data);
8418 +}
8419 +
8420 +dwc_tasklet_t *DWC_TASK_ALLOC(char *name, dwc_tasklet_callback_t cb, void *data)
8421 +{
8422 + dwc_tasklet_t *t = DWC_ALLOC(sizeof(*t));
8423 +
8424 + if (t) {
8425 + t->cb = cb;
8426 + t->data = data;
8427 + tasklet_init(&t->t, tasklet_callback, (unsigned long)t);
8428 + } else {
8429 + DWC_ERROR("Cannot allocate memory for tasklet\n");
8430 + }
8431 +
8432 + return t;
8433 +}
8434 +
8435 +void DWC_TASK_FREE(dwc_tasklet_t *task)
8436 +{
8437 + DWC_FREE(task);
8438 +}
8439 +
8440 +void DWC_TASK_SCHEDULE(dwc_tasklet_t *task)
8441 +{
8442 + tasklet_schedule(&task->t);
8443 +}
8444 +
8445 +void DWC_TASK_HI_SCHEDULE(dwc_tasklet_t *task)
8446 +{
8447 + tasklet_hi_schedule(&task->t);
8448 +}
8449 +
8450 +
8451 +/* workqueues
8452 + - run in process context (can sleep)
8453 + */
8454 +typedef struct work_container {
8455 + dwc_work_callback_t cb;
8456 + void *data;
8457 + dwc_workq_t *wq;
8458 + char *name;
8459 +
8460 +#ifdef DEBUG
8461 + DWC_CIRCLEQ_ENTRY(work_container) entry;
8462 +#endif
8463 + struct delayed_work work;
8464 +} work_container_t;
8465 +
8466 +#ifdef DEBUG
8467 +DWC_CIRCLEQ_HEAD(work_container_queue, work_container);
8468 +#endif
8469 +
8470 +struct dwc_workq {
8471 + struct workqueue_struct *wq;
8472 + dwc_spinlock_t *lock;
8473 + dwc_waitq_t *waitq;
8474 + int pending;
8475 +
8476 +#ifdef DEBUG
8477 + struct work_container_queue entries;
8478 +#endif
8479 +};
8480 +
8481 +static void do_work(struct work_struct *work)
8482 +{
8483 + dwc_irqflags_t flags;
8484 + struct delayed_work *dw = container_of(work, struct delayed_work, work);
8485 + work_container_t *container = container_of(dw, struct work_container, work);
8486 + dwc_workq_t *wq = container->wq;
8487 +
8488 + container->cb(container->data);
8489 +
8490 +#ifdef DEBUG
8491 + DWC_CIRCLEQ_REMOVE(&wq->entries, container, entry);
8492 +#endif
8493 + DWC_DEBUGC("Work done: %s, container=%p", container->name, container);
8494 + if (container->name) {
8495 + DWC_FREE(container->name);
8496 + }
8497 + DWC_FREE(container);
8498 +
8499 + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
8500 + wq->pending--;
8501 + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
8502 + DWC_WAITQ_TRIGGER(wq->waitq);
8503 +}
8504 +
8505 +static int work_done(void *data)
8506 +{
8507 + dwc_workq_t *workq = (dwc_workq_t *)data;
8508 + return workq->pending == 0;
8509 +}
8510 +
8511 +int DWC_WORKQ_WAIT_WORK_DONE(dwc_workq_t *workq, int timeout)
8512 +{
8513 + return DWC_WAITQ_WAIT_TIMEOUT(workq->waitq, work_done, workq, timeout);
8514 +}
8515 +
8516 +dwc_workq_t *DWC_WORKQ_ALLOC(char *name)
8517 +{
8518 + dwc_workq_t *wq = DWC_ALLOC(sizeof(*wq));
8519 +
8520 + if (!wq) {
8521 + return NULL;
8522 + }
8523 +
8524 + wq->wq = create_singlethread_workqueue(name);
8525 + if (!wq->wq) {
8526 + goto no_wq;
8527 + }
8528 +
8529 + wq->pending = 0;
8530 +
8531 +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_SPINLOCK))
8532 + DWC_SPINLOCK_ALLOC_LINUX_DEBUG(wq->lock);
8533 +#else
8534 + wq->lock = DWC_SPINLOCK_ALLOC();
8535 +#endif
8536 + if (!wq->lock) {
8537 + goto no_lock;
8538 + }
8539 +
8540 + wq->waitq = DWC_WAITQ_ALLOC();
8541 + if (!wq->waitq) {
8542 + goto no_waitq;
8543 + }
8544 +
8545 +#ifdef DEBUG
8546 + DWC_CIRCLEQ_INIT(&wq->entries);
8547 +#endif
8548 + return wq;
8549 +
8550 + no_waitq:
8551 + DWC_SPINLOCK_FREE(wq->lock);
8552 + no_lock:
8553 + destroy_workqueue(wq->wq);
8554 + no_wq:
8555 + DWC_FREE(wq);
8556 +
8557 + return NULL;
8558 +}
8559 +
8560 +void DWC_WORKQ_FREE(dwc_workq_t *wq)
8561 +{
8562 +#ifdef DEBUG
8563 + if (wq->pending != 0) {
8564 + struct work_container *wc;
8565 + DWC_ERROR("Destroying work queue with pending work");
8566 + DWC_CIRCLEQ_FOREACH(wc, &wq->entries, entry) {
8567 + DWC_ERROR("Work %s still pending", wc->name);
8568 + }
8569 + }
8570 +#endif
8571 + destroy_workqueue(wq->wq);
8572 + DWC_SPINLOCK_FREE(wq->lock);
8573 + DWC_WAITQ_FREE(wq->waitq);
8574 + DWC_FREE(wq);
8575 +}
8576 +
8577 +void DWC_WORKQ_SCHEDULE(dwc_workq_t *wq, dwc_work_callback_t cb, void *data,
8578 + char *format, ...)
8579 +{
8580 + dwc_irqflags_t flags;
8581 + work_container_t *container;
8582 + static char name[128];
8583 + va_list args;
8584 +
8585 + va_start(args, format);
8586 + DWC_VSNPRINTF(name, 128, format, args);
8587 + va_end(args);
8588 +
8589 + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
8590 + wq->pending++;
8591 + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
8592 + DWC_WAITQ_TRIGGER(wq->waitq);
8593 +
8594 + container = DWC_ALLOC_ATOMIC(sizeof(*container));
8595 + if (!container) {
8596 + DWC_ERROR("Cannot allocate memory for container\n");
8597 + return;
8598 + }
8599 +
8600 + container->name = DWC_STRDUP(name);
8601 + if (!container->name) {
8602 + DWC_ERROR("Cannot allocate memory for container->name\n");
8603 + DWC_FREE(container);
8604 + return;
8605 + }
8606 +
8607 + container->cb = cb;
8608 + container->data = data;
8609 + container->wq = wq;
8610 + DWC_DEBUGC("Queueing work: %s, container=%p", container->name, container);
8611 + INIT_WORK(&container->work.work, do_work);
8612 +
8613 +#ifdef DEBUG
8614 + DWC_CIRCLEQ_INSERT_TAIL(&wq->entries, container, entry);
8615 +#endif
8616 + queue_work(wq->wq, &container->work.work);
8617 +}
8618 +
8619 +void DWC_WORKQ_SCHEDULE_DELAYED(dwc_workq_t *wq, dwc_work_callback_t cb,
8620 + void *data, uint32_t time, char *format, ...)
8621 +{
8622 + dwc_irqflags_t flags;
8623 + work_container_t *container;
8624 + static char name[128];
8625 + va_list args;
8626 +
8627 + va_start(args, format);
8628 + DWC_VSNPRINTF(name, 128, format, args);
8629 + va_end(args);
8630 +
8631 + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
8632 + wq->pending++;
8633 + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
8634 + DWC_WAITQ_TRIGGER(wq->waitq);
8635 +
8636 + container = DWC_ALLOC_ATOMIC(sizeof(*container));
8637 + if (!container) {
8638 + DWC_ERROR("Cannot allocate memory for container\n");
8639 + return;
8640 + }
8641 +
8642 + container->name = DWC_STRDUP(name);
8643 + if (!container->name) {
8644 + DWC_ERROR("Cannot allocate memory for container->name\n");
8645 + DWC_FREE(container);
8646 + return;
8647 + }
8648 +
8649 + container->cb = cb;
8650 + container->data = data;
8651 + container->wq = wq;
8652 + DWC_DEBUGC("Queueing work: %s, container=%p", container->name, container);
8653 + INIT_DELAYED_WORK(&container->work, do_work);
8654 +
8655 +#ifdef DEBUG
8656 + DWC_CIRCLEQ_INSERT_TAIL(&wq->entries, container, entry);
8657 +#endif
8658 + queue_delayed_work(wq->wq, &container->work, msecs_to_jiffies(time));
8659 +}
8660 +
8661 +int DWC_WORKQ_PENDING(dwc_workq_t *wq)
8662 +{
8663 + return wq->pending;
8664 +}
8665 +
8666 +
8667 +#ifdef DWC_LIBMODULE
8668 +
8669 +#ifdef DWC_CCLIB
8670 +/* CC */
8671 +EXPORT_SYMBOL(dwc_cc_if_alloc);
8672 +EXPORT_SYMBOL(dwc_cc_if_free);
8673 +EXPORT_SYMBOL(dwc_cc_clear);
8674 +EXPORT_SYMBOL(dwc_cc_add);
8675 +EXPORT_SYMBOL(dwc_cc_remove);
8676 +EXPORT_SYMBOL(dwc_cc_change);
8677 +EXPORT_SYMBOL(dwc_cc_data_for_save);
8678 +EXPORT_SYMBOL(dwc_cc_restore_from_data);
8679 +EXPORT_SYMBOL(dwc_cc_match_chid);
8680 +EXPORT_SYMBOL(dwc_cc_match_cdid);
8681 +EXPORT_SYMBOL(dwc_cc_ck);
8682 +EXPORT_SYMBOL(dwc_cc_chid);
8683 +EXPORT_SYMBOL(dwc_cc_cdid);
8684 +EXPORT_SYMBOL(dwc_cc_name);
8685 +#endif /* DWC_CCLIB */
8686 +
8687 +#ifdef DWC_CRYPTOLIB
8688 +# ifndef CONFIG_MACH_IPMATE
8689 +/* Modpow */
8690 +EXPORT_SYMBOL(dwc_modpow);
8691 +
8692 +/* DH */
8693 +EXPORT_SYMBOL(dwc_dh_modpow);
8694 +EXPORT_SYMBOL(dwc_dh_derive_keys);
8695 +EXPORT_SYMBOL(dwc_dh_pk);
8696 +# endif /* CONFIG_MACH_IPMATE */
8697 +
8698 +/* Crypto */
8699 +EXPORT_SYMBOL(dwc_wusb_aes_encrypt);
8700 +EXPORT_SYMBOL(dwc_wusb_cmf);
8701 +EXPORT_SYMBOL(dwc_wusb_prf);
8702 +EXPORT_SYMBOL(dwc_wusb_fill_ccm_nonce);
8703 +EXPORT_SYMBOL(dwc_wusb_gen_nonce);
8704 +EXPORT_SYMBOL(dwc_wusb_gen_key);
8705 +EXPORT_SYMBOL(dwc_wusb_gen_mic);
8706 +#endif /* DWC_CRYPTOLIB */
8707 +
8708 +/* Notification */
8709 +#ifdef DWC_NOTIFYLIB
8710 +EXPORT_SYMBOL(dwc_alloc_notification_manager);
8711 +EXPORT_SYMBOL(dwc_free_notification_manager);
8712 +EXPORT_SYMBOL(dwc_register_notifier);
8713 +EXPORT_SYMBOL(dwc_unregister_notifier);
8714 +EXPORT_SYMBOL(dwc_add_observer);
8715 +EXPORT_SYMBOL(dwc_remove_observer);
8716 +EXPORT_SYMBOL(dwc_notify);
8717 +#endif
8718 +
8719 +/* Memory Debugging Routines */
8720 +#ifdef DWC_DEBUG_MEMORY
8721 +EXPORT_SYMBOL(dwc_alloc_debug);
8722 +EXPORT_SYMBOL(dwc_alloc_atomic_debug);
8723 +EXPORT_SYMBOL(dwc_free_debug);
8724 +EXPORT_SYMBOL(dwc_dma_alloc_debug);
8725 +EXPORT_SYMBOL(dwc_dma_free_debug);
8726 +#endif
8727 +
8728 +EXPORT_SYMBOL(DWC_MEMSET);
8729 +EXPORT_SYMBOL(DWC_MEMCPY);
8730 +EXPORT_SYMBOL(DWC_MEMMOVE);
8731 +EXPORT_SYMBOL(DWC_MEMCMP);
8732 +EXPORT_SYMBOL(DWC_STRNCMP);
8733 +EXPORT_SYMBOL(DWC_STRCMP);
8734 +EXPORT_SYMBOL(DWC_STRLEN);
8735 +EXPORT_SYMBOL(DWC_STRCPY);
8736 +EXPORT_SYMBOL(DWC_STRDUP);
8737 +EXPORT_SYMBOL(DWC_ATOI);
8738 +EXPORT_SYMBOL(DWC_ATOUI);
8739 +
8740 +#ifdef DWC_UTFLIB
8741 +EXPORT_SYMBOL(DWC_UTF8_TO_UTF16LE);
8742 +#endif /* DWC_UTFLIB */
8743 +
8744 +EXPORT_SYMBOL(DWC_IN_IRQ);
8745 +EXPORT_SYMBOL(DWC_IN_BH);
8746 +EXPORT_SYMBOL(DWC_VPRINTF);
8747 +EXPORT_SYMBOL(DWC_VSNPRINTF);
8748 +EXPORT_SYMBOL(DWC_PRINTF);
8749 +EXPORT_SYMBOL(DWC_SPRINTF);
8750 +EXPORT_SYMBOL(DWC_SNPRINTF);
8751 +EXPORT_SYMBOL(__DWC_WARN);
8752 +EXPORT_SYMBOL(__DWC_ERROR);
8753 +EXPORT_SYMBOL(DWC_EXCEPTION);
8754 +
8755 +#ifdef DEBUG
8756 +EXPORT_SYMBOL(__DWC_DEBUG);
8757 +#endif
8758 +
8759 +EXPORT_SYMBOL(__DWC_DMA_ALLOC);
8760 +EXPORT_SYMBOL(__DWC_DMA_ALLOC_ATOMIC);
8761 +EXPORT_SYMBOL(__DWC_DMA_FREE);
8762 +EXPORT_SYMBOL(__DWC_ALLOC);
8763 +EXPORT_SYMBOL(__DWC_ALLOC_ATOMIC);
8764 +EXPORT_SYMBOL(__DWC_FREE);
8765 +
8766 +#ifdef DWC_CRYPTOLIB
8767 +EXPORT_SYMBOL(DWC_RANDOM_BYTES);
8768 +EXPORT_SYMBOL(DWC_AES_CBC);
8769 +EXPORT_SYMBOL(DWC_SHA256);
8770 +EXPORT_SYMBOL(DWC_HMAC_SHA256);
8771 +#endif
8772 +
8773 +EXPORT_SYMBOL(DWC_CPU_TO_LE32);
8774 +EXPORT_SYMBOL(DWC_CPU_TO_BE32);
8775 +EXPORT_SYMBOL(DWC_LE32_TO_CPU);
8776 +EXPORT_SYMBOL(DWC_BE32_TO_CPU);
8777 +EXPORT_SYMBOL(DWC_CPU_TO_LE16);
8778 +EXPORT_SYMBOL(DWC_CPU_TO_BE16);
8779 +EXPORT_SYMBOL(DWC_LE16_TO_CPU);
8780 +EXPORT_SYMBOL(DWC_BE16_TO_CPU);
8781 +EXPORT_SYMBOL(DWC_READ_REG32);
8782 +EXPORT_SYMBOL(DWC_WRITE_REG32);
8783 +EXPORT_SYMBOL(DWC_MODIFY_REG32);
8784 +
8785 +#if 0
8786 +EXPORT_SYMBOL(DWC_READ_REG64);
8787 +EXPORT_SYMBOL(DWC_WRITE_REG64);
8788 +EXPORT_SYMBOL(DWC_MODIFY_REG64);
8789 +#endif
8790 +
8791 +EXPORT_SYMBOL(DWC_SPINLOCK_ALLOC);
8792 +EXPORT_SYMBOL(DWC_SPINLOCK_FREE);
8793 +EXPORT_SYMBOL(DWC_SPINLOCK);
8794 +EXPORT_SYMBOL(DWC_SPINUNLOCK);
8795 +EXPORT_SYMBOL(DWC_SPINLOCK_IRQSAVE);
8796 +EXPORT_SYMBOL(DWC_SPINUNLOCK_IRQRESTORE);
8797 +EXPORT_SYMBOL(DWC_MUTEX_ALLOC);
8798 +
8799 +#if (!defined(DWC_LINUX) || !defined(CONFIG_DEBUG_MUTEXES))
8800 +EXPORT_SYMBOL(DWC_MUTEX_FREE);
8801 +#endif
8802 +
8803 +EXPORT_SYMBOL(DWC_MUTEX_LOCK);
8804 +EXPORT_SYMBOL(DWC_MUTEX_TRYLOCK);
8805 +EXPORT_SYMBOL(DWC_MUTEX_UNLOCK);
8806 +EXPORT_SYMBOL(DWC_UDELAY);
8807 +EXPORT_SYMBOL(DWC_MDELAY);
8808 +EXPORT_SYMBOL(DWC_MSLEEP);
8809 +EXPORT_SYMBOL(DWC_TIME);
8810 +EXPORT_SYMBOL(DWC_TIMER_ALLOC);
8811 +EXPORT_SYMBOL(DWC_TIMER_FREE);
8812 +EXPORT_SYMBOL(DWC_TIMER_SCHEDULE);
8813 +EXPORT_SYMBOL(DWC_TIMER_CANCEL);
8814 +EXPORT_SYMBOL(DWC_WAITQ_ALLOC);
8815 +EXPORT_SYMBOL(DWC_WAITQ_FREE);
8816 +EXPORT_SYMBOL(DWC_WAITQ_WAIT);
8817 +EXPORT_SYMBOL(DWC_WAITQ_WAIT_TIMEOUT);
8818 +EXPORT_SYMBOL(DWC_WAITQ_TRIGGER);
8819 +EXPORT_SYMBOL(DWC_WAITQ_ABORT);
8820 +EXPORT_SYMBOL(DWC_THREAD_RUN);
8821 +EXPORT_SYMBOL(DWC_THREAD_STOP);
8822 +EXPORT_SYMBOL(DWC_THREAD_SHOULD_STOP);
8823 +EXPORT_SYMBOL(DWC_TASK_ALLOC);
8824 +EXPORT_SYMBOL(DWC_TASK_FREE);
8825 +EXPORT_SYMBOL(DWC_TASK_SCHEDULE);
8826 +EXPORT_SYMBOL(DWC_WORKQ_WAIT_WORK_DONE);
8827 +EXPORT_SYMBOL(DWC_WORKQ_ALLOC);
8828 +EXPORT_SYMBOL(DWC_WORKQ_FREE);
8829 +EXPORT_SYMBOL(DWC_WORKQ_SCHEDULE);
8830 +EXPORT_SYMBOL(DWC_WORKQ_SCHEDULE_DELAYED);
8831 +EXPORT_SYMBOL(DWC_WORKQ_PENDING);
8832 +
8833 +static int dwc_common_port_init_module(void)
8834 +{
8835 + int result = 0;
8836 +
8837 + printk(KERN_DEBUG "Module dwc_common_port init\n" );
8838 +
8839 +#ifdef DWC_DEBUG_MEMORY
8840 + result = dwc_memory_debug_start(NULL);
8841 + if (result) {
8842 + printk(KERN_ERR
8843 + "dwc_memory_debug_start() failed with error %d\n",
8844 + result);
8845 + return result;
8846 + }
8847 +#endif
8848 +
8849 +#ifdef DWC_NOTIFYLIB
8850 + result = dwc_alloc_notification_manager(NULL, NULL);
8851 + if (result) {
8852 + printk(KERN_ERR
8853 + "dwc_alloc_notification_manager() failed with error %d\n",
8854 + result);
8855 + return result;
8856 + }
8857 +#endif
8858 + return result;
8859 +}
8860 +
8861 +static void dwc_common_port_exit_module(void)
8862 +{
8863 + printk(KERN_DEBUG "Module dwc_common_port exit\n" );
8864 +
8865 +#ifdef DWC_NOTIFYLIB
8866 + dwc_free_notification_manager();
8867 +#endif
8868 +
8869 +#ifdef DWC_DEBUG_MEMORY
8870 + dwc_memory_debug_stop();
8871 +#endif
8872 +}
8873 +
8874 +module_init(dwc_common_port_init_module);
8875 +module_exit(dwc_common_port_exit_module);
8876 +
8877 +MODULE_DESCRIPTION("DWC Common Library - Portable version");
8878 +MODULE_AUTHOR("Synopsys Inc.");
8879 +MODULE_LICENSE ("GPL");
8880 +
8881 +#endif /* DWC_LIBMODULE */
8882 --- /dev/null
8883 +++ b/drivers/usb/host/dwc_common_port/dwc_common_nbsd.c
8884 @@ -0,0 +1,1275 @@
8885 +#include "dwc_os.h"
8886 +#include "dwc_list.h"
8887 +
8888 +#ifdef DWC_CCLIB
8889 +# include "dwc_cc.h"
8890 +#endif
8891 +
8892 +#ifdef DWC_CRYPTOLIB
8893 +# include "dwc_modpow.h"
8894 +# include "dwc_dh.h"
8895 +# include "dwc_crypto.h"
8896 +#endif
8897 +
8898 +#ifdef DWC_NOTIFYLIB
8899 +# include "dwc_notifier.h"
8900 +#endif
8901 +
8902 +/* OS-Level Implementations */
8903 +
8904 +/* This is the NetBSD 4.0.1 kernel implementation of the DWC platform library. */
8905 +
8906 +
8907 +/* MISC */
8908 +
8909 +void *DWC_MEMSET(void *dest, uint8_t byte, uint32_t size)
8910 +{
8911 + return memset(dest, byte, size);
8912 +}
8913 +
8914 +void *DWC_MEMCPY(void *dest, void const *src, uint32_t size)
8915 +{
8916 + return memcpy(dest, src, size);
8917 +}
8918 +
8919 +void *DWC_MEMMOVE(void *dest, void *src, uint32_t size)
8920 +{
8921 + bcopy(src, dest, size);
8922 + return dest;
8923 +}
8924 +
8925 +int DWC_MEMCMP(void *m1, void *m2, uint32_t size)
8926 +{
8927 + return memcmp(m1, m2, size);
8928 +}
8929 +
8930 +int DWC_STRNCMP(void *s1, void *s2, uint32_t size)
8931 +{
8932 + return strncmp(s1, s2, size);
8933 +}
8934 +
8935 +int DWC_STRCMP(void *s1, void *s2)
8936 +{
8937 + return strcmp(s1, s2);
8938 +}
8939 +
8940 +int DWC_STRLEN(char const *str)
8941 +{
8942 + return strlen(str);
8943 +}
8944 +
8945 +char *DWC_STRCPY(char *to, char const *from)
8946 +{
8947 + return strcpy(to, from);
8948 +}
8949 +
8950 +char *DWC_STRDUP(char const *str)
8951 +{
8952 + int len = DWC_STRLEN(str) + 1;
8953 + char *new = DWC_ALLOC_ATOMIC(len);
8954 +
8955 + if (!new) {
8956 + return NULL;
8957 + }
8958 +
8959 + DWC_MEMCPY(new, str, len);
8960 + return new;
8961 +}
8962 +
8963 +int DWC_ATOI(char *str, int32_t *value)
8964 +{
8965 + char *end = NULL;
8966 +
8967 + /* NetBSD doesn't have 'strtol' in the kernel, but 'strtoul'
8968 + * should be equivalent on 2's complement machines
8969 + */
8970 + *value = strtoul(str, &end, 0);
8971 + if (*end == '\0') {
8972 + return 0;
8973 + }
8974 +
8975 + return -1;
8976 +}
8977 +
8978 +int DWC_ATOUI(char *str, uint32_t *value)
8979 +{
8980 + char *end = NULL;
8981 +
8982 + *value = strtoul(str, &end, 0);
8983 + if (*end == '\0') {
8984 + return 0;
8985 + }
8986 +
8987 + return -1;
8988 +}
8989 +
8990 +
8991 +#ifdef DWC_UTFLIB
8992 +/* From usbstring.c */
8993 +
8994 +int DWC_UTF8_TO_UTF16LE(uint8_t const *s, uint16_t *cp, unsigned len)
8995 +{
8996 + int count = 0;
8997 + u8 c;
8998 + u16 uchar;
8999 +
9000 + /* this insists on correct encodings, though not minimal ones.
9001 + * BUT it currently rejects legit 4-byte UTF-8 code points,
9002 + * which need surrogate pairs. (Unicode 3.1 can use them.)
9003 + */
9004 + while (len != 0 && (c = (u8) *s++) != 0) {
9005 + if (unlikely(c & 0x80)) {
9006 + // 2-byte sequence:
9007 + // 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx
9008 + if ((c & 0xe0) == 0xc0) {
9009 + uchar = (c & 0x1f) << 6;
9010 +
9011 + c = (u8) *s++;
9012 + if ((c & 0xc0) != 0xc0)
9013 + goto fail;
9014 + c &= 0x3f;
9015 + uchar |= c;
9016 +
9017 + // 3-byte sequence (most CJKV characters):
9018 + // zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx
9019 + } else if ((c & 0xf0) == 0xe0) {
9020 + uchar = (c & 0x0f) << 12;
9021 +
9022 + c = (u8) *s++;
9023 + if ((c & 0xc0) != 0xc0)
9024 + goto fail;
9025 + c &= 0x3f;
9026 + uchar |= c << 6;
9027 +
9028 + c = (u8) *s++;
9029 + if ((c & 0xc0) != 0xc0)
9030 + goto fail;
9031 + c &= 0x3f;
9032 + uchar |= c;
9033 +
9034 + /* no bogus surrogates */
9035 + if (0xd800 <= uchar && uchar <= 0xdfff)
9036 + goto fail;
9037 +
9038 + // 4-byte sequence (surrogate pairs, currently rare):
9039 + // 11101110wwwwzzzzyy + 110111yyyyxxxxxx
9040 + // = 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx
9041 + // (uuuuu = wwww + 1)
9042 + // FIXME accept the surrogate code points (only)
9043 + } else
9044 + goto fail;
9045 + } else
9046 + uchar = c;
9047 + put_unaligned (cpu_to_le16 (uchar), cp++);
9048 + count++;
9049 + len--;
9050 + }
9051 + return count;
9052 +fail:
9053 + return -1;
9054 +}
9055 +
9056 +#endif /* DWC_UTFLIB */
9057 +
9058 +
9059 +/* dwc_debug.h */
9060 +
9061 +dwc_bool_t DWC_IN_IRQ(void)
9062 +{
9063 +// return in_irq();
9064 + return 0;
9065 +}
9066 +
9067 +dwc_bool_t DWC_IN_BH(void)
9068 +{
9069 +// return in_softirq();
9070 + return 0;
9071 +}
9072 +
9073 +void DWC_VPRINTF(char *format, va_list args)
9074 +{
9075 + vprintf(format, args);
9076 +}
9077 +
9078 +int DWC_VSNPRINTF(char *str, int size, char *format, va_list args)
9079 +{
9080 + return vsnprintf(str, size, format, args);
9081 +}
9082 +
9083 +void DWC_PRINTF(char *format, ...)
9084 +{
9085 + va_list args;
9086 +
9087 + va_start(args, format);
9088 + DWC_VPRINTF(format, args);
9089 + va_end(args);
9090 +}
9091 +
9092 +int DWC_SPRINTF(char *buffer, char *format, ...)
9093 +{
9094 + int retval;
9095 + va_list args;
9096 +
9097 + va_start(args, format);
9098 + retval = vsprintf(buffer, format, args);
9099 + va_end(args);
9100 + return retval;
9101 +}
9102 +
9103 +int DWC_SNPRINTF(char *buffer, int size, char *format, ...)
9104 +{
9105 + int retval;
9106 + va_list args;
9107 +
9108 + va_start(args, format);
9109 + retval = vsnprintf(buffer, size, format, args);
9110 + va_end(args);
9111 + return retval;
9112 +}
9113 +
9114 +void __DWC_WARN(char *format, ...)
9115 +{
9116 + va_list args;
9117 +
9118 + va_start(args, format);
9119 + DWC_VPRINTF(format, args);
9120 + va_end(args);
9121 +}
9122 +
9123 +void __DWC_ERROR(char *format, ...)
9124 +{
9125 + va_list args;
9126 +
9127 + va_start(args, format);
9128 + DWC_VPRINTF(format, args);
9129 + va_end(args);
9130 +}
9131 +
9132 +void DWC_EXCEPTION(char *format, ...)
9133 +{
9134 + va_list args;
9135 +
9136 + va_start(args, format);
9137 + DWC_VPRINTF(format, args);
9138 + va_end(args);
9139 +// BUG_ON(1); ???
9140 +}
9141 +
9142 +#ifdef DEBUG
9143 +void __DWC_DEBUG(char *format, ...)
9144 +{
9145 + va_list args;
9146 +
9147 + va_start(args, format);
9148 + DWC_VPRINTF(format, args);
9149 + va_end(args);
9150 +}
9151 +#endif
9152 +
9153 +
9154 +/* dwc_mem.h */
9155 +
9156 +#if 0
9157 +dwc_pool_t *DWC_DMA_POOL_CREATE(uint32_t size,
9158 + uint32_t align,
9159 + uint32_t alloc)
9160 +{
9161 + struct dma_pool *pool = dma_pool_create("Pool", NULL,
9162 + size, align, alloc);
9163 + return (dwc_pool_t *)pool;
9164 +}
9165 +
9166 +void DWC_DMA_POOL_DESTROY(dwc_pool_t *pool)
9167 +{
9168 + dma_pool_destroy((struct dma_pool *)pool);
9169 +}
9170 +
9171 +void *DWC_DMA_POOL_ALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
9172 +{
9173 +// return dma_pool_alloc((struct dma_pool *)pool, GFP_KERNEL, dma_addr);
9174 + return dma_pool_alloc((struct dma_pool *)pool, M_WAITOK, dma_addr);
9175 +}
9176 +
9177 +void *DWC_DMA_POOL_ZALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
9178 +{
9179 + void *vaddr = DWC_DMA_POOL_ALLOC(pool, dma_addr);
9180 + memset(..);
9181 +}
9182 +
9183 +void DWC_DMA_POOL_FREE(dwc_pool_t *pool, void *vaddr, void *daddr)
9184 +{
9185 + dma_pool_free(pool, vaddr, daddr);
9186 +}
9187 +#endif
9188 +
9189 +void *__DWC_DMA_ALLOC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr)
9190 +{
9191 + dwc_dmactx_t *dma = (dwc_dmactx_t *)dma_ctx;
9192 + int error;
9193 +
9194 + error = bus_dmamem_alloc(dma->dma_tag, size, 1, size, dma->segs,
9195 + sizeof(dma->segs) / sizeof(dma->segs[0]),
9196 + &dma->nsegs, BUS_DMA_NOWAIT);
9197 + if (error) {
9198 + printf("%s: bus_dmamem_alloc(%ju) failed: %d\n", __func__,
9199 + (uintmax_t)size, error);
9200 + goto fail_0;
9201 + }
9202 +
9203 + error = bus_dmamem_map(dma->dma_tag, dma->segs, dma->nsegs, size,
9204 + (caddr_t *)&dma->dma_vaddr,
9205 + BUS_DMA_NOWAIT | BUS_DMA_COHERENT);
9206 + if (error) {
9207 + printf("%s: bus_dmamem_map failed: %d\n", __func__, error);
9208 + goto fail_1;
9209 + }
9210 +
9211 + error = bus_dmamap_create(dma->dma_tag, size, 1, size, 0,
9212 + BUS_DMA_NOWAIT, &dma->dma_map);
9213 + if (error) {
9214 + printf("%s: bus_dmamap_create failed: %d\n", __func__, error);
9215 + goto fail_2;
9216 + }
9217 +
9218 + error = bus_dmamap_load(dma->dma_tag, dma->dma_map, dma->dma_vaddr,
9219 + size, NULL, BUS_DMA_NOWAIT);
9220 + if (error) {
9221 + printf("%s: bus_dmamap_load failed: %d\n", __func__, error);
9222 + goto fail_3;
9223 + }
9224 +
9225 + dma->dma_paddr = (bus_addr_t)dma->segs[0].ds_addr;
9226 + *dma_addr = dma->dma_paddr;
9227 + return dma->dma_vaddr;
9228 +
9229 +fail_3:
9230 + bus_dmamap_destroy(dma->dma_tag, dma->dma_map);
9231 +fail_2:
9232 + bus_dmamem_unmap(dma->dma_tag, dma->dma_vaddr, size);
9233 +fail_1:
9234 + bus_dmamem_free(dma->dma_tag, dma->segs, dma->nsegs);
9235 +fail_0:
9236 + dma->dma_map = NULL;
9237 + dma->dma_vaddr = NULL;
9238 + dma->nsegs = 0;
9239 +
9240 + return NULL;
9241 +}
9242 +
9243 +void __DWC_DMA_FREE(void *dma_ctx, uint32_t size, void *virt_addr, dwc_dma_t dma_addr)
9244 +{
9245 + dwc_dmactx_t *dma = (dwc_dmactx_t *)dma_ctx;
9246 +
9247 + if (dma->dma_map != NULL) {
9248 + bus_dmamap_sync(dma->dma_tag, dma->dma_map, 0, size,
9249 + BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
9250 + bus_dmamap_unload(dma->dma_tag, dma->dma_map);
9251 + bus_dmamap_destroy(dma->dma_tag, dma->dma_map);
9252 + bus_dmamem_unmap(dma->dma_tag, dma->dma_vaddr, size);
9253 + bus_dmamem_free(dma->dma_tag, dma->segs, dma->nsegs);
9254 + dma->dma_paddr = 0;
9255 + dma->dma_map = NULL;
9256 + dma->dma_vaddr = NULL;
9257 + dma->nsegs = 0;
9258 + }
9259 +}
9260 +
9261 +void *__DWC_ALLOC(void *mem_ctx, uint32_t size)
9262 +{
9263 + return malloc(size, M_DEVBUF, M_WAITOK | M_ZERO);
9264 +}
9265 +
9266 +void *__DWC_ALLOC_ATOMIC(void *mem_ctx, uint32_t size)
9267 +{
9268 + return malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO);
9269 +}
9270 +
9271 +void __DWC_FREE(void *mem_ctx, void *addr)
9272 +{
9273 + free(addr, M_DEVBUF);
9274 +}
9275 +
9276 +
9277 +#ifdef DWC_CRYPTOLIB
9278 +/* dwc_crypto.h */
9279 +
9280 +void DWC_RANDOM_BYTES(uint8_t *buffer, uint32_t length)
9281 +{
9282 + get_random_bytes(buffer, length);
9283 +}
9284 +
9285 +int DWC_AES_CBC(uint8_t *message, uint32_t messagelen, uint8_t *key, uint32_t keylen, uint8_t iv[16], uint8_t *out)
9286 +{
9287 + struct crypto_blkcipher *tfm;
9288 + struct blkcipher_desc desc;
9289 + struct scatterlist sgd;
9290 + struct scatterlist sgs;
9291 +
9292 + tfm = crypto_alloc_blkcipher("cbc(aes)", 0, CRYPTO_ALG_ASYNC);
9293 + if (tfm == NULL) {
9294 + printk("failed to load transform for aes CBC\n");
9295 + return -1;
9296 + }
9297 +
9298 + crypto_blkcipher_setkey(tfm, key, keylen);
9299 + crypto_blkcipher_set_iv(tfm, iv, 16);
9300 +
9301 + sg_init_one(&sgd, out, messagelen);
9302 + sg_init_one(&sgs, message, messagelen);
9303 +
9304 + desc.tfm = tfm;
9305 + desc.flags = 0;
9306 +
9307 + if (crypto_blkcipher_encrypt(&desc, &sgd, &sgs, messagelen)) {
9308 + crypto_free_blkcipher(tfm);
9309 + DWC_ERROR("AES CBC encryption failed");
9310 + return -1;
9311 + }
9312 +
9313 + crypto_free_blkcipher(tfm);
9314 + return 0;
9315 +}
9316 +
9317 +int DWC_SHA256(uint8_t *message, uint32_t len, uint8_t *out)
9318 +{
9319 + struct crypto_hash *tfm;
9320 + struct hash_desc desc;
9321 + struct scatterlist sg;
9322 +
9323 + tfm = crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC);
9324 + if (IS_ERR(tfm)) {
9325 + DWC_ERROR("Failed to load transform for sha256: %ld", PTR_ERR(tfm));
9326 + return 0;
9327 + }
9328 + desc.tfm = tfm;
9329 + desc.flags = 0;
9330 +
9331 + sg_init_one(&sg, message, len);
9332 + crypto_hash_digest(&desc, &sg, len, out);
9333 + crypto_free_hash(tfm);
9334 +
9335 + return 1;
9336 +}
9337 +
9338 +int DWC_HMAC_SHA256(uint8_t *message, uint32_t messagelen,
9339 + uint8_t *key, uint32_t keylen, uint8_t *out)
9340 +{
9341 + struct crypto_hash *tfm;
9342 + struct hash_desc desc;
9343 + struct scatterlist sg;
9344 +
9345 + tfm = crypto_alloc_hash("hmac(sha256)", 0, CRYPTO_ALG_ASYNC);
9346 + if (IS_ERR(tfm)) {
9347 + DWC_ERROR("Failed to load transform for hmac(sha256): %ld", PTR_ERR(tfm));
9348 + return 0;
9349 + }
9350 + desc.tfm = tfm;
9351 + desc.flags = 0;
9352 +
9353 + sg_init_one(&sg, message, messagelen);
9354 + crypto_hash_setkey(tfm, key, keylen);
9355 + crypto_hash_digest(&desc, &sg, messagelen, out);
9356 + crypto_free_hash(tfm);
9357 +
9358 + return 1;
9359 +}
9360 +
9361 +#endif /* DWC_CRYPTOLIB */
9362 +
9363 +
9364 +/* Byte Ordering Conversions */
9365 +
9366 +uint32_t DWC_CPU_TO_LE32(uint32_t *p)
9367 +{
9368 +#ifdef __LITTLE_ENDIAN
9369 + return *p;
9370 +#else
9371 + uint8_t *u_p = (uint8_t *)p;
9372 +
9373 + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
9374 +#endif
9375 +}
9376 +
9377 +uint32_t DWC_CPU_TO_BE32(uint32_t *p)
9378 +{
9379 +#ifdef __BIG_ENDIAN
9380 + return *p;
9381 +#else
9382 + uint8_t *u_p = (uint8_t *)p;
9383 +
9384 + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
9385 +#endif
9386 +}
9387 +
9388 +uint32_t DWC_LE32_TO_CPU(uint32_t *p)
9389 +{
9390 +#ifdef __LITTLE_ENDIAN
9391 + return *p;
9392 +#else
9393 + uint8_t *u_p = (uint8_t *)p;
9394 +
9395 + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
9396 +#endif
9397 +}
9398 +
9399 +uint32_t DWC_BE32_TO_CPU(uint32_t *p)
9400 +{
9401 +#ifdef __BIG_ENDIAN
9402 + return *p;
9403 +#else
9404 + uint8_t *u_p = (uint8_t *)p;
9405 +
9406 + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
9407 +#endif
9408 +}
9409 +
9410 +uint16_t DWC_CPU_TO_LE16(uint16_t *p)
9411 +{
9412 +#ifdef __LITTLE_ENDIAN
9413 + return *p;
9414 +#else
9415 + uint8_t *u_p = (uint8_t *)p;
9416 + return (u_p[1] | (u_p[0] << 8));
9417 +#endif
9418 +}
9419 +
9420 +uint16_t DWC_CPU_TO_BE16(uint16_t *p)
9421 +{
9422 +#ifdef __BIG_ENDIAN
9423 + return *p;
9424 +#else
9425 + uint8_t *u_p = (uint8_t *)p;
9426 + return (u_p[1] | (u_p[0] << 8));
9427 +#endif
9428 +}
9429 +
9430 +uint16_t DWC_LE16_TO_CPU(uint16_t *p)
9431 +{
9432 +#ifdef __LITTLE_ENDIAN
9433 + return *p;
9434 +#else
9435 + uint8_t *u_p = (uint8_t *)p;
9436 + return (u_p[1] | (u_p[0] << 8));
9437 +#endif
9438 +}
9439 +
9440 +uint16_t DWC_BE16_TO_CPU(uint16_t *p)
9441 +{
9442 +#ifdef __BIG_ENDIAN
9443 + return *p;
9444 +#else
9445 + uint8_t *u_p = (uint8_t *)p;
9446 + return (u_p[1] | (u_p[0] << 8));
9447 +#endif
9448 +}
9449 +
9450 +
9451 +/* Registers */
9452 +
9453 +uint32_t DWC_READ_REG32(void *io_ctx, uint32_t volatile *reg)
9454 +{
9455 + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
9456 + bus_size_t ior = (bus_size_t)reg;
9457 +
9458 + return bus_space_read_4(io->iot, io->ioh, ior);
9459 +}
9460 +
9461 +#if 0
9462 +uint64_t DWC_READ_REG64(void *io_ctx, uint64_t volatile *reg)
9463 +{
9464 + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
9465 + bus_size_t ior = (bus_size_t)reg;
9466 +
9467 + return bus_space_read_8(io->iot, io->ioh, ior);
9468 +}
9469 +#endif
9470 +
9471 +void DWC_WRITE_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t value)
9472 +{
9473 + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
9474 + bus_size_t ior = (bus_size_t)reg;
9475 +
9476 + bus_space_write_4(io->iot, io->ioh, ior, value);
9477 +}
9478 +
9479 +#if 0
9480 +void DWC_WRITE_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t value)
9481 +{
9482 + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
9483 + bus_size_t ior = (bus_size_t)reg;
9484 +
9485 + bus_space_write_8(io->iot, io->ioh, ior, value);
9486 +}
9487 +#endif
9488 +
9489 +void DWC_MODIFY_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t clear_mask,
9490 + uint32_t set_mask)
9491 +{
9492 + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
9493 + bus_size_t ior = (bus_size_t)reg;
9494 +
9495 + bus_space_write_4(io->iot, io->ioh, ior,
9496 + (bus_space_read_4(io->iot, io->ioh, ior) &
9497 + ~clear_mask) | set_mask);
9498 +}
9499 +
9500 +#if 0
9501 +void DWC_MODIFY_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t clear_mask,
9502 + uint64_t set_mask)
9503 +{
9504 + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
9505 + bus_size_t ior = (bus_size_t)reg;
9506 +
9507 + bus_space_write_8(io->iot, io->ioh, ior,
9508 + (bus_space_read_8(io->iot, io->ioh, ior) &
9509 + ~clear_mask) | set_mask);
9510 +}
9511 +#endif
9512 +
9513 +
9514 +/* Locking */
9515 +
9516 +dwc_spinlock_t *DWC_SPINLOCK_ALLOC(void)
9517 +{
9518 + struct simplelock *sl = DWC_ALLOC(sizeof(*sl));
9519 +
9520 + if (!sl) {
9521 + DWC_ERROR("Cannot allocate memory for spinlock");
9522 + return NULL;
9523 + }
9524 +
9525 + simple_lock_init(sl);
9526 + return (dwc_spinlock_t *)sl;
9527 +}
9528 +
9529 +void DWC_SPINLOCK_FREE(dwc_spinlock_t *lock)
9530 +{
9531 + struct simplelock *sl = (struct simplelock *)lock;
9532 +
9533 + DWC_FREE(sl);
9534 +}
9535 +
9536 +void DWC_SPINLOCK(dwc_spinlock_t *lock)
9537 +{
9538 + simple_lock((struct simplelock *)lock);
9539 +}
9540 +
9541 +void DWC_SPINUNLOCK(dwc_spinlock_t *lock)
9542 +{
9543 + simple_unlock((struct simplelock *)lock);
9544 +}
9545 +
9546 +void DWC_SPINLOCK_IRQSAVE(dwc_spinlock_t *lock, dwc_irqflags_t *flags)
9547 +{
9548 + simple_lock((struct simplelock *)lock);
9549 + *flags = splbio();
9550 +}
9551 +
9552 +void DWC_SPINUNLOCK_IRQRESTORE(dwc_spinlock_t *lock, dwc_irqflags_t flags)
9553 +{
9554 + splx(flags);
9555 + simple_unlock((struct simplelock *)lock);
9556 +}
9557 +
9558 +dwc_mutex_t *DWC_MUTEX_ALLOC(void)
9559 +{
9560 + dwc_mutex_t *mutex = DWC_ALLOC(sizeof(struct lock));
9561 +
9562 + if (!mutex) {
9563 + DWC_ERROR("Cannot allocate memory for mutex");
9564 + return NULL;
9565 + }
9566 +
9567 + lockinit((struct lock *)mutex, 0, "dw3mtx", 0, 0);
9568 + return mutex;
9569 +}
9570 +
9571 +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
9572 +#else
9573 +void DWC_MUTEX_FREE(dwc_mutex_t *mutex)
9574 +{
9575 + DWC_FREE(mutex);
9576 +}
9577 +#endif
9578 +
9579 +void DWC_MUTEX_LOCK(dwc_mutex_t *mutex)
9580 +{
9581 + lockmgr((struct lock *)mutex, LK_EXCLUSIVE, NULL);
9582 +}
9583 +
9584 +int DWC_MUTEX_TRYLOCK(dwc_mutex_t *mutex)
9585 +{
9586 + int status;
9587 +
9588 + status = lockmgr((struct lock *)mutex, LK_EXCLUSIVE | LK_NOWAIT, NULL);
9589 + return status == 0;
9590 +}
9591 +
9592 +void DWC_MUTEX_UNLOCK(dwc_mutex_t *mutex)
9593 +{
9594 + lockmgr((struct lock *)mutex, LK_RELEASE, NULL);
9595 +}
9596 +
9597 +
9598 +/* Timing */
9599 +
9600 +void DWC_UDELAY(uint32_t usecs)
9601 +{
9602 + DELAY(usecs);
9603 +}
9604 +
9605 +void DWC_MDELAY(uint32_t msecs)
9606 +{
9607 + do {
9608 + DELAY(1000);
9609 + } while (--msecs);
9610 +}
9611 +
9612 +void DWC_MSLEEP(uint32_t msecs)
9613 +{
9614 + struct timeval tv;
9615 +
9616 + tv.tv_sec = msecs / 1000;
9617 + tv.tv_usec = (msecs - tv.tv_sec * 1000) * 1000;
9618 + tsleep(&tv, 0, "dw3slp", tvtohz(&tv));
9619 +}
9620 +
9621 +uint32_t DWC_TIME(void)
9622 +{
9623 + struct timeval tv;
9624 +
9625 + microuptime(&tv); // or getmicrouptime? (less precise, but faster)
9626 + return tv.tv_sec * 1000 + tv.tv_usec / 1000;
9627 +}
9628 +
9629 +
9630 +/* Timers */
9631 +
9632 +struct dwc_timer {
9633 + struct callout t;
9634 + char *name;
9635 + dwc_spinlock_t *lock;
9636 + dwc_timer_callback_t cb;
9637 + void *data;
9638 +};
9639 +
9640 +dwc_timer_t *DWC_TIMER_ALLOC(char *name, dwc_timer_callback_t cb, void *data)
9641 +{
9642 + dwc_timer_t *t = DWC_ALLOC(sizeof(*t));
9643 +
9644 + if (!t) {
9645 + DWC_ERROR("Cannot allocate memory for timer");
9646 + return NULL;
9647 + }
9648 +
9649 + callout_init(&t->t);
9650 +
9651 + t->name = DWC_STRDUP(name);
9652 + if (!t->name) {
9653 + DWC_ERROR("Cannot allocate memory for timer->name");
9654 + goto no_name;
9655 + }
9656 +
9657 + t->lock = DWC_SPINLOCK_ALLOC();
9658 + if (!t->lock) {
9659 + DWC_ERROR("Cannot allocate memory for timer->lock");
9660 + goto no_lock;
9661 + }
9662 +
9663 + t->cb = cb;
9664 + t->data = data;
9665 +
9666 + return t;
9667 +
9668 + no_lock:
9669 + DWC_FREE(t->name);
9670 + no_name:
9671 + DWC_FREE(t);
9672 +
9673 + return NULL;
9674 +}
9675 +
9676 +void DWC_TIMER_FREE(dwc_timer_t *timer)
9677 +{
9678 + callout_stop(&timer->t);
9679 + DWC_SPINLOCK_FREE(timer->lock);
9680 + DWC_FREE(timer->name);
9681 + DWC_FREE(timer);
9682 +}
9683 +
9684 +void DWC_TIMER_SCHEDULE(dwc_timer_t *timer, uint32_t time)
9685 +{
9686 + struct timeval tv;
9687 +
9688 + tv.tv_sec = time / 1000;
9689 + tv.tv_usec = (time - tv.tv_sec * 1000) * 1000;
9690 + callout_reset(&timer->t, tvtohz(&tv), timer->cb, timer->data);
9691 +}
9692 +
9693 +void DWC_TIMER_CANCEL(dwc_timer_t *timer)
9694 +{
9695 + callout_stop(&timer->t);
9696 +}
9697 +
9698 +
9699 +/* Wait Queues */
9700 +
9701 +struct dwc_waitq {
9702 + struct simplelock lock;
9703 + int abort;
9704 +};
9705 +
9706 +dwc_waitq_t *DWC_WAITQ_ALLOC(void)
9707 +{
9708 + dwc_waitq_t *wq = DWC_ALLOC(sizeof(*wq));
9709 +
9710 + if (!wq) {
9711 + DWC_ERROR("Cannot allocate memory for waitqueue");
9712 + return NULL;
9713 + }
9714 +
9715 + simple_lock_init(&wq->lock);
9716 + wq->abort = 0;
9717 +
9718 + return wq;
9719 +}
9720 +
9721 +void DWC_WAITQ_FREE(dwc_waitq_t *wq)
9722 +{
9723 + DWC_FREE(wq);
9724 +}
9725 +
9726 +int32_t DWC_WAITQ_WAIT(dwc_waitq_t *wq, dwc_waitq_condition_t cond, void *data)
9727 +{
9728 + int ipl;
9729 + int result = 0;
9730 +
9731 + simple_lock(&wq->lock);
9732 + ipl = splbio();
9733 +
9734 + /* Skip the sleep if already aborted or triggered */
9735 + if (!wq->abort && !cond(data)) {
9736 + splx(ipl);
9737 + result = ltsleep(wq, PCATCH, "dw3wat", 0, &wq->lock); // infinite timeout
9738 + ipl = splbio();
9739 + }
9740 +
9741 + if (result == 0) { // awoken
9742 + if (wq->abort) {
9743 + wq->abort = 0;
9744 + result = -DWC_E_ABORT;
9745 + } else {
9746 + result = 0;
9747 + }
9748 +
9749 + splx(ipl);
9750 + simple_unlock(&wq->lock);
9751 + } else {
9752 + wq->abort = 0;
9753 + splx(ipl);
9754 + simple_unlock(&wq->lock);
9755 +
9756 + if (result == ERESTART) { // signaled - restart
9757 + result = -DWC_E_RESTART;
9758 + } else { // signaled - must be EINTR
9759 + result = -DWC_E_ABORT;
9760 + }
9761 + }
9762 +
9763 + return result;
9764 +}
9765 +
9766 +int32_t DWC_WAITQ_WAIT_TIMEOUT(dwc_waitq_t *wq, dwc_waitq_condition_t cond,
9767 + void *data, int32_t msecs)
9768 +{
9769 + struct timeval tv, tv1, tv2;
9770 + int ipl;
9771 + int result = 0;
9772 +
9773 + tv.tv_sec = msecs / 1000;
9774 + tv.tv_usec = (msecs - tv.tv_sec * 1000) * 1000;
9775 +
9776 + simple_lock(&wq->lock);
9777 + ipl = splbio();
9778 +
9779 + /* Skip the sleep if already aborted or triggered */
9780 + if (!wq->abort && !cond(data)) {
9781 + splx(ipl);
9782 + getmicrouptime(&tv1);
9783 + result = ltsleep(wq, PCATCH, "dw3wto", tvtohz(&tv), &wq->lock);
9784 + getmicrouptime(&tv2);
9785 + ipl = splbio();
9786 + }
9787 +
9788 + if (result == 0) { // awoken
9789 + if (wq->abort) {
9790 + wq->abort = 0;
9791 + splx(ipl);
9792 + simple_unlock(&wq->lock);
9793 + result = -DWC_E_ABORT;
9794 + } else {
9795 + splx(ipl);
9796 + simple_unlock(&wq->lock);
9797 +
9798 + tv2.tv_usec -= tv1.tv_usec;
9799 + if (tv2.tv_usec < 0) {
9800 + tv2.tv_usec += 1000000;
9801 + tv2.tv_sec--;
9802 + }
9803 +
9804 + tv2.tv_sec -= tv1.tv_sec;
9805 + result = tv2.tv_sec * 1000 + tv2.tv_usec / 1000;
9806 + result = msecs - result;
9807 + if (result <= 0)
9808 + result = 1;
9809 + }
9810 + } else {
9811 + wq->abort = 0;
9812 + splx(ipl);
9813 + simple_unlock(&wq->lock);
9814 +
9815 + if (result == ERESTART) { // signaled - restart
9816 + result = -DWC_E_RESTART;
9817 +
9818 + } else if (result == EINTR) { // signaled - interrupt
9819 + result = -DWC_E_ABORT;
9820 +
9821 + } else { // timed out
9822 + result = -DWC_E_TIMEOUT;
9823 + }
9824 + }
9825 +
9826 + return result;
9827 +}
9828 +
9829 +void DWC_WAITQ_TRIGGER(dwc_waitq_t *wq)
9830 +{
9831 + wakeup(wq);
9832 +}
9833 +
9834 +void DWC_WAITQ_ABORT(dwc_waitq_t *wq)
9835 +{
9836 + int ipl;
9837 +
9838 + simple_lock(&wq->lock);
9839 + ipl = splbio();
9840 + wq->abort = 1;
9841 + wakeup(wq);
9842 + splx(ipl);
9843 + simple_unlock(&wq->lock);
9844 +}
9845 +
9846 +
9847 +/* Threading */
9848 +
9849 +struct dwc_thread {
9850 + struct proc *proc;
9851 + int abort;
9852 +};
9853 +
9854 +dwc_thread_t *DWC_THREAD_RUN(dwc_thread_function_t func, char *name, void *data)
9855 +{
9856 + int retval;
9857 + dwc_thread_t *thread = DWC_ALLOC(sizeof(*thread));
9858 +
9859 + if (!thread) {
9860 + return NULL;
9861 + }
9862 +
9863 + thread->abort = 0;
9864 + retval = kthread_create1((void (*)(void *))func, data, &thread->proc,
9865 + "%s", name);
9866 + if (retval) {
9867 + DWC_FREE(thread);
9868 + return NULL;
9869 + }
9870 +
9871 + return thread;
9872 +}
9873 +
9874 +int DWC_THREAD_STOP(dwc_thread_t *thread)
9875 +{
9876 + int retval;
9877 +
9878 + thread->abort = 1;
9879 + retval = tsleep(&thread->abort, 0, "dw3stp", 60 * hz);
9880 +
9881 + if (retval == 0) {
9882 + /* DWC_THREAD_EXIT() will free the thread struct */
9883 + return 0;
9884 + }
9885 +
9886 + /* NOTE: We leak the thread struct if thread doesn't die */
9887 +
9888 + if (retval == EWOULDBLOCK) {
9889 + return -DWC_E_TIMEOUT;
9890 + }
9891 +
9892 + return -DWC_E_UNKNOWN;
9893 +}
9894 +
9895 +dwc_bool_t DWC_THREAD_SHOULD_STOP(dwc_thread_t *thread)
9896 +{
9897 + return thread->abort;
9898 +}
9899 +
9900 +void DWC_THREAD_EXIT(dwc_thread_t *thread)
9901 +{
9902 + wakeup(&thread->abort);
9903 + DWC_FREE(thread);
9904 + kthread_exit(0);
9905 +}
9906 +
9907 +/* tasklets
9908 + - Runs in interrupt context (cannot sleep)
9909 + - Each tasklet runs on a single CPU
9910 + - Different tasklets can be running simultaneously on different CPUs
9911 + [ On NetBSD there is no corresponding mechanism, drivers don't have bottom-
9912 + halves. So we just call the callback directly from DWC_TASK_SCHEDULE() ]
9913 + */
9914 +struct dwc_tasklet {
9915 + dwc_tasklet_callback_t cb;
9916 + void *data;
9917 +};
9918 +
9919 +static void tasklet_callback(void *data)
9920 +{
9921 + dwc_tasklet_t *task = (dwc_tasklet_t *)data;
9922 +
9923 + task->cb(task->data);
9924 +}
9925 +
9926 +dwc_tasklet_t *DWC_TASK_ALLOC(char *name, dwc_tasklet_callback_t cb, void *data)
9927 +{
9928 + dwc_tasklet_t *task = DWC_ALLOC(sizeof(*task));
9929 +
9930 + if (task) {
9931 + task->cb = cb;
9932 + task->data = data;
9933 + } else {
9934 + DWC_ERROR("Cannot allocate memory for tasklet");
9935 + }
9936 +
9937 + return task;
9938 +}
9939 +
9940 +void DWC_TASK_FREE(dwc_tasklet_t *task)
9941 +{
9942 + DWC_FREE(task);
9943 +}
9944 +
9945 +void DWC_TASK_SCHEDULE(dwc_tasklet_t *task)
9946 +{
9947 + tasklet_callback(task);
9948 +}
9949 +
9950 +
9951 +/* workqueues
9952 + - Runs in process context (can sleep)
9953 + */
9954 +typedef struct work_container {
9955 + dwc_work_callback_t cb;
9956 + void *data;
9957 + dwc_workq_t *wq;
9958 + char *name;
9959 + int hz;
9960 + struct work task;
9961 +} work_container_t;
9962 +
9963 +struct dwc_workq {
9964 + struct workqueue *taskq;
9965 + dwc_spinlock_t *lock;
9966 + dwc_waitq_t *waitq;
9967 + int pending;
9968 + struct work_container *container;
9969 +};
9970 +
9971 +static void do_work(struct work *task, void *data)
9972 +{
9973 + dwc_workq_t *wq = (dwc_workq_t *)data;
9974 + work_container_t *container = wq->container;
9975 + dwc_irqflags_t flags;
9976 +
9977 + if (container->hz) {
9978 + tsleep(container, 0, "dw3wrk", container->hz);
9979 + }
9980 +
9981 + container->cb(container->data);
9982 + DWC_DEBUG("Work done: %s, container=%p", container->name, container);
9983 +
9984 + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
9985 + if (container->name)
9986 + DWC_FREE(container->name);
9987 + DWC_FREE(container);
9988 + wq->pending--;
9989 + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
9990 + DWC_WAITQ_TRIGGER(wq->waitq);
9991 +}
9992 +
9993 +static int work_done(void *data)
9994 +{
9995 + dwc_workq_t *workq = (dwc_workq_t *)data;
9996 +
9997 + return workq->pending == 0;
9998 +}
9999 +
10000 +int DWC_WORKQ_WAIT_WORK_DONE(dwc_workq_t *workq, int timeout)
10001 +{
10002 + return DWC_WAITQ_WAIT_TIMEOUT(workq->waitq, work_done, workq, timeout);
10003 +}
10004 +
10005 +dwc_workq_t *DWC_WORKQ_ALLOC(char *name)
10006 +{
10007 + int result;
10008 + dwc_workq_t *wq = DWC_ALLOC(sizeof(*wq));
10009 +
10010 + if (!wq) {
10011 + DWC_ERROR("Cannot allocate memory for workqueue");
10012 + return NULL;
10013 + }
10014 +
10015 + result = workqueue_create(&wq->taskq, name, do_work, wq, 0 /*PWAIT*/,
10016 + IPL_BIO, 0);
10017 + if (result) {
10018 + DWC_ERROR("Cannot create workqueue");
10019 + goto no_taskq;
10020 + }
10021 +
10022 + wq->pending = 0;
10023 +
10024 + wq->lock = DWC_SPINLOCK_ALLOC();
10025 + if (!wq->lock) {
10026 + DWC_ERROR("Cannot allocate memory for spinlock");
10027 + goto no_lock;
10028 + }
10029 +
10030 + wq->waitq = DWC_WAITQ_ALLOC();
10031 + if (!wq->waitq) {
10032 + DWC_ERROR("Cannot allocate memory for waitqueue");
10033 + goto no_waitq;
10034 + }
10035 +
10036 + return wq;
10037 +
10038 + no_waitq:
10039 + DWC_SPINLOCK_FREE(wq->lock);
10040 + no_lock:
10041 + workqueue_destroy(wq->taskq);
10042 + no_taskq:
10043 + DWC_FREE(wq);
10044 +
10045 + return NULL;
10046 +}
10047 +
10048 +void DWC_WORKQ_FREE(dwc_workq_t *wq)
10049 +{
10050 +#ifdef DEBUG
10051 + dwc_irqflags_t flags;
10052 +
10053 + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
10054 +
10055 + if (wq->pending != 0) {
10056 + struct work_container *container = wq->container;
10057 +
10058 + DWC_ERROR("Destroying work queue with pending work");
10059 +
10060 + if (container && container->name) {
10061 + DWC_ERROR("Work %s still pending", container->name);
10062 + }
10063 + }
10064 +
10065 + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
10066 +#endif
10067 + DWC_WAITQ_FREE(wq->waitq);
10068 + DWC_SPINLOCK_FREE(wq->lock);
10069 + workqueue_destroy(wq->taskq);
10070 + DWC_FREE(wq);
10071 +}
10072 +
10073 +void DWC_WORKQ_SCHEDULE(dwc_workq_t *wq, dwc_work_callback_t cb, void *data,
10074 + char *format, ...)
10075 +{
10076 + dwc_irqflags_t flags;
10077 + work_container_t *container;
10078 + static char name[128];
10079 + va_list args;
10080 +
10081 + va_start(args, format);
10082 + DWC_VSNPRINTF(name, 128, format, args);
10083 + va_end(args);
10084 +
10085 + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
10086 + wq->pending++;
10087 + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
10088 + DWC_WAITQ_TRIGGER(wq->waitq);
10089 +
10090 + container = DWC_ALLOC_ATOMIC(sizeof(*container));
10091 + if (!container) {
10092 + DWC_ERROR("Cannot allocate memory for container");
10093 + return;
10094 + }
10095 +
10096 + container->name = DWC_STRDUP(name);
10097 + if (!container->name) {
10098 + DWC_ERROR("Cannot allocate memory for container->name");
10099 + DWC_FREE(container);
10100 + return;
10101 + }
10102 +
10103 + container->cb = cb;
10104 + container->data = data;
10105 + container->wq = wq;
10106 + container->hz = 0;
10107 + wq->container = container;
10108 +
10109 + DWC_DEBUG("Queueing work: %s, container=%p", container->name, container);
10110 + workqueue_enqueue(wq->taskq, &container->task);
10111 +}
10112 +
10113 +void DWC_WORKQ_SCHEDULE_DELAYED(dwc_workq_t *wq, dwc_work_callback_t cb,
10114 + void *data, uint32_t time, char *format, ...)
10115 +{
10116 + dwc_irqflags_t flags;
10117 + work_container_t *container;
10118 + static char name[128];
10119 + struct timeval tv;
10120 + va_list args;
10121 +
10122 + va_start(args, format);
10123 + DWC_VSNPRINTF(name, 128, format, args);
10124 + va_end(args);
10125 +
10126 + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
10127 + wq->pending++;
10128 + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
10129 + DWC_WAITQ_TRIGGER(wq->waitq);
10130 +
10131 + container = DWC_ALLOC_ATOMIC(sizeof(*container));
10132 + if (!container) {
10133 + DWC_ERROR("Cannot allocate memory for container");
10134 + return;
10135 + }
10136 +
10137 + container->name = DWC_STRDUP(name);
10138 + if (!container->name) {
10139 + DWC_ERROR("Cannot allocate memory for container->name");
10140 + DWC_FREE(container);
10141 + return;
10142 + }
10143 +
10144 + container->cb = cb;
10145 + container->data = data;
10146 + container->wq = wq;
10147 + tv.tv_sec = time / 1000;
10148 + tv.tv_usec = (time - tv.tv_sec * 1000) * 1000;
10149 + container->hz = tvtohz(&tv);
10150 + wq->container = container;
10151 +
10152 + DWC_DEBUG("Queueing work: %s, container=%p", container->name, container);
10153 + workqueue_enqueue(wq->taskq, &container->task);
10154 +}
10155 +
10156 +int DWC_WORKQ_PENDING(dwc_workq_t *wq)
10157 +{
10158 + return wq->pending;
10159 +}
10160 --- /dev/null
10161 +++ b/drivers/usb/host/dwc_common_port/dwc_crypto.c
10162 @@ -0,0 +1,308 @@
10163 +/* =========================================================================
10164 + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_crypto.c $
10165 + * $Revision: #5 $
10166 + * $Date: 2010/09/28 $
10167 + * $Change: 1596182 $
10168 + *
10169 + * Synopsys Portability Library Software and documentation
10170 + * (hereinafter, "Software") is an Unsupported proprietary work of
10171 + * Synopsys, Inc. unless otherwise expressly agreed to in writing
10172 + * between Synopsys and you.
10173 + *
10174 + * The Software IS NOT an item of Licensed Software or Licensed Product
10175 + * under any End User Software License Agreement or Agreement for
10176 + * Licensed Product with Synopsys or any supplement thereto. You are
10177 + * permitted to use and redistribute this Software in source and binary
10178 + * forms, with or without modification, provided that redistributions
10179 + * of source code must retain this notice. You may not view, use,
10180 + * disclose, copy or distribute this file or any information contained
10181 + * herein except pursuant to this license grant from Synopsys. If you
10182 + * do not agree with this notice, including the disclaimer below, then
10183 + * you are not authorized to use the Software.
10184 + *
10185 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
10186 + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
10187 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
10188 + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
10189 + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
10190 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
10191 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
10192 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
10193 + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
10194 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
10195 + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
10196 + * DAMAGE.
10197 + * ========================================================================= */
10198 +
10199 +/** @file
10200 + * This file contains the WUSB cryptographic routines.
10201 + */
10202 +
10203 +#ifdef DWC_CRYPTOLIB
10204 +
10205 +#include "dwc_crypto.h"
10206 +#include "usb.h"
10207 +
10208 +#ifdef DEBUG
10209 +static inline void dump_bytes(char *name, uint8_t *bytes, int len)
10210 +{
10211 + int i;
10212 + DWC_PRINTF("%s: ", name);
10213 + for (i=0; i<len; i++) {
10214 + DWC_PRINTF("%02x ", bytes[i]);
10215 + }
10216 + DWC_PRINTF("\n");
10217 +}
10218 +#else
10219 +#define dump_bytes(x...)
10220 +#endif
10221 +
10222 +/* Display a block */
10223 +void show_block(const u8 *blk, const char *prefix, const char *suffix, int a)
10224 +{
10225 +#ifdef DWC_DEBUG_CRYPTO
10226 + int i, blksize = 16;
10227 +
10228 + DWC_DEBUG("%s", prefix);
10229 +
10230 + if (suffix == NULL) {
10231 + suffix = "\n";
10232 + blksize = a;
10233 + }
10234 +
10235 + for (i = 0; i < blksize; i++)
10236 + DWC_PRINT("%02x%s", *blk++, ((i & 3) == 3) ? " " : " ");
10237 + DWC_PRINT(suffix);
10238 +#endif
10239 +}
10240 +
10241 +/**
10242 + * Encrypts an array of bytes using the AES encryption engine.
10243 + * If <code>dst</code> == <code>src</code>, then the bytes will be encrypted
10244 + * in-place.
10245 + *
10246 + * @return 0 on success, negative error code on error.
10247 + */
10248 +int dwc_wusb_aes_encrypt(u8 *src, u8 *key, u8 *dst)
10249 +{
10250 + u8 block_t[16];
10251 + DWC_MEMSET(block_t, 0, 16);
10252 +
10253 + return DWC_AES_CBC(src, 16, key, 16, block_t, dst);
10254 +}
10255 +
10256 +/**
10257 + * The CCM-MAC-FUNCTION described in section 6.5 of the WUSB spec.
10258 + * This function takes a data string and returns the encrypted CBC
10259 + * Counter-mode MIC.
10260 + *
10261 + * @param key The 128-bit symmetric key.
10262 + * @param nonce The CCM nonce.
10263 + * @param label The unique 14-byte ASCII text label.
10264 + * @param bytes The byte array to be encrypted.
10265 + * @param len Length of the byte array.
10266 + * @param result Byte array to receive the 8-byte encrypted MIC.
10267 + */
10268 +void dwc_wusb_cmf(u8 *key, u8 *nonce,
10269 + char *label, u8 *bytes, int len, u8 *result)
10270 +{
10271 + u8 block_m[16];
10272 + u8 block_x[16];
10273 + u8 block_t[8];
10274 + int idx, blkNum;
10275 + u16 la = (u16)(len + 14);
10276 +
10277 + /* Set the AES-128 key */
10278 + //dwc_aes_setkey(tfm, key, 16);
10279 +
10280 + /* Fill block B0 from flags = 0x59, N, and l(m) = 0 */
10281 + block_m[0] = 0x59;
10282 + for (idx = 0; idx < 13; idx++)
10283 + block_m[idx + 1] = nonce[idx];
10284 + block_m[14] = 0;
10285 + block_m[15] = 0;
10286 +
10287 + /* Produce the CBC IV */
10288 + dwc_wusb_aes_encrypt(block_m, key, block_x);
10289 + show_block(block_m, "CBC IV in: ", "\n", 0);
10290 + show_block(block_x, "CBC IV out:", "\n", 0);
10291 +
10292 + /* Fill block B1 from l(a) = Blen + 14, and A */
10293 + block_x[0] ^= (u8)(la >> 8);
10294 + block_x[1] ^= (u8)la;
10295 + for (idx = 0; idx < 14; idx++)
10296 + block_x[idx + 2] ^= label[idx];
10297 + show_block(block_x, "After xor: ", "b1\n", 16);
10298 +
10299 + dwc_wusb_aes_encrypt(block_x, key, block_x);
10300 + show_block(block_x, "After AES: ", "b1\n", 16);
10301 +
10302 + idx = 0;
10303 + blkNum = 0;
10304 +
10305 + /* Fill remaining blocks with B */
10306 + while (len-- > 0) {
10307 + block_x[idx] ^= *bytes++;
10308 + if (++idx >= 16) {
10309 + idx = 0;
10310 + show_block(block_x, "After xor: ", "\n", blkNum);
10311 + dwc_wusb_aes_encrypt(block_x, key, block_x);
10312 + show_block(block_x, "After AES: ", "\n", blkNum);
10313 + blkNum++;
10314 + }
10315 + }
10316 +
10317 + /* Handle partial last block */
10318 + if (idx > 0) {
10319 + show_block(block_x, "After xor: ", "\n", blkNum);
10320 + dwc_wusb_aes_encrypt(block_x, key, block_x);
10321 + show_block(block_x, "After AES: ", "\n", blkNum);
10322 + }
10323 +
10324 + /* Save the MIC tag */
10325 + DWC_MEMCPY(block_t, block_x, 8);
10326 + show_block(block_t, "MIC tag : ", NULL, 8);
10327 +
10328 + /* Fill block A0 from flags = 0x01, N, and counter = 0 */
10329 + block_m[0] = 0x01;
10330 + block_m[14] = 0;
10331 + block_m[15] = 0;
10332 +
10333 + /* Encrypt the counter */
10334 + dwc_wusb_aes_encrypt(block_m, key, block_x);
10335 + show_block(block_x, "CTR[MIC] : ", NULL, 8);
10336 +
10337 + /* XOR with MIC tag */
10338 + for (idx = 0; idx < 8; idx++) {
10339 + block_t[idx] ^= block_x[idx];
10340 + }
10341 +
10342 + /* Return result to caller */
10343 + DWC_MEMCPY(result, block_t, 8);
10344 + show_block(result, "CCM-MIC : ", NULL, 8);
10345 +
10346 +}
10347 +
10348 +/**
10349 + * The PRF function described in section 6.5 of the WUSB spec. This function
10350 + * concatenates MIC values returned from dwc_cmf() to create a value of
10351 + * the requested length.
10352 + *
10353 + * @param prf_len Length of the PRF function in bits (64, 128, or 256).
10354 + * @param key, nonce, label, bytes, len Same as for dwc_cmf().
10355 + * @param result Byte array to receive the result.
10356 + */
10357 +void dwc_wusb_prf(int prf_len, u8 *key,
10358 + u8 *nonce, char *label, u8 *bytes, int len, u8 *result)
10359 +{
10360 + int i;
10361 +
10362 + nonce[0] = 0;
10363 + for (i = 0; i < prf_len >> 6; i++, nonce[0]++) {
10364 + dwc_wusb_cmf(key, nonce, label, bytes, len, result);
10365 + result += 8;
10366 + }
10367 +}
10368 +
10369 +/**
10370 + * Fills in CCM Nonce per the WUSB spec.
10371 + *
10372 + * @param[in] haddr Host address.
10373 + * @param[in] daddr Device address.
10374 + * @param[in] tkid Session Key(PTK) identifier.
10375 + * @param[out] nonce Pointer to where the CCM Nonce output is to be written.
10376 + */
10377 +void dwc_wusb_fill_ccm_nonce(uint16_t haddr, uint16_t daddr, uint8_t *tkid,
10378 + uint8_t *nonce)
10379 +{
10380 +
10381 + DWC_DEBUG("%s %x %x\n", __func__, daddr, haddr);
10382 +
10383 + DWC_MEMSET(&nonce[0], 0, 16);
10384 +
10385 + DWC_MEMCPY(&nonce[6], tkid, 3);
10386 + nonce[9] = daddr & 0xFF;
10387 + nonce[10] = (daddr >> 8) & 0xFF;
10388 + nonce[11] = haddr & 0xFF;
10389 + nonce[12] = (haddr >> 8) & 0xFF;
10390 +
10391 + dump_bytes("CCM nonce", nonce, 16);
10392 +}
10393 +
10394 +/**
10395 + * Generates a 16-byte cryptographic-grade random number for the Host/Device
10396 + * Nonce.
10397 + */
10398 +void dwc_wusb_gen_nonce(uint16_t addr, uint8_t *nonce)
10399 +{
10400 + uint8_t inonce[16];
10401 + uint32_t temp[4];
10402 +
10403 + /* Fill in the Nonce */
10404 + DWC_MEMSET(&inonce[0], 0, sizeof(inonce));
10405 + inonce[9] = addr & 0xFF;
10406 + inonce[10] = (addr >> 8) & 0xFF;
10407 + inonce[11] = inonce[9];
10408 + inonce[12] = inonce[10];
10409 +
10410 + /* Collect "randomness samples" */
10411 + DWC_RANDOM_BYTES((uint8_t *)temp, 16);
10412 +
10413 + dwc_wusb_prf_128((uint8_t *)temp, nonce,
10414 + "Random Numbers", (uint8_t *)temp, sizeof(temp),
10415 + nonce);
10416 +}
10417 +
10418 +/**
10419 + * Generates the Session Key (PTK) and Key Confirmation Key (KCK) per the
10420 + * WUSB spec.
10421 + *
10422 + * @param[in] ccm_nonce Pointer to CCM Nonce.
10423 + * @param[in] mk Master Key to derive the session from
10424 + * @param[in] hnonce Pointer to Host Nonce.
10425 + * @param[in] dnonce Pointer to Device Nonce.
10426 + * @param[out] kck Pointer to where the KCK output is to be written.
10427 + * @param[out] ptk Pointer to where the PTK output is to be written.
10428 + */
10429 +void dwc_wusb_gen_key(uint8_t *ccm_nonce, uint8_t *mk, uint8_t *hnonce,
10430 + uint8_t *dnonce, uint8_t *kck, uint8_t *ptk)
10431 +{
10432 + uint8_t idata[32];
10433 + uint8_t odata[32];
10434 +
10435 + dump_bytes("ck", mk, 16);
10436 + dump_bytes("hnonce", hnonce, 16);
10437 + dump_bytes("dnonce", dnonce, 16);
10438 +
10439 + /* The data is the HNonce and DNonce concatenated */
10440 + DWC_MEMCPY(&idata[0], hnonce, 16);
10441 + DWC_MEMCPY(&idata[16], dnonce, 16);
10442 +
10443 + dwc_wusb_prf_256(mk, ccm_nonce, "Pair-wise keys", idata, 32, odata);
10444 +
10445 + /* Low 16 bytes of the result is the KCK, high 16 is the PTK */
10446 + DWC_MEMCPY(kck, &odata[0], 16);
10447 + DWC_MEMCPY(ptk, &odata[16], 16);
10448 +
10449 + dump_bytes("kck", kck, 16);
10450 + dump_bytes("ptk", ptk, 16);
10451 +}
10452 +
10453 +/**
10454 + * Generates the Message Integrity Code over the Handshake data per the
10455 + * WUSB spec.
10456 + *
10457 + * @param ccm_nonce Pointer to CCM Nonce.
10458 + * @param kck Pointer to Key Confirmation Key.
10459 + * @param data Pointer to Handshake data to be checked.
10460 + * @param mic Pointer to where the MIC output is to be written.
10461 + */
10462 +void dwc_wusb_gen_mic(uint8_t *ccm_nonce, uint8_t *kck,
10463 + uint8_t *data, uint8_t *mic)
10464 +{
10465 +
10466 + dwc_wusb_prf_64(kck, ccm_nonce, "out-of-bandMIC",
10467 + data, WUSB_HANDSHAKE_LEN_FOR_MIC, mic);
10468 +}
10469 +
10470 +#endif /* DWC_CRYPTOLIB */
10471 --- /dev/null
10472 +++ b/drivers/usb/host/dwc_common_port/dwc_crypto.h
10473 @@ -0,0 +1,111 @@
10474 +/* =========================================================================
10475 + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_crypto.h $
10476 + * $Revision: #3 $
10477 + * $Date: 2010/09/28 $
10478 + * $Change: 1596182 $
10479 + *
10480 + * Synopsys Portability Library Software and documentation
10481 + * (hereinafter, "Software") is an Unsupported proprietary work of
10482 + * Synopsys, Inc. unless otherwise expressly agreed to in writing
10483 + * between Synopsys and you.
10484 + *
10485 + * The Software IS NOT an item of Licensed Software or Licensed Product
10486 + * under any End User Software License Agreement or Agreement for
10487 + * Licensed Product with Synopsys or any supplement thereto. You are
10488 + * permitted to use and redistribute this Software in source and binary
10489 + * forms, with or without modification, provided that redistributions
10490 + * of source code must retain this notice. You may not view, use,
10491 + * disclose, copy or distribute this file or any information contained
10492 + * herein except pursuant to this license grant from Synopsys. If you
10493 + * do not agree with this notice, including the disclaimer below, then
10494 + * you are not authorized to use the Software.
10495 + *
10496 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
10497 + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
10498 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
10499 + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
10500 + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
10501 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
10502 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
10503 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
10504 + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
10505 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
10506 + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
10507 + * DAMAGE.
10508 + * ========================================================================= */
10509 +
10510 +#ifndef _DWC_CRYPTO_H_
10511 +#define _DWC_CRYPTO_H_
10512 +
10513 +#ifdef __cplusplus
10514 +extern "C" {
10515 +#endif
10516 +
10517 +/** @file
10518 + *
10519 + * This file contains declarations for the WUSB Cryptographic routines as
10520 + * defined in the WUSB spec. They are only to be used internally by the DWC UWB
10521 + * modules.
10522 + */
10523 +
10524 +#include "dwc_os.h"
10525 +
10526 +int dwc_wusb_aes_encrypt(u8 *src, u8 *key, u8 *dst);
10527 +
10528 +void dwc_wusb_cmf(u8 *key, u8 *nonce,
10529 + char *label, u8 *bytes, int len, u8 *result);
10530 +void dwc_wusb_prf(int prf_len, u8 *key,
10531 + u8 *nonce, char *label, u8 *bytes, int len, u8 *result);
10532 +
10533 +/**
10534 + * The PRF-64 function described in section 6.5 of the WUSB spec.
10535 + *
10536 + * @param key, nonce, label, bytes, len, result Same as for dwc_prf().
10537 + */
10538 +static inline void dwc_wusb_prf_64(u8 *key, u8 *nonce,
10539 + char *label, u8 *bytes, int len, u8 *result)
10540 +{
10541 + dwc_wusb_prf(64, key, nonce, label, bytes, len, result);
10542 +}
10543 +
10544 +/**
10545 + * The PRF-128 function described in section 6.5 of the WUSB spec.
10546 + *
10547 + * @param key, nonce, label, bytes, len, result Same as for dwc_prf().
10548 + */
10549 +static inline void dwc_wusb_prf_128(u8 *key, u8 *nonce,
10550 + char *label, u8 *bytes, int len, u8 *result)
10551 +{
10552 + dwc_wusb_prf(128, key, nonce, label, bytes, len, result);
10553 +}
10554 +
10555 +/**
10556 + * The PRF-256 function described in section 6.5 of the WUSB spec.
10557 + *
10558 + * @param key, nonce, label, bytes, len, result Same as for dwc_prf().
10559 + */
10560 +static inline void dwc_wusb_prf_256(u8 *key, u8 *nonce,
10561 + char *label, u8 *bytes, int len, u8 *result)
10562 +{
10563 + dwc_wusb_prf(256, key, nonce, label, bytes, len, result);
10564 +}
10565 +
10566 +
10567 +void dwc_wusb_fill_ccm_nonce(uint16_t haddr, uint16_t daddr, uint8_t *tkid,
10568 + uint8_t *nonce);
10569 +void dwc_wusb_gen_nonce(uint16_t addr,
10570 + uint8_t *nonce);
10571 +
10572 +void dwc_wusb_gen_key(uint8_t *ccm_nonce, uint8_t *mk,
10573 + uint8_t *hnonce, uint8_t *dnonce,
10574 + uint8_t *kck, uint8_t *ptk);
10575 +
10576 +
10577 +void dwc_wusb_gen_mic(uint8_t *ccm_nonce, uint8_t
10578 + *kck, uint8_t *data, uint8_t *mic);
10579 +
10580 +#ifdef __cplusplus
10581 +}
10582 +#endif
10583 +
10584 +#endif /* _DWC_CRYPTO_H_ */
10585 --- /dev/null
10586 +++ b/drivers/usb/host/dwc_common_port/dwc_dh.c
10587 @@ -0,0 +1,291 @@
10588 +/* =========================================================================
10589 + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_dh.c $
10590 + * $Revision: #3 $
10591 + * $Date: 2010/09/28 $
10592 + * $Change: 1596182 $
10593 + *
10594 + * Synopsys Portability Library Software and documentation
10595 + * (hereinafter, "Software") is an Unsupported proprietary work of
10596 + * Synopsys, Inc. unless otherwise expressly agreed to in writing
10597 + * between Synopsys and you.
10598 + *
10599 + * The Software IS NOT an item of Licensed Software or Licensed Product
10600 + * under any End User Software License Agreement or Agreement for
10601 + * Licensed Product with Synopsys or any supplement thereto. You are
10602 + * permitted to use and redistribute this Software in source and binary
10603 + * forms, with or without modification, provided that redistributions
10604 + * of source code must retain this notice. You may not view, use,
10605 + * disclose, copy or distribute this file or any information contained
10606 + * herein except pursuant to this license grant from Synopsys. If you
10607 + * do not agree with this notice, including the disclaimer below, then
10608 + * you are not authorized to use the Software.
10609 + *
10610 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
10611 + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
10612 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
10613 + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
10614 + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
10615 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
10616 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
10617 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
10618 + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
10619 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
10620 + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
10621 + * DAMAGE.
10622 + * ========================================================================= */
10623 +#ifdef DWC_CRYPTOLIB
10624 +
10625 +#ifndef CONFIG_MACH_IPMATE
10626 +
10627 +#include "dwc_dh.h"
10628 +#include "dwc_modpow.h"
10629 +
10630 +#ifdef DEBUG
10631 +/* This function prints out a buffer in the format described in the Association
10632 + * Model specification. */
10633 +static void dh_dump(char *str, void *_num, int len)
10634 +{
10635 + uint8_t *num = _num;
10636 + int i;
10637 + DWC_PRINTF("%s\n", str);
10638 + for (i = 0; i < len; i ++) {
10639 + DWC_PRINTF("%02x", num[i]);
10640 + if (((i + 1) % 2) == 0) DWC_PRINTF(" ");
10641 + if (((i + 1) % 26) == 0) DWC_PRINTF("\n");
10642 + }
10643 +
10644 + DWC_PRINTF("\n");
10645 +}
10646 +#else
10647 +#define dh_dump(_x...) do {; } while(0)
10648 +#endif
10649 +
10650 +/* Constant g value */
10651 +static __u32 dh_g[] = {
10652 + 0x02000000,
10653 +};
10654 +
10655 +/* Constant p value */
10656 +static __u32 dh_p[] = {
10657 + 0xFFFFFFFF, 0xFFFFFFFF, 0xA2DA0FC9, 0x34C26821, 0x8B62C6C4, 0xD11CDC80, 0x084E0229, 0x74CC678A,
10658 + 0xA6BE0B02, 0x229B133B, 0x79084A51, 0xDD04348E, 0xB31995EF, 0x1B433ACD, 0x6D0A2B30, 0x37145FF2,
10659 + 0x6D35E14F, 0x45C2516D, 0x76B585E4, 0xC67E5E62, 0xE9424CF4, 0x6BED37A6, 0xB65CFF0B, 0xEDB706F4,
10660 + 0xFB6B38EE, 0xA59F895A, 0x11249FAE, 0xE61F4B7C, 0x51662849, 0x3D5BE4EC, 0xB87C00C2, 0x05BF63A1,
10661 + 0x3648DA98, 0x9AD3551C, 0xA83F1669, 0x5FCF24FD, 0x235D6583, 0x96ADA3DC, 0x56F3621C, 0xBB528520,
10662 + 0x0729D59E, 0x6D969670, 0x4E350C67, 0x0498BC4A, 0x086C74F1, 0x7C2118CA, 0x465E9032, 0x3BCE362E,
10663 + 0x2C779EE3, 0x03860E18, 0xA283279B, 0x8FA207EC, 0xF05DC5B5, 0xC9524C6F, 0xF6CB2BDE, 0x18175895,
10664 + 0x7C499539, 0xE56A95EA, 0x1826D215, 0x1005FA98, 0x5A8E7215, 0x2DC4AA8A, 0x0D1733AD, 0x337A5004,
10665 + 0xAB2155A8, 0x64BA1CDF, 0x0485FBEC, 0x0AEFDB58, 0x5771EA8A, 0x7D0C065D, 0x850F97B3, 0xC7E4E1A6,
10666 + 0x8CAEF5AB, 0xD73309DB, 0xE0948C1E, 0x9D61254A, 0x26D2E3CE, 0x6BEED21A, 0x06FA2FF1, 0x64088AD9,
10667 + 0x730276D8, 0x646AC83E, 0x182B1F52, 0x0C207B17, 0x5717E1BB, 0x6C5D617A, 0xC0880977, 0xE246D9BA,
10668 + 0xA04FE208, 0x31ABE574, 0xFC5BDB43, 0x8E10FDE0, 0x20D1824B, 0xCAD23AA9, 0xFFFFFFFF, 0xFFFFFFFF,
10669 +};
10670 +
10671 +static void dh_swap_bytes(void *_in, void *_out, uint32_t len)
10672 +{
10673 + uint8_t *in = _in;
10674 + uint8_t *out = _out;
10675 + int i;
10676 + for (i=0; i<len; i++) {
10677 + out[i] = in[len-1-i];
10678 + }
10679 +}
10680 +
10681 +/* Computes the modular exponentiation (num^exp % mod). num, exp, and mod are
10682 + * big endian numbers of size len, in bytes. Each len value must be a multiple
10683 + * of 4. */
10684 +int dwc_dh_modpow(void *mem_ctx, void *num, uint32_t num_len,
10685 + void *exp, uint32_t exp_len,
10686 + void *mod, uint32_t mod_len,
10687 + void *out)
10688 +{
10689 + /* modpow() takes little endian numbers. AM uses big-endian. This
10690 + * function swaps bytes of numbers before passing onto modpow. */
10691 +
10692 + int retval = 0;
10693 + uint32_t *result;
10694 +
10695 + uint32_t *bignum_num = dwc_alloc(mem_ctx, num_len + 4);
10696 + uint32_t *bignum_exp = dwc_alloc(mem_ctx, exp_len + 4);
10697 + uint32_t *bignum_mod = dwc_alloc(mem_ctx, mod_len + 4);
10698 +
10699 + dh_swap_bytes(num, &bignum_num[1], num_len);
10700 + bignum_num[0] = num_len / 4;
10701 +
10702 + dh_swap_bytes(exp, &bignum_exp[1], exp_len);
10703 + bignum_exp[0] = exp_len / 4;
10704 +
10705 + dh_swap_bytes(mod, &bignum_mod[1], mod_len);
10706 + bignum_mod[0] = mod_len / 4;
10707 +
10708 + result = dwc_modpow(mem_ctx, bignum_num, bignum_exp, bignum_mod);
10709 + if (!result) {
10710 + retval = -1;
10711 + goto dh_modpow_nomem;
10712 + }
10713 +
10714 + dh_swap_bytes(&result[1], out, result[0] * 4);
10715 + dwc_free(mem_ctx, result);
10716 +
10717 + dh_modpow_nomem:
10718 + dwc_free(mem_ctx, bignum_num);
10719 + dwc_free(mem_ctx, bignum_exp);
10720 + dwc_free(mem_ctx, bignum_mod);
10721 + return retval;
10722 +}
10723 +
10724 +
10725 +int dwc_dh_pk(void *mem_ctx, uint8_t nd, uint8_t *exp, uint8_t *pk, uint8_t *hash)
10726 +{
10727 + int retval;
10728 + uint8_t m3[385];
10729 +
10730 +#ifndef DH_TEST_VECTORS
10731 + DWC_RANDOM_BYTES(exp, 32);
10732 +#endif
10733 +
10734 + /* Compute the pkd */
10735 + if ((retval = dwc_dh_modpow(mem_ctx, dh_g, 4,
10736 + exp, 32,
10737 + dh_p, 384, pk))) {
10738 + return retval;
10739 + }
10740 +
10741 + m3[384] = nd;
10742 + DWC_MEMCPY(&m3[0], pk, 384);
10743 + DWC_SHA256(m3, 385, hash);
10744 +
10745 + dh_dump("PK", pk, 384);
10746 + dh_dump("SHA-256(M3)", hash, 32);
10747 + return 0;
10748 +}
10749 +
10750 +int dwc_dh_derive_keys(void *mem_ctx, uint8_t nd, uint8_t *pkh, uint8_t *pkd,
10751 + uint8_t *exp, int is_host,
10752 + char *dd, uint8_t *ck, uint8_t *kdk)
10753 +{
10754 + int retval;
10755 + uint8_t mv[784];
10756 + uint8_t sha_result[32];
10757 + uint8_t dhkey[384];
10758 + uint8_t shared_secret[384];
10759 + char *message;
10760 + uint32_t vd;
10761 +
10762 + uint8_t *pk;
10763 +
10764 + if (is_host) {
10765 + pk = pkd;
10766 + }
10767 + else {
10768 + pk = pkh;
10769 + }
10770 +
10771 + if ((retval = dwc_dh_modpow(mem_ctx, pk, 384,
10772 + exp, 32,
10773 + dh_p, 384, shared_secret))) {
10774 + return retval;
10775 + }
10776 + dh_dump("Shared Secret", shared_secret, 384);
10777 +
10778 + DWC_SHA256(shared_secret, 384, dhkey);
10779 + dh_dump("DHKEY", dhkey, 384);
10780 +
10781 + DWC_MEMCPY(&mv[0], pkd, 384);
10782 + DWC_MEMCPY(&mv[384], pkh, 384);
10783 + DWC_MEMCPY(&mv[768], "displayed digest", 16);
10784 + dh_dump("MV", mv, 784);
10785 +
10786 + DWC_SHA256(mv, 784, sha_result);
10787 + dh_dump("SHA-256(MV)", sha_result, 32);
10788 + dh_dump("First 32-bits of SHA-256(MV)", sha_result, 4);
10789 +
10790 + dh_swap_bytes(sha_result, &vd, 4);
10791 +#ifdef DEBUG
10792 + DWC_PRINTF("Vd (decimal) = %d\n", vd);
10793 +#endif
10794 +
10795 + switch (nd) {
10796 + case 2:
10797 + vd = vd % 100;
10798 + DWC_SPRINTF(dd, "%02d", vd);
10799 + break;
10800 + case 3:
10801 + vd = vd % 1000;
10802 + DWC_SPRINTF(dd, "%03d", vd);
10803 + break;
10804 + case 4:
10805 + vd = vd % 10000;
10806 + DWC_SPRINTF(dd, "%04d", vd);
10807 + break;
10808 + }
10809 +#ifdef DEBUG
10810 + DWC_PRINTF("Display Digits: %s\n", dd);
10811 +#endif
10812 +
10813 + message = "connection key";
10814 + DWC_HMAC_SHA256(message, DWC_STRLEN(message), dhkey, 32, sha_result);
10815 + dh_dump("HMAC(SHA-256, DHKey, connection key)", sha_result, 32);
10816 + DWC_MEMCPY(ck, sha_result, 16);
10817 +
10818 + message = "key derivation key";
10819 + DWC_HMAC_SHA256(message, DWC_STRLEN(message), dhkey, 32, sha_result);
10820 + dh_dump("HMAC(SHA-256, DHKey, key derivation key)", sha_result, 32);
10821 + DWC_MEMCPY(kdk, sha_result, 32);
10822 +
10823 + return 0;
10824 +}
10825 +
10826 +
10827 +#ifdef DH_TEST_VECTORS
10828 +
10829 +static __u8 dh_a[] = {
10830 + 0x44, 0x00, 0x51, 0xd6,
10831 + 0xf0, 0xb5, 0x5e, 0xa9,
10832 + 0x67, 0xab, 0x31, 0xc6,
10833 + 0x8a, 0x8b, 0x5e, 0x37,
10834 + 0xd9, 0x10, 0xda, 0xe0,
10835 + 0xe2, 0xd4, 0x59, 0xa4,
10836 + 0x86, 0x45, 0x9c, 0xaa,
10837 + 0xdf, 0x36, 0x75, 0x16,
10838 +};
10839 +
10840 +static __u8 dh_b[] = {
10841 + 0x5d, 0xae, 0xc7, 0x86,
10842 + 0x79, 0x80, 0xa3, 0x24,
10843 + 0x8c, 0xe3, 0x57, 0x8f,
10844 + 0xc7, 0x5f, 0x1b, 0x0f,
10845 + 0x2d, 0xf8, 0x9d, 0x30,
10846 + 0x6f, 0xa4, 0x52, 0xcd,
10847 + 0xe0, 0x7a, 0x04, 0x8a,
10848 + 0xde, 0xd9, 0x26, 0x56,
10849 +};
10850 +
10851 +void dwc_run_dh_test_vectors(void *mem_ctx)
10852 +{
10853 + uint8_t pkd[384];
10854 + uint8_t pkh[384];
10855 + uint8_t hashd[32];
10856 + uint8_t hashh[32];
10857 + uint8_t ck[16];
10858 + uint8_t kdk[32];
10859 + char dd[5];
10860 +
10861 + DWC_PRINTF("\n\n\nDH_TEST_VECTORS\n\n");
10862 +
10863 + /* compute the PKd and SHA-256(PKd || Nd) */
10864 + DWC_PRINTF("Computing PKd\n");
10865 + dwc_dh_pk(mem_ctx, 2, dh_a, pkd, hashd);
10866 +
10867 + /* compute the PKd and SHA-256(PKh || Nd) */
10868 + DWC_PRINTF("Computing PKh\n");
10869 + dwc_dh_pk(mem_ctx, 2, dh_b, pkh, hashh);
10870 +
10871 + /* compute the dhkey */
10872 + dwc_dh_derive_keys(mem_ctx, 2, pkh, pkd, dh_a, 0, dd, ck, kdk);
10873 +}
10874 +#endif /* DH_TEST_VECTORS */
10875 +
10876 +#endif /* !CONFIG_MACH_IPMATE */
10877 +
10878 +#endif /* DWC_CRYPTOLIB */
10879 --- /dev/null
10880 +++ b/drivers/usb/host/dwc_common_port/dwc_dh.h
10881 @@ -0,0 +1,106 @@
10882 +/* =========================================================================
10883 + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_dh.h $
10884 + * $Revision: #4 $
10885 + * $Date: 2010/09/28 $
10886 + * $Change: 1596182 $
10887 + *
10888 + * Synopsys Portability Library Software and documentation
10889 + * (hereinafter, "Software") is an Unsupported proprietary work of
10890 + * Synopsys, Inc. unless otherwise expressly agreed to in writing
10891 + * between Synopsys and you.
10892 + *
10893 + * The Software IS NOT an item of Licensed Software or Licensed Product
10894 + * under any End User Software License Agreement or Agreement for
10895 + * Licensed Product with Synopsys or any supplement thereto. You are
10896 + * permitted to use and redistribute this Software in source and binary
10897 + * forms, with or without modification, provided that redistributions
10898 + * of source code must retain this notice. You may not view, use,
10899 + * disclose, copy or distribute this file or any information contained
10900 + * herein except pursuant to this license grant from Synopsys. If you
10901 + * do not agree with this notice, including the disclaimer below, then
10902 + * you are not authorized to use the Software.
10903 + *
10904 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
10905 + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
10906 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
10907 + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
10908 + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
10909 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
10910 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
10911 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
10912 + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
10913 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
10914 + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
10915 + * DAMAGE.
10916 + * ========================================================================= */
10917 +#ifndef _DWC_DH_H_
10918 +#define _DWC_DH_H_
10919 +
10920 +#ifdef __cplusplus
10921 +extern "C" {
10922 +#endif
10923 +
10924 +#include "dwc_os.h"
10925 +
10926 +/** @file
10927 + *
10928 + * This file defines the common functions on device and host for performing
10929 + * numeric association as defined in the WUSB spec. They are only to be
10930 + * used internally by the DWC UWB modules. */
10931 +
10932 +extern int dwc_dh_sha256(uint8_t *message, uint32_t len, uint8_t *out);
10933 +extern int dwc_dh_hmac_sha256(uint8_t *message, uint32_t messagelen,
10934 + uint8_t *key, uint32_t keylen,
10935 + uint8_t *out);
10936 +extern int dwc_dh_modpow(void *mem_ctx, void *num, uint32_t num_len,
10937 + void *exp, uint32_t exp_len,
10938 + void *mod, uint32_t mod_len,
10939 + void *out);
10940 +
10941 +/** Computes PKD or PKH, and SHA-256(PKd || Nd)
10942 + *
10943 + * PK = g^exp mod p.
10944 + *
10945 + * Input:
10946 + * Nd = Number of digits on the device.
10947 + *
10948 + * Output:
10949 + * exp = A 32-byte buffer to be filled with a randomly generated number.
10950 + * used as either A or B.
10951 + * pk = A 384-byte buffer to be filled with the PKH or PKD.
10952 + * hash = A 32-byte buffer to be filled with SHA-256(PK || ND).
10953 + */
10954 +extern int dwc_dh_pk(void *mem_ctx, uint8_t nd, uint8_t *exp, uint8_t *pkd, uint8_t *hash);
10955 +
10956 +/** Computes the DHKEY, and VD.
10957 + *
10958 + * If called from host, then it will comput DHKEY=PKD^exp % p.
10959 + * If called from device, then it will comput DHKEY=PKH^exp % p.
10960 + *
10961 + * Input:
10962 + * pkd = The PKD value.
10963 + * pkh = The PKH value.
10964 + * exp = The A value (if device) or B value (if host) generated in dwc_wudev_dh_pk.
10965 + * is_host = Set to non zero if a WUSB host is calling this function.
10966 + *
10967 + * Output:
10968 +
10969 + * dd = A pointer to an buffer to be set to the displayed digits string to be shown
10970 + * to the user. This buffer should be at 5 bytes long to hold 4 digits plus a
10971 + * null termination character. This buffer can be used directly for display.
10972 + * ck = A 16-byte buffer to be filled with the CK.
10973 + * kdk = A 32-byte buffer to be filled with the KDK.
10974 + */
10975 +extern int dwc_dh_derive_keys(void *mem_ctx, uint8_t nd, uint8_t *pkh, uint8_t *pkd,
10976 + uint8_t *exp, int is_host,
10977 + char *dd, uint8_t *ck, uint8_t *kdk);
10978 +
10979 +#ifdef DH_TEST_VECTORS
10980 +extern void dwc_run_dh_test_vectors(void);
10981 +#endif
10982 +
10983 +#ifdef __cplusplus
10984 +}
10985 +#endif
10986 +
10987 +#endif /* _DWC_DH_H_ */
10988 --- /dev/null
10989 +++ b/drivers/usb/host/dwc_common_port/dwc_list.h
10990 @@ -0,0 +1,594 @@
10991 +/* $OpenBSD: queue.h,v 1.26 2004/05/04 16:59:32 grange Exp $ */
10992 +/* $NetBSD: queue.h,v 1.11 1996/05/16 05:17:14 mycroft Exp $ */
10993 +
10994 +/*
10995 + * Copyright (c) 1991, 1993
10996 + * The Regents of the University of California. All rights reserved.
10997 + *
10998 + * Redistribution and use in source and binary forms, with or without
10999 + * modification, are permitted provided that the following conditions
11000 + * are met:
11001 + * 1. Redistributions of source code must retain the above copyright
11002 + * notice, this list of conditions and the following disclaimer.
11003 + * 2. Redistributions in binary form must reproduce the above copyright
11004 + * notice, this list of conditions and the following disclaimer in the
11005 + * documentation and/or other materials provided with the distribution.
11006 + * 3. Neither the name of the University nor the names of its contributors
11007 + * may be used to endorse or promote products derived from this software
11008 + * without specific prior written permission.
11009 + *
11010 + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
11011 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
11012 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
11013 + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
11014 + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
11015 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
11016 + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
11017 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
11018 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
11019 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
11020 + * SUCH DAMAGE.
11021 + *
11022 + * @(#)queue.h 8.5 (Berkeley) 8/20/94
11023 + */
11024 +
11025 +#ifndef _DWC_LIST_H_
11026 +#define _DWC_LIST_H_
11027 +
11028 +#ifdef __cplusplus
11029 +extern "C" {
11030 +#endif
11031 +
11032 +/** @file
11033 + *
11034 + * This file defines linked list operations. It is derived from BSD with
11035 + * only the MACRO names being prefixed with DWC_. This is because a few of
11036 + * these names conflict with those on Linux. For documentation on use, see the
11037 + * inline comments in the source code. The original license for this source
11038 + * code applies and is preserved in the dwc_list.h source file.
11039 + */
11040 +
11041 +/*
11042 + * This file defines five types of data structures: singly-linked lists,
11043 + * lists, simple queues, tail queues, and circular queues.
11044 + *
11045 + *
11046 + * A singly-linked list is headed by a single forward pointer. The elements
11047 + * are singly linked for minimum space and pointer manipulation overhead at
11048 + * the expense of O(n) removal for arbitrary elements. New elements can be
11049 + * added to the list after an existing element or at the head of the list.
11050 + * Elements being removed from the head of the list should use the explicit
11051 + * macro for this purpose for optimum efficiency. A singly-linked list may
11052 + * only be traversed in the forward direction. Singly-linked lists are ideal
11053 + * for applications with large datasets and few or no removals or for
11054 + * implementing a LIFO queue.
11055 + *
11056 + * A list is headed by a single forward pointer (or an array of forward
11057 + * pointers for a hash table header). The elements are doubly linked
11058 + * so that an arbitrary element can be removed without a need to
11059 + * traverse the list. New elements can be added to the list before
11060 + * or after an existing element or at the head of the list. A list
11061 + * may only be traversed in the forward direction.
11062 + *
11063 + * A simple queue is headed by a pair of pointers, one the head of the
11064 + * list and the other to the tail of the list. The elements are singly
11065 + * linked to save space, so elements can only be removed from the
11066 + * head of the list. New elements can be added to the list before or after
11067 + * an existing element, at the head of the list, or at the end of the
11068 + * list. A simple queue may only be traversed in the forward direction.
11069 + *
11070 + * A tail queue is headed by a pair of pointers, one to the head of the
11071 + * list and the other to the tail of the list. The elements are doubly
11072 + * linked so that an arbitrary element can be removed without a need to
11073 + * traverse the list. New elements can be added to the list before or
11074 + * after an existing element, at the head of the list, or at the end of
11075 + * the list. A tail queue may be traversed in either direction.
11076 + *
11077 + * A circle queue is headed by a pair of pointers, one to the head of the
11078 + * list and the other to the tail of the list. The elements are doubly
11079 + * linked so that an arbitrary element can be removed without a need to
11080 + * traverse the list. New elements can be added to the list before or after
11081 + * an existing element, at the head of the list, or at the end of the list.
11082 + * A circle queue may be traversed in either direction, but has a more
11083 + * complex end of list detection.
11084 + *
11085 + * For details on the use of these macros, see the queue(3) manual page.
11086 + */
11087 +
11088 +/*
11089 + * Double-linked List.
11090 + */
11091 +
11092 +typedef struct dwc_list_link {
11093 + struct dwc_list_link *next;
11094 + struct dwc_list_link *prev;
11095 +} dwc_list_link_t;
11096 +
11097 +#define DWC_LIST_INIT(link) do { \
11098 + (link)->next = (link); \
11099 + (link)->prev = (link); \
11100 +} while (0)
11101 +
11102 +#define DWC_LIST_FIRST(link) ((link)->next)
11103 +#define DWC_LIST_LAST(link) ((link)->prev)
11104 +#define DWC_LIST_END(link) (link)
11105 +#define DWC_LIST_NEXT(link) ((link)->next)
11106 +#define DWC_LIST_PREV(link) ((link)->prev)
11107 +#define DWC_LIST_EMPTY(link) \
11108 + (DWC_LIST_FIRST(link) == DWC_LIST_END(link))
11109 +#define DWC_LIST_ENTRY(link, type, field) \
11110 + (type *)((uint8_t *)(link) - (size_t)(&((type *)0)->field))
11111 +
11112 +#if 0
11113 +#define DWC_LIST_INSERT_HEAD(list, link) do { \
11114 + (link)->next = (list)->next; \
11115 + (link)->prev = (list); \
11116 + (list)->next->prev = (link); \
11117 + (list)->next = (link); \
11118 +} while (0)
11119 +
11120 +#define DWC_LIST_INSERT_TAIL(list, link) do { \
11121 + (link)->next = (list); \
11122 + (link)->prev = (list)->prev; \
11123 + (list)->prev->next = (link); \
11124 + (list)->prev = (link); \
11125 +} while (0)
11126 +#else
11127 +#define DWC_LIST_INSERT_HEAD(list, link) do { \
11128 + dwc_list_link_t *__next__ = (list)->next; \
11129 + __next__->prev = (link); \
11130 + (link)->next = __next__; \
11131 + (link)->prev = (list); \
11132 + (list)->next = (link); \
11133 +} while (0)
11134 +
11135 +#define DWC_LIST_INSERT_TAIL(list, link) do { \
11136 + dwc_list_link_t *__prev__ = (list)->prev; \
11137 + (list)->prev = (link); \
11138 + (link)->next = (list); \
11139 + (link)->prev = __prev__; \
11140 + __prev__->next = (link); \
11141 +} while (0)
11142 +#endif
11143 +
11144 +#if 0
11145 +static inline void __list_add(struct list_head *new,
11146 + struct list_head *prev,
11147 + struct list_head *next)
11148 +{
11149 + next->prev = new;
11150 + new->next = next;
11151 + new->prev = prev;
11152 + prev->next = new;
11153 +}
11154 +
11155 +static inline void list_add(struct list_head *new, struct list_head *head)
11156 +{
11157 + __list_add(new, head, head->next);
11158 +}
11159 +
11160 +static inline void list_add_tail(struct list_head *new, struct list_head *head)
11161 +{
11162 + __list_add(new, head->prev, head);
11163 +}
11164 +
11165 +static inline void __list_del(struct list_head * prev, struct list_head * next)
11166 +{
11167 + next->prev = prev;
11168 + prev->next = next;
11169 +}
11170 +
11171 +static inline void list_del(struct list_head *entry)
11172 +{
11173 + __list_del(entry->prev, entry->next);
11174 + entry->next = LIST_POISON1;
11175 + entry->prev = LIST_POISON2;
11176 +}
11177 +#endif
11178 +
11179 +#define DWC_LIST_REMOVE(link) do { \
11180 + (link)->next->prev = (link)->prev; \
11181 + (link)->prev->next = (link)->next; \
11182 +} while (0)
11183 +
11184 +#define DWC_LIST_REMOVE_INIT(link) do { \
11185 + DWC_LIST_REMOVE(link); \
11186 + DWC_LIST_INIT(link); \
11187 +} while (0)
11188 +
11189 +#define DWC_LIST_MOVE_HEAD(list, link) do { \
11190 + DWC_LIST_REMOVE(link); \
11191 + DWC_LIST_INSERT_HEAD(list, link); \
11192 +} while (0)
11193 +
11194 +#define DWC_LIST_MOVE_TAIL(list, link) do { \
11195 + DWC_LIST_REMOVE(link); \
11196 + DWC_LIST_INSERT_TAIL(list, link); \
11197 +} while (0)
11198 +
11199 +#define DWC_LIST_FOREACH(var, list) \
11200 + for((var) = DWC_LIST_FIRST(list); \
11201 + (var) != DWC_LIST_END(list); \
11202 + (var) = DWC_LIST_NEXT(var))
11203 +
11204 +#define DWC_LIST_FOREACH_SAFE(var, var2, list) \
11205 + for((var) = DWC_LIST_FIRST(list), (var2) = DWC_LIST_NEXT(var); \
11206 + (var) != DWC_LIST_END(list); \
11207 + (var) = (var2), (var2) = DWC_LIST_NEXT(var2))
11208 +
11209 +#define DWC_LIST_FOREACH_REVERSE(var, list) \
11210 + for((var) = DWC_LIST_LAST(list); \
11211 + (var) != DWC_LIST_END(list); \
11212 + (var) = DWC_LIST_PREV(var))
11213 +
11214 +/*
11215 + * Singly-linked List definitions.
11216 + */
11217 +#define DWC_SLIST_HEAD(name, type) \
11218 +struct name { \
11219 + struct type *slh_first; /* first element */ \
11220 +}
11221 +
11222 +#define DWC_SLIST_HEAD_INITIALIZER(head) \
11223 + { NULL }
11224 +
11225 +#define DWC_SLIST_ENTRY(type) \
11226 +struct { \
11227 + struct type *sle_next; /* next element */ \
11228 +}
11229 +
11230 +/*
11231 + * Singly-linked List access methods.
11232 + */
11233 +#define DWC_SLIST_FIRST(head) ((head)->slh_first)
11234 +#define DWC_SLIST_END(head) NULL
11235 +#define DWC_SLIST_EMPTY(head) (SLIST_FIRST(head) == SLIST_END(head))
11236 +#define DWC_SLIST_NEXT(elm, field) ((elm)->field.sle_next)
11237 +
11238 +#define DWC_SLIST_FOREACH(var, head, field) \
11239 + for((var) = SLIST_FIRST(head); \
11240 + (var) != SLIST_END(head); \
11241 + (var) = SLIST_NEXT(var, field))
11242 +
11243 +#define DWC_SLIST_FOREACH_PREVPTR(var, varp, head, field) \
11244 + for((varp) = &SLIST_FIRST((head)); \
11245 + ((var) = *(varp)) != SLIST_END(head); \
11246 + (varp) = &SLIST_NEXT((var), field))
11247 +
11248 +/*
11249 + * Singly-linked List functions.
11250 + */
11251 +#define DWC_SLIST_INIT(head) { \
11252 + SLIST_FIRST(head) = SLIST_END(head); \
11253 +}
11254 +
11255 +#define DWC_SLIST_INSERT_AFTER(slistelm, elm, field) do { \
11256 + (elm)->field.sle_next = (slistelm)->field.sle_next; \
11257 + (slistelm)->field.sle_next = (elm); \
11258 +} while (0)
11259 +
11260 +#define DWC_SLIST_INSERT_HEAD(head, elm, field) do { \
11261 + (elm)->field.sle_next = (head)->slh_first; \
11262 + (head)->slh_first = (elm); \
11263 +} while (0)
11264 +
11265 +#define DWC_SLIST_REMOVE_NEXT(head, elm, field) do { \
11266 + (elm)->field.sle_next = (elm)->field.sle_next->field.sle_next; \
11267 +} while (0)
11268 +
11269 +#define DWC_SLIST_REMOVE_HEAD(head, field) do { \
11270 + (head)->slh_first = (head)->slh_first->field.sle_next; \
11271 +} while (0)
11272 +
11273 +#define DWC_SLIST_REMOVE(head, elm, type, field) do { \
11274 + if ((head)->slh_first == (elm)) { \
11275 + SLIST_REMOVE_HEAD((head), field); \
11276 + } \
11277 + else { \
11278 + struct type *curelm = (head)->slh_first; \
11279 + while( curelm->field.sle_next != (elm) ) \
11280 + curelm = curelm->field.sle_next; \
11281 + curelm->field.sle_next = \
11282 + curelm->field.sle_next->field.sle_next; \
11283 + } \
11284 +} while (0)
11285 +
11286 +/*
11287 + * Simple queue definitions.
11288 + */
11289 +#define DWC_SIMPLEQ_HEAD(name, type) \
11290 +struct name { \
11291 + struct type *sqh_first; /* first element */ \
11292 + struct type **sqh_last; /* addr of last next element */ \
11293 +}
11294 +
11295 +#define DWC_SIMPLEQ_HEAD_INITIALIZER(head) \
11296 + { NULL, &(head).sqh_first }
11297 +
11298 +#define DWC_SIMPLEQ_ENTRY(type) \
11299 +struct { \
11300 + struct type *sqe_next; /* next element */ \
11301 +}
11302 +
11303 +/*
11304 + * Simple queue access methods.
11305 + */
11306 +#define DWC_SIMPLEQ_FIRST(head) ((head)->sqh_first)
11307 +#define DWC_SIMPLEQ_END(head) NULL
11308 +#define DWC_SIMPLEQ_EMPTY(head) (SIMPLEQ_FIRST(head) == SIMPLEQ_END(head))
11309 +#define DWC_SIMPLEQ_NEXT(elm, field) ((elm)->field.sqe_next)
11310 +
11311 +#define DWC_SIMPLEQ_FOREACH(var, head, field) \
11312 + for((var) = SIMPLEQ_FIRST(head); \
11313 + (var) != SIMPLEQ_END(head); \
11314 + (var) = SIMPLEQ_NEXT(var, field))
11315 +
11316 +/*
11317 + * Simple queue functions.
11318 + */
11319 +#define DWC_SIMPLEQ_INIT(head) do { \
11320 + (head)->sqh_first = NULL; \
11321 + (head)->sqh_last = &(head)->sqh_first; \
11322 +} while (0)
11323 +
11324 +#define DWC_SIMPLEQ_INSERT_HEAD(head, elm, field) do { \
11325 + if (((elm)->field.sqe_next = (head)->sqh_first) == NULL) \
11326 + (head)->sqh_last = &(elm)->field.sqe_next; \
11327 + (head)->sqh_first = (elm); \
11328 +} while (0)
11329 +
11330 +#define DWC_SIMPLEQ_INSERT_TAIL(head, elm, field) do { \
11331 + (elm)->field.sqe_next = NULL; \
11332 + *(head)->sqh_last = (elm); \
11333 + (head)->sqh_last = &(elm)->field.sqe_next; \
11334 +} while (0)
11335 +
11336 +#define DWC_SIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
11337 + if (((elm)->field.sqe_next = (listelm)->field.sqe_next) == NULL)\
11338 + (head)->sqh_last = &(elm)->field.sqe_next; \
11339 + (listelm)->field.sqe_next = (elm); \
11340 +} while (0)
11341 +
11342 +#define DWC_SIMPLEQ_REMOVE_HEAD(head, field) do { \
11343 + if (((head)->sqh_first = (head)->sqh_first->field.sqe_next) == NULL) \
11344 + (head)->sqh_last = &(head)->sqh_first; \
11345 +} while (0)
11346 +
11347 +/*
11348 + * Tail queue definitions.
11349 + */
11350 +#define DWC_TAILQ_HEAD(name, type) \
11351 +struct name { \
11352 + struct type *tqh_first; /* first element */ \
11353 + struct type **tqh_last; /* addr of last next element */ \
11354 +}
11355 +
11356 +#define DWC_TAILQ_HEAD_INITIALIZER(head) \
11357 + { NULL, &(head).tqh_first }
11358 +
11359 +#define DWC_TAILQ_ENTRY(type) \
11360 +struct { \
11361 + struct type *tqe_next; /* next element */ \
11362 + struct type **tqe_prev; /* address of previous next element */ \
11363 +}
11364 +
11365 +/*
11366 + * tail queue access methods
11367 + */
11368 +#define DWC_TAILQ_FIRST(head) ((head)->tqh_first)
11369 +#define DWC_TAILQ_END(head) NULL
11370 +#define DWC_TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
11371 +#define DWC_TAILQ_LAST(head, headname) \
11372 + (*(((struct headname *)((head)->tqh_last))->tqh_last))
11373 +/* XXX */
11374 +#define DWC_TAILQ_PREV(elm, headname, field) \
11375 + (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
11376 +#define DWC_TAILQ_EMPTY(head) \
11377 + (DWC_TAILQ_FIRST(head) == DWC_TAILQ_END(head))
11378 +
11379 +#define DWC_TAILQ_FOREACH(var, head, field) \
11380 + for ((var) = DWC_TAILQ_FIRST(head); \
11381 + (var) != DWC_TAILQ_END(head); \
11382 + (var) = DWC_TAILQ_NEXT(var, field))
11383 +
11384 +#define DWC_TAILQ_FOREACH_REVERSE(var, head, headname, field) \
11385 + for ((var) = DWC_TAILQ_LAST(head, headname); \
11386 + (var) != DWC_TAILQ_END(head); \
11387 + (var) = DWC_TAILQ_PREV(var, headname, field))
11388 +
11389 +/*
11390 + * Tail queue functions.
11391 + */
11392 +#define DWC_TAILQ_INIT(head) do { \
11393 + (head)->tqh_first = NULL; \
11394 + (head)->tqh_last = &(head)->tqh_first; \
11395 +} while (0)
11396 +
11397 +#define DWC_TAILQ_INSERT_HEAD(head, elm, field) do { \
11398 + if (((elm)->field.tqe_next = (head)->tqh_first) != NULL) \
11399 + (head)->tqh_first->field.tqe_prev = \
11400 + &(elm)->field.tqe_next; \
11401 + else \
11402 + (head)->tqh_last = &(elm)->field.tqe_next; \
11403 + (head)->tqh_first = (elm); \
11404 + (elm)->field.tqe_prev = &(head)->tqh_first; \
11405 +} while (0)
11406 +
11407 +#define DWC_TAILQ_INSERT_TAIL(head, elm, field) do { \
11408 + (elm)->field.tqe_next = NULL; \
11409 + (elm)->field.tqe_prev = (head)->tqh_last; \
11410 + *(head)->tqh_last = (elm); \
11411 + (head)->tqh_last = &(elm)->field.tqe_next; \
11412 +} while (0)
11413 +
11414 +#define DWC_TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \
11415 + if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\
11416 + (elm)->field.tqe_next->field.tqe_prev = \
11417 + &(elm)->field.tqe_next; \
11418 + else \
11419 + (head)->tqh_last = &(elm)->field.tqe_next; \
11420 + (listelm)->field.tqe_next = (elm); \
11421 + (elm)->field.tqe_prev = &(listelm)->field.tqe_next; \
11422 +} while (0)
11423 +
11424 +#define DWC_TAILQ_INSERT_BEFORE(listelm, elm, field) do { \
11425 + (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
11426 + (elm)->field.tqe_next = (listelm); \
11427 + *(listelm)->field.tqe_prev = (elm); \
11428 + (listelm)->field.tqe_prev = &(elm)->field.tqe_next; \
11429 +} while (0)
11430 +
11431 +#define DWC_TAILQ_REMOVE(head, elm, field) do { \
11432 + if (((elm)->field.tqe_next) != NULL) \
11433 + (elm)->field.tqe_next->field.tqe_prev = \
11434 + (elm)->field.tqe_prev; \
11435 + else \
11436 + (head)->tqh_last = (elm)->field.tqe_prev; \
11437 + *(elm)->field.tqe_prev = (elm)->field.tqe_next; \
11438 +} while (0)
11439 +
11440 +#define DWC_TAILQ_REPLACE(head, elm, elm2, field) do { \
11441 + if (((elm2)->field.tqe_next = (elm)->field.tqe_next) != NULL) \
11442 + (elm2)->field.tqe_next->field.tqe_prev = \
11443 + &(elm2)->field.tqe_next; \
11444 + else \
11445 + (head)->tqh_last = &(elm2)->field.tqe_next; \
11446 + (elm2)->field.tqe_prev = (elm)->field.tqe_prev; \
11447 + *(elm2)->field.tqe_prev = (elm2); \
11448 +} while (0)
11449 +
11450 +/*
11451 + * Circular queue definitions.
11452 + */
11453 +#define DWC_CIRCLEQ_HEAD(name, type) \
11454 +struct name { \
11455 + struct type *cqh_first; /* first element */ \
11456 + struct type *cqh_last; /* last element */ \
11457 +}
11458 +
11459 +#define DWC_CIRCLEQ_HEAD_INITIALIZER(head) \
11460 + { DWC_CIRCLEQ_END(&head), DWC_CIRCLEQ_END(&head) }
11461 +
11462 +#define DWC_CIRCLEQ_ENTRY(type) \
11463 +struct { \
11464 + struct type *cqe_next; /* next element */ \
11465 + struct type *cqe_prev; /* previous element */ \
11466 +}
11467 +
11468 +/*
11469 + * Circular queue access methods
11470 + */
11471 +#define DWC_CIRCLEQ_FIRST(head) ((head)->cqh_first)
11472 +#define DWC_CIRCLEQ_LAST(head) ((head)->cqh_last)
11473 +#define DWC_CIRCLEQ_END(head) ((void *)(head))
11474 +#define DWC_CIRCLEQ_NEXT(elm, field) ((elm)->field.cqe_next)
11475 +#define DWC_CIRCLEQ_PREV(elm, field) ((elm)->field.cqe_prev)
11476 +#define DWC_CIRCLEQ_EMPTY(head) \
11477 + (DWC_CIRCLEQ_FIRST(head) == DWC_CIRCLEQ_END(head))
11478 +
11479 +#define DWC_CIRCLEQ_EMPTY_ENTRY(elm, field) (((elm)->field.cqe_next == NULL) && ((elm)->field.cqe_prev == NULL))
11480 +
11481 +#define DWC_CIRCLEQ_FOREACH(var, head, field) \
11482 + for((var) = DWC_CIRCLEQ_FIRST(head); \
11483 + (var) != DWC_CIRCLEQ_END(head); \
11484 + (var) = DWC_CIRCLEQ_NEXT(var, field))
11485 +
11486 +#define DWC_CIRCLEQ_FOREACH_SAFE(var, var2, head, field) \
11487 + for((var) = DWC_CIRCLEQ_FIRST(head), var2 = DWC_CIRCLEQ_NEXT(var, field); \
11488 + (var) != DWC_CIRCLEQ_END(head); \
11489 + (var) = var2, var2 = DWC_CIRCLEQ_NEXT(var, field))
11490 +
11491 +#define DWC_CIRCLEQ_FOREACH_REVERSE(var, head, field) \
11492 + for((var) = DWC_CIRCLEQ_LAST(head); \
11493 + (var) != DWC_CIRCLEQ_END(head); \
11494 + (var) = DWC_CIRCLEQ_PREV(var, field))
11495 +
11496 +/*
11497 + * Circular queue functions.
11498 + */
11499 +#define DWC_CIRCLEQ_INIT(head) do { \
11500 + (head)->cqh_first = DWC_CIRCLEQ_END(head); \
11501 + (head)->cqh_last = DWC_CIRCLEQ_END(head); \
11502 +} while (0)
11503 +
11504 +#define DWC_CIRCLEQ_INIT_ENTRY(elm, field) do { \
11505 + (elm)->field.cqe_next = NULL; \
11506 + (elm)->field.cqe_prev = NULL; \
11507 +} while (0)
11508 +
11509 +#define DWC_CIRCLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
11510 + (elm)->field.cqe_next = (listelm)->field.cqe_next; \
11511 + (elm)->field.cqe_prev = (listelm); \
11512 + if ((listelm)->field.cqe_next == DWC_CIRCLEQ_END(head)) \
11513 + (head)->cqh_last = (elm); \
11514 + else \
11515 + (listelm)->field.cqe_next->field.cqe_prev = (elm); \
11516 + (listelm)->field.cqe_next = (elm); \
11517 +} while (0)
11518 +
11519 +#define DWC_CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) do { \
11520 + (elm)->field.cqe_next = (listelm); \
11521 + (elm)->field.cqe_prev = (listelm)->field.cqe_prev; \
11522 + if ((listelm)->field.cqe_prev == DWC_CIRCLEQ_END(head)) \
11523 + (head)->cqh_first = (elm); \
11524 + else \
11525 + (listelm)->field.cqe_prev->field.cqe_next = (elm); \
11526 + (listelm)->field.cqe_prev = (elm); \
11527 +} while (0)
11528 +
11529 +#define DWC_CIRCLEQ_INSERT_HEAD(head, elm, field) do { \
11530 + (elm)->field.cqe_next = (head)->cqh_first; \
11531 + (elm)->field.cqe_prev = DWC_CIRCLEQ_END(head); \
11532 + if ((head)->cqh_last == DWC_CIRCLEQ_END(head)) \
11533 + (head)->cqh_last = (elm); \
11534 + else \
11535 + (head)->cqh_first->field.cqe_prev = (elm); \
11536 + (head)->cqh_first = (elm); \
11537 +} while (0)
11538 +
11539 +#define DWC_CIRCLEQ_INSERT_TAIL(head, elm, field) do { \
11540 + (elm)->field.cqe_next = DWC_CIRCLEQ_END(head); \
11541 + (elm)->field.cqe_prev = (head)->cqh_last; \
11542 + if ((head)->cqh_first == DWC_CIRCLEQ_END(head)) \
11543 + (head)->cqh_first = (elm); \
11544 + else \
11545 + (head)->cqh_last->field.cqe_next = (elm); \
11546 + (head)->cqh_last = (elm); \
11547 +} while (0)
11548 +
11549 +#define DWC_CIRCLEQ_REMOVE(head, elm, field) do { \
11550 + if ((elm)->field.cqe_next == DWC_CIRCLEQ_END(head)) \
11551 + (head)->cqh_last = (elm)->field.cqe_prev; \
11552 + else \
11553 + (elm)->field.cqe_next->field.cqe_prev = \
11554 + (elm)->field.cqe_prev; \
11555 + if ((elm)->field.cqe_prev == DWC_CIRCLEQ_END(head)) \
11556 + (head)->cqh_first = (elm)->field.cqe_next; \
11557 + else \
11558 + (elm)->field.cqe_prev->field.cqe_next = \
11559 + (elm)->field.cqe_next; \
11560 +} while (0)
11561 +
11562 +#define DWC_CIRCLEQ_REMOVE_INIT(head, elm, field) do { \
11563 + DWC_CIRCLEQ_REMOVE(head, elm, field); \
11564 + DWC_CIRCLEQ_INIT_ENTRY(elm, field); \
11565 +} while (0)
11566 +
11567 +#define DWC_CIRCLEQ_REPLACE(head, elm, elm2, field) do { \
11568 + if (((elm2)->field.cqe_next = (elm)->field.cqe_next) == \
11569 + DWC_CIRCLEQ_END(head)) \
11570 + (head).cqh_last = (elm2); \
11571 + else \
11572 + (elm2)->field.cqe_next->field.cqe_prev = (elm2); \
11573 + if (((elm2)->field.cqe_prev = (elm)->field.cqe_prev) == \
11574 + DWC_CIRCLEQ_END(head)) \
11575 + (head).cqh_first = (elm2); \
11576 + else \
11577 + (elm2)->field.cqe_prev->field.cqe_next = (elm2); \
11578 +} while (0)
11579 +
11580 +#ifdef __cplusplus
11581 +}
11582 +#endif
11583 +
11584 +#endif /* _DWC_LIST_H_ */
11585 --- /dev/null
11586 +++ b/drivers/usb/host/dwc_common_port/dwc_mem.c
11587 @@ -0,0 +1,245 @@
11588 +/* Memory Debugging */
11589 +#ifdef DWC_DEBUG_MEMORY
11590 +
11591 +#include "dwc_os.h"
11592 +#include "dwc_list.h"
11593 +
11594 +struct allocation {
11595 + void *addr;
11596 + void *ctx;
11597 + char *func;
11598 + int line;
11599 + uint32_t size;
11600 + int dma;
11601 + DWC_CIRCLEQ_ENTRY(allocation) entry;
11602 +};
11603 +
11604 +DWC_CIRCLEQ_HEAD(allocation_queue, allocation);
11605 +
11606 +struct allocation_manager {
11607 + void *mem_ctx;
11608 + struct allocation_queue allocations;
11609 +
11610 + /* statistics */
11611 + int num;
11612 + int num_freed;
11613 + int num_active;
11614 + uint32_t total;
11615 + uint32_t cur;
11616 + uint32_t max;
11617 +};
11618 +
11619 +static struct allocation_manager *manager = NULL;
11620 +
11621 +static int add_allocation(void *ctx, uint32_t size, char const *func, int line, void *addr,
11622 + int dma)
11623 +{
11624 + struct allocation *a;
11625 +
11626 + DWC_ASSERT(manager != NULL, "manager not allocated");
11627 +
11628 + a = __DWC_ALLOC_ATOMIC(manager->mem_ctx, sizeof(*a));
11629 + if (!a) {
11630 + return -DWC_E_NO_MEMORY;
11631 + }
11632 +
11633 + a->func = __DWC_ALLOC_ATOMIC(manager->mem_ctx, DWC_STRLEN(func) + 1);
11634 + if (!a->func) {
11635 + __DWC_FREE(manager->mem_ctx, a);
11636 + return -DWC_E_NO_MEMORY;
11637 + }
11638 +
11639 + DWC_MEMCPY(a->func, func, DWC_STRLEN(func) + 1);
11640 + a->addr = addr;
11641 + a->ctx = ctx;
11642 + a->line = line;
11643 + a->size = size;
11644 + a->dma = dma;
11645 + DWC_CIRCLEQ_INSERT_TAIL(&manager->allocations, a, entry);
11646 +
11647 + /* Update stats */
11648 + manager->num++;
11649 + manager->num_active++;
11650 + manager->total += size;
11651 + manager->cur += size;
11652 +
11653 + if (manager->max < manager->cur) {
11654 + manager->max = manager->cur;
11655 + }
11656 +
11657 + return 0;
11658 +}
11659 +
11660 +static struct allocation *find_allocation(void *ctx, void *addr)
11661 +{
11662 + struct allocation *a;
11663 +
11664 + DWC_CIRCLEQ_FOREACH(a, &manager->allocations, entry) {
11665 + if (a->ctx == ctx && a->addr == addr) {
11666 + return a;
11667 + }
11668 + }
11669 +
11670 + return NULL;
11671 +}
11672 +
11673 +static void free_allocation(void *ctx, void *addr, char const *func, int line)
11674 +{
11675 + struct allocation *a = find_allocation(ctx, addr);
11676 +
11677 + if (!a) {
11678 + DWC_ASSERT(0,
11679 + "Free of address %p that was never allocated or already freed %s:%d",
11680 + addr, func, line);
11681 + return;
11682 + }
11683 +
11684 + DWC_CIRCLEQ_REMOVE(&manager->allocations, a, entry);
11685 +
11686 + manager->num_active--;
11687 + manager->num_freed++;
11688 + manager->cur -= a->size;
11689 + __DWC_FREE(manager->mem_ctx, a->func);
11690 + __DWC_FREE(manager->mem_ctx, a);
11691 +}
11692 +
11693 +int dwc_memory_debug_start(void *mem_ctx)
11694 +{
11695 + DWC_ASSERT(manager == NULL, "Memory debugging has already started\n");
11696 +
11697 + if (manager) {
11698 + return -DWC_E_BUSY;
11699 + }
11700 +
11701 + manager = __DWC_ALLOC(mem_ctx, sizeof(*manager));
11702 + if (!manager) {
11703 + return -DWC_E_NO_MEMORY;
11704 + }
11705 +
11706 + DWC_CIRCLEQ_INIT(&manager->allocations);
11707 + manager->mem_ctx = mem_ctx;
11708 + manager->num = 0;
11709 + manager->num_freed = 0;
11710 + manager->num_active = 0;
11711 + manager->total = 0;
11712 + manager->cur = 0;
11713 + manager->max = 0;
11714 +
11715 + return 0;
11716 +}
11717 +
11718 +void dwc_memory_debug_stop(void)
11719 +{
11720 + struct allocation *a;
11721 +
11722 + dwc_memory_debug_report();
11723 +
11724 + DWC_CIRCLEQ_FOREACH(a, &manager->allocations, entry) {
11725 + DWC_ERROR("Memory leaked from %s:%d\n", a->func, a->line);
11726 + free_allocation(a->ctx, a->addr, NULL, -1);
11727 + }
11728 +
11729 + __DWC_FREE(manager->mem_ctx, manager);
11730 +}
11731 +
11732 +void dwc_memory_debug_report(void)
11733 +{
11734 + struct allocation *a;
11735 +
11736 + DWC_PRINTF("\n\n\n----------------- Memory Debugging Report -----------------\n\n");
11737 + DWC_PRINTF("Num Allocations = %d\n", manager->num);
11738 + DWC_PRINTF("Freed = %d\n", manager->num_freed);
11739 + DWC_PRINTF("Active = %d\n", manager->num_active);
11740 + DWC_PRINTF("Current Memory Used = %d\n", manager->cur);
11741 + DWC_PRINTF("Total Memory Used = %d\n", manager->total);
11742 + DWC_PRINTF("Maximum Memory Used at Once = %d\n", manager->max);
11743 + DWC_PRINTF("Unfreed allocations:\n");
11744 +
11745 + DWC_CIRCLEQ_FOREACH(a, &manager->allocations, entry) {
11746 + DWC_PRINTF(" addr=%p, size=%d from %s:%d, DMA=%d\n",
11747 + a->addr, a->size, a->func, a->line, a->dma);
11748 + }
11749 +}
11750 +
11751 +/* The replacement functions */
11752 +void *dwc_alloc_debug(void *mem_ctx, uint32_t size, char const *func, int line)
11753 +{
11754 + void *addr = __DWC_ALLOC(mem_ctx, size);
11755 +
11756 + if (!addr) {
11757 + return NULL;
11758 + }
11759 +
11760 + if (add_allocation(mem_ctx, size, func, line, addr, 0)) {
11761 + __DWC_FREE(mem_ctx, addr);
11762 + return NULL;
11763 + }
11764 +
11765 + return addr;
11766 +}
11767 +
11768 +void *dwc_alloc_atomic_debug(void *mem_ctx, uint32_t size, char const *func,
11769 + int line)
11770 +{
11771 + void *addr = __DWC_ALLOC_ATOMIC(mem_ctx, size);
11772 +
11773 + if (!addr) {
11774 + return NULL;
11775 + }
11776 +
11777 + if (add_allocation(mem_ctx, size, func, line, addr, 0)) {
11778 + __DWC_FREE(mem_ctx, addr);
11779 + return NULL;
11780 + }
11781 +
11782 + return addr;
11783 +}
11784 +
11785 +void dwc_free_debug(void *mem_ctx, void *addr, char const *func, int line)
11786 +{
11787 + free_allocation(mem_ctx, addr, func, line);
11788 + __DWC_FREE(mem_ctx, addr);
11789 +}
11790 +
11791 +void *dwc_dma_alloc_debug(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr,
11792 + char const *func, int line)
11793 +{
11794 + void *addr = __DWC_DMA_ALLOC(dma_ctx, size, dma_addr);
11795 +
11796 + if (!addr) {
11797 + return NULL;
11798 + }
11799 +
11800 + if (add_allocation(dma_ctx, size, func, line, addr, 1)) {
11801 + __DWC_DMA_FREE(dma_ctx, size, addr, *dma_addr);
11802 + return NULL;
11803 + }
11804 +
11805 + return addr;
11806 +}
11807 +
11808 +void *dwc_dma_alloc_atomic_debug(void *dma_ctx, uint32_t size,
11809 + dwc_dma_t *dma_addr, char const *func, int line)
11810 +{
11811 + void *addr = __DWC_DMA_ALLOC_ATOMIC(dma_ctx, size, dma_addr);
11812 +
11813 + if (!addr) {
11814 + return NULL;
11815 + }
11816 +
11817 + if (add_allocation(dma_ctx, size, func, line, addr, 1)) {
11818 + __DWC_DMA_FREE(dma_ctx, size, addr, *dma_addr);
11819 + return NULL;
11820 + }
11821 +
11822 + return addr;
11823 +}
11824 +
11825 +void dwc_dma_free_debug(void *dma_ctx, uint32_t size, void *virt_addr,
11826 + dwc_dma_t dma_addr, char const *func, int line)
11827 +{
11828 + free_allocation(dma_ctx, virt_addr, func, line);
11829 + __DWC_DMA_FREE(dma_ctx, size, virt_addr, dma_addr);
11830 +}
11831 +
11832 +#endif /* DWC_DEBUG_MEMORY */
11833 --- /dev/null
11834 +++ b/drivers/usb/host/dwc_common_port/dwc_modpow.c
11835 @@ -0,0 +1,636 @@
11836 +/* Bignum routines adapted from PUTTY sources. PuTTY copyright notice follows.
11837 + *
11838 + * PuTTY is copyright 1997-2007 Simon Tatham.
11839 + *
11840 + * Portions copyright Robert de Bath, Joris van Rantwijk, Delian
11841 + * Delchev, Andreas Schultz, Jeroen Massar, Wez Furlong, Nicolas Barry,
11842 + * Justin Bradford, Ben Harris, Malcolm Smith, Ahmad Khalifa, Markus
11843 + * Kuhn, and CORE SDI S.A.
11844 + *
11845 + * Permission is hereby granted, free of charge, to any person
11846 + * obtaining a copy of this software and associated documentation files
11847 + * (the "Software"), to deal in the Software without restriction,
11848 + * including without limitation the rights to use, copy, modify, merge,
11849 + * publish, distribute, sublicense, and/or sell copies of the Software,
11850 + * and to permit persons to whom the Software is furnished to do so,
11851 + * subject to the following conditions:
11852 + *
11853 + * The above copyright notice and this permission notice shall be
11854 + * included in all copies or substantial portions of the Software.
11855 +
11856 + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
11857 + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
11858 + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
11859 + * NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE
11860 + * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
11861 + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
11862 + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
11863 + *
11864 + */
11865 +#ifdef DWC_CRYPTOLIB
11866 +
11867 +#ifndef CONFIG_MACH_IPMATE
11868 +
11869 +#include "dwc_modpow.h"
11870 +
11871 +#define BIGNUM_INT_MASK 0xFFFFFFFFUL
11872 +#define BIGNUM_TOP_BIT 0x80000000UL
11873 +#define BIGNUM_INT_BITS 32
11874 +
11875 +
11876 +static void *snmalloc(void *mem_ctx, size_t n, size_t size)
11877 +{
11878 + void *p;
11879 + size *= n;
11880 + if (size == 0) size = 1;
11881 + p = dwc_alloc(mem_ctx, size);
11882 + return p;
11883 +}
11884 +
11885 +#define snewn(ctx, n, type) ((type *)snmalloc((ctx), (n), sizeof(type)))
11886 +#define sfree dwc_free
11887 +
11888 +/*
11889 + * Usage notes:
11890 + * * Do not call the DIVMOD_WORD macro with expressions such as array
11891 + * subscripts, as some implementations object to this (see below).
11892 + * * Note that none of the division methods below will cope if the
11893 + * quotient won't fit into BIGNUM_INT_BITS. Callers should be careful
11894 + * to avoid this case.
11895 + * If this condition occurs, in the case of the x86 DIV instruction,
11896 + * an overflow exception will occur, which (according to a correspondent)
11897 + * will manifest on Windows as something like
11898 + * 0xC0000095: Integer overflow
11899 + * The C variant won't give the right answer, either.
11900 + */
11901 +
11902 +#define MUL_WORD(w1, w2) ((BignumDblInt)w1 * w2)
11903 +
11904 +#if defined __GNUC__ && defined __i386__
11905 +#define DIVMOD_WORD(q, r, hi, lo, w) \
11906 + __asm__("div %2" : \
11907 + "=d" (r), "=a" (q) : \
11908 + "r" (w), "d" (hi), "a" (lo))
11909 +#else
11910 +#define DIVMOD_WORD(q, r, hi, lo, w) do { \
11911 + BignumDblInt n = (((BignumDblInt)hi) << BIGNUM_INT_BITS) | lo; \
11912 + q = n / w; \
11913 + r = n % w; \
11914 +} while (0)
11915 +#endif
11916 +
11917 +// q = n / w;
11918 +// r = n % w;
11919 +
11920 +#define BIGNUM_INT_BYTES (BIGNUM_INT_BITS / 8)
11921 +
11922 +#define BIGNUM_INTERNAL
11923 +
11924 +static Bignum newbn(void *mem_ctx, int length)
11925 +{
11926 + Bignum b = snewn(mem_ctx, length + 1, BignumInt);
11927 + //if (!b)
11928 + //abort(); /* FIXME */
11929 + DWC_MEMSET(b, 0, (length + 1) * sizeof(*b));
11930 + b[0] = length;
11931 + return b;
11932 +}
11933 +
11934 +void freebn(void *mem_ctx, Bignum b)
11935 +{
11936 + /*
11937 + * Burn the evidence, just in case.
11938 + */
11939 + DWC_MEMSET(b, 0, sizeof(b[0]) * (b[0] + 1));
11940 + sfree(mem_ctx, b);
11941 +}
11942 +
11943 +/*
11944 + * Compute c = a * b.
11945 + * Input is in the first len words of a and b.
11946 + * Result is returned in the first 2*len words of c.
11947 + */
11948 +static void internal_mul(BignumInt *a, BignumInt *b,
11949 + BignumInt *c, int len)
11950 +{
11951 + int i, j;
11952 + BignumDblInt t;
11953 +
11954 + for (j = 0; j < 2 * len; j++)
11955 + c[j] = 0;
11956 +
11957 + for (i = len - 1; i >= 0; i--) {
11958 + t = 0;
11959 + for (j = len - 1; j >= 0; j--) {
11960 + t += MUL_WORD(a[i], (BignumDblInt) b[j]);
11961 + t += (BignumDblInt) c[i + j + 1];
11962 + c[i + j + 1] = (BignumInt) t;
11963 + t = t >> BIGNUM_INT_BITS;
11964 + }
11965 + c[i] = (BignumInt) t;
11966 + }
11967 +}
11968 +
11969 +static void internal_add_shifted(BignumInt *number,
11970 + unsigned n, int shift)
11971 +{
11972 + int word = 1 + (shift / BIGNUM_INT_BITS);
11973 + int bshift = shift % BIGNUM_INT_BITS;
11974 + BignumDblInt addend;
11975 +
11976 + addend = (BignumDblInt)n << bshift;
11977 +
11978 + while (addend) {
11979 + addend += number[word];
11980 + number[word] = (BignumInt) addend & BIGNUM_INT_MASK;
11981 + addend >>= BIGNUM_INT_BITS;
11982 + word++;
11983 + }
11984 +}
11985 +
11986 +/*
11987 + * Compute a = a % m.
11988 + * Input in first alen words of a and first mlen words of m.
11989 + * Output in first alen words of a
11990 + * (of which first alen-mlen words will be zero).
11991 + * The MSW of m MUST have its high bit set.
11992 + * Quotient is accumulated in the `quotient' array, which is a Bignum
11993 + * rather than the internal bigendian format. Quotient parts are shifted
11994 + * left by `qshift' before adding into quot.
11995 + */
11996 +static void internal_mod(BignumInt *a, int alen,
11997 + BignumInt *m, int mlen,
11998 + BignumInt *quot, int qshift)
11999 +{
12000 + BignumInt m0, m1;
12001 + unsigned int h;
12002 + int i, k;
12003 +
12004 + m0 = m[0];
12005 + if (mlen > 1)
12006 + m1 = m[1];
12007 + else
12008 + m1 = 0;
12009 +
12010 + for (i = 0; i <= alen - mlen; i++) {
12011 + BignumDblInt t;
12012 + unsigned int q, r, c, ai1;
12013 +
12014 + if (i == 0) {
12015 + h = 0;
12016 + } else {
12017 + h = a[i - 1];
12018 + a[i - 1] = 0;
12019 + }
12020 +
12021 + if (i == alen - 1)
12022 + ai1 = 0;
12023 + else
12024 + ai1 = a[i + 1];
12025 +
12026 + /* Find q = h:a[i] / m0 */
12027 + if (h >= m0) {
12028 + /*
12029 + * Special case.
12030 + *
12031 + * To illustrate it, suppose a BignumInt is 8 bits, and
12032 + * we are dividing (say) A1:23:45:67 by A1:B2:C3. Then
12033 + * our initial division will be 0xA123 / 0xA1, which
12034 + * will give a quotient of 0x100 and a divide overflow.
12035 + * However, the invariants in this division algorithm
12036 + * are not violated, since the full number A1:23:... is
12037 + * _less_ than the quotient prefix A1:B2:... and so the
12038 + * following correction loop would have sorted it out.
12039 + *
12040 + * In this situation we set q to be the largest
12041 + * quotient we _can_ stomach (0xFF, of course).
12042 + */
12043 + q = BIGNUM_INT_MASK;
12044 + } else {
12045 + /* Macro doesn't want an array subscript expression passed
12046 + * into it (see definition), so use a temporary. */
12047 + BignumInt tmplo = a[i];
12048 + DIVMOD_WORD(q, r, h, tmplo, m0);
12049 +
12050 + /* Refine our estimate of q by looking at
12051 + h:a[i]:a[i+1] / m0:m1 */
12052 + t = MUL_WORD(m1, q);
12053 + if (t > ((BignumDblInt) r << BIGNUM_INT_BITS) + ai1) {
12054 + q--;
12055 + t -= m1;
12056 + r = (r + m0) & BIGNUM_INT_MASK; /* overflow? */
12057 + if (r >= (BignumDblInt) m0 &&
12058 + t > ((BignumDblInt) r << BIGNUM_INT_BITS) + ai1) q--;
12059 + }
12060 + }
12061 +
12062 + /* Subtract q * m from a[i...] */
12063 + c = 0;
12064 + for (k = mlen - 1; k >= 0; k--) {
12065 + t = MUL_WORD(q, m[k]);
12066 + t += c;
12067 + c = (unsigned)(t >> BIGNUM_INT_BITS);
12068 + if ((BignumInt) t > a[i + k])
12069 + c++;
12070 + a[i + k] -= (BignumInt) t;
12071 + }
12072 +
12073 + /* Add back m in case of borrow */
12074 + if (c != h) {
12075 + t = 0;
12076 + for (k = mlen - 1; k >= 0; k--) {
12077 + t += m[k];
12078 + t += a[i + k];
12079 + a[i + k] = (BignumInt) t;
12080 + t = t >> BIGNUM_INT_BITS;
12081 + }
12082 + q--;
12083 + }
12084 + if (quot)
12085 + internal_add_shifted(quot, q, qshift + BIGNUM_INT_BITS * (alen - mlen - i));
12086 + }
12087 +}
12088 +
12089 +/*
12090 + * Compute p % mod.
12091 + * The most significant word of mod MUST be non-zero.
12092 + * We assume that the result array is the same size as the mod array.
12093 + * We optionally write out a quotient if `quotient' is non-NULL.
12094 + * We can avoid writing out the result if `result' is NULL.
12095 + */
12096 +void bigdivmod(void *mem_ctx, Bignum p, Bignum mod, Bignum result, Bignum quotient)
12097 +{
12098 + BignumInt *n, *m;
12099 + int mshift;
12100 + int plen, mlen, i, j;
12101 +
12102 + /* Allocate m of size mlen, copy mod to m */
12103 + /* We use big endian internally */
12104 + mlen = mod[0];
12105 + m = snewn(mem_ctx, mlen, BignumInt);
12106 + //if (!m)
12107 + //abort(); /* FIXME */
12108 + for (j = 0; j < mlen; j++)
12109 + m[j] = mod[mod[0] - j];
12110 +
12111 + /* Shift m left to make msb bit set */
12112 + for (mshift = 0; mshift < BIGNUM_INT_BITS-1; mshift++)
12113 + if ((m[0] << mshift) & BIGNUM_TOP_BIT)
12114 + break;
12115 + if (mshift) {
12116 + for (i = 0; i < mlen - 1; i++)
12117 + m[i] = (m[i] << mshift) | (m[i + 1] >> (BIGNUM_INT_BITS - mshift));
12118 + m[mlen - 1] = m[mlen - 1] << mshift;
12119 + }
12120 +
12121 + plen = p[0];
12122 + /* Ensure plen > mlen */
12123 + if (plen <= mlen)
12124 + plen = mlen + 1;
12125 +
12126 + /* Allocate n of size plen, copy p to n */
12127 + n = snewn(mem_ctx, plen, BignumInt);
12128 + //if (!n)
12129 + //abort(); /* FIXME */
12130 + for (j = 0; j < plen; j++)
12131 + n[j] = 0;
12132 + for (j = 1; j <= (int)p[0]; j++)
12133 + n[plen - j] = p[j];
12134 +
12135 + /* Main computation */
12136 + internal_mod(n, plen, m, mlen, quotient, mshift);
12137 +
12138 + /* Fixup result in case the modulus was shifted */
12139 + if (mshift) {
12140 + for (i = plen - mlen - 1; i < plen - 1; i++)
12141 + n[i] = (n[i] << mshift) | (n[i + 1] >> (BIGNUM_INT_BITS - mshift));
12142 + n[plen - 1] = n[plen - 1] << mshift;
12143 + internal_mod(n, plen, m, mlen, quotient, 0);
12144 + for (i = plen - 1; i >= plen - mlen; i--)
12145 + n[i] = (n[i] >> mshift) | (n[i - 1] << (BIGNUM_INT_BITS - mshift));
12146 + }
12147 +
12148 + /* Copy result to buffer */
12149 + if (result) {
12150 + for (i = 1; i <= (int)result[0]; i++) {
12151 + int j = plen - i;
12152 + result[i] = j >= 0 ? n[j] : 0;
12153 + }
12154 + }
12155 +
12156 + /* Free temporary arrays */
12157 + for (i = 0; i < mlen; i++)
12158 + m[i] = 0;
12159 + sfree(mem_ctx, m);
12160 + for (i = 0; i < plen; i++)
12161 + n[i] = 0;
12162 + sfree(mem_ctx, n);
12163 +}
12164 +
12165 +/*
12166 + * Simple remainder.
12167 + */
12168 +Bignum bigmod(void *mem_ctx, Bignum a, Bignum b)
12169 +{
12170 + Bignum r = newbn(mem_ctx, b[0]);
12171 + bigdivmod(mem_ctx, a, b, r, NULL);
12172 + return r;
12173 +}
12174 +
12175 +/*
12176 + * Compute (base ^ exp) % mod.
12177 + */
12178 +Bignum dwc_modpow(void *mem_ctx, Bignum base_in, Bignum exp, Bignum mod)
12179 +{
12180 + BignumInt *a, *b, *n, *m;
12181 + int mshift;
12182 + int mlen, i, j;
12183 + Bignum base, result;
12184 +
12185 + /*
12186 + * The most significant word of mod needs to be non-zero. It
12187 + * should already be, but let's make sure.
12188 + */
12189 + //assert(mod[mod[0]] != 0);
12190 +
12191 + /*
12192 + * Make sure the base is smaller than the modulus, by reducing
12193 + * it modulo the modulus if not.
12194 + */
12195 + base = bigmod(mem_ctx, base_in, mod);
12196 +
12197 + /* Allocate m of size mlen, copy mod to m */
12198 + /* We use big endian internally */
12199 + mlen = mod[0];
12200 + m = snewn(mem_ctx, mlen, BignumInt);
12201 + //if (!m)
12202 + //abort(); /* FIXME */
12203 + for (j = 0; j < mlen; j++)
12204 + m[j] = mod[mod[0] - j];
12205 +
12206 + /* Shift m left to make msb bit set */
12207 + for (mshift = 0; mshift < BIGNUM_INT_BITS - 1; mshift++)
12208 + if ((m[0] << mshift) & BIGNUM_TOP_BIT)
12209 + break;
12210 + if (mshift) {
12211 + for (i = 0; i < mlen - 1; i++)
12212 + m[i] =
12213 + (m[i] << mshift) | (m[i + 1] >>
12214 + (BIGNUM_INT_BITS - mshift));
12215 + m[mlen - 1] = m[mlen - 1] << mshift;
12216 + }
12217 +
12218 + /* Allocate n of size mlen, copy base to n */
12219 + n = snewn(mem_ctx, mlen, BignumInt);
12220 + //if (!n)
12221 + //abort(); /* FIXME */
12222 + i = mlen - base[0];
12223 + for (j = 0; j < i; j++)
12224 + n[j] = 0;
12225 + for (j = 0; j < base[0]; j++)
12226 + n[i + j] = base[base[0] - j];
12227 +
12228 + /* Allocate a and b of size 2*mlen. Set a = 1 */
12229 + a = snewn(mem_ctx, 2 * mlen, BignumInt);
12230 + //if (!a)
12231 + //abort(); /* FIXME */
12232 + b = snewn(mem_ctx, 2 * mlen, BignumInt);
12233 + //if (!b)
12234 + //abort(); /* FIXME */
12235 + for (i = 0; i < 2 * mlen; i++)
12236 + a[i] = 0;
12237 + a[2 * mlen - 1] = 1;
12238 +
12239 + /* Skip leading zero bits of exp. */
12240 + i = 0;
12241 + j = BIGNUM_INT_BITS - 1;
12242 + while (i < exp[0] && (exp[exp[0] - i] & (1 << j)) == 0) {
12243 + j--;
12244 + if (j < 0) {
12245 + i++;
12246 + j = BIGNUM_INT_BITS - 1;
12247 + }
12248 + }
12249 +
12250 + /* Main computation */
12251 + while (i < exp[0]) {
12252 + while (j >= 0) {
12253 + internal_mul(a + mlen, a + mlen, b, mlen);
12254 + internal_mod(b, mlen * 2, m, mlen, NULL, 0);
12255 + if ((exp[exp[0] - i] & (1 << j)) != 0) {
12256 + internal_mul(b + mlen, n, a, mlen);
12257 + internal_mod(a, mlen * 2, m, mlen, NULL, 0);
12258 + } else {
12259 + BignumInt *t;
12260 + t = a;
12261 + a = b;
12262 + b = t;
12263 + }
12264 + j--;
12265 + }
12266 + i++;
12267 + j = BIGNUM_INT_BITS - 1;
12268 + }
12269 +
12270 + /* Fixup result in case the modulus was shifted */
12271 + if (mshift) {
12272 + for (i = mlen - 1; i < 2 * mlen - 1; i++)
12273 + a[i] =
12274 + (a[i] << mshift) | (a[i + 1] >>
12275 + (BIGNUM_INT_BITS - mshift));
12276 + a[2 * mlen - 1] = a[2 * mlen - 1] << mshift;
12277 + internal_mod(a, mlen * 2, m, mlen, NULL, 0);
12278 + for (i = 2 * mlen - 1; i >= mlen; i--)
12279 + a[i] =
12280 + (a[i] >> mshift) | (a[i - 1] <<
12281 + (BIGNUM_INT_BITS - mshift));
12282 + }
12283 +
12284 + /* Copy result to buffer */
12285 + result = newbn(mem_ctx, mod[0]);
12286 + for (i = 0; i < mlen; i++)
12287 + result[result[0] - i] = a[i + mlen];
12288 + while (result[0] > 1 && result[result[0]] == 0)
12289 + result[0]--;
12290 +
12291 + /* Free temporary arrays */
12292 + for (i = 0; i < 2 * mlen; i++)
12293 + a[i] = 0;
12294 + sfree(mem_ctx, a);
12295 + for (i = 0; i < 2 * mlen; i++)
12296 + b[i] = 0;
12297 + sfree(mem_ctx, b);
12298 + for (i = 0; i < mlen; i++)
12299 + m[i] = 0;
12300 + sfree(mem_ctx, m);
12301 + for (i = 0; i < mlen; i++)
12302 + n[i] = 0;
12303 + sfree(mem_ctx, n);
12304 +
12305 + freebn(mem_ctx, base);
12306 +
12307 + return result;
12308 +}
12309 +
12310 +
12311 +#ifdef UNITTEST
12312 +
12313 +static __u32 dh_p[] = {
12314 + 96,
12315 + 0xFFFFFFFF,
12316 + 0xFFFFFFFF,
12317 + 0xA93AD2CA,
12318 + 0x4B82D120,
12319 + 0xE0FD108E,
12320 + 0x43DB5BFC,
12321 + 0x74E5AB31,
12322 + 0x08E24FA0,
12323 + 0xBAD946E2,
12324 + 0x770988C0,
12325 + 0x7A615D6C,
12326 + 0xBBE11757,
12327 + 0x177B200C,
12328 + 0x521F2B18,
12329 + 0x3EC86A64,
12330 + 0xD8760273,
12331 + 0xD98A0864,
12332 + 0xF12FFA06,
12333 + 0x1AD2EE6B,
12334 + 0xCEE3D226,
12335 + 0x4A25619D,
12336 + 0x1E8C94E0,
12337 + 0xDB0933D7,
12338 + 0xABF5AE8C,
12339 + 0xA6E1E4C7,
12340 + 0xB3970F85,
12341 + 0x5D060C7D,
12342 + 0x8AEA7157,
12343 + 0x58DBEF0A,
12344 + 0xECFB8504,
12345 + 0xDF1CBA64,
12346 + 0xA85521AB,
12347 + 0x04507A33,
12348 + 0xAD33170D,
12349 + 0x8AAAC42D,
12350 + 0x15728E5A,
12351 + 0x98FA0510,
12352 + 0x15D22618,
12353 + 0xEA956AE5,
12354 + 0x3995497C,
12355 + 0x95581718,
12356 + 0xDE2BCBF6,
12357 + 0x6F4C52C9,
12358 + 0xB5C55DF0,
12359 + 0xEC07A28F,
12360 + 0x9B2783A2,
12361 + 0x180E8603,
12362 + 0xE39E772C,
12363 + 0x2E36CE3B,
12364 + 0x32905E46,
12365 + 0xCA18217C,
12366 + 0xF1746C08,
12367 + 0x4ABC9804,
12368 + 0x670C354E,
12369 + 0x7096966D,
12370 + 0x9ED52907,
12371 + 0x208552BB,
12372 + 0x1C62F356,
12373 + 0xDCA3AD96,
12374 + 0x83655D23,
12375 + 0xFD24CF5F,
12376 + 0x69163FA8,
12377 + 0x1C55D39A,
12378 + 0x98DA4836,
12379 + 0xA163BF05,
12380 + 0xC2007CB8,
12381 + 0xECE45B3D,
12382 + 0x49286651,
12383 + 0x7C4B1FE6,
12384 + 0xAE9F2411,
12385 + 0x5A899FA5,
12386 + 0xEE386BFB,
12387 + 0xF406B7ED,
12388 + 0x0BFF5CB6,
12389 + 0xA637ED6B,
12390 + 0xF44C42E9,
12391 + 0x625E7EC6,
12392 + 0xE485B576,
12393 + 0x6D51C245,
12394 + 0x4FE1356D,
12395 + 0xF25F1437,
12396 + 0x302B0A6D,
12397 + 0xCD3A431B,
12398 + 0xEF9519B3,
12399 + 0x8E3404DD,
12400 + 0x514A0879,
12401 + 0x3B139B22,
12402 + 0x020BBEA6,
12403 + 0x8A67CC74,
12404 + 0x29024E08,
12405 + 0x80DC1CD1,
12406 + 0xC4C6628B,
12407 + 0x2168C234,
12408 + 0xC90FDAA2,
12409 + 0xFFFFFFFF,
12410 + 0xFFFFFFFF,
12411 +};
12412 +
12413 +static __u32 dh_a[] = {
12414 + 8,
12415 + 0xdf367516,
12416 + 0x86459caa,
12417 + 0xe2d459a4,
12418 + 0xd910dae0,
12419 + 0x8a8b5e37,
12420 + 0x67ab31c6,
12421 + 0xf0b55ea9,
12422 + 0x440051d6,
12423 +};
12424 +
12425 +static __u32 dh_b[] = {
12426 + 8,
12427 + 0xded92656,
12428 + 0xe07a048a,
12429 + 0x6fa452cd,
12430 + 0x2df89d30,
12431 + 0xc75f1b0f,
12432 + 0x8ce3578f,
12433 + 0x7980a324,
12434 + 0x5daec786,
12435 +};
12436 +
12437 +static __u32 dh_g[] = {
12438 + 1,
12439 + 2,
12440 +};
12441 +
12442 +int main(void)
12443 +{
12444 + int i;
12445 + __u32 *k;
12446 + k = dwc_modpow(NULL, dh_g, dh_a, dh_p);
12447 +
12448 + printf("\n\n");
12449 + for (i=0; i<k[0]; i++) {
12450 + __u32 word32 = k[k[0] - i];
12451 + __u16 l = word32 & 0xffff;
12452 + __u16 m = (word32 & 0xffff0000) >> 16;
12453 + printf("%04x %04x ", m, l);
12454 + if (!((i + 1)%13)) printf("\n");
12455 + }
12456 + printf("\n\n");
12457 +
12458 + if ((k[0] == 0x60) && (k[1] == 0x28e490e5) && (k[0x60] == 0x5a0d3d4e)) {
12459 + printf("PASS\n\n");
12460 + }
12461 + else {
12462 + printf("FAIL\n\n");
12463 + }
12464 +
12465 +}
12466 +
12467 +#endif /* UNITTEST */
12468 +
12469 +#endif /* CONFIG_MACH_IPMATE */
12470 +
12471 +#endif /*DWC_CRYPTOLIB */
12472 --- /dev/null
12473 +++ b/drivers/usb/host/dwc_common_port/dwc_modpow.h
12474 @@ -0,0 +1,34 @@
12475 +/*
12476 + * dwc_modpow.h
12477 + * See dwc_modpow.c for license and changes
12478 + */
12479 +#ifndef _DWC_MODPOW_H
12480 +#define _DWC_MODPOW_H
12481 +
12482 +#ifdef __cplusplus
12483 +extern "C" {
12484 +#endif
12485 +
12486 +#include "dwc_os.h"
12487 +
12488 +/** @file
12489 + *
12490 + * This file defines the module exponentiation function which is only used
12491 + * internally by the DWC UWB modules for calculation of PKs during numeric
12492 + * association. The routine is taken from the PUTTY, an open source terminal
12493 + * emulator. The PUTTY License is preserved in the dwc_modpow.c file.
12494 + *
12495 + */
12496 +
12497 +typedef uint32_t BignumInt;
12498 +typedef uint64_t BignumDblInt;
12499 +typedef BignumInt *Bignum;
12500 +
12501 +/* Compute modular exponentiaion */
12502 +extern Bignum dwc_modpow(void *mem_ctx, Bignum base_in, Bignum exp, Bignum mod);
12503 +
12504 +#ifdef __cplusplus
12505 +}
12506 +#endif
12507 +
12508 +#endif /* _LINUX_BIGNUM_H */
12509 --- /dev/null
12510 +++ b/drivers/usb/host/dwc_common_port/dwc_notifier.c
12511 @@ -0,0 +1,319 @@
12512 +#ifdef DWC_NOTIFYLIB
12513 +
12514 +#include "dwc_notifier.h"
12515 +#include "dwc_list.h"
12516 +
12517 +typedef struct dwc_observer {
12518 + void *observer;
12519 + dwc_notifier_callback_t callback;
12520 + void *data;
12521 + char *notification;
12522 + DWC_CIRCLEQ_ENTRY(dwc_observer) list_entry;
12523 +} observer_t;
12524 +
12525 +DWC_CIRCLEQ_HEAD(observer_queue, dwc_observer);
12526 +
12527 +typedef struct dwc_notifier {
12528 + void *mem_ctx;
12529 + void *object;
12530 + struct observer_queue observers;
12531 + DWC_CIRCLEQ_ENTRY(dwc_notifier) list_entry;
12532 +} notifier_t;
12533 +
12534 +DWC_CIRCLEQ_HEAD(notifier_queue, dwc_notifier);
12535 +
12536 +typedef struct manager {
12537 + void *mem_ctx;
12538 + void *wkq_ctx;
12539 + dwc_workq_t *wq;
12540 +// dwc_mutex_t *mutex;
12541 + struct notifier_queue notifiers;
12542 +} manager_t;
12543 +
12544 +static manager_t *manager = NULL;
12545 +
12546 +static int create_manager(void *mem_ctx, void *wkq_ctx)
12547 +{
12548 + manager = dwc_alloc(mem_ctx, sizeof(manager_t));
12549 + if (!manager) {
12550 + return -DWC_E_NO_MEMORY;
12551 + }
12552 +
12553 + DWC_CIRCLEQ_INIT(&manager->notifiers);
12554 +
12555 + manager->wq = dwc_workq_alloc(wkq_ctx, "DWC Notification WorkQ");
12556 + if (!manager->wq) {
12557 + return -DWC_E_NO_MEMORY;
12558 + }
12559 +
12560 + return 0;
12561 +}
12562 +
12563 +static void free_manager(void)
12564 +{
12565 + dwc_workq_free(manager->wq);
12566 +
12567 + /* All notifiers must have unregistered themselves before this module
12568 + * can be removed. Hitting this assertion indicates a programmer
12569 + * error. */
12570 + DWC_ASSERT(DWC_CIRCLEQ_EMPTY(&manager->notifiers),
12571 + "Notification manager being freed before all notifiers have been removed");
12572 + dwc_free(manager->mem_ctx, manager);
12573 +}
12574 +
12575 +#ifdef DEBUG
12576 +static void dump_manager(void)
12577 +{
12578 + notifier_t *n;
12579 + observer_t *o;
12580 +
12581 + DWC_ASSERT(manager, "Notification manager not found");
12582 +
12583 + DWC_DEBUG("List of all notifiers and observers:\n");
12584 + DWC_CIRCLEQ_FOREACH(n, &manager->notifiers, list_entry) {
12585 + DWC_DEBUG("Notifier %p has observers:\n", n->object);
12586 + DWC_CIRCLEQ_FOREACH(o, &n->observers, list_entry) {
12587 + DWC_DEBUG(" %p watching %s\n", o->observer, o->notification);
12588 + }
12589 + }
12590 +}
12591 +#else
12592 +#define dump_manager(...)
12593 +#endif
12594 +
12595 +static observer_t *alloc_observer(void *mem_ctx, void *observer, char *notification,
12596 + dwc_notifier_callback_t callback, void *data)
12597 +{
12598 + observer_t *new_observer = dwc_alloc(mem_ctx, sizeof(observer_t));
12599 +
12600 + if (!new_observer) {
12601 + return NULL;
12602 + }
12603 +
12604 + DWC_CIRCLEQ_INIT_ENTRY(new_observer, list_entry);
12605 + new_observer->observer = observer;
12606 + new_observer->notification = notification;
12607 + new_observer->callback = callback;
12608 + new_observer->data = data;
12609 + return new_observer;
12610 +}
12611 +
12612 +static void free_observer(void *mem_ctx, observer_t *observer)
12613 +{
12614 + dwc_free(mem_ctx, observer);
12615 +}
12616 +
12617 +static notifier_t *alloc_notifier(void *mem_ctx, void *object)
12618 +{
12619 + notifier_t *notifier;
12620 +
12621 + if (!object) {
12622 + return NULL;
12623 + }
12624 +
12625 + notifier = dwc_alloc(mem_ctx, sizeof(notifier_t));
12626 + if (!notifier) {
12627 + return NULL;
12628 + }
12629 +
12630 + DWC_CIRCLEQ_INIT(&notifier->observers);
12631 + DWC_CIRCLEQ_INIT_ENTRY(notifier, list_entry);
12632 +
12633 + notifier->mem_ctx = mem_ctx;
12634 + notifier->object = object;
12635 + return notifier;
12636 +}
12637 +
12638 +static void free_notifier(notifier_t *notifier)
12639 +{
12640 + observer_t *observer;
12641 +
12642 + DWC_CIRCLEQ_FOREACH(observer, &notifier->observers, list_entry) {
12643 + free_observer(notifier->mem_ctx, observer);
12644 + }
12645 +
12646 + dwc_free(notifier->mem_ctx, notifier);
12647 +}
12648 +
12649 +static notifier_t *find_notifier(void *object)
12650 +{
12651 + notifier_t *notifier;
12652 +
12653 + DWC_ASSERT(manager, "Notification manager not found");
12654 +
12655 + if (!object) {
12656 + return NULL;
12657 + }
12658 +
12659 + DWC_CIRCLEQ_FOREACH(notifier, &manager->notifiers, list_entry) {
12660 + if (notifier->object == object) {
12661 + return notifier;
12662 + }
12663 + }
12664 +
12665 + return NULL;
12666 +}
12667 +
12668 +int dwc_alloc_notification_manager(void *mem_ctx, void *wkq_ctx)
12669 +{
12670 + return create_manager(mem_ctx, wkq_ctx);
12671 +}
12672 +
12673 +void dwc_free_notification_manager(void)
12674 +{
12675 + free_manager();
12676 +}
12677 +
12678 +dwc_notifier_t *dwc_register_notifier(void *mem_ctx, void *object)
12679 +{
12680 + notifier_t *notifier;
12681 +
12682 + DWC_ASSERT(manager, "Notification manager not found");
12683 +
12684 + notifier = find_notifier(object);
12685 + if (notifier) {
12686 + DWC_ERROR("Notifier %p is already registered\n", object);
12687 + return NULL;
12688 + }
12689 +
12690 + notifier = alloc_notifier(mem_ctx, object);
12691 + if (!notifier) {
12692 + return NULL;
12693 + }
12694 +
12695 + DWC_CIRCLEQ_INSERT_TAIL(&manager->notifiers, notifier, list_entry);
12696 +
12697 + DWC_INFO("Notifier %p registered", object);
12698 + dump_manager();
12699 +
12700 + return notifier;
12701 +}
12702 +
12703 +void dwc_unregister_notifier(dwc_notifier_t *notifier)
12704 +{
12705 + DWC_ASSERT(manager, "Notification manager not found");
12706 +
12707 + if (!DWC_CIRCLEQ_EMPTY(&notifier->observers)) {
12708 + observer_t *o;
12709 +
12710 + DWC_ERROR("Notifier %p has active observers when removing\n", notifier->object);
12711 + DWC_CIRCLEQ_FOREACH(o, &notifier->observers, list_entry) {
12712 + DWC_DEBUGC(" %p watching %s\n", o->observer, o->notification);
12713 + }
12714 +
12715 + DWC_ASSERT(DWC_CIRCLEQ_EMPTY(&notifier->observers),
12716 + "Notifier %p has active observers when removing", notifier);
12717 + }
12718 +
12719 + DWC_CIRCLEQ_REMOVE_INIT(&manager->notifiers, notifier, list_entry);
12720 + free_notifier(notifier);
12721 +
12722 + DWC_INFO("Notifier unregistered");
12723 + dump_manager();
12724 +}
12725 +
12726 +/* Add an observer to observe the notifier for a particular state, event, or notification. */
12727 +int dwc_add_observer(void *observer, void *object, char *notification,
12728 + dwc_notifier_callback_t callback, void *data)
12729 +{
12730 + notifier_t *notifier = find_notifier(object);
12731 + observer_t *new_observer;
12732 +
12733 + if (!notifier) {
12734 + DWC_ERROR("Notifier %p is not found when adding observer\n", object);
12735 + return -DWC_E_INVALID;
12736 + }
12737 +
12738 + new_observer = alloc_observer(notifier->mem_ctx, observer, notification, callback, data);
12739 + if (!new_observer) {
12740 + return -DWC_E_NO_MEMORY;
12741 + }
12742 +
12743 + DWC_CIRCLEQ_INSERT_TAIL(&notifier->observers, new_observer, list_entry);
12744 +
12745 + DWC_INFO("Added observer %p to notifier %p observing notification %s, callback=%p, data=%p",
12746 + observer, object, notification, callback, data);
12747 +
12748 + dump_manager();
12749 + return 0;
12750 +}
12751 +
12752 +int dwc_remove_observer(void *observer)
12753 +{
12754 + notifier_t *n;
12755 +
12756 + DWC_ASSERT(manager, "Notification manager not found");
12757 +
12758 + DWC_CIRCLEQ_FOREACH(n, &manager->notifiers, list_entry) {
12759 + observer_t *o;
12760 + observer_t *o2;
12761 +
12762 + DWC_CIRCLEQ_FOREACH_SAFE(o, o2, &n->observers, list_entry) {
12763 + if (o->observer == observer) {
12764 + DWC_CIRCLEQ_REMOVE_INIT(&n->observers, o, list_entry);
12765 + DWC_INFO("Removing observer %p from notifier %p watching notification %s:",
12766 + o->observer, n->object, o->notification);
12767 + free_observer(n->mem_ctx, o);
12768 + }
12769 + }
12770 + }
12771 +
12772 + dump_manager();
12773 + return 0;
12774 +}
12775 +
12776 +typedef struct callback_data {
12777 + void *mem_ctx;
12778 + dwc_notifier_callback_t cb;
12779 + void *observer;
12780 + void *data;
12781 + void *object;
12782 + char *notification;
12783 + void *notification_data;
12784 +} cb_data_t;
12785 +
12786 +static void cb_task(void *data)
12787 +{
12788 + cb_data_t *cb = (cb_data_t *)data;
12789 +
12790 + cb->cb(cb->object, cb->notification, cb->observer, cb->notification_data, cb->data);
12791 + dwc_free(cb->mem_ctx, cb);
12792 +}
12793 +
12794 +void dwc_notify(dwc_notifier_t *notifier, char *notification, void *notification_data)
12795 +{
12796 + observer_t *o;
12797 +
12798 + DWC_ASSERT(manager, "Notification manager not found");
12799 +
12800 + DWC_CIRCLEQ_FOREACH(o, &notifier->observers, list_entry) {
12801 + int len = DWC_STRLEN(notification);
12802 +
12803 + if (DWC_STRLEN(o->notification) != len) {
12804 + continue;
12805 + }
12806 +
12807 + if (DWC_STRNCMP(o->notification, notification, len) == 0) {
12808 + cb_data_t *cb_data = dwc_alloc(notifier->mem_ctx, sizeof(cb_data_t));
12809 +
12810 + if (!cb_data) {
12811 + DWC_ERROR("Failed to allocate callback data\n");
12812 + return;
12813 + }
12814 +
12815 + cb_data->mem_ctx = notifier->mem_ctx;
12816 + cb_data->cb = o->callback;
12817 + cb_data->observer = o->observer;
12818 + cb_data->data = o->data;
12819 + cb_data->object = notifier->object;
12820 + cb_data->notification = notification;
12821 + cb_data->notification_data = notification_data;
12822 + DWC_DEBUGC("Observer found %p for notification %s\n", o->observer, notification);
12823 + DWC_WORKQ_SCHEDULE(manager->wq, cb_task, cb_data,
12824 + "Notify callback from %p for Notification %s, to observer %p",
12825 + cb_data->object, notification, cb_data->observer);
12826 + }
12827 + }
12828 +}
12829 +
12830 +#endif /* DWC_NOTIFYLIB */
12831 --- /dev/null
12832 +++ b/drivers/usb/host/dwc_common_port/dwc_notifier.h
12833 @@ -0,0 +1,122 @@
12834 +
12835 +#ifndef __DWC_NOTIFIER_H__
12836 +#define __DWC_NOTIFIER_H__
12837 +
12838 +#ifdef __cplusplus
12839 +extern "C" {
12840 +#endif
12841 +
12842 +#include "dwc_os.h"
12843 +
12844 +/** @file
12845 + *
12846 + * A simple implementation of the Observer pattern. Any "module" can
12847 + * register as an observer or notifier. The notion of "module" is abstract and
12848 + * can mean anything used to identify either an observer or notifier. Usually
12849 + * it will be a pointer to a data structure which contains some state, ie an
12850 + * object.
12851 + *
12852 + * Before any notifiers can be added, the global notification manager must be
12853 + * brought up with dwc_alloc_notification_manager().
12854 + * dwc_free_notification_manager() will bring it down and free all resources.
12855 + * These would typically be called upon module load and unload. The
12856 + * notification manager is a single global instance that handles all registered
12857 + * observable modules and observers so this should be done only once.
12858 + *
12859 + * A module can be observable by using Notifications to publicize some general
12860 + * information about it's state or operation. It does not care who listens, or
12861 + * even if anyone listens, or what they do with the information. The observable
12862 + * modules do not need to know any information about it's observers or their
12863 + * interface, or their state or data.
12864 + *
12865 + * Any module can register to emit Notifications. It should publish a list of
12866 + * notifications that it can emit and their behavior, such as when they will get
12867 + * triggered, and what information will be provided to the observer. Then it
12868 + * should register itself as an observable module. See dwc_register_notifier().
12869 + *
12870 + * Any module can observe any observable, registered module, provided it has a
12871 + * handle to the other module and knows what notifications to observe. See
12872 + * dwc_add_observer().
12873 + *
12874 + * A function of type dwc_notifier_callback_t is called whenever a notification
12875 + * is triggered with one or more observers observing it. This function is
12876 + * called in it's own process so it may sleep or block if needed. It is
12877 + * guaranteed to be called sometime after the notification has occurred and will
12878 + * be called once per each time the notification is triggered. It will NOT be
12879 + * called in the same process context used to trigger the notification.
12880 + *
12881 + * @section Limitiations
12882 + *
12883 + * Keep in mind that Notifications that can be triggered in rapid sucession may
12884 + * schedule too many processes too handle. Be aware of this limitation when
12885 + * designing to use notifications, and only add notifications for appropriate
12886 + * observable information.
12887 + *
12888 + * Also Notification callbacks are not synchronous. If you need to synchronize
12889 + * the behavior between module/observer you must use other means. And perhaps
12890 + * that will mean Notifications are not the proper solution.
12891 + */
12892 +
12893 +struct dwc_notifier;
12894 +typedef struct dwc_notifier dwc_notifier_t;
12895 +
12896 +/** The callback function must be of this type.
12897 + *
12898 + * @param object This is the object that is being observed.
12899 + * @param notification This is the notification that was triggered.
12900 + * @param observer This is the observer
12901 + * @param notification_data This is notification-specific data that the notifier
12902 + * has included in this notification. The value of this should be published in
12903 + * the documentation of the observable module with the notifications.
12904 + * @param user_data This is any custom data that the observer provided when
12905 + * adding itself as an observer to the notification. */
12906 +typedef void (*dwc_notifier_callback_t)(void *object, char *notification, void *observer,
12907 + void *notification_data, void *user_data);
12908 +
12909 +/** Brings up the notification manager. */
12910 +extern int dwc_alloc_notification_manager(void *mem_ctx, void *wkq_ctx);
12911 +/** Brings down the notification manager. */
12912 +extern void dwc_free_notification_manager(void);
12913 +
12914 +/** This function registers an observable module. A dwc_notifier_t object is
12915 + * returned to the observable module. This is an opaque object that is used by
12916 + * the observable module to trigger notifications. This object should only be
12917 + * accessible to functions that are authorized to trigger notifications for this
12918 + * module. Observers do not need this object. */
12919 +extern dwc_notifier_t *dwc_register_notifier(void *mem_ctx, void *object);
12920 +
12921 +/** This function unregisters an observable module. All observers have to be
12922 + * removed prior to unregistration. */
12923 +extern void dwc_unregister_notifier(dwc_notifier_t *notifier);
12924 +
12925 +/** Add a module as an observer to the observable module. The observable module
12926 + * needs to have previously registered with the notification manager.
12927 + *
12928 + * @param observer The observer module
12929 + * @param object The module to observe
12930 + * @param notification The notification to observe
12931 + * @param callback The callback function to call
12932 + * @param user_data Any additional user data to pass into the callback function */
12933 +extern int dwc_add_observer(void *observer, void *object, char *notification,
12934 + dwc_notifier_callback_t callback, void *user_data);
12935 +
12936 +/** Removes the specified observer from all notifications that it is currently
12937 + * observing. */
12938 +extern int dwc_remove_observer(void *observer);
12939 +
12940 +/** This function triggers a Notification. It should be called by the
12941 + * observable module, or any module or library which the observable module
12942 + * allows to trigger notification on it's behalf. Such as the dwc_cc_t.
12943 + *
12944 + * dwc_notify is a non-blocking function. Callbacks are scheduled called in
12945 + * their own process context for each trigger. Callbacks can be blocking.
12946 + * dwc_notify can be called from interrupt context if needed.
12947 + *
12948 + */
12949 +void dwc_notify(dwc_notifier_t *notifier, char *notification, void *notification_data);
12950 +
12951 +#ifdef __cplusplus
12952 +}
12953 +#endif
12954 +
12955 +#endif /* __DWC_NOTIFIER_H__ */
12956 --- /dev/null
12957 +++ b/drivers/usb/host/dwc_common_port/dwc_os.h
12958 @@ -0,0 +1,1276 @@
12959 +/* =========================================================================
12960 + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_os.h $
12961 + * $Revision: #14 $
12962 + * $Date: 2010/11/04 $
12963 + * $Change: 1621695 $
12964 + *
12965 + * Synopsys Portability Library Software and documentation
12966 + * (hereinafter, "Software") is an Unsupported proprietary work of
12967 + * Synopsys, Inc. unless otherwise expressly agreed to in writing
12968 + * between Synopsys and you.
12969 + *
12970 + * The Software IS NOT an item of Licensed Software or Licensed Product
12971 + * under any End User Software License Agreement or Agreement for
12972 + * Licensed Product with Synopsys or any supplement thereto. You are
12973 + * permitted to use and redistribute this Software in source and binary
12974 + * forms, with or without modification, provided that redistributions
12975 + * of source code must retain this notice. You may not view, use,
12976 + * disclose, copy or distribute this file or any information contained
12977 + * herein except pursuant to this license grant from Synopsys. If you
12978 + * do not agree with this notice, including the disclaimer below, then
12979 + * you are not authorized to use the Software.
12980 + *
12981 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
12982 + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
12983 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
12984 + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
12985 + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
12986 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
12987 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
12988 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
12989 + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12990 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
12991 + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
12992 + * DAMAGE.
12993 + * ========================================================================= */
12994 +#ifndef _DWC_OS_H_
12995 +#define _DWC_OS_H_
12996 +
12997 +#ifdef __cplusplus
12998 +extern "C" {
12999 +#endif
13000 +
13001 +/** @file
13002 + *
13003 + * DWC portability library, low level os-wrapper functions
13004 + *
13005 + */
13006 +
13007 +/* These basic types need to be defined by some OS header file or custom header
13008 + * file for your specific target architecture.
13009 + *
13010 + * uint8_t, int8_t, uint16_t, int16_t, uint32_t, int32_t, uint64_t, int64_t
13011 + *
13012 + * Any custom or alternate header file must be added and enabled here.
13013 + */
13014 +
13015 +#ifdef DWC_LINUX
13016 +# include <linux/types.h>
13017 +# ifdef CONFIG_DEBUG_MUTEXES
13018 +# include <linux/mutex.h>
13019 +# endif
13020 +# include <linux/spinlock.h>
13021 +# include <linux/errno.h>
13022 +# include <stdarg.h>
13023 +#endif
13024 +
13025 +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
13026 +# include <os_dep.h>
13027 +#endif
13028 +
13029 +
13030 +/** @name Primitive Types and Values */
13031 +
13032 +/** We define a boolean type for consistency. Can be either YES or NO */
13033 +typedef uint8_t dwc_bool_t;
13034 +#define YES 1
13035 +#define NO 0
13036 +
13037 +#ifdef DWC_LINUX
13038 +
13039 +/** @name Error Codes */
13040 +#define DWC_E_INVALID EINVAL
13041 +#define DWC_E_NO_MEMORY ENOMEM
13042 +#define DWC_E_NO_DEVICE ENODEV
13043 +#define DWC_E_NOT_SUPPORTED EOPNOTSUPP
13044 +#define DWC_E_TIMEOUT ETIMEDOUT
13045 +#define DWC_E_BUSY EBUSY
13046 +#define DWC_E_AGAIN EAGAIN
13047 +#define DWC_E_RESTART ERESTART
13048 +#define DWC_E_ABORT ECONNABORTED
13049 +#define DWC_E_SHUTDOWN ESHUTDOWN
13050 +#define DWC_E_NO_DATA ENODATA
13051 +#define DWC_E_DISCONNECT ECONNRESET
13052 +#define DWC_E_UNKNOWN EINVAL
13053 +#define DWC_E_NO_STREAM_RES ENOSR
13054 +#define DWC_E_COMMUNICATION ECOMM
13055 +#define DWC_E_OVERFLOW EOVERFLOW
13056 +#define DWC_E_PROTOCOL EPROTO
13057 +#define DWC_E_IN_PROGRESS EINPROGRESS
13058 +#define DWC_E_PIPE EPIPE
13059 +#define DWC_E_IO EIO
13060 +#define DWC_E_NO_SPACE ENOSPC
13061 +
13062 +#else
13063 +
13064 +/** @name Error Codes */
13065 +#define DWC_E_INVALID 1001
13066 +#define DWC_E_NO_MEMORY 1002
13067 +#define DWC_E_NO_DEVICE 1003
13068 +#define DWC_E_NOT_SUPPORTED 1004
13069 +#define DWC_E_TIMEOUT 1005
13070 +#define DWC_E_BUSY 1006
13071 +#define DWC_E_AGAIN 1007
13072 +#define DWC_E_RESTART 1008
13073 +#define DWC_E_ABORT 1009
13074 +#define DWC_E_SHUTDOWN 1010
13075 +#define DWC_E_NO_DATA 1011
13076 +#define DWC_E_DISCONNECT 2000
13077 +#define DWC_E_UNKNOWN 3000
13078 +#define DWC_E_NO_STREAM_RES 4001
13079 +#define DWC_E_COMMUNICATION 4002
13080 +#define DWC_E_OVERFLOW 4003
13081 +#define DWC_E_PROTOCOL 4004
13082 +#define DWC_E_IN_PROGRESS 4005
13083 +#define DWC_E_PIPE 4006
13084 +#define DWC_E_IO 4007
13085 +#define DWC_E_NO_SPACE 4008
13086 +
13087 +#endif
13088 +
13089 +
13090 +/** @name Tracing/Logging Functions
13091 + *
13092 + * These function provide the capability to add tracing, debugging, and error
13093 + * messages, as well exceptions as assertions. The WUDEV uses these
13094 + * extensively. These could be logged to the main console, the serial port, an
13095 + * internal buffer, etc. These functions could also be no-op if they are too
13096 + * expensive on your system. By default undefining the DEBUG macro already
13097 + * no-ops some of these functions. */
13098 +
13099 +/** Returns non-zero if in interrupt context. */
13100 +extern dwc_bool_t DWC_IN_IRQ(void);
13101 +#define dwc_in_irq DWC_IN_IRQ
13102 +
13103 +/** Returns "IRQ" if DWC_IN_IRQ is true. */
13104 +static inline char *dwc_irq(void) {
13105 + return DWC_IN_IRQ() ? "IRQ" : "";
13106 +}
13107 +
13108 +/** Returns non-zero if in bottom-half context. */
13109 +extern dwc_bool_t DWC_IN_BH(void);
13110 +#define dwc_in_bh DWC_IN_BH
13111 +
13112 +/** Returns "BH" if DWC_IN_BH is true. */
13113 +static inline char *dwc_bh(void) {
13114 + return DWC_IN_BH() ? "BH" : "";
13115 +}
13116 +
13117 +/**
13118 + * A vprintf() clone. Just call vprintf if you've got it.
13119 + */
13120 +extern void DWC_VPRINTF(char *format, va_list args);
13121 +#define dwc_vprintf DWC_VPRINTF
13122 +
13123 +/**
13124 + * A vsnprintf() clone. Just call vprintf if you've got it.
13125 + */
13126 +extern int DWC_VSNPRINTF(char *str, int size, char *format, va_list args);
13127 +#define dwc_vsnprintf DWC_VSNPRINTF
13128 +
13129 +/**
13130 + * printf() clone. Just call printf if you've go it.
13131 + */
13132 +extern void DWC_PRINTF(char *format, ...)
13133 +/* This provides compiler level static checking of the parameters if you're
13134 + * using GCC. */
13135 +#ifdef __GNUC__
13136 + __attribute__ ((format(printf, 1, 2)));
13137 +#else
13138 + ;
13139 +#endif
13140 +#define dwc_printf DWC_PRINTF
13141 +
13142 +/**
13143 + * sprintf() clone. Just call sprintf if you've got it.
13144 + */
13145 +extern int DWC_SPRINTF(char *string, char *format, ...)
13146 +#ifdef __GNUC__
13147 + __attribute__ ((format(printf, 2, 3)));
13148 +#else
13149 + ;
13150 +#endif
13151 +#define dwc_sprintf DWC_SPRINTF
13152 +
13153 +/**
13154 + * snprintf() clone. Just call snprintf if you've got it.
13155 + */
13156 +extern int DWC_SNPRINTF(char *string, int size, char *format, ...)
13157 +#ifdef __GNUC__
13158 + __attribute__ ((format(printf, 3, 4)));
13159 +#else
13160 + ;
13161 +#endif
13162 +#define dwc_snprintf DWC_SNPRINTF
13163 +
13164 +/**
13165 + * Prints a WARNING message. On systems that don't differentiate between
13166 + * warnings and regular log messages, just print it. Indicates that something
13167 + * may be wrong with the driver. Works like printf().
13168 + *
13169 + * Use the DWC_WARN macro to call this function.
13170 + */
13171 +extern void __DWC_WARN(char *format, ...)
13172 +#ifdef __GNUC__
13173 + __attribute__ ((format(printf, 1, 2)));
13174 +#else
13175 + ;
13176 +#endif
13177 +
13178 +/**
13179 + * Prints an error message. On systems that don't differentiate between errors
13180 + * and regular log messages, just print it. Indicates that something went wrong
13181 + * with the driver. Works like printf().
13182 + *
13183 + * Use the DWC_ERROR macro to call this function.
13184 + */
13185 +extern void __DWC_ERROR(char *format, ...)
13186 +#ifdef __GNUC__
13187 + __attribute__ ((format(printf, 1, 2)));
13188 +#else
13189 + ;
13190 +#endif
13191 +
13192 +/**
13193 + * Prints an exception error message and takes some user-defined action such as
13194 + * print out a backtrace or trigger a breakpoint. Indicates that something went
13195 + * abnormally wrong with the driver such as programmer error, or other
13196 + * exceptional condition. It should not be ignored so even on systems without
13197 + * printing capability, some action should be taken to notify the developer of
13198 + * it. Works like printf().
13199 + */
13200 +extern void DWC_EXCEPTION(char *format, ...)
13201 +#ifdef __GNUC__
13202 + __attribute__ ((format(printf, 1, 2)));
13203 +#else
13204 + ;
13205 +#endif
13206 +#define dwc_exception DWC_EXCEPTION
13207 +
13208 +#ifndef DWC_OTG_DEBUG_LEV
13209 +#define DWC_OTG_DEBUG_LEV 0
13210 +#endif
13211 +
13212 +#ifdef DEBUG
13213 +/**
13214 + * Prints out a debug message. Used for logging/trace messages.
13215 + *
13216 + * Use the DWC_DEBUG macro to call this function
13217 + */
13218 +extern void __DWC_DEBUG(char *format, ...)
13219 +#ifdef __GNUC__
13220 + __attribute__ ((format(printf, 1, 2)));
13221 +#else
13222 + ;
13223 +#endif
13224 +#else
13225 +#define __DWC_DEBUG printk
13226 +#endif
13227 +
13228 +/**
13229 + * Prints out a Debug message.
13230 + */
13231 +#define DWC_DEBUG(_format, _args...) __DWC_DEBUG("DEBUG:%s:%s: " _format "\n", \
13232 + __func__, dwc_irq(), ## _args)
13233 +#define dwc_debug DWC_DEBUG
13234 +/**
13235 + * Prints out a Debug message if enabled at compile time.
13236 + */
13237 +#if DWC_OTG_DEBUG_LEV > 0
13238 +#define DWC_DEBUGC(_format, _args...) DWC_DEBUG(_format, ##_args )
13239 +#else
13240 +#define DWC_DEBUGC(_format, _args...)
13241 +#endif
13242 +#define dwc_debugc DWC_DEBUGC
13243 +/**
13244 + * Prints out an informative message.
13245 + */
13246 +#define DWC_INFO(_format, _args...) DWC_PRINTF("INFO:%s: " _format "\n", \
13247 + dwc_irq(), ## _args)
13248 +#define dwc_info DWC_INFO
13249 +/**
13250 + * Prints out an informative message if enabled at compile time.
13251 + */
13252 +#if DWC_OTG_DEBUG_LEV > 1
13253 +#define DWC_INFOC(_format, _args...) DWC_INFO(_format, ##_args )
13254 +#else
13255 +#define DWC_INFOC(_format, _args...)
13256 +#endif
13257 +#define dwc_infoc DWC_INFOC
13258 +/**
13259 + * Prints out a warning message.
13260 + */
13261 +#define DWC_WARN(_format, _args...) __DWC_WARN("WARN:%s:%s:%d: " _format "\n", \
13262 + dwc_irq(), __func__, __LINE__, ## _args)
13263 +#define dwc_warn DWC_WARN
13264 +/**
13265 + * Prints out an error message.
13266 + */
13267 +#define DWC_ERROR(_format, _args...) __DWC_ERROR("ERROR:%s:%s:%d: " _format "\n", \
13268 + dwc_irq(), __func__, __LINE__, ## _args)
13269 +#define dwc_error DWC_ERROR
13270 +
13271 +#define DWC_PROTO_ERROR(_format, _args...) __DWC_WARN("ERROR:%s:%s:%d: " _format "\n", \
13272 + dwc_irq(), __func__, __LINE__, ## _args)
13273 +#define dwc_proto_error DWC_PROTO_ERROR
13274 +
13275 +#ifdef DEBUG
13276 +/** Prints out a exception error message if the _expr expression fails. Disabled
13277 + * if DEBUG is not enabled. */
13278 +#define DWC_ASSERT(_expr, _format, _args...) do { \
13279 + if (!(_expr)) { DWC_EXCEPTION("%s:%s:%d: " _format "\n", dwc_irq(), \
13280 + __FILE__, __LINE__, ## _args); } \
13281 + } while (0)
13282 +#else
13283 +#define DWC_ASSERT(_x...)
13284 +#endif
13285 +#define dwc_assert DWC_ASSERT
13286 +
13287 +
13288 +/** @name Byte Ordering
13289 + * The following functions are for conversions between processor's byte ordering
13290 + * and specific ordering you want.
13291 + */
13292 +
13293 +/** Converts 32 bit data in CPU byte ordering to little endian. */
13294 +extern uint32_t DWC_CPU_TO_LE32(uint32_t *p);
13295 +#define dwc_cpu_to_le32 DWC_CPU_TO_LE32
13296 +
13297 +/** Converts 32 bit data in CPU byte orderint to big endian. */
13298 +extern uint32_t DWC_CPU_TO_BE32(uint32_t *p);
13299 +#define dwc_cpu_to_be32 DWC_CPU_TO_BE32
13300 +
13301 +/** Converts 32 bit little endian data to CPU byte ordering. */
13302 +extern uint32_t DWC_LE32_TO_CPU(uint32_t *p);
13303 +#define dwc_le32_to_cpu DWC_LE32_TO_CPU
13304 +
13305 +/** Converts 32 bit big endian data to CPU byte ordering. */
13306 +extern uint32_t DWC_BE32_TO_CPU(uint32_t *p);
13307 +#define dwc_be32_to_cpu DWC_BE32_TO_CPU
13308 +
13309 +/** Converts 16 bit data in CPU byte ordering to little endian. */
13310 +extern uint16_t DWC_CPU_TO_LE16(uint16_t *p);
13311 +#define dwc_cpu_to_le16 DWC_CPU_TO_LE16
13312 +
13313 +/** Converts 16 bit data in CPU byte orderint to big endian. */
13314 +extern uint16_t DWC_CPU_TO_BE16(uint16_t *p);
13315 +#define dwc_cpu_to_be16 DWC_CPU_TO_BE16
13316 +
13317 +/** Converts 16 bit little endian data to CPU byte ordering. */
13318 +extern uint16_t DWC_LE16_TO_CPU(uint16_t *p);
13319 +#define dwc_le16_to_cpu DWC_LE16_TO_CPU
13320 +
13321 +/** Converts 16 bit bi endian data to CPU byte ordering. */
13322 +extern uint16_t DWC_BE16_TO_CPU(uint16_t *p);
13323 +#define dwc_be16_to_cpu DWC_BE16_TO_CPU
13324 +
13325 +
13326 +/** @name Register Read/Write
13327 + *
13328 + * The following six functions should be implemented to read/write registers of
13329 + * 32-bit and 64-bit sizes. All modules use this to read/write register values.
13330 + * The reg value is a pointer to the register calculated from the void *base
13331 + * variable passed into the driver when it is started. */
13332 +
13333 +#ifdef DWC_LINUX
13334 +/* Linux doesn't need any extra parameters for register read/write, so we
13335 + * just throw away the IO context parameter.
13336 + */
13337 +/** Reads the content of a 32-bit register. */
13338 +extern uint32_t DWC_READ_REG32(uint32_t volatile *reg);
13339 +#define dwc_read_reg32(_ctx_,_reg_) DWC_READ_REG32(_reg_)
13340 +
13341 +/** Reads the content of a 64-bit register. */
13342 +extern uint64_t DWC_READ_REG64(uint64_t volatile *reg);
13343 +#define dwc_read_reg64(_ctx_,_reg_) DWC_READ_REG64(_reg_)
13344 +
13345 +/** Writes to a 32-bit register. */
13346 +extern void DWC_WRITE_REG32(uint32_t volatile *reg, uint32_t value);
13347 +#define dwc_write_reg32(_ctx_,_reg_,_val_) DWC_WRITE_REG32(_reg_, _val_)
13348 +
13349 +/** Writes to a 64-bit register. */
13350 +extern void DWC_WRITE_REG64(uint64_t volatile *reg, uint64_t value);
13351 +#define dwc_write_reg64(_ctx_,_reg_,_val_) DWC_WRITE_REG64(_reg_, _val_)
13352 +
13353 +/**
13354 + * Modify bit values in a register. Using the
13355 + * algorithm: (reg_contents & ~clear_mask) | set_mask.
13356 + */
13357 +extern void DWC_MODIFY_REG32(uint32_t volatile *reg, uint32_t clear_mask, uint32_t set_mask);
13358 +#define dwc_modify_reg32(_ctx_,_reg_,_cmsk_,_smsk_) DWC_MODIFY_REG32(_reg_,_cmsk_,_smsk_)
13359 +extern void DWC_MODIFY_REG64(uint64_t volatile *reg, uint64_t clear_mask, uint64_t set_mask);
13360 +#define dwc_modify_reg64(_ctx_,_reg_,_cmsk_,_smsk_) DWC_MODIFY_REG64(_reg_,_cmsk_,_smsk_)
13361 +
13362 +#endif /* DWC_LINUX */
13363 +
13364 +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
13365 +typedef struct dwc_ioctx {
13366 + struct device *dev;
13367 + bus_space_tag_t iot;
13368 + bus_space_handle_t ioh;
13369 +} dwc_ioctx_t;
13370 +
13371 +/** BSD needs two extra parameters for register read/write, so we pass
13372 + * them in using the IO context parameter.
13373 + */
13374 +/** Reads the content of a 32-bit register. */
13375 +extern uint32_t DWC_READ_REG32(void *io_ctx, uint32_t volatile *reg);
13376 +#define dwc_read_reg32 DWC_READ_REG32
13377 +
13378 +/** Reads the content of a 64-bit register. */
13379 +extern uint64_t DWC_READ_REG64(void *io_ctx, uint64_t volatile *reg);
13380 +#define dwc_read_reg64 DWC_READ_REG64
13381 +
13382 +/** Writes to a 32-bit register. */
13383 +extern void DWC_WRITE_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t value);
13384 +#define dwc_write_reg32 DWC_WRITE_REG32
13385 +
13386 +/** Writes to a 64-bit register. */
13387 +extern void DWC_WRITE_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t value);
13388 +#define dwc_write_reg64 DWC_WRITE_REG64
13389 +
13390 +/**
13391 + * Modify bit values in a register. Using the
13392 + * algorithm: (reg_contents & ~clear_mask) | set_mask.
13393 + */
13394 +extern void DWC_MODIFY_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t clear_mask, uint32_t set_mask);
13395 +#define dwc_modify_reg32 DWC_MODIFY_REG32
13396 +extern void DWC_MODIFY_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t clear_mask, uint64_t set_mask);
13397 +#define dwc_modify_reg64 DWC_MODIFY_REG64
13398 +
13399 +#endif /* DWC_FREEBSD || DWC_NETBSD */
13400 +
13401 +/** @cond */
13402 +
13403 +/** @name Some convenience MACROS used internally. Define DWC_DEBUG_REGS to log the
13404 + * register writes. */
13405 +
13406 +#ifdef DWC_LINUX
13407 +
13408 +# ifdef DWC_DEBUG_REGS
13409 +
13410 +#define dwc_define_read_write_reg_n(_reg,_container_type) \
13411 +static inline uint32_t dwc_read_##_reg##_n(_container_type *container, int num) { \
13412 + return DWC_READ_REG32(&container->regs->_reg[num]); \
13413 +} \
13414 +static inline void dwc_write_##_reg##_n(_container_type *container, int num, uint32_t data) { \
13415 + DWC_DEBUG("WRITING %8s[%d]: %p: %08x", #_reg, num, \
13416 + &(((uint32_t*)container->regs->_reg)[num]), data); \
13417 + DWC_WRITE_REG32(&(((uint32_t*)container->regs->_reg)[num]), data); \
13418 +}
13419 +
13420 +#define dwc_define_read_write_reg(_reg,_container_type) \
13421 +static inline uint32_t dwc_read_##_reg(_container_type *container) { \
13422 + return DWC_READ_REG32(&container->regs->_reg); \
13423 +} \
13424 +static inline void dwc_write_##_reg(_container_type *container, uint32_t data) { \
13425 + DWC_DEBUG("WRITING %11s: %p: %08x", #_reg, &container->regs->_reg, data); \
13426 + DWC_WRITE_REG32(&container->regs->_reg, data); \
13427 +}
13428 +
13429 +# else /* DWC_DEBUG_REGS */
13430 +
13431 +#define dwc_define_read_write_reg_n(_reg,_container_type) \
13432 +static inline uint32_t dwc_read_##_reg##_n(_container_type *container, int num) { \
13433 + return DWC_READ_REG32(&container->regs->_reg[num]); \
13434 +} \
13435 +static inline void dwc_write_##_reg##_n(_container_type *container, int num, uint32_t data) { \
13436 + DWC_WRITE_REG32(&(((uint32_t*)container->regs->_reg)[num]), data); \
13437 +}
13438 +
13439 +#define dwc_define_read_write_reg(_reg,_container_type) \
13440 +static inline uint32_t dwc_read_##_reg(_container_type *container) { \
13441 + return DWC_READ_REG32(&container->regs->_reg); \
13442 +} \
13443 +static inline void dwc_write_##_reg(_container_type *container, uint32_t data) { \
13444 + DWC_WRITE_REG32(&container->regs->_reg, data); \
13445 +}
13446 +
13447 +# endif /* DWC_DEBUG_REGS */
13448 +
13449 +#endif /* DWC_LINUX */
13450 +
13451 +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
13452 +
13453 +# ifdef DWC_DEBUG_REGS
13454 +
13455 +#define dwc_define_read_write_reg_n(_reg,_container_type) \
13456 +static inline uint32_t dwc_read_##_reg##_n(void *io_ctx, _container_type *container, int num) { \
13457 + return DWC_READ_REG32(io_ctx, &container->regs->_reg[num]); \
13458 +} \
13459 +static inline void dwc_write_##_reg##_n(void *io_ctx, _container_type *container, int num, uint32_t data) { \
13460 + DWC_DEBUG("WRITING %8s[%d]: %p: %08x", #_reg, num, \
13461 + &(((uint32_t*)container->regs->_reg)[num]), data); \
13462 + DWC_WRITE_REG32(io_ctx, &(((uint32_t*)container->regs->_reg)[num]), data); \
13463 +}
13464 +
13465 +#define dwc_define_read_write_reg(_reg,_container_type) \
13466 +static inline uint32_t dwc_read_##_reg(void *io_ctx, _container_type *container) { \
13467 + return DWC_READ_REG32(io_ctx, &container->regs->_reg); \
13468 +} \
13469 +static inline void dwc_write_##_reg(void *io_ctx, _container_type *container, uint32_t data) { \
13470 + DWC_DEBUG("WRITING %11s: %p: %08x", #_reg, &container->regs->_reg, data); \
13471 + DWC_WRITE_REG32(io_ctx, &container->regs->_reg, data); \
13472 +}
13473 +
13474 +# else /* DWC_DEBUG_REGS */
13475 +
13476 +#define dwc_define_read_write_reg_n(_reg,_container_type) \
13477 +static inline uint32_t dwc_read_##_reg##_n(void *io_ctx, _container_type *container, int num) { \
13478 + return DWC_READ_REG32(io_ctx, &container->regs->_reg[num]); \
13479 +} \
13480 +static inline void dwc_write_##_reg##_n(void *io_ctx, _container_type *container, int num, uint32_t data) { \
13481 + DWC_WRITE_REG32(io_ctx, &(((uint32_t*)container->regs->_reg)[num]), data); \
13482 +}
13483 +
13484 +#define dwc_define_read_write_reg(_reg,_container_type) \
13485 +static inline uint32_t dwc_read_##_reg(void *io_ctx, _container_type *container) { \
13486 + return DWC_READ_REG32(io_ctx, &container->regs->_reg); \
13487 +} \
13488 +static inline void dwc_write_##_reg(void *io_ctx, _container_type *container, uint32_t data) { \
13489 + DWC_WRITE_REG32(io_ctx, &container->regs->_reg, data); \
13490 +}
13491 +
13492 +# endif /* DWC_DEBUG_REGS */
13493 +
13494 +#endif /* DWC_FREEBSD || DWC_NETBSD */
13495 +
13496 +/** @endcond */
13497 +
13498 +
13499 +#ifdef DWC_CRYPTOLIB
13500 +/** @name Crypto Functions
13501 + *
13502 + * These are the low-level cryptographic functions used by the driver. */
13503 +
13504 +/** Perform AES CBC */
13505 +extern int DWC_AES_CBC(uint8_t *message, uint32_t messagelen, uint8_t *key, uint32_t keylen, uint8_t iv[16], uint8_t *out);
13506 +#define dwc_aes_cbc DWC_AES_CBC
13507 +
13508 +/** Fill the provided buffer with random bytes. These should be cryptographic grade random numbers. */
13509 +extern void DWC_RANDOM_BYTES(uint8_t *buffer, uint32_t length);
13510 +#define dwc_random_bytes DWC_RANDOM_BYTES
13511 +
13512 +/** Perform the SHA-256 hash function */
13513 +extern int DWC_SHA256(uint8_t *message, uint32_t len, uint8_t *out);
13514 +#define dwc_sha256 DWC_SHA256
13515 +
13516 +/** Calculated the HMAC-SHA256 */
13517 +extern int DWC_HMAC_SHA256(uint8_t *message, uint32_t messagelen, uint8_t *key, uint32_t keylen, uint8_t *out);
13518 +#define dwc_hmac_sha256 DWC_HMAC_SHA256
13519 +
13520 +#endif /* DWC_CRYPTOLIB */
13521 +
13522 +
13523 +/** @name Memory Allocation
13524 + *
13525 + * These function provide access to memory allocation. There are only 2 DMA
13526 + * functions and 3 Regular memory functions that need to be implemented. None
13527 + * of the memory debugging routines need to be implemented. The allocation
13528 + * routines all ZERO the contents of the memory.
13529 + *
13530 + * Defining DWC_DEBUG_MEMORY turns on memory debugging and statistic gathering.
13531 + * This checks for memory leaks, keeping track of alloc/free pairs. It also
13532 + * keeps track of how much memory the driver is using at any given time. */
13533 +
13534 +#define DWC_PAGE_SIZE 4096
13535 +#define DWC_PAGE_OFFSET(addr) (((uint32_t)addr) & 0xfff)
13536 +#define DWC_PAGE_ALIGNED(addr) ((((uint32_t)addr) & 0xfff) == 0)
13537 +
13538 +#define DWC_INVALID_DMA_ADDR 0x0
13539 +
13540 +#ifdef DWC_LINUX
13541 +/** Type for a DMA address */
13542 +typedef dma_addr_t dwc_dma_t;
13543 +#endif
13544 +
13545 +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
13546 +typedef bus_addr_t dwc_dma_t;
13547 +#endif
13548 +
13549 +#ifdef DWC_FREEBSD
13550 +typedef struct dwc_dmactx {
13551 + struct device *dev;
13552 + bus_dma_tag_t dma_tag;
13553 + bus_dmamap_t dma_map;
13554 + bus_addr_t dma_paddr;
13555 + void *dma_vaddr;
13556 +} dwc_dmactx_t;
13557 +#endif
13558 +
13559 +#ifdef DWC_NETBSD
13560 +typedef struct dwc_dmactx {
13561 + struct device *dev;
13562 + bus_dma_tag_t dma_tag;
13563 + bus_dmamap_t dma_map;
13564 + bus_dma_segment_t segs[1];
13565 + int nsegs;
13566 + bus_addr_t dma_paddr;
13567 + void *dma_vaddr;
13568 +} dwc_dmactx_t;
13569 +#endif
13570 +
13571 +/* @todo these functions will be added in the future */
13572 +#if 0
13573 +/**
13574 + * Creates a DMA pool from which you can allocate DMA buffers. Buffers
13575 + * allocated from this pool will be guaranteed to meet the size, alignment, and
13576 + * boundary requirements specified.
13577 + *
13578 + * @param[in] size Specifies the size of the buffers that will be allocated from
13579 + * this pool.
13580 + * @param[in] align Specifies the byte alignment requirements of the buffers
13581 + * allocated from this pool. Must be a power of 2.
13582 + * @param[in] boundary Specifies the N-byte boundary that buffers allocated from
13583 + * this pool must not cross.
13584 + *
13585 + * @returns A pointer to an internal opaque structure which is not to be
13586 + * accessed outside of these library functions. Use this handle to specify
13587 + * which pools to allocate/free DMA buffers from and also to destroy the pool,
13588 + * when you are done with it.
13589 + */
13590 +extern dwc_pool_t *DWC_DMA_POOL_CREATE(uint32_t size, uint32_t align, uint32_t boundary);
13591 +
13592 +/**
13593 + * Destroy a DMA pool. All buffers allocated from that pool must be freed first.
13594 + */
13595 +extern void DWC_DMA_POOL_DESTROY(dwc_pool_t *pool);
13596 +
13597 +/**
13598 + * Allocate a buffer from the specified DMA pool and zeros its contents.
13599 + */
13600 +extern void *DWC_DMA_POOL_ALLOC(dwc_pool_t *pool, uint64_t *dma_addr);
13601 +
13602 +/**
13603 + * Free a previously allocated buffer from the DMA pool.
13604 + */
13605 +extern void DWC_DMA_POOL_FREE(dwc_pool_t *pool, void *vaddr, void *daddr);
13606 +#endif
13607 +
13608 +/** Allocates a DMA capable buffer and zeroes its contents. */
13609 +extern void *__DWC_DMA_ALLOC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr);
13610 +
13611 +/** Allocates a DMA capable buffer and zeroes its contents in atomic contest */
13612 +extern void *__DWC_DMA_ALLOC_ATOMIC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr);
13613 +
13614 +/** Frees a previously allocated buffer. */
13615 +extern void __DWC_DMA_FREE(void *dma_ctx, uint32_t size, void *virt_addr, dwc_dma_t dma_addr);
13616 +
13617 +/** Allocates a block of memory and zeroes its contents. */
13618 +extern void *__DWC_ALLOC(void *mem_ctx, uint32_t size);
13619 +
13620 +/** Allocates a block of memory and zeroes its contents, in an atomic manner
13621 + * which can be used inside interrupt context. The size should be sufficiently
13622 + * small, a few KB at most, such that failures are not likely to occur. Can just call
13623 + * __DWC_ALLOC if it is atomic. */
13624 +extern void *__DWC_ALLOC_ATOMIC(void *mem_ctx, uint32_t size);
13625 +
13626 +/** Frees a previously allocated buffer. */
13627 +extern void __DWC_FREE(void *mem_ctx, void *addr);
13628 +
13629 +#ifndef DWC_DEBUG_MEMORY
13630 +
13631 +#define DWC_ALLOC(_size_) __DWC_ALLOC(NULL, _size_)
13632 +#define DWC_ALLOC_ATOMIC(_size_) __DWC_ALLOC_ATOMIC(NULL, _size_)
13633 +#define DWC_FREE(_addr_) __DWC_FREE(NULL, _addr_)
13634 +
13635 +# ifdef DWC_LINUX
13636 +#define DWC_DMA_ALLOC(_dev, _size_, _dma_) __DWC_DMA_ALLOC(_dev, _size_, _dma_)
13637 +#define DWC_DMA_ALLOC_ATOMIC(_dev, _size_, _dma_) __DWC_DMA_ALLOC_ATOMIC(_dev, _size_, _dma_)
13638 +#define DWC_DMA_FREE(_dev, _size_,_virt_, _dma_) __DWC_DMA_FREE(_dev, _size_, _virt_, _dma_)
13639 +# endif
13640 +
13641 +# if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
13642 +#define DWC_DMA_ALLOC __DWC_DMA_ALLOC
13643 +#define DWC_DMA_FREE __DWC_DMA_FREE
13644 +# endif
13645 +extern void *dwc_dma_alloc_atomic_debug(uint32_t size, dwc_dma_t *dma_addr, char const *func, int line);
13646 +
13647 +#else /* DWC_DEBUG_MEMORY */
13648 +
13649 +extern void *dwc_alloc_debug(void *mem_ctx, uint32_t size, char const *func, int line);
13650 +extern void *dwc_alloc_atomic_debug(void *mem_ctx, uint32_t size, char const *func, int line);
13651 +extern void dwc_free_debug(void *mem_ctx, void *addr, char const *func, int line);
13652 +extern void *dwc_dma_alloc_debug(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr,
13653 + char const *func, int line);
13654 +extern void *dwc_dma_alloc_atomic_debug(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr,
13655 + char const *func, int line);
13656 +extern void dwc_dma_free_debug(void *dma_ctx, uint32_t size, void *virt_addr,
13657 + dwc_dma_t dma_addr, char const *func, int line);
13658 +
13659 +extern int dwc_memory_debug_start(void *mem_ctx);
13660 +extern void dwc_memory_debug_stop(void);
13661 +extern void dwc_memory_debug_report(void);
13662 +
13663 +#define DWC_ALLOC(_size_) dwc_alloc_debug(NULL, _size_, __func__, __LINE__)
13664 +#define DWC_ALLOC_ATOMIC(_size_) dwc_alloc_atomic_debug(NULL, _size_, \
13665 + __func__, __LINE__)
13666 +#define DWC_FREE(_addr_) dwc_free_debug(NULL, _addr_, __func__, __LINE__)
13667 +
13668 +# ifdef DWC_LINUX
13669 +#define DWC_DMA_ALLOC(_dev, _size_, _dma_) \
13670 + dwc_dma_alloc_debug(_dev, _size_, _dma_, __func__, __LINE__)
13671 +#define DWC_DMA_ALLOC_ATOMIC(_dev, _size_, _dma_) \
13672 + dwc_dma_alloc_atomic_debug(_dev, _size_, _dma_, __func__, __LINE__)
13673 +#define DWC_DMA_FREE(_dev, _size_, _virt_, _dma_) \
13674 + dwc_dma_free_debug(_dev, _size_, _virt_, _dma_, __func__, __LINE__)
13675 +# endif
13676 +
13677 +# if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
13678 +#define DWC_DMA_ALLOC(_ctx_,_size_,_dma_) dwc_dma_alloc_debug(_ctx_, _size_, \
13679 + _dma_, __func__, __LINE__)
13680 +#define DWC_DMA_FREE(_ctx_,_size_,_virt_,_dma_) dwc_dma_free_debug(_ctx_, _size_, \
13681 + _virt_, _dma_, __func__, __LINE__)
13682 +# endif
13683 +
13684 +#endif /* DWC_DEBUG_MEMORY */
13685 +
13686 +#define dwc_alloc(_ctx_,_size_) DWC_ALLOC(_size_)
13687 +#define dwc_alloc_atomic(_ctx_,_size_) DWC_ALLOC_ATOMIC(_size_)
13688 +#define dwc_free(_ctx_,_addr_) DWC_FREE(_addr_)
13689 +
13690 +#ifdef DWC_LINUX
13691 +/* Linux doesn't need any extra parameters for DMA buffer allocation, so we
13692 + * just throw away the DMA context parameter.
13693 + */
13694 +#define dwc_dma_alloc(_ctx_,_size_,_dma_) DWC_DMA_ALLOC(_size_, _dma_)
13695 +#define dwc_dma_alloc_atomic(_ctx_,_size_,_dma_) DWC_DMA_ALLOC_ATOMIC(_size_, _dma_)
13696 +#define dwc_dma_free(_ctx_,_size_,_virt_,_dma_) DWC_DMA_FREE(_size_, _virt_, _dma_)
13697 +#endif
13698 +
13699 +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
13700 +/** BSD needs several extra parameters for DMA buffer allocation, so we pass
13701 + * them in using the DMA context parameter.
13702 + */
13703 +#define dwc_dma_alloc DWC_DMA_ALLOC
13704 +#define dwc_dma_free DWC_DMA_FREE
13705 +#endif
13706 +
13707 +
13708 +/** @name Memory and String Processing */
13709 +
13710 +/** memset() clone */
13711 +extern void *DWC_MEMSET(void *dest, uint8_t byte, uint32_t size);
13712 +#define dwc_memset DWC_MEMSET
13713 +
13714 +/** memcpy() clone */
13715 +extern void *DWC_MEMCPY(void *dest, void const *src, uint32_t size);
13716 +#define dwc_memcpy DWC_MEMCPY
13717 +
13718 +/** memmove() clone */
13719 +extern void *DWC_MEMMOVE(void *dest, void *src, uint32_t size);
13720 +#define dwc_memmove DWC_MEMMOVE
13721 +
13722 +/** memcmp() clone */
13723 +extern int DWC_MEMCMP(void *m1, void *m2, uint32_t size);
13724 +#define dwc_memcmp DWC_MEMCMP
13725 +
13726 +/** strcmp() clone */
13727 +extern int DWC_STRCMP(void *s1, void *s2);
13728 +#define dwc_strcmp DWC_STRCMP
13729 +
13730 +/** strncmp() clone */
13731 +extern int DWC_STRNCMP(void *s1, void *s2, uint32_t size);
13732 +#define dwc_strncmp DWC_STRNCMP
13733 +
13734 +/** strlen() clone, for NULL terminated ASCII strings */
13735 +extern int DWC_STRLEN(char const *str);
13736 +#define dwc_strlen DWC_STRLEN
13737 +
13738 +/** strcpy() clone, for NULL terminated ASCII strings */
13739 +extern char *DWC_STRCPY(char *to, const char *from);
13740 +#define dwc_strcpy DWC_STRCPY
13741 +
13742 +/** strdup() clone. If you wish to use memory allocation debugging, this
13743 + * implementation of strdup should use the DWC_* memory routines instead of
13744 + * calling a predefined strdup. Otherwise the memory allocated by this routine
13745 + * will not be seen by the debugging routines. */
13746 +extern char *DWC_STRDUP(char const *str);
13747 +#define dwc_strdup(_ctx_,_str_) DWC_STRDUP(_str_)
13748 +
13749 +/** NOT an atoi() clone. Read the description carefully. Returns an integer
13750 + * converted from the string str in base 10 unless the string begins with a "0x"
13751 + * in which case it is base 16. String must be a NULL terminated sequence of
13752 + * ASCII characters and may optionally begin with whitespace, a + or -, and a
13753 + * "0x" prefix if base 16. The remaining characters must be valid digits for
13754 + * the number and end with a NULL character. If any invalid characters are
13755 + * encountered or it returns with a negative error code and the results of the
13756 + * conversion are undefined. On sucess it returns 0. Overflow conditions are
13757 + * undefined. An example implementation using atoi() can be referenced from the
13758 + * Linux implementation. */
13759 +extern int DWC_ATOI(const char *str, int32_t *value);
13760 +#define dwc_atoi DWC_ATOI
13761 +
13762 +/** Same as above but for unsigned. */
13763 +extern int DWC_ATOUI(const char *str, uint32_t *value);
13764 +#define dwc_atoui DWC_ATOUI
13765 +
13766 +#ifdef DWC_UTFLIB
13767 +/** This routine returns a UTF16LE unicode encoded string from a UTF8 string. */
13768 +extern int DWC_UTF8_TO_UTF16LE(uint8_t const *utf8string, uint16_t *utf16string, unsigned len);
13769 +#define dwc_utf8_to_utf16le DWC_UTF8_TO_UTF16LE
13770 +#endif
13771 +
13772 +
13773 +/** @name Wait queues
13774 + *
13775 + * Wait queues provide a means of synchronizing between threads or processes. A
13776 + * process can block on a waitq if some condition is not true, waiting for it to
13777 + * become true. When the waitq is triggered all waiting process will get
13778 + * unblocked and the condition will be check again. Waitqs should be triggered
13779 + * every time a condition can potentially change.*/
13780 +struct dwc_waitq;
13781 +
13782 +/** Type for a waitq */
13783 +typedef struct dwc_waitq dwc_waitq_t;
13784 +
13785 +/** The type of waitq condition callback function. This is called every time
13786 + * condition is evaluated. */
13787 +typedef int (*dwc_waitq_condition_t)(void *data);
13788 +
13789 +/** Allocate a waitq */
13790 +extern dwc_waitq_t *DWC_WAITQ_ALLOC(void);
13791 +#define dwc_waitq_alloc(_ctx_) DWC_WAITQ_ALLOC()
13792 +
13793 +/** Free a waitq */
13794 +extern void DWC_WAITQ_FREE(dwc_waitq_t *wq);
13795 +#define dwc_waitq_free DWC_WAITQ_FREE
13796 +
13797 +/** Check the condition and if it is false, block on the waitq. When unblocked, check the
13798 + * condition again. The function returns when the condition becomes true. The return value
13799 + * is 0 on condition true, DWC_WAITQ_ABORTED on abort or killed, or DWC_WAITQ_UNKNOWN on error. */
13800 +extern int32_t DWC_WAITQ_WAIT(dwc_waitq_t *wq, dwc_waitq_condition_t cond, void *data);
13801 +#define dwc_waitq_wait DWC_WAITQ_WAIT
13802 +
13803 +/** Check the condition and if it is false, block on the waitq. When unblocked,
13804 + * check the condition again. The function returns when the condition become
13805 + * true or the timeout has passed. The return value is 0 on condition true or
13806 + * DWC_TIMED_OUT on timeout, or DWC_WAITQ_ABORTED, or DWC_WAITQ_UNKNOWN on
13807 + * error. */
13808 +extern int32_t DWC_WAITQ_WAIT_TIMEOUT(dwc_waitq_t *wq, dwc_waitq_condition_t cond,
13809 + void *data, int32_t msecs);
13810 +#define dwc_waitq_wait_timeout DWC_WAITQ_WAIT_TIMEOUT
13811 +
13812 +/** Trigger a waitq, unblocking all processes. This should be called whenever a condition
13813 + * has potentially changed. */
13814 +extern void DWC_WAITQ_TRIGGER(dwc_waitq_t *wq);
13815 +#define dwc_waitq_trigger DWC_WAITQ_TRIGGER
13816 +
13817 +/** Unblock all processes waiting on the waitq with an ABORTED result. */
13818 +extern void DWC_WAITQ_ABORT(dwc_waitq_t *wq);
13819 +#define dwc_waitq_abort DWC_WAITQ_ABORT
13820 +
13821 +
13822 +/** @name Threads
13823 + *
13824 + * A thread must be explicitly stopped. It must check DWC_THREAD_SHOULD_STOP
13825 + * whenever it is woken up, and then return. The DWC_THREAD_STOP function
13826 + * returns the value from the thread.
13827 + */
13828 +
13829 +struct dwc_thread;
13830 +
13831 +/** Type for a thread */
13832 +typedef struct dwc_thread dwc_thread_t;
13833 +
13834 +/** The thread function */
13835 +typedef int (*dwc_thread_function_t)(void *data);
13836 +
13837 +/** Create a thread and start it running the thread_function. Returns a handle
13838 + * to the thread */
13839 +extern dwc_thread_t *DWC_THREAD_RUN(dwc_thread_function_t func, char *name, void *data);
13840 +#define dwc_thread_run(_ctx_,_func_,_name_,_data_) DWC_THREAD_RUN(_func_, _name_, _data_)
13841 +
13842 +/** Stops a thread. Return the value returned by the thread. Or will return
13843 + * DWC_ABORT if the thread never started. */
13844 +extern int DWC_THREAD_STOP(dwc_thread_t *thread);
13845 +#define dwc_thread_stop DWC_THREAD_STOP
13846 +
13847 +/** Signifies to the thread that it must stop. */
13848 +#ifdef DWC_LINUX
13849 +/* Linux doesn't need any parameters for kthread_should_stop() */
13850 +extern dwc_bool_t DWC_THREAD_SHOULD_STOP(void);
13851 +#define dwc_thread_should_stop(_thrd_) DWC_THREAD_SHOULD_STOP()
13852 +
13853 +/* No thread_exit function in Linux */
13854 +#define dwc_thread_exit(_thrd_)
13855 +#endif
13856 +
13857 +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
13858 +/** BSD needs the thread pointer for kthread_suspend_check() */
13859 +extern dwc_bool_t DWC_THREAD_SHOULD_STOP(dwc_thread_t *thread);
13860 +#define dwc_thread_should_stop DWC_THREAD_SHOULD_STOP
13861 +
13862 +/** The thread must call this to exit. */
13863 +extern void DWC_THREAD_EXIT(dwc_thread_t *thread);
13864 +#define dwc_thread_exit DWC_THREAD_EXIT
13865 +#endif
13866 +
13867 +
13868 +/** @name Work queues
13869 + *
13870 + * Workqs are used to queue a callback function to be called at some later time,
13871 + * in another thread. */
13872 +struct dwc_workq;
13873 +
13874 +/** Type for a workq */
13875 +typedef struct dwc_workq dwc_workq_t;
13876 +
13877 +/** The type of the callback function to be called. */
13878 +typedef void (*dwc_work_callback_t)(void *data);
13879 +
13880 +/** Allocate a workq */
13881 +extern dwc_workq_t *DWC_WORKQ_ALLOC(char *name);
13882 +#define dwc_workq_alloc(_ctx_,_name_) DWC_WORKQ_ALLOC(_name_)
13883 +
13884 +/** Free a workq. All work must be completed before being freed. */
13885 +extern void DWC_WORKQ_FREE(dwc_workq_t *workq);
13886 +#define dwc_workq_free DWC_WORKQ_FREE
13887 +
13888 +/** Schedule a callback on the workq, passing in data. The function will be
13889 + * scheduled at some later time. */
13890 +extern void DWC_WORKQ_SCHEDULE(dwc_workq_t *workq, dwc_work_callback_t cb,
13891 + void *data, char *format, ...)
13892 +#ifdef __GNUC__
13893 + __attribute__ ((format(printf, 4, 5)));
13894 +#else
13895 + ;
13896 +#endif
13897 +#define dwc_workq_schedule DWC_WORKQ_SCHEDULE
13898 +
13899 +/** Schedule a callback on the workq, that will be called until at least
13900 + * given number miliseconds have passed. */
13901 +extern void DWC_WORKQ_SCHEDULE_DELAYED(dwc_workq_t *workq, dwc_work_callback_t cb,
13902 + void *data, uint32_t time, char *format, ...)
13903 +#ifdef __GNUC__
13904 + __attribute__ ((format(printf, 5, 6)));
13905 +#else
13906 + ;
13907 +#endif
13908 +#define dwc_workq_schedule_delayed DWC_WORKQ_SCHEDULE_DELAYED
13909 +
13910 +/** The number of processes in the workq */
13911 +extern int DWC_WORKQ_PENDING(dwc_workq_t *workq);
13912 +#define dwc_workq_pending DWC_WORKQ_PENDING
13913 +
13914 +/** Blocks until all the work in the workq is complete or timed out. Returns <
13915 + * 0 on timeout. */
13916 +extern int DWC_WORKQ_WAIT_WORK_DONE(dwc_workq_t *workq, int timeout);
13917 +#define dwc_workq_wait_work_done DWC_WORKQ_WAIT_WORK_DONE
13918 +
13919 +
13920 +/** @name Tasklets
13921 + *
13922 + */
13923 +struct dwc_tasklet;
13924 +
13925 +/** Type for a tasklet */
13926 +typedef struct dwc_tasklet dwc_tasklet_t;
13927 +
13928 +/** The type of the callback function to be called */
13929 +typedef void (*dwc_tasklet_callback_t)(void *data);
13930 +
13931 +/** Allocates a tasklet */
13932 +extern dwc_tasklet_t *DWC_TASK_ALLOC(char *name, dwc_tasklet_callback_t cb, void *data);
13933 +#define dwc_task_alloc(_ctx_,_name_,_cb_,_data_) DWC_TASK_ALLOC(_name_, _cb_, _data_)
13934 +
13935 +/** Frees a tasklet */
13936 +extern void DWC_TASK_FREE(dwc_tasklet_t *task);
13937 +#define dwc_task_free DWC_TASK_FREE
13938 +
13939 +/** Schedules a tasklet to run */
13940 +extern void DWC_TASK_SCHEDULE(dwc_tasklet_t *task);
13941 +#define dwc_task_schedule DWC_TASK_SCHEDULE
13942 +
13943 +extern void DWC_TASK_HI_SCHEDULE(dwc_tasklet_t *task);
13944 +#define dwc_task_hi_schedule DWC_TASK_HI_SCHEDULE
13945 +
13946 +/** @name Timer
13947 + *
13948 + * Callbacks must be small and atomic.
13949 + */
13950 +struct dwc_timer;
13951 +
13952 +/** Type for a timer */
13953 +typedef struct dwc_timer dwc_timer_t;
13954 +
13955 +/** The type of the callback function to be called */
13956 +typedef void (*dwc_timer_callback_t)(void *data);
13957 +
13958 +/** Allocates a timer */
13959 +extern dwc_timer_t *DWC_TIMER_ALLOC(char *name, dwc_timer_callback_t cb, void *data);
13960 +#define dwc_timer_alloc(_ctx_,_name_,_cb_,_data_) DWC_TIMER_ALLOC(_name_,_cb_,_data_)
13961 +
13962 +/** Frees a timer */
13963 +extern void DWC_TIMER_FREE(dwc_timer_t *timer);
13964 +#define dwc_timer_free DWC_TIMER_FREE
13965 +
13966 +/** Schedules the timer to run at time ms from now. And will repeat at every
13967 + * repeat_interval msec therafter
13968 + *
13969 + * Modifies a timer that is still awaiting execution to a new expiration time.
13970 + * The mod_time is added to the old time. */
13971 +extern void DWC_TIMER_SCHEDULE(dwc_timer_t *timer, uint32_t time);
13972 +#define dwc_timer_schedule DWC_TIMER_SCHEDULE
13973 +
13974 +/** Disables the timer from execution. */
13975 +extern void DWC_TIMER_CANCEL(dwc_timer_t *timer);
13976 +#define dwc_timer_cancel DWC_TIMER_CANCEL
13977 +
13978 +
13979 +/** @name Spinlocks
13980 + *
13981 + * These locks are used when the work between the lock/unlock is atomic and
13982 + * short. Interrupts are also disabled during the lock/unlock and thus they are
13983 + * suitable to lock between interrupt/non-interrupt context. They also lock
13984 + * between processes if you have multiple CPUs or Preemption. If you don't have
13985 + * multiple CPUS or Preemption, then the you can simply implement the
13986 + * DWC_SPINLOCK and DWC_SPINUNLOCK to disable and enable interrupts. Because
13987 + * the work between the lock/unlock is atomic, the process context will never
13988 + * change, and so you never have to lock between processes. */
13989 +
13990 +struct dwc_spinlock;
13991 +
13992 +/** Type for a spinlock */
13993 +typedef struct dwc_spinlock dwc_spinlock_t;
13994 +
13995 +/** Type for the 'flags' argument to spinlock funtions */
13996 +typedef unsigned long dwc_irqflags_t;
13997 +
13998 +/** Returns an initialized lock variable. This function should allocate and
13999 + * initialize the OS-specific data structure used for locking. This data
14000 + * structure is to be used for the DWC_LOCK and DWC_UNLOCK functions and should
14001 + * be freed by the DWC_FREE_LOCK when it is no longer used.
14002 + *
14003 + * For Linux Spinlock Debugging make it macro because the debugging routines use
14004 + * the symbol name to determine recursive locking. Using a wrapper function
14005 + * makes it falsely think recursive locking occurs. */
14006 +#if defined(DWC_LINUX) && defined(CONFIG_DEBUG_SPINLOCK)
14007 +#define DWC_SPINLOCK_ALLOC_LINUX_DEBUG(lock) ({ \
14008 + lock = DWC_ALLOC(sizeof(spinlock_t)); \
14009 + if (lock) { \
14010 + spin_lock_init((spinlock_t *)lock); \
14011 + } \
14012 +})
14013 +#else
14014 +extern dwc_spinlock_t *DWC_SPINLOCK_ALLOC(void);
14015 +#define dwc_spinlock_alloc(_ctx_) DWC_SPINLOCK_ALLOC()
14016 +#endif
14017 +
14018 +/** Frees an initialized lock variable. */
14019 +extern void DWC_SPINLOCK_FREE(dwc_spinlock_t *lock);
14020 +#define dwc_spinlock_free(_ctx_,_lock_) DWC_SPINLOCK_FREE(_lock_)
14021 +
14022 +/** Disables interrupts and blocks until it acquires the lock.
14023 + *
14024 + * @param lock Pointer to the spinlock.
14025 + * @param flags Unsigned long for irq flags storage.
14026 + */
14027 +extern void DWC_SPINLOCK_IRQSAVE(dwc_spinlock_t *lock, dwc_irqflags_t *flags);
14028 +#define dwc_spinlock_irqsave DWC_SPINLOCK_IRQSAVE
14029 +
14030 +/** Re-enables the interrupt and releases the lock.
14031 + *
14032 + * @param lock Pointer to the spinlock.
14033 + * @param flags Unsigned long for irq flags storage. Must be the same as was
14034 + * passed into DWC_LOCK.
14035 + */
14036 +extern void DWC_SPINUNLOCK_IRQRESTORE(dwc_spinlock_t *lock, dwc_irqflags_t flags);
14037 +#define dwc_spinunlock_irqrestore DWC_SPINUNLOCK_IRQRESTORE
14038 +
14039 +/** Blocks until it acquires the lock.
14040 + *
14041 + * @param lock Pointer to the spinlock.
14042 + */
14043 +extern void DWC_SPINLOCK(dwc_spinlock_t *lock);
14044 +#define dwc_spinlock DWC_SPINLOCK
14045 +
14046 +/** Releases the lock.
14047 + *
14048 + * @param lock Pointer to the spinlock.
14049 + */
14050 +extern void DWC_SPINUNLOCK(dwc_spinlock_t *lock);
14051 +#define dwc_spinunlock DWC_SPINUNLOCK
14052 +
14053 +
14054 +/** @name Mutexes
14055 + *
14056 + * Unlike spinlocks Mutexes lock only between processes and the work between the
14057 + * lock/unlock CAN block, therefore it CANNOT be called from interrupt context.
14058 + */
14059 +
14060 +struct dwc_mutex;
14061 +
14062 +/** Type for a mutex */
14063 +typedef struct dwc_mutex dwc_mutex_t;
14064 +
14065 +/* For Linux Mutex Debugging make it inline because the debugging routines use
14066 + * the symbol to determine recursive locking. This makes it falsely think
14067 + * recursive locking occurs. */
14068 +#if defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES)
14069 +#define DWC_MUTEX_ALLOC_LINUX_DEBUG(__mutexp) ({ \
14070 + __mutexp = (dwc_mutex_t *)DWC_ALLOC(sizeof(struct mutex)); \
14071 + mutex_init((struct mutex *)__mutexp); \
14072 +})
14073 +#endif
14074 +
14075 +/** Allocate a mutex */
14076 +extern dwc_mutex_t *DWC_MUTEX_ALLOC(void);
14077 +#define dwc_mutex_alloc(_ctx_) DWC_MUTEX_ALLOC()
14078 +
14079 +/* For memory leak debugging when using Linux Mutex Debugging */
14080 +#if defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES)
14081 +#define DWC_MUTEX_FREE(__mutexp) do { \
14082 + mutex_destroy((struct mutex *)__mutexp); \
14083 + DWC_FREE(__mutexp); \
14084 +} while(0)
14085 +#else
14086 +/** Free a mutex */
14087 +extern void DWC_MUTEX_FREE(dwc_mutex_t *mutex);
14088 +#define dwc_mutex_free(_ctx_,_mutex_) DWC_MUTEX_FREE(_mutex_)
14089 +#endif
14090 +
14091 +/** Lock a mutex */
14092 +extern void DWC_MUTEX_LOCK(dwc_mutex_t *mutex);
14093 +#define dwc_mutex_lock DWC_MUTEX_LOCK
14094 +
14095 +/** Non-blocking lock returns 1 on successful lock. */
14096 +extern int DWC_MUTEX_TRYLOCK(dwc_mutex_t *mutex);
14097 +#define dwc_mutex_trylock DWC_MUTEX_TRYLOCK
14098 +
14099 +/** Unlock a mutex */
14100 +extern void DWC_MUTEX_UNLOCK(dwc_mutex_t *mutex);
14101 +#define dwc_mutex_unlock DWC_MUTEX_UNLOCK
14102 +
14103 +
14104 +/** @name Time */
14105 +
14106 +/** Microsecond delay.
14107 + *
14108 + * @param usecs Microseconds to delay.
14109 + */
14110 +extern void DWC_UDELAY(uint32_t usecs);
14111 +#define dwc_udelay DWC_UDELAY
14112 +
14113 +/** Millisecond delay.
14114 + *
14115 + * @param msecs Milliseconds to delay.
14116 + */
14117 +extern void DWC_MDELAY(uint32_t msecs);
14118 +#define dwc_mdelay DWC_MDELAY
14119 +
14120 +/** Non-busy waiting.
14121 + * Sleeps for specified number of milliseconds.
14122 + *
14123 + * @param msecs Milliseconds to sleep.
14124 + */
14125 +extern void DWC_MSLEEP(uint32_t msecs);
14126 +#define dwc_msleep DWC_MSLEEP
14127 +
14128 +/**
14129 + * Returns number of milliseconds since boot.
14130 + */
14131 +extern uint32_t DWC_TIME(void);
14132 +#define dwc_time DWC_TIME
14133 +
14134 +
14135 +
14136 +
14137 +/* @mainpage DWC Portability and Common Library
14138 + *
14139 + * This is the documentation for the DWC Portability and Common Library.
14140 + *
14141 + * @section intro Introduction
14142 + *
14143 + * The DWC Portability library consists of wrapper calls and data structures to
14144 + * all low-level functions which are typically provided by the OS. The WUDEV
14145 + * driver uses only these functions. In order to port the WUDEV driver, only
14146 + * the functions in this library need to be re-implemented, with the same
14147 + * behavior as documented here.
14148 + *
14149 + * The Common library consists of higher level functions, which rely only on
14150 + * calling the functions from the DWC Portability library. These common
14151 + * routines are shared across modules. Some of the common libraries need to be
14152 + * used directly by the driver programmer when porting WUDEV. Such as the
14153 + * parameter and notification libraries.
14154 + *
14155 + * @section low Portability Library OS Wrapper Functions
14156 + *
14157 + * Any function starting with DWC and in all CAPS is a low-level OS-wrapper that
14158 + * needs to be implemented when porting, for example DWC_MUTEX_ALLOC(). All of
14159 + * these functions are included in the dwc_os.h file.
14160 + *
14161 + * There are many functions here covering a wide array of OS services. Please
14162 + * see dwc_os.h for details, and implementation notes for each function.
14163 + *
14164 + * @section common Common Library Functions
14165 + *
14166 + * Any function starting with dwc and in all lowercase is a common library
14167 + * routine. These functions have a portable implementation and do not need to
14168 + * be reimplemented when porting. The common routines can be used by any
14169 + * driver, and some must be used by the end user to control the drivers. For
14170 + * example, you must use the Parameter common library in order to set the
14171 + * parameters in the WUDEV module.
14172 + *
14173 + * The common libraries consist of the following:
14174 + *
14175 + * - Connection Contexts - Used internally and can be used by end-user. See dwc_cc.h
14176 + * - Parameters - Used internally and can be used by end-user. See dwc_params.h
14177 + * - Notifications - Used internally and can be used by end-user. See dwc_notifier.h
14178 + * - Lists - Used internally and can be used by end-user. See dwc_list.h
14179 + * - Memory Debugging - Used internally and can be used by end-user. See dwc_os.h
14180 + * - Modpow - Used internally only. See dwc_modpow.h
14181 + * - DH - Used internally only. See dwc_dh.h
14182 + * - Crypto - Used internally only. See dwc_crypto.h
14183 + *
14184 + *
14185 + * @section prereq Prerequistes For dwc_os.h
14186 + * @subsection types Data Types
14187 + *
14188 + * The dwc_os.h file assumes that several low-level data types are pre defined for the
14189 + * compilation environment. These data types are:
14190 + *
14191 + * - uint8_t - unsigned 8-bit data type
14192 + * - int8_t - signed 8-bit data type
14193 + * - uint16_t - unsigned 16-bit data type
14194 + * - int16_t - signed 16-bit data type
14195 + * - uint32_t - unsigned 32-bit data type
14196 + * - int32_t - signed 32-bit data type
14197 + * - uint64_t - unsigned 64-bit data type
14198 + * - int64_t - signed 64-bit data type
14199 + *
14200 + * Ensure that these are defined before using dwc_os.h. The easiest way to do
14201 + * that is to modify the top of the file to include the appropriate header.
14202 + * This is already done for the Linux environment. If the DWC_LINUX macro is
14203 + * defined, the correct header will be added. A standard header <stdint.h> is
14204 + * also used for environments where standard C headers are available.
14205 + *
14206 + * @subsection stdarg Variable Arguments
14207 + *
14208 + * Variable arguments are provided by a standard C header <stdarg.h>. it is
14209 + * available in Both the Linux and ANSI C enviornment. An equivalent must be
14210 + * provided in your enviornment in order to use dwc_os.h with the debug and
14211 + * tracing message functionality.
14212 + *
14213 + * @subsection thread Threading
14214 + *
14215 + * WUDEV Core must be run on an operating system that provides for multiple
14216 + * threads/processes. Threading can be implemented in many ways, even in
14217 + * embedded systems without an operating system. At the bare minimum, the
14218 + * system should be able to start any number of processes at any time to handle
14219 + * special work. It need not be a pre-emptive system. Process context can
14220 + * change upon a call to a blocking function. The hardware interrupt context
14221 + * that calls the module's ISR() function must be differentiable from process
14222 + * context, even if your processes are impemented via a hardware interrupt.
14223 + * Further locking mechanism between process must exist (or be implemented), and
14224 + * process context must have a way to disable interrupts for a period of time to
14225 + * lock them out. If all of this exists, the functions in dwc_os.h related to
14226 + * threading should be able to be implemented with the defined behavior.
14227 + *
14228 + */
14229 +
14230 +#ifdef __cplusplus
14231 +}
14232 +#endif
14233 +
14234 +#endif /* _DWC_OS_H_ */
14235 --- /dev/null
14236 +++ b/drivers/usb/host/dwc_common_port/usb.h
14237 @@ -0,0 +1,946 @@
14238 +/*
14239 + * Copyright (c) 1998 The NetBSD Foundation, Inc.
14240 + * All rights reserved.
14241 + *
14242 + * This code is derived from software contributed to The NetBSD Foundation
14243 + * by Lennart Augustsson (lennart@augustsson.net) at
14244 + * Carlstedt Research & Technology.
14245 + *
14246 + * Redistribution and use in source and binary forms, with or without
14247 + * modification, are permitted provided that the following conditions
14248 + * are met:
14249 + * 1. Redistributions of source code must retain the above copyright
14250 + * notice, this list of conditions and the following disclaimer.
14251 + * 2. Redistributions in binary form must reproduce the above copyright
14252 + * notice, this list of conditions and the following disclaimer in the
14253 + * documentation and/or other materials provided with the distribution.
14254 + * 3. All advertising materials mentioning features or use of this software
14255 + * must display the following acknowledgement:
14256 + * This product includes software developed by the NetBSD
14257 + * Foundation, Inc. and its contributors.
14258 + * 4. Neither the name of The NetBSD Foundation nor the names of its
14259 + * contributors may be used to endorse or promote products derived
14260 + * from this software without specific prior written permission.
14261 + *
14262 + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
14263 + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
14264 + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
14265 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
14266 + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
14267 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
14268 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
14269 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
14270 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
14271 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
14272 + * POSSIBILITY OF SUCH DAMAGE.
14273 + */
14274 +
14275 +/* Modified by Synopsys, Inc, 12/12/2007 */
14276 +
14277 +
14278 +#ifndef _USB_H_
14279 +#define _USB_H_
14280 +
14281 +#ifdef __cplusplus
14282 +extern "C" {
14283 +#endif
14284 +
14285 +/*
14286 + * The USB records contain some unaligned little-endian word
14287 + * components. The U[SG]ETW macros take care of both the alignment
14288 + * and endian problem and should always be used to access non-byte
14289 + * values.
14290 + */
14291 +typedef u_int8_t uByte;
14292 +typedef u_int8_t uWord[2];
14293 +typedef u_int8_t uDWord[4];
14294 +
14295 +#define USETW2(w,h,l) ((w)[0] = (u_int8_t)(l), (w)[1] = (u_int8_t)(h))
14296 +#define UCONSTW(x) { (x) & 0xff, ((x) >> 8) & 0xff }
14297 +#define UCONSTDW(x) { (x) & 0xff, ((x) >> 8) & 0xff, \
14298 + ((x) >> 16) & 0xff, ((x) >> 24) & 0xff }
14299 +
14300 +#if 1
14301 +#define UGETW(w) ((w)[0] | ((w)[1] << 8))
14302 +#define USETW(w,v) ((w)[0] = (u_int8_t)(v), (w)[1] = (u_int8_t)((v) >> 8))
14303 +#define UGETDW(w) ((w)[0] | ((w)[1] << 8) | ((w)[2] << 16) | ((w)[3] << 24))
14304 +#define USETDW(w,v) ((w)[0] = (u_int8_t)(v), \
14305 + (w)[1] = (u_int8_t)((v) >> 8), \
14306 + (w)[2] = (u_int8_t)((v) >> 16), \
14307 + (w)[3] = (u_int8_t)((v) >> 24))
14308 +#else
14309 +/*
14310 + * On little-endian machines that can handle unanliged accesses
14311 + * (e.g. i386) these macros can be replaced by the following.
14312 + */
14313 +#define UGETW(w) (*(u_int16_t *)(w))
14314 +#define USETW(w,v) (*(u_int16_t *)(w) = (v))
14315 +#define UGETDW(w) (*(u_int32_t *)(w))
14316 +#define USETDW(w,v) (*(u_int32_t *)(w) = (v))
14317 +#endif
14318 +
14319 +/*
14320 + * Macros for accessing UAS IU fields, which are big-endian
14321 + */
14322 +#define IUSETW2(w,h,l) ((w)[0] = (u_int8_t)(h), (w)[1] = (u_int8_t)(l))
14323 +#define IUCONSTW(x) { ((x) >> 8) & 0xff, (x) & 0xff }
14324 +#define IUCONSTDW(x) { ((x) >> 24) & 0xff, ((x) >> 16) & 0xff, \
14325 + ((x) >> 8) & 0xff, (x) & 0xff }
14326 +#define IUGETW(w) (((w)[0] << 8) | (w)[1])
14327 +#define IUSETW(w,v) ((w)[0] = (u_int8_t)((v) >> 8), (w)[1] = (u_int8_t)(v))
14328 +#define IUGETDW(w) (((w)[0] << 24) | ((w)[1] << 16) | ((w)[2] << 8) | (w)[3])
14329 +#define IUSETDW(w,v) ((w)[0] = (u_int8_t)((v) >> 24), \
14330 + (w)[1] = (u_int8_t)((v) >> 16), \
14331 + (w)[2] = (u_int8_t)((v) >> 8), \
14332 + (w)[3] = (u_int8_t)(v))
14333 +
14334 +#define UPACKED __attribute__((__packed__))
14335 +
14336 +typedef struct {
14337 + uByte bmRequestType;
14338 + uByte bRequest;
14339 + uWord wValue;
14340 + uWord wIndex;
14341 + uWord wLength;
14342 +} UPACKED usb_device_request_t;
14343 +
14344 +#define UT_GET_DIR(a) ((a) & 0x80)
14345 +#define UT_WRITE 0x00
14346 +#define UT_READ 0x80
14347 +
14348 +#define UT_GET_TYPE(a) ((a) & 0x60)
14349 +#define UT_STANDARD 0x00
14350 +#define UT_CLASS 0x20
14351 +#define UT_VENDOR 0x40
14352 +
14353 +#define UT_GET_RECIPIENT(a) ((a) & 0x1f)
14354 +#define UT_DEVICE 0x00
14355 +#define UT_INTERFACE 0x01
14356 +#define UT_ENDPOINT 0x02
14357 +#define UT_OTHER 0x03
14358 +
14359 +#define UT_READ_DEVICE (UT_READ | UT_STANDARD | UT_DEVICE)
14360 +#define UT_READ_INTERFACE (UT_READ | UT_STANDARD | UT_INTERFACE)
14361 +#define UT_READ_ENDPOINT (UT_READ | UT_STANDARD | UT_ENDPOINT)
14362 +#define UT_WRITE_DEVICE (UT_WRITE | UT_STANDARD | UT_DEVICE)
14363 +#define UT_WRITE_INTERFACE (UT_WRITE | UT_STANDARD | UT_INTERFACE)
14364 +#define UT_WRITE_ENDPOINT (UT_WRITE | UT_STANDARD | UT_ENDPOINT)
14365 +#define UT_READ_CLASS_DEVICE (UT_READ | UT_CLASS | UT_DEVICE)
14366 +#define UT_READ_CLASS_INTERFACE (UT_READ | UT_CLASS | UT_INTERFACE)
14367 +#define UT_READ_CLASS_OTHER (UT_READ | UT_CLASS | UT_OTHER)
14368 +#define UT_READ_CLASS_ENDPOINT (UT_READ | UT_CLASS | UT_ENDPOINT)
14369 +#define UT_WRITE_CLASS_DEVICE (UT_WRITE | UT_CLASS | UT_DEVICE)
14370 +#define UT_WRITE_CLASS_INTERFACE (UT_WRITE | UT_CLASS | UT_INTERFACE)
14371 +#define UT_WRITE_CLASS_OTHER (UT_WRITE | UT_CLASS | UT_OTHER)
14372 +#define UT_WRITE_CLASS_ENDPOINT (UT_WRITE | UT_CLASS | UT_ENDPOINT)
14373 +#define UT_READ_VENDOR_DEVICE (UT_READ | UT_VENDOR | UT_DEVICE)
14374 +#define UT_READ_VENDOR_INTERFACE (UT_READ | UT_VENDOR | UT_INTERFACE)
14375 +#define UT_READ_VENDOR_OTHER (UT_READ | UT_VENDOR | UT_OTHER)
14376 +#define UT_READ_VENDOR_ENDPOINT (UT_READ | UT_VENDOR | UT_ENDPOINT)
14377 +#define UT_WRITE_VENDOR_DEVICE (UT_WRITE | UT_VENDOR | UT_DEVICE)
14378 +#define UT_WRITE_VENDOR_INTERFACE (UT_WRITE | UT_VENDOR | UT_INTERFACE)
14379 +#define UT_WRITE_VENDOR_OTHER (UT_WRITE | UT_VENDOR | UT_OTHER)
14380 +#define UT_WRITE_VENDOR_ENDPOINT (UT_WRITE | UT_VENDOR | UT_ENDPOINT)
14381 +
14382 +/* Requests */
14383 +#define UR_GET_STATUS 0x00
14384 +#define USTAT_STANDARD_STATUS 0x00
14385 +#define WUSTAT_WUSB_FEATURE 0x01
14386 +#define WUSTAT_CHANNEL_INFO 0x02
14387 +#define WUSTAT_RECEIVED_DATA 0x03
14388 +#define WUSTAT_MAS_AVAILABILITY 0x04
14389 +#define WUSTAT_CURRENT_TRANSMIT_POWER 0x05
14390 +#define UR_CLEAR_FEATURE 0x01
14391 +#define UR_SET_FEATURE 0x03
14392 +#define UR_SET_AND_TEST_FEATURE 0x0c
14393 +#define UR_SET_ADDRESS 0x05
14394 +#define UR_GET_DESCRIPTOR 0x06
14395 +#define UDESC_DEVICE 0x01
14396 +#define UDESC_CONFIG 0x02
14397 +#define UDESC_STRING 0x03
14398 +#define UDESC_INTERFACE 0x04
14399 +#define UDESC_ENDPOINT 0x05
14400 +#define UDESC_SS_USB_COMPANION 0x30
14401 +#define UDESC_DEVICE_QUALIFIER 0x06
14402 +#define UDESC_OTHER_SPEED_CONFIGURATION 0x07
14403 +#define UDESC_INTERFACE_POWER 0x08
14404 +#define UDESC_OTG 0x09
14405 +#define WUDESC_SECURITY 0x0c
14406 +#define WUDESC_KEY 0x0d
14407 +#define WUD_GET_KEY_INDEX(_wValue_) ((_wValue_) & 0xf)
14408 +#define WUD_GET_KEY_TYPE(_wValue_) (((_wValue_) & 0x30) >> 4)
14409 +#define WUD_KEY_TYPE_ASSOC 0x01
14410 +#define WUD_KEY_TYPE_GTK 0x02
14411 +#define WUD_GET_KEY_ORIGIN(_wValue_) (((_wValue_) & 0x40) >> 6)
14412 +#define WUD_KEY_ORIGIN_HOST 0x00
14413 +#define WUD_KEY_ORIGIN_DEVICE 0x01
14414 +#define WUDESC_ENCRYPTION_TYPE 0x0e
14415 +#define WUDESC_BOS 0x0f
14416 +#define WUDESC_DEVICE_CAPABILITY 0x10
14417 +#define WUDESC_WIRELESS_ENDPOINT_COMPANION 0x11
14418 +#define UDESC_BOS 0x0f
14419 +#define UDESC_DEVICE_CAPABILITY 0x10
14420 +#define UDESC_CS_DEVICE 0x21 /* class specific */
14421 +#define UDESC_CS_CONFIG 0x22
14422 +#define UDESC_CS_STRING 0x23
14423 +#define UDESC_CS_INTERFACE 0x24
14424 +#define UDESC_CS_ENDPOINT 0x25
14425 +#define UDESC_HUB 0x29
14426 +#define UR_SET_DESCRIPTOR 0x07
14427 +#define UR_GET_CONFIG 0x08
14428 +#define UR_SET_CONFIG 0x09
14429 +#define UR_GET_INTERFACE 0x0a
14430 +#define UR_SET_INTERFACE 0x0b
14431 +#define UR_SYNCH_FRAME 0x0c
14432 +#define WUR_SET_ENCRYPTION 0x0d
14433 +#define WUR_GET_ENCRYPTION 0x0e
14434 +#define WUR_SET_HANDSHAKE 0x0f
14435 +#define WUR_GET_HANDSHAKE 0x10
14436 +#define WUR_SET_CONNECTION 0x11
14437 +#define WUR_SET_SECURITY_DATA 0x12
14438 +#define WUR_GET_SECURITY_DATA 0x13
14439 +#define WUR_SET_WUSB_DATA 0x14
14440 +#define WUDATA_DRPIE_INFO 0x01
14441 +#define WUDATA_TRANSMIT_DATA 0x02
14442 +#define WUDATA_TRANSMIT_PARAMS 0x03
14443 +#define WUDATA_RECEIVE_PARAMS 0x04
14444 +#define WUDATA_TRANSMIT_POWER 0x05
14445 +#define WUR_LOOPBACK_DATA_WRITE 0x15
14446 +#define WUR_LOOPBACK_DATA_READ 0x16
14447 +#define WUR_SET_INTERFACE_DS 0x17
14448 +
14449 +/* Feature numbers */
14450 +#define UF_ENDPOINT_HALT 0
14451 +#define UF_DEVICE_REMOTE_WAKEUP 1
14452 +#define UF_TEST_MODE 2
14453 +#define UF_DEVICE_B_HNP_ENABLE 3
14454 +#define UF_DEVICE_A_HNP_SUPPORT 4
14455 +#define UF_DEVICE_A_ALT_HNP_SUPPORT 5
14456 +#define WUF_WUSB 3
14457 +#define WUF_TX_DRPIE 0x0
14458 +#define WUF_DEV_XMIT_PACKET 0x1
14459 +#define WUF_COUNT_PACKETS 0x2
14460 +#define WUF_CAPTURE_PACKETS 0x3
14461 +#define UF_FUNCTION_SUSPEND 0
14462 +#define UF_U1_ENABLE 48
14463 +#define UF_U2_ENABLE 49
14464 +#define UF_LTM_ENABLE 50
14465 +
14466 +/* Class requests from the USB 2.0 hub spec, table 11-15 */
14467 +#define UCR_CLEAR_HUB_FEATURE (0x2000 | UR_CLEAR_FEATURE)
14468 +#define UCR_CLEAR_PORT_FEATURE (0x2300 | UR_CLEAR_FEATURE)
14469 +#define UCR_GET_HUB_DESCRIPTOR (0xa000 | UR_GET_DESCRIPTOR)
14470 +#define UCR_GET_HUB_STATUS (0xa000 | UR_GET_STATUS)
14471 +#define UCR_GET_PORT_STATUS (0xa300 | UR_GET_STATUS)
14472 +#define UCR_SET_HUB_FEATURE (0x2000 | UR_SET_FEATURE)
14473 +#define UCR_SET_PORT_FEATURE (0x2300 | UR_SET_FEATURE)
14474 +#define UCR_SET_AND_TEST_PORT_FEATURE (0xa300 | UR_SET_AND_TEST_FEATURE)
14475 +
14476 +#ifdef _MSC_VER
14477 +#include <pshpack1.h>
14478 +#endif
14479 +
14480 +typedef struct {
14481 + uByte bLength;
14482 + uByte bDescriptorType;
14483 + uByte bDescriptorSubtype;
14484 +} UPACKED usb_descriptor_t;
14485 +
14486 +typedef struct {
14487 + uByte bLength;
14488 + uByte bDescriptorType;
14489 +} UPACKED usb_descriptor_header_t;
14490 +
14491 +typedef struct {
14492 + uByte bLength;
14493 + uByte bDescriptorType;
14494 + uWord bcdUSB;
14495 +#define UD_USB_2_0 0x0200
14496 +#define UD_IS_USB2(d) (UGETW((d)->bcdUSB) >= UD_USB_2_0)
14497 + uByte bDeviceClass;
14498 + uByte bDeviceSubClass;
14499 + uByte bDeviceProtocol;
14500 + uByte bMaxPacketSize;
14501 + /* The fields below are not part of the initial descriptor. */
14502 + uWord idVendor;
14503 + uWord idProduct;
14504 + uWord bcdDevice;
14505 + uByte iManufacturer;
14506 + uByte iProduct;
14507 + uByte iSerialNumber;
14508 + uByte bNumConfigurations;
14509 +} UPACKED usb_device_descriptor_t;
14510 +#define USB_DEVICE_DESCRIPTOR_SIZE 18
14511 +
14512 +typedef struct {
14513 + uByte bLength;
14514 + uByte bDescriptorType;
14515 + uWord wTotalLength;
14516 + uByte bNumInterface;
14517 + uByte bConfigurationValue;
14518 + uByte iConfiguration;
14519 +#define UC_ATT_ONE (1 << 7) /* must be set */
14520 +#define UC_ATT_SELFPOWER (1 << 6) /* self powered */
14521 +#define UC_ATT_WAKEUP (1 << 5) /* can wakeup */
14522 +#define UC_ATT_BATTERY (1 << 4) /* battery powered */
14523 + uByte bmAttributes;
14524 +#define UC_BUS_POWERED 0x80
14525 +#define UC_SELF_POWERED 0x40
14526 +#define UC_REMOTE_WAKEUP 0x20
14527 + uByte bMaxPower; /* max current in 2 mA units */
14528 +#define UC_POWER_FACTOR 2
14529 +} UPACKED usb_config_descriptor_t;
14530 +#define USB_CONFIG_DESCRIPTOR_SIZE 9
14531 +
14532 +typedef struct {
14533 + uByte bLength;
14534 + uByte bDescriptorType;
14535 + uByte bInterfaceNumber;
14536 + uByte bAlternateSetting;
14537 + uByte bNumEndpoints;
14538 + uByte bInterfaceClass;
14539 + uByte bInterfaceSubClass;
14540 + uByte bInterfaceProtocol;
14541 + uByte iInterface;
14542 +} UPACKED usb_interface_descriptor_t;
14543 +#define USB_INTERFACE_DESCRIPTOR_SIZE 9
14544 +
14545 +typedef struct {
14546 + uByte bLength;
14547 + uByte bDescriptorType;
14548 + uByte bEndpointAddress;
14549 +#define UE_GET_DIR(a) ((a) & 0x80)
14550 +#define UE_SET_DIR(a,d) ((a) | (((d)&1) << 7))
14551 +#define UE_DIR_IN 0x80
14552 +#define UE_DIR_OUT 0x00
14553 +#define UE_ADDR 0x0f
14554 +#define UE_GET_ADDR(a) ((a) & UE_ADDR)
14555 + uByte bmAttributes;
14556 +#define UE_XFERTYPE 0x03
14557 +#define UE_CONTROL 0x00
14558 +#define UE_ISOCHRONOUS 0x01
14559 +#define UE_BULK 0x02
14560 +#define UE_INTERRUPT 0x03
14561 +#define UE_GET_XFERTYPE(a) ((a) & UE_XFERTYPE)
14562 +#define UE_ISO_TYPE 0x0c
14563 +#define UE_ISO_ASYNC 0x04
14564 +#define UE_ISO_ADAPT 0x08
14565 +#define UE_ISO_SYNC 0x0c
14566 +#define UE_GET_ISO_TYPE(a) ((a) & UE_ISO_TYPE)
14567 + uWord wMaxPacketSize;
14568 + uByte bInterval;
14569 +} UPACKED usb_endpoint_descriptor_t;
14570 +#define USB_ENDPOINT_DESCRIPTOR_SIZE 7
14571 +
14572 +typedef struct ss_endpoint_companion_descriptor {
14573 + uByte bLength;
14574 + uByte bDescriptorType;
14575 + uByte bMaxBurst;
14576 +#define USSE_GET_MAX_STREAMS(a) ((a) & 0x1f)
14577 +#define USSE_SET_MAX_STREAMS(a, b) ((a) | ((b) & 0x1f))
14578 +#define USSE_GET_MAX_PACKET_NUM(a) ((a) & 0x03)
14579 +#define USSE_SET_MAX_PACKET_NUM(a, b) ((a) | ((b) & 0x03))
14580 + uByte bmAttributes;
14581 + uWord wBytesPerInterval;
14582 +} UPACKED ss_endpoint_companion_descriptor_t;
14583 +#define USB_SS_ENDPOINT_COMPANION_DESCRIPTOR_SIZE 6
14584 +
14585 +typedef struct {
14586 + uByte bLength;
14587 + uByte bDescriptorType;
14588 + uWord bString[127];
14589 +} UPACKED usb_string_descriptor_t;
14590 +#define USB_MAX_STRING_LEN 128
14591 +#define USB_LANGUAGE_TABLE 0 /* # of the string language id table */
14592 +
14593 +/* Hub specific request */
14594 +#define UR_GET_BUS_STATE 0x02
14595 +#define UR_CLEAR_TT_BUFFER 0x08
14596 +#define UR_RESET_TT 0x09
14597 +#define UR_GET_TT_STATE 0x0a
14598 +#define UR_STOP_TT 0x0b
14599 +
14600 +/* Hub features */
14601 +#define UHF_C_HUB_LOCAL_POWER 0
14602 +#define UHF_C_HUB_OVER_CURRENT 1
14603 +#define UHF_PORT_CONNECTION 0
14604 +#define UHF_PORT_ENABLE 1
14605 +#define UHF_PORT_SUSPEND 2
14606 +#define UHF_PORT_OVER_CURRENT 3
14607 +#define UHF_PORT_RESET 4
14608 +#define UHF_PORT_L1 5
14609 +#define UHF_PORT_POWER 8
14610 +#define UHF_PORT_LOW_SPEED 9
14611 +#define UHF_PORT_HIGH_SPEED 10
14612 +#define UHF_C_PORT_CONNECTION 16
14613 +#define UHF_C_PORT_ENABLE 17
14614 +#define UHF_C_PORT_SUSPEND 18
14615 +#define UHF_C_PORT_OVER_CURRENT 19
14616 +#define UHF_C_PORT_RESET 20
14617 +#define UHF_C_PORT_L1 23
14618 +#define UHF_PORT_TEST 21
14619 +#define UHF_PORT_INDICATOR 22
14620 +
14621 +typedef struct {
14622 + uByte bDescLength;
14623 + uByte bDescriptorType;
14624 + uByte bNbrPorts;
14625 + uWord wHubCharacteristics;
14626 +#define UHD_PWR 0x0003
14627 +#define UHD_PWR_GANGED 0x0000
14628 +#define UHD_PWR_INDIVIDUAL 0x0001
14629 +#define UHD_PWR_NO_SWITCH 0x0002
14630 +#define UHD_COMPOUND 0x0004
14631 +#define UHD_OC 0x0018
14632 +#define UHD_OC_GLOBAL 0x0000
14633 +#define UHD_OC_INDIVIDUAL 0x0008
14634 +#define UHD_OC_NONE 0x0010
14635 +#define UHD_TT_THINK 0x0060
14636 +#define UHD_TT_THINK_8 0x0000
14637 +#define UHD_TT_THINK_16 0x0020
14638 +#define UHD_TT_THINK_24 0x0040
14639 +#define UHD_TT_THINK_32 0x0060
14640 +#define UHD_PORT_IND 0x0080
14641 + uByte bPwrOn2PwrGood; /* delay in 2 ms units */
14642 +#define UHD_PWRON_FACTOR 2
14643 + uByte bHubContrCurrent;
14644 + uByte DeviceRemovable[32]; /* max 255 ports */
14645 +#define UHD_NOT_REMOV(desc, i) \
14646 + (((desc)->DeviceRemovable[(i)/8] >> ((i) % 8)) & 1)
14647 + /* deprecated */ uByte PortPowerCtrlMask[1];
14648 +} UPACKED usb_hub_descriptor_t;
14649 +#define USB_HUB_DESCRIPTOR_SIZE 9 /* includes deprecated PortPowerCtrlMask */
14650 +
14651 +typedef struct {
14652 + uByte bLength;
14653 + uByte bDescriptorType;
14654 + uWord bcdUSB;
14655 + uByte bDeviceClass;
14656 + uByte bDeviceSubClass;
14657 + uByte bDeviceProtocol;
14658 + uByte bMaxPacketSize0;
14659 + uByte bNumConfigurations;
14660 + uByte bReserved;
14661 +} UPACKED usb_device_qualifier_t;
14662 +#define USB_DEVICE_QUALIFIER_SIZE 10
14663 +
14664 +typedef struct {
14665 + uByte bLength;
14666 + uByte bDescriptorType;
14667 + uByte bmAttributes;
14668 +#define UOTG_SRP 0x01
14669 +#define UOTG_HNP 0x02
14670 +} UPACKED usb_otg_descriptor_t;
14671 +
14672 +/* OTG feature selectors */
14673 +#define UOTG_B_HNP_ENABLE 3
14674 +#define UOTG_A_HNP_SUPPORT 4
14675 +#define UOTG_A_ALT_HNP_SUPPORT 5
14676 +
14677 +typedef struct {
14678 + uWord wStatus;
14679 +/* Device status flags */
14680 +#define UDS_SELF_POWERED 0x0001
14681 +#define UDS_REMOTE_WAKEUP 0x0002
14682 +/* Endpoint status flags */
14683 +#define UES_HALT 0x0001
14684 +} UPACKED usb_status_t;
14685 +
14686 +typedef struct {
14687 + uWord wHubStatus;
14688 +#define UHS_LOCAL_POWER 0x0001
14689 +#define UHS_OVER_CURRENT 0x0002
14690 + uWord wHubChange;
14691 +} UPACKED usb_hub_status_t;
14692 +
14693 +typedef struct {
14694 + uWord wPortStatus;
14695 +#define UPS_CURRENT_CONNECT_STATUS 0x0001
14696 +#define UPS_PORT_ENABLED 0x0002
14697 +#define UPS_SUSPEND 0x0004
14698 +#define UPS_OVERCURRENT_INDICATOR 0x0008
14699 +#define UPS_RESET 0x0010
14700 +#define UPS_PORT_POWER 0x0100
14701 +#define UPS_LOW_SPEED 0x0200
14702 +#define UPS_HIGH_SPEED 0x0400
14703 +#define UPS_PORT_TEST 0x0800
14704 +#define UPS_PORT_INDICATOR 0x1000
14705 + uWord wPortChange;
14706 +#define UPS_C_CONNECT_STATUS 0x0001
14707 +#define UPS_C_PORT_ENABLED 0x0002
14708 +#define UPS_C_SUSPEND 0x0004
14709 +#define UPS_C_OVERCURRENT_INDICATOR 0x0008
14710 +#define UPS_C_PORT_RESET 0x0010
14711 +} UPACKED usb_port_status_t;
14712 +
14713 +#ifdef _MSC_VER
14714 +#include <poppack.h>
14715 +#endif
14716 +
14717 +/* Device class codes */
14718 +#define UDCLASS_IN_INTERFACE 0x00
14719 +#define UDCLASS_COMM 0x02
14720 +#define UDCLASS_HUB 0x09
14721 +#define UDSUBCLASS_HUB 0x00
14722 +#define UDPROTO_FSHUB 0x00
14723 +#define UDPROTO_HSHUBSTT 0x01
14724 +#define UDPROTO_HSHUBMTT 0x02
14725 +#define UDCLASS_DIAGNOSTIC 0xdc
14726 +#define UDCLASS_WIRELESS 0xe0
14727 +#define UDSUBCLASS_RF 0x01
14728 +#define UDPROTO_BLUETOOTH 0x01
14729 +#define UDCLASS_VENDOR 0xff
14730 +
14731 +/* Interface class codes */
14732 +#define UICLASS_UNSPEC 0x00
14733 +
14734 +#define UICLASS_AUDIO 0x01
14735 +#define UISUBCLASS_AUDIOCONTROL 1
14736 +#define UISUBCLASS_AUDIOSTREAM 2
14737 +#define UISUBCLASS_MIDISTREAM 3
14738 +
14739 +#define UICLASS_CDC 0x02 /* communication */
14740 +#define UISUBCLASS_DIRECT_LINE_CONTROL_MODEL 1
14741 +#define UISUBCLASS_ABSTRACT_CONTROL_MODEL 2
14742 +#define UISUBCLASS_TELEPHONE_CONTROL_MODEL 3
14743 +#define UISUBCLASS_MULTICHANNEL_CONTROL_MODEL 4
14744 +#define UISUBCLASS_CAPI_CONTROLMODEL 5
14745 +#define UISUBCLASS_ETHERNET_NETWORKING_CONTROL_MODEL 6
14746 +#define UISUBCLASS_ATM_NETWORKING_CONTROL_MODEL 7
14747 +#define UIPROTO_CDC_AT 1
14748 +
14749 +#define UICLASS_HID 0x03
14750 +#define UISUBCLASS_BOOT 1
14751 +#define UIPROTO_BOOT_KEYBOARD 1
14752 +
14753 +#define UICLASS_PHYSICAL 0x05
14754 +
14755 +#define UICLASS_IMAGE 0x06
14756 +
14757 +#define UICLASS_PRINTER 0x07
14758 +#define UISUBCLASS_PRINTER 1
14759 +#define UIPROTO_PRINTER_UNI 1
14760 +#define UIPROTO_PRINTER_BI 2
14761 +#define UIPROTO_PRINTER_1284 3
14762 +
14763 +#define UICLASS_MASS 0x08
14764 +#define UISUBCLASS_RBC 1
14765 +#define UISUBCLASS_SFF8020I 2
14766 +#define UISUBCLASS_QIC157 3
14767 +#define UISUBCLASS_UFI 4
14768 +#define UISUBCLASS_SFF8070I 5
14769 +#define UISUBCLASS_SCSI 6
14770 +#define UIPROTO_MASS_CBI_I 0
14771 +#define UIPROTO_MASS_CBI 1
14772 +#define UIPROTO_MASS_BBB_OLD 2 /* Not in the spec anymore */
14773 +#define UIPROTO_MASS_BBB 80 /* 'P' for the Iomega Zip drive */
14774 +
14775 +#define UICLASS_HUB 0x09
14776 +#define UISUBCLASS_HUB 0
14777 +#define UIPROTO_FSHUB 0
14778 +#define UIPROTO_HSHUBSTT 0 /* Yes, same as previous */
14779 +#define UIPROTO_HSHUBMTT 1
14780 +
14781 +#define UICLASS_CDC_DATA 0x0a
14782 +#define UISUBCLASS_DATA 0
14783 +#define UIPROTO_DATA_ISDNBRI 0x30 /* Physical iface */
14784 +#define UIPROTO_DATA_HDLC 0x31 /* HDLC */
14785 +#define UIPROTO_DATA_TRANSPARENT 0x32 /* Transparent */
14786 +#define UIPROTO_DATA_Q921M 0x50 /* Management for Q921 */
14787 +#define UIPROTO_DATA_Q921 0x51 /* Data for Q921 */
14788 +#define UIPROTO_DATA_Q921TM 0x52 /* TEI multiplexer for Q921 */
14789 +#define UIPROTO_DATA_V42BIS 0x90 /* Data compression */
14790 +#define UIPROTO_DATA_Q931 0x91 /* Euro-ISDN */
14791 +#define UIPROTO_DATA_V120 0x92 /* V.24 rate adaption */
14792 +#define UIPROTO_DATA_CAPI 0x93 /* CAPI 2.0 commands */
14793 +#define UIPROTO_DATA_HOST_BASED 0xfd /* Host based driver */
14794 +#define UIPROTO_DATA_PUF 0xfe /* see Prot. Unit Func. Desc.*/
14795 +#define UIPROTO_DATA_VENDOR 0xff /* Vendor specific */
14796 +
14797 +#define UICLASS_SMARTCARD 0x0b
14798 +
14799 +/*#define UICLASS_FIRM_UPD 0x0c*/
14800 +
14801 +#define UICLASS_SECURITY 0x0d
14802 +
14803 +#define UICLASS_DIAGNOSTIC 0xdc
14804 +
14805 +#define UICLASS_WIRELESS 0xe0
14806 +#define UISUBCLASS_RF 0x01
14807 +#define UIPROTO_BLUETOOTH 0x01
14808 +
14809 +#define UICLASS_APPL_SPEC 0xfe
14810 +#define UISUBCLASS_FIRMWARE_DOWNLOAD 1
14811 +#define UISUBCLASS_IRDA 2
14812 +#define UIPROTO_IRDA 0
14813 +
14814 +#define UICLASS_VENDOR 0xff
14815 +
14816 +#define USB_HUB_MAX_DEPTH 5
14817 +
14818 +/*
14819 + * Minimum time a device needs to be powered down to go through
14820 + * a power cycle. XXX Are these time in the spec?
14821 + */
14822 +#define USB_POWER_DOWN_TIME 200 /* ms */
14823 +#define USB_PORT_POWER_DOWN_TIME 100 /* ms */
14824 +
14825 +#if 0
14826 +/* These are the values from the spec. */
14827 +#define USB_PORT_RESET_DELAY 10 /* ms */
14828 +#define USB_PORT_ROOT_RESET_DELAY 50 /* ms */
14829 +#define USB_PORT_RESET_RECOVERY 10 /* ms */
14830 +#define USB_PORT_POWERUP_DELAY 100 /* ms */
14831 +#define USB_SET_ADDRESS_SETTLE 2 /* ms */
14832 +#define USB_RESUME_DELAY (20*5) /* ms */
14833 +#define USB_RESUME_WAIT 10 /* ms */
14834 +#define USB_RESUME_RECOVERY 10 /* ms */
14835 +#define USB_EXTRA_POWER_UP_TIME 0 /* ms */
14836 +#else
14837 +/* Allow for marginal (i.e. non-conforming) devices. */
14838 +#define USB_PORT_RESET_DELAY 50 /* ms */
14839 +#define USB_PORT_ROOT_RESET_DELAY 250 /* ms */
14840 +#define USB_PORT_RESET_RECOVERY 250 /* ms */
14841 +#define USB_PORT_POWERUP_DELAY 300 /* ms */
14842 +#define USB_SET_ADDRESS_SETTLE 10 /* ms */
14843 +#define USB_RESUME_DELAY (50*5) /* ms */
14844 +#define USB_RESUME_WAIT 50 /* ms */
14845 +#define USB_RESUME_RECOVERY 50 /* ms */
14846 +#define USB_EXTRA_POWER_UP_TIME 20 /* ms */
14847 +#endif
14848 +
14849 +#define USB_MIN_POWER 100 /* mA */
14850 +#define USB_MAX_POWER 500 /* mA */
14851 +
14852 +#define USB_BUS_RESET_DELAY 100 /* ms XXX?*/
14853 +
14854 +#define USB_UNCONFIG_NO 0
14855 +#define USB_UNCONFIG_INDEX (-1)
14856 +
14857 +/*** ioctl() related stuff ***/
14858 +
14859 +struct usb_ctl_request {
14860 + int ucr_addr;
14861 + usb_device_request_t ucr_request;
14862 + void *ucr_data;
14863 + int ucr_flags;
14864 +#define USBD_SHORT_XFER_OK 0x04 /* allow short reads */
14865 + int ucr_actlen; /* actual length transferred */
14866 +};
14867 +
14868 +struct usb_alt_interface {
14869 + int uai_config_index;
14870 + int uai_interface_index;
14871 + int uai_alt_no;
14872 +};
14873 +
14874 +#define USB_CURRENT_CONFIG_INDEX (-1)
14875 +#define USB_CURRENT_ALT_INDEX (-1)
14876 +
14877 +struct usb_config_desc {
14878 + int ucd_config_index;
14879 + usb_config_descriptor_t ucd_desc;
14880 +};
14881 +
14882 +struct usb_interface_desc {
14883 + int uid_config_index;
14884 + int uid_interface_index;
14885 + int uid_alt_index;
14886 + usb_interface_descriptor_t uid_desc;
14887 +};
14888 +
14889 +struct usb_endpoint_desc {
14890 + int ued_config_index;
14891 + int ued_interface_index;
14892 + int ued_alt_index;
14893 + int ued_endpoint_index;
14894 + usb_endpoint_descriptor_t ued_desc;
14895 +};
14896 +
14897 +struct usb_full_desc {
14898 + int ufd_config_index;
14899 + u_int ufd_size;
14900 + u_char *ufd_data;
14901 +};
14902 +
14903 +struct usb_string_desc {
14904 + int usd_string_index;
14905 + int usd_language_id;
14906 + usb_string_descriptor_t usd_desc;
14907 +};
14908 +
14909 +struct usb_ctl_report_desc {
14910 + int ucrd_size;
14911 + u_char ucrd_data[1024]; /* filled data size will vary */
14912 +};
14913 +
14914 +typedef struct { u_int32_t cookie; } usb_event_cookie_t;
14915 +
14916 +#define USB_MAX_DEVNAMES 4
14917 +#define USB_MAX_DEVNAMELEN 16
14918 +struct usb_device_info {
14919 + u_int8_t udi_bus;
14920 + u_int8_t udi_addr; /* device address */
14921 + usb_event_cookie_t udi_cookie;
14922 + char udi_product[USB_MAX_STRING_LEN];
14923 + char udi_vendor[USB_MAX_STRING_LEN];
14924 + char udi_release[8];
14925 + u_int16_t udi_productNo;
14926 + u_int16_t udi_vendorNo;
14927 + u_int16_t udi_releaseNo;
14928 + u_int8_t udi_class;
14929 + u_int8_t udi_subclass;
14930 + u_int8_t udi_protocol;
14931 + u_int8_t udi_config;
14932 + u_int8_t udi_speed;
14933 +#define USB_SPEED_UNKNOWN 0
14934 +#define USB_SPEED_LOW 1
14935 +#define USB_SPEED_FULL 2
14936 +#define USB_SPEED_HIGH 3
14937 +#define USB_SPEED_VARIABLE 4
14938 +#define USB_SPEED_SUPER 5
14939 + int udi_power; /* power consumption in mA, 0 if selfpowered */
14940 + int udi_nports;
14941 + char udi_devnames[USB_MAX_DEVNAMES][USB_MAX_DEVNAMELEN];
14942 + u_int8_t udi_ports[16];/* hub only: addresses of devices on ports */
14943 +#define USB_PORT_ENABLED 0xff
14944 +#define USB_PORT_SUSPENDED 0xfe
14945 +#define USB_PORT_POWERED 0xfd
14946 +#define USB_PORT_DISABLED 0xfc
14947 +};
14948 +
14949 +struct usb_ctl_report {
14950 + int ucr_report;
14951 + u_char ucr_data[1024]; /* filled data size will vary */
14952 +};
14953 +
14954 +struct usb_device_stats {
14955 + u_long uds_requests[4]; /* indexed by transfer type UE_* */
14956 +};
14957 +
14958 +#define WUSB_MIN_IE 0x80
14959 +#define WUSB_WCTA_IE 0x80
14960 +#define WUSB_WCONNECTACK_IE 0x81
14961 +#define WUSB_WHOSTINFO_IE 0x82
14962 +#define WUHI_GET_CA(_bmAttributes_) ((_bmAttributes_) & 0x3)
14963 +#define WUHI_CA_RECONN 0x00
14964 +#define WUHI_CA_LIMITED 0x01
14965 +#define WUHI_CA_ALL 0x03
14966 +#define WUHI_GET_MLSI(_bmAttributes_) (((_bmAttributes_) & 0x38) >> 3)
14967 +#define WUSB_WCHCHANGEANNOUNCE_IE 0x83
14968 +#define WUSB_WDEV_DISCONNECT_IE 0x84
14969 +#define WUSB_WHOST_DISCONNECT_IE 0x85
14970 +#define WUSB_WRELEASE_CHANNEL_IE 0x86
14971 +#define WUSB_WWORK_IE 0x87
14972 +#define WUSB_WCHANNEL_STOP_IE 0x88
14973 +#define WUSB_WDEV_KEEPALIVE_IE 0x89
14974 +#define WUSB_WISOCH_DISCARD_IE 0x8A
14975 +#define WUSB_WRESETDEVICE_IE 0x8B
14976 +#define WUSB_WXMIT_PACKET_ADJUST_IE 0x8C
14977 +#define WUSB_MAX_IE 0x8C
14978 +
14979 +/* Device Notification Types */
14980 +
14981 +#define WUSB_DN_MIN 0x01
14982 +#define WUSB_DN_CONNECT 0x01
14983 +# define WUSB_DA_OLDCONN 0x00
14984 +# define WUSB_DA_NEWCONN 0x01
14985 +# define WUSB_DA_SELF_BEACON 0x02
14986 +# define WUSB_DA_DIR_BEACON 0x04
14987 +# define WUSB_DA_NO_BEACON 0x06
14988 +#define WUSB_DN_DISCONNECT 0x02
14989 +#define WUSB_DN_EPRDY 0x03
14990 +#define WUSB_DN_MASAVAILCHANGED 0x04
14991 +#define WUSB_DN_REMOTEWAKEUP 0x05
14992 +#define WUSB_DN_SLEEP 0x06
14993 +#define WUSB_DN_ALIVE 0x07
14994 +#define WUSB_DN_MAX 0x07
14995 +
14996 +#ifdef _MSC_VER
14997 +#include <pshpack1.h>
14998 +#endif
14999 +
15000 +/* WUSB Handshake Data. Used during the SET/GET HANDSHAKE requests */
15001 +typedef struct wusb_hndshk_data {
15002 + uByte bMessageNumber;
15003 + uByte bStatus;
15004 + uByte tTKID[3];
15005 + uByte bReserved;
15006 + uByte CDID[16];
15007 + uByte Nonce[16];
15008 + uByte MIC[8];
15009 +} UPACKED wusb_hndshk_data_t;
15010 +#define WUSB_HANDSHAKE_LEN_FOR_MIC 38
15011 +
15012 +/* WUSB Connection Context */
15013 +typedef struct wusb_conn_context {
15014 + uByte CHID [16];
15015 + uByte CDID [16];
15016 + uByte CK [16];
15017 +} UPACKED wusb_conn_context_t;
15018 +
15019 +/* WUSB Security Descriptor */
15020 +typedef struct wusb_security_desc {
15021 + uByte bLength;
15022 + uByte bDescriptorType;
15023 + uWord wTotalLength;
15024 + uByte bNumEncryptionTypes;
15025 +} UPACKED wusb_security_desc_t;
15026 +
15027 +/* WUSB Encryption Type Descriptor */
15028 +typedef struct wusb_encrypt_type_desc {
15029 + uByte bLength;
15030 + uByte bDescriptorType;
15031 +
15032 + uByte bEncryptionType;
15033 +#define WUETD_UNSECURE 0
15034 +#define WUETD_WIRED 1
15035 +#define WUETD_CCM_1 2
15036 +#define WUETD_RSA_1 3
15037 +
15038 + uByte bEncryptionValue;
15039 + uByte bAuthKeyIndex;
15040 +} UPACKED wusb_encrypt_type_desc_t;
15041 +
15042 +/* WUSB Key Descriptor */
15043 +typedef struct wusb_key_desc {
15044 + uByte bLength;
15045 + uByte bDescriptorType;
15046 + uByte tTKID[3];
15047 + uByte bReserved;
15048 + uByte KeyData[1]; /* variable length */
15049 +} UPACKED wusb_key_desc_t;
15050 +
15051 +/* WUSB BOS Descriptor (Binary device Object Store) */
15052 +typedef struct wusb_bos_desc {
15053 + uByte bLength;
15054 + uByte bDescriptorType;
15055 + uWord wTotalLength;
15056 + uByte bNumDeviceCaps;
15057 +} UPACKED wusb_bos_desc_t;
15058 +
15059 +#define USB_DEVICE_CAPABILITY_20_EXTENSION 0x02
15060 +typedef struct usb_dev_cap_20_ext_desc {
15061 + uByte bLength;
15062 + uByte bDescriptorType;
15063 + uByte bDevCapabilityType;
15064 +#define USB_20_EXT_LPM 0x02
15065 + uDWord bmAttributes;
15066 +} UPACKED usb_dev_cap_20_ext_desc_t;
15067 +
15068 +#define USB_DEVICE_CAPABILITY_SS_USB 0x03
15069 +typedef struct usb_dev_cap_ss_usb {
15070 + uByte bLength;
15071 + uByte bDescriptorType;
15072 + uByte bDevCapabilityType;
15073 +#define USB_DC_SS_USB_LTM_CAPABLE 0x02
15074 + uByte bmAttributes;
15075 +#define USB_DC_SS_USB_SPEED_SUPPORT_LOW 0x01
15076 +#define USB_DC_SS_USB_SPEED_SUPPORT_FULL 0x02
15077 +#define USB_DC_SS_USB_SPEED_SUPPORT_HIGH 0x04
15078 +#define USB_DC_SS_USB_SPEED_SUPPORT_SS 0x08
15079 + uWord wSpeedsSupported;
15080 + uByte bFunctionalitySupport;
15081 + uByte bU1DevExitLat;
15082 + uWord wU2DevExitLat;
15083 +} UPACKED usb_dev_cap_ss_usb_t;
15084 +
15085 +#define USB_DEVICE_CAPABILITY_CONTAINER_ID 0x04
15086 +typedef struct usb_dev_cap_container_id {
15087 + uByte bLength;
15088 + uByte bDescriptorType;
15089 + uByte bDevCapabilityType;
15090 + uByte bReserved;
15091 + uByte containerID[16];
15092 +} UPACKED usb_dev_cap_container_id_t;
15093 +
15094 +/* Device Capability Type Codes */
15095 +#define WUSB_DEVICE_CAPABILITY_WIRELESS_USB 0x01
15096 +
15097 +/* Device Capability Descriptor */
15098 +typedef struct wusb_dev_cap_desc {
15099 + uByte bLength;
15100 + uByte bDescriptorType;
15101 + uByte bDevCapabilityType;
15102 + uByte caps[1]; /* Variable length */
15103 +} UPACKED wusb_dev_cap_desc_t;
15104 +
15105 +/* Device Capability Descriptor */
15106 +typedef struct wusb_dev_cap_uwb_desc {
15107 + uByte bLength;
15108 + uByte bDescriptorType;
15109 + uByte bDevCapabilityType;
15110 + uByte bmAttributes;
15111 + uWord wPHYRates; /* Bitmap */
15112 + uByte bmTFITXPowerInfo;
15113 + uByte bmFFITXPowerInfo;
15114 + uWord bmBandGroup;
15115 + uByte bReserved;
15116 +} UPACKED wusb_dev_cap_uwb_desc_t;
15117 +
15118 +/* Wireless USB Endpoint Companion Descriptor */
15119 +typedef struct wusb_endpoint_companion_desc {
15120 + uByte bLength;
15121 + uByte bDescriptorType;
15122 + uByte bMaxBurst;
15123 + uByte bMaxSequence;
15124 + uWord wMaxStreamDelay;
15125 + uWord wOverTheAirPacketSize;
15126 + uByte bOverTheAirInterval;
15127 + uByte bmCompAttributes;
15128 +} UPACKED wusb_endpoint_companion_desc_t;
15129 +
15130 +/* Wireless USB Numeric Association M1 Data Structure */
15131 +typedef struct wusb_m1_data {
15132 + uByte version;
15133 + uWord langId;
15134 + uByte deviceFriendlyNameLength;
15135 + uByte sha_256_m3[32];
15136 + uByte deviceFriendlyName[256];
15137 +} UPACKED wusb_m1_data_t;
15138 +
15139 +typedef struct wusb_m2_data {
15140 + uByte version;
15141 + uWord langId;
15142 + uByte hostFriendlyNameLength;
15143 + uByte pkh[384];
15144 + uByte hostFriendlyName[256];
15145 +} UPACKED wusb_m2_data_t;
15146 +
15147 +typedef struct wusb_m3_data {
15148 + uByte pkd[384];
15149 + uByte nd;
15150 +} UPACKED wusb_m3_data_t;
15151 +
15152 +typedef struct wusb_m4_data {
15153 + uDWord _attributeTypeIdAndLength_1;
15154 + uWord associationTypeId;
15155 +
15156 + uDWord _attributeTypeIdAndLength_2;
15157 + uWord associationSubTypeId;
15158 +
15159 + uDWord _attributeTypeIdAndLength_3;
15160 + uDWord length;
15161 +
15162 + uDWord _attributeTypeIdAndLength_4;
15163 + uDWord associationStatus;
15164 +
15165 + uDWord _attributeTypeIdAndLength_5;
15166 + uByte chid[16];
15167 +
15168 + uDWord _attributeTypeIdAndLength_6;
15169 + uByte cdid[16];
15170 +
15171 + uDWord _attributeTypeIdAndLength_7;
15172 + uByte bandGroups[2];
15173 +} UPACKED wusb_m4_data_t;
15174 +
15175 +#ifdef _MSC_VER
15176 +#include <poppack.h>
15177 +#endif
15178 +
15179 +#ifdef __cplusplus
15180 +}
15181 +#endif
15182 +
15183 +#endif /* _USB_H_ */
15184 --- /dev/null
15185 +++ b/drivers/usb/host/dwc_otg/Makefile
15186 @@ -0,0 +1,82 @@
15187 +#
15188 +# Makefile for DWC_otg Highspeed USB controller driver
15189 +#
15190 +
15191 +ifneq ($(KERNELRELEASE),)
15192 +
15193 +# Use the BUS_INTERFACE variable to compile the software for either
15194 +# PCI(PCI_INTERFACE) or LM(LM_INTERFACE) bus.
15195 +ifeq ($(BUS_INTERFACE),)
15196 +# BUS_INTERFACE = -DPCI_INTERFACE
15197 +# BUS_INTERFACE = -DLM_INTERFACE
15198 + BUS_INTERFACE = -DPLATFORM_INTERFACE
15199 +endif
15200 +
15201 +#ccflags-y += -DDEBUG
15202 +#ccflags-y += -DDWC_OTG_DEBUGLEV=1 # reduce common debug msgs
15203 +
15204 +# Use one of the following flags to compile the software in host-only or
15205 +# device-only mode.
15206 +#ccflags-y += -DDWC_HOST_ONLY
15207 +#ccflags-y += -DDWC_DEVICE_ONLY
15208 +
15209 +ccflags-y += -Dlinux -DDWC_HS_ELECT_TST
15210 +#ccflags-y += -DDWC_EN_ISOC
15211 +ccflags-y += -I$(obj)/../dwc_common_port
15212 +#ccflags-y += -I$(PORTLIB)
15213 +ccflags-y += -DDWC_LINUX
15214 +ccflags-y += $(CFI)
15215 +ccflags-y += $(BUS_INTERFACE)
15216 +#ccflags-y += -DDWC_DEV_SRPCAP
15217 +
15218 +obj-$(CONFIG_USB_DWCOTG) += dwc_otg.o
15219 +
15220 +dwc_otg-objs := dwc_otg_driver.o dwc_otg_attr.o
15221 +dwc_otg-objs += dwc_otg_cil.o dwc_otg_cil_intr.o
15222 +dwc_otg-objs += dwc_otg_pcd_linux.o dwc_otg_pcd.o dwc_otg_pcd_intr.o
15223 +dwc_otg-objs += dwc_otg_hcd.o dwc_otg_hcd_linux.o dwc_otg_hcd_intr.o dwc_otg_hcd_queue.o dwc_otg_hcd_ddma.o
15224 +dwc_otg-objs += dwc_otg_adp.o
15225 +dwc_otg-objs += dwc_otg_fiq_fsm.o
15226 +dwc_otg-objs += dwc_otg_fiq_stub.o
15227 +ifneq ($(CFI),)
15228 +dwc_otg-objs += dwc_otg_cfi.o
15229 +endif
15230 +
15231 +kernrelwd := $(subst ., ,$(KERNELRELEASE))
15232 +kernrel3 := $(word 1,$(kernrelwd)).$(word 2,$(kernrelwd)).$(word 3,$(kernrelwd))
15233 +
15234 +ifneq ($(kernrel3),2.6.20)
15235 +ccflags-y += $(CPPFLAGS)
15236 +endif
15237 +
15238 +else
15239 +
15240 +PWD := $(shell pwd)
15241 +PORTLIB := $(PWD)/../dwc_common_port
15242 +
15243 +# Command paths
15244 +CTAGS := $(CTAGS)
15245 +DOXYGEN := $(DOXYGEN)
15246 +
15247 +default: portlib
15248 + $(MAKE) -C$(KDIR) M=$(PWD) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) modules
15249 +
15250 +install: default
15251 + $(MAKE) -C$(KDIR) M=$(PORTLIB) modules_install
15252 + $(MAKE) -C$(KDIR) M=$(PWD) modules_install
15253 +
15254 +portlib:
15255 + $(MAKE) -C$(KDIR) M=$(PORTLIB) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) modules
15256 + cp $(PORTLIB)/Module.symvers $(PWD)/
15257 +
15258 +docs: $(wildcard *.[hc]) doc/doxygen.cfg
15259 + $(DOXYGEN) doc/doxygen.cfg
15260 +
15261 +tags: $(wildcard *.[hc])
15262 + $(CTAGS) -e $(wildcard *.[hc]) $(wildcard linux/*.[hc]) $(wildcard $(KDIR)/include/linux/usb*.h)
15263 +
15264 +
15265 +clean:
15266 + rm -rf *.o *.ko .*cmd *.mod.c .tmp_versions Module.symvers
15267 +
15268 +endif
15269 --- /dev/null
15270 +++ b/drivers/usb/host/dwc_otg/doc/doxygen.cfg
15271 @@ -0,0 +1,224 @@
15272 +# Doxyfile 1.3.9.1
15273 +
15274 +#---------------------------------------------------------------------------
15275 +# Project related configuration options
15276 +#---------------------------------------------------------------------------
15277 +PROJECT_NAME = "DesignWare USB 2.0 OTG Controller (DWC_otg) Device Driver"
15278 +PROJECT_NUMBER = v3.00a
15279 +OUTPUT_DIRECTORY = ./doc/
15280 +CREATE_SUBDIRS = NO
15281 +OUTPUT_LANGUAGE = English
15282 +BRIEF_MEMBER_DESC = YES
15283 +REPEAT_BRIEF = YES
15284 +ABBREVIATE_BRIEF = "The $name class" \
15285 + "The $name widget" \
15286 + "The $name file" \
15287 + is \
15288 + provides \
15289 + specifies \
15290 + contains \
15291 + represents \
15292 + a \
15293 + an \
15294 + the
15295 +ALWAYS_DETAILED_SEC = NO
15296 +INLINE_INHERITED_MEMB = NO
15297 +FULL_PATH_NAMES = NO
15298 +STRIP_FROM_PATH =
15299 +STRIP_FROM_INC_PATH =
15300 +SHORT_NAMES = NO
15301 +JAVADOC_AUTOBRIEF = YES
15302 +MULTILINE_CPP_IS_BRIEF = NO
15303 +INHERIT_DOCS = YES
15304 +DISTRIBUTE_GROUP_DOC = NO
15305 +TAB_SIZE = 8
15306 +ALIASES =
15307 +OPTIMIZE_OUTPUT_FOR_C = YES
15308 +OPTIMIZE_OUTPUT_JAVA = NO
15309 +SUBGROUPING = YES
15310 +#---------------------------------------------------------------------------
15311 +# Build related configuration options
15312 +#---------------------------------------------------------------------------
15313 +EXTRACT_ALL = NO
15314 +EXTRACT_PRIVATE = YES
15315 +EXTRACT_STATIC = YES
15316 +EXTRACT_LOCAL_CLASSES = YES
15317 +EXTRACT_LOCAL_METHODS = NO
15318 +HIDE_UNDOC_MEMBERS = NO
15319 +HIDE_UNDOC_CLASSES = NO
15320 +HIDE_FRIEND_COMPOUNDS = NO
15321 +HIDE_IN_BODY_DOCS = NO
15322 +INTERNAL_DOCS = NO
15323 +CASE_SENSE_NAMES = NO
15324 +HIDE_SCOPE_NAMES = NO
15325 +SHOW_INCLUDE_FILES = YES
15326 +INLINE_INFO = YES
15327 +SORT_MEMBER_DOCS = NO
15328 +SORT_BRIEF_DOCS = NO
15329 +SORT_BY_SCOPE_NAME = NO
15330 +GENERATE_TODOLIST = YES
15331 +GENERATE_TESTLIST = YES
15332 +GENERATE_BUGLIST = YES
15333 +GENERATE_DEPRECATEDLIST= YES
15334 +ENABLED_SECTIONS =
15335 +MAX_INITIALIZER_LINES = 30
15336 +SHOW_USED_FILES = YES
15337 +SHOW_DIRECTORIES = YES
15338 +#---------------------------------------------------------------------------
15339 +# configuration options related to warning and progress messages
15340 +#---------------------------------------------------------------------------
15341 +QUIET = YES
15342 +WARNINGS = YES
15343 +WARN_IF_UNDOCUMENTED = NO
15344 +WARN_IF_DOC_ERROR = YES
15345 +WARN_FORMAT = "$file:$line: $text"
15346 +WARN_LOGFILE =
15347 +#---------------------------------------------------------------------------
15348 +# configuration options related to the input files
15349 +#---------------------------------------------------------------------------
15350 +INPUT = .
15351 +FILE_PATTERNS = *.c \
15352 + *.h \
15353 + ./linux/*.c \
15354 + ./linux/*.h
15355 +RECURSIVE = NO
15356 +EXCLUDE = ./test/ \
15357 + ./dwc_otg/.AppleDouble/
15358 +EXCLUDE_SYMLINKS = YES
15359 +EXCLUDE_PATTERNS = *.mod.*
15360 +EXAMPLE_PATH =
15361 +EXAMPLE_PATTERNS = *
15362 +EXAMPLE_RECURSIVE = NO
15363 +IMAGE_PATH =
15364 +INPUT_FILTER =
15365 +FILTER_PATTERNS =
15366 +FILTER_SOURCE_FILES = NO
15367 +#---------------------------------------------------------------------------
15368 +# configuration options related to source browsing
15369 +#---------------------------------------------------------------------------
15370 +SOURCE_BROWSER = YES
15371 +INLINE_SOURCES = NO
15372 +STRIP_CODE_COMMENTS = YES
15373 +REFERENCED_BY_RELATION = NO
15374 +REFERENCES_RELATION = NO
15375 +VERBATIM_HEADERS = NO
15376 +#---------------------------------------------------------------------------
15377 +# configuration options related to the alphabetical class index
15378 +#---------------------------------------------------------------------------
15379 +ALPHABETICAL_INDEX = NO
15380 +COLS_IN_ALPHA_INDEX = 5
15381 +IGNORE_PREFIX =
15382 +#---------------------------------------------------------------------------
15383 +# configuration options related to the HTML output
15384 +#---------------------------------------------------------------------------
15385 +GENERATE_HTML = YES
15386 +HTML_OUTPUT = html
15387 +HTML_FILE_EXTENSION = .html
15388 +HTML_HEADER =
15389 +HTML_FOOTER =
15390 +HTML_STYLESHEET =
15391 +HTML_ALIGN_MEMBERS = YES
15392 +GENERATE_HTMLHELP = NO
15393 +CHM_FILE =
15394 +HHC_LOCATION =
15395 +GENERATE_CHI = NO
15396 +BINARY_TOC = NO
15397 +TOC_EXPAND = NO
15398 +DISABLE_INDEX = NO
15399 +ENUM_VALUES_PER_LINE = 4
15400 +GENERATE_TREEVIEW = YES
15401 +TREEVIEW_WIDTH = 250
15402 +#---------------------------------------------------------------------------
15403 +# configuration options related to the LaTeX output
15404 +#---------------------------------------------------------------------------
15405 +GENERATE_LATEX = NO
15406 +LATEX_OUTPUT = latex
15407 +LATEX_CMD_NAME = latex
15408 +MAKEINDEX_CMD_NAME = makeindex
15409 +COMPACT_LATEX = NO
15410 +PAPER_TYPE = a4wide
15411 +EXTRA_PACKAGES =
15412 +LATEX_HEADER =
15413 +PDF_HYPERLINKS = NO
15414 +USE_PDFLATEX = NO
15415 +LATEX_BATCHMODE = NO
15416 +LATEX_HIDE_INDICES = NO
15417 +#---------------------------------------------------------------------------
15418 +# configuration options related to the RTF output
15419 +#---------------------------------------------------------------------------
15420 +GENERATE_RTF = NO
15421 +RTF_OUTPUT = rtf
15422 +COMPACT_RTF = NO
15423 +RTF_HYPERLINKS = NO
15424 +RTF_STYLESHEET_FILE =
15425 +RTF_EXTENSIONS_FILE =
15426 +#---------------------------------------------------------------------------
15427 +# configuration options related to the man page output
15428 +#---------------------------------------------------------------------------
15429 +GENERATE_MAN = NO
15430 +MAN_OUTPUT = man
15431 +MAN_EXTENSION = .3
15432 +MAN_LINKS = NO
15433 +#---------------------------------------------------------------------------
15434 +# configuration options related to the XML output
15435 +#---------------------------------------------------------------------------
15436 +GENERATE_XML = NO
15437 +XML_OUTPUT = xml
15438 +XML_SCHEMA =
15439 +XML_DTD =
15440 +XML_PROGRAMLISTING = YES
15441 +#---------------------------------------------------------------------------
15442 +# configuration options for the AutoGen Definitions output
15443 +#---------------------------------------------------------------------------
15444 +GENERATE_AUTOGEN_DEF = NO
15445 +#---------------------------------------------------------------------------
15446 +# configuration options related to the Perl module output
15447 +#---------------------------------------------------------------------------
15448 +GENERATE_PERLMOD = NO
15449 +PERLMOD_LATEX = NO
15450 +PERLMOD_PRETTY = YES
15451 +PERLMOD_MAKEVAR_PREFIX =
15452 +#---------------------------------------------------------------------------
15453 +# Configuration options related to the preprocessor
15454 +#---------------------------------------------------------------------------
15455 +ENABLE_PREPROCESSING = YES
15456 +MACRO_EXPANSION = YES
15457 +EXPAND_ONLY_PREDEF = YES
15458 +SEARCH_INCLUDES = YES
15459 +INCLUDE_PATH =
15460 +INCLUDE_FILE_PATTERNS =
15461 +PREDEFINED = DEVICE_ATTR DWC_EN_ISOC
15462 +EXPAND_AS_DEFINED = DWC_OTG_DEVICE_ATTR_BITFIELD_SHOW DWC_OTG_DEVICE_ATTR_BITFIELD_STORE DWC_OTG_DEVICE_ATTR_BITFIELD_RW DWC_OTG_DEVICE_ATTR_BITFIELD_RO DWC_OTG_DEVICE_ATTR_REG_SHOW DWC_OTG_DEVICE_ATTR_REG_STORE DWC_OTG_DEVICE_ATTR_REG32_RW DWC_OTG_DEVICE_ATTR_REG32_RO DWC_EN_ISOC
15463 +SKIP_FUNCTION_MACROS = NO
15464 +#---------------------------------------------------------------------------
15465 +# Configuration::additions related to external references
15466 +#---------------------------------------------------------------------------
15467 +TAGFILES =
15468 +GENERATE_TAGFILE =
15469 +ALLEXTERNALS = NO
15470 +EXTERNAL_GROUPS = YES
15471 +PERL_PATH = /usr/bin/perl
15472 +#---------------------------------------------------------------------------
15473 +# Configuration options related to the dot tool
15474 +#---------------------------------------------------------------------------
15475 +CLASS_DIAGRAMS = YES
15476 +HIDE_UNDOC_RELATIONS = YES
15477 +HAVE_DOT = NO
15478 +CLASS_GRAPH = YES
15479 +COLLABORATION_GRAPH = YES
15480 +UML_LOOK = NO
15481 +TEMPLATE_RELATIONS = NO
15482 +INCLUDE_GRAPH = YES
15483 +INCLUDED_BY_GRAPH = YES
15484 +CALL_GRAPH = NO
15485 +GRAPHICAL_HIERARCHY = YES
15486 +DOT_IMAGE_FORMAT = png
15487 +DOT_PATH =
15488 +DOTFILE_DIRS =
15489 +MAX_DOT_GRAPH_DEPTH = 1000
15490 +GENERATE_LEGEND = YES
15491 +DOT_CLEANUP = YES
15492 +#---------------------------------------------------------------------------
15493 +# Configuration::additions related to the search engine
15494 +#---------------------------------------------------------------------------
15495 +SEARCHENGINE = NO
15496 --- /dev/null
15497 +++ b/drivers/usb/host/dwc_otg/dummy_audio.c
15498 @@ -0,0 +1,1574 @@
15499 +/*
15500 + * zero.c -- Gadget Zero, for USB development
15501 + *
15502 + * Copyright (C) 2003-2004 David Brownell
15503 + * All rights reserved.
15504 + *
15505 + * Redistribution and use in source and binary forms, with or without
15506 + * modification, are permitted provided that the following conditions
15507 + * are met:
15508 + * 1. Redistributions of source code must retain the above copyright
15509 + * notice, this list of conditions, and the following disclaimer,
15510 + * without modification.
15511 + * 2. Redistributions in binary form must reproduce the above copyright
15512 + * notice, this list of conditions and the following disclaimer in the
15513 + * documentation and/or other materials provided with the distribution.
15514 + * 3. The names of the above-listed copyright holders may not be used
15515 + * to endorse or promote products derived from this software without
15516 + * specific prior written permission.
15517 + *
15518 + * ALTERNATIVELY, this software may be distributed under the terms of the
15519 + * GNU General Public License ("GPL") as published by the Free Software
15520 + * Foundation, either version 2 of that License or (at your option) any
15521 + * later version.
15522 + *
15523 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
15524 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15525 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
15526 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
15527 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
15528 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
15529 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
15530 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
15531 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
15532 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
15533 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15534 + */
15535 +
15536 +
15537 +/*
15538 + * Gadget Zero only needs two bulk endpoints, and is an example of how you
15539 + * can write a hardware-agnostic gadget driver running inside a USB device.
15540 + *
15541 + * Hardware details are visible (see CONFIG_USB_ZERO_* below) but don't
15542 + * affect most of the driver.
15543 + *
15544 + * Use it with the Linux host/master side "usbtest" driver to get a basic
15545 + * functional test of your device-side usb stack, or with "usb-skeleton".
15546 + *
15547 + * It supports two similar configurations. One sinks whatever the usb host
15548 + * writes, and in return sources zeroes. The other loops whatever the host
15549 + * writes back, so the host can read it. Module options include:
15550 + *
15551 + * buflen=N default N=4096, buffer size used
15552 + * qlen=N default N=32, how many buffers in the loopback queue
15553 + * loopdefault default false, list loopback config first
15554 + *
15555 + * Many drivers will only have one configuration, letting them be much
15556 + * simpler if they also don't support high speed operation (like this
15557 + * driver does).
15558 + */
15559 +
15560 +#include <linux/config.h>
15561 +#include <linux/module.h>
15562 +#include <linux/kernel.h>
15563 +#include <linux/delay.h>
15564 +#include <linux/ioport.h>
15565 +#include <linux/sched.h>
15566 +#include <linux/slab.h>
15567 +#include <linux/smp_lock.h>
15568 +#include <linux/errno.h>
15569 +#include <linux/init.h>
15570 +#include <linux/timer.h>
15571 +#include <linux/list.h>
15572 +#include <linux/interrupt.h>
15573 +#include <linux/uts.h>
15574 +#include <linux/version.h>
15575 +#include <linux/device.h>
15576 +#include <linux/moduleparam.h>
15577 +#include <linux/proc_fs.h>
15578 +
15579 +#include <asm/byteorder.h>
15580 +#include <asm/io.h>
15581 +#include <asm/irq.h>
15582 +#include <asm/system.h>
15583 +#include <asm/unaligned.h>
15584 +
15585 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
15586 +# include <linux/usb/ch9.h>
15587 +#else
15588 +# include <linux/usb_ch9.h>
15589 +#endif
15590 +
15591 +#include <linux/usb_gadget.h>
15592 +
15593 +
15594 +/*-------------------------------------------------------------------------*/
15595 +/*-------------------------------------------------------------------------*/
15596 +
15597 +
15598 +static int utf8_to_utf16le(const char *s, u16 *cp, unsigned len)
15599 +{
15600 + int count = 0;
15601 + u8 c;
15602 + u16 uchar;
15603 +
15604 + /* this insists on correct encodings, though not minimal ones.
15605 + * BUT it currently rejects legit 4-byte UTF-8 code points,
15606 + * which need surrogate pairs. (Unicode 3.1 can use them.)
15607 + */
15608 + while (len != 0 && (c = (u8) *s++) != 0) {
15609 + if (unlikely(c & 0x80)) {
15610 + // 2-byte sequence:
15611 + // 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx
15612 + if ((c & 0xe0) == 0xc0) {
15613 + uchar = (c & 0x1f) << 6;
15614 +
15615 + c = (u8) *s++;
15616 + if ((c & 0xc0) != 0xc0)
15617 + goto fail;
15618 + c &= 0x3f;
15619 + uchar |= c;
15620 +
15621 + // 3-byte sequence (most CJKV characters):
15622 + // zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx
15623 + } else if ((c & 0xf0) == 0xe0) {
15624 + uchar = (c & 0x0f) << 12;
15625 +
15626 + c = (u8) *s++;
15627 + if ((c & 0xc0) != 0xc0)
15628 + goto fail;
15629 + c &= 0x3f;
15630 + uchar |= c << 6;
15631 +
15632 + c = (u8) *s++;
15633 + if ((c & 0xc0) != 0xc0)
15634 + goto fail;
15635 + c &= 0x3f;
15636 + uchar |= c;
15637 +
15638 + /* no bogus surrogates */
15639 + if (0xd800 <= uchar && uchar <= 0xdfff)
15640 + goto fail;
15641 +
15642 + // 4-byte sequence (surrogate pairs, currently rare):
15643 + // 11101110wwwwzzzzyy + 110111yyyyxxxxxx
15644 + // = 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx
15645 + // (uuuuu = wwww + 1)
15646 + // FIXME accept the surrogate code points (only)
15647 +
15648 + } else
15649 + goto fail;
15650 + } else
15651 + uchar = c;
15652 + put_unaligned (cpu_to_le16 (uchar), cp++);
15653 + count++;
15654 + len--;
15655 + }
15656 + return count;
15657 +fail:
15658 + return -1;
15659 +}
15660 +
15661 +
15662 +/**
15663 + * usb_gadget_get_string - fill out a string descriptor
15664 + * @table: of c strings encoded using UTF-8
15665 + * @id: string id, from low byte of wValue in get string descriptor
15666 + * @buf: at least 256 bytes
15667 + *
15668 + * Finds the UTF-8 string matching the ID, and converts it into a
15669 + * string descriptor in utf16-le.
15670 + * Returns length of descriptor (always even) or negative errno
15671 + *
15672 + * If your driver needs stings in multiple languages, you'll probably
15673 + * "switch (wIndex) { ... }" in your ep0 string descriptor logic,
15674 + * using this routine after choosing which set of UTF-8 strings to use.
15675 + * Note that US-ASCII is a strict subset of UTF-8; any string bytes with
15676 + * the eighth bit set will be multibyte UTF-8 characters, not ISO-8859/1
15677 + * characters (which are also widely used in C strings).
15678 + */
15679 +int
15680 +usb_gadget_get_string (struct usb_gadget_strings *table, int id, u8 *buf)
15681 +{
15682 + struct usb_string *s;
15683 + int len;
15684 +
15685 + /* descriptor 0 has the language id */
15686 + if (id == 0) {
15687 + buf [0] = 4;
15688 + buf [1] = USB_DT_STRING;
15689 + buf [2] = (u8) table->language;
15690 + buf [3] = (u8) (table->language >> 8);
15691 + return 4;
15692 + }
15693 + for (s = table->strings; s && s->s; s++)
15694 + if (s->id == id)
15695 + break;
15696 +
15697 + /* unrecognized: stall. */
15698 + if (!s || !s->s)
15699 + return -EINVAL;
15700 +
15701 + /* string descriptors have length, tag, then UTF16-LE text */
15702 + len = min ((size_t) 126, strlen (s->s));
15703 + memset (buf + 2, 0, 2 * len); /* zero all the bytes */
15704 + len = utf8_to_utf16le(s->s, (u16 *)&buf[2], len);
15705 + if (len < 0)
15706 + return -EINVAL;
15707 + buf [0] = (len + 1) * 2;
15708 + buf [1] = USB_DT_STRING;
15709 + return buf [0];
15710 +}
15711 +
15712 +
15713 +/*-------------------------------------------------------------------------*/
15714 +/*-------------------------------------------------------------------------*/
15715 +
15716 +
15717 +/**
15718 + * usb_descriptor_fillbuf - fill buffer with descriptors
15719 + * @buf: Buffer to be filled
15720 + * @buflen: Size of buf
15721 + * @src: Array of descriptor pointers, terminated by null pointer.
15722 + *
15723 + * Copies descriptors into the buffer, returning the length or a
15724 + * negative error code if they can't all be copied. Useful when
15725 + * assembling descriptors for an associated set of interfaces used
15726 + * as part of configuring a composite device; or in other cases where
15727 + * sets of descriptors need to be marshaled.
15728 + */
15729 +int
15730 +usb_descriptor_fillbuf(void *buf, unsigned buflen,
15731 + const struct usb_descriptor_header **src)
15732 +{
15733 + u8 *dest = buf;
15734 +
15735 + if (!src)
15736 + return -EINVAL;
15737 +
15738 + /* fill buffer from src[] until null descriptor ptr */
15739 + for (; 0 != *src; src++) {
15740 + unsigned len = (*src)->bLength;
15741 +
15742 + if (len > buflen)
15743 + return -EINVAL;
15744 + memcpy(dest, *src, len);
15745 + buflen -= len;
15746 + dest += len;
15747 + }
15748 + return dest - (u8 *)buf;
15749 +}
15750 +
15751 +
15752 +/**
15753 + * usb_gadget_config_buf - builts a complete configuration descriptor
15754 + * @config: Header for the descriptor, including characteristics such
15755 + * as power requirements and number of interfaces.
15756 + * @desc: Null-terminated vector of pointers to the descriptors (interface,
15757 + * endpoint, etc) defining all functions in this device configuration.
15758 + * @buf: Buffer for the resulting configuration descriptor.
15759 + * @length: Length of buffer. If this is not big enough to hold the
15760 + * entire configuration descriptor, an error code will be returned.
15761 + *
15762 + * This copies descriptors into the response buffer, building a descriptor
15763 + * for that configuration. It returns the buffer length or a negative
15764 + * status code. The config.wTotalLength field is set to match the length
15765 + * of the result, but other descriptor fields (including power usage and
15766 + * interface count) must be set by the caller.
15767 + *
15768 + * Gadget drivers could use this when constructing a config descriptor
15769 + * in response to USB_REQ_GET_DESCRIPTOR. They will need to patch the
15770 + * resulting bDescriptorType value if USB_DT_OTHER_SPEED_CONFIG is needed.
15771 + */
15772 +int usb_gadget_config_buf(
15773 + const struct usb_config_descriptor *config,
15774 + void *buf,
15775 + unsigned length,
15776 + const struct usb_descriptor_header **desc
15777 +)
15778 +{
15779 + struct usb_config_descriptor *cp = buf;
15780 + int len;
15781 +
15782 + /* config descriptor first */
15783 + if (length < USB_DT_CONFIG_SIZE || !desc)
15784 + return -EINVAL;
15785 + *cp = *config;
15786 +
15787 + /* then interface/endpoint/class/vendor/... */
15788 + len = usb_descriptor_fillbuf(USB_DT_CONFIG_SIZE + (u8*)buf,
15789 + length - USB_DT_CONFIG_SIZE, desc);
15790 + if (len < 0)
15791 + return len;
15792 + len += USB_DT_CONFIG_SIZE;
15793 + if (len > 0xffff)
15794 + return -EINVAL;
15795 +
15796 + /* patch up the config descriptor */
15797 + cp->bLength = USB_DT_CONFIG_SIZE;
15798 + cp->bDescriptorType = USB_DT_CONFIG;
15799 + cp->wTotalLength = cpu_to_le16(len);
15800 + cp->bmAttributes |= USB_CONFIG_ATT_ONE;
15801 + return len;
15802 +}
15803 +
15804 +/*-------------------------------------------------------------------------*/
15805 +/*-------------------------------------------------------------------------*/
15806 +
15807 +
15808 +#define RBUF_LEN (1024*1024)
15809 +static int rbuf_start;
15810 +static int rbuf_len;
15811 +static __u8 rbuf[RBUF_LEN];
15812 +
15813 +/*-------------------------------------------------------------------------*/
15814 +
15815 +#define DRIVER_VERSION "St Patrick's Day 2004"
15816 +
15817 +static const char shortname [] = "zero";
15818 +static const char longname [] = "YAMAHA YST-MS35D USB Speaker ";
15819 +
15820 +static const char source_sink [] = "source and sink data";
15821 +static const char loopback [] = "loop input to output";
15822 +
15823 +/*-------------------------------------------------------------------------*/
15824 +
15825 +/*
15826 + * driver assumes self-powered hardware, and
15827 + * has no way for users to trigger remote wakeup.
15828 + *
15829 + * this version autoconfigures as much as possible,
15830 + * which is reasonable for most "bulk-only" drivers.
15831 + */
15832 +static const char *EP_IN_NAME; /* source */
15833 +static const char *EP_OUT_NAME; /* sink */
15834 +
15835 +/*-------------------------------------------------------------------------*/
15836 +
15837 +/* big enough to hold our biggest descriptor */
15838 +#define USB_BUFSIZ 512
15839 +
15840 +struct zero_dev {
15841 + spinlock_t lock;
15842 + struct usb_gadget *gadget;
15843 + struct usb_request *req; /* for control responses */
15844 +
15845 + /* when configured, we have one of two configs:
15846 + * - source data (in to host) and sink it (out from host)
15847 + * - or loop it back (out from host back in to host)
15848 + */
15849 + u8 config;
15850 + struct usb_ep *in_ep, *out_ep;
15851 +
15852 + /* autoresume timer */
15853 + struct timer_list resume;
15854 +};
15855 +
15856 +#define xprintk(d,level,fmt,args...) \
15857 + dev_printk(level , &(d)->gadget->dev , fmt , ## args)
15858 +
15859 +#ifdef DEBUG
15860 +#define DBG(dev,fmt,args...) \
15861 + xprintk(dev , KERN_DEBUG , fmt , ## args)
15862 +#else
15863 +#define DBG(dev,fmt,args...) \
15864 + do { } while (0)
15865 +#endif /* DEBUG */
15866 +
15867 +#ifdef VERBOSE
15868 +#define VDBG DBG
15869 +#else
15870 +#define VDBG(dev,fmt,args...) \
15871 + do { } while (0)
15872 +#endif /* VERBOSE */
15873 +
15874 +#define ERROR(dev,fmt,args...) \
15875 + xprintk(dev , KERN_ERR , fmt , ## args)
15876 +#define WARN(dev,fmt,args...) \
15877 + xprintk(dev , KERN_WARNING , fmt , ## args)
15878 +#define INFO(dev,fmt,args...) \
15879 + xprintk(dev , KERN_INFO , fmt , ## args)
15880 +
15881 +/*-------------------------------------------------------------------------*/
15882 +
15883 +static unsigned buflen = 4096;
15884 +static unsigned qlen = 32;
15885 +static unsigned pattern = 0;
15886 +
15887 +module_param (buflen, uint, S_IRUGO|S_IWUSR);
15888 +module_param (qlen, uint, S_IRUGO|S_IWUSR);
15889 +module_param (pattern, uint, S_IRUGO|S_IWUSR);
15890 +
15891 +/*
15892 + * if it's nonzero, autoresume says how many seconds to wait
15893 + * before trying to wake up the host after suspend.
15894 + */
15895 +static unsigned autoresume = 0;
15896 +module_param (autoresume, uint, 0);
15897 +
15898 +/*
15899 + * Normally the "loopback" configuration is second (index 1) so
15900 + * it's not the default. Here's where to change that order, to
15901 + * work better with hosts where config changes are problematic.
15902 + * Or controllers (like superh) that only support one config.
15903 + */
15904 +static int loopdefault = 0;
15905 +
15906 +module_param (loopdefault, bool, S_IRUGO|S_IWUSR);
15907 +
15908 +/*-------------------------------------------------------------------------*/
15909 +
15910 +/* Thanks to NetChip Technologies for donating this product ID.
15911 + *
15912 + * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
15913 + * Instead: allocate your own, using normal USB-IF procedures.
15914 + */
15915 +#ifndef CONFIG_USB_ZERO_HNPTEST
15916 +#define DRIVER_VENDOR_NUM 0x0525 /* NetChip */
15917 +#define DRIVER_PRODUCT_NUM 0xa4a0 /* Linux-USB "Gadget Zero" */
15918 +#else
15919 +#define DRIVER_VENDOR_NUM 0x1a0a /* OTG test device IDs */
15920 +#define DRIVER_PRODUCT_NUM 0xbadd
15921 +#endif
15922 +
15923 +/*-------------------------------------------------------------------------*/
15924 +
15925 +/*
15926 + * DESCRIPTORS ... most are static, but strings and (full)
15927 + * configuration descriptors are built on demand.
15928 + */
15929 +
15930 +/*
15931 +#define STRING_MANUFACTURER 25
15932 +#define STRING_PRODUCT 42
15933 +#define STRING_SERIAL 101
15934 +*/
15935 +#define STRING_MANUFACTURER 1
15936 +#define STRING_PRODUCT 2
15937 +#define STRING_SERIAL 3
15938 +
15939 +#define STRING_SOURCE_SINK 250
15940 +#define STRING_LOOPBACK 251
15941 +
15942 +/*
15943 + * This device advertises two configurations; these numbers work
15944 + * on a pxa250 as well as more flexible hardware.
15945 + */
15946 +#define CONFIG_SOURCE_SINK 3
15947 +#define CONFIG_LOOPBACK 2
15948 +
15949 +/*
15950 +static struct usb_device_descriptor
15951 +device_desc = {
15952 + .bLength = sizeof device_desc,
15953 + .bDescriptorType = USB_DT_DEVICE,
15954 +
15955 + .bcdUSB = __constant_cpu_to_le16 (0x0200),
15956 + .bDeviceClass = USB_CLASS_VENDOR_SPEC,
15957 +
15958 + .idVendor = __constant_cpu_to_le16 (DRIVER_VENDOR_NUM),
15959 + .idProduct = __constant_cpu_to_le16 (DRIVER_PRODUCT_NUM),
15960 + .iManufacturer = STRING_MANUFACTURER,
15961 + .iProduct = STRING_PRODUCT,
15962 + .iSerialNumber = STRING_SERIAL,
15963 + .bNumConfigurations = 2,
15964 +};
15965 +*/
15966 +static struct usb_device_descriptor
15967 +device_desc = {
15968 + .bLength = sizeof device_desc,
15969 + .bDescriptorType = USB_DT_DEVICE,
15970 + .bcdUSB = __constant_cpu_to_le16 (0x0100),
15971 + .bDeviceClass = USB_CLASS_PER_INTERFACE,
15972 + .bDeviceSubClass = 0,
15973 + .bDeviceProtocol = 0,
15974 + .bMaxPacketSize0 = 64,
15975 + .bcdDevice = __constant_cpu_to_le16 (0x0100),
15976 + .idVendor = __constant_cpu_to_le16 (0x0499),
15977 + .idProduct = __constant_cpu_to_le16 (0x3002),
15978 + .iManufacturer = STRING_MANUFACTURER,
15979 + .iProduct = STRING_PRODUCT,
15980 + .iSerialNumber = STRING_SERIAL,
15981 + .bNumConfigurations = 1,
15982 +};
15983 +
15984 +static struct usb_config_descriptor
15985 +z_config = {
15986 + .bLength = sizeof z_config,
15987 + .bDescriptorType = USB_DT_CONFIG,
15988 +
15989 + /* compute wTotalLength on the fly */
15990 + .bNumInterfaces = 2,
15991 + .bConfigurationValue = 1,
15992 + .iConfiguration = 0,
15993 + .bmAttributes = 0x40,
15994 + .bMaxPower = 0, /* self-powered */
15995 +};
15996 +
15997 +
15998 +static struct usb_otg_descriptor
15999 +otg_descriptor = {
16000 + .bLength = sizeof otg_descriptor,
16001 + .bDescriptorType = USB_DT_OTG,
16002 +
16003 + .bmAttributes = USB_OTG_SRP,
16004 +};
16005 +
16006 +/* one interface in each configuration */
16007 +#ifdef CONFIG_USB_GADGET_DUALSPEED
16008 +
16009 +/*
16010 + * usb 2.0 devices need to expose both high speed and full speed
16011 + * descriptors, unless they only run at full speed.
16012 + *
16013 + * that means alternate endpoint descriptors (bigger packets)
16014 + * and a "device qualifier" ... plus more construction options
16015 + * for the config descriptor.
16016 + */
16017 +
16018 +static struct usb_qualifier_descriptor
16019 +dev_qualifier = {
16020 + .bLength = sizeof dev_qualifier,
16021 + .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
16022 +
16023 + .bcdUSB = __constant_cpu_to_le16 (0x0200),
16024 + .bDeviceClass = USB_CLASS_VENDOR_SPEC,
16025 +
16026 + .bNumConfigurations = 2,
16027 +};
16028 +
16029 +
16030 +struct usb_cs_as_general_descriptor {
16031 + __u8 bLength;
16032 + __u8 bDescriptorType;
16033 +
16034 + __u8 bDescriptorSubType;
16035 + __u8 bTerminalLink;
16036 + __u8 bDelay;
16037 + __u16 wFormatTag;
16038 +} __attribute__ ((packed));
16039 +
16040 +struct usb_cs_as_format_descriptor {
16041 + __u8 bLength;
16042 + __u8 bDescriptorType;
16043 +
16044 + __u8 bDescriptorSubType;
16045 + __u8 bFormatType;
16046 + __u8 bNrChannels;
16047 + __u8 bSubframeSize;
16048 + __u8 bBitResolution;
16049 + __u8 bSamfreqType;
16050 + __u8 tLowerSamFreq[3];
16051 + __u8 tUpperSamFreq[3];
16052 +} __attribute__ ((packed));
16053 +
16054 +static const struct usb_interface_descriptor
16055 +z_audio_control_if_desc = {
16056 + .bLength = sizeof z_audio_control_if_desc,
16057 + .bDescriptorType = USB_DT_INTERFACE,
16058 + .bInterfaceNumber = 0,
16059 + .bAlternateSetting = 0,
16060 + .bNumEndpoints = 0,
16061 + .bInterfaceClass = USB_CLASS_AUDIO,
16062 + .bInterfaceSubClass = 0x1,
16063 + .bInterfaceProtocol = 0,
16064 + .iInterface = 0,
16065 +};
16066 +
16067 +static const struct usb_interface_descriptor
16068 +z_audio_if_desc = {
16069 + .bLength = sizeof z_audio_if_desc,
16070 + .bDescriptorType = USB_DT_INTERFACE,
16071 + .bInterfaceNumber = 1,
16072 + .bAlternateSetting = 0,
16073 + .bNumEndpoints = 0,
16074 + .bInterfaceClass = USB_CLASS_AUDIO,
16075 + .bInterfaceSubClass = 0x2,
16076 + .bInterfaceProtocol = 0,
16077 + .iInterface = 0,
16078 +};
16079 +
16080 +static const struct usb_interface_descriptor
16081 +z_audio_if_desc2 = {
16082 + .bLength = sizeof z_audio_if_desc,
16083 + .bDescriptorType = USB_DT_INTERFACE,
16084 + .bInterfaceNumber = 1,
16085 + .bAlternateSetting = 1,
16086 + .bNumEndpoints = 1,
16087 + .bInterfaceClass = USB_CLASS_AUDIO,
16088 + .bInterfaceSubClass = 0x2,
16089 + .bInterfaceProtocol = 0,
16090 + .iInterface = 0,
16091 +};
16092 +
16093 +static const struct usb_cs_as_general_descriptor
16094 +z_audio_cs_as_if_desc = {
16095 + .bLength = 7,
16096 + .bDescriptorType = 0x24,
16097 +
16098 + .bDescriptorSubType = 0x01,
16099 + .bTerminalLink = 0x01,
16100 + .bDelay = 0x0,
16101 + .wFormatTag = __constant_cpu_to_le16 (0x0001)
16102 +};
16103 +
16104 +
16105 +static const struct usb_cs_as_format_descriptor
16106 +z_audio_cs_as_format_desc = {
16107 + .bLength = 0xe,
16108 + .bDescriptorType = 0x24,
16109 +
16110 + .bDescriptorSubType = 2,
16111 + .bFormatType = 1,
16112 + .bNrChannels = 1,
16113 + .bSubframeSize = 1,
16114 + .bBitResolution = 8,
16115 + .bSamfreqType = 0,
16116 + .tLowerSamFreq = {0x7e, 0x13, 0x00},
16117 + .tUpperSamFreq = {0xe2, 0xd6, 0x00},
16118 +};
16119 +
16120 +static const struct usb_endpoint_descriptor
16121 +z_iso_ep = {
16122 + .bLength = 0x09,
16123 + .bDescriptorType = 0x05,
16124 + .bEndpointAddress = 0x04,
16125 + .bmAttributes = 0x09,
16126 + .wMaxPacketSize = 0x0038,
16127 + .bInterval = 0x01,
16128 + .bRefresh = 0x00,
16129 + .bSynchAddress = 0x00,
16130 +};
16131 +
16132 +static char z_iso_ep2[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
16133 +
16134 +// 9 bytes
16135 +static char z_ac_interface_header_desc[] =
16136 +{ 0x09, 0x24, 0x01, 0x00, 0x01, 0x2b, 0x00, 0x01, 0x01 };
16137 +
16138 +// 12 bytes
16139 +static char z_0[] = {0x0c, 0x24, 0x02, 0x01, 0x01, 0x01, 0x00, 0x02,
16140 + 0x03, 0x00, 0x00, 0x00};
16141 +// 13 bytes
16142 +static char z_1[] = {0x0d, 0x24, 0x06, 0x02, 0x01, 0x02, 0x15, 0x00,
16143 + 0x02, 0x00, 0x02, 0x00, 0x00};
16144 +// 9 bytes
16145 +static char z_2[] = {0x09, 0x24, 0x03, 0x03, 0x01, 0x03, 0x00, 0x02,
16146 + 0x00};
16147 +
16148 +static char za_0[] = {0x09, 0x04, 0x01, 0x02, 0x01, 0x01, 0x02, 0x00,
16149 + 0x00};
16150 +
16151 +static char za_1[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
16152 +
16153 +static char za_2[] = {0x0e, 0x24, 0x02, 0x01, 0x02, 0x01, 0x08, 0x00,
16154 + 0x7e, 0x13, 0x00, 0xe2, 0xd6, 0x00};
16155 +
16156 +static char za_3[] = {0x09, 0x05, 0x04, 0x09, 0x70, 0x00, 0x01, 0x00,
16157 + 0x00};
16158 +
16159 +static char za_4[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
16160 +
16161 +static char za_5[] = {0x09, 0x04, 0x01, 0x03, 0x01, 0x01, 0x02, 0x00,
16162 + 0x00};
16163 +
16164 +static char za_6[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
16165 +
16166 +static char za_7[] = {0x0e, 0x24, 0x02, 0x01, 0x01, 0x02, 0x10, 0x00,
16167 + 0x7e, 0x13, 0x00, 0xe2, 0xd6, 0x00};
16168 +
16169 +static char za_8[] = {0x09, 0x05, 0x04, 0x09, 0x70, 0x00, 0x01, 0x00,
16170 + 0x00};
16171 +
16172 +static char za_9[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
16173 +
16174 +static char za_10[] = {0x09, 0x04, 0x01, 0x04, 0x01, 0x01, 0x02, 0x00,
16175 + 0x00};
16176 +
16177 +static char za_11[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
16178 +
16179 +static char za_12[] = {0x0e, 0x24, 0x02, 0x01, 0x02, 0x02, 0x10, 0x00,
16180 + 0x73, 0x13, 0x00, 0xe2, 0xd6, 0x00};
16181 +
16182 +static char za_13[] = {0x09, 0x05, 0x04, 0x09, 0xe0, 0x00, 0x01, 0x00,
16183 + 0x00};
16184 +
16185 +static char za_14[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
16186 +
16187 +static char za_15[] = {0x09, 0x04, 0x01, 0x05, 0x01, 0x01, 0x02, 0x00,
16188 + 0x00};
16189 +
16190 +static char za_16[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
16191 +
16192 +static char za_17[] = {0x0e, 0x24, 0x02, 0x01, 0x01, 0x03, 0x14, 0x00,
16193 + 0x7e, 0x13, 0x00, 0xe2, 0xd6, 0x00};
16194 +
16195 +static char za_18[] = {0x09, 0x05, 0x04, 0x09, 0xa8, 0x00, 0x01, 0x00,
16196 + 0x00};
16197 +
16198 +static char za_19[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
16199 +
16200 +static char za_20[] = {0x09, 0x04, 0x01, 0x06, 0x01, 0x01, 0x02, 0x00,
16201 + 0x00};
16202 +
16203 +static char za_21[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
16204 +
16205 +static char za_22[] = {0x0e, 0x24, 0x02, 0x01, 0x02, 0x03, 0x14, 0x00,
16206 + 0x7e, 0x13, 0x00, 0xe2, 0xd6, 0x00};
16207 +
16208 +static char za_23[] = {0x09, 0x05, 0x04, 0x09, 0x50, 0x01, 0x01, 0x00,
16209 + 0x00};
16210 +
16211 +static char za_24[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
16212 +
16213 +
16214 +
16215 +static const struct usb_descriptor_header *z_function [] = {
16216 + (struct usb_descriptor_header *) &z_audio_control_if_desc,
16217 + (struct usb_descriptor_header *) &z_ac_interface_header_desc,
16218 + (struct usb_descriptor_header *) &z_0,
16219 + (struct usb_descriptor_header *) &z_1,
16220 + (struct usb_descriptor_header *) &z_2,
16221 + (struct usb_descriptor_header *) &z_audio_if_desc,
16222 + (struct usb_descriptor_header *) &z_audio_if_desc2,
16223 + (struct usb_descriptor_header *) &z_audio_cs_as_if_desc,
16224 + (struct usb_descriptor_header *) &z_audio_cs_as_format_desc,
16225 + (struct usb_descriptor_header *) &z_iso_ep,
16226 + (struct usb_descriptor_header *) &z_iso_ep2,
16227 + (struct usb_descriptor_header *) &za_0,
16228 + (struct usb_descriptor_header *) &za_1,
16229 + (struct usb_descriptor_header *) &za_2,
16230 + (struct usb_descriptor_header *) &za_3,
16231 + (struct usb_descriptor_header *) &za_4,
16232 + (struct usb_descriptor_header *) &za_5,
16233 + (struct usb_descriptor_header *) &za_6,
16234 + (struct usb_descriptor_header *) &za_7,
16235 + (struct usb_descriptor_header *) &za_8,
16236 + (struct usb_descriptor_header *) &za_9,
16237 + (struct usb_descriptor_header *) &za_10,
16238 + (struct usb_descriptor_header *) &za_11,
16239 + (struct usb_descriptor_header *) &za_12,
16240 + (struct usb_descriptor_header *) &za_13,
16241 + (struct usb_descriptor_header *) &za_14,
16242 + (struct usb_descriptor_header *) &za_15,
16243 + (struct usb_descriptor_header *) &za_16,
16244 + (struct usb_descriptor_header *) &za_17,
16245 + (struct usb_descriptor_header *) &za_18,
16246 + (struct usb_descriptor_header *) &za_19,
16247 + (struct usb_descriptor_header *) &za_20,
16248 + (struct usb_descriptor_header *) &za_21,
16249 + (struct usb_descriptor_header *) &za_22,
16250 + (struct usb_descriptor_header *) &za_23,
16251 + (struct usb_descriptor_header *) &za_24,
16252 + NULL,
16253 +};
16254 +
16255 +/* maxpacket and other transfer characteristics vary by speed. */
16256 +#define ep_desc(g,hs,fs) (((g)->speed==USB_SPEED_HIGH)?(hs):(fs))
16257 +
16258 +#else
16259 +
16260 +/* if there's no high speed support, maxpacket doesn't change. */
16261 +#define ep_desc(g,hs,fs) fs
16262 +
16263 +#endif /* !CONFIG_USB_GADGET_DUALSPEED */
16264 +
16265 +static char manufacturer [40];
16266 +//static char serial [40];
16267 +static char serial [] = "Ser 00 em";
16268 +
16269 +/* static strings, in UTF-8 */
16270 +static struct usb_string strings [] = {
16271 + { STRING_MANUFACTURER, manufacturer, },
16272 + { STRING_PRODUCT, longname, },
16273 + { STRING_SERIAL, serial, },
16274 + { STRING_LOOPBACK, loopback, },
16275 + { STRING_SOURCE_SINK, source_sink, },
16276 + { } /* end of list */
16277 +};
16278 +
16279 +static struct usb_gadget_strings stringtab = {
16280 + .language = 0x0409, /* en-us */
16281 + .strings = strings,
16282 +};
16283 +
16284 +/*
16285 + * config descriptors are also handcrafted. these must agree with code
16286 + * that sets configurations, and with code managing interfaces and their
16287 + * altsettings. other complexity may come from:
16288 + *
16289 + * - high speed support, including "other speed config" rules
16290 + * - multiple configurations
16291 + * - interfaces with alternate settings
16292 + * - embedded class or vendor-specific descriptors
16293 + *
16294 + * this handles high speed, and has a second config that could as easily
16295 + * have been an alternate interface setting (on most hardware).
16296 + *
16297 + * NOTE: to demonstrate (and test) more USB capabilities, this driver
16298 + * should include an altsetting to test interrupt transfers, including
16299 + * high bandwidth modes at high speed. (Maybe work like Intel's test
16300 + * device?)
16301 + */
16302 +static int
16303 +config_buf (struct usb_gadget *gadget, u8 *buf, u8 type, unsigned index)
16304 +{
16305 + int len;
16306 + const struct usb_descriptor_header **function;
16307 +
16308 + function = z_function;
16309 + len = usb_gadget_config_buf (&z_config, buf, USB_BUFSIZ, function);
16310 + if (len < 0)
16311 + return len;
16312 + ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
16313 + return len;
16314 +}
16315 +
16316 +/*-------------------------------------------------------------------------*/
16317 +
16318 +static struct usb_request *
16319 +alloc_ep_req (struct usb_ep *ep, unsigned length)
16320 +{
16321 + struct usb_request *req;
16322 +
16323 + req = usb_ep_alloc_request (ep, GFP_ATOMIC);
16324 + if (req) {
16325 + req->length = length;
16326 + req->buf = usb_ep_alloc_buffer (ep, length,
16327 + &req->dma, GFP_ATOMIC);
16328 + if (!req->buf) {
16329 + usb_ep_free_request (ep, req);
16330 + req = NULL;
16331 + }
16332 + }
16333 + return req;
16334 +}
16335 +
16336 +static void free_ep_req (struct usb_ep *ep, struct usb_request *req)
16337 +{
16338 + if (req->buf)
16339 + usb_ep_free_buffer (ep, req->buf, req->dma, req->length);
16340 + usb_ep_free_request (ep, req);
16341 +}
16342 +
16343 +/*-------------------------------------------------------------------------*/
16344 +
16345 +/* optionally require specific source/sink data patterns */
16346 +
16347 +static int
16348 +check_read_data (
16349 + struct zero_dev *dev,
16350 + struct usb_ep *ep,
16351 + struct usb_request *req
16352 +)
16353 +{
16354 + unsigned i;
16355 + u8 *buf = req->buf;
16356 +
16357 + for (i = 0; i < req->actual; i++, buf++) {
16358 + switch (pattern) {
16359 + /* all-zeroes has no synchronization issues */
16360 + case 0:
16361 + if (*buf == 0)
16362 + continue;
16363 + break;
16364 + /* mod63 stays in sync with short-terminated transfers,
16365 + * or otherwise when host and gadget agree on how large
16366 + * each usb transfer request should be. resync is done
16367 + * with set_interface or set_config.
16368 + */
16369 + case 1:
16370 + if (*buf == (u8)(i % 63))
16371 + continue;
16372 + break;
16373 + }
16374 + ERROR (dev, "bad OUT byte, buf [%d] = %d\n", i, *buf);
16375 + usb_ep_set_halt (ep);
16376 + return -EINVAL;
16377 + }
16378 + return 0;
16379 +}
16380 +
16381 +/*-------------------------------------------------------------------------*/
16382 +
16383 +static void zero_reset_config (struct zero_dev *dev)
16384 +{
16385 + if (dev->config == 0)
16386 + return;
16387 +
16388 + DBG (dev, "reset config\n");
16389 +
16390 + /* just disable endpoints, forcing completion of pending i/o.
16391 + * all our completion handlers free their requests in this case.
16392 + */
16393 + if (dev->in_ep) {
16394 + usb_ep_disable (dev->in_ep);
16395 + dev->in_ep = NULL;
16396 + }
16397 + if (dev->out_ep) {
16398 + usb_ep_disable (dev->out_ep);
16399 + dev->out_ep = NULL;
16400 + }
16401 + dev->config = 0;
16402 + del_timer (&dev->resume);
16403 +}
16404 +
16405 +#define _write(f, buf, sz) (f->f_op->write(f, buf, sz, &f->f_pos))
16406 +
16407 +static void
16408 +zero_isoc_complete (struct usb_ep *ep, struct usb_request *req)
16409 +{
16410 + struct zero_dev *dev = ep->driver_data;
16411 + int status = req->status;
16412 + int i, j;
16413 +
16414 + switch (status) {
16415 +
16416 + case 0: /* normal completion? */
16417 + //printk ("\nzero ---------------> isoc normal completion %d bytes\n", req->actual);
16418 + for (i=0, j=rbuf_start; i<req->actual; i++) {
16419 + //printk ("%02x ", ((__u8*)req->buf)[i]);
16420 + rbuf[j] = ((__u8*)req->buf)[i];
16421 + j++;
16422 + if (j >= RBUF_LEN) j=0;
16423 + }
16424 + rbuf_start = j;
16425 + //printk ("\n\n");
16426 +
16427 + if (rbuf_len < RBUF_LEN) {
16428 + rbuf_len += req->actual;
16429 + if (rbuf_len > RBUF_LEN) {
16430 + rbuf_len = RBUF_LEN;
16431 + }
16432 + }
16433 +
16434 + break;
16435 +
16436 + /* this endpoint is normally active while we're configured */
16437 + case -ECONNABORTED: /* hardware forced ep reset */
16438 + case -ECONNRESET: /* request dequeued */
16439 + case -ESHUTDOWN: /* disconnect from host */
16440 + VDBG (dev, "%s gone (%d), %d/%d\n", ep->name, status,
16441 + req->actual, req->length);
16442 + if (ep == dev->out_ep)
16443 + check_read_data (dev, ep, req);
16444 + free_ep_req (ep, req);
16445 + return;
16446 +
16447 + case -EOVERFLOW: /* buffer overrun on read means that
16448 + * we didn't provide a big enough
16449 + * buffer.
16450 + */
16451 + default:
16452 +#if 1
16453 + DBG (dev, "%s complete --> %d, %d/%d\n", ep->name,
16454 + status, req->actual, req->length);
16455 +#endif
16456 + case -EREMOTEIO: /* short read */
16457 + break;
16458 + }
16459 +
16460 + status = usb_ep_queue (ep, req, GFP_ATOMIC);
16461 + if (status) {
16462 + ERROR (dev, "kill %s: resubmit %d bytes --> %d\n",
16463 + ep->name, req->length, status);
16464 + usb_ep_set_halt (ep);
16465 + /* FIXME recover later ... somehow */
16466 + }
16467 +}
16468 +
16469 +static struct usb_request *
16470 +zero_start_isoc_ep (struct usb_ep *ep, int gfp_flags)
16471 +{
16472 + struct usb_request *req;
16473 + int status;
16474 +
16475 + req = alloc_ep_req (ep, 512);
16476 + if (!req)
16477 + return NULL;
16478 +
16479 + req->complete = zero_isoc_complete;
16480 +
16481 + status = usb_ep_queue (ep, req, gfp_flags);
16482 + if (status) {
16483 + struct zero_dev *dev = ep->driver_data;
16484 +
16485 + ERROR (dev, "start %s --> %d\n", ep->name, status);
16486 + free_ep_req (ep, req);
16487 + req = NULL;
16488 + }
16489 +
16490 + return req;
16491 +}
16492 +
16493 +/* change our operational config. this code must agree with the code
16494 + * that returns config descriptors, and altsetting code.
16495 + *
16496 + * it's also responsible for power management interactions. some
16497 + * configurations might not work with our current power sources.
16498 + *
16499 + * note that some device controller hardware will constrain what this
16500 + * code can do, perhaps by disallowing more than one configuration or
16501 + * by limiting configuration choices (like the pxa2xx).
16502 + */
16503 +static int
16504 +zero_set_config (struct zero_dev *dev, unsigned number, int gfp_flags)
16505 +{
16506 + int result = 0;
16507 + struct usb_gadget *gadget = dev->gadget;
16508 + const struct usb_endpoint_descriptor *d;
16509 + struct usb_ep *ep;
16510 +
16511 + if (number == dev->config)
16512 + return 0;
16513 +
16514 + zero_reset_config (dev);
16515 +
16516 + gadget_for_each_ep (ep, gadget) {
16517 +
16518 + if (strcmp (ep->name, "ep4") == 0) {
16519 +
16520 + d = (struct usb_endpoint_descripter *)&za_23; // isoc ep desc for audio i/f alt setting 6
16521 + result = usb_ep_enable (ep, d);
16522 +
16523 + if (result == 0) {
16524 + ep->driver_data = dev;
16525 + dev->in_ep = ep;
16526 +
16527 + if (zero_start_isoc_ep (ep, gfp_flags) != 0) {
16528 +
16529 + dev->in_ep = ep;
16530 + continue;
16531 + }
16532 +
16533 + usb_ep_disable (ep);
16534 + result = -EIO;
16535 + }
16536 + }
16537 +
16538 + }
16539 +
16540 + dev->config = number;
16541 + return result;
16542 +}
16543 +
16544 +/*-------------------------------------------------------------------------*/
16545 +
16546 +static void zero_setup_complete (struct usb_ep *ep, struct usb_request *req)
16547 +{
16548 + if (req->status || req->actual != req->length)
16549 + DBG ((struct zero_dev *) ep->driver_data,
16550 + "setup complete --> %d, %d/%d\n",
16551 + req->status, req->actual, req->length);
16552 +}
16553 +
16554 +/*
16555 + * The setup() callback implements all the ep0 functionality that's
16556 + * not handled lower down, in hardware or the hardware driver (like
16557 + * device and endpoint feature flags, and their status). It's all
16558 + * housekeeping for the gadget function we're implementing. Most of
16559 + * the work is in config-specific setup.
16560 + */
16561 +static int
16562 +zero_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
16563 +{
16564 + struct zero_dev *dev = get_gadget_data (gadget);
16565 + struct usb_request *req = dev->req;
16566 + int value = -EOPNOTSUPP;
16567 +
16568 + /* usually this stores reply data in the pre-allocated ep0 buffer,
16569 + * but config change events will reconfigure hardware.
16570 + */
16571 + req->zero = 0;
16572 + switch (ctrl->bRequest) {
16573 +
16574 + case USB_REQ_GET_DESCRIPTOR:
16575 +
16576 + switch (ctrl->wValue >> 8) {
16577 +
16578 + case USB_DT_DEVICE:
16579 + value = min (ctrl->wLength, (u16) sizeof device_desc);
16580 + memcpy (req->buf, &device_desc, value);
16581 + break;
16582 +#ifdef CONFIG_USB_GADGET_DUALSPEED
16583 + case USB_DT_DEVICE_QUALIFIER:
16584 + if (!gadget->is_dualspeed)
16585 + break;
16586 + value = min (ctrl->wLength, (u16) sizeof dev_qualifier);
16587 + memcpy (req->buf, &dev_qualifier, value);
16588 + break;
16589 +
16590 + case USB_DT_OTHER_SPEED_CONFIG:
16591 + if (!gadget->is_dualspeed)
16592 + break;
16593 + // FALLTHROUGH
16594 +#endif /* CONFIG_USB_GADGET_DUALSPEED */
16595 + case USB_DT_CONFIG:
16596 + value = config_buf (gadget, req->buf,
16597 + ctrl->wValue >> 8,
16598 + ctrl->wValue & 0xff);
16599 + if (value >= 0)
16600 + value = min (ctrl->wLength, (u16) value);
16601 + break;
16602 +
16603 + case USB_DT_STRING:
16604 + /* wIndex == language code.
16605 + * this driver only handles one language, you can
16606 + * add string tables for other languages, using
16607 + * any UTF-8 characters
16608 + */
16609 + value = usb_gadget_get_string (&stringtab,
16610 + ctrl->wValue & 0xff, req->buf);
16611 + if (value >= 0) {
16612 + value = min (ctrl->wLength, (u16) value);
16613 + }
16614 + break;
16615 + }
16616 + break;
16617 +
16618 + /* currently two configs, two speeds */
16619 + case USB_REQ_SET_CONFIGURATION:
16620 + if (ctrl->bRequestType != 0)
16621 + goto unknown;
16622 +
16623 + spin_lock (&dev->lock);
16624 + value = zero_set_config (dev, ctrl->wValue, GFP_ATOMIC);
16625 + spin_unlock (&dev->lock);
16626 + break;
16627 + case USB_REQ_GET_CONFIGURATION:
16628 + if (ctrl->bRequestType != USB_DIR_IN)
16629 + goto unknown;
16630 + *(u8 *)req->buf = dev->config;
16631 + value = min (ctrl->wLength, (u16) 1);
16632 + break;
16633 +
16634 + /* until we add altsetting support, or other interfaces,
16635 + * only 0/0 are possible. pxa2xx only supports 0/0 (poorly)
16636 + * and already killed pending endpoint I/O.
16637 + */
16638 + case USB_REQ_SET_INTERFACE:
16639 +
16640 + if (ctrl->bRequestType != USB_RECIP_INTERFACE)
16641 + goto unknown;
16642 + spin_lock (&dev->lock);
16643 + if (dev->config) {
16644 + u8 config = dev->config;
16645 +
16646 + /* resets interface configuration, forgets about
16647 + * previous transaction state (queued bufs, etc)
16648 + * and re-inits endpoint state (toggle etc)
16649 + * no response queued, just zero status == success.
16650 + * if we had more than one interface we couldn't
16651 + * use this "reset the config" shortcut.
16652 + */
16653 + zero_reset_config (dev);
16654 + zero_set_config (dev, config, GFP_ATOMIC);
16655 + value = 0;
16656 + }
16657 + spin_unlock (&dev->lock);
16658 + break;
16659 + case USB_REQ_GET_INTERFACE:
16660 + if ((ctrl->bRequestType == 0x21) && (ctrl->wIndex == 0x02)) {
16661 + value = ctrl->wLength;
16662 + break;
16663 + }
16664 + else {
16665 + if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
16666 + goto unknown;
16667 + if (!dev->config)
16668 + break;
16669 + if (ctrl->wIndex != 0) {
16670 + value = -EDOM;
16671 + break;
16672 + }
16673 + *(u8 *)req->buf = 0;
16674 + value = min (ctrl->wLength, (u16) 1);
16675 + }
16676 + break;
16677 +
16678 + /*
16679 + * These are the same vendor-specific requests supported by
16680 + * Intel's USB 2.0 compliance test devices. We exceed that
16681 + * device spec by allowing multiple-packet requests.
16682 + */
16683 + case 0x5b: /* control WRITE test -- fill the buffer */
16684 + if (ctrl->bRequestType != (USB_DIR_OUT|USB_TYPE_VENDOR))
16685 + goto unknown;
16686 + if (ctrl->wValue || ctrl->wIndex)
16687 + break;
16688 + /* just read that many bytes into the buffer */
16689 + if (ctrl->wLength > USB_BUFSIZ)
16690 + break;
16691 + value = ctrl->wLength;
16692 + break;
16693 + case 0x5c: /* control READ test -- return the buffer */
16694 + if (ctrl->bRequestType != (USB_DIR_IN|USB_TYPE_VENDOR))
16695 + goto unknown;
16696 + if (ctrl->wValue || ctrl->wIndex)
16697 + break;
16698 + /* expect those bytes are still in the buffer; send back */
16699 + if (ctrl->wLength > USB_BUFSIZ
16700 + || ctrl->wLength != req->length)
16701 + break;
16702 + value = ctrl->wLength;
16703 + break;
16704 +
16705 + case 0x01: // SET_CUR
16706 + case 0x02:
16707 + case 0x03:
16708 + case 0x04:
16709 + case 0x05:
16710 + value = ctrl->wLength;
16711 + break;
16712 + case 0x81:
16713 + switch (ctrl->wValue) {
16714 + case 0x0201:
16715 + case 0x0202:
16716 + ((u8*)req->buf)[0] = 0x00;
16717 + ((u8*)req->buf)[1] = 0xe3;
16718 + break;
16719 + case 0x0300:
16720 + case 0x0500:
16721 + ((u8*)req->buf)[0] = 0x00;
16722 + break;
16723 + }
16724 + //((u8*)req->buf)[0] = 0x81;
16725 + //((u8*)req->buf)[1] = 0x81;
16726 + value = ctrl->wLength;
16727 + break;
16728 + case 0x82:
16729 + switch (ctrl->wValue) {
16730 + case 0x0201:
16731 + case 0x0202:
16732 + ((u8*)req->buf)[0] = 0x00;
16733 + ((u8*)req->buf)[1] = 0xc3;
16734 + break;
16735 + case 0x0300:
16736 + case 0x0500:
16737 + ((u8*)req->buf)[0] = 0x00;
16738 + break;
16739 + }
16740 + //((u8*)req->buf)[0] = 0x82;
16741 + //((u8*)req->buf)[1] = 0x82;
16742 + value = ctrl->wLength;
16743 + break;
16744 + case 0x83:
16745 + switch (ctrl->wValue) {
16746 + case 0x0201:
16747 + case 0x0202:
16748 + ((u8*)req->buf)[0] = 0x00;
16749 + ((u8*)req->buf)[1] = 0x00;
16750 + break;
16751 + case 0x0300:
16752 + ((u8*)req->buf)[0] = 0x60;
16753 + break;
16754 + case 0x0500:
16755 + ((u8*)req->buf)[0] = 0x18;
16756 + break;
16757 + }
16758 + //((u8*)req->buf)[0] = 0x83;
16759 + //((u8*)req->buf)[1] = 0x83;
16760 + value = ctrl->wLength;
16761 + break;
16762 + case 0x84:
16763 + switch (ctrl->wValue) {
16764 + case 0x0201:
16765 + case 0x0202:
16766 + ((u8*)req->buf)[0] = 0x00;
16767 + ((u8*)req->buf)[1] = 0x01;
16768 + break;
16769 + case 0x0300:
16770 + case 0x0500:
16771 + ((u8*)req->buf)[0] = 0x08;
16772 + break;
16773 + }
16774 + //((u8*)req->buf)[0] = 0x84;
16775 + //((u8*)req->buf)[1] = 0x84;
16776 + value = ctrl->wLength;
16777 + break;
16778 + case 0x85:
16779 + ((u8*)req->buf)[0] = 0x85;
16780 + ((u8*)req->buf)[1] = 0x85;
16781 + value = ctrl->wLength;
16782 + break;
16783 +
16784 +
16785 + default:
16786 +unknown:
16787 + printk("unknown control req%02x.%02x v%04x i%04x l%d\n",
16788 + ctrl->bRequestType, ctrl->bRequest,
16789 + ctrl->wValue, ctrl->wIndex, ctrl->wLength);
16790 + }
16791 +
16792 + /* respond with data transfer before status phase? */
16793 + if (value >= 0) {
16794 + req->length = value;
16795 + req->zero = value < ctrl->wLength
16796 + && (value % gadget->ep0->maxpacket) == 0;
16797 + value = usb_ep_queue (gadget->ep0, req, GFP_ATOMIC);
16798 + if (value < 0) {
16799 + DBG (dev, "ep_queue < 0 --> %d\n", value);
16800 + req->status = 0;
16801 + zero_setup_complete (gadget->ep0, req);
16802 + }
16803 + }
16804 +
16805 + /* device either stalls (value < 0) or reports success */
16806 + return value;
16807 +}
16808 +
16809 +static void
16810 +zero_disconnect (struct usb_gadget *gadget)
16811 +{
16812 + struct zero_dev *dev = get_gadget_data (gadget);
16813 + unsigned long flags;
16814 +
16815 + spin_lock_irqsave (&dev->lock, flags);
16816 + zero_reset_config (dev);
16817 +
16818 + /* a more significant application might have some non-usb
16819 + * activities to quiesce here, saving resources like power
16820 + * or pushing the notification up a network stack.
16821 + */
16822 + spin_unlock_irqrestore (&dev->lock, flags);
16823 +
16824 + /* next we may get setup() calls to enumerate new connections;
16825 + * or an unbind() during shutdown (including removing module).
16826 + */
16827 +}
16828 +
16829 +static void
16830 +zero_autoresume (unsigned long _dev)
16831 +{
16832 + struct zero_dev *dev = (struct zero_dev *) _dev;
16833 + int status;
16834 +
16835 + /* normally the host would be woken up for something
16836 + * more significant than just a timer firing...
16837 + */
16838 + if (dev->gadget->speed != USB_SPEED_UNKNOWN) {
16839 + status = usb_gadget_wakeup (dev->gadget);
16840 + DBG (dev, "wakeup --> %d\n", status);
16841 + }
16842 +}
16843 +
16844 +/*-------------------------------------------------------------------------*/
16845 +
16846 +static void
16847 +zero_unbind (struct usb_gadget *gadget)
16848 +{
16849 + struct zero_dev *dev = get_gadget_data (gadget);
16850 +
16851 + DBG (dev, "unbind\n");
16852 +
16853 + /* we've already been disconnected ... no i/o is active */
16854 + if (dev->req)
16855 + free_ep_req (gadget->ep0, dev->req);
16856 + del_timer_sync (&dev->resume);
16857 + kfree (dev);
16858 + set_gadget_data (gadget, NULL);
16859 +}
16860 +
16861 +static int
16862 +zero_bind (struct usb_gadget *gadget)
16863 +{
16864 + struct zero_dev *dev;
16865 + //struct usb_ep *ep;
16866 +
16867 + printk("binding\n");
16868 + /*
16869 + * DRIVER POLICY CHOICE: you may want to do this differently.
16870 + * One thing to avoid is reusing a bcdDevice revision code
16871 + * with different host-visible configurations or behavior
16872 + * restrictions -- using ep1in/ep2out vs ep1out/ep3in, etc
16873 + */
16874 + //device_desc.bcdDevice = __constant_cpu_to_le16 (0x0201);
16875 +
16876 +
16877 + /* ok, we made sense of the hardware ... */
16878 + dev = kzalloc (sizeof *dev, SLAB_KERNEL);
16879 + if (!dev)
16880 + return -ENOMEM;
16881 + spin_lock_init (&dev->lock);
16882 + dev->gadget = gadget;
16883 + set_gadget_data (gadget, dev);
16884 +
16885 + /* preallocate control response and buffer */
16886 + dev->req = usb_ep_alloc_request (gadget->ep0, GFP_KERNEL);
16887 + if (!dev->req)
16888 + goto enomem;
16889 + dev->req->buf = usb_ep_alloc_buffer (gadget->ep0, USB_BUFSIZ,
16890 + &dev->req->dma, GFP_KERNEL);
16891 + if (!dev->req->buf)
16892 + goto enomem;
16893 +
16894 + dev->req->complete = zero_setup_complete;
16895 +
16896 + device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
16897 +
16898 +#ifdef CONFIG_USB_GADGET_DUALSPEED
16899 + /* assume ep0 uses the same value for both speeds ... */
16900 + dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
16901 +
16902 + /* and that all endpoints are dual-speed */
16903 + //hs_source_desc.bEndpointAddress = fs_source_desc.bEndpointAddress;
16904 + //hs_sink_desc.bEndpointAddress = fs_sink_desc.bEndpointAddress;
16905 +#endif
16906 +
16907 + usb_gadget_set_selfpowered (gadget);
16908 +
16909 + init_timer (&dev->resume);
16910 + dev->resume.function = zero_autoresume;
16911 + dev->resume.data = (unsigned long) dev;
16912 +
16913 + gadget->ep0->driver_data = dev;
16914 +
16915 + INFO (dev, "%s, version: " DRIVER_VERSION "\n", longname);
16916 + INFO (dev, "using %s, OUT %s IN %s\n", gadget->name,
16917 + EP_OUT_NAME, EP_IN_NAME);
16918 +
16919 + snprintf (manufacturer, sizeof manufacturer,
16920 + UTS_SYSNAME " " UTS_RELEASE " with %s",
16921 + gadget->name);
16922 +
16923 + return 0;
16924 +
16925 +enomem:
16926 + zero_unbind (gadget);
16927 + return -ENOMEM;
16928 +}
16929 +
16930 +/*-------------------------------------------------------------------------*/
16931 +
16932 +static void
16933 +zero_suspend (struct usb_gadget *gadget)
16934 +{
16935 + struct zero_dev *dev = get_gadget_data (gadget);
16936 +
16937 + if (gadget->speed == USB_SPEED_UNKNOWN)
16938 + return;
16939 +
16940 + if (autoresume) {
16941 + mod_timer (&dev->resume, jiffies + (HZ * autoresume));
16942 + DBG (dev, "suspend, wakeup in %d seconds\n", autoresume);
16943 + } else
16944 + DBG (dev, "suspend\n");
16945 +}
16946 +
16947 +static void
16948 +zero_resume (struct usb_gadget *gadget)
16949 +{
16950 + struct zero_dev *dev = get_gadget_data (gadget);
16951 +
16952 + DBG (dev, "resume\n");
16953 + del_timer (&dev->resume);
16954 +}
16955 +
16956 +
16957 +/*-------------------------------------------------------------------------*/
16958 +
16959 +static struct usb_gadget_driver zero_driver = {
16960 +#ifdef CONFIG_USB_GADGET_DUALSPEED
16961 + .speed = USB_SPEED_HIGH,
16962 +#else
16963 + .speed = USB_SPEED_FULL,
16964 +#endif
16965 + .function = (char *) longname,
16966 + .bind = zero_bind,
16967 + .unbind = zero_unbind,
16968 +
16969 + .setup = zero_setup,
16970 + .disconnect = zero_disconnect,
16971 +
16972 + .suspend = zero_suspend,
16973 + .resume = zero_resume,
16974 +
16975 + .driver = {
16976 + .name = (char *) shortname,
16977 + // .shutdown = ...
16978 + // .suspend = ...
16979 + // .resume = ...
16980 + },
16981 +};
16982 +
16983 +MODULE_AUTHOR ("David Brownell");
16984 +MODULE_LICENSE ("Dual BSD/GPL");
16985 +
16986 +static struct proc_dir_entry *pdir, *pfile;
16987 +
16988 +static int isoc_read_data (char *page, char **start,
16989 + off_t off, int count,
16990 + int *eof, void *data)
16991 +{
16992 + int i;
16993 + static int c = 0;
16994 + static int done = 0;
16995 + static int s = 0;
16996 +
16997 +/*
16998 + printk ("\ncount: %d\n", count);
16999 + printk ("rbuf_start: %d\n", rbuf_start);
17000 + printk ("rbuf_len: %d\n", rbuf_len);
17001 + printk ("off: %d\n", off);
17002 + printk ("start: %p\n\n", *start);
17003 +*/
17004 + if (done) {
17005 + c = 0;
17006 + done = 0;
17007 + *eof = 1;
17008 + return 0;
17009 + }
17010 +
17011 + if (c == 0) {
17012 + if (rbuf_len == RBUF_LEN)
17013 + s = rbuf_start;
17014 + else s = 0;
17015 + }
17016 +
17017 + for (i=0; i<count && c<rbuf_len; i++, c++) {
17018 + page[i] = rbuf[(c+s) % RBUF_LEN];
17019 + }
17020 + *start = page;
17021 +
17022 + if (c >= rbuf_len) {
17023 + *eof = 1;
17024 + done = 1;
17025 + }
17026 +
17027 +
17028 + return i;
17029 +}
17030 +
17031 +static int __init init (void)
17032 +{
17033 +
17034 + int retval = 0;
17035 +
17036 + pdir = proc_mkdir("isoc_test", NULL);
17037 + if(pdir == NULL) {
17038 + retval = -ENOMEM;
17039 + printk("Error creating dir\n");
17040 + goto done;
17041 + }
17042 + pdir->owner = THIS_MODULE;
17043 +
17044 + pfile = create_proc_read_entry("isoc_data",
17045 + 0444, pdir,
17046 + isoc_read_data,
17047 + NULL);
17048 + if (pfile == NULL) {
17049 + retval = -ENOMEM;
17050 + printk("Error creating file\n");
17051 + goto no_file;
17052 + }
17053 + pfile->owner = THIS_MODULE;
17054 +
17055 + return usb_gadget_register_driver (&zero_driver);
17056 +
17057 + no_file:
17058 + remove_proc_entry("isoc_data", NULL);
17059 + done:
17060 + return retval;
17061 +}
17062 +module_init (init);
17063 +
17064 +static void __exit cleanup (void)
17065 +{
17066 +
17067 + usb_gadget_unregister_driver (&zero_driver);
17068 +
17069 + remove_proc_entry("isoc_data", pdir);
17070 + remove_proc_entry("isoc_test", NULL);
17071 +}
17072 +module_exit (cleanup);
17073 --- /dev/null
17074 +++ b/drivers/usb/host/dwc_otg/dwc_cfi_common.h
17075 @@ -0,0 +1,142 @@
17076 +/* ==========================================================================
17077 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
17078 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
17079 + * otherwise expressly agreed to in writing between Synopsys and you.
17080 + *
17081 + * The Software IS NOT an item of Licensed Software or Licensed Product under
17082 + * any End User Software License Agreement or Agreement for Licensed Product
17083 + * with Synopsys or any supplement thereto. You are permitted to use and
17084 + * redistribute this Software in source and binary forms, with or without
17085 + * modification, provided that redistributions of source code must retain this
17086 + * notice. You may not view, use, disclose, copy or distribute this file or
17087 + * any information contained herein except pursuant to this license grant from
17088 + * Synopsys. If you do not agree with this notice, including the disclaimer
17089 + * below, then you are not authorized to use the Software.
17090 + *
17091 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
17092 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17093 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17094 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
17095 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
17096 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
17097 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
17098 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
17099 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
17100 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
17101 + * DAMAGE.
17102 + * ========================================================================== */
17103 +
17104 +#if !defined(__DWC_CFI_COMMON_H__)
17105 +#define __DWC_CFI_COMMON_H__
17106 +
17107 +//#include <linux/types.h>
17108 +
17109 +/**
17110 + * @file
17111 + *
17112 + * This file contains the CFI specific common constants, interfaces
17113 + * (functions and macros) and structures for Linux. No PCD specific
17114 + * data structure or definition is to be included in this file.
17115 + *
17116 + */
17117 +
17118 +/** This is a request for all Core Features */
17119 +#define VEN_CORE_GET_FEATURES 0xB1
17120 +
17121 +/** This is a request to get the value of a specific Core Feature */
17122 +#define VEN_CORE_GET_FEATURE 0xB2
17123 +
17124 +/** This command allows the host to set the value of a specific Core Feature */
17125 +#define VEN_CORE_SET_FEATURE 0xB3
17126 +
17127 +/** This command allows the host to set the default values of
17128 + * either all or any specific Core Feature
17129 + */
17130 +#define VEN_CORE_RESET_FEATURES 0xB4
17131 +
17132 +/** This command forces the PCD to write the deferred values of a Core Features */
17133 +#define VEN_CORE_ACTIVATE_FEATURES 0xB5
17134 +
17135 +/** This request reads a DWORD value from a register at the specified offset */
17136 +#define VEN_CORE_READ_REGISTER 0xB6
17137 +
17138 +/** This request writes a DWORD value into a register at the specified offset */
17139 +#define VEN_CORE_WRITE_REGISTER 0xB7
17140 +
17141 +/** This structure is the header of the Core Features dataset returned to
17142 + * the Host
17143 + */
17144 +struct cfi_all_features_header {
17145 +/** The features header structure length is */
17146 +#define CFI_ALL_FEATURES_HDR_LEN 8
17147 + /**
17148 + * The total length of the features dataset returned to the Host
17149 + */
17150 + uint16_t wTotalLen;
17151 +
17152 + /**
17153 + * CFI version number inBinary-Coded Decimal (i.e., 1.00 is 100H).
17154 + * This field identifies the version of the CFI Specification with which
17155 + * the device is compliant.
17156 + */
17157 + uint16_t wVersion;
17158 +
17159 + /** The ID of the Core */
17160 + uint16_t wCoreID;
17161 +#define CFI_CORE_ID_UDC 1
17162 +#define CFI_CORE_ID_OTG 2
17163 +#define CFI_CORE_ID_WUDEV 3
17164 +
17165 + /** Number of features returned by VEN_CORE_GET_FEATURES request */
17166 + uint16_t wNumFeatures;
17167 +} UPACKED;
17168 +
17169 +typedef struct cfi_all_features_header cfi_all_features_header_t;
17170 +
17171 +/** This structure is a header of the Core Feature descriptor dataset returned to
17172 + * the Host after the VEN_CORE_GET_FEATURES request
17173 + */
17174 +struct cfi_feature_desc_header {
17175 +#define CFI_FEATURE_DESC_HDR_LEN 8
17176 +
17177 + /** The feature ID */
17178 + uint16_t wFeatureID;
17179 +
17180 + /** Length of this feature descriptor in bytes - including the
17181 + * length of the feature name string
17182 + */
17183 + uint16_t wLength;
17184 +
17185 + /** The data length of this feature in bytes */
17186 + uint16_t wDataLength;
17187 +
17188 + /**
17189 + * Attributes of this features
17190 + * D0: Access rights
17191 + * 0 - Read/Write
17192 + * 1 - Read only
17193 + */
17194 + uint8_t bmAttributes;
17195 +#define CFI_FEATURE_ATTR_RO 1
17196 +#define CFI_FEATURE_ATTR_RW 0
17197 +
17198 + /** Length of the feature name in bytes */
17199 + uint8_t bNameLen;
17200 +
17201 + /** The feature name buffer */
17202 + //uint8_t *name;
17203 +} UPACKED;
17204 +
17205 +typedef struct cfi_feature_desc_header cfi_feature_desc_header_t;
17206 +
17207 +/**
17208 + * This structure describes a NULL terminated string referenced by its id field.
17209 + * It is very similar to usb_string structure but has the id field type set to 16-bit.
17210 + */
17211 +struct cfi_string {
17212 + uint16_t id;
17213 + const uint8_t *s;
17214 +};
17215 +typedef struct cfi_string cfi_string_t;
17216 +
17217 +#endif
17218 --- /dev/null
17219 +++ b/drivers/usb/host/dwc_otg/dwc_otg_adp.c
17220 @@ -0,0 +1,854 @@
17221 +/* ==========================================================================
17222 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_adp.c $
17223 + * $Revision: #12 $
17224 + * $Date: 2011/10/26 $
17225 + * $Change: 1873028 $
17226 + *
17227 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
17228 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
17229 + * otherwise expressly agreed to in writing between Synopsys and you.
17230 + *
17231 + * The Software IS NOT an item of Licensed Software or Licensed Product under
17232 + * any End User Software License Agreement or Agreement for Licensed Product
17233 + * with Synopsys or any supplement thereto. You are permitted to use and
17234 + * redistribute this Software in source and binary forms, with or without
17235 + * modification, provided that redistributions of source code must retain this
17236 + * notice. You may not view, use, disclose, copy or distribute this file or
17237 + * any information contained herein except pursuant to this license grant from
17238 + * Synopsys. If you do not agree with this notice, including the disclaimer
17239 + * below, then you are not authorized to use the Software.
17240 + *
17241 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
17242 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17243 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17244 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
17245 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
17246 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
17247 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
17248 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
17249 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
17250 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
17251 + * DAMAGE.
17252 + * ========================================================================== */
17253 +
17254 +#include "dwc_os.h"
17255 +#include "dwc_otg_regs.h"
17256 +#include "dwc_otg_cil.h"
17257 +#include "dwc_otg_adp.h"
17258 +
17259 +/** @file
17260 + *
17261 + * This file contains the most of the Attach Detect Protocol implementation for
17262 + * the driver to support OTG Rev2.0.
17263 + *
17264 + */
17265 +
17266 +void dwc_otg_adp_write_reg(dwc_otg_core_if_t * core_if, uint32_t value)
17267 +{
17268 + adpctl_data_t adpctl;
17269 +
17270 + adpctl.d32 = value;
17271 + adpctl.b.ar = 0x2;
17272 +
17273 + DWC_WRITE_REG32(&core_if->core_global_regs->adpctl, adpctl.d32);
17274 +
17275 + while (adpctl.b.ar) {
17276 + adpctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->adpctl);
17277 + }
17278 +
17279 +}
17280 +
17281 +/**
17282 + * Function is called to read ADP registers
17283 + */
17284 +uint32_t dwc_otg_adp_read_reg(dwc_otg_core_if_t * core_if)
17285 +{
17286 + adpctl_data_t adpctl;
17287 +
17288 + adpctl.d32 = 0;
17289 + adpctl.b.ar = 0x1;
17290 +
17291 + DWC_WRITE_REG32(&core_if->core_global_regs->adpctl, adpctl.d32);
17292 +
17293 + while (adpctl.b.ar) {
17294 + adpctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->adpctl);
17295 + }
17296 +
17297 + return adpctl.d32;
17298 +}
17299 +
17300 +/**
17301 + * Function is called to read ADPCTL register and filter Write-clear bits
17302 + */
17303 +uint32_t dwc_otg_adp_read_reg_filter(dwc_otg_core_if_t * core_if)
17304 +{
17305 + adpctl_data_t adpctl;
17306 +
17307 + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
17308 + adpctl.b.adp_tmout_int = 0;
17309 + adpctl.b.adp_prb_int = 0;
17310 + adpctl.b.adp_tmout_int = 0;
17311 +
17312 + return adpctl.d32;
17313 +}
17314 +
17315 +/**
17316 + * Function is called to write ADP registers
17317 + */
17318 +void dwc_otg_adp_modify_reg(dwc_otg_core_if_t * core_if, uint32_t clr,
17319 + uint32_t set)
17320 +{
17321 + dwc_otg_adp_write_reg(core_if,
17322 + (dwc_otg_adp_read_reg(core_if) & (~clr)) | set);
17323 +}
17324 +
17325 +static void adp_sense_timeout(void *ptr)
17326 +{
17327 + dwc_otg_core_if_t *core_if = (dwc_otg_core_if_t *) ptr;
17328 + core_if->adp.sense_timer_started = 0;
17329 + DWC_PRINTF("ADP SENSE TIMEOUT\n");
17330 + if (core_if->adp_enable) {
17331 + dwc_otg_adp_sense_stop(core_if);
17332 + dwc_otg_adp_probe_start(core_if);
17333 + }
17334 +}
17335 +
17336 +/**
17337 + * This function is called when the ADP vbus timer expires. Timeout is 1.1s.
17338 + */
17339 +static void adp_vbuson_timeout(void *ptr)
17340 +{
17341 + gpwrdn_data_t gpwrdn;
17342 + dwc_otg_core_if_t *core_if = (dwc_otg_core_if_t *) ptr;
17343 + hprt0_data_t hprt0 = {.d32 = 0 };
17344 + pcgcctl_data_t pcgcctl = {.d32 = 0 };
17345 + DWC_PRINTF("%s: 1.1 seconds expire after turning on VBUS\n",__FUNCTION__);
17346 + if (core_if) {
17347 + core_if->adp.vbuson_timer_started = 0;
17348 + /* Turn off vbus */
17349 + hprt0.b.prtpwr = 1;
17350 + DWC_MODIFY_REG32(core_if->host_if->hprt0, hprt0.d32, 0);
17351 + gpwrdn.d32 = 0;
17352 +
17353 + /* Power off the core */
17354 + if (core_if->power_down == 2) {
17355 + /* Enable Wakeup Logic */
17356 +// gpwrdn.b.wkupactiv = 1;
17357 + gpwrdn.b.pmuactv = 0;
17358 + gpwrdn.b.pwrdnrstn = 1;
17359 + gpwrdn.b.pwrdnclmp = 1;
17360 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0,
17361 + gpwrdn.d32);
17362 +
17363 + /* Suspend the Phy Clock */
17364 + pcgcctl.b.stoppclk = 1;
17365 + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
17366 +
17367 + /* Switch on VDD */
17368 +// gpwrdn.b.wkupactiv = 1;
17369 + gpwrdn.b.pmuactv = 1;
17370 + gpwrdn.b.pwrdnrstn = 1;
17371 + gpwrdn.b.pwrdnclmp = 1;
17372 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0,
17373 + gpwrdn.d32);
17374 + } else {
17375 + /* Enable Power Down Logic */
17376 + gpwrdn.b.pmuintsel = 1;
17377 + gpwrdn.b.pmuactv = 1;
17378 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
17379 + }
17380 +
17381 + /* Power off the core */
17382 + if (core_if->power_down == 2) {
17383 + gpwrdn.d32 = 0;
17384 + gpwrdn.b.pwrdnswtch = 1;
17385 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn,
17386 + gpwrdn.d32, 0);
17387 + }
17388 +
17389 + /* Unmask SRP detected interrupt from Power Down Logic */
17390 + gpwrdn.d32 = 0;
17391 + gpwrdn.b.srp_det_msk = 1;
17392 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
17393 +
17394 + dwc_otg_adp_probe_start(core_if);
17395 + dwc_otg_dump_global_registers(core_if);
17396 + dwc_otg_dump_host_registers(core_if);
17397 + }
17398 +
17399 +}
17400 +
17401 +/**
17402 + * Start the ADP Initial Probe timer to detect if Port Connected interrupt is
17403 + * not asserted within 1.1 seconds.
17404 + *
17405 + * @param core_if the pointer to core_if strucure.
17406 + */
17407 +void dwc_otg_adp_vbuson_timer_start(dwc_otg_core_if_t * core_if)
17408 +{
17409 + core_if->adp.vbuson_timer_started = 1;
17410 + if (core_if->adp.vbuson_timer)
17411 + {
17412 + DWC_PRINTF("SCHEDULING VBUSON TIMER\n");
17413 + /* 1.1 secs + 60ms necessary for cil_hcd_start*/
17414 + DWC_TIMER_SCHEDULE(core_if->adp.vbuson_timer, 1160);
17415 + } else {
17416 + DWC_WARN("VBUSON_TIMER = %p\n",core_if->adp.vbuson_timer);
17417 + }
17418 +}
17419 +
17420 +#if 0
17421 +/**
17422 + * Masks all DWC OTG core interrupts
17423 + *
17424 + */
17425 +static void mask_all_interrupts(dwc_otg_core_if_t * core_if)
17426 +{
17427 + int i;
17428 + gahbcfg_data_t ahbcfg = {.d32 = 0 };
17429 +
17430 + /* Mask Host Interrupts */
17431 +
17432 + /* Clear and disable HCINTs */
17433 + for (i = 0; i < core_if->core_params->host_channels; i++) {
17434 + DWC_WRITE_REG32(&core_if->host_if->hc_regs[i]->hcintmsk, 0);
17435 + DWC_WRITE_REG32(&core_if->host_if->hc_regs[i]->hcint, 0xFFFFFFFF);
17436 +
17437 + }
17438 +
17439 + /* Clear and disable HAINT */
17440 + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->haintmsk, 0x0000);
17441 + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->haint, 0xFFFFFFFF);
17442 +
17443 + /* Mask Device Interrupts */
17444 + if (!core_if->multiproc_int_enable) {
17445 + /* Clear and disable IN Endpoint interrupts */
17446 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->diepmsk, 0);
17447 + for (i = 0; i <= core_if->dev_if->num_in_eps; i++) {
17448 + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[i]->
17449 + diepint, 0xFFFFFFFF);
17450 + }
17451 +
17452 + /* Clear and disable OUT Endpoint interrupts */
17453 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->doepmsk, 0);
17454 + for (i = 0; i <= core_if->dev_if->num_out_eps; i++) {
17455 + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[i]->
17456 + doepint, 0xFFFFFFFF);
17457 + }
17458 +
17459 + /* Clear and disable DAINT */
17460 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->daint,
17461 + 0xFFFFFFFF);
17462 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->daintmsk, 0);
17463 + } else {
17464 + for (i = 0; i < core_if->dev_if->num_in_eps; ++i) {
17465 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->
17466 + diepeachintmsk[i], 0);
17467 + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[i]->
17468 + diepint, 0xFFFFFFFF);
17469 + }
17470 +
17471 + for (i = 0; i < core_if->dev_if->num_out_eps; ++i) {
17472 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->
17473 + doepeachintmsk[i], 0);
17474 + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[i]->
17475 + doepint, 0xFFFFFFFF);
17476 + }
17477 +
17478 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->deachintmsk,
17479 + 0);
17480 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->deachint,
17481 + 0xFFFFFFFF);
17482 +
17483 + }
17484 +
17485 + /* Disable interrupts */
17486 + ahbcfg.b.glblintrmsk = 1;
17487 + DWC_MODIFY_REG32(&core_if->core_global_regs->gahbcfg, ahbcfg.d32, 0);
17488 +
17489 + /* Disable all interrupts. */
17490 + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, 0);
17491 +
17492 + /* Clear any pending interrupts */
17493 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
17494 +
17495 + /* Clear any pending OTG Interrupts */
17496 + DWC_WRITE_REG32(&core_if->core_global_regs->gotgint, 0xFFFFFFFF);
17497 +}
17498 +
17499 +/**
17500 + * Unmask Port Connection Detected interrupt
17501 + *
17502 + */
17503 +static void unmask_conn_det_intr(dwc_otg_core_if_t * core_if)
17504 +{
17505 + gintmsk_data_t gintmsk = {.d32 = 0,.b.portintr = 1 };
17506 +
17507 + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, gintmsk.d32);
17508 +}
17509 +#endif
17510 +
17511 +/**
17512 + * Starts the ADP Probing
17513 + *
17514 + * @param core_if the pointer to core_if structure.
17515 + */
17516 +uint32_t dwc_otg_adp_probe_start(dwc_otg_core_if_t * core_if)
17517 +{
17518 +
17519 + adpctl_data_t adpctl = {.d32 = 0};
17520 + gpwrdn_data_t gpwrdn;
17521 +#if 0
17522 + adpctl_data_t adpctl_int = {.d32 = 0, .b.adp_prb_int = 1,
17523 + .b.adp_sns_int = 1, b.adp_tmout_int};
17524 +#endif
17525 + dwc_otg_disable_global_interrupts(core_if);
17526 + DWC_PRINTF("ADP Probe Start\n");
17527 + core_if->adp.probe_enabled = 1;
17528 +
17529 + adpctl.b.adpres = 1;
17530 + dwc_otg_adp_write_reg(core_if, adpctl.d32);
17531 +
17532 + while (adpctl.b.adpres) {
17533 + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
17534 + }
17535 +
17536 + adpctl.d32 = 0;
17537 + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
17538 +
17539 + /* In Host mode unmask SRP detected interrupt */
17540 + gpwrdn.d32 = 0;
17541 + gpwrdn.b.sts_chngint_msk = 1;
17542 + if (!gpwrdn.b.idsts) {
17543 + gpwrdn.b.srp_det_msk = 1;
17544 + }
17545 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
17546 +
17547 + adpctl.b.adp_tmout_int_msk = 1;
17548 + adpctl.b.adp_prb_int_msk = 1;
17549 + adpctl.b.prb_dschg = 1;
17550 + adpctl.b.prb_delta = 1;
17551 + adpctl.b.prb_per = 1;
17552 + adpctl.b.adpen = 1;
17553 + adpctl.b.enaprb = 1;
17554 +
17555 + dwc_otg_adp_write_reg(core_if, adpctl.d32);
17556 + DWC_PRINTF("ADP Probe Finish\n");
17557 + return 0;
17558 +}
17559 +
17560 +/**
17561 + * Starts the ADP Sense timer to detect if ADP Sense interrupt is not asserted
17562 + * within 3 seconds.
17563 + *
17564 + * @param core_if the pointer to core_if strucure.
17565 + */
17566 +void dwc_otg_adp_sense_timer_start(dwc_otg_core_if_t * core_if)
17567 +{
17568 + core_if->adp.sense_timer_started = 1;
17569 + DWC_TIMER_SCHEDULE(core_if->adp.sense_timer, 3000 /* 3 secs */ );
17570 +}
17571 +
17572 +/**
17573 + * Starts the ADP Sense
17574 + *
17575 + * @param core_if the pointer to core_if strucure.
17576 + */
17577 +uint32_t dwc_otg_adp_sense_start(dwc_otg_core_if_t * core_if)
17578 +{
17579 + adpctl_data_t adpctl;
17580 +
17581 + DWC_PRINTF("ADP Sense Start\n");
17582 +
17583 + /* Unmask ADP sense interrupt and mask all other from the core */
17584 + adpctl.d32 = dwc_otg_adp_read_reg_filter(core_if);
17585 + adpctl.b.adp_sns_int_msk = 1;
17586 + dwc_otg_adp_write_reg(core_if, adpctl.d32);
17587 + dwc_otg_disable_global_interrupts(core_if); // vahrama
17588 +
17589 + /* Set ADP reset bit*/
17590 + adpctl.d32 = dwc_otg_adp_read_reg_filter(core_if);
17591 + adpctl.b.adpres = 1;
17592 + dwc_otg_adp_write_reg(core_if, adpctl.d32);
17593 +
17594 + while (adpctl.b.adpres) {
17595 + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
17596 + }
17597 +
17598 + adpctl.b.adpres = 0;
17599 + adpctl.b.adpen = 1;
17600 + adpctl.b.enasns = 1;
17601 + dwc_otg_adp_write_reg(core_if, adpctl.d32);
17602 +
17603 + dwc_otg_adp_sense_timer_start(core_if);
17604 +
17605 + return 0;
17606 +}
17607 +
17608 +/**
17609 + * Stops the ADP Probing
17610 + *
17611 + * @param core_if the pointer to core_if strucure.
17612 + */
17613 +uint32_t dwc_otg_adp_probe_stop(dwc_otg_core_if_t * core_if)
17614 +{
17615 +
17616 + adpctl_data_t adpctl;
17617 + DWC_PRINTF("Stop ADP probe\n");
17618 + core_if->adp.probe_enabled = 0;
17619 + core_if->adp.probe_counter = 0;
17620 + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
17621 +
17622 + adpctl.b.adpen = 0;
17623 + adpctl.b.adp_prb_int = 1;
17624 + adpctl.b.adp_tmout_int = 1;
17625 + adpctl.b.adp_sns_int = 1;
17626 + dwc_otg_adp_write_reg(core_if, adpctl.d32);
17627 +
17628 + return 0;
17629 +}
17630 +
17631 +/**
17632 + * Stops the ADP Sensing
17633 + *
17634 + * @param core_if the pointer to core_if strucure.
17635 + */
17636 +uint32_t dwc_otg_adp_sense_stop(dwc_otg_core_if_t * core_if)
17637 +{
17638 + adpctl_data_t adpctl;
17639 +
17640 + core_if->adp.sense_enabled = 0;
17641 +
17642 + adpctl.d32 = dwc_otg_adp_read_reg_filter(core_if);
17643 + adpctl.b.enasns = 0;
17644 + adpctl.b.adp_sns_int = 1;
17645 + dwc_otg_adp_write_reg(core_if, adpctl.d32);
17646 +
17647 + return 0;
17648 +}
17649 +
17650 +/**
17651 + * Called to turn on the VBUS after initial ADP probe in host mode.
17652 + * If port power was already enabled in cil_hcd_start function then
17653 + * only schedule a timer.
17654 + *
17655 + * @param core_if the pointer to core_if structure.
17656 + */
17657 +void dwc_otg_adp_turnon_vbus(dwc_otg_core_if_t * core_if)
17658 +{
17659 + hprt0_data_t hprt0 = {.d32 = 0 };
17660 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
17661 + DWC_PRINTF("Turn on VBUS for 1.1s, port power is %d\n", hprt0.b.prtpwr);
17662 +
17663 + if (hprt0.b.prtpwr == 0) {
17664 + hprt0.b.prtpwr = 1;
17665 + //DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
17666 + }
17667 +
17668 + dwc_otg_adp_vbuson_timer_start(core_if);
17669 +}
17670 +
17671 +/**
17672 + * Called right after driver is loaded
17673 + * to perform initial actions for ADP
17674 + *
17675 + * @param core_if the pointer to core_if structure.
17676 + * @param is_host - flag for current mode of operation either from GINTSTS or GPWRDN
17677 + */
17678 +void dwc_otg_adp_start(dwc_otg_core_if_t * core_if, uint8_t is_host)
17679 +{
17680 + gpwrdn_data_t gpwrdn;
17681 +
17682 + DWC_PRINTF("ADP Initial Start\n");
17683 + core_if->adp.adp_started = 1;
17684 +
17685 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
17686 + dwc_otg_disable_global_interrupts(core_if);
17687 + if (is_host) {
17688 + DWC_PRINTF("HOST MODE\n");
17689 + /* Enable Power Down Logic Interrupt*/
17690 + gpwrdn.d32 = 0;
17691 + gpwrdn.b.pmuintsel = 1;
17692 + gpwrdn.b.pmuactv = 1;
17693 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
17694 + /* Initialize first ADP probe to obtain Ramp Time value */
17695 + core_if->adp.initial_probe = 1;
17696 + dwc_otg_adp_probe_start(core_if);
17697 + } else {
17698 + gotgctl_data_t gotgctl;
17699 + gotgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
17700 + DWC_PRINTF("DEVICE MODE\n");
17701 + if (gotgctl.b.bsesvld == 0) {
17702 + /* Enable Power Down Logic Interrupt*/
17703 + gpwrdn.d32 = 0;
17704 + DWC_PRINTF("VBUS is not valid - start ADP probe\n");
17705 + gpwrdn.b.pmuintsel = 1;
17706 + gpwrdn.b.pmuactv = 1;
17707 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
17708 + core_if->adp.initial_probe = 1;
17709 + dwc_otg_adp_probe_start(core_if);
17710 + } else {
17711 + DWC_PRINTF("VBUS is valid - initialize core as a Device\n");
17712 + core_if->op_state = B_PERIPHERAL;
17713 + dwc_otg_core_init(core_if);
17714 + dwc_otg_enable_global_interrupts(core_if);
17715 + cil_pcd_start(core_if);
17716 + dwc_otg_dump_global_registers(core_if);
17717 + dwc_otg_dump_dev_registers(core_if);
17718 + }
17719 + }
17720 +}
17721 +
17722 +void dwc_otg_adp_init(dwc_otg_core_if_t * core_if)
17723 +{
17724 + core_if->adp.adp_started = 0;
17725 + core_if->adp.initial_probe = 0;
17726 + core_if->adp.probe_timer_values[0] = -1;
17727 + core_if->adp.probe_timer_values[1] = -1;
17728 + core_if->adp.probe_enabled = 0;
17729 + core_if->adp.sense_enabled = 0;
17730 + core_if->adp.sense_timer_started = 0;
17731 + core_if->adp.vbuson_timer_started = 0;
17732 + core_if->adp.probe_counter = 0;
17733 + core_if->adp.gpwrdn = 0;
17734 + core_if->adp.attached = DWC_OTG_ADP_UNKOWN;
17735 + /* Initialize timers */
17736 + core_if->adp.sense_timer =
17737 + DWC_TIMER_ALLOC("ADP SENSE TIMER", adp_sense_timeout, core_if);
17738 + core_if->adp.vbuson_timer =
17739 + DWC_TIMER_ALLOC("ADP VBUS ON TIMER", adp_vbuson_timeout, core_if);
17740 + if (!core_if->adp.sense_timer || !core_if->adp.vbuson_timer)
17741 + {
17742 + DWC_ERROR("Could not allocate memory for ADP timers\n");
17743 + }
17744 +}
17745 +
17746 +void dwc_otg_adp_remove(dwc_otg_core_if_t * core_if)
17747 +{
17748 + gpwrdn_data_t gpwrdn = { .d32 = 0 };
17749 + gpwrdn.b.pmuintsel = 1;
17750 + gpwrdn.b.pmuactv = 1;
17751 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
17752 +
17753 + if (core_if->adp.probe_enabled)
17754 + dwc_otg_adp_probe_stop(core_if);
17755 + if (core_if->adp.sense_enabled)
17756 + dwc_otg_adp_sense_stop(core_if);
17757 + if (core_if->adp.sense_timer_started)
17758 + DWC_TIMER_CANCEL(core_if->adp.sense_timer);
17759 + if (core_if->adp.vbuson_timer_started)
17760 + DWC_TIMER_CANCEL(core_if->adp.vbuson_timer);
17761 + DWC_TIMER_FREE(core_if->adp.sense_timer);
17762 + DWC_TIMER_FREE(core_if->adp.vbuson_timer);
17763 +}
17764 +
17765 +/////////////////////////////////////////////////////////////////////
17766 +////////////// ADP Interrupt Handlers ///////////////////////////////
17767 +/////////////////////////////////////////////////////////////////////
17768 +/**
17769 + * This function sets Ramp Timer values
17770 + */
17771 +static uint32_t set_timer_value(dwc_otg_core_if_t * core_if, uint32_t val)
17772 +{
17773 + if (core_if->adp.probe_timer_values[0] == -1) {
17774 + core_if->adp.probe_timer_values[0] = val;
17775 + core_if->adp.probe_timer_values[1] = -1;
17776 + return 1;
17777 + } else {
17778 + core_if->adp.probe_timer_values[1] =
17779 + core_if->adp.probe_timer_values[0];
17780 + core_if->adp.probe_timer_values[0] = val;
17781 + return 0;
17782 + }
17783 +}
17784 +
17785 +/**
17786 + * This function compares Ramp Timer values
17787 + */
17788 +static uint32_t compare_timer_values(dwc_otg_core_if_t * core_if)
17789 +{
17790 + uint32_t diff;
17791 + if (core_if->adp.probe_timer_values[0]>=core_if->adp.probe_timer_values[1])
17792 + diff = core_if->adp.probe_timer_values[0]-core_if->adp.probe_timer_values[1];
17793 + else
17794 + diff = core_if->adp.probe_timer_values[1]-core_if->adp.probe_timer_values[0];
17795 + if(diff < 2) {
17796 + return 0;
17797 + } else {
17798 + return 1;
17799 + }
17800 +}
17801 +
17802 +/**
17803 + * This function handles ADP Probe Interrupts
17804 + */
17805 +static int32_t dwc_otg_adp_handle_prb_intr(dwc_otg_core_if_t * core_if,
17806 + uint32_t val)
17807 +{
17808 + adpctl_data_t adpctl = {.d32 = 0 };
17809 + gpwrdn_data_t gpwrdn, temp;
17810 + adpctl.d32 = val;
17811 +
17812 + temp.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
17813 + core_if->adp.probe_counter++;
17814 + core_if->adp.gpwrdn = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
17815 + if (adpctl.b.rtim == 0 && !temp.b.idsts){
17816 + DWC_PRINTF("RTIM value is 0\n");
17817 + goto exit;
17818 + }
17819 + if (set_timer_value(core_if, adpctl.b.rtim) &&
17820 + core_if->adp.initial_probe) {
17821 + core_if->adp.initial_probe = 0;
17822 + dwc_otg_adp_probe_stop(core_if);
17823 + gpwrdn.d32 = 0;
17824 + gpwrdn.b.pmuactv = 1;
17825 + gpwrdn.b.pmuintsel = 1;
17826 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
17827 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
17828 +
17829 + /* check which value is for device mode and which for Host mode */
17830 + if (!temp.b.idsts) { /* considered host mode value is 0 */
17831 + /*
17832 + * Turn on VBUS after initial ADP probe.
17833 + */
17834 + core_if->op_state = A_HOST;
17835 + dwc_otg_enable_global_interrupts(core_if);
17836 + DWC_SPINUNLOCK(core_if->lock);
17837 + cil_hcd_start(core_if);
17838 + dwc_otg_adp_turnon_vbus(core_if);
17839 + DWC_SPINLOCK(core_if->lock);
17840 + } else {
17841 + /*
17842 + * Initiate SRP after initial ADP probe.
17843 + */
17844 + dwc_otg_enable_global_interrupts(core_if);
17845 + dwc_otg_initiate_srp(core_if);
17846 + }
17847 + } else if (core_if->adp.probe_counter > 2){
17848 + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
17849 + if (compare_timer_values(core_if)) {
17850 + DWC_PRINTF("Difference in timer values !!! \n");
17851 +// core_if->adp.attached = DWC_OTG_ADP_ATTACHED;
17852 + dwc_otg_adp_probe_stop(core_if);
17853 +
17854 + /* Power on the core */
17855 + if (core_if->power_down == 2) {
17856 + gpwrdn.b.pwrdnswtch = 1;
17857 + DWC_MODIFY_REG32(&core_if->core_global_regs->
17858 + gpwrdn, 0, gpwrdn.d32);
17859 + }
17860 +
17861 + /* check which value is for device mode and which for Host mode */
17862 + if (!temp.b.idsts) { /* considered host mode value is 0 */
17863 + /* Disable Interrupt from Power Down Logic */
17864 + gpwrdn.d32 = 0;
17865 + gpwrdn.b.pmuintsel = 1;
17866 + gpwrdn.b.pmuactv = 1;
17867 + DWC_MODIFY_REG32(&core_if->core_global_regs->
17868 + gpwrdn, gpwrdn.d32, 0);
17869 +
17870 + /*
17871 + * Initialize the Core for Host mode.
17872 + */
17873 + core_if->op_state = A_HOST;
17874 + dwc_otg_core_init(core_if);
17875 + dwc_otg_enable_global_interrupts(core_if);
17876 + cil_hcd_start(core_if);
17877 + } else {
17878 + gotgctl_data_t gotgctl;
17879 + /* Mask SRP detected interrupt from Power Down Logic */
17880 + gpwrdn.d32 = 0;
17881 + gpwrdn.b.srp_det_msk = 1;
17882 + DWC_MODIFY_REG32(&core_if->core_global_regs->
17883 + gpwrdn, gpwrdn.d32, 0);
17884 +
17885 + /* Disable Power Down Logic */
17886 + gpwrdn.d32 = 0;
17887 + gpwrdn.b.pmuintsel = 1;
17888 + gpwrdn.b.pmuactv = 1;
17889 + DWC_MODIFY_REG32(&core_if->core_global_regs->
17890 + gpwrdn, gpwrdn.d32, 0);
17891 +
17892 + /*
17893 + * Initialize the Core for Device mode.
17894 + */
17895 + core_if->op_state = B_PERIPHERAL;
17896 + dwc_otg_core_init(core_if);
17897 + dwc_otg_enable_global_interrupts(core_if);
17898 + cil_pcd_start(core_if);
17899 +
17900 + gotgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
17901 + if (!gotgctl.b.bsesvld) {
17902 + dwc_otg_initiate_srp(core_if);
17903 + }
17904 + }
17905 + }
17906 + if (core_if->power_down == 2) {
17907 + if (gpwrdn.b.bsessvld) {
17908 + /* Mask SRP detected interrupt from Power Down Logic */
17909 + gpwrdn.d32 = 0;
17910 + gpwrdn.b.srp_det_msk = 1;
17911 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
17912 +
17913 + /* Disable Power Down Logic */
17914 + gpwrdn.d32 = 0;
17915 + gpwrdn.b.pmuactv = 1;
17916 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
17917 +
17918 + /*
17919 + * Initialize the Core for Device mode.
17920 + */
17921 + core_if->op_state = B_PERIPHERAL;
17922 + dwc_otg_core_init(core_if);
17923 + dwc_otg_enable_global_interrupts(core_if);
17924 + cil_pcd_start(core_if);
17925 + }
17926 + }
17927 + }
17928 +exit:
17929 + /* Clear interrupt */
17930 + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
17931 + adpctl.b.adp_prb_int = 1;
17932 + dwc_otg_adp_write_reg(core_if, adpctl.d32);
17933 +
17934 + return 0;
17935 +}
17936 +
17937 +/**
17938 + * This function hadles ADP Sense Interrupt
17939 + */
17940 +static int32_t dwc_otg_adp_handle_sns_intr(dwc_otg_core_if_t * core_if)
17941 +{
17942 + adpctl_data_t adpctl;
17943 + /* Stop ADP Sense timer */
17944 + DWC_TIMER_CANCEL(core_if->adp.sense_timer);
17945 +
17946 + /* Restart ADP Sense timer */
17947 + dwc_otg_adp_sense_timer_start(core_if);
17948 +
17949 + /* Clear interrupt */
17950 + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
17951 + adpctl.b.adp_sns_int = 1;
17952 + dwc_otg_adp_write_reg(core_if, adpctl.d32);
17953 +
17954 + return 0;
17955 +}
17956 +
17957 +/**
17958 + * This function handles ADP Probe Interrupts
17959 + */
17960 +static int32_t dwc_otg_adp_handle_prb_tmout_intr(dwc_otg_core_if_t * core_if,
17961 + uint32_t val)
17962 +{
17963 + adpctl_data_t adpctl = {.d32 = 0 };
17964 + adpctl.d32 = val;
17965 + set_timer_value(core_if, adpctl.b.rtim);
17966 +
17967 + /* Clear interrupt */
17968 + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
17969 + adpctl.b.adp_tmout_int = 1;
17970 + dwc_otg_adp_write_reg(core_if, adpctl.d32);
17971 +
17972 + return 0;
17973 +}
17974 +
17975 +/**
17976 + * ADP Interrupt handler.
17977 + *
17978 + */
17979 +int32_t dwc_otg_adp_handle_intr(dwc_otg_core_if_t * core_if)
17980 +{
17981 + int retval = 0;
17982 + adpctl_data_t adpctl = {.d32 = 0};
17983 +
17984 + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
17985 + DWC_PRINTF("ADPCTL = %08x\n",adpctl.d32);
17986 +
17987 + if (adpctl.b.adp_sns_int & adpctl.b.adp_sns_int_msk) {
17988 + DWC_PRINTF("ADP Sense interrupt\n");
17989 + retval |= dwc_otg_adp_handle_sns_intr(core_if);
17990 + }
17991 + if (adpctl.b.adp_tmout_int & adpctl.b.adp_tmout_int_msk) {
17992 + DWC_PRINTF("ADP timeout interrupt\n");
17993 + retval |= dwc_otg_adp_handle_prb_tmout_intr(core_if, adpctl.d32);
17994 + }
17995 + if (adpctl.b.adp_prb_int & adpctl.b.adp_prb_int_msk) {
17996 + DWC_PRINTF("ADP Probe interrupt\n");
17997 + adpctl.b.adp_prb_int = 1;
17998 + retval |= dwc_otg_adp_handle_prb_intr(core_if, adpctl.d32);
17999 + }
18000 +
18001 +// dwc_otg_adp_modify_reg(core_if, adpctl.d32, 0);
18002 + //dwc_otg_adp_write_reg(core_if, adpctl.d32);
18003 + DWC_PRINTF("RETURN FROM ADP ISR\n");
18004 +
18005 + return retval;
18006 +}
18007 +
18008 +/**
18009 + *
18010 + * @param core_if Programming view of DWC_otg controller.
18011 + */
18012 +int32_t dwc_otg_adp_handle_srp_intr(dwc_otg_core_if_t * core_if)
18013 +{
18014 +
18015 +#ifndef DWC_HOST_ONLY
18016 + hprt0_data_t hprt0;
18017 + gpwrdn_data_t gpwrdn;
18018 + DWC_DEBUGPL(DBG_ANY, "++ Power Down Logic Session Request Interrupt++\n");
18019 +
18020 + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
18021 + /* check which value is for device mode and which for Host mode */
18022 + if (!gpwrdn.b.idsts) { /* considered host mode value is 0 */
18023 + DWC_PRINTF("SRP: Host mode\n");
18024 +
18025 + if (core_if->adp_enable) {
18026 + dwc_otg_adp_probe_stop(core_if);
18027 +
18028 + /* Power on the core */
18029 + if (core_if->power_down == 2) {
18030 + gpwrdn.b.pwrdnswtch = 1;
18031 + DWC_MODIFY_REG32(&core_if->core_global_regs->
18032 + gpwrdn, 0, gpwrdn.d32);
18033 + }
18034 +
18035 + core_if->op_state = A_HOST;
18036 + dwc_otg_core_init(core_if);
18037 + dwc_otg_enable_global_interrupts(core_if);
18038 + cil_hcd_start(core_if);
18039 + }
18040 +
18041 + /* Turn on the port power bit. */
18042 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
18043 + hprt0.b.prtpwr = 1;
18044 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
18045 +
18046 + /* Start the Connection timer. So a message can be displayed
18047 + * if connect does not occur within 10 seconds. */
18048 + cil_hcd_session_start(core_if);
18049 + } else {
18050 + DWC_PRINTF("SRP: Device mode %s\n", __FUNCTION__);
18051 + if (core_if->adp_enable) {
18052 + dwc_otg_adp_probe_stop(core_if);
18053 +
18054 + /* Power on the core */
18055 + if (core_if->power_down == 2) {
18056 + gpwrdn.b.pwrdnswtch = 1;
18057 + DWC_MODIFY_REG32(&core_if->core_global_regs->
18058 + gpwrdn, 0, gpwrdn.d32);
18059 + }
18060 +
18061 + gpwrdn.d32 = 0;
18062 + gpwrdn.b.pmuactv = 0;
18063 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0,
18064 + gpwrdn.d32);
18065 +
18066 + core_if->op_state = B_PERIPHERAL;
18067 + dwc_otg_core_init(core_if);
18068 + dwc_otg_enable_global_interrupts(core_if);
18069 + cil_pcd_start(core_if);
18070 + }
18071 + }
18072 +#endif
18073 + return 1;
18074 +}
18075 --- /dev/null
18076 +++ b/drivers/usb/host/dwc_otg/dwc_otg_adp.h
18077 @@ -0,0 +1,80 @@
18078 +/* ==========================================================================
18079 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_adp.h $
18080 + * $Revision: #7 $
18081 + * $Date: 2011/10/24 $
18082 + * $Change: 1871159 $
18083 + *
18084 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
18085 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
18086 + * otherwise expressly agreed to in writing between Synopsys and you.
18087 + *
18088 + * The Software IS NOT an item of Licensed Software or Licensed Product under
18089 + * any End User Software License Agreement or Agreement for Licensed Product
18090 + * with Synopsys or any supplement thereto. You are permitted to use and
18091 + * redistribute this Software in source and binary forms, with or without
18092 + * modification, provided that redistributions of source code must retain this
18093 + * notice. You may not view, use, disclose, copy or distribute this file or
18094 + * any information contained herein except pursuant to this license grant from
18095 + * Synopsys. If you do not agree with this notice, including the disclaimer
18096 + * below, then you are not authorized to use the Software.
18097 + *
18098 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
18099 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18100 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18101 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
18102 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18103 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
18104 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
18105 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
18106 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
18107 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
18108 + * DAMAGE.
18109 + * ========================================================================== */
18110 +
18111 +#ifndef __DWC_OTG_ADP_H__
18112 +#define __DWC_OTG_ADP_H__
18113 +
18114 +/**
18115 + * @file
18116 + *
18117 + * This file contains the Attach Detect Protocol interfaces and defines
18118 + * (functions) and structures for Linux.
18119 + *
18120 + */
18121 +
18122 +#define DWC_OTG_ADP_UNATTACHED 0
18123 +#define DWC_OTG_ADP_ATTACHED 1
18124 +#define DWC_OTG_ADP_UNKOWN 2
18125 +
18126 +typedef struct dwc_otg_adp {
18127 + uint32_t adp_started;
18128 + uint32_t initial_probe;
18129 + int32_t probe_timer_values[2];
18130 + uint32_t probe_enabled;
18131 + uint32_t sense_enabled;
18132 + dwc_timer_t *sense_timer;
18133 + uint32_t sense_timer_started;
18134 + dwc_timer_t *vbuson_timer;
18135 + uint32_t vbuson_timer_started;
18136 + uint32_t attached;
18137 + uint32_t probe_counter;
18138 + uint32_t gpwrdn;
18139 +} dwc_otg_adp_t;
18140 +
18141 +/**
18142 + * Attach Detect Protocol functions
18143 + */
18144 +
18145 +extern void dwc_otg_adp_write_reg(dwc_otg_core_if_t * core_if, uint32_t value);
18146 +extern uint32_t dwc_otg_adp_read_reg(dwc_otg_core_if_t * core_if);
18147 +extern uint32_t dwc_otg_adp_probe_start(dwc_otg_core_if_t * core_if);
18148 +extern uint32_t dwc_otg_adp_sense_start(dwc_otg_core_if_t * core_if);
18149 +extern uint32_t dwc_otg_adp_probe_stop(dwc_otg_core_if_t * core_if);
18150 +extern uint32_t dwc_otg_adp_sense_stop(dwc_otg_core_if_t * core_if);
18151 +extern void dwc_otg_adp_start(dwc_otg_core_if_t * core_if, uint8_t is_host);
18152 +extern void dwc_otg_adp_init(dwc_otg_core_if_t * core_if);
18153 +extern void dwc_otg_adp_remove(dwc_otg_core_if_t * core_if);
18154 +extern int32_t dwc_otg_adp_handle_intr(dwc_otg_core_if_t * core_if);
18155 +extern int32_t dwc_otg_adp_handle_srp_intr(dwc_otg_core_if_t * core_if);
18156 +
18157 +#endif //__DWC_OTG_ADP_H__
18158 --- /dev/null
18159 +++ b/drivers/usb/host/dwc_otg/dwc_otg_attr.c
18160 @@ -0,0 +1,1212 @@
18161 +/* ==========================================================================
18162 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_attr.c $
18163 + * $Revision: #44 $
18164 + * $Date: 2010/11/29 $
18165 + * $Change: 1636033 $
18166 + *
18167 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
18168 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
18169 + * otherwise expressly agreed to in writing between Synopsys and you.
18170 + *
18171 + * The Software IS NOT an item of Licensed Software or Licensed Product under
18172 + * any End User Software License Agreement or Agreement for Licensed Product
18173 + * with Synopsys or any supplement thereto. You are permitted to use and
18174 + * redistribute this Software in source and binary forms, with or without
18175 + * modification, provided that redistributions of source code must retain this
18176 + * notice. You may not view, use, disclose, copy or distribute this file or
18177 + * any information contained herein except pursuant to this license grant from
18178 + * Synopsys. If you do not agree with this notice, including the disclaimer
18179 + * below, then you are not authorized to use the Software.
18180 + *
18181 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
18182 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18183 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18184 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
18185 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18186 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
18187 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
18188 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
18189 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
18190 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
18191 + * DAMAGE.
18192 + * ========================================================================== */
18193 +
18194 +/** @file
18195 + *
18196 + * The diagnostic interface will provide access to the controller for
18197 + * bringing up the hardware and testing. The Linux driver attributes
18198 + * feature will be used to provide the Linux Diagnostic
18199 + * Interface. These attributes are accessed through sysfs.
18200 + */
18201 +
18202 +/** @page "Linux Module Attributes"
18203 + *
18204 + * The Linux module attributes feature is used to provide the Linux
18205 + * Diagnostic Interface. These attributes are accessed through sysfs.
18206 + * The diagnostic interface will provide access to the controller for
18207 + * bringing up the hardware and testing.
18208 +
18209 + The following table shows the attributes.
18210 + <table>
18211 + <tr>
18212 + <td><b> Name</b></td>
18213 + <td><b> Description</b></td>
18214 + <td><b> Access</b></td>
18215 + </tr>
18216 +
18217 + <tr>
18218 + <td> mode </td>
18219 + <td> Returns the current mode: 0 for device mode, 1 for host mode</td>
18220 + <td> Read</td>
18221 + </tr>
18222 +
18223 + <tr>
18224 + <td> hnpcapable </td>
18225 + <td> Gets or sets the "HNP-capable" bit in the Core USB Configuraton Register.
18226 + Read returns the current value.</td>
18227 + <td> Read/Write</td>
18228 + </tr>
18229 +
18230 + <tr>
18231 + <td> srpcapable </td>
18232 + <td> Gets or sets the "SRP-capable" bit in the Core USB Configuraton Register.
18233 + Read returns the current value.</td>
18234 + <td> Read/Write</td>
18235 + </tr>
18236 +
18237 + <tr>
18238 + <td> hsic_connect </td>
18239 + <td> Gets or sets the "HSIC-Connect" bit in the GLPMCFG Register.
18240 + Read returns the current value.</td>
18241 + <td> Read/Write</td>
18242 + </tr>
18243 +
18244 + <tr>
18245 + <td> inv_sel_hsic </td>
18246 + <td> Gets or sets the "Invert Select HSIC" bit in the GLPMFG Register.
18247 + Read returns the current value.</td>
18248 + <td> Read/Write</td>
18249 + </tr>
18250 +
18251 + <tr>
18252 + <td> hnp </td>
18253 + <td> Initiates the Host Negotiation Protocol. Read returns the status.</td>
18254 + <td> Read/Write</td>
18255 + </tr>
18256 +
18257 + <tr>
18258 + <td> srp </td>
18259 + <td> Initiates the Session Request Protocol. Read returns the status.</td>
18260 + <td> Read/Write</td>
18261 + </tr>
18262 +
18263 + <tr>
18264 + <td> buspower </td>
18265 + <td> Gets or sets the Power State of the bus (0 - Off or 1 - On)</td>
18266 + <td> Read/Write</td>
18267 + </tr>
18268 +
18269 + <tr>
18270 + <td> bussuspend </td>
18271 + <td> Suspends the USB bus.</td>
18272 + <td> Read/Write</td>
18273 + </tr>
18274 +
18275 + <tr>
18276 + <td> busconnected </td>
18277 + <td> Gets the connection status of the bus</td>
18278 + <td> Read</td>
18279 + </tr>
18280 +
18281 + <tr>
18282 + <td> gotgctl </td>
18283 + <td> Gets or sets the Core Control Status Register.</td>
18284 + <td> Read/Write</td>
18285 + </tr>
18286 +
18287 + <tr>
18288 + <td> gusbcfg </td>
18289 + <td> Gets or sets the Core USB Configuration Register</td>
18290 + <td> Read/Write</td>
18291 + </tr>
18292 +
18293 + <tr>
18294 + <td> grxfsiz </td>
18295 + <td> Gets or sets the Receive FIFO Size Register</td>
18296 + <td> Read/Write</td>
18297 + </tr>
18298 +
18299 + <tr>
18300 + <td> gnptxfsiz </td>
18301 + <td> Gets or sets the non-periodic Transmit Size Register</td>
18302 + <td> Read/Write</td>
18303 + </tr>
18304 +
18305 + <tr>
18306 + <td> gpvndctl </td>
18307 + <td> Gets or sets the PHY Vendor Control Register</td>
18308 + <td> Read/Write</td>
18309 + </tr>
18310 +
18311 + <tr>
18312 + <td> ggpio </td>
18313 + <td> Gets the value in the lower 16-bits of the General Purpose IO Register
18314 + or sets the upper 16 bits.</td>
18315 + <td> Read/Write</td>
18316 + </tr>
18317 +
18318 + <tr>
18319 + <td> guid </td>
18320 + <td> Gets or sets the value of the User ID Register</td>
18321 + <td> Read/Write</td>
18322 + </tr>
18323 +
18324 + <tr>
18325 + <td> gsnpsid </td>
18326 + <td> Gets the value of the Synopsys ID Regester</td>
18327 + <td> Read</td>
18328 + </tr>
18329 +
18330 + <tr>
18331 + <td> devspeed </td>
18332 + <td> Gets or sets the device speed setting in the DCFG register</td>
18333 + <td> Read/Write</td>
18334 + </tr>
18335 +
18336 + <tr>
18337 + <td> enumspeed </td>
18338 + <td> Gets the device enumeration Speed.</td>
18339 + <td> Read</td>
18340 + </tr>
18341 +
18342 + <tr>
18343 + <td> hptxfsiz </td>
18344 + <td> Gets the value of the Host Periodic Transmit FIFO</td>
18345 + <td> Read</td>
18346 + </tr>
18347 +
18348 + <tr>
18349 + <td> hprt0 </td>
18350 + <td> Gets or sets the value in the Host Port Control and Status Register</td>
18351 + <td> Read/Write</td>
18352 + </tr>
18353 +
18354 + <tr>
18355 + <td> regoffset </td>
18356 + <td> Sets the register offset for the next Register Access</td>
18357 + <td> Read/Write</td>
18358 + </tr>
18359 +
18360 + <tr>
18361 + <td> regvalue </td>
18362 + <td> Gets or sets the value of the register at the offset in the regoffset attribute.</td>
18363 + <td> Read/Write</td>
18364 + </tr>
18365 +
18366 + <tr>
18367 + <td> remote_wakeup </td>
18368 + <td> On read, shows the status of Remote Wakeup. On write, initiates a remote
18369 + wakeup of the host. When bit 0 is 1 and Remote Wakeup is enabled, the Remote
18370 + Wakeup signalling bit in the Device Control Register is set for 1
18371 + milli-second.</td>
18372 + <td> Read/Write</td>
18373 + </tr>
18374 +
18375 + <tr>
18376 + <td> rem_wakeup_pwrdn </td>
18377 + <td> On read, shows the status core - hibernated or not. On write, initiates
18378 + a remote wakeup of the device from Hibernation. </td>
18379 + <td> Read/Write</td>
18380 + </tr>
18381 +
18382 + <tr>
18383 + <td> mode_ch_tim_en </td>
18384 + <td> This bit is used to enable or disable the host core to wait for 200 PHY
18385 + clock cycles at the end of Resume to change the opmode signal to the PHY to 00
18386 + after Suspend or LPM. </td>
18387 + <td> Read/Write</td>
18388 + </tr>
18389 +
18390 + <tr>
18391 + <td> fr_interval </td>
18392 + <td> On read, shows the value of HFIR Frame Interval. On write, dynamically
18393 + reload HFIR register during runtime. The application can write a value to this
18394 + register only after the Port Enable bit of the Host Port Control and Status
18395 + register (HPRT.PrtEnaPort) has been set </td>
18396 + <td> Read/Write</td>
18397 + </tr>
18398 +
18399 + <tr>
18400 + <td> disconnect_us </td>
18401 + <td> On read, shows the status of disconnect_device_us. On write, sets disconnect_us
18402 + which causes soft disconnect for 100us. Applicable only for device mode of operation.</td>
18403 + <td> Read/Write</td>
18404 + </tr>
18405 +
18406 + <tr>
18407 + <td> regdump </td>
18408 + <td> Dumps the contents of core registers.</td>
18409 + <td> Read</td>
18410 + </tr>
18411 +
18412 + <tr>
18413 + <td> spramdump </td>
18414 + <td> Dumps the contents of core registers.</td>
18415 + <td> Read</td>
18416 + </tr>
18417 +
18418 + <tr>
18419 + <td> hcddump </td>
18420 + <td> Dumps the current HCD state.</td>
18421 + <td> Read</td>
18422 + </tr>
18423 +
18424 + <tr>
18425 + <td> hcd_frrem </td>
18426 + <td> Shows the average value of the Frame Remaining
18427 + field in the Host Frame Number/Frame Remaining register when an SOF interrupt
18428 + occurs. This can be used to determine the average interrupt latency. Also
18429 + shows the average Frame Remaining value for start_transfer and the "a" and
18430 + "b" sample points. The "a" and "b" sample points may be used during debugging
18431 + bto determine how long it takes to execute a section of the HCD code.</td>
18432 + <td> Read</td>
18433 + </tr>
18434 +
18435 + <tr>
18436 + <td> rd_reg_test </td>
18437 + <td> Displays the time required to read the GNPTXFSIZ register many times
18438 + (the output shows the number of times the register is read).
18439 + <td> Read</td>
18440 + </tr>
18441 +
18442 + <tr>
18443 + <td> wr_reg_test </td>
18444 + <td> Displays the time required to write the GNPTXFSIZ register many times
18445 + (the output shows the number of times the register is written).
18446 + <td> Read</td>
18447 + </tr>
18448 +
18449 + <tr>
18450 + <td> lpm_response </td>
18451 + <td> Gets or sets lpm_response mode. Applicable only in device mode.
18452 + <td> Write</td>
18453 + </tr>
18454 +
18455 + <tr>
18456 + <td> sleep_status </td>
18457 + <td> Shows sleep status of device.
18458 + <td> Read</td>
18459 + </tr>
18460 +
18461 + </table>
18462 +
18463 + Example usage:
18464 + To get the current mode:
18465 + cat /sys/devices/lm0/mode
18466 +
18467 + To power down the USB:
18468 + echo 0 > /sys/devices/lm0/buspower
18469 + */
18470 +
18471 +#include "dwc_otg_os_dep.h"
18472 +#include "dwc_os.h"
18473 +#include "dwc_otg_driver.h"
18474 +#include "dwc_otg_attr.h"
18475 +#include "dwc_otg_core_if.h"
18476 +#include "dwc_otg_pcd_if.h"
18477 +#include "dwc_otg_hcd_if.h"
18478 +
18479 +/*
18480 + * MACROs for defining sysfs attribute
18481 + */
18482 +#ifdef LM_INTERFACE
18483 +
18484 +#define DWC_OTG_DEVICE_ATTR_BITFIELD_SHOW(_otg_attr_name_,_string_) \
18485 +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
18486 +{ \
18487 + struct lm_device *lm_dev = container_of(_dev, struct lm_device, dev); \
18488 + dwc_otg_device_t *otg_dev = lm_get_drvdata(lm_dev); \
18489 + uint32_t val; \
18490 + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
18491 + return sprintf (buf, "%s = 0x%x\n", _string_, val); \
18492 +}
18493 +#define DWC_OTG_DEVICE_ATTR_BITFIELD_STORE(_otg_attr_name_,_string_) \
18494 +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
18495 + const char *buf, size_t count) \
18496 +{ \
18497 + struct lm_device *lm_dev = container_of(_dev, struct lm_device, dev); \
18498 + dwc_otg_device_t *otg_dev = lm_get_drvdata(lm_dev); \
18499 + uint32_t set = simple_strtoul(buf, NULL, 16); \
18500 + dwc_otg_set_##_otg_attr_name_(otg_dev->core_if, set);\
18501 + return count; \
18502 +}
18503 +
18504 +#elif defined(PCI_INTERFACE)
18505 +
18506 +#define DWC_OTG_DEVICE_ATTR_BITFIELD_SHOW(_otg_attr_name_,_string_) \
18507 +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
18508 +{ \
18509 + dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev); \
18510 + uint32_t val; \
18511 + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
18512 + return sprintf (buf, "%s = 0x%x\n", _string_, val); \
18513 +}
18514 +#define DWC_OTG_DEVICE_ATTR_BITFIELD_STORE(_otg_attr_name_,_string_) \
18515 +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
18516 + const char *buf, size_t count) \
18517 +{ \
18518 + dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev); \
18519 + uint32_t set = simple_strtoul(buf, NULL, 16); \
18520 + dwc_otg_set_##_otg_attr_name_(otg_dev->core_if, set);\
18521 + return count; \
18522 +}
18523 +
18524 +#elif defined(PLATFORM_INTERFACE)
18525 +
18526 +#define DWC_OTG_DEVICE_ATTR_BITFIELD_SHOW(_otg_attr_name_,_string_) \
18527 +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
18528 +{ \
18529 + struct platform_device *platform_dev = \
18530 + container_of(_dev, struct platform_device, dev); \
18531 + dwc_otg_device_t *otg_dev = platform_get_drvdata(platform_dev); \
18532 + uint32_t val; \
18533 + DWC_PRINTF("%s(%p) -> platform_dev %p, otg_dev %p\n", \
18534 + __func__, _dev, platform_dev, otg_dev); \
18535 + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
18536 + return sprintf (buf, "%s = 0x%x\n", _string_, val); \
18537 +}
18538 +#define DWC_OTG_DEVICE_ATTR_BITFIELD_STORE(_otg_attr_name_,_string_) \
18539 +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
18540 + const char *buf, size_t count) \
18541 +{ \
18542 + struct platform_device *platform_dev = container_of(_dev, struct platform_device, dev); \
18543 + dwc_otg_device_t *otg_dev = platform_get_drvdata(platform_dev); \
18544 + uint32_t set = simple_strtoul(buf, NULL, 16); \
18545 + dwc_otg_set_##_otg_attr_name_(otg_dev->core_if, set);\
18546 + return count; \
18547 +}
18548 +#endif
18549 +
18550 +/*
18551 + * MACROs for defining sysfs attribute for 32-bit registers
18552 + */
18553 +#ifdef LM_INTERFACE
18554 +#define DWC_OTG_DEVICE_ATTR_REG_SHOW(_otg_attr_name_,_string_) \
18555 +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
18556 +{ \
18557 + struct lm_device *lm_dev = container_of(_dev, struct lm_device, dev); \
18558 + dwc_otg_device_t *otg_dev = lm_get_drvdata(lm_dev); \
18559 + uint32_t val; \
18560 + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
18561 + return sprintf (buf, "%s = 0x%08x\n", _string_, val); \
18562 +}
18563 +#define DWC_OTG_DEVICE_ATTR_REG_STORE(_otg_attr_name_,_string_) \
18564 +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
18565 + const char *buf, size_t count) \
18566 +{ \
18567 + struct lm_device *lm_dev = container_of(_dev, struct lm_device, dev); \
18568 + dwc_otg_device_t *otg_dev = lm_get_drvdata(lm_dev); \
18569 + uint32_t val = simple_strtoul(buf, NULL, 16); \
18570 + dwc_otg_set_##_otg_attr_name_ (otg_dev->core_if, val); \
18571 + return count; \
18572 +}
18573 +#elif defined(PCI_INTERFACE)
18574 +#define DWC_OTG_DEVICE_ATTR_REG_SHOW(_otg_attr_name_,_string_) \
18575 +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
18576 +{ \
18577 + dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev); \
18578 + uint32_t val; \
18579 + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
18580 + return sprintf (buf, "%s = 0x%08x\n", _string_, val); \
18581 +}
18582 +#define DWC_OTG_DEVICE_ATTR_REG_STORE(_otg_attr_name_,_string_) \
18583 +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
18584 + const char *buf, size_t count) \
18585 +{ \
18586 + dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev); \
18587 + uint32_t val = simple_strtoul(buf, NULL, 16); \
18588 + dwc_otg_set_##_otg_attr_name_ (otg_dev->core_if, val); \
18589 + return count; \
18590 +}
18591 +
18592 +#elif defined(PLATFORM_INTERFACE)
18593 +#include "dwc_otg_dbg.h"
18594 +#define DWC_OTG_DEVICE_ATTR_REG_SHOW(_otg_attr_name_,_string_) \
18595 +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
18596 +{ \
18597 + struct platform_device *platform_dev = container_of(_dev, struct platform_device, dev); \
18598 + dwc_otg_device_t *otg_dev = platform_get_drvdata(platform_dev); \
18599 + uint32_t val; \
18600 + DWC_PRINTF("%s(%p) -> platform_dev %p, otg_dev %p\n", \
18601 + __func__, _dev, platform_dev, otg_dev); \
18602 + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
18603 + return sprintf (buf, "%s = 0x%08x\n", _string_, val); \
18604 +}
18605 +#define DWC_OTG_DEVICE_ATTR_REG_STORE(_otg_attr_name_,_string_) \
18606 +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
18607 + const char *buf, size_t count) \
18608 +{ \
18609 + struct platform_device *platform_dev = container_of(_dev, struct platform_device, dev); \
18610 + dwc_otg_device_t *otg_dev = platform_get_drvdata(platform_dev); \
18611 + uint32_t val = simple_strtoul(buf, NULL, 16); \
18612 + dwc_otg_set_##_otg_attr_name_ (otg_dev->core_if, val); \
18613 + return count; \
18614 +}
18615 +
18616 +#endif
18617 +
18618 +#define DWC_OTG_DEVICE_ATTR_BITFIELD_RW(_otg_attr_name_,_string_) \
18619 +DWC_OTG_DEVICE_ATTR_BITFIELD_SHOW(_otg_attr_name_,_string_) \
18620 +DWC_OTG_DEVICE_ATTR_BITFIELD_STORE(_otg_attr_name_,_string_) \
18621 +DEVICE_ATTR(_otg_attr_name_,0644,_otg_attr_name_##_show,_otg_attr_name_##_store);
18622 +
18623 +#define DWC_OTG_DEVICE_ATTR_BITFIELD_RO(_otg_attr_name_,_string_) \
18624 +DWC_OTG_DEVICE_ATTR_BITFIELD_SHOW(_otg_attr_name_,_string_) \
18625 +DEVICE_ATTR(_otg_attr_name_,0444,_otg_attr_name_##_show,NULL);
18626 +
18627 +#define DWC_OTG_DEVICE_ATTR_REG32_RW(_otg_attr_name_,_addr_,_string_) \
18628 +DWC_OTG_DEVICE_ATTR_REG_SHOW(_otg_attr_name_,_string_) \
18629 +DWC_OTG_DEVICE_ATTR_REG_STORE(_otg_attr_name_,_string_) \
18630 +DEVICE_ATTR(_otg_attr_name_,0644,_otg_attr_name_##_show,_otg_attr_name_##_store);
18631 +
18632 +#define DWC_OTG_DEVICE_ATTR_REG32_RO(_otg_attr_name_,_addr_,_string_) \
18633 +DWC_OTG_DEVICE_ATTR_REG_SHOW(_otg_attr_name_,_string_) \
18634 +DEVICE_ATTR(_otg_attr_name_,0444,_otg_attr_name_##_show,NULL);
18635 +
18636 +/** @name Functions for Show/Store of Attributes */
18637 +/**@{*/
18638 +
18639 +/**
18640 + * Helper function returning the otg_device structure of the given device
18641 + */
18642 +static dwc_otg_device_t *dwc_otg_drvdev(struct device *_dev)
18643 +{
18644 + dwc_otg_device_t *otg_dev;
18645 + DWC_OTG_GETDRVDEV(otg_dev, _dev);
18646 + return otg_dev;
18647 +}
18648 +
18649 +/**
18650 + * Show the register offset of the Register Access.
18651 + */
18652 +static ssize_t regoffset_show(struct device *_dev,
18653 + struct device_attribute *attr, char *buf)
18654 +{
18655 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18656 + return snprintf(buf, sizeof("0xFFFFFFFF\n") + 1, "0x%08x\n",
18657 + otg_dev->os_dep.reg_offset);
18658 +}
18659 +
18660 +/**
18661 + * Set the register offset for the next Register Access Read/Write
18662 + */
18663 +static ssize_t regoffset_store(struct device *_dev,
18664 + struct device_attribute *attr,
18665 + const char *buf, size_t count)
18666 +{
18667 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18668 + uint32_t offset = simple_strtoul(buf, NULL, 16);
18669 +#if defined(LM_INTERFACE) || defined(PLATFORM_INTERFACE)
18670 + if (offset < SZ_256K) {
18671 +#elif defined(PCI_INTERFACE)
18672 + if (offset < 0x00040000) {
18673 +#endif
18674 + otg_dev->os_dep.reg_offset = offset;
18675 + } else {
18676 + dev_err(_dev, "invalid offset\n");
18677 + }
18678 +
18679 + return count;
18680 +}
18681 +
18682 +DEVICE_ATTR(regoffset, S_IRUGO | S_IWUSR, regoffset_show, regoffset_store);
18683 +
18684 +/**
18685 + * Show the value of the register at the offset in the reg_offset
18686 + * attribute.
18687 + */
18688 +static ssize_t regvalue_show(struct device *_dev,
18689 + struct device_attribute *attr, char *buf)
18690 +{
18691 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18692 + uint32_t val;
18693 + volatile uint32_t *addr;
18694 +
18695 + if (otg_dev->os_dep.reg_offset != 0xFFFFFFFF && 0 != otg_dev->os_dep.base) {
18696 + /* Calculate the address */
18697 + addr = (uint32_t *) (otg_dev->os_dep.reg_offset +
18698 + (uint8_t *) otg_dev->os_dep.base);
18699 + val = DWC_READ_REG32(addr);
18700 + return snprintf(buf,
18701 + sizeof("Reg@0xFFFFFFFF = 0xFFFFFFFF\n") + 1,
18702 + "Reg@0x%06x = 0x%08x\n", otg_dev->os_dep.reg_offset,
18703 + val);
18704 + } else {
18705 + dev_err(_dev, "Invalid offset (0x%0x)\n", otg_dev->os_dep.reg_offset);
18706 + return sprintf(buf, "invalid offset\n");
18707 + }
18708 +}
18709 +
18710 +/**
18711 + * Store the value in the register at the offset in the reg_offset
18712 + * attribute.
18713 + *
18714 + */
18715 +static ssize_t regvalue_store(struct device *_dev,
18716 + struct device_attribute *attr,
18717 + const char *buf, size_t count)
18718 +{
18719 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18720 + volatile uint32_t *addr;
18721 + uint32_t val = simple_strtoul(buf, NULL, 16);
18722 + //dev_dbg(_dev, "Offset=0x%08x Val=0x%08x\n", otg_dev->reg_offset, val);
18723 + if (otg_dev->os_dep.reg_offset != 0xFFFFFFFF && 0 != otg_dev->os_dep.base) {
18724 + /* Calculate the address */
18725 + addr = (uint32_t *) (otg_dev->os_dep.reg_offset +
18726 + (uint8_t *) otg_dev->os_dep.base);
18727 + DWC_WRITE_REG32(addr, val);
18728 + } else {
18729 + dev_err(_dev, "Invalid Register Offset (0x%08x)\n",
18730 + otg_dev->os_dep.reg_offset);
18731 + }
18732 + return count;
18733 +}
18734 +
18735 +DEVICE_ATTR(regvalue, S_IRUGO | S_IWUSR, regvalue_show, regvalue_store);
18736 +
18737 +/*
18738 + * Attributes
18739 + */
18740 +DWC_OTG_DEVICE_ATTR_BITFIELD_RO(mode, "Mode");
18741 +DWC_OTG_DEVICE_ATTR_BITFIELD_RW(hnpcapable, "HNPCapable");
18742 +DWC_OTG_DEVICE_ATTR_BITFIELD_RW(srpcapable, "SRPCapable");
18743 +DWC_OTG_DEVICE_ATTR_BITFIELD_RW(hsic_connect, "HSIC Connect");
18744 +DWC_OTG_DEVICE_ATTR_BITFIELD_RW(inv_sel_hsic, "Invert Select HSIC");
18745 +
18746 +//DWC_OTG_DEVICE_ATTR_BITFIELD_RW(buspower,&(otg_dev->core_if->core_global_regs->gotgctl),(1<<8),8,"Mode");
18747 +//DWC_OTG_DEVICE_ATTR_BITFIELD_RW(bussuspend,&(otg_dev->core_if->core_global_regs->gotgctl),(1<<8),8,"Mode");
18748 +DWC_OTG_DEVICE_ATTR_BITFIELD_RO(busconnected, "Bus Connected");
18749 +
18750 +DWC_OTG_DEVICE_ATTR_REG32_RW(gotgctl, 0, "GOTGCTL");
18751 +DWC_OTG_DEVICE_ATTR_REG32_RW(gusbcfg,
18752 + &(otg_dev->core_if->core_global_regs->gusbcfg),
18753 + "GUSBCFG");
18754 +DWC_OTG_DEVICE_ATTR_REG32_RW(grxfsiz,
18755 + &(otg_dev->core_if->core_global_regs->grxfsiz),
18756 + "GRXFSIZ");
18757 +DWC_OTG_DEVICE_ATTR_REG32_RW(gnptxfsiz,
18758 + &(otg_dev->core_if->core_global_regs->gnptxfsiz),
18759 + "GNPTXFSIZ");
18760 +DWC_OTG_DEVICE_ATTR_REG32_RW(gpvndctl,
18761 + &(otg_dev->core_if->core_global_regs->gpvndctl),
18762 + "GPVNDCTL");
18763 +DWC_OTG_DEVICE_ATTR_REG32_RW(ggpio,
18764 + &(otg_dev->core_if->core_global_regs->ggpio),
18765 + "GGPIO");
18766 +DWC_OTG_DEVICE_ATTR_REG32_RW(guid, &(otg_dev->core_if->core_global_regs->guid),
18767 + "GUID");
18768 +DWC_OTG_DEVICE_ATTR_REG32_RO(gsnpsid,
18769 + &(otg_dev->core_if->core_global_regs->gsnpsid),
18770 + "GSNPSID");
18771 +DWC_OTG_DEVICE_ATTR_BITFIELD_RW(devspeed, "Device Speed");
18772 +DWC_OTG_DEVICE_ATTR_BITFIELD_RO(enumspeed, "Device Enumeration Speed");
18773 +
18774 +DWC_OTG_DEVICE_ATTR_REG32_RO(hptxfsiz,
18775 + &(otg_dev->core_if->core_global_regs->hptxfsiz),
18776 + "HPTXFSIZ");
18777 +DWC_OTG_DEVICE_ATTR_REG32_RW(hprt0, otg_dev->core_if->host_if->hprt0, "HPRT0");
18778 +
18779 +/**
18780 + * @todo Add code to initiate the HNP.
18781 + */
18782 +/**
18783 + * Show the HNP status bit
18784 + */
18785 +static ssize_t hnp_show(struct device *_dev,
18786 + struct device_attribute *attr, char *buf)
18787 +{
18788 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18789 + return sprintf(buf, "HstNegScs = 0x%x\n",
18790 + dwc_otg_get_hnpstatus(otg_dev->core_if));
18791 +}
18792 +
18793 +/**
18794 + * Set the HNP Request bit
18795 + */
18796 +static ssize_t hnp_store(struct device *_dev,
18797 + struct device_attribute *attr,
18798 + const char *buf, size_t count)
18799 +{
18800 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18801 + uint32_t in = simple_strtoul(buf, NULL, 16);
18802 + dwc_otg_set_hnpreq(otg_dev->core_if, in);
18803 + return count;
18804 +}
18805 +
18806 +DEVICE_ATTR(hnp, 0644, hnp_show, hnp_store);
18807 +
18808 +/**
18809 + * @todo Add code to initiate the SRP.
18810 + */
18811 +/**
18812 + * Show the SRP status bit
18813 + */
18814 +static ssize_t srp_show(struct device *_dev,
18815 + struct device_attribute *attr, char *buf)
18816 +{
18817 +#ifndef DWC_HOST_ONLY
18818 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18819 + return sprintf(buf, "SesReqScs = 0x%x\n",
18820 + dwc_otg_get_srpstatus(otg_dev->core_if));
18821 +#else
18822 + return sprintf(buf, "Host Only Mode!\n");
18823 +#endif
18824 +}
18825 +
18826 +/**
18827 + * Set the SRP Request bit
18828 + */
18829 +static ssize_t srp_store(struct device *_dev,
18830 + struct device_attribute *attr,
18831 + const char *buf, size_t count)
18832 +{
18833 +#ifndef DWC_HOST_ONLY
18834 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18835 + dwc_otg_pcd_initiate_srp(otg_dev->pcd);
18836 +#endif
18837 + return count;
18838 +}
18839 +
18840 +DEVICE_ATTR(srp, 0644, srp_show, srp_store);
18841 +
18842 +/**
18843 + * @todo Need to do more for power on/off?
18844 + */
18845 +/**
18846 + * Show the Bus Power status
18847 + */
18848 +static ssize_t buspower_show(struct device *_dev,
18849 + struct device_attribute *attr, char *buf)
18850 +{
18851 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18852 + return sprintf(buf, "Bus Power = 0x%x\n",
18853 + dwc_otg_get_prtpower(otg_dev->core_if));
18854 +}
18855 +
18856 +/**
18857 + * Set the Bus Power status
18858 + */
18859 +static ssize_t buspower_store(struct device *_dev,
18860 + struct device_attribute *attr,
18861 + const char *buf, size_t count)
18862 +{
18863 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18864 + uint32_t on = simple_strtoul(buf, NULL, 16);
18865 + dwc_otg_set_prtpower(otg_dev->core_if, on);
18866 + return count;
18867 +}
18868 +
18869 +DEVICE_ATTR(buspower, 0644, buspower_show, buspower_store);
18870 +
18871 +/**
18872 + * @todo Need to do more for suspend?
18873 + */
18874 +/**
18875 + * Show the Bus Suspend status
18876 + */
18877 +static ssize_t bussuspend_show(struct device *_dev,
18878 + struct device_attribute *attr, char *buf)
18879 +{
18880 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18881 + return sprintf(buf, "Bus Suspend = 0x%x\n",
18882 + dwc_otg_get_prtsuspend(otg_dev->core_if));
18883 +}
18884 +
18885 +/**
18886 + * Set the Bus Suspend status
18887 + */
18888 +static ssize_t bussuspend_store(struct device *_dev,
18889 + struct device_attribute *attr,
18890 + const char *buf, size_t count)
18891 +{
18892 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18893 + uint32_t in = simple_strtoul(buf, NULL, 16);
18894 + dwc_otg_set_prtsuspend(otg_dev->core_if, in);
18895 + return count;
18896 +}
18897 +
18898 +DEVICE_ATTR(bussuspend, 0644, bussuspend_show, bussuspend_store);
18899 +
18900 +/**
18901 + * Show the Mode Change Ready Timer status
18902 + */
18903 +static ssize_t mode_ch_tim_en_show(struct device *_dev,
18904 + struct device_attribute *attr, char *buf)
18905 +{
18906 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18907 + return sprintf(buf, "Mode Change Ready Timer Enable = 0x%x\n",
18908 + dwc_otg_get_mode_ch_tim(otg_dev->core_if));
18909 +}
18910 +
18911 +/**
18912 + * Set the Mode Change Ready Timer status
18913 + */
18914 +static ssize_t mode_ch_tim_en_store(struct device *_dev,
18915 + struct device_attribute *attr,
18916 + const char *buf, size_t count)
18917 +{
18918 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18919 + uint32_t in = simple_strtoul(buf, NULL, 16);
18920 + dwc_otg_set_mode_ch_tim(otg_dev->core_if, in);
18921 + return count;
18922 +}
18923 +
18924 +DEVICE_ATTR(mode_ch_tim_en, 0644, mode_ch_tim_en_show, mode_ch_tim_en_store);
18925 +
18926 +/**
18927 + * Show the value of HFIR Frame Interval bitfield
18928 + */
18929 +static ssize_t fr_interval_show(struct device *_dev,
18930 + struct device_attribute *attr, char *buf)
18931 +{
18932 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18933 + return sprintf(buf, "Frame Interval = 0x%x\n",
18934 + dwc_otg_get_fr_interval(otg_dev->core_if));
18935 +}
18936 +
18937 +/**
18938 + * Set the HFIR Frame Interval value
18939 + */
18940 +static ssize_t fr_interval_store(struct device *_dev,
18941 + struct device_attribute *attr,
18942 + const char *buf, size_t count)
18943 +{
18944 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18945 + uint32_t in = simple_strtoul(buf, NULL, 10);
18946 + dwc_otg_set_fr_interval(otg_dev->core_if, in);
18947 + return count;
18948 +}
18949 +
18950 +DEVICE_ATTR(fr_interval, 0644, fr_interval_show, fr_interval_store);
18951 +
18952 +/**
18953 + * Show the status of Remote Wakeup.
18954 + */
18955 +static ssize_t remote_wakeup_show(struct device *_dev,
18956 + struct device_attribute *attr, char *buf)
18957 +{
18958 +#ifndef DWC_HOST_ONLY
18959 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18960 +
18961 + return sprintf(buf,
18962 + "Remote Wakeup Sig = %d Enabled = %d LPM Remote Wakeup = %d\n",
18963 + dwc_otg_get_remotewakesig(otg_dev->core_if),
18964 + dwc_otg_pcd_get_rmwkup_enable(otg_dev->pcd),
18965 + dwc_otg_get_lpm_remotewakeenabled(otg_dev->core_if));
18966 +#else
18967 + return sprintf(buf, "Host Only Mode!\n");
18968 +#endif /* DWC_HOST_ONLY */
18969 +}
18970 +
18971 +/**
18972 + * Initiate a remote wakeup of the host. The Device control register
18973 + * Remote Wakeup Signal bit is written if the PCD Remote wakeup enable
18974 + * flag is set.
18975 + *
18976 + */
18977 +static ssize_t remote_wakeup_store(struct device *_dev,
18978 + struct device_attribute *attr,
18979 + const char *buf, size_t count)
18980 +{
18981 +#ifndef DWC_HOST_ONLY
18982 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
18983 + uint32_t val = simple_strtoul(buf, NULL, 16);
18984 +
18985 + if (val & 1) {
18986 + dwc_otg_pcd_remote_wakeup(otg_dev->pcd, 1);
18987 + } else {
18988 + dwc_otg_pcd_remote_wakeup(otg_dev->pcd, 0);
18989 + }
18990 +#endif /* DWC_HOST_ONLY */
18991 + return count;
18992 +}
18993 +
18994 +DEVICE_ATTR(remote_wakeup, S_IRUGO | S_IWUSR, remote_wakeup_show,
18995 + remote_wakeup_store);
18996 +
18997 +/**
18998 + * Show the whether core is hibernated or not.
18999 + */
19000 +static ssize_t rem_wakeup_pwrdn_show(struct device *_dev,
19001 + struct device_attribute *attr, char *buf)
19002 +{
19003 +#ifndef DWC_HOST_ONLY
19004 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
19005 +
19006 + if (dwc_otg_get_core_state(otg_dev->core_if)) {
19007 + DWC_PRINTF("Core is in hibernation\n");
19008 + } else {
19009 + DWC_PRINTF("Core is not in hibernation\n");
19010 + }
19011 +#endif /* DWC_HOST_ONLY */
19012 + return 0;
19013 +}
19014 +
19015 +extern int dwc_otg_device_hibernation_restore(dwc_otg_core_if_t * core_if,
19016 + int rem_wakeup, int reset);
19017 +
19018 +/**
19019 + * Initiate a remote wakeup of the device to exit from hibernation.
19020 + */
19021 +static ssize_t rem_wakeup_pwrdn_store(struct device *_dev,
19022 + struct device_attribute *attr,
19023 + const char *buf, size_t count)
19024 +{
19025 +#ifndef DWC_HOST_ONLY
19026 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
19027 + dwc_otg_device_hibernation_restore(otg_dev->core_if, 1, 0);
19028 +#endif
19029 + return count;
19030 +}
19031 +
19032 +DEVICE_ATTR(rem_wakeup_pwrdn, S_IRUGO | S_IWUSR, rem_wakeup_pwrdn_show,
19033 + rem_wakeup_pwrdn_store);
19034 +
19035 +static ssize_t disconnect_us(struct device *_dev,
19036 + struct device_attribute *attr,
19037 + const char *buf, size_t count)
19038 +{
19039 +
19040 +#ifndef DWC_HOST_ONLY
19041 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
19042 + uint32_t val = simple_strtoul(buf, NULL, 16);
19043 + DWC_PRINTF("The Passed value is %04x\n", val);
19044 +
19045 + dwc_otg_pcd_disconnect_us(otg_dev->pcd, 50);
19046 +
19047 +#endif /* DWC_HOST_ONLY */
19048 + return count;
19049 +}
19050 +
19051 +DEVICE_ATTR(disconnect_us, S_IWUSR, 0, disconnect_us);
19052 +
19053 +/**
19054 + * Dump global registers and either host or device registers (depending on the
19055 + * current mode of the core).
19056 + */
19057 +static ssize_t regdump_show(struct device *_dev,
19058 + struct device_attribute *attr, char *buf)
19059 +{
19060 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
19061 +
19062 + dwc_otg_dump_global_registers(otg_dev->core_if);
19063 + if (dwc_otg_is_host_mode(otg_dev->core_if)) {
19064 + dwc_otg_dump_host_registers(otg_dev->core_if);
19065 + } else {
19066 + dwc_otg_dump_dev_registers(otg_dev->core_if);
19067 +
19068 + }
19069 + return sprintf(buf, "Register Dump\n");
19070 +}
19071 +
19072 +DEVICE_ATTR(regdump, S_IRUGO, regdump_show, 0);
19073 +
19074 +/**
19075 + * Dump global registers and either host or device registers (depending on the
19076 + * current mode of the core).
19077 + */
19078 +static ssize_t spramdump_show(struct device *_dev,
19079 + struct device_attribute *attr, char *buf)
19080 +{
19081 +#if 0
19082 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
19083 +
19084 + dwc_otg_dump_spram(otg_dev->core_if);
19085 +#endif
19086 +
19087 + return sprintf(buf, "SPRAM Dump\n");
19088 +}
19089 +
19090 +DEVICE_ATTR(spramdump, S_IRUGO, spramdump_show, 0);
19091 +
19092 +/**
19093 + * Dump the current hcd state.
19094 + */
19095 +static ssize_t hcddump_show(struct device *_dev,
19096 + struct device_attribute *attr, char *buf)
19097 +{
19098 +#ifndef DWC_DEVICE_ONLY
19099 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
19100 + dwc_otg_hcd_dump_state(otg_dev->hcd);
19101 +#endif /* DWC_DEVICE_ONLY */
19102 + return sprintf(buf, "HCD Dump\n");
19103 +}
19104 +
19105 +DEVICE_ATTR(hcddump, S_IRUGO, hcddump_show, 0);
19106 +
19107 +/**
19108 + * Dump the average frame remaining at SOF. This can be used to
19109 + * determine average interrupt latency. Frame remaining is also shown for
19110 + * start transfer and two additional sample points.
19111 + */
19112 +static ssize_t hcd_frrem_show(struct device *_dev,
19113 + struct device_attribute *attr, char *buf)
19114 +{
19115 +#ifndef DWC_DEVICE_ONLY
19116 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
19117 +
19118 + dwc_otg_hcd_dump_frrem(otg_dev->hcd);
19119 +#endif /* DWC_DEVICE_ONLY */
19120 + return sprintf(buf, "HCD Dump Frame Remaining\n");
19121 +}
19122 +
19123 +DEVICE_ATTR(hcd_frrem, S_IRUGO, hcd_frrem_show, 0);
19124 +
19125 +/**
19126 + * Displays the time required to read the GNPTXFSIZ register many times (the
19127 + * output shows the number of times the register is read).
19128 + */
19129 +#define RW_REG_COUNT 10000000
19130 +#define MSEC_PER_JIFFIE 1000/HZ
19131 +static ssize_t rd_reg_test_show(struct device *_dev,
19132 + struct device_attribute *attr, char *buf)
19133 +{
19134 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
19135 + int i;
19136 + int time;
19137 + int start_jiffies;
19138 +
19139 + printk("HZ %d, MSEC_PER_JIFFIE %d, loops_per_jiffy %lu\n",
19140 + HZ, MSEC_PER_JIFFIE, loops_per_jiffy);
19141 + start_jiffies = jiffies;
19142 + for (i = 0; i < RW_REG_COUNT; i++) {
19143 + dwc_otg_get_gnptxfsiz(otg_dev->core_if);
19144 + }
19145 + time = jiffies - start_jiffies;
19146 + return sprintf(buf,
19147 + "Time to read GNPTXFSIZ reg %d times: %d msecs (%d jiffies)\n",
19148 + RW_REG_COUNT, time * MSEC_PER_JIFFIE, time);
19149 +}
19150 +
19151 +DEVICE_ATTR(rd_reg_test, S_IRUGO, rd_reg_test_show, 0);
19152 +
19153 +/**
19154 + * Displays the time required to write the GNPTXFSIZ register many times (the
19155 + * output shows the number of times the register is written).
19156 + */
19157 +static ssize_t wr_reg_test_show(struct device *_dev,
19158 + struct device_attribute *attr, char *buf)
19159 +{
19160 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
19161 + uint32_t reg_val;
19162 + int i;
19163 + int time;
19164 + int start_jiffies;
19165 +
19166 + printk("HZ %d, MSEC_PER_JIFFIE %d, loops_per_jiffy %lu\n",
19167 + HZ, MSEC_PER_JIFFIE, loops_per_jiffy);
19168 + reg_val = dwc_otg_get_gnptxfsiz(otg_dev->core_if);
19169 + start_jiffies = jiffies;
19170 + for (i = 0; i < RW_REG_COUNT; i++) {
19171 + dwc_otg_set_gnptxfsiz(otg_dev->core_if, reg_val);
19172 + }
19173 + time = jiffies - start_jiffies;
19174 + return sprintf(buf,
19175 + "Time to write GNPTXFSIZ reg %d times: %d msecs (%d jiffies)\n",
19176 + RW_REG_COUNT, time * MSEC_PER_JIFFIE, time);
19177 +}
19178 +
19179 +DEVICE_ATTR(wr_reg_test, S_IRUGO, wr_reg_test_show, 0);
19180 +
19181 +#ifdef CONFIG_USB_DWC_OTG_LPM
19182 +
19183 +/**
19184 +* Show the lpm_response attribute.
19185 +*/
19186 +static ssize_t lpmresp_show(struct device *_dev,
19187 + struct device_attribute *attr, char *buf)
19188 +{
19189 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
19190 +
19191 + if (!dwc_otg_get_param_lpm_enable(otg_dev->core_if))
19192 + return sprintf(buf, "** LPM is DISABLED **\n");
19193 +
19194 + if (!dwc_otg_is_device_mode(otg_dev->core_if)) {
19195 + return sprintf(buf, "** Current mode is not device mode\n");
19196 + }
19197 + return sprintf(buf, "lpm_response = %d\n",
19198 + dwc_otg_get_lpmresponse(otg_dev->core_if));
19199 +}
19200 +
19201 +/**
19202 +* Store the lpm_response attribute.
19203 +*/
19204 +static ssize_t lpmresp_store(struct device *_dev,
19205 + struct device_attribute *attr,
19206 + const char *buf, size_t count)
19207 +{
19208 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
19209 + uint32_t val = simple_strtoul(buf, NULL, 16);
19210 +
19211 + if (!dwc_otg_get_param_lpm_enable(otg_dev->core_if)) {
19212 + return 0;
19213 + }
19214 +
19215 + if (!dwc_otg_is_device_mode(otg_dev->core_if)) {
19216 + return 0;
19217 + }
19218 +
19219 + dwc_otg_set_lpmresponse(otg_dev->core_if, val);
19220 + return count;
19221 +}
19222 +
19223 +DEVICE_ATTR(lpm_response, S_IRUGO | S_IWUSR, lpmresp_show, lpmresp_store);
19224 +
19225 +/**
19226 +* Show the sleep_status attribute.
19227 +*/
19228 +static ssize_t sleepstatus_show(struct device *_dev,
19229 + struct device_attribute *attr, char *buf)
19230 +{
19231 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
19232 + return sprintf(buf, "Sleep Status = %d\n",
19233 + dwc_otg_get_lpm_portsleepstatus(otg_dev->core_if));
19234 +}
19235 +
19236 +/**
19237 + * Store the sleep_status attribure.
19238 + */
19239 +static ssize_t sleepstatus_store(struct device *_dev,
19240 + struct device_attribute *attr,
19241 + const char *buf, size_t count)
19242 +{
19243 + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
19244 + dwc_otg_core_if_t *core_if = otg_dev->core_if;
19245 +
19246 + if (dwc_otg_get_lpm_portsleepstatus(otg_dev->core_if)) {
19247 + if (dwc_otg_is_host_mode(core_if)) {
19248 +
19249 + DWC_PRINTF("Host initiated resume\n");
19250 + dwc_otg_set_prtresume(otg_dev->core_if, 1);
19251 + }
19252 + }
19253 +
19254 + return count;
19255 +}
19256 +
19257 +DEVICE_ATTR(sleep_status, S_IRUGO | S_IWUSR, sleepstatus_show,
19258 + sleepstatus_store);
19259 +
19260 +#endif /* CONFIG_USB_DWC_OTG_LPM_ENABLE */
19261 +
19262 +/**@}*/
19263 +
19264 +/**
19265 + * Create the device files
19266 + */
19267 +void dwc_otg_attr_create(
19268 +#ifdef LM_INTERFACE
19269 + struct lm_device *dev
19270 +#elif defined(PCI_INTERFACE)
19271 + struct pci_dev *dev
19272 +#elif defined(PLATFORM_INTERFACE)
19273 + struct platform_device *dev
19274 +#endif
19275 + )
19276 +{
19277 + int error;
19278 +
19279 + error = device_create_file(&dev->dev, &dev_attr_regoffset);
19280 + error = device_create_file(&dev->dev, &dev_attr_regvalue);
19281 + error = device_create_file(&dev->dev, &dev_attr_mode);
19282 + error = device_create_file(&dev->dev, &dev_attr_hnpcapable);
19283 + error = device_create_file(&dev->dev, &dev_attr_srpcapable);
19284 + error = device_create_file(&dev->dev, &dev_attr_hsic_connect);
19285 + error = device_create_file(&dev->dev, &dev_attr_inv_sel_hsic);
19286 + error = device_create_file(&dev->dev, &dev_attr_hnp);
19287 + error = device_create_file(&dev->dev, &dev_attr_srp);
19288 + error = device_create_file(&dev->dev, &dev_attr_buspower);
19289 + error = device_create_file(&dev->dev, &dev_attr_bussuspend);
19290 + error = device_create_file(&dev->dev, &dev_attr_mode_ch_tim_en);
19291 + error = device_create_file(&dev->dev, &dev_attr_fr_interval);
19292 + error = device_create_file(&dev->dev, &dev_attr_busconnected);
19293 + error = device_create_file(&dev->dev, &dev_attr_gotgctl);
19294 + error = device_create_file(&dev->dev, &dev_attr_gusbcfg);
19295 + error = device_create_file(&dev->dev, &dev_attr_grxfsiz);
19296 + error = device_create_file(&dev->dev, &dev_attr_gnptxfsiz);
19297 + error = device_create_file(&dev->dev, &dev_attr_gpvndctl);
19298 + error = device_create_file(&dev->dev, &dev_attr_ggpio);
19299 + error = device_create_file(&dev->dev, &dev_attr_guid);
19300 + error = device_create_file(&dev->dev, &dev_attr_gsnpsid);
19301 + error = device_create_file(&dev->dev, &dev_attr_devspeed);
19302 + error = device_create_file(&dev->dev, &dev_attr_enumspeed);
19303 + error = device_create_file(&dev->dev, &dev_attr_hptxfsiz);
19304 + error = device_create_file(&dev->dev, &dev_attr_hprt0);
19305 + error = device_create_file(&dev->dev, &dev_attr_remote_wakeup);
19306 + error = device_create_file(&dev->dev, &dev_attr_rem_wakeup_pwrdn);
19307 + error = device_create_file(&dev->dev, &dev_attr_disconnect_us);
19308 + error = device_create_file(&dev->dev, &dev_attr_regdump);
19309 + error = device_create_file(&dev->dev, &dev_attr_spramdump);
19310 + error = device_create_file(&dev->dev, &dev_attr_hcddump);
19311 + error = device_create_file(&dev->dev, &dev_attr_hcd_frrem);
19312 + error = device_create_file(&dev->dev, &dev_attr_rd_reg_test);
19313 + error = device_create_file(&dev->dev, &dev_attr_wr_reg_test);
19314 +#ifdef CONFIG_USB_DWC_OTG_LPM
19315 + error = device_create_file(&dev->dev, &dev_attr_lpm_response);
19316 + error = device_create_file(&dev->dev, &dev_attr_sleep_status);
19317 +#endif
19318 +}
19319 +
19320 +/**
19321 + * Remove the device files
19322 + */
19323 +void dwc_otg_attr_remove(
19324 +#ifdef LM_INTERFACE
19325 + struct lm_device *dev
19326 +#elif defined(PCI_INTERFACE)
19327 + struct pci_dev *dev
19328 +#elif defined(PLATFORM_INTERFACE)
19329 + struct platform_device *dev
19330 +#endif
19331 + )
19332 +{
19333 + device_remove_file(&dev->dev, &dev_attr_regoffset);
19334 + device_remove_file(&dev->dev, &dev_attr_regvalue);
19335 + device_remove_file(&dev->dev, &dev_attr_mode);
19336 + device_remove_file(&dev->dev, &dev_attr_hnpcapable);
19337 + device_remove_file(&dev->dev, &dev_attr_srpcapable);
19338 + device_remove_file(&dev->dev, &dev_attr_hsic_connect);
19339 + device_remove_file(&dev->dev, &dev_attr_inv_sel_hsic);
19340 + device_remove_file(&dev->dev, &dev_attr_hnp);
19341 + device_remove_file(&dev->dev, &dev_attr_srp);
19342 + device_remove_file(&dev->dev, &dev_attr_buspower);
19343 + device_remove_file(&dev->dev, &dev_attr_bussuspend);
19344 + device_remove_file(&dev->dev, &dev_attr_mode_ch_tim_en);
19345 + device_remove_file(&dev->dev, &dev_attr_fr_interval);
19346 + device_remove_file(&dev->dev, &dev_attr_busconnected);
19347 + device_remove_file(&dev->dev, &dev_attr_gotgctl);
19348 + device_remove_file(&dev->dev, &dev_attr_gusbcfg);
19349 + device_remove_file(&dev->dev, &dev_attr_grxfsiz);
19350 + device_remove_file(&dev->dev, &dev_attr_gnptxfsiz);
19351 + device_remove_file(&dev->dev, &dev_attr_gpvndctl);
19352 + device_remove_file(&dev->dev, &dev_attr_ggpio);
19353 + device_remove_file(&dev->dev, &dev_attr_guid);
19354 + device_remove_file(&dev->dev, &dev_attr_gsnpsid);
19355 + device_remove_file(&dev->dev, &dev_attr_devspeed);
19356 + device_remove_file(&dev->dev, &dev_attr_enumspeed);
19357 + device_remove_file(&dev->dev, &dev_attr_hptxfsiz);
19358 + device_remove_file(&dev->dev, &dev_attr_hprt0);
19359 + device_remove_file(&dev->dev, &dev_attr_remote_wakeup);
19360 + device_remove_file(&dev->dev, &dev_attr_rem_wakeup_pwrdn);
19361 + device_remove_file(&dev->dev, &dev_attr_disconnect_us);
19362 + device_remove_file(&dev->dev, &dev_attr_regdump);
19363 + device_remove_file(&dev->dev, &dev_attr_spramdump);
19364 + device_remove_file(&dev->dev, &dev_attr_hcddump);
19365 + device_remove_file(&dev->dev, &dev_attr_hcd_frrem);
19366 + device_remove_file(&dev->dev, &dev_attr_rd_reg_test);
19367 + device_remove_file(&dev->dev, &dev_attr_wr_reg_test);
19368 +#ifdef CONFIG_USB_DWC_OTG_LPM
19369 + device_remove_file(&dev->dev, &dev_attr_lpm_response);
19370 + device_remove_file(&dev->dev, &dev_attr_sleep_status);
19371 +#endif
19372 +}
19373 --- /dev/null
19374 +++ b/drivers/usb/host/dwc_otg/dwc_otg_attr.h
19375 @@ -0,0 +1,89 @@
19376 +/* ==========================================================================
19377 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_attr.h $
19378 + * $Revision: #13 $
19379 + * $Date: 2010/06/21 $
19380 + * $Change: 1532021 $
19381 + *
19382 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
19383 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
19384 + * otherwise expressly agreed to in writing between Synopsys and you.
19385 + *
19386 + * The Software IS NOT an item of Licensed Software or Licensed Product under
19387 + * any End User Software License Agreement or Agreement for Licensed Product
19388 + * with Synopsys or any supplement thereto. You are permitted to use and
19389 + * redistribute this Software in source and binary forms, with or without
19390 + * modification, provided that redistributions of source code must retain this
19391 + * notice. You may not view, use, disclose, copy or distribute this file or
19392 + * any information contained herein except pursuant to this license grant from
19393 + * Synopsys. If you do not agree with this notice, including the disclaimer
19394 + * below, then you are not authorized to use the Software.
19395 + *
19396 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
19397 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19398 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19399 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
19400 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19401 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19402 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
19403 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
19404 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
19405 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
19406 + * DAMAGE.
19407 + * ========================================================================== */
19408 +
19409 +#if !defined(__DWC_OTG_ATTR_H__)
19410 +#define __DWC_OTG_ATTR_H__
19411 +
19412 +/** @file
19413 + * This file contains the interface to the Linux device attributes.
19414 + */
19415 +extern struct device_attribute dev_attr_regoffset;
19416 +extern struct device_attribute dev_attr_regvalue;
19417 +
19418 +extern struct device_attribute dev_attr_mode;
19419 +extern struct device_attribute dev_attr_hnpcapable;
19420 +extern struct device_attribute dev_attr_srpcapable;
19421 +extern struct device_attribute dev_attr_hnp;
19422 +extern struct device_attribute dev_attr_srp;
19423 +extern struct device_attribute dev_attr_buspower;
19424 +extern struct device_attribute dev_attr_bussuspend;
19425 +extern struct device_attribute dev_attr_mode_ch_tim_en;
19426 +extern struct device_attribute dev_attr_fr_interval;
19427 +extern struct device_attribute dev_attr_busconnected;
19428 +extern struct device_attribute dev_attr_gotgctl;
19429 +extern struct device_attribute dev_attr_gusbcfg;
19430 +extern struct device_attribute dev_attr_grxfsiz;
19431 +extern struct device_attribute dev_attr_gnptxfsiz;
19432 +extern struct device_attribute dev_attr_gpvndctl;
19433 +extern struct device_attribute dev_attr_ggpio;
19434 +extern struct device_attribute dev_attr_guid;
19435 +extern struct device_attribute dev_attr_gsnpsid;
19436 +extern struct device_attribute dev_attr_devspeed;
19437 +extern struct device_attribute dev_attr_enumspeed;
19438 +extern struct device_attribute dev_attr_hptxfsiz;
19439 +extern struct device_attribute dev_attr_hprt0;
19440 +#ifdef CONFIG_USB_DWC_OTG_LPM
19441 +extern struct device_attribute dev_attr_lpm_response;
19442 +extern struct device_attribute devi_attr_sleep_status;
19443 +#endif
19444 +
19445 +void dwc_otg_attr_create(
19446 +#ifdef LM_INTERFACE
19447 + struct lm_device *dev
19448 +#elif defined(PCI_INTERFACE)
19449 + struct pci_dev *dev
19450 +#elif defined(PLATFORM_INTERFACE)
19451 + struct platform_device *dev
19452 +#endif
19453 + );
19454 +
19455 +void dwc_otg_attr_remove(
19456 +#ifdef LM_INTERFACE
19457 + struct lm_device *dev
19458 +#elif defined(PCI_INTERFACE)
19459 + struct pci_dev *dev
19460 +#elif defined(PLATFORM_INTERFACE)
19461 + struct platform_device *dev
19462 +#endif
19463 + );
19464 +#endif
19465 --- /dev/null
19466 +++ b/drivers/usb/host/dwc_otg/dwc_otg_cfi.c
19467 @@ -0,0 +1,1876 @@
19468 +/* ==========================================================================
19469 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
19470 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
19471 + * otherwise expressly agreed to in writing between Synopsys and you.
19472 + *
19473 + * The Software IS NOT an item of Licensed Software or Licensed Product under
19474 + * any End User Software License Agreement or Agreement for Licensed Product
19475 + * with Synopsys or any supplement thereto. You are permitted to use and
19476 + * redistribute this Software in source and binary forms, with or without
19477 + * modification, provided that redistributions of source code must retain this
19478 + * notice. You may not view, use, disclose, copy or distribute this file or
19479 + * any information contained herein except pursuant to this license grant from
19480 + * Synopsys. If you do not agree with this notice, including the disclaimer
19481 + * below, then you are not authorized to use the Software.
19482 + *
19483 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
19484 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19485 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19486 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
19487 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19488 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19489 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
19490 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
19491 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
19492 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
19493 + * DAMAGE.
19494 + * ========================================================================== */
19495 +
19496 +/** @file
19497 + *
19498 + * This file contains the most of the CFI(Core Feature Interface)
19499 + * implementation for the OTG.
19500 + */
19501 +
19502 +#ifdef DWC_UTE_CFI
19503 +
19504 +#include "dwc_otg_pcd.h"
19505 +#include "dwc_otg_cfi.h"
19506 +
19507 +/** This definition should actually migrate to the Portability Library */
19508 +#define DWC_CONSTANT_CPU_TO_LE16(x) (x)
19509 +
19510 +extern dwc_otg_pcd_ep_t *get_ep_by_addr(dwc_otg_pcd_t * pcd, u16 wIndex);
19511 +
19512 +static int cfi_core_features_buf(uint8_t * buf, uint16_t buflen);
19513 +static int cfi_get_feature_value(uint8_t * buf, uint16_t buflen,
19514 + struct dwc_otg_pcd *pcd,
19515 + struct cfi_usb_ctrlrequest *ctrl_req);
19516 +static int cfi_set_feature_value(struct dwc_otg_pcd *pcd);
19517 +static int cfi_ep_get_sg_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
19518 + struct cfi_usb_ctrlrequest *req);
19519 +static int cfi_ep_get_concat_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
19520 + struct cfi_usb_ctrlrequest *req);
19521 +static int cfi_ep_get_align_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
19522 + struct cfi_usb_ctrlrequest *req);
19523 +static int cfi_preproc_reset(struct dwc_otg_pcd *pcd,
19524 + struct cfi_usb_ctrlrequest *req);
19525 +static void cfi_free_ep_bs_dyn_data(cfi_ep_t * cfiep);
19526 +
19527 +static uint16_t get_dfifo_size(dwc_otg_core_if_t * core_if);
19528 +static int32_t get_rxfifo_size(dwc_otg_core_if_t * core_if, uint16_t wValue);
19529 +static int32_t get_txfifo_size(struct dwc_otg_pcd *pcd, uint16_t wValue);
19530 +
19531 +static uint8_t resize_fifos(dwc_otg_core_if_t * core_if);
19532 +
19533 +/** This is the header of the all features descriptor */
19534 +static cfi_all_features_header_t all_props_desc_header = {
19535 + .wVersion = DWC_CONSTANT_CPU_TO_LE16(0x100),
19536 + .wCoreID = DWC_CONSTANT_CPU_TO_LE16(CFI_CORE_ID_OTG),
19537 + .wNumFeatures = DWC_CONSTANT_CPU_TO_LE16(9),
19538 +};
19539 +
19540 +/** This is an array of statically allocated feature descriptors */
19541 +static cfi_feature_desc_header_t prop_descs[] = {
19542 +
19543 + /* FT_ID_DMA_MODE */
19544 + {
19545 + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DMA_MODE),
19546 + .bmAttributes = CFI_FEATURE_ATTR_RW,
19547 + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(1),
19548 + },
19549 +
19550 + /* FT_ID_DMA_BUFFER_SETUP */
19551 + {
19552 + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DMA_BUFFER_SETUP),
19553 + .bmAttributes = CFI_FEATURE_ATTR_RW,
19554 + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(6),
19555 + },
19556 +
19557 + /* FT_ID_DMA_BUFF_ALIGN */
19558 + {
19559 + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DMA_BUFF_ALIGN),
19560 + .bmAttributes = CFI_FEATURE_ATTR_RW,
19561 + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(2),
19562 + },
19563 +
19564 + /* FT_ID_DMA_CONCAT_SETUP */
19565 + {
19566 + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DMA_CONCAT_SETUP),
19567 + .bmAttributes = CFI_FEATURE_ATTR_RW,
19568 + //.wDataLength = DWC_CONSTANT_CPU_TO_LE16(6),
19569 + },
19570 +
19571 + /* FT_ID_DMA_CIRCULAR */
19572 + {
19573 + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DMA_CIRCULAR),
19574 + .bmAttributes = CFI_FEATURE_ATTR_RW,
19575 + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(6),
19576 + },
19577 +
19578 + /* FT_ID_THRESHOLD_SETUP */
19579 + {
19580 + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_THRESHOLD_SETUP),
19581 + .bmAttributes = CFI_FEATURE_ATTR_RW,
19582 + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(6),
19583 + },
19584 +
19585 + /* FT_ID_DFIFO_DEPTH */
19586 + {
19587 + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DFIFO_DEPTH),
19588 + .bmAttributes = CFI_FEATURE_ATTR_RO,
19589 + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(2),
19590 + },
19591 +
19592 + /* FT_ID_TX_FIFO_DEPTH */
19593 + {
19594 + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_TX_FIFO_DEPTH),
19595 + .bmAttributes = CFI_FEATURE_ATTR_RW,
19596 + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(2),
19597 + },
19598 +
19599 + /* FT_ID_RX_FIFO_DEPTH */
19600 + {
19601 + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_RX_FIFO_DEPTH),
19602 + .bmAttributes = CFI_FEATURE_ATTR_RW,
19603 + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(2),
19604 + }
19605 +};
19606 +
19607 +/** The table of feature names */
19608 +cfi_string_t prop_name_table[] = {
19609 + {FT_ID_DMA_MODE, "dma_mode"},
19610 + {FT_ID_DMA_BUFFER_SETUP, "buffer_setup"},
19611 + {FT_ID_DMA_BUFF_ALIGN, "buffer_align"},
19612 + {FT_ID_DMA_CONCAT_SETUP, "concat_setup"},
19613 + {FT_ID_DMA_CIRCULAR, "buffer_circular"},
19614 + {FT_ID_THRESHOLD_SETUP, "threshold_setup"},
19615 + {FT_ID_DFIFO_DEPTH, "dfifo_depth"},
19616 + {FT_ID_TX_FIFO_DEPTH, "txfifo_depth"},
19617 + {FT_ID_RX_FIFO_DEPTH, "rxfifo_depth"},
19618 + {}
19619 +};
19620 +
19621 +/************************************************************************/
19622 +
19623 +/**
19624 + * Returns the name of the feature by its ID
19625 + * or NULL if no featute ID matches.
19626 + *
19627 + */
19628 +const uint8_t *get_prop_name(uint16_t prop_id, int *len)
19629 +{
19630 + cfi_string_t *pstr;
19631 + *len = 0;
19632 +
19633 + for (pstr = prop_name_table; pstr && pstr->s; pstr++) {
19634 + if (pstr->id == prop_id) {
19635 + *len = DWC_STRLEN(pstr->s);
19636 + return pstr->s;
19637 + }
19638 + }
19639 + return NULL;
19640 +}
19641 +
19642 +/**
19643 + * This function handles all CFI specific control requests.
19644 + *
19645 + * Return a negative value to stall the DCE.
19646 + */
19647 +int cfi_setup(struct dwc_otg_pcd *pcd, struct cfi_usb_ctrlrequest *ctrl)
19648 +{
19649 + int retval = 0;
19650 + dwc_otg_pcd_ep_t *ep = NULL;
19651 + cfiobject_t *cfi = pcd->cfi;
19652 + struct dwc_otg_core_if *coreif = GET_CORE_IF(pcd);
19653 + uint16_t wLen = DWC_LE16_TO_CPU(&ctrl->wLength);
19654 + uint16_t wValue = DWC_LE16_TO_CPU(&ctrl->wValue);
19655 + uint16_t wIndex = DWC_LE16_TO_CPU(&ctrl->wIndex);
19656 + uint32_t regaddr = 0;
19657 + uint32_t regval = 0;
19658 +
19659 + /* Save this Control Request in the CFI object.
19660 + * The data field will be assigned in the data stage completion CB function.
19661 + */
19662 + cfi->ctrl_req = *ctrl;
19663 + cfi->ctrl_req.data = NULL;
19664 +
19665 + cfi->need_gadget_att = 0;
19666 + cfi->need_status_in_complete = 0;
19667 +
19668 + switch (ctrl->bRequest) {
19669 + case VEN_CORE_GET_FEATURES:
19670 + retval = cfi_core_features_buf(cfi->buf_in.buf, CFI_IN_BUF_LEN);
19671 + if (retval >= 0) {
19672 + //dump_msg(cfi->buf_in.buf, retval);
19673 + ep = &pcd->ep0;
19674 +
19675 + retval = min((uint16_t) retval, wLen);
19676 + /* Transfer this buffer to the host through the EP0-IN EP */
19677 + ep->dwc_ep.dma_addr = cfi->buf_in.addr;
19678 + ep->dwc_ep.start_xfer_buff = cfi->buf_in.buf;
19679 + ep->dwc_ep.xfer_buff = cfi->buf_in.buf;
19680 + ep->dwc_ep.xfer_len = retval;
19681 + ep->dwc_ep.xfer_count = 0;
19682 + ep->dwc_ep.sent_zlp = 0;
19683 + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
19684 +
19685 + pcd->ep0_pending = 1;
19686 + dwc_otg_ep0_start_transfer(coreif, &ep->dwc_ep);
19687 + }
19688 + retval = 0;
19689 + break;
19690 +
19691 + case VEN_CORE_GET_FEATURE:
19692 + CFI_INFO("VEN_CORE_GET_FEATURE\n");
19693 + retval = cfi_get_feature_value(cfi->buf_in.buf, CFI_IN_BUF_LEN,
19694 + pcd, ctrl);
19695 + if (retval >= 0) {
19696 + ep = &pcd->ep0;
19697 +
19698 + retval = min((uint16_t) retval, wLen);
19699 + /* Transfer this buffer to the host through the EP0-IN EP */
19700 + ep->dwc_ep.dma_addr = cfi->buf_in.addr;
19701 + ep->dwc_ep.start_xfer_buff = cfi->buf_in.buf;
19702 + ep->dwc_ep.xfer_buff = cfi->buf_in.buf;
19703 + ep->dwc_ep.xfer_len = retval;
19704 + ep->dwc_ep.xfer_count = 0;
19705 + ep->dwc_ep.sent_zlp = 0;
19706 + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
19707 +
19708 + pcd->ep0_pending = 1;
19709 + dwc_otg_ep0_start_transfer(coreif, &ep->dwc_ep);
19710 + }
19711 + CFI_INFO("VEN_CORE_GET_FEATURE=%d\n", retval);
19712 + dump_msg(cfi->buf_in.buf, retval);
19713 + break;
19714 +
19715 + case VEN_CORE_SET_FEATURE:
19716 + CFI_INFO("VEN_CORE_SET_FEATURE\n");
19717 + /* Set up an XFER to get the data stage of the control request,
19718 + * which is the new value of the feature to be modified.
19719 + */
19720 + ep = &pcd->ep0;
19721 + ep->dwc_ep.is_in = 0;
19722 + ep->dwc_ep.dma_addr = cfi->buf_out.addr;
19723 + ep->dwc_ep.start_xfer_buff = cfi->buf_out.buf;
19724 + ep->dwc_ep.xfer_buff = cfi->buf_out.buf;
19725 + ep->dwc_ep.xfer_len = wLen;
19726 + ep->dwc_ep.xfer_count = 0;
19727 + ep->dwc_ep.sent_zlp = 0;
19728 + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
19729 +
19730 + pcd->ep0_pending = 1;
19731 + /* Read the control write's data stage */
19732 + dwc_otg_ep0_start_transfer(coreif, &ep->dwc_ep);
19733 + retval = 0;
19734 + break;
19735 +
19736 + case VEN_CORE_RESET_FEATURES:
19737 + CFI_INFO("VEN_CORE_RESET_FEATURES\n");
19738 + cfi->need_gadget_att = 1;
19739 + cfi->need_status_in_complete = 1;
19740 + retval = cfi_preproc_reset(pcd, ctrl);
19741 + CFI_INFO("VEN_CORE_RESET_FEATURES = (%d)\n", retval);
19742 + break;
19743 +
19744 + case VEN_CORE_ACTIVATE_FEATURES:
19745 + CFI_INFO("VEN_CORE_ACTIVATE_FEATURES\n");
19746 + break;
19747 +
19748 + case VEN_CORE_READ_REGISTER:
19749 + CFI_INFO("VEN_CORE_READ_REGISTER\n");
19750 + /* wValue optionally contains the HI WORD of the register offset and
19751 + * wIndex contains the LOW WORD of the register offset
19752 + */
19753 + if (wValue == 0) {
19754 + /* @TODO - MAS - fix the access to the base field */
19755 + regaddr = 0;
19756 + //regaddr = (uint32_t) pcd->otg_dev->os_dep.base;
19757 + //GET_CORE_IF(pcd)->co
19758 + regaddr |= wIndex;
19759 + } else {
19760 + regaddr = (wValue << 16) | wIndex;
19761 + }
19762 +
19763 + /* Read a 32-bit value of the memory at the regaddr */
19764 + regval = DWC_READ_REG32((uint32_t *) regaddr);
19765 +
19766 + ep = &pcd->ep0;
19767 + dwc_memcpy(cfi->buf_in.buf, &regval, sizeof(uint32_t));
19768 + ep->dwc_ep.is_in = 1;
19769 + ep->dwc_ep.dma_addr = cfi->buf_in.addr;
19770 + ep->dwc_ep.start_xfer_buff = cfi->buf_in.buf;
19771 + ep->dwc_ep.xfer_buff = cfi->buf_in.buf;
19772 + ep->dwc_ep.xfer_len = wLen;
19773 + ep->dwc_ep.xfer_count = 0;
19774 + ep->dwc_ep.sent_zlp = 0;
19775 + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
19776 +
19777 + pcd->ep0_pending = 1;
19778 + dwc_otg_ep0_start_transfer(coreif, &ep->dwc_ep);
19779 + cfi->need_gadget_att = 0;
19780 + retval = 0;
19781 + break;
19782 +
19783 + case VEN_CORE_WRITE_REGISTER:
19784 + CFI_INFO("VEN_CORE_WRITE_REGISTER\n");
19785 + /* Set up an XFER to get the data stage of the control request,
19786 + * which is the new value of the register to be modified.
19787 + */
19788 + ep = &pcd->ep0;
19789 + ep->dwc_ep.is_in = 0;
19790 + ep->dwc_ep.dma_addr = cfi->buf_out.addr;
19791 + ep->dwc_ep.start_xfer_buff = cfi->buf_out.buf;
19792 + ep->dwc_ep.xfer_buff = cfi->buf_out.buf;
19793 + ep->dwc_ep.xfer_len = wLen;
19794 + ep->dwc_ep.xfer_count = 0;
19795 + ep->dwc_ep.sent_zlp = 0;
19796 + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
19797 +
19798 + pcd->ep0_pending = 1;
19799 + /* Read the control write's data stage */
19800 + dwc_otg_ep0_start_transfer(coreif, &ep->dwc_ep);
19801 + retval = 0;
19802 + break;
19803 +
19804 + default:
19805 + retval = -DWC_E_NOT_SUPPORTED;
19806 + break;
19807 + }
19808 +
19809 + return retval;
19810 +}
19811 +
19812 +/**
19813 + * This function prepares the core features descriptors and copies its
19814 + * raw representation into the buffer <buf>.
19815 + *
19816 + * The buffer structure is as follows:
19817 + * all_features_header (8 bytes)
19818 + * features_#1 (8 bytes + feature name string length)
19819 + * features_#2 (8 bytes + feature name string length)
19820 + * .....
19821 + * features_#n - where n=the total count of feature descriptors
19822 + */
19823 +static int cfi_core_features_buf(uint8_t * buf, uint16_t buflen)
19824 +{
19825 + cfi_feature_desc_header_t *prop_hdr = prop_descs;
19826 + cfi_feature_desc_header_t *prop;
19827 + cfi_all_features_header_t *all_props_hdr = &all_props_desc_header;
19828 + cfi_all_features_header_t *tmp;
19829 + uint8_t *tmpbuf = buf;
19830 + const uint8_t *pname = NULL;
19831 + int i, j, namelen = 0, totlen;
19832 +
19833 + /* Prepare and copy the core features into the buffer */
19834 + CFI_INFO("%s:\n", __func__);
19835 +
19836 + tmp = (cfi_all_features_header_t *) tmpbuf;
19837 + *tmp = *all_props_hdr;
19838 + tmpbuf += CFI_ALL_FEATURES_HDR_LEN;
19839 +
19840 + j = sizeof(prop_descs) / sizeof(cfi_all_features_header_t);
19841 + for (i = 0; i < j; i++, prop_hdr++) {
19842 + pname = get_prop_name(prop_hdr->wFeatureID, &namelen);
19843 + prop = (cfi_feature_desc_header_t *) tmpbuf;
19844 + *prop = *prop_hdr;
19845 +
19846 + prop->bNameLen = namelen;
19847 + prop->wLength =
19848 + DWC_CONSTANT_CPU_TO_LE16(CFI_FEATURE_DESC_HDR_LEN +
19849 + namelen);
19850 +
19851 + tmpbuf += CFI_FEATURE_DESC_HDR_LEN;
19852 + dwc_memcpy(tmpbuf, pname, namelen);
19853 + tmpbuf += namelen;
19854 + }
19855 +
19856 + totlen = tmpbuf - buf;
19857 +
19858 + if (totlen > 0) {
19859 + tmp = (cfi_all_features_header_t *) buf;
19860 + tmp->wTotalLen = DWC_CONSTANT_CPU_TO_LE16(totlen);
19861 + }
19862 +
19863 + return totlen;
19864 +}
19865 +
19866 +/**
19867 + * This function releases all the dynamic memory in the CFI object.
19868 + */
19869 +static void cfi_release(cfiobject_t * cfiobj)
19870 +{
19871 + cfi_ep_t *cfiep;
19872 + dwc_list_link_t *tmp;
19873 +
19874 + CFI_INFO("%s\n", __func__);
19875 +
19876 + if (cfiobj->buf_in.buf) {
19877 + DWC_DMA_FREE(CFI_IN_BUF_LEN, cfiobj->buf_in.buf,
19878 + cfiobj->buf_in.addr);
19879 + cfiobj->buf_in.buf = NULL;
19880 + }
19881 +
19882 + if (cfiobj->buf_out.buf) {
19883 + DWC_DMA_FREE(CFI_OUT_BUF_LEN, cfiobj->buf_out.buf,
19884 + cfiobj->buf_out.addr);
19885 + cfiobj->buf_out.buf = NULL;
19886 + }
19887 +
19888 + /* Free the Buffer Setup values for each EP */
19889 + //list_for_each_entry(cfiep, &cfiobj->active_eps, lh) {
19890 + DWC_LIST_FOREACH(tmp, &cfiobj->active_eps) {
19891 + cfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
19892 + cfi_free_ep_bs_dyn_data(cfiep);
19893 + }
19894 +}
19895 +
19896 +/**
19897 + * This function frees the dynamically allocated EP buffer setup data.
19898 + */
19899 +static void cfi_free_ep_bs_dyn_data(cfi_ep_t * cfiep)
19900 +{
19901 + if (cfiep->bm_sg) {
19902 + DWC_FREE(cfiep->bm_sg);
19903 + cfiep->bm_sg = NULL;
19904 + }
19905 +
19906 + if (cfiep->bm_align) {
19907 + DWC_FREE(cfiep->bm_align);
19908 + cfiep->bm_align = NULL;
19909 + }
19910 +
19911 + if (cfiep->bm_concat) {
19912 + if (NULL != cfiep->bm_concat->wTxBytes) {
19913 + DWC_FREE(cfiep->bm_concat->wTxBytes);
19914 + cfiep->bm_concat->wTxBytes = NULL;
19915 + }
19916 + DWC_FREE(cfiep->bm_concat);
19917 + cfiep->bm_concat = NULL;
19918 + }
19919 +}
19920 +
19921 +/**
19922 + * This function initializes the default values of the features
19923 + * for a specific endpoint and should be called only once when
19924 + * the EP is enabled first time.
19925 + */
19926 +static int cfi_ep_init_defaults(struct dwc_otg_pcd *pcd, cfi_ep_t * cfiep)
19927 +{
19928 + int retval = 0;
19929 +
19930 + cfiep->bm_sg = DWC_ALLOC(sizeof(ddma_sg_buffer_setup_t));
19931 + if (NULL == cfiep->bm_sg) {
19932 + CFI_INFO("Failed to allocate memory for SG feature value\n");
19933 + return -DWC_E_NO_MEMORY;
19934 + }
19935 + dwc_memset(cfiep->bm_sg, 0, sizeof(ddma_sg_buffer_setup_t));
19936 +
19937 + /* For the Concatenation feature's default value we do not allocate
19938 + * memory for the wTxBytes field - it will be done in the set_feature_value
19939 + * request handler.
19940 + */
19941 + cfiep->bm_concat = DWC_ALLOC(sizeof(ddma_concat_buffer_setup_t));
19942 + if (NULL == cfiep->bm_concat) {
19943 + CFI_INFO
19944 + ("Failed to allocate memory for CONCATENATION feature value\n");
19945 + DWC_FREE(cfiep->bm_sg);
19946 + return -DWC_E_NO_MEMORY;
19947 + }
19948 + dwc_memset(cfiep->bm_concat, 0, sizeof(ddma_concat_buffer_setup_t));
19949 +
19950 + cfiep->bm_align = DWC_ALLOC(sizeof(ddma_align_buffer_setup_t));
19951 + if (NULL == cfiep->bm_align) {
19952 + CFI_INFO
19953 + ("Failed to allocate memory for Alignment feature value\n");
19954 + DWC_FREE(cfiep->bm_sg);
19955 + DWC_FREE(cfiep->bm_concat);
19956 + return -DWC_E_NO_MEMORY;
19957 + }
19958 + dwc_memset(cfiep->bm_align, 0, sizeof(ddma_align_buffer_setup_t));
19959 +
19960 + return retval;
19961 +}
19962 +
19963 +/**
19964 + * The callback function that notifies the CFI on the activation of
19965 + * an endpoint in the PCD. The following steps are done in this function:
19966 + *
19967 + * Create a dynamically allocated cfi_ep_t object (a CFI wrapper to the PCD's
19968 + * active endpoint)
19969 + * Create MAX_DMA_DESCS_PER_EP count DMA Descriptors for the EP
19970 + * Set the Buffer Mode to standard
19971 + * Initialize the default values for all EP modes (SG, Circular, Concat, Align)
19972 + * Add the cfi_ep_t object to the list of active endpoints in the CFI object
19973 + */
19974 +static int cfi_ep_enable(struct cfiobject *cfi, struct dwc_otg_pcd *pcd,
19975 + struct dwc_otg_pcd_ep *ep)
19976 +{
19977 + cfi_ep_t *cfiep;
19978 + int retval = -DWC_E_NOT_SUPPORTED;
19979 +
19980 + CFI_INFO("%s: epname=%s; epnum=0x%02x\n", __func__,
19981 + "EP_" /*ep->ep.name */ , ep->desc->bEndpointAddress);
19982 + /* MAS - Check whether this endpoint already is in the list */
19983 + cfiep = get_cfi_ep_by_pcd_ep(cfi, ep);
19984 +
19985 + if (NULL == cfiep) {
19986 + /* Allocate a cfi_ep_t object */
19987 + cfiep = DWC_ALLOC(sizeof(cfi_ep_t));
19988 + if (NULL == cfiep) {
19989 + CFI_INFO
19990 + ("Unable to allocate memory for <cfiep> in function %s\n",
19991 + __func__);
19992 + return -DWC_E_NO_MEMORY;
19993 + }
19994 + dwc_memset(cfiep, 0, sizeof(cfi_ep_t));
19995 +
19996 + /* Save the dwc_otg_pcd_ep pointer in the cfiep object */
19997 + cfiep->ep = ep;
19998 +
19999 + /* Allocate the DMA Descriptors chain of MAX_DMA_DESCS_PER_EP count */
20000 + ep->dwc_ep.descs =
20001 + DWC_DMA_ALLOC(MAX_DMA_DESCS_PER_EP *
20002 + sizeof(dwc_otg_dma_desc_t),
20003 + &ep->dwc_ep.descs_dma_addr);
20004 +
20005 + if (NULL == ep->dwc_ep.descs) {
20006 + DWC_FREE(cfiep);
20007 + return -DWC_E_NO_MEMORY;
20008 + }
20009 +
20010 + DWC_LIST_INIT(&cfiep->lh);
20011 +
20012 + /* Set the buffer mode to BM_STANDARD. It will be modified
20013 + * when building descriptors for a specific buffer mode */
20014 + ep->dwc_ep.buff_mode = BM_STANDARD;
20015 +
20016 + /* Create and initialize the default values for this EP's Buffer modes */
20017 + if ((retval = cfi_ep_init_defaults(pcd, cfiep)) < 0)
20018 + return retval;
20019 +
20020 + /* Add the cfi_ep_t object to the CFI object's list of active endpoints */
20021 + DWC_LIST_INSERT_TAIL(&cfi->active_eps, &cfiep->lh);
20022 + retval = 0;
20023 + } else { /* The sought EP already is in the list */
20024 + CFI_INFO("%s: The sought EP already is in the list\n",
20025 + __func__);
20026 + }
20027 +
20028 + return retval;
20029 +}
20030 +
20031 +/**
20032 + * This function is called when the data stage of a 3-stage Control Write request
20033 + * is complete.
20034 + *
20035 + */
20036 +static int cfi_ctrl_write_complete(struct cfiobject *cfi,
20037 + struct dwc_otg_pcd *pcd)
20038 +{
20039 + uint32_t addr, reg_value;
20040 + uint16_t wIndex, wValue;
20041 + uint8_t bRequest;
20042 + uint8_t *buf = cfi->buf_out.buf;
20043 + //struct usb_ctrlrequest *ctrl_req = &cfi->ctrl_req_saved;
20044 + struct cfi_usb_ctrlrequest *ctrl_req = &cfi->ctrl_req;
20045 + int retval = -DWC_E_NOT_SUPPORTED;
20046 +
20047 + CFI_INFO("%s\n", __func__);
20048 +
20049 + bRequest = ctrl_req->bRequest;
20050 + wIndex = DWC_CONSTANT_CPU_TO_LE16(ctrl_req->wIndex);
20051 + wValue = DWC_CONSTANT_CPU_TO_LE16(ctrl_req->wValue);
20052 +
20053 + /*
20054 + * Save the pointer to the data stage in the ctrl_req's <data> field.
20055 + * The request should be already saved in the command stage by now.
20056 + */
20057 + ctrl_req->data = cfi->buf_out.buf;
20058 + cfi->need_status_in_complete = 0;
20059 + cfi->need_gadget_att = 0;
20060 +
20061 + switch (bRequest) {
20062 + case VEN_CORE_WRITE_REGISTER:
20063 + /* The buffer contains raw data of the new value for the register */
20064 + reg_value = *((uint32_t *) buf);
20065 + if (wValue == 0) {
20066 + addr = 0;
20067 + //addr = (uint32_t) pcd->otg_dev->os_dep.base;
20068 + addr += wIndex;
20069 + } else {
20070 + addr = (wValue << 16) | wIndex;
20071 + }
20072 +
20073 + //writel(reg_value, addr);
20074 +
20075 + retval = 0;
20076 + cfi->need_status_in_complete = 1;
20077 + break;
20078 +
20079 + case VEN_CORE_SET_FEATURE:
20080 + /* The buffer contains raw data of the new value of the feature */
20081 + retval = cfi_set_feature_value(pcd);
20082 + if (retval < 0)
20083 + return retval;
20084 +
20085 + cfi->need_status_in_complete = 1;
20086 + break;
20087 +
20088 + default:
20089 + break;
20090 + }
20091 +
20092 + return retval;
20093 +}
20094 +
20095 +/**
20096 + * This function builds the DMA descriptors for the SG buffer mode.
20097 + */
20098 +static void cfi_build_sg_descs(struct cfiobject *cfi, cfi_ep_t * cfiep,
20099 + dwc_otg_pcd_request_t * req)
20100 +{
20101 + struct dwc_otg_pcd_ep *ep = cfiep->ep;
20102 + ddma_sg_buffer_setup_t *sgval = cfiep->bm_sg;
20103 + struct dwc_otg_dma_desc *desc = cfiep->ep->dwc_ep.descs;
20104 + struct dwc_otg_dma_desc *desc_last = cfiep->ep->dwc_ep.descs;
20105 + dma_addr_t buff_addr = req->dma;
20106 + int i;
20107 + uint32_t txsize, off;
20108 +
20109 + txsize = sgval->wSize;
20110 + off = sgval->bOffset;
20111 +
20112 +// CFI_INFO("%s: %s TXSIZE=0x%08x; OFFSET=0x%08x\n",
20113 +// __func__, cfiep->ep->ep.name, txsize, off);
20114 +
20115 + for (i = 0; i < sgval->bCount; i++) {
20116 + desc->status.b.bs = BS_HOST_BUSY;
20117 + desc->buf = buff_addr;
20118 + desc->status.b.l = 0;
20119 + desc->status.b.ioc = 0;
20120 + desc->status.b.sp = 0;
20121 + desc->status.b.bytes = txsize;
20122 + desc->status.b.bs = BS_HOST_READY;
20123 +
20124 + /* Set the next address of the buffer */
20125 + buff_addr += txsize + off;
20126 + desc_last = desc;
20127 + desc++;
20128 + }
20129 +
20130 + /* Set the last, ioc and sp bits on the Last DMA Descriptor */
20131 + desc_last->status.b.l = 1;
20132 + desc_last->status.b.ioc = 1;
20133 + desc_last->status.b.sp = ep->dwc_ep.sent_zlp;
20134 + /* Save the last DMA descriptor pointer */
20135 + cfiep->dma_desc_last = desc_last;
20136 + cfiep->desc_count = sgval->bCount;
20137 +}
20138 +
20139 +/**
20140 + * This function builds the DMA descriptors for the Concatenation buffer mode.
20141 + */
20142 +static void cfi_build_concat_descs(struct cfiobject *cfi, cfi_ep_t * cfiep,
20143 + dwc_otg_pcd_request_t * req)
20144 +{
20145 + struct dwc_otg_pcd_ep *ep = cfiep->ep;
20146 + ddma_concat_buffer_setup_t *concatval = cfiep->bm_concat;
20147 + struct dwc_otg_dma_desc *desc = cfiep->ep->dwc_ep.descs;
20148 + struct dwc_otg_dma_desc *desc_last = cfiep->ep->dwc_ep.descs;
20149 + dma_addr_t buff_addr = req->dma;
20150 + int i;
20151 + uint16_t *txsize;
20152 +
20153 + txsize = concatval->wTxBytes;
20154 +
20155 + for (i = 0; i < concatval->hdr.bDescCount; i++) {
20156 + desc->buf = buff_addr;
20157 + desc->status.b.bs = BS_HOST_BUSY;
20158 + desc->status.b.l = 0;
20159 + desc->status.b.ioc = 0;
20160 + desc->status.b.sp = 0;
20161 + desc->status.b.bytes = *txsize;
20162 + desc->status.b.bs = BS_HOST_READY;
20163 +
20164 + txsize++;
20165 + /* Set the next address of the buffer */
20166 + buff_addr += UGETW(ep->desc->wMaxPacketSize);
20167 + desc_last = desc;
20168 + desc++;
20169 + }
20170 +
20171 + /* Set the last, ioc and sp bits on the Last DMA Descriptor */
20172 + desc_last->status.b.l = 1;
20173 + desc_last->status.b.ioc = 1;
20174 + desc_last->status.b.sp = ep->dwc_ep.sent_zlp;
20175 + cfiep->dma_desc_last = desc_last;
20176 + cfiep->desc_count = concatval->hdr.bDescCount;
20177 +}
20178 +
20179 +/**
20180 + * This function builds the DMA descriptors for the Circular buffer mode
20181 + */
20182 +static void cfi_build_circ_descs(struct cfiobject *cfi, cfi_ep_t * cfiep,
20183 + dwc_otg_pcd_request_t * req)
20184 +{
20185 + /* @todo: MAS - add implementation when this feature needs to be tested */
20186 +}
20187 +
20188 +/**
20189 + * This function builds the DMA descriptors for the Alignment buffer mode
20190 + */
20191 +static void cfi_build_align_descs(struct cfiobject *cfi, cfi_ep_t * cfiep,
20192 + dwc_otg_pcd_request_t * req)
20193 +{
20194 + struct dwc_otg_pcd_ep *ep = cfiep->ep;
20195 + ddma_align_buffer_setup_t *alignval = cfiep->bm_align;
20196 + struct dwc_otg_dma_desc *desc = cfiep->ep->dwc_ep.descs;
20197 + dma_addr_t buff_addr = req->dma;
20198 +
20199 + desc->status.b.bs = BS_HOST_BUSY;
20200 + desc->status.b.l = 1;
20201 + desc->status.b.ioc = 1;
20202 + desc->status.b.sp = ep->dwc_ep.sent_zlp;
20203 + desc->status.b.bytes = req->length;
20204 + /* Adjust the buffer alignment */
20205 + desc->buf = (buff_addr + alignval->bAlign);
20206 + desc->status.b.bs = BS_HOST_READY;
20207 + cfiep->dma_desc_last = desc;
20208 + cfiep->desc_count = 1;
20209 +}
20210 +
20211 +/**
20212 + * This function builds the DMA descriptors chain for different modes of the
20213 + * buffer setup of an endpoint.
20214 + */
20215 +static void cfi_build_descriptors(struct cfiobject *cfi,
20216 + struct dwc_otg_pcd *pcd,
20217 + struct dwc_otg_pcd_ep *ep,
20218 + dwc_otg_pcd_request_t * req)
20219 +{
20220 + cfi_ep_t *cfiep;
20221 +
20222 + /* Get the cfiep by the dwc_otg_pcd_ep */
20223 + cfiep = get_cfi_ep_by_pcd_ep(cfi, ep);
20224 + if (NULL == cfiep) {
20225 + CFI_INFO("%s: Unable to find a matching active endpoint\n",
20226 + __func__);
20227 + return;
20228 + }
20229 +
20230 + cfiep->xfer_len = req->length;
20231 +
20232 + /* Iterate through all the DMA descriptors */
20233 + switch (cfiep->ep->dwc_ep.buff_mode) {
20234 + case BM_SG:
20235 + cfi_build_sg_descs(cfi, cfiep, req);
20236 + break;
20237 +
20238 + case BM_CONCAT:
20239 + cfi_build_concat_descs(cfi, cfiep, req);
20240 + break;
20241 +
20242 + case BM_CIRCULAR:
20243 + cfi_build_circ_descs(cfi, cfiep, req);
20244 + break;
20245 +
20246 + case BM_ALIGN:
20247 + cfi_build_align_descs(cfi, cfiep, req);
20248 + break;
20249 +
20250 + default:
20251 + break;
20252 + }
20253 +}
20254 +
20255 +/**
20256 + * Allocate DMA buffer for different Buffer modes.
20257 + */
20258 +static void *cfi_ep_alloc_buf(struct cfiobject *cfi, struct dwc_otg_pcd *pcd,
20259 + struct dwc_otg_pcd_ep *ep, dma_addr_t * dma,
20260 + unsigned size, gfp_t flags)
20261 +{
20262 + return DWC_DMA_ALLOC(size, dma);
20263 +}
20264 +
20265 +/**
20266 + * This function initializes the CFI object.
20267 + */
20268 +int init_cfi(cfiobject_t * cfiobj)
20269 +{
20270 + CFI_INFO("%s\n", __func__);
20271 +
20272 + /* Allocate a buffer for IN XFERs */
20273 + cfiobj->buf_in.buf =
20274 + DWC_DMA_ALLOC(CFI_IN_BUF_LEN, &cfiobj->buf_in.addr);
20275 + if (NULL == cfiobj->buf_in.buf) {
20276 + CFI_INFO("Unable to allocate buffer for INs\n");
20277 + return -DWC_E_NO_MEMORY;
20278 + }
20279 +
20280 + /* Allocate a buffer for OUT XFERs */
20281 + cfiobj->buf_out.buf =
20282 + DWC_DMA_ALLOC(CFI_OUT_BUF_LEN, &cfiobj->buf_out.addr);
20283 + if (NULL == cfiobj->buf_out.buf) {
20284 + CFI_INFO("Unable to allocate buffer for OUT\n");
20285 + return -DWC_E_NO_MEMORY;
20286 + }
20287 +
20288 + /* Initialize the callback function pointers */
20289 + cfiobj->ops.release = cfi_release;
20290 + cfiobj->ops.ep_enable = cfi_ep_enable;
20291 + cfiobj->ops.ctrl_write_complete = cfi_ctrl_write_complete;
20292 + cfiobj->ops.build_descriptors = cfi_build_descriptors;
20293 + cfiobj->ops.ep_alloc_buf = cfi_ep_alloc_buf;
20294 +
20295 + /* Initialize the list of active endpoints in the CFI object */
20296 + DWC_LIST_INIT(&cfiobj->active_eps);
20297 +
20298 + return 0;
20299 +}
20300 +
20301 +/**
20302 + * This function reads the required feature's current value into the buffer
20303 + *
20304 + * @retval: Returns negative as error, or the data length of the feature
20305 + */
20306 +static int cfi_get_feature_value(uint8_t * buf, uint16_t buflen,
20307 + struct dwc_otg_pcd *pcd,
20308 + struct cfi_usb_ctrlrequest *ctrl_req)
20309 +{
20310 + int retval = -DWC_E_NOT_SUPPORTED;
20311 + struct dwc_otg_core_if *coreif = GET_CORE_IF(pcd);
20312 + uint16_t dfifo, rxfifo, txfifo;
20313 +
20314 + switch (ctrl_req->wIndex) {
20315 + /* Whether the DDMA is enabled or not */
20316 + case FT_ID_DMA_MODE:
20317 + *buf = (coreif->dma_enable && coreif->dma_desc_enable) ? 1 : 0;
20318 + retval = 1;
20319 + break;
20320 +
20321 + case FT_ID_DMA_BUFFER_SETUP:
20322 + retval = cfi_ep_get_sg_val(buf, pcd, ctrl_req);
20323 + break;
20324 +
20325 + case FT_ID_DMA_BUFF_ALIGN:
20326 + retval = cfi_ep_get_align_val(buf, pcd, ctrl_req);
20327 + break;
20328 +
20329 + case FT_ID_DMA_CONCAT_SETUP:
20330 + retval = cfi_ep_get_concat_val(buf, pcd, ctrl_req);
20331 + break;
20332 +
20333 + case FT_ID_DMA_CIRCULAR:
20334 + CFI_INFO("GetFeature value (FT_ID_DMA_CIRCULAR)\n");
20335 + break;
20336 +
20337 + case FT_ID_THRESHOLD_SETUP:
20338 + CFI_INFO("GetFeature value (FT_ID_THRESHOLD_SETUP)\n");
20339 + break;
20340 +
20341 + case FT_ID_DFIFO_DEPTH:
20342 + dfifo = get_dfifo_size(coreif);
20343 + *((uint16_t *) buf) = dfifo;
20344 + retval = sizeof(uint16_t);
20345 + break;
20346 +
20347 + case FT_ID_TX_FIFO_DEPTH:
20348 + retval = get_txfifo_size(pcd, ctrl_req->wValue);
20349 + if (retval >= 0) {
20350 + txfifo = retval;
20351 + *((uint16_t *) buf) = txfifo;
20352 + retval = sizeof(uint16_t);
20353 + }
20354 + break;
20355 +
20356 + case FT_ID_RX_FIFO_DEPTH:
20357 + retval = get_rxfifo_size(coreif, ctrl_req->wValue);
20358 + if (retval >= 0) {
20359 + rxfifo = retval;
20360 + *((uint16_t *) buf) = rxfifo;
20361 + retval = sizeof(uint16_t);
20362 + }
20363 + break;
20364 + }
20365 +
20366 + return retval;
20367 +}
20368 +
20369 +/**
20370 + * This function resets the SG for the specified EP to its default value
20371 + */
20372 +static int cfi_reset_sg_val(cfi_ep_t * cfiep)
20373 +{
20374 + dwc_memset(cfiep->bm_sg, 0, sizeof(ddma_sg_buffer_setup_t));
20375 + return 0;
20376 +}
20377 +
20378 +/**
20379 + * This function resets the Alignment for the specified EP to its default value
20380 + */
20381 +static int cfi_reset_align_val(cfi_ep_t * cfiep)
20382 +{
20383 + dwc_memset(cfiep->bm_sg, 0, sizeof(ddma_sg_buffer_setup_t));
20384 + return 0;
20385 +}
20386 +
20387 +/**
20388 + * This function resets the Concatenation for the specified EP to its default value
20389 + * This function will also set the value of the wTxBytes field to NULL after
20390 + * freeing the memory previously allocated for this field.
20391 + */
20392 +static int cfi_reset_concat_val(cfi_ep_t * cfiep)
20393 +{
20394 + /* First we need to free the wTxBytes field */
20395 + if (cfiep->bm_concat->wTxBytes) {
20396 + DWC_FREE(cfiep->bm_concat->wTxBytes);
20397 + cfiep->bm_concat->wTxBytes = NULL;
20398 + }
20399 +
20400 + dwc_memset(cfiep->bm_concat, 0, sizeof(ddma_concat_buffer_setup_t));
20401 + return 0;
20402 +}
20403 +
20404 +/**
20405 + * This function resets all the buffer setups of the specified endpoint
20406 + */
20407 +static int cfi_ep_reset_all_setup_vals(cfi_ep_t * cfiep)
20408 +{
20409 + cfi_reset_sg_val(cfiep);
20410 + cfi_reset_align_val(cfiep);
20411 + cfi_reset_concat_val(cfiep);
20412 + return 0;
20413 +}
20414 +
20415 +static int cfi_handle_reset_fifo_val(struct dwc_otg_pcd *pcd, uint8_t ep_addr,
20416 + uint8_t rx_rst, uint8_t tx_rst)
20417 +{
20418 + int retval = -DWC_E_INVALID;
20419 + uint16_t tx_siz[15];
20420 + uint16_t rx_siz = 0;
20421 + dwc_otg_pcd_ep_t *ep = NULL;
20422 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
20423 + dwc_otg_core_params_t *params = GET_CORE_IF(pcd)->core_params;
20424 +
20425 + if (rx_rst) {
20426 + rx_siz = params->dev_rx_fifo_size;
20427 + params->dev_rx_fifo_size = GET_CORE_IF(pcd)->init_rxfsiz;
20428 + }
20429 +
20430 + if (tx_rst) {
20431 + if (ep_addr == 0) {
20432 + int i;
20433 +
20434 + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
20435 + tx_siz[i] =
20436 + core_if->core_params->dev_tx_fifo_size[i];
20437 + core_if->core_params->dev_tx_fifo_size[i] =
20438 + core_if->init_txfsiz[i];
20439 + }
20440 + } else {
20441 +
20442 + ep = get_ep_by_addr(pcd, ep_addr);
20443 +
20444 + if (NULL == ep) {
20445 + CFI_INFO
20446 + ("%s: Unable to get the endpoint addr=0x%02x\n",
20447 + __func__, ep_addr);
20448 + return -DWC_E_INVALID;
20449 + }
20450 +
20451 + tx_siz[0] =
20452 + params->dev_tx_fifo_size[ep->dwc_ep.tx_fifo_num -
20453 + 1];
20454 + params->dev_tx_fifo_size[ep->dwc_ep.tx_fifo_num - 1] =
20455 + GET_CORE_IF(pcd)->init_txfsiz[ep->
20456 + dwc_ep.tx_fifo_num -
20457 + 1];
20458 + }
20459 + }
20460 +
20461 + if (resize_fifos(GET_CORE_IF(pcd))) {
20462 + retval = 0;
20463 + } else {
20464 + CFI_INFO
20465 + ("%s: Error resetting the feature Reset All(FIFO size)\n",
20466 + __func__);
20467 + if (rx_rst) {
20468 + params->dev_rx_fifo_size = rx_siz;
20469 + }
20470 +
20471 + if (tx_rst) {
20472 + if (ep_addr == 0) {
20473 + int i;
20474 + for (i = 0; i < core_if->hwcfg4.b.num_in_eps;
20475 + i++) {
20476 + core_if->
20477 + core_params->dev_tx_fifo_size[i] =
20478 + tx_siz[i];
20479 + }
20480 + } else {
20481 + params->dev_tx_fifo_size[ep->
20482 + dwc_ep.tx_fifo_num -
20483 + 1] = tx_siz[0];
20484 + }
20485 + }
20486 + retval = -DWC_E_INVALID;
20487 + }
20488 + return retval;
20489 +}
20490 +
20491 +static int cfi_handle_reset_all(struct dwc_otg_pcd *pcd, uint8_t addr)
20492 +{
20493 + int retval = 0;
20494 + cfi_ep_t *cfiep;
20495 + cfiobject_t *cfi = pcd->cfi;
20496 + dwc_list_link_t *tmp;
20497 +
20498 + retval = cfi_handle_reset_fifo_val(pcd, addr, 1, 1);
20499 + if (retval < 0) {
20500 + return retval;
20501 + }
20502 +
20503 + /* If the EP address is known then reset the features for only that EP */
20504 + if (addr) {
20505 + cfiep = get_cfi_ep_by_addr(pcd->cfi, addr);
20506 + if (NULL == cfiep) {
20507 + CFI_INFO("%s: Error getting the EP address 0x%02x\n",
20508 + __func__, addr);
20509 + return -DWC_E_INVALID;
20510 + }
20511 + retval = cfi_ep_reset_all_setup_vals(cfiep);
20512 + cfiep->ep->dwc_ep.buff_mode = BM_STANDARD;
20513 + }
20514 + /* Otherwise (wValue == 0), reset all features of all EP's */
20515 + else {
20516 + /* Traverse all the active EP's and reset the feature(s) value(s) */
20517 + //list_for_each_entry(cfiep, &cfi->active_eps, lh) {
20518 + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
20519 + cfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
20520 + retval = cfi_ep_reset_all_setup_vals(cfiep);
20521 + cfiep->ep->dwc_ep.buff_mode = BM_STANDARD;
20522 + if (retval < 0) {
20523 + CFI_INFO
20524 + ("%s: Error resetting the feature Reset All\n",
20525 + __func__);
20526 + return retval;
20527 + }
20528 + }
20529 + }
20530 + return retval;
20531 +}
20532 +
20533 +static int cfi_handle_reset_dma_buff_setup(struct dwc_otg_pcd *pcd,
20534 + uint8_t addr)
20535 +{
20536 + int retval = 0;
20537 + cfi_ep_t *cfiep;
20538 + cfiobject_t *cfi = pcd->cfi;
20539 + dwc_list_link_t *tmp;
20540 +
20541 + /* If the EP address is known then reset the features for only that EP */
20542 + if (addr) {
20543 + cfiep = get_cfi_ep_by_addr(pcd->cfi, addr);
20544 + if (NULL == cfiep) {
20545 + CFI_INFO("%s: Error getting the EP address 0x%02x\n",
20546 + __func__, addr);
20547 + return -DWC_E_INVALID;
20548 + }
20549 + retval = cfi_reset_sg_val(cfiep);
20550 + }
20551 + /* Otherwise (wValue == 0), reset all features of all EP's */
20552 + else {
20553 + /* Traverse all the active EP's and reset the feature(s) value(s) */
20554 + //list_for_each_entry(cfiep, &cfi->active_eps, lh) {
20555 + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
20556 + cfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
20557 + retval = cfi_reset_sg_val(cfiep);
20558 + if (retval < 0) {
20559 + CFI_INFO
20560 + ("%s: Error resetting the feature Buffer Setup\n",
20561 + __func__);
20562 + return retval;
20563 + }
20564 + }
20565 + }
20566 + return retval;
20567 +}
20568 +
20569 +static int cfi_handle_reset_concat_val(struct dwc_otg_pcd *pcd, uint8_t addr)
20570 +{
20571 + int retval = 0;
20572 + cfi_ep_t *cfiep;
20573 + cfiobject_t *cfi = pcd->cfi;
20574 + dwc_list_link_t *tmp;
20575 +
20576 + /* If the EP address is known then reset the features for only that EP */
20577 + if (addr) {
20578 + cfiep = get_cfi_ep_by_addr(pcd->cfi, addr);
20579 + if (NULL == cfiep) {
20580 + CFI_INFO("%s: Error getting the EP address 0x%02x\n",
20581 + __func__, addr);
20582 + return -DWC_E_INVALID;
20583 + }
20584 + retval = cfi_reset_concat_val(cfiep);
20585 + }
20586 + /* Otherwise (wValue == 0), reset all features of all EP's */
20587 + else {
20588 + /* Traverse all the active EP's and reset the feature(s) value(s) */
20589 + //list_for_each_entry(cfiep, &cfi->active_eps, lh) {
20590 + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
20591 + cfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
20592 + retval = cfi_reset_concat_val(cfiep);
20593 + if (retval < 0) {
20594 + CFI_INFO
20595 + ("%s: Error resetting the feature Concatenation Value\n",
20596 + __func__);
20597 + return retval;
20598 + }
20599 + }
20600 + }
20601 + return retval;
20602 +}
20603 +
20604 +static int cfi_handle_reset_align_val(struct dwc_otg_pcd *pcd, uint8_t addr)
20605 +{
20606 + int retval = 0;
20607 + cfi_ep_t *cfiep;
20608 + cfiobject_t *cfi = pcd->cfi;
20609 + dwc_list_link_t *tmp;
20610 +
20611 + /* If the EP address is known then reset the features for only that EP */
20612 + if (addr) {
20613 + cfiep = get_cfi_ep_by_addr(pcd->cfi, addr);
20614 + if (NULL == cfiep) {
20615 + CFI_INFO("%s: Error getting the EP address 0x%02x\n",
20616 + __func__, addr);
20617 + return -DWC_E_INVALID;
20618 + }
20619 + retval = cfi_reset_align_val(cfiep);
20620 + }
20621 + /* Otherwise (wValue == 0), reset all features of all EP's */
20622 + else {
20623 + /* Traverse all the active EP's and reset the feature(s) value(s) */
20624 + //list_for_each_entry(cfiep, &cfi->active_eps, lh) {
20625 + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
20626 + cfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
20627 + retval = cfi_reset_align_val(cfiep);
20628 + if (retval < 0) {
20629 + CFI_INFO
20630 + ("%s: Error resetting the feature Aliignment Value\n",
20631 + __func__);
20632 + return retval;
20633 + }
20634 + }
20635 + }
20636 + return retval;
20637 +
20638 +}
20639 +
20640 +static int cfi_preproc_reset(struct dwc_otg_pcd *pcd,
20641 + struct cfi_usb_ctrlrequest *req)
20642 +{
20643 + int retval = 0;
20644 +
20645 + switch (req->wIndex) {
20646 + case 0:
20647 + /* Reset all features */
20648 + retval = cfi_handle_reset_all(pcd, req->wValue & 0xff);
20649 + break;
20650 +
20651 + case FT_ID_DMA_BUFFER_SETUP:
20652 + /* Reset the SG buffer setup */
20653 + retval =
20654 + cfi_handle_reset_dma_buff_setup(pcd, req->wValue & 0xff);
20655 + break;
20656 +
20657 + case FT_ID_DMA_CONCAT_SETUP:
20658 + /* Reset the Concatenation buffer setup */
20659 + retval = cfi_handle_reset_concat_val(pcd, req->wValue & 0xff);
20660 + break;
20661 +
20662 + case FT_ID_DMA_BUFF_ALIGN:
20663 + /* Reset the Alignment buffer setup */
20664 + retval = cfi_handle_reset_align_val(pcd, req->wValue & 0xff);
20665 + break;
20666 +
20667 + case FT_ID_TX_FIFO_DEPTH:
20668 + retval =
20669 + cfi_handle_reset_fifo_val(pcd, req->wValue & 0xff, 0, 1);
20670 + pcd->cfi->need_gadget_att = 0;
20671 + break;
20672 +
20673 + case FT_ID_RX_FIFO_DEPTH:
20674 + retval = cfi_handle_reset_fifo_val(pcd, 0, 1, 0);
20675 + pcd->cfi->need_gadget_att = 0;
20676 + break;
20677 + default:
20678 + break;
20679 + }
20680 + return retval;
20681 +}
20682 +
20683 +/**
20684 + * This function sets a new value for the SG buffer setup.
20685 + */
20686 +static int cfi_ep_set_sg_val(uint8_t * buf, struct dwc_otg_pcd *pcd)
20687 +{
20688 + uint8_t inaddr, outaddr;
20689 + cfi_ep_t *epin, *epout;
20690 + ddma_sg_buffer_setup_t *psgval;
20691 + uint32_t desccount, size;
20692 +
20693 + CFI_INFO("%s\n", __func__);
20694 +
20695 + psgval = (ddma_sg_buffer_setup_t *) buf;
20696 + desccount = (uint32_t) psgval->bCount;
20697 + size = (uint32_t) psgval->wSize;
20698 +
20699 + /* Check the DMA descriptor count */
20700 + if ((desccount > MAX_DMA_DESCS_PER_EP) || (desccount == 0)) {
20701 + CFI_INFO
20702 + ("%s: The count of DMA Descriptors should be between 1 and %d\n",
20703 + __func__, MAX_DMA_DESCS_PER_EP);
20704 + return -DWC_E_INVALID;
20705 + }
20706 +
20707 + /* Check the DMA descriptor count */
20708 +
20709 + if (size == 0) {
20710 +
20711 + CFI_INFO("%s: The transfer size should be at least 1 byte\n",
20712 + __func__);
20713 +
20714 + return -DWC_E_INVALID;
20715 +
20716 + }
20717 +
20718 + inaddr = psgval->bInEndpointAddress;
20719 + outaddr = psgval->bOutEndpointAddress;
20720 +
20721 + epin = get_cfi_ep_by_addr(pcd->cfi, inaddr);
20722 + epout = get_cfi_ep_by_addr(pcd->cfi, outaddr);
20723 +
20724 + if (NULL == epin || NULL == epout) {
20725 + CFI_INFO
20726 + ("%s: Unable to get the endpoints inaddr=0x%02x outaddr=0x%02x\n",
20727 + __func__, inaddr, outaddr);
20728 + return -DWC_E_INVALID;
20729 + }
20730 +
20731 + epin->ep->dwc_ep.buff_mode = BM_SG;
20732 + dwc_memcpy(epin->bm_sg, psgval, sizeof(ddma_sg_buffer_setup_t));
20733 +
20734 + epout->ep->dwc_ep.buff_mode = BM_SG;
20735 + dwc_memcpy(epout->bm_sg, psgval, sizeof(ddma_sg_buffer_setup_t));
20736 +
20737 + return 0;
20738 +}
20739 +
20740 +/**
20741 + * This function sets a new value for the buffer Alignment setup.
20742 + */
20743 +static int cfi_ep_set_alignment_val(uint8_t * buf, struct dwc_otg_pcd *pcd)
20744 +{
20745 + cfi_ep_t *ep;
20746 + uint8_t addr;
20747 + ddma_align_buffer_setup_t *palignval;
20748 +
20749 + palignval = (ddma_align_buffer_setup_t *) buf;
20750 + addr = palignval->bEndpointAddress;
20751 +
20752 + ep = get_cfi_ep_by_addr(pcd->cfi, addr);
20753 +
20754 + if (NULL == ep) {
20755 + CFI_INFO("%s: Unable to get the endpoint addr=0x%02x\n",
20756 + __func__, addr);
20757 + return -DWC_E_INVALID;
20758 + }
20759 +
20760 + ep->ep->dwc_ep.buff_mode = BM_ALIGN;
20761 + dwc_memcpy(ep->bm_align, palignval, sizeof(ddma_align_buffer_setup_t));
20762 +
20763 + return 0;
20764 +}
20765 +
20766 +/**
20767 + * This function sets a new value for the Concatenation buffer setup.
20768 + */
20769 +static int cfi_ep_set_concat_val(uint8_t * buf, struct dwc_otg_pcd *pcd)
20770 +{
20771 + uint8_t addr;
20772 + cfi_ep_t *ep;
20773 + struct _ddma_concat_buffer_setup_hdr *pConcatValHdr;
20774 + uint16_t *pVals;
20775 + uint32_t desccount;
20776 + int i;
20777 + uint16_t mps;
20778 +
20779 + pConcatValHdr = (struct _ddma_concat_buffer_setup_hdr *)buf;
20780 + desccount = (uint32_t) pConcatValHdr->bDescCount;
20781 + pVals = (uint16_t *) (buf + BS_CONCAT_VAL_HDR_LEN);
20782 +
20783 + /* Check the DMA descriptor count */
20784 + if (desccount > MAX_DMA_DESCS_PER_EP) {
20785 + CFI_INFO("%s: Maximum DMA Descriptor count should be %d\n",
20786 + __func__, MAX_DMA_DESCS_PER_EP);
20787 + return -DWC_E_INVALID;
20788 + }
20789 +
20790 + addr = pConcatValHdr->bEndpointAddress;
20791 + ep = get_cfi_ep_by_addr(pcd->cfi, addr);
20792 + if (NULL == ep) {
20793 + CFI_INFO("%s: Unable to get the endpoint addr=0x%02x\n",
20794 + __func__, addr);
20795 + return -DWC_E_INVALID;
20796 + }
20797 +
20798 + mps = UGETW(ep->ep->desc->wMaxPacketSize);
20799 +
20800 +#if 0
20801 + for (i = 0; i < desccount; i++) {
20802 + CFI_INFO("%s: wTxSize[%d]=0x%04x\n", __func__, i, pVals[i]);
20803 + }
20804 + CFI_INFO("%s: epname=%s; mps=%d\n", __func__, ep->ep->ep.name, mps);
20805 +#endif
20806 +
20807 + /* Check the wTxSizes to be less than or equal to the mps */
20808 + for (i = 0; i < desccount; i++) {
20809 + if (pVals[i] > mps) {
20810 + CFI_INFO
20811 + ("%s: ERROR - the wTxSize[%d] should be <= MPS (wTxSize=%d)\n",
20812 + __func__, i, pVals[i]);
20813 + return -DWC_E_INVALID;
20814 + }
20815 + }
20816 +
20817 + ep->ep->dwc_ep.buff_mode = BM_CONCAT;
20818 + dwc_memcpy(ep->bm_concat, pConcatValHdr, BS_CONCAT_VAL_HDR_LEN);
20819 +
20820 + /* Free the previously allocated storage for the wTxBytes */
20821 + if (ep->bm_concat->wTxBytes) {
20822 + DWC_FREE(ep->bm_concat->wTxBytes);
20823 + }
20824 +
20825 + /* Allocate a new storage for the wTxBytes field */
20826 + ep->bm_concat->wTxBytes =
20827 + DWC_ALLOC(sizeof(uint16_t) * pConcatValHdr->bDescCount);
20828 + if (NULL == ep->bm_concat->wTxBytes) {
20829 + CFI_INFO("%s: Unable to allocate memory\n", __func__);
20830 + return -DWC_E_NO_MEMORY;
20831 + }
20832 +
20833 + /* Copy the new values into the wTxBytes filed */
20834 + dwc_memcpy(ep->bm_concat->wTxBytes, buf + BS_CONCAT_VAL_HDR_LEN,
20835 + sizeof(uint16_t) * pConcatValHdr->bDescCount);
20836 +
20837 + return 0;
20838 +}
20839 +
20840 +/**
20841 + * This function calculates the total of all FIFO sizes
20842 + *
20843 + * @param core_if Programming view of DWC_otg controller
20844 + *
20845 + * @return The total of data FIFO sizes.
20846 + *
20847 + */
20848 +static uint16_t get_dfifo_size(dwc_otg_core_if_t * core_if)
20849 +{
20850 + dwc_otg_core_params_t *params = core_if->core_params;
20851 + uint16_t dfifo_total = 0;
20852 + int i;
20853 +
20854 + /* The shared RxFIFO size */
20855 + dfifo_total =
20856 + params->dev_rx_fifo_size + params->dev_nperio_tx_fifo_size;
20857 +
20858 + /* Add up each TxFIFO size to the total */
20859 + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
20860 + dfifo_total += params->dev_tx_fifo_size[i];
20861 + }
20862 +
20863 + return dfifo_total;
20864 +}
20865 +
20866 +/**
20867 + * This function returns Rx FIFO size
20868 + *
20869 + * @param core_if Programming view of DWC_otg controller
20870 + *
20871 + * @return The total of data FIFO sizes.
20872 + *
20873 + */
20874 +static int32_t get_rxfifo_size(dwc_otg_core_if_t * core_if, uint16_t wValue)
20875 +{
20876 + switch (wValue >> 8) {
20877 + case 0:
20878 + return (core_if->pwron_rxfsiz <
20879 + 32768) ? core_if->pwron_rxfsiz : 32768;
20880 + break;
20881 + case 1:
20882 + return core_if->core_params->dev_rx_fifo_size;
20883 + break;
20884 + default:
20885 + return -DWC_E_INVALID;
20886 + break;
20887 + }
20888 +}
20889 +
20890 +/**
20891 + * This function returns Tx FIFO size for IN EP
20892 + *
20893 + * @param core_if Programming view of DWC_otg controller
20894 + *
20895 + * @return The total of data FIFO sizes.
20896 + *
20897 + */
20898 +static int32_t get_txfifo_size(struct dwc_otg_pcd *pcd, uint16_t wValue)
20899 +{
20900 + dwc_otg_pcd_ep_t *ep;
20901 +
20902 + ep = get_ep_by_addr(pcd, wValue & 0xff);
20903 +
20904 + if (NULL == ep) {
20905 + CFI_INFO("%s: Unable to get the endpoint addr=0x%02x\n",
20906 + __func__, wValue & 0xff);
20907 + return -DWC_E_INVALID;
20908 + }
20909 +
20910 + if (!ep->dwc_ep.is_in) {
20911 + CFI_INFO
20912 + ("%s: No Tx FIFO assingned to the Out endpoint addr=0x%02x\n",
20913 + __func__, wValue & 0xff);
20914 + return -DWC_E_INVALID;
20915 + }
20916 +
20917 + switch (wValue >> 8) {
20918 + case 0:
20919 + return (GET_CORE_IF(pcd)->pwron_txfsiz
20920 + [ep->dwc_ep.tx_fifo_num - 1] <
20921 + 768) ? GET_CORE_IF(pcd)->pwron_txfsiz[ep->
20922 + dwc_ep.tx_fifo_num
20923 + - 1] : 32768;
20924 + break;
20925 + case 1:
20926 + return GET_CORE_IF(pcd)->core_params->
20927 + dev_tx_fifo_size[ep->dwc_ep.num - 1];
20928 + break;
20929 + default:
20930 + return -DWC_E_INVALID;
20931 + break;
20932 + }
20933 +}
20934 +
20935 +/**
20936 + * This function checks if the submitted combination of
20937 + * device mode FIFO sizes is possible or not.
20938 + *
20939 + * @param core_if Programming view of DWC_otg controller
20940 + *
20941 + * @return 1 if possible, 0 otherwise.
20942 + *
20943 + */
20944 +static uint8_t check_fifo_sizes(dwc_otg_core_if_t * core_if)
20945 +{
20946 + uint16_t dfifo_actual = 0;
20947 + dwc_otg_core_params_t *params = core_if->core_params;
20948 + uint16_t start_addr = 0;
20949 + int i;
20950 +
20951 + dfifo_actual =
20952 + params->dev_rx_fifo_size + params->dev_nperio_tx_fifo_size;
20953 +
20954 + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
20955 + dfifo_actual += params->dev_tx_fifo_size[i];
20956 + }
20957 +
20958 + if (dfifo_actual > core_if->total_fifo_size) {
20959 + return 0;
20960 + }
20961 +
20962 + if (params->dev_rx_fifo_size > 32768 || params->dev_rx_fifo_size < 16)
20963 + return 0;
20964 +
20965 + if (params->dev_nperio_tx_fifo_size > 32768
20966 + || params->dev_nperio_tx_fifo_size < 16)
20967 + return 0;
20968 +
20969 + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
20970 +
20971 + if (params->dev_tx_fifo_size[i] > 768
20972 + || params->dev_tx_fifo_size[i] < 4)
20973 + return 0;
20974 + }
20975 +
20976 + if (params->dev_rx_fifo_size > core_if->pwron_rxfsiz)
20977 + return 0;
20978 + start_addr = params->dev_rx_fifo_size;
20979 +
20980 + if (params->dev_nperio_tx_fifo_size > core_if->pwron_gnptxfsiz)
20981 + return 0;
20982 + start_addr += params->dev_nperio_tx_fifo_size;
20983 +
20984 + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
20985 +
20986 + if (params->dev_tx_fifo_size[i] > core_if->pwron_txfsiz[i])
20987 + return 0;
20988 + start_addr += params->dev_tx_fifo_size[i];
20989 + }
20990 +
20991 + return 1;
20992 +}
20993 +
20994 +/**
20995 + * This function resizes Device mode FIFOs
20996 + *
20997 + * @param core_if Programming view of DWC_otg controller
20998 + *
20999 + * @return 1 if successful, 0 otherwise
21000 + *
21001 + */
21002 +static uint8_t resize_fifos(dwc_otg_core_if_t * core_if)
21003 +{
21004 + int i = 0;
21005 + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
21006 + dwc_otg_core_params_t *params = core_if->core_params;
21007 + uint32_t rx_fifo_size;
21008 + fifosize_data_t nptxfifosize;
21009 + fifosize_data_t txfifosize[15];
21010 +
21011 + uint32_t rx_fsz_bak;
21012 + uint32_t nptxfsz_bak;
21013 + uint32_t txfsz_bak[15];
21014 +
21015 + uint16_t start_address;
21016 + uint8_t retval = 1;
21017 +
21018 + if (!check_fifo_sizes(core_if)) {
21019 + return 0;
21020 + }
21021 +
21022 + /* Configure data FIFO sizes */
21023 + if (core_if->hwcfg2.b.dynamic_fifo && params->enable_dynamic_fifo) {
21024 + rx_fsz_bak = DWC_READ_REG32(&global_regs->grxfsiz);
21025 + rx_fifo_size = params->dev_rx_fifo_size;
21026 + DWC_WRITE_REG32(&global_regs->grxfsiz, rx_fifo_size);
21027 +
21028 + /*
21029 + * Tx FIFOs These FIFOs are numbered from 1 to 15.
21030 + * Indexes of the FIFO size module parameters in the
21031 + * dev_tx_fifo_size array and the FIFO size registers in
21032 + * the dtxfsiz array run from 0 to 14.
21033 + */
21034 +
21035 + /* Non-periodic Tx FIFO */
21036 + nptxfsz_bak = DWC_READ_REG32(&global_regs->gnptxfsiz);
21037 + nptxfifosize.b.depth = params->dev_nperio_tx_fifo_size;
21038 + start_address = params->dev_rx_fifo_size;
21039 + nptxfifosize.b.startaddr = start_address;
21040 +
21041 + DWC_WRITE_REG32(&global_regs->gnptxfsiz, nptxfifosize.d32);
21042 +
21043 + start_address += nptxfifosize.b.depth;
21044 +
21045 + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
21046 + txfsz_bak[i] = DWC_READ_REG32(&global_regs->dtxfsiz[i]);
21047 +
21048 + txfifosize[i].b.depth = params->dev_tx_fifo_size[i];
21049 + txfifosize[i].b.startaddr = start_address;
21050 + DWC_WRITE_REG32(&global_regs->dtxfsiz[i],
21051 + txfifosize[i].d32);
21052 +
21053 + start_address += txfifosize[i].b.depth;
21054 + }
21055 +
21056 + /** Check if register values are set correctly */
21057 + if (rx_fifo_size != DWC_READ_REG32(&global_regs->grxfsiz)) {
21058 + retval = 0;
21059 + }
21060 +
21061 + if (nptxfifosize.d32 != DWC_READ_REG32(&global_regs->gnptxfsiz)) {
21062 + retval = 0;
21063 + }
21064 +
21065 + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
21066 + if (txfifosize[i].d32 !=
21067 + DWC_READ_REG32(&global_regs->dtxfsiz[i])) {
21068 + retval = 0;
21069 + }
21070 + }
21071 +
21072 + /** If register values are not set correctly, reset old values */
21073 + if (retval == 0) {
21074 + DWC_WRITE_REG32(&global_regs->grxfsiz, rx_fsz_bak);
21075 +
21076 + /* Non-periodic Tx FIFO */
21077 + DWC_WRITE_REG32(&global_regs->gnptxfsiz, nptxfsz_bak);
21078 +
21079 + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
21080 + DWC_WRITE_REG32(&global_regs->dtxfsiz[i],
21081 + txfsz_bak[i]);
21082 + }
21083 + }
21084 + } else {
21085 + return 0;
21086 + }
21087 +
21088 + /* Flush the FIFOs */
21089 + dwc_otg_flush_tx_fifo(core_if, 0x10); /* all Tx FIFOs */
21090 + dwc_otg_flush_rx_fifo(core_if);
21091 +
21092 + return retval;
21093 +}
21094 +
21095 +/**
21096 + * This function sets a new value for the buffer Alignment setup.
21097 + */
21098 +static int cfi_ep_set_tx_fifo_val(uint8_t * buf, dwc_otg_pcd_t * pcd)
21099 +{
21100 + int retval;
21101 + uint32_t fsiz;
21102 + uint16_t size;
21103 + uint16_t ep_addr;
21104 + dwc_otg_pcd_ep_t *ep;
21105 + dwc_otg_core_params_t *params = GET_CORE_IF(pcd)->core_params;
21106 + tx_fifo_size_setup_t *ptxfifoval;
21107 +
21108 + ptxfifoval = (tx_fifo_size_setup_t *) buf;
21109 + ep_addr = ptxfifoval->bEndpointAddress;
21110 + size = ptxfifoval->wDepth;
21111 +
21112 + ep = get_ep_by_addr(pcd, ep_addr);
21113 +
21114 + CFI_INFO
21115 + ("%s: Set Tx FIFO size: endpoint addr=0x%02x, depth=%d, FIFO Num=%d\n",
21116 + __func__, ep_addr, size, ep->dwc_ep.tx_fifo_num);
21117 +
21118 + if (NULL == ep) {
21119 + CFI_INFO("%s: Unable to get the endpoint addr=0x%02x\n",
21120 + __func__, ep_addr);
21121 + return -DWC_E_INVALID;
21122 + }
21123 +
21124 + fsiz = params->dev_tx_fifo_size[ep->dwc_ep.tx_fifo_num - 1];
21125 + params->dev_tx_fifo_size[ep->dwc_ep.tx_fifo_num - 1] = size;
21126 +
21127 + if (resize_fifos(GET_CORE_IF(pcd))) {
21128 + retval = 0;
21129 + } else {
21130 + CFI_INFO
21131 + ("%s: Error setting the feature Tx FIFO Size for EP%d\n",
21132 + __func__, ep_addr);
21133 + params->dev_tx_fifo_size[ep->dwc_ep.tx_fifo_num - 1] = fsiz;
21134 + retval = -DWC_E_INVALID;
21135 + }
21136 +
21137 + return retval;
21138 +}
21139 +
21140 +/**
21141 + * This function sets a new value for the buffer Alignment setup.
21142 + */
21143 +static int cfi_set_rx_fifo_val(uint8_t * buf, dwc_otg_pcd_t * pcd)
21144 +{
21145 + int retval;
21146 + uint32_t fsiz;
21147 + uint16_t size;
21148 + dwc_otg_core_params_t *params = GET_CORE_IF(pcd)->core_params;
21149 + rx_fifo_size_setup_t *prxfifoval;
21150 +
21151 + prxfifoval = (rx_fifo_size_setup_t *) buf;
21152 + size = prxfifoval->wDepth;
21153 +
21154 + fsiz = params->dev_rx_fifo_size;
21155 + params->dev_rx_fifo_size = size;
21156 +
21157 + if (resize_fifos(GET_CORE_IF(pcd))) {
21158 + retval = 0;
21159 + } else {
21160 + CFI_INFO("%s: Error setting the feature Rx FIFO Size\n",
21161 + __func__);
21162 + params->dev_rx_fifo_size = fsiz;
21163 + retval = -DWC_E_INVALID;
21164 + }
21165 +
21166 + return retval;
21167 +}
21168 +
21169 +/**
21170 + * This function reads the SG of an EP's buffer setup into the buffer buf
21171 + */
21172 +static int cfi_ep_get_sg_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
21173 + struct cfi_usb_ctrlrequest *req)
21174 +{
21175 + int retval = -DWC_E_INVALID;
21176 + uint8_t addr;
21177 + cfi_ep_t *ep;
21178 +
21179 + /* The Low Byte of the wValue contains a non-zero address of the endpoint */
21180 + addr = req->wValue & 0xFF;
21181 + if (addr == 0) /* The address should be non-zero */
21182 + return retval;
21183 +
21184 + ep = get_cfi_ep_by_addr(pcd->cfi, addr);
21185 + if (NULL == ep) {
21186 + CFI_INFO("%s: Unable to get the endpoint address(0x%02x)\n",
21187 + __func__, addr);
21188 + return retval;
21189 + }
21190 +
21191 + dwc_memcpy(buf, ep->bm_sg, BS_SG_VAL_DESC_LEN);
21192 + retval = BS_SG_VAL_DESC_LEN;
21193 + return retval;
21194 +}
21195 +
21196 +/**
21197 + * This function reads the Concatenation value of an EP's buffer mode into
21198 + * the buffer buf
21199 + */
21200 +static int cfi_ep_get_concat_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
21201 + struct cfi_usb_ctrlrequest *req)
21202 +{
21203 + int retval = -DWC_E_INVALID;
21204 + uint8_t addr;
21205 + cfi_ep_t *ep;
21206 + uint8_t desc_count;
21207 +
21208 + /* The Low Byte of the wValue contains a non-zero address of the endpoint */
21209 + addr = req->wValue & 0xFF;
21210 + if (addr == 0) /* The address should be non-zero */
21211 + return retval;
21212 +
21213 + ep = get_cfi_ep_by_addr(pcd->cfi, addr);
21214 + if (NULL == ep) {
21215 + CFI_INFO("%s: Unable to get the endpoint address(0x%02x)\n",
21216 + __func__, addr);
21217 + return retval;
21218 + }
21219 +
21220 + /* Copy the header to the buffer */
21221 + dwc_memcpy(buf, ep->bm_concat, BS_CONCAT_VAL_HDR_LEN);
21222 + /* Advance the buffer pointer by the header size */
21223 + buf += BS_CONCAT_VAL_HDR_LEN;
21224 +
21225 + desc_count = ep->bm_concat->hdr.bDescCount;
21226 + /* Copy alll the wTxBytes to the buffer */
21227 + dwc_memcpy(buf, ep->bm_concat->wTxBytes, sizeof(uid16_t) * desc_count);
21228 +
21229 + retval = BS_CONCAT_VAL_HDR_LEN + sizeof(uid16_t) * desc_count;
21230 + return retval;
21231 +}
21232 +
21233 +/**
21234 + * This function reads the buffer Alignment value of an EP's buffer mode into
21235 + * the buffer buf
21236 + *
21237 + * @return The total number of bytes copied to the buffer or negative error code.
21238 + */
21239 +static int cfi_ep_get_align_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
21240 + struct cfi_usb_ctrlrequest *req)
21241 +{
21242 + int retval = -DWC_E_INVALID;
21243 + uint8_t addr;
21244 + cfi_ep_t *ep;
21245 +
21246 + /* The Low Byte of the wValue contains a non-zero address of the endpoint */
21247 + addr = req->wValue & 0xFF;
21248 + if (addr == 0) /* The address should be non-zero */
21249 + return retval;
21250 +
21251 + ep = get_cfi_ep_by_addr(pcd->cfi, addr);
21252 + if (NULL == ep) {
21253 + CFI_INFO("%s: Unable to get the endpoint address(0x%02x)\n",
21254 + __func__, addr);
21255 + return retval;
21256 + }
21257 +
21258 + dwc_memcpy(buf, ep->bm_align, BS_ALIGN_VAL_HDR_LEN);
21259 + retval = BS_ALIGN_VAL_HDR_LEN;
21260 +
21261 + return retval;
21262 +}
21263 +
21264 +/**
21265 + * This function sets a new value for the specified feature
21266 + *
21267 + * @param pcd A pointer to the PCD object
21268 + *
21269 + * @return 0 if successful, negative error code otherwise to stall the DCE.
21270 + */
21271 +static int cfi_set_feature_value(struct dwc_otg_pcd *pcd)
21272 +{
21273 + int retval = -DWC_E_NOT_SUPPORTED;
21274 + uint16_t wIndex, wValue;
21275 + uint8_t bRequest;
21276 + struct dwc_otg_core_if *coreif;
21277 + cfiobject_t *cfi = pcd->cfi;
21278 + struct cfi_usb_ctrlrequest *ctrl_req;
21279 + uint8_t *buf;
21280 + ctrl_req = &cfi->ctrl_req;
21281 +
21282 + buf = pcd->cfi->ctrl_req.data;
21283 +
21284 + coreif = GET_CORE_IF(pcd);
21285 + bRequest = ctrl_req->bRequest;
21286 + wIndex = DWC_CONSTANT_CPU_TO_LE16(ctrl_req->wIndex);
21287 + wValue = DWC_CONSTANT_CPU_TO_LE16(ctrl_req->wValue);
21288 +
21289 + /* See which feature is to be modified */
21290 + switch (wIndex) {
21291 + case FT_ID_DMA_BUFFER_SETUP:
21292 + /* Modify the feature */
21293 + if ((retval = cfi_ep_set_sg_val(buf, pcd)) < 0)
21294 + return retval;
21295 +
21296 + /* And send this request to the gadget */
21297 + cfi->need_gadget_att = 1;
21298 + break;
21299 +
21300 + case FT_ID_DMA_BUFF_ALIGN:
21301 + if ((retval = cfi_ep_set_alignment_val(buf, pcd)) < 0)
21302 + return retval;
21303 + cfi->need_gadget_att = 1;
21304 + break;
21305 +
21306 + case FT_ID_DMA_CONCAT_SETUP:
21307 + /* Modify the feature */
21308 + if ((retval = cfi_ep_set_concat_val(buf, pcd)) < 0)
21309 + return retval;
21310 + cfi->need_gadget_att = 1;
21311 + break;
21312 +
21313 + case FT_ID_DMA_CIRCULAR:
21314 + CFI_INFO("FT_ID_DMA_CIRCULAR\n");
21315 + break;
21316 +
21317 + case FT_ID_THRESHOLD_SETUP:
21318 + CFI_INFO("FT_ID_THRESHOLD_SETUP\n");
21319 + break;
21320 +
21321 + case FT_ID_DFIFO_DEPTH:
21322 + CFI_INFO("FT_ID_DFIFO_DEPTH\n");
21323 + break;
21324 +
21325 + case FT_ID_TX_FIFO_DEPTH:
21326 + CFI_INFO("FT_ID_TX_FIFO_DEPTH\n");
21327 + if ((retval = cfi_ep_set_tx_fifo_val(buf, pcd)) < 0)
21328 + return retval;
21329 + cfi->need_gadget_att = 0;
21330 + break;
21331 +
21332 + case FT_ID_RX_FIFO_DEPTH:
21333 + CFI_INFO("FT_ID_RX_FIFO_DEPTH\n");
21334 + if ((retval = cfi_set_rx_fifo_val(buf, pcd)) < 0)
21335 + return retval;
21336 + cfi->need_gadget_att = 0;
21337 + break;
21338 + }
21339 +
21340 + return retval;
21341 +}
21342 +
21343 +#endif //DWC_UTE_CFI
21344 --- /dev/null
21345 +++ b/drivers/usb/host/dwc_otg/dwc_otg_cfi.h
21346 @@ -0,0 +1,320 @@
21347 +/* ==========================================================================
21348 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
21349 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
21350 + * otherwise expressly agreed to in writing between Synopsys and you.
21351 + *
21352 + * The Software IS NOT an item of Licensed Software or Licensed Product under
21353 + * any End User Software License Agreement or Agreement for Licensed Product
21354 + * with Synopsys or any supplement thereto. You are permitted to use and
21355 + * redistribute this Software in source and binary forms, with or without
21356 + * modification, provided that redistributions of source code must retain this
21357 + * notice. You may not view, use, disclose, copy or distribute this file or
21358 + * any information contained herein except pursuant to this license grant from
21359 + * Synopsys. If you do not agree with this notice, including the disclaimer
21360 + * below, then you are not authorized to use the Software.
21361 + *
21362 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
21363 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21364 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21365 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
21366 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21367 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21368 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21369 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21370 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
21371 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
21372 + * DAMAGE.
21373 + * ========================================================================== */
21374 +
21375 +#if !defined(__DWC_OTG_CFI_H__)
21376 +#define __DWC_OTG_CFI_H__
21377 +
21378 +#include "dwc_otg_pcd.h"
21379 +#include "dwc_cfi_common.h"
21380 +
21381 +/**
21382 + * @file
21383 + * This file contains the CFI related OTG PCD specific common constants,
21384 + * interfaces(functions and macros) and data structures.The CFI Protocol is an
21385 + * optional interface for internal testing purposes that a DUT may implement to
21386 + * support testing of configurable features.
21387 + *
21388 + */
21389 +
21390 +struct dwc_otg_pcd;
21391 +struct dwc_otg_pcd_ep;
21392 +
21393 +/** OTG CFI Features (properties) ID constants */
21394 +/** This is a request for all Core Features */
21395 +#define FT_ID_DMA_MODE 0x0001
21396 +#define FT_ID_DMA_BUFFER_SETUP 0x0002
21397 +#define FT_ID_DMA_BUFF_ALIGN 0x0003
21398 +#define FT_ID_DMA_CONCAT_SETUP 0x0004
21399 +#define FT_ID_DMA_CIRCULAR 0x0005
21400 +#define FT_ID_THRESHOLD_SETUP 0x0006
21401 +#define FT_ID_DFIFO_DEPTH 0x0007
21402 +#define FT_ID_TX_FIFO_DEPTH 0x0008
21403 +#define FT_ID_RX_FIFO_DEPTH 0x0009
21404 +
21405 +/**********************************************************/
21406 +#define CFI_INFO_DEF
21407 +
21408 +#ifdef CFI_INFO_DEF
21409 +#define CFI_INFO(fmt...) DWC_PRINTF("CFI: " fmt);
21410 +#else
21411 +#define CFI_INFO(fmt...)
21412 +#endif
21413 +
21414 +#define min(x,y) ({ \
21415 + x < y ? x : y; })
21416 +
21417 +#define max(x,y) ({ \
21418 + x > y ? x : y; })
21419 +
21420 +/**
21421 + * Descriptor DMA SG Buffer setup structure (SG buffer). This structure is
21422 + * also used for setting up a buffer for Circular DDMA.
21423 + */
21424 +struct _ddma_sg_buffer_setup {
21425 +#define BS_SG_VAL_DESC_LEN 6
21426 + /* The OUT EP address */
21427 + uint8_t bOutEndpointAddress;
21428 + /* The IN EP address */
21429 + uint8_t bInEndpointAddress;
21430 + /* Number of bytes to put between transfer segments (must be DWORD boundaries) */
21431 + uint8_t bOffset;
21432 + /* The number of transfer segments (a DMA descriptors per each segment) */
21433 + uint8_t bCount;
21434 + /* Size (in byte) of each transfer segment */
21435 + uint16_t wSize;
21436 +} __attribute__ ((packed));
21437 +typedef struct _ddma_sg_buffer_setup ddma_sg_buffer_setup_t;
21438 +
21439 +/** Descriptor DMA Concatenation Buffer setup structure */
21440 +struct _ddma_concat_buffer_setup_hdr {
21441 +#define BS_CONCAT_VAL_HDR_LEN 4
21442 + /* The endpoint for which the buffer is to be set up */
21443 + uint8_t bEndpointAddress;
21444 + /* The count of descriptors to be used */
21445 + uint8_t bDescCount;
21446 + /* The total size of the transfer */
21447 + uint16_t wSize;
21448 +} __attribute__ ((packed));
21449 +typedef struct _ddma_concat_buffer_setup_hdr ddma_concat_buffer_setup_hdr_t;
21450 +
21451 +/** Descriptor DMA Concatenation Buffer setup structure */
21452 +struct _ddma_concat_buffer_setup {
21453 + /* The SG header */
21454 + ddma_concat_buffer_setup_hdr_t hdr;
21455 +
21456 + /* The XFER sizes pointer (allocated dynamically) */
21457 + uint16_t *wTxBytes;
21458 +} __attribute__ ((packed));
21459 +typedef struct _ddma_concat_buffer_setup ddma_concat_buffer_setup_t;
21460 +
21461 +/** Descriptor DMA Alignment Buffer setup structure */
21462 +struct _ddma_align_buffer_setup {
21463 +#define BS_ALIGN_VAL_HDR_LEN 2
21464 + uint8_t bEndpointAddress;
21465 + uint8_t bAlign;
21466 +} __attribute__ ((packed));
21467 +typedef struct _ddma_align_buffer_setup ddma_align_buffer_setup_t;
21468 +
21469 +/** Transmit FIFO Size setup structure */
21470 +struct _tx_fifo_size_setup {
21471 + uint8_t bEndpointAddress;
21472 + uint16_t wDepth;
21473 +} __attribute__ ((packed));
21474 +typedef struct _tx_fifo_size_setup tx_fifo_size_setup_t;
21475 +
21476 +/** Transmit FIFO Size setup structure */
21477 +struct _rx_fifo_size_setup {
21478 + uint16_t wDepth;
21479 +} __attribute__ ((packed));
21480 +typedef struct _rx_fifo_size_setup rx_fifo_size_setup_t;
21481 +
21482 +/**
21483 + * struct cfi_usb_ctrlrequest - the CFI implementation of the struct usb_ctrlrequest
21484 + * This structure encapsulates the standard usb_ctrlrequest and adds a pointer
21485 + * to the data returned in the data stage of a 3-stage Control Write requests.
21486 + */
21487 +struct cfi_usb_ctrlrequest {
21488 + uint8_t bRequestType;
21489 + uint8_t bRequest;
21490 + uint16_t wValue;
21491 + uint16_t wIndex;
21492 + uint16_t wLength;
21493 + uint8_t *data;
21494 +} UPACKED;
21495 +
21496 +/*---------------------------------------------------------------------------*/
21497 +
21498 +/**
21499 + * The CFI wrapper of the enabled and activated dwc_otg_pcd_ep structures.
21500 + * This structure is used to store the buffer setup data for any
21501 + * enabled endpoint in the PCD.
21502 + */
21503 +struct cfi_ep {
21504 + /* Entry for the list container */
21505 + dwc_list_link_t lh;
21506 + /* Pointer to the active PCD endpoint structure */
21507 + struct dwc_otg_pcd_ep *ep;
21508 + /* The last descriptor in the chain of DMA descriptors of the endpoint */
21509 + struct dwc_otg_dma_desc *dma_desc_last;
21510 + /* The SG feature value */
21511 + ddma_sg_buffer_setup_t *bm_sg;
21512 + /* The Circular feature value */
21513 + ddma_sg_buffer_setup_t *bm_circ;
21514 + /* The Concatenation feature value */
21515 + ddma_concat_buffer_setup_t *bm_concat;
21516 + /* The Alignment feature value */
21517 + ddma_align_buffer_setup_t *bm_align;
21518 + /* XFER length */
21519 + uint32_t xfer_len;
21520 + /*
21521 + * Count of DMA descriptors currently used.
21522 + * The total should not exceed the MAX_DMA_DESCS_PER_EP value
21523 + * defined in the dwc_otg_cil.h
21524 + */
21525 + uint32_t desc_count;
21526 +};
21527 +typedef struct cfi_ep cfi_ep_t;
21528 +
21529 +typedef struct cfi_dma_buff {
21530 +#define CFI_IN_BUF_LEN 1024
21531 +#define CFI_OUT_BUF_LEN 1024
21532 + dma_addr_t addr;
21533 + uint8_t *buf;
21534 +} cfi_dma_buff_t;
21535 +
21536 +struct cfiobject;
21537 +
21538 +/**
21539 + * This is the interface for the CFI operations.
21540 + *
21541 + * @param ep_enable Called when any endpoint is enabled and activated.
21542 + * @param release Called when the CFI object is released and it needs to correctly
21543 + * deallocate the dynamic memory
21544 + * @param ctrl_write_complete Called when the data stage of the request is complete
21545 + */
21546 +typedef struct cfi_ops {
21547 + int (*ep_enable) (struct cfiobject * cfi, struct dwc_otg_pcd * pcd,
21548 + struct dwc_otg_pcd_ep * ep);
21549 + void *(*ep_alloc_buf) (struct cfiobject * cfi, struct dwc_otg_pcd * pcd,
21550 + struct dwc_otg_pcd_ep * ep, dma_addr_t * dma,
21551 + unsigned size, gfp_t flags);
21552 + void (*release) (struct cfiobject * cfi);
21553 + int (*ctrl_write_complete) (struct cfiobject * cfi,
21554 + struct dwc_otg_pcd * pcd);
21555 + void (*build_descriptors) (struct cfiobject * cfi,
21556 + struct dwc_otg_pcd * pcd,
21557 + struct dwc_otg_pcd_ep * ep,
21558 + dwc_otg_pcd_request_t * req);
21559 +} cfi_ops_t;
21560 +
21561 +struct cfiobject {
21562 + cfi_ops_t ops;
21563 + struct dwc_otg_pcd *pcd;
21564 + struct usb_gadget *gadget;
21565 +
21566 + /* Buffers used to send/receive CFI-related request data */
21567 + cfi_dma_buff_t buf_in;
21568 + cfi_dma_buff_t buf_out;
21569 +
21570 + /* CFI specific Control request wrapper */
21571 + struct cfi_usb_ctrlrequest ctrl_req;
21572 +
21573 + /* The list of active EP's in the PCD of type cfi_ep_t */
21574 + dwc_list_link_t active_eps;
21575 +
21576 + /* This flag shall control the propagation of a specific request
21577 + * to the gadget's processing routines.
21578 + * 0 - no gadget handling
21579 + * 1 - the gadget needs to know about this request (w/o completing a status
21580 + * phase - just return a 0 to the _setup callback)
21581 + */
21582 + uint8_t need_gadget_att;
21583 +
21584 + /* Flag indicating whether the status IN phase needs to be
21585 + * completed by the PCD
21586 + */
21587 + uint8_t need_status_in_complete;
21588 +};
21589 +typedef struct cfiobject cfiobject_t;
21590 +
21591 +#define DUMP_MSG
21592 +
21593 +#if defined(DUMP_MSG)
21594 +static inline void dump_msg(const u8 * buf, unsigned int length)
21595 +{
21596 + unsigned int start, num, i;
21597 + char line[52], *p;
21598 +
21599 + if (length >= 512)
21600 + return;
21601 +
21602 + start = 0;
21603 + while (length > 0) {
21604 + num = min(length, 16u);
21605 + p = line;
21606 + for (i = 0; i < num; ++i) {
21607 + if (i == 8)
21608 + *p++ = ' ';
21609 + DWC_SPRINTF(p, " %02x", buf[i]);
21610 + p += 3;
21611 + }
21612 + *p = 0;
21613 + DWC_DEBUG("%6x: %s\n", start, line);
21614 + buf += num;
21615 + start += num;
21616 + length -= num;
21617 + }
21618 +}
21619 +#else
21620 +static inline void dump_msg(const u8 * buf, unsigned int length)
21621 +{
21622 +}
21623 +#endif
21624 +
21625 +/**
21626 + * This function returns a pointer to cfi_ep_t object with the addr address.
21627 + */
21628 +static inline struct cfi_ep *get_cfi_ep_by_addr(struct cfiobject *cfi,
21629 + uint8_t addr)
21630 +{
21631 + struct cfi_ep *pcfiep;
21632 + dwc_list_link_t *tmp;
21633 +
21634 + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
21635 + pcfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
21636 +
21637 + if (pcfiep->ep->desc->bEndpointAddress == addr) {
21638 + return pcfiep;
21639 + }
21640 + }
21641 +
21642 + return NULL;
21643 +}
21644 +
21645 +/**
21646 + * This function returns a pointer to cfi_ep_t object that matches
21647 + * the dwc_otg_pcd_ep object.
21648 + */
21649 +static inline struct cfi_ep *get_cfi_ep_by_pcd_ep(struct cfiobject *cfi,
21650 + struct dwc_otg_pcd_ep *ep)
21651 +{
21652 + struct cfi_ep *pcfiep = NULL;
21653 + dwc_list_link_t *tmp;
21654 +
21655 + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
21656 + pcfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
21657 + if (pcfiep->ep == ep) {
21658 + return pcfiep;
21659 + }
21660 + }
21661 + return NULL;
21662 +}
21663 +
21664 +int cfi_setup(struct dwc_otg_pcd *pcd, struct cfi_usb_ctrlrequest *ctrl);
21665 +
21666 +#endif /* (__DWC_OTG_CFI_H__) */
21667 --- /dev/null
21668 +++ b/drivers/usb/host/dwc_otg/dwc_otg_cil.c
21669 @@ -0,0 +1,7141 @@
21670 +/* ==========================================================================
21671 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_cil.c $
21672 + * $Revision: #191 $
21673 + * $Date: 2012/08/10 $
21674 + * $Change: 2047372 $
21675 + *
21676 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
21677 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
21678 + * otherwise expressly agreed to in writing between Synopsys and you.
21679 + *
21680 + * The Software IS NOT an item of Licensed Software or Licensed Product under
21681 + * any End User Software License Agreement or Agreement for Licensed Product
21682 + * with Synopsys or any supplement thereto. You are permitted to use and
21683 + * redistribute this Software in source and binary forms, with or without
21684 + * modification, provided that redistributions of source code must retain this
21685 + * notice. You may not view, use, disclose, copy or distribute this file or
21686 + * any information contained herein except pursuant to this license grant from
21687 + * Synopsys. If you do not agree with this notice, including the disclaimer
21688 + * below, then you are not authorized to use the Software.
21689 + *
21690 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
21691 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21692 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21693 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
21694 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21695 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21696 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21697 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21698 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
21699 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
21700 + * DAMAGE.
21701 + * ========================================================================== */
21702 +
21703 +/** @file
21704 + *
21705 + * The Core Interface Layer provides basic services for accessing and
21706 + * managing the DWC_otg hardware. These services are used by both the
21707 + * Host Controller Driver and the Peripheral Controller Driver.
21708 + *
21709 + * The CIL manages the memory map for the core so that the HCD and PCD
21710 + * don't have to do this separately. It also handles basic tasks like
21711 + * reading/writing the registers and data FIFOs in the controller.
21712 + * Some of the data access functions provide encapsulation of several
21713 + * operations required to perform a task, such as writing multiple
21714 + * registers to start a transfer. Finally, the CIL performs basic
21715 + * services that are not specific to either the host or device modes
21716 + * of operation. These services include management of the OTG Host
21717 + * Negotiation Protocol (HNP) and Session Request Protocol (SRP). A
21718 + * Diagnostic API is also provided to allow testing of the controller
21719 + * hardware.
21720 + *
21721 + * The Core Interface Layer has the following requirements:
21722 + * - Provides basic controller operations.
21723 + * - Minimal use of OS services.
21724 + * - The OS services used will be abstracted by using inline functions
21725 + * or macros.
21726 + *
21727 + */
21728 +
21729 +#include "dwc_os.h"
21730 +#include "dwc_otg_regs.h"
21731 +#include "dwc_otg_cil.h"
21732 +
21733 +static int dwc_otg_setup_params(dwc_otg_core_if_t * core_if);
21734 +
21735 +/**
21736 + * This function is called to initialize the DWC_otg CSR data
21737 + * structures. The register addresses in the device and host
21738 + * structures are initialized from the base address supplied by the
21739 + * caller. The calling function must make the OS calls to get the
21740 + * base address of the DWC_otg controller registers. The core_params
21741 + * argument holds the parameters that specify how the core should be
21742 + * configured.
21743 + *
21744 + * @param reg_base_addr Base address of DWC_otg core registers
21745 + *
21746 + */
21747 +dwc_otg_core_if_t *dwc_otg_cil_init(const uint32_t * reg_base_addr)
21748 +{
21749 + dwc_otg_core_if_t *core_if = 0;
21750 + dwc_otg_dev_if_t *dev_if = 0;
21751 + dwc_otg_host_if_t *host_if = 0;
21752 + uint8_t *reg_base = (uint8_t *) reg_base_addr;
21753 + int i = 0;
21754 +
21755 + DWC_DEBUGPL(DBG_CILV, "%s(%p)\n", __func__, reg_base_addr);
21756 +
21757 + core_if = DWC_ALLOC(sizeof(dwc_otg_core_if_t));
21758 +
21759 + if (core_if == NULL) {
21760 + DWC_DEBUGPL(DBG_CIL,
21761 + "Allocation of dwc_otg_core_if_t failed\n");
21762 + return 0;
21763 + }
21764 + core_if->core_global_regs = (dwc_otg_core_global_regs_t *) reg_base;
21765 +
21766 + /*
21767 + * Allocate the Device Mode structures.
21768 + */
21769 + dev_if = DWC_ALLOC(sizeof(dwc_otg_dev_if_t));
21770 +
21771 + if (dev_if == NULL) {
21772 + DWC_DEBUGPL(DBG_CIL, "Allocation of dwc_otg_dev_if_t failed\n");
21773 + DWC_FREE(core_if);
21774 + return 0;
21775 + }
21776 +
21777 + dev_if->dev_global_regs =
21778 + (dwc_otg_device_global_regs_t *) (reg_base +
21779 + DWC_DEV_GLOBAL_REG_OFFSET);
21780 +
21781 + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
21782 + dev_if->in_ep_regs[i] = (dwc_otg_dev_in_ep_regs_t *)
21783 + (reg_base + DWC_DEV_IN_EP_REG_OFFSET +
21784 + (i * DWC_EP_REG_OFFSET));
21785 +
21786 + dev_if->out_ep_regs[i] = (dwc_otg_dev_out_ep_regs_t *)
21787 + (reg_base + DWC_DEV_OUT_EP_REG_OFFSET +
21788 + (i * DWC_EP_REG_OFFSET));
21789 + DWC_DEBUGPL(DBG_CILV, "in_ep_regs[%d]->diepctl=%p\n",
21790 + i, &dev_if->in_ep_regs[i]->diepctl);
21791 + DWC_DEBUGPL(DBG_CILV, "out_ep_regs[%d]->doepctl=%p\n",
21792 + i, &dev_if->out_ep_regs[i]->doepctl);
21793 + }
21794 +
21795 + dev_if->speed = 0; // unknown
21796 +
21797 + core_if->dev_if = dev_if;
21798 +
21799 + /*
21800 + * Allocate the Host Mode structures.
21801 + */
21802 + host_if = DWC_ALLOC(sizeof(dwc_otg_host_if_t));
21803 +
21804 + if (host_if == NULL) {
21805 + DWC_DEBUGPL(DBG_CIL,
21806 + "Allocation of dwc_otg_host_if_t failed\n");
21807 + DWC_FREE(dev_if);
21808 + DWC_FREE(core_if);
21809 + return 0;
21810 + }
21811 +
21812 + host_if->host_global_regs = (dwc_otg_host_global_regs_t *)
21813 + (reg_base + DWC_OTG_HOST_GLOBAL_REG_OFFSET);
21814 +
21815 + host_if->hprt0 =
21816 + (uint32_t *) (reg_base + DWC_OTG_HOST_PORT_REGS_OFFSET);
21817 +
21818 + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
21819 + host_if->hc_regs[i] = (dwc_otg_hc_regs_t *)
21820 + (reg_base + DWC_OTG_HOST_CHAN_REGS_OFFSET +
21821 + (i * DWC_OTG_CHAN_REGS_OFFSET));
21822 + DWC_DEBUGPL(DBG_CILV, "hc_reg[%d]->hcchar=%p\n",
21823 + i, &host_if->hc_regs[i]->hcchar);
21824 + }
21825 +
21826 + host_if->num_host_channels = MAX_EPS_CHANNELS;
21827 + core_if->host_if = host_if;
21828 +
21829 + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
21830 + core_if->data_fifo[i] =
21831 + (uint32_t *) (reg_base + DWC_OTG_DATA_FIFO_OFFSET +
21832 + (i * DWC_OTG_DATA_FIFO_SIZE));
21833 + DWC_DEBUGPL(DBG_CILV, "data_fifo[%d]=0x%08lx\n",
21834 + i, (unsigned long)core_if->data_fifo[i]);
21835 + }
21836 +
21837 + core_if->pcgcctl = (uint32_t *) (reg_base + DWC_OTG_PCGCCTL_OFFSET);
21838 +
21839 + /* Initiate lx_state to L3 disconnected state */
21840 + core_if->lx_state = DWC_OTG_L3;
21841 + /*
21842 + * Store the contents of the hardware configuration registers here for
21843 + * easy access later.
21844 + */
21845 + core_if->hwcfg1.d32 =
21846 + DWC_READ_REG32(&core_if->core_global_regs->ghwcfg1);
21847 + core_if->hwcfg2.d32 =
21848 + DWC_READ_REG32(&core_if->core_global_regs->ghwcfg2);
21849 + core_if->hwcfg3.d32 =
21850 + DWC_READ_REG32(&core_if->core_global_regs->ghwcfg3);
21851 + core_if->hwcfg4.d32 =
21852 + DWC_READ_REG32(&core_if->core_global_regs->ghwcfg4);
21853 +
21854 + /* Force host mode to get HPTXFSIZ exact power on value */
21855 + {
21856 + gusbcfg_data_t gusbcfg = {.d32 = 0 };
21857 + gusbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
21858 + gusbcfg.b.force_host_mode = 1;
21859 + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, gusbcfg.d32);
21860 + dwc_mdelay(100);
21861 + core_if->hptxfsiz.d32 =
21862 + DWC_READ_REG32(&core_if->core_global_regs->hptxfsiz);
21863 + gusbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
21864 + gusbcfg.b.force_host_mode = 1;
21865 + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, gusbcfg.d32);
21866 + dwc_mdelay(100);
21867 + }
21868 +
21869 + DWC_DEBUGPL(DBG_CILV, "hwcfg1=%08x\n", core_if->hwcfg1.d32);
21870 + DWC_DEBUGPL(DBG_CILV, "hwcfg2=%08x\n", core_if->hwcfg2.d32);
21871 + DWC_DEBUGPL(DBG_CILV, "hwcfg3=%08x\n", core_if->hwcfg3.d32);
21872 + DWC_DEBUGPL(DBG_CILV, "hwcfg4=%08x\n", core_if->hwcfg4.d32);
21873 +
21874 + core_if->hcfg.d32 =
21875 + DWC_READ_REG32(&core_if->host_if->host_global_regs->hcfg);
21876 + core_if->dcfg.d32 =
21877 + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
21878 +
21879 + DWC_DEBUGPL(DBG_CILV, "hcfg=%08x\n", core_if->hcfg.d32);
21880 + DWC_DEBUGPL(DBG_CILV, "dcfg=%08x\n", core_if->dcfg.d32);
21881 +
21882 + DWC_DEBUGPL(DBG_CILV, "op_mode=%0x\n", core_if->hwcfg2.b.op_mode);
21883 + DWC_DEBUGPL(DBG_CILV, "arch=%0x\n", core_if->hwcfg2.b.architecture);
21884 + DWC_DEBUGPL(DBG_CILV, "num_dev_ep=%d\n", core_if->hwcfg2.b.num_dev_ep);
21885 + DWC_DEBUGPL(DBG_CILV, "num_host_chan=%d\n",
21886 + core_if->hwcfg2.b.num_host_chan);
21887 + DWC_DEBUGPL(DBG_CILV, "nonperio_tx_q_depth=0x%0x\n",
21888 + core_if->hwcfg2.b.nonperio_tx_q_depth);
21889 + DWC_DEBUGPL(DBG_CILV, "host_perio_tx_q_depth=0x%0x\n",
21890 + core_if->hwcfg2.b.host_perio_tx_q_depth);
21891 + DWC_DEBUGPL(DBG_CILV, "dev_token_q_depth=0x%0x\n",
21892 + core_if->hwcfg2.b.dev_token_q_depth);
21893 +
21894 + DWC_DEBUGPL(DBG_CILV, "Total FIFO SZ=%d\n",
21895 + core_if->hwcfg3.b.dfifo_depth);
21896 + DWC_DEBUGPL(DBG_CILV, "xfer_size_cntr_width=%0x\n",
21897 + core_if->hwcfg3.b.xfer_size_cntr_width);
21898 +
21899 + /*
21900 + * Set the SRP sucess bit for FS-I2c
21901 + */
21902 + core_if->srp_success = 0;
21903 + core_if->srp_timer_started = 0;
21904 +
21905 + /*
21906 + * Create new workqueue and init works
21907 + */
21908 + core_if->wq_otg = DWC_WORKQ_ALLOC("dwc_otg");
21909 + if (core_if->wq_otg == 0) {
21910 + DWC_WARN("DWC_WORKQ_ALLOC failed\n");
21911 + DWC_FREE(host_if);
21912 + DWC_FREE(dev_if);
21913 + DWC_FREE(core_if);
21914 + return 0;
21915 + }
21916 +
21917 + core_if->snpsid = DWC_READ_REG32(&core_if->core_global_regs->gsnpsid);
21918 +
21919 + DWC_PRINTF("Core Release: %x.%x%x%x\n",
21920 + (core_if->snpsid >> 12 & 0xF),
21921 + (core_if->snpsid >> 8 & 0xF),
21922 + (core_if->snpsid >> 4 & 0xF), (core_if->snpsid & 0xF));
21923 +
21924 + core_if->wkp_timer = DWC_TIMER_ALLOC("Wake Up Timer",
21925 + w_wakeup_detected, core_if);
21926 + if (core_if->wkp_timer == 0) {
21927 + DWC_WARN("DWC_TIMER_ALLOC failed\n");
21928 + DWC_FREE(host_if);
21929 + DWC_FREE(dev_if);
21930 + DWC_WORKQ_FREE(core_if->wq_otg);
21931 + DWC_FREE(core_if);
21932 + return 0;
21933 + }
21934 +
21935 + if (dwc_otg_setup_params(core_if)) {
21936 + DWC_WARN("Error while setting core params\n");
21937 + }
21938 +
21939 + core_if->hibernation_suspend = 0;
21940 +
21941 + /** ADP initialization */
21942 + dwc_otg_adp_init(core_if);
21943 +
21944 + return core_if;
21945 +}
21946 +
21947 +/**
21948 + * This function frees the structures allocated by dwc_otg_cil_init().
21949 + *
21950 + * @param core_if The core interface pointer returned from
21951 + * dwc_otg_cil_init().
21952 + *
21953 + */
21954 +void dwc_otg_cil_remove(dwc_otg_core_if_t * core_if)
21955 +{
21956 + dctl_data_t dctl = {.d32 = 0 };
21957 + DWC_DEBUGPL(DBG_CILV, "%s(%p)\n", __func__, core_if);
21958 +
21959 + /* Disable all interrupts */
21960 + DWC_MODIFY_REG32(&core_if->core_global_regs->gahbcfg, 1, 0);
21961 + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, 0);
21962 +
21963 + dctl.b.sftdiscon = 1;
21964 + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
21965 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0,
21966 + dctl.d32);
21967 + }
21968 +
21969 + if (core_if->wq_otg) {
21970 + DWC_WORKQ_WAIT_WORK_DONE(core_if->wq_otg, 500);
21971 + DWC_WORKQ_FREE(core_if->wq_otg);
21972 + }
21973 + if (core_if->dev_if) {
21974 + DWC_FREE(core_if->dev_if);
21975 + }
21976 + if (core_if->host_if) {
21977 + DWC_FREE(core_if->host_if);
21978 + }
21979 +
21980 + /** Remove ADP Stuff */
21981 + dwc_otg_adp_remove(core_if);
21982 + if (core_if->core_params) {
21983 + DWC_FREE(core_if->core_params);
21984 + }
21985 + if (core_if->wkp_timer) {
21986 + DWC_TIMER_FREE(core_if->wkp_timer);
21987 + }
21988 + if (core_if->srp_timer) {
21989 + DWC_TIMER_FREE(core_if->srp_timer);
21990 + }
21991 + DWC_FREE(core_if);
21992 +}
21993 +
21994 +/**
21995 + * This function enables the controller's Global Interrupt in the AHB Config
21996 + * register.
21997 + *
21998 + * @param core_if Programming view of DWC_otg controller.
21999 + */
22000 +void dwc_otg_enable_global_interrupts(dwc_otg_core_if_t * core_if)
22001 +{
22002 + gahbcfg_data_t ahbcfg = {.d32 = 0 };
22003 + ahbcfg.b.glblintrmsk = 1; /* Enable interrupts */
22004 + DWC_MODIFY_REG32(&core_if->core_global_regs->gahbcfg, 0, ahbcfg.d32);
22005 +}
22006 +
22007 +/**
22008 + * This function disables the controller's Global Interrupt in the AHB Config
22009 + * register.
22010 + *
22011 + * @param core_if Programming view of DWC_otg controller.
22012 + */
22013 +void dwc_otg_disable_global_interrupts(dwc_otg_core_if_t * core_if)
22014 +{
22015 + gahbcfg_data_t ahbcfg = {.d32 = 0 };
22016 + ahbcfg.b.glblintrmsk = 1; /* Disable interrupts */
22017 + DWC_MODIFY_REG32(&core_if->core_global_regs->gahbcfg, ahbcfg.d32, 0);
22018 +}
22019 +
22020 +/**
22021 + * This function initializes the commmon interrupts, used in both
22022 + * device and host modes.
22023 + *
22024 + * @param core_if Programming view of the DWC_otg controller
22025 + *
22026 + */
22027 +static void dwc_otg_enable_common_interrupts(dwc_otg_core_if_t * core_if)
22028 +{
22029 + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
22030 + gintmsk_data_t intr_mask = {.d32 = 0 };
22031 +
22032 + /* Clear any pending OTG Interrupts */
22033 + DWC_WRITE_REG32(&global_regs->gotgint, 0xFFFFFFFF);
22034 +
22035 + /* Clear any pending interrupts */
22036 + DWC_WRITE_REG32(&global_regs->gintsts, 0xFFFFFFFF);
22037 +
22038 + /*
22039 + * Enable the interrupts in the GINTMSK.
22040 + */
22041 + intr_mask.b.modemismatch = 1;
22042 + intr_mask.b.otgintr = 1;
22043 +
22044 + if (!core_if->dma_enable) {
22045 + intr_mask.b.rxstsqlvl = 1;
22046 + }
22047 +
22048 + intr_mask.b.conidstschng = 1;
22049 + intr_mask.b.wkupintr = 1;
22050 + intr_mask.b.disconnect = 0;
22051 + intr_mask.b.usbsuspend = 1;
22052 + intr_mask.b.sessreqintr = 1;
22053 +#ifdef CONFIG_USB_DWC_OTG_LPM
22054 + if (core_if->core_params->lpm_enable) {
22055 + intr_mask.b.lpmtranrcvd = 1;
22056 + }
22057 +#endif
22058 + DWC_WRITE_REG32(&global_regs->gintmsk, intr_mask.d32);
22059 +}
22060 +
22061 +/*
22062 + * The restore operation is modified to support Synopsys Emulated Powerdown and
22063 + * Hibernation. This function is for exiting from Device mode hibernation by
22064 + * Host Initiated Resume/Reset and Device Initiated Remote-Wakeup.
22065 + * @param core_if Programming view of DWC_otg controller.
22066 + * @param rem_wakeup - indicates whether resume is initiated by Device or Host.
22067 + * @param reset - indicates whether resume is initiated by Reset.
22068 + */
22069 +int dwc_otg_device_hibernation_restore(dwc_otg_core_if_t * core_if,
22070 + int rem_wakeup, int reset)
22071 +{
22072 + gpwrdn_data_t gpwrdn = {.d32 = 0 };
22073 + pcgcctl_data_t pcgcctl = {.d32 = 0 };
22074 + dctl_data_t dctl = {.d32 = 0 };
22075 +
22076 + int timeout = 2000;
22077 +
22078 + if (!core_if->hibernation_suspend) {
22079 + DWC_PRINTF("Already exited from Hibernation\n");
22080 + return 1;
22081 + }
22082 +
22083 + DWC_DEBUGPL(DBG_PCD, "%s called\n", __FUNCTION__);
22084 + /* Switch-on voltage to the core */
22085 + gpwrdn.b.pwrdnswtch = 1;
22086 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
22087 + dwc_udelay(10);
22088 +
22089 + /* Reset core */
22090 + gpwrdn.d32 = 0;
22091 + gpwrdn.b.pwrdnrstn = 1;
22092 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
22093 + dwc_udelay(10);
22094 +
22095 + /* Assert Restore signal */
22096 + gpwrdn.d32 = 0;
22097 + gpwrdn.b.restore = 1;
22098 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
22099 + dwc_udelay(10);
22100 +
22101 + /* Disable power clamps */
22102 + gpwrdn.d32 = 0;
22103 + gpwrdn.b.pwrdnclmp = 1;
22104 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
22105 +
22106 + if (rem_wakeup) {
22107 + dwc_udelay(70);
22108 + }
22109 +
22110 + /* Deassert Reset core */
22111 + gpwrdn.d32 = 0;
22112 + gpwrdn.b.pwrdnrstn = 1;
22113 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
22114 + dwc_udelay(10);
22115 +
22116 + /* Disable PMU interrupt */
22117 + gpwrdn.d32 = 0;
22118 + gpwrdn.b.pmuintsel = 1;
22119 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
22120 +
22121 + /* Mask interrupts from gpwrdn */
22122 + gpwrdn.d32 = 0;
22123 + gpwrdn.b.connect_det_msk = 1;
22124 + gpwrdn.b.srp_det_msk = 1;
22125 + gpwrdn.b.disconn_det_msk = 1;
22126 + gpwrdn.b.rst_det_msk = 1;
22127 + gpwrdn.b.lnstchng_msk = 1;
22128 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
22129 +
22130 + /* Indicates that we are going out from hibernation */
22131 + core_if->hibernation_suspend = 0;
22132 +
22133 + /*
22134 + * Set Restore Essential Regs bit in PCGCCTL register, restore_mode = 1
22135 + * indicates restore from remote_wakeup
22136 + */
22137 + restore_essential_regs(core_if, rem_wakeup, 0);
22138 +
22139 + /*
22140 + * Wait a little for seeing new value of variable hibernation_suspend if
22141 + * Restore done interrupt received before polling
22142 + */
22143 + dwc_udelay(10);
22144 +
22145 + if (core_if->hibernation_suspend == 0) {
22146 + /*
22147 + * Wait For Restore_done Interrupt. This mechanism of polling the
22148 + * interrupt is introduced to avoid any possible race conditions
22149 + */
22150 + do {
22151 + gintsts_data_t gintsts;
22152 + gintsts.d32 =
22153 + DWC_READ_REG32(&core_if->core_global_regs->gintsts);
22154 + if (gintsts.b.restoredone) {
22155 + gintsts.d32 = 0;
22156 + gintsts.b.restoredone = 1;
22157 + DWC_WRITE_REG32(&core_if->core_global_regs->
22158 + gintsts, gintsts.d32);
22159 + DWC_PRINTF("Restore Done Interrupt seen\n");
22160 + break;
22161 + }
22162 + dwc_udelay(10);
22163 + } while (--timeout);
22164 + if (!timeout) {
22165 + DWC_PRINTF("Restore Done interrupt wasn't generated here\n");
22166 + }
22167 + }
22168 + /* Clear all pending interupts */
22169 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
22170 +
22171 + /* De-assert Restore */
22172 + gpwrdn.d32 = 0;
22173 + gpwrdn.b.restore = 1;
22174 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
22175 + dwc_udelay(10);
22176 +
22177 + if (!rem_wakeup) {
22178 + pcgcctl.d32 = 0;
22179 + pcgcctl.b.rstpdwnmodule = 1;
22180 + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
22181 + }
22182 +
22183 + /* Restore GUSBCFG and DCFG */
22184 + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg,
22185 + core_if->gr_backup->gusbcfg_local);
22186 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg,
22187 + core_if->dr_backup->dcfg);
22188 +
22189 + /* De-assert Wakeup Logic */
22190 + gpwrdn.d32 = 0;
22191 + gpwrdn.b.pmuactv = 1;
22192 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
22193 + dwc_udelay(10);
22194 +
22195 + if (!rem_wakeup) {
22196 + /* Set Device programming done bit */
22197 + dctl.b.pwronprgdone = 1;
22198 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
22199 + } else {
22200 + /* Start Remote Wakeup Signaling */
22201 + dctl.d32 = core_if->dr_backup->dctl;
22202 + dctl.b.rmtwkupsig = 1;
22203 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32);
22204 + }
22205 +
22206 + dwc_mdelay(2);
22207 + /* Clear all pending interupts */
22208 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
22209 +
22210 + /* Restore global registers */
22211 + dwc_otg_restore_global_regs(core_if);
22212 + /* Restore device global registers */
22213 + dwc_otg_restore_dev_regs(core_if, rem_wakeup);
22214 +
22215 + if (rem_wakeup) {
22216 + dwc_mdelay(7);
22217 + dctl.d32 = 0;
22218 + dctl.b.rmtwkupsig = 1;
22219 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32, 0);
22220 + }
22221 +
22222 + core_if->hibernation_suspend = 0;
22223 + /* The core will be in ON STATE */
22224 + core_if->lx_state = DWC_OTG_L0;
22225 + DWC_PRINTF("Hibernation recovery completes here\n");
22226 +
22227 + return 1;
22228 +}
22229 +
22230 +/*
22231 + * The restore operation is modified to support Synopsys Emulated Powerdown and
22232 + * Hibernation. This function is for exiting from Host mode hibernation by
22233 + * Host Initiated Resume/Reset and Device Initiated Remote-Wakeup.
22234 + * @param core_if Programming view of DWC_otg controller.
22235 + * @param rem_wakeup - indicates whether resume is initiated by Device or Host.
22236 + * @param reset - indicates whether resume is initiated by Reset.
22237 + */
22238 +int dwc_otg_host_hibernation_restore(dwc_otg_core_if_t * core_if,
22239 + int rem_wakeup, int reset)
22240 +{
22241 + gpwrdn_data_t gpwrdn = {.d32 = 0 };
22242 + hprt0_data_t hprt0 = {.d32 = 0 };
22243 +
22244 + int timeout = 2000;
22245 +
22246 + DWC_DEBUGPL(DBG_HCD, "%s called\n", __FUNCTION__);
22247 + /* Switch-on voltage to the core */
22248 + gpwrdn.b.pwrdnswtch = 1;
22249 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
22250 + dwc_udelay(10);
22251 +
22252 + /* Reset core */
22253 + gpwrdn.d32 = 0;
22254 + gpwrdn.b.pwrdnrstn = 1;
22255 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
22256 + dwc_udelay(10);
22257 +
22258 + /* Assert Restore signal */
22259 + gpwrdn.d32 = 0;
22260 + gpwrdn.b.restore = 1;
22261 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
22262 + dwc_udelay(10);
22263 +
22264 + /* Disable power clamps */
22265 + gpwrdn.d32 = 0;
22266 + gpwrdn.b.pwrdnclmp = 1;
22267 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
22268 +
22269 + if (!rem_wakeup) {
22270 + dwc_udelay(50);
22271 + }
22272 +
22273 + /* Deassert Reset core */
22274 + gpwrdn.d32 = 0;
22275 + gpwrdn.b.pwrdnrstn = 1;
22276 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
22277 + dwc_udelay(10);
22278 +
22279 + /* Disable PMU interrupt */
22280 + gpwrdn.d32 = 0;
22281 + gpwrdn.b.pmuintsel = 1;
22282 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
22283 +
22284 + gpwrdn.d32 = 0;
22285 + gpwrdn.b.connect_det_msk = 1;
22286 + gpwrdn.b.srp_det_msk = 1;
22287 + gpwrdn.b.disconn_det_msk = 1;
22288 + gpwrdn.b.rst_det_msk = 1;
22289 + gpwrdn.b.lnstchng_msk = 1;
22290 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
22291 +
22292 + /* Indicates that we are going out from hibernation */
22293 + core_if->hibernation_suspend = 0;
22294 +
22295 + /* Set Restore Essential Regs bit in PCGCCTL register */
22296 + restore_essential_regs(core_if, rem_wakeup, 1);
22297 +
22298 + /* Wait a little for seeing new value of variable hibernation_suspend if
22299 + * Restore done interrupt received before polling */
22300 + dwc_udelay(10);
22301 +
22302 + if (core_if->hibernation_suspend == 0) {
22303 + /* Wait For Restore_done Interrupt. This mechanism of polling the
22304 + * interrupt is introduced to avoid any possible race conditions
22305 + */
22306 + do {
22307 + gintsts_data_t gintsts;
22308 + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
22309 + if (gintsts.b.restoredone) {
22310 + gintsts.d32 = 0;
22311 + gintsts.b.restoredone = 1;
22312 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
22313 + DWC_DEBUGPL(DBG_HCD,"Restore Done Interrupt seen\n");
22314 + break;
22315 + }
22316 + dwc_udelay(10);
22317 + } while (--timeout);
22318 + if (!timeout) {
22319 + DWC_WARN("Restore Done interrupt wasn't generated\n");
22320 + }
22321 + }
22322 +
22323 + /* Set the flag's value to 0 again after receiving restore done interrupt */
22324 + core_if->hibernation_suspend = 0;
22325 +
22326 + /* This step is not described in functional spec but if not wait for this
22327 + * delay, mismatch interrupts occurred because just after restore core is
22328 + * in Device mode(gintsts.curmode == 0) */
22329 + dwc_mdelay(100);
22330 +
22331 + /* Clear all pending interrupts */
22332 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
22333 +
22334 + /* De-assert Restore */
22335 + gpwrdn.d32 = 0;
22336 + gpwrdn.b.restore = 1;
22337 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
22338 + dwc_udelay(10);
22339 +
22340 + /* Restore GUSBCFG and HCFG */
22341 + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg,
22342 + core_if->gr_backup->gusbcfg_local);
22343 + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hcfg,
22344 + core_if->hr_backup->hcfg_local);
22345 +
22346 + /* De-assert Wakeup Logic */
22347 + gpwrdn.d32 = 0;
22348 + gpwrdn.b.pmuactv = 1;
22349 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
22350 + dwc_udelay(10);
22351 +
22352 + /* Start the Resume operation by programming HPRT0 */
22353 + hprt0.d32 = core_if->hr_backup->hprt0_local;
22354 + hprt0.b.prtpwr = 1;
22355 + hprt0.b.prtena = 0;
22356 + hprt0.b.prtsusp = 0;
22357 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
22358 +
22359 + DWC_PRINTF("Resume Starts Now\n");
22360 + if (!reset) { // Indicates it is Resume Operation
22361 + hprt0.d32 = core_if->hr_backup->hprt0_local;
22362 + hprt0.b.prtres = 1;
22363 + hprt0.b.prtpwr = 1;
22364 + hprt0.b.prtena = 0;
22365 + hprt0.b.prtsusp = 0;
22366 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
22367 +
22368 + if (!rem_wakeup)
22369 + hprt0.b.prtres = 0;
22370 + /* Wait for Resume time and then program HPRT again */
22371 + dwc_mdelay(100);
22372 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
22373 +
22374 + } else { // Indicates it is Reset Operation
22375 + hprt0.d32 = core_if->hr_backup->hprt0_local;
22376 + hprt0.b.prtrst = 1;
22377 + hprt0.b.prtpwr = 1;
22378 + hprt0.b.prtena = 0;
22379 + hprt0.b.prtsusp = 0;
22380 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
22381 + /* Wait for Reset time and then program HPRT again */
22382 + dwc_mdelay(60);
22383 + hprt0.b.prtrst = 0;
22384 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
22385 + }
22386 + /* Clear all interrupt status */
22387 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
22388 + hprt0.b.prtconndet = 1;
22389 + hprt0.b.prtenchng = 1;
22390 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
22391 +
22392 + /* Clear all pending interupts */
22393 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
22394 +
22395 + /* Restore global registers */
22396 + dwc_otg_restore_global_regs(core_if);
22397 + /* Restore host global registers */
22398 + dwc_otg_restore_host_regs(core_if, reset);
22399 +
22400 + /* The core will be in ON STATE */
22401 + core_if->lx_state = DWC_OTG_L0;
22402 + DWC_PRINTF("Hibernation recovery is complete here\n");
22403 + return 0;
22404 +}
22405 +
22406 +/** Saves some register values into system memory. */
22407 +int dwc_otg_save_global_regs(dwc_otg_core_if_t * core_if)
22408 +{
22409 + struct dwc_otg_global_regs_backup *gr;
22410 + int i;
22411 +
22412 + gr = core_if->gr_backup;
22413 + if (!gr) {
22414 + gr = DWC_ALLOC(sizeof(*gr));
22415 + if (!gr) {
22416 + return -DWC_E_NO_MEMORY;
22417 + }
22418 + core_if->gr_backup = gr;
22419 + }
22420 +
22421 + gr->gotgctl_local = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
22422 + gr->gintmsk_local = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
22423 + gr->gahbcfg_local = DWC_READ_REG32(&core_if->core_global_regs->gahbcfg);
22424 + gr->gusbcfg_local = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
22425 + gr->grxfsiz_local = DWC_READ_REG32(&core_if->core_global_regs->grxfsiz);
22426 + gr->gnptxfsiz_local = DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz);
22427 + gr->hptxfsiz_local = DWC_READ_REG32(&core_if->core_global_regs->hptxfsiz);
22428 +#ifdef CONFIG_USB_DWC_OTG_LPM
22429 + gr->glpmcfg_local = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
22430 +#endif
22431 + gr->gi2cctl_local = DWC_READ_REG32(&core_if->core_global_regs->gi2cctl);
22432 + gr->pcgcctl_local = DWC_READ_REG32(core_if->pcgcctl);
22433 + gr->gdfifocfg_local =
22434 + DWC_READ_REG32(&core_if->core_global_regs->gdfifocfg);
22435 + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
22436 + gr->dtxfsiz_local[i] =
22437 + DWC_READ_REG32(&(core_if->core_global_regs->dtxfsiz[i]));
22438 + }
22439 +
22440 + DWC_DEBUGPL(DBG_ANY, "===========Backing Global registers==========\n");
22441 + DWC_DEBUGPL(DBG_ANY, "Backed up gotgctl = %08x\n", gr->gotgctl_local);
22442 + DWC_DEBUGPL(DBG_ANY, "Backed up gintmsk = %08x\n", gr->gintmsk_local);
22443 + DWC_DEBUGPL(DBG_ANY, "Backed up gahbcfg = %08x\n", gr->gahbcfg_local);
22444 + DWC_DEBUGPL(DBG_ANY, "Backed up gusbcfg = %08x\n", gr->gusbcfg_local);
22445 + DWC_DEBUGPL(DBG_ANY, "Backed up grxfsiz = %08x\n", gr->grxfsiz_local);
22446 + DWC_DEBUGPL(DBG_ANY, "Backed up gnptxfsiz = %08x\n",
22447 + gr->gnptxfsiz_local);
22448 + DWC_DEBUGPL(DBG_ANY, "Backed up hptxfsiz = %08x\n",
22449 + gr->hptxfsiz_local);
22450 +#ifdef CONFIG_USB_DWC_OTG_LPM
22451 + DWC_DEBUGPL(DBG_ANY, "Backed up glpmcfg = %08x\n", gr->glpmcfg_local);
22452 +#endif
22453 + DWC_DEBUGPL(DBG_ANY, "Backed up gi2cctl = %08x\n", gr->gi2cctl_local);
22454 + DWC_DEBUGPL(DBG_ANY, "Backed up pcgcctl = %08x\n", gr->pcgcctl_local);
22455 + DWC_DEBUGPL(DBG_ANY,"Backed up gdfifocfg = %08x\n",gr->gdfifocfg_local);
22456 +
22457 + return 0;
22458 +}
22459 +
22460 +/** Saves GINTMSK register before setting the msk bits. */
22461 +int dwc_otg_save_gintmsk_reg(dwc_otg_core_if_t * core_if)
22462 +{
22463 + struct dwc_otg_global_regs_backup *gr;
22464 +
22465 + gr = core_if->gr_backup;
22466 + if (!gr) {
22467 + gr = DWC_ALLOC(sizeof(*gr));
22468 + if (!gr) {
22469 + return -DWC_E_NO_MEMORY;
22470 + }
22471 + core_if->gr_backup = gr;
22472 + }
22473 +
22474 + gr->gintmsk_local = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
22475 +
22476 + DWC_DEBUGPL(DBG_ANY,"=============Backing GINTMSK registers============\n");
22477 + DWC_DEBUGPL(DBG_ANY, "Backed up gintmsk = %08x\n", gr->gintmsk_local);
22478 +
22479 + return 0;
22480 +}
22481 +
22482 +int dwc_otg_save_dev_regs(dwc_otg_core_if_t * core_if)
22483 +{
22484 + struct dwc_otg_dev_regs_backup *dr;
22485 + int i;
22486 +
22487 + dr = core_if->dr_backup;
22488 + if (!dr) {
22489 + dr = DWC_ALLOC(sizeof(*dr));
22490 + if (!dr) {
22491 + return -DWC_E_NO_MEMORY;
22492 + }
22493 + core_if->dr_backup = dr;
22494 + }
22495 +
22496 + dr->dcfg = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
22497 + dr->dctl = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dctl);
22498 + dr->daintmsk =
22499 + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->daintmsk);
22500 + dr->diepmsk =
22501 + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->diepmsk);
22502 + dr->doepmsk =
22503 + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->doepmsk);
22504 +
22505 + for (i = 0; i < core_if->dev_if->num_in_eps; ++i) {
22506 + dr->diepctl[i] =
22507 + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[i]->diepctl);
22508 + dr->dieptsiz[i] =
22509 + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[i]->dieptsiz);
22510 + dr->diepdma[i] =
22511 + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[i]->diepdma);
22512 + }
22513 +
22514 + DWC_DEBUGPL(DBG_ANY,
22515 + "=============Backing Host registers==============\n");
22516 + DWC_DEBUGPL(DBG_ANY, "Backed up dcfg = %08x\n", dr->dcfg);
22517 + DWC_DEBUGPL(DBG_ANY, "Backed up dctl = %08x\n", dr->dctl);
22518 + DWC_DEBUGPL(DBG_ANY, "Backed up daintmsk = %08x\n",
22519 + dr->daintmsk);
22520 + DWC_DEBUGPL(DBG_ANY, "Backed up diepmsk = %08x\n", dr->diepmsk);
22521 + DWC_DEBUGPL(DBG_ANY, "Backed up doepmsk = %08x\n", dr->doepmsk);
22522 + for (i = 0; i < core_if->dev_if->num_in_eps; ++i) {
22523 + DWC_DEBUGPL(DBG_ANY, "Backed up diepctl[%d] = %08x\n", i,
22524 + dr->diepctl[i]);
22525 + DWC_DEBUGPL(DBG_ANY, "Backed up dieptsiz[%d] = %08x\n",
22526 + i, dr->dieptsiz[i]);
22527 + DWC_DEBUGPL(DBG_ANY, "Backed up diepdma[%d] = %08x\n", i,
22528 + dr->diepdma[i]);
22529 + }
22530 +
22531 + return 0;
22532 +}
22533 +
22534 +int dwc_otg_save_host_regs(dwc_otg_core_if_t * core_if)
22535 +{
22536 + struct dwc_otg_host_regs_backup *hr;
22537 + int i;
22538 +
22539 + hr = core_if->hr_backup;
22540 + if (!hr) {
22541 + hr = DWC_ALLOC(sizeof(*hr));
22542 + if (!hr) {
22543 + return -DWC_E_NO_MEMORY;
22544 + }
22545 + core_if->hr_backup = hr;
22546 + }
22547 +
22548 + hr->hcfg_local =
22549 + DWC_READ_REG32(&core_if->host_if->host_global_regs->hcfg);
22550 + hr->haintmsk_local =
22551 + DWC_READ_REG32(&core_if->host_if->host_global_regs->haintmsk);
22552 + for (i = 0; i < dwc_otg_get_param_host_channels(core_if); ++i) {
22553 + hr->hcintmsk_local[i] =
22554 + DWC_READ_REG32(&core_if->host_if->hc_regs[i]->hcintmsk);
22555 + }
22556 + hr->hprt0_local = DWC_READ_REG32(core_if->host_if->hprt0);
22557 + hr->hfir_local =
22558 + DWC_READ_REG32(&core_if->host_if->host_global_regs->hfir);
22559 +
22560 + DWC_DEBUGPL(DBG_ANY,
22561 + "=============Backing Host registers===============\n");
22562 + DWC_DEBUGPL(DBG_ANY, "Backed up hcfg = %08x\n",
22563 + hr->hcfg_local);
22564 + DWC_DEBUGPL(DBG_ANY, "Backed up haintmsk = %08x\n", hr->haintmsk_local);
22565 + for (i = 0; i < dwc_otg_get_param_host_channels(core_if); ++i) {
22566 + DWC_DEBUGPL(DBG_ANY, "Backed up hcintmsk[%02d]=%08x\n", i,
22567 + hr->hcintmsk_local[i]);
22568 + }
22569 + DWC_DEBUGPL(DBG_ANY, "Backed up hprt0 = %08x\n",
22570 + hr->hprt0_local);
22571 + DWC_DEBUGPL(DBG_ANY, "Backed up hfir = %08x\n",
22572 + hr->hfir_local);
22573 +
22574 + return 0;
22575 +}
22576 +
22577 +int dwc_otg_restore_global_regs(dwc_otg_core_if_t *core_if)
22578 +{
22579 + struct dwc_otg_global_regs_backup *gr;
22580 + int i;
22581 +
22582 + gr = core_if->gr_backup;
22583 + if (!gr) {
22584 + return -DWC_E_INVALID;
22585 + }
22586 +
22587 + DWC_WRITE_REG32(&core_if->core_global_regs->gotgctl, gr->gotgctl_local);
22588 + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, gr->gintmsk_local);
22589 + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, gr->gusbcfg_local);
22590 + DWC_WRITE_REG32(&core_if->core_global_regs->gahbcfg, gr->gahbcfg_local);
22591 + DWC_WRITE_REG32(&core_if->core_global_regs->grxfsiz, gr->grxfsiz_local);
22592 + DWC_WRITE_REG32(&core_if->core_global_regs->gnptxfsiz,
22593 + gr->gnptxfsiz_local);
22594 + DWC_WRITE_REG32(&core_if->core_global_regs->hptxfsiz,
22595 + gr->hptxfsiz_local);
22596 + DWC_WRITE_REG32(&core_if->core_global_regs->gdfifocfg,
22597 + gr->gdfifocfg_local);
22598 + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
22599 + DWC_WRITE_REG32(&core_if->core_global_regs->dtxfsiz[i],
22600 + gr->dtxfsiz_local[i]);
22601 + }
22602 +
22603 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
22604 + DWC_WRITE_REG32(core_if->host_if->hprt0, 0x0000100A);
22605 + DWC_WRITE_REG32(&core_if->core_global_regs->gahbcfg,
22606 + (gr->gahbcfg_local));
22607 + return 0;
22608 +}
22609 +
22610 +int dwc_otg_restore_dev_regs(dwc_otg_core_if_t * core_if, int rem_wakeup)
22611 +{
22612 + struct dwc_otg_dev_regs_backup *dr;
22613 + int i;
22614 +
22615 + dr = core_if->dr_backup;
22616 +
22617 + if (!dr) {
22618 + return -DWC_E_INVALID;
22619 + }
22620 +
22621 + if (!rem_wakeup) {
22622 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl,
22623 + dr->dctl);
22624 + }
22625 +
22626 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->daintmsk, dr->daintmsk);
22627 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->diepmsk, dr->diepmsk);
22628 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->doepmsk, dr->doepmsk);
22629 +
22630 + for (i = 0; i < core_if->dev_if->num_in_eps; ++i) {
22631 + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[i]->dieptsiz, dr->dieptsiz[i]);
22632 + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[i]->diepdma, dr->diepdma[i]);
22633 + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[i]->diepctl, dr->diepctl[i]);
22634 + }
22635 +
22636 + return 0;
22637 +}
22638 +
22639 +int dwc_otg_restore_host_regs(dwc_otg_core_if_t * core_if, int reset)
22640 +{
22641 + struct dwc_otg_host_regs_backup *hr;
22642 + int i;
22643 + hr = core_if->hr_backup;
22644 +
22645 + if (!hr) {
22646 + return -DWC_E_INVALID;
22647 + }
22648 +
22649 + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hcfg, hr->hcfg_local);
22650 + //if (!reset)
22651 + //{
22652 + // DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hfir, hr->hfir_local);
22653 + //}
22654 +
22655 + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->haintmsk,
22656 + hr->haintmsk_local);
22657 + for (i = 0; i < dwc_otg_get_param_host_channels(core_if); ++i) {
22658 + DWC_WRITE_REG32(&core_if->host_if->hc_regs[i]->hcintmsk,
22659 + hr->hcintmsk_local[i]);
22660 + }
22661 +
22662 + return 0;
22663 +}
22664 +
22665 +int restore_lpm_i2c_regs(dwc_otg_core_if_t * core_if)
22666 +{
22667 + struct dwc_otg_global_regs_backup *gr;
22668 +
22669 + gr = core_if->gr_backup;
22670 +
22671 + /* Restore values for LPM and I2C */
22672 +#ifdef CONFIG_USB_DWC_OTG_LPM
22673 + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg, gr->glpmcfg_local);
22674 +#endif
22675 + DWC_WRITE_REG32(&core_if->core_global_regs->gi2cctl, gr->gi2cctl_local);
22676 +
22677 + return 0;
22678 +}
22679 +
22680 +int restore_essential_regs(dwc_otg_core_if_t * core_if, int rmode, int is_host)
22681 +{
22682 + struct dwc_otg_global_regs_backup *gr;
22683 + pcgcctl_data_t pcgcctl = {.d32 = 0 };
22684 + gahbcfg_data_t gahbcfg = {.d32 = 0 };
22685 + gusbcfg_data_t gusbcfg = {.d32 = 0 };
22686 + gintmsk_data_t gintmsk = {.d32 = 0 };
22687 +
22688 + /* Restore LPM and I2C registers */
22689 + restore_lpm_i2c_regs(core_if);
22690 +
22691 + /* Set PCGCCTL to 0 */
22692 + DWC_WRITE_REG32(core_if->pcgcctl, 0x00000000);
22693 +
22694 + gr = core_if->gr_backup;
22695 + /* Load restore values for [31:14] bits */
22696 + DWC_WRITE_REG32(core_if->pcgcctl,
22697 + ((gr->pcgcctl_local & 0xffffc000) | 0x00020000));
22698 +
22699 + /* Umnask global Interrupt in GAHBCFG and restore it */
22700 + gahbcfg.d32 = gr->gahbcfg_local;
22701 + gahbcfg.b.glblintrmsk = 1;
22702 + DWC_WRITE_REG32(&core_if->core_global_regs->gahbcfg, gahbcfg.d32);
22703 +
22704 + /* Clear all pending interupts */
22705 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
22706 +
22707 + /* Unmask restore done interrupt */
22708 + gintmsk.b.restoredone = 1;
22709 + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, gintmsk.d32);
22710 +
22711 + /* Restore GUSBCFG and HCFG/DCFG */
22712 + gusbcfg.d32 = core_if->gr_backup->gusbcfg_local;
22713 + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, gusbcfg.d32);
22714 +
22715 + if (is_host) {
22716 + hcfg_data_t hcfg = {.d32 = 0 };
22717 + hcfg.d32 = core_if->hr_backup->hcfg_local;
22718 + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hcfg,
22719 + hcfg.d32);
22720 +
22721 + /* Load restore values for [31:14] bits */
22722 + pcgcctl.d32 = gr->pcgcctl_local & 0xffffc000;
22723 + pcgcctl.d32 = gr->pcgcctl_local | 0x00020000;
22724 +
22725 + if (rmode)
22726 + pcgcctl.b.restoremode = 1;
22727 + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
22728 + dwc_udelay(10);
22729 +
22730 + /* Load restore values for [31:14] bits and set EssRegRestored bit */
22731 + pcgcctl.d32 = gr->pcgcctl_local | 0xffffc000;
22732 + pcgcctl.d32 = gr->pcgcctl_local & 0xffffc000;
22733 + pcgcctl.b.ess_reg_restored = 1;
22734 + if (rmode)
22735 + pcgcctl.b.restoremode = 1;
22736 + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
22737 + } else {
22738 + dcfg_data_t dcfg = {.d32 = 0 };
22739 + dcfg.d32 = core_if->dr_backup->dcfg;
22740 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg, dcfg.d32);
22741 +
22742 + /* Load restore values for [31:14] bits */
22743 + pcgcctl.d32 = gr->pcgcctl_local & 0xffffc000;
22744 + pcgcctl.d32 = gr->pcgcctl_local | 0x00020000;
22745 + if (!rmode) {
22746 + pcgcctl.d32 |= 0x208;
22747 + }
22748 + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
22749 + dwc_udelay(10);
22750 +
22751 + /* Load restore values for [31:14] bits */
22752 + pcgcctl.d32 = gr->pcgcctl_local & 0xffffc000;
22753 + pcgcctl.d32 = gr->pcgcctl_local | 0x00020000;
22754 + pcgcctl.b.ess_reg_restored = 1;
22755 + if (!rmode)
22756 + pcgcctl.d32 |= 0x208;
22757 + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
22758 + }
22759 +
22760 + return 0;
22761 +}
22762 +
22763 +/**
22764 + * Initializes the FSLSPClkSel field of the HCFG register depending on the PHY
22765 + * type.
22766 + */
22767 +static void init_fslspclksel(dwc_otg_core_if_t * core_if)
22768 +{
22769 + uint32_t val;
22770 + hcfg_data_t hcfg;
22771 +
22772 + if (((core_if->hwcfg2.b.hs_phy_type == 2) &&
22773 + (core_if->hwcfg2.b.fs_phy_type == 1) &&
22774 + (core_if->core_params->ulpi_fs_ls)) ||
22775 + (core_if->core_params->phy_type == DWC_PHY_TYPE_PARAM_FS)) {
22776 + /* Full speed PHY */
22777 + val = DWC_HCFG_48_MHZ;
22778 + } else {
22779 + /* High speed PHY running at full speed or high speed */
22780 + val = DWC_HCFG_30_60_MHZ;
22781 + }
22782 +
22783 + DWC_DEBUGPL(DBG_CIL, "Initializing HCFG.FSLSPClkSel to 0x%1x\n", val);
22784 + hcfg.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->hcfg);
22785 + hcfg.b.fslspclksel = val;
22786 + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hcfg, hcfg.d32);
22787 +}
22788 +
22789 +/**
22790 + * Initializes the DevSpd field of the DCFG register depending on the PHY type
22791 + * and the enumeration speed of the device.
22792 + */
22793 +static void init_devspd(dwc_otg_core_if_t * core_if)
22794 +{
22795 + uint32_t val;
22796 + dcfg_data_t dcfg;
22797 +
22798 + if (((core_if->hwcfg2.b.hs_phy_type == 2) &&
22799 + (core_if->hwcfg2.b.fs_phy_type == 1) &&
22800 + (core_if->core_params->ulpi_fs_ls)) ||
22801 + (core_if->core_params->phy_type == DWC_PHY_TYPE_PARAM_FS)) {
22802 + /* Full speed PHY */
22803 + val = 0x3;
22804 + } else if (core_if->core_params->speed == DWC_SPEED_PARAM_FULL) {
22805 + /* High speed PHY running at full speed */
22806 + val = 0x1;
22807 + } else {
22808 + /* High speed PHY running at high speed */
22809 + val = 0x0;
22810 + }
22811 +
22812 + DWC_DEBUGPL(DBG_CIL, "Initializing DCFG.DevSpd to 0x%1x\n", val);
22813 +
22814 + dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
22815 + dcfg.b.devspd = val;
22816 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg, dcfg.d32);
22817 +}
22818 +
22819 +/**
22820 + * This function calculates the number of IN EPS
22821 + * using GHWCFG1 and GHWCFG2 registers values
22822 + *
22823 + * @param core_if Programming view of the DWC_otg controller
22824 + */
22825 +static uint32_t calc_num_in_eps(dwc_otg_core_if_t * core_if)
22826 +{
22827 + uint32_t num_in_eps = 0;
22828 + uint32_t num_eps = core_if->hwcfg2.b.num_dev_ep;
22829 + uint32_t hwcfg1 = core_if->hwcfg1.d32 >> 3;
22830 + uint32_t num_tx_fifos = core_if->hwcfg4.b.num_in_eps;
22831 + int i;
22832 +
22833 + for (i = 0; i < num_eps; ++i) {
22834 + if (!(hwcfg1 & 0x1))
22835 + num_in_eps++;
22836 +
22837 + hwcfg1 >>= 2;
22838 + }
22839 +
22840 + if (core_if->hwcfg4.b.ded_fifo_en) {
22841 + num_in_eps =
22842 + (num_in_eps > num_tx_fifos) ? num_tx_fifos : num_in_eps;
22843 + }
22844 +
22845 + return num_in_eps;
22846 +}
22847 +
22848 +/**
22849 + * This function calculates the number of OUT EPS
22850 + * using GHWCFG1 and GHWCFG2 registers values
22851 + *
22852 + * @param core_if Programming view of the DWC_otg controller
22853 + */
22854 +static uint32_t calc_num_out_eps(dwc_otg_core_if_t * core_if)
22855 +{
22856 + uint32_t num_out_eps = 0;
22857 + uint32_t num_eps = core_if->hwcfg2.b.num_dev_ep;
22858 + uint32_t hwcfg1 = core_if->hwcfg1.d32 >> 2;
22859 + int i;
22860 +
22861 + for (i = 0; i < num_eps; ++i) {
22862 + if (!(hwcfg1 & 0x1))
22863 + num_out_eps++;
22864 +
22865 + hwcfg1 >>= 2;
22866 + }
22867 + return num_out_eps;
22868 +}
22869 +
22870 +/**
22871 + * This function initializes the DWC_otg controller registers and
22872 + * prepares the core for device mode or host mode operation.
22873 + *
22874 + * @param core_if Programming view of the DWC_otg controller
22875 + *
22876 + */
22877 +void dwc_otg_core_init(dwc_otg_core_if_t * core_if)
22878 +{
22879 + int i = 0;
22880 + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
22881 + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
22882 + gahbcfg_data_t ahbcfg = {.d32 = 0 };
22883 + gusbcfg_data_t usbcfg = {.d32 = 0 };
22884 + gi2cctl_data_t i2cctl = {.d32 = 0 };
22885 +
22886 + DWC_DEBUGPL(DBG_CILV, "dwc_otg_core_init(%p) regs at %p\n",
22887 + core_if, global_regs);
22888 +
22889 + /* Common Initialization */
22890 + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
22891 +
22892 + /* Program the ULPI External VBUS bit if needed */
22893 + usbcfg.b.ulpi_ext_vbus_drv =
22894 + (core_if->core_params->phy_ulpi_ext_vbus ==
22895 + DWC_PHY_ULPI_EXTERNAL_VBUS) ? 1 : 0;
22896 +
22897 + /* Set external TS Dline pulsing */
22898 + usbcfg.b.term_sel_dl_pulse =
22899 + (core_if->core_params->ts_dline == 1) ? 1 : 0;
22900 + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
22901 +
22902 + /* Reset the Controller */
22903 + dwc_otg_core_reset(core_if);
22904 +
22905 + core_if->adp_enable = core_if->core_params->adp_supp_enable;
22906 + core_if->power_down = core_if->core_params->power_down;
22907 + core_if->otg_sts = 0;
22908 +
22909 + /* Initialize parameters from Hardware configuration registers. */
22910 + dev_if->num_in_eps = calc_num_in_eps(core_if);
22911 + dev_if->num_out_eps = calc_num_out_eps(core_if);
22912 +
22913 + DWC_DEBUGPL(DBG_CIL, "num_dev_perio_in_ep=%d\n",
22914 + core_if->hwcfg4.b.num_dev_perio_in_ep);
22915 +
22916 + for (i = 0; i < core_if->hwcfg4.b.num_dev_perio_in_ep; i++) {
22917 + dev_if->perio_tx_fifo_size[i] =
22918 + DWC_READ_REG32(&global_regs->dtxfsiz[i]) >> 16;
22919 + DWC_DEBUGPL(DBG_CIL, "Periodic Tx FIFO SZ #%d=0x%0x\n",
22920 + i, dev_if->perio_tx_fifo_size[i]);
22921 + }
22922 +
22923 + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
22924 + dev_if->tx_fifo_size[i] =
22925 + DWC_READ_REG32(&global_regs->dtxfsiz[i]) >> 16;
22926 + DWC_DEBUGPL(DBG_CIL, "Tx FIFO SZ #%d=0x%0x\n",
22927 + i, dev_if->tx_fifo_size[i]);
22928 + }
22929 +
22930 + core_if->total_fifo_size = core_if->hwcfg3.b.dfifo_depth;
22931 + core_if->rx_fifo_size = DWC_READ_REG32(&global_regs->grxfsiz);
22932 + core_if->nperio_tx_fifo_size =
22933 + DWC_READ_REG32(&global_regs->gnptxfsiz) >> 16;
22934 +
22935 + DWC_DEBUGPL(DBG_CIL, "Total FIFO SZ=%d\n", core_if->total_fifo_size);
22936 + DWC_DEBUGPL(DBG_CIL, "Rx FIFO SZ=%d\n", core_if->rx_fifo_size);
22937 + DWC_DEBUGPL(DBG_CIL, "NP Tx FIFO SZ=%d\n",
22938 + core_if->nperio_tx_fifo_size);
22939 +
22940 + /* This programming sequence needs to happen in FS mode before any other
22941 + * programming occurs */
22942 + if ((core_if->core_params->speed == DWC_SPEED_PARAM_FULL) &&
22943 + (core_if->core_params->phy_type == DWC_PHY_TYPE_PARAM_FS)) {
22944 + /* If FS mode with FS PHY */
22945 +
22946 + /* core_init() is now called on every switch so only call the
22947 + * following for the first time through. */
22948 + if (!core_if->phy_init_done) {
22949 + core_if->phy_init_done = 1;
22950 + DWC_DEBUGPL(DBG_CIL, "FS_PHY detected\n");
22951 + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
22952 + usbcfg.b.physel = 1;
22953 + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
22954 +
22955 + /* Reset after a PHY select */
22956 + dwc_otg_core_reset(core_if);
22957 + }
22958 +
22959 + /* Program DCFG.DevSpd or HCFG.FSLSPclkSel to 48Mhz in FS. Also
22960 + * do this on HNP Dev/Host mode switches (done in dev_init and
22961 + * host_init). */
22962 + if (dwc_otg_is_host_mode(core_if)) {
22963 + init_fslspclksel(core_if);
22964 + } else {
22965 + init_devspd(core_if);
22966 + }
22967 +
22968 + if (core_if->core_params->i2c_enable) {
22969 + DWC_DEBUGPL(DBG_CIL, "FS_PHY Enabling I2c\n");
22970 + /* Program GUSBCFG.OtgUtmifsSel to I2C */
22971 + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
22972 + usbcfg.b.otgutmifssel = 1;
22973 + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
22974 +
22975 + /* Program GI2CCTL.I2CEn */
22976 + i2cctl.d32 = DWC_READ_REG32(&global_regs->gi2cctl);
22977 + i2cctl.b.i2cdevaddr = 1;
22978 + i2cctl.b.i2cen = 0;
22979 + DWC_WRITE_REG32(&global_regs->gi2cctl, i2cctl.d32);
22980 + i2cctl.b.i2cen = 1;
22981 + DWC_WRITE_REG32(&global_regs->gi2cctl, i2cctl.d32);
22982 + }
22983 +
22984 + } /* endif speed == DWC_SPEED_PARAM_FULL */
22985 + else {
22986 + /* High speed PHY. */
22987 + if (!core_if->phy_init_done) {
22988 + core_if->phy_init_done = 1;
22989 + /* HS PHY parameters. These parameters are preserved
22990 + * during soft reset so only program the first time. Do
22991 + * a soft reset immediately after setting phyif. */
22992 +
22993 + if (core_if->core_params->phy_type == 2) {
22994 + /* ULPI interface */
22995 + usbcfg.b.ulpi_utmi_sel = 1;
22996 + usbcfg.b.phyif = 0;
22997 + usbcfg.b.ddrsel =
22998 + core_if->core_params->phy_ulpi_ddr;
22999 + } else if (core_if->core_params->phy_type == 1) {
23000 + /* UTMI+ interface */
23001 + usbcfg.b.ulpi_utmi_sel = 0;
23002 + if (core_if->core_params->phy_utmi_width == 16) {
23003 + usbcfg.b.phyif = 1;
23004 +
23005 + } else {
23006 + usbcfg.b.phyif = 0;
23007 + }
23008 + } else {
23009 + DWC_ERROR("FS PHY TYPE\n");
23010 + }
23011 + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
23012 + /* Reset after setting the PHY parameters */
23013 + dwc_otg_core_reset(core_if);
23014 + }
23015 + }
23016 +
23017 + if ((core_if->hwcfg2.b.hs_phy_type == 2) &&
23018 + (core_if->hwcfg2.b.fs_phy_type == 1) &&
23019 + (core_if->core_params->ulpi_fs_ls)) {
23020 + DWC_DEBUGPL(DBG_CIL, "Setting ULPI FSLS\n");
23021 + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
23022 + usbcfg.b.ulpi_fsls = 1;
23023 + usbcfg.b.ulpi_clk_sus_m = 1;
23024 + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
23025 + } else {
23026 + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
23027 + usbcfg.b.ulpi_fsls = 0;
23028 + usbcfg.b.ulpi_clk_sus_m = 0;
23029 + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
23030 + }
23031 +
23032 + /* Program the GAHBCFG Register. */
23033 + switch (core_if->hwcfg2.b.architecture) {
23034 +
23035 + case DWC_SLAVE_ONLY_ARCH:
23036 + DWC_DEBUGPL(DBG_CIL, "Slave Only Mode\n");
23037 + ahbcfg.b.nptxfemplvl_txfemplvl =
23038 + DWC_GAHBCFG_TXFEMPTYLVL_HALFEMPTY;
23039 + ahbcfg.b.ptxfemplvl = DWC_GAHBCFG_TXFEMPTYLVL_HALFEMPTY;
23040 + core_if->dma_enable = 0;
23041 + core_if->dma_desc_enable = 0;
23042 + break;
23043 +
23044 + case DWC_EXT_DMA_ARCH:
23045 + DWC_DEBUGPL(DBG_CIL, "External DMA Mode\n");
23046 + {
23047 + uint8_t brst_sz = core_if->core_params->dma_burst_size;
23048 + ahbcfg.b.hburstlen = 0;
23049 + while (brst_sz > 1) {
23050 + ahbcfg.b.hburstlen++;
23051 + brst_sz >>= 1;
23052 + }
23053 + }
23054 + core_if->dma_enable = (core_if->core_params->dma_enable != 0);
23055 + core_if->dma_desc_enable =
23056 + (core_if->core_params->dma_desc_enable != 0);
23057 + break;
23058 +
23059 + case DWC_INT_DMA_ARCH:
23060 + DWC_DEBUGPL(DBG_CIL, "Internal DMA Mode\n");
23061 + /* Old value was DWC_GAHBCFG_INT_DMA_BURST_INCR - done for
23062 + Host mode ISOC in issue fix - vahrama */
23063 + /* Broadcom had altered to (1<<3)|(0<<0) - WRESP=1, max 4 beats */
23064 + ahbcfg.b.hburstlen = (1<<3)|(0<<0);//DWC_GAHBCFG_INT_DMA_BURST_INCR4;
23065 + core_if->dma_enable = (core_if->core_params->dma_enable != 0);
23066 + core_if->dma_desc_enable =
23067 + (core_if->core_params->dma_desc_enable != 0);
23068 + break;
23069 +
23070 + }
23071 + if (core_if->dma_enable) {
23072 + if (core_if->dma_desc_enable) {
23073 + DWC_PRINTF("Using Descriptor DMA mode\n");
23074 + } else {
23075 + DWC_PRINTF("Using Buffer DMA mode\n");
23076 +
23077 + }
23078 + } else {
23079 + DWC_PRINTF("Using Slave mode\n");
23080 + core_if->dma_desc_enable = 0;
23081 + }
23082 +
23083 + if (core_if->core_params->ahb_single) {
23084 + ahbcfg.b.ahbsingle = 1;
23085 + }
23086 +
23087 + ahbcfg.b.dmaenable = core_if->dma_enable;
23088 + DWC_WRITE_REG32(&global_regs->gahbcfg, ahbcfg.d32);
23089 +
23090 + core_if->en_multiple_tx_fifo = core_if->hwcfg4.b.ded_fifo_en;
23091 +
23092 + core_if->pti_enh_enable = core_if->core_params->pti_enable != 0;
23093 + core_if->multiproc_int_enable = core_if->core_params->mpi_enable;
23094 + DWC_PRINTF("Periodic Transfer Interrupt Enhancement - %s\n",
23095 + ((core_if->pti_enh_enable) ? "enabled" : "disabled"));
23096 + DWC_PRINTF("Multiprocessor Interrupt Enhancement - %s\n",
23097 + ((core_if->multiproc_int_enable) ? "enabled" : "disabled"));
23098 +
23099 + /*
23100 + * Program the GUSBCFG register.
23101 + */
23102 + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
23103 +
23104 + switch (core_if->hwcfg2.b.op_mode) {
23105 + case DWC_MODE_HNP_SRP_CAPABLE:
23106 + usbcfg.b.hnpcap = (core_if->core_params->otg_cap ==
23107 + DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE);
23108 + usbcfg.b.srpcap = (core_if->core_params->otg_cap !=
23109 + DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE);
23110 + break;
23111 +
23112 + case DWC_MODE_SRP_ONLY_CAPABLE:
23113 + usbcfg.b.hnpcap = 0;
23114 + usbcfg.b.srpcap = (core_if->core_params->otg_cap !=
23115 + DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE);
23116 + break;
23117 +
23118 + case DWC_MODE_NO_HNP_SRP_CAPABLE:
23119 + usbcfg.b.hnpcap = 0;
23120 + usbcfg.b.srpcap = 0;
23121 + break;
23122 +
23123 + case DWC_MODE_SRP_CAPABLE_DEVICE:
23124 + usbcfg.b.hnpcap = 0;
23125 + usbcfg.b.srpcap = (core_if->core_params->otg_cap !=
23126 + DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE);
23127 + break;
23128 +
23129 + case DWC_MODE_NO_SRP_CAPABLE_DEVICE:
23130 + usbcfg.b.hnpcap = 0;
23131 + usbcfg.b.srpcap = 0;
23132 + break;
23133 +
23134 + case DWC_MODE_SRP_CAPABLE_HOST:
23135 + usbcfg.b.hnpcap = 0;
23136 + usbcfg.b.srpcap = (core_if->core_params->otg_cap !=
23137 + DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE);
23138 + break;
23139 +
23140 + case DWC_MODE_NO_SRP_CAPABLE_HOST:
23141 + usbcfg.b.hnpcap = 0;
23142 + usbcfg.b.srpcap = 0;
23143 + break;
23144 + }
23145 +
23146 + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
23147 +
23148 +#ifdef CONFIG_USB_DWC_OTG_LPM
23149 + if (core_if->core_params->lpm_enable) {
23150 + glpmcfg_data_t lpmcfg = {.d32 = 0 };
23151 +
23152 + /* To enable LPM support set lpm_cap_en bit */
23153 + lpmcfg.b.lpm_cap_en = 1;
23154 +
23155 + /* Make AppL1Res ACK */
23156 + lpmcfg.b.appl_resp = 1;
23157 +
23158 + /* Retry 3 times */
23159 + lpmcfg.b.retry_count = 3;
23160 +
23161 + DWC_MODIFY_REG32(&core_if->core_global_regs->glpmcfg,
23162 + 0, lpmcfg.d32);
23163 +
23164 + }
23165 +#endif
23166 + if (core_if->core_params->ic_usb_cap) {
23167 + gusbcfg_data_t gusbcfg = {.d32 = 0 };
23168 + gusbcfg.b.ic_usb_cap = 1;
23169 + DWC_MODIFY_REG32(&core_if->core_global_regs->gusbcfg,
23170 + 0, gusbcfg.d32);
23171 + }
23172 + {
23173 + gotgctl_data_t gotgctl = {.d32 = 0 };
23174 + gotgctl.b.otgver = core_if->core_params->otg_ver;
23175 + DWC_MODIFY_REG32(&core_if->core_global_regs->gotgctl, 0,
23176 + gotgctl.d32);
23177 + /* Set OTG version supported */
23178 + core_if->otg_ver = core_if->core_params->otg_ver;
23179 + DWC_PRINTF("OTG VER PARAM: %d, OTG VER FLAG: %d\n",
23180 + core_if->core_params->otg_ver, core_if->otg_ver);
23181 + }
23182 +
23183 +
23184 + /* Enable common interrupts */
23185 + dwc_otg_enable_common_interrupts(core_if);
23186 +
23187 + /* Do device or host intialization based on mode during PCD
23188 + * and HCD initialization */
23189 + if (dwc_otg_is_host_mode(core_if)) {
23190 + DWC_DEBUGPL(DBG_ANY, "Host Mode\n");
23191 + core_if->op_state = A_HOST;
23192 + } else {
23193 + DWC_DEBUGPL(DBG_ANY, "Device Mode\n");
23194 + core_if->op_state = B_PERIPHERAL;
23195 +#ifdef DWC_DEVICE_ONLY
23196 + dwc_otg_core_dev_init(core_if);
23197 +#endif
23198 + }
23199 +}
23200 +
23201 +/**
23202 + * This function enables the Device mode interrupts.
23203 + *
23204 + * @param core_if Programming view of DWC_otg controller
23205 + */
23206 +void dwc_otg_enable_device_interrupts(dwc_otg_core_if_t * core_if)
23207 +{
23208 + gintmsk_data_t intr_mask = {.d32 = 0 };
23209 + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
23210 +
23211 + DWC_DEBUGPL(DBG_CIL, "%s()\n", __func__);
23212 +
23213 + /* Disable all interrupts. */
23214 + DWC_WRITE_REG32(&global_regs->gintmsk, 0);
23215 +
23216 + /* Clear any pending interrupts */
23217 + DWC_WRITE_REG32(&global_regs->gintsts, 0xFFFFFFFF);
23218 +
23219 + /* Enable the common interrupts */
23220 + dwc_otg_enable_common_interrupts(core_if);
23221 +
23222 + /* Enable interrupts */
23223 + intr_mask.b.usbreset = 1;
23224 + intr_mask.b.enumdone = 1;
23225 + /* Disable Disconnect interrupt in Device mode */
23226 + intr_mask.b.disconnect = 0;
23227 +
23228 + if (!core_if->multiproc_int_enable) {
23229 + intr_mask.b.inepintr = 1;
23230 + intr_mask.b.outepintr = 1;
23231 + }
23232 +
23233 + intr_mask.b.erlysuspend = 1;
23234 +
23235 + if (core_if->en_multiple_tx_fifo == 0) {
23236 + intr_mask.b.epmismatch = 1;
23237 + }
23238 +
23239 + //intr_mask.b.incomplisoout = 1;
23240 + intr_mask.b.incomplisoin = 1;
23241 +
23242 +/* Enable the ignore frame number for ISOC xfers - MAS */
23243 +/* Disable to support high bandwith ISOC transfers - manukz */
23244 +#if 0
23245 +#ifdef DWC_UTE_PER_IO
23246 + if (core_if->dma_enable) {
23247 + if (core_if->dma_desc_enable) {
23248 + dctl_data_t dctl1 = {.d32 = 0 };
23249 + dctl1.b.ifrmnum = 1;
23250 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
23251 + dctl, 0, dctl1.d32);
23252 + DWC_DEBUG("----Enabled Ignore frame number (0x%08x)",
23253 + DWC_READ_REG32(&core_if->dev_if->
23254 + dev_global_regs->dctl));
23255 + }
23256 + }
23257 +#endif
23258 +#endif
23259 +#ifdef DWC_EN_ISOC
23260 + if (core_if->dma_enable) {
23261 + if (core_if->dma_desc_enable == 0) {
23262 + if (core_if->pti_enh_enable) {
23263 + dctl_data_t dctl = {.d32 = 0 };
23264 + dctl.b.ifrmnum = 1;
23265 + DWC_MODIFY_REG32(&core_if->
23266 + dev_if->dev_global_regs->dctl,
23267 + 0, dctl.d32);
23268 + } else {
23269 + intr_mask.b.incomplisoin = 1;
23270 + intr_mask.b.incomplisoout = 1;
23271 + }
23272 + }
23273 + } else {
23274 + intr_mask.b.incomplisoin = 1;
23275 + intr_mask.b.incomplisoout = 1;
23276 + }
23277 +#endif /* DWC_EN_ISOC */
23278 +
23279 + /** @todo NGS: Should this be a module parameter? */
23280 +#ifdef USE_PERIODIC_EP
23281 + intr_mask.b.isooutdrop = 1;
23282 + intr_mask.b.eopframe = 1;
23283 + intr_mask.b.incomplisoin = 1;
23284 + intr_mask.b.incomplisoout = 1;
23285 +#endif
23286 +
23287 + DWC_MODIFY_REG32(&global_regs->gintmsk, intr_mask.d32, intr_mask.d32);
23288 +
23289 + DWC_DEBUGPL(DBG_CIL, "%s() gintmsk=%0x\n", __func__,
23290 + DWC_READ_REG32(&global_regs->gintmsk));
23291 +}
23292 +
23293 +/**
23294 + * This function initializes the DWC_otg controller registers for
23295 + * device mode.
23296 + *
23297 + * @param core_if Programming view of DWC_otg controller
23298 + *
23299 + */
23300 +void dwc_otg_core_dev_init(dwc_otg_core_if_t * core_if)
23301 +{
23302 + int i;
23303 + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
23304 + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
23305 + dwc_otg_core_params_t *params = core_if->core_params;
23306 + dcfg_data_t dcfg = {.d32 = 0 };
23307 + depctl_data_t diepctl = {.d32 = 0 };
23308 + grstctl_t resetctl = {.d32 = 0 };
23309 + uint32_t rx_fifo_size;
23310 + fifosize_data_t nptxfifosize;
23311 + fifosize_data_t txfifosize;
23312 + dthrctl_data_t dthrctl;
23313 + fifosize_data_t ptxfifosize;
23314 + uint16_t rxfsiz, nptxfsiz;
23315 + gdfifocfg_data_t gdfifocfg = {.d32 = 0 };
23316 + hwcfg3_data_t hwcfg3 = {.d32 = 0 };
23317 +
23318 + /* Restart the Phy Clock */
23319 + DWC_WRITE_REG32(core_if->pcgcctl, 0);
23320 +
23321 + /* Device configuration register */
23322 + init_devspd(core_if);
23323 + dcfg.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dcfg);
23324 + dcfg.b.descdma = (core_if->dma_desc_enable) ? 1 : 0;
23325 + dcfg.b.perfrint = DWC_DCFG_FRAME_INTERVAL_80;
23326 + /* Enable Device OUT NAK in case of DDMA mode*/
23327 + if (core_if->core_params->dev_out_nak) {
23328 + dcfg.b.endevoutnak = 1;
23329 + }
23330 +
23331 + if (core_if->core_params->cont_on_bna) {
23332 + dctl_data_t dctl = {.d32 = 0 };
23333 + dctl.b.encontonbna = 1;
23334 + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, 0, dctl.d32);
23335 + }
23336 +
23337 +
23338 + DWC_WRITE_REG32(&dev_if->dev_global_regs->dcfg, dcfg.d32);
23339 +
23340 + /* Configure data FIFO sizes */
23341 + if (core_if->hwcfg2.b.dynamic_fifo && params->enable_dynamic_fifo) {
23342 + DWC_DEBUGPL(DBG_CIL, "Total FIFO Size=%d\n",
23343 + core_if->total_fifo_size);
23344 + DWC_DEBUGPL(DBG_CIL, "Rx FIFO Size=%d\n",
23345 + params->dev_rx_fifo_size);
23346 + DWC_DEBUGPL(DBG_CIL, "NP Tx FIFO Size=%d\n",
23347 + params->dev_nperio_tx_fifo_size);
23348 +
23349 + /* Rx FIFO */
23350 + DWC_DEBUGPL(DBG_CIL, "initial grxfsiz=%08x\n",
23351 + DWC_READ_REG32(&global_regs->grxfsiz));
23352 +
23353 +#ifdef DWC_UTE_CFI
23354 + core_if->pwron_rxfsiz = DWC_READ_REG32(&global_regs->grxfsiz);
23355 + core_if->init_rxfsiz = params->dev_rx_fifo_size;
23356 +#endif
23357 + rx_fifo_size = params->dev_rx_fifo_size;
23358 + DWC_WRITE_REG32(&global_regs->grxfsiz, rx_fifo_size);
23359 +
23360 + DWC_DEBUGPL(DBG_CIL, "new grxfsiz=%08x\n",
23361 + DWC_READ_REG32(&global_regs->grxfsiz));
23362 +
23363 + /** Set Periodic Tx FIFO Mask all bits 0 */
23364 + core_if->p_tx_msk = 0;
23365 +
23366 + /** Set Tx FIFO Mask all bits 0 */
23367 + core_if->tx_msk = 0;
23368 +
23369 + if (core_if->en_multiple_tx_fifo == 0) {
23370 + /* Non-periodic Tx FIFO */
23371 + DWC_DEBUGPL(DBG_CIL, "initial gnptxfsiz=%08x\n",
23372 + DWC_READ_REG32(&global_regs->gnptxfsiz));
23373 +
23374 + nptxfifosize.b.depth = params->dev_nperio_tx_fifo_size;
23375 + nptxfifosize.b.startaddr = params->dev_rx_fifo_size;
23376 +
23377 + DWC_WRITE_REG32(&global_regs->gnptxfsiz,
23378 + nptxfifosize.d32);
23379 +
23380 + DWC_DEBUGPL(DBG_CIL, "new gnptxfsiz=%08x\n",
23381 + DWC_READ_REG32(&global_regs->gnptxfsiz));
23382 +
23383 + /**@todo NGS: Fix Periodic FIFO Sizing! */
23384 + /*
23385 + * Periodic Tx FIFOs These FIFOs are numbered from 1 to 15.
23386 + * Indexes of the FIFO size module parameters in the
23387 + * dev_perio_tx_fifo_size array and the FIFO size registers in
23388 + * the dptxfsiz array run from 0 to 14.
23389 + */
23390 + /** @todo Finish debug of this */
23391 + ptxfifosize.b.startaddr =
23392 + nptxfifosize.b.startaddr + nptxfifosize.b.depth;
23393 + for (i = 0; i < core_if->hwcfg4.b.num_dev_perio_in_ep; i++) {
23394 + ptxfifosize.b.depth =
23395 + params->dev_perio_tx_fifo_size[i];
23396 + DWC_DEBUGPL(DBG_CIL,
23397 + "initial dtxfsiz[%d]=%08x\n", i,
23398 + DWC_READ_REG32(&global_regs->dtxfsiz
23399 + [i]));
23400 + DWC_WRITE_REG32(&global_regs->dtxfsiz[i],
23401 + ptxfifosize.d32);
23402 + DWC_DEBUGPL(DBG_CIL, "new dtxfsiz[%d]=%08x\n",
23403 + i,
23404 + DWC_READ_REG32(&global_regs->dtxfsiz
23405 + [i]));
23406 + ptxfifosize.b.startaddr += ptxfifosize.b.depth;
23407 + }
23408 + } else {
23409 + /*
23410 + * Tx FIFOs These FIFOs are numbered from 1 to 15.
23411 + * Indexes of the FIFO size module parameters in the
23412 + * dev_tx_fifo_size array and the FIFO size registers in
23413 + * the dtxfsiz array run from 0 to 14.
23414 + */
23415 +
23416 + /* Non-periodic Tx FIFO */
23417 + DWC_DEBUGPL(DBG_CIL, "initial gnptxfsiz=%08x\n",
23418 + DWC_READ_REG32(&global_regs->gnptxfsiz));
23419 +
23420 +#ifdef DWC_UTE_CFI
23421 + core_if->pwron_gnptxfsiz =
23422 + (DWC_READ_REG32(&global_regs->gnptxfsiz) >> 16);
23423 + core_if->init_gnptxfsiz =
23424 + params->dev_nperio_tx_fifo_size;
23425 +#endif
23426 + nptxfifosize.b.depth = params->dev_nperio_tx_fifo_size;
23427 + nptxfifosize.b.startaddr = params->dev_rx_fifo_size;
23428 +
23429 + DWC_WRITE_REG32(&global_regs->gnptxfsiz,
23430 + nptxfifosize.d32);
23431 +
23432 + DWC_DEBUGPL(DBG_CIL, "new gnptxfsiz=%08x\n",
23433 + DWC_READ_REG32(&global_regs->gnptxfsiz));
23434 +
23435 + txfifosize.b.startaddr =
23436 + nptxfifosize.b.startaddr + nptxfifosize.b.depth;
23437 +
23438 + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
23439 +
23440 + txfifosize.b.depth =
23441 + params->dev_tx_fifo_size[i];
23442 +
23443 + DWC_DEBUGPL(DBG_CIL,
23444 + "initial dtxfsiz[%d]=%08x\n",
23445 + i,
23446 + DWC_READ_REG32(&global_regs->dtxfsiz
23447 + [i]));
23448 +
23449 +#ifdef DWC_UTE_CFI
23450 + core_if->pwron_txfsiz[i] =
23451 + (DWC_READ_REG32
23452 + (&global_regs->dtxfsiz[i]) >> 16);
23453 + core_if->init_txfsiz[i] =
23454 + params->dev_tx_fifo_size[i];
23455 +#endif
23456 + DWC_WRITE_REG32(&global_regs->dtxfsiz[i],
23457 + txfifosize.d32);
23458 +
23459 + DWC_DEBUGPL(DBG_CIL,
23460 + "new dtxfsiz[%d]=%08x\n",
23461 + i,
23462 + DWC_READ_REG32(&global_regs->dtxfsiz
23463 + [i]));
23464 +
23465 + txfifosize.b.startaddr += txfifosize.b.depth;
23466 + }
23467 + if (core_if->snpsid <= OTG_CORE_REV_2_94a) {
23468 + /* Calculating DFIFOCFG for Device mode to include RxFIFO and NPTXFIFO */
23469 + gdfifocfg.d32 = DWC_READ_REG32(&global_regs->gdfifocfg);
23470 + hwcfg3.d32 = DWC_READ_REG32(&global_regs->ghwcfg3);
23471 + gdfifocfg.b.gdfifocfg = (DWC_READ_REG32(&global_regs->ghwcfg3) >> 16);
23472 + DWC_WRITE_REG32(&global_regs->gdfifocfg, gdfifocfg.d32);
23473 + rxfsiz = (DWC_READ_REG32(&global_regs->grxfsiz) & 0x0000ffff);
23474 + nptxfsiz = (DWC_READ_REG32(&global_regs->gnptxfsiz) >> 16);
23475 + gdfifocfg.b.epinfobase = rxfsiz + nptxfsiz;
23476 + DWC_WRITE_REG32(&global_regs->gdfifocfg, gdfifocfg.d32);
23477 + }
23478 + }
23479 +
23480 + /* Flush the FIFOs */
23481 + dwc_otg_flush_tx_fifo(core_if, 0x10); /* all Tx FIFOs */
23482 + dwc_otg_flush_rx_fifo(core_if);
23483 +
23484 + /* Flush the Learning Queue. */
23485 + resetctl.b.intknqflsh = 1;
23486 + DWC_WRITE_REG32(&core_if->core_global_regs->grstctl, resetctl.d32);
23487 +
23488 + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable) {
23489 + core_if->start_predict = 0;
23490 + for (i = 0; i<= core_if->dev_if->num_in_eps; ++i) {
23491 + core_if->nextep_seq[i] = 0xff; // 0xff - EP not active
23492 + }
23493 + core_if->nextep_seq[0] = 0;
23494 + core_if->first_in_nextep_seq = 0;
23495 + diepctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[0]->diepctl);
23496 + diepctl.b.nextep = 0;
23497 + DWC_WRITE_REG32(&dev_if->in_ep_regs[0]->diepctl, diepctl.d32);
23498 +
23499 + /* Update IN Endpoint Mismatch Count by active IN NP EP count + 1 */
23500 + dcfg.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dcfg);
23501 + dcfg.b.epmscnt = 2;
23502 + DWC_WRITE_REG32(&dev_if->dev_global_regs->dcfg, dcfg.d32);
23503 +
23504 + DWC_DEBUGPL(DBG_CILV,"%s first_in_nextep_seq= %2d; nextep_seq[]:\n",
23505 + __func__, core_if->first_in_nextep_seq);
23506 + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
23507 + DWC_DEBUGPL(DBG_CILV, "%2d ", core_if->nextep_seq[i]);
23508 + }
23509 + DWC_DEBUGPL(DBG_CILV,"\n");
23510 + }
23511 +
23512 + /* Clear all pending Device Interrupts */
23513 + /** @todo - if the condition needed to be checked
23514 + * or in any case all pending interrutps should be cleared?
23515 + */
23516 + if (core_if->multiproc_int_enable) {
23517 + for (i = 0; i < core_if->dev_if->num_in_eps; ++i) {
23518 + DWC_WRITE_REG32(&dev_if->
23519 + dev_global_regs->diepeachintmsk[i], 0);
23520 + }
23521 + }
23522 +
23523 + for (i = 0; i < core_if->dev_if->num_out_eps; ++i) {
23524 + DWC_WRITE_REG32(&dev_if->
23525 + dev_global_regs->doepeachintmsk[i], 0);
23526 + }
23527 +
23528 + DWC_WRITE_REG32(&dev_if->dev_global_regs->deachint, 0xFFFFFFFF);
23529 + DWC_WRITE_REG32(&dev_if->dev_global_regs->deachintmsk, 0);
23530 + } else {
23531 + DWC_WRITE_REG32(&dev_if->dev_global_regs->diepmsk, 0);
23532 + DWC_WRITE_REG32(&dev_if->dev_global_regs->doepmsk, 0);
23533 + DWC_WRITE_REG32(&dev_if->dev_global_regs->daint, 0xFFFFFFFF);
23534 + DWC_WRITE_REG32(&dev_if->dev_global_regs->daintmsk, 0);
23535 + }
23536 +
23537 + for (i = 0; i <= dev_if->num_in_eps; i++) {
23538 + depctl_data_t depctl;
23539 + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
23540 + if (depctl.b.epena) {
23541 + depctl.d32 = 0;
23542 + depctl.b.epdis = 1;
23543 + depctl.b.snak = 1;
23544 + } else {
23545 + depctl.d32 = 0;
23546 + }
23547 +
23548 + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepctl, depctl.d32);
23549 +
23550 + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->dieptsiz, 0);
23551 + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepdma, 0);
23552 + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepint, 0xFF);
23553 + }
23554 +
23555 + for (i = 0; i <= dev_if->num_out_eps; i++) {
23556 + depctl_data_t depctl;
23557 + depctl.d32 = DWC_READ_REG32(&dev_if->out_ep_regs[i]->doepctl);
23558 + if (depctl.b.epena) {
23559 + dctl_data_t dctl = {.d32 = 0 };
23560 + gintmsk_data_t gintsts = {.d32 = 0 };
23561 + doepint_data_t doepint = {.d32 = 0 };
23562 + dctl.b.sgoutnak = 1;
23563 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
23564 + do {
23565 + dwc_udelay(10);
23566 + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
23567 + } while (!gintsts.b.goutnakeff);
23568 + gintsts.d32 = 0;
23569 + gintsts.b.goutnakeff = 1;
23570 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
23571 +
23572 + depctl.d32 = 0;
23573 + depctl.b.epdis = 1;
23574 + depctl.b.snak = 1;
23575 + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[i]->doepctl, depctl.d32);
23576 + do {
23577 + dwc_udelay(10);
23578 + doepint.d32 = DWC_READ_REG32(&core_if->dev_if->
23579 + out_ep_regs[i]->doepint);
23580 + } while (!doepint.b.epdisabled);
23581 +
23582 + doepint.b.epdisabled = 1;
23583 + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[i]->doepint, doepint.d32);
23584 +
23585 + dctl.d32 = 0;
23586 + dctl.b.cgoutnak = 1;
23587 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
23588 + } else {
23589 + depctl.d32 = 0;
23590 + }
23591 +
23592 + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doepctl, depctl.d32);
23593 +
23594 + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doeptsiz, 0);
23595 + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doepdma, 0);
23596 + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doepint, 0xFF);
23597 + }
23598 +
23599 + if (core_if->en_multiple_tx_fifo && core_if->dma_enable) {
23600 + dev_if->non_iso_tx_thr_en = params->thr_ctl & 0x1;
23601 + dev_if->iso_tx_thr_en = (params->thr_ctl >> 1) & 0x1;
23602 + dev_if->rx_thr_en = (params->thr_ctl >> 2) & 0x1;
23603 +
23604 + dev_if->rx_thr_length = params->rx_thr_length;
23605 + dev_if->tx_thr_length = params->tx_thr_length;
23606 +
23607 + dev_if->setup_desc_index = 0;
23608 +
23609 + dthrctl.d32 = 0;
23610 + dthrctl.b.non_iso_thr_en = dev_if->non_iso_tx_thr_en;
23611 + dthrctl.b.iso_thr_en = dev_if->iso_tx_thr_en;
23612 + dthrctl.b.tx_thr_len = dev_if->tx_thr_length;
23613 + dthrctl.b.rx_thr_en = dev_if->rx_thr_en;
23614 + dthrctl.b.rx_thr_len = dev_if->rx_thr_length;
23615 + dthrctl.b.ahb_thr_ratio = params->ahb_thr_ratio;
23616 +
23617 + DWC_WRITE_REG32(&dev_if->dev_global_regs->dtknqr3_dthrctl,
23618 + dthrctl.d32);
23619 +
23620 + DWC_DEBUGPL(DBG_CIL,
23621 + "Non ISO Tx Thr - %d\nISO Tx Thr - %d\nRx Thr - %d\nTx Thr Len - %d\nRx Thr Len - %d\n",
23622 + dthrctl.b.non_iso_thr_en, dthrctl.b.iso_thr_en,
23623 + dthrctl.b.rx_thr_en, dthrctl.b.tx_thr_len,
23624 + dthrctl.b.rx_thr_len);
23625 +
23626 + }
23627 +
23628 + dwc_otg_enable_device_interrupts(core_if);
23629 +
23630 + {
23631 + diepmsk_data_t msk = {.d32 = 0 };
23632 + msk.b.txfifoundrn = 1;
23633 + if (core_if->multiproc_int_enable) {
23634 + DWC_MODIFY_REG32(&dev_if->dev_global_regs->
23635 + diepeachintmsk[0], msk.d32, msk.d32);
23636 + } else {
23637 + DWC_MODIFY_REG32(&dev_if->dev_global_regs->diepmsk,
23638 + msk.d32, msk.d32);
23639 + }
23640 + }
23641 +
23642 + if (core_if->multiproc_int_enable) {
23643 + /* Set NAK on Babble */
23644 + dctl_data_t dctl = {.d32 = 0 };
23645 + dctl.b.nakonbble = 1;
23646 + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, 0, dctl.d32);
23647 + }
23648 +
23649 + if (core_if->snpsid >= OTG_CORE_REV_2_94a) {
23650 + dctl_data_t dctl = {.d32 = 0 };
23651 + dctl.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dctl);
23652 + dctl.b.sftdiscon = 0;
23653 + DWC_WRITE_REG32(&dev_if->dev_global_regs->dctl, dctl.d32);
23654 + }
23655 +}
23656 +
23657 +/**
23658 + * This function enables the Host mode interrupts.
23659 + *
23660 + * @param core_if Programming view of DWC_otg controller
23661 + */
23662 +void dwc_otg_enable_host_interrupts(dwc_otg_core_if_t * core_if)
23663 +{
23664 + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
23665 + gintmsk_data_t intr_mask = {.d32 = 0 };
23666 +
23667 + DWC_DEBUGPL(DBG_CIL, "%s(%p)\n", __func__, core_if);
23668 +
23669 + /* Disable all interrupts. */
23670 + DWC_WRITE_REG32(&global_regs->gintmsk, 0);
23671 +
23672 + /* Clear any pending interrupts. */
23673 + DWC_WRITE_REG32(&global_regs->gintsts, 0xFFFFFFFF);
23674 +
23675 + /* Enable the common interrupts */
23676 + dwc_otg_enable_common_interrupts(core_if);
23677 +
23678 + /*
23679 + * Enable host mode interrupts without disturbing common
23680 + * interrupts.
23681 + */
23682 +
23683 + intr_mask.b.disconnect = 1;
23684 + intr_mask.b.portintr = 1;
23685 + intr_mask.b.hcintr = 1;
23686 +
23687 + DWC_MODIFY_REG32(&global_regs->gintmsk, intr_mask.d32, intr_mask.d32);
23688 +}
23689 +
23690 +/**
23691 + * This function disables the Host Mode interrupts.
23692 + *
23693 + * @param core_if Programming view of DWC_otg controller
23694 + */
23695 +void dwc_otg_disable_host_interrupts(dwc_otg_core_if_t * core_if)
23696 +{
23697 + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
23698 + gintmsk_data_t intr_mask = {.d32 = 0 };
23699 +
23700 + DWC_DEBUGPL(DBG_CILV, "%s()\n", __func__);
23701 +
23702 + /*
23703 + * Disable host mode interrupts without disturbing common
23704 + * interrupts.
23705 + */
23706 + intr_mask.b.sofintr = 1;
23707 + intr_mask.b.portintr = 1;
23708 + intr_mask.b.hcintr = 1;
23709 + intr_mask.b.ptxfempty = 1;
23710 + intr_mask.b.nptxfempty = 1;
23711 +
23712 + DWC_MODIFY_REG32(&global_regs->gintmsk, intr_mask.d32, 0);
23713 +}
23714 +
23715 +/**
23716 + * This function initializes the DWC_otg controller registers for
23717 + * host mode.
23718 + *
23719 + * This function flushes the Tx and Rx FIFOs and it flushes any entries in the
23720 + * request queues. Host channels are reset to ensure that they are ready for
23721 + * performing transfers.
23722 + *
23723 + * @param core_if Programming view of DWC_otg controller
23724 + *
23725 + */
23726 +void dwc_otg_core_host_init(dwc_otg_core_if_t * core_if)
23727 +{
23728 + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
23729 + dwc_otg_host_if_t *host_if = core_if->host_if;
23730 + dwc_otg_core_params_t *params = core_if->core_params;
23731 + hprt0_data_t hprt0 = {.d32 = 0 };
23732 + fifosize_data_t nptxfifosize;
23733 + fifosize_data_t ptxfifosize;
23734 + uint16_t rxfsiz, nptxfsiz, hptxfsiz;
23735 + gdfifocfg_data_t gdfifocfg = {.d32 = 0 };
23736 + int i;
23737 + hcchar_data_t hcchar;
23738 + hcfg_data_t hcfg;
23739 + hfir_data_t hfir;
23740 + dwc_otg_hc_regs_t *hc_regs;
23741 + int num_channels;
23742 + gotgctl_data_t gotgctl = {.d32 = 0 };
23743 +
23744 + DWC_DEBUGPL(DBG_CILV, "%s(%p)\n", __func__, core_if);
23745 +
23746 + /* Restart the Phy Clock */
23747 + DWC_WRITE_REG32(core_if->pcgcctl, 0);
23748 +
23749 + /* Initialize Host Configuration Register */
23750 + init_fslspclksel(core_if);
23751 + if (core_if->core_params->speed == DWC_SPEED_PARAM_FULL) {
23752 + hcfg.d32 = DWC_READ_REG32(&host_if->host_global_regs->hcfg);
23753 + hcfg.b.fslssupp = 1;
23754 + DWC_WRITE_REG32(&host_if->host_global_regs->hcfg, hcfg.d32);
23755 +
23756 + }
23757 +
23758 + /* This bit allows dynamic reloading of the HFIR register
23759 + * during runtime. This bit needs to be programmed during
23760 + * initial configuration and its value must not be changed
23761 + * during runtime.*/
23762 + if (core_if->core_params->reload_ctl == 1) {
23763 + hfir.d32 = DWC_READ_REG32(&host_if->host_global_regs->hfir);
23764 + hfir.b.hfirrldctrl = 1;
23765 + DWC_WRITE_REG32(&host_if->host_global_regs->hfir, hfir.d32);
23766 + }
23767 +
23768 + if (core_if->core_params->dma_desc_enable) {
23769 + uint8_t op_mode = core_if->hwcfg2.b.op_mode;
23770 + if (!
23771 + (core_if->hwcfg4.b.desc_dma
23772 + && (core_if->snpsid >= OTG_CORE_REV_2_90a)
23773 + && ((op_mode == DWC_HWCFG2_OP_MODE_HNP_SRP_CAPABLE_OTG)
23774 + || (op_mode == DWC_HWCFG2_OP_MODE_SRP_ONLY_CAPABLE_OTG)
23775 + || (op_mode ==
23776 + DWC_HWCFG2_OP_MODE_NO_HNP_SRP_CAPABLE_OTG)
23777 + || (op_mode == DWC_HWCFG2_OP_MODE_SRP_CAPABLE_HOST)
23778 + || (op_mode ==
23779 + DWC_HWCFG2_OP_MODE_NO_SRP_CAPABLE_HOST)))) {
23780 +
23781 + DWC_ERROR("Host can't operate in Descriptor DMA mode.\n"
23782 + "Either core version is below 2.90a or "
23783 + "GHWCFG2, GHWCFG4 registers' values do not allow Descriptor DMA in host mode.\n"
23784 + "To run the driver in Buffer DMA host mode set dma_desc_enable "
23785 + "module parameter to 0.\n");
23786 + return;
23787 + }
23788 + hcfg.d32 = DWC_READ_REG32(&host_if->host_global_regs->hcfg);
23789 + hcfg.b.descdma = 1;
23790 + DWC_WRITE_REG32(&host_if->host_global_regs->hcfg, hcfg.d32);
23791 + }
23792 +
23793 + /* Configure data FIFO sizes */
23794 + if (core_if->hwcfg2.b.dynamic_fifo && params->enable_dynamic_fifo) {
23795 + DWC_DEBUGPL(DBG_CIL, "Total FIFO Size=%d\n",
23796 + core_if->total_fifo_size);
23797 + DWC_DEBUGPL(DBG_CIL, "Rx FIFO Size=%d\n",
23798 + params->host_rx_fifo_size);
23799 + DWC_DEBUGPL(DBG_CIL, "NP Tx FIFO Size=%d\n",
23800 + params->host_nperio_tx_fifo_size);
23801 + DWC_DEBUGPL(DBG_CIL, "P Tx FIFO Size=%d\n",
23802 + params->host_perio_tx_fifo_size);
23803 +
23804 + /* Rx FIFO */
23805 + DWC_DEBUGPL(DBG_CIL, "initial grxfsiz=%08x\n",
23806 + DWC_READ_REG32(&global_regs->grxfsiz));
23807 + DWC_WRITE_REG32(&global_regs->grxfsiz,
23808 + params->host_rx_fifo_size);
23809 + DWC_DEBUGPL(DBG_CIL, "new grxfsiz=%08x\n",
23810 + DWC_READ_REG32(&global_regs->grxfsiz));
23811 +
23812 + /* Non-periodic Tx FIFO */
23813 + DWC_DEBUGPL(DBG_CIL, "initial gnptxfsiz=%08x\n",
23814 + DWC_READ_REG32(&global_regs->gnptxfsiz));
23815 + nptxfifosize.b.depth = params->host_nperio_tx_fifo_size;
23816 + nptxfifosize.b.startaddr = params->host_rx_fifo_size;
23817 + DWC_WRITE_REG32(&global_regs->gnptxfsiz, nptxfifosize.d32);
23818 + DWC_DEBUGPL(DBG_CIL, "new gnptxfsiz=%08x\n",
23819 + DWC_READ_REG32(&global_regs->gnptxfsiz));
23820 +
23821 + /* Periodic Tx FIFO */
23822 + DWC_DEBUGPL(DBG_CIL, "initial hptxfsiz=%08x\n",
23823 + DWC_READ_REG32(&global_regs->hptxfsiz));
23824 + ptxfifosize.b.depth = params->host_perio_tx_fifo_size;
23825 + ptxfifosize.b.startaddr =
23826 + nptxfifosize.b.startaddr + nptxfifosize.b.depth;
23827 + DWC_WRITE_REG32(&global_regs->hptxfsiz, ptxfifosize.d32);
23828 + DWC_DEBUGPL(DBG_CIL, "new hptxfsiz=%08x\n",
23829 + DWC_READ_REG32(&global_regs->hptxfsiz));
23830 +
23831 + if (core_if->en_multiple_tx_fifo
23832 + && core_if->snpsid <= OTG_CORE_REV_2_94a) {
23833 + /* Global DFIFOCFG calculation for Host mode - include RxFIFO, NPTXFIFO and HPTXFIFO */
23834 + gdfifocfg.d32 = DWC_READ_REG32(&global_regs->gdfifocfg);
23835 + rxfsiz = (DWC_READ_REG32(&global_regs->grxfsiz) & 0x0000ffff);
23836 + nptxfsiz = (DWC_READ_REG32(&global_regs->gnptxfsiz) >> 16);
23837 + hptxfsiz = (DWC_READ_REG32(&global_regs->hptxfsiz) >> 16);
23838 + gdfifocfg.b.epinfobase = rxfsiz + nptxfsiz + hptxfsiz;
23839 + DWC_WRITE_REG32(&global_regs->gdfifocfg, gdfifocfg.d32);
23840 + }
23841 + }
23842 +
23843 + /* TODO - check this */
23844 + /* Clear Host Set HNP Enable in the OTG Control Register */
23845 + gotgctl.b.hstsethnpen = 1;
23846 + DWC_MODIFY_REG32(&global_regs->gotgctl, gotgctl.d32, 0);
23847 + /* Make sure the FIFOs are flushed. */
23848 + dwc_otg_flush_tx_fifo(core_if, 0x10 /* all TX FIFOs */ );
23849 + dwc_otg_flush_rx_fifo(core_if);
23850 +
23851 + /* Clear Host Set HNP Enable in the OTG Control Register */
23852 + gotgctl.b.hstsethnpen = 1;
23853 + DWC_MODIFY_REG32(&global_regs->gotgctl, gotgctl.d32, 0);
23854 +
23855 + if (!core_if->core_params->dma_desc_enable) {
23856 + /* Flush out any leftover queued requests. */
23857 + num_channels = core_if->core_params->host_channels;
23858 +
23859 + for (i = 0; i < num_channels; i++) {
23860 + hc_regs = core_if->host_if->hc_regs[i];
23861 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
23862 + hcchar.b.chen = 0;
23863 + hcchar.b.chdis = 1;
23864 + hcchar.b.epdir = 0;
23865 + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
23866 + }
23867 +
23868 + /* Halt all channels to put them into a known state. */
23869 + for (i = 0; i < num_channels; i++) {
23870 + int count = 0;
23871 + hc_regs = core_if->host_if->hc_regs[i];
23872 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
23873 + hcchar.b.chen = 1;
23874 + hcchar.b.chdis = 1;
23875 + hcchar.b.epdir = 0;
23876 + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
23877 + DWC_DEBUGPL(DBG_HCDV, "%s: Halt channel %d regs %p\n", __func__, i, hc_regs);
23878 + do {
23879 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
23880 + if (++count > 1000) {
23881 + DWC_ERROR
23882 + ("%s: Unable to clear halt on channel %d (timeout HCCHAR 0x%X @%p)\n",
23883 + __func__, i, hcchar.d32, &hc_regs->hcchar);
23884 + break;
23885 + }
23886 + dwc_udelay(1);
23887 + } while (hcchar.b.chen);
23888 + }
23889 + }
23890 +
23891 + /* Turn on the vbus power. */
23892 + DWC_PRINTF("Init: Port Power? op_state=%d\n", core_if->op_state);
23893 + if (core_if->op_state == A_HOST) {
23894 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
23895 + DWC_PRINTF("Init: Power Port (%d)\n", hprt0.b.prtpwr);
23896 + if (hprt0.b.prtpwr == 0) {
23897 + hprt0.b.prtpwr = 1;
23898 + DWC_WRITE_REG32(host_if->hprt0, hprt0.d32);
23899 + }
23900 + }
23901 +
23902 + dwc_otg_enable_host_interrupts(core_if);
23903 +}
23904 +
23905 +/**
23906 + * Prepares a host channel for transferring packets to/from a specific
23907 + * endpoint. The HCCHARn register is set up with the characteristics specified
23908 + * in _hc. Host channel interrupts that may need to be serviced while this
23909 + * transfer is in progress are enabled.
23910 + *
23911 + * @param core_if Programming view of DWC_otg controller
23912 + * @param hc Information needed to initialize the host channel
23913 + */
23914 +void dwc_otg_hc_init(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
23915 +{
23916 + hcintmsk_data_t hc_intr_mask;
23917 + hcchar_data_t hcchar;
23918 + hcsplt_data_t hcsplt;
23919 +
23920 + uint8_t hc_num = hc->hc_num;
23921 + dwc_otg_host_if_t *host_if = core_if->host_if;
23922 + dwc_otg_hc_regs_t *hc_regs = host_if->hc_regs[hc_num];
23923 +
23924 + /* Clear old interrupt conditions for this host channel. */
23925 + hc_intr_mask.d32 = 0xFFFFFFFF;
23926 + hc_intr_mask.b.reserved14_31 = 0;
23927 + DWC_WRITE_REG32(&hc_regs->hcint, hc_intr_mask.d32);
23928 +
23929 + /* Enable channel interrupts required for this transfer. */
23930 + hc_intr_mask.d32 = 0;
23931 + hc_intr_mask.b.chhltd = 1;
23932 + if (core_if->dma_enable) {
23933 + /* For Descriptor DMA mode core halts the channel on AHB error. Interrupt is not required */
23934 + if (!core_if->dma_desc_enable)
23935 + hc_intr_mask.b.ahberr = 1;
23936 + else {
23937 + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC)
23938 + hc_intr_mask.b.xfercompl = 1;
23939 + }
23940 +
23941 + if (hc->error_state && !hc->do_split &&
23942 + hc->ep_type != DWC_OTG_EP_TYPE_ISOC) {
23943 + hc_intr_mask.b.ack = 1;
23944 + if (hc->ep_is_in) {
23945 + hc_intr_mask.b.datatglerr = 1;
23946 + if (hc->ep_type != DWC_OTG_EP_TYPE_INTR) {
23947 + hc_intr_mask.b.nak = 1;
23948 + }
23949 + }
23950 + }
23951 + } else {
23952 + switch (hc->ep_type) {
23953 + case DWC_OTG_EP_TYPE_CONTROL:
23954 + case DWC_OTG_EP_TYPE_BULK:
23955 + hc_intr_mask.b.xfercompl = 1;
23956 + hc_intr_mask.b.stall = 1;
23957 + hc_intr_mask.b.xacterr = 1;
23958 + hc_intr_mask.b.datatglerr = 1;
23959 + if (hc->ep_is_in) {
23960 + hc_intr_mask.b.bblerr = 1;
23961 + } else {
23962 + hc_intr_mask.b.nak = 1;
23963 + hc_intr_mask.b.nyet = 1;
23964 + if (hc->do_ping) {
23965 + hc_intr_mask.b.ack = 1;
23966 + }
23967 + }
23968 +
23969 + if (hc->do_split) {
23970 + hc_intr_mask.b.nak = 1;
23971 + if (hc->complete_split) {
23972 + hc_intr_mask.b.nyet = 1;
23973 + } else {
23974 + hc_intr_mask.b.ack = 1;
23975 + }
23976 + }
23977 +
23978 + if (hc->error_state) {
23979 + hc_intr_mask.b.ack = 1;
23980 + }
23981 + break;
23982 + case DWC_OTG_EP_TYPE_INTR:
23983 + hc_intr_mask.b.xfercompl = 1;
23984 + hc_intr_mask.b.nak = 1;
23985 + hc_intr_mask.b.stall = 1;
23986 + hc_intr_mask.b.xacterr = 1;
23987 + hc_intr_mask.b.datatglerr = 1;
23988 + hc_intr_mask.b.frmovrun = 1;
23989 +
23990 + if (hc->ep_is_in) {
23991 + hc_intr_mask.b.bblerr = 1;
23992 + }
23993 + if (hc->error_state) {
23994 + hc_intr_mask.b.ack = 1;
23995 + }
23996 + if (hc->do_split) {
23997 + if (hc->complete_split) {
23998 + hc_intr_mask.b.nyet = 1;
23999 + } else {
24000 + hc_intr_mask.b.ack = 1;
24001 + }
24002 + }
24003 + break;
24004 + case DWC_OTG_EP_TYPE_ISOC:
24005 + hc_intr_mask.b.xfercompl = 1;
24006 + hc_intr_mask.b.frmovrun = 1;
24007 + hc_intr_mask.b.ack = 1;
24008 +
24009 + if (hc->ep_is_in) {
24010 + hc_intr_mask.b.xacterr = 1;
24011 + hc_intr_mask.b.bblerr = 1;
24012 + }
24013 + break;
24014 + }
24015 + }
24016 + DWC_WRITE_REG32(&hc_regs->hcintmsk, hc_intr_mask.d32);
24017 +
24018 + /*
24019 + * Program the HCCHARn register with the endpoint characteristics for
24020 + * the current transfer.
24021 + */
24022 + hcchar.d32 = 0;
24023 + hcchar.b.devaddr = hc->dev_addr;
24024 + hcchar.b.epnum = hc->ep_num;
24025 + hcchar.b.epdir = hc->ep_is_in;
24026 + hcchar.b.lspddev = (hc->speed == DWC_OTG_EP_SPEED_LOW);
24027 + hcchar.b.eptype = hc->ep_type;
24028 + hcchar.b.mps = hc->max_packet;
24029 +
24030 + DWC_WRITE_REG32(&host_if->hc_regs[hc_num]->hcchar, hcchar.d32);
24031 +
24032 + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d, Dev Addr %d, EP #%d\n",
24033 + __func__, hc->hc_num, hcchar.b.devaddr, hcchar.b.epnum);
24034 + DWC_DEBUGPL(DBG_HCDV, " Is In %d, Is Low Speed %d, EP Type %d, "
24035 + "Max Pkt %d, Multi Cnt %d\n",
24036 + hcchar.b.epdir, hcchar.b.lspddev, hcchar.b.eptype,
24037 + hcchar.b.mps, hcchar.b.multicnt);
24038 +
24039 + /*
24040 + * Program the HCSPLIT register for SPLITs
24041 + */
24042 + hcsplt.d32 = 0;
24043 + if (hc->do_split) {
24044 + DWC_DEBUGPL(DBG_HCDV, "Programming HC %d with split --> %s\n",
24045 + hc->hc_num,
24046 + hc->complete_split ? "CSPLIT" : "SSPLIT");
24047 + hcsplt.b.compsplt = hc->complete_split;
24048 + hcsplt.b.xactpos = hc->xact_pos;
24049 + hcsplt.b.hubaddr = hc->hub_addr;
24050 + hcsplt.b.prtaddr = hc->port_addr;
24051 + DWC_DEBUGPL(DBG_HCDV, "\t comp split %d\n", hc->complete_split);
24052 + DWC_DEBUGPL(DBG_HCDV, "\t xact pos %d\n", hc->xact_pos);
24053 + DWC_DEBUGPL(DBG_HCDV, "\t hub addr %d\n", hc->hub_addr);
24054 + DWC_DEBUGPL(DBG_HCDV, "\t port addr %d\n", hc->port_addr);
24055 + DWC_DEBUGPL(DBG_HCDV, "\t is_in %d\n", hc->ep_is_in);
24056 + DWC_DEBUGPL(DBG_HCDV, "\t Max Pkt: %d\n", hcchar.b.mps);
24057 + DWC_DEBUGPL(DBG_HCDV, "\t xferlen: %d\n", hc->xfer_len);
24058 + }
24059 + DWC_WRITE_REG32(&host_if->hc_regs[hc_num]->hcsplt, hcsplt.d32);
24060 +
24061 +}
24062 +
24063 +/**
24064 + * Attempts to halt a host channel. This function should only be called in
24065 + * Slave mode or to abort a transfer in either Slave mode or DMA mode. Under
24066 + * normal circumstances in DMA mode, the controller halts the channel when the
24067 + * transfer is complete or a condition occurs that requires application
24068 + * intervention.
24069 + *
24070 + * In slave mode, checks for a free request queue entry, then sets the Channel
24071 + * Enable and Channel Disable bits of the Host Channel Characteristics
24072 + * register of the specified channel to intiate the halt. If there is no free
24073 + * request queue entry, sets only the Channel Disable bit of the HCCHARn
24074 + * register to flush requests for this channel. In the latter case, sets a
24075 + * flag to indicate that the host channel needs to be halted when a request
24076 + * queue slot is open.
24077 + *
24078 + * In DMA mode, always sets the Channel Enable and Channel Disable bits of the
24079 + * HCCHARn register. The controller ensures there is space in the request
24080 + * queue before submitting the halt request.
24081 + *
24082 + * Some time may elapse before the core flushes any posted requests for this
24083 + * host channel and halts. The Channel Halted interrupt handler completes the
24084 + * deactivation of the host channel.
24085 + *
24086 + * @param core_if Controller register interface.
24087 + * @param hc Host channel to halt.
24088 + * @param halt_status Reason for halting the channel.
24089 + */
24090 +void dwc_otg_hc_halt(dwc_otg_core_if_t * core_if,
24091 + dwc_hc_t * hc, dwc_otg_halt_status_e halt_status)
24092 +{
24093 + gnptxsts_data_t nptxsts;
24094 + hptxsts_data_t hptxsts;
24095 + hcchar_data_t hcchar;
24096 + dwc_otg_hc_regs_t *hc_regs;
24097 + dwc_otg_core_global_regs_t *global_regs;
24098 + dwc_otg_host_global_regs_t *host_global_regs;
24099 +
24100 + hc_regs = core_if->host_if->hc_regs[hc->hc_num];
24101 + global_regs = core_if->core_global_regs;
24102 + host_global_regs = core_if->host_if->host_global_regs;
24103 +
24104 + DWC_ASSERT(!(halt_status == DWC_OTG_HC_XFER_NO_HALT_STATUS),
24105 + "halt_status = %d\n", halt_status);
24106 +
24107 + if (halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE ||
24108 + halt_status == DWC_OTG_HC_XFER_AHB_ERR) {
24109 + /*
24110 + * Disable all channel interrupts except Ch Halted. The QTD
24111 + * and QH state associated with this transfer has been cleared
24112 + * (in the case of URB_DEQUEUE), so the channel needs to be
24113 + * shut down carefully to prevent crashes.
24114 + */
24115 + hcintmsk_data_t hcintmsk;
24116 + hcintmsk.d32 = 0;
24117 + hcintmsk.b.chhltd = 1;
24118 + DWC_WRITE_REG32(&hc_regs->hcintmsk, hcintmsk.d32);
24119 +
24120 + /*
24121 + * Make sure no other interrupts besides halt are currently
24122 + * pending. Handling another interrupt could cause a crash due
24123 + * to the QTD and QH state.
24124 + */
24125 + DWC_WRITE_REG32(&hc_regs->hcint, ~hcintmsk.d32);
24126 +
24127 + /*
24128 + * Make sure the halt status is set to URB_DEQUEUE or AHB_ERR
24129 + * even if the channel was already halted for some other
24130 + * reason.
24131 + */
24132 + hc->halt_status = halt_status;
24133 +
24134 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
24135 + if (hcchar.b.chen == 0) {
24136 + /*
24137 + * The channel is either already halted or it hasn't
24138 + * started yet. In DMA mode, the transfer may halt if
24139 + * it finishes normally or a condition occurs that
24140 + * requires driver intervention. Don't want to halt
24141 + * the channel again. In either Slave or DMA mode,
24142 + * it's possible that the transfer has been assigned
24143 + * to a channel, but not started yet when an URB is
24144 + * dequeued. Don't want to halt a channel that hasn't
24145 + * started yet.
24146 + */
24147 + return;
24148 + }
24149 + }
24150 + if (hc->halt_pending) {
24151 + /*
24152 + * A halt has already been issued for this channel. This might
24153 + * happen when a transfer is aborted by a higher level in
24154 + * the stack.
24155 + */
24156 +#ifdef DEBUG
24157 + DWC_PRINTF
24158 + ("*** %s: Channel %d, _hc->halt_pending already set ***\n",
24159 + __func__, hc->hc_num);
24160 +
24161 +#endif
24162 + return;
24163 + }
24164 +
24165 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
24166 +
24167 + /* No need to set the bit in DDMA for disabling the channel */
24168 + //TODO check it everywhere channel is disabled
24169 + if (!core_if->core_params->dma_desc_enable)
24170 + hcchar.b.chen = 1;
24171 + hcchar.b.chdis = 1;
24172 +
24173 + if (!core_if->dma_enable) {
24174 + /* Check for space in the request queue to issue the halt. */
24175 + if (hc->ep_type == DWC_OTG_EP_TYPE_CONTROL ||
24176 + hc->ep_type == DWC_OTG_EP_TYPE_BULK) {
24177 + nptxsts.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
24178 + if (nptxsts.b.nptxqspcavail == 0) {
24179 + hcchar.b.chen = 0;
24180 + }
24181 + } else {
24182 + hptxsts.d32 =
24183 + DWC_READ_REG32(&host_global_regs->hptxsts);
24184 + if ((hptxsts.b.ptxqspcavail == 0)
24185 + || (core_if->queuing_high_bandwidth)) {
24186 + hcchar.b.chen = 0;
24187 + }
24188 + }
24189 + }
24190 + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
24191 +
24192 + hc->halt_status = halt_status;
24193 +
24194 + if (hcchar.b.chen) {
24195 + hc->halt_pending = 1;
24196 + hc->halt_on_queue = 0;
24197 + } else {
24198 + hc->halt_on_queue = 1;
24199 + }
24200 +
24201 + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d\n", __func__, hc->hc_num);
24202 + DWC_DEBUGPL(DBG_HCDV, " hcchar: 0x%08x\n", hcchar.d32);
24203 + DWC_DEBUGPL(DBG_HCDV, " halt_pending: %d\n", hc->halt_pending);
24204 + DWC_DEBUGPL(DBG_HCDV, " halt_on_queue: %d\n", hc->halt_on_queue);
24205 + DWC_DEBUGPL(DBG_HCDV, " halt_status: %d\n", hc->halt_status);
24206 +
24207 + return;
24208 +}
24209 +
24210 +/**
24211 + * Clears the transfer state for a host channel. This function is normally
24212 + * called after a transfer is done and the host channel is being released.
24213 + *
24214 + * @param core_if Programming view of DWC_otg controller.
24215 + * @param hc Identifies the host channel to clean up.
24216 + */
24217 +void dwc_otg_hc_cleanup(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
24218 +{
24219 + dwc_otg_hc_regs_t *hc_regs;
24220 +
24221 + hc->xfer_started = 0;
24222 +
24223 + /*
24224 + * Clear channel interrupt enables and any unhandled channel interrupt
24225 + * conditions.
24226 + */
24227 + hc_regs = core_if->host_if->hc_regs[hc->hc_num];
24228 + DWC_WRITE_REG32(&hc_regs->hcintmsk, 0);
24229 + DWC_WRITE_REG32(&hc_regs->hcint, 0xFFFFFFFF);
24230 +#ifdef DEBUG
24231 + DWC_TIMER_CANCEL(core_if->hc_xfer_timer[hc->hc_num]);
24232 +#endif
24233 +}
24234 +
24235 +/**
24236 + * Sets the channel property that indicates in which frame a periodic transfer
24237 + * should occur. This is always set to the _next_ frame. This function has no
24238 + * effect on non-periodic transfers.
24239 + *
24240 + * @param core_if Programming view of DWC_otg controller.
24241 + * @param hc Identifies the host channel to set up and its properties.
24242 + * @param hcchar Current value of the HCCHAR register for the specified host
24243 + * channel.
24244 + */
24245 +static inline void hc_set_even_odd_frame(dwc_otg_core_if_t * core_if,
24246 + dwc_hc_t * hc, hcchar_data_t * hcchar)
24247 +{
24248 + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
24249 + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
24250 + hfnum_data_t hfnum;
24251 + hfnum.d32 =
24252 + DWC_READ_REG32(&core_if->host_if->host_global_regs->hfnum);
24253 +
24254 + /* 1 if _next_ frame is odd, 0 if it's even */
24255 + hcchar->b.oddfrm = (hfnum.b.frnum & 0x1) ? 0 : 1;
24256 +#ifdef DEBUG
24257 + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR && hc->do_split
24258 + && !hc->complete_split) {
24259 + switch (hfnum.b.frnum & 0x7) {
24260 + case 7:
24261 + core_if->hfnum_7_samples++;
24262 + core_if->hfnum_7_frrem_accum += hfnum.b.frrem;
24263 + break;
24264 + case 0:
24265 + core_if->hfnum_0_samples++;
24266 + core_if->hfnum_0_frrem_accum += hfnum.b.frrem;
24267 + break;
24268 + default:
24269 + core_if->hfnum_other_samples++;
24270 + core_if->hfnum_other_frrem_accum +=
24271 + hfnum.b.frrem;
24272 + break;
24273 + }
24274 + }
24275 +#endif
24276 + }
24277 +}
24278 +
24279 +#ifdef DEBUG
24280 +void hc_xfer_timeout(void *ptr)
24281 +{
24282 + hc_xfer_info_t *xfer_info = NULL;
24283 + int hc_num = 0;
24284 +
24285 + if (ptr)
24286 + xfer_info = (hc_xfer_info_t *) ptr;
24287 +
24288 + if (!xfer_info->hc) {
24289 + DWC_ERROR("xfer_info->hc = %p\n", xfer_info->hc);
24290 + return;
24291 + }
24292 +
24293 + hc_num = xfer_info->hc->hc_num;
24294 + DWC_WARN("%s: timeout on channel %d\n", __func__, hc_num);
24295 + DWC_WARN(" start_hcchar_val 0x%08x\n",
24296 + xfer_info->core_if->start_hcchar_val[hc_num]);
24297 +}
24298 +#endif
24299 +
24300 +void ep_xfer_timeout(void *ptr)
24301 +{
24302 + ep_xfer_info_t *xfer_info = NULL;
24303 + int ep_num = 0;
24304 + dctl_data_t dctl = {.d32 = 0 };
24305 + gintsts_data_t gintsts = {.d32 = 0 };
24306 + gintmsk_data_t gintmsk = {.d32 = 0 };
24307 +
24308 + if (ptr)
24309 + xfer_info = (ep_xfer_info_t *) ptr;
24310 +
24311 + if (!xfer_info->ep) {
24312 + DWC_ERROR("xfer_info->ep = %p\n", xfer_info->ep);
24313 + return;
24314 + }
24315 +
24316 + ep_num = xfer_info->ep->num;
24317 + DWC_WARN("%s: timeout on endpoit %d\n", __func__, ep_num);
24318 + /* Put the sate to 2 as it was time outed */
24319 + xfer_info->state = 2;
24320 +
24321 + dctl.d32 =
24322 + DWC_READ_REG32(&xfer_info->core_if->dev_if->dev_global_regs->dctl);
24323 + gintsts.d32 =
24324 + DWC_READ_REG32(&xfer_info->core_if->core_global_regs->gintsts);
24325 + gintmsk.d32 =
24326 + DWC_READ_REG32(&xfer_info->core_if->core_global_regs->gintmsk);
24327 +
24328 + if (!gintmsk.b.goutnakeff) {
24329 + /* Unmask it */
24330 + gintmsk.b.goutnakeff = 1;
24331 + DWC_WRITE_REG32(&xfer_info->core_if->core_global_regs->gintmsk,
24332 + gintmsk.d32);
24333 +
24334 + }
24335 +
24336 + if (!gintsts.b.goutnakeff) {
24337 + dctl.b.sgoutnak = 1;
24338 + }
24339 + DWC_WRITE_REG32(&xfer_info->core_if->dev_if->dev_global_regs->dctl,
24340 + dctl.d32);
24341 +
24342 +}
24343 +
24344 +void set_pid_isoc(dwc_hc_t * hc)
24345 +{
24346 + /* Set up the initial PID for the transfer. */
24347 + if (hc->speed == DWC_OTG_EP_SPEED_HIGH) {
24348 + if (hc->ep_is_in) {
24349 + if (hc->multi_count == 1) {
24350 + hc->data_pid_start = DWC_OTG_HC_PID_DATA0;
24351 + } else if (hc->multi_count == 2) {
24352 + hc->data_pid_start = DWC_OTG_HC_PID_DATA1;
24353 + } else {
24354 + hc->data_pid_start = DWC_OTG_HC_PID_DATA2;
24355 + }
24356 + } else {
24357 + if (hc->multi_count == 1) {
24358 + hc->data_pid_start = DWC_OTG_HC_PID_DATA0;
24359 + } else {
24360 + hc->data_pid_start = DWC_OTG_HC_PID_MDATA;
24361 + }
24362 + }
24363 + } else {
24364 + hc->data_pid_start = DWC_OTG_HC_PID_DATA0;
24365 + }
24366 +}
24367 +
24368 +/**
24369 + * This function does the setup for a data transfer for a host channel and
24370 + * starts the transfer. May be called in either Slave mode or DMA mode. In
24371 + * Slave mode, the caller must ensure that there is sufficient space in the
24372 + * request queue and Tx Data FIFO.
24373 + *
24374 + * For an OUT transfer in Slave mode, it loads a data packet into the
24375 + * appropriate FIFO. If necessary, additional data packets will be loaded in
24376 + * the Host ISR.
24377 + *
24378 + * For an IN transfer in Slave mode, a data packet is requested. The data
24379 + * packets are unloaded from the Rx FIFO in the Host ISR. If necessary,
24380 + * additional data packets are requested in the Host ISR.
24381 + *
24382 + * For a PING transfer in Slave mode, the Do Ping bit is set in the HCTSIZ
24383 + * register along with a packet count of 1 and the channel is enabled. This
24384 + * causes a single PING transaction to occur. Other fields in HCTSIZ are
24385 + * simply set to 0 since no data transfer occurs in this case.
24386 + *
24387 + * For a PING transfer in DMA mode, the HCTSIZ register is initialized with
24388 + * all the information required to perform the subsequent data transfer. In
24389 + * addition, the Do Ping bit is set in the HCTSIZ register. In this case, the
24390 + * controller performs the entire PING protocol, then starts the data
24391 + * transfer.
24392 + *
24393 + * @param core_if Programming view of DWC_otg controller.
24394 + * @param hc Information needed to initialize the host channel. The xfer_len
24395 + * value may be reduced to accommodate the max widths of the XferSize and
24396 + * PktCnt fields in the HCTSIZn register. The multi_count value may be changed
24397 + * to reflect the final xfer_len value.
24398 + */
24399 +void dwc_otg_hc_start_transfer(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
24400 +{
24401 + hcchar_data_t hcchar;
24402 + hctsiz_data_t hctsiz;
24403 + uint16_t num_packets;
24404 + uint32_t max_hc_xfer_size = core_if->core_params->max_transfer_size;
24405 + uint16_t max_hc_pkt_count = core_if->core_params->max_packet_count;
24406 + dwc_otg_hc_regs_t *hc_regs = core_if->host_if->hc_regs[hc->hc_num];
24407 +
24408 + hctsiz.d32 = 0;
24409 +
24410 + if (hc->do_ping) {
24411 + if (!core_if->dma_enable) {
24412 + dwc_otg_hc_do_ping(core_if, hc);
24413 + hc->xfer_started = 1;
24414 + return;
24415 + } else {
24416 + hctsiz.b.dopng = 1;
24417 + }
24418 + }
24419 +
24420 + if (hc->do_split) {
24421 + num_packets = 1;
24422 +
24423 + if (hc->complete_split && !hc->ep_is_in) {
24424 + /* For CSPLIT OUT Transfer, set the size to 0 so the
24425 + * core doesn't expect any data written to the FIFO */
24426 + hc->xfer_len = 0;
24427 + } else if (hc->ep_is_in || (hc->xfer_len > hc->max_packet)) {
24428 + hc->xfer_len = hc->max_packet;
24429 + } else if (!hc->ep_is_in && (hc->xfer_len > 188)) {
24430 + hc->xfer_len = 188;
24431 + }
24432 +
24433 + hctsiz.b.xfersize = hc->xfer_len;
24434 + } else {
24435 + /*
24436 + * Ensure that the transfer length and packet count will fit
24437 + * in the widths allocated for them in the HCTSIZn register.
24438 + */
24439 + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
24440 + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
24441 + /*
24442 + * Make sure the transfer size is no larger than one
24443 + * (micro)frame's worth of data. (A check was done
24444 + * when the periodic transfer was accepted to ensure
24445 + * that a (micro)frame's worth of data can be
24446 + * programmed into a channel.)
24447 + */
24448 + uint32_t max_periodic_len =
24449 + hc->multi_count * hc->max_packet;
24450 + if (hc->xfer_len > max_periodic_len) {
24451 + hc->xfer_len = max_periodic_len;
24452 + } else {
24453 + }
24454 + } else if (hc->xfer_len > max_hc_xfer_size) {
24455 + /* Make sure that xfer_len is a multiple of max packet size. */
24456 + hc->xfer_len = max_hc_xfer_size - hc->max_packet + 1;
24457 + }
24458 +
24459 + if (hc->xfer_len > 0) {
24460 + num_packets =
24461 + (hc->xfer_len + hc->max_packet -
24462 + 1) / hc->max_packet;
24463 + if (num_packets > max_hc_pkt_count) {
24464 + num_packets = max_hc_pkt_count;
24465 + hc->xfer_len = num_packets * hc->max_packet;
24466 + }
24467 + } else {
24468 + /* Need 1 packet for transfer length of 0. */
24469 + num_packets = 1;
24470 + }
24471 +
24472 + if (hc->ep_is_in) {
24473 + /* Always program an integral # of max packets for IN transfers. */
24474 + hc->xfer_len = num_packets * hc->max_packet;
24475 + }
24476 +
24477 + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
24478 + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
24479 + /*
24480 + * Make sure that the multi_count field matches the
24481 + * actual transfer length.
24482 + */
24483 + hc->multi_count = num_packets;
24484 + }
24485 +
24486 + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC)
24487 + set_pid_isoc(hc);
24488 +
24489 + hctsiz.b.xfersize = hc->xfer_len;
24490 + }
24491 +
24492 + hc->start_pkt_count = num_packets;
24493 + hctsiz.b.pktcnt = num_packets;
24494 + hctsiz.b.pid = hc->data_pid_start;
24495 + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
24496 +
24497 + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d\n", __func__, hc->hc_num);
24498 + DWC_DEBUGPL(DBG_HCDV, " Xfer Size: %d\n", hctsiz.b.xfersize);
24499 + DWC_DEBUGPL(DBG_HCDV, " Num Pkts: %d\n", hctsiz.b.pktcnt);
24500 + DWC_DEBUGPL(DBG_HCDV, " Start PID: %d\n", hctsiz.b.pid);
24501 +
24502 + if (core_if->dma_enable) {
24503 + dwc_dma_t dma_addr;
24504 + if (hc->align_buff) {
24505 + dma_addr = hc->align_buff;
24506 + } else {
24507 + dma_addr = ((unsigned long)hc->xfer_buff & 0xffffffff);
24508 + }
24509 + DWC_WRITE_REG32(&hc_regs->hcdma, dma_addr);
24510 + }
24511 +
24512 + /* Start the split */
24513 + if (hc->do_split) {
24514 + hcsplt_data_t hcsplt;
24515 + hcsplt.d32 = DWC_READ_REG32(&hc_regs->hcsplt);
24516 + hcsplt.b.spltena = 1;
24517 + DWC_WRITE_REG32(&hc_regs->hcsplt, hcsplt.d32);
24518 + }
24519 +
24520 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
24521 + hcchar.b.multicnt = hc->multi_count;
24522 + hc_set_even_odd_frame(core_if, hc, &hcchar);
24523 +#ifdef DEBUG
24524 + core_if->start_hcchar_val[hc->hc_num] = hcchar.d32;
24525 + if (hcchar.b.chdis) {
24526 + DWC_WARN("%s: chdis set, channel %d, hcchar 0x%08x\n",
24527 + __func__, hc->hc_num, hcchar.d32);
24528 + }
24529 +#endif
24530 +
24531 + /* Set host channel enable after all other setup is complete. */
24532 + hcchar.b.chen = 1;
24533 + hcchar.b.chdis = 0;
24534 + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
24535 +
24536 + hc->xfer_started = 1;
24537 + hc->requests++;
24538 +
24539 + if (!core_if->dma_enable && !hc->ep_is_in && hc->xfer_len > 0) {
24540 + /* Load OUT packet into the appropriate Tx FIFO. */
24541 + dwc_otg_hc_write_packet(core_if, hc);
24542 + }
24543 +#ifdef DEBUG
24544 + if (hc->ep_type != DWC_OTG_EP_TYPE_INTR) {
24545 + DWC_DEBUGPL(DBG_HCDV, "transfer %d from core_if %p\n",
24546 + hc->hc_num, core_if);//GRAYG
24547 + core_if->hc_xfer_info[hc->hc_num].core_if = core_if;
24548 + core_if->hc_xfer_info[hc->hc_num].hc = hc;
24549 +
24550 + /* Start a timer for this transfer. */
24551 + DWC_TIMER_SCHEDULE(core_if->hc_xfer_timer[hc->hc_num], 10000);
24552 + }
24553 +#endif
24554 +}
24555 +
24556 +/**
24557 + * This function does the setup for a data transfer for a host channel
24558 + * and starts the transfer in Descriptor DMA mode.
24559 + *
24560 + * Initializes HCTSIZ register. For a PING transfer the Do Ping bit is set.
24561 + * Sets PID and NTD values. For periodic transfers
24562 + * initializes SCHED_INFO field with micro-frame bitmap.
24563 + *
24564 + * Initializes HCDMA register with descriptor list address and CTD value
24565 + * then starts the transfer via enabling the channel.
24566 + *
24567 + * @param core_if Programming view of DWC_otg controller.
24568 + * @param hc Information needed to initialize the host channel.
24569 + */
24570 +void dwc_otg_hc_start_transfer_ddma(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
24571 +{
24572 + dwc_otg_hc_regs_t *hc_regs = core_if->host_if->hc_regs[hc->hc_num];
24573 + hcchar_data_t hcchar;
24574 + hctsiz_data_t hctsiz;
24575 + hcdma_data_t hcdma;
24576 +
24577 + hctsiz.d32 = 0;
24578 +
24579 + if (hc->do_ping)
24580 + hctsiz.b_ddma.dopng = 1;
24581 +
24582 + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC)
24583 + set_pid_isoc(hc);
24584 +
24585 + /* Packet Count and Xfer Size are not used in Descriptor DMA mode */
24586 + hctsiz.b_ddma.pid = hc->data_pid_start;
24587 + hctsiz.b_ddma.ntd = hc->ntd - 1; /* 0 - 1 descriptor, 1 - 2 descriptors, etc. */
24588 + hctsiz.b_ddma.schinfo = hc->schinfo; /* Non-zero only for high-speed interrupt endpoints */
24589 +
24590 + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d\n", __func__, hc->hc_num);
24591 + DWC_DEBUGPL(DBG_HCDV, " Start PID: %d\n", hctsiz.b.pid);
24592 + DWC_DEBUGPL(DBG_HCDV, " NTD: %d\n", hctsiz.b_ddma.ntd);
24593 +
24594 + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
24595 +
24596 + hcdma.d32 = 0;
24597 + hcdma.b.dma_addr = ((uint32_t) hc->desc_list_addr) >> 11;
24598 +
24599 + /* Always start from first descriptor. */
24600 + hcdma.b.ctd = 0;
24601 + DWC_WRITE_REG32(&hc_regs->hcdma, hcdma.d32);
24602 +
24603 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
24604 + hcchar.b.multicnt = hc->multi_count;
24605 +
24606 +#ifdef DEBUG
24607 + core_if->start_hcchar_val[hc->hc_num] = hcchar.d32;
24608 + if (hcchar.b.chdis) {
24609 + DWC_WARN("%s: chdis set, channel %d, hcchar 0x%08x\n",
24610 + __func__, hc->hc_num, hcchar.d32);
24611 + }
24612 +#endif
24613 +
24614 + /* Set host channel enable after all other setup is complete. */
24615 + hcchar.b.chen = 1;
24616 + hcchar.b.chdis = 0;
24617 +
24618 + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
24619 +
24620 + hc->xfer_started = 1;
24621 + hc->requests++;
24622 +
24623 +#ifdef DEBUG
24624 + if ((hc->ep_type != DWC_OTG_EP_TYPE_INTR)
24625 + && (hc->ep_type != DWC_OTG_EP_TYPE_ISOC)) {
24626 + DWC_DEBUGPL(DBG_HCDV, "DMA transfer %d from core_if %p\n",
24627 + hc->hc_num, core_if);//GRAYG
24628 + core_if->hc_xfer_info[hc->hc_num].core_if = core_if;
24629 + core_if->hc_xfer_info[hc->hc_num].hc = hc;
24630 + /* Start a timer for this transfer. */
24631 + DWC_TIMER_SCHEDULE(core_if->hc_xfer_timer[hc->hc_num], 10000);
24632 + }
24633 +#endif
24634 +
24635 +}
24636 +
24637 +/**
24638 + * This function continues a data transfer that was started by previous call
24639 + * to <code>dwc_otg_hc_start_transfer</code>. The caller must ensure there is
24640 + * sufficient space in the request queue and Tx Data FIFO. This function
24641 + * should only be called in Slave mode. In DMA mode, the controller acts
24642 + * autonomously to complete transfers programmed to a host channel.
24643 + *
24644 + * For an OUT transfer, a new data packet is loaded into the appropriate FIFO
24645 + * if there is any data remaining to be queued. For an IN transfer, another
24646 + * data packet is always requested. For the SETUP phase of a control transfer,
24647 + * this function does nothing.
24648 + *
24649 + * @return 1 if a new request is queued, 0 if no more requests are required
24650 + * for this transfer.
24651 + */
24652 +int dwc_otg_hc_continue_transfer(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
24653 +{
24654 + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d\n", __func__, hc->hc_num);
24655 +
24656 + if (hc->do_split) {
24657 + /* SPLITs always queue just once per channel */
24658 + return 0;
24659 + } else if (hc->data_pid_start == DWC_OTG_HC_PID_SETUP) {
24660 + /* SETUPs are queued only once since they can't be NAKed. */
24661 + return 0;
24662 + } else if (hc->ep_is_in) {
24663 + /*
24664 + * Always queue another request for other IN transfers. If
24665 + * back-to-back INs are issued and NAKs are received for both,
24666 + * the driver may still be processing the first NAK when the
24667 + * second NAK is received. When the interrupt handler clears
24668 + * the NAK interrupt for the first NAK, the second NAK will
24669 + * not be seen. So we can't depend on the NAK interrupt
24670 + * handler to requeue a NAKed request. Instead, IN requests
24671 + * are issued each time this function is called. When the
24672 + * transfer completes, the extra requests for the channel will
24673 + * be flushed.
24674 + */
24675 + hcchar_data_t hcchar;
24676 + dwc_otg_hc_regs_t *hc_regs =
24677 + core_if->host_if->hc_regs[hc->hc_num];
24678 +
24679 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
24680 + hc_set_even_odd_frame(core_if, hc, &hcchar);
24681 + hcchar.b.chen = 1;
24682 + hcchar.b.chdis = 0;
24683 + DWC_DEBUGPL(DBG_HCDV, " IN xfer: hcchar = 0x%08x\n",
24684 + hcchar.d32);
24685 + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
24686 + hc->requests++;
24687 + return 1;
24688 + } else {
24689 + /* OUT transfers. */
24690 + if (hc->xfer_count < hc->xfer_len) {
24691 + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
24692 + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
24693 + hcchar_data_t hcchar;
24694 + dwc_otg_hc_regs_t *hc_regs;
24695 + hc_regs = core_if->host_if->hc_regs[hc->hc_num];
24696 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
24697 + hc_set_even_odd_frame(core_if, hc, &hcchar);
24698 + }
24699 +
24700 + /* Load OUT packet into the appropriate Tx FIFO. */
24701 + dwc_otg_hc_write_packet(core_if, hc);
24702 + hc->requests++;
24703 + return 1;
24704 + } else {
24705 + return 0;
24706 + }
24707 + }
24708 +}
24709 +
24710 +/**
24711 + * Starts a PING transfer. This function should only be called in Slave mode.
24712 + * The Do Ping bit is set in the HCTSIZ register, then the channel is enabled.
24713 + */
24714 +void dwc_otg_hc_do_ping(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
24715 +{
24716 + hcchar_data_t hcchar;
24717 + hctsiz_data_t hctsiz;
24718 + dwc_otg_hc_regs_t *hc_regs = core_if->host_if->hc_regs[hc->hc_num];
24719 +
24720 + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d\n", __func__, hc->hc_num);
24721 +
24722 + hctsiz.d32 = 0;
24723 + hctsiz.b.dopng = 1;
24724 + hctsiz.b.pktcnt = 1;
24725 + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
24726 +
24727 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
24728 + hcchar.b.chen = 1;
24729 + hcchar.b.chdis = 0;
24730 + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
24731 +}
24732 +
24733 +/*
24734 + * This function writes a packet into the Tx FIFO associated with the Host
24735 + * Channel. For a channel associated with a non-periodic EP, the non-periodic
24736 + * Tx FIFO is written. For a channel associated with a periodic EP, the
24737 + * periodic Tx FIFO is written. This function should only be called in Slave
24738 + * mode.
24739 + *
24740 + * Upon return the xfer_buff and xfer_count fields in _hc are incremented by
24741 + * then number of bytes written to the Tx FIFO.
24742 + */
24743 +void dwc_otg_hc_write_packet(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
24744 +{
24745 + uint32_t i;
24746 + uint32_t remaining_count;
24747 + uint32_t byte_count;
24748 + uint32_t dword_count;
24749 +
24750 + uint32_t *data_buff = (uint32_t *) (hc->xfer_buff);
24751 + uint32_t *data_fifo = core_if->data_fifo[hc->hc_num];
24752 +
24753 + remaining_count = hc->xfer_len - hc->xfer_count;
24754 + if (remaining_count > hc->max_packet) {
24755 + byte_count = hc->max_packet;
24756 + } else {
24757 + byte_count = remaining_count;
24758 + }
24759 +
24760 + dword_count = (byte_count + 3) / 4;
24761 +
24762 + if ((((unsigned long)data_buff) & 0x3) == 0) {
24763 + /* xfer_buff is DWORD aligned. */
24764 + for (i = 0; i < dword_count; i++, data_buff++) {
24765 + DWC_WRITE_REG32(data_fifo, *data_buff);
24766 + }
24767 + } else {
24768 + /* xfer_buff is not DWORD aligned. */
24769 + for (i = 0; i < dword_count; i++, data_buff++) {
24770 + uint32_t data;
24771 + data =
24772 + (data_buff[0] | data_buff[1] << 8 | data_buff[2] <<
24773 + 16 | data_buff[3] << 24);
24774 + DWC_WRITE_REG32(data_fifo, data);
24775 + }
24776 + }
24777 +
24778 + hc->xfer_count += byte_count;
24779 + hc->xfer_buff += byte_count;
24780 +}
24781 +
24782 +/**
24783 + * Gets the current USB frame number. This is the frame number from the last
24784 + * SOF packet.
24785 + */
24786 +uint32_t dwc_otg_get_frame_number(dwc_otg_core_if_t * core_if)
24787 +{
24788 + dsts_data_t dsts;
24789 + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
24790 +
24791 + /* read current frame/microframe number from DSTS register */
24792 + return dsts.b.soffn;
24793 +}
24794 +
24795 +/**
24796 + * Calculates and gets the frame Interval value of HFIR register according PHY
24797 + * type and speed.The application can modify a value of HFIR register only after
24798 + * the Port Enable bit of the Host Port Control and Status register
24799 + * (HPRT.PrtEnaPort) has been set.
24800 +*/
24801 +
24802 +uint32_t calc_frame_interval(dwc_otg_core_if_t * core_if)
24803 +{
24804 + gusbcfg_data_t usbcfg;
24805 + hwcfg2_data_t hwcfg2;
24806 + hprt0_data_t hprt0;
24807 + int clock = 60; // default value
24808 + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
24809 + hwcfg2.d32 = DWC_READ_REG32(&core_if->core_global_regs->ghwcfg2);
24810 + hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
24811 + if (!usbcfg.b.physel && usbcfg.b.ulpi_utmi_sel && !usbcfg.b.phyif)
24812 + clock = 60;
24813 + if (usbcfg.b.physel && hwcfg2.b.fs_phy_type == 3)
24814 + clock = 48;
24815 + if (!usbcfg.b.phylpwrclksel && !usbcfg.b.physel &&
24816 + !usbcfg.b.ulpi_utmi_sel && usbcfg.b.phyif)
24817 + clock = 30;
24818 + if (!usbcfg.b.phylpwrclksel && !usbcfg.b.physel &&
24819 + !usbcfg.b.ulpi_utmi_sel && !usbcfg.b.phyif)
24820 + clock = 60;
24821 + if (usbcfg.b.phylpwrclksel && !usbcfg.b.physel &&
24822 + !usbcfg.b.ulpi_utmi_sel && usbcfg.b.phyif)
24823 + clock = 48;
24824 + if (usbcfg.b.physel && !usbcfg.b.phyif && hwcfg2.b.fs_phy_type == 2)
24825 + clock = 48;
24826 + if (usbcfg.b.physel && hwcfg2.b.fs_phy_type == 1)
24827 + clock = 48;
24828 + if (hprt0.b.prtspd == 0)
24829 + /* High speed case */
24830 + return 125 * clock - 1;
24831 + else
24832 + /* FS/LS case */
24833 + return 1000 * clock - 1;
24834 +}
24835 +
24836 +/**
24837 + * This function reads a setup packet from the Rx FIFO into the destination
24838 + * buffer. This function is called from the Rx Status Queue Level (RxStsQLvl)
24839 + * Interrupt routine when a SETUP packet has been received in Slave mode.
24840 + *
24841 + * @param core_if Programming view of DWC_otg controller.
24842 + * @param dest Destination buffer for packet data.
24843 + */
24844 +void dwc_otg_read_setup_packet(dwc_otg_core_if_t * core_if, uint32_t * dest)
24845 +{
24846 + device_grxsts_data_t status;
24847 + /* Get the 8 bytes of a setup transaction data */
24848 +
24849 + /* Pop 2 DWORDS off the receive data FIFO into memory */
24850 + dest[0] = DWC_READ_REG32(core_if->data_fifo[0]);
24851 + dest[1] = DWC_READ_REG32(core_if->data_fifo[0]);
24852 + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
24853 + status.d32 =
24854 + DWC_READ_REG32(&core_if->core_global_regs->grxstsp);
24855 + DWC_DEBUGPL(DBG_ANY,
24856 + "EP:%d BCnt:%d " "pktsts:%x Frame:%d(0x%0x)\n",
24857 + status.b.epnum, status.b.bcnt, status.b.pktsts,
24858 + status.b.fn, status.b.fn);
24859 + }
24860 +}
24861 +
24862 +/**
24863 + * This function enables EP0 OUT to receive SETUP packets and configures EP0
24864 + * IN for transmitting packets. It is normally called when the
24865 + * "Enumeration Done" interrupt occurs.
24866 + *
24867 + * @param core_if Programming view of DWC_otg controller.
24868 + * @param ep The EP0 data.
24869 + */
24870 +void dwc_otg_ep0_activate(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
24871 +{
24872 + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
24873 + dsts_data_t dsts;
24874 + depctl_data_t diepctl;
24875 + depctl_data_t doepctl;
24876 + dctl_data_t dctl = {.d32 = 0 };
24877 +
24878 + ep->stp_rollover = 0;
24879 + /* Read the Device Status and Endpoint 0 Control registers */
24880 + dsts.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dsts);
24881 + diepctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[0]->diepctl);
24882 + doepctl.d32 = DWC_READ_REG32(&dev_if->out_ep_regs[0]->doepctl);
24883 +
24884 + /* Set the MPS of the IN EP based on the enumeration speed */
24885 + switch (dsts.b.enumspd) {
24886 + case DWC_DSTS_ENUMSPD_HS_PHY_30MHZ_OR_60MHZ:
24887 + case DWC_DSTS_ENUMSPD_FS_PHY_30MHZ_OR_60MHZ:
24888 + case DWC_DSTS_ENUMSPD_FS_PHY_48MHZ:
24889 + diepctl.b.mps = DWC_DEP0CTL_MPS_64;
24890 + break;
24891 + case DWC_DSTS_ENUMSPD_LS_PHY_6MHZ:
24892 + diepctl.b.mps = DWC_DEP0CTL_MPS_8;
24893 + break;
24894 + }
24895 +
24896 + DWC_WRITE_REG32(&dev_if->in_ep_regs[0]->diepctl, diepctl.d32);
24897 +
24898 + /* Enable OUT EP for receive */
24899 + if (core_if->snpsid <= OTG_CORE_REV_2_94a) {
24900 + doepctl.b.epena = 1;
24901 + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doepctl, doepctl.d32);
24902 + }
24903 +#ifdef VERBOSE
24904 + DWC_DEBUGPL(DBG_PCDV, "doepctl0=%0x\n",
24905 + DWC_READ_REG32(&dev_if->out_ep_regs[0]->doepctl));
24906 + DWC_DEBUGPL(DBG_PCDV, "diepctl0=%0x\n",
24907 + DWC_READ_REG32(&dev_if->in_ep_regs[0]->diepctl));
24908 +#endif
24909 + dctl.b.cgnpinnak = 1;
24910 +
24911 + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, dctl.d32, dctl.d32);
24912 + DWC_DEBUGPL(DBG_PCDV, "dctl=%0x\n",
24913 + DWC_READ_REG32(&dev_if->dev_global_regs->dctl));
24914 +
24915 +}
24916 +
24917 +/**
24918 + * This function activates an EP. The Device EP control register for
24919 + * the EP is configured as defined in the ep structure. Note: This
24920 + * function is not used for EP0.
24921 + *
24922 + * @param core_if Programming view of DWC_otg controller.
24923 + * @param ep The EP to activate.
24924 + */
24925 +void dwc_otg_ep_activate(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
24926 +{
24927 + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
24928 + depctl_data_t depctl;
24929 + volatile uint32_t *addr;
24930 + daint_data_t daintmsk = {.d32 = 0 };
24931 + dcfg_data_t dcfg;
24932 + uint8_t i;
24933 +
24934 + DWC_DEBUGPL(DBG_PCDV, "%s() EP%d-%s\n", __func__, ep->num,
24935 + (ep->is_in ? "IN" : "OUT"));
24936 +
24937 +#ifdef DWC_UTE_PER_IO
24938 + ep->xiso_frame_num = 0xFFFFFFFF;
24939 + ep->xiso_active_xfers = 0;
24940 + ep->xiso_queued_xfers = 0;
24941 +#endif
24942 + /* Read DEPCTLn register */
24943 + if (ep->is_in == 1) {
24944 + addr = &dev_if->in_ep_regs[ep->num]->diepctl;
24945 + daintmsk.ep.in = 1 << ep->num;
24946 + } else {
24947 + addr = &dev_if->out_ep_regs[ep->num]->doepctl;
24948 + daintmsk.ep.out = 1 << ep->num;
24949 + }
24950 +
24951 + /* If the EP is already active don't change the EP Control
24952 + * register. */
24953 + depctl.d32 = DWC_READ_REG32(addr);
24954 + if (!depctl.b.usbactep) {
24955 + depctl.b.mps = ep->maxpacket;
24956 + depctl.b.eptype = ep->type;
24957 + depctl.b.txfnum = ep->tx_fifo_num;
24958 +
24959 + if (ep->type == DWC_OTG_EP_TYPE_ISOC) {
24960 + depctl.b.setd0pid = 1; // ???
24961 + } else {
24962 + depctl.b.setd0pid = 1;
24963 + }
24964 + depctl.b.usbactep = 1;
24965 +
24966 + /* Update nextep_seq array and EPMSCNT in DCFG*/
24967 + if (!(depctl.b.eptype & 1) && (ep->is_in == 1)) { // NP IN EP
24968 + for (i = 0; i <= core_if->dev_if->num_in_eps; i++) {
24969 + if (core_if->nextep_seq[i] == core_if->first_in_nextep_seq)
24970 + break;
24971 + }
24972 + core_if->nextep_seq[i] = ep->num;
24973 + core_if->nextep_seq[ep->num] = core_if->first_in_nextep_seq;
24974 + depctl.b.nextep = core_if->nextep_seq[ep->num];
24975 + dcfg.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dcfg);
24976 + dcfg.b.epmscnt++;
24977 + DWC_WRITE_REG32(&dev_if->dev_global_regs->dcfg, dcfg.d32);
24978 +
24979 + DWC_DEBUGPL(DBG_PCDV,
24980 + "%s first_in_nextep_seq= %2d; nextep_seq[]:\n",
24981 + __func__, core_if->first_in_nextep_seq);
24982 + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
24983 + DWC_DEBUGPL(DBG_PCDV, "%2d\n",
24984 + core_if->nextep_seq[i]);
24985 + }
24986 +
24987 + }
24988 +
24989 +
24990 + DWC_WRITE_REG32(addr, depctl.d32);
24991 + DWC_DEBUGPL(DBG_PCDV, "DEPCTL=%08x\n", DWC_READ_REG32(addr));
24992 + }
24993 +
24994 + /* Enable the Interrupt for this EP */
24995 + if (core_if->multiproc_int_enable) {
24996 + if (ep->is_in == 1) {
24997 + diepmsk_data_t diepmsk = {.d32 = 0 };
24998 + diepmsk.b.xfercompl = 1;
24999 + diepmsk.b.timeout = 1;
25000 + diepmsk.b.epdisabled = 1;
25001 + diepmsk.b.ahberr = 1;
25002 + diepmsk.b.intknepmis = 1;
25003 + if (!core_if->en_multiple_tx_fifo && core_if->dma_enable)
25004 + diepmsk.b.intknepmis = 0;
25005 + diepmsk.b.txfifoundrn = 1; //?????
25006 + if (ep->type == DWC_OTG_EP_TYPE_ISOC) {
25007 + diepmsk.b.nak = 1;
25008 + }
25009 +
25010 +
25011 +
25012 +/*
25013 + if (core_if->dma_desc_enable) {
25014 + diepmsk.b.bna = 1;
25015 + }
25016 +*/
25017 +/*
25018 + if (core_if->dma_enable) {
25019 + doepmsk.b.nak = 1;
25020 + }
25021 +*/
25022 + DWC_WRITE_REG32(&dev_if->dev_global_regs->
25023 + diepeachintmsk[ep->num], diepmsk.d32);
25024 +
25025 + } else {
25026 + doepmsk_data_t doepmsk = {.d32 = 0 };
25027 + doepmsk.b.xfercompl = 1;
25028 + doepmsk.b.ahberr = 1;
25029 + doepmsk.b.epdisabled = 1;
25030 + if (ep->type == DWC_OTG_EP_TYPE_ISOC)
25031 + doepmsk.b.outtknepdis = 1;
25032 +
25033 +/*
25034 +
25035 + if (core_if->dma_desc_enable) {
25036 + doepmsk.b.bna = 1;
25037 + }
25038 +*/
25039 +/*
25040 + doepmsk.b.babble = 1;
25041 + doepmsk.b.nyet = 1;
25042 + doepmsk.b.nak = 1;
25043 +*/
25044 + DWC_WRITE_REG32(&dev_if->dev_global_regs->
25045 + doepeachintmsk[ep->num], doepmsk.d32);
25046 + }
25047 + DWC_MODIFY_REG32(&dev_if->dev_global_regs->deachintmsk,
25048 + 0, daintmsk.d32);
25049 + } else {
25050 + if (ep->type == DWC_OTG_EP_TYPE_ISOC) {
25051 + if (ep->is_in) {
25052 + diepmsk_data_t diepmsk = {.d32 = 0 };
25053 + diepmsk.b.nak = 1;
25054 + DWC_MODIFY_REG32(&dev_if->dev_global_regs->diepmsk, 0, diepmsk.d32);
25055 + } else {
25056 + doepmsk_data_t doepmsk = {.d32 = 0 };
25057 + doepmsk.b.outtknepdis = 1;
25058 + DWC_MODIFY_REG32(&dev_if->dev_global_regs->doepmsk, 0, doepmsk.d32);
25059 + }
25060 + }
25061 + DWC_MODIFY_REG32(&dev_if->dev_global_regs->daintmsk,
25062 + 0, daintmsk.d32);
25063 + }
25064 +
25065 + DWC_DEBUGPL(DBG_PCDV, "DAINTMSK=%0x\n",
25066 + DWC_READ_REG32(&dev_if->dev_global_regs->daintmsk));
25067 +
25068 + ep->stall_clear_flag = 0;
25069 +
25070 + return;
25071 +}
25072 +
25073 +/**
25074 + * This function deactivates an EP. This is done by clearing the USB Active
25075 + * EP bit in the Device EP control register. Note: This function is not used
25076 + * for EP0. EP0 cannot be deactivated.
25077 + *
25078 + * @param core_if Programming view of DWC_otg controller.
25079 + * @param ep The EP to deactivate.
25080 + */
25081 +void dwc_otg_ep_deactivate(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
25082 +{
25083 + depctl_data_t depctl = {.d32 = 0 };
25084 + volatile uint32_t *addr;
25085 + daint_data_t daintmsk = {.d32 = 0 };
25086 + dcfg_data_t dcfg;
25087 + uint8_t i = 0;
25088 +
25089 +#ifdef DWC_UTE_PER_IO
25090 + ep->xiso_frame_num = 0xFFFFFFFF;
25091 + ep->xiso_active_xfers = 0;
25092 + ep->xiso_queued_xfers = 0;
25093 +#endif
25094 +
25095 + /* Read DEPCTLn register */
25096 + if (ep->is_in == 1) {
25097 + addr = &core_if->dev_if->in_ep_regs[ep->num]->diepctl;
25098 + daintmsk.ep.in = 1 << ep->num;
25099 + } else {
25100 + addr = &core_if->dev_if->out_ep_regs[ep->num]->doepctl;
25101 + daintmsk.ep.out = 1 << ep->num;
25102 + }
25103 +
25104 + depctl.d32 = DWC_READ_REG32(addr);
25105 +
25106 + depctl.b.usbactep = 0;
25107 +
25108 + /* Update nextep_seq array and EPMSCNT in DCFG*/
25109 + if (!(depctl.b.eptype & 1) && ep->is_in == 1) { // NP EP IN
25110 + for (i = 0; i <= core_if->dev_if->num_in_eps; i++) {
25111 + if (core_if->nextep_seq[i] == ep->num)
25112 + break;
25113 + }
25114 + core_if->nextep_seq[i] = core_if->nextep_seq[ep->num];
25115 + if (core_if->first_in_nextep_seq == ep->num)
25116 + core_if->first_in_nextep_seq = i;
25117 + core_if->nextep_seq[ep->num] = 0xff;
25118 + depctl.b.nextep = 0;
25119 + dcfg.d32 =
25120 + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
25121 + dcfg.b.epmscnt--;
25122 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg,
25123 + dcfg.d32);
25124 +
25125 + DWC_DEBUGPL(DBG_PCDV,
25126 + "%s first_in_nextep_seq= %2d; nextep_seq[]:\n",
25127 + __func__, core_if->first_in_nextep_seq);
25128 + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
25129 + DWC_DEBUGPL(DBG_PCDV, "%2d\n", core_if->nextep_seq[i]);
25130 + }
25131 + }
25132 +
25133 + if (ep->is_in == 1)
25134 + depctl.b.txfnum = 0;
25135 +
25136 + if (core_if->dma_desc_enable)
25137 + depctl.b.epdis = 1;
25138 +
25139 + DWC_WRITE_REG32(addr, depctl.d32);
25140 + depctl.d32 = DWC_READ_REG32(addr);
25141 + if (core_if->dma_enable && ep->type == DWC_OTG_EP_TYPE_ISOC
25142 + && depctl.b.epena) {
25143 + depctl_data_t depctl = {.d32 = 0};
25144 + if (ep->is_in) {
25145 + diepint_data_t diepint = {.d32 = 0};
25146 +
25147 + depctl.b.snak = 1;
25148 + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
25149 + diepctl, depctl.d32);
25150 + do {
25151 + dwc_udelay(10);
25152 + diepint.d32 =
25153 + DWC_READ_REG32(&core_if->
25154 + dev_if->in_ep_regs[ep->num]->
25155 + diepint);
25156 + } while (!diepint.b.inepnakeff);
25157 + diepint.b.inepnakeff = 1;
25158 + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
25159 + diepint, diepint.d32);
25160 + depctl.d32 = 0;
25161 + depctl.b.epdis = 1;
25162 + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
25163 + diepctl, depctl.d32);
25164 + do {
25165 + dwc_udelay(10);
25166 + diepint.d32 =
25167 + DWC_READ_REG32(&core_if->
25168 + dev_if->in_ep_regs[ep->num]->
25169 + diepint);
25170 + } while (!diepint.b.epdisabled);
25171 + diepint.b.epdisabled = 1;
25172 + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
25173 + diepint, diepint.d32);
25174 + } else {
25175 + dctl_data_t dctl = {.d32 = 0};
25176 + gintmsk_data_t gintsts = {.d32 = 0};
25177 + doepint_data_t doepint = {.d32 = 0};
25178 + dctl.b.sgoutnak = 1;
25179 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
25180 + dctl, 0, dctl.d32);
25181 + do {
25182 + dwc_udelay(10);
25183 + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
25184 + } while (!gintsts.b.goutnakeff);
25185 + gintsts.d32 = 0;
25186 + gintsts.b.goutnakeff = 1;
25187 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
25188 +
25189 + depctl.d32 = 0;
25190 + depctl.b.epdis = 1;
25191 + depctl.b.snak = 1;
25192 + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[ep->num]->doepctl, depctl.d32);
25193 + do
25194 + {
25195 + dwc_udelay(10);
25196 + doepint.d32 = DWC_READ_REG32(&core_if->dev_if->
25197 + out_ep_regs[ep->num]->doepint);
25198 + } while (!doepint.b.epdisabled);
25199 +
25200 + doepint.b.epdisabled = 1;
25201 + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[ep->num]->doepint, doepint.d32);
25202 +
25203 + dctl.d32 = 0;
25204 + dctl.b.cgoutnak = 1;
25205 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
25206 + }
25207 + }
25208 +
25209 + /* Disable the Interrupt for this EP */
25210 + if (core_if->multiproc_int_enable) {
25211 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->deachintmsk,
25212 + daintmsk.d32, 0);
25213 +
25214 + if (ep->is_in == 1) {
25215 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->
25216 + diepeachintmsk[ep->num], 0);
25217 + } else {
25218 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->
25219 + doepeachintmsk[ep->num], 0);
25220 + }
25221 + } else {
25222 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->daintmsk,
25223 + daintmsk.d32, 0);
25224 + }
25225 +
25226 +}
25227 +
25228 +/**
25229 + * This function initializes dma descriptor chain.
25230 + *
25231 + * @param core_if Programming view of DWC_otg controller.
25232 + * @param ep The EP to start the transfer on.
25233 + */
25234 +static void init_dma_desc_chain(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
25235 +{
25236 + dwc_otg_dev_dma_desc_t *dma_desc;
25237 + uint32_t offset;
25238 + uint32_t xfer_est;
25239 + int i;
25240 + unsigned maxxfer_local, total_len;
25241 +
25242 + if (!ep->is_in && ep->type == DWC_OTG_EP_TYPE_INTR &&
25243 + (ep->maxpacket%4)) {
25244 + maxxfer_local = ep->maxpacket;
25245 + total_len = ep->xfer_len;
25246 + } else {
25247 + maxxfer_local = ep->maxxfer;
25248 + total_len = ep->total_len;
25249 + }
25250 +
25251 + ep->desc_cnt = (total_len / maxxfer_local) +
25252 + ((total_len % maxxfer_local) ? 1 : 0);
25253 +
25254 + if (!ep->desc_cnt)
25255 + ep->desc_cnt = 1;
25256 +
25257 + if (ep->desc_cnt > MAX_DMA_DESC_CNT)
25258 + ep->desc_cnt = MAX_DMA_DESC_CNT;
25259 +
25260 + dma_desc = ep->desc_addr;
25261 + if (maxxfer_local == ep->maxpacket) {
25262 + if ((total_len % maxxfer_local) &&
25263 + (total_len/maxxfer_local < MAX_DMA_DESC_CNT)) {
25264 + xfer_est = (ep->desc_cnt - 1) * maxxfer_local +
25265 + (total_len % maxxfer_local);
25266 + } else
25267 + xfer_est = ep->desc_cnt * maxxfer_local;
25268 + } else
25269 + xfer_est = total_len;
25270 + offset = 0;
25271 + for (i = 0; i < ep->desc_cnt; ++i) {
25272 + /** DMA Descriptor Setup */
25273 + if (xfer_est > maxxfer_local) {
25274 + dma_desc->status.b.bs = BS_HOST_BUSY;
25275 + dma_desc->status.b.l = 0;
25276 + dma_desc->status.b.ioc = 0;
25277 + dma_desc->status.b.sp = 0;
25278 + dma_desc->status.b.bytes = maxxfer_local;
25279 + dma_desc->buf = ep->dma_addr + offset;
25280 + dma_desc->status.b.sts = 0;
25281 + dma_desc->status.b.bs = BS_HOST_READY;
25282 +
25283 + xfer_est -= maxxfer_local;
25284 + offset += maxxfer_local;
25285 + } else {
25286 + dma_desc->status.b.bs = BS_HOST_BUSY;
25287 + dma_desc->status.b.l = 1;
25288 + dma_desc->status.b.ioc = 1;
25289 + if (ep->is_in) {
25290 + dma_desc->status.b.sp =
25291 + (xfer_est %
25292 + ep->maxpacket) ? 1 : ((ep->
25293 + sent_zlp) ? 1 : 0);
25294 + dma_desc->status.b.bytes = xfer_est;
25295 + } else {
25296 + if (maxxfer_local == ep->maxpacket)
25297 + dma_desc->status.b.bytes = xfer_est;
25298 + else
25299 + dma_desc->status.b.bytes =
25300 + xfer_est + ((4 - (xfer_est & 0x3)) & 0x3);
25301 + }
25302 +
25303 + dma_desc->buf = ep->dma_addr + offset;
25304 + dma_desc->status.b.sts = 0;
25305 + dma_desc->status.b.bs = BS_HOST_READY;
25306 + }
25307 + dma_desc++;
25308 + }
25309 +}
25310 +/**
25311 + * This function is called when to write ISOC data into appropriate dedicated
25312 + * periodic FIFO.
25313 + */
25314 +static int32_t write_isoc_tx_fifo(dwc_otg_core_if_t * core_if, dwc_ep_t * dwc_ep)
25315 +{
25316 + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
25317 + dwc_otg_dev_in_ep_regs_t *ep_regs;
25318 + dtxfsts_data_t txstatus = {.d32 = 0 };
25319 + uint32_t len = 0;
25320 + int epnum = dwc_ep->num;
25321 + int dwords;
25322 +
25323 + DWC_DEBUGPL(DBG_PCD, "Dedicated TxFifo Empty: %d \n", epnum);
25324 +
25325 + ep_regs = core_if->dev_if->in_ep_regs[epnum];
25326 +
25327 + len = dwc_ep->xfer_len - dwc_ep->xfer_count;
25328 +
25329 + if (len > dwc_ep->maxpacket) {
25330 + len = dwc_ep->maxpacket;
25331 + }
25332 +
25333 + dwords = (len + 3) / 4;
25334 +
25335 + /* While there is space in the queue and space in the FIFO and
25336 + * More data to tranfer, Write packets to the Tx FIFO */
25337 + txstatus.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts);
25338 + DWC_DEBUGPL(DBG_PCDV, "b4 dtxfsts[%d]=0x%08x\n", epnum, txstatus.d32);
25339 +
25340 + while (txstatus.b.txfspcavail > dwords &&
25341 + dwc_ep->xfer_count < dwc_ep->xfer_len && dwc_ep->xfer_len != 0) {
25342 + /* Write the FIFO */
25343 + dwc_otg_ep_write_packet(core_if, dwc_ep, 0);
25344 +
25345 + len = dwc_ep->xfer_len - dwc_ep->xfer_count;
25346 + if (len > dwc_ep->maxpacket) {
25347 + len = dwc_ep->maxpacket;
25348 + }
25349 +
25350 + dwords = (len + 3) / 4;
25351 + txstatus.d32 =
25352 + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts);
25353 + DWC_DEBUGPL(DBG_PCDV, "dtxfsts[%d]=0x%08x\n", epnum,
25354 + txstatus.d32);
25355 + }
25356 +
25357 + DWC_DEBUGPL(DBG_PCDV, "b4 dtxfsts[%d]=0x%08x\n", epnum,
25358 + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts));
25359 +
25360 + return 1;
25361 +}
25362 +/**
25363 + * This function does the setup for a data transfer for an EP and
25364 + * starts the transfer. For an IN transfer, the packets will be
25365 + * loaded into the appropriate Tx FIFO in the ISR. For OUT transfers,
25366 + * the packets are unloaded from the Rx FIFO in the ISR. the ISR.
25367 + *
25368 + * @param core_if Programming view of DWC_otg controller.
25369 + * @param ep The EP to start the transfer on.
25370 + */
25371 +
25372 +void dwc_otg_ep_start_transfer(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
25373 +{
25374 + depctl_data_t depctl;
25375 + deptsiz_data_t deptsiz;
25376 + gintmsk_data_t intr_mask = {.d32 = 0 };
25377 +
25378 + DWC_DEBUGPL((DBG_PCDV | DBG_CILV), "%s()\n", __func__);
25379 + DWC_DEBUGPL(DBG_PCD, "ep%d-%s xfer_len=%d xfer_cnt=%d "
25380 + "xfer_buff=%p start_xfer_buff=%p, total_len = %d\n",
25381 + ep->num, (ep->is_in ? "IN" : "OUT"), ep->xfer_len,
25382 + ep->xfer_count, ep->xfer_buff, ep->start_xfer_buff,
25383 + ep->total_len);
25384 + /* IN endpoint */
25385 + if (ep->is_in == 1) {
25386 + dwc_otg_dev_in_ep_regs_t *in_regs =
25387 + core_if->dev_if->in_ep_regs[ep->num];
25388 +
25389 + gnptxsts_data_t gtxstatus;
25390 +
25391 + gtxstatus.d32 =
25392 + DWC_READ_REG32(&core_if->core_global_regs->gnptxsts);
25393 +
25394 + if (core_if->en_multiple_tx_fifo == 0
25395 + && gtxstatus.b.nptxqspcavail == 0 && !core_if->dma_enable) {
25396 +#ifdef DEBUG
25397 + DWC_PRINTF("TX Queue Full (0x%0x)\n", gtxstatus.d32);
25398 +#endif
25399 + return;
25400 + }
25401 +
25402 + depctl.d32 = DWC_READ_REG32(&(in_regs->diepctl));
25403 + deptsiz.d32 = DWC_READ_REG32(&(in_regs->dieptsiz));
25404 +
25405 + if (ep->maxpacket > ep->maxxfer / MAX_PKT_CNT)
25406 + ep->xfer_len += (ep->maxxfer < (ep->total_len - ep->xfer_len)) ?
25407 + ep->maxxfer : (ep->total_len - ep->xfer_len);
25408 + else
25409 + ep->xfer_len += (MAX_PKT_CNT * ep->maxpacket < (ep->total_len - ep->xfer_len)) ?
25410 + MAX_PKT_CNT * ep->maxpacket : (ep->total_len - ep->xfer_len);
25411 +
25412 +
25413 + /* Zero Length Packet? */
25414 + if ((ep->xfer_len - ep->xfer_count) == 0) {
25415 + deptsiz.b.xfersize = 0;
25416 + deptsiz.b.pktcnt = 1;
25417 + } else {
25418 + /* Program the transfer size and packet count
25419 + * as follows: xfersize = N * maxpacket +
25420 + * short_packet pktcnt = N + (short_packet
25421 + * exist ? 1 : 0)
25422 + */
25423 + deptsiz.b.xfersize = ep->xfer_len - ep->xfer_count;
25424 + deptsiz.b.pktcnt =
25425 + (ep->xfer_len - ep->xfer_count - 1 +
25426 + ep->maxpacket) / ep->maxpacket;
25427 + if (deptsiz.b.pktcnt > MAX_PKT_CNT) {
25428 + deptsiz.b.pktcnt = MAX_PKT_CNT;
25429 + deptsiz.b.xfersize = deptsiz.b.pktcnt * ep->maxpacket;
25430 + }
25431 + if (ep->type == DWC_OTG_EP_TYPE_ISOC)
25432 + deptsiz.b.mc = deptsiz.b.pktcnt;
25433 + }
25434 +
25435 + /* Write the DMA register */
25436 + if (core_if->dma_enable) {
25437 + if (core_if->dma_desc_enable == 0) {
25438 + if (ep->type != DWC_OTG_EP_TYPE_ISOC)
25439 + deptsiz.b.mc = 1;
25440 + DWC_WRITE_REG32(&in_regs->dieptsiz,
25441 + deptsiz.d32);
25442 + DWC_WRITE_REG32(&(in_regs->diepdma),
25443 + (uint32_t) ep->dma_addr);
25444 + } else {
25445 +#ifdef DWC_UTE_CFI
25446 + /* The descriptor chain should be already initialized by now */
25447 + if (ep->buff_mode != BM_STANDARD) {
25448 + DWC_WRITE_REG32(&in_regs->diepdma,
25449 + ep->descs_dma_addr);
25450 + } else {
25451 +#endif
25452 + init_dma_desc_chain(core_if, ep);
25453 + /** DIEPDMAn Register write */
25454 + DWC_WRITE_REG32(&in_regs->diepdma,
25455 + ep->dma_desc_addr);
25456 +#ifdef DWC_UTE_CFI
25457 + }
25458 +#endif
25459 + }
25460 + } else {
25461 + DWC_WRITE_REG32(&in_regs->dieptsiz, deptsiz.d32);
25462 + if (ep->type != DWC_OTG_EP_TYPE_ISOC) {
25463 + /**
25464 + * Enable the Non-Periodic Tx FIFO empty interrupt,
25465 + * or the Tx FIFO epmty interrupt in dedicated Tx FIFO mode,
25466 + * the data will be written into the fifo by the ISR.
25467 + */
25468 + if (core_if->en_multiple_tx_fifo == 0) {
25469 + intr_mask.b.nptxfempty = 1;
25470 + DWC_MODIFY_REG32
25471 + (&core_if->core_global_regs->gintmsk,
25472 + intr_mask.d32, intr_mask.d32);
25473 + } else {
25474 + /* Enable the Tx FIFO Empty Interrupt for this EP */
25475 + if (ep->xfer_len > 0) {
25476 + uint32_t fifoemptymsk = 0;
25477 + fifoemptymsk = 1 << ep->num;
25478 + DWC_MODIFY_REG32
25479 + (&core_if->dev_if->dev_global_regs->dtknqr4_fifoemptymsk,
25480 + 0, fifoemptymsk);
25481 +
25482 + }
25483 + }
25484 + } else {
25485 + write_isoc_tx_fifo(core_if, ep);
25486 + }
25487 + }
25488 + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable)
25489 + depctl.b.nextep = core_if->nextep_seq[ep->num];
25490 +
25491 + if (ep->type == DWC_OTG_EP_TYPE_ISOC) {
25492 + dsts_data_t dsts = {.d32 = 0};
25493 + if (ep->bInterval == 1) {
25494 + dsts.d32 =
25495 + DWC_READ_REG32(&core_if->dev_if->
25496 + dev_global_regs->dsts);
25497 + ep->frame_num = dsts.b.soffn + ep->bInterval;
25498 + if (ep->frame_num > 0x3FFF) {
25499 + ep->frm_overrun = 1;
25500 + ep->frame_num &= 0x3FFF;
25501 + } else
25502 + ep->frm_overrun = 0;
25503 + if (ep->frame_num & 0x1) {
25504 + depctl.b.setd1pid = 1;
25505 + } else {
25506 + depctl.b.setd0pid = 1;
25507 + }
25508 + }
25509 + }
25510 + /* EP enable, IN data in FIFO */
25511 + depctl.b.cnak = 1;
25512 + depctl.b.epena = 1;
25513 + DWC_WRITE_REG32(&in_regs->diepctl, depctl.d32);
25514 +
25515 + } else {
25516 + /* OUT endpoint */
25517 + dwc_otg_dev_out_ep_regs_t *out_regs =
25518 + core_if->dev_if->out_ep_regs[ep->num];
25519 +
25520 + depctl.d32 = DWC_READ_REG32(&(out_regs->doepctl));
25521 + deptsiz.d32 = DWC_READ_REG32(&(out_regs->doeptsiz));
25522 +
25523 + if (!core_if->dma_desc_enable) {
25524 + if (ep->maxpacket > ep->maxxfer / MAX_PKT_CNT)
25525 + ep->xfer_len += (ep->maxxfer < (ep->total_len - ep->xfer_len)) ?
25526 + ep->maxxfer : (ep->total_len - ep->xfer_len);
25527 + else
25528 + ep->xfer_len += (MAX_PKT_CNT * ep->maxpacket < (ep->total_len
25529 + - ep->xfer_len)) ? MAX_PKT_CNT * ep->maxpacket : (ep->total_len - ep->xfer_len);
25530 + }
25531 +
25532 + /* Program the transfer size and packet count as follows:
25533 + *
25534 + * pktcnt = N
25535 + * xfersize = N * maxpacket
25536 + */
25537 + if ((ep->xfer_len - ep->xfer_count) == 0) {
25538 + /* Zero Length Packet */
25539 + deptsiz.b.xfersize = ep->maxpacket;
25540 + deptsiz.b.pktcnt = 1;
25541 + } else {
25542 + deptsiz.b.pktcnt =
25543 + (ep->xfer_len - ep->xfer_count +
25544 + (ep->maxpacket - 1)) / ep->maxpacket;
25545 + if (deptsiz.b.pktcnt > MAX_PKT_CNT) {
25546 + deptsiz.b.pktcnt = MAX_PKT_CNT;
25547 + }
25548 + if (!core_if->dma_desc_enable) {
25549 + ep->xfer_len =
25550 + deptsiz.b.pktcnt * ep->maxpacket + ep->xfer_count;
25551 + }
25552 + deptsiz.b.xfersize = ep->xfer_len - ep->xfer_count;
25553 + }
25554 +
25555 + DWC_DEBUGPL(DBG_PCDV, "ep%d xfersize=%d pktcnt=%d\n",
25556 + ep->num, deptsiz.b.xfersize, deptsiz.b.pktcnt);
25557 +
25558 + if (core_if->dma_enable) {
25559 + if (!core_if->dma_desc_enable) {
25560 + DWC_WRITE_REG32(&out_regs->doeptsiz,
25561 + deptsiz.d32);
25562 +
25563 + DWC_WRITE_REG32(&(out_regs->doepdma),
25564 + (uint32_t) ep->dma_addr);
25565 + } else {
25566 +#ifdef DWC_UTE_CFI
25567 + /* The descriptor chain should be already initialized by now */
25568 + if (ep->buff_mode != BM_STANDARD) {
25569 + DWC_WRITE_REG32(&out_regs->doepdma,
25570 + ep->descs_dma_addr);
25571 + } else {
25572 +#endif
25573 + /** This is used for interrupt out transfers*/
25574 + if (!ep->xfer_len)
25575 + ep->xfer_len = ep->total_len;
25576 + init_dma_desc_chain(core_if, ep);
25577 +
25578 + if (core_if->core_params->dev_out_nak) {
25579 + if (ep->type == DWC_OTG_EP_TYPE_BULK) {
25580 + deptsiz.b.pktcnt = (ep->total_len +
25581 + (ep->maxpacket - 1)) / ep->maxpacket;
25582 + deptsiz.b.xfersize = ep->total_len;
25583 + /* Remember initial value of doeptsiz */
25584 + core_if->start_doeptsiz_val[ep->num] = deptsiz.d32;
25585 + DWC_WRITE_REG32(&out_regs->doeptsiz,
25586 + deptsiz.d32);
25587 + }
25588 + }
25589 + /** DOEPDMAn Register write */
25590 + DWC_WRITE_REG32(&out_regs->doepdma,
25591 + ep->dma_desc_addr);
25592 +#ifdef DWC_UTE_CFI
25593 + }
25594 +#endif
25595 + }
25596 + } else {
25597 + DWC_WRITE_REG32(&out_regs->doeptsiz, deptsiz.d32);
25598 + }
25599 +
25600 + if (ep->type == DWC_OTG_EP_TYPE_ISOC) {
25601 + dsts_data_t dsts = {.d32 = 0};
25602 + if (ep->bInterval == 1) {
25603 + dsts.d32 =
25604 + DWC_READ_REG32(&core_if->dev_if->
25605 + dev_global_regs->dsts);
25606 + ep->frame_num = dsts.b.soffn + ep->bInterval;
25607 + if (ep->frame_num > 0x3FFF) {
25608 + ep->frm_overrun = 1;
25609 + ep->frame_num &= 0x3FFF;
25610 + } else
25611 + ep->frm_overrun = 0;
25612 +
25613 + if (ep->frame_num & 0x1) {
25614 + depctl.b.setd1pid = 1;
25615 + } else {
25616 + depctl.b.setd0pid = 1;
25617 + }
25618 + }
25619 + }
25620 +
25621 + /* EP enable */
25622 + depctl.b.cnak = 1;
25623 + depctl.b.epena = 1;
25624 +
25625 + DWC_WRITE_REG32(&out_regs->doepctl, depctl.d32);
25626 +
25627 + DWC_DEBUGPL(DBG_PCD, "DOEPCTL=%08x DOEPTSIZ=%08x\n",
25628 + DWC_READ_REG32(&out_regs->doepctl),
25629 + DWC_READ_REG32(&out_regs->doeptsiz));
25630 + DWC_DEBUGPL(DBG_PCD, "DAINTMSK=%08x GINTMSK=%08x\n",
25631 + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->
25632 + daintmsk),
25633 + DWC_READ_REG32(&core_if->core_global_regs->
25634 + gintmsk));
25635 +
25636 + /* Timer is scheduling only for out bulk transfers for
25637 + * "Device DDMA OUT NAK Enhancement" feature to inform user
25638 + * about received data payload in case of timeout
25639 + */
25640 + if (core_if->core_params->dev_out_nak) {
25641 + if (ep->type == DWC_OTG_EP_TYPE_BULK) {
25642 + core_if->ep_xfer_info[ep->num].core_if = core_if;
25643 + core_if->ep_xfer_info[ep->num].ep = ep;
25644 + core_if->ep_xfer_info[ep->num].state = 1;
25645 +
25646 + /* Start a timer for this transfer. */
25647 + DWC_TIMER_SCHEDULE(core_if->ep_xfer_timer[ep->num], 10000);
25648 + }
25649 + }
25650 + }
25651 +}
25652 +
25653 +/**
25654 + * This function setup a zero length transfer in Buffer DMA and
25655 + * Slave modes for usb requests with zero field set
25656 + *
25657 + * @param core_if Programming view of DWC_otg controller.
25658 + * @param ep The EP to start the transfer on.
25659 + *
25660 + */
25661 +void dwc_otg_ep_start_zl_transfer(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
25662 +{
25663 +
25664 + depctl_data_t depctl;
25665 + deptsiz_data_t deptsiz;
25666 + gintmsk_data_t intr_mask = {.d32 = 0 };
25667 +
25668 + DWC_DEBUGPL((DBG_PCDV | DBG_CILV), "%s()\n", __func__);
25669 + DWC_PRINTF("zero length transfer is called\n");
25670 +
25671 + /* IN endpoint */
25672 + if (ep->is_in == 1) {
25673 + dwc_otg_dev_in_ep_regs_t *in_regs =
25674 + core_if->dev_if->in_ep_regs[ep->num];
25675 +
25676 + depctl.d32 = DWC_READ_REG32(&(in_regs->diepctl));
25677 + deptsiz.d32 = DWC_READ_REG32(&(in_regs->dieptsiz));
25678 +
25679 + deptsiz.b.xfersize = 0;
25680 + deptsiz.b.pktcnt = 1;
25681 +
25682 + /* Write the DMA register */
25683 + if (core_if->dma_enable) {
25684 + if (core_if->dma_desc_enable == 0) {
25685 + deptsiz.b.mc = 1;
25686 + DWC_WRITE_REG32(&in_regs->dieptsiz,
25687 + deptsiz.d32);
25688 + DWC_WRITE_REG32(&(in_regs->diepdma),
25689 + (uint32_t) ep->dma_addr);
25690 + }
25691 + } else {
25692 + DWC_WRITE_REG32(&in_regs->dieptsiz, deptsiz.d32);
25693 + /**
25694 + * Enable the Non-Periodic Tx FIFO empty interrupt,
25695 + * or the Tx FIFO epmty interrupt in dedicated Tx FIFO mode,
25696 + * the data will be written into the fifo by the ISR.
25697 + */
25698 + if (core_if->en_multiple_tx_fifo == 0) {
25699 + intr_mask.b.nptxfempty = 1;
25700 + DWC_MODIFY_REG32(&core_if->
25701 + core_global_regs->gintmsk,
25702 + intr_mask.d32, intr_mask.d32);
25703 + } else {
25704 + /* Enable the Tx FIFO Empty Interrupt for this EP */
25705 + if (ep->xfer_len > 0) {
25706 + uint32_t fifoemptymsk = 0;
25707 + fifoemptymsk = 1 << ep->num;
25708 + DWC_MODIFY_REG32(&core_if->
25709 + dev_if->dev_global_regs->dtknqr4_fifoemptymsk,
25710 + 0, fifoemptymsk);
25711 + }
25712 + }
25713 + }
25714 +
25715 + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable)
25716 + depctl.b.nextep = core_if->nextep_seq[ep->num];
25717 + /* EP enable, IN data in FIFO */
25718 + depctl.b.cnak = 1;
25719 + depctl.b.epena = 1;
25720 + DWC_WRITE_REG32(&in_regs->diepctl, depctl.d32);
25721 +
25722 + } else {
25723 + /* OUT endpoint */
25724 + dwc_otg_dev_out_ep_regs_t *out_regs =
25725 + core_if->dev_if->out_ep_regs[ep->num];
25726 +
25727 + depctl.d32 = DWC_READ_REG32(&(out_regs->doepctl));
25728 + deptsiz.d32 = DWC_READ_REG32(&(out_regs->doeptsiz));
25729 +
25730 + /* Zero Length Packet */
25731 + deptsiz.b.xfersize = ep->maxpacket;
25732 + deptsiz.b.pktcnt = 1;
25733 +
25734 + if (core_if->dma_enable) {
25735 + if (!core_if->dma_desc_enable) {
25736 + DWC_WRITE_REG32(&out_regs->doeptsiz,
25737 + deptsiz.d32);
25738 +
25739 + DWC_WRITE_REG32(&(out_regs->doepdma),
25740 + (uint32_t) ep->dma_addr);
25741 + }
25742 + } else {
25743 + DWC_WRITE_REG32(&out_regs->doeptsiz, deptsiz.d32);
25744 + }
25745 +
25746 + /* EP enable */
25747 + depctl.b.cnak = 1;
25748 + depctl.b.epena = 1;
25749 +
25750 + DWC_WRITE_REG32(&out_regs->doepctl, depctl.d32);
25751 +
25752 + }
25753 +}
25754 +
25755 +/**
25756 + * This function does the setup for a data transfer for EP0 and starts
25757 + * the transfer. For an IN transfer, the packets will be loaded into
25758 + * the appropriate Tx FIFO in the ISR. For OUT transfers, the packets are
25759 + * unloaded from the Rx FIFO in the ISR.
25760 + *
25761 + * @param core_if Programming view of DWC_otg controller.
25762 + * @param ep The EP0 data.
25763 + */
25764 +void dwc_otg_ep0_start_transfer(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
25765 +{
25766 + depctl_data_t depctl;
25767 + deptsiz0_data_t deptsiz;
25768 + gintmsk_data_t intr_mask = {.d32 = 0 };
25769 + dwc_otg_dev_dma_desc_t *dma_desc;
25770 +
25771 + DWC_DEBUGPL(DBG_PCD, "ep%d-%s xfer_len=%d xfer_cnt=%d "
25772 + "xfer_buff=%p start_xfer_buff=%p \n",
25773 + ep->num, (ep->is_in ? "IN" : "OUT"), ep->xfer_len,
25774 + ep->xfer_count, ep->xfer_buff, ep->start_xfer_buff);
25775 +
25776 + ep->total_len = ep->xfer_len;
25777 +
25778 + /* IN endpoint */
25779 + if (ep->is_in == 1) {
25780 + dwc_otg_dev_in_ep_regs_t *in_regs =
25781 + core_if->dev_if->in_ep_regs[0];
25782 +
25783 + gnptxsts_data_t gtxstatus;
25784 +
25785 + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
25786 + depctl.d32 = DWC_READ_REG32(&in_regs->diepctl);
25787 + if (depctl.b.epena)
25788 + return;
25789 + }
25790 +
25791 + gtxstatus.d32 =
25792 + DWC_READ_REG32(&core_if->core_global_regs->gnptxsts);
25793 +
25794 + /* If dedicated FIFO every time flush fifo before enable ep*/
25795 + if (core_if->en_multiple_tx_fifo && core_if->snpsid >= OTG_CORE_REV_3_00a)
25796 + dwc_otg_flush_tx_fifo(core_if, ep->tx_fifo_num);
25797 +
25798 + if (core_if->en_multiple_tx_fifo == 0
25799 + && gtxstatus.b.nptxqspcavail == 0
25800 + && !core_if->dma_enable) {
25801 +#ifdef DEBUG
25802 + deptsiz.d32 = DWC_READ_REG32(&in_regs->dieptsiz);
25803 + DWC_DEBUGPL(DBG_PCD, "DIEPCTL0=%0x\n",
25804 + DWC_READ_REG32(&in_regs->diepctl));
25805 + DWC_DEBUGPL(DBG_PCD, "DIEPTSIZ0=%0x (sz=%d, pcnt=%d)\n",
25806 + deptsiz.d32,
25807 + deptsiz.b.xfersize, deptsiz.b.pktcnt);
25808 + DWC_PRINTF("TX Queue or FIFO Full (0x%0x)\n",
25809 + gtxstatus.d32);
25810 +#endif
25811 + return;
25812 + }
25813 +
25814 + depctl.d32 = DWC_READ_REG32(&in_regs->diepctl);
25815 + deptsiz.d32 = DWC_READ_REG32(&in_regs->dieptsiz);
25816 +
25817 + /* Zero Length Packet? */
25818 + if (ep->xfer_len == 0) {
25819 + deptsiz.b.xfersize = 0;
25820 + deptsiz.b.pktcnt = 1;
25821 + } else {
25822 + /* Program the transfer size and packet count
25823 + * as follows: xfersize = N * maxpacket +
25824 + * short_packet pktcnt = N + (short_packet
25825 + * exist ? 1 : 0)
25826 + */
25827 + if (ep->xfer_len > ep->maxpacket) {
25828 + ep->xfer_len = ep->maxpacket;
25829 + deptsiz.b.xfersize = ep->maxpacket;
25830 + } else {
25831 + deptsiz.b.xfersize = ep->xfer_len;
25832 + }
25833 + deptsiz.b.pktcnt = 1;
25834 +
25835 + }
25836 + DWC_DEBUGPL(DBG_PCDV,
25837 + "IN len=%d xfersize=%d pktcnt=%d [%08x]\n",
25838 + ep->xfer_len, deptsiz.b.xfersize, deptsiz.b.pktcnt,
25839 + deptsiz.d32);
25840 +
25841 + /* Write the DMA register */
25842 + if (core_if->dma_enable) {
25843 + if (core_if->dma_desc_enable == 0) {
25844 + DWC_WRITE_REG32(&in_regs->dieptsiz,
25845 + deptsiz.d32);
25846 +
25847 + DWC_WRITE_REG32(&(in_regs->diepdma),
25848 + (uint32_t) ep->dma_addr);
25849 + } else {
25850 + dma_desc = core_if->dev_if->in_desc_addr;
25851 +
25852 + /** DMA Descriptor Setup */
25853 + dma_desc->status.b.bs = BS_HOST_BUSY;
25854 + dma_desc->status.b.l = 1;
25855 + dma_desc->status.b.ioc = 1;
25856 + dma_desc->status.b.sp =
25857 + (ep->xfer_len == ep->maxpacket) ? 0 : 1;
25858 + dma_desc->status.b.bytes = ep->xfer_len;
25859 + dma_desc->buf = ep->dma_addr;
25860 + dma_desc->status.b.sts = 0;
25861 + dma_desc->status.b.bs = BS_HOST_READY;
25862 +
25863 + /** DIEPDMA0 Register write */
25864 + DWC_WRITE_REG32(&in_regs->diepdma,
25865 + core_if->
25866 + dev_if->dma_in_desc_addr);
25867 + }
25868 + } else {
25869 + DWC_WRITE_REG32(&in_regs->dieptsiz, deptsiz.d32);
25870 + }
25871 +
25872 + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable)
25873 + depctl.b.nextep = core_if->nextep_seq[ep->num];
25874 + /* EP enable, IN data in FIFO */
25875 + depctl.b.cnak = 1;
25876 + depctl.b.epena = 1;
25877 + DWC_WRITE_REG32(&in_regs->diepctl, depctl.d32);
25878 +
25879 + /**
25880 + * Enable the Non-Periodic Tx FIFO empty interrupt, the
25881 + * data will be written into the fifo by the ISR.
25882 + */
25883 + if (!core_if->dma_enable) {
25884 + if (core_if->en_multiple_tx_fifo == 0) {
25885 + intr_mask.b.nptxfempty = 1;
25886 + DWC_MODIFY_REG32(&core_if->
25887 + core_global_regs->gintmsk,
25888 + intr_mask.d32, intr_mask.d32);
25889 + } else {
25890 + /* Enable the Tx FIFO Empty Interrupt for this EP */
25891 + if (ep->xfer_len > 0) {
25892 + uint32_t fifoemptymsk = 0;
25893 + fifoemptymsk |= 1 << ep->num;
25894 + DWC_MODIFY_REG32(&core_if->
25895 + dev_if->dev_global_regs->dtknqr4_fifoemptymsk,
25896 + 0, fifoemptymsk);
25897 + }
25898 + }
25899 + }
25900 + } else {
25901 + /* OUT endpoint */
25902 + dwc_otg_dev_out_ep_regs_t *out_regs =
25903 + core_if->dev_if->out_ep_regs[0];
25904 +
25905 + depctl.d32 = DWC_READ_REG32(&out_regs->doepctl);
25906 + deptsiz.d32 = DWC_READ_REG32(&out_regs->doeptsiz);
25907 +
25908 + /* Program the transfer size and packet count as follows:
25909 + * xfersize = N * (maxpacket + 4 - (maxpacket % 4))
25910 + * pktcnt = N */
25911 + /* Zero Length Packet */
25912 + deptsiz.b.xfersize = ep->maxpacket;
25913 + deptsiz.b.pktcnt = 1;
25914 + if (core_if->snpsid >= OTG_CORE_REV_3_00a)
25915 + deptsiz.b.supcnt = 3;
25916 +
25917 + DWC_DEBUGPL(DBG_PCDV, "len=%d xfersize=%d pktcnt=%d\n",
25918 + ep->xfer_len, deptsiz.b.xfersize, deptsiz.b.pktcnt);
25919 +
25920 + if (core_if->dma_enable) {
25921 + if (!core_if->dma_desc_enable) {
25922 + DWC_WRITE_REG32(&out_regs->doeptsiz,
25923 + deptsiz.d32);
25924 +
25925 + DWC_WRITE_REG32(&(out_regs->doepdma),
25926 + (uint32_t) ep->dma_addr);
25927 + } else {
25928 + dma_desc = core_if->dev_if->out_desc_addr;
25929 +
25930 + /** DMA Descriptor Setup */
25931 + dma_desc->status.b.bs = BS_HOST_BUSY;
25932 + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
25933 + dma_desc->status.b.mtrf = 0;
25934 + dma_desc->status.b.sr = 0;
25935 + }
25936 + dma_desc->status.b.l = 1;
25937 + dma_desc->status.b.ioc = 1;
25938 + dma_desc->status.b.bytes = ep->maxpacket;
25939 + dma_desc->buf = ep->dma_addr;
25940 + dma_desc->status.b.sts = 0;
25941 + dma_desc->status.b.bs = BS_HOST_READY;
25942 +
25943 + /** DOEPDMA0 Register write */
25944 + DWC_WRITE_REG32(&out_regs->doepdma,
25945 + core_if->dev_if->
25946 + dma_out_desc_addr);
25947 + }
25948 + } else {
25949 + DWC_WRITE_REG32(&out_regs->doeptsiz, deptsiz.d32);
25950 + }
25951 +
25952 + /* EP enable */
25953 + depctl.b.cnak = 1;
25954 + depctl.b.epena = 1;
25955 + DWC_WRITE_REG32(&(out_regs->doepctl), depctl.d32);
25956 + }
25957 +}
25958 +
25959 +/**
25960 + * This function continues control IN transfers started by
25961 + * dwc_otg_ep0_start_transfer, when the transfer does not fit in a
25962 + * single packet. NOTE: The DIEPCTL0/DOEPCTL0 registers only have one
25963 + * bit for the packet count.
25964 + *
25965 + * @param core_if Programming view of DWC_otg controller.
25966 + * @param ep The EP0 data.
25967 + */
25968 +void dwc_otg_ep0_continue_transfer(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
25969 +{
25970 + depctl_data_t depctl;
25971 + deptsiz0_data_t deptsiz;
25972 + gintmsk_data_t intr_mask = {.d32 = 0 };
25973 + dwc_otg_dev_dma_desc_t *dma_desc;
25974 +
25975 + if (ep->is_in == 1) {
25976 + dwc_otg_dev_in_ep_regs_t *in_regs =
25977 + core_if->dev_if->in_ep_regs[0];
25978 + gnptxsts_data_t tx_status = {.d32 = 0 };
25979 +
25980 + tx_status.d32 =
25981 + DWC_READ_REG32(&core_if->core_global_regs->gnptxsts);
25982 + /** @todo Should there be check for room in the Tx
25983 + * Status Queue. If not remove the code above this comment. */
25984 +
25985 + depctl.d32 = DWC_READ_REG32(&in_regs->diepctl);
25986 + deptsiz.d32 = DWC_READ_REG32(&in_regs->dieptsiz);
25987 +
25988 + /* Program the transfer size and packet count
25989 + * as follows: xfersize = N * maxpacket +
25990 + * short_packet pktcnt = N + (short_packet
25991 + * exist ? 1 : 0)
25992 + */
25993 +
25994 + if (core_if->dma_desc_enable == 0) {
25995 + deptsiz.b.xfersize =
25996 + (ep->total_len - ep->xfer_count) >
25997 + ep->maxpacket ? ep->maxpacket : (ep->total_len -
25998 + ep->xfer_count);
25999 + deptsiz.b.pktcnt = 1;
26000 + if (core_if->dma_enable == 0) {
26001 + ep->xfer_len += deptsiz.b.xfersize;
26002 + } else {
26003 + ep->xfer_len = deptsiz.b.xfersize;
26004 + }
26005 + DWC_WRITE_REG32(&in_regs->dieptsiz, deptsiz.d32);
26006 + } else {
26007 + ep->xfer_len =
26008 + (ep->total_len - ep->xfer_count) >
26009 + ep->maxpacket ? ep->maxpacket : (ep->total_len -
26010 + ep->xfer_count);
26011 +
26012 + dma_desc = core_if->dev_if->in_desc_addr;
26013 +
26014 + /** DMA Descriptor Setup */
26015 + dma_desc->status.b.bs = BS_HOST_BUSY;
26016 + dma_desc->status.b.l = 1;
26017 + dma_desc->status.b.ioc = 1;
26018 + dma_desc->status.b.sp =
26019 + (ep->xfer_len == ep->maxpacket) ? 0 : 1;
26020 + dma_desc->status.b.bytes = ep->xfer_len;
26021 + dma_desc->buf = ep->dma_addr;
26022 + dma_desc->status.b.sts = 0;
26023 + dma_desc->status.b.bs = BS_HOST_READY;
26024 +
26025 + /** DIEPDMA0 Register write */
26026 + DWC_WRITE_REG32(&in_regs->diepdma,
26027 + core_if->dev_if->dma_in_desc_addr);
26028 + }
26029 +
26030 + DWC_DEBUGPL(DBG_PCDV,
26031 + "IN len=%d xfersize=%d pktcnt=%d [%08x]\n",
26032 + ep->xfer_len, deptsiz.b.xfersize, deptsiz.b.pktcnt,
26033 + deptsiz.d32);
26034 +
26035 + /* Write the DMA register */
26036 + if (core_if->hwcfg2.b.architecture == DWC_INT_DMA_ARCH) {
26037 + if (core_if->dma_desc_enable == 0)
26038 + DWC_WRITE_REG32(&(in_regs->diepdma),
26039 + (uint32_t) ep->dma_addr);
26040 + }
26041 + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable)
26042 + depctl.b.nextep = core_if->nextep_seq[ep->num];
26043 + /* EP enable, IN data in FIFO */
26044 + depctl.b.cnak = 1;
26045 + depctl.b.epena = 1;
26046 + DWC_WRITE_REG32(&in_regs->diepctl, depctl.d32);
26047 +
26048 + /**
26049 + * Enable the Non-Periodic Tx FIFO empty interrupt, the
26050 + * data will be written into the fifo by the ISR.
26051 + */
26052 + if (!core_if->dma_enable) {
26053 + if (core_if->en_multiple_tx_fifo == 0) {
26054 + /* First clear it from GINTSTS */
26055 + intr_mask.b.nptxfempty = 1;
26056 + DWC_MODIFY_REG32(&core_if->
26057 + core_global_regs->gintmsk,
26058 + intr_mask.d32, intr_mask.d32);
26059 +
26060 + } else {
26061 + /* Enable the Tx FIFO Empty Interrupt for this EP */
26062 + if (ep->xfer_len > 0) {
26063 + uint32_t fifoemptymsk = 0;
26064 + fifoemptymsk |= 1 << ep->num;
26065 + DWC_MODIFY_REG32(&core_if->
26066 + dev_if->dev_global_regs->dtknqr4_fifoemptymsk,
26067 + 0, fifoemptymsk);
26068 + }
26069 + }
26070 + }
26071 + } else {
26072 + dwc_otg_dev_out_ep_regs_t *out_regs =
26073 + core_if->dev_if->out_ep_regs[0];
26074 +
26075 + depctl.d32 = DWC_READ_REG32(&out_regs->doepctl);
26076 + deptsiz.d32 = DWC_READ_REG32(&out_regs->doeptsiz);
26077 +
26078 + /* Program the transfer size and packet count
26079 + * as follows: xfersize = N * maxpacket +
26080 + * short_packet pktcnt = N + (short_packet
26081 + * exist ? 1 : 0)
26082 + */
26083 + deptsiz.b.xfersize = ep->maxpacket;
26084 + deptsiz.b.pktcnt = 1;
26085 +
26086 + if (core_if->dma_desc_enable == 0) {
26087 + DWC_WRITE_REG32(&out_regs->doeptsiz, deptsiz.d32);
26088 + } else {
26089 + dma_desc = core_if->dev_if->out_desc_addr;
26090 +
26091 + /** DMA Descriptor Setup */
26092 + dma_desc->status.b.bs = BS_HOST_BUSY;
26093 + dma_desc->status.b.l = 1;
26094 + dma_desc->status.b.ioc = 1;
26095 + dma_desc->status.b.bytes = ep->maxpacket;
26096 + dma_desc->buf = ep->dma_addr;
26097 + dma_desc->status.b.sts = 0;
26098 + dma_desc->status.b.bs = BS_HOST_READY;
26099 +
26100 + /** DOEPDMA0 Register write */
26101 + DWC_WRITE_REG32(&out_regs->doepdma,
26102 + core_if->dev_if->dma_out_desc_addr);
26103 + }
26104 +
26105 + DWC_DEBUGPL(DBG_PCDV,
26106 + "IN len=%d xfersize=%d pktcnt=%d [%08x]\n",
26107 + ep->xfer_len, deptsiz.b.xfersize, deptsiz.b.pktcnt,
26108 + deptsiz.d32);
26109 +
26110 + /* Write the DMA register */
26111 + if (core_if->hwcfg2.b.architecture == DWC_INT_DMA_ARCH) {
26112 + if (core_if->dma_desc_enable == 0)
26113 + DWC_WRITE_REG32(&(out_regs->doepdma),
26114 + (uint32_t) ep->dma_addr);
26115 +
26116 + }
26117 +
26118 + /* EP enable, IN data in FIFO */
26119 + depctl.b.cnak = 1;
26120 + depctl.b.epena = 1;
26121 + DWC_WRITE_REG32(&out_regs->doepctl, depctl.d32);
26122 +
26123 + }
26124 +}
26125 +
26126 +#ifdef DEBUG
26127 +void dump_msg(const u8 * buf, unsigned int length)
26128 +{
26129 + unsigned int start, num, i;
26130 + char line[52], *p;
26131 +
26132 + if (length >= 512)
26133 + return;
26134 + start = 0;
26135 + while (length > 0) {
26136 + num = length < 16u ? length : 16u;
26137 + p = line;
26138 + for (i = 0; i < num; ++i) {
26139 + if (i == 8)
26140 + *p++ = ' ';
26141 + DWC_SPRINTF(p, " %02x", buf[i]);
26142 + p += 3;
26143 + }
26144 + *p = 0;
26145 + DWC_PRINTF("%6x: %s\n", start, line);
26146 + buf += num;
26147 + start += num;
26148 + length -= num;
26149 + }
26150 +}
26151 +#else
26152 +static inline void dump_msg(const u8 * buf, unsigned int length)
26153 +{
26154 +}
26155 +#endif
26156 +
26157 +/**
26158 + * This function writes a packet into the Tx FIFO associated with the
26159 + * EP. For non-periodic EPs the non-periodic Tx FIFO is written. For
26160 + * periodic EPs the periodic Tx FIFO associated with the EP is written
26161 + * with all packets for the next micro-frame.
26162 + *
26163 + * @param core_if Programming view of DWC_otg controller.
26164 + * @param ep The EP to write packet for.
26165 + * @param dma Indicates if DMA is being used.
26166 + */
26167 +void dwc_otg_ep_write_packet(dwc_otg_core_if_t * core_if, dwc_ep_t * ep,
26168 + int dma)
26169 +{
26170 + /**
26171 + * The buffer is padded to DWORD on a per packet basis in
26172 + * slave/dma mode if the MPS is not DWORD aligned. The last
26173 + * packet, if short, is also padded to a multiple of DWORD.
26174 + *
26175 + * ep->xfer_buff always starts DWORD aligned in memory and is a
26176 + * multiple of DWORD in length
26177 + *
26178 + * ep->xfer_len can be any number of bytes
26179 + *
26180 + * ep->xfer_count is a multiple of ep->maxpacket until the last
26181 + * packet
26182 + *
26183 + * FIFO access is DWORD */
26184 +
26185 + uint32_t i;
26186 + uint32_t byte_count;
26187 + uint32_t dword_count;
26188 + uint32_t *fifo;
26189 + uint32_t *data_buff = (uint32_t *) ep->xfer_buff;
26190 +
26191 + DWC_DEBUGPL((DBG_PCDV | DBG_CILV), "%s(%p,%p)\n", __func__, core_if,
26192 + ep);
26193 + if (ep->xfer_count >= ep->xfer_len) {
26194 + DWC_WARN("%s() No data for EP%d!!!\n", __func__, ep->num);
26195 + return;
26196 + }
26197 +
26198 + /* Find the byte length of the packet either short packet or MPS */
26199 + if ((ep->xfer_len - ep->xfer_count) < ep->maxpacket) {
26200 + byte_count = ep->xfer_len - ep->xfer_count;
26201 + } else {
26202 + byte_count = ep->maxpacket;
26203 + }
26204 +
26205 + /* Find the DWORD length, padded by extra bytes as neccessary if MPS
26206 + * is not a multiple of DWORD */
26207 + dword_count = (byte_count + 3) / 4;
26208 +
26209 +#ifdef VERBOSE
26210 + dump_msg(ep->xfer_buff, byte_count);
26211 +#endif
26212 +
26213 + /**@todo NGS Where are the Periodic Tx FIFO addresses
26214 + * intialized? What should this be? */
26215 +
26216 + fifo = core_if->data_fifo[ep->num];
26217 +
26218 + DWC_DEBUGPL((DBG_PCDV | DBG_CILV), "fifo=%p buff=%p *p=%08x bc=%d\n",
26219 + fifo, data_buff, *data_buff, byte_count);
26220 +
26221 + if (!dma) {
26222 + for (i = 0; i < dword_count; i++, data_buff++) {
26223 + DWC_WRITE_REG32(fifo, *data_buff);
26224 + }
26225 + }
26226 +
26227 + ep->xfer_count += byte_count;
26228 + ep->xfer_buff += byte_count;
26229 + ep->dma_addr += byte_count;
26230 +}
26231 +
26232 +/**
26233 + * Set the EP STALL.
26234 + *
26235 + * @param core_if Programming view of DWC_otg controller.
26236 + * @param ep The EP to set the stall on.
26237 + */
26238 +void dwc_otg_ep_set_stall(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
26239 +{
26240 + depctl_data_t depctl;
26241 + volatile uint32_t *depctl_addr;
26242 +
26243 + DWC_DEBUGPL(DBG_PCD, "%s ep%d-%s\n", __func__, ep->num,
26244 + (ep->is_in ? "IN" : "OUT"));
26245 +
26246 + if (ep->is_in == 1) {
26247 + depctl_addr = &(core_if->dev_if->in_ep_regs[ep->num]->diepctl);
26248 + depctl.d32 = DWC_READ_REG32(depctl_addr);
26249 +
26250 + /* set the disable and stall bits */
26251 + if (depctl.b.epena) {
26252 + depctl.b.epdis = 1;
26253 + }
26254 + depctl.b.stall = 1;
26255 + DWC_WRITE_REG32(depctl_addr, depctl.d32);
26256 + } else {
26257 + depctl_addr = &(core_if->dev_if->out_ep_regs[ep->num]->doepctl);
26258 + depctl.d32 = DWC_READ_REG32(depctl_addr);
26259 +
26260 + /* set the stall bit */
26261 + depctl.b.stall = 1;
26262 + DWC_WRITE_REG32(depctl_addr, depctl.d32);
26263 + }
26264 +
26265 + DWC_DEBUGPL(DBG_PCD, "DEPCTL=%0x\n", DWC_READ_REG32(depctl_addr));
26266 +
26267 + return;
26268 +}
26269 +
26270 +/**
26271 + * Clear the EP STALL.
26272 + *
26273 + * @param core_if Programming view of DWC_otg controller.
26274 + * @param ep The EP to clear stall from.
26275 + */
26276 +void dwc_otg_ep_clear_stall(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
26277 +{
26278 + depctl_data_t depctl;
26279 + volatile uint32_t *depctl_addr;
26280 +
26281 + DWC_DEBUGPL(DBG_PCD, "%s ep%d-%s\n", __func__, ep->num,
26282 + (ep->is_in ? "IN" : "OUT"));
26283 +
26284 + if (ep->is_in == 1) {
26285 + depctl_addr = &(core_if->dev_if->in_ep_regs[ep->num]->diepctl);
26286 + } else {
26287 + depctl_addr = &(core_if->dev_if->out_ep_regs[ep->num]->doepctl);
26288 + }
26289 +
26290 + depctl.d32 = DWC_READ_REG32(depctl_addr);
26291 +
26292 + /* clear the stall bits */
26293 + depctl.b.stall = 0;
26294 +
26295 + /*
26296 + * USB Spec 9.4.5: For endpoints using data toggle, regardless
26297 + * of whether an endpoint has the Halt feature set, a
26298 + * ClearFeature(ENDPOINT_HALT) request always results in the
26299 + * data toggle being reinitialized to DATA0.
26300 + */
26301 + if (ep->type == DWC_OTG_EP_TYPE_INTR ||
26302 + ep->type == DWC_OTG_EP_TYPE_BULK) {
26303 + depctl.b.setd0pid = 1; /* DATA0 */
26304 + }
26305 +
26306 + DWC_WRITE_REG32(depctl_addr, depctl.d32);
26307 + DWC_DEBUGPL(DBG_PCD, "DEPCTL=%0x\n", DWC_READ_REG32(depctl_addr));
26308 + return;
26309 +}
26310 +
26311 +/**
26312 + * This function reads a packet from the Rx FIFO into the destination
26313 + * buffer. To read SETUP data use dwc_otg_read_setup_packet.
26314 + *
26315 + * @param core_if Programming view of DWC_otg controller.
26316 + * @param dest Destination buffer for the packet.
26317 + * @param bytes Number of bytes to copy to the destination.
26318 + */
26319 +void dwc_otg_read_packet(dwc_otg_core_if_t * core_if,
26320 + uint8_t * dest, uint16_t bytes)
26321 +{
26322 + int i;
26323 + int word_count = (bytes + 3) / 4;
26324 +
26325 + volatile uint32_t *fifo = core_if->data_fifo[0];
26326 + uint32_t *data_buff = (uint32_t *) dest;
26327 +
26328 + /**
26329 + * @todo Account for the case where _dest is not dword aligned. This
26330 + * requires reading data from the FIFO into a uint32_t temp buffer,
26331 + * then moving it into the data buffer.
26332 + */
26333 +
26334 + DWC_DEBUGPL((DBG_PCDV | DBG_CILV), "%s(%p,%p,%d)\n", __func__,
26335 + core_if, dest, bytes);
26336 +
26337 + for (i = 0; i < word_count; i++, data_buff++) {
26338 + *data_buff = DWC_READ_REG32(fifo);
26339 + }
26340 +
26341 + return;
26342 +}
26343 +
26344 +/**
26345 + * This functions reads the device registers and prints them
26346 + *
26347 + * @param core_if Programming view of DWC_otg controller.
26348 + */
26349 +void dwc_otg_dump_dev_registers(dwc_otg_core_if_t * core_if)
26350 +{
26351 + int i;
26352 + volatile uint32_t *addr;
26353 +
26354 + DWC_PRINTF("Device Global Registers\n");
26355 + addr = &core_if->dev_if->dev_global_regs->dcfg;
26356 + DWC_PRINTF("DCFG @0x%08lX : 0x%08X\n",
26357 + (unsigned long)addr, DWC_READ_REG32(addr));
26358 + addr = &core_if->dev_if->dev_global_regs->dctl;
26359 + DWC_PRINTF("DCTL @0x%08lX : 0x%08X\n",
26360 + (unsigned long)addr, DWC_READ_REG32(addr));
26361 + addr = &core_if->dev_if->dev_global_regs->dsts;
26362 + DWC_PRINTF("DSTS @0x%08lX : 0x%08X\n",
26363 + (unsigned long)addr, DWC_READ_REG32(addr));
26364 + addr = &core_if->dev_if->dev_global_regs->diepmsk;
26365 + DWC_PRINTF("DIEPMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
26366 + DWC_READ_REG32(addr));
26367 + addr = &core_if->dev_if->dev_global_regs->doepmsk;
26368 + DWC_PRINTF("DOEPMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
26369 + DWC_READ_REG32(addr));
26370 + addr = &core_if->dev_if->dev_global_regs->daint;
26371 + DWC_PRINTF("DAINT @0x%08lX : 0x%08X\n", (unsigned long)addr,
26372 + DWC_READ_REG32(addr));
26373 + addr = &core_if->dev_if->dev_global_regs->daintmsk;
26374 + DWC_PRINTF("DAINTMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
26375 + DWC_READ_REG32(addr));
26376 + addr = &core_if->dev_if->dev_global_regs->dtknqr1;
26377 + DWC_PRINTF("DTKNQR1 @0x%08lX : 0x%08X\n", (unsigned long)addr,
26378 + DWC_READ_REG32(addr));
26379 + if (core_if->hwcfg2.b.dev_token_q_depth > 6) {
26380 + addr = &core_if->dev_if->dev_global_regs->dtknqr2;
26381 + DWC_PRINTF("DTKNQR2 @0x%08lX : 0x%08X\n",
26382 + (unsigned long)addr, DWC_READ_REG32(addr));
26383 + }
26384 +
26385 + addr = &core_if->dev_if->dev_global_regs->dvbusdis;
26386 + DWC_PRINTF("DVBUSID @0x%08lX : 0x%08X\n", (unsigned long)addr,
26387 + DWC_READ_REG32(addr));
26388 +
26389 + addr = &core_if->dev_if->dev_global_regs->dvbuspulse;
26390 + DWC_PRINTF("DVBUSPULSE @0x%08lX : 0x%08X\n",
26391 + (unsigned long)addr, DWC_READ_REG32(addr));
26392 +
26393 + addr = &core_if->dev_if->dev_global_regs->dtknqr3_dthrctl;
26394 + DWC_PRINTF("DTKNQR3_DTHRCTL @0x%08lX : 0x%08X\n",
26395 + (unsigned long)addr, DWC_READ_REG32(addr));
26396 +
26397 + if (core_if->hwcfg2.b.dev_token_q_depth > 22) {
26398 + addr = &core_if->dev_if->dev_global_regs->dtknqr4_fifoemptymsk;
26399 + DWC_PRINTF("DTKNQR4 @0x%08lX : 0x%08X\n",
26400 + (unsigned long)addr, DWC_READ_REG32(addr));
26401 + }
26402 +
26403 + addr = &core_if->dev_if->dev_global_regs->dtknqr4_fifoemptymsk;
26404 + DWC_PRINTF("FIFOEMPMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
26405 + DWC_READ_REG32(addr));
26406 +
26407 + if (core_if->hwcfg2.b.multi_proc_int) {
26408 +
26409 + addr = &core_if->dev_if->dev_global_regs->deachint;
26410 + DWC_PRINTF("DEACHINT @0x%08lX : 0x%08X\n",
26411 + (unsigned long)addr, DWC_READ_REG32(addr));
26412 + addr = &core_if->dev_if->dev_global_regs->deachintmsk;
26413 + DWC_PRINTF("DEACHINTMSK @0x%08lX : 0x%08X\n",
26414 + (unsigned long)addr, DWC_READ_REG32(addr));
26415 +
26416 + for (i = 0; i <= core_if->dev_if->num_in_eps; i++) {
26417 + addr =
26418 + &core_if->dev_if->
26419 + dev_global_regs->diepeachintmsk[i];
26420 + DWC_PRINTF("DIEPEACHINTMSK[%d] @0x%08lX : 0x%08X\n",
26421 + i, (unsigned long)addr,
26422 + DWC_READ_REG32(addr));
26423 + }
26424 +
26425 + for (i = 0; i <= core_if->dev_if->num_out_eps; i++) {
26426 + addr =
26427 + &core_if->dev_if->
26428 + dev_global_regs->doepeachintmsk[i];
26429 + DWC_PRINTF("DOEPEACHINTMSK[%d] @0x%08lX : 0x%08X\n",
26430 + i, (unsigned long)addr,
26431 + DWC_READ_REG32(addr));
26432 + }
26433 + }
26434 +
26435 + for (i = 0; i <= core_if->dev_if->num_in_eps; i++) {
26436 + DWC_PRINTF("Device IN EP %d Registers\n", i);
26437 + addr = &core_if->dev_if->in_ep_regs[i]->diepctl;
26438 + DWC_PRINTF("DIEPCTL @0x%08lX : 0x%08X\n",
26439 + (unsigned long)addr, DWC_READ_REG32(addr));
26440 + addr = &core_if->dev_if->in_ep_regs[i]->diepint;
26441 + DWC_PRINTF("DIEPINT @0x%08lX : 0x%08X\n",
26442 + (unsigned long)addr, DWC_READ_REG32(addr));
26443 + addr = &core_if->dev_if->in_ep_regs[i]->dieptsiz;
26444 + DWC_PRINTF("DIETSIZ @0x%08lX : 0x%08X\n",
26445 + (unsigned long)addr, DWC_READ_REG32(addr));
26446 + addr = &core_if->dev_if->in_ep_regs[i]->diepdma;
26447 + DWC_PRINTF("DIEPDMA @0x%08lX : 0x%08X\n",
26448 + (unsigned long)addr, DWC_READ_REG32(addr));
26449 + addr = &core_if->dev_if->in_ep_regs[i]->dtxfsts;
26450 + DWC_PRINTF("DTXFSTS @0x%08lX : 0x%08X\n",
26451 + (unsigned long)addr, DWC_READ_REG32(addr));
26452 + addr = &core_if->dev_if->in_ep_regs[i]->diepdmab;
26453 + DWC_PRINTF("DIEPDMAB @0x%08lX : 0x%08X\n",
26454 + (unsigned long)addr, 0 /*DWC_READ_REG32(addr) */ );
26455 + }
26456 +
26457 + for (i = 0; i <= core_if->dev_if->num_out_eps; i++) {
26458 + DWC_PRINTF("Device OUT EP %d Registers\n", i);
26459 + addr = &core_if->dev_if->out_ep_regs[i]->doepctl;
26460 + DWC_PRINTF("DOEPCTL @0x%08lX : 0x%08X\n",
26461 + (unsigned long)addr, DWC_READ_REG32(addr));
26462 + addr = &core_if->dev_if->out_ep_regs[i]->doepint;
26463 + DWC_PRINTF("DOEPINT @0x%08lX : 0x%08X\n",
26464 + (unsigned long)addr, DWC_READ_REG32(addr));
26465 + addr = &core_if->dev_if->out_ep_regs[i]->doeptsiz;
26466 + DWC_PRINTF("DOETSIZ @0x%08lX : 0x%08X\n",
26467 + (unsigned long)addr, DWC_READ_REG32(addr));
26468 + addr = &core_if->dev_if->out_ep_regs[i]->doepdma;
26469 + DWC_PRINTF("DOEPDMA @0x%08lX : 0x%08X\n",
26470 + (unsigned long)addr, DWC_READ_REG32(addr));
26471 + if (core_if->dma_enable) { /* Don't access this register in SLAVE mode */
26472 + addr = &core_if->dev_if->out_ep_regs[i]->doepdmab;
26473 + DWC_PRINTF("DOEPDMAB @0x%08lX : 0x%08X\n",
26474 + (unsigned long)addr, DWC_READ_REG32(addr));
26475 + }
26476 +
26477 + }
26478 +}
26479 +
26480 +/**
26481 + * This functions reads the SPRAM and prints its content
26482 + *
26483 + * @param core_if Programming view of DWC_otg controller.
26484 + */
26485 +void dwc_otg_dump_spram(dwc_otg_core_if_t * core_if)
26486 +{
26487 + volatile uint8_t *addr, *start_addr, *end_addr;
26488 +
26489 + DWC_PRINTF("SPRAM Data:\n");
26490 + start_addr = (void *)core_if->core_global_regs;
26491 + DWC_PRINTF("Base Address: 0x%8lX\n", (unsigned long)start_addr);
26492 + start_addr += 0x00028000;
26493 + end_addr = (void *)core_if->core_global_regs;
26494 + end_addr += 0x000280e0;
26495 +
26496 + for (addr = start_addr; addr < end_addr; addr += 16) {
26497 + DWC_PRINTF
26498 + ("0x%8lX:\t%2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X\n",
26499 + (unsigned long)addr, addr[0], addr[1], addr[2], addr[3],
26500 + addr[4], addr[5], addr[6], addr[7], addr[8], addr[9],
26501 + addr[10], addr[11], addr[12], addr[13], addr[14], addr[15]
26502 + );
26503 + }
26504 +
26505 + return;
26506 +}
26507 +
26508 +/**
26509 + * This function reads the host registers and prints them
26510 + *
26511 + * @param core_if Programming view of DWC_otg controller.
26512 + */
26513 +void dwc_otg_dump_host_registers(dwc_otg_core_if_t * core_if)
26514 +{
26515 + int i;
26516 + volatile uint32_t *addr;
26517 +
26518 + DWC_PRINTF("Host Global Registers\n");
26519 + addr = &core_if->host_if->host_global_regs->hcfg;
26520 + DWC_PRINTF("HCFG @0x%08lX : 0x%08X\n",
26521 + (unsigned long)addr, DWC_READ_REG32(addr));
26522 + addr = &core_if->host_if->host_global_regs->hfir;
26523 + DWC_PRINTF("HFIR @0x%08lX : 0x%08X\n",
26524 + (unsigned long)addr, DWC_READ_REG32(addr));
26525 + addr = &core_if->host_if->host_global_regs->hfnum;
26526 + DWC_PRINTF("HFNUM @0x%08lX : 0x%08X\n", (unsigned long)addr,
26527 + DWC_READ_REG32(addr));
26528 + addr = &core_if->host_if->host_global_regs->hptxsts;
26529 + DWC_PRINTF("HPTXSTS @0x%08lX : 0x%08X\n", (unsigned long)addr,
26530 + DWC_READ_REG32(addr));
26531 + addr = &core_if->host_if->host_global_regs->haint;
26532 + DWC_PRINTF("HAINT @0x%08lX : 0x%08X\n", (unsigned long)addr,
26533 + DWC_READ_REG32(addr));
26534 + addr = &core_if->host_if->host_global_regs->haintmsk;
26535 + DWC_PRINTF("HAINTMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
26536 + DWC_READ_REG32(addr));
26537 + if (core_if->dma_desc_enable) {
26538 + addr = &core_if->host_if->host_global_regs->hflbaddr;
26539 + DWC_PRINTF("HFLBADDR @0x%08lX : 0x%08X\n",
26540 + (unsigned long)addr, DWC_READ_REG32(addr));
26541 + }
26542 +
26543 + addr = core_if->host_if->hprt0;
26544 + DWC_PRINTF("HPRT0 @0x%08lX : 0x%08X\n", (unsigned long)addr,
26545 + DWC_READ_REG32(addr));
26546 +
26547 + for (i = 0; i < core_if->core_params->host_channels; i++) {
26548 + DWC_PRINTF("Host Channel %d Specific Registers\n", i);
26549 + addr = &core_if->host_if->hc_regs[i]->hcchar;
26550 + DWC_PRINTF("HCCHAR @0x%08lX : 0x%08X\n",
26551 + (unsigned long)addr, DWC_READ_REG32(addr));
26552 + addr = &core_if->host_if->hc_regs[i]->hcsplt;
26553 + DWC_PRINTF("HCSPLT @0x%08lX : 0x%08X\n",
26554 + (unsigned long)addr, DWC_READ_REG32(addr));
26555 + addr = &core_if->host_if->hc_regs[i]->hcint;
26556 + DWC_PRINTF("HCINT @0x%08lX : 0x%08X\n",
26557 + (unsigned long)addr, DWC_READ_REG32(addr));
26558 + addr = &core_if->host_if->hc_regs[i]->hcintmsk;
26559 + DWC_PRINTF("HCINTMSK @0x%08lX : 0x%08X\n",
26560 + (unsigned long)addr, DWC_READ_REG32(addr));
26561 + addr = &core_if->host_if->hc_regs[i]->hctsiz;
26562 + DWC_PRINTF("HCTSIZ @0x%08lX : 0x%08X\n",
26563 + (unsigned long)addr, DWC_READ_REG32(addr));
26564 + addr = &core_if->host_if->hc_regs[i]->hcdma;
26565 + DWC_PRINTF("HCDMA @0x%08lX : 0x%08X\n",
26566 + (unsigned long)addr, DWC_READ_REG32(addr));
26567 + if (core_if->dma_desc_enable) {
26568 + addr = &core_if->host_if->hc_regs[i]->hcdmab;
26569 + DWC_PRINTF("HCDMAB @0x%08lX : 0x%08X\n",
26570 + (unsigned long)addr, DWC_READ_REG32(addr));
26571 + }
26572 +
26573 + }
26574 + return;
26575 +}
26576 +
26577 +/**
26578 + * This function reads the core global registers and prints them
26579 + *
26580 + * @param core_if Programming view of DWC_otg controller.
26581 + */
26582 +void dwc_otg_dump_global_registers(dwc_otg_core_if_t * core_if)
26583 +{
26584 + int i, ep_num;
26585 + volatile uint32_t *addr;
26586 + char *txfsiz;
26587 +
26588 + DWC_PRINTF("Core Global Registers\n");
26589 + addr = &core_if->core_global_regs->gotgctl;
26590 + DWC_PRINTF("GOTGCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
26591 + DWC_READ_REG32(addr));
26592 + addr = &core_if->core_global_regs->gotgint;
26593 + DWC_PRINTF("GOTGINT @0x%08lX : 0x%08X\n", (unsigned long)addr,
26594 + DWC_READ_REG32(addr));
26595 + addr = &core_if->core_global_regs->gahbcfg;
26596 + DWC_PRINTF("GAHBCFG @0x%08lX : 0x%08X\n", (unsigned long)addr,
26597 + DWC_READ_REG32(addr));
26598 + addr = &core_if->core_global_regs->gusbcfg;
26599 + DWC_PRINTF("GUSBCFG @0x%08lX : 0x%08X\n", (unsigned long)addr,
26600 + DWC_READ_REG32(addr));
26601 + addr = &core_if->core_global_regs->grstctl;
26602 + DWC_PRINTF("GRSTCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
26603 + DWC_READ_REG32(addr));
26604 + addr = &core_if->core_global_regs->gintsts;
26605 + DWC_PRINTF("GINTSTS @0x%08lX : 0x%08X\n", (unsigned long)addr,
26606 + DWC_READ_REG32(addr));
26607 + addr = &core_if->core_global_regs->gintmsk;
26608 + DWC_PRINTF("GINTMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
26609 + DWC_READ_REG32(addr));
26610 + addr = &core_if->core_global_regs->grxstsr;
26611 + DWC_PRINTF("GRXSTSR @0x%08lX : 0x%08X\n", (unsigned long)addr,
26612 + DWC_READ_REG32(addr));
26613 + addr = &core_if->core_global_regs->grxfsiz;
26614 + DWC_PRINTF("GRXFSIZ @0x%08lX : 0x%08X\n", (unsigned long)addr,
26615 + DWC_READ_REG32(addr));
26616 + addr = &core_if->core_global_regs->gnptxfsiz;
26617 + DWC_PRINTF("GNPTXFSIZ @0x%08lX : 0x%08X\n", (unsigned long)addr,
26618 + DWC_READ_REG32(addr));
26619 + addr = &core_if->core_global_regs->gnptxsts;
26620 + DWC_PRINTF("GNPTXSTS @0x%08lX : 0x%08X\n", (unsigned long)addr,
26621 + DWC_READ_REG32(addr));
26622 + addr = &core_if->core_global_regs->gi2cctl;
26623 + DWC_PRINTF("GI2CCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
26624 + DWC_READ_REG32(addr));
26625 + addr = &core_if->core_global_regs->gpvndctl;
26626 + DWC_PRINTF("GPVNDCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
26627 + DWC_READ_REG32(addr));
26628 + addr = &core_if->core_global_regs->ggpio;
26629 + DWC_PRINTF("GGPIO @0x%08lX : 0x%08X\n", (unsigned long)addr,
26630 + DWC_READ_REG32(addr));
26631 + addr = &core_if->core_global_regs->guid;
26632 + DWC_PRINTF("GUID @0x%08lX : 0x%08X\n",
26633 + (unsigned long)addr, DWC_READ_REG32(addr));
26634 + addr = &core_if->core_global_regs->gsnpsid;
26635 + DWC_PRINTF("GSNPSID @0x%08lX : 0x%08X\n", (unsigned long)addr,
26636 + DWC_READ_REG32(addr));
26637 + addr = &core_if->core_global_regs->ghwcfg1;
26638 + DWC_PRINTF("GHWCFG1 @0x%08lX : 0x%08X\n", (unsigned long)addr,
26639 + DWC_READ_REG32(addr));
26640 + addr = &core_if->core_global_regs->ghwcfg2;
26641 + DWC_PRINTF("GHWCFG2 @0x%08lX : 0x%08X\n", (unsigned long)addr,
26642 + DWC_READ_REG32(addr));
26643 + addr = &core_if->core_global_regs->ghwcfg3;
26644 + DWC_PRINTF("GHWCFG3 @0x%08lX : 0x%08X\n", (unsigned long)addr,
26645 + DWC_READ_REG32(addr));
26646 + addr = &core_if->core_global_regs->ghwcfg4;
26647 + DWC_PRINTF("GHWCFG4 @0x%08lX : 0x%08X\n", (unsigned long)addr,
26648 + DWC_READ_REG32(addr));
26649 + addr = &core_if->core_global_regs->glpmcfg;
26650 + DWC_PRINTF("GLPMCFG @0x%08lX : 0x%08X\n", (unsigned long)addr,
26651 + DWC_READ_REG32(addr));
26652 + addr = &core_if->core_global_regs->gpwrdn;
26653 + DWC_PRINTF("GPWRDN @0x%08lX : 0x%08X\n", (unsigned long)addr,
26654 + DWC_READ_REG32(addr));
26655 + addr = &core_if->core_global_regs->gdfifocfg;
26656 + DWC_PRINTF("GDFIFOCFG @0x%08lX : 0x%08X\n", (unsigned long)addr,
26657 + DWC_READ_REG32(addr));
26658 + addr = &core_if->core_global_regs->adpctl;
26659 + DWC_PRINTF("ADPCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
26660 + dwc_otg_adp_read_reg(core_if));
26661 + addr = &core_if->core_global_regs->hptxfsiz;
26662 + DWC_PRINTF("HPTXFSIZ @0x%08lX : 0x%08X\n", (unsigned long)addr,
26663 + DWC_READ_REG32(addr));
26664 +
26665 + if (core_if->en_multiple_tx_fifo == 0) {
26666 + ep_num = core_if->hwcfg4.b.num_dev_perio_in_ep;
26667 + txfsiz = "DPTXFSIZ";
26668 + } else {
26669 + ep_num = core_if->hwcfg4.b.num_in_eps;
26670 + txfsiz = "DIENPTXF";
26671 + }
26672 + for (i = 0; i < ep_num; i++) {
26673 + addr = &core_if->core_global_regs->dtxfsiz[i];
26674 + DWC_PRINTF("%s[%d] @0x%08lX : 0x%08X\n", txfsiz, i + 1,
26675 + (unsigned long)addr, DWC_READ_REG32(addr));
26676 + }
26677 + addr = core_if->pcgcctl;
26678 + DWC_PRINTF("PCGCCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
26679 + DWC_READ_REG32(addr));
26680 +}
26681 +
26682 +/**
26683 + * Flush a Tx FIFO.
26684 + *
26685 + * @param core_if Programming view of DWC_otg controller.
26686 + * @param num Tx FIFO to flush.
26687 + */
26688 +void dwc_otg_flush_tx_fifo(dwc_otg_core_if_t * core_if, const int num)
26689 +{
26690 + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
26691 + volatile grstctl_t greset = {.d32 = 0 };
26692 + int count = 0;
26693 +
26694 + DWC_DEBUGPL((DBG_CIL | DBG_PCDV), "Flush Tx FIFO %d\n", num);
26695 +
26696 + greset.b.txfflsh = 1;
26697 + greset.b.txfnum = num;
26698 + DWC_WRITE_REG32(&global_regs->grstctl, greset.d32);
26699 +
26700 + do {
26701 + greset.d32 = DWC_READ_REG32(&global_regs->grstctl);
26702 + if (++count > 10000) {
26703 + DWC_WARN("%s() HANG! GRSTCTL=%0x GNPTXSTS=0x%08x\n",
26704 + __func__, greset.d32,
26705 + DWC_READ_REG32(&global_regs->gnptxsts));
26706 + break;
26707 + }
26708 + dwc_udelay(1);
26709 + } while (greset.b.txfflsh == 1);
26710 +
26711 + /* Wait for 3 PHY Clocks */
26712 + dwc_udelay(1);
26713 +}
26714 +
26715 +/**
26716 + * Flush Rx FIFO.
26717 + *
26718 + * @param core_if Programming view of DWC_otg controller.
26719 + */
26720 +void dwc_otg_flush_rx_fifo(dwc_otg_core_if_t * core_if)
26721 +{
26722 + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
26723 + volatile grstctl_t greset = {.d32 = 0 };
26724 + int count = 0;
26725 +
26726 + DWC_DEBUGPL((DBG_CIL | DBG_PCDV), "%s\n", __func__);
26727 + /*
26728 + *
26729 + */
26730 + greset.b.rxfflsh = 1;
26731 + DWC_WRITE_REG32(&global_regs->grstctl, greset.d32);
26732 +
26733 + do {
26734 + greset.d32 = DWC_READ_REG32(&global_regs->grstctl);
26735 + if (++count > 10000) {
26736 + DWC_WARN("%s() HANG! GRSTCTL=%0x\n", __func__,
26737 + greset.d32);
26738 + break;
26739 + }
26740 + dwc_udelay(1);
26741 + } while (greset.b.rxfflsh == 1);
26742 +
26743 + /* Wait for 3 PHY Clocks */
26744 + dwc_udelay(1);
26745 +}
26746 +
26747 +/**
26748 + * Do core a soft reset of the core. Be careful with this because it
26749 + * resets all the internal state machines of the core.
26750 + */
26751 +void dwc_otg_core_reset(dwc_otg_core_if_t * core_if)
26752 +{
26753 + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
26754 + volatile grstctl_t greset = {.d32 = 0 };
26755 + int count = 0;
26756 +
26757 + DWC_DEBUGPL(DBG_CILV, "%s\n", __func__);
26758 + /* Wait for AHB master IDLE state. */
26759 + do {
26760 + dwc_udelay(10);
26761 + greset.d32 = DWC_READ_REG32(&global_regs->grstctl);
26762 + if (++count > 100000) {
26763 + DWC_WARN("%s() HANG! AHB Idle GRSTCTL=%0x\n", __func__,
26764 + greset.d32);
26765 + return;
26766 + }
26767 + }
26768 + while (greset.b.ahbidle == 0);
26769 +
26770 + /* Core Soft Reset */
26771 + count = 0;
26772 + greset.b.csftrst = 1;
26773 + DWC_WRITE_REG32(&global_regs->grstctl, greset.d32);
26774 + do {
26775 + greset.d32 = DWC_READ_REG32(&global_regs->grstctl);
26776 + if (++count > 10000) {
26777 + DWC_WARN("%s() HANG! Soft Reset GRSTCTL=%0x\n",
26778 + __func__, greset.d32);
26779 + break;
26780 + }
26781 + dwc_udelay(1);
26782 + }
26783 + while (greset.b.csftrst == 1);
26784 +
26785 + /* Wait for 3 PHY Clocks */
26786 + dwc_mdelay(100);
26787 +}
26788 +
26789 +uint8_t dwc_otg_is_device_mode(dwc_otg_core_if_t * _core_if)
26790 +{
26791 + return (dwc_otg_mode(_core_if) != DWC_HOST_MODE);
26792 +}
26793 +
26794 +uint8_t dwc_otg_is_host_mode(dwc_otg_core_if_t * _core_if)
26795 +{
26796 + return (dwc_otg_mode(_core_if) == DWC_HOST_MODE);
26797 +}
26798 +
26799 +/**
26800 + * Register HCD callbacks. The callbacks are used to start and stop
26801 + * the HCD for interrupt processing.
26802 + *
26803 + * @param core_if Programming view of DWC_otg controller.
26804 + * @param cb the HCD callback structure.
26805 + * @param p pointer to be passed to callback function (usb_hcd*).
26806 + */
26807 +void dwc_otg_cil_register_hcd_callbacks(dwc_otg_core_if_t * core_if,
26808 + dwc_otg_cil_callbacks_t * cb, void *p)
26809 +{
26810 + core_if->hcd_cb = cb;
26811 + cb->p = p;
26812 +}
26813 +
26814 +/**
26815 + * Register PCD callbacks. The callbacks are used to start and stop
26816 + * the PCD for interrupt processing.
26817 + *
26818 + * @param core_if Programming view of DWC_otg controller.
26819 + * @param cb the PCD callback structure.
26820 + * @param p pointer to be passed to callback function (pcd*).
26821 + */
26822 +void dwc_otg_cil_register_pcd_callbacks(dwc_otg_core_if_t * core_if,
26823 + dwc_otg_cil_callbacks_t * cb, void *p)
26824 +{
26825 + core_if->pcd_cb = cb;
26826 + cb->p = p;
26827 +}
26828 +
26829 +#ifdef DWC_EN_ISOC
26830 +
26831 +/**
26832 + * This function writes isoc data per 1 (micro)frame into tx fifo
26833 + *
26834 + * @param core_if Programming view of DWC_otg controller.
26835 + * @param ep The EP to start the transfer on.
26836 + *
26837 + */
26838 +void write_isoc_frame_data(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
26839 +{
26840 + dwc_otg_dev_in_ep_regs_t *ep_regs;
26841 + dtxfsts_data_t txstatus = {.d32 = 0 };
26842 + uint32_t len = 0;
26843 + uint32_t dwords;
26844 +
26845 + ep->xfer_len = ep->data_per_frame;
26846 + ep->xfer_count = 0;
26847 +
26848 + ep_regs = core_if->dev_if->in_ep_regs[ep->num];
26849 +
26850 + len = ep->xfer_len - ep->xfer_count;
26851 +
26852 + if (len > ep->maxpacket) {
26853 + len = ep->maxpacket;
26854 + }
26855 +
26856 + dwords = (len + 3) / 4;
26857 +
26858 + /* While there is space in the queue and space in the FIFO and
26859 + * More data to tranfer, Write packets to the Tx FIFO */
26860 + txstatus.d32 =
26861 + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[ep->num]->dtxfsts);
26862 + DWC_DEBUGPL(DBG_PCDV, "b4 dtxfsts[%d]=0x%08x\n", ep->num, txstatus.d32);
26863 +
26864 + while (txstatus.b.txfspcavail > dwords &&
26865 + ep->xfer_count < ep->xfer_len && ep->xfer_len != 0) {
26866 + /* Write the FIFO */
26867 + dwc_otg_ep_write_packet(core_if, ep, 0);
26868 +
26869 + len = ep->xfer_len - ep->xfer_count;
26870 + if (len > ep->maxpacket) {
26871 + len = ep->maxpacket;
26872 + }
26873 +
26874 + dwords = (len + 3) / 4;
26875 + txstatus.d32 =
26876 + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
26877 + dtxfsts);
26878 + DWC_DEBUGPL(DBG_PCDV, "dtxfsts[%d]=0x%08x\n", ep->num,
26879 + txstatus.d32);
26880 + }
26881 +}
26882 +
26883 +/**
26884 + * This function initializes a descriptor chain for Isochronous transfer
26885 + *
26886 + * @param core_if Programming view of DWC_otg controller.
26887 + * @param ep The EP to start the transfer on.
26888 + *
26889 + */
26890 +void dwc_otg_iso_ep_start_frm_transfer(dwc_otg_core_if_t * core_if,
26891 + dwc_ep_t * ep)
26892 +{
26893 + deptsiz_data_t deptsiz = {.d32 = 0 };
26894 + depctl_data_t depctl = {.d32 = 0 };
26895 + dsts_data_t dsts = {.d32 = 0 };
26896 + volatile uint32_t *addr;
26897 +
26898 + if (ep->is_in) {
26899 + addr = &core_if->dev_if->in_ep_regs[ep->num]->diepctl;
26900 + } else {
26901 + addr = &core_if->dev_if->out_ep_regs[ep->num]->doepctl;
26902 + }
26903 +
26904 + ep->xfer_len = ep->data_per_frame;
26905 + ep->xfer_count = 0;
26906 + ep->xfer_buff = ep->cur_pkt_addr;
26907 + ep->dma_addr = ep->cur_pkt_dma_addr;
26908 +
26909 + if (ep->is_in) {
26910 + /* Program the transfer size and packet count
26911 + * as follows: xfersize = N * maxpacket +
26912 + * short_packet pktcnt = N + (short_packet
26913 + * exist ? 1 : 0)
26914 + */
26915 + deptsiz.b.xfersize = ep->xfer_len;
26916 + deptsiz.b.pktcnt =
26917 + (ep->xfer_len - 1 + ep->maxpacket) / ep->maxpacket;
26918 + deptsiz.b.mc = deptsiz.b.pktcnt;
26919 + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->dieptsiz,
26920 + deptsiz.d32);
26921 +
26922 + /* Write the DMA register */
26923 + if (core_if->dma_enable) {
26924 + DWC_WRITE_REG32(&
26925 + (core_if->dev_if->in_ep_regs[ep->num]->
26926 + diepdma), (uint32_t) ep->dma_addr);
26927 + }
26928 + } else {
26929 + deptsiz.b.pktcnt =
26930 + (ep->xfer_len + (ep->maxpacket - 1)) / ep->maxpacket;
26931 + deptsiz.b.xfersize = deptsiz.b.pktcnt * ep->maxpacket;
26932 +
26933 + DWC_WRITE_REG32(&core_if->dev_if->
26934 + out_ep_regs[ep->num]->doeptsiz, deptsiz.d32);
26935 +
26936 + if (core_if->dma_enable) {
26937 + DWC_WRITE_REG32(&
26938 + (core_if->dev_if->
26939 + out_ep_regs[ep->num]->doepdma),
26940 + (uint32_t) ep->dma_addr);
26941 + }
26942 + }
26943 +
26944 + /** Enable endpoint, clear nak */
26945 +
26946 + depctl.d32 = 0;
26947 + if (ep->bInterval == 1) {
26948 + dsts.d32 =
26949 + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
26950 + ep->next_frame = dsts.b.soffn + ep->bInterval;
26951 +
26952 + if (ep->next_frame & 0x1) {
26953 + depctl.b.setd1pid = 1;
26954 + } else {
26955 + depctl.b.setd0pid = 1;
26956 + }
26957 + } else {
26958 + ep->next_frame += ep->bInterval;
26959 +
26960 + if (ep->next_frame & 0x1) {
26961 + depctl.b.setd1pid = 1;
26962 + } else {
26963 + depctl.b.setd0pid = 1;
26964 + }
26965 + }
26966 + depctl.b.epena = 1;
26967 + depctl.b.cnak = 1;
26968 +
26969 + DWC_MODIFY_REG32(addr, 0, depctl.d32);
26970 + depctl.d32 = DWC_READ_REG32(addr);
26971 +
26972 + if (ep->is_in && core_if->dma_enable == 0) {
26973 + write_isoc_frame_data(core_if, ep);
26974 + }
26975 +
26976 +}
26977 +#endif /* DWC_EN_ISOC */
26978 +
26979 +static void dwc_otg_set_uninitialized(int32_t * p, int size)
26980 +{
26981 + int i;
26982 + for (i = 0; i < size; i++) {
26983 + p[i] = -1;
26984 + }
26985 +}
26986 +
26987 +static int dwc_otg_param_initialized(int32_t val)
26988 +{
26989 + return val != -1;
26990 +}
26991 +
26992 +static int dwc_otg_setup_params(dwc_otg_core_if_t * core_if)
26993 +{
26994 + int i;
26995 + core_if->core_params = DWC_ALLOC(sizeof(*core_if->core_params));
26996 + if (!core_if->core_params) {
26997 + return -DWC_E_NO_MEMORY;
26998 + }
26999 + dwc_otg_set_uninitialized((int32_t *) core_if->core_params,
27000 + sizeof(*core_if->core_params) /
27001 + sizeof(int32_t));
27002 + DWC_PRINTF("Setting default values for core params\n");
27003 + dwc_otg_set_param_otg_cap(core_if, dwc_param_otg_cap_default);
27004 + dwc_otg_set_param_dma_enable(core_if, dwc_param_dma_enable_default);
27005 + dwc_otg_set_param_dma_desc_enable(core_if,
27006 + dwc_param_dma_desc_enable_default);
27007 + dwc_otg_set_param_opt(core_if, dwc_param_opt_default);
27008 + dwc_otg_set_param_dma_burst_size(core_if,
27009 + dwc_param_dma_burst_size_default);
27010 + dwc_otg_set_param_host_support_fs_ls_low_power(core_if,
27011 + dwc_param_host_support_fs_ls_low_power_default);
27012 + dwc_otg_set_param_enable_dynamic_fifo(core_if,
27013 + dwc_param_enable_dynamic_fifo_default);
27014 + dwc_otg_set_param_data_fifo_size(core_if,
27015 + dwc_param_data_fifo_size_default);
27016 + dwc_otg_set_param_dev_rx_fifo_size(core_if,
27017 + dwc_param_dev_rx_fifo_size_default);
27018 + dwc_otg_set_param_dev_nperio_tx_fifo_size(core_if,
27019 + dwc_param_dev_nperio_tx_fifo_size_default);
27020 + dwc_otg_set_param_host_rx_fifo_size(core_if,
27021 + dwc_param_host_rx_fifo_size_default);
27022 + dwc_otg_set_param_host_nperio_tx_fifo_size(core_if,
27023 + dwc_param_host_nperio_tx_fifo_size_default);
27024 + dwc_otg_set_param_host_perio_tx_fifo_size(core_if,
27025 + dwc_param_host_perio_tx_fifo_size_default);
27026 + dwc_otg_set_param_max_transfer_size(core_if,
27027 + dwc_param_max_transfer_size_default);
27028 + dwc_otg_set_param_max_packet_count(core_if,
27029 + dwc_param_max_packet_count_default);
27030 + dwc_otg_set_param_host_channels(core_if,
27031 + dwc_param_host_channels_default);
27032 + dwc_otg_set_param_dev_endpoints(core_if,
27033 + dwc_param_dev_endpoints_default);
27034 + dwc_otg_set_param_phy_type(core_if, dwc_param_phy_type_default);
27035 + dwc_otg_set_param_speed(core_if, dwc_param_speed_default);
27036 + dwc_otg_set_param_host_ls_low_power_phy_clk(core_if,
27037 + dwc_param_host_ls_low_power_phy_clk_default);
27038 + dwc_otg_set_param_phy_ulpi_ddr(core_if, dwc_param_phy_ulpi_ddr_default);
27039 + dwc_otg_set_param_phy_ulpi_ext_vbus(core_if,
27040 + dwc_param_phy_ulpi_ext_vbus_default);
27041 + dwc_otg_set_param_phy_utmi_width(core_if,
27042 + dwc_param_phy_utmi_width_default);
27043 + dwc_otg_set_param_ts_dline(core_if, dwc_param_ts_dline_default);
27044 + dwc_otg_set_param_i2c_enable(core_if, dwc_param_i2c_enable_default);
27045 + dwc_otg_set_param_ulpi_fs_ls(core_if, dwc_param_ulpi_fs_ls_default);
27046 + dwc_otg_set_param_en_multiple_tx_fifo(core_if,
27047 + dwc_param_en_multiple_tx_fifo_default);
27048 + for (i = 0; i < 15; i++) {
27049 + dwc_otg_set_param_dev_perio_tx_fifo_size(core_if,
27050 + dwc_param_dev_perio_tx_fifo_size_default,
27051 + i);
27052 + }
27053 +
27054 + for (i = 0; i < 15; i++) {
27055 + dwc_otg_set_param_dev_tx_fifo_size(core_if,
27056 + dwc_param_dev_tx_fifo_size_default,
27057 + i);
27058 + }
27059 + dwc_otg_set_param_thr_ctl(core_if, dwc_param_thr_ctl_default);
27060 + dwc_otg_set_param_mpi_enable(core_if, dwc_param_mpi_enable_default);
27061 + dwc_otg_set_param_pti_enable(core_if, dwc_param_pti_enable_default);
27062 + dwc_otg_set_param_lpm_enable(core_if, dwc_param_lpm_enable_default);
27063 + dwc_otg_set_param_ic_usb_cap(core_if, dwc_param_ic_usb_cap_default);
27064 + dwc_otg_set_param_tx_thr_length(core_if,
27065 + dwc_param_tx_thr_length_default);
27066 + dwc_otg_set_param_rx_thr_length(core_if,
27067 + dwc_param_rx_thr_length_default);
27068 + dwc_otg_set_param_ahb_thr_ratio(core_if,
27069 + dwc_param_ahb_thr_ratio_default);
27070 + dwc_otg_set_param_power_down(core_if, dwc_param_power_down_default);
27071 + dwc_otg_set_param_reload_ctl(core_if, dwc_param_reload_ctl_default);
27072 + dwc_otg_set_param_dev_out_nak(core_if, dwc_param_dev_out_nak_default);
27073 + dwc_otg_set_param_cont_on_bna(core_if, dwc_param_cont_on_bna_default);
27074 + dwc_otg_set_param_ahb_single(core_if, dwc_param_ahb_single_default);
27075 + dwc_otg_set_param_otg_ver(core_if, dwc_param_otg_ver_default);
27076 + dwc_otg_set_param_adp_enable(core_if, dwc_param_adp_enable_default);
27077 + DWC_PRINTF("Finished setting default values for core params\n");
27078 +
27079 + return 0;
27080 +}
27081 +
27082 +uint8_t dwc_otg_is_dma_enable(dwc_otg_core_if_t * core_if)
27083 +{
27084 + return core_if->dma_enable;
27085 +}
27086 +
27087 +/* Checks if the parameter is outside of its valid range of values */
27088 +#define DWC_OTG_PARAM_TEST(_param_, _low_, _high_) \
27089 + (((_param_) < (_low_)) || \
27090 + ((_param_) > (_high_)))
27091 +
27092 +/* Parameter access functions */
27093 +int dwc_otg_set_param_otg_cap(dwc_otg_core_if_t * core_if, int32_t val)
27094 +{
27095 + int valid;
27096 + int retval = 0;
27097 + if (DWC_OTG_PARAM_TEST(val, 0, 2)) {
27098 + DWC_WARN("Wrong value for otg_cap parameter\n");
27099 + DWC_WARN("otg_cap parameter must be 0,1 or 2\n");
27100 + retval = -DWC_E_INVALID;
27101 + goto out;
27102 + }
27103 +
27104 + valid = 1;
27105 + switch (val) {
27106 + case DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE:
27107 + if (core_if->hwcfg2.b.op_mode !=
27108 + DWC_HWCFG2_OP_MODE_HNP_SRP_CAPABLE_OTG)
27109 + valid = 0;
27110 + break;
27111 + case DWC_OTG_CAP_PARAM_SRP_ONLY_CAPABLE:
27112 + if ((core_if->hwcfg2.b.op_mode !=
27113 + DWC_HWCFG2_OP_MODE_HNP_SRP_CAPABLE_OTG)
27114 + && (core_if->hwcfg2.b.op_mode !=
27115 + DWC_HWCFG2_OP_MODE_SRP_ONLY_CAPABLE_OTG)
27116 + && (core_if->hwcfg2.b.op_mode !=
27117 + DWC_HWCFG2_OP_MODE_SRP_CAPABLE_DEVICE)
27118 + && (core_if->hwcfg2.b.op_mode !=
27119 + DWC_HWCFG2_OP_MODE_SRP_CAPABLE_HOST)) {
27120 + valid = 0;
27121 + }
27122 + break;
27123 + case DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE:
27124 + /* always valid */
27125 + break;
27126 + }
27127 + if (!valid) {
27128 + if (dwc_otg_param_initialized(core_if->core_params->otg_cap)) {
27129 + DWC_ERROR
27130 + ("%d invalid for otg_cap paremter. Check HW configuration.\n",
27131 + val);
27132 + }
27133 + val =
27134 + (((core_if->hwcfg2.b.op_mode ==
27135 + DWC_HWCFG2_OP_MODE_HNP_SRP_CAPABLE_OTG)
27136 + || (core_if->hwcfg2.b.op_mode ==
27137 + DWC_HWCFG2_OP_MODE_SRP_ONLY_CAPABLE_OTG)
27138 + || (core_if->hwcfg2.b.op_mode ==
27139 + DWC_HWCFG2_OP_MODE_SRP_CAPABLE_DEVICE)
27140 + || (core_if->hwcfg2.b.op_mode ==
27141 + DWC_HWCFG2_OP_MODE_SRP_CAPABLE_HOST)) ?
27142 + DWC_OTG_CAP_PARAM_SRP_ONLY_CAPABLE :
27143 + DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE);
27144 + retval = -DWC_E_INVALID;
27145 + }
27146 +
27147 + core_if->core_params->otg_cap = val;
27148 +out:
27149 + return retval;
27150 +}
27151 +
27152 +int32_t dwc_otg_get_param_otg_cap(dwc_otg_core_if_t * core_if)
27153 +{
27154 + return core_if->core_params->otg_cap;
27155 +}
27156 +
27157 +int dwc_otg_set_param_opt(dwc_otg_core_if_t * core_if, int32_t val)
27158 +{
27159 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
27160 + DWC_WARN("Wrong value for opt parameter\n");
27161 + return -DWC_E_INVALID;
27162 + }
27163 + core_if->core_params->opt = val;
27164 + return 0;
27165 +}
27166 +
27167 +int32_t dwc_otg_get_param_opt(dwc_otg_core_if_t * core_if)
27168 +{
27169 + return core_if->core_params->opt;
27170 +}
27171 +
27172 +int dwc_otg_set_param_dma_enable(dwc_otg_core_if_t * core_if, int32_t val)
27173 +{
27174 + int retval = 0;
27175 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
27176 + DWC_WARN("Wrong value for dma enable\n");
27177 + return -DWC_E_INVALID;
27178 + }
27179 +
27180 + if ((val == 1) && (core_if->hwcfg2.b.architecture == 0)) {
27181 + if (dwc_otg_param_initialized(core_if->core_params->dma_enable)) {
27182 + DWC_ERROR
27183 + ("%d invalid for dma_enable paremter. Check HW configuration.\n",
27184 + val);
27185 + }
27186 + val = 0;
27187 + retval = -DWC_E_INVALID;
27188 + }
27189 +
27190 + core_if->core_params->dma_enable = val;
27191 + if (val == 0) {
27192 + dwc_otg_set_param_dma_desc_enable(core_if, 0);
27193 + }
27194 + return retval;
27195 +}
27196 +
27197 +int32_t dwc_otg_get_param_dma_enable(dwc_otg_core_if_t * core_if)
27198 +{
27199 + return core_if->core_params->dma_enable;
27200 +}
27201 +
27202 +int dwc_otg_set_param_dma_desc_enable(dwc_otg_core_if_t * core_if, int32_t val)
27203 +{
27204 + int retval = 0;
27205 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
27206 + DWC_WARN("Wrong value for dma_enable\n");
27207 + DWC_WARN("dma_desc_enable must be 0 or 1\n");
27208 + return -DWC_E_INVALID;
27209 + }
27210 +
27211 + if ((val == 1)
27212 + && ((dwc_otg_get_param_dma_enable(core_if) == 0)
27213 + || (core_if->hwcfg4.b.desc_dma == 0))) {
27214 + if (dwc_otg_param_initialized
27215 + (core_if->core_params->dma_desc_enable)) {
27216 + DWC_ERROR
27217 + ("%d invalid for dma_desc_enable paremter. Check HW configuration.\n",
27218 + val);
27219 + }
27220 + val = 0;
27221 + retval = -DWC_E_INVALID;
27222 + }
27223 + core_if->core_params->dma_desc_enable = val;
27224 + return retval;
27225 +}
27226 +
27227 +int32_t dwc_otg_get_param_dma_desc_enable(dwc_otg_core_if_t * core_if)
27228 +{
27229 + return core_if->core_params->dma_desc_enable;
27230 +}
27231 +
27232 +int dwc_otg_set_param_host_support_fs_ls_low_power(dwc_otg_core_if_t * core_if,
27233 + int32_t val)
27234 +{
27235 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
27236 + DWC_WARN("Wrong value for host_support_fs_low_power\n");
27237 + DWC_WARN("host_support_fs_low_power must be 0 or 1\n");
27238 + return -DWC_E_INVALID;
27239 + }
27240 + core_if->core_params->host_support_fs_ls_low_power = val;
27241 + return 0;
27242 +}
27243 +
27244 +int32_t dwc_otg_get_param_host_support_fs_ls_low_power(dwc_otg_core_if_t *
27245 + core_if)
27246 +{
27247 + return core_if->core_params->host_support_fs_ls_low_power;
27248 +}
27249 +
27250 +int dwc_otg_set_param_enable_dynamic_fifo(dwc_otg_core_if_t * core_if,
27251 + int32_t val)
27252 +{
27253 + int retval = 0;
27254 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
27255 + DWC_WARN("Wrong value for enable_dynamic_fifo\n");
27256 + DWC_WARN("enable_dynamic_fifo must be 0 or 1\n");
27257 + return -DWC_E_INVALID;
27258 + }
27259 +
27260 + if ((val == 1) && (core_if->hwcfg2.b.dynamic_fifo == 0)) {
27261 + if (dwc_otg_param_initialized
27262 + (core_if->core_params->enable_dynamic_fifo)) {
27263 + DWC_ERROR
27264 + ("%d invalid for enable_dynamic_fifo paremter. Check HW configuration.\n",
27265 + val);
27266 + }
27267 + val = 0;
27268 + retval = -DWC_E_INVALID;
27269 + }
27270 + core_if->core_params->enable_dynamic_fifo = val;
27271 + return retval;
27272 +}
27273 +
27274 +int32_t dwc_otg_get_param_enable_dynamic_fifo(dwc_otg_core_if_t * core_if)
27275 +{
27276 + return core_if->core_params->enable_dynamic_fifo;
27277 +}
27278 +
27279 +int dwc_otg_set_param_data_fifo_size(dwc_otg_core_if_t * core_if, int32_t val)
27280 +{
27281 + int retval = 0;
27282 + if (DWC_OTG_PARAM_TEST(val, 32, 32768)) {
27283 + DWC_WARN("Wrong value for data_fifo_size\n");
27284 + DWC_WARN("data_fifo_size must be 32-32768\n");
27285 + return -DWC_E_INVALID;
27286 + }
27287 +
27288 + if (val > core_if->hwcfg3.b.dfifo_depth) {
27289 + if (dwc_otg_param_initialized
27290 + (core_if->core_params->data_fifo_size)) {
27291 + DWC_ERROR
27292 + ("%d invalid for data_fifo_size parameter. Check HW configuration.\n",
27293 + val);
27294 + }
27295 + val = core_if->hwcfg3.b.dfifo_depth;
27296 + retval = -DWC_E_INVALID;
27297 + }
27298 +
27299 + core_if->core_params->data_fifo_size = val;
27300 + return retval;
27301 +}
27302 +
27303 +int32_t dwc_otg_get_param_data_fifo_size(dwc_otg_core_if_t * core_if)
27304 +{
27305 + return core_if->core_params->data_fifo_size;
27306 +}
27307 +
27308 +int dwc_otg_set_param_dev_rx_fifo_size(dwc_otg_core_if_t * core_if, int32_t val)
27309 +{
27310 + int retval = 0;
27311 + if (DWC_OTG_PARAM_TEST(val, 16, 32768)) {
27312 + DWC_WARN("Wrong value for dev_rx_fifo_size\n");
27313 + DWC_WARN("dev_rx_fifo_size must be 16-32768\n");
27314 + return -DWC_E_INVALID;
27315 + }
27316 +
27317 + if (val > DWC_READ_REG32(&core_if->core_global_regs->grxfsiz)) {
27318 + if (dwc_otg_param_initialized(core_if->core_params->dev_rx_fifo_size)) {
27319 + DWC_WARN("%d invalid for dev_rx_fifo_size parameter\n", val);
27320 + }
27321 + val = DWC_READ_REG32(&core_if->core_global_regs->grxfsiz);
27322 + retval = -DWC_E_INVALID;
27323 + }
27324 +
27325 + core_if->core_params->dev_rx_fifo_size = val;
27326 + return retval;
27327 +}
27328 +
27329 +int32_t dwc_otg_get_param_dev_rx_fifo_size(dwc_otg_core_if_t * core_if)
27330 +{
27331 + return core_if->core_params->dev_rx_fifo_size;
27332 +}
27333 +
27334 +int dwc_otg_set_param_dev_nperio_tx_fifo_size(dwc_otg_core_if_t * core_if,
27335 + int32_t val)
27336 +{
27337 + int retval = 0;
27338 +
27339 + if (DWC_OTG_PARAM_TEST(val, 16, 32768)) {
27340 + DWC_WARN("Wrong value for dev_nperio_tx_fifo\n");
27341 + DWC_WARN("dev_nperio_tx_fifo must be 16-32768\n");
27342 + return -DWC_E_INVALID;
27343 + }
27344 +
27345 + if (val > (DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz) >> 16)) {
27346 + if (dwc_otg_param_initialized
27347 + (core_if->core_params->dev_nperio_tx_fifo_size)) {
27348 + DWC_ERROR
27349 + ("%d invalid for dev_nperio_tx_fifo_size. Check HW configuration.\n",
27350 + val);
27351 + }
27352 + val =
27353 + (DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz) >>
27354 + 16);
27355 + retval = -DWC_E_INVALID;
27356 + }
27357 +
27358 + core_if->core_params->dev_nperio_tx_fifo_size = val;
27359 + return retval;
27360 +}
27361 +
27362 +int32_t dwc_otg_get_param_dev_nperio_tx_fifo_size(dwc_otg_core_if_t * core_if)
27363 +{
27364 + return core_if->core_params->dev_nperio_tx_fifo_size;
27365 +}
27366 +
27367 +int dwc_otg_set_param_host_rx_fifo_size(dwc_otg_core_if_t * core_if,
27368 + int32_t val)
27369 +{
27370 + int retval = 0;
27371 +
27372 + if (DWC_OTG_PARAM_TEST(val, 16, 32768)) {
27373 + DWC_WARN("Wrong value for host_rx_fifo_size\n");
27374 + DWC_WARN("host_rx_fifo_size must be 16-32768\n");
27375 + return -DWC_E_INVALID;
27376 + }
27377 +
27378 + if (val > DWC_READ_REG32(&core_if->core_global_regs->grxfsiz)) {
27379 + if (dwc_otg_param_initialized
27380 + (core_if->core_params->host_rx_fifo_size)) {
27381 + DWC_ERROR
27382 + ("%d invalid for host_rx_fifo_size. Check HW configuration.\n",
27383 + val);
27384 + }
27385 + val = DWC_READ_REG32(&core_if->core_global_regs->grxfsiz);
27386 + retval = -DWC_E_INVALID;
27387 + }
27388 +
27389 + core_if->core_params->host_rx_fifo_size = val;
27390 + return retval;
27391 +
27392 +}
27393 +
27394 +int32_t dwc_otg_get_param_host_rx_fifo_size(dwc_otg_core_if_t * core_if)
27395 +{
27396 + return core_if->core_params->host_rx_fifo_size;
27397 +}
27398 +
27399 +int dwc_otg_set_param_host_nperio_tx_fifo_size(dwc_otg_core_if_t * core_if,
27400 + int32_t val)
27401 +{
27402 + int retval = 0;
27403 +
27404 + if (DWC_OTG_PARAM_TEST(val, 16, 32768)) {
27405 + DWC_WARN("Wrong value for host_nperio_tx_fifo_size\n");
27406 + DWC_WARN("host_nperio_tx_fifo_size must be 16-32768\n");
27407 + return -DWC_E_INVALID;
27408 + }
27409 +
27410 + if (val > (DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz) >> 16)) {
27411 + if (dwc_otg_param_initialized
27412 + (core_if->core_params->host_nperio_tx_fifo_size)) {
27413 + DWC_ERROR
27414 + ("%d invalid for host_nperio_tx_fifo_size. Check HW configuration.\n",
27415 + val);
27416 + }
27417 + val =
27418 + (DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz) >>
27419 + 16);
27420 + retval = -DWC_E_INVALID;
27421 + }
27422 +
27423 + core_if->core_params->host_nperio_tx_fifo_size = val;
27424 + return retval;
27425 +}
27426 +
27427 +int32_t dwc_otg_get_param_host_nperio_tx_fifo_size(dwc_otg_core_if_t * core_if)
27428 +{
27429 + return core_if->core_params->host_nperio_tx_fifo_size;
27430 +}
27431 +
27432 +int dwc_otg_set_param_host_perio_tx_fifo_size(dwc_otg_core_if_t * core_if,
27433 + int32_t val)
27434 +{
27435 + int retval = 0;
27436 + if (DWC_OTG_PARAM_TEST(val, 16, 32768)) {
27437 + DWC_WARN("Wrong value for host_perio_tx_fifo_size\n");
27438 + DWC_WARN("host_perio_tx_fifo_size must be 16-32768\n");
27439 + return -DWC_E_INVALID;
27440 + }
27441 +
27442 + if (val > ((core_if->hptxfsiz.d32) >> 16)) {
27443 + if (dwc_otg_param_initialized
27444 + (core_if->core_params->host_perio_tx_fifo_size)) {
27445 + DWC_ERROR
27446 + ("%d invalid for host_perio_tx_fifo_size. Check HW configuration.\n",
27447 + val);
27448 + }
27449 + val = (core_if->hptxfsiz.d32) >> 16;
27450 + retval = -DWC_E_INVALID;
27451 + }
27452 +
27453 + core_if->core_params->host_perio_tx_fifo_size = val;
27454 + return retval;
27455 +}
27456 +
27457 +int32_t dwc_otg_get_param_host_perio_tx_fifo_size(dwc_otg_core_if_t * core_if)
27458 +{
27459 + return core_if->core_params->host_perio_tx_fifo_size;
27460 +}
27461 +
27462 +int dwc_otg_set_param_max_transfer_size(dwc_otg_core_if_t * core_if,
27463 + int32_t val)
27464 +{
27465 + int retval = 0;
27466 +
27467 + if (DWC_OTG_PARAM_TEST(val, 2047, 524288)) {
27468 + DWC_WARN("Wrong value for max_transfer_size\n");
27469 + DWC_WARN("max_transfer_size must be 2047-524288\n");
27470 + return -DWC_E_INVALID;
27471 + }
27472 +
27473 + if (val >= (1 << (core_if->hwcfg3.b.xfer_size_cntr_width + 11))) {
27474 + if (dwc_otg_param_initialized
27475 + (core_if->core_params->max_transfer_size)) {
27476 + DWC_ERROR
27477 + ("%d invalid for max_transfer_size. Check HW configuration.\n",
27478 + val);
27479 + }
27480 + val =
27481 + ((1 << (core_if->hwcfg3.b.packet_size_cntr_width + 11)) -
27482 + 1);
27483 + retval = -DWC_E_INVALID;
27484 + }
27485 +
27486 + core_if->core_params->max_transfer_size = val;
27487 + return retval;
27488 +}
27489 +
27490 +int32_t dwc_otg_get_param_max_transfer_size(dwc_otg_core_if_t * core_if)
27491 +{
27492 + return core_if->core_params->max_transfer_size;
27493 +}
27494 +
27495 +int dwc_otg_set_param_max_packet_count(dwc_otg_core_if_t * core_if, int32_t val)
27496 +{
27497 + int retval = 0;
27498 +
27499 + if (DWC_OTG_PARAM_TEST(val, 15, 511)) {
27500 + DWC_WARN("Wrong value for max_packet_count\n");
27501 + DWC_WARN("max_packet_count must be 15-511\n");
27502 + return -DWC_E_INVALID;
27503 + }
27504 +
27505 + if (val > (1 << (core_if->hwcfg3.b.packet_size_cntr_width + 4))) {
27506 + if (dwc_otg_param_initialized
27507 + (core_if->core_params->max_packet_count)) {
27508 + DWC_ERROR
27509 + ("%d invalid for max_packet_count. Check HW configuration.\n",
27510 + val);
27511 + }
27512 + val =
27513 + ((1 << (core_if->hwcfg3.b.packet_size_cntr_width + 4)) - 1);
27514 + retval = -DWC_E_INVALID;
27515 + }
27516 +
27517 + core_if->core_params->max_packet_count = val;
27518 + return retval;
27519 +}
27520 +
27521 +int32_t dwc_otg_get_param_max_packet_count(dwc_otg_core_if_t * core_if)
27522 +{
27523 + return core_if->core_params->max_packet_count;
27524 +}
27525 +
27526 +int dwc_otg_set_param_host_channels(dwc_otg_core_if_t * core_if, int32_t val)
27527 +{
27528 + int retval = 0;
27529 +
27530 + if (DWC_OTG_PARAM_TEST(val, 1, 16)) {
27531 + DWC_WARN("Wrong value for host_channels\n");
27532 + DWC_WARN("host_channels must be 1-16\n");
27533 + return -DWC_E_INVALID;
27534 + }
27535 +
27536 + if (val > (core_if->hwcfg2.b.num_host_chan + 1)) {
27537 + if (dwc_otg_param_initialized
27538 + (core_if->core_params->host_channels)) {
27539 + DWC_ERROR
27540 + ("%d invalid for host_channels. Check HW configurations.\n",
27541 + val);
27542 + }
27543 + val = (core_if->hwcfg2.b.num_host_chan + 1);
27544 + retval = -DWC_E_INVALID;
27545 + }
27546 +
27547 + core_if->core_params->host_channels = val;
27548 + return retval;
27549 +}
27550 +
27551 +int32_t dwc_otg_get_param_host_channels(dwc_otg_core_if_t * core_if)
27552 +{
27553 + return core_if->core_params->host_channels;
27554 +}
27555 +
27556 +int dwc_otg_set_param_dev_endpoints(dwc_otg_core_if_t * core_if, int32_t val)
27557 +{
27558 + int retval = 0;
27559 +
27560 + if (DWC_OTG_PARAM_TEST(val, 1, 15)) {
27561 + DWC_WARN("Wrong value for dev_endpoints\n");
27562 + DWC_WARN("dev_endpoints must be 1-15\n");
27563 + return -DWC_E_INVALID;
27564 + }
27565 +
27566 + if (val > (core_if->hwcfg2.b.num_dev_ep)) {
27567 + if (dwc_otg_param_initialized
27568 + (core_if->core_params->dev_endpoints)) {
27569 + DWC_ERROR
27570 + ("%d invalid for dev_endpoints. Check HW configurations.\n",
27571 + val);
27572 + }
27573 + val = core_if->hwcfg2.b.num_dev_ep;
27574 + retval = -DWC_E_INVALID;
27575 + }
27576 +
27577 + core_if->core_params->dev_endpoints = val;
27578 + return retval;
27579 +}
27580 +
27581 +int32_t dwc_otg_get_param_dev_endpoints(dwc_otg_core_if_t * core_if)
27582 +{
27583 + return core_if->core_params->dev_endpoints;
27584 +}
27585 +
27586 +int dwc_otg_set_param_phy_type(dwc_otg_core_if_t * core_if, int32_t val)
27587 +{
27588 + int retval = 0;
27589 + int valid = 0;
27590 +
27591 + if (DWC_OTG_PARAM_TEST(val, 0, 2)) {
27592 + DWC_WARN("Wrong value for phy_type\n");
27593 + DWC_WARN("phy_type must be 0,1 or 2\n");
27594 + return -DWC_E_INVALID;
27595 + }
27596 +#ifndef NO_FS_PHY_HW_CHECKS
27597 + if ((val == DWC_PHY_TYPE_PARAM_UTMI) &&
27598 + ((core_if->hwcfg2.b.hs_phy_type == 1) ||
27599 + (core_if->hwcfg2.b.hs_phy_type == 3))) {
27600 + valid = 1;
27601 + } else if ((val == DWC_PHY_TYPE_PARAM_ULPI) &&
27602 + ((core_if->hwcfg2.b.hs_phy_type == 2) ||
27603 + (core_if->hwcfg2.b.hs_phy_type == 3))) {
27604 + valid = 1;
27605 + } else if ((val == DWC_PHY_TYPE_PARAM_FS) &&
27606 + (core_if->hwcfg2.b.fs_phy_type == 1)) {
27607 + valid = 1;
27608 + }
27609 + if (!valid) {
27610 + if (dwc_otg_param_initialized(core_if->core_params->phy_type)) {
27611 + DWC_ERROR
27612 + ("%d invalid for phy_type. Check HW configurations.\n",
27613 + val);
27614 + }
27615 + if (core_if->hwcfg2.b.hs_phy_type) {
27616 + if ((core_if->hwcfg2.b.hs_phy_type == 3) ||
27617 + (core_if->hwcfg2.b.hs_phy_type == 1)) {
27618 + val = DWC_PHY_TYPE_PARAM_UTMI;
27619 + } else {
27620 + val = DWC_PHY_TYPE_PARAM_ULPI;
27621 + }
27622 + }
27623 + retval = -DWC_E_INVALID;
27624 + }
27625 +#endif
27626 + core_if->core_params->phy_type = val;
27627 + return retval;
27628 +}
27629 +
27630 +int32_t dwc_otg_get_param_phy_type(dwc_otg_core_if_t * core_if)
27631 +{
27632 + return core_if->core_params->phy_type;
27633 +}
27634 +
27635 +int dwc_otg_set_param_speed(dwc_otg_core_if_t * core_if, int32_t val)
27636 +{
27637 + int retval = 0;
27638 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
27639 + DWC_WARN("Wrong value for speed parameter\n");
27640 + DWC_WARN("max_speed parameter must be 0 or 1\n");
27641 + return -DWC_E_INVALID;
27642 + }
27643 + if ((val == 0)
27644 + && dwc_otg_get_param_phy_type(core_if) == DWC_PHY_TYPE_PARAM_FS) {
27645 + if (dwc_otg_param_initialized(core_if->core_params->speed)) {
27646 + DWC_ERROR
27647 + ("%d invalid for speed paremter. Check HW configuration.\n",
27648 + val);
27649 + }
27650 + val =
27651 + (dwc_otg_get_param_phy_type(core_if) ==
27652 + DWC_PHY_TYPE_PARAM_FS ? 1 : 0);
27653 + retval = -DWC_E_INVALID;
27654 + }
27655 + core_if->core_params->speed = val;
27656 + return retval;
27657 +}
27658 +
27659 +int32_t dwc_otg_get_param_speed(dwc_otg_core_if_t * core_if)
27660 +{
27661 + return core_if->core_params->speed;
27662 +}
27663 +
27664 +int dwc_otg_set_param_host_ls_low_power_phy_clk(dwc_otg_core_if_t * core_if,
27665 + int32_t val)
27666 +{
27667 + int retval = 0;
27668 +
27669 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
27670 + DWC_WARN
27671 + ("Wrong value for host_ls_low_power_phy_clk parameter\n");
27672 + DWC_WARN("host_ls_low_power_phy_clk must be 0 or 1\n");
27673 + return -DWC_E_INVALID;
27674 + }
27675 +
27676 + if ((val == DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ)
27677 + && (dwc_otg_get_param_phy_type(core_if) == DWC_PHY_TYPE_PARAM_FS)) {
27678 + if (dwc_otg_param_initialized
27679 + (core_if->core_params->host_ls_low_power_phy_clk)) {
27680 + DWC_ERROR
27681 + ("%d invalid for host_ls_low_power_phy_clk. Check HW configuration.\n",
27682 + val);
27683 + }
27684 + val =
27685 + (dwc_otg_get_param_phy_type(core_if) ==
27686 + DWC_PHY_TYPE_PARAM_FS) ?
27687 + DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_6MHZ :
27688 + DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ;
27689 + retval = -DWC_E_INVALID;
27690 + }
27691 +
27692 + core_if->core_params->host_ls_low_power_phy_clk = val;
27693 + return retval;
27694 +}
27695 +
27696 +int32_t dwc_otg_get_param_host_ls_low_power_phy_clk(dwc_otg_core_if_t * core_if)
27697 +{
27698 + return core_if->core_params->host_ls_low_power_phy_clk;
27699 +}
27700 +
27701 +int dwc_otg_set_param_phy_ulpi_ddr(dwc_otg_core_if_t * core_if, int32_t val)
27702 +{
27703 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
27704 + DWC_WARN("Wrong value for phy_ulpi_ddr\n");
27705 + DWC_WARN("phy_upli_ddr must be 0 or 1\n");
27706 + return -DWC_E_INVALID;
27707 + }
27708 +
27709 + core_if->core_params->phy_ulpi_ddr = val;
27710 + return 0;
27711 +}
27712 +
27713 +int32_t dwc_otg_get_param_phy_ulpi_ddr(dwc_otg_core_if_t * core_if)
27714 +{
27715 + return core_if->core_params->phy_ulpi_ddr;
27716 +}
27717 +
27718 +int dwc_otg_set_param_phy_ulpi_ext_vbus(dwc_otg_core_if_t * core_if,
27719 + int32_t val)
27720 +{
27721 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
27722 + DWC_WARN("Wrong valaue for phy_ulpi_ext_vbus\n");
27723 + DWC_WARN("phy_ulpi_ext_vbus must be 0 or 1\n");
27724 + return -DWC_E_INVALID;
27725 + }
27726 +
27727 + core_if->core_params->phy_ulpi_ext_vbus = val;
27728 + return 0;
27729 +}
27730 +
27731 +int32_t dwc_otg_get_param_phy_ulpi_ext_vbus(dwc_otg_core_if_t * core_if)
27732 +{
27733 + return core_if->core_params->phy_ulpi_ext_vbus;
27734 +}
27735 +
27736 +int dwc_otg_set_param_phy_utmi_width(dwc_otg_core_if_t * core_if, int32_t val)
27737 +{
27738 + if (DWC_OTG_PARAM_TEST(val, 8, 8) && DWC_OTG_PARAM_TEST(val, 16, 16)) {
27739 + DWC_WARN("Wrong valaue for phy_utmi_width\n");
27740 + DWC_WARN("phy_utmi_width must be 8 or 16\n");
27741 + return -DWC_E_INVALID;
27742 + }
27743 +
27744 + core_if->core_params->phy_utmi_width = val;
27745 + return 0;
27746 +}
27747 +
27748 +int32_t dwc_otg_get_param_phy_utmi_width(dwc_otg_core_if_t * core_if)
27749 +{
27750 + return core_if->core_params->phy_utmi_width;
27751 +}
27752 +
27753 +int dwc_otg_set_param_ulpi_fs_ls(dwc_otg_core_if_t * core_if, int32_t val)
27754 +{
27755 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
27756 + DWC_WARN("Wrong valaue for ulpi_fs_ls\n");
27757 + DWC_WARN("ulpi_fs_ls must be 0 or 1\n");
27758 + return -DWC_E_INVALID;
27759 + }
27760 +
27761 + core_if->core_params->ulpi_fs_ls = val;
27762 + return 0;
27763 +}
27764 +
27765 +int32_t dwc_otg_get_param_ulpi_fs_ls(dwc_otg_core_if_t * core_if)
27766 +{
27767 + return core_if->core_params->ulpi_fs_ls;
27768 +}
27769 +
27770 +int dwc_otg_set_param_ts_dline(dwc_otg_core_if_t * core_if, int32_t val)
27771 +{
27772 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
27773 + DWC_WARN("Wrong valaue for ts_dline\n");
27774 + DWC_WARN("ts_dline must be 0 or 1\n");
27775 + return -DWC_E_INVALID;
27776 + }
27777 +
27778 + core_if->core_params->ts_dline = val;
27779 + return 0;
27780 +}
27781 +
27782 +int32_t dwc_otg_get_param_ts_dline(dwc_otg_core_if_t * core_if)
27783 +{
27784 + return core_if->core_params->ts_dline;
27785 +}
27786 +
27787 +int dwc_otg_set_param_i2c_enable(dwc_otg_core_if_t * core_if, int32_t val)
27788 +{
27789 + int retval = 0;
27790 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
27791 + DWC_WARN("Wrong valaue for i2c_enable\n");
27792 + DWC_WARN("i2c_enable must be 0 or 1\n");
27793 + return -DWC_E_INVALID;
27794 + }
27795 +#ifndef NO_FS_PHY_HW_CHECK
27796 + if (val == 1 && core_if->hwcfg3.b.i2c == 0) {
27797 + if (dwc_otg_param_initialized(core_if->core_params->i2c_enable)) {
27798 + DWC_ERROR
27799 + ("%d invalid for i2c_enable. Check HW configuration.\n",
27800 + val);
27801 + }
27802 + val = 0;
27803 + retval = -DWC_E_INVALID;
27804 + }
27805 +#endif
27806 +
27807 + core_if->core_params->i2c_enable = val;
27808 + return retval;
27809 +}
27810 +
27811 +int32_t dwc_otg_get_param_i2c_enable(dwc_otg_core_if_t * core_if)
27812 +{
27813 + return core_if->core_params->i2c_enable;
27814 +}
27815 +
27816 +int dwc_otg_set_param_dev_perio_tx_fifo_size(dwc_otg_core_if_t * core_if,
27817 + int32_t val, int fifo_num)
27818 +{
27819 + int retval = 0;
27820 +
27821 + if (DWC_OTG_PARAM_TEST(val, 4, 768)) {
27822 + DWC_WARN("Wrong value for dev_perio_tx_fifo_size\n");
27823 + DWC_WARN("dev_perio_tx_fifo_size must be 4-768\n");
27824 + return -DWC_E_INVALID;
27825 + }
27826 +
27827 + if (val >
27828 + (DWC_READ_REG32(&core_if->core_global_regs->dtxfsiz[fifo_num]))) {
27829 + if (dwc_otg_param_initialized
27830 + (core_if->core_params->dev_perio_tx_fifo_size[fifo_num])) {
27831 + DWC_ERROR
27832 + ("`%d' invalid for parameter `dev_perio_fifo_size_%d'. Check HW configuration.\n",
27833 + val, fifo_num);
27834 + }
27835 + val = (DWC_READ_REG32(&core_if->core_global_regs->dtxfsiz[fifo_num]));
27836 + retval = -DWC_E_INVALID;
27837 + }
27838 +
27839 + core_if->core_params->dev_perio_tx_fifo_size[fifo_num] = val;
27840 + return retval;
27841 +}
27842 +
27843 +int32_t dwc_otg_get_param_dev_perio_tx_fifo_size(dwc_otg_core_if_t * core_if,
27844 + int fifo_num)
27845 +{
27846 + return core_if->core_params->dev_perio_tx_fifo_size[fifo_num];
27847 +}
27848 +
27849 +int dwc_otg_set_param_en_multiple_tx_fifo(dwc_otg_core_if_t * core_if,
27850 + int32_t val)
27851 +{
27852 + int retval = 0;
27853 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
27854 + DWC_WARN("Wrong valaue for en_multiple_tx_fifo,\n");
27855 + DWC_WARN("en_multiple_tx_fifo must be 0 or 1\n");
27856 + return -DWC_E_INVALID;
27857 + }
27858 +
27859 + if (val == 1 && core_if->hwcfg4.b.ded_fifo_en == 0) {
27860 + if (dwc_otg_param_initialized
27861 + (core_if->core_params->en_multiple_tx_fifo)) {
27862 + DWC_ERROR
27863 + ("%d invalid for parameter en_multiple_tx_fifo. Check HW configuration.\n",
27864 + val);
27865 + }
27866 + val = 0;
27867 + retval = -DWC_E_INVALID;
27868 + }
27869 +
27870 + core_if->core_params->en_multiple_tx_fifo = val;
27871 + return retval;
27872 +}
27873 +
27874 +int32_t dwc_otg_get_param_en_multiple_tx_fifo(dwc_otg_core_if_t * core_if)
27875 +{
27876 + return core_if->core_params->en_multiple_tx_fifo;
27877 +}
27878 +
27879 +int dwc_otg_set_param_dev_tx_fifo_size(dwc_otg_core_if_t * core_if, int32_t val,
27880 + int fifo_num)
27881 +{
27882 + int retval = 0;
27883 +
27884 + if (DWC_OTG_PARAM_TEST(val, 4, 768)) {
27885 + DWC_WARN("Wrong value for dev_tx_fifo_size\n");
27886 + DWC_WARN("dev_tx_fifo_size must be 4-768\n");
27887 + return -DWC_E_INVALID;
27888 + }
27889 +
27890 + if (val >
27891 + (DWC_READ_REG32(&core_if->core_global_regs->dtxfsiz[fifo_num]))) {
27892 + if (dwc_otg_param_initialized
27893 + (core_if->core_params->dev_tx_fifo_size[fifo_num])) {
27894 + DWC_ERROR
27895 + ("`%d' invalid for parameter `dev_tx_fifo_size_%d'. Check HW configuration.\n",
27896 + val, fifo_num);
27897 + }
27898 + val = (DWC_READ_REG32(&core_if->core_global_regs->dtxfsiz[fifo_num]));
27899 + retval = -DWC_E_INVALID;
27900 + }
27901 +
27902 + core_if->core_params->dev_tx_fifo_size[fifo_num] = val;
27903 + return retval;
27904 +}
27905 +
27906 +int32_t dwc_otg_get_param_dev_tx_fifo_size(dwc_otg_core_if_t * core_if,
27907 + int fifo_num)
27908 +{
27909 + return core_if->core_params->dev_tx_fifo_size[fifo_num];
27910 +}
27911 +
27912 +int dwc_otg_set_param_thr_ctl(dwc_otg_core_if_t * core_if, int32_t val)
27913 +{
27914 + int retval = 0;
27915 +
27916 + if (DWC_OTG_PARAM_TEST(val, 0, 7)) {
27917 + DWC_WARN("Wrong value for thr_ctl\n");
27918 + DWC_WARN("thr_ctl must be 0-7\n");
27919 + return -DWC_E_INVALID;
27920 + }
27921 +
27922 + if ((val != 0) &&
27923 + (!dwc_otg_get_param_dma_enable(core_if) ||
27924 + !core_if->hwcfg4.b.ded_fifo_en)) {
27925 + if (dwc_otg_param_initialized(core_if->core_params->thr_ctl)) {
27926 + DWC_ERROR
27927 + ("%d invalid for parameter thr_ctl. Check HW configuration.\n",
27928 + val);
27929 + }
27930 + val = 0;
27931 + retval = -DWC_E_INVALID;
27932 + }
27933 +
27934 + core_if->core_params->thr_ctl = val;
27935 + return retval;
27936 +}
27937 +
27938 +int32_t dwc_otg_get_param_thr_ctl(dwc_otg_core_if_t * core_if)
27939 +{
27940 + return core_if->core_params->thr_ctl;
27941 +}
27942 +
27943 +int dwc_otg_set_param_lpm_enable(dwc_otg_core_if_t * core_if, int32_t val)
27944 +{
27945 + int retval = 0;
27946 +
27947 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
27948 + DWC_WARN("Wrong value for lpm_enable\n");
27949 + DWC_WARN("lpm_enable must be 0 or 1\n");
27950 + return -DWC_E_INVALID;
27951 + }
27952 +
27953 + if (val && !core_if->hwcfg3.b.otg_lpm_en) {
27954 + if (dwc_otg_param_initialized(core_if->core_params->lpm_enable)) {
27955 + DWC_ERROR
27956 + ("%d invalid for parameter lpm_enable. Check HW configuration.\n",
27957 + val);
27958 + }
27959 + val = 0;
27960 + retval = -DWC_E_INVALID;
27961 + }
27962 +
27963 + core_if->core_params->lpm_enable = val;
27964 + return retval;
27965 +}
27966 +
27967 +int32_t dwc_otg_get_param_lpm_enable(dwc_otg_core_if_t * core_if)
27968 +{
27969 + return core_if->core_params->lpm_enable;
27970 +}
27971 +
27972 +int dwc_otg_set_param_tx_thr_length(dwc_otg_core_if_t * core_if, int32_t val)
27973 +{
27974 + if (DWC_OTG_PARAM_TEST(val, 8, 128)) {
27975 + DWC_WARN("Wrong valaue for tx_thr_length\n");
27976 + DWC_WARN("tx_thr_length must be 8 - 128\n");
27977 + return -DWC_E_INVALID;
27978 + }
27979 +
27980 + core_if->core_params->tx_thr_length = val;
27981 + return 0;
27982 +}
27983 +
27984 +int32_t dwc_otg_get_param_tx_thr_length(dwc_otg_core_if_t * core_if)
27985 +{
27986 + return core_if->core_params->tx_thr_length;
27987 +}
27988 +
27989 +int dwc_otg_set_param_rx_thr_length(dwc_otg_core_if_t * core_if, int32_t val)
27990 +{
27991 + if (DWC_OTG_PARAM_TEST(val, 8, 128)) {
27992 + DWC_WARN("Wrong valaue for rx_thr_length\n");
27993 + DWC_WARN("rx_thr_length must be 8 - 128\n");
27994 + return -DWC_E_INVALID;
27995 + }
27996 +
27997 + core_if->core_params->rx_thr_length = val;
27998 + return 0;
27999 +}
28000 +
28001 +int32_t dwc_otg_get_param_rx_thr_length(dwc_otg_core_if_t * core_if)
28002 +{
28003 + return core_if->core_params->rx_thr_length;
28004 +}
28005 +
28006 +int dwc_otg_set_param_dma_burst_size(dwc_otg_core_if_t * core_if, int32_t val)
28007 +{
28008 + if (DWC_OTG_PARAM_TEST(val, 1, 1) &&
28009 + DWC_OTG_PARAM_TEST(val, 4, 4) &&
28010 + DWC_OTG_PARAM_TEST(val, 8, 8) &&
28011 + DWC_OTG_PARAM_TEST(val, 16, 16) &&
28012 + DWC_OTG_PARAM_TEST(val, 32, 32) &&
28013 + DWC_OTG_PARAM_TEST(val, 64, 64) &&
28014 + DWC_OTG_PARAM_TEST(val, 128, 128) &&
28015 + DWC_OTG_PARAM_TEST(val, 256, 256)) {
28016 + DWC_WARN("`%d' invalid for parameter `dma_burst_size'\n", val);
28017 + return -DWC_E_INVALID;
28018 + }
28019 + core_if->core_params->dma_burst_size = val;
28020 + return 0;
28021 +}
28022 +
28023 +int32_t dwc_otg_get_param_dma_burst_size(dwc_otg_core_if_t * core_if)
28024 +{
28025 + return core_if->core_params->dma_burst_size;
28026 +}
28027 +
28028 +int dwc_otg_set_param_pti_enable(dwc_otg_core_if_t * core_if, int32_t val)
28029 +{
28030 + int retval = 0;
28031 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
28032 + DWC_WARN("`%d' invalid for parameter `pti_enable'\n", val);
28033 + return -DWC_E_INVALID;
28034 + }
28035 + if (val && (core_if->snpsid < OTG_CORE_REV_2_72a)) {
28036 + if (dwc_otg_param_initialized(core_if->core_params->pti_enable)) {
28037 + DWC_ERROR
28038 + ("%d invalid for parameter pti_enable. Check HW configuration.\n",
28039 + val);
28040 + }
28041 + retval = -DWC_E_INVALID;
28042 + val = 0;
28043 + }
28044 + core_if->core_params->pti_enable = val;
28045 + return retval;
28046 +}
28047 +
28048 +int32_t dwc_otg_get_param_pti_enable(dwc_otg_core_if_t * core_if)
28049 +{
28050 + return core_if->core_params->pti_enable;
28051 +}
28052 +
28053 +int dwc_otg_set_param_mpi_enable(dwc_otg_core_if_t * core_if, int32_t val)
28054 +{
28055 + int retval = 0;
28056 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
28057 + DWC_WARN("`%d' invalid for parameter `mpi_enable'\n", val);
28058 + return -DWC_E_INVALID;
28059 + }
28060 + if (val && (core_if->hwcfg2.b.multi_proc_int == 0)) {
28061 + if (dwc_otg_param_initialized(core_if->core_params->mpi_enable)) {
28062 + DWC_ERROR
28063 + ("%d invalid for parameter mpi_enable. Check HW configuration.\n",
28064 + val);
28065 + }
28066 + retval = -DWC_E_INVALID;
28067 + val = 0;
28068 + }
28069 + core_if->core_params->mpi_enable = val;
28070 + return retval;
28071 +}
28072 +
28073 +int32_t dwc_otg_get_param_mpi_enable(dwc_otg_core_if_t * core_if)
28074 +{
28075 + return core_if->core_params->mpi_enable;
28076 +}
28077 +
28078 +int dwc_otg_set_param_adp_enable(dwc_otg_core_if_t * core_if, int32_t val)
28079 +{
28080 + int retval = 0;
28081 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
28082 + DWC_WARN("`%d' invalid for parameter `adp_enable'\n", val);
28083 + return -DWC_E_INVALID;
28084 + }
28085 + if (val && (core_if->hwcfg3.b.adp_supp == 0)) {
28086 + if (dwc_otg_param_initialized
28087 + (core_if->core_params->adp_supp_enable)) {
28088 + DWC_ERROR
28089 + ("%d invalid for parameter adp_enable. Check HW configuration.\n",
28090 + val);
28091 + }
28092 + retval = -DWC_E_INVALID;
28093 + val = 0;
28094 + }
28095 + core_if->core_params->adp_supp_enable = val;
28096 + /*Set OTG version 2.0 in case of enabling ADP*/
28097 + if (val)
28098 + dwc_otg_set_param_otg_ver(core_if, 1);
28099 +
28100 + return retval;
28101 +}
28102 +
28103 +int32_t dwc_otg_get_param_adp_enable(dwc_otg_core_if_t * core_if)
28104 +{
28105 + return core_if->core_params->adp_supp_enable;
28106 +}
28107 +
28108 +int dwc_otg_set_param_ic_usb_cap(dwc_otg_core_if_t * core_if, int32_t val)
28109 +{
28110 + int retval = 0;
28111 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
28112 + DWC_WARN("`%d' invalid for parameter `ic_usb_cap'\n", val);
28113 + DWC_WARN("ic_usb_cap must be 0 or 1\n");
28114 + return -DWC_E_INVALID;
28115 + }
28116 +
28117 + if (val && (core_if->hwcfg2.b.otg_enable_ic_usb == 0)) {
28118 + if (dwc_otg_param_initialized(core_if->core_params->ic_usb_cap)) {
28119 + DWC_ERROR
28120 + ("%d invalid for parameter ic_usb_cap. Check HW configuration.\n",
28121 + val);
28122 + }
28123 + retval = -DWC_E_INVALID;
28124 + val = 0;
28125 + }
28126 + core_if->core_params->ic_usb_cap = val;
28127 + return retval;
28128 +}
28129 +
28130 +int32_t dwc_otg_get_param_ic_usb_cap(dwc_otg_core_if_t * core_if)
28131 +{
28132 + return core_if->core_params->ic_usb_cap;
28133 +}
28134 +
28135 +int dwc_otg_set_param_ahb_thr_ratio(dwc_otg_core_if_t * core_if, int32_t val)
28136 +{
28137 + int retval = 0;
28138 + int valid = 1;
28139 +
28140 + if (DWC_OTG_PARAM_TEST(val, 0, 3)) {
28141 + DWC_WARN("`%d' invalid for parameter `ahb_thr_ratio'\n", val);
28142 + DWC_WARN("ahb_thr_ratio must be 0 - 3\n");
28143 + return -DWC_E_INVALID;
28144 + }
28145 +
28146 + if (val
28147 + && (core_if->snpsid < OTG_CORE_REV_2_81a
28148 + || !dwc_otg_get_param_thr_ctl(core_if))) {
28149 + valid = 0;
28150 + } else if (val
28151 + && ((dwc_otg_get_param_tx_thr_length(core_if) / (1 << val)) <
28152 + 4)) {
28153 + valid = 0;
28154 + }
28155 + if (valid == 0) {
28156 + if (dwc_otg_param_initialized
28157 + (core_if->core_params->ahb_thr_ratio)) {
28158 + DWC_ERROR
28159 + ("%d invalid for parameter ahb_thr_ratio. Check HW configuration.\n",
28160 + val);
28161 + }
28162 + retval = -DWC_E_INVALID;
28163 + val = 0;
28164 + }
28165 +
28166 + core_if->core_params->ahb_thr_ratio = val;
28167 + return retval;
28168 +}
28169 +
28170 +int32_t dwc_otg_get_param_ahb_thr_ratio(dwc_otg_core_if_t * core_if)
28171 +{
28172 + return core_if->core_params->ahb_thr_ratio;
28173 +}
28174 +
28175 +int dwc_otg_set_param_power_down(dwc_otg_core_if_t * core_if, int32_t val)
28176 +{
28177 + int retval = 0;
28178 + int valid = 1;
28179 + hwcfg4_data_t hwcfg4 = {.d32 = 0 };
28180 + hwcfg4.d32 = DWC_READ_REG32(&core_if->core_global_regs->ghwcfg4);
28181 +
28182 + if (DWC_OTG_PARAM_TEST(val, 0, 3)) {
28183 + DWC_WARN("`%d' invalid for parameter `power_down'\n", val);
28184 + DWC_WARN("power_down must be 0 - 2\n");
28185 + return -DWC_E_INVALID;
28186 + }
28187 +
28188 + if ((val == 2) && (core_if->snpsid < OTG_CORE_REV_2_91a)) {
28189 + valid = 0;
28190 + }
28191 + if ((val == 3)
28192 + && ((core_if->snpsid < OTG_CORE_REV_3_00a)
28193 + || (hwcfg4.b.xhiber == 0))) {
28194 + valid = 0;
28195 + }
28196 + if (valid == 0) {
28197 + if (dwc_otg_param_initialized(core_if->core_params->power_down)) {
28198 + DWC_ERROR
28199 + ("%d invalid for parameter power_down. Check HW configuration.\n",
28200 + val);
28201 + }
28202 + retval = -DWC_E_INVALID;
28203 + val = 0;
28204 + }
28205 + core_if->core_params->power_down = val;
28206 + return retval;
28207 +}
28208 +
28209 +int32_t dwc_otg_get_param_power_down(dwc_otg_core_if_t * core_if)
28210 +{
28211 + return core_if->core_params->power_down;
28212 +}
28213 +
28214 +int dwc_otg_set_param_reload_ctl(dwc_otg_core_if_t * core_if, int32_t val)
28215 +{
28216 + int retval = 0;
28217 + int valid = 1;
28218 +
28219 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
28220 + DWC_WARN("`%d' invalid for parameter `reload_ctl'\n", val);
28221 + DWC_WARN("reload_ctl must be 0 or 1\n");
28222 + return -DWC_E_INVALID;
28223 + }
28224 +
28225 + if ((val == 1) && (core_if->snpsid < OTG_CORE_REV_2_92a)) {
28226 + valid = 0;
28227 + }
28228 + if (valid == 0) {
28229 + if (dwc_otg_param_initialized(core_if->core_params->reload_ctl)) {
28230 + DWC_ERROR("%d invalid for parameter reload_ctl."
28231 + "Check HW configuration.\n", val);
28232 + }
28233 + retval = -DWC_E_INVALID;
28234 + val = 0;
28235 + }
28236 + core_if->core_params->reload_ctl = val;
28237 + return retval;
28238 +}
28239 +
28240 +int32_t dwc_otg_get_param_reload_ctl(dwc_otg_core_if_t * core_if)
28241 +{
28242 + return core_if->core_params->reload_ctl;
28243 +}
28244 +
28245 +int dwc_otg_set_param_dev_out_nak(dwc_otg_core_if_t * core_if, int32_t val)
28246 +{
28247 + int retval = 0;
28248 + int valid = 1;
28249 +
28250 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
28251 + DWC_WARN("`%d' invalid for parameter `dev_out_nak'\n", val);
28252 + DWC_WARN("dev_out_nak must be 0 or 1\n");
28253 + return -DWC_E_INVALID;
28254 + }
28255 +
28256 + if ((val == 1) && ((core_if->snpsid < OTG_CORE_REV_2_93a) ||
28257 + !(core_if->core_params->dma_desc_enable))) {
28258 + valid = 0;
28259 + }
28260 + if (valid == 0) {
28261 + if (dwc_otg_param_initialized(core_if->core_params->dev_out_nak)) {
28262 + DWC_ERROR("%d invalid for parameter dev_out_nak."
28263 + "Check HW configuration.\n", val);
28264 + }
28265 + retval = -DWC_E_INVALID;
28266 + val = 0;
28267 + }
28268 + core_if->core_params->dev_out_nak = val;
28269 + return retval;
28270 +}
28271 +
28272 +int32_t dwc_otg_get_param_dev_out_nak(dwc_otg_core_if_t * core_if)
28273 +{
28274 + return core_if->core_params->dev_out_nak;
28275 +}
28276 +
28277 +int dwc_otg_set_param_cont_on_bna(dwc_otg_core_if_t * core_if, int32_t val)
28278 +{
28279 + int retval = 0;
28280 + int valid = 1;
28281 +
28282 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
28283 + DWC_WARN("`%d' invalid for parameter `cont_on_bna'\n", val);
28284 + DWC_WARN("cont_on_bna must be 0 or 1\n");
28285 + return -DWC_E_INVALID;
28286 + }
28287 +
28288 + if ((val == 1) && ((core_if->snpsid < OTG_CORE_REV_2_94a) ||
28289 + !(core_if->core_params->dma_desc_enable))) {
28290 + valid = 0;
28291 + }
28292 + if (valid == 0) {
28293 + if (dwc_otg_param_initialized(core_if->core_params->cont_on_bna)) {
28294 + DWC_ERROR("%d invalid for parameter cont_on_bna."
28295 + "Check HW configuration.\n", val);
28296 + }
28297 + retval = -DWC_E_INVALID;
28298 + val = 0;
28299 + }
28300 + core_if->core_params->cont_on_bna = val;
28301 + return retval;
28302 +}
28303 +
28304 +int32_t dwc_otg_get_param_cont_on_bna(dwc_otg_core_if_t * core_if)
28305 +{
28306 + return core_if->core_params->cont_on_bna;
28307 +}
28308 +
28309 +int dwc_otg_set_param_ahb_single(dwc_otg_core_if_t * core_if, int32_t val)
28310 +{
28311 + int retval = 0;
28312 + int valid = 1;
28313 +
28314 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
28315 + DWC_WARN("`%d' invalid for parameter `ahb_single'\n", val);
28316 + DWC_WARN("ahb_single must be 0 or 1\n");
28317 + return -DWC_E_INVALID;
28318 + }
28319 +
28320 + if ((val == 1) && (core_if->snpsid < OTG_CORE_REV_2_94a)) {
28321 + valid = 0;
28322 + }
28323 + if (valid == 0) {
28324 + if (dwc_otg_param_initialized(core_if->core_params->ahb_single)) {
28325 + DWC_ERROR("%d invalid for parameter ahb_single."
28326 + "Check HW configuration.\n", val);
28327 + }
28328 + retval = -DWC_E_INVALID;
28329 + val = 0;
28330 + }
28331 + core_if->core_params->ahb_single = val;
28332 + return retval;
28333 +}
28334 +
28335 +int32_t dwc_otg_get_param_ahb_single(dwc_otg_core_if_t * core_if)
28336 +{
28337 + return core_if->core_params->ahb_single;
28338 +}
28339 +
28340 +int dwc_otg_set_param_otg_ver(dwc_otg_core_if_t * core_if, int32_t val)
28341 +{
28342 + int retval = 0;
28343 +
28344 + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
28345 + DWC_WARN("`%d' invalid for parameter `otg_ver'\n", val);
28346 + DWC_WARN
28347 + ("otg_ver must be 0(for OTG 1.3 support) or 1(for OTG 2.0 support)\n");
28348 + return -DWC_E_INVALID;
28349 + }
28350 +
28351 + core_if->core_params->otg_ver = val;
28352 + return retval;
28353 +}
28354 +
28355 +int32_t dwc_otg_get_param_otg_ver(dwc_otg_core_if_t * core_if)
28356 +{
28357 + return core_if->core_params->otg_ver;
28358 +}
28359 +
28360 +uint32_t dwc_otg_get_hnpstatus(dwc_otg_core_if_t * core_if)
28361 +{
28362 + gotgctl_data_t otgctl;
28363 + otgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
28364 + return otgctl.b.hstnegscs;
28365 +}
28366 +
28367 +uint32_t dwc_otg_get_srpstatus(dwc_otg_core_if_t * core_if)
28368 +{
28369 + gotgctl_data_t otgctl;
28370 + otgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
28371 + return otgctl.b.sesreqscs;
28372 +}
28373 +
28374 +void dwc_otg_set_hnpreq(dwc_otg_core_if_t * core_if, uint32_t val)
28375 +{
28376 + if(core_if->otg_ver == 0) {
28377 + gotgctl_data_t otgctl;
28378 + otgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
28379 + otgctl.b.hnpreq = val;
28380 + DWC_WRITE_REG32(&core_if->core_global_regs->gotgctl, otgctl.d32);
28381 + } else {
28382 + core_if->otg_sts = val;
28383 + }
28384 +}
28385 +
28386 +uint32_t dwc_otg_get_gsnpsid(dwc_otg_core_if_t * core_if)
28387 +{
28388 + return core_if->snpsid;
28389 +}
28390 +
28391 +uint32_t dwc_otg_get_mode(dwc_otg_core_if_t * core_if)
28392 +{
28393 + gintsts_data_t gintsts;
28394 + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
28395 + return gintsts.b.curmode;
28396 +}
28397 +
28398 +uint32_t dwc_otg_get_hnpcapable(dwc_otg_core_if_t * core_if)
28399 +{
28400 + gusbcfg_data_t usbcfg;
28401 + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
28402 + return usbcfg.b.hnpcap;
28403 +}
28404 +
28405 +void dwc_otg_set_hnpcapable(dwc_otg_core_if_t * core_if, uint32_t val)
28406 +{
28407 + gusbcfg_data_t usbcfg;
28408 + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
28409 + usbcfg.b.hnpcap = val;
28410 + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, usbcfg.d32);
28411 +}
28412 +
28413 +uint32_t dwc_otg_get_srpcapable(dwc_otg_core_if_t * core_if)
28414 +{
28415 + gusbcfg_data_t usbcfg;
28416 + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
28417 + return usbcfg.b.srpcap;
28418 +}
28419 +
28420 +void dwc_otg_set_srpcapable(dwc_otg_core_if_t * core_if, uint32_t val)
28421 +{
28422 + gusbcfg_data_t usbcfg;
28423 + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
28424 + usbcfg.b.srpcap = val;
28425 + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, usbcfg.d32);
28426 +}
28427 +
28428 +uint32_t dwc_otg_get_devspeed(dwc_otg_core_if_t * core_if)
28429 +{
28430 + dcfg_data_t dcfg;
28431 + /* originally: dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg); */
28432 +
28433 + dcfg.d32 = -1; //GRAYG
28434 + DWC_DEBUGPL(DBG_CILV, "%s - core_if(%p)\n", __func__, core_if);
28435 + if (NULL == core_if)
28436 + DWC_ERROR("reg request with NULL core_if\n");
28437 + DWC_DEBUGPL(DBG_CILV, "%s - core_if(%p)->dev_if(%p)\n", __func__,
28438 + core_if, core_if->dev_if);
28439 + if (NULL == core_if->dev_if)
28440 + DWC_ERROR("reg request with NULL dev_if\n");
28441 + DWC_DEBUGPL(DBG_CILV, "%s - core_if(%p)->dev_if(%p)->"
28442 + "dev_global_regs(%p)\n", __func__,
28443 + core_if, core_if->dev_if,
28444 + core_if->dev_if->dev_global_regs);
28445 + if (NULL == core_if->dev_if->dev_global_regs)
28446 + DWC_ERROR("reg request with NULL dev_global_regs\n");
28447 + else {
28448 + DWC_DEBUGPL(DBG_CILV, "%s - &core_if(%p)->dev_if(%p)->"
28449 + "dev_global_regs(%p)->dcfg = %p\n", __func__,
28450 + core_if, core_if->dev_if,
28451 + core_if->dev_if->dev_global_regs,
28452 + &core_if->dev_if->dev_global_regs->dcfg);
28453 + dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
28454 + }
28455 + return dcfg.b.devspd;
28456 +}
28457 +
28458 +void dwc_otg_set_devspeed(dwc_otg_core_if_t * core_if, uint32_t val)
28459 +{
28460 + dcfg_data_t dcfg;
28461 + dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
28462 + dcfg.b.devspd = val;
28463 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg, dcfg.d32);
28464 +}
28465 +
28466 +uint32_t dwc_otg_get_busconnected(dwc_otg_core_if_t * core_if)
28467 +{
28468 + hprt0_data_t hprt0;
28469 + hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
28470 + return hprt0.b.prtconnsts;
28471 +}
28472 +
28473 +uint32_t dwc_otg_get_enumspeed(dwc_otg_core_if_t * core_if)
28474 +{
28475 + dsts_data_t dsts;
28476 + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
28477 + return dsts.b.enumspd;
28478 +}
28479 +
28480 +uint32_t dwc_otg_get_prtpower(dwc_otg_core_if_t * core_if)
28481 +{
28482 + hprt0_data_t hprt0;
28483 + hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
28484 + return hprt0.b.prtpwr;
28485 +
28486 +}
28487 +
28488 +uint32_t dwc_otg_get_core_state(dwc_otg_core_if_t * core_if)
28489 +{
28490 + return core_if->hibernation_suspend;
28491 +}
28492 +
28493 +void dwc_otg_set_prtpower(dwc_otg_core_if_t * core_if, uint32_t val)
28494 +{
28495 + hprt0_data_t hprt0;
28496 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
28497 + hprt0.b.prtpwr = val;
28498 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
28499 +}
28500 +
28501 +uint32_t dwc_otg_get_prtsuspend(dwc_otg_core_if_t * core_if)
28502 +{
28503 + hprt0_data_t hprt0;
28504 + hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
28505 + return hprt0.b.prtsusp;
28506 +
28507 +}
28508 +
28509 +void dwc_otg_set_prtsuspend(dwc_otg_core_if_t * core_if, uint32_t val)
28510 +{
28511 + hprt0_data_t hprt0;
28512 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
28513 + hprt0.b.prtsusp = val;
28514 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
28515 +}
28516 +
28517 +uint32_t dwc_otg_get_fr_interval(dwc_otg_core_if_t * core_if)
28518 +{
28519 + hfir_data_t hfir;
28520 + hfir.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->hfir);
28521 + return hfir.b.frint;
28522 +
28523 +}
28524 +
28525 +void dwc_otg_set_fr_interval(dwc_otg_core_if_t * core_if, uint32_t val)
28526 +{
28527 + hfir_data_t hfir;
28528 + uint32_t fram_int;
28529 + fram_int = calc_frame_interval(core_if);
28530 + hfir.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->hfir);
28531 + if (!core_if->core_params->reload_ctl) {
28532 + DWC_WARN("\nCannot reload HFIR register.HFIR.HFIRRldCtrl bit is"
28533 + "not set to 1.\nShould load driver with reload_ctl=1"
28534 + " module parameter\n");
28535 + return;
28536 + }
28537 + switch (fram_int) {
28538 + case 3750:
28539 + if ((val < 3350) || (val > 4150)) {
28540 + DWC_WARN("HFIR interval for HS core and 30 MHz"
28541 + "clock freq should be from 3350 to 4150\n");
28542 + return;
28543 + }
28544 + break;
28545 + case 30000:
28546 + if ((val < 26820) || (val > 33180)) {
28547 + DWC_WARN("HFIR interval for FS/LS core and 30 MHz"
28548 + "clock freq should be from 26820 to 33180\n");
28549 + return;
28550 + }
28551 + break;
28552 + case 6000:
28553 + if ((val < 5360) || (val > 6640)) {
28554 + DWC_WARN("HFIR interval for HS core and 48 MHz"
28555 + "clock freq should be from 5360 to 6640\n");
28556 + return;
28557 + }
28558 + break;
28559 + case 48000:
28560 + if ((val < 42912) || (val > 53088)) {
28561 + DWC_WARN("HFIR interval for FS/LS core and 48 MHz"
28562 + "clock freq should be from 42912 to 53088\n");
28563 + return;
28564 + }
28565 + break;
28566 + case 7500:
28567 + if ((val < 6700) || (val > 8300)) {
28568 + DWC_WARN("HFIR interval for HS core and 60 MHz"
28569 + "clock freq should be from 6700 to 8300\n");
28570 + return;
28571 + }
28572 + break;
28573 + case 60000:
28574 + if ((val < 53640) || (val > 65536)) {
28575 + DWC_WARN("HFIR interval for FS/LS core and 60 MHz"
28576 + "clock freq should be from 53640 to 65536\n");
28577 + return;
28578 + }
28579 + break;
28580 + default:
28581 + DWC_WARN("Unknown frame interval\n");
28582 + return;
28583 + break;
28584 +
28585 + }
28586 + hfir.b.frint = val;
28587 + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hfir, hfir.d32);
28588 +}
28589 +
28590 +uint32_t dwc_otg_get_mode_ch_tim(dwc_otg_core_if_t * core_if)
28591 +{
28592 + hcfg_data_t hcfg;
28593 + hcfg.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->hcfg);
28594 + return hcfg.b.modechtimen;
28595 +
28596 +}
28597 +
28598 +void dwc_otg_set_mode_ch_tim(dwc_otg_core_if_t * core_if, uint32_t val)
28599 +{
28600 + hcfg_data_t hcfg;
28601 + hcfg.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->hcfg);
28602 + hcfg.b.modechtimen = val;
28603 + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hcfg, hcfg.d32);
28604 +}
28605 +
28606 +void dwc_otg_set_prtresume(dwc_otg_core_if_t * core_if, uint32_t val)
28607 +{
28608 + hprt0_data_t hprt0;
28609 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
28610 + hprt0.b.prtres = val;
28611 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
28612 +}
28613 +
28614 +uint32_t dwc_otg_get_remotewakesig(dwc_otg_core_if_t * core_if)
28615 +{
28616 + dctl_data_t dctl;
28617 + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dctl);
28618 + return dctl.b.rmtwkupsig;
28619 +}
28620 +
28621 +uint32_t dwc_otg_get_lpm_portsleepstatus(dwc_otg_core_if_t * core_if)
28622 +{
28623 + glpmcfg_data_t lpmcfg;
28624 + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
28625 +
28626 + DWC_ASSERT(!
28627 + ((core_if->lx_state == DWC_OTG_L1) ^ lpmcfg.b.prt_sleep_sts),
28628 + "lx_state = %d, lmpcfg.prt_sleep_sts = %d\n",
28629 + core_if->lx_state, lpmcfg.b.prt_sleep_sts);
28630 +
28631 + return lpmcfg.b.prt_sleep_sts;
28632 +}
28633 +
28634 +uint32_t dwc_otg_get_lpm_remotewakeenabled(dwc_otg_core_if_t * core_if)
28635 +{
28636 + glpmcfg_data_t lpmcfg;
28637 + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
28638 + return lpmcfg.b.rem_wkup_en;
28639 +}
28640 +
28641 +uint32_t dwc_otg_get_lpmresponse(dwc_otg_core_if_t * core_if)
28642 +{
28643 + glpmcfg_data_t lpmcfg;
28644 + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
28645 + return lpmcfg.b.appl_resp;
28646 +}
28647 +
28648 +void dwc_otg_set_lpmresponse(dwc_otg_core_if_t * core_if, uint32_t val)
28649 +{
28650 + glpmcfg_data_t lpmcfg;
28651 + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
28652 + lpmcfg.b.appl_resp = val;
28653 + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg, lpmcfg.d32);
28654 +}
28655 +
28656 +uint32_t dwc_otg_get_hsic_connect(dwc_otg_core_if_t * core_if)
28657 +{
28658 + glpmcfg_data_t lpmcfg;
28659 + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
28660 + return lpmcfg.b.hsic_connect;
28661 +}
28662 +
28663 +void dwc_otg_set_hsic_connect(dwc_otg_core_if_t * core_if, uint32_t val)
28664 +{
28665 + glpmcfg_data_t lpmcfg;
28666 + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
28667 + lpmcfg.b.hsic_connect = val;
28668 + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg, lpmcfg.d32);
28669 +}
28670 +
28671 +uint32_t dwc_otg_get_inv_sel_hsic(dwc_otg_core_if_t * core_if)
28672 +{
28673 + glpmcfg_data_t lpmcfg;
28674 + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
28675 + return lpmcfg.b.inv_sel_hsic;
28676 +
28677 +}
28678 +
28679 +void dwc_otg_set_inv_sel_hsic(dwc_otg_core_if_t * core_if, uint32_t val)
28680 +{
28681 + glpmcfg_data_t lpmcfg;
28682 + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
28683 + lpmcfg.b.inv_sel_hsic = val;
28684 + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg, lpmcfg.d32);
28685 +}
28686 +
28687 +uint32_t dwc_otg_get_gotgctl(dwc_otg_core_if_t * core_if)
28688 +{
28689 + return DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
28690 +}
28691 +
28692 +void dwc_otg_set_gotgctl(dwc_otg_core_if_t * core_if, uint32_t val)
28693 +{
28694 + DWC_WRITE_REG32(&core_if->core_global_regs->gotgctl, val);
28695 +}
28696 +
28697 +uint32_t dwc_otg_get_gusbcfg(dwc_otg_core_if_t * core_if)
28698 +{
28699 + return DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
28700 +}
28701 +
28702 +void dwc_otg_set_gusbcfg(dwc_otg_core_if_t * core_if, uint32_t val)
28703 +{
28704 + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, val);
28705 +}
28706 +
28707 +uint32_t dwc_otg_get_grxfsiz(dwc_otg_core_if_t * core_if)
28708 +{
28709 + return DWC_READ_REG32(&core_if->core_global_regs->grxfsiz);
28710 +}
28711 +
28712 +void dwc_otg_set_grxfsiz(dwc_otg_core_if_t * core_if, uint32_t val)
28713 +{
28714 + DWC_WRITE_REG32(&core_if->core_global_regs->grxfsiz, val);
28715 +}
28716 +
28717 +uint32_t dwc_otg_get_gnptxfsiz(dwc_otg_core_if_t * core_if)
28718 +{
28719 + return DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz);
28720 +}
28721 +
28722 +void dwc_otg_set_gnptxfsiz(dwc_otg_core_if_t * core_if, uint32_t val)
28723 +{
28724 + DWC_WRITE_REG32(&core_if->core_global_regs->gnptxfsiz, val);
28725 +}
28726 +
28727 +uint32_t dwc_otg_get_gpvndctl(dwc_otg_core_if_t * core_if)
28728 +{
28729 + return DWC_READ_REG32(&core_if->core_global_regs->gpvndctl);
28730 +}
28731 +
28732 +void dwc_otg_set_gpvndctl(dwc_otg_core_if_t * core_if, uint32_t val)
28733 +{
28734 + DWC_WRITE_REG32(&core_if->core_global_regs->gpvndctl, val);
28735 +}
28736 +
28737 +uint32_t dwc_otg_get_ggpio(dwc_otg_core_if_t * core_if)
28738 +{
28739 + return DWC_READ_REG32(&core_if->core_global_regs->ggpio);
28740 +}
28741 +
28742 +void dwc_otg_set_ggpio(dwc_otg_core_if_t * core_if, uint32_t val)
28743 +{
28744 + DWC_WRITE_REG32(&core_if->core_global_regs->ggpio, val);
28745 +}
28746 +
28747 +uint32_t dwc_otg_get_hprt0(dwc_otg_core_if_t * core_if)
28748 +{
28749 + return DWC_READ_REG32(core_if->host_if->hprt0);
28750 +
28751 +}
28752 +
28753 +void dwc_otg_set_hprt0(dwc_otg_core_if_t * core_if, uint32_t val)
28754 +{
28755 + DWC_WRITE_REG32(core_if->host_if->hprt0, val);
28756 +}
28757 +
28758 +uint32_t dwc_otg_get_guid(dwc_otg_core_if_t * core_if)
28759 +{
28760 + return DWC_READ_REG32(&core_if->core_global_regs->guid);
28761 +}
28762 +
28763 +void dwc_otg_set_guid(dwc_otg_core_if_t * core_if, uint32_t val)
28764 +{
28765 + DWC_WRITE_REG32(&core_if->core_global_regs->guid, val);
28766 +}
28767 +
28768 +uint32_t dwc_otg_get_hptxfsiz(dwc_otg_core_if_t * core_if)
28769 +{
28770 + return DWC_READ_REG32(&core_if->core_global_regs->hptxfsiz);
28771 +}
28772 +
28773 +uint16_t dwc_otg_get_otg_version(dwc_otg_core_if_t * core_if)
28774 +{
28775 + return ((core_if->otg_ver == 1) ? (uint16_t)0x0200 : (uint16_t)0x0103);
28776 +}
28777 +
28778 +/**
28779 + * Start the SRP timer to detect when the SRP does not complete within
28780 + * 6 seconds.
28781 + *
28782 + * @param core_if the pointer to core_if strucure.
28783 + */
28784 +void dwc_otg_pcd_start_srp_timer(dwc_otg_core_if_t * core_if)
28785 +{
28786 + core_if->srp_timer_started = 1;
28787 + DWC_TIMER_SCHEDULE(core_if->srp_timer, 6000 /* 6 secs */ );
28788 +}
28789 +
28790 +void dwc_otg_initiate_srp(dwc_otg_core_if_t * core_if)
28791 +{
28792 + uint32_t *addr = (uint32_t *) & (core_if->core_global_regs->gotgctl);
28793 + gotgctl_data_t mem;
28794 + gotgctl_data_t val;
28795 +
28796 + val.d32 = DWC_READ_REG32(addr);
28797 + if (val.b.sesreq) {
28798 + DWC_ERROR("Session Request Already active!\n");
28799 + return;
28800 + }
28801 +
28802 + DWC_INFO("Session Request Initated\n"); //NOTICE
28803 + mem.d32 = DWC_READ_REG32(addr);
28804 + mem.b.sesreq = 1;
28805 + DWC_WRITE_REG32(addr, mem.d32);
28806 +
28807 + /* Start the SRP timer */
28808 + dwc_otg_pcd_start_srp_timer(core_if);
28809 + return;
28810 +}
28811 --- /dev/null
28812 +++ b/drivers/usb/host/dwc_otg/dwc_otg_cil.h
28813 @@ -0,0 +1,1464 @@
28814 +/* ==========================================================================
28815 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_cil.h $
28816 + * $Revision: #123 $
28817 + * $Date: 2012/08/10 $
28818 + * $Change: 2047372 $
28819 + *
28820 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
28821 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
28822 + * otherwise expressly agreed to in writing between Synopsys and you.
28823 + *
28824 + * The Software IS NOT an item of Licensed Software or Licensed Product under
28825 + * any End User Software License Agreement or Agreement for Licensed Product
28826 + * with Synopsys or any supplement thereto. You are permitted to use and
28827 + * redistribute this Software in source and binary forms, with or without
28828 + * modification, provided that redistributions of source code must retain this
28829 + * notice. You may not view, use, disclose, copy or distribute this file or
28830 + * any information contained herein except pursuant to this license grant from
28831 + * Synopsys. If you do not agree with this notice, including the disclaimer
28832 + * below, then you are not authorized to use the Software.
28833 + *
28834 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
28835 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28836 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28837 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
28838 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28839 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28840 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28841 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28842 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28843 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
28844 + * DAMAGE.
28845 + * ========================================================================== */
28846 +
28847 +#if !defined(__DWC_CIL_H__)
28848 +#define __DWC_CIL_H__
28849 +
28850 +#include "dwc_list.h"
28851 +#include "dwc_otg_dbg.h"
28852 +#include "dwc_otg_regs.h"
28853 +
28854 +#include "dwc_otg_core_if.h"
28855 +#include "dwc_otg_adp.h"
28856 +
28857 +/**
28858 + * @file
28859 + * This file contains the interface to the Core Interface Layer.
28860 + */
28861 +
28862 +#ifdef DWC_UTE_CFI
28863 +
28864 +#define MAX_DMA_DESCS_PER_EP 256
28865 +
28866 +/**
28867 + * Enumeration for the data buffer mode
28868 + */
28869 +typedef enum _data_buffer_mode {
28870 + BM_STANDARD = 0, /* data buffer is in normal mode */
28871 + BM_SG = 1, /* data buffer uses the scatter/gather mode */
28872 + BM_CONCAT = 2, /* data buffer uses the concatenation mode */
28873 + BM_CIRCULAR = 3, /* data buffer uses the circular DMA mode */
28874 + BM_ALIGN = 4 /* data buffer is in buffer alignment mode */
28875 +} data_buffer_mode_e;
28876 +#endif //DWC_UTE_CFI
28877 +
28878 +/** Macros defined for DWC OTG HW Release version */
28879 +
28880 +#define OTG_CORE_REV_2_60a 0x4F54260A
28881 +#define OTG_CORE_REV_2_71a 0x4F54271A
28882 +#define OTG_CORE_REV_2_72a 0x4F54272A
28883 +#define OTG_CORE_REV_2_80a 0x4F54280A
28884 +#define OTG_CORE_REV_2_81a 0x4F54281A
28885 +#define OTG_CORE_REV_2_90a 0x4F54290A
28886 +#define OTG_CORE_REV_2_91a 0x4F54291A
28887 +#define OTG_CORE_REV_2_92a 0x4F54292A
28888 +#define OTG_CORE_REV_2_93a 0x4F54293A
28889 +#define OTG_CORE_REV_2_94a 0x4F54294A
28890 +#define OTG_CORE_REV_3_00a 0x4F54300A
28891 +
28892 +/**
28893 + * Information for each ISOC packet.
28894 + */
28895 +typedef struct iso_pkt_info {
28896 + uint32_t offset;
28897 + uint32_t length;
28898 + int32_t status;
28899 +} iso_pkt_info_t;
28900 +
28901 +/**
28902 + * The <code>dwc_ep</code> structure represents the state of a single
28903 + * endpoint when acting in device mode. It contains the data items
28904 + * needed for an endpoint to be activated and transfer packets.
28905 + */
28906 +typedef struct dwc_ep {
28907 + /** EP number used for register address lookup */
28908 + uint8_t num;
28909 + /** EP direction 0 = OUT */
28910 + unsigned is_in:1;
28911 + /** EP active. */
28912 + unsigned active:1;
28913 +
28914 + /**
28915 + * Periodic Tx FIFO # for IN EPs For INTR EP set to 0 to use non-periodic
28916 + * Tx FIFO. If dedicated Tx FIFOs are enabled Tx FIFO # FOR IN EPs*/
28917 + unsigned tx_fifo_num:4;
28918 + /** EP type: 0 - Control, 1 - ISOC, 2 - BULK, 3 - INTR */
28919 + unsigned type:2;
28920 +#define DWC_OTG_EP_TYPE_CONTROL 0
28921 +#define DWC_OTG_EP_TYPE_ISOC 1
28922 +#define DWC_OTG_EP_TYPE_BULK 2
28923 +#define DWC_OTG_EP_TYPE_INTR 3
28924 +
28925 + /** DATA start PID for INTR and BULK EP */
28926 + unsigned data_pid_start:1;
28927 + /** Frame (even/odd) for ISOC EP */
28928 + unsigned even_odd_frame:1;
28929 + /** Max Packet bytes */
28930 + unsigned maxpacket:11;
28931 +
28932 + /** Max Transfer size */
28933 + uint32_t maxxfer;
28934 +
28935 + /** @name Transfer state */
28936 + /** @{ */
28937 +
28938 + /**
28939 + * Pointer to the beginning of the transfer buffer -- do not modify
28940 + * during transfer.
28941 + */
28942 +
28943 + dwc_dma_t dma_addr;
28944 +
28945 + dwc_dma_t dma_desc_addr;
28946 + dwc_otg_dev_dma_desc_t *desc_addr;
28947 +
28948 + uint8_t *start_xfer_buff;
28949 + /** pointer to the transfer buffer */
28950 + uint8_t *xfer_buff;
28951 + /** Number of bytes to transfer */
28952 + unsigned xfer_len:19;
28953 + /** Number of bytes transferred. */
28954 + unsigned xfer_count:19;
28955 + /** Sent ZLP */
28956 + unsigned sent_zlp:1;
28957 + /** Total len for control transfer */
28958 + unsigned total_len:19;
28959 +
28960 + /** stall clear flag */
28961 + unsigned stall_clear_flag:1;
28962 +
28963 + /** SETUP pkt cnt rollover flag for EP0 out*/
28964 + unsigned stp_rollover;
28965 +
28966 +#ifdef DWC_UTE_CFI
28967 + /* The buffer mode */
28968 + data_buffer_mode_e buff_mode;
28969 +
28970 + /* The chain of DMA descriptors.
28971 + * MAX_DMA_DESCS_PER_EP will be allocated for each active EP.
28972 + */
28973 + dwc_otg_dma_desc_t *descs;
28974 +
28975 + /* The DMA address of the descriptors chain start */
28976 + dma_addr_t descs_dma_addr;
28977 + /** This variable stores the length of the last enqueued request */
28978 + uint32_t cfi_req_len;
28979 +#endif //DWC_UTE_CFI
28980 +
28981 +/** Max DMA Descriptor count for any EP */
28982 +#define MAX_DMA_DESC_CNT 256
28983 + /** Allocated DMA Desc count */
28984 + uint32_t desc_cnt;
28985 +
28986 + /** bInterval */
28987 + uint32_t bInterval;
28988 + /** Next frame num to setup next ISOC transfer */
28989 + uint32_t frame_num;
28990 + /** Indicates SOF number overrun in DSTS */
28991 + uint8_t frm_overrun;
28992 +
28993 +#ifdef DWC_UTE_PER_IO
28994 + /** Next frame num for which will be setup DMA Desc */
28995 + uint32_t xiso_frame_num;
28996 + /** bInterval */
28997 + uint32_t xiso_bInterval;
28998 + /** Count of currently active transfers - shall be either 0 or 1 */
28999 + int xiso_active_xfers;
29000 + int xiso_queued_xfers;
29001 +#endif
29002 +#ifdef DWC_EN_ISOC
29003 + /**
29004 + * Variables specific for ISOC EPs
29005 + *
29006 + */
29007 + /** DMA addresses of ISOC buffers */
29008 + dwc_dma_t dma_addr0;
29009 + dwc_dma_t dma_addr1;
29010 +
29011 + dwc_dma_t iso_dma_desc_addr;
29012 + dwc_otg_dev_dma_desc_t *iso_desc_addr;
29013 +
29014 + /** pointer to the transfer buffers */
29015 + uint8_t *xfer_buff0;
29016 + uint8_t *xfer_buff1;
29017 +
29018 + /** number of ISOC Buffer is processing */
29019 + uint32_t proc_buf_num;
29020 + /** Interval of ISOC Buffer processing */
29021 + uint32_t buf_proc_intrvl;
29022 + /** Data size for regular frame */
29023 + uint32_t data_per_frame;
29024 +
29025 + /* todo - pattern data support is to be implemented in the future */
29026 + /** Data size for pattern frame */
29027 + uint32_t data_pattern_frame;
29028 + /** Frame number of pattern data */
29029 + uint32_t sync_frame;
29030 +
29031 + /** bInterval */
29032 + uint32_t bInterval;
29033 + /** ISO Packet number per frame */
29034 + uint32_t pkt_per_frm;
29035 + /** Next frame num for which will be setup DMA Desc */
29036 + uint32_t next_frame;
29037 + /** Number of packets per buffer processing */
29038 + uint32_t pkt_cnt;
29039 + /** Info for all isoc packets */
29040 + iso_pkt_info_t *pkt_info;
29041 + /** current pkt number */
29042 + uint32_t cur_pkt;
29043 + /** current pkt number */
29044 + uint8_t *cur_pkt_addr;
29045 + /** current pkt number */
29046 + uint32_t cur_pkt_dma_addr;
29047 +#endif /* DWC_EN_ISOC */
29048 +
29049 +/** @} */
29050 +} dwc_ep_t;
29051 +
29052 +/*
29053 + * Reasons for halting a host channel.
29054 + */
29055 +typedef enum dwc_otg_halt_status {
29056 + DWC_OTG_HC_XFER_NO_HALT_STATUS,
29057 + DWC_OTG_HC_XFER_COMPLETE,
29058 + DWC_OTG_HC_XFER_URB_COMPLETE,
29059 + DWC_OTG_HC_XFER_ACK,
29060 + DWC_OTG_HC_XFER_NAK,
29061 + DWC_OTG_HC_XFER_NYET,
29062 + DWC_OTG_HC_XFER_STALL,
29063 + DWC_OTG_HC_XFER_XACT_ERR,
29064 + DWC_OTG_HC_XFER_FRAME_OVERRUN,
29065 + DWC_OTG_HC_XFER_BABBLE_ERR,
29066 + DWC_OTG_HC_XFER_DATA_TOGGLE_ERR,
29067 + DWC_OTG_HC_XFER_AHB_ERR,
29068 + DWC_OTG_HC_XFER_PERIODIC_INCOMPLETE,
29069 + DWC_OTG_HC_XFER_URB_DEQUEUE
29070 +} dwc_otg_halt_status_e;
29071 +
29072 +/**
29073 + * Host channel descriptor. This structure represents the state of a single
29074 + * host channel when acting in host mode. It contains the data items needed to
29075 + * transfer packets to an endpoint via a host channel.
29076 + */
29077 +typedef struct dwc_hc {
29078 + /** Host channel number used for register address lookup */
29079 + uint8_t hc_num;
29080 +
29081 + /** Device to access */
29082 + unsigned dev_addr:7;
29083 +
29084 + /** EP to access */
29085 + unsigned ep_num:4;
29086 +
29087 + /** EP direction. 0: OUT, 1: IN */
29088 + unsigned ep_is_in:1;
29089 +
29090 + /**
29091 + * EP speed.
29092 + * One of the following values:
29093 + * - DWC_OTG_EP_SPEED_LOW
29094 + * - DWC_OTG_EP_SPEED_FULL
29095 + * - DWC_OTG_EP_SPEED_HIGH
29096 + */
29097 + unsigned speed:2;
29098 +#define DWC_OTG_EP_SPEED_LOW 0
29099 +#define DWC_OTG_EP_SPEED_FULL 1
29100 +#define DWC_OTG_EP_SPEED_HIGH 2
29101 +
29102 + /**
29103 + * Endpoint type.
29104 + * One of the following values:
29105 + * - DWC_OTG_EP_TYPE_CONTROL: 0
29106 + * - DWC_OTG_EP_TYPE_ISOC: 1
29107 + * - DWC_OTG_EP_TYPE_BULK: 2
29108 + * - DWC_OTG_EP_TYPE_INTR: 3
29109 + */
29110 + unsigned ep_type:2;
29111 +
29112 + /** Max packet size in bytes */
29113 + unsigned max_packet:11;
29114 +
29115 + /**
29116 + * PID for initial transaction.
29117 + * 0: DATA0,<br>
29118 + * 1: DATA2,<br>
29119 + * 2: DATA1,<br>
29120 + * 3: MDATA (non-Control EP),
29121 + * SETUP (Control EP)
29122 + */
29123 + unsigned data_pid_start:2;
29124 +#define DWC_OTG_HC_PID_DATA0 0
29125 +#define DWC_OTG_HC_PID_DATA2 1
29126 +#define DWC_OTG_HC_PID_DATA1 2
29127 +#define DWC_OTG_HC_PID_MDATA 3
29128 +#define DWC_OTG_HC_PID_SETUP 3
29129 +
29130 + /** Number of periodic transactions per (micro)frame */
29131 + unsigned multi_count:2;
29132 +
29133 + /** @name Transfer State */
29134 + /** @{ */
29135 +
29136 + /** Pointer to the current transfer buffer position. */
29137 + uint8_t *xfer_buff;
29138 + /**
29139 + * In Buffer DMA mode this buffer will be used
29140 + * if xfer_buff is not DWORD aligned.
29141 + */
29142 + dwc_dma_t align_buff;
29143 + /** Total number of bytes to transfer. */
29144 + uint32_t xfer_len;
29145 + /** Number of bytes transferred so far. */
29146 + uint32_t xfer_count;
29147 + /** Packet count at start of transfer.*/
29148 + uint16_t start_pkt_count;
29149 +
29150 + /**
29151 + * Flag to indicate whether the transfer has been started. Set to 1 if
29152 + * it has been started, 0 otherwise.
29153 + */
29154 + uint8_t xfer_started;
29155 +
29156 + /**
29157 + * Set to 1 to indicate that a PING request should be issued on this
29158 + * channel. If 0, process normally.
29159 + */
29160 + uint8_t do_ping;
29161 +
29162 + /**
29163 + * Set to 1 to indicate that the error count for this transaction is
29164 + * non-zero. Set to 0 if the error count is 0.
29165 + */
29166 + uint8_t error_state;
29167 +
29168 + /**
29169 + * Set to 1 to indicate that this channel should be halted the next
29170 + * time a request is queued for the channel. This is necessary in
29171 + * slave mode if no request queue space is available when an attempt
29172 + * is made to halt the channel.
29173 + */
29174 + uint8_t halt_on_queue;
29175 +
29176 + /**
29177 + * Set to 1 if the host channel has been halted, but the core is not
29178 + * finished flushing queued requests. Otherwise 0.
29179 + */
29180 + uint8_t halt_pending;
29181 +
29182 + /**
29183 + * Reason for halting the host channel.
29184 + */
29185 + dwc_otg_halt_status_e halt_status;
29186 +
29187 + /*
29188 + * Split settings for the host channel
29189 + */
29190 + uint8_t do_split; /**< Enable split for the channel */
29191 + uint8_t complete_split; /**< Enable complete split */
29192 + uint8_t hub_addr; /**< Address of high speed hub */
29193 +
29194 + uint8_t port_addr; /**< Port of the low/full speed device */
29195 + /** Split transaction position
29196 + * One of the following values:
29197 + * - DWC_HCSPLIT_XACTPOS_MID
29198 + * - DWC_HCSPLIT_XACTPOS_BEGIN
29199 + * - DWC_HCSPLIT_XACTPOS_END
29200 + * - DWC_HCSPLIT_XACTPOS_ALL */
29201 + uint8_t xact_pos;
29202 +
29203 + /** Set when the host channel does a short read. */
29204 + uint8_t short_read;
29205 +
29206 + /**
29207 + * Number of requests issued for this channel since it was assigned to
29208 + * the current transfer (not counting PINGs).
29209 + */
29210 + uint8_t requests;
29211 +
29212 + /**
29213 + * Queue Head for the transfer being processed by this channel.
29214 + */
29215 + struct dwc_otg_qh *qh;
29216 +
29217 + /** @} */
29218 +
29219 + /** Entry in list of host channels. */
29220 + DWC_CIRCLEQ_ENTRY(dwc_hc) hc_list_entry;
29221 +
29222 + /** @name Descriptor DMA support */
29223 + /** @{ */
29224 +
29225 + /** Number of Transfer Descriptors */
29226 + uint16_t ntd;
29227 +
29228 + /** Descriptor List DMA address */
29229 + dwc_dma_t desc_list_addr;
29230 +
29231 + /** Scheduling micro-frame bitmap. */
29232 + uint8_t schinfo;
29233 +
29234 + /** @} */
29235 +} dwc_hc_t;
29236 +
29237 +/**
29238 + * The following parameters may be specified when starting the module. These
29239 + * parameters define how the DWC_otg controller should be configured.
29240 + */
29241 +typedef struct dwc_otg_core_params {
29242 + int32_t opt;
29243 +
29244 + /**
29245 + * Specifies the OTG capabilities. The driver will automatically
29246 + * detect the value for this parameter if none is specified.
29247 + * 0 - HNP and SRP capable (default)
29248 + * 1 - SRP Only capable
29249 + * 2 - No HNP/SRP capable
29250 + */
29251 + int32_t otg_cap;
29252 +
29253 + /**
29254 + * Specifies whether to use slave or DMA mode for accessing the data
29255 + * FIFOs. The driver will automatically detect the value for this
29256 + * parameter if none is specified.
29257 + * 0 - Slave
29258 + * 1 - DMA (default, if available)
29259 + */
29260 + int32_t dma_enable;
29261 +
29262 + /**
29263 + * When DMA mode is enabled specifies whether to use address DMA or DMA
29264 + * Descriptor mode for accessing the data FIFOs in device mode. The driver
29265 + * will automatically detect the value for this if none is specified.
29266 + * 0 - address DMA
29267 + * 1 - DMA Descriptor(default, if available)
29268 + */
29269 + int32_t dma_desc_enable;
29270 + /** The DMA Burst size (applicable only for External DMA
29271 + * Mode). 1, 4, 8 16, 32, 64, 128, 256 (default 32)
29272 + */
29273 + int32_t dma_burst_size; /* Translate this to GAHBCFG values */
29274 +
29275 + /**
29276 + * Specifies the maximum speed of operation in host and device mode.
29277 + * The actual speed depends on the speed of the attached device and
29278 + * the value of phy_type. The actual speed depends on the speed of the
29279 + * attached device.
29280 + * 0 - High Speed (default)
29281 + * 1 - Full Speed
29282 + */
29283 + int32_t speed;
29284 + /** Specifies whether low power mode is supported when attached
29285 + * to a Full Speed or Low Speed device in host mode.
29286 + * 0 - Don't support low power mode (default)
29287 + * 1 - Support low power mode
29288 + */
29289 + int32_t host_support_fs_ls_low_power;
29290 +
29291 + /** Specifies the PHY clock rate in low power mode when connected to a
29292 + * Low Speed device in host mode. This parameter is applicable only if
29293 + * HOST_SUPPORT_FS_LS_LOW_POWER is enabled. If PHY_TYPE is set to FS
29294 + * then defaults to 6 MHZ otherwise 48 MHZ.
29295 + *
29296 + * 0 - 48 MHz
29297 + * 1 - 6 MHz
29298 + */
29299 + int32_t host_ls_low_power_phy_clk;
29300 +
29301 + /**
29302 + * 0 - Use cC FIFO size parameters
29303 + * 1 - Allow dynamic FIFO sizing (default)
29304 + */
29305 + int32_t enable_dynamic_fifo;
29306 +
29307 + /** Total number of 4-byte words in the data FIFO memory. This
29308 + * memory includes the Rx FIFO, non-periodic Tx FIFO, and periodic
29309 + * Tx FIFOs.
29310 + * 32 to 32768 (default 8192)
29311 + * Note: The total FIFO memory depth in the FPGA configuration is 8192.
29312 + */
29313 + int32_t data_fifo_size;
29314 +
29315 + /** Number of 4-byte words in the Rx FIFO in device mode when dynamic
29316 + * FIFO sizing is enabled.
29317 + * 16 to 32768 (default 1064)
29318 + */
29319 + int32_t dev_rx_fifo_size;
29320 +
29321 + /** Number of 4-byte words in the non-periodic Tx FIFO in device mode
29322 + * when dynamic FIFO sizing is enabled.
29323 + * 16 to 32768 (default 1024)
29324 + */
29325 + int32_t dev_nperio_tx_fifo_size;
29326 +
29327 + /** Number of 4-byte words in each of the periodic Tx FIFOs in device
29328 + * mode when dynamic FIFO sizing is enabled.
29329 + * 4 to 768 (default 256)
29330 + */
29331 + uint32_t dev_perio_tx_fifo_size[MAX_PERIO_FIFOS];
29332 +
29333 + /** Number of 4-byte words in the Rx FIFO in host mode when dynamic
29334 + * FIFO sizing is enabled.
29335 + * 16 to 32768 (default 1024)
29336 + */
29337 + int32_t host_rx_fifo_size;
29338 +
29339 + /** Number of 4-byte words in the non-periodic Tx FIFO in host mode
29340 + * when Dynamic FIFO sizing is enabled in the core.
29341 + * 16 to 32768 (default 1024)
29342 + */
29343 + int32_t host_nperio_tx_fifo_size;
29344 +
29345 + /** Number of 4-byte words in the host periodic Tx FIFO when dynamic
29346 + * FIFO sizing is enabled.
29347 + * 16 to 32768 (default 1024)
29348 + */
29349 + int32_t host_perio_tx_fifo_size;
29350 +
29351 + /** The maximum transfer size supported in bytes.
29352 + * 2047 to 65,535 (default 65,535)
29353 + */
29354 + int32_t max_transfer_size;
29355 +
29356 + /** The maximum number of packets in a transfer.
29357 + * 15 to 511 (default 511)
29358 + */
29359 + int32_t max_packet_count;
29360 +
29361 + /** The number of host channel registers to use.
29362 + * 1 to 16 (default 12)
29363 + * Note: The FPGA configuration supports a maximum of 12 host channels.
29364 + */
29365 + int32_t host_channels;
29366 +
29367 + /** The number of endpoints in addition to EP0 available for device
29368 + * mode operations.
29369 + * 1 to 15 (default 6 IN and OUT)
29370 + * Note: The FPGA configuration supports a maximum of 6 IN and OUT
29371 + * endpoints in addition to EP0.
29372 + */
29373 + int32_t dev_endpoints;
29374 +
29375 + /**
29376 + * Specifies the type of PHY interface to use. By default, the driver
29377 + * will automatically detect the phy_type.
29378 + *
29379 + * 0 - Full Speed PHY
29380 + * 1 - UTMI+ (default)
29381 + * 2 - ULPI
29382 + */
29383 + int32_t phy_type;
29384 +
29385 + /**
29386 + * Specifies the UTMI+ Data Width. This parameter is
29387 + * applicable for a PHY_TYPE of UTMI+ or ULPI. (For a ULPI
29388 + * PHY_TYPE, this parameter indicates the data width between
29389 + * the MAC and the ULPI Wrapper.) Also, this parameter is
29390 + * applicable only if the OTG_HSPHY_WIDTH cC parameter was set
29391 + * to "8 and 16 bits", meaning that the core has been
29392 + * configured to work at either data path width.
29393 + *
29394 + * 8 or 16 bits (default 16)
29395 + */
29396 + int32_t phy_utmi_width;
29397 +
29398 + /**
29399 + * Specifies whether the ULPI operates at double or single
29400 + * data rate. This parameter is only applicable if PHY_TYPE is
29401 + * ULPI.
29402 + *
29403 + * 0 - single data rate ULPI interface with 8 bit wide data
29404 + * bus (default)
29405 + * 1 - double data rate ULPI interface with 4 bit wide data
29406 + * bus
29407 + */
29408 + int32_t phy_ulpi_ddr;
29409 +
29410 + /**
29411 + * Specifies whether to use the internal or external supply to
29412 + * drive the vbus with a ULPI phy.
29413 + */
29414 + int32_t phy_ulpi_ext_vbus;
29415 +
29416 + /**
29417 + * Specifies whether to use the I2Cinterface for full speed PHY. This
29418 + * parameter is only applicable if PHY_TYPE is FS.
29419 + * 0 - No (default)
29420 + * 1 - Yes
29421 + */
29422 + int32_t i2c_enable;
29423 +
29424 + int32_t ulpi_fs_ls;
29425 +
29426 + int32_t ts_dline;
29427 +
29428 + /**
29429 + * Specifies whether dedicated transmit FIFOs are
29430 + * enabled for non periodic IN endpoints in device mode
29431 + * 0 - No
29432 + * 1 - Yes
29433 + */
29434 + int32_t en_multiple_tx_fifo;
29435 +
29436 + /** Number of 4-byte words in each of the Tx FIFOs in device
29437 + * mode when dynamic FIFO sizing is enabled.
29438 + * 4 to 768 (default 256)
29439 + */
29440 + uint32_t dev_tx_fifo_size[MAX_TX_FIFOS];
29441 +
29442 + /** Thresholding enable flag-
29443 + * bit 0 - enable non-ISO Tx thresholding
29444 + * bit 1 - enable ISO Tx thresholding
29445 + * bit 2 - enable Rx thresholding
29446 + */
29447 + uint32_t thr_ctl;
29448 +
29449 + /** Thresholding length for Tx
29450 + * FIFOs in 32 bit DWORDs
29451 + */
29452 + uint32_t tx_thr_length;
29453 +
29454 + /** Thresholding length for Rx
29455 + * FIFOs in 32 bit DWORDs
29456 + */
29457 + uint32_t rx_thr_length;
29458 +
29459 + /**
29460 + * Specifies whether LPM (Link Power Management) support is enabled
29461 + */
29462 + int32_t lpm_enable;
29463 +
29464 + /** Per Transfer Interrupt
29465 + * mode enable flag
29466 + * 1 - Enabled
29467 + * 0 - Disabled
29468 + */
29469 + int32_t pti_enable;
29470 +
29471 + /** Multi Processor Interrupt
29472 + * mode enable flag
29473 + * 1 - Enabled
29474 + * 0 - Disabled
29475 + */
29476 + int32_t mpi_enable;
29477 +
29478 + /** IS_USB Capability
29479 + * 1 - Enabled
29480 + * 0 - Disabled
29481 + */
29482 + int32_t ic_usb_cap;
29483 +
29484 + /** AHB Threshold Ratio
29485 + * 2'b00 AHB Threshold = MAC Threshold
29486 + * 2'b01 AHB Threshold = 1/2 MAC Threshold
29487 + * 2'b10 AHB Threshold = 1/4 MAC Threshold
29488 + * 2'b11 AHB Threshold = 1/8 MAC Threshold
29489 + */
29490 + int32_t ahb_thr_ratio;
29491 +
29492 + /** ADP Support
29493 + * 1 - Enabled
29494 + * 0 - Disabled
29495 + */
29496 + int32_t adp_supp_enable;
29497 +
29498 + /** HFIR Reload Control
29499 + * 0 - The HFIR cannot be reloaded dynamically.
29500 + * 1 - Allow dynamic reloading of the HFIR register during runtime.
29501 + */
29502 + int32_t reload_ctl;
29503 +
29504 + /** DCFG: Enable device Out NAK
29505 + * 0 - The core does not set NAK after Bulk Out transfer complete.
29506 + * 1 - The core sets NAK after Bulk OUT transfer complete.
29507 + */
29508 + int32_t dev_out_nak;
29509 +
29510 + /** DCFG: Enable Continue on BNA
29511 + * After receiving BNA interrupt the core disables the endpoint,when the
29512 + * endpoint is re-enabled by the application the core starts processing
29513 + * 0 - from the DOEPDMA descriptor
29514 + * 1 - from the descriptor which received the BNA.
29515 + */
29516 + int32_t cont_on_bna;
29517 +
29518 + /** GAHBCFG: AHB Single Support
29519 + * This bit when programmed supports SINGLE transfers for remainder
29520 + * data in a transfer for DMA mode of operation.
29521 + * 0 - in this case the remainder data will be sent using INCR burst size.
29522 + * 1 - in this case the remainder data will be sent using SINGLE burst size.
29523 + */
29524 + int32_t ahb_single;
29525 +
29526 + /** Core Power down mode
29527 + * 0 - No Power Down is enabled
29528 + * 1 - Reserved
29529 + * 2 - Complete Power Down (Hibernation)
29530 + */
29531 + int32_t power_down;
29532 +
29533 + /** OTG revision supported
29534 + * 0 - OTG 1.3 revision
29535 + * 1 - OTG 2.0 revision
29536 + */
29537 + int32_t otg_ver;
29538 +
29539 +} dwc_otg_core_params_t;
29540 +
29541 +#ifdef DEBUG
29542 +struct dwc_otg_core_if;
29543 +typedef struct hc_xfer_info {
29544 + struct dwc_otg_core_if *core_if;
29545 + dwc_hc_t *hc;
29546 +} hc_xfer_info_t;
29547 +#endif
29548 +
29549 +typedef struct ep_xfer_info {
29550 + struct dwc_otg_core_if *core_if;
29551 + dwc_ep_t *ep;
29552 + uint8_t state;
29553 +} ep_xfer_info_t;
29554 +/*
29555 + * Device States
29556 + */
29557 +typedef enum dwc_otg_lx_state {
29558 + /** On state */
29559 + DWC_OTG_L0,
29560 + /** LPM sleep state*/
29561 + DWC_OTG_L1,
29562 + /** USB suspend state*/
29563 + DWC_OTG_L2,
29564 + /** Off state*/
29565 + DWC_OTG_L3
29566 +} dwc_otg_lx_state_e;
29567 +
29568 +struct dwc_otg_global_regs_backup {
29569 + uint32_t gotgctl_local;
29570 + uint32_t gintmsk_local;
29571 + uint32_t gahbcfg_local;
29572 + uint32_t gusbcfg_local;
29573 + uint32_t grxfsiz_local;
29574 + uint32_t gnptxfsiz_local;
29575 +#ifdef CONFIG_USB_DWC_OTG_LPM
29576 + uint32_t glpmcfg_local;
29577 +#endif
29578 + uint32_t gi2cctl_local;
29579 + uint32_t hptxfsiz_local;
29580 + uint32_t pcgcctl_local;
29581 + uint32_t gdfifocfg_local;
29582 + uint32_t dtxfsiz_local[MAX_EPS_CHANNELS];
29583 + uint32_t gpwrdn_local;
29584 + uint32_t xhib_pcgcctl;
29585 + uint32_t xhib_gpwrdn;
29586 +};
29587 +
29588 +struct dwc_otg_host_regs_backup {
29589 + uint32_t hcfg_local;
29590 + uint32_t haintmsk_local;
29591 + uint32_t hcintmsk_local[MAX_EPS_CHANNELS];
29592 + uint32_t hprt0_local;
29593 + uint32_t hfir_local;
29594 +};
29595 +
29596 +struct dwc_otg_dev_regs_backup {
29597 + uint32_t dcfg;
29598 + uint32_t dctl;
29599 + uint32_t daintmsk;
29600 + uint32_t diepmsk;
29601 + uint32_t doepmsk;
29602 + uint32_t diepctl[MAX_EPS_CHANNELS];
29603 + uint32_t dieptsiz[MAX_EPS_CHANNELS];
29604 + uint32_t diepdma[MAX_EPS_CHANNELS];
29605 +};
29606 +/**
29607 + * The <code>dwc_otg_core_if</code> structure contains information needed to manage
29608 + * the DWC_otg controller acting in either host or device mode. It
29609 + * represents the programming view of the controller as a whole.
29610 + */
29611 +struct dwc_otg_core_if {
29612 + /** Parameters that define how the core should be configured.*/
29613 + dwc_otg_core_params_t *core_params;
29614 +
29615 + /** Core Global registers starting at offset 000h. */
29616 + dwc_otg_core_global_regs_t *core_global_regs;
29617 +
29618 + /** Device-specific information */
29619 + dwc_otg_dev_if_t *dev_if;
29620 + /** Host-specific information */
29621 + dwc_otg_host_if_t *host_if;
29622 +
29623 + /** Value from SNPSID register */
29624 + uint32_t snpsid;
29625 +
29626 + /*
29627 + * Set to 1 if the core PHY interface bits in USBCFG have been
29628 + * initialized.
29629 + */
29630 + uint8_t phy_init_done;
29631 +
29632 + /*
29633 + * SRP Success flag, set by srp success interrupt in FS I2C mode
29634 + */
29635 + uint8_t srp_success;
29636 + uint8_t srp_timer_started;
29637 + /** Timer for SRP. If it expires before SRP is successful
29638 + * clear the SRP. */
29639 + dwc_timer_t *srp_timer;
29640 +
29641 +#ifdef DWC_DEV_SRPCAP
29642 + /* This timer is needed to power on the hibernated host core if SRP is not
29643 + * initiated on connected SRP capable device for limited period of time
29644 + */
29645 + uint8_t pwron_timer_started;
29646 + dwc_timer_t *pwron_timer;
29647 +#endif
29648 + /* Common configuration information */
29649 + /** Power and Clock Gating Control Register */
29650 + volatile uint32_t *pcgcctl;
29651 +#define DWC_OTG_PCGCCTL_OFFSET 0xE00
29652 +
29653 + /** Push/pop addresses for endpoints or host channels.*/
29654 + uint32_t *data_fifo[MAX_EPS_CHANNELS];
29655 +#define DWC_OTG_DATA_FIFO_OFFSET 0x1000
29656 +#define DWC_OTG_DATA_FIFO_SIZE 0x1000
29657 +
29658 + /** Total RAM for FIFOs (Bytes) */
29659 + uint16_t total_fifo_size;
29660 + /** Size of Rx FIFO (Bytes) */
29661 + uint16_t rx_fifo_size;
29662 + /** Size of Non-periodic Tx FIFO (Bytes) */
29663 + uint16_t nperio_tx_fifo_size;
29664 +
29665 + /** 1 if DMA is enabled, 0 otherwise. */
29666 + uint8_t dma_enable;
29667 +
29668 + /** 1 if DMA descriptor is enabled, 0 otherwise. */
29669 + uint8_t dma_desc_enable;
29670 +
29671 + /** 1 if PTI Enhancement mode is enabled, 0 otherwise. */
29672 + uint8_t pti_enh_enable;
29673 +
29674 + /** 1 if MPI Enhancement mode is enabled, 0 otherwise. */
29675 + uint8_t multiproc_int_enable;
29676 +
29677 + /** 1 if dedicated Tx FIFOs are enabled, 0 otherwise. */
29678 + uint8_t en_multiple_tx_fifo;
29679 +
29680 + /** Set to 1 if multiple packets of a high-bandwidth transfer is in
29681 + * process of being queued */
29682 + uint8_t queuing_high_bandwidth;
29683 +
29684 + /** Hardware Configuration -- stored here for convenience.*/
29685 + hwcfg1_data_t hwcfg1;
29686 + hwcfg2_data_t hwcfg2;
29687 + hwcfg3_data_t hwcfg3;
29688 + hwcfg4_data_t hwcfg4;
29689 + fifosize_data_t hptxfsiz;
29690 +
29691 + /** Host and Device Configuration -- stored here for convenience.*/
29692 + hcfg_data_t hcfg;
29693 + dcfg_data_t dcfg;
29694 +
29695 + /** The operational State, during transations
29696 + * (a_host>>a_peripherial and b_device=>b_host) this may not
29697 + * match the core but allows the software to determine
29698 + * transitions.
29699 + */
29700 + uint8_t op_state;
29701 +
29702 + /**
29703 + * Set to 1 if the HCD needs to be restarted on a session request
29704 + * interrupt. This is required if no connector ID status change has
29705 + * occurred since the HCD was last disconnected.
29706 + */
29707 + uint8_t restart_hcd_on_session_req;
29708 +
29709 + /** HCD callbacks */
29710 + /** A-Device is a_host */
29711 +#define A_HOST (1)
29712 + /** A-Device is a_suspend */
29713 +#define A_SUSPEND (2)
29714 + /** A-Device is a_peripherial */
29715 +#define A_PERIPHERAL (3)
29716 + /** B-Device is operating as a Peripheral. */
29717 +#define B_PERIPHERAL (4)
29718 + /** B-Device is operating as a Host. */
29719 +#define B_HOST (5)
29720 +
29721 + /** HCD callbacks */
29722 + struct dwc_otg_cil_callbacks *hcd_cb;
29723 + /** PCD callbacks */
29724 + struct dwc_otg_cil_callbacks *pcd_cb;
29725 +
29726 + /** Device mode Periodic Tx FIFO Mask */
29727 + uint32_t p_tx_msk;
29728 + /** Device mode Periodic Tx FIFO Mask */
29729 + uint32_t tx_msk;
29730 +
29731 + /** Workqueue object used for handling several interrupts */
29732 + dwc_workq_t *wq_otg;
29733 +
29734 + /** Timer object used for handling "Wakeup Detected" Interrupt */
29735 + dwc_timer_t *wkp_timer;
29736 + /** This arrays used for debug purposes for DEV OUT NAK enhancement */
29737 + uint32_t start_doeptsiz_val[MAX_EPS_CHANNELS];
29738 + ep_xfer_info_t ep_xfer_info[MAX_EPS_CHANNELS];
29739 + dwc_timer_t *ep_xfer_timer[MAX_EPS_CHANNELS];
29740 +#ifdef DEBUG
29741 + uint32_t start_hcchar_val[MAX_EPS_CHANNELS];
29742 +
29743 + hc_xfer_info_t hc_xfer_info[MAX_EPS_CHANNELS];
29744 + dwc_timer_t *hc_xfer_timer[MAX_EPS_CHANNELS];
29745 +
29746 + uint32_t hfnum_7_samples;
29747 + uint64_t hfnum_7_frrem_accum;
29748 + uint32_t hfnum_0_samples;
29749 + uint64_t hfnum_0_frrem_accum;
29750 + uint32_t hfnum_other_samples;
29751 + uint64_t hfnum_other_frrem_accum;
29752 +#endif
29753 +
29754 +#ifdef DWC_UTE_CFI
29755 + uint16_t pwron_rxfsiz;
29756 + uint16_t pwron_gnptxfsiz;
29757 + uint16_t pwron_txfsiz[15];
29758 +
29759 + uint16_t init_rxfsiz;
29760 + uint16_t init_gnptxfsiz;
29761 + uint16_t init_txfsiz[15];
29762 +#endif
29763 +
29764 + /** Lx state of device */
29765 + dwc_otg_lx_state_e lx_state;
29766 +
29767 + /** Saved Core Global registers */
29768 + struct dwc_otg_global_regs_backup *gr_backup;
29769 + /** Saved Host registers */
29770 + struct dwc_otg_host_regs_backup *hr_backup;
29771 + /** Saved Device registers */
29772 + struct dwc_otg_dev_regs_backup *dr_backup;
29773 +
29774 + /** Power Down Enable */
29775 + uint32_t power_down;
29776 +
29777 + /** ADP support Enable */
29778 + uint32_t adp_enable;
29779 +
29780 + /** ADP structure object */
29781 + dwc_otg_adp_t adp;
29782 +
29783 + /** hibernation/suspend flag */
29784 + int hibernation_suspend;
29785 +
29786 + /** Device mode extended hibernation flag */
29787 + int xhib;
29788 +
29789 + /** OTG revision supported */
29790 + uint32_t otg_ver;
29791 +
29792 + /** OTG status flag used for HNP polling */
29793 + uint8_t otg_sts;
29794 +
29795 + /** Pointer to either hcd->lock or pcd->lock */
29796 + dwc_spinlock_t *lock;
29797 +
29798 + /** Start predict NextEP based on Learning Queue if equal 1,
29799 + * also used as counter of disabled NP IN EP's */
29800 + uint8_t start_predict;
29801 +
29802 + /** NextEp sequence, including EP0: nextep_seq[] = EP if non-periodic and
29803 + * active, 0xff otherwise */
29804 + uint8_t nextep_seq[MAX_EPS_CHANNELS];
29805 +
29806 + /** Index of fisrt EP in nextep_seq array which should be re-enabled **/
29807 + uint8_t first_in_nextep_seq;
29808 +
29809 + /** Frame number while entering to ISR - needed for ISOCs **/
29810 + uint32_t frame_num;
29811 +
29812 +};
29813 +
29814 +#ifdef DEBUG
29815 +/*
29816 + * This function is called when transfer is timed out.
29817 + */
29818 +extern void hc_xfer_timeout(void *ptr);
29819 +#endif
29820 +
29821 +/*
29822 + * This function is called when transfer is timed out on endpoint.
29823 + */
29824 +extern void ep_xfer_timeout(void *ptr);
29825 +
29826 +/*
29827 + * The following functions are functions for works
29828 + * using during handling some interrupts
29829 + */
29830 +extern void w_conn_id_status_change(void *p);
29831 +
29832 +extern void w_wakeup_detected(void *p);
29833 +
29834 +/** Saves global register values into system memory. */
29835 +extern int dwc_otg_save_global_regs(dwc_otg_core_if_t * core_if);
29836 +/** Saves device register values into system memory. */
29837 +extern int dwc_otg_save_dev_regs(dwc_otg_core_if_t * core_if);
29838 +/** Saves host register values into system memory. */
29839 +extern int dwc_otg_save_host_regs(dwc_otg_core_if_t * core_if);
29840 +/** Restore global register values. */
29841 +extern int dwc_otg_restore_global_regs(dwc_otg_core_if_t * core_if);
29842 +/** Restore host register values. */
29843 +extern int dwc_otg_restore_host_regs(dwc_otg_core_if_t * core_if, int reset);
29844 +/** Restore device register values. */
29845 +extern int dwc_otg_restore_dev_regs(dwc_otg_core_if_t * core_if,
29846 + int rem_wakeup);
29847 +extern int restore_lpm_i2c_regs(dwc_otg_core_if_t * core_if);
29848 +extern int restore_essential_regs(dwc_otg_core_if_t * core_if, int rmode,
29849 + int is_host);
29850 +
29851 +extern int dwc_otg_host_hibernation_restore(dwc_otg_core_if_t * core_if,
29852 + int restore_mode, int reset);
29853 +extern int dwc_otg_device_hibernation_restore(dwc_otg_core_if_t * core_if,
29854 + int rem_wakeup, int reset);
29855 +
29856 +/*
29857 + * The following functions support initialization of the CIL driver component
29858 + * and the DWC_otg controller.
29859 + */
29860 +extern void dwc_otg_core_host_init(dwc_otg_core_if_t * _core_if);
29861 +extern void dwc_otg_core_dev_init(dwc_otg_core_if_t * _core_if);
29862 +
29863 +/** @name Device CIL Functions
29864 + * The following functions support managing the DWC_otg controller in device
29865 + * mode.
29866 + */
29867 +/**@{*/
29868 +extern void dwc_otg_wakeup(dwc_otg_core_if_t * _core_if);
29869 +extern void dwc_otg_read_setup_packet(dwc_otg_core_if_t * _core_if,
29870 + uint32_t * _dest);
29871 +extern uint32_t dwc_otg_get_frame_number(dwc_otg_core_if_t * _core_if);
29872 +extern void dwc_otg_ep0_activate(dwc_otg_core_if_t * _core_if, dwc_ep_t * _ep);
29873 +extern void dwc_otg_ep_activate(dwc_otg_core_if_t * _core_if, dwc_ep_t * _ep);
29874 +extern void dwc_otg_ep_deactivate(dwc_otg_core_if_t * _core_if, dwc_ep_t * _ep);
29875 +extern void dwc_otg_ep_start_transfer(dwc_otg_core_if_t * _core_if,
29876 + dwc_ep_t * _ep);
29877 +extern void dwc_otg_ep_start_zl_transfer(dwc_otg_core_if_t * _core_if,
29878 + dwc_ep_t * _ep);
29879 +extern void dwc_otg_ep0_start_transfer(dwc_otg_core_if_t * _core_if,
29880 + dwc_ep_t * _ep);
29881 +extern void dwc_otg_ep0_continue_transfer(dwc_otg_core_if_t * _core_if,
29882 + dwc_ep_t * _ep);
29883 +extern void dwc_otg_ep_write_packet(dwc_otg_core_if_t * _core_if,
29884 + dwc_ep_t * _ep, int _dma);
29885 +extern void dwc_otg_ep_set_stall(dwc_otg_core_if_t * _core_if, dwc_ep_t * _ep);
29886 +extern void dwc_otg_ep_clear_stall(dwc_otg_core_if_t * _core_if,
29887 + dwc_ep_t * _ep);
29888 +extern void dwc_otg_enable_device_interrupts(dwc_otg_core_if_t * _core_if);
29889 +
29890 +#ifdef DWC_EN_ISOC
29891 +extern void dwc_otg_iso_ep_start_frm_transfer(dwc_otg_core_if_t * core_if,
29892 + dwc_ep_t * ep);
29893 +extern void dwc_otg_iso_ep_start_buf_transfer(dwc_otg_core_if_t * core_if,
29894 + dwc_ep_t * ep);
29895 +#endif /* DWC_EN_ISOC */
29896 +/**@}*/
29897 +
29898 +/** @name Host CIL Functions
29899 + * The following functions support managing the DWC_otg controller in host
29900 + * mode.
29901 + */
29902 +/**@{*/
29903 +extern void dwc_otg_hc_init(dwc_otg_core_if_t * _core_if, dwc_hc_t * _hc);
29904 +extern void dwc_otg_hc_halt(dwc_otg_core_if_t * _core_if,
29905 + dwc_hc_t * _hc, dwc_otg_halt_status_e _halt_status);
29906 +extern void dwc_otg_hc_cleanup(dwc_otg_core_if_t * _core_if, dwc_hc_t * _hc);
29907 +extern void dwc_otg_hc_start_transfer(dwc_otg_core_if_t * _core_if,
29908 + dwc_hc_t * _hc);
29909 +extern int dwc_otg_hc_continue_transfer(dwc_otg_core_if_t * _core_if,
29910 + dwc_hc_t * _hc);
29911 +extern void dwc_otg_hc_do_ping(dwc_otg_core_if_t * _core_if, dwc_hc_t * _hc);
29912 +extern void dwc_otg_hc_write_packet(dwc_otg_core_if_t * _core_if,
29913 + dwc_hc_t * _hc);
29914 +extern void dwc_otg_enable_host_interrupts(dwc_otg_core_if_t * _core_if);
29915 +extern void dwc_otg_disable_host_interrupts(dwc_otg_core_if_t * _core_if);
29916 +
29917 +extern void dwc_otg_hc_start_transfer_ddma(dwc_otg_core_if_t * core_if,
29918 + dwc_hc_t * hc);
29919 +
29920 +extern uint32_t calc_frame_interval(dwc_otg_core_if_t * core_if);
29921 +
29922 +/* Macro used to clear one channel interrupt */
29923 +#define clear_hc_int(_hc_regs_, _intr_) \
29924 +do { \
29925 + hcint_data_t hcint_clear = {.d32 = 0}; \
29926 + hcint_clear.b._intr_ = 1; \
29927 + DWC_WRITE_REG32(&(_hc_regs_)->hcint, hcint_clear.d32); \
29928 +} while (0)
29929 +
29930 +/*
29931 + * Macro used to disable one channel interrupt. Channel interrupts are
29932 + * disabled when the channel is halted or released by the interrupt handler.
29933 + * There is no need to handle further interrupts of that type until the
29934 + * channel is re-assigned. In fact, subsequent handling may cause crashes
29935 + * because the channel structures are cleaned up when the channel is released.
29936 + */
29937 +#define disable_hc_int(_hc_regs_, _intr_) \
29938 +do { \
29939 + hcintmsk_data_t hcintmsk = {.d32 = 0}; \
29940 + hcintmsk.b._intr_ = 1; \
29941 + DWC_MODIFY_REG32(&(_hc_regs_)->hcintmsk, hcintmsk.d32, 0); \
29942 +} while (0)
29943 +
29944 +/**
29945 + * This function Reads HPRT0 in preparation to modify. It keeps the
29946 + * WC bits 0 so that if they are read as 1, they won't clear when you
29947 + * write it back
29948 + */
29949 +static inline uint32_t dwc_otg_read_hprt0(dwc_otg_core_if_t * _core_if)
29950 +{
29951 + hprt0_data_t hprt0;
29952 + hprt0.d32 = DWC_READ_REG32(_core_if->host_if->hprt0);
29953 + hprt0.b.prtena = 0;
29954 + hprt0.b.prtconndet = 0;
29955 + hprt0.b.prtenchng = 0;
29956 + hprt0.b.prtovrcurrchng = 0;
29957 + return hprt0.d32;
29958 +}
29959 +
29960 +/**@}*/
29961 +
29962 +/** @name Common CIL Functions
29963 + * The following functions support managing the DWC_otg controller in either
29964 + * device or host mode.
29965 + */
29966 +/**@{*/
29967 +
29968 +extern void dwc_otg_read_packet(dwc_otg_core_if_t * core_if,
29969 + uint8_t * dest, uint16_t bytes);
29970 +
29971 +extern void dwc_otg_flush_tx_fifo(dwc_otg_core_if_t * _core_if, const int _num);
29972 +extern void dwc_otg_flush_rx_fifo(dwc_otg_core_if_t * _core_if);
29973 +extern void dwc_otg_core_reset(dwc_otg_core_if_t * _core_if);
29974 +
29975 +/**
29976 + * This function returns the Core Interrupt register.
29977 + */
29978 +static inline uint32_t dwc_otg_read_core_intr(dwc_otg_core_if_t * core_if)
29979 +{
29980 + return (DWC_READ_REG32(&core_if->core_global_regs->gintsts) &
29981 + DWC_READ_REG32(&core_if->core_global_regs->gintmsk));
29982 +}
29983 +
29984 +/**
29985 + * This function returns the OTG Interrupt register.
29986 + */
29987 +static inline uint32_t dwc_otg_read_otg_intr(dwc_otg_core_if_t * core_if)
29988 +{
29989 + return (DWC_READ_REG32(&core_if->core_global_regs->gotgint));
29990 +}
29991 +
29992 +/**
29993 + * This function reads the Device All Endpoints Interrupt register and
29994 + * returns the IN endpoint interrupt bits.
29995 + */
29996 +static inline uint32_t dwc_otg_read_dev_all_in_ep_intr(dwc_otg_core_if_t *
29997 + core_if)
29998 +{
29999 +
30000 + uint32_t v;
30001 +
30002 + if (core_if->multiproc_int_enable) {
30003 + v = DWC_READ_REG32(&core_if->dev_if->
30004 + dev_global_regs->deachint) &
30005 + DWC_READ_REG32(&core_if->
30006 + dev_if->dev_global_regs->deachintmsk);
30007 + } else {
30008 + v = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->daint) &
30009 + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->daintmsk);
30010 + }
30011 + return (v & 0xffff);
30012 +}
30013 +
30014 +/**
30015 + * This function reads the Device All Endpoints Interrupt register and
30016 + * returns the OUT endpoint interrupt bits.
30017 + */
30018 +static inline uint32_t dwc_otg_read_dev_all_out_ep_intr(dwc_otg_core_if_t *
30019 + core_if)
30020 +{
30021 + uint32_t v;
30022 +
30023 + if (core_if->multiproc_int_enable) {
30024 + v = DWC_READ_REG32(&core_if->dev_if->
30025 + dev_global_regs->deachint) &
30026 + DWC_READ_REG32(&core_if->
30027 + dev_if->dev_global_regs->deachintmsk);
30028 + } else {
30029 + v = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->daint) &
30030 + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->daintmsk);
30031 + }
30032 +
30033 + return ((v & 0xffff0000) >> 16);
30034 +}
30035 +
30036 +/**
30037 + * This function returns the Device IN EP Interrupt register
30038 + */
30039 +static inline uint32_t dwc_otg_read_dev_in_ep_intr(dwc_otg_core_if_t * core_if,
30040 + dwc_ep_t * ep)
30041 +{
30042 + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
30043 + uint32_t v, msk, emp;
30044 +
30045 + if (core_if->multiproc_int_enable) {
30046 + msk =
30047 + DWC_READ_REG32(&dev_if->
30048 + dev_global_regs->diepeachintmsk[ep->num]);
30049 + emp =
30050 + DWC_READ_REG32(&dev_if->
30051 + dev_global_regs->dtknqr4_fifoemptymsk);
30052 + msk |= ((emp >> ep->num) & 0x1) << 7;
30053 + v = DWC_READ_REG32(&dev_if->in_ep_regs[ep->num]->diepint) & msk;
30054 + } else {
30055 + msk = DWC_READ_REG32(&dev_if->dev_global_regs->diepmsk);
30056 + emp =
30057 + DWC_READ_REG32(&dev_if->
30058 + dev_global_regs->dtknqr4_fifoemptymsk);
30059 + msk |= ((emp >> ep->num) & 0x1) << 7;
30060 + v = DWC_READ_REG32(&dev_if->in_ep_regs[ep->num]->diepint) & msk;
30061 + }
30062 +
30063 + return v;
30064 +}
30065 +
30066 +/**
30067 + * This function returns the Device OUT EP Interrupt register
30068 + */
30069 +static inline uint32_t dwc_otg_read_dev_out_ep_intr(dwc_otg_core_if_t *
30070 + _core_if, dwc_ep_t * _ep)
30071 +{
30072 + dwc_otg_dev_if_t *dev_if = _core_if->dev_if;
30073 + uint32_t v;
30074 + doepmsk_data_t msk = {.d32 = 0 };
30075 +
30076 + if (_core_if->multiproc_int_enable) {
30077 + msk.d32 =
30078 + DWC_READ_REG32(&dev_if->
30079 + dev_global_regs->doepeachintmsk[_ep->num]);
30080 + if (_core_if->pti_enh_enable) {
30081 + msk.b.pktdrpsts = 1;
30082 + }
30083 + v = DWC_READ_REG32(&dev_if->
30084 + out_ep_regs[_ep->num]->doepint) & msk.d32;
30085 + } else {
30086 + msk.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->doepmsk);
30087 + if (_core_if->pti_enh_enable) {
30088 + msk.b.pktdrpsts = 1;
30089 + }
30090 + v = DWC_READ_REG32(&dev_if->
30091 + out_ep_regs[_ep->num]->doepint) & msk.d32;
30092 + }
30093 + return v;
30094 +}
30095 +
30096 +/**
30097 + * This function returns the Host All Channel Interrupt register
30098 + */
30099 +static inline uint32_t dwc_otg_read_host_all_channels_intr(dwc_otg_core_if_t *
30100 + _core_if)
30101 +{
30102 + return (DWC_READ_REG32(&_core_if->host_if->host_global_regs->haint));
30103 +}
30104 +
30105 +static inline uint32_t dwc_otg_read_host_channel_intr(dwc_otg_core_if_t *
30106 + _core_if, dwc_hc_t * _hc)
30107 +{
30108 + return (DWC_READ_REG32
30109 + (&_core_if->host_if->hc_regs[_hc->hc_num]->hcint));
30110 +}
30111 +
30112 +/**
30113 + * This function returns the mode of the operation, host or device.
30114 + *
30115 + * @return 0 - Device Mode, 1 - Host Mode
30116 + */
30117 +static inline uint32_t dwc_otg_mode(dwc_otg_core_if_t * _core_if)
30118 +{
30119 + return (DWC_READ_REG32(&_core_if->core_global_regs->gintsts) & 0x1);
30120 +}
30121 +
30122 +/**@}*/
30123 +
30124 +/**
30125 + * DWC_otg CIL callback structure. This structure allows the HCD and
30126 + * PCD to register functions used for starting and stopping the PCD
30127 + * and HCD for role change on for a DRD.
30128 + */
30129 +typedef struct dwc_otg_cil_callbacks {
30130 + /** Start function for role change */
30131 + int (*start) (void *_p);
30132 + /** Stop Function for role change */
30133 + int (*stop) (void *_p);
30134 + /** Disconnect Function for role change */
30135 + int (*disconnect) (void *_p);
30136 + /** Resume/Remote wakeup Function */
30137 + int (*resume_wakeup) (void *_p);
30138 + /** Suspend function */
30139 + int (*suspend) (void *_p);
30140 + /** Session Start (SRP) */
30141 + int (*session_start) (void *_p);
30142 +#ifdef CONFIG_USB_DWC_OTG_LPM
30143 + /** Sleep (switch to L0 state) */
30144 + int (*sleep) (void *_p);
30145 +#endif
30146 + /** Pointer passed to start() and stop() */
30147 + void *p;
30148 +} dwc_otg_cil_callbacks_t;
30149 +
30150 +extern void dwc_otg_cil_register_pcd_callbacks(dwc_otg_core_if_t * _core_if,
30151 + dwc_otg_cil_callbacks_t * _cb,
30152 + void *_p);
30153 +extern void dwc_otg_cil_register_hcd_callbacks(dwc_otg_core_if_t * _core_if,
30154 + dwc_otg_cil_callbacks_t * _cb,
30155 + void *_p);
30156 +
30157 +void dwc_otg_initiate_srp(dwc_otg_core_if_t * core_if);
30158 +
30159 +//////////////////////////////////////////////////////////////////////
30160 +/** Start the HCD. Helper function for using the HCD callbacks.
30161 + *
30162 + * @param core_if Programming view of DWC_otg controller.
30163 + */
30164 +static inline void cil_hcd_start(dwc_otg_core_if_t * core_if)
30165 +{
30166 + if (core_if->hcd_cb && core_if->hcd_cb->start) {
30167 + core_if->hcd_cb->start(core_if->hcd_cb->p);
30168 + }
30169 +}
30170 +
30171 +/** Stop the HCD. Helper function for using the HCD callbacks.
30172 + *
30173 + * @param core_if Programming view of DWC_otg controller.
30174 + */
30175 +static inline void cil_hcd_stop(dwc_otg_core_if_t * core_if)
30176 +{
30177 + if (core_if->hcd_cb && core_if->hcd_cb->stop) {
30178 + core_if->hcd_cb->stop(core_if->hcd_cb->p);
30179 + }
30180 +}
30181 +
30182 +/** Disconnect the HCD. Helper function for using the HCD callbacks.
30183 + *
30184 + * @param core_if Programming view of DWC_otg controller.
30185 + */
30186 +static inline void cil_hcd_disconnect(dwc_otg_core_if_t * core_if)
30187 +{
30188 + if (core_if->hcd_cb && core_if->hcd_cb->disconnect) {
30189 + core_if->hcd_cb->disconnect(core_if->hcd_cb->p);
30190 + }
30191 +}
30192 +
30193 +/** Inform the HCD the a New Session has begun. Helper function for
30194 + * using the HCD callbacks.
30195 + *
30196 + * @param core_if Programming view of DWC_otg controller.
30197 + */
30198 +static inline void cil_hcd_session_start(dwc_otg_core_if_t * core_if)
30199 +{
30200 + if (core_if->hcd_cb && core_if->hcd_cb->session_start) {
30201 + core_if->hcd_cb->session_start(core_if->hcd_cb->p);
30202 + }
30203 +}
30204 +
30205 +#ifdef CONFIG_USB_DWC_OTG_LPM
30206 +/**
30207 + * Inform the HCD about LPM sleep.
30208 + * Helper function for using the HCD callbacks.
30209 + *
30210 + * @param core_if Programming view of DWC_otg controller.
30211 + */
30212 +static inline void cil_hcd_sleep(dwc_otg_core_if_t * core_if)
30213 +{
30214 + if (core_if->hcd_cb && core_if->hcd_cb->sleep) {
30215 + core_if->hcd_cb->sleep(core_if->hcd_cb->p);
30216 + }
30217 +}
30218 +#endif
30219 +
30220 +/** Resume the HCD. Helper function for using the HCD callbacks.
30221 + *
30222 + * @param core_if Programming view of DWC_otg controller.
30223 + */
30224 +static inline void cil_hcd_resume(dwc_otg_core_if_t * core_if)
30225 +{
30226 + if (core_if->hcd_cb && core_if->hcd_cb->resume_wakeup) {
30227 + core_if->hcd_cb->resume_wakeup(core_if->hcd_cb->p);
30228 + }
30229 +}
30230 +
30231 +/** Start the PCD. Helper function for using the PCD callbacks.
30232 + *
30233 + * @param core_if Programming view of DWC_otg controller.
30234 + */
30235 +static inline void cil_pcd_start(dwc_otg_core_if_t * core_if)
30236 +{
30237 + if (core_if->pcd_cb && core_if->pcd_cb->start) {
30238 + core_if->pcd_cb->start(core_if->pcd_cb->p);
30239 + }
30240 +}
30241 +
30242 +/** Stop the PCD. Helper function for using the PCD callbacks.
30243 + *
30244 + * @param core_if Programming view of DWC_otg controller.
30245 + */
30246 +static inline void cil_pcd_stop(dwc_otg_core_if_t * core_if)
30247 +{
30248 + if (core_if->pcd_cb && core_if->pcd_cb->stop) {
30249 + core_if->pcd_cb->stop(core_if->pcd_cb->p);
30250 + }
30251 +}
30252 +
30253 +/** Suspend the PCD. Helper function for using the PCD callbacks.
30254 + *
30255 + * @param core_if Programming view of DWC_otg controller.
30256 + */
30257 +static inline void cil_pcd_suspend(dwc_otg_core_if_t * core_if)
30258 +{
30259 + if (core_if->pcd_cb && core_if->pcd_cb->suspend) {
30260 + core_if->pcd_cb->suspend(core_if->pcd_cb->p);
30261 + }
30262 +}
30263 +
30264 +/** Resume the PCD. Helper function for using the PCD callbacks.
30265 + *
30266 + * @param core_if Programming view of DWC_otg controller.
30267 + */
30268 +static inline void cil_pcd_resume(dwc_otg_core_if_t * core_if)
30269 +{
30270 + if (core_if->pcd_cb && core_if->pcd_cb->resume_wakeup) {
30271 + core_if->pcd_cb->resume_wakeup(core_if->pcd_cb->p);
30272 + }
30273 +}
30274 +
30275 +//////////////////////////////////////////////////////////////////////
30276 +
30277 +#endif
30278 --- /dev/null
30279 +++ b/drivers/usb/host/dwc_otg/dwc_otg_cil_intr.c
30280 @@ -0,0 +1,1596 @@
30281 +/* ==========================================================================
30282 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_cil_intr.c $
30283 + * $Revision: #32 $
30284 + * $Date: 2012/08/10 $
30285 + * $Change: 2047372 $
30286 + *
30287 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
30288 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
30289 + * otherwise expressly agreed to in writing between Synopsys and you.
30290 + *
30291 + * The Software IS NOT an item of Licensed Software or Licensed Product under
30292 + * any End User Software License Agreement or Agreement for Licensed Product
30293 + * with Synopsys or any supplement thereto. You are permitted to use and
30294 + * redistribute this Software in source and binary forms, with or without
30295 + * modification, provided that redistributions of source code must retain this
30296 + * notice. You may not view, use, disclose, copy or distribute this file or
30297 + * any information contained herein except pursuant to this license grant from
30298 + * Synopsys. If you do not agree with this notice, including the disclaimer
30299 + * below, then you are not authorized to use the Software.
30300 + *
30301 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
30302 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30303 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30304 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
30305 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30306 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30307 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30308 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30309 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30310 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
30311 + * DAMAGE.
30312 + * ========================================================================== */
30313 +
30314 +/** @file
30315 + *
30316 + * The Core Interface Layer provides basic services for accessing and
30317 + * managing the DWC_otg hardware. These services are used by both the
30318 + * Host Controller Driver and the Peripheral Controller Driver.
30319 + *
30320 + * This file contains the Common Interrupt handlers.
30321 + */
30322 +#include "dwc_os.h"
30323 +#include "dwc_otg_regs.h"
30324 +#include "dwc_otg_cil.h"
30325 +#include "dwc_otg_driver.h"
30326 +#include "dwc_otg_pcd.h"
30327 +#include "dwc_otg_hcd.h"
30328 +
30329 +#ifdef DEBUG
30330 +inline const char *op_state_str(dwc_otg_core_if_t * core_if)
30331 +{
30332 + return (core_if->op_state == A_HOST ? "a_host" :
30333 + (core_if->op_state == A_SUSPEND ? "a_suspend" :
30334 + (core_if->op_state == A_PERIPHERAL ? "a_peripheral" :
30335 + (core_if->op_state == B_PERIPHERAL ? "b_peripheral" :
30336 + (core_if->op_state == B_HOST ? "b_host" : "unknown")))));
30337 +}
30338 +#endif
30339 +
30340 +/** This function will log a debug message
30341 + *
30342 + * @param core_if Programming view of DWC_otg controller.
30343 + */
30344 +int32_t dwc_otg_handle_mode_mismatch_intr(dwc_otg_core_if_t * core_if)
30345 +{
30346 + gintsts_data_t gintsts;
30347 + DWC_WARN("Mode Mismatch Interrupt: currently in %s mode\n",
30348 + dwc_otg_mode(core_if) ? "Host" : "Device");
30349 +
30350 + /* Clear interrupt */
30351 + gintsts.d32 = 0;
30352 + gintsts.b.modemismatch = 1;
30353 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
30354 + return 1;
30355 +}
30356 +
30357 +/**
30358 + * This function handles the OTG Interrupts. It reads the OTG
30359 + * Interrupt Register (GOTGINT) to determine what interrupt has
30360 + * occurred.
30361 + *
30362 + * @param core_if Programming view of DWC_otg controller.
30363 + */
30364 +int32_t dwc_otg_handle_otg_intr(dwc_otg_core_if_t * core_if)
30365 +{
30366 + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
30367 + gotgint_data_t gotgint;
30368 + gotgctl_data_t gotgctl;
30369 + gintmsk_data_t gintmsk;
30370 + gpwrdn_data_t gpwrdn;
30371 +
30372 + gotgint.d32 = DWC_READ_REG32(&global_regs->gotgint);
30373 + gotgctl.d32 = DWC_READ_REG32(&global_regs->gotgctl);
30374 + DWC_DEBUGPL(DBG_CIL, "++OTG Interrupt gotgint=%0x [%s]\n", gotgint.d32,
30375 + op_state_str(core_if));
30376 +
30377 + if (gotgint.b.sesenddet) {
30378 + DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: "
30379 + "Session End Detected++ (%s)\n",
30380 + op_state_str(core_if));
30381 + gotgctl.d32 = DWC_READ_REG32(&global_regs->gotgctl);
30382 +
30383 + if (core_if->op_state == B_HOST) {
30384 + cil_pcd_start(core_if);
30385 + core_if->op_state = B_PERIPHERAL;
30386 + } else {
30387 + /* If not B_HOST and Device HNP still set. HNP
30388 + * Did not succeed!*/
30389 + if (gotgctl.b.devhnpen) {
30390 + DWC_DEBUGPL(DBG_ANY, "Session End Detected\n");
30391 + __DWC_ERROR("Device Not Connected/Responding!\n");
30392 + }
30393 +
30394 + /* If Session End Detected the B-Cable has
30395 + * been disconnected. */
30396 + /* Reset PCD and Gadget driver to a
30397 + * clean state. */
30398 + core_if->lx_state = DWC_OTG_L0;
30399 + DWC_SPINUNLOCK(core_if->lock);
30400 + cil_pcd_stop(core_if);
30401 + DWC_SPINLOCK(core_if->lock);
30402 +
30403 + if (core_if->adp_enable) {
30404 + if (core_if->power_down == 2) {
30405 + gpwrdn.d32 = 0;
30406 + gpwrdn.b.pwrdnswtch = 1;
30407 + DWC_MODIFY_REG32(&core_if->
30408 + core_global_regs->
30409 + gpwrdn, gpwrdn.d32, 0);
30410 + }
30411 +
30412 + gpwrdn.d32 = 0;
30413 + gpwrdn.b.pmuintsel = 1;
30414 + gpwrdn.b.pmuactv = 1;
30415 + DWC_MODIFY_REG32(&core_if->core_global_regs->
30416 + gpwrdn, 0, gpwrdn.d32);
30417 +
30418 + dwc_otg_adp_sense_start(core_if);
30419 + }
30420 + }
30421 +
30422 + gotgctl.d32 = 0;
30423 + gotgctl.b.devhnpen = 1;
30424 + DWC_MODIFY_REG32(&global_regs->gotgctl, gotgctl.d32, 0);
30425 + }
30426 + if (gotgint.b.sesreqsucstschng) {
30427 + DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: "
30428 + "Session Reqeust Success Status Change++\n");
30429 + gotgctl.d32 = DWC_READ_REG32(&global_regs->gotgctl);
30430 + if (gotgctl.b.sesreqscs) {
30431 +
30432 + if ((core_if->core_params->phy_type ==
30433 + DWC_PHY_TYPE_PARAM_FS) && (core_if->core_params->i2c_enable)) {
30434 + core_if->srp_success = 1;
30435 + } else {
30436 + DWC_SPINUNLOCK(core_if->lock);
30437 + cil_pcd_resume(core_if);
30438 + DWC_SPINLOCK(core_if->lock);
30439 + /* Clear Session Request */
30440 + gotgctl.d32 = 0;
30441 + gotgctl.b.sesreq = 1;
30442 + DWC_MODIFY_REG32(&global_regs->gotgctl,
30443 + gotgctl.d32, 0);
30444 + }
30445 + }
30446 + }
30447 + if (gotgint.b.hstnegsucstschng) {
30448 + /* Print statements during the HNP interrupt handling
30449 + * can cause it to fail.*/
30450 + gotgctl.d32 = DWC_READ_REG32(&global_regs->gotgctl);
30451 + /* WA for 3.00a- HW is not setting cur_mode, even sometimes
30452 + * this does not help*/
30453 + if (core_if->snpsid >= OTG_CORE_REV_3_00a)
30454 + dwc_udelay(100);
30455 + if (gotgctl.b.hstnegscs) {
30456 + if (dwc_otg_is_host_mode(core_if)) {
30457 + core_if->op_state = B_HOST;
30458 + /*
30459 + * Need to disable SOF interrupt immediately.
30460 + * When switching from device to host, the PCD
30461 + * interrupt handler won't handle the
30462 + * interrupt if host mode is already set. The
30463 + * HCD interrupt handler won't get called if
30464 + * the HCD state is HALT. This means that the
30465 + * interrupt does not get handled and Linux
30466 + * complains loudly.
30467 + */
30468 + gintmsk.d32 = 0;
30469 + gintmsk.b.sofintr = 1;
30470 + DWC_MODIFY_REG32(&global_regs->gintmsk,
30471 + gintmsk.d32, 0);
30472 + /* Call callback function with spin lock released */
30473 + DWC_SPINUNLOCK(core_if->lock);
30474 + cil_pcd_stop(core_if);
30475 + /*
30476 + * Initialize the Core for Host mode.
30477 + */
30478 + cil_hcd_start(core_if);
30479 + DWC_SPINLOCK(core_if->lock);
30480 + core_if->op_state = B_HOST;
30481 + }
30482 + } else {
30483 + gotgctl.d32 = 0;
30484 + gotgctl.b.hnpreq = 1;
30485 + gotgctl.b.devhnpen = 1;
30486 + DWC_MODIFY_REG32(&global_regs->gotgctl, gotgctl.d32, 0);
30487 + DWC_DEBUGPL(DBG_ANY, "HNP Failed\n");
30488 + __DWC_ERROR("Device Not Connected/Responding\n");
30489 + }
30490 + }
30491 + if (gotgint.b.hstnegdet) {
30492 + /* The disconnect interrupt is set at the same time as
30493 + * Host Negotiation Detected. During the mode
30494 + * switch all interrupts are cleared so the disconnect
30495 + * interrupt handler will not get executed.
30496 + */
30497 + DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: "
30498 + "Host Negotiation Detected++ (%s)\n",
30499 + (dwc_otg_is_host_mode(core_if) ? "Host" :
30500 + "Device"));
30501 + if (dwc_otg_is_device_mode(core_if)) {
30502 + DWC_DEBUGPL(DBG_ANY, "a_suspend->a_peripheral (%d)\n",
30503 + core_if->op_state);
30504 + DWC_SPINUNLOCK(core_if->lock);
30505 + cil_hcd_disconnect(core_if);
30506 + cil_pcd_start(core_if);
30507 + DWC_SPINLOCK(core_if->lock);
30508 + core_if->op_state = A_PERIPHERAL;
30509 + } else {
30510 + /*
30511 + * Need to disable SOF interrupt immediately. When
30512 + * switching from device to host, the PCD interrupt
30513 + * handler won't handle the interrupt if host mode is
30514 + * already set. The HCD interrupt handler won't get
30515 + * called if the HCD state is HALT. This means that
30516 + * the interrupt does not get handled and Linux
30517 + * complains loudly.
30518 + */
30519 + gintmsk.d32 = 0;
30520 + gintmsk.b.sofintr = 1;
30521 + DWC_MODIFY_REG32(&global_regs->gintmsk, gintmsk.d32, 0);
30522 + DWC_SPINUNLOCK(core_if->lock);
30523 + cil_pcd_stop(core_if);
30524 + cil_hcd_start(core_if);
30525 + DWC_SPINLOCK(core_if->lock);
30526 + core_if->op_state = A_HOST;
30527 + }
30528 + }
30529 + if (gotgint.b.adevtoutchng) {
30530 + DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: "
30531 + "A-Device Timeout Change++\n");
30532 + }
30533 + if (gotgint.b.debdone) {
30534 + DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: " "Debounce Done++\n");
30535 + }
30536 +
30537 + /* Clear GOTGINT */
30538 + DWC_WRITE_REG32(&core_if->core_global_regs->gotgint, gotgint.d32);
30539 +
30540 + return 1;
30541 +}
30542 +
30543 +void w_conn_id_status_change(void *p)
30544 +{
30545 + dwc_otg_core_if_t *core_if = p;
30546 + uint32_t count = 0;
30547 + gotgctl_data_t gotgctl = {.d32 = 0 };
30548 +
30549 + gotgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
30550 + DWC_DEBUGPL(DBG_CIL, "gotgctl=%0x\n", gotgctl.d32);
30551 + DWC_DEBUGPL(DBG_CIL, "gotgctl.b.conidsts=%d\n", gotgctl.b.conidsts);
30552 +
30553 + /* B-Device connector (Device Mode) */
30554 + if (gotgctl.b.conidsts) {
30555 + /* Wait for switch to device mode. */
30556 + while (!dwc_otg_is_device_mode(core_if)) {
30557 + DWC_PRINTF("Waiting for Peripheral Mode, Mode=%s\n",
30558 + (dwc_otg_is_host_mode(core_if) ? "Host" :
30559 + "Peripheral"));
30560 + dwc_mdelay(100);
30561 + if (++count > 10000)
30562 + break;
30563 + }
30564 + DWC_ASSERT(++count < 10000,
30565 + "Connection id status change timed out");
30566 + core_if->op_state = B_PERIPHERAL;
30567 + dwc_otg_core_init(core_if);
30568 + dwc_otg_enable_global_interrupts(core_if);
30569 + cil_pcd_start(core_if);
30570 + } else {
30571 + /* A-Device connector (Host Mode) */
30572 + while (!dwc_otg_is_host_mode(core_if)) {
30573 + DWC_PRINTF("Waiting for Host Mode, Mode=%s\n",
30574 + (dwc_otg_is_host_mode(core_if) ? "Host" :
30575 + "Peripheral"));
30576 + dwc_mdelay(100);
30577 + if (++count > 10000)
30578 + break;
30579 + }
30580 + DWC_ASSERT(++count < 10000,
30581 + "Connection id status change timed out");
30582 + core_if->op_state = A_HOST;
30583 + /*
30584 + * Initialize the Core for Host mode.
30585 + */
30586 + dwc_otg_core_init(core_if);
30587 + dwc_otg_enable_global_interrupts(core_if);
30588 + cil_hcd_start(core_if);
30589 + }
30590 +}
30591 +
30592 +/**
30593 + * This function handles the Connector ID Status Change Interrupt. It
30594 + * reads the OTG Interrupt Register (GOTCTL) to determine whether this
30595 + * is a Device to Host Mode transition or a Host Mode to Device
30596 + * Transition.
30597 + *
30598 + * This only occurs when the cable is connected/removed from the PHY
30599 + * connector.
30600 + *
30601 + * @param core_if Programming view of DWC_otg controller.
30602 + */
30603 +int32_t dwc_otg_handle_conn_id_status_change_intr(dwc_otg_core_if_t * core_if)
30604 +{
30605 +
30606 + /*
30607 + * Need to disable SOF interrupt immediately. If switching from device
30608 + * to host, the PCD interrupt handler won't handle the interrupt if
30609 + * host mode is already set. The HCD interrupt handler won't get
30610 + * called if the HCD state is HALT. This means that the interrupt does
30611 + * not get handled and Linux complains loudly.
30612 + */
30613 + gintmsk_data_t gintmsk = {.d32 = 0 };
30614 + gintsts_data_t gintsts = {.d32 = 0 };
30615 +
30616 + gintmsk.b.sofintr = 1;
30617 + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, gintmsk.d32, 0);
30618 +
30619 + DWC_DEBUGPL(DBG_CIL,
30620 + " ++Connector ID Status Change Interrupt++ (%s)\n",
30621 + (dwc_otg_is_host_mode(core_if) ? "Host" : "Device"));
30622 +
30623 + DWC_SPINUNLOCK(core_if->lock);
30624 +
30625 + /*
30626 + * Need to schedule a work, as there are possible DELAY function calls
30627 + * Release lock before scheduling workq as it holds spinlock during scheduling
30628 + */
30629 +
30630 + DWC_WORKQ_SCHEDULE(core_if->wq_otg, w_conn_id_status_change,
30631 + core_if, "connection id status change");
30632 + DWC_SPINLOCK(core_if->lock);
30633 +
30634 + /* Set flag and clear interrupt */
30635 + gintsts.b.conidstschng = 1;
30636 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
30637 +
30638 + return 1;
30639 +}
30640 +
30641 +/**
30642 + * This interrupt indicates that a device is initiating the Session
30643 + * Request Protocol to request the host to turn on bus power so a new
30644 + * session can begin. The handler responds by turning on bus power. If
30645 + * the DWC_otg controller is in low power mode, the handler brings the
30646 + * controller out of low power mode before turning on bus power.
30647 + *
30648 + * @param core_if Programming view of DWC_otg controller.
30649 + */
30650 +int32_t dwc_otg_handle_session_req_intr(dwc_otg_core_if_t * core_if)
30651 +{
30652 + gintsts_data_t gintsts;
30653 +
30654 +#ifndef DWC_HOST_ONLY
30655 + DWC_DEBUGPL(DBG_ANY, "++Session Request Interrupt++\n");
30656 +
30657 + if (dwc_otg_is_device_mode(core_if)) {
30658 + DWC_PRINTF("SRP: Device mode\n");
30659 + } else {
30660 + hprt0_data_t hprt0;
30661 + DWC_PRINTF("SRP: Host mode\n");
30662 +
30663 + /* Turn on the port power bit. */
30664 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
30665 + hprt0.b.prtpwr = 1;
30666 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
30667 +
30668 + /* Start the Connection timer. So a message can be displayed
30669 + * if connect does not occur within 10 seconds. */
30670 + cil_hcd_session_start(core_if);
30671 + }
30672 +#endif
30673 +
30674 + /* Clear interrupt */
30675 + gintsts.d32 = 0;
30676 + gintsts.b.sessreqintr = 1;
30677 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
30678 +
30679 + return 1;
30680 +}
30681 +
30682 +void w_wakeup_detected(void *p)
30683 +{
30684 + dwc_otg_core_if_t *core_if = (dwc_otg_core_if_t *) p;
30685 + /*
30686 + * Clear the Resume after 70ms. (Need 20 ms minimum. Use 70 ms
30687 + * so that OPT tests pass with all PHYs).
30688 + */
30689 + hprt0_data_t hprt0 = {.d32 = 0 };
30690 +#if 0
30691 + pcgcctl_data_t pcgcctl = {.d32 = 0 };
30692 + /* Restart the Phy Clock */
30693 + pcgcctl.b.stoppclk = 1;
30694 + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
30695 + dwc_udelay(10);
30696 +#endif //0
30697 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
30698 + DWC_DEBUGPL(DBG_ANY, "Resume: HPRT0=%0x\n", hprt0.d32);
30699 +// dwc_mdelay(70);
30700 + hprt0.b.prtres = 0; /* Resume */
30701 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
30702 + DWC_DEBUGPL(DBG_ANY, "Clear Resume: HPRT0=%0x\n",
30703 + DWC_READ_REG32(core_if->host_if->hprt0));
30704 +
30705 + cil_hcd_resume(core_if);
30706 +
30707 + /** Change to L0 state*/
30708 + core_if->lx_state = DWC_OTG_L0;
30709 +}
30710 +
30711 +/**
30712 + * This interrupt indicates that the DWC_otg controller has detected a
30713 + * resume or remote wakeup sequence. If the DWC_otg controller is in
30714 + * low power mode, the handler must brings the controller out of low
30715 + * power mode. The controller automatically begins resume
30716 + * signaling. The handler schedules a time to stop resume signaling.
30717 + */
30718 +int32_t dwc_otg_handle_wakeup_detected_intr(dwc_otg_core_if_t * core_if)
30719 +{
30720 + gintsts_data_t gintsts;
30721 +
30722 + DWC_DEBUGPL(DBG_ANY,
30723 + "++Resume and Remote Wakeup Detected Interrupt++\n");
30724 +
30725 + DWC_PRINTF("%s lxstate = %d\n", __func__, core_if->lx_state);
30726 +
30727 + if (dwc_otg_is_device_mode(core_if)) {
30728 + dctl_data_t dctl = {.d32 = 0 };
30729 + DWC_DEBUGPL(DBG_PCD, "DSTS=0x%0x\n",
30730 + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->
30731 + dsts));
30732 + if (core_if->lx_state == DWC_OTG_L2) {
30733 +#ifdef PARTIAL_POWER_DOWN
30734 + if (core_if->hwcfg4.b.power_optimiz) {
30735 + pcgcctl_data_t power = {.d32 = 0 };
30736 +
30737 + power.d32 = DWC_READ_REG32(core_if->pcgcctl);
30738 + DWC_DEBUGPL(DBG_CIL, "PCGCCTL=%0x\n",
30739 + power.d32);
30740 +
30741 + power.b.stoppclk = 0;
30742 + DWC_WRITE_REG32(core_if->pcgcctl, power.d32);
30743 +
30744 + power.b.pwrclmp = 0;
30745 + DWC_WRITE_REG32(core_if->pcgcctl, power.d32);
30746 +
30747 + power.b.rstpdwnmodule = 0;
30748 + DWC_WRITE_REG32(core_if->pcgcctl, power.d32);
30749 + }
30750 +#endif
30751 + /* Clear the Remote Wakeup Signaling */
30752 + dctl.b.rmtwkupsig = 1;
30753 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
30754 + dctl, dctl.d32, 0);
30755 +
30756 + DWC_SPINUNLOCK(core_if->lock);
30757 + if (core_if->pcd_cb && core_if->pcd_cb->resume_wakeup) {
30758 + core_if->pcd_cb->resume_wakeup(core_if->pcd_cb->p);
30759 + }
30760 + DWC_SPINLOCK(core_if->lock);
30761 + } else {
30762 + glpmcfg_data_t lpmcfg;
30763 + lpmcfg.d32 =
30764 + DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
30765 + lpmcfg.b.hird_thres &= (~(1 << 4));
30766 + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg,
30767 + lpmcfg.d32);
30768 + }
30769 + /** Change to L0 state*/
30770 + core_if->lx_state = DWC_OTG_L0;
30771 + } else {
30772 + if (core_if->lx_state != DWC_OTG_L1) {
30773 + pcgcctl_data_t pcgcctl = {.d32 = 0 };
30774 +
30775 + /* Restart the Phy Clock */
30776 + pcgcctl.b.stoppclk = 1;
30777 + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
30778 + DWC_TIMER_SCHEDULE(core_if->wkp_timer, 71);
30779 + } else {
30780 + /** Change to L0 state*/
30781 + core_if->lx_state = DWC_OTG_L0;
30782 + }
30783 + }
30784 +
30785 + /* Clear interrupt */
30786 + gintsts.d32 = 0;
30787 + gintsts.b.wkupintr = 1;
30788 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
30789 +
30790 + return 1;
30791 +}
30792 +
30793 +/**
30794 + * This interrupt indicates that the Wakeup Logic has detected a
30795 + * Device disconnect.
30796 + */
30797 +static int32_t dwc_otg_handle_pwrdn_disconnect_intr(dwc_otg_core_if_t *core_if)
30798 +{
30799 + gpwrdn_data_t gpwrdn = { .d32 = 0 };
30800 + gpwrdn_data_t gpwrdn_temp = { .d32 = 0 };
30801 + gpwrdn_temp.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
30802 +
30803 + DWC_PRINTF("%s called\n", __FUNCTION__);
30804 +
30805 + if (!core_if->hibernation_suspend) {
30806 + DWC_PRINTF("Already exited from Hibernation\n");
30807 + return 1;
30808 + }
30809 +
30810 + /* Switch on the voltage to the core */
30811 + gpwrdn.b.pwrdnswtch = 1;
30812 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
30813 + dwc_udelay(10);
30814 +
30815 + /* Reset the core */
30816 + gpwrdn.d32 = 0;
30817 + gpwrdn.b.pwrdnrstn = 1;
30818 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
30819 + dwc_udelay(10);
30820 +
30821 + /* Disable power clamps*/
30822 + gpwrdn.d32 = 0;
30823 + gpwrdn.b.pwrdnclmp = 1;
30824 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
30825 +
30826 + /* Remove reset the core signal */
30827 + gpwrdn.d32 = 0;
30828 + gpwrdn.b.pwrdnrstn = 1;
30829 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
30830 + dwc_udelay(10);
30831 +
30832 + /* Disable PMU interrupt */
30833 + gpwrdn.d32 = 0;
30834 + gpwrdn.b.pmuintsel = 1;
30835 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
30836 +
30837 + core_if->hibernation_suspend = 0;
30838 +
30839 + /* Disable PMU */
30840 + gpwrdn.d32 = 0;
30841 + gpwrdn.b.pmuactv = 1;
30842 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
30843 + dwc_udelay(10);
30844 +
30845 + if (gpwrdn_temp.b.idsts) {
30846 + core_if->op_state = B_PERIPHERAL;
30847 + dwc_otg_core_init(core_if);
30848 + dwc_otg_enable_global_interrupts(core_if);
30849 + cil_pcd_start(core_if);
30850 + } else {
30851 + core_if->op_state = A_HOST;
30852 + dwc_otg_core_init(core_if);
30853 + dwc_otg_enable_global_interrupts(core_if);
30854 + cil_hcd_start(core_if);
30855 + }
30856 +
30857 + return 1;
30858 +}
30859 +
30860 +/**
30861 + * This interrupt indicates that the Wakeup Logic has detected a
30862 + * remote wakeup sequence.
30863 + */
30864 +static int32_t dwc_otg_handle_pwrdn_wakeup_detected_intr(dwc_otg_core_if_t * core_if)
30865 +{
30866 + gpwrdn_data_t gpwrdn = {.d32 = 0 };
30867 + DWC_DEBUGPL(DBG_ANY,
30868 + "++Powerdown Remote Wakeup Detected Interrupt++\n");
30869 +
30870 + if (!core_if->hibernation_suspend) {
30871 + DWC_PRINTF("Already exited from Hibernation\n");
30872 + return 1;
30873 + }
30874 +
30875 + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
30876 + if (gpwrdn.b.idsts) { // Device Mode
30877 + if ((core_if->power_down == 2)
30878 + && (core_if->hibernation_suspend == 1)) {
30879 + dwc_otg_device_hibernation_restore(core_if, 0, 0);
30880 + }
30881 + } else {
30882 + if ((core_if->power_down == 2)
30883 + && (core_if->hibernation_suspend == 1)) {
30884 + dwc_otg_host_hibernation_restore(core_if, 1, 0);
30885 + }
30886 + }
30887 + return 1;
30888 +}
30889 +
30890 +static int32_t dwc_otg_handle_pwrdn_idsts_change(dwc_otg_device_t *otg_dev)
30891 +{
30892 + gpwrdn_data_t gpwrdn = {.d32 = 0 };
30893 + gpwrdn_data_t gpwrdn_temp = {.d32 = 0 };
30894 + dwc_otg_core_if_t *core_if = otg_dev->core_if;
30895 +
30896 + DWC_DEBUGPL(DBG_ANY, "%s called\n", __FUNCTION__);
30897 + gpwrdn_temp.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
30898 + if (core_if->power_down == 2) {
30899 + if (!core_if->hibernation_suspend) {
30900 + DWC_PRINTF("Already exited from Hibernation\n");
30901 + return 1;
30902 + }
30903 + DWC_DEBUGPL(DBG_ANY, "Exit from hibernation on ID sts change\n");
30904 + /* Switch on the voltage to the core */
30905 + gpwrdn.b.pwrdnswtch = 1;
30906 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
30907 + dwc_udelay(10);
30908 +
30909 + /* Reset the core */
30910 + gpwrdn.d32 = 0;
30911 + gpwrdn.b.pwrdnrstn = 1;
30912 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
30913 + dwc_udelay(10);
30914 +
30915 + /* Disable power clamps */
30916 + gpwrdn.d32 = 0;
30917 + gpwrdn.b.pwrdnclmp = 1;
30918 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
30919 +
30920 + /* Remove reset the core signal */
30921 + gpwrdn.d32 = 0;
30922 + gpwrdn.b.pwrdnrstn = 1;
30923 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
30924 + dwc_udelay(10);
30925 +
30926 + /* Disable PMU interrupt */
30927 + gpwrdn.d32 = 0;
30928 + gpwrdn.b.pmuintsel = 1;
30929 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
30930 +
30931 + /*Indicates that we are exiting from hibernation */
30932 + core_if->hibernation_suspend = 0;
30933 +
30934 + /* Disable PMU */
30935 + gpwrdn.d32 = 0;
30936 + gpwrdn.b.pmuactv = 1;
30937 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
30938 + dwc_udelay(10);
30939 +
30940 + gpwrdn.d32 = core_if->gr_backup->gpwrdn_local;
30941 + if (gpwrdn.b.dis_vbus == 1) {
30942 + gpwrdn.d32 = 0;
30943 + gpwrdn.b.dis_vbus = 1;
30944 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
30945 + }
30946 +
30947 + if (gpwrdn_temp.b.idsts) {
30948 + core_if->op_state = B_PERIPHERAL;
30949 + dwc_otg_core_init(core_if);
30950 + dwc_otg_enable_global_interrupts(core_if);
30951 + cil_pcd_start(core_if);
30952 + } else {
30953 + core_if->op_state = A_HOST;
30954 + dwc_otg_core_init(core_if);
30955 + dwc_otg_enable_global_interrupts(core_if);
30956 + cil_hcd_start(core_if);
30957 + }
30958 + }
30959 +
30960 + if (core_if->adp_enable) {
30961 + uint8_t is_host = 0;
30962 + DWC_SPINUNLOCK(core_if->lock);
30963 + /* Change the core_if's lock to hcd/pcd lock depend on mode? */
30964 +#ifndef DWC_HOST_ONLY
30965 + if (gpwrdn_temp.b.idsts)
30966 + core_if->lock = otg_dev->pcd->lock;
30967 +#endif
30968 +#ifndef DWC_DEVICE_ONLY
30969 + if (!gpwrdn_temp.b.idsts) {
30970 + core_if->lock = otg_dev->hcd->lock;
30971 + is_host = 1;
30972 + }
30973 +#endif
30974 + DWC_PRINTF("RESTART ADP\n");
30975 + if (core_if->adp.probe_enabled)
30976 + dwc_otg_adp_probe_stop(core_if);
30977 + if (core_if->adp.sense_enabled)
30978 + dwc_otg_adp_sense_stop(core_if);
30979 + if (core_if->adp.sense_timer_started)
30980 + DWC_TIMER_CANCEL(core_if->adp.sense_timer);
30981 + if (core_if->adp.vbuson_timer_started)
30982 + DWC_TIMER_CANCEL(core_if->adp.vbuson_timer);
30983 + core_if->adp.probe_timer_values[0] = -1;
30984 + core_if->adp.probe_timer_values[1] = -1;
30985 + core_if->adp.sense_timer_started = 0;
30986 + core_if->adp.vbuson_timer_started = 0;
30987 + core_if->adp.probe_counter = 0;
30988 + core_if->adp.gpwrdn = 0;
30989 +
30990 + /* Disable PMU and restart ADP */
30991 + gpwrdn_temp.d32 = 0;
30992 + gpwrdn_temp.b.pmuactv = 1;
30993 + gpwrdn_temp.b.pmuintsel = 1;
30994 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
30995 + DWC_PRINTF("Check point 1\n");
30996 + dwc_mdelay(110);
30997 + dwc_otg_adp_start(core_if, is_host);
30998 + DWC_SPINLOCK(core_if->lock);
30999 + }
31000 +
31001 +
31002 + return 1;
31003 +}
31004 +
31005 +static int32_t dwc_otg_handle_pwrdn_session_change(dwc_otg_core_if_t * core_if)
31006 +{
31007 + gpwrdn_data_t gpwrdn = {.d32 = 0 };
31008 + int32_t otg_cap_param = core_if->core_params->otg_cap;
31009 + DWC_DEBUGPL(DBG_ANY, "%s called\n", __FUNCTION__);
31010 +
31011 + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
31012 + if (core_if->power_down == 2) {
31013 + if (!core_if->hibernation_suspend) {
31014 + DWC_PRINTF("Already exited from Hibernation\n");
31015 + return 1;
31016 + }
31017 +
31018 + if ((otg_cap_param != DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE ||
31019 + otg_cap_param != DWC_OTG_CAP_PARAM_SRP_ONLY_CAPABLE) &&
31020 + gpwrdn.b.bsessvld == 0) {
31021 + /* Save gpwrdn register for further usage if stschng interrupt */
31022 + core_if->gr_backup->gpwrdn_local =
31023 + DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
31024 + /*Exit from ISR and wait for stschng interrupt with bsessvld = 1 */
31025 + return 1;
31026 + }
31027 +
31028 + /* Switch on the voltage to the core */
31029 + gpwrdn.d32 = 0;
31030 + gpwrdn.b.pwrdnswtch = 1;
31031 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
31032 + dwc_udelay(10);
31033 +
31034 + /* Reset the core */
31035 + gpwrdn.d32 = 0;
31036 + gpwrdn.b.pwrdnrstn = 1;
31037 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
31038 + dwc_udelay(10);
31039 +
31040 + /* Disable power clamps */
31041 + gpwrdn.d32 = 0;
31042 + gpwrdn.b.pwrdnclmp = 1;
31043 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
31044 +
31045 + /* Remove reset the core signal */
31046 + gpwrdn.d32 = 0;
31047 + gpwrdn.b.pwrdnrstn = 1;
31048 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
31049 + dwc_udelay(10);
31050 +
31051 + /* Disable PMU interrupt */
31052 + gpwrdn.d32 = 0;
31053 + gpwrdn.b.pmuintsel = 1;
31054 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
31055 + dwc_udelay(10);
31056 +
31057 + /*Indicates that we are exiting from hibernation */
31058 + core_if->hibernation_suspend = 0;
31059 +
31060 + /* Disable PMU */
31061 + gpwrdn.d32 = 0;
31062 + gpwrdn.b.pmuactv = 1;
31063 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
31064 + dwc_udelay(10);
31065 +
31066 + core_if->op_state = B_PERIPHERAL;
31067 + dwc_otg_core_init(core_if);
31068 + dwc_otg_enable_global_interrupts(core_if);
31069 + cil_pcd_start(core_if);
31070 +
31071 + if (otg_cap_param == DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE ||
31072 + otg_cap_param == DWC_OTG_CAP_PARAM_SRP_ONLY_CAPABLE) {
31073 + /*
31074 + * Initiate SRP after initial ADP probe.
31075 + */
31076 + dwc_otg_initiate_srp(core_if);
31077 + }
31078 + }
31079 +
31080 + return 1;
31081 +}
31082 +/**
31083 + * This interrupt indicates that the Wakeup Logic has detected a
31084 + * status change either on IDDIG or BSessVld.
31085 + */
31086 +static uint32_t dwc_otg_handle_pwrdn_stschng_intr(dwc_otg_device_t *otg_dev)
31087 +{
31088 + int retval;
31089 + gpwrdn_data_t gpwrdn = {.d32 = 0 };
31090 + gpwrdn_data_t gpwrdn_temp = {.d32 = 0 };
31091 + dwc_otg_core_if_t *core_if = otg_dev->core_if;
31092 +
31093 + DWC_PRINTF("%s called\n", __FUNCTION__);
31094 +
31095 + if (core_if->power_down == 2) {
31096 + if (core_if->hibernation_suspend <= 0) {
31097 + DWC_PRINTF("Already exited from Hibernation\n");
31098 + return 1;
31099 + } else
31100 + gpwrdn_temp.d32 = core_if->gr_backup->gpwrdn_local;
31101 +
31102 + } else {
31103 + gpwrdn_temp.d32 = core_if->adp.gpwrdn;
31104 + }
31105 +
31106 + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
31107 +
31108 + if (gpwrdn.b.idsts ^ gpwrdn_temp.b.idsts) {
31109 + retval = dwc_otg_handle_pwrdn_idsts_change(otg_dev);
31110 + } else if (gpwrdn.b.bsessvld ^ gpwrdn_temp.b.bsessvld) {
31111 + retval = dwc_otg_handle_pwrdn_session_change(core_if);
31112 + }
31113 +
31114 + return retval;
31115 +}
31116 +
31117 +/**
31118 + * This interrupt indicates that the Wakeup Logic has detected a
31119 + * SRP.
31120 + */
31121 +static int32_t dwc_otg_handle_pwrdn_srp_intr(dwc_otg_core_if_t * core_if)
31122 +{
31123 + gpwrdn_data_t gpwrdn = {.d32 = 0 };
31124 +
31125 + DWC_PRINTF("%s called\n", __FUNCTION__);
31126 +
31127 + if (!core_if->hibernation_suspend) {
31128 + DWC_PRINTF("Already exited from Hibernation\n");
31129 + return 1;
31130 + }
31131 +#ifdef DWC_DEV_SRPCAP
31132 + if (core_if->pwron_timer_started) {
31133 + core_if->pwron_timer_started = 0;
31134 + DWC_TIMER_CANCEL(core_if->pwron_timer);
31135 + }
31136 +#endif
31137 +
31138 + /* Switch on the voltage to the core */
31139 + gpwrdn.b.pwrdnswtch = 1;
31140 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
31141 + dwc_udelay(10);
31142 +
31143 + /* Reset the core */
31144 + gpwrdn.d32 = 0;
31145 + gpwrdn.b.pwrdnrstn = 1;
31146 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
31147 + dwc_udelay(10);
31148 +
31149 + /* Disable power clamps */
31150 + gpwrdn.d32 = 0;
31151 + gpwrdn.b.pwrdnclmp = 1;
31152 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
31153 +
31154 + /* Remove reset the core signal */
31155 + gpwrdn.d32 = 0;
31156 + gpwrdn.b.pwrdnrstn = 1;
31157 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
31158 + dwc_udelay(10);
31159 +
31160 + /* Disable PMU interrupt */
31161 + gpwrdn.d32 = 0;
31162 + gpwrdn.b.pmuintsel = 1;
31163 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
31164 +
31165 + /* Indicates that we are exiting from hibernation */
31166 + core_if->hibernation_suspend = 0;
31167 +
31168 + /* Disable PMU */
31169 + gpwrdn.d32 = 0;
31170 + gpwrdn.b.pmuactv = 1;
31171 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
31172 + dwc_udelay(10);
31173 +
31174 + /* Programm Disable VBUS to 0 */
31175 + gpwrdn.d32 = 0;
31176 + gpwrdn.b.dis_vbus = 1;
31177 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
31178 +
31179 + /*Initialize the core as Host */
31180 + core_if->op_state = A_HOST;
31181 + dwc_otg_core_init(core_if);
31182 + dwc_otg_enable_global_interrupts(core_if);
31183 + cil_hcd_start(core_if);
31184 +
31185 + return 1;
31186 +}
31187 +
31188 +/** This interrupt indicates that restore command after Hibernation
31189 + * was completed by the core. */
31190 +int32_t dwc_otg_handle_restore_done_intr(dwc_otg_core_if_t * core_if)
31191 +{
31192 + pcgcctl_data_t pcgcctl;
31193 + DWC_DEBUGPL(DBG_ANY, "++Restore Done Interrupt++\n");
31194 +
31195 + //TODO De-assert restore signal. 8.a
31196 + pcgcctl.d32 = DWC_READ_REG32(core_if->pcgcctl);
31197 + if (pcgcctl.b.restoremode == 1) {
31198 + gintmsk_data_t gintmsk = {.d32 = 0 };
31199 + /*
31200 + * If restore mode is Remote Wakeup,
31201 + * unmask Remote Wakeup interrupt.
31202 + */
31203 + gintmsk.b.wkupintr = 1;
31204 + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk,
31205 + 0, gintmsk.d32);
31206 + }
31207 +
31208 + return 1;
31209 +}
31210 +
31211 +/**
31212 + * This interrupt indicates that a device has been disconnected from
31213 + * the root port.
31214 + */
31215 +int32_t dwc_otg_handle_disconnect_intr(dwc_otg_core_if_t * core_if)
31216 +{
31217 + gintsts_data_t gintsts;
31218 +
31219 + DWC_DEBUGPL(DBG_ANY, "++Disconnect Detected Interrupt++ (%s) %s\n",
31220 + (dwc_otg_is_host_mode(core_if) ? "Host" : "Device"),
31221 + op_state_str(core_if));
31222 +
31223 +/** @todo Consolidate this if statement. */
31224 +#ifndef DWC_HOST_ONLY
31225 + if (core_if->op_state == B_HOST) {
31226 + /* If in device mode Disconnect and stop the HCD, then
31227 + * start the PCD. */
31228 + DWC_SPINUNLOCK(core_if->lock);
31229 + cil_hcd_disconnect(core_if);
31230 + cil_pcd_start(core_if);
31231 + DWC_SPINLOCK(core_if->lock);
31232 + core_if->op_state = B_PERIPHERAL;
31233 + } else if (dwc_otg_is_device_mode(core_if)) {
31234 + gotgctl_data_t gotgctl = {.d32 = 0 };
31235 + gotgctl.d32 =
31236 + DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
31237 + if (gotgctl.b.hstsethnpen == 1) {
31238 + /* Do nothing, if HNP in process the OTG
31239 + * interrupt "Host Negotiation Detected"
31240 + * interrupt will do the mode switch.
31241 + */
31242 + } else if (gotgctl.b.devhnpen == 0) {
31243 + /* If in device mode Disconnect and stop the HCD, then
31244 + * start the PCD. */
31245 + DWC_SPINUNLOCK(core_if->lock);
31246 + cil_hcd_disconnect(core_if);
31247 + cil_pcd_start(core_if);
31248 + DWC_SPINLOCK(core_if->lock);
31249 + core_if->op_state = B_PERIPHERAL;
31250 + } else {
31251 + DWC_DEBUGPL(DBG_ANY, "!a_peripheral && !devhnpen\n");
31252 + }
31253 + } else {
31254 + if (core_if->op_state == A_HOST) {
31255 + /* A-Cable still connected but device disconnected. */
31256 + DWC_SPINUNLOCK(core_if->lock);
31257 + cil_hcd_disconnect(core_if);
31258 + DWC_SPINLOCK(core_if->lock);
31259 + if (core_if->adp_enable) {
31260 + gpwrdn_data_t gpwrdn = { .d32 = 0 };
31261 + cil_hcd_stop(core_if);
31262 + /* Enable Power Down Logic */
31263 + gpwrdn.b.pmuintsel = 1;
31264 + gpwrdn.b.pmuactv = 1;
31265 + DWC_MODIFY_REG32(&core_if->core_global_regs->
31266 + gpwrdn, 0, gpwrdn.d32);
31267 + dwc_otg_adp_probe_start(core_if);
31268 +
31269 + /* Power off the core */
31270 + if (core_if->power_down == 2) {
31271 + gpwrdn.d32 = 0;
31272 + gpwrdn.b.pwrdnswtch = 1;
31273 + DWC_MODIFY_REG32
31274 + (&core_if->core_global_regs->gpwrdn,
31275 + gpwrdn.d32, 0);
31276 + }
31277 + }
31278 + }
31279 + }
31280 +#endif
31281 + /* Change to L3(OFF) state */
31282 + core_if->lx_state = DWC_OTG_L3;
31283 +
31284 + gintsts.d32 = 0;
31285 + gintsts.b.disconnect = 1;
31286 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
31287 + return 1;
31288 +}
31289 +
31290 +/**
31291 + * This interrupt indicates that SUSPEND state has been detected on
31292 + * the USB.
31293 + *
31294 + * For HNP the USB Suspend interrupt signals the change from
31295 + * "a_peripheral" to "a_host".
31296 + *
31297 + * When power management is enabled the core will be put in low power
31298 + * mode.
31299 + */
31300 +int32_t dwc_otg_handle_usb_suspend_intr(dwc_otg_core_if_t * core_if)
31301 +{
31302 + dsts_data_t dsts;
31303 + gintsts_data_t gintsts;
31304 + dcfg_data_t dcfg;
31305 +
31306 + DWC_DEBUGPL(DBG_ANY, "USB SUSPEND\n");
31307 +
31308 + if (dwc_otg_is_device_mode(core_if)) {
31309 + /* Check the Device status register to determine if the Suspend
31310 + * state is active. */
31311 + dsts.d32 =
31312 + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
31313 + DWC_DEBUGPL(DBG_PCD, "DSTS=0x%0x\n", dsts.d32);
31314 + DWC_DEBUGPL(DBG_PCD, "DSTS.Suspend Status=%d "
31315 + "HWCFG4.power Optimize=%d\n",
31316 + dsts.b.suspsts, core_if->hwcfg4.b.power_optimiz);
31317 +
31318 +#ifdef PARTIAL_POWER_DOWN
31319 +/** @todo Add a module parameter for power management. */
31320 +
31321 + if (dsts.b.suspsts && core_if->hwcfg4.b.power_optimiz) {
31322 + pcgcctl_data_t power = {.d32 = 0 };
31323 + DWC_DEBUGPL(DBG_CIL, "suspend\n");
31324 +
31325 + power.b.pwrclmp = 1;
31326 + DWC_WRITE_REG32(core_if->pcgcctl, power.d32);
31327 +
31328 + power.b.rstpdwnmodule = 1;
31329 + DWC_MODIFY_REG32(core_if->pcgcctl, 0, power.d32);
31330 +
31331 + power.b.stoppclk = 1;
31332 + DWC_MODIFY_REG32(core_if->pcgcctl, 0, power.d32);
31333 +
31334 + } else {
31335 + DWC_DEBUGPL(DBG_ANY, "disconnect?\n");
31336 + }
31337 +#endif
31338 + /* PCD callback for suspend. Release the lock inside of callback function */
31339 + cil_pcd_suspend(core_if);
31340 + if (core_if->power_down == 2)
31341 + {
31342 + dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
31343 + DWC_DEBUGPL(DBG_ANY,"lx_state = %08x\n",core_if->lx_state);
31344 + DWC_DEBUGPL(DBG_ANY," device address = %08d\n",dcfg.b.devaddr);
31345 +
31346 + if (core_if->lx_state != DWC_OTG_L3 && dcfg.b.devaddr) {
31347 + pcgcctl_data_t pcgcctl = {.d32 = 0 };
31348 + gpwrdn_data_t gpwrdn = {.d32 = 0 };
31349 + gusbcfg_data_t gusbcfg = {.d32 = 0 };
31350 +
31351 + /* Change to L2(suspend) state */
31352 + core_if->lx_state = DWC_OTG_L2;
31353 +
31354 + /* Clear interrupt in gintsts */
31355 + gintsts.d32 = 0;
31356 + gintsts.b.usbsuspend = 1;
31357 + DWC_WRITE_REG32(&core_if->core_global_regs->
31358 + gintsts, gintsts.d32);
31359 + DWC_PRINTF("Start of hibernation completed\n");
31360 + dwc_otg_save_global_regs(core_if);
31361 + dwc_otg_save_dev_regs(core_if);
31362 +
31363 + gusbcfg.d32 =
31364 + DWC_READ_REG32(&core_if->core_global_regs->
31365 + gusbcfg);
31366 + if (gusbcfg.b.ulpi_utmi_sel == 1) {
31367 + /* ULPI interface */
31368 + /* Suspend the Phy Clock */
31369 + pcgcctl.d32 = 0;
31370 + pcgcctl.b.stoppclk = 1;
31371 + DWC_MODIFY_REG32(core_if->pcgcctl, 0,
31372 + pcgcctl.d32);
31373 + dwc_udelay(10);
31374 + gpwrdn.b.pmuactv = 1;
31375 + DWC_MODIFY_REG32(&core_if->
31376 + core_global_regs->
31377 + gpwrdn, 0, gpwrdn.d32);
31378 + } else {
31379 + /* UTMI+ Interface */
31380 + gpwrdn.b.pmuactv = 1;
31381 + DWC_MODIFY_REG32(&core_if->
31382 + core_global_regs->
31383 + gpwrdn, 0, gpwrdn.d32);
31384 + dwc_udelay(10);
31385 + pcgcctl.b.stoppclk = 1;
31386 + DWC_MODIFY_REG32(core_if->pcgcctl, 0,
31387 + pcgcctl.d32);
31388 + dwc_udelay(10);
31389 + }
31390 +
31391 + /* Set flag to indicate that we are in hibernation */
31392 + core_if->hibernation_suspend = 1;
31393 + /* Enable interrupts from wake up logic */
31394 + gpwrdn.d32 = 0;
31395 + gpwrdn.b.pmuintsel = 1;
31396 + DWC_MODIFY_REG32(&core_if->core_global_regs->
31397 + gpwrdn, 0, gpwrdn.d32);
31398 + dwc_udelay(10);
31399 +
31400 + /* Unmask device mode interrupts in GPWRDN */
31401 + gpwrdn.d32 = 0;
31402 + gpwrdn.b.rst_det_msk = 1;
31403 + gpwrdn.b.lnstchng_msk = 1;
31404 + gpwrdn.b.sts_chngint_msk = 1;
31405 + DWC_MODIFY_REG32(&core_if->core_global_regs->
31406 + gpwrdn, 0, gpwrdn.d32);
31407 + dwc_udelay(10);
31408 +
31409 + /* Enable Power Down Clamp */
31410 + gpwrdn.d32 = 0;
31411 + gpwrdn.b.pwrdnclmp = 1;
31412 + DWC_MODIFY_REG32(&core_if->core_global_regs->
31413 + gpwrdn, 0, gpwrdn.d32);
31414 + dwc_udelay(10);
31415 +
31416 + /* Switch off VDD */
31417 + gpwrdn.d32 = 0;
31418 + gpwrdn.b.pwrdnswtch = 1;
31419 + DWC_MODIFY_REG32(&core_if->core_global_regs->
31420 + gpwrdn, 0, gpwrdn.d32);
31421 +
31422 + /* Save gpwrdn register for further usage if stschng interrupt */
31423 + core_if->gr_backup->gpwrdn_local =
31424 + DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
31425 + DWC_PRINTF("Hibernation completed\n");
31426 +
31427 + return 1;
31428 + }
31429 + } else if (core_if->power_down == 3) {
31430 + pcgcctl_data_t pcgcctl = {.d32 = 0 };
31431 + dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
31432 + DWC_DEBUGPL(DBG_ANY, "lx_state = %08x\n",core_if->lx_state);
31433 + DWC_DEBUGPL(DBG_ANY, " device address = %08d\n",dcfg.b.devaddr);
31434 +
31435 + if (core_if->lx_state != DWC_OTG_L3 && dcfg.b.devaddr) {
31436 + DWC_DEBUGPL(DBG_ANY, "Start entering to extended hibernation\n");
31437 + core_if->xhib = 1;
31438 +
31439 + /* Clear interrupt in gintsts */
31440 + gintsts.d32 = 0;
31441 + gintsts.b.usbsuspend = 1;
31442 + DWC_WRITE_REG32(&core_if->core_global_regs->
31443 + gintsts, gintsts.d32);
31444 +
31445 + dwc_otg_save_global_regs(core_if);
31446 + dwc_otg_save_dev_regs(core_if);
31447 +
31448 + /* Wait for 10 PHY clocks */
31449 + dwc_udelay(10);
31450 +
31451 + /* Program GPIO register while entering to xHib */
31452 + DWC_WRITE_REG32(&core_if->core_global_regs->ggpio, 0x1);
31453 +
31454 + pcgcctl.b.enbl_extnd_hiber = 1;
31455 + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
31456 + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
31457 +
31458 + pcgcctl.d32 = 0;
31459 + pcgcctl.b.extnd_hiber_pwrclmp = 1;
31460 + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
31461 +
31462 + pcgcctl.d32 = 0;
31463 + pcgcctl.b.extnd_hiber_switch = 1;
31464 + core_if->gr_backup->xhib_gpwrdn = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
31465 + core_if->gr_backup->xhib_pcgcctl = DWC_READ_REG32(core_if->pcgcctl) | pcgcctl.d32;
31466 + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
31467 +
31468 + DWC_DEBUGPL(DBG_ANY, "Finished entering to extended hibernation\n");
31469 +
31470 + return 1;
31471 + }
31472 + }
31473 + } else {
31474 + if (core_if->op_state == A_PERIPHERAL) {
31475 + DWC_DEBUGPL(DBG_ANY, "a_peripheral->a_host\n");
31476 + /* Clear the a_peripheral flag, back to a_host. */
31477 + DWC_SPINUNLOCK(core_if->lock);
31478 + cil_pcd_stop(core_if);
31479 + cil_hcd_start(core_if);
31480 + DWC_SPINLOCK(core_if->lock);
31481 + core_if->op_state = A_HOST;
31482 + }
31483 + }
31484 +
31485 + /* Change to L2(suspend) state */
31486 + core_if->lx_state = DWC_OTG_L2;
31487 +
31488 + /* Clear interrupt */
31489 + gintsts.d32 = 0;
31490 + gintsts.b.usbsuspend = 1;
31491 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
31492 +
31493 + return 1;
31494 +}
31495 +
31496 +static int32_t dwc_otg_handle_xhib_exit_intr(dwc_otg_core_if_t * core_if)
31497 +{
31498 + gpwrdn_data_t gpwrdn = {.d32 = 0 };
31499 + pcgcctl_data_t pcgcctl = {.d32 = 0 };
31500 + gahbcfg_data_t gahbcfg = {.d32 = 0 };
31501 +
31502 + dwc_udelay(10);
31503 +
31504 + /* Program GPIO register while entering to xHib */
31505 + DWC_WRITE_REG32(&core_if->core_global_regs->ggpio, 0x0);
31506 +
31507 + pcgcctl.d32 = core_if->gr_backup->xhib_pcgcctl;
31508 + pcgcctl.b.extnd_hiber_pwrclmp = 0;
31509 + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
31510 + dwc_udelay(10);
31511 +
31512 + gpwrdn.d32 = core_if->gr_backup->xhib_gpwrdn;
31513 + gpwrdn.b.restore = 1;
31514 + DWC_WRITE_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32);
31515 + dwc_udelay(10);
31516 +
31517 + restore_lpm_i2c_regs(core_if);
31518 +
31519 + pcgcctl.d32 = core_if->gr_backup->pcgcctl_local & (0x3FFFF << 14);
31520 + pcgcctl.b.max_xcvrselect = 1;
31521 + pcgcctl.b.ess_reg_restored = 0;
31522 + pcgcctl.b.extnd_hiber_switch = 0;
31523 + pcgcctl.b.extnd_hiber_pwrclmp = 0;
31524 + pcgcctl.b.enbl_extnd_hiber = 1;
31525 + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
31526 +
31527 + gahbcfg.d32 = core_if->gr_backup->gahbcfg_local;
31528 + gahbcfg.b.glblintrmsk = 1;
31529 + DWC_WRITE_REG32(&core_if->core_global_regs->gahbcfg, gahbcfg.d32);
31530 +
31531 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
31532 + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, 0x1 << 16);
31533 +
31534 + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg,
31535 + core_if->gr_backup->gusbcfg_local);
31536 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg,
31537 + core_if->dr_backup->dcfg);
31538 +
31539 + pcgcctl.d32 = 0;
31540 + pcgcctl.d32 = core_if->gr_backup->pcgcctl_local & (0x3FFFF << 14);
31541 + pcgcctl.b.max_xcvrselect = 1;
31542 + pcgcctl.d32 |= 0x608;
31543 + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
31544 + dwc_udelay(10);
31545 +
31546 + pcgcctl.d32 = 0;
31547 + pcgcctl.d32 = core_if->gr_backup->pcgcctl_local & (0x3FFFF << 14);
31548 + pcgcctl.b.max_xcvrselect = 1;
31549 + pcgcctl.b.ess_reg_restored = 1;
31550 + pcgcctl.b.enbl_extnd_hiber = 1;
31551 + pcgcctl.b.rstpdwnmodule = 1;
31552 + pcgcctl.b.restoremode = 1;
31553 + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
31554 +
31555 + DWC_DEBUGPL(DBG_ANY, "%s called\n", __FUNCTION__);
31556 +
31557 + return 1;
31558 +}
31559 +
31560 +#ifdef CONFIG_USB_DWC_OTG_LPM
31561 +/**
31562 + * This function hadles LPM transaction received interrupt.
31563 + */
31564 +static int32_t dwc_otg_handle_lpm_intr(dwc_otg_core_if_t * core_if)
31565 +{
31566 + glpmcfg_data_t lpmcfg;
31567 + gintsts_data_t gintsts;
31568 +
31569 + if (!core_if->core_params->lpm_enable) {
31570 + DWC_PRINTF("Unexpected LPM interrupt\n");
31571 + }
31572 +
31573 + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
31574 + DWC_PRINTF("LPM config register = 0x%08x\n", lpmcfg.d32);
31575 +
31576 + if (dwc_otg_is_host_mode(core_if)) {
31577 + cil_hcd_sleep(core_if);
31578 + } else {
31579 + lpmcfg.b.hird_thres |= (1 << 4);
31580 + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg,
31581 + lpmcfg.d32);
31582 + }
31583 +
31584 + /* Examine prt_sleep_sts after TL1TokenTetry period max (10 us) */
31585 + dwc_udelay(10);
31586 + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
31587 + if (lpmcfg.b.prt_sleep_sts) {
31588 + /* Save the current state */
31589 + core_if->lx_state = DWC_OTG_L1;
31590 + }
31591 +
31592 + /* Clear interrupt */
31593 + gintsts.d32 = 0;
31594 + gintsts.b.lpmtranrcvd = 1;
31595 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
31596 + return 1;
31597 +}
31598 +#endif /* CONFIG_USB_DWC_OTG_LPM */
31599 +
31600 +/**
31601 + * This function returns the Core Interrupt register.
31602 + */
31603 +static inline uint32_t dwc_otg_read_common_intr(dwc_otg_core_if_t * core_if, gintmsk_data_t *reenable_gintmsk, dwc_otg_hcd_t *hcd)
31604 +{
31605 + gahbcfg_data_t gahbcfg = {.d32 = 0 };
31606 + gintsts_data_t gintsts;
31607 + gintmsk_data_t gintmsk;
31608 + gintmsk_data_t gintmsk_common = {.d32 = 0 };
31609 + gintmsk_common.b.wkupintr = 1;
31610 + gintmsk_common.b.sessreqintr = 1;
31611 + gintmsk_common.b.conidstschng = 1;
31612 + gintmsk_common.b.otgintr = 1;
31613 + gintmsk_common.b.modemismatch = 1;
31614 + gintmsk_common.b.disconnect = 1;
31615 + gintmsk_common.b.usbsuspend = 1;
31616 +#ifdef CONFIG_USB_DWC_OTG_LPM
31617 + gintmsk_common.b.lpmtranrcvd = 1;
31618 +#endif
31619 + gintmsk_common.b.restoredone = 1;
31620 + if(dwc_otg_is_device_mode(core_if))
31621 + {
31622 + /** @todo: The port interrupt occurs while in device
31623 + * mode. Added code to CIL to clear the interrupt for now!
31624 + */
31625 + gintmsk_common.b.portintr = 1;
31626 + }
31627 + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
31628 + gintmsk.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
31629 + if(fiq_enable) {
31630 + local_fiq_disable();
31631 + /* Pull in the interrupts that the FIQ has masked */
31632 + gintmsk.d32 |= ~(hcd->fiq_state->gintmsk_saved.d32);
31633 + gintmsk.d32 |= gintmsk_common.d32;
31634 + /* for the upstairs function to reenable - have to read it here in case FIQ triggers again */
31635 + reenable_gintmsk->d32 = gintmsk.d32;
31636 + local_fiq_enable();
31637 + }
31638 +
31639 + gahbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gahbcfg);
31640 +
31641 +#ifdef DEBUG
31642 + /* if any common interrupts set */
31643 + if (gintsts.d32 & gintmsk_common.d32) {
31644 + DWC_DEBUGPL(DBG_ANY, "common_intr: gintsts=%08x gintmsk=%08x\n",
31645 + gintsts.d32, gintmsk.d32);
31646 + }
31647 +#endif
31648 + if (!fiq_enable){
31649 + if (gahbcfg.b.glblintrmsk)
31650 + return ((gintsts.d32 & gintmsk.d32) & gintmsk_common.d32);
31651 + else
31652 + return 0;
31653 + } else {
31654 + /* Our IRQ kicker is no longer the USB hardware, it's the MPHI interface.
31655 + * Can't trust the global interrupt mask bit in this case.
31656 + */
31657 + return ((gintsts.d32 & gintmsk.d32) & gintmsk_common.d32);
31658 + }
31659 +
31660 +}
31661 +
31662 +/* MACRO for clearing interupt bits in GPWRDN register */
31663 +#define CLEAR_GPWRDN_INTR(__core_if,__intr) \
31664 +do { \
31665 + gpwrdn_data_t gpwrdn = {.d32=0}; \
31666 + gpwrdn.b.__intr = 1; \
31667 + DWC_MODIFY_REG32(&__core_if->core_global_regs->gpwrdn, \
31668 + 0, gpwrdn.d32); \
31669 +} while (0)
31670 +
31671 +/**
31672 + * Common interrupt handler.
31673 + *
31674 + * The common interrupts are those that occur in both Host and Device mode.
31675 + * This handler handles the following interrupts:
31676 + * - Mode Mismatch Interrupt
31677 + * - Disconnect Interrupt
31678 + * - OTG Interrupt
31679 + * - Connector ID Status Change Interrupt
31680 + * - Session Request Interrupt.
31681 + * - Resume / Remote Wakeup Detected Interrupt.
31682 + * - LPM Transaction Received Interrupt
31683 + * - ADP Transaction Received Interrupt
31684 + *
31685 + */
31686 +int32_t dwc_otg_handle_common_intr(void *dev)
31687 +{
31688 + int retval = 0;
31689 + gintsts_data_t gintsts;
31690 + gintmsk_data_t gintmsk_reenable = { .d32 = 0 };
31691 + gpwrdn_data_t gpwrdn = {.d32 = 0 };
31692 + dwc_otg_device_t *otg_dev = dev;
31693 + dwc_otg_core_if_t *core_if = otg_dev->core_if;
31694 + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
31695 + if (dwc_otg_is_device_mode(core_if))
31696 + core_if->frame_num = dwc_otg_get_frame_number(core_if);
31697 +
31698 + if (core_if->lock)
31699 + DWC_SPINLOCK(core_if->lock);
31700 +
31701 + if (core_if->power_down == 3 && core_if->xhib == 1) {
31702 + DWC_DEBUGPL(DBG_ANY, "Exiting from xHIB state\n");
31703 + retval |= dwc_otg_handle_xhib_exit_intr(core_if);
31704 + core_if->xhib = 2;
31705 + if (core_if->lock)
31706 + DWC_SPINUNLOCK(core_if->lock);
31707 +
31708 + return retval;
31709 + }
31710 +
31711 + if (core_if->hibernation_suspend <= 0) {
31712 + /* read_common will have to poke the FIQ's saved mask. We must then clear this mask at the end
31713 + * of this handler - god only knows why it's done like this
31714 + */
31715 + gintsts.d32 = dwc_otg_read_common_intr(core_if, &gintmsk_reenable, otg_dev->hcd);
31716 +
31717 + if (gintsts.b.modemismatch) {
31718 + retval |= dwc_otg_handle_mode_mismatch_intr(core_if);
31719 + }
31720 + if (gintsts.b.otgintr) {
31721 + retval |= dwc_otg_handle_otg_intr(core_if);
31722 + }
31723 + if (gintsts.b.conidstschng) {
31724 + retval |=
31725 + dwc_otg_handle_conn_id_status_change_intr(core_if);
31726 + }
31727 + if (gintsts.b.disconnect) {
31728 + retval |= dwc_otg_handle_disconnect_intr(core_if);
31729 + }
31730 + if (gintsts.b.sessreqintr) {
31731 + retval |= dwc_otg_handle_session_req_intr(core_if);
31732 + }
31733 + if (gintsts.b.wkupintr) {
31734 + retval |= dwc_otg_handle_wakeup_detected_intr(core_if);
31735 + }
31736 + if (gintsts.b.usbsuspend) {
31737 + retval |= dwc_otg_handle_usb_suspend_intr(core_if);
31738 + }
31739 +#ifdef CONFIG_USB_DWC_OTG_LPM
31740 + if (gintsts.b.lpmtranrcvd) {
31741 + retval |= dwc_otg_handle_lpm_intr(core_if);
31742 + }
31743 +#endif
31744 + if (gintsts.b.restoredone) {
31745 + gintsts.d32 = 0;
31746 + if (core_if->power_down == 2)
31747 + core_if->hibernation_suspend = -1;
31748 + else if (core_if->power_down == 3 && core_if->xhib == 2) {
31749 + gpwrdn_data_t gpwrdn = {.d32 = 0 };
31750 + pcgcctl_data_t pcgcctl = {.d32 = 0 };
31751 + dctl_data_t dctl = {.d32 = 0 };
31752 +
31753 + DWC_WRITE_REG32(&core_if->core_global_regs->
31754 + gintsts, 0xFFFFFFFF);
31755 +
31756 + DWC_DEBUGPL(DBG_ANY,
31757 + "RESTORE DONE generated\n");
31758 +
31759 + gpwrdn.b.restore = 1;
31760 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
31761 + dwc_udelay(10);
31762 +
31763 + pcgcctl.b.rstpdwnmodule = 1;
31764 + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
31765 +
31766 + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, core_if->gr_backup->gusbcfg_local);
31767 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg, core_if->dr_backup->dcfg);
31768 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl, core_if->dr_backup->dctl);
31769 + dwc_udelay(50);
31770 +
31771 + dctl.b.pwronprgdone = 1;
31772 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
31773 + dwc_udelay(10);
31774 +
31775 + dwc_otg_restore_global_regs(core_if);
31776 + dwc_otg_restore_dev_regs(core_if, 0);
31777 +
31778 + dctl.d32 = 0;
31779 + dctl.b.pwronprgdone = 1;
31780 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32, 0);
31781 + dwc_udelay(10);
31782 +
31783 + pcgcctl.d32 = 0;
31784 + pcgcctl.b.enbl_extnd_hiber = 1;
31785 + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
31786 +
31787 + /* The core will be in ON STATE */
31788 + core_if->lx_state = DWC_OTG_L0;
31789 + core_if->xhib = 0;
31790 +
31791 + DWC_SPINUNLOCK(core_if->lock);
31792 + if (core_if->pcd_cb && core_if->pcd_cb->resume_wakeup) {
31793 + core_if->pcd_cb->resume_wakeup(core_if->pcd_cb->p);
31794 + }
31795 + DWC_SPINLOCK(core_if->lock);
31796 +
31797 + }
31798 +
31799 + gintsts.b.restoredone = 1;
31800 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts,gintsts.d32);
31801 + DWC_PRINTF(" --Restore done interrupt received-- \n");
31802 + retval |= 1;
31803 + }
31804 + if (gintsts.b.portintr && dwc_otg_is_device_mode(core_if)) {
31805 + /* The port interrupt occurs while in device mode with HPRT0
31806 + * Port Enable/Disable.
31807 + */
31808 + gintsts.d32 = 0;
31809 + gintsts.b.portintr = 1;
31810 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts,gintsts.d32);
31811 + retval |= 1;
31812 + gintmsk_reenable.b.portintr = 1;
31813 +
31814 + }
31815 + /* Did we actually handle anything? if so, unmask the interrupt */
31816 +// fiq_print(FIQDBG_INT, otg_dev->hcd->fiq_state, "CILOUT %1d", retval);
31817 +// fiq_print(FIQDBG_INT, otg_dev->hcd->fiq_state, "%08x", gintsts.d32);
31818 +// fiq_print(FIQDBG_INT, otg_dev->hcd->fiq_state, "%08x", gintmsk_reenable.d32);
31819 + if (retval && fiq_enable) {
31820 + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, gintmsk_reenable.d32);
31821 + }
31822 +
31823 + } else {
31824 + DWC_DEBUGPL(DBG_ANY, "gpwrdn=%08x\n", gpwrdn.d32);
31825 +
31826 + if (gpwrdn.b.disconn_det && gpwrdn.b.disconn_det_msk) {
31827 + CLEAR_GPWRDN_INTR(core_if, disconn_det);
31828 + if (gpwrdn.b.linestate == 0) {
31829 + dwc_otg_handle_pwrdn_disconnect_intr(core_if);
31830 + } else {
31831 + DWC_PRINTF("Disconnect detected while linestate is not 0\n");
31832 + }
31833 +
31834 + retval |= 1;
31835 + }
31836 + if (gpwrdn.b.lnstschng && gpwrdn.b.lnstchng_msk) {
31837 + CLEAR_GPWRDN_INTR(core_if, lnstschng);
31838 + /* remote wakeup from hibernation */
31839 + if (gpwrdn.b.linestate == 2 || gpwrdn.b.linestate == 1) {
31840 + dwc_otg_handle_pwrdn_wakeup_detected_intr(core_if);
31841 + } else {
31842 + DWC_PRINTF("gpwrdn.linestate = %d\n", gpwrdn.b.linestate);
31843 + }
31844 + retval |= 1;
31845 + }
31846 + if (gpwrdn.b.rst_det && gpwrdn.b.rst_det_msk) {
31847 + CLEAR_GPWRDN_INTR(core_if, rst_det);
31848 + if (gpwrdn.b.linestate == 0) {
31849 + DWC_PRINTF("Reset detected\n");
31850 + retval |= dwc_otg_device_hibernation_restore(core_if, 0, 1);
31851 + }
31852 + }
31853 + if (gpwrdn.b.srp_det && gpwrdn.b.srp_det_msk) {
31854 + CLEAR_GPWRDN_INTR(core_if, srp_det);
31855 + dwc_otg_handle_pwrdn_srp_intr(core_if);
31856 + retval |= 1;
31857 + }
31858 + }
31859 + /* Handle ADP interrupt here */
31860 + if (gpwrdn.b.adp_int) {
31861 + DWC_PRINTF("ADP interrupt\n");
31862 + CLEAR_GPWRDN_INTR(core_if, adp_int);
31863 + dwc_otg_adp_handle_intr(core_if);
31864 + retval |= 1;
31865 + }
31866 + if (gpwrdn.b.sts_chngint && gpwrdn.b.sts_chngint_msk) {
31867 + DWC_PRINTF("STS CHNG interrupt asserted\n");
31868 + CLEAR_GPWRDN_INTR(core_if, sts_chngint);
31869 + dwc_otg_handle_pwrdn_stschng_intr(otg_dev);
31870 +
31871 + retval |= 1;
31872 + }
31873 + if (core_if->lock)
31874 + DWC_SPINUNLOCK(core_if->lock);
31875 + return retval;
31876 +}
31877 --- /dev/null
31878 +++ b/drivers/usb/host/dwc_otg/dwc_otg_core_if.h
31879 @@ -0,0 +1,705 @@
31880 +/* ==========================================================================
31881 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_core_if.h $
31882 + * $Revision: #13 $
31883 + * $Date: 2012/08/10 $
31884 + * $Change: 2047372 $
31885 + *
31886 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
31887 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
31888 + * otherwise expressly agreed to in writing between Synopsys and you.
31889 + *
31890 + * The Software IS NOT an item of Licensed Software or Licensed Product under
31891 + * any End User Software License Agreement or Agreement for Licensed Product
31892 + * with Synopsys or any supplement thereto. You are permitted to use and
31893 + * redistribute this Software in source and binary forms, with or without
31894 + * modification, provided that redistributions of source code must retain this
31895 + * notice. You may not view, use, disclose, copy or distribute this file or
31896 + * any information contained herein except pursuant to this license grant from
31897 + * Synopsys. If you do not agree with this notice, including the disclaimer
31898 + * below, then you are not authorized to use the Software.
31899 + *
31900 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
31901 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31902 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31903 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
31904 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
31905 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31906 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31907 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31908 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31909 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
31910 + * DAMAGE.
31911 + * ========================================================================== */
31912 +#if !defined(__DWC_CORE_IF_H__)
31913 +#define __DWC_CORE_IF_H__
31914 +
31915 +#include "dwc_os.h"
31916 +
31917 +/** @file
31918 + * This file defines DWC_OTG Core API
31919 + */
31920 +
31921 +struct dwc_otg_core_if;
31922 +typedef struct dwc_otg_core_if dwc_otg_core_if_t;
31923 +
31924 +/** Maximum number of Periodic FIFOs */
31925 +#define MAX_PERIO_FIFOS 15
31926 +/** Maximum number of Periodic FIFOs */
31927 +#define MAX_TX_FIFOS 15
31928 +
31929 +/** Maximum number of Endpoints/HostChannels */
31930 +#define MAX_EPS_CHANNELS 16
31931 +
31932 +extern dwc_otg_core_if_t *dwc_otg_cil_init(const uint32_t * _reg_base_addr);
31933 +extern void dwc_otg_core_init(dwc_otg_core_if_t * _core_if);
31934 +extern void dwc_otg_cil_remove(dwc_otg_core_if_t * _core_if);
31935 +
31936 +extern void dwc_otg_enable_global_interrupts(dwc_otg_core_if_t * _core_if);
31937 +extern void dwc_otg_disable_global_interrupts(dwc_otg_core_if_t * _core_if);
31938 +
31939 +extern uint8_t dwc_otg_is_device_mode(dwc_otg_core_if_t * _core_if);
31940 +extern uint8_t dwc_otg_is_host_mode(dwc_otg_core_if_t * _core_if);
31941 +
31942 +extern uint8_t dwc_otg_is_dma_enable(dwc_otg_core_if_t * core_if);
31943 +
31944 +/** This function should be called on every hardware interrupt. */
31945 +extern int32_t dwc_otg_handle_common_intr(void *otg_dev);
31946 +
31947 +/** @name OTG Core Parameters */
31948 +/** @{ */
31949 +
31950 +/**
31951 + * Specifies the OTG capabilities. The driver will automatically
31952 + * detect the value for this parameter if none is specified.
31953 + * 0 - HNP and SRP capable (default)
31954 + * 1 - SRP Only capable
31955 + * 2 - No HNP/SRP capable
31956 + */
31957 +extern int dwc_otg_set_param_otg_cap(dwc_otg_core_if_t * core_if, int32_t val);
31958 +extern int32_t dwc_otg_get_param_otg_cap(dwc_otg_core_if_t * core_if);
31959 +#define DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE 0
31960 +#define DWC_OTG_CAP_PARAM_SRP_ONLY_CAPABLE 1
31961 +#define DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE 2
31962 +#define dwc_param_otg_cap_default DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE
31963 +
31964 +extern int dwc_otg_set_param_opt(dwc_otg_core_if_t * core_if, int32_t val);
31965 +extern int32_t dwc_otg_get_param_opt(dwc_otg_core_if_t * core_if);
31966 +#define dwc_param_opt_default 1
31967 +
31968 +/**
31969 + * Specifies whether to use slave or DMA mode for accessing the data
31970 + * FIFOs. The driver will automatically detect the value for this
31971 + * parameter if none is specified.
31972 + * 0 - Slave
31973 + * 1 - DMA (default, if available)
31974 + */
31975 +extern int dwc_otg_set_param_dma_enable(dwc_otg_core_if_t * core_if,
31976 + int32_t val);
31977 +extern int32_t dwc_otg_get_param_dma_enable(dwc_otg_core_if_t * core_if);
31978 +#define dwc_param_dma_enable_default 1
31979 +
31980 +/**
31981 + * When DMA mode is enabled specifies whether to use
31982 + * address DMA or DMA Descritor mode for accessing the data
31983 + * FIFOs in device mode. The driver will automatically detect
31984 + * the value for this parameter if none is specified.
31985 + * 0 - address DMA
31986 + * 1 - DMA Descriptor(default, if available)
31987 + */
31988 +extern int dwc_otg_set_param_dma_desc_enable(dwc_otg_core_if_t * core_if,
31989 + int32_t val);
31990 +extern int32_t dwc_otg_get_param_dma_desc_enable(dwc_otg_core_if_t * core_if);
31991 +//#define dwc_param_dma_desc_enable_default 1
31992 +#define dwc_param_dma_desc_enable_default 0 // Broadcom BCM2708
31993 +
31994 +/** The DMA Burst size (applicable only for External DMA
31995 + * Mode). 1, 4, 8 16, 32, 64, 128, 256 (default 32)
31996 + */
31997 +extern int dwc_otg_set_param_dma_burst_size(dwc_otg_core_if_t * core_if,
31998 + int32_t val);
31999 +extern int32_t dwc_otg_get_param_dma_burst_size(dwc_otg_core_if_t * core_if);
32000 +#define dwc_param_dma_burst_size_default 32
32001 +
32002 +/**
32003 + * Specifies the maximum speed of operation in host and device mode.
32004 + * The actual speed depends on the speed of the attached device and
32005 + * the value of phy_type. The actual speed depends on the speed of the
32006 + * attached device.
32007 + * 0 - High Speed (default)
32008 + * 1 - Full Speed
32009 + */
32010 +extern int dwc_otg_set_param_speed(dwc_otg_core_if_t * core_if, int32_t val);
32011 +extern int32_t dwc_otg_get_param_speed(dwc_otg_core_if_t * core_if);
32012 +#define dwc_param_speed_default 0
32013 +#define DWC_SPEED_PARAM_HIGH 0
32014 +#define DWC_SPEED_PARAM_FULL 1
32015 +
32016 +/** Specifies whether low power mode is supported when attached
32017 + * to a Full Speed or Low Speed device in host mode.
32018 + * 0 - Don't support low power mode (default)
32019 + * 1 - Support low power mode
32020 + */
32021 +extern int dwc_otg_set_param_host_support_fs_ls_low_power(dwc_otg_core_if_t *
32022 + core_if, int32_t val);
32023 +extern int32_t dwc_otg_get_param_host_support_fs_ls_low_power(dwc_otg_core_if_t
32024 + * core_if);
32025 +#define dwc_param_host_support_fs_ls_low_power_default 0
32026 +
32027 +/** Specifies the PHY clock rate in low power mode when connected to a
32028 + * Low Speed device in host mode. This parameter is applicable only if
32029 + * HOST_SUPPORT_FS_LS_LOW_POWER is enabled. If PHY_TYPE is set to FS
32030 + * then defaults to 6 MHZ otherwise 48 MHZ.
32031 + *
32032 + * 0 - 48 MHz
32033 + * 1 - 6 MHz
32034 + */
32035 +extern int dwc_otg_set_param_host_ls_low_power_phy_clk(dwc_otg_core_if_t *
32036 + core_if, int32_t val);
32037 +extern int32_t dwc_otg_get_param_host_ls_low_power_phy_clk(dwc_otg_core_if_t *
32038 + core_if);
32039 +#define dwc_param_host_ls_low_power_phy_clk_default 0
32040 +#define DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ 0
32041 +#define DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_6MHZ 1
32042 +
32043 +/**
32044 + * 0 - Use cC FIFO size parameters
32045 + * 1 - Allow dynamic FIFO sizing (default)
32046 + */
32047 +extern int dwc_otg_set_param_enable_dynamic_fifo(dwc_otg_core_if_t * core_if,
32048 + int32_t val);
32049 +extern int32_t dwc_otg_get_param_enable_dynamic_fifo(dwc_otg_core_if_t *
32050 + core_if);
32051 +#define dwc_param_enable_dynamic_fifo_default 1
32052 +
32053 +/** Total number of 4-byte words in the data FIFO memory. This
32054 + * memory includes the Rx FIFO, non-periodic Tx FIFO, and periodic
32055 + * Tx FIFOs.
32056 + * 32 to 32768 (default 8192)
32057 + * Note: The total FIFO memory depth in the FPGA configuration is 8192.
32058 + */
32059 +extern int dwc_otg_set_param_data_fifo_size(dwc_otg_core_if_t * core_if,
32060 + int32_t val);
32061 +extern int32_t dwc_otg_get_param_data_fifo_size(dwc_otg_core_if_t * core_if);
32062 +//#define dwc_param_data_fifo_size_default 8192
32063 +#define dwc_param_data_fifo_size_default 0xFF0 // Broadcom BCM2708
32064 +
32065 +/** Number of 4-byte words in the Rx FIFO in device mode when dynamic
32066 + * FIFO sizing is enabled.
32067 + * 16 to 32768 (default 1064)
32068 + */
32069 +extern int dwc_otg_set_param_dev_rx_fifo_size(dwc_otg_core_if_t * core_if,
32070 + int32_t val);
32071 +extern int32_t dwc_otg_get_param_dev_rx_fifo_size(dwc_otg_core_if_t * core_if);
32072 +#define dwc_param_dev_rx_fifo_size_default 1064
32073 +
32074 +/** Number of 4-byte words in the non-periodic Tx FIFO in device mode
32075 + * when dynamic FIFO sizing is enabled.
32076 + * 16 to 32768 (default 1024)
32077 + */
32078 +extern int dwc_otg_set_param_dev_nperio_tx_fifo_size(dwc_otg_core_if_t *
32079 + core_if, int32_t val);
32080 +extern int32_t dwc_otg_get_param_dev_nperio_tx_fifo_size(dwc_otg_core_if_t *
32081 + core_if);
32082 +#define dwc_param_dev_nperio_tx_fifo_size_default 1024
32083 +
32084 +/** Number of 4-byte words in each of the periodic Tx FIFOs in device
32085 + * mode when dynamic FIFO sizing is enabled.
32086 + * 4 to 768 (default 256)
32087 + */
32088 +extern int dwc_otg_set_param_dev_perio_tx_fifo_size(dwc_otg_core_if_t * core_if,
32089 + int32_t val, int fifo_num);
32090 +extern int32_t dwc_otg_get_param_dev_perio_tx_fifo_size(dwc_otg_core_if_t *
32091 + core_if, int fifo_num);
32092 +#define dwc_param_dev_perio_tx_fifo_size_default 256
32093 +
32094 +/** Number of 4-byte words in the Rx FIFO in host mode when dynamic
32095 + * FIFO sizing is enabled.
32096 + * 16 to 32768 (default 1024)
32097 + */
32098 +extern int dwc_otg_set_param_host_rx_fifo_size(dwc_otg_core_if_t * core_if,
32099 + int32_t val);
32100 +extern int32_t dwc_otg_get_param_host_rx_fifo_size(dwc_otg_core_if_t * core_if);
32101 +//#define dwc_param_host_rx_fifo_size_default 1024
32102 +#define dwc_param_host_rx_fifo_size_default 774 // Broadcom BCM2708
32103 +
32104 +/** Number of 4-byte words in the non-periodic Tx FIFO in host mode
32105 + * when Dynamic FIFO sizing is enabled in the core.
32106 + * 16 to 32768 (default 1024)
32107 + */
32108 +extern int dwc_otg_set_param_host_nperio_tx_fifo_size(dwc_otg_core_if_t *
32109 + core_if, int32_t val);
32110 +extern int32_t dwc_otg_get_param_host_nperio_tx_fifo_size(dwc_otg_core_if_t *
32111 + core_if);
32112 +//#define dwc_param_host_nperio_tx_fifo_size_default 1024
32113 +#define dwc_param_host_nperio_tx_fifo_size_default 0x100 // Broadcom BCM2708
32114 +
32115 +/** Number of 4-byte words in the host periodic Tx FIFO when dynamic
32116 + * FIFO sizing is enabled.
32117 + * 16 to 32768 (default 1024)
32118 + */
32119 +extern int dwc_otg_set_param_host_perio_tx_fifo_size(dwc_otg_core_if_t *
32120 + core_if, int32_t val);
32121 +extern int32_t dwc_otg_get_param_host_perio_tx_fifo_size(dwc_otg_core_if_t *
32122 + core_if);
32123 +//#define dwc_param_host_perio_tx_fifo_size_default 1024
32124 +#define dwc_param_host_perio_tx_fifo_size_default 0x200 // Broadcom BCM2708
32125 +
32126 +/** The maximum transfer size supported in bytes.
32127 + * 2047 to 65,535 (default 65,535)
32128 + */
32129 +extern int dwc_otg_set_param_max_transfer_size(dwc_otg_core_if_t * core_if,
32130 + int32_t val);
32131 +extern int32_t dwc_otg_get_param_max_transfer_size(dwc_otg_core_if_t * core_if);
32132 +#define dwc_param_max_transfer_size_default 65535
32133 +
32134 +/** The maximum number of packets in a transfer.
32135 + * 15 to 511 (default 511)
32136 + */
32137 +extern int dwc_otg_set_param_max_packet_count(dwc_otg_core_if_t * core_if,
32138 + int32_t val);
32139 +extern int32_t dwc_otg_get_param_max_packet_count(dwc_otg_core_if_t * core_if);
32140 +#define dwc_param_max_packet_count_default 511
32141 +
32142 +/** The number of host channel registers to use.
32143 + * 1 to 16 (default 12)
32144 + * Note: The FPGA configuration supports a maximum of 12 host channels.
32145 + */
32146 +extern int dwc_otg_set_param_host_channels(dwc_otg_core_if_t * core_if,
32147 + int32_t val);
32148 +extern int32_t dwc_otg_get_param_host_channels(dwc_otg_core_if_t * core_if);
32149 +//#define dwc_param_host_channels_default 12
32150 +#define dwc_param_host_channels_default 8 // Broadcom BCM2708
32151 +
32152 +/** The number of endpoints in addition to EP0 available for device
32153 + * mode operations.
32154 + * 1 to 15 (default 6 IN and OUT)
32155 + * Note: The FPGA configuration supports a maximum of 6 IN and OUT
32156 + * endpoints in addition to EP0.
32157 + */
32158 +extern int dwc_otg_set_param_dev_endpoints(dwc_otg_core_if_t * core_if,
32159 + int32_t val);
32160 +extern int32_t dwc_otg_get_param_dev_endpoints(dwc_otg_core_if_t * core_if);
32161 +#define dwc_param_dev_endpoints_default 6
32162 +
32163 +/**
32164 + * Specifies the type of PHY interface to use. By default, the driver
32165 + * will automatically detect the phy_type.
32166 + *
32167 + * 0 - Full Speed PHY
32168 + * 1 - UTMI+ (default)
32169 + * 2 - ULPI
32170 + */
32171 +extern int dwc_otg_set_param_phy_type(dwc_otg_core_if_t * core_if, int32_t val);
32172 +extern int32_t dwc_otg_get_param_phy_type(dwc_otg_core_if_t * core_if);
32173 +#define DWC_PHY_TYPE_PARAM_FS 0
32174 +#define DWC_PHY_TYPE_PARAM_UTMI 1
32175 +#define DWC_PHY_TYPE_PARAM_ULPI 2
32176 +#define dwc_param_phy_type_default DWC_PHY_TYPE_PARAM_UTMI
32177 +
32178 +/**
32179 + * Specifies the UTMI+ Data Width. This parameter is
32180 + * applicable for a PHY_TYPE of UTMI+ or ULPI. (For a ULPI
32181 + * PHY_TYPE, this parameter indicates the data width between
32182 + * the MAC and the ULPI Wrapper.) Also, this parameter is
32183 + * applicable only if the OTG_HSPHY_WIDTH cC parameter was set
32184 + * to "8 and 16 bits", meaning that the core has been
32185 + * configured to work at either data path width.
32186 + *
32187 + * 8 or 16 bits (default 16)
32188 + */
32189 +extern int dwc_otg_set_param_phy_utmi_width(dwc_otg_core_if_t * core_if,
32190 + int32_t val);
32191 +extern int32_t dwc_otg_get_param_phy_utmi_width(dwc_otg_core_if_t * core_if);
32192 +//#define dwc_param_phy_utmi_width_default 16
32193 +#define dwc_param_phy_utmi_width_default 8 // Broadcom BCM2708
32194 +
32195 +/**
32196 + * Specifies whether the ULPI operates at double or single
32197 + * data rate. This parameter is only applicable if PHY_TYPE is
32198 + * ULPI.
32199 + *
32200 + * 0 - single data rate ULPI interface with 8 bit wide data
32201 + * bus (default)
32202 + * 1 - double data rate ULPI interface with 4 bit wide data
32203 + * bus
32204 + */
32205 +extern int dwc_otg_set_param_phy_ulpi_ddr(dwc_otg_core_if_t * core_if,
32206 + int32_t val);
32207 +extern int32_t dwc_otg_get_param_phy_ulpi_ddr(dwc_otg_core_if_t * core_if);
32208 +#define dwc_param_phy_ulpi_ddr_default 0
32209 +
32210 +/**
32211 + * Specifies whether to use the internal or external supply to
32212 + * drive the vbus with a ULPI phy.
32213 + */
32214 +extern int dwc_otg_set_param_phy_ulpi_ext_vbus(dwc_otg_core_if_t * core_if,
32215 + int32_t val);
32216 +extern int32_t dwc_otg_get_param_phy_ulpi_ext_vbus(dwc_otg_core_if_t * core_if);
32217 +#define DWC_PHY_ULPI_INTERNAL_VBUS 0
32218 +#define DWC_PHY_ULPI_EXTERNAL_VBUS 1
32219 +#define dwc_param_phy_ulpi_ext_vbus_default DWC_PHY_ULPI_INTERNAL_VBUS
32220 +
32221 +/**
32222 + * Specifies whether to use the I2Cinterface for full speed PHY. This
32223 + * parameter is only applicable if PHY_TYPE is FS.
32224 + * 0 - No (default)
32225 + * 1 - Yes
32226 + */
32227 +extern int dwc_otg_set_param_i2c_enable(dwc_otg_core_if_t * core_if,
32228 + int32_t val);
32229 +extern int32_t dwc_otg_get_param_i2c_enable(dwc_otg_core_if_t * core_if);
32230 +#define dwc_param_i2c_enable_default 0
32231 +
32232 +extern int dwc_otg_set_param_ulpi_fs_ls(dwc_otg_core_if_t * core_if,
32233 + int32_t val);
32234 +extern int32_t dwc_otg_get_param_ulpi_fs_ls(dwc_otg_core_if_t * core_if);
32235 +#define dwc_param_ulpi_fs_ls_default 0
32236 +
32237 +extern int dwc_otg_set_param_ts_dline(dwc_otg_core_if_t * core_if, int32_t val);
32238 +extern int32_t dwc_otg_get_param_ts_dline(dwc_otg_core_if_t * core_if);
32239 +#define dwc_param_ts_dline_default 0
32240 +
32241 +/**
32242 + * Specifies whether dedicated transmit FIFOs are
32243 + * enabled for non periodic IN endpoints in device mode
32244 + * 0 - No
32245 + * 1 - Yes
32246 + */
32247 +extern int dwc_otg_set_param_en_multiple_tx_fifo(dwc_otg_core_if_t * core_if,
32248 + int32_t val);
32249 +extern int32_t dwc_otg_get_param_en_multiple_tx_fifo(dwc_otg_core_if_t *
32250 + core_if);
32251 +#define dwc_param_en_multiple_tx_fifo_default 1
32252 +
32253 +/** Number of 4-byte words in each of the Tx FIFOs in device
32254 + * mode when dynamic FIFO sizing is enabled.
32255 + * 4 to 768 (default 256)
32256 + */
32257 +extern int dwc_otg_set_param_dev_tx_fifo_size(dwc_otg_core_if_t * core_if,
32258 + int fifo_num, int32_t val);
32259 +extern int32_t dwc_otg_get_param_dev_tx_fifo_size(dwc_otg_core_if_t * core_if,
32260 + int fifo_num);
32261 +#define dwc_param_dev_tx_fifo_size_default 768
32262 +
32263 +/** Thresholding enable flag-
32264 + * bit 0 - enable non-ISO Tx thresholding
32265 + * bit 1 - enable ISO Tx thresholding
32266 + * bit 2 - enable Rx thresholding
32267 + */
32268 +extern int dwc_otg_set_param_thr_ctl(dwc_otg_core_if_t * core_if, int32_t val);
32269 +extern int32_t dwc_otg_get_thr_ctl(dwc_otg_core_if_t * core_if, int fifo_num);
32270 +#define dwc_param_thr_ctl_default 0
32271 +
32272 +/** Thresholding length for Tx
32273 + * FIFOs in 32 bit DWORDs
32274 + */
32275 +extern int dwc_otg_set_param_tx_thr_length(dwc_otg_core_if_t * core_if,
32276 + int32_t val);
32277 +extern int32_t dwc_otg_get_tx_thr_length(dwc_otg_core_if_t * core_if);
32278 +#define dwc_param_tx_thr_length_default 64
32279 +
32280 +/** Thresholding length for Rx
32281 + * FIFOs in 32 bit DWORDs
32282 + */
32283 +extern int dwc_otg_set_param_rx_thr_length(dwc_otg_core_if_t * core_if,
32284 + int32_t val);
32285 +extern int32_t dwc_otg_get_rx_thr_length(dwc_otg_core_if_t * core_if);
32286 +#define dwc_param_rx_thr_length_default 64
32287 +
32288 +/**
32289 + * Specifies whether LPM (Link Power Management) support is enabled
32290 + */
32291 +extern int dwc_otg_set_param_lpm_enable(dwc_otg_core_if_t * core_if,
32292 + int32_t val);
32293 +extern int32_t dwc_otg_get_param_lpm_enable(dwc_otg_core_if_t * core_if);
32294 +#define dwc_param_lpm_enable_default 1
32295 +
32296 +/**
32297 + * Specifies whether PTI enhancement is enabled
32298 + */
32299 +extern int dwc_otg_set_param_pti_enable(dwc_otg_core_if_t * core_if,
32300 + int32_t val);
32301 +extern int32_t dwc_otg_get_param_pti_enable(dwc_otg_core_if_t * core_if);
32302 +#define dwc_param_pti_enable_default 0
32303 +
32304 +/**
32305 + * Specifies whether MPI enhancement is enabled
32306 + */
32307 +extern int dwc_otg_set_param_mpi_enable(dwc_otg_core_if_t * core_if,
32308 + int32_t val);
32309 +extern int32_t dwc_otg_get_param_mpi_enable(dwc_otg_core_if_t * core_if);
32310 +#define dwc_param_mpi_enable_default 0
32311 +
32312 +/**
32313 + * Specifies whether ADP capability is enabled
32314 + */
32315 +extern int dwc_otg_set_param_adp_enable(dwc_otg_core_if_t * core_if,
32316 + int32_t val);
32317 +extern int32_t dwc_otg_get_param_adp_enable(dwc_otg_core_if_t * core_if);
32318 +#define dwc_param_adp_enable_default 0
32319 +
32320 +/**
32321 + * Specifies whether IC_USB capability is enabled
32322 + */
32323 +
32324 +extern int dwc_otg_set_param_ic_usb_cap(dwc_otg_core_if_t * core_if,
32325 + int32_t val);
32326 +extern int32_t dwc_otg_get_param_ic_usb_cap(dwc_otg_core_if_t * core_if);
32327 +#define dwc_param_ic_usb_cap_default 0
32328 +
32329 +extern int dwc_otg_set_param_ahb_thr_ratio(dwc_otg_core_if_t * core_if,
32330 + int32_t val);
32331 +extern int32_t dwc_otg_get_param_ahb_thr_ratio(dwc_otg_core_if_t * core_if);
32332 +#define dwc_param_ahb_thr_ratio_default 0
32333 +
32334 +extern int dwc_otg_set_param_power_down(dwc_otg_core_if_t * core_if,
32335 + int32_t val);
32336 +extern int32_t dwc_otg_get_param_power_down(dwc_otg_core_if_t * core_if);
32337 +#define dwc_param_power_down_default 0
32338 +
32339 +extern int dwc_otg_set_param_reload_ctl(dwc_otg_core_if_t * core_if,
32340 + int32_t val);
32341 +extern int32_t dwc_otg_get_param_reload_ctl(dwc_otg_core_if_t * core_if);
32342 +#define dwc_param_reload_ctl_default 0
32343 +
32344 +extern int dwc_otg_set_param_dev_out_nak(dwc_otg_core_if_t * core_if,
32345 + int32_t val);
32346 +extern int32_t dwc_otg_get_param_dev_out_nak(dwc_otg_core_if_t * core_if);
32347 +#define dwc_param_dev_out_nak_default 0
32348 +
32349 +extern int dwc_otg_set_param_cont_on_bna(dwc_otg_core_if_t * core_if,
32350 + int32_t val);
32351 +extern int32_t dwc_otg_get_param_cont_on_bna(dwc_otg_core_if_t * core_if);
32352 +#define dwc_param_cont_on_bna_default 0
32353 +
32354 +extern int dwc_otg_set_param_ahb_single(dwc_otg_core_if_t * core_if,
32355 + int32_t val);
32356 +extern int32_t dwc_otg_get_param_ahb_single(dwc_otg_core_if_t * core_if);
32357 +#define dwc_param_ahb_single_default 0
32358 +
32359 +extern int dwc_otg_set_param_otg_ver(dwc_otg_core_if_t * core_if, int32_t val);
32360 +extern int32_t dwc_otg_get_param_otg_ver(dwc_otg_core_if_t * core_if);
32361 +#define dwc_param_otg_ver_default 0
32362 +
32363 +/** @} */
32364 +
32365 +/** @name Access to registers and bit-fields */
32366 +
32367 +/**
32368 + * Dump core registers and SPRAM
32369 + */
32370 +extern void dwc_otg_dump_dev_registers(dwc_otg_core_if_t * _core_if);
32371 +extern void dwc_otg_dump_spram(dwc_otg_core_if_t * _core_if);
32372 +extern void dwc_otg_dump_host_registers(dwc_otg_core_if_t * _core_if);
32373 +extern void dwc_otg_dump_global_registers(dwc_otg_core_if_t * _core_if);
32374 +
32375 +/**
32376 + * Get host negotiation status.
32377 + */
32378 +extern uint32_t dwc_otg_get_hnpstatus(dwc_otg_core_if_t * core_if);
32379 +
32380 +/**
32381 + * Get srp status
32382 + */
32383 +extern uint32_t dwc_otg_get_srpstatus(dwc_otg_core_if_t * core_if);
32384 +
32385 +/**
32386 + * Set hnpreq bit in the GOTGCTL register.
32387 + */
32388 +extern void dwc_otg_set_hnpreq(dwc_otg_core_if_t * core_if, uint32_t val);
32389 +
32390 +/**
32391 + * Get Content of SNPSID register.
32392 + */
32393 +extern uint32_t dwc_otg_get_gsnpsid(dwc_otg_core_if_t * core_if);
32394 +
32395 +/**
32396 + * Get current mode.
32397 + * Returns 0 if in device mode, and 1 if in host mode.
32398 + */
32399 +extern uint32_t dwc_otg_get_mode(dwc_otg_core_if_t * core_if);
32400 +
32401 +/**
32402 + * Get value of hnpcapable field in the GUSBCFG register
32403 + */
32404 +extern uint32_t dwc_otg_get_hnpcapable(dwc_otg_core_if_t * core_if);
32405 +/**
32406 + * Set value of hnpcapable field in the GUSBCFG register
32407 + */
32408 +extern void dwc_otg_set_hnpcapable(dwc_otg_core_if_t * core_if, uint32_t val);
32409 +
32410 +/**
32411 + * Get value of srpcapable field in the GUSBCFG register
32412 + */
32413 +extern uint32_t dwc_otg_get_srpcapable(dwc_otg_core_if_t * core_if);
32414 +/**
32415 + * Set value of srpcapable field in the GUSBCFG register
32416 + */
32417 +extern void dwc_otg_set_srpcapable(dwc_otg_core_if_t * core_if, uint32_t val);
32418 +
32419 +/**
32420 + * Get value of devspeed field in the DCFG register
32421 + */
32422 +extern uint32_t dwc_otg_get_devspeed(dwc_otg_core_if_t * core_if);
32423 +/**
32424 + * Set value of devspeed field in the DCFG register
32425 + */
32426 +extern void dwc_otg_set_devspeed(dwc_otg_core_if_t * core_if, uint32_t val);
32427 +
32428 +/**
32429 + * Get the value of busconnected field from the HPRT0 register
32430 + */
32431 +extern uint32_t dwc_otg_get_busconnected(dwc_otg_core_if_t * core_if);
32432 +
32433 +/**
32434 + * Gets the device enumeration Speed.
32435 + */
32436 +extern uint32_t dwc_otg_get_enumspeed(dwc_otg_core_if_t * core_if);
32437 +
32438 +/**
32439 + * Get value of prtpwr field from the HPRT0 register
32440 + */
32441 +extern uint32_t dwc_otg_get_prtpower(dwc_otg_core_if_t * core_if);
32442 +
32443 +/**
32444 + * Get value of flag indicating core state - hibernated or not
32445 + */
32446 +extern uint32_t dwc_otg_get_core_state(dwc_otg_core_if_t * core_if);
32447 +
32448 +/**
32449 + * Set value of prtpwr field from the HPRT0 register
32450 + */
32451 +extern void dwc_otg_set_prtpower(dwc_otg_core_if_t * core_if, uint32_t val);
32452 +
32453 +/**
32454 + * Get value of prtsusp field from the HPRT0 regsiter
32455 + */
32456 +extern uint32_t dwc_otg_get_prtsuspend(dwc_otg_core_if_t * core_if);
32457 +/**
32458 + * Set value of prtpwr field from the HPRT0 register
32459 + */
32460 +extern void dwc_otg_set_prtsuspend(dwc_otg_core_if_t * core_if, uint32_t val);
32461 +
32462 +/**
32463 + * Get value of ModeChTimEn field from the HCFG regsiter
32464 + */
32465 +extern uint32_t dwc_otg_get_mode_ch_tim(dwc_otg_core_if_t * core_if);
32466 +/**
32467 + * Set value of ModeChTimEn field from the HCFG regsiter
32468 + */
32469 +extern void dwc_otg_set_mode_ch_tim(dwc_otg_core_if_t * core_if, uint32_t val);
32470 +
32471 +/**
32472 + * Get value of Fram Interval field from the HFIR regsiter
32473 + */
32474 +extern uint32_t dwc_otg_get_fr_interval(dwc_otg_core_if_t * core_if);
32475 +/**
32476 + * Set value of Frame Interval field from the HFIR regsiter
32477 + */
32478 +extern void dwc_otg_set_fr_interval(dwc_otg_core_if_t * core_if, uint32_t val);
32479 +
32480 +/**
32481 + * Set value of prtres field from the HPRT0 register
32482 + *FIXME Remove?
32483 + */
32484 +extern void dwc_otg_set_prtresume(dwc_otg_core_if_t * core_if, uint32_t val);
32485 +
32486 +/**
32487 + * Get value of rmtwkupsig bit in DCTL register
32488 + */
32489 +extern uint32_t dwc_otg_get_remotewakesig(dwc_otg_core_if_t * core_if);
32490 +
32491 +/**
32492 + * Get value of prt_sleep_sts field from the GLPMCFG register
32493 + */
32494 +extern uint32_t dwc_otg_get_lpm_portsleepstatus(dwc_otg_core_if_t * core_if);
32495 +
32496 +/**
32497 + * Get value of rem_wkup_en field from the GLPMCFG register
32498 + */
32499 +extern uint32_t dwc_otg_get_lpm_remotewakeenabled(dwc_otg_core_if_t * core_if);
32500 +
32501 +/**
32502 + * Get value of appl_resp field from the GLPMCFG register
32503 + */
32504 +extern uint32_t dwc_otg_get_lpmresponse(dwc_otg_core_if_t * core_if);
32505 +/**
32506 + * Set value of appl_resp field from the GLPMCFG register
32507 + */
32508 +extern void dwc_otg_set_lpmresponse(dwc_otg_core_if_t * core_if, uint32_t val);
32509 +
32510 +/**
32511 + * Get value of hsic_connect field from the GLPMCFG register
32512 + */
32513 +extern uint32_t dwc_otg_get_hsic_connect(dwc_otg_core_if_t * core_if);
32514 +/**
32515 + * Set value of hsic_connect field from the GLPMCFG register
32516 + */
32517 +extern void dwc_otg_set_hsic_connect(dwc_otg_core_if_t * core_if, uint32_t val);
32518 +
32519 +/**
32520 + * Get value of inv_sel_hsic field from the GLPMCFG register.
32521 + */
32522 +extern uint32_t dwc_otg_get_inv_sel_hsic(dwc_otg_core_if_t * core_if);
32523 +/**
32524 + * Set value of inv_sel_hsic field from the GLPMFG register.
32525 + */
32526 +extern void dwc_otg_set_inv_sel_hsic(dwc_otg_core_if_t * core_if, uint32_t val);
32527 +
32528 +/*
32529 + * Some functions for accessing registers
32530 + */
32531 +
32532 +/**
32533 + * GOTGCTL register
32534 + */
32535 +extern uint32_t dwc_otg_get_gotgctl(dwc_otg_core_if_t * core_if);
32536 +extern void dwc_otg_set_gotgctl(dwc_otg_core_if_t * core_if, uint32_t val);
32537 +
32538 +/**
32539 + * GUSBCFG register
32540 + */
32541 +extern uint32_t dwc_otg_get_gusbcfg(dwc_otg_core_if_t * core_if);
32542 +extern void dwc_otg_set_gusbcfg(dwc_otg_core_if_t * core_if, uint32_t val);
32543 +
32544 +/**
32545 + * GRXFSIZ register
32546 + */
32547 +extern uint32_t dwc_otg_get_grxfsiz(dwc_otg_core_if_t * core_if);
32548 +extern void dwc_otg_set_grxfsiz(dwc_otg_core_if_t * core_if, uint32_t val);
32549 +
32550 +/**
32551 + * GNPTXFSIZ register
32552 + */
32553 +extern uint32_t dwc_otg_get_gnptxfsiz(dwc_otg_core_if_t * core_if);
32554 +extern void dwc_otg_set_gnptxfsiz(dwc_otg_core_if_t * core_if, uint32_t val);
32555 +
32556 +extern uint32_t dwc_otg_get_gpvndctl(dwc_otg_core_if_t * core_if);
32557 +extern void dwc_otg_set_gpvndctl(dwc_otg_core_if_t * core_if, uint32_t val);
32558 +
32559 +/**
32560 + * GGPIO register
32561 + */
32562 +extern uint32_t dwc_otg_get_ggpio(dwc_otg_core_if_t * core_if);
32563 +extern void dwc_otg_set_ggpio(dwc_otg_core_if_t * core_if, uint32_t val);
32564 +
32565 +/**
32566 + * GUID register
32567 + */
32568 +extern uint32_t dwc_otg_get_guid(dwc_otg_core_if_t * core_if);
32569 +extern void dwc_otg_set_guid(dwc_otg_core_if_t * core_if, uint32_t val);
32570 +
32571 +/**
32572 + * HPRT0 register
32573 + */
32574 +extern uint32_t dwc_otg_get_hprt0(dwc_otg_core_if_t * core_if);
32575 +extern void dwc_otg_set_hprt0(dwc_otg_core_if_t * core_if, uint32_t val);
32576 +
32577 +/**
32578 + * GHPTXFSIZE
32579 + */
32580 +extern uint32_t dwc_otg_get_hptxfsiz(dwc_otg_core_if_t * core_if);
32581 +
32582 +/** @} */
32583 +
32584 +#endif /* __DWC_CORE_IF_H__ */
32585 --- /dev/null
32586 +++ b/drivers/usb/host/dwc_otg/dwc_otg_dbg.h
32587 @@ -0,0 +1,117 @@
32588 +/* ==========================================================================
32589 + *
32590 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
32591 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
32592 + * otherwise expressly agreed to in writing between Synopsys and you.
32593 + *
32594 + * The Software IS NOT an item of Licensed Software or Licensed Product under
32595 + * any End User Software License Agreement or Agreement for Licensed Product
32596 + * with Synopsys or any supplement thereto. You are permitted to use and
32597 + * redistribute this Software in source and binary forms, with or without
32598 + * modification, provided that redistributions of source code must retain this
32599 + * notice. You may not view, use, disclose, copy or distribute this file or
32600 + * any information contained herein except pursuant to this license grant from
32601 + * Synopsys. If you do not agree with this notice, including the disclaimer
32602 + * below, then you are not authorized to use the Software.
32603 + *
32604 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
32605 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32606 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32607 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
32608 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
32609 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
32610 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32611 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32612 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32613 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
32614 + * DAMAGE.
32615 + * ========================================================================== */
32616 +
32617 +#ifndef __DWC_OTG_DBG_H__
32618 +#define __DWC_OTG_DBG_H__
32619 +
32620 +/** @file
32621 + * This file defines debug levels.
32622 + * Debugging support vanishes in non-debug builds.
32623 + */
32624 +
32625 +/**
32626 + * The Debug Level bit-mask variable.
32627 + */
32628 +extern uint32_t g_dbg_lvl;
32629 +/**
32630 + * Set the Debug Level variable.
32631 + */
32632 +static inline uint32_t SET_DEBUG_LEVEL(const uint32_t new)
32633 +{
32634 + uint32_t old = g_dbg_lvl;
32635 + g_dbg_lvl = new;
32636 + return old;
32637 +}
32638 +
32639 +#define DBG_USER (0x1)
32640 +/** When debug level has the DBG_CIL bit set, display CIL Debug messages. */
32641 +#define DBG_CIL (0x2)
32642 +/** When debug level has the DBG_CILV bit set, display CIL Verbose debug
32643 + * messages */
32644 +#define DBG_CILV (0x20)
32645 +/** When debug level has the DBG_PCD bit set, display PCD (Device) debug
32646 + * messages */
32647 +#define DBG_PCD (0x4)
32648 +/** When debug level has the DBG_PCDV set, display PCD (Device) Verbose debug
32649 + * messages */
32650 +#define DBG_PCDV (0x40)
32651 +/** When debug level has the DBG_HCD bit set, display Host debug messages */
32652 +#define DBG_HCD (0x8)
32653 +/** When debug level has the DBG_HCDV bit set, display Verbose Host debug
32654 + * messages */
32655 +#define DBG_HCDV (0x80)
32656 +/** When debug level has the DBG_HCD_URB bit set, display enqueued URBs in host
32657 + * mode. */
32658 +#define DBG_HCD_URB (0x800)
32659 +/** When debug level has the DBG_HCDI bit set, display host interrupt
32660 + * messages. */
32661 +#define DBG_HCDI (0x1000)
32662 +
32663 +/** When debug level has any bit set, display debug messages */
32664 +#define DBG_ANY (0xFF)
32665 +
32666 +/** All debug messages off */
32667 +#define DBG_OFF 0
32668 +
32669 +/** Prefix string for DWC_DEBUG print macros. */
32670 +#define USB_DWC "DWC_otg: "
32671 +
32672 +/**
32673 + * Print a debug message when the Global debug level variable contains
32674 + * the bit defined in <code>lvl</code>.
32675 + *
32676 + * @param[in] lvl - Debug level, use one of the DBG_ constants above.
32677 + * @param[in] x - like printf
32678 + *
32679 + * Example:<p>
32680 + * <code>
32681 + * DWC_DEBUGPL( DBG_ANY, "%s(%p)\n", __func__, _reg_base_addr);
32682 + * </code>
32683 + * <br>
32684 + * results in:<br>
32685 + * <code>
32686 + * usb-DWC_otg: dwc_otg_cil_init(ca867000)
32687 + * </code>
32688 + */
32689 +#ifdef DEBUG
32690 +
32691 +# define DWC_DEBUGPL(lvl, x...) do{ if ((lvl)&g_dbg_lvl)__DWC_DEBUG(USB_DWC x ); }while(0)
32692 +# define DWC_DEBUGP(x...) DWC_DEBUGPL(DBG_ANY, x )
32693 +
32694 +# define CHK_DEBUG_LEVEL(level) ((level) & g_dbg_lvl)
32695 +
32696 +#else
32697 +
32698 +# define DWC_DEBUGPL(lvl, x...) do{}while(0)
32699 +# define DWC_DEBUGP(x...)
32700 +
32701 +# define CHK_DEBUG_LEVEL(level) (0)
32702 +
32703 +#endif /*DEBUG*/
32704 +#endif
32705 --- /dev/null
32706 +++ b/drivers/usb/host/dwc_otg/dwc_otg_driver.c
32707 @@ -0,0 +1,1760 @@
32708 +/* ==========================================================================
32709 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_driver.c $
32710 + * $Revision: #92 $
32711 + * $Date: 2012/08/10 $
32712 + * $Change: 2047372 $
32713 + *
32714 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
32715 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
32716 + * otherwise expressly agreed to in writing between Synopsys and you.
32717 + *
32718 + * The Software IS NOT an item of Licensed Software or Licensed Product under
32719 + * any End User Software License Agreement or Agreement for Licensed Product
32720 + * with Synopsys or any supplement thereto. You are permitted to use and
32721 + * redistribute this Software in source and binary forms, with or without
32722 + * modification, provided that redistributions of source code must retain this
32723 + * notice. You may not view, use, disclose, copy or distribute this file or
32724 + * any information contained herein except pursuant to this license grant from
32725 + * Synopsys. If you do not agree with this notice, including the disclaimer
32726 + * below, then you are not authorized to use the Software.
32727 + *
32728 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
32729 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32730 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32731 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
32732 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
32733 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
32734 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32735 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32736 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32737 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
32738 + * DAMAGE.
32739 + * ========================================================================== */
32740 +
32741 +/** @file
32742 + * The dwc_otg_driver module provides the initialization and cleanup entry
32743 + * points for the DWC_otg driver. This module will be dynamically installed
32744 + * after Linux is booted using the insmod command. When the module is
32745 + * installed, the dwc_otg_driver_init function is called. When the module is
32746 + * removed (using rmmod), the dwc_otg_driver_cleanup function is called.
32747 + *
32748 + * This module also defines a data structure for the dwc_otg_driver, which is
32749 + * used in conjunction with the standard ARM lm_device structure. These
32750 + * structures allow the OTG driver to comply with the standard Linux driver
32751 + * model in which devices and drivers are registered with a bus driver. This
32752 + * has the benefit that Linux can expose attributes of the driver and device
32753 + * in its special sysfs file system. Users can then read or write files in
32754 + * this file system to perform diagnostics on the driver components or the
32755 + * device.
32756 + */
32757 +
32758 +#include "dwc_otg_os_dep.h"
32759 +#include "dwc_os.h"
32760 +#include "dwc_otg_dbg.h"
32761 +#include "dwc_otg_driver.h"
32762 +#include "dwc_otg_attr.h"
32763 +#include "dwc_otg_core_if.h"
32764 +#include "dwc_otg_pcd_if.h"
32765 +#include "dwc_otg_hcd_if.h"
32766 +#include "dwc_otg_fiq_fsm.h"
32767 +
32768 +#define DWC_DRIVER_VERSION "3.00a 10-AUG-2012"
32769 +#define DWC_DRIVER_DESC "HS OTG USB Controller driver"
32770 +
32771 +bool microframe_schedule=true;
32772 +
32773 +static const char dwc_driver_name[] = "dwc_otg";
32774 +
32775 +
32776 +extern int pcd_init(
32777 +#ifdef LM_INTERFACE
32778 + struct lm_device *_dev
32779 +#elif defined(PCI_INTERFACE)
32780 + struct pci_dev *_dev
32781 +#elif defined(PLATFORM_INTERFACE)
32782 + struct platform_device *dev
32783 +#endif
32784 + );
32785 +extern int hcd_init(
32786 +#ifdef LM_INTERFACE
32787 + struct lm_device *_dev
32788 +#elif defined(PCI_INTERFACE)
32789 + struct pci_dev *_dev
32790 +#elif defined(PLATFORM_INTERFACE)
32791 + struct platform_device *dev
32792 +#endif
32793 + );
32794 +
32795 +extern int pcd_remove(
32796 +#ifdef LM_INTERFACE
32797 + struct lm_device *_dev
32798 +#elif defined(PCI_INTERFACE)
32799 + struct pci_dev *_dev
32800 +#elif defined(PLATFORM_INTERFACE)
32801 + struct platform_device *_dev
32802 +#endif
32803 + );
32804 +
32805 +extern void hcd_remove(
32806 +#ifdef LM_INTERFACE
32807 + struct lm_device *_dev
32808 +#elif defined(PCI_INTERFACE)
32809 + struct pci_dev *_dev
32810 +#elif defined(PLATFORM_INTERFACE)
32811 + struct platform_device *_dev
32812 +#endif
32813 + );
32814 +
32815 +extern void dwc_otg_adp_start(dwc_otg_core_if_t * core_if, uint8_t is_host);
32816 +
32817 +/*-------------------------------------------------------------------------*/
32818 +/* Encapsulate the module parameter settings */
32819 +
32820 +struct dwc_otg_driver_module_params {
32821 + int32_t opt;
32822 + int32_t otg_cap;
32823 + int32_t dma_enable;
32824 + int32_t dma_desc_enable;
32825 + int32_t dma_burst_size;
32826 + int32_t speed;
32827 + int32_t host_support_fs_ls_low_power;
32828 + int32_t host_ls_low_power_phy_clk;
32829 + int32_t enable_dynamic_fifo;
32830 + int32_t data_fifo_size;
32831 + int32_t dev_rx_fifo_size;
32832 + int32_t dev_nperio_tx_fifo_size;
32833 + uint32_t dev_perio_tx_fifo_size[MAX_PERIO_FIFOS];
32834 + int32_t host_rx_fifo_size;
32835 + int32_t host_nperio_tx_fifo_size;
32836 + int32_t host_perio_tx_fifo_size;
32837 + int32_t max_transfer_size;
32838 + int32_t max_packet_count;
32839 + int32_t host_channels;
32840 + int32_t dev_endpoints;
32841 + int32_t phy_type;
32842 + int32_t phy_utmi_width;
32843 + int32_t phy_ulpi_ddr;
32844 + int32_t phy_ulpi_ext_vbus;
32845 + int32_t i2c_enable;
32846 + int32_t ulpi_fs_ls;
32847 + int32_t ts_dline;
32848 + int32_t en_multiple_tx_fifo;
32849 + uint32_t dev_tx_fifo_size[MAX_TX_FIFOS];
32850 + uint32_t thr_ctl;
32851 + uint32_t tx_thr_length;
32852 + uint32_t rx_thr_length;
32853 + int32_t pti_enable;
32854 + int32_t mpi_enable;
32855 + int32_t lpm_enable;
32856 + int32_t ic_usb_cap;
32857 + int32_t ahb_thr_ratio;
32858 + int32_t power_down;
32859 + int32_t reload_ctl;
32860 + int32_t dev_out_nak;
32861 + int32_t cont_on_bna;
32862 + int32_t ahb_single;
32863 + int32_t otg_ver;
32864 + int32_t adp_enable;
32865 +};
32866 +
32867 +static struct dwc_otg_driver_module_params dwc_otg_module_params = {
32868 + .opt = -1,
32869 + .otg_cap = -1,
32870 + .dma_enable = -1,
32871 + .dma_desc_enable = -1,
32872 + .dma_burst_size = -1,
32873 + .speed = -1,
32874 + .host_support_fs_ls_low_power = -1,
32875 + .host_ls_low_power_phy_clk = -1,
32876 + .enable_dynamic_fifo = -1,
32877 + .data_fifo_size = -1,
32878 + .dev_rx_fifo_size = -1,
32879 + .dev_nperio_tx_fifo_size = -1,
32880 + .dev_perio_tx_fifo_size = {
32881 + /* dev_perio_tx_fifo_size_1 */
32882 + -1,
32883 + -1,
32884 + -1,
32885 + -1,
32886 + -1,
32887 + -1,
32888 + -1,
32889 + -1,
32890 + -1,
32891 + -1,
32892 + -1,
32893 + -1,
32894 + -1,
32895 + -1,
32896 + -1
32897 + /* 15 */
32898 + },
32899 + .host_rx_fifo_size = -1,
32900 + .host_nperio_tx_fifo_size = -1,
32901 + .host_perio_tx_fifo_size = -1,
32902 + .max_transfer_size = -1,
32903 + .max_packet_count = -1,
32904 + .host_channels = -1,
32905 + .dev_endpoints = -1,
32906 + .phy_type = -1,
32907 + .phy_utmi_width = -1,
32908 + .phy_ulpi_ddr = -1,
32909 + .phy_ulpi_ext_vbus = -1,
32910 + .i2c_enable = -1,
32911 + .ulpi_fs_ls = -1,
32912 + .ts_dline = -1,
32913 + .en_multiple_tx_fifo = -1,
32914 + .dev_tx_fifo_size = {
32915 + /* dev_tx_fifo_size */
32916 + -1,
32917 + -1,
32918 + -1,
32919 + -1,
32920 + -1,
32921 + -1,
32922 + -1,
32923 + -1,
32924 + -1,
32925 + -1,
32926 + -1,
32927 + -1,
32928 + -1,
32929 + -1,
32930 + -1
32931 + /* 15 */
32932 + },
32933 + .thr_ctl = -1,
32934 + .tx_thr_length = -1,
32935 + .rx_thr_length = -1,
32936 + .pti_enable = -1,
32937 + .mpi_enable = -1,
32938 + .lpm_enable = 0,
32939 + .ic_usb_cap = -1,
32940 + .ahb_thr_ratio = -1,
32941 + .power_down = -1,
32942 + .reload_ctl = -1,
32943 + .dev_out_nak = -1,
32944 + .cont_on_bna = -1,
32945 + .ahb_single = -1,
32946 + .otg_ver = -1,
32947 + .adp_enable = -1,
32948 +};
32949 +
32950 +//Global variable to switch the fiq fix on or off
32951 +bool fiq_enable = 1;
32952 +// Global variable to enable the split transaction fix
32953 +bool fiq_fsm_enable = true;
32954 +//Bulk split-transaction NAK holdoff in microframes
32955 +uint16_t nak_holdoff = 8;
32956 +
32957 +unsigned short fiq_fsm_mask = 0x0F;
32958 +
32959 +unsigned short int_ep_interval_min = 0;
32960 +/**
32961 + * This function shows the Driver Version.
32962 + */
32963 +static ssize_t version_show(struct device_driver *dev, char *buf)
32964 +{
32965 + return snprintf(buf, sizeof(DWC_DRIVER_VERSION) + 2, "%s\n",
32966 + DWC_DRIVER_VERSION);
32967 +}
32968 +
32969 +static DRIVER_ATTR_RO(version);
32970 +
32971 +/**
32972 + * Global Debug Level Mask.
32973 + */
32974 +uint32_t g_dbg_lvl = 0; /* OFF */
32975 +
32976 +/**
32977 + * This function shows the driver Debug Level.
32978 + */
32979 +static ssize_t debuglevel_show(struct device_driver *drv, char *buf)
32980 +{
32981 + return sprintf(buf, "0x%0x\n", g_dbg_lvl);
32982 +}
32983 +
32984 +/**
32985 + * This function stores the driver Debug Level.
32986 + */
32987 +static ssize_t debuglevel_store(struct device_driver *drv, const char *buf,
32988 + size_t count)
32989 +{
32990 + g_dbg_lvl = simple_strtoul(buf, NULL, 16);
32991 + return count;
32992 +}
32993 +
32994 +static DRIVER_ATTR_RW(debuglevel);
32995 +
32996 +/**
32997 + * This function is called during module intialization
32998 + * to pass module parameters to the DWC_OTG CORE.
32999 + */
33000 +static int set_parameters(dwc_otg_core_if_t * core_if)
33001 +{
33002 + int retval = 0;
33003 + int i;
33004 +
33005 + if (dwc_otg_module_params.otg_cap != -1) {
33006 + retval +=
33007 + dwc_otg_set_param_otg_cap(core_if,
33008 + dwc_otg_module_params.otg_cap);
33009 + }
33010 + if (dwc_otg_module_params.dma_enable != -1) {
33011 + retval +=
33012 + dwc_otg_set_param_dma_enable(core_if,
33013 + dwc_otg_module_params.
33014 + dma_enable);
33015 + }
33016 + if (dwc_otg_module_params.dma_desc_enable != -1) {
33017 + retval +=
33018 + dwc_otg_set_param_dma_desc_enable(core_if,
33019 + dwc_otg_module_params.
33020 + dma_desc_enable);
33021 + }
33022 + if (dwc_otg_module_params.opt != -1) {
33023 + retval +=
33024 + dwc_otg_set_param_opt(core_if, dwc_otg_module_params.opt);
33025 + }
33026 + if (dwc_otg_module_params.dma_burst_size != -1) {
33027 + retval +=
33028 + dwc_otg_set_param_dma_burst_size(core_if,
33029 + dwc_otg_module_params.
33030 + dma_burst_size);
33031 + }
33032 + if (dwc_otg_module_params.host_support_fs_ls_low_power != -1) {
33033 + retval +=
33034 + dwc_otg_set_param_host_support_fs_ls_low_power(core_if,
33035 + dwc_otg_module_params.
33036 + host_support_fs_ls_low_power);
33037 + }
33038 + if (dwc_otg_module_params.enable_dynamic_fifo != -1) {
33039 + retval +=
33040 + dwc_otg_set_param_enable_dynamic_fifo(core_if,
33041 + dwc_otg_module_params.
33042 + enable_dynamic_fifo);
33043 + }
33044 + if (dwc_otg_module_params.data_fifo_size != -1) {
33045 + retval +=
33046 + dwc_otg_set_param_data_fifo_size(core_if,
33047 + dwc_otg_module_params.
33048 + data_fifo_size);
33049 + }
33050 + if (dwc_otg_module_params.dev_rx_fifo_size != -1) {
33051 + retval +=
33052 + dwc_otg_set_param_dev_rx_fifo_size(core_if,
33053 + dwc_otg_module_params.
33054 + dev_rx_fifo_size);
33055 + }
33056 + if (dwc_otg_module_params.dev_nperio_tx_fifo_size != -1) {
33057 + retval +=
33058 + dwc_otg_set_param_dev_nperio_tx_fifo_size(core_if,
33059 + dwc_otg_module_params.
33060 + dev_nperio_tx_fifo_size);
33061 + }
33062 + if (dwc_otg_module_params.host_rx_fifo_size != -1) {
33063 + retval +=
33064 + dwc_otg_set_param_host_rx_fifo_size(core_if,
33065 + dwc_otg_module_params.host_rx_fifo_size);
33066 + }
33067 + if (dwc_otg_module_params.host_nperio_tx_fifo_size != -1) {
33068 + retval +=
33069 + dwc_otg_set_param_host_nperio_tx_fifo_size(core_if,
33070 + dwc_otg_module_params.
33071 + host_nperio_tx_fifo_size);
33072 + }
33073 + if (dwc_otg_module_params.host_perio_tx_fifo_size != -1) {
33074 + retval +=
33075 + dwc_otg_set_param_host_perio_tx_fifo_size(core_if,
33076 + dwc_otg_module_params.
33077 + host_perio_tx_fifo_size);
33078 + }
33079 + if (dwc_otg_module_params.max_transfer_size != -1) {
33080 + retval +=
33081 + dwc_otg_set_param_max_transfer_size(core_if,
33082 + dwc_otg_module_params.
33083 + max_transfer_size);
33084 + }
33085 + if (dwc_otg_module_params.max_packet_count != -1) {
33086 + retval +=
33087 + dwc_otg_set_param_max_packet_count(core_if,
33088 + dwc_otg_module_params.
33089 + max_packet_count);
33090 + }
33091 + if (dwc_otg_module_params.host_channels != -1) {
33092 + retval +=
33093 + dwc_otg_set_param_host_channels(core_if,
33094 + dwc_otg_module_params.
33095 + host_channels);
33096 + }
33097 + if (dwc_otg_module_params.dev_endpoints != -1) {
33098 + retval +=
33099 + dwc_otg_set_param_dev_endpoints(core_if,
33100 + dwc_otg_module_params.
33101 + dev_endpoints);
33102 + }
33103 + if (dwc_otg_module_params.phy_type != -1) {
33104 + retval +=
33105 + dwc_otg_set_param_phy_type(core_if,
33106 + dwc_otg_module_params.phy_type);
33107 + }
33108 + if (dwc_otg_module_params.speed != -1) {
33109 + retval +=
33110 + dwc_otg_set_param_speed(core_if,
33111 + dwc_otg_module_params.speed);
33112 + }
33113 + if (dwc_otg_module_params.host_ls_low_power_phy_clk != -1) {
33114 + retval +=
33115 + dwc_otg_set_param_host_ls_low_power_phy_clk(core_if,
33116 + dwc_otg_module_params.
33117 + host_ls_low_power_phy_clk);
33118 + }
33119 + if (dwc_otg_module_params.phy_ulpi_ddr != -1) {
33120 + retval +=
33121 + dwc_otg_set_param_phy_ulpi_ddr(core_if,
33122 + dwc_otg_module_params.
33123 + phy_ulpi_ddr);
33124 + }
33125 + if (dwc_otg_module_params.phy_ulpi_ext_vbus != -1) {
33126 + retval +=
33127 + dwc_otg_set_param_phy_ulpi_ext_vbus(core_if,
33128 + dwc_otg_module_params.
33129 + phy_ulpi_ext_vbus);
33130 + }
33131 + if (dwc_otg_module_params.phy_utmi_width != -1) {
33132 + retval +=
33133 + dwc_otg_set_param_phy_utmi_width(core_if,
33134 + dwc_otg_module_params.
33135 + phy_utmi_width);
33136 + }
33137 + if (dwc_otg_module_params.ulpi_fs_ls != -1) {
33138 + retval +=
33139 + dwc_otg_set_param_ulpi_fs_ls(core_if,
33140 + dwc_otg_module_params.ulpi_fs_ls);
33141 + }
33142 + if (dwc_otg_module_params.ts_dline != -1) {
33143 + retval +=
33144 + dwc_otg_set_param_ts_dline(core_if,
33145 + dwc_otg_module_params.ts_dline);
33146 + }
33147 + if (dwc_otg_module_params.i2c_enable != -1) {
33148 + retval +=
33149 + dwc_otg_set_param_i2c_enable(core_if,
33150 + dwc_otg_module_params.
33151 + i2c_enable);
33152 + }
33153 + if (dwc_otg_module_params.en_multiple_tx_fifo != -1) {
33154 + retval +=
33155 + dwc_otg_set_param_en_multiple_tx_fifo(core_if,
33156 + dwc_otg_module_params.
33157 + en_multiple_tx_fifo);
33158 + }
33159 + for (i = 0; i < 15; i++) {
33160 + if (dwc_otg_module_params.dev_perio_tx_fifo_size[i] != -1) {
33161 + retval +=
33162 + dwc_otg_set_param_dev_perio_tx_fifo_size(core_if,
33163 + dwc_otg_module_params.
33164 + dev_perio_tx_fifo_size
33165 + [i], i);
33166 + }
33167 + }
33168 +
33169 + for (i = 0; i < 15; i++) {
33170 + if (dwc_otg_module_params.dev_tx_fifo_size[i] != -1) {
33171 + retval += dwc_otg_set_param_dev_tx_fifo_size(core_if,
33172 + dwc_otg_module_params.
33173 + dev_tx_fifo_size
33174 + [i], i);
33175 + }
33176 + }
33177 + if (dwc_otg_module_params.thr_ctl != -1) {
33178 + retval +=
33179 + dwc_otg_set_param_thr_ctl(core_if,
33180 + dwc_otg_module_params.thr_ctl);
33181 + }
33182 + if (dwc_otg_module_params.mpi_enable != -1) {
33183 + retval +=
33184 + dwc_otg_set_param_mpi_enable(core_if,
33185 + dwc_otg_module_params.
33186 + mpi_enable);
33187 + }
33188 + if (dwc_otg_module_params.pti_enable != -1) {
33189 + retval +=
33190 + dwc_otg_set_param_pti_enable(core_if,
33191 + dwc_otg_module_params.
33192 + pti_enable);
33193 + }
33194 + if (dwc_otg_module_params.lpm_enable != -1) {
33195 + retval +=
33196 + dwc_otg_set_param_lpm_enable(core_if,
33197 + dwc_otg_module_params.
33198 + lpm_enable);
33199 + }
33200 + if (dwc_otg_module_params.ic_usb_cap != -1) {
33201 + retval +=
33202 + dwc_otg_set_param_ic_usb_cap(core_if,
33203 + dwc_otg_module_params.
33204 + ic_usb_cap);
33205 + }
33206 + if (dwc_otg_module_params.tx_thr_length != -1) {
33207 + retval +=
33208 + dwc_otg_set_param_tx_thr_length(core_if,
33209 + dwc_otg_module_params.tx_thr_length);
33210 + }
33211 + if (dwc_otg_module_params.rx_thr_length != -1) {
33212 + retval +=
33213 + dwc_otg_set_param_rx_thr_length(core_if,
33214 + dwc_otg_module_params.
33215 + rx_thr_length);
33216 + }
33217 + if (dwc_otg_module_params.ahb_thr_ratio != -1) {
33218 + retval +=
33219 + dwc_otg_set_param_ahb_thr_ratio(core_if,
33220 + dwc_otg_module_params.ahb_thr_ratio);
33221 + }
33222 + if (dwc_otg_module_params.power_down != -1) {
33223 + retval +=
33224 + dwc_otg_set_param_power_down(core_if,
33225 + dwc_otg_module_params.power_down);
33226 + }
33227 + if (dwc_otg_module_params.reload_ctl != -1) {
33228 + retval +=
33229 + dwc_otg_set_param_reload_ctl(core_if,
33230 + dwc_otg_module_params.reload_ctl);
33231 + }
33232 +
33233 + if (dwc_otg_module_params.dev_out_nak != -1) {
33234 + retval +=
33235 + dwc_otg_set_param_dev_out_nak(core_if,
33236 + dwc_otg_module_params.dev_out_nak);
33237 + }
33238 +
33239 + if (dwc_otg_module_params.cont_on_bna != -1) {
33240 + retval +=
33241 + dwc_otg_set_param_cont_on_bna(core_if,
33242 + dwc_otg_module_params.cont_on_bna);
33243 + }
33244 +
33245 + if (dwc_otg_module_params.ahb_single != -1) {
33246 + retval +=
33247 + dwc_otg_set_param_ahb_single(core_if,
33248 + dwc_otg_module_params.ahb_single);
33249 + }
33250 +
33251 + if (dwc_otg_module_params.otg_ver != -1) {
33252 + retval +=
33253 + dwc_otg_set_param_otg_ver(core_if,
33254 + dwc_otg_module_params.otg_ver);
33255 + }
33256 + if (dwc_otg_module_params.adp_enable != -1) {
33257 + retval +=
33258 + dwc_otg_set_param_adp_enable(core_if,
33259 + dwc_otg_module_params.
33260 + adp_enable);
33261 + }
33262 + return retval;
33263 +}
33264 +
33265 +/**
33266 + * This function is the top level interrupt handler for the Common
33267 + * (Device and host modes) interrupts.
33268 + */
33269 +static irqreturn_t dwc_otg_common_irq(int irq, void *dev)
33270 +{
33271 + int32_t retval = IRQ_NONE;
33272 +
33273 + retval = dwc_otg_handle_common_intr(dev);
33274 + if (retval != 0) {
33275 + S3C2410X_CLEAR_EINTPEND();
33276 + }
33277 + return IRQ_RETVAL(retval);
33278 +}
33279 +
33280 +/**
33281 + * This function is called when a lm_device is unregistered with the
33282 + * dwc_otg_driver. This happens, for example, when the rmmod command is
33283 + * executed. The device may or may not be electrically present. If it is
33284 + * present, the driver stops device processing. Any resources used on behalf
33285 + * of this device are freed.
33286 + *
33287 + * @param _dev
33288 + */
33289 +#ifdef LM_INTERFACE
33290 +#define REM_RETVAL(n)
33291 +static void dwc_otg_driver_remove( struct lm_device *_dev )
33292 +{ dwc_otg_device_t *otg_dev = lm_get_drvdata(_dev);
33293 +#elif defined(PCI_INTERFACE)
33294 +#define REM_RETVAL(n)
33295 +static void dwc_otg_driver_remove( struct pci_dev *_dev )
33296 +{ dwc_otg_device_t *otg_dev = pci_get_drvdata(_dev);
33297 +#elif defined(PLATFORM_INTERFACE)
33298 +#define REM_RETVAL(n) n
33299 +static int dwc_otg_driver_remove( struct platform_device *_dev )
33300 +{ dwc_otg_device_t *otg_dev = platform_get_drvdata(_dev);
33301 +#endif
33302 +
33303 + DWC_DEBUGPL(DBG_ANY, "%s(%p) otg_dev %p\n", __func__, _dev, otg_dev);
33304 +
33305 + if (!otg_dev) {
33306 + /* Memory allocation for the dwc_otg_device failed. */
33307 + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev NULL!\n", __func__);
33308 + return REM_RETVAL(-ENOMEM);
33309 + }
33310 +#ifndef DWC_DEVICE_ONLY
33311 + if (otg_dev->hcd) {
33312 + hcd_remove(_dev);
33313 + } else {
33314 + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev->hcd NULL!\n", __func__);
33315 + return REM_RETVAL(-EINVAL);
33316 + }
33317 +#endif
33318 +
33319 +#ifndef DWC_HOST_ONLY
33320 + if (otg_dev->pcd) {
33321 + pcd_remove(_dev);
33322 + } else {
33323 + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev->pcd NULL!\n", __func__);
33324 + return REM_RETVAL(-EINVAL);
33325 + }
33326 +#endif
33327 + /*
33328 + * Free the IRQ
33329 + */
33330 + if (otg_dev->common_irq_installed) {
33331 +#ifdef PLATFORM_INTERFACE
33332 + free_irq(platform_get_irq(_dev, 0), otg_dev);
33333 +#else
33334 + free_irq(_dev->irq, otg_dev);
33335 +#endif
33336 + } else {
33337 + DWC_DEBUGPL(DBG_ANY, "%s: There is no installed irq!\n", __func__);
33338 + return REM_RETVAL(-ENXIO);
33339 + }
33340 +
33341 + if (otg_dev->core_if) {
33342 + dwc_otg_cil_remove(otg_dev->core_if);
33343 + } else {
33344 + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev->core_if NULL!\n", __func__);
33345 + return REM_RETVAL(-ENXIO);
33346 + }
33347 +
33348 + /*
33349 + * Remove the device attributes
33350 + */
33351 + dwc_otg_attr_remove(_dev);
33352 +
33353 + /*
33354 + * Return the memory.
33355 + */
33356 + if (otg_dev->os_dep.base) {
33357 + iounmap(otg_dev->os_dep.base);
33358 + }
33359 + DWC_FREE(otg_dev);
33360 +
33361 + /*
33362 + * Clear the drvdata pointer.
33363 + */
33364 +#ifdef LM_INTERFACE
33365 + lm_set_drvdata(_dev, 0);
33366 +#elif defined(PCI_INTERFACE)
33367 + release_mem_region(otg_dev->os_dep.rsrc_start,
33368 + otg_dev->os_dep.rsrc_len);
33369 + pci_set_drvdata(_dev, 0);
33370 +#elif defined(PLATFORM_INTERFACE)
33371 + platform_set_drvdata(_dev, 0);
33372 +#endif
33373 + return REM_RETVAL(0);
33374 +}
33375 +
33376 +/**
33377 + * This function is called when an lm_device is bound to a
33378 + * dwc_otg_driver. It creates the driver components required to
33379 + * control the device (CIL, HCD, and PCD) and it initializes the
33380 + * device. The driver components are stored in a dwc_otg_device
33381 + * structure. A reference to the dwc_otg_device is saved in the
33382 + * lm_device. This allows the driver to access the dwc_otg_device
33383 + * structure on subsequent calls to driver methods for this device.
33384 + *
33385 + * @param _dev Bus device
33386 + */
33387 +static int dwc_otg_driver_probe(
33388 +#ifdef LM_INTERFACE
33389 + struct lm_device *_dev
33390 +#elif defined(PCI_INTERFACE)
33391 + struct pci_dev *_dev,
33392 + const struct pci_device_id *id
33393 +#elif defined(PLATFORM_INTERFACE)
33394 + struct platform_device *_dev
33395 +#endif
33396 + )
33397 +{
33398 + int retval = 0;
33399 + dwc_otg_device_t *dwc_otg_device;
33400 + int devirq;
33401 +
33402 + dev_dbg(&_dev->dev, "dwc_otg_driver_probe(%p)\n", _dev);
33403 +#ifdef LM_INTERFACE
33404 + dev_dbg(&_dev->dev, "start=0x%08x\n", (unsigned)_dev->resource.start);
33405 +#elif defined(PCI_INTERFACE)
33406 + if (!id) {
33407 + DWC_ERROR("Invalid pci_device_id %p", id);
33408 + return -EINVAL;
33409 + }
33410 +
33411 + if (!_dev || (pci_enable_device(_dev) < 0)) {
33412 + DWC_ERROR("Invalid pci_device %p", _dev);
33413 + return -ENODEV;
33414 + }
33415 + dev_dbg(&_dev->dev, "start=0x%08x\n", (unsigned)pci_resource_start(_dev,0));
33416 + /* other stuff needed as well? */
33417 +
33418 +#elif defined(PLATFORM_INTERFACE)
33419 + dev_dbg(&_dev->dev, "start=0x%08x (len 0x%x)\n",
33420 + (unsigned)_dev->resource->start,
33421 + (unsigned)(_dev->resource->end - _dev->resource->start));
33422 +#endif
33423 +
33424 + dwc_otg_device = DWC_ALLOC(sizeof(dwc_otg_device_t));
33425 +
33426 + if (!dwc_otg_device) {
33427 + dev_err(&_dev->dev, "kmalloc of dwc_otg_device failed\n");
33428 + return -ENOMEM;
33429 + }
33430 +
33431 + memset(dwc_otg_device, 0, sizeof(*dwc_otg_device));
33432 + dwc_otg_device->os_dep.reg_offset = 0xFFFFFFFF;
33433 + dwc_otg_device->os_dep.platformdev = _dev;
33434 +
33435 + /*
33436 + * Map the DWC_otg Core memory into virtual address space.
33437 + */
33438 +#ifdef LM_INTERFACE
33439 + dwc_otg_device->os_dep.base = ioremap(_dev->resource.start, SZ_256K);
33440 +
33441 + if (!dwc_otg_device->os_dep.base) {
33442 + dev_err(&_dev->dev, "ioremap() failed\n");
33443 + DWC_FREE(dwc_otg_device);
33444 + return -ENOMEM;
33445 + }
33446 + dev_dbg(&_dev->dev, "base=0x%08x\n",
33447 + (unsigned)dwc_otg_device->os_dep.base);
33448 +#elif defined(PCI_INTERFACE)
33449 + _dev->current_state = PCI_D0;
33450 + _dev->dev.power.power_state = PMSG_ON;
33451 +
33452 + if (!_dev->irq) {
33453 + DWC_ERROR("Found HC with no IRQ. Check BIOS/PCI %s setup!",
33454 + pci_name(_dev));
33455 + iounmap(dwc_otg_device->os_dep.base);
33456 + DWC_FREE(dwc_otg_device);
33457 + return -ENODEV;
33458 + }
33459 +
33460 + dwc_otg_device->os_dep.rsrc_start = pci_resource_start(_dev, 0);
33461 + dwc_otg_device->os_dep.rsrc_len = pci_resource_len(_dev, 0);
33462 + DWC_DEBUGPL(DBG_ANY, "PCI resource: start=%08x, len=%08x\n",
33463 + (unsigned)dwc_otg_device->os_dep.rsrc_start,
33464 + (unsigned)dwc_otg_device->os_dep.rsrc_len);
33465 + if (!request_mem_region
33466 + (dwc_otg_device->os_dep.rsrc_start, dwc_otg_device->os_dep.rsrc_len,
33467 + "dwc_otg")) {
33468 + dev_dbg(&_dev->dev, "error requesting memory\n");
33469 + iounmap(dwc_otg_device->os_dep.base);
33470 + DWC_FREE(dwc_otg_device);
33471 + return -EFAULT;
33472 + }
33473 +
33474 + dwc_otg_device->os_dep.base =
33475 + ioremap_nocache(dwc_otg_device->os_dep.rsrc_start,
33476 + dwc_otg_device->os_dep.rsrc_len);
33477 + if (dwc_otg_device->os_dep.base == NULL) {
33478 + dev_dbg(&_dev->dev, "error mapping memory\n");
33479 + release_mem_region(dwc_otg_device->os_dep.rsrc_start,
33480 + dwc_otg_device->os_dep.rsrc_len);
33481 + iounmap(dwc_otg_device->os_dep.base);
33482 + DWC_FREE(dwc_otg_device);
33483 + return -EFAULT;
33484 + }
33485 + dev_dbg(&_dev->dev, "base=0x%p (before adjust) \n",
33486 + dwc_otg_device->os_dep.base);
33487 + dwc_otg_device->os_dep.base = (char *)dwc_otg_device->os_dep.base;
33488 + dev_dbg(&_dev->dev, "base=0x%p (after adjust) \n",
33489 + dwc_otg_device->os_dep.base);
33490 + dev_dbg(&_dev->dev, "%s: mapped PA 0x%x to VA 0x%p\n", __func__,
33491 + (unsigned)dwc_otg_device->os_dep.rsrc_start,
33492 + dwc_otg_device->os_dep.base);
33493 +
33494 + pci_set_master(_dev);
33495 + pci_set_drvdata(_dev, dwc_otg_device);
33496 +#elif defined(PLATFORM_INTERFACE)
33497 + DWC_DEBUGPL(DBG_ANY,"Platform resource: start=%08x, len=%08x\n",
33498 + _dev->resource->start,
33499 + _dev->resource->end - _dev->resource->start + 1);
33500 +#if 1
33501 + if (!request_mem_region(_dev->resource[0].start,
33502 + _dev->resource[0].end - _dev->resource[0].start + 1,
33503 + "dwc_otg")) {
33504 + dev_dbg(&_dev->dev, "error reserving mapped memory\n");
33505 + retval = -EFAULT;
33506 + goto fail;
33507 + }
33508 +
33509 + dwc_otg_device->os_dep.base = ioremap_nocache(_dev->resource[0].start,
33510 + _dev->resource[0].end -
33511 + _dev->resource[0].start+1);
33512 + if (fiq_enable)
33513 + {
33514 + if (!request_mem_region(_dev->resource[1].start,
33515 + _dev->resource[1].end - _dev->resource[1].start + 1,
33516 + "dwc_otg")) {
33517 + dev_dbg(&_dev->dev, "error reserving mapped memory\n");
33518 + retval = -EFAULT;
33519 + goto fail;
33520 + }
33521 +
33522 + dwc_otg_device->os_dep.mphi_base = ioremap_nocache(_dev->resource[1].start,
33523 + _dev->resource[1].end -
33524 + _dev->resource[1].start + 1);
33525 + }
33526 +
33527 +#else
33528 + {
33529 + struct map_desc desc = {
33530 + .virtual = IO_ADDRESS((unsigned)_dev->resource->start),
33531 + .pfn = __phys_to_pfn((unsigned)_dev->resource->start),
33532 + .length = SZ_128K,
33533 + .type = MT_DEVICE
33534 + };
33535 + iotable_init(&desc, 1);
33536 + dwc_otg_device->os_dep.base = (void *)desc.virtual;
33537 + }
33538 +#endif
33539 + if (!dwc_otg_device->os_dep.base) {
33540 + dev_err(&_dev->dev, "ioremap() failed\n");
33541 + retval = -ENOMEM;
33542 + goto fail;
33543 + }
33544 + dev_dbg(&_dev->dev, "base=0x%08x\n",
33545 + (unsigned)dwc_otg_device->os_dep.base);
33546 +#endif
33547 +
33548 + /*
33549 + * Initialize driver data to point to the global DWC_otg
33550 + * Device structure.
33551 + */
33552 +#ifdef LM_INTERFACE
33553 + lm_set_drvdata(_dev, dwc_otg_device);
33554 +#elif defined(PLATFORM_INTERFACE)
33555 + platform_set_drvdata(_dev, dwc_otg_device);
33556 +#endif
33557 + dev_dbg(&_dev->dev, "dwc_otg_device=0x%p\n", dwc_otg_device);
33558 +
33559 + dwc_otg_device->core_if = dwc_otg_cil_init(dwc_otg_device->os_dep.base);
33560 + DWC_DEBUGPL(DBG_HCDV, "probe of device %p given core_if %p\n",
33561 + dwc_otg_device, dwc_otg_device->core_if);//GRAYG
33562 +
33563 + if (!dwc_otg_device->core_if) {
33564 + dev_err(&_dev->dev, "CIL initialization failed!\n");
33565 + retval = -ENOMEM;
33566 + goto fail;
33567 + }
33568 +
33569 + dev_dbg(&_dev->dev, "Calling get_gsnpsid\n");
33570 + /*
33571 + * Attempt to ensure this device is really a DWC_otg Controller.
33572 + * Read and verify the SNPSID register contents. The value should be
33573 + * 0x45F42XXX or 0x45F42XXX, which corresponds to either "OT2" or "OTG3",
33574 + * as in "OTG version 2.XX" or "OTG version 3.XX".
33575 + */
33576 +
33577 + if (((dwc_otg_get_gsnpsid(dwc_otg_device->core_if) & 0xFFFFF000) != 0x4F542000) &&
33578 + ((dwc_otg_get_gsnpsid(dwc_otg_device->core_if) & 0xFFFFF000) != 0x4F543000)) {
33579 + dev_err(&_dev->dev, "Bad value for SNPSID: 0x%08x\n",
33580 + dwc_otg_get_gsnpsid(dwc_otg_device->core_if));
33581 + retval = -EINVAL;
33582 + goto fail;
33583 + }
33584 +
33585 + /*
33586 + * Validate parameter values.
33587 + */
33588 + dev_dbg(&_dev->dev, "Calling set_parameters\n");
33589 + if (set_parameters(dwc_otg_device->core_if)) {
33590 + retval = -EINVAL;
33591 + goto fail;
33592 + }
33593 +
33594 + /*
33595 + * Create Device Attributes in sysfs
33596 + */
33597 + dev_dbg(&_dev->dev, "Calling attr_create\n");
33598 + dwc_otg_attr_create(_dev);
33599 +
33600 + /*
33601 + * Disable the global interrupt until all the interrupt
33602 + * handlers are installed.
33603 + */
33604 + dev_dbg(&_dev->dev, "Calling disable_global_interrupts\n");
33605 + dwc_otg_disable_global_interrupts(dwc_otg_device->core_if);
33606 +
33607 + /*
33608 + * Install the interrupt handler for the common interrupts before
33609 + * enabling common interrupts in core_init below.
33610 + */
33611 +
33612 +#if defined(PLATFORM_INTERFACE)
33613 + devirq = platform_get_irq(_dev, fiq_enable ? 0 : 1);
33614 +#else
33615 + devirq = _dev->irq;
33616 +#endif
33617 + DWC_DEBUGPL(DBG_CIL, "registering (common) handler for irq%d\n",
33618 + devirq);
33619 + dev_dbg(&_dev->dev, "Calling request_irq(%d)\n", devirq);
33620 + retval = request_irq(devirq, dwc_otg_common_irq,
33621 + IRQF_SHARED,
33622 + "dwc_otg", dwc_otg_device);
33623 + if (retval) {
33624 + DWC_ERROR("request of irq%d failed\n", devirq);
33625 + retval = -EBUSY;
33626 + goto fail;
33627 + } else {
33628 + dwc_otg_device->common_irq_installed = 1;
33629 + }
33630 +
33631 +#ifndef IRQF_TRIGGER_LOW
33632 +#if defined(LM_INTERFACE) || defined(PLATFORM_INTERFACE)
33633 + dev_dbg(&_dev->dev, "Calling set_irq_type\n");
33634 + set_irq_type(devirq,
33635 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30))
33636 + IRQT_LOW
33637 +#else
33638 + IRQ_TYPE_LEVEL_LOW
33639 +#endif
33640 + );
33641 +#endif
33642 +#endif /*IRQF_TRIGGER_LOW*/
33643 +
33644 + /*
33645 + * Initialize the DWC_otg core.
33646 + */
33647 + dev_dbg(&_dev->dev, "Calling dwc_otg_core_init\n");
33648 + dwc_otg_core_init(dwc_otg_device->core_if);
33649 +
33650 +#ifndef DWC_HOST_ONLY
33651 + /*
33652 + * Initialize the PCD
33653 + */
33654 + dev_dbg(&_dev->dev, "Calling pcd_init\n");
33655 + retval = pcd_init(_dev);
33656 + if (retval != 0) {
33657 + DWC_ERROR("pcd_init failed\n");
33658 + dwc_otg_device->pcd = NULL;
33659 + goto fail;
33660 + }
33661 +#endif
33662 +#ifndef DWC_DEVICE_ONLY
33663 + /*
33664 + * Initialize the HCD
33665 + */
33666 + dev_dbg(&_dev->dev, "Calling hcd_init\n");
33667 + retval = hcd_init(_dev);
33668 + if (retval != 0) {
33669 + DWC_ERROR("hcd_init failed\n");
33670 + dwc_otg_device->hcd = NULL;
33671 + goto fail;
33672 + }
33673 +#endif
33674 + /* Recover from drvdata having been overwritten by hcd_init() */
33675 +#ifdef LM_INTERFACE
33676 + lm_set_drvdata(_dev, dwc_otg_device);
33677 +#elif defined(PLATFORM_INTERFACE)
33678 + platform_set_drvdata(_dev, dwc_otg_device);
33679 +#elif defined(PCI_INTERFACE)
33680 + pci_set_drvdata(_dev, dwc_otg_device);
33681 + dwc_otg_device->os_dep.pcidev = _dev;
33682 +#endif
33683 +
33684 + /*
33685 + * Enable the global interrupt after all the interrupt
33686 + * handlers are installed if there is no ADP support else
33687 + * perform initial actions required for Internal ADP logic.
33688 + */
33689 + if (!dwc_otg_get_param_adp_enable(dwc_otg_device->core_if)) {
33690 + dev_dbg(&_dev->dev, "Calling enable_global_interrupts\n");
33691 + dwc_otg_enable_global_interrupts(dwc_otg_device->core_if);
33692 + dev_dbg(&_dev->dev, "Done\n");
33693 + } else
33694 + dwc_otg_adp_start(dwc_otg_device->core_if,
33695 + dwc_otg_is_host_mode(dwc_otg_device->core_if));
33696 +
33697 + return 0;
33698 +
33699 +fail:
33700 + dwc_otg_driver_remove(_dev);
33701 + return retval;
33702 +}
33703 +
33704 +/**
33705 + * This structure defines the methods to be called by a bus driver
33706 + * during the lifecycle of a device on that bus. Both drivers and
33707 + * devices are registered with a bus driver. The bus driver matches
33708 + * devices to drivers based on information in the device and driver
33709 + * structures.
33710 + *
33711 + * The probe function is called when the bus driver matches a device
33712 + * to this driver. The remove function is called when a device is
33713 + * unregistered with the bus driver.
33714 + */
33715 +#ifdef LM_INTERFACE
33716 +static struct lm_driver dwc_otg_driver = {
33717 + .drv = {.name = (char *)dwc_driver_name,},
33718 + .probe = dwc_otg_driver_probe,
33719 + .remove = dwc_otg_driver_remove,
33720 + // 'suspend' and 'resume' absent
33721 +};
33722 +#elif defined(PCI_INTERFACE)
33723 +static const struct pci_device_id pci_ids[] = { {
33724 + PCI_DEVICE(0x16c3, 0xabcd),
33725 + .driver_data =
33726 + (unsigned long)0xdeadbeef,
33727 + }, { /* end: all zeroes */ }
33728 +};
33729 +
33730 +MODULE_DEVICE_TABLE(pci, pci_ids);
33731 +
33732 +/* pci driver glue; this is a "new style" PCI driver module */
33733 +static struct pci_driver dwc_otg_driver = {
33734 + .name = "dwc_otg",
33735 + .id_table = pci_ids,
33736 +
33737 + .probe = dwc_otg_driver_probe,
33738 + .remove = dwc_otg_driver_remove,
33739 +
33740 + .driver = {
33741 + .name = (char *)dwc_driver_name,
33742 + },
33743 +};
33744 +#elif defined(PLATFORM_INTERFACE)
33745 +static struct platform_device_id platform_ids[] = {
33746 + {
33747 + .name = "bcm2708_usb",
33748 + .driver_data = (kernel_ulong_t) 0xdeadbeef,
33749 + },
33750 + { /* end: all zeroes */ }
33751 +};
33752 +MODULE_DEVICE_TABLE(platform, platform_ids);
33753 +
33754 +static const struct of_device_id dwc_otg_of_match_table[] = {
33755 + { .compatible = "brcm,bcm2708-usb", },
33756 + {},
33757 +};
33758 +MODULE_DEVICE_TABLE(of, dwc_otg_of_match_table);
33759 +
33760 +static struct platform_driver dwc_otg_driver = {
33761 + .driver = {
33762 + .name = (char *)dwc_driver_name,
33763 + .of_match_table = dwc_otg_of_match_table,
33764 + },
33765 + .id_table = platform_ids,
33766 +
33767 + .probe = dwc_otg_driver_probe,
33768 + .remove = dwc_otg_driver_remove,
33769 + // no 'shutdown', 'suspend', 'resume', 'suspend_late' or 'resume_early'
33770 +};
33771 +#endif
33772 +
33773 +/**
33774 + * This function is called when the dwc_otg_driver is installed with the
33775 + * insmod command. It registers the dwc_otg_driver structure with the
33776 + * appropriate bus driver. This will cause the dwc_otg_driver_probe function
33777 + * to be called. In addition, the bus driver will automatically expose
33778 + * attributes defined for the device and driver in the special sysfs file
33779 + * system.
33780 + *
33781 + * @return
33782 + */
33783 +static int __init dwc_otg_driver_init(void)
33784 +{
33785 + int retval = 0;
33786 + int error;
33787 + struct device_driver *drv;
33788 +
33789 + if(fiq_fsm_enable && !fiq_enable) {
33790 + printk(KERN_WARNING "dwc_otg: fiq_fsm_enable was set without fiq_enable! Correcting.\n");
33791 + fiq_enable = 1;
33792 + }
33793 +
33794 + printk(KERN_INFO "%s: version %s (%s bus)\n", dwc_driver_name,
33795 + DWC_DRIVER_VERSION,
33796 +#ifdef LM_INTERFACE
33797 + "logicmodule");
33798 + retval = lm_driver_register(&dwc_otg_driver);
33799 + drv = &dwc_otg_driver.drv;
33800 +#elif defined(PCI_INTERFACE)
33801 + "pci");
33802 + retval = pci_register_driver(&dwc_otg_driver);
33803 + drv = &dwc_otg_driver.driver;
33804 +#elif defined(PLATFORM_INTERFACE)
33805 + "platform");
33806 + retval = platform_driver_register(&dwc_otg_driver);
33807 + drv = &dwc_otg_driver.driver;
33808 +#endif
33809 + if (retval < 0) {
33810 + printk(KERN_ERR "%s retval=%d\n", __func__, retval);
33811 + return retval;
33812 + }
33813 + printk(KERN_DEBUG "dwc_otg: FIQ %s\n", fiq_enable ? "enabled":"disabled");
33814 + printk(KERN_DEBUG "dwc_otg: NAK holdoff %s\n", nak_holdoff ? "enabled":"disabled");
33815 + printk(KERN_DEBUG "dwc_otg: FIQ split-transaction FSM %s\n", fiq_fsm_enable ? "enabled":"disabled");
33816 +
33817 + error = driver_create_file(drv, &driver_attr_version);
33818 +#ifdef DEBUG
33819 + error = driver_create_file(drv, &driver_attr_debuglevel);
33820 +#endif
33821 + return retval;
33822 +}
33823 +
33824 +module_init(dwc_otg_driver_init);
33825 +
33826 +/**
33827 + * This function is called when the driver is removed from the kernel
33828 + * with the rmmod command. The driver unregisters itself with its bus
33829 + * driver.
33830 + *
33831 + */
33832 +static void __exit dwc_otg_driver_cleanup(void)
33833 +{
33834 + printk(KERN_DEBUG "dwc_otg_driver_cleanup()\n");
33835 +
33836 +#ifdef LM_INTERFACE
33837 + driver_remove_file(&dwc_otg_driver.drv, &driver_attr_debuglevel);
33838 + driver_remove_file(&dwc_otg_driver.drv, &driver_attr_version);
33839 + lm_driver_unregister(&dwc_otg_driver);
33840 +#elif defined(PCI_INTERFACE)
33841 + driver_remove_file(&dwc_otg_driver.driver, &driver_attr_debuglevel);
33842 + driver_remove_file(&dwc_otg_driver.driver, &driver_attr_version);
33843 + pci_unregister_driver(&dwc_otg_driver);
33844 +#elif defined(PLATFORM_INTERFACE)
33845 + driver_remove_file(&dwc_otg_driver.driver, &driver_attr_debuglevel);
33846 + driver_remove_file(&dwc_otg_driver.driver, &driver_attr_version);
33847 + platform_driver_unregister(&dwc_otg_driver);
33848 +#endif
33849 +
33850 + printk(KERN_INFO "%s module removed\n", dwc_driver_name);
33851 +}
33852 +
33853 +module_exit(dwc_otg_driver_cleanup);
33854 +
33855 +MODULE_DESCRIPTION(DWC_DRIVER_DESC);
33856 +MODULE_AUTHOR("Synopsys Inc.");
33857 +MODULE_LICENSE("GPL");
33858 +
33859 +module_param_named(otg_cap, dwc_otg_module_params.otg_cap, int, 0444);
33860 +MODULE_PARM_DESC(otg_cap, "OTG Capabilities 0=HNP&SRP 1=SRP Only 2=None");
33861 +module_param_named(opt, dwc_otg_module_params.opt, int, 0444);
33862 +MODULE_PARM_DESC(opt, "OPT Mode");
33863 +module_param_named(dma_enable, dwc_otg_module_params.dma_enable, int, 0444);
33864 +MODULE_PARM_DESC(dma_enable, "DMA Mode 0=Slave 1=DMA enabled");
33865 +
33866 +module_param_named(dma_desc_enable, dwc_otg_module_params.dma_desc_enable, int,
33867 + 0444);
33868 +MODULE_PARM_DESC(dma_desc_enable,
33869 + "DMA Desc Mode 0=Address DMA 1=DMA Descriptor enabled");
33870 +
33871 +module_param_named(dma_burst_size, dwc_otg_module_params.dma_burst_size, int,
33872 + 0444);
33873 +MODULE_PARM_DESC(dma_burst_size,
33874 + "DMA Burst Size 1, 4, 8, 16, 32, 64, 128, 256");
33875 +module_param_named(speed, dwc_otg_module_params.speed, int, 0444);
33876 +MODULE_PARM_DESC(speed, "Speed 0=High Speed 1=Full Speed");
33877 +module_param_named(host_support_fs_ls_low_power,
33878 + dwc_otg_module_params.host_support_fs_ls_low_power, int,
33879 + 0444);
33880 +MODULE_PARM_DESC(host_support_fs_ls_low_power,
33881 + "Support Low Power w/FS or LS 0=Support 1=Don't Support");
33882 +module_param_named(host_ls_low_power_phy_clk,
33883 + dwc_otg_module_params.host_ls_low_power_phy_clk, int, 0444);
33884 +MODULE_PARM_DESC(host_ls_low_power_phy_clk,
33885 + "Low Speed Low Power Clock 0=48Mhz 1=6Mhz");
33886 +module_param_named(enable_dynamic_fifo,
33887 + dwc_otg_module_params.enable_dynamic_fifo, int, 0444);
33888 +MODULE_PARM_DESC(enable_dynamic_fifo, "0=cC Setting 1=Allow Dynamic Sizing");
33889 +module_param_named(data_fifo_size, dwc_otg_module_params.data_fifo_size, int,
33890 + 0444);
33891 +MODULE_PARM_DESC(data_fifo_size,
33892 + "Total number of words in the data FIFO memory 32-32768");
33893 +module_param_named(dev_rx_fifo_size, dwc_otg_module_params.dev_rx_fifo_size,
33894 + int, 0444);
33895 +MODULE_PARM_DESC(dev_rx_fifo_size, "Number of words in the Rx FIFO 16-32768");
33896 +module_param_named(dev_nperio_tx_fifo_size,
33897 + dwc_otg_module_params.dev_nperio_tx_fifo_size, int, 0444);
33898 +MODULE_PARM_DESC(dev_nperio_tx_fifo_size,
33899 + "Number of words in the non-periodic Tx FIFO 16-32768");
33900 +module_param_named(dev_perio_tx_fifo_size_1,
33901 + dwc_otg_module_params.dev_perio_tx_fifo_size[0], int, 0444);
33902 +MODULE_PARM_DESC(dev_perio_tx_fifo_size_1,
33903 + "Number of words in the periodic Tx FIFO 4-768");
33904 +module_param_named(dev_perio_tx_fifo_size_2,
33905 + dwc_otg_module_params.dev_perio_tx_fifo_size[1], int, 0444);
33906 +MODULE_PARM_DESC(dev_perio_tx_fifo_size_2,
33907 + "Number of words in the periodic Tx FIFO 4-768");
33908 +module_param_named(dev_perio_tx_fifo_size_3,
33909 + dwc_otg_module_params.dev_perio_tx_fifo_size[2], int, 0444);
33910 +MODULE_PARM_DESC(dev_perio_tx_fifo_size_3,
33911 + "Number of words in the periodic Tx FIFO 4-768");
33912 +module_param_named(dev_perio_tx_fifo_size_4,
33913 + dwc_otg_module_params.dev_perio_tx_fifo_size[3], int, 0444);
33914 +MODULE_PARM_DESC(dev_perio_tx_fifo_size_4,
33915 + "Number of words in the periodic Tx FIFO 4-768");
33916 +module_param_named(dev_perio_tx_fifo_size_5,
33917 + dwc_otg_module_params.dev_perio_tx_fifo_size[4], int, 0444);
33918 +MODULE_PARM_DESC(dev_perio_tx_fifo_size_5,
33919 + "Number of words in the periodic Tx FIFO 4-768");
33920 +module_param_named(dev_perio_tx_fifo_size_6,
33921 + dwc_otg_module_params.dev_perio_tx_fifo_size[5], int, 0444);
33922 +MODULE_PARM_DESC(dev_perio_tx_fifo_size_6,
33923 + "Number of words in the periodic Tx FIFO 4-768");
33924 +module_param_named(dev_perio_tx_fifo_size_7,
33925 + dwc_otg_module_params.dev_perio_tx_fifo_size[6], int, 0444);
33926 +MODULE_PARM_DESC(dev_perio_tx_fifo_size_7,
33927 + "Number of words in the periodic Tx FIFO 4-768");
33928 +module_param_named(dev_perio_tx_fifo_size_8,
33929 + dwc_otg_module_params.dev_perio_tx_fifo_size[7], int, 0444);
33930 +MODULE_PARM_DESC(dev_perio_tx_fifo_size_8,
33931 + "Number of words in the periodic Tx FIFO 4-768");
33932 +module_param_named(dev_perio_tx_fifo_size_9,
33933 + dwc_otg_module_params.dev_perio_tx_fifo_size[8], int, 0444);
33934 +MODULE_PARM_DESC(dev_perio_tx_fifo_size_9,
33935 + "Number of words in the periodic Tx FIFO 4-768");
33936 +module_param_named(dev_perio_tx_fifo_size_10,
33937 + dwc_otg_module_params.dev_perio_tx_fifo_size[9], int, 0444);
33938 +MODULE_PARM_DESC(dev_perio_tx_fifo_size_10,
33939 + "Number of words in the periodic Tx FIFO 4-768");
33940 +module_param_named(dev_perio_tx_fifo_size_11,
33941 + dwc_otg_module_params.dev_perio_tx_fifo_size[10], int, 0444);
33942 +MODULE_PARM_DESC(dev_perio_tx_fifo_size_11,
33943 + "Number of words in the periodic Tx FIFO 4-768");
33944 +module_param_named(dev_perio_tx_fifo_size_12,
33945 + dwc_otg_module_params.dev_perio_tx_fifo_size[11], int, 0444);
33946 +MODULE_PARM_DESC(dev_perio_tx_fifo_size_12,
33947 + "Number of words in the periodic Tx FIFO 4-768");
33948 +module_param_named(dev_perio_tx_fifo_size_13,
33949 + dwc_otg_module_params.dev_perio_tx_fifo_size[12], int, 0444);
33950 +MODULE_PARM_DESC(dev_perio_tx_fifo_size_13,
33951 + "Number of words in the periodic Tx FIFO 4-768");
33952 +module_param_named(dev_perio_tx_fifo_size_14,
33953 + dwc_otg_module_params.dev_perio_tx_fifo_size[13], int, 0444);
33954 +MODULE_PARM_DESC(dev_perio_tx_fifo_size_14,
33955 + "Number of words in the periodic Tx FIFO 4-768");
33956 +module_param_named(dev_perio_tx_fifo_size_15,
33957 + dwc_otg_module_params.dev_perio_tx_fifo_size[14], int, 0444);
33958 +MODULE_PARM_DESC(dev_perio_tx_fifo_size_15,
33959 + "Number of words in the periodic Tx FIFO 4-768");
33960 +module_param_named(host_rx_fifo_size, dwc_otg_module_params.host_rx_fifo_size,
33961 + int, 0444);
33962 +MODULE_PARM_DESC(host_rx_fifo_size, "Number of words in the Rx FIFO 16-32768");
33963 +module_param_named(host_nperio_tx_fifo_size,
33964 + dwc_otg_module_params.host_nperio_tx_fifo_size, int, 0444);
33965 +MODULE_PARM_DESC(host_nperio_tx_fifo_size,
33966 + "Number of words in the non-periodic Tx FIFO 16-32768");
33967 +module_param_named(host_perio_tx_fifo_size,
33968 + dwc_otg_module_params.host_perio_tx_fifo_size, int, 0444);
33969 +MODULE_PARM_DESC(host_perio_tx_fifo_size,
33970 + "Number of words in the host periodic Tx FIFO 16-32768");
33971 +module_param_named(max_transfer_size, dwc_otg_module_params.max_transfer_size,
33972 + int, 0444);
33973 +/** @todo Set the max to 512K, modify checks */
33974 +MODULE_PARM_DESC(max_transfer_size,
33975 + "The maximum transfer size supported in bytes 2047-65535");
33976 +module_param_named(max_packet_count, dwc_otg_module_params.max_packet_count,
33977 + int, 0444);
33978 +MODULE_PARM_DESC(max_packet_count,
33979 + "The maximum number of packets in a transfer 15-511");
33980 +module_param_named(host_channels, dwc_otg_module_params.host_channels, int,
33981 + 0444);
33982 +MODULE_PARM_DESC(host_channels,
33983 + "The number of host channel registers to use 1-16");
33984 +module_param_named(dev_endpoints, dwc_otg_module_params.dev_endpoints, int,
33985 + 0444);
33986 +MODULE_PARM_DESC(dev_endpoints,
33987 + "The number of endpoints in addition to EP0 available for device mode 1-15");
33988 +module_param_named(phy_type, dwc_otg_module_params.phy_type, int, 0444);
33989 +MODULE_PARM_DESC(phy_type, "0=Reserved 1=UTMI+ 2=ULPI");
33990 +module_param_named(phy_utmi_width, dwc_otg_module_params.phy_utmi_width, int,
33991 + 0444);
33992 +MODULE_PARM_DESC(phy_utmi_width, "Specifies the UTMI+ Data Width 8 or 16 bits");
33993 +module_param_named(phy_ulpi_ddr, dwc_otg_module_params.phy_ulpi_ddr, int, 0444);
33994 +MODULE_PARM_DESC(phy_ulpi_ddr,
33995 + "ULPI at double or single data rate 0=Single 1=Double");
33996 +module_param_named(phy_ulpi_ext_vbus, dwc_otg_module_params.phy_ulpi_ext_vbus,
33997 + int, 0444);
33998 +MODULE_PARM_DESC(phy_ulpi_ext_vbus,
33999 + "ULPI PHY using internal or external vbus 0=Internal");
34000 +module_param_named(i2c_enable, dwc_otg_module_params.i2c_enable, int, 0444);
34001 +MODULE_PARM_DESC(i2c_enable, "FS PHY Interface");
34002 +module_param_named(ulpi_fs_ls, dwc_otg_module_params.ulpi_fs_ls, int, 0444);
34003 +MODULE_PARM_DESC(ulpi_fs_ls, "ULPI PHY FS/LS mode only");
34004 +module_param_named(ts_dline, dwc_otg_module_params.ts_dline, int, 0444);
34005 +MODULE_PARM_DESC(ts_dline, "Term select Dline pulsing for all PHYs");
34006 +module_param_named(debug, g_dbg_lvl, int, 0444);
34007 +MODULE_PARM_DESC(debug, "");
34008 +
34009 +module_param_named(en_multiple_tx_fifo,
34010 + dwc_otg_module_params.en_multiple_tx_fifo, int, 0444);
34011 +MODULE_PARM_DESC(en_multiple_tx_fifo,
34012 + "Dedicated Non Periodic Tx FIFOs 0=disabled 1=enabled");
34013 +module_param_named(dev_tx_fifo_size_1,
34014 + dwc_otg_module_params.dev_tx_fifo_size[0], int, 0444);
34015 +MODULE_PARM_DESC(dev_tx_fifo_size_1, "Number of words in the Tx FIFO 4-768");
34016 +module_param_named(dev_tx_fifo_size_2,
34017 + dwc_otg_module_params.dev_tx_fifo_size[1], int, 0444);
34018 +MODULE_PARM_DESC(dev_tx_fifo_size_2, "Number of words in the Tx FIFO 4-768");
34019 +module_param_named(dev_tx_fifo_size_3,
34020 + dwc_otg_module_params.dev_tx_fifo_size[2], int, 0444);
34021 +MODULE_PARM_DESC(dev_tx_fifo_size_3, "Number of words in the Tx FIFO 4-768");
34022 +module_param_named(dev_tx_fifo_size_4,
34023 + dwc_otg_module_params.dev_tx_fifo_size[3], int, 0444);
34024 +MODULE_PARM_DESC(dev_tx_fifo_size_4, "Number of words in the Tx FIFO 4-768");
34025 +module_param_named(dev_tx_fifo_size_5,
34026 + dwc_otg_module_params.dev_tx_fifo_size[4], int, 0444);
34027 +MODULE_PARM_DESC(dev_tx_fifo_size_5, "Number of words in the Tx FIFO 4-768");
34028 +module_param_named(dev_tx_fifo_size_6,
34029 + dwc_otg_module_params.dev_tx_fifo_size[5], int, 0444);
34030 +MODULE_PARM_DESC(dev_tx_fifo_size_6, "Number of words in the Tx FIFO 4-768");
34031 +module_param_named(dev_tx_fifo_size_7,
34032 + dwc_otg_module_params.dev_tx_fifo_size[6], int, 0444);
34033 +MODULE_PARM_DESC(dev_tx_fifo_size_7, "Number of words in the Tx FIFO 4-768");
34034 +module_param_named(dev_tx_fifo_size_8,
34035 + dwc_otg_module_params.dev_tx_fifo_size[7], int, 0444);
34036 +MODULE_PARM_DESC(dev_tx_fifo_size_8, "Number of words in the Tx FIFO 4-768");
34037 +module_param_named(dev_tx_fifo_size_9,
34038 + dwc_otg_module_params.dev_tx_fifo_size[8], int, 0444);
34039 +MODULE_PARM_DESC(dev_tx_fifo_size_9, "Number of words in the Tx FIFO 4-768");
34040 +module_param_named(dev_tx_fifo_size_10,
34041 + dwc_otg_module_params.dev_tx_fifo_size[9], int, 0444);
34042 +MODULE_PARM_DESC(dev_tx_fifo_size_10, "Number of words in the Tx FIFO 4-768");
34043 +module_param_named(dev_tx_fifo_size_11,
34044 + dwc_otg_module_params.dev_tx_fifo_size[10], int, 0444);
34045 +MODULE_PARM_DESC(dev_tx_fifo_size_11, "Number of words in the Tx FIFO 4-768");
34046 +module_param_named(dev_tx_fifo_size_12,
34047 + dwc_otg_module_params.dev_tx_fifo_size[11], int, 0444);
34048 +MODULE_PARM_DESC(dev_tx_fifo_size_12, "Number of words in the Tx FIFO 4-768");
34049 +module_param_named(dev_tx_fifo_size_13,
34050 + dwc_otg_module_params.dev_tx_fifo_size[12], int, 0444);
34051 +MODULE_PARM_DESC(dev_tx_fifo_size_13, "Number of words in the Tx FIFO 4-768");
34052 +module_param_named(dev_tx_fifo_size_14,
34053 + dwc_otg_module_params.dev_tx_fifo_size[13], int, 0444);
34054 +MODULE_PARM_DESC(dev_tx_fifo_size_14, "Number of words in the Tx FIFO 4-768");
34055 +module_param_named(dev_tx_fifo_size_15,
34056 + dwc_otg_module_params.dev_tx_fifo_size[14], int, 0444);
34057 +MODULE_PARM_DESC(dev_tx_fifo_size_15, "Number of words in the Tx FIFO 4-768");
34058 +
34059 +module_param_named(thr_ctl, dwc_otg_module_params.thr_ctl, int, 0444);
34060 +MODULE_PARM_DESC(thr_ctl,
34061 + "Thresholding enable flag bit 0 - non ISO Tx thr., 1 - ISO Tx thr., 2 - Rx thr.- bit 0=disabled 1=enabled");
34062 +module_param_named(tx_thr_length, dwc_otg_module_params.tx_thr_length, int,
34063 + 0444);
34064 +MODULE_PARM_DESC(tx_thr_length, "Tx Threshold length in 32 bit DWORDs");
34065 +module_param_named(rx_thr_length, dwc_otg_module_params.rx_thr_length, int,
34066 + 0444);
34067 +MODULE_PARM_DESC(rx_thr_length, "Rx Threshold length in 32 bit DWORDs");
34068 +
34069 +module_param_named(pti_enable, dwc_otg_module_params.pti_enable, int, 0444);
34070 +module_param_named(mpi_enable, dwc_otg_module_params.mpi_enable, int, 0444);
34071 +module_param_named(lpm_enable, dwc_otg_module_params.lpm_enable, int, 0444);
34072 +MODULE_PARM_DESC(lpm_enable, "LPM Enable 0=LPM Disabled 1=LPM Enabled");
34073 +module_param_named(ic_usb_cap, dwc_otg_module_params.ic_usb_cap, int, 0444);
34074 +MODULE_PARM_DESC(ic_usb_cap,
34075 + "IC_USB Capability 0=IC_USB Disabled 1=IC_USB Enabled");
34076 +module_param_named(ahb_thr_ratio, dwc_otg_module_params.ahb_thr_ratio, int,
34077 + 0444);
34078 +MODULE_PARM_DESC(ahb_thr_ratio, "AHB Threshold Ratio");
34079 +module_param_named(power_down, dwc_otg_module_params.power_down, int, 0444);
34080 +MODULE_PARM_DESC(power_down, "Power Down Mode");
34081 +module_param_named(reload_ctl, dwc_otg_module_params.reload_ctl, int, 0444);
34082 +MODULE_PARM_DESC(reload_ctl, "HFIR Reload Control");
34083 +module_param_named(dev_out_nak, dwc_otg_module_params.dev_out_nak, int, 0444);
34084 +MODULE_PARM_DESC(dev_out_nak, "Enable Device OUT NAK");
34085 +module_param_named(cont_on_bna, dwc_otg_module_params.cont_on_bna, int, 0444);
34086 +MODULE_PARM_DESC(cont_on_bna, "Enable Enable Continue on BNA");
34087 +module_param_named(ahb_single, dwc_otg_module_params.ahb_single, int, 0444);
34088 +MODULE_PARM_DESC(ahb_single, "Enable AHB Single Support");
34089 +module_param_named(adp_enable, dwc_otg_module_params.adp_enable, int, 0444);
34090 +MODULE_PARM_DESC(adp_enable, "ADP Enable 0=ADP Disabled 1=ADP Enabled");
34091 +module_param_named(otg_ver, dwc_otg_module_params.otg_ver, int, 0444);
34092 +MODULE_PARM_DESC(otg_ver, "OTG revision supported 0=OTG 1.3 1=OTG 2.0");
34093 +module_param(microframe_schedule, bool, 0444);
34094 +MODULE_PARM_DESC(microframe_schedule, "Enable the microframe scheduler");
34095 +
34096 +module_param(fiq_enable, bool, 0444);
34097 +MODULE_PARM_DESC(fiq_enable, "Enable the FIQ");
34098 +module_param(nak_holdoff, ushort, 0644);
34099 +MODULE_PARM_DESC(nak_holdoff, "Throttle duration for bulk split-transaction endpoints on a NAK. Default 8");
34100 +module_param(fiq_fsm_enable, bool, 0444);
34101 +MODULE_PARM_DESC(fiq_fsm_enable, "Enable the FIQ to perform split transactions as defined by fiq_fsm_mask");
34102 +module_param(fiq_fsm_mask, ushort, 0444);
34103 +MODULE_PARM_DESC(fiq_fsm_mask, "Bitmask of transactions to perform in the FIQ.\n"
34104 + "Bit 0 : Non-periodic split transactions\n"
34105 + "Bit 1 : Periodic split transactions\n"
34106 + "Bit 2 : High-speed multi-transfer isochronous\n"
34107 + "All other bits should be set 0.");
34108 +module_param(int_ep_interval_min, ushort, 0644);
34109 +MODULE_PARM_DESC(int_ep_interval_min, "Clamp high-speed Interrupt endpoints to a minimum polling interval.\n"
34110 + "0..1 = Use endpoint default\n"
34111 + "2..n = Minimum interval n microframes. Use powers of 2.\n");
34112 +
34113 +/** @page "Module Parameters"
34114 + *
34115 + * The following parameters may be specified when starting the module.
34116 + * These parameters define how the DWC_otg controller should be
34117 + * configured. Parameter values are passed to the CIL initialization
34118 + * function dwc_otg_cil_init
34119 + *
34120 + * Example: <code>modprobe dwc_otg speed=1 otg_cap=1</code>
34121 + *
34122 +
34123 + <table>
34124 + <tr><td>Parameter Name</td><td>Meaning</td></tr>
34125 +
34126 + <tr>
34127 + <td>otg_cap</td>
34128 + <td>Specifies the OTG capabilities. The driver will automatically detect the
34129 + value for this parameter if none is specified.
34130 + - 0: HNP and SRP capable (default, if available)
34131 + - 1: SRP Only capable
34132 + - 2: No HNP/SRP capable
34133 + </td></tr>
34134 +
34135 + <tr>
34136 + <td>dma_enable</td>
34137 + <td>Specifies whether to use slave or DMA mode for accessing the data FIFOs.
34138 + The driver will automatically detect the value for this parameter if none is
34139 + specified.
34140 + - 0: Slave
34141 + - 1: DMA (default, if available)
34142 + </td></tr>
34143 +
34144 + <tr>
34145 + <td>dma_burst_size</td>
34146 + <td>The DMA Burst size (applicable only for External DMA Mode).
34147 + - Values: 1, 4, 8 16, 32, 64, 128, 256 (default 32)
34148 + </td></tr>
34149 +
34150 + <tr>
34151 + <td>speed</td>
34152 + <td>Specifies the maximum speed of operation in host and device mode. The
34153 + actual speed depends on the speed of the attached device and the value of
34154 + phy_type.
34155 + - 0: High Speed (default)
34156 + - 1: Full Speed
34157 + </td></tr>
34158 +
34159 + <tr>
34160 + <td>host_support_fs_ls_low_power</td>
34161 + <td>Specifies whether low power mode is supported when attached to a Full
34162 + Speed or Low Speed device in host mode.
34163 + - 0: Don't support low power mode (default)
34164 + - 1: Support low power mode
34165 + </td></tr>
34166 +
34167 + <tr>
34168 + <td>host_ls_low_power_phy_clk</td>
34169 + <td>Specifies the PHY clock rate in low power mode when connected to a Low
34170 + Speed device in host mode. This parameter is applicable only if
34171 + HOST_SUPPORT_FS_LS_LOW_POWER is enabled.
34172 + - 0: 48 MHz (default)
34173 + - 1: 6 MHz
34174 + </td></tr>
34175 +
34176 + <tr>
34177 + <td>enable_dynamic_fifo</td>
34178 + <td> Specifies whether FIFOs may be resized by the driver software.
34179 + - 0: Use cC FIFO size parameters
34180 + - 1: Allow dynamic FIFO sizing (default)
34181 + </td></tr>
34182 +
34183 + <tr>
34184 + <td>data_fifo_size</td>
34185 + <td>Total number of 4-byte words in the data FIFO memory. This memory
34186 + includes the Rx FIFO, non-periodic Tx FIFO, and periodic Tx FIFOs.
34187 + - Values: 32 to 32768 (default 8192)
34188 +
34189 + Note: The total FIFO memory depth in the FPGA configuration is 8192.
34190 + </td></tr>
34191 +
34192 + <tr>
34193 + <td>dev_rx_fifo_size</td>
34194 + <td>Number of 4-byte words in the Rx FIFO in device mode when dynamic
34195 + FIFO sizing is enabled.
34196 + - Values: 16 to 32768 (default 1064)
34197 + </td></tr>
34198 +
34199 + <tr>
34200 + <td>dev_nperio_tx_fifo_size</td>
34201 + <td>Number of 4-byte words in the non-periodic Tx FIFO in device mode when
34202 + dynamic FIFO sizing is enabled.
34203 + - Values: 16 to 32768 (default 1024)
34204 + </td></tr>
34205 +
34206 + <tr>
34207 + <td>dev_perio_tx_fifo_size_n (n = 1 to 15)</td>
34208 + <td>Number of 4-byte words in each of the periodic Tx FIFOs in device mode
34209 + when dynamic FIFO sizing is enabled.
34210 + - Values: 4 to 768 (default 256)
34211 + </td></tr>
34212 +
34213 + <tr>
34214 + <td>host_rx_fifo_size</td>
34215 + <td>Number of 4-byte words in the Rx FIFO in host mode when dynamic FIFO
34216 + sizing is enabled.
34217 + - Values: 16 to 32768 (default 1024)
34218 + </td></tr>
34219 +
34220 + <tr>
34221 + <td>host_nperio_tx_fifo_size</td>
34222 + <td>Number of 4-byte words in the non-periodic Tx FIFO in host mode when
34223 + dynamic FIFO sizing is enabled in the core.
34224 + - Values: 16 to 32768 (default 1024)
34225 + </td></tr>
34226 +
34227 + <tr>
34228 + <td>host_perio_tx_fifo_size</td>
34229 + <td>Number of 4-byte words in the host periodic Tx FIFO when dynamic FIFO
34230 + sizing is enabled.
34231 + - Values: 16 to 32768 (default 1024)
34232 + </td></tr>
34233 +
34234 + <tr>
34235 + <td>max_transfer_size</td>
34236 + <td>The maximum transfer size supported in bytes.
34237 + - Values: 2047 to 65,535 (default 65,535)
34238 + </td></tr>
34239 +
34240 + <tr>
34241 + <td>max_packet_count</td>
34242 + <td>The maximum number of packets in a transfer.
34243 + - Values: 15 to 511 (default 511)
34244 + </td></tr>
34245 +
34246 + <tr>
34247 + <td>host_channels</td>
34248 + <td>The number of host channel registers to use.
34249 + - Values: 1 to 16 (default 12)
34250 +
34251 + Note: The FPGA configuration supports a maximum of 12 host channels.
34252 + </td></tr>
34253 +
34254 + <tr>
34255 + <td>dev_endpoints</td>
34256 + <td>The number of endpoints in addition to EP0 available for device mode
34257 + operations.
34258 + - Values: 1 to 15 (default 6 IN and OUT)
34259 +
34260 + Note: The FPGA configuration supports a maximum of 6 IN and OUT endpoints in
34261 + addition to EP0.
34262 + </td></tr>
34263 +
34264 + <tr>
34265 + <td>phy_type</td>
34266 + <td>Specifies the type of PHY interface to use. By default, the driver will
34267 + automatically detect the phy_type.
34268 + - 0: Full Speed
34269 + - 1: UTMI+ (default, if available)
34270 + - 2: ULPI
34271 + </td></tr>
34272 +
34273 + <tr>
34274 + <td>phy_utmi_width</td>
34275 + <td>Specifies the UTMI+ Data Width. This parameter is applicable for a
34276 + phy_type of UTMI+. Also, this parameter is applicable only if the
34277 + OTG_HSPHY_WIDTH cC parameter was set to "8 and 16 bits", meaning that the
34278 + core has been configured to work at either data path width.
34279 + - Values: 8 or 16 bits (default 16)
34280 + </td></tr>
34281 +
34282 + <tr>
34283 + <td>phy_ulpi_ddr</td>
34284 + <td>Specifies whether the ULPI operates at double or single data rate. This
34285 + parameter is only applicable if phy_type is ULPI.
34286 + - 0: single data rate ULPI interface with 8 bit wide data bus (default)
34287 + - 1: double data rate ULPI interface with 4 bit wide data bus
34288 + </td></tr>
34289 +
34290 + <tr>
34291 + <td>i2c_enable</td>
34292 + <td>Specifies whether to use the I2C interface for full speed PHY. This
34293 + parameter is only applicable if PHY_TYPE is FS.
34294 + - 0: Disabled (default)
34295 + - 1: Enabled
34296 + </td></tr>
34297 +
34298 + <tr>
34299 + <td>ulpi_fs_ls</td>
34300 + <td>Specifies whether to use ULPI FS/LS mode only.
34301 + - 0: Disabled (default)
34302 + - 1: Enabled
34303 + </td></tr>
34304 +
34305 + <tr>
34306 + <td>ts_dline</td>
34307 + <td>Specifies whether term select D-Line pulsing for all PHYs is enabled.
34308 + - 0: Disabled (default)
34309 + - 1: Enabled
34310 + </td></tr>
34311 +
34312 + <tr>
34313 + <td>en_multiple_tx_fifo</td>
34314 + <td>Specifies whether dedicatedto tx fifos are enabled for non periodic IN EPs.
34315 + The driver will automatically detect the value for this parameter if none is
34316 + specified.
34317 + - 0: Disabled
34318 + - 1: Enabled (default, if available)
34319 + </td></tr>
34320 +
34321 + <tr>
34322 + <td>dev_tx_fifo_size_n (n = 1 to 15)</td>
34323 + <td>Number of 4-byte words in each of the Tx FIFOs in device mode
34324 + when dynamic FIFO sizing is enabled.
34325 + - Values: 4 to 768 (default 256)
34326 + </td></tr>
34327 +
34328 + <tr>
34329 + <td>tx_thr_length</td>
34330 + <td>Transmit Threshold length in 32 bit double words
34331 + - Values: 8 to 128 (default 64)
34332 + </td></tr>
34333 +
34334 + <tr>
34335 + <td>rx_thr_length</td>
34336 + <td>Receive Threshold length in 32 bit double words
34337 + - Values: 8 to 128 (default 64)
34338 + </td></tr>
34339 +
34340 +<tr>
34341 + <td>thr_ctl</td>
34342 + <td>Specifies whether to enable Thresholding for Device mode. Bits 0, 1, 2 of
34343 + this parmater specifies if thresholding is enabled for non-Iso Tx, Iso Tx and
34344 + Rx transfers accordingly.
34345 + The driver will automatically detect the value for this parameter if none is
34346 + specified.
34347 + - Values: 0 to 7 (default 0)
34348 + Bit values indicate:
34349 + - 0: Thresholding disabled
34350 + - 1: Thresholding enabled
34351 + </td></tr>
34352 +
34353 +<tr>
34354 + <td>dma_desc_enable</td>
34355 + <td>Specifies whether to enable Descriptor DMA mode.
34356 + The driver will automatically detect the value for this parameter if none is
34357 + specified.
34358 + - 0: Descriptor DMA disabled
34359 + - 1: Descriptor DMA (default, if available)
34360 + </td></tr>
34361 +
34362 +<tr>
34363 + <td>mpi_enable</td>
34364 + <td>Specifies whether to enable MPI enhancement mode.
34365 + The driver will automatically detect the value for this parameter if none is
34366 + specified.
34367 + - 0: MPI disabled (default)
34368 + - 1: MPI enable
34369 + </td></tr>
34370 +
34371 +<tr>
34372 + <td>pti_enable</td>
34373 + <td>Specifies whether to enable PTI enhancement support.
34374 + The driver will automatically detect the value for this parameter if none is
34375 + specified.
34376 + - 0: PTI disabled (default)
34377 + - 1: PTI enable
34378 + </td></tr>
34379 +
34380 +<tr>
34381 + <td>lpm_enable</td>
34382 + <td>Specifies whether to enable LPM support.
34383 + The driver will automatically detect the value for this parameter if none is
34384 + specified.
34385 + - 0: LPM disabled
34386 + - 1: LPM enable (default, if available)
34387 + </td></tr>
34388 +
34389 +<tr>
34390 + <td>ic_usb_cap</td>
34391 + <td>Specifies whether to enable IC_USB capability.
34392 + The driver will automatically detect the value for this parameter if none is
34393 + specified.
34394 + - 0: IC_USB disabled (default, if available)
34395 + - 1: IC_USB enable
34396 + </td></tr>
34397 +
34398 +<tr>
34399 + <td>ahb_thr_ratio</td>
34400 + <td>Specifies AHB Threshold ratio.
34401 + - Values: 0 to 3 (default 0)
34402 + </td></tr>
34403 +
34404 +<tr>
34405 + <td>power_down</td>
34406 + <td>Specifies Power Down(Hibernation) Mode.
34407 + The driver will automatically detect the value for this parameter if none is
34408 + specified.
34409 + - 0: Power Down disabled (default)
34410 + - 2: Power Down enabled
34411 + </td></tr>
34412 +
34413 + <tr>
34414 + <td>reload_ctl</td>
34415 + <td>Specifies whether dynamic reloading of the HFIR register is allowed during
34416 + run time. The driver will automatically detect the value for this parameter if
34417 + none is specified. In case the HFIR value is reloaded when HFIR.RldCtrl == 1'b0
34418 + the core might misbehave.
34419 + - 0: Reload Control disabled (default)
34420 + - 1: Reload Control enabled
34421 + </td></tr>
34422 +
34423 + <tr>
34424 + <td>dev_out_nak</td>
34425 + <td>Specifies whether Device OUT NAK enhancement enabled or no.
34426 + The driver will automatically detect the value for this parameter if
34427 + none is specified. This parameter is valid only when OTG_EN_DESC_DMA == 1b1.
34428 + - 0: The core does not set NAK after Bulk OUT transfer complete (default)
34429 + - 1: The core sets NAK after Bulk OUT transfer complete
34430 + </td></tr>
34431 +
34432 + <tr>
34433 + <td>cont_on_bna</td>
34434 + <td>Specifies whether Enable Continue on BNA enabled or no.
34435 + After receiving BNA interrupt the core disables the endpoint,when the
34436 + endpoint is re-enabled by the application the
34437 + - 0: Core starts processing from the DOEPDMA descriptor (default)
34438 + - 1: Core starts processing from the descriptor which received the BNA.
34439 + This parameter is valid only when OTG_EN_DESC_DMA == 1b1.
34440 + </td></tr>
34441 +
34442 + <tr>
34443 + <td>ahb_single</td>
34444 + <td>This bit when programmed supports SINGLE transfers for remainder data
34445 + in a transfer for DMA mode of operation.
34446 + - 0: The remainder data will be sent using INCR burst size (default)
34447 + - 1: The remainder data will be sent using SINGLE burst size.
34448 + </td></tr>
34449 +
34450 +<tr>
34451 + <td>adp_enable</td>
34452 + <td>Specifies whether ADP feature is enabled.
34453 + The driver will automatically detect the value for this parameter if none is
34454 + specified.
34455 + - 0: ADP feature disabled (default)
34456 + - 1: ADP feature enabled
34457 + </td></tr>
34458 +
34459 + <tr>
34460 + <td>otg_ver</td>
34461 + <td>Specifies whether OTG is performing as USB OTG Revision 2.0 or Revision 1.3
34462 + USB OTG device.
34463 + - 0: OTG 2.0 support disabled (default)
34464 + - 1: OTG 2.0 support enabled
34465 + </td></tr>
34466 +
34467 +*/
34468 --- /dev/null
34469 +++ b/drivers/usb/host/dwc_otg/dwc_otg_driver.h
34470 @@ -0,0 +1,86 @@
34471 +/* ==========================================================================
34472 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_driver.h $
34473 + * $Revision: #19 $
34474 + * $Date: 2010/11/15 $
34475 + * $Change: 1627671 $
34476 + *
34477 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
34478 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
34479 + * otherwise expressly agreed to in writing between Synopsys and you.
34480 + *
34481 + * The Software IS NOT an item of Licensed Software or Licensed Product under
34482 + * any End User Software License Agreement or Agreement for Licensed Product
34483 + * with Synopsys or any supplement thereto. You are permitted to use and
34484 + * redistribute this Software in source and binary forms, with or without
34485 + * modification, provided that redistributions of source code must retain this
34486 + * notice. You may not view, use, disclose, copy or distribute this file or
34487 + * any information contained herein except pursuant to this license grant from
34488 + * Synopsys. If you do not agree with this notice, including the disclaimer
34489 + * below, then you are not authorized to use the Software.
34490 + *
34491 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
34492 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34493 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34494 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
34495 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34496 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
34497 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
34498 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34499 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34500 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
34501 + * DAMAGE.
34502 + * ========================================================================== */
34503 +
34504 +#ifndef __DWC_OTG_DRIVER_H__
34505 +#define __DWC_OTG_DRIVER_H__
34506 +
34507 +/** @file
34508 + * This file contains the interface to the Linux driver.
34509 + */
34510 +#include "dwc_otg_os_dep.h"
34511 +#include "dwc_otg_core_if.h"
34512 +
34513 +/* Type declarations */
34514 +struct dwc_otg_pcd;
34515 +struct dwc_otg_hcd;
34516 +
34517 +/**
34518 + * This structure is a wrapper that encapsulates the driver components used to
34519 + * manage a single DWC_otg controller.
34520 + */
34521 +typedef struct dwc_otg_device {
34522 + /** Structure containing OS-dependent stuff. KEEP THIS STRUCT AT THE
34523 + * VERY BEGINNING OF THE DEVICE STRUCT. OSes such as FreeBSD and NetBSD
34524 + * require this. */
34525 + struct os_dependent os_dep;
34526 +
34527 + /** Pointer to the core interface structure. */
34528 + dwc_otg_core_if_t *core_if;
34529 +
34530 + /** Pointer to the PCD structure. */
34531 + struct dwc_otg_pcd *pcd;
34532 +
34533 + /** Pointer to the HCD structure. */
34534 + struct dwc_otg_hcd *hcd;
34535 +
34536 + /** Flag to indicate whether the common IRQ handler is installed. */
34537 + uint8_t common_irq_installed;
34538 +
34539 +} dwc_otg_device_t;
34540 +
34541 +/*We must clear S3C24XX_EINTPEND external interrupt register
34542 + * because after clearing in this register trigerred IRQ from
34543 + * H/W core in kernel interrupt can be occured again before OTG
34544 + * handlers clear all IRQ sources of Core registers because of
34545 + * timing latencies and Low Level IRQ Type.
34546 + */
34547 +#ifdef CONFIG_MACH_IPMATE
34548 +#define S3C2410X_CLEAR_EINTPEND() \
34549 +do { \
34550 + __raw_writel(1UL << 11,S3C24XX_EINTPEND); \
34551 +} while (0)
34552 +#else
34553 +#define S3C2410X_CLEAR_EINTPEND() do { } while (0)
34554 +#endif
34555 +
34556 +#endif
34557 --- /dev/null
34558 +++ b/drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.c
34559 @@ -0,0 +1,1389 @@
34560 +/*
34561 + * dwc_otg_fiq_fsm.c - The finite state machine FIQ
34562 + *
34563 + * Copyright (c) 2013 Raspberry Pi Foundation
34564 + *
34565 + * Author: Jonathan Bell <jonathan@raspberrypi.org>
34566 + * All rights reserved.
34567 + *
34568 + * Redistribution and use in source and binary forms, with or without
34569 + * modification, are permitted provided that the following conditions are met:
34570 + * * Redistributions of source code must retain the above copyright
34571 + * notice, this list of conditions and the following disclaimer.
34572 + * * Redistributions in binary form must reproduce the above copyright
34573 + * notice, this list of conditions and the following disclaimer in the
34574 + * documentation and/or other materials provided with the distribution.
34575 + * * Neither the name of Raspberry Pi nor the
34576 + * names of its contributors may be used to endorse or promote products
34577 + * derived from this software without specific prior written permission.
34578 + *
34579 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
34580 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
34581 + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
34582 + * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
34583 + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34584 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
34585 + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
34586 + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34587 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34588 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34589 + *
34590 + * This FIQ implements functionality that performs split transactions on
34591 + * the dwc_otg hardware without any outside intervention. A split transaction
34592 + * is "queued" by nominating a specific host channel to perform the entirety
34593 + * of a split transaction. This FIQ will then perform the microframe-precise
34594 + * scheduling required in each phase of the transaction until completion.
34595 + *
34596 + * The FIQ functionality is glued into the Synopsys driver via the entry point
34597 + * in the FSM enqueue function, and at the exit point in handling a HC interrupt
34598 + * for a FSM-enabled channel.
34599 + *
34600 + * NB: Large parts of this implementation have architecture-specific code.
34601 + * For porting this functionality to other ARM machines, the minimum is required:
34602 + * - An interrupt controller allowing the top-level dwc USB interrupt to be routed
34603 + * to the FIQ
34604 + * - A method of forcing a software generated interrupt from FIQ mode that then
34605 + * triggers an IRQ entry (with the dwc USB handler called by this IRQ number)
34606 + * - Guaranteed interrupt routing such that both the FIQ and SGI occur on the same
34607 + * processor core - there is no locking between the FIQ and IRQ (aside from
34608 + * local_fiq_disable)
34609 + *
34610 + */
34611 +
34612 +#include "dwc_otg_fiq_fsm.h"
34613 +
34614 +
34615 +char buffer[1000*16];
34616 +int wptr;
34617 +void notrace _fiq_print(enum fiq_debug_level dbg_lvl, volatile struct fiq_state *state, char *fmt, ...)
34618 +{
34619 + enum fiq_debug_level dbg_lvl_req = FIQDBG_ERR;
34620 + va_list args;
34621 + char text[17];
34622 + hfnum_data_t hfnum = { .d32 = FIQ_READ(state->dwc_regs_base + 0x408) };
34623 +
34624 + if((dbg_lvl & dbg_lvl_req) || dbg_lvl == FIQDBG_ERR)
34625 + {
34626 + snprintf(text, 9, " %4d:%1u ", hfnum.b.frnum/8, hfnum.b.frnum & 7);
34627 + va_start(args, fmt);
34628 + vsnprintf(text+8, 9, fmt, args);
34629 + va_end(args);
34630 +
34631 + memcpy(buffer + wptr, text, 16);
34632 + wptr = (wptr + 16) % sizeof(buffer);
34633 + }
34634 +}
34635 +
34636 +/**
34637 + * fiq_fsm_spin_lock() - ARMv6+ bare bones spinlock
34638 + * Must be called with local interrupts and FIQ disabled.
34639 + */
34640 +#if defined(CONFIG_ARCH_BCM2835) && defined(CONFIG_SMP)
34641 +inline void fiq_fsm_spin_lock(fiq_lock_t *lock)
34642 +{
34643 + unsigned long tmp;
34644 + uint32_t newval;
34645 + fiq_lock_t lockval;
34646 + /* Nested locking, yay. If we are on the same CPU as the fiq, then the disable
34647 + * will be sufficient. If we are on a different CPU, then the lock protects us. */
34648 + prefetchw(&lock->slock);
34649 + asm volatile (
34650 + "1: ldrex %0, [%3]\n"
34651 + " add %1, %0, %4\n"
34652 + " strex %2, %1, [%3]\n"
34653 + " teq %2, #0\n"
34654 + " bne 1b"
34655 + : "=&r" (lockval), "=&r" (newval), "=&r" (tmp)
34656 + : "r" (&lock->slock), "I" (1 << 16)
34657 + : "cc");
34658 +
34659 + while (lockval.tickets.next != lockval.tickets.owner) {
34660 + wfe();
34661 + lockval.tickets.owner = ACCESS_ONCE(lock->tickets.owner);
34662 + }
34663 + smp_mb();
34664 +}
34665 +#else
34666 +inline void fiq_fsm_spin_lock(fiq_lock_t *lock) { }
34667 +#endif
34668 +
34669 +/**
34670 + * fiq_fsm_spin_unlock() - ARMv6+ bare bones spinunlock
34671 + */
34672 +#if defined(CONFIG_ARCH_BCM2835) && defined(CONFIG_SMP)
34673 +inline void fiq_fsm_spin_unlock(fiq_lock_t *lock)
34674 +{
34675 + smp_mb();
34676 + lock->tickets.owner++;
34677 + dsb_sev();
34678 +}
34679 +#else
34680 +inline void fiq_fsm_spin_unlock(fiq_lock_t *lock) { }
34681 +#endif
34682 +
34683 +/**
34684 + * fiq_fsm_restart_channel() - Poke channel enable bit for a split transaction
34685 + * @channel: channel to re-enable
34686 + */
34687 +static void fiq_fsm_restart_channel(struct fiq_state *st, int n, int force)
34688 +{
34689 + hcchar_data_t hcchar = { .d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR) };
34690 +
34691 + hcchar.b.chen = 0;
34692 + if (st->channel[n].hcchar_copy.b.eptype & 0x1) {
34693 + hfnum_data_t hfnum = { .d32 = FIQ_READ(st->dwc_regs_base + HFNUM) };
34694 + /* Hardware bug workaround: update the ssplit index */
34695 + if (st->channel[n].hcsplt_copy.b.spltena)
34696 + st->channel[n].expected_uframe = (hfnum.b.frnum + 1) & 0x3FFF;
34697 +
34698 + hcchar.b.oddfrm = (hfnum.b.frnum & 0x1) ? 0 : 1;
34699 + }
34700 +
34701 + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR, hcchar.d32);
34702 + hcchar.d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR);
34703 + hcchar.b.chen = 1;
34704 +
34705 + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR, hcchar.d32);
34706 + fiq_print(FIQDBG_INT, st, "HCGO %01d %01d", n, force);
34707 +}
34708 +
34709 +/**
34710 + * fiq_fsm_setup_csplit() - Prepare a host channel for a CSplit transaction stage
34711 + * @st: Pointer to the channel's state
34712 + * @n : channel number
34713 + *
34714 + * Change host channel registers to perform a complete-split transaction. Being mindful of the
34715 + * endpoint direction, set control regs up correctly.
34716 + */
34717 +static void notrace fiq_fsm_setup_csplit(struct fiq_state *st, int n)
34718 +{
34719 + hcsplt_data_t hcsplt = { .d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCSPLT) };
34720 + hctsiz_data_t hctsiz = { .d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ) };
34721 +
34722 + hcsplt.b.compsplt = 1;
34723 + if (st->channel[n].hcchar_copy.b.epdir == 1) {
34724 + // If IN, the CSPLIT result contains the data or a hub handshake. hctsiz = maxpacket.
34725 + hctsiz.b.xfersize = st->channel[n].hctsiz_copy.b.xfersize;
34726 + } else {
34727 + // If OUT, the CSPLIT result contains handshake only.
34728 + hctsiz.b.xfersize = 0;
34729 + }
34730 + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCSPLT, hcsplt.d32);
34731 + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ, hctsiz.d32);
34732 + mb();
34733 +}
34734 +
34735 +/**
34736 + * fiq_fsm_restart_np_pending() - Restart a single non-periodic contended transfer
34737 + * @st: Pointer to the channel's state
34738 + * @num_channels: Total number of host channels
34739 + * @orig_channel: Channel index of completed transfer
34740 + *
34741 + * In the case where an IN and OUT transfer are simultaneously scheduled to the
34742 + * same device/EP, inadequate hub implementations will misbehave. Once the first
34743 + * transfer is complete, a pending non-periodic split can then be issued.
34744 + */
34745 +static void notrace fiq_fsm_restart_np_pending(struct fiq_state *st, int num_channels, int orig_channel)
34746 +{
34747 + int i;
34748 + int dev_addr = st->channel[orig_channel].hcchar_copy.b.devaddr;
34749 + int ep_num = st->channel[orig_channel].hcchar_copy.b.epnum;
34750 + for (i = 0; i < num_channels; i++) {
34751 + if (st->channel[i].fsm == FIQ_NP_SSPLIT_PENDING &&
34752 + st->channel[i].hcchar_copy.b.devaddr == dev_addr &&
34753 + st->channel[i].hcchar_copy.b.epnum == ep_num) {
34754 + st->channel[i].fsm = FIQ_NP_SSPLIT_STARTED;
34755 + fiq_fsm_restart_channel(st, i, 0);
34756 + break;
34757 + }
34758 + }
34759 +}
34760 +
34761 +static inline int notrace fiq_get_xfer_len(struct fiq_state *st, int n)
34762 +{
34763 + /* The xfersize register is a bit wonky. For IN transfers, it decrements by the packet size. */
34764 + hctsiz_data_t hctsiz = { .d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ) };
34765 +
34766 + if (st->channel[n].hcchar_copy.b.epdir == 0) {
34767 + return st->channel[n].hctsiz_copy.b.xfersize;
34768 + } else {
34769 + return st->channel[n].hctsiz_copy.b.xfersize - hctsiz.b.xfersize;
34770 + }
34771 +
34772 +}
34773 +
34774 +
34775 +/**
34776 + * fiq_increment_dma_buf() - update DMA address for bounce buffers after a CSPLIT
34777 + *
34778 + * Of use only for IN periodic transfers.
34779 + */
34780 +static int notrace fiq_increment_dma_buf(struct fiq_state *st, int num_channels, int n)
34781 +{
34782 + hcdma_data_t hcdma;
34783 + int i = st->channel[n].dma_info.index;
34784 + int len;
34785 + struct fiq_dma_blob *blob = (struct fiq_dma_blob *) st->dma_base;
34786 +
34787 + len = fiq_get_xfer_len(st, n);
34788 + fiq_print(FIQDBG_INT, st, "LEN: %03d", len);
34789 + st->channel[n].dma_info.slot_len[i] = len;
34790 + i++;
34791 + if (i > 6)
34792 + BUG();
34793 +
34794 + hcdma.d32 = (dma_addr_t) &blob->channel[n].index[i].buf[0];
34795 + FIQ_WRITE(st->dwc_regs_base + HC_DMA + (HC_OFFSET * n), hcdma.d32);
34796 + st->channel[n].dma_info.index = i;
34797 + return 0;
34798 +}
34799 +
34800 +/**
34801 + * fiq_reload_hctsiz() - for IN transactions, reset HCTSIZ
34802 + */
34803 +static void notrace fiq_fsm_reload_hctsiz(struct fiq_state *st, int n)
34804 +{
34805 + hctsiz_data_t hctsiz = { .d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ) };
34806 + hctsiz.b.xfersize = st->channel[n].hctsiz_copy.b.xfersize;
34807 + hctsiz.b.pktcnt = 1;
34808 + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ, hctsiz.d32);
34809 +}
34810 +
34811 +/**
34812 + * fiq_iso_out_advance() - update DMA address and split position bits
34813 + * for isochronous OUT transactions.
34814 + *
34815 + * Returns 1 if this is the last packet queued, 0 otherwise. Split-ALL and
34816 + * Split-BEGIN states are not handled - this is done when the transaction was queued.
34817 + *
34818 + * This function must only be called from the FIQ_ISO_OUT_ACTIVE state.
34819 + */
34820 +static int notrace fiq_iso_out_advance(struct fiq_state *st, int num_channels, int n)
34821 +{
34822 + hcsplt_data_t hcsplt;
34823 + hctsiz_data_t hctsiz;
34824 + hcdma_data_t hcdma;
34825 + struct fiq_dma_blob *blob = (struct fiq_dma_blob *) st->dma_base;
34826 + int last = 0;
34827 + int i = st->channel[n].dma_info.index;
34828 +
34829 + fiq_print(FIQDBG_INT, st, "ADV %01d %01d ", n, i);
34830 + i++;
34831 + if (i == 4)
34832 + last = 1;
34833 + if (st->channel[n].dma_info.slot_len[i+1] == 255)
34834 + last = 1;
34835 +
34836 + /* New DMA address - address of bounce buffer referred to in index */
34837 + hcdma.d32 = (uint32_t) &blob->channel[n].index[i].buf[0];
34838 + //hcdma.d32 = FIQ_READ(st->dwc_regs_base + HC_DMA + (HC_OFFSET * n));
34839 + //hcdma.d32 += st->channel[n].dma_info.slot_len[i];
34840 + fiq_print(FIQDBG_INT, st, "LAST: %01d ", last);
34841 + fiq_print(FIQDBG_INT, st, "LEN: %03d", st->channel[n].dma_info.slot_len[i]);
34842 + hcsplt.d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCSPLT);
34843 + hctsiz.d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ);
34844 + hcsplt.b.xactpos = (last) ? ISOC_XACTPOS_END : ISOC_XACTPOS_MID;
34845 + /* Set up new packet length */
34846 + hctsiz.b.pktcnt = 1;
34847 + hctsiz.b.xfersize = st->channel[n].dma_info.slot_len[i];
34848 + fiq_print(FIQDBG_INT, st, "%08x", hctsiz.d32);
34849 +
34850 + st->channel[n].dma_info.index++;
34851 + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCSPLT, hcsplt.d32);
34852 + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ, hctsiz.d32);
34853 + FIQ_WRITE(st->dwc_regs_base + HC_DMA + (HC_OFFSET * n), hcdma.d32);
34854 + return last;
34855 +}
34856 +
34857 +/**
34858 + * fiq_fsm_tt_next_isoc() - queue next pending isochronous out start-split on a TT
34859 + *
34860 + * Despite the limitations of the DWC core, we can force a microframe pipeline of
34861 + * isochronous OUT start-split transactions while waiting for a corresponding other-type
34862 + * of endpoint to finish its CSPLITs. TTs have big periodic buffers therefore it
34863 + * is very unlikely that filling the start-split FIFO will cause data loss.
34864 + * This allows much better interleaving of transactions in an order-independent way-
34865 + * there is no requirement to prioritise isochronous, just a state-space search has
34866 + * to be performed on each periodic start-split complete interrupt.
34867 + */
34868 +static int notrace fiq_fsm_tt_next_isoc(struct fiq_state *st, int num_channels, int n)
34869 +{
34870 + int hub_addr = st->channel[n].hub_addr;
34871 + int port_addr = st->channel[n].port_addr;
34872 + int i, poked = 0;
34873 + for (i = 0; i < num_channels; i++) {
34874 + if (i == n || st->channel[i].fsm == FIQ_PASSTHROUGH)
34875 + continue;
34876 + if (st->channel[i].hub_addr == hub_addr &&
34877 + st->channel[i].port_addr == port_addr) {
34878 + switch (st->channel[i].fsm) {
34879 + case FIQ_PER_ISO_OUT_PENDING:
34880 + if (st->channel[i].nrpackets == 1) {
34881 + st->channel[i].fsm = FIQ_PER_ISO_OUT_LAST;
34882 + } else {
34883 + st->channel[i].fsm = FIQ_PER_ISO_OUT_ACTIVE;
34884 + }
34885 + fiq_fsm_restart_channel(st, i, 0);
34886 + poked = 1;
34887 + break;
34888 +
34889 + default:
34890 + break;
34891 + }
34892 + }
34893 + if (poked)
34894 + break;
34895 + }
34896 + return poked;
34897 +}
34898 +
34899 +/**
34900 + * fiq_fsm_tt_in_use() - search for host channels using this TT
34901 + * @n: Channel to use as reference
34902 + *
34903 + */
34904 +int notrace noinline fiq_fsm_tt_in_use(struct fiq_state *st, int num_channels, int n)
34905 +{
34906 + int hub_addr = st->channel[n].hub_addr;
34907 + int port_addr = st->channel[n].port_addr;
34908 + int i, in_use = 0;
34909 + for (i = 0; i < num_channels; i++) {
34910 + if (i == n || st->channel[i].fsm == FIQ_PASSTHROUGH)
34911 + continue;
34912 + switch (st->channel[i].fsm) {
34913 + /* TT is reserved for channels that are in the middle of a periodic
34914 + * split transaction.
34915 + */
34916 + case FIQ_PER_SSPLIT_STARTED:
34917 + case FIQ_PER_CSPLIT_WAIT:
34918 + case FIQ_PER_CSPLIT_NYET1:
34919 + //case FIQ_PER_CSPLIT_POLL:
34920 + case FIQ_PER_ISO_OUT_ACTIVE:
34921 + case FIQ_PER_ISO_OUT_LAST:
34922 + if (st->channel[i].hub_addr == hub_addr &&
34923 + st->channel[i].port_addr == port_addr) {
34924 + in_use = 1;
34925 + }
34926 + break;
34927 + default:
34928 + break;
34929 + }
34930 + if (in_use)
34931 + break;
34932 + }
34933 + return in_use;
34934 +}
34935 +
34936 +/**
34937 + * fiq_fsm_more_csplits() - determine whether additional CSPLITs need
34938 + * to be issued for this IN transaction.
34939 + *
34940 + * We cannot tell the inbound PID of a data packet due to hardware limitations.
34941 + * we need to make an educated guess as to whether we need to queue another CSPLIT
34942 + * or not. A no-brainer is when we have received enough data to fill the endpoint
34943 + * size, but for endpoints that give variable-length data then we have to resort
34944 + * to heuristics.
34945 + *
34946 + * We also return whether this is the last CSPLIT to be queued, again based on
34947 + * heuristics. This is to allow a 1-uframe overlap of periodic split transactions.
34948 + * Note: requires at least 1 CSPLIT to have been performed prior to being called.
34949 + */
34950 +
34951 +/*
34952 + * We need some way of guaranteeing if a returned periodic packet of size X
34953 + * has a DATA0 PID.
34954 + * The heuristic value of 144 bytes assumes that the received data has maximal
34955 + * bit-stuffing and the clock frequency of the transmitting device is at the lowest
34956 + * permissible limit. If the transfer length results in a final packet size
34957 + * 144 < p <= 188, then an erroneous CSPLIT will be issued.
34958 + * Also used to ensure that an endpoint will nominally only return a single
34959 + * complete-split worth of data.
34960 + */
34961 +#define DATA0_PID_HEURISTIC 144
34962 +
34963 +static int notrace noinline fiq_fsm_more_csplits(struct fiq_state *state, int n, int *probably_last)
34964 +{
34965 +
34966 + int i;
34967 + int total_len = 0;
34968 + int more_needed = 1;
34969 + struct fiq_channel_state *st = &state->channel[n];
34970 +
34971 + for (i = 0; i < st->dma_info.index; i++) {
34972 + total_len += st->dma_info.slot_len[i];
34973 + }
34974 +
34975 + *probably_last = 0;
34976 +
34977 + if (st->hcchar_copy.b.eptype == 0x3) {
34978 + /*
34979 + * An interrupt endpoint will take max 2 CSPLITs. if we are receiving data
34980 + * then this is definitely the last CSPLIT.
34981 + */
34982 + *probably_last = 1;
34983 + } else {
34984 + /* Isoc IN. This is a bit risky if we are the first transaction:
34985 + * we may have been held off slightly. */
34986 + if (i > 1 && st->dma_info.slot_len[st->dma_info.index-1] <= DATA0_PID_HEURISTIC) {
34987 + more_needed = 0;
34988 + }
34989 + /* If in the next uframe we will receive enough data to fill the endpoint,
34990 + * then only issue 1 more csplit.
34991 + */
34992 + if (st->hctsiz_copy.b.xfersize - total_len <= DATA0_PID_HEURISTIC)
34993 + *probably_last = 1;
34994 + }
34995 +
34996 + if (total_len >= st->hctsiz_copy.b.xfersize ||
34997 + i == 6 || total_len == 0)
34998 + /* Note: due to bit stuffing it is possible to have > 6 CSPLITs for
34999 + * a single endpoint. Accepting more would completely break our scheduling mechanism though
35000 + * - in these extreme cases we will pass through a truncated packet.
35001 + */
35002 + more_needed = 0;
35003 +
35004 + return more_needed;
35005 +}
35006 +
35007 +/**
35008 + * fiq_fsm_too_late() - Test transaction for lateness
35009 + *
35010 + * If a SSPLIT for a large IN transaction is issued too late in a frame,
35011 + * the hub will disable the port to the device and respond with ERR handshakes.
35012 + * The hub status endpoint will not reflect this change.
35013 + * Returns 1 if we will issue a SSPLIT that will result in a device babble.
35014 + */
35015 +int notrace fiq_fsm_too_late(struct fiq_state *st, int n)
35016 +{
35017 + int uframe;
35018 + hfnum_data_t hfnum = { .d32 = FIQ_READ(st->dwc_regs_base + HFNUM) };
35019 + uframe = hfnum.b.frnum & 0x7;
35020 + if ((uframe < 6) && (st->channel[n].nrpackets + 1 + uframe > 7)) {
35021 + return 1;
35022 + } else {
35023 + return 0;
35024 + }
35025 +}
35026 +
35027 +
35028 +/**
35029 + * fiq_fsm_start_next_periodic() - A half-arsed attempt at a microframe pipeline
35030 + *
35031 + * Search pending transactions in the start-split pending state and queue them.
35032 + * Don't queue packets in uframe .5 (comes out in .6) (USB2.0 11.18.4).
35033 + * Note: we specifically don't do isochronous OUT transactions first because better
35034 + * use of the TT's start-split fifo can be achieved by pipelining an IN before an OUT.
35035 + */
35036 +static void notrace noinline fiq_fsm_start_next_periodic(struct fiq_state *st, int num_channels)
35037 +{
35038 + int n;
35039 + hfnum_data_t hfnum = { .d32 = FIQ_READ(st->dwc_regs_base + HFNUM) };
35040 + if ((hfnum.b.frnum & 0x7) == 5)
35041 + return;
35042 + for (n = 0; n < num_channels; n++) {
35043 + if (st->channel[n].fsm == FIQ_PER_SSPLIT_QUEUED) {
35044 + /* Check to see if any other transactions are using this TT */
35045 + if(!fiq_fsm_tt_in_use(st, num_channels, n)) {
35046 + if (!fiq_fsm_too_late(st, n)) {
35047 + st->channel[n].fsm = FIQ_PER_SSPLIT_STARTED;
35048 + fiq_print(FIQDBG_INT, st, "NEXTPER ");
35049 + fiq_fsm_restart_channel(st, n, 0);
35050 + } else {
35051 + st->channel[n].fsm = FIQ_PER_SPLIT_TIMEOUT;
35052 + }
35053 + break;
35054 + }
35055 + }
35056 + }
35057 + for (n = 0; n < num_channels; n++) {
35058 + if (st->channel[n].fsm == FIQ_PER_ISO_OUT_PENDING) {
35059 + if (!fiq_fsm_tt_in_use(st, num_channels, n)) {
35060 + fiq_print(FIQDBG_INT, st, "NEXTISO ");
35061 + if (st->channel[n].nrpackets == 1)
35062 + st->channel[n].fsm = FIQ_PER_ISO_OUT_LAST;
35063 + else
35064 + st->channel[n].fsm = FIQ_PER_ISO_OUT_ACTIVE;
35065 + fiq_fsm_restart_channel(st, n, 0);
35066 + break;
35067 + }
35068 + }
35069 + }
35070 +}
35071 +
35072 +/**
35073 + * fiq_fsm_update_hs_isoc() - update isochronous frame and transfer data
35074 + * @state: Pointer to fiq_state
35075 + * @n: Channel transaction is active on
35076 + * @hcint: Copy of host channel interrupt register
35077 + *
35078 + * Returns 0 if there are no more transactions for this HC to do, 1
35079 + * otherwise.
35080 + */
35081 +static int notrace noinline fiq_fsm_update_hs_isoc(struct fiq_state *state, int n, hcint_data_t hcint)
35082 +{
35083 + struct fiq_channel_state *st = &state->channel[n];
35084 + int xfer_len = 0, nrpackets = 0;
35085 + hcdma_data_t hcdma;
35086 + fiq_print(FIQDBG_INT, state, "HSISO %02d", n);
35087 +
35088 + xfer_len = fiq_get_xfer_len(state, n);
35089 + st->hs_isoc_info.iso_desc[st->hs_isoc_info.index].actual_length = xfer_len;
35090 +
35091 + st->hs_isoc_info.iso_desc[st->hs_isoc_info.index].status = hcint.d32;
35092 +
35093 + st->hs_isoc_info.index++;
35094 + if (st->hs_isoc_info.index == st->hs_isoc_info.nrframes) {
35095 + return 0;
35096 + }
35097 +
35098 + /* grab the next DMA address offset from the array */
35099 + hcdma.d32 = st->hcdma_copy.d32 + st->hs_isoc_info.iso_desc[st->hs_isoc_info.index].offset;
35100 + FIQ_WRITE(state->dwc_regs_base + HC_DMA + (HC_OFFSET * n), hcdma.d32);
35101 +
35102 + /* We need to set multi_count. This is a bit tricky - has to be set per-transaction as
35103 + * the core needs to be told to send the correct number. Caution: for IN transfers,
35104 + * this is always set to the maximum size of the endpoint. */
35105 + xfer_len = st->hs_isoc_info.iso_desc[st->hs_isoc_info.index].length;
35106 + /* Integer divide in a FIQ: fun. FIXME: make this not suck */
35107 + nrpackets = (xfer_len + st->hcchar_copy.b.mps - 1) / st->hcchar_copy.b.mps;
35108 + if (nrpackets == 0)
35109 + nrpackets = 1;
35110 + st->hcchar_copy.b.multicnt = nrpackets;
35111 + st->hctsiz_copy.b.pktcnt = nrpackets;
35112 +
35113 + /* Initial PID also needs to be set */
35114 + if (st->hcchar_copy.b.epdir == 0) {
35115 + st->hctsiz_copy.b.xfersize = xfer_len;
35116 + switch (st->hcchar_copy.b.multicnt) {
35117 + case 1:
35118 + st->hctsiz_copy.b.pid = DWC_PID_DATA0;
35119 + break;
35120 + case 2:
35121 + case 3:
35122 + st->hctsiz_copy.b.pid = DWC_PID_MDATA;
35123 + break;
35124 + }
35125 +
35126 + } else {
35127 + switch (st->hcchar_copy.b.multicnt) {
35128 + st->hctsiz_copy.b.xfersize = nrpackets * st->hcchar_copy.b.mps;
35129 + case 1:
35130 + st->hctsiz_copy.b.pid = DWC_PID_DATA0;
35131 + break;
35132 + case 2:
35133 + st->hctsiz_copy.b.pid = DWC_PID_DATA1;
35134 + break;
35135 + case 3:
35136 + st->hctsiz_copy.b.pid = DWC_PID_DATA2;
35137 + break;
35138 + }
35139 + }
35140 + FIQ_WRITE(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ, st->hctsiz_copy.d32);
35141 + FIQ_WRITE(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR, st->hcchar_copy.d32);
35142 + /* Channel is enabled on hcint handler exit */
35143 + fiq_print(FIQDBG_INT, state, "HSISOOUT");
35144 + return 1;
35145 +}
35146 +
35147 +
35148 +/**
35149 + * fiq_fsm_do_sof() - FSM start-of-frame interrupt handler
35150 + * @state: Pointer to the state struct passed from banked FIQ mode registers.
35151 + * @num_channels: set according to the DWC hardware configuration
35152 + *
35153 + * The SOF handler in FSM mode has two functions
35154 + * 1. Hold off SOF from causing schedule advancement in IRQ context if there's
35155 + * nothing to do
35156 + * 2. Advance certain FSM states that require either a microframe delay, or a microframe
35157 + * of holdoff.
35158 + *
35159 + * The second part is architecture-specific to mach-bcm2835 -
35160 + * a sane interrupt controller would have a mask register for ARM interrupt sources
35161 + * to be promoted to the nFIQ line, but it doesn't. Instead a single interrupt
35162 + * number (USB) can be enabled. This means that certain parts of the USB specification
35163 + * that require "wait a little while, then issue another packet" cannot be fulfilled with
35164 + * the timing granularity required to achieve optimal throughout. The workaround is to use
35165 + * the SOF "timer" (125uS) to perform this task.
35166 + */
35167 +static int notrace noinline fiq_fsm_do_sof(struct fiq_state *state, int num_channels)
35168 +{
35169 + hfnum_data_t hfnum = { .d32 = FIQ_READ(state->dwc_regs_base + HFNUM) };
35170 + int n;
35171 + int kick_irq = 0;
35172 +
35173 + if ((hfnum.b.frnum & 0x7) == 1) {
35174 + /* We cannot issue csplits for transactions in the last frame past (n+1).1
35175 + * Check to see if there are any transactions that are stale.
35176 + * Boot them out.
35177 + */
35178 + for (n = 0; n < num_channels; n++) {
35179 + switch (state->channel[n].fsm) {
35180 + case FIQ_PER_CSPLIT_WAIT:
35181 + case FIQ_PER_CSPLIT_NYET1:
35182 + case FIQ_PER_CSPLIT_POLL:
35183 + case FIQ_PER_CSPLIT_LAST:
35184 + /* Check if we are no longer in the same full-speed frame. */
35185 + if (((state->channel[n].expected_uframe & 0x3FFF) & ~0x7) <
35186 + (hfnum.b.frnum & ~0x7))
35187 + state->channel[n].fsm = FIQ_PER_SPLIT_TIMEOUT;
35188 + break;
35189 + default:
35190 + break;
35191 + }
35192 + }
35193 + }
35194 +
35195 + for (n = 0; n < num_channels; n++) {
35196 + switch (state->channel[n].fsm) {
35197 +
35198 + case FIQ_NP_SSPLIT_RETRY:
35199 + case FIQ_NP_IN_CSPLIT_RETRY:
35200 + case FIQ_NP_OUT_CSPLIT_RETRY:
35201 + fiq_fsm_restart_channel(state, n, 0);
35202 + break;
35203 +
35204 + case FIQ_HS_ISOC_SLEEPING:
35205 + /* Is it time to wake this channel yet? */
35206 + if (--state->channel[n].uframe_sleeps == 0) {
35207 + state->channel[n].fsm = FIQ_HS_ISOC_TURBO;
35208 + fiq_fsm_restart_channel(state, n, 0);
35209 + }
35210 + break;
35211 +
35212 + case FIQ_PER_SSPLIT_QUEUED:
35213 + if ((hfnum.b.frnum & 0x7) == 5)
35214 + break;
35215 + if(!fiq_fsm_tt_in_use(state, num_channels, n)) {
35216 + if (!fiq_fsm_too_late(state, n)) {
35217 + fiq_print(FIQDBG_INT, state, "SOF GO %01d", n);
35218 + fiq_fsm_restart_channel(state, n, 0);
35219 + state->channel[n].fsm = FIQ_PER_SSPLIT_STARTED;
35220 + } else {
35221 + /* Transaction cannot be started without risking a device babble error */
35222 + state->channel[n].fsm = FIQ_PER_SPLIT_TIMEOUT;
35223 + state->haintmsk_saved.b2.chint &= ~(1 << n);
35224 + FIQ_WRITE(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCINTMSK, 0);
35225 + kick_irq |= 1;
35226 + }
35227 + }
35228 + break;
35229 +
35230 + case FIQ_PER_ISO_OUT_PENDING:
35231 + /* Ordinarily, this should be poked after the SSPLIT
35232 + * complete interrupt for a competing transfer on the same
35233 + * TT. Doesn't happen for aborted transactions though.
35234 + */
35235 + if ((hfnum.b.frnum & 0x7) >= 5)
35236 + break;
35237 + if (!fiq_fsm_tt_in_use(state, num_channels, n)) {
35238 + /* Hardware bug. SOF can sometimes occur after the channel halt interrupt
35239 + * that caused this.
35240 + */
35241 + fiq_fsm_restart_channel(state, n, 0);
35242 + fiq_print(FIQDBG_INT, state, "SOF ISOC");
35243 + if (state->channel[n].nrpackets == 1) {
35244 + state->channel[n].fsm = FIQ_PER_ISO_OUT_LAST;
35245 + } else {
35246 + state->channel[n].fsm = FIQ_PER_ISO_OUT_ACTIVE;
35247 + }
35248 + }
35249 + break;
35250 +
35251 + case FIQ_PER_CSPLIT_WAIT:
35252 + /* we are guaranteed to be in this state if and only if the SSPLIT interrupt
35253 + * occurred when the bus transaction occurred. The SOF interrupt reversal bug
35254 + * will utterly bugger this up though.
35255 + */
35256 + if (hfnum.b.frnum != state->channel[n].expected_uframe) {
35257 + fiq_print(FIQDBG_INT, state, "SOFCS %d ", n);
35258 + state->channel[n].fsm = FIQ_PER_CSPLIT_POLL;
35259 + fiq_fsm_restart_channel(state, n, 0);
35260 + fiq_fsm_start_next_periodic(state, num_channels);
35261 +
35262 + }
35263 + break;
35264 +
35265 + case FIQ_PER_SPLIT_TIMEOUT:
35266 + case FIQ_DEQUEUE_ISSUED:
35267 + /* Ugly: we have to force a HCD interrupt.
35268 + * Poke the mask for the channel in question.
35269 + * We will take a fake SOF because of this, but
35270 + * that's OK.
35271 + */
35272 + state->haintmsk_saved.b2.chint &= ~(1 << n);
35273 + FIQ_WRITE(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCINTMSK, 0);
35274 + kick_irq |= 1;
35275 + break;
35276 +
35277 + default:
35278 + break;
35279 + }
35280 + }
35281 +
35282 + if (state->kick_np_queues ||
35283 + dwc_frame_num_le(state->next_sched_frame, hfnum.b.frnum))
35284 + kick_irq |= 1;
35285 +
35286 + return !kick_irq;
35287 +}
35288 +
35289 +
35290 +/**
35291 + * fiq_fsm_do_hcintr() - FSM host channel interrupt handler
35292 + * @state: Pointer to the FIQ state struct
35293 + * @num_channels: Number of channels as per hardware config
35294 + * @n: channel for which HAINT(i) was raised
35295 + *
35296 + * An important property is that only the CHHLT interrupt is unmasked. Unfortunately, AHBerr is as well.
35297 + */
35298 +static int notrace noinline fiq_fsm_do_hcintr(struct fiq_state *state, int num_channels, int n)
35299 +{
35300 + hcint_data_t hcint;
35301 + hcintmsk_data_t hcintmsk;
35302 + hcint_data_t hcint_probe;
35303 + hcchar_data_t hcchar;
35304 + int handled = 0;
35305 + int restart = 0;
35306 + int last_csplit = 0;
35307 + int start_next_periodic = 0;
35308 + struct fiq_channel_state *st = &state->channel[n];
35309 + hfnum_data_t hfnum;
35310 +
35311 + hcint.d32 = FIQ_READ(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCINT);
35312 + hcintmsk.d32 = FIQ_READ(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCINTMSK);
35313 + hcint_probe.d32 = hcint.d32 & hcintmsk.d32;
35314 +
35315 + if (st->fsm != FIQ_PASSTHROUGH) {
35316 + fiq_print(FIQDBG_INT, state, "HC%01d ST%02d", n, st->fsm);
35317 + fiq_print(FIQDBG_INT, state, "%08x", hcint.d32);
35318 + }
35319 +
35320 + switch (st->fsm) {
35321 +
35322 + case FIQ_PASSTHROUGH:
35323 + case FIQ_DEQUEUE_ISSUED:
35324 + /* doesn't belong to us, kick it upstairs */
35325 + break;
35326 +
35327 + case FIQ_PASSTHROUGH_ERRORSTATE:
35328 + /* We are here to emulate the error recovery mechanism of the dwc HCD.
35329 + * Several interrupts are unmasked if a previous transaction failed - it's
35330 + * death for the FIQ to attempt to handle them as the channel isn't halted.
35331 + * Emulate what the HCD does in this situation: mask and continue.
35332 + * The FSM has no other state setup so this has to be handled out-of-band.
35333 + */
35334 + fiq_print(FIQDBG_ERR, state, "ERRST %02d", n);
35335 + if (hcint_probe.b.nak || hcint_probe.b.ack || hcint_probe.b.datatglerr) {
35336 + fiq_print(FIQDBG_ERR, state, "RESET %02d", n);
35337 + /* In some random cases we can get a NAK interrupt coincident with a Xacterr
35338 + * interrupt, after the device has disappeared.
35339 + */
35340 + if (!hcint.b.xacterr)
35341 + st->nr_errors = 0;
35342 + hcintmsk.b.nak = 0;
35343 + hcintmsk.b.ack = 0;
35344 + hcintmsk.b.datatglerr = 0;
35345 + FIQ_WRITE(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCINTMSK, hcintmsk.d32);
35346 + return 1;
35347 + }
35348 + if (hcint_probe.b.chhltd) {
35349 + fiq_print(FIQDBG_ERR, state, "CHHLT %02d", n);
35350 + fiq_print(FIQDBG_ERR, state, "%08x", hcint.d32);
35351 + return 0;
35352 + }
35353 + break;
35354 +
35355 + /* Non-periodic state groups */
35356 + case FIQ_NP_SSPLIT_STARTED:
35357 + case FIQ_NP_SSPLIT_RETRY:
35358 + /* Got a HCINT for a NP SSPLIT. Expected ACK / NAK / fail */
35359 + if (hcint.b.ack) {
35360 + /* SSPLIT complete. For OUT, the data has been sent. For IN, the LS transaction
35361 + * will start shortly. SOF needs to kick the transaction to prevent a NYET flood.
35362 + */
35363 + if(st->hcchar_copy.b.epdir == 1)
35364 + st->fsm = FIQ_NP_IN_CSPLIT_RETRY;
35365 + else
35366 + st->fsm = FIQ_NP_OUT_CSPLIT_RETRY;
35367 + st->nr_errors = 0;
35368 + handled = 1;
35369 + fiq_fsm_setup_csplit(state, n);
35370 + } else if (hcint.b.nak) {
35371 + // No buffer space in TT. Retry on a uframe boundary.
35372 + st->fsm = FIQ_NP_SSPLIT_RETRY;
35373 + handled = 1;
35374 + } else if (hcint.b.xacterr) {
35375 + // The only other one we care about is xacterr. This implies HS bus error - retry.
35376 + st->nr_errors++;
35377 + st->fsm = FIQ_NP_SSPLIT_RETRY;
35378 + if (st->nr_errors >= 3) {
35379 + st->fsm = FIQ_NP_SPLIT_HS_ABORTED;
35380 + } else {
35381 + handled = 1;
35382 + restart = 1;
35383 + }
35384 + } else {
35385 + st->fsm = FIQ_NP_SPLIT_LS_ABORTED;
35386 + handled = 0;
35387 + restart = 0;
35388 + }
35389 + break;
35390 +
35391 + case FIQ_NP_IN_CSPLIT_RETRY:
35392 + /* Received a CSPLIT done interrupt.
35393 + * Expected Data/NAK/STALL/NYET for IN.
35394 + */
35395 + if (hcint.b.xfercomp) {
35396 + /* For IN, data is present. */
35397 + st->fsm = FIQ_NP_SPLIT_DONE;
35398 + } else if (hcint.b.nak) {
35399 + /* no endpoint data. Punt it upstairs */
35400 + st->fsm = FIQ_NP_SPLIT_DONE;
35401 + } else if (hcint.b.nyet) {
35402 + /* CSPLIT NYET - retry on a uframe boundary. */
35403 + handled = 1;
35404 + st->nr_errors = 0;
35405 + } else if (hcint.b.datatglerr) {
35406 + /* data toggle errors do not set the xfercomp bit. */
35407 + st->fsm = FIQ_NP_SPLIT_LS_ABORTED;
35408 + } else if (hcint.b.xacterr) {
35409 + /* HS error. Retry immediate */
35410 + st->fsm = FIQ_NP_IN_CSPLIT_RETRY;
35411 + st->nr_errors++;
35412 + if (st->nr_errors >= 3) {
35413 + st->fsm = FIQ_NP_SPLIT_HS_ABORTED;
35414 + } else {
35415 + handled = 1;
35416 + restart = 1;
35417 + }
35418 + } else if (hcint.b.stall || hcint.b.bblerr) {
35419 + /* A STALL implies either a LS bus error or a genuine STALL. */
35420 + st->fsm = FIQ_NP_SPLIT_LS_ABORTED;
35421 + } else {
35422 + /* Hardware bug. It's possible in some cases to
35423 + * get a channel halt with nothing else set when
35424 + * the response was a NYET. Treat as local 3-strikes retry.
35425 + */
35426 + hcint_data_t hcint_test = hcint;
35427 + hcint_test.b.chhltd = 0;
35428 + if (!hcint_test.d32) {
35429 + st->nr_errors++;
35430 + if (st->nr_errors >= 3) {
35431 + st->fsm = FIQ_NP_SPLIT_HS_ABORTED;
35432 + } else {
35433 + handled = 1;
35434 + }
35435 + } else {
35436 + /* Bail out if something unexpected happened */
35437 + st->fsm = FIQ_NP_SPLIT_HS_ABORTED;
35438 + }
35439 + }
35440 + if (st->fsm != FIQ_NP_IN_CSPLIT_RETRY) {
35441 + fiq_fsm_restart_np_pending(state, num_channels, n);
35442 + }
35443 + break;
35444 +
35445 + case FIQ_NP_OUT_CSPLIT_RETRY:
35446 + /* Received a CSPLIT done interrupt.
35447 + * Expected ACK/NAK/STALL/NYET/XFERCOMP for OUT.*/
35448 + if (hcint.b.xfercomp) {
35449 + st->fsm = FIQ_NP_SPLIT_DONE;
35450 + } else if (hcint.b.nak) {
35451 + // The HCD will implement the holdoff on frame boundaries.
35452 + st->fsm = FIQ_NP_SPLIT_DONE;
35453 + } else if (hcint.b.nyet) {
35454 + // Hub still processing.
35455 + st->fsm = FIQ_NP_OUT_CSPLIT_RETRY;
35456 + handled = 1;
35457 + st->nr_errors = 0;
35458 + //restart = 1;
35459 + } else if (hcint.b.xacterr) {
35460 + /* HS error. retry immediate */
35461 + st->fsm = FIQ_NP_OUT_CSPLIT_RETRY;
35462 + st->nr_errors++;
35463 + if (st->nr_errors >= 3) {
35464 + st->fsm = FIQ_NP_SPLIT_HS_ABORTED;
35465 + } else {
35466 + handled = 1;
35467 + restart = 1;
35468 + }
35469 + } else if (hcint.b.stall) {
35470 + /* LS bus error or genuine stall */
35471 + st->fsm = FIQ_NP_SPLIT_LS_ABORTED;
35472 + } else {
35473 + /*
35474 + * Hardware bug. It's possible in some cases to get a
35475 + * channel halt with nothing else set when the response was a NYET.
35476 + * Treat as local 3-strikes retry.
35477 + */
35478 + hcint_data_t hcint_test = hcint;
35479 + hcint_test.b.chhltd = 0;
35480 + if (!hcint_test.d32) {
35481 + st->nr_errors++;
35482 + if (st->nr_errors >= 3) {
35483 + st->fsm = FIQ_NP_SPLIT_HS_ABORTED;
35484 + } else {
35485 + handled = 1;
35486 + }
35487 + } else {
35488 + // Something unexpected happened. AHBerror or babble perhaps. Let the IRQ deal with it.
35489 + st->fsm = FIQ_NP_SPLIT_HS_ABORTED;
35490 + }
35491 + }
35492 + if (st->fsm != FIQ_NP_OUT_CSPLIT_RETRY) {
35493 + fiq_fsm_restart_np_pending(state, num_channels, n);
35494 + }
35495 + break;
35496 +
35497 + /* Periodic split states (except isoc out) */
35498 + case FIQ_PER_SSPLIT_STARTED:
35499 + /* Expect an ACK or failure for SSPLIT */
35500 + if (hcint.b.ack) {
35501 + /*
35502 + * SSPLIT transfer complete interrupt - the generation of this interrupt is fraught with bugs.
35503 + * For a packet queued in microframe n-3 to appear in n-2, if the channel is enabled near the EOF1
35504 + * point for microframe n-3, the packet will not appear on the bus until microframe n.
35505 + * Additionally, the generation of the actual interrupt is dodgy. For a packet appearing on the bus
35506 + * in microframe n, sometimes the interrupt is generated immediately. Sometimes, it appears in n+1
35507 + * coincident with SOF for n+1.
35508 + * SOF is also buggy. It can sometimes be raised AFTER the first bus transaction has taken place.
35509 + * These appear to be caused by timing/clock crossing bugs within the core itself.
35510 + * State machine workaround.
35511 + */
35512 + hfnum.d32 = FIQ_READ(state->dwc_regs_base + HFNUM);
35513 + hcchar.d32 = FIQ_READ(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR);
35514 + fiq_fsm_setup_csplit(state, n);
35515 + /* Poke the oddfrm bit. If we are equivalent, we received the interrupt at the correct
35516 + * time. If not, then we're in the next SOF.
35517 + */
35518 + if ((hfnum.b.frnum & 0x1) == hcchar.b.oddfrm) {
35519 + fiq_print(FIQDBG_INT, state, "CSWAIT %01d", n);
35520 + st->expected_uframe = hfnum.b.frnum;
35521 + st->fsm = FIQ_PER_CSPLIT_WAIT;
35522 + } else {
35523 + fiq_print(FIQDBG_INT, state, "CSPOL %01d", n);
35524 + /* For isochronous IN endpoints,
35525 + * we need to hold off if we are expecting a lot of data */
35526 + if (st->hcchar_copy.b.mps < DATA0_PID_HEURISTIC) {
35527 + start_next_periodic = 1;
35528 + }
35529 + /* Danger will robinson: we are in a broken state. If our first interrupt after
35530 + * this is a NYET, it will be delayed by 1 uframe and result in an unrecoverable
35531 + * lag. Unmask the NYET interrupt.
35532 + */
35533 + st->expected_uframe = (hfnum.b.frnum + 1) & 0x3FFF;
35534 + st->fsm = FIQ_PER_CSPLIT_BROKEN_NYET1;
35535 + restart = 1;
35536 + }
35537 + handled = 1;
35538 + } else if (hcint.b.xacterr) {
35539 + /* 3-strikes retry is enabled, we have hit our max nr_errors */
35540 + st->fsm = FIQ_PER_SPLIT_HS_ABORTED;
35541 + start_next_periodic = 1;
35542 + } else {
35543 + st->fsm = FIQ_PER_SPLIT_HS_ABORTED;
35544 + start_next_periodic = 1;
35545 + }
35546 + /* We can now queue the next isochronous OUT transaction, if one is pending. */
35547 + if(fiq_fsm_tt_next_isoc(state, num_channels, n)) {
35548 + fiq_print(FIQDBG_INT, state, "NEXTISO ");
35549 + }
35550 + break;
35551 +
35552 + case FIQ_PER_CSPLIT_NYET1:
35553 + /* First CSPLIT attempt was a NYET. If we get a subsequent NYET,
35554 + * we are too late and the TT has dropped its CSPLIT fifo.
35555 + */
35556 + hfnum.d32 = FIQ_READ(state->dwc_regs_base + HFNUM);
35557 + hcchar.d32 = FIQ_READ(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR);
35558 + start_next_periodic = 1;
35559 + if (hcint.b.nak) {
35560 + st->fsm = FIQ_PER_SPLIT_DONE;
35561 + } else if (hcint.b.xfercomp) {
35562 + fiq_increment_dma_buf(state, num_channels, n);
35563 + st->fsm = FIQ_PER_CSPLIT_POLL;
35564 + st->nr_errors = 0;
35565 + if (fiq_fsm_more_csplits(state, n, &last_csplit)) {
35566 + handled = 1;
35567 + restart = 1;
35568 + if (!last_csplit)
35569 + start_next_periodic = 0;
35570 + } else {
35571 + st->fsm = FIQ_PER_SPLIT_DONE;
35572 + }
35573 + } else if (hcint.b.nyet) {
35574 + /* Doh. Data lost. */
35575 + st->fsm = FIQ_PER_SPLIT_NYET_ABORTED;
35576 + } else if (hcint.b.xacterr || hcint.b.stall || hcint.b.bblerr) {
35577 + st->fsm = FIQ_PER_SPLIT_LS_ABORTED;
35578 + } else {
35579 + st->fsm = FIQ_PER_SPLIT_HS_ABORTED;
35580 + }
35581 + break;
35582 +
35583 + case FIQ_PER_CSPLIT_BROKEN_NYET1:
35584 + /*
35585 + * we got here because our host channel is in the delayed-interrupt
35586 + * state and we cannot take a NYET interrupt any later than when it
35587 + * occurred. Disable then re-enable the channel if this happens to force
35588 + * CSPLITs to occur at the right time.
35589 + */
35590 + hfnum.d32 = FIQ_READ(state->dwc_regs_base + HFNUM);
35591 + hcchar.d32 = FIQ_READ(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR);
35592 + fiq_print(FIQDBG_INT, state, "BROK: %01d ", n);
35593 + if (hcint.b.nak) {
35594 + st->fsm = FIQ_PER_SPLIT_DONE;
35595 + start_next_periodic = 1;
35596 + } else if (hcint.b.xfercomp) {
35597 + fiq_increment_dma_buf(state, num_channels, n);
35598 + if (fiq_fsm_more_csplits(state, n, &last_csplit)) {
35599 + st->fsm = FIQ_PER_CSPLIT_POLL;
35600 + handled = 1;
35601 + restart = 1;
35602 + start_next_periodic = 1;
35603 + /* Reload HCTSIZ for the next transfer */
35604 + fiq_fsm_reload_hctsiz(state, n);
35605 + if (!last_csplit)
35606 + start_next_periodic = 0;
35607 + } else {
35608 + st->fsm = FIQ_PER_SPLIT_DONE;
35609 + }
35610 + } else if (hcint.b.nyet) {
35611 + st->fsm = FIQ_PER_SPLIT_NYET_ABORTED;
35612 + start_next_periodic = 1;
35613 + } else if (hcint.b.xacterr || hcint.b.stall || hcint.b.bblerr) {
35614 + /* Local 3-strikes retry is handled by the core. This is a ERR response.*/
35615 + st->fsm = FIQ_PER_SPLIT_LS_ABORTED;
35616 + } else {
35617 + st->fsm = FIQ_PER_SPLIT_HS_ABORTED;
35618 + }
35619 + break;
35620 +
35621 + case FIQ_PER_CSPLIT_POLL:
35622 + hfnum.d32 = FIQ_READ(state->dwc_regs_base + HFNUM);
35623 + hcchar.d32 = FIQ_READ(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR);
35624 + start_next_periodic = 1;
35625 + if (hcint.b.nak) {
35626 + st->fsm = FIQ_PER_SPLIT_DONE;
35627 + } else if (hcint.b.xfercomp) {
35628 + fiq_increment_dma_buf(state, num_channels, n);
35629 + if (fiq_fsm_more_csplits(state, n, &last_csplit)) {
35630 + handled = 1;
35631 + restart = 1;
35632 + /* Reload HCTSIZ for the next transfer */
35633 + fiq_fsm_reload_hctsiz(state, n);
35634 + if (!last_csplit)
35635 + start_next_periodic = 0;
35636 + } else {
35637 + st->fsm = FIQ_PER_SPLIT_DONE;
35638 + }
35639 + } else if (hcint.b.nyet) {
35640 + /* Are we a NYET after the first data packet? */
35641 + if (st->nrpackets == 0) {
35642 + st->fsm = FIQ_PER_CSPLIT_NYET1;
35643 + handled = 1;
35644 + restart = 1;
35645 + } else {
35646 + /* We got a NYET when polling CSPLITs. Can happen
35647 + * if our heuristic fails, or if someone disables us
35648 + * for any significant length of time.
35649 + */
35650 + if (st->nr_errors >= 3) {
35651 + st->fsm = FIQ_PER_SPLIT_NYET_ABORTED;
35652 + } else {
35653 + st->fsm = FIQ_PER_SPLIT_DONE;
35654 + }
35655 + }
35656 + } else if (hcint.b.xacterr || hcint.b.stall || hcint.b.bblerr) {
35657 + /* For xacterr, Local 3-strikes retry is handled by the core. This is a ERR response.*/
35658 + st->fsm = FIQ_PER_SPLIT_LS_ABORTED;
35659 + } else {
35660 + st->fsm = FIQ_PER_SPLIT_HS_ABORTED;
35661 + }
35662 + break;
35663 +
35664 + case FIQ_HS_ISOC_TURBO:
35665 + if (fiq_fsm_update_hs_isoc(state, n, hcint)) {
35666 + /* more transactions to come */
35667 + handled = 1;
35668 + fiq_print(FIQDBG_INT, state, "HSISO M ");
35669 + /* For strided transfers, put ourselves to sleep */
35670 + if (st->hs_isoc_info.stride > 1) {
35671 + st->uframe_sleeps = st->hs_isoc_info.stride - 1;
35672 + st->fsm = FIQ_HS_ISOC_SLEEPING;
35673 + } else {
35674 + restart = 1;
35675 + }
35676 + } else {
35677 + st->fsm = FIQ_HS_ISOC_DONE;
35678 + fiq_print(FIQDBG_INT, state, "HSISO F ");
35679 + }
35680 + break;
35681 +
35682 + case FIQ_HS_ISOC_ABORTED:
35683 + /* This abort is called by the driver rewriting the state mid-transaction
35684 + * which allows the dequeue mechanism to work more effectively.
35685 + */
35686 + break;
35687 +
35688 + case FIQ_PER_ISO_OUT_ACTIVE:
35689 + if (hcint.b.ack) {
35690 + if(fiq_iso_out_advance(state, num_channels, n)) {
35691 + /* last OUT transfer */
35692 + st->fsm = FIQ_PER_ISO_OUT_LAST;
35693 + /*
35694 + * Assuming the periodic FIFO in the dwc core
35695 + * actually does its job properly, we can queue
35696 + * the next ssplit now and in theory, the wire
35697 + * transactions will be in-order.
35698 + */
35699 + // No it doesn't. It appears to process requests in host channel order.
35700 + //start_next_periodic = 1;
35701 + }
35702 + handled = 1;
35703 + restart = 1;
35704 + } else {
35705 + /*
35706 + * Isochronous transactions carry on regardless. Log the error
35707 + * and continue.
35708 + */
35709 + //explode += 1;
35710 + st->nr_errors++;
35711 + if(fiq_iso_out_advance(state, num_channels, n)) {
35712 + st->fsm = FIQ_PER_ISO_OUT_LAST;
35713 + //start_next_periodic = 1;
35714 + }
35715 + handled = 1;
35716 + restart = 1;
35717 + }
35718 + break;
35719 +
35720 + case FIQ_PER_ISO_OUT_LAST:
35721 + if (hcint.b.ack) {
35722 + /* All done here */
35723 + st->fsm = FIQ_PER_ISO_OUT_DONE;
35724 + } else {
35725 + st->fsm = FIQ_PER_ISO_OUT_DONE;
35726 + st->nr_errors++;
35727 + }
35728 + start_next_periodic = 1;
35729 + break;
35730 +
35731 + case FIQ_PER_SPLIT_TIMEOUT:
35732 + /* SOF kicked us because we overran. */
35733 + start_next_periodic = 1;
35734 + break;
35735 +
35736 + default:
35737 + break;
35738 + }
35739 +
35740 + if (handled) {
35741 + FIQ_WRITE(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCINT, hcint.d32);
35742 + } else {
35743 + /* Copy the regs into the state so the IRQ knows what to do */
35744 + st->hcint_copy.d32 = hcint.d32;
35745 + }
35746 +
35747 + if (restart) {
35748 + /* Restart always implies handled. */
35749 + if (restart == 2) {
35750 + /* For complete-split INs, the show must go on.
35751 + * Force a channel restart */
35752 + fiq_fsm_restart_channel(state, n, 1);
35753 + } else {
35754 + fiq_fsm_restart_channel(state, n, 0);
35755 + }
35756 + }
35757 + if (start_next_periodic) {
35758 + fiq_fsm_start_next_periodic(state, num_channels);
35759 + }
35760 + if (st->fsm != FIQ_PASSTHROUGH)
35761 + fiq_print(FIQDBG_INT, state, "FSMOUT%02d", st->fsm);
35762 +
35763 + return handled;
35764 +}
35765 +
35766 +
35767 +/**
35768 + * dwc_otg_fiq_fsm() - Flying State Machine (monster) FIQ
35769 + * @state: pointer to state struct passed from the banked FIQ mode registers.
35770 + * @num_channels: set according to the DWC hardware configuration
35771 + * @dma: pointer to DMA bounce buffers for split transaction slots
35772 + *
35773 + * The FSM FIQ performs the low-level tasks that normally would be performed by the microcode
35774 + * inside an EHCI or similar host controller regarding split transactions. The DWC core
35775 + * interrupts each and every time a split transaction packet is received or sent successfully.
35776 + * This results in either an interrupt storm when everything is working "properly", or
35777 + * the interrupt latency of the system in general breaks time-sensitive periodic split
35778 + * transactions. Pushing the low-level, but relatively easy state machine work into the FIQ
35779 + * solves these problems.
35780 + *
35781 + * Return: void
35782 + */
35783 +void notrace dwc_otg_fiq_fsm(struct fiq_state *state, int num_channels)
35784 +{
35785 + gintsts_data_t gintsts, gintsts_handled;
35786 + gintmsk_data_t gintmsk;
35787 + //hfnum_data_t hfnum;
35788 + haint_data_t haint, haint_handled;
35789 + haintmsk_data_t haintmsk;
35790 + int kick_irq = 0;
35791 +
35792 + gintsts_handled.d32 = 0;
35793 + haint_handled.d32 = 0;
35794 +
35795 + fiq_fsm_spin_lock(&state->lock);
35796 + gintsts.d32 = FIQ_READ(state->dwc_regs_base + GINTSTS);
35797 + gintmsk.d32 = FIQ_READ(state->dwc_regs_base + GINTMSK);
35798 + gintsts.d32 &= gintmsk.d32;
35799 +
35800 + if (gintsts.b.sofintr) {
35801 + /* For FSM mode, SOF is required to keep the state machine advance for
35802 + * certain stages of the periodic pipeline. It's death to mask this
35803 + * interrupt in that case.
35804 + */
35805 +
35806 + if (!fiq_fsm_do_sof(state, num_channels)) {
35807 + /* Kick IRQ once. Queue advancement means that all pending transactions
35808 + * will get serviced when the IRQ finally executes.
35809 + */
35810 + if (state->gintmsk_saved.b.sofintr == 1)
35811 + kick_irq |= 1;
35812 + state->gintmsk_saved.b.sofintr = 0;
35813 + }
35814 + gintsts_handled.b.sofintr = 1;
35815 + }
35816 +
35817 + if (gintsts.b.hcintr) {
35818 + int i;
35819 + haint.d32 = FIQ_READ(state->dwc_regs_base + HAINT);
35820 + haintmsk.d32 = FIQ_READ(state->dwc_regs_base + HAINTMSK);
35821 + haint.d32 &= haintmsk.d32;
35822 + haint_handled.d32 = 0;
35823 + for (i=0; i<num_channels; i++) {
35824 + if (haint.b2.chint & (1 << i)) {
35825 + if(!fiq_fsm_do_hcintr(state, num_channels, i)) {
35826 + /* HCINT was not handled in FIQ
35827 + * HAINT is level-sensitive, leading to level-sensitive ginststs.b.hcint bit.
35828 + * Mask HAINT(i) but keep top-level hcint unmasked.
35829 + */
35830 + state->haintmsk_saved.b2.chint &= ~(1 << i);
35831 + } else {
35832 + /* do_hcintr cleaned up after itself, but clear haint */
35833 + haint_handled.b2.chint |= (1 << i);
35834 + }
35835 + }
35836 + }
35837 +
35838 + if (haint_handled.b2.chint) {
35839 + FIQ_WRITE(state->dwc_regs_base + HAINT, haint_handled.d32);
35840 + }
35841 +
35842 + if (haintmsk.d32 != (haintmsk.d32 & state->haintmsk_saved.d32)) {
35843 + /*
35844 + * This is necessary to avoid multiple retriggers of the MPHI in the case
35845 + * where interrupts are held off and HCINTs start to pile up.
35846 + * Only wake up the IRQ if a new interrupt came in, was not handled and was
35847 + * masked.
35848 + */
35849 + haintmsk.d32 &= state->haintmsk_saved.d32;
35850 + FIQ_WRITE(state->dwc_regs_base + HAINTMSK, haintmsk.d32);
35851 + kick_irq |= 1;
35852 + }
35853 + /* Top-Level interrupt - always handled because it's level-sensitive */
35854 + gintsts_handled.b.hcintr = 1;
35855 + }
35856 +
35857 +
35858 + /* Clear the bits in the saved register that were not handled but were triggered. */
35859 + state->gintmsk_saved.d32 &= ~(gintsts.d32 & ~gintsts_handled.d32);
35860 +
35861 + /* FIQ didn't handle something - mask has changed - write new mask */
35862 + if (gintmsk.d32 != (gintmsk.d32 & state->gintmsk_saved.d32)) {
35863 + gintmsk.d32 &= state->gintmsk_saved.d32;
35864 + gintmsk.b.sofintr = 1;
35865 + FIQ_WRITE(state->dwc_regs_base + GINTMSK, gintmsk.d32);
35866 +// fiq_print(FIQDBG_INT, state, "KICKGINT");
35867 +// fiq_print(FIQDBG_INT, state, "%08x", gintmsk.d32);
35868 +// fiq_print(FIQDBG_INT, state, "%08x", state->gintmsk_saved.d32);
35869 + kick_irq |= 1;
35870 + }
35871 +
35872 + if (gintsts_handled.d32) {
35873 + /* Only applies to edge-sensitive bits in GINTSTS */
35874 + FIQ_WRITE(state->dwc_regs_base + GINTSTS, gintsts_handled.d32);
35875 + }
35876 +
35877 + /* We got an interrupt, didn't handle it. */
35878 + if (kick_irq) {
35879 + state->mphi_int_count++;
35880 + FIQ_WRITE(state->mphi_regs.outdda, (int) state->dummy_send);
35881 + FIQ_WRITE(state->mphi_regs.outddb, (1<<29));
35882 +
35883 + }
35884 + state->fiq_done++;
35885 + mb();
35886 + fiq_fsm_spin_unlock(&state->lock);
35887 +}
35888 +
35889 +
35890 +/**
35891 + * dwc_otg_fiq_nop() - FIQ "lite"
35892 + * @state: pointer to state struct passed from the banked FIQ mode registers.
35893 + *
35894 + * The "nop" handler does not intervene on any interrupts other than SOF.
35895 + * It is limited in scope to deciding at each SOF if the IRQ SOF handler (which deals
35896 + * with non-periodic/periodic queues) needs to be kicked.
35897 + *
35898 + * This is done to hold off the SOF interrupt, which occurs at a rate of 8000 per second.
35899 + *
35900 + * Return: void
35901 + */
35902 +void notrace dwc_otg_fiq_nop(struct fiq_state *state)
35903 +{
35904 + gintsts_data_t gintsts, gintsts_handled;
35905 + gintmsk_data_t gintmsk;
35906 + hfnum_data_t hfnum;
35907 +
35908 + fiq_fsm_spin_lock(&state->lock);
35909 + hfnum.d32 = FIQ_READ(state->dwc_regs_base + HFNUM);
35910 + gintsts.d32 = FIQ_READ(state->dwc_regs_base + GINTSTS);
35911 + gintmsk.d32 = FIQ_READ(state->dwc_regs_base + GINTMSK);
35912 + gintsts.d32 &= gintmsk.d32;
35913 + gintsts_handled.d32 = 0;
35914 +
35915 + if (gintsts.b.sofintr) {
35916 + if (!state->kick_np_queues &&
35917 + dwc_frame_num_gt(state->next_sched_frame, hfnum.b.frnum)) {
35918 + /* SOF handled, no work to do, just ACK interrupt */
35919 + gintsts_handled.b.sofintr = 1;
35920 + } else {
35921 + /* Kick IRQ */
35922 + state->gintmsk_saved.b.sofintr = 0;
35923 + }
35924 + }
35925 +
35926 + /* Reset handled interrupts */
35927 + if(gintsts_handled.d32) {
35928 + FIQ_WRITE(state->dwc_regs_base + GINTSTS, gintsts_handled.d32);
35929 + }
35930 +
35931 + /* Clear the bits in the saved register that were not handled but were triggered. */
35932 + state->gintmsk_saved.d32 &= ~(gintsts.d32 & ~gintsts_handled.d32);
35933 +
35934 + /* We got an interrupt, didn't handle it and want to mask it */
35935 + if (~(state->gintmsk_saved.d32)) {
35936 + state->mphi_int_count++;
35937 + gintmsk.d32 &= state->gintmsk_saved.d32;
35938 + FIQ_WRITE(state->dwc_regs_base + GINTMSK, gintmsk.d32);
35939 + /* Force a clear before another dummy send */
35940 + FIQ_WRITE(state->mphi_regs.intstat, (1<<29));
35941 + FIQ_WRITE(state->mphi_regs.outdda, (int) state->dummy_send);
35942 + FIQ_WRITE(state->mphi_regs.outddb, (1<<29));
35943 +
35944 + }
35945 + state->fiq_done++;
35946 + mb();
35947 + fiq_fsm_spin_unlock(&state->lock);
35948 +}
35949 --- /dev/null
35950 +++ b/drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.h
35951 @@ -0,0 +1,372 @@
35952 +/*
35953 + * dwc_otg_fiq_fsm.h - Finite state machine FIQ header definitions
35954 + *
35955 + * Copyright (c) 2013 Raspberry Pi Foundation
35956 + *
35957 + * Author: Jonathan Bell <jonathan@raspberrypi.org>
35958 + * All rights reserved.
35959 + *
35960 + * Redistribution and use in source and binary forms, with or without
35961 + * modification, are permitted provided that the following conditions are met:
35962 + * * Redistributions of source code must retain the above copyright
35963 + * notice, this list of conditions and the following disclaimer.
35964 + * * Redistributions in binary form must reproduce the above copyright
35965 + * notice, this list of conditions and the following disclaimer in the
35966 + * documentation and/or other materials provided with the distribution.
35967 + * * Neither the name of Raspberry Pi nor the
35968 + * names of its contributors may be used to endorse or promote products
35969 + * derived from this software without specific prior written permission.
35970 + *
35971 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
35972 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
35973 + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
35974 + * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
35975 + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
35976 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
35977 + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35978 + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35979 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35980 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35981 + *
35982 + * This FIQ implements functionality that performs split transactions on
35983 + * the dwc_otg hardware without any outside intervention. A split transaction
35984 + * is "queued" by nominating a specific host channel to perform the entirety
35985 + * of a split transaction. This FIQ will then perform the microframe-precise
35986 + * scheduling required in each phase of the transaction until completion.
35987 + *
35988 + * The FIQ functionality has been surgically implanted into the Synopsys
35989 + * vendor-provided driver.
35990 + *
35991 + */
35992 +
35993 +#ifndef DWC_OTG_FIQ_FSM_H_
35994 +#define DWC_OTG_FIQ_FSM_H_
35995 +
35996 +#include "dwc_otg_regs.h"
35997 +#include "dwc_otg_cil.h"
35998 +#include "dwc_otg_hcd.h"
35999 +#include <linux/kernel.h>
36000 +#include <linux/irqflags.h>
36001 +#include <linux/string.h>
36002 +#include <asm/barrier.h>
36003 +
36004 +#if 0
36005 +#define FLAME_ON(x) \
36006 +do { \
36007 + int gpioreg; \
36008 + \
36009 + gpioreg = readl(__io_address(0x20200000+0x8)); \
36010 + gpioreg &= ~(7 << (x-20)*3); \
36011 + gpioreg |= 0x1 << (x-20)*3; \
36012 + writel(gpioreg, __io_address(0x20200000+0x8)); \
36013 + \
36014 + writel(1<<x, __io_address(0x20200000+(0x1C))); \
36015 +} while (0)
36016 +
36017 +#define FLAME_OFF(x) \
36018 +do { \
36019 + writel(1<<x, __io_address(0x20200000+(0x28))); \
36020 +} while (0)
36021 +#else
36022 +#define FLAME_ON(x) do { } while (0)
36023 +#define FLAME_OFF(X) do { } while (0)
36024 +#endif
36025 +
36026 +/* This is a quick-and-dirty arch-specific register read/write. We know that
36027 + * writes to a peripheral on BCM2835 will always arrive in-order, also that
36028 + * reads and writes are executed in-order therefore the need for memory barriers
36029 + * is obviated if we're only talking to USB.
36030 + */
36031 +#define FIQ_WRITE(_addr_,_data_) (*(volatile unsigned int *) (_addr_) = (_data_))
36032 +#define FIQ_READ(_addr_) (*(volatile unsigned int *) (_addr_))
36033 +
36034 +/* FIQ-ified register definitions. Offsets are from dwc_regs_base. */
36035 +#define GINTSTS 0x014
36036 +#define GINTMSK 0x018
36037 +/* Debug register. Poll the top of the received packets FIFO. */
36038 +#define GRXSTSR 0x01C
36039 +#define HFNUM 0x408
36040 +#define HAINT 0x414
36041 +#define HAINTMSK 0x418
36042 +#define HPRT0 0x440
36043 +
36044 +/* HC_regs start from an offset of 0x500 */
36045 +#define HC_START 0x500
36046 +#define HC_OFFSET 0x020
36047 +
36048 +#define HC_DMA 0x514
36049 +
36050 +#define HCCHAR 0x00
36051 +#define HCSPLT 0x04
36052 +#define HCINT 0x08
36053 +#define HCINTMSK 0x0C
36054 +#define HCTSIZ 0x10
36055 +
36056 +#define ISOC_XACTPOS_ALL 0b11
36057 +#define ISOC_XACTPOS_BEGIN 0b10
36058 +#define ISOC_XACTPOS_MID 0b00
36059 +#define ISOC_XACTPOS_END 0b01
36060 +
36061 +#define DWC_PID_DATA2 0b01
36062 +#define DWC_PID_MDATA 0b11
36063 +#define DWC_PID_DATA1 0b10
36064 +#define DWC_PID_DATA0 0b00
36065 +
36066 +typedef struct {
36067 + volatile void* base;
36068 + volatile void* ctrl;
36069 + volatile void* outdda;
36070 + volatile void* outddb;
36071 + volatile void* intstat;
36072 +} mphi_regs_t;
36073 +
36074 +enum fiq_debug_level {
36075 + FIQDBG_SCHED = (1 << 0),
36076 + FIQDBG_INT = (1 << 1),
36077 + FIQDBG_ERR = (1 << 2),
36078 + FIQDBG_PORTHUB = (1 << 3),
36079 +};
36080 +
36081 +typedef struct {
36082 + union {
36083 + uint32_t slock;
36084 + struct _tickets {
36085 + uint16_t owner;
36086 + uint16_t next;
36087 + } tickets;
36088 + };
36089 +} fiq_lock_t;
36090 +
36091 +struct fiq_state;
36092 +
36093 +extern void _fiq_print (enum fiq_debug_level dbg_lvl, volatile struct fiq_state *state, char *fmt, ...);
36094 +#if 0
36095 +#define fiq_print _fiq_print
36096 +#else
36097 +#define fiq_print(x, y, ...)
36098 +#endif
36099 +
36100 +extern bool fiq_enable, fiq_fsm_enable;
36101 +extern ushort nak_holdoff;
36102 +
36103 +/**
36104 + * enum fiq_fsm_state - The FIQ FSM states.
36105 + *
36106 + * This is the "core" of the FIQ FSM. Broadly, the FSM states follow the
36107 + * USB2.0 specification for host responses to various transaction states.
36108 + * There are modifications to this host state machine because of a variety of
36109 + * quirks and limitations in the dwc_otg hardware.
36110 + *
36111 + * The fsm state is also used to communicate back to the driver on completion of
36112 + * a split transaction. The end states are used in conjunction with the interrupts
36113 + * raised by the final transaction.
36114 + */
36115 +enum fiq_fsm_state {
36116 + /* FIQ isn't enabled for this host channel */
36117 + FIQ_PASSTHROUGH = 0,
36118 + /* For the first interrupt received for this channel,
36119 + * the FIQ has to ack any interrupts indicating success. */
36120 + FIQ_PASSTHROUGH_ERRORSTATE = 31,
36121 + /* Nonperiodic state groups */
36122 + FIQ_NP_SSPLIT_STARTED = 1,
36123 + FIQ_NP_SSPLIT_RETRY = 2,
36124 + /* TT contention - working around hub bugs */
36125 + FIQ_NP_SSPLIT_PENDING = 33,
36126 + FIQ_NP_OUT_CSPLIT_RETRY = 3,
36127 + FIQ_NP_IN_CSPLIT_RETRY = 4,
36128 + FIQ_NP_SPLIT_DONE = 5,
36129 + FIQ_NP_SPLIT_LS_ABORTED = 6,
36130 + /* This differentiates a HS transaction error from a LS one
36131 + * (handling the hub state is different) */
36132 + FIQ_NP_SPLIT_HS_ABORTED = 7,
36133 +
36134 + /* Periodic state groups */
36135 + /* Periodic transactions are either started directly by the IRQ handler
36136 + * or deferred if the TT is already in use.
36137 + */
36138 + FIQ_PER_SSPLIT_QUEUED = 8,
36139 + FIQ_PER_SSPLIT_STARTED = 9,
36140 + FIQ_PER_SSPLIT_LAST = 10,
36141 +
36142 +
36143 + FIQ_PER_ISO_OUT_PENDING = 11,
36144 + FIQ_PER_ISO_OUT_ACTIVE = 12,
36145 + FIQ_PER_ISO_OUT_LAST = 13,
36146 + FIQ_PER_ISO_OUT_DONE = 27,
36147 +
36148 + FIQ_PER_CSPLIT_WAIT = 14,
36149 + FIQ_PER_CSPLIT_NYET1 = 15,
36150 + FIQ_PER_CSPLIT_BROKEN_NYET1 = 28,
36151 + FIQ_PER_CSPLIT_NYET_FAFF = 29,
36152 + /* For multiple CSPLITs (large isoc IN, or delayed interrupt) */
36153 + FIQ_PER_CSPLIT_POLL = 16,
36154 + /* The last CSPLIT for a transaction has been issued, differentiates
36155 + * for the state machine to queue the next packet.
36156 + */
36157 + FIQ_PER_CSPLIT_LAST = 17,
36158 +
36159 + FIQ_PER_SPLIT_DONE = 18,
36160 + FIQ_PER_SPLIT_LS_ABORTED = 19,
36161 + FIQ_PER_SPLIT_HS_ABORTED = 20,
36162 + FIQ_PER_SPLIT_NYET_ABORTED = 21,
36163 + /* Frame rollover has occurred without the transaction finishing. */
36164 + FIQ_PER_SPLIT_TIMEOUT = 22,
36165 +
36166 + /* FIQ-accelerated HS Isochronous state groups */
36167 + FIQ_HS_ISOC_TURBO = 23,
36168 + /* For interval > 1, SOF wakes up the isochronous FSM */
36169 + FIQ_HS_ISOC_SLEEPING = 24,
36170 + FIQ_HS_ISOC_DONE = 25,
36171 + FIQ_HS_ISOC_ABORTED = 26,
36172 + FIQ_DEQUEUE_ISSUED = 30,
36173 + FIQ_TEST = 32,
36174 +};
36175 +
36176 +struct fiq_stack {
36177 + int magic1;
36178 + uint8_t stack[2048];
36179 + int magic2;
36180 +};
36181 +
36182 +
36183 +/**
36184 + * struct fiq_dma_info - DMA bounce buffer utilisation information (per-channel)
36185 + * @index: Number of slots reported used for IN transactions / number of slots
36186 + * transmitted for an OUT transaction
36187 + * @slot_len[6]: Number of actual transfer bytes in each slot (255 if unused)
36188 + *
36189 + * Split transaction transfers can have variable length depending on other bus
36190 + * traffic. The OTG core DMA engine requires 4-byte aligned addresses therefore
36191 + * each transaction needs a guaranteed aligned address. A maximum of 6 split transfers
36192 + * can happen per-frame.
36193 + */
36194 +struct fiq_dma_info {
36195 + u8 index;
36196 + u8 slot_len[6];
36197 +};
36198 +
36199 +struct __attribute__((packed)) fiq_split_dma_slot {
36200 + u8 buf[188];
36201 +};
36202 +
36203 +struct fiq_dma_channel {
36204 + struct __attribute__((packed)) fiq_split_dma_slot index[6];
36205 +};
36206 +
36207 +struct fiq_dma_blob {
36208 + struct __attribute__((packed)) fiq_dma_channel channel[0];
36209 +};
36210 +
36211 +/**
36212 + * struct fiq_hs_isoc_info - USB2.0 isochronous data
36213 + * @iso_frame: Pointer to the array of OTG URB iso_frame_descs.
36214 + * @nrframes: Total length of iso_frame_desc array
36215 + * @index: Current index (FIQ-maintained)
36216 + * @stride: Interval in uframes between HS isoc transactions
36217 + */
36218 +struct fiq_hs_isoc_info {
36219 + struct dwc_otg_hcd_iso_packet_desc *iso_desc;
36220 + unsigned int nrframes;
36221 + unsigned int index;
36222 + unsigned int stride;
36223 +};
36224 +
36225 +/**
36226 + * struct fiq_channel_state - FIQ state machine storage
36227 + * @fsm: Current state of the channel as understood by the FIQ
36228 + * @nr_errors: Number of transaction errors on this split-transaction
36229 + * @hub_addr: SSPLIT/CSPLIT destination hub
36230 + * @port_addr: SSPLIT/CSPLIT destination port - always 1 if single TT hub
36231 + * @nrpackets: For isoc OUT, the number of split-OUT packets to transmit. For
36232 + * split-IN, number of CSPLIT data packets that were received.
36233 + * @hcchar_copy:
36234 + * @hcsplt_copy:
36235 + * @hcintmsk_copy:
36236 + * @hctsiz_copy: Copies of the host channel registers.
36237 + * For use as scratch, or for returning state.
36238 + *
36239 + * The fiq_channel_state is state storage between interrupts for a host channel. The
36240 + * FSM state is stored here. Members of this structure must only be set up by the
36241 + * driver prior to enabling the FIQ for this host channel, and not touched until the FIQ
36242 + * has updated the state to either a COMPLETE state group or ABORT state group.
36243 + */
36244 +
36245 +struct fiq_channel_state {
36246 + enum fiq_fsm_state fsm;
36247 + unsigned int nr_errors;
36248 + unsigned int hub_addr;
36249 + unsigned int port_addr;
36250 + /* Hardware bug workaround: sometimes channel halt interrupts are
36251 + * delayed until the next SOF. Keep track of when we expected to get interrupted. */
36252 + unsigned int expected_uframe;
36253 + /* number of uframes remaining (for interval > 1 HS isoc transfers) before next transfer */
36254 + unsigned int uframe_sleeps;
36255 + /* in/out for communicating number of dma buffers used, or number of ISOC to do */
36256 + unsigned int nrpackets;
36257 + struct fiq_dma_info dma_info;
36258 + struct fiq_hs_isoc_info hs_isoc_info;
36259 + /* Copies of HC registers - in/out communication from/to IRQ handler
36260 + * and for ease of channel setup. A bit of mungeing is performed - for
36261 + * example the hctsiz.b.maxp is _always_ the max packet size of the endpoint.
36262 + */
36263 + hcchar_data_t hcchar_copy;
36264 + hcsplt_data_t hcsplt_copy;
36265 + hcint_data_t hcint_copy;
36266 + hcintmsk_data_t hcintmsk_copy;
36267 + hctsiz_data_t hctsiz_copy;
36268 + hcdma_data_t hcdma_copy;
36269 +};
36270 +
36271 +/**
36272 + * struct fiq_state - top-level FIQ state machine storage
36273 + * @mphi_regs: virtual address of the MPHI peripheral register file
36274 + * @dwc_regs_base: virtual address of the base of the DWC core register file
36275 + * @dma_base: physical address for the base of the DMA bounce buffers
36276 + * @dummy_send: Scratch area for sending a fake message to the MPHI peripheral
36277 + * @gintmsk_saved: Top-level mask of interrupts that the FIQ has not handled.
36278 + * Used for determining which interrupts fired to set off the IRQ handler.
36279 + * @haintmsk_saved: Mask of interrupts from host channels that the FIQ did not handle internally.
36280 + * @np_count: Non-periodic transactions in the active queue
36281 + * @np_sent: Count of non-periodic transactions that have completed
36282 + * @next_sched_frame: For periodic transactions handled by the driver's SOF-driven queuing mechanism,
36283 + * this is the next frame on which a SOF interrupt is required. Used to hold off
36284 + * passing SOF through to the driver until necessary.
36285 + * @channel[n]: Per-channel FIQ state. Allocated during init depending on the number of host
36286 + * channels configured into the core logic.
36287 + *
36288 + * This is passed as the first argument to the dwc_otg_fiq_fsm top-level FIQ handler from the asm stub.
36289 + * It contains top-level state information.
36290 + */
36291 +struct fiq_state {
36292 + fiq_lock_t lock;
36293 + mphi_regs_t mphi_regs;
36294 + void *dwc_regs_base;
36295 + dma_addr_t dma_base;
36296 + struct fiq_dma_blob *fiq_dmab;
36297 + void *dummy_send;
36298 + gintmsk_data_t gintmsk_saved;
36299 + haintmsk_data_t haintmsk_saved;
36300 + int mphi_int_count;
36301 + unsigned int fiq_done;
36302 + unsigned int kick_np_queues;
36303 + unsigned int next_sched_frame;
36304 +#ifdef FIQ_DEBUG
36305 + char * buffer;
36306 + unsigned int bufsiz;
36307 +#endif
36308 + struct fiq_channel_state channel[0];
36309 +};
36310 +
36311 +extern void fiq_fsm_spin_lock(fiq_lock_t *lock);
36312 +
36313 +extern void fiq_fsm_spin_unlock(fiq_lock_t *lock);
36314 +
36315 +extern int fiq_fsm_too_late(struct fiq_state *st, int n);
36316 +
36317 +extern int fiq_fsm_tt_in_use(struct fiq_state *st, int num_channels, int n);
36318 +
36319 +extern void dwc_otg_fiq_fsm(struct fiq_state *state, int num_channels);
36320 +
36321 +extern void dwc_otg_fiq_nop(struct fiq_state *state);
36322 +
36323 +#endif /* DWC_OTG_FIQ_FSM_H_ */
36324 --- /dev/null
36325 +++ b/drivers/usb/host/dwc_otg/dwc_otg_fiq_stub.S
36326 @@ -0,0 +1,80 @@
36327 +/*
36328 + * dwc_otg_fiq_fsm.S - assembly stub for the FSM FIQ
36329 + *
36330 + * Copyright (c) 2013 Raspberry Pi Foundation
36331 + *
36332 + * Author: Jonathan Bell <jonathan@raspberrypi.org>
36333 + * All rights reserved.
36334 + *
36335 + * Redistribution and use in source and binary forms, with or without
36336 + * modification, are permitted provided that the following conditions are met:
36337 + * * Redistributions of source code must retain the above copyright
36338 + * notice, this list of conditions and the following disclaimer.
36339 + * * Redistributions in binary form must reproduce the above copyright
36340 + * notice, this list of conditions and the following disclaimer in the
36341 + * documentation and/or other materials provided with the distribution.
36342 + * * Neither the name of Raspberry Pi nor the
36343 + * names of its contributors may be used to endorse or promote products
36344 + * derived from this software without specific prior written permission.
36345 + *
36346 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
36347 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
36348 + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
36349 + * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
36350 + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
36351 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
36352 + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
36353 + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36354 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36355 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36356 + */
36357 +
36358 +
36359 +#include <asm/assembler.h>
36360 +#include <linux/linkage.h>
36361 +
36362 +
36363 +.text
36364 +
36365 +.global _dwc_otg_fiq_stub_end;
36366 +
36367 +/**
36368 + * _dwc_otg_fiq_stub() - entry copied to the FIQ vector page to allow
36369 + * a C-style function call with arguments from the FIQ banked registers.
36370 + * r0 = &hcd->fiq_state
36371 + * r1 = &hcd->num_channels
36372 + * r2 = &hcd->dma_buffers
36373 + * Tramples: r0, r1, r2, r4, fp, ip
36374 + */
36375 +
36376 +ENTRY(_dwc_otg_fiq_stub)
36377 + /* Stash unbanked regs - SP will have been set up for us */
36378 + mov ip, sp;
36379 + stmdb sp!, {r0-r12, lr};
36380 +#ifdef FIQ_DEBUG
36381 + // Cycle profiling - read cycle counter at start
36382 + mrc p15, 0, r5, c15, c12, 1;
36383 +#endif
36384 + /* r11 = fp, don't trample it */
36385 + mov r4, fp;
36386 + /* set EABI frame size */
36387 + sub fp, ip, #512;
36388 +
36389 + /* for fiq NOP mode - just need state */
36390 + mov r0, r8;
36391 + /* r9 = num_channels */
36392 + mov r1, r9;
36393 + /* r10 = struct *dma_bufs */
36394 +// mov r2, r10;
36395 +
36396 + /* r4 = &fiq_c_function */
36397 + blx r4;
36398 +#ifdef FIQ_DEBUG
36399 + mrc p15, 0, r4, c15, c12, 1;
36400 + subs r5, r5, r4;
36401 + // r5 is now the cycle count time for executing the FIQ. Store it somewhere?
36402 +#endif
36403 + ldmia sp!, {r0-r12, lr};
36404 + subs pc, lr, #4;
36405 +_dwc_otg_fiq_stub_end:
36406 +END(_dwc_otg_fiq_stub)
36407 --- /dev/null
36408 +++ b/drivers/usb/host/dwc_otg/dwc_otg_hcd.c
36409 @@ -0,0 +1,4283 @@
36410 +
36411 +/* ==========================================================================
36412 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd.c $
36413 + * $Revision: #104 $
36414 + * $Date: 2011/10/24 $
36415 + * $Change: 1871159 $
36416 + *
36417 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
36418 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
36419 + * otherwise expressly agreed to in writing between Synopsys and you.
36420 + *
36421 + * The Software IS NOT an item of Licensed Software or Licensed Product under
36422 + * any End User Software License Agreement or Agreement for Licensed Product
36423 + * with Synopsys or any supplement thereto. You are permitted to use and
36424 + * redistribute this Software in source and binary forms, with or without
36425 + * modification, provided that redistributions of source code must retain this
36426 + * notice. You may not view, use, disclose, copy or distribute this file or
36427 + * any information contained herein except pursuant to this license grant from
36428 + * Synopsys. If you do not agree with this notice, including the disclaimer
36429 + * below, then you are not authorized to use the Software.
36430 + *
36431 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
36432 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36433 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36434 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
36435 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
36436 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
36437 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
36438 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36439 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36440 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
36441 + * DAMAGE.
36442 + * ========================================================================== */
36443 +#ifndef DWC_DEVICE_ONLY
36444 +
36445 +/** @file
36446 + * This file implements HCD Core. All code in this file is portable and doesn't
36447 + * use any OS specific functions.
36448 + * Interface provided by HCD Core is defined in <code><hcd_if.h></code>
36449 + * header file.
36450 + */
36451 +
36452 +#include <linux/usb.h>
36453 +#include <linux/usb/hcd.h>
36454 +
36455 +#include "dwc_otg_hcd.h"
36456 +#include "dwc_otg_regs.h"
36457 +#include "dwc_otg_fiq_fsm.h"
36458 +
36459 +extern bool microframe_schedule;
36460 +extern uint16_t fiq_fsm_mask, nak_holdoff;
36461 +
36462 +//#define DEBUG_HOST_CHANNELS
36463 +#ifdef DEBUG_HOST_CHANNELS
36464 +static int last_sel_trans_num_per_scheduled = 0;
36465 +static int last_sel_trans_num_nonper_scheduled = 0;
36466 +static int last_sel_trans_num_avail_hc_at_start = 0;
36467 +static int last_sel_trans_num_avail_hc_at_end = 0;
36468 +#endif /* DEBUG_HOST_CHANNELS */
36469 +
36470 +
36471 +dwc_otg_hcd_t *dwc_otg_hcd_alloc_hcd(void)
36472 +{
36473 + return DWC_ALLOC(sizeof(dwc_otg_hcd_t));
36474 +}
36475 +
36476 +/**
36477 + * Connection timeout function. An OTG host is required to display a
36478 + * message if the device does not connect within 10 seconds.
36479 + */
36480 +void dwc_otg_hcd_connect_timeout(void *ptr)
36481 +{
36482 + DWC_DEBUGPL(DBG_HCDV, "%s(%p)\n", __func__, ptr);
36483 + DWC_PRINTF("Connect Timeout\n");
36484 + __DWC_ERROR("Device Not Connected/Responding\n");
36485 +}
36486 +
36487 +#if defined(DEBUG)
36488 +static void dump_channel_info(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
36489 +{
36490 + if (qh->channel != NULL) {
36491 + dwc_hc_t *hc = qh->channel;
36492 + dwc_list_link_t *item;
36493 + dwc_otg_qh_t *qh_item;
36494 + int num_channels = hcd->core_if->core_params->host_channels;
36495 + int i;
36496 +
36497 + dwc_otg_hc_regs_t *hc_regs;
36498 + hcchar_data_t hcchar;
36499 + hcsplt_data_t hcsplt;
36500 + hctsiz_data_t hctsiz;
36501 + uint32_t hcdma;
36502 +
36503 + hc_regs = hcd->core_if->host_if->hc_regs[hc->hc_num];
36504 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
36505 + hcsplt.d32 = DWC_READ_REG32(&hc_regs->hcsplt);
36506 + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
36507 + hcdma = DWC_READ_REG32(&hc_regs->hcdma);
36508 +
36509 + DWC_PRINTF(" Assigned to channel %p:\n", hc);
36510 + DWC_PRINTF(" hcchar 0x%08x, hcsplt 0x%08x\n", hcchar.d32,
36511 + hcsplt.d32);
36512 + DWC_PRINTF(" hctsiz 0x%08x, hcdma 0x%08x\n", hctsiz.d32,
36513 + hcdma);
36514 + DWC_PRINTF(" dev_addr: %d, ep_num: %d, ep_is_in: %d\n",
36515 + hc->dev_addr, hc->ep_num, hc->ep_is_in);
36516 + DWC_PRINTF(" ep_type: %d\n", hc->ep_type);
36517 + DWC_PRINTF(" max_packet: %d\n", hc->max_packet);
36518 + DWC_PRINTF(" data_pid_start: %d\n", hc->data_pid_start);
36519 + DWC_PRINTF(" xfer_started: %d\n", hc->xfer_started);
36520 + DWC_PRINTF(" halt_status: %d\n", hc->halt_status);
36521 + DWC_PRINTF(" xfer_buff: %p\n", hc->xfer_buff);
36522 + DWC_PRINTF(" xfer_len: %d\n", hc->xfer_len);
36523 + DWC_PRINTF(" qh: %p\n", hc->qh);
36524 + DWC_PRINTF(" NP inactive sched:\n");
36525 + DWC_LIST_FOREACH(item, &hcd->non_periodic_sched_inactive) {
36526 + qh_item =
36527 + DWC_LIST_ENTRY(item, dwc_otg_qh_t, qh_list_entry);
36528 + DWC_PRINTF(" %p\n", qh_item);
36529 + }
36530 + DWC_PRINTF(" NP active sched:\n");
36531 + DWC_LIST_FOREACH(item, &hcd->non_periodic_sched_active) {
36532 + qh_item =
36533 + DWC_LIST_ENTRY(item, dwc_otg_qh_t, qh_list_entry);
36534 + DWC_PRINTF(" %p\n", qh_item);
36535 + }
36536 + DWC_PRINTF(" Channels: \n");
36537 + for (i = 0; i < num_channels; i++) {
36538 + dwc_hc_t *hc = hcd->hc_ptr_array[i];
36539 + DWC_PRINTF(" %2d: %p\n", i, hc);
36540 + }
36541 + }
36542 +}
36543 +#else
36544 +#define dump_channel_info(hcd, qh)
36545 +#endif /* DEBUG */
36546 +
36547 +/**
36548 + * Work queue function for starting the HCD when A-Cable is connected.
36549 + * The hcd_start() must be called in a process context.
36550 + */
36551 +static void hcd_start_func(void *_vp)
36552 +{
36553 + dwc_otg_hcd_t *hcd = (dwc_otg_hcd_t *) _vp;
36554 +
36555 + DWC_DEBUGPL(DBG_HCDV, "%s() %p\n", __func__, hcd);
36556 + if (hcd) {
36557 + hcd->fops->start(hcd);
36558 + }
36559 +}
36560 +
36561 +static void del_xfer_timers(dwc_otg_hcd_t * hcd)
36562 +{
36563 +#ifdef DEBUG
36564 + int i;
36565 + int num_channels = hcd->core_if->core_params->host_channels;
36566 + for (i = 0; i < num_channels; i++) {
36567 + DWC_TIMER_CANCEL(hcd->core_if->hc_xfer_timer[i]);
36568 + }
36569 +#endif
36570 +}
36571 +
36572 +static void del_timers(dwc_otg_hcd_t * hcd)
36573 +{
36574 + del_xfer_timers(hcd);
36575 + DWC_TIMER_CANCEL(hcd->conn_timer);
36576 +}
36577 +
36578 +/**
36579 + * Processes all the URBs in a single list of QHs. Completes them with
36580 + * -ESHUTDOWN and frees the QTD.
36581 + */
36582 +static void kill_urbs_in_qh_list(dwc_otg_hcd_t * hcd, dwc_list_link_t * qh_list)
36583 +{
36584 + dwc_list_link_t *qh_item, *qh_tmp;
36585 + dwc_otg_qh_t *qh;
36586 + dwc_otg_qtd_t *qtd, *qtd_tmp;
36587 +
36588 + DWC_LIST_FOREACH_SAFE(qh_item, qh_tmp, qh_list) {
36589 + qh = DWC_LIST_ENTRY(qh_item, dwc_otg_qh_t, qh_list_entry);
36590 + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp,
36591 + &qh->qtd_list, qtd_list_entry) {
36592 + qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
36593 + if (qtd->urb != NULL) {
36594 + hcd->fops->complete(hcd, qtd->urb->priv,
36595 + qtd->urb, -DWC_E_SHUTDOWN);
36596 + dwc_otg_hcd_qtd_remove_and_free(hcd, qtd, qh);
36597 + }
36598 +
36599 + }
36600 + if(qh->channel) {
36601 + /* Using hcchar.chen == 1 is not a reliable test.
36602 + * It is possible that the channel has already halted
36603 + * but not yet been through the IRQ handler.
36604 + */
36605 + if (fiq_fsm_enable && (hcd->fiq_state->channel[qh->channel->hc_num].fsm != FIQ_PASSTHROUGH)) {
36606 + qh->channel->halt_status = DWC_OTG_HC_XFER_URB_DEQUEUE;
36607 + qh->channel->halt_pending = 1;
36608 + } else {
36609 + dwc_otg_hc_halt(hcd->core_if, qh->channel,
36610 + DWC_OTG_HC_XFER_URB_DEQUEUE);
36611 + }
36612 + qh->channel = NULL;
36613 + }
36614 + dwc_otg_hcd_qh_remove(hcd, qh);
36615 + }
36616 +}
36617 +
36618 +/**
36619 + * Responds with an error status of ESHUTDOWN to all URBs in the non-periodic
36620 + * and periodic schedules. The QTD associated with each URB is removed from
36621 + * the schedule and freed. This function may be called when a disconnect is
36622 + * detected or when the HCD is being stopped.
36623 + */
36624 +static void kill_all_urbs(dwc_otg_hcd_t * hcd)
36625 +{
36626 + kill_urbs_in_qh_list(hcd, &hcd->non_periodic_sched_inactive);
36627 + kill_urbs_in_qh_list(hcd, &hcd->non_periodic_sched_active);
36628 + kill_urbs_in_qh_list(hcd, &hcd->periodic_sched_inactive);
36629 + kill_urbs_in_qh_list(hcd, &hcd->periodic_sched_ready);
36630 + kill_urbs_in_qh_list(hcd, &hcd->periodic_sched_assigned);
36631 + kill_urbs_in_qh_list(hcd, &hcd->periodic_sched_queued);
36632 +}
36633 +
36634 +/**
36635 + * Start the connection timer. An OTG host is required to display a
36636 + * message if the device does not connect within 10 seconds. The
36637 + * timer is deleted if a port connect interrupt occurs before the
36638 + * timer expires.
36639 + */
36640 +static void dwc_otg_hcd_start_connect_timer(dwc_otg_hcd_t * hcd)
36641 +{
36642 + DWC_TIMER_SCHEDULE(hcd->conn_timer, 10000 /* 10 secs */ );
36643 +}
36644 +
36645 +/**
36646 + * HCD Callback function for disconnect of the HCD.
36647 + *
36648 + * @param p void pointer to the <code>struct usb_hcd</code>
36649 + */
36650 +static int32_t dwc_otg_hcd_session_start_cb(void *p)
36651 +{
36652 + dwc_otg_hcd_t *dwc_otg_hcd;
36653 + DWC_DEBUGPL(DBG_HCDV, "%s(%p)\n", __func__, p);
36654 + dwc_otg_hcd = p;
36655 + dwc_otg_hcd_start_connect_timer(dwc_otg_hcd);
36656 + return 1;
36657 +}
36658 +
36659 +/**
36660 + * HCD Callback function for starting the HCD when A-Cable is
36661 + * connected.
36662 + *
36663 + * @param p void pointer to the <code>struct usb_hcd</code>
36664 + */
36665 +static int32_t dwc_otg_hcd_start_cb(void *p)
36666 +{
36667 + dwc_otg_hcd_t *dwc_otg_hcd = p;
36668 + dwc_otg_core_if_t *core_if;
36669 + hprt0_data_t hprt0;
36670 +
36671 + core_if = dwc_otg_hcd->core_if;
36672 +
36673 + if (core_if->op_state == B_HOST) {
36674 + /*
36675 + * Reset the port. During a HNP mode switch the reset
36676 + * needs to occur within 1ms and have a duration of at
36677 + * least 50ms.
36678 + */
36679 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
36680 + hprt0.b.prtrst = 1;
36681 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
36682 + }
36683 + DWC_WORKQ_SCHEDULE_DELAYED(core_if->wq_otg,
36684 + hcd_start_func, dwc_otg_hcd, 50,
36685 + "start hcd");
36686 +
36687 + return 1;
36688 +}
36689 +
36690 +/**
36691 + * HCD Callback function for disconnect of the HCD.
36692 + *
36693 + * @param p void pointer to the <code>struct usb_hcd</code>
36694 + */
36695 +static int32_t dwc_otg_hcd_disconnect_cb(void *p)
36696 +{
36697 + gintsts_data_t intr;
36698 + dwc_otg_hcd_t *dwc_otg_hcd = p;
36699 +
36700 + DWC_SPINLOCK(dwc_otg_hcd->lock);
36701 + /*
36702 + * Set status flags for the hub driver.
36703 + */
36704 + dwc_otg_hcd->flags.b.port_connect_status_change = 1;
36705 + dwc_otg_hcd->flags.b.port_connect_status = 0;
36706 + if(fiq_enable) {
36707 + local_fiq_disable();
36708 + fiq_fsm_spin_lock(&dwc_otg_hcd->fiq_state->lock);
36709 + }
36710 + /*
36711 + * Shutdown any transfers in process by clearing the Tx FIFO Empty
36712 + * interrupt mask and status bits and disabling subsequent host
36713 + * channel interrupts.
36714 + */
36715 + intr.d32 = 0;
36716 + intr.b.nptxfempty = 1;
36717 + intr.b.ptxfempty = 1;
36718 + intr.b.hcintr = 1;
36719 + DWC_MODIFY_REG32(&dwc_otg_hcd->core_if->core_global_regs->gintmsk,
36720 + intr.d32, 0);
36721 + DWC_MODIFY_REG32(&dwc_otg_hcd->core_if->core_global_regs->gintsts,
36722 + intr.d32, 0);
36723 +
36724 + del_timers(dwc_otg_hcd);
36725 +
36726 + /*
36727 + * Turn off the vbus power only if the core has transitioned to device
36728 + * mode. If still in host mode, need to keep power on to detect a
36729 + * reconnection.
36730 + */
36731 + if (dwc_otg_is_device_mode(dwc_otg_hcd->core_if)) {
36732 + if (dwc_otg_hcd->core_if->op_state != A_SUSPEND) {
36733 + hprt0_data_t hprt0 = {.d32 = 0 };
36734 + DWC_PRINTF("Disconnect: PortPower off\n");
36735 + hprt0.b.prtpwr = 0;
36736 + DWC_WRITE_REG32(dwc_otg_hcd->core_if->host_if->hprt0,
36737 + hprt0.d32);
36738 + }
36739 +
36740 + dwc_otg_disable_host_interrupts(dwc_otg_hcd->core_if);
36741 + }
36742 +
36743 + /* Respond with an error status to all URBs in the schedule. */
36744 + kill_all_urbs(dwc_otg_hcd);
36745 +
36746 + if (dwc_otg_is_host_mode(dwc_otg_hcd->core_if)) {
36747 + /* Clean up any host channels that were in use. */
36748 + int num_channels;
36749 + int i;
36750 + dwc_hc_t *channel;
36751 + dwc_otg_hc_regs_t *hc_regs;
36752 + hcchar_data_t hcchar;
36753 +
36754 + num_channels = dwc_otg_hcd->core_if->core_params->host_channels;
36755 +
36756 + if (!dwc_otg_hcd->core_if->dma_enable) {
36757 + /* Flush out any channel requests in slave mode. */
36758 + for (i = 0; i < num_channels; i++) {
36759 + channel = dwc_otg_hcd->hc_ptr_array[i];
36760 + if (DWC_CIRCLEQ_EMPTY_ENTRY
36761 + (channel, hc_list_entry)) {
36762 + hc_regs =
36763 + dwc_otg_hcd->core_if->
36764 + host_if->hc_regs[i];
36765 + hcchar.d32 =
36766 + DWC_READ_REG32(&hc_regs->hcchar);
36767 + if (hcchar.b.chen) {
36768 + hcchar.b.chen = 0;
36769 + hcchar.b.chdis = 1;
36770 + hcchar.b.epdir = 0;
36771 + DWC_WRITE_REG32
36772 + (&hc_regs->hcchar,
36773 + hcchar.d32);
36774 + }
36775 + }
36776 + }
36777 + }
36778 +
36779 + if(fiq_fsm_enable) {
36780 + for(i=0; i < 128; i++) {
36781 + dwc_otg_hcd->hub_port[i] = 0;
36782 + }
36783 + }
36784 + }
36785 +
36786 + if(fiq_enable) {
36787 + fiq_fsm_spin_unlock(&dwc_otg_hcd->fiq_state->lock);
36788 + local_fiq_enable();
36789 + }
36790 +
36791 + if (dwc_otg_hcd->fops->disconnect) {
36792 + dwc_otg_hcd->fops->disconnect(dwc_otg_hcd);
36793 + }
36794 +
36795 + DWC_SPINUNLOCK(dwc_otg_hcd->lock);
36796 + return 1;
36797 +}
36798 +
36799 +/**
36800 + * HCD Callback function for stopping the HCD.
36801 + *
36802 + * @param p void pointer to the <code>struct usb_hcd</code>
36803 + */
36804 +static int32_t dwc_otg_hcd_stop_cb(void *p)
36805 +{
36806 + dwc_otg_hcd_t *dwc_otg_hcd = p;
36807 +
36808 + DWC_DEBUGPL(DBG_HCDV, "%s(%p)\n", __func__, p);
36809 + dwc_otg_hcd_stop(dwc_otg_hcd);
36810 + return 1;
36811 +}
36812 +
36813 +#ifdef CONFIG_USB_DWC_OTG_LPM
36814 +/**
36815 + * HCD Callback function for sleep of HCD.
36816 + *
36817 + * @param p void pointer to the <code>struct usb_hcd</code>
36818 + */
36819 +static int dwc_otg_hcd_sleep_cb(void *p)
36820 +{
36821 + dwc_otg_hcd_t *hcd = p;
36822 +
36823 + dwc_otg_hcd_free_hc_from_lpm(hcd);
36824 +
36825 + return 0;
36826 +}
36827 +#endif
36828 +
36829 +
36830 +/**
36831 + * HCD Callback function for Remote Wakeup.
36832 + *
36833 + * @param p void pointer to the <code>struct usb_hcd</code>
36834 + */
36835 +static int dwc_otg_hcd_rem_wakeup_cb(void *p)
36836 +{
36837 + dwc_otg_hcd_t *hcd = p;
36838 +
36839 + if (hcd->core_if->lx_state == DWC_OTG_L2) {
36840 + hcd->flags.b.port_suspend_change = 1;
36841 + }
36842 +#ifdef CONFIG_USB_DWC_OTG_LPM
36843 + else {
36844 + hcd->flags.b.port_l1_change = 1;
36845 + }
36846 +#endif
36847 + return 0;
36848 +}
36849 +
36850 +/**
36851 + * Halts the DWC_otg host mode operations in a clean manner. USB transfers are
36852 + * stopped.
36853 + */
36854 +void dwc_otg_hcd_stop(dwc_otg_hcd_t * hcd)
36855 +{
36856 + hprt0_data_t hprt0 = {.d32 = 0 };
36857 +
36858 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD STOP\n");
36859 +
36860 + /*
36861 + * The root hub should be disconnected before this function is called.
36862 + * The disconnect will clear the QTD lists (via ..._hcd_urb_dequeue)
36863 + * and the QH lists (via ..._hcd_endpoint_disable).
36864 + */
36865 +
36866 + /* Turn off all host-specific interrupts. */
36867 + dwc_otg_disable_host_interrupts(hcd->core_if);
36868 +
36869 + /* Turn off the vbus power */
36870 + DWC_PRINTF("PortPower off\n");
36871 + hprt0.b.prtpwr = 0;
36872 + DWC_WRITE_REG32(hcd->core_if->host_if->hprt0, hprt0.d32);
36873 + dwc_mdelay(1);
36874 +}
36875 +
36876 +int dwc_otg_hcd_urb_enqueue(dwc_otg_hcd_t * hcd,
36877 + dwc_otg_hcd_urb_t * dwc_otg_urb, void **ep_handle,
36878 + int atomic_alloc)
36879 +{
36880 + int retval = 0;
36881 + uint8_t needs_scheduling = 0;
36882 + dwc_otg_transaction_type_e tr_type;
36883 + dwc_otg_qtd_t *qtd;
36884 + gintmsk_data_t intr_mask = {.d32 = 0 };
36885 + hprt0_data_t hprt0 = { .d32 = 0 };
36886 +
36887 +#ifdef DEBUG /* integrity checks (Broadcom) */
36888 + if (NULL == hcd->core_if) {
36889 + DWC_ERROR("**** DWC OTG HCD URB Enqueue - HCD has NULL core_if\n");
36890 + /* No longer connected. */
36891 + return -DWC_E_INVALID;
36892 + }
36893 +#endif
36894 + if (!hcd->flags.b.port_connect_status) {
36895 + /* No longer connected. */
36896 + DWC_ERROR("Not connected\n");
36897 + return -DWC_E_NO_DEVICE;
36898 + }
36899 +
36900 + /* Some core configurations cannot support LS traffic on a FS root port */
36901 + if ((hcd->fops->speed(hcd, dwc_otg_urb->priv) == USB_SPEED_LOW) &&
36902 + (hcd->core_if->hwcfg2.b.fs_phy_type == 1) &&
36903 + (hcd->core_if->hwcfg2.b.hs_phy_type == 1)) {
36904 + hprt0.d32 = DWC_READ_REG32(hcd->core_if->host_if->hprt0);
36905 + if (hprt0.b.prtspd == DWC_HPRT0_PRTSPD_FULL_SPEED) {
36906 + return -DWC_E_NO_DEVICE;
36907 + }
36908 + }
36909 +
36910 + qtd = dwc_otg_hcd_qtd_create(dwc_otg_urb, atomic_alloc);
36911 + if (qtd == NULL) {
36912 + DWC_ERROR("DWC OTG HCD URB Enqueue failed creating QTD\n");
36913 + return -DWC_E_NO_MEMORY;
36914 + }
36915 +#ifdef DEBUG /* integrity checks (Broadcom) */
36916 + if (qtd->urb == NULL) {
36917 + DWC_ERROR("**** DWC OTG HCD URB Enqueue created QTD with no URBs\n");
36918 + return -DWC_E_NO_MEMORY;
36919 + }
36920 + if (qtd->urb->priv == NULL) {
36921 + DWC_ERROR("**** DWC OTG HCD URB Enqueue created QTD URB with no URB handle\n");
36922 + return -DWC_E_NO_MEMORY;
36923 + }
36924 +#endif
36925 + intr_mask.d32 = DWC_READ_REG32(&hcd->core_if->core_global_regs->gintmsk);
36926 + if(!intr_mask.b.sofintr || fiq_enable) needs_scheduling = 1;
36927 + if((((dwc_otg_qh_t *)ep_handle)->ep_type == UE_BULK) && !(qtd->urb->flags & URB_GIVEBACK_ASAP))
36928 + /* Do not schedule SG transactions until qtd has URB_GIVEBACK_ASAP set */
36929 + needs_scheduling = 0;
36930 +
36931 + retval = dwc_otg_hcd_qtd_add(qtd, hcd, (dwc_otg_qh_t **) ep_handle, atomic_alloc);
36932 + // creates a new queue in ep_handle if it doesn't exist already
36933 + if (retval < 0) {
36934 + DWC_ERROR("DWC OTG HCD URB Enqueue failed adding QTD. "
36935 + "Error status %d\n", retval);
36936 + dwc_otg_hcd_qtd_free(qtd);
36937 + return retval;
36938 + }
36939 +
36940 + if(needs_scheduling) {
36941 + tr_type = dwc_otg_hcd_select_transactions(hcd);
36942 + if (tr_type != DWC_OTG_TRANSACTION_NONE) {
36943 + dwc_otg_hcd_queue_transactions(hcd, tr_type);
36944 + }
36945 + }
36946 + return retval;
36947 +}
36948 +
36949 +int dwc_otg_hcd_urb_dequeue(dwc_otg_hcd_t * hcd,
36950 + dwc_otg_hcd_urb_t * dwc_otg_urb)
36951 +{
36952 + dwc_otg_qh_t *qh;
36953 + dwc_otg_qtd_t *urb_qtd;
36954 + BUG_ON(!hcd);
36955 + BUG_ON(!dwc_otg_urb);
36956 +
36957 +#ifdef DEBUG /* integrity checks (Broadcom) */
36958 +
36959 + if (hcd == NULL) {
36960 + DWC_ERROR("**** DWC OTG HCD URB Dequeue has NULL HCD\n");
36961 + return -DWC_E_INVALID;
36962 + }
36963 + if (dwc_otg_urb == NULL) {
36964 + DWC_ERROR("**** DWC OTG HCD URB Dequeue has NULL URB\n");
36965 + return -DWC_E_INVALID;
36966 + }
36967 + if (dwc_otg_urb->qtd == NULL) {
36968 + DWC_ERROR("**** DWC OTG HCD URB Dequeue with NULL QTD\n");
36969 + return -DWC_E_INVALID;
36970 + }
36971 + urb_qtd = dwc_otg_urb->qtd;
36972 + BUG_ON(!urb_qtd);
36973 + if (urb_qtd->qh == NULL) {
36974 + DWC_ERROR("**** DWC OTG HCD URB Dequeue with QTD with NULL Q handler\n");
36975 + return -DWC_E_INVALID;
36976 + }
36977 +#else
36978 + urb_qtd = dwc_otg_urb->qtd;
36979 + BUG_ON(!urb_qtd);
36980 +#endif
36981 + qh = urb_qtd->qh;
36982 + BUG_ON(!qh);
36983 + if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
36984 + if (urb_qtd->in_process) {
36985 + dump_channel_info(hcd, qh);
36986 + }
36987 + }
36988 +#ifdef DEBUG /* integrity checks (Broadcom) */
36989 + if (hcd->core_if == NULL) {
36990 + DWC_ERROR("**** DWC OTG HCD URB Dequeue HCD has NULL core_if\n");
36991 + return -DWC_E_INVALID;
36992 + }
36993 +#endif
36994 + if (urb_qtd->in_process && qh->channel) {
36995 + /* The QTD is in process (it has been assigned to a channel). */
36996 + if (hcd->flags.b.port_connect_status) {
36997 + int n = qh->channel->hc_num;
36998 + /*
36999 + * If still connected (i.e. in host mode), halt the
37000 + * channel so it can be used for other transfers. If
37001 + * no longer connected, the host registers can't be
37002 + * written to halt the channel since the core is in
37003 + * device mode.
37004 + */
37005 + /* In FIQ FSM mode, we need to shut down carefully.
37006 + * The FIQ may attempt to restart a disabled channel */
37007 + if (fiq_fsm_enable && (hcd->fiq_state->channel[n].fsm != FIQ_PASSTHROUGH)) {
37008 + qh->channel->halt_status = DWC_OTG_HC_XFER_URB_DEQUEUE;
37009 + qh->channel->halt_pending = 1;
37010 + //hcd->fiq_state->channel[n].fsm = FIQ_DEQUEUE_ISSUED;
37011 + } else {
37012 + dwc_otg_hc_halt(hcd->core_if, qh->channel,
37013 + DWC_OTG_HC_XFER_URB_DEQUEUE);
37014 + }
37015 + }
37016 + }
37017 +
37018 + /*
37019 + * Free the QTD and clean up the associated QH. Leave the QH in the
37020 + * schedule if it has any remaining QTDs.
37021 + */
37022 +
37023 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD URB Dequeue - "
37024 + "delete %sQueue handler\n",
37025 + hcd->core_if->dma_desc_enable?"DMA ":"");
37026 + if (!hcd->core_if->dma_desc_enable) {
37027 + uint8_t b = urb_qtd->in_process;
37028 + if (nak_holdoff && qh->do_split && dwc_qh_is_non_per(qh))
37029 + qh->nak_frame = 0xFFFF;
37030 + dwc_otg_hcd_qtd_remove_and_free(hcd, urb_qtd, qh);
37031 + if (b) {
37032 + dwc_otg_hcd_qh_deactivate(hcd, qh, 0);
37033 + qh->channel = NULL;
37034 + } else if (DWC_CIRCLEQ_EMPTY(&qh->qtd_list)) {
37035 + dwc_otg_hcd_qh_remove(hcd, qh);
37036 + }
37037 + } else {
37038 + dwc_otg_hcd_qtd_remove_and_free(hcd, urb_qtd, qh);
37039 + }
37040 + return 0;
37041 +}
37042 +
37043 +int dwc_otg_hcd_endpoint_disable(dwc_otg_hcd_t * hcd, void *ep_handle,
37044 + int retry)
37045 +{
37046 + dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
37047 + int retval = 0;
37048 + dwc_irqflags_t flags;
37049 +
37050 + if (retry < 0) {
37051 + retval = -DWC_E_INVALID;
37052 + goto done;
37053 + }
37054 +
37055 + if (!qh) {
37056 + retval = -DWC_E_INVALID;
37057 + goto done;
37058 + }
37059 +
37060 + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
37061 +
37062 + while (!DWC_CIRCLEQ_EMPTY(&qh->qtd_list) && retry) {
37063 + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
37064 + retry--;
37065 + dwc_msleep(5);
37066 + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
37067 + }
37068 +
37069 + dwc_otg_hcd_qh_remove(hcd, qh);
37070 +
37071 + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
37072 + /*
37073 + * Split dwc_otg_hcd_qh_remove_and_free() into qh_remove
37074 + * and qh_free to prevent stack dump on DWC_DMA_FREE() with
37075 + * irq_disabled (spinlock_irqsave) in dwc_otg_hcd_desc_list_free()
37076 + * and dwc_otg_hcd_frame_list_alloc().
37077 + */
37078 + dwc_otg_hcd_qh_free(hcd, qh);
37079 +
37080 +done:
37081 + return retval;
37082 +}
37083 +
37084 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30)
37085 +int dwc_otg_hcd_endpoint_reset(dwc_otg_hcd_t * hcd, void *ep_handle)
37086 +{
37087 + int retval = 0;
37088 + dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
37089 + if (!qh)
37090 + return -DWC_E_INVALID;
37091 +
37092 + qh->data_toggle = DWC_OTG_HC_PID_DATA0;
37093 + return retval;
37094 +}
37095 +#endif
37096 +
37097 +/**
37098 + * HCD Callback structure for handling mode switching.
37099 + */
37100 +static dwc_otg_cil_callbacks_t hcd_cil_callbacks = {
37101 + .start = dwc_otg_hcd_start_cb,
37102 + .stop = dwc_otg_hcd_stop_cb,
37103 + .disconnect = dwc_otg_hcd_disconnect_cb,
37104 + .session_start = dwc_otg_hcd_session_start_cb,
37105 + .resume_wakeup = dwc_otg_hcd_rem_wakeup_cb,
37106 +#ifdef CONFIG_USB_DWC_OTG_LPM
37107 + .sleep = dwc_otg_hcd_sleep_cb,
37108 +#endif
37109 + .p = 0,
37110 +};
37111 +
37112 +/**
37113 + * Reset tasklet function
37114 + */
37115 +static void reset_tasklet_func(void *data)
37116 +{
37117 + dwc_otg_hcd_t *dwc_otg_hcd = (dwc_otg_hcd_t *) data;
37118 + dwc_otg_core_if_t *core_if = dwc_otg_hcd->core_if;
37119 + hprt0_data_t hprt0;
37120 +
37121 + DWC_DEBUGPL(DBG_HCDV, "USB RESET tasklet called\n");
37122 +
37123 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
37124 + hprt0.b.prtrst = 1;
37125 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
37126 + dwc_mdelay(60);
37127 +
37128 + hprt0.b.prtrst = 0;
37129 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
37130 + dwc_otg_hcd->flags.b.port_reset_change = 1;
37131 +}
37132 +
37133 +static void completion_tasklet_func(void *ptr)
37134 +{
37135 + dwc_otg_hcd_t *hcd = (dwc_otg_hcd_t *) ptr;
37136 + struct urb *urb;
37137 + urb_tq_entry_t *item;
37138 + dwc_irqflags_t flags;
37139 +
37140 + /* This could just be spin_lock_irq */
37141 + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
37142 + while (!DWC_TAILQ_EMPTY(&hcd->completed_urb_list)) {
37143 + item = DWC_TAILQ_FIRST(&hcd->completed_urb_list);
37144 + urb = item->urb;
37145 + DWC_TAILQ_REMOVE(&hcd->completed_urb_list, item,
37146 + urb_tq_entries);
37147 + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
37148 + DWC_FREE(item);
37149 +
37150 + usb_hcd_giveback_urb(hcd->priv, urb, urb->status);
37151 +
37152 +
37153 + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
37154 + }
37155 + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
37156 + return;
37157 +}
37158 +
37159 +static void qh_list_free(dwc_otg_hcd_t * hcd, dwc_list_link_t * qh_list)
37160 +{
37161 + dwc_list_link_t *item;
37162 + dwc_otg_qh_t *qh;
37163 + dwc_irqflags_t flags;
37164 +
37165 + if (!qh_list->next) {
37166 + /* The list hasn't been initialized yet. */
37167 + return;
37168 + }
37169 + /*
37170 + * Hold spinlock here. Not needed in that case if bellow
37171 + * function is being called from ISR
37172 + */
37173 + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
37174 + /* Ensure there are no QTDs or URBs left. */
37175 + kill_urbs_in_qh_list(hcd, qh_list);
37176 + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
37177 +
37178 + DWC_LIST_FOREACH(item, qh_list) {
37179 + qh = DWC_LIST_ENTRY(item, dwc_otg_qh_t, qh_list_entry);
37180 + dwc_otg_hcd_qh_remove_and_free(hcd, qh);
37181 + }
37182 +}
37183 +
37184 +/**
37185 + * Exit from Hibernation if Host did not detect SRP from connected SRP capable
37186 + * Device during SRP time by host power up.
37187 + */
37188 +void dwc_otg_hcd_power_up(void *ptr)
37189 +{
37190 + gpwrdn_data_t gpwrdn = {.d32 = 0 };
37191 + dwc_otg_core_if_t *core_if = (dwc_otg_core_if_t *) ptr;
37192 +
37193 + DWC_PRINTF("%s called\n", __FUNCTION__);
37194 +
37195 + if (!core_if->hibernation_suspend) {
37196 + DWC_PRINTF("Already exited from Hibernation\n");
37197 + return;
37198 + }
37199 +
37200 + /* Switch on the voltage to the core */
37201 + gpwrdn.b.pwrdnswtch = 1;
37202 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
37203 + dwc_udelay(10);
37204 +
37205 + /* Reset the core */
37206 + gpwrdn.d32 = 0;
37207 + gpwrdn.b.pwrdnrstn = 1;
37208 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
37209 + dwc_udelay(10);
37210 +
37211 + /* Disable power clamps */
37212 + gpwrdn.d32 = 0;
37213 + gpwrdn.b.pwrdnclmp = 1;
37214 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
37215 +
37216 + /* Remove reset the core signal */
37217 + gpwrdn.d32 = 0;
37218 + gpwrdn.b.pwrdnrstn = 1;
37219 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
37220 + dwc_udelay(10);
37221 +
37222 + /* Disable PMU interrupt */
37223 + gpwrdn.d32 = 0;
37224 + gpwrdn.b.pmuintsel = 1;
37225 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
37226 +
37227 + core_if->hibernation_suspend = 0;
37228 +
37229 + /* Disable PMU */
37230 + gpwrdn.d32 = 0;
37231 + gpwrdn.b.pmuactv = 1;
37232 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
37233 + dwc_udelay(10);
37234 +
37235 + /* Enable VBUS */
37236 + gpwrdn.d32 = 0;
37237 + gpwrdn.b.dis_vbus = 1;
37238 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
37239 +
37240 + core_if->op_state = A_HOST;
37241 + dwc_otg_core_init(core_if);
37242 + dwc_otg_enable_global_interrupts(core_if);
37243 + cil_hcd_start(core_if);
37244 +}
37245 +
37246 +void dwc_otg_cleanup_fiq_channel(dwc_otg_hcd_t *hcd, uint32_t num)
37247 +{
37248 + struct fiq_channel_state *st = &hcd->fiq_state->channel[num];
37249 + struct fiq_dma_blob *blob = hcd->fiq_dmab;
37250 + int i;
37251 +
37252 + st->fsm = FIQ_PASSTHROUGH;
37253 + st->hcchar_copy.d32 = 0;
37254 + st->hcsplt_copy.d32 = 0;
37255 + st->hcint_copy.d32 = 0;
37256 + st->hcintmsk_copy.d32 = 0;
37257 + st->hctsiz_copy.d32 = 0;
37258 + st->hcdma_copy.d32 = 0;
37259 + st->nr_errors = 0;
37260 + st->hub_addr = 0;
37261 + st->port_addr = 0;
37262 + st->expected_uframe = 0;
37263 + st->nrpackets = 0;
37264 + st->dma_info.index = 0;
37265 + for (i = 0; i < 6; i++)
37266 + st->dma_info.slot_len[i] = 255;
37267 + st->hs_isoc_info.index = 0;
37268 + st->hs_isoc_info.iso_desc = NULL;
37269 + st->hs_isoc_info.nrframes = 0;
37270 +
37271 + DWC_MEMSET(&blob->channel[num].index[0], 0x6b, 1128);
37272 +}
37273 +
37274 +/**
37275 + * Frees secondary storage associated with the dwc_otg_hcd structure contained
37276 + * in the struct usb_hcd field.
37277 + */
37278 +static void dwc_otg_hcd_free(dwc_otg_hcd_t * dwc_otg_hcd)
37279 +{
37280 + struct device *dev = dwc_otg_hcd_to_dev(dwc_otg_hcd);
37281 + int i;
37282 +
37283 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD FREE\n");
37284 +
37285 + del_timers(dwc_otg_hcd);
37286 +
37287 + /* Free memory for QH/QTD lists */
37288 + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->non_periodic_sched_inactive);
37289 + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->non_periodic_sched_active);
37290 + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->periodic_sched_inactive);
37291 + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->periodic_sched_ready);
37292 + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->periodic_sched_assigned);
37293 + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->periodic_sched_queued);
37294 +
37295 + /* Free memory for the host channels. */
37296 + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
37297 + dwc_hc_t *hc = dwc_otg_hcd->hc_ptr_array[i];
37298 +
37299 +#ifdef DEBUG
37300 + if (dwc_otg_hcd->core_if->hc_xfer_timer[i]) {
37301 + DWC_TIMER_FREE(dwc_otg_hcd->core_if->hc_xfer_timer[i]);
37302 + }
37303 +#endif
37304 + if (hc != NULL) {
37305 + DWC_DEBUGPL(DBG_HCDV, "HCD Free channel #%i, hc=%p\n",
37306 + i, hc);
37307 + DWC_FREE(hc);
37308 + }
37309 + }
37310 +
37311 + if (dwc_otg_hcd->core_if->dma_enable) {
37312 + if (dwc_otg_hcd->status_buf_dma) {
37313 + DWC_DMA_FREE(dev, DWC_OTG_HCD_STATUS_BUF_SIZE,
37314 + dwc_otg_hcd->status_buf,
37315 + dwc_otg_hcd->status_buf_dma);
37316 + }
37317 + } else if (dwc_otg_hcd->status_buf != NULL) {
37318 + DWC_FREE(dwc_otg_hcd->status_buf);
37319 + }
37320 + DWC_SPINLOCK_FREE(dwc_otg_hcd->lock);
37321 + /* Set core_if's lock pointer to NULL */
37322 + dwc_otg_hcd->core_if->lock = NULL;
37323 +
37324 + DWC_TIMER_FREE(dwc_otg_hcd->conn_timer);
37325 + DWC_TASK_FREE(dwc_otg_hcd->reset_tasklet);
37326 + DWC_TASK_FREE(dwc_otg_hcd->completion_tasklet);
37327 + DWC_FREE(dwc_otg_hcd->fiq_state);
37328 +
37329 +#ifdef DWC_DEV_SRPCAP
37330 + if (dwc_otg_hcd->core_if->power_down == 2 &&
37331 + dwc_otg_hcd->core_if->pwron_timer) {
37332 + DWC_TIMER_FREE(dwc_otg_hcd->core_if->pwron_timer);
37333 + }
37334 +#endif
37335 + DWC_FREE(dwc_otg_hcd);
37336 +}
37337 +
37338 +int dwc_otg_hcd_init(dwc_otg_hcd_t * hcd, dwc_otg_core_if_t * core_if)
37339 +{
37340 + struct device *dev = dwc_otg_hcd_to_dev(hcd);
37341 + int retval = 0;
37342 + int num_channels;
37343 + int i;
37344 + dwc_hc_t *channel;
37345 +
37346 +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_SPINLOCK))
37347 + DWC_SPINLOCK_ALLOC_LINUX_DEBUG(hcd->lock);
37348 +#else
37349 + hcd->lock = DWC_SPINLOCK_ALLOC();
37350 +#endif
37351 + DWC_DEBUGPL(DBG_HCDV, "init of HCD %p given core_if %p\n",
37352 + hcd, core_if);
37353 + if (!hcd->lock) {
37354 + DWC_ERROR("Could not allocate lock for pcd");
37355 + DWC_FREE(hcd);
37356 + retval = -DWC_E_NO_MEMORY;
37357 + goto out;
37358 + }
37359 + hcd->core_if = core_if;
37360 +
37361 + /* Register the HCD CIL Callbacks */
37362 + dwc_otg_cil_register_hcd_callbacks(hcd->core_if,
37363 + &hcd_cil_callbacks, hcd);
37364 +
37365 + /* Initialize the non-periodic schedule. */
37366 + DWC_LIST_INIT(&hcd->non_periodic_sched_inactive);
37367 + DWC_LIST_INIT(&hcd->non_periodic_sched_active);
37368 +
37369 + /* Initialize the periodic schedule. */
37370 + DWC_LIST_INIT(&hcd->periodic_sched_inactive);
37371 + DWC_LIST_INIT(&hcd->periodic_sched_ready);
37372 + DWC_LIST_INIT(&hcd->periodic_sched_assigned);
37373 + DWC_LIST_INIT(&hcd->periodic_sched_queued);
37374 + DWC_TAILQ_INIT(&hcd->completed_urb_list);
37375 + /*
37376 + * Create a host channel descriptor for each host channel implemented
37377 + * in the controller. Initialize the channel descriptor array.
37378 + */
37379 + DWC_CIRCLEQ_INIT(&hcd->free_hc_list);
37380 + num_channels = hcd->core_if->core_params->host_channels;
37381 + DWC_MEMSET(hcd->hc_ptr_array, 0, sizeof(hcd->hc_ptr_array));
37382 + for (i = 0; i < num_channels; i++) {
37383 + channel = DWC_ALLOC(sizeof(dwc_hc_t));
37384 + if (channel == NULL) {
37385 + retval = -DWC_E_NO_MEMORY;
37386 + DWC_ERROR("%s: host channel allocation failed\n",
37387 + __func__);
37388 + dwc_otg_hcd_free(hcd);
37389 + goto out;
37390 + }
37391 + channel->hc_num = i;
37392 + hcd->hc_ptr_array[i] = channel;
37393 +#ifdef DEBUG
37394 + hcd->core_if->hc_xfer_timer[i] =
37395 + DWC_TIMER_ALLOC("hc timer", hc_xfer_timeout,
37396 + &hcd->core_if->hc_xfer_info[i]);
37397 +#endif
37398 + DWC_DEBUGPL(DBG_HCDV, "HCD Added channel #%d, hc=%p\n", i,
37399 + channel);
37400 + }
37401 +
37402 + if (fiq_enable) {
37403 + hcd->fiq_state = DWC_ALLOC(sizeof(struct fiq_state) + (sizeof(struct fiq_channel_state) * num_channels));
37404 + if (!hcd->fiq_state) {
37405 + retval = -DWC_E_NO_MEMORY;
37406 + DWC_ERROR("%s: cannot allocate fiq_state structure\n", __func__);
37407 + dwc_otg_hcd_free(hcd);
37408 + goto out;
37409 + }
37410 + DWC_MEMSET(hcd->fiq_state, 0, (sizeof(struct fiq_state) + (sizeof(struct fiq_channel_state) * num_channels)));
37411 +
37412 + for (i = 0; i < num_channels; i++) {
37413 + hcd->fiq_state->channel[i].fsm = FIQ_PASSTHROUGH;
37414 + }
37415 + hcd->fiq_state->dummy_send = DWC_ALLOC_ATOMIC(16);
37416 +
37417 + hcd->fiq_stack = DWC_ALLOC(sizeof(struct fiq_stack));
37418 + if (!hcd->fiq_stack) {
37419 + retval = -DWC_E_NO_MEMORY;
37420 + DWC_ERROR("%s: cannot allocate fiq_stack structure\n", __func__);
37421 + dwc_otg_hcd_free(hcd);
37422 + goto out;
37423 + }
37424 + hcd->fiq_stack->magic1 = 0xDEADBEEF;
37425 + hcd->fiq_stack->magic2 = 0xD00DFEED;
37426 + hcd->fiq_state->gintmsk_saved.d32 = ~0;
37427 + hcd->fiq_state->haintmsk_saved.b2.chint = ~0;
37428 +
37429 + /* This bit is terrible and uses no API, but necessary. The FIQ has no concept of DMA pools
37430 + * (and if it did, would be a lot slower). This allocates a chunk of memory (~9kiB for 8 host channels)
37431 + * for use as transaction bounce buffers in a 2-D array. Our access into this chunk is done by some
37432 + * moderately readable array casts.
37433 + */
37434 + hcd->fiq_dmab = DWC_DMA_ALLOC(dev, (sizeof(struct fiq_dma_channel) * num_channels), &hcd->fiq_state->dma_base);
37435 + DWC_WARN("FIQ DMA bounce buffers: virt = 0x%08x dma = 0x%08x len=%d",
37436 + (unsigned int)hcd->fiq_dmab, (unsigned int)hcd->fiq_state->dma_base,
37437 + sizeof(struct fiq_dma_channel) * num_channels);
37438 +
37439 + DWC_MEMSET(hcd->fiq_dmab, 0x6b, 9024);
37440 +
37441 + /* pointer for debug in fiq_print */
37442 + hcd->fiq_state->fiq_dmab = hcd->fiq_dmab;
37443 + if (fiq_fsm_enable) {
37444 + int i;
37445 + for (i=0; i < hcd->core_if->core_params->host_channels; i++) {
37446 + dwc_otg_cleanup_fiq_channel(hcd, i);
37447 + }
37448 + DWC_PRINTF("FIQ FSM acceleration enabled for :\n%s%s%s%s",
37449 + (fiq_fsm_mask & 0x1) ? "Non-periodic Split Transactions\n" : "",
37450 + (fiq_fsm_mask & 0x2) ? "Periodic Split Transactions\n" : "",
37451 + (fiq_fsm_mask & 0x4) ? "High-Speed Isochronous Endpoints\n" : "",
37452 + (fiq_fsm_mask & 0x8) ? "Interrupt/Control Split Transaction hack enabled\n" : "");
37453 + }
37454 + }
37455 +
37456 + /* Initialize the Connection timeout timer. */
37457 + hcd->conn_timer = DWC_TIMER_ALLOC("Connection timer",
37458 + dwc_otg_hcd_connect_timeout, 0);
37459 +
37460 + printk(KERN_DEBUG "dwc_otg: Microframe scheduler %s\n", microframe_schedule ? "enabled":"disabled");
37461 + if (microframe_schedule)
37462 + init_hcd_usecs(hcd);
37463 +
37464 + /* Initialize reset tasklet. */
37465 + hcd->reset_tasklet = DWC_TASK_ALLOC("reset_tasklet", reset_tasklet_func, hcd);
37466 +
37467 + hcd->completion_tasklet = DWC_TASK_ALLOC("completion_tasklet",
37468 + completion_tasklet_func, hcd);
37469 +#ifdef DWC_DEV_SRPCAP
37470 + if (hcd->core_if->power_down == 2) {
37471 + /* Initialize Power on timer for Host power up in case hibernation */
37472 + hcd->core_if->pwron_timer = DWC_TIMER_ALLOC("PWRON TIMER",
37473 + dwc_otg_hcd_power_up, core_if);
37474 + }
37475 +#endif
37476 +
37477 + /*
37478 + * Allocate space for storing data on status transactions. Normally no
37479 + * data is sent, but this space acts as a bit bucket. This must be
37480 + * done after usb_add_hcd since that function allocates the DMA buffer
37481 + * pool.
37482 + */
37483 + if (hcd->core_if->dma_enable) {
37484 + hcd->status_buf =
37485 + DWC_DMA_ALLOC(dev, DWC_OTG_HCD_STATUS_BUF_SIZE,
37486 + &hcd->status_buf_dma);
37487 + } else {
37488 + hcd->status_buf = DWC_ALLOC(DWC_OTG_HCD_STATUS_BUF_SIZE);
37489 + }
37490 + if (!hcd->status_buf) {
37491 + retval = -DWC_E_NO_MEMORY;
37492 + DWC_ERROR("%s: status_buf allocation failed\n", __func__);
37493 + dwc_otg_hcd_free(hcd);
37494 + goto out;
37495 + }
37496 +
37497 + hcd->otg_port = 1;
37498 + hcd->frame_list = NULL;
37499 + hcd->frame_list_dma = 0;
37500 + hcd->periodic_qh_count = 0;
37501 +
37502 + DWC_MEMSET(hcd->hub_port, 0, sizeof(hcd->hub_port));
37503 +#ifdef FIQ_DEBUG
37504 + DWC_MEMSET(hcd->hub_port_alloc, -1, sizeof(hcd->hub_port_alloc));
37505 +#endif
37506 +
37507 +out:
37508 + return retval;
37509 +}
37510 +
37511 +void dwc_otg_hcd_remove(dwc_otg_hcd_t * hcd)
37512 +{
37513 + /* Turn off all host-specific interrupts. */
37514 + dwc_otg_disable_host_interrupts(hcd->core_if);
37515 +
37516 + dwc_otg_hcd_free(hcd);
37517 +}
37518 +
37519 +/**
37520 + * Initializes dynamic portions of the DWC_otg HCD state.
37521 + */
37522 +static void dwc_otg_hcd_reinit(dwc_otg_hcd_t * hcd)
37523 +{
37524 + int num_channels;
37525 + int i;
37526 + dwc_hc_t *channel;
37527 + dwc_hc_t *channel_tmp;
37528 +
37529 + hcd->flags.d32 = 0;
37530 +
37531 + hcd->non_periodic_qh_ptr = &hcd->non_periodic_sched_active;
37532 + if (!microframe_schedule) {
37533 + hcd->non_periodic_channels = 0;
37534 + hcd->periodic_channels = 0;
37535 + } else {
37536 + hcd->available_host_channels = hcd->core_if->core_params->host_channels;
37537 + }
37538 + /*
37539 + * Put all channels in the free channel list and clean up channel
37540 + * states.
37541 + */
37542 + DWC_CIRCLEQ_FOREACH_SAFE(channel, channel_tmp,
37543 + &hcd->free_hc_list, hc_list_entry) {
37544 + DWC_CIRCLEQ_REMOVE(&hcd->free_hc_list, channel, hc_list_entry);
37545 + }
37546 +
37547 + num_channels = hcd->core_if->core_params->host_channels;
37548 + for (i = 0; i < num_channels; i++) {
37549 + channel = hcd->hc_ptr_array[i];
37550 + DWC_CIRCLEQ_INSERT_TAIL(&hcd->free_hc_list, channel,
37551 + hc_list_entry);
37552 + dwc_otg_hc_cleanup(hcd->core_if, channel);
37553 + }
37554 +
37555 + /* Initialize the DWC core for host mode operation. */
37556 + dwc_otg_core_host_init(hcd->core_if);
37557 +
37558 + /* Set core_if's lock pointer to the hcd->lock */
37559 + hcd->core_if->lock = hcd->lock;
37560 +}
37561 +
37562 +/**
37563 + * Assigns transactions from a QTD to a free host channel and initializes the
37564 + * host channel to perform the transactions. The host channel is removed from
37565 + * the free list.
37566 + *
37567 + * @param hcd The HCD state structure.
37568 + * @param qh Transactions from the first QTD for this QH are selected and
37569 + * assigned to a free host channel.
37570 + */
37571 +static void assign_and_init_hc(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
37572 +{
37573 + dwc_hc_t *hc;
37574 + dwc_otg_qtd_t *qtd;
37575 + dwc_otg_hcd_urb_t *urb;
37576 + void* ptr = NULL;
37577 + uint32_t intr_enable;
37578 + unsigned long flags;
37579 + gintmsk_data_t gintmsk = { .d32 = 0, };
37580 + struct device *dev = dwc_otg_hcd_to_dev(hcd);
37581 +
37582 + qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
37583 +
37584 + urb = qtd->urb;
37585 +
37586 + DWC_DEBUGPL(DBG_HCDV, "%s(%p,%p) - urb %x, actual_length %d\n", __func__, hcd, qh, (unsigned int)urb, urb->actual_length);
37587 +
37588 + if (((urb->actual_length < 0) || (urb->actual_length > urb->length)) && !dwc_otg_hcd_is_pipe_in(&urb->pipe_info))
37589 + urb->actual_length = urb->length;
37590 +
37591 +
37592 + hc = DWC_CIRCLEQ_FIRST(&hcd->free_hc_list);
37593 +
37594 + /* Remove the host channel from the free list. */
37595 + DWC_CIRCLEQ_REMOVE_INIT(&hcd->free_hc_list, hc, hc_list_entry);
37596 +
37597 + qh->channel = hc;
37598 +
37599 + qtd->in_process = 1;
37600 +
37601 + /*
37602 + * Use usb_pipedevice to determine device address. This address is
37603 + * 0 before the SET_ADDRESS command and the correct address afterward.
37604 + */
37605 + hc->dev_addr = dwc_otg_hcd_get_dev_addr(&urb->pipe_info);
37606 + hc->ep_num = dwc_otg_hcd_get_ep_num(&urb->pipe_info);
37607 + hc->speed = qh->dev_speed;
37608 + hc->max_packet = dwc_max_packet(qh->maxp);
37609 +
37610 + hc->xfer_started = 0;
37611 + hc->halt_status = DWC_OTG_HC_XFER_NO_HALT_STATUS;
37612 + hc->error_state = (qtd->error_count > 0);
37613 + hc->halt_on_queue = 0;
37614 + hc->halt_pending = 0;
37615 + hc->requests = 0;
37616 +
37617 + /*
37618 + * The following values may be modified in the transfer type section
37619 + * below. The xfer_len value may be reduced when the transfer is
37620 + * started to accommodate the max widths of the XferSize and PktCnt
37621 + * fields in the HCTSIZn register.
37622 + */
37623 +
37624 + hc->ep_is_in = (dwc_otg_hcd_is_pipe_in(&urb->pipe_info) != 0);
37625 + if (hc->ep_is_in) {
37626 + hc->do_ping = 0;
37627 + } else {
37628 + hc->do_ping = qh->ping_state;
37629 + }
37630 +
37631 + hc->data_pid_start = qh->data_toggle;
37632 + hc->multi_count = 1;
37633 +
37634 + if (hcd->core_if->dma_enable) {
37635 + hc->xfer_buff = (uint8_t *) urb->dma + urb->actual_length;
37636 +
37637 + /* For non-dword aligned case */
37638 + if (((unsigned long)hc->xfer_buff & 0x3)
37639 + && !hcd->core_if->dma_desc_enable) {
37640 + ptr = (uint8_t *) urb->buf + urb->actual_length;
37641 + }
37642 + } else {
37643 + hc->xfer_buff = (uint8_t *) urb->buf + urb->actual_length;
37644 + }
37645 + hc->xfer_len = urb->length - urb->actual_length;
37646 + hc->xfer_count = 0;
37647 +
37648 + /*
37649 + * Set the split attributes
37650 + */
37651 + hc->do_split = 0;
37652 + if (qh->do_split) {
37653 + uint32_t hub_addr, port_addr;
37654 + hc->do_split = 1;
37655 + hc->start_pkt_count = 1;
37656 + hc->xact_pos = qtd->isoc_split_pos;
37657 + /* We don't need to do complete splits anymore */
37658 +// if(fiq_fsm_enable)
37659 + if (0)
37660 + hc->complete_split = qtd->complete_split = 0;
37661 + else
37662 + hc->complete_split = qtd->complete_split;
37663 +
37664 + hcd->fops->hub_info(hcd, urb->priv, &hub_addr, &port_addr);
37665 + hc->hub_addr = (uint8_t) hub_addr;
37666 + hc->port_addr = (uint8_t) port_addr;
37667 + }
37668 +
37669 + switch (dwc_otg_hcd_get_pipe_type(&urb->pipe_info)) {
37670 + case UE_CONTROL:
37671 + hc->ep_type = DWC_OTG_EP_TYPE_CONTROL;
37672 + switch (qtd->control_phase) {
37673 + case DWC_OTG_CONTROL_SETUP:
37674 + DWC_DEBUGPL(DBG_HCDV, " Control setup transaction\n");
37675 + hc->do_ping = 0;
37676 + hc->ep_is_in = 0;
37677 + hc->data_pid_start = DWC_OTG_HC_PID_SETUP;
37678 + if (hcd->core_if->dma_enable) {
37679 + hc->xfer_buff = (uint8_t *) urb->setup_dma;
37680 + } else {
37681 + hc->xfer_buff = (uint8_t *) urb->setup_packet;
37682 + }
37683 + hc->xfer_len = 8;
37684 + ptr = NULL;
37685 + break;
37686 + case DWC_OTG_CONTROL_DATA:
37687 + DWC_DEBUGPL(DBG_HCDV, " Control data transaction\n");
37688 + hc->data_pid_start = qtd->data_toggle;
37689 + break;
37690 + case DWC_OTG_CONTROL_STATUS:
37691 + /*
37692 + * Direction is opposite of data direction or IN if no
37693 + * data.
37694 + */
37695 + DWC_DEBUGPL(DBG_HCDV, " Control status transaction\n");
37696 + if (urb->length == 0) {
37697 + hc->ep_is_in = 1;
37698 + } else {
37699 + hc->ep_is_in =
37700 + dwc_otg_hcd_is_pipe_out(&urb->pipe_info);
37701 + }
37702 + if (hc->ep_is_in) {
37703 + hc->do_ping = 0;
37704 + }
37705 +
37706 + hc->data_pid_start = DWC_OTG_HC_PID_DATA1;
37707 +
37708 + hc->xfer_len = 0;
37709 + if (hcd->core_if->dma_enable) {
37710 + hc->xfer_buff = (uint8_t *) hcd->status_buf_dma;
37711 + } else {
37712 + hc->xfer_buff = (uint8_t *) hcd->status_buf;
37713 + }
37714 + ptr = NULL;
37715 + break;
37716 + }
37717 + break;
37718 + case UE_BULK:
37719 + hc->ep_type = DWC_OTG_EP_TYPE_BULK;
37720 + break;
37721 + case UE_INTERRUPT:
37722 + hc->ep_type = DWC_OTG_EP_TYPE_INTR;
37723 + break;
37724 + case UE_ISOCHRONOUS:
37725 + {
37726 + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
37727 +
37728 + hc->ep_type = DWC_OTG_EP_TYPE_ISOC;
37729 +
37730 + if (hcd->core_if->dma_desc_enable)
37731 + break;
37732 +
37733 + frame_desc = &urb->iso_descs[qtd->isoc_frame_index];
37734 +
37735 + frame_desc->status = 0;
37736 +
37737 + if (hcd->core_if->dma_enable) {
37738 + hc->xfer_buff = (uint8_t *) urb->dma;
37739 + } else {
37740 + hc->xfer_buff = (uint8_t *) urb->buf;
37741 + }
37742 + hc->xfer_buff +=
37743 + frame_desc->offset + qtd->isoc_split_offset;
37744 + hc->xfer_len =
37745 + frame_desc->length - qtd->isoc_split_offset;
37746 +
37747 + /* For non-dword aligned buffers */
37748 + if (((unsigned long)hc->xfer_buff & 0x3)
37749 + && hcd->core_if->dma_enable) {
37750 + ptr =
37751 + (uint8_t *) urb->buf + frame_desc->offset +
37752 + qtd->isoc_split_offset;
37753 + } else
37754 + ptr = NULL;
37755 +
37756 + if (hc->xact_pos == DWC_HCSPLIT_XACTPOS_ALL) {
37757 + if (hc->xfer_len <= 188) {
37758 + hc->xact_pos = DWC_HCSPLIT_XACTPOS_ALL;
37759 + } else {
37760 + hc->xact_pos =
37761 + DWC_HCSPLIT_XACTPOS_BEGIN;
37762 + }
37763 + }
37764 + }
37765 + break;
37766 + }
37767 + /* non DWORD-aligned buffer case */
37768 + if (ptr) {
37769 + uint32_t buf_size;
37770 + if (hc->ep_type != DWC_OTG_EP_TYPE_ISOC) {
37771 + buf_size = hcd->core_if->core_params->max_transfer_size;
37772 + } else {
37773 + buf_size = 4096;
37774 + }
37775 + if (!qh->dw_align_buf) {
37776 + qh->dw_align_buf = DWC_DMA_ALLOC_ATOMIC(dev, buf_size,
37777 + &qh->dw_align_buf_dma);
37778 + if (!qh->dw_align_buf) {
37779 + DWC_ERROR
37780 + ("%s: Failed to allocate memory to handle "
37781 + "non-dword aligned buffer case\n",
37782 + __func__);
37783 + return;
37784 + }
37785 + }
37786 + if (!hc->ep_is_in) {
37787 + dwc_memcpy(qh->dw_align_buf, ptr, hc->xfer_len);
37788 + }
37789 + hc->align_buff = qh->dw_align_buf_dma;
37790 + } else {
37791 + hc->align_buff = 0;
37792 + }
37793 +
37794 + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
37795 + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
37796 + /*
37797 + * This value may be modified when the transfer is started to
37798 + * reflect the actual transfer length.
37799 + */
37800 + hc->multi_count = dwc_hb_mult(qh->maxp);
37801 + }
37802 +
37803 + if (hcd->core_if->dma_desc_enable)
37804 + hc->desc_list_addr = qh->desc_list_dma;
37805 +
37806 + dwc_otg_hc_init(hcd->core_if, hc);
37807 +
37808 + local_irq_save(flags);
37809 +
37810 + if (fiq_enable) {
37811 + local_fiq_disable();
37812 + fiq_fsm_spin_lock(&hcd->fiq_state->lock);
37813 + }
37814 +
37815 + /* Enable the top level host channel interrupt. */
37816 + intr_enable = (1 << hc->hc_num);
37817 + DWC_MODIFY_REG32(&hcd->core_if->host_if->host_global_regs->haintmsk, 0, intr_enable);
37818 +
37819 + /* Make sure host channel interrupts are enabled. */
37820 + gintmsk.b.hcintr = 1;
37821 + DWC_MODIFY_REG32(&hcd->core_if->core_global_regs->gintmsk, 0, gintmsk.d32);
37822 +
37823 + if (fiq_enable) {
37824 + fiq_fsm_spin_unlock(&hcd->fiq_state->lock);
37825 + local_fiq_enable();
37826 + }
37827 +
37828 + local_irq_restore(flags);
37829 + hc->qh = qh;
37830 +}
37831 +
37832 +
37833 +/**
37834 + * fiq_fsm_transaction_suitable() - Test a QH for compatibility with the FIQ
37835 + * @hcd: Pointer to the dwc_otg_hcd struct
37836 + * @qh: pointer to the endpoint's queue head
37837 + *
37838 + * Transaction start/end control flow is grafted onto the existing dwc_otg
37839 + * mechanisms, to avoid spaghettifying the functions more than they already are.
37840 + * This function's eligibility check is altered by debug parameter.
37841 + *
37842 + * Returns: 0 for unsuitable, 1 implies the FIQ can be enabled for this transaction.
37843 + */
37844 +
37845 +int fiq_fsm_transaction_suitable(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh)
37846 +{
37847 + if (qh->do_split) {
37848 + switch (qh->ep_type) {
37849 + case UE_CONTROL:
37850 + case UE_BULK:
37851 + if (fiq_fsm_mask & (1 << 0))
37852 + return 1;
37853 + break;
37854 + case UE_INTERRUPT:
37855 + case UE_ISOCHRONOUS:
37856 + if (fiq_fsm_mask & (1 << 1))
37857 + return 1;
37858 + break;
37859 + default:
37860 + break;
37861 + }
37862 + } else if (qh->ep_type == UE_ISOCHRONOUS) {
37863 + if (fiq_fsm_mask & (1 << 2)) {
37864 + /* ISOCH support. We test for compatibility:
37865 + * - DWORD aligned buffers
37866 + * - Must be at least 2 transfers (otherwise pointless to use the FIQ)
37867 + * If yes, then the fsm enqueue function will handle the state machine setup.
37868 + */
37869 + dwc_otg_qtd_t *qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
37870 + dwc_otg_hcd_urb_t *urb = qtd->urb;
37871 + dwc_dma_t ptr;
37872 + int i;
37873 +
37874 + if (urb->packet_count < 2)
37875 + return 0;
37876 + for (i = 0; i < urb->packet_count; i++) {
37877 + ptr = urb->dma + urb->iso_descs[i].offset;
37878 + if (ptr & 0x3)
37879 + return 0;
37880 + }
37881 + return 1;
37882 + }
37883 + }
37884 + return 0;
37885 +}
37886 +
37887 +/**
37888 + * fiq_fsm_setup_periodic_dma() - Set up DMA bounce buffers
37889 + * @hcd: Pointer to the dwc_otg_hcd struct
37890 + * @qh: Pointer to the endpoint's queue head
37891 + *
37892 + * Periodic split transactions are transmitted modulo 188 bytes.
37893 + * This necessitates slicing data up into buckets for isochronous out
37894 + * and fixing up the DMA address for all IN transfers.
37895 + *
37896 + * Returns 1 if the DMA bounce buffers have been used, 0 if the default
37897 + * HC buffer has been used.
37898 + */
37899 +int fiq_fsm_setup_periodic_dma(dwc_otg_hcd_t *hcd, struct fiq_channel_state *st, dwc_otg_qh_t *qh)
37900 + {
37901 + int frame_length, i = 0;
37902 + uint8_t *ptr = NULL;
37903 + dwc_hc_t *hc = qh->channel;
37904 + struct fiq_dma_blob *blob;
37905 + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
37906 +
37907 + for (i = 0; i < 6; i++) {
37908 + st->dma_info.slot_len[i] = 255;
37909 + }
37910 + st->dma_info.index = 0;
37911 + i = 0;
37912 + if (hc->ep_is_in) {
37913 + /*
37914 + * Set dma_regs to bounce buffer. FIQ will update the
37915 + * state depending on transaction progress.
37916 + */
37917 + blob = (struct fiq_dma_blob *) hcd->fiq_state->dma_base;
37918 + st->hcdma_copy.d32 = (uint32_t) &blob->channel[hc->hc_num].index[0].buf[0];
37919 + /* Calculate the max number of CSPLITS such that the FIQ can time out
37920 + * a transaction if it fails.
37921 + */
37922 + frame_length = st->hcchar_copy.b.mps;
37923 + do {
37924 + i++;
37925 + frame_length -= 188;
37926 + } while (frame_length >= 0);
37927 + st->nrpackets = i;
37928 + return 1;
37929 + } else {
37930 + if (qh->ep_type == UE_ISOCHRONOUS) {
37931 +
37932 + dwc_otg_qtd_t *qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
37933 +
37934 + frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
37935 + frame_length = frame_desc->length;
37936 +
37937 + /* Virtual address for bounce buffers */
37938 + blob = hcd->fiq_dmab;
37939 +
37940 + ptr = qtd->urb->buf + frame_desc->offset;
37941 + if (frame_length == 0) {
37942 + /*
37943 + * for isochronous transactions, we must still transmit a packet
37944 + * even if the length is zero.
37945 + */
37946 + st->dma_info.slot_len[0] = 0;
37947 + st->nrpackets = 1;
37948 + } else {
37949 + do {
37950 + if (frame_length <= 188) {
37951 + dwc_memcpy(&blob->channel[hc->hc_num].index[i].buf[0], ptr, frame_length);
37952 + st->dma_info.slot_len[i] = frame_length;
37953 + ptr += frame_length;
37954 + } else {
37955 + dwc_memcpy(&blob->channel[hc->hc_num].index[i].buf[0], ptr, 188);
37956 + st->dma_info.slot_len[i] = 188;
37957 + ptr += 188;
37958 + }
37959 + i++;
37960 + frame_length -= 188;
37961 + } while (frame_length > 0);
37962 + st->nrpackets = i;
37963 + }
37964 + ptr = qtd->urb->buf + frame_desc->offset;
37965 + /* Point the HC at the DMA address of the bounce buffers */
37966 + blob = (struct fiq_dma_blob *) hcd->fiq_state->dma_base;
37967 + st->hcdma_copy.d32 = (uint32_t) &blob->channel[hc->hc_num].index[0].buf[0];
37968 +
37969 + /* fixup xfersize to the actual packet size */
37970 + st->hctsiz_copy.b.pid = 0;
37971 + st->hctsiz_copy.b.xfersize = st->dma_info.slot_len[0];
37972 + return 1;
37973 + } else {
37974 + /* For interrupt, single OUT packet required, goes in the SSPLIT from hc_buff. */
37975 + return 0;
37976 + }
37977 + }
37978 +}
37979 +
37980 +/**
37981 + * fiq_fsm_np_tt_contended() - Avoid performing contended non-periodic transfers
37982 + * @hcd: Pointer to the dwc_otg_hcd struct
37983 + * @qh: Pointer to the endpoint's queue head
37984 + *
37985 + * Certain hub chips don't differentiate between IN and OUT non-periodic pipes
37986 + * with the same endpoint number. If transfers get completed out of order
37987 + * (disregarding the direction token) then the hub can lock up
37988 + * or return erroneous responses.
37989 + *
37990 + * Returns 1 if initiating the transfer would cause contention, 0 otherwise.
37991 + */
37992 +int fiq_fsm_np_tt_contended(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh)
37993 +{
37994 + int i;
37995 + struct fiq_channel_state *st;
37996 + int dev_addr = qh->channel->dev_addr;
37997 + int ep_num = qh->channel->ep_num;
37998 + for (i = 0; i < hcd->core_if->core_params->host_channels; i++) {
37999 + if (i == qh->channel->hc_num)
38000 + continue;
38001 + st = &hcd->fiq_state->channel[i];
38002 + switch (st->fsm) {
38003 + case FIQ_NP_SSPLIT_STARTED:
38004 + case FIQ_NP_SSPLIT_RETRY:
38005 + case FIQ_NP_SSPLIT_PENDING:
38006 + case FIQ_NP_OUT_CSPLIT_RETRY:
38007 + case FIQ_NP_IN_CSPLIT_RETRY:
38008 + if (st->hcchar_copy.b.devaddr == dev_addr &&
38009 + st->hcchar_copy.b.epnum == ep_num)
38010 + return 1;
38011 + break;
38012 + default:
38013 + break;
38014 + }
38015 + }
38016 + return 0;
38017 +}
38018 +
38019 +/*
38020 + * Pushing a periodic request into the queue near the EOF1 point
38021 + * in a microframe causes erroneous behaviour (frmovrun) interrupt.
38022 + * Usually, the request goes out on the bus causing a transfer but
38023 + * the core does not transfer the data to memory.
38024 + * This guard interval (in number of 60MHz clocks) is required which
38025 + * must cater for CPU latency between reading the value and enabling
38026 + * the channel.
38027 + */
38028 +#define PERIODIC_FRREM_BACKOFF 1000
38029 +
38030 +int fiq_fsm_queue_isoc_transaction(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh)
38031 +{
38032 + dwc_hc_t *hc = qh->channel;
38033 + dwc_otg_hc_regs_t *hc_regs = hcd->core_if->host_if->hc_regs[hc->hc_num];
38034 + dwc_otg_qtd_t *qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
38035 + int frame;
38036 + struct fiq_channel_state *st = &hcd->fiq_state->channel[hc->hc_num];
38037 + int xfer_len, nrpackets;
38038 + hcdma_data_t hcdma;
38039 + hfnum_data_t hfnum;
38040 +
38041 + if (st->fsm != FIQ_PASSTHROUGH)
38042 + return 0;
38043 +
38044 + st->nr_errors = 0;
38045 +
38046 + st->hcchar_copy.d32 = 0;
38047 + st->hcchar_copy.b.mps = hc->max_packet;
38048 + st->hcchar_copy.b.epdir = hc->ep_is_in;
38049 + st->hcchar_copy.b.devaddr = hc->dev_addr;
38050 + st->hcchar_copy.b.epnum = hc->ep_num;
38051 + st->hcchar_copy.b.eptype = hc->ep_type;
38052 +
38053 + st->hcintmsk_copy.b.chhltd = 1;
38054 +
38055 + frame = dwc_otg_hcd_get_frame_number(hcd);
38056 + st->hcchar_copy.b.oddfrm = (frame & 0x1) ? 0 : 1;
38057 +
38058 + st->hcchar_copy.b.lspddev = 0;
38059 + /* Enable the channel later as a final register write. */
38060 +
38061 + st->hcsplt_copy.d32 = 0;
38062 +
38063 + st->hs_isoc_info.iso_desc = (struct dwc_otg_hcd_iso_packet_desc *) &qtd->urb->iso_descs;
38064 + st->hs_isoc_info.nrframes = qtd->urb->packet_count;
38065 + /* grab the next DMA address offset from the array */
38066 + st->hcdma_copy.d32 = qtd->urb->dma;
38067 + hcdma.d32 = st->hcdma_copy.d32 + st->hs_isoc_info.iso_desc[0].offset;
38068 +
38069 + /* We need to set multi_count. This is a bit tricky - has to be set per-transaction as
38070 + * the core needs to be told to send the correct number. Caution: for IN transfers,
38071 + * this is always set to the maximum size of the endpoint. */
38072 + xfer_len = st->hs_isoc_info.iso_desc[0].length;
38073 + nrpackets = (xfer_len + st->hcchar_copy.b.mps - 1) / st->hcchar_copy.b.mps;
38074 + if (nrpackets == 0)
38075 + nrpackets = 1;
38076 + st->hcchar_copy.b.multicnt = nrpackets;
38077 + st->hctsiz_copy.b.pktcnt = nrpackets;
38078 +
38079 + /* Initial PID also needs to be set */
38080 + if (st->hcchar_copy.b.epdir == 0) {
38081 + st->hctsiz_copy.b.xfersize = xfer_len;
38082 + switch (st->hcchar_copy.b.multicnt) {
38083 + case 1:
38084 + st->hctsiz_copy.b.pid = DWC_PID_DATA0;
38085 + break;
38086 + case 2:
38087 + case 3:
38088 + st->hctsiz_copy.b.pid = DWC_PID_MDATA;
38089 + break;
38090 + }
38091 +
38092 + } else {
38093 + st->hctsiz_copy.b.xfersize = nrpackets * st->hcchar_copy.b.mps;
38094 + switch (st->hcchar_copy.b.multicnt) {
38095 + case 1:
38096 + st->hctsiz_copy.b.pid = DWC_PID_DATA0;
38097 + break;
38098 + case 2:
38099 + st->hctsiz_copy.b.pid = DWC_PID_DATA1;
38100 + break;
38101 + case 3:
38102 + st->hctsiz_copy.b.pid = DWC_PID_DATA2;
38103 + break;
38104 + }
38105 + }
38106 +
38107 + st->hs_isoc_info.stride = qh->interval;
38108 + st->uframe_sleeps = 0;
38109 +
38110 + fiq_print(FIQDBG_INT, hcd->fiq_state, "FSMQ %01d ", hc->hc_num);
38111 + fiq_print(FIQDBG_INT, hcd->fiq_state, "%08x", st->hcchar_copy.d32);
38112 + fiq_print(FIQDBG_INT, hcd->fiq_state, "%08x", st->hctsiz_copy.d32);
38113 + fiq_print(FIQDBG_INT, hcd->fiq_state, "%08x", st->hcdma_copy.d32);
38114 + hfnum.d32 = DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hfnum);
38115 + local_fiq_disable();
38116 + fiq_fsm_spin_lock(&hcd->fiq_state->lock);
38117 + DWC_WRITE_REG32(&hc_regs->hctsiz, st->hctsiz_copy.d32);
38118 + DWC_WRITE_REG32(&hc_regs->hcsplt, st->hcsplt_copy.d32);
38119 + DWC_WRITE_REG32(&hc_regs->hcdma, st->hcdma_copy.d32);
38120 + DWC_WRITE_REG32(&hc_regs->hcchar, st->hcchar_copy.d32);
38121 + DWC_WRITE_REG32(&hc_regs->hcintmsk, st->hcintmsk_copy.d32);
38122 + if (hfnum.b.frrem < PERIODIC_FRREM_BACKOFF) {
38123 + /* Prevent queueing near EOF1. Bad things happen if a periodic
38124 + * split transaction is queued very close to EOF. SOF interrupt handler
38125 + * will wake this channel at the next interrupt.
38126 + */
38127 + st->fsm = FIQ_HS_ISOC_SLEEPING;
38128 + st->uframe_sleeps = 1;
38129 + } else {
38130 + st->fsm = FIQ_HS_ISOC_TURBO;
38131 + st->hcchar_copy.b.chen = 1;
38132 + DWC_WRITE_REG32(&hc_regs->hcchar, st->hcchar_copy.d32);
38133 + }
38134 + mb();
38135 + st->hcchar_copy.b.chen = 0;
38136 + fiq_fsm_spin_unlock(&hcd->fiq_state->lock);
38137 + local_fiq_enable();
38138 + return 0;
38139 +}
38140 +
38141 +
38142 +/**
38143 + * fiq_fsm_queue_split_transaction() - Set up a host channel and FIQ state
38144 + * @hcd: Pointer to the dwc_otg_hcd struct
38145 + * @qh: Pointer to the endpoint's queue head
38146 + *
38147 + * This overrides the dwc_otg driver's normal method of queueing a transaction.
38148 + * Called from dwc_otg_hcd_queue_transactions(), this performs specific setup
38149 + * for the nominated host channel.
38150 + *
38151 + * For periodic transfers, it also peeks at the FIQ state to see if an immediate
38152 + * start is possible. If not, then the FIQ is left to start the transfer.
38153 + */
38154 +int fiq_fsm_queue_split_transaction(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh)
38155 +{
38156 + int start_immediate = 1, i;
38157 + hfnum_data_t hfnum;
38158 + dwc_hc_t *hc = qh->channel;
38159 + dwc_otg_hc_regs_t *hc_regs = hcd->core_if->host_if->hc_regs[hc->hc_num];
38160 + /* Program HC registers, setup FIQ_state, examine FIQ if periodic, start transfer (not if uframe 5) */
38161 + int hub_addr, port_addr, frame, uframe;
38162 + struct fiq_channel_state *st = &hcd->fiq_state->channel[hc->hc_num];
38163 +
38164 + /*
38165 + * Non-periodic channel assignments stay in the non_periodic_active queue.
38166 + * Therefore we get repeatedly called until the FIQ's done processing this channel.
38167 + */
38168 + if (qh->channel->xfer_started == 1)
38169 + return 0;
38170 +
38171 + if (st->fsm != FIQ_PASSTHROUGH) {
38172 + pr_warn_ratelimited("%s:%d: Queue called for an active channel\n", __func__, __LINE__);
38173 + return 0;
38174 + }
38175 +
38176 + qh->channel->xfer_started = 1;
38177 +
38178 + st->nr_errors = 0;
38179 +
38180 + st->hcchar_copy.d32 = 0;
38181 + st->hcchar_copy.b.mps = hc->max_packet;
38182 + st->hcchar_copy.b.epdir = hc->ep_is_in;
38183 + st->hcchar_copy.b.devaddr = hc->dev_addr;
38184 + st->hcchar_copy.b.epnum = hc->ep_num;
38185 + st->hcchar_copy.b.eptype = hc->ep_type;
38186 + if (hc->ep_type & 0x1) {
38187 + if (hc->ep_is_in)
38188 + st->hcchar_copy.b.multicnt = 3;
38189 + else
38190 + /* Docs say set this to 1, but driver sets to 0! */
38191 + st->hcchar_copy.b.multicnt = 0;
38192 + } else {
38193 + st->hcchar_copy.b.multicnt = 1;
38194 + st->hcchar_copy.b.oddfrm = 0;
38195 + }
38196 + st->hcchar_copy.b.lspddev = (hc->speed == DWC_OTG_EP_SPEED_LOW) ? 1 : 0;
38197 + /* Enable the channel later as a final register write. */
38198 +
38199 + st->hcsplt_copy.d32 = 0;
38200 + if(qh->do_split) {
38201 + hcd->fops->hub_info(hcd, DWC_CIRCLEQ_FIRST(&qh->qtd_list)->urb->priv, &hub_addr, &port_addr);
38202 + st->hcsplt_copy.b.compsplt = 0;
38203 + st->hcsplt_copy.b.spltena = 1;
38204 + // XACTPOS is for isoc-out only but needs initialising anyway.
38205 + st->hcsplt_copy.b.xactpos = ISOC_XACTPOS_ALL;
38206 + if((qh->ep_type == DWC_OTG_EP_TYPE_ISOC) && (!qh->ep_is_in)) {
38207 + /* For packetsize 0 < L < 188, ISOC_XACTPOS_ALL.
38208 + * for longer than this, ISOC_XACTPOS_BEGIN and the FIQ
38209 + * will update as necessary.
38210 + */
38211 + if (hc->xfer_len > 188) {
38212 + st->hcsplt_copy.b.xactpos = ISOC_XACTPOS_BEGIN;
38213 + }
38214 + }
38215 + st->hcsplt_copy.b.hubaddr = (uint8_t) hub_addr;
38216 + st->hcsplt_copy.b.prtaddr = (uint8_t) port_addr;
38217 + st->hub_addr = hub_addr;
38218 + st->port_addr = port_addr;
38219 + }
38220 +
38221 + st->hctsiz_copy.d32 = 0;
38222 + st->hctsiz_copy.b.dopng = 0;
38223 + st->hctsiz_copy.b.pid = hc->data_pid_start;
38224 +
38225 + if (hc->ep_is_in || (hc->xfer_len > hc->max_packet)) {
38226 + hc->xfer_len = hc->max_packet;
38227 + } else if (!hc->ep_is_in && (hc->xfer_len > 188)) {
38228 + hc->xfer_len = 188;
38229 + }
38230 + st->hctsiz_copy.b.xfersize = hc->xfer_len;
38231 +
38232 + st->hctsiz_copy.b.pktcnt = 1;
38233 +
38234 + if (hc->ep_type & 0x1) {
38235 + /*
38236 + * For potentially multi-packet transfers, must use the DMA bounce buffers. For IN transfers,
38237 + * the DMA address is the address of the first 188byte slot buffer in the bounce buffer array.
38238 + * For multi-packet OUT transfers, we need to copy the data into the bounce buffer array so the FIQ can punt
38239 + * the right address out as necessary. hc->xfer_buff and hc->xfer_len have already been set
38240 + * in assign_and_init_hc(), but this is for the eventual transaction completion only. The FIQ
38241 + * must not touch internal driver state.
38242 + */
38243 + if(!fiq_fsm_setup_periodic_dma(hcd, st, qh)) {
38244 + if (hc->align_buff) {
38245 + st->hcdma_copy.d32 = hc->align_buff;
38246 + } else {
38247 + st->hcdma_copy.d32 = ((unsigned long) hc->xfer_buff & 0xFFFFFFFF);
38248 + }
38249 + }
38250 + } else {
38251 + if (hc->align_buff) {
38252 + st->hcdma_copy.d32 = hc->align_buff;
38253 + } else {
38254 + st->hcdma_copy.d32 = ((unsigned long) hc->xfer_buff & 0xFFFFFFFF);
38255 + }
38256 + }
38257 + /* The FIQ depends upon no other interrupts being enabled except channel halt.
38258 + * Fixup channel interrupt mask. */
38259 + st->hcintmsk_copy.d32 = 0;
38260 + st->hcintmsk_copy.b.chhltd = 1;
38261 + st->hcintmsk_copy.b.ahberr = 1;
38262 +
38263 + /* Hack courtesy of FreeBSD: apparently forcing Interrupt Split transactions
38264 + * as Control puts the transfer into the non-periodic request queue and the
38265 + * non-periodic handler in the hub. Makes things lots easier.
38266 + */
38267 + if ((fiq_fsm_mask & 0x8) && hc->ep_type == UE_INTERRUPT) {
38268 + st->hcchar_copy.b.multicnt = 0;
38269 + st->hcchar_copy.b.oddfrm = 0;
38270 + st->hcchar_copy.b.eptype = UE_CONTROL;
38271 + if (hc->align_buff) {
38272 + st->hcdma_copy.d32 = hc->align_buff;
38273 + } else {
38274 + st->hcdma_copy.d32 = ((unsigned long) hc->xfer_buff & 0xFFFFFFFF);
38275 + }
38276 + }
38277 + DWC_WRITE_REG32(&hc_regs->hcdma, st->hcdma_copy.d32);
38278 + DWC_WRITE_REG32(&hc_regs->hctsiz, st->hctsiz_copy.d32);
38279 + DWC_WRITE_REG32(&hc_regs->hcsplt, st->hcsplt_copy.d32);
38280 + DWC_WRITE_REG32(&hc_regs->hcchar, st->hcchar_copy.d32);
38281 + DWC_WRITE_REG32(&hc_regs->hcintmsk, st->hcintmsk_copy.d32);
38282 +
38283 + local_fiq_disable();
38284 + fiq_fsm_spin_lock(&hcd->fiq_state->lock);
38285 +
38286 + if (hc->ep_type & 0x1) {
38287 + hfnum.d32 = DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hfnum);
38288 + frame = (hfnum.b.frnum & ~0x7) >> 3;
38289 + uframe = hfnum.b.frnum & 0x7;
38290 + if (hfnum.b.frrem < PERIODIC_FRREM_BACKOFF) {
38291 + /* Prevent queueing near EOF1. Bad things happen if a periodic
38292 + * split transaction is queued very close to EOF.
38293 + */
38294 + start_immediate = 0;
38295 + } else if (uframe == 5) {
38296 + start_immediate = 0;
38297 + } else if (hc->ep_type == UE_ISOCHRONOUS && !hc->ep_is_in) {
38298 + start_immediate = 0;
38299 + } else if (hc->ep_is_in && fiq_fsm_too_late(hcd->fiq_state, hc->hc_num)) {
38300 + start_immediate = 0;
38301 + } else {
38302 + /* Search through all host channels to determine if a transaction
38303 + * is currently in progress */
38304 + for (i = 0; i < hcd->core_if->core_params->host_channels; i++) {
38305 + if (i == hc->hc_num || hcd->fiq_state->channel[i].fsm == FIQ_PASSTHROUGH)
38306 + continue;
38307 + switch (hcd->fiq_state->channel[i].fsm) {
38308 + /* TT is reserved for channels that are in the middle of a periodic
38309 + * split transaction.
38310 + */
38311 + case FIQ_PER_SSPLIT_STARTED:
38312 + case FIQ_PER_CSPLIT_WAIT:
38313 + case FIQ_PER_CSPLIT_NYET1:
38314 + case FIQ_PER_CSPLIT_POLL:
38315 + case FIQ_PER_ISO_OUT_ACTIVE:
38316 + case FIQ_PER_ISO_OUT_LAST:
38317 + if (hcd->fiq_state->channel[i].hub_addr == hub_addr &&
38318 + hcd->fiq_state->channel[i].port_addr == port_addr) {
38319 + start_immediate = 0;
38320 + }
38321 + break;
38322 + default:
38323 + break;
38324 + }
38325 + if (!start_immediate)
38326 + break;
38327 + }
38328 + }
38329 + }
38330 + if ((fiq_fsm_mask & 0x8) && hc->ep_type == UE_INTERRUPT)
38331 + start_immediate = 1;
38332 +
38333 + fiq_print(FIQDBG_INT, hcd->fiq_state, "FSMQ %01d %01d", hc->hc_num, start_immediate);
38334 + fiq_print(FIQDBG_INT, hcd->fiq_state, "%08d", hfnum.b.frrem);
38335 + //fiq_print(FIQDBG_INT, hcd->fiq_state, "H:%02dP:%02d", hub_addr, port_addr);
38336 + //fiq_print(FIQDBG_INT, hcd->fiq_state, "%08x", st->hctsiz_copy.d32);
38337 + //fiq_print(FIQDBG_INT, hcd->fiq_state, "%08x", st->hcdma_copy.d32);
38338 + switch (hc->ep_type) {
38339 + case UE_CONTROL:
38340 + case UE_BULK:
38341 + if (fiq_fsm_np_tt_contended(hcd, qh)) {
38342 + st->fsm = FIQ_NP_SSPLIT_PENDING;
38343 + start_immediate = 0;
38344 + } else {
38345 + st->fsm = FIQ_NP_SSPLIT_STARTED;
38346 + }
38347 + break;
38348 + case UE_ISOCHRONOUS:
38349 + if (hc->ep_is_in) {
38350 + if (start_immediate) {
38351 + st->fsm = FIQ_PER_SSPLIT_STARTED;
38352 + } else {
38353 + st->fsm = FIQ_PER_SSPLIT_QUEUED;
38354 + }
38355 + } else {
38356 + if (start_immediate) {
38357 + /* Single-isoc OUT packets don't require FIQ involvement */
38358 + if (st->nrpackets == 1) {
38359 + st->fsm = FIQ_PER_ISO_OUT_LAST;
38360 + } else {
38361 + st->fsm = FIQ_PER_ISO_OUT_ACTIVE;
38362 + }
38363 + } else {
38364 + st->fsm = FIQ_PER_ISO_OUT_PENDING;
38365 + }
38366 + }
38367 + break;
38368 + case UE_INTERRUPT:
38369 + if (fiq_fsm_mask & 0x8) {
38370 + if (fiq_fsm_np_tt_contended(hcd, qh)) {
38371 + st->fsm = FIQ_NP_SSPLIT_PENDING;
38372 + start_immediate = 0;
38373 + } else {
38374 + st->fsm = FIQ_NP_SSPLIT_STARTED;
38375 + }
38376 + } else if (start_immediate) {
38377 + st->fsm = FIQ_PER_SSPLIT_STARTED;
38378 + } else {
38379 + st->fsm = FIQ_PER_SSPLIT_QUEUED;
38380 + }
38381 + default:
38382 + break;
38383 + }
38384 + if (start_immediate) {
38385 + /* Set the oddfrm bit as close as possible to actual queueing */
38386 + frame = dwc_otg_hcd_get_frame_number(hcd);
38387 + st->expected_uframe = (frame + 1) & 0x3FFF;
38388 + st->hcchar_copy.b.oddfrm = (frame & 0x1) ? 0 : 1;
38389 + st->hcchar_copy.b.chen = 1;
38390 + DWC_WRITE_REG32(&hc_regs->hcchar, st->hcchar_copy.d32);
38391 + }
38392 + mb();
38393 + fiq_fsm_spin_unlock(&hcd->fiq_state->lock);
38394 + local_fiq_enable();
38395 + return 0;
38396 +}
38397 +
38398 +
38399 +/**
38400 + * This function selects transactions from the HCD transfer schedule and
38401 + * assigns them to available host channels. It is called from HCD interrupt
38402 + * handler functions.
38403 + *
38404 + * @param hcd The HCD state structure.
38405 + *
38406 + * @return The types of new transactions that were assigned to host channels.
38407 + */
38408 +dwc_otg_transaction_type_e dwc_otg_hcd_select_transactions(dwc_otg_hcd_t * hcd)
38409 +{
38410 + dwc_list_link_t *qh_ptr;
38411 + dwc_otg_qh_t *qh;
38412 + int num_channels;
38413 + dwc_otg_transaction_type_e ret_val = DWC_OTG_TRANSACTION_NONE;
38414 +
38415 +#ifdef DEBUG_HOST_CHANNELS
38416 + last_sel_trans_num_per_scheduled = 0;
38417 + last_sel_trans_num_nonper_scheduled = 0;
38418 + last_sel_trans_num_avail_hc_at_start = hcd->available_host_channels;
38419 +#endif /* DEBUG_HOST_CHANNELS */
38420 +
38421 + /* Process entries in the periodic ready list. */
38422 + qh_ptr = DWC_LIST_FIRST(&hcd->periodic_sched_ready);
38423 +
38424 + while (qh_ptr != &hcd->periodic_sched_ready &&
38425 + !DWC_CIRCLEQ_EMPTY(&hcd->free_hc_list)) {
38426 +
38427 + qh = DWC_LIST_ENTRY(qh_ptr, dwc_otg_qh_t, qh_list_entry);
38428 +
38429 + if (microframe_schedule) {
38430 + // Make sure we leave one channel for non periodic transactions.
38431 + if (hcd->available_host_channels <= 1) {
38432 + break;
38433 + }
38434 + hcd->available_host_channels--;
38435 +#ifdef DEBUG_HOST_CHANNELS
38436 + last_sel_trans_num_per_scheduled++;
38437 +#endif /* DEBUG_HOST_CHANNELS */
38438 + }
38439 + qh = DWC_LIST_ENTRY(qh_ptr, dwc_otg_qh_t, qh_list_entry);
38440 + assign_and_init_hc(hcd, qh);
38441 +
38442 + /*
38443 + * Move the QH from the periodic ready schedule to the
38444 + * periodic assigned schedule.
38445 + */
38446 + qh_ptr = DWC_LIST_NEXT(qh_ptr);
38447 + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_assigned,
38448 + &qh->qh_list_entry);
38449 + }
38450 +
38451 + /*
38452 + * Process entries in the inactive portion of the non-periodic
38453 + * schedule. Some free host channels may not be used if they are
38454 + * reserved for periodic transfers.
38455 + */
38456 + qh_ptr = hcd->non_periodic_sched_inactive.next;
38457 + num_channels = hcd->core_if->core_params->host_channels;
38458 + while (qh_ptr != &hcd->non_periodic_sched_inactive &&
38459 + (microframe_schedule || hcd->non_periodic_channels <
38460 + num_channels - hcd->periodic_channels) &&
38461 + !DWC_CIRCLEQ_EMPTY(&hcd->free_hc_list)) {
38462 +
38463 + qh = DWC_LIST_ENTRY(qh_ptr, dwc_otg_qh_t, qh_list_entry);
38464 + /*
38465 + * Check to see if this is a NAK'd retransmit, in which case ignore for retransmission
38466 + * we hold off on bulk retransmissions to reduce NAK interrupt overhead for full-speed
38467 + * cheeky devices that just hold off using NAKs
38468 + */
38469 + if (fiq_enable && nak_holdoff && qh->do_split) {
38470 + if (qh->nak_frame != 0xffff) {
38471 + uint16_t next_frame = dwc_frame_num_inc(qh->nak_frame, (qh->ep_type == UE_BULK) ? nak_holdoff : 8);
38472 + uint16_t frame = dwc_otg_hcd_get_frame_number(hcd);
38473 + if (dwc_frame_num_le(frame, next_frame)) {
38474 + if(dwc_frame_num_le(next_frame, hcd->fiq_state->next_sched_frame)) {
38475 + hcd->fiq_state->next_sched_frame = next_frame;
38476 + }
38477 + qh_ptr = DWC_LIST_NEXT(qh_ptr);
38478 + continue;
38479 + } else {
38480 + qh->nak_frame = 0xFFFF;
38481 + }
38482 + }
38483 + }
38484 +
38485 + if (microframe_schedule) {
38486 + if (hcd->available_host_channels < 1) {
38487 + break;
38488 + }
38489 + hcd->available_host_channels--;
38490 +#ifdef DEBUG_HOST_CHANNELS
38491 + last_sel_trans_num_nonper_scheduled++;
38492 +#endif /* DEBUG_HOST_CHANNELS */
38493 + }
38494 +
38495 + assign_and_init_hc(hcd, qh);
38496 +
38497 + /*
38498 + * Move the QH from the non-periodic inactive schedule to the
38499 + * non-periodic active schedule.
38500 + */
38501 + qh_ptr = DWC_LIST_NEXT(qh_ptr);
38502 + DWC_LIST_MOVE_HEAD(&hcd->non_periodic_sched_active,
38503 + &qh->qh_list_entry);
38504 +
38505 + if (!microframe_schedule)
38506 + hcd->non_periodic_channels++;
38507 + }
38508 + /* we moved a non-periodic QH to the active schedule. If the inactive queue is empty,
38509 + * stop the FIQ from kicking us. We could potentially still have elements here if we
38510 + * ran out of host channels.
38511 + */
38512 + if (fiq_enable) {
38513 + if (DWC_LIST_EMPTY(&hcd->non_periodic_sched_inactive)) {
38514 + hcd->fiq_state->kick_np_queues = 0;
38515 + } else {
38516 + /* For each entry remaining in the NP inactive queue,
38517 + * if this a NAK'd retransmit then don't set the kick flag.
38518 + */
38519 + if(nak_holdoff) {
38520 + DWC_LIST_FOREACH(qh_ptr, &hcd->non_periodic_sched_inactive) {
38521 + qh = DWC_LIST_ENTRY(qh_ptr, dwc_otg_qh_t, qh_list_entry);
38522 + if (qh->nak_frame == 0xFFFF) {
38523 + hcd->fiq_state->kick_np_queues = 1;
38524 + }
38525 + }
38526 + }
38527 + }
38528 + }
38529 + if(!DWC_LIST_EMPTY(&hcd->periodic_sched_assigned))
38530 + ret_val |= DWC_OTG_TRANSACTION_PERIODIC;
38531 +
38532 + if(!DWC_LIST_EMPTY(&hcd->non_periodic_sched_active))
38533 + ret_val |= DWC_OTG_TRANSACTION_NON_PERIODIC;
38534 +
38535 +
38536 +#ifdef DEBUG_HOST_CHANNELS
38537 + last_sel_trans_num_avail_hc_at_end = hcd->available_host_channels;
38538 +#endif /* DEBUG_HOST_CHANNELS */
38539 + return ret_val;
38540 +}
38541 +
38542 +/**
38543 + * Attempts to queue a single transaction request for a host channel
38544 + * associated with either a periodic or non-periodic transfer. This function
38545 + * assumes that there is space available in the appropriate request queue. For
38546 + * an OUT transfer or SETUP transaction in Slave mode, it checks whether space
38547 + * is available in the appropriate Tx FIFO.
38548 + *
38549 + * @param hcd The HCD state structure.
38550 + * @param hc Host channel descriptor associated with either a periodic or
38551 + * non-periodic transfer.
38552 + * @param fifo_dwords_avail Number of DWORDs available in the periodic Tx
38553 + * FIFO for periodic transfers or the non-periodic Tx FIFO for non-periodic
38554 + * transfers.
38555 + *
38556 + * @return 1 if a request is queued and more requests may be needed to
38557 + * complete the transfer, 0 if no more requests are required for this
38558 + * transfer, -1 if there is insufficient space in the Tx FIFO.
38559 + */
38560 +static int queue_transaction(dwc_otg_hcd_t * hcd,
38561 + dwc_hc_t * hc, uint16_t fifo_dwords_avail)
38562 +{
38563 + int retval;
38564 +
38565 + if (hcd->core_if->dma_enable) {
38566 + if (hcd->core_if->dma_desc_enable) {
38567 + if (!hc->xfer_started
38568 + || (hc->ep_type == DWC_OTG_EP_TYPE_ISOC)) {
38569 + dwc_otg_hcd_start_xfer_ddma(hcd, hc->qh);
38570 + hc->qh->ping_state = 0;
38571 + }
38572 + } else if (!hc->xfer_started) {
38573 + if (fiq_fsm_enable && hc->error_state) {
38574 + hcd->fiq_state->channel[hc->hc_num].nr_errors =
38575 + DWC_CIRCLEQ_FIRST(&hc->qh->qtd_list)->error_count;
38576 + hcd->fiq_state->channel[hc->hc_num].fsm =
38577 + FIQ_PASSTHROUGH_ERRORSTATE;
38578 + }
38579 + dwc_otg_hc_start_transfer(hcd->core_if, hc);
38580 + hc->qh->ping_state = 0;
38581 + }
38582 + retval = 0;
38583 + } else if (hc->halt_pending) {
38584 + /* Don't queue a request if the channel has been halted. */
38585 + retval = 0;
38586 + } else if (hc->halt_on_queue) {
38587 + dwc_otg_hc_halt(hcd->core_if, hc, hc->halt_status);
38588 + retval = 0;
38589 + } else if (hc->do_ping) {
38590 + if (!hc->xfer_started) {
38591 + dwc_otg_hc_start_transfer(hcd->core_if, hc);
38592 + }
38593 + retval = 0;
38594 + } else if (!hc->ep_is_in || hc->data_pid_start == DWC_OTG_HC_PID_SETUP) {
38595 + if ((fifo_dwords_avail * 4) >= hc->max_packet) {
38596 + if (!hc->xfer_started) {
38597 + dwc_otg_hc_start_transfer(hcd->core_if, hc);
38598 + retval = 1;
38599 + } else {
38600 + retval =
38601 + dwc_otg_hc_continue_transfer(hcd->core_if,
38602 + hc);
38603 + }
38604 + } else {
38605 + retval = -1;
38606 + }
38607 + } else {
38608 + if (!hc->xfer_started) {
38609 + dwc_otg_hc_start_transfer(hcd->core_if, hc);
38610 + retval = 1;
38611 + } else {
38612 + retval = dwc_otg_hc_continue_transfer(hcd->core_if, hc);
38613 + }
38614 + }
38615 +
38616 + return retval;
38617 +}
38618 +
38619 +/**
38620 + * Processes periodic channels for the next frame and queues transactions for
38621 + * these channels to the DWC_otg controller. After queueing transactions, the
38622 + * Periodic Tx FIFO Empty interrupt is enabled if there are more transactions
38623 + * to queue as Periodic Tx FIFO or request queue space becomes available.
38624 + * Otherwise, the Periodic Tx FIFO Empty interrupt is disabled.
38625 + */
38626 +static void process_periodic_channels(dwc_otg_hcd_t * hcd)
38627 +{
38628 + hptxsts_data_t tx_status;
38629 + dwc_list_link_t *qh_ptr;
38630 + dwc_otg_qh_t *qh;
38631 + int status = 0;
38632 + int no_queue_space = 0;
38633 + int no_fifo_space = 0;
38634 +
38635 + dwc_otg_host_global_regs_t *host_regs;
38636 + host_regs = hcd->core_if->host_if->host_global_regs;
38637 +
38638 + DWC_DEBUGPL(DBG_HCDV, "Queue periodic transactions\n");
38639 +#ifdef DEBUG
38640 + tx_status.d32 = DWC_READ_REG32(&host_regs->hptxsts);
38641 + DWC_DEBUGPL(DBG_HCDV,
38642 + " P Tx Req Queue Space Avail (before queue): %d\n",
38643 + tx_status.b.ptxqspcavail);
38644 + DWC_DEBUGPL(DBG_HCDV, " P Tx FIFO Space Avail (before queue): %d\n",
38645 + tx_status.b.ptxfspcavail);
38646 +#endif
38647 +
38648 + qh_ptr = hcd->periodic_sched_assigned.next;
38649 + while (qh_ptr != &hcd->periodic_sched_assigned) {
38650 + tx_status.d32 = DWC_READ_REG32(&host_regs->hptxsts);
38651 + if (tx_status.b.ptxqspcavail == 0) {
38652 + no_queue_space = 1;
38653 + break;
38654 + }
38655 +
38656 + qh = DWC_LIST_ENTRY(qh_ptr, dwc_otg_qh_t, qh_list_entry);
38657 +
38658 + // Do not send a split start transaction any later than frame .6
38659 + // Note, we have to schedule a periodic in .5 to make it go in .6
38660 + if(fiq_fsm_enable && qh->do_split && ((dwc_otg_hcd_get_frame_number(hcd) + 1) & 7) > 6)
38661 + {
38662 + qh_ptr = qh_ptr->next;
38663 + hcd->fiq_state->next_sched_frame = dwc_otg_hcd_get_frame_number(hcd) | 7;
38664 + continue;
38665 + }
38666 +
38667 + if (fiq_fsm_enable && fiq_fsm_transaction_suitable(hcd, qh)) {
38668 + if (qh->do_split)
38669 + fiq_fsm_queue_split_transaction(hcd, qh);
38670 + else
38671 + fiq_fsm_queue_isoc_transaction(hcd, qh);
38672 + } else {
38673 +
38674 + /*
38675 + * Set a flag if we're queueing high-bandwidth in slave mode.
38676 + * The flag prevents any halts to get into the request queue in
38677 + * the middle of multiple high-bandwidth packets getting queued.
38678 + */
38679 + if (!hcd->core_if->dma_enable && qh->channel->multi_count > 1) {
38680 + hcd->core_if->queuing_high_bandwidth = 1;
38681 + }
38682 + status = queue_transaction(hcd, qh->channel,
38683 + tx_status.b.ptxfspcavail);
38684 + if (status < 0) {
38685 + no_fifo_space = 1;
38686 + break;
38687 + }
38688 + }
38689 +
38690 + /*
38691 + * In Slave mode, stay on the current transfer until there is
38692 + * nothing more to do or the high-bandwidth request count is
38693 + * reached. In DMA mode, only need to queue one request. The
38694 + * controller automatically handles multiple packets for
38695 + * high-bandwidth transfers.
38696 + */
38697 + if (hcd->core_if->dma_enable || status == 0 ||
38698 + qh->channel->requests == qh->channel->multi_count) {
38699 + qh_ptr = qh_ptr->next;
38700 + /*
38701 + * Move the QH from the periodic assigned schedule to
38702 + * the periodic queued schedule.
38703 + */
38704 + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_queued,
38705 + &qh->qh_list_entry);
38706 +
38707 + /* done queuing high bandwidth */
38708 + hcd->core_if->queuing_high_bandwidth = 0;
38709 + }
38710 + }
38711 +
38712 + if (!hcd->core_if->dma_enable) {
38713 + dwc_otg_core_global_regs_t *global_regs;
38714 + gintmsk_data_t intr_mask = {.d32 = 0 };
38715 +
38716 + global_regs = hcd->core_if->core_global_regs;
38717 + intr_mask.b.ptxfempty = 1;
38718 +#ifdef DEBUG
38719 + tx_status.d32 = DWC_READ_REG32(&host_regs->hptxsts);
38720 + DWC_DEBUGPL(DBG_HCDV,
38721 + " P Tx Req Queue Space Avail (after queue): %d\n",
38722 + tx_status.b.ptxqspcavail);
38723 + DWC_DEBUGPL(DBG_HCDV,
38724 + " P Tx FIFO Space Avail (after queue): %d\n",
38725 + tx_status.b.ptxfspcavail);
38726 +#endif
38727 + if (!DWC_LIST_EMPTY(&hcd->periodic_sched_assigned) ||
38728 + no_queue_space || no_fifo_space) {
38729 + /*
38730 + * May need to queue more transactions as the request
38731 + * queue or Tx FIFO empties. Enable the periodic Tx
38732 + * FIFO empty interrupt. (Always use the half-empty
38733 + * level to ensure that new requests are loaded as
38734 + * soon as possible.)
38735 + */
38736 + DWC_MODIFY_REG32(&global_regs->gintmsk, 0,
38737 + intr_mask.d32);
38738 + } else {
38739 + /*
38740 + * Disable the Tx FIFO empty interrupt since there are
38741 + * no more transactions that need to be queued right
38742 + * now. This function is called from interrupt
38743 + * handlers to queue more transactions as transfer
38744 + * states change.
38745 + */
38746 + DWC_MODIFY_REG32(&global_regs->gintmsk, intr_mask.d32,
38747 + 0);
38748 + }
38749 + }
38750 +}
38751 +
38752 +/**
38753 + * Processes active non-periodic channels and queues transactions for these
38754 + * channels to the DWC_otg controller. After queueing transactions, the NP Tx
38755 + * FIFO Empty interrupt is enabled if there are more transactions to queue as
38756 + * NP Tx FIFO or request queue space becomes available. Otherwise, the NP Tx
38757 + * FIFO Empty interrupt is disabled.
38758 + */
38759 +static void process_non_periodic_channels(dwc_otg_hcd_t * hcd)
38760 +{
38761 + gnptxsts_data_t tx_status;
38762 + dwc_list_link_t *orig_qh_ptr;
38763 + dwc_otg_qh_t *qh;
38764 + int status;
38765 + int no_queue_space = 0;
38766 + int no_fifo_space = 0;
38767 + int more_to_do = 0;
38768 +
38769 + dwc_otg_core_global_regs_t *global_regs =
38770 + hcd->core_if->core_global_regs;
38771 +
38772 + DWC_DEBUGPL(DBG_HCDV, "Queue non-periodic transactions\n");
38773 +#ifdef DEBUG
38774 + tx_status.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
38775 + DWC_DEBUGPL(DBG_HCDV,
38776 + " NP Tx Req Queue Space Avail (before queue): %d\n",
38777 + tx_status.b.nptxqspcavail);
38778 + DWC_DEBUGPL(DBG_HCDV, " NP Tx FIFO Space Avail (before queue): %d\n",
38779 + tx_status.b.nptxfspcavail);
38780 +#endif
38781 + /*
38782 + * Keep track of the starting point. Skip over the start-of-list
38783 + * entry.
38784 + */
38785 + if (hcd->non_periodic_qh_ptr == &hcd->non_periodic_sched_active) {
38786 + hcd->non_periodic_qh_ptr = hcd->non_periodic_qh_ptr->next;
38787 + }
38788 + orig_qh_ptr = hcd->non_periodic_qh_ptr;
38789 +
38790 + /*
38791 + * Process once through the active list or until no more space is
38792 + * available in the request queue or the Tx FIFO.
38793 + */
38794 + do {
38795 + tx_status.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
38796 + if (!hcd->core_if->dma_enable && tx_status.b.nptxqspcavail == 0) {
38797 + no_queue_space = 1;
38798 + break;
38799 + }
38800 +
38801 + qh = DWC_LIST_ENTRY(hcd->non_periodic_qh_ptr, dwc_otg_qh_t,
38802 + qh_list_entry);
38803 +
38804 + if(fiq_fsm_enable && fiq_fsm_transaction_suitable(hcd, qh)) {
38805 + fiq_fsm_queue_split_transaction(hcd, qh);
38806 + } else {
38807 + status = queue_transaction(hcd, qh->channel,
38808 + tx_status.b.nptxfspcavail);
38809 +
38810 + if (status > 0) {
38811 + more_to_do = 1;
38812 + } else if (status < 0) {
38813 + no_fifo_space = 1;
38814 + break;
38815 + }
38816 + }
38817 + /* Advance to next QH, skipping start-of-list entry. */
38818 + hcd->non_periodic_qh_ptr = hcd->non_periodic_qh_ptr->next;
38819 + if (hcd->non_periodic_qh_ptr == &hcd->non_periodic_sched_active) {
38820 + hcd->non_periodic_qh_ptr =
38821 + hcd->non_periodic_qh_ptr->next;
38822 + }
38823 +
38824 + } while (hcd->non_periodic_qh_ptr != orig_qh_ptr);
38825 +
38826 + if (!hcd->core_if->dma_enable) {
38827 + gintmsk_data_t intr_mask = {.d32 = 0 };
38828 + intr_mask.b.nptxfempty = 1;
38829 +
38830 +#ifdef DEBUG
38831 + tx_status.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
38832 + DWC_DEBUGPL(DBG_HCDV,
38833 + " NP Tx Req Queue Space Avail (after queue): %d\n",
38834 + tx_status.b.nptxqspcavail);
38835 + DWC_DEBUGPL(DBG_HCDV,
38836 + " NP Tx FIFO Space Avail (after queue): %d\n",
38837 + tx_status.b.nptxfspcavail);
38838 +#endif
38839 + if (more_to_do || no_queue_space || no_fifo_space) {
38840 + /*
38841 + * May need to queue more transactions as the request
38842 + * queue or Tx FIFO empties. Enable the non-periodic
38843 + * Tx FIFO empty interrupt. (Always use the half-empty
38844 + * level to ensure that new requests are loaded as
38845 + * soon as possible.)
38846 + */
38847 + DWC_MODIFY_REG32(&global_regs->gintmsk, 0,
38848 + intr_mask.d32);
38849 + } else {
38850 + /*
38851 + * Disable the Tx FIFO empty interrupt since there are
38852 + * no more transactions that need to be queued right
38853 + * now. This function is called from interrupt
38854 + * handlers to queue more transactions as transfer
38855 + * states change.
38856 + */
38857 + DWC_MODIFY_REG32(&global_regs->gintmsk, intr_mask.d32,
38858 + 0);
38859 + }
38860 + }
38861 +}
38862 +
38863 +/**
38864 + * This function processes the currently active host channels and queues
38865 + * transactions for these channels to the DWC_otg controller. It is called
38866 + * from HCD interrupt handler functions.
38867 + *
38868 + * @param hcd The HCD state structure.
38869 + * @param tr_type The type(s) of transactions to queue (non-periodic,
38870 + * periodic, or both).
38871 + */
38872 +void dwc_otg_hcd_queue_transactions(dwc_otg_hcd_t * hcd,
38873 + dwc_otg_transaction_type_e tr_type)
38874 +{
38875 +#ifdef DEBUG_SOF
38876 + DWC_DEBUGPL(DBG_HCD, "Queue Transactions\n");
38877 +#endif
38878 + /* Process host channels associated with periodic transfers. */
38879 + if ((tr_type == DWC_OTG_TRANSACTION_PERIODIC ||
38880 + tr_type == DWC_OTG_TRANSACTION_ALL) &&
38881 + !DWC_LIST_EMPTY(&hcd->periodic_sched_assigned)) {
38882 +
38883 + process_periodic_channels(hcd);
38884 + }
38885 +
38886 + /* Process host channels associated with non-periodic transfers. */
38887 + if (tr_type == DWC_OTG_TRANSACTION_NON_PERIODIC ||
38888 + tr_type == DWC_OTG_TRANSACTION_ALL) {
38889 + if (!DWC_LIST_EMPTY(&hcd->non_periodic_sched_active)) {
38890 + process_non_periodic_channels(hcd);
38891 + } else {
38892 + /*
38893 + * Ensure NP Tx FIFO empty interrupt is disabled when
38894 + * there are no non-periodic transfers to process.
38895 + */
38896 + gintmsk_data_t gintmsk = {.d32 = 0 };
38897 + gintmsk.b.nptxfempty = 1;
38898 +
38899 + if (fiq_enable) {
38900 + local_fiq_disable();
38901 + fiq_fsm_spin_lock(&hcd->fiq_state->lock);
38902 + DWC_MODIFY_REG32(&hcd->core_if->core_global_regs->gintmsk, gintmsk.d32, 0);
38903 + fiq_fsm_spin_unlock(&hcd->fiq_state->lock);
38904 + local_fiq_enable();
38905 + } else {
38906 + DWC_MODIFY_REG32(&hcd->core_if->core_global_regs->gintmsk, gintmsk.d32, 0);
38907 + }
38908 + }
38909 + }
38910 +}
38911 +
38912 +#ifdef DWC_HS_ELECT_TST
38913 +/*
38914 + * Quick and dirty hack to implement the HS Electrical Test
38915 + * SINGLE_STEP_GET_DEVICE_DESCRIPTOR feature.
38916 + *
38917 + * This code was copied from our userspace app "hset". It sends a
38918 + * Get Device Descriptor control sequence in two parts, first the
38919 + * Setup packet by itself, followed some time later by the In and
38920 + * Ack packets. Rather than trying to figure out how to add this
38921 + * functionality to the normal driver code, we just hijack the
38922 + * hardware, using these two function to drive the hardware
38923 + * directly.
38924 + */
38925 +
38926 +static dwc_otg_core_global_regs_t *global_regs;
38927 +static dwc_otg_host_global_regs_t *hc_global_regs;
38928 +static dwc_otg_hc_regs_t *hc_regs;
38929 +static uint32_t *data_fifo;
38930 +
38931 +static void do_setup(void)
38932 +{
38933 + gintsts_data_t gintsts;
38934 + hctsiz_data_t hctsiz;
38935 + hcchar_data_t hcchar;
38936 + haint_data_t haint;
38937 + hcint_data_t hcint;
38938 +
38939 + /* Enable HAINTs */
38940 + DWC_WRITE_REG32(&hc_global_regs->haintmsk, 0x0001);
38941 +
38942 + /* Enable HCINTs */
38943 + DWC_WRITE_REG32(&hc_regs->hcintmsk, 0x04a3);
38944 +
38945 + /* Read GINTSTS */
38946 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
38947 +
38948 + /* Read HAINT */
38949 + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
38950 +
38951 + /* Read HCINT */
38952 + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
38953 +
38954 + /* Read HCCHAR */
38955 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
38956 +
38957 + /* Clear HCINT */
38958 + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
38959 +
38960 + /* Clear HAINT */
38961 + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
38962 +
38963 + /* Clear GINTSTS */
38964 + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
38965 +
38966 + /* Read GINTSTS */
38967 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
38968 +
38969 + /*
38970 + * Send Setup packet (Get Device Descriptor)
38971 + */
38972 +
38973 + /* Make sure channel is disabled */
38974 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
38975 + if (hcchar.b.chen) {
38976 + hcchar.b.chdis = 1;
38977 +// hcchar.b.chen = 1;
38978 + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
38979 + //sleep(1);
38980 + dwc_mdelay(1000);
38981 +
38982 + /* Read GINTSTS */
38983 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
38984 +
38985 + /* Read HAINT */
38986 + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
38987 +
38988 + /* Read HCINT */
38989 + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
38990 +
38991 + /* Read HCCHAR */
38992 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
38993 +
38994 + /* Clear HCINT */
38995 + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
38996 +
38997 + /* Clear HAINT */
38998 + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
38999 +
39000 + /* Clear GINTSTS */
39001 + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
39002 +
39003 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
39004 + }
39005 +
39006 + /* Set HCTSIZ */
39007 + hctsiz.d32 = 0;
39008 + hctsiz.b.xfersize = 8;
39009 + hctsiz.b.pktcnt = 1;
39010 + hctsiz.b.pid = DWC_OTG_HC_PID_SETUP;
39011 + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
39012 +
39013 + /* Set HCCHAR */
39014 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
39015 + hcchar.b.eptype = DWC_OTG_EP_TYPE_CONTROL;
39016 + hcchar.b.epdir = 0;
39017 + hcchar.b.epnum = 0;
39018 + hcchar.b.mps = 8;
39019 + hcchar.b.chen = 1;
39020 + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
39021 +
39022 + /* Fill FIFO with Setup data for Get Device Descriptor */
39023 + data_fifo = (uint32_t *) ((char *)global_regs + 0x1000);
39024 + DWC_WRITE_REG32(data_fifo++, 0x01000680);
39025 + DWC_WRITE_REG32(data_fifo++, 0x00080000);
39026 +
39027 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
39028 +
39029 + /* Wait for host channel interrupt */
39030 + do {
39031 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
39032 + } while (gintsts.b.hcintr == 0);
39033 +
39034 + /* Disable HCINTs */
39035 + DWC_WRITE_REG32(&hc_regs->hcintmsk, 0x0000);
39036 +
39037 + /* Disable HAINTs */
39038 + DWC_WRITE_REG32(&hc_global_regs->haintmsk, 0x0000);
39039 +
39040 + /* Read HAINT */
39041 + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
39042 +
39043 + /* Read HCINT */
39044 + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
39045 +
39046 + /* Read HCCHAR */
39047 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
39048 +
39049 + /* Clear HCINT */
39050 + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
39051 +
39052 + /* Clear HAINT */
39053 + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
39054 +
39055 + /* Clear GINTSTS */
39056 + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
39057 +
39058 + /* Read GINTSTS */
39059 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
39060 +}
39061 +
39062 +static void do_in_ack(void)
39063 +{
39064 + gintsts_data_t gintsts;
39065 + hctsiz_data_t hctsiz;
39066 + hcchar_data_t hcchar;
39067 + haint_data_t haint;
39068 + hcint_data_t hcint;
39069 + host_grxsts_data_t grxsts;
39070 +
39071 + /* Enable HAINTs */
39072 + DWC_WRITE_REG32(&hc_global_regs->haintmsk, 0x0001);
39073 +
39074 + /* Enable HCINTs */
39075 + DWC_WRITE_REG32(&hc_regs->hcintmsk, 0x04a3);
39076 +
39077 + /* Read GINTSTS */
39078 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
39079 +
39080 + /* Read HAINT */
39081 + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
39082 +
39083 + /* Read HCINT */
39084 + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
39085 +
39086 + /* Read HCCHAR */
39087 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
39088 +
39089 + /* Clear HCINT */
39090 + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
39091 +
39092 + /* Clear HAINT */
39093 + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
39094 +
39095 + /* Clear GINTSTS */
39096 + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
39097 +
39098 + /* Read GINTSTS */
39099 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
39100 +
39101 + /*
39102 + * Receive Control In packet
39103 + */
39104 +
39105 + /* Make sure channel is disabled */
39106 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
39107 + if (hcchar.b.chen) {
39108 + hcchar.b.chdis = 1;
39109 + hcchar.b.chen = 1;
39110 + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
39111 + //sleep(1);
39112 + dwc_mdelay(1000);
39113 +
39114 + /* Read GINTSTS */
39115 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
39116 +
39117 + /* Read HAINT */
39118 + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
39119 +
39120 + /* Read HCINT */
39121 + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
39122 +
39123 + /* Read HCCHAR */
39124 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
39125 +
39126 + /* Clear HCINT */
39127 + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
39128 +
39129 + /* Clear HAINT */
39130 + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
39131 +
39132 + /* Clear GINTSTS */
39133 + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
39134 +
39135 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
39136 + }
39137 +
39138 + /* Set HCTSIZ */
39139 + hctsiz.d32 = 0;
39140 + hctsiz.b.xfersize = 8;
39141 + hctsiz.b.pktcnt = 1;
39142 + hctsiz.b.pid = DWC_OTG_HC_PID_DATA1;
39143 + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
39144 +
39145 + /* Set HCCHAR */
39146 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
39147 + hcchar.b.eptype = DWC_OTG_EP_TYPE_CONTROL;
39148 + hcchar.b.epdir = 1;
39149 + hcchar.b.epnum = 0;
39150 + hcchar.b.mps = 8;
39151 + hcchar.b.chen = 1;
39152 + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
39153 +
39154 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
39155 +
39156 + /* Wait for receive status queue interrupt */
39157 + do {
39158 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
39159 + } while (gintsts.b.rxstsqlvl == 0);
39160 +
39161 + /* Read RXSTS */
39162 + grxsts.d32 = DWC_READ_REG32(&global_regs->grxstsp);
39163 +
39164 + /* Clear RXSTSQLVL in GINTSTS */
39165 + gintsts.d32 = 0;
39166 + gintsts.b.rxstsqlvl = 1;
39167 + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
39168 +
39169 + switch (grxsts.b.pktsts) {
39170 + case DWC_GRXSTS_PKTSTS_IN:
39171 + /* Read the data into the host buffer */
39172 + if (grxsts.b.bcnt > 0) {
39173 + int i;
39174 + int word_count = (grxsts.b.bcnt + 3) / 4;
39175 +
39176 + data_fifo = (uint32_t *) ((char *)global_regs + 0x1000);
39177 +
39178 + for (i = 0; i < word_count; i++) {
39179 + (void)DWC_READ_REG32(data_fifo++);
39180 + }
39181 + }
39182 + break;
39183 +
39184 + default:
39185 + break;
39186 + }
39187 +
39188 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
39189 +
39190 + /* Wait for receive status queue interrupt */
39191 + do {
39192 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
39193 + } while (gintsts.b.rxstsqlvl == 0);
39194 +
39195 + /* Read RXSTS */
39196 + grxsts.d32 = DWC_READ_REG32(&global_regs->grxstsp);
39197 +
39198 + /* Clear RXSTSQLVL in GINTSTS */
39199 + gintsts.d32 = 0;
39200 + gintsts.b.rxstsqlvl = 1;
39201 + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
39202 +
39203 + switch (grxsts.b.pktsts) {
39204 + case DWC_GRXSTS_PKTSTS_IN_XFER_COMP:
39205 + break;
39206 +
39207 + default:
39208 + break;
39209 + }
39210 +
39211 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
39212 +
39213 + /* Wait for host channel interrupt */
39214 + do {
39215 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
39216 + } while (gintsts.b.hcintr == 0);
39217 +
39218 + /* Read HAINT */
39219 + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
39220 +
39221 + /* Read HCINT */
39222 + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
39223 +
39224 + /* Read HCCHAR */
39225 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
39226 +
39227 + /* Clear HCINT */
39228 + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
39229 +
39230 + /* Clear HAINT */
39231 + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
39232 +
39233 + /* Clear GINTSTS */
39234 + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
39235 +
39236 + /* Read GINTSTS */
39237 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
39238 +
39239 +// usleep(100000);
39240 +// mdelay(100);
39241 + dwc_mdelay(1);
39242 +
39243 + /*
39244 + * Send handshake packet
39245 + */
39246 +
39247 + /* Read HAINT */
39248 + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
39249 +
39250 + /* Read HCINT */
39251 + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
39252 +
39253 + /* Read HCCHAR */
39254 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
39255 +
39256 + /* Clear HCINT */
39257 + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
39258 +
39259 + /* Clear HAINT */
39260 + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
39261 +
39262 + /* Clear GINTSTS */
39263 + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
39264 +
39265 + /* Read GINTSTS */
39266 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
39267 +
39268 + /* Make sure channel is disabled */
39269 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
39270 + if (hcchar.b.chen) {
39271 + hcchar.b.chdis = 1;
39272 + hcchar.b.chen = 1;
39273 + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
39274 + //sleep(1);
39275 + dwc_mdelay(1000);
39276 +
39277 + /* Read GINTSTS */
39278 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
39279 +
39280 + /* Read HAINT */
39281 + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
39282 +
39283 + /* Read HCINT */
39284 + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
39285 +
39286 + /* Read HCCHAR */
39287 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
39288 +
39289 + /* Clear HCINT */
39290 + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
39291 +
39292 + /* Clear HAINT */
39293 + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
39294 +
39295 + /* Clear GINTSTS */
39296 + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
39297 +
39298 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
39299 + }
39300 +
39301 + /* Set HCTSIZ */
39302 + hctsiz.d32 = 0;
39303 + hctsiz.b.xfersize = 0;
39304 + hctsiz.b.pktcnt = 1;
39305 + hctsiz.b.pid = DWC_OTG_HC_PID_DATA1;
39306 + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
39307 +
39308 + /* Set HCCHAR */
39309 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
39310 + hcchar.b.eptype = DWC_OTG_EP_TYPE_CONTROL;
39311 + hcchar.b.epdir = 0;
39312 + hcchar.b.epnum = 0;
39313 + hcchar.b.mps = 8;
39314 + hcchar.b.chen = 1;
39315 + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
39316 +
39317 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
39318 +
39319 + /* Wait for host channel interrupt */
39320 + do {
39321 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
39322 + } while (gintsts.b.hcintr == 0);
39323 +
39324 + /* Disable HCINTs */
39325 + DWC_WRITE_REG32(&hc_regs->hcintmsk, 0x0000);
39326 +
39327 + /* Disable HAINTs */
39328 + DWC_WRITE_REG32(&hc_global_regs->haintmsk, 0x0000);
39329 +
39330 + /* Read HAINT */
39331 + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
39332 +
39333 + /* Read HCINT */
39334 + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
39335 +
39336 + /* Read HCCHAR */
39337 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
39338 +
39339 + /* Clear HCINT */
39340 + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
39341 +
39342 + /* Clear HAINT */
39343 + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
39344 +
39345 + /* Clear GINTSTS */
39346 + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
39347 +
39348 + /* Read GINTSTS */
39349 + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
39350 +}
39351 +#endif
39352 +
39353 +/** Handles hub class-specific requests. */
39354 +int dwc_otg_hcd_hub_control(dwc_otg_hcd_t * dwc_otg_hcd,
39355 + uint16_t typeReq,
39356 + uint16_t wValue,
39357 + uint16_t wIndex, uint8_t * buf, uint16_t wLength)
39358 +{
39359 + int retval = 0;
39360 +
39361 + dwc_otg_core_if_t *core_if = dwc_otg_hcd->core_if;
39362 + usb_hub_descriptor_t *hub_desc;
39363 + hprt0_data_t hprt0 = {.d32 = 0 };
39364 +
39365 + uint32_t port_status;
39366 +
39367 + switch (typeReq) {
39368 + case UCR_CLEAR_HUB_FEATURE:
39369 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
39370 + "ClearHubFeature 0x%x\n", wValue);
39371 + switch (wValue) {
39372 + case UHF_C_HUB_LOCAL_POWER:
39373 + case UHF_C_HUB_OVER_CURRENT:
39374 + /* Nothing required here */
39375 + break;
39376 + default:
39377 + retval = -DWC_E_INVALID;
39378 + DWC_ERROR("DWC OTG HCD - "
39379 + "ClearHubFeature request %xh unknown\n",
39380 + wValue);
39381 + }
39382 + break;
39383 + case UCR_CLEAR_PORT_FEATURE:
39384 +#ifdef CONFIG_USB_DWC_OTG_LPM
39385 + if (wValue != UHF_PORT_L1)
39386 +#endif
39387 + if (!wIndex || wIndex > 1)
39388 + goto error;
39389 +
39390 + switch (wValue) {
39391 + case UHF_PORT_ENABLE:
39392 + DWC_DEBUGPL(DBG_ANY, "DWC OTG HCD HUB CONTROL - "
39393 + "ClearPortFeature USB_PORT_FEAT_ENABLE\n");
39394 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
39395 + hprt0.b.prtena = 1;
39396 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
39397 + break;
39398 + case UHF_PORT_SUSPEND:
39399 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
39400 + "ClearPortFeature USB_PORT_FEAT_SUSPEND\n");
39401 +
39402 + if (core_if->power_down == 2) {
39403 + dwc_otg_host_hibernation_restore(core_if, 0, 0);
39404 + } else {
39405 + DWC_WRITE_REG32(core_if->pcgcctl, 0);
39406 + dwc_mdelay(5);
39407 +
39408 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
39409 + hprt0.b.prtres = 1;
39410 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
39411 + hprt0.b.prtsusp = 0;
39412 + /* Clear Resume bit */
39413 + dwc_mdelay(100);
39414 + hprt0.b.prtres = 0;
39415 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
39416 + }
39417 + break;
39418 +#ifdef CONFIG_USB_DWC_OTG_LPM
39419 + case UHF_PORT_L1:
39420 + {
39421 + pcgcctl_data_t pcgcctl = {.d32 = 0 };
39422 + glpmcfg_data_t lpmcfg = {.d32 = 0 };
39423 +
39424 + lpmcfg.d32 =
39425 + DWC_READ_REG32(&core_if->
39426 + core_global_regs->glpmcfg);
39427 + lpmcfg.b.en_utmi_sleep = 0;
39428 + lpmcfg.b.hird_thres &= (~(1 << 4));
39429 + lpmcfg.b.prt_sleep_sts = 1;
39430 + DWC_WRITE_REG32(&core_if->
39431 + core_global_regs->glpmcfg,
39432 + lpmcfg.d32);
39433 +
39434 + /* Clear Enbl_L1Gating bit. */
39435 + pcgcctl.b.enbl_sleep_gating = 1;
39436 + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32,
39437 + 0);
39438 +
39439 + dwc_mdelay(5);
39440 +
39441 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
39442 + hprt0.b.prtres = 1;
39443 + DWC_WRITE_REG32(core_if->host_if->hprt0,
39444 + hprt0.d32);
39445 + /* This bit will be cleared in wakeup interrupt handle */
39446 + break;
39447 + }
39448 +#endif
39449 + case UHF_PORT_POWER:
39450 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
39451 + "ClearPortFeature USB_PORT_FEAT_POWER\n");
39452 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
39453 + hprt0.b.prtpwr = 0;
39454 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
39455 + break;
39456 + case UHF_PORT_INDICATOR:
39457 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
39458 + "ClearPortFeature USB_PORT_FEAT_INDICATOR\n");
39459 + /* Port inidicator not supported */
39460 + break;
39461 + case UHF_C_PORT_CONNECTION:
39462 + /* Clears drivers internal connect status change
39463 + * flag */
39464 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
39465 + "ClearPortFeature USB_PORT_FEAT_C_CONNECTION\n");
39466 + dwc_otg_hcd->flags.b.port_connect_status_change = 0;
39467 + break;
39468 + case UHF_C_PORT_RESET:
39469 + /* Clears the driver's internal Port Reset Change
39470 + * flag */
39471 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
39472 + "ClearPortFeature USB_PORT_FEAT_C_RESET\n");
39473 + dwc_otg_hcd->flags.b.port_reset_change = 0;
39474 + break;
39475 + case UHF_C_PORT_ENABLE:
39476 + /* Clears the driver's internal Port
39477 + * Enable/Disable Change flag */
39478 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
39479 + "ClearPortFeature USB_PORT_FEAT_C_ENABLE\n");
39480 + dwc_otg_hcd->flags.b.port_enable_change = 0;
39481 + break;
39482 + case UHF_C_PORT_SUSPEND:
39483 + /* Clears the driver's internal Port Suspend
39484 + * Change flag, which is set when resume signaling on
39485 + * the host port is complete */
39486 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
39487 + "ClearPortFeature USB_PORT_FEAT_C_SUSPEND\n");
39488 + dwc_otg_hcd->flags.b.port_suspend_change = 0;
39489 + break;
39490 +#ifdef CONFIG_USB_DWC_OTG_LPM
39491 + case UHF_C_PORT_L1:
39492 + dwc_otg_hcd->flags.b.port_l1_change = 0;
39493 + break;
39494 +#endif
39495 + case UHF_C_PORT_OVER_CURRENT:
39496 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
39497 + "ClearPortFeature USB_PORT_FEAT_C_OVER_CURRENT\n");
39498 + dwc_otg_hcd->flags.b.port_over_current_change = 0;
39499 + break;
39500 + default:
39501 + retval = -DWC_E_INVALID;
39502 + DWC_ERROR("DWC OTG HCD - "
39503 + "ClearPortFeature request %xh "
39504 + "unknown or unsupported\n", wValue);
39505 + }
39506 + break;
39507 + case UCR_GET_HUB_DESCRIPTOR:
39508 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
39509 + "GetHubDescriptor\n");
39510 + hub_desc = (usb_hub_descriptor_t *) buf;
39511 + hub_desc->bDescLength = 9;
39512 + hub_desc->bDescriptorType = 0x29;
39513 + hub_desc->bNbrPorts = 1;
39514 + USETW(hub_desc->wHubCharacteristics, 0x08);
39515 + hub_desc->bPwrOn2PwrGood = 1;
39516 + hub_desc->bHubContrCurrent = 0;
39517 + hub_desc->DeviceRemovable[0] = 0;
39518 + hub_desc->DeviceRemovable[1] = 0xff;
39519 + break;
39520 + case UCR_GET_HUB_STATUS:
39521 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
39522 + "GetHubStatus\n");
39523 + DWC_MEMSET(buf, 0, 4);
39524 + break;
39525 + case UCR_GET_PORT_STATUS:
39526 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
39527 + "GetPortStatus wIndex = 0x%04x FLAGS=0x%08x\n",
39528 + wIndex, dwc_otg_hcd->flags.d32);
39529 + if (!wIndex || wIndex > 1)
39530 + goto error;
39531 +
39532 + port_status = 0;
39533 +
39534 + if (dwc_otg_hcd->flags.b.port_connect_status_change)
39535 + port_status |= (1 << UHF_C_PORT_CONNECTION);
39536 +
39537 + if (dwc_otg_hcd->flags.b.port_enable_change)
39538 + port_status |= (1 << UHF_C_PORT_ENABLE);
39539 +
39540 + if (dwc_otg_hcd->flags.b.port_suspend_change)
39541 + port_status |= (1 << UHF_C_PORT_SUSPEND);
39542 +
39543 + if (dwc_otg_hcd->flags.b.port_l1_change)
39544 + port_status |= (1 << UHF_C_PORT_L1);
39545 +
39546 + if (dwc_otg_hcd->flags.b.port_reset_change) {
39547 + port_status |= (1 << UHF_C_PORT_RESET);
39548 + }
39549 +
39550 + if (dwc_otg_hcd->flags.b.port_over_current_change) {
39551 + DWC_WARN("Overcurrent change detected\n");
39552 + port_status |= (1 << UHF_C_PORT_OVER_CURRENT);
39553 + }
39554 +
39555 + if (!dwc_otg_hcd->flags.b.port_connect_status) {
39556 + /*
39557 + * The port is disconnected, which means the core is
39558 + * either in device mode or it soon will be. Just
39559 + * return 0's for the remainder of the port status
39560 + * since the port register can't be read if the core
39561 + * is in device mode.
39562 + */
39563 + *((__le32 *) buf) = dwc_cpu_to_le32(&port_status);
39564 + break;
39565 + }
39566 +
39567 + hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
39568 + DWC_DEBUGPL(DBG_HCDV, " HPRT0: 0x%08x\n", hprt0.d32);
39569 +
39570 + if (hprt0.b.prtconnsts)
39571 + port_status |= (1 << UHF_PORT_CONNECTION);
39572 +
39573 + if (hprt0.b.prtena)
39574 + port_status |= (1 << UHF_PORT_ENABLE);
39575 +
39576 + if (hprt0.b.prtsusp)
39577 + port_status |= (1 << UHF_PORT_SUSPEND);
39578 +
39579 + if (hprt0.b.prtovrcurract)
39580 + port_status |= (1 << UHF_PORT_OVER_CURRENT);
39581 +
39582 + if (hprt0.b.prtrst)
39583 + port_status |= (1 << UHF_PORT_RESET);
39584 +
39585 + if (hprt0.b.prtpwr)
39586 + port_status |= (1 << UHF_PORT_POWER);
39587 +
39588 + if (hprt0.b.prtspd == DWC_HPRT0_PRTSPD_HIGH_SPEED)
39589 + port_status |= (1 << UHF_PORT_HIGH_SPEED);
39590 + else if (hprt0.b.prtspd == DWC_HPRT0_PRTSPD_LOW_SPEED)
39591 + port_status |= (1 << UHF_PORT_LOW_SPEED);
39592 +
39593 + if (hprt0.b.prttstctl)
39594 + port_status |= (1 << UHF_PORT_TEST);
39595 + if (dwc_otg_get_lpm_portsleepstatus(dwc_otg_hcd->core_if)) {
39596 + port_status |= (1 << UHF_PORT_L1);
39597 + }
39598 + /*
39599 + For Synopsys HW emulation of Power down wkup_control asserts the
39600 + hreset_n and prst_n on suspned. This causes the HPRT0 to be zero.
39601 + We intentionally tell the software that port is in L2Suspend state.
39602 + Only for STE.
39603 + */
39604 + if ((core_if->power_down == 2)
39605 + && (core_if->hibernation_suspend == 1)) {
39606 + port_status |= (1 << UHF_PORT_SUSPEND);
39607 + }
39608 + /* USB_PORT_FEAT_INDICATOR unsupported always 0 */
39609 +
39610 + *((__le32 *) buf) = dwc_cpu_to_le32(&port_status);
39611 +
39612 + break;
39613 + case UCR_SET_HUB_FEATURE:
39614 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
39615 + "SetHubFeature\n");
39616 + /* No HUB features supported */
39617 + break;
39618 + case UCR_SET_PORT_FEATURE:
39619 + if (wValue != UHF_PORT_TEST && (!wIndex || wIndex > 1))
39620 + goto error;
39621 +
39622 + if (!dwc_otg_hcd->flags.b.port_connect_status) {
39623 + /*
39624 + * The port is disconnected, which means the core is
39625 + * either in device mode or it soon will be. Just
39626 + * return without doing anything since the port
39627 + * register can't be written if the core is in device
39628 + * mode.
39629 + */
39630 + break;
39631 + }
39632 +
39633 + switch (wValue) {
39634 + case UHF_PORT_SUSPEND:
39635 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
39636 + "SetPortFeature - USB_PORT_FEAT_SUSPEND\n");
39637 + if (dwc_otg_hcd_otg_port(dwc_otg_hcd) != wIndex) {
39638 + goto error;
39639 + }
39640 + if (core_if->power_down == 2) {
39641 + int timeout = 300;
39642 + dwc_irqflags_t flags;
39643 + pcgcctl_data_t pcgcctl = {.d32 = 0 };
39644 + gpwrdn_data_t gpwrdn = {.d32 = 0 };
39645 + gusbcfg_data_t gusbcfg = {.d32 = 0 };
39646 +#ifdef DWC_DEV_SRPCAP
39647 + int32_t otg_cap_param = core_if->core_params->otg_cap;
39648 +#endif
39649 + DWC_PRINTF("Preparing for complete power-off\n");
39650 +
39651 + /* Save registers before hibernation */
39652 + dwc_otg_save_global_regs(core_if);
39653 + dwc_otg_save_host_regs(core_if);
39654 +
39655 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
39656 + hprt0.b.prtsusp = 1;
39657 + hprt0.b.prtena = 0;
39658 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
39659 + /* Spin hprt0.b.prtsusp to became 1 */
39660 + do {
39661 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
39662 + if (hprt0.b.prtsusp) {
39663 + break;
39664 + }
39665 + dwc_mdelay(1);
39666 + } while (--timeout);
39667 + if (!timeout) {
39668 + DWC_WARN("Suspend wasn't genereted\n");
39669 + }
39670 + dwc_udelay(10);
39671 +
39672 + /*
39673 + * We need to disable interrupts to prevent servicing of any IRQ
39674 + * during going to hibernation
39675 + */
39676 + DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &flags);
39677 + core_if->lx_state = DWC_OTG_L2;
39678 +#ifdef DWC_DEV_SRPCAP
39679 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
39680 + hprt0.b.prtpwr = 0;
39681 + hprt0.b.prtena = 0;
39682 + DWC_WRITE_REG32(core_if->host_if->hprt0,
39683 + hprt0.d32);
39684 +#endif
39685 + gusbcfg.d32 =
39686 + DWC_READ_REG32(&core_if->core_global_regs->
39687 + gusbcfg);
39688 + if (gusbcfg.b.ulpi_utmi_sel == 1) {
39689 + /* ULPI interface */
39690 + /* Suspend the Phy Clock */
39691 + pcgcctl.d32 = 0;
39692 + pcgcctl.b.stoppclk = 1;
39693 + DWC_MODIFY_REG32(core_if->pcgcctl, 0,
39694 + pcgcctl.d32);
39695 + dwc_udelay(10);
39696 + gpwrdn.b.pmuactv = 1;
39697 + DWC_MODIFY_REG32(&core_if->
39698 + core_global_regs->
39699 + gpwrdn, 0, gpwrdn.d32);
39700 + } else {
39701 + /* UTMI+ Interface */
39702 + gpwrdn.b.pmuactv = 1;
39703 + DWC_MODIFY_REG32(&core_if->
39704 + core_global_regs->
39705 + gpwrdn, 0, gpwrdn.d32);
39706 + dwc_udelay(10);
39707 + pcgcctl.b.stoppclk = 1;
39708 + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
39709 + dwc_udelay(10);
39710 + }
39711 +#ifdef DWC_DEV_SRPCAP
39712 + gpwrdn.d32 = 0;
39713 + gpwrdn.b.dis_vbus = 1;
39714 + DWC_MODIFY_REG32(&core_if->core_global_regs->
39715 + gpwrdn, 0, gpwrdn.d32);
39716 +#endif
39717 + gpwrdn.d32 = 0;
39718 + gpwrdn.b.pmuintsel = 1;
39719 + DWC_MODIFY_REG32(&core_if->core_global_regs->
39720 + gpwrdn, 0, gpwrdn.d32);
39721 + dwc_udelay(10);
39722 +
39723 + gpwrdn.d32 = 0;
39724 +#ifdef DWC_DEV_SRPCAP
39725 + gpwrdn.b.srp_det_msk = 1;
39726 +#endif
39727 + gpwrdn.b.disconn_det_msk = 1;
39728 + gpwrdn.b.lnstchng_msk = 1;
39729 + gpwrdn.b.sts_chngint_msk = 1;
39730 + DWC_MODIFY_REG32(&core_if->core_global_regs->
39731 + gpwrdn, 0, gpwrdn.d32);
39732 + dwc_udelay(10);
39733 +
39734 + /* Enable Power Down Clamp and all interrupts in GPWRDN */
39735 + gpwrdn.d32 = 0;
39736 + gpwrdn.b.pwrdnclmp = 1;
39737 + DWC_MODIFY_REG32(&core_if->core_global_regs->
39738 + gpwrdn, 0, gpwrdn.d32);
39739 + dwc_udelay(10);
39740 +
39741 + /* Switch off VDD */
39742 + gpwrdn.d32 = 0;
39743 + gpwrdn.b.pwrdnswtch = 1;
39744 + DWC_MODIFY_REG32(&core_if->core_global_regs->
39745 + gpwrdn, 0, gpwrdn.d32);
39746 +
39747 +#ifdef DWC_DEV_SRPCAP
39748 + if (otg_cap_param == DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE)
39749 + {
39750 + core_if->pwron_timer_started = 1;
39751 + DWC_TIMER_SCHEDULE(core_if->pwron_timer, 6000 /* 6 secs */ );
39752 + }
39753 +#endif
39754 + /* Save gpwrdn register for further usage if stschng interrupt */
39755 + core_if->gr_backup->gpwrdn_local =
39756 + DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
39757 +
39758 + /* Set flag to indicate that we are in hibernation */
39759 + core_if->hibernation_suspend = 1;
39760 + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock,flags);
39761 +
39762 + DWC_PRINTF("Host hibernation completed\n");
39763 + // Exit from case statement
39764 + break;
39765 +
39766 + }
39767 + if (dwc_otg_hcd_otg_port(dwc_otg_hcd) == wIndex &&
39768 + dwc_otg_hcd->fops->get_b_hnp_enable(dwc_otg_hcd)) {
39769 + gotgctl_data_t gotgctl = {.d32 = 0 };
39770 + gotgctl.b.hstsethnpen = 1;
39771 + DWC_MODIFY_REG32(&core_if->core_global_regs->
39772 + gotgctl, 0, gotgctl.d32);
39773 + core_if->op_state = A_SUSPEND;
39774 + }
39775 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
39776 + hprt0.b.prtsusp = 1;
39777 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
39778 + {
39779 + dwc_irqflags_t flags;
39780 + /* Update lx_state */
39781 + DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &flags);
39782 + core_if->lx_state = DWC_OTG_L2;
39783 + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, flags);
39784 + }
39785 + /* Suspend the Phy Clock */
39786 + {
39787 + pcgcctl_data_t pcgcctl = {.d32 = 0 };
39788 + pcgcctl.b.stoppclk = 1;
39789 + DWC_MODIFY_REG32(core_if->pcgcctl, 0,
39790 + pcgcctl.d32);
39791 + dwc_udelay(10);
39792 + }
39793 +
39794 + /* For HNP the bus must be suspended for at least 200ms. */
39795 + if (dwc_otg_hcd->fops->get_b_hnp_enable(dwc_otg_hcd)) {
39796 + pcgcctl_data_t pcgcctl = {.d32 = 0 };
39797 + pcgcctl.b.stoppclk = 1;
39798 + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
39799 + dwc_mdelay(200);
39800 + }
39801 +
39802 + /** @todo - check how sw can wait for 1 sec to check asesvld??? */
39803 +#if 0 //vahrama !!!!!!!!!!!!!!!!!!
39804 + if (core_if->adp_enable) {
39805 + gotgctl_data_t gotgctl = {.d32 = 0 };
39806 + gpwrdn_data_t gpwrdn;
39807 +
39808 + while (gotgctl.b.asesvld == 1) {
39809 + gotgctl.d32 =
39810 + DWC_READ_REG32(&core_if->
39811 + core_global_regs->
39812 + gotgctl);
39813 + dwc_mdelay(100);
39814 + }
39815 +
39816 + /* Enable Power Down Logic */
39817 + gpwrdn.d32 = 0;
39818 + gpwrdn.b.pmuactv = 1;
39819 + DWC_MODIFY_REG32(&core_if->core_global_regs->
39820 + gpwrdn, 0, gpwrdn.d32);
39821 +
39822 + /* Unmask SRP detected interrupt from Power Down Logic */
39823 + gpwrdn.d32 = 0;
39824 + gpwrdn.b.srp_det_msk = 1;
39825 + DWC_MODIFY_REG32(&core_if->core_global_regs->
39826 + gpwrdn, 0, gpwrdn.d32);
39827 +
39828 + dwc_otg_adp_probe_start(core_if);
39829 + }
39830 +#endif
39831 + break;
39832 + case UHF_PORT_POWER:
39833 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
39834 + "SetPortFeature - USB_PORT_FEAT_POWER\n");
39835 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
39836 + hprt0.b.prtpwr = 1;
39837 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
39838 + break;
39839 + case UHF_PORT_RESET:
39840 + if ((core_if->power_down == 2)
39841 + && (core_if->hibernation_suspend == 1)) {
39842 + /* If we are going to exit from Hibernated
39843 + * state via USB RESET.
39844 + */
39845 + dwc_otg_host_hibernation_restore(core_if, 0, 1);
39846 + } else {
39847 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
39848 +
39849 + DWC_DEBUGPL(DBG_HCD,
39850 + "DWC OTG HCD HUB CONTROL - "
39851 + "SetPortFeature - USB_PORT_FEAT_RESET\n");
39852 + {
39853 + pcgcctl_data_t pcgcctl = {.d32 = 0 };
39854 + pcgcctl.b.enbl_sleep_gating = 1;
39855 + pcgcctl.b.stoppclk = 1;
39856 + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
39857 + DWC_WRITE_REG32(core_if->pcgcctl, 0);
39858 + }
39859 +#ifdef CONFIG_USB_DWC_OTG_LPM
39860 + {
39861 + glpmcfg_data_t lpmcfg;
39862 + lpmcfg.d32 =
39863 + DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
39864 + if (lpmcfg.b.prt_sleep_sts) {
39865 + lpmcfg.b.en_utmi_sleep = 0;
39866 + lpmcfg.b.hird_thres &= (~(1 << 4));
39867 + DWC_WRITE_REG32
39868 + (&core_if->core_global_regs->glpmcfg,
39869 + lpmcfg.d32);
39870 + dwc_mdelay(1);
39871 + }
39872 + }
39873 +#endif
39874 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
39875 + /* Clear suspend bit if resetting from suspended state. */
39876 + hprt0.b.prtsusp = 0;
39877 + /* When B-Host the Port reset bit is set in
39878 + * the Start HCD Callback function, so that
39879 + * the reset is started within 1ms of the HNP
39880 + * success interrupt. */
39881 + if (!dwc_otg_hcd_is_b_host(dwc_otg_hcd)) {
39882 + hprt0.b.prtpwr = 1;
39883 + hprt0.b.prtrst = 1;
39884 + DWC_PRINTF("Indeed it is in host mode hprt0 = %08x\n",hprt0.d32);
39885 + DWC_WRITE_REG32(core_if->host_if->hprt0,
39886 + hprt0.d32);
39887 + }
39888 + /* Clear reset bit in 10ms (FS/LS) or 50ms (HS) */
39889 + dwc_mdelay(60);
39890 + hprt0.b.prtrst = 0;
39891 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
39892 + core_if->lx_state = DWC_OTG_L0; /* Now back to the on state */
39893 + }
39894 + break;
39895 +#ifdef DWC_HS_ELECT_TST
39896 + case UHF_PORT_TEST:
39897 + {
39898 + uint32_t t;
39899 + gintmsk_data_t gintmsk;
39900 +
39901 + t = (wIndex >> 8); /* MSB wIndex USB */
39902 + DWC_DEBUGPL(DBG_HCD,
39903 + "DWC OTG HCD HUB CONTROL - "
39904 + "SetPortFeature - USB_PORT_FEAT_TEST %d\n",
39905 + t);
39906 + DWC_WARN("USB_PORT_FEAT_TEST %d\n", t);
39907 + if (t < 6) {
39908 + hprt0.d32 = dwc_otg_read_hprt0(core_if);
39909 + hprt0.b.prttstctl = t;
39910 + DWC_WRITE_REG32(core_if->host_if->hprt0,
39911 + hprt0.d32);
39912 + } else {
39913 + /* Setup global vars with reg addresses (quick and
39914 + * dirty hack, should be cleaned up)
39915 + */
39916 + global_regs = core_if->core_global_regs;
39917 + hc_global_regs =
39918 + core_if->host_if->host_global_regs;
39919 + hc_regs =
39920 + (dwc_otg_hc_regs_t *) ((char *)
39921 + global_regs +
39922 + 0x500);
39923 + data_fifo =
39924 + (uint32_t *) ((char *)global_regs +
39925 + 0x1000);
39926 +
39927 + if (t == 6) { /* HS_HOST_PORT_SUSPEND_RESUME */
39928 + /* Save current interrupt mask */
39929 + gintmsk.d32 =
39930 + DWC_READ_REG32
39931 + (&global_regs->gintmsk);
39932 +
39933 + /* Disable all interrupts while we muck with
39934 + * the hardware directly
39935 + */
39936 + DWC_WRITE_REG32(&global_regs->gintmsk, 0);
39937 +
39938 + /* 15 second delay per the test spec */
39939 + dwc_mdelay(15000);
39940 +
39941 + /* Drive suspend on the root port */
39942 + hprt0.d32 =
39943 + dwc_otg_read_hprt0(core_if);
39944 + hprt0.b.prtsusp = 1;
39945 + hprt0.b.prtres = 0;
39946 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
39947 +
39948 + /* 15 second delay per the test spec */
39949 + dwc_mdelay(15000);
39950 +
39951 + /* Drive resume on the root port */
39952 + hprt0.d32 =
39953 + dwc_otg_read_hprt0(core_if);
39954 + hprt0.b.prtsusp = 0;
39955 + hprt0.b.prtres = 1;
39956 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
39957 + dwc_mdelay(100);
39958 +
39959 + /* Clear the resume bit */
39960 + hprt0.b.prtres = 0;
39961 + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
39962 +
39963 + /* Restore interrupts */
39964 + DWC_WRITE_REG32(&global_regs->gintmsk, gintmsk.d32);
39965 + } else if (t == 7) { /* SINGLE_STEP_GET_DEVICE_DESCRIPTOR setup */
39966 + /* Save current interrupt mask */
39967 + gintmsk.d32 =
39968 + DWC_READ_REG32
39969 + (&global_regs->gintmsk);
39970 +
39971 + /* Disable all interrupts while we muck with
39972 + * the hardware directly
39973 + */
39974 + DWC_WRITE_REG32(&global_regs->gintmsk, 0);
39975 +
39976 + /* 15 second delay per the test spec */
39977 + dwc_mdelay(15000);
39978 +
39979 + /* Send the Setup packet */
39980 + do_setup();
39981 +
39982 + /* 15 second delay so nothing else happens for awhile */
39983 + dwc_mdelay(15000);
39984 +
39985 + /* Restore interrupts */
39986 + DWC_WRITE_REG32(&global_regs->gintmsk, gintmsk.d32);
39987 + } else if (t == 8) { /* SINGLE_STEP_GET_DEVICE_DESCRIPTOR execute */
39988 + /* Save current interrupt mask */
39989 + gintmsk.d32 =
39990 + DWC_READ_REG32
39991 + (&global_regs->gintmsk);
39992 +
39993 + /* Disable all interrupts while we muck with
39994 + * the hardware directly
39995 + */
39996 + DWC_WRITE_REG32(&global_regs->gintmsk, 0);
39997 +
39998 + /* Send the Setup packet */
39999 + do_setup();
40000 +
40001 + /* 15 second delay so nothing else happens for awhile */
40002 + dwc_mdelay(15000);
40003 +
40004 + /* Send the In and Ack packets */
40005 + do_in_ack();
40006 +
40007 + /* 15 second delay so nothing else happens for awhile */
40008 + dwc_mdelay(15000);
40009 +
40010 + /* Restore interrupts */
40011 + DWC_WRITE_REG32(&global_regs->gintmsk, gintmsk.d32);
40012 + }
40013 + }
40014 + break;
40015 + }
40016 +#endif /* DWC_HS_ELECT_TST */
40017 +
40018 + case UHF_PORT_INDICATOR:
40019 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
40020 + "SetPortFeature - USB_PORT_FEAT_INDICATOR\n");
40021 + /* Not supported */
40022 + break;
40023 + default:
40024 + retval = -DWC_E_INVALID;
40025 + DWC_ERROR("DWC OTG HCD - "
40026 + "SetPortFeature request %xh "
40027 + "unknown or unsupported\n", wValue);
40028 + break;
40029 + }
40030 + break;
40031 +#ifdef CONFIG_USB_DWC_OTG_LPM
40032 + case UCR_SET_AND_TEST_PORT_FEATURE:
40033 + if (wValue != UHF_PORT_L1) {
40034 + goto error;
40035 + }
40036 + {
40037 + int portnum, hird, devaddr, remwake;
40038 + glpmcfg_data_t lpmcfg;
40039 + uint32_t time_usecs;
40040 + gintsts_data_t gintsts;
40041 + gintmsk_data_t gintmsk;
40042 +
40043 + if (!dwc_otg_get_param_lpm_enable(core_if)) {
40044 + goto error;
40045 + }
40046 + if (wValue != UHF_PORT_L1 || wLength != 1) {
40047 + goto error;
40048 + }
40049 + /* Check if the port currently is in SLEEP state */
40050 + lpmcfg.d32 =
40051 + DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
40052 + if (lpmcfg.b.prt_sleep_sts) {
40053 + DWC_INFO("Port is already in sleep mode\n");
40054 + buf[0] = 0; /* Return success */
40055 + break;
40056 + }
40057 +
40058 + portnum = wIndex & 0xf;
40059 + hird = (wIndex >> 4) & 0xf;
40060 + devaddr = (wIndex >> 8) & 0x7f;
40061 + remwake = (wIndex >> 15);
40062 +
40063 + if (portnum != 1) {
40064 + retval = -DWC_E_INVALID;
40065 + DWC_WARN
40066 + ("Wrong port number(%d) in SetandTestPortFeature request\n",
40067 + portnum);
40068 + break;
40069 + }
40070 +
40071 + DWC_PRINTF
40072 + ("SetandTestPortFeature request: portnum = %d, hird = %d, devaddr = %d, rewake = %d\n",
40073 + portnum, hird, devaddr, remwake);
40074 + /* Disable LPM interrupt */
40075 + gintmsk.d32 = 0;
40076 + gintmsk.b.lpmtranrcvd = 1;
40077 + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk,
40078 + gintmsk.d32, 0);
40079 +
40080 + if (dwc_otg_hcd_send_lpm
40081 + (dwc_otg_hcd, devaddr, hird, remwake)) {
40082 + retval = -DWC_E_INVALID;
40083 + break;
40084 + }
40085 +
40086 + time_usecs = 10 * (lpmcfg.b.retry_count + 1);
40087 + /* We will consider timeout if time_usecs microseconds pass,
40088 + * and we don't receive LPM transaction status.
40089 + * After receiving non-error responce(ACK/NYET/STALL) from device,
40090 + * core will set lpmtranrcvd bit.
40091 + */
40092 + do {
40093 + gintsts.d32 =
40094 + DWC_READ_REG32(&core_if->core_global_regs->gintsts);
40095 + if (gintsts.b.lpmtranrcvd) {
40096 + break;
40097 + }
40098 + dwc_udelay(1);
40099 + } while (--time_usecs);
40100 + /* lpm_int bit will be cleared in LPM interrupt handler */
40101 +
40102 + /* Now fill status
40103 + * 0x00 - Success
40104 + * 0x10 - NYET
40105 + * 0x11 - Timeout
40106 + */
40107 + if (!gintsts.b.lpmtranrcvd) {
40108 + buf[0] = 0x3; /* Completion code is Timeout */
40109 + dwc_otg_hcd_free_hc_from_lpm(dwc_otg_hcd);
40110 + } else {
40111 + lpmcfg.d32 =
40112 + DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
40113 + if (lpmcfg.b.lpm_resp == 0x3) {
40114 + /* ACK responce from the device */
40115 + buf[0] = 0x00; /* Success */
40116 + } else if (lpmcfg.b.lpm_resp == 0x2) {
40117 + /* NYET responce from the device */
40118 + buf[0] = 0x2;
40119 + } else {
40120 + /* Otherwise responce with Timeout */
40121 + buf[0] = 0x3;
40122 + }
40123 + }
40124 + DWC_PRINTF("Device responce to LPM trans is %x\n",
40125 + lpmcfg.b.lpm_resp);
40126 + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, 0,
40127 + gintmsk.d32);
40128 +
40129 + break;
40130 + }
40131 +#endif /* CONFIG_USB_DWC_OTG_LPM */
40132 + default:
40133 +error:
40134 + retval = -DWC_E_INVALID;
40135 + DWC_WARN("DWC OTG HCD - "
40136 + "Unknown hub control request type or invalid typeReq: %xh wIndex: %xh wValue: %xh\n",
40137 + typeReq, wIndex, wValue);
40138 + break;
40139 + }
40140 +
40141 + return retval;
40142 +}
40143 +
40144 +#ifdef CONFIG_USB_DWC_OTG_LPM
40145 +/** Returns index of host channel to perform LPM transaction. */
40146 +int dwc_otg_hcd_get_hc_for_lpm_tran(dwc_otg_hcd_t * hcd, uint8_t devaddr)
40147 +{
40148 + dwc_otg_core_if_t *core_if = hcd->core_if;
40149 + dwc_hc_t *hc;
40150 + hcchar_data_t hcchar;
40151 + gintmsk_data_t gintmsk = {.d32 = 0 };
40152 +
40153 + if (DWC_CIRCLEQ_EMPTY(&hcd->free_hc_list)) {
40154 + DWC_PRINTF("No free channel to select for LPM transaction\n");
40155 + return -1;
40156 + }
40157 +
40158 + hc = DWC_CIRCLEQ_FIRST(&hcd->free_hc_list);
40159 +
40160 + /* Mask host channel interrupts. */
40161 + gintmsk.b.hcintr = 1;
40162 + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, gintmsk.d32, 0);
40163 +
40164 + /* Fill fields that core needs for LPM transaction */
40165 + hcchar.b.devaddr = devaddr;
40166 + hcchar.b.epnum = 0;
40167 + hcchar.b.eptype = DWC_OTG_EP_TYPE_CONTROL;
40168 + hcchar.b.mps = 64;
40169 + hcchar.b.lspddev = (hc->speed == DWC_OTG_EP_SPEED_LOW);
40170 + hcchar.b.epdir = 0; /* OUT */
40171 + DWC_WRITE_REG32(&core_if->host_if->hc_regs[hc->hc_num]->hcchar,
40172 + hcchar.d32);
40173 +
40174 + /* Remove the host channel from the free list. */
40175 + DWC_CIRCLEQ_REMOVE_INIT(&hcd->free_hc_list, hc, hc_list_entry);
40176 +
40177 + DWC_PRINTF("hcnum = %d devaddr = %d\n", hc->hc_num, devaddr);
40178 +
40179 + return hc->hc_num;
40180 +}
40181 +
40182 +/** Release hc after performing LPM transaction */
40183 +void dwc_otg_hcd_free_hc_from_lpm(dwc_otg_hcd_t * hcd)
40184 +{
40185 + dwc_hc_t *hc;
40186 + glpmcfg_data_t lpmcfg;
40187 + uint8_t hc_num;
40188 +
40189 + lpmcfg.d32 = DWC_READ_REG32(&hcd->core_if->core_global_regs->glpmcfg);
40190 + hc_num = lpmcfg.b.lpm_chan_index;
40191 +
40192 + hc = hcd->hc_ptr_array[hc_num];
40193 +
40194 + DWC_PRINTF("Freeing channel %d after LPM\n", hc_num);
40195 + /* Return host channel to free list */
40196 + DWC_CIRCLEQ_INSERT_TAIL(&hcd->free_hc_list, hc, hc_list_entry);
40197 +}
40198 +
40199 +int dwc_otg_hcd_send_lpm(dwc_otg_hcd_t * hcd, uint8_t devaddr, uint8_t hird,
40200 + uint8_t bRemoteWake)
40201 +{
40202 + glpmcfg_data_t lpmcfg;
40203 + pcgcctl_data_t pcgcctl = {.d32 = 0 };
40204 + int channel;
40205 +
40206 + channel = dwc_otg_hcd_get_hc_for_lpm_tran(hcd, devaddr);
40207 + if (channel < 0) {
40208 + return channel;
40209 + }
40210 +
40211 + pcgcctl.b.enbl_sleep_gating = 1;
40212 + DWC_MODIFY_REG32(hcd->core_if->pcgcctl, 0, pcgcctl.d32);
40213 +
40214 + /* Read LPM config register */
40215 + lpmcfg.d32 = DWC_READ_REG32(&hcd->core_if->core_global_regs->glpmcfg);
40216 +
40217 + /* Program LPM transaction fields */
40218 + lpmcfg.b.rem_wkup_en = bRemoteWake;
40219 + lpmcfg.b.hird = hird;
40220 + lpmcfg.b.hird_thres = 0x1c;
40221 + lpmcfg.b.lpm_chan_index = channel;
40222 + lpmcfg.b.en_utmi_sleep = 1;
40223 + /* Program LPM config register */
40224 + DWC_WRITE_REG32(&hcd->core_if->core_global_regs->glpmcfg, lpmcfg.d32);
40225 +
40226 + /* Send LPM transaction */
40227 + lpmcfg.b.send_lpm = 1;
40228 + DWC_WRITE_REG32(&hcd->core_if->core_global_regs->glpmcfg, lpmcfg.d32);
40229 +
40230 + return 0;
40231 +}
40232 +
40233 +#endif /* CONFIG_USB_DWC_OTG_LPM */
40234 +
40235 +int dwc_otg_hcd_is_status_changed(dwc_otg_hcd_t * hcd, int port)
40236 +{
40237 + int retval;
40238 +
40239 + if (port != 1) {
40240 + return -DWC_E_INVALID;
40241 + }
40242 +
40243 + retval = (hcd->flags.b.port_connect_status_change ||
40244 + hcd->flags.b.port_reset_change ||
40245 + hcd->flags.b.port_enable_change ||
40246 + hcd->flags.b.port_suspend_change ||
40247 + hcd->flags.b.port_over_current_change);
40248 +#ifdef DEBUG
40249 + if (retval) {
40250 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB STATUS DATA:"
40251 + " Root port status changed\n");
40252 + DWC_DEBUGPL(DBG_HCDV, " port_connect_status_change: %d\n",
40253 + hcd->flags.b.port_connect_status_change);
40254 + DWC_DEBUGPL(DBG_HCDV, " port_reset_change: %d\n",
40255 + hcd->flags.b.port_reset_change);
40256 + DWC_DEBUGPL(DBG_HCDV, " port_enable_change: %d\n",
40257 + hcd->flags.b.port_enable_change);
40258 + DWC_DEBUGPL(DBG_HCDV, " port_suspend_change: %d\n",
40259 + hcd->flags.b.port_suspend_change);
40260 + DWC_DEBUGPL(DBG_HCDV, " port_over_current_change: %d\n",
40261 + hcd->flags.b.port_over_current_change);
40262 + }
40263 +#endif
40264 + return retval;
40265 +}
40266 +
40267 +int dwc_otg_hcd_get_frame_number(dwc_otg_hcd_t * dwc_otg_hcd)
40268 +{
40269 + hfnum_data_t hfnum;
40270 + hfnum.d32 =
40271 + DWC_READ_REG32(&dwc_otg_hcd->core_if->host_if->host_global_regs->
40272 + hfnum);
40273 +
40274 +#ifdef DEBUG_SOF
40275 + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD GET FRAME NUMBER %d\n",
40276 + hfnum.b.frnum);
40277 +#endif
40278 + return hfnum.b.frnum;
40279 +}
40280 +
40281 +int dwc_otg_hcd_start(dwc_otg_hcd_t * hcd,
40282 + struct dwc_otg_hcd_function_ops *fops)
40283 +{
40284 + int retval = 0;
40285 +
40286 + hcd->fops = fops;
40287 + if (!dwc_otg_is_device_mode(hcd->core_if) &&
40288 + (!hcd->core_if->adp_enable || hcd->core_if->adp.adp_started)) {
40289 + dwc_otg_hcd_reinit(hcd);
40290 + } else {
40291 + retval = -DWC_E_NO_DEVICE;
40292 + }
40293 +
40294 + return retval;
40295 +}
40296 +
40297 +void *dwc_otg_hcd_get_priv_data(dwc_otg_hcd_t * hcd)
40298 +{
40299 + return hcd->priv;
40300 +}
40301 +
40302 +void dwc_otg_hcd_set_priv_data(dwc_otg_hcd_t * hcd, void *priv_data)
40303 +{
40304 + hcd->priv = priv_data;
40305 +}
40306 +
40307 +uint32_t dwc_otg_hcd_otg_port(dwc_otg_hcd_t * hcd)
40308 +{
40309 + return hcd->otg_port;
40310 +}
40311 +
40312 +uint32_t dwc_otg_hcd_is_b_host(dwc_otg_hcd_t * hcd)
40313 +{
40314 + uint32_t is_b_host;
40315 + if (hcd->core_if->op_state == B_HOST) {
40316 + is_b_host = 1;
40317 + } else {
40318 + is_b_host = 0;
40319 + }
40320 +
40321 + return is_b_host;
40322 +}
40323 +
40324 +dwc_otg_hcd_urb_t *dwc_otg_hcd_urb_alloc(dwc_otg_hcd_t * hcd,
40325 + int iso_desc_count, int atomic_alloc)
40326 +{
40327 + dwc_otg_hcd_urb_t *dwc_otg_urb;
40328 + uint32_t size;
40329 +
40330 + size =
40331 + sizeof(*dwc_otg_urb) +
40332 + iso_desc_count * sizeof(struct dwc_otg_hcd_iso_packet_desc);
40333 + if (atomic_alloc)
40334 + dwc_otg_urb = DWC_ALLOC_ATOMIC(size);
40335 + else
40336 + dwc_otg_urb = DWC_ALLOC(size);
40337 +
40338 + if (dwc_otg_urb)
40339 + dwc_otg_urb->packet_count = iso_desc_count;
40340 + else {
40341 + DWC_ERROR("**** DWC OTG HCD URB alloc - "
40342 + "%salloc of %db failed\n",
40343 + atomic_alloc?"atomic ":"", size);
40344 + }
40345 + return dwc_otg_urb;
40346 +}
40347 +
40348 +void dwc_otg_hcd_urb_set_pipeinfo(dwc_otg_hcd_urb_t * dwc_otg_urb,
40349 + uint8_t dev_addr, uint8_t ep_num,
40350 + uint8_t ep_type, uint8_t ep_dir, uint16_t mps)
40351 +{
40352 + dwc_otg_hcd_fill_pipe(&dwc_otg_urb->pipe_info, dev_addr, ep_num,
40353 + ep_type, ep_dir, mps);
40354 +#if 0
40355 + DWC_PRINTF
40356 + ("addr = %d, ep_num = %d, ep_dir = 0x%x, ep_type = 0x%x, mps = %d\n",
40357 + dev_addr, ep_num, ep_dir, ep_type, mps);
40358 +#endif
40359 +}
40360 +
40361 +void dwc_otg_hcd_urb_set_params(dwc_otg_hcd_urb_t * dwc_otg_urb,
40362 + void *urb_handle, void *buf, dwc_dma_t dma,
40363 + uint32_t buflen, void *setup_packet,
40364 + dwc_dma_t setup_dma, uint32_t flags,
40365 + uint16_t interval)
40366 +{
40367 + dwc_otg_urb->priv = urb_handle;
40368 + dwc_otg_urb->buf = buf;
40369 + dwc_otg_urb->dma = dma;
40370 + dwc_otg_urb->length = buflen;
40371 + dwc_otg_urb->setup_packet = setup_packet;
40372 + dwc_otg_urb->setup_dma = setup_dma;
40373 + dwc_otg_urb->flags = flags;
40374 + dwc_otg_urb->interval = interval;
40375 + dwc_otg_urb->status = -DWC_E_IN_PROGRESS;
40376 +}
40377 +
40378 +uint32_t dwc_otg_hcd_urb_get_status(dwc_otg_hcd_urb_t * dwc_otg_urb)
40379 +{
40380 + return dwc_otg_urb->status;
40381 +}
40382 +
40383 +uint32_t dwc_otg_hcd_urb_get_actual_length(dwc_otg_hcd_urb_t * dwc_otg_urb)
40384 +{
40385 + return dwc_otg_urb->actual_length;
40386 +}
40387 +
40388 +uint32_t dwc_otg_hcd_urb_get_error_count(dwc_otg_hcd_urb_t * dwc_otg_urb)
40389 +{
40390 + return dwc_otg_urb->error_count;
40391 +}
40392 +
40393 +void dwc_otg_hcd_urb_set_iso_desc_params(dwc_otg_hcd_urb_t * dwc_otg_urb,
40394 + int desc_num, uint32_t offset,
40395 + uint32_t length)
40396 +{
40397 + dwc_otg_urb->iso_descs[desc_num].offset = offset;
40398 + dwc_otg_urb->iso_descs[desc_num].length = length;
40399 +}
40400 +
40401 +uint32_t dwc_otg_hcd_urb_get_iso_desc_status(dwc_otg_hcd_urb_t * dwc_otg_urb,
40402 + int desc_num)
40403 +{
40404 + return dwc_otg_urb->iso_descs[desc_num].status;
40405 +}
40406 +
40407 +uint32_t dwc_otg_hcd_urb_get_iso_desc_actual_length(dwc_otg_hcd_urb_t *
40408 + dwc_otg_urb, int desc_num)
40409 +{
40410 + return dwc_otg_urb->iso_descs[desc_num].actual_length;
40411 +}
40412 +
40413 +int dwc_otg_hcd_is_bandwidth_allocated(dwc_otg_hcd_t * hcd, void *ep_handle)
40414 +{
40415 + int allocated = 0;
40416 + dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
40417 +
40418 + if (qh) {
40419 + if (!DWC_LIST_EMPTY(&qh->qh_list_entry)) {
40420 + allocated = 1;
40421 + }
40422 + }
40423 + return allocated;
40424 +}
40425 +
40426 +int dwc_otg_hcd_is_bandwidth_freed(dwc_otg_hcd_t * hcd, void *ep_handle)
40427 +{
40428 + dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
40429 + int freed = 0;
40430 + DWC_ASSERT(qh, "qh is not allocated\n");
40431 +
40432 + if (DWC_LIST_EMPTY(&qh->qh_list_entry)) {
40433 + freed = 1;
40434 + }
40435 +
40436 + return freed;
40437 +}
40438 +
40439 +uint8_t dwc_otg_hcd_get_ep_bandwidth(dwc_otg_hcd_t * hcd, void *ep_handle)
40440 +{
40441 + dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
40442 + DWC_ASSERT(qh, "qh is not allocated\n");
40443 + return qh->usecs;
40444 +}
40445 +
40446 +void dwc_otg_hcd_dump_state(dwc_otg_hcd_t * hcd)
40447 +{
40448 +#ifdef DEBUG
40449 + int num_channels;
40450 + int i;
40451 + gnptxsts_data_t np_tx_status;
40452 + hptxsts_data_t p_tx_status;
40453 +
40454 + num_channels = hcd->core_if->core_params->host_channels;
40455 + DWC_PRINTF("\n");
40456 + DWC_PRINTF
40457 + ("************************************************************\n");
40458 + DWC_PRINTF("HCD State:\n");
40459 + DWC_PRINTF(" Num channels: %d\n", num_channels);
40460 + for (i = 0; i < num_channels; i++) {
40461 + dwc_hc_t *hc = hcd->hc_ptr_array[i];
40462 + DWC_PRINTF(" Channel %d:\n", i);
40463 + DWC_PRINTF(" dev_addr: %d, ep_num: %d, ep_is_in: %d\n",
40464 + hc->dev_addr, hc->ep_num, hc->ep_is_in);
40465 + DWC_PRINTF(" speed: %d\n", hc->speed);
40466 + DWC_PRINTF(" ep_type: %d\n", hc->ep_type);
40467 + DWC_PRINTF(" max_packet: %d\n", hc->max_packet);
40468 + DWC_PRINTF(" data_pid_start: %d\n", hc->data_pid_start);
40469 + DWC_PRINTF(" multi_count: %d\n", hc->multi_count);
40470 + DWC_PRINTF(" xfer_started: %d\n", hc->xfer_started);
40471 + DWC_PRINTF(" xfer_buff: %p\n", hc->xfer_buff);
40472 + DWC_PRINTF(" xfer_len: %d\n", hc->xfer_len);
40473 + DWC_PRINTF(" xfer_count: %d\n", hc->xfer_count);
40474 + DWC_PRINTF(" halt_on_queue: %d\n", hc->halt_on_queue);
40475 + DWC_PRINTF(" halt_pending: %d\n", hc->halt_pending);
40476 + DWC_PRINTF(" halt_status: %d\n", hc->halt_status);
40477 + DWC_PRINTF(" do_split: %d\n", hc->do_split);
40478 + DWC_PRINTF(" complete_split: %d\n", hc->complete_split);
40479 + DWC_PRINTF(" hub_addr: %d\n", hc->hub_addr);
40480 + DWC_PRINTF(" port_addr: %d\n", hc->port_addr);
40481 + DWC_PRINTF(" xact_pos: %d\n", hc->xact_pos);
40482 + DWC_PRINTF(" requests: %d\n", hc->requests);
40483 + DWC_PRINTF(" qh: %p\n", hc->qh);
40484 + if (hc->xfer_started) {
40485 + hfnum_data_t hfnum;
40486 + hcchar_data_t hcchar;
40487 + hctsiz_data_t hctsiz;
40488 + hcint_data_t hcint;
40489 + hcintmsk_data_t hcintmsk;
40490 + hfnum.d32 =
40491 + DWC_READ_REG32(&hcd->core_if->
40492 + host_if->host_global_regs->hfnum);
40493 + hcchar.d32 =
40494 + DWC_READ_REG32(&hcd->core_if->host_if->
40495 + hc_regs[i]->hcchar);
40496 + hctsiz.d32 =
40497 + DWC_READ_REG32(&hcd->core_if->host_if->
40498 + hc_regs[i]->hctsiz);
40499 + hcint.d32 =
40500 + DWC_READ_REG32(&hcd->core_if->host_if->
40501 + hc_regs[i]->hcint);
40502 + hcintmsk.d32 =
40503 + DWC_READ_REG32(&hcd->core_if->host_if->
40504 + hc_regs[i]->hcintmsk);
40505 + DWC_PRINTF(" hfnum: 0x%08x\n", hfnum.d32);
40506 + DWC_PRINTF(" hcchar: 0x%08x\n", hcchar.d32);
40507 + DWC_PRINTF(" hctsiz: 0x%08x\n", hctsiz.d32);
40508 + DWC_PRINTF(" hcint: 0x%08x\n", hcint.d32);
40509 + DWC_PRINTF(" hcintmsk: 0x%08x\n", hcintmsk.d32);
40510 + }
40511 + if (hc->xfer_started && hc->qh) {
40512 + dwc_otg_qtd_t *qtd;
40513 + dwc_otg_hcd_urb_t *urb;
40514 +
40515 + DWC_CIRCLEQ_FOREACH(qtd, &hc->qh->qtd_list, qtd_list_entry) {
40516 + if (!qtd->in_process)
40517 + break;
40518 +
40519 + urb = qtd->urb;
40520 + DWC_PRINTF(" URB Info:\n");
40521 + DWC_PRINTF(" qtd: %p, urb: %p\n", qtd, urb);
40522 + if (urb) {
40523 + DWC_PRINTF(" Dev: %d, EP: %d %s\n",
40524 + dwc_otg_hcd_get_dev_addr(&urb->
40525 + pipe_info),
40526 + dwc_otg_hcd_get_ep_num(&urb->
40527 + pipe_info),
40528 + dwc_otg_hcd_is_pipe_in(&urb->
40529 + pipe_info) ?
40530 + "IN" : "OUT");
40531 + DWC_PRINTF(" Max packet size: %d\n",
40532 + dwc_otg_hcd_get_mps(&urb->
40533 + pipe_info));
40534 + DWC_PRINTF(" transfer_buffer: %p\n",
40535 + urb->buf);
40536 + DWC_PRINTF(" transfer_dma: %p\n",
40537 + (void *)urb->dma);
40538 + DWC_PRINTF(" transfer_buffer_length: %d\n",
40539 + urb->length);
40540 + DWC_PRINTF(" actual_length: %d\n",
40541 + urb->actual_length);
40542 + }
40543 + }
40544 + }
40545 + }
40546 + DWC_PRINTF(" non_periodic_channels: %d\n", hcd->non_periodic_channels);
40547 + DWC_PRINTF(" periodic_channels: %d\n", hcd->periodic_channels);
40548 + DWC_PRINTF(" periodic_usecs: %d\n", hcd->periodic_usecs);
40549 + np_tx_status.d32 =
40550 + DWC_READ_REG32(&hcd->core_if->core_global_regs->gnptxsts);
40551 + DWC_PRINTF(" NP Tx Req Queue Space Avail: %d\n",
40552 + np_tx_status.b.nptxqspcavail);
40553 + DWC_PRINTF(" NP Tx FIFO Space Avail: %d\n",
40554 + np_tx_status.b.nptxfspcavail);
40555 + p_tx_status.d32 =
40556 + DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hptxsts);
40557 + DWC_PRINTF(" P Tx Req Queue Space Avail: %d\n",
40558 + p_tx_status.b.ptxqspcavail);
40559 + DWC_PRINTF(" P Tx FIFO Space Avail: %d\n", p_tx_status.b.ptxfspcavail);
40560 + dwc_otg_hcd_dump_frrem(hcd);
40561 + dwc_otg_dump_global_registers(hcd->core_if);
40562 + dwc_otg_dump_host_registers(hcd->core_if);
40563 + DWC_PRINTF
40564 + ("************************************************************\n");
40565 + DWC_PRINTF("\n");
40566 +#endif
40567 +}
40568 +
40569 +#ifdef DEBUG
40570 +void dwc_print_setup_data(uint8_t * setup)
40571 +{
40572 + int i;
40573 + if (CHK_DEBUG_LEVEL(DBG_HCD)) {
40574 + DWC_PRINTF("Setup Data = MSB ");
40575 + for (i = 7; i >= 0; i--)
40576 + DWC_PRINTF("%02x ", setup[i]);
40577 + DWC_PRINTF("\n");
40578 + DWC_PRINTF(" bmRequestType Tranfer = %s\n",
40579 + (setup[0] & 0x80) ? "Device-to-Host" :
40580 + "Host-to-Device");
40581 + DWC_PRINTF(" bmRequestType Type = ");
40582 + switch ((setup[0] & 0x60) >> 5) {
40583 + case 0:
40584 + DWC_PRINTF("Standard\n");
40585 + break;
40586 + case 1:
40587 + DWC_PRINTF("Class\n");
40588 + break;
40589 + case 2:
40590 + DWC_PRINTF("Vendor\n");
40591 + break;
40592 + case 3:
40593 + DWC_PRINTF("Reserved\n");
40594 + break;
40595 + }
40596 + DWC_PRINTF(" bmRequestType Recipient = ");
40597 + switch (setup[0] & 0x1f) {
40598 + case 0:
40599 + DWC_PRINTF("Device\n");
40600 + break;
40601 + case 1:
40602 + DWC_PRINTF("Interface\n");
40603 + break;
40604 + case 2:
40605 + DWC_PRINTF("Endpoint\n");
40606 + break;
40607 + case 3:
40608 + DWC_PRINTF("Other\n");
40609 + break;
40610 + default:
40611 + DWC_PRINTF("Reserved\n");
40612 + break;
40613 + }
40614 + DWC_PRINTF(" bRequest = 0x%0x\n", setup[1]);
40615 + DWC_PRINTF(" wValue = 0x%0x\n", *((uint16_t *) & setup[2]));
40616 + DWC_PRINTF(" wIndex = 0x%0x\n", *((uint16_t *) & setup[4]));
40617 + DWC_PRINTF(" wLength = 0x%0x\n\n", *((uint16_t *) & setup[6]));
40618 + }
40619 +}
40620 +#endif
40621 +
40622 +void dwc_otg_hcd_dump_frrem(dwc_otg_hcd_t * hcd)
40623 +{
40624 +#if 0
40625 + DWC_PRINTF("Frame remaining at SOF:\n");
40626 + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
40627 + hcd->frrem_samples, hcd->frrem_accum,
40628 + (hcd->frrem_samples > 0) ?
40629 + hcd->frrem_accum / hcd->frrem_samples : 0);
40630 +
40631 + DWC_PRINTF("\n");
40632 + DWC_PRINTF("Frame remaining at start_transfer (uframe 7):\n");
40633 + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
40634 + hcd->core_if->hfnum_7_samples,
40635 + hcd->core_if->hfnum_7_frrem_accum,
40636 + (hcd->core_if->hfnum_7_samples >
40637 + 0) ? hcd->core_if->hfnum_7_frrem_accum /
40638 + hcd->core_if->hfnum_7_samples : 0);
40639 + DWC_PRINTF("Frame remaining at start_transfer (uframe 0):\n");
40640 + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
40641 + hcd->core_if->hfnum_0_samples,
40642 + hcd->core_if->hfnum_0_frrem_accum,
40643 + (hcd->core_if->hfnum_0_samples >
40644 + 0) ? hcd->core_if->hfnum_0_frrem_accum /
40645 + hcd->core_if->hfnum_0_samples : 0);
40646 + DWC_PRINTF("Frame remaining at start_transfer (uframe 1-6):\n");
40647 + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
40648 + hcd->core_if->hfnum_other_samples,
40649 + hcd->core_if->hfnum_other_frrem_accum,
40650 + (hcd->core_if->hfnum_other_samples >
40651 + 0) ? hcd->core_if->hfnum_other_frrem_accum /
40652 + hcd->core_if->hfnum_other_samples : 0);
40653 +
40654 + DWC_PRINTF("\n");
40655 + DWC_PRINTF("Frame remaining at sample point A (uframe 7):\n");
40656 + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
40657 + hcd->hfnum_7_samples_a, hcd->hfnum_7_frrem_accum_a,
40658 + (hcd->hfnum_7_samples_a > 0) ?
40659 + hcd->hfnum_7_frrem_accum_a / hcd->hfnum_7_samples_a : 0);
40660 + DWC_PRINTF("Frame remaining at sample point A (uframe 0):\n");
40661 + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
40662 + hcd->hfnum_0_samples_a, hcd->hfnum_0_frrem_accum_a,
40663 + (hcd->hfnum_0_samples_a > 0) ?
40664 + hcd->hfnum_0_frrem_accum_a / hcd->hfnum_0_samples_a : 0);
40665 + DWC_PRINTF("Frame remaining at sample point A (uframe 1-6):\n");
40666 + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
40667 + hcd->hfnum_other_samples_a, hcd->hfnum_other_frrem_accum_a,
40668 + (hcd->hfnum_other_samples_a > 0) ?
40669 + hcd->hfnum_other_frrem_accum_a /
40670 + hcd->hfnum_other_samples_a : 0);
40671 +
40672 + DWC_PRINTF("\n");
40673 + DWC_PRINTF("Frame remaining at sample point B (uframe 7):\n");
40674 + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
40675 + hcd->hfnum_7_samples_b, hcd->hfnum_7_frrem_accum_b,
40676 + (hcd->hfnum_7_samples_b > 0) ?
40677 + hcd->hfnum_7_frrem_accum_b / hcd->hfnum_7_samples_b : 0);
40678 + DWC_PRINTF("Frame remaining at sample point B (uframe 0):\n");
40679 + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
40680 + hcd->hfnum_0_samples_b, hcd->hfnum_0_frrem_accum_b,
40681 + (hcd->hfnum_0_samples_b > 0) ?
40682 + hcd->hfnum_0_frrem_accum_b / hcd->hfnum_0_samples_b : 0);
40683 + DWC_PRINTF("Frame remaining at sample point B (uframe 1-6):\n");
40684 + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
40685 + hcd->hfnum_other_samples_b, hcd->hfnum_other_frrem_accum_b,
40686 + (hcd->hfnum_other_samples_b > 0) ?
40687 + hcd->hfnum_other_frrem_accum_b /
40688 + hcd->hfnum_other_samples_b : 0);
40689 +#endif
40690 +}
40691 +
40692 +#endif /* DWC_DEVICE_ONLY */
40693 --- /dev/null
40694 +++ b/drivers/usb/host/dwc_otg/dwc_otg_hcd.h
40695 @@ -0,0 +1,870 @@
40696 +/* ==========================================================================
40697 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd.h $
40698 + * $Revision: #58 $
40699 + * $Date: 2011/09/15 $
40700 + * $Change: 1846647 $
40701 + *
40702 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
40703 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
40704 + * otherwise expressly agreed to in writing between Synopsys and you.
40705 + *
40706 + * The Software IS NOT an item of Licensed Software or Licensed Product under
40707 + * any End User Software License Agreement or Agreement for Licensed Product
40708 + * with Synopsys or any supplement thereto. You are permitted to use and
40709 + * redistribute this Software in source and binary forms, with or without
40710 + * modification, provided that redistributions of source code must retain this
40711 + * notice. You may not view, use, disclose, copy or distribute this file or
40712 + * any information contained herein except pursuant to this license grant from
40713 + * Synopsys. If you do not agree with this notice, including the disclaimer
40714 + * below, then you are not authorized to use the Software.
40715 + *
40716 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
40717 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40718 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
40719 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
40720 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
40721 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
40722 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
40723 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40724 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
40725 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
40726 + * DAMAGE.
40727 + * ========================================================================== */
40728 +#ifndef DWC_DEVICE_ONLY
40729 +#ifndef __DWC_HCD_H__
40730 +#define __DWC_HCD_H__
40731 +
40732 +#include "dwc_otg_os_dep.h"
40733 +#include "usb.h"
40734 +#include "dwc_otg_hcd_if.h"
40735 +#include "dwc_otg_core_if.h"
40736 +#include "dwc_list.h"
40737 +#include "dwc_otg_cil.h"
40738 +#include "dwc_otg_fiq_fsm.h"
40739 +#include "dwc_otg_driver.h"
40740 +
40741 +
40742 +/**
40743 + * @file
40744 + *
40745 + * This file contains the structures, constants, and interfaces for
40746 + * the Host Contoller Driver (HCD).
40747 + *
40748 + * The Host Controller Driver (HCD) is responsible for translating requests
40749 + * from the USB Driver into the appropriate actions on the DWC_otg controller.
40750 + * It isolates the USBD from the specifics of the controller by providing an
40751 + * API to the USBD.
40752 + */
40753 +
40754 +struct dwc_otg_hcd_pipe_info {
40755 + uint8_t dev_addr;
40756 + uint8_t ep_num;
40757 + uint8_t pipe_type;
40758 + uint8_t pipe_dir;
40759 + uint16_t mps;
40760 +};
40761 +
40762 +struct dwc_otg_hcd_iso_packet_desc {
40763 + uint32_t offset;
40764 + uint32_t length;
40765 + uint32_t actual_length;
40766 + uint32_t status;
40767 +};
40768 +
40769 +struct dwc_otg_qtd;
40770 +
40771 +struct dwc_otg_hcd_urb {
40772 + void *priv;
40773 + struct dwc_otg_qtd *qtd;
40774 + void *buf;
40775 + dwc_dma_t dma;
40776 + void *setup_packet;
40777 + dwc_dma_t setup_dma;
40778 + uint32_t length;
40779 + uint32_t actual_length;
40780 + uint32_t status;
40781 + uint32_t error_count;
40782 + uint32_t packet_count;
40783 + uint32_t flags;
40784 + uint16_t interval;
40785 + struct dwc_otg_hcd_pipe_info pipe_info;
40786 + struct dwc_otg_hcd_iso_packet_desc iso_descs[0];
40787 +};
40788 +
40789 +static inline uint8_t dwc_otg_hcd_get_ep_num(struct dwc_otg_hcd_pipe_info *pipe)
40790 +{
40791 + return pipe->ep_num;
40792 +}
40793 +
40794 +static inline uint8_t dwc_otg_hcd_get_pipe_type(struct dwc_otg_hcd_pipe_info
40795 + *pipe)
40796 +{
40797 + return pipe->pipe_type;
40798 +}
40799 +
40800 +static inline uint16_t dwc_otg_hcd_get_mps(struct dwc_otg_hcd_pipe_info *pipe)
40801 +{
40802 + return pipe->mps;
40803 +}
40804 +
40805 +static inline uint8_t dwc_otg_hcd_get_dev_addr(struct dwc_otg_hcd_pipe_info
40806 + *pipe)
40807 +{
40808 + return pipe->dev_addr;
40809 +}
40810 +
40811 +static inline uint8_t dwc_otg_hcd_is_pipe_isoc(struct dwc_otg_hcd_pipe_info
40812 + *pipe)
40813 +{
40814 + return (pipe->pipe_type == UE_ISOCHRONOUS);
40815 +}
40816 +
40817 +static inline uint8_t dwc_otg_hcd_is_pipe_int(struct dwc_otg_hcd_pipe_info
40818 + *pipe)
40819 +{
40820 + return (pipe->pipe_type == UE_INTERRUPT);
40821 +}
40822 +
40823 +static inline uint8_t dwc_otg_hcd_is_pipe_bulk(struct dwc_otg_hcd_pipe_info
40824 + *pipe)
40825 +{
40826 + return (pipe->pipe_type == UE_BULK);
40827 +}
40828 +
40829 +static inline uint8_t dwc_otg_hcd_is_pipe_control(struct dwc_otg_hcd_pipe_info
40830 + *pipe)
40831 +{
40832 + return (pipe->pipe_type == UE_CONTROL);
40833 +}
40834 +
40835 +static inline uint8_t dwc_otg_hcd_is_pipe_in(struct dwc_otg_hcd_pipe_info *pipe)
40836 +{
40837 + return (pipe->pipe_dir == UE_DIR_IN);
40838 +}
40839 +
40840 +static inline uint8_t dwc_otg_hcd_is_pipe_out(struct dwc_otg_hcd_pipe_info
40841 + *pipe)
40842 +{
40843 + return (!dwc_otg_hcd_is_pipe_in(pipe));
40844 +}
40845 +
40846 +static inline void dwc_otg_hcd_fill_pipe(struct dwc_otg_hcd_pipe_info *pipe,
40847 + uint8_t devaddr, uint8_t ep_num,
40848 + uint8_t pipe_type, uint8_t pipe_dir,
40849 + uint16_t mps)
40850 +{
40851 + pipe->dev_addr = devaddr;
40852 + pipe->ep_num = ep_num;
40853 + pipe->pipe_type = pipe_type;
40854 + pipe->pipe_dir = pipe_dir;
40855 + pipe->mps = mps;
40856 +}
40857 +
40858 +/**
40859 + * Phases for control transfers.
40860 + */
40861 +typedef enum dwc_otg_control_phase {
40862 + DWC_OTG_CONTROL_SETUP,
40863 + DWC_OTG_CONTROL_DATA,
40864 + DWC_OTG_CONTROL_STATUS
40865 +} dwc_otg_control_phase_e;
40866 +
40867 +/** Transaction types. */
40868 +typedef enum dwc_otg_transaction_type {
40869 + DWC_OTG_TRANSACTION_NONE = 0,
40870 + DWC_OTG_TRANSACTION_PERIODIC = 1,
40871 + DWC_OTG_TRANSACTION_NON_PERIODIC = 2,
40872 + DWC_OTG_TRANSACTION_ALL = DWC_OTG_TRANSACTION_PERIODIC + DWC_OTG_TRANSACTION_NON_PERIODIC
40873 +} dwc_otg_transaction_type_e;
40874 +
40875 +struct dwc_otg_qh;
40876 +
40877 +/**
40878 + * A Queue Transfer Descriptor (QTD) holds the state of a bulk, control,
40879 + * interrupt, or isochronous transfer. A single QTD is created for each URB
40880 + * (of one of these types) submitted to the HCD. The transfer associated with
40881 + * a QTD may require one or multiple transactions.
40882 + *
40883 + * A QTD is linked to a Queue Head, which is entered in either the
40884 + * non-periodic or periodic schedule for execution. When a QTD is chosen for
40885 + * execution, some or all of its transactions may be executed. After
40886 + * execution, the state of the QTD is updated. The QTD may be retired if all
40887 + * its transactions are complete or if an error occurred. Otherwise, it
40888 + * remains in the schedule so more transactions can be executed later.
40889 + */
40890 +typedef struct dwc_otg_qtd {
40891 + /**
40892 + * Determines the PID of the next data packet for the data phase of
40893 + * control transfers. Ignored for other transfer types.<br>
40894 + * One of the following values:
40895 + * - DWC_OTG_HC_PID_DATA0
40896 + * - DWC_OTG_HC_PID_DATA1
40897 + */
40898 + uint8_t data_toggle;
40899 +
40900 + /** Current phase for control transfers (Setup, Data, or Status). */
40901 + dwc_otg_control_phase_e control_phase;
40902 +
40903 + /** Keep track of the current split type
40904 + * for FS/LS endpoints on a HS Hub */
40905 + uint8_t complete_split;
40906 +
40907 + /** How many bytes transferred during SSPLIT OUT */
40908 + uint32_t ssplit_out_xfer_count;
40909 +
40910 + /**
40911 + * Holds the number of bus errors that have occurred for a transaction
40912 + * within this transfer.
40913 + */
40914 + uint8_t error_count;
40915 +
40916 + /**
40917 + * Index of the next frame descriptor for an isochronous transfer. A
40918 + * frame descriptor describes the buffer position and length of the
40919 + * data to be transferred in the next scheduled (micro)frame of an
40920 + * isochronous transfer. It also holds status for that transaction.
40921 + * The frame index starts at 0.
40922 + */
40923 + uint16_t isoc_frame_index;
40924 +
40925 + /** Position of the ISOC split on full/low speed */
40926 + uint8_t isoc_split_pos;
40927 +
40928 + /** Position of the ISOC split in the buffer for the current frame */
40929 + uint16_t isoc_split_offset;
40930 +
40931 + /** URB for this transfer */
40932 + struct dwc_otg_hcd_urb *urb;
40933 +
40934 + struct dwc_otg_qh *qh;
40935 +
40936 + /** This list of QTDs */
40937 + DWC_CIRCLEQ_ENTRY(dwc_otg_qtd) qtd_list_entry;
40938 +
40939 + /** Indicates if this QTD is currently processed by HW. */
40940 + uint8_t in_process;
40941 +
40942 + /** Number of DMA descriptors for this QTD */
40943 + uint8_t n_desc;
40944 +
40945 + /**
40946 + * Last activated frame(packet) index.
40947 + * Used in Descriptor DMA mode only.
40948 + */
40949 + uint16_t isoc_frame_index_last;
40950 +
40951 +} dwc_otg_qtd_t;
40952 +
40953 +DWC_CIRCLEQ_HEAD(dwc_otg_qtd_list, dwc_otg_qtd);
40954 +
40955 +/**
40956 + * A Queue Head (QH) holds the static characteristics of an endpoint and
40957 + * maintains a list of transfers (QTDs) for that endpoint. A QH structure may
40958 + * be entered in either the non-periodic or periodic schedule.
40959 + */
40960 +typedef struct dwc_otg_qh {
40961 + /**
40962 + * Endpoint type.
40963 + * One of the following values:
40964 + * - UE_CONTROL
40965 + * - UE_BULK
40966 + * - UE_INTERRUPT
40967 + * - UE_ISOCHRONOUS
40968 + */
40969 + uint8_t ep_type;
40970 + uint8_t ep_is_in;
40971 +
40972 + /** wMaxPacketSize Field of Endpoint Descriptor. */
40973 + uint16_t maxp;
40974 +
40975 + /**
40976 + * Device speed.
40977 + * One of the following values:
40978 + * - DWC_OTG_EP_SPEED_LOW
40979 + * - DWC_OTG_EP_SPEED_FULL
40980 + * - DWC_OTG_EP_SPEED_HIGH
40981 + */
40982 + uint8_t dev_speed;
40983 +
40984 + /**
40985 + * Determines the PID of the next data packet for non-control
40986 + * transfers. Ignored for control transfers.<br>
40987 + * One of the following values:
40988 + * - DWC_OTG_HC_PID_DATA0
40989 + * - DWC_OTG_HC_PID_DATA1
40990 + */
40991 + uint8_t data_toggle;
40992 +
40993 + /** Ping state if 1. */
40994 + uint8_t ping_state;
40995 +
40996 + /**
40997 + * List of QTDs for this QH.
40998 + */
40999 + struct dwc_otg_qtd_list qtd_list;
41000 +
41001 + /** Host channel currently processing transfers for this QH. */
41002 + struct dwc_hc *channel;
41003 +
41004 + /** Full/low speed endpoint on high-speed hub requires split. */
41005 + uint8_t do_split;
41006 +
41007 + /** @name Periodic schedule information */
41008 + /** @{ */
41009 +
41010 + /** Bandwidth in microseconds per (micro)frame. */
41011 + uint16_t usecs;
41012 +
41013 + /** Interval between transfers in (micro)frames. */
41014 + uint16_t interval;
41015 +
41016 + /**
41017 + * (micro)frame to initialize a periodic transfer. The transfer
41018 + * executes in the following (micro)frame.
41019 + */
41020 + uint16_t sched_frame;
41021 +
41022 + /*
41023 + ** Frame a NAK was received on this queue head, used to minimise NAK retransmission
41024 + */
41025 + uint16_t nak_frame;
41026 +
41027 + /** (micro)frame at which last start split was initialized. */
41028 + uint16_t start_split_frame;
41029 +
41030 + /** @} */
41031 +
41032 + /**
41033 + * Used instead of original buffer if
41034 + * it(physical address) is not dword-aligned.
41035 + */
41036 + uint8_t *dw_align_buf;
41037 + dwc_dma_t dw_align_buf_dma;
41038 +
41039 + /** Entry for QH in either the periodic or non-periodic schedule. */
41040 + dwc_list_link_t qh_list_entry;
41041 +
41042 + /** @name Descriptor DMA support */
41043 + /** @{ */
41044 +
41045 + /** Descriptor List. */
41046 + dwc_otg_host_dma_desc_t *desc_list;
41047 +
41048 + /** Descriptor List physical address. */
41049 + dwc_dma_t desc_list_dma;
41050 +
41051 + /**
41052 + * Xfer Bytes array.
41053 + * Each element corresponds to a descriptor and indicates
41054 + * original XferSize size value for the descriptor.
41055 + */
41056 + uint32_t *n_bytes;
41057 +
41058 + /** Actual number of transfer descriptors in a list. */
41059 + uint16_t ntd;
41060 +
41061 + /** First activated isochronous transfer descriptor index. */
41062 + uint8_t td_first;
41063 + /** Last activated isochronous transfer descriptor index. */
41064 + uint8_t td_last;
41065 +
41066 + /** @} */
41067 +
41068 +
41069 + uint16_t speed;
41070 + uint16_t frame_usecs[8];
41071 +
41072 + uint32_t skip_count;
41073 +} dwc_otg_qh_t;
41074 +
41075 +DWC_CIRCLEQ_HEAD(hc_list, dwc_hc);
41076 +
41077 +typedef struct urb_tq_entry {
41078 + struct urb *urb;
41079 + DWC_TAILQ_ENTRY(urb_tq_entry) urb_tq_entries;
41080 +} urb_tq_entry_t;
41081 +
41082 +DWC_TAILQ_HEAD(urb_list, urb_tq_entry);
41083 +
41084 +/**
41085 + * This structure holds the state of the HCD, including the non-periodic and
41086 + * periodic schedules.
41087 + */
41088 +struct dwc_otg_hcd {
41089 + /** The DWC otg device pointer */
41090 + struct dwc_otg_device *otg_dev;
41091 + /** DWC OTG Core Interface Layer */
41092 + dwc_otg_core_if_t *core_if;
41093 +
41094 + /** Function HCD driver callbacks */
41095 + struct dwc_otg_hcd_function_ops *fops;
41096 +
41097 + /** Internal DWC HCD Flags */
41098 + volatile union dwc_otg_hcd_internal_flags {
41099 + uint32_t d32;
41100 + struct {
41101 + unsigned port_connect_status_change:1;
41102 + unsigned port_connect_status:1;
41103 + unsigned port_reset_change:1;
41104 + unsigned port_enable_change:1;
41105 + unsigned port_suspend_change:1;
41106 + unsigned port_over_current_change:1;
41107 + unsigned port_l1_change:1;
41108 + unsigned port_speed:2;
41109 + unsigned reserved:24;
41110 + } b;
41111 + } flags;
41112 +
41113 + /**
41114 + * Inactive items in the non-periodic schedule. This is a list of
41115 + * Queue Heads. Transfers associated with these Queue Heads are not
41116 + * currently assigned to a host channel.
41117 + */
41118 + dwc_list_link_t non_periodic_sched_inactive;
41119 +
41120 + /**
41121 + * Active items in the non-periodic schedule. This is a list of
41122 + * Queue Heads. Transfers associated with these Queue Heads are
41123 + * currently assigned to a host channel.
41124 + */
41125 + dwc_list_link_t non_periodic_sched_active;
41126 +
41127 + /**
41128 + * Pointer to the next Queue Head to process in the active
41129 + * non-periodic schedule.
41130 + */
41131 + dwc_list_link_t *non_periodic_qh_ptr;
41132 +
41133 + /**
41134 + * Inactive items in the periodic schedule. This is a list of QHs for
41135 + * periodic transfers that are _not_ scheduled for the next frame.
41136 + * Each QH in the list has an interval counter that determines when it
41137 + * needs to be scheduled for execution. This scheduling mechanism
41138 + * allows only a simple calculation for periodic bandwidth used (i.e.
41139 + * must assume that all periodic transfers may need to execute in the
41140 + * same frame). However, it greatly simplifies scheduling and should
41141 + * be sufficient for the vast majority of OTG hosts, which need to
41142 + * connect to a small number of peripherals at one time.
41143 + *
41144 + * Items move from this list to periodic_sched_ready when the QH
41145 + * interval counter is 0 at SOF.
41146 + */
41147 + dwc_list_link_t periodic_sched_inactive;
41148 +
41149 + /**
41150 + * List of periodic QHs that are ready for execution in the next
41151 + * frame, but have not yet been assigned to host channels.
41152 + *
41153 + * Items move from this list to periodic_sched_assigned as host
41154 + * channels become available during the current frame.
41155 + */
41156 + dwc_list_link_t periodic_sched_ready;
41157 +
41158 + /**
41159 + * List of periodic QHs to be executed in the next frame that are
41160 + * assigned to host channels.
41161 + *
41162 + * Items move from this list to periodic_sched_queued as the
41163 + * transactions for the QH are queued to the DWC_otg controller.
41164 + */
41165 + dwc_list_link_t periodic_sched_assigned;
41166 +
41167 + /**
41168 + * List of periodic QHs that have been queued for execution.
41169 + *
41170 + * Items move from this list to either periodic_sched_inactive or
41171 + * periodic_sched_ready when the channel associated with the transfer
41172 + * is released. If the interval for the QH is 1, the item moves to
41173 + * periodic_sched_ready because it must be rescheduled for the next
41174 + * frame. Otherwise, the item moves to periodic_sched_inactive.
41175 + */
41176 + dwc_list_link_t periodic_sched_queued;
41177 +
41178 + /**
41179 + * Total bandwidth claimed so far for periodic transfers. This value
41180 + * is in microseconds per (micro)frame. The assumption is that all
41181 + * periodic transfers may occur in the same (micro)frame.
41182 + */
41183 + uint16_t periodic_usecs;
41184 +
41185 + /**
41186 + * Total bandwidth claimed so far for all periodic transfers
41187 + * in a frame.
41188 + * This will include a mixture of HS and FS transfers.
41189 + * Units are microseconds per (micro)frame.
41190 + * We have a budget per frame and have to schedule
41191 + * transactions accordingly.
41192 + * Watch out for the fact that things are actually scheduled for the
41193 + * "next frame".
41194 + */
41195 + uint16_t frame_usecs[8];
41196 +
41197 +
41198 + /**
41199 + * Frame number read from the core at SOF. The value ranges from 0 to
41200 + * DWC_HFNUM_MAX_FRNUM.
41201 + */
41202 + uint16_t frame_number;
41203 +
41204 + /**
41205 + * Count of periodic QHs, if using several eps. For SOF enable/disable.
41206 + */
41207 + uint16_t periodic_qh_count;
41208 +
41209 + /**
41210 + * Free host channels in the controller. This is a list of
41211 + * dwc_hc_t items.
41212 + */
41213 + struct hc_list free_hc_list;
41214 + /**
41215 + * Number of host channels assigned to periodic transfers. Currently
41216 + * assuming that there is a dedicated host channel for each periodic
41217 + * transaction and at least one host channel available for
41218 + * non-periodic transactions.
41219 + */
41220 + int periodic_channels; /* microframe_schedule==0 */
41221 +
41222 + /**
41223 + * Number of host channels assigned to non-periodic transfers.
41224 + */
41225 + int non_periodic_channels; /* microframe_schedule==0 */
41226 +
41227 + /**
41228 + * Number of host channels assigned to non-periodic transfers.
41229 + */
41230 + int available_host_channels;
41231 +
41232 + /**
41233 + * Array of pointers to the host channel descriptors. Allows accessing
41234 + * a host channel descriptor given the host channel number. This is
41235 + * useful in interrupt handlers.
41236 + */
41237 + struct dwc_hc *hc_ptr_array[MAX_EPS_CHANNELS];
41238 +
41239 + /**
41240 + * Buffer to use for any data received during the status phase of a
41241 + * control transfer. Normally no data is transferred during the status
41242 + * phase. This buffer is used as a bit bucket.
41243 + */
41244 + uint8_t *status_buf;
41245 +
41246 + /**
41247 + * DMA address for status_buf.
41248 + */
41249 + dma_addr_t status_buf_dma;
41250 +#define DWC_OTG_HCD_STATUS_BUF_SIZE 64
41251 +
41252 + /**
41253 + * Connection timer. An OTG host must display a message if the device
41254 + * does not connect. Started when the VBus power is turned on via
41255 + * sysfs attribute "buspower".
41256 + */
41257 + dwc_timer_t *conn_timer;
41258 +
41259 + /* Tasket to do a reset */
41260 + dwc_tasklet_t *reset_tasklet;
41261 +
41262 + dwc_tasklet_t *completion_tasklet;
41263 + struct urb_list completed_urb_list;
41264 +
41265 + /* */
41266 + dwc_spinlock_t *lock;
41267 + /**
41268 + * Private data that could be used by OS wrapper.
41269 + */
41270 + void *priv;
41271 +
41272 + uint8_t otg_port;
41273 +
41274 + /** Frame List */
41275 + uint32_t *frame_list;
41276 +
41277 + /** Hub - Port assignment */
41278 + int hub_port[128];
41279 +#ifdef FIQ_DEBUG
41280 + int hub_port_alloc[2048];
41281 +#endif
41282 +
41283 + /** Frame List DMA address */
41284 + dma_addr_t frame_list_dma;
41285 +
41286 + struct fiq_stack *fiq_stack;
41287 + struct fiq_state *fiq_state;
41288 +
41289 + /** Virtual address for split transaction DMA bounce buffers */
41290 + struct fiq_dma_blob *fiq_dmab;
41291 +
41292 +#ifdef DEBUG
41293 + uint32_t frrem_samples;
41294 + uint64_t frrem_accum;
41295 +
41296 + uint32_t hfnum_7_samples_a;
41297 + uint64_t hfnum_7_frrem_accum_a;
41298 + uint32_t hfnum_0_samples_a;
41299 + uint64_t hfnum_0_frrem_accum_a;
41300 + uint32_t hfnum_other_samples_a;
41301 + uint64_t hfnum_other_frrem_accum_a;
41302 +
41303 + uint32_t hfnum_7_samples_b;
41304 + uint64_t hfnum_7_frrem_accum_b;
41305 + uint32_t hfnum_0_samples_b;
41306 + uint64_t hfnum_0_frrem_accum_b;
41307 + uint32_t hfnum_other_samples_b;
41308 + uint64_t hfnum_other_frrem_accum_b;
41309 +#endif
41310 +};
41311 +
41312 +static inline struct device *dwc_otg_hcd_to_dev(struct dwc_otg_hcd *hcd)
41313 +{
41314 + return &hcd->otg_dev->os_dep.platformdev->dev;
41315 +}
41316 +
41317 +/** @name Transaction Execution Functions */
41318 +/** @{ */
41319 +extern dwc_otg_transaction_type_e dwc_otg_hcd_select_transactions(dwc_otg_hcd_t
41320 + * hcd);
41321 +extern void dwc_otg_hcd_queue_transactions(dwc_otg_hcd_t * hcd,
41322 + dwc_otg_transaction_type_e tr_type);
41323 +
41324 +int dwc_otg_hcd_allocate_port(dwc_otg_hcd_t * hcd, dwc_otg_qh_t *qh);
41325 +void dwc_otg_hcd_release_port(dwc_otg_hcd_t * dwc_otg_hcd, dwc_otg_qh_t *qh);
41326 +
41327 +extern int fiq_fsm_queue_transaction(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh);
41328 +extern int fiq_fsm_transaction_suitable(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh);
41329 +extern void dwc_otg_cleanup_fiq_channel(dwc_otg_hcd_t *hcd, uint32_t num);
41330 +
41331 +/** @} */
41332 +
41333 +/** @name Interrupt Handler Functions */
41334 +/** @{ */
41335 +extern int32_t dwc_otg_hcd_handle_intr(dwc_otg_hcd_t * dwc_otg_hcd);
41336 +extern int32_t dwc_otg_hcd_handle_sof_intr(dwc_otg_hcd_t * dwc_otg_hcd);
41337 +extern int32_t dwc_otg_hcd_handle_rx_status_q_level_intr(dwc_otg_hcd_t *
41338 + dwc_otg_hcd);
41339 +extern int32_t dwc_otg_hcd_handle_np_tx_fifo_empty_intr(dwc_otg_hcd_t *
41340 + dwc_otg_hcd);
41341 +extern int32_t dwc_otg_hcd_handle_perio_tx_fifo_empty_intr(dwc_otg_hcd_t *
41342 + dwc_otg_hcd);
41343 +extern int32_t dwc_otg_hcd_handle_incomplete_periodic_intr(dwc_otg_hcd_t *
41344 + dwc_otg_hcd);
41345 +extern int32_t dwc_otg_hcd_handle_port_intr(dwc_otg_hcd_t * dwc_otg_hcd);
41346 +extern int32_t dwc_otg_hcd_handle_conn_id_status_change_intr(dwc_otg_hcd_t *
41347 + dwc_otg_hcd);
41348 +extern int32_t dwc_otg_hcd_handle_disconnect_intr(dwc_otg_hcd_t * dwc_otg_hcd);
41349 +extern int32_t dwc_otg_hcd_handle_hc_intr(dwc_otg_hcd_t * dwc_otg_hcd);
41350 +extern int32_t dwc_otg_hcd_handle_hc_n_intr(dwc_otg_hcd_t * dwc_otg_hcd,
41351 + uint32_t num);
41352 +extern int32_t dwc_otg_hcd_handle_session_req_intr(dwc_otg_hcd_t * dwc_otg_hcd);
41353 +extern int32_t dwc_otg_hcd_handle_wakeup_detected_intr(dwc_otg_hcd_t *
41354 + dwc_otg_hcd);
41355 +/** @} */
41356 +
41357 +/** @name Schedule Queue Functions */
41358 +/** @{ */
41359 +
41360 +/* Implemented in dwc_otg_hcd_queue.c */
41361 +extern dwc_otg_qh_t *dwc_otg_hcd_qh_create(dwc_otg_hcd_t * hcd,
41362 + dwc_otg_hcd_urb_t * urb, int atomic_alloc);
41363 +extern void dwc_otg_hcd_qh_free(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
41364 +extern int dwc_otg_hcd_qh_add(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
41365 +extern void dwc_otg_hcd_qh_remove(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
41366 +extern void dwc_otg_hcd_qh_deactivate(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh,
41367 + int sched_csplit);
41368 +
41369 +/** Remove and free a QH */
41370 +static inline void dwc_otg_hcd_qh_remove_and_free(dwc_otg_hcd_t * hcd,
41371 + dwc_otg_qh_t * qh)
41372 +{
41373 + dwc_irqflags_t flags;
41374 + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
41375 + dwc_otg_hcd_qh_remove(hcd, qh);
41376 + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
41377 + dwc_otg_hcd_qh_free(hcd, qh);
41378 +}
41379 +
41380 +/** Allocates memory for a QH structure.
41381 + * @return Returns the memory allocate or NULL on error. */
41382 +static inline dwc_otg_qh_t *dwc_otg_hcd_qh_alloc(int atomic_alloc)
41383 +{
41384 + if (atomic_alloc)
41385 + return (dwc_otg_qh_t *) DWC_ALLOC_ATOMIC(sizeof(dwc_otg_qh_t));
41386 + else
41387 + return (dwc_otg_qh_t *) DWC_ALLOC(sizeof(dwc_otg_qh_t));
41388 +}
41389 +
41390 +extern dwc_otg_qtd_t *dwc_otg_hcd_qtd_create(dwc_otg_hcd_urb_t * urb,
41391 + int atomic_alloc);
41392 +extern void dwc_otg_hcd_qtd_init(dwc_otg_qtd_t * qtd, dwc_otg_hcd_urb_t * urb);
41393 +extern int dwc_otg_hcd_qtd_add(dwc_otg_qtd_t * qtd, dwc_otg_hcd_t * dwc_otg_hcd,
41394 + dwc_otg_qh_t ** qh, int atomic_alloc);
41395 +
41396 +/** Allocates memory for a QTD structure.
41397 + * @return Returns the memory allocate or NULL on error. */
41398 +static inline dwc_otg_qtd_t *dwc_otg_hcd_qtd_alloc(int atomic_alloc)
41399 +{
41400 + if (atomic_alloc)
41401 + return (dwc_otg_qtd_t *) DWC_ALLOC_ATOMIC(sizeof(dwc_otg_qtd_t));
41402 + else
41403 + return (dwc_otg_qtd_t *) DWC_ALLOC(sizeof(dwc_otg_qtd_t));
41404 +}
41405 +
41406 +/** Frees the memory for a QTD structure. QTD should already be removed from
41407 + * list.
41408 + * @param qtd QTD to free.*/
41409 +static inline void dwc_otg_hcd_qtd_free(dwc_otg_qtd_t * qtd)
41410 +{
41411 + DWC_FREE(qtd);
41412 +}
41413 +
41414 +/** Removes a QTD from list.
41415 + * @param hcd HCD instance.
41416 + * @param qtd QTD to remove from list.
41417 + * @param qh QTD belongs to.
41418 + */
41419 +static inline void dwc_otg_hcd_qtd_remove(dwc_otg_hcd_t * hcd,
41420 + dwc_otg_qtd_t * qtd,
41421 + dwc_otg_qh_t * qh)
41422 +{
41423 + DWC_CIRCLEQ_REMOVE(&qh->qtd_list, qtd, qtd_list_entry);
41424 +}
41425 +
41426 +/** Remove and free a QTD
41427 + * Need to disable IRQ and hold hcd lock while calling this function out of
41428 + * interrupt servicing chain */
41429 +static inline void dwc_otg_hcd_qtd_remove_and_free(dwc_otg_hcd_t * hcd,
41430 + dwc_otg_qtd_t * qtd,
41431 + dwc_otg_qh_t * qh)
41432 +{
41433 + dwc_otg_hcd_qtd_remove(hcd, qtd, qh);
41434 + dwc_otg_hcd_qtd_free(qtd);
41435 +}
41436 +
41437 +/** @} */
41438 +
41439 +/** @name Descriptor DMA Supporting Functions */
41440 +/** @{ */
41441 +
41442 +extern void dwc_otg_hcd_start_xfer_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
41443 +extern void dwc_otg_hcd_complete_xfer_ddma(dwc_otg_hcd_t * hcd,
41444 + dwc_hc_t * hc,
41445 + dwc_otg_hc_regs_t * hc_regs,
41446 + dwc_otg_halt_status_e halt_status);
41447 +
41448 +extern int dwc_otg_hcd_qh_init_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
41449 +extern void dwc_otg_hcd_qh_free_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
41450 +
41451 +/** @} */
41452 +
41453 +/** @name Internal Functions */
41454 +/** @{ */
41455 +dwc_otg_qh_t *dwc_urb_to_qh(dwc_otg_hcd_urb_t * urb);
41456 +/** @} */
41457 +
41458 +#ifdef CONFIG_USB_DWC_OTG_LPM
41459 +extern int dwc_otg_hcd_get_hc_for_lpm_tran(dwc_otg_hcd_t * hcd,
41460 + uint8_t devaddr);
41461 +extern void dwc_otg_hcd_free_hc_from_lpm(dwc_otg_hcd_t * hcd);
41462 +#endif
41463 +
41464 +/** Gets the QH that contains the list_head */
41465 +#define dwc_list_to_qh(_list_head_ptr_) container_of(_list_head_ptr_, dwc_otg_qh_t, qh_list_entry)
41466 +
41467 +/** Gets the QTD that contains the list_head */
41468 +#define dwc_list_to_qtd(_list_head_ptr_) container_of(_list_head_ptr_, dwc_otg_qtd_t, qtd_list_entry)
41469 +
41470 +/** Check if QH is non-periodic */
41471 +#define dwc_qh_is_non_per(_qh_ptr_) ((_qh_ptr_->ep_type == UE_BULK) || \
41472 + (_qh_ptr_->ep_type == UE_CONTROL))
41473 +
41474 +/** High bandwidth multiplier as encoded in highspeed endpoint descriptors */
41475 +#define dwc_hb_mult(wMaxPacketSize) (1 + (((wMaxPacketSize) >> 11) & 0x03))
41476 +
41477 +/** Packet size for any kind of endpoint descriptor */
41478 +#define dwc_max_packet(wMaxPacketSize) ((wMaxPacketSize) & 0x07ff)
41479 +
41480 +/**
41481 + * Returns true if _frame1 is less than or equal to _frame2. The comparison is
41482 + * done modulo DWC_HFNUM_MAX_FRNUM. This accounts for the rollover of the
41483 + * frame number when the max frame number is reached.
41484 + */
41485 +static inline int dwc_frame_num_le(uint16_t frame1, uint16_t frame2)
41486 +{
41487 + return ((frame2 - frame1) & DWC_HFNUM_MAX_FRNUM) <=
41488 + (DWC_HFNUM_MAX_FRNUM >> 1);
41489 +}
41490 +
41491 +/**
41492 + * Returns true if _frame1 is greater than _frame2. The comparison is done
41493 + * modulo DWC_HFNUM_MAX_FRNUM. This accounts for the rollover of the frame
41494 + * number when the max frame number is reached.
41495 + */
41496 +static inline int dwc_frame_num_gt(uint16_t frame1, uint16_t frame2)
41497 +{
41498 + return (frame1 != frame2) &&
41499 + (((frame1 - frame2) & DWC_HFNUM_MAX_FRNUM) <
41500 + (DWC_HFNUM_MAX_FRNUM >> 1));
41501 +}
41502 +
41503 +/**
41504 + * Increments _frame by the amount specified by _inc. The addition is done
41505 + * modulo DWC_HFNUM_MAX_FRNUM. Returns the incremented value.
41506 + */
41507 +static inline uint16_t dwc_frame_num_inc(uint16_t frame, uint16_t inc)
41508 +{
41509 + return (frame + inc) & DWC_HFNUM_MAX_FRNUM;
41510 +}
41511 +
41512 +static inline uint16_t dwc_full_frame_num(uint16_t frame)
41513 +{
41514 + return (frame & DWC_HFNUM_MAX_FRNUM) >> 3;
41515 +}
41516 +
41517 +static inline uint16_t dwc_micro_frame_num(uint16_t frame)
41518 +{
41519 + return frame & 0x7;
41520 +}
41521 +
41522 +extern void init_hcd_usecs(dwc_otg_hcd_t *_hcd);
41523 +
41524 +void dwc_otg_hcd_save_data_toggle(dwc_hc_t * hc,
41525 + dwc_otg_hc_regs_t * hc_regs,
41526 + dwc_otg_qtd_t * qtd);
41527 +
41528 +#ifdef DEBUG
41529 +/**
41530 + * Macro to sample the remaining PHY clocks left in the current frame. This
41531 + * may be used during debugging to determine the average time it takes to
41532 + * execute sections of code. There are two possible sample points, "a" and
41533 + * "b", so the _letter argument must be one of these values.
41534 + *
41535 + * To dump the average sample times, read the "hcd_frrem" sysfs attribute. For
41536 + * example, "cat /sys/devices/lm0/hcd_frrem".
41537 + */
41538 +#define dwc_sample_frrem(_hcd, _qh, _letter) \
41539 +{ \
41540 + hfnum_data_t hfnum; \
41541 + dwc_otg_qtd_t *qtd; \
41542 + qtd = list_entry(_qh->qtd_list.next, dwc_otg_qtd_t, qtd_list_entry); \
41543 + if (usb_pipeint(qtd->urb->pipe) && _qh->start_split_frame != 0 && !qtd->complete_split) { \
41544 + hfnum.d32 = DWC_READ_REG32(&_hcd->core_if->host_if->host_global_regs->hfnum); \
41545 + switch (hfnum.b.frnum & 0x7) { \
41546 + case 7: \
41547 + _hcd->hfnum_7_samples_##_letter++; \
41548 + _hcd->hfnum_7_frrem_accum_##_letter += hfnum.b.frrem; \
41549 + break; \
41550 + case 0: \
41551 + _hcd->hfnum_0_samples_##_letter++; \
41552 + _hcd->hfnum_0_frrem_accum_##_letter += hfnum.b.frrem; \
41553 + break; \
41554 + default: \
41555 + _hcd->hfnum_other_samples_##_letter++; \
41556 + _hcd->hfnum_other_frrem_accum_##_letter += hfnum.b.frrem; \
41557 + break; \
41558 + } \
41559 + } \
41560 +}
41561 +#else
41562 +#define dwc_sample_frrem(_hcd, _qh, _letter)
41563 +#endif
41564 +#endif
41565 +#endif /* DWC_DEVICE_ONLY */
41566 --- /dev/null
41567 +++ b/drivers/usb/host/dwc_otg/dwc_otg_hcd_ddma.c
41568 @@ -0,0 +1,1134 @@
41569 +/*==========================================================================
41570 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd_ddma.c $
41571 + * $Revision: #10 $
41572 + * $Date: 2011/10/20 $
41573 + * $Change: 1869464 $
41574 + *
41575 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
41576 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
41577 + * otherwise expressly agreed to in writing between Synopsys and you.
41578 + *
41579 + * The Software IS NOT an item of Licensed Software or Licensed Product under
41580 + * any End User Software License Agreement or Agreement for Licensed Product
41581 + * with Synopsys or any supplement thereto. You are permitted to use and
41582 + * redistribute this Software in source and binary forms, with or without
41583 + * modification, provided that redistributions of source code must retain this
41584 + * notice. You may not view, use, disclose, copy or distribute this file or
41585 + * any information contained herein except pursuant to this license grant from
41586 + * Synopsys. If you do not agree with this notice, including the disclaimer
41587 + * below, then you are not authorized to use the Software.
41588 + *
41589 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
41590 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41591 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
41592 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
41593 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
41594 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
41595 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
41596 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
41597 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41598 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
41599 + * DAMAGE.
41600 + * ========================================================================== */
41601 +#ifndef DWC_DEVICE_ONLY
41602 +
41603 +/** @file
41604 + * This file contains Descriptor DMA support implementation for host mode.
41605 + */
41606 +
41607 +#include "dwc_otg_hcd.h"
41608 +#include "dwc_otg_regs.h"
41609 +
41610 +extern bool microframe_schedule;
41611 +
41612 +static inline uint8_t frame_list_idx(uint16_t frame)
41613 +{
41614 + return (frame & (MAX_FRLIST_EN_NUM - 1));
41615 +}
41616 +
41617 +static inline uint16_t desclist_idx_inc(uint16_t idx, uint16_t inc, uint8_t speed)
41618 +{
41619 + return (idx + inc) &
41620 + (((speed ==
41621 + DWC_OTG_EP_SPEED_HIGH) ? MAX_DMA_DESC_NUM_HS_ISOC :
41622 + MAX_DMA_DESC_NUM_GENERIC) - 1);
41623 +}
41624 +
41625 +static inline uint16_t desclist_idx_dec(uint16_t idx, uint16_t inc, uint8_t speed)
41626 +{
41627 + return (idx - inc) &
41628 + (((speed ==
41629 + DWC_OTG_EP_SPEED_HIGH) ? MAX_DMA_DESC_NUM_HS_ISOC :
41630 + MAX_DMA_DESC_NUM_GENERIC) - 1);
41631 +}
41632 +
41633 +static inline uint16_t max_desc_num(dwc_otg_qh_t * qh)
41634 +{
41635 + return (((qh->ep_type == UE_ISOCHRONOUS)
41636 + && (qh->dev_speed == DWC_OTG_EP_SPEED_HIGH))
41637 + ? MAX_DMA_DESC_NUM_HS_ISOC : MAX_DMA_DESC_NUM_GENERIC);
41638 +}
41639 +static inline uint16_t frame_incr_val(dwc_otg_qh_t * qh)
41640 +{
41641 + return ((qh->dev_speed == DWC_OTG_EP_SPEED_HIGH)
41642 + ? ((qh->interval + 8 - 1) / 8)
41643 + : qh->interval);
41644 +}
41645 +
41646 +static int desc_list_alloc(struct device *dev, dwc_otg_qh_t * qh)
41647 +{
41648 + int retval = 0;
41649 +
41650 + qh->desc_list = (dwc_otg_host_dma_desc_t *)
41651 + DWC_DMA_ALLOC(dev, sizeof(dwc_otg_host_dma_desc_t) * max_desc_num(qh),
41652 + &qh->desc_list_dma);
41653 +
41654 + if (!qh->desc_list) {
41655 + retval = -DWC_E_NO_MEMORY;
41656 + DWC_ERROR("%s: DMA descriptor list allocation failed\n", __func__);
41657 +
41658 + }
41659 +
41660 + dwc_memset(qh->desc_list, 0x00,
41661 + sizeof(dwc_otg_host_dma_desc_t) * max_desc_num(qh));
41662 +
41663 + qh->n_bytes =
41664 + (uint32_t *) DWC_ALLOC(sizeof(uint32_t) * max_desc_num(qh));
41665 +
41666 + if (!qh->n_bytes) {
41667 + retval = -DWC_E_NO_MEMORY;
41668 + DWC_ERROR
41669 + ("%s: Failed to allocate array for descriptors' size actual values\n",
41670 + __func__);
41671 +
41672 + }
41673 + return retval;
41674 +
41675 +}
41676 +
41677 +static void desc_list_free(struct device *dev, dwc_otg_qh_t * qh)
41678 +{
41679 + if (qh->desc_list) {
41680 + DWC_DMA_FREE(dev, max_desc_num(qh), qh->desc_list,
41681 + qh->desc_list_dma);
41682 + qh->desc_list = NULL;
41683 + }
41684 +
41685 + if (qh->n_bytes) {
41686 + DWC_FREE(qh->n_bytes);
41687 + qh->n_bytes = NULL;
41688 + }
41689 +}
41690 +
41691 +static int frame_list_alloc(dwc_otg_hcd_t * hcd)
41692 +{
41693 + struct device *dev = dwc_otg_hcd_to_dev(hcd);
41694 + int retval = 0;
41695 +
41696 + if (hcd->frame_list)
41697 + return 0;
41698 +
41699 + hcd->frame_list = DWC_DMA_ALLOC(dev, 4 * MAX_FRLIST_EN_NUM,
41700 + &hcd->frame_list_dma);
41701 + if (!hcd->frame_list) {
41702 + retval = -DWC_E_NO_MEMORY;
41703 + DWC_ERROR("%s: Frame List allocation failed\n", __func__);
41704 + }
41705 +
41706 + dwc_memset(hcd->frame_list, 0x00, 4 * MAX_FRLIST_EN_NUM);
41707 +
41708 + return retval;
41709 +}
41710 +
41711 +static void frame_list_free(dwc_otg_hcd_t * hcd)
41712 +{
41713 + struct device *dev = dwc_otg_hcd_to_dev(hcd);
41714 +
41715 + if (!hcd->frame_list)
41716 + return;
41717 +
41718 + DWC_DMA_FREE(dev, 4 * MAX_FRLIST_EN_NUM, hcd->frame_list, hcd->frame_list_dma);
41719 + hcd->frame_list = NULL;
41720 +}
41721 +
41722 +static void per_sched_enable(dwc_otg_hcd_t * hcd, uint16_t fr_list_en)
41723 +{
41724 +
41725 + hcfg_data_t hcfg;
41726 +
41727 + hcfg.d32 = DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hcfg);
41728 +
41729 + if (hcfg.b.perschedena) {
41730 + /* already enabled */
41731 + return;
41732 + }
41733 +
41734 + DWC_WRITE_REG32(&hcd->core_if->host_if->host_global_regs->hflbaddr,
41735 + hcd->frame_list_dma);
41736 +
41737 + switch (fr_list_en) {
41738 + case 64:
41739 + hcfg.b.frlisten = 3;
41740 + break;
41741 + case 32:
41742 + hcfg.b.frlisten = 2;
41743 + break;
41744 + case 16:
41745 + hcfg.b.frlisten = 1;
41746 + break;
41747 + case 8:
41748 + hcfg.b.frlisten = 0;
41749 + break;
41750 + default:
41751 + break;
41752 + }
41753 +
41754 + hcfg.b.perschedena = 1;
41755 +
41756 + DWC_DEBUGPL(DBG_HCD, "Enabling Periodic schedule\n");
41757 + DWC_WRITE_REG32(&hcd->core_if->host_if->host_global_regs->hcfg, hcfg.d32);
41758 +
41759 +}
41760 +
41761 +static void per_sched_disable(dwc_otg_hcd_t * hcd)
41762 +{
41763 + hcfg_data_t hcfg;
41764 +
41765 + hcfg.d32 = DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hcfg);
41766 +
41767 + if (!hcfg.b.perschedena) {
41768 + /* already disabled */
41769 + return;
41770 + }
41771 + hcfg.b.perschedena = 0;
41772 +
41773 + DWC_DEBUGPL(DBG_HCD, "Disabling Periodic schedule\n");
41774 + DWC_WRITE_REG32(&hcd->core_if->host_if->host_global_regs->hcfg, hcfg.d32);
41775 +}
41776 +
41777 +/*
41778 + * Activates/Deactivates FrameList entries for the channel
41779 + * based on endpoint servicing period.
41780 + */
41781 +void update_frame_list(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh, uint8_t enable)
41782 +{
41783 + uint16_t i, j, inc;
41784 + dwc_hc_t *hc = NULL;
41785 +
41786 + if (!qh->channel) {
41787 + DWC_ERROR("qh->channel = %p", qh->channel);
41788 + return;
41789 + }
41790 +
41791 + if (!hcd) {
41792 + DWC_ERROR("------hcd = %p", hcd);
41793 + return;
41794 + }
41795 +
41796 + if (!hcd->frame_list) {
41797 + DWC_ERROR("-------hcd->frame_list = %p", hcd->frame_list);
41798 + return;
41799 + }
41800 +
41801 + hc = qh->channel;
41802 + inc = frame_incr_val(qh);
41803 + if (qh->ep_type == UE_ISOCHRONOUS)
41804 + i = frame_list_idx(qh->sched_frame);
41805 + else
41806 + i = 0;
41807 +
41808 + j = i;
41809 + do {
41810 + if (enable)
41811 + hcd->frame_list[j] |= (1 << hc->hc_num);
41812 + else
41813 + hcd->frame_list[j] &= ~(1 << hc->hc_num);
41814 + j = (j + inc) & (MAX_FRLIST_EN_NUM - 1);
41815 + }
41816 + while (j != i);
41817 + if (!enable)
41818 + return;
41819 + hc->schinfo = 0;
41820 + if (qh->channel->speed == DWC_OTG_EP_SPEED_HIGH) {
41821 + j = 1;
41822 + /* TODO - check this */
41823 + inc = (8 + qh->interval - 1) / qh->interval;
41824 + for (i = 0; i < inc; i++) {
41825 + hc->schinfo |= j;
41826 + j = j << qh->interval;
41827 + }
41828 + } else {
41829 + hc->schinfo = 0xff;
41830 + }
41831 +}
41832 +
41833 +#if 1
41834 +void dump_frame_list(dwc_otg_hcd_t * hcd)
41835 +{
41836 + int i = 0;
41837 + DWC_PRINTF("--FRAME LIST (hex) --\n");
41838 + for (i = 0; i < MAX_FRLIST_EN_NUM; i++) {
41839 + DWC_PRINTF("%x\t", hcd->frame_list[i]);
41840 + if (!(i % 8) && i)
41841 + DWC_PRINTF("\n");
41842 + }
41843 + DWC_PRINTF("\n----\n");
41844 +
41845 +}
41846 +#endif
41847 +
41848 +static void release_channel_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
41849 +{
41850 + dwc_hc_t *hc = qh->channel;
41851 + if (dwc_qh_is_non_per(qh)) {
41852 + if (!microframe_schedule)
41853 + hcd->non_periodic_channels--;
41854 + else
41855 + hcd->available_host_channels++;
41856 + } else
41857 + update_frame_list(hcd, qh, 0);
41858 +
41859 + /*
41860 + * The condition is added to prevent double cleanup try in case of device
41861 + * disconnect. See channel cleanup in dwc_otg_hcd_disconnect_cb().
41862 + */
41863 + if (hc->qh) {
41864 + dwc_otg_hc_cleanup(hcd->core_if, hc);
41865 + DWC_CIRCLEQ_INSERT_TAIL(&hcd->free_hc_list, hc, hc_list_entry);
41866 + hc->qh = NULL;
41867 + }
41868 +
41869 + qh->channel = NULL;
41870 + qh->ntd = 0;
41871 +
41872 + if (qh->desc_list) {
41873 + dwc_memset(qh->desc_list, 0x00,
41874 + sizeof(dwc_otg_host_dma_desc_t) * max_desc_num(qh));
41875 + }
41876 +}
41877 +
41878 +/**
41879 + * Initializes a QH structure's Descriptor DMA related members.
41880 + * Allocates memory for descriptor list.
41881 + * On first periodic QH, allocates memory for FrameList
41882 + * and enables periodic scheduling.
41883 + *
41884 + * @param hcd The HCD state structure for the DWC OTG controller.
41885 + * @param qh The QH to init.
41886 + *
41887 + * @return 0 if successful, negative error code otherwise.
41888 + */
41889 +int dwc_otg_hcd_qh_init_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
41890 +{
41891 + struct device *dev = dwc_otg_hcd_to_dev(hcd);
41892 + int retval = 0;
41893 +
41894 + if (qh->do_split) {
41895 + DWC_ERROR("SPLIT Transfers are not supported in Descriptor DMA.\n");
41896 + return -1;
41897 + }
41898 +
41899 + retval = desc_list_alloc(dev, qh);
41900 +
41901 + if ((retval == 0)
41902 + && (qh->ep_type == UE_ISOCHRONOUS || qh->ep_type == UE_INTERRUPT)) {
41903 + if (!hcd->frame_list) {
41904 + retval = frame_list_alloc(hcd);
41905 + /* Enable periodic schedule on first periodic QH */
41906 + if (retval == 0)
41907 + per_sched_enable(hcd, MAX_FRLIST_EN_NUM);
41908 + }
41909 + }
41910 +
41911 + qh->ntd = 0;
41912 +
41913 + return retval;
41914 +}
41915 +
41916 +/**
41917 + * Frees descriptor list memory associated with the QH.
41918 + * If QH is periodic and the last, frees FrameList memory
41919 + * and disables periodic scheduling.
41920 + *
41921 + * @param hcd The HCD state structure for the DWC OTG controller.
41922 + * @param qh The QH to init.
41923 + */
41924 +void dwc_otg_hcd_qh_free_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
41925 +{
41926 + struct device *dev = dwc_otg_hcd_to_dev(hcd);
41927 +
41928 + desc_list_free(dev, qh);
41929 +
41930 + /*
41931 + * Channel still assigned due to some reasons.
41932 + * Seen on Isoc URB dequeue. Channel halted but no subsequent
41933 + * ChHalted interrupt to release the channel. Afterwards
41934 + * when it comes here from endpoint disable routine
41935 + * channel remains assigned.
41936 + */
41937 + if (qh->channel)
41938 + release_channel_ddma(hcd, qh);
41939 +
41940 + if ((qh->ep_type == UE_ISOCHRONOUS || qh->ep_type == UE_INTERRUPT)
41941 + && (microframe_schedule || !hcd->periodic_channels) && hcd->frame_list) {
41942 +
41943 + per_sched_disable(hcd);
41944 + frame_list_free(hcd);
41945 + }
41946 +}
41947 +
41948 +static uint8_t frame_to_desc_idx(dwc_otg_qh_t * qh, uint16_t frame_idx)
41949 +{
41950 + if (qh->dev_speed == DWC_OTG_EP_SPEED_HIGH) {
41951 + /*
41952 + * Descriptor set(8 descriptors) index
41953 + * which is 8-aligned.
41954 + */
41955 + return (frame_idx & ((MAX_DMA_DESC_NUM_HS_ISOC / 8) - 1)) * 8;
41956 + } else {
41957 + return (frame_idx & (MAX_DMA_DESC_NUM_GENERIC - 1));
41958 + }
41959 +}
41960 +
41961 +/*
41962 + * Determine starting frame for Isochronous transfer.
41963 + * Few frames skipped to prevent race condition with HC.
41964 + */
41965 +static uint8_t calc_starting_frame(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh,
41966 + uint8_t * skip_frames)
41967 +{
41968 + uint16_t frame = 0;
41969 + hcd->frame_number = dwc_otg_hcd_get_frame_number(hcd);
41970 +
41971 + /* sched_frame is always frame number(not uFrame) both in FS and HS !! */
41972 +
41973 + /*
41974 + * skip_frames is used to limit activated descriptors number
41975 + * to avoid the situation when HC services the last activated
41976 + * descriptor firstly.
41977 + * Example for FS:
41978 + * Current frame is 1, scheduled frame is 3. Since HC always fetches the descriptor
41979 + * corresponding to curr_frame+1, the descriptor corresponding to frame 2
41980 + * will be fetched. If the number of descriptors is max=64 (or greather) the
41981 + * list will be fully programmed with Active descriptors and it is possible
41982 + * case(rare) that the latest descriptor(considering rollback) corresponding
41983 + * to frame 2 will be serviced first. HS case is more probable because, in fact,
41984 + * up to 11 uframes(16 in the code) may be skipped.
41985 + */
41986 + if (qh->dev_speed == DWC_OTG_EP_SPEED_HIGH) {
41987 + /*
41988 + * Consider uframe counter also, to start xfer asap.
41989 + * If half of the frame elapsed skip 2 frames otherwise
41990 + * just 1 frame.
41991 + * Starting descriptor index must be 8-aligned, so
41992 + * if the current frame is near to complete the next one
41993 + * is skipped as well.
41994 + */
41995 +
41996 + if (dwc_micro_frame_num(hcd->frame_number) >= 5) {
41997 + *skip_frames = 2 * 8;
41998 + frame = dwc_frame_num_inc(hcd->frame_number, *skip_frames);
41999 + } else {
42000 + *skip_frames = 1 * 8;
42001 + frame = dwc_frame_num_inc(hcd->frame_number, *skip_frames);
42002 + }
42003 +
42004 + frame = dwc_full_frame_num(frame);
42005 + } else {
42006 + /*
42007 + * Two frames are skipped for FS - the current and the next.
42008 + * But for descriptor programming, 1 frame(descriptor) is enough,
42009 + * see example above.
42010 + */
42011 + *skip_frames = 1;
42012 + frame = dwc_frame_num_inc(hcd->frame_number, 2);
42013 + }
42014 +
42015 + return frame;
42016 +}
42017 +
42018 +/*
42019 + * Calculate initial descriptor index for isochronous transfer
42020 + * based on scheduled frame.
42021 + */
42022 +static uint8_t recalc_initial_desc_idx(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
42023 +{
42024 + uint16_t frame = 0, fr_idx, fr_idx_tmp;
42025 + uint8_t skip_frames = 0;
42026 + /*
42027 + * With current ISOC processing algorithm the channel is being
42028 + * released when no more QTDs in the list(qh->ntd == 0).
42029 + * Thus this function is called only when qh->ntd == 0 and qh->channel == 0.
42030 + *
42031 + * So qh->channel != NULL branch is not used and just not removed from the
42032 + * source file. It is required for another possible approach which is,
42033 + * do not disable and release the channel when ISOC session completed,
42034 + * just move QH to inactive schedule until new QTD arrives.
42035 + * On new QTD, the QH moved back to 'ready' schedule,
42036 + * starting frame and therefore starting desc_index are recalculated.
42037 + * In this case channel is released only on ep_disable.
42038 + */
42039 +
42040 + /* Calculate starting descriptor index. For INTERRUPT endpoint it is always 0. */
42041 + if (qh->channel) {
42042 + frame = calc_starting_frame(hcd, qh, &skip_frames);
42043 + /*
42044 + * Calculate initial descriptor index based on FrameList current bitmap
42045 + * and servicing period.
42046 + */
42047 + fr_idx_tmp = frame_list_idx(frame);
42048 + fr_idx =
42049 + (MAX_FRLIST_EN_NUM + frame_list_idx(qh->sched_frame) -
42050 + fr_idx_tmp)
42051 + % frame_incr_val(qh);
42052 + fr_idx = (fr_idx + fr_idx_tmp) % MAX_FRLIST_EN_NUM;
42053 + } else {
42054 + qh->sched_frame = calc_starting_frame(hcd, qh, &skip_frames);
42055 + fr_idx = frame_list_idx(qh->sched_frame);
42056 + }
42057 +
42058 + qh->td_first = qh->td_last = frame_to_desc_idx(qh, fr_idx);
42059 +
42060 + return skip_frames;
42061 +}
42062 +
42063 +#define ISOC_URB_GIVEBACK_ASAP
42064 +
42065 +#define MAX_ISOC_XFER_SIZE_FS 1023
42066 +#define MAX_ISOC_XFER_SIZE_HS 3072
42067 +#define DESCNUM_THRESHOLD 4
42068 +
42069 +static void init_isoc_dma_desc(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh,
42070 + uint8_t skip_frames)
42071 +{
42072 + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
42073 + dwc_otg_qtd_t *qtd;
42074 + dwc_otg_host_dma_desc_t *dma_desc;
42075 + uint16_t idx, inc, n_desc, ntd_max, max_xfer_size;
42076 +
42077 + idx = qh->td_last;
42078 + inc = qh->interval;
42079 + n_desc = 0;
42080 +
42081 + ntd_max = (max_desc_num(qh) + qh->interval - 1) / qh->interval;
42082 + if (skip_frames && !qh->channel)
42083 + ntd_max = ntd_max - skip_frames / qh->interval;
42084 +
42085 + max_xfer_size =
42086 + (qh->dev_speed ==
42087 + DWC_OTG_EP_SPEED_HIGH) ? MAX_ISOC_XFER_SIZE_HS :
42088 + MAX_ISOC_XFER_SIZE_FS;
42089 +
42090 + DWC_CIRCLEQ_FOREACH(qtd, &qh->qtd_list, qtd_list_entry) {
42091 + while ((qh->ntd < ntd_max)
42092 + && (qtd->isoc_frame_index_last <
42093 + qtd->urb->packet_count)) {
42094 +
42095 + dma_desc = &qh->desc_list[idx];
42096 + dwc_memset(dma_desc, 0x00, sizeof(dwc_otg_host_dma_desc_t));
42097 +
42098 + frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index_last];
42099 +
42100 + if (frame_desc->length > max_xfer_size)
42101 + qh->n_bytes[idx] = max_xfer_size;
42102 + else
42103 + qh->n_bytes[idx] = frame_desc->length;
42104 + dma_desc->status.b_isoc.n_bytes = qh->n_bytes[idx];
42105 + dma_desc->status.b_isoc.a = 1;
42106 + dma_desc->status.b_isoc.sts = 0;
42107 +
42108 + dma_desc->buf = qtd->urb->dma + frame_desc->offset;
42109 +
42110 + qh->ntd++;
42111 +
42112 + qtd->isoc_frame_index_last++;
42113 +
42114 +#ifdef ISOC_URB_GIVEBACK_ASAP
42115 + /*
42116 + * Set IOC for each descriptor corresponding to the
42117 + * last frame of the URB.
42118 + */
42119 + if (qtd->isoc_frame_index_last ==
42120 + qtd->urb->packet_count)
42121 + dma_desc->status.b_isoc.ioc = 1;
42122 +
42123 +#endif
42124 + idx = desclist_idx_inc(idx, inc, qh->dev_speed);
42125 + n_desc++;
42126 +
42127 + }
42128 + qtd->in_process = 1;
42129 + }
42130 +
42131 + qh->td_last = idx;
42132 +
42133 +#ifdef ISOC_URB_GIVEBACK_ASAP
42134 + /* Set IOC for the last descriptor if descriptor list is full */
42135 + if (qh->ntd == ntd_max) {
42136 + idx = desclist_idx_dec(qh->td_last, inc, qh->dev_speed);
42137 + qh->desc_list[idx].status.b_isoc.ioc = 1;
42138 + }
42139 +#else
42140 + /*
42141 + * Set IOC bit only for one descriptor.
42142 + * Always try to be ahead of HW processing,
42143 + * i.e. on IOC generation driver activates next descriptors but
42144 + * core continues to process descriptors followed the one with IOC set.
42145 + */
42146 +
42147 + if (n_desc > DESCNUM_THRESHOLD) {
42148 + /*
42149 + * Move IOC "up". Required even if there is only one QTD
42150 + * in the list, cause QTDs migth continue to be queued,
42151 + * but during the activation it was only one queued.
42152 + * Actually more than one QTD might be in the list if this function called
42153 + * from XferCompletion - QTDs was queued during HW processing of the previous
42154 + * descriptor chunk.
42155 + */
42156 + idx = dwc_desclist_idx_dec(idx, inc * ((qh->ntd + 1) / 2), qh->dev_speed);
42157 + } else {
42158 + /*
42159 + * Set the IOC for the latest descriptor
42160 + * if either number of descriptor is not greather than threshold
42161 + * or no more new descriptors activated.
42162 + */
42163 + idx = dwc_desclist_idx_dec(qh->td_last, inc, qh->dev_speed);
42164 + }
42165 +
42166 + qh->desc_list[idx].status.b_isoc.ioc = 1;
42167 +#endif
42168 +}
42169 +
42170 +static void init_non_isoc_dma_desc(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
42171 +{
42172 +
42173 + dwc_hc_t *hc;
42174 + dwc_otg_host_dma_desc_t *dma_desc;
42175 + dwc_otg_qtd_t *qtd;
42176 + int num_packets, len, n_desc = 0;
42177 +
42178 + hc = qh->channel;
42179 +
42180 + /*
42181 + * Start with hc->xfer_buff initialized in
42182 + * assign_and_init_hc(), then if SG transfer consists of multiple URBs,
42183 + * this pointer re-assigned to the buffer of the currently processed QTD.
42184 + * For non-SG request there is always one QTD active.
42185 + */
42186 +
42187 + DWC_CIRCLEQ_FOREACH(qtd, &qh->qtd_list, qtd_list_entry) {
42188 +
42189 + if (n_desc) {
42190 + /* SG request - more than 1 QTDs */
42191 + hc->xfer_buff = (uint8_t *)qtd->urb->dma + qtd->urb->actual_length;
42192 + hc->xfer_len = qtd->urb->length - qtd->urb->actual_length;
42193 + }
42194 +
42195 + qtd->n_desc = 0;
42196 +
42197 + do {
42198 + dma_desc = &qh->desc_list[n_desc];
42199 + len = hc->xfer_len;
42200 +
42201 + if (len > MAX_DMA_DESC_SIZE)
42202 + len = MAX_DMA_DESC_SIZE - hc->max_packet + 1;
42203 +
42204 + if (hc->ep_is_in) {
42205 + if (len > 0) {
42206 + num_packets = (len + hc->max_packet - 1) / hc->max_packet;
42207 + } else {
42208 + /* Need 1 packet for transfer length of 0. */
42209 + num_packets = 1;
42210 + }
42211 + /* Always program an integral # of max packets for IN transfers. */
42212 + len = num_packets * hc->max_packet;
42213 + }
42214 +
42215 + dma_desc->status.b.n_bytes = len;
42216 +
42217 + qh->n_bytes[n_desc] = len;
42218 +
42219 + if ((qh->ep_type == UE_CONTROL)
42220 + && (qtd->control_phase == DWC_OTG_CONTROL_SETUP))
42221 + dma_desc->status.b.sup = 1; /* Setup Packet */
42222 +
42223 + dma_desc->status.b.a = 1; /* Active descriptor */
42224 + dma_desc->status.b.sts = 0;
42225 +
42226 + dma_desc->buf =
42227 + ((unsigned long)hc->xfer_buff & 0xffffffff);
42228 +
42229 + /*
42230 + * Last descriptor(or single) of IN transfer
42231 + * with actual size less than MaxPacket.
42232 + */
42233 + if (len > hc->xfer_len) {
42234 + hc->xfer_len = 0;
42235 + } else {
42236 + hc->xfer_buff += len;
42237 + hc->xfer_len -= len;
42238 + }
42239 +
42240 + qtd->n_desc++;
42241 + n_desc++;
42242 + }
42243 + while ((hc->xfer_len > 0) && (n_desc != MAX_DMA_DESC_NUM_GENERIC));
42244 +
42245 +
42246 + qtd->in_process = 1;
42247 +
42248 + if (qh->ep_type == UE_CONTROL)
42249 + break;
42250 +
42251 + if (n_desc == MAX_DMA_DESC_NUM_GENERIC)
42252 + break;
42253 + }
42254 +
42255 + if (n_desc) {
42256 + /* Request Transfer Complete interrupt for the last descriptor */
42257 + qh->desc_list[n_desc - 1].status.b.ioc = 1;
42258 + /* End of List indicator */
42259 + qh->desc_list[n_desc - 1].status.b.eol = 1;
42260 +
42261 + hc->ntd = n_desc;
42262 + }
42263 +}
42264 +
42265 +/**
42266 + * For Control and Bulk endpoints initializes descriptor list
42267 + * and starts the transfer.
42268 + *
42269 + * For Interrupt and Isochronous endpoints initializes descriptor list
42270 + * then updates FrameList, marking appropriate entries as active.
42271 + * In case of Isochronous, the starting descriptor index is calculated based
42272 + * on the scheduled frame, but only on the first transfer descriptor within a session.
42273 + * Then starts the transfer via enabling the channel.
42274 + * For Isochronous endpoint the channel is not halted on XferComplete
42275 + * interrupt so remains assigned to the endpoint(QH) until session is done.
42276 + *
42277 + * @param hcd The HCD state structure for the DWC OTG controller.
42278 + * @param qh The QH to init.
42279 + *
42280 + * @return 0 if successful, negative error code otherwise.
42281 + */
42282 +void dwc_otg_hcd_start_xfer_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
42283 +{
42284 + /* Channel is already assigned */
42285 + dwc_hc_t *hc = qh->channel;
42286 + uint8_t skip_frames = 0;
42287 +
42288 + switch (hc->ep_type) {
42289 + case DWC_OTG_EP_TYPE_CONTROL:
42290 + case DWC_OTG_EP_TYPE_BULK:
42291 + init_non_isoc_dma_desc(hcd, qh);
42292 +
42293 + dwc_otg_hc_start_transfer_ddma(hcd->core_if, hc);
42294 + break;
42295 + case DWC_OTG_EP_TYPE_INTR:
42296 + init_non_isoc_dma_desc(hcd, qh);
42297 +
42298 + update_frame_list(hcd, qh, 1);
42299 +
42300 + dwc_otg_hc_start_transfer_ddma(hcd->core_if, hc);
42301 + break;
42302 + case DWC_OTG_EP_TYPE_ISOC:
42303 +
42304 + if (!qh->ntd)
42305 + skip_frames = recalc_initial_desc_idx(hcd, qh);
42306 +
42307 + init_isoc_dma_desc(hcd, qh, skip_frames);
42308 +
42309 + if (!hc->xfer_started) {
42310 +
42311 + update_frame_list(hcd, qh, 1);
42312 +
42313 + /*
42314 + * Always set to max, instead of actual size.
42315 + * Otherwise ntd will be changed with
42316 + * channel being enabled. Not recommended.
42317 + *
42318 + */
42319 + hc->ntd = max_desc_num(qh);
42320 + /* Enable channel only once for ISOC */
42321 + dwc_otg_hc_start_transfer_ddma(hcd->core_if, hc);
42322 + }
42323 +
42324 + break;
42325 + default:
42326 +
42327 + break;
42328 + }
42329 +}
42330 +
42331 +static void complete_isoc_xfer_ddma(dwc_otg_hcd_t * hcd,
42332 + dwc_hc_t * hc,
42333 + dwc_otg_hc_regs_t * hc_regs,
42334 + dwc_otg_halt_status_e halt_status)
42335 +{
42336 + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
42337 + dwc_otg_qtd_t *qtd, *qtd_tmp;
42338 + dwc_otg_qh_t *qh;
42339 + dwc_otg_host_dma_desc_t *dma_desc;
42340 + uint16_t idx, remain;
42341 + uint8_t urb_compl;
42342 +
42343 + qh = hc->qh;
42344 + idx = qh->td_first;
42345 +
42346 + if (hc->halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE) {
42347 + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &hc->qh->qtd_list, qtd_list_entry)
42348 + qtd->in_process = 0;
42349 + return;
42350 + } else if ((halt_status == DWC_OTG_HC_XFER_AHB_ERR) ||
42351 + (halt_status == DWC_OTG_HC_XFER_BABBLE_ERR)) {
42352 + /*
42353 + * Channel is halted in these error cases.
42354 + * Considered as serious issues.
42355 + * Complete all URBs marking all frames as failed,
42356 + * irrespective whether some of the descriptors(frames) succeeded or no.
42357 + * Pass error code to completion routine as well, to
42358 + * update urb->status, some of class drivers might use it to stop
42359 + * queing transfer requests.
42360 + */
42361 + int err = (halt_status == DWC_OTG_HC_XFER_AHB_ERR)
42362 + ? (-DWC_E_IO)
42363 + : (-DWC_E_OVERFLOW);
42364 +
42365 + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &hc->qh->qtd_list, qtd_list_entry) {
42366 + for (idx = 0; idx < qtd->urb->packet_count; idx++) {
42367 + frame_desc = &qtd->urb->iso_descs[idx];
42368 + frame_desc->status = err;
42369 + }
42370 + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, err);
42371 + dwc_otg_hcd_qtd_remove_and_free(hcd, qtd, qh);
42372 + }
42373 + return;
42374 + }
42375 +
42376 + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &hc->qh->qtd_list, qtd_list_entry) {
42377 +
42378 + if (!qtd->in_process)
42379 + break;
42380 +
42381 + urb_compl = 0;
42382 +
42383 + do {
42384 +
42385 + dma_desc = &qh->desc_list[idx];
42386 +
42387 + frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
42388 + remain = hc->ep_is_in ? dma_desc->status.b_isoc.n_bytes : 0;
42389 +
42390 + if (dma_desc->status.b_isoc.sts == DMA_DESC_STS_PKTERR) {
42391 + /*
42392 + * XactError or, unable to complete all the transactions
42393 + * in the scheduled micro-frame/frame,
42394 + * both indicated by DMA_DESC_STS_PKTERR.
42395 + */
42396 + qtd->urb->error_count++;
42397 + frame_desc->actual_length = qh->n_bytes[idx] - remain;
42398 + frame_desc->status = -DWC_E_PROTOCOL;
42399 + } else {
42400 + /* Success */
42401 +
42402 + frame_desc->actual_length = qh->n_bytes[idx] - remain;
42403 + frame_desc->status = 0;
42404 + }
42405 +
42406 + if (++qtd->isoc_frame_index == qtd->urb->packet_count) {
42407 + /*
42408 + * urb->status is not used for isoc transfers here.
42409 + * The individual frame_desc status are used instead.
42410 + */
42411 +
42412 + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
42413 + dwc_otg_hcd_qtd_remove_and_free(hcd, qtd, qh);
42414 +
42415 + /*
42416 + * This check is necessary because urb_dequeue can be called
42417 + * from urb complete callback(sound driver example).
42418 + * All pending URBs are dequeued there, so no need for
42419 + * further processing.
42420 + */
42421 + if (hc->halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE) {
42422 + return;
42423 + }
42424 +
42425 + urb_compl = 1;
42426 +
42427 + }
42428 +
42429 + qh->ntd--;
42430 +
42431 + /* Stop if IOC requested descriptor reached */
42432 + if (dma_desc->status.b_isoc.ioc) {
42433 + idx = desclist_idx_inc(idx, qh->interval, hc->speed);
42434 + goto stop_scan;
42435 + }
42436 +
42437 + idx = desclist_idx_inc(idx, qh->interval, hc->speed);
42438 +
42439 + if (urb_compl)
42440 + break;
42441 + }
42442 + while (idx != qh->td_first);
42443 + }
42444 +stop_scan:
42445 + qh->td_first = idx;
42446 +}
42447 +
42448 +uint8_t update_non_isoc_urb_state_ddma(dwc_otg_hcd_t * hcd,
42449 + dwc_hc_t * hc,
42450 + dwc_otg_qtd_t * qtd,
42451 + dwc_otg_host_dma_desc_t * dma_desc,
42452 + dwc_otg_halt_status_e halt_status,
42453 + uint32_t n_bytes, uint8_t * xfer_done)
42454 +{
42455 +
42456 + uint16_t remain = hc->ep_is_in ? dma_desc->status.b.n_bytes : 0;
42457 + dwc_otg_hcd_urb_t *urb = qtd->urb;
42458 +
42459 + if (halt_status == DWC_OTG_HC_XFER_AHB_ERR) {
42460 + urb->status = -DWC_E_IO;
42461 + return 1;
42462 + }
42463 + if (dma_desc->status.b.sts == DMA_DESC_STS_PKTERR) {
42464 + switch (halt_status) {
42465 + case DWC_OTG_HC_XFER_STALL:
42466 + urb->status = -DWC_E_PIPE;
42467 + break;
42468 + case DWC_OTG_HC_XFER_BABBLE_ERR:
42469 + urb->status = -DWC_E_OVERFLOW;
42470 + break;
42471 + case DWC_OTG_HC_XFER_XACT_ERR:
42472 + urb->status = -DWC_E_PROTOCOL;
42473 + break;
42474 + default:
42475 + DWC_ERROR("%s: Unhandled descriptor error status (%d)\n", __func__,
42476 + halt_status);
42477 + break;
42478 + }
42479 + return 1;
42480 + }
42481 +
42482 + if (dma_desc->status.b.a == 1) {
42483 + DWC_DEBUGPL(DBG_HCDV,
42484 + "Active descriptor encountered on channel %d\n",
42485 + hc->hc_num);
42486 + return 0;
42487 + }
42488 +
42489 + if (hc->ep_type == DWC_OTG_EP_TYPE_CONTROL) {
42490 + if (qtd->control_phase == DWC_OTG_CONTROL_DATA) {
42491 + urb->actual_length += n_bytes - remain;
42492 + if (remain || urb->actual_length == urb->length) {
42493 + /*
42494 + * For Control Data stage do not set urb->status=0 to prevent
42495 + * URB callback. Set it when Status phase done. See below.
42496 + */
42497 + *xfer_done = 1;
42498 + }
42499 +
42500 + } else if (qtd->control_phase == DWC_OTG_CONTROL_STATUS) {
42501 + urb->status = 0;
42502 + *xfer_done = 1;
42503 + }
42504 + /* No handling for SETUP stage */
42505 + } else {
42506 + /* BULK and INTR */
42507 + urb->actual_length += n_bytes - remain;
42508 + if (remain || urb->actual_length == urb->length) {
42509 + urb->status = 0;
42510 + *xfer_done = 1;
42511 + }
42512 + }
42513 +
42514 + return 0;
42515 +}
42516 +
42517 +static void complete_non_isoc_xfer_ddma(dwc_otg_hcd_t * hcd,
42518 + dwc_hc_t * hc,
42519 + dwc_otg_hc_regs_t * hc_regs,
42520 + dwc_otg_halt_status_e halt_status)
42521 +{
42522 + dwc_otg_hcd_urb_t *urb = NULL;
42523 + dwc_otg_qtd_t *qtd, *qtd_tmp;
42524 + dwc_otg_qh_t *qh;
42525 + dwc_otg_host_dma_desc_t *dma_desc;
42526 + uint32_t n_bytes, n_desc, i;
42527 + uint8_t failed = 0, xfer_done;
42528 +
42529 + n_desc = 0;
42530 +
42531 + qh = hc->qh;
42532 +
42533 + if (hc->halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE) {
42534 + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &hc->qh->qtd_list, qtd_list_entry) {
42535 + qtd->in_process = 0;
42536 + }
42537 + return;
42538 + }
42539 +
42540 + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &qh->qtd_list, qtd_list_entry) {
42541 +
42542 + urb = qtd->urb;
42543 +
42544 + n_bytes = 0;
42545 + xfer_done = 0;
42546 +
42547 + for (i = 0; i < qtd->n_desc; i++) {
42548 + dma_desc = &qh->desc_list[n_desc];
42549 +
42550 + n_bytes = qh->n_bytes[n_desc];
42551 +
42552 + failed =
42553 + update_non_isoc_urb_state_ddma(hcd, hc, qtd,
42554 + dma_desc,
42555 + halt_status, n_bytes,
42556 + &xfer_done);
42557 +
42558 + if (failed
42559 + || (xfer_done
42560 + && (urb->status != -DWC_E_IN_PROGRESS))) {
42561 +
42562 + hcd->fops->complete(hcd, urb->priv, urb,
42563 + urb->status);
42564 + dwc_otg_hcd_qtd_remove_and_free(hcd, qtd, qh);
42565 +
42566 + if (failed)
42567 + goto stop_scan;
42568 + } else if (qh->ep_type == UE_CONTROL) {
42569 + if (qtd->control_phase == DWC_OTG_CONTROL_SETUP) {
42570 + if (urb->length > 0) {
42571 + qtd->control_phase = DWC_OTG_CONTROL_DATA;
42572 + } else {
42573 + qtd->control_phase = DWC_OTG_CONTROL_STATUS;
42574 + }
42575 + DWC_DEBUGPL(DBG_HCDV, " Control setup transaction done\n");
42576 + } else if (qtd->control_phase == DWC_OTG_CONTROL_DATA) {
42577 + if (xfer_done) {
42578 + qtd->control_phase = DWC_OTG_CONTROL_STATUS;
42579 + DWC_DEBUGPL(DBG_HCDV, " Control data transfer done\n");
42580 + } else if (i + 1 == qtd->n_desc) {
42581 + /*
42582 + * Last descriptor for Control data stage which is
42583 + * not completed yet.
42584 + */
42585 + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
42586 + }
42587 + }
42588 + }
42589 +
42590 + n_desc++;
42591 + }
42592 +
42593 + }
42594 +
42595 +stop_scan:
42596 +
42597 + if (qh->ep_type != UE_CONTROL) {
42598 + /*
42599 + * Resetting the data toggle for bulk
42600 + * and interrupt endpoints in case of stall. See handle_hc_stall_intr()
42601 + */
42602 + if (halt_status == DWC_OTG_HC_XFER_STALL)
42603 + qh->data_toggle = DWC_OTG_HC_PID_DATA0;
42604 + else
42605 + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
42606 + }
42607 +
42608 + if (halt_status == DWC_OTG_HC_XFER_COMPLETE) {
42609 + hcint_data_t hcint;
42610 + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
42611 + if (hcint.b.nyet) {
42612 + /*
42613 + * Got a NYET on the last transaction of the transfer. It
42614 + * means that the endpoint should be in the PING state at the
42615 + * beginning of the next transfer.
42616 + */
42617 + qh->ping_state = 1;
42618 + clear_hc_int(hc_regs, nyet);
42619 + }
42620 +
42621 + }
42622 +
42623 +}
42624 +
42625 +/**
42626 + * This function is called from interrupt handlers.
42627 + * Scans the descriptor list, updates URB's status and
42628 + * calls completion routine for the URB if it's done.
42629 + * Releases the channel to be used by other transfers.
42630 + * In case of Isochronous endpoint the channel is not halted until
42631 + * the end of the session, i.e. QTD list is empty.
42632 + * If periodic channel released the FrameList is updated accordingly.
42633 + *
42634 + * Calls transaction selection routines to activate pending transfers.
42635 + *
42636 + * @param hcd The HCD state structure for the DWC OTG controller.
42637 + * @param hc Host channel, the transfer is completed on.
42638 + * @param hc_regs Host channel registers.
42639 + * @param halt_status Reason the channel is being halted,
42640 + * or just XferComplete for isochronous transfer
42641 + */
42642 +void dwc_otg_hcd_complete_xfer_ddma(dwc_otg_hcd_t * hcd,
42643 + dwc_hc_t * hc,
42644 + dwc_otg_hc_regs_t * hc_regs,
42645 + dwc_otg_halt_status_e halt_status)
42646 +{
42647 + uint8_t continue_isoc_xfer = 0;
42648 + dwc_otg_transaction_type_e tr_type;
42649 + dwc_otg_qh_t *qh = hc->qh;
42650 +
42651 + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
42652 +
42653 + complete_isoc_xfer_ddma(hcd, hc, hc_regs, halt_status);
42654 +
42655 + /* Release the channel if halted or session completed */
42656 + if (halt_status != DWC_OTG_HC_XFER_COMPLETE ||
42657 + DWC_CIRCLEQ_EMPTY(&qh->qtd_list)) {
42658 +
42659 + /* Halt the channel if session completed */
42660 + if (halt_status == DWC_OTG_HC_XFER_COMPLETE) {
42661 + dwc_otg_hc_halt(hcd->core_if, hc, halt_status);
42662 + }
42663 +
42664 + release_channel_ddma(hcd, qh);
42665 + dwc_otg_hcd_qh_remove(hcd, qh);
42666 + } else {
42667 + /* Keep in assigned schedule to continue transfer */
42668 + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_assigned,
42669 + &qh->qh_list_entry);
42670 + continue_isoc_xfer = 1;
42671 +
42672 + }
42673 + /** @todo Consider the case when period exceeds FrameList size.
42674 + * Frame Rollover interrupt should be used.
42675 + */
42676 + } else {
42677 + /* Scan descriptor list to complete the URB(s), then release the channel */
42678 + complete_non_isoc_xfer_ddma(hcd, hc, hc_regs, halt_status);
42679 +
42680 + release_channel_ddma(hcd, qh);
42681 + dwc_otg_hcd_qh_remove(hcd, qh);
42682 +
42683 + if (!DWC_CIRCLEQ_EMPTY(&qh->qtd_list)) {
42684 + /* Add back to inactive non-periodic schedule on normal completion */
42685 + dwc_otg_hcd_qh_add(hcd, qh);
42686 + }
42687 +
42688 + }
42689 + tr_type = dwc_otg_hcd_select_transactions(hcd);
42690 + if (tr_type != DWC_OTG_TRANSACTION_NONE || continue_isoc_xfer) {
42691 + if (continue_isoc_xfer) {
42692 + if (tr_type == DWC_OTG_TRANSACTION_NONE) {
42693 + tr_type = DWC_OTG_TRANSACTION_PERIODIC;
42694 + } else if (tr_type == DWC_OTG_TRANSACTION_NON_PERIODIC) {
42695 + tr_type = DWC_OTG_TRANSACTION_ALL;
42696 + }
42697 + }
42698 + dwc_otg_hcd_queue_transactions(hcd, tr_type);
42699 + }
42700 +}
42701 +
42702 +#endif /* DWC_DEVICE_ONLY */
42703 --- /dev/null
42704 +++ b/drivers/usb/host/dwc_otg/dwc_otg_hcd_if.h
42705 @@ -0,0 +1,417 @@
42706 +/* ==========================================================================
42707 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd_if.h $
42708 + * $Revision: #12 $
42709 + * $Date: 2011/10/26 $
42710 + * $Change: 1873028 $
42711 + *
42712 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
42713 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
42714 + * otherwise expressly agreed to in writing between Synopsys and you.
42715 + *
42716 + * The Software IS NOT an item of Licensed Software or Licensed Product under
42717 + * any End User Software License Agreement or Agreement for Licensed Product
42718 + * with Synopsys or any supplement thereto. You are permitted to use and
42719 + * redistribute this Software in source and binary forms, with or without
42720 + * modification, provided that redistributions of source code must retain this
42721 + * notice. You may not view, use, disclose, copy or distribute this file or
42722 + * any information contained herein except pursuant to this license grant from
42723 + * Synopsys. If you do not agree with this notice, including the disclaimer
42724 + * below, then you are not authorized to use the Software.
42725 + *
42726 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
42727 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42728 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
42729 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
42730 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
42731 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
42732 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
42733 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42734 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
42735 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
42736 + * DAMAGE.
42737 + * ========================================================================== */
42738 +#ifndef DWC_DEVICE_ONLY
42739 +#ifndef __DWC_HCD_IF_H__
42740 +#define __DWC_HCD_IF_H__
42741 +
42742 +#include "dwc_otg_core_if.h"
42743 +
42744 +/** @file
42745 + * This file defines DWC_OTG HCD Core API.
42746 + */
42747 +
42748 +struct dwc_otg_hcd;
42749 +typedef struct dwc_otg_hcd dwc_otg_hcd_t;
42750 +
42751 +struct dwc_otg_hcd_urb;
42752 +typedef struct dwc_otg_hcd_urb dwc_otg_hcd_urb_t;
42753 +
42754 +/** @name HCD Function Driver Callbacks */
42755 +/** @{ */
42756 +
42757 +/** This function is called whenever core switches to host mode. */
42758 +typedef int (*dwc_otg_hcd_start_cb_t) (dwc_otg_hcd_t * hcd);
42759 +
42760 +/** This function is called when device has been disconnected */
42761 +typedef int (*dwc_otg_hcd_disconnect_cb_t) (dwc_otg_hcd_t * hcd);
42762 +
42763 +/** Wrapper provides this function to HCD to core, so it can get hub information to which device is connected */
42764 +typedef int (*dwc_otg_hcd_hub_info_from_urb_cb_t) (dwc_otg_hcd_t * hcd,
42765 + void *urb_handle,
42766 + uint32_t * hub_addr,
42767 + uint32_t * port_addr);
42768 +/** Via this function HCD core gets device speed */
42769 +typedef int (*dwc_otg_hcd_speed_from_urb_cb_t) (dwc_otg_hcd_t * hcd,
42770 + void *urb_handle);
42771 +
42772 +/** This function is called when urb is completed */
42773 +typedef int (*dwc_otg_hcd_complete_urb_cb_t) (dwc_otg_hcd_t * hcd,
42774 + void *urb_handle,
42775 + dwc_otg_hcd_urb_t * dwc_otg_urb,
42776 + int32_t status);
42777 +
42778 +/** Via this function HCD core gets b_hnp_enable parameter */
42779 +typedef int (*dwc_otg_hcd_get_b_hnp_enable) (dwc_otg_hcd_t * hcd);
42780 +
42781 +struct dwc_otg_hcd_function_ops {
42782 + dwc_otg_hcd_start_cb_t start;
42783 + dwc_otg_hcd_disconnect_cb_t disconnect;
42784 + dwc_otg_hcd_hub_info_from_urb_cb_t hub_info;
42785 + dwc_otg_hcd_speed_from_urb_cb_t speed;
42786 + dwc_otg_hcd_complete_urb_cb_t complete;
42787 + dwc_otg_hcd_get_b_hnp_enable get_b_hnp_enable;
42788 +};
42789 +/** @} */
42790 +
42791 +/** @name HCD Core API */
42792 +/** @{ */
42793 +/** This function allocates dwc_otg_hcd structure and returns pointer on it. */
42794 +extern dwc_otg_hcd_t *dwc_otg_hcd_alloc_hcd(void);
42795 +
42796 +/** This function should be called to initiate HCD Core.
42797 + *
42798 + * @param hcd The HCD
42799 + * @param core_if The DWC_OTG Core
42800 + *
42801 + * Returns -DWC_E_NO_MEMORY if no enough memory.
42802 + * Returns 0 on success
42803 + */
42804 +extern int dwc_otg_hcd_init(dwc_otg_hcd_t * hcd, dwc_otg_core_if_t * core_if);
42805 +
42806 +/** Frees HCD
42807 + *
42808 + * @param hcd The HCD
42809 + */
42810 +extern void dwc_otg_hcd_remove(dwc_otg_hcd_t * hcd);
42811 +
42812 +/** This function should be called on every hardware interrupt.
42813 + *
42814 + * @param dwc_otg_hcd The HCD
42815 + *
42816 + * Returns non zero if interrupt is handled
42817 + * Return 0 if interrupt is not handled
42818 + */
42819 +extern int32_t dwc_otg_hcd_handle_intr(dwc_otg_hcd_t * dwc_otg_hcd);
42820 +
42821 +/** This function is used to handle the fast interrupt
42822 + *
42823 + */
42824 +extern void __attribute__ ((naked)) dwc_otg_hcd_handle_fiq(void);
42825 +
42826 +/**
42827 + * Returns private data set by
42828 + * dwc_otg_hcd_set_priv_data function.
42829 + *
42830 + * @param hcd The HCD
42831 + */
42832 +extern void *dwc_otg_hcd_get_priv_data(dwc_otg_hcd_t * hcd);
42833 +
42834 +/**
42835 + * Set private data.
42836 + *
42837 + * @param hcd The HCD
42838 + * @param priv_data pointer to be stored in private data
42839 + */
42840 +extern void dwc_otg_hcd_set_priv_data(dwc_otg_hcd_t * hcd, void *priv_data);
42841 +
42842 +/**
42843 + * This function initializes the HCD Core.
42844 + *
42845 + * @param hcd The HCD
42846 + * @param fops The Function Driver Operations data structure containing pointers to all callbacks.
42847 + *
42848 + * Returns -DWC_E_NO_DEVICE if Core is currently is in device mode.
42849 + * Returns 0 on success
42850 + */
42851 +extern int dwc_otg_hcd_start(dwc_otg_hcd_t * hcd,
42852 + struct dwc_otg_hcd_function_ops *fops);
42853 +
42854 +/**
42855 + * Halts the DWC_otg host mode operations in a clean manner. USB transfers are
42856 + * stopped.
42857 + *
42858 + * @param hcd The HCD
42859 + */
42860 +extern void dwc_otg_hcd_stop(dwc_otg_hcd_t * hcd);
42861 +
42862 +/**
42863 + * Handles hub class-specific requests.
42864 + *
42865 + * @param dwc_otg_hcd The HCD
42866 + * @param typeReq Request Type
42867 + * @param wValue wValue from control request
42868 + * @param wIndex wIndex from control request
42869 + * @param buf data buffer
42870 + * @param wLength data buffer length
42871 + *
42872 + * Returns -DWC_E_INVALID if invalid argument is passed
42873 + * Returns 0 on success
42874 + */
42875 +extern int dwc_otg_hcd_hub_control(dwc_otg_hcd_t * dwc_otg_hcd,
42876 + uint16_t typeReq, uint16_t wValue,
42877 + uint16_t wIndex, uint8_t * buf,
42878 + uint16_t wLength);
42879 +
42880 +/**
42881 + * Returns otg port number.
42882 + *
42883 + * @param hcd The HCD
42884 + */
42885 +extern uint32_t dwc_otg_hcd_otg_port(dwc_otg_hcd_t * hcd);
42886 +
42887 +/**
42888 + * Returns OTG version - either 1.3 or 2.0.
42889 + *
42890 + * @param core_if The core_if structure pointer
42891 + */
42892 +extern uint16_t dwc_otg_get_otg_version(dwc_otg_core_if_t * core_if);
42893 +
42894 +/**
42895 + * Returns 1 if currently core is acting as B host, and 0 otherwise.
42896 + *
42897 + * @param hcd The HCD
42898 + */
42899 +extern uint32_t dwc_otg_hcd_is_b_host(dwc_otg_hcd_t * hcd);
42900 +
42901 +/**
42902 + * Returns current frame number.
42903 + *
42904 + * @param hcd The HCD
42905 + */
42906 +extern int dwc_otg_hcd_get_frame_number(dwc_otg_hcd_t * hcd);
42907 +
42908 +/**
42909 + * Dumps hcd state.
42910 + *
42911 + * @param hcd The HCD
42912 + */
42913 +extern void dwc_otg_hcd_dump_state(dwc_otg_hcd_t * hcd);
42914 +
42915 +/**
42916 + * Dump the average frame remaining at SOF. This can be used to
42917 + * determine average interrupt latency. Frame remaining is also shown for
42918 + * start transfer and two additional sample points.
42919 + * Currently this function is not implemented.
42920 + *
42921 + * @param hcd The HCD
42922 + */
42923 +extern void dwc_otg_hcd_dump_frrem(dwc_otg_hcd_t * hcd);
42924 +
42925 +/**
42926 + * Sends LPM transaction to the local device.
42927 + *
42928 + * @param hcd The HCD
42929 + * @param devaddr Device Address
42930 + * @param hird Host initiated resume duration
42931 + * @param bRemoteWake Value of bRemoteWake field in LPM transaction
42932 + *
42933 + * Returns negative value if sending LPM transaction was not succeeded.
42934 + * Returns 0 on success.
42935 + */
42936 +extern int dwc_otg_hcd_send_lpm(dwc_otg_hcd_t * hcd, uint8_t devaddr,
42937 + uint8_t hird, uint8_t bRemoteWake);
42938 +
42939 +/* URB interface */
42940 +
42941 +/**
42942 + * Allocates memory for dwc_otg_hcd_urb structure.
42943 + * Allocated memory should be freed by call of DWC_FREE.
42944 + *
42945 + * @param hcd The HCD
42946 + * @param iso_desc_count Count of ISOC descriptors
42947 + * @param atomic_alloc Specefies whether to perform atomic allocation.
42948 + */
42949 +extern dwc_otg_hcd_urb_t *dwc_otg_hcd_urb_alloc(dwc_otg_hcd_t * hcd,
42950 + int iso_desc_count,
42951 + int atomic_alloc);
42952 +
42953 +/**
42954 + * Set pipe information in URB.
42955 + *
42956 + * @param hcd_urb DWC_OTG URB
42957 + * @param devaddr Device Address
42958 + * @param ep_num Endpoint Number
42959 + * @param ep_type Endpoint Type
42960 + * @param ep_dir Endpoint Direction
42961 + * @param mps Max Packet Size
42962 + */
42963 +extern void dwc_otg_hcd_urb_set_pipeinfo(dwc_otg_hcd_urb_t * hcd_urb,
42964 + uint8_t devaddr, uint8_t ep_num,
42965 + uint8_t ep_type, uint8_t ep_dir,
42966 + uint16_t mps);
42967 +
42968 +/* Transfer flags */
42969 +#define URB_GIVEBACK_ASAP 0x1
42970 +#define URB_SEND_ZERO_PACKET 0x2
42971 +
42972 +/**
42973 + * Sets dwc_otg_hcd_urb parameters.
42974 + *
42975 + * @param urb DWC_OTG URB allocated by dwc_otg_hcd_urb_alloc function.
42976 + * @param urb_handle Unique handle for request, this will be passed back
42977 + * to function driver in completion callback.
42978 + * @param buf The buffer for the data
42979 + * @param dma The DMA buffer for the data
42980 + * @param buflen Transfer length
42981 + * @param sp Buffer for setup data
42982 + * @param sp_dma DMA address of setup data buffer
42983 + * @param flags Transfer flags
42984 + * @param interval Polling interval for interrupt or isochronous transfers.
42985 + */
42986 +extern void dwc_otg_hcd_urb_set_params(dwc_otg_hcd_urb_t * urb,
42987 + void *urb_handle, void *buf,
42988 + dwc_dma_t dma, uint32_t buflen, void *sp,
42989 + dwc_dma_t sp_dma, uint32_t flags,
42990 + uint16_t interval);
42991 +
42992 +/** Gets status from dwc_otg_hcd_urb
42993 + *
42994 + * @param dwc_otg_urb DWC_OTG URB
42995 + */
42996 +extern uint32_t dwc_otg_hcd_urb_get_status(dwc_otg_hcd_urb_t * dwc_otg_urb);
42997 +
42998 +/** Gets actual length from dwc_otg_hcd_urb
42999 + *
43000 + * @param dwc_otg_urb DWC_OTG URB
43001 + */
43002 +extern uint32_t dwc_otg_hcd_urb_get_actual_length(dwc_otg_hcd_urb_t *
43003 + dwc_otg_urb);
43004 +
43005 +/** Gets error count from dwc_otg_hcd_urb. Only for ISOC URBs
43006 + *
43007 + * @param dwc_otg_urb DWC_OTG URB
43008 + */
43009 +extern uint32_t dwc_otg_hcd_urb_get_error_count(dwc_otg_hcd_urb_t *
43010 + dwc_otg_urb);
43011 +
43012 +/** Set ISOC descriptor offset and length
43013 + *
43014 + * @param dwc_otg_urb DWC_OTG URB
43015 + * @param desc_num ISOC descriptor number
43016 + * @param offset Offset from beginig of buffer.
43017 + * @param length Transaction length
43018 + */
43019 +extern void dwc_otg_hcd_urb_set_iso_desc_params(dwc_otg_hcd_urb_t * dwc_otg_urb,
43020 + int desc_num, uint32_t offset,
43021 + uint32_t length);
43022 +
43023 +/** Get status of ISOC descriptor, specified by desc_num
43024 + *
43025 + * @param dwc_otg_urb DWC_OTG URB
43026 + * @param desc_num ISOC descriptor number
43027 + */
43028 +extern uint32_t dwc_otg_hcd_urb_get_iso_desc_status(dwc_otg_hcd_urb_t *
43029 + dwc_otg_urb, int desc_num);
43030 +
43031 +/** Get actual length of ISOC descriptor, specified by desc_num
43032 + *
43033 + * @param dwc_otg_urb DWC_OTG URB
43034 + * @param desc_num ISOC descriptor number
43035 + */
43036 +extern uint32_t dwc_otg_hcd_urb_get_iso_desc_actual_length(dwc_otg_hcd_urb_t *
43037 + dwc_otg_urb,
43038 + int desc_num);
43039 +
43040 +/** Queue URB. After transfer is completes, the complete callback will be called with the URB status
43041 + *
43042 + * @param dwc_otg_hcd The HCD
43043 + * @param dwc_otg_urb DWC_OTG URB
43044 + * @param ep_handle Out parameter for returning endpoint handle
43045 + * @param atomic_alloc Flag to do atomic allocation if needed
43046 + *
43047 + * Returns -DWC_E_NO_DEVICE if no device is connected.
43048 + * Returns -DWC_E_NO_MEMORY if there is no enough memory.
43049 + * Returns 0 on success.
43050 + */
43051 +extern int dwc_otg_hcd_urb_enqueue(dwc_otg_hcd_t * dwc_otg_hcd,
43052 + dwc_otg_hcd_urb_t * dwc_otg_urb,
43053 + void **ep_handle, int atomic_alloc);
43054 +
43055 +/** De-queue the specified URB
43056 + *
43057 + * @param dwc_otg_hcd The HCD
43058 + * @param dwc_otg_urb DWC_OTG URB
43059 + */
43060 +extern int dwc_otg_hcd_urb_dequeue(dwc_otg_hcd_t * dwc_otg_hcd,
43061 + dwc_otg_hcd_urb_t * dwc_otg_urb);
43062 +
43063 +/** Frees resources in the DWC_otg controller related to a given endpoint.
43064 + * Any URBs for the endpoint must already be dequeued.
43065 + *
43066 + * @param hcd The HCD
43067 + * @param ep_handle Endpoint handle, returned by dwc_otg_hcd_urb_enqueue function
43068 + * @param retry Number of retries if there are queued transfers.
43069 + *
43070 + * Returns -DWC_E_INVALID if invalid arguments are passed.
43071 + * Returns 0 on success
43072 + */
43073 +extern int dwc_otg_hcd_endpoint_disable(dwc_otg_hcd_t * hcd, void *ep_handle,
43074 + int retry);
43075 +
43076 +/* Resets the data toggle in qh structure. This function can be called from
43077 + * usb_clear_halt routine.
43078 + *
43079 + * @param hcd The HCD
43080 + * @param ep_handle Endpoint handle, returned by dwc_otg_hcd_urb_enqueue function
43081 + *
43082 + * Returns -DWC_E_INVALID if invalid arguments are passed.
43083 + * Returns 0 on success
43084 + */
43085 +extern int dwc_otg_hcd_endpoint_reset(dwc_otg_hcd_t * hcd, void *ep_handle);
43086 +
43087 +/** Returns 1 if status of specified port is changed and 0 otherwise.
43088 + *
43089 + * @param hcd The HCD
43090 + * @param port Port number
43091 + */
43092 +extern int dwc_otg_hcd_is_status_changed(dwc_otg_hcd_t * hcd, int port);
43093 +
43094 +/** Call this function to check if bandwidth was allocated for specified endpoint.
43095 + * Only for ISOC and INTERRUPT endpoints.
43096 + *
43097 + * @param hcd The HCD
43098 + * @param ep_handle Endpoint handle
43099 + */
43100 +extern int dwc_otg_hcd_is_bandwidth_allocated(dwc_otg_hcd_t * hcd,
43101 + void *ep_handle);
43102 +
43103 +/** Call this function to check if bandwidth was freed for specified endpoint.
43104 + *
43105 + * @param hcd The HCD
43106 + * @param ep_handle Endpoint handle
43107 + */
43108 +extern int dwc_otg_hcd_is_bandwidth_freed(dwc_otg_hcd_t * hcd, void *ep_handle);
43109 +
43110 +/** Returns bandwidth allocated for specified endpoint in microseconds.
43111 + * Only for ISOC and INTERRUPT endpoints.
43112 + *
43113 + * @param hcd The HCD
43114 + * @param ep_handle Endpoint handle
43115 + */
43116 +extern uint8_t dwc_otg_hcd_get_ep_bandwidth(dwc_otg_hcd_t * hcd,
43117 + void *ep_handle);
43118 +
43119 +/** @} */
43120 +
43121 +#endif /* __DWC_HCD_IF_H__ */
43122 +#endif /* DWC_DEVICE_ONLY */
43123 --- /dev/null
43124 +++ b/drivers/usb/host/dwc_otg/dwc_otg_hcd_intr.c
43125 @@ -0,0 +1,2752 @@
43126 +/* ==========================================================================
43127 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd_intr.c $
43128 + * $Revision: #89 $
43129 + * $Date: 2011/10/20 $
43130 + * $Change: 1869487 $
43131 + *
43132 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
43133 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
43134 + * otherwise expressly agreed to in writing between Synopsys and you.
43135 + *
43136 + * The Software IS NOT an item of Licensed Software or Licensed Product under
43137 + * any End User Software License Agreement or Agreement for Licensed Product
43138 + * with Synopsys or any supplement thereto. You are permitted to use and
43139 + * redistribute this Software in source and binary forms, with or without
43140 + * modification, provided that redistributions of source code must retain this
43141 + * notice. You may not view, use, disclose, copy or distribute this file or
43142 + * any information contained herein except pursuant to this license grant from
43143 + * Synopsys. If you do not agree with this notice, including the disclaimer
43144 + * below, then you are not authorized to use the Software.
43145 + *
43146 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
43147 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43148 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43149 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
43150 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
43151 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
43152 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
43153 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
43154 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43155 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
43156 + * DAMAGE.
43157 + * ========================================================================== */
43158 +#ifndef DWC_DEVICE_ONLY
43159 +
43160 +#include "dwc_otg_hcd.h"
43161 +#include "dwc_otg_regs.h"
43162 +
43163 +#include <linux/jiffies.h>
43164 +#include <asm/fiq.h>
43165 +
43166 +
43167 +extern bool microframe_schedule;
43168 +
43169 +/** @file
43170 + * This file contains the implementation of the HCD Interrupt handlers.
43171 + */
43172 +
43173 +int fiq_done, int_done;
43174 +
43175 +#ifdef FIQ_DEBUG
43176 +char buffer[1000*16];
43177 +int wptr;
43178 +void notrace _fiq_print(FIQDBG_T dbg_lvl, char *fmt, ...)
43179 +{
43180 + FIQDBG_T dbg_lvl_req = FIQDBG_PORTHUB;
43181 + va_list args;
43182 + char text[17];
43183 + hfnum_data_t hfnum = { .d32 = FIQ_READ(dwc_regs_base + 0x408) };
43184 +
43185 + if(dbg_lvl & dbg_lvl_req || dbg_lvl == FIQDBG_ERR)
43186 + {
43187 + local_fiq_disable();
43188 + snprintf(text, 9, "%4d%d:%d ", hfnum.b.frnum/8, hfnum.b.frnum%8, 8 - hfnum.b.frrem/937);
43189 + va_start(args, fmt);
43190 + vsnprintf(text+8, 9, fmt, args);
43191 + va_end(args);
43192 +
43193 + memcpy(buffer + wptr, text, 16);
43194 + wptr = (wptr + 16) % sizeof(buffer);
43195 + local_fiq_enable();
43196 + }
43197 +}
43198 +#endif
43199 +
43200 +/** This function handles interrupts for the HCD. */
43201 +int32_t dwc_otg_hcd_handle_intr(dwc_otg_hcd_t * dwc_otg_hcd)
43202 +{
43203 + int retval = 0;
43204 + static int last_time;
43205 + dwc_otg_core_if_t *core_if = dwc_otg_hcd->core_if;
43206 + gintsts_data_t gintsts;
43207 + gintmsk_data_t gintmsk;
43208 + hfnum_data_t hfnum;
43209 + haintmsk_data_t haintmsk;
43210 +
43211 +#ifdef DEBUG
43212 + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
43213 +
43214 +#endif
43215 +
43216 + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
43217 + gintmsk.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
43218 +
43219 + /* Exit from ISR if core is hibernated */
43220 + if (core_if->hibernation_suspend == 1) {
43221 + goto exit_handler_routine;
43222 + }
43223 + DWC_SPINLOCK(dwc_otg_hcd->lock);
43224 + /* Check if HOST Mode */
43225 + if (dwc_otg_is_host_mode(core_if)) {
43226 + if (fiq_enable) {
43227 + local_fiq_disable();
43228 + fiq_fsm_spin_lock(&dwc_otg_hcd->fiq_state->lock);
43229 + /* Pull in from the FIQ's disabled mask */
43230 + gintmsk.d32 = gintmsk.d32 | ~(dwc_otg_hcd->fiq_state->gintmsk_saved.d32);
43231 + dwc_otg_hcd->fiq_state->gintmsk_saved.d32 = ~0;
43232 + }
43233 +
43234 + if (fiq_fsm_enable && ( 0x0000FFFF & ~(dwc_otg_hcd->fiq_state->haintmsk_saved.b2.chint))) {
43235 + gintsts.b.hcintr = 1;
43236 + }
43237 +
43238 + /* Danger will robinson: fake a SOF if necessary */
43239 + if (fiq_fsm_enable && (dwc_otg_hcd->fiq_state->gintmsk_saved.b.sofintr == 1)) {
43240 + gintsts.b.sofintr = 1;
43241 + }
43242 + gintsts.d32 &= gintmsk.d32;
43243 +
43244 + if (fiq_enable) {
43245 + fiq_fsm_spin_unlock(&dwc_otg_hcd->fiq_state->lock);
43246 + local_fiq_enable();
43247 + }
43248 +
43249 + if (!gintsts.d32) {
43250 + goto exit_handler_routine;
43251 + }
43252 +
43253 +#ifdef DEBUG
43254 + // We should be OK doing this because the common interrupts should already have been serviced
43255 + /* Don't print debug message in the interrupt handler on SOF */
43256 +#ifndef DEBUG_SOF
43257 + if (gintsts.d32 != DWC_SOF_INTR_MASK)
43258 +#endif
43259 + DWC_DEBUGPL(DBG_HCDI, "\n");
43260 +#endif
43261 +
43262 +#ifdef DEBUG
43263 +#ifndef DEBUG_SOF
43264 + if (gintsts.d32 != DWC_SOF_INTR_MASK)
43265 +#endif
43266 + DWC_DEBUGPL(DBG_HCDI,
43267 + "DWC OTG HCD Interrupt Detected gintsts&gintmsk=0x%08x core_if=%p\n",
43268 + gintsts.d32, core_if);
43269 +#endif
43270 + hfnum.d32 = DWC_READ_REG32(&dwc_otg_hcd->core_if->host_if->host_global_regs->hfnum);
43271 + if (gintsts.b.sofintr) {
43272 + retval |= dwc_otg_hcd_handle_sof_intr(dwc_otg_hcd);
43273 + }
43274 +
43275 + if (gintsts.b.rxstsqlvl) {
43276 + retval |=
43277 + dwc_otg_hcd_handle_rx_status_q_level_intr
43278 + (dwc_otg_hcd);
43279 + }
43280 + if (gintsts.b.nptxfempty) {
43281 + retval |=
43282 + dwc_otg_hcd_handle_np_tx_fifo_empty_intr
43283 + (dwc_otg_hcd);
43284 + }
43285 + if (gintsts.b.i2cintr) {
43286 + /** @todo Implement i2cintr handler. */
43287 + }
43288 + if (gintsts.b.portintr) {
43289 +
43290 + gintmsk_data_t gintmsk = { .b.portintr = 1};
43291 + retval |= dwc_otg_hcd_handle_port_intr(dwc_otg_hcd);
43292 + if (fiq_enable) {
43293 + local_fiq_disable();
43294 + fiq_fsm_spin_lock(&dwc_otg_hcd->fiq_state->lock);
43295 + DWC_MODIFY_REG32(&dwc_otg_hcd->core_if->core_global_regs->gintmsk, 0, gintmsk.d32);
43296 + fiq_fsm_spin_unlock(&dwc_otg_hcd->fiq_state->lock);
43297 + local_fiq_enable();
43298 + } else {
43299 + DWC_MODIFY_REG32(&dwc_otg_hcd->core_if->core_global_regs->gintmsk, 0, gintmsk.d32);
43300 + }
43301 + }
43302 + if (gintsts.b.hcintr) {
43303 + retval |= dwc_otg_hcd_handle_hc_intr(dwc_otg_hcd);
43304 + }
43305 + if (gintsts.b.ptxfempty) {
43306 + retval |=
43307 + dwc_otg_hcd_handle_perio_tx_fifo_empty_intr
43308 + (dwc_otg_hcd);
43309 + }
43310 +#ifdef DEBUG
43311 +#ifndef DEBUG_SOF
43312 + if (gintsts.d32 != DWC_SOF_INTR_MASK)
43313 +#endif
43314 + {
43315 + DWC_DEBUGPL(DBG_HCDI,
43316 + "DWC OTG HCD Finished Servicing Interrupts\n");
43317 + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD gintsts=0x%08x\n",
43318 + DWC_READ_REG32(&global_regs->gintsts));
43319 + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD gintmsk=0x%08x\n",
43320 + DWC_READ_REG32(&global_regs->gintmsk));
43321 + }
43322 +#endif
43323 +
43324 +#ifdef DEBUG
43325 +#ifndef DEBUG_SOF
43326 + if (gintsts.d32 != DWC_SOF_INTR_MASK)
43327 +#endif
43328 + DWC_DEBUGPL(DBG_HCDI, "\n");
43329 +#endif
43330 +
43331 + }
43332 +
43333 +exit_handler_routine:
43334 + if (fiq_enable) {
43335 + gintmsk_data_t gintmsk_new;
43336 + haintmsk_data_t haintmsk_new;
43337 + local_fiq_disable();
43338 + fiq_fsm_spin_lock(&dwc_otg_hcd->fiq_state->lock);
43339 + gintmsk_new.d32 = *(volatile uint32_t *)&dwc_otg_hcd->fiq_state->gintmsk_saved.d32;
43340 + if(fiq_fsm_enable)
43341 + haintmsk_new.d32 = *(volatile uint32_t *)&dwc_otg_hcd->fiq_state->haintmsk_saved.d32;
43342 + else
43343 + haintmsk_new.d32 = 0x0000FFFF;
43344 +
43345 + /* The FIQ could have sneaked another interrupt in. If so, don't clear MPHI */
43346 + if ((gintmsk_new.d32 == ~0) && (haintmsk_new.d32 == 0x0000FFFF)) {
43347 + DWC_WRITE_REG32(dwc_otg_hcd->fiq_state->mphi_regs.intstat, (1<<16));
43348 + if (dwc_otg_hcd->fiq_state->mphi_int_count >= 50) {
43349 + fiq_print(FIQDBG_INT, dwc_otg_hcd->fiq_state, "MPHI CLR");
43350 + DWC_WRITE_REG32(dwc_otg_hcd->fiq_state->mphi_regs.ctrl, ((1<<31) + (1<<16)));
43351 + while (!(DWC_READ_REG32(dwc_otg_hcd->fiq_state->mphi_regs.ctrl) & (1 << 17)))
43352 + ;
43353 + DWC_WRITE_REG32(dwc_otg_hcd->fiq_state->mphi_regs.ctrl, (1<<31));
43354 + dwc_otg_hcd->fiq_state->mphi_int_count = 0;
43355 + }
43356 + int_done++;
43357 + }
43358 + haintmsk.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->haintmsk);
43359 + /* Re-enable interrupts that the FIQ masked (first time round) */
43360 + FIQ_WRITE(dwc_otg_hcd->fiq_state->dwc_regs_base + GINTMSK, gintmsk.d32);
43361 + fiq_fsm_spin_unlock(&dwc_otg_hcd->fiq_state->lock);
43362 + local_fiq_enable();
43363 +
43364 + if ((jiffies / HZ) > last_time) {
43365 + //dwc_otg_qh_t *qh;
43366 + //dwc_list_link_t *cur;
43367 + /* Once a second output the fiq and irq numbers, useful for debug */
43368 + last_time = jiffies / HZ;
43369 + // DWC_WARN("np_kick=%d AHC=%d sched_frame=%d cur_frame=%d int_done=%d fiq_done=%d",
43370 + // dwc_otg_hcd->fiq_state->kick_np_queues, dwc_otg_hcd->available_host_channels,
43371 + // dwc_otg_hcd->fiq_state->next_sched_frame, hfnum.b.frnum, int_done, dwc_otg_hcd->fiq_state->fiq_done);
43372 + //printk(KERN_WARNING "Periodic queues:\n");
43373 + }
43374 + }
43375 +
43376 + DWC_SPINUNLOCK(dwc_otg_hcd->lock);
43377 + return retval;
43378 +}
43379 +
43380 +#ifdef DWC_TRACK_MISSED_SOFS
43381 +
43382 +#warning Compiling code to track missed SOFs
43383 +#define FRAME_NUM_ARRAY_SIZE 1000
43384 +/**
43385 + * This function is for debug only.
43386 + */
43387 +static inline void track_missed_sofs(uint16_t curr_frame_number)
43388 +{
43389 + static uint16_t frame_num_array[FRAME_NUM_ARRAY_SIZE];
43390 + static uint16_t last_frame_num_array[FRAME_NUM_ARRAY_SIZE];
43391 + static int frame_num_idx = 0;
43392 + static uint16_t last_frame_num = DWC_HFNUM_MAX_FRNUM;
43393 + static int dumped_frame_num_array = 0;
43394 +
43395 + if (frame_num_idx < FRAME_NUM_ARRAY_SIZE) {
43396 + if (((last_frame_num + 1) & DWC_HFNUM_MAX_FRNUM) !=
43397 + curr_frame_number) {
43398 + frame_num_array[frame_num_idx] = curr_frame_number;
43399 + last_frame_num_array[frame_num_idx++] = last_frame_num;
43400 + }
43401 + } else if (!dumped_frame_num_array) {
43402 + int i;
43403 + DWC_PRINTF("Frame Last Frame\n");
43404 + DWC_PRINTF("----- ----------\n");
43405 + for (i = 0; i < FRAME_NUM_ARRAY_SIZE; i++) {
43406 + DWC_PRINTF("0x%04x 0x%04x\n",
43407 + frame_num_array[i], last_frame_num_array[i]);
43408 + }
43409 + dumped_frame_num_array = 1;
43410 + }
43411 + last_frame_num = curr_frame_number;
43412 +}
43413 +#endif
43414 +
43415 +/**
43416 + * Handles the start-of-frame interrupt in host mode. Non-periodic
43417 + * transactions may be queued to the DWC_otg controller for the current
43418 + * (micro)frame. Periodic transactions may be queued to the controller for the
43419 + * next (micro)frame.
43420 + */
43421 +int32_t dwc_otg_hcd_handle_sof_intr(dwc_otg_hcd_t * hcd)
43422 +{
43423 + hfnum_data_t hfnum;
43424 + gintsts_data_t gintsts = { .d32 = 0 };
43425 + dwc_list_link_t *qh_entry;
43426 + dwc_otg_qh_t *qh;
43427 + dwc_otg_transaction_type_e tr_type;
43428 + int did_something = 0;
43429 + int32_t next_sched_frame = -1;
43430 +
43431 + hfnum.d32 =
43432 + DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hfnum);
43433 +
43434 +#ifdef DEBUG_SOF
43435 + DWC_DEBUGPL(DBG_HCD, "--Start of Frame Interrupt--\n");
43436 +#endif
43437 + hcd->frame_number = hfnum.b.frnum;
43438 +
43439 +#ifdef DEBUG
43440 + hcd->frrem_accum += hfnum.b.frrem;
43441 + hcd->frrem_samples++;
43442 +#endif
43443 +
43444 +#ifdef DWC_TRACK_MISSED_SOFS
43445 + track_missed_sofs(hcd->frame_number);
43446 +#endif
43447 + /* Determine whether any periodic QHs should be executed. */
43448 + qh_entry = DWC_LIST_FIRST(&hcd->periodic_sched_inactive);
43449 + while (qh_entry != &hcd->periodic_sched_inactive) {
43450 + qh = DWC_LIST_ENTRY(qh_entry, dwc_otg_qh_t, qh_list_entry);
43451 + qh_entry = qh_entry->next;
43452 + if (dwc_frame_num_le(qh->sched_frame, hcd->frame_number)) {
43453 +
43454 + /*
43455 + * Move QH to the ready list to be executed next
43456 + * (micro)frame.
43457 + */
43458 + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_ready,
43459 + &qh->qh_list_entry);
43460 +
43461 + did_something = 1;
43462 + }
43463 + else
43464 + {
43465 + if(next_sched_frame < 0 || dwc_frame_num_le(qh->sched_frame, next_sched_frame))
43466 + {
43467 + next_sched_frame = qh->sched_frame;
43468 + }
43469 + }
43470 + }
43471 + if (fiq_enable)
43472 + hcd->fiq_state->next_sched_frame = next_sched_frame;
43473 +
43474 + tr_type = dwc_otg_hcd_select_transactions(hcd);
43475 + if (tr_type != DWC_OTG_TRANSACTION_NONE) {
43476 + dwc_otg_hcd_queue_transactions(hcd, tr_type);
43477 + did_something = 1;
43478 + }
43479 +
43480 + /* Clear interrupt - but do not trample on the FIQ sof */
43481 + if (!fiq_fsm_enable) {
43482 + gintsts.b.sofintr = 1;
43483 + DWC_WRITE_REG32(&hcd->core_if->core_global_regs->gintsts, gintsts.d32);
43484 + }
43485 + return 1;
43486 +}
43487 +
43488 +/** Handles the Rx Status Queue Level Interrupt, which indicates that there is at
43489 + * least one packet in the Rx FIFO. The packets are moved from the FIFO to
43490 + * memory if the DWC_otg controller is operating in Slave mode. */
43491 +int32_t dwc_otg_hcd_handle_rx_status_q_level_intr(dwc_otg_hcd_t * dwc_otg_hcd)
43492 +{
43493 + host_grxsts_data_t grxsts;
43494 + dwc_hc_t *hc = NULL;
43495 +
43496 + DWC_DEBUGPL(DBG_HCD, "--RxStsQ Level Interrupt--\n");
43497 +
43498 + grxsts.d32 =
43499 + DWC_READ_REG32(&dwc_otg_hcd->core_if->core_global_regs->grxstsp);
43500 +
43501 + hc = dwc_otg_hcd->hc_ptr_array[grxsts.b.chnum];
43502 + if (!hc) {
43503 + DWC_ERROR("Unable to get corresponding channel\n");
43504 + return 0;
43505 + }
43506 +
43507 + /* Packet Status */
43508 + DWC_DEBUGPL(DBG_HCDV, " Ch num = %d\n", grxsts.b.chnum);
43509 + DWC_DEBUGPL(DBG_HCDV, " Count = %d\n", grxsts.b.bcnt);
43510 + DWC_DEBUGPL(DBG_HCDV, " DPID = %d, hc.dpid = %d\n", grxsts.b.dpid,
43511 + hc->data_pid_start);
43512 + DWC_DEBUGPL(DBG_HCDV, " PStatus = %d\n", grxsts.b.pktsts);
43513 +
43514 + switch (grxsts.b.pktsts) {
43515 + case DWC_GRXSTS_PKTSTS_IN:
43516 + /* Read the data into the host buffer. */
43517 + if (grxsts.b.bcnt > 0) {
43518 + dwc_otg_read_packet(dwc_otg_hcd->core_if,
43519 + hc->xfer_buff, grxsts.b.bcnt);
43520 +
43521 + /* Update the HC fields for the next packet received. */
43522 + hc->xfer_count += grxsts.b.bcnt;
43523 + hc->xfer_buff += grxsts.b.bcnt;
43524 + }
43525 +
43526 + case DWC_GRXSTS_PKTSTS_IN_XFER_COMP:
43527 + case DWC_GRXSTS_PKTSTS_DATA_TOGGLE_ERR:
43528 + case DWC_GRXSTS_PKTSTS_CH_HALTED:
43529 + /* Handled in interrupt, just ignore data */
43530 + break;
43531 + default:
43532 + DWC_ERROR("RX_STS_Q Interrupt: Unknown status %d\n",
43533 + grxsts.b.pktsts);
43534 + break;
43535 + }
43536 +
43537 + return 1;
43538 +}
43539 +
43540 +/** This interrupt occurs when the non-periodic Tx FIFO is half-empty. More
43541 + * data packets may be written to the FIFO for OUT transfers. More requests
43542 + * may be written to the non-periodic request queue for IN transfers. This
43543 + * interrupt is enabled only in Slave mode. */
43544 +int32_t dwc_otg_hcd_handle_np_tx_fifo_empty_intr(dwc_otg_hcd_t * dwc_otg_hcd)
43545 +{
43546 + DWC_DEBUGPL(DBG_HCD, "--Non-Periodic TxFIFO Empty Interrupt--\n");
43547 + dwc_otg_hcd_queue_transactions(dwc_otg_hcd,
43548 + DWC_OTG_TRANSACTION_NON_PERIODIC);
43549 + return 1;
43550 +}
43551 +
43552 +/** This interrupt occurs when the periodic Tx FIFO is half-empty. More data
43553 + * packets may be written to the FIFO for OUT transfers. More requests may be
43554 + * written to the periodic request queue for IN transfers. This interrupt is
43555 + * enabled only in Slave mode. */
43556 +int32_t dwc_otg_hcd_handle_perio_tx_fifo_empty_intr(dwc_otg_hcd_t * dwc_otg_hcd)
43557 +{
43558 + DWC_DEBUGPL(DBG_HCD, "--Periodic TxFIFO Empty Interrupt--\n");
43559 + dwc_otg_hcd_queue_transactions(dwc_otg_hcd,
43560 + DWC_OTG_TRANSACTION_PERIODIC);
43561 + return 1;
43562 +}
43563 +
43564 +/** There are multiple conditions that can cause a port interrupt. This function
43565 + * determines which interrupt conditions have occurred and handles them
43566 + * appropriately. */
43567 +int32_t dwc_otg_hcd_handle_port_intr(dwc_otg_hcd_t * dwc_otg_hcd)
43568 +{
43569 + int retval = 0;
43570 + hprt0_data_t hprt0;
43571 + hprt0_data_t hprt0_modify;
43572 +
43573 + hprt0.d32 = DWC_READ_REG32(dwc_otg_hcd->core_if->host_if->hprt0);
43574 + hprt0_modify.d32 = DWC_READ_REG32(dwc_otg_hcd->core_if->host_if->hprt0);
43575 +
43576 + /* Clear appropriate bits in HPRT0 to clear the interrupt bit in
43577 + * GINTSTS */
43578 +
43579 + hprt0_modify.b.prtena = 0;
43580 + hprt0_modify.b.prtconndet = 0;
43581 + hprt0_modify.b.prtenchng = 0;
43582 + hprt0_modify.b.prtovrcurrchng = 0;
43583 +
43584 + /* Port Connect Detected
43585 + * Set flag and clear if detected */
43586 + if (dwc_otg_hcd->core_if->hibernation_suspend == 1) {
43587 + // Dont modify port status if we are in hibernation state
43588 + hprt0_modify.b.prtconndet = 1;
43589 + hprt0_modify.b.prtenchng = 1;
43590 + DWC_WRITE_REG32(dwc_otg_hcd->core_if->host_if->hprt0, hprt0_modify.d32);
43591 + hprt0.d32 = DWC_READ_REG32(dwc_otg_hcd->core_if->host_if->hprt0);
43592 + return retval;
43593 + }
43594 +
43595 + if (hprt0.b.prtconndet) {
43596 + /** @todo - check if steps performed in 'else' block should be perfromed regardles adp */
43597 + if (dwc_otg_hcd->core_if->adp_enable &&
43598 + dwc_otg_hcd->core_if->adp.vbuson_timer_started == 1) {
43599 + DWC_PRINTF("PORT CONNECT DETECTED ----------------\n");
43600 + DWC_TIMER_CANCEL(dwc_otg_hcd->core_if->adp.vbuson_timer);
43601 + dwc_otg_hcd->core_if->adp.vbuson_timer_started = 0;
43602 + /* TODO - check if this is required, as
43603 + * host initialization was already performed
43604 + * after initial ADP probing
43605 + */
43606 + /*dwc_otg_hcd->core_if->adp.vbuson_timer_started = 0;
43607 + dwc_otg_core_init(dwc_otg_hcd->core_if);
43608 + dwc_otg_enable_global_interrupts(dwc_otg_hcd->core_if);
43609 + cil_hcd_start(dwc_otg_hcd->core_if);*/
43610 + } else {
43611 +
43612 + DWC_DEBUGPL(DBG_HCD, "--Port Interrupt HPRT0=0x%08x "
43613 + "Port Connect Detected--\n", hprt0.d32);
43614 + dwc_otg_hcd->flags.b.port_connect_status_change = 1;
43615 + dwc_otg_hcd->flags.b.port_connect_status = 1;
43616 + hprt0_modify.b.prtconndet = 1;
43617 +
43618 + /* B-Device has connected, Delete the connection timer. */
43619 + DWC_TIMER_CANCEL(dwc_otg_hcd->conn_timer);
43620 + }
43621 + /* The Hub driver asserts a reset when it sees port connect
43622 + * status change flag */
43623 + retval |= 1;
43624 + }
43625 +
43626 + /* Port Enable Changed
43627 + * Clear if detected - Set internal flag if disabled */
43628 + if (hprt0.b.prtenchng) {
43629 + DWC_DEBUGPL(DBG_HCD, " --Port Interrupt HPRT0=0x%08x "
43630 + "Port Enable Changed--\n", hprt0.d32);
43631 + hprt0_modify.b.prtenchng = 1;
43632 + if (hprt0.b.prtena == 1) {
43633 + hfir_data_t hfir;
43634 + int do_reset = 0;
43635 + dwc_otg_core_params_t *params =
43636 + dwc_otg_hcd->core_if->core_params;
43637 + dwc_otg_core_global_regs_t *global_regs =
43638 + dwc_otg_hcd->core_if->core_global_regs;
43639 + dwc_otg_host_if_t *host_if =
43640 + dwc_otg_hcd->core_if->host_if;
43641 +
43642 + dwc_otg_hcd->flags.b.port_speed = hprt0.b.prtspd;
43643 + if (microframe_schedule)
43644 + init_hcd_usecs(dwc_otg_hcd);
43645 +
43646 + /* Every time when port enables calculate
43647 + * HFIR.FrInterval
43648 + */
43649 + hfir.d32 = DWC_READ_REG32(&host_if->host_global_regs->hfir);
43650 + hfir.b.frint = calc_frame_interval(dwc_otg_hcd->core_if);
43651 + DWC_WRITE_REG32(&host_if->host_global_regs->hfir, hfir.d32);
43652 +
43653 + /* Check if we need to adjust the PHY clock speed for
43654 + * low power and adjust it */
43655 + if (params->host_support_fs_ls_low_power) {
43656 + gusbcfg_data_t usbcfg;
43657 +
43658 + usbcfg.d32 =
43659 + DWC_READ_REG32(&global_regs->gusbcfg);
43660 +
43661 + if (hprt0.b.prtspd == DWC_HPRT0_PRTSPD_LOW_SPEED
43662 + || hprt0.b.prtspd ==
43663 + DWC_HPRT0_PRTSPD_FULL_SPEED) {
43664 + /*
43665 + * Low power
43666 + */
43667 + hcfg_data_t hcfg;
43668 + if (usbcfg.b.phylpwrclksel == 0) {
43669 + /* Set PHY low power clock select for FS/LS devices */
43670 + usbcfg.b.phylpwrclksel = 1;
43671 + DWC_WRITE_REG32
43672 + (&global_regs->gusbcfg,
43673 + usbcfg.d32);
43674 + do_reset = 1;
43675 + }
43676 +
43677 + hcfg.d32 =
43678 + DWC_READ_REG32
43679 + (&host_if->host_global_regs->hcfg);
43680 +
43681 + if (hprt0.b.prtspd ==
43682 + DWC_HPRT0_PRTSPD_LOW_SPEED
43683 + && params->host_ls_low_power_phy_clk
43684 + ==
43685 + DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_6MHZ)
43686 + {
43687 + /* 6 MHZ */
43688 + DWC_DEBUGPL(DBG_CIL,
43689 + "FS_PHY programming HCFG to 6 MHz (Low Power)\n");
43690 + if (hcfg.b.fslspclksel !=
43691 + DWC_HCFG_6_MHZ) {
43692 + hcfg.b.fslspclksel =
43693 + DWC_HCFG_6_MHZ;
43694 + DWC_WRITE_REG32
43695 + (&host_if->host_global_regs->hcfg,
43696 + hcfg.d32);
43697 + do_reset = 1;
43698 + }
43699 + } else {
43700 + /* 48 MHZ */
43701 + DWC_DEBUGPL(DBG_CIL,
43702 + "FS_PHY programming HCFG to 48 MHz ()\n");
43703 + if (hcfg.b.fslspclksel !=
43704 + DWC_HCFG_48_MHZ) {
43705 + hcfg.b.fslspclksel =
43706 + DWC_HCFG_48_MHZ;
43707 + DWC_WRITE_REG32
43708 + (&host_if->host_global_regs->hcfg,
43709 + hcfg.d32);
43710 + do_reset = 1;
43711 + }
43712 + }
43713 + } else {
43714 + /*
43715 + * Not low power
43716 + */
43717 + if (usbcfg.b.phylpwrclksel == 1) {
43718 + usbcfg.b.phylpwrclksel = 0;
43719 + DWC_WRITE_REG32
43720 + (&global_regs->gusbcfg,
43721 + usbcfg.d32);
43722 + do_reset = 1;
43723 + }
43724 + }
43725 +
43726 + if (do_reset) {
43727 + DWC_TASK_SCHEDULE(dwc_otg_hcd->reset_tasklet);
43728 + }
43729 + }
43730 +
43731 + if (!do_reset) {
43732 + /* Port has been enabled set the reset change flag */
43733 + dwc_otg_hcd->flags.b.port_reset_change = 1;
43734 + }
43735 + } else {
43736 + dwc_otg_hcd->flags.b.port_enable_change = 1;
43737 + }
43738 + retval |= 1;
43739 + }
43740 +
43741 + /** Overcurrent Change Interrupt */
43742 + if (hprt0.b.prtovrcurrchng) {
43743 + DWC_DEBUGPL(DBG_HCD, " --Port Interrupt HPRT0=0x%08x "
43744 + "Port Overcurrent Changed--\n", hprt0.d32);
43745 + dwc_otg_hcd->flags.b.port_over_current_change = 1;
43746 + hprt0_modify.b.prtovrcurrchng = 1;
43747 + retval |= 1;
43748 + }
43749 +
43750 + /* Clear Port Interrupts */
43751 + DWC_WRITE_REG32(dwc_otg_hcd->core_if->host_if->hprt0, hprt0_modify.d32);
43752 +
43753 + return retval;
43754 +}
43755 +
43756 +/** This interrupt indicates that one or more host channels has a pending
43757 + * interrupt. There are multiple conditions that can cause each host channel
43758 + * interrupt. This function determines which conditions have occurred for each
43759 + * host channel interrupt and handles them appropriately. */
43760 +int32_t dwc_otg_hcd_handle_hc_intr(dwc_otg_hcd_t * dwc_otg_hcd)
43761 +{
43762 + int i;
43763 + int retval = 0;
43764 + haint_data_t haint = { .d32 = 0 } ;
43765 +
43766 + /* Clear appropriate bits in HCINTn to clear the interrupt bit in
43767 + * GINTSTS */
43768 +
43769 + if (!fiq_fsm_enable)
43770 + haint.d32 = dwc_otg_read_host_all_channels_intr(dwc_otg_hcd->core_if);
43771 +
43772 + // Overwrite with saved interrupts from fiq handler
43773 + if(fiq_fsm_enable)
43774 + {
43775 + /* check the mask? */
43776 + local_fiq_disable();
43777 + fiq_fsm_spin_lock(&dwc_otg_hcd->fiq_state->lock);
43778 + haint.b2.chint |= ~(dwc_otg_hcd->fiq_state->haintmsk_saved.b2.chint);
43779 + dwc_otg_hcd->fiq_state->haintmsk_saved.b2.chint = ~0;
43780 + fiq_fsm_spin_unlock(&dwc_otg_hcd->fiq_state->lock);
43781 + local_fiq_enable();
43782 + }
43783 +
43784 + for (i = 0; i < dwc_otg_hcd->core_if->core_params->host_channels; i++) {
43785 + if (haint.b2.chint & (1 << i)) {
43786 + retval |= dwc_otg_hcd_handle_hc_n_intr(dwc_otg_hcd, i);
43787 + }
43788 + }
43789 +
43790 + return retval;
43791 +}
43792 +
43793 +/**
43794 + * Gets the actual length of a transfer after the transfer halts. _halt_status
43795 + * holds the reason for the halt.
43796 + *
43797 + * For IN transfers where halt_status is DWC_OTG_HC_XFER_COMPLETE,
43798 + * *short_read is set to 1 upon return if less than the requested
43799 + * number of bytes were transferred. Otherwise, *short_read is set to 0 upon
43800 + * return. short_read may also be NULL on entry, in which case it remains
43801 + * unchanged.
43802 + */
43803 +static uint32_t get_actual_xfer_length(dwc_hc_t * hc,
43804 + dwc_otg_hc_regs_t * hc_regs,
43805 + dwc_otg_qtd_t * qtd,
43806 + dwc_otg_halt_status_e halt_status,
43807 + int *short_read)
43808 +{
43809 + hctsiz_data_t hctsiz;
43810 + uint32_t length;
43811 +
43812 + if (short_read != NULL) {
43813 + *short_read = 0;
43814 + }
43815 + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
43816 +
43817 + if (halt_status == DWC_OTG_HC_XFER_COMPLETE) {
43818 + if (hc->ep_is_in) {
43819 + length = hc->xfer_len - hctsiz.b.xfersize;
43820 + if (short_read != NULL) {
43821 + *short_read = (hctsiz.b.xfersize != 0);
43822 + }
43823 + } else if (hc->qh->do_split) {
43824 + //length = split_out_xfersize[hc->hc_num];
43825 + length = qtd->ssplit_out_xfer_count;
43826 + } else {
43827 + length = hc->xfer_len;
43828 + }
43829 + } else {
43830 + /*
43831 + * Must use the hctsiz.pktcnt field to determine how much data
43832 + * has been transferred. This field reflects the number of
43833 + * packets that have been transferred via the USB. This is
43834 + * always an integral number of packets if the transfer was
43835 + * halted before its normal completion. (Can't use the
43836 + * hctsiz.xfersize field because that reflects the number of
43837 + * bytes transferred via the AHB, not the USB).
43838 + */
43839 + length =
43840 + (hc->start_pkt_count - hctsiz.b.pktcnt) * hc->max_packet;
43841 + }
43842 +
43843 + return length;
43844 +}
43845 +
43846 +/**
43847 + * Updates the state of the URB after a Transfer Complete interrupt on the
43848 + * host channel. Updates the actual_length field of the URB based on the
43849 + * number of bytes transferred via the host channel. Sets the URB status
43850 + * if the data transfer is finished.
43851 + *
43852 + * @return 1 if the data transfer specified by the URB is completely finished,
43853 + * 0 otherwise.
43854 + */
43855 +static int update_urb_state_xfer_comp(dwc_hc_t * hc,
43856 + dwc_otg_hc_regs_t * hc_regs,
43857 + dwc_otg_hcd_urb_t * urb,
43858 + dwc_otg_qtd_t * qtd)
43859 +{
43860 + int xfer_done = 0;
43861 + int short_read = 0;
43862 +
43863 + int xfer_length;
43864 +
43865 + xfer_length = get_actual_xfer_length(hc, hc_regs, qtd,
43866 + DWC_OTG_HC_XFER_COMPLETE,
43867 + &short_read);
43868 +
43869 + if (urb->actual_length + xfer_length > urb->length) {
43870 + printk_once(KERN_DEBUG "dwc_otg: DEVICE:%03d : %s:%d:trimming xfer length\n",
43871 + hc->dev_addr, __func__, __LINE__);
43872 + xfer_length = urb->length - urb->actual_length;
43873 + }
43874 +
43875 + /* non DWORD-aligned buffer case handling. */
43876 + if (hc->align_buff && xfer_length && hc->ep_is_in) {
43877 + dwc_memcpy(urb->buf + urb->actual_length, hc->qh->dw_align_buf,
43878 + xfer_length);
43879 + }
43880 +
43881 + urb->actual_length += xfer_length;
43882 +
43883 + if (xfer_length && (hc->ep_type == DWC_OTG_EP_TYPE_BULK) &&
43884 + (urb->flags & URB_SEND_ZERO_PACKET)
43885 + && (urb->actual_length == urb->length)
43886 + && !(urb->length % hc->max_packet)) {
43887 + xfer_done = 0;
43888 + } else if (short_read || urb->actual_length >= urb->length) {
43889 + xfer_done = 1;
43890 + urb->status = 0;
43891 + }
43892 +
43893 +#ifdef DEBUG
43894 + {
43895 + hctsiz_data_t hctsiz;
43896 + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
43897 + DWC_DEBUGPL(DBG_HCDV, "DWC_otg: %s: %s, channel %d\n",
43898 + __func__, (hc->ep_is_in ? "IN" : "OUT"),
43899 + hc->hc_num);
43900 + DWC_DEBUGPL(DBG_HCDV, " hc->xfer_len %d\n", hc->xfer_len);
43901 + DWC_DEBUGPL(DBG_HCDV, " hctsiz.xfersize %d\n",
43902 + hctsiz.b.xfersize);
43903 + DWC_DEBUGPL(DBG_HCDV, " urb->transfer_buffer_length %d\n",
43904 + urb->length);
43905 + DWC_DEBUGPL(DBG_HCDV, " urb->actual_length %d\n",
43906 + urb->actual_length);
43907 + DWC_DEBUGPL(DBG_HCDV, " short_read %d, xfer_done %d\n",
43908 + short_read, xfer_done);
43909 + }
43910 +#endif
43911 +
43912 + return xfer_done;
43913 +}
43914 +
43915 +/*
43916 + * Save the starting data toggle for the next transfer. The data toggle is
43917 + * saved in the QH for non-control transfers and it's saved in the QTD for
43918 + * control transfers.
43919 + */
43920 +void dwc_otg_hcd_save_data_toggle(dwc_hc_t * hc,
43921 + dwc_otg_hc_regs_t * hc_regs, dwc_otg_qtd_t * qtd)
43922 +{
43923 + hctsiz_data_t hctsiz;
43924 + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
43925 +
43926 + if (hc->ep_type != DWC_OTG_EP_TYPE_CONTROL) {
43927 + dwc_otg_qh_t *qh = hc->qh;
43928 + if (hctsiz.b.pid == DWC_HCTSIZ_DATA0) {
43929 + qh->data_toggle = DWC_OTG_HC_PID_DATA0;
43930 + } else {
43931 + qh->data_toggle = DWC_OTG_HC_PID_DATA1;
43932 + }
43933 + } else {
43934 + if (hctsiz.b.pid == DWC_HCTSIZ_DATA0) {
43935 + qtd->data_toggle = DWC_OTG_HC_PID_DATA0;
43936 + } else {
43937 + qtd->data_toggle = DWC_OTG_HC_PID_DATA1;
43938 + }
43939 + }
43940 +}
43941 +
43942 +/**
43943 + * Updates the state of an Isochronous URB when the transfer is stopped for
43944 + * any reason. The fields of the current entry in the frame descriptor array
43945 + * are set based on the transfer state and the input _halt_status. Completes
43946 + * the Isochronous URB if all the URB frames have been completed.
43947 + *
43948 + * @return DWC_OTG_HC_XFER_COMPLETE if there are more frames remaining to be
43949 + * transferred in the URB. Otherwise return DWC_OTG_HC_XFER_URB_COMPLETE.
43950 + */
43951 +static dwc_otg_halt_status_e
43952 +update_isoc_urb_state(dwc_otg_hcd_t * hcd,
43953 + dwc_hc_t * hc,
43954 + dwc_otg_hc_regs_t * hc_regs,
43955 + dwc_otg_qtd_t * qtd, dwc_otg_halt_status_e halt_status)
43956 +{
43957 + dwc_otg_hcd_urb_t *urb = qtd->urb;
43958 + dwc_otg_halt_status_e ret_val = halt_status;
43959 + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
43960 +
43961 + frame_desc = &urb->iso_descs[qtd->isoc_frame_index];
43962 + switch (halt_status) {
43963 + case DWC_OTG_HC_XFER_COMPLETE:
43964 + frame_desc->status = 0;
43965 + frame_desc->actual_length =
43966 + get_actual_xfer_length(hc, hc_regs, qtd, halt_status, NULL);
43967 +
43968 + /* non DWORD-aligned buffer case handling. */
43969 + if (hc->align_buff && frame_desc->actual_length && hc->ep_is_in) {
43970 + dwc_memcpy(urb->buf + frame_desc->offset + qtd->isoc_split_offset,
43971 + hc->qh->dw_align_buf, frame_desc->actual_length);
43972 + }
43973 +
43974 + break;
43975 + case DWC_OTG_HC_XFER_FRAME_OVERRUN:
43976 + urb->error_count++;
43977 + if (hc->ep_is_in) {
43978 + frame_desc->status = -DWC_E_NO_STREAM_RES;
43979 + } else {
43980 + frame_desc->status = -DWC_E_COMMUNICATION;
43981 + }
43982 + frame_desc->actual_length = 0;
43983 + break;
43984 + case DWC_OTG_HC_XFER_BABBLE_ERR:
43985 + urb->error_count++;
43986 + frame_desc->status = -DWC_E_OVERFLOW;
43987 + /* Don't need to update actual_length in this case. */
43988 + break;
43989 + case DWC_OTG_HC_XFER_XACT_ERR:
43990 + urb->error_count++;
43991 + frame_desc->status = -DWC_E_PROTOCOL;
43992 + frame_desc->actual_length =
43993 + get_actual_xfer_length(hc, hc_regs, qtd, halt_status, NULL);
43994 +
43995 + /* non DWORD-aligned buffer case handling. */
43996 + if (hc->align_buff && frame_desc->actual_length && hc->ep_is_in) {
43997 + dwc_memcpy(urb->buf + frame_desc->offset + qtd->isoc_split_offset,
43998 + hc->qh->dw_align_buf, frame_desc->actual_length);
43999 + }
44000 + /* Skip whole frame */
44001 + if (hc->qh->do_split && (hc->ep_type == DWC_OTG_EP_TYPE_ISOC) &&
44002 + hc->ep_is_in && hcd->core_if->dma_enable) {
44003 + qtd->complete_split = 0;
44004 + qtd->isoc_split_offset = 0;
44005 + }
44006 +
44007 + break;
44008 + default:
44009 + DWC_ASSERT(1, "Unhandled _halt_status (%d)\n", halt_status);
44010 + break;
44011 + }
44012 + if (++qtd->isoc_frame_index == urb->packet_count) {
44013 + /*
44014 + * urb->status is not used for isoc transfers.
44015 + * The individual frame_desc statuses are used instead.
44016 + */
44017 + hcd->fops->complete(hcd, urb->priv, urb, 0);
44018 + ret_val = DWC_OTG_HC_XFER_URB_COMPLETE;
44019 + } else {
44020 + ret_val = DWC_OTG_HC_XFER_COMPLETE;
44021 + }
44022 + return ret_val;
44023 +}
44024 +
44025 +/**
44026 + * Frees the first QTD in the QH's list if free_qtd is 1. For non-periodic
44027 + * QHs, removes the QH from the active non-periodic schedule. If any QTDs are
44028 + * still linked to the QH, the QH is added to the end of the inactive
44029 + * non-periodic schedule. For periodic QHs, removes the QH from the periodic
44030 + * schedule if no more QTDs are linked to the QH.
44031 + */
44032 +static void deactivate_qh(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh, int free_qtd)
44033 +{
44034 + int continue_split = 0;
44035 + dwc_otg_qtd_t *qtd;
44036 +
44037 + DWC_DEBUGPL(DBG_HCDV, " %s(%p,%p,%d)\n", __func__, hcd, qh, free_qtd);
44038 +
44039 + qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
44040 +
44041 + if (qtd->complete_split) {
44042 + continue_split = 1;
44043 + } else if (qtd->isoc_split_pos == DWC_HCSPLIT_XACTPOS_MID ||
44044 + qtd->isoc_split_pos == DWC_HCSPLIT_XACTPOS_END) {
44045 + continue_split = 1;
44046 + }
44047 +
44048 + if (free_qtd) {
44049 + dwc_otg_hcd_qtd_remove_and_free(hcd, qtd, qh);
44050 + continue_split = 0;
44051 + }
44052 +
44053 + qh->channel = NULL;
44054 + dwc_otg_hcd_qh_deactivate(hcd, qh, continue_split);
44055 +}
44056 +
44057 +/**
44058 + * Releases a host channel for use by other transfers. Attempts to select and
44059 + * queue more transactions since at least one host channel is available.
44060 + *
44061 + * @param hcd The HCD state structure.
44062 + * @param hc The host channel to release.
44063 + * @param qtd The QTD associated with the host channel. This QTD may be freed
44064 + * if the transfer is complete or an error has occurred.
44065 + * @param halt_status Reason the channel is being released. This status
44066 + * determines the actions taken by this function.
44067 + */
44068 +static void release_channel(dwc_otg_hcd_t * hcd,
44069 + dwc_hc_t * hc,
44070 + dwc_otg_qtd_t * qtd,
44071 + dwc_otg_halt_status_e halt_status)
44072 +{
44073 + dwc_otg_transaction_type_e tr_type;
44074 + int free_qtd;
44075 +
44076 + int hog_port = 0;
44077 +
44078 + DWC_DEBUGPL(DBG_HCDV, " %s: channel %d, halt_status %d, xfer_len %d\n",
44079 + __func__, hc->hc_num, halt_status, hc->xfer_len);
44080 +
44081 + if(fiq_fsm_enable && hc->do_split) {
44082 + if(!hc->ep_is_in && hc->ep_type == UE_ISOCHRONOUS) {
44083 + if(hc->xact_pos == DWC_HCSPLIT_XACTPOS_MID ||
44084 + hc->xact_pos == DWC_HCSPLIT_XACTPOS_BEGIN) {
44085 + hog_port = 0;
44086 + }
44087 + }
44088 + }
44089 +
44090 + switch (halt_status) {
44091 + case DWC_OTG_HC_XFER_URB_COMPLETE:
44092 + free_qtd = 1;
44093 + break;
44094 + case DWC_OTG_HC_XFER_AHB_ERR:
44095 + case DWC_OTG_HC_XFER_STALL:
44096 + case DWC_OTG_HC_XFER_BABBLE_ERR:
44097 + free_qtd = 1;
44098 + break;
44099 + case DWC_OTG_HC_XFER_XACT_ERR:
44100 + if (qtd->error_count >= 3) {
44101 + DWC_DEBUGPL(DBG_HCDV,
44102 + " Complete URB with transaction error\n");
44103 + free_qtd = 1;
44104 + qtd->urb->status = -DWC_E_PROTOCOL;
44105 + hcd->fops->complete(hcd, qtd->urb->priv,
44106 + qtd->urb, -DWC_E_PROTOCOL);
44107 + } else {
44108 + free_qtd = 0;
44109 + }
44110 + break;
44111 + case DWC_OTG_HC_XFER_URB_DEQUEUE:
44112 + /*
44113 + * The QTD has already been removed and the QH has been
44114 + * deactivated. Don't want to do anything except release the
44115 + * host channel and try to queue more transfers.
44116 + */
44117 + goto cleanup;
44118 + case DWC_OTG_HC_XFER_NO_HALT_STATUS:
44119 + free_qtd = 0;
44120 + break;
44121 + case DWC_OTG_HC_XFER_PERIODIC_INCOMPLETE:
44122 + DWC_DEBUGPL(DBG_HCDV,
44123 + " Complete URB with I/O error\n");
44124 + free_qtd = 1;
44125 + qtd->urb->status = -DWC_E_IO;
44126 + hcd->fops->complete(hcd, qtd->urb->priv,
44127 + qtd->urb, -DWC_E_IO);
44128 + break;
44129 + default:
44130 + free_qtd = 0;
44131 + break;
44132 + }
44133 +
44134 + deactivate_qh(hcd, hc->qh, free_qtd);
44135 +
44136 +cleanup:
44137 + /*
44138 + * Release the host channel for use by other transfers. The cleanup
44139 + * function clears the channel interrupt enables and conditions, so
44140 + * there's no need to clear the Channel Halted interrupt separately.
44141 + */
44142 + if (fiq_fsm_enable && hcd->fiq_state->channel[hc->hc_num].fsm != FIQ_PASSTHROUGH)
44143 + dwc_otg_cleanup_fiq_channel(hcd, hc->hc_num);
44144 + dwc_otg_hc_cleanup(hcd->core_if, hc);
44145 + DWC_CIRCLEQ_INSERT_TAIL(&hcd->free_hc_list, hc, hc_list_entry);
44146 +
44147 + if (!microframe_schedule) {
44148 + switch (hc->ep_type) {
44149 + case DWC_OTG_EP_TYPE_CONTROL:
44150 + case DWC_OTG_EP_TYPE_BULK:
44151 + hcd->non_periodic_channels--;
44152 + break;
44153 +
44154 + default:
44155 + /*
44156 + * Don't release reservations for periodic channels here.
44157 + * That's done when a periodic transfer is descheduled (i.e.
44158 + * when the QH is removed from the periodic schedule).
44159 + */
44160 + break;
44161 + }
44162 + } else {
44163 + hcd->available_host_channels++;
44164 + fiq_print(FIQDBG_INT, hcd->fiq_state, "AHC = %d ", hcd->available_host_channels);
44165 + }
44166 +
44167 + /* Try to queue more transfers now that there's a free channel. */
44168 + tr_type = dwc_otg_hcd_select_transactions(hcd);
44169 + if (tr_type != DWC_OTG_TRANSACTION_NONE) {
44170 + dwc_otg_hcd_queue_transactions(hcd, tr_type);
44171 + }
44172 +}
44173 +
44174 +/**
44175 + * Halts a host channel. If the channel cannot be halted immediately because
44176 + * the request queue is full, this function ensures that the FIFO empty
44177 + * interrupt for the appropriate queue is enabled so that the halt request can
44178 + * be queued when there is space in the request queue.
44179 + *
44180 + * This function may also be called in DMA mode. In that case, the channel is
44181 + * simply released since the core always halts the channel automatically in
44182 + * DMA mode.
44183 + */
44184 +static void halt_channel(dwc_otg_hcd_t * hcd,
44185 + dwc_hc_t * hc,
44186 + dwc_otg_qtd_t * qtd, dwc_otg_halt_status_e halt_status)
44187 +{
44188 + if (hcd->core_if->dma_enable) {
44189 + release_channel(hcd, hc, qtd, halt_status);
44190 + return;
44191 + }
44192 +
44193 + /* Slave mode processing... */
44194 + dwc_otg_hc_halt(hcd->core_if, hc, halt_status);
44195 +
44196 + if (hc->halt_on_queue) {
44197 + gintmsk_data_t gintmsk = {.d32 = 0 };
44198 + dwc_otg_core_global_regs_t *global_regs;
44199 + global_regs = hcd->core_if->core_global_regs;
44200 +
44201 + if (hc->ep_type == DWC_OTG_EP_TYPE_CONTROL ||
44202 + hc->ep_type == DWC_OTG_EP_TYPE_BULK) {
44203 + /*
44204 + * Make sure the Non-periodic Tx FIFO empty interrupt
44205 + * is enabled so that the non-periodic schedule will
44206 + * be processed.
44207 + */
44208 + gintmsk.b.nptxfempty = 1;
44209 + if (fiq_enable) {
44210 + local_fiq_disable();
44211 + fiq_fsm_spin_lock(&hcd->fiq_state->lock);
44212 + DWC_MODIFY_REG32(&global_regs->gintmsk, 0, gintmsk.d32);
44213 + fiq_fsm_spin_unlock(&hcd->fiq_state->lock);
44214 + local_fiq_enable();
44215 + } else {
44216 + DWC_MODIFY_REG32(&global_regs->gintmsk, 0, gintmsk.d32);
44217 + }
44218 + } else {
44219 + /*
44220 + * Move the QH from the periodic queued schedule to
44221 + * the periodic assigned schedule. This allows the
44222 + * halt to be queued when the periodic schedule is
44223 + * processed.
44224 + */
44225 + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_assigned,
44226 + &hc->qh->qh_list_entry);
44227 +
44228 + /*
44229 + * Make sure the Periodic Tx FIFO Empty interrupt is
44230 + * enabled so that the periodic schedule will be
44231 + * processed.
44232 + */
44233 + gintmsk.b.ptxfempty = 1;
44234 + if (fiq_enable) {
44235 + local_fiq_disable();
44236 + fiq_fsm_spin_lock(&hcd->fiq_state->lock);
44237 + DWC_MODIFY_REG32(&global_regs->gintmsk, 0, gintmsk.d32);
44238 + fiq_fsm_spin_unlock(&hcd->fiq_state->lock);
44239 + local_fiq_enable();
44240 + } else {
44241 + DWC_MODIFY_REG32(&global_regs->gintmsk, 0, gintmsk.d32);
44242 + }
44243 + }
44244 + }
44245 +}
44246 +
44247 +/**
44248 + * Performs common cleanup for non-periodic transfers after a Transfer
44249 + * Complete interrupt. This function should be called after any endpoint type
44250 + * specific handling is finished to release the host channel.
44251 + */
44252 +static void complete_non_periodic_xfer(dwc_otg_hcd_t * hcd,
44253 + dwc_hc_t * hc,
44254 + dwc_otg_hc_regs_t * hc_regs,
44255 + dwc_otg_qtd_t * qtd,
44256 + dwc_otg_halt_status_e halt_status)
44257 +{
44258 + hcint_data_t hcint;
44259 +
44260 + qtd->error_count = 0;
44261 +
44262 + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
44263 + if (hcint.b.nyet) {
44264 + /*
44265 + * Got a NYET on the last transaction of the transfer. This
44266 + * means that the endpoint should be in the PING state at the
44267 + * beginning of the next transfer.
44268 + */
44269 + hc->qh->ping_state = 1;
44270 + clear_hc_int(hc_regs, nyet);
44271 + }
44272 +
44273 + /*
44274 + * Always halt and release the host channel to make it available for
44275 + * more transfers. There may still be more phases for a control
44276 + * transfer or more data packets for a bulk transfer at this point,
44277 + * but the host channel is still halted. A channel will be reassigned
44278 + * to the transfer when the non-periodic schedule is processed after
44279 + * the channel is released. This allows transactions to be queued
44280 + * properly via dwc_otg_hcd_queue_transactions, which also enables the
44281 + * Tx FIFO Empty interrupt if necessary.
44282 + */
44283 + if (hc->ep_is_in) {
44284 + /*
44285 + * IN transfers in Slave mode require an explicit disable to
44286 + * halt the channel. (In DMA mode, this call simply releases
44287 + * the channel.)
44288 + */
44289 + halt_channel(hcd, hc, qtd, halt_status);
44290 + } else {
44291 + /*
44292 + * The channel is automatically disabled by the core for OUT
44293 + * transfers in Slave mode.
44294 + */
44295 + release_channel(hcd, hc, qtd, halt_status);
44296 + }
44297 +}
44298 +
44299 +/**
44300 + * Performs common cleanup for periodic transfers after a Transfer Complete
44301 + * interrupt. This function should be called after any endpoint type specific
44302 + * handling is finished to release the host channel.
44303 + */
44304 +static void complete_periodic_xfer(dwc_otg_hcd_t * hcd,
44305 + dwc_hc_t * hc,
44306 + dwc_otg_hc_regs_t * hc_regs,
44307 + dwc_otg_qtd_t * qtd,
44308 + dwc_otg_halt_status_e halt_status)
44309 +{
44310 + hctsiz_data_t hctsiz;
44311 + qtd->error_count = 0;
44312 +
44313 + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
44314 + if (!hc->ep_is_in || hctsiz.b.pktcnt == 0) {
44315 + /* Core halts channel in these cases. */
44316 + release_channel(hcd, hc, qtd, halt_status);
44317 + } else {
44318 + /* Flush any outstanding requests from the Tx queue. */
44319 + halt_channel(hcd, hc, qtd, halt_status);
44320 + }
44321 +}
44322 +
44323 +static int32_t handle_xfercomp_isoc_split_in(dwc_otg_hcd_t * hcd,
44324 + dwc_hc_t * hc,
44325 + dwc_otg_hc_regs_t * hc_regs,
44326 + dwc_otg_qtd_t * qtd)
44327 +{
44328 + uint32_t len;
44329 + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
44330 + frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
44331 +
44332 + len = get_actual_xfer_length(hc, hc_regs, qtd,
44333 + DWC_OTG_HC_XFER_COMPLETE, NULL);
44334 +
44335 + if (!len) {
44336 + qtd->complete_split = 0;
44337 + qtd->isoc_split_offset = 0;
44338 + return 0;
44339 + }
44340 + frame_desc->actual_length += len;
44341 +
44342 + if (hc->align_buff && len)
44343 + dwc_memcpy(qtd->urb->buf + frame_desc->offset +
44344 + qtd->isoc_split_offset, hc->qh->dw_align_buf, len);
44345 + qtd->isoc_split_offset += len;
44346 +
44347 + if (frame_desc->length == frame_desc->actual_length) {
44348 + frame_desc->status = 0;
44349 + qtd->isoc_frame_index++;
44350 + qtd->complete_split = 0;
44351 + qtd->isoc_split_offset = 0;
44352 + }
44353 +
44354 + if (qtd->isoc_frame_index == qtd->urb->packet_count) {
44355 + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
44356 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
44357 + } else {
44358 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
44359 + }
44360 +
44361 + return 1; /* Indicates that channel released */
44362 +}
44363 +
44364 +/**
44365 + * Handles a host channel Transfer Complete interrupt. This handler may be
44366 + * called in either DMA mode or Slave mode.
44367 + */
44368 +static int32_t handle_hc_xfercomp_intr(dwc_otg_hcd_t * hcd,
44369 + dwc_hc_t * hc,
44370 + dwc_otg_hc_regs_t * hc_regs,
44371 + dwc_otg_qtd_t * qtd)
44372 +{
44373 + int urb_xfer_done;
44374 + dwc_otg_halt_status_e halt_status = DWC_OTG_HC_XFER_COMPLETE;
44375 + dwc_otg_hcd_urb_t *urb = qtd->urb;
44376 + int pipe_type = dwc_otg_hcd_get_pipe_type(&urb->pipe_info);
44377 +
44378 + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
44379 + "Transfer Complete--\n", hc->hc_num);
44380 +
44381 + if (hcd->core_if->dma_desc_enable) {
44382 + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs, halt_status);
44383 + if (pipe_type == UE_ISOCHRONOUS) {
44384 + /* Do not disable the interrupt, just clear it */
44385 + clear_hc_int(hc_regs, xfercomp);
44386 + return 1;
44387 + }
44388 + goto handle_xfercomp_done;
44389 + }
44390 +
44391 + /*
44392 + * Handle xfer complete on CSPLIT.
44393 + */
44394 +
44395 + if (hc->qh->do_split) {
44396 + if ((hc->ep_type == DWC_OTG_EP_TYPE_ISOC) && hc->ep_is_in
44397 + && hcd->core_if->dma_enable) {
44398 + if (qtd->complete_split
44399 + && handle_xfercomp_isoc_split_in(hcd, hc, hc_regs,
44400 + qtd))
44401 + goto handle_xfercomp_done;
44402 + } else {
44403 + qtd->complete_split = 0;
44404 + }
44405 + }
44406 +
44407 + /* Update the QTD and URB states. */
44408 + switch (pipe_type) {
44409 + case UE_CONTROL:
44410 + switch (qtd->control_phase) {
44411 + case DWC_OTG_CONTROL_SETUP:
44412 + if (urb->length > 0) {
44413 + qtd->control_phase = DWC_OTG_CONTROL_DATA;
44414 + } else {
44415 + qtd->control_phase = DWC_OTG_CONTROL_STATUS;
44416 + }
44417 + DWC_DEBUGPL(DBG_HCDV,
44418 + " Control setup transaction done\n");
44419 + halt_status = DWC_OTG_HC_XFER_COMPLETE;
44420 + break;
44421 + case DWC_OTG_CONTROL_DATA:{
44422 + urb_xfer_done =
44423 + update_urb_state_xfer_comp(hc, hc_regs, urb,
44424 + qtd);
44425 + if (urb_xfer_done) {
44426 + qtd->control_phase =
44427 + DWC_OTG_CONTROL_STATUS;
44428 + DWC_DEBUGPL(DBG_HCDV,
44429 + " Control data transfer done\n");
44430 + } else {
44431 + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
44432 + }
44433 + halt_status = DWC_OTG_HC_XFER_COMPLETE;
44434 + break;
44435 + }
44436 + case DWC_OTG_CONTROL_STATUS:
44437 + DWC_DEBUGPL(DBG_HCDV, " Control transfer complete\n");
44438 + if (urb->status == -DWC_E_IN_PROGRESS) {
44439 + urb->status = 0;
44440 + }
44441 + hcd->fops->complete(hcd, urb->priv, urb, urb->status);
44442 + halt_status = DWC_OTG_HC_XFER_URB_COMPLETE;
44443 + break;
44444 + }
44445 +
44446 + complete_non_periodic_xfer(hcd, hc, hc_regs, qtd, halt_status);
44447 + break;
44448 + case UE_BULK:
44449 + DWC_DEBUGPL(DBG_HCDV, " Bulk transfer complete\n");
44450 + urb_xfer_done =
44451 + update_urb_state_xfer_comp(hc, hc_regs, urb, qtd);
44452 + if (urb_xfer_done) {
44453 + hcd->fops->complete(hcd, urb->priv, urb, urb->status);
44454 + halt_status = DWC_OTG_HC_XFER_URB_COMPLETE;
44455 + } else {
44456 + halt_status = DWC_OTG_HC_XFER_COMPLETE;
44457 + }
44458 +
44459 + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
44460 + complete_non_periodic_xfer(hcd, hc, hc_regs, qtd, halt_status);
44461 + break;
44462 + case UE_INTERRUPT:
44463 + DWC_DEBUGPL(DBG_HCDV, " Interrupt transfer complete\n");
44464 + urb_xfer_done =
44465 + update_urb_state_xfer_comp(hc, hc_regs, urb, qtd);
44466 +
44467 + /*
44468 + * Interrupt URB is done on the first transfer complete
44469 + * interrupt.
44470 + */
44471 + if (urb_xfer_done) {
44472 + hcd->fops->complete(hcd, urb->priv, urb, urb->status);
44473 + halt_status = DWC_OTG_HC_XFER_URB_COMPLETE;
44474 + } else {
44475 + halt_status = DWC_OTG_HC_XFER_COMPLETE;
44476 + }
44477 +
44478 + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
44479 + complete_periodic_xfer(hcd, hc, hc_regs, qtd, halt_status);
44480 + break;
44481 + case UE_ISOCHRONOUS:
44482 + DWC_DEBUGPL(DBG_HCDV, " Isochronous transfer complete\n");
44483 + if (qtd->isoc_split_pos == DWC_HCSPLIT_XACTPOS_ALL) {
44484 + halt_status =
44485 + update_isoc_urb_state(hcd, hc, hc_regs, qtd,
44486 + DWC_OTG_HC_XFER_COMPLETE);
44487 + }
44488 + complete_periodic_xfer(hcd, hc, hc_regs, qtd, halt_status);
44489 + break;
44490 + }
44491 +
44492 +handle_xfercomp_done:
44493 + disable_hc_int(hc_regs, xfercompl);
44494 +
44495 + return 1;
44496 +}
44497 +
44498 +/**
44499 + * Handles a host channel STALL interrupt. This handler may be called in
44500 + * either DMA mode or Slave mode.
44501 + */
44502 +static int32_t handle_hc_stall_intr(dwc_otg_hcd_t * hcd,
44503 + dwc_hc_t * hc,
44504 + dwc_otg_hc_regs_t * hc_regs,
44505 + dwc_otg_qtd_t * qtd)
44506 +{
44507 + dwc_otg_hcd_urb_t *urb = qtd->urb;
44508 + int pipe_type = dwc_otg_hcd_get_pipe_type(&urb->pipe_info);
44509 +
44510 + DWC_DEBUGPL(DBG_HCD, "--Host Channel %d Interrupt: "
44511 + "STALL Received--\n", hc->hc_num);
44512 +
44513 + if (hcd->core_if->dma_desc_enable) {
44514 + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs, DWC_OTG_HC_XFER_STALL);
44515 + goto handle_stall_done;
44516 + }
44517 +
44518 + if (pipe_type == UE_CONTROL) {
44519 + hcd->fops->complete(hcd, urb->priv, urb, -DWC_E_PIPE);
44520 + }
44521 +
44522 + if (pipe_type == UE_BULK || pipe_type == UE_INTERRUPT) {
44523 + hcd->fops->complete(hcd, urb->priv, urb, -DWC_E_PIPE);
44524 + /*
44525 + * USB protocol requires resetting the data toggle for bulk
44526 + * and interrupt endpoints when a CLEAR_FEATURE(ENDPOINT_HALT)
44527 + * setup command is issued to the endpoint. Anticipate the
44528 + * CLEAR_FEATURE command since a STALL has occurred and reset
44529 + * the data toggle now.
44530 + */
44531 + hc->qh->data_toggle = 0;
44532 + }
44533 +
44534 + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_STALL);
44535 +
44536 +handle_stall_done:
44537 + disable_hc_int(hc_regs, stall);
44538 +
44539 + return 1;
44540 +}
44541 +
44542 +/*
44543 + * Updates the state of the URB when a transfer has been stopped due to an
44544 + * abnormal condition before the transfer completes. Modifies the
44545 + * actual_length field of the URB to reflect the number of bytes that have
44546 + * actually been transferred via the host channel.
44547 + */
44548 +static void update_urb_state_xfer_intr(dwc_hc_t * hc,
44549 + dwc_otg_hc_regs_t * hc_regs,
44550 + dwc_otg_hcd_urb_t * urb,
44551 + dwc_otg_qtd_t * qtd,
44552 + dwc_otg_halt_status_e halt_status)
44553 +{
44554 + uint32_t bytes_transferred = get_actual_xfer_length(hc, hc_regs, qtd,
44555 + halt_status, NULL);
44556 +
44557 + if (urb->actual_length + bytes_transferred > urb->length) {
44558 + printk_once(KERN_DEBUG "dwc_otg: DEVICE:%03d : %s:%d:trimming xfer length\n",
44559 + hc->dev_addr, __func__, __LINE__);
44560 + bytes_transferred = urb->length - urb->actual_length;
44561 + }
44562 +
44563 + /* non DWORD-aligned buffer case handling. */
44564 + if (hc->align_buff && bytes_transferred && hc->ep_is_in) {
44565 + dwc_memcpy(urb->buf + urb->actual_length, hc->qh->dw_align_buf,
44566 + bytes_transferred);
44567 + }
44568 +
44569 + urb->actual_length += bytes_transferred;
44570 +
44571 +#ifdef DEBUG
44572 + {
44573 + hctsiz_data_t hctsiz;
44574 + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
44575 + DWC_DEBUGPL(DBG_HCDV, "DWC_otg: %s: %s, channel %d\n",
44576 + __func__, (hc->ep_is_in ? "IN" : "OUT"),
44577 + hc->hc_num);
44578 + DWC_DEBUGPL(DBG_HCDV, " hc->start_pkt_count %d\n",
44579 + hc->start_pkt_count);
44580 + DWC_DEBUGPL(DBG_HCDV, " hctsiz.pktcnt %d\n", hctsiz.b.pktcnt);
44581 + DWC_DEBUGPL(DBG_HCDV, " hc->max_packet %d\n", hc->max_packet);
44582 + DWC_DEBUGPL(DBG_HCDV, " bytes_transferred %d\n",
44583 + bytes_transferred);
44584 + DWC_DEBUGPL(DBG_HCDV, " urb->actual_length %d\n",
44585 + urb->actual_length);
44586 + DWC_DEBUGPL(DBG_HCDV, " urb->transfer_buffer_length %d\n",
44587 + urb->length);
44588 + }
44589 +#endif
44590 +}
44591 +
44592 +/**
44593 + * Handles a host channel NAK interrupt. This handler may be called in either
44594 + * DMA mode or Slave mode.
44595 + */
44596 +static int32_t handle_hc_nak_intr(dwc_otg_hcd_t * hcd,
44597 + dwc_hc_t * hc,
44598 + dwc_otg_hc_regs_t * hc_regs,
44599 + dwc_otg_qtd_t * qtd)
44600 +{
44601 + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
44602 + "NAK Received--\n", hc->hc_num);
44603 +
44604 + /*
44605 + * When we get bulk NAKs then remember this so we holdoff on this qh until
44606 + * the beginning of the next frame
44607 + */
44608 + switch(dwc_otg_hcd_get_pipe_type(&qtd->urb->pipe_info)) {
44609 + case UE_BULK:
44610 + case UE_CONTROL:
44611 + if (nak_holdoff && qtd->qh->do_split)
44612 + hc->qh->nak_frame = dwc_otg_hcd_get_frame_number(hcd);
44613 + }
44614 +
44615 + /*
44616 + * Handle NAK for IN/OUT SSPLIT/CSPLIT transfers, bulk, control, and
44617 + * interrupt. Re-start the SSPLIT transfer.
44618 + */
44619 + if (hc->do_split) {
44620 + if (hc->complete_split) {
44621 + qtd->error_count = 0;
44622 + }
44623 + qtd->complete_split = 0;
44624 + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NAK);
44625 + goto handle_nak_done;
44626 + }
44627 +
44628 + switch (dwc_otg_hcd_get_pipe_type(&qtd->urb->pipe_info)) {
44629 + case UE_CONTROL:
44630 + case UE_BULK:
44631 + if (hcd->core_if->dma_enable && hc->ep_is_in) {
44632 + /*
44633 + * NAK interrupts are enabled on bulk/control IN
44634 + * transfers in DMA mode for the sole purpose of
44635 + * resetting the error count after a transaction error
44636 + * occurs. The core will continue transferring data.
44637 + * Disable other interrupts unmasked for the same
44638 + * reason.
44639 + */
44640 + disable_hc_int(hc_regs, datatglerr);
44641 + disable_hc_int(hc_regs, ack);
44642 + qtd->error_count = 0;
44643 + goto handle_nak_done;
44644 + }
44645 +
44646 + /*
44647 + * NAK interrupts normally occur during OUT transfers in DMA
44648 + * or Slave mode. For IN transfers, more requests will be
44649 + * queued as request queue space is available.
44650 + */
44651 + qtd->error_count = 0;
44652 +
44653 + if (!hc->qh->ping_state) {
44654 + update_urb_state_xfer_intr(hc, hc_regs,
44655 + qtd->urb, qtd,
44656 + DWC_OTG_HC_XFER_NAK);
44657 + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
44658 +
44659 + if (hc->speed == DWC_OTG_EP_SPEED_HIGH)
44660 + hc->qh->ping_state = 1;
44661 + }
44662 +
44663 + /*
44664 + * Halt the channel so the transfer can be re-started from
44665 + * the appropriate point or the PING protocol will
44666 + * start/continue.
44667 + */
44668 + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NAK);
44669 + break;
44670 + case UE_INTERRUPT:
44671 + qtd->error_count = 0;
44672 + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NAK);
44673 + break;
44674 + case UE_ISOCHRONOUS:
44675 + /* Should never get called for isochronous transfers. */
44676 + DWC_ASSERT(1, "NACK interrupt for ISOC transfer\n");
44677 + break;
44678 + }
44679 +
44680 +handle_nak_done:
44681 + disable_hc_int(hc_regs, nak);
44682 +
44683 + return 1;
44684 +}
44685 +
44686 +/**
44687 + * Handles a host channel ACK interrupt. This interrupt is enabled when
44688 + * performing the PING protocol in Slave mode, when errors occur during
44689 + * either Slave mode or DMA mode, and during Start Split transactions.
44690 + */
44691 +static int32_t handle_hc_ack_intr(dwc_otg_hcd_t * hcd,
44692 + dwc_hc_t * hc,
44693 + dwc_otg_hc_regs_t * hc_regs,
44694 + dwc_otg_qtd_t * qtd)
44695 +{
44696 + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
44697 + "ACK Received--\n", hc->hc_num);
44698 +
44699 + if (hc->do_split) {
44700 + /*
44701 + * Handle ACK on SSPLIT.
44702 + * ACK should not occur in CSPLIT.
44703 + */
44704 + if (!hc->ep_is_in && hc->data_pid_start != DWC_OTG_HC_PID_SETUP) {
44705 + qtd->ssplit_out_xfer_count = hc->xfer_len;
44706 + }
44707 + if (!(hc->ep_type == DWC_OTG_EP_TYPE_ISOC && !hc->ep_is_in)) {
44708 + /* Don't need complete for isochronous out transfers. */
44709 + qtd->complete_split = 1;
44710 + }
44711 +
44712 + /* ISOC OUT */
44713 + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC && !hc->ep_is_in) {
44714 + switch (hc->xact_pos) {
44715 + case DWC_HCSPLIT_XACTPOS_ALL:
44716 + break;
44717 + case DWC_HCSPLIT_XACTPOS_END:
44718 + qtd->isoc_split_pos = DWC_HCSPLIT_XACTPOS_ALL;
44719 + qtd->isoc_split_offset = 0;
44720 + break;
44721 + case DWC_HCSPLIT_XACTPOS_BEGIN:
44722 + case DWC_HCSPLIT_XACTPOS_MID:
44723 + /*
44724 + * For BEGIN or MID, calculate the length for
44725 + * the next microframe to determine the correct
44726 + * SSPLIT token, either MID or END.
44727 + */
44728 + {
44729 + struct dwc_otg_hcd_iso_packet_desc
44730 + *frame_desc;
44731 +
44732 + frame_desc =
44733 + &qtd->urb->
44734 + iso_descs[qtd->isoc_frame_index];
44735 + qtd->isoc_split_offset += 188;
44736 +
44737 + if ((frame_desc->length -
44738 + qtd->isoc_split_offset) <= 188) {
44739 + qtd->isoc_split_pos =
44740 + DWC_HCSPLIT_XACTPOS_END;
44741 + } else {
44742 + qtd->isoc_split_pos =
44743 + DWC_HCSPLIT_XACTPOS_MID;
44744 + }
44745 +
44746 + }
44747 + break;
44748 + }
44749 + } else {
44750 + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_ACK);
44751 + }
44752 + } else {
44753 + /*
44754 + * An unmasked ACK on a non-split DMA transaction is
44755 + * for the sole purpose of resetting error counts. Disable other
44756 + * interrupts unmasked for the same reason.
44757 + */
44758 + if(hcd->core_if->dma_enable) {
44759 + disable_hc_int(hc_regs, datatglerr);
44760 + disable_hc_int(hc_regs, nak);
44761 + }
44762 + qtd->error_count = 0;
44763 +
44764 + if (hc->qh->ping_state) {
44765 + hc->qh->ping_state = 0;
44766 + /*
44767 + * Halt the channel so the transfer can be re-started
44768 + * from the appropriate point. This only happens in
44769 + * Slave mode. In DMA mode, the ping_state is cleared
44770 + * when the transfer is started because the core
44771 + * automatically executes the PING, then the transfer.
44772 + */
44773 + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_ACK);
44774 + }
44775 + }
44776 +
44777 + /*
44778 + * If the ACK occurred when _not_ in the PING state, let the channel
44779 + * continue transferring data after clearing the error count.
44780 + */
44781 +
44782 + disable_hc_int(hc_regs, ack);
44783 +
44784 + return 1;
44785 +}
44786 +
44787 +/**
44788 + * Handles a host channel NYET interrupt. This interrupt should only occur on
44789 + * Bulk and Control OUT endpoints and for complete split transactions. If a
44790 + * NYET occurs at the same time as a Transfer Complete interrupt, it is
44791 + * handled in the xfercomp interrupt handler, not here. This handler may be
44792 + * called in either DMA mode or Slave mode.
44793 + */
44794 +static int32_t handle_hc_nyet_intr(dwc_otg_hcd_t * hcd,
44795 + dwc_hc_t * hc,
44796 + dwc_otg_hc_regs_t * hc_regs,
44797 + dwc_otg_qtd_t * qtd)
44798 +{
44799 + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
44800 + "NYET Received--\n", hc->hc_num);
44801 +
44802 + /*
44803 + * NYET on CSPLIT
44804 + * re-do the CSPLIT immediately on non-periodic
44805 + */
44806 + if (hc->do_split && hc->complete_split) {
44807 + if (hc->ep_is_in && (hc->ep_type == DWC_OTG_EP_TYPE_ISOC)
44808 + && hcd->core_if->dma_enable) {
44809 + qtd->complete_split = 0;
44810 + qtd->isoc_split_offset = 0;
44811 + if (++qtd->isoc_frame_index == qtd->urb->packet_count) {
44812 + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
44813 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
44814 + }
44815 + else
44816 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
44817 + goto handle_nyet_done;
44818 + }
44819 +
44820 + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
44821 + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
44822 + int frnum = dwc_otg_hcd_get_frame_number(hcd);
44823 +
44824 + // With the FIQ running we only ever see the failed NYET
44825 + if (dwc_full_frame_num(frnum) !=
44826 + dwc_full_frame_num(hc->qh->sched_frame) ||
44827 + fiq_fsm_enable) {
44828 + /*
44829 + * No longer in the same full speed frame.
44830 + * Treat this as a transaction error.
44831 + */
44832 +#if 0
44833 + /** @todo Fix system performance so this can
44834 + * be treated as an error. Right now complete
44835 + * splits cannot be scheduled precisely enough
44836 + * due to other system activity, so this error
44837 + * occurs regularly in Slave mode.
44838 + */
44839 + qtd->error_count++;
44840 +#endif
44841 + qtd->complete_split = 0;
44842 + halt_channel(hcd, hc, qtd,
44843 + DWC_OTG_HC_XFER_XACT_ERR);
44844 + /** @todo add support for isoc release */
44845 + goto handle_nyet_done;
44846 + }
44847 + }
44848 +
44849 + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NYET);
44850 + goto handle_nyet_done;
44851 + }
44852 +
44853 + hc->qh->ping_state = 1;
44854 + qtd->error_count = 0;
44855 +
44856 + update_urb_state_xfer_intr(hc, hc_regs, qtd->urb, qtd,
44857 + DWC_OTG_HC_XFER_NYET);
44858 + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
44859 +
44860 + /*
44861 + * Halt the channel and re-start the transfer so the PING
44862 + * protocol will start.
44863 + */
44864 + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NYET);
44865 +
44866 +handle_nyet_done:
44867 + disable_hc_int(hc_regs, nyet);
44868 + return 1;
44869 +}
44870 +
44871 +/**
44872 + * Handles a host channel babble interrupt. This handler may be called in
44873 + * either DMA mode or Slave mode.
44874 + */
44875 +static int32_t handle_hc_babble_intr(dwc_otg_hcd_t * hcd,
44876 + dwc_hc_t * hc,
44877 + dwc_otg_hc_regs_t * hc_regs,
44878 + dwc_otg_qtd_t * qtd)
44879 +{
44880 + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
44881 + "Babble Error--\n", hc->hc_num);
44882 +
44883 + if (hcd->core_if->dma_desc_enable) {
44884 + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs,
44885 + DWC_OTG_HC_XFER_BABBLE_ERR);
44886 + goto handle_babble_done;
44887 + }
44888 +
44889 + if (hc->ep_type != DWC_OTG_EP_TYPE_ISOC) {
44890 + hcd->fops->complete(hcd, qtd->urb->priv,
44891 + qtd->urb, -DWC_E_OVERFLOW);
44892 + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_BABBLE_ERR);
44893 + } else {
44894 + dwc_otg_halt_status_e halt_status;
44895 + halt_status = update_isoc_urb_state(hcd, hc, hc_regs, qtd,
44896 + DWC_OTG_HC_XFER_BABBLE_ERR);
44897 + halt_channel(hcd, hc, qtd, halt_status);
44898 + }
44899 +
44900 +handle_babble_done:
44901 + disable_hc_int(hc_regs, bblerr);
44902 + return 1;
44903 +}
44904 +
44905 +/**
44906 + * Handles a host channel AHB error interrupt. This handler is only called in
44907 + * DMA mode.
44908 + */
44909 +static int32_t handle_hc_ahberr_intr(dwc_otg_hcd_t * hcd,
44910 + dwc_hc_t * hc,
44911 + dwc_otg_hc_regs_t * hc_regs,
44912 + dwc_otg_qtd_t * qtd)
44913 +{
44914 + hcchar_data_t hcchar;
44915 + hcsplt_data_t hcsplt;
44916 + hctsiz_data_t hctsiz;
44917 + uint32_t hcdma;
44918 + char *pipetype, *speed;
44919 +
44920 + dwc_otg_hcd_urb_t *urb = qtd->urb;
44921 +
44922 + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
44923 + "AHB Error--\n", hc->hc_num);
44924 +
44925 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
44926 + hcsplt.d32 = DWC_READ_REG32(&hc_regs->hcsplt);
44927 + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
44928 + hcdma = DWC_READ_REG32(&hc_regs->hcdma);
44929 +
44930 + DWC_ERROR("AHB ERROR, Channel %d\n", hc->hc_num);
44931 + DWC_ERROR(" hcchar 0x%08x, hcsplt 0x%08x\n", hcchar.d32, hcsplt.d32);
44932 + DWC_ERROR(" hctsiz 0x%08x, hcdma 0x%08x\n", hctsiz.d32, hcdma);
44933 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD URB Enqueue\n");
44934 + DWC_ERROR(" Device address: %d\n",
44935 + dwc_otg_hcd_get_dev_addr(&urb->pipe_info));
44936 + DWC_ERROR(" Endpoint: %d, %s\n",
44937 + dwc_otg_hcd_get_ep_num(&urb->pipe_info),
44938 + (dwc_otg_hcd_is_pipe_in(&urb->pipe_info) ? "IN" : "OUT"));
44939 +
44940 + switch (dwc_otg_hcd_get_pipe_type(&urb->pipe_info)) {
44941 + case UE_CONTROL:
44942 + pipetype = "CONTROL";
44943 + break;
44944 + case UE_BULK:
44945 + pipetype = "BULK";
44946 + break;
44947 + case UE_INTERRUPT:
44948 + pipetype = "INTERRUPT";
44949 + break;
44950 + case UE_ISOCHRONOUS:
44951 + pipetype = "ISOCHRONOUS";
44952 + break;
44953 + default:
44954 + pipetype = "UNKNOWN";
44955 + break;
44956 + }
44957 +
44958 + DWC_ERROR(" Endpoint type: %s\n", pipetype);
44959 +
44960 + switch (hc->speed) {
44961 + case DWC_OTG_EP_SPEED_HIGH:
44962 + speed = "HIGH";
44963 + break;
44964 + case DWC_OTG_EP_SPEED_FULL:
44965 + speed = "FULL";
44966 + break;
44967 + case DWC_OTG_EP_SPEED_LOW:
44968 + speed = "LOW";
44969 + break;
44970 + default:
44971 + speed = "UNKNOWN";
44972 + break;
44973 + };
44974 +
44975 + DWC_ERROR(" Speed: %s\n", speed);
44976 +
44977 + DWC_ERROR(" Max packet size: %d\n",
44978 + dwc_otg_hcd_get_mps(&urb->pipe_info));
44979 + DWC_ERROR(" Data buffer length: %d\n", urb->length);
44980 + DWC_ERROR(" Transfer buffer: %p, Transfer DMA: %p\n",
44981 + urb->buf, (void *)urb->dma);
44982 + DWC_ERROR(" Setup buffer: %p, Setup DMA: %p\n",
44983 + urb->setup_packet, (void *)urb->setup_dma);
44984 + DWC_ERROR(" Interval: %d\n", urb->interval);
44985 +
44986 + /* Core haltes the channel for Descriptor DMA mode */
44987 + if (hcd->core_if->dma_desc_enable) {
44988 + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs,
44989 + DWC_OTG_HC_XFER_AHB_ERR);
44990 + goto handle_ahberr_done;
44991 + }
44992 +
44993 + hcd->fops->complete(hcd, urb->priv, urb, -DWC_E_IO);
44994 +
44995 + /*
44996 + * Force a channel halt. Don't call halt_channel because that won't
44997 + * write to the HCCHARn register in DMA mode to force the halt.
44998 + */
44999 + dwc_otg_hc_halt(hcd->core_if, hc, DWC_OTG_HC_XFER_AHB_ERR);
45000 +handle_ahberr_done:
45001 + disable_hc_int(hc_regs, ahberr);
45002 + return 1;
45003 +}
45004 +
45005 +/**
45006 + * Handles a host channel transaction error interrupt. This handler may be
45007 + * called in either DMA mode or Slave mode.
45008 + */
45009 +static int32_t handle_hc_xacterr_intr(dwc_otg_hcd_t * hcd,
45010 + dwc_hc_t * hc,
45011 + dwc_otg_hc_regs_t * hc_regs,
45012 + dwc_otg_qtd_t * qtd)
45013 +{
45014 + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
45015 + "Transaction Error--\n", hc->hc_num);
45016 +
45017 + if (hcd->core_if->dma_desc_enable) {
45018 + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs,
45019 + DWC_OTG_HC_XFER_XACT_ERR);
45020 + goto handle_xacterr_done;
45021 + }
45022 +
45023 + switch (dwc_otg_hcd_get_pipe_type(&qtd->urb->pipe_info)) {
45024 + case UE_CONTROL:
45025 + case UE_BULK:
45026 + qtd->error_count++;
45027 + if (!hc->qh->ping_state) {
45028 +
45029 + update_urb_state_xfer_intr(hc, hc_regs,
45030 + qtd->urb, qtd,
45031 + DWC_OTG_HC_XFER_XACT_ERR);
45032 + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
45033 + if (!hc->ep_is_in && hc->speed == DWC_OTG_EP_SPEED_HIGH) {
45034 + hc->qh->ping_state = 1;
45035 + }
45036 + }
45037 +
45038 + /*
45039 + * Halt the channel so the transfer can be re-started from
45040 + * the appropriate point or the PING protocol will start.
45041 + */
45042 + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_XACT_ERR);
45043 + break;
45044 + case UE_INTERRUPT:
45045 + qtd->error_count++;
45046 + if (hc->do_split && hc->complete_split) {
45047 + qtd->complete_split = 0;
45048 + }
45049 + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_XACT_ERR);
45050 + break;
45051 + case UE_ISOCHRONOUS:
45052 + {
45053 + dwc_otg_halt_status_e halt_status;
45054 + halt_status =
45055 + update_isoc_urb_state(hcd, hc, hc_regs, qtd,
45056 + DWC_OTG_HC_XFER_XACT_ERR);
45057 +
45058 + halt_channel(hcd, hc, qtd, halt_status);
45059 + }
45060 + break;
45061 + }
45062 +handle_xacterr_done:
45063 + disable_hc_int(hc_regs, xacterr);
45064 +
45065 + return 1;
45066 +}
45067 +
45068 +/**
45069 + * Handles a host channel frame overrun interrupt. This handler may be called
45070 + * in either DMA mode or Slave mode.
45071 + */
45072 +static int32_t handle_hc_frmovrun_intr(dwc_otg_hcd_t * hcd,
45073 + dwc_hc_t * hc,
45074 + dwc_otg_hc_regs_t * hc_regs,
45075 + dwc_otg_qtd_t * qtd)
45076 +{
45077 + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
45078 + "Frame Overrun--\n", hc->hc_num);
45079 +
45080 + switch (dwc_otg_hcd_get_pipe_type(&qtd->urb->pipe_info)) {
45081 + case UE_CONTROL:
45082 + case UE_BULK:
45083 + break;
45084 + case UE_INTERRUPT:
45085 + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_FRAME_OVERRUN);
45086 + break;
45087 + case UE_ISOCHRONOUS:
45088 + {
45089 + dwc_otg_halt_status_e halt_status;
45090 + halt_status =
45091 + update_isoc_urb_state(hcd, hc, hc_regs, qtd,
45092 + DWC_OTG_HC_XFER_FRAME_OVERRUN);
45093 +
45094 + halt_channel(hcd, hc, qtd, halt_status);
45095 + }
45096 + break;
45097 + }
45098 +
45099 + disable_hc_int(hc_regs, frmovrun);
45100 +
45101 + return 1;
45102 +}
45103 +
45104 +/**
45105 + * Handles a host channel data toggle error interrupt. This handler may be
45106 + * called in either DMA mode or Slave mode.
45107 + */
45108 +static int32_t handle_hc_datatglerr_intr(dwc_otg_hcd_t * hcd,
45109 + dwc_hc_t * hc,
45110 + dwc_otg_hc_regs_t * hc_regs,
45111 + dwc_otg_qtd_t * qtd)
45112 +{
45113 + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
45114 + "Data Toggle Error on %s transfer--\n",
45115 + hc->hc_num, (hc->ep_is_in ? "IN" : "OUT"));
45116 +
45117 + /* Data toggles on split transactions cause the hc to halt.
45118 + * restart transfer */
45119 + if(hc->qh->do_split)
45120 + {
45121 + qtd->error_count++;
45122 + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
45123 + update_urb_state_xfer_intr(hc, hc_regs,
45124 + qtd->urb, qtd, DWC_OTG_HC_XFER_XACT_ERR);
45125 + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_XACT_ERR);
45126 + } else if (hc->ep_is_in) {
45127 + /* An unmasked data toggle error on a non-split DMA transaction is
45128 + * for the sole purpose of resetting error counts. Disable other
45129 + * interrupts unmasked for the same reason.
45130 + */
45131 + if(hcd->core_if->dma_enable) {
45132 + disable_hc_int(hc_regs, ack);
45133 + disable_hc_int(hc_regs, nak);
45134 + }
45135 + qtd->error_count = 0;
45136 + }
45137 +
45138 + disable_hc_int(hc_regs, datatglerr);
45139 +
45140 + return 1;
45141 +}
45142 +
45143 +#ifdef DEBUG
45144 +/**
45145 + * This function is for debug only. It checks that a valid halt status is set
45146 + * and that HCCHARn.chdis is clear. If there's a problem, corrective action is
45147 + * taken and a warning is issued.
45148 + * @return 1 if halt status is ok, 0 otherwise.
45149 + */
45150 +static inline int halt_status_ok(dwc_otg_hcd_t * hcd,
45151 + dwc_hc_t * hc,
45152 + dwc_otg_hc_regs_t * hc_regs,
45153 + dwc_otg_qtd_t * qtd)
45154 +{
45155 + hcchar_data_t hcchar;
45156 + hctsiz_data_t hctsiz;
45157 + hcint_data_t hcint;
45158 + hcintmsk_data_t hcintmsk;
45159 + hcsplt_data_t hcsplt;
45160 +
45161 + if (hc->halt_status == DWC_OTG_HC_XFER_NO_HALT_STATUS) {
45162 + /*
45163 + * This code is here only as a check. This condition should
45164 + * never happen. Ignore the halt if it does occur.
45165 + */
45166 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
45167 + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
45168 + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
45169 + hcintmsk.d32 = DWC_READ_REG32(&hc_regs->hcintmsk);
45170 + hcsplt.d32 = DWC_READ_REG32(&hc_regs->hcsplt);
45171 + DWC_WARN
45172 + ("%s: hc->halt_status == DWC_OTG_HC_XFER_NO_HALT_STATUS, "
45173 + "channel %d, hcchar 0x%08x, hctsiz 0x%08x, "
45174 + "hcint 0x%08x, hcintmsk 0x%08x, "
45175 + "hcsplt 0x%08x, qtd->complete_split %d\n", __func__,
45176 + hc->hc_num, hcchar.d32, hctsiz.d32, hcint.d32,
45177 + hcintmsk.d32, hcsplt.d32, qtd->complete_split);
45178 +
45179 + DWC_WARN("%s: no halt status, channel %d, ignoring interrupt\n",
45180 + __func__, hc->hc_num);
45181 + DWC_WARN("\n");
45182 + clear_hc_int(hc_regs, chhltd);
45183 + return 0;
45184 + }
45185 +
45186 + /*
45187 + * This code is here only as a check. hcchar.chdis should
45188 + * never be set when the halt interrupt occurs. Halt the
45189 + * channel again if it does occur.
45190 + */
45191 + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
45192 + if (hcchar.b.chdis) {
45193 + DWC_WARN("%s: hcchar.chdis set unexpectedly, "
45194 + "hcchar 0x%08x, trying to halt again\n",
45195 + __func__, hcchar.d32);
45196 + clear_hc_int(hc_regs, chhltd);
45197 + hc->halt_pending = 0;
45198 + halt_channel(hcd, hc, qtd, hc->halt_status);
45199 + return 0;
45200 + }
45201 +
45202 + return 1;
45203 +}
45204 +#endif
45205 +
45206 +/**
45207 + * Handles a host Channel Halted interrupt in DMA mode. This handler
45208 + * determines the reason the channel halted and proceeds accordingly.
45209 + */
45210 +static void handle_hc_chhltd_intr_dma(dwc_otg_hcd_t * hcd,
45211 + dwc_hc_t * hc,
45212 + dwc_otg_hc_regs_t * hc_regs,
45213 + dwc_otg_qtd_t * qtd)
45214 +{
45215 + int out_nak_enh = 0;
45216 + hcint_data_t hcint;
45217 + hcintmsk_data_t hcintmsk;
45218 + /* For core with OUT NAK enhancement, the flow for high-
45219 + * speed CONTROL/BULK OUT is handled a little differently.
45220 + */
45221 + if (hcd->core_if->snpsid >= OTG_CORE_REV_2_71a) {
45222 + if (hc->speed == DWC_OTG_EP_SPEED_HIGH && !hc->ep_is_in &&
45223 + (hc->ep_type == DWC_OTG_EP_TYPE_CONTROL ||
45224 + hc->ep_type == DWC_OTG_EP_TYPE_BULK)) {
45225 + out_nak_enh = 1;
45226 + }
45227 + }
45228 +
45229 + if (hc->halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE ||
45230 + (hc->halt_status == DWC_OTG_HC_XFER_AHB_ERR
45231 + && !hcd->core_if->dma_desc_enable)) {
45232 + /*
45233 + * Just release the channel. A dequeue can happen on a
45234 + * transfer timeout. In the case of an AHB Error, the channel
45235 + * was forced to halt because there's no way to gracefully
45236 + * recover.
45237 + */
45238 + if (hcd->core_if->dma_desc_enable)
45239 + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs,
45240 + hc->halt_status);
45241 + else
45242 + release_channel(hcd, hc, qtd, hc->halt_status);
45243 + return;
45244 + }
45245 +
45246 + /* Read the HCINTn register to determine the cause for the halt. */
45247 +
45248 + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
45249 + hcintmsk.d32 = DWC_READ_REG32(&hc_regs->hcintmsk);
45250 +
45251 + if (hcint.b.xfercomp) {
45252 + /** @todo This is here because of a possible hardware bug. Spec
45253 + * says that on SPLIT-ISOC OUT transfers in DMA mode that a HALT
45254 + * interrupt w/ACK bit set should occur, but I only see the
45255 + * XFERCOMP bit, even with it masked out. This is a workaround
45256 + * for that behavior. Should fix this when hardware is fixed.
45257 + */
45258 + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC && !hc->ep_is_in) {
45259 + handle_hc_ack_intr(hcd, hc, hc_regs, qtd);
45260 + }
45261 + handle_hc_xfercomp_intr(hcd, hc, hc_regs, qtd);
45262 + } else if (hcint.b.stall) {
45263 + handle_hc_stall_intr(hcd, hc, hc_regs, qtd);
45264 + } else if (hcint.b.xacterr && !hcd->core_if->dma_desc_enable) {
45265 + if (out_nak_enh) {
45266 + if (hcint.b.nyet || hcint.b.nak || hcint.b.ack) {
45267 + DWC_DEBUGPL(DBG_HCD, "XactErr with NYET/NAK/ACK\n");
45268 + qtd->error_count = 0;
45269 + } else {
45270 + DWC_DEBUGPL(DBG_HCD, "XactErr without NYET/NAK/ACK\n");
45271 + }
45272 + }
45273 +
45274 + /*
45275 + * Must handle xacterr before nak or ack. Could get a xacterr
45276 + * at the same time as either of these on a BULK/CONTROL OUT
45277 + * that started with a PING. The xacterr takes precedence.
45278 + */
45279 + handle_hc_xacterr_intr(hcd, hc, hc_regs, qtd);
45280 + } else if (hcint.b.xcs_xact && hcd->core_if->dma_desc_enable) {
45281 + handle_hc_xacterr_intr(hcd, hc, hc_regs, qtd);
45282 + } else if (hcint.b.ahberr && hcd->core_if->dma_desc_enable) {
45283 + handle_hc_ahberr_intr(hcd, hc, hc_regs, qtd);
45284 + } else if (hcint.b.bblerr) {
45285 + handle_hc_babble_intr(hcd, hc, hc_regs, qtd);
45286 + } else if (hcint.b.frmovrun) {
45287 + handle_hc_frmovrun_intr(hcd, hc, hc_regs, qtd);
45288 + } else if (hcint.b.datatglerr) {
45289 + handle_hc_datatglerr_intr(hcd, hc, hc_regs, qtd);
45290 + } else if (!out_nak_enh) {
45291 + if (hcint.b.nyet) {
45292 + /*
45293 + * Must handle nyet before nak or ack. Could get a nyet at the
45294 + * same time as either of those on a BULK/CONTROL OUT that
45295 + * started with a PING. The nyet takes precedence.
45296 + */
45297 + handle_hc_nyet_intr(hcd, hc, hc_regs, qtd);
45298 + } else if (hcint.b.nak && !hcintmsk.b.nak) {
45299 + /*
45300 + * If nak is not masked, it's because a non-split IN transfer
45301 + * is in an error state. In that case, the nak is handled by
45302 + * the nak interrupt handler, not here. Handle nak here for
45303 + * BULK/CONTROL OUT transfers, which halt on a NAK to allow
45304 + * rewinding the buffer pointer.
45305 + */
45306 + handle_hc_nak_intr(hcd, hc, hc_regs, qtd);
45307 + } else if (hcint.b.ack && !hcintmsk.b.ack) {
45308 + /*
45309 + * If ack is not masked, it's because a non-split IN transfer
45310 + * is in an error state. In that case, the ack is handled by
45311 + * the ack interrupt handler, not here. Handle ack here for
45312 + * split transfers. Start splits halt on ACK.
45313 + */
45314 + handle_hc_ack_intr(hcd, hc, hc_regs, qtd);
45315 + } else {
45316 + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
45317 + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
45318 + /*
45319 + * A periodic transfer halted with no other channel
45320 + * interrupts set. Assume it was halted by the core
45321 + * because it could not be completed in its scheduled
45322 + * (micro)frame.
45323 + */
45324 +#ifdef DEBUG
45325 + DWC_PRINTF
45326 + ("%s: Halt channel %d (assume incomplete periodic transfer)\n",
45327 + __func__, hc->hc_num);
45328 +#endif
45329 + halt_channel(hcd, hc, qtd,
45330 + DWC_OTG_HC_XFER_PERIODIC_INCOMPLETE);
45331 + } else {
45332 + DWC_ERROR
45333 + ("%s: Channel %d, DMA Mode -- ChHltd set, but reason "
45334 + "for halting is unknown, hcint 0x%08x, intsts 0x%08x\n",
45335 + __func__, hc->hc_num, hcint.d32,
45336 + DWC_READ_REG32(&hcd->
45337 + core_if->core_global_regs->
45338 + gintsts));
45339 + /* Failthrough: use 3-strikes rule */
45340 + qtd->error_count++;
45341 + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
45342 + update_urb_state_xfer_intr(hc, hc_regs,
45343 + qtd->urb, qtd, DWC_OTG_HC_XFER_XACT_ERR);
45344 + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_XACT_ERR);
45345 + }
45346 +
45347 + }
45348 + } else {
45349 + DWC_PRINTF("NYET/NAK/ACK/other in non-error case, 0x%08x\n",
45350 + hcint.d32);
45351 + /* Failthrough: use 3-strikes rule */
45352 + qtd->error_count++;
45353 + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
45354 + update_urb_state_xfer_intr(hc, hc_regs,
45355 + qtd->urb, qtd, DWC_OTG_HC_XFER_XACT_ERR);
45356 + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_XACT_ERR);
45357 + }
45358 +}
45359 +
45360 +/**
45361 + * Handles a host channel Channel Halted interrupt.
45362 + *
45363 + * In slave mode, this handler is called only when the driver specifically
45364 + * requests a halt. This occurs during handling other host channel interrupts
45365 + * (e.g. nak, xacterr, stall, nyet, etc.).
45366 + *
45367 + * In DMA mode, this is the interrupt that occurs when the core has finished
45368 + * processing a transfer on a channel. Other host channel interrupts (except
45369 + * ahberr) are disabled in DMA mode.
45370 + */
45371 +static int32_t handle_hc_chhltd_intr(dwc_otg_hcd_t * hcd,
45372 + dwc_hc_t * hc,
45373 + dwc_otg_hc_regs_t * hc_regs,
45374 + dwc_otg_qtd_t * qtd)
45375 +{
45376 + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
45377 + "Channel Halted--\n", hc->hc_num);
45378 +
45379 + if (hcd->core_if->dma_enable) {
45380 + handle_hc_chhltd_intr_dma(hcd, hc, hc_regs, qtd);
45381 + } else {
45382 +#ifdef DEBUG
45383 + if (!halt_status_ok(hcd, hc, hc_regs, qtd)) {
45384 + return 1;
45385 + }
45386 +#endif
45387 + release_channel(hcd, hc, qtd, hc->halt_status);
45388 + }
45389 +
45390 + return 1;
45391 +}
45392 +
45393 +
45394 +/**
45395 + * dwc_otg_fiq_unmangle_isoc() - Update the iso_frame_desc structure on
45396 + * FIQ transfer completion
45397 + * @hcd: Pointer to dwc_otg_hcd struct
45398 + * @num: Host channel number
45399 + *
45400 + * 1. Un-mangle the status as recorded in each iso_frame_desc status
45401 + * 2. Copy it from the dwc_otg_urb into the real URB
45402 + */
45403 +void dwc_otg_fiq_unmangle_isoc(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh, dwc_otg_qtd_t *qtd, uint32_t num)
45404 +{
45405 + struct dwc_otg_hcd_urb *dwc_urb = qtd->urb;
45406 + int nr_frames = dwc_urb->packet_count;
45407 + int i;
45408 + hcint_data_t frame_hcint;
45409 +
45410 + for (i = 0; i < nr_frames; i++) {
45411 + frame_hcint.d32 = dwc_urb->iso_descs[i].status;
45412 + if (frame_hcint.b.xfercomp) {
45413 + dwc_urb->iso_descs[i].status = 0;
45414 + dwc_urb->actual_length += dwc_urb->iso_descs[i].actual_length;
45415 + } else if (frame_hcint.b.frmovrun) {
45416 + if (qh->ep_is_in)
45417 + dwc_urb->iso_descs[i].status = -DWC_E_NO_STREAM_RES;
45418 + else
45419 + dwc_urb->iso_descs[i].status = -DWC_E_COMMUNICATION;
45420 + dwc_urb->error_count++;
45421 + dwc_urb->iso_descs[i].actual_length = 0;
45422 + } else if (frame_hcint.b.xacterr) {
45423 + dwc_urb->iso_descs[i].status = -DWC_E_PROTOCOL;
45424 + dwc_urb->error_count++;
45425 + dwc_urb->iso_descs[i].actual_length = 0;
45426 + } else if (frame_hcint.b.bblerr) {
45427 + dwc_urb->iso_descs[i].status = -DWC_E_OVERFLOW;
45428 + dwc_urb->error_count++;
45429 + dwc_urb->iso_descs[i].actual_length = 0;
45430 + } else {
45431 + /* Something went wrong */
45432 + dwc_urb->iso_descs[i].status = -1;
45433 + dwc_urb->iso_descs[i].actual_length = 0;
45434 + dwc_urb->error_count++;
45435 + }
45436 + }
45437 + qh->sched_frame = dwc_frame_num_inc(qh->sched_frame, qh->interval * (nr_frames - 1));
45438 +
45439 + //printk_ratelimited(KERN_INFO "%s: HS isochronous of %d/%d frames with %d errors complete\n",
45440 + // __FUNCTION__, i, dwc_urb->packet_count, dwc_urb->error_count);
45441 +}
45442 +
45443 +/**
45444 + * dwc_otg_fiq_unsetup_per_dma() - Remove data from bounce buffers for split transactions
45445 + * @hcd: Pointer to dwc_otg_hcd struct
45446 + * @num: Host channel number
45447 + *
45448 + * Copies data from the FIQ bounce buffers into the URB's transfer buffer. Does not modify URB state.
45449 + * Returns total length of data or -1 if the buffers were not used.
45450 + *
45451 + */
45452 +int dwc_otg_fiq_unsetup_per_dma(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh, dwc_otg_qtd_t *qtd, uint32_t num)
45453 +{
45454 + dwc_hc_t *hc = qh->channel;
45455 + struct fiq_dma_blob *blob = hcd->fiq_dmab;
45456 + struct fiq_channel_state *st = &hcd->fiq_state->channel[num];
45457 + uint8_t *ptr = NULL;
45458 + int index = 0, len = 0;
45459 + int i = 0;
45460 + if (hc->ep_is_in) {
45461 + /* Copy data out of the DMA bounce buffers to the URB's buffer.
45462 + * The align_buf is ignored as this is ignored on FSM enqueue. */
45463 + ptr = qtd->urb->buf;
45464 + if (qh->ep_type == UE_ISOCHRONOUS) {
45465 + /* Isoc IN transactions - grab the offset of the iso_frame_desc into the URB transfer buffer */
45466 + index = qtd->isoc_frame_index;
45467 + ptr += qtd->urb->iso_descs[index].offset;
45468 + } else {
45469 + /* Need to increment by actual_length for interrupt IN */
45470 + ptr += qtd->urb->actual_length;
45471 + }
45472 +
45473 + for (i = 0; i < st->dma_info.index; i++) {
45474 + len += st->dma_info.slot_len[i];
45475 + dwc_memcpy(ptr, &blob->channel[num].index[i].buf[0], st->dma_info.slot_len[i]);
45476 + ptr += st->dma_info.slot_len[i];
45477 + }
45478 + return len;
45479 + } else {
45480 + /* OUT endpoints - nothing to do. */
45481 + return -1;
45482 + }
45483 +
45484 +}
45485 +/**
45486 + * dwc_otg_hcd_handle_hc_fsm() - handle an unmasked channel interrupt
45487 + * from a channel handled in the FIQ
45488 + * @hcd: Pointer to dwc_otg_hcd struct
45489 + * @num: Host channel number
45490 + *
45491 + * If a host channel interrupt was received by the IRQ and this was a channel
45492 + * used by the FIQ, the execution flow for transfer completion is substantially
45493 + * different from the normal (messy) path. This function and its friends handles
45494 + * channel cleanup and transaction completion from a FIQ transaction.
45495 + */
45496 +void dwc_otg_hcd_handle_hc_fsm(dwc_otg_hcd_t *hcd, uint32_t num)
45497 +{
45498 + struct fiq_channel_state *st = &hcd->fiq_state->channel[num];
45499 + dwc_hc_t *hc = hcd->hc_ptr_array[num];
45500 + dwc_otg_qtd_t *qtd;
45501 + dwc_otg_hc_regs_t *hc_regs = hcd->core_if->host_if->hc_regs[num];
45502 + hcint_data_t hcint = hcd->fiq_state->channel[num].hcint_copy;
45503 + hctsiz_data_t hctsiz = hcd->fiq_state->channel[num].hctsiz_copy;
45504 + int hostchannels = 0;
45505 + fiq_print(FIQDBG_INT, hcd->fiq_state, "OUT %01d %01d ", num , st->fsm);
45506 +
45507 + hostchannels = hcd->available_host_channels;
45508 + if (hc->halt_pending) {
45509 + /* Dequeue: The FIQ was allowed to complete the transfer but state has been cleared. */
45510 + if (hc->qh && st->fsm == FIQ_NP_SPLIT_DONE &&
45511 + hcint.b.xfercomp && hc->qh->ep_type == UE_BULK) {
45512 + if (hctsiz.b.pid == DWC_HCTSIZ_DATA0) {
45513 + hc->qh->data_toggle = DWC_OTG_HC_PID_DATA1;
45514 + } else {
45515 + hc->qh->data_toggle = DWC_OTG_HC_PID_DATA0;
45516 + }
45517 + }
45518 + release_channel(hcd, hc, NULL, hc->halt_status);
45519 + return;
45520 + }
45521 +
45522 + qtd = DWC_CIRCLEQ_FIRST(&hc->qh->qtd_list);
45523 + switch (st->fsm) {
45524 + case FIQ_TEST:
45525 + break;
45526 +
45527 + case FIQ_DEQUEUE_ISSUED:
45528 + /* Handled above, but keep for posterity */
45529 + release_channel(hcd, hc, NULL, hc->halt_status);
45530 + break;
45531 +
45532 + case FIQ_NP_SPLIT_DONE:
45533 + /* Nonperiodic transaction complete. */
45534 + if (!hc->ep_is_in) {
45535 + qtd->ssplit_out_xfer_count = hc->xfer_len;
45536 + }
45537 + if (hcint.b.xfercomp) {
45538 + handle_hc_xfercomp_intr(hcd, hc, hc_regs, qtd);
45539 + } else if (hcint.b.nak) {
45540 + handle_hc_nak_intr(hcd, hc, hc_regs, qtd);
45541 + } else {
45542 + DWC_WARN("Unexpected IRQ state on FSM transaction:"
45543 + "dev_addr=%d ep=%d fsm=%d, hcint=0x%08x\n",
45544 + hc->dev_addr, hc->ep_num, st->fsm, hcint.d32);
45545 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
45546 + }
45547 + break;
45548 +
45549 + case FIQ_NP_SPLIT_HS_ABORTED:
45550 + /* A HS abort is a 3-strikes on the HS bus at any point in the transaction.
45551 + * Normally a CLEAR_TT_BUFFER hub command would be required: we can't do that
45552 + * because there's no guarantee which order a non-periodic split happened in.
45553 + * We could end up clearing a perfectly good transaction out of the buffer.
45554 + */
45555 + if (hcint.b.xacterr) {
45556 + qtd->error_count += st->nr_errors;
45557 + handle_hc_xacterr_intr(hcd, hc, hc_regs, qtd);
45558 + } else if (hcint.b.ahberr) {
45559 + handle_hc_ahberr_intr(hcd, hc, hc_regs, qtd);
45560 + } else {
45561 + DWC_WARN("Unexpected IRQ state on FSM transaction:"
45562 + "dev_addr=%d ep=%d fsm=%d, hcint=0x%08x\n",
45563 + hc->dev_addr, hc->ep_num, st->fsm, hcint.d32);
45564 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
45565 + }
45566 + break;
45567 +
45568 + case FIQ_NP_SPLIT_LS_ABORTED:
45569 + /* A few cases can cause this - either an unknown state on a SSPLIT or
45570 + * STALL/data toggle error response on a CSPLIT */
45571 + if (hcint.b.stall) {
45572 + handle_hc_stall_intr(hcd, hc, hc_regs, qtd);
45573 + } else if (hcint.b.datatglerr) {
45574 + handle_hc_datatglerr_intr(hcd, hc, hc_regs, qtd);
45575 + } else if (hcint.b.bblerr) {
45576 + handle_hc_babble_intr(hcd, hc, hc_regs, qtd);
45577 + } else if (hcint.b.ahberr) {
45578 + handle_hc_ahberr_intr(hcd, hc, hc_regs, qtd);
45579 + } else {
45580 + DWC_WARN("Unexpected IRQ state on FSM transaction:"
45581 + "dev_addr=%d ep=%d fsm=%d, hcint=0x%08x\n",
45582 + hc->dev_addr, hc->ep_num, st->fsm, hcint.d32);
45583 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
45584 + }
45585 + break;
45586 +
45587 + case FIQ_PER_SPLIT_DONE:
45588 + /* Isoc IN or Interrupt IN/OUT */
45589 +
45590 + /* Flow control here is different from the normal execution by the driver.
45591 + * We need to completely ignore most of the driver's method of handling
45592 + * split transactions and do it ourselves.
45593 + */
45594 + if (hc->ep_type == UE_INTERRUPT) {
45595 + if (hcint.b.nak) {
45596 + handle_hc_nak_intr(hcd, hc, hc_regs, qtd);
45597 + } else if (hc->ep_is_in) {
45598 + int len;
45599 + len = dwc_otg_fiq_unsetup_per_dma(hcd, hc->qh, qtd, num);
45600 + //printk(KERN_NOTICE "FIQ Transaction: hc=%d len=%d urb_len = %d\n", num, len, qtd->urb->length);
45601 + qtd->urb->actual_length += len;
45602 + if (qtd->urb->actual_length >= qtd->urb->length) {
45603 + qtd->urb->status = 0;
45604 + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, qtd->urb->status);
45605 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
45606 + } else {
45607 + /* Interrupt transfer not complete yet - is it a short read? */
45608 + if (len < hc->max_packet) {
45609 + /* Interrupt transaction complete */
45610 + qtd->urb->status = 0;
45611 + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, qtd->urb->status);
45612 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
45613 + } else {
45614 + /* Further transactions required */
45615 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_COMPLETE);
45616 + }
45617 + }
45618 + } else {
45619 + /* Interrupt OUT complete. */
45620 + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
45621 + qtd->urb->actual_length += hc->xfer_len;
45622 + if (qtd->urb->actual_length >= qtd->urb->length) {
45623 + qtd->urb->status = 0;
45624 + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, qtd->urb->status);
45625 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
45626 + } else {
45627 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_COMPLETE);
45628 + }
45629 + }
45630 + } else {
45631 + /* ISOC IN complete. */
45632 + struct dwc_otg_hcd_iso_packet_desc *frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
45633 + int len = 0;
45634 + /* Record errors, update qtd. */
45635 + if (st->nr_errors) {
45636 + frame_desc->actual_length = 0;
45637 + frame_desc->status = -DWC_E_PROTOCOL;
45638 + } else {
45639 + frame_desc->status = 0;
45640 + /* Unswizzle dma */
45641 + len = dwc_otg_fiq_unsetup_per_dma(hcd, hc->qh, qtd, num);
45642 + frame_desc->actual_length = len;
45643 + }
45644 + qtd->isoc_frame_index++;
45645 + if (qtd->isoc_frame_index == qtd->urb->packet_count) {
45646 + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
45647 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
45648 + } else {
45649 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_COMPLETE);
45650 + }
45651 + }
45652 + break;
45653 +
45654 + case FIQ_PER_ISO_OUT_DONE: {
45655 + struct dwc_otg_hcd_iso_packet_desc *frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
45656 + /* Record errors, update qtd. */
45657 + if (st->nr_errors) {
45658 + frame_desc->actual_length = 0;
45659 + frame_desc->status = -DWC_E_PROTOCOL;
45660 + } else {
45661 + frame_desc->status = 0;
45662 + frame_desc->actual_length = frame_desc->length;
45663 + }
45664 + qtd->isoc_frame_index++;
45665 + qtd->isoc_split_offset = 0;
45666 + if (qtd->isoc_frame_index == qtd->urb->packet_count) {
45667 + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
45668 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
45669 + } else {
45670 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_COMPLETE);
45671 + }
45672 + }
45673 + break;
45674 +
45675 + case FIQ_PER_SPLIT_NYET_ABORTED:
45676 + /* Doh. lost the data. */
45677 + printk_ratelimited(KERN_INFO "Transfer to device %d endpoint 0x%x frame %d failed "
45678 + "- FIQ reported NYET. Data may have been lost.\n",
45679 + hc->dev_addr, hc->ep_num, dwc_otg_hcd_get_frame_number(hcd) >> 3);
45680 + if (hc->ep_type == UE_ISOCHRONOUS) {
45681 + struct dwc_otg_hcd_iso_packet_desc *frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
45682 + /* Record errors, update qtd. */
45683 + frame_desc->actual_length = 0;
45684 + frame_desc->status = -DWC_E_PROTOCOL;
45685 + qtd->isoc_frame_index++;
45686 + qtd->isoc_split_offset = 0;
45687 + if (qtd->isoc_frame_index == qtd->urb->packet_count) {
45688 + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
45689 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
45690 + } else {
45691 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_COMPLETE);
45692 + }
45693 + } else {
45694 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
45695 + }
45696 + break;
45697 +
45698 + case FIQ_HS_ISOC_DONE:
45699 + /* The FIQ has performed a whole pile of isochronous transactions.
45700 + * The status is recorded as the interrupt state should the transaction
45701 + * fail.
45702 + */
45703 + dwc_otg_fiq_unmangle_isoc(hcd, hc->qh, qtd, num);
45704 + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
45705 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
45706 + break;
45707 +
45708 + case FIQ_PER_SPLIT_LS_ABORTED:
45709 + if (hcint.b.xacterr) {
45710 + /* Hub has responded with an ERR packet. Device
45711 + * has been unplugged or the port has been disabled.
45712 + * TODO: need to issue a reset to the hub port. */
45713 + qtd->error_count += 3;
45714 + handle_hc_xacterr_intr(hcd, hc, hc_regs, qtd);
45715 + } else if (hcint.b.stall) {
45716 + handle_hc_stall_intr(hcd, hc, hc_regs, qtd);
45717 + } else if (hcint.b.bblerr) {
45718 + handle_hc_babble_intr(hcd, hc, hc_regs, qtd);
45719 + } else {
45720 + printk_ratelimited(KERN_INFO "Transfer to device %d endpoint 0x%x failed "
45721 + "- FIQ reported FSM=%d. Data may have been lost.\n",
45722 + st->fsm, hc->dev_addr, hc->ep_num);
45723 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
45724 + }
45725 + break;
45726 +
45727 + case FIQ_PER_SPLIT_HS_ABORTED:
45728 + /* Either the SSPLIT phase suffered transaction errors or something
45729 + * unexpected happened.
45730 + */
45731 + qtd->error_count += 3;
45732 + handle_hc_xacterr_intr(hcd, hc, hc_regs, qtd);
45733 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
45734 + break;
45735 +
45736 + case FIQ_PER_SPLIT_TIMEOUT:
45737 + /* Couldn't complete in the nominated frame */
45738 + printk(KERN_INFO "Transfer to device %d endpoint 0x%x frame %d failed "
45739 + "- FIQ timed out. Data may have been lost.\n",
45740 + hc->dev_addr, hc->ep_num, dwc_otg_hcd_get_frame_number(hcd) >> 3);
45741 + if (hc->ep_type == UE_ISOCHRONOUS) {
45742 + struct dwc_otg_hcd_iso_packet_desc *frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
45743 + /* Record errors, update qtd. */
45744 + frame_desc->actual_length = 0;
45745 + if (hc->ep_is_in) {
45746 + frame_desc->status = -DWC_E_NO_STREAM_RES;
45747 + } else {
45748 + frame_desc->status = -DWC_E_COMMUNICATION;
45749 + }
45750 + qtd->isoc_frame_index++;
45751 + if (qtd->isoc_frame_index == qtd->urb->packet_count) {
45752 + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
45753 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
45754 + } else {
45755 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_COMPLETE);
45756 + }
45757 + } else {
45758 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
45759 + }
45760 + break;
45761 +
45762 + default:
45763 + DWC_WARN("Unexpected state received on hc=%d fsm=%d on transfer to device %d ep 0x%x",
45764 + hc->hc_num, st->fsm, hc->dev_addr, hc->ep_num);
45765 + qtd->error_count++;
45766 + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
45767 + }
45768 + return;
45769 +}
45770 +
45771 +/** Handles interrupt for a specific Host Channel */
45772 +int32_t dwc_otg_hcd_handle_hc_n_intr(dwc_otg_hcd_t * dwc_otg_hcd, uint32_t num)
45773 +{
45774 + int retval = 0;
45775 + hcint_data_t hcint;
45776 + hcintmsk_data_t hcintmsk;
45777 + dwc_hc_t *hc;
45778 + dwc_otg_hc_regs_t *hc_regs;
45779 + dwc_otg_qtd_t *qtd;
45780 +
45781 + DWC_DEBUGPL(DBG_HCDV, "--Host Channel Interrupt--, Channel %d\n", num);
45782 +
45783 + hc = dwc_otg_hcd->hc_ptr_array[num];
45784 + hc_regs = dwc_otg_hcd->core_if->host_if->hc_regs[num];
45785 + if(hc->halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE) {
45786 + /* A dequeue was issued for this transfer. Our QTD has gone away
45787 + * but in the case of a FIQ transfer, the transfer would have run
45788 + * to completion.
45789 + */
45790 + if (fiq_fsm_enable && dwc_otg_hcd->fiq_state->channel[num].fsm != FIQ_PASSTHROUGH) {
45791 + dwc_otg_hcd_handle_hc_fsm(dwc_otg_hcd, num);
45792 + } else {
45793 + release_channel(dwc_otg_hcd, hc, NULL, hc->halt_status);
45794 + }
45795 + return 1;
45796 + }
45797 + qtd = DWC_CIRCLEQ_FIRST(&hc->qh->qtd_list);
45798 +
45799 + /*
45800 + * FSM mode: Check to see if this is a HC interrupt from a channel handled by the FIQ.
45801 + * Execution path is fundamentally different for the channels after a FIQ has completed
45802 + * a split transaction.
45803 + */
45804 + if (fiq_fsm_enable) {
45805 + switch (dwc_otg_hcd->fiq_state->channel[num].fsm) {
45806 + case FIQ_PASSTHROUGH:
45807 + break;
45808 + case FIQ_PASSTHROUGH_ERRORSTATE:
45809 + /* Hook into the error count */
45810 + fiq_print(FIQDBG_ERR, dwc_otg_hcd->fiq_state, "HCDERR%02d", num);
45811 + if (!dwc_otg_hcd->fiq_state->channel[num].nr_errors) {
45812 + qtd->error_count = 0;
45813 + fiq_print(FIQDBG_ERR, dwc_otg_hcd->fiq_state, "RESET ");
45814 + }
45815 + break;
45816 + default:
45817 + dwc_otg_hcd_handle_hc_fsm(dwc_otg_hcd, num);
45818 + return 1;
45819 + }
45820 + }
45821 +
45822 + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
45823 + hcintmsk.d32 = DWC_READ_REG32(&hc_regs->hcintmsk);
45824 + hcint.d32 = hcint.d32 & hcintmsk.d32;
45825 + if (!dwc_otg_hcd->core_if->dma_enable) {
45826 + if (hcint.b.chhltd && hcint.d32 != 0x2) {
45827 + hcint.b.chhltd = 0;
45828 + }
45829 + }
45830 +
45831 + if (hcint.b.xfercomp) {
45832 + retval |=
45833 + handle_hc_xfercomp_intr(dwc_otg_hcd, hc, hc_regs, qtd);
45834 + /*
45835 + * If NYET occurred at same time as Xfer Complete, the NYET is
45836 + * handled by the Xfer Complete interrupt handler. Don't want
45837 + * to call the NYET interrupt handler in this case.
45838 + */
45839 + hcint.b.nyet = 0;
45840 + }
45841 + if (hcint.b.chhltd) {
45842 + retval |= handle_hc_chhltd_intr(dwc_otg_hcd, hc, hc_regs, qtd);
45843 + }
45844 + if (hcint.b.ahberr) {
45845 + retval |= handle_hc_ahberr_intr(dwc_otg_hcd, hc, hc_regs, qtd);
45846 + }
45847 + if (hcint.b.stall) {
45848 + retval |= handle_hc_stall_intr(dwc_otg_hcd, hc, hc_regs, qtd);
45849 + }
45850 + if (hcint.b.nak) {
45851 + retval |= handle_hc_nak_intr(dwc_otg_hcd, hc, hc_regs, qtd);
45852 + }
45853 + if (hcint.b.ack) {
45854 + if(!hcint.b.chhltd)
45855 + retval |= handle_hc_ack_intr(dwc_otg_hcd, hc, hc_regs, qtd);
45856 + }
45857 + if (hcint.b.nyet) {
45858 + retval |= handle_hc_nyet_intr(dwc_otg_hcd, hc, hc_regs, qtd);
45859 + }
45860 + if (hcint.b.xacterr) {
45861 + retval |= handle_hc_xacterr_intr(dwc_otg_hcd, hc, hc_regs, qtd);
45862 + }
45863 + if (hcint.b.bblerr) {
45864 + retval |= handle_hc_babble_intr(dwc_otg_hcd, hc, hc_regs, qtd);
45865 + }
45866 + if (hcint.b.frmovrun) {
45867 + retval |=
45868 + handle_hc_frmovrun_intr(dwc_otg_hcd, hc, hc_regs, qtd);
45869 + }
45870 + if (hcint.b.datatglerr) {
45871 + retval |=
45872 + handle_hc_datatglerr_intr(dwc_otg_hcd, hc, hc_regs, qtd);
45873 + }
45874 +
45875 + return retval;
45876 +}
45877 +#endif /* DWC_DEVICE_ONLY */
45878 --- /dev/null
45879 +++ b/drivers/usb/host/dwc_otg/dwc_otg_hcd_linux.c
45880 @@ -0,0 +1,1007 @@
45881 +
45882 +/* ==========================================================================
45883 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd_linux.c $
45884 + * $Revision: #20 $
45885 + * $Date: 2011/10/26 $
45886 + * $Change: 1872981 $
45887 + *
45888 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
45889 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
45890 + * otherwise expressly agreed to in writing between Synopsys and you.
45891 + *
45892 + * The Software IS NOT an item of Licensed Software or Licensed Product under
45893 + * any End User Software License Agreement or Agreement for Licensed Product
45894 + * with Synopsys or any supplement thereto. You are permitted to use and
45895 + * redistribute this Software in source and binary forms, with or without
45896 + * modification, provided that redistributions of source code must retain this
45897 + * notice. You may not view, use, disclose, copy or distribute this file or
45898 + * any information contained herein except pursuant to this license grant from
45899 + * Synopsys. If you do not agree with this notice, including the disclaimer
45900 + * below, then you are not authorized to use the Software.
45901 + *
45902 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
45903 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45904 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45905 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
45906 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
45907 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
45908 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
45909 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
45910 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
45911 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
45912 + * DAMAGE.
45913 + * ========================================================================== */
45914 +#ifndef DWC_DEVICE_ONLY
45915 +
45916 +/**
45917 + * @file
45918 + *
45919 + * This file contains the implementation of the HCD. In Linux, the HCD
45920 + * implements the hc_driver API.
45921 + */
45922 +#include <linux/kernel.h>
45923 +#include <linux/module.h>
45924 +#include <linux/moduleparam.h>
45925 +#include <linux/init.h>
45926 +#include <linux/device.h>
45927 +#include <linux/errno.h>
45928 +#include <linux/list.h>
45929 +#include <linux/interrupt.h>
45930 +#include <linux/string.h>
45931 +#include <linux/dma-mapping.h>
45932 +#include <linux/version.h>
45933 +#include <asm/io.h>
45934 +#include <asm/fiq.h>
45935 +#include <linux/usb.h>
45936 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,35)
45937 +#include <../drivers/usb/core/hcd.h>
45938 +#else
45939 +#include <linux/usb/hcd.h>
45940 +#endif
45941 +#include <asm/bug.h>
45942 +
45943 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30))
45944 +#define USB_URB_EP_LINKING 1
45945 +#else
45946 +#define USB_URB_EP_LINKING 0
45947 +#endif
45948 +
45949 +#include "dwc_otg_hcd_if.h"
45950 +#include "dwc_otg_dbg.h"
45951 +#include "dwc_otg_driver.h"
45952 +#include "dwc_otg_hcd.h"
45953 +
45954 +extern unsigned char _dwc_otg_fiq_stub, _dwc_otg_fiq_stub_end;
45955 +
45956 +/**
45957 + * Gets the endpoint number from a _bEndpointAddress argument. The endpoint is
45958 + * qualified with its direction (possible 32 endpoints per device).
45959 + */
45960 +#define dwc_ep_addr_to_endpoint(_bEndpointAddress_) ((_bEndpointAddress_ & USB_ENDPOINT_NUMBER_MASK) | \
45961 + ((_bEndpointAddress_ & USB_DIR_IN) != 0) << 4)
45962 +
45963 +static const char dwc_otg_hcd_name[] = "dwc_otg_hcd";
45964 +
45965 +extern bool fiq_enable;
45966 +
45967 +/** @name Linux HC Driver API Functions */
45968 +/** @{ */
45969 +/* manage i/o requests, device state */
45970 +static int dwc_otg_urb_enqueue(struct usb_hcd *hcd,
45971 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
45972 + struct usb_host_endpoint *ep,
45973 +#endif
45974 + struct urb *urb, gfp_t mem_flags);
45975 +
45976 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30)
45977 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
45978 +static int dwc_otg_urb_dequeue(struct usb_hcd *hcd, struct urb *urb);
45979 +#endif
45980 +#else /* kernels at or post 2.6.30 */
45981 +static int dwc_otg_urb_dequeue(struct usb_hcd *hcd,
45982 + struct urb *urb, int status);
45983 +#endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30) */
45984 +
45985 +static void endpoint_disable(struct usb_hcd *hcd, struct usb_host_endpoint *ep);
45986 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30)
45987 +static void endpoint_reset(struct usb_hcd *hcd, struct usb_host_endpoint *ep);
45988 +#endif
45989 +static irqreturn_t dwc_otg_hcd_irq(struct usb_hcd *hcd);
45990 +extern int hcd_start(struct usb_hcd *hcd);
45991 +extern void hcd_stop(struct usb_hcd *hcd);
45992 +static int get_frame_number(struct usb_hcd *hcd);
45993 +extern int hub_status_data(struct usb_hcd *hcd, char *buf);
45994 +extern int hub_control(struct usb_hcd *hcd,
45995 + u16 typeReq,
45996 + u16 wValue, u16 wIndex, char *buf, u16 wLength);
45997 +
45998 +struct wrapper_priv_data {
45999 + dwc_otg_hcd_t *dwc_otg_hcd;
46000 +};
46001 +
46002 +/** @} */
46003 +
46004 +static struct hc_driver dwc_otg_hc_driver = {
46005 +
46006 + .description = dwc_otg_hcd_name,
46007 + .product_desc = "DWC OTG Controller",
46008 + .hcd_priv_size = sizeof(struct wrapper_priv_data),
46009 +
46010 + .irq = dwc_otg_hcd_irq,
46011 +
46012 + .flags = HCD_MEMORY | HCD_USB2,
46013 +
46014 + //.reset =
46015 + .start = hcd_start,
46016 + //.suspend =
46017 + //.resume =
46018 + .stop = hcd_stop,
46019 +
46020 + .urb_enqueue = dwc_otg_urb_enqueue,
46021 + .urb_dequeue = dwc_otg_urb_dequeue,
46022 + .endpoint_disable = endpoint_disable,
46023 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30)
46024 + .endpoint_reset = endpoint_reset,
46025 +#endif
46026 + .get_frame_number = get_frame_number,
46027 +
46028 + .hub_status_data = hub_status_data,
46029 + .hub_control = hub_control,
46030 + //.bus_suspend =
46031 + //.bus_resume =
46032 +};
46033 +
46034 +/** Gets the dwc_otg_hcd from a struct usb_hcd */
46035 +static inline dwc_otg_hcd_t *hcd_to_dwc_otg_hcd(struct usb_hcd *hcd)
46036 +{
46037 + struct wrapper_priv_data *p;
46038 + p = (struct wrapper_priv_data *)(hcd->hcd_priv);
46039 + return p->dwc_otg_hcd;
46040 +}
46041 +
46042 +/** Gets the struct usb_hcd that contains a dwc_otg_hcd_t. */
46043 +static inline struct usb_hcd *dwc_otg_hcd_to_hcd(dwc_otg_hcd_t * dwc_otg_hcd)
46044 +{
46045 + return dwc_otg_hcd_get_priv_data(dwc_otg_hcd);
46046 +}
46047 +
46048 +/** Gets the usb_host_endpoint associated with an URB. */
46049 +inline struct usb_host_endpoint *dwc_urb_to_endpoint(struct urb *urb)
46050 +{
46051 + struct usb_device *dev = urb->dev;
46052 + int ep_num = usb_pipeendpoint(urb->pipe);
46053 +
46054 + if (usb_pipein(urb->pipe))
46055 + return dev->ep_in[ep_num];
46056 + else
46057 + return dev->ep_out[ep_num];
46058 +}
46059 +
46060 +static int _disconnect(dwc_otg_hcd_t * hcd)
46061 +{
46062 + struct usb_hcd *usb_hcd = dwc_otg_hcd_to_hcd(hcd);
46063 +
46064 + usb_hcd->self.is_b_host = 0;
46065 + return 0;
46066 +}
46067 +
46068 +static int _start(dwc_otg_hcd_t * hcd)
46069 +{
46070 + struct usb_hcd *usb_hcd = dwc_otg_hcd_to_hcd(hcd);
46071 +
46072 + usb_hcd->self.is_b_host = dwc_otg_hcd_is_b_host(hcd);
46073 + hcd_start(usb_hcd);
46074 +
46075 + return 0;
46076 +}
46077 +
46078 +static int _hub_info(dwc_otg_hcd_t * hcd, void *urb_handle, uint32_t * hub_addr,
46079 + uint32_t * port_addr)
46080 +{
46081 + struct urb *urb = (struct urb *)urb_handle;
46082 + struct usb_bus *bus;
46083 +#if 1 //GRAYG - temporary
46084 + if (NULL == urb_handle)
46085 + DWC_ERROR("**** %s - NULL URB handle\n", __func__);//GRAYG
46086 + if (NULL == urb->dev)
46087 + DWC_ERROR("**** %s - URB has no device\n", __func__);//GRAYG
46088 + if (NULL == port_addr)
46089 + DWC_ERROR("**** %s - NULL port_address\n", __func__);//GRAYG
46090 +#endif
46091 + if (urb->dev->tt) {
46092 + if (NULL == urb->dev->tt->hub) {
46093 + DWC_ERROR("**** %s - (URB's transactor has no TT - giving no hub)\n",
46094 + __func__); //GRAYG
46095 + //*hub_addr = (u8)usb_pipedevice(urb->pipe); //GRAYG
46096 + *hub_addr = 0; //GRAYG
46097 + // we probably shouldn't have a transaction translator if
46098 + // there's no associated hub?
46099 + } else {
46100 + bus = hcd_to_bus(dwc_otg_hcd_to_hcd(hcd));
46101 + if (urb->dev->tt->hub == bus->root_hub)
46102 + *hub_addr = 0;
46103 + else
46104 + *hub_addr = urb->dev->tt->hub->devnum;
46105 + }
46106 + *port_addr = urb->dev->tt->multi ? urb->dev->ttport : 1;
46107 + } else {
46108 + *hub_addr = 0;
46109 + *port_addr = urb->dev->ttport;
46110 + }
46111 + return 0;
46112 +}
46113 +
46114 +static int _speed(dwc_otg_hcd_t * hcd, void *urb_handle)
46115 +{
46116 + struct urb *urb = (struct urb *)urb_handle;
46117 + return urb->dev->speed;
46118 +}
46119 +
46120 +static int _get_b_hnp_enable(dwc_otg_hcd_t * hcd)
46121 +{
46122 + struct usb_hcd *usb_hcd = dwc_otg_hcd_to_hcd(hcd);
46123 + return usb_hcd->self.b_hnp_enable;
46124 +}
46125 +
46126 +static void allocate_bus_bandwidth(struct usb_hcd *hcd, uint32_t bw,
46127 + struct urb *urb)
46128 +{
46129 + hcd_to_bus(hcd)->bandwidth_allocated += bw / urb->interval;
46130 + if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
46131 + hcd_to_bus(hcd)->bandwidth_isoc_reqs++;
46132 + } else {
46133 + hcd_to_bus(hcd)->bandwidth_int_reqs++;
46134 + }
46135 +}
46136 +
46137 +static void free_bus_bandwidth(struct usb_hcd *hcd, uint32_t bw,
46138 + struct urb *urb)
46139 +{
46140 + hcd_to_bus(hcd)->bandwidth_allocated -= bw / urb->interval;
46141 + if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
46142 + hcd_to_bus(hcd)->bandwidth_isoc_reqs--;
46143 + } else {
46144 + hcd_to_bus(hcd)->bandwidth_int_reqs--;
46145 + }
46146 +}
46147 +
46148 +/**
46149 + * Sets the final status of an URB and returns it to the device driver. Any
46150 + * required cleanup of the URB is performed. The HCD lock should be held on
46151 + * entry.
46152 + */
46153 +static int _complete(dwc_otg_hcd_t * hcd, void *urb_handle,
46154 + dwc_otg_hcd_urb_t * dwc_otg_urb, int32_t status)
46155 +{
46156 + struct urb *urb = (struct urb *)urb_handle;
46157 + urb_tq_entry_t *new_entry;
46158 + int rc = 0;
46159 + if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
46160 + DWC_PRINTF("%s: urb %p, device %d, ep %d %s, status=%d\n",
46161 + __func__, urb, usb_pipedevice(urb->pipe),
46162 + usb_pipeendpoint(urb->pipe),
46163 + usb_pipein(urb->pipe) ? "IN" : "OUT", status);
46164 + if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
46165 + int i;
46166 + for (i = 0; i < urb->number_of_packets; i++) {
46167 + DWC_PRINTF(" ISO Desc %d status: %d\n",
46168 + i, urb->iso_frame_desc[i].status);
46169 + }
46170 + }
46171 + }
46172 + new_entry = DWC_ALLOC_ATOMIC(sizeof(urb_tq_entry_t));
46173 + urb->actual_length = dwc_otg_hcd_urb_get_actual_length(dwc_otg_urb);
46174 + /* Convert status value. */
46175 + switch (status) {
46176 + case -DWC_E_PROTOCOL:
46177 + status = -EPROTO;
46178 + break;
46179 + case -DWC_E_IN_PROGRESS:
46180 + status = -EINPROGRESS;
46181 + break;
46182 + case -DWC_E_PIPE:
46183 + status = -EPIPE;
46184 + break;
46185 + case -DWC_E_IO:
46186 + status = -EIO;
46187 + break;
46188 + case -DWC_E_TIMEOUT:
46189 + status = -ETIMEDOUT;
46190 + break;
46191 + case -DWC_E_OVERFLOW:
46192 + status = -EOVERFLOW;
46193 + break;
46194 + case -DWC_E_SHUTDOWN:
46195 + status = -ESHUTDOWN;
46196 + break;
46197 + default:
46198 + if (status) {
46199 + DWC_PRINTF("Uknown urb status %d\n", status);
46200 +
46201 + }
46202 + }
46203 +
46204 + if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
46205 + int i;
46206 +
46207 + urb->error_count = dwc_otg_hcd_urb_get_error_count(dwc_otg_urb);
46208 + urb->actual_length = 0;
46209 + for (i = 0; i < urb->number_of_packets; ++i) {
46210 + urb->iso_frame_desc[i].actual_length =
46211 + dwc_otg_hcd_urb_get_iso_desc_actual_length
46212 + (dwc_otg_urb, i);
46213 + urb->actual_length += urb->iso_frame_desc[i].actual_length;
46214 + urb->iso_frame_desc[i].status =
46215 + dwc_otg_hcd_urb_get_iso_desc_status(dwc_otg_urb, i);
46216 + }
46217 + }
46218 +
46219 + urb->status = status;
46220 + urb->hcpriv = NULL;
46221 + if (!status) {
46222 + if ((urb->transfer_flags & URB_SHORT_NOT_OK) &&
46223 + (urb->actual_length < urb->transfer_buffer_length)) {
46224 + urb->status = -EREMOTEIO;
46225 + }
46226 + }
46227 +
46228 + if ((usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) ||
46229 + (usb_pipetype(urb->pipe) == PIPE_INTERRUPT)) {
46230 + struct usb_host_endpoint *ep = dwc_urb_to_endpoint(urb);
46231 + if (ep) {
46232 + free_bus_bandwidth(dwc_otg_hcd_to_hcd(hcd),
46233 + dwc_otg_hcd_get_ep_bandwidth(hcd,
46234 + ep->hcpriv),
46235 + urb);
46236 + }
46237 + }
46238 + DWC_FREE(dwc_otg_urb);
46239 + if (!new_entry) {
46240 + DWC_ERROR("dwc_otg_hcd: complete: cannot allocate URB TQ entry\n");
46241 + urb->status = -EPROTO;
46242 + /* don't schedule the tasklet -
46243 + * directly return the packet here with error. */
46244 +#if USB_URB_EP_LINKING
46245 + usb_hcd_unlink_urb_from_ep(dwc_otg_hcd_to_hcd(hcd), urb);
46246 +#endif
46247 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
46248 + usb_hcd_giveback_urb(dwc_otg_hcd_to_hcd(hcd), urb);
46249 +#else
46250 + usb_hcd_giveback_urb(dwc_otg_hcd_to_hcd(hcd), urb, urb->status);
46251 +#endif
46252 + } else {
46253 + new_entry->urb = urb;
46254 +#if USB_URB_EP_LINKING
46255 + rc = usb_hcd_check_unlink_urb(dwc_otg_hcd_to_hcd(hcd), urb, urb->status);
46256 + if(0 == rc) {
46257 + usb_hcd_unlink_urb_from_ep(dwc_otg_hcd_to_hcd(hcd), urb);
46258 + }
46259 +#endif
46260 + if(0 == rc) {
46261 + DWC_TAILQ_INSERT_TAIL(&hcd->completed_urb_list, new_entry,
46262 + urb_tq_entries);
46263 + DWC_TASK_HI_SCHEDULE(hcd->completion_tasklet);
46264 + }
46265 + }
46266 + return 0;
46267 +}
46268 +
46269 +static struct dwc_otg_hcd_function_ops hcd_fops = {
46270 + .start = _start,
46271 + .disconnect = _disconnect,
46272 + .hub_info = _hub_info,
46273 + .speed = _speed,
46274 + .complete = _complete,
46275 + .get_b_hnp_enable = _get_b_hnp_enable,
46276 +};
46277 +
46278 +static struct fiq_handler fh = {
46279 + .name = "usb_fiq",
46280 +};
46281 +
46282 +static void hcd_init_fiq(void *cookie)
46283 +{
46284 + dwc_otg_device_t *otg_dev = cookie;
46285 + dwc_otg_hcd_t *dwc_otg_hcd = otg_dev->hcd;
46286 + struct pt_regs regs;
46287 + int irq;
46288 +
46289 + if (claim_fiq(&fh)) {
46290 + DWC_ERROR("Can't claim FIQ");
46291 + BUG();
46292 + }
46293 + DWC_WARN("FIQ on core %d at 0x%08x",
46294 + smp_processor_id(),
46295 + (fiq_fsm_enable ? (int)&dwc_otg_fiq_fsm : (int)&dwc_otg_fiq_nop));
46296 + DWC_WARN("FIQ ASM at 0x%08x length %d", (int)&_dwc_otg_fiq_stub, (int)(&_dwc_otg_fiq_stub_end - &_dwc_otg_fiq_stub));
46297 + set_fiq_handler((void *) &_dwc_otg_fiq_stub, &_dwc_otg_fiq_stub_end - &_dwc_otg_fiq_stub);
46298 + memset(&regs,0,sizeof(regs));
46299 +
46300 + regs.ARM_r8 = (long) dwc_otg_hcd->fiq_state;
46301 + if (fiq_fsm_enable) {
46302 + regs.ARM_r9 = dwc_otg_hcd->core_if->core_params->host_channels;
46303 + //regs.ARM_r10 = dwc_otg_hcd->dma;
46304 + regs.ARM_fp = (long) dwc_otg_fiq_fsm;
46305 + } else {
46306 + regs.ARM_fp = (long) dwc_otg_fiq_nop;
46307 + }
46308 +
46309 + regs.ARM_sp = (long) dwc_otg_hcd->fiq_stack + (sizeof(struct fiq_stack) - 4);
46310 +
46311 +// __show_regs(&regs);
46312 + set_fiq_regs(&regs);
46313 +
46314 + //Set the mphi periph to the required registers
46315 + dwc_otg_hcd->fiq_state->mphi_regs.base = otg_dev->os_dep.mphi_base;
46316 + dwc_otg_hcd->fiq_state->mphi_regs.ctrl = otg_dev->os_dep.mphi_base + 0x4c;
46317 + dwc_otg_hcd->fiq_state->mphi_regs.outdda = otg_dev->os_dep.mphi_base + 0x28;
46318 + dwc_otg_hcd->fiq_state->mphi_regs.outddb = otg_dev->os_dep.mphi_base + 0x2c;
46319 + dwc_otg_hcd->fiq_state->mphi_regs.intstat = otg_dev->os_dep.mphi_base + 0x50;
46320 + dwc_otg_hcd->fiq_state->dwc_regs_base = otg_dev->os_dep.base;
46321 + DWC_WARN("MPHI regs_base at 0x%08x", (int)dwc_otg_hcd->fiq_state->mphi_regs.base);
46322 + //Enable mphi peripheral
46323 + writel((1<<31),dwc_otg_hcd->fiq_state->mphi_regs.ctrl);
46324 +#ifdef DEBUG
46325 + if (readl(dwc_otg_hcd->fiq_state->mphi_regs.ctrl) & 0x80000000)
46326 + DWC_WARN("MPHI periph has been enabled");
46327 + else
46328 + DWC_WARN("MPHI periph has NOT been enabled");
46329 +#endif
46330 + // Enable FIQ interrupt from USB peripheral
46331 +#ifdef CONFIG_MULTI_IRQ_HANDLER
46332 + irq = platform_get_irq(otg_dev->os_dep.platformdev, 1);
46333 +#else
46334 + irq = INTERRUPT_VC_USB;
46335 +#endif
46336 + if (irq < 0) {
46337 + DWC_ERROR("Can't get FIQ irq");
46338 + return;
46339 + }
46340 + enable_fiq(irq);
46341 + local_fiq_enable();
46342 +}
46343 +
46344 +/**
46345 + * Initializes the HCD. This function allocates memory for and initializes the
46346 + * static parts of the usb_hcd and dwc_otg_hcd structures. It also registers the
46347 + * USB bus with the core and calls the hc_driver->start() function. It returns
46348 + * a negative error on failure.
46349 + */
46350 +int hcd_init(dwc_bus_dev_t *_dev)
46351 +{
46352 + struct usb_hcd *hcd = NULL;
46353 + dwc_otg_hcd_t *dwc_otg_hcd = NULL;
46354 + dwc_otg_device_t *otg_dev = DWC_OTG_BUSDRVDATA(_dev);
46355 + int retval = 0;
46356 + u64 dmamask;
46357 +
46358 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD INIT otg_dev=%p\n", otg_dev);
46359 +
46360 + /* Set device flags indicating whether the HCD supports DMA. */
46361 + if (dwc_otg_is_dma_enable(otg_dev->core_if))
46362 + dmamask = DMA_BIT_MASK(32);
46363 + else
46364 + dmamask = 0;
46365 +
46366 +#if defined(LM_INTERFACE) || defined(PLATFORM_INTERFACE)
46367 + dma_set_mask(&_dev->dev, dmamask);
46368 + dma_set_coherent_mask(&_dev->dev, dmamask);
46369 +#elif defined(PCI_INTERFACE)
46370 + pci_set_dma_mask(_dev, dmamask);
46371 + pci_set_consistent_dma_mask(_dev, dmamask);
46372 +#endif
46373 +
46374 + /*
46375 + * Allocate memory for the base HCD plus the DWC OTG HCD.
46376 + * Initialize the base HCD.
46377 + */
46378 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30)
46379 + hcd = usb_create_hcd(&dwc_otg_hc_driver, &_dev->dev, _dev->dev.bus_id);
46380 +#else
46381 + hcd = usb_create_hcd(&dwc_otg_hc_driver, &_dev->dev, dev_name(&_dev->dev));
46382 + hcd->has_tt = 1;
46383 +// hcd->uses_new_polling = 1;
46384 +// hcd->poll_rh = 0;
46385 +#endif
46386 + if (!hcd) {
46387 + retval = -ENOMEM;
46388 + goto error1;
46389 + }
46390 +
46391 + hcd->regs = otg_dev->os_dep.base;
46392 +
46393 +
46394 + /* Initialize the DWC OTG HCD. */
46395 + dwc_otg_hcd = dwc_otg_hcd_alloc_hcd();
46396 + if (!dwc_otg_hcd) {
46397 + goto error2;
46398 + }
46399 + ((struct wrapper_priv_data *)(hcd->hcd_priv))->dwc_otg_hcd =
46400 + dwc_otg_hcd;
46401 + otg_dev->hcd = dwc_otg_hcd;
46402 + otg_dev->hcd->otg_dev = otg_dev;
46403 +
46404 + if (dwc_otg_hcd_init(dwc_otg_hcd, otg_dev->core_if)) {
46405 + goto error2;
46406 + }
46407 +
46408 + if (fiq_enable) {
46409 + if (num_online_cpus() > 1) {
46410 + /* bcm2709: can run the FIQ on a separate core to IRQs */
46411 + smp_call_function_single(1, hcd_init_fiq, otg_dev, 1);
46412 + } else {
46413 + smp_call_function_single(0, hcd_init_fiq, otg_dev, 1);
46414 + }
46415 + }
46416 +
46417 + hcd->self.otg_port = dwc_otg_hcd_otg_port(dwc_otg_hcd);
46418 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,33) //don't support for LM(with 2.6.20.1 kernel)
46419 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,35) //version field absent later
46420 + hcd->self.otg_version = dwc_otg_get_otg_version(otg_dev->core_if);
46421 +#endif
46422 + /* Don't support SG list at this point */
46423 + hcd->self.sg_tablesize = 0;
46424 +#endif
46425 + /*
46426 + * Finish generic HCD initialization and start the HCD. This function
46427 + * allocates the DMA buffer pool, registers the USB bus, requests the
46428 + * IRQ line, and calls hcd_start method.
46429 + */
46430 +#ifdef PLATFORM_INTERFACE
46431 + retval = usb_add_hcd(hcd, platform_get_irq(_dev, fiq_enable ? 0 : 1), IRQF_SHARED);
46432 +#else
46433 + retval = usb_add_hcd(hcd, _dev->irq, IRQF_SHARED);
46434 +#endif
46435 + if (retval < 0) {
46436 + goto error2;
46437 + }
46438 +
46439 + dwc_otg_hcd_set_priv_data(dwc_otg_hcd, hcd);
46440 + return 0;
46441 +
46442 +error2:
46443 + usb_put_hcd(hcd);
46444 +error1:
46445 + return retval;
46446 +}
46447 +
46448 +/**
46449 + * Removes the HCD.
46450 + * Frees memory and resources associated with the HCD and deregisters the bus.
46451 + */
46452 +void hcd_remove(dwc_bus_dev_t *_dev)
46453 +{
46454 + dwc_otg_device_t *otg_dev = DWC_OTG_BUSDRVDATA(_dev);
46455 + dwc_otg_hcd_t *dwc_otg_hcd;
46456 + struct usb_hcd *hcd;
46457 +
46458 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD REMOVE otg_dev=%p\n", otg_dev);
46459 +
46460 + if (!otg_dev) {
46461 + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev NULL!\n", __func__);
46462 + return;
46463 + }
46464 +
46465 + dwc_otg_hcd = otg_dev->hcd;
46466 +
46467 + if (!dwc_otg_hcd) {
46468 + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev->hcd NULL!\n", __func__);
46469 + return;
46470 + }
46471 +
46472 + hcd = dwc_otg_hcd_to_hcd(dwc_otg_hcd);
46473 +
46474 + if (!hcd) {
46475 + DWC_DEBUGPL(DBG_ANY,
46476 + "%s: dwc_otg_hcd_to_hcd(dwc_otg_hcd) NULL!\n",
46477 + __func__);
46478 + return;
46479 + }
46480 + usb_remove_hcd(hcd);
46481 + dwc_otg_hcd_set_priv_data(dwc_otg_hcd, NULL);
46482 + dwc_otg_hcd_remove(dwc_otg_hcd);
46483 + usb_put_hcd(hcd);
46484 +}
46485 +
46486 +/* =========================================================================
46487 + * Linux HC Driver Functions
46488 + * ========================================================================= */
46489 +
46490 +/** Initializes the DWC_otg controller and its root hub and prepares it for host
46491 + * mode operation. Activates the root port. Returns 0 on success and a negative
46492 + * error code on failure. */
46493 +int hcd_start(struct usb_hcd *hcd)
46494 +{
46495 + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
46496 + struct usb_bus *bus;
46497 +
46498 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD START\n");
46499 + bus = hcd_to_bus(hcd);
46500 +
46501 + hcd->state = HC_STATE_RUNNING;
46502 + if (dwc_otg_hcd_start(dwc_otg_hcd, &hcd_fops)) {
46503 + return 0;
46504 + }
46505 +
46506 + /* Initialize and connect root hub if one is not already attached */
46507 + if (bus->root_hub) {
46508 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD Has Root Hub\n");
46509 + /* Inform the HUB driver to resume. */
46510 + usb_hcd_resume_root_hub(hcd);
46511 + }
46512 +
46513 + return 0;
46514 +}
46515 +
46516 +/**
46517 + * Halts the DWC_otg host mode operations in a clean manner. USB transfers are
46518 + * stopped.
46519 + */
46520 +void hcd_stop(struct usb_hcd *hcd)
46521 +{
46522 + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
46523 +
46524 + dwc_otg_hcd_stop(dwc_otg_hcd);
46525 +}
46526 +
46527 +/** Returns the current frame number. */
46528 +static int get_frame_number(struct usb_hcd *hcd)
46529 +{
46530 + hprt0_data_t hprt0;
46531 + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
46532 + hprt0.d32 = DWC_READ_REG32(dwc_otg_hcd->core_if->host_if->hprt0);
46533 + if (hprt0.b.prtspd == DWC_HPRT0_PRTSPD_HIGH_SPEED)
46534 + return dwc_otg_hcd_get_frame_number(dwc_otg_hcd) >> 3;
46535 + else
46536 + return dwc_otg_hcd_get_frame_number(dwc_otg_hcd);
46537 +}
46538 +
46539 +#ifdef DEBUG
46540 +static void dump_urb_info(struct urb *urb, char *fn_name)
46541 +{
46542 + DWC_PRINTF("%s, urb %p\n", fn_name, urb);
46543 + DWC_PRINTF(" Device address: %d\n", usb_pipedevice(urb->pipe));
46544 + DWC_PRINTF(" Endpoint: %d, %s\n", usb_pipeendpoint(urb->pipe),
46545 + (usb_pipein(urb->pipe) ? "IN" : "OUT"));
46546 + DWC_PRINTF(" Endpoint type: %s\n", ( {
46547 + char *pipetype;
46548 + switch (usb_pipetype(urb->pipe)) {
46549 +case PIPE_CONTROL:
46550 +pipetype = "CONTROL"; break; case PIPE_BULK:
46551 +pipetype = "BULK"; break; case PIPE_INTERRUPT:
46552 +pipetype = "INTERRUPT"; break; case PIPE_ISOCHRONOUS:
46553 +pipetype = "ISOCHRONOUS"; break; default:
46554 + pipetype = "UNKNOWN"; break;};
46555 + pipetype;}
46556 + )) ;
46557 + DWC_PRINTF(" Speed: %s\n", ( {
46558 + char *speed; switch (urb->dev->speed) {
46559 +case USB_SPEED_HIGH:
46560 +speed = "HIGH"; break; case USB_SPEED_FULL:
46561 +speed = "FULL"; break; case USB_SPEED_LOW:
46562 +speed = "LOW"; break; default:
46563 + speed = "UNKNOWN"; break;};
46564 + speed;}
46565 + )) ;
46566 + DWC_PRINTF(" Max packet size: %d\n",
46567 + usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe)));
46568 + DWC_PRINTF(" Data buffer length: %d\n", urb->transfer_buffer_length);
46569 + DWC_PRINTF(" Transfer buffer: %p, Transfer DMA: %p\n",
46570 + urb->transfer_buffer, (void *)urb->transfer_dma);
46571 + DWC_PRINTF(" Setup buffer: %p, Setup DMA: %p\n",
46572 + urb->setup_packet, (void *)urb->setup_dma);
46573 + DWC_PRINTF(" Interval: %d\n", urb->interval);
46574 + if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
46575 + int i;
46576 + for (i = 0; i < urb->number_of_packets; i++) {
46577 + DWC_PRINTF(" ISO Desc %d:\n", i);
46578 + DWC_PRINTF(" offset: %d, length %d\n",
46579 + urb->iso_frame_desc[i].offset,
46580 + urb->iso_frame_desc[i].length);
46581 + }
46582 + }
46583 +}
46584 +#endif
46585 +
46586 +/** Starts processing a USB transfer request specified by a USB Request Block
46587 + * (URB). mem_flags indicates the type of memory allocation to use while
46588 + * processing this URB. */
46589 +static int dwc_otg_urb_enqueue(struct usb_hcd *hcd,
46590 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
46591 + struct usb_host_endpoint *ep,
46592 +#endif
46593 + struct urb *urb, gfp_t mem_flags)
46594 +{
46595 + int retval = 0;
46596 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,28)
46597 + struct usb_host_endpoint *ep = urb->ep;
46598 +#endif
46599 + dwc_irqflags_t irqflags;
46600 + void **ref_ep_hcpriv = &ep->hcpriv;
46601 + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
46602 + dwc_otg_hcd_urb_t *dwc_otg_urb;
46603 + int i;
46604 + int alloc_bandwidth = 0;
46605 + uint8_t ep_type = 0;
46606 + uint32_t flags = 0;
46607 + void *buf;
46608 +
46609 +#ifdef DEBUG
46610 + if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
46611 + dump_urb_info(urb, "dwc_otg_urb_enqueue");
46612 + }
46613 +#endif
46614 +
46615 + if (!urb->transfer_buffer && urb->transfer_buffer_length)
46616 + return -EINVAL;
46617 +
46618 + if ((usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
46619 + || (usb_pipetype(urb->pipe) == PIPE_INTERRUPT)) {
46620 + if (!dwc_otg_hcd_is_bandwidth_allocated
46621 + (dwc_otg_hcd, ref_ep_hcpriv)) {
46622 + alloc_bandwidth = 1;
46623 + }
46624 + }
46625 +
46626 + switch (usb_pipetype(urb->pipe)) {
46627 + case PIPE_CONTROL:
46628 + ep_type = USB_ENDPOINT_XFER_CONTROL;
46629 + break;
46630 + case PIPE_ISOCHRONOUS:
46631 + ep_type = USB_ENDPOINT_XFER_ISOC;
46632 + break;
46633 + case PIPE_BULK:
46634 + ep_type = USB_ENDPOINT_XFER_BULK;
46635 + break;
46636 + case PIPE_INTERRUPT:
46637 + ep_type = USB_ENDPOINT_XFER_INT;
46638 + break;
46639 + default:
46640 + DWC_WARN("Wrong EP type - %d\n", usb_pipetype(urb->pipe));
46641 + }
46642 +
46643 + /* # of packets is often 0 - do we really need to call this then? */
46644 + dwc_otg_urb = dwc_otg_hcd_urb_alloc(dwc_otg_hcd,
46645 + urb->number_of_packets,
46646 + mem_flags == GFP_ATOMIC ? 1 : 0);
46647 +
46648 + if(dwc_otg_urb == NULL)
46649 + return -ENOMEM;
46650 +
46651 + if (!dwc_otg_urb && urb->number_of_packets)
46652 + return -ENOMEM;
46653 +
46654 + dwc_otg_hcd_urb_set_pipeinfo(dwc_otg_urb, usb_pipedevice(urb->pipe),
46655 + usb_pipeendpoint(urb->pipe), ep_type,
46656 + usb_pipein(urb->pipe),
46657 + usb_maxpacket(urb->dev, urb->pipe,
46658 + !(usb_pipein(urb->pipe))));
46659 +
46660 + buf = urb->transfer_buffer;
46661 + if (hcd->self.uses_dma && !buf && urb->transfer_buffer_length) {
46662 + /*
46663 + * Calculate virtual address from physical address,
46664 + * because some class driver may not fill transfer_buffer.
46665 + * In Buffer DMA mode virual address is used,
46666 + * when handling non DWORD aligned buffers.
46667 + */
46668 + buf = (void *)__bus_to_virt((unsigned long)urb->transfer_dma);
46669 + dev_warn_once(&urb->dev->dev,
46670 + "USB transfer_buffer was NULL, will use __bus_to_virt(%pad)=%p\n",
46671 + &urb->transfer_dma, buf);
46672 + }
46673 +
46674 + if (!(urb->transfer_flags & URB_NO_INTERRUPT))
46675 + flags |= URB_GIVEBACK_ASAP;
46676 + if (urb->transfer_flags & URB_ZERO_PACKET)
46677 + flags |= URB_SEND_ZERO_PACKET;
46678 +
46679 + dwc_otg_hcd_urb_set_params(dwc_otg_urb, urb, buf,
46680 + urb->transfer_dma,
46681 + urb->transfer_buffer_length,
46682 + urb->setup_packet,
46683 + urb->setup_dma, flags, urb->interval);
46684 +
46685 + for (i = 0; i < urb->number_of_packets; ++i) {
46686 + dwc_otg_hcd_urb_set_iso_desc_params(dwc_otg_urb, i,
46687 + urb->
46688 + iso_frame_desc[i].offset,
46689 + urb->
46690 + iso_frame_desc[i].length);
46691 + }
46692 +
46693 + DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &irqflags);
46694 + urb->hcpriv = dwc_otg_urb;
46695 +#if USB_URB_EP_LINKING
46696 + retval = usb_hcd_link_urb_to_ep(hcd, urb);
46697 + if (0 == retval)
46698 +#endif
46699 + {
46700 + retval = dwc_otg_hcd_urb_enqueue(dwc_otg_hcd, dwc_otg_urb,
46701 + /*(dwc_otg_qh_t **)*/
46702 + ref_ep_hcpriv, 1);
46703 + if (0 == retval) {
46704 + if (alloc_bandwidth) {
46705 + allocate_bus_bandwidth(hcd,
46706 + dwc_otg_hcd_get_ep_bandwidth(
46707 + dwc_otg_hcd, *ref_ep_hcpriv),
46708 + urb);
46709 + }
46710 + } else {
46711 + DWC_DEBUGPL(DBG_HCD, "DWC OTG dwc_otg_hcd_urb_enqueue failed rc %d\n", retval);
46712 +#if USB_URB_EP_LINKING
46713 + usb_hcd_unlink_urb_from_ep(hcd, urb);
46714 +#endif
46715 + DWC_FREE(dwc_otg_urb);
46716 + urb->hcpriv = NULL;
46717 + if (retval == -DWC_E_NO_DEVICE)
46718 + retval = -ENODEV;
46719 + }
46720 + }
46721 +#if USB_URB_EP_LINKING
46722 + else
46723 + {
46724 + DWC_FREE(dwc_otg_urb);
46725 + urb->hcpriv = NULL;
46726 + }
46727 +#endif
46728 + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, irqflags);
46729 + return retval;
46730 +}
46731 +
46732 +/** Aborts/cancels a USB transfer request. Always returns 0 to indicate
46733 + * success. */
46734 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
46735 +static int dwc_otg_urb_dequeue(struct usb_hcd *hcd, struct urb *urb)
46736 +#else
46737 +static int dwc_otg_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
46738 +#endif
46739 +{
46740 + dwc_irqflags_t flags;
46741 + dwc_otg_hcd_t *dwc_otg_hcd;
46742 + int rc;
46743 +
46744 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD URB Dequeue\n");
46745 +
46746 + dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
46747 +
46748 +#ifdef DEBUG
46749 + if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
46750 + dump_urb_info(urb, "dwc_otg_urb_dequeue");
46751 + }
46752 +#endif
46753 +
46754 + DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &flags);
46755 + rc = usb_hcd_check_unlink_urb(hcd, urb, status);
46756 + if (0 == rc) {
46757 + if(urb->hcpriv != NULL) {
46758 + dwc_otg_hcd_urb_dequeue(dwc_otg_hcd,
46759 + (dwc_otg_hcd_urb_t *)urb->hcpriv);
46760 +
46761 + DWC_FREE(urb->hcpriv);
46762 + urb->hcpriv = NULL;
46763 + }
46764 + }
46765 +
46766 + if (0 == rc) {
46767 + /* Higher layer software sets URB status. */
46768 +#if USB_URB_EP_LINKING
46769 + usb_hcd_unlink_urb_from_ep(hcd, urb);
46770 +#endif
46771 + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, flags);
46772 +
46773 +
46774 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
46775 + usb_hcd_giveback_urb(hcd, urb);
46776 +#else
46777 + usb_hcd_giveback_urb(hcd, urb, status);
46778 +#endif
46779 + if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
46780 + DWC_PRINTF("Called usb_hcd_giveback_urb() \n");
46781 + DWC_PRINTF(" 1urb->status = %d\n", urb->status);
46782 + }
46783 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD URB Dequeue OK\n");
46784 + } else {
46785 + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, flags);
46786 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD URB Dequeue failed - rc %d\n",
46787 + rc);
46788 + }
46789 +
46790 + return rc;
46791 +}
46792 +
46793 +/* Frees resources in the DWC_otg controller related to a given endpoint. Also
46794 + * clears state in the HCD related to the endpoint. Any URBs for the endpoint
46795 + * must already be dequeued. */
46796 +static void endpoint_disable(struct usb_hcd *hcd, struct usb_host_endpoint *ep)
46797 +{
46798 + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
46799 +
46800 + DWC_DEBUGPL(DBG_HCD,
46801 + "DWC OTG HCD EP DISABLE: _bEndpointAddress=0x%02x, "
46802 + "endpoint=%d\n", ep->desc.bEndpointAddress,
46803 + dwc_ep_addr_to_endpoint(ep->desc.bEndpointAddress));
46804 + dwc_otg_hcd_endpoint_disable(dwc_otg_hcd, ep->hcpriv, 250);
46805 + ep->hcpriv = NULL;
46806 +}
46807 +
46808 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30)
46809 +/* Resets endpoint specific parameter values, in current version used to reset
46810 + * the data toggle(as a WA). This function can be called from usb_clear_halt routine */
46811 +static void endpoint_reset(struct usb_hcd *hcd, struct usb_host_endpoint *ep)
46812 +{
46813 + dwc_irqflags_t flags;
46814 + struct usb_device *udev = NULL;
46815 + int epnum = usb_endpoint_num(&ep->desc);
46816 + int is_out = usb_endpoint_dir_out(&ep->desc);
46817 + int is_control = usb_endpoint_xfer_control(&ep->desc);
46818 + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
46819 + struct device *dev = DWC_OTG_OS_GETDEV(dwc_otg_hcd->otg_dev->os_dep);
46820 +
46821 + if (dev)
46822 + udev = to_usb_device(dev);
46823 + else
46824 + return;
46825 +
46826 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD EP RESET: Endpoint Num=0x%02d\n", epnum);
46827 +
46828 + DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &flags);
46829 + usb_settoggle(udev, epnum, is_out, 0);
46830 + if (is_control)
46831 + usb_settoggle(udev, epnum, !is_out, 0);
46832 +
46833 + if (ep->hcpriv) {
46834 + dwc_otg_hcd_endpoint_reset(dwc_otg_hcd, ep->hcpriv);
46835 + }
46836 + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, flags);
46837 +}
46838 +#endif
46839 +
46840 +/** Handles host mode interrupts for the DWC_otg controller. Returns IRQ_NONE if
46841 + * there was no interrupt to handle. Returns IRQ_HANDLED if there was a valid
46842 + * interrupt.
46843 + *
46844 + * This function is called by the USB core when an interrupt occurs */
46845 +static irqreturn_t dwc_otg_hcd_irq(struct usb_hcd *hcd)
46846 +{
46847 + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
46848 + int32_t retval = dwc_otg_hcd_handle_intr(dwc_otg_hcd);
46849 + if (retval != 0) {
46850 + S3C2410X_CLEAR_EINTPEND();
46851 + }
46852 + return IRQ_RETVAL(retval);
46853 +}
46854 +
46855 +/** Creates Status Change bitmap for the root hub and root port. The bitmap is
46856 + * returned in buf. Bit 0 is the status change indicator for the root hub. Bit 1
46857 + * is the status change indicator for the single root port. Returns 1 if either
46858 + * change indicator is 1, otherwise returns 0. */
46859 +int hub_status_data(struct usb_hcd *hcd, char *buf)
46860 +{
46861 + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
46862 +
46863 + buf[0] = 0;
46864 + buf[0] |= (dwc_otg_hcd_is_status_changed(dwc_otg_hcd, 1)) << 1;
46865 +
46866 + return (buf[0] != 0);
46867 +}
46868 +
46869 +/** Handles hub class-specific requests. */
46870 +int hub_control(struct usb_hcd *hcd,
46871 + u16 typeReq, u16 wValue, u16 wIndex, char *buf, u16 wLength)
46872 +{
46873 + int retval;
46874 +
46875 + retval = dwc_otg_hcd_hub_control(hcd_to_dwc_otg_hcd(hcd),
46876 + typeReq, wValue, wIndex, buf, wLength);
46877 +
46878 + switch (retval) {
46879 + case -DWC_E_INVALID:
46880 + retval = -EINVAL;
46881 + break;
46882 + }
46883 +
46884 + return retval;
46885 +}
46886 +
46887 +#endif /* DWC_DEVICE_ONLY */
46888 --- /dev/null
46889 +++ b/drivers/usb/host/dwc_otg/dwc_otg_hcd_queue.c
46890 @@ -0,0 +1,971 @@
46891 +/* ==========================================================================
46892 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd_queue.c $
46893 + * $Revision: #44 $
46894 + * $Date: 2011/10/26 $
46895 + * $Change: 1873028 $
46896 + *
46897 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
46898 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
46899 + * otherwise expressly agreed to in writing between Synopsys and you.
46900 + *
46901 + * The Software IS NOT an item of Licensed Software or Licensed Product under
46902 + * any End User Software License Agreement or Agreement for Licensed Product
46903 + * with Synopsys or any supplement thereto. You are permitted to use and
46904 + * redistribute this Software in source and binary forms, with or without
46905 + * modification, provided that redistributions of source code must retain this
46906 + * notice. You may not view, use, disclose, copy or distribute this file or
46907 + * any information contained herein except pursuant to this license grant from
46908 + * Synopsys. If you do not agree with this notice, including the disclaimer
46909 + * below, then you are not authorized to use the Software.
46910 + *
46911 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
46912 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46913 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46914 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
46915 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
46916 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
46917 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
46918 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
46919 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
46920 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
46921 + * DAMAGE.
46922 + * ========================================================================== */
46923 +#ifndef DWC_DEVICE_ONLY
46924 +
46925 +/**
46926 + * @file
46927 + *
46928 + * This file contains the functions to manage Queue Heads and Queue
46929 + * Transfer Descriptors.
46930 + */
46931 +
46932 +#include "dwc_otg_hcd.h"
46933 +#include "dwc_otg_regs.h"
46934 +
46935 +extern bool microframe_schedule;
46936 +extern unsigned short int_ep_interval_min;
46937 +
46938 +/**
46939 + * Free each QTD in the QH's QTD-list then free the QH. QH should already be
46940 + * removed from a list. QTD list should already be empty if called from URB
46941 + * Dequeue.
46942 + *
46943 + * @param hcd HCD instance.
46944 + * @param qh The QH to free.
46945 + */
46946 +void dwc_otg_hcd_qh_free(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
46947 +{
46948 + dwc_otg_qtd_t *qtd, *qtd_tmp;
46949 + dwc_irqflags_t flags;
46950 + uint32_t buf_size = 0;
46951 + uint8_t *align_buf_virt = NULL;
46952 + dwc_dma_t align_buf_dma;
46953 + struct device *dev = dwc_otg_hcd_to_dev(hcd);
46954 +
46955 + /* Free each QTD in the QTD list */
46956 + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
46957 + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &qh->qtd_list, qtd_list_entry) {
46958 + DWC_CIRCLEQ_REMOVE(&qh->qtd_list, qtd, qtd_list_entry);
46959 + dwc_otg_hcd_qtd_free(qtd);
46960 + }
46961 +
46962 + if (hcd->core_if->dma_desc_enable) {
46963 + dwc_otg_hcd_qh_free_ddma(hcd, qh);
46964 + } else if (qh->dw_align_buf) {
46965 + if (qh->ep_type == UE_ISOCHRONOUS) {
46966 + buf_size = 4096;
46967 + } else {
46968 + buf_size = hcd->core_if->core_params->max_transfer_size;
46969 + }
46970 + align_buf_virt = qh->dw_align_buf;
46971 + align_buf_dma = qh->dw_align_buf_dma;
46972 + }
46973 +
46974 + DWC_FREE(qh);
46975 + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
46976 + if (align_buf_virt)
46977 + DWC_DMA_FREE(dev, buf_size, align_buf_virt, align_buf_dma);
46978 + return;
46979 +}
46980 +
46981 +#define BitStuffTime(bytecount) ((8 * 7* bytecount) / 6)
46982 +#define HS_HOST_DELAY 5 /* nanoseconds */
46983 +#define FS_LS_HOST_DELAY 1000 /* nanoseconds */
46984 +#define HUB_LS_SETUP 333 /* nanoseconds */
46985 +#define NS_TO_US(ns) ((ns + 500) / 1000)
46986 + /* convert & round nanoseconds to microseconds */
46987 +
46988 +static uint32_t calc_bus_time(int speed, int is_in, int is_isoc, int bytecount)
46989 +{
46990 + unsigned long retval;
46991 +
46992 + switch (speed) {
46993 + case USB_SPEED_HIGH:
46994 + if (is_isoc) {
46995 + retval =
46996 + ((38 * 8 * 2083) +
46997 + (2083 * (3 + BitStuffTime(bytecount)))) / 1000 +
46998 + HS_HOST_DELAY;
46999 + } else {
47000 + retval =
47001 + ((55 * 8 * 2083) +
47002 + (2083 * (3 + BitStuffTime(bytecount)))) / 1000 +
47003 + HS_HOST_DELAY;
47004 + }
47005 + break;
47006 + case USB_SPEED_FULL:
47007 + if (is_isoc) {
47008 + retval =
47009 + (8354 * (31 + 10 * BitStuffTime(bytecount))) / 1000;
47010 + if (is_in) {
47011 + retval = 7268 + FS_LS_HOST_DELAY + retval;
47012 + } else {
47013 + retval = 6265 + FS_LS_HOST_DELAY + retval;
47014 + }
47015 + } else {
47016 + retval =
47017 + (8354 * (31 + 10 * BitStuffTime(bytecount))) / 1000;
47018 + retval = 9107 + FS_LS_HOST_DELAY + retval;
47019 + }
47020 + break;
47021 + case USB_SPEED_LOW:
47022 + if (is_in) {
47023 + retval =
47024 + (67667 * (31 + 10 * BitStuffTime(bytecount))) /
47025 + 1000;
47026 + retval =
47027 + 64060 + (2 * HUB_LS_SETUP) + FS_LS_HOST_DELAY +
47028 + retval;
47029 + } else {
47030 + retval =
47031 + (66700 * (31 + 10 * BitStuffTime(bytecount))) /
47032 + 1000;
47033 + retval =
47034 + 64107 + (2 * HUB_LS_SETUP) + FS_LS_HOST_DELAY +
47035 + retval;
47036 + }
47037 + break;
47038 + default:
47039 + DWC_WARN("Unknown device speed\n");
47040 + retval = -1;
47041 + }
47042 +
47043 + return NS_TO_US(retval);
47044 +}
47045 +
47046 +/**
47047 + * Initializes a QH structure.
47048 + *
47049 + * @param hcd The HCD state structure for the DWC OTG controller.
47050 + * @param qh The QH to init.
47051 + * @param urb Holds the information about the device/endpoint that we need
47052 + * to initialize the QH.
47053 + */
47054 +#define SCHEDULE_SLOP 10
47055 +void qh_init(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh, dwc_otg_hcd_urb_t * urb)
47056 +{
47057 + char *speed, *type;
47058 + int dev_speed;
47059 + uint32_t hub_addr, hub_port;
47060 +
47061 + dwc_memset(qh, 0, sizeof(dwc_otg_qh_t));
47062 +
47063 + /* Initialize QH */
47064 + qh->ep_type = dwc_otg_hcd_get_pipe_type(&urb->pipe_info);
47065 + qh->ep_is_in = dwc_otg_hcd_is_pipe_in(&urb->pipe_info) ? 1 : 0;
47066 +
47067 + qh->data_toggle = DWC_OTG_HC_PID_DATA0;
47068 + qh->maxp = dwc_otg_hcd_get_mps(&urb->pipe_info);
47069 + DWC_CIRCLEQ_INIT(&qh->qtd_list);
47070 + DWC_LIST_INIT(&qh->qh_list_entry);
47071 + qh->channel = NULL;
47072 +
47073 + /* FS/LS Enpoint on HS Hub
47074 + * NOT virtual root hub */
47075 + dev_speed = hcd->fops->speed(hcd, urb->priv);
47076 +
47077 + hcd->fops->hub_info(hcd, urb->priv, &hub_addr, &hub_port);
47078 + qh->do_split = 0;
47079 + if (microframe_schedule)
47080 + qh->speed = dev_speed;
47081 +
47082 + qh->nak_frame = 0xffff;
47083 +
47084 + if (((dev_speed == USB_SPEED_LOW) ||
47085 + (dev_speed == USB_SPEED_FULL)) &&
47086 + (hub_addr != 0 && hub_addr != 1)) {
47087 + DWC_DEBUGPL(DBG_HCD,
47088 + "QH init: EP %d: TT found at hub addr %d, for port %d\n",
47089 + dwc_otg_hcd_get_ep_num(&urb->pipe_info), hub_addr,
47090 + hub_port);
47091 + qh->do_split = 1;
47092 + qh->skip_count = 0;
47093 + }
47094 +
47095 + if (qh->ep_type == UE_INTERRUPT || qh->ep_type == UE_ISOCHRONOUS) {
47096 + /* Compute scheduling parameters once and save them. */
47097 + hprt0_data_t hprt;
47098 +
47099 + /** @todo Account for split transfers in the bus time. */
47100 + int bytecount =
47101 + dwc_hb_mult(qh->maxp) * dwc_max_packet(qh->maxp);
47102 +
47103 + qh->usecs =
47104 + calc_bus_time((qh->do_split ? USB_SPEED_HIGH : dev_speed),
47105 + qh->ep_is_in, (qh->ep_type == UE_ISOCHRONOUS),
47106 + bytecount);
47107 + /* Start in a slightly future (micro)frame. */
47108 + qh->sched_frame = dwc_frame_num_inc(hcd->frame_number,
47109 + SCHEDULE_SLOP);
47110 + qh->interval = urb->interval;
47111 +
47112 + hprt.d32 = DWC_READ_REG32(hcd->core_if->host_if->hprt0);
47113 + if (hprt.b.prtspd == DWC_HPRT0_PRTSPD_HIGH_SPEED) {
47114 + if (dev_speed == USB_SPEED_LOW ||
47115 + dev_speed == USB_SPEED_FULL) {
47116 + qh->interval *= 8;
47117 + qh->sched_frame |= 0x7;
47118 + qh->start_split_frame = qh->sched_frame;
47119 + } else if (int_ep_interval_min >= 2 &&
47120 + qh->interval < int_ep_interval_min &&
47121 + qh->ep_type == UE_INTERRUPT) {
47122 + qh->interval = int_ep_interval_min;
47123 + }
47124 + }
47125 + }
47126 +
47127 + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD QH Initialized\n");
47128 + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - qh = %p\n", qh);
47129 + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - Device Address = %d\n",
47130 + dwc_otg_hcd_get_dev_addr(&urb->pipe_info));
47131 + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - Endpoint %d, %s\n",
47132 + dwc_otg_hcd_get_ep_num(&urb->pipe_info),
47133 + dwc_otg_hcd_is_pipe_in(&urb->pipe_info) ? "IN" : "OUT");
47134 + switch (dev_speed) {
47135 + case USB_SPEED_LOW:
47136 + qh->dev_speed = DWC_OTG_EP_SPEED_LOW;
47137 + speed = "low";
47138 + break;
47139 + case USB_SPEED_FULL:
47140 + qh->dev_speed = DWC_OTG_EP_SPEED_FULL;
47141 + speed = "full";
47142 + break;
47143 + case USB_SPEED_HIGH:
47144 + qh->dev_speed = DWC_OTG_EP_SPEED_HIGH;
47145 + speed = "high";
47146 + break;
47147 + default:
47148 + speed = "?";
47149 + break;
47150 + }
47151 + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - Speed = %s\n", speed);
47152 +
47153 + switch (qh->ep_type) {
47154 + case UE_ISOCHRONOUS:
47155 + type = "isochronous";
47156 + break;
47157 + case UE_INTERRUPT:
47158 + type = "interrupt";
47159 + break;
47160 + case UE_CONTROL:
47161 + type = "control";
47162 + break;
47163 + case UE_BULK:
47164 + type = "bulk";
47165 + break;
47166 + default:
47167 + type = "?";
47168 + break;
47169 + }
47170 +
47171 + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - Type = %s\n", type);
47172 +
47173 +#ifdef DEBUG
47174 + if (qh->ep_type == UE_INTERRUPT) {
47175 + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - usecs = %d\n",
47176 + qh->usecs);
47177 + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - interval = %d\n",
47178 + qh->interval);
47179 + }
47180 +#endif
47181 +
47182 +}
47183 +
47184 +/**
47185 + * This function allocates and initializes a QH.
47186 + *
47187 + * @param hcd The HCD state structure for the DWC OTG controller.
47188 + * @param urb Holds the information about the device/endpoint that we need
47189 + * to initialize the QH.
47190 + * @param atomic_alloc Flag to do atomic allocation if needed
47191 + *
47192 + * @return Returns pointer to the newly allocated QH, or NULL on error. */
47193 +dwc_otg_qh_t *dwc_otg_hcd_qh_create(dwc_otg_hcd_t * hcd,
47194 + dwc_otg_hcd_urb_t * urb, int atomic_alloc)
47195 +{
47196 + dwc_otg_qh_t *qh;
47197 +
47198 + /* Allocate memory */
47199 + /** @todo add memflags argument */
47200 + qh = dwc_otg_hcd_qh_alloc(atomic_alloc);
47201 + if (qh == NULL) {
47202 + DWC_ERROR("qh allocation failed");
47203 + return NULL;
47204 + }
47205 +
47206 + qh_init(hcd, qh, urb);
47207 +
47208 + if (hcd->core_if->dma_desc_enable
47209 + && (dwc_otg_hcd_qh_init_ddma(hcd, qh) < 0)) {
47210 + dwc_otg_hcd_qh_free(hcd, qh);
47211 + return NULL;
47212 + }
47213 +
47214 + return qh;
47215 +}
47216 +
47217 +/* microframe_schedule=0 start */
47218 +
47219 +/**
47220 + * Checks that a channel is available for a periodic transfer.
47221 + *
47222 + * @return 0 if successful, negative error code otherise.
47223 + */
47224 +static int periodic_channel_available(dwc_otg_hcd_t * hcd)
47225 +{
47226 + /*
47227 + * Currently assuming that there is a dedicated host channnel for each
47228 + * periodic transaction plus at least one host channel for
47229 + * non-periodic transactions.
47230 + */
47231 + int status;
47232 + int num_channels;
47233 +
47234 + num_channels = hcd->core_if->core_params->host_channels;
47235 + if ((hcd->periodic_channels + hcd->non_periodic_channels < num_channels)
47236 + && (hcd->periodic_channels < num_channels - 1)) {
47237 + status = 0;
47238 + } else {
47239 + DWC_INFO("%s: Total channels: %d, Periodic: %d, Non-periodic: %d\n",
47240 + __func__, num_channels, hcd->periodic_channels, hcd->non_periodic_channels); //NOTICE
47241 + status = -DWC_E_NO_SPACE;
47242 + }
47243 +
47244 + return status;
47245 +}
47246 +
47247 +/**
47248 + * Checks that there is sufficient bandwidth for the specified QH in the
47249 + * periodic schedule. For simplicity, this calculation assumes that all the
47250 + * transfers in the periodic schedule may occur in the same (micro)frame.
47251 + *
47252 + * @param hcd The HCD state structure for the DWC OTG controller.
47253 + * @param qh QH containing periodic bandwidth required.
47254 + *
47255 + * @return 0 if successful, negative error code otherwise.
47256 + */
47257 +static int check_periodic_bandwidth(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
47258 +{
47259 + int status;
47260 + int16_t max_claimed_usecs;
47261 +
47262 + status = 0;
47263 +
47264 + if ((qh->dev_speed == DWC_OTG_EP_SPEED_HIGH) || qh->do_split) {
47265 + /*
47266 + * High speed mode.
47267 + * Max periodic usecs is 80% x 125 usec = 100 usec.
47268 + */
47269 +
47270 + max_claimed_usecs = 100 - qh->usecs;
47271 + } else {
47272 + /*
47273 + * Full speed mode.
47274 + * Max periodic usecs is 90% x 1000 usec = 900 usec.
47275 + */
47276 + max_claimed_usecs = 900 - qh->usecs;
47277 + }
47278 +
47279 + if (hcd->periodic_usecs > max_claimed_usecs) {
47280 + DWC_INFO("%s: already claimed usecs %d, required usecs %d\n", __func__, hcd->periodic_usecs, qh->usecs); //NOTICE
47281 + status = -DWC_E_NO_SPACE;
47282 + }
47283 +
47284 + return status;
47285 +}
47286 +
47287 +/* microframe_schedule=0 end */
47288 +
47289 +/**
47290 + * Microframe scheduler
47291 + * track the total use in hcd->frame_usecs
47292 + * keep each qh use in qh->frame_usecs
47293 + * when surrendering the qh then donate the time back
47294 + */
47295 +const unsigned short max_uframe_usecs[]={ 100, 100, 100, 100, 100, 100, 30, 0 };
47296 +
47297 +/*
47298 + * called from dwc_otg_hcd.c:dwc_otg_hcd_init
47299 + */
47300 +void init_hcd_usecs(dwc_otg_hcd_t *_hcd)
47301 +{
47302 + int i;
47303 + if (_hcd->flags.b.port_speed == DWC_HPRT0_PRTSPD_FULL_SPEED) {
47304 + _hcd->frame_usecs[0] = 900;
47305 + for (i = 1; i < 8; i++)
47306 + _hcd->frame_usecs[i] = 0;
47307 + } else {
47308 + for (i = 0; i < 8; i++)
47309 + _hcd->frame_usecs[i] = max_uframe_usecs[i];
47310 + }
47311 +}
47312 +
47313 +static int find_single_uframe(dwc_otg_hcd_t * _hcd, dwc_otg_qh_t * _qh)
47314 +{
47315 + int i;
47316 + unsigned short utime;
47317 + int t_left;
47318 + int ret;
47319 + int done;
47320 +
47321 + ret = -1;
47322 + utime = _qh->usecs;
47323 + t_left = utime;
47324 + i = 0;
47325 + done = 0;
47326 + while (done == 0) {
47327 + /* At the start _hcd->frame_usecs[i] = max_uframe_usecs[i]; */
47328 + if (utime <= _hcd->frame_usecs[i]) {
47329 + _hcd->frame_usecs[i] -= utime;
47330 + _qh->frame_usecs[i] += utime;
47331 + t_left -= utime;
47332 + ret = i;
47333 + done = 1;
47334 + return ret;
47335 + } else {
47336 + i++;
47337 + if (i == 8) {
47338 + done = 1;
47339 + ret = -1;
47340 + }
47341 + }
47342 + }
47343 + return ret;
47344 + }
47345 +
47346 +/*
47347 + * use this for FS apps that can span multiple uframes
47348 + */
47349 +static int find_multi_uframe(dwc_otg_hcd_t * _hcd, dwc_otg_qh_t * _qh)
47350 +{
47351 + int i;
47352 + int j;
47353 + unsigned short utime;
47354 + int t_left;
47355 + int ret;
47356 + int done;
47357 + unsigned short xtime;
47358 +
47359 + ret = -1;
47360 + utime = _qh->usecs;
47361 + t_left = utime;
47362 + i = 0;
47363 + done = 0;
47364 +loop:
47365 + while (done == 0) {
47366 + if(_hcd->frame_usecs[i] <= 0) {
47367 + i++;
47368 + if (i == 8) {
47369 + done = 1;
47370 + ret = -1;
47371 + }
47372 + goto loop;
47373 + }
47374 +
47375 + /*
47376 + * we need n consecutive slots
47377 + * so use j as a start slot j plus j+1 must be enough time (for now)
47378 + */
47379 + xtime= _hcd->frame_usecs[i];
47380 + for (j = i+1 ; j < 8 ; j++ ) {
47381 + /*
47382 + * if we add this frame remaining time to xtime we may
47383 + * be OK, if not we need to test j for a complete frame
47384 + */
47385 + if ((xtime+_hcd->frame_usecs[j]) < utime) {
47386 + if (_hcd->frame_usecs[j] < max_uframe_usecs[j]) {
47387 + j = 8;
47388 + ret = -1;
47389 + continue;
47390 + }
47391 + }
47392 + if (xtime >= utime) {
47393 + ret = i;
47394 + j = 8; /* stop loop with a good value ret */
47395 + continue;
47396 + }
47397 + /* add the frame time to x time */
47398 + xtime += _hcd->frame_usecs[j];
47399 + /* we must have a fully available next frame or break */
47400 + if ((xtime < utime)
47401 + && (_hcd->frame_usecs[j] == max_uframe_usecs[j])) {
47402 + ret = -1;
47403 + j = 8; /* stop loop with a bad value ret */
47404 + continue;
47405 + }
47406 + }
47407 + if (ret >= 0) {
47408 + t_left = utime;
47409 + for (j = i; (t_left>0) && (j < 8); j++ ) {
47410 + t_left -= _hcd->frame_usecs[j];
47411 + if ( t_left <= 0 ) {
47412 + _qh->frame_usecs[j] += _hcd->frame_usecs[j] + t_left;
47413 + _hcd->frame_usecs[j]= -t_left;
47414 + ret = i;
47415 + done = 1;
47416 + } else {
47417 + _qh->frame_usecs[j] += _hcd->frame_usecs[j];
47418 + _hcd->frame_usecs[j] = 0;
47419 + }
47420 + }
47421 + } else {
47422 + i++;
47423 + if (i == 8) {
47424 + done = 1;
47425 + ret = -1;
47426 + }
47427 + }
47428 + }
47429 + return ret;
47430 +}
47431 +
47432 +static int find_uframe(dwc_otg_hcd_t * _hcd, dwc_otg_qh_t * _qh)
47433 +{
47434 + int ret;
47435 + ret = -1;
47436 +
47437 + if (_qh->speed == USB_SPEED_HIGH ||
47438 + _hcd->flags.b.port_speed == DWC_HPRT0_PRTSPD_FULL_SPEED) {
47439 + /* if this is a hs transaction we need a full frame - or account for FS usecs */
47440 + ret = find_single_uframe(_hcd, _qh);
47441 + } else {
47442 + /* if this is a fs transaction we may need a sequence of frames */
47443 + ret = find_multi_uframe(_hcd, _qh);
47444 + }
47445 + return ret;
47446 +}
47447 +
47448 +/**
47449 + * Checks that the max transfer size allowed in a host channel is large enough
47450 + * to handle the maximum data transfer in a single (micro)frame for a periodic
47451 + * transfer.
47452 + *
47453 + * @param hcd The HCD state structure for the DWC OTG controller.
47454 + * @param qh QH for a periodic endpoint.
47455 + *
47456 + * @return 0 if successful, negative error code otherwise.
47457 + */
47458 +static int check_max_xfer_size(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
47459 +{
47460 + int status;
47461 + uint32_t max_xfer_size;
47462 + uint32_t max_channel_xfer_size;
47463 +
47464 + status = 0;
47465 +
47466 + max_xfer_size = dwc_max_packet(qh->maxp) * dwc_hb_mult(qh->maxp);
47467 + max_channel_xfer_size = hcd->core_if->core_params->max_transfer_size;
47468 +
47469 + if (max_xfer_size > max_channel_xfer_size) {
47470 + DWC_INFO("%s: Periodic xfer length %d > " "max xfer length for channel %d\n",
47471 + __func__, max_xfer_size, max_channel_xfer_size); //NOTICE
47472 + status = -DWC_E_NO_SPACE;
47473 + }
47474 +
47475 + return status;
47476 +}
47477 +
47478 +
47479 +
47480 +/**
47481 + * Schedules an interrupt or isochronous transfer in the periodic schedule.
47482 + *
47483 + * @param hcd The HCD state structure for the DWC OTG controller.
47484 + * @param qh QH for the periodic transfer. The QH should already contain the
47485 + * scheduling information.
47486 + *
47487 + * @return 0 if successful, negative error code otherwise.
47488 + */
47489 +static int schedule_periodic(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
47490 +{
47491 + int status = 0;
47492 +
47493 + if (microframe_schedule) {
47494 + int frame;
47495 + status = find_uframe(hcd, qh);
47496 + frame = -1;
47497 + if (status == 0) {
47498 + frame = 7;
47499 + } else {
47500 + if (status > 0 )
47501 + frame = status-1;
47502 + }
47503 +
47504 + /* Set the new frame up */
47505 + if (frame > -1) {
47506 + qh->sched_frame &= ~0x7;
47507 + qh->sched_frame |= (frame & 7);
47508 + }
47509 +
47510 + if (status != -1)
47511 + status = 0;
47512 + } else {
47513 + status = periodic_channel_available(hcd);
47514 + if (status) {
47515 + DWC_INFO("%s: No host channel available for periodic " "transfer.\n", __func__); //NOTICE
47516 + return status;
47517 + }
47518 +
47519 + status = check_periodic_bandwidth(hcd, qh);
47520 + }
47521 + if (status) {
47522 + DWC_INFO("%s: Insufficient periodic bandwidth for "
47523 + "periodic transfer.\n", __func__);
47524 + return -DWC_E_NO_SPACE;
47525 + }
47526 + status = check_max_xfer_size(hcd, qh);
47527 + if (status) {
47528 + DWC_INFO("%s: Channel max transfer size too small "
47529 + "for periodic transfer.\n", __func__);
47530 + return status;
47531 + }
47532 +
47533 + if (hcd->core_if->dma_desc_enable) {
47534 + /* Don't rely on SOF and start in ready schedule */
47535 + DWC_LIST_INSERT_TAIL(&hcd->periodic_sched_ready, &qh->qh_list_entry);
47536 + }
47537 + else {
47538 + if(fiq_enable && (DWC_LIST_EMPTY(&hcd->periodic_sched_inactive) || dwc_frame_num_le(qh->sched_frame, hcd->fiq_state->next_sched_frame)))
47539 + {
47540 + hcd->fiq_state->next_sched_frame = qh->sched_frame;
47541 +
47542 + }
47543 + /* Always start in the inactive schedule. */
47544 + DWC_LIST_INSERT_TAIL(&hcd->periodic_sched_inactive, &qh->qh_list_entry);
47545 + }
47546 +
47547 + if (!microframe_schedule) {
47548 + /* Reserve the periodic channel. */
47549 + hcd->periodic_channels++;
47550 + }
47551 +
47552 + /* Update claimed usecs per (micro)frame. */
47553 + hcd->periodic_usecs += qh->usecs;
47554 +
47555 + return status;
47556 +}
47557 +
47558 +
47559 +/**
47560 + * This function adds a QH to either the non periodic or periodic schedule if
47561 + * it is not already in the schedule. If the QH is already in the schedule, no
47562 + * action is taken.
47563 + *
47564 + * @return 0 if successful, negative error code otherwise.
47565 + */
47566 +int dwc_otg_hcd_qh_add(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
47567 +{
47568 + int status = 0;
47569 + gintmsk_data_t intr_mask = {.d32 = 0 };
47570 +
47571 + if (!DWC_LIST_EMPTY(&qh->qh_list_entry)) {
47572 + /* QH already in a schedule. */
47573 + return status;
47574 + }
47575 +
47576 + /* Add the new QH to the appropriate schedule */
47577 + if (dwc_qh_is_non_per(qh)) {
47578 + /* Always start in the inactive schedule. */
47579 + DWC_LIST_INSERT_TAIL(&hcd->non_periodic_sched_inactive,
47580 + &qh->qh_list_entry);
47581 + //hcd->fiq_state->kick_np_queues = 1;
47582 + } else {
47583 + status = schedule_periodic(hcd, qh);
47584 + if ( !hcd->periodic_qh_count ) {
47585 + intr_mask.b.sofintr = 1;
47586 + if (fiq_enable) {
47587 + local_fiq_disable();
47588 + fiq_fsm_spin_lock(&hcd->fiq_state->lock);
47589 + DWC_MODIFY_REG32(&hcd->core_if->core_global_regs->gintmsk, intr_mask.d32, intr_mask.d32);
47590 + fiq_fsm_spin_unlock(&hcd->fiq_state->lock);
47591 + local_fiq_enable();
47592 + } else {
47593 + DWC_MODIFY_REG32(&hcd->core_if->core_global_regs->gintmsk, intr_mask.d32, intr_mask.d32);
47594 + }
47595 + }
47596 + hcd->periodic_qh_count++;
47597 + }
47598 +
47599 + return status;
47600 +}
47601 +
47602 +/**
47603 + * Removes an interrupt or isochronous transfer from the periodic schedule.
47604 + *
47605 + * @param hcd The HCD state structure for the DWC OTG controller.
47606 + * @param qh QH for the periodic transfer.
47607 + */
47608 +static void deschedule_periodic(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
47609 +{
47610 + int i;
47611 + DWC_LIST_REMOVE_INIT(&qh->qh_list_entry);
47612 +
47613 + /* Update claimed usecs per (micro)frame. */
47614 + hcd->periodic_usecs -= qh->usecs;
47615 +
47616 + if (!microframe_schedule) {
47617 + /* Release the periodic channel reservation. */
47618 + hcd->periodic_channels--;
47619 + } else {
47620 + for (i = 0; i < 8; i++) {
47621 + hcd->frame_usecs[i] += qh->frame_usecs[i];
47622 + qh->frame_usecs[i] = 0;
47623 + }
47624 + }
47625 +}
47626 +
47627 +/**
47628 + * Removes a QH from either the non-periodic or periodic schedule. Memory is
47629 + * not freed.
47630 + *
47631 + * @param hcd The HCD state structure.
47632 + * @param qh QH to remove from schedule. */
47633 +void dwc_otg_hcd_qh_remove(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
47634 +{
47635 + gintmsk_data_t intr_mask = {.d32 = 0 };
47636 +
47637 + if (DWC_LIST_EMPTY(&qh->qh_list_entry)) {
47638 + /* QH is not in a schedule. */
47639 + return;
47640 + }
47641 +
47642 + if (dwc_qh_is_non_per(qh)) {
47643 + if (hcd->non_periodic_qh_ptr == &qh->qh_list_entry) {
47644 + hcd->non_periodic_qh_ptr =
47645 + hcd->non_periodic_qh_ptr->next;
47646 + }
47647 + DWC_LIST_REMOVE_INIT(&qh->qh_list_entry);
47648 + //if (!DWC_LIST_EMPTY(&hcd->non_periodic_sched_inactive))
47649 + // hcd->fiq_state->kick_np_queues = 1;
47650 + } else {
47651 + deschedule_periodic(hcd, qh);
47652 + hcd->periodic_qh_count--;
47653 + if( !hcd->periodic_qh_count && !fiq_fsm_enable ) {
47654 + intr_mask.b.sofintr = 1;
47655 + if (fiq_enable) {
47656 + local_fiq_disable();
47657 + fiq_fsm_spin_lock(&hcd->fiq_state->lock);
47658 + DWC_MODIFY_REG32(&hcd->core_if->core_global_regs->gintmsk, intr_mask.d32, 0);
47659 + fiq_fsm_spin_unlock(&hcd->fiq_state->lock);
47660 + local_fiq_enable();
47661 + } else {
47662 + DWC_MODIFY_REG32(&hcd->core_if->core_global_regs->gintmsk, intr_mask.d32, 0);
47663 + }
47664 + }
47665 + }
47666 +}
47667 +
47668 +/**
47669 + * Deactivates a QH. For non-periodic QHs, removes the QH from the active
47670 + * non-periodic schedule. The QH is added to the inactive non-periodic
47671 + * schedule if any QTDs are still attached to the QH.
47672 + *
47673 + * For periodic QHs, the QH is removed from the periodic queued schedule. If
47674 + * there are any QTDs still attached to the QH, the QH is added to either the
47675 + * periodic inactive schedule or the periodic ready schedule and its next
47676 + * scheduled frame is calculated. The QH is placed in the ready schedule if
47677 + * the scheduled frame has been reached already. Otherwise it's placed in the
47678 + * inactive schedule. If there are no QTDs attached to the QH, the QH is
47679 + * completely removed from the periodic schedule.
47680 + */
47681 +void dwc_otg_hcd_qh_deactivate(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh,
47682 + int sched_next_periodic_split)
47683 +{
47684 + if (dwc_qh_is_non_per(qh)) {
47685 + dwc_otg_hcd_qh_remove(hcd, qh);
47686 + if (!DWC_CIRCLEQ_EMPTY(&qh->qtd_list)) {
47687 + /* Add back to inactive non-periodic schedule. */
47688 + dwc_otg_hcd_qh_add(hcd, qh);
47689 + //hcd->fiq_state->kick_np_queues = 1;
47690 + } else {
47691 + if(nak_holdoff && qh->do_split) {
47692 + qh->nak_frame = 0xFFFF;
47693 + }
47694 + }
47695 + } else {
47696 + uint16_t frame_number = dwc_otg_hcd_get_frame_number(hcd);
47697 +
47698 + if (qh->do_split) {
47699 + /* Schedule the next continuing periodic split transfer */
47700 + if (sched_next_periodic_split) {
47701 +
47702 + qh->sched_frame = frame_number;
47703 +
47704 + if (dwc_frame_num_le(frame_number,
47705 + dwc_frame_num_inc
47706 + (qh->start_split_frame,
47707 + 1))) {
47708 + /*
47709 + * Allow one frame to elapse after start
47710 + * split microframe before scheduling
47711 + * complete split, but DONT if we are
47712 + * doing the next start split in the
47713 + * same frame for an ISOC out.
47714 + */
47715 + if ((qh->ep_type != UE_ISOCHRONOUS) ||
47716 + (qh->ep_is_in != 0)) {
47717 + qh->sched_frame =
47718 + dwc_frame_num_inc(qh->sched_frame, 1);
47719 + }
47720 + }
47721 + } else {
47722 + qh->sched_frame =
47723 + dwc_frame_num_inc(qh->start_split_frame,
47724 + qh->interval);
47725 + if (dwc_frame_num_le
47726 + (qh->sched_frame, frame_number)) {
47727 + qh->sched_frame = frame_number;
47728 + }
47729 + qh->sched_frame |= 0x7;
47730 + qh->start_split_frame = qh->sched_frame;
47731 + }
47732 + } else {
47733 + qh->sched_frame =
47734 + dwc_frame_num_inc(qh->sched_frame, qh->interval);
47735 + if (dwc_frame_num_le(qh->sched_frame, frame_number)) {
47736 + qh->sched_frame = frame_number;
47737 + }
47738 + }
47739 +
47740 + if (DWC_CIRCLEQ_EMPTY(&qh->qtd_list)) {
47741 + dwc_otg_hcd_qh_remove(hcd, qh);
47742 + } else {
47743 + /*
47744 + * Remove from periodic_sched_queued and move to
47745 + * appropriate queue.
47746 + */
47747 + if ((microframe_schedule && dwc_frame_num_le(qh->sched_frame, frame_number)) ||
47748 + (!microframe_schedule && qh->sched_frame == frame_number)) {
47749 + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_ready,
47750 + &qh->qh_list_entry);
47751 + } else {
47752 + if(fiq_enable && !dwc_frame_num_le(hcd->fiq_state->next_sched_frame, qh->sched_frame))
47753 + {
47754 + hcd->fiq_state->next_sched_frame = qh->sched_frame;
47755 + }
47756 +
47757 + DWC_LIST_MOVE_HEAD
47758 + (&hcd->periodic_sched_inactive,
47759 + &qh->qh_list_entry);
47760 + }
47761 + }
47762 + }
47763 +}
47764 +
47765 +/**
47766 + * This function allocates and initializes a QTD.
47767 + *
47768 + * @param urb The URB to create a QTD from. Each URB-QTD pair will end up
47769 + * pointing to each other so each pair should have a unique correlation.
47770 + * @param atomic_alloc Flag to do atomic alloc if needed
47771 + *
47772 + * @return Returns pointer to the newly allocated QTD, or NULL on error. */
47773 +dwc_otg_qtd_t *dwc_otg_hcd_qtd_create(dwc_otg_hcd_urb_t * urb, int atomic_alloc)
47774 +{
47775 + dwc_otg_qtd_t *qtd;
47776 +
47777 + qtd = dwc_otg_hcd_qtd_alloc(atomic_alloc);
47778 + if (qtd == NULL) {
47779 + return NULL;
47780 + }
47781 +
47782 + dwc_otg_hcd_qtd_init(qtd, urb);
47783 + return qtd;
47784 +}
47785 +
47786 +/**
47787 + * Initializes a QTD structure.
47788 + *
47789 + * @param qtd The QTD to initialize.
47790 + * @param urb The URB to use for initialization. */
47791 +void dwc_otg_hcd_qtd_init(dwc_otg_qtd_t * qtd, dwc_otg_hcd_urb_t * urb)
47792 +{
47793 + dwc_memset(qtd, 0, sizeof(dwc_otg_qtd_t));
47794 + qtd->urb = urb;
47795 + if (dwc_otg_hcd_get_pipe_type(&urb->pipe_info) == UE_CONTROL) {
47796 + /*
47797 + * The only time the QTD data toggle is used is on the data
47798 + * phase of control transfers. This phase always starts with
47799 + * DATA1.
47800 + */
47801 + qtd->data_toggle = DWC_OTG_HC_PID_DATA1;
47802 + qtd->control_phase = DWC_OTG_CONTROL_SETUP;
47803 + }
47804 +
47805 + /* start split */
47806 + qtd->complete_split = 0;
47807 + qtd->isoc_split_pos = DWC_HCSPLIT_XACTPOS_ALL;
47808 + qtd->isoc_split_offset = 0;
47809 + qtd->in_process = 0;
47810 +
47811 + /* Store the qtd ptr in the urb to reference what QTD. */
47812 + urb->qtd = qtd;
47813 + return;
47814 +}
47815 +
47816 +/**
47817 + * This function adds a QTD to the QTD-list of a QH. It will find the correct
47818 + * QH to place the QTD into. If it does not find a QH, then it will create a
47819 + * new QH. If the QH to which the QTD is added is not currently scheduled, it
47820 + * is placed into the proper schedule based on its EP type.
47821 + * HCD lock must be held and interrupts must be disabled on entry
47822 + *
47823 + * @param[in] qtd The QTD to add
47824 + * @param[in] hcd The DWC HCD structure
47825 + * @param[out] qh out parameter to return queue head
47826 + * @param atomic_alloc Flag to do atomic alloc if needed
47827 + *
47828 + * @return 0 if successful, negative error code otherwise.
47829 + */
47830 +int dwc_otg_hcd_qtd_add(dwc_otg_qtd_t * qtd,
47831 + dwc_otg_hcd_t * hcd, dwc_otg_qh_t ** qh, int atomic_alloc)
47832 +{
47833 + int retval = 0;
47834 + dwc_otg_hcd_urb_t *urb = qtd->urb;
47835 +
47836 + /*
47837 + * Get the QH which holds the QTD-list to insert to. Create QH if it
47838 + * doesn't exist.
47839 + */
47840 + if (*qh == NULL) {
47841 + *qh = dwc_otg_hcd_qh_create(hcd, urb, atomic_alloc);
47842 + if (*qh == NULL) {
47843 + retval = -DWC_E_NO_MEMORY;
47844 + goto done;
47845 + } else {
47846 + if (fiq_enable)
47847 + hcd->fiq_state->kick_np_queues = 1;
47848 + }
47849 + }
47850 + retval = dwc_otg_hcd_qh_add(hcd, *qh);
47851 + if (retval == 0) {
47852 + DWC_CIRCLEQ_INSERT_TAIL(&((*qh)->qtd_list), qtd,
47853 + qtd_list_entry);
47854 + qtd->qh = *qh;
47855 + }
47856 +done:
47857 +
47858 + return retval;
47859 +}
47860 +
47861 +#endif /* DWC_DEVICE_ONLY */
47862 --- /dev/null
47863 +++ b/drivers/usb/host/dwc_otg/dwc_otg_os_dep.h
47864 @@ -0,0 +1,188 @@
47865 +#ifndef _DWC_OS_DEP_H_
47866 +#define _DWC_OS_DEP_H_
47867 +
47868 +/**
47869 + * @file
47870 + *
47871 + * This file contains OS dependent structures.
47872 + *
47873 + */
47874 +
47875 +#include <linux/kernel.h>
47876 +#include <linux/module.h>
47877 +#include <linux/moduleparam.h>
47878 +#include <linux/init.h>
47879 +#include <linux/device.h>
47880 +#include <linux/errno.h>
47881 +#include <linux/types.h>
47882 +#include <linux/slab.h>
47883 +#include <linux/list.h>
47884 +#include <linux/interrupt.h>
47885 +#include <linux/ctype.h>
47886 +#include <linux/string.h>
47887 +#include <linux/dma-mapping.h>
47888 +#include <linux/jiffies.h>
47889 +#include <linux/delay.h>
47890 +#include <linux/timer.h>
47891 +#include <linux/workqueue.h>
47892 +#include <linux/stat.h>
47893 +#include <linux/pci.h>
47894 +
47895 +#include <linux/version.h>
47896 +
47897 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
47898 +# include <linux/irq.h>
47899 +#endif
47900 +
47901 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
47902 +# include <linux/usb/ch9.h>
47903 +#else
47904 +# include <linux/usb_ch9.h>
47905 +#endif
47906 +
47907 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
47908 +# include <linux/usb/gadget.h>
47909 +#else
47910 +# include <linux/usb_gadget.h>
47911 +#endif
47912 +
47913 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
47914 +# include <asm/irq.h>
47915 +#endif
47916 +
47917 +#ifdef PCI_INTERFACE
47918 +# include <asm/io.h>
47919 +#endif
47920 +
47921 +#ifdef LM_INTERFACE
47922 +# include <asm/unaligned.h>
47923 +# include <asm/sizes.h>
47924 +# include <asm/param.h>
47925 +# include <asm/io.h>
47926 +# if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30))
47927 +# include <asm/arch/hardware.h>
47928 +# include <asm/arch/lm.h>
47929 +# include <asm/arch/irqs.h>
47930 +# include <asm/arch/regs-irq.h>
47931 +# else
47932 +/* in 2.6.31, at least, we seem to have lost the generic LM infrastructure -
47933 + here we assume that the machine architecture provides definitions
47934 + in its own header
47935 +*/
47936 +# include <mach/lm.h>
47937 +# include <mach/hardware.h>
47938 +# endif
47939 +#endif
47940 +
47941 +#ifdef PLATFORM_INTERFACE
47942 +#include <linux/platform_device.h>
47943 +#include <asm/mach/map.h>
47944 +#endif
47945 +
47946 +/** The OS page size */
47947 +#define DWC_OS_PAGE_SIZE PAGE_SIZE
47948 +
47949 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,14)
47950 +typedef int gfp_t;
47951 +#endif
47952 +
47953 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18)
47954 +# define IRQF_SHARED SA_SHIRQ
47955 +#endif
47956 +
47957 +typedef struct os_dependent {
47958 + /** Base address returned from ioremap() */
47959 + void *base;
47960 +
47961 + /** Register offset for Diagnostic API */
47962 + uint32_t reg_offset;
47963 +
47964 + /** Base address for MPHI peripheral */
47965 + void *mphi_base;
47966 +
47967 +#ifdef LM_INTERFACE
47968 + struct lm_device *lmdev;
47969 +#elif defined(PCI_INTERFACE)
47970 + struct pci_dev *pcidev;
47971 +
47972 + /** Start address of a PCI region */
47973 + resource_size_t rsrc_start;
47974 +
47975 + /** Length address of a PCI region */
47976 + resource_size_t rsrc_len;
47977 +#elif defined(PLATFORM_INTERFACE)
47978 + struct platform_device *platformdev;
47979 +#endif
47980 +
47981 +} os_dependent_t;
47982 +
47983 +#ifdef __cplusplus
47984 +}
47985 +#endif
47986 +
47987 +
47988 +
47989 +/* Type for the our device on the chosen bus */
47990 +#if defined(LM_INTERFACE)
47991 +typedef struct lm_device dwc_bus_dev_t;
47992 +#elif defined(PCI_INTERFACE)
47993 +typedef struct pci_dev dwc_bus_dev_t;
47994 +#elif defined(PLATFORM_INTERFACE)
47995 +typedef struct platform_device dwc_bus_dev_t;
47996 +#endif
47997 +
47998 +/* Helper macro to retrieve drvdata from the device on the chosen bus */
47999 +#if defined(LM_INTERFACE)
48000 +#define DWC_OTG_BUSDRVDATA(_dev) lm_get_drvdata(_dev)
48001 +#elif defined(PCI_INTERFACE)
48002 +#define DWC_OTG_BUSDRVDATA(_dev) pci_get_drvdata(_dev)
48003 +#elif defined(PLATFORM_INTERFACE)
48004 +#define DWC_OTG_BUSDRVDATA(_dev) platform_get_drvdata(_dev)
48005 +#endif
48006 +
48007 +/**
48008 + * Helper macro returning the otg_device structure of a given struct device
48009 + *
48010 + * c.f. static dwc_otg_device_t *dwc_otg_drvdev(struct device *_dev)
48011 + */
48012 +#ifdef LM_INTERFACE
48013 +#define DWC_OTG_GETDRVDEV(_var, _dev) do { \
48014 + struct lm_device *lm_dev = \
48015 + container_of(_dev, struct lm_device, dev); \
48016 + _var = lm_get_drvdata(lm_dev); \
48017 + } while (0)
48018 +
48019 +#elif defined(PCI_INTERFACE)
48020 +#define DWC_OTG_GETDRVDEV(_var, _dev) do { \
48021 + _var = dev_get_drvdata(_dev); \
48022 + } while (0)
48023 +
48024 +#elif defined(PLATFORM_INTERFACE)
48025 +#define DWC_OTG_GETDRVDEV(_var, _dev) do { \
48026 + struct platform_device *platform_dev = \
48027 + container_of(_dev, struct platform_device, dev); \
48028 + _var = platform_get_drvdata(platform_dev); \
48029 + } while (0)
48030 +#endif
48031 +
48032 +
48033 +/**
48034 + * Helper macro returning the struct dev of the given struct os_dependent
48035 + *
48036 + * c.f. static struct device *dwc_otg_getdev(struct os_dependent *osdep)
48037 + */
48038 +#ifdef LM_INTERFACE
48039 +#define DWC_OTG_OS_GETDEV(_osdep) \
48040 + ((_osdep).lmdev == NULL? NULL: &(_osdep).lmdev->dev)
48041 +#elif defined(PCI_INTERFACE)
48042 +#define DWC_OTG_OS_GETDEV(_osdep) \
48043 + ((_osdep).pci_dev == NULL? NULL: &(_osdep).pci_dev->dev)
48044 +#elif defined(PLATFORM_INTERFACE)
48045 +#define DWC_OTG_OS_GETDEV(_osdep) \
48046 + ((_osdep).platformdev == NULL? NULL: &(_osdep).platformdev->dev)
48047 +#endif
48048 +
48049 +
48050 +
48051 +
48052 +#endif /* _DWC_OS_DEP_H_ */
48053 --- /dev/null
48054 +++ b/drivers/usb/host/dwc_otg/dwc_otg_pcd.c
48055 @@ -0,0 +1,2725 @@
48056 +/* ==========================================================================
48057 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_pcd.c $
48058 + * $Revision: #101 $
48059 + * $Date: 2012/08/10 $
48060 + * $Change: 2047372 $
48061 + *
48062 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
48063 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
48064 + * otherwise expressly agreed to in writing between Synopsys and you.
48065 + *
48066 + * The Software IS NOT an item of Licensed Software or Licensed Product under
48067 + * any End User Software License Agreement or Agreement for Licensed Product
48068 + * with Synopsys or any supplement thereto. You are permitted to use and
48069 + * redistribute this Software in source and binary forms, with or without
48070 + * modification, provided that redistributions of source code must retain this
48071 + * notice. You may not view, use, disclose, copy or distribute this file or
48072 + * any information contained herein except pursuant to this license grant from
48073 + * Synopsys. If you do not agree with this notice, including the disclaimer
48074 + * below, then you are not authorized to use the Software.
48075 + *
48076 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
48077 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48078 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48079 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
48080 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
48081 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
48082 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
48083 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48084 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
48085 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
48086 + * DAMAGE.
48087 + * ========================================================================== */
48088 +#ifndef DWC_HOST_ONLY
48089 +
48090 +/** @file
48091 + * This file implements PCD Core. All code in this file is portable and doesn't
48092 + * use any OS specific functions.
48093 + * PCD Core provides Interface, defined in <code><dwc_otg_pcd_if.h></code>
48094 + * header file, which can be used to implement OS specific PCD interface.
48095 + *
48096 + * An important function of the PCD is managing interrupts generated
48097 + * by the DWC_otg controller. The implementation of the DWC_otg device
48098 + * mode interrupt service routines is in dwc_otg_pcd_intr.c.
48099 + *
48100 + * @todo Add Device Mode test modes (Test J mode, Test K mode, etc).
48101 + * @todo Does it work when the request size is greater than DEPTSIZ
48102 + * transfer size
48103 + *
48104 + */
48105 +
48106 +#include "dwc_otg_pcd.h"
48107 +
48108 +#ifdef DWC_UTE_CFI
48109 +#include "dwc_otg_cfi.h"
48110 +
48111 +extern int init_cfi(cfiobject_t * cfiobj);
48112 +#endif
48113 +
48114 +/**
48115 + * Choose endpoint from ep arrays using usb_ep structure.
48116 + */
48117 +static dwc_otg_pcd_ep_t *get_ep_from_handle(dwc_otg_pcd_t * pcd, void *handle)
48118 +{
48119 + int i;
48120 + if (pcd->ep0.priv == handle) {
48121 + return &pcd->ep0;
48122 + }
48123 + for (i = 0; i < MAX_EPS_CHANNELS - 1; i++) {
48124 + if (pcd->in_ep[i].priv == handle)
48125 + return &pcd->in_ep[i];
48126 + if (pcd->out_ep[i].priv == handle)
48127 + return &pcd->out_ep[i];
48128 + }
48129 +
48130 + return NULL;
48131 +}
48132 +
48133 +/**
48134 + * This function completes a request. It call's the request call back.
48135 + */
48136 +void dwc_otg_request_done(dwc_otg_pcd_ep_t * ep, dwc_otg_pcd_request_t * req,
48137 + int32_t status)
48138 +{
48139 + unsigned stopped = ep->stopped;
48140 +
48141 + DWC_DEBUGPL(DBG_PCDV, "%s(ep %p req %p)\n", __func__, ep, req);
48142 + DWC_CIRCLEQ_REMOVE_INIT(&ep->queue, req, queue_entry);
48143 +
48144 + /* don't modify queue heads during completion callback */
48145 + ep->stopped = 1;
48146 + /* spin_unlock/spin_lock now done in fops->complete() */
48147 + ep->pcd->fops->complete(ep->pcd, ep->priv, req->priv, status,
48148 + req->actual);
48149 +
48150 + if (ep->pcd->request_pending > 0) {
48151 + --ep->pcd->request_pending;
48152 + }
48153 +
48154 + ep->stopped = stopped;
48155 + DWC_FREE(req);
48156 +}
48157 +
48158 +/**
48159 + * This function terminates all the requsts in the EP request queue.
48160 + */
48161 +void dwc_otg_request_nuke(dwc_otg_pcd_ep_t * ep)
48162 +{
48163 + dwc_otg_pcd_request_t *req;
48164 +
48165 + ep->stopped = 1;
48166 +
48167 + /* called with irqs blocked?? */
48168 + while (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
48169 + req = DWC_CIRCLEQ_FIRST(&ep->queue);
48170 + dwc_otg_request_done(ep, req, -DWC_E_SHUTDOWN);
48171 + }
48172 +}
48173 +
48174 +void dwc_otg_pcd_start(dwc_otg_pcd_t * pcd,
48175 + const struct dwc_otg_pcd_function_ops *fops)
48176 +{
48177 + pcd->fops = fops;
48178 +}
48179 +
48180 +/**
48181 + * PCD Callback function for initializing the PCD when switching to
48182 + * device mode.
48183 + *
48184 + * @param p void pointer to the <code>dwc_otg_pcd_t</code>
48185 + */
48186 +static int32_t dwc_otg_pcd_start_cb(void *p)
48187 +{
48188 + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) p;
48189 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
48190 +
48191 + /*
48192 + * Initialized the Core for Device mode.
48193 + */
48194 + if (dwc_otg_is_device_mode(core_if)) {
48195 + dwc_otg_core_dev_init(core_if);
48196 + /* Set core_if's lock pointer to the pcd->lock */
48197 + core_if->lock = pcd->lock;
48198 + }
48199 + return 1;
48200 +}
48201 +
48202 +/** CFI-specific buffer allocation function for EP */
48203 +#ifdef DWC_UTE_CFI
48204 +uint8_t *cfiw_ep_alloc_buffer(dwc_otg_pcd_t * pcd, void *pep, dwc_dma_t * addr,
48205 + size_t buflen, int flags)
48206 +{
48207 + dwc_otg_pcd_ep_t *ep;
48208 + ep = get_ep_from_handle(pcd, pep);
48209 + if (!ep) {
48210 + DWC_WARN("bad ep\n");
48211 + return -DWC_E_INVALID;
48212 + }
48213 +
48214 + return pcd->cfi->ops.ep_alloc_buf(pcd->cfi, pcd, ep, addr, buflen,
48215 + flags);
48216 +}
48217 +#else
48218 +uint8_t *cfiw_ep_alloc_buffer(dwc_otg_pcd_t * pcd, void *pep, dwc_dma_t * addr,
48219 + size_t buflen, int flags);
48220 +#endif
48221 +
48222 +/**
48223 + * PCD Callback function for notifying the PCD when resuming from
48224 + * suspend.
48225 + *
48226 + * @param p void pointer to the <code>dwc_otg_pcd_t</code>
48227 + */
48228 +static int32_t dwc_otg_pcd_resume_cb(void *p)
48229 +{
48230 + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) p;
48231 +
48232 + if (pcd->fops->resume) {
48233 + pcd->fops->resume(pcd);
48234 + }
48235 +
48236 + /* Stop the SRP timeout timer. */
48237 + if ((GET_CORE_IF(pcd)->core_params->phy_type != DWC_PHY_TYPE_PARAM_FS)
48238 + || (!GET_CORE_IF(pcd)->core_params->i2c_enable)) {
48239 + if (GET_CORE_IF(pcd)->srp_timer_started) {
48240 + GET_CORE_IF(pcd)->srp_timer_started = 0;
48241 + DWC_TIMER_CANCEL(GET_CORE_IF(pcd)->srp_timer);
48242 + }
48243 + }
48244 + return 1;
48245 +}
48246 +
48247 +/**
48248 + * PCD Callback function for notifying the PCD device is suspended.
48249 + *
48250 + * @param p void pointer to the <code>dwc_otg_pcd_t</code>
48251 + */
48252 +static int32_t dwc_otg_pcd_suspend_cb(void *p)
48253 +{
48254 + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) p;
48255 +
48256 + if (pcd->fops->suspend) {
48257 + DWC_SPINUNLOCK(pcd->lock);
48258 + pcd->fops->suspend(pcd);
48259 + DWC_SPINLOCK(pcd->lock);
48260 + }
48261 +
48262 + return 1;
48263 +}
48264 +
48265 +/**
48266 + * PCD Callback function for stopping the PCD when switching to Host
48267 + * mode.
48268 + *
48269 + * @param p void pointer to the <code>dwc_otg_pcd_t</code>
48270 + */
48271 +static int32_t dwc_otg_pcd_stop_cb(void *p)
48272 +{
48273 + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) p;
48274 + extern void dwc_otg_pcd_stop(dwc_otg_pcd_t * _pcd);
48275 +
48276 + dwc_otg_pcd_stop(pcd);
48277 + return 1;
48278 +}
48279 +
48280 +/**
48281 + * PCD Callback structure for handling mode switching.
48282 + */
48283 +static dwc_otg_cil_callbacks_t pcd_callbacks = {
48284 + .start = dwc_otg_pcd_start_cb,
48285 + .stop = dwc_otg_pcd_stop_cb,
48286 + .suspend = dwc_otg_pcd_suspend_cb,
48287 + .resume_wakeup = dwc_otg_pcd_resume_cb,
48288 + .p = 0, /* Set at registration */
48289 +};
48290 +
48291 +/**
48292 + * This function allocates a DMA Descriptor chain for the Endpoint
48293 + * buffer to be used for a transfer to/from the specified endpoint.
48294 + */
48295 +dwc_otg_dev_dma_desc_t *dwc_otg_ep_alloc_desc_chain(struct device *dev,
48296 + dwc_dma_t * dma_desc_addr,
48297 + uint32_t count)
48298 +{
48299 + return DWC_DMA_ALLOC_ATOMIC(dev, count * sizeof(dwc_otg_dev_dma_desc_t),
48300 + dma_desc_addr);
48301 +}
48302 +
48303 +/**
48304 + * This function frees a DMA Descriptor chain that was allocated by ep_alloc_desc.
48305 + */
48306 +void dwc_otg_ep_free_desc_chain(struct device *dev,
48307 + dwc_otg_dev_dma_desc_t * desc_addr,
48308 + uint32_t dma_desc_addr, uint32_t count)
48309 +{
48310 + DWC_DMA_FREE(dev, count * sizeof(dwc_otg_dev_dma_desc_t), desc_addr,
48311 + dma_desc_addr);
48312 +}
48313 +
48314 +#ifdef DWC_EN_ISOC
48315 +
48316 +/**
48317 + * This function initializes a descriptor chain for Isochronous transfer
48318 + *
48319 + * @param core_if Programming view of DWC_otg controller.
48320 + * @param dwc_ep The EP to start the transfer on.
48321 + *
48322 + */
48323 +void dwc_otg_iso_ep_start_ddma_transfer(dwc_otg_core_if_t * core_if,
48324 + dwc_ep_t * dwc_ep)
48325 +{
48326 +
48327 + dsts_data_t dsts = {.d32 = 0 };
48328 + depctl_data_t depctl = {.d32 = 0 };
48329 + volatile uint32_t *addr;
48330 + int i, j;
48331 + uint32_t len;
48332 +
48333 + if (dwc_ep->is_in)
48334 + dwc_ep->desc_cnt = dwc_ep->buf_proc_intrvl / dwc_ep->bInterval;
48335 + else
48336 + dwc_ep->desc_cnt =
48337 + dwc_ep->buf_proc_intrvl * dwc_ep->pkt_per_frm /
48338 + dwc_ep->bInterval;
48339 +
48340 + /** Allocate descriptors for double buffering */
48341 + dwc_ep->iso_desc_addr =
48342 + dwc_otg_ep_alloc_desc_chain(&dwc_ep->iso_dma_desc_addr,
48343 + dwc_ep->desc_cnt * 2);
48344 + if (dwc_ep->desc_addr) {
48345 + DWC_WARN("%s, can't allocate DMA descriptor chain\n", __func__);
48346 + return;
48347 + }
48348 +
48349 + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
48350 +
48351 + /** ISO OUT EP */
48352 + if (dwc_ep->is_in == 0) {
48353 + dev_dma_desc_sts_t sts = {.d32 = 0 };
48354 + dwc_otg_dev_dma_desc_t *dma_desc = dwc_ep->iso_desc_addr;
48355 + dma_addr_t dma_ad;
48356 + uint32_t data_per_desc;
48357 + dwc_otg_dev_out_ep_regs_t *out_regs =
48358 + core_if->dev_if->out_ep_regs[dwc_ep->num];
48359 + int offset;
48360 +
48361 + addr = &core_if->dev_if->out_ep_regs[dwc_ep->num]->doepctl;
48362 + dma_ad = (dma_addr_t) DWC_READ_REG32(&(out_regs->doepdma));
48363 +
48364 + /** Buffer 0 descriptors setup */
48365 + dma_ad = dwc_ep->dma_addr0;
48366 +
48367 + sts.b_iso_out.bs = BS_HOST_READY;
48368 + sts.b_iso_out.rxsts = 0;
48369 + sts.b_iso_out.l = 0;
48370 + sts.b_iso_out.sp = 0;
48371 + sts.b_iso_out.ioc = 0;
48372 + sts.b_iso_out.pid = 0;
48373 + sts.b_iso_out.framenum = 0;
48374 +
48375 + offset = 0;
48376 + for (i = 0; i < dwc_ep->desc_cnt - dwc_ep->pkt_per_frm;
48377 + i += dwc_ep->pkt_per_frm) {
48378 +
48379 + for (j = 0; j < dwc_ep->pkt_per_frm; ++j) {
48380 + uint32_t len = (j + 1) * dwc_ep->maxpacket;
48381 + if (len > dwc_ep->data_per_frame)
48382 + data_per_desc =
48383 + dwc_ep->data_per_frame -
48384 + j * dwc_ep->maxpacket;
48385 + else
48386 + data_per_desc = dwc_ep->maxpacket;
48387 + len = data_per_desc % 4;
48388 + if (len)
48389 + data_per_desc += 4 - len;
48390 +
48391 + sts.b_iso_out.rxbytes = data_per_desc;
48392 + dma_desc->buf = dma_ad;
48393 + dma_desc->status.d32 = sts.d32;
48394 +
48395 + offset += data_per_desc;
48396 + dma_desc++;
48397 + dma_ad += data_per_desc;
48398 + }
48399 + }
48400 +
48401 + for (j = 0; j < dwc_ep->pkt_per_frm - 1; ++j) {
48402 + uint32_t len = (j + 1) * dwc_ep->maxpacket;
48403 + if (len > dwc_ep->data_per_frame)
48404 + data_per_desc =
48405 + dwc_ep->data_per_frame -
48406 + j * dwc_ep->maxpacket;
48407 + else
48408 + data_per_desc = dwc_ep->maxpacket;
48409 + len = data_per_desc % 4;
48410 + if (len)
48411 + data_per_desc += 4 - len;
48412 + sts.b_iso_out.rxbytes = data_per_desc;
48413 + dma_desc->buf = dma_ad;
48414 + dma_desc->status.d32 = sts.d32;
48415 +
48416 + offset += data_per_desc;
48417 + dma_desc++;
48418 + dma_ad += data_per_desc;
48419 + }
48420 +
48421 + sts.b_iso_out.ioc = 1;
48422 + len = (j + 1) * dwc_ep->maxpacket;
48423 + if (len > dwc_ep->data_per_frame)
48424 + data_per_desc =
48425 + dwc_ep->data_per_frame - j * dwc_ep->maxpacket;
48426 + else
48427 + data_per_desc = dwc_ep->maxpacket;
48428 + len = data_per_desc % 4;
48429 + if (len)
48430 + data_per_desc += 4 - len;
48431 + sts.b_iso_out.rxbytes = data_per_desc;
48432 +
48433 + dma_desc->buf = dma_ad;
48434 + dma_desc->status.d32 = sts.d32;
48435 + dma_desc++;
48436 +
48437 + /** Buffer 1 descriptors setup */
48438 + sts.b_iso_out.ioc = 0;
48439 + dma_ad = dwc_ep->dma_addr1;
48440 +
48441 + offset = 0;
48442 + for (i = 0; i < dwc_ep->desc_cnt - dwc_ep->pkt_per_frm;
48443 + i += dwc_ep->pkt_per_frm) {
48444 + for (j = 0; j < dwc_ep->pkt_per_frm; ++j) {
48445 + uint32_t len = (j + 1) * dwc_ep->maxpacket;
48446 + if (len > dwc_ep->data_per_frame)
48447 + data_per_desc =
48448 + dwc_ep->data_per_frame -
48449 + j * dwc_ep->maxpacket;
48450 + else
48451 + data_per_desc = dwc_ep->maxpacket;
48452 + len = data_per_desc % 4;
48453 + if (len)
48454 + data_per_desc += 4 - len;
48455 +
48456 + data_per_desc =
48457 + sts.b_iso_out.rxbytes = data_per_desc;
48458 + dma_desc->buf = dma_ad;
48459 + dma_desc->status.d32 = sts.d32;
48460 +
48461 + offset += data_per_desc;
48462 + dma_desc++;
48463 + dma_ad += data_per_desc;
48464 + }
48465 + }
48466 + for (j = 0; j < dwc_ep->pkt_per_frm - 1; ++j) {
48467 + data_per_desc =
48468 + ((j + 1) * dwc_ep->maxpacket >
48469 + dwc_ep->data_per_frame) ? dwc_ep->data_per_frame -
48470 + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
48471 + data_per_desc +=
48472 + (data_per_desc % 4) ? (4 - data_per_desc % 4) : 0;
48473 + sts.b_iso_out.rxbytes = data_per_desc;
48474 + dma_desc->buf = dma_ad;
48475 + dma_desc->status.d32 = sts.d32;
48476 +
48477 + offset += data_per_desc;
48478 + dma_desc++;
48479 + dma_ad += data_per_desc;
48480 + }
48481 +
48482 + sts.b_iso_out.ioc = 1;
48483 + sts.b_iso_out.l = 1;
48484 + data_per_desc =
48485 + ((j + 1) * dwc_ep->maxpacket >
48486 + dwc_ep->data_per_frame) ? dwc_ep->data_per_frame -
48487 + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
48488 + data_per_desc +=
48489 + (data_per_desc % 4) ? (4 - data_per_desc % 4) : 0;
48490 + sts.b_iso_out.rxbytes = data_per_desc;
48491 +
48492 + dma_desc->buf = dma_ad;
48493 + dma_desc->status.d32 = sts.d32;
48494 +
48495 + dwc_ep->next_frame = 0;
48496 +
48497 + /** Write dma_ad into DOEPDMA register */
48498 + DWC_WRITE_REG32(&(out_regs->doepdma),
48499 + (uint32_t) dwc_ep->iso_dma_desc_addr);
48500 +
48501 + }
48502 + /** ISO IN EP */
48503 + else {
48504 + dev_dma_desc_sts_t sts = {.d32 = 0 };
48505 + dwc_otg_dev_dma_desc_t *dma_desc = dwc_ep->iso_desc_addr;
48506 + dma_addr_t dma_ad;
48507 + dwc_otg_dev_in_ep_regs_t *in_regs =
48508 + core_if->dev_if->in_ep_regs[dwc_ep->num];
48509 + unsigned int frmnumber;
48510 + fifosize_data_t txfifosize, rxfifosize;
48511 +
48512 + txfifosize.d32 =
48513 + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[dwc_ep->num]->
48514 + dtxfsts);
48515 + rxfifosize.d32 =
48516 + DWC_READ_REG32(&core_if->core_global_regs->grxfsiz);
48517 +
48518 + addr = &core_if->dev_if->in_ep_regs[dwc_ep->num]->diepctl;
48519 +
48520 + dma_ad = dwc_ep->dma_addr0;
48521 +
48522 + dsts.d32 =
48523 + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
48524 +
48525 + sts.b_iso_in.bs = BS_HOST_READY;
48526 + sts.b_iso_in.txsts = 0;
48527 + sts.b_iso_in.sp =
48528 + (dwc_ep->data_per_frame % dwc_ep->maxpacket) ? 1 : 0;
48529 + sts.b_iso_in.ioc = 0;
48530 + sts.b_iso_in.pid = dwc_ep->pkt_per_frm;
48531 +
48532 + frmnumber = dwc_ep->next_frame;
48533 +
48534 + sts.b_iso_in.framenum = frmnumber;
48535 + sts.b_iso_in.txbytes = dwc_ep->data_per_frame;
48536 + sts.b_iso_in.l = 0;
48537 +
48538 + /** Buffer 0 descriptors setup */
48539 + for (i = 0; i < dwc_ep->desc_cnt - 1; i++) {
48540 + dma_desc->buf = dma_ad;
48541 + dma_desc->status.d32 = sts.d32;
48542 + dma_desc++;
48543 +
48544 + dma_ad += dwc_ep->data_per_frame;
48545 + sts.b_iso_in.framenum += dwc_ep->bInterval;
48546 + }
48547 +
48548 + sts.b_iso_in.ioc = 1;
48549 + dma_desc->buf = dma_ad;
48550 + dma_desc->status.d32 = sts.d32;
48551 + ++dma_desc;
48552 +
48553 + /** Buffer 1 descriptors setup */
48554 + sts.b_iso_in.ioc = 0;
48555 + dma_ad = dwc_ep->dma_addr1;
48556 +
48557 + for (i = 0; i < dwc_ep->desc_cnt - dwc_ep->pkt_per_frm;
48558 + i += dwc_ep->pkt_per_frm) {
48559 + dma_desc->buf = dma_ad;
48560 + dma_desc->status.d32 = sts.d32;
48561 + dma_desc++;
48562 +
48563 + dma_ad += dwc_ep->data_per_frame;
48564 + sts.b_iso_in.framenum += dwc_ep->bInterval;
48565 +
48566 + sts.b_iso_in.ioc = 0;
48567 + }
48568 + sts.b_iso_in.ioc = 1;
48569 + sts.b_iso_in.l = 1;
48570 +
48571 + dma_desc->buf = dma_ad;
48572 + dma_desc->status.d32 = sts.d32;
48573 +
48574 + dwc_ep->next_frame = sts.b_iso_in.framenum + dwc_ep->bInterval;
48575 +
48576 + /** Write dma_ad into diepdma register */
48577 + DWC_WRITE_REG32(&(in_regs->diepdma),
48578 + (uint32_t) dwc_ep->iso_dma_desc_addr);
48579 + }
48580 + /** Enable endpoint, clear nak */
48581 + depctl.d32 = 0;
48582 + depctl.b.epena = 1;
48583 + depctl.b.usbactep = 1;
48584 + depctl.b.cnak = 1;
48585 +
48586 + DWC_MODIFY_REG32(addr, depctl.d32, depctl.d32);
48587 + depctl.d32 = DWC_READ_REG32(addr);
48588 +}
48589 +
48590 +/**
48591 + * This function initializes a descriptor chain for Isochronous transfer
48592 + *
48593 + * @param core_if Programming view of DWC_otg controller.
48594 + * @param ep The EP to start the transfer on.
48595 + *
48596 + */
48597 +void dwc_otg_iso_ep_start_buf_transfer(dwc_otg_core_if_t * core_if,
48598 + dwc_ep_t * ep)
48599 +{
48600 + depctl_data_t depctl = {.d32 = 0 };
48601 + volatile uint32_t *addr;
48602 +
48603 + if (ep->is_in) {
48604 + addr = &core_if->dev_if->in_ep_regs[ep->num]->diepctl;
48605 + } else {
48606 + addr = &core_if->dev_if->out_ep_regs[ep->num]->doepctl;
48607 + }
48608 +
48609 + if (core_if->dma_enable == 0 || core_if->dma_desc_enable != 0) {
48610 + return;
48611 + } else {
48612 + deptsiz_data_t deptsiz = {.d32 = 0 };
48613 +
48614 + ep->xfer_len =
48615 + ep->data_per_frame * ep->buf_proc_intrvl / ep->bInterval;
48616 + ep->pkt_cnt =
48617 + (ep->xfer_len - 1 + ep->maxpacket) / ep->maxpacket;
48618 + ep->xfer_count = 0;
48619 + ep->xfer_buff =
48620 + (ep->proc_buf_num) ? ep->xfer_buff1 : ep->xfer_buff0;
48621 + ep->dma_addr =
48622 + (ep->proc_buf_num) ? ep->dma_addr1 : ep->dma_addr0;
48623 +
48624 + if (ep->is_in) {
48625 + /* Program the transfer size and packet count
48626 + * as follows: xfersize = N * maxpacket +
48627 + * short_packet pktcnt = N + (short_packet
48628 + * exist ? 1 : 0)
48629 + */
48630 + deptsiz.b.mc = ep->pkt_per_frm;
48631 + deptsiz.b.xfersize = ep->xfer_len;
48632 + deptsiz.b.pktcnt =
48633 + (ep->xfer_len - 1 + ep->maxpacket) / ep->maxpacket;
48634 + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
48635 + dieptsiz, deptsiz.d32);
48636 +
48637 + /* Write the DMA register */
48638 + DWC_WRITE_REG32(&
48639 + (core_if->dev_if->in_ep_regs[ep->num]->
48640 + diepdma), (uint32_t) ep->dma_addr);
48641 +
48642 + } else {
48643 + deptsiz.b.pktcnt =
48644 + (ep->xfer_len + (ep->maxpacket - 1)) /
48645 + ep->maxpacket;
48646 + deptsiz.b.xfersize = deptsiz.b.pktcnt * ep->maxpacket;
48647 +
48648 + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[ep->num]->
48649 + doeptsiz, deptsiz.d32);
48650 +
48651 + /* Write the DMA register */
48652 + DWC_WRITE_REG32(&
48653 + (core_if->dev_if->out_ep_regs[ep->num]->
48654 + doepdma), (uint32_t) ep->dma_addr);
48655 +
48656 + }
48657 + /** Enable endpoint, clear nak */
48658 + depctl.d32 = 0;
48659 + depctl.b.epena = 1;
48660 + depctl.b.cnak = 1;
48661 +
48662 + DWC_MODIFY_REG32(addr, depctl.d32, depctl.d32);
48663 + }
48664 +}
48665 +
48666 +/**
48667 + * This function does the setup for a data transfer for an EP and
48668 + * starts the transfer. For an IN transfer, the packets will be
48669 + * loaded into the appropriate Tx FIFO in the ISR. For OUT transfers,
48670 + * the packets are unloaded from the Rx FIFO in the ISR.
48671 + *
48672 + * @param core_if Programming view of DWC_otg controller.
48673 + * @param ep The EP to start the transfer on.
48674 + */
48675 +
48676 +static void dwc_otg_iso_ep_start_transfer(dwc_otg_core_if_t * core_if,
48677 + dwc_ep_t * ep)
48678 +{
48679 + if (core_if->dma_enable) {
48680 + if (core_if->dma_desc_enable) {
48681 + if (ep->is_in) {
48682 + ep->desc_cnt = ep->pkt_cnt / ep->pkt_per_frm;
48683 + } else {
48684 + ep->desc_cnt = ep->pkt_cnt;
48685 + }
48686 + dwc_otg_iso_ep_start_ddma_transfer(core_if, ep);
48687 + } else {
48688 + if (core_if->pti_enh_enable) {
48689 + dwc_otg_iso_ep_start_buf_transfer(core_if, ep);
48690 + } else {
48691 + ep->cur_pkt_addr =
48692 + (ep->proc_buf_num) ? ep->xfer_buff1 : ep->
48693 + xfer_buff0;
48694 + ep->cur_pkt_dma_addr =
48695 + (ep->proc_buf_num) ? ep->dma_addr1 : ep->
48696 + dma_addr0;
48697 + dwc_otg_iso_ep_start_frm_transfer(core_if, ep);
48698 + }
48699 + }
48700 + } else {
48701 + ep->cur_pkt_addr =
48702 + (ep->proc_buf_num) ? ep->xfer_buff1 : ep->xfer_buff0;
48703 + ep->cur_pkt_dma_addr =
48704 + (ep->proc_buf_num) ? ep->dma_addr1 : ep->dma_addr0;
48705 + dwc_otg_iso_ep_start_frm_transfer(core_if, ep);
48706 + }
48707 +}
48708 +
48709 +/**
48710 + * This function stops transfer for an EP and
48711 + * resets the ep's variables.
48712 + *
48713 + * @param core_if Programming view of DWC_otg controller.
48714 + * @param ep The EP to start the transfer on.
48715 + */
48716 +
48717 +void dwc_otg_iso_ep_stop_transfer(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
48718 +{
48719 + depctl_data_t depctl = {.d32 = 0 };
48720 + volatile uint32_t *addr;
48721 +
48722 + if (ep->is_in == 1) {
48723 + addr = &core_if->dev_if->in_ep_regs[ep->num]->diepctl;
48724 + } else {
48725 + addr = &core_if->dev_if->out_ep_regs[ep->num]->doepctl;
48726 + }
48727 +
48728 + /* disable the ep */
48729 + depctl.d32 = DWC_READ_REG32(addr);
48730 +
48731 + depctl.b.epdis = 1;
48732 + depctl.b.snak = 1;
48733 +
48734 + DWC_WRITE_REG32(addr, depctl.d32);
48735 +
48736 + if (core_if->dma_desc_enable &&
48737 + ep->iso_desc_addr && ep->iso_dma_desc_addr) {
48738 + dwc_otg_ep_free_desc_chain(ep->iso_desc_addr,
48739 + ep->iso_dma_desc_addr,
48740 + ep->desc_cnt * 2);
48741 + }
48742 +
48743 + /* reset varibales */
48744 + ep->dma_addr0 = 0;
48745 + ep->dma_addr1 = 0;
48746 + ep->xfer_buff0 = 0;
48747 + ep->xfer_buff1 = 0;
48748 + ep->data_per_frame = 0;
48749 + ep->data_pattern_frame = 0;
48750 + ep->sync_frame = 0;
48751 + ep->buf_proc_intrvl = 0;
48752 + ep->bInterval = 0;
48753 + ep->proc_buf_num = 0;
48754 + ep->pkt_per_frm = 0;
48755 + ep->pkt_per_frm = 0;
48756 + ep->desc_cnt = 0;
48757 + ep->iso_desc_addr = 0;
48758 + ep->iso_dma_desc_addr = 0;
48759 +}
48760 +
48761 +int dwc_otg_pcd_iso_ep_start(dwc_otg_pcd_t * pcd, void *ep_handle,
48762 + uint8_t * buf0, uint8_t * buf1, dwc_dma_t dma0,
48763 + dwc_dma_t dma1, int sync_frame, int dp_frame,
48764 + int data_per_frame, int start_frame,
48765 + int buf_proc_intrvl, void *req_handle,
48766 + int atomic_alloc)
48767 +{
48768 + dwc_otg_pcd_ep_t *ep;
48769 + dwc_irqflags_t flags = 0;
48770 + dwc_ep_t *dwc_ep;
48771 + int32_t frm_data;
48772 + dsts_data_t dsts;
48773 + dwc_otg_core_if_t *core_if;
48774 +
48775 + ep = get_ep_from_handle(pcd, ep_handle);
48776 +
48777 + if (!ep || !ep->desc || ep->dwc_ep.num == 0) {
48778 + DWC_WARN("bad ep\n");
48779 + return -DWC_E_INVALID;
48780 + }
48781 +
48782 + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
48783 + core_if = GET_CORE_IF(pcd);
48784 + dwc_ep = &ep->dwc_ep;
48785 +
48786 + if (ep->iso_req_handle) {
48787 + DWC_WARN("ISO request in progress\n");
48788 + }
48789 +
48790 + dwc_ep->dma_addr0 = dma0;
48791 + dwc_ep->dma_addr1 = dma1;
48792 +
48793 + dwc_ep->xfer_buff0 = buf0;
48794 + dwc_ep->xfer_buff1 = buf1;
48795 +
48796 + dwc_ep->data_per_frame = data_per_frame;
48797 +
48798 + /** @todo - pattern data support is to be implemented in the future */
48799 + dwc_ep->data_pattern_frame = dp_frame;
48800 + dwc_ep->sync_frame = sync_frame;
48801 +
48802 + dwc_ep->buf_proc_intrvl = buf_proc_intrvl;
48803 +
48804 + dwc_ep->bInterval = 1 << (ep->desc->bInterval - 1);
48805 +
48806 + dwc_ep->proc_buf_num = 0;
48807 +
48808 + dwc_ep->pkt_per_frm = 0;
48809 + frm_data = ep->dwc_ep.data_per_frame;
48810 + while (frm_data > 0) {
48811 + dwc_ep->pkt_per_frm++;
48812 + frm_data -= ep->dwc_ep.maxpacket;
48813 + }
48814 +
48815 + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
48816 +
48817 + if (start_frame == -1) {
48818 + dwc_ep->next_frame = dsts.b.soffn + 1;
48819 + if (dwc_ep->bInterval != 1) {
48820 + dwc_ep->next_frame =
48821 + dwc_ep->next_frame + (dwc_ep->bInterval - 1 -
48822 + dwc_ep->next_frame %
48823 + dwc_ep->bInterval);
48824 + }
48825 + } else {
48826 + dwc_ep->next_frame = start_frame;
48827 + }
48828 +
48829 + if (!core_if->pti_enh_enable) {
48830 + dwc_ep->pkt_cnt =
48831 + dwc_ep->buf_proc_intrvl * dwc_ep->pkt_per_frm /
48832 + dwc_ep->bInterval;
48833 + } else {
48834 + dwc_ep->pkt_cnt =
48835 + (dwc_ep->data_per_frame *
48836 + (dwc_ep->buf_proc_intrvl / dwc_ep->bInterval)
48837 + - 1 + dwc_ep->maxpacket) / dwc_ep->maxpacket;
48838 + }
48839 +
48840 + if (core_if->dma_desc_enable) {
48841 + dwc_ep->desc_cnt =
48842 + dwc_ep->buf_proc_intrvl * dwc_ep->pkt_per_frm /
48843 + dwc_ep->bInterval;
48844 + }
48845 +
48846 + if (atomic_alloc) {
48847 + dwc_ep->pkt_info =
48848 + DWC_ALLOC_ATOMIC(sizeof(iso_pkt_info_t) * dwc_ep->pkt_cnt);
48849 + } else {
48850 + dwc_ep->pkt_info =
48851 + DWC_ALLOC(sizeof(iso_pkt_info_t) * dwc_ep->pkt_cnt);
48852 + }
48853 + if (!dwc_ep->pkt_info) {
48854 + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
48855 + return -DWC_E_NO_MEMORY;
48856 + }
48857 + if (core_if->pti_enh_enable) {
48858 + dwc_memset(dwc_ep->pkt_info, 0,
48859 + sizeof(iso_pkt_info_t) * dwc_ep->pkt_cnt);
48860 + }
48861 +
48862 + dwc_ep->cur_pkt = 0;
48863 + ep->iso_req_handle = req_handle;
48864 +
48865 + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
48866 + dwc_otg_iso_ep_start_transfer(core_if, dwc_ep);
48867 + return 0;
48868 +}
48869 +
48870 +int dwc_otg_pcd_iso_ep_stop(dwc_otg_pcd_t * pcd, void *ep_handle,
48871 + void *req_handle)
48872 +{
48873 + dwc_irqflags_t flags = 0;
48874 + dwc_otg_pcd_ep_t *ep;
48875 + dwc_ep_t *dwc_ep;
48876 +
48877 + ep = get_ep_from_handle(pcd, ep_handle);
48878 + if (!ep || !ep->desc || ep->dwc_ep.num == 0) {
48879 + DWC_WARN("bad ep\n");
48880 + return -DWC_E_INVALID;
48881 + }
48882 + dwc_ep = &ep->dwc_ep;
48883 +
48884 + dwc_otg_iso_ep_stop_transfer(GET_CORE_IF(pcd), dwc_ep);
48885 +
48886 + DWC_FREE(dwc_ep->pkt_info);
48887 + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
48888 + if (ep->iso_req_handle != req_handle) {
48889 + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
48890 + return -DWC_E_INVALID;
48891 + }
48892 +
48893 + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
48894 +
48895 + ep->iso_req_handle = 0;
48896 + return 0;
48897 +}
48898 +
48899 +/**
48900 + * This function is used for perodical data exchnage between PCD and gadget drivers.
48901 + * for Isochronous EPs
48902 + *
48903 + * - Every time a sync period completes this function is called to
48904 + * perform data exchange between PCD and gadget
48905 + */
48906 +void dwc_otg_iso_buffer_done(dwc_otg_pcd_t * pcd, dwc_otg_pcd_ep_t * ep,
48907 + void *req_handle)
48908 +{
48909 + int i;
48910 + dwc_ep_t *dwc_ep;
48911 +
48912 + dwc_ep = &ep->dwc_ep;
48913 +
48914 + DWC_SPINUNLOCK(ep->pcd->lock);
48915 + pcd->fops->isoc_complete(pcd, ep->priv, ep->iso_req_handle,
48916 + dwc_ep->proc_buf_num ^ 0x1);
48917 + DWC_SPINLOCK(ep->pcd->lock);
48918 +
48919 + for (i = 0; i < dwc_ep->pkt_cnt; ++i) {
48920 + dwc_ep->pkt_info[i].status = 0;
48921 + dwc_ep->pkt_info[i].offset = 0;
48922 + dwc_ep->pkt_info[i].length = 0;
48923 + }
48924 +}
48925 +
48926 +int dwc_otg_pcd_get_iso_packet_count(dwc_otg_pcd_t * pcd, void *ep_handle,
48927 + void *iso_req_handle)
48928 +{
48929 + dwc_otg_pcd_ep_t *ep;
48930 + dwc_ep_t *dwc_ep;
48931 +
48932 + ep = get_ep_from_handle(pcd, ep_handle);
48933 + if (!ep->desc || ep->dwc_ep.num == 0) {
48934 + DWC_WARN("bad ep\n");
48935 + return -DWC_E_INVALID;
48936 + }
48937 + dwc_ep = &ep->dwc_ep;
48938 +
48939 + return dwc_ep->pkt_cnt;
48940 +}
48941 +
48942 +void dwc_otg_pcd_get_iso_packet_params(dwc_otg_pcd_t * pcd, void *ep_handle,
48943 + void *iso_req_handle, int packet,
48944 + int *status, int *actual, int *offset)
48945 +{
48946 + dwc_otg_pcd_ep_t *ep;
48947 + dwc_ep_t *dwc_ep;
48948 +
48949 + ep = get_ep_from_handle(pcd, ep_handle);
48950 + if (!ep)
48951 + DWC_WARN("bad ep\n");
48952 +
48953 + dwc_ep = &ep->dwc_ep;
48954 +
48955 + *status = dwc_ep->pkt_info[packet].status;
48956 + *actual = dwc_ep->pkt_info[packet].length;
48957 + *offset = dwc_ep->pkt_info[packet].offset;
48958 +}
48959 +
48960 +#endif /* DWC_EN_ISOC */
48961 +
48962 +static void dwc_otg_pcd_init_ep(dwc_otg_pcd_t * pcd, dwc_otg_pcd_ep_t * pcd_ep,
48963 + uint32_t is_in, uint32_t ep_num)
48964 +{
48965 + /* Init EP structure */
48966 + pcd_ep->desc = 0;
48967 + pcd_ep->pcd = pcd;
48968 + pcd_ep->stopped = 1;
48969 + pcd_ep->queue_sof = 0;
48970 +
48971 + /* Init DWC ep structure */
48972 + pcd_ep->dwc_ep.is_in = is_in;
48973 + pcd_ep->dwc_ep.num = ep_num;
48974 + pcd_ep->dwc_ep.active = 0;
48975 + pcd_ep->dwc_ep.tx_fifo_num = 0;
48976 + /* Control until ep is actvated */
48977 + pcd_ep->dwc_ep.type = DWC_OTG_EP_TYPE_CONTROL;
48978 + pcd_ep->dwc_ep.maxpacket = MAX_PACKET_SIZE;
48979 + pcd_ep->dwc_ep.dma_addr = 0;
48980 + pcd_ep->dwc_ep.start_xfer_buff = 0;
48981 + pcd_ep->dwc_ep.xfer_buff = 0;
48982 + pcd_ep->dwc_ep.xfer_len = 0;
48983 + pcd_ep->dwc_ep.xfer_count = 0;
48984 + pcd_ep->dwc_ep.sent_zlp = 0;
48985 + pcd_ep->dwc_ep.total_len = 0;
48986 + pcd_ep->dwc_ep.desc_addr = 0;
48987 + pcd_ep->dwc_ep.dma_desc_addr = 0;
48988 + DWC_CIRCLEQ_INIT(&pcd_ep->queue);
48989 +}
48990 +
48991 +/**
48992 + * Initialize ep's
48993 + */
48994 +static void dwc_otg_pcd_reinit(dwc_otg_pcd_t * pcd)
48995 +{
48996 + int i;
48997 + uint32_t hwcfg1;
48998 + dwc_otg_pcd_ep_t *ep;
48999 + int in_ep_cntr, out_ep_cntr;
49000 + uint32_t num_in_eps = (GET_CORE_IF(pcd))->dev_if->num_in_eps;
49001 + uint32_t num_out_eps = (GET_CORE_IF(pcd))->dev_if->num_out_eps;
49002 +
49003 + /**
49004 + * Initialize the EP0 structure.
49005 + */
49006 + ep = &pcd->ep0;
49007 + dwc_otg_pcd_init_ep(pcd, ep, 0, 0);
49008 +
49009 + in_ep_cntr = 0;
49010 + hwcfg1 = (GET_CORE_IF(pcd))->hwcfg1.d32 >> 3;
49011 + for (i = 1; in_ep_cntr < num_in_eps; i++) {
49012 + if ((hwcfg1 & 0x1) == 0) {
49013 + dwc_otg_pcd_ep_t *ep = &pcd->in_ep[in_ep_cntr];
49014 + in_ep_cntr++;
49015 + /**
49016 + * @todo NGS: Add direction to EP, based on contents
49017 + * of HWCFG1. Need a copy of HWCFG1 in pcd structure?
49018 + * sprintf(";r
49019 + */
49020 + dwc_otg_pcd_init_ep(pcd, ep, 1 /* IN */ , i);
49021 +
49022 + DWC_CIRCLEQ_INIT(&ep->queue);
49023 + }
49024 + hwcfg1 >>= 2;
49025 + }
49026 +
49027 + out_ep_cntr = 0;
49028 + hwcfg1 = (GET_CORE_IF(pcd))->hwcfg1.d32 >> 2;
49029 + for (i = 1; out_ep_cntr < num_out_eps; i++) {
49030 + if ((hwcfg1 & 0x1) == 0) {
49031 + dwc_otg_pcd_ep_t *ep = &pcd->out_ep[out_ep_cntr];
49032 + out_ep_cntr++;
49033 + /**
49034 + * @todo NGS: Add direction to EP, based on contents
49035 + * of HWCFG1. Need a copy of HWCFG1 in pcd structure?
49036 + * sprintf(";r
49037 + */
49038 + dwc_otg_pcd_init_ep(pcd, ep, 0 /* OUT */ , i);
49039 + DWC_CIRCLEQ_INIT(&ep->queue);
49040 + }
49041 + hwcfg1 >>= 2;
49042 + }
49043 +
49044 + pcd->ep0state = EP0_DISCONNECT;
49045 + pcd->ep0.dwc_ep.maxpacket = MAX_EP0_SIZE;
49046 + pcd->ep0.dwc_ep.type = DWC_OTG_EP_TYPE_CONTROL;
49047 +}
49048 +
49049 +/**
49050 + * This function is called when the SRP timer expires. The SRP should
49051 + * complete within 6 seconds.
49052 + */
49053 +static void srp_timeout(void *ptr)
49054 +{
49055 + gotgctl_data_t gotgctl;
49056 + dwc_otg_core_if_t *core_if = (dwc_otg_core_if_t *) ptr;
49057 + volatile uint32_t *addr = &core_if->core_global_regs->gotgctl;
49058 +
49059 + gotgctl.d32 = DWC_READ_REG32(addr);
49060 +
49061 + core_if->srp_timer_started = 0;
49062 +
49063 + if (core_if->adp_enable) {
49064 + if (gotgctl.b.bsesvld == 0) {
49065 + gpwrdn_data_t gpwrdn = {.d32 = 0 };
49066 + DWC_PRINTF("SRP Timeout BSESSVLD = 0\n");
49067 + /* Power off the core */
49068 + if (core_if->power_down == 2) {
49069 + gpwrdn.b.pwrdnswtch = 1;
49070 + DWC_MODIFY_REG32(&core_if->
49071 + core_global_regs->gpwrdn,
49072 + gpwrdn.d32, 0);
49073 + }
49074 +
49075 + gpwrdn.d32 = 0;
49076 + gpwrdn.b.pmuintsel = 1;
49077 + gpwrdn.b.pmuactv = 1;
49078 + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0,
49079 + gpwrdn.d32);
49080 + dwc_otg_adp_probe_start(core_if);
49081 + } else {
49082 + DWC_PRINTF("SRP Timeout BSESSVLD = 1\n");
49083 + core_if->op_state = B_PERIPHERAL;
49084 + dwc_otg_core_init(core_if);
49085 + dwc_otg_enable_global_interrupts(core_if);
49086 + cil_pcd_start(core_if);
49087 + }
49088 + }
49089 +
49090 + if ((core_if->core_params->phy_type == DWC_PHY_TYPE_PARAM_FS) &&
49091 + (core_if->core_params->i2c_enable)) {
49092 + DWC_PRINTF("SRP Timeout\n");
49093 +
49094 + if ((core_if->srp_success) && (gotgctl.b.bsesvld)) {
49095 + if (core_if->pcd_cb && core_if->pcd_cb->resume_wakeup) {
49096 + core_if->pcd_cb->resume_wakeup(core_if->pcd_cb->p);
49097 + }
49098 +
49099 + /* Clear Session Request */
49100 + gotgctl.d32 = 0;
49101 + gotgctl.b.sesreq = 1;
49102 + DWC_MODIFY_REG32(&core_if->core_global_regs->gotgctl,
49103 + gotgctl.d32, 0);
49104 +
49105 + core_if->srp_success = 0;
49106 + } else {
49107 + __DWC_ERROR("Device not connected/responding\n");
49108 + gotgctl.b.sesreq = 0;
49109 + DWC_WRITE_REG32(addr, gotgctl.d32);
49110 + }
49111 + } else if (gotgctl.b.sesreq) {
49112 + DWC_PRINTF("SRP Timeout\n");
49113 +
49114 + __DWC_ERROR("Device not connected/responding\n");
49115 + gotgctl.b.sesreq = 0;
49116 + DWC_WRITE_REG32(addr, gotgctl.d32);
49117 + } else {
49118 + DWC_PRINTF(" SRP GOTGCTL=%0x\n", gotgctl.d32);
49119 + }
49120 +}
49121 +
49122 +/**
49123 + * Tasklet
49124 + *
49125 + */
49126 +extern void start_next_request(dwc_otg_pcd_ep_t * ep);
49127 +
49128 +static void start_xfer_tasklet_func(void *data)
49129 +{
49130 + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) data;
49131 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
49132 +
49133 + int i;
49134 + depctl_data_t diepctl;
49135 +
49136 + DWC_DEBUGPL(DBG_PCDV, "Start xfer tasklet\n");
49137 +
49138 + diepctl.d32 = DWC_READ_REG32(&core_if->dev_if->in_ep_regs[0]->diepctl);
49139 +
49140 + if (pcd->ep0.queue_sof) {
49141 + pcd->ep0.queue_sof = 0;
49142 + start_next_request(&pcd->ep0);
49143 + // break;
49144 + }
49145 +
49146 + for (i = 0; i < core_if->dev_if->num_in_eps; i++) {
49147 + depctl_data_t diepctl;
49148 + diepctl.d32 =
49149 + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[i]->diepctl);
49150 +
49151 + if (pcd->in_ep[i].queue_sof) {
49152 + pcd->in_ep[i].queue_sof = 0;
49153 + start_next_request(&pcd->in_ep[i]);
49154 + // break;
49155 + }
49156 + }
49157 +
49158 + return;
49159 +}
49160 +
49161 +/**
49162 + * This function initialized the PCD portion of the driver.
49163 + *
49164 + */
49165 +dwc_otg_pcd_t *dwc_otg_pcd_init(dwc_otg_device_t *otg_dev)
49166 +{
49167 + struct device *dev = &otg_dev->os_dep.platformdev->dev;
49168 + dwc_otg_core_if_t *core_if = otg_dev->core_if;
49169 + dwc_otg_pcd_t *pcd = NULL;
49170 + dwc_otg_dev_if_t *dev_if;
49171 + int i;
49172 +
49173 + /*
49174 + * Allocate PCD structure
49175 + */
49176 + pcd = DWC_ALLOC(sizeof(dwc_otg_pcd_t));
49177 +
49178 + if (pcd == NULL) {
49179 + return NULL;
49180 + }
49181 +
49182 +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_SPINLOCK))
49183 + DWC_SPINLOCK_ALLOC_LINUX_DEBUG(pcd->lock);
49184 +#else
49185 + pcd->lock = DWC_SPINLOCK_ALLOC();
49186 +#endif
49187 + DWC_DEBUGPL(DBG_HCDV, "Init of PCD %p given core_if %p\n",
49188 + pcd, core_if);//GRAYG
49189 + if (!pcd->lock) {
49190 + DWC_ERROR("Could not allocate lock for pcd");
49191 + DWC_FREE(pcd);
49192 + return NULL;
49193 + }
49194 + /* Set core_if's lock pointer to hcd->lock */
49195 + core_if->lock = pcd->lock;
49196 + pcd->core_if = core_if;
49197 +
49198 + dev_if = core_if->dev_if;
49199 + dev_if->isoc_ep = NULL;
49200 +
49201 + if (core_if->hwcfg4.b.ded_fifo_en) {
49202 + DWC_PRINTF("Dedicated Tx FIFOs mode\n");
49203 + } else {
49204 + DWC_PRINTF("Shared Tx FIFO mode\n");
49205 + }
49206 +
49207 + /*
49208 + * Initialized the Core for Device mode here if there is nod ADP support.
49209 + * Otherwise it will be done later in dwc_otg_adp_start routine.
49210 + */
49211 + if (dwc_otg_is_device_mode(core_if) /*&& !core_if->adp_enable*/) {
49212 + dwc_otg_core_dev_init(core_if);
49213 + }
49214 +
49215 + /*
49216 + * Register the PCD Callbacks.
49217 + */
49218 + dwc_otg_cil_register_pcd_callbacks(core_if, &pcd_callbacks, pcd);
49219 +
49220 + /*
49221 + * Initialize the DMA buffer for SETUP packets
49222 + */
49223 + if (GET_CORE_IF(pcd)->dma_enable) {
49224 + pcd->setup_pkt =
49225 + DWC_DMA_ALLOC(dev, sizeof(*pcd->setup_pkt) * 5,
49226 + &pcd->setup_pkt_dma_handle);
49227 + if (pcd->setup_pkt == NULL) {
49228 + DWC_FREE(pcd);
49229 + return NULL;
49230 + }
49231 +
49232 + pcd->status_buf =
49233 + DWC_DMA_ALLOC(dev, sizeof(uint16_t),
49234 + &pcd->status_buf_dma_handle);
49235 + if (pcd->status_buf == NULL) {
49236 + DWC_DMA_FREE(dev, sizeof(*pcd->setup_pkt) * 5,
49237 + pcd->setup_pkt, pcd->setup_pkt_dma_handle);
49238 + DWC_FREE(pcd);
49239 + return NULL;
49240 + }
49241 +
49242 + if (GET_CORE_IF(pcd)->dma_desc_enable) {
49243 + dev_if->setup_desc_addr[0] =
49244 + dwc_otg_ep_alloc_desc_chain(dev,
49245 + &dev_if->dma_setup_desc_addr[0], 1);
49246 + dev_if->setup_desc_addr[1] =
49247 + dwc_otg_ep_alloc_desc_chain(dev,
49248 + &dev_if->dma_setup_desc_addr[1], 1);
49249 + dev_if->in_desc_addr =
49250 + dwc_otg_ep_alloc_desc_chain(dev,
49251 + &dev_if->dma_in_desc_addr, 1);
49252 + dev_if->out_desc_addr =
49253 + dwc_otg_ep_alloc_desc_chain(dev,
49254 + &dev_if->dma_out_desc_addr, 1);
49255 + pcd->data_terminated = 0;
49256 +
49257 + if (dev_if->setup_desc_addr[0] == 0
49258 + || dev_if->setup_desc_addr[1] == 0
49259 + || dev_if->in_desc_addr == 0
49260 + || dev_if->out_desc_addr == 0) {
49261 +
49262 + if (dev_if->out_desc_addr)
49263 + dwc_otg_ep_free_desc_chain(dev,
49264 + dev_if->out_desc_addr,
49265 + dev_if->dma_out_desc_addr, 1);
49266 + if (dev_if->in_desc_addr)
49267 + dwc_otg_ep_free_desc_chain(dev,
49268 + dev_if->in_desc_addr,
49269 + dev_if->dma_in_desc_addr, 1);
49270 + if (dev_if->setup_desc_addr[1])
49271 + dwc_otg_ep_free_desc_chain(dev,
49272 + dev_if->setup_desc_addr[1],
49273 + dev_if->dma_setup_desc_addr[1], 1);
49274 + if (dev_if->setup_desc_addr[0])
49275 + dwc_otg_ep_free_desc_chain(dev,
49276 + dev_if->setup_desc_addr[0],
49277 + dev_if->dma_setup_desc_addr[0], 1);
49278 +
49279 + DWC_DMA_FREE(dev, sizeof(*pcd->setup_pkt) * 5,
49280 + pcd->setup_pkt,
49281 + pcd->setup_pkt_dma_handle);
49282 + DWC_DMA_FREE(dev, sizeof(*pcd->status_buf),
49283 + pcd->status_buf,
49284 + pcd->status_buf_dma_handle);
49285 +
49286 + DWC_FREE(pcd);
49287 +
49288 + return NULL;
49289 + }
49290 + }
49291 + } else {
49292 + pcd->setup_pkt = DWC_ALLOC(sizeof(*pcd->setup_pkt) * 5);
49293 + if (pcd->setup_pkt == NULL) {
49294 + DWC_FREE(pcd);
49295 + return NULL;
49296 + }
49297 +
49298 + pcd->status_buf = DWC_ALLOC(sizeof(uint16_t));
49299 + if (pcd->status_buf == NULL) {
49300 + DWC_FREE(pcd->setup_pkt);
49301 + DWC_FREE(pcd);
49302 + return NULL;
49303 + }
49304 + }
49305 +
49306 + dwc_otg_pcd_reinit(pcd);
49307 +
49308 + /* Allocate the cfi object for the PCD */
49309 +#ifdef DWC_UTE_CFI
49310 + pcd->cfi = DWC_ALLOC(sizeof(cfiobject_t));
49311 + if (NULL == pcd->cfi)
49312 + goto fail;
49313 + if (init_cfi(pcd->cfi)) {
49314 + CFI_INFO("%s: Failed to init the CFI object\n", __func__);
49315 + goto fail;
49316 + }
49317 +#endif
49318 +
49319 + /* Initialize tasklets */
49320 + pcd->start_xfer_tasklet = DWC_TASK_ALLOC("xfer_tasklet",
49321 + start_xfer_tasklet_func, pcd);
49322 + pcd->test_mode_tasklet = DWC_TASK_ALLOC("test_mode_tasklet",
49323 + do_test_mode, pcd);
49324 +
49325 + /* Initialize SRP timer */
49326 + core_if->srp_timer = DWC_TIMER_ALLOC("SRP TIMER", srp_timeout, core_if);
49327 +
49328 + if (core_if->core_params->dev_out_nak) {
49329 + /**
49330 + * Initialize xfer timeout timer. Implemented for
49331 + * 2.93a feature "Device DDMA OUT NAK Enhancement"
49332 + */
49333 + for(i = 0; i < MAX_EPS_CHANNELS; i++) {
49334 + pcd->core_if->ep_xfer_timer[i] =
49335 + DWC_TIMER_ALLOC("ep timer", ep_xfer_timeout,
49336 + &pcd->core_if->ep_xfer_info[i]);
49337 + }
49338 + }
49339 +
49340 + return pcd;
49341 +#ifdef DWC_UTE_CFI
49342 +fail:
49343 +#endif
49344 + if (pcd->setup_pkt)
49345 + DWC_FREE(pcd->setup_pkt);
49346 + if (pcd->status_buf)
49347 + DWC_FREE(pcd->status_buf);
49348 +#ifdef DWC_UTE_CFI
49349 + if (pcd->cfi)
49350 + DWC_FREE(pcd->cfi);
49351 +#endif
49352 + if (pcd)
49353 + DWC_FREE(pcd);
49354 + return NULL;
49355 +
49356 +}
49357 +
49358 +/**
49359 + * Remove PCD specific data
49360 + */
49361 +void dwc_otg_pcd_remove(dwc_otg_pcd_t * pcd)
49362 +{
49363 + dwc_otg_dev_if_t *dev_if = GET_CORE_IF(pcd)->dev_if;
49364 + struct device *dev = dwc_otg_pcd_to_dev(pcd);
49365 + int i;
49366 +
49367 + if (pcd->core_if->core_params->dev_out_nak) {
49368 + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
49369 + DWC_TIMER_CANCEL(pcd->core_if->ep_xfer_timer[i]);
49370 + pcd->core_if->ep_xfer_info[i].state = 0;
49371 + }
49372 + }
49373 +
49374 + if (GET_CORE_IF(pcd)->dma_enable) {
49375 + DWC_DMA_FREE(dev, sizeof(*pcd->setup_pkt) * 5, pcd->setup_pkt,
49376 + pcd->setup_pkt_dma_handle);
49377 + DWC_DMA_FREE(dev, sizeof(uint16_t), pcd->status_buf,
49378 + pcd->status_buf_dma_handle);
49379 + if (GET_CORE_IF(pcd)->dma_desc_enable) {
49380 + dwc_otg_ep_free_desc_chain(dev,
49381 + dev_if->setup_desc_addr[0],
49382 + dev_if->dma_setup_desc_addr
49383 + [0], 1);
49384 + dwc_otg_ep_free_desc_chain(dev,
49385 + dev_if->setup_desc_addr[1],
49386 + dev_if->dma_setup_desc_addr
49387 + [1], 1);
49388 + dwc_otg_ep_free_desc_chain(dev,
49389 + dev_if->in_desc_addr,
49390 + dev_if->dma_in_desc_addr, 1);
49391 + dwc_otg_ep_free_desc_chain(dev,
49392 + dev_if->out_desc_addr,
49393 + dev_if->dma_out_desc_addr,
49394 + 1);
49395 + }
49396 + } else {
49397 + DWC_FREE(pcd->setup_pkt);
49398 + DWC_FREE(pcd->status_buf);
49399 + }
49400 + DWC_SPINLOCK_FREE(pcd->lock);
49401 + /* Set core_if's lock pointer to NULL */
49402 + pcd->core_if->lock = NULL;
49403 +
49404 + DWC_TASK_FREE(pcd->start_xfer_tasklet);
49405 + DWC_TASK_FREE(pcd->test_mode_tasklet);
49406 + if (pcd->core_if->core_params->dev_out_nak) {
49407 + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
49408 + if (pcd->core_if->ep_xfer_timer[i]) {
49409 + DWC_TIMER_FREE(pcd->core_if->ep_xfer_timer[i]);
49410 + }
49411 + }
49412 + }
49413 +
49414 +/* Release the CFI object's dynamic memory */
49415 +#ifdef DWC_UTE_CFI
49416 + if (pcd->cfi->ops.release) {
49417 + pcd->cfi->ops.release(pcd->cfi);
49418 + }
49419 +#endif
49420 +
49421 + DWC_FREE(pcd);
49422 +}
49423 +
49424 +/**
49425 + * Returns whether registered pcd is dual speed or not
49426 + */
49427 +uint32_t dwc_otg_pcd_is_dualspeed(dwc_otg_pcd_t * pcd)
49428 +{
49429 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
49430 +
49431 + if ((core_if->core_params->speed == DWC_SPEED_PARAM_FULL) ||
49432 + ((core_if->hwcfg2.b.hs_phy_type == 2) &&
49433 + (core_if->hwcfg2.b.fs_phy_type == 1) &&
49434 + (core_if->core_params->ulpi_fs_ls))) {
49435 + return 0;
49436 + }
49437 +
49438 + return 1;
49439 +}
49440 +
49441 +/**
49442 + * Returns whether registered pcd is OTG capable or not
49443 + */
49444 +uint32_t dwc_otg_pcd_is_otg(dwc_otg_pcd_t * pcd)
49445 +{
49446 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
49447 + gusbcfg_data_t usbcfg = {.d32 = 0 };
49448 +
49449 + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
49450 + if (!usbcfg.b.srpcap || !usbcfg.b.hnpcap) {
49451 + return 0;
49452 + }
49453 +
49454 + return 1;
49455 +}
49456 +
49457 +/**
49458 + * This function assigns periodic Tx FIFO to an periodic EP
49459 + * in shared Tx FIFO mode
49460 + */
49461 +static uint32_t assign_tx_fifo(dwc_otg_core_if_t * core_if)
49462 +{
49463 + uint32_t TxMsk = 1;
49464 + int i;
49465 +
49466 + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; ++i) {
49467 + if ((TxMsk & core_if->tx_msk) == 0) {
49468 + core_if->tx_msk |= TxMsk;
49469 + return i + 1;
49470 + }
49471 + TxMsk <<= 1;
49472 + }
49473 + return 0;
49474 +}
49475 +
49476 +/**
49477 + * This function assigns periodic Tx FIFO to an periodic EP
49478 + * in shared Tx FIFO mode
49479 + */
49480 +static uint32_t assign_perio_tx_fifo(dwc_otg_core_if_t * core_if)
49481 +{
49482 + uint32_t PerTxMsk = 1;
49483 + int i;
49484 + for (i = 0; i < core_if->hwcfg4.b.num_dev_perio_in_ep; ++i) {
49485 + if ((PerTxMsk & core_if->p_tx_msk) == 0) {
49486 + core_if->p_tx_msk |= PerTxMsk;
49487 + return i + 1;
49488 + }
49489 + PerTxMsk <<= 1;
49490 + }
49491 + return 0;
49492 +}
49493 +
49494 +/**
49495 + * This function releases periodic Tx FIFO
49496 + * in shared Tx FIFO mode
49497 + */
49498 +static void release_perio_tx_fifo(dwc_otg_core_if_t * core_if,
49499 + uint32_t fifo_num)
49500 +{
49501 + core_if->p_tx_msk =
49502 + (core_if->p_tx_msk & (1 << (fifo_num - 1))) ^ core_if->p_tx_msk;
49503 +}
49504 +
49505 +/**
49506 + * This function releases periodic Tx FIFO
49507 + * in shared Tx FIFO mode
49508 + */
49509 +static void release_tx_fifo(dwc_otg_core_if_t * core_if, uint32_t fifo_num)
49510 +{
49511 + core_if->tx_msk =
49512 + (core_if->tx_msk & (1 << (fifo_num - 1))) ^ core_if->tx_msk;
49513 +}
49514 +
49515 +/**
49516 + * This function is being called from gadget
49517 + * to enable PCD endpoint.
49518 + */
49519 +int dwc_otg_pcd_ep_enable(dwc_otg_pcd_t * pcd,
49520 + const uint8_t * ep_desc, void *usb_ep)
49521 +{
49522 + int num, dir;
49523 + dwc_otg_pcd_ep_t *ep = NULL;
49524 + const usb_endpoint_descriptor_t *desc;
49525 + dwc_irqflags_t flags;
49526 + fifosize_data_t dptxfsiz = {.d32 = 0 };
49527 + gdfifocfg_data_t gdfifocfg = {.d32 = 0 };
49528 + gdfifocfg_data_t gdfifocfgbase = {.d32 = 0 };
49529 + int retval = 0;
49530 + int i, epcount;
49531 + struct device *dev = dwc_otg_pcd_to_dev(pcd);
49532 +
49533 + desc = (const usb_endpoint_descriptor_t *)ep_desc;
49534 +
49535 + if (!desc) {
49536 + pcd->ep0.priv = usb_ep;
49537 + ep = &pcd->ep0;
49538 + retval = -DWC_E_INVALID;
49539 + goto out;
49540 + }
49541 +
49542 + num = UE_GET_ADDR(desc->bEndpointAddress);
49543 + dir = UE_GET_DIR(desc->bEndpointAddress);
49544 +
49545 + if (!desc->wMaxPacketSize) {
49546 + DWC_WARN("bad maxpacketsize\n");
49547 + retval = -DWC_E_INVALID;
49548 + goto out;
49549 + }
49550 +
49551 + if (dir == UE_DIR_IN) {
49552 + epcount = pcd->core_if->dev_if->num_in_eps;
49553 + for (i = 0; i < epcount; i++) {
49554 + if (num == pcd->in_ep[i].dwc_ep.num) {
49555 + ep = &pcd->in_ep[i];
49556 + break;
49557 + }
49558 + }
49559 + } else {
49560 + epcount = pcd->core_if->dev_if->num_out_eps;
49561 + for (i = 0; i < epcount; i++) {
49562 + if (num == pcd->out_ep[i].dwc_ep.num) {
49563 + ep = &pcd->out_ep[i];
49564 + break;
49565 + }
49566 + }
49567 + }
49568 +
49569 + if (!ep) {
49570 + DWC_WARN("bad address\n");
49571 + retval = -DWC_E_INVALID;
49572 + goto out;
49573 + }
49574 +
49575 + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
49576 +
49577 + ep->desc = desc;
49578 + ep->priv = usb_ep;
49579 +
49580 + /*
49581 + * Activate the EP
49582 + */
49583 + ep->stopped = 0;
49584 +
49585 + ep->dwc_ep.is_in = (dir == UE_DIR_IN);
49586 + ep->dwc_ep.maxpacket = UGETW(desc->wMaxPacketSize);
49587 +
49588 + ep->dwc_ep.type = desc->bmAttributes & UE_XFERTYPE;
49589 +
49590 + if (ep->dwc_ep.is_in) {
49591 + if (!GET_CORE_IF(pcd)->en_multiple_tx_fifo) {
49592 + ep->dwc_ep.tx_fifo_num = 0;
49593 +
49594 + if (ep->dwc_ep.type == UE_ISOCHRONOUS) {
49595 + /*
49596 + * if ISOC EP then assign a Periodic Tx FIFO.
49597 + */
49598 + ep->dwc_ep.tx_fifo_num =
49599 + assign_perio_tx_fifo(GET_CORE_IF(pcd));
49600 + }
49601 + } else {
49602 + /*
49603 + * if Dedicated FIFOs mode is on then assign a Tx FIFO.
49604 + */
49605 + ep->dwc_ep.tx_fifo_num =
49606 + assign_tx_fifo(GET_CORE_IF(pcd));
49607 + }
49608 +
49609 + /* Calculating EP info controller base address */
49610 + if (ep->dwc_ep.tx_fifo_num
49611 + && GET_CORE_IF(pcd)->en_multiple_tx_fifo) {
49612 + gdfifocfg.d32 =
49613 + DWC_READ_REG32(&GET_CORE_IF(pcd)->
49614 + core_global_regs->gdfifocfg);
49615 + gdfifocfgbase.d32 = gdfifocfg.d32 >> 16;
49616 + dptxfsiz.d32 =
49617 + (DWC_READ_REG32
49618 + (&GET_CORE_IF(pcd)->core_global_regs->
49619 + dtxfsiz[ep->dwc_ep.tx_fifo_num - 1]) >> 16);
49620 + gdfifocfg.b.epinfobase =
49621 + gdfifocfgbase.d32 + dptxfsiz.d32;
49622 + if (GET_CORE_IF(pcd)->snpsid <= OTG_CORE_REV_2_94a) {
49623 + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->
49624 + core_global_regs->gdfifocfg,
49625 + gdfifocfg.d32);
49626 + }
49627 + }
49628 + }
49629 + /* Set initial data PID. */
49630 + if (ep->dwc_ep.type == UE_BULK) {
49631 + ep->dwc_ep.data_pid_start = 0;
49632 + }
49633 +
49634 + /* Alloc DMA Descriptors */
49635 + if (GET_CORE_IF(pcd)->dma_desc_enable) {
49636 +#ifndef DWC_UTE_PER_IO
49637 + if (ep->dwc_ep.type != UE_ISOCHRONOUS) {
49638 +#endif
49639 + ep->dwc_ep.desc_addr =
49640 + dwc_otg_ep_alloc_desc_chain(dev,
49641 + &ep->dwc_ep.dma_desc_addr,
49642 + MAX_DMA_DESC_CNT);
49643 + if (!ep->dwc_ep.desc_addr) {
49644 + DWC_WARN("%s, can't allocate DMA descriptor\n",
49645 + __func__);
49646 + retval = -DWC_E_SHUTDOWN;
49647 + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
49648 + goto out;
49649 + }
49650 +#ifndef DWC_UTE_PER_IO
49651 + }
49652 +#endif
49653 + }
49654 +
49655 + DWC_DEBUGPL(DBG_PCD, "Activate %s: type=%d, mps=%d desc=%p\n",
49656 + (ep->dwc_ep.is_in ? "IN" : "OUT"),
49657 + ep->dwc_ep.type, ep->dwc_ep.maxpacket, ep->desc);
49658 +#ifdef DWC_UTE_PER_IO
49659 + ep->dwc_ep.xiso_bInterval = 1 << (ep->desc->bInterval - 1);
49660 +#endif
49661 + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
49662 + ep->dwc_ep.bInterval = 1 << (ep->desc->bInterval - 1);
49663 + ep->dwc_ep.frame_num = 0xFFFFFFFF;
49664 + }
49665 +
49666 + dwc_otg_ep_activate(GET_CORE_IF(pcd), &ep->dwc_ep);
49667 +
49668 +#ifdef DWC_UTE_CFI
49669 + if (pcd->cfi->ops.ep_enable) {
49670 + pcd->cfi->ops.ep_enable(pcd->cfi, pcd, ep);
49671 + }
49672 +#endif
49673 +
49674 + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
49675 +
49676 +out:
49677 + return retval;
49678 +}
49679 +
49680 +/**
49681 + * This function is being called from gadget
49682 + * to disable PCD endpoint.
49683 + */
49684 +int dwc_otg_pcd_ep_disable(dwc_otg_pcd_t * pcd, void *ep_handle)
49685 +{
49686 + dwc_otg_pcd_ep_t *ep;
49687 + dwc_irqflags_t flags;
49688 + dwc_otg_dev_dma_desc_t *desc_addr;
49689 + dwc_dma_t dma_desc_addr;
49690 + gdfifocfg_data_t gdfifocfgbase = {.d32 = 0 };
49691 + gdfifocfg_data_t gdfifocfg = {.d32 = 0 };
49692 + fifosize_data_t dptxfsiz = {.d32 = 0 };
49693 + struct device *dev = dwc_otg_pcd_to_dev(pcd);
49694 +
49695 + ep = get_ep_from_handle(pcd, ep_handle);
49696 +
49697 + if (!ep || !ep->desc) {
49698 + DWC_DEBUGPL(DBG_PCD, "bad ep address\n");
49699 + return -DWC_E_INVALID;
49700 + }
49701 +
49702 + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
49703 +
49704 + dwc_otg_request_nuke(ep);
49705 +
49706 + dwc_otg_ep_deactivate(GET_CORE_IF(pcd), &ep->dwc_ep);
49707 + if (pcd->core_if->core_params->dev_out_nak) {
49708 + DWC_TIMER_CANCEL(pcd->core_if->ep_xfer_timer[ep->dwc_ep.num]);
49709 + pcd->core_if->ep_xfer_info[ep->dwc_ep.num].state = 0;
49710 + }
49711 + ep->desc = NULL;
49712 + ep->stopped = 1;
49713 +
49714 + gdfifocfg.d32 =
49715 + DWC_READ_REG32(&GET_CORE_IF(pcd)->core_global_regs->gdfifocfg);
49716 + gdfifocfgbase.d32 = gdfifocfg.d32 >> 16;
49717 +
49718 + if (ep->dwc_ep.is_in) {
49719 + if (GET_CORE_IF(pcd)->en_multiple_tx_fifo) {
49720 + /* Flush the Tx FIFO */
49721 + dwc_otg_flush_tx_fifo(GET_CORE_IF(pcd),
49722 + ep->dwc_ep.tx_fifo_num);
49723 + }
49724 + release_perio_tx_fifo(GET_CORE_IF(pcd), ep->dwc_ep.tx_fifo_num);
49725 + release_tx_fifo(GET_CORE_IF(pcd), ep->dwc_ep.tx_fifo_num);
49726 + if (GET_CORE_IF(pcd)->en_multiple_tx_fifo) {
49727 + /* Decreasing EPinfo Base Addr */
49728 + dptxfsiz.d32 =
49729 + (DWC_READ_REG32
49730 + (&GET_CORE_IF(pcd)->
49731 + core_global_regs->dtxfsiz[ep->dwc_ep.tx_fifo_num-1]) >> 16);
49732 + gdfifocfg.b.epinfobase = gdfifocfgbase.d32 - dptxfsiz.d32;
49733 + if (GET_CORE_IF(pcd)->snpsid <= OTG_CORE_REV_2_94a) {
49734 + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gdfifocfg,
49735 + gdfifocfg.d32);
49736 + }
49737 + }
49738 + }
49739 +
49740 + /* Free DMA Descriptors */
49741 + if (GET_CORE_IF(pcd)->dma_desc_enable) {
49742 + if (ep->dwc_ep.type != UE_ISOCHRONOUS) {
49743 + desc_addr = ep->dwc_ep.desc_addr;
49744 + dma_desc_addr = ep->dwc_ep.dma_desc_addr;
49745 +
49746 + /* Cannot call dma_free_coherent() with IRQs disabled */
49747 + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
49748 + dwc_otg_ep_free_desc_chain(dev, desc_addr, dma_desc_addr,
49749 + MAX_DMA_DESC_CNT);
49750 +
49751 + goto out_unlocked;
49752 + }
49753 + }
49754 + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
49755 +
49756 +out_unlocked:
49757 + DWC_DEBUGPL(DBG_PCD, "%d %s disabled\n", ep->dwc_ep.num,
49758 + ep->dwc_ep.is_in ? "IN" : "OUT");
49759 + return 0;
49760 +
49761 +}
49762 +
49763 +/******************************************************************************/
49764 +#ifdef DWC_UTE_PER_IO
49765 +
49766 +/**
49767 + * Free the request and its extended parts
49768 + *
49769 + */
49770 +void dwc_pcd_xiso_ereq_free(dwc_otg_pcd_ep_t * ep, dwc_otg_pcd_request_t * req)
49771 +{
49772 + DWC_FREE(req->ext_req.per_io_frame_descs);
49773 + DWC_FREE(req);
49774 +}
49775 +
49776 +/**
49777 + * Start the next request in the endpoint's queue.
49778 + *
49779 + */
49780 +int dwc_otg_pcd_xiso_start_next_request(dwc_otg_pcd_t * pcd,
49781 + dwc_otg_pcd_ep_t * ep)
49782 +{
49783 + int i;
49784 + dwc_otg_pcd_request_t *req = NULL;
49785 + dwc_ep_t *dwcep = NULL;
49786 + struct dwc_iso_xreq_port *ereq = NULL;
49787 + struct dwc_iso_pkt_desc_port *ddesc_iso;
49788 + uint16_t nat;
49789 + depctl_data_t diepctl;
49790 +
49791 + dwcep = &ep->dwc_ep;
49792 +
49793 + if (dwcep->xiso_active_xfers > 0) {
49794 +#if 0 //Disable this to decrease s/w overhead that is crucial for Isoc transfers
49795 + DWC_WARN("There are currently active transfers for EP%d \
49796 + (active=%d; queued=%d)", dwcep->num, dwcep->xiso_active_xfers,
49797 + dwcep->xiso_queued_xfers);
49798 +#endif
49799 + return 0;
49800 + }
49801 +
49802 + nat = UGETW(ep->desc->wMaxPacketSize);
49803 + nat = (nat >> 11) & 0x03;
49804 +
49805 + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
49806 + req = DWC_CIRCLEQ_FIRST(&ep->queue);
49807 + ereq = &req->ext_req;
49808 + ep->stopped = 0;
49809 +
49810 + /* Get the frame number */
49811 + dwcep->xiso_frame_num =
49812 + dwc_otg_get_frame_number(GET_CORE_IF(pcd));
49813 + DWC_DEBUG("FRM_NUM=%d", dwcep->xiso_frame_num);
49814 +
49815 + ddesc_iso = ereq->per_io_frame_descs;
49816 +
49817 + if (dwcep->is_in) {
49818 + /* Setup DMA Descriptor chain for IN Isoc request */
49819 + for (i = 0; i < ereq->pio_pkt_count; i++) {
49820 + //if ((i % (nat + 1)) == 0)
49821 + if ( i > 0 )
49822 + dwcep->xiso_frame_num =
49823 + (dwcep->xiso_bInterval +
49824 + dwcep->xiso_frame_num) & 0x3FFF;
49825 + dwcep->desc_addr[i].buf =
49826 + req->dma + ddesc_iso[i].offset;
49827 + dwcep->desc_addr[i].status.b_iso_in.txbytes =
49828 + ddesc_iso[i].length;
49829 + dwcep->desc_addr[i].status.b_iso_in.framenum =
49830 + dwcep->xiso_frame_num;
49831 + dwcep->desc_addr[i].status.b_iso_in.bs =
49832 + BS_HOST_READY;
49833 + dwcep->desc_addr[i].status.b_iso_in.txsts = 0;
49834 + dwcep->desc_addr[i].status.b_iso_in.sp =
49835 + (ddesc_iso[i].length %
49836 + dwcep->maxpacket) ? 1 : 0;
49837 + dwcep->desc_addr[i].status.b_iso_in.ioc = 0;
49838 + dwcep->desc_addr[i].status.b_iso_in.pid = nat + 1;
49839 + dwcep->desc_addr[i].status.b_iso_in.l = 0;
49840 +
49841 + /* Process the last descriptor */
49842 + if (i == ereq->pio_pkt_count - 1) {
49843 + dwcep->desc_addr[i].status.b_iso_in.ioc = 1;
49844 + dwcep->desc_addr[i].status.b_iso_in.l = 1;
49845 + }
49846 + }
49847 +
49848 + /* Setup and start the transfer for this endpoint */
49849 + dwcep->xiso_active_xfers++;
49850 + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->dev_if->
49851 + in_ep_regs[dwcep->num]->diepdma,
49852 + dwcep->dma_desc_addr);
49853 + diepctl.d32 = 0;
49854 + diepctl.b.epena = 1;
49855 + diepctl.b.cnak = 1;
49856 + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->dev_if->
49857 + in_ep_regs[dwcep->num]->diepctl, 0,
49858 + diepctl.d32);
49859 + } else {
49860 + /* Setup DMA Descriptor chain for OUT Isoc request */
49861 + for (i = 0; i < ereq->pio_pkt_count; i++) {
49862 + //if ((i % (nat + 1)) == 0)
49863 + dwcep->xiso_frame_num = (dwcep->xiso_bInterval +
49864 + dwcep->xiso_frame_num) & 0x3FFF;
49865 + dwcep->desc_addr[i].buf =
49866 + req->dma + ddesc_iso[i].offset;
49867 + dwcep->desc_addr[i].status.b_iso_out.rxbytes =
49868 + ddesc_iso[i].length;
49869 + dwcep->desc_addr[i].status.b_iso_out.framenum =
49870 + dwcep->xiso_frame_num;
49871 + dwcep->desc_addr[i].status.b_iso_out.bs =
49872 + BS_HOST_READY;
49873 + dwcep->desc_addr[i].status.b_iso_out.rxsts = 0;
49874 + dwcep->desc_addr[i].status.b_iso_out.sp =
49875 + (ddesc_iso[i].length %
49876 + dwcep->maxpacket) ? 1 : 0;
49877 + dwcep->desc_addr[i].status.b_iso_out.ioc = 0;
49878 + dwcep->desc_addr[i].status.b_iso_out.pid = nat + 1;
49879 + dwcep->desc_addr[i].status.b_iso_out.l = 0;
49880 +
49881 + /* Process the last descriptor */
49882 + if (i == ereq->pio_pkt_count - 1) {
49883 + dwcep->desc_addr[i].status.b_iso_out.ioc = 1;
49884 + dwcep->desc_addr[i].status.b_iso_out.l = 1;
49885 + }
49886 + }
49887 +
49888 + /* Setup and start the transfer for this endpoint */
49889 + dwcep->xiso_active_xfers++;
49890 + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->
49891 + dev_if->out_ep_regs[dwcep->num]->
49892 + doepdma, dwcep->dma_desc_addr);
49893 + diepctl.d32 = 0;
49894 + diepctl.b.epena = 1;
49895 + diepctl.b.cnak = 1;
49896 + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->
49897 + dev_if->out_ep_regs[dwcep->num]->
49898 + doepctl, 0, diepctl.d32);
49899 + }
49900 +
49901 + } else {
49902 + ep->stopped = 1;
49903 + }
49904 +
49905 + return 0;
49906 +}
49907 +
49908 +/**
49909 + * - Remove the request from the queue
49910 + */
49911 +void complete_xiso_ep(dwc_otg_pcd_ep_t * ep)
49912 +{
49913 + dwc_otg_pcd_request_t *req = NULL;
49914 + struct dwc_iso_xreq_port *ereq = NULL;
49915 + struct dwc_iso_pkt_desc_port *ddesc_iso = NULL;
49916 + dwc_ep_t *dwcep = NULL;
49917 + int i;
49918 +
49919 + //DWC_DEBUG();
49920 + dwcep = &ep->dwc_ep;
49921 +
49922 + /* Get the first pending request from the queue */
49923 + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
49924 + req = DWC_CIRCLEQ_FIRST(&ep->queue);
49925 + if (!req) {
49926 + DWC_PRINTF("complete_ep 0x%p, req = NULL!\n", ep);
49927 + return;
49928 + }
49929 + dwcep->xiso_active_xfers--;
49930 + dwcep->xiso_queued_xfers--;
49931 + /* Remove this request from the queue */
49932 + DWC_CIRCLEQ_REMOVE_INIT(&ep->queue, req, queue_entry);
49933 + } else {
49934 + DWC_PRINTF("complete_ep 0x%p, ep->queue empty!\n", ep);
49935 + return;
49936 + }
49937 +
49938 + ep->stopped = 1;
49939 + ereq = &req->ext_req;
49940 + ddesc_iso = ereq->per_io_frame_descs;
49941 +
49942 + if (dwcep->xiso_active_xfers < 0) {
49943 + DWC_WARN("EP#%d (xiso_active_xfers=%d)", dwcep->num,
49944 + dwcep->xiso_active_xfers);
49945 + }
49946 +
49947 + /* Fill the Isoc descs of portable extended req from dma descriptors */
49948 + for (i = 0; i < ereq->pio_pkt_count; i++) {
49949 + if (dwcep->is_in) { /* IN endpoints */
49950 + ddesc_iso[i].actual_length = ddesc_iso[i].length -
49951 + dwcep->desc_addr[i].status.b_iso_in.txbytes;
49952 + ddesc_iso[i].status =
49953 + dwcep->desc_addr[i].status.b_iso_in.txsts;
49954 + } else { /* OUT endpoints */
49955 + ddesc_iso[i].actual_length = ddesc_iso[i].length -
49956 + dwcep->desc_addr[i].status.b_iso_out.rxbytes;
49957 + ddesc_iso[i].status =
49958 + dwcep->desc_addr[i].status.b_iso_out.rxsts;
49959 + }
49960 + }
49961 +
49962 + DWC_SPINUNLOCK(ep->pcd->lock);
49963 +
49964 + /* Call the completion function in the non-portable logic */
49965 + ep->pcd->fops->xisoc_complete(ep->pcd, ep->priv, req->priv, 0,
49966 + &req->ext_req);
49967 +
49968 + DWC_SPINLOCK(ep->pcd->lock);
49969 +
49970 + /* Free the request - specific freeing needed for extended request object */
49971 + dwc_pcd_xiso_ereq_free(ep, req);
49972 +
49973 + /* Start the next request */
49974 + dwc_otg_pcd_xiso_start_next_request(ep->pcd, ep);
49975 +
49976 + return;
49977 +}
49978 +
49979 +/**
49980 + * Create and initialize the Isoc pkt descriptors of the extended request.
49981 + *
49982 + */
49983 +static int dwc_otg_pcd_xiso_create_pkt_descs(dwc_otg_pcd_request_t * req,
49984 + void *ereq_nonport,
49985 + int atomic_alloc)
49986 +{
49987 + struct dwc_iso_xreq_port *ereq = NULL;
49988 + struct dwc_iso_xreq_port *req_mapped = NULL;
49989 + struct dwc_iso_pkt_desc_port *ipds = NULL; /* To be created in this function */
49990 + uint32_t pkt_count;
49991 + int i;
49992 +
49993 + ereq = &req->ext_req;
49994 + req_mapped = (struct dwc_iso_xreq_port *)ereq_nonport;
49995 + pkt_count = req_mapped->pio_pkt_count;
49996 +
49997 + /* Create the isoc descs */
49998 + if (atomic_alloc) {
49999 + ipds = DWC_ALLOC_ATOMIC(sizeof(*ipds) * pkt_count);
50000 + } else {
50001 + ipds = DWC_ALLOC(sizeof(*ipds) * pkt_count);
50002 + }
50003 +
50004 + if (!ipds) {
50005 + DWC_ERROR("Failed to allocate isoc descriptors");
50006 + return -DWC_E_NO_MEMORY;
50007 + }
50008 +
50009 + /* Initialize the extended request fields */
50010 + ereq->per_io_frame_descs = ipds;
50011 + ereq->error_count = 0;
50012 + ereq->pio_alloc_pkt_count = pkt_count;
50013 + ereq->pio_pkt_count = pkt_count;
50014 + ereq->tr_sub_flags = req_mapped->tr_sub_flags;
50015 +
50016 + /* Init the Isoc descriptors */
50017 + for (i = 0; i < pkt_count; i++) {
50018 + ipds[i].length = req_mapped->per_io_frame_descs[i].length;
50019 + ipds[i].offset = req_mapped->per_io_frame_descs[i].offset;
50020 + ipds[i].status = req_mapped->per_io_frame_descs[i].status; /* 0 */
50021 + ipds[i].actual_length =
50022 + req_mapped->per_io_frame_descs[i].actual_length;
50023 + }
50024 +
50025 + return 0;
50026 +}
50027 +
50028 +static void prn_ext_request(struct dwc_iso_xreq_port *ereq)
50029 +{
50030 + struct dwc_iso_pkt_desc_port *xfd = NULL;
50031 + int i;
50032 +
50033 + DWC_DEBUG("per_io_frame_descs=%p", ereq->per_io_frame_descs);
50034 + DWC_DEBUG("tr_sub_flags=%d", ereq->tr_sub_flags);
50035 + DWC_DEBUG("error_count=%d", ereq->error_count);
50036 + DWC_DEBUG("pio_alloc_pkt_count=%d", ereq->pio_alloc_pkt_count);
50037 + DWC_DEBUG("pio_pkt_count=%d", ereq->pio_pkt_count);
50038 + DWC_DEBUG("res=%d", ereq->res);
50039 +
50040 + for (i = 0; i < ereq->pio_pkt_count; i++) {
50041 + xfd = &ereq->per_io_frame_descs[0];
50042 + DWC_DEBUG("FD #%d", i);
50043 +
50044 + DWC_DEBUG("xfd->actual_length=%d", xfd->actual_length);
50045 + DWC_DEBUG("xfd->length=%d", xfd->length);
50046 + DWC_DEBUG("xfd->offset=%d", xfd->offset);
50047 + DWC_DEBUG("xfd->status=%d", xfd->status);
50048 + }
50049 +}
50050 +
50051 +/**
50052 + *
50053 + */
50054 +int dwc_otg_pcd_xiso_ep_queue(dwc_otg_pcd_t * pcd, void *ep_handle,
50055 + uint8_t * buf, dwc_dma_t dma_buf, uint32_t buflen,
50056 + int zero, void *req_handle, int atomic_alloc,
50057 + void *ereq_nonport)
50058 +{
50059 + dwc_otg_pcd_request_t *req = NULL;
50060 + dwc_otg_pcd_ep_t *ep;
50061 + dwc_irqflags_t flags;
50062 + int res;
50063 +
50064 + ep = get_ep_from_handle(pcd, ep_handle);
50065 + if (!ep) {
50066 + DWC_WARN("bad ep\n");
50067 + return -DWC_E_INVALID;
50068 + }
50069 +
50070 + /* We support this extension only for DDMA mode */
50071 + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC)
50072 + if (!GET_CORE_IF(pcd)->dma_desc_enable)
50073 + return -DWC_E_INVALID;
50074 +
50075 + /* Create a dwc_otg_pcd_request_t object */
50076 + if (atomic_alloc) {
50077 + req = DWC_ALLOC_ATOMIC(sizeof(*req));
50078 + } else {
50079 + req = DWC_ALLOC(sizeof(*req));
50080 + }
50081 +
50082 + if (!req) {
50083 + return -DWC_E_NO_MEMORY;
50084 + }
50085 +
50086 + /* Create the Isoc descs for this request which shall be the exact match
50087 + * of the structure sent to us from the non-portable logic */
50088 + res =
50089 + dwc_otg_pcd_xiso_create_pkt_descs(req, ereq_nonport, atomic_alloc);
50090 + if (res) {
50091 + DWC_WARN("Failed to init the Isoc descriptors");
50092 + DWC_FREE(req);
50093 + return res;
50094 + }
50095 +
50096 + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
50097 +
50098 + DWC_CIRCLEQ_INIT_ENTRY(req, queue_entry);
50099 + req->buf = buf;
50100 + req->dma = dma_buf;
50101 + req->length = buflen;
50102 + req->sent_zlp = zero;
50103 + req->priv = req_handle;
50104 +
50105 + //DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
50106 + ep->dwc_ep.dma_addr = dma_buf;
50107 + ep->dwc_ep.start_xfer_buff = buf;
50108 + ep->dwc_ep.xfer_buff = buf;
50109 + ep->dwc_ep.xfer_len = 0;
50110 + ep->dwc_ep.xfer_count = 0;
50111 + ep->dwc_ep.sent_zlp = 0;
50112 + ep->dwc_ep.total_len = buflen;
50113 +
50114 + /* Add this request to the tail */
50115 + DWC_CIRCLEQ_INSERT_TAIL(&ep->queue, req, queue_entry);
50116 + ep->dwc_ep.xiso_queued_xfers++;
50117 +
50118 +//DWC_DEBUG("CP_0");
50119 +//DWC_DEBUG("req->ext_req.tr_sub_flags=%d", req->ext_req.tr_sub_flags);
50120 +//prn_ext_request((struct dwc_iso_xreq_port *) ereq_nonport);
50121 +//prn_ext_request(&req->ext_req);
50122 +
50123 + //DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
50124 +
50125 + /* If the req->status == ASAP then check if there is any active transfer
50126 + * for this endpoint. If no active transfers, then get the first entry
50127 + * from the queue and start that transfer
50128 + */
50129 + if (req->ext_req.tr_sub_flags == DWC_EREQ_TF_ASAP) {
50130 + res = dwc_otg_pcd_xiso_start_next_request(pcd, ep);
50131 + if (res) {
50132 + DWC_WARN("Failed to start the next Isoc transfer");
50133 + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
50134 + DWC_FREE(req);
50135 + return res;
50136 + }
50137 + }
50138 +
50139 + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
50140 + return 0;
50141 +}
50142 +
50143 +#endif
50144 +/* END ifdef DWC_UTE_PER_IO ***************************************************/
50145 +int dwc_otg_pcd_ep_queue(dwc_otg_pcd_t * pcd, void *ep_handle,
50146 + uint8_t * buf, dwc_dma_t dma_buf, uint32_t buflen,
50147 + int zero, void *req_handle, int atomic_alloc)
50148 +{
50149 + struct device *dev = dwc_otg_pcd_to_dev(pcd);
50150 + dwc_irqflags_t flags;
50151 + dwc_otg_pcd_request_t *req;
50152 + dwc_otg_pcd_ep_t *ep;
50153 + uint32_t max_transfer;
50154 +
50155 + ep = get_ep_from_handle(pcd, ep_handle);
50156 + if (!ep || (!ep->desc && ep->dwc_ep.num != 0)) {
50157 + DWC_WARN("bad ep\n");
50158 + return -DWC_E_INVALID;
50159 + }
50160 +
50161 + if (atomic_alloc) {
50162 + req = DWC_ALLOC_ATOMIC(sizeof(*req));
50163 + } else {
50164 + req = DWC_ALLOC(sizeof(*req));
50165 + }
50166 +
50167 + if (!req) {
50168 + return -DWC_E_NO_MEMORY;
50169 + }
50170 + DWC_CIRCLEQ_INIT_ENTRY(req, queue_entry);
50171 + if (!GET_CORE_IF(pcd)->core_params->opt) {
50172 + if (ep->dwc_ep.num != 0) {
50173 + DWC_ERROR("queue req %p, len %d buf %p\n",
50174 + req_handle, buflen, buf);
50175 + }
50176 + }
50177 +
50178 + req->buf = buf;
50179 + req->dma = dma_buf;
50180 + req->length = buflen;
50181 + req->sent_zlp = zero;
50182 + req->priv = req_handle;
50183 + req->dw_align_buf = NULL;
50184 + if ((dma_buf & 0x3) && GET_CORE_IF(pcd)->dma_enable
50185 + && !GET_CORE_IF(pcd)->dma_desc_enable)
50186 + req->dw_align_buf = DWC_DMA_ALLOC(dev, buflen,
50187 + &req->dw_align_buf_dma);
50188 + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
50189 +
50190 + /*
50191 + * After adding request to the queue for IN ISOC wait for In Token Received
50192 + * when TX FIFO is empty interrupt and for OUT ISOC wait for OUT Token
50193 + * Received when EP is disabled interrupt to obtain starting microframe
50194 + * (odd/even) start transfer
50195 + */
50196 + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
50197 + if (req != 0) {
50198 + depctl_data_t depctl = {.d32 =
50199 + DWC_READ_REG32(&pcd->core_if->dev_if->
50200 + in_ep_regs[ep->dwc_ep.num]->
50201 + diepctl) };
50202 + ++pcd->request_pending;
50203 +
50204 + DWC_CIRCLEQ_INSERT_TAIL(&ep->queue, req, queue_entry);
50205 + if (ep->dwc_ep.is_in) {
50206 + depctl.b.cnak = 1;
50207 + DWC_WRITE_REG32(&pcd->core_if->dev_if->
50208 + in_ep_regs[ep->dwc_ep.num]->
50209 + diepctl, depctl.d32);
50210 + }
50211 +
50212 + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
50213 + }
50214 + return 0;
50215 + }
50216 +
50217 + /*
50218 + * For EP0 IN without premature status, zlp is required?
50219 + */
50220 + if (ep->dwc_ep.num == 0 && ep->dwc_ep.is_in) {
50221 + DWC_DEBUGPL(DBG_PCDV, "%d-OUT ZLP\n", ep->dwc_ep.num);
50222 + //_req->zero = 1;
50223 + }
50224 +
50225 + /* Start the transfer */
50226 + if (DWC_CIRCLEQ_EMPTY(&ep->queue) && !ep->stopped) {
50227 + /* EP0 Transfer? */
50228 + if (ep->dwc_ep.num == 0) {
50229 + switch (pcd->ep0state) {
50230 + case EP0_IN_DATA_PHASE:
50231 + DWC_DEBUGPL(DBG_PCD,
50232 + "%s ep0: EP0_IN_DATA_PHASE\n",
50233 + __func__);
50234 + break;
50235 +
50236 + case EP0_OUT_DATA_PHASE:
50237 + DWC_DEBUGPL(DBG_PCD,
50238 + "%s ep0: EP0_OUT_DATA_PHASE\n",
50239 + __func__);
50240 + if (pcd->request_config) {
50241 + /* Complete STATUS PHASE */
50242 + ep->dwc_ep.is_in = 1;
50243 + pcd->ep0state = EP0_IN_STATUS_PHASE;
50244 + }
50245 + break;
50246 +
50247 + case EP0_IN_STATUS_PHASE:
50248 + DWC_DEBUGPL(DBG_PCD,
50249 + "%s ep0: EP0_IN_STATUS_PHASE\n",
50250 + __func__);
50251 + break;
50252 +
50253 + default:
50254 + DWC_DEBUGPL(DBG_ANY, "ep0: odd state %d\n",
50255 + pcd->ep0state);
50256 + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
50257 + return -DWC_E_SHUTDOWN;
50258 + }
50259 +
50260 + ep->dwc_ep.dma_addr = dma_buf;
50261 + ep->dwc_ep.start_xfer_buff = buf;
50262 + ep->dwc_ep.xfer_buff = buf;
50263 + ep->dwc_ep.xfer_len = buflen;
50264 + ep->dwc_ep.xfer_count = 0;
50265 + ep->dwc_ep.sent_zlp = 0;
50266 + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
50267 +
50268 + if (zero) {
50269 + if ((ep->dwc_ep.xfer_len %
50270 + ep->dwc_ep.maxpacket == 0)
50271 + && (ep->dwc_ep.xfer_len != 0)) {
50272 + ep->dwc_ep.sent_zlp = 1;
50273 + }
50274 +
50275 + }
50276 +
50277 + dwc_otg_ep0_start_transfer(GET_CORE_IF(pcd),
50278 + &ep->dwc_ep);
50279 + } // non-ep0 endpoints
50280 + else {
50281 +#ifdef DWC_UTE_CFI
50282 + if (ep->dwc_ep.buff_mode != BM_STANDARD) {
50283 + /* store the request length */
50284 + ep->dwc_ep.cfi_req_len = buflen;
50285 + pcd->cfi->ops.build_descriptors(pcd->cfi, pcd,
50286 + ep, req);
50287 + } else {
50288 +#endif
50289 + max_transfer =
50290 + GET_CORE_IF(ep->pcd)->core_params->
50291 + max_transfer_size;
50292 +
50293 + /* Setup and start the Transfer */
50294 + if (req->dw_align_buf){
50295 + if (ep->dwc_ep.is_in)
50296 + dwc_memcpy(req->dw_align_buf,
50297 + buf, buflen);
50298 + ep->dwc_ep.dma_addr =
50299 + req->dw_align_buf_dma;
50300 + ep->dwc_ep.start_xfer_buff =
50301 + req->dw_align_buf;
50302 + ep->dwc_ep.xfer_buff =
50303 + req->dw_align_buf;
50304 + } else {
50305 + ep->dwc_ep.dma_addr = dma_buf;
50306 + ep->dwc_ep.start_xfer_buff = buf;
50307 + ep->dwc_ep.xfer_buff = buf;
50308 + }
50309 + ep->dwc_ep.xfer_len = 0;
50310 + ep->dwc_ep.xfer_count = 0;
50311 + ep->dwc_ep.sent_zlp = 0;
50312 + ep->dwc_ep.total_len = buflen;
50313 +
50314 + ep->dwc_ep.maxxfer = max_transfer;
50315 + if (GET_CORE_IF(pcd)->dma_desc_enable) {
50316 + uint32_t out_max_xfer =
50317 + DDMA_MAX_TRANSFER_SIZE -
50318 + (DDMA_MAX_TRANSFER_SIZE % 4);
50319 + if (ep->dwc_ep.is_in) {
50320 + if (ep->dwc_ep.maxxfer >
50321 + DDMA_MAX_TRANSFER_SIZE) {
50322 + ep->dwc_ep.maxxfer =
50323 + DDMA_MAX_TRANSFER_SIZE;
50324 + }
50325 + } else {
50326 + if (ep->dwc_ep.maxxfer >
50327 + out_max_xfer) {
50328 + ep->dwc_ep.maxxfer =
50329 + out_max_xfer;
50330 + }
50331 + }
50332 + }
50333 + if (ep->dwc_ep.maxxfer < ep->dwc_ep.total_len) {
50334 + ep->dwc_ep.maxxfer -=
50335 + (ep->dwc_ep.maxxfer %
50336 + ep->dwc_ep.maxpacket);
50337 + }
50338 +
50339 + if (zero) {
50340 + if ((ep->dwc_ep.total_len %
50341 + ep->dwc_ep.maxpacket == 0)
50342 + && (ep->dwc_ep.total_len != 0)) {
50343 + ep->dwc_ep.sent_zlp = 1;
50344 + }
50345 + }
50346 +#ifdef DWC_UTE_CFI
50347 + }
50348 +#endif
50349 + dwc_otg_ep_start_transfer(GET_CORE_IF(pcd),
50350 + &ep->dwc_ep);
50351 + }
50352 + }
50353 +
50354 + if (req != 0) {
50355 + ++pcd->request_pending;
50356 + DWC_CIRCLEQ_INSERT_TAIL(&ep->queue, req, queue_entry);
50357 + if (ep->dwc_ep.is_in && ep->stopped
50358 + && !(GET_CORE_IF(pcd)->dma_enable)) {
50359 + /** @todo NGS Create a function for this. */
50360 + diepmsk_data_t diepmsk = {.d32 = 0 };
50361 + diepmsk.b.intktxfemp = 1;
50362 + if (GET_CORE_IF(pcd)->multiproc_int_enable) {
50363 + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->
50364 + dev_if->dev_global_regs->diepeachintmsk
50365 + [ep->dwc_ep.num], 0,
50366 + diepmsk.d32);
50367 + } else {
50368 + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->
50369 + dev_if->dev_global_regs->
50370 + diepmsk, 0, diepmsk.d32);
50371 + }
50372 +
50373 + }
50374 + }
50375 + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
50376 +
50377 + return 0;
50378 +}
50379 +
50380 +int dwc_otg_pcd_ep_dequeue(dwc_otg_pcd_t * pcd, void *ep_handle,
50381 + void *req_handle)
50382 +{
50383 + dwc_irqflags_t flags;
50384 + dwc_otg_pcd_request_t *req;
50385 + dwc_otg_pcd_ep_t *ep;
50386 +
50387 + ep = get_ep_from_handle(pcd, ep_handle);
50388 + if (!ep || (!ep->desc && ep->dwc_ep.num != 0)) {
50389 + DWC_WARN("bad argument\n");
50390 + return -DWC_E_INVALID;
50391 + }
50392 +
50393 + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
50394 +
50395 + /* make sure it's actually queued on this endpoint */
50396 + DWC_CIRCLEQ_FOREACH(req, &ep->queue, queue_entry) {
50397 + if (req->priv == (void *)req_handle) {
50398 + break;
50399 + }
50400 + }
50401 +
50402 + if (req->priv != (void *)req_handle) {
50403 + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
50404 + return -DWC_E_INVALID;
50405 + }
50406 +
50407 + if (!DWC_CIRCLEQ_EMPTY_ENTRY(req, queue_entry)) {
50408 + dwc_otg_request_done(ep, req, -DWC_E_RESTART);
50409 + } else {
50410 + req = NULL;
50411 + }
50412 +
50413 + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
50414 +
50415 + return req ? 0 : -DWC_E_SHUTDOWN;
50416 +
50417 +}
50418 +
50419 +/**
50420 + * dwc_otg_pcd_ep_wedge - sets the halt feature and ignores clear requests
50421 + *
50422 + * Use this to stall an endpoint and ignore CLEAR_FEATURE(HALT_ENDPOINT)
50423 + * requests. If the gadget driver clears the halt status, it will
50424 + * automatically unwedge the endpoint.
50425 + *
50426 + * Returns zero on success, else negative DWC error code.
50427 + */
50428 +int dwc_otg_pcd_ep_wedge(dwc_otg_pcd_t * pcd, void *ep_handle)
50429 +{
50430 + dwc_otg_pcd_ep_t *ep;
50431 + dwc_irqflags_t flags;
50432 + int retval = 0;
50433 +
50434 + ep = get_ep_from_handle(pcd, ep_handle);
50435 +
50436 + if ((!ep->desc && ep != &pcd->ep0) ||
50437 + (ep->desc && (ep->desc->bmAttributes == UE_ISOCHRONOUS))) {
50438 + DWC_WARN("%s, bad ep\n", __func__);
50439 + return -DWC_E_INVALID;
50440 + }
50441 +
50442 + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
50443 + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
50444 + DWC_WARN("%d %s XFer In process\n", ep->dwc_ep.num,
50445 + ep->dwc_ep.is_in ? "IN" : "OUT");
50446 + retval = -DWC_E_AGAIN;
50447 + } else {
50448 + /* This code needs to be reviewed */
50449 + if (ep->dwc_ep.is_in == 1 && GET_CORE_IF(pcd)->dma_desc_enable) {
50450 + dtxfsts_data_t txstatus;
50451 + fifosize_data_t txfifosize;
50452 +
50453 + txfifosize.d32 =
50454 + DWC_READ_REG32(&GET_CORE_IF(pcd)->
50455 + core_global_regs->dtxfsiz[ep->dwc_ep.
50456 + tx_fifo_num]);
50457 + txstatus.d32 =
50458 + DWC_READ_REG32(&GET_CORE_IF(pcd)->
50459 + dev_if->in_ep_regs[ep->dwc_ep.num]->
50460 + dtxfsts);
50461 +
50462 + if (txstatus.b.txfspcavail < txfifosize.b.depth) {
50463 + DWC_WARN("%s() Data In Tx Fifo\n", __func__);
50464 + retval = -DWC_E_AGAIN;
50465 + } else {
50466 + if (ep->dwc_ep.num == 0) {
50467 + pcd->ep0state = EP0_STALL;
50468 + }
50469 +
50470 + ep->stopped = 1;
50471 + dwc_otg_ep_set_stall(GET_CORE_IF(pcd),
50472 + &ep->dwc_ep);
50473 + }
50474 + } else {
50475 + if (ep->dwc_ep.num == 0) {
50476 + pcd->ep0state = EP0_STALL;
50477 + }
50478 +
50479 + ep->stopped = 1;
50480 + dwc_otg_ep_set_stall(GET_CORE_IF(pcd), &ep->dwc_ep);
50481 + }
50482 + }
50483 +
50484 + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
50485 +
50486 + return retval;
50487 +}
50488 +
50489 +int dwc_otg_pcd_ep_halt(dwc_otg_pcd_t * pcd, void *ep_handle, int value)
50490 +{
50491 + dwc_otg_pcd_ep_t *ep;
50492 + dwc_irqflags_t flags;
50493 + int retval = 0;
50494 +
50495 + ep = get_ep_from_handle(pcd, ep_handle);
50496 +
50497 + if (!ep || (!ep->desc && ep != &pcd->ep0) ||
50498 + (ep->desc && (ep->desc->bmAttributes == UE_ISOCHRONOUS))) {
50499 + DWC_WARN("%s, bad ep\n", __func__);
50500 + return -DWC_E_INVALID;
50501 + }
50502 +
50503 + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
50504 + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
50505 + DWC_WARN("%d %s XFer In process\n", ep->dwc_ep.num,
50506 + ep->dwc_ep.is_in ? "IN" : "OUT");
50507 + retval = -DWC_E_AGAIN;
50508 + } else if (value == 0) {
50509 + dwc_otg_ep_clear_stall(GET_CORE_IF(pcd), &ep->dwc_ep);
50510 + } else if (value == 1) {
50511 + if (ep->dwc_ep.is_in == 1 && GET_CORE_IF(pcd)->dma_desc_enable) {
50512 + dtxfsts_data_t txstatus;
50513 + fifosize_data_t txfifosize;
50514 +
50515 + txfifosize.d32 =
50516 + DWC_READ_REG32(&GET_CORE_IF(pcd)->core_global_regs->
50517 + dtxfsiz[ep->dwc_ep.tx_fifo_num]);
50518 + txstatus.d32 =
50519 + DWC_READ_REG32(&GET_CORE_IF(pcd)->dev_if->
50520 + in_ep_regs[ep->dwc_ep.num]->dtxfsts);
50521 +
50522 + if (txstatus.b.txfspcavail < txfifosize.b.depth) {
50523 + DWC_WARN("%s() Data In Tx Fifo\n", __func__);
50524 + retval = -DWC_E_AGAIN;
50525 + } else {
50526 + if (ep->dwc_ep.num == 0) {
50527 + pcd->ep0state = EP0_STALL;
50528 + }
50529 +
50530 + ep->stopped = 1;
50531 + dwc_otg_ep_set_stall(GET_CORE_IF(pcd),
50532 + &ep->dwc_ep);
50533 + }
50534 + } else {
50535 + if (ep->dwc_ep.num == 0) {
50536 + pcd->ep0state = EP0_STALL;
50537 + }
50538 +
50539 + ep->stopped = 1;
50540 + dwc_otg_ep_set_stall(GET_CORE_IF(pcd), &ep->dwc_ep);
50541 + }
50542 + } else if (value == 2) {
50543 + ep->dwc_ep.stall_clear_flag = 0;
50544 + } else if (value == 3) {
50545 + ep->dwc_ep.stall_clear_flag = 1;
50546 + }
50547 +
50548 + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
50549 +
50550 + return retval;
50551 +}
50552 +
50553 +/**
50554 + * This function initiates remote wakeup of the host from suspend state.
50555 + */
50556 +void dwc_otg_pcd_rem_wkup_from_suspend(dwc_otg_pcd_t * pcd, int set)
50557 +{
50558 + dctl_data_t dctl = { 0 };
50559 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
50560 + dsts_data_t dsts;
50561 +
50562 + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
50563 + if (!dsts.b.suspsts) {
50564 + DWC_WARN("Remote wakeup while is not in suspend state\n");
50565 + }
50566 + /* Check if DEVICE_REMOTE_WAKEUP feature enabled */
50567 + if (pcd->remote_wakeup_enable) {
50568 + if (set) {
50569 +
50570 + if (core_if->adp_enable) {
50571 + gpwrdn_data_t gpwrdn;
50572 +
50573 + dwc_otg_adp_probe_stop(core_if);
50574 +
50575 + /* Mask SRP detected interrupt from Power Down Logic */
50576 + gpwrdn.d32 = 0;
50577 + gpwrdn.b.srp_det_msk = 1;
50578 + DWC_MODIFY_REG32(&core_if->
50579 + core_global_regs->gpwrdn,
50580 + gpwrdn.d32, 0);
50581 +
50582 + /* Disable Power Down Logic */
50583 + gpwrdn.d32 = 0;
50584 + gpwrdn.b.pmuactv = 1;
50585 + DWC_MODIFY_REG32(&core_if->
50586 + core_global_regs->gpwrdn,
50587 + gpwrdn.d32, 0);
50588 +
50589 + /*
50590 + * Initialize the Core for Device mode.
50591 + */
50592 + core_if->op_state = B_PERIPHERAL;
50593 + dwc_otg_core_init(core_if);
50594 + dwc_otg_enable_global_interrupts(core_if);
50595 + cil_pcd_start(core_if);
50596 +
50597 + dwc_otg_initiate_srp(core_if);
50598 + }
50599 +
50600 + dctl.b.rmtwkupsig = 1;
50601 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
50602 + dctl, 0, dctl.d32);
50603 + DWC_DEBUGPL(DBG_PCD, "Set Remote Wakeup\n");
50604 +
50605 + dwc_mdelay(2);
50606 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
50607 + dctl, dctl.d32, 0);
50608 + DWC_DEBUGPL(DBG_PCD, "Clear Remote Wakeup\n");
50609 + }
50610 + } else {
50611 + DWC_DEBUGPL(DBG_PCD, "Remote Wakeup is disabled\n");
50612 + }
50613 +}
50614 +
50615 +#ifdef CONFIG_USB_DWC_OTG_LPM
50616 +/**
50617 + * This function initiates remote wakeup of the host from L1 sleep state.
50618 + */
50619 +void dwc_otg_pcd_rem_wkup_from_sleep(dwc_otg_pcd_t * pcd, int set)
50620 +{
50621 + glpmcfg_data_t lpmcfg;
50622 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
50623 +
50624 + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
50625 +
50626 + /* Check if we are in L1 state */
50627 + if (!lpmcfg.b.prt_sleep_sts) {
50628 + DWC_DEBUGPL(DBG_PCD, "Device is not in sleep state\n");
50629 + return;
50630 + }
50631 +
50632 + /* Check if host allows remote wakeup */
50633 + if (!lpmcfg.b.rem_wkup_en) {
50634 + DWC_DEBUGPL(DBG_PCD, "Host does not allow remote wakeup\n");
50635 + return;
50636 + }
50637 +
50638 + /* Check if Resume OK */
50639 + if (!lpmcfg.b.sleep_state_resumeok) {
50640 + DWC_DEBUGPL(DBG_PCD, "Sleep state resume is not OK\n");
50641 + return;
50642 + }
50643 +
50644 + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
50645 + lpmcfg.b.en_utmi_sleep = 0;
50646 + lpmcfg.b.hird_thres &= (~(1 << 4));
50647 + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg, lpmcfg.d32);
50648 +
50649 + if (set) {
50650 + dctl_data_t dctl = {.d32 = 0 };
50651 + dctl.b.rmtwkupsig = 1;
50652 + /* Set RmtWkUpSig bit to start remote wakup signaling.
50653 + * Hardware will automatically clear this bit.
50654 + */
50655 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl,
50656 + 0, dctl.d32);
50657 + DWC_DEBUGPL(DBG_PCD, "Set Remote Wakeup\n");
50658 + }
50659 +
50660 +}
50661 +#endif
50662 +
50663 +/**
50664 + * Performs remote wakeup.
50665 + */
50666 +void dwc_otg_pcd_remote_wakeup(dwc_otg_pcd_t * pcd, int set)
50667 +{
50668 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
50669 + dwc_irqflags_t flags;
50670 + if (dwc_otg_is_device_mode(core_if)) {
50671 + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
50672 +#ifdef CONFIG_USB_DWC_OTG_LPM
50673 + if (core_if->lx_state == DWC_OTG_L1) {
50674 + dwc_otg_pcd_rem_wkup_from_sleep(pcd, set);
50675 + } else {
50676 +#endif
50677 + dwc_otg_pcd_rem_wkup_from_suspend(pcd, set);
50678 +#ifdef CONFIG_USB_DWC_OTG_LPM
50679 + }
50680 +#endif
50681 + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
50682 + }
50683 + return;
50684 +}
50685 +
50686 +void dwc_otg_pcd_disconnect_us(dwc_otg_pcd_t * pcd, int no_of_usecs)
50687 +{
50688 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
50689 + dctl_data_t dctl = { 0 };
50690 +
50691 + if (dwc_otg_is_device_mode(core_if)) {
50692 + dctl.b.sftdiscon = 1;
50693 + DWC_PRINTF("Soft disconnect for %d useconds\n",no_of_usecs);
50694 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
50695 + dwc_udelay(no_of_usecs);
50696 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32,0);
50697 +
50698 + } else{
50699 + DWC_PRINTF("NOT SUPPORTED IN HOST MODE\n");
50700 + }
50701 + return;
50702 +
50703 +}
50704 +
50705 +int dwc_otg_pcd_wakeup(dwc_otg_pcd_t * pcd)
50706 +{
50707 + dsts_data_t dsts;
50708 + gotgctl_data_t gotgctl;
50709 +
50710 + /*
50711 + * This function starts the Protocol if no session is in progress. If
50712 + * a session is already in progress, but the device is suspended,
50713 + * remote wakeup signaling is started.
50714 + */
50715 +
50716 + /* Check if valid session */
50717 + gotgctl.d32 =
50718 + DWC_READ_REG32(&(GET_CORE_IF(pcd)->core_global_regs->gotgctl));
50719 + if (gotgctl.b.bsesvld) {
50720 + /* Check if suspend state */
50721 + dsts.d32 =
50722 + DWC_READ_REG32(&
50723 + (GET_CORE_IF(pcd)->dev_if->
50724 + dev_global_regs->dsts));
50725 + if (dsts.b.suspsts) {
50726 + dwc_otg_pcd_remote_wakeup(pcd, 1);
50727 + }
50728 + } else {
50729 + dwc_otg_pcd_initiate_srp(pcd);
50730 + }
50731 +
50732 + return 0;
50733 +
50734 +}
50735 +
50736 +/**
50737 + * Start the SRP timer to detect when the SRP does not complete within
50738 + * 6 seconds.
50739 + *
50740 + * @param pcd the pcd structure.
50741 + */
50742 +void dwc_otg_pcd_initiate_srp(dwc_otg_pcd_t * pcd)
50743 +{
50744 + dwc_irqflags_t flags;
50745 + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
50746 + dwc_otg_initiate_srp(GET_CORE_IF(pcd));
50747 + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
50748 +}
50749 +
50750 +int dwc_otg_pcd_get_frame_number(dwc_otg_pcd_t * pcd)
50751 +{
50752 + return dwc_otg_get_frame_number(GET_CORE_IF(pcd));
50753 +}
50754 +
50755 +int dwc_otg_pcd_is_lpm_enabled(dwc_otg_pcd_t * pcd)
50756 +{
50757 + return GET_CORE_IF(pcd)->core_params->lpm_enable;
50758 +}
50759 +
50760 +uint32_t get_b_hnp_enable(dwc_otg_pcd_t * pcd)
50761 +{
50762 + return pcd->b_hnp_enable;
50763 +}
50764 +
50765 +uint32_t get_a_hnp_support(dwc_otg_pcd_t * pcd)
50766 +{
50767 + return pcd->a_hnp_support;
50768 +}
50769 +
50770 +uint32_t get_a_alt_hnp_support(dwc_otg_pcd_t * pcd)
50771 +{
50772 + return pcd->a_alt_hnp_support;
50773 +}
50774 +
50775 +int dwc_otg_pcd_get_rmwkup_enable(dwc_otg_pcd_t * pcd)
50776 +{
50777 + return pcd->remote_wakeup_enable;
50778 +}
50779 +
50780 +#endif /* DWC_HOST_ONLY */
50781 --- /dev/null
50782 +++ b/drivers/usb/host/dwc_otg/dwc_otg_pcd.h
50783 @@ -0,0 +1,273 @@
50784 +/* ==========================================================================
50785 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_pcd.h $
50786 + * $Revision: #48 $
50787 + * $Date: 2012/08/10 $
50788 + * $Change: 2047372 $
50789 + *
50790 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
50791 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
50792 + * otherwise expressly agreed to in writing between Synopsys and you.
50793 + *
50794 + * The Software IS NOT an item of Licensed Software or Licensed Product under
50795 + * any End User Software License Agreement or Agreement for Licensed Product
50796 + * with Synopsys or any supplement thereto. You are permitted to use and
50797 + * redistribute this Software in source and binary forms, with or without
50798 + * modification, provided that redistributions of source code must retain this
50799 + * notice. You may not view, use, disclose, copy or distribute this file or
50800 + * any information contained herein except pursuant to this license grant from
50801 + * Synopsys. If you do not agree with this notice, including the disclaimer
50802 + * below, then you are not authorized to use the Software.
50803 + *
50804 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
50805 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50806 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50807 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
50808 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
50809 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
50810 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
50811 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50812 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50813 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
50814 + * DAMAGE.
50815 + * ========================================================================== */
50816 +#ifndef DWC_HOST_ONLY
50817 +#if !defined(__DWC_PCD_H__)
50818 +#define __DWC_PCD_H__
50819 +
50820 +#include "dwc_otg_os_dep.h"
50821 +#include "usb.h"
50822 +#include "dwc_otg_cil.h"
50823 +#include "dwc_otg_pcd_if.h"
50824 +#include "dwc_otg_driver.h"
50825 +
50826 +struct cfiobject;
50827 +
50828 +/**
50829 + * @file
50830 + *
50831 + * This file contains the structures, constants, and interfaces for
50832 + * the Perpherial Contoller Driver (PCD).
50833 + *
50834 + * The Peripheral Controller Driver (PCD) for Linux will implement the
50835 + * Gadget API, so that the existing Gadget drivers can be used. For
50836 + * the Mass Storage Function driver the File-backed USB Storage Gadget
50837 + * (FBS) driver will be used. The FBS driver supports the
50838 + * Control-Bulk (CB), Control-Bulk-Interrupt (CBI), and Bulk-Only
50839 + * transports.
50840 + *
50841 + */
50842 +
50843 +/** Invalid DMA Address */
50844 +#define DWC_DMA_ADDR_INVALID (~(dwc_dma_t)0)
50845 +
50846 +/** Max Transfer size for any EP */
50847 +#define DDMA_MAX_TRANSFER_SIZE 65535
50848 +
50849 +/**
50850 + * Get the pointer to the core_if from the pcd pointer.
50851 + */
50852 +#define GET_CORE_IF( _pcd ) (_pcd->core_if)
50853 +
50854 +/**
50855 + * States of EP0.
50856 + */
50857 +typedef enum ep0_state {
50858 + EP0_DISCONNECT, /* no host */
50859 + EP0_IDLE,
50860 + EP0_IN_DATA_PHASE,
50861 + EP0_OUT_DATA_PHASE,
50862 + EP0_IN_STATUS_PHASE,
50863 + EP0_OUT_STATUS_PHASE,
50864 + EP0_STALL,
50865 +} ep0state_e;
50866 +
50867 +/** Fordward declaration.*/
50868 +struct dwc_otg_pcd;
50869 +
50870 +/** DWC_otg iso request structure.
50871 + *
50872 + */
50873 +typedef struct usb_iso_request dwc_otg_pcd_iso_request_t;
50874 +
50875 +#ifdef DWC_UTE_PER_IO
50876 +
50877 +/**
50878 + * This shall be the exact analogy of the same type structure defined in the
50879 + * usb_gadget.h. Each descriptor contains
50880 + */
50881 +struct dwc_iso_pkt_desc_port {
50882 + uint32_t offset;
50883 + uint32_t length; /* expected length */
50884 + uint32_t actual_length;
50885 + uint32_t status;
50886 +};
50887 +
50888 +struct dwc_iso_xreq_port {
50889 + /** transfer/submission flag */
50890 + uint32_t tr_sub_flags;
50891 + /** Start the request ASAP */
50892 +#define DWC_EREQ_TF_ASAP 0x00000002
50893 + /** Just enqueue the request w/o initiating a transfer */
50894 +#define DWC_EREQ_TF_ENQUEUE 0x00000004
50895 +
50896 + /**
50897 + * count of ISO packets attached to this request - shall
50898 + * not exceed the pio_alloc_pkt_count
50899 + */
50900 + uint32_t pio_pkt_count;
50901 + /** count of ISO packets allocated for this request */
50902 + uint32_t pio_alloc_pkt_count;
50903 + /** number of ISO packet errors */
50904 + uint32_t error_count;
50905 + /** reserved for future extension */
50906 + uint32_t res;
50907 + /** Will be allocated and freed in the UTE gadget and based on the CFC value */
50908 + struct dwc_iso_pkt_desc_port *per_io_frame_descs;
50909 +};
50910 +#endif
50911 +/** DWC_otg request structure.
50912 + * This structure is a list of requests.
50913 + */
50914 +typedef struct dwc_otg_pcd_request {
50915 + void *priv;
50916 + void *buf;
50917 + dwc_dma_t dma;
50918 + uint32_t length;
50919 + uint32_t actual;
50920 + unsigned sent_zlp:1;
50921 + /**
50922 + * Used instead of original buffer if
50923 + * it(physical address) is not dword-aligned.
50924 + **/
50925 + uint8_t *dw_align_buf;
50926 + dwc_dma_t dw_align_buf_dma;
50927 +
50928 + DWC_CIRCLEQ_ENTRY(dwc_otg_pcd_request) queue_entry;
50929 +#ifdef DWC_UTE_PER_IO
50930 + struct dwc_iso_xreq_port ext_req;
50931 + //void *priv_ereq_nport; /* */
50932 +#endif
50933 +} dwc_otg_pcd_request_t;
50934 +
50935 +DWC_CIRCLEQ_HEAD(req_list, dwc_otg_pcd_request);
50936 +
50937 +/** PCD EP structure.
50938 + * This structure describes an EP, there is an array of EPs in the PCD
50939 + * structure.
50940 + */
50941 +typedef struct dwc_otg_pcd_ep {
50942 + /** USB EP Descriptor */
50943 + const usb_endpoint_descriptor_t *desc;
50944 +
50945 + /** queue of dwc_otg_pcd_requests. */
50946 + struct req_list queue;
50947 + unsigned stopped:1;
50948 + unsigned disabling:1;
50949 + unsigned dma:1;
50950 + unsigned queue_sof:1;
50951 +
50952 +#ifdef DWC_EN_ISOC
50953 + /** ISOC req handle passed */
50954 + void *iso_req_handle;
50955 +#endif //_EN_ISOC_
50956 +
50957 + /** DWC_otg ep data. */
50958 + dwc_ep_t dwc_ep;
50959 +
50960 + /** Pointer to PCD */
50961 + struct dwc_otg_pcd *pcd;
50962 +
50963 + void *priv;
50964 +} dwc_otg_pcd_ep_t;
50965 +
50966 +/** DWC_otg PCD Structure.
50967 + * This structure encapsulates the data for the dwc_otg PCD.
50968 + */
50969 +struct dwc_otg_pcd {
50970 + const struct dwc_otg_pcd_function_ops *fops;
50971 + /** The DWC otg device pointer */
50972 + struct dwc_otg_device *otg_dev;
50973 + /** Core Interface */
50974 + dwc_otg_core_if_t *core_if;
50975 + /** State of EP0 */
50976 + ep0state_e ep0state;
50977 + /** EP0 Request is pending */
50978 + unsigned ep0_pending:1;
50979 + /** Indicates when SET CONFIGURATION Request is in process */
50980 + unsigned request_config:1;
50981 + /** The state of the Remote Wakeup Enable. */
50982 + unsigned remote_wakeup_enable:1;
50983 + /** The state of the B-Device HNP Enable. */
50984 + unsigned b_hnp_enable:1;
50985 + /** The state of A-Device HNP Support. */
50986 + unsigned a_hnp_support:1;
50987 + /** The state of the A-Device Alt HNP support. */
50988 + unsigned a_alt_hnp_support:1;
50989 + /** Count of pending Requests */
50990 + unsigned request_pending;
50991 +
50992 + /** SETUP packet for EP0
50993 + * This structure is allocated as a DMA buffer on PCD initialization
50994 + * with enough space for up to 3 setup packets.
50995 + */
50996 + union {
50997 + usb_device_request_t req;
50998 + uint32_t d32[2];
50999 + } *setup_pkt;
51000 +
51001 + dwc_dma_t setup_pkt_dma_handle;
51002 +
51003 + /* Additional buffer and flag for CTRL_WR premature case */
51004 + uint8_t *backup_buf;
51005 + unsigned data_terminated;
51006 +
51007 + /** 2-byte dma buffer used to return status from GET_STATUS */
51008 + uint16_t *status_buf;
51009 + dwc_dma_t status_buf_dma_handle;
51010 +
51011 + /** EP0 */
51012 + dwc_otg_pcd_ep_t ep0;
51013 +
51014 + /** Array of IN EPs. */
51015 + dwc_otg_pcd_ep_t in_ep[MAX_EPS_CHANNELS - 1];
51016 + /** Array of OUT EPs. */
51017 + dwc_otg_pcd_ep_t out_ep[MAX_EPS_CHANNELS - 1];
51018 + /** number of valid EPs in the above array. */
51019 +// unsigned num_eps : 4;
51020 + dwc_spinlock_t *lock;
51021 +
51022 + /** Tasklet to defer starting of TEST mode transmissions until
51023 + * Status Phase has been completed.
51024 + */
51025 + dwc_tasklet_t *test_mode_tasklet;
51026 +
51027 + /** Tasklet to delay starting of xfer in DMA mode */
51028 + dwc_tasklet_t *start_xfer_tasklet;
51029 +
51030 + /** The test mode to enter when the tasklet is executed. */
51031 + unsigned test_mode;
51032 + /** The cfi_api structure that implements most of the CFI API
51033 + * and OTG specific core configuration functionality
51034 + */
51035 +#ifdef DWC_UTE_CFI
51036 + struct cfiobject *cfi;
51037 +#endif
51038 +
51039 +};
51040 +
51041 +static inline struct device *dwc_otg_pcd_to_dev(struct dwc_otg_pcd *pcd)
51042 +{
51043 + return &pcd->otg_dev->os_dep.platformdev->dev;
51044 +}
51045 +
51046 +//FIXME this functions should be static, and this prototypes should be removed
51047 +extern void dwc_otg_request_nuke(dwc_otg_pcd_ep_t * ep);
51048 +extern void dwc_otg_request_done(dwc_otg_pcd_ep_t * ep,
51049 + dwc_otg_pcd_request_t * req, int32_t status);
51050 +
51051 +void dwc_otg_iso_buffer_done(dwc_otg_pcd_t * pcd, dwc_otg_pcd_ep_t * ep,
51052 + void *req_handle);
51053 +
51054 +extern void do_test_mode(void *data);
51055 +#endif
51056 +#endif /* DWC_HOST_ONLY */
51057 --- /dev/null
51058 +++ b/drivers/usb/host/dwc_otg/dwc_otg_pcd_if.h
51059 @@ -0,0 +1,361 @@
51060 +/* ==========================================================================
51061 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_pcd_if.h $
51062 + * $Revision: #11 $
51063 + * $Date: 2011/10/26 $
51064 + * $Change: 1873028 $
51065 + *
51066 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
51067 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
51068 + * otherwise expressly agreed to in writing between Synopsys and you.
51069 + *
51070 + * The Software IS NOT an item of Licensed Software or Licensed Product under
51071 + * any End User Software License Agreement or Agreement for Licensed Product
51072 + * with Synopsys or any supplement thereto. You are permitted to use and
51073 + * redistribute this Software in source and binary forms, with or without
51074 + * modification, provided that redistributions of source code must retain this
51075 + * notice. You may not view, use, disclose, copy or distribute this file or
51076 + * any information contained herein except pursuant to this license grant from
51077 + * Synopsys. If you do not agree with this notice, including the disclaimer
51078 + * below, then you are not authorized to use the Software.
51079 + *
51080 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
51081 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51082 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51083 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
51084 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
51085 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
51086 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
51087 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51088 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51089 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
51090 + * DAMAGE.
51091 + * ========================================================================== */
51092 +#ifndef DWC_HOST_ONLY
51093 +
51094 +#if !defined(__DWC_PCD_IF_H__)
51095 +#define __DWC_PCD_IF_H__
51096 +
51097 +//#include "dwc_os.h"
51098 +#include "dwc_otg_core_if.h"
51099 +#include "dwc_otg_driver.h"
51100 +
51101 +/** @file
51102 + * This file defines DWC_OTG PCD Core API.
51103 + */
51104 +
51105 +struct dwc_otg_pcd;
51106 +typedef struct dwc_otg_pcd dwc_otg_pcd_t;
51107 +
51108 +/** Maxpacket size for EP0 */
51109 +#define MAX_EP0_SIZE 64
51110 +/** Maxpacket size for any EP */
51111 +#define MAX_PACKET_SIZE 1024
51112 +
51113 +/** @name Function Driver Callbacks */
51114 +/** @{ */
51115 +
51116 +/** This function will be called whenever a previously queued request has
51117 + * completed. The status value will be set to -DWC_E_SHUTDOWN to indicated a
51118 + * failed or aborted transfer, or -DWC_E_RESTART to indicate the device was reset,
51119 + * or -DWC_E_TIMEOUT to indicate it timed out, or -DWC_E_INVALID to indicate invalid
51120 + * parameters. */
51121 +typedef int (*dwc_completion_cb_t) (dwc_otg_pcd_t * pcd, void *ep_handle,
51122 + void *req_handle, int32_t status,
51123 + uint32_t actual);
51124 +/**
51125 + * This function will be called whenever a previousle queued ISOC request has
51126 + * completed. Count of ISOC packets could be read using dwc_otg_pcd_get_iso_packet_count
51127 + * function.
51128 + * The status of each ISOC packet could be read using dwc_otg_pcd_get_iso_packet_*
51129 + * functions.
51130 + */
51131 +typedef int (*dwc_isoc_completion_cb_t) (dwc_otg_pcd_t * pcd, void *ep_handle,
51132 + void *req_handle, int proc_buf_num);
51133 +/** This function should handle any SETUP request that cannot be handled by the
51134 + * PCD Core. This includes most GET_DESCRIPTORs, SET_CONFIGS, Any
51135 + * class-specific requests, etc. The function must non-blocking.
51136 + *
51137 + * Returns 0 on success.
51138 + * Returns -DWC_E_NOT_SUPPORTED if the request is not supported.
51139 + * Returns -DWC_E_INVALID if the setup request had invalid parameters or bytes.
51140 + * Returns -DWC_E_SHUTDOWN on any other error. */
51141 +typedef int (*dwc_setup_cb_t) (dwc_otg_pcd_t * pcd, uint8_t * bytes);
51142 +/** This is called whenever the device has been disconnected. The function
51143 + * driver should take appropriate action to clean up all pending requests in the
51144 + * PCD Core, remove all endpoints (except ep0), and initialize back to reset
51145 + * state. */
51146 +typedef int (*dwc_disconnect_cb_t) (dwc_otg_pcd_t * pcd);
51147 +/** This function is called when device has been connected. */
51148 +typedef int (*dwc_connect_cb_t) (dwc_otg_pcd_t * pcd, int speed);
51149 +/** This function is called when device has been suspended */
51150 +typedef int (*dwc_suspend_cb_t) (dwc_otg_pcd_t * pcd);
51151 +/** This function is called when device has received LPM tokens, i.e.
51152 + * device has been sent to sleep state. */
51153 +typedef int (*dwc_sleep_cb_t) (dwc_otg_pcd_t * pcd);
51154 +/** This function is called when device has been resumed
51155 + * from suspend(L2) or L1 sleep state. */
51156 +typedef int (*dwc_resume_cb_t) (dwc_otg_pcd_t * pcd);
51157 +/** This function is called whenever hnp params has been changed.
51158 + * User can call get_b_hnp_enable, get_a_hnp_support, get_a_alt_hnp_support functions
51159 + * to get hnp parameters. */
51160 +typedef int (*dwc_hnp_params_changed_cb_t) (dwc_otg_pcd_t * pcd);
51161 +/** This function is called whenever USB RESET is detected. */
51162 +typedef int (*dwc_reset_cb_t) (dwc_otg_pcd_t * pcd);
51163 +
51164 +typedef int (*cfi_setup_cb_t) (dwc_otg_pcd_t * pcd, void *ctrl_req_bytes);
51165 +
51166 +/**
51167 + *
51168 + * @param ep_handle Void pointer to the usb_ep structure
51169 + * @param ereq_port Pointer to the extended request structure created in the
51170 + * portable part.
51171 + */
51172 +typedef int (*xiso_completion_cb_t) (dwc_otg_pcd_t * pcd, void *ep_handle,
51173 + void *req_handle, int32_t status,
51174 + void *ereq_port);
51175 +/** Function Driver Ops Data Structure */
51176 +struct dwc_otg_pcd_function_ops {
51177 + dwc_connect_cb_t connect;
51178 + dwc_disconnect_cb_t disconnect;
51179 + dwc_setup_cb_t setup;
51180 + dwc_completion_cb_t complete;
51181 + dwc_isoc_completion_cb_t isoc_complete;
51182 + dwc_suspend_cb_t suspend;
51183 + dwc_sleep_cb_t sleep;
51184 + dwc_resume_cb_t resume;
51185 + dwc_reset_cb_t reset;
51186 + dwc_hnp_params_changed_cb_t hnp_changed;
51187 + cfi_setup_cb_t cfi_setup;
51188 +#ifdef DWC_UTE_PER_IO
51189 + xiso_completion_cb_t xisoc_complete;
51190 +#endif
51191 +};
51192 +/** @} */
51193 +
51194 +/** @name Function Driver Functions */
51195 +/** @{ */
51196 +
51197 +/** Call this function to get pointer on dwc_otg_pcd_t,
51198 + * this pointer will be used for all PCD API functions.
51199 + *
51200 + * @param core_if The DWC_OTG Core
51201 + */
51202 +extern dwc_otg_pcd_t *dwc_otg_pcd_init(dwc_otg_device_t *otg_dev);
51203 +
51204 +/** Frees PCD allocated by dwc_otg_pcd_init
51205 + *
51206 + * @param pcd The PCD
51207 + */
51208 +extern void dwc_otg_pcd_remove(dwc_otg_pcd_t * pcd);
51209 +
51210 +/** Call this to bind the function driver to the PCD Core.
51211 + *
51212 + * @param pcd Pointer on dwc_otg_pcd_t returned by dwc_otg_pcd_init function.
51213 + * @param fops The Function Driver Ops data structure containing pointers to all callbacks.
51214 + */
51215 +extern void dwc_otg_pcd_start(dwc_otg_pcd_t * pcd,
51216 + const struct dwc_otg_pcd_function_ops *fops);
51217 +
51218 +/** Enables an endpoint for use. This function enables an endpoint in
51219 + * the PCD. The endpoint is described by the ep_desc which has the
51220 + * same format as a USB ep descriptor. The ep_handle parameter is used to refer
51221 + * to the endpoint from other API functions and in callbacks. Normally this
51222 + * should be called after a SET_CONFIGURATION/SET_INTERFACE to configure the
51223 + * core for that interface.
51224 + *
51225 + * Returns -DWC_E_INVALID if invalid parameters were passed.
51226 + * Returns -DWC_E_SHUTDOWN if any other error ocurred.
51227 + * Returns 0 on success.
51228 + *
51229 + * @param pcd The PCD
51230 + * @param ep_desc Endpoint descriptor
51231 + * @param usb_ep Handle on endpoint, that will be used to identify endpoint.
51232 + */
51233 +extern int dwc_otg_pcd_ep_enable(dwc_otg_pcd_t * pcd,
51234 + const uint8_t * ep_desc, void *usb_ep);
51235 +
51236 +/** Disable the endpoint referenced by ep_handle.
51237 + *
51238 + * Returns -DWC_E_INVALID if invalid parameters were passed.
51239 + * Returns -DWC_E_SHUTDOWN if any other error occurred.
51240 + * Returns 0 on success. */
51241 +extern int dwc_otg_pcd_ep_disable(dwc_otg_pcd_t * pcd, void *ep_handle);
51242 +
51243 +/** Queue a data transfer request on the endpoint referenced by ep_handle.
51244 + * After the transfer is completes, the complete callback will be called with
51245 + * the request status.
51246 + *
51247 + * @param pcd The PCD
51248 + * @param ep_handle The handle of the endpoint
51249 + * @param buf The buffer for the data
51250 + * @param dma_buf The DMA buffer for the data
51251 + * @param buflen The length of the data transfer
51252 + * @param zero Specifies whether to send zero length last packet.
51253 + * @param req_handle Set this handle to any value to use to reference this
51254 + * request in the ep_dequeue function or from the complete callback
51255 + * @param atomic_alloc If driver need to perform atomic allocations
51256 + * for internal data structures.
51257 + *
51258 + * Returns -DWC_E_INVALID if invalid parameters were passed.
51259 + * Returns -DWC_E_SHUTDOWN if any other error ocurred.
51260 + * Returns 0 on success. */
51261 +extern int dwc_otg_pcd_ep_queue(dwc_otg_pcd_t * pcd, void *ep_handle,
51262 + uint8_t * buf, dwc_dma_t dma_buf,
51263 + uint32_t buflen, int zero, void *req_handle,
51264 + int atomic_alloc);
51265 +#ifdef DWC_UTE_PER_IO
51266 +/**
51267 + *
51268 + * @param ereq_nonport Pointer to the extended request part of the
51269 + * usb_request structure defined in usb_gadget.h file.
51270 + */
51271 +extern int dwc_otg_pcd_xiso_ep_queue(dwc_otg_pcd_t * pcd, void *ep_handle,
51272 + uint8_t * buf, dwc_dma_t dma_buf,
51273 + uint32_t buflen, int zero,
51274 + void *req_handle, int atomic_alloc,
51275 + void *ereq_nonport);
51276 +
51277 +#endif
51278 +
51279 +/** De-queue the specified data transfer that has not yet completed.
51280 + *
51281 + * Returns -DWC_E_INVALID if invalid parameters were passed.
51282 + * Returns -DWC_E_SHUTDOWN if any other error ocurred.
51283 + * Returns 0 on success. */
51284 +extern int dwc_otg_pcd_ep_dequeue(dwc_otg_pcd_t * pcd, void *ep_handle,
51285 + void *req_handle);
51286 +
51287 +/** Halt (STALL) an endpoint or clear it.
51288 + *
51289 + * Returns -DWC_E_INVALID if invalid parameters were passed.
51290 + * Returns -DWC_E_SHUTDOWN if any other error ocurred.
51291 + * Returns -DWC_E_AGAIN if the STALL cannot be sent and must be tried again later
51292 + * Returns 0 on success. */
51293 +extern int dwc_otg_pcd_ep_halt(dwc_otg_pcd_t * pcd, void *ep_handle, int value);
51294 +
51295 +/** This function */
51296 +extern int dwc_otg_pcd_ep_wedge(dwc_otg_pcd_t * pcd, void *ep_handle);
51297 +
51298 +/** This function should be called on every hardware interrupt */
51299 +extern int32_t dwc_otg_pcd_handle_intr(dwc_otg_pcd_t * pcd);
51300 +
51301 +/** This function returns current frame number */
51302 +extern int dwc_otg_pcd_get_frame_number(dwc_otg_pcd_t * pcd);
51303 +
51304 +/**
51305 + * Start isochronous transfers on the endpoint referenced by ep_handle.
51306 + * For isochronous transfers duble buffering is used.
51307 + * After processing each of buffers comlete callback will be called with
51308 + * status for each transaction.
51309 + *
51310 + * @param pcd The PCD
51311 + * @param ep_handle The handle of the endpoint
51312 + * @param buf0 The virtual address of first data buffer
51313 + * @param buf1 The virtual address of second data buffer
51314 + * @param dma0 The DMA address of first data buffer
51315 + * @param dma1 The DMA address of second data buffer
51316 + * @param sync_frame Data pattern frame number
51317 + * @param dp_frame Data size for pattern frame
51318 + * @param data_per_frame Data size for regular frame
51319 + * @param start_frame Frame number to start transfers, if -1 then start transfers ASAP.
51320 + * @param buf_proc_intrvl Interval of ISOC Buffer processing
51321 + * @param req_handle Handle of ISOC request
51322 + * @param atomic_alloc Specefies whether to perform atomic allocation for
51323 + * internal data structures.
51324 + *
51325 + * Returns -DWC_E_NO_MEMORY if there is no enough memory.
51326 + * Returns -DWC_E_INVALID if incorrect arguments are passed to the function.
51327 + * Returns -DW_E_SHUTDOWN for any other error.
51328 + * Returns 0 on success
51329 + */
51330 +extern int dwc_otg_pcd_iso_ep_start(dwc_otg_pcd_t * pcd, void *ep_handle,
51331 + uint8_t * buf0, uint8_t * buf1,
51332 + dwc_dma_t dma0, dwc_dma_t dma1,
51333 + int sync_frame, int dp_frame,
51334 + int data_per_frame, int start_frame,
51335 + int buf_proc_intrvl, void *req_handle,
51336 + int atomic_alloc);
51337 +
51338 +/** Stop ISOC transfers on endpoint referenced by ep_handle.
51339 + *
51340 + * @param pcd The PCD
51341 + * @param ep_handle The handle of the endpoint
51342 + * @param req_handle Handle of ISOC request
51343 + *
51344 + * Returns -DWC_E_INVALID if incorrect arguments are passed to the function
51345 + * Returns 0 on success
51346 + */
51347 +int dwc_otg_pcd_iso_ep_stop(dwc_otg_pcd_t * pcd, void *ep_handle,
51348 + void *req_handle);
51349 +
51350 +/** Get ISOC packet status.
51351 + *
51352 + * @param pcd The PCD
51353 + * @param ep_handle The handle of the endpoint
51354 + * @param iso_req_handle Isochronoush request handle
51355 + * @param packet Number of packet
51356 + * @param status Out parameter for returning status
51357 + * @param actual Out parameter for returning actual length
51358 + * @param offset Out parameter for returning offset
51359 + *
51360 + */
51361 +extern void dwc_otg_pcd_get_iso_packet_params(dwc_otg_pcd_t * pcd,
51362 + void *ep_handle,
51363 + void *iso_req_handle, int packet,
51364 + int *status, int *actual,
51365 + int *offset);
51366 +
51367 +/** Get ISOC packet count.
51368 + *
51369 + * @param pcd The PCD
51370 + * @param ep_handle The handle of the endpoint
51371 + * @param iso_req_handle
51372 + */
51373 +extern int dwc_otg_pcd_get_iso_packet_count(dwc_otg_pcd_t * pcd,
51374 + void *ep_handle,
51375 + void *iso_req_handle);
51376 +
51377 +/** This function starts the SRP Protocol if no session is in progress. If
51378 + * a session is already in progress, but the device is suspended,
51379 + * remote wakeup signaling is started.
51380 + */
51381 +extern int dwc_otg_pcd_wakeup(dwc_otg_pcd_t * pcd);
51382 +
51383 +/** This function returns 1 if LPM support is enabled, and 0 otherwise. */
51384 +extern int dwc_otg_pcd_is_lpm_enabled(dwc_otg_pcd_t * pcd);
51385 +
51386 +/** This function returns 1 if remote wakeup is allowed and 0, otherwise. */
51387 +extern int dwc_otg_pcd_get_rmwkup_enable(dwc_otg_pcd_t * pcd);
51388 +
51389 +/** Initiate SRP */
51390 +extern void dwc_otg_pcd_initiate_srp(dwc_otg_pcd_t * pcd);
51391 +
51392 +/** Starts remote wakeup signaling. */
51393 +extern void dwc_otg_pcd_remote_wakeup(dwc_otg_pcd_t * pcd, int set);
51394 +
51395 +/** Starts micorsecond soft disconnect. */
51396 +extern void dwc_otg_pcd_disconnect_us(dwc_otg_pcd_t * pcd, int no_of_usecs);
51397 +/** This function returns whether device is dualspeed.*/
51398 +extern uint32_t dwc_otg_pcd_is_dualspeed(dwc_otg_pcd_t * pcd);
51399 +
51400 +/** This function returns whether device is otg. */
51401 +extern uint32_t dwc_otg_pcd_is_otg(dwc_otg_pcd_t * pcd);
51402 +
51403 +/** These functions allow to get hnp parameters */
51404 +extern uint32_t get_b_hnp_enable(dwc_otg_pcd_t * pcd);
51405 +extern uint32_t get_a_hnp_support(dwc_otg_pcd_t * pcd);
51406 +extern uint32_t get_a_alt_hnp_support(dwc_otg_pcd_t * pcd);
51407 +
51408 +/** CFI specific Interface functions */
51409 +/** Allocate a cfi buffer */
51410 +extern uint8_t *cfiw_ep_alloc_buffer(dwc_otg_pcd_t * pcd, void *pep,
51411 + dwc_dma_t * addr, size_t buflen,
51412 + int flags);
51413 +
51414 +/******************************************************************************/
51415 +
51416 +/** @} */
51417 +
51418 +#endif /* __DWC_PCD_IF_H__ */
51419 +
51420 +#endif /* DWC_HOST_ONLY */
51421 --- /dev/null
51422 +++ b/drivers/usb/host/dwc_otg/dwc_otg_pcd_intr.c
51423 @@ -0,0 +1,5148 @@
51424 +/* ==========================================================================
51425 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_pcd_intr.c $
51426 + * $Revision: #116 $
51427 + * $Date: 2012/08/10 $
51428 + * $Change: 2047372 $
51429 + *
51430 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
51431 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
51432 + * otherwise expressly agreed to in writing between Synopsys and you.
51433 + *
51434 + * The Software IS NOT an item of Licensed Software or Licensed Product under
51435 + * any End User Software License Agreement or Agreement for Licensed Product
51436 + * with Synopsys or any supplement thereto. You are permitted to use and
51437 + * redistribute this Software in source and binary forms, with or without
51438 + * modification, provided that redistributions of source code must retain this
51439 + * notice. You may not view, use, disclose, copy or distribute this file or
51440 + * any information contained herein except pursuant to this license grant from
51441 + * Synopsys. If you do not agree with this notice, including the disclaimer
51442 + * below, then you are not authorized to use the Software.
51443 + *
51444 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
51445 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51446 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51447 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
51448 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
51449 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
51450 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
51451 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51452 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51453 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
51454 + * DAMAGE.
51455 + * ========================================================================== */
51456 +#ifndef DWC_HOST_ONLY
51457 +
51458 +#include "dwc_otg_pcd.h"
51459 +
51460 +#ifdef DWC_UTE_CFI
51461 +#include "dwc_otg_cfi.h"
51462 +#endif
51463 +
51464 +#ifdef DWC_UTE_PER_IO
51465 +extern void complete_xiso_ep(dwc_otg_pcd_ep_t * ep);
51466 +#endif
51467 +//#define PRINT_CFI_DMA_DESCS
51468 +
51469 +#define DEBUG_EP0
51470 +
51471 +/**
51472 + * This function updates OTG.
51473 + */
51474 +static void dwc_otg_pcd_update_otg(dwc_otg_pcd_t * pcd, const unsigned reset)
51475 +{
51476 +
51477 + if (reset) {
51478 + pcd->b_hnp_enable = 0;
51479 + pcd->a_hnp_support = 0;
51480 + pcd->a_alt_hnp_support = 0;
51481 + }
51482 +
51483 + if (pcd->fops->hnp_changed) {
51484 + pcd->fops->hnp_changed(pcd);
51485 + }
51486 +}
51487 +
51488 +/** @file
51489 + * This file contains the implementation of the PCD Interrupt handlers.
51490 + *
51491 + * The PCD handles the device interrupts. Many conditions can cause a
51492 + * device interrupt. When an interrupt occurs, the device interrupt
51493 + * service routine determines the cause of the interrupt and
51494 + * dispatches handling to the appropriate function. These interrupt
51495 + * handling functions are described below.
51496 + * All interrupt registers are processed from LSB to MSB.
51497 + */
51498 +
51499 +/**
51500 + * This function prints the ep0 state for debug purposes.
51501 + */
51502 +static inline void print_ep0_state(dwc_otg_pcd_t * pcd)
51503 +{
51504 +#ifdef DEBUG
51505 + char str[40];
51506 +
51507 + switch (pcd->ep0state) {
51508 + case EP0_DISCONNECT:
51509 + dwc_strcpy(str, "EP0_DISCONNECT");
51510 + break;
51511 + case EP0_IDLE:
51512 + dwc_strcpy(str, "EP0_IDLE");
51513 + break;
51514 + case EP0_IN_DATA_PHASE:
51515 + dwc_strcpy(str, "EP0_IN_DATA_PHASE");
51516 + break;
51517 + case EP0_OUT_DATA_PHASE:
51518 + dwc_strcpy(str, "EP0_OUT_DATA_PHASE");
51519 + break;
51520 + case EP0_IN_STATUS_PHASE:
51521 + dwc_strcpy(str, "EP0_IN_STATUS_PHASE");
51522 + break;
51523 + case EP0_OUT_STATUS_PHASE:
51524 + dwc_strcpy(str, "EP0_OUT_STATUS_PHASE");
51525 + break;
51526 + case EP0_STALL:
51527 + dwc_strcpy(str, "EP0_STALL");
51528 + break;
51529 + default:
51530 + dwc_strcpy(str, "EP0_INVALID");
51531 + }
51532 +
51533 + DWC_DEBUGPL(DBG_ANY, "%s(%d)\n", str, pcd->ep0state);
51534 +#endif
51535 +}
51536 +
51537 +/**
51538 + * This function calculate the size of the payload in the memory
51539 + * for out endpoints and prints size for debug purposes(used in
51540 + * 2.93a DevOutNak feature).
51541 + */
51542 +static inline void print_memory_payload(dwc_otg_pcd_t * pcd, dwc_ep_t * ep)
51543 +{
51544 +#ifdef DEBUG
51545 + deptsiz_data_t deptsiz_init = {.d32 = 0 };
51546 + deptsiz_data_t deptsiz_updt = {.d32 = 0 };
51547 + int pack_num;
51548 + unsigned payload;
51549 +
51550 + deptsiz_init.d32 = pcd->core_if->start_doeptsiz_val[ep->num];
51551 + deptsiz_updt.d32 =
51552 + DWC_READ_REG32(&pcd->core_if->dev_if->
51553 + out_ep_regs[ep->num]->doeptsiz);
51554 + /* Payload will be */
51555 + payload = deptsiz_init.b.xfersize - deptsiz_updt.b.xfersize;
51556 + /* Packet count is decremented every time a packet
51557 + * is written to the RxFIFO not in to the external memory
51558 + * So, if payload == 0, then it means no packet was sent to ext memory*/
51559 + pack_num = (!payload) ? 0 : (deptsiz_init.b.pktcnt - deptsiz_updt.b.pktcnt);
51560 + DWC_DEBUGPL(DBG_PCDV,
51561 + "Payload for EP%d-%s\n",
51562 + ep->num, (ep->is_in ? "IN" : "OUT"));
51563 + DWC_DEBUGPL(DBG_PCDV,
51564 + "Number of transfered bytes = 0x%08x\n", payload);
51565 + DWC_DEBUGPL(DBG_PCDV,
51566 + "Number of transfered packets = %d\n", pack_num);
51567 +#endif
51568 +}
51569 +
51570 +
51571 +#ifdef DWC_UTE_CFI
51572 +static inline void print_desc(struct dwc_otg_dma_desc *ddesc,
51573 + const uint8_t * epname, int descnum)
51574 +{
51575 + CFI_INFO
51576 + ("%s DMA_DESC(%d) buf=0x%08x bytes=0x%04x; sp=0x%x; l=0x%x; sts=0x%02x; bs=0x%02x\n",
51577 + epname, descnum, ddesc->buf, ddesc->status.b.bytes,
51578 + ddesc->status.b.sp, ddesc->status.b.l, ddesc->status.b.sts,
51579 + ddesc->status.b.bs);
51580 +}
51581 +#endif
51582 +
51583 +/**
51584 + * This function returns pointer to in ep struct with number ep_num
51585 + */
51586 +static inline dwc_otg_pcd_ep_t *get_in_ep(dwc_otg_pcd_t * pcd, uint32_t ep_num)
51587 +{
51588 + int i;
51589 + int num_in_eps = GET_CORE_IF(pcd)->dev_if->num_in_eps;
51590 + if (ep_num == 0) {
51591 + return &pcd->ep0;
51592 + } else {
51593 + for (i = 0; i < num_in_eps; ++i) {
51594 + if (pcd->in_ep[i].dwc_ep.num == ep_num)
51595 + return &pcd->in_ep[i];
51596 + }
51597 + return 0;
51598 + }
51599 +}
51600 +
51601 +/**
51602 + * This function returns pointer to out ep struct with number ep_num
51603 + */
51604 +static inline dwc_otg_pcd_ep_t *get_out_ep(dwc_otg_pcd_t * pcd, uint32_t ep_num)
51605 +{
51606 + int i;
51607 + int num_out_eps = GET_CORE_IF(pcd)->dev_if->num_out_eps;
51608 + if (ep_num == 0) {
51609 + return &pcd->ep0;
51610 + } else {
51611 + for (i = 0; i < num_out_eps; ++i) {
51612 + if (pcd->out_ep[i].dwc_ep.num == ep_num)
51613 + return &pcd->out_ep[i];
51614 + }
51615 + return 0;
51616 + }
51617 +}
51618 +
51619 +/**
51620 + * This functions gets a pointer to an EP from the wIndex address
51621 + * value of the control request.
51622 + */
51623 +dwc_otg_pcd_ep_t *get_ep_by_addr(dwc_otg_pcd_t * pcd, u16 wIndex)
51624 +{
51625 + dwc_otg_pcd_ep_t *ep;
51626 + uint32_t ep_num = UE_GET_ADDR(wIndex);
51627 +
51628 + if (ep_num == 0) {
51629 + ep = &pcd->ep0;
51630 + } else if (UE_GET_DIR(wIndex) == UE_DIR_IN) { /* in ep */
51631 + ep = &pcd->in_ep[ep_num - 1];
51632 + } else {
51633 + ep = &pcd->out_ep[ep_num - 1];
51634 + }
51635 +
51636 + return ep;
51637 +}
51638 +
51639 +/**
51640 + * This function checks the EP request queue, if the queue is not
51641 + * empty the next request is started.
51642 + */
51643 +void start_next_request(dwc_otg_pcd_ep_t * ep)
51644 +{
51645 + dwc_otg_pcd_request_t *req = 0;
51646 + uint32_t max_transfer =
51647 + GET_CORE_IF(ep->pcd)->core_params->max_transfer_size;
51648 +
51649 +#ifdef DWC_UTE_CFI
51650 + struct dwc_otg_pcd *pcd;
51651 + pcd = ep->pcd;
51652 +#endif
51653 +
51654 + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
51655 + req = DWC_CIRCLEQ_FIRST(&ep->queue);
51656 +
51657 +#ifdef DWC_UTE_CFI
51658 + if (ep->dwc_ep.buff_mode != BM_STANDARD) {
51659 + ep->dwc_ep.cfi_req_len = req->length;
51660 + pcd->cfi->ops.build_descriptors(pcd->cfi, pcd, ep, req);
51661 + } else {
51662 +#endif
51663 + /* Setup and start the Transfer */
51664 + if (req->dw_align_buf) {
51665 + ep->dwc_ep.dma_addr = req->dw_align_buf_dma;
51666 + ep->dwc_ep.start_xfer_buff = req->dw_align_buf;
51667 + ep->dwc_ep.xfer_buff = req->dw_align_buf;
51668 + } else {
51669 + ep->dwc_ep.dma_addr = req->dma;
51670 + ep->dwc_ep.start_xfer_buff = req->buf;
51671 + ep->dwc_ep.xfer_buff = req->buf;
51672 + }
51673 + ep->dwc_ep.sent_zlp = 0;
51674 + ep->dwc_ep.total_len = req->length;
51675 + ep->dwc_ep.xfer_len = 0;
51676 + ep->dwc_ep.xfer_count = 0;
51677 +
51678 + ep->dwc_ep.maxxfer = max_transfer;
51679 + if (GET_CORE_IF(ep->pcd)->dma_desc_enable) {
51680 + uint32_t out_max_xfer = DDMA_MAX_TRANSFER_SIZE
51681 + - (DDMA_MAX_TRANSFER_SIZE % 4);
51682 + if (ep->dwc_ep.is_in) {
51683 + if (ep->dwc_ep.maxxfer >
51684 + DDMA_MAX_TRANSFER_SIZE) {
51685 + ep->dwc_ep.maxxfer =
51686 + DDMA_MAX_TRANSFER_SIZE;
51687 + }
51688 + } else {
51689 + if (ep->dwc_ep.maxxfer > out_max_xfer) {
51690 + ep->dwc_ep.maxxfer =
51691 + out_max_xfer;
51692 + }
51693 + }
51694 + }
51695 + if (ep->dwc_ep.maxxfer < ep->dwc_ep.total_len) {
51696 + ep->dwc_ep.maxxfer -=
51697 + (ep->dwc_ep.maxxfer % ep->dwc_ep.maxpacket);
51698 + }
51699 + if (req->sent_zlp) {
51700 + if ((ep->dwc_ep.total_len %
51701 + ep->dwc_ep.maxpacket == 0)
51702 + && (ep->dwc_ep.total_len != 0)) {
51703 + ep->dwc_ep.sent_zlp = 1;
51704 + }
51705 +
51706 + }
51707 +#ifdef DWC_UTE_CFI
51708 + }
51709 +#endif
51710 + dwc_otg_ep_start_transfer(GET_CORE_IF(ep->pcd), &ep->dwc_ep);
51711 + } else if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
51712 + DWC_PRINTF("There are no more ISOC requests \n");
51713 + ep->dwc_ep.frame_num = 0xFFFFFFFF;
51714 + }
51715 +}
51716 +
51717 +/**
51718 + * This function handles the SOF Interrupts. At this time the SOF
51719 + * Interrupt is disabled.
51720 + */
51721 +int32_t dwc_otg_pcd_handle_sof_intr(dwc_otg_pcd_t * pcd)
51722 +{
51723 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
51724 +
51725 + gintsts_data_t gintsts;
51726 +
51727 + DWC_DEBUGPL(DBG_PCD, "SOF\n");
51728 +
51729 + /* Clear interrupt */
51730 + gintsts.d32 = 0;
51731 + gintsts.b.sofintr = 1;
51732 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
51733 +
51734 + return 1;
51735 +}
51736 +
51737 +/**
51738 + * This function handles the Rx Status Queue Level Interrupt, which
51739 + * indicates that there is a least one packet in the Rx FIFO. The
51740 + * packets are moved from the FIFO to memory, where they will be
51741 + * processed when the Endpoint Interrupt Register indicates Transfer
51742 + * Complete or SETUP Phase Done.
51743 + *
51744 + * Repeat the following until the Rx Status Queue is empty:
51745 + * -# Read the Receive Status Pop Register (GRXSTSP) to get Packet
51746 + * info
51747 + * -# If Receive FIFO is empty then skip to step Clear the interrupt
51748 + * and exit
51749 + * -# If SETUP Packet call dwc_otg_read_setup_packet to copy the
51750 + * SETUP data to the buffer
51751 + * -# If OUT Data Packet call dwc_otg_read_packet to copy the data
51752 + * to the destination buffer
51753 + */
51754 +int32_t dwc_otg_pcd_handle_rx_status_q_level_intr(dwc_otg_pcd_t * pcd)
51755 +{
51756 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
51757 + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
51758 + gintmsk_data_t gintmask = {.d32 = 0 };
51759 + device_grxsts_data_t status;
51760 + dwc_otg_pcd_ep_t *ep;
51761 + gintsts_data_t gintsts;
51762 +#ifdef DEBUG
51763 + static char *dpid_str[] = { "D0", "D2", "D1", "MDATA" };
51764 +#endif
51765 +
51766 + //DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, _pcd);
51767 + /* Disable the Rx Status Queue Level interrupt */
51768 + gintmask.b.rxstsqlvl = 1;
51769 + DWC_MODIFY_REG32(&global_regs->gintmsk, gintmask.d32, 0);
51770 +
51771 + /* Get the Status from the top of the FIFO */
51772 + status.d32 = DWC_READ_REG32(&global_regs->grxstsp);
51773 +
51774 + DWC_DEBUGPL(DBG_PCD, "EP:%d BCnt:%d DPID:%s "
51775 + "pktsts:%x Frame:%d(0x%0x)\n",
51776 + status.b.epnum, status.b.bcnt,
51777 + dpid_str[status.b.dpid],
51778 + status.b.pktsts, status.b.fn, status.b.fn);
51779 + /* Get pointer to EP structure */
51780 + ep = get_out_ep(pcd, status.b.epnum);
51781 +
51782 + switch (status.b.pktsts) {
51783 + case DWC_DSTS_GOUT_NAK:
51784 + DWC_DEBUGPL(DBG_PCDV, "Global OUT NAK\n");
51785 + break;
51786 + case DWC_STS_DATA_UPDT:
51787 + DWC_DEBUGPL(DBG_PCDV, "OUT Data Packet\n");
51788 + if (status.b.bcnt && ep->dwc_ep.xfer_buff) {
51789 + /** @todo NGS Check for buffer overflow? */
51790 + dwc_otg_read_packet(core_if,
51791 + ep->dwc_ep.xfer_buff,
51792 + status.b.bcnt);
51793 + ep->dwc_ep.xfer_count += status.b.bcnt;
51794 + ep->dwc_ep.xfer_buff += status.b.bcnt;
51795 + }
51796 + break;
51797 + case DWC_STS_XFER_COMP:
51798 + DWC_DEBUGPL(DBG_PCDV, "OUT Complete\n");
51799 + break;
51800 + case DWC_DSTS_SETUP_COMP:
51801 +#ifdef DEBUG_EP0
51802 + DWC_DEBUGPL(DBG_PCDV, "Setup Complete\n");
51803 +#endif
51804 + break;
51805 + case DWC_DSTS_SETUP_UPDT:
51806 + dwc_otg_read_setup_packet(core_if, pcd->setup_pkt->d32);
51807 +#ifdef DEBUG_EP0
51808 + DWC_DEBUGPL(DBG_PCD,
51809 + "SETUP PKT: %02x.%02x v%04x i%04x l%04x\n",
51810 + pcd->setup_pkt->req.bmRequestType,
51811 + pcd->setup_pkt->req.bRequest,
51812 + UGETW(pcd->setup_pkt->req.wValue),
51813 + UGETW(pcd->setup_pkt->req.wIndex),
51814 + UGETW(pcd->setup_pkt->req.wLength));
51815 +#endif
51816 + ep->dwc_ep.xfer_count += status.b.bcnt;
51817 + break;
51818 + default:
51819 + DWC_DEBUGPL(DBG_PCDV, "Invalid Packet Status (0x%0x)\n",
51820 + status.b.pktsts);
51821 + break;
51822 + }
51823 +
51824 + /* Enable the Rx Status Queue Level interrupt */
51825 + DWC_MODIFY_REG32(&global_regs->gintmsk, 0, gintmask.d32);
51826 + /* Clear interrupt */
51827 + gintsts.d32 = 0;
51828 + gintsts.b.rxstsqlvl = 1;
51829 + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
51830 +
51831 + //DWC_DEBUGPL(DBG_PCDV, "EXIT: %s\n", __func__);
51832 + return 1;
51833 +}
51834 +
51835 +/**
51836 + * This function examines the Device IN Token Learning Queue to
51837 + * determine the EP number of the last IN token received. This
51838 + * implementation is for the Mass Storage device where there are only
51839 + * 2 IN EPs (Control-IN and BULK-IN).
51840 + *
51841 + * The EP numbers for the first six IN Tokens are in DTKNQR1 and there
51842 + * are 8 EP Numbers in each of the other possible DTKNQ Registers.
51843 + *
51844 + * @param core_if Programming view of DWC_otg controller.
51845 + *
51846 + */
51847 +static inline int get_ep_of_last_in_token(dwc_otg_core_if_t * core_if)
51848 +{
51849 + dwc_otg_device_global_regs_t *dev_global_regs =
51850 + core_if->dev_if->dev_global_regs;
51851 + const uint32_t TOKEN_Q_DEPTH = core_if->hwcfg2.b.dev_token_q_depth;
51852 + /* Number of Token Queue Registers */
51853 + const int DTKNQ_REG_CNT = (TOKEN_Q_DEPTH + 7) / 8;
51854 + dtknq1_data_t dtknqr1;
51855 + uint32_t in_tkn_epnums[4];
51856 + int ndx = 0;
51857 + int i = 0;
51858 + volatile uint32_t *addr = &dev_global_regs->dtknqr1;
51859 + int epnum = 0;
51860 +
51861 + //DWC_DEBUGPL(DBG_PCD,"dev_token_q_depth=%d\n",TOKEN_Q_DEPTH);
51862 +
51863 + /* Read the DTKNQ Registers */
51864 + for (i = 0; i < DTKNQ_REG_CNT; i++) {
51865 + in_tkn_epnums[i] = DWC_READ_REG32(addr);
51866 + DWC_DEBUGPL(DBG_PCDV, "DTKNQR%d=0x%08x\n", i + 1,
51867 + in_tkn_epnums[i]);
51868 + if (addr == &dev_global_regs->dvbusdis) {
51869 + addr = &dev_global_regs->dtknqr3_dthrctl;
51870 + } else {
51871 + ++addr;
51872 + }
51873 +
51874 + }
51875 +
51876 + /* Copy the DTKNQR1 data to the bit field. */
51877 + dtknqr1.d32 = in_tkn_epnums[0];
51878 + /* Get the EP numbers */
51879 + in_tkn_epnums[0] = dtknqr1.b.epnums0_5;
51880 + ndx = dtknqr1.b.intknwptr - 1;
51881 +
51882 + //DWC_DEBUGPL(DBG_PCDV,"ndx=%d\n",ndx);
51883 + if (ndx == -1) {
51884 + /** @todo Find a simpler way to calculate the max
51885 + * queue position.*/
51886 + int cnt = TOKEN_Q_DEPTH;
51887 + if (TOKEN_Q_DEPTH <= 6) {
51888 + cnt = TOKEN_Q_DEPTH - 1;
51889 + } else if (TOKEN_Q_DEPTH <= 14) {
51890 + cnt = TOKEN_Q_DEPTH - 7;
51891 + } else if (TOKEN_Q_DEPTH <= 22) {
51892 + cnt = TOKEN_Q_DEPTH - 15;
51893 + } else {
51894 + cnt = TOKEN_Q_DEPTH - 23;
51895 + }
51896 + epnum = (in_tkn_epnums[DTKNQ_REG_CNT - 1] >> (cnt * 4)) & 0xF;
51897 + } else {
51898 + if (ndx <= 5) {
51899 + epnum = (in_tkn_epnums[0] >> (ndx * 4)) & 0xF;
51900 + } else if (ndx <= 13) {
51901 + ndx -= 6;
51902 + epnum = (in_tkn_epnums[1] >> (ndx * 4)) & 0xF;
51903 + } else if (ndx <= 21) {
51904 + ndx -= 14;
51905 + epnum = (in_tkn_epnums[2] >> (ndx * 4)) & 0xF;
51906 + } else if (ndx <= 29) {
51907 + ndx -= 22;
51908 + epnum = (in_tkn_epnums[3] >> (ndx * 4)) & 0xF;
51909 + }
51910 + }
51911 + //DWC_DEBUGPL(DBG_PCD,"epnum=%d\n",epnum);
51912 + return epnum;
51913 +}
51914 +
51915 +/**
51916 + * This interrupt occurs when the non-periodic Tx FIFO is half-empty.
51917 + * The active request is checked for the next packet to be loaded into
51918 + * the non-periodic Tx FIFO.
51919 + */
51920 +int32_t dwc_otg_pcd_handle_np_tx_fifo_empty_intr(dwc_otg_pcd_t * pcd)
51921 +{
51922 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
51923 + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
51924 + dwc_otg_dev_in_ep_regs_t *ep_regs;
51925 + gnptxsts_data_t txstatus = {.d32 = 0 };
51926 + gintsts_data_t gintsts;
51927 +
51928 + int epnum = 0;
51929 + dwc_otg_pcd_ep_t *ep = 0;
51930 + uint32_t len = 0;
51931 + int dwords;
51932 +
51933 + /* Get the epnum from the IN Token Learning Queue. */
51934 + epnum = get_ep_of_last_in_token(core_if);
51935 + ep = get_in_ep(pcd, epnum);
51936 +
51937 + DWC_DEBUGPL(DBG_PCD, "NP TxFifo Empty: %d \n", epnum);
51938 +
51939 + ep_regs = core_if->dev_if->in_ep_regs[epnum];
51940 +
51941 + len = ep->dwc_ep.xfer_len - ep->dwc_ep.xfer_count;
51942 + if (len > ep->dwc_ep.maxpacket) {
51943 + len = ep->dwc_ep.maxpacket;
51944 + }
51945 + dwords = (len + 3) / 4;
51946 +
51947 + /* While there is space in the queue and space in the FIFO and
51948 + * More data to tranfer, Write packets to the Tx FIFO */
51949 + txstatus.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
51950 + DWC_DEBUGPL(DBG_PCDV, "b4 GNPTXSTS=0x%08x\n", txstatus.d32);
51951 +
51952 + while (txstatus.b.nptxqspcavail > 0 &&
51953 + txstatus.b.nptxfspcavail > dwords &&
51954 + ep->dwc_ep.xfer_count < ep->dwc_ep.xfer_len) {
51955 + /* Write the FIFO */
51956 + dwc_otg_ep_write_packet(core_if, &ep->dwc_ep, 0);
51957 + len = ep->dwc_ep.xfer_len - ep->dwc_ep.xfer_count;
51958 +
51959 + if (len > ep->dwc_ep.maxpacket) {
51960 + len = ep->dwc_ep.maxpacket;
51961 + }
51962 +
51963 + dwords = (len + 3) / 4;
51964 + txstatus.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
51965 + DWC_DEBUGPL(DBG_PCDV, "GNPTXSTS=0x%08x\n", txstatus.d32);
51966 + }
51967 +
51968 + DWC_DEBUGPL(DBG_PCDV, "GNPTXSTS=0x%08x\n",
51969 + DWC_READ_REG32(&global_regs->gnptxsts));
51970 +
51971 + /* Clear interrupt */
51972 + gintsts.d32 = 0;
51973 + gintsts.b.nptxfempty = 1;
51974 + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
51975 +
51976 + return 1;
51977 +}
51978 +
51979 +/**
51980 + * This function is called when dedicated Tx FIFO Empty interrupt occurs.
51981 + * The active request is checked for the next packet to be loaded into
51982 + * apropriate Tx FIFO.
51983 + */
51984 +static int32_t write_empty_tx_fifo(dwc_otg_pcd_t * pcd, uint32_t epnum)
51985 +{
51986 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
51987 + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
51988 + dwc_otg_dev_in_ep_regs_t *ep_regs;
51989 + dtxfsts_data_t txstatus = {.d32 = 0 };
51990 + dwc_otg_pcd_ep_t *ep = 0;
51991 + uint32_t len = 0;
51992 + int dwords;
51993 +
51994 + ep = get_in_ep(pcd, epnum);
51995 +
51996 + DWC_DEBUGPL(DBG_PCD, "Dedicated TxFifo Empty: %d \n", epnum);
51997 +
51998 + ep_regs = core_if->dev_if->in_ep_regs[epnum];
51999 +
52000 + len = ep->dwc_ep.xfer_len - ep->dwc_ep.xfer_count;
52001 +
52002 + if (len > ep->dwc_ep.maxpacket) {
52003 + len = ep->dwc_ep.maxpacket;
52004 + }
52005 +
52006 + dwords = (len + 3) / 4;
52007 +
52008 + /* While there is space in the queue and space in the FIFO and
52009 + * More data to tranfer, Write packets to the Tx FIFO */
52010 + txstatus.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts);
52011 + DWC_DEBUGPL(DBG_PCDV, "b4 dtxfsts[%d]=0x%08x\n", epnum, txstatus.d32);
52012 +
52013 + while (txstatus.b.txfspcavail > dwords &&
52014 + ep->dwc_ep.xfer_count < ep->dwc_ep.xfer_len &&
52015 + ep->dwc_ep.xfer_len != 0) {
52016 + /* Write the FIFO */
52017 + dwc_otg_ep_write_packet(core_if, &ep->dwc_ep, 0);
52018 +
52019 + len = ep->dwc_ep.xfer_len - ep->dwc_ep.xfer_count;
52020 + if (len > ep->dwc_ep.maxpacket) {
52021 + len = ep->dwc_ep.maxpacket;
52022 + }
52023 +
52024 + dwords = (len + 3) / 4;
52025 + txstatus.d32 =
52026 + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts);
52027 + DWC_DEBUGPL(DBG_PCDV, "dtxfsts[%d]=0x%08x\n", epnum,
52028 + txstatus.d32);
52029 + }
52030 +
52031 + DWC_DEBUGPL(DBG_PCDV, "b4 dtxfsts[%d]=0x%08x\n", epnum,
52032 + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts));
52033 +
52034 + return 1;
52035 +}
52036 +
52037 +/**
52038 + * This function is called when the Device is disconnected. It stops
52039 + * any active requests and informs the Gadget driver of the
52040 + * disconnect.
52041 + */
52042 +void dwc_otg_pcd_stop(dwc_otg_pcd_t * pcd)
52043 +{
52044 + int i, num_in_eps, num_out_eps;
52045 + dwc_otg_pcd_ep_t *ep;
52046 +
52047 + gintmsk_data_t intr_mask = {.d32 = 0 };
52048 +
52049 + DWC_SPINLOCK(pcd->lock);
52050 +
52051 + num_in_eps = GET_CORE_IF(pcd)->dev_if->num_in_eps;
52052 + num_out_eps = GET_CORE_IF(pcd)->dev_if->num_out_eps;
52053 +
52054 + DWC_DEBUGPL(DBG_PCDV, "%s() \n", __func__);
52055 + /* don't disconnect drivers more than once */
52056 + if (pcd->ep0state == EP0_DISCONNECT) {
52057 + DWC_DEBUGPL(DBG_ANY, "%s() Already Disconnected\n", __func__);
52058 + DWC_SPINUNLOCK(pcd->lock);
52059 + return;
52060 + }
52061 + pcd->ep0state = EP0_DISCONNECT;
52062 +
52063 + /* Reset the OTG state. */
52064 + dwc_otg_pcd_update_otg(pcd, 1);
52065 +
52066 + /* Disable the NP Tx Fifo Empty Interrupt. */
52067 + intr_mask.b.nptxfempty = 1;
52068 + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
52069 + intr_mask.d32, 0);
52070 +
52071 + /* Flush the FIFOs */
52072 + /**@todo NGS Flush Periodic FIFOs */
52073 + dwc_otg_flush_tx_fifo(GET_CORE_IF(pcd), 0x10);
52074 + dwc_otg_flush_rx_fifo(GET_CORE_IF(pcd));
52075 +
52076 + /* prevent new request submissions, kill any outstanding requests */
52077 + ep = &pcd->ep0;
52078 + dwc_otg_request_nuke(ep);
52079 + /* prevent new request submissions, kill any outstanding requests */
52080 + for (i = 0; i < num_in_eps; i++) {
52081 + dwc_otg_pcd_ep_t *ep = &pcd->in_ep[i];
52082 + dwc_otg_request_nuke(ep);
52083 + }
52084 + /* prevent new request submissions, kill any outstanding requests */
52085 + for (i = 0; i < num_out_eps; i++) {
52086 + dwc_otg_pcd_ep_t *ep = &pcd->out_ep[i];
52087 + dwc_otg_request_nuke(ep);
52088 + }
52089 +
52090 + /* report disconnect; the driver is already quiesced */
52091 + if (pcd->fops->disconnect) {
52092 + DWC_SPINUNLOCK(pcd->lock);
52093 + pcd->fops->disconnect(pcd);
52094 + DWC_SPINLOCK(pcd->lock);
52095 + }
52096 + DWC_SPINUNLOCK(pcd->lock);
52097 +}
52098 +
52099 +/**
52100 + * This interrupt indicates that ...
52101 + */
52102 +int32_t dwc_otg_pcd_handle_i2c_intr(dwc_otg_pcd_t * pcd)
52103 +{
52104 + gintmsk_data_t intr_mask = {.d32 = 0 };
52105 + gintsts_data_t gintsts;
52106 +
52107 + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n", "i2cintr");
52108 + intr_mask.b.i2cintr = 1;
52109 + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
52110 + intr_mask.d32, 0);
52111 +
52112 + /* Clear interrupt */
52113 + gintsts.d32 = 0;
52114 + gintsts.b.i2cintr = 1;
52115 + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
52116 + gintsts.d32);
52117 + return 1;
52118 +}
52119 +
52120 +/**
52121 + * This interrupt indicates that ...
52122 + */
52123 +int32_t dwc_otg_pcd_handle_early_suspend_intr(dwc_otg_pcd_t * pcd)
52124 +{
52125 + gintsts_data_t gintsts;
52126 +#if defined(VERBOSE)
52127 + DWC_PRINTF("Early Suspend Detected\n");
52128 +#endif
52129 +
52130 + /* Clear interrupt */
52131 + gintsts.d32 = 0;
52132 + gintsts.b.erlysuspend = 1;
52133 + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
52134 + gintsts.d32);
52135 + return 1;
52136 +}
52137 +
52138 +/**
52139 + * This function configures EPO to receive SETUP packets.
52140 + *
52141 + * @todo NGS: Update the comments from the HW FS.
52142 + *
52143 + * -# Program the following fields in the endpoint specific registers
52144 + * for Control OUT EP 0, in order to receive a setup packet
52145 + * - DOEPTSIZ0.Packet Count = 3 (To receive up to 3 back to back
52146 + * setup packets)
52147 + * - DOEPTSIZE0.Transfer Size = 24 Bytes (To receive up to 3 back
52148 + * to back setup packets)
52149 + * - In DMA mode, DOEPDMA0 Register with a memory address to
52150 + * store any setup packets received
52151 + *
52152 + * @param core_if Programming view of DWC_otg controller.
52153 + * @param pcd Programming view of the PCD.
52154 + */
52155 +static inline void ep0_out_start(dwc_otg_core_if_t * core_if,
52156 + dwc_otg_pcd_t * pcd)
52157 +{
52158 + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
52159 + deptsiz0_data_t doeptsize0 = {.d32 = 0 };
52160 + dwc_otg_dev_dma_desc_t *dma_desc;
52161 + depctl_data_t doepctl = {.d32 = 0 };
52162 +
52163 +#ifdef VERBOSE
52164 + DWC_DEBUGPL(DBG_PCDV, "%s() doepctl0=%0x\n", __func__,
52165 + DWC_READ_REG32(&dev_if->out_ep_regs[0]->doepctl));
52166 +#endif
52167 + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
52168 + doepctl.d32 = DWC_READ_REG32(&dev_if->out_ep_regs[0]->doepctl);
52169 + if (doepctl.b.epena) {
52170 + return;
52171 + }
52172 + }
52173 +
52174 + doeptsize0.b.supcnt = 3;
52175 + doeptsize0.b.pktcnt = 1;
52176 + doeptsize0.b.xfersize = 8 * 3;
52177 +
52178 + if (core_if->dma_enable) {
52179 + if (!core_if->dma_desc_enable) {
52180 + /** put here as for Hermes mode deptisz register should not be written */
52181 + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doeptsiz,
52182 + doeptsize0.d32);
52183 +
52184 + /** @todo dma needs to handle multiple setup packets (up to 3) */
52185 + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doepdma,
52186 + pcd->setup_pkt_dma_handle);
52187 + } else {
52188 + dev_if->setup_desc_index =
52189 + (dev_if->setup_desc_index + 1) & 1;
52190 + dma_desc =
52191 + dev_if->setup_desc_addr[dev_if->setup_desc_index];
52192 +
52193 + /** DMA Descriptor Setup */
52194 + dma_desc->status.b.bs = BS_HOST_BUSY;
52195 + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
52196 + dma_desc->status.b.sr = 0;
52197 + dma_desc->status.b.mtrf = 0;
52198 + }
52199 + dma_desc->status.b.l = 1;
52200 + dma_desc->status.b.ioc = 1;
52201 + dma_desc->status.b.bytes = pcd->ep0.dwc_ep.maxpacket;
52202 + dma_desc->buf = pcd->setup_pkt_dma_handle;
52203 + dma_desc->status.b.sts = 0;
52204 + dma_desc->status.b.bs = BS_HOST_READY;
52205 +
52206 + /** DOEPDMA0 Register write */
52207 + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doepdma,
52208 + dev_if->dma_setup_desc_addr
52209 + [dev_if->setup_desc_index]);
52210 + }
52211 +
52212 + } else {
52213 + /** put here as for Hermes mode deptisz register should not be written */
52214 + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doeptsiz,
52215 + doeptsize0.d32);
52216 + }
52217 +
52218 + /** DOEPCTL0 Register write cnak will be set after setup interrupt */
52219 + doepctl.d32 = 0;
52220 + doepctl.b.epena = 1;
52221 + if (core_if->snpsid <= OTG_CORE_REV_2_94a) {
52222 + doepctl.b.cnak = 1;
52223 + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doepctl, doepctl.d32);
52224 + } else {
52225 + DWC_MODIFY_REG32(&dev_if->out_ep_regs[0]->doepctl, 0, doepctl.d32);
52226 + }
52227 +
52228 +#ifdef VERBOSE
52229 + DWC_DEBUGPL(DBG_PCDV, "doepctl0=%0x\n",
52230 + DWC_READ_REG32(&dev_if->out_ep_regs[0]->doepctl));
52231 + DWC_DEBUGPL(DBG_PCDV, "diepctl0=%0x\n",
52232 + DWC_READ_REG32(&dev_if->in_ep_regs[0]->diepctl));
52233 +#endif
52234 +}
52235 +
52236 +/**
52237 + * This interrupt occurs when a USB Reset is detected. When the USB
52238 + * Reset Interrupt occurs the device state is set to DEFAULT and the
52239 + * EP0 state is set to IDLE.
52240 + * -# Set the NAK bit for all OUT endpoints (DOEPCTLn.SNAK = 1)
52241 + * -# Unmask the following interrupt bits
52242 + * - DAINTMSK.INEP0 = 1 (Control 0 IN endpoint)
52243 + * - DAINTMSK.OUTEP0 = 1 (Control 0 OUT endpoint)
52244 + * - DOEPMSK.SETUP = 1
52245 + * - DOEPMSK.XferCompl = 1
52246 + * - DIEPMSK.XferCompl = 1
52247 + * - DIEPMSK.TimeOut = 1
52248 + * -# Program the following fields in the endpoint specific registers
52249 + * for Control OUT EP 0, in order to receive a setup packet
52250 + * - DOEPTSIZ0.Packet Count = 3 (To receive up to 3 back to back
52251 + * setup packets)
52252 + * - DOEPTSIZE0.Transfer Size = 24 Bytes (To receive up to 3 back
52253 + * to back setup packets)
52254 + * - In DMA mode, DOEPDMA0 Register with a memory address to
52255 + * store any setup packets received
52256 + * At this point, all the required initialization, except for enabling
52257 + * the control 0 OUT endpoint is done, for receiving SETUP packets.
52258 + */
52259 +int32_t dwc_otg_pcd_handle_usb_reset_intr(dwc_otg_pcd_t * pcd)
52260 +{
52261 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
52262 + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
52263 + depctl_data_t doepctl = {.d32 = 0 };
52264 + depctl_data_t diepctl = {.d32 = 0 };
52265 + daint_data_t daintmsk = {.d32 = 0 };
52266 + doepmsk_data_t doepmsk = {.d32 = 0 };
52267 + diepmsk_data_t diepmsk = {.d32 = 0 };
52268 + dcfg_data_t dcfg = {.d32 = 0 };
52269 + grstctl_t resetctl = {.d32 = 0 };
52270 + dctl_data_t dctl = {.d32 = 0 };
52271 + int i = 0;
52272 + gintsts_data_t gintsts;
52273 + pcgcctl_data_t power = {.d32 = 0 };
52274 +
52275 + power.d32 = DWC_READ_REG32(core_if->pcgcctl);
52276 + if (power.b.stoppclk) {
52277 + power.d32 = 0;
52278 + power.b.stoppclk = 1;
52279 + DWC_MODIFY_REG32(core_if->pcgcctl, power.d32, 0);
52280 +
52281 + power.b.pwrclmp = 1;
52282 + DWC_MODIFY_REG32(core_if->pcgcctl, power.d32, 0);
52283 +
52284 + power.b.rstpdwnmodule = 1;
52285 + DWC_MODIFY_REG32(core_if->pcgcctl, power.d32, 0);
52286 + }
52287 +
52288 + core_if->lx_state = DWC_OTG_L0;
52289 +
52290 + DWC_PRINTF("USB RESET\n");
52291 +#ifdef DWC_EN_ISOC
52292 + for (i = 1; i < 16; ++i) {
52293 + dwc_otg_pcd_ep_t *ep;
52294 + dwc_ep_t *dwc_ep;
52295 + ep = get_in_ep(pcd, i);
52296 + if (ep != 0) {
52297 + dwc_ep = &ep->dwc_ep;
52298 + dwc_ep->next_frame = 0xffffffff;
52299 + }
52300 + }
52301 +#endif /* DWC_EN_ISOC */
52302 +
52303 + /* reset the HNP settings */
52304 + dwc_otg_pcd_update_otg(pcd, 1);
52305 +
52306 + /* Clear the Remote Wakeup Signalling */
52307 + dctl.b.rmtwkupsig = 1;
52308 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32, 0);
52309 +
52310 + /* Set NAK for all OUT EPs */
52311 + doepctl.b.snak = 1;
52312 + for (i = 0; i <= dev_if->num_out_eps; i++) {
52313 + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doepctl, doepctl.d32);
52314 + }
52315 +
52316 + /* Flush the NP Tx FIFO */
52317 + dwc_otg_flush_tx_fifo(core_if, 0x10);
52318 + /* Flush the Learning Queue */
52319 + resetctl.b.intknqflsh = 1;
52320 + DWC_WRITE_REG32(&core_if->core_global_regs->grstctl, resetctl.d32);
52321 +
52322 + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable) {
52323 + core_if->start_predict = 0;
52324 + for (i = 0; i<= core_if->dev_if->num_in_eps; ++i) {
52325 + core_if->nextep_seq[i] = 0xff; // 0xff - EP not active
52326 + }
52327 + core_if->nextep_seq[0] = 0;
52328 + core_if->first_in_nextep_seq = 0;
52329 + diepctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[0]->diepctl);
52330 + diepctl.b.nextep = 0;
52331 + DWC_WRITE_REG32(&dev_if->in_ep_regs[0]->diepctl, diepctl.d32);
52332 +
52333 + /* Update IN Endpoint Mismatch Count by active IN NP EP count + 1 */
52334 + dcfg.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dcfg);
52335 + dcfg.b.epmscnt = 2;
52336 + DWC_WRITE_REG32(&dev_if->dev_global_regs->dcfg, dcfg.d32);
52337 +
52338 + DWC_DEBUGPL(DBG_PCDV,
52339 + "%s first_in_nextep_seq= %2d; nextep_seq[]:\n",
52340 + __func__, core_if->first_in_nextep_seq);
52341 + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
52342 + DWC_DEBUGPL(DBG_PCDV, "%2d\n", core_if->nextep_seq[i]);
52343 + }
52344 + }
52345 +
52346 + if (core_if->multiproc_int_enable) {
52347 + daintmsk.b.inep0 = 1;
52348 + daintmsk.b.outep0 = 1;
52349 + DWC_WRITE_REG32(&dev_if->dev_global_regs->deachintmsk,
52350 + daintmsk.d32);
52351 +
52352 + doepmsk.b.setup = 1;
52353 + doepmsk.b.xfercompl = 1;
52354 + doepmsk.b.ahberr = 1;
52355 + doepmsk.b.epdisabled = 1;
52356 +
52357 + if ((core_if->dma_desc_enable) ||
52358 + (core_if->dma_enable
52359 + && core_if->snpsid >= OTG_CORE_REV_3_00a)) {
52360 + doepmsk.b.stsphsercvd = 1;
52361 + }
52362 + if (core_if->dma_desc_enable)
52363 + doepmsk.b.bna = 1;
52364 +/*
52365 + doepmsk.b.babble = 1;
52366 + doepmsk.b.nyet = 1;
52367 +
52368 + if (core_if->dma_enable) {
52369 + doepmsk.b.nak = 1;
52370 + }
52371 +*/
52372 + DWC_WRITE_REG32(&dev_if->dev_global_regs->doepeachintmsk[0],
52373 + doepmsk.d32);
52374 +
52375 + diepmsk.b.xfercompl = 1;
52376 + diepmsk.b.timeout = 1;
52377 + diepmsk.b.epdisabled = 1;
52378 + diepmsk.b.ahberr = 1;
52379 + diepmsk.b.intknepmis = 1;
52380 + if (!core_if->en_multiple_tx_fifo && core_if->dma_enable)
52381 + diepmsk.b.intknepmis = 0;
52382 +
52383 +/* if (core_if->dma_desc_enable) {
52384 + diepmsk.b.bna = 1;
52385 + }
52386 +*/
52387 +/*
52388 + if (core_if->dma_enable) {
52389 + diepmsk.b.nak = 1;
52390 + }
52391 +*/
52392 + DWC_WRITE_REG32(&dev_if->dev_global_regs->diepeachintmsk[0],
52393 + diepmsk.d32);
52394 + } else {
52395 + daintmsk.b.inep0 = 1;
52396 + daintmsk.b.outep0 = 1;
52397 + DWC_WRITE_REG32(&dev_if->dev_global_regs->daintmsk,
52398 + daintmsk.d32);
52399 +
52400 + doepmsk.b.setup = 1;
52401 + doepmsk.b.xfercompl = 1;
52402 + doepmsk.b.ahberr = 1;
52403 + doepmsk.b.epdisabled = 1;
52404 +
52405 + if ((core_if->dma_desc_enable) ||
52406 + (core_if->dma_enable
52407 + && core_if->snpsid >= OTG_CORE_REV_3_00a)) {
52408 + doepmsk.b.stsphsercvd = 1;
52409 + }
52410 + if (core_if->dma_desc_enable)
52411 + doepmsk.b.bna = 1;
52412 + DWC_WRITE_REG32(&dev_if->dev_global_regs->doepmsk, doepmsk.d32);
52413 +
52414 + diepmsk.b.xfercompl = 1;
52415 + diepmsk.b.timeout = 1;
52416 + diepmsk.b.epdisabled = 1;
52417 + diepmsk.b.ahberr = 1;
52418 + if (!core_if->en_multiple_tx_fifo && core_if->dma_enable)
52419 + diepmsk.b.intknepmis = 0;
52420 +/*
52421 + if (core_if->dma_desc_enable) {
52422 + diepmsk.b.bna = 1;
52423 + }
52424 +*/
52425 +
52426 + DWC_WRITE_REG32(&dev_if->dev_global_regs->diepmsk, diepmsk.d32);
52427 + }
52428 +
52429 + /* Reset Device Address */
52430 + dcfg.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dcfg);
52431 + dcfg.b.devaddr = 0;
52432 + DWC_WRITE_REG32(&dev_if->dev_global_regs->dcfg, dcfg.d32);
52433 +
52434 + /* setup EP0 to receive SETUP packets */
52435 + if (core_if->snpsid <= OTG_CORE_REV_2_94a)
52436 + ep0_out_start(core_if, pcd);
52437 +
52438 + /* Clear interrupt */
52439 + gintsts.d32 = 0;
52440 + gintsts.b.usbreset = 1;
52441 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
52442 +
52443 + return 1;
52444 +}
52445 +
52446 +/**
52447 + * Get the device speed from the device status register and convert it
52448 + * to USB speed constant.
52449 + *
52450 + * @param core_if Programming view of DWC_otg controller.
52451 + */
52452 +static int get_device_speed(dwc_otg_core_if_t * core_if)
52453 +{
52454 + dsts_data_t dsts;
52455 + int speed = 0;
52456 + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
52457 +
52458 + switch (dsts.b.enumspd) {
52459 + case DWC_DSTS_ENUMSPD_HS_PHY_30MHZ_OR_60MHZ:
52460 + speed = USB_SPEED_HIGH;
52461 + break;
52462 + case DWC_DSTS_ENUMSPD_FS_PHY_30MHZ_OR_60MHZ:
52463 + case DWC_DSTS_ENUMSPD_FS_PHY_48MHZ:
52464 + speed = USB_SPEED_FULL;
52465 + break;
52466 +
52467 + case DWC_DSTS_ENUMSPD_LS_PHY_6MHZ:
52468 + speed = USB_SPEED_LOW;
52469 + break;
52470 + }
52471 +
52472 + return speed;
52473 +}
52474 +
52475 +/**
52476 + * Read the device status register and set the device speed in the
52477 + * data structure.
52478 + * Set up EP0 to receive SETUP packets by calling dwc_ep0_activate.
52479 + */
52480 +int32_t dwc_otg_pcd_handle_enum_done_intr(dwc_otg_pcd_t * pcd)
52481 +{
52482 + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
52483 + gintsts_data_t gintsts;
52484 + gusbcfg_data_t gusbcfg;
52485 + dwc_otg_core_global_regs_t *global_regs =
52486 + GET_CORE_IF(pcd)->core_global_regs;
52487 + uint8_t utmi16b, utmi8b;
52488 + int speed;
52489 + DWC_DEBUGPL(DBG_PCD, "SPEED ENUM\n");
52490 +
52491 + if (GET_CORE_IF(pcd)->snpsid >= OTG_CORE_REV_2_60a) {
52492 + utmi16b = 6; //vahrama old value was 6;
52493 + utmi8b = 9;
52494 + } else {
52495 + utmi16b = 4;
52496 + utmi8b = 8;
52497 + }
52498 + dwc_otg_ep0_activate(GET_CORE_IF(pcd), &ep0->dwc_ep);
52499 + if (GET_CORE_IF(pcd)->snpsid >= OTG_CORE_REV_3_00a) {
52500 + ep0_out_start(GET_CORE_IF(pcd), pcd);
52501 + }
52502 +
52503 +#ifdef DEBUG_EP0
52504 + print_ep0_state(pcd);
52505 +#endif
52506 +
52507 + if (pcd->ep0state == EP0_DISCONNECT) {
52508 + pcd->ep0state = EP0_IDLE;
52509 + } else if (pcd->ep0state == EP0_STALL) {
52510 + pcd->ep0state = EP0_IDLE;
52511 + }
52512 +
52513 + pcd->ep0state = EP0_IDLE;
52514 +
52515 + ep0->stopped = 0;
52516 +
52517 + speed = get_device_speed(GET_CORE_IF(pcd));
52518 + pcd->fops->connect(pcd, speed);
52519 +
52520 + /* Set USB turnaround time based on device speed and PHY interface. */
52521 + gusbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
52522 + if (speed == USB_SPEED_HIGH) {
52523 + if (GET_CORE_IF(pcd)->hwcfg2.b.hs_phy_type ==
52524 + DWC_HWCFG2_HS_PHY_TYPE_ULPI) {
52525 + /* ULPI interface */
52526 + gusbcfg.b.usbtrdtim = 9;
52527 + }
52528 + if (GET_CORE_IF(pcd)->hwcfg2.b.hs_phy_type ==
52529 + DWC_HWCFG2_HS_PHY_TYPE_UTMI) {
52530 + /* UTMI+ interface */
52531 + if (GET_CORE_IF(pcd)->hwcfg4.b.utmi_phy_data_width == 0) {
52532 + gusbcfg.b.usbtrdtim = utmi8b;
52533 + } else if (GET_CORE_IF(pcd)->hwcfg4.
52534 + b.utmi_phy_data_width == 1) {
52535 + gusbcfg.b.usbtrdtim = utmi16b;
52536 + } else if (GET_CORE_IF(pcd)->
52537 + core_params->phy_utmi_width == 8) {
52538 + gusbcfg.b.usbtrdtim = utmi8b;
52539 + } else {
52540 + gusbcfg.b.usbtrdtim = utmi16b;
52541 + }
52542 + }
52543 + if (GET_CORE_IF(pcd)->hwcfg2.b.hs_phy_type ==
52544 + DWC_HWCFG2_HS_PHY_TYPE_UTMI_ULPI) {
52545 + /* UTMI+ OR ULPI interface */
52546 + if (gusbcfg.b.ulpi_utmi_sel == 1) {
52547 + /* ULPI interface */
52548 + gusbcfg.b.usbtrdtim = 9;
52549 + } else {
52550 + /* UTMI+ interface */
52551 + if (GET_CORE_IF(pcd)->
52552 + core_params->phy_utmi_width == 16) {
52553 + gusbcfg.b.usbtrdtim = utmi16b;
52554 + } else {
52555 + gusbcfg.b.usbtrdtim = utmi8b;
52556 + }
52557 + }
52558 + }
52559 + } else {
52560 + /* Full or low speed */
52561 + gusbcfg.b.usbtrdtim = 9;
52562 + }
52563 + DWC_WRITE_REG32(&global_regs->gusbcfg, gusbcfg.d32);
52564 +
52565 + /* Clear interrupt */
52566 + gintsts.d32 = 0;
52567 + gintsts.b.enumdone = 1;
52568 + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
52569 + gintsts.d32);
52570 + return 1;
52571 +}
52572 +
52573 +/**
52574 + * This interrupt indicates that the ISO OUT Packet was dropped due to
52575 + * Rx FIFO full or Rx Status Queue Full. If this interrupt occurs
52576 + * read all the data from the Rx FIFO.
52577 + */
52578 +int32_t dwc_otg_pcd_handle_isoc_out_packet_dropped_intr(dwc_otg_pcd_t * pcd)
52579 +{
52580 + gintmsk_data_t intr_mask = {.d32 = 0 };
52581 + gintsts_data_t gintsts;
52582 +
52583 + DWC_WARN("INTERRUPT Handler not implemented for %s\n",
52584 + "ISOC Out Dropped");
52585 +
52586 + intr_mask.b.isooutdrop = 1;
52587 + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
52588 + intr_mask.d32, 0);
52589 +
52590 + /* Clear interrupt */
52591 + gintsts.d32 = 0;
52592 + gintsts.b.isooutdrop = 1;
52593 + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
52594 + gintsts.d32);
52595 +
52596 + return 1;
52597 +}
52598 +
52599 +/**
52600 + * This interrupt indicates the end of the portion of the micro-frame
52601 + * for periodic transactions. If there is a periodic transaction for
52602 + * the next frame, load the packets into the EP periodic Tx FIFO.
52603 + */
52604 +int32_t dwc_otg_pcd_handle_end_periodic_frame_intr(dwc_otg_pcd_t * pcd)
52605 +{
52606 + gintmsk_data_t intr_mask = {.d32 = 0 };
52607 + gintsts_data_t gintsts;
52608 + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n", "EOP");
52609 +
52610 + intr_mask.b.eopframe = 1;
52611 + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
52612 + intr_mask.d32, 0);
52613 +
52614 + /* Clear interrupt */
52615 + gintsts.d32 = 0;
52616 + gintsts.b.eopframe = 1;
52617 + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
52618 + gintsts.d32);
52619 +
52620 + return 1;
52621 +}
52622 +
52623 +/**
52624 + * This interrupt indicates that EP of the packet on the top of the
52625 + * non-periodic Tx FIFO does not match EP of the IN Token received.
52626 + *
52627 + * The "Device IN Token Queue" Registers are read to determine the
52628 + * order the IN Tokens have been received. The non-periodic Tx FIFO
52629 + * is flushed, so it can be reloaded in the order seen in the IN Token
52630 + * Queue.
52631 + */
52632 +int32_t dwc_otg_pcd_handle_ep_mismatch_intr(dwc_otg_pcd_t * pcd)
52633 +{
52634 + gintsts_data_t gintsts;
52635 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
52636 + dctl_data_t dctl;
52637 + gintmsk_data_t intr_mask = {.d32 = 0 };
52638 +
52639 + if (!core_if->en_multiple_tx_fifo && core_if->dma_enable) {
52640 + core_if->start_predict = 1;
52641 +
52642 + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, core_if);
52643 +
52644 + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
52645 + if (!gintsts.b.ginnakeff) {
52646 + /* Disable EP Mismatch interrupt */
52647 + intr_mask.d32 = 0;
52648 + intr_mask.b.epmismatch = 1;
52649 + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, intr_mask.d32, 0);
52650 + /* Enable the Global IN NAK Effective Interrupt */
52651 + intr_mask.d32 = 0;
52652 + intr_mask.b.ginnakeff = 1;
52653 + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, 0, intr_mask.d32);
52654 + /* Set the global non-periodic IN NAK handshake */
52655 + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dctl);
52656 + dctl.b.sgnpinnak = 1;
52657 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32);
52658 + } else {
52659 + DWC_PRINTF("gintsts.b.ginnakeff = 1! dctl.b.sgnpinnak not set\n");
52660 + }
52661 + /* Disabling of all EP's will be done in dwc_otg_pcd_handle_in_nak_effective()
52662 + * handler after Global IN NAK Effective interrupt will be asserted */
52663 + }
52664 + /* Clear interrupt */
52665 + gintsts.d32 = 0;
52666 + gintsts.b.epmismatch = 1;
52667 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
52668 +
52669 + return 1;
52670 +}
52671 +
52672 +/**
52673 + * This interrupt is valid only in DMA mode. This interrupt indicates that the
52674 + * core has stopped fetching data for IN endpoints due to the unavailability of
52675 + * TxFIFO space or Request Queue space. This interrupt is used by the
52676 + * application for an endpoint mismatch algorithm.
52677 + *
52678 + * @param pcd The PCD
52679 + */
52680 +int32_t dwc_otg_pcd_handle_ep_fetsusp_intr(dwc_otg_pcd_t * pcd)
52681 +{
52682 + gintsts_data_t gintsts;
52683 + gintmsk_data_t gintmsk_data;
52684 + dctl_data_t dctl;
52685 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
52686 + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, core_if);
52687 +
52688 + /* Clear the global non-periodic IN NAK handshake */
52689 + dctl.d32 = 0;
52690 + dctl.b.cgnpinnak = 1;
52691 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32, dctl.d32);
52692 +
52693 + /* Mask GINTSTS.FETSUSP interrupt */
52694 + gintmsk_data.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
52695 + gintmsk_data.b.fetsusp = 0;
52696 + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, gintmsk_data.d32);
52697 +
52698 + /* Clear interrupt */
52699 + gintsts.d32 = 0;
52700 + gintsts.b.fetsusp = 1;
52701 + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
52702 +
52703 + return 1;
52704 +}
52705 +/**
52706 + * This funcion stalls EP0.
52707 + */
52708 +static inline void ep0_do_stall(dwc_otg_pcd_t * pcd, const int err_val)
52709 +{
52710 + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
52711 + usb_device_request_t *ctrl = &pcd->setup_pkt->req;
52712 + DWC_WARN("req %02x.%02x protocol STALL; err %d\n",
52713 + ctrl->bmRequestType, ctrl->bRequest, err_val);
52714 +
52715 + ep0->dwc_ep.is_in = 1;
52716 + dwc_otg_ep_set_stall(GET_CORE_IF(pcd), &ep0->dwc_ep);
52717 + pcd->ep0.stopped = 1;
52718 + pcd->ep0state = EP0_IDLE;
52719 + ep0_out_start(GET_CORE_IF(pcd), pcd);
52720 +}
52721 +
52722 +/**
52723 + * This functions delegates the setup command to the gadget driver.
52724 + */
52725 +static inline void do_gadget_setup(dwc_otg_pcd_t * pcd,
52726 + usb_device_request_t * ctrl)
52727 +{
52728 + int ret = 0;
52729 + DWC_SPINUNLOCK(pcd->lock);
52730 + ret = pcd->fops->setup(pcd, (uint8_t *) ctrl);
52731 + DWC_SPINLOCK(pcd->lock);
52732 + if (ret < 0) {
52733 + ep0_do_stall(pcd, ret);
52734 + }
52735 +
52736 + /** @todo This is a g_file_storage gadget driver specific
52737 + * workaround: a DELAYED_STATUS result from the fsg_setup
52738 + * routine will result in the gadget queueing a EP0 IN status
52739 + * phase for a two-stage control transfer. Exactly the same as
52740 + * a SET_CONFIGURATION/SET_INTERFACE except that this is a class
52741 + * specific request. Need a generic way to know when the gadget
52742 + * driver will queue the status phase. Can we assume when we
52743 + * call the gadget driver setup() function that it will always
52744 + * queue and require the following flag? Need to look into
52745 + * this.
52746 + */
52747 +
52748 + if (ret == 256 + 999) {
52749 + pcd->request_config = 1;
52750 + }
52751 +}
52752 +
52753 +#ifdef DWC_UTE_CFI
52754 +/**
52755 + * This functions delegates the CFI setup commands to the gadget driver.
52756 + * This function will return a negative value to indicate a failure.
52757 + */
52758 +static inline int cfi_gadget_setup(dwc_otg_pcd_t * pcd,
52759 + struct cfi_usb_ctrlrequest *ctrl_req)
52760 +{
52761 + int ret = 0;
52762 +
52763 + if (pcd->fops && pcd->fops->cfi_setup) {
52764 + DWC_SPINUNLOCK(pcd->lock);
52765 + ret = pcd->fops->cfi_setup(pcd, ctrl_req);
52766 + DWC_SPINLOCK(pcd->lock);
52767 + if (ret < 0) {
52768 + ep0_do_stall(pcd, ret);
52769 + return ret;
52770 + }
52771 + }
52772 +
52773 + return ret;
52774 +}
52775 +#endif
52776 +
52777 +/**
52778 + * This function starts the Zero-Length Packet for the IN status phase
52779 + * of a 2 stage control transfer.
52780 + */
52781 +static inline void do_setup_in_status_phase(dwc_otg_pcd_t * pcd)
52782 +{
52783 + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
52784 + if (pcd->ep0state == EP0_STALL) {
52785 + return;
52786 + }
52787 +
52788 + pcd->ep0state = EP0_IN_STATUS_PHASE;
52789 +
52790 + /* Prepare for more SETUP Packets */
52791 + DWC_DEBUGPL(DBG_PCD, "EP0 IN ZLP\n");
52792 + if ((GET_CORE_IF(pcd)->snpsid >= OTG_CORE_REV_3_00a)
52793 + && (pcd->core_if->dma_desc_enable)
52794 + && (ep0->dwc_ep.xfer_count < ep0->dwc_ep.total_len)) {
52795 + DWC_DEBUGPL(DBG_PCDV,
52796 + "Data terminated wait next packet in out_desc_addr\n");
52797 + pcd->backup_buf = phys_to_virt(ep0->dwc_ep.dma_addr);
52798 + pcd->data_terminated = 1;
52799 + }
52800 + ep0->dwc_ep.xfer_len = 0;
52801 + ep0->dwc_ep.xfer_count = 0;
52802 + ep0->dwc_ep.is_in = 1;
52803 + ep0->dwc_ep.dma_addr = pcd->setup_pkt_dma_handle;
52804 + dwc_otg_ep0_start_transfer(GET_CORE_IF(pcd), &ep0->dwc_ep);
52805 +
52806 + /* Prepare for more SETUP Packets */
52807 + //ep0_out_start(GET_CORE_IF(pcd), pcd);
52808 +}
52809 +
52810 +/**
52811 + * This function starts the Zero-Length Packet for the OUT status phase
52812 + * of a 2 stage control transfer.
52813 + */
52814 +static inline void do_setup_out_status_phase(dwc_otg_pcd_t * pcd)
52815 +{
52816 + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
52817 + if (pcd->ep0state == EP0_STALL) {
52818 + DWC_DEBUGPL(DBG_PCD, "EP0 STALLED\n");
52819 + return;
52820 + }
52821 + pcd->ep0state = EP0_OUT_STATUS_PHASE;
52822 +
52823 + DWC_DEBUGPL(DBG_PCD, "EP0 OUT ZLP\n");
52824 + ep0->dwc_ep.xfer_len = 0;
52825 + ep0->dwc_ep.xfer_count = 0;
52826 + ep0->dwc_ep.is_in = 0;
52827 + ep0->dwc_ep.dma_addr = pcd->setup_pkt_dma_handle;
52828 + dwc_otg_ep0_start_transfer(GET_CORE_IF(pcd), &ep0->dwc_ep);
52829 +
52830 + /* Prepare for more SETUP Packets */
52831 + if (GET_CORE_IF(pcd)->dma_enable == 0) {
52832 + ep0_out_start(GET_CORE_IF(pcd), pcd);
52833 + }
52834 +}
52835 +
52836 +/**
52837 + * Clear the EP halt (STALL) and if pending requests start the
52838 + * transfer.
52839 + */
52840 +static inline void pcd_clear_halt(dwc_otg_pcd_t * pcd, dwc_otg_pcd_ep_t * ep)
52841 +{
52842 + if (ep->dwc_ep.stall_clear_flag == 0)
52843 + dwc_otg_ep_clear_stall(GET_CORE_IF(pcd), &ep->dwc_ep);
52844 +
52845 + /* Reactive the EP */
52846 + dwc_otg_ep_activate(GET_CORE_IF(pcd), &ep->dwc_ep);
52847 + if (ep->stopped) {
52848 + ep->stopped = 0;
52849 + /* If there is a request in the EP queue start it */
52850 +
52851 + /** @todo FIXME: this causes an EP mismatch in DMA mode.
52852 + * epmismatch not yet implemented. */
52853 +
52854 + /*
52855 + * Above fixme is solved by implmenting a tasklet to call the
52856 + * start_next_request(), outside of interrupt context at some
52857 + * time after the current time, after a clear-halt setup packet.
52858 + * Still need to implement ep mismatch in the future if a gadget
52859 + * ever uses more than one endpoint at once
52860 + */
52861 + ep->queue_sof = 1;
52862 + DWC_TASK_SCHEDULE(pcd->start_xfer_tasklet);
52863 + }
52864 + /* Start Control Status Phase */
52865 + do_setup_in_status_phase(pcd);
52866 +}
52867 +
52868 +/**
52869 + * This function is called when the SET_FEATURE TEST_MODE Setup packet
52870 + * is sent from the host. The Device Control register is written with
52871 + * the Test Mode bits set to the specified Test Mode. This is done as
52872 + * a tasklet so that the "Status" phase of the control transfer
52873 + * completes before transmitting the TEST packets.
52874 + *
52875 + * @todo This has not been tested since the tasklet struct was put
52876 + * into the PCD struct!
52877 + *
52878 + */
52879 +void do_test_mode(void *data)
52880 +{
52881 + dctl_data_t dctl;
52882 + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) data;
52883 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
52884 + int test_mode = pcd->test_mode;
52885 +
52886 +// DWC_WARN("%s() has not been tested since being rewritten!\n", __func__);
52887 +
52888 + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dctl);
52889 + switch (test_mode) {
52890 + case 1: // TEST_J
52891 + dctl.b.tstctl = 1;
52892 + break;
52893 +
52894 + case 2: // TEST_K
52895 + dctl.b.tstctl = 2;
52896 + break;
52897 +
52898 + case 3: // TEST_SE0_NAK
52899 + dctl.b.tstctl = 3;
52900 + break;
52901 +
52902 + case 4: // TEST_PACKET
52903 + dctl.b.tstctl = 4;
52904 + break;
52905 +
52906 + case 5: // TEST_FORCE_ENABLE
52907 + dctl.b.tstctl = 5;
52908 + break;
52909 + }
52910 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32);
52911 +}
52912 +
52913 +/**
52914 + * This function process the GET_STATUS Setup Commands.
52915 + */
52916 +static inline void do_get_status(dwc_otg_pcd_t * pcd)
52917 +{
52918 + usb_device_request_t ctrl = pcd->setup_pkt->req;
52919 + dwc_otg_pcd_ep_t *ep;
52920 + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
52921 + uint16_t *status = pcd->status_buf;
52922 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
52923 +
52924 +#ifdef DEBUG_EP0
52925 + DWC_DEBUGPL(DBG_PCD,
52926 + "GET_STATUS %02x.%02x v%04x i%04x l%04x\n",
52927 + ctrl.bmRequestType, ctrl.bRequest,
52928 + UGETW(ctrl.wValue), UGETW(ctrl.wIndex),
52929 + UGETW(ctrl.wLength));
52930 +#endif
52931 +
52932 + switch (UT_GET_RECIPIENT(ctrl.bmRequestType)) {
52933 + case UT_DEVICE:
52934 + if(UGETW(ctrl.wIndex) == 0xF000) { /* OTG Status selector */
52935 + DWC_PRINTF("wIndex - %d\n", UGETW(ctrl.wIndex));
52936 + DWC_PRINTF("OTG VERSION - %d\n", core_if->otg_ver);
52937 + DWC_PRINTF("OTG CAP - %d, %d\n",
52938 + core_if->core_params->otg_cap,
52939 + DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE);
52940 + if (core_if->otg_ver == 1
52941 + && core_if->core_params->otg_cap ==
52942 + DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE) {
52943 + uint8_t *otgsts = (uint8_t*)pcd->status_buf;
52944 + *otgsts = (core_if->otg_sts & 0x1);
52945 + pcd->ep0_pending = 1;
52946 + ep0->dwc_ep.start_xfer_buff =
52947 + (uint8_t *) otgsts;
52948 + ep0->dwc_ep.xfer_buff = (uint8_t *) otgsts;
52949 + ep0->dwc_ep.dma_addr =
52950 + pcd->status_buf_dma_handle;
52951 + ep0->dwc_ep.xfer_len = 1;
52952 + ep0->dwc_ep.xfer_count = 0;
52953 + ep0->dwc_ep.total_len = ep0->dwc_ep.xfer_len;
52954 + dwc_otg_ep0_start_transfer(GET_CORE_IF(pcd),
52955 + &ep0->dwc_ep);
52956 + return;
52957 + } else {
52958 + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
52959 + return;
52960 + }
52961 + break;
52962 + } else {
52963 + *status = 0x1; /* Self powered */
52964 + *status |= pcd->remote_wakeup_enable << 1;
52965 + break;
52966 + }
52967 + case UT_INTERFACE:
52968 + *status = 0;
52969 + break;
52970 +
52971 + case UT_ENDPOINT:
52972 + ep = get_ep_by_addr(pcd, UGETW(ctrl.wIndex));
52973 + if (ep == 0 || UGETW(ctrl.wLength) > 2) {
52974 + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
52975 + return;
52976 + }
52977 + /** @todo check for EP stall */
52978 + *status = ep->stopped;
52979 + break;
52980 + }
52981 + pcd->ep0_pending = 1;
52982 + ep0->dwc_ep.start_xfer_buff = (uint8_t *) status;
52983 + ep0->dwc_ep.xfer_buff = (uint8_t *) status;
52984 + ep0->dwc_ep.dma_addr = pcd->status_buf_dma_handle;
52985 + ep0->dwc_ep.xfer_len = 2;
52986 + ep0->dwc_ep.xfer_count = 0;
52987 + ep0->dwc_ep.total_len = ep0->dwc_ep.xfer_len;
52988 + dwc_otg_ep0_start_transfer(GET_CORE_IF(pcd), &ep0->dwc_ep);
52989 +}
52990 +
52991 +/**
52992 + * This function process the SET_FEATURE Setup Commands.
52993 + */
52994 +static inline void do_set_feature(dwc_otg_pcd_t * pcd)
52995 +{
52996 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
52997 + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
52998 + usb_device_request_t ctrl = pcd->setup_pkt->req;
52999 + dwc_otg_pcd_ep_t *ep = 0;
53000 + int32_t otg_cap_param = core_if->core_params->otg_cap;
53001 + gotgctl_data_t gotgctl = {.d32 = 0 };
53002 +
53003 + DWC_DEBUGPL(DBG_PCD, "SET_FEATURE:%02x.%02x v%04x i%04x l%04x\n",
53004 + ctrl.bmRequestType, ctrl.bRequest,
53005 + UGETW(ctrl.wValue), UGETW(ctrl.wIndex),
53006 + UGETW(ctrl.wLength));
53007 + DWC_DEBUGPL(DBG_PCD, "otg_cap=%d\n", otg_cap_param);
53008 +
53009 + switch (UT_GET_RECIPIENT(ctrl.bmRequestType)) {
53010 + case UT_DEVICE:
53011 + switch (UGETW(ctrl.wValue)) {
53012 + case UF_DEVICE_REMOTE_WAKEUP:
53013 + pcd->remote_wakeup_enable = 1;
53014 + break;
53015 +
53016 + case UF_TEST_MODE:
53017 + /* Setup the Test Mode tasklet to do the Test
53018 + * Packet generation after the SETUP Status
53019 + * phase has completed. */
53020 +
53021 + /** @todo This has not been tested since the
53022 + * tasklet struct was put into the PCD
53023 + * struct! */
53024 + pcd->test_mode = UGETW(ctrl.wIndex) >> 8;
53025 + DWC_TASK_SCHEDULE(pcd->test_mode_tasklet);
53026 + break;
53027 +
53028 + case UF_DEVICE_B_HNP_ENABLE:
53029 + DWC_DEBUGPL(DBG_PCDV,
53030 + "SET_FEATURE: USB_DEVICE_B_HNP_ENABLE\n");
53031 +
53032 + /* dev may initiate HNP */
53033 + if (otg_cap_param == DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE) {
53034 + pcd->b_hnp_enable = 1;
53035 + dwc_otg_pcd_update_otg(pcd, 0);
53036 + DWC_DEBUGPL(DBG_PCD, "Request B HNP\n");
53037 + /**@todo Is the gotgctl.devhnpen cleared
53038 + * by a USB Reset? */
53039 + gotgctl.b.devhnpen = 1;
53040 + gotgctl.b.hnpreq = 1;
53041 + DWC_WRITE_REG32(&global_regs->gotgctl,
53042 + gotgctl.d32);
53043 + } else {
53044 + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
53045 + return;
53046 + }
53047 + break;
53048 +
53049 + case UF_DEVICE_A_HNP_SUPPORT:
53050 + /* RH port supports HNP */
53051 + DWC_DEBUGPL(DBG_PCDV,
53052 + "SET_FEATURE: USB_DEVICE_A_HNP_SUPPORT\n");
53053 + if (otg_cap_param == DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE) {
53054 + pcd->a_hnp_support = 1;
53055 + dwc_otg_pcd_update_otg(pcd, 0);
53056 + } else {
53057 + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
53058 + return;
53059 + }
53060 + break;
53061 +
53062 + case UF_DEVICE_A_ALT_HNP_SUPPORT:
53063 + /* other RH port does */
53064 + DWC_DEBUGPL(DBG_PCDV,
53065 + "SET_FEATURE: USB_DEVICE_A_ALT_HNP_SUPPORT\n");
53066 + if (otg_cap_param == DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE) {
53067 + pcd->a_alt_hnp_support = 1;
53068 + dwc_otg_pcd_update_otg(pcd, 0);
53069 + } else {
53070 + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
53071 + return;
53072 + }
53073 + break;
53074 +
53075 + default:
53076 + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
53077 + return;
53078 +
53079 + }
53080 + do_setup_in_status_phase(pcd);
53081 + break;
53082 +
53083 + case UT_INTERFACE:
53084 + do_gadget_setup(pcd, &ctrl);
53085 + break;
53086 +
53087 + case UT_ENDPOINT:
53088 + if (UGETW(ctrl.wValue) == UF_ENDPOINT_HALT) {
53089 + ep = get_ep_by_addr(pcd, UGETW(ctrl.wIndex));
53090 + if (ep == 0) {
53091 + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
53092 + return;
53093 + }
53094 + ep->stopped = 1;
53095 + dwc_otg_ep_set_stall(core_if, &ep->dwc_ep);
53096 + }
53097 + do_setup_in_status_phase(pcd);
53098 + break;
53099 + }
53100 +}
53101 +
53102 +/**
53103 + * This function process the CLEAR_FEATURE Setup Commands.
53104 + */
53105 +static inline void do_clear_feature(dwc_otg_pcd_t * pcd)
53106 +{
53107 + usb_device_request_t ctrl = pcd->setup_pkt->req;
53108 + dwc_otg_pcd_ep_t *ep = 0;
53109 +
53110 + DWC_DEBUGPL(DBG_PCD,
53111 + "CLEAR_FEATURE:%02x.%02x v%04x i%04x l%04x\n",
53112 + ctrl.bmRequestType, ctrl.bRequest,
53113 + UGETW(ctrl.wValue), UGETW(ctrl.wIndex),
53114 + UGETW(ctrl.wLength));
53115 +
53116 + switch (UT_GET_RECIPIENT(ctrl.bmRequestType)) {
53117 + case UT_DEVICE:
53118 + switch (UGETW(ctrl.wValue)) {
53119 + case UF_DEVICE_REMOTE_WAKEUP:
53120 + pcd->remote_wakeup_enable = 0;
53121 + break;
53122 +
53123 + case UF_TEST_MODE:
53124 + /** @todo Add CLEAR_FEATURE for TEST modes. */
53125 + break;
53126 +
53127 + default:
53128 + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
53129 + return;
53130 + }
53131 + do_setup_in_status_phase(pcd);
53132 + break;
53133 +
53134 + case UT_ENDPOINT:
53135 + ep = get_ep_by_addr(pcd, UGETW(ctrl.wIndex));
53136 + if (ep == 0) {
53137 + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
53138 + return;
53139 + }
53140 +
53141 + pcd_clear_halt(pcd, ep);
53142 +
53143 + break;
53144 + }
53145 +}
53146 +
53147 +/**
53148 + * This function process the SET_ADDRESS Setup Commands.
53149 + */
53150 +static inline void do_set_address(dwc_otg_pcd_t * pcd)
53151 +{
53152 + dwc_otg_dev_if_t *dev_if = GET_CORE_IF(pcd)->dev_if;
53153 + usb_device_request_t ctrl = pcd->setup_pkt->req;
53154 +
53155 + if (ctrl.bmRequestType == UT_DEVICE) {
53156 + dcfg_data_t dcfg = {.d32 = 0 };
53157 +
53158 +#ifdef DEBUG_EP0
53159 +// DWC_DEBUGPL(DBG_PCDV, "SET_ADDRESS:%d\n", ctrl.wValue);
53160 +#endif
53161 + dcfg.b.devaddr = UGETW(ctrl.wValue);
53162 + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dcfg, 0, dcfg.d32);
53163 + do_setup_in_status_phase(pcd);
53164 + }
53165 +}
53166 +
53167 +/**
53168 + * This function processes SETUP commands. In Linux, the USB Command
53169 + * processing is done in two places - the first being the PCD and the
53170 + * second in the Gadget Driver (for example, the File-Backed Storage
53171 + * Gadget Driver).
53172 + *
53173 + * <table>
53174 + * <tr><td>Command </td><td>Driver </td><td>Description</td></tr>
53175 + *
53176 + * <tr><td>GET_STATUS </td><td>PCD </td><td>Command is processed as
53177 + * defined in chapter 9 of the USB 2.0 Specification chapter 9
53178 + * </td></tr>
53179 + *
53180 + * <tr><td>CLEAR_FEATURE </td><td>PCD </td><td>The Device and Endpoint
53181 + * requests are the ENDPOINT_HALT feature is procesed, all others the
53182 + * interface requests are ignored.</td></tr>
53183 + *
53184 + * <tr><td>SET_FEATURE </td><td>PCD </td><td>The Device and Endpoint
53185 + * requests are processed by the PCD. Interface requests are passed
53186 + * to the Gadget Driver.</td></tr>
53187 + *
53188 + * <tr><td>SET_ADDRESS </td><td>PCD </td><td>Program the DCFG reg,
53189 + * with device address received </td></tr>
53190 + *
53191 + * <tr><td>GET_DESCRIPTOR </td><td>Gadget Driver </td><td>Return the
53192 + * requested descriptor</td></tr>
53193 + *
53194 + * <tr><td>SET_DESCRIPTOR </td><td>Gadget Driver </td><td>Optional -
53195 + * not implemented by any of the existing Gadget Drivers.</td></tr>
53196 + *
53197 + * <tr><td>SET_CONFIGURATION </td><td>Gadget Driver </td><td>Disable
53198 + * all EPs and enable EPs for new configuration.</td></tr>
53199 + *
53200 + * <tr><td>GET_CONFIGURATION </td><td>Gadget Driver </td><td>Return
53201 + * the current configuration</td></tr>
53202 + *
53203 + * <tr><td>SET_INTERFACE </td><td>Gadget Driver </td><td>Disable all
53204 + * EPs and enable EPs for new configuration.</td></tr>
53205 + *
53206 + * <tr><td>GET_INTERFACE </td><td>Gadget Driver </td><td>Return the
53207 + * current interface.</td></tr>
53208 + *
53209 + * <tr><td>SYNC_FRAME </td><td>PCD </td><td>Display debug
53210 + * message.</td></tr>
53211 + * </table>
53212 + *
53213 + * When the SETUP Phase Done interrupt occurs, the PCD SETUP commands are
53214 + * processed by pcd_setup. Calling the Function Driver's setup function from
53215 + * pcd_setup processes the gadget SETUP commands.
53216 + */
53217 +static inline void pcd_setup(dwc_otg_pcd_t * pcd)
53218 +{
53219 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
53220 + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
53221 + usb_device_request_t ctrl = pcd->setup_pkt->req;
53222 + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
53223 +
53224 + deptsiz0_data_t doeptsize0 = {.d32 = 0 };
53225 +
53226 +#ifdef DWC_UTE_CFI
53227 + int retval = 0;
53228 + struct cfi_usb_ctrlrequest cfi_req;
53229 +#endif
53230 +
53231 + doeptsize0.d32 = DWC_READ_REG32(&dev_if->out_ep_regs[0]->doeptsiz);
53232 +
53233 + /** In BDMA more then 1 setup packet is not supported till 3.00a */
53234 + if (core_if->dma_enable && core_if->dma_desc_enable == 0
53235 + && (doeptsize0.b.supcnt < 2)
53236 + && (core_if->snpsid < OTG_CORE_REV_2_94a)) {
53237 + DWC_ERROR
53238 + ("\n\n----------- CANNOT handle > 1 setup packet in DMA mode\n\n");
53239 + }
53240 + if ((core_if->snpsid >= OTG_CORE_REV_3_00a)
53241 + && (core_if->dma_enable == 1) && (core_if->dma_desc_enable == 0)) {
53242 + ctrl =
53243 + (pcd->setup_pkt +
53244 + (3 - doeptsize0.b.supcnt - 1 +
53245 + ep0->dwc_ep.stp_rollover))->req;
53246 + }
53247 +#ifdef DEBUG_EP0
53248 + DWC_DEBUGPL(DBG_PCD, "SETUP %02x.%02x v%04x i%04x l%04x\n",
53249 + ctrl.bmRequestType, ctrl.bRequest,
53250 + UGETW(ctrl.wValue), UGETW(ctrl.wIndex),
53251 + UGETW(ctrl.wLength));
53252 +#endif
53253 +
53254 + /* Clean up the request queue */
53255 + dwc_otg_request_nuke(ep0);
53256 + ep0->stopped = 0;
53257 +
53258 + if (ctrl.bmRequestType & UE_DIR_IN) {
53259 + ep0->dwc_ep.is_in = 1;
53260 + pcd->ep0state = EP0_IN_DATA_PHASE;
53261 + } else {
53262 + ep0->dwc_ep.is_in = 0;
53263 + pcd->ep0state = EP0_OUT_DATA_PHASE;
53264 + }
53265 +
53266 + if (UGETW(ctrl.wLength) == 0) {
53267 + ep0->dwc_ep.is_in = 1;
53268 + pcd->ep0state = EP0_IN_STATUS_PHASE;
53269 + }
53270 +
53271 + if (UT_GET_TYPE(ctrl.bmRequestType) != UT_STANDARD) {
53272 +
53273 +#ifdef DWC_UTE_CFI
53274 + DWC_MEMCPY(&cfi_req, &ctrl, sizeof(usb_device_request_t));
53275 +
53276 + //printk(KERN_ALERT "CFI: req_type=0x%02x; req=0x%02x\n",
53277 + ctrl.bRequestType, ctrl.bRequest);
53278 + if (UT_GET_TYPE(cfi_req.bRequestType) == UT_VENDOR) {
53279 + if (cfi_req.bRequest > 0xB0 && cfi_req.bRequest < 0xBF) {
53280 + retval = cfi_setup(pcd, &cfi_req);
53281 + if (retval < 0) {
53282 + ep0_do_stall(pcd, retval);
53283 + pcd->ep0_pending = 0;
53284 + return;
53285 + }
53286 +
53287 + /* if need gadget setup then call it and check the retval */
53288 + if (pcd->cfi->need_gadget_att) {
53289 + retval =
53290 + cfi_gadget_setup(pcd,
53291 + &pcd->
53292 + cfi->ctrl_req);
53293 + if (retval < 0) {
53294 + pcd->ep0_pending = 0;
53295 + return;
53296 + }
53297 + }
53298 +
53299 + if (pcd->cfi->need_status_in_complete) {
53300 + do_setup_in_status_phase(pcd);
53301 + }
53302 + return;
53303 + }
53304 + }
53305 +#endif
53306 +
53307 + /* handle non-standard (class/vendor) requests in the gadget driver */
53308 + do_gadget_setup(pcd, &ctrl);
53309 + return;
53310 + }
53311 +
53312 + /** @todo NGS: Handle bad setup packet? */
53313 +
53314 +///////////////////////////////////////////
53315 +//// --- Standard Request handling --- ////
53316 +
53317 + switch (ctrl.bRequest) {
53318 + case UR_GET_STATUS:
53319 + do_get_status(pcd);
53320 + break;
53321 +
53322 + case UR_CLEAR_FEATURE:
53323 + do_clear_feature(pcd);
53324 + break;
53325 +
53326 + case UR_SET_FEATURE:
53327 + do_set_feature(pcd);
53328 + break;
53329 +
53330 + case UR_SET_ADDRESS:
53331 + do_set_address(pcd);
53332 + break;
53333 +
53334 + case UR_SET_INTERFACE:
53335 + case UR_SET_CONFIG:
53336 +// _pcd->request_config = 1; /* Configuration changed */
53337 + do_gadget_setup(pcd, &ctrl);
53338 + break;
53339 +
53340 + case UR_SYNCH_FRAME:
53341 + do_gadget_setup(pcd, &ctrl);
53342 + break;
53343 +
53344 + default:
53345 + /* Call the Gadget Driver's setup functions */
53346 + do_gadget_setup(pcd, &ctrl);
53347 + break;
53348 + }
53349 +}
53350 +
53351 +/**
53352 + * This function completes the ep0 control transfer.
53353 + */
53354 +static int32_t ep0_complete_request(dwc_otg_pcd_ep_t * ep)
53355 +{
53356 + dwc_otg_core_if_t *core_if = GET_CORE_IF(ep->pcd);
53357 + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
53358 + dwc_otg_dev_in_ep_regs_t *in_ep_regs =
53359 + dev_if->in_ep_regs[ep->dwc_ep.num];
53360 +#ifdef DEBUG_EP0
53361 + dwc_otg_dev_out_ep_regs_t *out_ep_regs =
53362 + dev_if->out_ep_regs[ep->dwc_ep.num];
53363 +#endif
53364 + deptsiz0_data_t deptsiz;
53365 + dev_dma_desc_sts_t desc_sts;
53366 + dwc_otg_pcd_request_t *req;
53367 + int is_last = 0;
53368 + dwc_otg_pcd_t *pcd = ep->pcd;
53369 +
53370 +#ifdef DWC_UTE_CFI
53371 + struct cfi_usb_ctrlrequest *ctrlreq;
53372 + int retval = -DWC_E_NOT_SUPPORTED;
53373 +#endif
53374 +
53375 + desc_sts.b.bytes = 0;
53376 +
53377 + if (pcd->ep0_pending && DWC_CIRCLEQ_EMPTY(&ep->queue)) {
53378 + if (ep->dwc_ep.is_in) {
53379 +#ifdef DEBUG_EP0
53380 + DWC_DEBUGPL(DBG_PCDV, "Do setup OUT status phase\n");
53381 +#endif
53382 + do_setup_out_status_phase(pcd);
53383 + } else {
53384 +#ifdef DEBUG_EP0
53385 + DWC_DEBUGPL(DBG_PCDV, "Do setup IN status phase\n");
53386 +#endif
53387 +
53388 +#ifdef DWC_UTE_CFI
53389 + ctrlreq = &pcd->cfi->ctrl_req;
53390 +
53391 + if (UT_GET_TYPE(ctrlreq->bRequestType) == UT_VENDOR) {
53392 + if (ctrlreq->bRequest > 0xB0
53393 + && ctrlreq->bRequest < 0xBF) {
53394 +
53395 + /* Return if the PCD failed to handle the request */
53396 + if ((retval =
53397 + pcd->cfi->ops.
53398 + ctrl_write_complete(pcd->cfi,
53399 + pcd)) < 0) {
53400 + CFI_INFO
53401 + ("ERROR setting a new value in the PCD(%d)\n",
53402 + retval);
53403 + ep0_do_stall(pcd, retval);
53404 + pcd->ep0_pending = 0;
53405 + return 0;
53406 + }
53407 +
53408 + /* If the gadget needs to be notified on the request */
53409 + if (pcd->cfi->need_gadget_att == 1) {
53410 + //retval = do_gadget_setup(pcd, &pcd->cfi->ctrl_req);
53411 + retval =
53412 + cfi_gadget_setup(pcd,
53413 + &pcd->cfi->
53414 + ctrl_req);
53415 +
53416 + /* Return from the function if the gadget failed to process
53417 + * the request properly - this should never happen !!!
53418 + */
53419 + if (retval < 0) {
53420 + CFI_INFO
53421 + ("ERROR setting a new value in the gadget(%d)\n",
53422 + retval);
53423 + pcd->ep0_pending = 0;
53424 + return 0;
53425 + }
53426 + }
53427 +
53428 + CFI_INFO("%s: RETVAL=%d\n", __func__,
53429 + retval);
53430 + /* If we hit here then the PCD and the gadget has properly
53431 + * handled the request - so send the ZLP IN to the host.
53432 + */
53433 + /* @todo: MAS - decide whether we need to start the setup
53434 + * stage based on the need_setup value of the cfi object
53435 + */
53436 + do_setup_in_status_phase(pcd);
53437 + pcd->ep0_pending = 0;
53438 + return 1;
53439 + }
53440 + }
53441 +#endif
53442 +
53443 + do_setup_in_status_phase(pcd);
53444 + }
53445 + pcd->ep0_pending = 0;
53446 + return 1;
53447 + }
53448 +
53449 + if (DWC_CIRCLEQ_EMPTY(&ep->queue)) {
53450 + return 0;
53451 + }
53452 + req = DWC_CIRCLEQ_FIRST(&ep->queue);
53453 +
53454 + if (pcd->ep0state == EP0_OUT_STATUS_PHASE
53455 + || pcd->ep0state == EP0_IN_STATUS_PHASE) {
53456 + is_last = 1;
53457 + } else if (ep->dwc_ep.is_in) {
53458 + deptsiz.d32 = DWC_READ_REG32(&in_ep_regs->dieptsiz);
53459 + if (core_if->dma_desc_enable != 0)
53460 + desc_sts = dev_if->in_desc_addr->status;
53461 +#ifdef DEBUG_EP0
53462 + DWC_DEBUGPL(DBG_PCDV, "%d len=%d xfersize=%d pktcnt=%d\n",
53463 + ep->dwc_ep.num, ep->dwc_ep.xfer_len,
53464 + deptsiz.b.xfersize, deptsiz.b.pktcnt);
53465 +#endif
53466 +
53467 + if (((core_if->dma_desc_enable == 0)
53468 + && (deptsiz.b.xfersize == 0))
53469 + || ((core_if->dma_desc_enable != 0)
53470 + && (desc_sts.b.bytes == 0))) {
53471 + req->actual = ep->dwc_ep.xfer_count;
53472 + /* Is a Zero Len Packet needed? */
53473 + if (req->sent_zlp) {
53474 +#ifdef DEBUG_EP0
53475 + DWC_DEBUGPL(DBG_PCD, "Setup Rx ZLP\n");
53476 +#endif
53477 + req->sent_zlp = 0;
53478 + }
53479 + do_setup_out_status_phase(pcd);
53480 + }
53481 + } else {
53482 + /* ep0-OUT */
53483 +#ifdef DEBUG_EP0
53484 + deptsiz.d32 = DWC_READ_REG32(&out_ep_regs->doeptsiz);
53485 + DWC_DEBUGPL(DBG_PCDV, "%d len=%d xsize=%d pktcnt=%d\n",
53486 + ep->dwc_ep.num, ep->dwc_ep.xfer_len,
53487 + deptsiz.b.xfersize, deptsiz.b.pktcnt);
53488 +#endif
53489 + req->actual = ep->dwc_ep.xfer_count;
53490 +
53491 + /* Is a Zero Len Packet needed? */
53492 + if (req->sent_zlp) {
53493 +#ifdef DEBUG_EP0
53494 + DWC_DEBUGPL(DBG_PCDV, "Setup Tx ZLP\n");
53495 +#endif
53496 + req->sent_zlp = 0;
53497 + }
53498 + /* For older cores do setup in status phase in Slave/BDMA modes,
53499 + * starting from 3.00 do that only in slave, and for DMA modes
53500 + * just re-enable ep 0 OUT here*/
53501 + if (core_if->dma_enable == 0
53502 + || (core_if->dma_desc_enable == 0
53503 + && core_if->snpsid <= OTG_CORE_REV_2_94a)) {
53504 + do_setup_in_status_phase(pcd);
53505 + } else if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
53506 + DWC_DEBUGPL(DBG_PCDV,
53507 + "Enable out ep before in status phase\n");
53508 + ep0_out_start(core_if, pcd);
53509 + }
53510 + }
53511 +
53512 + /* Complete the request */
53513 + if (is_last) {
53514 + dwc_otg_request_done(ep, req, 0);
53515 + ep->dwc_ep.start_xfer_buff = 0;
53516 + ep->dwc_ep.xfer_buff = 0;
53517 + ep->dwc_ep.xfer_len = 0;
53518 + return 1;
53519 + }
53520 + return 0;
53521 +}
53522 +
53523 +#ifdef DWC_UTE_CFI
53524 +/**
53525 + * This function calculates traverses all the CFI DMA descriptors and
53526 + * and accumulates the bytes that are left to be transfered.
53527 + *
53528 + * @return The total bytes left to transfered, or a negative value as failure
53529 + */
53530 +static inline int cfi_calc_desc_residue(dwc_otg_pcd_ep_t * ep)
53531 +{
53532 + int32_t ret = 0;
53533 + int i;
53534 + struct dwc_otg_dma_desc *ddesc = NULL;
53535 + struct cfi_ep *cfiep;
53536 +
53537 + /* See if the pcd_ep has its respective cfi_ep mapped */
53538 + cfiep = get_cfi_ep_by_pcd_ep(ep->pcd->cfi, ep);
53539 + if (!cfiep) {
53540 + CFI_INFO("%s: Failed to find ep\n", __func__);
53541 + return -1;
53542 + }
53543 +
53544 + ddesc = ep->dwc_ep.descs;
53545 +
53546 + for (i = 0; (i < cfiep->desc_count) && (i < MAX_DMA_DESCS_PER_EP); i++) {
53547 +
53548 +#if defined(PRINT_CFI_DMA_DESCS)
53549 + print_desc(ddesc, ep->ep.name, i);
53550 +#endif
53551 + ret += ddesc->status.b.bytes;
53552 + ddesc++;
53553 + }
53554 +
53555 + if (ret)
53556 + CFI_INFO("!!!!!!!!!! WARNING (%s) - residue=%d\n", __func__,
53557 + ret);
53558 +
53559 + return ret;
53560 +}
53561 +#endif
53562 +
53563 +/**
53564 + * This function completes the request for the EP. If there are
53565 + * additional requests for the EP in the queue they will be started.
53566 + */
53567 +static void complete_ep(dwc_otg_pcd_ep_t * ep)
53568 +{
53569 + dwc_otg_core_if_t *core_if = GET_CORE_IF(ep->pcd);
53570 + struct device *dev = dwc_otg_pcd_to_dev(ep->pcd);
53571 + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
53572 + dwc_otg_dev_in_ep_regs_t *in_ep_regs =
53573 + dev_if->in_ep_regs[ep->dwc_ep.num];
53574 + deptsiz_data_t deptsiz;
53575 + dev_dma_desc_sts_t desc_sts;
53576 + dwc_otg_pcd_request_t *req = 0;
53577 + dwc_otg_dev_dma_desc_t *dma_desc;
53578 + uint32_t byte_count = 0;
53579 + int is_last = 0;
53580 + int i;
53581 +
53582 + DWC_DEBUGPL(DBG_PCDV, "%s() %d-%s\n", __func__, ep->dwc_ep.num,
53583 + (ep->dwc_ep.is_in ? "IN" : "OUT"));
53584 +
53585 + /* Get any pending requests */
53586 + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
53587 + req = DWC_CIRCLEQ_FIRST(&ep->queue);
53588 + if (!req) {
53589 + DWC_PRINTF("complete_ep 0x%p, req = NULL!\n", ep);
53590 + return;
53591 + }
53592 + } else {
53593 + DWC_PRINTF("complete_ep 0x%p, ep->queue empty!\n", ep);
53594 + return;
53595 + }
53596 +
53597 + DWC_DEBUGPL(DBG_PCD, "Requests %d\n", ep->pcd->request_pending);
53598 +
53599 + if (ep->dwc_ep.is_in) {
53600 + deptsiz.d32 = DWC_READ_REG32(&in_ep_regs->dieptsiz);
53601 +
53602 + if (core_if->dma_enable) {
53603 + if (core_if->dma_desc_enable == 0) {
53604 + if (deptsiz.b.xfersize == 0
53605 + && deptsiz.b.pktcnt == 0) {
53606 + byte_count =
53607 + ep->dwc_ep.xfer_len -
53608 + ep->dwc_ep.xfer_count;
53609 +
53610 + ep->dwc_ep.xfer_buff += byte_count;
53611 + ep->dwc_ep.dma_addr += byte_count;
53612 + ep->dwc_ep.xfer_count += byte_count;
53613 +
53614 + DWC_DEBUGPL(DBG_PCDV,
53615 + "%d-%s len=%d xfersize=%d pktcnt=%d\n",
53616 + ep->dwc_ep.num,
53617 + (ep->dwc_ep.
53618 + is_in ? "IN" : "OUT"),
53619 + ep->dwc_ep.xfer_len,
53620 + deptsiz.b.xfersize,
53621 + deptsiz.b.pktcnt);
53622 +
53623 + if (ep->dwc_ep.xfer_len <
53624 + ep->dwc_ep.total_len) {
53625 + dwc_otg_ep_start_transfer
53626 + (core_if, &ep->dwc_ep);
53627 + } else if (ep->dwc_ep.sent_zlp) {
53628 + /*
53629 + * This fragment of code should initiate 0
53630 + * length transfer in case if it is queued
53631 + * a transfer with size divisible to EPs max
53632 + * packet size and with usb_request zero field
53633 + * is set, which means that after data is transfered,
53634 + * it is also should be transfered
53635 + * a 0 length packet at the end. For Slave and
53636 + * Buffer DMA modes in this case SW has
53637 + * to initiate 2 transfers one with transfer size,
53638 + * and the second with 0 size. For Descriptor
53639 + * DMA mode SW is able to initiate a transfer,
53640 + * which will handle all the packets including
53641 + * the last 0 length.
53642 + */
53643 + ep->dwc_ep.sent_zlp = 0;
53644 + dwc_otg_ep_start_zl_transfer
53645 + (core_if, &ep->dwc_ep);
53646 + } else {
53647 + is_last = 1;
53648 + }
53649 + } else {
53650 + if (ep->dwc_ep.type ==
53651 + DWC_OTG_EP_TYPE_ISOC) {
53652 + req->actual = 0;
53653 + dwc_otg_request_done(ep, req, 0);
53654 +
53655 + ep->dwc_ep.start_xfer_buff = 0;
53656 + ep->dwc_ep.xfer_buff = 0;
53657 + ep->dwc_ep.xfer_len = 0;
53658 +
53659 + /* If there is a request in the queue start it. */
53660 + start_next_request(ep);
53661 + } else
53662 + DWC_WARN
53663 + ("Incomplete transfer (%d - %s [siz=%d pkt=%d])\n",
53664 + ep->dwc_ep.num,
53665 + (ep->dwc_ep.is_in ? "IN" : "OUT"),
53666 + deptsiz.b.xfersize,
53667 + deptsiz.b.pktcnt);
53668 + }
53669 + } else {
53670 + dma_desc = ep->dwc_ep.desc_addr;
53671 + byte_count = 0;
53672 + ep->dwc_ep.sent_zlp = 0;
53673 +
53674 +#ifdef DWC_UTE_CFI
53675 + CFI_INFO("%s: BUFFER_MODE=%d\n", __func__,
53676 + ep->dwc_ep.buff_mode);
53677 + if (ep->dwc_ep.buff_mode != BM_STANDARD) {
53678 + int residue;
53679 +
53680 + residue = cfi_calc_desc_residue(ep);
53681 + if (residue < 0)
53682 + return;
53683 +
53684 + byte_count = residue;
53685 + } else {
53686 +#endif
53687 + for (i = 0; i < ep->dwc_ep.desc_cnt;
53688 + ++i) {
53689 + desc_sts = dma_desc->status;
53690 + byte_count += desc_sts.b.bytes;
53691 + dma_desc++;
53692 + }
53693 +#ifdef DWC_UTE_CFI
53694 + }
53695 +#endif
53696 + if (byte_count == 0) {
53697 + ep->dwc_ep.xfer_count =
53698 + ep->dwc_ep.total_len;
53699 + is_last = 1;
53700 + } else {
53701 + DWC_WARN("Incomplete transfer\n");
53702 + }
53703 + }
53704 + } else {
53705 + if (deptsiz.b.xfersize == 0 && deptsiz.b.pktcnt == 0) {
53706 + DWC_DEBUGPL(DBG_PCDV,
53707 + "%d-%s len=%d xfersize=%d pktcnt=%d\n",
53708 + ep->dwc_ep.num,
53709 + ep->dwc_ep.is_in ? "IN" : "OUT",
53710 + ep->dwc_ep.xfer_len,
53711 + deptsiz.b.xfersize,
53712 + deptsiz.b.pktcnt);
53713 +
53714 + /* Check if the whole transfer was completed,
53715 + * if no, setup transfer for next portion of data
53716 + */
53717 + if (ep->dwc_ep.xfer_len < ep->dwc_ep.total_len) {
53718 + dwc_otg_ep_start_transfer(core_if,
53719 + &ep->dwc_ep);
53720 + } else if (ep->dwc_ep.sent_zlp) {
53721 + /*
53722 + * This fragment of code should initiate 0
53723 + * length trasfer in case if it is queued
53724 + * a trasfer with size divisible to EPs max
53725 + * packet size and with usb_request zero field
53726 + * is set, which means that after data is transfered,
53727 + * it is also should be transfered
53728 + * a 0 length packet at the end. For Slave and
53729 + * Buffer DMA modes in this case SW has
53730 + * to initiate 2 transfers one with transfer size,
53731 + * and the second with 0 size. For Desriptor
53732 + * DMA mode SW is able to initiate a transfer,
53733 + * which will handle all the packets including
53734 + * the last 0 legth.
53735 + */
53736 + ep->dwc_ep.sent_zlp = 0;
53737 + dwc_otg_ep_start_zl_transfer(core_if,
53738 + &ep->dwc_ep);
53739 + } else {
53740 + is_last = 1;
53741 + }
53742 + } else {
53743 + DWC_WARN
53744 + ("Incomplete transfer (%d-%s [siz=%d pkt=%d])\n",
53745 + ep->dwc_ep.num,
53746 + (ep->dwc_ep.is_in ? "IN" : "OUT"),
53747 + deptsiz.b.xfersize, deptsiz.b.pktcnt);
53748 + }
53749 + }
53750 + } else {
53751 + dwc_otg_dev_out_ep_regs_t *out_ep_regs =
53752 + dev_if->out_ep_regs[ep->dwc_ep.num];
53753 + desc_sts.d32 = 0;
53754 + if (core_if->dma_enable) {
53755 + if (core_if->dma_desc_enable) {
53756 + dma_desc = ep->dwc_ep.desc_addr;
53757 + byte_count = 0;
53758 + ep->dwc_ep.sent_zlp = 0;
53759 +
53760 +#ifdef DWC_UTE_CFI
53761 + CFI_INFO("%s: BUFFER_MODE=%d\n", __func__,
53762 + ep->dwc_ep.buff_mode);
53763 + if (ep->dwc_ep.buff_mode != BM_STANDARD) {
53764 + int residue;
53765 + residue = cfi_calc_desc_residue(ep);
53766 + if (residue < 0)
53767 + return;
53768 + byte_count = residue;
53769 + } else {
53770 +#endif
53771 +
53772 + for (i = 0; i < ep->dwc_ep.desc_cnt;
53773 + ++i) {
53774 + desc_sts = dma_desc->status;
53775 + byte_count += desc_sts.b.bytes;
53776 + dma_desc++;
53777 + }
53778 +
53779 +#ifdef DWC_UTE_CFI
53780 + }
53781 +#endif
53782 + /* Checking for interrupt Out transfers with not
53783 + * dword aligned mps sizes
53784 + */
53785 + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_INTR &&
53786 + (ep->dwc_ep.maxpacket%4)) {
53787 + ep->dwc_ep.xfer_count =
53788 + ep->dwc_ep.total_len - byte_count;
53789 + if ((ep->dwc_ep.xfer_len %
53790 + ep->dwc_ep.maxpacket)
53791 + && (ep->dwc_ep.xfer_len /
53792 + ep->dwc_ep.maxpacket <
53793 + MAX_DMA_DESC_CNT))
53794 + ep->dwc_ep.xfer_len -=
53795 + (ep->dwc_ep.desc_cnt -
53796 + 1) * ep->dwc_ep.maxpacket +
53797 + ep->dwc_ep.xfer_len %
53798 + ep->dwc_ep.maxpacket;
53799 + else
53800 + ep->dwc_ep.xfer_len -=
53801 + ep->dwc_ep.desc_cnt *
53802 + ep->dwc_ep.maxpacket;
53803 + if (ep->dwc_ep.xfer_len > 0) {
53804 + dwc_otg_ep_start_transfer
53805 + (core_if, &ep->dwc_ep);
53806 + } else {
53807 + is_last = 1;
53808 + }
53809 + } else {
53810 + ep->dwc_ep.xfer_count =
53811 + ep->dwc_ep.total_len - byte_count +
53812 + ((4 -
53813 + (ep->dwc_ep.
53814 + total_len & 0x3)) & 0x3);
53815 + is_last = 1;
53816 + }
53817 + } else {
53818 + deptsiz.d32 = 0;
53819 + deptsiz.d32 =
53820 + DWC_READ_REG32(&out_ep_regs->doeptsiz);
53821 +
53822 + byte_count = (ep->dwc_ep.xfer_len -
53823 + ep->dwc_ep.xfer_count -
53824 + deptsiz.b.xfersize);
53825 + ep->dwc_ep.xfer_buff += byte_count;
53826 + ep->dwc_ep.dma_addr += byte_count;
53827 + ep->dwc_ep.xfer_count += byte_count;
53828 +
53829 + /* Check if the whole transfer was completed,
53830 + * if no, setup transfer for next portion of data
53831 + */
53832 + if (ep->dwc_ep.xfer_len < ep->dwc_ep.total_len) {
53833 + dwc_otg_ep_start_transfer(core_if,
53834 + &ep->dwc_ep);
53835 + } else if (ep->dwc_ep.sent_zlp) {
53836 + /*
53837 + * This fragment of code should initiate 0
53838 + * length trasfer in case if it is queued
53839 + * a trasfer with size divisible to EPs max
53840 + * packet size and with usb_request zero field
53841 + * is set, which means that after data is transfered,
53842 + * it is also should be transfered
53843 + * a 0 length packet at the end. For Slave and
53844 + * Buffer DMA modes in this case SW has
53845 + * to initiate 2 transfers one with transfer size,
53846 + * and the second with 0 size. For Desriptor
53847 + * DMA mode SW is able to initiate a transfer,
53848 + * which will handle all the packets including
53849 + * the last 0 legth.
53850 + */
53851 + ep->dwc_ep.sent_zlp = 0;
53852 + dwc_otg_ep_start_zl_transfer(core_if,
53853 + &ep->dwc_ep);
53854 + } else {
53855 + is_last = 1;
53856 + }
53857 + }
53858 + } else {
53859 + /* Check if the whole transfer was completed,
53860 + * if no, setup transfer for next portion of data
53861 + */
53862 + if (ep->dwc_ep.xfer_len < ep->dwc_ep.total_len) {
53863 + dwc_otg_ep_start_transfer(core_if, &ep->dwc_ep);
53864 + } else if (ep->dwc_ep.sent_zlp) {
53865 + /*
53866 + * This fragment of code should initiate 0
53867 + * length transfer in case if it is queued
53868 + * a transfer with size divisible to EPs max
53869 + * packet size and with usb_request zero field
53870 + * is set, which means that after data is transfered,
53871 + * it is also should be transfered
53872 + * a 0 length packet at the end. For Slave and
53873 + * Buffer DMA modes in this case SW has
53874 + * to initiate 2 transfers one with transfer size,
53875 + * and the second with 0 size. For Descriptor
53876 + * DMA mode SW is able to initiate a transfer,
53877 + * which will handle all the packets including
53878 + * the last 0 length.
53879 + */
53880 + ep->dwc_ep.sent_zlp = 0;
53881 + dwc_otg_ep_start_zl_transfer(core_if,
53882 + &ep->dwc_ep);
53883 + } else {
53884 + is_last = 1;
53885 + }
53886 + }
53887 +
53888 + DWC_DEBUGPL(DBG_PCDV,
53889 + "addr %p, %d-%s len=%d cnt=%d xsize=%d pktcnt=%d\n",
53890 + &out_ep_regs->doeptsiz, ep->dwc_ep.num,
53891 + ep->dwc_ep.is_in ? "IN" : "OUT",
53892 + ep->dwc_ep.xfer_len, ep->dwc_ep.xfer_count,
53893 + deptsiz.b.xfersize, deptsiz.b.pktcnt);
53894 + }
53895 +
53896 + /* Complete the request */
53897 + if (is_last) {
53898 +#ifdef DWC_UTE_CFI
53899 + if (ep->dwc_ep.buff_mode != BM_STANDARD) {
53900 + req->actual = ep->dwc_ep.cfi_req_len - byte_count;
53901 + } else {
53902 +#endif
53903 + req->actual = ep->dwc_ep.xfer_count;
53904 +#ifdef DWC_UTE_CFI
53905 + }
53906 +#endif
53907 + if (req->dw_align_buf) {
53908 + if (!ep->dwc_ep.is_in) {
53909 + dwc_memcpy(req->buf, req->dw_align_buf, req->length);
53910 + }
53911 + DWC_DMA_FREE(dev, req->length, req->dw_align_buf,
53912 + req->dw_align_buf_dma);
53913 + }
53914 +
53915 + dwc_otg_request_done(ep, req, 0);
53916 +
53917 + ep->dwc_ep.start_xfer_buff = 0;
53918 + ep->dwc_ep.xfer_buff = 0;
53919 + ep->dwc_ep.xfer_len = 0;
53920 +
53921 + /* If there is a request in the queue start it. */
53922 + start_next_request(ep);
53923 + }
53924 +}
53925 +
53926 +#ifdef DWC_EN_ISOC
53927 +
53928 +/**
53929 + * This function BNA interrupt for Isochronous EPs
53930 + *
53931 + */
53932 +static void dwc_otg_pcd_handle_iso_bna(dwc_otg_pcd_ep_t * ep)
53933 +{
53934 + dwc_ep_t *dwc_ep = &ep->dwc_ep;
53935 + volatile uint32_t *addr;
53936 + depctl_data_t depctl = {.d32 = 0 };
53937 + dwc_otg_pcd_t *pcd = ep->pcd;
53938 + dwc_otg_dev_dma_desc_t *dma_desc;
53939 + int i;
53940 +
53941 + dma_desc =
53942 + dwc_ep->iso_desc_addr + dwc_ep->desc_cnt * (dwc_ep->proc_buf_num);
53943 +
53944 + if (dwc_ep->is_in) {
53945 + dev_dma_desc_sts_t sts = {.d32 = 0 };
53946 + for (i = 0; i < dwc_ep->desc_cnt; ++i, ++dma_desc) {
53947 + sts.d32 = dma_desc->status.d32;
53948 + sts.b_iso_in.bs = BS_HOST_READY;
53949 + dma_desc->status.d32 = sts.d32;
53950 + }
53951 + } else {
53952 + dev_dma_desc_sts_t sts = {.d32 = 0 };
53953 + for (i = 0; i < dwc_ep->desc_cnt; ++i, ++dma_desc) {
53954 + sts.d32 = dma_desc->status.d32;
53955 + sts.b_iso_out.bs = BS_HOST_READY;
53956 + dma_desc->status.d32 = sts.d32;
53957 + }
53958 + }
53959 +
53960 + if (dwc_ep->is_in == 0) {
53961 + addr =
53962 + &GET_CORE_IF(pcd)->dev_if->out_ep_regs[dwc_ep->
53963 + num]->doepctl;
53964 + } else {
53965 + addr =
53966 + &GET_CORE_IF(pcd)->dev_if->in_ep_regs[dwc_ep->num]->diepctl;
53967 + }
53968 + depctl.b.epena = 1;
53969 + DWC_MODIFY_REG32(addr, depctl.d32, depctl.d32);
53970 +}
53971 +
53972 +/**
53973 + * This function sets latest iso packet information(non-PTI mode)
53974 + *
53975 + * @param core_if Programming view of DWC_otg controller.
53976 + * @param ep The EP to start the transfer on.
53977 + *
53978 + */
53979 +void set_current_pkt_info(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
53980 +{
53981 + deptsiz_data_t deptsiz = {.d32 = 0 };
53982 + dma_addr_t dma_addr;
53983 + uint32_t offset;
53984 +
53985 + if (ep->proc_buf_num)
53986 + dma_addr = ep->dma_addr1;
53987 + else
53988 + dma_addr = ep->dma_addr0;
53989 +
53990 + if (ep->is_in) {
53991 + deptsiz.d32 =
53992 + DWC_READ_REG32(&core_if->dev_if->
53993 + in_ep_regs[ep->num]->dieptsiz);
53994 + offset = ep->data_per_frame;
53995 + } else {
53996 + deptsiz.d32 =
53997 + DWC_READ_REG32(&core_if->dev_if->
53998 + out_ep_regs[ep->num]->doeptsiz);
53999 + offset =
54000 + ep->data_per_frame +
54001 + (0x4 & (0x4 - (ep->data_per_frame & 0x3)));
54002 + }
54003 +
54004 + if (!deptsiz.b.xfersize) {
54005 + ep->pkt_info[ep->cur_pkt].length = ep->data_per_frame;
54006 + ep->pkt_info[ep->cur_pkt].offset =
54007 + ep->cur_pkt_dma_addr - dma_addr;
54008 + ep->pkt_info[ep->cur_pkt].status = 0;
54009 + } else {
54010 + ep->pkt_info[ep->cur_pkt].length = ep->data_per_frame;
54011 + ep->pkt_info[ep->cur_pkt].offset =
54012 + ep->cur_pkt_dma_addr - dma_addr;
54013 + ep->pkt_info[ep->cur_pkt].status = -DWC_E_NO_DATA;
54014 + }
54015 + ep->cur_pkt_addr += offset;
54016 + ep->cur_pkt_dma_addr += offset;
54017 + ep->cur_pkt++;
54018 +}
54019 +
54020 +/**
54021 + * This function sets latest iso packet information(DDMA mode)
54022 + *
54023 + * @param core_if Programming view of DWC_otg controller.
54024 + * @param dwc_ep The EP to start the transfer on.
54025 + *
54026 + */
54027 +static void set_ddma_iso_pkts_info(dwc_otg_core_if_t * core_if,
54028 + dwc_ep_t * dwc_ep)
54029 +{
54030 + dwc_otg_dev_dma_desc_t *dma_desc;
54031 + dev_dma_desc_sts_t sts = {.d32 = 0 };
54032 + iso_pkt_info_t *iso_packet;
54033 + uint32_t data_per_desc;
54034 + uint32_t offset;
54035 + int i, j;
54036 +
54037 + iso_packet = dwc_ep->pkt_info;
54038 +
54039 + /** Reinit closed DMA Descriptors*/
54040 + /** ISO OUT EP */
54041 + if (dwc_ep->is_in == 0) {
54042 + dma_desc =
54043 + dwc_ep->iso_desc_addr +
54044 + dwc_ep->desc_cnt * dwc_ep->proc_buf_num;
54045 + offset = 0;
54046 +
54047 + for (i = 0; i < dwc_ep->desc_cnt - dwc_ep->pkt_per_frm;
54048 + i += dwc_ep->pkt_per_frm) {
54049 + for (j = 0; j < dwc_ep->pkt_per_frm; ++j) {
54050 + data_per_desc =
54051 + ((j + 1) * dwc_ep->maxpacket >
54052 + dwc_ep->
54053 + data_per_frame) ? dwc_ep->data_per_frame -
54054 + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
54055 + data_per_desc +=
54056 + (data_per_desc % 4) ? (4 -
54057 + data_per_desc %
54058 + 4) : 0;
54059 +
54060 + sts.d32 = dma_desc->status.d32;
54061 +
54062 + /* Write status in iso_packet_decsriptor */
54063 + iso_packet->status =
54064 + sts.b_iso_out.rxsts +
54065 + (sts.b_iso_out.bs ^ BS_DMA_DONE);
54066 + if (iso_packet->status) {
54067 + iso_packet->status = -DWC_E_NO_DATA;
54068 + }
54069 +
54070 + /* Received data length */
54071 + if (!sts.b_iso_out.rxbytes) {
54072 + iso_packet->length =
54073 + data_per_desc -
54074 + sts.b_iso_out.rxbytes;
54075 + } else {
54076 + iso_packet->length =
54077 + data_per_desc -
54078 + sts.b_iso_out.rxbytes + (4 -
54079 + dwc_ep->data_per_frame
54080 + % 4);
54081 + }
54082 +
54083 + iso_packet->offset = offset;
54084 +
54085 + offset += data_per_desc;
54086 + dma_desc++;
54087 + iso_packet++;
54088 + }
54089 + }
54090 +
54091 + for (j = 0; j < dwc_ep->pkt_per_frm - 1; ++j) {
54092 + data_per_desc =
54093 + ((j + 1) * dwc_ep->maxpacket >
54094 + dwc_ep->data_per_frame) ? dwc_ep->data_per_frame -
54095 + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
54096 + data_per_desc +=
54097 + (data_per_desc % 4) ? (4 - data_per_desc % 4) : 0;
54098 +
54099 + sts.d32 = dma_desc->status.d32;
54100 +
54101 + /* Write status in iso_packet_decsriptor */
54102 + iso_packet->status =
54103 + sts.b_iso_out.rxsts +
54104 + (sts.b_iso_out.bs ^ BS_DMA_DONE);
54105 + if (iso_packet->status) {
54106 + iso_packet->status = -DWC_E_NO_DATA;
54107 + }
54108 +
54109 + /* Received data length */
54110 + iso_packet->length =
54111 + dwc_ep->data_per_frame - sts.b_iso_out.rxbytes;
54112 +
54113 + iso_packet->offset = offset;
54114 +
54115 + offset += data_per_desc;
54116 + iso_packet++;
54117 + dma_desc++;
54118 + }
54119 +
54120 + sts.d32 = dma_desc->status.d32;
54121 +
54122 + /* Write status in iso_packet_decsriptor */
54123 + iso_packet->status =
54124 + sts.b_iso_out.rxsts + (sts.b_iso_out.bs ^ BS_DMA_DONE);
54125 + if (iso_packet->status) {
54126 + iso_packet->status = -DWC_E_NO_DATA;
54127 + }
54128 + /* Received data length */
54129 + if (!sts.b_iso_out.rxbytes) {
54130 + iso_packet->length =
54131 + dwc_ep->data_per_frame - sts.b_iso_out.rxbytes;
54132 + } else {
54133 + iso_packet->length =
54134 + dwc_ep->data_per_frame - sts.b_iso_out.rxbytes +
54135 + (4 - dwc_ep->data_per_frame % 4);
54136 + }
54137 +
54138 + iso_packet->offset = offset;
54139 + } else {
54140 +/** ISO IN EP */
54141 +
54142 + dma_desc =
54143 + dwc_ep->iso_desc_addr +
54144 + dwc_ep->desc_cnt * dwc_ep->proc_buf_num;
54145 +
54146 + for (i = 0; i < dwc_ep->desc_cnt - 1; i++) {
54147 + sts.d32 = dma_desc->status.d32;
54148 +
54149 + /* Write status in iso packet descriptor */
54150 + iso_packet->status =
54151 + sts.b_iso_in.txsts +
54152 + (sts.b_iso_in.bs ^ BS_DMA_DONE);
54153 + if (iso_packet->status != 0) {
54154 + iso_packet->status = -DWC_E_NO_DATA;
54155 +
54156 + }
54157 + /* Bytes has been transfered */
54158 + iso_packet->length =
54159 + dwc_ep->data_per_frame - sts.b_iso_in.txbytes;
54160 +
54161 + dma_desc++;
54162 + iso_packet++;
54163 + }
54164 +
54165 + sts.d32 = dma_desc->status.d32;
54166 + while (sts.b_iso_in.bs == BS_DMA_BUSY) {
54167 + sts.d32 = dma_desc->status.d32;
54168 + }
54169 +
54170 + /* Write status in iso packet descriptor ??? do be done with ERROR codes */
54171 + iso_packet->status =
54172 + sts.b_iso_in.txsts + (sts.b_iso_in.bs ^ BS_DMA_DONE);
54173 + if (iso_packet->status != 0) {
54174 + iso_packet->status = -DWC_E_NO_DATA;
54175 + }
54176 +
54177 + /* Bytes has been transfered */
54178 + iso_packet->length =
54179 + dwc_ep->data_per_frame - sts.b_iso_in.txbytes;
54180 + }
54181 +}
54182 +
54183 +/**
54184 + * This function reinitialize DMA Descriptors for Isochronous transfer
54185 + *
54186 + * @param core_if Programming view of DWC_otg controller.
54187 + * @param dwc_ep The EP to start the transfer on.
54188 + *
54189 + */
54190 +static void reinit_ddma_iso_xfer(dwc_otg_core_if_t * core_if, dwc_ep_t * dwc_ep)
54191 +{
54192 + int i, j;
54193 + dwc_otg_dev_dma_desc_t *dma_desc;
54194 + dma_addr_t dma_ad;
54195 + volatile uint32_t *addr;
54196 + dev_dma_desc_sts_t sts = {.d32 = 0 };
54197 + uint32_t data_per_desc;
54198 +
54199 + if (dwc_ep->is_in == 0) {
54200 + addr = &core_if->dev_if->out_ep_regs[dwc_ep->num]->doepctl;
54201 + } else {
54202 + addr = &core_if->dev_if->in_ep_regs[dwc_ep->num]->diepctl;
54203 + }
54204 +
54205 + if (dwc_ep->proc_buf_num == 0) {
54206 + /** Buffer 0 descriptors setup */
54207 + dma_ad = dwc_ep->dma_addr0;
54208 + } else {
54209 + /** Buffer 1 descriptors setup */
54210 + dma_ad = dwc_ep->dma_addr1;
54211 + }
54212 +
54213 + /** Reinit closed DMA Descriptors*/
54214 + /** ISO OUT EP */
54215 + if (dwc_ep->is_in == 0) {
54216 + dma_desc =
54217 + dwc_ep->iso_desc_addr +
54218 + dwc_ep->desc_cnt * dwc_ep->proc_buf_num;
54219 +
54220 + sts.b_iso_out.bs = BS_HOST_READY;
54221 + sts.b_iso_out.rxsts = 0;
54222 + sts.b_iso_out.l = 0;
54223 + sts.b_iso_out.sp = 0;
54224 + sts.b_iso_out.ioc = 0;
54225 + sts.b_iso_out.pid = 0;
54226 + sts.b_iso_out.framenum = 0;
54227 +
54228 + for (i = 0; i < dwc_ep->desc_cnt - dwc_ep->pkt_per_frm;
54229 + i += dwc_ep->pkt_per_frm) {
54230 + for (j = 0; j < dwc_ep->pkt_per_frm; ++j) {
54231 + data_per_desc =
54232 + ((j + 1) * dwc_ep->maxpacket >
54233 + dwc_ep->
54234 + data_per_frame) ? dwc_ep->data_per_frame -
54235 + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
54236 + data_per_desc +=
54237 + (data_per_desc % 4) ? (4 -
54238 + data_per_desc %
54239 + 4) : 0;
54240 + sts.b_iso_out.rxbytes = data_per_desc;
54241 + dma_desc->buf = dma_ad;
54242 + dma_desc->status.d32 = sts.d32;
54243 +
54244 + dma_ad += data_per_desc;
54245 + dma_desc++;
54246 + }
54247 + }
54248 +
54249 + for (j = 0; j < dwc_ep->pkt_per_frm - 1; ++j) {
54250 +
54251 + data_per_desc =
54252 + ((j + 1) * dwc_ep->maxpacket >
54253 + dwc_ep->data_per_frame) ? dwc_ep->data_per_frame -
54254 + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
54255 + data_per_desc +=
54256 + (data_per_desc % 4) ? (4 - data_per_desc % 4) : 0;
54257 + sts.b_iso_out.rxbytes = data_per_desc;
54258 +
54259 + dma_desc->buf = dma_ad;
54260 + dma_desc->status.d32 = sts.d32;
54261 +
54262 + dma_desc++;
54263 + dma_ad += data_per_desc;
54264 + }
54265 +
54266 + sts.b_iso_out.ioc = 1;
54267 + sts.b_iso_out.l = dwc_ep->proc_buf_num;
54268 +
54269 + data_per_desc =
54270 + ((j + 1) * dwc_ep->maxpacket >
54271 + dwc_ep->data_per_frame) ? dwc_ep->data_per_frame -
54272 + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
54273 + data_per_desc +=
54274 + (data_per_desc % 4) ? (4 - data_per_desc % 4) : 0;
54275 + sts.b_iso_out.rxbytes = data_per_desc;
54276 +
54277 + dma_desc->buf = dma_ad;
54278 + dma_desc->status.d32 = sts.d32;
54279 + } else {
54280 +/** ISO IN EP */
54281 +
54282 + dma_desc =
54283 + dwc_ep->iso_desc_addr +
54284 + dwc_ep->desc_cnt * dwc_ep->proc_buf_num;
54285 +
54286 + sts.b_iso_in.bs = BS_HOST_READY;
54287 + sts.b_iso_in.txsts = 0;
54288 + sts.b_iso_in.sp = 0;
54289 + sts.b_iso_in.ioc = 0;
54290 + sts.b_iso_in.pid = dwc_ep->pkt_per_frm;
54291 + sts.b_iso_in.framenum = dwc_ep->next_frame;
54292 + sts.b_iso_in.txbytes = dwc_ep->data_per_frame;
54293 + sts.b_iso_in.l = 0;
54294 +
54295 + for (i = 0; i < dwc_ep->desc_cnt - 1; i++) {
54296 + dma_desc->buf = dma_ad;
54297 + dma_desc->status.d32 = sts.d32;
54298 +
54299 + sts.b_iso_in.framenum += dwc_ep->bInterval;
54300 + dma_ad += dwc_ep->data_per_frame;
54301 + dma_desc++;
54302 + }
54303 +
54304 + sts.b_iso_in.ioc = 1;
54305 + sts.b_iso_in.l = dwc_ep->proc_buf_num;
54306 +
54307 + dma_desc->buf = dma_ad;
54308 + dma_desc->status.d32 = sts.d32;
54309 +
54310 + dwc_ep->next_frame =
54311 + sts.b_iso_in.framenum + dwc_ep->bInterval * 1;
54312 + }
54313 + dwc_ep->proc_buf_num = (dwc_ep->proc_buf_num ^ 1) & 0x1;
54314 +}
54315 +
54316 +/**
54317 + * This function is to handle Iso EP transfer complete interrupt
54318 + * in case Iso out packet was dropped
54319 + *
54320 + * @param core_if Programming view of DWC_otg controller.
54321 + * @param dwc_ep The EP for wihich transfer complete was asserted
54322 + *
54323 + */
54324 +static uint32_t handle_iso_out_pkt_dropped(dwc_otg_core_if_t * core_if,
54325 + dwc_ep_t * dwc_ep)
54326 +{
54327 + uint32_t dma_addr;
54328 + uint32_t drp_pkt;
54329 + uint32_t drp_pkt_cnt;
54330 + deptsiz_data_t deptsiz = {.d32 = 0 };
54331 + depctl_data_t depctl = {.d32 = 0 };
54332 + int i;
54333 +
54334 + deptsiz.d32 =
54335 + DWC_READ_REG32(&core_if->dev_if->
54336 + out_ep_regs[dwc_ep->num]->doeptsiz);
54337 +
54338 + drp_pkt = dwc_ep->pkt_cnt - deptsiz.b.pktcnt;
54339 + drp_pkt_cnt = dwc_ep->pkt_per_frm - (drp_pkt % dwc_ep->pkt_per_frm);
54340 +
54341 + /* Setting dropped packets status */
54342 + for (i = 0; i < drp_pkt_cnt; ++i) {
54343 + dwc_ep->pkt_info[drp_pkt].status = -DWC_E_NO_DATA;
54344 + drp_pkt++;
54345 + deptsiz.b.pktcnt--;
54346 + }
54347 +
54348 + if (deptsiz.b.pktcnt > 0) {
54349 + deptsiz.b.xfersize =
54350 + dwc_ep->xfer_len - (dwc_ep->pkt_cnt -
54351 + deptsiz.b.pktcnt) * dwc_ep->maxpacket;
54352 + } else {
54353 + deptsiz.b.xfersize = 0;
54354 + deptsiz.b.pktcnt = 0;
54355 + }
54356 +
54357 + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[dwc_ep->num]->doeptsiz,
54358 + deptsiz.d32);
54359 +
54360 + if (deptsiz.b.pktcnt > 0) {
54361 + if (dwc_ep->proc_buf_num) {
54362 + dma_addr =
54363 + dwc_ep->dma_addr1 + dwc_ep->xfer_len -
54364 + deptsiz.b.xfersize;
54365 + } else {
54366 + dma_addr =
54367 + dwc_ep->dma_addr0 + dwc_ep->xfer_len -
54368 + deptsiz.b.xfersize;;
54369 + }
54370 +
54371 + DWC_WRITE_REG32(&core_if->dev_if->
54372 + out_ep_regs[dwc_ep->num]->doepdma, dma_addr);
54373 +
54374 + /** Re-enable endpoint, clear nak */
54375 + depctl.d32 = 0;
54376 + depctl.b.epena = 1;
54377 + depctl.b.cnak = 1;
54378 +
54379 + DWC_MODIFY_REG32(&core_if->dev_if->
54380 + out_ep_regs[dwc_ep->num]->doepctl, depctl.d32,
54381 + depctl.d32);
54382 + return 0;
54383 + } else {
54384 + return 1;
54385 + }
54386 +}
54387 +
54388 +/**
54389 + * This function sets iso packets information(PTI mode)
54390 + *
54391 + * @param core_if Programming view of DWC_otg controller.
54392 + * @param ep The EP to start the transfer on.
54393 + *
54394 + */
54395 +static uint32_t set_iso_pkts_info(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
54396 +{
54397 + int i, j;
54398 + dma_addr_t dma_ad;
54399 + iso_pkt_info_t *packet_info = ep->pkt_info;
54400 + uint32_t offset;
54401 + uint32_t frame_data;
54402 + deptsiz_data_t deptsiz;
54403 +
54404 + if (ep->proc_buf_num == 0) {
54405 + /** Buffer 0 descriptors setup */
54406 + dma_ad = ep->dma_addr0;
54407 + } else {
54408 + /** Buffer 1 descriptors setup */
54409 + dma_ad = ep->dma_addr1;
54410 + }
54411 +
54412 + if (ep->is_in) {
54413 + deptsiz.d32 =
54414 + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
54415 + dieptsiz);
54416 + } else {
54417 + deptsiz.d32 =
54418 + DWC_READ_REG32(&core_if->dev_if->out_ep_regs[ep->num]->
54419 + doeptsiz);
54420 + }
54421 +
54422 + if (!deptsiz.b.xfersize) {
54423 + offset = 0;
54424 + for (i = 0; i < ep->pkt_cnt; i += ep->pkt_per_frm) {
54425 + frame_data = ep->data_per_frame;
54426 + for (j = 0; j < ep->pkt_per_frm; ++j) {
54427 +
54428 + /* Packet status - is not set as initially
54429 + * it is set to 0 and if packet was sent
54430 + successfully, status field will remain 0*/
54431 +
54432 + /* Bytes has been transfered */
54433 + packet_info->length =
54434 + (ep->maxpacket <
54435 + frame_data) ? ep->maxpacket : frame_data;
54436 +
54437 + /* Received packet offset */
54438 + packet_info->offset = offset;
54439 + offset += packet_info->length;
54440 + frame_data -= packet_info->length;
54441 +
54442 + packet_info++;
54443 + }
54444 + }
54445 + return 1;
54446 + } else {
54447 + /* This is a workaround for in case of Transfer Complete with
54448 + * PktDrpSts interrupts merging - in this case Transfer complete
54449 + * interrupt for Isoc Out Endpoint is asserted without PktDrpSts
54450 + * set and with DOEPTSIZ register non zero. Investigations showed,
54451 + * that this happens when Out packet is dropped, but because of
54452 + * interrupts merging during first interrupt handling PktDrpSts
54453 + * bit is cleared and for next merged interrupts it is not reset.
54454 + * In this case SW hadles the interrupt as if PktDrpSts bit is set.
54455 + */
54456 + if (ep->is_in) {
54457 + return 1;
54458 + } else {
54459 + return handle_iso_out_pkt_dropped(core_if, ep);
54460 + }
54461 + }
54462 +}
54463 +
54464 +/**
54465 + * This function is to handle Iso EP transfer complete interrupt
54466 + *
54467 + * @param pcd The PCD
54468 + * @param ep The EP for which transfer complete was asserted
54469 + *
54470 + */
54471 +static void complete_iso_ep(dwc_otg_pcd_t * pcd, dwc_otg_pcd_ep_t * ep)
54472 +{
54473 + dwc_otg_core_if_t *core_if = GET_CORE_IF(ep->pcd);
54474 + dwc_ep_t *dwc_ep = &ep->dwc_ep;
54475 + uint8_t is_last = 0;
54476 +
54477 + if (ep->dwc_ep.next_frame == 0xffffffff) {
54478 + DWC_WARN("Next frame is not set!\n");
54479 + return;
54480 + }
54481 +
54482 + if (core_if->dma_enable) {
54483 + if (core_if->dma_desc_enable) {
54484 + set_ddma_iso_pkts_info(core_if, dwc_ep);
54485 + reinit_ddma_iso_xfer(core_if, dwc_ep);
54486 + is_last = 1;
54487 + } else {
54488 + if (core_if->pti_enh_enable) {
54489 + if (set_iso_pkts_info(core_if, dwc_ep)) {
54490 + dwc_ep->proc_buf_num =
54491 + (dwc_ep->proc_buf_num ^ 1) & 0x1;
54492 + dwc_otg_iso_ep_start_buf_transfer
54493 + (core_if, dwc_ep);
54494 + is_last = 1;
54495 + }
54496 + } else {
54497 + set_current_pkt_info(core_if, dwc_ep);
54498 + if (dwc_ep->cur_pkt >= dwc_ep->pkt_cnt) {
54499 + is_last = 1;
54500 + dwc_ep->cur_pkt = 0;
54501 + dwc_ep->proc_buf_num =
54502 + (dwc_ep->proc_buf_num ^ 1) & 0x1;
54503 + if (dwc_ep->proc_buf_num) {
54504 + dwc_ep->cur_pkt_addr =
54505 + dwc_ep->xfer_buff1;
54506 + dwc_ep->cur_pkt_dma_addr =
54507 + dwc_ep->dma_addr1;
54508 + } else {
54509 + dwc_ep->cur_pkt_addr =
54510 + dwc_ep->xfer_buff0;
54511 + dwc_ep->cur_pkt_dma_addr =
54512 + dwc_ep->dma_addr0;
54513 + }
54514 +
54515 + }
54516 + dwc_otg_iso_ep_start_frm_transfer(core_if,
54517 + dwc_ep);
54518 + }
54519 + }
54520 + } else {
54521 + set_current_pkt_info(core_if, dwc_ep);
54522 + if (dwc_ep->cur_pkt >= dwc_ep->pkt_cnt) {
54523 + is_last = 1;
54524 + dwc_ep->cur_pkt = 0;
54525 + dwc_ep->proc_buf_num = (dwc_ep->proc_buf_num ^ 1) & 0x1;
54526 + if (dwc_ep->proc_buf_num) {
54527 + dwc_ep->cur_pkt_addr = dwc_ep->xfer_buff1;
54528 + dwc_ep->cur_pkt_dma_addr = dwc_ep->dma_addr1;
54529 + } else {
54530 + dwc_ep->cur_pkt_addr = dwc_ep->xfer_buff0;
54531 + dwc_ep->cur_pkt_dma_addr = dwc_ep->dma_addr0;
54532 + }
54533 +
54534 + }
54535 + dwc_otg_iso_ep_start_frm_transfer(core_if, dwc_ep);
54536 + }
54537 + if (is_last)
54538 + dwc_otg_iso_buffer_done(pcd, ep, ep->iso_req_handle);
54539 +}
54540 +#endif /* DWC_EN_ISOC */
54541 +
54542 +/**
54543 + * This function handle BNA interrupt for Non Isochronous EPs
54544 + *
54545 + */
54546 +static void dwc_otg_pcd_handle_noniso_bna(dwc_otg_pcd_ep_t * ep)
54547 +{
54548 + dwc_ep_t *dwc_ep = &ep->dwc_ep;
54549 + volatile uint32_t *addr;
54550 + depctl_data_t depctl = {.d32 = 0 };
54551 + dwc_otg_pcd_t *pcd = ep->pcd;
54552 + dwc_otg_dev_dma_desc_t *dma_desc;
54553 + dev_dma_desc_sts_t sts = {.d32 = 0 };
54554 + dwc_otg_core_if_t *core_if = ep->pcd->core_if;
54555 + int i, start;
54556 +
54557 + if (!dwc_ep->desc_cnt)
54558 + DWC_WARN("Ep%d %s Descriptor count = %d \n", dwc_ep->num,
54559 + (dwc_ep->is_in ? "IN" : "OUT"), dwc_ep->desc_cnt);
54560 +
54561 + if (core_if->core_params->cont_on_bna && !dwc_ep->is_in
54562 + && dwc_ep->type != DWC_OTG_EP_TYPE_CONTROL) {
54563 + uint32_t doepdma;
54564 + dwc_otg_dev_out_ep_regs_t *out_regs =
54565 + core_if->dev_if->out_ep_regs[dwc_ep->num];
54566 + doepdma = DWC_READ_REG32(&(out_regs->doepdma));
54567 + start = (doepdma - dwc_ep->dma_desc_addr)/sizeof(dwc_otg_dev_dma_desc_t);
54568 + dma_desc = &(dwc_ep->desc_addr[start]);
54569 + } else {
54570 + start = 0;
54571 + dma_desc = dwc_ep->desc_addr;
54572 + }
54573 +
54574 +
54575 + for (i = start; i < dwc_ep->desc_cnt; ++i, ++dma_desc) {
54576 + sts.d32 = dma_desc->status.d32;
54577 + sts.b.bs = BS_HOST_READY;
54578 + dma_desc->status.d32 = sts.d32;
54579 + }
54580 +
54581 + if (dwc_ep->is_in == 0) {
54582 + addr =
54583 + &GET_CORE_IF(pcd)->dev_if->out_ep_regs[dwc_ep->num]->
54584 + doepctl;
54585 + } else {
54586 + addr =
54587 + &GET_CORE_IF(pcd)->dev_if->in_ep_regs[dwc_ep->num]->diepctl;
54588 + }
54589 + depctl.b.epena = 1;
54590 + depctl.b.cnak = 1;
54591 + DWC_MODIFY_REG32(addr, 0, depctl.d32);
54592 +}
54593 +
54594 +/**
54595 + * This function handles EP0 Control transfers.
54596 + *
54597 + * The state of the control transfers are tracked in
54598 + * <code>ep0state</code>.
54599 + */
54600 +static void handle_ep0(dwc_otg_pcd_t * pcd)
54601 +{
54602 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
54603 + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
54604 + dev_dma_desc_sts_t desc_sts;
54605 + deptsiz0_data_t deptsiz;
54606 + uint32_t byte_count;
54607 +
54608 +#ifdef DEBUG_EP0
54609 + DWC_DEBUGPL(DBG_PCDV, "%s()\n", __func__);
54610 + print_ep0_state(pcd);
54611 +#endif
54612 +
54613 +// DWC_PRINTF("HANDLE EP0\n");
54614 +
54615 + switch (pcd->ep0state) {
54616 + case EP0_DISCONNECT:
54617 + break;
54618 +
54619 + case EP0_IDLE:
54620 + pcd->request_config = 0;
54621 +
54622 + pcd_setup(pcd);
54623 + break;
54624 +
54625 + case EP0_IN_DATA_PHASE:
54626 +#ifdef DEBUG_EP0
54627 + DWC_DEBUGPL(DBG_PCD, "DATA_IN EP%d-%s: type=%d, mps=%d\n",
54628 + ep0->dwc_ep.num, (ep0->dwc_ep.is_in ? "IN" : "OUT"),
54629 + ep0->dwc_ep.type, ep0->dwc_ep.maxpacket);
54630 +#endif
54631 +
54632 + if (core_if->dma_enable != 0) {
54633 + /*
54634 + * For EP0 we can only program 1 packet at a time so we
54635 + * need to do the make calculations after each complete.
54636 + * Call write_packet to make the calculations, as in
54637 + * slave mode, and use those values to determine if we
54638 + * can complete.
54639 + */
54640 + if (core_if->dma_desc_enable == 0) {
54641 + deptsiz.d32 =
54642 + DWC_READ_REG32(&core_if->
54643 + dev_if->in_ep_regs[0]->
54644 + dieptsiz);
54645 + byte_count =
54646 + ep0->dwc_ep.xfer_len - deptsiz.b.xfersize;
54647 + } else {
54648 + desc_sts =
54649 + core_if->dev_if->in_desc_addr->status;
54650 + byte_count =
54651 + ep0->dwc_ep.xfer_len - desc_sts.b.bytes;
54652 + }
54653 + ep0->dwc_ep.xfer_count += byte_count;
54654 + ep0->dwc_ep.xfer_buff += byte_count;
54655 + ep0->dwc_ep.dma_addr += byte_count;
54656 + }
54657 + if (ep0->dwc_ep.xfer_count < ep0->dwc_ep.total_len) {
54658 + dwc_otg_ep0_continue_transfer(GET_CORE_IF(pcd),
54659 + &ep0->dwc_ep);
54660 + DWC_DEBUGPL(DBG_PCD, "CONTINUE TRANSFER\n");
54661 + } else if (ep0->dwc_ep.sent_zlp) {
54662 + dwc_otg_ep0_continue_transfer(GET_CORE_IF(pcd),
54663 + &ep0->dwc_ep);
54664 + ep0->dwc_ep.sent_zlp = 0;
54665 + DWC_DEBUGPL(DBG_PCD, "CONTINUE TRANSFER sent zlp\n");
54666 + } else {
54667 + ep0_complete_request(ep0);
54668 + DWC_DEBUGPL(DBG_PCD, "COMPLETE TRANSFER\n");
54669 + }
54670 + break;
54671 + case EP0_OUT_DATA_PHASE:
54672 +#ifdef DEBUG_EP0
54673 + DWC_DEBUGPL(DBG_PCD, "DATA_OUT EP%d-%s: type=%d, mps=%d\n",
54674 + ep0->dwc_ep.num, (ep0->dwc_ep.is_in ? "IN" : "OUT"),
54675 + ep0->dwc_ep.type, ep0->dwc_ep.maxpacket);
54676 +#endif
54677 + if (core_if->dma_enable != 0) {
54678 + if (core_if->dma_desc_enable == 0) {
54679 + deptsiz.d32 =
54680 + DWC_READ_REG32(&core_if->
54681 + dev_if->out_ep_regs[0]->
54682 + doeptsiz);
54683 + byte_count =
54684 + ep0->dwc_ep.maxpacket - deptsiz.b.xfersize;
54685 + } else {
54686 + desc_sts =
54687 + core_if->dev_if->out_desc_addr->status;
54688 + byte_count =
54689 + ep0->dwc_ep.maxpacket - desc_sts.b.bytes;
54690 + }
54691 + ep0->dwc_ep.xfer_count += byte_count;
54692 + ep0->dwc_ep.xfer_buff += byte_count;
54693 + ep0->dwc_ep.dma_addr += byte_count;
54694 + }
54695 + if (ep0->dwc_ep.xfer_count < ep0->dwc_ep.total_len) {
54696 + dwc_otg_ep0_continue_transfer(GET_CORE_IF(pcd),
54697 + &ep0->dwc_ep);
54698 + DWC_DEBUGPL(DBG_PCD, "CONTINUE TRANSFER\n");
54699 + } else if (ep0->dwc_ep.sent_zlp) {
54700 + dwc_otg_ep0_continue_transfer(GET_CORE_IF(pcd),
54701 + &ep0->dwc_ep);
54702 + ep0->dwc_ep.sent_zlp = 0;
54703 + DWC_DEBUGPL(DBG_PCD, "CONTINUE TRANSFER sent zlp\n");
54704 + } else {
54705 + ep0_complete_request(ep0);
54706 + DWC_DEBUGPL(DBG_PCD, "COMPLETE TRANSFER\n");
54707 + }
54708 + break;
54709 +
54710 + case EP0_IN_STATUS_PHASE:
54711 + case EP0_OUT_STATUS_PHASE:
54712 + DWC_DEBUGPL(DBG_PCD, "CASE: EP0_STATUS\n");
54713 + ep0_complete_request(ep0);
54714 + pcd->ep0state = EP0_IDLE;
54715 + ep0->stopped = 1;
54716 + ep0->dwc_ep.is_in = 0; /* OUT for next SETUP */
54717 +
54718 + /* Prepare for more SETUP Packets */
54719 + if (core_if->dma_enable) {
54720 + ep0_out_start(core_if, pcd);
54721 + }
54722 + break;
54723 +
54724 + case EP0_STALL:
54725 + DWC_ERROR("EP0 STALLed, should not get here pcd_setup()\n");
54726 + break;
54727 + }
54728 +#ifdef DEBUG_EP0
54729 + print_ep0_state(pcd);
54730 +#endif
54731 +}
54732 +
54733 +/**
54734 + * Restart transfer
54735 + */
54736 +static void restart_transfer(dwc_otg_pcd_t * pcd, const uint32_t epnum)
54737 +{
54738 + dwc_otg_core_if_t *core_if;
54739 + dwc_otg_dev_if_t *dev_if;
54740 + deptsiz_data_t dieptsiz = {.d32 = 0 };
54741 + dwc_otg_pcd_ep_t *ep;
54742 +
54743 + ep = get_in_ep(pcd, epnum);
54744 +
54745 +#ifdef DWC_EN_ISOC
54746 + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
54747 + return;
54748 + }
54749 +#endif /* DWC_EN_ISOC */
54750 +
54751 + core_if = GET_CORE_IF(pcd);
54752 + dev_if = core_if->dev_if;
54753 +
54754 + dieptsiz.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dieptsiz);
54755 +
54756 + DWC_DEBUGPL(DBG_PCD, "xfer_buff=%p xfer_count=%0x xfer_len=%0x"
54757 + " stopped=%d\n", ep->dwc_ep.xfer_buff,
54758 + ep->dwc_ep.xfer_count, ep->dwc_ep.xfer_len, ep->stopped);
54759 + /*
54760 + * If xfersize is 0 and pktcnt in not 0, resend the last packet.
54761 + */
54762 + if (dieptsiz.b.pktcnt && dieptsiz.b.xfersize == 0 &&
54763 + ep->dwc_ep.start_xfer_buff != 0) {
54764 + if (ep->dwc_ep.total_len <= ep->dwc_ep.maxpacket) {
54765 + ep->dwc_ep.xfer_count = 0;
54766 + ep->dwc_ep.xfer_buff = ep->dwc_ep.start_xfer_buff;
54767 + ep->dwc_ep.xfer_len = ep->dwc_ep.xfer_count;
54768 + } else {
54769 + ep->dwc_ep.xfer_count -= ep->dwc_ep.maxpacket;
54770 + /* convert packet size to dwords. */
54771 + ep->dwc_ep.xfer_buff -= ep->dwc_ep.maxpacket;
54772 + ep->dwc_ep.xfer_len = ep->dwc_ep.xfer_count;
54773 + }
54774 + ep->stopped = 0;
54775 + DWC_DEBUGPL(DBG_PCD, "xfer_buff=%p xfer_count=%0x "
54776 + "xfer_len=%0x stopped=%d\n",
54777 + ep->dwc_ep.xfer_buff,
54778 + ep->dwc_ep.xfer_count, ep->dwc_ep.xfer_len,
54779 + ep->stopped);
54780 + if (epnum == 0) {
54781 + dwc_otg_ep0_start_transfer(core_if, &ep->dwc_ep);
54782 + } else {
54783 + dwc_otg_ep_start_transfer(core_if, &ep->dwc_ep);
54784 + }
54785 + }
54786 +}
54787 +
54788 +/*
54789 + * This function create new nextep sequnce based on Learn Queue.
54790 + *
54791 + * @param core_if Programming view of DWC_otg controller
54792 + */
54793 +void predict_nextep_seq( dwc_otg_core_if_t * core_if)
54794 +{
54795 + dwc_otg_device_global_regs_t *dev_global_regs =
54796 + core_if->dev_if->dev_global_regs;
54797 + const uint32_t TOKEN_Q_DEPTH = core_if->hwcfg2.b.dev_token_q_depth;
54798 + /* Number of Token Queue Registers */
54799 + const int DTKNQ_REG_CNT = (TOKEN_Q_DEPTH + 7) / 8;
54800 + dtknq1_data_t dtknqr1;
54801 + uint32_t in_tkn_epnums[4];
54802 + uint8_t seqnum[MAX_EPS_CHANNELS];
54803 + uint8_t intkn_seq[TOKEN_Q_DEPTH];
54804 + grstctl_t resetctl = {.d32 = 0 };
54805 + uint8_t temp;
54806 + int ndx = 0;
54807 + int start = 0;
54808 + int end = 0;
54809 + int sort_done = 0;
54810 + int i = 0;
54811 + volatile uint32_t *addr = &dev_global_regs->dtknqr1;
54812 +
54813 +
54814 + DWC_DEBUGPL(DBG_PCD,"dev_token_q_depth=%d\n",TOKEN_Q_DEPTH);
54815 +
54816 + /* Read the DTKNQ Registers */
54817 + for (i = 0; i < DTKNQ_REG_CNT; i++) {
54818 + in_tkn_epnums[i] = DWC_READ_REG32(addr);
54819 + DWC_DEBUGPL(DBG_PCDV, "DTKNQR%d=0x%08x\n", i + 1,
54820 + in_tkn_epnums[i]);
54821 + if (addr == &dev_global_regs->dvbusdis) {
54822 + addr = &dev_global_regs->dtknqr3_dthrctl;
54823 + } else {
54824 + ++addr;
54825 + }
54826 +
54827 + }
54828 +
54829 + /* Copy the DTKNQR1 data to the bit field. */
54830 + dtknqr1.d32 = in_tkn_epnums[0];
54831 + if (dtknqr1.b.wrap_bit) {
54832 + ndx = dtknqr1.b.intknwptr;
54833 + end = ndx -1;
54834 + if (end < 0)
54835 + end = TOKEN_Q_DEPTH -1;
54836 + } else {
54837 + ndx = 0;
54838 + end = dtknqr1.b.intknwptr -1;
54839 + if (end < 0)
54840 + end = 0;
54841 + }
54842 + start = ndx;
54843 +
54844 + /* Fill seqnum[] by initial values: EP number + 31 */
54845 + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
54846 + seqnum[i] = i +31;
54847 + }
54848 +
54849 + /* Fill intkn_seq[] from in_tkn_epnums[0] */
54850 + for (i=0; i < 6; i++)
54851 + intkn_seq[i] = (in_tkn_epnums[0] >> ((7-i) * 4)) & 0xf;
54852 +
54853 + if (TOKEN_Q_DEPTH > 6) {
54854 + /* Fill intkn_seq[] from in_tkn_epnums[1] */
54855 + for (i=6; i < 14; i++)
54856 + intkn_seq[i] =
54857 + (in_tkn_epnums[1] >> ((7 - (i - 6)) * 4)) & 0xf;
54858 + }
54859 +
54860 + if (TOKEN_Q_DEPTH > 14) {
54861 + /* Fill intkn_seq[] from in_tkn_epnums[1] */
54862 + for (i=14; i < 22; i++)
54863 + intkn_seq[i] =
54864 + (in_tkn_epnums[2] >> ((7 - (i - 14)) * 4)) & 0xf;
54865 + }
54866 +
54867 + if (TOKEN_Q_DEPTH > 22) {
54868 + /* Fill intkn_seq[] from in_tkn_epnums[1] */
54869 + for (i=22; i < 30; i++)
54870 + intkn_seq[i] =
54871 + (in_tkn_epnums[3] >> ((7 - (i - 22)) * 4)) & 0xf;
54872 + }
54873 +
54874 + DWC_DEBUGPL(DBG_PCDV, "%s start=%d end=%d intkn_seq[]:\n", __func__,
54875 + start, end);
54876 + for (i=0; i<TOKEN_Q_DEPTH; i++)
54877 + DWC_DEBUGPL(DBG_PCDV,"%d\n", intkn_seq[i]);
54878 +
54879 + /* Update seqnum based on intkn_seq[] */
54880 + i = 0;
54881 + do {
54882 + seqnum[intkn_seq[ndx]] = i;
54883 + ndx++;
54884 + i++;
54885 + if (ndx == TOKEN_Q_DEPTH)
54886 + ndx = 0;
54887 + } while ( i < TOKEN_Q_DEPTH );
54888 +
54889 + /* Mark non active EP's in seqnum[] by 0xff */
54890 + for (i=0; i<=core_if->dev_if->num_in_eps; i++) {
54891 + if (core_if->nextep_seq[i] == 0xff )
54892 + seqnum[i] = 0xff;
54893 + }
54894 +
54895 + /* Sort seqnum[] */
54896 + sort_done = 0;
54897 + while (!sort_done) {
54898 + sort_done = 1;
54899 + for (i=0; i<core_if->dev_if->num_in_eps; i++) {
54900 + if (seqnum[i] > seqnum[i+1]) {
54901 + temp = seqnum[i];
54902 + seqnum[i] = seqnum[i+1];
54903 + seqnum[i+1] = temp;
54904 + sort_done = 0;
54905 + }
54906 + }
54907 + }
54908 +
54909 + ndx = start + seqnum[0];
54910 + if (ndx >= TOKEN_Q_DEPTH)
54911 + ndx = ndx % TOKEN_Q_DEPTH;
54912 + core_if->first_in_nextep_seq = intkn_seq[ndx];
54913 +
54914 + /* Update seqnum[] by EP numbers */
54915 + for (i=0; i<=core_if->dev_if->num_in_eps; i++) {
54916 + ndx = start + i;
54917 + if (seqnum[i] < 31) {
54918 + ndx = start + seqnum[i];
54919 + if (ndx >= TOKEN_Q_DEPTH)
54920 + ndx = ndx % TOKEN_Q_DEPTH;
54921 + seqnum[i] = intkn_seq[ndx];
54922 + } else {
54923 + if (seqnum[i] < 0xff) {
54924 + seqnum[i] = seqnum[i] - 31;
54925 + } else {
54926 + break;
54927 + }
54928 + }
54929 + }
54930 +
54931 + /* Update nextep_seq[] based on seqnum[] */
54932 + for (i=0; i<core_if->dev_if->num_in_eps; i++) {
54933 + if (seqnum[i] != 0xff) {
54934 + if (seqnum[i+1] != 0xff) {
54935 + core_if->nextep_seq[seqnum[i]] = seqnum[i+1];
54936 + } else {
54937 + core_if->nextep_seq[seqnum[i]] = core_if->first_in_nextep_seq;
54938 + break;
54939 + }
54940 + } else {
54941 + break;
54942 + }
54943 + }
54944 +
54945 + DWC_DEBUGPL(DBG_PCDV, "%s first_in_nextep_seq= %2d; nextep_seq[]:\n",
54946 + __func__, core_if->first_in_nextep_seq);
54947 + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
54948 + DWC_DEBUGPL(DBG_PCDV,"%2d\n", core_if->nextep_seq[i]);
54949 + }
54950 +
54951 + /* Flush the Learning Queue */
54952 + resetctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->grstctl);
54953 + resetctl.b.intknqflsh = 1;
54954 + DWC_WRITE_REG32(&core_if->core_global_regs->grstctl, resetctl.d32);
54955 +
54956 +
54957 +}
54958 +
54959 +/**
54960 + * handle the IN EP disable interrupt.
54961 + */
54962 +static inline void handle_in_ep_disable_intr(dwc_otg_pcd_t * pcd,
54963 + const uint32_t epnum)
54964 +{
54965 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
54966 + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
54967 + deptsiz_data_t dieptsiz = {.d32 = 0 };
54968 + dctl_data_t dctl = {.d32 = 0 };
54969 + dwc_otg_pcd_ep_t *ep;
54970 + dwc_ep_t *dwc_ep;
54971 + gintmsk_data_t gintmsk_data;
54972 + depctl_data_t depctl;
54973 + uint32_t diepdma;
54974 + uint32_t remain_to_transfer = 0;
54975 + uint8_t i;
54976 + uint32_t xfer_size;
54977 +
54978 + ep = get_in_ep(pcd, epnum);
54979 + dwc_ep = &ep->dwc_ep;
54980 +
54981 + if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
54982 + dwc_otg_flush_tx_fifo(core_if, dwc_ep->tx_fifo_num);
54983 + complete_ep(ep);
54984 + return;
54985 + }
54986 +
54987 + DWC_DEBUGPL(DBG_PCD, "diepctl%d=%0x\n", epnum,
54988 + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->diepctl));
54989 + dieptsiz.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dieptsiz);
54990 + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->diepctl);
54991 +
54992 + DWC_DEBUGPL(DBG_ANY, "pktcnt=%d size=%d\n",
54993 + dieptsiz.b.pktcnt, dieptsiz.b.xfersize);
54994 +
54995 + if ((core_if->start_predict == 0) || (depctl.b.eptype & 1)) {
54996 + if (ep->stopped) {
54997 + if (core_if->en_multiple_tx_fifo)
54998 + /* Flush the Tx FIFO */
54999 + dwc_otg_flush_tx_fifo(core_if, dwc_ep->tx_fifo_num);
55000 + /* Clear the Global IN NP NAK */
55001 + dctl.d32 = 0;
55002 + dctl.b.cgnpinnak = 1;
55003 + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, dctl.d32, dctl.d32);
55004 + /* Restart the transaction */
55005 + if (dieptsiz.b.pktcnt != 0 || dieptsiz.b.xfersize != 0) {
55006 + restart_transfer(pcd, epnum);
55007 + }
55008 + } else {
55009 + /* Restart the transaction */
55010 + if (dieptsiz.b.pktcnt != 0 || dieptsiz.b.xfersize != 0) {
55011 + restart_transfer(pcd, epnum);
55012 + }
55013 + DWC_DEBUGPL(DBG_ANY, "STOPPED!!!\n");
55014 + }
55015 + return;
55016 + }
55017 +
55018 + if (core_if->start_predict > 2) { // NP IN EP
55019 + core_if->start_predict--;
55020 + return;
55021 + }
55022 +
55023 + core_if->start_predict--;
55024 +
55025 + if (core_if->start_predict == 1) { // All NP IN Ep's disabled now
55026 +
55027 + predict_nextep_seq(core_if);
55028 +
55029 + /* Update all active IN EP's NextEP field based of nextep_seq[] */
55030 + for ( i = 0; i <= core_if->dev_if->num_in_eps; i++) {
55031 + depctl.d32 =
55032 + DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
55033 + if (core_if->nextep_seq[i] != 0xff) { // Active NP IN EP
55034 + depctl.b.nextep = core_if->nextep_seq[i];
55035 + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepctl, depctl.d32);
55036 + }
55037 + }
55038 + /* Flush Shared NP TxFIFO */
55039 + dwc_otg_flush_tx_fifo(core_if, 0);
55040 + /* Rewind buffers */
55041 + if (!core_if->dma_desc_enable) {
55042 + i = core_if->first_in_nextep_seq;
55043 + do {
55044 + ep = get_in_ep(pcd, i);
55045 + dieptsiz.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->dieptsiz);
55046 + xfer_size = ep->dwc_ep.total_len - ep->dwc_ep.xfer_count;
55047 + if (xfer_size > ep->dwc_ep.maxxfer)
55048 + xfer_size = ep->dwc_ep.maxxfer;
55049 + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
55050 + if (dieptsiz.b.pktcnt != 0) {
55051 + if (xfer_size == 0) {
55052 + remain_to_transfer = 0;
55053 + } else {
55054 + if ((xfer_size % ep->dwc_ep.maxpacket) == 0) {
55055 + remain_to_transfer =
55056 + dieptsiz.b.pktcnt * ep->dwc_ep.maxpacket;
55057 + } else {
55058 + remain_to_transfer = ((dieptsiz.b.pktcnt -1) * ep->dwc_ep.maxpacket)
55059 + + (xfer_size % ep->dwc_ep.maxpacket);
55060 + }
55061 + }
55062 + diepdma = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepdma);
55063 + dieptsiz.b.xfersize = remain_to_transfer;
55064 + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->dieptsiz, dieptsiz.d32);
55065 + diepdma = ep->dwc_ep.dma_addr + (xfer_size - remain_to_transfer);
55066 + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepdma, diepdma);
55067 + }
55068 + i = core_if->nextep_seq[i];
55069 + } while (i != core_if->first_in_nextep_seq);
55070 + } else { // dma_desc_enable
55071 + DWC_PRINTF("%s Learning Queue not supported in DDMA\n", __func__);
55072 + }
55073 +
55074 + /* Restart transfers in predicted sequences */
55075 + i = core_if->first_in_nextep_seq;
55076 + do {
55077 + dieptsiz.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->dieptsiz);
55078 + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
55079 + if (dieptsiz.b.pktcnt != 0) {
55080 + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
55081 + depctl.b.epena = 1;
55082 + depctl.b.cnak = 1;
55083 + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepctl, depctl.d32);
55084 + }
55085 + i = core_if->nextep_seq[i];
55086 + } while (i != core_if->first_in_nextep_seq);
55087 +
55088 + /* Clear the global non-periodic IN NAK handshake */
55089 + dctl.d32 = 0;
55090 + dctl.b.cgnpinnak = 1;
55091 + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, dctl.d32, dctl.d32);
55092 +
55093 + /* Unmask EP Mismatch interrupt */
55094 + gintmsk_data.d32 = 0;
55095 + gintmsk_data.b.epmismatch = 1;
55096 + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, 0, gintmsk_data.d32);
55097 +
55098 + core_if->start_predict = 0;
55099 +
55100 + }
55101 +}
55102 +
55103 +/**
55104 + * Handler for the IN EP timeout handshake interrupt.
55105 + */
55106 +static inline void handle_in_ep_timeout_intr(dwc_otg_pcd_t * pcd,
55107 + const uint32_t epnum)
55108 +{
55109 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
55110 + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
55111 +
55112 +#ifdef DEBUG
55113 + deptsiz_data_t dieptsiz = {.d32 = 0 };
55114 + uint32_t num = 0;
55115 +#endif
55116 + dctl_data_t dctl = {.d32 = 0 };
55117 + dwc_otg_pcd_ep_t *ep;
55118 +
55119 + gintmsk_data_t intr_mask = {.d32 = 0 };
55120 +
55121 + ep = get_in_ep(pcd, epnum);
55122 +
55123 + /* Disable the NP Tx Fifo Empty Interrrupt */
55124 + if (!core_if->dma_enable) {
55125 + intr_mask.b.nptxfempty = 1;
55126 + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk,
55127 + intr_mask.d32, 0);
55128 + }
55129 + /** @todo NGS Check EP type.
55130 + * Implement for Periodic EPs */
55131 + /*
55132 + * Non-periodic EP
55133 + */
55134 + /* Enable the Global IN NAK Effective Interrupt */
55135 + intr_mask.b.ginnakeff = 1;
55136 + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, 0, intr_mask.d32);
55137 +
55138 + /* Set Global IN NAK */
55139 + dctl.b.sgnpinnak = 1;
55140 + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, dctl.d32, dctl.d32);
55141 +
55142 + ep->stopped = 1;
55143 +
55144 +#ifdef DEBUG
55145 + dieptsiz.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[num]->dieptsiz);
55146 + DWC_DEBUGPL(DBG_ANY, "pktcnt=%d size=%d\n",
55147 + dieptsiz.b.pktcnt, dieptsiz.b.xfersize);
55148 +#endif
55149 +
55150 +#ifdef DISABLE_PERIODIC_EP
55151 + /*
55152 + * Set the NAK bit for this EP to
55153 + * start the disable process.
55154 + */
55155 + diepctl.d32 = 0;
55156 + diepctl.b.snak = 1;
55157 + DWC_MODIFY_REG32(&dev_if->in_ep_regs[num]->diepctl, diepctl.d32,
55158 + diepctl.d32);
55159 + ep->disabling = 1;
55160 + ep->stopped = 1;
55161 +#endif
55162 +}
55163 +
55164 +/**
55165 + * Handler for the IN EP NAK interrupt.
55166 + */
55167 +static inline int32_t handle_in_ep_nak_intr(dwc_otg_pcd_t * pcd,
55168 + const uint32_t epnum)
55169 +{
55170 + /** @todo implement ISR */
55171 + dwc_otg_core_if_t *core_if;
55172 + diepmsk_data_t intr_mask = {.d32 = 0 };
55173 +
55174 + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n", "IN EP NAK");
55175 + core_if = GET_CORE_IF(pcd);
55176 + intr_mask.b.nak = 1;
55177 +
55178 + if (core_if->multiproc_int_enable) {
55179 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
55180 + diepeachintmsk[epnum], intr_mask.d32, 0);
55181 + } else {
55182 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->diepmsk,
55183 + intr_mask.d32, 0);
55184 + }
55185 +
55186 + return 1;
55187 +}
55188 +
55189 +/**
55190 + * Handler for the OUT EP Babble interrupt.
55191 + */
55192 +static inline int32_t handle_out_ep_babble_intr(dwc_otg_pcd_t * pcd,
55193 + const uint32_t epnum)
55194 +{
55195 + /** @todo implement ISR */
55196 + dwc_otg_core_if_t *core_if;
55197 + doepmsk_data_t intr_mask = {.d32 = 0 };
55198 +
55199 + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n",
55200 + "OUT EP Babble");
55201 + core_if = GET_CORE_IF(pcd);
55202 + intr_mask.b.babble = 1;
55203 +
55204 + if (core_if->multiproc_int_enable) {
55205 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
55206 + doepeachintmsk[epnum], intr_mask.d32, 0);
55207 + } else {
55208 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->doepmsk,
55209 + intr_mask.d32, 0);
55210 + }
55211 +
55212 + return 1;
55213 +}
55214 +
55215 +/**
55216 + * Handler for the OUT EP NAK interrupt.
55217 + */
55218 +static inline int32_t handle_out_ep_nak_intr(dwc_otg_pcd_t * pcd,
55219 + const uint32_t epnum)
55220 +{
55221 + /** @todo implement ISR */
55222 + dwc_otg_core_if_t *core_if;
55223 + doepmsk_data_t intr_mask = {.d32 = 0 };
55224 +
55225 + DWC_DEBUGPL(DBG_ANY, "INTERRUPT Handler not implemented for %s\n", "OUT EP NAK");
55226 + core_if = GET_CORE_IF(pcd);
55227 + intr_mask.b.nak = 1;
55228 +
55229 + if (core_if->multiproc_int_enable) {
55230 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
55231 + doepeachintmsk[epnum], intr_mask.d32, 0);
55232 + } else {
55233 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->doepmsk,
55234 + intr_mask.d32, 0);
55235 + }
55236 +
55237 + return 1;
55238 +}
55239 +
55240 +/**
55241 + * Handler for the OUT EP NYET interrupt.
55242 + */
55243 +static inline int32_t handle_out_ep_nyet_intr(dwc_otg_pcd_t * pcd,
55244 + const uint32_t epnum)
55245 +{
55246 + /** @todo implement ISR */
55247 + dwc_otg_core_if_t *core_if;
55248 + doepmsk_data_t intr_mask = {.d32 = 0 };
55249 +
55250 + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n", "OUT EP NYET");
55251 + core_if = GET_CORE_IF(pcd);
55252 + intr_mask.b.nyet = 1;
55253 +
55254 + if (core_if->multiproc_int_enable) {
55255 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
55256 + doepeachintmsk[epnum], intr_mask.d32, 0);
55257 + } else {
55258 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->doepmsk,
55259 + intr_mask.d32, 0);
55260 + }
55261 +
55262 + return 1;
55263 +}
55264 +
55265 +/**
55266 + * This interrupt indicates that an IN EP has a pending Interrupt.
55267 + * The sequence for handling the IN EP interrupt is shown below:
55268 + * -# Read the Device All Endpoint Interrupt register
55269 + * -# Repeat the following for each IN EP interrupt bit set (from
55270 + * LSB to MSB).
55271 + * -# Read the Device Endpoint Interrupt (DIEPINTn) register
55272 + * -# If "Transfer Complete" call the request complete function
55273 + * -# If "Endpoint Disabled" complete the EP disable procedure.
55274 + * -# If "AHB Error Interrupt" log error
55275 + * -# If "Time-out Handshake" log error
55276 + * -# If "IN Token Received when TxFIFO Empty" write packet to Tx
55277 + * FIFO.
55278 + * -# If "IN Token EP Mismatch" (disable, this is handled by EP
55279 + * Mismatch Interrupt)
55280 + */
55281 +static int32_t dwc_otg_pcd_handle_in_ep_intr(dwc_otg_pcd_t * pcd)
55282 +{
55283 +#define CLEAR_IN_EP_INTR(__core_if,__epnum,__intr) \
55284 +do { \
55285 + diepint_data_t diepint = {.d32=0}; \
55286 + diepint.b.__intr = 1; \
55287 + DWC_WRITE_REG32(&__core_if->dev_if->in_ep_regs[__epnum]->diepint, \
55288 + diepint.d32); \
55289 +} while (0)
55290 +
55291 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
55292 + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
55293 + diepint_data_t diepint = {.d32 = 0 };
55294 + depctl_data_t depctl = {.d32 = 0 };
55295 + uint32_t ep_intr;
55296 + uint32_t epnum = 0;
55297 + dwc_otg_pcd_ep_t *ep;
55298 + dwc_ep_t *dwc_ep;
55299 + gintmsk_data_t intr_mask = {.d32 = 0 };
55300 +
55301 + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, pcd);
55302 +
55303 + /* Read in the device interrupt bits */
55304 + ep_intr = dwc_otg_read_dev_all_in_ep_intr(core_if);
55305 +
55306 + /* Service the Device IN interrupts for each endpoint */
55307 + while (ep_intr) {
55308 + if (ep_intr & 0x1) {
55309 + uint32_t empty_msk;
55310 + /* Get EP pointer */
55311 + ep = get_in_ep(pcd, epnum);
55312 + dwc_ep = &ep->dwc_ep;
55313 +
55314 + depctl.d32 =
55315 + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->diepctl);
55316 + empty_msk =
55317 + DWC_READ_REG32(&dev_if->
55318 + dev_global_regs->dtknqr4_fifoemptymsk);
55319 +
55320 + DWC_DEBUGPL(DBG_PCDV,
55321 + "IN EP INTERRUPT - %d\nepmty_msk - %8x diepctl - %8x\n",
55322 + epnum, empty_msk, depctl.d32);
55323 +
55324 + DWC_DEBUGPL(DBG_PCD,
55325 + "EP%d-%s: type=%d, mps=%d\n",
55326 + dwc_ep->num, (dwc_ep->is_in ? "IN" : "OUT"),
55327 + dwc_ep->type, dwc_ep->maxpacket);
55328 +
55329 + diepint.d32 =
55330 + dwc_otg_read_dev_in_ep_intr(core_if, dwc_ep);
55331 +
55332 + DWC_DEBUGPL(DBG_PCDV,
55333 + "EP %d Interrupt Register - 0x%x\n", epnum,
55334 + diepint.d32);
55335 + /* Transfer complete */
55336 + if (diepint.b.xfercompl) {
55337 + /* Disable the NP Tx FIFO Empty
55338 + * Interrupt */
55339 + if (core_if->en_multiple_tx_fifo == 0) {
55340 + intr_mask.b.nptxfempty = 1;
55341 + DWC_MODIFY_REG32
55342 + (&core_if->core_global_regs->gintmsk,
55343 + intr_mask.d32, 0);
55344 + } else {
55345 + /* Disable the Tx FIFO Empty Interrupt for this EP */
55346 + uint32_t fifoemptymsk =
55347 + 0x1 << dwc_ep->num;
55348 + DWC_MODIFY_REG32(&core_if->
55349 + dev_if->dev_global_regs->dtknqr4_fifoemptymsk,
55350 + fifoemptymsk, 0);
55351 + }
55352 + /* Clear the bit in DIEPINTn for this interrupt */
55353 + CLEAR_IN_EP_INTR(core_if, epnum, xfercompl);
55354 +
55355 + /* Complete the transfer */
55356 + if (epnum == 0) {
55357 + handle_ep0(pcd);
55358 + }
55359 +#ifdef DWC_EN_ISOC
55360 + else if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
55361 + if (!ep->stopped)
55362 + complete_iso_ep(pcd, ep);
55363 + }
55364 +#endif /* DWC_EN_ISOC */
55365 +#ifdef DWC_UTE_PER_IO
55366 + else if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
55367 + if (!ep->stopped)
55368 + complete_xiso_ep(ep);
55369 + }
55370 +#endif /* DWC_UTE_PER_IO */
55371 + else {
55372 + if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC &&
55373 + dwc_ep->bInterval > 1) {
55374 + dwc_ep->frame_num += dwc_ep->bInterval;
55375 + if (dwc_ep->frame_num > 0x3FFF)
55376 + {
55377 + dwc_ep->frm_overrun = 1;
55378 + dwc_ep->frame_num &= 0x3FFF;
55379 + } else
55380 + dwc_ep->frm_overrun = 0;
55381 + }
55382 + complete_ep(ep);
55383 + if(diepint.b.nak)
55384 + CLEAR_IN_EP_INTR(core_if, epnum, nak);
55385 + }
55386 + }
55387 + /* Endpoint disable */
55388 + if (diepint.b.epdisabled) {
55389 + DWC_DEBUGPL(DBG_ANY, "EP%d IN disabled\n",
55390 + epnum);
55391 + handle_in_ep_disable_intr(pcd, epnum);
55392 +
55393 + /* Clear the bit in DIEPINTn for this interrupt */
55394 + CLEAR_IN_EP_INTR(core_if, epnum, epdisabled);
55395 + }
55396 + /* AHB Error */
55397 + if (diepint.b.ahberr) {
55398 + DWC_ERROR("EP%d IN AHB Error\n", epnum);
55399 + /* Clear the bit in DIEPINTn for this interrupt */
55400 + CLEAR_IN_EP_INTR(core_if, epnum, ahberr);
55401 + }
55402 + /* TimeOUT Handshake (non-ISOC IN EPs) */
55403 + if (diepint.b.timeout) {
55404 + DWC_ERROR("EP%d IN Time-out\n", epnum);
55405 + handle_in_ep_timeout_intr(pcd, epnum);
55406 +
55407 + CLEAR_IN_EP_INTR(core_if, epnum, timeout);
55408 + }
55409 + /** IN Token received with TxF Empty */
55410 + if (diepint.b.intktxfemp) {
55411 + DWC_DEBUGPL(DBG_ANY,
55412 + "EP%d IN TKN TxFifo Empty\n",
55413 + epnum);
55414 + if (!ep->stopped && epnum != 0) {
55415 +
55416 + diepmsk_data_t diepmsk = {.d32 = 0 };
55417 + diepmsk.b.intktxfemp = 1;
55418 +
55419 + if (core_if->multiproc_int_enable) {
55420 + DWC_MODIFY_REG32
55421 + (&dev_if->dev_global_regs->diepeachintmsk
55422 + [epnum], diepmsk.d32, 0);
55423 + } else {
55424 + DWC_MODIFY_REG32
55425 + (&dev_if->dev_global_regs->diepmsk,
55426 + diepmsk.d32, 0);
55427 + }
55428 + } else if (core_if->dma_desc_enable
55429 + && epnum == 0
55430 + && pcd->ep0state ==
55431 + EP0_OUT_STATUS_PHASE) {
55432 + // EP0 IN set STALL
55433 + depctl.d32 =
55434 + DWC_READ_REG32(&dev_if->in_ep_regs
55435 + [epnum]->diepctl);
55436 +
55437 + /* set the disable and stall bits */
55438 + if (depctl.b.epena) {
55439 + depctl.b.epdis = 1;
55440 + }
55441 + depctl.b.stall = 1;
55442 + DWC_WRITE_REG32(&dev_if->in_ep_regs
55443 + [epnum]->diepctl,
55444 + depctl.d32);
55445 + }
55446 + CLEAR_IN_EP_INTR(core_if, epnum, intktxfemp);
55447 + }
55448 + /** IN Token Received with EP mismatch */
55449 + if (diepint.b.intknepmis) {
55450 + DWC_DEBUGPL(DBG_ANY,
55451 + "EP%d IN TKN EP Mismatch\n", epnum);
55452 + CLEAR_IN_EP_INTR(core_if, epnum, intknepmis);
55453 + }
55454 + /** IN Endpoint NAK Effective */
55455 + if (diepint.b.inepnakeff) {
55456 + DWC_DEBUGPL(DBG_ANY,
55457 + "EP%d IN EP NAK Effective\n",
55458 + epnum);
55459 + /* Periodic EP */
55460 + if (ep->disabling) {
55461 + depctl.d32 = 0;
55462 + depctl.b.snak = 1;
55463 + depctl.b.epdis = 1;
55464 + DWC_MODIFY_REG32(&dev_if->in_ep_regs
55465 + [epnum]->diepctl,
55466 + depctl.d32,
55467 + depctl.d32);
55468 + }
55469 + CLEAR_IN_EP_INTR(core_if, epnum, inepnakeff);
55470 +
55471 + }
55472 +
55473 + /** IN EP Tx FIFO Empty Intr */
55474 + if (diepint.b.emptyintr) {
55475 + DWC_DEBUGPL(DBG_ANY,
55476 + "EP%d Tx FIFO Empty Intr \n",
55477 + epnum);
55478 + write_empty_tx_fifo(pcd, epnum);
55479 +
55480 + CLEAR_IN_EP_INTR(core_if, epnum, emptyintr);
55481 +
55482 + }
55483 +
55484 + /** IN EP BNA Intr */
55485 + if (diepint.b.bna) {
55486 + CLEAR_IN_EP_INTR(core_if, epnum, bna);
55487 + if (core_if->dma_desc_enable) {
55488 +#ifdef DWC_EN_ISOC
55489 + if (dwc_ep->type ==
55490 + DWC_OTG_EP_TYPE_ISOC) {
55491 + /*
55492 + * This checking is performed to prevent first "false" BNA
55493 + * handling occuring right after reconnect
55494 + */
55495 + if (dwc_ep->next_frame !=
55496 + 0xffffffff)
55497 + dwc_otg_pcd_handle_iso_bna(ep);
55498 + } else
55499 +#endif /* DWC_EN_ISOC */
55500 + {
55501 + dwc_otg_pcd_handle_noniso_bna(ep);
55502 + }
55503 + }
55504 + }
55505 + /* NAK Interrutp */
55506 + if (diepint.b.nak) {
55507 + DWC_DEBUGPL(DBG_ANY, "EP%d IN NAK Interrupt\n",
55508 + epnum);
55509 + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
55510 + depctl_data_t depctl;
55511 + if (ep->dwc_ep.frame_num == 0xFFFFFFFF) {
55512 + ep->dwc_ep.frame_num = core_if->frame_num;
55513 + if (ep->dwc_ep.bInterval > 1) {
55514 + depctl.d32 = 0;
55515 + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->diepctl);
55516 + if (ep->dwc_ep.frame_num & 0x1) {
55517 + depctl.b.setd1pid = 1;
55518 + depctl.b.setd0pid = 0;
55519 + } else {
55520 + depctl.b.setd0pid = 1;
55521 + depctl.b.setd1pid = 0;
55522 + }
55523 + DWC_WRITE_REG32(&dev_if->in_ep_regs[epnum]->diepctl, depctl.d32);
55524 + }
55525 + start_next_request(ep);
55526 + }
55527 + ep->dwc_ep.frame_num += ep->dwc_ep.bInterval;
55528 + if (dwc_ep->frame_num > 0x3FFF) {
55529 + dwc_ep->frm_overrun = 1;
55530 + dwc_ep->frame_num &= 0x3FFF;
55531 + } else
55532 + dwc_ep->frm_overrun = 0;
55533 + }
55534 +
55535 + CLEAR_IN_EP_INTR(core_if, epnum, nak);
55536 + }
55537 + }
55538 + epnum++;
55539 + ep_intr >>= 1;
55540 + }
55541 +
55542 + return 1;
55543 +#undef CLEAR_IN_EP_INTR
55544 +}
55545 +
55546 +/**
55547 + * This interrupt indicates that an OUT EP has a pending Interrupt.
55548 + * The sequence for handling the OUT EP interrupt is shown below:
55549 + * -# Read the Device All Endpoint Interrupt register
55550 + * -# Repeat the following for each OUT EP interrupt bit set (from
55551 + * LSB to MSB).
55552 + * -# Read the Device Endpoint Interrupt (DOEPINTn) register
55553 + * -# If "Transfer Complete" call the request complete function
55554 + * -# If "Endpoint Disabled" complete the EP disable procedure.
55555 + * -# If "AHB Error Interrupt" log error
55556 + * -# If "Setup Phase Done" process Setup Packet (See Standard USB
55557 + * Command Processing)
55558 + */
55559 +static int32_t dwc_otg_pcd_handle_out_ep_intr(dwc_otg_pcd_t * pcd)
55560 +{
55561 +#define CLEAR_OUT_EP_INTR(__core_if,__epnum,__intr) \
55562 +do { \
55563 + doepint_data_t doepint = {.d32=0}; \
55564 + doepint.b.__intr = 1; \
55565 + DWC_WRITE_REG32(&__core_if->dev_if->out_ep_regs[__epnum]->doepint, \
55566 + doepint.d32); \
55567 +} while (0)
55568 +
55569 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
55570 + uint32_t ep_intr;
55571 + doepint_data_t doepint = {.d32 = 0 };
55572 + uint32_t epnum = 0;
55573 + dwc_otg_pcd_ep_t *ep;
55574 + dwc_ep_t *dwc_ep;
55575 + dctl_data_t dctl = {.d32 = 0 };
55576 + gintmsk_data_t gintmsk = {.d32 = 0 };
55577 +
55578 +
55579 + DWC_DEBUGPL(DBG_PCDV, "%s()\n", __func__);
55580 +
55581 + /* Read in the device interrupt bits */
55582 + ep_intr = dwc_otg_read_dev_all_out_ep_intr(core_if);
55583 +
55584 + while (ep_intr) {
55585 + if (ep_intr & 0x1) {
55586 + /* Get EP pointer */
55587 + ep = get_out_ep(pcd, epnum);
55588 + dwc_ep = &ep->dwc_ep;
55589 +
55590 +#ifdef VERBOSE
55591 + DWC_DEBUGPL(DBG_PCDV,
55592 + "EP%d-%s: type=%d, mps=%d\n",
55593 + dwc_ep->num, (dwc_ep->is_in ? "IN" : "OUT"),
55594 + dwc_ep->type, dwc_ep->maxpacket);
55595 +#endif
55596 + doepint.d32 =
55597 + dwc_otg_read_dev_out_ep_intr(core_if, dwc_ep);
55598 + /* Moved this interrupt upper due to core deffect of asserting
55599 + * OUT EP 0 xfercompl along with stsphsrcvd in BDMA */
55600 + if (doepint.b.stsphsercvd) {
55601 + deptsiz0_data_t deptsiz;
55602 + CLEAR_OUT_EP_INTR(core_if, epnum, stsphsercvd);
55603 + deptsiz.d32 =
55604 + DWC_READ_REG32(&core_if->dev_if->
55605 + out_ep_regs[0]->doeptsiz);
55606 + if (core_if->snpsid >= OTG_CORE_REV_3_00a
55607 + && core_if->dma_enable
55608 + && core_if->dma_desc_enable == 0
55609 + && doepint.b.xfercompl
55610 + && deptsiz.b.xfersize == 24) {
55611 + CLEAR_OUT_EP_INTR(core_if, epnum,
55612 + xfercompl);
55613 + doepint.b.xfercompl = 0;
55614 + ep0_out_start(core_if, pcd);
55615 + }
55616 + if ((core_if->dma_desc_enable) ||
55617 + (core_if->dma_enable
55618 + && core_if->snpsid >=
55619 + OTG_CORE_REV_3_00a)) {
55620 + do_setup_in_status_phase(pcd);
55621 + }
55622 + }
55623 + /* Transfer complete */
55624 + if (doepint.b.xfercompl) {
55625 +
55626 + if (epnum == 0) {
55627 + /* Clear the bit in DOEPINTn for this interrupt */
55628 + CLEAR_OUT_EP_INTR(core_if, epnum, xfercompl);
55629 + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
55630 + DWC_DEBUGPL(DBG_PCDV, "DOEPINT=%x doepint=%x\n",
55631 + DWC_READ_REG32(&core_if->dev_if->out_ep_regs[0]->doepint),
55632 + doepint.d32);
55633 + DWC_DEBUGPL(DBG_PCDV, "DOEPCTL=%x \n",
55634 + DWC_READ_REG32(&core_if->dev_if->out_ep_regs[0]->doepctl));
55635 +
55636 + if (core_if->snpsid >= OTG_CORE_REV_3_00a
55637 + && core_if->dma_enable == 0) {
55638 + doepint_data_t doepint;
55639 + doepint.d32 = DWC_READ_REG32(&core_if->dev_if->
55640 + out_ep_regs[0]->doepint);
55641 + if (pcd->ep0state == EP0_IDLE && doepint.b.sr) {
55642 + CLEAR_OUT_EP_INTR(core_if, epnum, sr);
55643 + goto exit_xfercompl;
55644 + }
55645 + }
55646 + /* In case of DDMA look at SR bit to go to the Data Stage */
55647 + if (core_if->dma_desc_enable) {
55648 + dev_dma_desc_sts_t status = {.d32 = 0};
55649 + if (pcd->ep0state == EP0_IDLE) {
55650 + status.d32 = core_if->dev_if->setup_desc_addr[core_if->
55651 + dev_if->setup_desc_index]->status.d32;
55652 + if(pcd->data_terminated) {
55653 + pcd->data_terminated = 0;
55654 + status.d32 = core_if->dev_if->out_desc_addr->status.d32;
55655 + dwc_memcpy(&pcd->setup_pkt->req, pcd->backup_buf, 8);
55656 + }
55657 + if (status.b.sr) {
55658 + if (doepint.b.setup) {
55659 + DWC_DEBUGPL(DBG_PCDV, "DMA DESC EP0_IDLE SR=1 setup=1\n");
55660 + /* Already started data stage, clear setup */
55661 + CLEAR_OUT_EP_INTR(core_if, epnum, setup);
55662 + doepint.b.setup = 0;
55663 + handle_ep0(pcd);
55664 + /* Prepare for more setup packets */
55665 + if (pcd->ep0state == EP0_IN_STATUS_PHASE ||
55666 + pcd->ep0state == EP0_IN_DATA_PHASE) {
55667 + ep0_out_start(core_if, pcd);
55668 + }
55669 +
55670 + goto exit_xfercompl;
55671 + } else {
55672 + /* Prepare for more setup packets */
55673 + DWC_DEBUGPL(DBG_PCDV,
55674 + "EP0_IDLE SR=1 setup=0 new setup comes\n");
55675 + ep0_out_start(core_if, pcd);
55676 + }
55677 + }
55678 + } else {
55679 + dwc_otg_pcd_request_t *req;
55680 + dev_dma_desc_sts_t status = {.d32 = 0};
55681 + diepint_data_t diepint0;
55682 + diepint0.d32 = DWC_READ_REG32(&core_if->dev_if->
55683 + in_ep_regs[0]->diepint);
55684 +
55685 + if (pcd->ep0state == EP0_STALL || pcd->ep0state == EP0_DISCONNECT) {
55686 + DWC_ERROR("EP0 is stalled/disconnected\n");
55687 + }
55688 +
55689 + /* Clear IN xfercompl if set */
55690 + if (diepint0.b.xfercompl && (pcd->ep0state == EP0_IN_STATUS_PHASE
55691 + || pcd->ep0state == EP0_IN_DATA_PHASE)) {
55692 + DWC_WRITE_REG32(&core_if->dev_if->
55693 + in_ep_regs[0]->diepint, diepint0.d32);
55694 + }
55695 +
55696 + status.d32 = core_if->dev_if->setup_desc_addr[core_if->
55697 + dev_if->setup_desc_index]->status.d32;
55698 +
55699 + if (ep->dwc_ep.xfer_count != ep->dwc_ep.total_len
55700 + && (pcd->ep0state == EP0_OUT_DATA_PHASE))
55701 + status.d32 = core_if->dev_if->out_desc_addr->status.d32;
55702 + if (pcd->ep0state == EP0_OUT_STATUS_PHASE)
55703 + status.d32 = core_if->dev_if->
55704 + out_desc_addr->status.d32;
55705 +
55706 + if (status.b.sr) {
55707 + if (DWC_CIRCLEQ_EMPTY(&ep->queue)) {
55708 + DWC_DEBUGPL(DBG_PCDV, "Request queue empty!!\n");
55709 + } else {
55710 + DWC_DEBUGPL(DBG_PCDV, "complete req!!\n");
55711 + req = DWC_CIRCLEQ_FIRST(&ep->queue);
55712 + if (ep->dwc_ep.xfer_count != ep->dwc_ep.total_len &&
55713 + pcd->ep0state == EP0_OUT_DATA_PHASE) {
55714 + /* Read arrived setup packet from req->buf */
55715 + dwc_memcpy(&pcd->setup_pkt->req,
55716 + req->buf + ep->dwc_ep.xfer_count, 8);
55717 + }
55718 + req->actual = ep->dwc_ep.xfer_count;
55719 + dwc_otg_request_done(ep, req, -ECONNRESET);
55720 + ep->dwc_ep.start_xfer_buff = 0;
55721 + ep->dwc_ep.xfer_buff = 0;
55722 + ep->dwc_ep.xfer_len = 0;
55723 + }
55724 + pcd->ep0state = EP0_IDLE;
55725 + if (doepint.b.setup) {
55726 + DWC_DEBUGPL(DBG_PCDV, "EP0_IDLE SR=1 setup=1\n");
55727 + /* Data stage started, clear setup */
55728 + CLEAR_OUT_EP_INTR(core_if, epnum, setup);
55729 + doepint.b.setup = 0;
55730 + handle_ep0(pcd);
55731 + /* Prepare for setup packets if ep0in was enabled*/
55732 + if (pcd->ep0state == EP0_IN_STATUS_PHASE) {
55733 + ep0_out_start(core_if, pcd);
55734 + }
55735 +
55736 + goto exit_xfercompl;
55737 + } else {
55738 + /* Prepare for more setup packets */
55739 + DWC_DEBUGPL(DBG_PCDV,
55740 + "EP0_IDLE SR=1 setup=0 new setup comes 2\n");
55741 + ep0_out_start(core_if, pcd);
55742 + }
55743 + }
55744 + }
55745 + }
55746 + if (core_if->snpsid >= OTG_CORE_REV_2_94a && core_if->dma_enable
55747 + && core_if->dma_desc_enable == 0) {
55748 + doepint_data_t doepint_temp = {.d32 = 0};
55749 + deptsiz0_data_t doeptsize0 = {.d32 = 0 };
55750 + doepint_temp.d32 = DWC_READ_REG32(&core_if->dev_if->
55751 + out_ep_regs[ep->dwc_ep.num]->doepint);
55752 + doeptsize0.d32 = DWC_READ_REG32(&core_if->dev_if->
55753 + out_ep_regs[ep->dwc_ep.num]->doeptsiz);
55754 + if (pcd->ep0state == EP0_IDLE) {
55755 + if (doepint_temp.b.sr) {
55756 + CLEAR_OUT_EP_INTR(core_if, epnum, sr);
55757 + }
55758 + doepint.d32 = DWC_READ_REG32(&core_if->dev_if->
55759 + out_ep_regs[0]->doepint);
55760 + if (doeptsize0.b.supcnt == 3) {
55761 + DWC_DEBUGPL(DBG_ANY, "Rolling over!!!!!!!\n");
55762 + ep->dwc_ep.stp_rollover = 1;
55763 + }
55764 + if (doepint.b.setup) {
55765 +retry:
55766 + /* Already started data stage, clear setup */
55767 + CLEAR_OUT_EP_INTR(core_if, epnum, setup);
55768 + doepint.b.setup = 0;
55769 + handle_ep0(pcd);
55770 + ep->dwc_ep.stp_rollover = 0;
55771 + /* Prepare for more setup packets */
55772 + if (pcd->ep0state == EP0_IN_STATUS_PHASE ||
55773 + pcd->ep0state == EP0_IN_DATA_PHASE) {
55774 + ep0_out_start(core_if, pcd);
55775 + }
55776 + goto exit_xfercompl;
55777 + } else {
55778 + /* Prepare for more setup packets */
55779 + DWC_DEBUGPL(DBG_ANY,
55780 + "EP0_IDLE SR=1 setup=0 new setup comes\n");
55781 + doepint.d32 = DWC_READ_REG32(&core_if->dev_if->
55782 + out_ep_regs[0]->doepint);
55783 + if(doepint.b.setup)
55784 + goto retry;
55785 + ep0_out_start(core_if, pcd);
55786 + }
55787 + } else {
55788 + dwc_otg_pcd_request_t *req;
55789 + diepint_data_t diepint0 = {.d32 = 0};
55790 + doepint_data_t doepint_temp = {.d32 = 0};
55791 + depctl_data_t diepctl0;
55792 + diepint0.d32 = DWC_READ_REG32(&core_if->dev_if->
55793 + in_ep_regs[0]->diepint);
55794 + diepctl0.d32 = DWC_READ_REG32(&core_if->dev_if->
55795 + in_ep_regs[0]->diepctl);
55796 +
55797 + if (pcd->ep0state == EP0_IN_DATA_PHASE
55798 + || pcd->ep0state == EP0_IN_STATUS_PHASE) {
55799 + if (diepint0.b.xfercompl) {
55800 + DWC_WRITE_REG32(&core_if->dev_if->
55801 + in_ep_regs[0]->diepint, diepint0.d32);
55802 + }
55803 + if (diepctl0.b.epena) {
55804 + diepint_data_t diepint = {.d32 = 0};
55805 + diepctl0.b.snak = 1;
55806 + DWC_WRITE_REG32(&core_if->dev_if->
55807 + in_ep_regs[0]->diepctl, diepctl0.d32);
55808 + do {
55809 + dwc_udelay(10);
55810 + diepint.d32 = DWC_READ_REG32(&core_if->dev_if->
55811 + in_ep_regs[0]->diepint);
55812 + } while (!diepint.b.inepnakeff);
55813 + diepint.b.inepnakeff = 1;
55814 + DWC_WRITE_REG32(&core_if->dev_if->
55815 + in_ep_regs[0]->diepint, diepint.d32);
55816 + diepctl0.d32 = 0;
55817 + diepctl0.b.epdis = 1;
55818 + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[0]->diepctl,
55819 + diepctl0.d32);
55820 + do {
55821 + dwc_udelay(10);
55822 + diepint.d32 = DWC_READ_REG32(&core_if->dev_if->
55823 + in_ep_regs[0]->diepint);
55824 + } while (!diepint.b.epdisabled);
55825 + diepint.b.epdisabled = 1;
55826 + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[0]->diepint,
55827 + diepint.d32);
55828 + }
55829 + }
55830 + doepint_temp.d32 = DWC_READ_REG32(&core_if->dev_if->
55831 + out_ep_regs[ep->dwc_ep.num]->doepint);
55832 + if (doepint_temp.b.sr) {
55833 + CLEAR_OUT_EP_INTR(core_if, epnum, sr);
55834 + if (DWC_CIRCLEQ_EMPTY(&ep->queue)) {
55835 + DWC_DEBUGPL(DBG_PCDV, "Request queue empty!!\n");
55836 + } else {
55837 + DWC_DEBUGPL(DBG_PCDV, "complete req!!\n");
55838 + req = DWC_CIRCLEQ_FIRST(&ep->queue);
55839 + if (ep->dwc_ep.xfer_count != ep->dwc_ep.total_len &&
55840 + pcd->ep0state == EP0_OUT_DATA_PHASE) {
55841 + /* Read arrived setup packet from req->buf */
55842 + dwc_memcpy(&pcd->setup_pkt->req,
55843 + req->buf + ep->dwc_ep.xfer_count, 8);
55844 + }
55845 + req->actual = ep->dwc_ep.xfer_count;
55846 + dwc_otg_request_done(ep, req, -ECONNRESET);
55847 + ep->dwc_ep.start_xfer_buff = 0;
55848 + ep->dwc_ep.xfer_buff = 0;
55849 + ep->dwc_ep.xfer_len = 0;
55850 + }
55851 + pcd->ep0state = EP0_IDLE;
55852 + if (doepint.b.setup) {
55853 + DWC_DEBUGPL(DBG_PCDV, "EP0_IDLE SR=1 setup=1\n");
55854 + /* Data stage started, clear setup */
55855 + CLEAR_OUT_EP_INTR(core_if, epnum, setup);
55856 + doepint.b.setup = 0;
55857 + handle_ep0(pcd);
55858 + /* Prepare for setup packets if ep0in was enabled*/
55859 + if (pcd->ep0state == EP0_IN_STATUS_PHASE) {
55860 + ep0_out_start(core_if, pcd);
55861 + }
55862 + goto exit_xfercompl;
55863 + } else {
55864 + /* Prepare for more setup packets */
55865 + DWC_DEBUGPL(DBG_PCDV,
55866 + "EP0_IDLE SR=1 setup=0 new setup comes 2\n");
55867 + ep0_out_start(core_if, pcd);
55868 + }
55869 + }
55870 + }
55871 + }
55872 + if (core_if->dma_enable == 0 || pcd->ep0state != EP0_IDLE)
55873 + handle_ep0(pcd);
55874 +exit_xfercompl:
55875 + DWC_DEBUGPL(DBG_PCDV, "DOEPINT=%x doepint=%x\n",
55876 + dwc_otg_read_dev_out_ep_intr(core_if, dwc_ep), doepint.d32);
55877 + } else {
55878 + if (core_if->dma_desc_enable == 0
55879 + || pcd->ep0state != EP0_IDLE)
55880 + handle_ep0(pcd);
55881 + }
55882 +#ifdef DWC_EN_ISOC
55883 + } else if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
55884 + if (doepint.b.pktdrpsts == 0) {
55885 + /* Clear the bit in DOEPINTn for this interrupt */
55886 + CLEAR_OUT_EP_INTR(core_if,
55887 + epnum,
55888 + xfercompl);
55889 + complete_iso_ep(pcd, ep);
55890 + } else {
55891 +
55892 + doepint_data_t doepint = {.d32 = 0 };
55893 + doepint.b.xfercompl = 1;
55894 + doepint.b.pktdrpsts = 1;
55895 + DWC_WRITE_REG32
55896 + (&core_if->dev_if->out_ep_regs
55897 + [epnum]->doepint,
55898 + doepint.d32);
55899 + if (handle_iso_out_pkt_dropped
55900 + (core_if, dwc_ep)) {
55901 + complete_iso_ep(pcd,
55902 + ep);
55903 + }
55904 + }
55905 +#endif /* DWC_EN_ISOC */
55906 +#ifdef DWC_UTE_PER_IO
55907 + } else if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
55908 + CLEAR_OUT_EP_INTR(core_if, epnum, xfercompl);
55909 + if (!ep->stopped)
55910 + complete_xiso_ep(ep);
55911 +#endif /* DWC_UTE_PER_IO */
55912 + } else {
55913 + /* Clear the bit in DOEPINTn for this interrupt */
55914 + CLEAR_OUT_EP_INTR(core_if, epnum,
55915 + xfercompl);
55916 +
55917 + if (core_if->core_params->dev_out_nak) {
55918 + DWC_TIMER_CANCEL(pcd->core_if->ep_xfer_timer[epnum]);
55919 + pcd->core_if->ep_xfer_info[epnum].state = 0;
55920 +#ifdef DEBUG
55921 + print_memory_payload(pcd, dwc_ep);
55922 +#endif
55923 + }
55924 + complete_ep(ep);
55925 + }
55926 +
55927 + }
55928 +
55929 + /* Endpoint disable */
55930 + if (doepint.b.epdisabled) {
55931 +
55932 + /* Clear the bit in DOEPINTn for this interrupt */
55933 + CLEAR_OUT_EP_INTR(core_if, epnum, epdisabled);
55934 + if (core_if->core_params->dev_out_nak) {
55935 +#ifdef DEBUG
55936 + print_memory_payload(pcd, dwc_ep);
55937 +#endif
55938 + /* In case of timeout condition */
55939 + if (core_if->ep_xfer_info[epnum].state == 2) {
55940 + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->
55941 + dev_global_regs->dctl);
55942 + dctl.b.cgoutnak = 1;
55943 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl,
55944 + dctl.d32);
55945 + /* Unmask goutnakeff interrupt which was masked
55946 + * during handle nak out interrupt */
55947 + gintmsk.b.goutnakeff = 1;
55948 + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk,
55949 + 0, gintmsk.d32);
55950 +
55951 + complete_ep(ep);
55952 + }
55953 + }
55954 + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC)
55955 + {
55956 + dctl_data_t dctl;
55957 + gintmsk_data_t intr_mask = {.d32 = 0};
55958 + dwc_otg_pcd_request_t *req = 0;
55959 +
55960 + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->
55961 + dev_global_regs->dctl);
55962 + dctl.b.cgoutnak = 1;
55963 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl,
55964 + dctl.d32);
55965 +
55966 + intr_mask.d32 = 0;
55967 + intr_mask.b.incomplisoout = 1;
55968 +
55969 + /* Get any pending requests */
55970 + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
55971 + req = DWC_CIRCLEQ_FIRST(&ep->queue);
55972 + if (!req) {
55973 + DWC_PRINTF("complete_ep 0x%p, req = NULL!\n", ep);
55974 + } else {
55975 + dwc_otg_request_done(ep, req, 0);
55976 + start_next_request(ep);
55977 + }
55978 + } else {
55979 + DWC_PRINTF("complete_ep 0x%p, ep->queue empty!\n", ep);
55980 + }
55981 + }
55982 + }
55983 + /* AHB Error */
55984 + if (doepint.b.ahberr) {
55985 + DWC_ERROR("EP%d OUT AHB Error\n", epnum);
55986 + DWC_ERROR("EP%d DEPDMA=0x%08x \n",
55987 + epnum, core_if->dev_if->out_ep_regs[epnum]->doepdma);
55988 + CLEAR_OUT_EP_INTR(core_if, epnum, ahberr);
55989 + }
55990 + /* Setup Phase Done (contorl EPs) */
55991 + if (doepint.b.setup) {
55992 +#ifdef DEBUG_EP0
55993 + DWC_DEBUGPL(DBG_PCD, "EP%d SETUP Done\n", epnum);
55994 +#endif
55995 + CLEAR_OUT_EP_INTR(core_if, epnum, setup);
55996 +
55997 + handle_ep0(pcd);
55998 + }
55999 +
56000 + /** OUT EP BNA Intr */
56001 + if (doepint.b.bna) {
56002 + CLEAR_OUT_EP_INTR(core_if, epnum, bna);
56003 + if (core_if->dma_desc_enable) {
56004 +#ifdef DWC_EN_ISOC
56005 + if (dwc_ep->type ==
56006 + DWC_OTG_EP_TYPE_ISOC) {
56007 + /*
56008 + * This checking is performed to prevent first "false" BNA
56009 + * handling occuring right after reconnect
56010 + */
56011 + if (dwc_ep->next_frame !=
56012 + 0xffffffff)
56013 + dwc_otg_pcd_handle_iso_bna(ep);
56014 + } else
56015 +#endif /* DWC_EN_ISOC */
56016 + {
56017 + dwc_otg_pcd_handle_noniso_bna(ep);
56018 + }
56019 + }
56020 + }
56021 + /* Babble Interrupt */
56022 + if (doepint.b.babble) {
56023 + DWC_DEBUGPL(DBG_ANY, "EP%d OUT Babble\n",
56024 + epnum);
56025 + handle_out_ep_babble_intr(pcd, epnum);
56026 +
56027 + CLEAR_OUT_EP_INTR(core_if, epnum, babble);
56028 + }
56029 + if (doepint.b.outtknepdis) {
56030 + DWC_DEBUGPL(DBG_ANY, "EP%d OUT Token received when EP is \
56031 + disabled\n",epnum);
56032 + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
56033 + doepmsk_data_t doepmsk = {.d32 = 0};
56034 + ep->dwc_ep.frame_num = core_if->frame_num;
56035 + if (ep->dwc_ep.bInterval > 1) {
56036 + depctl_data_t depctl;
56037 + depctl.d32 = DWC_READ_REG32(&core_if->dev_if->
56038 + out_ep_regs[epnum]->doepctl);
56039 + if (ep->dwc_ep.frame_num & 0x1) {
56040 + depctl.b.setd1pid = 1;
56041 + depctl.b.setd0pid = 0;
56042 + } else {
56043 + depctl.b.setd0pid = 1;
56044 + depctl.b.setd1pid = 0;
56045 + }
56046 + DWC_WRITE_REG32(&core_if->dev_if->
56047 + out_ep_regs[epnum]->doepctl, depctl.d32);
56048 + }
56049 + start_next_request(ep);
56050 + doepmsk.b.outtknepdis = 1;
56051 + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->doepmsk,
56052 + doepmsk.d32, 0);
56053 + }
56054 + CLEAR_OUT_EP_INTR(core_if, epnum, outtknepdis);
56055 + }
56056 +
56057 + /* NAK Interrutp */
56058 + if (doepint.b.nak) {
56059 + DWC_DEBUGPL(DBG_ANY, "EP%d OUT NAK\n", epnum);
56060 + handle_out_ep_nak_intr(pcd, epnum);
56061 +
56062 + CLEAR_OUT_EP_INTR(core_if, epnum, nak);
56063 + }
56064 + /* NYET Interrutp */
56065 + if (doepint.b.nyet) {
56066 + DWC_DEBUGPL(DBG_ANY, "EP%d OUT NYET\n", epnum);
56067 + handle_out_ep_nyet_intr(pcd, epnum);
56068 +
56069 + CLEAR_OUT_EP_INTR(core_if, epnum, nyet);
56070 + }
56071 + }
56072 +
56073 + epnum++;
56074 + ep_intr >>= 1;
56075 + }
56076 +
56077 + return 1;
56078 +
56079 +#undef CLEAR_OUT_EP_INTR
56080 +}
56081 +static int drop_transfer(uint32_t trgt_fr, uint32_t curr_fr, uint8_t frm_overrun)
56082 +{
56083 + int retval = 0;
56084 + if(!frm_overrun && curr_fr >= trgt_fr)
56085 + retval = 1;
56086 + else if (frm_overrun
56087 + && (curr_fr >= trgt_fr && ((curr_fr - trgt_fr) < 0x3FFF / 2)))
56088 + retval = 1;
56089 + return retval;
56090 +}
56091 +/**
56092 + * Incomplete ISO IN Transfer Interrupt.
56093 + * This interrupt indicates one of the following conditions occurred
56094 + * while transmitting an ISOC transaction.
56095 + * - Corrupted IN Token for ISOC EP.
56096 + * - Packet not complete in FIFO.
56097 + * The follow actions will be taken:
56098 + * -# Determine the EP
56099 + * -# Set incomplete flag in dwc_ep structure
56100 + * -# Disable EP; when "Endpoint Disabled" interrupt is received
56101 + * Flush FIFO
56102 + */
56103 +int32_t dwc_otg_pcd_handle_incomplete_isoc_in_intr(dwc_otg_pcd_t * pcd)
56104 +{
56105 + gintsts_data_t gintsts;
56106 +
56107 +#ifdef DWC_EN_ISOC
56108 + dwc_otg_dev_if_t *dev_if;
56109 + deptsiz_data_t deptsiz = {.d32 = 0 };
56110 + depctl_data_t depctl = {.d32 = 0 };
56111 + dsts_data_t dsts = {.d32 = 0 };
56112 + dwc_ep_t *dwc_ep;
56113 + int i;
56114 +
56115 + dev_if = GET_CORE_IF(pcd)->dev_if;
56116 +
56117 + for (i = 1; i <= dev_if->num_in_eps; ++i) {
56118 + dwc_ep = &pcd->in_ep[i].dwc_ep;
56119 + if (dwc_ep->active && dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
56120 + deptsiz.d32 =
56121 + DWC_READ_REG32(&dev_if->in_ep_regs[i]->dieptsiz);
56122 + depctl.d32 =
56123 + DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
56124 +
56125 + if (depctl.b.epdis && deptsiz.d32) {
56126 + set_current_pkt_info(GET_CORE_IF(pcd), dwc_ep);
56127 + if (dwc_ep->cur_pkt >= dwc_ep->pkt_cnt) {
56128 + dwc_ep->cur_pkt = 0;
56129 + dwc_ep->proc_buf_num =
56130 + (dwc_ep->proc_buf_num ^ 1) & 0x1;
56131 +
56132 + if (dwc_ep->proc_buf_num) {
56133 + dwc_ep->cur_pkt_addr =
56134 + dwc_ep->xfer_buff1;
56135 + dwc_ep->cur_pkt_dma_addr =
56136 + dwc_ep->dma_addr1;
56137 + } else {
56138 + dwc_ep->cur_pkt_addr =
56139 + dwc_ep->xfer_buff0;
56140 + dwc_ep->cur_pkt_dma_addr =
56141 + dwc_ep->dma_addr0;
56142 + }
56143 +
56144 + }
56145 +
56146 + dsts.d32 =
56147 + DWC_READ_REG32(&GET_CORE_IF(pcd)->dev_if->
56148 + dev_global_regs->dsts);
56149 + dwc_ep->next_frame = dsts.b.soffn;
56150 +
56151 + dwc_otg_iso_ep_start_frm_transfer(GET_CORE_IF
56152 + (pcd),
56153 + dwc_ep);
56154 + }
56155 + }
56156 + }
56157 +
56158 +#else
56159 + depctl_data_t depctl = {.d32 = 0 };
56160 + dwc_ep_t *dwc_ep;
56161 + dwc_otg_dev_if_t *dev_if;
56162 + int i;
56163 + dev_if = GET_CORE_IF(pcd)->dev_if;
56164 +
56165 + DWC_DEBUGPL(DBG_PCD,"Incomplete ISO IN \n");
56166 +
56167 + for (i = 1; i <= dev_if->num_in_eps; ++i) {
56168 + dwc_ep = &pcd->in_ep[i-1].dwc_ep;
56169 + depctl.d32 =
56170 + DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
56171 + if (depctl.b.epena && dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
56172 + if (drop_transfer(dwc_ep->frame_num, GET_CORE_IF(pcd)->frame_num,
56173 + dwc_ep->frm_overrun))
56174 + {
56175 + depctl.d32 =
56176 + DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
56177 + depctl.b.snak = 1;
56178 + depctl.b.epdis = 1;
56179 + DWC_MODIFY_REG32(&dev_if->in_ep_regs[i]->diepctl, depctl.d32, depctl.d32);
56180 + }
56181 + }
56182 + }
56183 +
56184 + /*intr_mask.b.incomplisoin = 1;
56185 + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
56186 + intr_mask.d32, 0); */
56187 +#endif //DWC_EN_ISOC
56188 +
56189 + /* Clear interrupt */
56190 + gintsts.d32 = 0;
56191 + gintsts.b.incomplisoin = 1;
56192 + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
56193 + gintsts.d32);
56194 +
56195 + return 1;
56196 +}
56197 +
56198 +/**
56199 + * Incomplete ISO OUT Transfer Interrupt.
56200 + *
56201 + * This interrupt indicates that the core has dropped an ISO OUT
56202 + * packet. The following conditions can be the cause:
56203 + * - FIFO Full, the entire packet would not fit in the FIFO.
56204 + * - CRC Error
56205 + * - Corrupted Token
56206 + * The follow actions will be taken:
56207 + * -# Determine the EP
56208 + * -# Set incomplete flag in dwc_ep structure
56209 + * -# Read any data from the FIFO
56210 + * -# Disable EP. When "Endpoint Disabled" interrupt is received
56211 + * re-enable EP.
56212 + */
56213 +int32_t dwc_otg_pcd_handle_incomplete_isoc_out_intr(dwc_otg_pcd_t * pcd)
56214 +{
56215 +
56216 + gintsts_data_t gintsts;
56217 +
56218 +#ifdef DWC_EN_ISOC
56219 + dwc_otg_dev_if_t *dev_if;
56220 + deptsiz_data_t deptsiz = {.d32 = 0 };
56221 + depctl_data_t depctl = {.d32 = 0 };
56222 + dsts_data_t dsts = {.d32 = 0 };
56223 + dwc_ep_t *dwc_ep;
56224 + int i;
56225 +
56226 + dev_if = GET_CORE_IF(pcd)->dev_if;
56227 +
56228 + for (i = 1; i <= dev_if->num_out_eps; ++i) {
56229 + dwc_ep = &pcd->in_ep[i].dwc_ep;
56230 + if (pcd->out_ep[i].dwc_ep.active &&
56231 + pcd->out_ep[i].dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
56232 + deptsiz.d32 =
56233 + DWC_READ_REG32(&dev_if->out_ep_regs[i]->doeptsiz);
56234 + depctl.d32 =
56235 + DWC_READ_REG32(&dev_if->out_ep_regs[i]->doepctl);
56236 +
56237 + if (depctl.b.epdis && deptsiz.d32) {
56238 + set_current_pkt_info(GET_CORE_IF(pcd),
56239 + &pcd->out_ep[i].dwc_ep);
56240 + if (dwc_ep->cur_pkt >= dwc_ep->pkt_cnt) {
56241 + dwc_ep->cur_pkt = 0;
56242 + dwc_ep->proc_buf_num =
56243 + (dwc_ep->proc_buf_num ^ 1) & 0x1;
56244 +
56245 + if (dwc_ep->proc_buf_num) {
56246 + dwc_ep->cur_pkt_addr =
56247 + dwc_ep->xfer_buff1;
56248 + dwc_ep->cur_pkt_dma_addr =
56249 + dwc_ep->dma_addr1;
56250 + } else {
56251 + dwc_ep->cur_pkt_addr =
56252 + dwc_ep->xfer_buff0;
56253 + dwc_ep->cur_pkt_dma_addr =
56254 + dwc_ep->dma_addr0;
56255 + }
56256 +
56257 + }
56258 +
56259 + dsts.d32 =
56260 + DWC_READ_REG32(&GET_CORE_IF(pcd)->dev_if->
56261 + dev_global_regs->dsts);
56262 + dwc_ep->next_frame = dsts.b.soffn;
56263 +
56264 + dwc_otg_iso_ep_start_frm_transfer(GET_CORE_IF
56265 + (pcd),
56266 + dwc_ep);
56267 + }
56268 + }
56269 + }
56270 +#else
56271 + /** @todo implement ISR */
56272 + gintmsk_data_t intr_mask = {.d32 = 0 };
56273 + dwc_otg_core_if_t *core_if;
56274 + deptsiz_data_t deptsiz = {.d32 = 0 };
56275 + depctl_data_t depctl = {.d32 = 0 };
56276 + dctl_data_t dctl = {.d32 = 0 };
56277 + dwc_ep_t *dwc_ep = NULL;
56278 + int i;
56279 + core_if = GET_CORE_IF(pcd);
56280 +
56281 + for (i = 0; i < core_if->dev_if->num_out_eps; ++i) {
56282 + dwc_ep = &pcd->out_ep[i].dwc_ep;
56283 + depctl.d32 =
56284 + DWC_READ_REG32(&core_if->dev_if->out_ep_regs[dwc_ep->num]->doepctl);
56285 + if (depctl.b.epena && depctl.b.dpid == (core_if->frame_num & 0x1)) {
56286 + core_if->dev_if->isoc_ep = dwc_ep;
56287 + deptsiz.d32 =
56288 + DWC_READ_REG32(&core_if->dev_if->out_ep_regs[dwc_ep->num]->doeptsiz);
56289 + break;
56290 + }
56291 + }
56292 + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dctl);
56293 + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
56294 + intr_mask.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
56295 +
56296 + if (!intr_mask.b.goutnakeff) {
56297 + /* Unmask it */
56298 + intr_mask.b.goutnakeff = 1;
56299 + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, intr_mask.d32);
56300 + }
56301 + if (!gintsts.b.goutnakeff) {
56302 + dctl.b.sgoutnak = 1;
56303 + }
56304 + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32);
56305 +
56306 + depctl.d32 = DWC_READ_REG32(&core_if->dev_if->out_ep_regs[dwc_ep->num]->doepctl);
56307 + if (depctl.b.epena) {
56308 + depctl.b.epdis = 1;
56309 + depctl.b.snak = 1;
56310 + }
56311 + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[dwc_ep->num]->doepctl, depctl.d32);
56312 +
56313 + intr_mask.d32 = 0;
56314 + intr_mask.b.incomplisoout = 1;
56315 +
56316 +#endif /* DWC_EN_ISOC */
56317 +
56318 + /* Clear interrupt */
56319 + gintsts.d32 = 0;
56320 + gintsts.b.incomplisoout = 1;
56321 + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
56322 + gintsts.d32);
56323 +
56324 + return 1;
56325 +}
56326 +
56327 +/**
56328 + * This function handles the Global IN NAK Effective interrupt.
56329 + *
56330 + */
56331 +int32_t dwc_otg_pcd_handle_in_nak_effective(dwc_otg_pcd_t * pcd)
56332 +{
56333 + dwc_otg_dev_if_t *dev_if = GET_CORE_IF(pcd)->dev_if;
56334 + depctl_data_t diepctl = {.d32 = 0 };
56335 + gintmsk_data_t intr_mask = {.d32 = 0 };
56336 + gintsts_data_t gintsts;
56337 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
56338 + int i;
56339 +
56340 + DWC_DEBUGPL(DBG_PCD, "Global IN NAK Effective\n");
56341 +
56342 + /* Disable all active IN EPs */
56343 + for (i = 0; i <= dev_if->num_in_eps; i++) {
56344 + diepctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
56345 + if (!(diepctl.b.eptype & 1) && diepctl.b.epena) {
56346 + if (core_if->start_predict > 0)
56347 + core_if->start_predict++;
56348 + diepctl.b.epdis = 1;
56349 + diepctl.b.snak = 1;
56350 + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepctl, diepctl.d32);
56351 + }
56352 + }
56353 +
56354 +
56355 + /* Disable the Global IN NAK Effective Interrupt */
56356 + intr_mask.b.ginnakeff = 1;
56357 + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
56358 + intr_mask.d32, 0);
56359 +
56360 + /* Clear interrupt */
56361 + gintsts.d32 = 0;
56362 + gintsts.b.ginnakeff = 1;
56363 + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
56364 + gintsts.d32);
56365 +
56366 + return 1;
56367 +}
56368 +
56369 +/**
56370 + * OUT NAK Effective.
56371 + *
56372 + */
56373 +int32_t dwc_otg_pcd_handle_out_nak_effective(dwc_otg_pcd_t * pcd)
56374 +{
56375 + dwc_otg_dev_if_t *dev_if = GET_CORE_IF(pcd)->dev_if;
56376 + gintmsk_data_t intr_mask = {.d32 = 0 };
56377 + gintsts_data_t gintsts;
56378 + depctl_data_t doepctl;
56379 + int i;
56380 +
56381 + /* Disable the Global OUT NAK Effective Interrupt */
56382 + intr_mask.b.goutnakeff = 1;
56383 + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
56384 + intr_mask.d32, 0);
56385 +
56386 + /* If DEV OUT NAK enabled*/
56387 + if (pcd->core_if->core_params->dev_out_nak) {
56388 + /* Run over all out endpoints to determine the ep number on
56389 + * which the timeout has happened
56390 + */
56391 + for (i = 0; i <= dev_if->num_out_eps; i++) {
56392 + if ( pcd->core_if->ep_xfer_info[i].state == 2 )
56393 + break;
56394 + }
56395 + if (i > dev_if->num_out_eps) {
56396 + dctl_data_t dctl;
56397 + dctl.d32 =
56398 + DWC_READ_REG32(&dev_if->dev_global_regs->dctl);
56399 + dctl.b.cgoutnak = 1;
56400 + DWC_WRITE_REG32(&dev_if->dev_global_regs->dctl,
56401 + dctl.d32);
56402 + goto out;
56403 + }
56404 +
56405 + /* Disable the endpoint */
56406 + doepctl.d32 = DWC_READ_REG32(&dev_if->out_ep_regs[i]->doepctl);
56407 + if (doepctl.b.epena) {
56408 + doepctl.b.epdis = 1;
56409 + doepctl.b.snak = 1;
56410 + }
56411 + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doepctl, doepctl.d32);
56412 + return 1;
56413 + }
56414 + /* We come here from Incomplete ISO OUT handler */
56415 + if (dev_if->isoc_ep) {
56416 + dwc_ep_t *dwc_ep = (dwc_ep_t *)dev_if->isoc_ep;
56417 + uint32_t epnum = dwc_ep->num;
56418 + doepint_data_t doepint;
56419 + doepint.d32 =
56420 + DWC_READ_REG32(&dev_if->out_ep_regs[dwc_ep->num]->doepint);
56421 + dev_if->isoc_ep = NULL;
56422 + doepctl.d32 =
56423 + DWC_READ_REG32(&dev_if->out_ep_regs[epnum]->doepctl);
56424 + DWC_PRINTF("Before disable DOEPCTL = %08x\n", doepctl.d32);
56425 + if (doepctl.b.epena) {
56426 + doepctl.b.epdis = 1;
56427 + doepctl.b.snak = 1;
56428 + }
56429 + DWC_WRITE_REG32(&dev_if->out_ep_regs[epnum]->doepctl,
56430 + doepctl.d32);
56431 + return 1;
56432 + } else
56433 + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n",
56434 + "Global OUT NAK Effective\n");
56435 +
56436 +out:
56437 + /* Clear interrupt */
56438 + gintsts.d32 = 0;
56439 + gintsts.b.goutnakeff = 1;
56440 + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
56441 + gintsts.d32);
56442 +
56443 + return 1;
56444 +}
56445 +
56446 +/**
56447 + * PCD interrupt handler.
56448 + *
56449 + * The PCD handles the device interrupts. Many conditions can cause a
56450 + * device interrupt. When an interrupt occurs, the device interrupt
56451 + * service routine determines the cause of the interrupt and
56452 + * dispatches handling to the appropriate function. These interrupt
56453 + * handling functions are described below.
56454 + *
56455 + * All interrupt registers are processed from LSB to MSB.
56456 + *
56457 + */
56458 +int32_t dwc_otg_pcd_handle_intr(dwc_otg_pcd_t * pcd)
56459 +{
56460 + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
56461 +#ifdef VERBOSE
56462 + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
56463 +#endif
56464 + gintsts_data_t gintr_status;
56465 + int32_t retval = 0;
56466 +
56467 + /* Exit from ISR if core is hibernated */
56468 + if (core_if->hibernation_suspend == 1) {
56469 + return retval;
56470 + }
56471 +#ifdef VERBOSE
56472 + DWC_DEBUGPL(DBG_ANY, "%s() gintsts=%08x gintmsk=%08x\n",
56473 + __func__,
56474 + DWC_READ_REG32(&global_regs->gintsts),
56475 + DWC_READ_REG32(&global_regs->gintmsk));
56476 +#endif
56477 +
56478 + if (dwc_otg_is_device_mode(core_if)) {
56479 + DWC_SPINLOCK(pcd->lock);
56480 +#ifdef VERBOSE
56481 + DWC_DEBUGPL(DBG_PCDV, "%s() gintsts=%08x gintmsk=%08x\n",
56482 + __func__,
56483 + DWC_READ_REG32(&global_regs->gintsts),
56484 + DWC_READ_REG32(&global_regs->gintmsk));
56485 +#endif
56486 +
56487 + gintr_status.d32 = dwc_otg_read_core_intr(core_if);
56488 +
56489 + DWC_DEBUGPL(DBG_PCDV, "%s: gintsts&gintmsk=%08x\n",
56490 + __func__, gintr_status.d32);
56491 +
56492 + if (gintr_status.b.sofintr) {
56493 + retval |= dwc_otg_pcd_handle_sof_intr(pcd);
56494 + }
56495 + if (gintr_status.b.rxstsqlvl) {
56496 + retval |=
56497 + dwc_otg_pcd_handle_rx_status_q_level_intr(pcd);
56498 + }
56499 + if (gintr_status.b.nptxfempty) {
56500 + retval |= dwc_otg_pcd_handle_np_tx_fifo_empty_intr(pcd);
56501 + }
56502 + if (gintr_status.b.goutnakeff) {
56503 + retval |= dwc_otg_pcd_handle_out_nak_effective(pcd);
56504 + }
56505 + if (gintr_status.b.i2cintr) {
56506 + retval |= dwc_otg_pcd_handle_i2c_intr(pcd);
56507 + }
56508 + if (gintr_status.b.erlysuspend) {
56509 + retval |= dwc_otg_pcd_handle_early_suspend_intr(pcd);
56510 + }
56511 + if (gintr_status.b.usbreset) {
56512 + retval |= dwc_otg_pcd_handle_usb_reset_intr(pcd);
56513 + }
56514 + if (gintr_status.b.enumdone) {
56515 + retval |= dwc_otg_pcd_handle_enum_done_intr(pcd);
56516 + }
56517 + if (gintr_status.b.isooutdrop) {
56518 + retval |=
56519 + dwc_otg_pcd_handle_isoc_out_packet_dropped_intr
56520 + (pcd);
56521 + }
56522 + if (gintr_status.b.eopframe) {
56523 + retval |=
56524 + dwc_otg_pcd_handle_end_periodic_frame_intr(pcd);
56525 + }
56526 + if (gintr_status.b.inepint) {
56527 + if (!core_if->multiproc_int_enable) {
56528 + retval |= dwc_otg_pcd_handle_in_ep_intr(pcd);
56529 + }
56530 + }
56531 + if (gintr_status.b.outepintr) {
56532 + if (!core_if->multiproc_int_enable) {
56533 + retval |= dwc_otg_pcd_handle_out_ep_intr(pcd);
56534 + }
56535 + }
56536 + if (gintr_status.b.epmismatch) {
56537 + retval |= dwc_otg_pcd_handle_ep_mismatch_intr(pcd);
56538 + }
56539 + if (gintr_status.b.fetsusp) {
56540 + retval |= dwc_otg_pcd_handle_ep_fetsusp_intr(pcd);
56541 + }
56542 + if (gintr_status.b.ginnakeff) {
56543 + retval |= dwc_otg_pcd_handle_in_nak_effective(pcd);
56544 + }
56545 + if (gintr_status.b.incomplisoin) {
56546 + retval |=
56547 + dwc_otg_pcd_handle_incomplete_isoc_in_intr(pcd);
56548 + }
56549 + if (gintr_status.b.incomplisoout) {
56550 + retval |=
56551 + dwc_otg_pcd_handle_incomplete_isoc_out_intr(pcd);
56552 + }
56553 +
56554 + /* In MPI mode Device Endpoints interrupts are asserted
56555 + * without setting outepintr and inepint bits set, so these
56556 + * Interrupt handlers are called without checking these bit-fields
56557 + */
56558 + if (core_if->multiproc_int_enable) {
56559 + retval |= dwc_otg_pcd_handle_in_ep_intr(pcd);
56560 + retval |= dwc_otg_pcd_handle_out_ep_intr(pcd);
56561 + }
56562 +#ifdef VERBOSE
56563 + DWC_DEBUGPL(DBG_PCDV, "%s() gintsts=%0x\n", __func__,
56564 + DWC_READ_REG32(&global_regs->gintsts));
56565 +#endif
56566 + DWC_SPINUNLOCK(pcd->lock);
56567 + }
56568 + return retval;
56569 +}
56570 +
56571 +#endif /* DWC_HOST_ONLY */
56572 --- /dev/null
56573 +++ b/drivers/usb/host/dwc_otg/dwc_otg_pcd_linux.c
56574 @@ -0,0 +1,1280 @@
56575 + /* ==========================================================================
56576 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_pcd_linux.c $
56577 + * $Revision: #21 $
56578 + * $Date: 2012/08/10 $
56579 + * $Change: 2047372 $
56580 + *
56581 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
56582 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
56583 + * otherwise expressly agreed to in writing between Synopsys and you.
56584 + *
56585 + * The Software IS NOT an item of Licensed Software or Licensed Product under
56586 + * any End User Software License Agreement or Agreement for Licensed Product
56587 + * with Synopsys or any supplement thereto. You are permitted to use and
56588 + * redistribute this Software in source and binary forms, with or without
56589 + * modification, provided that redistributions of source code must retain this
56590 + * notice. You may not view, use, disclose, copy or distribute this file or
56591 + * any information contained herein except pursuant to this license grant from
56592 + * Synopsys. If you do not agree with this notice, including the disclaimer
56593 + * below, then you are not authorized to use the Software.
56594 + *
56595 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
56596 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56597 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56598 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
56599 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
56600 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
56601 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
56602 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56603 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56604 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
56605 + * DAMAGE.
56606 + * ========================================================================== */
56607 +#ifndef DWC_HOST_ONLY
56608 +
56609 +/** @file
56610 + * This file implements the Peripheral Controller Driver.
56611 + *
56612 + * The Peripheral Controller Driver (PCD) is responsible for
56613 + * translating requests from the Function Driver into the appropriate
56614 + * actions on the DWC_otg controller. It isolates the Function Driver
56615 + * from the specifics of the controller by providing an API to the
56616 + * Function Driver.
56617 + *
56618 + * The Peripheral Controller Driver for Linux will implement the
56619 + * Gadget API, so that the existing Gadget drivers can be used.
56620 + * (Gadget Driver is the Linux terminology for a Function Driver.)
56621 + *
56622 + * The Linux Gadget API is defined in the header file
56623 + * <code><linux/usb_gadget.h></code>. The USB EP operations API is
56624 + * defined in the structure <code>usb_ep_ops</code> and the USB
56625 + * Controller API is defined in the structure
56626 + * <code>usb_gadget_ops</code>.
56627 + *
56628 + */
56629 +
56630 +#include "dwc_otg_os_dep.h"
56631 +#include "dwc_otg_pcd_if.h"
56632 +#include "dwc_otg_pcd.h"
56633 +#include "dwc_otg_driver.h"
56634 +#include "dwc_otg_dbg.h"
56635 +
56636 +extern bool fiq_enable;
56637 +
56638 +static struct gadget_wrapper {
56639 + dwc_otg_pcd_t *pcd;
56640 +
56641 + struct usb_gadget gadget;
56642 + struct usb_gadget_driver *driver;
56643 +
56644 + struct usb_ep ep0;
56645 + struct usb_ep in_ep[16];
56646 + struct usb_ep out_ep[16];
56647 +
56648 +} *gadget_wrapper;
56649 +
56650 +/* Display the contents of the buffer */
56651 +extern void dump_msg(const u8 * buf, unsigned int length);
56652 +/**
56653 + * Get the dwc_otg_pcd_ep_t* from usb_ep* pointer - NULL in case
56654 + * if the endpoint is not found
56655 + */
56656 +static struct dwc_otg_pcd_ep *ep_from_handle(dwc_otg_pcd_t * pcd, void *handle)
56657 +{
56658 + int i;
56659 + if (pcd->ep0.priv == handle) {
56660 + return &pcd->ep0;
56661 + }
56662 +
56663 + for (i = 0; i < MAX_EPS_CHANNELS - 1; i++) {
56664 + if (pcd->in_ep[i].priv == handle)
56665 + return &pcd->in_ep[i];
56666 + if (pcd->out_ep[i].priv == handle)
56667 + return &pcd->out_ep[i];
56668 + }
56669 +
56670 + return NULL;
56671 +}
56672 +
56673 +/* USB Endpoint Operations */
56674 +/*
56675 + * The following sections briefly describe the behavior of the Gadget
56676 + * API endpoint operations implemented in the DWC_otg driver
56677 + * software. Detailed descriptions of the generic behavior of each of
56678 + * these functions can be found in the Linux header file
56679 + * include/linux/usb_gadget.h.
56680 + *
56681 + * The Gadget API provides wrapper functions for each of the function
56682 + * pointers defined in usb_ep_ops. The Gadget Driver calls the wrapper
56683 + * function, which then calls the underlying PCD function. The
56684 + * following sections are named according to the wrapper
56685 + * functions. Within each section, the corresponding DWC_otg PCD
56686 + * function name is specified.
56687 + *
56688 + */
56689 +
56690 +/**
56691 + * This function is called by the Gadget Driver for each EP to be
56692 + * configured for the current configuration (SET_CONFIGURATION).
56693 + *
56694 + * This function initializes the dwc_otg_ep_t data structure, and then
56695 + * calls dwc_otg_ep_activate.
56696 + */
56697 +static int ep_enable(struct usb_ep *usb_ep,
56698 + const struct usb_endpoint_descriptor *ep_desc)
56699 +{
56700 + int retval;
56701 +
56702 + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%p)\n", __func__, usb_ep, ep_desc);
56703 +
56704 + if (!usb_ep || !ep_desc || ep_desc->bDescriptorType != USB_DT_ENDPOINT) {
56705 + DWC_WARN("%s, bad ep or descriptor\n", __func__);
56706 + return -EINVAL;
56707 + }
56708 + if (usb_ep == &gadget_wrapper->ep0) {
56709 + DWC_WARN("%s, bad ep(0)\n", __func__);
56710 + return -EINVAL;
56711 + }
56712 +
56713 + /* Check FIFO size? */
56714 + if (!ep_desc->wMaxPacketSize) {
56715 + DWC_WARN("%s, bad %s maxpacket\n", __func__, usb_ep->name);
56716 + return -ERANGE;
56717 + }
56718 +
56719 + if (!gadget_wrapper->driver ||
56720 + gadget_wrapper->gadget.speed == USB_SPEED_UNKNOWN) {
56721 + DWC_WARN("%s, bogus device state\n", __func__);
56722 + return -ESHUTDOWN;
56723 + }
56724 +
56725 + /* Delete after check - MAS */
56726 +#if 0
56727 + nat = (uint32_t) ep_desc->wMaxPacketSize;
56728 + printk(KERN_ALERT "%s: nat (before) =%d\n", __func__, nat);
56729 + nat = (nat >> 11) & 0x03;
56730 + printk(KERN_ALERT "%s: nat (after) =%d\n", __func__, nat);
56731 +#endif
56732 + retval = dwc_otg_pcd_ep_enable(gadget_wrapper->pcd,
56733 + (const uint8_t *)ep_desc,
56734 + (void *)usb_ep);
56735 + if (retval) {
56736 + DWC_WARN("dwc_otg_pcd_ep_enable failed\n");
56737 + return -EINVAL;
56738 + }
56739 +
56740 + usb_ep->maxpacket = le16_to_cpu(ep_desc->wMaxPacketSize);
56741 +
56742 + return 0;
56743 +}
56744 +
56745 +/**
56746 + * This function is called when an EP is disabled due to disconnect or
56747 + * change in configuration. Any pending requests will terminate with a
56748 + * status of -ESHUTDOWN.
56749 + *
56750 + * This function modifies the dwc_otg_ep_t data structure for this EP,
56751 + * and then calls dwc_otg_ep_deactivate.
56752 + */
56753 +static int ep_disable(struct usb_ep *usb_ep)
56754 +{
56755 + int retval;
56756 +
56757 + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, usb_ep);
56758 + if (!usb_ep) {
56759 + DWC_DEBUGPL(DBG_PCD, "%s, %s not enabled\n", __func__,
56760 + usb_ep ? usb_ep->name : NULL);
56761 + return -EINVAL;
56762 + }
56763 +
56764 + retval = dwc_otg_pcd_ep_disable(gadget_wrapper->pcd, usb_ep);
56765 + if (retval) {
56766 + retval = -EINVAL;
56767 + }
56768 +
56769 + return retval;
56770 +}
56771 +
56772 +/**
56773 + * This function allocates a request object to use with the specified
56774 + * endpoint.
56775 + *
56776 + * @param ep The endpoint to be used with with the request
56777 + * @param gfp_flags the GFP_* flags to use.
56778 + */
56779 +static struct usb_request *dwc_otg_pcd_alloc_request(struct usb_ep *ep,
56780 + gfp_t gfp_flags)
56781 +{
56782 + struct usb_request *usb_req;
56783 +
56784 + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%d)\n", __func__, ep, gfp_flags);
56785 + if (0 == ep) {
56786 + DWC_WARN("%s() %s\n", __func__, "Invalid EP!\n");
56787 + return 0;
56788 + }
56789 + usb_req = kzalloc(sizeof(*usb_req), gfp_flags);
56790 + if (0 == usb_req) {
56791 + DWC_WARN("%s() %s\n", __func__, "request allocation failed!\n");
56792 + return 0;
56793 + }
56794 + usb_req->dma = DWC_DMA_ADDR_INVALID;
56795 +
56796 + return usb_req;
56797 +}
56798 +
56799 +/**
56800 + * This function frees a request object.
56801 + *
56802 + * @param ep The endpoint associated with the request
56803 + * @param req The request being freed
56804 + */
56805 +static void dwc_otg_pcd_free_request(struct usb_ep *ep, struct usb_request *req)
56806 +{
56807 + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%p)\n", __func__, ep, req);
56808 +
56809 + if (0 == ep || 0 == req) {
56810 + DWC_WARN("%s() %s\n", __func__,
56811 + "Invalid ep or req argument!\n");
56812 + return;
56813 + }
56814 +
56815 + kfree(req);
56816 +}
56817 +
56818 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
56819 +/**
56820 + * This function allocates an I/O buffer to be used for a transfer
56821 + * to/from the specified endpoint.
56822 + *
56823 + * @param usb_ep The endpoint to be used with with the request
56824 + * @param bytes The desired number of bytes for the buffer
56825 + * @param dma Pointer to the buffer's DMA address; must be valid
56826 + * @param gfp_flags the GFP_* flags to use.
56827 + * @return address of a new buffer or null is buffer could not be allocated.
56828 + */
56829 +static void *dwc_otg_pcd_alloc_buffer(struct usb_ep *usb_ep, unsigned bytes,
56830 + dma_addr_t * dma, gfp_t gfp_flags)
56831 +{
56832 + void *buf;
56833 + dwc_otg_pcd_t *pcd = 0;
56834 +
56835 + pcd = gadget_wrapper->pcd;
56836 +
56837 + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%d,%p,%0x)\n", __func__, usb_ep, bytes,
56838 + dma, gfp_flags);
56839 +
56840 + /* Check dword alignment */
56841 + if ((bytes & 0x3UL) != 0) {
56842 + DWC_WARN("%s() Buffer size is not a multiple of"
56843 + "DWORD size (%d)", __func__, bytes);
56844 + }
56845 +
56846 + buf = dma_alloc_coherent(NULL, bytes, dma, gfp_flags);
56847 + WARN_ON(!buf);
56848 +
56849 + /* Check dword alignment */
56850 + if (((int)buf & 0x3UL) != 0) {
56851 + DWC_WARN("%s() Buffer is not DWORD aligned (%p)",
56852 + __func__, buf);
56853 + }
56854 +
56855 + return buf;
56856 +}
56857 +
56858 +/**
56859 + * This function frees an I/O buffer that was allocated by alloc_buffer.
56860 + *
56861 + * @param usb_ep the endpoint associated with the buffer
56862 + * @param buf address of the buffer
56863 + * @param dma The buffer's DMA address
56864 + * @param bytes The number of bytes of the buffer
56865 + */
56866 +static void dwc_otg_pcd_free_buffer(struct usb_ep *usb_ep, void *buf,
56867 + dma_addr_t dma, unsigned bytes)
56868 +{
56869 + dwc_otg_pcd_t *pcd = 0;
56870 +
56871 + pcd = gadget_wrapper->pcd;
56872 +
56873 + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%0x,%d)\n", __func__, buf, dma, bytes);
56874 +
56875 + dma_free_coherent(NULL, bytes, buf, dma);
56876 +}
56877 +#endif
56878 +
56879 +/**
56880 + * This function is used to submit an I/O Request to an EP.
56881 + *
56882 + * - When the request completes the request's completion callback
56883 + * is called to return the request to the driver.
56884 + * - An EP, except control EPs, may have multiple requests
56885 + * pending.
56886 + * - Once submitted the request cannot be examined or modified.
56887 + * - Each request is turned into one or more packets.
56888 + * - A BULK EP can queue any amount of data; the transfer is
56889 + * packetized.
56890 + * - Zero length Packets are specified with the request 'zero'
56891 + * flag.
56892 + */
56893 +static int ep_queue(struct usb_ep *usb_ep, struct usb_request *usb_req,
56894 + gfp_t gfp_flags)
56895 +{
56896 + dwc_otg_pcd_t *pcd;
56897 + struct dwc_otg_pcd_ep *ep = NULL;
56898 + int retval = 0, is_isoc_ep = 0;
56899 + dma_addr_t dma_addr = DWC_DMA_ADDR_INVALID;
56900 +
56901 + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%p,%d)\n",
56902 + __func__, usb_ep, usb_req, gfp_flags);
56903 +
56904 + if (!usb_req || !usb_req->complete || !usb_req->buf) {
56905 + DWC_WARN("bad params\n");
56906 + return -EINVAL;
56907 + }
56908 +
56909 + if (!usb_ep) {
56910 + DWC_WARN("bad ep\n");
56911 + return -EINVAL;
56912 + }
56913 +
56914 + pcd = gadget_wrapper->pcd;
56915 + if (!gadget_wrapper->driver ||
56916 + gadget_wrapper->gadget.speed == USB_SPEED_UNKNOWN) {
56917 + DWC_DEBUGPL(DBG_PCDV, "gadget.speed=%d\n",
56918 + gadget_wrapper->gadget.speed);
56919 + DWC_WARN("bogus device state\n");
56920 + return -ESHUTDOWN;
56921 + }
56922 +
56923 + DWC_DEBUGPL(DBG_PCD, "%s queue req %p, len %d buf %p\n",
56924 + usb_ep->name, usb_req, usb_req->length, usb_req->buf);
56925 +
56926 + usb_req->status = -EINPROGRESS;
56927 + usb_req->actual = 0;
56928 +
56929 + ep = ep_from_handle(pcd, usb_ep);
56930 + if (ep == NULL)
56931 + is_isoc_ep = 0;
56932 + else
56933 + is_isoc_ep = (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) ? 1 : 0;
56934 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
56935 + dma_addr = usb_req->dma;
56936 +#else
56937 + if (GET_CORE_IF(pcd)->dma_enable) {
56938 + dwc_otg_device_t *otg_dev = gadget_wrapper->pcd->otg_dev;
56939 + struct device *dev = NULL;
56940 +
56941 + if (otg_dev != NULL)
56942 + dev = DWC_OTG_OS_GETDEV(otg_dev->os_dep);
56943 +
56944 + if (usb_req->length != 0 &&
56945 + usb_req->dma == DWC_DMA_ADDR_INVALID) {
56946 + dma_addr = dma_map_single(dev, usb_req->buf,
56947 + usb_req->length,
56948 + ep->dwc_ep.is_in ?
56949 + DMA_TO_DEVICE:
56950 + DMA_FROM_DEVICE);
56951 + }
56952 + }
56953 +#endif
56954 +
56955 +#ifdef DWC_UTE_PER_IO
56956 + if (is_isoc_ep == 1) {
56957 + retval = dwc_otg_pcd_xiso_ep_queue(pcd, usb_ep, usb_req->buf, dma_addr,
56958 + usb_req->length, usb_req->zero, usb_req,
56959 + gfp_flags == GFP_ATOMIC ? 1 : 0, &usb_req->ext_req);
56960 + if (retval)
56961 + return -EINVAL;
56962 +
56963 + return 0;
56964 + }
56965 +#endif
56966 + retval = dwc_otg_pcd_ep_queue(pcd, usb_ep, usb_req->buf, dma_addr,
56967 + usb_req->length, usb_req->zero, usb_req,
56968 + gfp_flags == GFP_ATOMIC ? 1 : 0);
56969 + if (retval) {
56970 + return -EINVAL;
56971 + }
56972 +
56973 + return 0;
56974 +}
56975 +
56976 +/**
56977 + * This function cancels an I/O request from an EP.
56978 + */
56979 +static int ep_dequeue(struct usb_ep *usb_ep, struct usb_request *usb_req)
56980 +{
56981 + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%p)\n", __func__, usb_ep, usb_req);
56982 +
56983 + if (!usb_ep || !usb_req) {
56984 + DWC_WARN("bad argument\n");
56985 + return -EINVAL;
56986 + }
56987 + if (!gadget_wrapper->driver ||
56988 + gadget_wrapper->gadget.speed == USB_SPEED_UNKNOWN) {
56989 + DWC_WARN("bogus device state\n");
56990 + return -ESHUTDOWN;
56991 + }
56992 + if (dwc_otg_pcd_ep_dequeue(gadget_wrapper->pcd, usb_ep, usb_req)) {
56993 + return -EINVAL;
56994 + }
56995 +
56996 + return 0;
56997 +}
56998 +
56999 +/**
57000 + * usb_ep_set_halt stalls an endpoint.
57001 + *
57002 + * usb_ep_clear_halt clears an endpoint halt and resets its data
57003 + * toggle.
57004 + *
57005 + * Both of these functions are implemented with the same underlying
57006 + * function. The behavior depends on the value argument.
57007 + *
57008 + * @param[in] usb_ep the Endpoint to halt or clear halt.
57009 + * @param[in] value
57010 + * - 0 means clear_halt.
57011 + * - 1 means set_halt,
57012 + * - 2 means clear stall lock flag.
57013 + * - 3 means set stall lock flag.
57014 + */
57015 +static int ep_halt(struct usb_ep *usb_ep, int value)
57016 +{
57017 + int retval = 0;
57018 +
57019 + DWC_DEBUGPL(DBG_PCD, "HALT %s %d\n", usb_ep->name, value);
57020 +
57021 + if (!usb_ep) {
57022 + DWC_WARN("bad ep\n");
57023 + return -EINVAL;
57024 + }
57025 +
57026 + retval = dwc_otg_pcd_ep_halt(gadget_wrapper->pcd, usb_ep, value);
57027 + if (retval == -DWC_E_AGAIN) {
57028 + return -EAGAIN;
57029 + } else if (retval) {
57030 + retval = -EINVAL;
57031 + }
57032 +
57033 + return retval;
57034 +}
57035 +
57036 +//#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30))
57037 +#if 0
57038 +/**
57039 + * ep_wedge: sets the halt feature and ignores clear requests
57040 + *
57041 + * @usb_ep: the endpoint being wedged
57042 + *
57043 + * Use this to stall an endpoint and ignore CLEAR_FEATURE(HALT_ENDPOINT)
57044 + * requests. If the gadget driver clears the halt status, it will
57045 + * automatically unwedge the endpoint.
57046 + *
57047 + * Returns zero on success, else negative errno. *
57048 + * Check usb_ep_set_wedge() at "usb_gadget.h" for details
57049 + */
57050 +static int ep_wedge(struct usb_ep *usb_ep)
57051 +{
57052 + int retval = 0;
57053 +
57054 + DWC_DEBUGPL(DBG_PCD, "WEDGE %s\n", usb_ep->name);
57055 +
57056 + if (!usb_ep) {
57057 + DWC_WARN("bad ep\n");
57058 + return -EINVAL;
57059 + }
57060 +
57061 + retval = dwc_otg_pcd_ep_wedge(gadget_wrapper->pcd, usb_ep);
57062 + if (retval == -DWC_E_AGAIN) {
57063 + retval = -EAGAIN;
57064 + } else if (retval) {
57065 + retval = -EINVAL;
57066 + }
57067 +
57068 + return retval;
57069 +}
57070 +#endif
57071 +
57072 +#ifdef DWC_EN_ISOC
57073 +/**
57074 + * This function is used to submit an ISOC Transfer Request to an EP.
57075 + *
57076 + * - Every time a sync period completes the request's completion callback
57077 + * is called to provide data to the gadget driver.
57078 + * - Once submitted the request cannot be modified.
57079 + * - Each request is turned into periodic data packets untill ISO
57080 + * Transfer is stopped..
57081 + */
57082 +static int iso_ep_start(struct usb_ep *usb_ep, struct usb_iso_request *req,
57083 + gfp_t gfp_flags)
57084 +{
57085 + int retval = 0;
57086 +
57087 + if (!req || !req->process_buffer || !req->buf0 || !req->buf1) {
57088 + DWC_WARN("bad params\n");
57089 + return -EINVAL;
57090 + }
57091 +
57092 + if (!usb_ep) {
57093 + DWC_PRINTF("bad params\n");
57094 + return -EINVAL;
57095 + }
57096 +
57097 + req->status = -EINPROGRESS;
57098 +
57099 + retval =
57100 + dwc_otg_pcd_iso_ep_start(gadget_wrapper->pcd, usb_ep, req->buf0,
57101 + req->buf1, req->dma0, req->dma1,
57102 + req->sync_frame, req->data_pattern_frame,
57103 + req->data_per_frame,
57104 + req->
57105 + flags & USB_REQ_ISO_ASAP ? -1 :
57106 + req->start_frame, req->buf_proc_intrvl,
57107 + req, gfp_flags == GFP_ATOMIC ? 1 : 0);
57108 +
57109 + if (retval) {
57110 + return -EINVAL;
57111 + }
57112 +
57113 + return retval;
57114 +}
57115 +
57116 +/**
57117 + * This function stops ISO EP Periodic Data Transfer.
57118 + */
57119 +static int iso_ep_stop(struct usb_ep *usb_ep, struct usb_iso_request *req)
57120 +{
57121 + int retval = 0;
57122 + if (!usb_ep) {
57123 + DWC_WARN("bad ep\n");
57124 + }
57125 +
57126 + if (!gadget_wrapper->driver ||
57127 + gadget_wrapper->gadget.speed == USB_SPEED_UNKNOWN) {
57128 + DWC_DEBUGPL(DBG_PCDV, "gadget.speed=%d\n",
57129 + gadget_wrapper->gadget.speed);
57130 + DWC_WARN("bogus device state\n");
57131 + }
57132 +
57133 + dwc_otg_pcd_iso_ep_stop(gadget_wrapper->pcd, usb_ep, req);
57134 + if (retval) {
57135 + retval = -EINVAL;
57136 + }
57137 +
57138 + return retval;
57139 +}
57140 +
57141 +static struct usb_iso_request *alloc_iso_request(struct usb_ep *ep,
57142 + int packets, gfp_t gfp_flags)
57143 +{
57144 + struct usb_iso_request *pReq = NULL;
57145 + uint32_t req_size;
57146 +
57147 + req_size = sizeof(struct usb_iso_request);
57148 + req_size +=
57149 + (2 * packets * (sizeof(struct usb_gadget_iso_packet_descriptor)));
57150 +
57151 + pReq = kmalloc(req_size, gfp_flags);
57152 + if (!pReq) {
57153 + DWC_WARN("Can't allocate Iso Request\n");
57154 + return 0;
57155 + }
57156 + pReq->iso_packet_desc0 = (void *)(pReq + 1);
57157 +
57158 + pReq->iso_packet_desc1 = pReq->iso_packet_desc0 + packets;
57159 +
57160 + return pReq;
57161 +}
57162 +
57163 +static void free_iso_request(struct usb_ep *ep, struct usb_iso_request *req)
57164 +{
57165 + kfree(req);
57166 +}
57167 +
57168 +static struct usb_isoc_ep_ops dwc_otg_pcd_ep_ops = {
57169 + .ep_ops = {
57170 + .enable = ep_enable,
57171 + .disable = ep_disable,
57172 +
57173 + .alloc_request = dwc_otg_pcd_alloc_request,
57174 + .free_request = dwc_otg_pcd_free_request,
57175 +
57176 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
57177 + .alloc_buffer = dwc_otg_pcd_alloc_buffer,
57178 + .free_buffer = dwc_otg_pcd_free_buffer,
57179 +#endif
57180 +
57181 + .queue = ep_queue,
57182 + .dequeue = ep_dequeue,
57183 +
57184 + .set_halt = ep_halt,
57185 + .fifo_status = 0,
57186 + .fifo_flush = 0,
57187 + },
57188 + .iso_ep_start = iso_ep_start,
57189 + .iso_ep_stop = iso_ep_stop,
57190 + .alloc_iso_request = alloc_iso_request,
57191 + .free_iso_request = free_iso_request,
57192 +};
57193 +
57194 +#else
57195 +
57196 + int (*enable) (struct usb_ep *ep,
57197 + const struct usb_endpoint_descriptor *desc);
57198 + int (*disable) (struct usb_ep *ep);
57199 +
57200 + struct usb_request *(*alloc_request) (struct usb_ep *ep,
57201 + gfp_t gfp_flags);
57202 + void (*free_request) (struct usb_ep *ep, struct usb_request *req);
57203 +
57204 + int (*queue) (struct usb_ep *ep, struct usb_request *req,
57205 + gfp_t gfp_flags);
57206 + int (*dequeue) (struct usb_ep *ep, struct usb_request *req);
57207 +
57208 + int (*set_halt) (struct usb_ep *ep, int value);
57209 + int (*set_wedge) (struct usb_ep *ep);
57210 +
57211 + int (*fifo_status) (struct usb_ep *ep);
57212 + void (*fifo_flush) (struct usb_ep *ep);
57213 +static struct usb_ep_ops dwc_otg_pcd_ep_ops = {
57214 + .enable = ep_enable,
57215 + .disable = ep_disable,
57216 +
57217 + .alloc_request = dwc_otg_pcd_alloc_request,
57218 + .free_request = dwc_otg_pcd_free_request,
57219 +
57220 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
57221 + .alloc_buffer = dwc_otg_pcd_alloc_buffer,
57222 + .free_buffer = dwc_otg_pcd_free_buffer,
57223 +#else
57224 + /* .set_wedge = ep_wedge, */
57225 + .set_wedge = NULL, /* uses set_halt instead */
57226 +#endif
57227 +
57228 + .queue = ep_queue,
57229 + .dequeue = ep_dequeue,
57230 +
57231 + .set_halt = ep_halt,
57232 + .fifo_status = 0,
57233 + .fifo_flush = 0,
57234 +
57235 +};
57236 +
57237 +#endif /* _EN_ISOC_ */
57238 +/* Gadget Operations */
57239 +/**
57240 + * The following gadget operations will be implemented in the DWC_otg
57241 + * PCD. Functions in the API that are not described below are not
57242 + * implemented.
57243 + *
57244 + * The Gadget API provides wrapper functions for each of the function
57245 + * pointers defined in usb_gadget_ops. The Gadget Driver calls the
57246 + * wrapper function, which then calls the underlying PCD function. The
57247 + * following sections are named according to the wrapper functions
57248 + * (except for ioctl, which doesn't have a wrapper function). Within
57249 + * each section, the corresponding DWC_otg PCD function name is
57250 + * specified.
57251 + *
57252 + */
57253 +
57254 +/**
57255 + *Gets the USB Frame number of the last SOF.
57256 + */
57257 +static int get_frame_number(struct usb_gadget *gadget)
57258 +{
57259 + struct gadget_wrapper *d;
57260 +
57261 + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, gadget);
57262 +
57263 + if (gadget == 0) {
57264 + return -ENODEV;
57265 + }
57266 +
57267 + d = container_of(gadget, struct gadget_wrapper, gadget);
57268 + return dwc_otg_pcd_get_frame_number(d->pcd);
57269 +}
57270 +
57271 +#ifdef CONFIG_USB_DWC_OTG_LPM
57272 +static int test_lpm_enabled(struct usb_gadget *gadget)
57273 +{
57274 + struct gadget_wrapper *d;
57275 +
57276 + d = container_of(gadget, struct gadget_wrapper, gadget);
57277 +
57278 + return dwc_otg_pcd_is_lpm_enabled(d->pcd);
57279 +}
57280 +#endif
57281 +
57282 +/**
57283 + * Initiates Session Request Protocol (SRP) to wakeup the host if no
57284 + * session is in progress. If a session is already in progress, but
57285 + * the device is suspended, remote wakeup signaling is started.
57286 + *
57287 + */
57288 +static int wakeup(struct usb_gadget *gadget)
57289 +{
57290 + struct gadget_wrapper *d;
57291 +
57292 + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, gadget);
57293 +
57294 + if (gadget == 0) {
57295 + return -ENODEV;
57296 + } else {
57297 + d = container_of(gadget, struct gadget_wrapper, gadget);
57298 + }
57299 + dwc_otg_pcd_wakeup(d->pcd);
57300 + return 0;
57301 +}
57302 +
57303 +static const struct usb_gadget_ops dwc_otg_pcd_ops = {
57304 + .get_frame = get_frame_number,
57305 + .wakeup = wakeup,
57306 +#ifdef CONFIG_USB_DWC_OTG_LPM
57307 + .lpm_support = test_lpm_enabled,
57308 +#endif
57309 + // current versions must always be self-powered
57310 +};
57311 +
57312 +static int _setup(dwc_otg_pcd_t * pcd, uint8_t * bytes)
57313 +{
57314 + int retval = -DWC_E_NOT_SUPPORTED;
57315 + if (gadget_wrapper->driver && gadget_wrapper->driver->setup) {
57316 + retval = gadget_wrapper->driver->setup(&gadget_wrapper->gadget,
57317 + (struct usb_ctrlrequest
57318 + *)bytes);
57319 + }
57320 +
57321 + if (retval == -ENOTSUPP) {
57322 + retval = -DWC_E_NOT_SUPPORTED;
57323 + } else if (retval < 0) {
57324 + retval = -DWC_E_INVALID;
57325 + }
57326 +
57327 + return retval;
57328 +}
57329 +
57330 +#ifdef DWC_EN_ISOC
57331 +static int _isoc_complete(dwc_otg_pcd_t * pcd, void *ep_handle,
57332 + void *req_handle, int proc_buf_num)
57333 +{
57334 + int i, packet_count;
57335 + struct usb_gadget_iso_packet_descriptor *iso_packet = 0;
57336 + struct usb_iso_request *iso_req = req_handle;
57337 +
57338 + if (proc_buf_num) {
57339 + iso_packet = iso_req->iso_packet_desc1;
57340 + } else {
57341 + iso_packet = iso_req->iso_packet_desc0;
57342 + }
57343 + packet_count =
57344 + dwc_otg_pcd_get_iso_packet_count(pcd, ep_handle, req_handle);
57345 + for (i = 0; i < packet_count; ++i) {
57346 + int status;
57347 + int actual;
57348 + int offset;
57349 + dwc_otg_pcd_get_iso_packet_params(pcd, ep_handle, req_handle,
57350 + i, &status, &actual, &offset);
57351 + switch (status) {
57352 + case -DWC_E_NO_DATA:
57353 + status = -ENODATA;
57354 + break;
57355 + default:
57356 + if (status) {
57357 + DWC_PRINTF("unknown status in isoc packet\n");
57358 + }
57359 +
57360 + }
57361 + iso_packet[i].status = status;
57362 + iso_packet[i].offset = offset;
57363 + iso_packet[i].actual_length = actual;
57364 + }
57365 +
57366 + iso_req->status = 0;
57367 + iso_req->process_buffer(ep_handle, iso_req);
57368 +
57369 + return 0;
57370 +}
57371 +#endif /* DWC_EN_ISOC */
57372 +
57373 +#ifdef DWC_UTE_PER_IO
57374 +/**
57375 + * Copy the contents of the extended request to the Linux usb_request's
57376 + * extended part and call the gadget's completion.
57377 + *
57378 + * @param pcd Pointer to the pcd structure
57379 + * @param ep_handle Void pointer to the usb_ep structure
57380 + * @param req_handle Void pointer to the usb_request structure
57381 + * @param status Request status returned from the portable logic
57382 + * @param ereq_port Void pointer to the extended request structure
57383 + * created in the the portable part that contains the
57384 + * results of the processed iso packets.
57385 + */
57386 +static int _xisoc_complete(dwc_otg_pcd_t * pcd, void *ep_handle,
57387 + void *req_handle, int32_t status, void *ereq_port)
57388 +{
57389 + struct dwc_ute_iso_req_ext *ereqorg = NULL;
57390 + struct dwc_iso_xreq_port *ereqport = NULL;
57391 + struct dwc_ute_iso_packet_descriptor *desc_org = NULL;
57392 + int i;
57393 + struct usb_request *req;
57394 + //struct dwc_ute_iso_packet_descriptor *
57395 + //int status = 0;
57396 +
57397 + req = (struct usb_request *)req_handle;
57398 + ereqorg = &req->ext_req;
57399 + ereqport = (struct dwc_iso_xreq_port *)ereq_port;
57400 + desc_org = ereqorg->per_io_frame_descs;
57401 +
57402 + if (req && req->complete) {
57403 + /* Copy the request data from the portable logic to our request */
57404 + for (i = 0; i < ereqport->pio_pkt_count; i++) {
57405 + desc_org[i].actual_length =
57406 + ereqport->per_io_frame_descs[i].actual_length;
57407 + desc_org[i].status =
57408 + ereqport->per_io_frame_descs[i].status;
57409 + }
57410 +
57411 + switch (status) {
57412 + case -DWC_E_SHUTDOWN:
57413 + req->status = -ESHUTDOWN;
57414 + break;
57415 + case -DWC_E_RESTART:
57416 + req->status = -ECONNRESET;
57417 + break;
57418 + case -DWC_E_INVALID:
57419 + req->status = -EINVAL;
57420 + break;
57421 + case -DWC_E_TIMEOUT:
57422 + req->status = -ETIMEDOUT;
57423 + break;
57424 + default:
57425 + req->status = status;
57426 + }
57427 +
57428 + /* And call the gadget's completion */
57429 + req->complete(ep_handle, req);
57430 + }
57431 +
57432 + return 0;
57433 +}
57434 +#endif /* DWC_UTE_PER_IO */
57435 +
57436 +static int _complete(dwc_otg_pcd_t * pcd, void *ep_handle,
57437 + void *req_handle, int32_t status, uint32_t actual)
57438 +{
57439 + struct usb_request *req = (struct usb_request *)req_handle;
57440 +#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,27)
57441 + struct dwc_otg_pcd_ep *ep = NULL;
57442 +#endif
57443 +
57444 + if (req && req->complete) {
57445 + switch (status) {
57446 + case -DWC_E_SHUTDOWN:
57447 + req->status = -ESHUTDOWN;
57448 + break;
57449 + case -DWC_E_RESTART:
57450 + req->status = -ECONNRESET;
57451 + break;
57452 + case -DWC_E_INVALID:
57453 + req->status = -EINVAL;
57454 + break;
57455 + case -DWC_E_TIMEOUT:
57456 + req->status = -ETIMEDOUT;
57457 + break;
57458 + default:
57459 + req->status = status;
57460 +
57461 + }
57462 +
57463 + req->actual = actual;
57464 + DWC_SPINUNLOCK(pcd->lock);
57465 + req->complete(ep_handle, req);
57466 + DWC_SPINLOCK(pcd->lock);
57467 + }
57468 +#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,27)
57469 + ep = ep_from_handle(pcd, ep_handle);
57470 + if (GET_CORE_IF(pcd)->dma_enable) {
57471 + if (req->length != 0) {
57472 + dwc_otg_device_t *otg_dev = gadget_wrapper->pcd->otg_dev;
57473 + struct device *dev = NULL;
57474 +
57475 + if (otg_dev != NULL)
57476 + dev = DWC_OTG_OS_GETDEV(otg_dev->os_dep);
57477 +
57478 + dma_unmap_single(dev, req->dma, req->length,
57479 + ep->dwc_ep.is_in ?
57480 + DMA_TO_DEVICE: DMA_FROM_DEVICE);
57481 + }
57482 + }
57483 +#endif
57484 +
57485 + return 0;
57486 +}
57487 +
57488 +static int _connect(dwc_otg_pcd_t * pcd, int speed)
57489 +{
57490 + gadget_wrapper->gadget.speed = speed;
57491 + return 0;
57492 +}
57493 +
57494 +static int _disconnect(dwc_otg_pcd_t * pcd)
57495 +{
57496 + if (gadget_wrapper->driver && gadget_wrapper->driver->disconnect) {
57497 + gadget_wrapper->driver->disconnect(&gadget_wrapper->gadget);
57498 + }
57499 + return 0;
57500 +}
57501 +
57502 +static int _resume(dwc_otg_pcd_t * pcd)
57503 +{
57504 + if (gadget_wrapper->driver && gadget_wrapper->driver->resume) {
57505 + gadget_wrapper->driver->resume(&gadget_wrapper->gadget);
57506 + }
57507 +
57508 + return 0;
57509 +}
57510 +
57511 +static int _suspend(dwc_otg_pcd_t * pcd)
57512 +{
57513 + if (gadget_wrapper->driver && gadget_wrapper->driver->suspend) {
57514 + gadget_wrapper->driver->suspend(&gadget_wrapper->gadget);
57515 + }
57516 + return 0;
57517 +}
57518 +
57519 +/**
57520 + * This function updates the otg values in the gadget structure.
57521 + */
57522 +static int _hnp_changed(dwc_otg_pcd_t * pcd)
57523 +{
57524 +
57525 + if (!gadget_wrapper->gadget.is_otg)
57526 + return 0;
57527 +
57528 + gadget_wrapper->gadget.b_hnp_enable = get_b_hnp_enable(pcd);
57529 + gadget_wrapper->gadget.a_hnp_support = get_a_hnp_support(pcd);
57530 + gadget_wrapper->gadget.a_alt_hnp_support = get_a_alt_hnp_support(pcd);
57531 + return 0;
57532 +}
57533 +
57534 +static int _reset(dwc_otg_pcd_t * pcd)
57535 +{
57536 + return 0;
57537 +}
57538 +
57539 +#ifdef DWC_UTE_CFI
57540 +static int _cfi_setup(dwc_otg_pcd_t * pcd, void *cfi_req)
57541 +{
57542 + int retval = -DWC_E_INVALID;
57543 + if (gadget_wrapper->driver->cfi_feature_setup) {
57544 + retval =
57545 + gadget_wrapper->driver->
57546 + cfi_feature_setup(&gadget_wrapper->gadget,
57547 + (struct cfi_usb_ctrlrequest *)cfi_req);
57548 + }
57549 +
57550 + return retval;
57551 +}
57552 +#endif
57553 +
57554 +static const struct dwc_otg_pcd_function_ops fops = {
57555 + .complete = _complete,
57556 +#ifdef DWC_EN_ISOC
57557 + .isoc_complete = _isoc_complete,
57558 +#endif
57559 + .setup = _setup,
57560 + .disconnect = _disconnect,
57561 + .connect = _connect,
57562 + .resume = _resume,
57563 + .suspend = _suspend,
57564 + .hnp_changed = _hnp_changed,
57565 + .reset = _reset,
57566 +#ifdef DWC_UTE_CFI
57567 + .cfi_setup = _cfi_setup,
57568 +#endif
57569 +#ifdef DWC_UTE_PER_IO
57570 + .xisoc_complete = _xisoc_complete,
57571 +#endif
57572 +};
57573 +
57574 +/**
57575 + * This function is the top level PCD interrupt handler.
57576 + */
57577 +static irqreturn_t dwc_otg_pcd_irq(int irq, void *dev)
57578 +{
57579 + dwc_otg_pcd_t *pcd = dev;
57580 + int32_t retval = IRQ_NONE;
57581 +
57582 + retval = dwc_otg_pcd_handle_intr(pcd);
57583 + if (retval != 0) {
57584 + S3C2410X_CLEAR_EINTPEND();
57585 + }
57586 + return IRQ_RETVAL(retval);
57587 +}
57588 +
57589 +/**
57590 + * This function initialized the usb_ep structures to there default
57591 + * state.
57592 + *
57593 + * @param d Pointer on gadget_wrapper.
57594 + */
57595 +void gadget_add_eps(struct gadget_wrapper *d)
57596 +{
57597 + static const char *names[] = {
57598 +
57599 + "ep0",
57600 + "ep1in",
57601 + "ep2in",
57602 + "ep3in",
57603 + "ep4in",
57604 + "ep5in",
57605 + "ep6in",
57606 + "ep7in",
57607 + "ep8in",
57608 + "ep9in",
57609 + "ep10in",
57610 + "ep11in",
57611 + "ep12in",
57612 + "ep13in",
57613 + "ep14in",
57614 + "ep15in",
57615 + "ep1out",
57616 + "ep2out",
57617 + "ep3out",
57618 + "ep4out",
57619 + "ep5out",
57620 + "ep6out",
57621 + "ep7out",
57622 + "ep8out",
57623 + "ep9out",
57624 + "ep10out",
57625 + "ep11out",
57626 + "ep12out",
57627 + "ep13out",
57628 + "ep14out",
57629 + "ep15out"
57630 + };
57631 +
57632 + int i;
57633 + struct usb_ep *ep;
57634 + int8_t dev_endpoints;
57635 +
57636 + DWC_DEBUGPL(DBG_PCDV, "%s\n", __func__);
57637 +
57638 + INIT_LIST_HEAD(&d->gadget.ep_list);
57639 + d->gadget.ep0 = &d->ep0;
57640 + d->gadget.speed = USB_SPEED_UNKNOWN;
57641 +
57642 + INIT_LIST_HEAD(&d->gadget.ep0->ep_list);
57643 +
57644 + /**
57645 + * Initialize the EP0 structure.
57646 + */
57647 + ep = &d->ep0;
57648 +
57649 + /* Init the usb_ep structure. */
57650 + ep->name = names[0];
57651 + ep->ops = (struct usb_ep_ops *)&dwc_otg_pcd_ep_ops;
57652 +
57653 + /**
57654 + * @todo NGS: What should the max packet size be set to
57655 + * here? Before EP type is set?
57656 + */
57657 + ep->maxpacket = MAX_PACKET_SIZE;
57658 + dwc_otg_pcd_ep_enable(d->pcd, NULL, ep);
57659 +
57660 + list_add_tail(&ep->ep_list, &d->gadget.ep_list);
57661 +
57662 + /**
57663 + * Initialize the EP structures.
57664 + */
57665 + dev_endpoints = d->pcd->core_if->dev_if->num_in_eps;
57666 +
57667 + for (i = 0; i < dev_endpoints; i++) {
57668 + ep = &d->in_ep[i];
57669 +
57670 + /* Init the usb_ep structure. */
57671 + ep->name = names[d->pcd->in_ep[i].dwc_ep.num];
57672 + ep->ops = (struct usb_ep_ops *)&dwc_otg_pcd_ep_ops;
57673 +
57674 + /**
57675 + * @todo NGS: What should the max packet size be set to
57676 + * here? Before EP type is set?
57677 + */
57678 + ep->maxpacket = MAX_PACKET_SIZE;
57679 + list_add_tail(&ep->ep_list, &d->gadget.ep_list);
57680 + }
57681 +
57682 + dev_endpoints = d->pcd->core_if->dev_if->num_out_eps;
57683 +
57684 + for (i = 0; i < dev_endpoints; i++) {
57685 + ep = &d->out_ep[i];
57686 +
57687 + /* Init the usb_ep structure. */
57688 + ep->name = names[15 + d->pcd->out_ep[i].dwc_ep.num];
57689 + ep->ops = (struct usb_ep_ops *)&dwc_otg_pcd_ep_ops;
57690 +
57691 + /**
57692 + * @todo NGS: What should the max packet size be set to
57693 + * here? Before EP type is set?
57694 + */
57695 + ep->maxpacket = MAX_PACKET_SIZE;
57696 +
57697 + list_add_tail(&ep->ep_list, &d->gadget.ep_list);
57698 + }
57699 +
57700 + /* remove ep0 from the list. There is a ep0 pointer. */
57701 + list_del_init(&d->ep0.ep_list);
57702 +
57703 + d->ep0.maxpacket = MAX_EP0_SIZE;
57704 +}
57705 +
57706 +/**
57707 + * This function releases the Gadget device.
57708 + * required by device_unregister().
57709 + *
57710 + * @todo Should this do something? Should it free the PCD?
57711 + */
57712 +static void dwc_otg_pcd_gadget_release(struct device *dev)
57713 +{
57714 + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, dev);
57715 +}
57716 +
57717 +static struct gadget_wrapper *alloc_wrapper(dwc_bus_dev_t *_dev)
57718 +{
57719 + static char pcd_name[] = "dwc_otg_pcd";
57720 + dwc_otg_device_t *otg_dev = DWC_OTG_BUSDRVDATA(_dev);
57721 + struct gadget_wrapper *d;
57722 + int retval;
57723 +
57724 + d = DWC_ALLOC(sizeof(*d));
57725 + if (d == NULL) {
57726 + return NULL;
57727 + }
57728 +
57729 + memset(d, 0, sizeof(*d));
57730 +
57731 + d->gadget.name = pcd_name;
57732 + d->pcd = otg_dev->pcd;
57733 +
57734 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30)
57735 + strcpy(d->gadget.dev.bus_id, "gadget");
57736 +#else
57737 + dev_set_name(&d->gadget.dev, "%s", "gadget");
57738 +#endif
57739 +
57740 + d->gadget.dev.parent = &_dev->dev;
57741 + d->gadget.dev.release = dwc_otg_pcd_gadget_release;
57742 + d->gadget.ops = &dwc_otg_pcd_ops;
57743 + d->gadget.max_speed = dwc_otg_pcd_is_dualspeed(otg_dev->pcd) ? USB_SPEED_HIGH:USB_SPEED_FULL;
57744 + d->gadget.is_otg = dwc_otg_pcd_is_otg(otg_dev->pcd);
57745 +
57746 + d->driver = 0;
57747 + /* Register the gadget device */
57748 + retval = device_register(&d->gadget.dev);
57749 + if (retval != 0) {
57750 + DWC_ERROR("device_register failed\n");
57751 + DWC_FREE(d);
57752 + return NULL;
57753 + }
57754 +
57755 + return d;
57756 +}
57757 +
57758 +static void free_wrapper(struct gadget_wrapper *d)
57759 +{
57760 + if (d->driver) {
57761 + /* should have been done already by driver model core */
57762 + DWC_WARN("driver '%s' is still registered\n",
57763 + d->driver->driver.name);
57764 +#ifdef CONFIG_USB_GADGET
57765 + usb_gadget_unregister_driver(d->driver);
57766 +#endif
57767 + }
57768 +
57769 + device_unregister(&d->gadget.dev);
57770 + DWC_FREE(d);
57771 +}
57772 +
57773 +/**
57774 + * This function initialized the PCD portion of the driver.
57775 + *
57776 + */
57777 +int pcd_init(dwc_bus_dev_t *_dev)
57778 +{
57779 + dwc_otg_device_t *otg_dev = DWC_OTG_BUSDRVDATA(_dev);
57780 + int retval = 0;
57781 +
57782 + DWC_DEBUGPL(DBG_PCDV, "%s(%p) otg_dev=%p\n", __func__, _dev, otg_dev);
57783 +
57784 + otg_dev->pcd = dwc_otg_pcd_init(otg_dev);
57785 +
57786 + if (!otg_dev->pcd) {
57787 + DWC_ERROR("dwc_otg_pcd_init failed\n");
57788 + return -ENOMEM;
57789 + }
57790 +
57791 + otg_dev->pcd->otg_dev = otg_dev;
57792 + gadget_wrapper = alloc_wrapper(_dev);
57793 +
57794 + /*
57795 + * Initialize EP structures
57796 + */
57797 + gadget_add_eps(gadget_wrapper);
57798 + /*
57799 + * Setup interupt handler
57800 + */
57801 +#ifdef PLATFORM_INTERFACE
57802 + DWC_DEBUGPL(DBG_ANY, "registering handler for irq%d\n",
57803 + platform_get_irq(_dev, fiq_enable ? 0 : 1));
57804 + retval = request_irq(platform_get_irq(_dev, fiq_enable ? 0 : 1), dwc_otg_pcd_irq,
57805 + IRQF_SHARED, gadget_wrapper->gadget.name,
57806 + otg_dev->pcd);
57807 + if (retval != 0) {
57808 + DWC_ERROR("request of irq%d failed\n",
57809 + platform_get_irq(_dev, fiq_enable ? 0 : 1));
57810 + free_wrapper(gadget_wrapper);
57811 + return -EBUSY;
57812 + }
57813 +#else
57814 + DWC_DEBUGPL(DBG_ANY, "registering handler for irq%d\n",
57815 + _dev->irq);
57816 + retval = request_irq(_dev->irq, dwc_otg_pcd_irq,
57817 + IRQF_SHARED | IRQF_DISABLED,
57818 + gadget_wrapper->gadget.name, otg_dev->pcd);
57819 + if (retval != 0) {
57820 + DWC_ERROR("request of irq%d failed\n", _dev->irq);
57821 + free_wrapper(gadget_wrapper);
57822 + return -EBUSY;
57823 + }
57824 +#endif
57825 +
57826 + dwc_otg_pcd_start(gadget_wrapper->pcd, &fops);
57827 +
57828 + return retval;
57829 +}
57830 +
57831 +/**
57832 + * Cleanup the PCD.
57833 + */
57834 +void pcd_remove(dwc_bus_dev_t *_dev)
57835 +{
57836 + dwc_otg_device_t *otg_dev = DWC_OTG_BUSDRVDATA(_dev);
57837 + dwc_otg_pcd_t *pcd = otg_dev->pcd;
57838 +
57839 + DWC_DEBUGPL(DBG_PCDV, "%s(%p) otg_dev %p\n", __func__, _dev, otg_dev);
57840 +
57841 + /*
57842 + * Free the IRQ
57843 + */
57844 +#ifdef PLATFORM_INTERFACE
57845 + free_irq(platform_get_irq(_dev, 0), pcd);
57846 +#else
57847 + free_irq(_dev->irq, pcd);
57848 +#endif
57849 + dwc_otg_pcd_remove(otg_dev->pcd);
57850 + free_wrapper(gadget_wrapper);
57851 + otg_dev->pcd = 0;
57852 +}
57853 +
57854 +#endif /* DWC_HOST_ONLY */
57855 --- /dev/null
57856 +++ b/drivers/usb/host/dwc_otg/dwc_otg_regs.h
57857 @@ -0,0 +1,2550 @@
57858 +/* ==========================================================================
57859 + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_regs.h $
57860 + * $Revision: #98 $
57861 + * $Date: 2012/08/10 $
57862 + * $Change: 2047372 $
57863 + *
57864 + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
57865 + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
57866 + * otherwise expressly agreed to in writing between Synopsys and you.
57867 + *
57868 + * The Software IS NOT an item of Licensed Software or Licensed Product under
57869 + * any End User Software License Agreement or Agreement for Licensed Product
57870 + * with Synopsys or any supplement thereto. You are permitted to use and
57871 + * redistribute this Software in source and binary forms, with or without
57872 + * modification, provided that redistributions of source code must retain this
57873 + * notice. You may not view, use, disclose, copy or distribute this file or
57874 + * any information contained herein except pursuant to this license grant from
57875 + * Synopsys. If you do not agree with this notice, including the disclaimer
57876 + * below, then you are not authorized to use the Software.
57877 + *
57878 + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
57879 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57880 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57881 + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
57882 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
57883 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
57884 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
57885 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57886 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57887 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
57888 + * DAMAGE.
57889 + * ========================================================================== */
57890 +
57891 +#ifndef __DWC_OTG_REGS_H__
57892 +#define __DWC_OTG_REGS_H__
57893 +
57894 +#include "dwc_otg_core_if.h"
57895 +
57896 +/**
57897 + * @file
57898 + *
57899 + * This file contains the data structures for accessing the DWC_otg core registers.
57900 + *
57901 + * The application interfaces with the HS OTG core by reading from and
57902 + * writing to the Control and Status Register (CSR) space through the
57903 + * AHB Slave interface. These registers are 32 bits wide, and the
57904 + * addresses are 32-bit-block aligned.
57905 + * CSRs are classified as follows:
57906 + * - Core Global Registers
57907 + * - Device Mode Registers
57908 + * - Device Global Registers
57909 + * - Device Endpoint Specific Registers
57910 + * - Host Mode Registers
57911 + * - Host Global Registers
57912 + * - Host Port CSRs
57913 + * - Host Channel Specific Registers
57914 + *
57915 + * Only the Core Global registers can be accessed in both Device and
57916 + * Host modes. When the HS OTG core is operating in one mode, either
57917 + * Device or Host, the application must not access registers from the
57918 + * other mode. When the core switches from one mode to another, the
57919 + * registers in the new mode of operation must be reprogrammed as they
57920 + * would be after a power-on reset.
57921 + */
57922 +
57923 +/****************************************************************************/
57924 +/** DWC_otg Core registers .
57925 + * The dwc_otg_core_global_regs structure defines the size
57926 + * and relative field offsets for the Core Global registers.
57927 + */
57928 +typedef struct dwc_otg_core_global_regs {
57929 + /** OTG Control and Status Register. <i>Offset: 000h</i> */
57930 + volatile uint32_t gotgctl;
57931 + /** OTG Interrupt Register. <i>Offset: 004h</i> */
57932 + volatile uint32_t gotgint;
57933 + /**Core AHB Configuration Register. <i>Offset: 008h</i> */
57934 + volatile uint32_t gahbcfg;
57935 +
57936 +#define DWC_GLBINTRMASK 0x0001
57937 +#define DWC_DMAENABLE 0x0020
57938 +#define DWC_NPTXEMPTYLVL_EMPTY 0x0080
57939 +#define DWC_NPTXEMPTYLVL_HALFEMPTY 0x0000
57940 +#define DWC_PTXEMPTYLVL_EMPTY 0x0100
57941 +#define DWC_PTXEMPTYLVL_HALFEMPTY 0x0000
57942 +
57943 + /**Core USB Configuration Register. <i>Offset: 00Ch</i> */
57944 + volatile uint32_t gusbcfg;
57945 + /**Core Reset Register. <i>Offset: 010h</i> */
57946 + volatile uint32_t grstctl;
57947 + /**Core Interrupt Register. <i>Offset: 014h</i> */
57948 + volatile uint32_t gintsts;
57949 + /**Core Interrupt Mask Register. <i>Offset: 018h</i> */
57950 + volatile uint32_t gintmsk;
57951 + /**Receive Status Queue Read Register (Read Only). <i>Offset: 01Ch</i> */
57952 + volatile uint32_t grxstsr;
57953 + /**Receive Status Queue Read & POP Register (Read Only). <i>Offset: 020h</i>*/
57954 + volatile uint32_t grxstsp;
57955 + /**Receive FIFO Size Register. <i>Offset: 024h</i> */
57956 + volatile uint32_t grxfsiz;
57957 + /**Non Periodic Transmit FIFO Size Register. <i>Offset: 028h</i> */
57958 + volatile uint32_t gnptxfsiz;
57959 + /**Non Periodic Transmit FIFO/Queue Status Register (Read
57960 + * Only). <i>Offset: 02Ch</i> */
57961 + volatile uint32_t gnptxsts;
57962 + /**I2C Access Register. <i>Offset: 030h</i> */
57963 + volatile uint32_t gi2cctl;
57964 + /**PHY Vendor Control Register. <i>Offset: 034h</i> */
57965 + volatile uint32_t gpvndctl;
57966 + /**General Purpose Input/Output Register. <i>Offset: 038h</i> */
57967 + volatile uint32_t ggpio;
57968 + /**User ID Register. <i>Offset: 03Ch</i> */
57969 + volatile uint32_t guid;
57970 + /**Synopsys ID Register (Read Only). <i>Offset: 040h</i> */
57971 + volatile uint32_t gsnpsid;
57972 + /**User HW Config1 Register (Read Only). <i>Offset: 044h</i> */
57973 + volatile uint32_t ghwcfg1;
57974 + /**User HW Config2 Register (Read Only). <i>Offset: 048h</i> */
57975 + volatile uint32_t ghwcfg2;
57976 +#define DWC_SLAVE_ONLY_ARCH 0
57977 +#define DWC_EXT_DMA_ARCH 1
57978 +#define DWC_INT_DMA_ARCH 2
57979 +
57980 +#define DWC_MODE_HNP_SRP_CAPABLE 0
57981 +#define DWC_MODE_SRP_ONLY_CAPABLE 1
57982 +#define DWC_MODE_NO_HNP_SRP_CAPABLE 2
57983 +#define DWC_MODE_SRP_CAPABLE_DEVICE 3
57984 +#define DWC_MODE_NO_SRP_CAPABLE_DEVICE 4
57985 +#define DWC_MODE_SRP_CAPABLE_HOST 5
57986 +#define DWC_MODE_NO_SRP_CAPABLE_HOST 6
57987 +
57988 + /**User HW Config3 Register (Read Only). <i>Offset: 04Ch</i> */
57989 + volatile uint32_t ghwcfg3;
57990 + /**User HW Config4 Register (Read Only). <i>Offset: 050h</i>*/
57991 + volatile uint32_t ghwcfg4;
57992 + /** Core LPM Configuration register <i>Offset: 054h</i>*/
57993 + volatile uint32_t glpmcfg;
57994 + /** Global PowerDn Register <i>Offset: 058h</i> */
57995 + volatile uint32_t gpwrdn;
57996 + /** Global DFIFO SW Config Register <i>Offset: 05Ch</i> */
57997 + volatile uint32_t gdfifocfg;
57998 + /** ADP Control Register <i>Offset: 060h</i> */
57999 + volatile uint32_t adpctl;
58000 + /** Reserved <i>Offset: 064h-0FFh</i> */
58001 + volatile uint32_t reserved39[39];
58002 + /** Host Periodic Transmit FIFO Size Register. <i>Offset: 100h</i> */
58003 + volatile uint32_t hptxfsiz;
58004 + /** Device Periodic Transmit FIFO#n Register if dedicated fifos are disabled,
58005 + otherwise Device Transmit FIFO#n Register.
58006 + * <i>Offset: 104h + (FIFO_Number-1)*04h, 1 <= FIFO Number <= 15 (1<=n<=15).</i> */
58007 + volatile uint32_t dtxfsiz[15];
58008 +} dwc_otg_core_global_regs_t;
58009 +
58010 +/**
58011 + * This union represents the bit fields of the Core OTG Control
58012 + * and Status Register (GOTGCTL). Set the bits using the bit
58013 + * fields then write the <i>d32</i> value to the register.
58014 + */
58015 +typedef union gotgctl_data {
58016 + /** raw register data */
58017 + uint32_t d32;
58018 + /** register bits */
58019 + struct {
58020 + unsigned sesreqscs:1;
58021 + unsigned sesreq:1;
58022 + unsigned vbvalidoven:1;
58023 + unsigned vbvalidovval:1;
58024 + unsigned avalidoven:1;
58025 + unsigned avalidovval:1;
58026 + unsigned bvalidoven:1;
58027 + unsigned bvalidovval:1;
58028 + unsigned hstnegscs:1;
58029 + unsigned hnpreq:1;
58030 + unsigned hstsethnpen:1;
58031 + unsigned devhnpen:1;
58032 + unsigned reserved12_15:4;
58033 + unsigned conidsts:1;
58034 + unsigned dbnctime:1;
58035 + unsigned asesvld:1;
58036 + unsigned bsesvld:1;
58037 + unsigned otgver:1;
58038 + unsigned reserved1:1;
58039 + unsigned multvalidbc:5;
58040 + unsigned chirpen:1;
58041 + unsigned reserved28_31:4;
58042 + } b;
58043 +} gotgctl_data_t;
58044 +
58045 +/**
58046 + * This union represents the bit fields of the Core OTG Interrupt Register
58047 + * (GOTGINT). Set/clear the bits using the bit fields then write the <i>d32</i>
58048 + * value to the register.
58049 + */
58050 +typedef union gotgint_data {
58051 + /** raw register data */
58052 + uint32_t d32;
58053 + /** register bits */
58054 + struct {
58055 + /** Current Mode */
58056 + unsigned reserved0_1:2;
58057 +
58058 + /** Session End Detected */
58059 + unsigned sesenddet:1;
58060 +
58061 + unsigned reserved3_7:5;
58062 +
58063 + /** Session Request Success Status Change */
58064 + unsigned sesreqsucstschng:1;
58065 + /** Host Negotiation Success Status Change */
58066 + unsigned hstnegsucstschng:1;
58067 +
58068 + unsigned reserved10_16:7;
58069 +
58070 + /** Host Negotiation Detected */
58071 + unsigned hstnegdet:1;
58072 + /** A-Device Timeout Change */
58073 + unsigned adevtoutchng:1;
58074 + /** Debounce Done */
58075 + unsigned debdone:1;
58076 + /** Multi-Valued input changed */
58077 + unsigned mvic:1;
58078 +
58079 + unsigned reserved31_21:11;
58080 +
58081 + } b;
58082 +} gotgint_data_t;
58083 +
58084 +/**
58085 + * This union represents the bit fields of the Core AHB Configuration
58086 + * Register (GAHBCFG). Set/clear the bits using the bit fields then
58087 + * write the <i>d32</i> value to the register.
58088 + */
58089 +typedef union gahbcfg_data {
58090 + /** raw register data */
58091 + uint32_t d32;
58092 + /** register bits */
58093 + struct {
58094 + unsigned glblintrmsk:1;
58095 +#define DWC_GAHBCFG_GLBINT_ENABLE 1
58096 +
58097 + unsigned hburstlen:4;
58098 +#define DWC_GAHBCFG_INT_DMA_BURST_SINGLE 0
58099 +#define DWC_GAHBCFG_INT_DMA_BURST_INCR 1
58100 +#define DWC_GAHBCFG_INT_DMA_BURST_INCR4 3
58101 +#define DWC_GAHBCFG_INT_DMA_BURST_INCR8 5
58102 +#define DWC_GAHBCFG_INT_DMA_BURST_INCR16 7
58103 +
58104 + unsigned dmaenable:1;
58105 +#define DWC_GAHBCFG_DMAENABLE 1
58106 + unsigned reserved:1;
58107 + unsigned nptxfemplvl_txfemplvl:1;
58108 + unsigned ptxfemplvl:1;
58109 +#define DWC_GAHBCFG_TXFEMPTYLVL_EMPTY 1
58110 +#define DWC_GAHBCFG_TXFEMPTYLVL_HALFEMPTY 0
58111 + unsigned reserved9_20:12;
58112 + unsigned remmemsupp:1;
58113 + unsigned notialldmawrit:1;
58114 + unsigned ahbsingle:1;
58115 + unsigned reserved24_31:8;
58116 + } b;
58117 +} gahbcfg_data_t;
58118 +
58119 +/**
58120 + * This union represents the bit fields of the Core USB Configuration
58121 + * Register (GUSBCFG). Set the bits using the bit fields then write
58122 + * the <i>d32</i> value to the register.
58123 + */
58124 +typedef union gusbcfg_data {
58125 + /** raw register data */
58126 + uint32_t d32;
58127 + /** register bits */
58128 + struct {
58129 + unsigned toutcal:3;
58130 + unsigned phyif:1;
58131 + unsigned ulpi_utmi_sel:1;
58132 + unsigned fsintf:1;
58133 + unsigned physel:1;
58134 + unsigned ddrsel:1;
58135 + unsigned srpcap:1;
58136 + unsigned hnpcap:1;
58137 + unsigned usbtrdtim:4;
58138 + unsigned reserved1:1;
58139 + unsigned phylpwrclksel:1;
58140 + unsigned otgutmifssel:1;
58141 + unsigned ulpi_fsls:1;
58142 + unsigned ulpi_auto_res:1;
58143 + unsigned ulpi_clk_sus_m:1;
58144 + unsigned ulpi_ext_vbus_drv:1;
58145 + unsigned ulpi_int_vbus_indicator:1;
58146 + unsigned term_sel_dl_pulse:1;
58147 + unsigned indicator_complement:1;
58148 + unsigned indicator_pass_through:1;
58149 + unsigned ulpi_int_prot_dis:1;
58150 + unsigned ic_usb_cap:1;
58151 + unsigned ic_traffic_pull_remove:1;
58152 + unsigned tx_end_delay:1;
58153 + unsigned force_host_mode:1;
58154 + unsigned force_dev_mode:1;
58155 + unsigned reserved31:1;
58156 + } b;
58157 +} gusbcfg_data_t;
58158 +
58159 +/**
58160 + * This union represents the bit fields of the Core Reset Register
58161 + * (GRSTCTL). Set/clear the bits using the bit fields then write the
58162 + * <i>d32</i> value to the register.
58163 + */
58164 +typedef union grstctl_data {
58165 + /** raw register data */
58166 + uint32_t d32;
58167 + /** register bits */
58168 + struct {
58169 + /** Core Soft Reset (CSftRst) (Device and Host)
58170 + *
58171 + * The application can flush the control logic in the
58172 + * entire core using this bit. This bit resets the
58173 + * pipelines in the AHB Clock domain as well as the
58174 + * PHY Clock domain.
58175 + *
58176 + * The state machines are reset to an IDLE state, the
58177 + * control bits in the CSRs are cleared, all the
58178 + * transmit FIFOs and the receive FIFO are flushed.
58179 + *
58180 + * The status mask bits that control the generation of
58181 + * the interrupt, are cleared, to clear the
58182 + * interrupt. The interrupt status bits are not
58183 + * cleared, so the application can get the status of
58184 + * any events that occurred in the core after it has
58185 + * set this bit.
58186 + *
58187 + * Any transactions on the AHB are terminated as soon
58188 + * as possible following the protocol. Any
58189 + * transactions on the USB are terminated immediately.
58190 + *
58191 + * The configuration settings in the CSRs are
58192 + * unchanged, so the software doesn't have to
58193 + * reprogram these registers (Device
58194 + * Configuration/Host Configuration/Core System
58195 + * Configuration/Core PHY Configuration).
58196 + *
58197 + * The application can write to this bit, any time it
58198 + * wants to reset the core. This is a self clearing
58199 + * bit and the core clears this bit after all the
58200 + * necessary logic is reset in the core, which may
58201 + * take several clocks, depending on the current state
58202 + * of the core.
58203 + */
58204 + unsigned csftrst:1;
58205 + /** Hclk Soft Reset
58206 + *
58207 + * The application uses this bit to reset the control logic in
58208 + * the AHB clock domain. Only AHB clock domain pipelines are
58209 + * reset.
58210 + */
58211 + unsigned hsftrst:1;
58212 + /** Host Frame Counter Reset (Host Only)<br>
58213 + *
58214 + * The application can reset the (micro)frame number
58215 + * counter inside the core, using this bit. When the
58216 + * (micro)frame counter is reset, the subsequent SOF
58217 + * sent out by the core, will have a (micro)frame
58218 + * number of 0.
58219 + */
58220 + unsigned hstfrm:1;
58221 + /** In Token Sequence Learning Queue Flush
58222 + * (INTknQFlsh) (Device Only)
58223 + */
58224 + unsigned intknqflsh:1;
58225 + /** RxFIFO Flush (RxFFlsh) (Device and Host)
58226 + *
58227 + * The application can flush the entire Receive FIFO
58228 + * using this bit. The application must first
58229 + * ensure that the core is not in the middle of a
58230 + * transaction. The application should write into
58231 + * this bit, only after making sure that neither the
58232 + * DMA engine is reading from the RxFIFO nor the MAC
58233 + * is writing the data in to the FIFO. The
58234 + * application should wait until the bit is cleared
58235 + * before performing any other operations. This bit
58236 + * will takes 8 clocks (slowest of PHY or AHB clock)
58237 + * to clear.
58238 + */
58239 + unsigned rxfflsh:1;
58240 + /** TxFIFO Flush (TxFFlsh) (Device and Host).
58241 + *
58242 + * This bit is used to selectively flush a single or
58243 + * all transmit FIFOs. The application must first
58244 + * ensure that the core is not in the middle of a
58245 + * transaction. The application should write into
58246 + * this bit, only after making sure that neither the
58247 + * DMA engine is writing into the TxFIFO nor the MAC
58248 + * is reading the data out of the FIFO. The
58249 + * application should wait until the core clears this
58250 + * bit, before performing any operations. This bit
58251 + * will takes 8 clocks (slowest of PHY or AHB clock)
58252 + * to clear.
58253 + */
58254 + unsigned txfflsh:1;
58255 +
58256 + /** TxFIFO Number (TxFNum) (Device and Host).
58257 + *
58258 + * This is the FIFO number which needs to be flushed,
58259 + * using the TxFIFO Flush bit. This field should not
58260 + * be changed until the TxFIFO Flush bit is cleared by
58261 + * the core.
58262 + * - 0x0 : Non Periodic TxFIFO Flush
58263 + * - 0x1 : Periodic TxFIFO #1 Flush in device mode
58264 + * or Periodic TxFIFO in host mode
58265 + * - 0x2 : Periodic TxFIFO #2 Flush in device mode.
58266 + * - ...
58267 + * - 0xF : Periodic TxFIFO #15 Flush in device mode
58268 + * - 0x10: Flush all the Transmit NonPeriodic and
58269 + * Transmit Periodic FIFOs in the core
58270 + */
58271 + unsigned txfnum:5;
58272 + /** Reserved */
58273 + unsigned reserved11_29:19;
58274 + /** DMA Request Signal. Indicated DMA request is in
58275 + * probress. Used for debug purpose. */
58276 + unsigned dmareq:1;
58277 + /** AHB Master Idle. Indicates the AHB Master State
58278 + * Machine is in IDLE condition. */
58279 + unsigned ahbidle:1;
58280 + } b;
58281 +} grstctl_t;
58282 +
58283 +/**
58284 + * This union represents the bit fields of the Core Interrupt Mask
58285 + * Register (GINTMSK). Set/clear the bits using the bit fields then
58286 + * write the <i>d32</i> value to the register.
58287 + */
58288 +typedef union gintmsk_data {
58289 + /** raw register data */
58290 + uint32_t d32;
58291 + /** register bits */
58292 + struct {
58293 + unsigned reserved0:1;
58294 + unsigned modemismatch:1;
58295 + unsigned otgintr:1;
58296 + unsigned sofintr:1;
58297 + unsigned rxstsqlvl:1;
58298 + unsigned nptxfempty:1;
58299 + unsigned ginnakeff:1;
58300 + unsigned goutnakeff:1;
58301 + unsigned ulpickint:1;
58302 + unsigned i2cintr:1;
58303 + unsigned erlysuspend:1;
58304 + unsigned usbsuspend:1;
58305 + unsigned usbreset:1;
58306 + unsigned enumdone:1;
58307 + unsigned isooutdrop:1;
58308 + unsigned eopframe:1;
58309 + unsigned restoredone:1;
58310 + unsigned epmismatch:1;
58311 + unsigned inepintr:1;
58312 + unsigned outepintr:1;
58313 + unsigned incomplisoin:1;
58314 + unsigned incomplisoout:1;
58315 + unsigned fetsusp:1;
58316 + unsigned resetdet:1;
58317 + unsigned portintr:1;
58318 + unsigned hcintr:1;
58319 + unsigned ptxfempty:1;
58320 + unsigned lpmtranrcvd:1;
58321 + unsigned conidstschng:1;
58322 + unsigned disconnect:1;
58323 + unsigned sessreqintr:1;
58324 + unsigned wkupintr:1;
58325 + } b;
58326 +} gintmsk_data_t;
58327 +/**
58328 + * This union represents the bit fields of the Core Interrupt Register
58329 + * (GINTSTS). Set/clear the bits using the bit fields then write the
58330 + * <i>d32</i> value to the register.
58331 + */
58332 +typedef union gintsts_data {
58333 + /** raw register data */
58334 + uint32_t d32;
58335 +#define DWC_SOF_INTR_MASK 0x0008
58336 + /** register bits */
58337 + struct {
58338 +#define DWC_HOST_MODE 1
58339 + unsigned curmode:1;
58340 + unsigned modemismatch:1;
58341 + unsigned otgintr:1;
58342 + unsigned sofintr:1;
58343 + unsigned rxstsqlvl:1;
58344 + unsigned nptxfempty:1;
58345 + unsigned ginnakeff:1;
58346 + unsigned goutnakeff:1;
58347 + unsigned ulpickint:1;
58348 + unsigned i2cintr:1;
58349 + unsigned erlysuspend:1;
58350 + unsigned usbsuspend:1;
58351 + unsigned usbreset:1;
58352 + unsigned enumdone:1;
58353 + unsigned isooutdrop:1;
58354 + unsigned eopframe:1;
58355 + unsigned restoredone:1;
58356 + unsigned epmismatch:1;
58357 + unsigned inepint:1;
58358 + unsigned outepintr:1;
58359 + unsigned incomplisoin:1;
58360 + unsigned incomplisoout:1;
58361 + unsigned fetsusp:1;
58362 + unsigned resetdet:1;
58363 + unsigned portintr:1;
58364 + unsigned hcintr:1;
58365 + unsigned ptxfempty:1;
58366 + unsigned lpmtranrcvd:1;
58367 + unsigned conidstschng:1;
58368 + unsigned disconnect:1;
58369 + unsigned sessreqintr:1;
58370 + unsigned wkupintr:1;
58371 + } b;
58372 +} gintsts_data_t;
58373 +
58374 +/**
58375 + * This union represents the bit fields in the Device Receive Status Read and
58376 + * Pop Registers (GRXSTSR, GRXSTSP) Read the register into the <i>d32</i>
58377 + * element then read out the bits using the <i>b</i>it elements.
58378 + */
58379 +typedef union device_grxsts_data {
58380 + /** raw register data */
58381 + uint32_t d32;
58382 + /** register bits */
58383 + struct {
58384 + unsigned epnum:4;
58385 + unsigned bcnt:11;
58386 + unsigned dpid:2;
58387 +
58388 +#define DWC_STS_DATA_UPDT 0x2 // OUT Data Packet
58389 +#define DWC_STS_XFER_COMP 0x3 // OUT Data Transfer Complete
58390 +
58391 +#define DWC_DSTS_GOUT_NAK 0x1 // Global OUT NAK
58392 +#define DWC_DSTS_SETUP_COMP 0x4 // Setup Phase Complete
58393 +#define DWC_DSTS_SETUP_UPDT 0x6 // SETUP Packet
58394 + unsigned pktsts:4;
58395 + unsigned fn:4;
58396 + unsigned reserved25_31:7;
58397 + } b;
58398 +} device_grxsts_data_t;
58399 +
58400 +/**
58401 + * This union represents the bit fields in the Host Receive Status Read and
58402 + * Pop Registers (GRXSTSR, GRXSTSP) Read the register into the <i>d32</i>
58403 + * element then read out the bits using the <i>b</i>it elements.
58404 + */
58405 +typedef union host_grxsts_data {
58406 + /** raw register data */
58407 + uint32_t d32;
58408 + /** register bits */
58409 + struct {
58410 + unsigned chnum:4;
58411 + unsigned bcnt:11;
58412 + unsigned dpid:2;
58413 +
58414 + unsigned pktsts:4;
58415 +#define DWC_GRXSTS_PKTSTS_IN 0x2
58416 +#define DWC_GRXSTS_PKTSTS_IN_XFER_COMP 0x3
58417 +#define DWC_GRXSTS_PKTSTS_DATA_TOGGLE_ERR 0x5
58418 +#define DWC_GRXSTS_PKTSTS_CH_HALTED 0x7
58419 +
58420 + unsigned reserved21_31:11;
58421 + } b;
58422 +} host_grxsts_data_t;
58423 +
58424 +/**
58425 + * This union represents the bit fields in the FIFO Size Registers (HPTXFSIZ,
58426 + * GNPTXFSIZ, DPTXFSIZn, DIEPTXFn). Read the register into the <i>d32</i> element
58427 + * then read out the bits using the <i>b</i>it elements.
58428 + */
58429 +typedef union fifosize_data {
58430 + /** raw register data */
58431 + uint32_t d32;
58432 + /** register bits */
58433 + struct {
58434 + unsigned startaddr:16;
58435 + unsigned depth:16;
58436 + } b;
58437 +} fifosize_data_t;
58438 +
58439 +/**
58440 + * This union represents the bit fields in the Non-Periodic Transmit
58441 + * FIFO/Queue Status Register (GNPTXSTS). Read the register into the
58442 + * <i>d32</i> element then read out the bits using the <i>b</i>it
58443 + * elements.
58444 + */
58445 +typedef union gnptxsts_data {
58446 + /** raw register data */
58447 + uint32_t d32;
58448 + /** register bits */
58449 + struct {
58450 + unsigned nptxfspcavail:16;
58451 + unsigned nptxqspcavail:8;
58452 + /** Top of the Non-Periodic Transmit Request Queue
58453 + * - bit 24 - Terminate (Last entry for the selected
58454 + * channel/EP)
58455 + * - bits 26:25 - Token Type
58456 + * - 2'b00 - IN/OUT
58457 + * - 2'b01 - Zero Length OUT
58458 + * - 2'b10 - PING/Complete Split
58459 + * - 2'b11 - Channel Halt
58460 + * - bits 30:27 - Channel/EP Number
58461 + */
58462 + unsigned nptxqtop_terminate:1;
58463 + unsigned nptxqtop_token:2;
58464 + unsigned nptxqtop_chnep:4;
58465 + unsigned reserved:1;
58466 + } b;
58467 +} gnptxsts_data_t;
58468 +
58469 +/**
58470 + * This union represents the bit fields in the Transmit
58471 + * FIFO Status Register (DTXFSTS). Read the register into the
58472 + * <i>d32</i> element then read out the bits using the <i>b</i>it
58473 + * elements.
58474 + */
58475 +typedef union dtxfsts_data {
58476 + /** raw register data */
58477 + uint32_t d32;
58478 + /** register bits */
58479 + struct {
58480 + unsigned txfspcavail:16;
58481 + unsigned reserved:16;
58482 + } b;
58483 +} dtxfsts_data_t;
58484 +
58485 +/**
58486 + * This union represents the bit fields in the I2C Control Register
58487 + * (I2CCTL). Read the register into the <i>d32</i> element then read out the
58488 + * bits using the <i>b</i>it elements.
58489 + */
58490 +typedef union gi2cctl_data {
58491 + /** raw register data */
58492 + uint32_t d32;
58493 + /** register bits */
58494 + struct {
58495 + unsigned rwdata:8;
58496 + unsigned regaddr:8;
58497 + unsigned addr:7;
58498 + unsigned i2cen:1;
58499 + unsigned ack:1;
58500 + unsigned i2csuspctl:1;
58501 + unsigned i2cdevaddr:2;
58502 + unsigned i2cdatse0:1;
58503 + unsigned reserved:1;
58504 + unsigned rw:1;
58505 + unsigned bsydne:1;
58506 + } b;
58507 +} gi2cctl_data_t;
58508 +
58509 +/**
58510 + * This union represents the bit fields in the PHY Vendor Control Register
58511 + * (GPVNDCTL). Read the register into the <i>d32</i> element then read out the
58512 + * bits using the <i>b</i>it elements.
58513 + */
58514 +typedef union gpvndctl_data {
58515 + /** raw register data */
58516 + uint32_t d32;
58517 + /** register bits */
58518 + struct {
58519 + unsigned regdata:8;
58520 + unsigned vctrl:8;
58521 + unsigned regaddr16_21:6;
58522 + unsigned regwr:1;
58523 + unsigned reserved23_24:2;
58524 + unsigned newregreq:1;
58525 + unsigned vstsbsy:1;
58526 + unsigned vstsdone:1;
58527 + unsigned reserved28_30:3;
58528 + unsigned disulpidrvr:1;
58529 + } b;
58530 +} gpvndctl_data_t;
58531 +
58532 +/**
58533 + * This union represents the bit fields in the General Purpose
58534 + * Input/Output Register (GGPIO).
58535 + * Read the register into the <i>d32</i> element then read out the
58536 + * bits using the <i>b</i>it elements.
58537 + */
58538 +typedef union ggpio_data {
58539 + /** raw register data */
58540 + uint32_t d32;
58541 + /** register bits */
58542 + struct {
58543 + unsigned gpi:16;
58544 + unsigned gpo:16;
58545 + } b;
58546 +} ggpio_data_t;
58547 +
58548 +/**
58549 + * This union represents the bit fields in the User ID Register
58550 + * (GUID). Read the register into the <i>d32</i> element then read out the
58551 + * bits using the <i>b</i>it elements.
58552 + */
58553 +typedef union guid_data {
58554 + /** raw register data */
58555 + uint32_t d32;
58556 + /** register bits */
58557 + struct {
58558 + unsigned rwdata:32;
58559 + } b;
58560 +} guid_data_t;
58561 +
58562 +/**
58563 + * This union represents the bit fields in the Synopsys ID Register
58564 + * (GSNPSID). Read the register into the <i>d32</i> element then read out the
58565 + * bits using the <i>b</i>it elements.
58566 + */
58567 +typedef union gsnpsid_data {
58568 + /** raw register data */
58569 + uint32_t d32;
58570 + /** register bits */
58571 + struct {
58572 + unsigned rwdata:32;
58573 + } b;
58574 +} gsnpsid_data_t;
58575 +
58576 +/**
58577 + * This union represents the bit fields in the User HW Config1
58578 + * Register. Read the register into the <i>d32</i> element then read
58579 + * out the bits using the <i>b</i>it elements.
58580 + */
58581 +typedef union hwcfg1_data {
58582 + /** raw register data */
58583 + uint32_t d32;
58584 + /** register bits */
58585 + struct {
58586 + unsigned ep_dir0:2;
58587 + unsigned ep_dir1:2;
58588 + unsigned ep_dir2:2;
58589 + unsigned ep_dir3:2;
58590 + unsigned ep_dir4:2;
58591 + unsigned ep_dir5:2;
58592 + unsigned ep_dir6:2;
58593 + unsigned ep_dir7:2;
58594 + unsigned ep_dir8:2;
58595 + unsigned ep_dir9:2;
58596 + unsigned ep_dir10:2;
58597 + unsigned ep_dir11:2;
58598 + unsigned ep_dir12:2;
58599 + unsigned ep_dir13:2;
58600 + unsigned ep_dir14:2;
58601 + unsigned ep_dir15:2;
58602 + } b;
58603 +} hwcfg1_data_t;
58604 +
58605 +/**
58606 + * This union represents the bit fields in the User HW Config2
58607 + * Register. Read the register into the <i>d32</i> element then read
58608 + * out the bits using the <i>b</i>it elements.
58609 + */
58610 +typedef union hwcfg2_data {
58611 + /** raw register data */
58612 + uint32_t d32;
58613 + /** register bits */
58614 + struct {
58615 + /* GHWCFG2 */
58616 + unsigned op_mode:3;
58617 +#define DWC_HWCFG2_OP_MODE_HNP_SRP_CAPABLE_OTG 0
58618 +#define DWC_HWCFG2_OP_MODE_SRP_ONLY_CAPABLE_OTG 1
58619 +#define DWC_HWCFG2_OP_MODE_NO_HNP_SRP_CAPABLE_OTG 2
58620 +#define DWC_HWCFG2_OP_MODE_SRP_CAPABLE_DEVICE 3
58621 +#define DWC_HWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE 4
58622 +#define DWC_HWCFG2_OP_MODE_SRP_CAPABLE_HOST 5
58623 +#define DWC_HWCFG2_OP_MODE_NO_SRP_CAPABLE_HOST 6
58624 +
58625 + unsigned architecture:2;
58626 + unsigned point2point:1;
58627 + unsigned hs_phy_type:2;
58628 +#define DWC_HWCFG2_HS_PHY_TYPE_NOT_SUPPORTED 0
58629 +#define DWC_HWCFG2_HS_PHY_TYPE_UTMI 1
58630 +#define DWC_HWCFG2_HS_PHY_TYPE_ULPI 2
58631 +#define DWC_HWCFG2_HS_PHY_TYPE_UTMI_ULPI 3
58632 +
58633 + unsigned fs_phy_type:2;
58634 + unsigned num_dev_ep:4;
58635 + unsigned num_host_chan:4;
58636 + unsigned perio_ep_supported:1;
58637 + unsigned dynamic_fifo:1;
58638 + unsigned multi_proc_int:1;
58639 + unsigned reserved21:1;
58640 + unsigned nonperio_tx_q_depth:2;
58641 + unsigned host_perio_tx_q_depth:2;
58642 + unsigned dev_token_q_depth:5;
58643 + unsigned otg_enable_ic_usb:1;
58644 + } b;
58645 +} hwcfg2_data_t;
58646 +
58647 +/**
58648 + * This union represents the bit fields in the User HW Config3
58649 + * Register. Read the register into the <i>d32</i> element then read
58650 + * out the bits using the <i>b</i>it elements.
58651 + */
58652 +typedef union hwcfg3_data {
58653 + /** raw register data */
58654 + uint32_t d32;
58655 + /** register bits */
58656 + struct {
58657 + /* GHWCFG3 */
58658 + unsigned xfer_size_cntr_width:4;
58659 + unsigned packet_size_cntr_width:3;
58660 + unsigned otg_func:1;
58661 + unsigned i2c:1;
58662 + unsigned vendor_ctrl_if:1;
58663 + unsigned optional_features:1;
58664 + unsigned synch_reset_type:1;
58665 + unsigned adp_supp:1;
58666 + unsigned otg_enable_hsic:1;
58667 + unsigned bc_support:1;
58668 + unsigned otg_lpm_en:1;
58669 + unsigned dfifo_depth:16;
58670 + } b;
58671 +} hwcfg3_data_t;
58672 +
58673 +/**
58674 + * This union represents the bit fields in the User HW Config4
58675 + * Register. Read the register into the <i>d32</i> element then read
58676 + * out the bits using the <i>b</i>it elements.
58677 + */
58678 +typedef union hwcfg4_data {
58679 + /** raw register data */
58680 + uint32_t d32;
58681 + /** register bits */
58682 + struct {
58683 + unsigned num_dev_perio_in_ep:4;
58684 + unsigned power_optimiz:1;
58685 + unsigned min_ahb_freq:1;
58686 + unsigned hiber:1;
58687 + unsigned xhiber:1;
58688 + unsigned reserved:6;
58689 + unsigned utmi_phy_data_width:2;
58690 + unsigned num_dev_mode_ctrl_ep:4;
58691 + unsigned iddig_filt_en:1;
58692 + unsigned vbus_valid_filt_en:1;
58693 + unsigned a_valid_filt_en:1;
58694 + unsigned b_valid_filt_en:1;
58695 + unsigned session_end_filt_en:1;
58696 + unsigned ded_fifo_en:1;
58697 + unsigned num_in_eps:4;
58698 + unsigned desc_dma:1;
58699 + unsigned desc_dma_dyn:1;
58700 + } b;
58701 +} hwcfg4_data_t;
58702 +
58703 +/**
58704 + * This union represents the bit fields of the Core LPM Configuration
58705 + * Register (GLPMCFG). Set the bits using bit fields then write
58706 + * the <i>d32</i> value to the register.
58707 + */
58708 +typedef union glpmctl_data {
58709 + /** raw register data */
58710 + uint32_t d32;
58711 + /** register bits */
58712 + struct {
58713 + /** LPM-Capable (LPMCap) (Device and Host)
58714 + * The application uses this bit to control
58715 + * the DWC_otg core LPM capabilities.
58716 + */
58717 + unsigned lpm_cap_en:1;
58718 + /** LPM response programmed by application (AppL1Res) (Device)
58719 + * Handshake response to LPM token pre-programmed
58720 + * by device application software.
58721 + */
58722 + unsigned appl_resp:1;
58723 + /** Host Initiated Resume Duration (HIRD) (Device and Host)
58724 + * In Host mode this field indicates the value of HIRD
58725 + * to be sent in an LPM transaction.
58726 + * In Device mode this field is updated with the
58727 + * Received LPM Token HIRD bmAttribute
58728 + * when an ACK/NYET/STALL response is sent
58729 + * to an LPM transaction.
58730 + */
58731 + unsigned hird:4;
58732 + /** RemoteWakeEnable (bRemoteWake) (Device and Host)
58733 + * In Host mode this bit indicates the value of remote
58734 + * wake up to be sent in wIndex field of LPM transaction.
58735 + * In Device mode this field is updated with the
58736 + * Received LPM Token bRemoteWake bmAttribute
58737 + * when an ACK/NYET/STALL response is sent
58738 + * to an LPM transaction.
58739 + */
58740 + unsigned rem_wkup_en:1;
58741 + /** Enable utmi_sleep_n (EnblSlpM) (Device and Host)
58742 + * The application uses this bit to control
58743 + * the utmi_sleep_n assertion to the PHY when in L1 state.
58744 + */
58745 + unsigned en_utmi_sleep:1;
58746 + /** HIRD Threshold (HIRD_Thres) (Device and Host)
58747 + */
58748 + unsigned hird_thres:5;
58749 + /** LPM Response (CoreL1Res) (Device and Host)
58750 + * In Host mode this bit contains handsake response to
58751 + * LPM transaction.
58752 + * In Device mode the response of the core to
58753 + * LPM transaction received is reflected in these two bits.
58754 + - 0x0 : ERROR (No handshake response)
58755 + - 0x1 : STALL
58756 + - 0x2 : NYET
58757 + - 0x3 : ACK
58758 + */
58759 + unsigned lpm_resp:2;
58760 + /** Port Sleep Status (SlpSts) (Device and Host)
58761 + * This bit is set as long as a Sleep condition
58762 + * is present on the USB bus.
58763 + */
58764 + unsigned prt_sleep_sts:1;
58765 + /** Sleep State Resume OK (L1ResumeOK) (Device and Host)
58766 + * Indicates that the application or host
58767 + * can start resume from Sleep state.
58768 + */
58769 + unsigned sleep_state_resumeok:1;
58770 + /** LPM channel Index (LPM_Chnl_Indx) (Host)
58771 + * The channel number on which the LPM transaction
58772 + * has to be applied while sending
58773 + * an LPM transaction to the local device.
58774 + */
58775 + unsigned lpm_chan_index:4;
58776 + /** LPM Retry Count (LPM_Retry_Cnt) (Host)
58777 + * Number host retries that would be performed
58778 + * if the device response was not valid response.
58779 + */
58780 + unsigned retry_count:3;
58781 + /** Send LPM Transaction (SndLPM) (Host)
58782 + * When set by application software,
58783 + * an LPM transaction containing two tokens
58784 + * is sent.
58785 + */
58786 + unsigned send_lpm:1;
58787 + /** LPM Retry status (LPM_RetryCnt_Sts) (Host)
58788 + * Number of LPM Host Retries still remaining
58789 + * to be transmitted for the current LPM sequence
58790 + */
58791 + unsigned retry_count_sts:3;
58792 + unsigned reserved28_29:2;
58793 + /** In host mode once this bit is set, the host
58794 + * configures to drive the HSIC Idle state on the bus.
58795 + * It then waits for the device to initiate the Connect sequence.
58796 + * In device mode once this bit is set, the device waits for
58797 + * the HSIC Idle line state on the bus. Upon receving the Idle
58798 + * line state, it initiates the HSIC Connect sequence.
58799 + */
58800 + unsigned hsic_connect:1;
58801 + /** This bit overrides and functionally inverts
58802 + * the if_select_hsic input port signal.
58803 + */
58804 + unsigned inv_sel_hsic:1;
58805 + } b;
58806 +} glpmcfg_data_t;
58807 +
58808 +/**
58809 + * This union represents the bit fields of the Core ADP Timer, Control and
58810 + * Status Register (ADPTIMCTLSTS). Set the bits using bit fields then write
58811 + * the <i>d32</i> value to the register.
58812 + */
58813 +typedef union adpctl_data {
58814 + /** raw register data */
58815 + uint32_t d32;
58816 + /** register bits */
58817 + struct {
58818 + /** Probe Discharge (PRB_DSCHG)
58819 + * These bits set the times for TADP_DSCHG.
58820 + * These bits are defined as follows:
58821 + * 2'b00 - 4 msec
58822 + * 2'b01 - 8 msec
58823 + * 2'b10 - 16 msec
58824 + * 2'b11 - 32 msec
58825 + */
58826 + unsigned prb_dschg:2;
58827 + /** Probe Delta (PRB_DELTA)
58828 + * These bits set the resolution for RTIM value.
58829 + * The bits are defined in units of 32 kHz clock cycles as follows:
58830 + * 2'b00 - 1 cycles
58831 + * 2'b01 - 2 cycles
58832 + * 2'b10 - 3 cycles
58833 + * 2'b11 - 4 cycles
58834 + * For example if this value is chosen to 2'b01, it means that RTIM
58835 + * increments for every 3(three) 32Khz clock cycles.
58836 + */
58837 + unsigned prb_delta:2;
58838 + /** Probe Period (PRB_PER)
58839 + * These bits sets the TADP_PRD as shown in Figure 4 as follows:
58840 + * 2'b00 - 0.625 to 0.925 sec (typical 0.775 sec)
58841 + * 2'b01 - 1.25 to 1.85 sec (typical 1.55 sec)
58842 + * 2'b10 - 1.9 to 2.6 sec (typical 2.275 sec)
58843 + * 2'b11 - Reserved
58844 + */
58845 + unsigned prb_per:2;
58846 + /** These bits capture the latest time it took for VBUS to ramp from
58847 + * VADP_SINK to VADP_PRB.
58848 + * 0x000 - 1 cycles
58849 + * 0x001 - 2 cycles
58850 + * 0x002 - 3 cycles
58851 + * etc
58852 + * 0x7FF - 2048 cycles
58853 + * A time of 1024 cycles at 32 kHz corresponds to a time of 32 msec.
58854 + */
58855 + unsigned rtim:11;
58856 + /** Enable Probe (EnaPrb)
58857 + * When programmed to 1'b1, the core performs a probe operation.
58858 + * This bit is valid only if OTG_Ver = 1'b1.
58859 + */
58860 + unsigned enaprb:1;
58861 + /** Enable Sense (EnaSns)
58862 + * When programmed to 1'b1, the core performs a Sense operation.
58863 + * This bit is valid only if OTG_Ver = 1'b1.
58864 + */
58865 + unsigned enasns:1;
58866 + /** ADP Reset (ADPRes)
58867 + * When set, ADP controller is reset.
58868 + * This bit is valid only if OTG_Ver = 1'b1.
58869 + */
58870 + unsigned adpres:1;
58871 + /** ADP Enable (ADPEn)
58872 + * When set, the core performs either ADP probing or sensing
58873 + * based on EnaPrb or EnaSns.
58874 + * This bit is valid only if OTG_Ver = 1'b1.
58875 + */
58876 + unsigned adpen:1;
58877 + /** ADP Probe Interrupt (ADP_PRB_INT)
58878 + * When this bit is set, it means that the VBUS
58879 + * voltage is greater than VADP_PRB or VADP_PRB is reached.
58880 + * This bit is valid only if OTG_Ver = 1'b1.
58881 + */
58882 + unsigned adp_prb_int:1;
58883 + /**
58884 + * ADP Sense Interrupt (ADP_SNS_INT)
58885 + * When this bit is set, it means that the VBUS voltage is greater than
58886 + * VADP_SNS value or VADP_SNS is reached.
58887 + * This bit is valid only if OTG_Ver = 1'b1.
58888 + */
58889 + unsigned adp_sns_int:1;
58890 + /** ADP Tomeout Interrupt (ADP_TMOUT_INT)
58891 + * This bit is relevant only for an ADP probe.
58892 + * When this bit is set, it means that the ramp time has
58893 + * completed ie ADPCTL.RTIM has reached its terminal value
58894 + * of 0x7FF. This is a debug feature that allows software
58895 + * to read the ramp time after each cycle.
58896 + * This bit is valid only if OTG_Ver = 1'b1.
58897 + */
58898 + unsigned adp_tmout_int:1;
58899 + /** ADP Probe Interrupt Mask (ADP_PRB_INT_MSK)
58900 + * When this bit is set, it unmasks the interrupt due to ADP_PRB_INT.
58901 + * This bit is valid only if OTG_Ver = 1'b1.
58902 + */
58903 + unsigned adp_prb_int_msk:1;
58904 + /** ADP Sense Interrupt Mask (ADP_SNS_INT_MSK)
58905 + * When this bit is set, it unmasks the interrupt due to ADP_SNS_INT.
58906 + * This bit is valid only if OTG_Ver = 1'b1.
58907 + */
58908 + unsigned adp_sns_int_msk:1;
58909 + /** ADP Timoeout Interrupt Mask (ADP_TMOUT_MSK)
58910 + * When this bit is set, it unmasks the interrupt due to ADP_TMOUT_INT.
58911 + * This bit is valid only if OTG_Ver = 1'b1.
58912 + */
58913 + unsigned adp_tmout_int_msk:1;
58914 + /** Access Request
58915 + * 2'b00 - Read/Write Valid (updated by the core)
58916 + * 2'b01 - Read
58917 + * 2'b00 - Write
58918 + * 2'b00 - Reserved
58919 + */
58920 + unsigned ar:2;
58921 + /** Reserved */
58922 + unsigned reserved29_31:3;
58923 + } b;
58924 +} adpctl_data_t;
58925 +
58926 +////////////////////////////////////////////
58927 +// Device Registers
58928 +/**
58929 + * Device Global Registers. <i>Offsets 800h-BFFh</i>
58930 + *
58931 + * The following structures define the size and relative field offsets
58932 + * for the Device Mode Registers.
58933 + *
58934 + * <i>These registers are visible only in Device mode and must not be
58935 + * accessed in Host mode, as the results are unknown.</i>
58936 + */
58937 +typedef struct dwc_otg_dev_global_regs {
58938 + /** Device Configuration Register. <i>Offset 800h</i> */
58939 + volatile uint32_t dcfg;
58940 + /** Device Control Register. <i>Offset: 804h</i> */
58941 + volatile uint32_t dctl;
58942 + /** Device Status Register (Read Only). <i>Offset: 808h</i> */
58943 + volatile uint32_t dsts;
58944 + /** Reserved. <i>Offset: 80Ch</i> */
58945 + uint32_t unused;
58946 + /** Device IN Endpoint Common Interrupt Mask
58947 + * Register. <i>Offset: 810h</i> */
58948 + volatile uint32_t diepmsk;
58949 + /** Device OUT Endpoint Common Interrupt Mask
58950 + * Register. <i>Offset: 814h</i> */
58951 + volatile uint32_t doepmsk;
58952 + /** Device All Endpoints Interrupt Register. <i>Offset: 818h</i> */
58953 + volatile uint32_t daint;
58954 + /** Device All Endpoints Interrupt Mask Register. <i>Offset:
58955 + * 81Ch</i> */
58956 + volatile uint32_t daintmsk;
58957 + /** Device IN Token Queue Read Register-1 (Read Only).
58958 + * <i>Offset: 820h</i> */
58959 + volatile uint32_t dtknqr1;
58960 + /** Device IN Token Queue Read Register-2 (Read Only).
58961 + * <i>Offset: 824h</i> */
58962 + volatile uint32_t dtknqr2;
58963 + /** Device VBUS discharge Register. <i>Offset: 828h</i> */
58964 + volatile uint32_t dvbusdis;
58965 + /** Device VBUS Pulse Register. <i>Offset: 82Ch</i> */
58966 + volatile uint32_t dvbuspulse;
58967 + /** Device IN Token Queue Read Register-3 (Read Only). /
58968 + * Device Thresholding control register (Read/Write)
58969 + * <i>Offset: 830h</i> */
58970 + volatile uint32_t dtknqr3_dthrctl;
58971 + /** Device IN Token Queue Read Register-4 (Read Only). /
58972 + * Device IN EPs empty Inr. Mask Register (Read/Write)
58973 + * <i>Offset: 834h</i> */
58974 + volatile uint32_t dtknqr4_fifoemptymsk;
58975 + /** Device Each Endpoint Interrupt Register (Read Only). /
58976 + * <i>Offset: 838h</i> */
58977 + volatile uint32_t deachint;
58978 + /** Device Each Endpoint Interrupt mask Register (Read/Write). /
58979 + * <i>Offset: 83Ch</i> */
58980 + volatile uint32_t deachintmsk;
58981 + /** Device Each In Endpoint Interrupt mask Register (Read/Write). /
58982 + * <i>Offset: 840h</i> */
58983 + volatile uint32_t diepeachintmsk[MAX_EPS_CHANNELS];
58984 + /** Device Each Out Endpoint Interrupt mask Register (Read/Write). /
58985 + * <i>Offset: 880h</i> */
58986 + volatile uint32_t doepeachintmsk[MAX_EPS_CHANNELS];
58987 +} dwc_otg_device_global_regs_t;
58988 +
58989 +/**
58990 + * This union represents the bit fields in the Device Configuration
58991 + * Register. Read the register into the <i>d32</i> member then
58992 + * set/clear the bits using the <i>b</i>it elements. Write the
58993 + * <i>d32</i> member to the dcfg register.
58994 + */
58995 +typedef union dcfg_data {
58996 + /** raw register data */
58997 + uint32_t d32;
58998 + /** register bits */
58999 + struct {
59000 + /** Device Speed */
59001 + unsigned devspd:2;
59002 + /** Non Zero Length Status OUT Handshake */
59003 + unsigned nzstsouthshk:1;
59004 +#define DWC_DCFG_SEND_STALL 1
59005 +
59006 + unsigned ena32khzs:1;
59007 + /** Device Addresses */
59008 + unsigned devaddr:7;
59009 + /** Periodic Frame Interval */
59010 + unsigned perfrint:2;
59011 +#define DWC_DCFG_FRAME_INTERVAL_80 0
59012 +#define DWC_DCFG_FRAME_INTERVAL_85 1
59013 +#define DWC_DCFG_FRAME_INTERVAL_90 2
59014 +#define DWC_DCFG_FRAME_INTERVAL_95 3
59015 +
59016 + /** Enable Device OUT NAK for bulk in DDMA mode */
59017 + unsigned endevoutnak:1;
59018 +
59019 + unsigned reserved14_17:4;
59020 + /** In Endpoint Mis-match count */
59021 + unsigned epmscnt:5;
59022 + /** Enable Descriptor DMA in Device mode */
59023 + unsigned descdma:1;
59024 + unsigned perschintvl:2;
59025 + unsigned resvalid:6;
59026 + } b;
59027 +} dcfg_data_t;
59028 +
59029 +/**
59030 + * This union represents the bit fields in the Device Control
59031 + * Register. Read the register into the <i>d32</i> member then
59032 + * set/clear the bits using the <i>b</i>it elements.
59033 + */
59034 +typedef union dctl_data {
59035 + /** raw register data */
59036 + uint32_t d32;
59037 + /** register bits */
59038 + struct {
59039 + /** Remote Wakeup */
59040 + unsigned rmtwkupsig:1;
59041 + /** Soft Disconnect */
59042 + unsigned sftdiscon:1;
59043 + /** Global Non-Periodic IN NAK Status */
59044 + unsigned gnpinnaksts:1;
59045 + /** Global OUT NAK Status */
59046 + unsigned goutnaksts:1;
59047 + /** Test Control */
59048 + unsigned tstctl:3;
59049 + /** Set Global Non-Periodic IN NAK */
59050 + unsigned sgnpinnak:1;
59051 + /** Clear Global Non-Periodic IN NAK */
59052 + unsigned cgnpinnak:1;
59053 + /** Set Global OUT NAK */
59054 + unsigned sgoutnak:1;
59055 + /** Clear Global OUT NAK */
59056 + unsigned cgoutnak:1;
59057 + /** Power-On Programming Done */
59058 + unsigned pwronprgdone:1;
59059 + /** Reserved */
59060 + unsigned reserved:1;
59061 + /** Global Multi Count */
59062 + unsigned gmc:2;
59063 + /** Ignore Frame Number for ISOC EPs */
59064 + unsigned ifrmnum:1;
59065 + /** NAK on Babble */
59066 + unsigned nakonbble:1;
59067 + /** Enable Continue on BNA */
59068 + unsigned encontonbna:1;
59069 +
59070 + unsigned reserved18_31:14;
59071 + } b;
59072 +} dctl_data_t;
59073 +
59074 +/**
59075 + * This union represents the bit fields in the Device Status
59076 + * Register. Read the register into the <i>d32</i> member then
59077 + * set/clear the bits using the <i>b</i>it elements.
59078 + */
59079 +typedef union dsts_data {
59080 + /** raw register data */
59081 + uint32_t d32;
59082 + /** register bits */
59083 + struct {
59084 + /** Suspend Status */
59085 + unsigned suspsts:1;
59086 + /** Enumerated Speed */
59087 + unsigned enumspd:2;
59088 +#define DWC_DSTS_ENUMSPD_HS_PHY_30MHZ_OR_60MHZ 0
59089 +#define DWC_DSTS_ENUMSPD_FS_PHY_30MHZ_OR_60MHZ 1
59090 +#define DWC_DSTS_ENUMSPD_LS_PHY_6MHZ 2
59091 +#define DWC_DSTS_ENUMSPD_FS_PHY_48MHZ 3
59092 + /** Erratic Error */
59093 + unsigned errticerr:1;
59094 + unsigned reserved4_7:4;
59095 + /** Frame or Microframe Number of the received SOF */
59096 + unsigned soffn:14;
59097 + unsigned reserved22_31:10;
59098 + } b;
59099 +} dsts_data_t;
59100 +
59101 +/**
59102 + * This union represents the bit fields in the Device IN EP Interrupt
59103 + * Register and the Device IN EP Common Mask Register.
59104 + *
59105 + * - Read the register into the <i>d32</i> member then set/clear the
59106 + * bits using the <i>b</i>it elements.
59107 + */
59108 +typedef union diepint_data {
59109 + /** raw register data */
59110 + uint32_t d32;
59111 + /** register bits */
59112 + struct {
59113 + /** Transfer complete mask */
59114 + unsigned xfercompl:1;
59115 + /** Endpoint disable mask */
59116 + unsigned epdisabled:1;
59117 + /** AHB Error mask */
59118 + unsigned ahberr:1;
59119 + /** TimeOUT Handshake mask (non-ISOC EPs) */
59120 + unsigned timeout:1;
59121 + /** IN Token received with TxF Empty mask */
59122 + unsigned intktxfemp:1;
59123 + /** IN Token Received with EP mismatch mask */
59124 + unsigned intknepmis:1;
59125 + /** IN Endpoint NAK Effective mask */
59126 + unsigned inepnakeff:1;
59127 + /** Reserved */
59128 + unsigned emptyintr:1;
59129 +
59130 + unsigned txfifoundrn:1;
59131 +
59132 + /** BNA Interrupt mask */
59133 + unsigned bna:1;
59134 +
59135 + unsigned reserved10_12:3;
59136 + /** BNA Interrupt mask */
59137 + unsigned nak:1;
59138 +
59139 + unsigned reserved14_31:18;
59140 + } b;
59141 +} diepint_data_t;
59142 +
59143 +/**
59144 + * This union represents the bit fields in the Device IN EP
59145 + * Common/Dedicated Interrupt Mask Register.
59146 + */
59147 +typedef union diepint_data diepmsk_data_t;
59148 +
59149 +/**
59150 + * This union represents the bit fields in the Device OUT EP Interrupt
59151 + * Registerand Device OUT EP Common Interrupt Mask Register.
59152 + *
59153 + * - Read the register into the <i>d32</i> member then set/clear the
59154 + * bits using the <i>b</i>it elements.
59155 + */
59156 +typedef union doepint_data {
59157 + /** raw register data */
59158 + uint32_t d32;
59159 + /** register bits */
59160 + struct {
59161 + /** Transfer complete */
59162 + unsigned xfercompl:1;
59163 + /** Endpoint disable */
59164 + unsigned epdisabled:1;
59165 + /** AHB Error */
59166 + unsigned ahberr:1;
59167 + /** Setup Phase Done (contorl EPs) */
59168 + unsigned setup:1;
59169 + /** OUT Token Received when Endpoint Disabled */
59170 + unsigned outtknepdis:1;
59171 +
59172 + unsigned stsphsercvd:1;
59173 + /** Back-to-Back SETUP Packets Received */
59174 + unsigned back2backsetup:1;
59175 +
59176 + unsigned reserved7:1;
59177 + /** OUT packet Error */
59178 + unsigned outpkterr:1;
59179 + /** BNA Interrupt */
59180 + unsigned bna:1;
59181 +
59182 + unsigned reserved10:1;
59183 + /** Packet Drop Status */
59184 + unsigned pktdrpsts:1;
59185 + /** Babble Interrupt */
59186 + unsigned babble:1;
59187 + /** NAK Interrupt */
59188 + unsigned nak:1;
59189 + /** NYET Interrupt */
59190 + unsigned nyet:1;
59191 + /** Bit indicating setup packet received */
59192 + unsigned sr:1;
59193 +
59194 + unsigned reserved16_31:16;
59195 + } b;
59196 +} doepint_data_t;
59197 +
59198 +/**
59199 + * This union represents the bit fields in the Device OUT EP
59200 + * Common/Dedicated Interrupt Mask Register.
59201 + */
59202 +typedef union doepint_data doepmsk_data_t;
59203 +
59204 +/**
59205 + * This union represents the bit fields in the Device All EP Interrupt
59206 + * and Mask Registers.
59207 + * - Read the register into the <i>d32</i> member then set/clear the
59208 + * bits using the <i>b</i>it elements.
59209 + */
59210 +typedef union daint_data {
59211 + /** raw register data */
59212 + uint32_t d32;
59213 + /** register bits */
59214 + struct {
59215 + /** IN Endpoint bits */
59216 + unsigned in:16;
59217 + /** OUT Endpoint bits */
59218 + unsigned out:16;
59219 + } ep;
59220 + struct {
59221 + /** IN Endpoint bits */
59222 + unsigned inep0:1;
59223 + unsigned inep1:1;
59224 + unsigned inep2:1;
59225 + unsigned inep3:1;
59226 + unsigned inep4:1;
59227 + unsigned inep5:1;
59228 + unsigned inep6:1;
59229 + unsigned inep7:1;
59230 + unsigned inep8:1;
59231 + unsigned inep9:1;
59232 + unsigned inep10:1;
59233 + unsigned inep11:1;
59234 + unsigned inep12:1;
59235 + unsigned inep13:1;
59236 + unsigned inep14:1;
59237 + unsigned inep15:1;
59238 + /** OUT Endpoint bits */
59239 + unsigned outep0:1;
59240 + unsigned outep1:1;
59241 + unsigned outep2:1;
59242 + unsigned outep3:1;
59243 + unsigned outep4:1;
59244 + unsigned outep5:1;
59245 + unsigned outep6:1;
59246 + unsigned outep7:1;
59247 + unsigned outep8:1;
59248 + unsigned outep9:1;
59249 + unsigned outep10:1;
59250 + unsigned outep11:1;
59251 + unsigned outep12:1;
59252 + unsigned outep13:1;
59253 + unsigned outep14:1;
59254 + unsigned outep15:1;
59255 + } b;
59256 +} daint_data_t;
59257 +
59258 +/**
59259 + * This union represents the bit fields in the Device IN Token Queue
59260 + * Read Registers.
59261 + * - Read the register into the <i>d32</i> member.
59262 + * - READ-ONLY Register
59263 + */
59264 +typedef union dtknq1_data {
59265 + /** raw register data */
59266 + uint32_t d32;
59267 + /** register bits */
59268 + struct {
59269 + /** In Token Queue Write Pointer */
59270 + unsigned intknwptr:5;
59271 + /** Reserved */
59272 + unsigned reserved05_06:2;
59273 + /** write pointer has wrapped. */
59274 + unsigned wrap_bit:1;
59275 + /** EP Numbers of IN Tokens 0 ... 4 */
59276 + unsigned epnums0_5:24;
59277 + } b;
59278 +} dtknq1_data_t;
59279 +
59280 +/**
59281 + * This union represents Threshold control Register
59282 + * - Read and write the register into the <i>d32</i> member.
59283 + * - READ-WRITABLE Register
59284 + */
59285 +typedef union dthrctl_data {
59286 + /** raw register data */
59287 + uint32_t d32;
59288 + /** register bits */
59289 + struct {
59290 + /** non ISO Tx Thr. Enable */
59291 + unsigned non_iso_thr_en:1;
59292 + /** ISO Tx Thr. Enable */
59293 + unsigned iso_thr_en:1;
59294 + /** Tx Thr. Length */
59295 + unsigned tx_thr_len:9;
59296 + /** AHB Threshold ratio */
59297 + unsigned ahb_thr_ratio:2;
59298 + /** Reserved */
59299 + unsigned reserved13_15:3;
59300 + /** Rx Thr. Enable */
59301 + unsigned rx_thr_en:1;
59302 + /** Rx Thr. Length */
59303 + unsigned rx_thr_len:9;
59304 + unsigned reserved26:1;
59305 + /** Arbiter Parking Enable*/
59306 + unsigned arbprken:1;
59307 + /** Reserved */
59308 + unsigned reserved28_31:4;
59309 + } b;
59310 +} dthrctl_data_t;
59311 +
59312 +/**
59313 + * Device Logical IN Endpoint-Specific Registers. <i>Offsets
59314 + * 900h-AFCh</i>
59315 + *
59316 + * There will be one set of endpoint registers per logical endpoint
59317 + * implemented.
59318 + *
59319 + * <i>These registers are visible only in Device mode and must not be
59320 + * accessed in Host mode, as the results are unknown.</i>
59321 + */
59322 +typedef struct dwc_otg_dev_in_ep_regs {
59323 + /** Device IN Endpoint Control Register. <i>Offset:900h +
59324 + * (ep_num * 20h) + 00h</i> */
59325 + volatile uint32_t diepctl;
59326 + /** Reserved. <i>Offset:900h + (ep_num * 20h) + 04h</i> */
59327 + uint32_t reserved04;
59328 + /** Device IN Endpoint Interrupt Register. <i>Offset:900h +
59329 + * (ep_num * 20h) + 08h</i> */
59330 + volatile uint32_t diepint;
59331 + /** Reserved. <i>Offset:900h + (ep_num * 20h) + 0Ch</i> */
59332 + uint32_t reserved0C;
59333 + /** Device IN Endpoint Transfer Size
59334 + * Register. <i>Offset:900h + (ep_num * 20h) + 10h</i> */
59335 + volatile uint32_t dieptsiz;
59336 + /** Device IN Endpoint DMA Address Register. <i>Offset:900h +
59337 + * (ep_num * 20h) + 14h</i> */
59338 + volatile uint32_t diepdma;
59339 + /** Device IN Endpoint Transmit FIFO Status Register. <i>Offset:900h +
59340 + * (ep_num * 20h) + 18h</i> */
59341 + volatile uint32_t dtxfsts;
59342 + /** Device IN Endpoint DMA Buffer Register. <i>Offset:900h +
59343 + * (ep_num * 20h) + 1Ch</i> */
59344 + volatile uint32_t diepdmab;
59345 +} dwc_otg_dev_in_ep_regs_t;
59346 +
59347 +/**
59348 + * Device Logical OUT Endpoint-Specific Registers. <i>Offsets:
59349 + * B00h-CFCh</i>
59350 + *
59351 + * There will be one set of endpoint registers per logical endpoint
59352 + * implemented.
59353 + *
59354 + * <i>These registers are visible only in Device mode and must not be
59355 + * accessed in Host mode, as the results are unknown.</i>
59356 + */
59357 +typedef struct dwc_otg_dev_out_ep_regs {
59358 + /** Device OUT Endpoint Control Register. <i>Offset:B00h +
59359 + * (ep_num * 20h) + 00h</i> */
59360 + volatile uint32_t doepctl;
59361 + /** Reserved. <i>Offset:B00h + (ep_num * 20h) + 04h</i> */
59362 + uint32_t reserved04;
59363 + /** Device OUT Endpoint Interrupt Register. <i>Offset:B00h +
59364 + * (ep_num * 20h) + 08h</i> */
59365 + volatile uint32_t doepint;
59366 + /** Reserved. <i>Offset:B00h + (ep_num * 20h) + 0Ch</i> */
59367 + uint32_t reserved0C;
59368 + /** Device OUT Endpoint Transfer Size Register. <i>Offset:
59369 + * B00h + (ep_num * 20h) + 10h</i> */
59370 + volatile uint32_t doeptsiz;
59371 + /** Device OUT Endpoint DMA Address Register. <i>Offset:B00h
59372 + * + (ep_num * 20h) + 14h</i> */
59373 + volatile uint32_t doepdma;
59374 + /** Reserved. <i>Offset:B00h + * (ep_num * 20h) + 18h</i> */
59375 + uint32_t unused;
59376 + /** Device OUT Endpoint DMA Buffer Register. <i>Offset:B00h
59377 + * + (ep_num * 20h) + 1Ch</i> */
59378 + uint32_t doepdmab;
59379 +} dwc_otg_dev_out_ep_regs_t;
59380 +
59381 +/**
59382 + * This union represents the bit fields in the Device EP Control
59383 + * Register. Read the register into the <i>d32</i> member then
59384 + * set/clear the bits using the <i>b</i>it elements.
59385 + */
59386 +typedef union depctl_data {
59387 + /** raw register data */
59388 + uint32_t d32;
59389 + /** register bits */
59390 + struct {
59391 + /** Maximum Packet Size
59392 + * IN/OUT EPn
59393 + * IN/OUT EP0 - 2 bits
59394 + * 2'b00: 64 Bytes
59395 + * 2'b01: 32
59396 + * 2'b10: 16
59397 + * 2'b11: 8 */
59398 + unsigned mps:11;
59399 +#define DWC_DEP0CTL_MPS_64 0
59400 +#define DWC_DEP0CTL_MPS_32 1
59401 +#define DWC_DEP0CTL_MPS_16 2
59402 +#define DWC_DEP0CTL_MPS_8 3
59403 +
59404 + /** Next Endpoint
59405 + * IN EPn/IN EP0
59406 + * OUT EPn/OUT EP0 - reserved */
59407 + unsigned nextep:4;
59408 +
59409 + /** USB Active Endpoint */
59410 + unsigned usbactep:1;
59411 +
59412 + /** Endpoint DPID (INTR/Bulk IN and OUT endpoints)
59413 + * This field contains the PID of the packet going to
59414 + * be received or transmitted on this endpoint. The
59415 + * application should program the PID of the first
59416 + * packet going to be received or transmitted on this
59417 + * endpoint , after the endpoint is
59418 + * activated. Application use the SetD1PID and
59419 + * SetD0PID fields of this register to program either
59420 + * D0 or D1 PID.
59421 + *
59422 + * The encoding for this field is
59423 + * - 0: D0
59424 + * - 1: D1
59425 + */
59426 + unsigned dpid:1;
59427 +
59428 + /** NAK Status */
59429 + unsigned naksts:1;
59430 +
59431 + /** Endpoint Type
59432 + * 2'b00: Control
59433 + * 2'b01: Isochronous
59434 + * 2'b10: Bulk
59435 + * 2'b11: Interrupt */
59436 + unsigned eptype:2;
59437 +
59438 + /** Snoop Mode
59439 + * OUT EPn/OUT EP0
59440 + * IN EPn/IN EP0 - reserved */
59441 + unsigned snp:1;
59442 +
59443 + /** Stall Handshake */
59444 + unsigned stall:1;
59445 +
59446 + /** Tx Fifo Number
59447 + * IN EPn/IN EP0
59448 + * OUT EPn/OUT EP0 - reserved */
59449 + unsigned txfnum:4;
59450 +
59451 + /** Clear NAK */
59452 + unsigned cnak:1;
59453 + /** Set NAK */
59454 + unsigned snak:1;
59455 + /** Set DATA0 PID (INTR/Bulk IN and OUT endpoints)
59456 + * Writing to this field sets the Endpoint DPID (DPID)
59457 + * field in this register to DATA0. Set Even
59458 + * (micro)frame (SetEvenFr) (ISO IN and OUT Endpoints)
59459 + * Writing to this field sets the Even/Odd
59460 + * (micro)frame (EO_FrNum) field to even (micro)
59461 + * frame.
59462 + */
59463 + unsigned setd0pid:1;
59464 + /** Set DATA1 PID (INTR/Bulk IN and OUT endpoints)
59465 + * Writing to this field sets the Endpoint DPID (DPID)
59466 + * field in this register to DATA1 Set Odd
59467 + * (micro)frame (SetOddFr) (ISO IN and OUT Endpoints)
59468 + * Writing to this field sets the Even/Odd
59469 + * (micro)frame (EO_FrNum) field to odd (micro) frame.
59470 + */
59471 + unsigned setd1pid:1;
59472 +
59473 + /** Endpoint Disable */
59474 + unsigned epdis:1;
59475 + /** Endpoint Enable */
59476 + unsigned epena:1;
59477 + } b;
59478 +} depctl_data_t;
59479 +
59480 +/**
59481 + * This union represents the bit fields in the Device EP Transfer
59482 + * Size Register. Read the register into the <i>d32</i> member then
59483 + * set/clear the bits using the <i>b</i>it elements.
59484 + */
59485 +typedef union deptsiz_data {
59486 + /** raw register data */
59487 + uint32_t d32;
59488 + /** register bits */
59489 + struct {
59490 + /** Transfer size */
59491 + unsigned xfersize:19;
59492 +/** Max packet count for EP (pow(2,10)-1) */
59493 +#define MAX_PKT_CNT 1023
59494 + /** Packet Count */
59495 + unsigned pktcnt:10;
59496 + /** Multi Count - Periodic IN endpoints */
59497 + unsigned mc:2;
59498 + unsigned reserved:1;
59499 + } b;
59500 +} deptsiz_data_t;
59501 +
59502 +/**
59503 + * This union represents the bit fields in the Device EP 0 Transfer
59504 + * Size Register. Read the register into the <i>d32</i> member then
59505 + * set/clear the bits using the <i>b</i>it elements.
59506 + */
59507 +typedef union deptsiz0_data {
59508 + /** raw register data */
59509 + uint32_t d32;
59510 + /** register bits */
59511 + struct {
59512 + /** Transfer size */
59513 + unsigned xfersize:7;
59514 + /** Reserved */
59515 + unsigned reserved7_18:12;
59516 + /** Packet Count */
59517 + unsigned pktcnt:2;
59518 + /** Reserved */
59519 + unsigned reserved21_28:8;
59520 + /**Setup Packet Count (DOEPTSIZ0 Only) */
59521 + unsigned supcnt:2;
59522 + unsigned reserved31;
59523 + } b;
59524 +} deptsiz0_data_t;
59525 +
59526 +/////////////////////////////////////////////////
59527 +// DMA Descriptor Specific Structures
59528 +//
59529 +
59530 +/** Buffer status definitions */
59531 +
59532 +#define BS_HOST_READY 0x0
59533 +#define BS_DMA_BUSY 0x1
59534 +#define BS_DMA_DONE 0x2
59535 +#define BS_HOST_BUSY 0x3
59536 +
59537 +/** Receive/Transmit status definitions */
59538 +
59539 +#define RTS_SUCCESS 0x0
59540 +#define RTS_BUFFLUSH 0x1
59541 +#define RTS_RESERVED 0x2
59542 +#define RTS_BUFERR 0x3
59543 +
59544 +/**
59545 + * This union represents the bit fields in the DMA Descriptor
59546 + * status quadlet. Read the quadlet into the <i>d32</i> member then
59547 + * set/clear the bits using the <i>b</i>it, <i>b_iso_out</i> and
59548 + * <i>b_iso_in</i> elements.
59549 + */
59550 +typedef union dev_dma_desc_sts {
59551 + /** raw register data */
59552 + uint32_t d32;
59553 + /** quadlet bits */
59554 + struct {
59555 + /** Received number of bytes */
59556 + unsigned bytes:16;
59557 + /** NAK bit - only for OUT EPs */
59558 + unsigned nak:1;
59559 + unsigned reserved17_22:6;
59560 + /** Multiple Transfer - only for OUT EPs */
59561 + unsigned mtrf:1;
59562 + /** Setup Packet received - only for OUT EPs */
59563 + unsigned sr:1;
59564 + /** Interrupt On Complete */
59565 + unsigned ioc:1;
59566 + /** Short Packet */
59567 + unsigned sp:1;
59568 + /** Last */
59569 + unsigned l:1;
59570 + /** Receive Status */
59571 + unsigned sts:2;
59572 + /** Buffer Status */
59573 + unsigned bs:2;
59574 + } b;
59575 +
59576 +//#ifdef DWC_EN_ISOC
59577 + /** iso out quadlet bits */
59578 + struct {
59579 + /** Received number of bytes */
59580 + unsigned rxbytes:11;
59581 +
59582 + unsigned reserved11:1;
59583 + /** Frame Number */
59584 + unsigned framenum:11;
59585 + /** Received ISO Data PID */
59586 + unsigned pid:2;
59587 + /** Interrupt On Complete */
59588 + unsigned ioc:1;
59589 + /** Short Packet */
59590 + unsigned sp:1;
59591 + /** Last */
59592 + unsigned l:1;
59593 + /** Receive Status */
59594 + unsigned rxsts:2;
59595 + /** Buffer Status */
59596 + unsigned bs:2;
59597 + } b_iso_out;
59598 +
59599 + /** iso in quadlet bits */
59600 + struct {
59601 + /** Transmited number of bytes */
59602 + unsigned txbytes:12;
59603 + /** Frame Number */
59604 + unsigned framenum:11;
59605 + /** Transmited ISO Data PID */
59606 + unsigned pid:2;
59607 + /** Interrupt On Complete */
59608 + unsigned ioc:1;
59609 + /** Short Packet */
59610 + unsigned sp:1;
59611 + /** Last */
59612 + unsigned l:1;
59613 + /** Transmit Status */
59614 + unsigned txsts:2;
59615 + /** Buffer Status */
59616 + unsigned bs:2;
59617 + } b_iso_in;
59618 +//#endif /* DWC_EN_ISOC */
59619 +} dev_dma_desc_sts_t;
59620 +
59621 +/**
59622 + * DMA Descriptor structure
59623 + *
59624 + * DMA Descriptor structure contains two quadlets:
59625 + * Status quadlet and Data buffer pointer.
59626 + */
59627 +typedef struct dwc_otg_dev_dma_desc {
59628 + /** DMA Descriptor status quadlet */
59629 + dev_dma_desc_sts_t status;
59630 + /** DMA Descriptor data buffer pointer */
59631 + uint32_t buf;
59632 +} dwc_otg_dev_dma_desc_t;
59633 +
59634 +/**
59635 + * The dwc_otg_dev_if structure contains information needed to manage
59636 + * the DWC_otg controller acting in device mode. It represents the
59637 + * programming view of the device-specific aspects of the controller.
59638 + */
59639 +typedef struct dwc_otg_dev_if {
59640 + /** Pointer to device Global registers.
59641 + * Device Global Registers starting at offset 800h
59642 + */
59643 + dwc_otg_device_global_regs_t *dev_global_regs;
59644 +#define DWC_DEV_GLOBAL_REG_OFFSET 0x800
59645 +
59646 + /**
59647 + * Device Logical IN Endpoint-Specific Registers 900h-AFCh
59648 + */
59649 + dwc_otg_dev_in_ep_regs_t *in_ep_regs[MAX_EPS_CHANNELS];
59650 +#define DWC_DEV_IN_EP_REG_OFFSET 0x900
59651 +#define DWC_EP_REG_OFFSET 0x20
59652 +
59653 + /** Device Logical OUT Endpoint-Specific Registers B00h-CFCh */
59654 + dwc_otg_dev_out_ep_regs_t *out_ep_regs[MAX_EPS_CHANNELS];
59655 +#define DWC_DEV_OUT_EP_REG_OFFSET 0xB00
59656 +
59657 + /* Device configuration information */
59658 + uint8_t speed; /**< Device Speed 0: Unknown, 1: LS, 2:FS, 3: HS */
59659 + uint8_t num_in_eps; /**< Number # of Tx EP range: 0-15 exept ep0 */
59660 + uint8_t num_out_eps; /**< Number # of Rx EP range: 0-15 exept ep 0*/
59661 +
59662 + /** Size of periodic FIFOs (Bytes) */
59663 + uint16_t perio_tx_fifo_size[MAX_PERIO_FIFOS];
59664 +
59665 + /** Size of Tx FIFOs (Bytes) */
59666 + uint16_t tx_fifo_size[MAX_TX_FIFOS];
59667 +
59668 + /** Thresholding enable flags and length varaiables **/
59669 + uint16_t rx_thr_en;
59670 + uint16_t iso_tx_thr_en;
59671 + uint16_t non_iso_tx_thr_en;
59672 +
59673 + uint16_t rx_thr_length;
59674 + uint16_t tx_thr_length;
59675 +
59676 + /**
59677 + * Pointers to the DMA Descriptors for EP0 Control
59678 + * transfers (virtual and physical)
59679 + */
59680 +
59681 + /** 2 descriptors for SETUP packets */
59682 + dwc_dma_t dma_setup_desc_addr[2];
59683 + dwc_otg_dev_dma_desc_t *setup_desc_addr[2];
59684 +
59685 + /** Pointer to Descriptor with latest SETUP packet */
59686 + dwc_otg_dev_dma_desc_t *psetup;
59687 +
59688 + /** Index of current SETUP handler descriptor */
59689 + uint32_t setup_desc_index;
59690 +
59691 + /** Descriptor for Data In or Status In phases */
59692 + dwc_dma_t dma_in_desc_addr;
59693 + dwc_otg_dev_dma_desc_t *in_desc_addr;
59694 +
59695 + /** Descriptor for Data Out or Status Out phases */
59696 + dwc_dma_t dma_out_desc_addr;
59697 + dwc_otg_dev_dma_desc_t *out_desc_addr;
59698 +
59699 + /** Setup Packet Detected - if set clear NAK when queueing */
59700 + uint32_t spd;
59701 + /** Isoc ep pointer on which incomplete happens */
59702 + void *isoc_ep;
59703 +
59704 +} dwc_otg_dev_if_t;
59705 +
59706 +/////////////////////////////////////////////////
59707 +// Host Mode Register Structures
59708 +//
59709 +/**
59710 + * The Host Global Registers structure defines the size and relative
59711 + * field offsets for the Host Mode Global Registers. Host Global
59712 + * Registers offsets 400h-7FFh.
59713 +*/
59714 +typedef struct dwc_otg_host_global_regs {
59715 + /** Host Configuration Register. <i>Offset: 400h</i> */
59716 + volatile uint32_t hcfg;
59717 + /** Host Frame Interval Register. <i>Offset: 404h</i> */
59718 + volatile uint32_t hfir;
59719 + /** Host Frame Number / Frame Remaining Register. <i>Offset: 408h</i> */
59720 + volatile uint32_t hfnum;
59721 + /** Reserved. <i>Offset: 40Ch</i> */
59722 + uint32_t reserved40C;
59723 + /** Host Periodic Transmit FIFO/ Queue Status Register. <i>Offset: 410h</i> */
59724 + volatile uint32_t hptxsts;
59725 + /** Host All Channels Interrupt Register. <i>Offset: 414h</i> */
59726 + volatile uint32_t haint;
59727 + /** Host All Channels Interrupt Mask Register. <i>Offset: 418h</i> */
59728 + volatile uint32_t haintmsk;
59729 + /** Host Frame List Base Address Register . <i>Offset: 41Ch</i> */
59730 + volatile uint32_t hflbaddr;
59731 +} dwc_otg_host_global_regs_t;
59732 +
59733 +/**
59734 + * This union represents the bit fields in the Host Configuration Register.
59735 + * Read the register into the <i>d32</i> member then set/clear the bits using
59736 + * the <i>b</i>it elements. Write the <i>d32</i> member to the hcfg register.
59737 + */
59738 +typedef union hcfg_data {
59739 + /** raw register data */
59740 + uint32_t d32;
59741 +
59742 + /** register bits */
59743 + struct {
59744 + /** FS/LS Phy Clock Select */
59745 + unsigned fslspclksel:2;
59746 +#define DWC_HCFG_30_60_MHZ 0
59747 +#define DWC_HCFG_48_MHZ 1
59748 +#define DWC_HCFG_6_MHZ 2
59749 +
59750 + /** FS/LS Only Support */
59751 + unsigned fslssupp:1;
59752 + unsigned reserved3_6:4;
59753 + /** Enable 32-KHz Suspend Mode */
59754 + unsigned ena32khzs:1;
59755 + /** Resume Validation Periiod */
59756 + unsigned resvalid:8;
59757 + unsigned reserved16_22:7;
59758 + /** Enable Scatter/gather DMA in Host mode */
59759 + unsigned descdma:1;
59760 + /** Frame List Entries */
59761 + unsigned frlisten:2;
59762 + /** Enable Periodic Scheduling */
59763 + unsigned perschedena:1;
59764 + unsigned reserved27_30:4;
59765 + unsigned modechtimen:1;
59766 + } b;
59767 +} hcfg_data_t;
59768 +
59769 +/**
59770 + * This union represents the bit fields in the Host Frame Remaing/Number
59771 + * Register.
59772 + */
59773 +typedef union hfir_data {
59774 + /** raw register data */
59775 + uint32_t d32;
59776 +
59777 + /** register bits */
59778 + struct {
59779 + unsigned frint:16;
59780 + unsigned hfirrldctrl:1;
59781 + unsigned reserved:15;
59782 + } b;
59783 +} hfir_data_t;
59784 +
59785 +/**
59786 + * This union represents the bit fields in the Host Frame Remaing/Number
59787 + * Register.
59788 + */
59789 +typedef union hfnum_data {
59790 + /** raw register data */
59791 + uint32_t d32;
59792 +
59793 + /** register bits */
59794 + struct {
59795 + unsigned frnum:16;
59796 +#define DWC_HFNUM_MAX_FRNUM 0x3FFF
59797 + unsigned frrem:16;
59798 + } b;
59799 +} hfnum_data_t;
59800 +
59801 +typedef union hptxsts_data {
59802 + /** raw register data */
59803 + uint32_t d32;
59804 +
59805 + /** register bits */
59806 + struct {
59807 + unsigned ptxfspcavail:16;
59808 + unsigned ptxqspcavail:8;
59809 + /** Top of the Periodic Transmit Request Queue
59810 + * - bit 24 - Terminate (last entry for the selected channel)
59811 + * - bits 26:25 - Token Type
59812 + * - 2'b00 - Zero length
59813 + * - 2'b01 - Ping
59814 + * - 2'b10 - Disable
59815 + * - bits 30:27 - Channel Number
59816 + * - bit 31 - Odd/even microframe
59817 + */
59818 + unsigned ptxqtop_terminate:1;
59819 + unsigned ptxqtop_token:2;
59820 + unsigned ptxqtop_chnum:4;
59821 + unsigned ptxqtop_odd:1;
59822 + } b;
59823 +} hptxsts_data_t;
59824 +
59825 +/**
59826 + * This union represents the bit fields in the Host Port Control and Status
59827 + * Register. Read the register into the <i>d32</i> member then set/clear the
59828 + * bits using the <i>b</i>it elements. Write the <i>d32</i> member to the
59829 + * hprt0 register.
59830 + */
59831 +typedef union hprt0_data {
59832 + /** raw register data */
59833 + uint32_t d32;
59834 + /** register bits */
59835 + struct {
59836 + unsigned prtconnsts:1;
59837 + unsigned prtconndet:1;
59838 + unsigned prtena:1;
59839 + unsigned prtenchng:1;
59840 + unsigned prtovrcurract:1;
59841 + unsigned prtovrcurrchng:1;
59842 + unsigned prtres:1;
59843 + unsigned prtsusp:1;
59844 + unsigned prtrst:1;
59845 + unsigned reserved9:1;
59846 + unsigned prtlnsts:2;
59847 + unsigned prtpwr:1;
59848 + unsigned prttstctl:4;
59849 + unsigned prtspd:2;
59850 +#define DWC_HPRT0_PRTSPD_HIGH_SPEED 0
59851 +#define DWC_HPRT0_PRTSPD_FULL_SPEED 1
59852 +#define DWC_HPRT0_PRTSPD_LOW_SPEED 2
59853 + unsigned reserved19_31:13;
59854 + } b;
59855 +} hprt0_data_t;
59856 +
59857 +/**
59858 + * This union represents the bit fields in the Host All Interrupt
59859 + * Register.
59860 + */
59861 +typedef union haint_data {
59862 + /** raw register data */
59863 + uint32_t d32;
59864 + /** register bits */
59865 + struct {
59866 + unsigned ch0:1;
59867 + unsigned ch1:1;
59868 + unsigned ch2:1;
59869 + unsigned ch3:1;
59870 + unsigned ch4:1;
59871 + unsigned ch5:1;
59872 + unsigned ch6:1;
59873 + unsigned ch7:1;
59874 + unsigned ch8:1;
59875 + unsigned ch9:1;
59876 + unsigned ch10:1;
59877 + unsigned ch11:1;
59878 + unsigned ch12:1;
59879 + unsigned ch13:1;
59880 + unsigned ch14:1;
59881 + unsigned ch15:1;
59882 + unsigned reserved:16;
59883 + } b;
59884 +
59885 + struct {
59886 + unsigned chint:16;
59887 + unsigned reserved:16;
59888 + } b2;
59889 +} haint_data_t;
59890 +
59891 +/**
59892 + * This union represents the bit fields in the Host All Interrupt
59893 + * Register.
59894 + */
59895 +typedef union haintmsk_data {
59896 + /** raw register data */
59897 + uint32_t d32;
59898 + /** register bits */
59899 + struct {
59900 + unsigned ch0:1;
59901 + unsigned ch1:1;
59902 + unsigned ch2:1;
59903 + unsigned ch3:1;
59904 + unsigned ch4:1;
59905 + unsigned ch5:1;
59906 + unsigned ch6:1;
59907 + unsigned ch7:1;
59908 + unsigned ch8:1;
59909 + unsigned ch9:1;
59910 + unsigned ch10:1;
59911 + unsigned ch11:1;
59912 + unsigned ch12:1;
59913 + unsigned ch13:1;
59914 + unsigned ch14:1;
59915 + unsigned ch15:1;
59916 + unsigned reserved:16;
59917 + } b;
59918 +
59919 + struct {
59920 + unsigned chint:16;
59921 + unsigned reserved:16;
59922 + } b2;
59923 +} haintmsk_data_t;
59924 +
59925 +/**
59926 + * Host Channel Specific Registers. <i>500h-5FCh</i>
59927 + */
59928 +typedef struct dwc_otg_hc_regs {
59929 + /** Host Channel 0 Characteristic Register. <i>Offset: 500h + (chan_num * 20h) + 00h</i> */
59930 + volatile uint32_t hcchar;
59931 + /** Host Channel 0 Split Control Register. <i>Offset: 500h + (chan_num * 20h) + 04h</i> */
59932 + volatile uint32_t hcsplt;
59933 + /** Host Channel 0 Interrupt Register. <i>Offset: 500h + (chan_num * 20h) + 08h</i> */
59934 + volatile uint32_t hcint;
59935 + /** Host Channel 0 Interrupt Mask Register. <i>Offset: 500h + (chan_num * 20h) + 0Ch</i> */
59936 + volatile uint32_t hcintmsk;
59937 + /** Host Channel 0 Transfer Size Register. <i>Offset: 500h + (chan_num * 20h) + 10h</i> */
59938 + volatile uint32_t hctsiz;
59939 + /** Host Channel 0 DMA Address Register. <i>Offset: 500h + (chan_num * 20h) + 14h</i> */
59940 + volatile uint32_t hcdma;
59941 + volatile uint32_t reserved;
59942 + /** Host Channel 0 DMA Buffer Address Register. <i>Offset: 500h + (chan_num * 20h) + 1Ch</i> */
59943 + volatile uint32_t hcdmab;
59944 +} dwc_otg_hc_regs_t;
59945 +
59946 +/**
59947 + * This union represents the bit fields in the Host Channel Characteristics
59948 + * Register. Read the register into the <i>d32</i> member then set/clear the
59949 + * bits using the <i>b</i>it elements. Write the <i>d32</i> member to the
59950 + * hcchar register.
59951 + */
59952 +typedef union hcchar_data {
59953 + /** raw register data */
59954 + uint32_t d32;
59955 +
59956 + /** register bits */
59957 + struct {
59958 + /** Maximum packet size in bytes */
59959 + unsigned mps:11;
59960 +
59961 + /** Endpoint number */
59962 + unsigned epnum:4;
59963 +
59964 + /** 0: OUT, 1: IN */
59965 + unsigned epdir:1;
59966 +
59967 + unsigned reserved:1;
59968 +
59969 + /** 0: Full/high speed device, 1: Low speed device */
59970 + unsigned lspddev:1;
59971 +
59972 + /** 0: Control, 1: Isoc, 2: Bulk, 3: Intr */
59973 + unsigned eptype:2;
59974 +
59975 + /** Packets per frame for periodic transfers. 0 is reserved. */
59976 + unsigned multicnt:2;
59977 +
59978 + /** Device address */
59979 + unsigned devaddr:7;
59980 +
59981 + /**
59982 + * Frame to transmit periodic transaction.
59983 + * 0: even, 1: odd
59984 + */
59985 + unsigned oddfrm:1;
59986 +
59987 + /** Channel disable */
59988 + unsigned chdis:1;
59989 +
59990 + /** Channel enable */
59991 + unsigned chen:1;
59992 + } b;
59993 +} hcchar_data_t;
59994 +
59995 +typedef union hcsplt_data {
59996 + /** raw register data */
59997 + uint32_t d32;
59998 +
59999 + /** register bits */
60000 + struct {
60001 + /** Port Address */
60002 + unsigned prtaddr:7;
60003 +
60004 + /** Hub Address */
60005 + unsigned hubaddr:7;
60006 +
60007 + /** Transaction Position */
60008 + unsigned xactpos:2;
60009 +#define DWC_HCSPLIT_XACTPOS_MID 0
60010 +#define DWC_HCSPLIT_XACTPOS_END 1
60011 +#define DWC_HCSPLIT_XACTPOS_BEGIN 2
60012 +#define DWC_HCSPLIT_XACTPOS_ALL 3
60013 +
60014 + /** Do Complete Split */
60015 + unsigned compsplt:1;
60016 +
60017 + /** Reserved */
60018 + unsigned reserved:14;
60019 +
60020 + /** Split Enble */
60021 + unsigned spltena:1;
60022 + } b;
60023 +} hcsplt_data_t;
60024 +
60025 +/**
60026 + * This union represents the bit fields in the Host All Interrupt
60027 + * Register.
60028 + */
60029 +typedef union hcint_data {
60030 + /** raw register data */
60031 + uint32_t d32;
60032 + /** register bits */
60033 + struct {
60034 + /** Transfer Complete */
60035 + unsigned xfercomp:1;
60036 + /** Channel Halted */
60037 + unsigned chhltd:1;
60038 + /** AHB Error */
60039 + unsigned ahberr:1;
60040 + /** STALL Response Received */
60041 + unsigned stall:1;
60042 + /** NAK Response Received */
60043 + unsigned nak:1;
60044 + /** ACK Response Received */
60045 + unsigned ack:1;
60046 + /** NYET Response Received */
60047 + unsigned nyet:1;
60048 + /** Transaction Err */
60049 + unsigned xacterr:1;
60050 + /** Babble Error */
60051 + unsigned bblerr:1;
60052 + /** Frame Overrun */
60053 + unsigned frmovrun:1;
60054 + /** Data Toggle Error */
60055 + unsigned datatglerr:1;
60056 + /** Buffer Not Available (only for DDMA mode) */
60057 + unsigned bna:1;
60058 + /** Exessive transaction error (only for DDMA mode) */
60059 + unsigned xcs_xact:1;
60060 + /** Frame List Rollover interrupt */
60061 + unsigned frm_list_roll:1;
60062 + /** Reserved */
60063 + unsigned reserved14_31:18;
60064 + } b;
60065 +} hcint_data_t;
60066 +
60067 +/**
60068 + * This union represents the bit fields in the Host Channel Interrupt Mask
60069 + * Register. Read the register into the <i>d32</i> member then set/clear the
60070 + * bits using the <i>b</i>it elements. Write the <i>d32</i> member to the
60071 + * hcintmsk register.
60072 + */
60073 +typedef union hcintmsk_data {
60074 + /** raw register data */
60075 + uint32_t d32;
60076 +
60077 + /** register bits */
60078 + struct {
60079 + unsigned xfercompl:1;
60080 + unsigned chhltd:1;
60081 + unsigned ahberr:1;
60082 + unsigned stall:1;
60083 + unsigned nak:1;
60084 + unsigned ack:1;
60085 + unsigned nyet:1;
60086 + unsigned xacterr:1;
60087 + unsigned bblerr:1;
60088 + unsigned frmovrun:1;
60089 + unsigned datatglerr:1;
60090 + unsigned bna:1;
60091 + unsigned xcs_xact:1;
60092 + unsigned frm_list_roll:1;
60093 + unsigned reserved14_31:18;
60094 + } b;
60095 +} hcintmsk_data_t;
60096 +
60097 +/**
60098 + * This union represents the bit fields in the Host Channel Transfer Size
60099 + * Register. Read the register into the <i>d32</i> member then set/clear the
60100 + * bits using the <i>b</i>it elements. Write the <i>d32</i> member to the
60101 + * hcchar register.
60102 + */
60103 +
60104 +typedef union hctsiz_data {
60105 + /** raw register data */
60106 + uint32_t d32;
60107 +
60108 + /** register bits */
60109 + struct {
60110 + /** Total transfer size in bytes */
60111 + unsigned xfersize:19;
60112 +
60113 + /** Data packets to transfer */
60114 + unsigned pktcnt:10;
60115 +
60116 + /**
60117 + * Packet ID for next data packet
60118 + * 0: DATA0
60119 + * 1: DATA2
60120 + * 2: DATA1
60121 + * 3: MDATA (non-Control), SETUP (Control)
60122 + */
60123 + unsigned pid:2;
60124 +#define DWC_HCTSIZ_DATA0 0
60125 +#define DWC_HCTSIZ_DATA1 2
60126 +#define DWC_HCTSIZ_DATA2 1
60127 +#define DWC_HCTSIZ_MDATA 3
60128 +#define DWC_HCTSIZ_SETUP 3
60129 +
60130 + /** Do PING protocol when 1 */
60131 + unsigned dopng:1;
60132 + } b;
60133 +
60134 + /** register bits */
60135 + struct {
60136 + /** Scheduling information */
60137 + unsigned schinfo:8;
60138 +
60139 + /** Number of transfer descriptors.
60140 + * Max value:
60141 + * 64 in general,
60142 + * 256 only for HS isochronous endpoint.
60143 + */
60144 + unsigned ntd:8;
60145 +
60146 + /** Data packets to transfer */
60147 + unsigned reserved16_28:13;
60148 +
60149 + /**
60150 + * Packet ID for next data packet
60151 + * 0: DATA0
60152 + * 1: DATA2
60153 + * 2: DATA1
60154 + * 3: MDATA (non-Control)
60155 + */
60156 + unsigned pid:2;
60157 +
60158 + /** Do PING protocol when 1 */
60159 + unsigned dopng:1;
60160 + } b_ddma;
60161 +} hctsiz_data_t;
60162 +
60163 +/**
60164 + * This union represents the bit fields in the Host DMA Address
60165 + * Register used in Descriptor DMA mode.
60166 + */
60167 +typedef union hcdma_data {
60168 + /** raw register data */
60169 + uint32_t d32;
60170 + /** register bits */
60171 + struct {
60172 + unsigned reserved0_2:3;
60173 + /** Current Transfer Descriptor. Not used for ISOC */
60174 + unsigned ctd:8;
60175 + /** Start Address of Descriptor List */
60176 + unsigned dma_addr:21;
60177 + } b;
60178 +} hcdma_data_t;
60179 +
60180 +/**
60181 + * This union represents the bit fields in the DMA Descriptor
60182 + * status quadlet for host mode. Read the quadlet into the <i>d32</i> member then
60183 + * set/clear the bits using the <i>b</i>it elements.
60184 + */
60185 +typedef union host_dma_desc_sts {
60186 + /** raw register data */
60187 + uint32_t d32;
60188 + /** quadlet bits */
60189 +
60190 + /* for non-isochronous */
60191 + struct {
60192 + /** Number of bytes */
60193 + unsigned n_bytes:17;
60194 + /** QTD offset to jump when Short Packet received - only for IN EPs */
60195 + unsigned qtd_offset:6;
60196 + /**
60197 + * Set to request the core to jump to alternate QTD if
60198 + * Short Packet received - only for IN EPs
60199 + */
60200 + unsigned a_qtd:1;
60201 + /**
60202 + * Setup Packet bit. When set indicates that buffer contains
60203 + * setup packet.
60204 + */
60205 + unsigned sup:1;
60206 + /** Interrupt On Complete */
60207 + unsigned ioc:1;
60208 + /** End of List */
60209 + unsigned eol:1;
60210 + unsigned reserved27:1;
60211 + /** Rx/Tx Status */
60212 + unsigned sts:2;
60213 +#define DMA_DESC_STS_PKTERR 1
60214 + unsigned reserved30:1;
60215 + /** Active Bit */
60216 + unsigned a:1;
60217 + } b;
60218 + /* for isochronous */
60219 + struct {
60220 + /** Number of bytes */
60221 + unsigned n_bytes:12;
60222 + unsigned reserved12_24:13;
60223 + /** Interrupt On Complete */
60224 + unsigned ioc:1;
60225 + unsigned reserved26_27:2;
60226 + /** Rx/Tx Status */
60227 + unsigned sts:2;
60228 + unsigned reserved30:1;
60229 + /** Active Bit */
60230 + unsigned a:1;
60231 + } b_isoc;
60232 +} host_dma_desc_sts_t;
60233 +
60234 +#define MAX_DMA_DESC_SIZE 131071
60235 +#define MAX_DMA_DESC_NUM_GENERIC 64
60236 +#define MAX_DMA_DESC_NUM_HS_ISOC 256
60237 +#define MAX_FRLIST_EN_NUM 64
60238 +/**
60239 + * Host-mode DMA Descriptor structure
60240 + *
60241 + * DMA Descriptor structure contains two quadlets:
60242 + * Status quadlet and Data buffer pointer.
60243 + */
60244 +typedef struct dwc_otg_host_dma_desc {
60245 + /** DMA Descriptor status quadlet */
60246 + host_dma_desc_sts_t status;
60247 + /** DMA Descriptor data buffer pointer */
60248 + uint32_t buf;
60249 +} dwc_otg_host_dma_desc_t;
60250 +
60251 +/** OTG Host Interface Structure.
60252 + *
60253 + * The OTG Host Interface Structure structure contains information
60254 + * needed to manage the DWC_otg controller acting in host mode. It
60255 + * represents the programming view of the host-specific aspects of the
60256 + * controller.
60257 + */
60258 +typedef struct dwc_otg_host_if {
60259 + /** Host Global Registers starting at offset 400h.*/
60260 + dwc_otg_host_global_regs_t *host_global_regs;
60261 +#define DWC_OTG_HOST_GLOBAL_REG_OFFSET 0x400
60262 +
60263 + /** Host Port 0 Control and Status Register */
60264 + volatile uint32_t *hprt0;
60265 +#define DWC_OTG_HOST_PORT_REGS_OFFSET 0x440
60266 +
60267 + /** Host Channel Specific Registers at offsets 500h-5FCh. */
60268 + dwc_otg_hc_regs_t *hc_regs[MAX_EPS_CHANNELS];
60269 +#define DWC_OTG_HOST_CHAN_REGS_OFFSET 0x500
60270 +#define DWC_OTG_CHAN_REGS_OFFSET 0x20
60271 +
60272 + /* Host configuration information */
60273 + /** Number of Host Channels (range: 1-16) */
60274 + uint8_t num_host_channels;
60275 + /** Periodic EPs supported (0: no, 1: yes) */
60276 + uint8_t perio_eps_supported;
60277 + /** Periodic Tx FIFO Size (Only 1 host periodic Tx FIFO) */
60278 + uint16_t perio_tx_fifo_size;
60279 +
60280 +} dwc_otg_host_if_t;
60281 +
60282 +/**
60283 + * This union represents the bit fields in the Power and Clock Gating Control
60284 + * Register. Read the register into the <i>d32</i> member then set/clear the
60285 + * bits using the <i>b</i>it elements.
60286 + */
60287 +typedef union pcgcctl_data {
60288 + /** raw register data */
60289 + uint32_t d32;
60290 +
60291 + /** register bits */
60292 + struct {
60293 + /** Stop Pclk */
60294 + unsigned stoppclk:1;
60295 + /** Gate Hclk */
60296 + unsigned gatehclk:1;
60297 + /** Power Clamp */
60298 + unsigned pwrclmp:1;
60299 + /** Reset Power Down Modules */
60300 + unsigned rstpdwnmodule:1;
60301 + /** Reserved */
60302 + unsigned reserved:1;
60303 + /** Enable Sleep Clock Gating (Enbl_L1Gating) */
60304 + unsigned enbl_sleep_gating:1;
60305 + /** PHY In Sleep (PhySleep) */
60306 + unsigned phy_in_sleep:1;
60307 + /** Deep Sleep*/
60308 + unsigned deep_sleep:1;
60309 + unsigned resetaftsusp:1;
60310 + unsigned restoremode:1;
60311 + unsigned enbl_extnd_hiber:1;
60312 + unsigned extnd_hiber_pwrclmp:1;
60313 + unsigned extnd_hiber_switch:1;
60314 + unsigned ess_reg_restored:1;
60315 + unsigned prt_clk_sel:2;
60316 + unsigned port_power:1;
60317 + unsigned max_xcvrselect:2;
60318 + unsigned max_termsel:1;
60319 + unsigned mac_dev_addr:7;
60320 + unsigned p2hd_dev_enum_spd:2;
60321 + unsigned p2hd_prt_spd:2;
60322 + unsigned if_dev_mode:1;
60323 + } b;
60324 +} pcgcctl_data_t;
60325 +
60326 +/**
60327 + * This union represents the bit fields in the Global Data FIFO Software
60328 + * Configuration Register. Read the register into the <i>d32</i> member then
60329 + * set/clear the bits using the <i>b</i>it elements.
60330 + */
60331 +typedef union gdfifocfg_data {
60332 + /* raw register data */
60333 + uint32_t d32;
60334 + /** register bits */
60335 + struct {
60336 + /** OTG Data FIFO depth */
60337 + unsigned gdfifocfg:16;
60338 + /** Start address of EP info controller */
60339 + unsigned epinfobase:16;
60340 + } b;
60341 +} gdfifocfg_data_t;
60342 +
60343 +/**
60344 + * This union represents the bit fields in the Global Power Down Register
60345 + * Register. Read the register into the <i>d32</i> member then set/clear the
60346 + * bits using the <i>b</i>it elements.
60347 + */
60348 +typedef union gpwrdn_data {
60349 + /* raw register data */
60350 + uint32_t d32;
60351 +
60352 + /** register bits */
60353 + struct {
60354 + /** PMU Interrupt Select */
60355 + unsigned pmuintsel:1;
60356 + /** PMU Active */
60357 + unsigned pmuactv:1;
60358 + /** Restore */
60359 + unsigned restore:1;
60360 + /** Power Down Clamp */
60361 + unsigned pwrdnclmp:1;
60362 + /** Power Down Reset */
60363 + unsigned pwrdnrstn:1;
60364 + /** Power Down Switch */
60365 + unsigned pwrdnswtch:1;
60366 + /** Disable VBUS */
60367 + unsigned dis_vbus:1;
60368 + /** Line State Change */
60369 + unsigned lnstschng:1;
60370 + /** Line state change mask */
60371 + unsigned lnstchng_msk:1;
60372 + /** Reset Detected */
60373 + unsigned rst_det:1;
60374 + /** Reset Detect mask */
60375 + unsigned rst_det_msk:1;
60376 + /** Disconnect Detected */
60377 + unsigned disconn_det:1;
60378 + /** Disconnect Detect mask */
60379 + unsigned disconn_det_msk:1;
60380 + /** Connect Detected*/
60381 + unsigned connect_det:1;
60382 + /** Connect Detected Mask*/
60383 + unsigned connect_det_msk:1;
60384 + /** SRP Detected */
60385 + unsigned srp_det:1;
60386 + /** SRP Detect mask */
60387 + unsigned srp_det_msk:1;
60388 + /** Status Change Interrupt */
60389 + unsigned sts_chngint:1;
60390 + /** Status Change Interrupt Mask */
60391 + unsigned sts_chngint_msk:1;
60392 + /** Line State */
60393 + unsigned linestate:2;
60394 + /** Indicates current mode(status of IDDIG signal) */
60395 + unsigned idsts:1;
60396 + /** B Session Valid signal status*/
60397 + unsigned bsessvld:1;
60398 + /** ADP Event Detected */
60399 + unsigned adp_int:1;
60400 + /** Multi Valued ID pin */
60401 + unsigned mult_val_id_bc:5;
60402 + /** Reserved 24_31 */
60403 + unsigned reserved29_31:3;
60404 + } b;
60405 +} gpwrdn_data_t;
60406 +
60407 +#endif
60408 --- /dev/null
60409 +++ b/drivers/usb/host/dwc_otg/test/Makefile
60410 @@ -0,0 +1,16 @@
60411 +
60412 +PERL=/usr/bin/perl
60413 +PL_TESTS=test_sysfs.pl test_mod_param.pl
60414 +
60415 +.PHONY : test
60416 +test : perl_tests
60417 +
60418 +perl_tests :
60419 + @echo
60420 + @echo Running perl tests
60421 + @for test in $(PL_TESTS); do \
60422 + if $(PERL) ./$$test ; then \
60423 + echo "=======> $$test, PASSED" ; \
60424 + else echo "=======> $$test, FAILED" ; \
60425 + fi \
60426 + done
60427 --- /dev/null
60428 +++ b/drivers/usb/host/dwc_otg/test/dwc_otg_test.pm
60429 @@ -0,0 +1,337 @@
60430 +package dwc_otg_test;
60431 +
60432 +use strict;
60433 +use Exporter ();
60434 +
60435 +use vars qw(@ISA @EXPORT
60436 +$sysfsdir $paramdir $errors $params
60437 +);
60438 +
60439 +@ISA = qw(Exporter);
60440 +
60441 +#
60442 +# Globals
60443 +#
60444 +$sysfsdir = "/sys/devices/lm0";
60445 +$paramdir = "/sys/module/dwc_otg";
60446 +$errors = 0;
60447 +
60448 +$params = [
60449 + {
60450 + NAME => "otg_cap",
60451 + DEFAULT => 0,
60452 + ENUM => [],
60453 + LOW => 0,
60454 + HIGH => 2
60455 + },
60456 + {
60457 + NAME => "dma_enable",
60458 + DEFAULT => 0,
60459 + ENUM => [],
60460 + LOW => 0,
60461 + HIGH => 1
60462 + },
60463 + {
60464 + NAME => "dma_burst_size",
60465 + DEFAULT => 32,
60466 + ENUM => [1, 4, 8, 16, 32, 64, 128, 256],
60467 + LOW => 1,
60468 + HIGH => 256
60469 + },
60470 + {
60471 + NAME => "host_speed",
60472 + DEFAULT => 0,
60473 + ENUM => [],
60474 + LOW => 0,
60475 + HIGH => 1
60476 + },
60477 + {
60478 + NAME => "host_support_fs_ls_low_power",
60479 + DEFAULT => 0,
60480 + ENUM => [],
60481 + LOW => 0,
60482 + HIGH => 1
60483 + },
60484 + {
60485 + NAME => "host_ls_low_power_phy_clk",
60486 + DEFAULT => 0,
60487 + ENUM => [],
60488 + LOW => 0,
60489 + HIGH => 1
60490 + },
60491 + {
60492 + NAME => "dev_speed",
60493 + DEFAULT => 0,
60494 + ENUM => [],
60495 + LOW => 0,
60496 + HIGH => 1
60497 + },
60498 + {
60499 + NAME => "enable_dynamic_fifo",
60500 + DEFAULT => 1,
60501 + ENUM => [],
60502 + LOW => 0,
60503 + HIGH => 1
60504 + },
60505 + {
60506 + NAME => "data_fifo_size",
60507 + DEFAULT => 8192,
60508 + ENUM => [],
60509 + LOW => 32,
60510 + HIGH => 32768
60511 + },
60512 + {
60513 + NAME => "dev_rx_fifo_size",
60514 + DEFAULT => 1064,
60515 + ENUM => [],
60516 + LOW => 16,
60517 + HIGH => 32768
60518 + },
60519 + {
60520 + NAME => "dev_nperio_tx_fifo_size",
60521 + DEFAULT => 1024,
60522 + ENUM => [],
60523 + LOW => 16,
60524 + HIGH => 32768
60525 + },
60526 + {
60527 + NAME => "dev_perio_tx_fifo_size_1",
60528 + DEFAULT => 256,
60529 + ENUM => [],
60530 + LOW => 4,
60531 + HIGH => 768
60532 + },
60533 + {
60534 + NAME => "dev_perio_tx_fifo_size_2",
60535 + DEFAULT => 256,
60536 + ENUM => [],
60537 + LOW => 4,
60538 + HIGH => 768
60539 + },
60540 + {
60541 + NAME => "dev_perio_tx_fifo_size_3",
60542 + DEFAULT => 256,
60543 + ENUM => [],
60544 + LOW => 4,
60545 + HIGH => 768
60546 + },
60547 + {
60548 + NAME => "dev_perio_tx_fifo_size_4",
60549 + DEFAULT => 256,
60550 + ENUM => [],
60551 + LOW => 4,
60552 + HIGH => 768
60553 + },
60554 + {
60555 + NAME => "dev_perio_tx_fifo_size_5",
60556 + DEFAULT => 256,
60557 + ENUM => [],
60558 + LOW => 4,
60559 + HIGH => 768
60560 + },
60561 + {
60562 + NAME => "dev_perio_tx_fifo_size_6",
60563 + DEFAULT => 256,
60564 + ENUM => [],
60565 + LOW => 4,
60566 + HIGH => 768
60567 + },
60568 + {
60569 + NAME => "dev_perio_tx_fifo_size_7",
60570 + DEFAULT => 256,
60571 + ENUM => [],
60572 + LOW => 4,
60573 + HIGH => 768
60574 + },
60575 + {
60576 + NAME => "dev_perio_tx_fifo_size_8",
60577 + DEFAULT => 256,
60578 + ENUM => [],
60579 + LOW => 4,
60580 + HIGH => 768
60581 + },
60582 + {
60583 + NAME => "dev_perio_tx_fifo_size_9",
60584 + DEFAULT => 256,
60585 + ENUM => [],
60586 + LOW => 4,
60587 + HIGH => 768
60588 + },
60589 + {
60590 + NAME => "dev_perio_tx_fifo_size_10",
60591 + DEFAULT => 256,
60592 + ENUM => [],
60593 + LOW => 4,
60594 + HIGH => 768
60595 + },
60596 + {
60597 + NAME => "dev_perio_tx_fifo_size_11",
60598 + DEFAULT => 256,
60599 + ENUM => [],
60600 + LOW => 4,
60601 + HIGH => 768
60602 + },
60603 + {
60604 + NAME => "dev_perio_tx_fifo_size_12",
60605 + DEFAULT => 256,
60606 + ENUM => [],
60607 + LOW => 4,
60608 + HIGH => 768
60609 + },
60610 + {
60611 + NAME => "dev_perio_tx_fifo_size_13",
60612 + DEFAULT => 256,
60613 + ENUM => [],
60614 + LOW => 4,
60615 + HIGH => 768
60616 + },
60617 + {
60618 + NAME => "dev_perio_tx_fifo_size_14",
60619 + DEFAULT => 256,
60620 + ENUM => [],
60621 + LOW => 4,
60622 + HIGH => 768
60623 + },
60624 + {
60625 + NAME => "dev_perio_tx_fifo_size_15",
60626 + DEFAULT => 256,
60627 + ENUM => [],
60628 + LOW => 4,
60629 + HIGH => 768
60630 + },
60631 + {
60632 + NAME => "host_rx_fifo_size",
60633 + DEFAULT => 1024,
60634 + ENUM => [],
60635 + LOW => 16,
60636 + HIGH => 32768
60637 + },
60638 + {
60639 + NAME => "host_nperio_tx_fifo_size",
60640 + DEFAULT => 1024,
60641 + ENUM => [],
60642 + LOW => 16,
60643 + HIGH => 32768
60644 + },
60645 + {
60646 + NAME => "host_perio_tx_fifo_size",
60647 + DEFAULT => 1024,
60648 + ENUM => [],
60649 + LOW => 16,
60650 + HIGH => 32768
60651 + },
60652 + {
60653 + NAME => "max_transfer_size",
60654 + DEFAULT => 65535,
60655 + ENUM => [],
60656 + LOW => 2047,
60657 + HIGH => 65535
60658 + },
60659 + {
60660 + NAME => "max_packet_count",
60661 + DEFAULT => 511,
60662 + ENUM => [],
60663 + LOW => 15,
60664 + HIGH => 511
60665 + },
60666 + {
60667 + NAME => "host_channels",
60668 + DEFAULT => 12,
60669 + ENUM => [],
60670 + LOW => 1,
60671 + HIGH => 16
60672 + },
60673 + {
60674 + NAME => "dev_endpoints",
60675 + DEFAULT => 6,
60676 + ENUM => [],
60677 + LOW => 1,
60678 + HIGH => 15
60679 + },
60680 + {
60681 + NAME => "phy_type",
60682 + DEFAULT => 1,
60683 + ENUM => [],
60684 + LOW => 0,
60685 + HIGH => 2
60686 + },
60687 + {
60688 + NAME => "phy_utmi_width",
60689 + DEFAULT => 16,
60690 + ENUM => [8, 16],
60691 + LOW => 8,
60692 + HIGH => 16
60693 + },
60694 + {
60695 + NAME => "phy_ulpi_ddr",
60696 + DEFAULT => 0,
60697 + ENUM => [],
60698 + LOW => 0,
60699 + HIGH => 1
60700 + },
60701 + ];
60702 +
60703 +
60704 +#
60705 +#
60706 +sub check_arch {
60707 + $_ = `uname -m`;
60708 + chomp;
60709 + unless (m/armv4tl/) {
60710 + warn "# \n# Can't execute on $_. Run on integrator platform.\n# \n";
60711 + return 0;
60712 + }
60713 + return 1;
60714 +}
60715 +
60716 +#
60717 +#
60718 +sub load_module {
60719 + my $params = shift;
60720 + print "\nRemoving Module\n";
60721 + system "rmmod dwc_otg";
60722 + print "Loading Module\n";
60723 + if ($params ne "") {
60724 + print "Module Parameters: $params\n";
60725 + }
60726 + if (system("modprobe dwc_otg $params")) {
60727 + warn "Unable to load module\n";
60728 + return 0;
60729 + }
60730 + return 1;
60731 +}
60732 +
60733 +#
60734 +#
60735 +sub test_status {
60736 + my $arg = shift;
60737 +
60738 + print "\n";
60739 +
60740 + if (defined $arg) {
60741 + warn "WARNING: $arg\n";
60742 + }
60743 +
60744 + if ($errors > 0) {
60745 + warn "TEST FAILED with $errors errors\n";
60746 + return 0;
60747 + } else {
60748 + print "TEST PASSED\n";
60749 + return 0 if (defined $arg);
60750 + }
60751 + return 1;
60752 +}
60753 +
60754 +#
60755 +#
60756 +@EXPORT = qw(
60757 +$sysfsdir
60758 +$paramdir
60759 +$params
60760 +$errors
60761 +check_arch
60762 +load_module
60763 +test_status
60764 +);
60765 +
60766 +1;
60767 --- /dev/null
60768 +++ b/drivers/usb/host/dwc_otg/test/test_mod_param.pl
60769 @@ -0,0 +1,133 @@
60770 +#!/usr/bin/perl -w
60771 +#
60772 +# Run this program on the integrator.
60773 +#
60774 +# - Tests module parameter default values.
60775 +# - Tests setting of valid module parameter values via modprobe.
60776 +# - Tests invalid module parameter values.
60777 +# -----------------------------------------------------------------------------
60778 +use strict;
60779 +use dwc_otg_test;
60780 +
60781 +check_arch() or die;
60782 +
60783 +#
60784 +#
60785 +sub test {
60786 + my ($param,$expected) = @_;
60787 + my $value = get($param);
60788 +
60789 + if ($value == $expected) {
60790 + print "$param = $value, okay\n";
60791 + }
60792 +
60793 + else {
60794 + warn "ERROR: value of $param != $expected, $value\n";
60795 + $errors ++;
60796 + }
60797 +}
60798 +
60799 +#
60800 +#
60801 +sub get {
60802 + my $param = shift;
60803 + my $tmp = `cat $paramdir/$param`;
60804 + chomp $tmp;
60805 + return $tmp;
60806 +}
60807 +
60808 +#
60809 +#
60810 +sub test_main {
60811 +
60812 + print "\nTesting Module Parameters\n";
60813 +
60814 + load_module("") or die;
60815 +
60816 + # Test initial values
60817 + print "\nTesting Default Values\n";
60818 + foreach (@{$params}) {
60819 + test ($_->{NAME}, $_->{DEFAULT});
60820 + }
60821 +
60822 + # Test low value
60823 + print "\nTesting Low Value\n";
60824 + my $cmd_params = "";
60825 + foreach (@{$params}) {
60826 + $cmd_params = $cmd_params . "$_->{NAME}=$_->{LOW} ";
60827 + }
60828 + load_module($cmd_params) or die;
60829 +
60830 + foreach (@{$params}) {
60831 + test ($_->{NAME}, $_->{LOW});
60832 + }
60833 +
60834 + # Test high value
60835 + print "\nTesting High Value\n";
60836 + $cmd_params = "";
60837 + foreach (@{$params}) {
60838 + $cmd_params = $cmd_params . "$_->{NAME}=$_->{HIGH} ";
60839 + }
60840 + load_module($cmd_params) or die;
60841 +
60842 + foreach (@{$params}) {
60843 + test ($_->{NAME}, $_->{HIGH});
60844 + }
60845 +
60846 + # Test Enum
60847 + print "\nTesting Enumerated\n";
60848 + foreach (@{$params}) {
60849 + if (defined $_->{ENUM}) {
60850 + my $value;
60851 + foreach $value (@{$_->{ENUM}}) {
60852 + $cmd_params = "$_->{NAME}=$value";
60853 + load_module($cmd_params) or die;
60854 + test ($_->{NAME}, $value);
60855 + }
60856 + }
60857 + }
60858 +
60859 + # Test Invalid Values
60860 + print "\nTesting Invalid Values\n";
60861 + $cmd_params = "";
60862 + foreach (@{$params}) {
60863 + $cmd_params = $cmd_params . sprintf "$_->{NAME}=%d ", $_->{LOW}-1;
60864 + }
60865 + load_module($cmd_params) or die;
60866 +
60867 + foreach (@{$params}) {
60868 + test ($_->{NAME}, $_->{DEFAULT});
60869 + }
60870 +
60871 + $cmd_params = "";
60872 + foreach (@{$params}) {
60873 + $cmd_params = $cmd_params . sprintf "$_->{NAME}=%d ", $_->{HIGH}+1;
60874 + }
60875 + load_module($cmd_params) or die;
60876 +
60877 + foreach (@{$params}) {
60878 + test ($_->{NAME}, $_->{DEFAULT});
60879 + }
60880 +
60881 + print "\nTesting Enumerated\n";
60882 + foreach (@{$params}) {
60883 + if (defined $_->{ENUM}) {
60884 + my $value;
60885 + foreach $value (@{$_->{ENUM}}) {
60886 + $value = $value + 1;
60887 + $cmd_params = "$_->{NAME}=$value";
60888 + load_module($cmd_params) or die;
60889 + test ($_->{NAME}, $_->{DEFAULT});
60890 + $value = $value - 2;
60891 + $cmd_params = "$_->{NAME}=$value";
60892 + load_module($cmd_params) or die;
60893 + test ($_->{NAME}, $_->{DEFAULT});
60894 + }
60895 + }
60896 + }
60897 +
60898 + test_status() or die;
60899 +}
60900 +
60901 +test_main();
60902 +0;
60903 --- /dev/null
60904 +++ b/drivers/usb/host/dwc_otg/test/test_sysfs.pl
60905 @@ -0,0 +1,193 @@
60906 +#!/usr/bin/perl -w
60907 +#
60908 +# Run this program on the integrator
60909 +# - Tests select sysfs attributes.
60910 +# - Todo ... test more attributes, hnp/srp, buspower/bussuspend, etc.
60911 +# -----------------------------------------------------------------------------
60912 +use strict;
60913 +use dwc_otg_test;
60914 +
60915 +check_arch() or die;
60916 +
60917 +#
60918 +#
60919 +sub test {
60920 + my ($attr,$expected) = @_;
60921 + my $string = get($attr);
60922 +
60923 + if ($string eq $expected) {
60924 + printf("$attr = $string, okay\n");
60925 + }
60926 + else {
60927 + warn "ERROR: value of $attr != $expected, $string\n";
60928 + $errors ++;
60929 + }
60930 +}
60931 +
60932 +#
60933 +#
60934 +sub set {
60935 + my ($reg, $value) = @_;
60936 + system "echo $value > $sysfsdir/$reg";
60937 +}
60938 +
60939 +#
60940 +#
60941 +sub get {
60942 + my $attr = shift;
60943 + my $string = `cat $sysfsdir/$attr`;
60944 + chomp $string;
60945 + if ($string =~ m/\s\=\s/) {
60946 + my $tmp;
60947 + ($tmp, $string) = split /\s=\s/, $string;
60948 + }
60949 + return $string;
60950 +}
60951 +
60952 +#
60953 +#
60954 +sub test_main {
60955 + print("\nTesting Sysfs Attributes\n");
60956 +
60957 + load_module("") or die;
60958 +
60959 + # Test initial values of regoffset/regvalue/guid/gsnpsid
60960 + print("\nTesting Default Values\n");
60961 +
60962 + test("regoffset", "0xffffffff");
60963 + test("regvalue", "invalid offset");
60964 + test("guid", "0x12345678"); # this will fail if it has been changed
60965 + test("gsnpsid", "0x4f54200a");
60966 +
60967 + # Test operation of regoffset/regvalue
60968 + print("\nTesting regoffset\n");
60969 + set('regoffset', '5a5a5a5a');
60970 + test("regoffset", "0xffffffff");
60971 +
60972 + set('regoffset', '0');
60973 + test("regoffset", "0x00000000");
60974 +
60975 + set('regoffset', '40000');
60976 + test("regoffset", "0x00000000");
60977 +
60978 + set('regoffset', '3ffff');
60979 + test("regoffset", "0x0003ffff");
60980 +
60981 + set('regoffset', '1');
60982 + test("regoffset", "0x00000001");
60983 +
60984 + print("\nTesting regvalue\n");
60985 + set('regoffset', '3c');
60986 + test("regvalue", "0x12345678");
60987 + set('regvalue', '5a5a5a5a');
60988 + test("regvalue", "0x5a5a5a5a");
60989 + set('regvalue','a5a5a5a5');
60990 + test("regvalue", "0xa5a5a5a5");
60991 + set('guid','12345678');
60992 +
60993 + # Test HNP Capable
60994 + print("\nTesting HNP Capable bit\n");
60995 + set('hnpcapable', '1');
60996 + test("hnpcapable", "0x1");
60997 + set('hnpcapable','0');
60998 + test("hnpcapable", "0x0");
60999 +
61000 + set('regoffset','0c');
61001 +
61002 + my $old = get('gusbcfg');
61003 + print("setting hnpcapable\n");
61004 + set('hnpcapable', '1');
61005 + test("hnpcapable", "0x1");
61006 + test('gusbcfg', sprintf "0x%08x", (oct ($old) | (1<<9)));
61007 + test('regvalue', sprintf "0x%08x", (oct ($old) | (1<<9)));
61008 +
61009 + $old = get('gusbcfg');
61010 + print("clearing hnpcapable\n");
61011 + set('hnpcapable', '0');
61012 + test("hnpcapable", "0x0");
61013 + test ('gusbcfg', sprintf "0x%08x", oct ($old) & (~(1<<9)));
61014 + test ('regvalue', sprintf "0x%08x", oct ($old) & (~(1<<9)));
61015 +
61016 + # Test SRP Capable
61017 + print("\nTesting SRP Capable bit\n");
61018 + set('srpcapable', '1');
61019 + test("srpcapable", "0x1");
61020 + set('srpcapable','0');
61021 + test("srpcapable", "0x0");
61022 +
61023 + set('regoffset','0c');
61024 +
61025 + $old = get('gusbcfg');
61026 + print("setting srpcapable\n");
61027 + set('srpcapable', '1');
61028 + test("srpcapable", "0x1");
61029 + test('gusbcfg', sprintf "0x%08x", (oct ($old) | (1<<8)));
61030 + test('regvalue', sprintf "0x%08x", (oct ($old) | (1<<8)));
61031 +
61032 + $old = get('gusbcfg');
61033 + print("clearing srpcapable\n");
61034 + set('srpcapable', '0');
61035 + test("srpcapable", "0x0");
61036 + test('gusbcfg', sprintf "0x%08x", oct ($old) & (~(1<<8)));
61037 + test('regvalue', sprintf "0x%08x", oct ($old) & (~(1<<8)));
61038 +
61039 + # Test GGPIO
61040 + print("\nTesting GGPIO\n");
61041 + set('ggpio','5a5a5a5a');
61042 + test('ggpio','0x5a5a0000');
61043 + set('ggpio','a5a5a5a5');
61044 + test('ggpio','0xa5a50000');
61045 + set('ggpio','11110000');
61046 + test('ggpio','0x11110000');
61047 + set('ggpio','00001111');
61048 + test('ggpio','0x00000000');
61049 +
61050 + # Test DEVSPEED
61051 + print("\nTesting DEVSPEED\n");
61052 + set('regoffset','800');
61053 + $old = get('regvalue');
61054 + set('devspeed','0');
61055 + test('devspeed','0x0');
61056 + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3)));
61057 + set('devspeed','1');
61058 + test('devspeed','0x1');
61059 + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3) | 1));
61060 + set('devspeed','2');
61061 + test('devspeed','0x2');
61062 + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3) | 2));
61063 + set('devspeed','3');
61064 + test('devspeed','0x3');
61065 + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3) | 3));
61066 + set('devspeed','4');
61067 + test('devspeed','0x0');
61068 + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3)));
61069 + set('devspeed','5');
61070 + test('devspeed','0x1');
61071 + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3) | 1));
61072 +
61073 +
61074 + # mode Returns the current mode:0 for device mode1 for host mode Read
61075 + # hnp Initiate the Host Negotiation Protocol. Read returns the status. Read/Write
61076 + # srp Initiate the Session Request Protocol. Read returns the status. Read/Write
61077 + # buspower Get or Set the Power State of the bus (0 - Off or 1 - On) Read/Write
61078 + # bussuspend Suspend the USB bus. Read/Write
61079 + # busconnected Get the connection status of the bus Read
61080 +
61081 + # gotgctl Get or set the Core Control Status Register. Read/Write
61082 + ## gusbcfg Get or set the Core USB Configuration Register Read/Write
61083 + # grxfsiz Get or set the Receive FIFO Size Register Read/Write
61084 + # gnptxfsiz Get or set the non-periodic Transmit Size Register Read/Write
61085 + # gpvndctl Get or set the PHY Vendor Control Register Read/Write
61086 + ## ggpio Get the value in the lower 16-bits of the General Purpose IO Register or Set the upper 16 bits. Read/Write
61087 + ## guid Get or set the value of the User ID Register Read/Write
61088 + ## gsnpsid Get the value of the Synopsys ID Regester Read
61089 + ## devspeed Get or set the device speed setting in the DCFG register Read/Write
61090 + # enumspeed Gets the device enumeration Speed. Read
61091 + # hptxfsiz Get the value of the Host Periodic Transmit FIFO Read
61092 + # hprt0 Get or Set the value in the Host Port Control and Status Register Read/Write
61093 +
61094 + test_status("TEST NYI") or die;
61095 +}
61096 +
61097 +test_main();
61098 +0;