cx88-blackbird.c 36 KB

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