cx88-blackbird.c 38 KB

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