pvrusb2-debugifc.c 8.5 KB

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