pvrusb2-debugifc.c 8.6 KB

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