cx231xx-417.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028
  1. /*
  2. *
  3. * Support for a cx23417 mpeg encoder via cx231xx host port.
  4. *
  5. * (c) 2004 Jelle Foks <jelle@foks.us>
  6. * (c) 2004 Gerd Knorr <kraxel@bytesex.org>
  7. * (c) 2008 Steven Toth <stoth@linuxtv.org>
  8. * - CX23885/7/8 support
  9. *
  10. * Includes parts from the ivtv driver( http://ivtv.sourceforge.net/),
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  25. */
  26. #include "cx231xx.h"
  27. #include <linux/module.h>
  28. #include <linux/moduleparam.h>
  29. #include <linux/init.h>
  30. #include <linux/fs.h>
  31. #include <linux/delay.h>
  32. #include <linux/device.h>
  33. #include <linux/firmware.h>
  34. #include <linux/vmalloc.h>
  35. #include <media/v4l2-common.h>
  36. #include <media/v4l2-ioctl.h>
  37. #include <media/v4l2-event.h>
  38. #include <media/drv-intf/cx2341x.h>
  39. #include <media/tuner.h>
  40. #define CX231xx_FIRM_IMAGE_SIZE 376836
  41. #define CX231xx_FIRM_IMAGE_NAME "/*(DEBLOBBED)*/"
  42. /* for polaris ITVC */
  43. #define ITVC_WRITE_DIR 0x03FDFC00
  44. #define ITVC_READ_DIR 0x0001FC00
  45. #define MCI_MEMORY_DATA_BYTE0 0x00
  46. #define MCI_MEMORY_DATA_BYTE1 0x08
  47. #define MCI_MEMORY_DATA_BYTE2 0x10
  48. #define MCI_MEMORY_DATA_BYTE3 0x18
  49. #define MCI_MEMORY_ADDRESS_BYTE2 0x20
  50. #define MCI_MEMORY_ADDRESS_BYTE1 0x28
  51. #define MCI_MEMORY_ADDRESS_BYTE0 0x30
  52. #define MCI_REGISTER_DATA_BYTE0 0x40
  53. #define MCI_REGISTER_DATA_BYTE1 0x48
  54. #define MCI_REGISTER_DATA_BYTE2 0x50
  55. #define MCI_REGISTER_DATA_BYTE3 0x58
  56. #define MCI_REGISTER_ADDRESS_BYTE0 0x60
  57. #define MCI_REGISTER_ADDRESS_BYTE1 0x68
  58. #define MCI_REGISTER_MODE 0x70
  59. /* Read and write modes for polaris ITVC */
  60. #define MCI_MODE_REGISTER_READ 0x000
  61. #define MCI_MODE_REGISTER_WRITE 0x100
  62. #define MCI_MODE_MEMORY_READ 0x000
  63. #define MCI_MODE_MEMORY_WRITE 0x4000
  64. static unsigned int mpegbufs = 8;
  65. module_param(mpegbufs, int, 0644);
  66. MODULE_PARM_DESC(mpegbufs, "number of mpeg buffers, range 2-32");
  67. static unsigned int mpeglines = 128;
  68. module_param(mpeglines, int, 0644);
  69. MODULE_PARM_DESC(mpeglines, "number of lines in an MPEG buffer, range 2-32");
  70. static unsigned int mpeglinesize = 512;
  71. module_param(mpeglinesize, int, 0644);
  72. MODULE_PARM_DESC(mpeglinesize,
  73. "number of bytes in each line of an MPEG buffer, range 512-1024");
  74. static unsigned int v4l_debug = 1;
  75. module_param(v4l_debug, int, 0644);
  76. MODULE_PARM_DESC(v4l_debug, "enable V4L debug messages");
  77. #define dprintk(level, fmt, arg...) \
  78. do { \
  79. if (v4l_debug >= level) \
  80. printk(KERN_DEBUG pr_fmt(fmt), ## arg); \
  81. } while (0)
  82. static struct cx231xx_tvnorm cx231xx_tvnorms[] = {
  83. {
  84. .name = "NTSC-M",
  85. .id = V4L2_STD_NTSC_M,
  86. }, {
  87. .name = "NTSC-JP",
  88. .id = V4L2_STD_NTSC_M_JP,
  89. }, {
  90. .name = "PAL-BG",
  91. .id = V4L2_STD_PAL_BG,
  92. }, {
  93. .name = "PAL-DK",
  94. .id = V4L2_STD_PAL_DK,
  95. }, {
  96. .name = "PAL-I",
  97. .id = V4L2_STD_PAL_I,
  98. }, {
  99. .name = "PAL-M",
  100. .id = V4L2_STD_PAL_M,
  101. }, {
  102. .name = "PAL-N",
  103. .id = V4L2_STD_PAL_N,
  104. }, {
  105. .name = "PAL-Nc",
  106. .id = V4L2_STD_PAL_Nc,
  107. }, {
  108. .name = "PAL-60",
  109. .id = V4L2_STD_PAL_60,
  110. }, {
  111. .name = "SECAM-L",
  112. .id = V4L2_STD_SECAM_L,
  113. }, {
  114. .name = "SECAM-DK",
  115. .id = V4L2_STD_SECAM_DK,
  116. }
  117. };
  118. /* ------------------------------------------------------------------ */
  119. enum cx231xx_capture_type {
  120. CX231xx_MPEG_CAPTURE,
  121. CX231xx_RAW_CAPTURE,
  122. CX231xx_RAW_PASSTHRU_CAPTURE
  123. };
  124. enum cx231xx_capture_bits {
  125. CX231xx_RAW_BITS_NONE = 0x00,
  126. CX231xx_RAW_BITS_YUV_CAPTURE = 0x01,
  127. CX231xx_RAW_BITS_PCM_CAPTURE = 0x02,
  128. CX231xx_RAW_BITS_VBI_CAPTURE = 0x04,
  129. CX231xx_RAW_BITS_PASSTHRU_CAPTURE = 0x08,
  130. CX231xx_RAW_BITS_TO_HOST_CAPTURE = 0x10
  131. };
  132. enum cx231xx_capture_end {
  133. CX231xx_END_AT_GOP, /* stop at the end of gop, generate irq */
  134. CX231xx_END_NOW, /* stop immediately, no irq */
  135. };
  136. enum cx231xx_framerate {
  137. CX231xx_FRAMERATE_NTSC_30, /* NTSC: 30fps */
  138. CX231xx_FRAMERATE_PAL_25 /* PAL: 25fps */
  139. };
  140. enum cx231xx_stream_port {
  141. CX231xx_OUTPUT_PORT_MEMORY,
  142. CX231xx_OUTPUT_PORT_STREAMING,
  143. CX231xx_OUTPUT_PORT_SERIAL
  144. };
  145. enum cx231xx_data_xfer_status {
  146. CX231xx_MORE_BUFFERS_FOLLOW,
  147. CX231xx_LAST_BUFFER,
  148. };
  149. enum cx231xx_picture_mask {
  150. CX231xx_PICTURE_MASK_NONE,
  151. CX231xx_PICTURE_MASK_I_FRAMES,
  152. CX231xx_PICTURE_MASK_I_P_FRAMES = 0x3,
  153. CX231xx_PICTURE_MASK_ALL_FRAMES = 0x7,
  154. };
  155. enum cx231xx_vbi_mode_bits {
  156. CX231xx_VBI_BITS_SLICED,
  157. CX231xx_VBI_BITS_RAW,
  158. };
  159. enum cx231xx_vbi_insertion_bits {
  160. CX231xx_VBI_BITS_INSERT_IN_XTENSION_USR_DATA,
  161. CX231xx_VBI_BITS_INSERT_IN_PRIVATE_PACKETS = 0x1 << 1,
  162. CX231xx_VBI_BITS_SEPARATE_STREAM = 0x2 << 1,
  163. CX231xx_VBI_BITS_SEPARATE_STREAM_USR_DATA = 0x4 << 1,
  164. CX231xx_VBI_BITS_SEPARATE_STREAM_PRV_DATA = 0x5 << 1,
  165. };
  166. enum cx231xx_dma_unit {
  167. CX231xx_DMA_BYTES,
  168. CX231xx_DMA_FRAMES,
  169. };
  170. enum cx231xx_dma_transfer_status_bits {
  171. CX231xx_DMA_TRANSFER_BITS_DONE = 0x01,
  172. CX231xx_DMA_TRANSFER_BITS_ERROR = 0x04,
  173. CX231xx_DMA_TRANSFER_BITS_LL_ERROR = 0x10,
  174. };
  175. enum cx231xx_pause {
  176. CX231xx_PAUSE_ENCODING,
  177. CX231xx_RESUME_ENCODING,
  178. };
  179. enum cx231xx_copyright {
  180. CX231xx_COPYRIGHT_OFF,
  181. CX231xx_COPYRIGHT_ON,
  182. };
  183. enum cx231xx_notification_type {
  184. CX231xx_NOTIFICATION_REFRESH,
  185. };
  186. enum cx231xx_notification_status {
  187. CX231xx_NOTIFICATION_OFF,
  188. CX231xx_NOTIFICATION_ON,
  189. };
  190. enum cx231xx_notification_mailbox {
  191. CX231xx_NOTIFICATION_NO_MAILBOX = -1,
  192. };
  193. enum cx231xx_field1_lines {
  194. CX231xx_FIELD1_SAA7114 = 0x00EF, /* 239 */
  195. CX231xx_FIELD1_SAA7115 = 0x00F0, /* 240 */
  196. CX231xx_FIELD1_MICRONAS = 0x0105, /* 261 */
  197. };
  198. enum cx231xx_field2_lines {
  199. CX231xx_FIELD2_SAA7114 = 0x00EF, /* 239 */
  200. CX231xx_FIELD2_SAA7115 = 0x00F0, /* 240 */
  201. CX231xx_FIELD2_MICRONAS = 0x0106, /* 262 */
  202. };
  203. enum cx231xx_custom_data_type {
  204. CX231xx_CUSTOM_EXTENSION_USR_DATA,
  205. CX231xx_CUSTOM_PRIVATE_PACKET,
  206. };
  207. enum cx231xx_mute {
  208. CX231xx_UNMUTE,
  209. CX231xx_MUTE,
  210. };
  211. enum cx231xx_mute_video_mask {
  212. CX231xx_MUTE_VIDEO_V_MASK = 0x0000FF00,
  213. CX231xx_MUTE_VIDEO_U_MASK = 0x00FF0000,
  214. CX231xx_MUTE_VIDEO_Y_MASK = 0xFF000000,
  215. };
  216. enum cx231xx_mute_video_shift {
  217. CX231xx_MUTE_VIDEO_V_SHIFT = 8,
  218. CX231xx_MUTE_VIDEO_U_SHIFT = 16,
  219. CX231xx_MUTE_VIDEO_Y_SHIFT = 24,
  220. };
  221. /* defines below are from ivtv-driver.h */
  222. #define IVTV_CMD_HW_BLOCKS_RST 0xFFFFFFFF
  223. /* Firmware API commands */
  224. #define IVTV_API_STD_TIMEOUT 500
  225. /* Registers */
  226. /* IVTV_REG_OFFSET */
  227. #define IVTV_REG_ENC_SDRAM_REFRESH (0x07F8)
  228. #define IVTV_REG_ENC_SDRAM_PRECHARGE (0x07FC)
  229. #define IVTV_REG_SPU (0x9050)
  230. #define IVTV_REG_HW_BLOCKS (0x9054)
  231. #define IVTV_REG_VPU (0x9058)
  232. #define IVTV_REG_APU (0xA064)
  233. /*
  234. * Bit definitions for MC417_RWD and MC417_OEN registers
  235. *
  236. * bits 31-16
  237. *+-----------+
  238. *| Reserved |
  239. *|+-----------+
  240. *| bit 15 bit 14 bit 13 bit 12 bit 11 bit 10 bit 9 bit 8
  241. *|+-------+-------+-------+-------+-------+-------+-------+-------+
  242. *|| MIWR# | MIRD# | MICS# |MIRDY# |MIADDR3|MIADDR2|MIADDR1|MIADDR0|
  243. *|+-------+-------+-------+-------+-------+-------+-------+-------+
  244. *| bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0
  245. *|+-------+-------+-------+-------+-------+-------+-------+-------+
  246. *||MIDATA7|MIDATA6|MIDATA5|MIDATA4|MIDATA3|MIDATA2|MIDATA1|MIDATA0|
  247. *|+-------+-------+-------+-------+-------+-------+-------+-------+
  248. */
  249. #define MC417_MIWR 0x8000
  250. #define MC417_MIRD 0x4000
  251. #define MC417_MICS 0x2000
  252. #define MC417_MIRDY 0x1000
  253. #define MC417_MIADDR 0x0F00
  254. #define MC417_MIDATA 0x00FF
  255. /* Bit definitions for MC417_CTL register ****
  256. *bits 31-6 bits 5-4 bit 3 bits 2-1 Bit 0
  257. *+--------+-------------+--------+--------------+------------+
  258. *|Reserved|MC417_SPD_CTL|Reserved|MC417_GPIO_SEL|UART_GPIO_EN|
  259. *+--------+-------------+--------+--------------+------------+
  260. */
  261. #define MC417_SPD_CTL(x) (((x) << 4) & 0x00000030)
  262. #define MC417_GPIO_SEL(x) (((x) << 1) & 0x00000006)
  263. #define MC417_UART_GPIO_EN 0x00000001
  264. /* Values for speed control */
  265. #define MC417_SPD_CTL_SLOW 0x1
  266. #define MC417_SPD_CTL_MEDIUM 0x0
  267. #define MC417_SPD_CTL_FAST 0x3 /* b'1x, but we use b'11 */
  268. /* Values for GPIO select */
  269. #define MC417_GPIO_SEL_GPIO3 0x3
  270. #define MC417_GPIO_SEL_GPIO2 0x2
  271. #define MC417_GPIO_SEL_GPIO1 0x1
  272. #define MC417_GPIO_SEL_GPIO0 0x0
  273. #define CX23417_GPIO_MASK 0xFC0003FF
  274. static int set_itvc_reg(struct cx231xx *dev, u32 gpio_direction, u32 value)
  275. {
  276. int status = 0;
  277. u32 _gpio_direction = 0;
  278. _gpio_direction = _gpio_direction & CX23417_GPIO_MASK;
  279. _gpio_direction = _gpio_direction | gpio_direction;
  280. status = cx231xx_send_gpio_cmd(dev, _gpio_direction,
  281. (u8 *)&value, 4, 0, 0);
  282. return status;
  283. }
  284. static int get_itvc_reg(struct cx231xx *dev, u32 gpio_direction, u32 *val_ptr)
  285. {
  286. int status = 0;
  287. u32 _gpio_direction = 0;
  288. _gpio_direction = _gpio_direction & CX23417_GPIO_MASK;
  289. _gpio_direction = _gpio_direction | gpio_direction;
  290. status = cx231xx_send_gpio_cmd(dev, _gpio_direction,
  291. (u8 *)val_ptr, 4, 0, 1);
  292. return status;
  293. }
  294. static int wait_for_mci_complete(struct cx231xx *dev)
  295. {
  296. u32 gpio;
  297. u32 gpio_direction = 0;
  298. u8 count = 0;
  299. get_itvc_reg(dev, gpio_direction, &gpio);
  300. while (!(gpio&0x020000)) {
  301. msleep(10);
  302. get_itvc_reg(dev, gpio_direction, &gpio);
  303. if (count++ > 100) {
  304. dprintk(3, "ERROR: Timeout - gpio=%x\n", gpio);
  305. return -EIO;
  306. }
  307. }
  308. return 0;
  309. }
  310. static int mc417_register_write(struct cx231xx *dev, u16 address, u32 value)
  311. {
  312. u32 temp;
  313. int status = 0;
  314. temp = 0x82 | MCI_REGISTER_DATA_BYTE0 | ((value & 0x000000FF) << 8);
  315. temp = temp << 10;
  316. status = set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
  317. if (status < 0)
  318. return status;
  319. temp = temp | (0x05 << 10);
  320. set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
  321. /*write data byte 1;*/
  322. temp = 0x82 | MCI_REGISTER_DATA_BYTE1 | (value & 0x0000FF00);
  323. temp = temp << 10;
  324. set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
  325. temp = temp | (0x05 << 10);
  326. set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
  327. /*write data byte 2;*/
  328. temp = 0x82 | MCI_REGISTER_DATA_BYTE2 | ((value & 0x00FF0000) >> 8);
  329. temp = temp << 10;
  330. set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
  331. temp = temp | (0x05 << 10);
  332. set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
  333. /*write data byte 3;*/
  334. temp = 0x82 | MCI_REGISTER_DATA_BYTE3 | ((value & 0xFF000000) >> 16);
  335. temp = temp << 10;
  336. set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
  337. temp = temp | (0x05 << 10);
  338. set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
  339. /*write address byte 0;*/
  340. temp = 0x82 | MCI_REGISTER_ADDRESS_BYTE0 | ((address & 0x000000FF) << 8);
  341. temp = temp << 10;
  342. set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
  343. temp = temp | (0x05 << 10);
  344. set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
  345. /*write address byte 1;*/
  346. temp = 0x82 | MCI_REGISTER_ADDRESS_BYTE1 | (address & 0x0000FF00);
  347. temp = temp << 10;
  348. set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
  349. temp = temp | (0x05 << 10);
  350. set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
  351. /*Write that the mode is write.*/
  352. temp = 0x82 | MCI_REGISTER_MODE | MCI_MODE_REGISTER_WRITE;
  353. temp = temp << 10;
  354. set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
  355. temp = temp | (0x05 << 10);
  356. set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
  357. return wait_for_mci_complete(dev);
  358. }
  359. static int mc417_register_read(struct cx231xx *dev, u16 address, u32 *value)
  360. {
  361. /*write address byte 0;*/
  362. u32 temp;
  363. u32 return_value = 0;
  364. int ret = 0;
  365. temp = 0x82 | MCI_REGISTER_ADDRESS_BYTE0 | ((address & 0x00FF) << 8);
  366. temp = temp << 10;
  367. set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
  368. temp = temp | ((0x05) << 10);
  369. set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
  370. /*write address byte 1;*/
  371. temp = 0x82 | MCI_REGISTER_ADDRESS_BYTE1 | (address & 0xFF00);
  372. temp = temp << 10;
  373. set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
  374. temp = temp | ((0x05) << 10);
  375. set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
  376. /*write that the mode is read;*/
  377. temp = 0x82 | MCI_REGISTER_MODE | MCI_MODE_REGISTER_READ;
  378. temp = temp << 10;
  379. set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
  380. temp = temp | ((0x05) << 10);
  381. set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
  382. /*wait for the MIRDY line to be asserted ,
  383. signalling that the read is done;*/
  384. ret = wait_for_mci_complete(dev);
  385. /*switch the DATA- GPIO to input mode;*/
  386. /*Read data byte 0;*/
  387. temp = (0x82 | MCI_REGISTER_DATA_BYTE0) << 10;
  388. set_itvc_reg(dev, ITVC_READ_DIR, temp);
  389. temp = ((0x81 | MCI_REGISTER_DATA_BYTE0) << 10);
  390. set_itvc_reg(dev, ITVC_READ_DIR, temp);
  391. get_itvc_reg(dev, ITVC_READ_DIR, &temp);
  392. return_value |= ((temp & 0x03FC0000) >> 18);
  393. set_itvc_reg(dev, ITVC_READ_DIR, (0x87 << 10));
  394. /* Read data byte 1;*/
  395. temp = (0x82 | MCI_REGISTER_DATA_BYTE1) << 10;
  396. set_itvc_reg(dev, ITVC_READ_DIR, temp);
  397. temp = ((0x81 | MCI_REGISTER_DATA_BYTE1) << 10);
  398. set_itvc_reg(dev, ITVC_READ_DIR, temp);
  399. get_itvc_reg(dev, ITVC_READ_DIR, &temp);
  400. return_value |= ((temp & 0x03FC0000) >> 10);
  401. set_itvc_reg(dev, ITVC_READ_DIR, (0x87 << 10));
  402. /*Read data byte 2;*/
  403. temp = (0x82 | MCI_REGISTER_DATA_BYTE2) << 10;
  404. set_itvc_reg(dev, ITVC_READ_DIR, temp);
  405. temp = ((0x81 | MCI_REGISTER_DATA_BYTE2) << 10);
  406. set_itvc_reg(dev, ITVC_READ_DIR, temp);
  407. get_itvc_reg(dev, ITVC_READ_DIR, &temp);
  408. return_value |= ((temp & 0x03FC0000) >> 2);
  409. set_itvc_reg(dev, ITVC_READ_DIR, (0x87 << 10));
  410. /*Read data byte 3;*/
  411. temp = (0x82 | MCI_REGISTER_DATA_BYTE3) << 10;
  412. set_itvc_reg(dev, ITVC_READ_DIR, temp);
  413. temp = ((0x81 | MCI_REGISTER_DATA_BYTE3) << 10);
  414. set_itvc_reg(dev, ITVC_READ_DIR, temp);
  415. get_itvc_reg(dev, ITVC_READ_DIR, &temp);
  416. return_value |= ((temp & 0x03FC0000) << 6);
  417. set_itvc_reg(dev, ITVC_READ_DIR, (0x87 << 10));
  418. *value = return_value;
  419. return ret;
  420. }
  421. static int mc417_memory_write(struct cx231xx *dev, u32 address, u32 value)
  422. {
  423. /*write data byte 0;*/
  424. u32 temp;
  425. int ret = 0;
  426. temp = 0x82 | MCI_MEMORY_DATA_BYTE0 | ((value & 0x000000FF) << 8);
  427. temp = temp << 10;
  428. ret = set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
  429. if (ret < 0)
  430. return ret;
  431. temp = temp | (0x05 << 10);
  432. set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
  433. /*write data byte 1;*/
  434. temp = 0x82 | MCI_MEMORY_DATA_BYTE1 | (value & 0x0000FF00);
  435. temp = temp << 10;
  436. set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
  437. temp = temp | (0x05 << 10);
  438. set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
  439. /*write data byte 2;*/
  440. temp = 0x82 | MCI_MEMORY_DATA_BYTE2 | ((value & 0x00FF0000) >> 8);
  441. temp = temp << 10;
  442. set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
  443. temp = temp | (0x05 << 10);
  444. set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
  445. /*write data byte 3;*/
  446. temp = 0x82 | MCI_MEMORY_DATA_BYTE3 | ((value & 0xFF000000) >> 16);
  447. temp = temp << 10;
  448. set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
  449. temp = temp | (0x05 << 10);
  450. set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
  451. /* write address byte 2;*/
  452. temp = 0x82 | MCI_MEMORY_ADDRESS_BYTE2 | MCI_MODE_MEMORY_WRITE |
  453. ((address & 0x003F0000) >> 8);
  454. temp = temp << 10;
  455. set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
  456. temp = temp | (0x05 << 10);
  457. set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
  458. /* write address byte 1;*/
  459. temp = 0x82 | MCI_MEMORY_ADDRESS_BYTE1 | (address & 0xFF00);
  460. temp = temp << 10;
  461. set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
  462. temp = temp | (0x05 << 10);
  463. set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
  464. /* write address byte 0;*/
  465. temp = 0x82 | MCI_MEMORY_ADDRESS_BYTE0 | ((address & 0x00FF) << 8);
  466. temp = temp << 10;
  467. set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
  468. temp = temp | (0x05 << 10);
  469. set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
  470. /*wait for MIRDY line;*/
  471. wait_for_mci_complete(dev);
  472. return 0;
  473. }
  474. static int mc417_memory_read(struct cx231xx *dev, u32 address, u32 *value)
  475. {
  476. u32 temp = 0;
  477. u32 return_value = 0;
  478. int ret = 0;
  479. /*write address byte 2;*/
  480. temp = 0x82 | MCI_MEMORY_ADDRESS_BYTE2 | MCI_MODE_MEMORY_READ |
  481. ((address & 0x003F0000) >> 8);
  482. temp = temp << 10;
  483. ret = set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
  484. if (ret < 0)
  485. return ret;
  486. temp = temp | (0x05 << 10);
  487. set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
  488. /*write address byte 1*/
  489. temp = 0x82 | MCI_MEMORY_ADDRESS_BYTE1 | (address & 0xFF00);
  490. temp = temp << 10;
  491. set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
  492. temp = temp | (0x05 << 10);
  493. set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
  494. /*write address byte 0*/
  495. temp = 0x82 | MCI_MEMORY_ADDRESS_BYTE0 | ((address & 0x00FF) << 8);
  496. temp = temp << 10;
  497. set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
  498. temp = temp | (0x05 << 10);
  499. set_itvc_reg(dev, ITVC_WRITE_DIR, temp);
  500. /*Wait for MIRDY line*/
  501. ret = wait_for_mci_complete(dev);
  502. /*Read data byte 3;*/
  503. temp = (0x82 | MCI_MEMORY_DATA_BYTE3) << 10;
  504. set_itvc_reg(dev, ITVC_READ_DIR, temp);
  505. temp = ((0x81 | MCI_MEMORY_DATA_BYTE3) << 10);
  506. set_itvc_reg(dev, ITVC_READ_DIR, temp);
  507. get_itvc_reg(dev, ITVC_READ_DIR, &temp);
  508. return_value |= ((temp & 0x03FC0000) << 6);
  509. set_itvc_reg(dev, ITVC_READ_DIR, (0x87 << 10));
  510. /*Read data byte 2;*/
  511. temp = (0x82 | MCI_MEMORY_DATA_BYTE2) << 10;
  512. set_itvc_reg(dev, ITVC_READ_DIR, temp);
  513. temp = ((0x81 | MCI_MEMORY_DATA_BYTE2) << 10);
  514. set_itvc_reg(dev, ITVC_READ_DIR, temp);
  515. get_itvc_reg(dev, ITVC_READ_DIR, &temp);
  516. return_value |= ((temp & 0x03FC0000) >> 2);
  517. set_itvc_reg(dev, ITVC_READ_DIR, (0x87 << 10));
  518. /* Read data byte 1;*/
  519. temp = (0x82 | MCI_MEMORY_DATA_BYTE1) << 10;
  520. set_itvc_reg(dev, ITVC_READ_DIR, temp);
  521. temp = ((0x81 | MCI_MEMORY_DATA_BYTE1) << 10);
  522. set_itvc_reg(dev, ITVC_READ_DIR, temp);
  523. get_itvc_reg(dev, ITVC_READ_DIR, &temp);
  524. return_value |= ((temp & 0x03FC0000) >> 10);
  525. set_itvc_reg(dev, ITVC_READ_DIR, (0x87 << 10));
  526. /*Read data byte 0;*/
  527. temp = (0x82 | MCI_MEMORY_DATA_BYTE0) << 10;
  528. set_itvc_reg(dev, ITVC_READ_DIR, temp);
  529. temp = ((0x81 | MCI_MEMORY_DATA_BYTE0) << 10);
  530. set_itvc_reg(dev, ITVC_READ_DIR, temp);
  531. get_itvc_reg(dev, ITVC_READ_DIR, &temp);
  532. return_value |= ((temp & 0x03FC0000) >> 18);
  533. set_itvc_reg(dev, ITVC_READ_DIR, (0x87 << 10));
  534. *value = return_value;
  535. return ret;
  536. }
  537. /* ------------------------------------------------------------------ */
  538. /* MPEG encoder API */
  539. static char *cmd_to_str(int cmd)
  540. {
  541. switch (cmd) {
  542. case CX2341X_ENC_PING_FW:
  543. return "PING_FW";
  544. case CX2341X_ENC_START_CAPTURE:
  545. return "START_CAPTURE";
  546. case CX2341X_ENC_STOP_CAPTURE:
  547. return "STOP_CAPTURE";
  548. case CX2341X_ENC_SET_AUDIO_ID:
  549. return "SET_AUDIO_ID";
  550. case CX2341X_ENC_SET_VIDEO_ID:
  551. return "SET_VIDEO_ID";
  552. case CX2341X_ENC_SET_PCR_ID:
  553. return "SET_PCR_PID";
  554. case CX2341X_ENC_SET_FRAME_RATE:
  555. return "SET_FRAME_RATE";
  556. case CX2341X_ENC_SET_FRAME_SIZE:
  557. return "SET_FRAME_SIZE";
  558. case CX2341X_ENC_SET_BIT_RATE:
  559. return "SET_BIT_RATE";
  560. case CX2341X_ENC_SET_GOP_PROPERTIES:
  561. return "SET_GOP_PROPERTIES";
  562. case CX2341X_ENC_SET_ASPECT_RATIO:
  563. return "SET_ASPECT_RATIO";
  564. case CX2341X_ENC_SET_DNR_FILTER_MODE:
  565. return "SET_DNR_FILTER_PROPS";
  566. case CX2341X_ENC_SET_DNR_FILTER_PROPS:
  567. return "SET_DNR_FILTER_PROPS";
  568. case CX2341X_ENC_SET_CORING_LEVELS:
  569. return "SET_CORING_LEVELS";
  570. case CX2341X_ENC_SET_SPATIAL_FILTER_TYPE:
  571. return "SET_SPATIAL_FILTER_TYPE";
  572. case CX2341X_ENC_SET_VBI_LINE:
  573. return "SET_VBI_LINE";
  574. case CX2341X_ENC_SET_STREAM_TYPE:
  575. return "SET_STREAM_TYPE";
  576. case CX2341X_ENC_SET_OUTPUT_PORT:
  577. return "SET_OUTPUT_PORT";
  578. case CX2341X_ENC_SET_AUDIO_PROPERTIES:
  579. return "SET_AUDIO_PROPERTIES";
  580. case CX2341X_ENC_HALT_FW:
  581. return "HALT_FW";
  582. case CX2341X_ENC_GET_VERSION:
  583. return "GET_VERSION";
  584. case CX2341X_ENC_SET_GOP_CLOSURE:
  585. return "SET_GOP_CLOSURE";
  586. case CX2341X_ENC_GET_SEQ_END:
  587. return "GET_SEQ_END";
  588. case CX2341X_ENC_SET_PGM_INDEX_INFO:
  589. return "SET_PGM_INDEX_INFO";
  590. case CX2341X_ENC_SET_VBI_CONFIG:
  591. return "SET_VBI_CONFIG";
  592. case CX2341X_ENC_SET_DMA_BLOCK_SIZE:
  593. return "SET_DMA_BLOCK_SIZE";
  594. case CX2341X_ENC_GET_PREV_DMA_INFO_MB_10:
  595. return "GET_PREV_DMA_INFO_MB_10";
  596. case CX2341X_ENC_GET_PREV_DMA_INFO_MB_9:
  597. return "GET_PREV_DMA_INFO_MB_9";
  598. case CX2341X_ENC_SCHED_DMA_TO_HOST:
  599. return "SCHED_DMA_TO_HOST";
  600. case CX2341X_ENC_INITIALIZE_INPUT:
  601. return "INITIALIZE_INPUT";
  602. case CX2341X_ENC_SET_FRAME_DROP_RATE:
  603. return "SET_FRAME_DROP_RATE";
  604. case CX2341X_ENC_PAUSE_ENCODER:
  605. return "PAUSE_ENCODER";
  606. case CX2341X_ENC_REFRESH_INPUT:
  607. return "REFRESH_INPUT";
  608. case CX2341X_ENC_SET_COPYRIGHT:
  609. return "SET_COPYRIGHT";
  610. case CX2341X_ENC_SET_EVENT_NOTIFICATION:
  611. return "SET_EVENT_NOTIFICATION";
  612. case CX2341X_ENC_SET_NUM_VSYNC_LINES:
  613. return "SET_NUM_VSYNC_LINES";
  614. case CX2341X_ENC_SET_PLACEHOLDER:
  615. return "SET_PLACEHOLDER";
  616. case CX2341X_ENC_MUTE_VIDEO:
  617. return "MUTE_VIDEO";
  618. case CX2341X_ENC_MUTE_AUDIO:
  619. return "MUTE_AUDIO";
  620. case CX2341X_ENC_MISC:
  621. return "MISC";
  622. default:
  623. return "UNKNOWN";
  624. }
  625. }
  626. static int cx231xx_mbox_func(void *priv, u32 command, int in, int out,
  627. u32 data[CX2341X_MBOX_MAX_DATA])
  628. {
  629. struct cx231xx *dev = priv;
  630. unsigned long timeout;
  631. u32 value, flag, retval = 0;
  632. int i;
  633. dprintk(3, "%s: command(0x%X) = %s\n", __func__, command,
  634. cmd_to_str(command));
  635. /* this may not be 100% safe if we can't read any memory location
  636. without side effects */
  637. mc417_memory_read(dev, dev->cx23417_mailbox - 4, &value);
  638. if (value != 0x12345678) {
  639. dprintk(3, "Firmware and/or mailbox pointer not initialized or corrupted, signature = 0x%x, cmd = %s\n",
  640. value, cmd_to_str(command));
  641. return -EIO;
  642. }
  643. /* This read looks at 32 bits, but flag is only 8 bits.
  644. * Seems we also bail if CMD or TIMEOUT bytes are set???
  645. */
  646. mc417_memory_read(dev, dev->cx23417_mailbox, &flag);
  647. if (flag) {
  648. dprintk(3, "ERROR: Mailbox appears to be in use (%x), cmd = %s\n",
  649. flag, cmd_to_str(command));
  650. return -EBUSY;
  651. }
  652. flag |= 1; /* tell 'em we're working on it */
  653. mc417_memory_write(dev, dev->cx23417_mailbox, flag);
  654. /* write command + args + fill remaining with zeros */
  655. /* command code */
  656. mc417_memory_write(dev, dev->cx23417_mailbox + 1, command);
  657. mc417_memory_write(dev, dev->cx23417_mailbox + 3,
  658. IVTV_API_STD_TIMEOUT); /* timeout */
  659. for (i = 0; i < in; i++) {
  660. mc417_memory_write(dev, dev->cx23417_mailbox + 4 + i, data[i]);
  661. dprintk(3, "API Input %d = %d\n", i, data[i]);
  662. }
  663. for (; i < CX2341X_MBOX_MAX_DATA; i++)
  664. mc417_memory_write(dev, dev->cx23417_mailbox + 4 + i, 0);
  665. flag |= 3; /* tell 'em we're done writing */
  666. mc417_memory_write(dev, dev->cx23417_mailbox, flag);
  667. /* wait for firmware to handle the API command */
  668. timeout = jiffies + msecs_to_jiffies(10);
  669. for (;;) {
  670. mc417_memory_read(dev, dev->cx23417_mailbox, &flag);
  671. if (0 != (flag & 4))
  672. break;
  673. if (time_after(jiffies, timeout)) {
  674. dprintk(3, "ERROR: API Mailbox timeout\n");
  675. return -EIO;
  676. }
  677. udelay(10);
  678. }
  679. /* read output values */
  680. for (i = 0; i < out; i++) {
  681. mc417_memory_read(dev, dev->cx23417_mailbox + 4 + i, data + i);
  682. dprintk(3, "API Output %d = %d\n", i, data[i]);
  683. }
  684. mc417_memory_read(dev, dev->cx23417_mailbox + 2, &retval);
  685. dprintk(3, "API result = %d\n", retval);
  686. flag = 0;
  687. mc417_memory_write(dev, dev->cx23417_mailbox, flag);
  688. return 0;
  689. }
  690. /* We don't need to call the API often, so using just one
  691. * mailbox will probably suffice
  692. */
  693. static int cx231xx_api_cmd(struct cx231xx *dev, u32 command,
  694. u32 inputcnt, u32 outputcnt, ...)
  695. {
  696. u32 data[CX2341X_MBOX_MAX_DATA];
  697. va_list vargs;
  698. int i, err;
  699. dprintk(3, "%s() cmds = 0x%08x\n", __func__, command);
  700. va_start(vargs, outputcnt);
  701. for (i = 0; i < inputcnt; i++)
  702. data[i] = va_arg(vargs, int);
  703. err = cx231xx_mbox_func(dev, command, inputcnt, outputcnt, data);
  704. for (i = 0; i < outputcnt; i++) {
  705. int *vptr = va_arg(vargs, int *);
  706. *vptr = data[i];
  707. }
  708. va_end(vargs);
  709. return err;
  710. }
  711. static int cx231xx_find_mailbox(struct cx231xx *dev)
  712. {
  713. u32 signature[4] = {
  714. 0x12345678, 0x34567812, 0x56781234, 0x78123456
  715. };
  716. int signaturecnt = 0;
  717. u32 value;
  718. int i;
  719. int ret = 0;
  720. dprintk(2, "%s()\n", __func__);
  721. for (i = 0; i < 0x100; i++) {/*CX231xx_FIRM_IMAGE_SIZE*/
  722. ret = mc417_memory_read(dev, i, &value);
  723. if (ret < 0)
  724. return ret;
  725. if (value == signature[signaturecnt])
  726. signaturecnt++;
  727. else
  728. signaturecnt = 0;
  729. if (4 == signaturecnt) {
  730. dprintk(1, "Mailbox signature found at 0x%x\n", i + 1);
  731. return i + 1;
  732. }
  733. }
  734. dprintk(3, "Mailbox signature values not found!\n");
  735. return -EIO;
  736. }
  737. static void mci_write_memory_to_gpio(struct cx231xx *dev, u32 address, u32 value,
  738. u32 *p_fw_image)
  739. {
  740. u32 temp = 0;
  741. int i = 0;
  742. temp = 0x82 | MCI_MEMORY_DATA_BYTE0 | ((value & 0x000000FF) << 8);
  743. temp = temp << 10;
  744. *p_fw_image = temp;
  745. p_fw_image++;
  746. temp = temp | (0x05 << 10);
  747. *p_fw_image = temp;
  748. p_fw_image++;
  749. /*write data byte 1;*/
  750. temp = 0x82 | MCI_MEMORY_DATA_BYTE1 | (value & 0x0000FF00);
  751. temp = temp << 10;
  752. *p_fw_image = temp;
  753. p_fw_image++;
  754. temp = temp | (0x05 << 10);
  755. *p_fw_image = temp;
  756. p_fw_image++;
  757. /*write data byte 2;*/
  758. temp = 0x82 | MCI_MEMORY_DATA_BYTE2 | ((value & 0x00FF0000) >> 8);
  759. temp = temp << 10;
  760. *p_fw_image = temp;
  761. p_fw_image++;
  762. temp = temp | (0x05 << 10);
  763. *p_fw_image = temp;
  764. p_fw_image++;
  765. /*write data byte 3;*/
  766. temp = 0x82 | MCI_MEMORY_DATA_BYTE3 | ((value & 0xFF000000) >> 16);
  767. temp = temp << 10;
  768. *p_fw_image = temp;
  769. p_fw_image++;
  770. temp = temp | (0x05 << 10);
  771. *p_fw_image = temp;
  772. p_fw_image++;
  773. /* write address byte 2;*/
  774. temp = 0x82 | MCI_MEMORY_ADDRESS_BYTE2 | MCI_MODE_MEMORY_WRITE |
  775. ((address & 0x003F0000) >> 8);
  776. temp = temp << 10;
  777. *p_fw_image = temp;
  778. p_fw_image++;
  779. temp = temp | (0x05 << 10);
  780. *p_fw_image = temp;
  781. p_fw_image++;
  782. /* write address byte 1;*/
  783. temp = 0x82 | MCI_MEMORY_ADDRESS_BYTE1 | (address & 0xFF00);
  784. temp = temp << 10;
  785. *p_fw_image = temp;
  786. p_fw_image++;
  787. temp = temp | (0x05 << 10);
  788. *p_fw_image = temp;
  789. p_fw_image++;
  790. /* write address byte 0;*/
  791. temp = 0x82 | MCI_MEMORY_ADDRESS_BYTE0 | ((address & 0x00FF) << 8);
  792. temp = temp << 10;
  793. *p_fw_image = temp;
  794. p_fw_image++;
  795. temp = temp | (0x05 << 10);
  796. *p_fw_image = temp;
  797. p_fw_image++;
  798. for (i = 0; i < 6; i++) {
  799. *p_fw_image = 0xFFFFFFFF;
  800. p_fw_image++;
  801. }
  802. }
  803. static int cx231xx_load_firmware(struct cx231xx *dev)
  804. {
  805. static const unsigned char magic[8] = {
  806. 0xa7, 0x0d, 0x00, 0x00, 0x66, 0xbb, 0x55, 0xaa
  807. };
  808. const struct firmware *firmware;
  809. int i, retval = 0;
  810. u32 value = 0;
  811. u32 gpio_output = 0;
  812. /*u32 checksum = 0;*/
  813. /*u32 *dataptr;*/
  814. u32 transfer_size = 0;
  815. u32 fw_data = 0;
  816. u32 address = 0;
  817. /*u32 current_fw[800];*/
  818. u32 *p_current_fw, *p_fw;
  819. u32 *p_fw_data;
  820. int frame = 0;
  821. u16 _buffer_size = 4096;
  822. u8 *p_buffer;
  823. p_current_fw = vmalloc(1884180 * 4);
  824. p_fw = p_current_fw;
  825. if (p_current_fw == NULL) {
  826. dprintk(2, "FAIL!!!\n");
  827. return -ENOMEM;
  828. }
  829. p_buffer = vmalloc(4096);
  830. if (p_buffer == NULL) {
  831. dprintk(2, "FAIL!!!\n");
  832. vfree(p_current_fw);
  833. return -ENOMEM;
  834. }
  835. dprintk(2, "%s()\n", __func__);
  836. /* Save GPIO settings before reset of APU */
  837. retval |= mc417_memory_read(dev, 0x9020, &gpio_output);
  838. retval |= mc417_memory_read(dev, 0x900C, &value);
  839. retval = mc417_register_write(dev,
  840. IVTV_REG_VPU, 0xFFFFFFED);
  841. retval |= mc417_register_write(dev,
  842. IVTV_REG_HW_BLOCKS, IVTV_CMD_HW_BLOCKS_RST);
  843. retval |= mc417_register_write(dev,
  844. IVTV_REG_ENC_SDRAM_REFRESH, 0x80000800);
  845. retval |= mc417_register_write(dev,
  846. IVTV_REG_ENC_SDRAM_PRECHARGE, 0x1A);
  847. retval |= mc417_register_write(dev,
  848. IVTV_REG_APU, 0);
  849. if (retval != 0) {
  850. dev_err(dev->dev,
  851. "%s: Error with mc417_register_write\n", __func__);
  852. vfree(p_current_fw);
  853. vfree(p_buffer);
  854. return retval;
  855. }
  856. retval = reject_firmware(&firmware, CX231xx_FIRM_IMAGE_NAME,
  857. dev->dev);
  858. if (retval != 0) {
  859. dev_err(dev->dev,
  860. "ERROR: Hotplug firmware request failed (%s).\n",
  861. CX231xx_FIRM_IMAGE_NAME);
  862. dev_err(dev->dev,
  863. "Please fix your hotplug setup, the board will not work without firmware loaded!\n");
  864. vfree(p_current_fw);
  865. vfree(p_buffer);
  866. return retval;
  867. }
  868. if (firmware->size != CX231xx_FIRM_IMAGE_SIZE) {
  869. dev_err(dev->dev,
  870. "ERROR: Firmware size mismatch (have %zd, expected %d)\n",
  871. firmware->size, CX231xx_FIRM_IMAGE_SIZE);
  872. release_firmware(firmware);
  873. vfree(p_current_fw);
  874. vfree(p_buffer);
  875. return -EINVAL;
  876. }
  877. if (0 != memcmp(firmware->data, magic, 8)) {
  878. dev_err(dev->dev,
  879. "ERROR: Firmware magic mismatch, wrong file?\n");
  880. release_firmware(firmware);
  881. vfree(p_current_fw);
  882. vfree(p_buffer);
  883. return -EINVAL;
  884. }
  885. initGPIO(dev);
  886. /* transfer to the chip */
  887. dprintk(2, "Loading firmware to GPIO...\n");
  888. p_fw_data = (u32 *)firmware->data;
  889. dprintk(2, "firmware->size=%zd\n", firmware->size);
  890. for (transfer_size = 0; transfer_size < firmware->size;
  891. transfer_size += 4) {
  892. fw_data = *p_fw_data;
  893. mci_write_memory_to_gpio(dev, address, fw_data, p_current_fw);
  894. address = address + 1;
  895. p_current_fw += 20;
  896. p_fw_data += 1;
  897. }
  898. /*download the firmware by ep5-out*/
  899. for (frame = 0; frame < (int)(CX231xx_FIRM_IMAGE_SIZE*20/_buffer_size);
  900. frame++) {
  901. for (i = 0; i < _buffer_size; i++) {
  902. *(p_buffer + i) = (u8)(*(p_fw + (frame * 128 * 8 + (i / 4))) & 0x000000FF);
  903. i++;
  904. *(p_buffer + i) = (u8)((*(p_fw + (frame * 128 * 8 + (i / 4))) & 0x0000FF00) >> 8);
  905. i++;
  906. *(p_buffer + i) = (u8)((*(p_fw + (frame * 128 * 8 + (i / 4))) & 0x00FF0000) >> 16);
  907. i++;
  908. *(p_buffer + i) = (u8)((*(p_fw + (frame * 128 * 8 + (i / 4))) & 0xFF000000) >> 24);
  909. }
  910. cx231xx_ep5_bulkout(dev, p_buffer, _buffer_size);
  911. }
  912. p_current_fw = p_fw;
  913. vfree(p_current_fw);
  914. p_current_fw = NULL;
  915. uninitGPIO(dev);
  916. release_firmware(firmware);
  917. dprintk(1, "Firmware upload successful.\n");
  918. retval |= mc417_register_write(dev, IVTV_REG_HW_BLOCKS,
  919. IVTV_CMD_HW_BLOCKS_RST);
  920. if (retval < 0) {
  921. dev_err(dev->dev,
  922. "%s: Error with mc417_register_write\n",
  923. __func__);
  924. return retval;
  925. }
  926. /* F/W power up disturbs the GPIOs, restore state */
  927. retval |= mc417_register_write(dev, 0x9020, gpio_output);
  928. retval |= mc417_register_write(dev, 0x900C, value);
  929. retval |= mc417_register_read(dev, IVTV_REG_VPU, &value);
  930. retval |= mc417_register_write(dev, IVTV_REG_VPU, value & 0xFFFFFFE8);
  931. if (retval < 0) {
  932. dev_err(dev->dev,
  933. "%s: Error with mc417_register_write\n",
  934. __func__);
  935. return retval;
  936. }
  937. return 0;
  938. }
  939. static void cx231xx_417_check_encoder(struct cx231xx *dev)
  940. {
  941. u32 status, seq;
  942. status = 0;
  943. seq = 0;
  944. cx231xx_api_cmd(dev, CX2341X_ENC_GET_SEQ_END, 0, 2, &status, &seq);
  945. dprintk(1, "%s() status = %d, seq = %d\n", __func__, status, seq);
  946. }
  947. static void cx231xx_codec_settings(struct cx231xx *dev)
  948. {
  949. dprintk(1, "%s()\n", __func__);
  950. /* assign frame size */
  951. cx231xx_api_cmd(dev, CX2341X_ENC_SET_FRAME_SIZE, 2, 0,
  952. dev->ts1.height, dev->ts1.width);
  953. dev->mpeg_ctrl_handler.width = dev->ts1.width;
  954. dev->mpeg_ctrl_handler.height = dev->ts1.height;
  955. cx2341x_handler_setup(&dev->mpeg_ctrl_handler);
  956. cx231xx_api_cmd(dev, CX2341X_ENC_MISC, 2, 0, 3, 1);
  957. cx231xx_api_cmd(dev, CX2341X_ENC_MISC, 2, 0, 4, 1);
  958. }
  959. static int cx231xx_initialize_codec(struct cx231xx *dev)
  960. {
  961. int version;
  962. int retval;
  963. u32 i;
  964. u32 val = 0;
  965. dprintk(1, "%s()\n", __func__);
  966. cx231xx_disable656(dev);
  967. retval = cx231xx_api_cmd(dev, CX2341X_ENC_PING_FW, 0, 0); /* ping */
  968. if (retval < 0) {
  969. dprintk(2, "%s: PING OK\n", __func__);
  970. retval = cx231xx_load_firmware(dev);
  971. if (retval < 0) {
  972. dev_err(dev->dev,
  973. "%s: f/w load failed\n", __func__);
  974. return retval;
  975. }
  976. retval = cx231xx_find_mailbox(dev);
  977. if (retval < 0) {
  978. dev_err(dev->dev, "%s: mailbox < 0, error\n",
  979. __func__);
  980. return retval;
  981. }
  982. dev->cx23417_mailbox = retval;
  983. retval = cx231xx_api_cmd(dev, CX2341X_ENC_PING_FW, 0, 0);
  984. if (retval < 0) {
  985. dev_err(dev->dev,
  986. "ERROR: cx23417 firmware ping failed!\n");
  987. return retval;
  988. }
  989. retval = cx231xx_api_cmd(dev, CX2341X_ENC_GET_VERSION, 0, 1,
  990. &version);
  991. if (retval < 0) {
  992. dev_err(dev->dev,
  993. "ERROR: cx23417 firmware get encoder: version failed!\n");
  994. return retval;
  995. }
  996. dprintk(1, "cx23417 firmware version is 0x%08x\n", version);
  997. msleep(200);
  998. }
  999. for (i = 0; i < 1; i++) {
  1000. retval = mc417_register_read(dev, 0x20f8, &val);
  1001. dprintk(3, "***before enable656() VIM Capture Lines = %d ***\n",
  1002. val);
  1003. if (retval < 0)
  1004. return retval;
  1005. }
  1006. cx231xx_enable656(dev);
  1007. /* stop mpeg capture */
  1008. cx231xx_api_cmd(dev, CX2341X_ENC_STOP_CAPTURE, 3, 0, 1, 3, 4);
  1009. cx231xx_codec_settings(dev);
  1010. msleep(60);
  1011. /* cx231xx_api_cmd(dev, CX2341X_ENC_SET_NUM_VSYNC_LINES, 2, 0,
  1012. CX231xx_FIELD1_SAA7115, CX231xx_FIELD2_SAA7115);
  1013. cx231xx_api_cmd(dev, CX2341X_ENC_SET_PLACEHOLDER, 12, 0,
  1014. CX231xx_CUSTOM_EXTENSION_USR_DATA, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1015. 0, 0);
  1016. */
  1017. #if 0
  1018. /* TODO */
  1019. u32 data[7];
  1020. /* Setup to capture VBI */
  1021. data[0] = 0x0001BD00;
  1022. data[1] = 1; /* frames per interrupt */
  1023. data[2] = 4; /* total bufs */
  1024. data[3] = 0x91559155; /* start codes */
  1025. data[4] = 0x206080C0; /* stop codes */
  1026. data[5] = 6; /* lines */
  1027. data[6] = 64; /* BPL */
  1028. cx231xx_api_cmd(dev, CX2341X_ENC_SET_VBI_CONFIG, 7, 0, data[0], data[1],
  1029. data[2], data[3], data[4], data[5], data[6]);
  1030. for (i = 2; i <= 24; i++) {
  1031. int valid;
  1032. valid = ((i >= 19) && (i <= 21));
  1033. cx231xx_api_cmd(dev, CX2341X_ENC_SET_VBI_LINE, 5, 0, i,
  1034. valid, 0 , 0, 0);
  1035. cx231xx_api_cmd(dev, CX2341X_ENC_SET_VBI_LINE, 5, 0,
  1036. i | 0x80000000, valid, 0, 0, 0);
  1037. }
  1038. #endif
  1039. /* cx231xx_api_cmd(dev, CX2341X_ENC_MUTE_AUDIO, 1, 0, CX231xx_UNMUTE);
  1040. msleep(60);
  1041. */
  1042. /* initialize the video input */
  1043. retval = cx231xx_api_cmd(dev, CX2341X_ENC_INITIALIZE_INPUT, 0, 0);
  1044. if (retval < 0)
  1045. return retval;
  1046. msleep(60);
  1047. /* Enable VIP style pixel invalidation so we work with scaled mode */
  1048. mc417_memory_write(dev, 2120, 0x00000080);
  1049. /* start capturing to the host interface */
  1050. retval = cx231xx_api_cmd(dev, CX2341X_ENC_START_CAPTURE, 2, 0,
  1051. CX231xx_MPEG_CAPTURE, CX231xx_RAW_BITS_NONE);
  1052. if (retval < 0)
  1053. return retval;
  1054. msleep(10);
  1055. for (i = 0; i < 1; i++) {
  1056. mc417_register_read(dev, 0x20f8, &val);
  1057. dprintk(3, "***VIM Capture Lines =%d ***\n", val);
  1058. }
  1059. return 0;
  1060. }
  1061. /* ------------------------------------------------------------------ */
  1062. static int bb_buf_setup(struct videobuf_queue *q,
  1063. unsigned int *count, unsigned int *size)
  1064. {
  1065. struct cx231xx_fh *fh = q->priv_data;
  1066. fh->dev->ts1.ts_packet_size = mpeglinesize;
  1067. fh->dev->ts1.ts_packet_count = mpeglines;
  1068. *size = fh->dev->ts1.ts_packet_size * fh->dev->ts1.ts_packet_count;
  1069. *count = mpegbufs;
  1070. return 0;
  1071. }
  1072. static void free_buffer(struct videobuf_queue *vq, struct cx231xx_buffer *buf)
  1073. {
  1074. struct cx231xx_fh *fh = vq->priv_data;
  1075. struct cx231xx *dev = fh->dev;
  1076. unsigned long flags = 0;
  1077. BUG_ON(in_interrupt());
  1078. spin_lock_irqsave(&dev->video_mode.slock, flags);
  1079. if (dev->USE_ISO) {
  1080. if (dev->video_mode.isoc_ctl.buf == buf)
  1081. dev->video_mode.isoc_ctl.buf = NULL;
  1082. } else {
  1083. if (dev->video_mode.bulk_ctl.buf == buf)
  1084. dev->video_mode.bulk_ctl.buf = NULL;
  1085. }
  1086. spin_unlock_irqrestore(&dev->video_mode.slock, flags);
  1087. videobuf_waiton(vq, &buf->vb, 0, 0);
  1088. videobuf_vmalloc_free(&buf->vb);
  1089. buf->vb.state = VIDEOBUF_NEEDS_INIT;
  1090. }
  1091. static void buffer_copy(struct cx231xx *dev, char *data, int len, struct urb *urb,
  1092. struct cx231xx_dmaqueue *dma_q)
  1093. {
  1094. void *vbuf;
  1095. struct cx231xx_buffer *buf;
  1096. u32 tail_data = 0;
  1097. char *p_data;
  1098. if (dma_q->mpeg_buffer_done == 0) {
  1099. if (list_empty(&dma_q->active))
  1100. return;
  1101. buf = list_entry(dma_q->active.next,
  1102. struct cx231xx_buffer, vb.queue);
  1103. dev->video_mode.isoc_ctl.buf = buf;
  1104. dma_q->mpeg_buffer_done = 1;
  1105. }
  1106. /* Fill buffer */
  1107. buf = dev->video_mode.isoc_ctl.buf;
  1108. vbuf = videobuf_to_vmalloc(&buf->vb);
  1109. if ((dma_q->mpeg_buffer_completed+len) <
  1110. mpeglines*mpeglinesize) {
  1111. if (dma_q->add_ps_package_head ==
  1112. CX231XX_NEED_ADD_PS_PACKAGE_HEAD) {
  1113. memcpy(vbuf+dma_q->mpeg_buffer_completed,
  1114. dma_q->ps_head, 3);
  1115. dma_q->mpeg_buffer_completed =
  1116. dma_q->mpeg_buffer_completed + 3;
  1117. dma_q->add_ps_package_head =
  1118. CX231XX_NONEED_PS_PACKAGE_HEAD;
  1119. }
  1120. memcpy(vbuf+dma_q->mpeg_buffer_completed, data, len);
  1121. dma_q->mpeg_buffer_completed =
  1122. dma_q->mpeg_buffer_completed + len;
  1123. } else {
  1124. dma_q->mpeg_buffer_done = 0;
  1125. tail_data =
  1126. mpeglines*mpeglinesize - dma_q->mpeg_buffer_completed;
  1127. memcpy(vbuf+dma_q->mpeg_buffer_completed,
  1128. data, tail_data);
  1129. buf->vb.state = VIDEOBUF_DONE;
  1130. buf->vb.field_count++;
  1131. v4l2_get_timestamp(&buf->vb.ts);
  1132. list_del(&buf->vb.queue);
  1133. wake_up(&buf->vb.done);
  1134. dma_q->mpeg_buffer_completed = 0;
  1135. if (len - tail_data > 0) {
  1136. p_data = data + tail_data;
  1137. dma_q->left_data_count = len - tail_data;
  1138. memcpy(dma_q->p_left_data,
  1139. p_data, len - tail_data);
  1140. }
  1141. }
  1142. }
  1143. static void buffer_filled(char *data, int len, struct urb *urb,
  1144. struct cx231xx_dmaqueue *dma_q)
  1145. {
  1146. void *vbuf;
  1147. struct cx231xx_buffer *buf;
  1148. if (list_empty(&dma_q->active))
  1149. return;
  1150. buf = list_entry(dma_q->active.next,
  1151. struct cx231xx_buffer, vb.queue);
  1152. /* Fill buffer */
  1153. vbuf = videobuf_to_vmalloc(&buf->vb);
  1154. memcpy(vbuf, data, len);
  1155. buf->vb.state = VIDEOBUF_DONE;
  1156. buf->vb.field_count++;
  1157. v4l2_get_timestamp(&buf->vb.ts);
  1158. list_del(&buf->vb.queue);
  1159. wake_up(&buf->vb.done);
  1160. }
  1161. static int cx231xx_isoc_copy(struct cx231xx *dev, struct urb *urb)
  1162. {
  1163. struct cx231xx_dmaqueue *dma_q = urb->context;
  1164. unsigned char *p_buffer;
  1165. u32 buffer_size = 0;
  1166. u32 i = 0;
  1167. for (i = 0; i < urb->number_of_packets; i++) {
  1168. if (dma_q->left_data_count > 0) {
  1169. buffer_copy(dev, dma_q->p_left_data,
  1170. dma_q->left_data_count, urb, dma_q);
  1171. dma_q->mpeg_buffer_completed = dma_q->left_data_count;
  1172. dma_q->left_data_count = 0;
  1173. }
  1174. p_buffer = urb->transfer_buffer +
  1175. urb->iso_frame_desc[i].offset;
  1176. buffer_size = urb->iso_frame_desc[i].actual_length;
  1177. if (buffer_size > 0)
  1178. buffer_copy(dev, p_buffer, buffer_size, urb, dma_q);
  1179. }
  1180. return 0;
  1181. }
  1182. static int cx231xx_bulk_copy(struct cx231xx *dev, struct urb *urb)
  1183. {
  1184. struct cx231xx_dmaqueue *dma_q = urb->context;
  1185. unsigned char *p_buffer, *buffer;
  1186. u32 buffer_size = 0;
  1187. p_buffer = urb->transfer_buffer;
  1188. buffer_size = urb->actual_length;
  1189. buffer = kmalloc(buffer_size, GFP_ATOMIC);
  1190. if (!buffer)
  1191. return -ENOMEM;
  1192. memcpy(buffer, dma_q->ps_head, 3);
  1193. memcpy(buffer+3, p_buffer, buffer_size-3);
  1194. memcpy(dma_q->ps_head, p_buffer+buffer_size-3, 3);
  1195. p_buffer = buffer;
  1196. buffer_filled(p_buffer, buffer_size, urb, dma_q);
  1197. kfree(buffer);
  1198. return 0;
  1199. }
  1200. static int bb_buf_prepare(struct videobuf_queue *q,
  1201. struct videobuf_buffer *vb, enum v4l2_field field)
  1202. {
  1203. struct cx231xx_fh *fh = q->priv_data;
  1204. struct cx231xx_buffer *buf =
  1205. container_of(vb, struct cx231xx_buffer, vb);
  1206. struct cx231xx *dev = fh->dev;
  1207. int rc = 0, urb_init = 0;
  1208. int size = fh->dev->ts1.ts_packet_size * fh->dev->ts1.ts_packet_count;
  1209. if (0 != buf->vb.baddr && buf->vb.bsize < size)
  1210. return -EINVAL;
  1211. buf->vb.width = fh->dev->ts1.ts_packet_size;
  1212. buf->vb.height = fh->dev->ts1.ts_packet_count;
  1213. buf->vb.size = size;
  1214. buf->vb.field = field;
  1215. if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
  1216. rc = videobuf_iolock(q, &buf->vb, NULL);
  1217. if (rc < 0)
  1218. goto fail;
  1219. }
  1220. if (dev->USE_ISO) {
  1221. if (!dev->video_mode.isoc_ctl.num_bufs)
  1222. urb_init = 1;
  1223. } else {
  1224. if (!dev->video_mode.bulk_ctl.num_bufs)
  1225. urb_init = 1;
  1226. }
  1227. dev_dbg(dev->dev,
  1228. "urb_init=%d dev->video_mode.max_pkt_size=%d\n",
  1229. urb_init, dev->video_mode.max_pkt_size);
  1230. dev->mode_tv = 1;
  1231. if (urb_init) {
  1232. rc = cx231xx_set_mode(dev, CX231XX_DIGITAL_MODE);
  1233. rc = cx231xx_unmute_audio(dev);
  1234. if (dev->USE_ISO) {
  1235. cx231xx_set_alt_setting(dev, INDEX_TS1, 4);
  1236. rc = cx231xx_init_isoc(dev, mpeglines,
  1237. mpegbufs,
  1238. dev->ts1_mode.max_pkt_size,
  1239. cx231xx_isoc_copy);
  1240. } else {
  1241. cx231xx_set_alt_setting(dev, INDEX_TS1, 0);
  1242. rc = cx231xx_init_bulk(dev, mpeglines,
  1243. mpegbufs,
  1244. dev->ts1_mode.max_pkt_size,
  1245. cx231xx_bulk_copy);
  1246. }
  1247. if (rc < 0)
  1248. goto fail;
  1249. }
  1250. buf->vb.state = VIDEOBUF_PREPARED;
  1251. return 0;
  1252. fail:
  1253. free_buffer(q, buf);
  1254. return rc;
  1255. }
  1256. static void bb_buf_queue(struct videobuf_queue *q,
  1257. struct videobuf_buffer *vb)
  1258. {
  1259. struct cx231xx_fh *fh = q->priv_data;
  1260. struct cx231xx_buffer *buf =
  1261. container_of(vb, struct cx231xx_buffer, vb);
  1262. struct cx231xx *dev = fh->dev;
  1263. struct cx231xx_dmaqueue *vidq = &dev->video_mode.vidq;
  1264. buf->vb.state = VIDEOBUF_QUEUED;
  1265. list_add_tail(&buf->vb.queue, &vidq->active);
  1266. }
  1267. static void bb_buf_release(struct videobuf_queue *q,
  1268. struct videobuf_buffer *vb)
  1269. {
  1270. struct cx231xx_buffer *buf =
  1271. container_of(vb, struct cx231xx_buffer, vb);
  1272. /*struct cx231xx_fh *fh = q->priv_data;*/
  1273. /*struct cx231xx *dev = (struct cx231xx *)fh->dev;*/
  1274. free_buffer(q, buf);
  1275. }
  1276. static struct videobuf_queue_ops cx231xx_qops = {
  1277. .buf_setup = bb_buf_setup,
  1278. .buf_prepare = bb_buf_prepare,
  1279. .buf_queue = bb_buf_queue,
  1280. .buf_release = bb_buf_release,
  1281. };
  1282. /* ------------------------------------------------------------------ */
  1283. static int vidioc_cropcap(struct file *file, void *priv,
  1284. struct v4l2_cropcap *cc)
  1285. {
  1286. struct cx231xx_fh *fh = priv;
  1287. struct cx231xx *dev = fh->dev;
  1288. bool is_50hz = dev->encodernorm.id & V4L2_STD_625_50;
  1289. if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1290. return -EINVAL;
  1291. cc->bounds.left = 0;
  1292. cc->bounds.top = 0;
  1293. cc->bounds.width = dev->ts1.width;
  1294. cc->bounds.height = dev->ts1.height;
  1295. cc->defrect = cc->bounds;
  1296. cc->pixelaspect.numerator = is_50hz ? 54 : 11;
  1297. cc->pixelaspect.denominator = is_50hz ? 59 : 10;
  1298. return 0;
  1299. }
  1300. static int vidioc_g_std(struct file *file, void *fh0, v4l2_std_id *norm)
  1301. {
  1302. struct cx231xx_fh *fh = file->private_data;
  1303. struct cx231xx *dev = fh->dev;
  1304. *norm = dev->encodernorm.id;
  1305. return 0;
  1306. }
  1307. static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id id)
  1308. {
  1309. struct cx231xx_fh *fh = file->private_data;
  1310. struct cx231xx *dev = fh->dev;
  1311. unsigned int i;
  1312. for (i = 0; i < ARRAY_SIZE(cx231xx_tvnorms); i++)
  1313. if (id & cx231xx_tvnorms[i].id)
  1314. break;
  1315. if (i == ARRAY_SIZE(cx231xx_tvnorms))
  1316. return -EINVAL;
  1317. dev->encodernorm = cx231xx_tvnorms[i];
  1318. if (dev->encodernorm.id & 0xb000) {
  1319. dprintk(3, "encodernorm set to NTSC\n");
  1320. dev->norm = V4L2_STD_NTSC;
  1321. dev->ts1.height = 480;
  1322. cx2341x_handler_set_50hz(&dev->mpeg_ctrl_handler, false);
  1323. } else {
  1324. dprintk(3, "encodernorm set to PAL\n");
  1325. dev->norm = V4L2_STD_PAL_B;
  1326. dev->ts1.height = 576;
  1327. cx2341x_handler_set_50hz(&dev->mpeg_ctrl_handler, true);
  1328. }
  1329. call_all(dev, video, s_std, dev->norm);
  1330. /* do mode control overrides */
  1331. cx231xx_do_mode_ctrl_overrides(dev);
  1332. dprintk(3, "exit vidioc_s_std() i=0x%x\n", i);
  1333. return 0;
  1334. }
  1335. static int vidioc_s_ctrl(struct file *file, void *priv,
  1336. struct v4l2_control *ctl)
  1337. {
  1338. struct cx231xx_fh *fh = file->private_data;
  1339. struct cx231xx *dev = fh->dev;
  1340. struct v4l2_subdev *sd;
  1341. dprintk(3, "enter vidioc_s_ctrl()\n");
  1342. /* Update the A/V core */
  1343. v4l2_device_for_each_subdev(sd, &dev->v4l2_dev)
  1344. v4l2_s_ctrl(NULL, sd->ctrl_handler, ctl);
  1345. dprintk(3, "exit vidioc_s_ctrl()\n");
  1346. return 0;
  1347. }
  1348. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  1349. struct v4l2_fmtdesc *f)
  1350. {
  1351. if (f->index != 0)
  1352. return -EINVAL;
  1353. strlcpy(f->description, "MPEG", sizeof(f->description));
  1354. f->pixelformat = V4L2_PIX_FMT_MPEG;
  1355. return 0;
  1356. }
  1357. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  1358. struct v4l2_format *f)
  1359. {
  1360. struct cx231xx_fh *fh = file->private_data;
  1361. struct cx231xx *dev = fh->dev;
  1362. dprintk(3, "enter vidioc_g_fmt_vid_cap()\n");
  1363. f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
  1364. f->fmt.pix.bytesperline = 0;
  1365. f->fmt.pix.sizeimage = mpeglines * mpeglinesize;
  1366. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  1367. f->fmt.pix.width = dev->ts1.width;
  1368. f->fmt.pix.height = dev->ts1.height;
  1369. f->fmt.pix.field = V4L2_FIELD_INTERLACED;
  1370. dprintk(1, "VIDIOC_G_FMT: w: %d, h: %d\n",
  1371. dev->ts1.width, dev->ts1.height);
  1372. dprintk(3, "exit vidioc_g_fmt_vid_cap()\n");
  1373. return 0;
  1374. }
  1375. static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  1376. struct v4l2_format *f)
  1377. {
  1378. struct cx231xx_fh *fh = file->private_data;
  1379. struct cx231xx *dev = fh->dev;
  1380. dprintk(3, "enter vidioc_try_fmt_vid_cap()\n");
  1381. f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
  1382. f->fmt.pix.bytesperline = 0;
  1383. f->fmt.pix.sizeimage = mpeglines * mpeglinesize;
  1384. f->fmt.pix.field = V4L2_FIELD_INTERLACED;
  1385. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  1386. dprintk(1, "VIDIOC_TRY_FMT: w: %d, h: %d\n",
  1387. dev->ts1.width, dev->ts1.height);
  1388. dprintk(3, "exit vidioc_try_fmt_vid_cap()\n");
  1389. return 0;
  1390. }
  1391. static int vidioc_reqbufs(struct file *file, void *priv,
  1392. struct v4l2_requestbuffers *p)
  1393. {
  1394. struct cx231xx_fh *fh = file->private_data;
  1395. return videobuf_reqbufs(&fh->vidq, p);
  1396. }
  1397. static int vidioc_querybuf(struct file *file, void *priv,
  1398. struct v4l2_buffer *p)
  1399. {
  1400. struct cx231xx_fh *fh = file->private_data;
  1401. return videobuf_querybuf(&fh->vidq, p);
  1402. }
  1403. static int vidioc_qbuf(struct file *file, void *priv,
  1404. struct v4l2_buffer *p)
  1405. {
  1406. struct cx231xx_fh *fh = file->private_data;
  1407. return videobuf_qbuf(&fh->vidq, p);
  1408. }
  1409. static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
  1410. {
  1411. struct cx231xx_fh *fh = priv;
  1412. return videobuf_dqbuf(&fh->vidq, b, file->f_flags & O_NONBLOCK);
  1413. }
  1414. static int vidioc_streamon(struct file *file, void *priv,
  1415. enum v4l2_buf_type i)
  1416. {
  1417. struct cx231xx_fh *fh = file->private_data;
  1418. struct cx231xx *dev = fh->dev;
  1419. dprintk(3, "enter vidioc_streamon()\n");
  1420. cx231xx_set_alt_setting(dev, INDEX_TS1, 0);
  1421. cx231xx_set_mode(dev, CX231XX_DIGITAL_MODE);
  1422. if (dev->USE_ISO)
  1423. cx231xx_init_isoc(dev, CX231XX_NUM_PACKETS,
  1424. CX231XX_NUM_BUFS,
  1425. dev->video_mode.max_pkt_size,
  1426. cx231xx_isoc_copy);
  1427. else {
  1428. cx231xx_init_bulk(dev, 320,
  1429. 5,
  1430. dev->ts1_mode.max_pkt_size,
  1431. cx231xx_bulk_copy);
  1432. }
  1433. dprintk(3, "exit vidioc_streamon()\n");
  1434. return videobuf_streamon(&fh->vidq);
  1435. }
  1436. static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
  1437. {
  1438. struct cx231xx_fh *fh = file->private_data;
  1439. return videobuf_streamoff(&fh->vidq);
  1440. }
  1441. static int vidioc_log_status(struct file *file, void *priv)
  1442. {
  1443. struct cx231xx_fh *fh = priv;
  1444. struct cx231xx *dev = fh->dev;
  1445. call_all(dev, core, log_status);
  1446. return v4l2_ctrl_log_status(file, priv);
  1447. }
  1448. static int mpeg_open(struct file *file)
  1449. {
  1450. struct video_device *vdev = video_devdata(file);
  1451. struct cx231xx *dev = video_drvdata(file);
  1452. struct cx231xx_fh *fh;
  1453. dprintk(2, "%s()\n", __func__);
  1454. if (mutex_lock_interruptible(&dev->lock))
  1455. return -ERESTARTSYS;
  1456. /* allocate + initialize per filehandle data */
  1457. fh = kzalloc(sizeof(*fh), GFP_KERNEL);
  1458. if (NULL == fh) {
  1459. mutex_unlock(&dev->lock);
  1460. return -ENOMEM;
  1461. }
  1462. file->private_data = fh;
  1463. v4l2_fh_init(&fh->fh, vdev);
  1464. fh->dev = dev;
  1465. videobuf_queue_vmalloc_init(&fh->vidq, &cx231xx_qops,
  1466. NULL, &dev->video_mode.slock,
  1467. V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_FIELD_INTERLACED,
  1468. sizeof(struct cx231xx_buffer), fh, &dev->lock);
  1469. /*
  1470. videobuf_queue_sg_init(&fh->vidq, &cx231xx_qops,
  1471. dev->dev, &dev->ts1.slock,
  1472. V4L2_BUF_TYPE_VIDEO_CAPTURE,
  1473. V4L2_FIELD_INTERLACED,
  1474. sizeof(struct cx231xx_buffer),
  1475. fh, &dev->lock);
  1476. */
  1477. cx231xx_set_alt_setting(dev, INDEX_VANC, 1);
  1478. cx231xx_set_gpio_value(dev, 2, 0);
  1479. cx231xx_initialize_codec(dev);
  1480. mutex_unlock(&dev->lock);
  1481. v4l2_fh_add(&fh->fh);
  1482. cx231xx_start_TS1(dev);
  1483. return 0;
  1484. }
  1485. static int mpeg_release(struct file *file)
  1486. {
  1487. struct cx231xx_fh *fh = file->private_data;
  1488. struct cx231xx *dev = fh->dev;
  1489. dprintk(3, "mpeg_release()! dev=0x%p\n", dev);
  1490. mutex_lock(&dev->lock);
  1491. cx231xx_stop_TS1(dev);
  1492. /* do this before setting alternate! */
  1493. if (dev->USE_ISO)
  1494. cx231xx_uninit_isoc(dev);
  1495. else
  1496. cx231xx_uninit_bulk(dev);
  1497. cx231xx_set_mode(dev, CX231XX_SUSPEND);
  1498. cx231xx_api_cmd(fh->dev, CX2341X_ENC_STOP_CAPTURE, 3, 0,
  1499. CX231xx_END_NOW, CX231xx_MPEG_CAPTURE,
  1500. CX231xx_RAW_BITS_NONE);
  1501. /* FIXME: Review this crap */
  1502. /* Shut device down on last close */
  1503. if (atomic_cmpxchg(&fh->v4l_reading, 1, 0) == 1) {
  1504. if (atomic_dec_return(&dev->v4l_reader_count) == 0) {
  1505. /* stop mpeg capture */
  1506. msleep(500);
  1507. cx231xx_417_check_encoder(dev);
  1508. }
  1509. }
  1510. if (fh->vidq.streaming)
  1511. videobuf_streamoff(&fh->vidq);
  1512. if (fh->vidq.reading)
  1513. videobuf_read_stop(&fh->vidq);
  1514. videobuf_mmap_free(&fh->vidq);
  1515. v4l2_fh_del(&fh->fh);
  1516. v4l2_fh_exit(&fh->fh);
  1517. kfree(fh);
  1518. mutex_unlock(&dev->lock);
  1519. return 0;
  1520. }
  1521. static ssize_t mpeg_read(struct file *file, char __user *data,
  1522. size_t count, loff_t *ppos)
  1523. {
  1524. struct cx231xx_fh *fh = file->private_data;
  1525. struct cx231xx *dev = fh->dev;
  1526. /* Deal w/ A/V decoder * and mpeg encoder sync issues. */
  1527. /* Start mpeg encoder on first read. */
  1528. if (atomic_cmpxchg(&fh->v4l_reading, 0, 1) == 0) {
  1529. if (atomic_inc_return(&dev->v4l_reader_count) == 1) {
  1530. if (cx231xx_initialize_codec(dev) < 0)
  1531. return -EINVAL;
  1532. }
  1533. }
  1534. return videobuf_read_stream(&fh->vidq, data, count, ppos, 0,
  1535. file->f_flags & O_NONBLOCK);
  1536. }
  1537. static unsigned int mpeg_poll(struct file *file,
  1538. struct poll_table_struct *wait)
  1539. {
  1540. unsigned long req_events = poll_requested_events(wait);
  1541. struct cx231xx_fh *fh = file->private_data;
  1542. struct cx231xx *dev = fh->dev;
  1543. unsigned int res = 0;
  1544. if (v4l2_event_pending(&fh->fh))
  1545. res |= POLLPRI;
  1546. else
  1547. poll_wait(file, &fh->fh.wait, wait);
  1548. if (!(req_events & (POLLIN | POLLRDNORM)))
  1549. return res;
  1550. mutex_lock(&dev->lock);
  1551. res |= videobuf_poll_stream(file, &fh->vidq, wait);
  1552. mutex_unlock(&dev->lock);
  1553. return res;
  1554. }
  1555. static int mpeg_mmap(struct file *file, struct vm_area_struct *vma)
  1556. {
  1557. struct cx231xx_fh *fh = file->private_data;
  1558. dprintk(2, "%s()\n", __func__);
  1559. return videobuf_mmap_mapper(&fh->vidq, vma);
  1560. }
  1561. static struct v4l2_file_operations mpeg_fops = {
  1562. .owner = THIS_MODULE,
  1563. .open = mpeg_open,
  1564. .release = mpeg_release,
  1565. .read = mpeg_read,
  1566. .poll = mpeg_poll,
  1567. .mmap = mpeg_mmap,
  1568. .unlocked_ioctl = video_ioctl2,
  1569. };
  1570. static const struct v4l2_ioctl_ops mpeg_ioctl_ops = {
  1571. .vidioc_s_std = vidioc_s_std,
  1572. .vidioc_g_std = vidioc_g_std,
  1573. .vidioc_g_tuner = cx231xx_g_tuner,
  1574. .vidioc_s_tuner = cx231xx_s_tuner,
  1575. .vidioc_g_frequency = cx231xx_g_frequency,
  1576. .vidioc_s_frequency = cx231xx_s_frequency,
  1577. .vidioc_enum_input = cx231xx_enum_input,
  1578. .vidioc_g_input = cx231xx_g_input,
  1579. .vidioc_s_input = cx231xx_s_input,
  1580. .vidioc_s_ctrl = vidioc_s_ctrl,
  1581. .vidioc_cropcap = vidioc_cropcap,
  1582. .vidioc_querycap = cx231xx_querycap,
  1583. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1584. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  1585. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  1586. .vidioc_s_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  1587. .vidioc_reqbufs = vidioc_reqbufs,
  1588. .vidioc_querybuf = vidioc_querybuf,
  1589. .vidioc_qbuf = vidioc_qbuf,
  1590. .vidioc_dqbuf = vidioc_dqbuf,
  1591. .vidioc_streamon = vidioc_streamon,
  1592. .vidioc_streamoff = vidioc_streamoff,
  1593. .vidioc_log_status = vidioc_log_status,
  1594. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1595. .vidioc_g_register = cx231xx_g_register,
  1596. .vidioc_s_register = cx231xx_s_register,
  1597. #endif
  1598. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1599. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1600. };
  1601. static struct video_device cx231xx_mpeg_template = {
  1602. .name = "cx231xx",
  1603. .fops = &mpeg_fops,
  1604. .ioctl_ops = &mpeg_ioctl_ops,
  1605. .minor = -1,
  1606. .tvnorms = V4L2_STD_ALL,
  1607. };
  1608. void cx231xx_417_unregister(struct cx231xx *dev)
  1609. {
  1610. dprintk(1, "%s()\n", __func__);
  1611. dprintk(3, "%s()\n", __func__);
  1612. if (video_is_registered(&dev->v4l_device)) {
  1613. video_unregister_device(&dev->v4l_device);
  1614. v4l2_ctrl_handler_free(&dev->mpeg_ctrl_handler.hdl);
  1615. }
  1616. }
  1617. static int cx231xx_s_video_encoding(struct cx2341x_handler *cxhdl, u32 val)
  1618. {
  1619. struct cx231xx *dev = container_of(cxhdl, struct cx231xx, mpeg_ctrl_handler);
  1620. int is_mpeg1 = val == V4L2_MPEG_VIDEO_ENCODING_MPEG_1;
  1621. struct v4l2_subdev_format format = {
  1622. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  1623. };
  1624. /* fix videodecoder resolution */
  1625. format.format.width = cxhdl->width / (is_mpeg1 ? 2 : 1);
  1626. format.format.height = cxhdl->height;
  1627. format.format.code = MEDIA_BUS_FMT_FIXED;
  1628. v4l2_subdev_call(dev->sd_cx25840, pad, set_fmt, NULL, &format);
  1629. return 0;
  1630. }
  1631. static int cx231xx_s_audio_sampling_freq(struct cx2341x_handler *cxhdl, u32 idx)
  1632. {
  1633. static const u32 freqs[3] = { 44100, 48000, 32000 };
  1634. struct cx231xx *dev = container_of(cxhdl, struct cx231xx, mpeg_ctrl_handler);
  1635. /* The audio clock of the digitizer must match the codec sample
  1636. rate otherwise you get some very strange effects. */
  1637. if (idx < ARRAY_SIZE(freqs))
  1638. call_all(dev, audio, s_clock_freq, freqs[idx]);
  1639. return 0;
  1640. }
  1641. static const struct cx2341x_handler_ops cx231xx_ops = {
  1642. /* needed for the video clock freq */
  1643. .s_audio_sampling_freq = cx231xx_s_audio_sampling_freq,
  1644. /* needed for setting up the video resolution */
  1645. .s_video_encoding = cx231xx_s_video_encoding,
  1646. };
  1647. static void cx231xx_video_dev_init(
  1648. struct cx231xx *dev,
  1649. struct usb_device *usbdev,
  1650. struct video_device *vfd,
  1651. const struct video_device *template,
  1652. const char *type)
  1653. {
  1654. dprintk(1, "%s()\n", __func__);
  1655. *vfd = *template;
  1656. snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)", dev->name,
  1657. type, cx231xx_boards[dev->model].name);
  1658. vfd->v4l2_dev = &dev->v4l2_dev;
  1659. vfd->lock = &dev->lock;
  1660. vfd->release = video_device_release_empty;
  1661. vfd->ctrl_handler = &dev->mpeg_ctrl_handler.hdl;
  1662. video_set_drvdata(vfd, dev);
  1663. if (dev->tuner_type == TUNER_ABSENT) {
  1664. v4l2_disable_ioctl(vfd, VIDIOC_G_FREQUENCY);
  1665. v4l2_disable_ioctl(vfd, VIDIOC_S_FREQUENCY);
  1666. v4l2_disable_ioctl(vfd, VIDIOC_G_TUNER);
  1667. v4l2_disable_ioctl(vfd, VIDIOC_S_TUNER);
  1668. }
  1669. }
  1670. int cx231xx_417_register(struct cx231xx *dev)
  1671. {
  1672. /* FIXME: Port1 hardcoded here */
  1673. int err = -ENODEV;
  1674. struct cx231xx_tsport *tsport = &dev->ts1;
  1675. dprintk(1, "%s()\n", __func__);
  1676. /* Set default TV standard */
  1677. dev->encodernorm = cx231xx_tvnorms[0];
  1678. if (dev->encodernorm.id & V4L2_STD_525_60)
  1679. tsport->height = 480;
  1680. else
  1681. tsport->height = 576;
  1682. tsport->width = 720;
  1683. err = cx2341x_handler_init(&dev->mpeg_ctrl_handler, 50);
  1684. if (err) {
  1685. dprintk(3, "%s: can't init cx2341x controls\n", dev->name);
  1686. return err;
  1687. }
  1688. dev->mpeg_ctrl_handler.func = cx231xx_mbox_func;
  1689. dev->mpeg_ctrl_handler.priv = dev;
  1690. dev->mpeg_ctrl_handler.ops = &cx231xx_ops;
  1691. if (dev->sd_cx25840)
  1692. v4l2_ctrl_add_handler(&dev->mpeg_ctrl_handler.hdl,
  1693. dev->sd_cx25840->ctrl_handler, NULL);
  1694. if (dev->mpeg_ctrl_handler.hdl.error) {
  1695. err = dev->mpeg_ctrl_handler.hdl.error;
  1696. dprintk(3, "%s: can't add cx25840 controls\n", dev->name);
  1697. v4l2_ctrl_handler_free(&dev->mpeg_ctrl_handler.hdl);
  1698. return err;
  1699. }
  1700. dev->norm = V4L2_STD_NTSC;
  1701. dev->mpeg_ctrl_handler.port = CX2341X_PORT_SERIAL;
  1702. cx2341x_handler_set_50hz(&dev->mpeg_ctrl_handler, false);
  1703. /* Allocate and initialize V4L video device */
  1704. cx231xx_video_dev_init(dev, dev->udev,
  1705. &dev->v4l_device, &cx231xx_mpeg_template, "mpeg");
  1706. err = video_register_device(&dev->v4l_device,
  1707. VFL_TYPE_GRABBER, -1);
  1708. if (err < 0) {
  1709. dprintk(3, "%s: can't register mpeg device\n", dev->name);
  1710. v4l2_ctrl_handler_free(&dev->mpeg_ctrl_handler.hdl);
  1711. return err;
  1712. }
  1713. dprintk(3, "%s: registered device video%d [mpeg]\n",
  1714. dev->name, dev->v4l_device.num);
  1715. return 0;
  1716. }
  1717. /*(DEBLOBBED)*/