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