2 * xhci-dbgcap.c - xHCI debug capability support
4 * Copyright (C) 2017 Intel Corporation
6 * Author: Lu Baolu <baolu.lu@linux.intel.com>
8 #include <linux/dma-mapping.h>
9 #include <linux/slab.h>
10 #include <linux/nls.h>
13 #include "xhci-trace.h"
14 #include "xhci-dbgcap.h"
17 dbc_dma_alloc_coherent(struct xhci_hcd
*xhci
, size_t size
,
18 dma_addr_t
*dma_handle
, gfp_t flags
)
22 vaddr
= dma_alloc_coherent(xhci_to_hcd(xhci
)->self
.sysdev
,
23 size
, dma_handle
, flags
);
24 memset(vaddr
, 0, size
);
29 dbc_dma_free_coherent(struct xhci_hcd
*xhci
, size_t size
,
30 void *cpu_addr
, dma_addr_t dma_handle
)
33 dma_free_coherent(xhci_to_hcd(xhci
)->self
.sysdev
,
34 size
, cpu_addr
, dma_handle
);
37 static u32
xhci_dbc_populate_strings(struct dbc_str_descs
*strings
)
39 struct usb_string_descriptor
*s_desc
;
43 s_desc
= (struct usb_string_descriptor
*)strings
->serial
;
44 utf8s_to_utf16s(DBC_STRING_SERIAL
, strlen(DBC_STRING_SERIAL
),
45 UTF16_LITTLE_ENDIAN
, (wchar_t *)s_desc
->wData
,
46 DBC_MAX_STRING_LENGTH
);
48 s_desc
->bLength
= (strlen(DBC_STRING_SERIAL
) + 1) * 2;
49 s_desc
->bDescriptorType
= USB_DT_STRING
;
50 string_length
= s_desc
->bLength
;
54 s_desc
= (struct usb_string_descriptor
*)strings
->product
;
55 utf8s_to_utf16s(DBC_STRING_PRODUCT
, strlen(DBC_STRING_PRODUCT
),
56 UTF16_LITTLE_ENDIAN
, (wchar_t *)s_desc
->wData
,
57 DBC_MAX_STRING_LENGTH
);
59 s_desc
->bLength
= (strlen(DBC_STRING_PRODUCT
) + 1) * 2;
60 s_desc
->bDescriptorType
= USB_DT_STRING
;
61 string_length
+= s_desc
->bLength
;
64 /* Manufacture string: */
65 s_desc
= (struct usb_string_descriptor
*)strings
->manufacturer
;
66 utf8s_to_utf16s(DBC_STRING_MANUFACTURER
,
67 strlen(DBC_STRING_MANUFACTURER
),
68 UTF16_LITTLE_ENDIAN
, (wchar_t *)s_desc
->wData
,
69 DBC_MAX_STRING_LENGTH
);
71 s_desc
->bLength
= (strlen(DBC_STRING_MANUFACTURER
) + 1) * 2;
72 s_desc
->bDescriptorType
= USB_DT_STRING
;
73 string_length
+= s_desc
->bLength
;
77 strings
->string0
[0] = 4;
78 strings
->string0
[1] = USB_DT_STRING
;
79 strings
->string0
[2] = 0x09;
80 strings
->string0
[3] = 0x04;
86 static void xhci_dbc_init_contexts(struct xhci_hcd
*xhci
, u32 string_length
)
89 struct dbc_info_context
*info
;
90 struct xhci_ep_ctx
*ep_ctx
;
93 unsigned int max_burst
;
99 /* Populate info Context: */
100 info
= (struct dbc_info_context
*)dbc
->ctx
->bytes
;
101 dma
= dbc
->string_dma
;
102 info
->string0
= cpu_to_le64(dma
);
103 info
->manufacturer
= cpu_to_le64(dma
+ DBC_MAX_STRING_LENGTH
);
104 info
->product
= cpu_to_le64(dma
+ DBC_MAX_STRING_LENGTH
* 2);
105 info
->serial
= cpu_to_le64(dma
+ DBC_MAX_STRING_LENGTH
* 3);
106 info
->length
= cpu_to_le32(string_length
);
108 /* Populate bulk out endpoint context: */
109 ep_ctx
= dbc_bulkout_ctx(dbc
);
110 max_burst
= DBC_CTRL_MAXBURST(readl(&dbc
->regs
->control
));
111 deq
= dbc_bulkout_enq(dbc
);
113 ep_ctx
->ep_info2
= dbc_epctx_info2(BULK_OUT_EP
, 1024, max_burst
);
114 ep_ctx
->deq
= cpu_to_le64(deq
| dbc
->ring_out
->cycle_state
);
116 /* Populate bulk in endpoint context: */
117 ep_ctx
= dbc_bulkin_ctx(dbc
);
118 deq
= dbc_bulkin_enq(dbc
);
120 ep_ctx
->ep_info2
= dbc_epctx_info2(BULK_IN_EP
, 1024, max_burst
);
121 ep_ctx
->deq
= cpu_to_le64(deq
| dbc
->ring_in
->cycle_state
);
123 /* Set DbC context and info registers: */
124 xhci_write_64(xhci
, dbc
->ctx
->dma
, &dbc
->regs
->dccp
);
126 dev_info
= cpu_to_le32((DBC_VENDOR_ID
<< 16) | DBC_PROTOCOL
);
127 writel(dev_info
, &dbc
->regs
->devinfo1
);
129 dev_info
= cpu_to_le32((DBC_DEVICE_REV
<< 16) | DBC_PRODUCT_ID
);
130 writel(dev_info
, &dbc
->regs
->devinfo2
);
133 static void xhci_dbc_giveback(struct dbc_request
*req
, int status
)
134 __releases(&dbc
->lock
)
135 __acquires(&dbc
->lock
)
137 struct dbc_ep
*dep
= req
->dep
;
138 struct xhci_dbc
*dbc
= dep
->dbc
;
139 struct xhci_hcd
*xhci
= dbc
->xhci
;
140 struct device
*dev
= xhci_to_hcd(dbc
->xhci
)->self
.sysdev
;
142 list_del_init(&req
->list_pending
);
146 if (req
->status
== -EINPROGRESS
)
147 req
->status
= status
;
149 trace_xhci_dbc_giveback_request(req
);
151 dma_unmap_single(dev
,
154 dbc_ep_dma_direction(dep
));
156 /* Give back the transfer request: */
157 spin_unlock(&dbc
->lock
);
158 req
->complete(xhci
, req
);
159 spin_lock(&dbc
->lock
);
162 static void xhci_dbc_flush_single_request(struct dbc_request
*req
)
164 union xhci_trb
*trb
= req
->trb
;
166 trb
->generic
.field
[0] = 0;
167 trb
->generic
.field
[1] = 0;
168 trb
->generic
.field
[2] = 0;
169 trb
->generic
.field
[3] &= cpu_to_le32(TRB_CYCLE
);
170 trb
->generic
.field
[3] |= cpu_to_le32(TRB_TYPE(TRB_TR_NOOP
));
172 xhci_dbc_giveback(req
, -ESHUTDOWN
);
175 static void xhci_dbc_flush_endpoint_requests(struct dbc_ep
*dep
)
177 struct dbc_request
*req
, *tmp
;
179 list_for_each_entry_safe(req
, tmp
, &dep
->list_pending
, list_pending
)
180 xhci_dbc_flush_single_request(req
);
183 static void xhci_dbc_flush_reqests(struct xhci_dbc
*dbc
)
185 xhci_dbc_flush_endpoint_requests(&dbc
->eps
[BULK_OUT
]);
186 xhci_dbc_flush_endpoint_requests(&dbc
->eps
[BULK_IN
]);
190 dbc_alloc_request(struct dbc_ep
*dep
, gfp_t gfp_flags
)
192 struct dbc_request
*req
;
194 req
= kzalloc(sizeof(*req
), gfp_flags
);
199 INIT_LIST_HEAD(&req
->list_pending
);
200 INIT_LIST_HEAD(&req
->list_pool
);
201 req
->direction
= dep
->direction
;
203 trace_xhci_dbc_alloc_request(req
);
209 dbc_free_request(struct dbc_ep
*dep
, struct dbc_request
*req
)
211 trace_xhci_dbc_free_request(req
);
217 xhci_dbc_queue_trb(struct xhci_ring
*ring
, u32 field1
,
218 u32 field2
, u32 field3
, u32 field4
)
220 union xhci_trb
*trb
, *next
;
223 trb
->generic
.field
[0] = cpu_to_le32(field1
);
224 trb
->generic
.field
[1] = cpu_to_le32(field2
);
225 trb
->generic
.field
[2] = cpu_to_le32(field3
);
226 trb
->generic
.field
[3] = cpu_to_le32(field4
);
228 trace_xhci_dbc_gadget_ep_queue(ring
, &trb
->generic
);
230 ring
->num_trbs_free
--;
231 next
= ++(ring
->enqueue
);
232 if (TRB_TYPE_LINK_LE32(next
->link
.control
)) {
233 next
->link
.control
^= cpu_to_le32(TRB_CYCLE
);
234 ring
->enqueue
= ring
->enq_seg
->trbs
;
235 ring
->cycle_state
^= 1;
239 static int xhci_dbc_queue_bulk_tx(struct dbc_ep
*dep
,
240 struct dbc_request
*req
)
244 unsigned int num_trbs
;
245 struct xhci_dbc
*dbc
= dep
->dbc
;
246 struct xhci_ring
*ring
= dep
->ring
;
247 u32 length
, control
, cycle
;
249 num_trbs
= count_trbs(req
->dma
, req
->length
);
250 WARN_ON(num_trbs
!= 1);
251 if (ring
->num_trbs_free
< num_trbs
)
256 cycle
= ring
->cycle_state
;
257 length
= TRB_LEN(req
->length
);
258 control
= TRB_TYPE(TRB_NORMAL
) | TRB_IOC
;
261 control
&= cpu_to_le32(~TRB_CYCLE
);
263 control
|= cpu_to_le32(TRB_CYCLE
);
265 req
->trb
= ring
->enqueue
;
266 req
->trb_dma
= xhci_trb_virt_to_dma(ring
->enq_seg
, ring
->enqueue
);
267 xhci_dbc_queue_trb(ring
,
273 * Add a barrier between writes of trb fields and flipping
279 trb
->generic
.field
[3] |= cpu_to_le32(TRB_CYCLE
);
281 trb
->generic
.field
[3] &= cpu_to_le32(~TRB_CYCLE
);
283 writel(DBC_DOOR_BELL_TARGET(dep
->direction
), &dbc
->regs
->doorbell
);
289 dbc_ep_do_queue(struct dbc_ep
*dep
, struct dbc_request
*req
)
293 struct xhci_dbc
*dbc
= dep
->dbc
;
294 struct xhci_hcd
*xhci
= dbc
->xhci
;
296 dev
= xhci_to_hcd(xhci
)->self
.sysdev
;
298 if (!req
->length
|| !req
->buf
)
302 req
->status
= -EINPROGRESS
;
304 req
->dma
= dma_map_single(dev
,
307 dbc_ep_dma_direction(dep
));
308 if (dma_mapping_error(dev
, req
->dma
)) {
309 xhci_err(xhci
, "failed to map buffer\n");
313 ret
= xhci_dbc_queue_bulk_tx(dep
, req
);
315 xhci_err(xhci
, "failed to queue trbs\n");
316 dma_unmap_single(dev
,
319 dbc_ep_dma_direction(dep
));
323 list_add_tail(&req
->list_pending
, &dep
->list_pending
);
328 int dbc_ep_queue(struct dbc_ep
*dep
, struct dbc_request
*req
,
332 struct xhci_dbc
*dbc
= dep
->dbc
;
333 int ret
= -ESHUTDOWN
;
335 spin_lock_irqsave(&dbc
->lock
, flags
);
336 if (dbc
->state
== DS_CONFIGURED
)
337 ret
= dbc_ep_do_queue(dep
, req
);
338 spin_unlock_irqrestore(&dbc
->lock
, flags
);
340 mod_delayed_work(system_wq
, &dbc
->event_work
, 0);
342 trace_xhci_dbc_queue_request(req
);
347 static inline void xhci_dbc_do_eps_init(struct xhci_hcd
*xhci
, bool direction
)
350 struct xhci_dbc
*dbc
= xhci
->dbc
;
352 dep
= &dbc
->eps
[direction
];
354 dep
->direction
= direction
;
355 dep
->ring
= direction
? dbc
->ring_in
: dbc
->ring_out
;
357 INIT_LIST_HEAD(&dep
->list_pending
);
360 static void xhci_dbc_eps_init(struct xhci_hcd
*xhci
)
362 xhci_dbc_do_eps_init(xhci
, BULK_OUT
);
363 xhci_dbc_do_eps_init(xhci
, BULK_IN
);
366 static void xhci_dbc_eps_exit(struct xhci_hcd
*xhci
)
368 struct xhci_dbc
*dbc
= xhci
->dbc
;
370 memset(dbc
->eps
, 0, ARRAY_SIZE(dbc
->eps
));
373 static int xhci_dbc_mem_init(struct xhci_hcd
*xhci
, gfp_t flags
)
378 struct xhci_dbc
*dbc
= xhci
->dbc
;
380 /* Allocate various rings for events and transfers: */
381 dbc
->ring_evt
= xhci_ring_alloc(xhci
, 1, 1, TYPE_EVENT
, 0, flags
);
385 dbc
->ring_in
= xhci_ring_alloc(xhci
, 1, 1, TYPE_BULK
, 0, flags
);
389 dbc
->ring_out
= xhci_ring_alloc(xhci
, 1, 1, TYPE_BULK
, 0, flags
);
393 /* Allocate and populate ERST: */
394 ret
= xhci_alloc_erst(xhci
, dbc
->ring_evt
, &dbc
->erst
, flags
);
398 /* Allocate context data structure: */
399 dbc
->ctx
= xhci_alloc_container_ctx(xhci
, XHCI_CTX_TYPE_DEVICE
, flags
);
403 /* Allocate the string table: */
404 dbc
->string_size
= sizeof(struct dbc_str_descs
);
405 dbc
->string
= dbc_dma_alloc_coherent(xhci
,
412 /* Setup ERST register: */
413 writel(dbc
->erst
.erst_size
, &dbc
->regs
->ersts
);
414 xhci_write_64(xhci
, dbc
->erst
.erst_dma_addr
, &dbc
->regs
->erstba
);
415 deq
= xhci_trb_virt_to_dma(dbc
->ring_evt
->deq_seg
,
416 dbc
->ring_evt
->dequeue
);
417 xhci_write_64(xhci
, deq
, &dbc
->regs
->erdp
);
419 /* Setup strings and contexts: */
420 string_length
= xhci_dbc_populate_strings(dbc
->string
);
421 xhci_dbc_init_contexts(xhci
, string_length
);
425 xhci_dbc_eps_init(xhci
);
426 dbc
->state
= DS_INITIALIZED
;
431 xhci_free_container_ctx(xhci
, dbc
->ctx
);
434 xhci_free_erst(xhci
, &dbc
->erst
);
436 xhci_ring_free(xhci
, dbc
->ring_out
);
437 dbc
->ring_out
= NULL
;
439 xhci_ring_free(xhci
, dbc
->ring_in
);
442 xhci_ring_free(xhci
, dbc
->ring_evt
);
443 dbc
->ring_evt
= NULL
;
448 static void xhci_dbc_mem_cleanup(struct xhci_hcd
*xhci
)
450 struct xhci_dbc
*dbc
= xhci
->dbc
;
455 xhci_dbc_eps_exit(xhci
);
458 dbc_dma_free_coherent(xhci
,
460 dbc
->string
, dbc
->string_dma
);
464 xhci_free_container_ctx(xhci
, dbc
->ctx
);
467 xhci_free_erst(xhci
, &dbc
->erst
);
468 xhci_ring_free(xhci
, dbc
->ring_out
);
469 xhci_ring_free(xhci
, dbc
->ring_in
);
470 xhci_ring_free(xhci
, dbc
->ring_evt
);
472 dbc
->ring_out
= NULL
;
473 dbc
->ring_evt
= NULL
;
476 static int xhci_do_dbc_start(struct xhci_hcd
*xhci
)
480 struct xhci_dbc
*dbc
= xhci
->dbc
;
482 if (dbc
->state
!= DS_DISABLED
)
485 writel(0, &dbc
->regs
->control
);
486 ret
= xhci_handshake(&dbc
->regs
->control
,
492 ret
= xhci_dbc_mem_init(xhci
, GFP_ATOMIC
);
496 ctrl
= readl(&dbc
->regs
->control
);
497 writel(ctrl
| DBC_CTRL_DBC_ENABLE
| DBC_CTRL_PORT_ENABLE
,
498 &dbc
->regs
->control
);
499 ret
= xhci_handshake(&dbc
->regs
->control
,
501 DBC_CTRL_DBC_ENABLE
, 1000);
505 dbc
->state
= DS_ENABLED
;
510 static int xhci_do_dbc_stop(struct xhci_hcd
*xhci
)
512 struct xhci_dbc
*dbc
= xhci
->dbc
;
514 if (dbc
->state
== DS_DISABLED
)
517 writel(0, &dbc
->regs
->control
);
518 xhci_dbc_mem_cleanup(xhci
);
519 dbc
->state
= DS_DISABLED
;
524 static int xhci_dbc_start(struct xhci_hcd
*xhci
)
528 struct xhci_dbc
*dbc
= xhci
->dbc
;
532 pm_runtime_get_sync(xhci_to_hcd(xhci
)->self
.controller
);
534 spin_lock_irqsave(&dbc
->lock
, flags
);
535 ret
= xhci_do_dbc_start(xhci
);
536 spin_unlock_irqrestore(&dbc
->lock
, flags
);
539 pm_runtime_put(xhci_to_hcd(xhci
)->self
.controller
);
543 return mod_delayed_work(system_wq
, &dbc
->event_work
, 1);
546 static void xhci_dbc_stop(struct xhci_hcd
*xhci
)
550 struct xhci_dbc
*dbc
= xhci
->dbc
;
551 struct dbc_port
*port
= &dbc
->port
;
555 cancel_delayed_work_sync(&dbc
->event_work
);
557 if (port
->registered
)
558 xhci_dbc_tty_unregister_device(xhci
);
560 spin_lock_irqsave(&dbc
->lock
, flags
);
561 ret
= xhci_do_dbc_stop(xhci
);
562 spin_unlock_irqrestore(&dbc
->lock
, flags
);
565 pm_runtime_put_sync(xhci_to_hcd(xhci
)->self
.controller
);
569 dbc_handle_port_status(struct xhci_hcd
*xhci
, union xhci_trb
*event
)
572 struct xhci_dbc
*dbc
= xhci
->dbc
;
574 portsc
= readl(&dbc
->regs
->portsc
);
575 if (portsc
& DBC_PORTSC_CONN_CHANGE
)
576 xhci_info(xhci
, "DbC port connect change\n");
578 if (portsc
& DBC_PORTSC_RESET_CHANGE
)
579 xhci_info(xhci
, "DbC port reset change\n");
581 if (portsc
& DBC_PORTSC_LINK_CHANGE
)
582 xhci_info(xhci
, "DbC port link status change\n");
584 if (portsc
& DBC_PORTSC_CONFIG_CHANGE
)
585 xhci_info(xhci
, "DbC config error change\n");
587 /* Port reset change bit will be cleared in other place: */
588 writel(portsc
& ~DBC_PORTSC_RESET_CHANGE
, &dbc
->regs
->portsc
);
591 static void dbc_handle_xfer_event(struct xhci_hcd
*xhci
, union xhci_trb
*event
)
594 struct xhci_ring
*ring
;
598 size_t remain_length
;
599 struct dbc_request
*req
= NULL
, *r
;
601 comp_code
= GET_COMP_CODE(le32_to_cpu(event
->generic
.field
[2]));
602 remain_length
= EVENT_TRB_LEN(le32_to_cpu(event
->generic
.field
[2]));
603 ep_id
= TRB_TO_EP_ID(le32_to_cpu(event
->generic
.field
[3]));
604 dep
= (ep_id
== EPID_OUT
) ?
605 get_out_ep(xhci
) : get_in_ep(xhci
);
612 case COMP_SHORT_PACKET
:
616 case COMP_BABBLE_DETECTED_ERROR
:
617 case COMP_USB_TRANSACTION_ERROR
:
618 case COMP_STALL_ERROR
:
619 xhci_warn(xhci
, "tx error %d detected\n", comp_code
);
623 xhci_err(xhci
, "unknown tx error %d\n", comp_code
);
628 /* Match the pending request: */
629 list_for_each_entry(r
, &dep
->list_pending
, list_pending
) {
630 if (r
->trb_dma
== event
->trans_event
.buffer
) {
637 xhci_warn(xhci
, "no matched request\n");
641 trace_xhci_dbc_handle_transfer(ring
, &req
->trb
->generic
);
643 ring
->num_trbs_free
++;
644 req
->actual
= req
->length
- remain_length
;
645 xhci_dbc_giveback(req
, status
);
648 static enum evtreturn
xhci_dbc_do_handle_events(struct xhci_dbc
*dbc
)
654 struct xhci_hcd
*xhci
= dbc
->xhci
;
655 bool update_erdp
= false;
657 /* DbC state machine: */
658 switch (dbc
->state
) {
664 portsc
= readl(&dbc
->regs
->portsc
);
665 if (portsc
& DBC_PORTSC_CONN_STATUS
) {
666 dbc
->state
= DS_CONNECTED
;
667 xhci_info(xhci
, "DbC connected\n");
672 ctrl
= readl(&dbc
->regs
->control
);
673 if (ctrl
& DBC_CTRL_DBC_RUN
) {
674 dbc
->state
= DS_CONFIGURED
;
675 xhci_info(xhci
, "DbC configured\n");
676 portsc
= readl(&dbc
->regs
->portsc
);
677 writel(portsc
, &dbc
->regs
->portsc
);
683 /* Handle cable unplug event: */
684 portsc
= readl(&dbc
->regs
->portsc
);
685 if (!(portsc
& DBC_PORTSC_PORT_ENABLED
) &&
686 !(portsc
& DBC_PORTSC_CONN_STATUS
)) {
687 xhci_info(xhci
, "DbC cable unplugged\n");
688 dbc
->state
= DS_ENABLED
;
689 xhci_dbc_flush_reqests(dbc
);
694 /* Handle debug port reset event: */
695 if (portsc
& DBC_PORTSC_RESET_CHANGE
) {
696 xhci_info(xhci
, "DbC port reset\n");
697 writel(portsc
, &dbc
->regs
->portsc
);
698 dbc
->state
= DS_ENABLED
;
699 xhci_dbc_flush_reqests(dbc
);
704 /* Handle endpoint stall event: */
705 ctrl
= readl(&dbc
->regs
->control
);
706 if ((ctrl
& DBC_CTRL_HALT_IN_TR
) ||
707 (ctrl
& DBC_CTRL_HALT_OUT_TR
)) {
708 xhci_info(xhci
, "DbC Endpoint stall\n");
709 dbc
->state
= DS_STALLED
;
711 if (ctrl
& DBC_CTRL_HALT_IN_TR
) {
712 dep
= get_in_ep(xhci
);
713 xhci_dbc_flush_endpoint_requests(dep
);
716 if (ctrl
& DBC_CTRL_HALT_OUT_TR
) {
717 dep
= get_out_ep(xhci
);
718 xhci_dbc_flush_endpoint_requests(dep
);
724 /* Clear DbC run change bit: */
725 if (ctrl
& DBC_CTRL_DBC_RUN_CHANGE
) {
726 writel(ctrl
, &dbc
->regs
->control
);
727 ctrl
= readl(&dbc
->regs
->control
);
732 ctrl
= readl(&dbc
->regs
->control
);
733 if (!(ctrl
& DBC_CTRL_HALT_IN_TR
) &&
734 !(ctrl
& DBC_CTRL_HALT_OUT_TR
) &&
735 (ctrl
& DBC_CTRL_DBC_RUN
)) {
736 dbc
->state
= DS_CONFIGURED
;
742 xhci_err(xhci
, "Unknown DbC state %d\n", dbc
->state
);
746 /* Handle the events in the event ring: */
747 evt
= dbc
->ring_evt
->dequeue
;
748 while ((le32_to_cpu(evt
->event_cmd
.flags
) & TRB_CYCLE
) ==
749 dbc
->ring_evt
->cycle_state
) {
751 * Add a barrier between reading the cycle flag and any
752 * reads of the event's flags/data below:
756 trace_xhci_dbc_handle_event(dbc
->ring_evt
, &evt
->generic
);
758 switch (le32_to_cpu(evt
->event_cmd
.flags
) & TRB_TYPE_BITMASK
) {
759 case TRB_TYPE(TRB_PORT_STATUS
):
760 dbc_handle_port_status(xhci
, evt
);
762 case TRB_TYPE(TRB_TRANSFER
):
763 dbc_handle_xfer_event(xhci
, evt
);
769 inc_deq(xhci
, dbc
->ring_evt
);
770 evt
= dbc
->ring_evt
->dequeue
;
774 /* Update event ring dequeue pointer: */
776 deq
= xhci_trb_virt_to_dma(dbc
->ring_evt
->deq_seg
,
777 dbc
->ring_evt
->dequeue
);
778 xhci_write_64(xhci
, deq
, &dbc
->regs
->erdp
);
784 static void xhci_dbc_handle_events(struct work_struct
*work
)
788 struct xhci_dbc
*dbc
;
790 struct xhci_hcd
*xhci
;
792 dbc
= container_of(to_delayed_work(work
), struct xhci_dbc
, event_work
);
795 spin_lock_irqsave(&dbc
->lock
, flags
);
796 evtr
= xhci_dbc_do_handle_events(dbc
);
797 spin_unlock_irqrestore(&dbc
->lock
, flags
);
801 ret
= xhci_dbc_tty_register_device(xhci
);
803 xhci_err(xhci
, "failed to alloc tty device\n");
807 xhci_info(xhci
, "DbC now attached to /dev/ttyDBC0\n");
810 xhci_dbc_tty_unregister_device(xhci
);
815 xhci_info(xhci
, "stop handling dbc events\n");
819 mod_delayed_work(system_wq
, &dbc
->event_work
, 1);
822 static void xhci_do_dbc_exit(struct xhci_hcd
*xhci
)
826 spin_lock_irqsave(&xhci
->lock
, flags
);
829 spin_unlock_irqrestore(&xhci
->lock
, flags
);
832 static int xhci_do_dbc_init(struct xhci_hcd
*xhci
)
835 struct xhci_dbc
*dbc
;
840 base
= &xhci
->cap_regs
->hc_capbase
;
841 dbc_cap_offs
= xhci_find_next_ext_cap(base
, 0, XHCI_EXT_CAPS_DEBUG
);
845 dbc
= kzalloc(sizeof(*dbc
), GFP_KERNEL
);
849 dbc
->regs
= base
+ dbc_cap_offs
;
851 /* We will avoid using DbC in xhci driver if it's in use. */
852 reg
= readl(&dbc
->regs
->control
);
853 if (reg
& DBC_CTRL_DBC_ENABLE
) {
858 spin_lock_irqsave(&xhci
->lock
, flags
);
860 spin_unlock_irqrestore(&xhci
->lock
, flags
);
865 spin_unlock_irqrestore(&xhci
->lock
, flags
);
868 INIT_DELAYED_WORK(&dbc
->event_work
, xhci_dbc_handle_events
);
869 spin_lock_init(&dbc
->lock
);
874 static ssize_t
dbc_show(struct device
*dev
,
875 struct device_attribute
*attr
,
879 struct xhci_dbc
*dbc
;
880 struct xhci_hcd
*xhci
;
882 xhci
= hcd_to_xhci(dev_get_drvdata(dev
));
885 switch (dbc
->state
) {
908 return sprintf(buf
, "%s\n", p
);
911 static ssize_t
dbc_store(struct device
*dev
,
912 struct device_attribute
*attr
,
913 const char *buf
, size_t count
)
915 struct xhci_dbc
*dbc
;
916 struct xhci_hcd
*xhci
;
918 xhci
= hcd_to_xhci(dev_get_drvdata(dev
));
921 if (!strncmp(buf
, "enable", 6))
922 xhci_dbc_start(xhci
);
923 else if (!strncmp(buf
, "disable", 7))
931 static DEVICE_ATTR(dbc
, 0644, dbc_show
, dbc_store
);
933 int xhci_dbc_init(struct xhci_hcd
*xhci
)
936 struct device
*dev
= xhci_to_hcd(xhci
)->self
.controller
;
938 ret
= xhci_do_dbc_init(xhci
);
942 ret
= xhci_dbc_tty_register_driver(xhci
);
946 ret
= device_create_file(dev
, &dev_attr_dbc
);
953 xhci_dbc_tty_unregister_driver();
955 xhci_do_dbc_exit(xhci
);
960 void xhci_dbc_exit(struct xhci_hcd
*xhci
)
962 struct device
*dev
= xhci_to_hcd(xhci
)->self
.controller
;
967 device_remove_file(dev
, &dev_attr_dbc
);
968 xhci_dbc_tty_unregister_driver();
970 xhci_do_dbc_exit(xhci
);
974 int xhci_dbc_suspend(struct xhci_hcd
*xhci
)
976 struct xhci_dbc
*dbc
= xhci
->dbc
;
981 if (dbc
->state
== DS_CONFIGURED
)
982 dbc
->resume_required
= 1;
989 int xhci_dbc_resume(struct xhci_hcd
*xhci
)
992 struct xhci_dbc
*dbc
= xhci
->dbc
;
997 if (dbc
->resume_required
) {
998 dbc
->resume_required
= 0;
999 xhci_dbc_start(xhci
);
1004 #endif /* CONFIG_PM */