cx88-blackbird.c 38 KB

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