cx88-blackbird.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257
  1. /*
  2. * Support for a cx23416 mpeg encoder via cx2388x host port.
  3. * "blackbird" reference design.
  4. *
  5. * (c) 2004 Jelle Foks <jelle@foks.us>
  6. * (c) 2004 Gerd Knorr <kraxel@bytesex.org>
  7. *
  8. * (c) 2005-2006 Mauro Carvalho Chehab <mchehab@kernel.org>
  9. * - video_ioctl2 conversion
  10. *
  11. * Includes parts from the ivtv driver <http://sourceforge.net/projects/ivtv/>
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. */
  23. #include "cx88.h"
  24. #include <linux/module.h>
  25. #include <linux/init.h>
  26. #include <linux/slab.h>
  27. #include <linux/fs.h>
  28. #include <linux/delay.h>
  29. #include <linux/device.h>
  30. #include <linux/firmware.h>
  31. #include <media/v4l2-common.h>
  32. #include <media/v4l2-ioctl.h>
  33. #include <media/v4l2-event.h>
  34. #include <media/drv-intf/cx2341x.h>
  35. MODULE_DESCRIPTION("driver for cx2388x/cx23416 based mpeg encoder cards");
  36. MODULE_AUTHOR("Jelle Foks <jelle@foks.us>, Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
  37. MODULE_LICENSE("GPL");
  38. MODULE_VERSION(CX88_VERSION);
  39. static unsigned int debug;
  40. module_param(debug, int, 0644);
  41. MODULE_PARM_DESC(debug, "enable debug messages [blackbird]");
  42. #define dprintk(level, fmt, arg...) do { \
  43. if (debug + 1 > level) \
  44. printk(KERN_DEBUG pr_fmt("%s: blackbird:" fmt), \
  45. __func__, ##arg); \
  46. } while (0)
  47. /* ------------------------------------------------------------------ */
  48. #define BLACKBIRD_FIRM_IMAGE_SIZE 376836
  49. /* defines below are from ivtv-driver.h */
  50. #define IVTV_CMD_HW_BLOCKS_RST 0xFFFFFFFF
  51. /* Firmware API commands */
  52. #define IVTV_API_STD_TIMEOUT 500
  53. enum blackbird_capture_type {
  54. BLACKBIRD_MPEG_CAPTURE,
  55. BLACKBIRD_RAW_CAPTURE,
  56. BLACKBIRD_RAW_PASSTHRU_CAPTURE
  57. };
  58. enum blackbird_capture_bits {
  59. BLACKBIRD_RAW_BITS_NONE = 0x00,
  60. BLACKBIRD_RAW_BITS_YUV_CAPTURE = 0x01,
  61. BLACKBIRD_RAW_BITS_PCM_CAPTURE = 0x02,
  62. BLACKBIRD_RAW_BITS_VBI_CAPTURE = 0x04,
  63. BLACKBIRD_RAW_BITS_PASSTHRU_CAPTURE = 0x08,
  64. BLACKBIRD_RAW_BITS_TO_HOST_CAPTURE = 0x10
  65. };
  66. enum blackbird_capture_end {
  67. BLACKBIRD_END_AT_GOP, /* stop at the end of gop, generate irq */
  68. BLACKBIRD_END_NOW, /* stop immediately, no irq */
  69. };
  70. enum blackbird_framerate {
  71. BLACKBIRD_FRAMERATE_NTSC_30, /* NTSC: 30fps */
  72. BLACKBIRD_FRAMERATE_PAL_25 /* PAL: 25fps */
  73. };
  74. enum blackbird_stream_port {
  75. BLACKBIRD_OUTPUT_PORT_MEMORY,
  76. BLACKBIRD_OUTPUT_PORT_STREAMING,
  77. BLACKBIRD_OUTPUT_PORT_SERIAL
  78. };
  79. enum blackbird_data_xfer_status {
  80. BLACKBIRD_MORE_BUFFERS_FOLLOW,
  81. BLACKBIRD_LAST_BUFFER,
  82. };
  83. enum blackbird_picture_mask {
  84. BLACKBIRD_PICTURE_MASK_NONE,
  85. BLACKBIRD_PICTURE_MASK_I_FRAMES,
  86. BLACKBIRD_PICTURE_MASK_I_P_FRAMES = 0x3,
  87. BLACKBIRD_PICTURE_MASK_ALL_FRAMES = 0x7,
  88. };
  89. enum blackbird_vbi_mode_bits {
  90. BLACKBIRD_VBI_BITS_SLICED,
  91. BLACKBIRD_VBI_BITS_RAW,
  92. };
  93. enum blackbird_vbi_insertion_bits {
  94. BLACKBIRD_VBI_BITS_INSERT_IN_XTENSION_USR_DATA,
  95. BLACKBIRD_VBI_BITS_INSERT_IN_PRIVATE_PACKETS = 0x1 << 1,
  96. BLACKBIRD_VBI_BITS_SEPARATE_STREAM = 0x2 << 1,
  97. BLACKBIRD_VBI_BITS_SEPARATE_STREAM_USR_DATA = 0x4 << 1,
  98. BLACKBIRD_VBI_BITS_SEPARATE_STREAM_PRV_DATA = 0x5 << 1,
  99. };
  100. enum blackbird_dma_unit {
  101. BLACKBIRD_DMA_BYTES,
  102. BLACKBIRD_DMA_FRAMES,
  103. };
  104. enum blackbird_dma_transfer_status_bits {
  105. BLACKBIRD_DMA_TRANSFER_BITS_DONE = 0x01,
  106. BLACKBIRD_DMA_TRANSFER_BITS_ERROR = 0x04,
  107. BLACKBIRD_DMA_TRANSFER_BITS_LL_ERROR = 0x10,
  108. };
  109. enum blackbird_pause {
  110. BLACKBIRD_PAUSE_ENCODING,
  111. BLACKBIRD_RESUME_ENCODING,
  112. };
  113. enum blackbird_copyright {
  114. BLACKBIRD_COPYRIGHT_OFF,
  115. BLACKBIRD_COPYRIGHT_ON,
  116. };
  117. enum blackbird_notification_type {
  118. BLACKBIRD_NOTIFICATION_REFRESH,
  119. };
  120. enum blackbird_notification_status {
  121. BLACKBIRD_NOTIFICATION_OFF,
  122. BLACKBIRD_NOTIFICATION_ON,
  123. };
  124. enum blackbird_notification_mailbox {
  125. BLACKBIRD_NOTIFICATION_NO_MAILBOX = -1,
  126. };
  127. enum blackbird_field1_lines {
  128. BLACKBIRD_FIELD1_SAA7114 = 0x00EF, /* 239 */
  129. BLACKBIRD_FIELD1_SAA7115 = 0x00F0, /* 240 */
  130. BLACKBIRD_FIELD1_MICRONAS = 0x0105, /* 261 */
  131. };
  132. enum blackbird_field2_lines {
  133. BLACKBIRD_FIELD2_SAA7114 = 0x00EF, /* 239 */
  134. BLACKBIRD_FIELD2_SAA7115 = 0x00F0, /* 240 */
  135. BLACKBIRD_FIELD2_MICRONAS = 0x0106, /* 262 */
  136. };
  137. enum blackbird_custom_data_type {
  138. BLACKBIRD_CUSTOM_EXTENSION_USR_DATA,
  139. BLACKBIRD_CUSTOM_PRIVATE_PACKET,
  140. };
  141. enum blackbird_mute {
  142. BLACKBIRD_UNMUTE,
  143. BLACKBIRD_MUTE,
  144. };
  145. enum blackbird_mute_video_mask {
  146. BLACKBIRD_MUTE_VIDEO_V_MASK = 0x0000FF00,
  147. BLACKBIRD_MUTE_VIDEO_U_MASK = 0x00FF0000,
  148. BLACKBIRD_MUTE_VIDEO_Y_MASK = 0xFF000000,
  149. };
  150. enum blackbird_mute_video_shift {
  151. BLACKBIRD_MUTE_VIDEO_V_SHIFT = 8,
  152. BLACKBIRD_MUTE_VIDEO_U_SHIFT = 16,
  153. BLACKBIRD_MUTE_VIDEO_Y_SHIFT = 24,
  154. };
  155. /* Registers */
  156. #define IVTV_REG_ENC_SDRAM_REFRESH (0x07F8 /*| IVTV_REG_OFFSET*/)
  157. #define IVTV_REG_ENC_SDRAM_PRECHARGE (0x07FC /*| IVTV_REG_OFFSET*/)
  158. #define IVTV_REG_SPU (0x9050 /*| IVTV_REG_OFFSET*/)
  159. #define IVTV_REG_HW_BLOCKS (0x9054 /*| IVTV_REG_OFFSET*/)
  160. #define IVTV_REG_VPU (0x9058 /*| IVTV_REG_OFFSET*/)
  161. #define IVTV_REG_APU (0xA064 /*| IVTV_REG_OFFSET*/)
  162. /* ------------------------------------------------------------------ */
  163. static void host_setup(struct cx88_core *core)
  164. {
  165. /* toggle reset of the host */
  166. cx_write(MO_GPHST_SOFT_RST, 1);
  167. udelay(100);
  168. cx_write(MO_GPHST_SOFT_RST, 0);
  169. udelay(100);
  170. /* host port setup */
  171. cx_write(MO_GPHST_WSC, 0x44444444U);
  172. cx_write(MO_GPHST_XFR, 0);
  173. cx_write(MO_GPHST_WDTH, 15);
  174. cx_write(MO_GPHST_HDSHK, 0);
  175. cx_write(MO_GPHST_MUX16, 0x44448888U);
  176. cx_write(MO_GPHST_MODE, 0);
  177. }
  178. /* ------------------------------------------------------------------ */
  179. #define P1_MDATA0 0x390000
  180. #define P1_MDATA1 0x390001
  181. #define P1_MDATA2 0x390002
  182. #define P1_MDATA3 0x390003
  183. #define P1_MADDR2 0x390004
  184. #define P1_MADDR1 0x390005
  185. #define P1_MADDR0 0x390006
  186. #define P1_RDATA0 0x390008
  187. #define P1_RDATA1 0x390009
  188. #define P1_RDATA2 0x39000A
  189. #define P1_RDATA3 0x39000B
  190. #define P1_RADDR0 0x39000C
  191. #define P1_RADDR1 0x39000D
  192. #define P1_RRDWR 0x39000E
  193. static int wait_ready_gpio0_bit1(struct cx88_core *core, u32 state)
  194. {
  195. unsigned long timeout = jiffies + msecs_to_jiffies(1);
  196. u32 gpio0, need;
  197. need = state ? 2 : 0;
  198. for (;;) {
  199. gpio0 = cx_read(MO_GP0_IO) & 2;
  200. if (need == gpio0)
  201. return 0;
  202. if (time_after(jiffies, timeout))
  203. return -1;
  204. udelay(1);
  205. }
  206. }
  207. static int memory_write(struct cx88_core *core, u32 address, u32 value)
  208. {
  209. /* Warning: address is dword address (4 bytes) */
  210. cx_writeb(P1_MDATA0, (unsigned int)value);
  211. cx_writeb(P1_MDATA1, (unsigned int)(value >> 8));
  212. cx_writeb(P1_MDATA2, (unsigned int)(value >> 16));
  213. cx_writeb(P1_MDATA3, (unsigned int)(value >> 24));
  214. cx_writeb(P1_MADDR2, (unsigned int)(address >> 16) | 0x40);
  215. cx_writeb(P1_MADDR1, (unsigned int)(address >> 8));
  216. cx_writeb(P1_MADDR0, (unsigned int)address);
  217. cx_read(P1_MDATA0);
  218. cx_read(P1_MADDR0);
  219. return wait_ready_gpio0_bit1(core, 1);
  220. }
  221. static int memory_read(struct cx88_core *core, u32 address, u32 *value)
  222. {
  223. int retval;
  224. u32 val;
  225. /* Warning: address is dword address (4 bytes) */
  226. cx_writeb(P1_MADDR2, (unsigned int)(address >> 16) & ~0xC0);
  227. cx_writeb(P1_MADDR1, (unsigned int)(address >> 8));
  228. cx_writeb(P1_MADDR0, (unsigned int)address);
  229. cx_read(P1_MADDR0);
  230. retval = wait_ready_gpio0_bit1(core, 1);
  231. cx_writeb(P1_MDATA3, 0);
  232. val = (unsigned char)cx_read(P1_MDATA3) << 24;
  233. cx_writeb(P1_MDATA2, 0);
  234. val |= (unsigned char)cx_read(P1_MDATA2) << 16;
  235. cx_writeb(P1_MDATA1, 0);
  236. val |= (unsigned char)cx_read(P1_MDATA1) << 8;
  237. cx_writeb(P1_MDATA0, 0);
  238. val |= (unsigned char)cx_read(P1_MDATA0);
  239. *value = val;
  240. return retval;
  241. }
  242. static int register_write(struct cx88_core *core, u32 address, u32 value)
  243. {
  244. cx_writeb(P1_RDATA0, (unsigned int)value);
  245. cx_writeb(P1_RDATA1, (unsigned int)(value >> 8));
  246. cx_writeb(P1_RDATA2, (unsigned int)(value >> 16));
  247. cx_writeb(P1_RDATA3, (unsigned int)(value >> 24));
  248. cx_writeb(P1_RADDR0, (unsigned int)address);
  249. cx_writeb(P1_RADDR1, (unsigned int)(address >> 8));
  250. cx_writeb(P1_RRDWR, 1);
  251. cx_read(P1_RDATA0);
  252. cx_read(P1_RADDR0);
  253. return wait_ready_gpio0_bit1(core, 1);
  254. }
  255. static int register_read(struct cx88_core *core, u32 address, u32 *value)
  256. {
  257. int retval;
  258. u32 val;
  259. cx_writeb(P1_RADDR0, (unsigned int)address);
  260. cx_writeb(P1_RADDR1, (unsigned int)(address >> 8));
  261. cx_writeb(P1_RRDWR, 0);
  262. cx_read(P1_RADDR0);
  263. retval = wait_ready_gpio0_bit1(core, 1);
  264. val = (unsigned char)cx_read(P1_RDATA0);
  265. val |= (unsigned char)cx_read(P1_RDATA1) << 8;
  266. val |= (unsigned char)cx_read(P1_RDATA2) << 16;
  267. val |= (unsigned char)cx_read(P1_RDATA3) << 24;
  268. *value = val;
  269. return retval;
  270. }
  271. /* ------------------------------------------------------------------ */
  272. static int blackbird_mbox_func(void *priv, u32 command, int in,
  273. int out, u32 data[CX2341X_MBOX_MAX_DATA])
  274. {
  275. struct cx8802_dev *dev = priv;
  276. unsigned long timeout;
  277. u32 value, flag, retval;
  278. int i;
  279. dprintk(1, "%s: 0x%X\n", __func__, command);
  280. /*
  281. * this may not be 100% safe if we can't read any memory location
  282. * without side effects
  283. */
  284. memory_read(dev->core, dev->mailbox - 4, &value);
  285. if (value != 0x12345678) {
  286. dprintk(0,
  287. "Firmware and/or mailbox pointer not initialized or corrupted\n");
  288. return -EIO;
  289. }
  290. memory_read(dev->core, dev->mailbox, &flag);
  291. if (flag) {
  292. dprintk(0, "ERROR: Mailbox appears to be in use (%x)\n", flag);
  293. return -EIO;
  294. }
  295. flag |= 1; /* tell 'em we're working on it */
  296. memory_write(dev->core, dev->mailbox, flag);
  297. /* write command + args + fill remaining with zeros */
  298. memory_write(dev->core, dev->mailbox + 1, command); /* command code */
  299. /* timeout */
  300. memory_write(dev->core, dev->mailbox + 3, IVTV_API_STD_TIMEOUT);
  301. for (i = 0; i < in; i++) {
  302. memory_write(dev->core, dev->mailbox + 4 + i, data[i]);
  303. dprintk(1, "API Input %d = %d\n", i, data[i]);
  304. }
  305. for (; i < CX2341X_MBOX_MAX_DATA; i++)
  306. memory_write(dev->core, dev->mailbox + 4 + i, 0);
  307. flag |= 3; /* tell 'em we're done writing */
  308. memory_write(dev->core, dev->mailbox, flag);
  309. /* wait for firmware to handle the API command */
  310. timeout = jiffies + msecs_to_jiffies(1000);
  311. for (;;) {
  312. memory_read(dev->core, dev->mailbox, &flag);
  313. if (0 != (flag & 4))
  314. break;
  315. if (time_after(jiffies, timeout)) {
  316. dprintk(0, "ERROR: API Mailbox timeout %x\n", command);
  317. return -EIO;
  318. }
  319. udelay(10);
  320. }
  321. /* read output values */
  322. for (i = 0; i < out; i++) {
  323. memory_read(dev->core, dev->mailbox + 4 + i, data + i);
  324. dprintk(1, "API Output %d = %d\n", i, data[i]);
  325. }
  326. memory_read(dev->core, dev->mailbox + 2, &retval);
  327. dprintk(1, "API result = %d\n", retval);
  328. flag = 0;
  329. memory_write(dev->core, dev->mailbox, flag);
  330. return retval;
  331. }
  332. /* ------------------------------------------------------------------ */
  333. /*
  334. * We don't need to call the API often, so using just one mailbox
  335. * will probably suffice
  336. */
  337. static int blackbird_api_cmd(struct cx8802_dev *dev, u32 command,
  338. u32 inputcnt, u32 outputcnt, ...)
  339. {
  340. u32 data[CX2341X_MBOX_MAX_DATA];
  341. va_list vargs;
  342. int i, err;
  343. va_start(vargs, outputcnt);
  344. for (i = 0; i < inputcnt; i++)
  345. data[i] = va_arg(vargs, int);
  346. err = blackbird_mbox_func(dev, command, inputcnt, outputcnt, data);
  347. for (i = 0; i < outputcnt; i++) {
  348. int *vptr = va_arg(vargs, int *);
  349. *vptr = data[i];
  350. }
  351. va_end(vargs);
  352. return err;
  353. }
  354. static int blackbird_find_mailbox(struct cx8802_dev *dev)
  355. {
  356. u32 signature[4] = {0x12345678, 0x34567812, 0x56781234, 0x78123456};
  357. int signaturecnt = 0;
  358. u32 value;
  359. int i;
  360. for (i = 0; i < BLACKBIRD_FIRM_IMAGE_SIZE; i++) {
  361. memory_read(dev->core, i, &value);
  362. if (value == signature[signaturecnt])
  363. signaturecnt++;
  364. else
  365. signaturecnt = 0;
  366. if (signaturecnt == 4) {
  367. dprintk(1, "Mailbox signature found\n");
  368. return i + 1;
  369. }
  370. }
  371. dprintk(0, "Mailbox signature values not found!\n");
  372. return -EIO;
  373. }
  374. static int blackbird_load_firmware(struct cx8802_dev *dev)
  375. {
  376. static const unsigned char magic[8] = {
  377. 0xa7, 0x0d, 0x00, 0x00, 0x66, 0xbb, 0x55, 0xaa
  378. };
  379. const struct firmware *firmware;
  380. int i, retval = 0;
  381. u32 value = 0;
  382. u32 checksum = 0;
  383. __le32 *dataptr;
  384. retval = register_write(dev->core, IVTV_REG_VPU, 0xFFFFFFED);
  385. retval |= register_write(dev->core, IVTV_REG_HW_BLOCKS,
  386. IVTV_CMD_HW_BLOCKS_RST);
  387. retval |= register_write(dev->core, IVTV_REG_ENC_SDRAM_REFRESH,
  388. 0x80000640);
  389. retval |= register_write(dev->core, IVTV_REG_ENC_SDRAM_PRECHARGE,
  390. 0x1A);
  391. usleep_range(10000, 20000);
  392. retval |= register_write(dev->core, IVTV_REG_APU, 0);
  393. if (retval < 0)
  394. dprintk(0, "Error with register_write\n");
  395. retval = request_firmware(&firmware, CX2341X_FIRM_ENC_FILENAME,
  396. &dev->pci->dev);
  397. if (retval != 0)
  398. return -EIO;
  399. if (firmware->size != BLACKBIRD_FIRM_IMAGE_SIZE) {
  400. pr_err("Firmware size mismatch (have %zd, expected %d)\n",
  401. firmware->size, BLACKBIRD_FIRM_IMAGE_SIZE);
  402. release_firmware(firmware);
  403. return -EINVAL;
  404. }
  405. if (memcmp(firmware->data, magic, 8) != 0) {
  406. pr_err("Firmware magic mismatch, wrong file?\n");
  407. release_firmware(firmware);
  408. return -EINVAL;
  409. }
  410. /* transfer to the chip */
  411. dprintk(1, "Loading firmware ...\n");
  412. dataptr = (__le32 *)firmware->data;
  413. for (i = 0; i < (firmware->size >> 2); i++) {
  414. value = le32_to_cpu(*dataptr);
  415. checksum += ~value;
  416. memory_write(dev->core, i, value);
  417. dataptr++;
  418. }
  419. /* read back to verify with the checksum */
  420. for (i--; i >= 0; i--) {
  421. memory_read(dev->core, i, &value);
  422. checksum -= ~value;
  423. }
  424. release_firmware(firmware);
  425. if (checksum) {
  426. pr_err("Firmware load might have failed (checksum mismatch).\n");
  427. return -EIO;
  428. }
  429. dprintk(0, "Firmware upload successful.\n");
  430. retval |= register_write(dev->core, IVTV_REG_HW_BLOCKS,
  431. IVTV_CMD_HW_BLOCKS_RST);
  432. retval |= register_read(dev->core, IVTV_REG_SPU, &value);
  433. retval |= register_write(dev->core, IVTV_REG_SPU, value & 0xFFFFFFFE);
  434. usleep_range(10000, 20000);
  435. retval |= register_read(dev->core, IVTV_REG_VPU, &value);
  436. retval |= register_write(dev->core, IVTV_REG_VPU, value & 0xFFFFFFE8);
  437. if (retval < 0)
  438. dprintk(0, "Error with register_write\n");
  439. return 0;
  440. }
  441. /*
  442. * Settings used by the windows tv app for PVR2000:
  443. * =================================================================================================================
  444. * Profile | Codec | Resolution | CBR/VBR | Video Qlty | V. Bitrate | Frmrate | Audio Codec | A. Bitrate | A. Mode
  445. * -----------------------------------------------------------------------------------------------------------------
  446. * MPEG-1 | MPEG1 | 352x288PAL | (CBR) | 1000:Optimal | 2000 Kbps | 25fps | MPG1 Layer2 | 224kbps | Stereo
  447. * MPEG-2 | MPEG2 | 720x576PAL | VBR | 600 :Good | 4000 Kbps | 25fps | MPG1 Layer2 | 224kbps | Stereo
  448. * VCD | MPEG1 | 352x288PAL | (CBR) | 1000:Optimal | 1150 Kbps | 25fps | MPG1 Layer2 | 224kbps | Stereo
  449. * DVD | MPEG2 | 720x576PAL | VBR | 600 :Good | 6000 Kbps | 25fps | MPG1 Layer2 | 224kbps | Stereo
  450. * DB* DVD | MPEG2 | 720x576PAL | CBR | 600 :Good | 6000 Kbps | 25fps | MPG1 Layer2 | 224kbps | Stereo
  451. * =================================================================================================================
  452. * [*] DB: "DirectBurn"
  453. */
  454. static void blackbird_codec_settings(struct cx8802_dev *dev)
  455. {
  456. struct cx88_core *core = dev->core;
  457. /* assign frame size */
  458. blackbird_api_cmd(dev, CX2341X_ENC_SET_FRAME_SIZE, 2, 0,
  459. core->height, core->width);
  460. dev->cxhdl.width = core->width;
  461. dev->cxhdl.height = core->height;
  462. cx2341x_handler_set_50hz(&dev->cxhdl,
  463. dev->core->tvnorm & V4L2_STD_625_50);
  464. cx2341x_handler_setup(&dev->cxhdl);
  465. }
  466. static int blackbird_initialize_codec(struct cx8802_dev *dev)
  467. {
  468. struct cx88_core *core = dev->core;
  469. int version;
  470. int retval;
  471. dprintk(1, "Initialize codec\n");
  472. retval = blackbird_api_cmd(dev, CX2341X_ENC_PING_FW, 0, 0); /* ping */
  473. if (retval < 0) {
  474. /* ping was not successful, reset and upload firmware */
  475. cx_write(MO_SRST_IO, 0); /* SYS_RSTO=0 */
  476. cx_write(MO_SRST_IO, 1); /* SYS_RSTO=1 */
  477. retval = blackbird_load_firmware(dev);
  478. if (retval < 0)
  479. return retval;
  480. retval = blackbird_find_mailbox(dev);
  481. if (retval < 0)
  482. return -1;
  483. dev->mailbox = retval;
  484. /* ping */
  485. retval = blackbird_api_cmd(dev, CX2341X_ENC_PING_FW, 0, 0);
  486. if (retval < 0) {
  487. dprintk(0, "ERROR: Firmware ping failed!\n");
  488. return -1;
  489. }
  490. retval = blackbird_api_cmd(dev, CX2341X_ENC_GET_VERSION,
  491. 0, 1, &version);
  492. if (retval < 0) {
  493. dprintk(0,
  494. "ERROR: Firmware get encoder version failed!\n");
  495. return -1;
  496. }
  497. dprintk(0, "Firmware version is 0x%08x\n", version);
  498. }
  499. cx_write(MO_PINMUX_IO, 0x88); /* 656-8bit IO and enable MPEG parallel IO */
  500. cx_clear(MO_INPUT_FORMAT, 0x100); /* chroma subcarrier lock to normal? */
  501. cx_write(MO_VBOS_CONTROL, 0x84A00); /* no 656 mode, 8-bit pixels, disable VBI */
  502. cx_clear(MO_OUTPUT_FORMAT, 0x0008); /* Normal Y-limits to let the mpeg encoder sync */
  503. blackbird_codec_settings(dev);
  504. blackbird_api_cmd(dev, CX2341X_ENC_SET_NUM_VSYNC_LINES, 2, 0,
  505. BLACKBIRD_FIELD1_SAA7115, BLACKBIRD_FIELD2_SAA7115);
  506. blackbird_api_cmd(dev, CX2341X_ENC_SET_PLACEHOLDER, 12, 0,
  507. BLACKBIRD_CUSTOM_EXTENSION_USR_DATA,
  508. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
  509. return 0;
  510. }
  511. static int blackbird_start_codec(struct cx8802_dev *dev)
  512. {
  513. struct cx88_core *core = dev->core;
  514. /* start capturing to the host interface */
  515. u32 reg;
  516. int i;
  517. int lastchange = -1;
  518. int lastval = 0;
  519. for (i = 0; (i < 10) && (i < (lastchange + 4)); i++) {
  520. reg = cx_read(AUD_STATUS);
  521. dprintk(1, "AUD_STATUS:%dL: 0x%x\n", i, reg);
  522. if ((reg & 0x0F) != lastval) {
  523. lastval = reg & 0x0F;
  524. lastchange = i;
  525. }
  526. msleep(100);
  527. }
  528. /* unmute audio source */
  529. cx_clear(AUD_VOL_CTL, (1 << 6));
  530. blackbird_api_cmd(dev, CX2341X_ENC_REFRESH_INPUT, 0, 0);
  531. /* initialize the video input */
  532. blackbird_api_cmd(dev, CX2341X_ENC_INITIALIZE_INPUT, 0, 0);
  533. cx2341x_handler_set_busy(&dev->cxhdl, 1);
  534. /* start capturing to the host interface */
  535. blackbird_api_cmd(dev, CX2341X_ENC_START_CAPTURE, 2, 0,
  536. BLACKBIRD_MPEG_CAPTURE, BLACKBIRD_RAW_BITS_NONE);
  537. return 0;
  538. }
  539. static int blackbird_stop_codec(struct cx8802_dev *dev)
  540. {
  541. blackbird_api_cmd(dev, CX2341X_ENC_STOP_CAPTURE, 3, 0,
  542. BLACKBIRD_END_NOW,
  543. BLACKBIRD_MPEG_CAPTURE,
  544. BLACKBIRD_RAW_BITS_NONE);
  545. cx2341x_handler_set_busy(&dev->cxhdl, 0);
  546. return 0;
  547. }
  548. /* ------------------------------------------------------------------ */
  549. static int queue_setup(struct vb2_queue *q,
  550. unsigned int *num_buffers, unsigned int *num_planes,
  551. unsigned int sizes[], struct device *alloc_devs[])
  552. {
  553. struct cx8802_dev *dev = q->drv_priv;
  554. *num_planes = 1;
  555. dev->ts_packet_size = 188 * 4;
  556. dev->ts_packet_count = 32;
  557. sizes[0] = dev->ts_packet_size * dev->ts_packet_count;
  558. return 0;
  559. }
  560. static int buffer_prepare(struct vb2_buffer *vb)
  561. {
  562. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  563. struct cx8802_dev *dev = vb->vb2_queue->drv_priv;
  564. struct cx88_buffer *buf = container_of(vbuf, struct cx88_buffer, vb);
  565. return cx8802_buf_prepare(vb->vb2_queue, dev, buf);
  566. }
  567. static void buffer_finish(struct vb2_buffer *vb)
  568. {
  569. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  570. struct cx8802_dev *dev = vb->vb2_queue->drv_priv;
  571. struct cx88_buffer *buf = container_of(vbuf, struct cx88_buffer, vb);
  572. struct cx88_riscmem *risc = &buf->risc;
  573. if (risc->cpu)
  574. pci_free_consistent(dev->pci, risc->size, risc->cpu, risc->dma);
  575. memset(risc, 0, sizeof(*risc));
  576. }
  577. static void buffer_queue(struct vb2_buffer *vb)
  578. {
  579. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  580. struct cx8802_dev *dev = vb->vb2_queue->drv_priv;
  581. struct cx88_buffer *buf = container_of(vbuf, struct cx88_buffer, vb);
  582. cx8802_buf_queue(dev, buf);
  583. }
  584. static int start_streaming(struct vb2_queue *q, unsigned int count)
  585. {
  586. struct cx8802_dev *dev = q->drv_priv;
  587. struct cx88_dmaqueue *dmaq = &dev->mpegq;
  588. struct cx8802_driver *drv;
  589. struct cx88_buffer *buf;
  590. unsigned long flags;
  591. int err;
  592. /* Make sure we can acquire the hardware */
  593. drv = cx8802_get_driver(dev, CX88_MPEG_BLACKBIRD);
  594. if (!drv) {
  595. dprintk(1, "%s: blackbird driver is not loaded\n", __func__);
  596. err = -ENODEV;
  597. goto fail;
  598. }
  599. err = drv->request_acquire(drv);
  600. if (err != 0) {
  601. dprintk(1, "%s: Unable to acquire hardware, %d\n", __func__,
  602. err);
  603. goto fail;
  604. }
  605. if (blackbird_initialize_codec(dev) < 0) {
  606. drv->request_release(drv);
  607. err = -EINVAL;
  608. goto fail;
  609. }
  610. err = blackbird_start_codec(dev);
  611. if (err == 0) {
  612. buf = list_entry(dmaq->active.next, struct cx88_buffer, list);
  613. cx8802_start_dma(dev, dmaq, buf);
  614. return 0;
  615. }
  616. fail:
  617. spin_lock_irqsave(&dev->slock, flags);
  618. while (!list_empty(&dmaq->active)) {
  619. struct cx88_buffer *buf = list_entry(dmaq->active.next,
  620. struct cx88_buffer, list);
  621. list_del(&buf->list);
  622. vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_QUEUED);
  623. }
  624. spin_unlock_irqrestore(&dev->slock, flags);
  625. return err;
  626. }
  627. static void stop_streaming(struct vb2_queue *q)
  628. {
  629. struct cx8802_dev *dev = q->drv_priv;
  630. struct cx88_dmaqueue *dmaq = &dev->mpegq;
  631. struct cx8802_driver *drv = NULL;
  632. unsigned long flags;
  633. cx8802_cancel_buffers(dev);
  634. blackbird_stop_codec(dev);
  635. /* Make sure we release the hardware */
  636. drv = cx8802_get_driver(dev, CX88_MPEG_BLACKBIRD);
  637. WARN_ON(!drv);
  638. if (drv)
  639. drv->request_release(drv);
  640. spin_lock_irqsave(&dev->slock, flags);
  641. while (!list_empty(&dmaq->active)) {
  642. struct cx88_buffer *buf = list_entry(dmaq->active.next,
  643. struct cx88_buffer, list);
  644. list_del(&buf->list);
  645. vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
  646. }
  647. spin_unlock_irqrestore(&dev->slock, flags);
  648. }
  649. static const struct vb2_ops blackbird_qops = {
  650. .queue_setup = queue_setup,
  651. .buf_prepare = buffer_prepare,
  652. .buf_finish = buffer_finish,
  653. .buf_queue = buffer_queue,
  654. .wait_prepare = vb2_ops_wait_prepare,
  655. .wait_finish = vb2_ops_wait_finish,
  656. .start_streaming = start_streaming,
  657. .stop_streaming = stop_streaming,
  658. };
  659. /* ------------------------------------------------------------------ */
  660. static int vidioc_querycap(struct file *file, void *priv,
  661. struct v4l2_capability *cap)
  662. {
  663. struct cx8802_dev *dev = video_drvdata(file);
  664. struct cx88_core *core = dev->core;
  665. strcpy(cap->driver, "cx88_blackbird");
  666. sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
  667. return cx88_querycap(file, core, cap);
  668. }
  669. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  670. struct v4l2_fmtdesc *f)
  671. {
  672. if (f->index != 0)
  673. return -EINVAL;
  674. strlcpy(f->description, "MPEG", sizeof(f->description));
  675. f->pixelformat = V4L2_PIX_FMT_MPEG;
  676. f->flags = V4L2_FMT_FLAG_COMPRESSED;
  677. return 0;
  678. }
  679. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  680. struct v4l2_format *f)
  681. {
  682. struct cx8802_dev *dev = video_drvdata(file);
  683. struct cx88_core *core = dev->core;
  684. f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
  685. f->fmt.pix.bytesperline = 0;
  686. f->fmt.pix.sizeimage = dev->ts_packet_size * dev->ts_packet_count;
  687. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  688. f->fmt.pix.width = core->width;
  689. f->fmt.pix.height = core->height;
  690. f->fmt.pix.field = core->field;
  691. return 0;
  692. }
  693. static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  694. struct v4l2_format *f)
  695. {
  696. struct cx8802_dev *dev = video_drvdata(file);
  697. struct cx88_core *core = dev->core;
  698. unsigned int maxw, maxh;
  699. enum v4l2_field field;
  700. f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
  701. f->fmt.pix.bytesperline = 0;
  702. f->fmt.pix.sizeimage = dev->ts_packet_size * dev->ts_packet_count;
  703. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  704. maxw = norm_maxw(core->tvnorm);
  705. maxh = norm_maxh(core->tvnorm);
  706. field = f->fmt.pix.field;
  707. switch (field) {
  708. case V4L2_FIELD_TOP:
  709. case V4L2_FIELD_BOTTOM:
  710. case V4L2_FIELD_INTERLACED:
  711. case V4L2_FIELD_SEQ_BT:
  712. case V4L2_FIELD_SEQ_TB:
  713. break;
  714. default:
  715. field = (f->fmt.pix.height > maxh / 2)
  716. ? V4L2_FIELD_INTERLACED
  717. : V4L2_FIELD_BOTTOM;
  718. break;
  719. }
  720. if (V4L2_FIELD_HAS_T_OR_B(field))
  721. maxh /= 2;
  722. v4l_bound_align_image(&f->fmt.pix.width, 48, maxw, 2,
  723. &f->fmt.pix.height, 32, maxh, 0, 0);
  724. f->fmt.pix.field = field;
  725. return 0;
  726. }
  727. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  728. struct v4l2_format *f)
  729. {
  730. struct cx8802_dev *dev = video_drvdata(file);
  731. struct cx88_core *core = dev->core;
  732. if (vb2_is_busy(&dev->vb2_mpegq))
  733. return -EBUSY;
  734. if (core->v4ldev && (vb2_is_busy(&core->v4ldev->vb2_vidq) ||
  735. vb2_is_busy(&core->v4ldev->vb2_vbiq)))
  736. return -EBUSY;
  737. vidioc_try_fmt_vid_cap(file, priv, f);
  738. core->width = f->fmt.pix.width;
  739. core->height = f->fmt.pix.height;
  740. core->field = f->fmt.pix.field;
  741. cx88_set_scale(core, f->fmt.pix.width, f->fmt.pix.height,
  742. f->fmt.pix.field);
  743. blackbird_api_cmd(dev, CX2341X_ENC_SET_FRAME_SIZE, 2, 0,
  744. f->fmt.pix.height, f->fmt.pix.width);
  745. return 0;
  746. }
  747. static int vidioc_s_frequency(struct file *file, void *priv,
  748. const struct v4l2_frequency *f)
  749. {
  750. struct cx8802_dev *dev = video_drvdata(file);
  751. struct cx88_core *core = dev->core;
  752. bool streaming;
  753. if (unlikely(core->board.tuner_type == UNSET))
  754. return -EINVAL;
  755. if (unlikely(f->tuner != 0))
  756. return -EINVAL;
  757. streaming = vb2_start_streaming_called(&dev->vb2_mpegq);
  758. if (streaming)
  759. blackbird_stop_codec(dev);
  760. cx88_set_freq(core, f);
  761. blackbird_initialize_codec(dev);
  762. cx88_set_scale(core, core->width, core->height, core->field);
  763. if (streaming)
  764. blackbird_start_codec(dev);
  765. return 0;
  766. }
  767. static int vidioc_log_status(struct file *file, void *priv)
  768. {
  769. struct cx8802_dev *dev = video_drvdata(file);
  770. struct cx88_core *core = dev->core;
  771. char name[32 + 2];
  772. snprintf(name, sizeof(name), "%s/2", core->name);
  773. call_all(core, core, log_status);
  774. v4l2_ctrl_handler_log_status(&dev->cxhdl.hdl, name);
  775. return 0;
  776. }
  777. static int vidioc_enum_input(struct file *file, void *priv,
  778. struct v4l2_input *i)
  779. {
  780. struct cx8802_dev *dev = video_drvdata(file);
  781. struct cx88_core *core = dev->core;
  782. return cx88_enum_input(core, i);
  783. }
  784. static int vidioc_g_frequency(struct file *file, void *priv,
  785. struct v4l2_frequency *f)
  786. {
  787. struct cx8802_dev *dev = video_drvdata(file);
  788. struct cx88_core *core = dev->core;
  789. if (unlikely(core->board.tuner_type == UNSET))
  790. return -EINVAL;
  791. if (unlikely(f->tuner != 0))
  792. return -EINVAL;
  793. f->frequency = core->freq;
  794. call_all(core, tuner, g_frequency, f);
  795. return 0;
  796. }
  797. static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  798. {
  799. struct cx8802_dev *dev = video_drvdata(file);
  800. struct cx88_core *core = dev->core;
  801. *i = core->input;
  802. return 0;
  803. }
  804. static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  805. {
  806. struct cx8802_dev *dev = video_drvdata(file);
  807. struct cx88_core *core = dev->core;
  808. if (i >= 4)
  809. return -EINVAL;
  810. if (!INPUT(i).type)
  811. return -EINVAL;
  812. cx88_newstation(core);
  813. cx88_video_mux(core, i);
  814. return 0;
  815. }
  816. static int vidioc_g_tuner(struct file *file, void *priv,
  817. struct v4l2_tuner *t)
  818. {
  819. struct cx8802_dev *dev = video_drvdata(file);
  820. struct cx88_core *core = dev->core;
  821. u32 reg;
  822. if (unlikely(core->board.tuner_type == UNSET))
  823. return -EINVAL;
  824. if (t->index != 0)
  825. return -EINVAL;
  826. strcpy(t->name, "Television");
  827. t->capability = V4L2_TUNER_CAP_NORM;
  828. t->rangehigh = 0xffffffffUL;
  829. call_all(core, tuner, g_tuner, t);
  830. cx88_get_stereo(core, t);
  831. reg = cx_read(MO_DEVICE_STATUS);
  832. t->signal = (reg & (1 << 5)) ? 0xffff : 0x0000;
  833. return 0;
  834. }
  835. static int vidioc_s_tuner(struct file *file, void *priv,
  836. const struct v4l2_tuner *t)
  837. {
  838. struct cx8802_dev *dev = video_drvdata(file);
  839. struct cx88_core *core = dev->core;
  840. if (core->board.tuner_type == UNSET)
  841. return -EINVAL;
  842. if (t->index != 0)
  843. return -EINVAL;
  844. cx88_set_stereo(core, t->audmode, 1);
  845. return 0;
  846. }
  847. static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *tvnorm)
  848. {
  849. struct cx8802_dev *dev = video_drvdata(file);
  850. struct cx88_core *core = dev->core;
  851. *tvnorm = core->tvnorm;
  852. return 0;
  853. }
  854. static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id id)
  855. {
  856. struct cx8802_dev *dev = video_drvdata(file);
  857. struct cx88_core *core = dev->core;
  858. return cx88_set_tvnorm(core, id);
  859. }
  860. static const struct v4l2_file_operations mpeg_fops = {
  861. .owner = THIS_MODULE,
  862. .open = v4l2_fh_open,
  863. .release = vb2_fop_release,
  864. .read = vb2_fop_read,
  865. .poll = vb2_fop_poll,
  866. .mmap = vb2_fop_mmap,
  867. .unlocked_ioctl = video_ioctl2,
  868. };
  869. static const struct v4l2_ioctl_ops mpeg_ioctl_ops = {
  870. .vidioc_querycap = vidioc_querycap,
  871. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  872. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  873. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  874. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  875. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  876. .vidioc_querybuf = vb2_ioctl_querybuf,
  877. .vidioc_qbuf = vb2_ioctl_qbuf,
  878. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  879. .vidioc_streamon = vb2_ioctl_streamon,
  880. .vidioc_streamoff = vb2_ioctl_streamoff,
  881. .vidioc_s_frequency = vidioc_s_frequency,
  882. .vidioc_log_status = vidioc_log_status,
  883. .vidioc_enum_input = vidioc_enum_input,
  884. .vidioc_g_frequency = vidioc_g_frequency,
  885. .vidioc_g_input = vidioc_g_input,
  886. .vidioc_s_input = vidioc_s_input,
  887. .vidioc_g_tuner = vidioc_g_tuner,
  888. .vidioc_s_tuner = vidioc_s_tuner,
  889. .vidioc_g_std = vidioc_g_std,
  890. .vidioc_s_std = vidioc_s_std,
  891. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  892. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  893. };
  894. static const struct video_device cx8802_mpeg_template = {
  895. .name = "cx8802",
  896. .fops = &mpeg_fops,
  897. .ioctl_ops = &mpeg_ioctl_ops,
  898. .tvnorms = CX88_NORMS,
  899. };
  900. /* ------------------------------------------------------------------ */
  901. /* The CX8802 MPEG API will call this when we can use the hardware */
  902. static int cx8802_blackbird_advise_acquire(struct cx8802_driver *drv)
  903. {
  904. struct cx88_core *core = drv->core;
  905. int err = 0;
  906. switch (core->boardnr) {
  907. case CX88_BOARD_HAUPPAUGE_HVR1300:
  908. /*
  909. * By default, core setup will leave the cx22702 out of reset,
  910. * on the bus.
  911. * We left the hardware on power up with the cx22702 active.
  912. * We're being given access to re-arrange the GPIOs.
  913. * Take the bus off the cx22702 and put the cx23416 on it.
  914. */
  915. /* Toggle reset on cx22702 leaving i2c active */
  916. cx_set(MO_GP0_IO, 0x00000080);
  917. udelay(1000);
  918. cx_clear(MO_GP0_IO, 0x00000080);
  919. udelay(50);
  920. cx_set(MO_GP0_IO, 0x00000080);
  921. udelay(1000);
  922. /* tri-state the cx22702 pins */
  923. cx_set(MO_GP0_IO, 0x00000004);
  924. udelay(1000);
  925. break;
  926. default:
  927. err = -ENODEV;
  928. }
  929. return err;
  930. }
  931. /* The CX8802 MPEG API will call this when we need to release the hardware */
  932. static int cx8802_blackbird_advise_release(struct cx8802_driver *drv)
  933. {
  934. struct cx88_core *core = drv->core;
  935. int err = 0;
  936. switch (core->boardnr) {
  937. case CX88_BOARD_HAUPPAUGE_HVR1300:
  938. /* Exit leaving the cx23416 on the bus */
  939. break;
  940. default:
  941. err = -ENODEV;
  942. }
  943. return err;
  944. }
  945. static void blackbird_unregister_video(struct cx8802_dev *dev)
  946. {
  947. video_unregister_device(&dev->mpeg_dev);
  948. }
  949. static int blackbird_register_video(struct cx8802_dev *dev)
  950. {
  951. int err;
  952. cx88_vdev_init(dev->core, dev->pci, &dev->mpeg_dev,
  953. &cx8802_mpeg_template, "mpeg");
  954. dev->mpeg_dev.ctrl_handler = &dev->cxhdl.hdl;
  955. video_set_drvdata(&dev->mpeg_dev, dev);
  956. dev->mpeg_dev.queue = &dev->vb2_mpegq;
  957. err = video_register_device(&dev->mpeg_dev, VFL_TYPE_GRABBER, -1);
  958. if (err < 0) {
  959. pr_info("can't register mpeg device\n");
  960. return err;
  961. }
  962. pr_info("registered device %s [mpeg]\n",
  963. video_device_node_name(&dev->mpeg_dev));
  964. return 0;
  965. }
  966. /* ----------------------------------------------------------- */
  967. static int cx8802_blackbird_probe(struct cx8802_driver *drv)
  968. {
  969. struct cx88_core *core = drv->core;
  970. struct cx8802_dev *dev = core->dvbdev;
  971. struct vb2_queue *q;
  972. int err;
  973. dprintk(1, "%s\n", __func__);
  974. dprintk(1, " ->being probed by Card=%d Name=%s, PCI %02x:%02x\n",
  975. core->boardnr,
  976. core->name,
  977. core->pci_bus,
  978. core->pci_slot);
  979. err = -ENODEV;
  980. if (!(core->board.mpeg & CX88_MPEG_BLACKBIRD))
  981. goto fail_core;
  982. dev->cxhdl.port = CX2341X_PORT_STREAMING;
  983. dev->cxhdl.width = core->width;
  984. dev->cxhdl.height = core->height;
  985. dev->cxhdl.func = blackbird_mbox_func;
  986. dev->cxhdl.priv = dev;
  987. err = cx2341x_handler_init(&dev->cxhdl, 36);
  988. if (err)
  989. goto fail_core;
  990. v4l2_ctrl_add_handler(&dev->cxhdl.hdl, &core->video_hdl, NULL);
  991. /* blackbird stuff */
  992. pr_info("cx23416 based mpeg encoder (blackbird reference design)\n");
  993. host_setup(dev->core);
  994. blackbird_initialize_codec(dev);
  995. /* initial device configuration: needed ? */
  996. // init_controls(core);
  997. cx88_set_tvnorm(core, core->tvnorm);
  998. cx88_video_mux(core, 0);
  999. cx2341x_handler_set_50hz(&dev->cxhdl, core->height == 576);
  1000. cx2341x_handler_setup(&dev->cxhdl);
  1001. q = &dev->vb2_mpegq;
  1002. q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1003. q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ;
  1004. q->gfp_flags = GFP_DMA32;
  1005. q->min_buffers_needed = 2;
  1006. q->drv_priv = dev;
  1007. q->buf_struct_size = sizeof(struct cx88_buffer);
  1008. q->ops = &blackbird_qops;
  1009. q->mem_ops = &vb2_dma_sg_memops;
  1010. q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  1011. q->lock = &core->lock;
  1012. q->dev = &dev->pci->dev;
  1013. err = vb2_queue_init(q);
  1014. if (err < 0)
  1015. goto fail_core;
  1016. blackbird_register_video(dev);
  1017. return 0;
  1018. fail_core:
  1019. return err;
  1020. }
  1021. static int cx8802_blackbird_remove(struct cx8802_driver *drv)
  1022. {
  1023. struct cx88_core *core = drv->core;
  1024. struct cx8802_dev *dev = core->dvbdev;
  1025. /* blackbird */
  1026. blackbird_unregister_video(drv->core->dvbdev);
  1027. v4l2_ctrl_handler_free(&dev->cxhdl.hdl);
  1028. return 0;
  1029. }
  1030. static struct cx8802_driver cx8802_blackbird_driver = {
  1031. .type_id = CX88_MPEG_BLACKBIRD,
  1032. .hw_access = CX8802_DRVCTL_SHARED,
  1033. .probe = cx8802_blackbird_probe,
  1034. .remove = cx8802_blackbird_remove,
  1035. .advise_acquire = cx8802_blackbird_advise_acquire,
  1036. .advise_release = cx8802_blackbird_advise_release,
  1037. };
  1038. static int __init blackbird_init(void)
  1039. {
  1040. pr_info("cx2388x blackbird driver version %s loaded\n",
  1041. CX88_VERSION);
  1042. return cx8802_register_driver(&cx8802_blackbird_driver);
  1043. }
  1044. static void __exit blackbird_fini(void)
  1045. {
  1046. cx8802_unregister_driver(&cx8802_blackbird_driver);
  1047. }
  1048. module_init(blackbird_init);
  1049. module_exit(blackbird_fini);