cx88-video.c 52 KB

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