cx88-blackbird.c 39 KB

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