cx88-blackbird.c 38 KB

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