xref: /OK3568_Linux_fs/kernel/drivers/media/usb/pvrusb2/pvrusb2-debugifc.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  *  Copyright (C) 2005 Mike Isely <isely@pobox.com>
5*4882a593Smuzhiyun  */
6*4882a593Smuzhiyun 
7*4882a593Smuzhiyun #include <linux/string.h>
8*4882a593Smuzhiyun #include "pvrusb2-debugifc.h"
9*4882a593Smuzhiyun #include "pvrusb2-hdw.h"
10*4882a593Smuzhiyun #include "pvrusb2-debug.h"
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun struct debugifc_mask_item {
13*4882a593Smuzhiyun 	const char *name;
14*4882a593Smuzhiyun 	unsigned long msk;
15*4882a593Smuzhiyun };
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun 
debugifc_count_whitespace(const char * buf,unsigned int count)18*4882a593Smuzhiyun static unsigned int debugifc_count_whitespace(const char *buf,
19*4882a593Smuzhiyun 					      unsigned int count)
20*4882a593Smuzhiyun {
21*4882a593Smuzhiyun 	unsigned int scnt;
22*4882a593Smuzhiyun 	char ch;
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun 	for (scnt = 0; scnt < count; scnt++) {
25*4882a593Smuzhiyun 		ch = buf[scnt];
26*4882a593Smuzhiyun 		if (ch == ' ') continue;
27*4882a593Smuzhiyun 		if (ch == '\t') continue;
28*4882a593Smuzhiyun 		if (ch == '\n') continue;
29*4882a593Smuzhiyun 		break;
30*4882a593Smuzhiyun 	}
31*4882a593Smuzhiyun 	return scnt;
32*4882a593Smuzhiyun }
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun 
debugifc_count_nonwhitespace(const char * buf,unsigned int count)35*4882a593Smuzhiyun static unsigned int debugifc_count_nonwhitespace(const char *buf,
36*4882a593Smuzhiyun 						 unsigned int count)
37*4882a593Smuzhiyun {
38*4882a593Smuzhiyun 	unsigned int scnt;
39*4882a593Smuzhiyun 	char ch;
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun 	for (scnt = 0; scnt < count; scnt++) {
42*4882a593Smuzhiyun 		ch = buf[scnt];
43*4882a593Smuzhiyun 		if (ch == ' ') break;
44*4882a593Smuzhiyun 		if (ch == '\t') break;
45*4882a593Smuzhiyun 		if (ch == '\n') break;
46*4882a593Smuzhiyun 	}
47*4882a593Smuzhiyun 	return scnt;
48*4882a593Smuzhiyun }
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun 
debugifc_isolate_word(const char * buf,unsigned int count,const char ** wstrPtr,unsigned int * wlenPtr)51*4882a593Smuzhiyun static unsigned int debugifc_isolate_word(const char *buf,unsigned int count,
52*4882a593Smuzhiyun 					  const char **wstrPtr,
53*4882a593Smuzhiyun 					  unsigned int *wlenPtr)
54*4882a593Smuzhiyun {
55*4882a593Smuzhiyun 	const char *wptr;
56*4882a593Smuzhiyun 	unsigned int consume_cnt = 0;
57*4882a593Smuzhiyun 	unsigned int wlen;
58*4882a593Smuzhiyun 	unsigned int scnt;
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun 	wptr = NULL;
61*4882a593Smuzhiyun 	wlen = 0;
62*4882a593Smuzhiyun 	scnt = debugifc_count_whitespace(buf,count);
63*4882a593Smuzhiyun 	consume_cnt += scnt; count -= scnt; buf += scnt;
64*4882a593Smuzhiyun 	if (!count) goto done;
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun 	scnt = debugifc_count_nonwhitespace(buf,count);
67*4882a593Smuzhiyun 	if (!scnt) goto done;
68*4882a593Smuzhiyun 	wptr = buf;
69*4882a593Smuzhiyun 	wlen = scnt;
70*4882a593Smuzhiyun 	consume_cnt += scnt; count -= scnt; buf += scnt;
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun  done:
73*4882a593Smuzhiyun 	*wstrPtr = wptr;
74*4882a593Smuzhiyun 	*wlenPtr = wlen;
75*4882a593Smuzhiyun 	return consume_cnt;
76*4882a593Smuzhiyun }
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun 
debugifc_parse_unsigned_number(const char * buf,unsigned int count,u32 * num_ptr)79*4882a593Smuzhiyun static int debugifc_parse_unsigned_number(const char *buf,unsigned int count,
80*4882a593Smuzhiyun 					  u32 *num_ptr)
81*4882a593Smuzhiyun {
82*4882a593Smuzhiyun 	u32 result = 0;
83*4882a593Smuzhiyun 	int radix = 10;
84*4882a593Smuzhiyun 	if ((count >= 2) && (buf[0] == '0') &&
85*4882a593Smuzhiyun 	    ((buf[1] == 'x') || (buf[1] == 'X'))) {
86*4882a593Smuzhiyun 		radix = 16;
87*4882a593Smuzhiyun 		count -= 2;
88*4882a593Smuzhiyun 		buf += 2;
89*4882a593Smuzhiyun 	} else if ((count >= 1) && (buf[0] == '0')) {
90*4882a593Smuzhiyun 		radix = 8;
91*4882a593Smuzhiyun 	}
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun 	while (count--) {
94*4882a593Smuzhiyun 		int val = hex_to_bin(*buf++);
95*4882a593Smuzhiyun 		if (val < 0 || val >= radix)
96*4882a593Smuzhiyun 			return -EINVAL;
97*4882a593Smuzhiyun 		result *= radix;
98*4882a593Smuzhiyun 		result += val;
99*4882a593Smuzhiyun 	}
100*4882a593Smuzhiyun 	*num_ptr = result;
101*4882a593Smuzhiyun 	return 0;
102*4882a593Smuzhiyun }
103*4882a593Smuzhiyun 
104*4882a593Smuzhiyun 
debugifc_match_keyword(const char * buf,unsigned int count,const char * keyword)105*4882a593Smuzhiyun static int debugifc_match_keyword(const char *buf,unsigned int count,
106*4882a593Smuzhiyun 				  const char *keyword)
107*4882a593Smuzhiyun {
108*4882a593Smuzhiyun 	unsigned int kl;
109*4882a593Smuzhiyun 	if (!keyword) return 0;
110*4882a593Smuzhiyun 	kl = strlen(keyword);
111*4882a593Smuzhiyun 	if (kl != count) return 0;
112*4882a593Smuzhiyun 	return !memcmp(buf,keyword,kl);
113*4882a593Smuzhiyun }
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun 
pvr2_debugifc_print_info(struct pvr2_hdw * hdw,char * buf,unsigned int acnt)116*4882a593Smuzhiyun int pvr2_debugifc_print_info(struct pvr2_hdw *hdw,char *buf,unsigned int acnt)
117*4882a593Smuzhiyun {
118*4882a593Smuzhiyun 	int bcnt = 0;
119*4882a593Smuzhiyun 	int ccnt;
120*4882a593Smuzhiyun 	ccnt = scnprintf(buf, acnt, "Driver hardware description: %s\n",
121*4882a593Smuzhiyun 			 pvr2_hdw_get_desc(hdw));
122*4882a593Smuzhiyun 	bcnt += ccnt; acnt -= ccnt; buf += ccnt;
123*4882a593Smuzhiyun 	ccnt = scnprintf(buf,acnt,"Driver state info:\n");
124*4882a593Smuzhiyun 	bcnt += ccnt; acnt -= ccnt; buf += ccnt;
125*4882a593Smuzhiyun 	ccnt = pvr2_hdw_state_report(hdw,buf,acnt);
126*4882a593Smuzhiyun 	bcnt += ccnt; acnt -= ccnt; buf += ccnt;
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun 	return bcnt;
129*4882a593Smuzhiyun }
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun 
pvr2_debugifc_print_status(struct pvr2_hdw * hdw,char * buf,unsigned int acnt)132*4882a593Smuzhiyun int pvr2_debugifc_print_status(struct pvr2_hdw *hdw,
133*4882a593Smuzhiyun 			       char *buf,unsigned int acnt)
134*4882a593Smuzhiyun {
135*4882a593Smuzhiyun 	int bcnt = 0;
136*4882a593Smuzhiyun 	int ccnt;
137*4882a593Smuzhiyun 	int ret;
138*4882a593Smuzhiyun 	u32 gpio_dir,gpio_in,gpio_out;
139*4882a593Smuzhiyun 	struct pvr2_stream_stats stats;
140*4882a593Smuzhiyun 	struct pvr2_stream *sp;
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun 	ret = pvr2_hdw_is_hsm(hdw);
143*4882a593Smuzhiyun 	ccnt = scnprintf(buf,acnt,"USB link speed: %s\n",
144*4882a593Smuzhiyun 			 (ret < 0 ? "FAIL" : (ret ? "high" : "full")));
145*4882a593Smuzhiyun 	bcnt += ccnt; acnt -= ccnt; buf += ccnt;
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun 	gpio_dir = 0; gpio_in = 0; gpio_out = 0;
148*4882a593Smuzhiyun 	pvr2_hdw_gpio_get_dir(hdw,&gpio_dir);
149*4882a593Smuzhiyun 	pvr2_hdw_gpio_get_out(hdw,&gpio_out);
150*4882a593Smuzhiyun 	pvr2_hdw_gpio_get_in(hdw,&gpio_in);
151*4882a593Smuzhiyun 	ccnt = scnprintf(buf,acnt,"GPIO state: dir=0x%x in=0x%x out=0x%x\n",
152*4882a593Smuzhiyun 			 gpio_dir,gpio_in,gpio_out);
153*4882a593Smuzhiyun 	bcnt += ccnt; acnt -= ccnt; buf += ccnt;
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun 	ccnt = scnprintf(buf,acnt,"Streaming is %s\n",
156*4882a593Smuzhiyun 			 pvr2_hdw_get_streaming(hdw) ? "on" : "off");
157*4882a593Smuzhiyun 	bcnt += ccnt; acnt -= ccnt; buf += ccnt;
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun 	sp = pvr2_hdw_get_video_stream(hdw);
161*4882a593Smuzhiyun 	if (sp) {
162*4882a593Smuzhiyun 		pvr2_stream_get_stats(sp, &stats, 0);
163*4882a593Smuzhiyun 		ccnt = scnprintf(
164*4882a593Smuzhiyun 			buf,acnt,
165*4882a593Smuzhiyun 			"Bytes streamed=%u URBs: queued=%u idle=%u ready=%u processed=%u failed=%u\n",
166*4882a593Smuzhiyun 			stats.bytes_processed,
167*4882a593Smuzhiyun 			stats.buffers_in_queue,
168*4882a593Smuzhiyun 			stats.buffers_in_idle,
169*4882a593Smuzhiyun 			stats.buffers_in_ready,
170*4882a593Smuzhiyun 			stats.buffers_processed,
171*4882a593Smuzhiyun 			stats.buffers_failed);
172*4882a593Smuzhiyun 		bcnt += ccnt; acnt -= ccnt; buf += ccnt;
173*4882a593Smuzhiyun 	}
174*4882a593Smuzhiyun 
175*4882a593Smuzhiyun 	return bcnt;
176*4882a593Smuzhiyun }
177*4882a593Smuzhiyun 
178*4882a593Smuzhiyun 
pvr2_debugifc_do1cmd(struct pvr2_hdw * hdw,const char * buf,unsigned int count)179*4882a593Smuzhiyun static int pvr2_debugifc_do1cmd(struct pvr2_hdw *hdw,const char *buf,
180*4882a593Smuzhiyun 				unsigned int count)
181*4882a593Smuzhiyun {
182*4882a593Smuzhiyun 	const char *wptr;
183*4882a593Smuzhiyun 	unsigned int wlen;
184*4882a593Smuzhiyun 	unsigned int scnt;
185*4882a593Smuzhiyun 
186*4882a593Smuzhiyun 	scnt = debugifc_isolate_word(buf,count,&wptr,&wlen);
187*4882a593Smuzhiyun 	if (!scnt) return 0;
188*4882a593Smuzhiyun 	count -= scnt; buf += scnt;
189*4882a593Smuzhiyun 	if (!wptr) return 0;
190*4882a593Smuzhiyun 
191*4882a593Smuzhiyun 	pvr2_trace(PVR2_TRACE_DEBUGIFC,"debugifc cmd: \"%.*s\"",wlen,wptr);
192*4882a593Smuzhiyun 	if (debugifc_match_keyword(wptr,wlen,"reset")) {
193*4882a593Smuzhiyun 		scnt = debugifc_isolate_word(buf,count,&wptr,&wlen);
194*4882a593Smuzhiyun 		if (!scnt) return -EINVAL;
195*4882a593Smuzhiyun 		count -= scnt; buf += scnt;
196*4882a593Smuzhiyun 		if (!wptr) return -EINVAL;
197*4882a593Smuzhiyun 		if (debugifc_match_keyword(wptr,wlen,"cpu")) {
198*4882a593Smuzhiyun 			pvr2_hdw_cpureset_assert(hdw,!0);
199*4882a593Smuzhiyun 			pvr2_hdw_cpureset_assert(hdw,0);
200*4882a593Smuzhiyun 			return 0;
201*4882a593Smuzhiyun 		} else if (debugifc_match_keyword(wptr,wlen,"bus")) {
202*4882a593Smuzhiyun 			pvr2_hdw_device_reset(hdw);
203*4882a593Smuzhiyun 		} else if (debugifc_match_keyword(wptr,wlen,"soft")) {
204*4882a593Smuzhiyun 			return pvr2_hdw_cmd_powerup(hdw);
205*4882a593Smuzhiyun 		} else if (debugifc_match_keyword(wptr,wlen,"deep")) {
206*4882a593Smuzhiyun 			return pvr2_hdw_cmd_deep_reset(hdw);
207*4882a593Smuzhiyun 		} else if (debugifc_match_keyword(wptr,wlen,"firmware")) {
208*4882a593Smuzhiyun 			return pvr2_upload_firmware2(hdw);
209*4882a593Smuzhiyun 		} else if (debugifc_match_keyword(wptr,wlen,"decoder")) {
210*4882a593Smuzhiyun 			return pvr2_hdw_cmd_decoder_reset(hdw);
211*4882a593Smuzhiyun 		} else if (debugifc_match_keyword(wptr,wlen,"worker")) {
212*4882a593Smuzhiyun 			return pvr2_hdw_untrip(hdw);
213*4882a593Smuzhiyun 		} else if (debugifc_match_keyword(wptr,wlen,"usbstats")) {
214*4882a593Smuzhiyun 			pvr2_stream_get_stats(pvr2_hdw_get_video_stream(hdw),
215*4882a593Smuzhiyun 					      NULL, !0);
216*4882a593Smuzhiyun 			return 0;
217*4882a593Smuzhiyun 		}
218*4882a593Smuzhiyun 		return -EINVAL;
219*4882a593Smuzhiyun 	} else if (debugifc_match_keyword(wptr,wlen,"cpufw")) {
220*4882a593Smuzhiyun 		scnt = debugifc_isolate_word(buf,count,&wptr,&wlen);
221*4882a593Smuzhiyun 		if (!scnt) return -EINVAL;
222*4882a593Smuzhiyun 		count -= scnt; buf += scnt;
223*4882a593Smuzhiyun 		if (!wptr) return -EINVAL;
224*4882a593Smuzhiyun 		if (debugifc_match_keyword(wptr,wlen,"fetch")) {
225*4882a593Smuzhiyun 			scnt = debugifc_isolate_word(buf,count,&wptr,&wlen);
226*4882a593Smuzhiyun 			if (scnt && wptr) {
227*4882a593Smuzhiyun 				count -= scnt; buf += scnt;
228*4882a593Smuzhiyun 				if (debugifc_match_keyword(wptr, wlen,
229*4882a593Smuzhiyun 							   "prom")) {
230*4882a593Smuzhiyun 					pvr2_hdw_cpufw_set_enabled(hdw, 2, !0);
231*4882a593Smuzhiyun 				} else if (debugifc_match_keyword(wptr, wlen,
232*4882a593Smuzhiyun 								  "ram8k")) {
233*4882a593Smuzhiyun 					pvr2_hdw_cpufw_set_enabled(hdw, 0, !0);
234*4882a593Smuzhiyun 				} else if (debugifc_match_keyword(wptr, wlen,
235*4882a593Smuzhiyun 								  "ram16k")) {
236*4882a593Smuzhiyun 					pvr2_hdw_cpufw_set_enabled(hdw, 1, !0);
237*4882a593Smuzhiyun 				} else {
238*4882a593Smuzhiyun 					return -EINVAL;
239*4882a593Smuzhiyun 				}
240*4882a593Smuzhiyun 			}
241*4882a593Smuzhiyun 			pvr2_hdw_cpufw_set_enabled(hdw,0,!0);
242*4882a593Smuzhiyun 			return 0;
243*4882a593Smuzhiyun 		} else if (debugifc_match_keyword(wptr,wlen,"done")) {
244*4882a593Smuzhiyun 			pvr2_hdw_cpufw_set_enabled(hdw,0,0);
245*4882a593Smuzhiyun 			return 0;
246*4882a593Smuzhiyun 		} else {
247*4882a593Smuzhiyun 			return -EINVAL;
248*4882a593Smuzhiyun 		}
249*4882a593Smuzhiyun 	} else if (debugifc_match_keyword(wptr,wlen,"gpio")) {
250*4882a593Smuzhiyun 		int dir_fl = 0;
251*4882a593Smuzhiyun 		int ret;
252*4882a593Smuzhiyun 		u32 msk,val;
253*4882a593Smuzhiyun 		scnt = debugifc_isolate_word(buf,count,&wptr,&wlen);
254*4882a593Smuzhiyun 		if (!scnt) return -EINVAL;
255*4882a593Smuzhiyun 		count -= scnt; buf += scnt;
256*4882a593Smuzhiyun 		if (!wptr) return -EINVAL;
257*4882a593Smuzhiyun 		if (debugifc_match_keyword(wptr,wlen,"dir")) {
258*4882a593Smuzhiyun 			dir_fl = !0;
259*4882a593Smuzhiyun 		} else if (!debugifc_match_keyword(wptr,wlen,"out")) {
260*4882a593Smuzhiyun 			return -EINVAL;
261*4882a593Smuzhiyun 		}
262*4882a593Smuzhiyun 		scnt = debugifc_isolate_word(buf,count,&wptr,&wlen);
263*4882a593Smuzhiyun 		if (!scnt) return -EINVAL;
264*4882a593Smuzhiyun 		count -= scnt; buf += scnt;
265*4882a593Smuzhiyun 		if (!wptr) return -EINVAL;
266*4882a593Smuzhiyun 		ret = debugifc_parse_unsigned_number(wptr,wlen,&msk);
267*4882a593Smuzhiyun 		if (ret) return ret;
268*4882a593Smuzhiyun 		scnt = debugifc_isolate_word(buf,count,&wptr,&wlen);
269*4882a593Smuzhiyun 		if (wptr) {
270*4882a593Smuzhiyun 			ret = debugifc_parse_unsigned_number(wptr,wlen,&val);
271*4882a593Smuzhiyun 			if (ret) return ret;
272*4882a593Smuzhiyun 		} else {
273*4882a593Smuzhiyun 			val = msk;
274*4882a593Smuzhiyun 			msk = 0xffffffff;
275*4882a593Smuzhiyun 		}
276*4882a593Smuzhiyun 		if (dir_fl) {
277*4882a593Smuzhiyun 			ret = pvr2_hdw_gpio_chg_dir(hdw,msk,val);
278*4882a593Smuzhiyun 		} else {
279*4882a593Smuzhiyun 			ret = pvr2_hdw_gpio_chg_out(hdw,msk,val);
280*4882a593Smuzhiyun 		}
281*4882a593Smuzhiyun 		return ret;
282*4882a593Smuzhiyun 	}
283*4882a593Smuzhiyun 	pvr2_trace(PVR2_TRACE_DEBUGIFC,
284*4882a593Smuzhiyun 		   "debugifc failed to recognize cmd: \"%.*s\"",wlen,wptr);
285*4882a593Smuzhiyun 	return -EINVAL;
286*4882a593Smuzhiyun }
287*4882a593Smuzhiyun 
288*4882a593Smuzhiyun 
pvr2_debugifc_docmd(struct pvr2_hdw * hdw,const char * buf,unsigned int count)289*4882a593Smuzhiyun int pvr2_debugifc_docmd(struct pvr2_hdw *hdw,const char *buf,
290*4882a593Smuzhiyun 			unsigned int count)
291*4882a593Smuzhiyun {
292*4882a593Smuzhiyun 	unsigned int bcnt = 0;
293*4882a593Smuzhiyun 	int ret;
294*4882a593Smuzhiyun 
295*4882a593Smuzhiyun 	while (count) {
296*4882a593Smuzhiyun 		for (bcnt = 0; bcnt < count; bcnt++) {
297*4882a593Smuzhiyun 			if (buf[bcnt] == '\n') break;
298*4882a593Smuzhiyun 		}
299*4882a593Smuzhiyun 
300*4882a593Smuzhiyun 		ret = pvr2_debugifc_do1cmd(hdw,buf,bcnt);
301*4882a593Smuzhiyun 		if (ret < 0) return ret;
302*4882a593Smuzhiyun 		if (bcnt < count) bcnt++;
303*4882a593Smuzhiyun 		buf += bcnt;
304*4882a593Smuzhiyun 		count -= bcnt;
305*4882a593Smuzhiyun 	}
306*4882a593Smuzhiyun 
307*4882a593Smuzhiyun 	return 0;
308*4882a593Smuzhiyun }
309