cx88-blackbird.c 38 KB

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