cx88-video.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100
  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 = 0x7f,
  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 = 0x7f,
  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 = 0x3f,
  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. mutex_lock(&core->lock);
  315. if (dev->resources & bit) {
  316. /* no, someone else uses it */
  317. mutex_unlock(&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. mutex_unlock(&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. BUG_ON((fh->resources & bits) != bits);
  342. mutex_lock(&core->lock);
  343. fh->resources &= ~bits;
  344. dev->resources &= ~bits;
  345. dprintk(1,"res: put %d\n",bits);
  346. mutex_unlock(&core->lock);
  347. }
  348. /* ------------------------------------------------------------------ */
  349. /* static int video_mux(struct cx8800_dev *dev, unsigned int input) */
  350. static int video_mux(struct cx88_core *core, unsigned int input)
  351. {
  352. /* struct cx88_core *core = dev->core; */
  353. dprintk(1,"video_mux: %d [vmux=%d,gpio=0x%x,0x%x,0x%x,0x%x]\n",
  354. input, INPUT(input)->vmux,
  355. INPUT(input)->gpio0,INPUT(input)->gpio1,
  356. INPUT(input)->gpio2,INPUT(input)->gpio3);
  357. core->input = input;
  358. cx_andor(MO_INPUT_FORMAT, 0x03 << 14, INPUT(input)->vmux << 14);
  359. cx_write(MO_GP3_IO, INPUT(input)->gpio3);
  360. cx_write(MO_GP0_IO, INPUT(input)->gpio0);
  361. cx_write(MO_GP1_IO, INPUT(input)->gpio1);
  362. cx_write(MO_GP2_IO, INPUT(input)->gpio2);
  363. switch (INPUT(input)->type) {
  364. case CX88_VMUX_SVIDEO:
  365. cx_set(MO_AFECFG_IO, 0x00000001);
  366. cx_set(MO_INPUT_FORMAT, 0x00010010);
  367. cx_set(MO_FILTER_EVEN, 0x00002020);
  368. cx_set(MO_FILTER_ODD, 0x00002020);
  369. break;
  370. default:
  371. cx_clear(MO_AFECFG_IO, 0x00000001);
  372. cx_clear(MO_INPUT_FORMAT, 0x00010010);
  373. cx_clear(MO_FILTER_EVEN, 0x00002020);
  374. cx_clear(MO_FILTER_ODD, 0x00002020);
  375. break;
  376. }
  377. return 0;
  378. }
  379. /* ------------------------------------------------------------------ */
  380. static int start_video_dma(struct cx8800_dev *dev,
  381. struct cx88_dmaqueue *q,
  382. struct cx88_buffer *buf)
  383. {
  384. struct cx88_core *core = dev->core;
  385. /* setup fifo + format */
  386. cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH21],
  387. buf->bpl, buf->risc.dma);
  388. cx88_set_scale(core, buf->vb.width, buf->vb.height, buf->vb.field);
  389. cx_write(MO_COLOR_CTRL, buf->fmt->cxformat | ColorFormatGamma);
  390. /* reset counter */
  391. cx_write(MO_VIDY_GPCNTRL,GP_COUNT_CONTROL_RESET);
  392. q->count = 1;
  393. /* enable irqs */
  394. cx_set(MO_PCI_INTMSK, core->pci_irqmask | 0x01);
  395. /* Enables corresponding bits at PCI_INT_STAT:
  396. bits 0 to 4: video, audio, transport stream, VIP, Host
  397. bit 7: timer
  398. bits 8 and 9: DMA complete for: SRC, DST
  399. bits 10 and 11: BERR signal asserted for RISC: RD, WR
  400. bits 12 to 15: BERR signal asserted for: BRDG, SRC, DST, IPB
  401. */
  402. cx_set(MO_VID_INTMSK, 0x0f0011);
  403. /* enable capture */
  404. cx_set(VID_CAPTURE_CONTROL,0x06);
  405. /* start dma */
  406. cx_set(MO_DEV_CNTRL2, (1<<5));
  407. cx_set(MO_VID_DMACNTRL, 0x11); /* Planar Y and packed FIFO and RISC enable */
  408. return 0;
  409. }
  410. static int stop_video_dma(struct cx8800_dev *dev)
  411. {
  412. struct cx88_core *core = dev->core;
  413. /* stop dma */
  414. cx_clear(MO_VID_DMACNTRL, 0x11);
  415. /* disable capture */
  416. cx_clear(VID_CAPTURE_CONTROL,0x06);
  417. /* disable irqs */
  418. cx_clear(MO_PCI_INTMSK, 0x000001);
  419. cx_clear(MO_VID_INTMSK, 0x0f0011);
  420. return 0;
  421. }
  422. static int restart_video_queue(struct cx8800_dev *dev,
  423. struct cx88_dmaqueue *q)
  424. {
  425. struct cx88_core *core = dev->core;
  426. struct cx88_buffer *buf, *prev;
  427. struct list_head *item;
  428. if (!list_empty(&q->active)) {
  429. buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
  430. dprintk(2,"restart_queue [%p/%d]: restart dma\n",
  431. buf, buf->vb.i);
  432. start_video_dma(dev, q, buf);
  433. list_for_each(item,&q->active) {
  434. buf = list_entry(item, struct cx88_buffer, vb.queue);
  435. buf->count = q->count++;
  436. }
  437. mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
  438. return 0;
  439. }
  440. prev = NULL;
  441. for (;;) {
  442. if (list_empty(&q->queued))
  443. return 0;
  444. buf = list_entry(q->queued.next, struct cx88_buffer, vb.queue);
  445. if (NULL == prev) {
  446. list_del(&buf->vb.queue);
  447. list_add_tail(&buf->vb.queue,&q->active);
  448. start_video_dma(dev, q, buf);
  449. buf->vb.state = STATE_ACTIVE;
  450. buf->count = q->count++;
  451. mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
  452. dprintk(2,"[%p/%d] restart_queue - first active\n",
  453. buf,buf->vb.i);
  454. } else if (prev->vb.width == buf->vb.width &&
  455. prev->vb.height == buf->vb.height &&
  456. prev->fmt == buf->fmt) {
  457. list_del(&buf->vb.queue);
  458. list_add_tail(&buf->vb.queue,&q->active);
  459. buf->vb.state = STATE_ACTIVE;
  460. buf->count = q->count++;
  461. prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
  462. dprintk(2,"[%p/%d] restart_queue - move to active\n",
  463. buf,buf->vb.i);
  464. } else {
  465. return 0;
  466. }
  467. prev = buf;
  468. }
  469. }
  470. /* ------------------------------------------------------------------ */
  471. static int
  472. buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
  473. {
  474. struct cx8800_fh *fh = q->priv_data;
  475. *size = fh->fmt->depth*fh->width*fh->height >> 3;
  476. if (0 == *count)
  477. *count = 32;
  478. while (*size * *count > vid_limit * 1024 * 1024)
  479. (*count)--;
  480. return 0;
  481. }
  482. static int
  483. buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
  484. enum v4l2_field field)
  485. {
  486. struct cx8800_fh *fh = q->priv_data;
  487. struct cx8800_dev *dev = fh->dev;
  488. struct cx88_core *core = dev->core;
  489. struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
  490. int rc, init_buffer = 0;
  491. BUG_ON(NULL == fh->fmt);
  492. if (fh->width < 48 || fh->width > norm_maxw(core->tvnorm) ||
  493. fh->height < 32 || fh->height > norm_maxh(core->tvnorm))
  494. return -EINVAL;
  495. buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
  496. if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
  497. return -EINVAL;
  498. if (buf->fmt != fh->fmt ||
  499. buf->vb.width != fh->width ||
  500. buf->vb.height != fh->height ||
  501. buf->vb.field != field) {
  502. buf->fmt = fh->fmt;
  503. buf->vb.width = fh->width;
  504. buf->vb.height = fh->height;
  505. buf->vb.field = field;
  506. init_buffer = 1;
  507. }
  508. if (STATE_NEEDS_INIT == buf->vb.state) {
  509. init_buffer = 1;
  510. if (0 != (rc = videobuf_iolock(dev->pci,&buf->vb,NULL)))
  511. goto fail;
  512. }
  513. if (init_buffer) {
  514. buf->bpl = buf->vb.width * buf->fmt->depth >> 3;
  515. switch (buf->vb.field) {
  516. case V4L2_FIELD_TOP:
  517. cx88_risc_buffer(dev->pci, &buf->risc,
  518. buf->vb.dma.sglist, 0, UNSET,
  519. buf->bpl, 0, buf->vb.height);
  520. break;
  521. case V4L2_FIELD_BOTTOM:
  522. cx88_risc_buffer(dev->pci, &buf->risc,
  523. buf->vb.dma.sglist, UNSET, 0,
  524. buf->bpl, 0, buf->vb.height);
  525. break;
  526. case V4L2_FIELD_INTERLACED:
  527. cx88_risc_buffer(dev->pci, &buf->risc,
  528. buf->vb.dma.sglist, 0, buf->bpl,
  529. buf->bpl, buf->bpl,
  530. buf->vb.height >> 1);
  531. break;
  532. case V4L2_FIELD_SEQ_TB:
  533. cx88_risc_buffer(dev->pci, &buf->risc,
  534. buf->vb.dma.sglist,
  535. 0, buf->bpl * (buf->vb.height >> 1),
  536. buf->bpl, 0,
  537. buf->vb.height >> 1);
  538. break;
  539. case V4L2_FIELD_SEQ_BT:
  540. cx88_risc_buffer(dev->pci, &buf->risc,
  541. buf->vb.dma.sglist,
  542. buf->bpl * (buf->vb.height >> 1), 0,
  543. buf->bpl, 0,
  544. buf->vb.height >> 1);
  545. break;
  546. default:
  547. BUG();
  548. }
  549. }
  550. dprintk(2,"[%p/%d] buffer_prepare - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
  551. buf, buf->vb.i,
  552. fh->width, fh->height, fh->fmt->depth, fh->fmt->name,
  553. (unsigned long)buf->risc.dma);
  554. buf->vb.state = STATE_PREPARED;
  555. return 0;
  556. fail:
  557. cx88_free_buffer(dev->pci,buf);
  558. return rc;
  559. }
  560. static void
  561. buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  562. {
  563. struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
  564. struct cx88_buffer *prev;
  565. struct cx8800_fh *fh = vq->priv_data;
  566. struct cx8800_dev *dev = fh->dev;
  567. struct cx88_core *core = dev->core;
  568. struct cx88_dmaqueue *q = &dev->vidq;
  569. /* add jump to stopper */
  570. buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
  571. buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
  572. if (!list_empty(&q->queued)) {
  573. list_add_tail(&buf->vb.queue,&q->queued);
  574. buf->vb.state = STATE_QUEUED;
  575. dprintk(2,"[%p/%d] buffer_queue - append to queued\n",
  576. buf, buf->vb.i);
  577. } else if (list_empty(&q->active)) {
  578. list_add_tail(&buf->vb.queue,&q->active);
  579. start_video_dma(dev, q, buf);
  580. buf->vb.state = STATE_ACTIVE;
  581. buf->count = q->count++;
  582. mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
  583. dprintk(2,"[%p/%d] buffer_queue - first active\n",
  584. buf, buf->vb.i);
  585. } else {
  586. prev = list_entry(q->active.prev, struct cx88_buffer, vb.queue);
  587. if (prev->vb.width == buf->vb.width &&
  588. prev->vb.height == buf->vb.height &&
  589. prev->fmt == buf->fmt) {
  590. list_add_tail(&buf->vb.queue,&q->active);
  591. buf->vb.state = STATE_ACTIVE;
  592. buf->count = q->count++;
  593. prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
  594. dprintk(2,"[%p/%d] buffer_queue - append to active\n",
  595. buf, buf->vb.i);
  596. } else {
  597. list_add_tail(&buf->vb.queue,&q->queued);
  598. buf->vb.state = STATE_QUEUED;
  599. dprintk(2,"[%p/%d] buffer_queue - first queued\n",
  600. buf, buf->vb.i);
  601. }
  602. }
  603. }
  604. static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
  605. {
  606. struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
  607. struct cx8800_fh *fh = q->priv_data;
  608. cx88_free_buffer(fh->dev->pci,buf);
  609. }
  610. static struct videobuf_queue_ops cx8800_video_qops = {
  611. .buf_setup = buffer_setup,
  612. .buf_prepare = buffer_prepare,
  613. .buf_queue = buffer_queue,
  614. .buf_release = buffer_release,
  615. };
  616. /* ------------------------------------------------------------------ */
  617. /* ------------------------------------------------------------------ */
  618. static struct videobuf_queue* get_queue(struct cx8800_fh *fh)
  619. {
  620. switch (fh->type) {
  621. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  622. return &fh->vidq;
  623. case V4L2_BUF_TYPE_VBI_CAPTURE:
  624. return &fh->vbiq;
  625. default:
  626. BUG();
  627. return NULL;
  628. }
  629. }
  630. static int get_ressource(struct cx8800_fh *fh)
  631. {
  632. switch (fh->type) {
  633. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  634. return RESOURCE_VIDEO;
  635. case V4L2_BUF_TYPE_VBI_CAPTURE:
  636. return RESOURCE_VBI;
  637. default:
  638. BUG();
  639. return 0;
  640. }
  641. }
  642. static int video_open(struct inode *inode, struct file *file)
  643. {
  644. int minor = iminor(inode);
  645. struct cx8800_dev *h,*dev = NULL;
  646. struct cx88_core *core;
  647. struct cx8800_fh *fh;
  648. struct list_head *list;
  649. enum v4l2_buf_type type = 0;
  650. int radio = 0;
  651. list_for_each(list,&cx8800_devlist) {
  652. h = list_entry(list, struct cx8800_dev, devlist);
  653. if (h->video_dev->minor == minor) {
  654. dev = h;
  655. type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  656. }
  657. if (h->vbi_dev->minor == minor) {
  658. dev = h;
  659. type = V4L2_BUF_TYPE_VBI_CAPTURE;
  660. }
  661. if (h->radio_dev &&
  662. h->radio_dev->minor == minor) {
  663. radio = 1;
  664. dev = h;
  665. }
  666. }
  667. if (NULL == dev)
  668. return -ENODEV;
  669. core = dev->core;
  670. dprintk(1,"open minor=%d radio=%d type=%s\n",
  671. minor,radio,v4l2_type_names[type]);
  672. /* allocate + initialize per filehandle data */
  673. fh = kzalloc(sizeof(*fh),GFP_KERNEL);
  674. if (NULL == fh)
  675. return -ENOMEM;
  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 & 0x7f) < 0x40) ? ((value & 0x7f) + 0x40)
  812. : (0x7f - (value & 0x7f));
  813. break;
  814. case V4L2_CID_AUDIO_VOLUME:
  815. ctl->value = 0x3f - (value & 0x3f);
  816. break;
  817. default:
  818. ctl->value = ((value + (c->off << c->shift)) & c->mask) >> c->shift;
  819. break;
  820. }
  821. dprintk(1,"get_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
  822. ctl->id, c->v.name, ctl->value, c->reg,
  823. value,c->mask, c->sreg ? " [shadowed]" : "");
  824. return 0;
  825. }
  826. /* static int set_control(struct cx8800_dev *dev, struct v4l2_control *ctl) */
  827. static int set_control(struct cx88_core *core, struct v4l2_control *ctl)
  828. {
  829. /* struct cx88_core *core = dev->core; */
  830. struct cx88_ctrl *c = NULL;
  831. u32 value,mask;
  832. int i;
  833. for (i = 0; i < CX8800_CTLS; i++) {
  834. if (cx8800_ctls[i].v.id == ctl->id) {
  835. c = &cx8800_ctls[i];
  836. }
  837. }
  838. if (NULL == c)
  839. return -EINVAL;
  840. if (ctl->value < c->v.minimum)
  841. ctl->value = c->v.minimum;
  842. if (ctl->value > c->v.maximum)
  843. ctl->value = c->v.maximum;
  844. mask=c->mask;
  845. switch (ctl->id) {
  846. case V4L2_CID_AUDIO_BALANCE:
  847. value = (ctl->value < 0x40) ? (0x7f - ctl->value) : (ctl->value - 0x40);
  848. break;
  849. case V4L2_CID_AUDIO_VOLUME:
  850. value = 0x3f - (ctl->value & 0x3f);
  851. break;
  852. case V4L2_CID_SATURATION:
  853. /* special v_sat handling */
  854. value = ((ctl->value - c->off) << c->shift) & c->mask;
  855. if (core->tvnorm->id & V4L2_STD_SECAM) {
  856. /* For SECAM, both U and V sat should be equal */
  857. value=value<<8|value;
  858. } else {
  859. /* Keeps U Saturation proportional to V Sat */
  860. value=(value*0x5a)/0x7f<<8|value;
  861. }
  862. mask=0xffff;
  863. break;
  864. default:
  865. value = ((ctl->value - c->off) << c->shift) & c->mask;
  866. break;
  867. }
  868. dprintk(1,"set_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
  869. ctl->id, c->v.name, ctl->value, c->reg, value,
  870. mask, c->sreg ? " [shadowed]" : "");
  871. if (c->sreg) {
  872. cx_sandor(c->sreg, c->reg, mask, value);
  873. } else {
  874. cx_andor(c->reg, mask, value);
  875. }
  876. return 0;
  877. }
  878. static void init_controls(struct cx88_core *core)
  879. {
  880. struct v4l2_control ctrl;
  881. int i;
  882. for (i = 0; i < CX8800_CTLS; i++) {
  883. ctrl.id=cx8800_ctls[i].v.id;
  884. ctrl.value=cx8800_ctls[i].v.default_value;
  885. set_control(core, &ctrl);
  886. }
  887. }
  888. /* ------------------------------------------------------------------ */
  889. static int cx8800_g_fmt(struct cx8800_dev *dev, struct cx8800_fh *fh,
  890. struct v4l2_format *f)
  891. {
  892. switch (f->type) {
  893. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  894. memset(&f->fmt.pix,0,sizeof(f->fmt.pix));
  895. f->fmt.pix.width = fh->width;
  896. f->fmt.pix.height = fh->height;
  897. f->fmt.pix.field = fh->vidq.field;
  898. f->fmt.pix.pixelformat = fh->fmt->fourcc;
  899. f->fmt.pix.bytesperline =
  900. (f->fmt.pix.width * fh->fmt->depth) >> 3;
  901. f->fmt.pix.sizeimage =
  902. f->fmt.pix.height * f->fmt.pix.bytesperline;
  903. return 0;
  904. case V4L2_BUF_TYPE_VBI_CAPTURE:
  905. cx8800_vbi_fmt(dev, f);
  906. return 0;
  907. default:
  908. return -EINVAL;
  909. }
  910. }
  911. static int cx8800_try_fmt(struct cx8800_dev *dev, struct cx8800_fh *fh,
  912. struct v4l2_format *f)
  913. {
  914. struct cx88_core *core = dev->core;
  915. switch (f->type) {
  916. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  917. {
  918. struct cx8800_fmt *fmt;
  919. enum v4l2_field field;
  920. unsigned int maxw, maxh;
  921. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  922. if (NULL == fmt)
  923. return -EINVAL;
  924. field = f->fmt.pix.field;
  925. maxw = norm_maxw(core->tvnorm);
  926. maxh = norm_maxh(core->tvnorm);
  927. if (V4L2_FIELD_ANY == field) {
  928. field = (f->fmt.pix.height > maxh/2)
  929. ? V4L2_FIELD_INTERLACED
  930. : V4L2_FIELD_BOTTOM;
  931. }
  932. switch (field) {
  933. case V4L2_FIELD_TOP:
  934. case V4L2_FIELD_BOTTOM:
  935. maxh = maxh / 2;
  936. break;
  937. case V4L2_FIELD_INTERLACED:
  938. break;
  939. default:
  940. return -EINVAL;
  941. }
  942. f->fmt.pix.field = field;
  943. if (f->fmt.pix.height < 32)
  944. f->fmt.pix.height = 32;
  945. if (f->fmt.pix.height > maxh)
  946. f->fmt.pix.height = maxh;
  947. if (f->fmt.pix.width < 48)
  948. f->fmt.pix.width = 48;
  949. if (f->fmt.pix.width > maxw)
  950. f->fmt.pix.width = maxw;
  951. f->fmt.pix.width &= ~0x03;
  952. f->fmt.pix.bytesperline =
  953. (f->fmt.pix.width * fmt->depth) >> 3;
  954. f->fmt.pix.sizeimage =
  955. f->fmt.pix.height * f->fmt.pix.bytesperline;
  956. return 0;
  957. }
  958. case V4L2_BUF_TYPE_VBI_CAPTURE:
  959. cx8800_vbi_fmt(dev, f);
  960. return 0;
  961. default:
  962. return -EINVAL;
  963. }
  964. }
  965. static int cx8800_s_fmt(struct cx8800_dev *dev, struct cx8800_fh *fh,
  966. struct v4l2_format *f)
  967. {
  968. int err;
  969. switch (f->type) {
  970. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  971. err = cx8800_try_fmt(dev,fh,f);
  972. if (0 != err)
  973. return err;
  974. fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  975. fh->width = f->fmt.pix.width;
  976. fh->height = f->fmt.pix.height;
  977. fh->vidq.field = f->fmt.pix.field;
  978. return 0;
  979. case V4L2_BUF_TYPE_VBI_CAPTURE:
  980. cx8800_vbi_fmt(dev, f);
  981. return 0;
  982. default:
  983. return -EINVAL;
  984. }
  985. }
  986. /*
  987. * This function is _not_ called directly, but from
  988. * video_generic_ioctl (and maybe others). userspace
  989. * copying is done already, arg is a kernel pointer.
  990. */
  991. static int video_do_ioctl(struct inode *inode, struct file *file,
  992. unsigned int cmd, void *arg)
  993. {
  994. struct cx8800_fh *fh = file->private_data;
  995. struct cx8800_dev *dev = fh->dev;
  996. struct cx88_core *core = dev->core;
  997. int err;
  998. if (video_debug > 1)
  999. v4l_print_ioctl(core->name,cmd);
  1000. switch (cmd) {
  1001. /* --- capabilities ------------------------------------------ */
  1002. case VIDIOC_QUERYCAP:
  1003. {
  1004. struct v4l2_capability *cap = arg;
  1005. memset(cap,0,sizeof(*cap));
  1006. strcpy(cap->driver, "cx8800");
  1007. strlcpy(cap->card, cx88_boards[core->board].name,
  1008. sizeof(cap->card));
  1009. sprintf(cap->bus_info,"PCI:%s",pci_name(dev->pci));
  1010. cap->version = CX88_VERSION_CODE;
  1011. cap->capabilities =
  1012. V4L2_CAP_VIDEO_CAPTURE |
  1013. V4L2_CAP_READWRITE |
  1014. V4L2_CAP_STREAMING |
  1015. V4L2_CAP_VBI_CAPTURE |
  1016. V4L2_CAP_VIDEO_OVERLAY |
  1017. 0;
  1018. if (UNSET != core->tuner_type)
  1019. cap->capabilities |= V4L2_CAP_TUNER;
  1020. return 0;
  1021. }
  1022. /* --- capture ioctls ---------------------------------------- */
  1023. case VIDIOC_ENUM_FMT:
  1024. {
  1025. struct v4l2_fmtdesc *f = arg;
  1026. enum v4l2_buf_type type;
  1027. unsigned int index;
  1028. index = f->index;
  1029. type = f->type;
  1030. switch (type) {
  1031. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  1032. if (index >= ARRAY_SIZE(formats))
  1033. return -EINVAL;
  1034. memset(f,0,sizeof(*f));
  1035. f->index = index;
  1036. f->type = type;
  1037. strlcpy(f->description,formats[index].name,sizeof(f->description));
  1038. f->pixelformat = formats[index].fourcc;
  1039. break;
  1040. default:
  1041. return -EINVAL;
  1042. }
  1043. return 0;
  1044. }
  1045. case VIDIOC_G_FMT:
  1046. {
  1047. struct v4l2_format *f = arg;
  1048. return cx8800_g_fmt(dev,fh,f);
  1049. }
  1050. case VIDIOC_S_FMT:
  1051. {
  1052. struct v4l2_format *f = arg;
  1053. return cx8800_s_fmt(dev,fh,f);
  1054. }
  1055. case VIDIOC_TRY_FMT:
  1056. {
  1057. struct v4l2_format *f = arg;
  1058. return cx8800_try_fmt(dev,fh,f);
  1059. }
  1060. #ifdef HAVE_V4L1
  1061. /* --- streaming capture ------------------------------------- */
  1062. case VIDIOCGMBUF:
  1063. {
  1064. struct video_mbuf *mbuf = arg;
  1065. struct videobuf_queue *q;
  1066. struct v4l2_requestbuffers req;
  1067. unsigned int i;
  1068. q = get_queue(fh);
  1069. memset(&req,0,sizeof(req));
  1070. req.type = q->type;
  1071. req.count = 8;
  1072. req.memory = V4L2_MEMORY_MMAP;
  1073. err = videobuf_reqbufs(q,&req);
  1074. if (err < 0)
  1075. return err;
  1076. memset(mbuf,0,sizeof(*mbuf));
  1077. mbuf->frames = req.count;
  1078. mbuf->size = 0;
  1079. for (i = 0; i < mbuf->frames; i++) {
  1080. mbuf->offsets[i] = q->bufs[i]->boff;
  1081. mbuf->size += q->bufs[i]->bsize;
  1082. }
  1083. return 0;
  1084. }
  1085. #endif
  1086. case VIDIOC_REQBUFS:
  1087. return videobuf_reqbufs(get_queue(fh), arg);
  1088. case VIDIOC_QUERYBUF:
  1089. return videobuf_querybuf(get_queue(fh), arg);
  1090. case VIDIOC_QBUF:
  1091. return videobuf_qbuf(get_queue(fh), arg);
  1092. case VIDIOC_DQBUF:
  1093. return videobuf_dqbuf(get_queue(fh), arg,
  1094. file->f_flags & O_NONBLOCK);
  1095. case VIDIOC_STREAMON:
  1096. {
  1097. int res = get_ressource(fh);
  1098. if (!res_get(dev,fh,res))
  1099. return -EBUSY;
  1100. return videobuf_streamon(get_queue(fh));
  1101. }
  1102. case VIDIOC_STREAMOFF:
  1103. {
  1104. int res = get_ressource(fh);
  1105. err = videobuf_streamoff(get_queue(fh));
  1106. if (err < 0)
  1107. return err;
  1108. res_free(dev,fh,res);
  1109. return 0;
  1110. }
  1111. default:
  1112. return cx88_do_ioctl( inode, file, fh->radio, core, cmd, arg, video_do_ioctl );
  1113. }
  1114. return 0;
  1115. }
  1116. int cx88_do_ioctl(struct inode *inode, struct file *file, int radio,
  1117. struct cx88_core *core, unsigned int cmd, void *arg, v4l2_kioctl driver_ioctl)
  1118. {
  1119. int err;
  1120. dprintk(2, "CORE IOCTL: 0x%x\n", cmd );
  1121. if (video_debug > 1)
  1122. v4l_print_ioctl(core->name,cmd);
  1123. switch (cmd) {
  1124. /* ---------- tv norms ---------- */
  1125. case VIDIOC_ENUMSTD:
  1126. {
  1127. struct v4l2_standard *e = arg;
  1128. unsigned int i;
  1129. i = e->index;
  1130. if (i >= ARRAY_SIZE(tvnorms))
  1131. return -EINVAL;
  1132. err = v4l2_video_std_construct(e, tvnorms[e->index].id,
  1133. tvnorms[e->index].name);
  1134. e->index = i;
  1135. if (err < 0)
  1136. return err;
  1137. return 0;
  1138. }
  1139. case VIDIOC_G_STD:
  1140. {
  1141. v4l2_std_id *id = arg;
  1142. *id = core->tvnorm->id;
  1143. return 0;
  1144. }
  1145. case VIDIOC_S_STD:
  1146. {
  1147. v4l2_std_id *id = arg;
  1148. unsigned int i;
  1149. for(i = 0; i < ARRAY_SIZE(tvnorms); i++)
  1150. if (*id & tvnorms[i].id)
  1151. break;
  1152. if (i == ARRAY_SIZE(tvnorms))
  1153. return -EINVAL;
  1154. mutex_lock(&core->lock);
  1155. cx88_set_tvnorm(core,&tvnorms[i]);
  1156. mutex_unlock(&core->lock);
  1157. return 0;
  1158. }
  1159. /* ------ input switching ---------- */
  1160. case VIDIOC_ENUMINPUT:
  1161. {
  1162. static const char *iname[] = {
  1163. [ CX88_VMUX_COMPOSITE1 ] = "Composite1",
  1164. [ CX88_VMUX_COMPOSITE2 ] = "Composite2",
  1165. [ CX88_VMUX_COMPOSITE3 ] = "Composite3",
  1166. [ CX88_VMUX_COMPOSITE4 ] = "Composite4",
  1167. [ CX88_VMUX_SVIDEO ] = "S-Video",
  1168. [ CX88_VMUX_TELEVISION ] = "Television",
  1169. [ CX88_VMUX_CABLE ] = "Cable TV",
  1170. [ CX88_VMUX_DVB ] = "DVB",
  1171. [ CX88_VMUX_DEBUG ] = "for debug only",
  1172. };
  1173. struct v4l2_input *i = arg;
  1174. unsigned int n;
  1175. n = i->index;
  1176. if (n >= 4)
  1177. return -EINVAL;
  1178. if (0 == INPUT(n)->type)
  1179. return -EINVAL;
  1180. memset(i,0,sizeof(*i));
  1181. i->index = n;
  1182. i->type = V4L2_INPUT_TYPE_CAMERA;
  1183. strcpy(i->name,iname[INPUT(n)->type]);
  1184. if ((CX88_VMUX_TELEVISION == INPUT(n)->type) ||
  1185. (CX88_VMUX_CABLE == INPUT(n)->type))
  1186. i->type = V4L2_INPUT_TYPE_TUNER;
  1187. for (n = 0; n < ARRAY_SIZE(tvnorms); n++)
  1188. i->std |= tvnorms[n].id;
  1189. return 0;
  1190. }
  1191. case VIDIOC_G_INPUT:
  1192. {
  1193. unsigned int *i = arg;
  1194. *i = core->input;
  1195. return 0;
  1196. }
  1197. case VIDIOC_S_INPUT:
  1198. {
  1199. unsigned int *i = arg;
  1200. if (*i >= 4)
  1201. return -EINVAL;
  1202. mutex_lock(&core->lock);
  1203. cx88_newstation(core);
  1204. video_mux(core,*i);
  1205. mutex_unlock(&core->lock);
  1206. return 0;
  1207. }
  1208. /* --- controls ---------------------------------------------- */
  1209. case VIDIOC_QUERYCTRL:
  1210. {
  1211. struct v4l2_queryctrl *c = arg;
  1212. int i;
  1213. if (c->id < V4L2_CID_BASE ||
  1214. c->id >= V4L2_CID_LASTP1)
  1215. return -EINVAL;
  1216. for (i = 0; i < CX8800_CTLS; i++)
  1217. if (cx8800_ctls[i].v.id == c->id)
  1218. break;
  1219. if (i == CX8800_CTLS) {
  1220. *c = no_ctl;
  1221. return 0;
  1222. }
  1223. *c = cx8800_ctls[i].v;
  1224. return 0;
  1225. }
  1226. case VIDIOC_G_CTRL:
  1227. return get_control(core,arg);
  1228. case VIDIOC_S_CTRL:
  1229. return set_control(core,arg);
  1230. /* --- tuner ioctls ------------------------------------------ */
  1231. case VIDIOC_G_TUNER:
  1232. {
  1233. struct v4l2_tuner *t = arg;
  1234. u32 reg;
  1235. if (UNSET == core->tuner_type)
  1236. return -EINVAL;
  1237. if (0 != t->index)
  1238. return -EINVAL;
  1239. memset(t,0,sizeof(*t));
  1240. strcpy(t->name, "Television");
  1241. t->type = V4L2_TUNER_ANALOG_TV;
  1242. t->capability = V4L2_TUNER_CAP_NORM;
  1243. t->rangehigh = 0xffffffffUL;
  1244. cx88_get_stereo(core ,t);
  1245. reg = cx_read(MO_DEVICE_STATUS);
  1246. t->signal = (reg & (1<<5)) ? 0xffff : 0x0000;
  1247. return 0;
  1248. }
  1249. case VIDIOC_S_TUNER:
  1250. {
  1251. struct v4l2_tuner *t = arg;
  1252. if (UNSET == core->tuner_type)
  1253. return -EINVAL;
  1254. if (0 != t->index)
  1255. return -EINVAL;
  1256. cx88_set_stereo(core, t->audmode, 1);
  1257. return 0;
  1258. }
  1259. case VIDIOC_G_FREQUENCY:
  1260. {
  1261. struct v4l2_frequency *f = arg;
  1262. memset(f,0,sizeof(*f));
  1263. if (UNSET == core->tuner_type)
  1264. return -EINVAL;
  1265. /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
  1266. f->type = radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  1267. f->frequency = core->freq;
  1268. cx88_call_i2c_clients(core,VIDIOC_G_FREQUENCY,f);
  1269. return 0;
  1270. }
  1271. case VIDIOC_S_FREQUENCY:
  1272. {
  1273. struct v4l2_frequency *f = arg;
  1274. if (UNSET == core->tuner_type)
  1275. return -EINVAL;
  1276. if (f->tuner != 0)
  1277. return -EINVAL;
  1278. if (0 == radio && f->type != V4L2_TUNER_ANALOG_TV)
  1279. return -EINVAL;
  1280. if (1 == radio && f->type != V4L2_TUNER_RADIO)
  1281. return -EINVAL;
  1282. mutex_lock(&core->lock);
  1283. core->freq = f->frequency;
  1284. cx88_newstation(core);
  1285. cx88_call_i2c_clients(core,VIDIOC_S_FREQUENCY,f);
  1286. /* When changing channels it is required to reset TVAUDIO */
  1287. msleep (10);
  1288. cx88_set_tvaudio(core);
  1289. mutex_unlock(&core->lock);
  1290. return 0;
  1291. }
  1292. default:
  1293. return v4l_compat_translate_ioctl(inode,file,cmd,arg,
  1294. driver_ioctl);
  1295. }
  1296. return 0;
  1297. }
  1298. static int video_ioctl(struct inode *inode, struct file *file,
  1299. unsigned int cmd, unsigned long arg)
  1300. {
  1301. return video_usercopy(inode, file, cmd, arg, video_do_ioctl);
  1302. }
  1303. /* ----------------------------------------------------------- */
  1304. static int radio_do_ioctl(struct inode *inode, struct file *file,
  1305. unsigned int cmd, void *arg)
  1306. {
  1307. struct cx8800_fh *fh = file->private_data;
  1308. struct cx8800_dev *dev = fh->dev;
  1309. struct cx88_core *core = dev->core;
  1310. if (video_debug > 1)
  1311. v4l_print_ioctl(core->name,cmd);
  1312. switch (cmd) {
  1313. case VIDIOC_QUERYCAP:
  1314. {
  1315. struct v4l2_capability *cap = arg;
  1316. memset(cap,0,sizeof(*cap));
  1317. strcpy(cap->driver, "cx8800");
  1318. strlcpy(cap->card, cx88_boards[core->board].name,
  1319. sizeof(cap->card));
  1320. sprintf(cap->bus_info,"PCI:%s", pci_name(dev->pci));
  1321. cap->version = CX88_VERSION_CODE;
  1322. cap->capabilities = V4L2_CAP_TUNER;
  1323. return 0;
  1324. }
  1325. case VIDIOC_G_TUNER:
  1326. {
  1327. struct v4l2_tuner *t = arg;
  1328. if (t->index > 0)
  1329. return -EINVAL;
  1330. memset(t,0,sizeof(*t));
  1331. strcpy(t->name, "Radio");
  1332. t->type = V4L2_TUNER_RADIO;
  1333. cx88_call_i2c_clients(core,VIDIOC_G_TUNER,t);
  1334. return 0;
  1335. }
  1336. case VIDIOC_ENUMINPUT:
  1337. {
  1338. struct v4l2_input *i = arg;
  1339. if (i->index != 0)
  1340. return -EINVAL;
  1341. strcpy(i->name,"Radio");
  1342. i->type = V4L2_INPUT_TYPE_TUNER;
  1343. return 0;
  1344. }
  1345. case VIDIOC_G_INPUT:
  1346. {
  1347. int *i = arg;
  1348. *i = 0;
  1349. return 0;
  1350. }
  1351. case VIDIOC_G_AUDIO:
  1352. {
  1353. struct v4l2_audio *a = arg;
  1354. memset(a,0,sizeof(*a));
  1355. strcpy(a->name,"Radio");
  1356. return 0;
  1357. }
  1358. case VIDIOC_G_STD:
  1359. {
  1360. v4l2_std_id *id = arg;
  1361. *id = 0;
  1362. return 0;
  1363. }
  1364. #ifdef HAVE_V4L1
  1365. case VIDIOCSTUNER:
  1366. {
  1367. struct video_tuner *v = arg;
  1368. if (v->tuner) /* Only tuner 0 */
  1369. return -EINVAL;
  1370. cx88_call_i2c_clients(core,VIDIOCSTUNER,v);
  1371. return 0;
  1372. }
  1373. #endif
  1374. case VIDIOC_S_TUNER:
  1375. {
  1376. struct v4l2_tuner *t = arg;
  1377. if (0 != t->index)
  1378. return -EINVAL;
  1379. cx88_call_i2c_clients(core,VIDIOC_S_TUNER,t);
  1380. return 0;
  1381. }
  1382. case VIDIOC_S_AUDIO:
  1383. case VIDIOC_S_INPUT:
  1384. case VIDIOC_S_STD:
  1385. return 0;
  1386. case VIDIOC_QUERYCTRL:
  1387. {
  1388. struct v4l2_queryctrl *c = arg;
  1389. int i;
  1390. if (c->id < V4L2_CID_BASE ||
  1391. c->id >= V4L2_CID_LASTP1)
  1392. return -EINVAL;
  1393. if (c->id == V4L2_CID_AUDIO_MUTE) {
  1394. for (i = 0; i < CX8800_CTLS; i++)
  1395. if (cx8800_ctls[i].v.id == c->id)
  1396. break;
  1397. *c = cx8800_ctls[i].v;
  1398. } else
  1399. *c = no_ctl;
  1400. return 0;
  1401. }
  1402. case VIDIOC_G_CTRL:
  1403. case VIDIOC_S_CTRL:
  1404. case VIDIOC_G_FREQUENCY:
  1405. case VIDIOC_S_FREQUENCY:
  1406. return video_do_ioctl(inode,file,cmd,arg);
  1407. default:
  1408. return v4l_compat_translate_ioctl(inode,file,cmd,arg,
  1409. radio_do_ioctl);
  1410. }
  1411. return 0;
  1412. };
  1413. static int radio_ioctl(struct inode *inode, struct file *file,
  1414. unsigned int cmd, unsigned long arg)
  1415. {
  1416. return video_usercopy(inode, file, cmd, arg, radio_do_ioctl);
  1417. };
  1418. /* ----------------------------------------------------------- */
  1419. static void cx8800_vid_timeout(unsigned long data)
  1420. {
  1421. struct cx8800_dev *dev = (struct cx8800_dev*)data;
  1422. struct cx88_core *core = dev->core;
  1423. struct cx88_dmaqueue *q = &dev->vidq;
  1424. struct cx88_buffer *buf;
  1425. unsigned long flags;
  1426. cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
  1427. cx_clear(MO_VID_DMACNTRL, 0x11);
  1428. cx_clear(VID_CAPTURE_CONTROL, 0x06);
  1429. spin_lock_irqsave(&dev->slock,flags);
  1430. while (!list_empty(&q->active)) {
  1431. buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
  1432. list_del(&buf->vb.queue);
  1433. buf->vb.state = STATE_ERROR;
  1434. wake_up(&buf->vb.done);
  1435. printk("%s/0: [%p/%d] timeout - dma=0x%08lx\n", core->name,
  1436. buf, buf->vb.i, (unsigned long)buf->risc.dma);
  1437. }
  1438. restart_video_queue(dev,q);
  1439. spin_unlock_irqrestore(&dev->slock,flags);
  1440. }
  1441. static char *cx88_vid_irqs[32] = {
  1442. "y_risci1", "u_risci1", "v_risci1", "vbi_risc1",
  1443. "y_risci2", "u_risci2", "v_risci2", "vbi_risc2",
  1444. "y_oflow", "u_oflow", "v_oflow", "vbi_oflow",
  1445. "y_sync", "u_sync", "v_sync", "vbi_sync",
  1446. "opc_err", "par_err", "rip_err", "pci_abort",
  1447. };
  1448. static void cx8800_vid_irq(struct cx8800_dev *dev)
  1449. {
  1450. struct cx88_core *core = dev->core;
  1451. u32 status, mask, count;
  1452. status = cx_read(MO_VID_INTSTAT);
  1453. mask = cx_read(MO_VID_INTMSK);
  1454. if (0 == (status & mask))
  1455. return;
  1456. cx_write(MO_VID_INTSTAT, status);
  1457. if (irq_debug || (status & mask & ~0xff))
  1458. cx88_print_irqbits(core->name, "irq vid",
  1459. cx88_vid_irqs, status, mask);
  1460. /* risc op code error */
  1461. if (status & (1 << 16)) {
  1462. printk(KERN_WARNING "%s/0: video risc op code error\n",core->name);
  1463. cx_clear(MO_VID_DMACNTRL, 0x11);
  1464. cx_clear(VID_CAPTURE_CONTROL, 0x06);
  1465. cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
  1466. }
  1467. /* risc1 y */
  1468. if (status & 0x01) {
  1469. spin_lock(&dev->slock);
  1470. count = cx_read(MO_VIDY_GPCNT);
  1471. cx88_wakeup(core, &dev->vidq, count);
  1472. spin_unlock(&dev->slock);
  1473. }
  1474. /* risc1 vbi */
  1475. if (status & 0x08) {
  1476. spin_lock(&dev->slock);
  1477. count = cx_read(MO_VBI_GPCNT);
  1478. cx88_wakeup(core, &dev->vbiq, count);
  1479. spin_unlock(&dev->slock);
  1480. }
  1481. /* risc2 y */
  1482. if (status & 0x10) {
  1483. dprintk(2,"stopper video\n");
  1484. spin_lock(&dev->slock);
  1485. restart_video_queue(dev,&dev->vidq);
  1486. spin_unlock(&dev->slock);
  1487. }
  1488. /* risc2 vbi */
  1489. if (status & 0x80) {
  1490. dprintk(2,"stopper vbi\n");
  1491. spin_lock(&dev->slock);
  1492. cx8800_restart_vbi_queue(dev,&dev->vbiq);
  1493. spin_unlock(&dev->slock);
  1494. }
  1495. }
  1496. static irqreturn_t cx8800_irq(int irq, void *dev_id, struct pt_regs *regs)
  1497. {
  1498. struct cx8800_dev *dev = dev_id;
  1499. struct cx88_core *core = dev->core;
  1500. u32 status;
  1501. int loop, handled = 0;
  1502. for (loop = 0; loop < 10; loop++) {
  1503. status = cx_read(MO_PCI_INTSTAT) & (core->pci_irqmask | 0x01);
  1504. if (0 == status)
  1505. goto out;
  1506. cx_write(MO_PCI_INTSTAT, status);
  1507. handled = 1;
  1508. if (status & core->pci_irqmask)
  1509. cx88_core_irq(core,status);
  1510. if (status & 0x01)
  1511. cx8800_vid_irq(dev);
  1512. };
  1513. if (10 == loop) {
  1514. printk(KERN_WARNING "%s/0: irq loop -- clearing mask\n",
  1515. core->name);
  1516. cx_write(MO_PCI_INTMSK,0);
  1517. }
  1518. out:
  1519. return IRQ_RETVAL(handled);
  1520. }
  1521. /* ----------------------------------------------------------- */
  1522. /* exported stuff */
  1523. static struct file_operations video_fops =
  1524. {
  1525. .owner = THIS_MODULE,
  1526. .open = video_open,
  1527. .release = video_release,
  1528. .read = video_read,
  1529. .poll = video_poll,
  1530. .mmap = video_mmap,
  1531. .ioctl = video_ioctl,
  1532. .compat_ioctl = v4l_compat_ioctl32,
  1533. .llseek = no_llseek,
  1534. };
  1535. static struct video_device cx8800_video_template =
  1536. {
  1537. .name = "cx8800-video",
  1538. .type = VID_TYPE_CAPTURE|VID_TYPE_TUNER|VID_TYPE_SCALES,
  1539. .hardware = 0,
  1540. .fops = &video_fops,
  1541. .minor = -1,
  1542. };
  1543. static struct video_device cx8800_vbi_template =
  1544. {
  1545. .name = "cx8800-vbi",
  1546. .type = VID_TYPE_TELETEXT|VID_TYPE_TUNER,
  1547. .hardware = 0,
  1548. .fops = &video_fops,
  1549. .minor = -1,
  1550. };
  1551. static struct file_operations radio_fops =
  1552. {
  1553. .owner = THIS_MODULE,
  1554. .open = video_open,
  1555. .release = video_release,
  1556. .ioctl = radio_ioctl,
  1557. .compat_ioctl = v4l_compat_ioctl32,
  1558. .llseek = no_llseek,
  1559. };
  1560. static struct video_device cx8800_radio_template =
  1561. {
  1562. .name = "cx8800-radio",
  1563. .type = VID_TYPE_TUNER,
  1564. .hardware = 0,
  1565. .fops = &radio_fops,
  1566. .minor = -1,
  1567. };
  1568. /* ----------------------------------------------------------- */
  1569. static void cx8800_unregister_video(struct cx8800_dev *dev)
  1570. {
  1571. if (dev->radio_dev) {
  1572. if (-1 != dev->radio_dev->minor)
  1573. video_unregister_device(dev->radio_dev);
  1574. else
  1575. video_device_release(dev->radio_dev);
  1576. dev->radio_dev = NULL;
  1577. }
  1578. if (dev->vbi_dev) {
  1579. if (-1 != dev->vbi_dev->minor)
  1580. video_unregister_device(dev->vbi_dev);
  1581. else
  1582. video_device_release(dev->vbi_dev);
  1583. dev->vbi_dev = NULL;
  1584. }
  1585. if (dev->video_dev) {
  1586. if (-1 != dev->video_dev->minor)
  1587. video_unregister_device(dev->video_dev);
  1588. else
  1589. video_device_release(dev->video_dev);
  1590. dev->video_dev = NULL;
  1591. }
  1592. }
  1593. static int __devinit cx8800_initdev(struct pci_dev *pci_dev,
  1594. const struct pci_device_id *pci_id)
  1595. {
  1596. struct cx8800_dev *dev;
  1597. struct cx88_core *core;
  1598. int err;
  1599. dev = kzalloc(sizeof(*dev),GFP_KERNEL);
  1600. if (NULL == dev)
  1601. return -ENOMEM;
  1602. /* pci init */
  1603. dev->pci = pci_dev;
  1604. if (pci_enable_device(pci_dev)) {
  1605. err = -EIO;
  1606. goto fail_free;
  1607. }
  1608. core = cx88_core_get(dev->pci);
  1609. if (NULL == core) {
  1610. err = -EINVAL;
  1611. goto fail_free;
  1612. }
  1613. dev->core = core;
  1614. /* print pci info */
  1615. pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &dev->pci_rev);
  1616. pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &dev->pci_lat);
  1617. printk(KERN_INFO "%s/0: found at %s, rev: %d, irq: %d, "
  1618. "latency: %d, mmio: 0x%lx\n", core->name,
  1619. pci_name(pci_dev), dev->pci_rev, pci_dev->irq,
  1620. dev->pci_lat,pci_resource_start(pci_dev,0));
  1621. pci_set_master(pci_dev);
  1622. if (!pci_dma_supported(pci_dev,0xffffffff)) {
  1623. printk("%s/0: Oops: no 32bit PCI DMA ???\n",core->name);
  1624. err = -EIO;
  1625. goto fail_core;
  1626. }
  1627. /* initialize driver struct */
  1628. spin_lock_init(&dev->slock);
  1629. core->tvnorm = tvnorms;
  1630. /* init video dma queues */
  1631. INIT_LIST_HEAD(&dev->vidq.active);
  1632. INIT_LIST_HEAD(&dev->vidq.queued);
  1633. dev->vidq.timeout.function = cx8800_vid_timeout;
  1634. dev->vidq.timeout.data = (unsigned long)dev;
  1635. init_timer(&dev->vidq.timeout);
  1636. cx88_risc_stopper(dev->pci,&dev->vidq.stopper,
  1637. MO_VID_DMACNTRL,0x11,0x00);
  1638. /* init vbi dma queues */
  1639. INIT_LIST_HEAD(&dev->vbiq.active);
  1640. INIT_LIST_HEAD(&dev->vbiq.queued);
  1641. dev->vbiq.timeout.function = cx8800_vbi_timeout;
  1642. dev->vbiq.timeout.data = (unsigned long)dev;
  1643. init_timer(&dev->vbiq.timeout);
  1644. cx88_risc_stopper(dev->pci,&dev->vbiq.stopper,
  1645. MO_VID_DMACNTRL,0x88,0x00);
  1646. /* get irq */
  1647. err = request_irq(pci_dev->irq, cx8800_irq,
  1648. SA_SHIRQ | SA_INTERRUPT, core->name, dev);
  1649. if (err < 0) {
  1650. printk(KERN_ERR "%s: can't get IRQ %d\n",
  1651. core->name,pci_dev->irq);
  1652. goto fail_core;
  1653. }
  1654. cx_set(MO_PCI_INTMSK, core->pci_irqmask);
  1655. /* load and configure helper modules */
  1656. if (TUNER_ABSENT != core->tuner_type)
  1657. request_module("tuner");
  1658. if (core->tda9887_conf)
  1659. request_module("tda9887");
  1660. /* register v4l devices */
  1661. dev->video_dev = cx88_vdev_init(core,dev->pci,
  1662. &cx8800_video_template,"video");
  1663. err = video_register_device(dev->video_dev,VFL_TYPE_GRABBER,
  1664. video_nr[core->nr]);
  1665. if (err < 0) {
  1666. printk(KERN_INFO "%s: can't register video device\n",
  1667. core->name);
  1668. goto fail_unreg;
  1669. }
  1670. printk(KERN_INFO "%s/0: registered device video%d [v4l2]\n",
  1671. core->name,dev->video_dev->minor & 0x1f);
  1672. dev->vbi_dev = cx88_vdev_init(core,dev->pci,&cx8800_vbi_template,"vbi");
  1673. err = video_register_device(dev->vbi_dev,VFL_TYPE_VBI,
  1674. vbi_nr[core->nr]);
  1675. if (err < 0) {
  1676. printk(KERN_INFO "%s/0: can't register vbi device\n",
  1677. core->name);
  1678. goto fail_unreg;
  1679. }
  1680. printk(KERN_INFO "%s/0: registered device vbi%d\n",
  1681. core->name,dev->vbi_dev->minor & 0x1f);
  1682. if (core->has_radio) {
  1683. dev->radio_dev = cx88_vdev_init(core,dev->pci,
  1684. &cx8800_radio_template,"radio");
  1685. err = video_register_device(dev->radio_dev,VFL_TYPE_RADIO,
  1686. radio_nr[core->nr]);
  1687. if (err < 0) {
  1688. printk(KERN_INFO "%s/0: can't register radio device\n",
  1689. core->name);
  1690. goto fail_unreg;
  1691. }
  1692. printk(KERN_INFO "%s/0: registered device radio%d\n",
  1693. core->name,dev->radio_dev->minor & 0x1f);
  1694. }
  1695. /* everything worked */
  1696. list_add_tail(&dev->devlist,&cx8800_devlist);
  1697. pci_set_drvdata(pci_dev,dev);
  1698. /* initial device configuration */
  1699. mutex_lock(&core->lock);
  1700. cx88_set_tvnorm(core,tvnorms);
  1701. init_controls(core);
  1702. video_mux(core,0);
  1703. mutex_unlock(&core->lock);
  1704. /* start tvaudio thread */
  1705. if (core->tuner_type != TUNER_ABSENT)
  1706. core->kthread = kthread_run(cx88_audio_thread, core, "cx88 tvaudio");
  1707. return 0;
  1708. fail_unreg:
  1709. cx8800_unregister_video(dev);
  1710. free_irq(pci_dev->irq, dev);
  1711. fail_core:
  1712. cx88_core_put(core,dev->pci);
  1713. fail_free:
  1714. kfree(dev);
  1715. return err;
  1716. }
  1717. static void __devexit cx8800_finidev(struct pci_dev *pci_dev)
  1718. {
  1719. struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
  1720. struct cx88_core *core = dev->core;
  1721. /* stop thread */
  1722. if (core->kthread) {
  1723. kthread_stop(core->kthread);
  1724. core->kthread = NULL;
  1725. }
  1726. cx88_shutdown(core); /* FIXME */
  1727. pci_disable_device(pci_dev);
  1728. /* unregister stuff */
  1729. free_irq(pci_dev->irq, dev);
  1730. cx8800_unregister_video(dev);
  1731. pci_set_drvdata(pci_dev, NULL);
  1732. /* free memory */
  1733. btcx_riscmem_free(dev->pci,&dev->vidq.stopper);
  1734. list_del(&dev->devlist);
  1735. cx88_core_put(core,dev->pci);
  1736. kfree(dev);
  1737. }
  1738. static int cx8800_suspend(struct pci_dev *pci_dev, pm_message_t state)
  1739. {
  1740. struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
  1741. struct cx88_core *core = dev->core;
  1742. /* stop video+vbi capture */
  1743. spin_lock(&dev->slock);
  1744. if (!list_empty(&dev->vidq.active)) {
  1745. printk("%s: suspend video\n", core->name);
  1746. stop_video_dma(dev);
  1747. del_timer(&dev->vidq.timeout);
  1748. }
  1749. if (!list_empty(&dev->vbiq.active)) {
  1750. printk("%s: suspend vbi\n", core->name);
  1751. cx8800_stop_vbi_dma(dev);
  1752. del_timer(&dev->vbiq.timeout);
  1753. }
  1754. spin_unlock(&dev->slock);
  1755. /* FIXME -- shutdown device */
  1756. cx88_shutdown(core);
  1757. pci_save_state(pci_dev);
  1758. if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
  1759. pci_disable_device(pci_dev);
  1760. dev->state.disabled = 1;
  1761. }
  1762. return 0;
  1763. }
  1764. static int cx8800_resume(struct pci_dev *pci_dev)
  1765. {
  1766. struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
  1767. struct cx88_core *core = dev->core;
  1768. int err;
  1769. if (dev->state.disabled) {
  1770. err=pci_enable_device(pci_dev);
  1771. if (err) {
  1772. printk(KERN_ERR "%s: can't enable device\n",
  1773. core->name);
  1774. return err;
  1775. }
  1776. dev->state.disabled = 0;
  1777. }
  1778. err= pci_set_power_state(pci_dev, PCI_D0);
  1779. if (err) {
  1780. printk(KERN_ERR "%s: can't enable device\n",
  1781. core->name);
  1782. pci_disable_device(pci_dev);
  1783. dev->state.disabled = 1;
  1784. return err;
  1785. }
  1786. pci_restore_state(pci_dev);
  1787. /* FIXME: re-initialize hardware */
  1788. cx88_reset(core);
  1789. /* restart video+vbi capture */
  1790. spin_lock(&dev->slock);
  1791. if (!list_empty(&dev->vidq.active)) {
  1792. printk("%s: resume video\n", core->name);
  1793. restart_video_queue(dev,&dev->vidq);
  1794. }
  1795. if (!list_empty(&dev->vbiq.active)) {
  1796. printk("%s: resume vbi\n", core->name);
  1797. cx8800_restart_vbi_queue(dev,&dev->vbiq);
  1798. }
  1799. spin_unlock(&dev->slock);
  1800. return 0;
  1801. }
  1802. /* ----------------------------------------------------------- */
  1803. static struct pci_device_id cx8800_pci_tbl[] = {
  1804. {
  1805. .vendor = 0x14f1,
  1806. .device = 0x8800,
  1807. .subvendor = PCI_ANY_ID,
  1808. .subdevice = PCI_ANY_ID,
  1809. },{
  1810. /* --- end of list --- */
  1811. }
  1812. };
  1813. MODULE_DEVICE_TABLE(pci, cx8800_pci_tbl);
  1814. static struct pci_driver cx8800_pci_driver = {
  1815. .name = "cx8800",
  1816. .id_table = cx8800_pci_tbl,
  1817. .probe = cx8800_initdev,
  1818. .remove = __devexit_p(cx8800_finidev),
  1819. .suspend = cx8800_suspend,
  1820. .resume = cx8800_resume,
  1821. };
  1822. static int cx8800_init(void)
  1823. {
  1824. printk(KERN_INFO "cx2388x v4l2 driver version %d.%d.%d loaded\n",
  1825. (CX88_VERSION_CODE >> 16) & 0xff,
  1826. (CX88_VERSION_CODE >> 8) & 0xff,
  1827. CX88_VERSION_CODE & 0xff);
  1828. #ifdef SNAPSHOT
  1829. printk(KERN_INFO "cx2388x: snapshot date %04d-%02d-%02d\n",
  1830. SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
  1831. #endif
  1832. return pci_register_driver(&cx8800_pci_driver);
  1833. }
  1834. static void cx8800_fini(void)
  1835. {
  1836. pci_unregister_driver(&cx8800_pci_driver);
  1837. }
  1838. module_init(cx8800_init);
  1839. module_exit(cx8800_fini);
  1840. EXPORT_SYMBOL(cx88_do_ioctl);
  1841. /* ----------------------------------------------------------- */
  1842. /*
  1843. * Local variables:
  1844. * c-basic-offset: 8
  1845. * End:
  1846. * 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
  1847. */