cx88-blackbird.c 41 KB

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