cx88-blackbird.c 38 KB

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