1 #include <linux/kernel.h>
2 #include <linux/device.h>
3 #include <linux/types.h>
4 #include <linux/spinlock.h>
5 #include <linux/debugfs.h>
6 #include <linux/seq_file.h>
7 #include <linux/uaccess.h>
8 #include <linux/usb/ch9.h>
9 #include <linux/usb/gadget.h>
18 * ci_device_show: prints information about device capabilities and status
20 static int ci_device_show(struct seq_file *s, void *data)
22 struct ci_hdrc *ci = s->private;
23 struct usb_gadget *gadget = &ci->gadget;
25 seq_printf(s, "speed = %d\n", gadget->speed);
26 seq_printf(s, "max_speed = %d\n", gadget->max_speed);
27 seq_printf(s, "is_otg = %d\n", gadget->is_otg);
28 seq_printf(s, "is_a_peripheral = %d\n", gadget->is_a_peripheral);
29 seq_printf(s, "b_hnp_enable = %d\n", gadget->b_hnp_enable);
30 seq_printf(s, "a_hnp_support = %d\n", gadget->a_hnp_support);
31 seq_printf(s, "a_alt_hnp_support = %d\n", gadget->a_alt_hnp_support);
32 seq_printf(s, "name = %s\n",
33 (gadget->name ? gadget->name : ""));
38 seq_printf(s, "gadget function = %s\n",
39 (ci->driver->function ? ci->driver->function : ""));
40 seq_printf(s, "gadget max speed = %d\n", ci->driver->max_speed);
45 static int ci_device_open(struct inode *inode, struct file *file)
47 return single_open(file, ci_device_show, inode->i_private);
50 static const struct file_operations ci_device_fops = {
51 .open = ci_device_open,
54 .release = single_release,
58 * ci_port_test_show: reads port test mode
60 static int ci_port_test_show(struct seq_file *s, void *data)
62 struct ci_hdrc *ci = s->private;
66 spin_lock_irqsave(&ci->lock, flags);
67 mode = hw_port_test_get(ci);
68 spin_unlock_irqrestore(&ci->lock, flags);
70 seq_printf(s, "mode = %u\n", mode);
76 * ci_port_test_write: writes port test mode
78 static ssize_t ci_port_test_write(struct file *file, const char __user *ubuf,
79 size_t count, loff_t *ppos)
81 struct seq_file *s = file->private_data;
82 struct ci_hdrc *ci = s->private;
88 if (copy_from_user(buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
91 if (sscanf(buf, "%u", &mode) != 1)
94 spin_lock_irqsave(&ci->lock, flags);
95 ret = hw_port_test_set(ci, mode);
96 spin_unlock_irqrestore(&ci->lock, flags);
98 return ret ? ret : count;
101 static int ci_port_test_open(struct inode *inode, struct file *file)
103 return single_open(file, ci_port_test_show, inode->i_private);
106 static const struct file_operations ci_port_test_fops = {
107 .open = ci_port_test_open,
108 .write = ci_port_test_write,
111 .release = single_release,
115 * ci_qheads_show: DMA contents of all queue heads
117 static int ci_qheads_show(struct seq_file *s, void *data)
119 struct ci_hdrc *ci = s->private;
123 if (ci->role != CI_ROLE_GADGET) {
124 seq_printf(s, "not in gadget mode\n");
128 spin_lock_irqsave(&ci->lock, flags);
129 for (i = 0; i < ci->hw_ep_max/2; i++) {
130 struct ci_hw_ep *hweprx = &ci->ci_hw_ep[i];
131 struct ci_hw_ep *hweptx =
132 &ci->ci_hw_ep[i + ci->hw_ep_max/2];
133 seq_printf(s, "EP=%02i: RX=%08X TX=%08X\n",
134 i, (u32)hweprx->qh.dma, (u32)hweptx->qh.dma);
135 for (j = 0; j < (sizeof(struct ci_hw_qh)/sizeof(u32)); j++)
136 seq_printf(s, " %04X: %08X %08X\n", j,
137 *((u32 *)hweprx->qh.ptr + j),
138 *((u32 *)hweptx->qh.ptr + j));
140 spin_unlock_irqrestore(&ci->lock, flags);
145 static int ci_qheads_open(struct inode *inode, struct file *file)
147 return single_open(file, ci_qheads_show, inode->i_private);
150 static const struct file_operations ci_qheads_fops = {
151 .open = ci_qheads_open,
154 .release = single_release,
158 * ci_requests_show: DMA contents of all requests currently queued (all endpts)
160 static int ci_requests_show(struct seq_file *s, void *data)
162 struct ci_hdrc *ci = s->private;
164 struct list_head *ptr = NULL;
165 struct ci_hw_req *req = NULL;
166 struct td_node *node, *tmpnode;
167 unsigned i, j, qsize = sizeof(struct ci_hw_td)/sizeof(u32);
169 if (ci->role != CI_ROLE_GADGET) {
170 seq_printf(s, "not in gadget mode\n");
174 spin_lock_irqsave(&ci->lock, flags);
175 for (i = 0; i < ci->hw_ep_max; i++)
176 list_for_each(ptr, &ci->ci_hw_ep[i].qh.queue) {
177 req = list_entry(ptr, struct ci_hw_req, queue);
179 list_for_each_entry_safe(node, tmpnode, &req->tds, td) {
180 seq_printf(s, "EP=%02i: TD=%08X %s\n",
181 i % (ci->hw_ep_max / 2),
183 ((i < ci->hw_ep_max/2) ?
186 for (j = 0; j < qsize; j++)
187 seq_printf(s, " %04X: %08X\n", j,
188 *((u32 *)node->ptr + j));
191 spin_unlock_irqrestore(&ci->lock, flags);
196 static int ci_requests_open(struct inode *inode, struct file *file)
198 return single_open(file, ci_requests_show, inode->i_private);
201 static const struct file_operations ci_requests_fops = {
202 .open = ci_requests_open,
205 .release = single_release,
208 static int ci_role_show(struct seq_file *s, void *data)
210 struct ci_hdrc *ci = s->private;
212 seq_printf(s, "%s\n", ci_role(ci)->name);
217 static ssize_t ci_role_write(struct file *file, const char __user *ubuf,
218 size_t count, loff_t *ppos)
220 struct seq_file *s = file->private_data;
221 struct ci_hdrc *ci = s->private;
226 if (copy_from_user(buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
229 for (role = CI_ROLE_HOST; role < CI_ROLE_END; role++)
230 if (ci->roles[role] &&
231 !strncmp(buf, ci->roles[role]->name,
232 strlen(ci->roles[role]->name)))
235 if (role == CI_ROLE_END || role == ci->role)
239 ret = ci_role_start(ci, role);
241 return ret ? ret : count;
244 static int ci_role_open(struct inode *inode, struct file *file)
246 return single_open(file, ci_role_show, inode->i_private);
249 static const struct file_operations ci_role_fops = {
250 .open = ci_role_open,
251 .write = ci_role_write,
254 .release = single_release,
257 int ci_registers_show(struct seq_file *s, void *unused)
259 struct ci_hdrc *ci = s->private;
265 /* ------ Registers ----- */
266 tmp_reg = hw_read_intr_enable(ci);
267 seq_printf(s, "USBINTR reg: %08x\n", tmp_reg);
269 tmp_reg = hw_read_intr_status(ci);
270 seq_printf(s, "USBSTS reg: %08x\n", tmp_reg);
272 tmp_reg = hw_read(ci, OP_USBMODE, ~0);
273 seq_printf(s, "USBMODE reg: %08x\n", tmp_reg);
275 tmp_reg = hw_read(ci, OP_USBCMD, ~0);
276 seq_printf(s, "USBCMD reg: %08x\n", tmp_reg);
278 tmp_reg = hw_read(ci, OP_PORTSC, ~0);
279 seq_printf(s, "PORTSC reg: %08x\n", tmp_reg);
282 tmp_reg = hw_read_otgsc(ci, ~0);
283 seq_printf(s, "OTGSC reg: %08x\n", tmp_reg);
289 static int ci_registers_open(struct inode *inode, struct file *file)
291 return single_open(file, ci_registers_show, inode->i_private);
294 static const struct file_operations ci_registers_fops = {
295 .open = ci_registers_open,
298 .release = single_release,
302 * dbg_create_files: initializes the attribute interface
305 * This function returns an error code
307 int dbg_create_files(struct ci_hdrc *ci)
311 ci->debugfs = debugfs_create_dir(dev_name(ci->dev), NULL);
315 dent = debugfs_create_file("device", S_IRUGO, ci->debugfs, ci,
320 dent = debugfs_create_file("port_test", S_IRUGO | S_IWUSR, ci->debugfs,
321 ci, &ci_port_test_fops);
325 dent = debugfs_create_file("qheads", S_IRUGO, ci->debugfs, ci,
330 dent = debugfs_create_file("requests", S_IRUGO, ci->debugfs, ci,
335 dent = debugfs_create_file("role", S_IRUGO | S_IWUSR, ci->debugfs, ci,
340 dent = debugfs_create_file("registers", S_IRUGO, ci->debugfs, ci,
346 debugfs_remove_recursive(ci->debugfs);
351 * dbg_remove_files: destroys the attribute interface
354 void dbg_remove_files(struct ci_hdrc *ci)
356 debugfs_remove_recursive(ci->debugfs);