cx88-blackbird.c 55 KB

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