cx88-blackbird.c 53 KB

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