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