cx88-blackbird.c 34 KB

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