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