cx88-blackbird.c 39 KB

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