1 // SPDX-License-Identifier: GPL-2.0
3 * xhci-dbgcap.c - xHCI debug capability support
5 * Copyright (C) 2017 Intel Corporation
7 * Author: Lu Baolu <baolu.lu@linux.intel.com>
10 #include <linux/device.h>
11 #include <linux/dma-mapping.h>
12 #include <linux/errno.h>
13 #include <linux/kstrtox.h>
14 #include <linux/list.h>
15 #include <linux/nls.h>
16 #include <linux/pm_runtime.h>
17 #include <linux/slab.h>
18 #include <linux/spinlock.h>
19 #include <linux/string.h>
20 #include <linux/sysfs.h>
21 #include <linux/types.h>
22 #include <linux/workqueue.h>
24 #include <linux/io-64-nonatomic-lo-hi.h>
26 #include <asm/byteorder.h>
29 #include "xhci-trace.h"
30 #include "xhci-dbgcap.h"
32 static void dbc_free_ctx(struct device *dev, struct xhci_container_ctx *ctx)
36 dma_free_coherent(dev, ctx->size, ctx->bytes, ctx->dma);
40 /* we use only one segment for DbC rings */
41 static void dbc_ring_free(struct device *dev, struct xhci_ring *ring)
46 if (ring->first_seg) {
47 dma_free_coherent(dev, TRB_SEGMENT_SIZE,
48 ring->first_seg->trbs,
49 ring->first_seg->dma);
50 kfree(ring->first_seg);
55 static u32 xhci_dbc_populate_strings(struct dbc_str_descs *strings)
57 struct usb_string_descriptor *s_desc;
61 s_desc = (struct usb_string_descriptor *)strings->serial;
62 utf8s_to_utf16s(DBC_STRING_SERIAL, strlen(DBC_STRING_SERIAL),
63 UTF16_LITTLE_ENDIAN, (wchar_t *)s_desc->wData,
64 DBC_MAX_STRING_LENGTH);
66 s_desc->bLength = (strlen(DBC_STRING_SERIAL) + 1) * 2;
67 s_desc->bDescriptorType = USB_DT_STRING;
68 string_length = s_desc->bLength;
72 s_desc = (struct usb_string_descriptor *)strings->product;
73 utf8s_to_utf16s(DBC_STRING_PRODUCT, strlen(DBC_STRING_PRODUCT),
74 UTF16_LITTLE_ENDIAN, (wchar_t *)s_desc->wData,
75 DBC_MAX_STRING_LENGTH);
77 s_desc->bLength = (strlen(DBC_STRING_PRODUCT) + 1) * 2;
78 s_desc->bDescriptorType = USB_DT_STRING;
79 string_length += s_desc->bLength;
82 /* Manufacture string: */
83 s_desc = (struct usb_string_descriptor *)strings->manufacturer;
84 utf8s_to_utf16s(DBC_STRING_MANUFACTURER,
85 strlen(DBC_STRING_MANUFACTURER),
86 UTF16_LITTLE_ENDIAN, (wchar_t *)s_desc->wData,
87 DBC_MAX_STRING_LENGTH);
89 s_desc->bLength = (strlen(DBC_STRING_MANUFACTURER) + 1) * 2;
90 s_desc->bDescriptorType = USB_DT_STRING;
91 string_length += s_desc->bLength;
95 strings->string0[0] = 4;
96 strings->string0[1] = USB_DT_STRING;
97 strings->string0[2] = 0x09;
98 strings->string0[3] = 0x04;
101 return string_length;
104 static void xhci_dbc_init_contexts(struct xhci_dbc *dbc, u32 string_length)
106 struct dbc_info_context *info;
107 struct xhci_ep_ctx *ep_ctx;
110 unsigned int max_burst;
115 /* Populate info Context: */
116 info = (struct dbc_info_context *)dbc->ctx->bytes;
117 dma = dbc->string_dma;
118 info->string0 = cpu_to_le64(dma);
119 info->manufacturer = cpu_to_le64(dma + DBC_MAX_STRING_LENGTH);
120 info->product = cpu_to_le64(dma + DBC_MAX_STRING_LENGTH * 2);
121 info->serial = cpu_to_le64(dma + DBC_MAX_STRING_LENGTH * 3);
122 info->length = cpu_to_le32(string_length);
124 /* Populate bulk out endpoint context: */
125 ep_ctx = dbc_bulkout_ctx(dbc);
126 max_burst = DBC_CTRL_MAXBURST(readl(&dbc->regs->control));
127 deq = dbc_bulkout_enq(dbc);
129 ep_ctx->ep_info2 = dbc_epctx_info2(BULK_OUT_EP, 1024, max_burst);
130 ep_ctx->deq = cpu_to_le64(deq | dbc->ring_out->cycle_state);
132 /* Populate bulk in endpoint context: */
133 ep_ctx = dbc_bulkin_ctx(dbc);
134 deq = dbc_bulkin_enq(dbc);
136 ep_ctx->ep_info2 = dbc_epctx_info2(BULK_IN_EP, 1024, max_burst);
137 ep_ctx->deq = cpu_to_le64(deq | dbc->ring_in->cycle_state);
139 /* Set DbC context and info registers: */
140 lo_hi_writeq(dbc->ctx->dma, &dbc->regs->dccp);
142 dev_info = (dbc->idVendor << 16) | dbc->bInterfaceProtocol;
143 writel(dev_info, &dbc->regs->devinfo1);
145 dev_info = (dbc->bcdDevice << 16) | dbc->idProduct;
146 writel(dev_info, &dbc->regs->devinfo2);
149 static void xhci_dbc_giveback(struct dbc_request *req, int status)
150 __releases(&dbc->lock)
151 __acquires(&dbc->lock)
153 struct xhci_dbc *dbc = req->dbc;
154 struct device *dev = dbc->dev;
156 list_del_init(&req->list_pending);
160 if (req->status == -EINPROGRESS)
161 req->status = status;
163 trace_xhci_dbc_giveback_request(req);
165 dma_unmap_single(dev,
168 dbc_ep_dma_direction(req));
170 /* Give back the transfer request: */
171 spin_unlock(&dbc->lock);
172 req->complete(dbc, req);
173 spin_lock(&dbc->lock);
176 static void xhci_dbc_flush_single_request(struct dbc_request *req)
178 union xhci_trb *trb = req->trb;
180 trb->generic.field[0] = 0;
181 trb->generic.field[1] = 0;
182 trb->generic.field[2] = 0;
183 trb->generic.field[3] &= cpu_to_le32(TRB_CYCLE);
184 trb->generic.field[3] |= cpu_to_le32(TRB_TYPE(TRB_TR_NOOP));
186 xhci_dbc_giveback(req, -ESHUTDOWN);
189 static void xhci_dbc_flush_endpoint_requests(struct dbc_ep *dep)
191 struct dbc_request *req, *tmp;
193 list_for_each_entry_safe(req, tmp, &dep->list_pending, list_pending)
194 xhci_dbc_flush_single_request(req);
197 static void xhci_dbc_flush_requests(struct xhci_dbc *dbc)
199 xhci_dbc_flush_endpoint_requests(&dbc->eps[BULK_OUT]);
200 xhci_dbc_flush_endpoint_requests(&dbc->eps[BULK_IN]);
204 dbc_alloc_request(struct xhci_dbc *dbc, unsigned int direction, gfp_t flags)
206 struct dbc_request *req;
208 if (direction != BULK_IN &&
209 direction != BULK_OUT)
215 req = kzalloc(sizeof(*req), flags);
220 INIT_LIST_HEAD(&req->list_pending);
221 INIT_LIST_HEAD(&req->list_pool);
222 req->direction = direction;
224 trace_xhci_dbc_alloc_request(req);
230 dbc_free_request(struct dbc_request *req)
232 trace_xhci_dbc_free_request(req);
238 xhci_dbc_queue_trb(struct xhci_ring *ring, u32 field1,
239 u32 field2, u32 field3, u32 field4)
241 union xhci_trb *trb, *next;
244 trb->generic.field[0] = cpu_to_le32(field1);
245 trb->generic.field[1] = cpu_to_le32(field2);
246 trb->generic.field[2] = cpu_to_le32(field3);
247 trb->generic.field[3] = cpu_to_le32(field4);
249 trace_xhci_dbc_gadget_ep_queue(ring, &trb->generic);
251 ring->num_trbs_free--;
252 next = ++(ring->enqueue);
253 if (TRB_TYPE_LINK_LE32(next->link.control)) {
254 next->link.control ^= cpu_to_le32(TRB_CYCLE);
255 ring->enqueue = ring->enq_seg->trbs;
256 ring->cycle_state ^= 1;
260 static int xhci_dbc_queue_bulk_tx(struct dbc_ep *dep,
261 struct dbc_request *req)
265 unsigned int num_trbs;
266 struct xhci_dbc *dbc = req->dbc;
267 struct xhci_ring *ring = dep->ring;
268 u32 length, control, cycle;
270 num_trbs = count_trbs(req->dma, req->length);
271 WARN_ON(num_trbs != 1);
272 if (ring->num_trbs_free < num_trbs)
277 cycle = ring->cycle_state;
278 length = TRB_LEN(req->length);
279 control = TRB_TYPE(TRB_NORMAL) | TRB_IOC;
282 control &= cpu_to_le32(~TRB_CYCLE);
284 control |= cpu_to_le32(TRB_CYCLE);
286 req->trb = ring->enqueue;
287 req->trb_dma = xhci_trb_virt_to_dma(ring->enq_seg, ring->enqueue);
288 xhci_dbc_queue_trb(ring,
294 * Add a barrier between writes of trb fields and flipping
300 trb->generic.field[3] |= cpu_to_le32(TRB_CYCLE);
302 trb->generic.field[3] &= cpu_to_le32(~TRB_CYCLE);
304 writel(DBC_DOOR_BELL_TARGET(dep->direction), &dbc->regs->doorbell);
310 dbc_ep_do_queue(struct dbc_request *req)
313 struct xhci_dbc *dbc = req->dbc;
314 struct device *dev = dbc->dev;
315 struct dbc_ep *dep = &dbc->eps[req->direction];
317 if (!req->length || !req->buf)
321 req->status = -EINPROGRESS;
323 req->dma = dma_map_single(dev,
326 dbc_ep_dma_direction(dep));
327 if (dma_mapping_error(dev, req->dma)) {
328 dev_err(dbc->dev, "failed to map buffer\n");
332 ret = xhci_dbc_queue_bulk_tx(dep, req);
334 dev_err(dbc->dev, "failed to queue trbs\n");
335 dma_unmap_single(dev,
338 dbc_ep_dma_direction(dep));
342 list_add_tail(&req->list_pending, &dep->list_pending);
347 int dbc_ep_queue(struct dbc_request *req)
350 struct xhci_dbc *dbc = req->dbc;
351 int ret = -ESHUTDOWN;
356 if (req->direction != BULK_IN &&
357 req->direction != BULK_OUT)
360 spin_lock_irqsave(&dbc->lock, flags);
361 if (dbc->state == DS_CONFIGURED)
362 ret = dbc_ep_do_queue(req);
363 spin_unlock_irqrestore(&dbc->lock, flags);
365 mod_delayed_work(system_wq, &dbc->event_work, 0);
367 trace_xhci_dbc_queue_request(req);
372 static inline void xhci_dbc_do_eps_init(struct xhci_dbc *dbc, bool direction)
376 dep = &dbc->eps[direction];
378 dep->direction = direction;
379 dep->ring = direction ? dbc->ring_in : dbc->ring_out;
381 INIT_LIST_HEAD(&dep->list_pending);
384 static void xhci_dbc_eps_init(struct xhci_dbc *dbc)
386 xhci_dbc_do_eps_init(dbc, BULK_OUT);
387 xhci_dbc_do_eps_init(dbc, BULK_IN);
390 static void xhci_dbc_eps_exit(struct xhci_dbc *dbc)
392 memset(dbc->eps, 0, sizeof_field(struct xhci_dbc, eps));
395 static int dbc_erst_alloc(struct device *dev, struct xhci_ring *evt_ring,
396 struct xhci_erst *erst, gfp_t flags)
398 erst->entries = dma_alloc_coherent(dev, sizeof(*erst->entries),
399 &erst->erst_dma_addr, flags);
403 erst->num_entries = 1;
404 erst->entries[0].seg_addr = cpu_to_le64(evt_ring->first_seg->dma);
405 erst->entries[0].seg_size = cpu_to_le32(TRBS_PER_SEGMENT);
406 erst->entries[0].rsvd = 0;
410 static void dbc_erst_free(struct device *dev, struct xhci_erst *erst)
412 dma_free_coherent(dev, sizeof(*erst->entries), erst->entries,
413 erst->erst_dma_addr);
414 erst->entries = NULL;
417 static struct xhci_container_ctx *
418 dbc_alloc_ctx(struct device *dev, gfp_t flags)
420 struct xhci_container_ctx *ctx;
422 ctx = kzalloc(sizeof(*ctx), flags);
426 /* xhci 7.6.9, all three contexts; info, ep-out and ep-in. Each 64 bytes*/
427 ctx->size = 3 * DBC_CONTEXT_SIZE;
428 ctx->bytes = dma_alloc_coherent(dev, ctx->size, &ctx->dma, flags);
436 static struct xhci_ring *
437 xhci_dbc_ring_alloc(struct device *dev, enum xhci_ring_type type, gfp_t flags)
439 struct xhci_ring *ring;
440 struct xhci_segment *seg;
443 ring = kzalloc(sizeof(*ring), flags);
450 seg = kzalloc(sizeof(*seg), flags);
454 ring->first_seg = seg;
455 ring->last_seg = seg;
458 seg->trbs = dma_alloc_coherent(dev, TRB_SEGMENT_SIZE, &dma, flags);
464 /* Only event ring does not use link TRB */
465 if (type != TYPE_EVENT) {
466 union xhci_trb *trb = &seg->trbs[TRBS_PER_SEGMENT - 1];
468 trb->link.segment_ptr = cpu_to_le64(dma);
469 trb->link.control = cpu_to_le32(LINK_TOGGLE | TRB_TYPE(TRB_LINK));
471 INIT_LIST_HEAD(&ring->td_list);
472 xhci_initialize_ring_info(ring, 1);
481 static int xhci_dbc_mem_init(struct xhci_dbc *dbc, gfp_t flags)
486 struct device *dev = dbc->dev;
488 /* Allocate various rings for events and transfers: */
489 dbc->ring_evt = xhci_dbc_ring_alloc(dev, TYPE_EVENT, flags);
493 dbc->ring_in = xhci_dbc_ring_alloc(dev, TYPE_BULK, flags);
497 dbc->ring_out = xhci_dbc_ring_alloc(dev, TYPE_BULK, flags);
501 /* Allocate and populate ERST: */
502 ret = dbc_erst_alloc(dev, dbc->ring_evt, &dbc->erst, flags);
506 /* Allocate context data structure: */
507 dbc->ctx = dbc_alloc_ctx(dev, flags); /* was sysdev, and is still */
511 /* Allocate the string table: */
512 dbc->string_size = sizeof(*dbc->string);
513 dbc->string = dma_alloc_coherent(dev, dbc->string_size,
514 &dbc->string_dma, flags);
518 /* Setup ERST register: */
519 writel(dbc->erst.erst_size, &dbc->regs->ersts);
521 lo_hi_writeq(dbc->erst.erst_dma_addr, &dbc->regs->erstba);
522 deq = xhci_trb_virt_to_dma(dbc->ring_evt->deq_seg,
523 dbc->ring_evt->dequeue);
524 lo_hi_writeq(deq, &dbc->regs->erdp);
526 /* Setup strings and contexts: */
527 string_length = xhci_dbc_populate_strings(dbc->string);
528 xhci_dbc_init_contexts(dbc, string_length);
530 xhci_dbc_eps_init(dbc);
531 dbc->state = DS_INITIALIZED;
536 dbc_free_ctx(dev, dbc->ctx);
539 dbc_erst_free(dev, &dbc->erst);
541 dbc_ring_free(dev, dbc->ring_out);
542 dbc->ring_out = NULL;
544 dbc_ring_free(dev, dbc->ring_in);
547 dbc_ring_free(dev, dbc->ring_evt);
548 dbc->ring_evt = NULL;
553 static void xhci_dbc_mem_cleanup(struct xhci_dbc *dbc)
558 xhci_dbc_eps_exit(dbc);
560 dma_free_coherent(dbc->dev, dbc->string_size, dbc->string, dbc->string_dma);
563 dbc_free_ctx(dbc->dev, dbc->ctx);
566 dbc_erst_free(dbc->dev, &dbc->erst);
567 dbc_ring_free(dbc->dev, dbc->ring_out);
568 dbc_ring_free(dbc->dev, dbc->ring_in);
569 dbc_ring_free(dbc->dev, dbc->ring_evt);
571 dbc->ring_out = NULL;
572 dbc->ring_evt = NULL;
575 static int xhci_do_dbc_start(struct xhci_dbc *dbc)
580 if (dbc->state != DS_DISABLED)
583 writel(0, &dbc->regs->control);
584 ret = xhci_handshake(&dbc->regs->control,
590 ret = xhci_dbc_mem_init(dbc, GFP_ATOMIC);
594 ctrl = readl(&dbc->regs->control);
595 writel(ctrl | DBC_CTRL_DBC_ENABLE | DBC_CTRL_PORT_ENABLE,
596 &dbc->regs->control);
597 ret = xhci_handshake(&dbc->regs->control,
599 DBC_CTRL_DBC_ENABLE, 1000);
603 dbc->state = DS_ENABLED;
608 static int xhci_do_dbc_stop(struct xhci_dbc *dbc)
610 if (dbc->state == DS_DISABLED)
613 writel(0, &dbc->regs->control);
614 dbc->state = DS_DISABLED;
619 static int xhci_dbc_start(struct xhci_dbc *dbc)
626 pm_runtime_get_sync(dbc->dev); /* note this was self.controller */
628 spin_lock_irqsave(&dbc->lock, flags);
629 ret = xhci_do_dbc_start(dbc);
630 spin_unlock_irqrestore(&dbc->lock, flags);
633 pm_runtime_put(dbc->dev); /* note this was self.controller */
637 return mod_delayed_work(system_wq, &dbc->event_work, 1);
640 static void xhci_dbc_stop(struct xhci_dbc *dbc)
647 switch (dbc->state) {
652 if (dbc->driver->disconnect)
653 dbc->driver->disconnect(dbc);
659 cancel_delayed_work_sync(&dbc->event_work);
661 spin_lock_irqsave(&dbc->lock, flags);
662 ret = xhci_do_dbc_stop(dbc);
663 spin_unlock_irqrestore(&dbc->lock, flags);
667 xhci_dbc_mem_cleanup(dbc);
668 pm_runtime_put_sync(dbc->dev); /* note, was self.controller */
672 dbc_handle_port_status(struct xhci_dbc *dbc, union xhci_trb *event)
676 portsc = readl(&dbc->regs->portsc);
677 if (portsc & DBC_PORTSC_CONN_CHANGE)
678 dev_info(dbc->dev, "DbC port connect change\n");
680 if (portsc & DBC_PORTSC_RESET_CHANGE)
681 dev_info(dbc->dev, "DbC port reset change\n");
683 if (portsc & DBC_PORTSC_LINK_CHANGE)
684 dev_info(dbc->dev, "DbC port link status change\n");
686 if (portsc & DBC_PORTSC_CONFIG_CHANGE)
687 dev_info(dbc->dev, "DbC config error change\n");
689 /* Port reset change bit will be cleared in other place: */
690 writel(portsc & ~DBC_PORTSC_RESET_CHANGE, &dbc->regs->portsc);
693 static void dbc_handle_xfer_event(struct xhci_dbc *dbc, union xhci_trb *event)
696 struct xhci_ring *ring;
700 size_t remain_length;
701 struct dbc_request *req = NULL, *r;
703 comp_code = GET_COMP_CODE(le32_to_cpu(event->generic.field[2]));
704 remain_length = EVENT_TRB_LEN(le32_to_cpu(event->generic.field[2]));
705 ep_id = TRB_TO_EP_ID(le32_to_cpu(event->generic.field[3]));
706 dep = (ep_id == EPID_OUT) ?
707 get_out_ep(dbc) : get_in_ep(dbc);
714 case COMP_SHORT_PACKET:
718 case COMP_BABBLE_DETECTED_ERROR:
719 case COMP_USB_TRANSACTION_ERROR:
720 case COMP_STALL_ERROR:
721 dev_warn(dbc->dev, "tx error %d detected\n", comp_code);
725 dev_err(dbc->dev, "unknown tx error %d\n", comp_code);
730 /* Match the pending request: */
731 list_for_each_entry(r, &dep->list_pending, list_pending) {
732 if (r->trb_dma == event->trans_event.buffer) {
739 dev_warn(dbc->dev, "no matched request\n");
743 trace_xhci_dbc_handle_transfer(ring, &req->trb->generic);
745 ring->num_trbs_free++;
746 req->actual = req->length - remain_length;
747 xhci_dbc_giveback(req, status);
750 static void inc_evt_deq(struct xhci_ring *ring)
752 /* If on the last TRB of the segment go back to the beginning */
753 if (ring->dequeue == &ring->deq_seg->trbs[TRBS_PER_SEGMENT - 1]) {
754 ring->cycle_state ^= 1;
755 ring->dequeue = ring->deq_seg->trbs;
761 static enum evtreturn xhci_dbc_do_handle_events(struct xhci_dbc *dbc)
767 bool update_erdp = false;
769 /* DbC state machine: */
770 switch (dbc->state) {
776 portsc = readl(&dbc->regs->portsc);
777 if (portsc & DBC_PORTSC_CONN_STATUS) {
778 dbc->state = DS_CONNECTED;
779 dev_info(dbc->dev, "DbC connected\n");
784 ctrl = readl(&dbc->regs->control);
785 if (ctrl & DBC_CTRL_DBC_RUN) {
786 dbc->state = DS_CONFIGURED;
787 dev_info(dbc->dev, "DbC configured\n");
788 portsc = readl(&dbc->regs->portsc);
789 writel(portsc, &dbc->regs->portsc);
795 /* Handle cable unplug event: */
796 portsc = readl(&dbc->regs->portsc);
797 if (!(portsc & DBC_PORTSC_PORT_ENABLED) &&
798 !(portsc & DBC_PORTSC_CONN_STATUS)) {
799 dev_info(dbc->dev, "DbC cable unplugged\n");
800 dbc->state = DS_ENABLED;
801 xhci_dbc_flush_requests(dbc);
806 /* Handle debug port reset event: */
807 if (portsc & DBC_PORTSC_RESET_CHANGE) {
808 dev_info(dbc->dev, "DbC port reset\n");
809 writel(portsc, &dbc->regs->portsc);
810 dbc->state = DS_ENABLED;
811 xhci_dbc_flush_requests(dbc);
816 /* Handle endpoint stall event: */
817 ctrl = readl(&dbc->regs->control);
818 if ((ctrl & DBC_CTRL_HALT_IN_TR) ||
819 (ctrl & DBC_CTRL_HALT_OUT_TR)) {
820 dev_info(dbc->dev, "DbC Endpoint stall\n");
821 dbc->state = DS_STALLED;
823 if (ctrl & DBC_CTRL_HALT_IN_TR) {
824 dep = get_in_ep(dbc);
825 xhci_dbc_flush_endpoint_requests(dep);
828 if (ctrl & DBC_CTRL_HALT_OUT_TR) {
829 dep = get_out_ep(dbc);
830 xhci_dbc_flush_endpoint_requests(dep);
836 /* Clear DbC run change bit: */
837 if (ctrl & DBC_CTRL_DBC_RUN_CHANGE) {
838 writel(ctrl, &dbc->regs->control);
839 ctrl = readl(&dbc->regs->control);
844 ctrl = readl(&dbc->regs->control);
845 if (!(ctrl & DBC_CTRL_HALT_IN_TR) &&
846 !(ctrl & DBC_CTRL_HALT_OUT_TR) &&
847 (ctrl & DBC_CTRL_DBC_RUN)) {
848 dbc->state = DS_CONFIGURED;
854 dev_err(dbc->dev, "Unknown DbC state %d\n", dbc->state);
858 /* Handle the events in the event ring: */
859 evt = dbc->ring_evt->dequeue;
860 while ((le32_to_cpu(evt->event_cmd.flags) & TRB_CYCLE) ==
861 dbc->ring_evt->cycle_state) {
863 * Add a barrier between reading the cycle flag and any
864 * reads of the event's flags/data below:
868 trace_xhci_dbc_handle_event(dbc->ring_evt, &evt->generic);
870 switch (le32_to_cpu(evt->event_cmd.flags) & TRB_TYPE_BITMASK) {
871 case TRB_TYPE(TRB_PORT_STATUS):
872 dbc_handle_port_status(dbc, evt);
874 case TRB_TYPE(TRB_TRANSFER):
875 dbc_handle_xfer_event(dbc, evt);
881 inc_evt_deq(dbc->ring_evt);
883 evt = dbc->ring_evt->dequeue;
887 /* Update event ring dequeue pointer: */
889 deq = xhci_trb_virt_to_dma(dbc->ring_evt->deq_seg,
890 dbc->ring_evt->dequeue);
891 lo_hi_writeq(deq, &dbc->regs->erdp);
897 static void xhci_dbc_handle_events(struct work_struct *work)
900 struct xhci_dbc *dbc;
903 dbc = container_of(to_delayed_work(work), struct xhci_dbc, event_work);
905 spin_lock_irqsave(&dbc->lock, flags);
906 evtr = xhci_dbc_do_handle_events(dbc);
907 spin_unlock_irqrestore(&dbc->lock, flags);
911 if (dbc->driver->configure)
912 dbc->driver->configure(dbc);
915 if (dbc->driver->disconnect)
916 dbc->driver->disconnect(dbc);
921 dev_info(dbc->dev, "stop handling dbc events\n");
925 mod_delayed_work(system_wq, &dbc->event_work, 1);
928 static const char * const dbc_state_strings[DS_MAX] = {
929 [DS_DISABLED] = "disabled",
930 [DS_INITIALIZED] = "initialized",
931 [DS_ENABLED] = "enabled",
932 [DS_CONNECTED] = "connected",
933 [DS_CONFIGURED] = "configured",
934 [DS_STALLED] = "stalled",
937 static ssize_t dbc_show(struct device *dev,
938 struct device_attribute *attr,
941 struct xhci_dbc *dbc;
942 struct xhci_hcd *xhci;
944 xhci = hcd_to_xhci(dev_get_drvdata(dev));
947 if (dbc->state >= ARRAY_SIZE(dbc_state_strings))
948 return sysfs_emit(buf, "unknown\n");
950 return sysfs_emit(buf, "%s\n", dbc_state_strings[dbc->state]);
953 static ssize_t dbc_store(struct device *dev,
954 struct device_attribute *attr,
955 const char *buf, size_t count)
957 struct xhci_hcd *xhci;
958 struct xhci_dbc *dbc;
960 xhci = hcd_to_xhci(dev_get_drvdata(dev));
963 if (sysfs_streq(buf, "enable"))
965 else if (sysfs_streq(buf, "disable"))
973 static ssize_t dbc_idVendor_show(struct device *dev,
974 struct device_attribute *attr,
977 struct xhci_dbc *dbc;
978 struct xhci_hcd *xhci;
980 xhci = hcd_to_xhci(dev_get_drvdata(dev));
983 return sysfs_emit(buf, "%04x\n", dbc->idVendor);
986 static ssize_t dbc_idVendor_store(struct device *dev,
987 struct device_attribute *attr,
988 const char *buf, size_t size)
990 struct xhci_dbc *dbc;
991 struct xhci_hcd *xhci;
997 ret = kstrtou16(buf, 0, &value);
1001 xhci = hcd_to_xhci(dev_get_drvdata(dev));
1003 if (dbc->state != DS_DISABLED)
1006 dbc->idVendor = value;
1007 ptr = &dbc->regs->devinfo1;
1008 dev_info = readl(ptr);
1009 dev_info = (dev_info & ~(0xffffu << 16)) | (value << 16);
1010 writel(dev_info, ptr);
1015 static ssize_t dbc_idProduct_show(struct device *dev,
1016 struct device_attribute *attr,
1019 struct xhci_dbc *dbc;
1020 struct xhci_hcd *xhci;
1022 xhci = hcd_to_xhci(dev_get_drvdata(dev));
1025 return sysfs_emit(buf, "%04x\n", dbc->idProduct);
1028 static ssize_t dbc_idProduct_store(struct device *dev,
1029 struct device_attribute *attr,
1030 const char *buf, size_t size)
1032 struct xhci_dbc *dbc;
1033 struct xhci_hcd *xhci;
1039 ret = kstrtou16(buf, 0, &value);
1043 xhci = hcd_to_xhci(dev_get_drvdata(dev));
1045 if (dbc->state != DS_DISABLED)
1048 dbc->idProduct = value;
1049 ptr = &dbc->regs->devinfo2;
1050 dev_info = readl(ptr);
1051 dev_info = (dev_info & ~(0xffffu)) | value;
1052 writel(dev_info, ptr);
1056 static ssize_t dbc_bcdDevice_show(struct device *dev,
1057 struct device_attribute *attr,
1060 struct xhci_dbc *dbc;
1061 struct xhci_hcd *xhci;
1063 xhci = hcd_to_xhci(dev_get_drvdata(dev));
1066 return sysfs_emit(buf, "%04x\n", dbc->bcdDevice);
1069 static ssize_t dbc_bcdDevice_store(struct device *dev,
1070 struct device_attribute *attr,
1071 const char *buf, size_t size)
1073 struct xhci_dbc *dbc;
1074 struct xhci_hcd *xhci;
1080 ret = kstrtou16(buf, 0, &value);
1084 xhci = hcd_to_xhci(dev_get_drvdata(dev));
1086 if (dbc->state != DS_DISABLED)
1089 dbc->bcdDevice = value;
1090 ptr = &dbc->regs->devinfo2;
1091 dev_info = readl(ptr);
1092 dev_info = (dev_info & ~(0xffffu << 16)) | (value << 16);
1093 writel(dev_info, ptr);
1098 static ssize_t dbc_bInterfaceProtocol_show(struct device *dev,
1099 struct device_attribute *attr,
1102 struct xhci_dbc *dbc;
1103 struct xhci_hcd *xhci;
1105 xhci = hcd_to_xhci(dev_get_drvdata(dev));
1108 return sysfs_emit(buf, "%02x\n", dbc->bInterfaceProtocol);
1111 static ssize_t dbc_bInterfaceProtocol_store(struct device *dev,
1112 struct device_attribute *attr,
1113 const char *buf, size_t size)
1115 struct xhci_dbc *dbc;
1116 struct xhci_hcd *xhci;
1122 /* bInterfaceProtocol is 8 bit, but... */
1123 ret = kstrtou8(buf, 0, &value);
1127 /* ...xhci only supports values 0 and 1 */
1131 xhci = hcd_to_xhci(dev_get_drvdata(dev));
1133 if (dbc->state != DS_DISABLED)
1136 dbc->bInterfaceProtocol = value;
1137 ptr = &dbc->regs->devinfo1;
1138 dev_info = readl(ptr);
1139 dev_info = (dev_info & ~(0xffu)) | value;
1140 writel(dev_info, ptr);
1145 static DEVICE_ATTR_RW(dbc);
1146 static DEVICE_ATTR_RW(dbc_idVendor);
1147 static DEVICE_ATTR_RW(dbc_idProduct);
1148 static DEVICE_ATTR_RW(dbc_bcdDevice);
1149 static DEVICE_ATTR_RW(dbc_bInterfaceProtocol);
1151 static struct attribute *dbc_dev_attrs[] = {
1153 &dev_attr_dbc_idVendor.attr,
1154 &dev_attr_dbc_idProduct.attr,
1155 &dev_attr_dbc_bcdDevice.attr,
1156 &dev_attr_dbc_bInterfaceProtocol.attr,
1159 ATTRIBUTE_GROUPS(dbc_dev);
1162 xhci_alloc_dbc(struct device *dev, void __iomem *base, const struct dbc_driver *driver)
1164 struct xhci_dbc *dbc;
1167 dbc = kzalloc(sizeof(*dbc), GFP_KERNEL);
1173 dbc->driver = driver;
1174 dbc->idProduct = DBC_PRODUCT_ID;
1175 dbc->idVendor = DBC_VENDOR_ID;
1176 dbc->bcdDevice = DBC_DEVICE_REV;
1177 dbc->bInterfaceProtocol = DBC_PROTOCOL;
1179 if (readl(&dbc->regs->control) & DBC_CTRL_DBC_ENABLE)
1182 INIT_DELAYED_WORK(&dbc->event_work, xhci_dbc_handle_events);
1183 spin_lock_init(&dbc->lock);
1185 ret = sysfs_create_groups(&dev->kobj, dbc_dev_groups);
1195 /* undo what xhci_alloc_dbc() did */
1196 void xhci_dbc_remove(struct xhci_dbc *dbc)
1200 /* stop hw, stop wq and call dbc->ops->stop() */
1203 /* remove sysfs files */
1204 sysfs_remove_groups(&dbc->dev->kobj, dbc_dev_groups);
1210 int xhci_create_dbc_dev(struct xhci_hcd *xhci)
1217 /* create all parameters needed resembling a dbc device */
1218 dev = xhci_to_hcd(xhci)->self.controller;
1219 base = &xhci->cap_regs->hc_capbase;
1221 dbc_cap_offs = xhci_find_next_ext_cap(base, 0, XHCI_EXT_CAPS_DEBUG);
1225 /* already allocated and in use */
1229 ret = xhci_dbc_tty_probe(dev, base + dbc_cap_offs, xhci);
1234 void xhci_remove_dbc_dev(struct xhci_hcd *xhci)
1236 unsigned long flags;
1241 xhci_dbc_tty_remove(xhci->dbc);
1242 spin_lock_irqsave(&xhci->lock, flags);
1244 spin_unlock_irqrestore(&xhci->lock, flags);
1248 int xhci_dbc_suspend(struct xhci_hcd *xhci)
1250 struct xhci_dbc *dbc = xhci->dbc;
1255 if (dbc->state == DS_CONFIGURED)
1256 dbc->resume_required = 1;
1263 int xhci_dbc_resume(struct xhci_hcd *xhci)
1266 struct xhci_dbc *dbc = xhci->dbc;
1271 if (dbc->resume_required) {
1272 dbc->resume_required = 0;
1273 xhci_dbc_start(dbc);
1278 #endif /* CONFIG_PM */
1280 int xhci_dbc_init(void)
1282 return dbc_tty_init();
1285 void xhci_dbc_exit(void)