cx88-video.c 55 KB

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