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 spin_lock(&dbc->lock);
656 xhci_dbc_flush_requests(dbc);
657 spin_unlock(&dbc->lock);
658
659 if (dbc->driver->disconnect)
660 dbc->driver->disconnect(dbc);
661 break;
662 default:
663 break;
664 }
665
666 cancel_delayed_work_sync(&dbc->event_work);
667
668 spin_lock_irqsave(&dbc->lock, flags);
669 ret = xhci_do_dbc_stop(dbc);
670 spin_unlock_irqrestore(&dbc->lock, flags);
671 if (ret)
672 return;
673
674 xhci_dbc_mem_cleanup(dbc);
675 pm_runtime_put_sync(dbc->dev); /* note, was self.controller */
676 }
677
678 static void
handle_ep_halt_changes(struct xhci_dbc * dbc,struct dbc_ep * dep,bool halted)679 handle_ep_halt_changes(struct xhci_dbc *dbc, struct dbc_ep *dep, bool halted)
680 {
681 if (halted) {
682 dev_info(dbc->dev, "DbC Endpoint halted\n");
683 dep->halted = 1;
684
685 } else if (dep->halted) {
686 dev_info(dbc->dev, "DbC Endpoint halt cleared\n");
687 dep->halted = 0;
688
689 if (!list_empty(&dep->list_pending))
690 writel(DBC_DOOR_BELL_TARGET(dep->direction),
691 &dbc->regs->doorbell);
692 }
693 }
694
695 static void
dbc_handle_port_status(struct xhci_dbc * dbc,union xhci_trb * event)696 dbc_handle_port_status(struct xhci_dbc *dbc, union xhci_trb *event)
697 {
698 u32 portsc;
699
700 portsc = readl(&dbc->regs->portsc);
701 if (portsc & DBC_PORTSC_CONN_CHANGE)
702 dev_info(dbc->dev, "DbC port connect change\n");
703
704 if (portsc & DBC_PORTSC_RESET_CHANGE)
705 dev_info(dbc->dev, "DbC port reset change\n");
706
707 if (portsc & DBC_PORTSC_LINK_CHANGE)
708 dev_info(dbc->dev, "DbC port link status change\n");
709
710 if (portsc & DBC_PORTSC_CONFIG_CHANGE)
711 dev_info(dbc->dev, "DbC config error change\n");
712
713 /* Port reset change bit will be cleared in other place: */
714 writel(portsc & ~DBC_PORTSC_RESET_CHANGE, &dbc->regs->portsc);
715 }
716
dbc_handle_xfer_event(struct xhci_dbc * dbc,union xhci_trb * event)717 static void dbc_handle_xfer_event(struct xhci_dbc *dbc, union xhci_trb *event)
718 {
719 struct dbc_ep *dep;
720 struct xhci_ring *ring;
721 int ep_id;
722 int status;
723 struct xhci_ep_ctx *ep_ctx;
724 u32 comp_code;
725 size_t remain_length;
726 struct dbc_request *req = NULL, *r;
727
728 comp_code = GET_COMP_CODE(le32_to_cpu(event->generic.field[2]));
729 remain_length = EVENT_TRB_LEN(le32_to_cpu(event->generic.field[2]));
730 ep_id = TRB_TO_EP_ID(le32_to_cpu(event->generic.field[3]));
731 dep = (ep_id == EPID_OUT) ?
732 get_out_ep(dbc) : get_in_ep(dbc);
733 ep_ctx = (ep_id == EPID_OUT) ?
734 dbc_bulkout_ctx(dbc) : dbc_bulkin_ctx(dbc);
735 ring = dep->ring;
736
737 /* Match the pending request: */
738 list_for_each_entry(r, &dep->list_pending, list_pending) {
739 if (r->trb_dma == event->trans_event.buffer) {
740 req = r;
741 break;
742 }
743 if (r->status == -COMP_STALL_ERROR) {
744 dev_warn(dbc->dev, "Give back stale stalled req\n");
745 ring->num_trbs_free++;
746 xhci_dbc_giveback(r, 0);
747 }
748 }
749
750 if (!req) {
751 dev_warn(dbc->dev, "no matched request\n");
752 return;
753 }
754
755 trace_xhci_dbc_handle_transfer(ring, &req->trb->generic, req->trb_dma);
756
757 switch (comp_code) {
758 case COMP_SUCCESS:
759 remain_length = 0;
760 fallthrough;
761 case COMP_SHORT_PACKET:
762 status = 0;
763 break;
764 case COMP_TRB_ERROR:
765 case COMP_BABBLE_DETECTED_ERROR:
766 case COMP_USB_TRANSACTION_ERROR:
767 dev_warn(dbc->dev, "tx error %d detected\n", comp_code);
768 status = -comp_code;
769 break;
770 case COMP_STALL_ERROR:
771 dev_warn(dbc->dev, "Stall error at bulk TRB %llx, remaining %zu, ep deq %llx\n",
772 event->trans_event.buffer, remain_length, ep_ctx->deq);
773 status = 0;
774 dep->halted = 1;
775
776 /*
777 * xHC DbC may trigger a STALL bulk xfer event when host sends a
778 * ClearFeature(ENDPOINT_HALT) request even if there wasn't an
779 * active bulk transfer.
780 *
781 * Don't give back this transfer request as hardware will later
782 * start processing TRBs starting from this 'STALLED' TRB,
783 * causing TRBs and requests to be out of sync.
784 *
785 * If STALL event shows some bytes were transferred then assume
786 * it's an actual transfer issue and give back the request.
787 * In this case mark the TRB as No-Op to avoid hw from using the
788 * TRB again.
789 */
790
791 if ((ep_ctx->deq & ~TRB_CYCLE) == event->trans_event.buffer) {
792 dev_dbg(dbc->dev, "Ep stopped on Stalled TRB\n");
793 if (remain_length == req->length) {
794 dev_dbg(dbc->dev, "Spurious stall event, keep req\n");
795 req->status = -COMP_STALL_ERROR;
796 req->actual = 0;
797 return;
798 }
799 dev_dbg(dbc->dev, "Give back stalled req, but turn TRB to No-op\n");
800 trb_to_noop(req->trb);
801 }
802 break;
803
804 default:
805 dev_err(dbc->dev, "unknown tx error %d\n", comp_code);
806 status = -comp_code;
807 break;
808 }
809
810 ring->num_trbs_free++;
811 req->actual = req->length - remain_length;
812 xhci_dbc_giveback(req, status);
813 }
814
inc_evt_deq(struct xhci_ring * ring)815 static void inc_evt_deq(struct xhci_ring *ring)
816 {
817 /* If on the last TRB of the segment go back to the beginning */
818 if (ring->dequeue == &ring->deq_seg->trbs[TRBS_PER_SEGMENT - 1]) {
819 ring->cycle_state ^= 1;
820 ring->dequeue = ring->deq_seg->trbs;
821 return;
822 }
823 ring->dequeue++;
824 }
825
xhci_dbc_do_handle_events(struct xhci_dbc * dbc)826 static enum evtreturn xhci_dbc_do_handle_events(struct xhci_dbc *dbc)
827 {
828 dma_addr_t deq;
829 union xhci_trb *evt;
830 enum evtreturn ret = EVT_DONE;
831 u32 ctrl, portsc;
832 bool update_erdp = false;
833
834 /* DbC state machine: */
835 switch (dbc->state) {
836 case DS_DISABLED:
837 case DS_INITIALIZED:
838
839 return EVT_ERR;
840 case DS_ENABLED:
841 portsc = readl(&dbc->regs->portsc);
842 if (portsc & DBC_PORTSC_CONN_STATUS) {
843 dbc->state = DS_CONNECTED;
844 dev_info(dbc->dev, "DbC connected\n");
845 }
846
847 return EVT_DONE;
848 case DS_CONNECTED:
849 ctrl = readl(&dbc->regs->control);
850 if (ctrl & DBC_CTRL_DBC_RUN) {
851 dbc->state = DS_CONFIGURED;
852 dev_info(dbc->dev, "DbC configured\n");
853 portsc = readl(&dbc->regs->portsc);
854 writel(portsc, &dbc->regs->portsc);
855 return EVT_GSER;
856 }
857
858 return EVT_DONE;
859 case DS_CONFIGURED:
860 /* Handle cable unplug event: */
861 portsc = readl(&dbc->regs->portsc);
862 if (!(portsc & DBC_PORTSC_PORT_ENABLED) &&
863 !(portsc & DBC_PORTSC_CONN_STATUS)) {
864 dev_info(dbc->dev, "DbC cable unplugged\n");
865 dbc->state = DS_ENABLED;
866 xhci_dbc_flush_requests(dbc);
867
868 return EVT_DISC;
869 }
870
871 /* Handle debug port reset event: */
872 if (portsc & DBC_PORTSC_RESET_CHANGE) {
873 dev_info(dbc->dev, "DbC port reset\n");
874 writel(portsc, &dbc->regs->portsc);
875 dbc->state = DS_ENABLED;
876 xhci_dbc_flush_requests(dbc);
877
878 return EVT_DISC;
879 }
880
881 /* Check and handle changes in endpoint halt status */
882 ctrl = readl(&dbc->regs->control);
883 handle_ep_halt_changes(dbc, get_in_ep(dbc), ctrl & DBC_CTRL_HALT_IN_TR);
884 handle_ep_halt_changes(dbc, get_out_ep(dbc), ctrl & DBC_CTRL_HALT_OUT_TR);
885
886 /* Clear DbC run change bit: */
887 if (ctrl & DBC_CTRL_DBC_RUN_CHANGE) {
888 writel(ctrl, &dbc->regs->control);
889 ctrl = readl(&dbc->regs->control);
890 }
891 break;
892 default:
893 dev_err(dbc->dev, "Unknown DbC state %d\n", dbc->state);
894 break;
895 }
896
897 /* Handle the events in the event ring: */
898 evt = dbc->ring_evt->dequeue;
899 while ((le32_to_cpu(evt->event_cmd.flags) & TRB_CYCLE) ==
900 dbc->ring_evt->cycle_state) {
901 /*
902 * Add a barrier between reading the cycle flag and any
903 * reads of the event's flags/data below:
904 */
905 rmb();
906
907 trace_xhci_dbc_handle_event(dbc->ring_evt, &evt->generic,
908 xhci_trb_virt_to_dma(dbc->ring_evt->deq_seg,
909 dbc->ring_evt->dequeue));
910
911 switch (le32_to_cpu(evt->event_cmd.flags) & TRB_TYPE_BITMASK) {
912 case TRB_TYPE(TRB_PORT_STATUS):
913 dbc_handle_port_status(dbc, evt);
914 break;
915 case TRB_TYPE(TRB_TRANSFER):
916 dbc_handle_xfer_event(dbc, evt);
917 ret = EVT_XFER_DONE;
918 break;
919 default:
920 break;
921 }
922
923 inc_evt_deq(dbc->ring_evt);
924
925 evt = dbc->ring_evt->dequeue;
926 update_erdp = true;
927 }
928
929 /* Update event ring dequeue pointer: */
930 if (update_erdp) {
931 deq = xhci_trb_virt_to_dma(dbc->ring_evt->deq_seg,
932 dbc->ring_evt->dequeue);
933 lo_hi_writeq(deq, &dbc->regs->erdp);
934 }
935
936 return ret;
937 }
938
xhci_dbc_handle_events(struct work_struct * work)939 static void xhci_dbc_handle_events(struct work_struct *work)
940 {
941 enum evtreturn evtr;
942 struct xhci_dbc *dbc;
943 unsigned long flags;
944 unsigned int poll_interval;
945 unsigned long busypoll_timelimit;
946
947 dbc = container_of(to_delayed_work(work), struct xhci_dbc, event_work);
948 poll_interval = dbc->poll_interval;
949
950 spin_lock_irqsave(&dbc->lock, flags);
951 evtr = xhci_dbc_do_handle_events(dbc);
952 spin_unlock_irqrestore(&dbc->lock, flags);
953
954 switch (evtr) {
955 case EVT_GSER:
956 if (dbc->driver->configure)
957 dbc->driver->configure(dbc);
958 break;
959 case EVT_DISC:
960 if (dbc->driver->disconnect)
961 dbc->driver->disconnect(dbc);
962 break;
963 case EVT_DONE:
964 /*
965 * Set fast poll rate if there are pending out transfers, or
966 * a transfer was recently processed
967 */
968 busypoll_timelimit = dbc->xfer_timestamp +
969 msecs_to_jiffies(DBC_XFER_INACTIVITY_TIMEOUT);
970
971 if (!list_empty(&dbc->eps[BULK_OUT].list_pending) ||
972 time_is_after_jiffies(busypoll_timelimit))
973 poll_interval = 0;
974 break;
975 case EVT_XFER_DONE:
976 dbc->xfer_timestamp = jiffies;
977 poll_interval = 0;
978 break;
979 default:
980 dev_info(dbc->dev, "stop handling dbc events\n");
981 return;
982 }
983
984 mod_delayed_work(system_wq, &dbc->event_work,
985 msecs_to_jiffies(poll_interval));
986 }
987
988 static const char * const dbc_state_strings[DS_MAX] = {
989 [DS_DISABLED] = "disabled",
990 [DS_INITIALIZED] = "initialized",
991 [DS_ENABLED] = "enabled",
992 [DS_CONNECTED] = "connected",
993 [DS_CONFIGURED] = "configured",
994 };
995
dbc_show(struct device * dev,struct device_attribute * attr,char * buf)996 static ssize_t dbc_show(struct device *dev,
997 struct device_attribute *attr,
998 char *buf)
999 {
1000 struct xhci_dbc *dbc;
1001 struct xhci_hcd *xhci;
1002
1003 xhci = hcd_to_xhci(dev_get_drvdata(dev));
1004 dbc = xhci->dbc;
1005
1006 if (dbc->state >= ARRAY_SIZE(dbc_state_strings))
1007 return sysfs_emit(buf, "unknown\n");
1008
1009 return sysfs_emit(buf, "%s\n", dbc_state_strings[dbc->state]);
1010 }
1011
dbc_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1012 static ssize_t dbc_store(struct device *dev,
1013 struct device_attribute *attr,
1014 const char *buf, size_t count)
1015 {
1016 struct xhci_hcd *xhci;
1017 struct xhci_dbc *dbc;
1018
1019 xhci = hcd_to_xhci(dev_get_drvdata(dev));
1020 dbc = xhci->dbc;
1021
1022 if (sysfs_streq(buf, "enable"))
1023 xhci_dbc_start(dbc);
1024 else if (sysfs_streq(buf, "disable"))
1025 xhci_dbc_stop(dbc);
1026 else
1027 return -EINVAL;
1028
1029 return count;
1030 }
1031
dbc_idVendor_show(struct device * dev,struct device_attribute * attr,char * buf)1032 static ssize_t dbc_idVendor_show(struct device *dev,
1033 struct device_attribute *attr,
1034 char *buf)
1035 {
1036 struct xhci_dbc *dbc;
1037 struct xhci_hcd *xhci;
1038
1039 xhci = hcd_to_xhci(dev_get_drvdata(dev));
1040 dbc = xhci->dbc;
1041
1042 return sysfs_emit(buf, "%04x\n", dbc->idVendor);
1043 }
1044
dbc_idVendor_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)1045 static ssize_t dbc_idVendor_store(struct device *dev,
1046 struct device_attribute *attr,
1047 const char *buf, size_t size)
1048 {
1049 struct xhci_dbc *dbc;
1050 struct xhci_hcd *xhci;
1051 void __iomem *ptr;
1052 u16 value;
1053 u32 dev_info;
1054 int ret;
1055
1056 ret = kstrtou16(buf, 0, &value);
1057 if (ret)
1058 return ret;
1059
1060 xhci = hcd_to_xhci(dev_get_drvdata(dev));
1061 dbc = xhci->dbc;
1062 if (dbc->state != DS_DISABLED)
1063 return -EBUSY;
1064
1065 dbc->idVendor = value;
1066 ptr = &dbc->regs->devinfo1;
1067 dev_info = readl(ptr);
1068 dev_info = (dev_info & ~(0xffffu << 16)) | (value << 16);
1069 writel(dev_info, ptr);
1070
1071 return size;
1072 }
1073
dbc_idProduct_show(struct device * dev,struct device_attribute * attr,char * buf)1074 static ssize_t dbc_idProduct_show(struct device *dev,
1075 struct device_attribute *attr,
1076 char *buf)
1077 {
1078 struct xhci_dbc *dbc;
1079 struct xhci_hcd *xhci;
1080
1081 xhci = hcd_to_xhci(dev_get_drvdata(dev));
1082 dbc = xhci->dbc;
1083
1084 return sysfs_emit(buf, "%04x\n", dbc->idProduct);
1085 }
1086
dbc_idProduct_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)1087 static ssize_t dbc_idProduct_store(struct device *dev,
1088 struct device_attribute *attr,
1089 const char *buf, size_t size)
1090 {
1091 struct xhci_dbc *dbc;
1092 struct xhci_hcd *xhci;
1093 void __iomem *ptr;
1094 u32 dev_info;
1095 u16 value;
1096 int ret;
1097
1098 ret = kstrtou16(buf, 0, &value);
1099 if (ret)
1100 return ret;
1101
1102 xhci = hcd_to_xhci(dev_get_drvdata(dev));
1103 dbc = xhci->dbc;
1104 if (dbc->state != DS_DISABLED)
1105 return -EBUSY;
1106
1107 dbc->idProduct = value;
1108 ptr = &dbc->regs->devinfo2;
1109 dev_info = readl(ptr);
1110 dev_info = (dev_info & ~(0xffffu)) | value;
1111 writel(dev_info, ptr);
1112 return size;
1113 }
1114
dbc_bcdDevice_show(struct device * dev,struct device_attribute * attr,char * buf)1115 static ssize_t dbc_bcdDevice_show(struct device *dev,
1116 struct device_attribute *attr,
1117 char *buf)
1118 {
1119 struct xhci_dbc *dbc;
1120 struct xhci_hcd *xhci;
1121
1122 xhci = hcd_to_xhci(dev_get_drvdata(dev));
1123 dbc = xhci->dbc;
1124
1125 return sysfs_emit(buf, "%04x\n", dbc->bcdDevice);
1126 }
1127
dbc_bcdDevice_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)1128 static ssize_t dbc_bcdDevice_store(struct device *dev,
1129 struct device_attribute *attr,
1130 const char *buf, size_t size)
1131 {
1132 struct xhci_dbc *dbc;
1133 struct xhci_hcd *xhci;
1134 void __iomem *ptr;
1135 u32 dev_info;
1136 u16 value;
1137 int ret;
1138
1139 ret = kstrtou16(buf, 0, &value);
1140 if (ret)
1141 return ret;
1142
1143 xhci = hcd_to_xhci(dev_get_drvdata(dev));
1144 dbc = xhci->dbc;
1145 if (dbc->state != DS_DISABLED)
1146 return -EBUSY;
1147
1148 dbc->bcdDevice = value;
1149 ptr = &dbc->regs->devinfo2;
1150 dev_info = readl(ptr);
1151 dev_info = (dev_info & ~(0xffffu << 16)) | (value << 16);
1152 writel(dev_info, ptr);
1153
1154 return size;
1155 }
1156
dbc_bInterfaceProtocol_show(struct device * dev,struct device_attribute * attr,char * buf)1157 static ssize_t dbc_bInterfaceProtocol_show(struct device *dev,
1158 struct device_attribute *attr,
1159 char *buf)
1160 {
1161 struct xhci_dbc *dbc;
1162 struct xhci_hcd *xhci;
1163
1164 xhci = hcd_to_xhci(dev_get_drvdata(dev));
1165 dbc = xhci->dbc;
1166
1167 return sysfs_emit(buf, "%02x\n", dbc->bInterfaceProtocol);
1168 }
1169
dbc_bInterfaceProtocol_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)1170 static ssize_t dbc_bInterfaceProtocol_store(struct device *dev,
1171 struct device_attribute *attr,
1172 const char *buf, size_t size)
1173 {
1174 struct xhci_dbc *dbc;
1175 struct xhci_hcd *xhci;
1176 void __iomem *ptr;
1177 u32 dev_info;
1178 u8 value;
1179 int ret;
1180
1181 /* bInterfaceProtocol is 8 bit, but... */
1182 ret = kstrtou8(buf, 0, &value);
1183 if (ret)
1184 return ret;
1185
1186 /* ...xhci only supports values 0 and 1 */
1187 if (value > 1)
1188 return -EINVAL;
1189
1190 xhci = hcd_to_xhci(dev_get_drvdata(dev));
1191 dbc = xhci->dbc;
1192 if (dbc->state != DS_DISABLED)
1193 return -EBUSY;
1194
1195 dbc->bInterfaceProtocol = value;
1196 ptr = &dbc->regs->devinfo1;
1197 dev_info = readl(ptr);
1198 dev_info = (dev_info & ~(0xffu)) | value;
1199 writel(dev_info, ptr);
1200
1201 return size;
1202 }
1203
dbc_poll_interval_ms_show(struct device * dev,struct device_attribute * attr,char * buf)1204 static ssize_t dbc_poll_interval_ms_show(struct device *dev,
1205 struct device_attribute *attr,
1206 char *buf)
1207 {
1208 struct xhci_dbc *dbc;
1209 struct xhci_hcd *xhci;
1210
1211 xhci = hcd_to_xhci(dev_get_drvdata(dev));
1212 dbc = xhci->dbc;
1213
1214 return sysfs_emit(buf, "%u\n", dbc->poll_interval);
1215 }
1216
dbc_poll_interval_ms_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)1217 static ssize_t dbc_poll_interval_ms_store(struct device *dev,
1218 struct device_attribute *attr,
1219 const char *buf, size_t size)
1220 {
1221 struct xhci_dbc *dbc;
1222 struct xhci_hcd *xhci;
1223 u32 value;
1224 int ret;
1225
1226 ret = kstrtou32(buf, 0, &value);
1227 if (ret || value > DBC_POLL_INTERVAL_MAX)
1228 return -EINVAL;
1229
1230 xhci = hcd_to_xhci(dev_get_drvdata(dev));
1231 dbc = xhci->dbc;
1232
1233 dbc->poll_interval = value;
1234
1235 mod_delayed_work(system_wq, &dbc->event_work, 0);
1236
1237 return size;
1238 }
1239
1240 static DEVICE_ATTR_RW(dbc);
1241 static DEVICE_ATTR_RW(dbc_idVendor);
1242 static DEVICE_ATTR_RW(dbc_idProduct);
1243 static DEVICE_ATTR_RW(dbc_bcdDevice);
1244 static DEVICE_ATTR_RW(dbc_bInterfaceProtocol);
1245 static DEVICE_ATTR_RW(dbc_poll_interval_ms);
1246
1247 static struct attribute *dbc_dev_attrs[] = {
1248 &dev_attr_dbc.attr,
1249 &dev_attr_dbc_idVendor.attr,
1250 &dev_attr_dbc_idProduct.attr,
1251 &dev_attr_dbc_bcdDevice.attr,
1252 &dev_attr_dbc_bInterfaceProtocol.attr,
1253 &dev_attr_dbc_poll_interval_ms.attr,
1254 NULL
1255 };
1256 ATTRIBUTE_GROUPS(dbc_dev);
1257
1258 struct xhci_dbc *
xhci_alloc_dbc(struct device * dev,void __iomem * base,const struct dbc_driver * driver)1259 xhci_alloc_dbc(struct device *dev, void __iomem *base, const struct dbc_driver *driver)
1260 {
1261 struct xhci_dbc *dbc;
1262 int ret;
1263
1264 dbc = kzalloc(sizeof(*dbc), GFP_KERNEL);
1265 if (!dbc)
1266 return NULL;
1267
1268 dbc->regs = base;
1269 dbc->dev = dev;
1270 dbc->driver = driver;
1271 dbc->idProduct = DBC_PRODUCT_ID;
1272 dbc->idVendor = DBC_VENDOR_ID;
1273 dbc->bcdDevice = DBC_DEVICE_REV;
1274 dbc->bInterfaceProtocol = DBC_PROTOCOL;
1275 dbc->poll_interval = DBC_POLL_INTERVAL_DEFAULT;
1276
1277 if (readl(&dbc->regs->control) & DBC_CTRL_DBC_ENABLE)
1278 goto err;
1279
1280 INIT_DELAYED_WORK(&dbc->event_work, xhci_dbc_handle_events);
1281 spin_lock_init(&dbc->lock);
1282
1283 ret = sysfs_create_groups(&dev->kobj, dbc_dev_groups);
1284 if (ret)
1285 goto err;
1286
1287 return dbc;
1288 err:
1289 kfree(dbc);
1290 return NULL;
1291 }
1292
1293 /* undo what xhci_alloc_dbc() did */
xhci_dbc_remove(struct xhci_dbc * dbc)1294 void xhci_dbc_remove(struct xhci_dbc *dbc)
1295 {
1296 if (!dbc)
1297 return;
1298 /* stop hw, stop wq and call dbc->ops->stop() */
1299 xhci_dbc_stop(dbc);
1300
1301 /* remove sysfs files */
1302 sysfs_remove_groups(&dbc->dev->kobj, dbc_dev_groups);
1303
1304 kfree(dbc);
1305 }
1306
1307
xhci_create_dbc_dev(struct xhci_hcd * xhci)1308 int xhci_create_dbc_dev(struct xhci_hcd *xhci)
1309 {
1310 struct device *dev;
1311 void __iomem *base;
1312 int ret;
1313 int dbc_cap_offs;
1314
1315 /* create all parameters needed resembling a dbc device */
1316 dev = xhci_to_hcd(xhci)->self.controller;
1317 base = &xhci->cap_regs->hc_capbase;
1318
1319 dbc_cap_offs = xhci_find_next_ext_cap(base, 0, XHCI_EXT_CAPS_DEBUG);
1320 if (!dbc_cap_offs)
1321 return -ENODEV;
1322
1323 /* already allocated and in use */
1324 if (xhci->dbc)
1325 return -EBUSY;
1326
1327 ret = xhci_dbc_tty_probe(dev, base + dbc_cap_offs, xhci);
1328
1329 return ret;
1330 }
1331
xhci_remove_dbc_dev(struct xhci_hcd * xhci)1332 void xhci_remove_dbc_dev(struct xhci_hcd *xhci)
1333 {
1334 unsigned long flags;
1335
1336 if (!xhci->dbc)
1337 return;
1338
1339 xhci_dbc_tty_remove(xhci->dbc);
1340 spin_lock_irqsave(&xhci->lock, flags);
1341 xhci->dbc = NULL;
1342 spin_unlock_irqrestore(&xhci->lock, flags);
1343 }
1344
1345 #ifdef CONFIG_PM
xhci_dbc_suspend(struct xhci_hcd * xhci)1346 int xhci_dbc_suspend(struct xhci_hcd *xhci)
1347 {
1348 struct xhci_dbc *dbc = xhci->dbc;
1349
1350 if (!dbc)
1351 return 0;
1352
1353 if (dbc->state == DS_CONFIGURED)
1354 dbc->resume_required = 1;
1355
1356 xhci_dbc_stop(dbc);
1357
1358 return 0;
1359 }
1360
xhci_dbc_resume(struct xhci_hcd * xhci)1361 int xhci_dbc_resume(struct xhci_hcd *xhci)
1362 {
1363 int ret = 0;
1364 struct xhci_dbc *dbc = xhci->dbc;
1365
1366 if (!dbc)
1367 return 0;
1368
1369 if (dbc->resume_required) {
1370 dbc->resume_required = 0;
1371 xhci_dbc_start(dbc);
1372 }
1373
1374 return ret;
1375 }
1376 #endif /* CONFIG_PM */
1377
xhci_dbc_init(void)1378 int xhci_dbc_init(void)
1379 {
1380 return dbc_tty_init();
1381 }
1382
xhci_dbc_exit(void)1383 void xhci_dbc_exit(void)
1384 {
1385 dbc_tty_exit();
1386 }
1387