cx88-video.c 52 KB

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