cx88-blackbird.c 38 KB

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