cx88-blackbird.c 55 KB

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