cx88-video.c 56 KB

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