cx88-video.c 52 KB

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