pvrusb2-debugifc.c 8.6 KB

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