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