pvrusb2-debugifc.c 8.2 KB

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