cx88-blackbird.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764
  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. default:
  1315. return cx88_do_ioctl( inode, file, 0, dev->core, cmd, arg, cx88_ioctl_hook );
  1316. }
  1317. return 0;
  1318. }
  1319. int (*cx88_ioctl_hook)(struct inode *inode, struct file *file,
  1320. unsigned int cmd, void *arg);
  1321. unsigned int (*cx88_ioctl_translator)(unsigned int cmd);
  1322. static unsigned int mpeg_translate_ioctl(unsigned int cmd)
  1323. {
  1324. return cmd;
  1325. }
  1326. static int mpeg_ioctl(struct inode *inode, struct file *file,
  1327. unsigned int cmd, unsigned long arg)
  1328. {
  1329. cmd = cx88_ioctl_translator( cmd );
  1330. return video_usercopy(inode, file, cmd, arg, cx88_ioctl_hook);
  1331. }
  1332. static int mpeg_open(struct inode *inode, struct file *file)
  1333. {
  1334. int minor = iminor(inode);
  1335. struct cx8802_dev *h,*dev = NULL;
  1336. struct cx8802_fh *fh;
  1337. struct list_head *list;
  1338. list_for_each(list,&cx8802_devlist) {
  1339. h = list_entry(list, struct cx8802_dev, devlist);
  1340. if (h->mpeg_dev->minor == minor)
  1341. dev = h;
  1342. }
  1343. if (NULL == dev)
  1344. return -ENODEV;
  1345. if (blackbird_initialize_codec(dev) < 0)
  1346. return -EINVAL;
  1347. dprintk(1,"open minor=%d\n",minor);
  1348. /* allocate + initialize per filehandle data */
  1349. fh = kzalloc(sizeof(*fh),GFP_KERNEL);
  1350. if (NULL == fh)
  1351. return -ENOMEM;
  1352. file->private_data = fh;
  1353. fh->dev = dev;
  1354. videobuf_queue_init(&fh->mpegq, &blackbird_qops,
  1355. dev->pci, &dev->slock,
  1356. V4L2_BUF_TYPE_VIDEO_CAPTURE,
  1357. V4L2_FIELD_INTERLACED,
  1358. sizeof(struct cx88_buffer),
  1359. fh);
  1360. /* FIXME: locking against other video device */
  1361. cx88_set_scale(dev->core, dev->width, dev->height,
  1362. fh->mpegq.field);
  1363. return 0;
  1364. }
  1365. static int mpeg_release(struct inode *inode, struct file *file)
  1366. {
  1367. struct cx8802_fh *fh = file->private_data;
  1368. /* blackbird_api_cmd(fh->dev, CX2341X_ENC_STOP_CAPTURE, 3, 0, BLACKBIRD_END_NOW, 0, 0x13); */
  1369. blackbird_api_cmd(fh->dev, CX2341X_ENC_STOP_CAPTURE, 3, 0,
  1370. BLACKBIRD_END_NOW,
  1371. BLACKBIRD_MPEG_CAPTURE,
  1372. BLACKBIRD_RAW_BITS_NONE
  1373. );
  1374. cx8802_cancel_buffers(fh->dev);
  1375. /* stop mpeg capture */
  1376. if (fh->mpegq.streaming)
  1377. videobuf_streamoff(&fh->mpegq);
  1378. if (fh->mpegq.reading)
  1379. videobuf_read_stop(&fh->mpegq);
  1380. videobuf_mmap_free(&fh->mpegq);
  1381. file->private_data = NULL;
  1382. kfree(fh);
  1383. return 0;
  1384. }
  1385. static ssize_t
  1386. mpeg_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
  1387. {
  1388. struct cx8802_fh *fh = file->private_data;
  1389. return videobuf_read_stream(&fh->mpegq, data, count, ppos, 0,
  1390. file->f_flags & O_NONBLOCK);
  1391. }
  1392. static unsigned int
  1393. mpeg_poll(struct file *file, struct poll_table_struct *wait)
  1394. {
  1395. struct cx8802_fh *fh = file->private_data;
  1396. return videobuf_poll_stream(file, &fh->mpegq, wait);
  1397. }
  1398. static int
  1399. mpeg_mmap(struct file *file, struct vm_area_struct * vma)
  1400. {
  1401. struct cx8802_fh *fh = file->private_data;
  1402. return videobuf_mmap_mapper(&fh->mpegq, vma);
  1403. }
  1404. static struct file_operations mpeg_fops =
  1405. {
  1406. .owner = THIS_MODULE,
  1407. .open = mpeg_open,
  1408. .release = mpeg_release,
  1409. .read = mpeg_read,
  1410. .poll = mpeg_poll,
  1411. .mmap = mpeg_mmap,
  1412. .ioctl = mpeg_ioctl,
  1413. .llseek = no_llseek,
  1414. };
  1415. static struct video_device cx8802_mpeg_template =
  1416. {
  1417. .name = "cx8802",
  1418. .type = VID_TYPE_CAPTURE|VID_TYPE_TUNER|VID_TYPE_SCALES|VID_TYPE_MPEG_ENCODER,
  1419. .hardware = 0,
  1420. .fops = &mpeg_fops,
  1421. .minor = -1,
  1422. };
  1423. /* ------------------------------------------------------------------ */
  1424. static void blackbird_unregister_video(struct cx8802_dev *dev)
  1425. {
  1426. if (dev->mpeg_dev) {
  1427. if (-1 != dev->mpeg_dev->minor)
  1428. video_unregister_device(dev->mpeg_dev);
  1429. else
  1430. video_device_release(dev->mpeg_dev);
  1431. dev->mpeg_dev = NULL;
  1432. }
  1433. }
  1434. static int blackbird_register_video(struct cx8802_dev *dev)
  1435. {
  1436. int err;
  1437. dev->mpeg_dev = cx88_vdev_init(dev->core,dev->pci,
  1438. &cx8802_mpeg_template,"mpeg");
  1439. err = video_register_device(dev->mpeg_dev,VFL_TYPE_GRABBER, -1);
  1440. if (err < 0) {
  1441. printk(KERN_INFO "%s/2: can't register mpeg device\n",
  1442. dev->core->name);
  1443. return err;
  1444. }
  1445. printk(KERN_INFO "%s/2: registered device video%d [mpeg]\n",
  1446. dev->core->name,dev->mpeg_dev->minor & 0x1f);
  1447. return 0;
  1448. }
  1449. /* ----------------------------------------------------------- */
  1450. static int __devinit blackbird_probe(struct pci_dev *pci_dev,
  1451. const struct pci_device_id *pci_id)
  1452. {
  1453. struct cx8802_dev *dev;
  1454. struct cx88_core *core;
  1455. int err;
  1456. /* general setup */
  1457. core = cx88_core_get(pci_dev);
  1458. if (NULL == core)
  1459. return -EINVAL;
  1460. err = -ENODEV;
  1461. if (!cx88_boards[core->board].blackbird)
  1462. goto fail_core;
  1463. err = -ENOMEM;
  1464. dev = kzalloc(sizeof(*dev),GFP_KERNEL);
  1465. if (NULL == dev)
  1466. goto fail_core;
  1467. dev->pci = pci_dev;
  1468. dev->core = core;
  1469. dev->width = 720;
  1470. dev->height = 576;
  1471. memcpy(&dev->params,&default_mpeg_params,sizeof(default_mpeg_params));
  1472. memcpy(&dev->dnr_params,&default_dnr_params,sizeof(default_dnr_params));
  1473. switch (core->board) {
  1474. case CX88_BOARD_HAUPPAUGE_ROSLYN:
  1475. if (core->tuner_formats & V4L2_STD_525_60) {
  1476. dev->height = 480;
  1477. dev->params.vi_frame_rate = 30;
  1478. } else {
  1479. dev->height = 576;
  1480. dev->params.vi_frame_rate = 25;
  1481. }
  1482. break;
  1483. case CX88_BOARD_KWORLD_HARDWARE_MPEG_TV_XPERT:
  1484. if (core->tvnorm->id & V4L2_STD_525_60) {
  1485. dev->height = 480;
  1486. dev->params.vi_frame_rate = 30;
  1487. } else {
  1488. dev->height = 576;
  1489. dev->params.vi_frame_rate = 25;
  1490. }
  1491. break;
  1492. }
  1493. err = cx8802_init_common(dev);
  1494. if (0 != err)
  1495. goto fail_free;
  1496. /* blackbird stuff */
  1497. printk("%s/2: cx23416 based mpeg encoder (blackbird reference design)\n",
  1498. core->name);
  1499. host_setup(dev->core);
  1500. list_add_tail(&dev->devlist,&cx8802_devlist);
  1501. blackbird_register_video(dev);
  1502. /* initial device configuration: needed ? */
  1503. return 0;
  1504. fail_free:
  1505. kfree(dev);
  1506. fail_core:
  1507. cx88_core_put(core,pci_dev);
  1508. return err;
  1509. }
  1510. static void __devexit blackbird_remove(struct pci_dev *pci_dev)
  1511. {
  1512. struct cx8802_dev *dev = pci_get_drvdata(pci_dev);
  1513. /* blackbird */
  1514. blackbird_unregister_video(dev);
  1515. list_del(&dev->devlist);
  1516. /* common */
  1517. cx8802_fini_common(dev);
  1518. cx88_core_put(dev->core,dev->pci);
  1519. kfree(dev);
  1520. }
  1521. static struct pci_device_id cx8802_pci_tbl[] = {
  1522. {
  1523. .vendor = 0x14f1,
  1524. .device = 0x8802,
  1525. .subvendor = PCI_ANY_ID,
  1526. .subdevice = PCI_ANY_ID,
  1527. },{
  1528. /* --- end of list --- */
  1529. }
  1530. };
  1531. MODULE_DEVICE_TABLE(pci, cx8802_pci_tbl);
  1532. static struct pci_driver blackbird_pci_driver = {
  1533. .name = "cx88-blackbird",
  1534. .id_table = cx8802_pci_tbl,
  1535. .probe = blackbird_probe,
  1536. .remove = __devexit_p(blackbird_remove),
  1537. .suspend = cx8802_suspend_common,
  1538. .resume = cx8802_resume_common,
  1539. };
  1540. static int blackbird_init(void)
  1541. {
  1542. printk(KERN_INFO "cx2388x blackbird driver version %d.%d.%d loaded\n",
  1543. (CX88_VERSION_CODE >> 16) & 0xff,
  1544. (CX88_VERSION_CODE >> 8) & 0xff,
  1545. CX88_VERSION_CODE & 0xff);
  1546. #ifdef SNAPSHOT
  1547. printk(KERN_INFO "cx2388x: snapshot date %04d-%02d-%02d\n",
  1548. SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
  1549. #endif
  1550. cx88_ioctl_hook = mpeg_do_ioctl;
  1551. cx88_ioctl_translator = mpeg_translate_ioctl;
  1552. return pci_register_driver(&blackbird_pci_driver);
  1553. }
  1554. static void blackbird_fini(void)
  1555. {
  1556. pci_unregister_driver(&blackbird_pci_driver);
  1557. }
  1558. module_init(blackbird_init);
  1559. module_exit(blackbird_fini);
  1560. EXPORT_SYMBOL(cx88_ioctl_hook);
  1561. EXPORT_SYMBOL(cx88_ioctl_translator);
  1562. EXPORT_SYMBOL(blackbird_set_params);
  1563. EXPORT_SYMBOL(blackbird_set_dnr_params);
  1564. /* ----------------------------------------------------------- */
  1565. /*
  1566. * Local variables:
  1567. * c-basic-offset: 8
  1568. * End:
  1569. * 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
  1570. */