0cc5a015451b65d5601febf82a239fd98d0cdda1
[android-sdk/kernel-video.git] / drivers / usb / dwc3 / debugfs.c
1 /**
2  * debugfs.c - DesignWare USB3 DRD Controller DebugFS file
3  *
4  * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
5  *
6  * Authors: Felipe Balbi <balbi@ti.com>,
7  *          Sebastian Andrzej Siewior <bigeasy@linutronix.de>
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2  of
11  * the License as published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  */
19 #include <linux/kernel.h>
20 #include <linux/slab.h>
21 #include <linux/ptrace.h>
22 #include <linux/types.h>
23 #include <linux/spinlock.h>
24 #include <linux/debugfs.h>
25 #include <linux/seq_file.h>
26 #include <linux/delay.h>
27 #include <linux/uaccess.h>
29 #include <linux/usb/ch9.h>
30 #include <linux/usb/drd.h>
32 #include "core.h"
33 #include "gadget.h"
34 #include "io.h"
35 #include "debug.h"
37 #define dump_register(nm)                               \
38 {                                                       \
39         .name   = __stringify(nm),                      \
40         .offset = DWC3_ ##nm - DWC3_GLOBALS_REGS_START, \
41 }
43 static const struct debugfs_reg32 dwc3_regs[] = {
44         dump_register(GSBUSCFG0),
45         dump_register(GSBUSCFG1),
46         dump_register(GTXTHRCFG),
47         dump_register(GRXTHRCFG),
48         dump_register(GCTL),
49         dump_register(GEVTEN),
50         dump_register(GSTS),
51         dump_register(GSNPSID),
52         dump_register(GGPIO),
53         dump_register(GUID),
54         dump_register(GUCTL),
55         dump_register(GBUSERRADDR0),
56         dump_register(GBUSERRADDR1),
57         dump_register(GPRTBIMAP0),
58         dump_register(GPRTBIMAP1),
59         dump_register(GHWPARAMS0),
60         dump_register(GHWPARAMS1),
61         dump_register(GHWPARAMS2),
62         dump_register(GHWPARAMS3),
63         dump_register(GHWPARAMS4),
64         dump_register(GHWPARAMS5),
65         dump_register(GHWPARAMS6),
66         dump_register(GHWPARAMS7),
67         dump_register(GDBGFIFOSPACE),
68         dump_register(GDBGLTSSM),
69         dump_register(GPRTBIMAP_HS0),
70         dump_register(GPRTBIMAP_HS1),
71         dump_register(GPRTBIMAP_FS0),
72         dump_register(GPRTBIMAP_FS1),
74         dump_register(GUSB2PHYCFG(0)),
75         dump_register(GUSB2PHYCFG(1)),
76         dump_register(GUSB2PHYCFG(2)),
77         dump_register(GUSB2PHYCFG(3)),
78         dump_register(GUSB2PHYCFG(4)),
79         dump_register(GUSB2PHYCFG(5)),
80         dump_register(GUSB2PHYCFG(6)),
81         dump_register(GUSB2PHYCFG(7)),
82         dump_register(GUSB2PHYCFG(8)),
83         dump_register(GUSB2PHYCFG(9)),
84         dump_register(GUSB2PHYCFG(10)),
85         dump_register(GUSB2PHYCFG(11)),
86         dump_register(GUSB2PHYCFG(12)),
87         dump_register(GUSB2PHYCFG(13)),
88         dump_register(GUSB2PHYCFG(14)),
89         dump_register(GUSB2PHYCFG(15)),
91         dump_register(GUSB2I2CCTL(0)),
92         dump_register(GUSB2I2CCTL(1)),
93         dump_register(GUSB2I2CCTL(2)),
94         dump_register(GUSB2I2CCTL(3)),
95         dump_register(GUSB2I2CCTL(4)),
96         dump_register(GUSB2I2CCTL(5)),
97         dump_register(GUSB2I2CCTL(6)),
98         dump_register(GUSB2I2CCTL(7)),
99         dump_register(GUSB2I2CCTL(8)),
100         dump_register(GUSB2I2CCTL(9)),
101         dump_register(GUSB2I2CCTL(10)),
102         dump_register(GUSB2I2CCTL(11)),
103         dump_register(GUSB2I2CCTL(12)),
104         dump_register(GUSB2I2CCTL(13)),
105         dump_register(GUSB2I2CCTL(14)),
106         dump_register(GUSB2I2CCTL(15)),
108         dump_register(GUSB2PHYACC(0)),
109         dump_register(GUSB2PHYACC(1)),
110         dump_register(GUSB2PHYACC(2)),
111         dump_register(GUSB2PHYACC(3)),
112         dump_register(GUSB2PHYACC(4)),
113         dump_register(GUSB2PHYACC(5)),
114         dump_register(GUSB2PHYACC(6)),
115         dump_register(GUSB2PHYACC(7)),
116         dump_register(GUSB2PHYACC(8)),
117         dump_register(GUSB2PHYACC(9)),
118         dump_register(GUSB2PHYACC(10)),
119         dump_register(GUSB2PHYACC(11)),
120         dump_register(GUSB2PHYACC(12)),
121         dump_register(GUSB2PHYACC(13)),
122         dump_register(GUSB2PHYACC(14)),
123         dump_register(GUSB2PHYACC(15)),
125         dump_register(GUSB3PIPECTL(0)),
126         dump_register(GUSB3PIPECTL(1)),
127         dump_register(GUSB3PIPECTL(2)),
128         dump_register(GUSB3PIPECTL(3)),
129         dump_register(GUSB3PIPECTL(4)),
130         dump_register(GUSB3PIPECTL(5)),
131         dump_register(GUSB3PIPECTL(6)),
132         dump_register(GUSB3PIPECTL(7)),
133         dump_register(GUSB3PIPECTL(8)),
134         dump_register(GUSB3PIPECTL(9)),
135         dump_register(GUSB3PIPECTL(10)),
136         dump_register(GUSB3PIPECTL(11)),
137         dump_register(GUSB3PIPECTL(12)),
138         dump_register(GUSB3PIPECTL(13)),
139         dump_register(GUSB3PIPECTL(14)),
140         dump_register(GUSB3PIPECTL(15)),
142         dump_register(GTXFIFOSIZ(0)),
143         dump_register(GTXFIFOSIZ(1)),
144         dump_register(GTXFIFOSIZ(2)),
145         dump_register(GTXFIFOSIZ(3)),
146         dump_register(GTXFIFOSIZ(4)),
147         dump_register(GTXFIFOSIZ(5)),
148         dump_register(GTXFIFOSIZ(6)),
149         dump_register(GTXFIFOSIZ(7)),
150         dump_register(GTXFIFOSIZ(8)),
151         dump_register(GTXFIFOSIZ(9)),
152         dump_register(GTXFIFOSIZ(10)),
153         dump_register(GTXFIFOSIZ(11)),
154         dump_register(GTXFIFOSIZ(12)),
155         dump_register(GTXFIFOSIZ(13)),
156         dump_register(GTXFIFOSIZ(14)),
157         dump_register(GTXFIFOSIZ(15)),
158         dump_register(GTXFIFOSIZ(16)),
159         dump_register(GTXFIFOSIZ(17)),
160         dump_register(GTXFIFOSIZ(18)),
161         dump_register(GTXFIFOSIZ(19)),
162         dump_register(GTXFIFOSIZ(20)),
163         dump_register(GTXFIFOSIZ(21)),
164         dump_register(GTXFIFOSIZ(22)),
165         dump_register(GTXFIFOSIZ(23)),
166         dump_register(GTXFIFOSIZ(24)),
167         dump_register(GTXFIFOSIZ(25)),
168         dump_register(GTXFIFOSIZ(26)),
169         dump_register(GTXFIFOSIZ(27)),
170         dump_register(GTXFIFOSIZ(28)),
171         dump_register(GTXFIFOSIZ(29)),
172         dump_register(GTXFIFOSIZ(30)),
173         dump_register(GTXFIFOSIZ(31)),
175         dump_register(GRXFIFOSIZ(0)),
176         dump_register(GRXFIFOSIZ(1)),
177         dump_register(GRXFIFOSIZ(2)),
178         dump_register(GRXFIFOSIZ(3)),
179         dump_register(GRXFIFOSIZ(4)),
180         dump_register(GRXFIFOSIZ(5)),
181         dump_register(GRXFIFOSIZ(6)),
182         dump_register(GRXFIFOSIZ(7)),
183         dump_register(GRXFIFOSIZ(8)),
184         dump_register(GRXFIFOSIZ(9)),
185         dump_register(GRXFIFOSIZ(10)),
186         dump_register(GRXFIFOSIZ(11)),
187         dump_register(GRXFIFOSIZ(12)),
188         dump_register(GRXFIFOSIZ(13)),
189         dump_register(GRXFIFOSIZ(14)),
190         dump_register(GRXFIFOSIZ(15)),
191         dump_register(GRXFIFOSIZ(16)),
192         dump_register(GRXFIFOSIZ(17)),
193         dump_register(GRXFIFOSIZ(18)),
194         dump_register(GRXFIFOSIZ(19)),
195         dump_register(GRXFIFOSIZ(20)),
196         dump_register(GRXFIFOSIZ(21)),
197         dump_register(GRXFIFOSIZ(22)),
198         dump_register(GRXFIFOSIZ(23)),
199         dump_register(GRXFIFOSIZ(24)),
200         dump_register(GRXFIFOSIZ(25)),
201         dump_register(GRXFIFOSIZ(26)),
202         dump_register(GRXFIFOSIZ(27)),
203         dump_register(GRXFIFOSIZ(28)),
204         dump_register(GRXFIFOSIZ(29)),
205         dump_register(GRXFIFOSIZ(30)),
206         dump_register(GRXFIFOSIZ(31)),
208         dump_register(GEVNTADRLO(0)),
209         dump_register(GEVNTADRHI(0)),
210         dump_register(GEVNTSIZ(0)),
211         dump_register(GEVNTCOUNT(0)),
213         dump_register(GHWPARAMS8),
214         dump_register(DCFG),
215         dump_register(DCTL),
216         dump_register(DEVTEN),
217         dump_register(DSTS),
218         dump_register(DGCMDPAR),
219         dump_register(DGCMD),
220         dump_register(DALEPENA),
222         dump_register(DEPCMDPAR2(0)),
223         dump_register(DEPCMDPAR2(1)),
224         dump_register(DEPCMDPAR2(2)),
225         dump_register(DEPCMDPAR2(3)),
226         dump_register(DEPCMDPAR2(4)),
227         dump_register(DEPCMDPAR2(5)),
228         dump_register(DEPCMDPAR2(6)),
229         dump_register(DEPCMDPAR2(7)),
230         dump_register(DEPCMDPAR2(8)),
231         dump_register(DEPCMDPAR2(9)),
232         dump_register(DEPCMDPAR2(10)),
233         dump_register(DEPCMDPAR2(11)),
234         dump_register(DEPCMDPAR2(12)),
235         dump_register(DEPCMDPAR2(13)),
236         dump_register(DEPCMDPAR2(14)),
237         dump_register(DEPCMDPAR2(15)),
238         dump_register(DEPCMDPAR2(16)),
239         dump_register(DEPCMDPAR2(17)),
240         dump_register(DEPCMDPAR2(18)),
241         dump_register(DEPCMDPAR2(19)),
242         dump_register(DEPCMDPAR2(20)),
243         dump_register(DEPCMDPAR2(21)),
244         dump_register(DEPCMDPAR2(22)),
245         dump_register(DEPCMDPAR2(23)),
246         dump_register(DEPCMDPAR2(24)),
247         dump_register(DEPCMDPAR2(25)),
248         dump_register(DEPCMDPAR2(26)),
249         dump_register(DEPCMDPAR2(27)),
250         dump_register(DEPCMDPAR2(28)),
251         dump_register(DEPCMDPAR2(29)),
252         dump_register(DEPCMDPAR2(30)),
253         dump_register(DEPCMDPAR2(31)),
255         dump_register(DEPCMDPAR1(0)),
256         dump_register(DEPCMDPAR1(1)),
257         dump_register(DEPCMDPAR1(2)),
258         dump_register(DEPCMDPAR1(3)),
259         dump_register(DEPCMDPAR1(4)),
260         dump_register(DEPCMDPAR1(5)),
261         dump_register(DEPCMDPAR1(6)),
262         dump_register(DEPCMDPAR1(7)),
263         dump_register(DEPCMDPAR1(8)),
264         dump_register(DEPCMDPAR1(9)),
265         dump_register(DEPCMDPAR1(10)),
266         dump_register(DEPCMDPAR1(11)),
267         dump_register(DEPCMDPAR1(12)),
268         dump_register(DEPCMDPAR1(13)),
269         dump_register(DEPCMDPAR1(14)),
270         dump_register(DEPCMDPAR1(15)),
271         dump_register(DEPCMDPAR1(16)),
272         dump_register(DEPCMDPAR1(17)),
273         dump_register(DEPCMDPAR1(18)),
274         dump_register(DEPCMDPAR1(19)),
275         dump_register(DEPCMDPAR1(20)),
276         dump_register(DEPCMDPAR1(21)),
277         dump_register(DEPCMDPAR1(22)),
278         dump_register(DEPCMDPAR1(23)),
279         dump_register(DEPCMDPAR1(24)),
280         dump_register(DEPCMDPAR1(25)),
281         dump_register(DEPCMDPAR1(26)),
282         dump_register(DEPCMDPAR1(27)),
283         dump_register(DEPCMDPAR1(28)),
284         dump_register(DEPCMDPAR1(29)),
285         dump_register(DEPCMDPAR1(30)),
286         dump_register(DEPCMDPAR1(31)),
288         dump_register(DEPCMDPAR0(0)),
289         dump_register(DEPCMDPAR0(1)),
290         dump_register(DEPCMDPAR0(2)),
291         dump_register(DEPCMDPAR0(3)),
292         dump_register(DEPCMDPAR0(4)),
293         dump_register(DEPCMDPAR0(5)),
294         dump_register(DEPCMDPAR0(6)),
295         dump_register(DEPCMDPAR0(7)),
296         dump_register(DEPCMDPAR0(8)),
297         dump_register(DEPCMDPAR0(9)),
298         dump_register(DEPCMDPAR0(10)),
299         dump_register(DEPCMDPAR0(11)),
300         dump_register(DEPCMDPAR0(12)),
301         dump_register(DEPCMDPAR0(13)),
302         dump_register(DEPCMDPAR0(14)),
303         dump_register(DEPCMDPAR0(15)),
304         dump_register(DEPCMDPAR0(16)),
305         dump_register(DEPCMDPAR0(17)),
306         dump_register(DEPCMDPAR0(18)),
307         dump_register(DEPCMDPAR0(19)),
308         dump_register(DEPCMDPAR0(20)),
309         dump_register(DEPCMDPAR0(21)),
310         dump_register(DEPCMDPAR0(22)),
311         dump_register(DEPCMDPAR0(23)),
312         dump_register(DEPCMDPAR0(24)),
313         dump_register(DEPCMDPAR0(25)),
314         dump_register(DEPCMDPAR0(26)),
315         dump_register(DEPCMDPAR0(27)),
316         dump_register(DEPCMDPAR0(28)),
317         dump_register(DEPCMDPAR0(29)),
318         dump_register(DEPCMDPAR0(30)),
319         dump_register(DEPCMDPAR0(31)),
321         dump_register(DEPCMD(0)),
322         dump_register(DEPCMD(1)),
323         dump_register(DEPCMD(2)),
324         dump_register(DEPCMD(3)),
325         dump_register(DEPCMD(4)),
326         dump_register(DEPCMD(5)),
327         dump_register(DEPCMD(6)),
328         dump_register(DEPCMD(7)),
329         dump_register(DEPCMD(8)),
330         dump_register(DEPCMD(9)),
331         dump_register(DEPCMD(10)),
332         dump_register(DEPCMD(11)),
333         dump_register(DEPCMD(12)),
334         dump_register(DEPCMD(13)),
335         dump_register(DEPCMD(14)),
336         dump_register(DEPCMD(15)),
337         dump_register(DEPCMD(16)),
338         dump_register(DEPCMD(17)),
339         dump_register(DEPCMD(18)),
340         dump_register(DEPCMD(19)),
341         dump_register(DEPCMD(20)),
342         dump_register(DEPCMD(21)),
343         dump_register(DEPCMD(22)),
344         dump_register(DEPCMD(23)),
345         dump_register(DEPCMD(24)),
346         dump_register(DEPCMD(25)),
347         dump_register(DEPCMD(26)),
348         dump_register(DEPCMD(27)),
349         dump_register(DEPCMD(28)),
350         dump_register(DEPCMD(29)),
351         dump_register(DEPCMD(30)),
352         dump_register(DEPCMD(31)),
354         dump_register(OCFG),
355         dump_register(OCTL),
356         dump_register(OEVT),
357         dump_register(OEVTEN),
358         dump_register(OSTS),
359 };
361 static int dwc3_mode_show(struct seq_file *s, void *unused)
363         struct dwc3             *dwc = s->private;
364         unsigned long           flags;
365         u32                     reg;
367         spin_lock_irqsave(&dwc->lock, flags);
368         reg = dwc3_readl(dwc->regs, DWC3_GCTL);
369         spin_unlock_irqrestore(&dwc->lock, flags);
371         switch (DWC3_GCTL_PRTCAP(reg)) {
372         case DWC3_GCTL_PRTCAP_HOST:
373                 seq_printf(s, "host\n");
374                 break;
375         case DWC3_GCTL_PRTCAP_DEVICE:
376                 seq_printf(s, "device\n");
377                 break;
378         case DWC3_GCTL_PRTCAP_OTG:
379                 seq_printf(s, "OTG\n");
380                 break;
381         default:
382                 seq_printf(s, "UNKNOWN %08x\n", DWC3_GCTL_PRTCAP(reg));
383         }
385         return 0;
388 static int dwc3_mode_open(struct inode *inode, struct file *file)
390         return single_open(file, dwc3_mode_show, inode->i_private);
393 static ssize_t dwc3_mode_write(struct file *file,
394                 const char __user *ubuf, size_t count, loff_t *ppos)
396         struct seq_file         *s = file->private_data;
397         struct dwc3             *dwc = s->private;
398         unsigned long           flags;
399         u32                     mode = 0;
400         char                    buf[32];
402         if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
403                 return -EFAULT;
405         if (!strncmp(buf, "host", 4))
406                 mode |= DWC3_GCTL_PRTCAP_HOST;
408         if (!strncmp(buf, "device", 6))
409                 mode |= DWC3_GCTL_PRTCAP_DEVICE;
411         if (!strncmp(buf, "otg", 3))
412                 mode |= DWC3_GCTL_PRTCAP_OTG;
414         if (mode) {
415                 spin_lock_irqsave(&dwc->lock, flags);
416                 if (mode & DWC3_GCTL_PRTCAP_HOST) {
417                         dwc3_omap_usbvbus_id_handler(dwc->dev->parent,
418                                 OMAP_DWC3_ID_GROUND);
419                         mode = 0;
420                 } else if (mode & DWC3_GCTL_PRTCAP_DEVICE) {
421                         dwc3_omap_usbvbus_id_handler(dwc->dev->parent,
422                                 OMAP_DWC3_VBUS_VALID);
423                         mode = 1;
424                 }
425                 spin_unlock_irqrestore(&dwc->lock, flags);
426         }
427         return count;
430 static const struct file_operations dwc3_mode_fops = {
431         .open                   = dwc3_mode_open,
432         .write                  = dwc3_mode_write,
433         .read                   = seq_read,
434         .llseek                 = seq_lseek,
435         .release                = single_release,
436 };
438 static int dwc3_testmode_show(struct seq_file *s, void *unused)
440         struct dwc3             *dwc = s->private;
441         unsigned long           flags;
442         u32                     reg;
444         spin_lock_irqsave(&dwc->lock, flags);
445         reg = dwc3_readl(dwc->regs, DWC3_DCTL);
446         reg &= DWC3_DCTL_TSTCTRL_MASK;
447         reg >>= 1;
448         spin_unlock_irqrestore(&dwc->lock, flags);
450         switch (reg) {
451         case 0:
452                 seq_printf(s, "no test\n");
453                 break;
454         case TEST_J:
455                 seq_printf(s, "test_j\n");
456                 break;
457         case TEST_K:
458                 seq_printf(s, "test_k\n");
459                 break;
460         case TEST_SE0_NAK:
461                 seq_printf(s, "test_se0_nak\n");
462                 break;
463         case TEST_PACKET:
464                 seq_printf(s, "test_packet\n");
465                 break;
466         case TEST_FORCE_EN:
467                 seq_printf(s, "test_force_enable\n");
468                 break;
469         default:
470                 seq_printf(s, "UNKNOWN %d\n", reg);
471         }
473         return 0;
476 static int dwc3_testmode_open(struct inode *inode, struct file *file)
478         return single_open(file, dwc3_testmode_show, inode->i_private);
481 static ssize_t dwc3_testmode_write(struct file *file,
482                 const char __user *ubuf, size_t count, loff_t *ppos)
484         struct seq_file         *s = file->private_data;
485         struct dwc3             *dwc = s->private;
486         unsigned long           flags;
487         u32                     testmode = 0;
488         char                    buf[32];
490         if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
491                 return -EFAULT;
493         if (!strncmp(buf, "test_j", 6))
494                 testmode = TEST_J;
495         else if (!strncmp(buf, "test_k", 6))
496                 testmode = TEST_K;
497         else if (!strncmp(buf, "test_se0_nak", 12))
498                 testmode = TEST_SE0_NAK;
499         else if (!strncmp(buf, "test_packet", 11))
500                 testmode = TEST_PACKET;
501         else if (!strncmp(buf, "test_force_enable", 17))
502                 testmode = TEST_FORCE_EN;
503         else
504                 testmode = 0;
506         spin_lock_irqsave(&dwc->lock, flags);
507         dwc3_gadget_set_test_mode(dwc, testmode);
508         spin_unlock_irqrestore(&dwc->lock, flags);
510         return count;
513 static const struct file_operations dwc3_testmode_fops = {
514         .open                   = dwc3_testmode_open,
515         .write                  = dwc3_testmode_write,
516         .read                   = seq_read,
517         .llseek                 = seq_lseek,
518         .release                = single_release,
519 };
521 static int dwc3_link_state_show(struct seq_file *s, void *unused)
523         struct dwc3             *dwc = s->private;
524         unsigned long           flags;
525         enum dwc3_link_state    state;
526         u32                     reg;
528         spin_lock_irqsave(&dwc->lock, flags);
529         reg = dwc3_readl(dwc->regs, DWC3_DSTS);
530         state = DWC3_DSTS_USBLNKST(reg);
531         spin_unlock_irqrestore(&dwc->lock, flags);
533         switch (state) {
534         case DWC3_LINK_STATE_U0:
535                 seq_printf(s, "U0\n");
536                 break;
537         case DWC3_LINK_STATE_U1:
538                 seq_printf(s, "U1\n");
539                 break;
540         case DWC3_LINK_STATE_U2:
541                 seq_printf(s, "U2\n");
542                 break;
543         case DWC3_LINK_STATE_U3:
544                 seq_printf(s, "U3\n");
545                 break;
546         case DWC3_LINK_STATE_SS_DIS:
547                 seq_printf(s, "SS.Disabled\n");
548                 break;
549         case DWC3_LINK_STATE_RX_DET:
550                 seq_printf(s, "Rx.Detect\n");
551                 break;
552         case DWC3_LINK_STATE_SS_INACT:
553                 seq_printf(s, "SS.Inactive\n");
554                 break;
555         case DWC3_LINK_STATE_POLL:
556                 seq_printf(s, "Poll\n");
557                 break;
558         case DWC3_LINK_STATE_RECOV:
559                 seq_printf(s, "Recovery\n");
560                 break;
561         case DWC3_LINK_STATE_HRESET:
562                 seq_printf(s, "HRESET\n");
563                 break;
564         case DWC3_LINK_STATE_CMPLY:
565                 seq_printf(s, "Compliance\n");
566                 break;
567         case DWC3_LINK_STATE_LPBK:
568                 seq_printf(s, "Loopback\n");
569                 break;
570         case DWC3_LINK_STATE_RESET:
571                 seq_printf(s, "Reset\n");
572                 break;
573         case DWC3_LINK_STATE_RESUME:
574                 seq_printf(s, "Resume\n");
575                 break;
576         default:
577                 seq_printf(s, "UNKNOWN %d\n", state);
578         }
580         return 0;
583 static int dwc3_link_state_open(struct inode *inode, struct file *file)
585         return single_open(file, dwc3_link_state_show, inode->i_private);
588 static ssize_t dwc3_link_state_write(struct file *file,
589                 const char __user *ubuf, size_t count, loff_t *ppos)
591         struct seq_file         *s = file->private_data;
592         struct dwc3             *dwc = s->private;
593         unsigned long           flags;
594         enum dwc3_link_state    state = 0;
595         char                    buf[32];
597         if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
598                 return -EFAULT;
600         if (!strncmp(buf, "SS.Disabled", 11))
601                 state = DWC3_LINK_STATE_SS_DIS;
602         else if (!strncmp(buf, "Rx.Detect", 9))
603                 state = DWC3_LINK_STATE_RX_DET;
604         else if (!strncmp(buf, "SS.Inactive", 11))
605                 state = DWC3_LINK_STATE_SS_INACT;
606         else if (!strncmp(buf, "Recovery", 8))
607                 state = DWC3_LINK_STATE_RECOV;
608         else if (!strncmp(buf, "Compliance", 10))
609                 state = DWC3_LINK_STATE_CMPLY;
610         else if (!strncmp(buf, "Loopback", 8))
611                 state = DWC3_LINK_STATE_LPBK;
612         else
613                 return -EINVAL;
615         spin_lock_irqsave(&dwc->lock, flags);
616         dwc3_gadget_set_link_state(dwc, state);
617         spin_unlock_irqrestore(&dwc->lock, flags);
619         return count;
622 static const struct file_operations dwc3_link_state_fops = {
623         .open                   = dwc3_link_state_open,
624         .write                  = dwc3_link_state_write,
625         .read                   = seq_read,
626         .llseek                 = seq_lseek,
627         .release                = single_release,
628 };
630 int dwc3_debugfs_init(struct dwc3 *dwc)
632         struct dentry           *root;
633         struct dentry           *file;
634         int                     ret;
636         root = debugfs_create_dir(dev_name(dwc->dev), NULL);
637         if (!root) {
638                 ret = -ENOMEM;
639                 goto err0;
640         }
642         dwc->root = root;
644         dwc->regset = kzalloc(sizeof(*dwc->regset), GFP_KERNEL);
645         if (!dwc->regset) {
646                 ret = -ENOMEM;
647                 goto err1;
648         }
650         dwc->regset->regs = dwc3_regs;
651         dwc->regset->nregs = ARRAY_SIZE(dwc3_regs);
652         dwc->regset->base = dwc->regs;
654         file = debugfs_create_regset32("regdump", S_IRUGO, root, dwc->regset);
655         if (!file) {
656                 ret = -ENOMEM;
657                 goto err1;
658         }
660         if (IS_ENABLED(CONFIG_USB_DWC3_DUAL_ROLE)) {
661                 file = debugfs_create_file("mode", S_IRUGO | S_IWUSR, root,
662                                 dwc, &dwc3_mode_fops);
663                 if (!file) {
664                         ret = -ENOMEM;
665                         goto err1;
666                 }
667         }
669         if (IS_ENABLED(CONFIG_USB_DWC3_DUAL_ROLE) ||
670                         IS_ENABLED(CONFIG_USB_DWC3_GADGET)) {
671                 file = debugfs_create_file("testmode", S_IRUGO | S_IWUSR, root,
672                                 dwc, &dwc3_testmode_fops);
673                 if (!file) {
674                         ret = -ENOMEM;
675                         goto err1;
676                 }
678                 file = debugfs_create_file("link_state", S_IRUGO | S_IWUSR, root,
679                                 dwc, &dwc3_link_state_fops);
680                 if (!file) {
681                         ret = -ENOMEM;
682                         goto err1;
683                 }
684         }
686         return 0;
688 err1:
689         debugfs_remove_recursive(root);
691 err0:
692         return ret;
695 void dwc3_debugfs_exit(struct dwc3 *dwc)
697         debugfs_remove_recursive(dwc->root);
698         dwc->root = NULL;