cx88-video.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157
  1. /*
  2. *
  3. * device driver for Conexant 2388x based TV cards
  4. * video4linux video interface
  5. *
  6. * (c) 2003-04 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #include <linux/init.h>
  23. #include <linux/list.h>
  24. #include <linux/module.h>
  25. #include <linux/moduleparam.h>
  26. #include <linux/kmod.h>
  27. #include <linux/kernel.h>
  28. #include <linux/slab.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/delay.h>
  31. #include <linux/kthread.h>
  32. #include <asm/div64.h>
  33. #include "cx88.h"
  34. #include <media/v4l2-common.h>
  35. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  36. /* Include V4L1 specific functions. Should be removed soon */
  37. #include <linux/videodev.h>
  38. #endif
  39. MODULE_DESCRIPTION("v4l2 driver module for cx2388x based TV cards");
  40. MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
  41. MODULE_LICENSE("GPL");
  42. /* ------------------------------------------------------------------ */
  43. static unsigned int video_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
  44. static unsigned int vbi_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
  45. static unsigned int radio_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
  46. module_param_array(video_nr, int, NULL, 0444);
  47. module_param_array(vbi_nr, int, NULL, 0444);
  48. module_param_array(radio_nr, int, NULL, 0444);
  49. MODULE_PARM_DESC(video_nr,"video device numbers");
  50. MODULE_PARM_DESC(vbi_nr,"vbi device numbers");
  51. MODULE_PARM_DESC(radio_nr,"radio device numbers");
  52. static unsigned int video_debug = 0;
  53. module_param(video_debug,int,0644);
  54. MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
  55. static unsigned int irq_debug = 0;
  56. module_param(irq_debug,int,0644);
  57. MODULE_PARM_DESC(irq_debug,"enable debug messages [IRQ handler]");
  58. static unsigned int vid_limit = 16;
  59. module_param(vid_limit,int,0644);
  60. MODULE_PARM_DESC(vid_limit,"capture memory limit in megabytes");
  61. #define dprintk(level,fmt, arg...) if (video_debug >= level) \
  62. printk(KERN_DEBUG "%s/0: " fmt, core->name , ## arg)
  63. /* ------------------------------------------------------------------ */
  64. static LIST_HEAD(cx8800_devlist);
  65. /* ------------------------------------------------------------------- */
  66. /* static data */
  67. static struct cx88_tvnorm tvnorms[] = {
  68. {
  69. .name = "NTSC-M",
  70. .id = V4L2_STD_NTSC_M,
  71. .cxiformat = VideoFormatNTSC,
  72. .cxoformat = 0x181f0008,
  73. },{
  74. .name = "NTSC-JP",
  75. .id = V4L2_STD_NTSC_M_JP,
  76. .cxiformat = VideoFormatNTSCJapan,
  77. .cxoformat = 0x181f0008,
  78. },{
  79. .name = "PAL-BG",
  80. .id = V4L2_STD_PAL_BG,
  81. .cxiformat = VideoFormatPAL,
  82. .cxoformat = 0x181f0008,
  83. },{
  84. .name = "PAL-DK",
  85. .id = V4L2_STD_PAL_DK,
  86. .cxiformat = VideoFormatPAL,
  87. .cxoformat = 0x181f0008,
  88. },{
  89. .name = "PAL-I",
  90. .id = V4L2_STD_PAL_I,
  91. .cxiformat = VideoFormatPAL,
  92. .cxoformat = 0x181f0008,
  93. },{
  94. .name = "PAL-M",
  95. .id = V4L2_STD_PAL_M,
  96. .cxiformat = VideoFormatPALM,
  97. .cxoformat = 0x1c1f0008,
  98. },{
  99. .name = "PAL-N",
  100. .id = V4L2_STD_PAL_N,
  101. .cxiformat = VideoFormatPALN,
  102. .cxoformat = 0x1c1f0008,
  103. },{
  104. .name = "PAL-Nc",
  105. .id = V4L2_STD_PAL_Nc,
  106. .cxiformat = VideoFormatPALNC,
  107. .cxoformat = 0x1c1f0008,
  108. },{
  109. .name = "PAL-60",
  110. .id = V4L2_STD_PAL_60,
  111. .cxiformat = VideoFormatPAL60,
  112. .cxoformat = 0x181f0008,
  113. },{
  114. .name = "SECAM-L",
  115. .id = V4L2_STD_SECAM_L,
  116. .cxiformat = VideoFormatSECAM,
  117. .cxoformat = 0x181f0008,
  118. },{
  119. .name = "SECAM-DK",
  120. .id = V4L2_STD_SECAM_DK,
  121. .cxiformat = VideoFormatSECAM,
  122. .cxoformat = 0x181f0008,
  123. }
  124. };
  125. static struct cx8800_fmt formats[] = {
  126. {
  127. .name = "8 bpp, gray",
  128. .fourcc = V4L2_PIX_FMT_GREY,
  129. .cxformat = ColorFormatY8,
  130. .depth = 8,
  131. .flags = FORMAT_FLAGS_PACKED,
  132. },{
  133. .name = "15 bpp RGB, le",
  134. .fourcc = V4L2_PIX_FMT_RGB555,
  135. .cxformat = ColorFormatRGB15,
  136. .depth = 16,
  137. .flags = FORMAT_FLAGS_PACKED,
  138. },{
  139. .name = "15 bpp RGB, be",
  140. .fourcc = V4L2_PIX_FMT_RGB555X,
  141. .cxformat = ColorFormatRGB15 | ColorFormatBSWAP,
  142. .depth = 16,
  143. .flags = FORMAT_FLAGS_PACKED,
  144. },{
  145. .name = "16 bpp RGB, le",
  146. .fourcc = V4L2_PIX_FMT_RGB565,
  147. .cxformat = ColorFormatRGB16,
  148. .depth = 16,
  149. .flags = FORMAT_FLAGS_PACKED,
  150. },{
  151. .name = "16 bpp RGB, be",
  152. .fourcc = V4L2_PIX_FMT_RGB565X,
  153. .cxformat = ColorFormatRGB16 | ColorFormatBSWAP,
  154. .depth = 16,
  155. .flags = FORMAT_FLAGS_PACKED,
  156. },{
  157. .name = "24 bpp RGB, le",
  158. .fourcc = V4L2_PIX_FMT_BGR24,
  159. .cxformat = ColorFormatRGB24,
  160. .depth = 24,
  161. .flags = FORMAT_FLAGS_PACKED,
  162. },{
  163. .name = "32 bpp RGB, le",
  164. .fourcc = V4L2_PIX_FMT_BGR32,
  165. .cxformat = ColorFormatRGB32,
  166. .depth = 32,
  167. .flags = FORMAT_FLAGS_PACKED,
  168. },{
  169. .name = "32 bpp RGB, be",
  170. .fourcc = V4L2_PIX_FMT_RGB32,
  171. .cxformat = ColorFormatRGB32 | ColorFormatBSWAP | ColorFormatWSWAP,
  172. .depth = 32,
  173. .flags = FORMAT_FLAGS_PACKED,
  174. },{
  175. .name = "4:2:2, packed, YUYV",
  176. .fourcc = V4L2_PIX_FMT_YUYV,
  177. .cxformat = ColorFormatYUY2,
  178. .depth = 16,
  179. .flags = FORMAT_FLAGS_PACKED,
  180. },{
  181. .name = "4:2:2, packed, UYVY",
  182. .fourcc = V4L2_PIX_FMT_UYVY,
  183. .cxformat = ColorFormatYUY2 | ColorFormatBSWAP,
  184. .depth = 16,
  185. .flags = FORMAT_FLAGS_PACKED,
  186. },
  187. };
  188. static struct cx8800_fmt* format_by_fourcc(unsigned int fourcc)
  189. {
  190. unsigned int i;
  191. for (i = 0; i < ARRAY_SIZE(formats); i++)
  192. if (formats[i].fourcc == fourcc)
  193. return formats+i;
  194. return NULL;
  195. }
  196. /* ------------------------------------------------------------------- */
  197. static const struct v4l2_queryctrl no_ctl = {
  198. .name = "42",
  199. .flags = V4L2_CTRL_FLAG_DISABLED,
  200. };
  201. static struct cx88_ctrl cx8800_ctls[] = {
  202. /* --- video --- */
  203. {
  204. .v = {
  205. .id = V4L2_CID_BRIGHTNESS,
  206. .name = "Brightness",
  207. .minimum = 0x00,
  208. .maximum = 0xff,
  209. .step = 1,
  210. .default_value = 0x7f,
  211. .type = V4L2_CTRL_TYPE_INTEGER,
  212. },
  213. .off = 128,
  214. .reg = MO_CONTR_BRIGHT,
  215. .mask = 0x00ff,
  216. .shift = 0,
  217. },{
  218. .v = {
  219. .id = V4L2_CID_CONTRAST,
  220. .name = "Contrast",
  221. .minimum = 0,
  222. .maximum = 0xff,
  223. .step = 1,
  224. .default_value = 0x3f,
  225. .type = V4L2_CTRL_TYPE_INTEGER,
  226. },
  227. .off = 0,
  228. .reg = MO_CONTR_BRIGHT,
  229. .mask = 0xff00,
  230. .shift = 8,
  231. },{
  232. .v = {
  233. .id = V4L2_CID_HUE,
  234. .name = "Hue",
  235. .minimum = 0,
  236. .maximum = 0xff,
  237. .step = 1,
  238. .default_value = 0x7f,
  239. .type = V4L2_CTRL_TYPE_INTEGER,
  240. },
  241. .off = 128,
  242. .reg = MO_HUE,
  243. .mask = 0x00ff,
  244. .shift = 0,
  245. },{
  246. /* strictly, this only describes only U saturation.
  247. * V saturation is handled specially through code.
  248. */
  249. .v = {
  250. .id = V4L2_CID_SATURATION,
  251. .name = "Saturation",
  252. .minimum = 0,
  253. .maximum = 0xff,
  254. .step = 1,
  255. .default_value = 0x7f,
  256. .type = V4L2_CTRL_TYPE_INTEGER,
  257. },
  258. .off = 0,
  259. .reg = MO_UV_SATURATION,
  260. .mask = 0x00ff,
  261. .shift = 0,
  262. },{
  263. /* --- audio --- */
  264. .v = {
  265. .id = V4L2_CID_AUDIO_MUTE,
  266. .name = "Mute",
  267. .minimum = 0,
  268. .maximum = 1,
  269. .default_value = 1,
  270. .type = V4L2_CTRL_TYPE_BOOLEAN,
  271. },
  272. .reg = AUD_VOL_CTL,
  273. .sreg = SHADOW_AUD_VOL_CTL,
  274. .mask = (1 << 6),
  275. .shift = 6,
  276. },{
  277. .v = {
  278. .id = V4L2_CID_AUDIO_VOLUME,
  279. .name = "Volume",
  280. .minimum = 0,
  281. .maximum = 0x3f,
  282. .step = 1,
  283. .default_value = 0x3f,
  284. .type = V4L2_CTRL_TYPE_INTEGER,
  285. },
  286. .reg = AUD_VOL_CTL,
  287. .sreg = SHADOW_AUD_VOL_CTL,
  288. .mask = 0x3f,
  289. .shift = 0,
  290. },{
  291. .v = {
  292. .id = V4L2_CID_AUDIO_BALANCE,
  293. .name = "Balance",
  294. .minimum = 0,
  295. .maximum = 0x7f,
  296. .step = 1,
  297. .default_value = 0x40,
  298. .type = V4L2_CTRL_TYPE_INTEGER,
  299. },
  300. .reg = AUD_BAL_CTL,
  301. .sreg = SHADOW_AUD_BAL_CTL,
  302. .mask = 0x7f,
  303. .shift = 0,
  304. }
  305. };
  306. static const int CX8800_CTLS = ARRAY_SIZE(cx8800_ctls);
  307. const u32 cx88_user_ctrls[] = {
  308. V4L2_CID_USER_CLASS,
  309. V4L2_CID_BRIGHTNESS,
  310. V4L2_CID_CONTRAST,
  311. V4L2_CID_SATURATION,
  312. V4L2_CID_HUE,
  313. V4L2_CID_AUDIO_VOLUME,
  314. V4L2_CID_AUDIO_BALANCE,
  315. V4L2_CID_AUDIO_MUTE,
  316. 0
  317. };
  318. EXPORT_SYMBOL(cx88_user_ctrls);
  319. static const u32 *ctrl_classes[] = {
  320. cx88_user_ctrls,
  321. NULL
  322. };
  323. int cx8800_ctrl_query(struct v4l2_queryctrl *qctrl)
  324. {
  325. int i;
  326. if (qctrl->id < V4L2_CID_BASE ||
  327. qctrl->id >= V4L2_CID_LASTP1)
  328. return -EINVAL;
  329. for (i = 0; i < CX8800_CTLS; i++)
  330. if (cx8800_ctls[i].v.id == qctrl->id)
  331. break;
  332. if (i == CX8800_CTLS) {
  333. *qctrl = no_ctl;
  334. return 0;
  335. }
  336. *qctrl = cx8800_ctls[i].v;
  337. return 0;
  338. }
  339. EXPORT_SYMBOL(cx8800_ctrl_query);
  340. static int cx88_queryctrl(struct v4l2_queryctrl *qctrl)
  341. {
  342. qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
  343. if (qctrl->id == 0)
  344. return -EINVAL;
  345. return cx8800_ctrl_query(qctrl);
  346. }
  347. /* ------------------------------------------------------------------- */
  348. /* resource management */
  349. static int res_get(struct cx8800_dev *dev, struct cx8800_fh *fh, unsigned int bit)
  350. {
  351. struct cx88_core *core = dev->core;
  352. if (fh->resources & bit)
  353. /* have it already allocated */
  354. return 1;
  355. /* is it free? */
  356. mutex_lock(&core->lock);
  357. if (dev->resources & bit) {
  358. /* no, someone else uses it */
  359. mutex_unlock(&core->lock);
  360. return 0;
  361. }
  362. /* it's free, grab it */
  363. fh->resources |= bit;
  364. dev->resources |= bit;
  365. dprintk(1,"res: get %d\n",bit);
  366. mutex_unlock(&core->lock);
  367. return 1;
  368. }
  369. static
  370. int res_check(struct cx8800_fh *fh, unsigned int bit)
  371. {
  372. return (fh->resources & bit);
  373. }
  374. static
  375. int res_locked(struct cx8800_dev *dev, unsigned int bit)
  376. {
  377. return (dev->resources & bit);
  378. }
  379. static
  380. void res_free(struct cx8800_dev *dev, struct cx8800_fh *fh, unsigned int bits)
  381. {
  382. struct cx88_core *core = dev->core;
  383. BUG_ON((fh->resources & bits) != bits);
  384. mutex_lock(&core->lock);
  385. fh->resources &= ~bits;
  386. dev->resources &= ~bits;
  387. dprintk(1,"res: put %d\n",bits);
  388. mutex_unlock(&core->lock);
  389. }
  390. /* ------------------------------------------------------------------ */
  391. /* static int video_mux(struct cx8800_dev *dev, unsigned int input) */
  392. static int video_mux(struct cx88_core *core, unsigned int input)
  393. {
  394. /* struct cx88_core *core = dev->core; */
  395. dprintk(1,"video_mux: %d [vmux=%d,gpio=0x%x,0x%x,0x%x,0x%x]\n",
  396. input, INPUT(input)->vmux,
  397. INPUT(input)->gpio0,INPUT(input)->gpio1,
  398. INPUT(input)->gpio2,INPUT(input)->gpio3);
  399. core->input = input;
  400. cx_andor(MO_INPUT_FORMAT, 0x03 << 14, INPUT(input)->vmux << 14);
  401. cx_write(MO_GP3_IO, INPUT(input)->gpio3);
  402. cx_write(MO_GP0_IO, INPUT(input)->gpio0);
  403. cx_write(MO_GP1_IO, INPUT(input)->gpio1);
  404. cx_write(MO_GP2_IO, INPUT(input)->gpio2);
  405. switch (INPUT(input)->type) {
  406. case CX88_VMUX_SVIDEO:
  407. cx_set(MO_AFECFG_IO, 0x00000001);
  408. cx_set(MO_INPUT_FORMAT, 0x00010010);
  409. cx_set(MO_FILTER_EVEN, 0x00002020);
  410. cx_set(MO_FILTER_ODD, 0x00002020);
  411. break;
  412. default:
  413. cx_clear(MO_AFECFG_IO, 0x00000001);
  414. cx_clear(MO_INPUT_FORMAT, 0x00010010);
  415. cx_clear(MO_FILTER_EVEN, 0x00002020);
  416. cx_clear(MO_FILTER_ODD, 0x00002020);
  417. break;
  418. }
  419. return 0;
  420. }
  421. /* ------------------------------------------------------------------ */
  422. static int start_video_dma(struct cx8800_dev *dev,
  423. struct cx88_dmaqueue *q,
  424. struct cx88_buffer *buf)
  425. {
  426. struct cx88_core *core = dev->core;
  427. /* setup fifo + format */
  428. cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH21],
  429. buf->bpl, buf->risc.dma);
  430. cx88_set_scale(core, buf->vb.width, buf->vb.height, buf->vb.field);
  431. cx_write(MO_COLOR_CTRL, buf->fmt->cxformat | ColorFormatGamma);
  432. /* reset counter */
  433. cx_write(MO_VIDY_GPCNTRL,GP_COUNT_CONTROL_RESET);
  434. q->count = 1;
  435. /* enable irqs */
  436. cx_set(MO_PCI_INTMSK, core->pci_irqmask | 0x01);
  437. /* Enables corresponding bits at PCI_INT_STAT:
  438. bits 0 to 4: video, audio, transport stream, VIP, Host
  439. bit 7: timer
  440. bits 8 and 9: DMA complete for: SRC, DST
  441. bits 10 and 11: BERR signal asserted for RISC: RD, WR
  442. bits 12 to 15: BERR signal asserted for: BRDG, SRC, DST, IPB
  443. */
  444. cx_set(MO_VID_INTMSK, 0x0f0011);
  445. /* enable capture */
  446. cx_set(VID_CAPTURE_CONTROL,0x06);
  447. /* start dma */
  448. cx_set(MO_DEV_CNTRL2, (1<<5));
  449. cx_set(MO_VID_DMACNTRL, 0x11); /* Planar Y and packed FIFO and RISC enable */
  450. return 0;
  451. }
  452. #ifdef CONFIG_PM
  453. static int stop_video_dma(struct cx8800_dev *dev)
  454. {
  455. struct cx88_core *core = dev->core;
  456. /* stop dma */
  457. cx_clear(MO_VID_DMACNTRL, 0x11);
  458. /* disable capture */
  459. cx_clear(VID_CAPTURE_CONTROL,0x06);
  460. /* disable irqs */
  461. cx_clear(MO_PCI_INTMSK, 0x000001);
  462. cx_clear(MO_VID_INTMSK, 0x0f0011);
  463. return 0;
  464. }
  465. #endif
  466. static int restart_video_queue(struct cx8800_dev *dev,
  467. struct cx88_dmaqueue *q)
  468. {
  469. struct cx88_core *core = dev->core;
  470. struct cx88_buffer *buf, *prev;
  471. struct list_head *item;
  472. if (!list_empty(&q->active)) {
  473. buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
  474. dprintk(2,"restart_queue [%p/%d]: restart dma\n",
  475. buf, buf->vb.i);
  476. start_video_dma(dev, q, buf);
  477. list_for_each(item,&q->active) {
  478. buf = list_entry(item, struct cx88_buffer, vb.queue);
  479. buf->count = q->count++;
  480. }
  481. mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
  482. return 0;
  483. }
  484. prev = NULL;
  485. for (;;) {
  486. if (list_empty(&q->queued))
  487. return 0;
  488. buf = list_entry(q->queued.next, struct cx88_buffer, vb.queue);
  489. if (NULL == prev) {
  490. list_move_tail(&buf->vb.queue, &q->active);
  491. start_video_dma(dev, q, buf);
  492. buf->vb.state = STATE_ACTIVE;
  493. buf->count = q->count++;
  494. mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
  495. dprintk(2,"[%p/%d] restart_queue - first active\n",
  496. buf,buf->vb.i);
  497. } else if (prev->vb.width == buf->vb.width &&
  498. prev->vb.height == buf->vb.height &&
  499. prev->fmt == buf->fmt) {
  500. list_move_tail(&buf->vb.queue, &q->active);
  501. buf->vb.state = STATE_ACTIVE;
  502. buf->count = q->count++;
  503. prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
  504. dprintk(2,"[%p/%d] restart_queue - move to active\n",
  505. buf,buf->vb.i);
  506. } else {
  507. return 0;
  508. }
  509. prev = buf;
  510. }
  511. }
  512. /* ------------------------------------------------------------------ */
  513. static int
  514. buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
  515. {
  516. struct cx8800_fh *fh = q->priv_data;
  517. *size = fh->fmt->depth*fh->width*fh->height >> 3;
  518. if (0 == *count)
  519. *count = 32;
  520. while (*size * *count > vid_limit * 1024 * 1024)
  521. (*count)--;
  522. return 0;
  523. }
  524. static int
  525. buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
  526. enum v4l2_field field)
  527. {
  528. struct cx8800_fh *fh = q->priv_data;
  529. struct cx8800_dev *dev = fh->dev;
  530. struct cx88_core *core = dev->core;
  531. struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
  532. int rc, init_buffer = 0;
  533. BUG_ON(NULL == fh->fmt);
  534. if (fh->width < 48 || fh->width > norm_maxw(core->tvnorm) ||
  535. fh->height < 32 || fh->height > norm_maxh(core->tvnorm))
  536. return -EINVAL;
  537. buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
  538. if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
  539. return -EINVAL;
  540. if (buf->fmt != fh->fmt ||
  541. buf->vb.width != fh->width ||
  542. buf->vb.height != fh->height ||
  543. buf->vb.field != field) {
  544. buf->fmt = fh->fmt;
  545. buf->vb.width = fh->width;
  546. buf->vb.height = fh->height;
  547. buf->vb.field = field;
  548. init_buffer = 1;
  549. }
  550. if (STATE_NEEDS_INIT == buf->vb.state) {
  551. init_buffer = 1;
  552. if (0 != (rc = videobuf_iolock(q,&buf->vb,NULL)))
  553. goto fail;
  554. }
  555. if (init_buffer) {
  556. buf->bpl = buf->vb.width * buf->fmt->depth >> 3;
  557. switch (buf->vb.field) {
  558. case V4L2_FIELD_TOP:
  559. cx88_risc_buffer(dev->pci, &buf->risc,
  560. buf->vb.dma.sglist, 0, UNSET,
  561. buf->bpl, 0, buf->vb.height);
  562. break;
  563. case V4L2_FIELD_BOTTOM:
  564. cx88_risc_buffer(dev->pci, &buf->risc,
  565. buf->vb.dma.sglist, UNSET, 0,
  566. buf->bpl, 0, buf->vb.height);
  567. break;
  568. case V4L2_FIELD_INTERLACED:
  569. cx88_risc_buffer(dev->pci, &buf->risc,
  570. buf->vb.dma.sglist, 0, buf->bpl,
  571. buf->bpl, buf->bpl,
  572. buf->vb.height >> 1);
  573. break;
  574. case V4L2_FIELD_SEQ_TB:
  575. cx88_risc_buffer(dev->pci, &buf->risc,
  576. buf->vb.dma.sglist,
  577. 0, buf->bpl * (buf->vb.height >> 1),
  578. buf->bpl, 0,
  579. buf->vb.height >> 1);
  580. break;
  581. case V4L2_FIELD_SEQ_BT:
  582. cx88_risc_buffer(dev->pci, &buf->risc,
  583. buf->vb.dma.sglist,
  584. buf->bpl * (buf->vb.height >> 1), 0,
  585. buf->bpl, 0,
  586. buf->vb.height >> 1);
  587. break;
  588. default:
  589. BUG();
  590. }
  591. }
  592. dprintk(2,"[%p/%d] buffer_prepare - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
  593. buf, buf->vb.i,
  594. fh->width, fh->height, fh->fmt->depth, fh->fmt->name,
  595. (unsigned long)buf->risc.dma);
  596. buf->vb.state = STATE_PREPARED;
  597. return 0;
  598. fail:
  599. cx88_free_buffer(q,buf);
  600. return rc;
  601. }
  602. static void
  603. buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  604. {
  605. struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
  606. struct cx88_buffer *prev;
  607. struct cx8800_fh *fh = vq->priv_data;
  608. struct cx8800_dev *dev = fh->dev;
  609. struct cx88_core *core = dev->core;
  610. struct cx88_dmaqueue *q = &dev->vidq;
  611. /* add jump to stopper */
  612. buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
  613. buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
  614. if (!list_empty(&q->queued)) {
  615. list_add_tail(&buf->vb.queue,&q->queued);
  616. buf->vb.state = STATE_QUEUED;
  617. dprintk(2,"[%p/%d] buffer_queue - append to queued\n",
  618. buf, buf->vb.i);
  619. } else if (list_empty(&q->active)) {
  620. list_add_tail(&buf->vb.queue,&q->active);
  621. start_video_dma(dev, q, buf);
  622. buf->vb.state = STATE_ACTIVE;
  623. buf->count = q->count++;
  624. mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
  625. dprintk(2,"[%p/%d] buffer_queue - first active\n",
  626. buf, buf->vb.i);
  627. } else {
  628. prev = list_entry(q->active.prev, struct cx88_buffer, vb.queue);
  629. if (prev->vb.width == buf->vb.width &&
  630. prev->vb.height == buf->vb.height &&
  631. prev->fmt == buf->fmt) {
  632. list_add_tail(&buf->vb.queue,&q->active);
  633. buf->vb.state = STATE_ACTIVE;
  634. buf->count = q->count++;
  635. prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
  636. dprintk(2,"[%p/%d] buffer_queue - append to active\n",
  637. buf, buf->vb.i);
  638. } else {
  639. list_add_tail(&buf->vb.queue,&q->queued);
  640. buf->vb.state = STATE_QUEUED;
  641. dprintk(2,"[%p/%d] buffer_queue - first queued\n",
  642. buf, buf->vb.i);
  643. }
  644. }
  645. }
  646. static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
  647. {
  648. struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
  649. cx88_free_buffer(q,buf);
  650. }
  651. static struct videobuf_queue_ops cx8800_video_qops = {
  652. .buf_setup = buffer_setup,
  653. .buf_prepare = buffer_prepare,
  654. .buf_queue = buffer_queue,
  655. .buf_release = buffer_release,
  656. };
  657. /* ------------------------------------------------------------------ */
  658. /* ------------------------------------------------------------------ */
  659. static struct videobuf_queue* get_queue(struct cx8800_fh *fh)
  660. {
  661. switch (fh->type) {
  662. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  663. return &fh->vidq;
  664. case V4L2_BUF_TYPE_VBI_CAPTURE:
  665. return &fh->vbiq;
  666. default:
  667. BUG();
  668. return NULL;
  669. }
  670. }
  671. static int get_ressource(struct cx8800_fh *fh)
  672. {
  673. switch (fh->type) {
  674. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  675. return RESOURCE_VIDEO;
  676. case V4L2_BUF_TYPE_VBI_CAPTURE:
  677. return RESOURCE_VBI;
  678. default:
  679. BUG();
  680. return 0;
  681. }
  682. }
  683. static int video_open(struct inode *inode, struct file *file)
  684. {
  685. int minor = iminor(inode);
  686. struct cx8800_dev *h,*dev = NULL;
  687. struct cx88_core *core;
  688. struct cx8800_fh *fh;
  689. struct list_head *list;
  690. enum v4l2_buf_type type = 0;
  691. int radio = 0;
  692. list_for_each(list,&cx8800_devlist) {
  693. h = list_entry(list, struct cx8800_dev, devlist);
  694. if (h->video_dev->minor == minor) {
  695. dev = h;
  696. type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  697. }
  698. if (h->vbi_dev->minor == minor) {
  699. dev = h;
  700. type = V4L2_BUF_TYPE_VBI_CAPTURE;
  701. }
  702. if (h->radio_dev &&
  703. h->radio_dev->minor == minor) {
  704. radio = 1;
  705. dev = h;
  706. }
  707. }
  708. if (NULL == dev)
  709. return -ENODEV;
  710. core = dev->core;
  711. dprintk(1,"open minor=%d radio=%d type=%s\n",
  712. minor,radio,v4l2_type_names[type]);
  713. /* allocate + initialize per filehandle data */
  714. fh = kzalloc(sizeof(*fh),GFP_KERNEL);
  715. if (NULL == fh)
  716. return -ENOMEM;
  717. file->private_data = fh;
  718. fh->dev = dev;
  719. fh->radio = radio;
  720. fh->type = type;
  721. fh->width = 320;
  722. fh->height = 240;
  723. fh->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
  724. videobuf_queue_init(&fh->vidq, &cx8800_video_qops,
  725. dev->pci, &dev->slock,
  726. V4L2_BUF_TYPE_VIDEO_CAPTURE,
  727. V4L2_FIELD_INTERLACED,
  728. sizeof(struct cx88_buffer),
  729. fh);
  730. videobuf_queue_init(&fh->vbiq, &cx8800_vbi_qops,
  731. dev->pci, &dev->slock,
  732. V4L2_BUF_TYPE_VBI_CAPTURE,
  733. V4L2_FIELD_SEQ_TB,
  734. sizeof(struct cx88_buffer),
  735. fh);
  736. if (fh->radio) {
  737. int board = core->board;
  738. dprintk(1,"video_open: setting radio device\n");
  739. cx_write(MO_GP3_IO, cx88_boards[board].radio.gpio3);
  740. cx_write(MO_GP0_IO, cx88_boards[board].radio.gpio0);
  741. cx_write(MO_GP1_IO, cx88_boards[board].radio.gpio1);
  742. cx_write(MO_GP2_IO, cx88_boards[board].radio.gpio2);
  743. core->tvaudio = WW_FM;
  744. cx88_set_tvaudio(core);
  745. cx88_set_stereo(core,V4L2_TUNER_MODE_STEREO,1);
  746. cx88_call_i2c_clients(core,AUDC_SET_RADIO,NULL);
  747. }
  748. return 0;
  749. }
  750. static ssize_t
  751. video_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
  752. {
  753. struct cx8800_fh *fh = file->private_data;
  754. switch (fh->type) {
  755. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  756. if (res_locked(fh->dev,RESOURCE_VIDEO))
  757. return -EBUSY;
  758. return videobuf_read_one(&fh->vidq, data, count, ppos,
  759. file->f_flags & O_NONBLOCK);
  760. case V4L2_BUF_TYPE_VBI_CAPTURE:
  761. if (!res_get(fh->dev,fh,RESOURCE_VBI))
  762. return -EBUSY;
  763. return videobuf_read_stream(&fh->vbiq, data, count, ppos, 1,
  764. file->f_flags & O_NONBLOCK);
  765. default:
  766. BUG();
  767. return 0;
  768. }
  769. }
  770. static unsigned int
  771. video_poll(struct file *file, struct poll_table_struct *wait)
  772. {
  773. struct cx8800_fh *fh = file->private_data;
  774. struct cx88_buffer *buf;
  775. if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
  776. if (!res_get(fh->dev,fh,RESOURCE_VBI))
  777. return POLLERR;
  778. return videobuf_poll_stream(file, &fh->vbiq, wait);
  779. }
  780. if (res_check(fh,RESOURCE_VIDEO)) {
  781. /* streaming capture */
  782. if (list_empty(&fh->vidq.stream))
  783. return POLLERR;
  784. buf = list_entry(fh->vidq.stream.next,struct cx88_buffer,vb.stream);
  785. } else {
  786. /* read() capture */
  787. buf = (struct cx88_buffer*)fh->vidq.read_buf;
  788. if (NULL == buf)
  789. return POLLERR;
  790. }
  791. poll_wait(file, &buf->vb.done, wait);
  792. if (buf->vb.state == STATE_DONE ||
  793. buf->vb.state == STATE_ERROR)
  794. return POLLIN|POLLRDNORM;
  795. return 0;
  796. }
  797. static int video_release(struct inode *inode, struct file *file)
  798. {
  799. struct cx8800_fh *fh = file->private_data;
  800. struct cx8800_dev *dev = fh->dev;
  801. /* turn off overlay */
  802. if (res_check(fh, RESOURCE_OVERLAY)) {
  803. /* FIXME */
  804. res_free(dev,fh,RESOURCE_OVERLAY);
  805. }
  806. /* stop video capture */
  807. if (res_check(fh, RESOURCE_VIDEO)) {
  808. videobuf_queue_cancel(&fh->vidq);
  809. res_free(dev,fh,RESOURCE_VIDEO);
  810. }
  811. if (fh->vidq.read_buf) {
  812. buffer_release(&fh->vidq,fh->vidq.read_buf);
  813. kfree(fh->vidq.read_buf);
  814. }
  815. /* stop vbi capture */
  816. if (res_check(fh, RESOURCE_VBI)) {
  817. if (fh->vbiq.streaming)
  818. videobuf_streamoff(&fh->vbiq);
  819. if (fh->vbiq.reading)
  820. videobuf_read_stop(&fh->vbiq);
  821. res_free(dev,fh,RESOURCE_VBI);
  822. }
  823. videobuf_mmap_free(&fh->vidq);
  824. videobuf_mmap_free(&fh->vbiq);
  825. file->private_data = NULL;
  826. kfree(fh);
  827. cx88_call_i2c_clients (dev->core, TUNER_SET_STANDBY, NULL);
  828. return 0;
  829. }
  830. static int
  831. video_mmap(struct file *file, struct vm_area_struct * vma)
  832. {
  833. struct cx8800_fh *fh = file->private_data;
  834. return videobuf_mmap_mapper(get_queue(fh), vma);
  835. }
  836. /* ------------------------------------------------------------------ */
  837. /* static int get_control(struct cx8800_dev *dev, struct v4l2_control *ctl) */
  838. static int get_control(struct cx88_core *core, struct v4l2_control *ctl)
  839. {
  840. /* struct cx88_core *core = dev->core; */
  841. struct cx88_ctrl *c = NULL;
  842. u32 value;
  843. int i;
  844. for (i = 0; i < CX8800_CTLS; i++)
  845. if (cx8800_ctls[i].v.id == ctl->id)
  846. c = &cx8800_ctls[i];
  847. if (NULL == c)
  848. return -EINVAL;
  849. value = c->sreg ? cx_sread(c->sreg) : cx_read(c->reg);
  850. switch (ctl->id) {
  851. case V4L2_CID_AUDIO_BALANCE:
  852. ctl->value = ((value & 0x7f) < 0x40) ? ((value & 0x7f) + 0x40)
  853. : (0x7f - (value & 0x7f));
  854. break;
  855. case V4L2_CID_AUDIO_VOLUME:
  856. ctl->value = 0x3f - (value & 0x3f);
  857. break;
  858. default:
  859. ctl->value = ((value + (c->off << c->shift)) & c->mask) >> c->shift;
  860. break;
  861. }
  862. dprintk(1,"get_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
  863. ctl->id, c->v.name, ctl->value, c->reg,
  864. value,c->mask, c->sreg ? " [shadowed]" : "");
  865. return 0;
  866. }
  867. /* static int set_control(struct cx8800_dev *dev, struct v4l2_control *ctl) */
  868. static int set_control(struct cx88_core *core, struct v4l2_control *ctl)
  869. {
  870. /* struct cx88_core *core = dev->core; */
  871. struct cx88_ctrl *c = NULL;
  872. u32 value,mask;
  873. int i;
  874. for (i = 0; i < CX8800_CTLS; i++) {
  875. if (cx8800_ctls[i].v.id == ctl->id) {
  876. c = &cx8800_ctls[i];
  877. }
  878. }
  879. if (NULL == c)
  880. return -EINVAL;
  881. if (ctl->value < c->v.minimum)
  882. ctl->value = c->v.minimum;
  883. if (ctl->value > c->v.maximum)
  884. ctl->value = c->v.maximum;
  885. mask=c->mask;
  886. switch (ctl->id) {
  887. case V4L2_CID_AUDIO_BALANCE:
  888. value = (ctl->value < 0x40) ? (0x7f - ctl->value) : (ctl->value - 0x40);
  889. break;
  890. case V4L2_CID_AUDIO_VOLUME:
  891. value = 0x3f - (ctl->value & 0x3f);
  892. break;
  893. case V4L2_CID_SATURATION:
  894. /* special v_sat handling */
  895. value = ((ctl->value - c->off) << c->shift) & c->mask;
  896. if (core->tvnorm->id & V4L2_STD_SECAM) {
  897. /* For SECAM, both U and V sat should be equal */
  898. value=value<<8|value;
  899. } else {
  900. /* Keeps U Saturation proportional to V Sat */
  901. value=(value*0x5a)/0x7f<<8|value;
  902. }
  903. mask=0xffff;
  904. break;
  905. default:
  906. value = ((ctl->value - c->off) << c->shift) & c->mask;
  907. break;
  908. }
  909. dprintk(1,"set_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
  910. ctl->id, c->v.name, ctl->value, c->reg, value,
  911. mask, c->sreg ? " [shadowed]" : "");
  912. if (c->sreg) {
  913. cx_sandor(c->sreg, c->reg, mask, value);
  914. } else {
  915. cx_andor(c->reg, mask, value);
  916. }
  917. return 0;
  918. }
  919. static void init_controls(struct cx88_core *core)
  920. {
  921. struct v4l2_control ctrl;
  922. int i;
  923. for (i = 0; i < CX8800_CTLS; i++) {
  924. ctrl.id=cx8800_ctls[i].v.id;
  925. ctrl.value=cx8800_ctls[i].v.default_value;
  926. set_control(core, &ctrl);
  927. }
  928. }
  929. /* ------------------------------------------------------------------ */
  930. static int cx8800_g_fmt(struct cx8800_dev *dev, struct cx8800_fh *fh,
  931. struct v4l2_format *f)
  932. {
  933. switch (f->type) {
  934. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  935. memset(&f->fmt.pix,0,sizeof(f->fmt.pix));
  936. f->fmt.pix.width = fh->width;
  937. f->fmt.pix.height = fh->height;
  938. f->fmt.pix.field = fh->vidq.field;
  939. f->fmt.pix.pixelformat = fh->fmt->fourcc;
  940. f->fmt.pix.bytesperline =
  941. (f->fmt.pix.width * fh->fmt->depth) >> 3;
  942. f->fmt.pix.sizeimage =
  943. f->fmt.pix.height * f->fmt.pix.bytesperline;
  944. return 0;
  945. case V4L2_BUF_TYPE_VBI_CAPTURE:
  946. cx8800_vbi_fmt(dev, f);
  947. return 0;
  948. default:
  949. return -EINVAL;
  950. }
  951. }
  952. static int cx8800_try_fmt(struct cx8800_dev *dev, struct cx8800_fh *fh,
  953. struct v4l2_format *f)
  954. {
  955. struct cx88_core *core = dev->core;
  956. switch (f->type) {
  957. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  958. {
  959. struct cx8800_fmt *fmt;
  960. enum v4l2_field field;
  961. unsigned int maxw, maxh;
  962. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  963. if (NULL == fmt)
  964. return -EINVAL;
  965. field = f->fmt.pix.field;
  966. maxw = norm_maxw(core->tvnorm);
  967. maxh = norm_maxh(core->tvnorm);
  968. if (V4L2_FIELD_ANY == field) {
  969. field = (f->fmt.pix.height > maxh/2)
  970. ? V4L2_FIELD_INTERLACED
  971. : V4L2_FIELD_BOTTOM;
  972. }
  973. switch (field) {
  974. case V4L2_FIELD_TOP:
  975. case V4L2_FIELD_BOTTOM:
  976. maxh = maxh / 2;
  977. break;
  978. case V4L2_FIELD_INTERLACED:
  979. break;
  980. default:
  981. return -EINVAL;
  982. }
  983. f->fmt.pix.field = field;
  984. if (f->fmt.pix.height < 32)
  985. f->fmt.pix.height = 32;
  986. if (f->fmt.pix.height > maxh)
  987. f->fmt.pix.height = maxh;
  988. if (f->fmt.pix.width < 48)
  989. f->fmt.pix.width = 48;
  990. if (f->fmt.pix.width > maxw)
  991. f->fmt.pix.width = maxw;
  992. f->fmt.pix.width &= ~0x03;
  993. f->fmt.pix.bytesperline =
  994. (f->fmt.pix.width * fmt->depth) >> 3;
  995. f->fmt.pix.sizeimage =
  996. f->fmt.pix.height * f->fmt.pix.bytesperline;
  997. return 0;
  998. }
  999. case V4L2_BUF_TYPE_VBI_CAPTURE:
  1000. cx8800_vbi_fmt(dev, f);
  1001. return 0;
  1002. default:
  1003. return -EINVAL;
  1004. }
  1005. }
  1006. static int cx8800_s_fmt(struct cx8800_dev *dev, struct cx8800_fh *fh,
  1007. struct v4l2_format *f)
  1008. {
  1009. int err;
  1010. switch (f->type) {
  1011. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  1012. err = cx8800_try_fmt(dev,fh,f);
  1013. if (0 != err)
  1014. return err;
  1015. fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  1016. fh->width = f->fmt.pix.width;
  1017. fh->height = f->fmt.pix.height;
  1018. fh->vidq.field = f->fmt.pix.field;
  1019. return 0;
  1020. case V4L2_BUF_TYPE_VBI_CAPTURE:
  1021. cx8800_vbi_fmt(dev, f);
  1022. return 0;
  1023. default:
  1024. return -EINVAL;
  1025. }
  1026. }
  1027. /*
  1028. * This function is _not_ called directly, but from
  1029. * video_generic_ioctl (and maybe others). userspace
  1030. * copying is done already, arg is a kernel pointer.
  1031. */
  1032. static int video_do_ioctl(struct inode *inode, struct file *file,
  1033. unsigned int cmd, void *arg)
  1034. {
  1035. struct cx8800_fh *fh = file->private_data;
  1036. struct cx8800_dev *dev = fh->dev;
  1037. struct cx88_core *core = dev->core;
  1038. int err;
  1039. if (video_debug > 1)
  1040. v4l_print_ioctl(core->name,cmd);
  1041. switch (cmd) {
  1042. /* --- capabilities ------------------------------------------ */
  1043. case VIDIOC_QUERYCAP:
  1044. {
  1045. struct v4l2_capability *cap = arg;
  1046. memset(cap,0,sizeof(*cap));
  1047. strcpy(cap->driver, "cx8800");
  1048. strlcpy(cap->card, cx88_boards[core->board].name,
  1049. sizeof(cap->card));
  1050. sprintf(cap->bus_info,"PCI:%s",pci_name(dev->pci));
  1051. cap->version = CX88_VERSION_CODE;
  1052. cap->capabilities =
  1053. V4L2_CAP_VIDEO_CAPTURE |
  1054. V4L2_CAP_READWRITE |
  1055. V4L2_CAP_STREAMING |
  1056. V4L2_CAP_VBI_CAPTURE |
  1057. 0;
  1058. if (UNSET != core->tuner_type)
  1059. cap->capabilities |= V4L2_CAP_TUNER;
  1060. return 0;
  1061. }
  1062. /* --- capture ioctls ---------------------------------------- */
  1063. case VIDIOC_ENUM_FMT:
  1064. {
  1065. struct v4l2_fmtdesc *f = arg;
  1066. enum v4l2_buf_type type;
  1067. unsigned int index;
  1068. index = f->index;
  1069. type = f->type;
  1070. switch (type) {
  1071. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  1072. if (index >= ARRAY_SIZE(formats))
  1073. return -EINVAL;
  1074. memset(f,0,sizeof(*f));
  1075. f->index = index;
  1076. f->type = type;
  1077. strlcpy(f->description,formats[index].name,sizeof(f->description));
  1078. f->pixelformat = formats[index].fourcc;
  1079. break;
  1080. default:
  1081. return -EINVAL;
  1082. }
  1083. return 0;
  1084. }
  1085. case VIDIOC_G_FMT:
  1086. {
  1087. struct v4l2_format *f = arg;
  1088. return cx8800_g_fmt(dev,fh,f);
  1089. }
  1090. case VIDIOC_S_FMT:
  1091. {
  1092. struct v4l2_format *f = arg;
  1093. return cx8800_s_fmt(dev,fh,f);
  1094. }
  1095. case VIDIOC_TRY_FMT:
  1096. {
  1097. struct v4l2_format *f = arg;
  1098. return cx8800_try_fmt(dev,fh,f);
  1099. }
  1100. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  1101. /* --- streaming capture ------------------------------------- */
  1102. case VIDIOCGMBUF:
  1103. {
  1104. struct video_mbuf *mbuf = arg;
  1105. struct videobuf_queue *q;
  1106. struct v4l2_requestbuffers req;
  1107. unsigned int i;
  1108. q = get_queue(fh);
  1109. memset(&req,0,sizeof(req));
  1110. req.type = q->type;
  1111. req.count = 8;
  1112. req.memory = V4L2_MEMORY_MMAP;
  1113. err = videobuf_reqbufs(q,&req);
  1114. if (err < 0)
  1115. return err;
  1116. memset(mbuf,0,sizeof(*mbuf));
  1117. mbuf->frames = req.count;
  1118. mbuf->size = 0;
  1119. for (i = 0; i < mbuf->frames; i++) {
  1120. mbuf->offsets[i] = q->bufs[i]->boff;
  1121. mbuf->size += q->bufs[i]->bsize;
  1122. }
  1123. return 0;
  1124. }
  1125. #endif
  1126. case VIDIOC_REQBUFS:
  1127. return videobuf_reqbufs(get_queue(fh), arg);
  1128. case VIDIOC_QUERYBUF:
  1129. return videobuf_querybuf(get_queue(fh), arg);
  1130. case VIDIOC_QBUF:
  1131. return videobuf_qbuf(get_queue(fh), arg);
  1132. case VIDIOC_DQBUF:
  1133. return videobuf_dqbuf(get_queue(fh), arg,
  1134. file->f_flags & O_NONBLOCK);
  1135. case VIDIOC_STREAMON:
  1136. {
  1137. int res = get_ressource(fh);
  1138. if (!res_get(dev,fh,res))
  1139. return -EBUSY;
  1140. return videobuf_streamon(get_queue(fh));
  1141. }
  1142. case VIDIOC_STREAMOFF:
  1143. {
  1144. int res = get_ressource(fh);
  1145. err = videobuf_streamoff(get_queue(fh));
  1146. if (err < 0)
  1147. return err;
  1148. res_free(dev,fh,res);
  1149. return 0;
  1150. }
  1151. default:
  1152. return cx88_do_ioctl( inode, file, fh->radio, core, cmd, arg, video_do_ioctl );
  1153. }
  1154. return 0;
  1155. }
  1156. int cx88_do_ioctl(struct inode *inode, struct file *file, int radio,
  1157. struct cx88_core *core, unsigned int cmd, void *arg, v4l2_kioctl driver_ioctl)
  1158. {
  1159. int err;
  1160. if (video_debug) {
  1161. if (video_debug > 1) {
  1162. if (_IOC_DIR(cmd) & _IOC_WRITE)
  1163. v4l_printk_ioctl_arg("cx88(w)",cmd, arg);
  1164. else if (!_IOC_DIR(cmd) & _IOC_READ) {
  1165. v4l_print_ioctl("cx88", cmd);
  1166. }
  1167. } else
  1168. v4l_print_ioctl(core->name,cmd);
  1169. }
  1170. switch (cmd) {
  1171. /* ---------- tv norms ---------- */
  1172. case VIDIOC_ENUMSTD:
  1173. {
  1174. struct v4l2_standard *e = arg;
  1175. unsigned int i;
  1176. i = e->index;
  1177. if (i >= ARRAY_SIZE(tvnorms))
  1178. return -EINVAL;
  1179. err = v4l2_video_std_construct(e, tvnorms[e->index].id,
  1180. tvnorms[e->index].name);
  1181. e->index = i;
  1182. if (err < 0)
  1183. return err;
  1184. return 0;
  1185. }
  1186. case VIDIOC_G_STD:
  1187. {
  1188. v4l2_std_id *id = arg;
  1189. *id = core->tvnorm->id;
  1190. return 0;
  1191. }
  1192. case VIDIOC_S_STD:
  1193. {
  1194. v4l2_std_id *id = arg;
  1195. unsigned int i;
  1196. for(i = 0; i < ARRAY_SIZE(tvnorms); i++)
  1197. if (*id & tvnorms[i].id)
  1198. break;
  1199. if (i == ARRAY_SIZE(tvnorms))
  1200. return -EINVAL;
  1201. mutex_lock(&core->lock);
  1202. cx88_set_tvnorm(core,&tvnorms[i]);
  1203. mutex_unlock(&core->lock);
  1204. return 0;
  1205. }
  1206. /* ------ input switching ---------- */
  1207. case VIDIOC_ENUMINPUT:
  1208. {
  1209. static const char *iname[] = {
  1210. [ CX88_VMUX_COMPOSITE1 ] = "Composite1",
  1211. [ CX88_VMUX_COMPOSITE2 ] = "Composite2",
  1212. [ CX88_VMUX_COMPOSITE3 ] = "Composite3",
  1213. [ CX88_VMUX_COMPOSITE4 ] = "Composite4",
  1214. [ CX88_VMUX_SVIDEO ] = "S-Video",
  1215. [ CX88_VMUX_TELEVISION ] = "Television",
  1216. [ CX88_VMUX_CABLE ] = "Cable TV",
  1217. [ CX88_VMUX_DVB ] = "DVB",
  1218. [ CX88_VMUX_DEBUG ] = "for debug only",
  1219. };
  1220. struct v4l2_input *i = arg;
  1221. unsigned int n;
  1222. n = i->index;
  1223. if (n >= 4)
  1224. return -EINVAL;
  1225. if (0 == INPUT(n)->type)
  1226. return -EINVAL;
  1227. memset(i,0,sizeof(*i));
  1228. i->index = n;
  1229. i->type = V4L2_INPUT_TYPE_CAMERA;
  1230. strcpy(i->name,iname[INPUT(n)->type]);
  1231. if ((CX88_VMUX_TELEVISION == INPUT(n)->type) ||
  1232. (CX88_VMUX_CABLE == INPUT(n)->type))
  1233. i->type = V4L2_INPUT_TYPE_TUNER;
  1234. for (n = 0; n < ARRAY_SIZE(tvnorms); n++)
  1235. i->std |= tvnorms[n].id;
  1236. return 0;
  1237. }
  1238. case VIDIOC_G_INPUT:
  1239. {
  1240. unsigned int *i = arg;
  1241. *i = core->input;
  1242. return 0;
  1243. }
  1244. case VIDIOC_S_INPUT:
  1245. {
  1246. unsigned int *i = arg;
  1247. if (*i >= 4)
  1248. return -EINVAL;
  1249. mutex_lock(&core->lock);
  1250. cx88_newstation(core);
  1251. video_mux(core,*i);
  1252. mutex_unlock(&core->lock);
  1253. return 0;
  1254. }
  1255. /* --- controls ---------------------------------------------- */
  1256. case VIDIOC_QUERYCTRL:
  1257. {
  1258. struct v4l2_queryctrl *c = arg;
  1259. return cx88_queryctrl(c);
  1260. }
  1261. case VIDIOC_G_CTRL:
  1262. return get_control(core,arg);
  1263. case VIDIOC_S_CTRL:
  1264. return set_control(core,arg);
  1265. /* --- tuner ioctls ------------------------------------------ */
  1266. case VIDIOC_G_TUNER:
  1267. {
  1268. struct v4l2_tuner *t = arg;
  1269. u32 reg;
  1270. if (UNSET == core->tuner_type)
  1271. return -EINVAL;
  1272. if (0 != t->index)
  1273. return -EINVAL;
  1274. memset(t,0,sizeof(*t));
  1275. strcpy(t->name, "Television");
  1276. t->type = V4L2_TUNER_ANALOG_TV;
  1277. t->capability = V4L2_TUNER_CAP_NORM;
  1278. t->rangehigh = 0xffffffffUL;
  1279. cx88_get_stereo(core ,t);
  1280. reg = cx_read(MO_DEVICE_STATUS);
  1281. t->signal = (reg & (1<<5)) ? 0xffff : 0x0000;
  1282. return 0;
  1283. }
  1284. case VIDIOC_S_TUNER:
  1285. {
  1286. struct v4l2_tuner *t = arg;
  1287. if (UNSET == core->tuner_type)
  1288. return -EINVAL;
  1289. if (0 != t->index)
  1290. return -EINVAL;
  1291. cx88_set_stereo(core, t->audmode, 1);
  1292. return 0;
  1293. }
  1294. case VIDIOC_G_FREQUENCY:
  1295. {
  1296. struct v4l2_frequency *f = arg;
  1297. memset(f,0,sizeof(*f));
  1298. if (UNSET == core->tuner_type)
  1299. return -EINVAL;
  1300. /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
  1301. f->type = radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  1302. f->frequency = core->freq;
  1303. cx88_call_i2c_clients(core,VIDIOC_G_FREQUENCY,f);
  1304. return 0;
  1305. }
  1306. case VIDIOC_S_FREQUENCY:
  1307. {
  1308. struct v4l2_frequency *f = arg;
  1309. if (UNSET == core->tuner_type)
  1310. return -EINVAL;
  1311. if (f->tuner != 0)
  1312. return -EINVAL;
  1313. if (0 == radio && f->type != V4L2_TUNER_ANALOG_TV)
  1314. return -EINVAL;
  1315. if (1 == radio && f->type != V4L2_TUNER_RADIO)
  1316. return -EINVAL;
  1317. mutex_lock(&core->lock);
  1318. core->freq = f->frequency;
  1319. cx88_newstation(core);
  1320. cx88_call_i2c_clients(core,VIDIOC_S_FREQUENCY,f);
  1321. /* When changing channels it is required to reset TVAUDIO */
  1322. msleep (10);
  1323. cx88_set_tvaudio(core);
  1324. mutex_unlock(&core->lock);
  1325. return 0;
  1326. }
  1327. default:
  1328. return v4l_compat_translate_ioctl(inode,file,cmd,arg,
  1329. driver_ioctl);
  1330. }
  1331. return 0;
  1332. }
  1333. static int video_ioctl(struct inode *inode, struct file *file,
  1334. unsigned int cmd, unsigned long arg)
  1335. {
  1336. int retval;
  1337. retval=video_usercopy(inode, file, cmd, arg, video_do_ioctl);
  1338. if (video_debug > 1) {
  1339. if (retval < 0) {
  1340. v4l_print_ioctl("cx88(err)", cmd);
  1341. printk(KERN_DEBUG "cx88(err): errcode=%d\n",retval);
  1342. } else if (_IOC_DIR(cmd) & _IOC_READ)
  1343. v4l_printk_ioctl_arg("cx88(r)",cmd, (void *)arg);
  1344. }
  1345. return retval;
  1346. }
  1347. /* ----------------------------------------------------------- */
  1348. static int radio_do_ioctl(struct inode *inode, struct file *file,
  1349. unsigned int cmd, void *arg)
  1350. {
  1351. struct cx8800_fh *fh = file->private_data;
  1352. struct cx8800_dev *dev = fh->dev;
  1353. struct cx88_core *core = dev->core;
  1354. if (video_debug > 1)
  1355. v4l_print_ioctl(core->name,cmd);
  1356. switch (cmd) {
  1357. case VIDIOC_QUERYCAP:
  1358. {
  1359. struct v4l2_capability *cap = arg;
  1360. memset(cap,0,sizeof(*cap));
  1361. strcpy(cap->driver, "cx8800");
  1362. strlcpy(cap->card, cx88_boards[core->board].name,
  1363. sizeof(cap->card));
  1364. sprintf(cap->bus_info,"PCI:%s", pci_name(dev->pci));
  1365. cap->version = CX88_VERSION_CODE;
  1366. cap->capabilities = V4L2_CAP_TUNER;
  1367. return 0;
  1368. }
  1369. case VIDIOC_G_TUNER:
  1370. {
  1371. struct v4l2_tuner *t = arg;
  1372. if (t->index > 0)
  1373. return -EINVAL;
  1374. memset(t,0,sizeof(*t));
  1375. strcpy(t->name, "Radio");
  1376. t->type = V4L2_TUNER_RADIO;
  1377. cx88_call_i2c_clients(core,VIDIOC_G_TUNER,t);
  1378. return 0;
  1379. }
  1380. case VIDIOC_ENUMINPUT:
  1381. {
  1382. struct v4l2_input *i = arg;
  1383. if (i->index != 0)
  1384. return -EINVAL;
  1385. strcpy(i->name,"Radio");
  1386. i->type = V4L2_INPUT_TYPE_TUNER;
  1387. return 0;
  1388. }
  1389. case VIDIOC_G_INPUT:
  1390. {
  1391. int *i = arg;
  1392. *i = 0;
  1393. return 0;
  1394. }
  1395. case VIDIOC_G_AUDIO:
  1396. {
  1397. struct v4l2_audio *a = arg;
  1398. memset(a,0,sizeof(*a));
  1399. strcpy(a->name,"Radio");
  1400. return 0;
  1401. }
  1402. case VIDIOC_G_STD:
  1403. {
  1404. v4l2_std_id *id = arg;
  1405. *id = 0;
  1406. return 0;
  1407. }
  1408. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  1409. case VIDIOCSTUNER:
  1410. {
  1411. struct video_tuner *v = arg;
  1412. if (v->tuner) /* Only tuner 0 */
  1413. return -EINVAL;
  1414. cx88_call_i2c_clients(core,VIDIOCSTUNER,v);
  1415. return 0;
  1416. }
  1417. #endif
  1418. case VIDIOC_S_TUNER:
  1419. {
  1420. struct v4l2_tuner *t = arg;
  1421. if (0 != t->index)
  1422. return -EINVAL;
  1423. cx88_call_i2c_clients(core,VIDIOC_S_TUNER,t);
  1424. return 0;
  1425. }
  1426. case VIDIOC_S_AUDIO:
  1427. case VIDIOC_S_INPUT:
  1428. case VIDIOC_S_STD:
  1429. return 0;
  1430. case VIDIOC_QUERYCTRL:
  1431. {
  1432. struct v4l2_queryctrl *c = arg;
  1433. int i;
  1434. if (c->id < V4L2_CID_BASE ||
  1435. c->id >= V4L2_CID_LASTP1)
  1436. return -EINVAL;
  1437. if (c->id == V4L2_CID_AUDIO_MUTE) {
  1438. for (i = 0; i < CX8800_CTLS; i++)
  1439. if (cx8800_ctls[i].v.id == c->id)
  1440. break;
  1441. *c = cx8800_ctls[i].v;
  1442. } else
  1443. *c = no_ctl;
  1444. return 0;
  1445. }
  1446. case VIDIOC_G_CTRL:
  1447. case VIDIOC_S_CTRL:
  1448. case VIDIOC_G_FREQUENCY:
  1449. case VIDIOC_S_FREQUENCY:
  1450. return video_do_ioctl(inode,file,cmd,arg);
  1451. default:
  1452. return v4l_compat_translate_ioctl(inode,file,cmd,arg,
  1453. radio_do_ioctl);
  1454. }
  1455. return 0;
  1456. };
  1457. static int radio_ioctl(struct inode *inode, struct file *file,
  1458. unsigned int cmd, unsigned long arg)
  1459. {
  1460. return video_usercopy(inode, file, cmd, arg, radio_do_ioctl);
  1461. };
  1462. /* ----------------------------------------------------------- */
  1463. static void cx8800_vid_timeout(unsigned long data)
  1464. {
  1465. struct cx8800_dev *dev = (struct cx8800_dev*)data;
  1466. struct cx88_core *core = dev->core;
  1467. struct cx88_dmaqueue *q = &dev->vidq;
  1468. struct cx88_buffer *buf;
  1469. unsigned long flags;
  1470. cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
  1471. cx_clear(MO_VID_DMACNTRL, 0x11);
  1472. cx_clear(VID_CAPTURE_CONTROL, 0x06);
  1473. spin_lock_irqsave(&dev->slock,flags);
  1474. while (!list_empty(&q->active)) {
  1475. buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
  1476. list_del(&buf->vb.queue);
  1477. buf->vb.state = STATE_ERROR;
  1478. wake_up(&buf->vb.done);
  1479. printk("%s/0: [%p/%d] timeout - dma=0x%08lx\n", core->name,
  1480. buf, buf->vb.i, (unsigned long)buf->risc.dma);
  1481. }
  1482. restart_video_queue(dev,q);
  1483. spin_unlock_irqrestore(&dev->slock,flags);
  1484. }
  1485. static char *cx88_vid_irqs[32] = {
  1486. "y_risci1", "u_risci1", "v_risci1", "vbi_risc1",
  1487. "y_risci2", "u_risci2", "v_risci2", "vbi_risc2",
  1488. "y_oflow", "u_oflow", "v_oflow", "vbi_oflow",
  1489. "y_sync", "u_sync", "v_sync", "vbi_sync",
  1490. "opc_err", "par_err", "rip_err", "pci_abort",
  1491. };
  1492. static void cx8800_vid_irq(struct cx8800_dev *dev)
  1493. {
  1494. struct cx88_core *core = dev->core;
  1495. u32 status, mask, count;
  1496. status = cx_read(MO_VID_INTSTAT);
  1497. mask = cx_read(MO_VID_INTMSK);
  1498. if (0 == (status & mask))
  1499. return;
  1500. cx_write(MO_VID_INTSTAT, status);
  1501. if (irq_debug || (status & mask & ~0xff))
  1502. cx88_print_irqbits(core->name, "irq vid",
  1503. cx88_vid_irqs, status, mask);
  1504. /* risc op code error */
  1505. if (status & (1 << 16)) {
  1506. printk(KERN_WARNING "%s/0: video risc op code error\n",core->name);
  1507. cx_clear(MO_VID_DMACNTRL, 0x11);
  1508. cx_clear(VID_CAPTURE_CONTROL, 0x06);
  1509. cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
  1510. }
  1511. /* risc1 y */
  1512. if (status & 0x01) {
  1513. spin_lock(&dev->slock);
  1514. count = cx_read(MO_VIDY_GPCNT);
  1515. cx88_wakeup(core, &dev->vidq, count);
  1516. spin_unlock(&dev->slock);
  1517. }
  1518. /* risc1 vbi */
  1519. if (status & 0x08) {
  1520. spin_lock(&dev->slock);
  1521. count = cx_read(MO_VBI_GPCNT);
  1522. cx88_wakeup(core, &dev->vbiq, count);
  1523. spin_unlock(&dev->slock);
  1524. }
  1525. /* risc2 y */
  1526. if (status & 0x10) {
  1527. dprintk(2,"stopper video\n");
  1528. spin_lock(&dev->slock);
  1529. restart_video_queue(dev,&dev->vidq);
  1530. spin_unlock(&dev->slock);
  1531. }
  1532. /* risc2 vbi */
  1533. if (status & 0x80) {
  1534. dprintk(2,"stopper vbi\n");
  1535. spin_lock(&dev->slock);
  1536. cx8800_restart_vbi_queue(dev,&dev->vbiq);
  1537. spin_unlock(&dev->slock);
  1538. }
  1539. }
  1540. static irqreturn_t cx8800_irq(int irq, void *dev_id, struct pt_regs *regs)
  1541. {
  1542. struct cx8800_dev *dev = dev_id;
  1543. struct cx88_core *core = dev->core;
  1544. u32 status;
  1545. int loop, handled = 0;
  1546. for (loop = 0; loop < 10; loop++) {
  1547. status = cx_read(MO_PCI_INTSTAT) & (core->pci_irqmask | 0x01);
  1548. if (0 == status)
  1549. goto out;
  1550. cx_write(MO_PCI_INTSTAT, status);
  1551. handled = 1;
  1552. if (status & core->pci_irqmask)
  1553. cx88_core_irq(core,status);
  1554. if (status & 0x01)
  1555. cx8800_vid_irq(dev);
  1556. };
  1557. if (10 == loop) {
  1558. printk(KERN_WARNING "%s/0: irq loop -- clearing mask\n",
  1559. core->name);
  1560. cx_write(MO_PCI_INTMSK,0);
  1561. }
  1562. out:
  1563. return IRQ_RETVAL(handled);
  1564. }
  1565. /* ----------------------------------------------------------- */
  1566. /* exported stuff */
  1567. static struct file_operations video_fops =
  1568. {
  1569. .owner = THIS_MODULE,
  1570. .open = video_open,
  1571. .release = video_release,
  1572. .read = video_read,
  1573. .poll = video_poll,
  1574. .mmap = video_mmap,
  1575. .ioctl = video_ioctl,
  1576. .compat_ioctl = v4l_compat_ioctl32,
  1577. .llseek = no_llseek,
  1578. };
  1579. static struct video_device cx8800_video_template =
  1580. {
  1581. .name = "cx8800-video",
  1582. .type = VID_TYPE_CAPTURE|VID_TYPE_TUNER|VID_TYPE_SCALES,
  1583. .hardware = 0,
  1584. .fops = &video_fops,
  1585. .minor = -1,
  1586. };
  1587. static struct video_device cx8800_vbi_template =
  1588. {
  1589. .name = "cx8800-vbi",
  1590. .type = VID_TYPE_TELETEXT|VID_TYPE_TUNER,
  1591. .hardware = 0,
  1592. .fops = &video_fops,
  1593. .minor = -1,
  1594. };
  1595. static struct file_operations radio_fops =
  1596. {
  1597. .owner = THIS_MODULE,
  1598. .open = video_open,
  1599. .release = video_release,
  1600. .ioctl = radio_ioctl,
  1601. .compat_ioctl = v4l_compat_ioctl32,
  1602. .llseek = no_llseek,
  1603. };
  1604. static struct video_device cx8800_radio_template =
  1605. {
  1606. .name = "cx8800-radio",
  1607. .type = VID_TYPE_TUNER,
  1608. .hardware = 0,
  1609. .fops = &radio_fops,
  1610. .minor = -1,
  1611. };
  1612. /* ----------------------------------------------------------- */
  1613. static void cx8800_unregister_video(struct cx8800_dev *dev)
  1614. {
  1615. if (dev->radio_dev) {
  1616. if (-1 != dev->radio_dev->minor)
  1617. video_unregister_device(dev->radio_dev);
  1618. else
  1619. video_device_release(dev->radio_dev);
  1620. dev->radio_dev = NULL;
  1621. }
  1622. if (dev->vbi_dev) {
  1623. if (-1 != dev->vbi_dev->minor)
  1624. video_unregister_device(dev->vbi_dev);
  1625. else
  1626. video_device_release(dev->vbi_dev);
  1627. dev->vbi_dev = NULL;
  1628. }
  1629. if (dev->video_dev) {
  1630. if (-1 != dev->video_dev->minor)
  1631. video_unregister_device(dev->video_dev);
  1632. else
  1633. video_device_release(dev->video_dev);
  1634. dev->video_dev = NULL;
  1635. }
  1636. }
  1637. static int __devinit cx8800_initdev(struct pci_dev *pci_dev,
  1638. const struct pci_device_id *pci_id)
  1639. {
  1640. struct cx8800_dev *dev;
  1641. struct cx88_core *core;
  1642. int err;
  1643. dev = kzalloc(sizeof(*dev),GFP_KERNEL);
  1644. if (NULL == dev)
  1645. return -ENOMEM;
  1646. /* pci init */
  1647. dev->pci = pci_dev;
  1648. if (pci_enable_device(pci_dev)) {
  1649. err = -EIO;
  1650. goto fail_free;
  1651. }
  1652. core = cx88_core_get(dev->pci);
  1653. if (NULL == core) {
  1654. err = -EINVAL;
  1655. goto fail_free;
  1656. }
  1657. dev->core = core;
  1658. /* print pci info */
  1659. pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &dev->pci_rev);
  1660. pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &dev->pci_lat);
  1661. printk(KERN_INFO "%s/0: found at %s, rev: %d, irq: %d, "
  1662. "latency: %d, mmio: 0x%llx\n", core->name,
  1663. pci_name(pci_dev), dev->pci_rev, pci_dev->irq,
  1664. dev->pci_lat,(unsigned long long)pci_resource_start(pci_dev,0));
  1665. pci_set_master(pci_dev);
  1666. if (!pci_dma_supported(pci_dev,0xffffffff)) {
  1667. printk("%s/0: Oops: no 32bit PCI DMA ???\n",core->name);
  1668. err = -EIO;
  1669. goto fail_core;
  1670. }
  1671. /* initialize driver struct */
  1672. spin_lock_init(&dev->slock);
  1673. core->tvnorm = tvnorms;
  1674. /* init video dma queues */
  1675. INIT_LIST_HEAD(&dev->vidq.active);
  1676. INIT_LIST_HEAD(&dev->vidq.queued);
  1677. dev->vidq.timeout.function = cx8800_vid_timeout;
  1678. dev->vidq.timeout.data = (unsigned long)dev;
  1679. init_timer(&dev->vidq.timeout);
  1680. cx88_risc_stopper(dev->pci,&dev->vidq.stopper,
  1681. MO_VID_DMACNTRL,0x11,0x00);
  1682. /* init vbi dma queues */
  1683. INIT_LIST_HEAD(&dev->vbiq.active);
  1684. INIT_LIST_HEAD(&dev->vbiq.queued);
  1685. dev->vbiq.timeout.function = cx8800_vbi_timeout;
  1686. dev->vbiq.timeout.data = (unsigned long)dev;
  1687. init_timer(&dev->vbiq.timeout);
  1688. cx88_risc_stopper(dev->pci,&dev->vbiq.stopper,
  1689. MO_VID_DMACNTRL,0x88,0x00);
  1690. /* get irq */
  1691. err = request_irq(pci_dev->irq, cx8800_irq,
  1692. IRQF_SHARED | IRQF_DISABLED, core->name, dev);
  1693. if (err < 0) {
  1694. printk(KERN_ERR "%s: can't get IRQ %d\n",
  1695. core->name,pci_dev->irq);
  1696. goto fail_core;
  1697. }
  1698. cx_set(MO_PCI_INTMSK, core->pci_irqmask);
  1699. /* load and configure helper modules */
  1700. if (TUNER_ABSENT != core->tuner_type)
  1701. request_module("tuner");
  1702. if (cx88_boards[ core->board ].audio_chip == AUDIO_CHIP_WM8775)
  1703. request_module("wm8775");
  1704. /* register v4l devices */
  1705. dev->video_dev = cx88_vdev_init(core,dev->pci,
  1706. &cx8800_video_template,"video");
  1707. err = video_register_device(dev->video_dev,VFL_TYPE_GRABBER,
  1708. video_nr[core->nr]);
  1709. if (err < 0) {
  1710. printk(KERN_INFO "%s: can't register video device\n",
  1711. core->name);
  1712. goto fail_unreg;
  1713. }
  1714. printk(KERN_INFO "%s/0: registered device video%d [v4l2]\n",
  1715. core->name,dev->video_dev->minor & 0x1f);
  1716. dev->vbi_dev = cx88_vdev_init(core,dev->pci,&cx8800_vbi_template,"vbi");
  1717. err = video_register_device(dev->vbi_dev,VFL_TYPE_VBI,
  1718. vbi_nr[core->nr]);
  1719. if (err < 0) {
  1720. printk(KERN_INFO "%s/0: can't register vbi device\n",
  1721. core->name);
  1722. goto fail_unreg;
  1723. }
  1724. printk(KERN_INFO "%s/0: registered device vbi%d\n",
  1725. core->name,dev->vbi_dev->minor & 0x1f);
  1726. if (core->has_radio) {
  1727. dev->radio_dev = cx88_vdev_init(core,dev->pci,
  1728. &cx8800_radio_template,"radio");
  1729. err = video_register_device(dev->radio_dev,VFL_TYPE_RADIO,
  1730. radio_nr[core->nr]);
  1731. if (err < 0) {
  1732. printk(KERN_INFO "%s/0: can't register radio device\n",
  1733. core->name);
  1734. goto fail_unreg;
  1735. }
  1736. printk(KERN_INFO "%s/0: registered device radio%d\n",
  1737. core->name,dev->radio_dev->minor & 0x1f);
  1738. }
  1739. /* everything worked */
  1740. list_add_tail(&dev->devlist,&cx8800_devlist);
  1741. pci_set_drvdata(pci_dev,dev);
  1742. /* initial device configuration */
  1743. mutex_lock(&core->lock);
  1744. cx88_set_tvnorm(core,tvnorms);
  1745. init_controls(core);
  1746. video_mux(core,0);
  1747. mutex_unlock(&core->lock);
  1748. /* start tvaudio thread */
  1749. if (core->tuner_type != TUNER_ABSENT)
  1750. core->kthread = kthread_run(cx88_audio_thread, core, "cx88 tvaudio");
  1751. return 0;
  1752. fail_unreg:
  1753. cx8800_unregister_video(dev);
  1754. free_irq(pci_dev->irq, dev);
  1755. fail_core:
  1756. cx88_core_put(core,dev->pci);
  1757. fail_free:
  1758. kfree(dev);
  1759. return err;
  1760. }
  1761. static void __devexit cx8800_finidev(struct pci_dev *pci_dev)
  1762. {
  1763. struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
  1764. struct cx88_core *core = dev->core;
  1765. /* stop thread */
  1766. if (core->kthread) {
  1767. kthread_stop(core->kthread);
  1768. core->kthread = NULL;
  1769. }
  1770. cx88_shutdown(core); /* FIXME */
  1771. pci_disable_device(pci_dev);
  1772. /* unregister stuff */
  1773. free_irq(pci_dev->irq, dev);
  1774. cx8800_unregister_video(dev);
  1775. pci_set_drvdata(pci_dev, NULL);
  1776. /* free memory */
  1777. btcx_riscmem_free(dev->pci,&dev->vidq.stopper);
  1778. list_del(&dev->devlist);
  1779. cx88_core_put(core,dev->pci);
  1780. kfree(dev);
  1781. }
  1782. #ifdef CONFIG_PM
  1783. static int cx8800_suspend(struct pci_dev *pci_dev, pm_message_t state)
  1784. {
  1785. struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
  1786. struct cx88_core *core = dev->core;
  1787. /* stop video+vbi capture */
  1788. spin_lock(&dev->slock);
  1789. if (!list_empty(&dev->vidq.active)) {
  1790. printk("%s: suspend video\n", core->name);
  1791. stop_video_dma(dev);
  1792. del_timer(&dev->vidq.timeout);
  1793. }
  1794. if (!list_empty(&dev->vbiq.active)) {
  1795. printk("%s: suspend vbi\n", core->name);
  1796. cx8800_stop_vbi_dma(dev);
  1797. del_timer(&dev->vbiq.timeout);
  1798. }
  1799. spin_unlock(&dev->slock);
  1800. /* FIXME -- shutdown device */
  1801. cx88_shutdown(core);
  1802. pci_save_state(pci_dev);
  1803. if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
  1804. pci_disable_device(pci_dev);
  1805. dev->state.disabled = 1;
  1806. }
  1807. return 0;
  1808. }
  1809. static int cx8800_resume(struct pci_dev *pci_dev)
  1810. {
  1811. struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
  1812. struct cx88_core *core = dev->core;
  1813. int err;
  1814. if (dev->state.disabled) {
  1815. err=pci_enable_device(pci_dev);
  1816. if (err) {
  1817. printk(KERN_ERR "%s: can't enable device\n",
  1818. core->name);
  1819. return err;
  1820. }
  1821. dev->state.disabled = 0;
  1822. }
  1823. err= pci_set_power_state(pci_dev, PCI_D0);
  1824. if (err) {
  1825. printk(KERN_ERR "%s: can't enable device\n",
  1826. core->name);
  1827. pci_disable_device(pci_dev);
  1828. dev->state.disabled = 1;
  1829. return err;
  1830. }
  1831. pci_restore_state(pci_dev);
  1832. /* FIXME: re-initialize hardware */
  1833. cx88_reset(core);
  1834. /* restart video+vbi capture */
  1835. spin_lock(&dev->slock);
  1836. if (!list_empty(&dev->vidq.active)) {
  1837. printk("%s: resume video\n", core->name);
  1838. restart_video_queue(dev,&dev->vidq);
  1839. }
  1840. if (!list_empty(&dev->vbiq.active)) {
  1841. printk("%s: resume vbi\n", core->name);
  1842. cx8800_restart_vbi_queue(dev,&dev->vbiq);
  1843. }
  1844. spin_unlock(&dev->slock);
  1845. return 0;
  1846. }
  1847. #endif
  1848. /* ----------------------------------------------------------- */
  1849. static struct pci_device_id cx8800_pci_tbl[] = {
  1850. {
  1851. .vendor = 0x14f1,
  1852. .device = 0x8800,
  1853. .subvendor = PCI_ANY_ID,
  1854. .subdevice = PCI_ANY_ID,
  1855. },{
  1856. /* --- end of list --- */
  1857. }
  1858. };
  1859. MODULE_DEVICE_TABLE(pci, cx8800_pci_tbl);
  1860. static struct pci_driver cx8800_pci_driver = {
  1861. .name = "cx8800",
  1862. .id_table = cx8800_pci_tbl,
  1863. .probe = cx8800_initdev,
  1864. .remove = __devexit_p(cx8800_finidev),
  1865. #ifdef CONFIG_PM
  1866. .suspend = cx8800_suspend,
  1867. .resume = cx8800_resume,
  1868. #endif
  1869. };
  1870. static int cx8800_init(void)
  1871. {
  1872. printk(KERN_INFO "cx2388x v4l2 driver version %d.%d.%d loaded\n",
  1873. (CX88_VERSION_CODE >> 16) & 0xff,
  1874. (CX88_VERSION_CODE >> 8) & 0xff,
  1875. CX88_VERSION_CODE & 0xff);
  1876. #ifdef SNAPSHOT
  1877. printk(KERN_INFO "cx2388x: snapshot date %04d-%02d-%02d\n",
  1878. SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
  1879. #endif
  1880. return pci_register_driver(&cx8800_pci_driver);
  1881. }
  1882. static void cx8800_fini(void)
  1883. {
  1884. pci_unregister_driver(&cx8800_pci_driver);
  1885. }
  1886. module_init(cx8800_init);
  1887. module_exit(cx8800_fini);
  1888. EXPORT_SYMBOL(cx88_do_ioctl);
  1889. /* ----------------------------------------------------------- */
  1890. /*
  1891. * Local variables:
  1892. * c-basic-offset: 8
  1893. * End:
  1894. * 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
  1895. */