]> bbs.cooldavid.org Git - net-next-2.6.git/blob - drivers/media/video/pvrusb2/pvrusb2-debugifc.c
ae977668c4969d29fd1e9d983e1b394dbcd9e968
[net-next-2.6.git] / drivers / media / video / pvrusb2 / pvrusb2-debugifc.c
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 <linux/slab.h>
23 #include "pvrusb2-debugifc.h"
24 #include "pvrusb2-hdw.h"
25 #include "pvrusb2-debug.h"
26
27 struct debugifc_mask_item {
28         const char *name;
29         unsigned long msk;
30 };
31
32
33 static 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
50 static 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
66 static 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
75         wptr = NULL;
76         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
94 static 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
130 static 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
141 int pvr2_debugifc_print_info(struct pvr2_hdw *hdw,char *buf,unsigned int acnt)
142 {
143         int bcnt = 0;
144         int ccnt;
145         ccnt = scnprintf(buf, acnt, "Driver hardware description: %s\n",
146                          pvr2_hdw_get_desc(hdw));
147         bcnt += ccnt; acnt -= ccnt; buf += ccnt;
148         ccnt = scnprintf(buf,acnt,"Driver state info:\n");
149         bcnt += ccnt; acnt -= ccnt; buf += ccnt;
150         ccnt = pvr2_hdw_state_report(hdw,buf,acnt);
151         bcnt += ccnt; acnt -= ccnt; buf += ccnt;
152
153         return bcnt;
154 }
155
156
157 int pvr2_debugifc_print_status(struct pvr2_hdw *hdw,
158                                char *buf,unsigned int acnt)
159 {
160         int bcnt = 0;
161         int ccnt;
162         int ret;
163         u32 gpio_dir,gpio_in,gpio_out;
164         struct pvr2_stream_stats stats;
165         struct pvr2_stream *sp;
166
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
184
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
202         return bcnt;
203 }
204
205
206 static int pvr2_debugifc_do1cmd(struct pvr2_hdw *hdw,const char *buf,
207                                 unsigned int count)
208 {
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);
238                 } else if (debugifc_match_keyword(wptr,wlen,"worker")) {
239                         return pvr2_hdw_untrip(hdw);
240                 } 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;
244                 }
245                 return -EINVAL;
246         } 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")) {
252                         scnt = debugifc_isolate_word(buf,count,&wptr,&wlen);
253                         if (scnt && wptr) {
254                                 count -= scnt; buf += scnt;
255                                 if (debugifc_match_keyword(wptr, wlen,
256                                                            "prom")) {
257                                         pvr2_hdw_cpufw_set_enabled(hdw, 2, !0);
258                                 } else if (debugifc_match_keyword(wptr, wlen,
259                                                                   "ram8k")) {
260                                         pvr2_hdw_cpufw_set_enabled(hdw, 0, !0);
261                                 } else if (debugifc_match_keyword(wptr, wlen,
262                                                                   "ram16k")) {
263                                         pvr2_hdw_cpufw_set_enabled(hdw, 1, !0);
264                                 } else {
265                                         return -EINVAL;
266                                 }
267                         }
268                         pvr2_hdw_cpufw_set_enabled(hdw,0,!0);
269                         return 0;
270                 } else if (debugifc_match_keyword(wptr,wlen,"done")) {
271                         pvr2_hdw_cpufw_set_enabled(hdw,0,0);
272                         return 0;
273                 } else {
274                         return -EINVAL;
275                 }
276         } else if (debugifc_match_keyword(wptr,wlen,"gpio")) {
277                 int dir_fl = 0;
278                 int ret;
279                 u32 msk,val;
280                 scnt = debugifc_isolate_word(buf,count,&wptr,&wlen);
281                 if (!scnt) return -EINVAL;
282                 count -= scnt; buf += scnt;
283                 if (!wptr) return -EINVAL;
284                 if (debugifc_match_keyword(wptr,wlen,"dir")) {
285                         dir_fl = !0;
286                 } else if (!debugifc_match_keyword(wptr,wlen,"out")) {
287                         return -EINVAL;
288                 }
289                 scnt = debugifc_isolate_word(buf,count,&wptr,&wlen);
290                 if (!scnt) return -EINVAL;
291                 count -= scnt; buf += scnt;
292                 if (!wptr) return -EINVAL;
293                 ret = debugifc_parse_unsigned_number(wptr,wlen,&msk);
294                 if (ret) return ret;
295                 scnt = debugifc_isolate_word(buf,count,&wptr,&wlen);
296                 if (wptr) {
297                         ret = debugifc_parse_unsigned_number(wptr,wlen,&val);
298                         if (ret) return ret;
299                 } else {
300                         val = msk;
301                         msk = 0xffffffff;
302                 }
303                 if (dir_fl) {
304                         ret = pvr2_hdw_gpio_chg_dir(hdw,msk,val);
305                 } else {
306                         ret = pvr2_hdw_gpio_chg_out(hdw,msk,val);
307                 }
308                 return ret;
309         }
310         pvr2_trace(PVR2_TRACE_DEBUGIFC,
311                    "debugifc failed to recognize cmd: \"%.*s\"",wlen,wptr);
312         return -EINVAL;
313 }
314
315
316 int pvr2_debugifc_docmd(struct pvr2_hdw *hdw,const char *buf,
317                         unsigned int count)
318 {
319         unsigned int bcnt = 0;
320         int ret;
321
322         while (count) {
323                 for (bcnt = 0; bcnt < count; bcnt++) {
324                         if (buf[bcnt] == '\n') break;
325                 }
326
327                 ret = pvr2_debugifc_do1cmd(hdw,buf,bcnt);
328                 if (ret < 0) return ret;
329                 if (bcnt < count) bcnt++;
330                 buf += bcnt;
331                 count -= bcnt;
332         }
333
334         return 0;
335 }
336
337
338 /*
339   Stuff for Emacs to see, in order to encourage consistent editing style:
340   *** Local Variables: ***
341   *** mode: c ***
342   *** fill-column: 75 ***
343   *** tab-width: 8 ***
344   *** c-basic-offset: 8 ***
345   *** End: ***
346   */