cx88-blackbird.c 55 KB

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