cx88-video.c 52 KB

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