cx88-blackbird.c 39 KB

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