cx88-blackbird.c 36 KB

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