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