1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * xhci-dbgcap.c - xHCI debug capability support
4 *
5 * Copyright (C) 2017 Intel Corporation
6 *
7 * Author: Lu Baolu <baolu.lu@linux.intel.com>
8 */
9 #include <linux/bug.h>
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>
23
24 #include <linux/io-64-nonatomic-lo-hi.h>
25
26 #include <asm/byteorder.h>
27
28 #include "xhci.h"
29 #include "xhci-trace.h"
30 #include "xhci-dbgcap.h"
31
dbc_free_ctx(struct device * dev,struct xhci_container_ctx * ctx)32 static void dbc_free_ctx(struct device *dev, struct xhci_container_ctx *ctx)
33 {
34 if (!ctx)
35 return;
36 dma_free_coherent(dev, ctx->size, ctx->bytes, ctx->dma);
37 kfree(ctx);
38 }
39
40 /* we use only one segment for DbC rings */
dbc_ring_free(struct device * dev,struct xhci_ring * ring)41 static void dbc_ring_free(struct device *dev, struct xhci_ring *ring)
42 {
43 if (!ring)
44 return;
45
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);
51 }
52 kfree(ring);
53 }
54
xhci_dbc_populate_strings(struct dbc_str_descs * strings)55 static u32 xhci_dbc_populate_strings(struct dbc_str_descs *strings)
56 {
57 struct usb_string_descriptor *s_desc;
58 u32 string_length;
59
60 /* Serial string: */
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);
65
66 s_desc->bLength = (strlen(DBC_STRING_SERIAL) + 1) * 2;
67 s_desc->bDescriptorType = USB_DT_STRING;
68 string_length = s_desc->bLength;
69 string_length <<= 8;
70
71 /* Product string: */
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);
76
77 s_desc->bLength = (strlen(DBC_STRING_PRODUCT) + 1) * 2;
78 s_desc->bDescriptorType = USB_DT_STRING;
79 string_length += s_desc->bLength;
80 string_length <<= 8;
81
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);
88
89 s_desc->bLength = (strlen(DBC_STRING_MANUFACTURER) + 1) * 2;
90 s_desc->bDescriptorType = USB_DT_STRING;
91 string_length += s_desc->bLength;
92 string_length <<= 8;
93
94 /* String0: */
95 strings->string0[0] = 4;
96 strings->string0[1] = USB_DT_STRING;
97 strings->string0[2] = 0x09;
98 strings->string0[3] = 0x04;
99 string_length += 4;
100
101 return string_length;
102 }
103
xhci_dbc_init_contexts(struct xhci_dbc * dbc,u32 string_length)104 static void xhci_dbc_init_contexts(struct xhci_dbc *dbc, u32 string_length)
105 {
106 struct dbc_info_context *info;
107 struct xhci_ep_ctx *ep_ctx;
108 u32 dev_info;
109 dma_addr_t deq, dma;
110 unsigned int max_burst;
111
112 if (!dbc)
113 return;
114
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);
123
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);
128 ep_ctx->ep_info = 0;
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);
131
132 /* Populate bulk in endpoint context: */
133 ep_ctx = dbc_bulkin_ctx(dbc);
134 deq = dbc_bulkin_enq(dbc);
135 ep_ctx->ep_info = 0;
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);
138
139 /* Set DbC context and info registers: */
140 lo_hi_writeq(dbc->ctx->dma, &dbc->regs->dccp);
141
142 dev_info = (dbc->idVendor << 16) | dbc->bInterfaceProtocol;
143 writel(dev_info, &dbc->regs->devinfo1);
144
145 dev_info = (dbc->bcdDevice << 16) | dbc->idProduct;
146 writel(dev_info, &dbc->regs->devinfo2);
147 }
148
xhci_dbc_giveback(struct dbc_request * req,int status)149 static void xhci_dbc_giveback(struct dbc_request *req, int status)
150 __releases(&dbc->lock)
151 __acquires(&dbc->lock)
152 {
153 struct xhci_dbc *dbc = req->dbc;
154 struct device *dev = dbc->dev;
155
156 list_del_init(&req->list_pending);
157 req->trb_dma = 0;
158 req->trb = NULL;
159
160 if (req->status == -EINPROGRESS)
161 req->status = status;
162
163 trace_xhci_dbc_giveback_request(req);
164
165 dma_unmap_single(dev,
166 req->dma,
167 req->length,
168 dbc_ep_dma_direction(req));
169
170 /* Give back the transfer request: */
171 spin_unlock(&dbc->lock);
172 req->complete(dbc, req);
173 spin_lock(&dbc->lock);
174 }
175
trb_to_noop(union xhci_trb * trb)176 static void trb_to_noop(union xhci_trb *trb)
177 {
178 trb->generic.field[0] = 0;
179 trb->generic.field[1] = 0;
180 trb->generic.field[2] = 0;
181 trb->generic.field[3] &= cpu_to_le32(TRB_CYCLE);
182 trb->generic.field[3] |= cpu_to_le32(TRB_TYPE(TRB_TR_NOOP));
183 }
184
xhci_dbc_flush_single_request(struct dbc_request * req)185 static void xhci_dbc_flush_single_request(struct dbc_request *req)
186 {
187 trb_to_noop(req->trb);
188 xhci_dbc_giveback(req, -ESHUTDOWN);
189 }
190
xhci_dbc_flush_endpoint_requests(struct dbc_ep * dep)191 static void xhci_dbc_flush_endpoint_requests(struct dbc_ep *dep)
192 {
193 struct dbc_request *req, *tmp;
194
195 list_for_each_entry_safe(req, tmp, &dep->list_pending, list_pending)
196 xhci_dbc_flush_single_request(req);
197 }
198
xhci_dbc_flush_requests(struct xhci_dbc * dbc)199 static void xhci_dbc_flush_requests(struct xhci_dbc *dbc)
200 {
201 xhci_dbc_flush_endpoint_requests(&dbc->eps[BULK_OUT]);
202 xhci_dbc_flush_endpoint_requests(&dbc->eps[BULK_IN]);
203 }
204
205 struct dbc_request *
dbc_alloc_request(struct xhci_dbc * dbc,unsigned int direction,gfp_t flags)206 dbc_alloc_request(struct xhci_dbc *dbc, unsigned int direction, gfp_t flags)
207 {
208 struct dbc_request *req;
209
210 if (direction != BULK_IN &&
211 direction != BULK_OUT)
212 return NULL;
213
214 if (!dbc)
215 return NULL;
216
217 req = kzalloc(sizeof(*req), flags);
218 if (!req)
219 return NULL;
220
221 req->dbc = dbc;
222 INIT_LIST_HEAD(&req->list_pending);
223 INIT_LIST_HEAD(&req->list_pool);
224 req->direction = direction;
225
226 trace_xhci_dbc_alloc_request(req);
227
228 return req;
229 }
230
231 void
dbc_free_request(struct dbc_request * req)232 dbc_free_request(struct dbc_request *req)
233 {
234 trace_xhci_dbc_free_request(req);
235
236 kfree(req);
237 }
238
239 static void
xhci_dbc_queue_trb(struct xhci_ring * ring,u32 field1,u32 field2,u32 field3,u32 field4)240 xhci_dbc_queue_trb(struct xhci_ring *ring, u32 field1,
241 u32 field2, u32 field3, u32 field4)
242 {
243 union xhci_trb *trb, *next;
244
245 trb = ring->enqueue;
246 trb->generic.field[0] = cpu_to_le32(field1);
247 trb->generic.field[1] = cpu_to_le32(field2);
248 trb->generic.field[2] = cpu_to_le32(field3);
249 trb->generic.field[3] = cpu_to_le32(field4);
250
251 trace_xhci_dbc_gadget_ep_queue(ring, &trb->generic,
252 xhci_trb_virt_to_dma(ring->enq_seg,
253 ring->enqueue));
254 ring->num_trbs_free--;
255 next = ++(ring->enqueue);
256 if (TRB_TYPE_LINK_LE32(next->link.control)) {
257 next->link.control ^= cpu_to_le32(TRB_CYCLE);
258 ring->enqueue = ring->enq_seg->trbs;
259 ring->cycle_state ^= 1;
260 }
261 }
262
xhci_dbc_queue_bulk_tx(struct dbc_ep * dep,struct dbc_request * req)263 static int xhci_dbc_queue_bulk_tx(struct dbc_ep *dep,
264 struct dbc_request *req)
265 {
266 u64 addr;
267 union xhci_trb *trb;
268 unsigned int num_trbs;
269 struct xhci_dbc *dbc = req->dbc;
270 struct xhci_ring *ring = dep->ring;
271 u32 length, control, cycle;
272
273 num_trbs = count_trbs(req->dma, req->length);
274 WARN_ON(num_trbs != 1);
275 if (ring->num_trbs_free < num_trbs)
276 return -EBUSY;
277
278 addr = req->dma;
279 trb = ring->enqueue;
280 cycle = ring->cycle_state;
281 length = TRB_LEN(req->length);
282 control = TRB_TYPE(TRB_NORMAL) | TRB_IOC;
283
284 if (cycle)
285 control &= cpu_to_le32(~TRB_CYCLE);
286 else
287 control |= cpu_to_le32(TRB_CYCLE);
288
289 req->trb = ring->enqueue;
290 req->trb_dma = xhci_trb_virt_to_dma(ring->enq_seg, ring->enqueue);
291 xhci_dbc_queue_trb(ring,
292 lower_32_bits(addr),
293 upper_32_bits(addr),
294 length, control);
295
296 /*
297 * Add a barrier between writes of trb fields and flipping
298 * the cycle bit:
299 */
300 wmb();
301
302 if (cycle)
303 trb->generic.field[3] |= cpu_to_le32(TRB_CYCLE);
304 else
305 trb->generic.field[3] &= cpu_to_le32(~TRB_CYCLE);
306
307 writel(DBC_DOOR_BELL_TARGET(dep->direction), &dbc->regs->doorbell);
308
309 return 0;
310 }
311
312 static int
dbc_ep_do_queue(struct dbc_request * req)313 dbc_ep_do_queue(struct dbc_request *req)
314 {
315 int ret;
316 struct xhci_dbc *dbc = req->dbc;
317 struct device *dev = dbc->dev;
318 struct dbc_ep *dep = &dbc->eps[req->direction];
319
320 if (!req->length || !req->buf)
321 return -EINVAL;
322
323 req->actual = 0;
324 req->status = -EINPROGRESS;
325
326 req->dma = dma_map_single(dev,
327 req->buf,
328 req->length,
329 dbc_ep_dma_direction(dep));
330 if (dma_mapping_error(dev, req->dma)) {
331 dev_err(dbc->dev, "failed to map buffer\n");
332 return -EFAULT;
333 }
334
335 ret = xhci_dbc_queue_bulk_tx(dep, req);
336 if (ret) {
337 dev_err(dbc->dev, "failed to queue trbs\n");
338 dma_unmap_single(dev,
339 req->dma,
340 req->length,
341 dbc_ep_dma_direction(dep));
342 return -EFAULT;
343 }
344
345 list_add_tail(&req->list_pending, &dep->list_pending);
346
347 return 0;
348 }
349
dbc_ep_queue(struct dbc_request * req)350 int dbc_ep_queue(struct dbc_request *req)
351 {
352 unsigned long flags;
353 struct xhci_dbc *dbc = req->dbc;
354 int ret = -ESHUTDOWN;
355
356 if (!dbc)
357 return -ENODEV;
358
359 if (req->direction != BULK_IN &&
360 req->direction != BULK_OUT)
361 return -EINVAL;
362
363 spin_lock_irqsave(&dbc->lock, flags);
364 if (dbc->state == DS_CONFIGURED)
365 ret = dbc_ep_do_queue(req);
366 spin_unlock_irqrestore(&dbc->lock, flags);
367
368 mod_delayed_work(system_wq, &dbc->event_work, 0);
369
370 trace_xhci_dbc_queue_request(req);
371
372 return ret;
373 }
374
xhci_dbc_do_eps_init(struct xhci_dbc * dbc,bool direction)375 static inline void xhci_dbc_do_eps_init(struct xhci_dbc *dbc, bool direction)
376 {
377 struct dbc_ep *dep;
378
379 dep = &dbc->eps[direction];
380 dep->dbc = dbc;
381 dep->direction = direction;
382 dep->ring = direction ? dbc->ring_in : dbc->ring_out;
383
384 INIT_LIST_HEAD(&dep->list_pending);
385 }
386
xhci_dbc_eps_init(struct xhci_dbc * dbc)387 static void xhci_dbc_eps_init(struct xhci_dbc *dbc)
388 {
389 xhci_dbc_do_eps_init(dbc, BULK_OUT);
390 xhci_dbc_do_eps_init(dbc, BULK_IN);
391 }
392
xhci_dbc_eps_exit(struct xhci_dbc * dbc)393 static void xhci_dbc_eps_exit(struct xhci_dbc *dbc)
394 {
395 memset(dbc->eps, 0, sizeof_field(struct xhci_dbc, eps));
396 }
397
dbc_erst_alloc(struct device * dev,struct xhci_ring * evt_ring,struct xhci_erst * erst,gfp_t flags)398 static int dbc_erst_alloc(struct device *dev, struct xhci_ring *evt_ring,
399 struct xhci_erst *erst, gfp_t flags)
400 {
401 erst->entries = dma_alloc_coherent(dev, sizeof(*erst->entries),
402 &erst->erst_dma_addr, flags);
403 if (!erst->entries)
404 return -ENOMEM;
405
406 erst->num_entries = 1;
407 erst->entries[0].seg_addr = cpu_to_le64(evt_ring->first_seg->dma);
408 erst->entries[0].seg_size = cpu_to_le32(TRBS_PER_SEGMENT);
409 erst->entries[0].rsvd = 0;
410 return 0;
411 }
412
dbc_erst_free(struct device * dev,struct xhci_erst * erst)413 static void dbc_erst_free(struct device *dev, struct xhci_erst *erst)
414 {
415 dma_free_coherent(dev, sizeof(*erst->entries), erst->entries,
416 erst->erst_dma_addr);
417 erst->entries = NULL;
418 }
419
420 static struct xhci_container_ctx *
dbc_alloc_ctx(struct device * dev,gfp_t flags)421 dbc_alloc_ctx(struct device *dev, gfp_t flags)
422 {
423 struct xhci_container_ctx *ctx;
424
425 ctx = kzalloc(sizeof(*ctx), flags);
426 if (!ctx)
427 return NULL;
428
429 /* xhci 7.6.9, all three contexts; info, ep-out and ep-in. Each 64 bytes*/
430 ctx->size = 3 * DBC_CONTEXT_SIZE;
431 ctx->bytes = dma_alloc_coherent(dev, ctx->size, &ctx->dma, flags);
432 if (!ctx->bytes) {
433 kfree(ctx);
434 return NULL;
435 }
436 return ctx;
437 }
438
439 static struct xhci_ring *
xhci_dbc_ring_alloc(struct device * dev,enum xhci_ring_type type,gfp_t flags)440 xhci_dbc_ring_alloc(struct device *dev, enum xhci_ring_type type, gfp_t flags)
441 {
442 struct xhci_ring *ring;
443 struct xhci_segment *seg;
444 dma_addr_t dma;
445
446 ring = kzalloc(sizeof(*ring), flags);
447 if (!ring)
448 return NULL;
449
450 ring->num_segs = 1;
451 ring->type = type;
452
453 seg = kzalloc(sizeof(*seg), flags);
454 if (!seg)
455 goto seg_fail;
456
457 ring->first_seg = seg;
458 ring->last_seg = seg;
459 seg->next = seg;
460
461 seg->trbs = dma_alloc_coherent(dev, TRB_SEGMENT_SIZE, &dma, flags);
462 if (!seg->trbs)
463 goto dma_fail;
464
465 seg->dma = dma;
466
467 /* Only event ring does not use link TRB */
468 if (type != TYPE_EVENT) {
469 union xhci_trb *trb = &seg->trbs[TRBS_PER_SEGMENT - 1];
470
471 trb->link.segment_ptr = cpu_to_le64(dma);
472 trb->link.control = cpu_to_le32(LINK_TOGGLE | TRB_TYPE(TRB_LINK));
473 }
474 INIT_LIST_HEAD(&ring->td_list);
475 xhci_initialize_ring_info(ring);
476 return ring;
477 dma_fail:
478 kfree(seg);
479 seg_fail:
480 kfree(ring);
481 return NULL;
482 }
483
xhci_dbc_mem_init(struct xhci_dbc * dbc,gfp_t flags)484 static int xhci_dbc_mem_init(struct xhci_dbc *dbc, gfp_t flags)
485 {
486 int ret;
487 dma_addr_t deq;
488 u32 string_length;
489 struct device *dev = dbc->dev;
490
491 /* Allocate various rings for events and transfers: */
492 dbc->ring_evt = xhci_dbc_ring_alloc(dev, TYPE_EVENT, flags);
493 if (!dbc->ring_evt)
494 goto evt_fail;
495
496 dbc->ring_in = xhci_dbc_ring_alloc(dev, TYPE_BULK, flags);
497 if (!dbc->ring_in)
498 goto in_fail;
499
500 dbc->ring_out = xhci_dbc_ring_alloc(dev, TYPE_BULK, flags);
501 if (!dbc->ring_out)
502 goto out_fail;
503
504 /* Allocate and populate ERST: */
505 ret = dbc_erst_alloc(dev, dbc->ring_evt, &dbc->erst, flags);
506 if (ret)
507 goto erst_fail;
508
509 /* Allocate context data structure: */
510 dbc->ctx = dbc_alloc_ctx(dev, flags); /* was sysdev, and is still */
511 if (!dbc->ctx)
512 goto ctx_fail;
513
514 /* Allocate the string table: */
515 dbc->string_size = sizeof(*dbc->string);
516 dbc->string = dma_alloc_coherent(dev, dbc->string_size,
517 &dbc->string_dma, flags);
518 if (!dbc->string)
519 goto string_fail;
520
521 /* Setup ERST register: */
522 writel(dbc->erst.num_entries, &dbc->regs->ersts);
523
524 lo_hi_writeq(dbc->erst.erst_dma_addr, &dbc->regs->erstba);
525 deq = xhci_trb_virt_to_dma(dbc->ring_evt->deq_seg,
526 dbc->ring_evt->dequeue);
527 lo_hi_writeq(deq, &dbc->regs->erdp);
528
529 /* Setup strings and contexts: */
530 string_length = xhci_dbc_populate_strings(dbc->string);
531 xhci_dbc_init_contexts(dbc, string_length);
532
533 xhci_dbc_eps_init(dbc);
534 dbc->state = DS_INITIALIZED;
535
536 return 0;
537
538 string_fail:
539 dbc_free_ctx(dev, dbc->ctx);
540 dbc->ctx = NULL;
541 ctx_fail:
542 dbc_erst_free(dev, &dbc->erst);
543 erst_fail:
544 dbc_ring_free(dev, dbc->ring_out);
545 dbc->ring_out = NULL;
546 out_fail:
547 dbc_ring_free(dev, dbc->ring_in);
548 dbc->ring_in = NULL;
549 in_fail:
550 dbc_ring_free(dev, dbc->ring_evt);
551 dbc->ring_evt = NULL;
552 evt_fail:
553 return -ENOMEM;
554 }
555
xhci_dbc_mem_cleanup(struct xhci_dbc * dbc)556 static void xhci_dbc_mem_cleanup(struct xhci_dbc *dbc)
557 {
558 if (!dbc)
559 return;
560
561 xhci_dbc_eps_exit(dbc);
562
563 dma_free_coherent(dbc->dev, dbc->string_size, dbc->string, dbc->string_dma);
564 dbc->string = NULL;
565
566 dbc_free_ctx(dbc->dev, dbc->ctx);
567 dbc->ctx = NULL;
568
569 dbc_erst_free(dbc->dev, &dbc->erst);
570 dbc_ring_free(dbc->dev, dbc->ring_out);
571 dbc_ring_free(dbc->dev, dbc->ring_in);
572 dbc_ring_free(dbc->dev, dbc->ring_evt);
573 dbc->ring_in = NULL;
574 dbc->ring_out = NULL;
575 dbc->ring_evt = NULL;
576 }
577
xhci_do_dbc_start(struct xhci_dbc * dbc)578 static int xhci_do_dbc_start(struct xhci_dbc *dbc)
579 {
580 int ret;
581 u32 ctrl;
582
583 if (dbc->state != DS_DISABLED)
584 return -EINVAL;
585
586 writel(0, &dbc->regs->control);
587 ret = xhci_handshake(&dbc->regs->control,
588 DBC_CTRL_DBC_ENABLE,
589 0, 1000);
590 if (ret)
591 return ret;
592
593 ret = xhci_dbc_mem_init(dbc, GFP_ATOMIC);
594 if (ret)
595 return ret;
596
597 ctrl = readl(&dbc->regs->control);
598 writel(ctrl | DBC_CTRL_DBC_ENABLE | DBC_CTRL_PORT_ENABLE,
599 &dbc->regs->control);
600 ret = xhci_handshake(&dbc->regs->control,
601 DBC_CTRL_DBC_ENABLE,
602 DBC_CTRL_DBC_ENABLE, 1000);
603 if (ret)
604 return ret;
605
606 dbc->state = DS_ENABLED;
607
608 return 0;
609 }
610
xhci_do_dbc_stop(struct xhci_dbc * dbc)611 static int xhci_do_dbc_stop(struct xhci_dbc *dbc)
612 {
613 if (dbc->state == DS_DISABLED)
614 return -EINVAL;
615
616 writel(0, &dbc->regs->control);
617 dbc->state = DS_DISABLED;
618
619 return 0;
620 }
621
xhci_dbc_start(struct xhci_dbc * dbc)622 static int xhci_dbc_start(struct xhci_dbc *dbc)
623 {
624 int ret;
625 unsigned long flags;
626
627 WARN_ON(!dbc);
628
629 pm_runtime_get_sync(dbc->dev); /* note this was self.controller */
630
631 spin_lock_irqsave(&dbc->lock, flags);
632 ret = xhci_do_dbc_start(dbc);
633 spin_unlock_irqrestore(&dbc->lock, flags);
634
635 if (ret) {
636 pm_runtime_put(dbc->dev); /* note this was self.controller */
637 return ret;
638 }
639
640 return mod_delayed_work(system_wq, &dbc->event_work,
641 msecs_to_jiffies(dbc->poll_interval));
642 }
643
xhci_dbc_stop(struct xhci_dbc * dbc)644 static void xhci_dbc_stop(struct xhci_dbc *dbc)
645 {
646 int ret;
647 unsigned long flags;
648
649 WARN_ON(!dbc);
650
651 switch (dbc->state) {
652 case DS_DISABLED:
653 return;
654 case DS_CONFIGURED:
655 if (dbc->driver->disconnect)
656 dbc->driver->disconnect(dbc);
657 break;
658 default:
659 break;
660 }
661
662 cancel_delayed_work_sync(&dbc->event_work);
663
664 spin_lock_irqsave(&dbc->lock, flags);
665 ret = xhci_do_dbc_stop(dbc);
666 spin_unlock_irqrestore(&dbc->lock, flags);
667 if (ret)
668 return;
669
670 xhci_dbc_mem_cleanup(dbc);
671 pm_runtime_put_sync(dbc->dev); /* note, was self.controller */
672 }
673
674 static void
handle_ep_halt_changes(struct xhci_dbc * dbc,struct dbc_ep * dep,bool halted)675 handle_ep_halt_changes(struct xhci_dbc *dbc, struct dbc_ep *dep, bool halted)
676 {
677 if (halted) {
678 dev_info(dbc->dev, "DbC Endpoint halted\n");
679 dep->halted = 1;
680
681 } else if (dep->halted) {
682 dev_info(dbc->dev, "DbC Endpoint halt cleared\n");
683 dep->halted = 0;
684
685 if (!list_empty(&dep->list_pending))
686 writel(DBC_DOOR_BELL_TARGET(dep->direction),
687 &dbc->regs->doorbell);
688 }
689 }
690
691 static void
dbc_handle_port_status(struct xhci_dbc * dbc,union xhci_trb * event)692 dbc_handle_port_status(struct xhci_dbc *dbc, union xhci_trb *event)
693 {
694 u32 portsc;
695
696 portsc = readl(&dbc->regs->portsc);
697 if (portsc & DBC_PORTSC_CONN_CHANGE)
698 dev_info(dbc->dev, "DbC port connect change\n");
699
700 if (portsc & DBC_PORTSC_RESET_CHANGE)
701 dev_info(dbc->dev, "DbC port reset change\n");
702
703 if (portsc & DBC_PORTSC_LINK_CHANGE)
704 dev_info(dbc->dev, "DbC port link status change\n");
705
706 if (portsc & DBC_PORTSC_CONFIG_CHANGE)
707 dev_info(dbc->dev, "DbC config error change\n");
708
709 /* Port reset change bit will be cleared in other place: */
710 writel(portsc & ~DBC_PORTSC_RESET_CHANGE, &dbc->regs->portsc);
711 }
712
dbc_handle_xfer_event(struct xhci_dbc * dbc,union xhci_trb * event)713 static void dbc_handle_xfer_event(struct xhci_dbc *dbc, union xhci_trb *event)
714 {
715 struct dbc_ep *dep;
716 struct xhci_ring *ring;
717 int ep_id;
718 int status;
719 struct xhci_ep_ctx *ep_ctx;
720 u32 comp_code;
721 size_t remain_length;
722 struct dbc_request *req = NULL, *r;
723
724 comp_code = GET_COMP_CODE(le32_to_cpu(event->generic.field[2]));
725 remain_length = EVENT_TRB_LEN(le32_to_cpu(event->generic.field[2]));
726 ep_id = TRB_TO_EP_ID(le32_to_cpu(event->generic.field[3]));
727 dep = (ep_id == EPID_OUT) ?
728 get_out_ep(dbc) : get_in_ep(dbc);
729 ep_ctx = (ep_id == EPID_OUT) ?
730 dbc_bulkout_ctx(dbc) : dbc_bulkin_ctx(dbc);
731 ring = dep->ring;
732
733 /* Match the pending request: */
734 list_for_each_entry(r, &dep->list_pending, list_pending) {
735 if (r->trb_dma == event->trans_event.buffer) {
736 req = r;
737 break;
738 }
739 if (r->status == -COMP_STALL_ERROR) {
740 dev_warn(dbc->dev, "Give back stale stalled req\n");
741 ring->num_trbs_free++;
742 xhci_dbc_giveback(r, 0);
743 }
744 }
745
746 if (!req) {
747 dev_warn(dbc->dev, "no matched request\n");
748 return;
749 }
750
751 trace_xhci_dbc_handle_transfer(ring, &req->trb->generic, req->trb_dma);
752
753 switch (comp_code) {
754 case COMP_SUCCESS:
755 remain_length = 0;
756 fallthrough;
757 case COMP_SHORT_PACKET:
758 status = 0;
759 break;
760 case COMP_TRB_ERROR:
761 case COMP_BABBLE_DETECTED_ERROR:
762 case COMP_USB_TRANSACTION_ERROR:
763 dev_warn(dbc->dev, "tx error %d detected\n", comp_code);
764 status = -comp_code;
765 break;
766 case COMP_STALL_ERROR:
767 dev_warn(dbc->dev, "Stall error at bulk TRB %llx, remaining %zu, ep deq %llx\n",
768 event->trans_event.buffer, remain_length, ep_ctx->deq);
769 status = 0;
770 dep->halted = 1;
771
772 /*
773 * xHC DbC may trigger a STALL bulk xfer event when host sends a
774 * ClearFeature(ENDPOINT_HALT) request even if there wasn't an
775 * active bulk transfer.
776 *
777 * Don't give back this transfer request as hardware will later
778 * start processing TRBs starting from this 'STALLED' TRB,
779 * causing TRBs and requests to be out of sync.
780 *
781 * If STALL event shows some bytes were transferred then assume
782 * it's an actual transfer issue and give back the request.
783 * In this case mark the TRB as No-Op to avoid hw from using the
784 * TRB again.
785 */
786
787 if ((ep_ctx->deq & ~TRB_CYCLE) == event->trans_event.buffer) {
788 dev_dbg(dbc->dev, "Ep stopped on Stalled TRB\n");
789 if (remain_length == req->length) {
790 dev_dbg(dbc->dev, "Spurious stall event, keep req\n");
791 req->status = -COMP_STALL_ERROR;
792 req->actual = 0;
793 return;
794 }
795 dev_dbg(dbc->dev, "Give back stalled req, but turn TRB to No-op\n");
796 trb_to_noop(req->trb);
797 }
798 break;
799
800 default:
801 dev_err(dbc->dev, "unknown tx error %d\n", comp_code);
802 status = -comp_code;
803 break;
804 }
805
806 ring->num_trbs_free++;
807 req->actual = req->length - remain_length;
808 xhci_dbc_giveback(req, status);
809 }
810
inc_evt_deq(struct xhci_ring * ring)811 static void inc_evt_deq(struct xhci_ring *ring)
812 {
813 /* If on the last TRB of the segment go back to the beginning */
814 if (ring->dequeue == &ring->deq_seg->trbs[TRBS_PER_SEGMENT - 1]) {
815 ring->cycle_state ^= 1;
816 ring->dequeue = ring->deq_seg->trbs;
817 return;
818 }
819 ring->dequeue++;
820 }
821
xhci_dbc_do_handle_events(struct xhci_dbc * dbc)822 static enum evtreturn xhci_dbc_do_handle_events(struct xhci_dbc *dbc)
823 {
824 dma_addr_t deq;
825 union xhci_trb *evt;
826 enum evtreturn ret = EVT_DONE;
827 u32 ctrl, portsc;
828 bool update_erdp = false;
829
830 /* DbC state machine: */
831 switch (dbc->state) {
832 case DS_DISABLED:
833 case DS_INITIALIZED:
834
835 return EVT_ERR;
836 case DS_ENABLED:
837 portsc = readl(&dbc->regs->portsc);
838 if (portsc & DBC_PORTSC_CONN_STATUS) {
839 dbc->state = DS_CONNECTED;
840 dev_info(dbc->dev, "DbC connected\n");
841 }
842
843 return EVT_DONE;
844 case DS_CONNECTED:
845 ctrl = readl(&dbc->regs->control);
846 if (ctrl & DBC_CTRL_DBC_RUN) {
847 dbc->state = DS_CONFIGURED;
848 dev_info(dbc->dev, "DbC configured\n");
849 portsc = readl(&dbc->regs->portsc);
850 writel(portsc, &dbc->regs->portsc);
851 return EVT_GSER;
852 }
853
854 return EVT_DONE;
855 case DS_CONFIGURED:
856 /* Handle cable unplug event: */
857 portsc = readl(&dbc->regs->portsc);
858 if (!(portsc & DBC_PORTSC_PORT_ENABLED) &&
859 !(portsc & DBC_PORTSC_CONN_STATUS)) {
860 dev_info(dbc->dev, "DbC cable unplugged\n");
861 dbc->state = DS_ENABLED;
862 xhci_dbc_flush_requests(dbc);
863
864 return EVT_DISC;
865 }
866
867 /* Handle debug port reset event: */
868 if (portsc & DBC_PORTSC_RESET_CHANGE) {
869 dev_info(dbc->dev, "DbC port reset\n");
870 writel(portsc, &dbc->regs->portsc);
871 dbc->state = DS_ENABLED;
872 xhci_dbc_flush_requests(dbc);
873
874 return EVT_DISC;
875 }
876
877 /* Check and handle changes in endpoint halt status */
878 ctrl = readl(&dbc->regs->control);
879 handle_ep_halt_changes(dbc, get_in_ep(dbc), ctrl & DBC_CTRL_HALT_IN_TR);
880 handle_ep_halt_changes(dbc, get_out_ep(dbc), ctrl & DBC_CTRL_HALT_OUT_TR);
881
882 /* Clear DbC run change bit: */
883 if (ctrl & DBC_CTRL_DBC_RUN_CHANGE) {
884 writel(ctrl, &dbc->regs->control);
885 ctrl = readl(&dbc->regs->control);
886 }
887 break;
888 default:
889 dev_err(dbc->dev, "Unknown DbC state %d\n", dbc->state);
890 break;
891 }
892
893 /* Handle the events in the event ring: */
894 evt = dbc->ring_evt->dequeue;
895 while ((le32_to_cpu(evt->event_cmd.flags) & TRB_CYCLE) ==
896 dbc->ring_evt->cycle_state) {
897 /*
898 * Add a barrier between reading the cycle flag and any
899 * reads of the event's flags/data below:
900 */
901 rmb();
902
903 trace_xhci_dbc_handle_event(dbc->ring_evt, &evt->generic,
904 xhci_trb_virt_to_dma(dbc->ring_evt->deq_seg,
905 dbc->ring_evt->dequeue));
906
907 switch (le32_to_cpu(evt->event_cmd.flags) & TRB_TYPE_BITMASK) {
908 case TRB_TYPE(TRB_PORT_STATUS):
909 dbc_handle_port_status(dbc, evt);
910 break;
911 case TRB_TYPE(TRB_TRANSFER):
912 dbc_handle_xfer_event(dbc, evt);
913 ret = EVT_XFER_DONE;
914 break;
915 default:
916 break;
917 }
918
919 inc_evt_deq(dbc->ring_evt);
920
921 evt = dbc->ring_evt->dequeue;
922 update_erdp = true;
923 }
924
925 /* Update event ring dequeue pointer: */
926 if (update_erdp) {
927 deq = xhci_trb_virt_to_dma(dbc->ring_evt->deq_seg,
928 dbc->ring_evt->dequeue);
929 lo_hi_writeq(deq, &dbc->regs->erdp);
930 }
931
932 return ret;
933 }
934
xhci_dbc_handle_events(struct work_struct * work)935 static void xhci_dbc_handle_events(struct work_struct *work)
936 {
937 enum evtreturn evtr;
938 struct xhci_dbc *dbc;
939 unsigned long flags;
940 unsigned int poll_interval;
941 unsigned long busypoll_timelimit;
942
943 dbc = container_of(to_delayed_work(work), struct xhci_dbc, event_work);
944 poll_interval = dbc->poll_interval;
945
946 spin_lock_irqsave(&dbc->lock, flags);
947 evtr = xhci_dbc_do_handle_events(dbc);
948 spin_unlock_irqrestore(&dbc->lock, flags);
949
950 switch (evtr) {
951 case EVT_GSER:
952 if (dbc->driver->configure)
953 dbc->driver->configure(dbc);
954 break;
955 case EVT_DISC:
956 if (dbc->driver->disconnect)
957 dbc->driver->disconnect(dbc);
958 break;
959 case EVT_DONE:
960 /*
961 * Set fast poll rate if there are pending out transfers, or
962 * a transfer was recently processed
963 */
964 busypoll_timelimit = dbc->xfer_timestamp +
965 msecs_to_jiffies(DBC_XFER_INACTIVITY_TIMEOUT);
966
967 if (!list_empty(&dbc->eps[BULK_OUT].list_pending) ||
968 time_is_after_jiffies(busypoll_timelimit))
969 poll_interval = 0;
970 break;
971 case EVT_XFER_DONE:
972 dbc->xfer_timestamp = jiffies;
973 poll_interval = 0;
974 break;
975 default:
976 dev_info(dbc->dev, "stop handling dbc events\n");
977 return;
978 }
979
980 mod_delayed_work(system_wq, &dbc->event_work,
981 msecs_to_jiffies(poll_interval));
982 }
983
984 static const char * const dbc_state_strings[DS_MAX] = {
985 [DS_DISABLED] = "disabled",
986 [DS_INITIALIZED] = "initialized",
987 [DS_ENABLED] = "enabled",
988 [DS_CONNECTED] = "connected",
989 [DS_CONFIGURED] = "configured",
990 };
991
dbc_show(struct device * dev,struct device_attribute * attr,char * buf)992 static ssize_t dbc_show(struct device *dev,
993 struct device_attribute *attr,
994 char *buf)
995 {
996 struct xhci_dbc *dbc;
997 struct xhci_hcd *xhci;
998
999 xhci = hcd_to_xhci(dev_get_drvdata(dev));
1000 dbc = xhci->dbc;
1001
1002 if (dbc->state >= ARRAY_SIZE(dbc_state_strings))
1003 return sysfs_emit(buf, "unknown\n");
1004
1005 return sysfs_emit(buf, "%s\n", dbc_state_strings[dbc->state]);
1006 }
1007
dbc_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1008 static ssize_t dbc_store(struct device *dev,
1009 struct device_attribute *attr,
1010 const char *buf, size_t count)
1011 {
1012 struct xhci_hcd *xhci;
1013 struct xhci_dbc *dbc;
1014
1015 xhci = hcd_to_xhci(dev_get_drvdata(dev));
1016 dbc = xhci->dbc;
1017
1018 if (sysfs_streq(buf, "enable"))
1019 xhci_dbc_start(dbc);
1020 else if (sysfs_streq(buf, "disable"))
1021 xhci_dbc_stop(dbc);
1022 else
1023 return -EINVAL;
1024
1025 return count;
1026 }
1027
dbc_idVendor_show(struct device * dev,struct device_attribute * attr,char * buf)1028 static ssize_t dbc_idVendor_show(struct device *dev,
1029 struct device_attribute *attr,
1030 char *buf)
1031 {
1032 struct xhci_dbc *dbc;
1033 struct xhci_hcd *xhci;
1034
1035 xhci = hcd_to_xhci(dev_get_drvdata(dev));
1036 dbc = xhci->dbc;
1037
1038 return sysfs_emit(buf, "%04x\n", dbc->idVendor);
1039 }
1040
dbc_idVendor_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)1041 static ssize_t dbc_idVendor_store(struct device *dev,
1042 struct device_attribute *attr,
1043 const char *buf, size_t size)
1044 {
1045 struct xhci_dbc *dbc;
1046 struct xhci_hcd *xhci;
1047 void __iomem *ptr;
1048 u16 value;
1049 u32 dev_info;
1050 int ret;
1051
1052 ret = kstrtou16(buf, 0, &value);
1053 if (ret)
1054 return ret;
1055
1056 xhci = hcd_to_xhci(dev_get_drvdata(dev));
1057 dbc = xhci->dbc;
1058 if (dbc->state != DS_DISABLED)
1059 return -EBUSY;
1060
1061 dbc->idVendor = value;
1062 ptr = &dbc->regs->devinfo1;
1063 dev_info = readl(ptr);
1064 dev_info = (dev_info & ~(0xffffu << 16)) | (value << 16);
1065 writel(dev_info, ptr);
1066
1067 return size;
1068 }
1069
dbc_idProduct_show(struct device * dev,struct device_attribute * attr,char * buf)1070 static ssize_t dbc_idProduct_show(struct device *dev,
1071 struct device_attribute *attr,
1072 char *buf)
1073 {
1074 struct xhci_dbc *dbc;
1075 struct xhci_hcd *xhci;
1076
1077 xhci = hcd_to_xhci(dev_get_drvdata(dev));
1078 dbc = xhci->dbc;
1079
1080 return sysfs_emit(buf, "%04x\n", dbc->idProduct);
1081 }
1082
dbc_idProduct_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)1083 static ssize_t dbc_idProduct_store(struct device *dev,
1084 struct device_attribute *attr,
1085 const char *buf, size_t size)
1086 {
1087 struct xhci_dbc *dbc;
1088 struct xhci_hcd *xhci;
1089 void __iomem *ptr;
1090 u32 dev_info;
1091 u16 value;
1092 int ret;
1093
1094 ret = kstrtou16(buf, 0, &value);
1095 if (ret)
1096 return ret;
1097
1098 xhci = hcd_to_xhci(dev_get_drvdata(dev));
1099 dbc = xhci->dbc;
1100 if (dbc->state != DS_DISABLED)
1101 return -EBUSY;
1102
1103 dbc->idProduct = value;
1104 ptr = &dbc->regs->devinfo2;
1105 dev_info = readl(ptr);
1106 dev_info = (dev_info & ~(0xffffu)) | value;
1107 writel(dev_info, ptr);
1108 return size;
1109 }
1110
dbc_bcdDevice_show(struct device * dev,struct device_attribute * attr,char * buf)1111 static ssize_t dbc_bcdDevice_show(struct device *dev,
1112 struct device_attribute *attr,
1113 char *buf)
1114 {
1115 struct xhci_dbc *dbc;
1116 struct xhci_hcd *xhci;
1117
1118 xhci = hcd_to_xhci(dev_get_drvdata(dev));
1119 dbc = xhci->dbc;
1120
1121 return sysfs_emit(buf, "%04x\n", dbc->bcdDevice);
1122 }
1123
dbc_bcdDevice_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)1124 static ssize_t dbc_bcdDevice_store(struct device *dev,
1125 struct device_attribute *attr,
1126 const char *buf, size_t size)
1127 {
1128 struct xhci_dbc *dbc;
1129 struct xhci_hcd *xhci;
1130 void __iomem *ptr;
1131 u32 dev_info;
1132 u16 value;
1133 int ret;
1134
1135 ret = kstrtou16(buf, 0, &value);
1136 if (ret)
1137 return ret;
1138
1139 xhci = hcd_to_xhci(dev_get_drvdata(dev));
1140 dbc = xhci->dbc;
1141 if (dbc->state != DS_DISABLED)
1142 return -EBUSY;
1143
1144 dbc->bcdDevice = value;
1145 ptr = &dbc->regs->devinfo2;
1146 dev_info = readl(ptr);
1147 dev_info = (dev_info & ~(0xffffu << 16)) | (value << 16);
1148 writel(dev_info, ptr);
1149
1150 return size;
1151 }
1152
dbc_bInterfaceProtocol_show(struct device * dev,struct device_attribute * attr,char * buf)1153 static ssize_t dbc_bInterfaceProtocol_show(struct device *dev,
1154 struct device_attribute *attr,
1155 char *buf)
1156 {
1157 struct xhci_dbc *dbc;
1158 struct xhci_hcd *xhci;
1159
1160 xhci = hcd_to_xhci(dev_get_drvdata(dev));
1161 dbc = xhci->dbc;
1162
1163 return sysfs_emit(buf, "%02x\n", dbc->bInterfaceProtocol);
1164 }
1165
dbc_bInterfaceProtocol_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)1166 static ssize_t dbc_bInterfaceProtocol_store(struct device *dev,
1167 struct device_attribute *attr,
1168 const char *buf, size_t size)
1169 {
1170 struct xhci_dbc *dbc;
1171 struct xhci_hcd *xhci;
1172 void __iomem *ptr;
1173 u32 dev_info;
1174 u8 value;
1175 int ret;
1176
1177 /* bInterfaceProtocol is 8 bit, but... */
1178 ret = kstrtou8(buf, 0, &value);
1179 if (ret)
1180 return ret;
1181
1182 /* ...xhci only supports values 0 and 1 */
1183 if (value > 1)
1184 return -EINVAL;
1185
1186 xhci = hcd_to_xhci(dev_get_drvdata(dev));
1187 dbc = xhci->dbc;
1188 if (dbc->state != DS_DISABLED)
1189 return -EBUSY;
1190
1191 dbc->bInterfaceProtocol = value;
1192 ptr = &dbc->regs->devinfo1;
1193 dev_info = readl(ptr);
1194 dev_info = (dev_info & ~(0xffu)) | value;
1195 writel(dev_info, ptr);
1196
1197 return size;
1198 }
1199
dbc_poll_interval_ms_show(struct device * dev,struct device_attribute * attr,char * buf)1200 static ssize_t dbc_poll_interval_ms_show(struct device *dev,
1201 struct device_attribute *attr,
1202 char *buf)
1203 {
1204 struct xhci_dbc *dbc;
1205 struct xhci_hcd *xhci;
1206
1207 xhci = hcd_to_xhci(dev_get_drvdata(dev));
1208 dbc = xhci->dbc;
1209
1210 return sysfs_emit(buf, "%u\n", dbc->poll_interval);
1211 }
1212
dbc_poll_interval_ms_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)1213 static ssize_t dbc_poll_interval_ms_store(struct device *dev,
1214 struct device_attribute *attr,
1215 const char *buf, size_t size)
1216 {
1217 struct xhci_dbc *dbc;
1218 struct xhci_hcd *xhci;
1219 u32 value;
1220 int ret;
1221
1222 ret = kstrtou32(buf, 0, &value);
1223 if (ret || value > DBC_POLL_INTERVAL_MAX)
1224 return -EINVAL;
1225
1226 xhci = hcd_to_xhci(dev_get_drvdata(dev));
1227 dbc = xhci->dbc;
1228
1229 dbc->poll_interval = value;
1230
1231 mod_delayed_work(system_wq, &dbc->event_work, 0);
1232
1233 return size;
1234 }
1235
1236 static DEVICE_ATTR_RW(dbc);
1237 static DEVICE_ATTR_RW(dbc_idVendor);
1238 static DEVICE_ATTR_RW(dbc_idProduct);
1239 static DEVICE_ATTR_RW(dbc_bcdDevice);
1240 static DEVICE_ATTR_RW(dbc_bInterfaceProtocol);
1241 static DEVICE_ATTR_RW(dbc_poll_interval_ms);
1242
1243 static struct attribute *dbc_dev_attrs[] = {
1244 &dev_attr_dbc.attr,
1245 &dev_attr_dbc_idVendor.attr,
1246 &dev_attr_dbc_idProduct.attr,
1247 &dev_attr_dbc_bcdDevice.attr,
1248 &dev_attr_dbc_bInterfaceProtocol.attr,
1249 &dev_attr_dbc_poll_interval_ms.attr,
1250 NULL
1251 };
1252 ATTRIBUTE_GROUPS(dbc_dev);
1253
1254 struct xhci_dbc *
xhci_alloc_dbc(struct device * dev,void __iomem * base,const struct dbc_driver * driver)1255 xhci_alloc_dbc(struct device *dev, void __iomem *base, const struct dbc_driver *driver)
1256 {
1257 struct xhci_dbc *dbc;
1258 int ret;
1259
1260 dbc = kzalloc(sizeof(*dbc), GFP_KERNEL);
1261 if (!dbc)
1262 return NULL;
1263
1264 dbc->regs = base;
1265 dbc->dev = dev;
1266 dbc->driver = driver;
1267 dbc->idProduct = DBC_PRODUCT_ID;
1268 dbc->idVendor = DBC_VENDOR_ID;
1269 dbc->bcdDevice = DBC_DEVICE_REV;
1270 dbc->bInterfaceProtocol = DBC_PROTOCOL;
1271 dbc->poll_interval = DBC_POLL_INTERVAL_DEFAULT;
1272
1273 if (readl(&dbc->regs->control) & DBC_CTRL_DBC_ENABLE)
1274 goto err;
1275
1276 INIT_DELAYED_WORK(&dbc->event_work, xhci_dbc_handle_events);
1277 spin_lock_init(&dbc->lock);
1278
1279 ret = sysfs_create_groups(&dev->kobj, dbc_dev_groups);
1280 if (ret)
1281 goto err;
1282
1283 return dbc;
1284 err:
1285 kfree(dbc);
1286 return NULL;
1287 }
1288
1289 /* undo what xhci_alloc_dbc() did */
xhci_dbc_remove(struct xhci_dbc * dbc)1290 void xhci_dbc_remove(struct xhci_dbc *dbc)
1291 {
1292 if (!dbc)
1293 return;
1294 /* stop hw, stop wq and call dbc->ops->stop() */
1295 xhci_dbc_stop(dbc);
1296
1297 /* remove sysfs files */
1298 sysfs_remove_groups(&dbc->dev->kobj, dbc_dev_groups);
1299
1300 kfree(dbc);
1301 }
1302
1303
xhci_create_dbc_dev(struct xhci_hcd * xhci)1304 int xhci_create_dbc_dev(struct xhci_hcd *xhci)
1305 {
1306 struct device *dev;
1307 void __iomem *base;
1308 int ret;
1309 int dbc_cap_offs;
1310
1311 /* create all parameters needed resembling a dbc device */
1312 dev = xhci_to_hcd(xhci)->self.controller;
1313 base = &xhci->cap_regs->hc_capbase;
1314
1315 dbc_cap_offs = xhci_find_next_ext_cap(base, 0, XHCI_EXT_CAPS_DEBUG);
1316 if (!dbc_cap_offs)
1317 return -ENODEV;
1318
1319 /* already allocated and in use */
1320 if (xhci->dbc)
1321 return -EBUSY;
1322
1323 ret = xhci_dbc_tty_probe(dev, base + dbc_cap_offs, xhci);
1324
1325 return ret;
1326 }
1327
xhci_remove_dbc_dev(struct xhci_hcd * xhci)1328 void xhci_remove_dbc_dev(struct xhci_hcd *xhci)
1329 {
1330 unsigned long flags;
1331
1332 if (!xhci->dbc)
1333 return;
1334
1335 xhci_dbc_tty_remove(xhci->dbc);
1336 spin_lock_irqsave(&xhci->lock, flags);
1337 xhci->dbc = NULL;
1338 spin_unlock_irqrestore(&xhci->lock, flags);
1339 }
1340
1341 #ifdef CONFIG_PM
xhci_dbc_suspend(struct xhci_hcd * xhci)1342 int xhci_dbc_suspend(struct xhci_hcd *xhci)
1343 {
1344 struct xhci_dbc *dbc = xhci->dbc;
1345
1346 if (!dbc)
1347 return 0;
1348
1349 if (dbc->state == DS_CONFIGURED)
1350 dbc->resume_required = 1;
1351
1352 xhci_dbc_stop(dbc);
1353
1354 return 0;
1355 }
1356
xhci_dbc_resume(struct xhci_hcd * xhci)1357 int xhci_dbc_resume(struct xhci_hcd *xhci)
1358 {
1359 int ret = 0;
1360 struct xhci_dbc *dbc = xhci->dbc;
1361
1362 if (!dbc)
1363 return 0;
1364
1365 if (dbc->resume_required) {
1366 dbc->resume_required = 0;
1367 xhci_dbc_start(dbc);
1368 }
1369
1370 return ret;
1371 }
1372 #endif /* CONFIG_PM */
1373
xhci_dbc_init(void)1374 int xhci_dbc_init(void)
1375 {
1376 return dbc_tty_init();
1377 }
1378
xhci_dbc_exit(void)1379 void xhci_dbc_exit(void)
1380 {
1381 dbc_tty_exit();
1382 }
1383