pvrusb2-encoder.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. /*
  2. *
  3. * $Id$
  4. *
  5. * Copyright (C) 2005 Mike Isely <isely@pobox.com>
  6. * Copyright (C) 2004 Aurelien Alleaume <slts@free.fr>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. */
  22. #include <linux/device.h> // for linux/firmware.h
  23. #include <linux/firmware.h>
  24. #include "pvrusb2-util.h"
  25. #include "pvrusb2-encoder.h"
  26. #include "pvrusb2-hdw-internal.h"
  27. #include "pvrusb2-debug.h"
  28. #include "pvrusb2-fx2-cmd.h"
  29. /* Firmware mailbox flags - definitions found from ivtv */
  30. #define IVTV_MBOX_FIRMWARE_DONE 0x00000004
  31. #define IVTV_MBOX_DRIVER_DONE 0x00000002
  32. #define IVTV_MBOX_DRIVER_BUSY 0x00000001
  33. #define MBOX_BASE 0x44
  34. static int pvr2_encoder_write_words(struct pvr2_hdw *hdw,
  35. unsigned int offs,
  36. const u32 *data, unsigned int dlen)
  37. {
  38. unsigned int idx,addr;
  39. unsigned int bAddr;
  40. int ret;
  41. unsigned int chunkCnt;
  42. /*
  43. Format: First byte must be 0x01. Remaining 32 bit words are
  44. spread out into chunks of 7 bytes each, with the first 4 bytes
  45. being the data word (little endian), and the next 3 bytes
  46. being the address where that data word is to be written (big
  47. endian). Repeat request for additional words, with offset
  48. adjusted accordingly.
  49. */
  50. while (dlen) {
  51. chunkCnt = 8;
  52. if (chunkCnt > dlen) chunkCnt = dlen;
  53. memset(hdw->cmd_buffer,0,sizeof(hdw->cmd_buffer));
  54. bAddr = 0;
  55. hdw->cmd_buffer[bAddr++] = FX2CMD_MEM_WRITE_DWORD;
  56. for (idx = 0; idx < chunkCnt; idx++) {
  57. addr = idx + offs;
  58. hdw->cmd_buffer[bAddr+6] = (addr & 0xffu);
  59. hdw->cmd_buffer[bAddr+5] = ((addr>>8) & 0xffu);
  60. hdw->cmd_buffer[bAddr+4] = ((addr>>16) & 0xffu);
  61. PVR2_DECOMPOSE_LE(hdw->cmd_buffer, bAddr,data[idx]);
  62. bAddr += 7;
  63. }
  64. ret = pvr2_send_request(hdw,
  65. hdw->cmd_buffer,1+(chunkCnt*7),
  66. NULL,0);
  67. if (ret) return ret;
  68. data += chunkCnt;
  69. dlen -= chunkCnt;
  70. offs += chunkCnt;
  71. }
  72. return 0;
  73. }
  74. static int pvr2_encoder_read_words(struct pvr2_hdw *hdw,
  75. unsigned int offs,
  76. u32 *data, unsigned int dlen)
  77. {
  78. unsigned int idx;
  79. int ret;
  80. unsigned int chunkCnt;
  81. /*
  82. Format: First byte must be 0x02 (status check) or 0x28 (read
  83. back block of 32 bit words). Next 6 bytes must be zero,
  84. followed by a single byte of MBOX_BASE+offset for portion to
  85. be read. Returned data is packed set of 32 bits words that
  86. were read.
  87. */
  88. while (dlen) {
  89. chunkCnt = 16;
  90. if (chunkCnt > dlen) chunkCnt = dlen;
  91. if (chunkCnt < 16) chunkCnt = 1;
  92. hdw->cmd_buffer[0] =
  93. ((chunkCnt == 1) ?
  94. FX2CMD_MEM_READ_DWORD : FX2CMD_MEM_READ_64BYTES);
  95. hdw->cmd_buffer[1] = 0;
  96. hdw->cmd_buffer[2] = 0;
  97. hdw->cmd_buffer[3] = 0;
  98. hdw->cmd_buffer[4] = 0;
  99. hdw->cmd_buffer[5] = ((offs>>16) & 0xffu);
  100. hdw->cmd_buffer[6] = ((offs>>8) & 0xffu);
  101. hdw->cmd_buffer[7] = (offs & 0xffu);
  102. ret = pvr2_send_request(hdw,
  103. hdw->cmd_buffer,8,
  104. hdw->cmd_buffer,
  105. (chunkCnt == 1 ? 4 : 16 * 4));
  106. if (ret) return ret;
  107. for (idx = 0; idx < chunkCnt; idx++) {
  108. data[idx] = PVR2_COMPOSE_LE(hdw->cmd_buffer,idx*4);
  109. }
  110. data += chunkCnt;
  111. dlen -= chunkCnt;
  112. offs += chunkCnt;
  113. }
  114. return 0;
  115. }
  116. /* This prototype is set up to be compatible with the
  117. cx2341x_mbox_func prototype in cx2341x.h, which should be in
  118. kernels 2.6.18 or later. We do this so that we can enable
  119. cx2341x.ko to write to our encoder (by handing it a pointer to this
  120. function). For earlier kernels this doesn't really matter. */
  121. static int pvr2_encoder_cmd(void *ctxt,
  122. int cmd,
  123. int arg_cnt_send,
  124. int arg_cnt_recv,
  125. u32 *argp)
  126. {
  127. unsigned int poll_count;
  128. unsigned int try_count = 0;
  129. int retry_flag;
  130. int ret = 0;
  131. unsigned int idx;
  132. /* These sizes look to be limited by the FX2 firmware implementation */
  133. u32 wrData[16];
  134. u32 rdData[16];
  135. struct pvr2_hdw *hdw = (struct pvr2_hdw *)ctxt;
  136. /*
  137. The encoder seems to speak entirely using blocks 32 bit words.
  138. In ivtv driver terms, this is a mailbox at MBOX_BASE which we
  139. populate with data and watch what the hardware does with it.
  140. The first word is a set of flags used to control the
  141. transaction, the second word is the command to execute, the
  142. third byte is zero (ivtv driver suggests that this is some
  143. kind of return value), and the fourth byte is a specified
  144. timeout (windows driver always uses 0x00060000 except for one
  145. case when it is zero). All successive words are the argument
  146. words for the command.
  147. First, write out the entire set of words, with the first word
  148. being zero.
  149. Next, write out just the first word again, but set it to
  150. IVTV_MBOX_DRIVER_DONE | IVTV_DRIVER_BUSY this time (which
  151. probably means "go").
  152. Next, read back the return count words. Check the first word,
  153. which should have IVTV_MBOX_FIRMWARE_DONE set. If however
  154. that bit is not set, then the command isn't done so repeat the
  155. read until it is set.
  156. Finally, write out just the first word again, but set it to
  157. 0x0 this time (which probably means "idle").
  158. */
  159. if (arg_cnt_send > (ARRAY_SIZE(wrData) - 4)) {
  160. pvr2_trace(
  161. PVR2_TRACE_ERROR_LEGS,
  162. "Failed to write cx23416 command"
  163. " - too many input arguments"
  164. " (was given %u limit %lu)",
  165. arg_cnt_send, (long unsigned) ARRAY_SIZE(wrData) - 4);
  166. return -EINVAL;
  167. }
  168. if (arg_cnt_recv > (ARRAY_SIZE(rdData) - 4)) {
  169. pvr2_trace(
  170. PVR2_TRACE_ERROR_LEGS,
  171. "Failed to write cx23416 command"
  172. " - too many return arguments"
  173. " (was given %u limit %lu)",
  174. arg_cnt_recv, (long unsigned) ARRAY_SIZE(rdData) - 4);
  175. return -EINVAL;
  176. }
  177. LOCK_TAKE(hdw->ctl_lock); do {
  178. retry_flag = 0;
  179. try_count++;
  180. ret = 0;
  181. wrData[0] = 0;
  182. wrData[1] = cmd;
  183. wrData[2] = 0;
  184. wrData[3] = 0x00060000;
  185. for (idx = 0; idx < arg_cnt_send; idx++) {
  186. wrData[idx+4] = argp[idx];
  187. }
  188. for (; idx < ARRAY_SIZE(wrData) - 4; idx++) {
  189. wrData[idx+4] = 0;
  190. }
  191. ret = pvr2_encoder_write_words(hdw,MBOX_BASE,wrData,idx);
  192. if (ret) break;
  193. wrData[0] = IVTV_MBOX_DRIVER_DONE|IVTV_MBOX_DRIVER_BUSY;
  194. ret = pvr2_encoder_write_words(hdw,MBOX_BASE,wrData,1);
  195. if (ret) break;
  196. poll_count = 0;
  197. while (1) {
  198. poll_count++;
  199. ret = pvr2_encoder_read_words(hdw,MBOX_BASE,rdData,
  200. arg_cnt_recv+4);
  201. if (ret) {
  202. break;
  203. }
  204. if (rdData[0] & IVTV_MBOX_FIRMWARE_DONE) {
  205. break;
  206. }
  207. if (rdData[0] && (poll_count < 1000)) continue;
  208. if (!rdData[0]) {
  209. retry_flag = !0;
  210. pvr2_trace(
  211. PVR2_TRACE_ERROR_LEGS,
  212. "Encoder timed out waiting for us"
  213. "; arranging to retry");
  214. } else {
  215. pvr2_trace(
  216. PVR2_TRACE_ERROR_LEGS,
  217. "***WARNING*** device's encoder"
  218. " appears to be stuck"
  219. " (status=0x%08x)",rdData[0]);
  220. }
  221. pvr2_trace(
  222. PVR2_TRACE_ERROR_LEGS,
  223. "Encoder command: 0x%02x",cmd);
  224. for (idx = 4; idx < arg_cnt_send; idx++) {
  225. pvr2_trace(
  226. PVR2_TRACE_ERROR_LEGS,
  227. "Encoder arg%d: 0x%08x",
  228. idx-3,wrData[idx]);
  229. }
  230. ret = -EBUSY;
  231. break;
  232. }
  233. if (retry_flag) {
  234. if (try_count < 20) continue;
  235. pvr2_trace(
  236. PVR2_TRACE_ERROR_LEGS,
  237. "Too many retries...");
  238. ret = -EBUSY;
  239. }
  240. if (ret) {
  241. pvr2_trace(
  242. PVR2_TRACE_ERROR_LEGS,
  243. "Giving up on command."
  244. " It is likely that"
  245. " this is a bad idea...");
  246. break;
  247. }
  248. wrData[0] = 0x7;
  249. for (idx = 0; idx < arg_cnt_recv; idx++) {
  250. argp[idx] = rdData[idx+4];
  251. }
  252. wrData[0] = 0x0;
  253. ret = pvr2_encoder_write_words(hdw,MBOX_BASE,wrData,1);
  254. if (ret) break;
  255. } while(0); LOCK_GIVE(hdw->ctl_lock);
  256. return ret;
  257. }
  258. static int pvr2_encoder_vcmd(struct pvr2_hdw *hdw, int cmd,
  259. int args, ...)
  260. {
  261. va_list vl;
  262. unsigned int idx;
  263. u32 data[12];
  264. if (args > ARRAY_SIZE(data)) {
  265. pvr2_trace(
  266. PVR2_TRACE_ERROR_LEGS,
  267. "Failed to write cx23416 command"
  268. " - too many arguments"
  269. " (was given %u limit %lu)",
  270. args, (long unsigned) ARRAY_SIZE(data));
  271. return -EINVAL;
  272. }
  273. va_start(vl, args);
  274. for (idx = 0; idx < args; idx++) {
  275. data[idx] = va_arg(vl, u32);
  276. }
  277. va_end(vl);
  278. return pvr2_encoder_cmd(hdw,cmd,args,0,data);
  279. }
  280. /* This implements some extra setup for the encoder that seems to be
  281. specific to the PVR USB2 hardware. */
  282. static int pvr2_encoder_prep_config(struct pvr2_hdw *hdw)
  283. {
  284. int ret = 0;
  285. int encMisc3Arg = 0;
  286. #if 0
  287. /* This inexplicable bit happens in the Hauppage windows
  288. driver (for both 24xxx and 29xxx devices). However I
  289. currently see no difference in behavior with or without
  290. this stuff. Leave this here as a note of its existence,
  291. but don't use it. */
  292. LOCK_TAKE(hdw->ctl_lock); do {
  293. u32 dat[1];
  294. dat[0] = 0x80000640;
  295. pvr2_encoder_write_words(hdw,0x01fe,dat,1);
  296. pvr2_encoder_write_words(hdw,0x023e,dat,1);
  297. } while(0); LOCK_GIVE(hdw->ctl_lock);
  298. #endif
  299. /* Mike Isely <isely@pobox.com> 26-Jan-2006 The windows driver
  300. sends the following list of ENC_MISC commands (for both
  301. 24xxx and 29xxx devices). Meanings are not entirely clear,
  302. however without the ENC_MISC(3,1) command then we risk
  303. random perpetual video corruption whenever the video input
  304. breaks up for a moment (like when switching channels). */
  305. #if 0
  306. /* This ENC_MISC(5,0) command seems to hurt 29xxx sync
  307. performance on channel changes, but is not a problem on
  308. 24xxx devices. */
  309. ret |= pvr2_encoder_vcmd(hdw, CX2341X_ENC_MISC,4, 5,0,0,0);
  310. #endif
  311. /* This ENC_MISC(3,encMisc3Arg) command is critical - without
  312. it there will eventually be video corruption. Also, the
  313. 29xxx case is strange - the Windows driver is passing 1
  314. regardless of device type but if we have 1 for 29xxx device
  315. the video turns sluggish. */
  316. switch (hdw->hdw_type) {
  317. case PVR2_HDW_TYPE_24XXX: encMisc3Arg = 1; break;
  318. case PVR2_HDW_TYPE_29XXX: encMisc3Arg = 0; break;
  319. default: break;
  320. }
  321. ret |= pvr2_encoder_vcmd(hdw, CX2341X_ENC_MISC,4, 3,
  322. encMisc3Arg,0,0);
  323. ret |= pvr2_encoder_vcmd(hdw, CX2341X_ENC_MISC,4, 8,0,0,0);
  324. #if 0
  325. /* This ENC_MISC(4,1) command is poisonous, so it is commented
  326. out. But I'm leaving it here anyway to document its
  327. existence in the Windows driver. The effect of this
  328. command is that apps displaying the stream become sluggish
  329. with stuttering video. */
  330. ret |= pvr2_encoder_vcmd(hdw, CX2341X_ENC_MISC,4, 4,1,0,0);
  331. #endif
  332. ret |= pvr2_encoder_vcmd(hdw, CX2341X_ENC_MISC,4, 0,3,0,0);
  333. ret |= pvr2_encoder_vcmd(hdw, CX2341X_ENC_MISC,4,15,0,0,0);
  334. return ret;
  335. }
  336. int pvr2_encoder_configure(struct pvr2_hdw *hdw)
  337. {
  338. int ret;
  339. int val;
  340. pvr2_trace(PVR2_TRACE_ENCODER,"pvr2_encoder_configure"
  341. " (cx2341x module)");
  342. hdw->enc_ctl_state.port = CX2341X_PORT_STREAMING;
  343. hdw->enc_ctl_state.width = hdw->res_hor_val;
  344. hdw->enc_ctl_state.height = hdw->res_ver_val;
  345. hdw->enc_ctl_state.is_50hz = ((hdw->std_mask_cur & V4L2_STD_525_60) ?
  346. 0 : 1);
  347. ret = 0;
  348. ret |= pvr2_encoder_prep_config(hdw);
  349. /* saa7115: 0xf0 */
  350. val = 0xf0;
  351. if (hdw->hdw_type == PVR2_HDW_TYPE_24XXX) {
  352. /* ivtv cx25840: 0x140 */
  353. val = 0x140;
  354. }
  355. if (!ret) ret = pvr2_encoder_vcmd(
  356. hdw,CX2341X_ENC_SET_NUM_VSYNC_LINES, 2,
  357. val, val);
  358. /* setup firmware to notify us about some events (don't know why...) */
  359. if (!ret) ret = pvr2_encoder_vcmd(
  360. hdw,CX2341X_ENC_SET_EVENT_NOTIFICATION, 4,
  361. 0, 0, 0x10000000, 0xffffffff);
  362. if (!ret) ret = pvr2_encoder_vcmd(
  363. hdw,CX2341X_ENC_SET_VBI_LINE, 5,
  364. 0xffffffff,0,0,0,0);
  365. if (ret) {
  366. pvr2_trace(PVR2_TRACE_ERROR_LEGS,
  367. "Failed to configure cx23416");
  368. return ret;
  369. }
  370. ret = cx2341x_update(hdw,pvr2_encoder_cmd,
  371. (hdw->enc_cur_valid ? &hdw->enc_cur_state : NULL),
  372. &hdw->enc_ctl_state);
  373. if (ret) {
  374. pvr2_trace(PVR2_TRACE_ERROR_LEGS,
  375. "Error from cx2341x module code=%d",ret);
  376. return ret;
  377. }
  378. ret = 0;
  379. if (!ret) ret = pvr2_encoder_vcmd(
  380. hdw, CX2341X_ENC_INITIALIZE_INPUT, 0);
  381. if (ret) {
  382. pvr2_trace(PVR2_TRACE_ERROR_LEGS,
  383. "Failed to initialize cx23416 video input");
  384. return ret;
  385. }
  386. hdw->subsys_enabled_mask |= (1<<PVR2_SUBSYS_B_ENC_CFG);
  387. memcpy(&hdw->enc_cur_state,&hdw->enc_ctl_state,
  388. sizeof(struct cx2341x_mpeg_params));
  389. hdw->enc_cur_valid = !0;
  390. return 0;
  391. }
  392. int pvr2_encoder_start(struct pvr2_hdw *hdw)
  393. {
  394. int status;
  395. /* unmask some interrupts */
  396. pvr2_write_register(hdw, 0x0048, 0xbfffffff);
  397. /* change some GPIO data */
  398. pvr2_hdw_gpio_chg_dir(hdw,0xffffffff,0x00000481);
  399. pvr2_hdw_gpio_chg_out(hdw,0xffffffff,0x00000000);
  400. pvr2_encoder_vcmd(hdw,CX2341X_ENC_MUTE_VIDEO,1,
  401. hdw->input_val == PVR2_CVAL_INPUT_RADIO ? 1 : 0);
  402. switch (hdw->config) {
  403. case pvr2_config_vbi:
  404. status = pvr2_encoder_vcmd(hdw,CX2341X_ENC_START_CAPTURE,2,
  405. 0x01,0x14);
  406. break;
  407. case pvr2_config_mpeg:
  408. status = pvr2_encoder_vcmd(hdw,CX2341X_ENC_START_CAPTURE,2,
  409. 0,0x13);
  410. break;
  411. default: /* Unhandled cases for now */
  412. status = pvr2_encoder_vcmd(hdw,CX2341X_ENC_START_CAPTURE,2,
  413. 0,0x13);
  414. break;
  415. }
  416. if (!status) {
  417. hdw->subsys_enabled_mask |= (1<<PVR2_SUBSYS_B_ENC_RUN);
  418. }
  419. return status;
  420. }
  421. int pvr2_encoder_stop(struct pvr2_hdw *hdw)
  422. {
  423. int status;
  424. /* mask all interrupts */
  425. pvr2_write_register(hdw, 0x0048, 0xffffffff);
  426. switch (hdw->config) {
  427. case pvr2_config_vbi:
  428. status = pvr2_encoder_vcmd(hdw,CX2341X_ENC_STOP_CAPTURE,3,
  429. 0x01,0x01,0x14);
  430. break;
  431. case pvr2_config_mpeg:
  432. status = pvr2_encoder_vcmd(hdw,CX2341X_ENC_STOP_CAPTURE,3,
  433. 0x01,0,0x13);
  434. break;
  435. default: /* Unhandled cases for now */
  436. status = pvr2_encoder_vcmd(hdw,CX2341X_ENC_STOP_CAPTURE,3,
  437. 0x01,0,0x13);
  438. break;
  439. }
  440. /* change some GPIO data */
  441. /* Note: Bit d7 of dir appears to control the LED. So we shut it
  442. off here. */
  443. pvr2_hdw_gpio_chg_dir(hdw,0xffffffff,0x00000401);
  444. pvr2_hdw_gpio_chg_out(hdw,0xffffffff,0x00000000);
  445. if (!status) {
  446. hdw->subsys_enabled_mask &= ~(1<<PVR2_SUBSYS_B_ENC_RUN);
  447. }
  448. return status;
  449. }
  450. /*
  451. Stuff for Emacs to see, in order to encourage consistent editing style:
  452. *** Local Variables: ***
  453. *** mode: c ***
  454. *** fill-column: 70 ***
  455. *** tab-width: 8 ***
  456. *** c-basic-offset: 8 ***
  457. *** End: ***
  458. */