pvrusb2-debugifc.c 8.3 KB

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