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>
10#include <linux/usb/phy.h>
11#include <linux/usb/otg.h>
12#include <linux/usb/otg-fsm.h>
13
14#include "ci.h"
15#include "udc.h"
16#include "bits.h"
17#include "debug.h"
18#include "otg.h"
19
20/**
21 * ci_device_show: prints information about device capabilities and status
22 */
23static int ci_device_show(struct seq_file *s, void *data)
24{
25	struct ci_hdrc *ci = s->private;
26	struct usb_gadget *gadget = &ci->gadget;
27
28	seq_printf(s, "speed             = %d\n", gadget->speed);
29	seq_printf(s, "max_speed         = %d\n", gadget->max_speed);
30	seq_printf(s, "is_otg            = %d\n", gadget->is_otg);
31	seq_printf(s, "is_a_peripheral   = %d\n", gadget->is_a_peripheral);
32	seq_printf(s, "b_hnp_enable      = %d\n", gadget->b_hnp_enable);
33	seq_printf(s, "a_hnp_support     = %d\n", gadget->a_hnp_support);
34	seq_printf(s, "a_alt_hnp_support = %d\n", gadget->a_alt_hnp_support);
35	seq_printf(s, "name              = %s\n",
36		   (gadget->name ? gadget->name : ""));
37
38	if (!ci->driver)
39		return 0;
40
41	seq_printf(s, "gadget function   = %s\n",
42		       (ci->driver->function ? ci->driver->function : ""));
43	seq_printf(s, "gadget max speed  = %d\n", ci->driver->max_speed);
44
45	return 0;
46}
47
48static int ci_device_open(struct inode *inode, struct file *file)
49{
50	return single_open(file, ci_device_show, inode->i_private);
51}
52
53static const struct file_operations ci_device_fops = {
54	.open		= ci_device_open,
55	.read		= seq_read,
56	.llseek		= seq_lseek,
57	.release	= single_release,
58};
59
60/**
61 * ci_port_test_show: reads port test mode
62 */
63static int ci_port_test_show(struct seq_file *s, void *data)
64{
65	struct ci_hdrc *ci = s->private;
66	unsigned long flags;
67	unsigned mode;
68
69	spin_lock_irqsave(&ci->lock, flags);
70	mode = hw_port_test_get(ci);
71	spin_unlock_irqrestore(&ci->lock, flags);
72
73	seq_printf(s, "mode = %u\n", mode);
74
75	return 0;
76}
77
78/**
79 * ci_port_test_write: writes port test mode
80 */
81static ssize_t ci_port_test_write(struct file *file, const char __user *ubuf,
82				  size_t count, loff_t *ppos)
83{
84	struct seq_file *s = file->private_data;
85	struct ci_hdrc *ci = s->private;
86	unsigned long flags;
87	unsigned mode;
88	char buf[32];
89	int ret;
90
91	count = min_t(size_t, sizeof(buf) - 1, count);
92	if (copy_from_user(buf, ubuf, count))
93		return -EFAULT;
94
95	/* sscanf requires a zero terminated string */
96	buf[count] = '\0';
97
98	if (sscanf(buf, "%u", &mode) != 1)
99		return -EINVAL;
100
101	spin_lock_irqsave(&ci->lock, flags);
102	ret = hw_port_test_set(ci, mode);
103	spin_unlock_irqrestore(&ci->lock, flags);
104
105	return ret ? ret : count;
106}
107
108static int ci_port_test_open(struct inode *inode, struct file *file)
109{
110	return single_open(file, ci_port_test_show, inode->i_private);
111}
112
113static const struct file_operations ci_port_test_fops = {
114	.open		= ci_port_test_open,
115	.write		= ci_port_test_write,
116	.read		= seq_read,
117	.llseek		= seq_lseek,
118	.release	= single_release,
119};
120
121/**
122 * ci_qheads_show: DMA contents of all queue heads
123 */
124static int ci_qheads_show(struct seq_file *s, void *data)
125{
126	struct ci_hdrc *ci = s->private;
127	unsigned long flags;
128	unsigned i, j;
129
130	if (ci->role != CI_ROLE_GADGET) {
131		seq_printf(s, "not in gadget mode\n");
132		return 0;
133	}
134
135	spin_lock_irqsave(&ci->lock, flags);
136	for (i = 0; i < ci->hw_ep_max/2; i++) {
137		struct ci_hw_ep *hweprx = &ci->ci_hw_ep[i];
138		struct ci_hw_ep *hweptx =
139			&ci->ci_hw_ep[i + ci->hw_ep_max/2];
140		seq_printf(s, "EP=%02i: RX=%08X TX=%08X\n",
141			   i, (u32)hweprx->qh.dma, (u32)hweptx->qh.dma);
142		for (j = 0; j < (sizeof(struct ci_hw_qh)/sizeof(u32)); j++)
143			seq_printf(s, " %04X:    %08X    %08X\n", j,
144				   *((u32 *)hweprx->qh.ptr + j),
145				   *((u32 *)hweptx->qh.ptr + j));
146	}
147	spin_unlock_irqrestore(&ci->lock, flags);
148
149	return 0;
150}
151
152static int ci_qheads_open(struct inode *inode, struct file *file)
153{
154	return single_open(file, ci_qheads_show, inode->i_private);
155}
156
157static const struct file_operations ci_qheads_fops = {
158	.open		= ci_qheads_open,
159	.read		= seq_read,
160	.llseek		= seq_lseek,
161	.release	= single_release,
162};
163
164/**
165 * ci_requests_show: DMA contents of all requests currently queued (all endpts)
166 */
167static int ci_requests_show(struct seq_file *s, void *data)
168{
169	struct ci_hdrc *ci = s->private;
170	unsigned long flags;
171	struct list_head   *ptr = NULL;
172	struct ci_hw_req *req = NULL;
173	struct td_node *node, *tmpnode;
174	unsigned i, j, qsize = sizeof(struct ci_hw_td)/sizeof(u32);
175
176	if (ci->role != CI_ROLE_GADGET) {
177		seq_printf(s, "not in gadget mode\n");
178		return 0;
179	}
180
181	spin_lock_irqsave(&ci->lock, flags);
182	for (i = 0; i < ci->hw_ep_max; i++)
183		list_for_each(ptr, &ci->ci_hw_ep[i].qh.queue) {
184			req = list_entry(ptr, struct ci_hw_req, queue);
185
186			list_for_each_entry_safe(node, tmpnode, &req->tds, td) {
187				seq_printf(s, "EP=%02i: TD=%08X %s\n",
188					   i % (ci->hw_ep_max / 2),
189					   (u32)node->dma,
190					   ((i < ci->hw_ep_max/2) ?
191					   "RX" : "TX"));
192
193				for (j = 0; j < qsize; j++)
194					seq_printf(s, " %04X:    %08X\n", j,
195						   *((u32 *)node->ptr + j));
196			}
197		}
198	spin_unlock_irqrestore(&ci->lock, flags);
199
200	return 0;
201}
202
203static int ci_requests_open(struct inode *inode, struct file *file)
204{
205	return single_open(file, ci_requests_show, inode->i_private);
206}
207
208static const struct file_operations ci_requests_fops = {
209	.open		= ci_requests_open,
210	.read		= seq_read,
211	.llseek		= seq_lseek,
212	.release	= single_release,
213};
214
215static int ci_otg_show(struct seq_file *s, void *unused)
216{
217	struct ci_hdrc *ci = s->private;
218	struct otg_fsm *fsm;
219
220	if (!ci || !ci_otg_is_fsm_mode(ci))
221		return 0;
222
223	fsm = &ci->fsm;
224
225	/* ------ State ----- */
226	seq_printf(s, "OTG state: %s\n\n",
227			usb_otg_state_string(ci->otg.state));
228
229	/* ------ State Machine Variables ----- */
230	seq_printf(s, "a_bus_drop: %d\n", fsm->a_bus_drop);
231
232	seq_printf(s, "a_bus_req: %d\n", fsm->a_bus_req);
233
234	seq_printf(s, "a_srp_det: %d\n", fsm->a_srp_det);
235
236	seq_printf(s, "a_vbus_vld: %d\n", fsm->a_vbus_vld);
237
238	seq_printf(s, "b_conn: %d\n", fsm->b_conn);
239
240	seq_printf(s, "adp_change: %d\n", fsm->adp_change);
241
242	seq_printf(s, "power_up: %d\n", fsm->power_up);
243
244	seq_printf(s, "a_bus_resume: %d\n", fsm->a_bus_resume);
245
246	seq_printf(s, "a_bus_suspend: %d\n", fsm->a_bus_suspend);
247
248	seq_printf(s, "a_conn: %d\n", fsm->a_conn);
249
250	seq_printf(s, "b_bus_req: %d\n", fsm->b_bus_req);
251
252	seq_printf(s, "b_bus_suspend: %d\n", fsm->b_bus_suspend);
253
254	seq_printf(s, "b_se0_srp: %d\n", fsm->b_se0_srp);
255
256	seq_printf(s, "b_ssend_srp: %d\n", fsm->b_ssend_srp);
257
258	seq_printf(s, "b_sess_vld: %d\n", fsm->b_sess_vld);
259
260	seq_printf(s, "b_srp_done: %d\n", fsm->b_srp_done);
261
262	seq_printf(s, "drv_vbus: %d\n", fsm->drv_vbus);
263
264	seq_printf(s, "loc_conn: %d\n", fsm->loc_conn);
265
266	seq_printf(s, "loc_sof: %d\n", fsm->loc_sof);
267
268	seq_printf(s, "adp_prb: %d\n", fsm->adp_prb);
269
270	seq_printf(s, "id: %d\n", fsm->id);
271
272	seq_printf(s, "protocol: %d\n", fsm->protocol);
273
274	return 0;
275}
276
277static int ci_otg_open(struct inode *inode, struct file *file)
278{
279	return single_open(file, ci_otg_show, inode->i_private);
280}
281
282static const struct file_operations ci_otg_fops = {
283	.open			= ci_otg_open,
284	.read			= seq_read,
285	.llseek			= seq_lseek,
286	.release		= single_release,
287};
288
289static int ci_role_show(struct seq_file *s, void *data)
290{
291	struct ci_hdrc *ci = s->private;
292
293	seq_printf(s, "%s\n", ci_role(ci)->name);
294
295	return 0;
296}
297
298static ssize_t ci_role_write(struct file *file, const char __user *ubuf,
299			     size_t count, loff_t *ppos)
300{
301	struct seq_file *s = file->private_data;
302	struct ci_hdrc *ci = s->private;
303	enum ci_role role;
304	char buf[8];
305	int ret;
306
307	if (copy_from_user(buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
308		return -EFAULT;
309
310	for (role = CI_ROLE_HOST; role < CI_ROLE_END; role++)
311		if (ci->roles[role] &&
312		    !strncmp(buf, ci->roles[role]->name,
313			     strlen(ci->roles[role]->name)))
314			break;
315
316	if (role == CI_ROLE_END || role == ci->role)
317		return -EINVAL;
318
319	ci_role_stop(ci);
320	ret = ci_role_start(ci, role);
321
322	return ret ? ret : count;
323}
324
325static int ci_role_open(struct inode *inode, struct file *file)
326{
327	return single_open(file, ci_role_show, inode->i_private);
328}
329
330static const struct file_operations ci_role_fops = {
331	.open		= ci_role_open,
332	.write		= ci_role_write,
333	.read		= seq_read,
334	.llseek		= seq_lseek,
335	.release	= single_release,
336};
337
338static int ci_registers_show(struct seq_file *s, void *unused)
339{
340	struct ci_hdrc *ci = s->private;
341	u32 tmp_reg;
342
343	if (!ci || ci->in_lpm)
344		return -EPERM;
345
346	/* ------ Registers ----- */
347	tmp_reg = hw_read_intr_enable(ci);
348	seq_printf(s, "USBINTR reg: %08x\n", tmp_reg);
349
350	tmp_reg = hw_read_intr_status(ci);
351	seq_printf(s, "USBSTS reg: %08x\n", tmp_reg);
352
353	tmp_reg = hw_read(ci, OP_USBMODE, ~0);
354	seq_printf(s, "USBMODE reg: %08x\n", tmp_reg);
355
356	tmp_reg = hw_read(ci, OP_USBCMD, ~0);
357	seq_printf(s, "USBCMD reg: %08x\n", tmp_reg);
358
359	tmp_reg = hw_read(ci, OP_PORTSC, ~0);
360	seq_printf(s, "PORTSC reg: %08x\n", tmp_reg);
361
362	if (ci->is_otg) {
363		tmp_reg = hw_read_otgsc(ci, ~0);
364		seq_printf(s, "OTGSC reg: %08x\n", tmp_reg);
365	}
366
367	return 0;
368}
369
370static int ci_registers_open(struct inode *inode, struct file *file)
371{
372	return single_open(file, ci_registers_show, inode->i_private);
373}
374
375static const struct file_operations ci_registers_fops = {
376	.open			= ci_registers_open,
377	.read			= seq_read,
378	.llseek			= seq_lseek,
379	.release		= single_release,
380};
381
382/**
383 * dbg_create_files: initializes the attribute interface
384 * @ci: device
385 *
386 * This function returns an error code
387 */
388int dbg_create_files(struct ci_hdrc *ci)
389{
390	struct dentry *dent;
391
392	ci->debugfs = debugfs_create_dir(dev_name(ci->dev), NULL);
393	if (!ci->debugfs)
394		return -ENOMEM;
395
396	dent = debugfs_create_file("device", S_IRUGO, ci->debugfs, ci,
397				   &ci_device_fops);
398	if (!dent)
399		goto err;
400
401	dent = debugfs_create_file("port_test", S_IRUGO | S_IWUSR, ci->debugfs,
402				   ci, &ci_port_test_fops);
403	if (!dent)
404		goto err;
405
406	dent = debugfs_create_file("qheads", S_IRUGO, ci->debugfs, ci,
407				   &ci_qheads_fops);
408	if (!dent)
409		goto err;
410
411	dent = debugfs_create_file("requests", S_IRUGO, ci->debugfs, ci,
412				   &ci_requests_fops);
413	if (!dent)
414		goto err;
415
416	if (ci_otg_is_fsm_mode(ci)) {
417		dent = debugfs_create_file("otg", S_IRUGO, ci->debugfs, ci,
418					&ci_otg_fops);
419		if (!dent)
420			goto err;
421	}
422
423	dent = debugfs_create_file("role", S_IRUGO | S_IWUSR, ci->debugfs, ci,
424				   &ci_role_fops);
425	if (!dent)
426		goto err;
427
428	dent = debugfs_create_file("registers", S_IRUGO, ci->debugfs, ci,
429				&ci_registers_fops);
430
431	if (dent)
432		return 0;
433err:
434	debugfs_remove_recursive(ci->debugfs);
435	return -ENOMEM;
436}
437
438/**
439 * dbg_remove_files: destroys the attribute interface
440 * @ci: device
441 */
442void dbg_remove_files(struct ci_hdrc *ci)
443{
444	debugfs_remove_recursive(ci->debugfs);
445}
446