cx88-blackbird.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778
  1. /*
  2. *
  3. * Support for a cx23416 mpeg encoder via cx2388x host port.
  4. * "blackbird" reference design.
  5. *
  6. * (c) 2004 Jelle Foks <jelle@foks.8m.com>
  7. * (c) 2004 Gerd Knorr <kraxel@bytesex.org>
  8. *
  9. * Includes parts from the ivtv driver( http://ivtv.sourceforge.net/),
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24. */
  25. #include <linux/module.h>
  26. #include <linux/moduleparam.h>
  27. #include <linux/init.h>
  28. #include <linux/fs.h>
  29. #include <linux/delay.h>
  30. #include <linux/device.h>
  31. #include <linux/firmware.h>
  32. #include <media/v4l2-common.h>
  33. #include <media/cx2341x.h>
  34. #include "cx88.h"
  35. MODULE_DESCRIPTION("driver for cx2388x/cx23416 based mpeg encoder cards");
  36. MODULE_AUTHOR("Jelle Foks <jelle@foks.8m.com>, Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
  37. MODULE_LICENSE("GPL");
  38. static unsigned int mpegbufs = 32;
  39. module_param(mpegbufs,int,0644);
  40. MODULE_PARM_DESC(mpegbufs,"number of mpeg buffers, range 2-32");
  41. static unsigned int debug = 0;
  42. module_param(debug,int,0644);
  43. MODULE_PARM_DESC(debug,"enable debug messages [blackbird]");
  44. #define dprintk(level,fmt, arg...) if (debug >= level) \
  45. printk(KERN_DEBUG "%s/2-bb: " fmt, dev->core->name , ## arg)
  46. static LIST_HEAD(cx8802_devlist);
  47. /* ------------------------------------------------------------------ */
  48. #define BLACKBIRD_FIRM_ENC_FILENAME "v4l-cx2341x-enc.fw"
  49. #define BLACKBIRD_FIRM_IMAGE_SIZE 256*1024
  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_video_bitrate_type {
  76. BLACKBIRD_VIDEO_VBR,
  77. BLACKBIRD_VIDEO_CBR
  78. };
  79. #define BLACKBIRD_PEAK_RATE_DIVISOR 400
  80. enum blackbird_mux_rate {
  81. BLACKBIRD_MUX_RATE_DEFAULT,
  82. /* dvd mux rate: multiply by 400 to get the actual rate */
  83. BLACKBIRD_MUX_RATE_DVD = 25200
  84. };
  85. enum blackbird_aspect_ratio {
  86. BLACKBIRD_ASPECT_RATIO_FORBIDDEN,
  87. BLACKBIRD_ASPECT_RATIO_1_1_SQUARE,
  88. BLACKBIRD_ASPECT_RATIO_4_3,
  89. BLACKBIRD_ASPECT_RATIO_16_9,
  90. BLACKBIRD_ASPECT_RATIO_221_100,
  91. BLACKBIRD_ASPECT_RATIO_RESERVED
  92. };
  93. enum blackbird_dnr_bits {
  94. BLACKBIRD_DNR_BITS_MANUAL,
  95. BLACKBIRD_DNR_BITS_AUTO_SPATIAL,
  96. BLACKBIRD_DNR_BITS_AUTO_TEMPORAL,
  97. BLACKBIRD_DNR_BITS_AUTO
  98. };
  99. enum blackbird_median_filter {
  100. BLACKBIRD_MEDIAN_FILTER_DISABLED,
  101. BLACKBIRD_MEDIAN_FILTER_HORIZONTAL,
  102. BLACKBIRD_MEDIAN_FILTER_VERTICAL,
  103. BLACKBIRD_MEDIAN_FILTER_HV,
  104. BLACKBIRD_MEDIAN_FILTER_DIAGONAL
  105. };
  106. enum blackbird_spatial_filter_luma {
  107. BLACKBIRD_SPATIAL_FILTER_LUMA_DISABLED,
  108. BLACKBIRD_SPATIAL_FILTER_LUMA_1D_HORIZ,
  109. BLACKBIRD_SPATIAL_FILTER_LUMA_1D_VERT,
  110. BLACKBIRD_SPATIAL_FILTER_LUMA_2D_HV, /* separable, default */
  111. BLACKBIRD_SPATIAL_FILTER_LUMA_2D_SYMM /* symmetric non-separable */
  112. };
  113. enum blackbird_spatial_filter_chroma {
  114. BLACKBIRD_SPATIAL_FILTER_CHROMA_DISABLED,
  115. BLACKBIRD_SPATIAL_FILTER_CHROMA_1D_HORIZ /* default */
  116. };
  117. enum blackbird_pulldown {
  118. BLACKBIRD_3_2_PULLDOWN_DISABLED,
  119. BLACKBIRD_3_2_PULLDOWN_ENABLED
  120. };
  121. enum blackbird_vbi_line_bits {
  122. BLACKBIRD_VBI_LINE_BITS_TOP_FIELD,
  123. BLACKBIRD_VBI_LINE_BITS_BOT_FIELD = (1 << 31),
  124. BLACKBIRD_VBI_LINE_BITS_ALL_LINES = 0xFFFFFFFF
  125. };
  126. enum blackbird_vbi_line {
  127. BLACKBIRD_VBI_LINE_DISABLED,
  128. BLACKBIRD_VBI_LINE_ENABLED
  129. };
  130. enum blackbird_vbi_slicing {
  131. BLACKBIRD_VBI_SLICING_NONE,
  132. BLACKBIRD_VBI_SLICING_CLOSED_CAPTION
  133. };
  134. enum blackbird_stream_type {
  135. BLACKBIRD_STREAM_PROGRAM,
  136. BLACKBIRD_STREAM_TRANSPORT,
  137. BLACKBIRD_STREAM_MPEG1,
  138. BLACKBIRD_STREAM_PES_AV,
  139. BLACKBIRD_STREAM_UNKNOWN4,
  140. BLACKBIRD_STREAM_PES_VIDEO,
  141. BLACKBIRD_STREAM_UNKNOWN6,
  142. BLACKBIRD_STREAM_PES_AUDIO,
  143. BLACKBIRD_STREAM_UNKNOWN8,
  144. BLACKBIRD_STREAM_UNKNOWN9, /* audio/pcm ? */
  145. BLACKBIRD_STREAM_DVD,
  146. BLACKBIRD_STREAM_VCD,
  147. BLACKBIRD_STREAM_UNKNOWN12 /* svcd/xvcd ? */
  148. };
  149. enum blackbird_stream_port {
  150. BLACKBIRD_OUTPUT_PORT_MEMORY,
  151. BLACKBIRD_OUTPUT_PORT_STREAMING,
  152. BLACKBIRD_OUTPUT_PORT_SERIAL
  153. };
  154. enum blackbird_audio_bits_sample_rate {
  155. BLACKBIRD_AUDIO_BITS_44100HZ,
  156. BLACKBIRD_AUDIO_BITS_48000HZ,
  157. BLACKBIRD_AUDIO_BITS_32000HZ,
  158. BLACKBIRD_AUDIO_BITS_RESERVED_HZ,
  159. };
  160. enum blackbird_audio_bits_encoding {
  161. BLACKBIRD_AUDIO_BITS_LAYER_1 = 0x1 << 2,
  162. BLACKBIRD_AUDIO_BITS_LAYER_2 = 0x2 << 2,
  163. };
  164. enum blackbird_audio_bits_bitrate_layer_1 {
  165. BLACKBIRD_AUDIO_BITS_LAYER_1_FREE_FORMAT,
  166. BLACKBIRD_AUDIO_BITS_LAYER_1_32 = 0x01 << 4,
  167. BLACKBIRD_AUDIO_BITS_LAYER_1_64 = 0x02 << 4,
  168. BLACKBIRD_AUDIO_BITS_LAYER_1_96 = 0x03 << 4,
  169. BLACKBIRD_AUDIO_BITS_LAYER_1_128 = 0x04 << 4,
  170. BLACKBIRD_AUDIO_BITS_LAYER_1_160 = 0x05 << 4,
  171. BLACKBIRD_AUDIO_BITS_LAYER_1_192 = 0x06 << 4,
  172. BLACKBIRD_AUDIO_BITS_LAYER_1_224 = 0x07 << 4,
  173. BLACKBIRD_AUDIO_BITS_LAYER_1_256 = 0x08 << 4,
  174. BLACKBIRD_AUDIO_BITS_LAYER_1_288 = 0x09 << 4,
  175. BLACKBIRD_AUDIO_BITS_LAYER_1_320 = 0x0A << 4,
  176. BLACKBIRD_AUDIO_BITS_LAYER_1_352 = 0x0B << 4,
  177. BLACKBIRD_AUDIO_BITS_LAYER_1_384 = 0x0C << 4,
  178. BLACKBIRD_AUDIO_BITS_LAYER_1_416 = 0x0D << 4,
  179. BLACKBIRD_AUDIO_BITS_LAYER_1_448 = 0x0E << 4,
  180. };
  181. enum blackbird_audio_bits_bitrate_layer_2 {
  182. BLACKBIRD_AUDIO_BITS_LAYER_2_FREE_FORMAT,
  183. BLACKBIRD_AUDIO_BITS_LAYER_2_32 = 0x01 << 4,
  184. BLACKBIRD_AUDIO_BITS_LAYER_2_48 = 0x02 << 4,
  185. BLACKBIRD_AUDIO_BITS_LAYER_2_56 = 0x03 << 4,
  186. BLACKBIRD_AUDIO_BITS_LAYER_2_64 = 0x04 << 4,
  187. BLACKBIRD_AUDIO_BITS_LAYER_2_80 = 0x05 << 4,
  188. BLACKBIRD_AUDIO_BITS_LAYER_2_96 = 0x06 << 4,
  189. BLACKBIRD_AUDIO_BITS_LAYER_2_112 = 0x07 << 4,
  190. BLACKBIRD_AUDIO_BITS_LAYER_2_128 = 0x08 << 4,
  191. BLACKBIRD_AUDIO_BITS_LAYER_2_160 = 0x09 << 4,
  192. BLACKBIRD_AUDIO_BITS_LAYER_2_192 = 0x0A << 4,
  193. BLACKBIRD_AUDIO_BITS_LAYER_2_224 = 0x0B << 4,
  194. BLACKBIRD_AUDIO_BITS_LAYER_2_256 = 0x0C << 4,
  195. BLACKBIRD_AUDIO_BITS_LAYER_2_320 = 0x0D << 4,
  196. BLACKBIRD_AUDIO_BITS_LAYER_2_384 = 0x0E << 4,
  197. };
  198. enum blackbird_audio_bits_mode {
  199. BLACKBIRD_AUDIO_BITS_STEREO,
  200. BLACKBIRD_AUDIO_BITS_JOINT_STEREO = 0x1 << 8,
  201. BLACKBIRD_AUDIO_BITS_DUAL = 0x2 << 8,
  202. BLACKBIRD_AUDIO_BITS_MONO = 0x3 << 8,
  203. };
  204. enum blackbird_audio_bits_mode_extension {
  205. BLACKBIRD_AUDIO_BITS_BOUND_4,
  206. BLACKBIRD_AUDIO_BITS_BOUND_8 = 0x1 << 10,
  207. BLACKBIRD_AUDIO_BITS_BOUND_12 = 0x2 << 10,
  208. BLACKBIRD_AUDIO_BITS_BOUND_16 = 0x3 << 10,
  209. };
  210. enum blackbird_audio_bits_emphasis {
  211. BLACKBIRD_AUDIO_BITS_EMPHASIS_NONE,
  212. BLACKBIRD_AUDIO_BITS_EMPHASIS_50_15 = 0x1 << 12,
  213. BLACKBIRD_AUDIO_BITS_EMPHASIS_RESERVED = 0x2 << 12,
  214. BLACKBIRD_AUDIO_BITS_EMPHASIS_CCITT_J17 = 0x3 << 12,
  215. };
  216. enum blackbird_audio_bits_crc {
  217. BLACKBIRD_AUDIO_BITS_CRC_OFF,
  218. BLACKBIRD_AUDIO_BITS_CRC_ON = 0x1 << 14,
  219. };
  220. enum blackbird_audio_bits_copyright {
  221. BLACKBIRD_AUDIO_BITS_COPYRIGHT_OFF,
  222. BLACKBIRD_AUDIO_BITS_COPYRIGHT_ON = 0x1 << 15,
  223. };
  224. enum blackbird_audio_bits_original {
  225. BLACKBIRD_AUDIO_BITS_COPY,
  226. BLACKBIRD_AUDIO_BITS_ORIGINAL = 0x1 << 16,
  227. };
  228. enum blackbird_gop_closure {
  229. BLACKBIRD_GOP_CLOSURE_OFF,
  230. BLACKBIRD_GOP_CLOSURE_ON,
  231. };
  232. enum blackbird_data_xfer_status {
  233. BLACKBIRD_MORE_BUFFERS_FOLLOW,
  234. BLACKBIRD_LAST_BUFFER,
  235. };
  236. enum blackbird_picture_mask {
  237. BLACKBIRD_PICTURE_MASK_NONE,
  238. BLACKBIRD_PICTURE_MASK_I_FRAMES,
  239. BLACKBIRD_PICTURE_MASK_I_P_FRAMES = 0x3,
  240. BLACKBIRD_PICTURE_MASK_ALL_FRAMES = 0x7,
  241. };
  242. enum blackbird_vbi_mode_bits {
  243. BLACKBIRD_VBI_BITS_SLICED,
  244. BLACKBIRD_VBI_BITS_RAW,
  245. };
  246. enum blackbird_vbi_insertion_bits {
  247. BLACKBIRD_VBI_BITS_INSERT_IN_XTENSION_USR_DATA,
  248. BLACKBIRD_VBI_BITS_INSERT_IN_PRIVATE_PACKETS = 0x1 << 1,
  249. BLACKBIRD_VBI_BITS_SEPARATE_STREAM = 0x2 << 1,
  250. BLACKBIRD_VBI_BITS_SEPARATE_STREAM_USR_DATA = 0x4 << 1,
  251. BLACKBIRD_VBI_BITS_SEPARATE_STREAM_PRV_DATA = 0x5 << 1,
  252. };
  253. enum blackbird_dma_unit {
  254. BLACKBIRD_DMA_BYTES,
  255. BLACKBIRD_DMA_FRAMES,
  256. };
  257. enum blackbird_dma_transfer_status_bits {
  258. BLACKBIRD_DMA_TRANSFER_BITS_DONE = 0x01,
  259. BLACKBIRD_DMA_TRANSFER_BITS_ERROR = 0x04,
  260. BLACKBIRD_DMA_TRANSFER_BITS_LL_ERROR = 0x10,
  261. };
  262. enum blackbird_pause {
  263. BLACKBIRD_PAUSE_ENCODING,
  264. BLACKBIRD_RESUME_ENCODING,
  265. };
  266. enum blackbird_copyright {
  267. BLACKBIRD_COPYRIGHT_OFF,
  268. BLACKBIRD_COPYRIGHT_ON,
  269. };
  270. enum blackbird_notification_type {
  271. BLACKBIRD_NOTIFICATION_REFRESH,
  272. };
  273. enum blackbird_notification_status {
  274. BLACKBIRD_NOTIFICATION_OFF,
  275. BLACKBIRD_NOTIFICATION_ON,
  276. };
  277. enum blackbird_notification_mailbox {
  278. BLACKBIRD_NOTIFICATION_NO_MAILBOX = -1,
  279. };
  280. enum blackbird_field1_lines {
  281. BLACKBIRD_FIELD1_SAA7114 = 0x00EF, /* 239 */
  282. BLACKBIRD_FIELD1_SAA7115 = 0x00F0, /* 240 */
  283. BLACKBIRD_FIELD1_MICRONAS = 0x0105, /* 261 */
  284. };
  285. enum blackbird_field2_lines {
  286. BLACKBIRD_FIELD2_SAA7114 = 0x00EF, /* 239 */
  287. BLACKBIRD_FIELD2_SAA7115 = 0x00F0, /* 240 */
  288. BLACKBIRD_FIELD2_MICRONAS = 0x0106, /* 262 */
  289. };
  290. enum blackbird_custom_data_type {
  291. BLACKBIRD_CUSTOM_EXTENSION_USR_DATA,
  292. BLACKBIRD_CUSTOM_PRIVATE_PACKET,
  293. };
  294. enum blackbird_mute {
  295. BLACKBIRD_UNMUTE,
  296. BLACKBIRD_MUTE,
  297. };
  298. enum blackbird_mute_video_mask {
  299. BLACKBIRD_MUTE_VIDEO_V_MASK = 0x0000FF00,
  300. BLACKBIRD_MUTE_VIDEO_U_MASK = 0x00FF0000,
  301. BLACKBIRD_MUTE_VIDEO_Y_MASK = 0xFF000000,
  302. };
  303. enum blackbird_mute_video_shift {
  304. BLACKBIRD_MUTE_VIDEO_V_SHIFT = 8,
  305. BLACKBIRD_MUTE_VIDEO_U_SHIFT = 16,
  306. BLACKBIRD_MUTE_VIDEO_Y_SHIFT = 24,
  307. };
  308. /* Registers */
  309. #define IVTV_REG_ENC_SDRAM_REFRESH (0x07F8 /*| IVTV_REG_OFFSET*/)
  310. #define IVTV_REG_ENC_SDRAM_PRECHARGE (0x07FC /*| IVTV_REG_OFFSET*/)
  311. #define IVTV_REG_SPU (0x9050 /*| IVTV_REG_OFFSET*/)
  312. #define IVTV_REG_HW_BLOCKS (0x9054 /*| IVTV_REG_OFFSET*/)
  313. #define IVTV_REG_VPU (0x9058 /*| IVTV_REG_OFFSET*/)
  314. #define IVTV_REG_APU (0xA064 /*| IVTV_REG_OFFSET*/)
  315. /* ------------------------------------------------------------------ */
  316. static void host_setup(struct cx88_core *core)
  317. {
  318. /* toggle reset of the host */
  319. cx_write(MO_GPHST_SOFT_RST, 1);
  320. udelay(100);
  321. cx_write(MO_GPHST_SOFT_RST, 0);
  322. udelay(100);
  323. /* host port setup */
  324. cx_write(MO_GPHST_WSC, 0x44444444U);
  325. cx_write(MO_GPHST_XFR, 0);
  326. cx_write(MO_GPHST_WDTH, 15);
  327. cx_write(MO_GPHST_HDSHK, 0);
  328. cx_write(MO_GPHST_MUX16, 0x44448888U);
  329. cx_write(MO_GPHST_MODE, 0);
  330. }
  331. /* ------------------------------------------------------------------ */
  332. #define P1_MDATA0 0x390000
  333. #define P1_MDATA1 0x390001
  334. #define P1_MDATA2 0x390002
  335. #define P1_MDATA3 0x390003
  336. #define P1_MADDR2 0x390004
  337. #define P1_MADDR1 0x390005
  338. #define P1_MADDR0 0x390006
  339. #define P1_RDATA0 0x390008
  340. #define P1_RDATA1 0x390009
  341. #define P1_RDATA2 0x39000A
  342. #define P1_RDATA3 0x39000B
  343. #define P1_RADDR0 0x39000C
  344. #define P1_RADDR1 0x39000D
  345. #define P1_RRDWR 0x39000E
  346. static int wait_ready_gpio0_bit1(struct cx88_core *core, u32 state)
  347. {
  348. unsigned long timeout = jiffies + msecs_to_jiffies(1);
  349. u32 gpio0,need;
  350. need = state ? 2 : 0;
  351. for (;;) {
  352. gpio0 = cx_read(MO_GP0_IO) & 2;
  353. if (need == gpio0)
  354. return 0;
  355. if (time_after(jiffies,timeout))
  356. return -1;
  357. udelay(1);
  358. }
  359. }
  360. static int memory_write(struct cx88_core *core, u32 address, u32 value)
  361. {
  362. /* Warning: address is dword address (4 bytes) */
  363. cx_writeb(P1_MDATA0, (unsigned int)value);
  364. cx_writeb(P1_MDATA1, (unsigned int)(value >> 8));
  365. cx_writeb(P1_MDATA2, (unsigned int)(value >> 16));
  366. cx_writeb(P1_MDATA3, (unsigned int)(value >> 24));
  367. cx_writeb(P1_MADDR2, (unsigned int)(address >> 16) | 0x40);
  368. cx_writeb(P1_MADDR1, (unsigned int)(address >> 8));
  369. cx_writeb(P1_MADDR0, (unsigned int)address);
  370. cx_read(P1_MDATA0);
  371. cx_read(P1_MADDR0);
  372. return wait_ready_gpio0_bit1(core,1);
  373. }
  374. static int memory_read(struct cx88_core *core, u32 address, u32 *value)
  375. {
  376. int retval;
  377. u32 val;
  378. /* Warning: address is dword address (4 bytes) */
  379. cx_writeb(P1_MADDR2, (unsigned int)(address >> 16) & ~0xC0);
  380. cx_writeb(P1_MADDR1, (unsigned int)(address >> 8));
  381. cx_writeb(P1_MADDR0, (unsigned int)address);
  382. cx_read(P1_MADDR0);
  383. retval = wait_ready_gpio0_bit1(core,1);
  384. cx_writeb(P1_MDATA3, 0);
  385. val = (unsigned char)cx_read(P1_MDATA3) << 24;
  386. cx_writeb(P1_MDATA2, 0);
  387. val |= (unsigned char)cx_read(P1_MDATA2) << 16;
  388. cx_writeb(P1_MDATA1, 0);
  389. val |= (unsigned char)cx_read(P1_MDATA1) << 8;
  390. cx_writeb(P1_MDATA0, 0);
  391. val |= (unsigned char)cx_read(P1_MDATA0);
  392. *value = val;
  393. return retval;
  394. }
  395. static int register_write(struct cx88_core *core, u32 address, u32 value)
  396. {
  397. cx_writeb(P1_RDATA0, (unsigned int)value);
  398. cx_writeb(P1_RDATA1, (unsigned int)(value >> 8));
  399. cx_writeb(P1_RDATA2, (unsigned int)(value >> 16));
  400. cx_writeb(P1_RDATA3, (unsigned int)(value >> 24));
  401. cx_writeb(P1_RADDR0, (unsigned int)address);
  402. cx_writeb(P1_RADDR1, (unsigned int)(address >> 8));
  403. cx_writeb(P1_RRDWR, 1);
  404. cx_read(P1_RDATA0);
  405. cx_read(P1_RADDR0);
  406. return wait_ready_gpio0_bit1(core,1);
  407. }
  408. static int register_read(struct cx88_core *core, u32 address, u32 *value)
  409. {
  410. int retval;
  411. u32 val;
  412. cx_writeb(P1_RADDR0, (unsigned int)address);
  413. cx_writeb(P1_RADDR1, (unsigned int)(address >> 8));
  414. cx_writeb(P1_RRDWR, 0);
  415. cx_read(P1_RADDR0);
  416. retval = wait_ready_gpio0_bit1(core,1);
  417. val = (unsigned char)cx_read(P1_RDATA0);
  418. val |= (unsigned char)cx_read(P1_RDATA1) << 8;
  419. val |= (unsigned char)cx_read(P1_RDATA2) << 16;
  420. val |= (unsigned char)cx_read(P1_RDATA3) << 24;
  421. *value = val;
  422. return retval;
  423. }
  424. /* ------------------------------------------------------------------ */
  425. /* We don't need to call the API often, so using just one mailbox will probably suffice */
  426. static int blackbird_api_cmd(struct cx8802_dev *dev, u32 command,
  427. u32 inputcnt, u32 outputcnt, ...)
  428. {
  429. unsigned long timeout;
  430. u32 value, flag, retval;
  431. int i;
  432. va_list args;
  433. va_start(args, outputcnt);
  434. dprintk(1,"%s: 0x%X\n", __FUNCTION__, command);
  435. /* this may not be 100% safe if we can't read any memory location
  436. without side effects */
  437. memory_read(dev->core, dev->mailbox - 4, &value);
  438. if (value != 0x12345678) {
  439. dprintk(0, "Firmware and/or mailbox pointer not initialized or corrupted\n");
  440. return -1;
  441. }
  442. memory_read(dev->core, dev->mailbox, &flag);
  443. if (flag) {
  444. dprintk(0, "ERROR: Mailbox appears to be in use (%x)\n", flag);
  445. return -1;
  446. }
  447. flag |= 1; /* tell 'em we're working on it */
  448. memory_write(dev->core, dev->mailbox, flag);
  449. /* write command + args + fill remaining with zeros */
  450. memory_write(dev->core, dev->mailbox + 1, command); /* command code */
  451. memory_write(dev->core, dev->mailbox + 3, IVTV_API_STD_TIMEOUT); /* timeout */
  452. for (i = 0; i < inputcnt ; i++) {
  453. value = va_arg(args, int);
  454. memory_write(dev->core, dev->mailbox + 4 + i, value);
  455. dprintk(1, "API Input %d = %d\n", i, value);
  456. }
  457. for (; i < 16 ; i++)
  458. memory_write(dev->core, dev->mailbox + 4 + i, 0);
  459. flag |= 3; /* tell 'em we're done writing */
  460. memory_write(dev->core, dev->mailbox, flag);
  461. /* wait for firmware to handle the API command */
  462. timeout = jiffies + msecs_to_jiffies(10);
  463. for (;;) {
  464. memory_read(dev->core, dev->mailbox, &flag);
  465. if (0 != (flag & 4))
  466. break;
  467. if (time_after(jiffies,timeout)) {
  468. dprintk(0, "ERROR: API Mailbox timeout\n");
  469. return -1;
  470. }
  471. udelay(10);
  472. }
  473. /* read output values */
  474. for (i = 0; i < outputcnt ; i++) {
  475. int *vptr = va_arg(args, int *);
  476. memory_read(dev->core, dev->mailbox + 4 + i, vptr);
  477. dprintk(1, "API Output %d = %d\n", i, *vptr);
  478. }
  479. va_end(args);
  480. memory_read(dev->core, dev->mailbox + 2, &retval);
  481. dprintk(1, "API result = %d\n",retval);
  482. flag = 0;
  483. memory_write(dev->core, dev->mailbox, flag);
  484. return retval;
  485. }
  486. static int blackbird_find_mailbox(struct cx8802_dev *dev)
  487. {
  488. u32 signature[4]={0x12345678, 0x34567812, 0x56781234, 0x78123456};
  489. int signaturecnt=0;
  490. u32 value;
  491. int i;
  492. for (i = 0; i < BLACKBIRD_FIRM_IMAGE_SIZE; i++) {
  493. memory_read(dev->core, i, &value);
  494. if (value == signature[signaturecnt])
  495. signaturecnt++;
  496. else
  497. signaturecnt = 0;
  498. if (4 == signaturecnt) {
  499. dprintk(1, "Mailbox signature found\n");
  500. return i+1;
  501. }
  502. }
  503. dprintk(0, "Mailbox signature values not found!\n");
  504. return -1;
  505. }
  506. static int blackbird_load_firmware(struct cx8802_dev *dev)
  507. {
  508. static const unsigned char magic[8] = {
  509. 0xa7, 0x0d, 0x00, 0x00, 0x66, 0xbb, 0x55, 0xaa
  510. };
  511. const struct firmware *firmware;
  512. int i, retval = 0;
  513. u32 value = 0;
  514. u32 checksum = 0;
  515. u32 *dataptr;
  516. retval = register_write(dev->core, IVTV_REG_VPU, 0xFFFFFFED);
  517. retval |= register_write(dev->core, IVTV_REG_HW_BLOCKS, IVTV_CMD_HW_BLOCKS_RST);
  518. retval |= register_write(dev->core, IVTV_REG_ENC_SDRAM_REFRESH, 0x80000640);
  519. retval |= register_write(dev->core, IVTV_REG_ENC_SDRAM_PRECHARGE, 0x1A);
  520. msleep(1);
  521. retval |= register_write(dev->core, IVTV_REG_APU, 0);
  522. if (retval < 0)
  523. dprintk(0, "Error with register_write\n");
  524. retval = request_firmware(&firmware, BLACKBIRD_FIRM_ENC_FILENAME,
  525. &dev->pci->dev);
  526. if (retval != 0) {
  527. dprintk(0, "ERROR: Hotplug firmware request failed (%s).\n",
  528. BLACKBIRD_FIRM_ENC_FILENAME);
  529. dprintk(0, "Please fix your hotplug setup, the board will "
  530. "not work without firmware loaded!\n");
  531. return -1;
  532. }
  533. if (firmware->size != BLACKBIRD_FIRM_IMAGE_SIZE) {
  534. dprintk(0, "ERROR: Firmware size mismatch (have %zd, expected %d)\n",
  535. firmware->size, BLACKBIRD_FIRM_IMAGE_SIZE);
  536. return -1;
  537. }
  538. if (0 != memcmp(firmware->data, magic, 8)) {
  539. dprintk(0, "ERROR: Firmware magic mismatch, wrong file?\n");
  540. return -1;
  541. }
  542. /* transfer to the chip */
  543. dprintk(1,"Loading firmware ...\n");
  544. dataptr = (u32*)firmware->data;
  545. for (i = 0; i < (firmware->size >> 2); i++) {
  546. value = *dataptr;
  547. checksum += ~value;
  548. memory_write(dev->core, i, value);
  549. dataptr++;
  550. }
  551. /* read back to verify with the checksum */
  552. for (i--; i >= 0; i--) {
  553. memory_read(dev->core, i, &value);
  554. checksum -= ~value;
  555. }
  556. if (checksum) {
  557. dprintk(0, "ERROR: Firmware load failed (checksum mismatch).\n");
  558. return -1;
  559. }
  560. release_firmware(firmware);
  561. dprintk(0, "Firmware upload successful.\n");
  562. retval |= register_write(dev->core, IVTV_REG_HW_BLOCKS, IVTV_CMD_HW_BLOCKS_RST);
  563. retval |= register_read(dev->core, IVTV_REG_SPU, &value);
  564. retval |= register_write(dev->core, IVTV_REG_SPU, value & 0xFFFFFFFE);
  565. msleep(1);
  566. retval |= register_read(dev->core, IVTV_REG_VPU, &value);
  567. retval |= register_write(dev->core, IVTV_REG_VPU, value & 0xFFFFFFE8);
  568. if (retval < 0)
  569. dprintk(0, "Error with register_write\n");
  570. return 0;
  571. }
  572. /**
  573. Settings used by the windows tv app for PVR2000:
  574. =================================================================================================================
  575. Profile | Codec | Resolution | CBR/VBR | Video Qlty | V. Bitrate | Frmrate | Audio Codec | A. Bitrate | A. Mode
  576. -----------------------------------------------------------------------------------------------------------------
  577. MPEG-1 | MPEG1 | 352x288PAL | (CBR) | 1000:Optimal | 2000 Kbps | 25fps | MPG1 Layer2 | 224kbps | Stereo
  578. MPEG-2 | MPEG2 | 720x576PAL | VBR | 600 :Good | 4000 Kbps | 25fps | MPG1 Layer2 | 224kbps | Stereo
  579. VCD | MPEG1 | 352x288PAL | (CBR) | 1000:Optimal | 1150 Kbps | 25fps | MPG1 Layer2 | 224kbps | Stereo
  580. DVD | MPEG2 | 720x576PAL | VBR | 600 :Good | 6000 Kbps | 25fps | MPG1 Layer2 | 224kbps | Stereo
  581. DB* DVD | MPEG2 | 720x576PAL | CBR | 600 :Good | 6000 Kbps | 25fps | MPG1 Layer2 | 224kbps | Stereo
  582. =================================================================================================================
  583. *DB: "DirectBurn"
  584. */
  585. static struct blackbird_dnr default_dnr_params = {
  586. .mode = BLACKBIRD_DNR_BITS_MANUAL,
  587. .type = BLACKBIRD_MEDIAN_FILTER_DISABLED,
  588. .spatial = 0,
  589. .temporal = 0
  590. };
  591. static struct v4l2_mpeg_compression default_mpeg_params = {
  592. .st_type = V4L2_MPEG_PS_2,
  593. .st_bitrate = {
  594. .mode = V4L2_BITRATE_CBR,
  595. .min = 0,
  596. .target = 0,
  597. .max = 0
  598. },
  599. .ts_pid_pmt = 16,
  600. .ts_pid_audio = 260,
  601. .ts_pid_video = 256,
  602. .ts_pid_pcr = 259,
  603. .ps_size = 0,
  604. .au_type = V4L2_MPEG_AU_2_II,
  605. .au_bitrate = {
  606. .mode = V4L2_BITRATE_CBR,
  607. .min = 224,
  608. .target = 224,
  609. .max = 224
  610. },
  611. .au_sample_rate = 44100,
  612. .au_pesid = 0,
  613. .vi_type = V4L2_MPEG_VI_2,
  614. .vi_aspect_ratio = V4L2_MPEG_ASPECT_4_3,
  615. .vi_bitrate = {
  616. .mode = V4L2_BITRATE_CBR,
  617. .min = 4000,
  618. .target = 4500,
  619. .max = 6000
  620. },
  621. .vi_frame_rate = 25,
  622. .vi_frames_per_gop = 15,
  623. .vi_bframes_count = 2,
  624. .vi_pesid = 0,
  625. .closed_gops = 0,
  626. .pulldown = 0
  627. };
  628. static enum blackbird_stream_type mpeg_stream_types[] = {
  629. [V4L2_MPEG_SS_1] = BLACKBIRD_STREAM_MPEG1,
  630. [V4L2_MPEG_PS_2] = BLACKBIRD_STREAM_PROGRAM,
  631. [V4L2_MPEG_TS_2] = BLACKBIRD_STREAM_TRANSPORT,
  632. [V4L2_MPEG_PS_DVD] = BLACKBIRD_STREAM_DVD,
  633. };
  634. static enum blackbird_aspect_ratio mpeg_stream_ratios[] = {
  635. [V4L2_MPEG_ASPECT_SQUARE] = BLACKBIRD_ASPECT_RATIO_1_1_SQUARE,
  636. [V4L2_MPEG_ASPECT_4_3] = BLACKBIRD_ASPECT_RATIO_4_3,
  637. [V4L2_MPEG_ASPECT_16_9] = BLACKBIRD_ASPECT_RATIO_16_9,
  638. [V4L2_MPEG_ASPECT_1_221] = BLACKBIRD_ASPECT_RATIO_221_100,
  639. };
  640. static enum blackbird_video_bitrate_type mpeg_video_bitrates[] = {
  641. [V4L2_BITRATE_NONE] = BLACKBIRD_VIDEO_CBR,
  642. [V4L2_BITRATE_CBR] = BLACKBIRD_VIDEO_CBR,
  643. [V4L2_BITRATE_VBR] = BLACKBIRD_VIDEO_VBR,
  644. };
  645. /* find the best layer I/II bitrate to fit a given numeric value */
  646. struct bitrate_bits {
  647. u32 bits; /* layer bits for the best fit */
  648. u32 rate; /* actual numeric value for the layer best fit */
  649. };
  650. struct bitrate_approximation {
  651. u32 target; /* numeric value of the rate we want */
  652. struct bitrate_bits layer[2];
  653. };
  654. static struct bitrate_approximation mpeg_audio_bitrates[] = {
  655. /* target layer[0].bits layer[0].rate layer[1].bits layer[1].rate */
  656. { 0, { { 0, 0, }, { 0, 0, }, }, },
  657. { 32, { { BLACKBIRD_AUDIO_BITS_LAYER_1_32 , 32, }, { BLACKBIRD_AUDIO_BITS_LAYER_2_32 , 32, }, }, },
  658. { 48, { { BLACKBIRD_AUDIO_BITS_LAYER_1_64 , 64, }, { BLACKBIRD_AUDIO_BITS_LAYER_2_48 , 48, }, }, },
  659. { 56, { { BLACKBIRD_AUDIO_BITS_LAYER_1_64 , 64, }, { BLACKBIRD_AUDIO_BITS_LAYER_2_56 , 56, }, }, },
  660. { 64, { { BLACKBIRD_AUDIO_BITS_LAYER_1_64 , 64, }, { BLACKBIRD_AUDIO_BITS_LAYER_2_64 , 64, }, }, },
  661. { 80, { { BLACKBIRD_AUDIO_BITS_LAYER_1_96 , 96, }, { BLACKBIRD_AUDIO_BITS_LAYER_2_80 , 80, }, }, },
  662. { 96, { { BLACKBIRD_AUDIO_BITS_LAYER_1_96 , 96, }, { BLACKBIRD_AUDIO_BITS_LAYER_2_96 , 96, }, }, },
  663. { 112, { { BLACKBIRD_AUDIO_BITS_LAYER_1_128, 128, }, { BLACKBIRD_AUDIO_BITS_LAYER_2_112, 112, }, }, },
  664. { 128, { { BLACKBIRD_AUDIO_BITS_LAYER_1_128, 128, }, { BLACKBIRD_AUDIO_BITS_LAYER_2_128, 128, }, }, },
  665. { 160, { { BLACKBIRD_AUDIO_BITS_LAYER_1_160, 160, }, { BLACKBIRD_AUDIO_BITS_LAYER_2_160, 160, }, }, },
  666. { 192, { { BLACKBIRD_AUDIO_BITS_LAYER_1_192, 192, }, { BLACKBIRD_AUDIO_BITS_LAYER_2_192, 192, }, }, },
  667. { 224, { { BLACKBIRD_AUDIO_BITS_LAYER_1_224, 224, }, { BLACKBIRD_AUDIO_BITS_LAYER_2_224, 224, }, }, },
  668. { 256, { { BLACKBIRD_AUDIO_BITS_LAYER_1_256, 256, }, { BLACKBIRD_AUDIO_BITS_LAYER_2_256, 256, }, }, },
  669. { 288, { { BLACKBIRD_AUDIO_BITS_LAYER_1_288, 288, }, { BLACKBIRD_AUDIO_BITS_LAYER_2_320, 320, }, }, },
  670. { 320, { { BLACKBIRD_AUDIO_BITS_LAYER_1_320, 320, }, { BLACKBIRD_AUDIO_BITS_LAYER_2_320, 320, }, }, },
  671. { 352, { { BLACKBIRD_AUDIO_BITS_LAYER_1_352, 352, }, { BLACKBIRD_AUDIO_BITS_LAYER_2_384, 384, }, }, },
  672. { 384, { { BLACKBIRD_AUDIO_BITS_LAYER_1_384, 384, }, { BLACKBIRD_AUDIO_BITS_LAYER_2_384, 384, }, }, },
  673. { 416, { { BLACKBIRD_AUDIO_BITS_LAYER_1_416, 416, }, { BLACKBIRD_AUDIO_BITS_LAYER_2_384, 384, }, }, },
  674. { 448, { { BLACKBIRD_AUDIO_BITS_LAYER_1_448, 448, }, { BLACKBIRD_AUDIO_BITS_LAYER_2_384, 384, }, }, },
  675. };
  676. static const int BITRATES_SIZE = ARRAY_SIZE(mpeg_audio_bitrates);
  677. static void blackbird_set_default_params(struct cx8802_dev *dev)
  678. {
  679. struct v4l2_mpeg_compression *params = &dev->params;
  680. u32 au_params;
  681. /* assign stream type */
  682. if( params->st_type >= ARRAY_SIZE(mpeg_stream_types) )
  683. params->st_type = V4L2_MPEG_PS_2;
  684. if( params->st_type == V4L2_MPEG_SS_1 )
  685. params->vi_type = V4L2_MPEG_VI_1;
  686. else
  687. params->vi_type = V4L2_MPEG_VI_2;
  688. blackbird_api_cmd(dev, CX2341X_ENC_SET_STREAM_TYPE, 1, 0, mpeg_stream_types[params->st_type]);
  689. /* assign framerate */
  690. if( params->vi_frame_rate <= 25 )
  691. {
  692. params->vi_frame_rate = 25;
  693. blackbird_api_cmd(dev, CX2341X_ENC_SET_FRAME_RATE, 1, 0, BLACKBIRD_FRAMERATE_PAL_25);
  694. }
  695. else
  696. {
  697. params->vi_frame_rate = 30;
  698. blackbird_api_cmd(dev, CX2341X_ENC_SET_FRAME_RATE, 1, 0, BLACKBIRD_FRAMERATE_NTSC_30);
  699. }
  700. /* assign aspect ratio */
  701. if( params->vi_aspect_ratio >= ARRAY_SIZE(mpeg_stream_ratios) )
  702. params->vi_aspect_ratio = V4L2_MPEG_ASPECT_4_3;
  703. blackbird_api_cmd(dev, CX2341X_ENC_SET_ASPECT_RATIO, 1, 0, mpeg_stream_ratios[params->vi_aspect_ratio]);
  704. /* assign gop properties */
  705. blackbird_api_cmd(dev, CX2341X_ENC_SET_GOP_PROPERTIES, 2, 0, params->vi_frames_per_gop, params->vi_bframes_count+1);
  706. /* assign gop closure */
  707. blackbird_api_cmd(dev, CX2341X_ENC_SET_GOP_CLOSURE, 1, 0, params->closed_gops);
  708. /* assign 3 2 pulldown */
  709. blackbird_api_cmd(dev, CX2341X_ENC_SET_3_2_PULLDOWN, 1, 0, params->pulldown);
  710. /* make sure the params are within bounds */
  711. if( params->st_bitrate.mode >= ARRAY_SIZE(mpeg_video_bitrates) )
  712. params->vi_bitrate.mode = V4L2_BITRATE_NONE;
  713. if( params->vi_bitrate.mode >= ARRAY_SIZE(mpeg_video_bitrates) )
  714. params->vi_bitrate.mode = V4L2_BITRATE_NONE;
  715. if( params->au_bitrate.mode >= ARRAY_SIZE(mpeg_video_bitrates) )
  716. params->au_bitrate.mode = V4L2_BITRATE_NONE;
  717. /* assign audio properties */
  718. /* note: it's not necessary to set the samplerate, the mpeg encoder seems to autodetect/adjust */
  719. au_params = BLACKBIRD_AUDIO_BITS_STEREO |
  720. /* BLACKBIRD_AUDIO_BITS_BOUND_4 | */
  721. BLACKBIRD_AUDIO_BITS_EMPHASIS_NONE |
  722. BLACKBIRD_AUDIO_BITS_CRC_OFF |
  723. BLACKBIRD_AUDIO_BITS_COPYRIGHT_OFF |
  724. BLACKBIRD_AUDIO_BITS_COPY |
  725. 0;
  726. if( params->au_sample_rate <= 32000 )
  727. {
  728. params->au_sample_rate = 32000;
  729. au_params |= BLACKBIRD_AUDIO_BITS_32000HZ;
  730. }
  731. else if( params->au_sample_rate <= 44100 )
  732. {
  733. params->au_sample_rate = 44100;
  734. au_params |= BLACKBIRD_AUDIO_BITS_44100HZ;
  735. }
  736. else
  737. {
  738. params->au_sample_rate = 48000;
  739. au_params |= BLACKBIRD_AUDIO_BITS_48000HZ;
  740. }
  741. if( params->au_type == V4L2_MPEG_AU_2_I )
  742. {
  743. au_params |= BLACKBIRD_AUDIO_BITS_LAYER_1;
  744. }
  745. else
  746. {
  747. /* TODO: try to handle the other formats more gracefully */
  748. params->au_type = V4L2_MPEG_AU_2_II;
  749. au_params |= BLACKBIRD_AUDIO_BITS_LAYER_2;
  750. }
  751. if( params->au_bitrate.mode )
  752. {
  753. int layer;
  754. if( params->au_bitrate.mode == V4L2_BITRATE_CBR )
  755. params->au_bitrate.max = params->vi_bitrate.target;
  756. else
  757. params->au_bitrate.target = params->vi_bitrate.max;
  758. layer = params->au_type;
  759. if( params->au_bitrate.target == 0 )
  760. {
  761. /* TODO: use the minimum possible bitrate instead of 0 ? */
  762. au_params |= 0;
  763. }
  764. else if( params->au_bitrate.target >=
  765. mpeg_audio_bitrates[BITRATES_SIZE-1].layer[layer].rate )
  766. {
  767. /* clamp the bitrate to the max supported by the standard */
  768. params->au_bitrate.target = mpeg_audio_bitrates[BITRATES_SIZE-1].layer[layer].rate;
  769. params->au_bitrate.max = params->au_bitrate.target;
  770. au_params |= mpeg_audio_bitrates[BITRATES_SIZE-1].layer[layer].bits;
  771. }
  772. else
  773. {
  774. /* round up to the nearest supported bitrate */
  775. int i;
  776. for(i = 1; i < BITRATES_SIZE; i++)
  777. {
  778. if( params->au_bitrate.target > mpeg_audio_bitrates[i-1].layer[layer].rate &&
  779. params->au_bitrate.target <= mpeg_audio_bitrates[i].layer[layer].rate )
  780. {
  781. params->au_bitrate.target = mpeg_audio_bitrates[i].layer[layer].rate;
  782. params->au_bitrate.max = params->au_bitrate.target;
  783. au_params |= mpeg_audio_bitrates[i].layer[layer].bits;
  784. break;
  785. }
  786. }
  787. }
  788. }
  789. else
  790. {
  791. /* TODO: ??? */
  792. params->au_bitrate.target = params->au_bitrate.max = 0;
  793. au_params |= 0;
  794. }
  795. blackbird_api_cmd(dev, CX2341X_ENC_SET_AUDIO_PROPERTIES, 1, 0, au_params );
  796. /* assign bitrates */
  797. if( params->vi_bitrate.mode )
  798. {
  799. /* bitrate is set, let's figure out the cbr/vbr mess */
  800. if( params->vi_bitrate.max < params->vi_bitrate.target )
  801. {
  802. if( params->vi_bitrate.mode == V4L2_BITRATE_CBR )
  803. params->vi_bitrate.max = params->vi_bitrate.target;
  804. else
  805. params->vi_bitrate.target = params->vi_bitrate.max;
  806. }
  807. }
  808. else
  809. {
  810. if( params->st_bitrate.max < params->st_bitrate.target )
  811. {
  812. if( params->st_bitrate.mode == V4L2_BITRATE_VBR )
  813. params->st_bitrate.target = params->st_bitrate.max;
  814. else
  815. params->st_bitrate.max = params->st_bitrate.target;
  816. }
  817. /* calculate vi_bitrate = st_bitrate - au_bitrate */
  818. params->vi_bitrate.max = params->st_bitrate.max - params->au_bitrate.max;
  819. params->vi_bitrate.target = params->st_bitrate.target - params->au_bitrate.target;
  820. }
  821. blackbird_api_cmd(dev, CX2341X_ENC_SET_BIT_RATE, 4, 0,
  822. mpeg_video_bitrates[params->vi_bitrate.mode],
  823. params->vi_bitrate.target * 1000, /* kbps -> bps */
  824. params->vi_bitrate.max * 1000 / BLACKBIRD_PEAK_RATE_DIVISOR, /* peak/400 */
  825. BLACKBIRD_MUX_RATE_DEFAULT /*, 0x70*/); /* encoding buffer, ckennedy */
  826. /* TODO: implement the stream ID stuff:
  827. ts_pid_pmt, ts_pid_audio, ts_pid_video, ts_pid_pcr,
  828. ps_size, au_pesid, vi_pesid
  829. */
  830. }
  831. #define CHECK_PARAM( name ) ( dev->params.name != params->name )
  832. #define IF_PARAM( name ) if( CHECK_PARAM( name ) )
  833. #define UPDATE_PARAM( name ) dev->params.name = params->name
  834. void blackbird_set_params(struct cx8802_dev *dev, struct v4l2_mpeg_compression *params)
  835. {
  836. u32 au_params;
  837. /* assign stream type */
  838. if( params->st_type >= ARRAY_SIZE(mpeg_stream_types) )
  839. params->st_type = V4L2_MPEG_PS_2;
  840. if( params->st_type == V4L2_MPEG_SS_1 )
  841. params->vi_type = V4L2_MPEG_VI_1;
  842. else
  843. params->vi_type = V4L2_MPEG_VI_2;
  844. if( CHECK_PARAM( st_type ) || CHECK_PARAM( vi_type ) )
  845. {
  846. UPDATE_PARAM( st_type );
  847. UPDATE_PARAM( vi_type );
  848. blackbird_api_cmd(dev, CX2341X_ENC_SET_STREAM_TYPE, 1, 0, mpeg_stream_types[params->st_type]);
  849. }
  850. /* assign framerate */
  851. if( params->vi_frame_rate <= 25 )
  852. params->vi_frame_rate = 25;
  853. else
  854. params->vi_frame_rate = 30;
  855. IF_PARAM( vi_frame_rate )
  856. {
  857. UPDATE_PARAM( vi_frame_rate );
  858. if( params->vi_frame_rate == 25 )
  859. blackbird_api_cmd(dev, CX2341X_ENC_SET_FRAME_RATE, 1, 0, BLACKBIRD_FRAMERATE_PAL_25);
  860. else
  861. blackbird_api_cmd(dev, CX2341X_ENC_SET_FRAME_RATE, 1, 0, BLACKBIRD_FRAMERATE_NTSC_30);
  862. }
  863. /* assign aspect ratio */
  864. if( params->vi_aspect_ratio >= ARRAY_SIZE(mpeg_stream_ratios) )
  865. params->vi_aspect_ratio = V4L2_MPEG_ASPECT_4_3;
  866. IF_PARAM( vi_aspect_ratio )
  867. {
  868. UPDATE_PARAM( vi_aspect_ratio );
  869. blackbird_api_cmd(dev, CX2341X_ENC_SET_ASPECT_RATIO, 1, 0, mpeg_stream_ratios[params->vi_aspect_ratio]);
  870. }
  871. /* assign gop properties */
  872. if( CHECK_PARAM( vi_frames_per_gop ) || CHECK_PARAM( vi_bframes_count ) )
  873. {
  874. UPDATE_PARAM( vi_frames_per_gop );
  875. UPDATE_PARAM( vi_bframes_count );
  876. blackbird_api_cmd(dev, CX2341X_ENC_SET_GOP_PROPERTIES, 2, 0, params->vi_frames_per_gop, params->vi_bframes_count+1);
  877. }
  878. /* assign gop closure */
  879. IF_PARAM( closed_gops )
  880. {
  881. UPDATE_PARAM( closed_gops );
  882. blackbird_api_cmd(dev, CX2341X_ENC_SET_GOP_CLOSURE, 1, 0, params->closed_gops);
  883. }
  884. /* assign 3 2 pulldown */
  885. IF_PARAM( pulldown )
  886. {
  887. UPDATE_PARAM( pulldown );
  888. blackbird_api_cmd(dev, CX2341X_ENC_SET_3_2_PULLDOWN, 1, 0, params->pulldown);
  889. }
  890. /* make sure the params are within bounds */
  891. if( params->st_bitrate.mode >= ARRAY_SIZE(mpeg_video_bitrates) )
  892. params->vi_bitrate.mode = V4L2_BITRATE_NONE;
  893. if( params->vi_bitrate.mode >= ARRAY_SIZE(mpeg_video_bitrates) )
  894. params->vi_bitrate.mode = V4L2_BITRATE_NONE;
  895. if( params->au_bitrate.mode >= ARRAY_SIZE(mpeg_video_bitrates) )
  896. params->au_bitrate.mode = V4L2_BITRATE_NONE;
  897. /* assign audio properties */
  898. /* note: it's not necessary to set the samplerate, the mpeg encoder seems to autodetect/adjust */
  899. au_params = BLACKBIRD_AUDIO_BITS_STEREO |
  900. /* BLACKBIRD_AUDIO_BITS_BOUND_4 | */
  901. BLACKBIRD_AUDIO_BITS_EMPHASIS_NONE |
  902. BLACKBIRD_AUDIO_BITS_CRC_OFF |
  903. BLACKBIRD_AUDIO_BITS_COPYRIGHT_OFF |
  904. BLACKBIRD_AUDIO_BITS_COPY |
  905. 0;
  906. if( params->au_sample_rate < 32000 )
  907. {
  908. params->au_sample_rate = 32000;
  909. au_params |= BLACKBIRD_AUDIO_BITS_32000HZ;
  910. }
  911. else if( params->au_sample_rate < 44100 )
  912. {
  913. params->au_sample_rate = 44100;
  914. au_params |= BLACKBIRD_AUDIO_BITS_44100HZ;
  915. }
  916. else
  917. {
  918. params->au_sample_rate = 48000;
  919. au_params |= BLACKBIRD_AUDIO_BITS_48000HZ;
  920. }
  921. if( params->au_type == V4L2_MPEG_AU_2_I )
  922. {
  923. au_params |= BLACKBIRD_AUDIO_BITS_LAYER_1;
  924. }
  925. else
  926. {
  927. /* TODO: try to handle the other formats more gracefully */
  928. params->au_type = V4L2_MPEG_AU_2_II;
  929. au_params |= BLACKBIRD_AUDIO_BITS_LAYER_2;
  930. }
  931. if( params->au_bitrate.mode )
  932. {
  933. int layer;
  934. if( params->au_bitrate.mode == V4L2_BITRATE_CBR )
  935. params->au_bitrate.max = params->vi_bitrate.target;
  936. else
  937. params->au_bitrate.target = params->vi_bitrate.max;
  938. layer = params->au_type;
  939. if( params->au_bitrate.target == 0 )
  940. {
  941. /* TODO: use the minimum possible bitrate instead of 0 ? */
  942. au_params |= 0;
  943. }
  944. else if( params->au_bitrate.target >=
  945. mpeg_audio_bitrates[BITRATES_SIZE-1].layer[layer].rate )
  946. {
  947. /* clamp the bitrate to the max supported by the standard */
  948. params->au_bitrate.target = mpeg_audio_bitrates[BITRATES_SIZE-1].layer[layer].rate;
  949. params->au_bitrate.max = params->au_bitrate.target;
  950. au_params |= mpeg_audio_bitrates[BITRATES_SIZE-1].layer[layer].bits;
  951. }
  952. else
  953. {
  954. /* round up to the nearest supported bitrate */
  955. int i;
  956. for(i = 1; i < BITRATES_SIZE; i++)
  957. {
  958. if( params->au_bitrate.target > mpeg_audio_bitrates[i-1].layer[layer].rate &&
  959. params->au_bitrate.target <= mpeg_audio_bitrates[i].layer[layer].rate )
  960. {
  961. params->au_bitrate.target = mpeg_audio_bitrates[i].layer[layer].rate;
  962. params->au_bitrate.max = params->au_bitrate.target;
  963. au_params |= mpeg_audio_bitrates[i].layer[layer].bits;
  964. break;
  965. }
  966. }
  967. }
  968. }
  969. else
  970. {
  971. /* TODO: ??? */
  972. params->au_bitrate.target = params->au_bitrate.max = 0;
  973. au_params |= 0;
  974. }
  975. if( CHECK_PARAM( au_type ) || CHECK_PARAM( au_sample_rate )
  976. || CHECK_PARAM( au_bitrate.mode ) || CHECK_PARAM( au_bitrate.max )
  977. || CHECK_PARAM( au_bitrate.target )
  978. )
  979. {
  980. UPDATE_PARAM( au_type );
  981. UPDATE_PARAM( au_sample_rate );
  982. UPDATE_PARAM( au_bitrate );
  983. blackbird_api_cmd(dev, CX2341X_ENC_SET_AUDIO_PROPERTIES, 1, 0, au_params );
  984. }
  985. /* assign bitrates */
  986. if( params->vi_bitrate.mode )
  987. {
  988. /* bitrate is set, let's figure out the cbr/vbr mess */
  989. if( params->vi_bitrate.max < params->vi_bitrate.target )
  990. {
  991. if( params->vi_bitrate.mode == V4L2_BITRATE_CBR )
  992. params->vi_bitrate.max = params->vi_bitrate.target;
  993. else
  994. params->vi_bitrate.target = params->vi_bitrate.max;
  995. }
  996. }
  997. else
  998. {
  999. if( params->st_bitrate.max < params->st_bitrate.target )
  1000. {
  1001. if( params->st_bitrate.mode == V4L2_BITRATE_VBR )
  1002. params->st_bitrate.target = params->st_bitrate.max;
  1003. else
  1004. params->st_bitrate.max = params->st_bitrate.target;
  1005. }
  1006. /* calculate vi_bitrate = st_bitrate - au_bitrate */
  1007. params->vi_bitrate.max = params->st_bitrate.max - params->au_bitrate.max;
  1008. params->vi_bitrate.target = params->st_bitrate.target - params->au_bitrate.target;
  1009. }
  1010. UPDATE_PARAM( st_bitrate );
  1011. if( CHECK_PARAM( vi_bitrate.mode ) || CHECK_PARAM( vi_bitrate.max )
  1012. || CHECK_PARAM( vi_bitrate.target )
  1013. )
  1014. {
  1015. UPDATE_PARAM( vi_bitrate );
  1016. blackbird_api_cmd(dev, CX2341X_ENC_SET_BIT_RATE, 4, 0,
  1017. mpeg_video_bitrates[params->vi_bitrate.mode],
  1018. params->vi_bitrate.target * 1000, /* kbps -> bps */
  1019. params->vi_bitrate.max * 1000 / BLACKBIRD_PEAK_RATE_DIVISOR, /* peak/400 */
  1020. BLACKBIRD_MUX_RATE_DEFAULT /*, 0x70*/); /* encoding buffer, ckennedy */
  1021. }
  1022. /* TODO: implement the stream ID stuff:
  1023. ts_pid_pmt, ts_pid_audio, ts_pid_video, ts_pid_pcr,
  1024. ps_size, au_pesid, vi_pesid
  1025. */
  1026. UPDATE_PARAM( ts_pid_pmt );
  1027. UPDATE_PARAM( ts_pid_audio );
  1028. UPDATE_PARAM( ts_pid_video );
  1029. UPDATE_PARAM( ts_pid_pcr );
  1030. UPDATE_PARAM( ps_size );
  1031. UPDATE_PARAM( au_pesid );
  1032. UPDATE_PARAM( vi_pesid );
  1033. }
  1034. static void blackbird_set_default_dnr_params(struct cx8802_dev *dev)
  1035. {
  1036. /* assign dnr filter mode */
  1037. if( dev->dnr_params.mode > BLACKBIRD_DNR_BITS_AUTO )
  1038. dev->dnr_params.mode = BLACKBIRD_DNR_BITS_MANUAL;
  1039. if( dev->dnr_params.type > BLACKBIRD_MEDIAN_FILTER_DIAGONAL )
  1040. dev->dnr_params.type = BLACKBIRD_MEDIAN_FILTER_DISABLED;
  1041. blackbird_api_cmd(dev, CX2341X_ENC_SET_DNR_FILTER_MODE, 2, 0,
  1042. dev->dnr_params.mode,
  1043. dev->dnr_params.type
  1044. );
  1045. /* assign dnr filter props*/
  1046. if( dev->dnr_params.spatial > 15 )
  1047. dev->dnr_params.spatial = 15;
  1048. if( dev->dnr_params.temporal > 31 )
  1049. dev->dnr_params.temporal = 31;
  1050. blackbird_api_cmd(dev, CX2341X_ENC_SET_DNR_FILTER_PROPS, 2, 0,
  1051. dev->dnr_params.spatial,
  1052. dev->dnr_params.temporal
  1053. );
  1054. }
  1055. #define CHECK_DNR_PARAM( name ) ( dev->dnr_params.name != dnr_params->name )
  1056. #define UPDATE_DNR_PARAM( name ) dev->dnr_params.name = dnr_params->name
  1057. void blackbird_set_dnr_params(struct cx8802_dev *dev, struct blackbird_dnr* dnr_params)
  1058. {
  1059. /* assign dnr filter mode */
  1060. /* clamp values */
  1061. if( dnr_params->mode > BLACKBIRD_DNR_BITS_AUTO )
  1062. dnr_params->mode = BLACKBIRD_DNR_BITS_MANUAL;
  1063. if( dnr_params->type > BLACKBIRD_MEDIAN_FILTER_DIAGONAL )
  1064. dnr_params->type = BLACKBIRD_MEDIAN_FILTER_DISABLED;
  1065. /* check if the params actually changed */
  1066. if( CHECK_DNR_PARAM( mode ) || CHECK_DNR_PARAM( type ) )
  1067. {
  1068. UPDATE_DNR_PARAM( mode );
  1069. UPDATE_DNR_PARAM( type );
  1070. blackbird_api_cmd(dev, CX2341X_ENC_SET_DNR_FILTER_MODE, 2, 0, dnr_params->mode, dnr_params->type);
  1071. }
  1072. /* assign dnr filter props*/
  1073. if( dnr_params->spatial > 15 )
  1074. dnr_params->spatial = 15;
  1075. if( dnr_params->temporal > 31 )
  1076. dnr_params->temporal = 31;
  1077. if( CHECK_DNR_PARAM( spatial ) || CHECK_DNR_PARAM( temporal ) )
  1078. {
  1079. UPDATE_DNR_PARAM( spatial );
  1080. UPDATE_DNR_PARAM( temporal );
  1081. blackbird_api_cmd(dev, CX2341X_ENC_SET_DNR_FILTER_PROPS, 2, 0, dnr_params->spatial, dnr_params->temporal);
  1082. }
  1083. }
  1084. static void blackbird_codec_settings(struct cx8802_dev *dev)
  1085. {
  1086. /* assign output port */
  1087. blackbird_api_cmd(dev, CX2341X_ENC_SET_OUTPUT_PORT, 1, 0, BLACKBIRD_OUTPUT_PORT_STREAMING); /* Host */
  1088. /* assign frame size */
  1089. blackbird_api_cmd(dev, CX2341X_ENC_SET_FRAME_SIZE, 2, 0,
  1090. dev->height, dev->width);
  1091. /* assign coring levels (luma_h, luma_l, chroma_h, chroma_l) */
  1092. blackbird_api_cmd(dev, CX2341X_ENC_SET_CORING_LEVELS, 4, 0, 0, 255, 0, 255);
  1093. /* assign spatial filter type: luma_t: horiz_only, chroma_t: horiz_only */
  1094. blackbird_api_cmd(dev, CX2341X_ENC_SET_SPATIAL_FILTER_TYPE, 2, 0,
  1095. BLACKBIRD_SPATIAL_FILTER_LUMA_1D_HORIZ,
  1096. BLACKBIRD_SPATIAL_FILTER_CHROMA_1D_HORIZ
  1097. );
  1098. /* assign frame drop rate */
  1099. /* blackbird_api_cmd(dev, IVTV_API_ASSIGN_FRAME_DROP_RATE, 1, 0, 0); */
  1100. blackbird_set_default_params(dev);
  1101. blackbird_set_default_dnr_params(dev);
  1102. }
  1103. static int blackbird_initialize_codec(struct cx8802_dev *dev)
  1104. {
  1105. struct cx88_core *core = dev->core;
  1106. int version;
  1107. int retval;
  1108. dprintk(1,"Initialize codec\n");
  1109. retval = blackbird_api_cmd(dev, CX2341X_ENC_PING_FW, 0, 0); /* ping */
  1110. if (retval < 0) {
  1111. /* ping was not successful, reset and upload firmware */
  1112. cx_write(MO_SRST_IO, 0); /* SYS_RSTO=0 */
  1113. msleep(1);
  1114. cx_write(MO_SRST_IO, 1); /* SYS_RSTO=1 */
  1115. msleep(1);
  1116. retval = blackbird_load_firmware(dev);
  1117. if (retval < 0)
  1118. return retval;
  1119. dev->mailbox = blackbird_find_mailbox(dev);
  1120. if (dev->mailbox < 0)
  1121. return -1;
  1122. retval = blackbird_api_cmd(dev, CX2341X_ENC_PING_FW, 0, 0); /* ping */
  1123. if (retval < 0) {
  1124. dprintk(0, "ERROR: Firmware ping failed!\n");
  1125. return -1;
  1126. }
  1127. retval = blackbird_api_cmd(dev, CX2341X_ENC_GET_VERSION, 0, 1, &version);
  1128. if (retval < 0) {
  1129. dprintk(0, "ERROR: Firmware get encoder version failed!\n");
  1130. return -1;
  1131. }
  1132. dprintk(0, "Firmware version is 0x%08x\n", version);
  1133. }
  1134. msleep(1);
  1135. cx_write(MO_PINMUX_IO, 0x88); /* 656-8bit IO and enable MPEG parallel IO */
  1136. cx_clear(MO_INPUT_FORMAT, 0x100); /* chroma subcarrier lock to normal? */
  1137. cx_write(MO_VBOS_CONTROL, 0x84A00); /* no 656 mode, 8-bit pixels, disable VBI */
  1138. cx_clear(MO_OUTPUT_FORMAT, 0x0008); /* Normal Y-limits to let the mpeg encoder sync */
  1139. blackbird_codec_settings(dev);
  1140. msleep(1);
  1141. /* blackbird_api_cmd(dev, IVTV_API_ASSIGN_NUM_VSYNC_LINES, 4, 0, 0xef, 0xef);
  1142. blackbird_api_cmd(dev, IVTV_API_ASSIGN_NUM_VSYNC_LINES, 4, 0, 0xf0, 0xf0);
  1143. blackbird_api_cmd(dev, IVTV_API_ASSIGN_NUM_VSYNC_LINES, 4, 0, 0x180, 0x180); */
  1144. blackbird_api_cmd(dev, CX2341X_ENC_SET_NUM_VSYNC_LINES, 2, 0,
  1145. BLACKBIRD_FIELD1_SAA7115,
  1146. BLACKBIRD_FIELD2_SAA7115
  1147. );
  1148. /* blackbird_api_cmd(dev, IVTV_API_ASSIGN_PLACEHOLDER, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); */
  1149. blackbird_api_cmd(dev, CX2341X_ENC_SET_PLACEHOLDER, 12, 0,
  1150. BLACKBIRD_CUSTOM_EXTENSION_USR_DATA,
  1151. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
  1152. /* initialize the video input */
  1153. blackbird_api_cmd(dev, CX2341X_ENC_INITIALIZE_INPUT, 0, 0);
  1154. msleep(1);
  1155. blackbird_api_cmd(dev, CX2341X_ENC_MUTE_VIDEO, 1, 0, BLACKBIRD_UNMUTE);
  1156. msleep(1);
  1157. blackbird_api_cmd(dev, CX2341X_ENC_MUTE_AUDIO, 1, 0, BLACKBIRD_UNMUTE);
  1158. msleep(1);
  1159. /* start capturing to the host interface */
  1160. /* blackbird_api_cmd(dev, CX2341X_ENC_START_CAPTURE, 2, 0, 0, 0x13); */
  1161. blackbird_api_cmd(dev, CX2341X_ENC_START_CAPTURE, 2, 0,
  1162. BLACKBIRD_MPEG_CAPTURE,
  1163. BLACKBIRD_RAW_BITS_NONE
  1164. );
  1165. msleep(10);
  1166. blackbird_api_cmd(dev, CX2341X_ENC_REFRESH_INPUT, 0,0);
  1167. return 0;
  1168. }
  1169. /* ------------------------------------------------------------------ */
  1170. static int bb_buf_setup(struct videobuf_queue *q,
  1171. unsigned int *count, unsigned int *size)
  1172. {
  1173. struct cx8802_fh *fh = q->priv_data;
  1174. fh->dev->ts_packet_size = 188 * 4; /* was: 512 */
  1175. fh->dev->ts_packet_count = mpegbufs; /* was: 100 */
  1176. *size = fh->dev->ts_packet_size * fh->dev->ts_packet_count;
  1177. *count = fh->dev->ts_packet_count;
  1178. return 0;
  1179. }
  1180. static int
  1181. bb_buf_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
  1182. enum v4l2_field field)
  1183. {
  1184. struct cx8802_fh *fh = q->priv_data;
  1185. return cx8802_buf_prepare(q, fh->dev, (struct cx88_buffer*)vb, field);
  1186. }
  1187. static void
  1188. bb_buf_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
  1189. {
  1190. struct cx8802_fh *fh = q->priv_data;
  1191. cx8802_buf_queue(fh->dev, (struct cx88_buffer*)vb);
  1192. }
  1193. static void
  1194. bb_buf_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
  1195. {
  1196. cx88_free_buffer(q, (struct cx88_buffer*)vb);
  1197. }
  1198. static struct videobuf_queue_ops blackbird_qops = {
  1199. .buf_setup = bb_buf_setup,
  1200. .buf_prepare = bb_buf_prepare,
  1201. .buf_queue = bb_buf_queue,
  1202. .buf_release = bb_buf_release,
  1203. };
  1204. /* ------------------------------------------------------------------ */
  1205. static int mpeg_do_ioctl(struct inode *inode, struct file *file,
  1206. unsigned int cmd, void *arg)
  1207. {
  1208. struct cx8802_fh *fh = file->private_data;
  1209. struct cx8802_dev *dev = fh->dev;
  1210. struct cx88_core *core = dev->core;
  1211. if (debug > 1)
  1212. v4l_print_ioctl(core->name,cmd);
  1213. switch (cmd) {
  1214. /* --- capabilities ------------------------------------------ */
  1215. case VIDIOC_QUERYCAP:
  1216. {
  1217. struct v4l2_capability *cap = arg;
  1218. memset(cap,0,sizeof(*cap));
  1219. strcpy(cap->driver, "cx88_blackbird");
  1220. strlcpy(cap->card, cx88_boards[core->board].name,sizeof(cap->card));
  1221. sprintf(cap->bus_info,"PCI:%s",pci_name(dev->pci));
  1222. cap->version = CX88_VERSION_CODE;
  1223. cap->capabilities =
  1224. V4L2_CAP_VIDEO_CAPTURE |
  1225. V4L2_CAP_READWRITE |
  1226. V4L2_CAP_STREAMING |
  1227. 0;
  1228. if (UNSET != core->tuner_type)
  1229. cap->capabilities |= V4L2_CAP_TUNER;
  1230. return 0;
  1231. }
  1232. /* --- capture ioctls ---------------------------------------- */
  1233. case VIDIOC_ENUM_FMT:
  1234. {
  1235. struct v4l2_fmtdesc *f = arg;
  1236. int index;
  1237. index = f->index;
  1238. if (index != 0)
  1239. return -EINVAL;
  1240. memset(f,0,sizeof(*f));
  1241. f->index = index;
  1242. strlcpy(f->description, "MPEG", sizeof(f->description));
  1243. f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1244. f->pixelformat = V4L2_PIX_FMT_MPEG;
  1245. return 0;
  1246. }
  1247. case VIDIOC_G_FMT:
  1248. {
  1249. struct v4l2_format *f = arg;
  1250. memset(f,0,sizeof(*f));
  1251. f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1252. f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
  1253. f->fmt.pix.bytesperline = 0;
  1254. f->fmt.pix.sizeimage = dev->ts_packet_size * dev->ts_packet_count; /* 188 * 4 * 1024; */
  1255. f->fmt.pix.colorspace = 0;
  1256. f->fmt.pix.width = dev->width;
  1257. f->fmt.pix.height = dev->height;
  1258. f->fmt.pix.field = fh->mpegq.field;
  1259. dprintk(0,"VIDIOC_G_FMT: w: %d, h: %d, f: %d\n",
  1260. dev->width, dev->height, fh->mpegq.field );
  1261. return 0;
  1262. }
  1263. case VIDIOC_TRY_FMT:
  1264. {
  1265. struct v4l2_format *f = arg;
  1266. f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1267. f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
  1268. f->fmt.pix.bytesperline = 0;
  1269. f->fmt.pix.sizeimage = dev->ts_packet_size * dev->ts_packet_count; /* 188 * 4 * 1024; */;
  1270. f->fmt.pix.colorspace = 0;
  1271. dprintk(0,"VIDIOC_TRY_FMT: w: %d, h: %d, f: %d\n",
  1272. dev->width, dev->height, fh->mpegq.field );
  1273. return 0;
  1274. }
  1275. case VIDIOC_S_FMT:
  1276. {
  1277. struct v4l2_format *f = arg;
  1278. f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1279. f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
  1280. f->fmt.pix.bytesperline = 0;
  1281. f->fmt.pix.sizeimage = dev->ts_packet_size * dev->ts_packet_count; /* 188 * 4 * 1024; */;
  1282. f->fmt.pix.colorspace = 0;
  1283. dprintk(0,"VIDIOC_S_FMT: w: %d, h: %d, f: %d\n",
  1284. f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.field );
  1285. return 0;
  1286. }
  1287. /* --- streaming capture ------------------------------------- */
  1288. case VIDIOC_REQBUFS:
  1289. return videobuf_reqbufs(&fh->mpegq, arg);
  1290. case VIDIOC_QUERYBUF:
  1291. return videobuf_querybuf(&fh->mpegq, arg);
  1292. case VIDIOC_QBUF:
  1293. return videobuf_qbuf(&fh->mpegq, arg);
  1294. case VIDIOC_DQBUF:
  1295. return videobuf_dqbuf(&fh->mpegq, arg,
  1296. file->f_flags & O_NONBLOCK);
  1297. case VIDIOC_STREAMON:
  1298. return videobuf_streamon(&fh->mpegq);
  1299. case VIDIOC_STREAMOFF:
  1300. return videobuf_streamoff(&fh->mpegq);
  1301. /* --- mpeg compression -------------------------------------- */
  1302. case VIDIOC_G_MPEGCOMP:
  1303. {
  1304. struct v4l2_mpeg_compression *f = arg;
  1305. memcpy(f,&dev->params,sizeof(*f));
  1306. return 0;
  1307. }
  1308. case VIDIOC_S_MPEGCOMP:
  1309. {
  1310. struct v4l2_mpeg_compression *f = arg;
  1311. blackbird_set_params(dev, f);
  1312. return 0;
  1313. }
  1314. case VIDIOC_S_FREQUENCY:
  1315. {
  1316. blackbird_api_cmd(fh->dev, CX2341X_ENC_STOP_CAPTURE, 3, 0,
  1317. BLACKBIRD_END_NOW,
  1318. BLACKBIRD_MPEG_CAPTURE,
  1319. BLACKBIRD_RAW_BITS_NONE);
  1320. cx88_do_ioctl( inode, file, 0, dev->core, cmd, arg, cx88_ioctl_hook );
  1321. blackbird_initialize_codec(dev);
  1322. cx88_set_scale(dev->core, dev->width, dev->height,
  1323. fh->mpegq.field);
  1324. return 0;
  1325. }
  1326. default:
  1327. return cx88_do_ioctl( inode, file, 0, dev->core, cmd, arg, cx88_ioctl_hook );
  1328. }
  1329. return 0;
  1330. }
  1331. int (*cx88_ioctl_hook)(struct inode *inode, struct file *file,
  1332. unsigned int cmd, void *arg);
  1333. unsigned int (*cx88_ioctl_translator)(unsigned int cmd);
  1334. static unsigned int mpeg_translate_ioctl(unsigned int cmd)
  1335. {
  1336. return cmd;
  1337. }
  1338. static int mpeg_ioctl(struct inode *inode, struct file *file,
  1339. unsigned int cmd, unsigned long arg)
  1340. {
  1341. cmd = cx88_ioctl_translator( cmd );
  1342. return video_usercopy(inode, file, cmd, arg, cx88_ioctl_hook);
  1343. }
  1344. static int mpeg_open(struct inode *inode, struct file *file)
  1345. {
  1346. int minor = iminor(inode);
  1347. struct cx8802_dev *h,*dev = NULL;
  1348. struct cx8802_fh *fh;
  1349. struct list_head *list;
  1350. list_for_each(list,&cx8802_devlist) {
  1351. h = list_entry(list, struct cx8802_dev, devlist);
  1352. if (h->mpeg_dev->minor == minor)
  1353. dev = h;
  1354. }
  1355. if (NULL == dev)
  1356. return -ENODEV;
  1357. if (blackbird_initialize_codec(dev) < 0)
  1358. return -EINVAL;
  1359. dprintk(1,"open minor=%d\n",minor);
  1360. /* allocate + initialize per filehandle data */
  1361. fh = kzalloc(sizeof(*fh),GFP_KERNEL);
  1362. if (NULL == fh)
  1363. return -ENOMEM;
  1364. file->private_data = fh;
  1365. fh->dev = dev;
  1366. videobuf_queue_init(&fh->mpegq, &blackbird_qops,
  1367. dev->pci, &dev->slock,
  1368. V4L2_BUF_TYPE_VIDEO_CAPTURE,
  1369. V4L2_FIELD_INTERLACED,
  1370. sizeof(struct cx88_buffer),
  1371. fh);
  1372. /* FIXME: locking against other video device */
  1373. cx88_set_scale(dev->core, dev->width, dev->height,
  1374. fh->mpegq.field);
  1375. return 0;
  1376. }
  1377. static int mpeg_release(struct inode *inode, struct file *file)
  1378. {
  1379. struct cx8802_fh *fh = file->private_data;
  1380. /* blackbird_api_cmd(fh->dev, CX2341X_ENC_STOP_CAPTURE, 3, 0, BLACKBIRD_END_NOW, 0, 0x13); */
  1381. blackbird_api_cmd(fh->dev, CX2341X_ENC_STOP_CAPTURE, 3, 0,
  1382. BLACKBIRD_END_NOW,
  1383. BLACKBIRD_MPEG_CAPTURE,
  1384. BLACKBIRD_RAW_BITS_NONE
  1385. );
  1386. cx8802_cancel_buffers(fh->dev);
  1387. /* stop mpeg capture */
  1388. if (fh->mpegq.streaming)
  1389. videobuf_streamoff(&fh->mpegq);
  1390. if (fh->mpegq.reading)
  1391. videobuf_read_stop(&fh->mpegq);
  1392. videobuf_mmap_free(&fh->mpegq);
  1393. file->private_data = NULL;
  1394. kfree(fh);
  1395. return 0;
  1396. }
  1397. static ssize_t
  1398. mpeg_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
  1399. {
  1400. struct cx8802_fh *fh = file->private_data;
  1401. return videobuf_read_stream(&fh->mpegq, data, count, ppos, 0,
  1402. file->f_flags & O_NONBLOCK);
  1403. }
  1404. static unsigned int
  1405. mpeg_poll(struct file *file, struct poll_table_struct *wait)
  1406. {
  1407. struct cx8802_fh *fh = file->private_data;
  1408. return videobuf_poll_stream(file, &fh->mpegq, wait);
  1409. }
  1410. static int
  1411. mpeg_mmap(struct file *file, struct vm_area_struct * vma)
  1412. {
  1413. struct cx8802_fh *fh = file->private_data;
  1414. return videobuf_mmap_mapper(&fh->mpegq, vma);
  1415. }
  1416. static struct file_operations mpeg_fops =
  1417. {
  1418. .owner = THIS_MODULE,
  1419. .open = mpeg_open,
  1420. .release = mpeg_release,
  1421. .read = mpeg_read,
  1422. .poll = mpeg_poll,
  1423. .mmap = mpeg_mmap,
  1424. .ioctl = mpeg_ioctl,
  1425. .llseek = no_llseek,
  1426. };
  1427. static struct video_device cx8802_mpeg_template =
  1428. {
  1429. .name = "cx8802",
  1430. .type = VID_TYPE_CAPTURE|VID_TYPE_TUNER|VID_TYPE_SCALES|VID_TYPE_MPEG_ENCODER,
  1431. .hardware = 0,
  1432. .fops = &mpeg_fops,
  1433. .minor = -1,
  1434. };
  1435. /* ------------------------------------------------------------------ */
  1436. static void blackbird_unregister_video(struct cx8802_dev *dev)
  1437. {
  1438. if (dev->mpeg_dev) {
  1439. if (-1 != dev->mpeg_dev->minor)
  1440. video_unregister_device(dev->mpeg_dev);
  1441. else
  1442. video_device_release(dev->mpeg_dev);
  1443. dev->mpeg_dev = NULL;
  1444. }
  1445. }
  1446. static int blackbird_register_video(struct cx8802_dev *dev)
  1447. {
  1448. int err;
  1449. dev->mpeg_dev = cx88_vdev_init(dev->core,dev->pci,
  1450. &cx8802_mpeg_template,"mpeg");
  1451. err = video_register_device(dev->mpeg_dev,VFL_TYPE_GRABBER, -1);
  1452. if (err < 0) {
  1453. printk(KERN_INFO "%s/2: can't register mpeg device\n",
  1454. dev->core->name);
  1455. return err;
  1456. }
  1457. printk(KERN_INFO "%s/2: registered device video%d [mpeg]\n",
  1458. dev->core->name,dev->mpeg_dev->minor & 0x1f);
  1459. return 0;
  1460. }
  1461. /* ----------------------------------------------------------- */
  1462. static int __devinit blackbird_probe(struct pci_dev *pci_dev,
  1463. const struct pci_device_id *pci_id)
  1464. {
  1465. struct cx8802_dev *dev;
  1466. struct cx88_core *core;
  1467. int err;
  1468. /* general setup */
  1469. core = cx88_core_get(pci_dev);
  1470. if (NULL == core)
  1471. return -EINVAL;
  1472. err = -ENODEV;
  1473. if (!cx88_boards[core->board].blackbird)
  1474. goto fail_core;
  1475. err = -ENOMEM;
  1476. dev = kzalloc(sizeof(*dev),GFP_KERNEL);
  1477. if (NULL == dev)
  1478. goto fail_core;
  1479. dev->pci = pci_dev;
  1480. dev->core = core;
  1481. dev->width = 720;
  1482. dev->height = 576;
  1483. memcpy(&dev->params,&default_mpeg_params,sizeof(default_mpeg_params));
  1484. memcpy(&dev->dnr_params,&default_dnr_params,sizeof(default_dnr_params));
  1485. switch (core->board) {
  1486. case CX88_BOARD_HAUPPAUGE_ROSLYN:
  1487. if (core->tuner_formats & V4L2_STD_525_60) {
  1488. dev->height = 480;
  1489. dev->params.vi_frame_rate = 30;
  1490. } else {
  1491. dev->height = 576;
  1492. dev->params.vi_frame_rate = 25;
  1493. }
  1494. break;
  1495. case CX88_BOARD_KWORLD_HARDWARE_MPEG_TV_XPERT:
  1496. if (core->tvnorm->id & V4L2_STD_525_60) {
  1497. dev->height = 480;
  1498. dev->params.vi_frame_rate = 30;
  1499. } else {
  1500. dev->height = 576;
  1501. dev->params.vi_frame_rate = 25;
  1502. }
  1503. break;
  1504. }
  1505. err = cx8802_init_common(dev);
  1506. if (0 != err)
  1507. goto fail_free;
  1508. /* blackbird stuff */
  1509. printk("%s/2: cx23416 based mpeg encoder (blackbird reference design)\n",
  1510. core->name);
  1511. host_setup(dev->core);
  1512. list_add_tail(&dev->devlist,&cx8802_devlist);
  1513. blackbird_register_video(dev);
  1514. /* initial device configuration: needed ? */
  1515. return 0;
  1516. fail_free:
  1517. kfree(dev);
  1518. fail_core:
  1519. cx88_core_put(core,pci_dev);
  1520. return err;
  1521. }
  1522. static void __devexit blackbird_remove(struct pci_dev *pci_dev)
  1523. {
  1524. struct cx8802_dev *dev = pci_get_drvdata(pci_dev);
  1525. /* blackbird */
  1526. blackbird_unregister_video(dev);
  1527. list_del(&dev->devlist);
  1528. /* common */
  1529. cx8802_fini_common(dev);
  1530. cx88_core_put(dev->core,dev->pci);
  1531. kfree(dev);
  1532. }
  1533. static struct pci_device_id cx8802_pci_tbl[] = {
  1534. {
  1535. .vendor = 0x14f1,
  1536. .device = 0x8802,
  1537. .subvendor = PCI_ANY_ID,
  1538. .subdevice = PCI_ANY_ID,
  1539. },{
  1540. /* --- end of list --- */
  1541. }
  1542. };
  1543. MODULE_DEVICE_TABLE(pci, cx8802_pci_tbl);
  1544. static struct pci_driver blackbird_pci_driver = {
  1545. .name = "cx88-blackbird",
  1546. .id_table = cx8802_pci_tbl,
  1547. .probe = blackbird_probe,
  1548. .remove = __devexit_p(blackbird_remove),
  1549. .suspend = cx8802_suspend_common,
  1550. .resume = cx8802_resume_common,
  1551. };
  1552. static int blackbird_init(void)
  1553. {
  1554. printk(KERN_INFO "cx2388x blackbird driver version %d.%d.%d loaded\n",
  1555. (CX88_VERSION_CODE >> 16) & 0xff,
  1556. (CX88_VERSION_CODE >> 8) & 0xff,
  1557. CX88_VERSION_CODE & 0xff);
  1558. #ifdef SNAPSHOT
  1559. printk(KERN_INFO "cx2388x: snapshot date %04d-%02d-%02d\n",
  1560. SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
  1561. #endif
  1562. cx88_ioctl_hook = mpeg_do_ioctl;
  1563. cx88_ioctl_translator = mpeg_translate_ioctl;
  1564. return pci_register_driver(&blackbird_pci_driver);
  1565. }
  1566. static void blackbird_fini(void)
  1567. {
  1568. pci_unregister_driver(&blackbird_pci_driver);
  1569. }
  1570. module_init(blackbird_init);
  1571. module_exit(blackbird_fini);
  1572. EXPORT_SYMBOL(cx88_ioctl_hook);
  1573. EXPORT_SYMBOL(cx88_ioctl_translator);
  1574. EXPORT_SYMBOL(blackbird_set_params);
  1575. EXPORT_SYMBOL(blackbird_set_dnr_params);
  1576. /* ----------------------------------------------------------- */
  1577. /*
  1578. * Local variables:
  1579. * c-basic-offset: 8
  1580. * End:
  1581. * 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
  1582. */