cx88-blackbird.c 35 KB

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