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