pvrusb2-debugifc.c 7.5 KB

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