V4L/DVB (11693): gspca - stv06xx-vv6410: Add exposure ctrl
[linux-2.6] / 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 state info:\n");
146         bcnt += ccnt; acnt -= ccnt; buf += ccnt;
147         ccnt = pvr2_hdw_state_report(hdw,buf,acnt);
148         bcnt += ccnt; acnt -= ccnt; buf += ccnt;
149
150         return bcnt;
151 }
152
153
154 int pvr2_debugifc_print_status(struct pvr2_hdw *hdw,
155                                char *buf,unsigned int acnt)
156 {
157         int bcnt = 0;
158         int ccnt;
159         int ret;
160         u32 gpio_dir,gpio_in,gpio_out;
161         struct pvr2_stream_stats stats;
162         struct pvr2_stream *sp;
163
164         ret = pvr2_hdw_is_hsm(hdw);
165         ccnt = scnprintf(buf,acnt,"USB link speed: %s\n",
166                          (ret < 0 ? "FAIL" : (ret ? "high" : "full")));
167         bcnt += ccnt; acnt -= ccnt; buf += ccnt;
168
169         gpio_dir = 0; gpio_in = 0; gpio_out = 0;
170         pvr2_hdw_gpio_get_dir(hdw,&gpio_dir);
171         pvr2_hdw_gpio_get_out(hdw,&gpio_out);
172         pvr2_hdw_gpio_get_in(hdw,&gpio_in);
173         ccnt = scnprintf(buf,acnt,"GPIO state: dir=0x%x in=0x%x out=0x%x\n",
174                          gpio_dir,gpio_in,gpio_out);
175         bcnt += ccnt; acnt -= ccnt; buf += ccnt;
176
177         ccnt = scnprintf(buf,acnt,"Streaming is %s\n",
178                          pvr2_hdw_get_streaming(hdw) ? "on" : "off");
179         bcnt += ccnt; acnt -= ccnt; buf += ccnt;
180
181
182         sp = pvr2_hdw_get_video_stream(hdw);
183         if (sp) {
184                 pvr2_stream_get_stats(sp, &stats, 0);
185                 ccnt = scnprintf(
186                         buf,acnt,
187                         "Bytes streamed=%u"
188                         " URBs: queued=%u idle=%u ready=%u"
189                         " processed=%u failed=%u\n",
190                         stats.bytes_processed,
191                         stats.buffers_in_queue,
192                         stats.buffers_in_idle,
193                         stats.buffers_in_ready,
194                         stats.buffers_processed,
195                         stats.buffers_failed);
196                 bcnt += ccnt; acnt -= ccnt; buf += ccnt;
197         }
198
199         return bcnt;
200 }
201
202
203 static int pvr2_debugifc_do1cmd(struct pvr2_hdw *hdw,const char *buf,
204                                 unsigned int count)
205 {
206         const char *wptr;
207         unsigned int wlen;
208         unsigned int scnt;
209
210         scnt = debugifc_isolate_word(buf,count,&wptr,&wlen);
211         if (!scnt) return 0;
212         count -= scnt; buf += scnt;
213         if (!wptr) return 0;
214
215         pvr2_trace(PVR2_TRACE_DEBUGIFC,"debugifc cmd: \"%.*s\"",wlen,wptr);
216         if (debugifc_match_keyword(wptr,wlen,"reset")) {
217                 scnt = debugifc_isolate_word(buf,count,&wptr,&wlen);
218                 if (!scnt) return -EINVAL;
219                 count -= scnt; buf += scnt;
220                 if (!wptr) return -EINVAL;
221                 if (debugifc_match_keyword(wptr,wlen,"cpu")) {
222                         pvr2_hdw_cpureset_assert(hdw,!0);
223                         pvr2_hdw_cpureset_assert(hdw,0);
224                         return 0;
225                 } else if (debugifc_match_keyword(wptr,wlen,"bus")) {
226                         pvr2_hdw_device_reset(hdw);
227                 } else if (debugifc_match_keyword(wptr,wlen,"soft")) {
228                         return pvr2_hdw_cmd_powerup(hdw);
229                 } else if (debugifc_match_keyword(wptr,wlen,"deep")) {
230                         return pvr2_hdw_cmd_deep_reset(hdw);
231                 } else if (debugifc_match_keyword(wptr,wlen,"firmware")) {
232                         return pvr2_upload_firmware2(hdw);
233                 } else if (debugifc_match_keyword(wptr,wlen,"decoder")) {
234                         return pvr2_hdw_cmd_decoder_reset(hdw);
235                 } else if (debugifc_match_keyword(wptr,wlen,"worker")) {
236                         return pvr2_hdw_untrip(hdw);
237                 } else if (debugifc_match_keyword(wptr,wlen,"usbstats")) {
238                         pvr2_stream_get_stats(pvr2_hdw_get_video_stream(hdw),
239                                               NULL, !0);
240                         return 0;
241                 }
242                 return -EINVAL;
243         } else if (debugifc_match_keyword(wptr,wlen,"cpufw")) {
244                 scnt = debugifc_isolate_word(buf,count,&wptr,&wlen);
245                 if (!scnt) return -EINVAL;
246                 count -= scnt; buf += scnt;
247                 if (!wptr) return -EINVAL;
248                 if (debugifc_match_keyword(wptr,wlen,"fetch")) {
249                         scnt = debugifc_isolate_word(buf,count,&wptr,&wlen);
250                         if (scnt && wptr) {
251                                 count -= scnt; buf += scnt;
252                                 if (debugifc_match_keyword(wptr,wlen,"prom")) {
253                                         pvr2_hdw_cpufw_set_enabled(hdw,!0,!0);
254                                 } else if (debugifc_match_keyword(wptr,wlen,
255                                                                   "ram")) {
256                                         pvr2_hdw_cpufw_set_enabled(hdw,0,!0);
257                                 } else {
258                                         return -EINVAL;
259                                 }
260                         }
261                         pvr2_hdw_cpufw_set_enabled(hdw,0,!0);
262                         return 0;
263                 } else if (debugifc_match_keyword(wptr,wlen,"done")) {
264                         pvr2_hdw_cpufw_set_enabled(hdw,0,0);
265                         return 0;
266                 } else {
267                         return -EINVAL;
268                 }
269         } else if (debugifc_match_keyword(wptr,wlen,"gpio")) {
270                 int dir_fl = 0;
271                 int ret;
272                 u32 msk,val;
273                 scnt = debugifc_isolate_word(buf,count,&wptr,&wlen);
274                 if (!scnt) return -EINVAL;
275                 count -= scnt; buf += scnt;
276                 if (!wptr) return -EINVAL;
277                 if (debugifc_match_keyword(wptr,wlen,"dir")) {
278                         dir_fl = !0;
279                 } else if (!debugifc_match_keyword(wptr,wlen,"out")) {
280                         return -EINVAL;
281                 }
282                 scnt = debugifc_isolate_word(buf,count,&wptr,&wlen);
283                 if (!scnt) return -EINVAL;
284                 count -= scnt; buf += scnt;
285                 if (!wptr) return -EINVAL;
286                 ret = debugifc_parse_unsigned_number(wptr,wlen,&msk);
287                 if (ret) return ret;
288                 scnt = debugifc_isolate_word(buf,count,&wptr,&wlen);
289                 if (wptr) {
290                         ret = debugifc_parse_unsigned_number(wptr,wlen,&val);
291                         if (ret) return ret;
292                 } else {
293                         val = msk;
294                         msk = 0xffffffff;
295                 }
296                 if (dir_fl) {
297                         ret = pvr2_hdw_gpio_chg_dir(hdw,msk,val);
298                 } else {
299                         ret = pvr2_hdw_gpio_chg_out(hdw,msk,val);
300                 }
301                 return ret;
302         }
303         pvr2_trace(PVR2_TRACE_DEBUGIFC,
304                    "debugifc failed to recognize cmd: \"%.*s\"",wlen,wptr);
305         return -EINVAL;
306 }
307
308
309 int pvr2_debugifc_docmd(struct pvr2_hdw *hdw,const char *buf,
310                         unsigned int count)
311 {
312         unsigned int bcnt = 0;
313         int ret;
314
315         while (count) {
316                 for (bcnt = 0; bcnt < count; bcnt++) {
317                         if (buf[bcnt] == '\n') break;
318                 }
319
320                 ret = pvr2_debugifc_do1cmd(hdw,buf,bcnt);
321                 if (ret < 0) return ret;
322                 if (bcnt < count) bcnt++;
323                 buf += bcnt;
324                 count -= bcnt;
325         }
326
327         return 0;
328 }
329
330
331 /*
332   Stuff for Emacs to see, in order to encourage consistent editing style:
333   *** Local Variables: ***
334   *** mode: c ***
335   *** fill-column: 75 ***
336   *** tab-width: 8 ***
337   *** c-basic-offset: 8 ***
338   *** End: ***
339   */