blob: 010018bc8383158a37cad7472daa2d2ea4bdcb8a [file] [log] [blame]
Mike Iselyd8554972006-06-26 20:58:46 -03001/*
2 *
Mike Iselyd8554972006-06-26 20:58:46 -03003 *
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 <linux/slab.h>
23#include "pvrusb2-debugifc.h"
24#include "pvrusb2-hdw.h"
25#include "pvrusb2-debug.h"
Mike Iselyd8554972006-06-26 20:58:46 -030026
27struct debugifc_mask_item {
28 const char *name;
29 unsigned long msk;
30};
31
Mike Iselyd8554972006-06-26 20:58:46 -030032
33static unsigned int debugifc_count_whitespace(const char *buf,
34 unsigned int count)
35{
36 unsigned int scnt;
37 char ch;
38
39 for (scnt = 0; scnt < count; scnt++) {
40 ch = buf[scnt];
41 if (ch == ' ') continue;
42 if (ch == '\t') continue;
43 if (ch == '\n') continue;
44 break;
45 }
46 return scnt;
47}
48
49
50static unsigned int debugifc_count_nonwhitespace(const char *buf,
51 unsigned int count)
52{
53 unsigned int scnt;
54 char ch;
55
56 for (scnt = 0; scnt < count; scnt++) {
57 ch = buf[scnt];
58 if (ch == ' ') break;
59 if (ch == '\t') break;
60 if (ch == '\n') break;
61 }
62 return scnt;
63}
64
65
66static unsigned int debugifc_isolate_word(const char *buf,unsigned int count,
67 const char **wstrPtr,
68 unsigned int *wlenPtr)
69{
70 const char *wptr;
71 unsigned int consume_cnt = 0;
72 unsigned int wlen;
73 unsigned int scnt;
74
Mike Iselya0fd1cb2006-06-30 11:35:28 -030075 wptr = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -030076 wlen = 0;
77 scnt = debugifc_count_whitespace(buf,count);
78 consume_cnt += scnt; count -= scnt; buf += scnt;
79 if (!count) goto done;
80
81 scnt = debugifc_count_nonwhitespace(buf,count);
82 if (!scnt) goto done;
83 wptr = buf;
84 wlen = scnt;
85 consume_cnt += scnt; count -= scnt; buf += scnt;
86
87 done:
88 *wstrPtr = wptr;
89 *wlenPtr = wlen;
90 return consume_cnt;
91}
92
93
94static int debugifc_parse_unsigned_number(const char *buf,unsigned int count,
95 u32 *num_ptr)
96{
97 u32 result = 0;
98 u32 val;
99 int ch;
100 int radix = 10;
101 if ((count >= 2) && (buf[0] == '0') &&
102 ((buf[1] == 'x') || (buf[1] == 'X'))) {
103 radix = 16;
104 count -= 2;
105 buf += 2;
106 } else if ((count >= 1) && (buf[0] == '0')) {
107 radix = 8;
108 }
109
110 while (count--) {
111 ch = *buf++;
112 if ((ch >= '0') && (ch <= '9')) {
113 val = ch - '0';
114 } else if ((ch >= 'a') && (ch <= 'f')) {
115 val = ch - 'a' + 10;
116 } else if ((ch >= 'A') && (ch <= 'F')) {
117 val = ch - 'A' + 10;
118 } else {
119 return -EINVAL;
120 }
121 if (val >= radix) return -EINVAL;
122 result *= radix;
123 result += val;
124 }
125 *num_ptr = result;
126 return 0;
127}
128
129
130static int debugifc_match_keyword(const char *buf,unsigned int count,
131 const char *keyword)
132{
133 unsigned int kl;
134 if (!keyword) return 0;
135 kl = strlen(keyword);
136 if (kl != count) return 0;
137 return !memcmp(buf,keyword,kl);
138}
139
140
Mike Iselyd8554972006-06-26 20:58:46 -0300141int pvr2_debugifc_print_info(struct pvr2_hdw *hdw,char *buf,unsigned int acnt)
142{
143 int bcnt = 0;
144 int ccnt;
Mike Iselyaa976ca2009-10-12 00:25:09 -0300145 ccnt = scnprintf(buf, acnt, "Driver hardware description: %s\n",
146 pvr2_hdw_get_desc(hdw));
147 bcnt += ccnt; acnt -= ccnt; buf += ccnt;
Mike Isely681c7392007-11-26 01:48:52 -0300148 ccnt = scnprintf(buf,acnt,"Driver state info:\n");
Mike Iselyd8554972006-06-26 20:58:46 -0300149 bcnt += ccnt; acnt -= ccnt; buf += ccnt;
Mike Isely681c7392007-11-26 01:48:52 -0300150 ccnt = pvr2_hdw_state_report(hdw,buf,acnt);
Mike Iselyd8554972006-06-26 20:58:46 -0300151 bcnt += ccnt; acnt -= ccnt; buf += ccnt;
Mike Iselyd8554972006-06-26 20:58:46 -0300152
153 return bcnt;
154}
155
156
157int pvr2_debugifc_print_status(struct pvr2_hdw *hdw,
158 char *buf,unsigned int acnt)
159{
160 int bcnt = 0;
161 int ccnt;
Mike Iselyd8554972006-06-26 20:58:46 -0300162 int ret;
163 u32 gpio_dir,gpio_in,gpio_out;
Mike Iselyad0992e2008-03-28 05:34:45 -0300164 struct pvr2_stream_stats stats;
165 struct pvr2_stream *sp;
Mike Iselyd8554972006-06-26 20:58:46 -0300166
167 ret = pvr2_hdw_is_hsm(hdw);
168 ccnt = scnprintf(buf,acnt,"USB link speed: %s\n",
169 (ret < 0 ? "FAIL" : (ret ? "high" : "full")));
170 bcnt += ccnt; acnt -= ccnt; buf += ccnt;
171
172 gpio_dir = 0; gpio_in = 0; gpio_out = 0;
173 pvr2_hdw_gpio_get_dir(hdw,&gpio_dir);
174 pvr2_hdw_gpio_get_out(hdw,&gpio_out);
175 pvr2_hdw_gpio_get_in(hdw,&gpio_in);
176 ccnt = scnprintf(buf,acnt,"GPIO state: dir=0x%x in=0x%x out=0x%x\n",
177 gpio_dir,gpio_in,gpio_out);
178 bcnt += ccnt; acnt -= ccnt; buf += ccnt;
179
180 ccnt = scnprintf(buf,acnt,"Streaming is %s\n",
181 pvr2_hdw_get_streaming(hdw) ? "on" : "off");
182 bcnt += ccnt; acnt -= ccnt; buf += ccnt;
183
Mike Iselyad0992e2008-03-28 05:34:45 -0300184
185 sp = pvr2_hdw_get_video_stream(hdw);
186 if (sp) {
187 pvr2_stream_get_stats(sp, &stats, 0);
188 ccnt = scnprintf(
189 buf,acnt,
190 "Bytes streamed=%u"
191 " URBs: queued=%u idle=%u ready=%u"
192 " processed=%u failed=%u\n",
193 stats.bytes_processed,
194 stats.buffers_in_queue,
195 stats.buffers_in_idle,
196 stats.buffers_in_ready,
197 stats.buffers_processed,
198 stats.buffers_failed);
199 bcnt += ccnt; acnt -= ccnt; buf += ccnt;
200 }
201
Mike Iselyd8554972006-06-26 20:58:46 -0300202 return bcnt;
203}
204
205
Adrian Bunk07e337e2006-06-30 11:30:20 -0300206static int pvr2_debugifc_do1cmd(struct pvr2_hdw *hdw,const char *buf,
207 unsigned int count)
Mike Iselyd8554972006-06-26 20:58:46 -0300208{
209 const char *wptr;
210 unsigned int wlen;
211 unsigned int scnt;
212
213 scnt = debugifc_isolate_word(buf,count,&wptr,&wlen);
214 if (!scnt) return 0;
215 count -= scnt; buf += scnt;
216 if (!wptr) return 0;
217
218 pvr2_trace(PVR2_TRACE_DEBUGIFC,"debugifc cmd: \"%.*s\"",wlen,wptr);
219 if (debugifc_match_keyword(wptr,wlen,"reset")) {
220 scnt = debugifc_isolate_word(buf,count,&wptr,&wlen);
221 if (!scnt) return -EINVAL;
222 count -= scnt; buf += scnt;
223 if (!wptr) return -EINVAL;
224 if (debugifc_match_keyword(wptr,wlen,"cpu")) {
225 pvr2_hdw_cpureset_assert(hdw,!0);
226 pvr2_hdw_cpureset_assert(hdw,0);
227 return 0;
228 } else if (debugifc_match_keyword(wptr,wlen,"bus")) {
229 pvr2_hdw_device_reset(hdw);
230 } else if (debugifc_match_keyword(wptr,wlen,"soft")) {
231 return pvr2_hdw_cmd_powerup(hdw);
232 } else if (debugifc_match_keyword(wptr,wlen,"deep")) {
233 return pvr2_hdw_cmd_deep_reset(hdw);
234 } else if (debugifc_match_keyword(wptr,wlen,"firmware")) {
235 return pvr2_upload_firmware2(hdw);
236 } else if (debugifc_match_keyword(wptr,wlen,"decoder")) {
237 return pvr2_hdw_cmd_decoder_reset(hdw);
Mike Isely681c7392007-11-26 01:48:52 -0300238 } else if (debugifc_match_keyword(wptr,wlen,"worker")) {
239 return pvr2_hdw_untrip(hdw);
Mike Iselyad0992e2008-03-28 05:34:45 -0300240 } else if (debugifc_match_keyword(wptr,wlen,"usbstats")) {
241 pvr2_stream_get_stats(pvr2_hdw_get_video_stream(hdw),
242 NULL, !0);
243 return 0;
Mike Iselyd8554972006-06-26 20:58:46 -0300244 }
245 return -EINVAL;
Mike Iselyd8554972006-06-26 20:58:46 -0300246 } else if (debugifc_match_keyword(wptr,wlen,"cpufw")) {
247 scnt = debugifc_isolate_word(buf,count,&wptr,&wlen);
248 if (!scnt) return -EINVAL;
249 count -= scnt; buf += scnt;
250 if (!wptr) return -EINVAL;
251 if (debugifc_match_keyword(wptr,wlen,"fetch")) {
Mike Isely4db666c2007-09-08 22:16:27 -0300252 scnt = debugifc_isolate_word(buf,count,&wptr,&wlen);
253 if (scnt && wptr) {
254 count -= scnt; buf += scnt;
255 if (debugifc_match_keyword(wptr,wlen,"prom")) {
256 pvr2_hdw_cpufw_set_enabled(hdw,!0,!0);
257 } else if (debugifc_match_keyword(wptr,wlen,
258 "ram")) {
259 pvr2_hdw_cpufw_set_enabled(hdw,0,!0);
260 } else {
261 return -EINVAL;
262 }
263 }
264 pvr2_hdw_cpufw_set_enabled(hdw,0,!0);
Mike Iselyd8554972006-06-26 20:58:46 -0300265 return 0;
266 } else if (debugifc_match_keyword(wptr,wlen,"done")) {
Mike Isely4db666c2007-09-08 22:16:27 -0300267 pvr2_hdw_cpufw_set_enabled(hdw,0,0);
Mike Iselyd8554972006-06-26 20:58:46 -0300268 return 0;
269 } else {
270 return -EINVAL;
271 }
272 } else if (debugifc_match_keyword(wptr,wlen,"gpio")) {
273 int dir_fl = 0;
274 int ret;
275 u32 msk,val;
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 if (debugifc_match_keyword(wptr,wlen,"dir")) {
281 dir_fl = !0;
282 } else if (!debugifc_match_keyword(wptr,wlen,"out")) {
283 return -EINVAL;
284 }
285 scnt = debugifc_isolate_word(buf,count,&wptr,&wlen);
286 if (!scnt) return -EINVAL;
287 count -= scnt; buf += scnt;
288 if (!wptr) return -EINVAL;
289 ret = debugifc_parse_unsigned_number(wptr,wlen,&msk);
290 if (ret) return ret;
291 scnt = debugifc_isolate_word(buf,count,&wptr,&wlen);
292 if (wptr) {
293 ret = debugifc_parse_unsigned_number(wptr,wlen,&val);
294 if (ret) return ret;
295 } else {
296 val = msk;
297 msk = 0xffffffff;
298 }
299 if (dir_fl) {
300 ret = pvr2_hdw_gpio_chg_dir(hdw,msk,val);
301 } else {
302 ret = pvr2_hdw_gpio_chg_out(hdw,msk,val);
303 }
304 return ret;
305 }
306 pvr2_trace(PVR2_TRACE_DEBUGIFC,
307 "debugifc failed to recognize cmd: \"%.*s\"",wlen,wptr);
308 return -EINVAL;
309}
310
311
312int pvr2_debugifc_docmd(struct pvr2_hdw *hdw,const char *buf,
313 unsigned int count)
314{
315 unsigned int bcnt = 0;
316 int ret;
317
318 while (count) {
319 for (bcnt = 0; bcnt < count; bcnt++) {
320 if (buf[bcnt] == '\n') break;
321 }
322
323 ret = pvr2_debugifc_do1cmd(hdw,buf,bcnt);
324 if (ret < 0) return ret;
325 if (bcnt < count) bcnt++;
326 buf += bcnt;
327 count -= bcnt;
328 }
329
330 return 0;
331}
332
333
334/*
335 Stuff for Emacs to see, in order to encourage consistent editing style:
336 *** Local Variables: ***
337 *** mode: c ***
338 *** fill-column: 75 ***
339 *** tab-width: 8 ***
340 *** c-basic-offset: 8 ***
341 *** End: ***
342 */