cx88-video.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671
  1. /*
  2. *
  3. * device driver for Conexant 2388x based TV cards
  4. * video4linux video interface
  5. *
  6. * (c) 2003-04 Gerd Knorr <[email protected]> [SuSE Labs]
  7. *
  8. * (c) 2005-2006 Mauro Carvalho Chehab <[email protected]>
  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/kmod.h>
  31. #include <linux/kernel.h>
  32. #include <linux/slab.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/dma-mapping.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. #include <media/v4l2-ioctl.h>
  41. #include <media/v4l2-event.h>
  42. #include <media/i2c/wm8775.h>
  43. MODULE_DESCRIPTION("v4l2 driver module for cx2388x based TV cards");
  44. MODULE_AUTHOR("Gerd Knorr <[email protected]> [SuSE Labs]");
  45. MODULE_LICENSE("GPL");
  46. MODULE_VERSION(CX88_VERSION);
  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;
  58. module_param(video_debug,int,0644);
  59. MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
  60. static unsigned int irq_debug;
  61. module_param(irq_debug,int,0644);
  62. MODULE_PARM_DESC(irq_debug,"enable debug messages [IRQ handler]");
  63. #define dprintk(level,fmt, arg...) if (video_debug >= level) \
  64. printk(KERN_DEBUG "%s/0: " fmt, core->name , ## arg)
  65. /* ------------------------------------------------------------------- */
  66. /* static data */
  67. static const struct cx8800_fmt formats[] = {
  68. {
  69. .name = "8 bpp, gray",
  70. .fourcc = V4L2_PIX_FMT_GREY,
  71. .cxformat = ColorFormatY8,
  72. .depth = 8,
  73. .flags = FORMAT_FLAGS_PACKED,
  74. },{
  75. .name = "15 bpp RGB, le",
  76. .fourcc = V4L2_PIX_FMT_RGB555,
  77. .cxformat = ColorFormatRGB15,
  78. .depth = 16,
  79. .flags = FORMAT_FLAGS_PACKED,
  80. },{
  81. .name = "15 bpp RGB, be",
  82. .fourcc = V4L2_PIX_FMT_RGB555X,
  83. .cxformat = ColorFormatRGB15 | ColorFormatBSWAP,
  84. .depth = 16,
  85. .flags = FORMAT_FLAGS_PACKED,
  86. },{
  87. .name = "16 bpp RGB, le",
  88. .fourcc = V4L2_PIX_FMT_RGB565,
  89. .cxformat = ColorFormatRGB16,
  90. .depth = 16,
  91. .flags = FORMAT_FLAGS_PACKED,
  92. },{
  93. .name = "16 bpp RGB, be",
  94. .fourcc = V4L2_PIX_FMT_RGB565X,
  95. .cxformat = ColorFormatRGB16 | ColorFormatBSWAP,
  96. .depth = 16,
  97. .flags = FORMAT_FLAGS_PACKED,
  98. },{
  99. .name = "24 bpp RGB, le",
  100. .fourcc = V4L2_PIX_FMT_BGR24,
  101. .cxformat = ColorFormatRGB24,
  102. .depth = 24,
  103. .flags = FORMAT_FLAGS_PACKED,
  104. },{
  105. .name = "32 bpp RGB, le",
  106. .fourcc = V4L2_PIX_FMT_BGR32,
  107. .cxformat = ColorFormatRGB32,
  108. .depth = 32,
  109. .flags = FORMAT_FLAGS_PACKED,
  110. },{
  111. .name = "32 bpp RGB, be",
  112. .fourcc = V4L2_PIX_FMT_RGB32,
  113. .cxformat = ColorFormatRGB32 | ColorFormatBSWAP | ColorFormatWSWAP,
  114. .depth = 32,
  115. .flags = FORMAT_FLAGS_PACKED,
  116. },{
  117. .name = "4:2:2, packed, YUYV",
  118. .fourcc = V4L2_PIX_FMT_YUYV,
  119. .cxformat = ColorFormatYUY2,
  120. .depth = 16,
  121. .flags = FORMAT_FLAGS_PACKED,
  122. },{
  123. .name = "4:2:2, packed, UYVY",
  124. .fourcc = V4L2_PIX_FMT_UYVY,
  125. .cxformat = ColorFormatYUY2 | ColorFormatBSWAP,
  126. .depth = 16,
  127. .flags = FORMAT_FLAGS_PACKED,
  128. },
  129. };
  130. static const struct cx8800_fmt* format_by_fourcc(unsigned int fourcc)
  131. {
  132. unsigned int i;
  133. for (i = 0; i < ARRAY_SIZE(formats); i++)
  134. if (formats[i].fourcc == fourcc)
  135. return formats+i;
  136. return NULL;
  137. }
  138. /* ------------------------------------------------------------------- */
  139. struct cx88_ctrl {
  140. /* control information */
  141. u32 id;
  142. s32 minimum;
  143. s32 maximum;
  144. u32 step;
  145. s32 default_value;
  146. /* control register information */
  147. u32 off;
  148. u32 reg;
  149. u32 sreg;
  150. u32 mask;
  151. u32 shift;
  152. };
  153. static const struct cx88_ctrl cx8800_vid_ctls[] = {
  154. /* --- video --- */
  155. {
  156. .id = V4L2_CID_BRIGHTNESS,
  157. .minimum = 0x00,
  158. .maximum = 0xff,
  159. .step = 1,
  160. .default_value = 0x7f,
  161. .off = 128,
  162. .reg = MO_CONTR_BRIGHT,
  163. .mask = 0x00ff,
  164. .shift = 0,
  165. },{
  166. .id = V4L2_CID_CONTRAST,
  167. .minimum = 0,
  168. .maximum = 0xff,
  169. .step = 1,
  170. .default_value = 0x3f,
  171. .off = 0,
  172. .reg = MO_CONTR_BRIGHT,
  173. .mask = 0xff00,
  174. .shift = 8,
  175. },{
  176. .id = V4L2_CID_HUE,
  177. .minimum = 0,
  178. .maximum = 0xff,
  179. .step = 1,
  180. .default_value = 0x7f,
  181. .off = 128,
  182. .reg = MO_HUE,
  183. .mask = 0x00ff,
  184. .shift = 0,
  185. },{
  186. /* strictly, this only describes only U saturation.
  187. * V saturation is handled specially through code.
  188. */
  189. .id = V4L2_CID_SATURATION,
  190. .minimum = 0,
  191. .maximum = 0xff,
  192. .step = 1,
  193. .default_value = 0x7f,
  194. .off = 0,
  195. .reg = MO_UV_SATURATION,
  196. .mask = 0x00ff,
  197. .shift = 0,
  198. }, {
  199. .id = V4L2_CID_SHARPNESS,
  200. .minimum = 0,
  201. .maximum = 4,
  202. .step = 1,
  203. .default_value = 0x0,
  204. .off = 0,
  205. /* NOTE: the value is converted and written to both even
  206. and odd registers in the code */
  207. .reg = MO_FILTER_ODD,
  208. .mask = 7 << 7,
  209. .shift = 7,
  210. }, {
  211. .id = V4L2_CID_CHROMA_AGC,
  212. .minimum = 0,
  213. .maximum = 1,
  214. .default_value = 0x1,
  215. .reg = MO_INPUT_FORMAT,
  216. .mask = 1 << 10,
  217. .shift = 10,
  218. }, {
  219. .id = V4L2_CID_COLOR_KILLER,
  220. .minimum = 0,
  221. .maximum = 1,
  222. .default_value = 0x1,
  223. .reg = MO_INPUT_FORMAT,
  224. .mask = 1 << 9,
  225. .shift = 9,
  226. }, {
  227. .id = V4L2_CID_BAND_STOP_FILTER,
  228. .minimum = 0,
  229. .maximum = 1,
  230. .step = 1,
  231. .default_value = 0x0,
  232. .off = 0,
  233. .reg = MO_HTOTAL,
  234. .mask = 3 << 11,
  235. .shift = 11,
  236. }
  237. };
  238. static const struct cx88_ctrl cx8800_aud_ctls[] = {
  239. {
  240. /* --- audio --- */
  241. .id = V4L2_CID_AUDIO_MUTE,
  242. .minimum = 0,
  243. .maximum = 1,
  244. .default_value = 1,
  245. .reg = AUD_VOL_CTL,
  246. .sreg = SHADOW_AUD_VOL_CTL,
  247. .mask = (1 << 6),
  248. .shift = 6,
  249. },{
  250. .id = V4L2_CID_AUDIO_VOLUME,
  251. .minimum = 0,
  252. .maximum = 0x3f,
  253. .step = 1,
  254. .default_value = 0x3f,
  255. .reg = AUD_VOL_CTL,
  256. .sreg = SHADOW_AUD_VOL_CTL,
  257. .mask = 0x3f,
  258. .shift = 0,
  259. },{
  260. .id = V4L2_CID_AUDIO_BALANCE,
  261. .minimum = 0,
  262. .maximum = 0x7f,
  263. .step = 1,
  264. .default_value = 0x40,
  265. .reg = AUD_BAL_CTL,
  266. .sreg = SHADOW_AUD_BAL_CTL,
  267. .mask = 0x7f,
  268. .shift = 0,
  269. }
  270. };
  271. enum {
  272. CX8800_VID_CTLS = ARRAY_SIZE(cx8800_vid_ctls),
  273. CX8800_AUD_CTLS = ARRAY_SIZE(cx8800_aud_ctls),
  274. };
  275. /* ------------------------------------------------------------------ */
  276. int cx88_video_mux(struct cx88_core *core, unsigned int input)
  277. {
  278. /* struct cx88_core *core = dev->core; */
  279. dprintk(1,"video_mux: %d [vmux=%d,gpio=0x%x,0x%x,0x%x,0x%x]\n",
  280. input, INPUT(input).vmux,
  281. INPUT(input).gpio0,INPUT(input).gpio1,
  282. INPUT(input).gpio2,INPUT(input).gpio3);
  283. core->input = input;
  284. cx_andor(MO_INPUT_FORMAT, 0x03 << 14, INPUT(input).vmux << 14);
  285. cx_write(MO_GP3_IO, INPUT(input).gpio3);
  286. cx_write(MO_GP0_IO, INPUT(input).gpio0);
  287. cx_write(MO_GP1_IO, INPUT(input).gpio1);
  288. cx_write(MO_GP2_IO, INPUT(input).gpio2);
  289. switch (INPUT(input).type) {
  290. case CX88_VMUX_SVIDEO:
  291. cx_set(MO_AFECFG_IO, 0x00000001);
  292. cx_set(MO_INPUT_FORMAT, 0x00010010);
  293. cx_set(MO_FILTER_EVEN, 0x00002020);
  294. cx_set(MO_FILTER_ODD, 0x00002020);
  295. break;
  296. default:
  297. cx_clear(MO_AFECFG_IO, 0x00000001);
  298. cx_clear(MO_INPUT_FORMAT, 0x00010010);
  299. cx_clear(MO_FILTER_EVEN, 0x00002020);
  300. cx_clear(MO_FILTER_ODD, 0x00002020);
  301. break;
  302. }
  303. /* if there are audioroutes defined, we have an external
  304. ADC to deal with audio */
  305. if (INPUT(input).audioroute) {
  306. /* The wm8775 module has the "2" route hardwired into
  307. the initialization. Some boards may use different
  308. routes for different inputs. HVR-1300 surely does */
  309. if (core->sd_wm8775) {
  310. call_all(core, audio, s_routing,
  311. INPUT(input).audioroute, 0, 0);
  312. }
  313. /* cx2388's C-ADC is connected to the tuner only.
  314. When used with S-Video, that ADC is busy dealing with
  315. chroma, so an external must be used for baseband audio */
  316. if (INPUT(input).type != CX88_VMUX_TELEVISION &&
  317. INPUT(input).type != CX88_VMUX_CABLE) {
  318. /* "I2S ADC mode" */
  319. core->tvaudio = WW_I2SADC;
  320. cx88_set_tvaudio(core);
  321. } else {
  322. /* Normal mode */
  323. cx_write(AUD_I2SCNTL, 0x0);
  324. cx_clear(AUD_CTL, EN_I2SIN_ENABLE);
  325. }
  326. }
  327. return 0;
  328. }
  329. EXPORT_SYMBOL(cx88_video_mux);
  330. /* ------------------------------------------------------------------ */
  331. static int start_video_dma(struct cx8800_dev *dev,
  332. struct cx88_dmaqueue *q,
  333. struct cx88_buffer *buf)
  334. {
  335. struct cx88_core *core = dev->core;
  336. /* setup fifo + format */
  337. cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH21],
  338. buf->bpl, buf->risc.dma);
  339. cx88_set_scale(core, core->width, core->height, core->field);
  340. cx_write(MO_COLOR_CTRL, dev->fmt->cxformat | ColorFormatGamma);
  341. /* reset counter */
  342. cx_write(MO_VIDY_GPCNTRL,GP_COUNT_CONTROL_RESET);
  343. q->count = 0;
  344. /* enable irqs */
  345. cx_set(MO_PCI_INTMSK, core->pci_irqmask | PCI_INT_VIDINT);
  346. /* Enables corresponding bits at PCI_INT_STAT:
  347. bits 0 to 4: video, audio, transport stream, VIP, Host
  348. bit 7: timer
  349. bits 8 and 9: DMA complete for: SRC, DST
  350. bits 10 and 11: BERR signal asserted for RISC: RD, WR
  351. bits 12 to 15: BERR signal asserted for: BRDG, SRC, DST, IPB
  352. */
  353. cx_set(MO_VID_INTMSK, 0x0f0011);
  354. /* enable capture */
  355. cx_set(VID_CAPTURE_CONTROL,0x06);
  356. /* start dma */
  357. cx_set(MO_DEV_CNTRL2, (1<<5));
  358. cx_set(MO_VID_DMACNTRL, 0x11); /* Planar Y and packed FIFO and RISC enable */
  359. return 0;
  360. }
  361. #ifdef CONFIG_PM
  362. static int stop_video_dma(struct cx8800_dev *dev)
  363. {
  364. struct cx88_core *core = dev->core;
  365. /* stop dma */
  366. cx_clear(MO_VID_DMACNTRL, 0x11);
  367. /* disable capture */
  368. cx_clear(VID_CAPTURE_CONTROL,0x06);
  369. /* disable irqs */
  370. cx_clear(MO_PCI_INTMSK, PCI_INT_VIDINT);
  371. cx_clear(MO_VID_INTMSK, 0x0f0011);
  372. return 0;
  373. }
  374. static int restart_video_queue(struct cx8800_dev *dev,
  375. struct cx88_dmaqueue *q)
  376. {
  377. struct cx88_core *core = dev->core;
  378. struct cx88_buffer *buf;
  379. if (!list_empty(&q->active)) {
  380. buf = list_entry(q->active.next, struct cx88_buffer, list);
  381. dprintk(2,"restart_queue [%p/%d]: restart dma\n",
  382. buf, buf->vb.vb2_buf.index);
  383. start_video_dma(dev, q, buf);
  384. }
  385. return 0;
  386. }
  387. #endif
  388. /* ------------------------------------------------------------------ */
  389. static int queue_setup(struct vb2_queue *q,
  390. unsigned int *num_buffers, unsigned int *num_planes,
  391. unsigned int sizes[], struct device *alloc_devs[])
  392. {
  393. struct cx8800_dev *dev = q->drv_priv;
  394. struct cx88_core *core = dev->core;
  395. *num_planes = 1;
  396. sizes[0] = (dev->fmt->depth * core->width * core->height) >> 3;
  397. return 0;
  398. }
  399. static int buffer_prepare(struct vb2_buffer *vb)
  400. {
  401. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  402. struct cx8800_dev *dev = vb->vb2_queue->drv_priv;
  403. struct cx88_core *core = dev->core;
  404. struct cx88_buffer *buf = container_of(vbuf, struct cx88_buffer, vb);
  405. struct sg_table *sgt = vb2_dma_sg_plane_desc(vb, 0);
  406. buf->bpl = core->width * dev->fmt->depth >> 3;
  407. if (vb2_plane_size(vb, 0) < core->height * buf->bpl)
  408. return -EINVAL;
  409. vb2_set_plane_payload(vb, 0, core->height * buf->bpl);
  410. switch (core->field) {
  411. case V4L2_FIELD_TOP:
  412. cx88_risc_buffer(dev->pci, &buf->risc,
  413. sgt->sgl, 0, UNSET,
  414. buf->bpl, 0, core->height);
  415. break;
  416. case V4L2_FIELD_BOTTOM:
  417. cx88_risc_buffer(dev->pci, &buf->risc,
  418. sgt->sgl, UNSET, 0,
  419. buf->bpl, 0, core->height);
  420. break;
  421. case V4L2_FIELD_SEQ_TB:
  422. cx88_risc_buffer(dev->pci, &buf->risc,
  423. sgt->sgl,
  424. 0, buf->bpl * (core->height >> 1),
  425. buf->bpl, 0,
  426. core->height >> 1);
  427. break;
  428. case V4L2_FIELD_SEQ_BT:
  429. cx88_risc_buffer(dev->pci, &buf->risc,
  430. sgt->sgl,
  431. buf->bpl * (core->height >> 1), 0,
  432. buf->bpl, 0,
  433. core->height >> 1);
  434. break;
  435. case V4L2_FIELD_INTERLACED:
  436. default:
  437. cx88_risc_buffer(dev->pci, &buf->risc,
  438. sgt->sgl, 0, buf->bpl,
  439. buf->bpl, buf->bpl,
  440. core->height >> 1);
  441. break;
  442. }
  443. dprintk(2,"[%p/%d] buffer_prepare - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
  444. buf, buf->vb.vb2_buf.index,
  445. core->width, core->height, dev->fmt->depth, dev->fmt->name,
  446. (unsigned long)buf->risc.dma);
  447. return 0;
  448. }
  449. static void buffer_finish(struct vb2_buffer *vb)
  450. {
  451. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  452. struct cx8800_dev *dev = vb->vb2_queue->drv_priv;
  453. struct cx88_buffer *buf = container_of(vbuf, struct cx88_buffer, vb);
  454. struct cx88_riscmem *risc = &buf->risc;
  455. if (risc->cpu)
  456. pci_free_consistent(dev->pci, risc->size, risc->cpu, risc->dma);
  457. memset(risc, 0, sizeof(*risc));
  458. }
  459. static void buffer_queue(struct vb2_buffer *vb)
  460. {
  461. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  462. struct cx8800_dev *dev = vb->vb2_queue->drv_priv;
  463. struct cx88_buffer *buf = container_of(vbuf, struct cx88_buffer, vb);
  464. struct cx88_buffer *prev;
  465. struct cx88_core *core = dev->core;
  466. struct cx88_dmaqueue *q = &dev->vidq;
  467. /* add jump to start */
  468. buf->risc.cpu[1] = cpu_to_le32(buf->risc.dma + 8);
  469. buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_CNT_INC);
  470. buf->risc.jmp[1] = cpu_to_le32(buf->risc.dma + 8);
  471. if (list_empty(&q->active)) {
  472. list_add_tail(&buf->list, &q->active);
  473. dprintk(2,"[%p/%d] buffer_queue - first active\n",
  474. buf, buf->vb.vb2_buf.index);
  475. } else {
  476. buf->risc.cpu[0] |= cpu_to_le32(RISC_IRQ1);
  477. prev = list_entry(q->active.prev, struct cx88_buffer, list);
  478. list_add_tail(&buf->list, &q->active);
  479. prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
  480. dprintk(2, "[%p/%d] buffer_queue - append to active\n",
  481. buf, buf->vb.vb2_buf.index);
  482. }
  483. }
  484. static int start_streaming(struct vb2_queue *q, unsigned int count)
  485. {
  486. struct cx8800_dev *dev = q->drv_priv;
  487. struct cx88_dmaqueue *dmaq = &dev->vidq;
  488. struct cx88_buffer *buf = list_entry(dmaq->active.next,
  489. struct cx88_buffer, list);
  490. start_video_dma(dev, dmaq, buf);
  491. return 0;
  492. }
  493. static void stop_streaming(struct vb2_queue *q)
  494. {
  495. struct cx8800_dev *dev = q->drv_priv;
  496. struct cx88_core *core = dev->core;
  497. struct cx88_dmaqueue *dmaq = &dev->vidq;
  498. unsigned long flags;
  499. cx_clear(MO_VID_DMACNTRL, 0x11);
  500. cx_clear(VID_CAPTURE_CONTROL, 0x06);
  501. spin_lock_irqsave(&dev->slock, flags);
  502. while (!list_empty(&dmaq->active)) {
  503. struct cx88_buffer *buf = list_entry(dmaq->active.next,
  504. struct cx88_buffer, list);
  505. list_del(&buf->list);
  506. vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
  507. }
  508. spin_unlock_irqrestore(&dev->slock, flags);
  509. }
  510. static const struct vb2_ops cx8800_video_qops = {
  511. .queue_setup = queue_setup,
  512. .buf_prepare = buffer_prepare,
  513. .buf_finish = buffer_finish,
  514. .buf_queue = buffer_queue,
  515. .wait_prepare = vb2_ops_wait_prepare,
  516. .wait_finish = vb2_ops_wait_finish,
  517. .start_streaming = start_streaming,
  518. .stop_streaming = stop_streaming,
  519. };
  520. /* ------------------------------------------------------------------ */
  521. static int radio_open(struct file *file)
  522. {
  523. struct cx8800_dev *dev = video_drvdata(file);
  524. struct cx88_core *core = dev->core;
  525. int ret = v4l2_fh_open(file);
  526. if (ret)
  527. return ret;
  528. cx_write(MO_GP3_IO, core->board.radio.gpio3);
  529. cx_write(MO_GP0_IO, core->board.radio.gpio0);
  530. cx_write(MO_GP1_IO, core->board.radio.gpio1);
  531. cx_write(MO_GP2_IO, core->board.radio.gpio2);
  532. if (core->board.radio.audioroute) {
  533. if (core->sd_wm8775) {
  534. call_all(core, audio, s_routing,
  535. core->board.radio.audioroute, 0, 0);
  536. }
  537. /* "I2S ADC mode" */
  538. core->tvaudio = WW_I2SADC;
  539. cx88_set_tvaudio(core);
  540. } else {
  541. /* FM Mode */
  542. core->tvaudio = WW_FM;
  543. cx88_set_tvaudio(core);
  544. cx88_set_stereo(core, V4L2_TUNER_MODE_STEREO, 1);
  545. }
  546. call_all(core, tuner, s_radio);
  547. return 0;
  548. }
  549. /* ------------------------------------------------------------------ */
  550. /* VIDEO CTRL IOCTLS */
  551. static int cx8800_s_vid_ctrl(struct v4l2_ctrl *ctrl)
  552. {
  553. struct cx88_core *core =
  554. container_of(ctrl->handler, struct cx88_core, video_hdl);
  555. const struct cx88_ctrl *cc = ctrl->priv;
  556. u32 value, mask;
  557. mask = cc->mask;
  558. switch (ctrl->id) {
  559. case V4L2_CID_SATURATION:
  560. /* special v_sat handling */
  561. value = ((ctrl->val - cc->off) << cc->shift) & cc->mask;
  562. if (core->tvnorm & V4L2_STD_SECAM) {
  563. /* For SECAM, both U and V sat should be equal */
  564. value = value << 8 | value;
  565. } else {
  566. /* Keeps U Saturation proportional to V Sat */
  567. value = (value * 0x5a) / 0x7f << 8 | value;
  568. }
  569. mask = 0xffff;
  570. break;
  571. case V4L2_CID_SHARPNESS:
  572. /* 0b000, 0b100, 0b101, 0b110, or 0b111 */
  573. value = (ctrl->val < 1 ? 0 : ((ctrl->val + 3) << 7));
  574. /* needs to be set for both fields */
  575. cx_andor(MO_FILTER_EVEN, mask, value);
  576. break;
  577. case V4L2_CID_CHROMA_AGC:
  578. value = ((ctrl->val - cc->off) << cc->shift) & cc->mask;
  579. break;
  580. default:
  581. value = ((ctrl->val - cc->off) << cc->shift) & cc->mask;
  582. break;
  583. }
  584. dprintk(1, "set_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
  585. ctrl->id, ctrl->name, ctrl->val, cc->reg, value,
  586. mask, cc->sreg ? " [shadowed]" : "");
  587. if (cc->sreg)
  588. cx_sandor(cc->sreg, cc->reg, mask, value);
  589. else
  590. cx_andor(cc->reg, mask, value);
  591. return 0;
  592. }
  593. static int cx8800_s_aud_ctrl(struct v4l2_ctrl *ctrl)
  594. {
  595. struct cx88_core *core =
  596. container_of(ctrl->handler, struct cx88_core, audio_hdl);
  597. const struct cx88_ctrl *cc = ctrl->priv;
  598. u32 value,mask;
  599. /* Pass changes onto any WM8775 */
  600. if (core->sd_wm8775) {
  601. switch (ctrl->id) {
  602. case V4L2_CID_AUDIO_MUTE:
  603. wm8775_s_ctrl(core, ctrl->id, ctrl->val);
  604. break;
  605. case V4L2_CID_AUDIO_VOLUME:
  606. wm8775_s_ctrl(core, ctrl->id, (ctrl->val) ?
  607. (0x90 + ctrl->val) << 8 : 0);
  608. break;
  609. case V4L2_CID_AUDIO_BALANCE:
  610. wm8775_s_ctrl(core, ctrl->id, ctrl->val << 9);
  611. break;
  612. default:
  613. break;
  614. }
  615. }
  616. mask = cc->mask;
  617. switch (ctrl->id) {
  618. case V4L2_CID_AUDIO_BALANCE:
  619. value = (ctrl->val < 0x40) ? (0x7f - ctrl->val) : (ctrl->val - 0x40);
  620. break;
  621. case V4L2_CID_AUDIO_VOLUME:
  622. value = 0x3f - (ctrl->val & 0x3f);
  623. break;
  624. default:
  625. value = ((ctrl->val - cc->off) << cc->shift) & cc->mask;
  626. break;
  627. }
  628. dprintk(1,"set_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
  629. ctrl->id, ctrl->name, ctrl->val, cc->reg, value,
  630. mask, cc->sreg ? " [shadowed]" : "");
  631. if (cc->sreg)
  632. cx_sandor(cc->sreg, cc->reg, mask, value);
  633. else
  634. cx_andor(cc->reg, mask, value);
  635. return 0;
  636. }
  637. /* ------------------------------------------------------------------ */
  638. /* VIDEO IOCTLS */
  639. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  640. struct v4l2_format *f)
  641. {
  642. struct cx8800_dev *dev = video_drvdata(file);
  643. struct cx88_core *core = dev->core;
  644. f->fmt.pix.width = core->width;
  645. f->fmt.pix.height = core->height;
  646. f->fmt.pix.field = core->field;
  647. f->fmt.pix.pixelformat = dev->fmt->fourcc;
  648. f->fmt.pix.bytesperline =
  649. (f->fmt.pix.width * dev->fmt->depth) >> 3;
  650. f->fmt.pix.sizeimage =
  651. f->fmt.pix.height * f->fmt.pix.bytesperline;
  652. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  653. return 0;
  654. }
  655. static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  656. struct v4l2_format *f)
  657. {
  658. struct cx8800_dev *dev = video_drvdata(file);
  659. struct cx88_core *core = dev->core;
  660. const struct cx8800_fmt *fmt;
  661. enum v4l2_field field;
  662. unsigned int maxw, maxh;
  663. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  664. if (NULL == fmt)
  665. return -EINVAL;
  666. maxw = norm_maxw(core->tvnorm);
  667. maxh = norm_maxh(core->tvnorm);
  668. field = f->fmt.pix.field;
  669. switch (field) {
  670. case V4L2_FIELD_TOP:
  671. case V4L2_FIELD_BOTTOM:
  672. case V4L2_FIELD_INTERLACED:
  673. case V4L2_FIELD_SEQ_BT:
  674. case V4L2_FIELD_SEQ_TB:
  675. break;
  676. default:
  677. field = (f->fmt.pix.height > maxh / 2)
  678. ? V4L2_FIELD_INTERLACED
  679. : V4L2_FIELD_BOTTOM;
  680. break;
  681. }
  682. if (V4L2_FIELD_HAS_T_OR_B(field))
  683. maxh /= 2;
  684. v4l_bound_align_image(&f->fmt.pix.width, 48, maxw, 2,
  685. &f->fmt.pix.height, 32, maxh, 0, 0);
  686. f->fmt.pix.field = field;
  687. f->fmt.pix.bytesperline =
  688. (f->fmt.pix.width * fmt->depth) >> 3;
  689. f->fmt.pix.sizeimage =
  690. f->fmt.pix.height * f->fmt.pix.bytesperline;
  691. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  692. return 0;
  693. }
  694. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  695. struct v4l2_format *f)
  696. {
  697. struct cx8800_dev *dev = video_drvdata(file);
  698. struct cx88_core *core = dev->core;
  699. int err = vidioc_try_fmt_vid_cap (file,priv,f);
  700. if (0 != err)
  701. return err;
  702. if (vb2_is_busy(&dev->vb2_vidq) || vb2_is_busy(&dev->vb2_vbiq))
  703. return -EBUSY;
  704. if (core->dvbdev && vb2_is_busy(&core->dvbdev->vb2_mpegq))
  705. return -EBUSY;
  706. dev->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  707. core->width = f->fmt.pix.width;
  708. core->height = f->fmt.pix.height;
  709. core->field = f->fmt.pix.field;
  710. return 0;
  711. }
  712. void cx88_querycap(struct file *file, struct cx88_core *core,
  713. struct v4l2_capability *cap)
  714. {
  715. struct video_device *vdev = video_devdata(file);
  716. strlcpy(cap->card, core->board.name, sizeof(cap->card));
  717. cap->device_caps = V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
  718. if (UNSET != core->board.tuner_type)
  719. cap->device_caps |= V4L2_CAP_TUNER;
  720. switch (vdev->vfl_type) {
  721. case VFL_TYPE_RADIO:
  722. cap->device_caps = V4L2_CAP_RADIO | V4L2_CAP_TUNER;
  723. break;
  724. case VFL_TYPE_GRABBER:
  725. cap->device_caps |= V4L2_CAP_VIDEO_CAPTURE;
  726. break;
  727. case VFL_TYPE_VBI:
  728. cap->device_caps |= V4L2_CAP_VBI_CAPTURE;
  729. break;
  730. }
  731. cap->capabilities = cap->device_caps | V4L2_CAP_VIDEO_CAPTURE |
  732. V4L2_CAP_VBI_CAPTURE | V4L2_CAP_DEVICE_CAPS;
  733. if (core->board.radio.type == CX88_RADIO)
  734. cap->capabilities |= V4L2_CAP_RADIO;
  735. }
  736. EXPORT_SYMBOL(cx88_querycap);
  737. static int vidioc_querycap(struct file *file, void *priv,
  738. struct v4l2_capability *cap)
  739. {
  740. struct cx8800_dev *dev = video_drvdata(file);
  741. struct cx88_core *core = dev->core;
  742. strcpy(cap->driver, "cx8800");
  743. sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
  744. cx88_querycap(file, core, cap);
  745. return 0;
  746. }
  747. static int vidioc_enum_fmt_vid_cap (struct file *file, void *priv,
  748. struct v4l2_fmtdesc *f)
  749. {
  750. if (unlikely(f->index >= ARRAY_SIZE(formats)))
  751. return -EINVAL;
  752. strlcpy(f->description,formats[f->index].name,sizeof(f->description));
  753. f->pixelformat = formats[f->index].fourcc;
  754. return 0;
  755. }
  756. static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *tvnorm)
  757. {
  758. struct cx8800_dev *dev = video_drvdata(file);
  759. struct cx88_core *core = dev->core;
  760. *tvnorm = core->tvnorm;
  761. return 0;
  762. }
  763. static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id tvnorms)
  764. {
  765. struct cx8800_dev *dev = video_drvdata(file);
  766. struct cx88_core *core = dev->core;
  767. return cx88_set_tvnorm(core, tvnorms);
  768. }
  769. /* only one input in this sample driver */
  770. int cx88_enum_input (struct cx88_core *core,struct v4l2_input *i)
  771. {
  772. static const char * const iname[] = {
  773. [ CX88_VMUX_COMPOSITE1 ] = "Composite1",
  774. [ CX88_VMUX_COMPOSITE2 ] = "Composite2",
  775. [ CX88_VMUX_COMPOSITE3 ] = "Composite3",
  776. [ CX88_VMUX_COMPOSITE4 ] = "Composite4",
  777. [ CX88_VMUX_SVIDEO ] = "S-Video",
  778. [ CX88_VMUX_TELEVISION ] = "Television",
  779. [ CX88_VMUX_CABLE ] = "Cable TV",
  780. [ CX88_VMUX_DVB ] = "DVB",
  781. [ CX88_VMUX_DEBUG ] = "for debug only",
  782. };
  783. unsigned int n = i->index;
  784. if (n >= 4)
  785. return -EINVAL;
  786. if (0 == INPUT(n).type)
  787. return -EINVAL;
  788. i->type = V4L2_INPUT_TYPE_CAMERA;
  789. strcpy(i->name,iname[INPUT(n).type]);
  790. if ((CX88_VMUX_TELEVISION == INPUT(n).type) ||
  791. (CX88_VMUX_CABLE == INPUT(n).type)) {
  792. i->type = V4L2_INPUT_TYPE_TUNER;
  793. }
  794. i->std = CX88_NORMS;
  795. return 0;
  796. }
  797. EXPORT_SYMBOL(cx88_enum_input);
  798. static int vidioc_enum_input (struct file *file, void *priv,
  799. struct v4l2_input *i)
  800. {
  801. struct cx8800_dev *dev = video_drvdata(file);
  802. struct cx88_core *core = dev->core;
  803. return cx88_enum_input (core,i);
  804. }
  805. static int vidioc_g_input (struct file *file, void *priv, unsigned int *i)
  806. {
  807. struct cx8800_dev *dev = video_drvdata(file);
  808. struct cx88_core *core = dev->core;
  809. *i = core->input;
  810. return 0;
  811. }
  812. static int vidioc_s_input (struct file *file, void *priv, unsigned int i)
  813. {
  814. struct cx8800_dev *dev = video_drvdata(file);
  815. struct cx88_core *core = dev->core;
  816. if (i >= 4)
  817. return -EINVAL;
  818. if (0 == INPUT(i).type)
  819. return -EINVAL;
  820. cx88_newstation(core);
  821. cx88_video_mux(core,i);
  822. return 0;
  823. }
  824. static int vidioc_g_tuner (struct file *file, void *priv,
  825. struct v4l2_tuner *t)
  826. {
  827. struct cx8800_dev *dev = video_drvdata(file);
  828. struct cx88_core *core = dev->core;
  829. u32 reg;
  830. if (unlikely(UNSET == core->board.tuner_type))
  831. return -EINVAL;
  832. if (0 != t->index)
  833. return -EINVAL;
  834. strcpy(t->name, "Television");
  835. t->capability = V4L2_TUNER_CAP_NORM;
  836. t->rangehigh = 0xffffffffUL;
  837. call_all(core, tuner, g_tuner, t);
  838. cx88_get_stereo(core ,t);
  839. reg = cx_read(MO_DEVICE_STATUS);
  840. t->signal = (reg & (1<<5)) ? 0xffff : 0x0000;
  841. return 0;
  842. }
  843. static int vidioc_s_tuner (struct file *file, void *priv,
  844. const struct v4l2_tuner *t)
  845. {
  846. struct cx8800_dev *dev = video_drvdata(file);
  847. struct cx88_core *core = dev->core;
  848. if (UNSET == core->board.tuner_type)
  849. return -EINVAL;
  850. if (0 != t->index)
  851. return -EINVAL;
  852. cx88_set_stereo(core, t->audmode, 1);
  853. return 0;
  854. }
  855. static int vidioc_g_frequency (struct file *file, void *priv,
  856. struct v4l2_frequency *f)
  857. {
  858. struct cx8800_dev *dev = video_drvdata(file);
  859. struct cx88_core *core = dev->core;
  860. if (unlikely(UNSET == core->board.tuner_type))
  861. return -EINVAL;
  862. if (f->tuner)
  863. return -EINVAL;
  864. f->frequency = core->freq;
  865. call_all(core, tuner, g_frequency, f);
  866. return 0;
  867. }
  868. int cx88_set_freq (struct cx88_core *core,
  869. const struct v4l2_frequency *f)
  870. {
  871. struct v4l2_frequency new_freq = *f;
  872. if (unlikely(UNSET == core->board.tuner_type))
  873. return -EINVAL;
  874. if (unlikely(f->tuner != 0))
  875. return -EINVAL;
  876. cx88_newstation(core);
  877. call_all(core, tuner, s_frequency, f);
  878. call_all(core, tuner, g_frequency, &new_freq);
  879. core->freq = new_freq.frequency;
  880. /* When changing channels it is required to reset TVAUDIO */
  881. msleep (10);
  882. cx88_set_tvaudio(core);
  883. return 0;
  884. }
  885. EXPORT_SYMBOL(cx88_set_freq);
  886. static int vidioc_s_frequency (struct file *file, void *priv,
  887. const struct v4l2_frequency *f)
  888. {
  889. struct cx8800_dev *dev = video_drvdata(file);
  890. struct cx88_core *core = dev->core;
  891. return cx88_set_freq(core, f);
  892. }
  893. #ifdef CONFIG_VIDEO_ADV_DEBUG
  894. static int vidioc_g_register (struct file *file, void *fh,
  895. struct v4l2_dbg_register *reg)
  896. {
  897. struct cx8800_dev *dev = video_drvdata(file);
  898. struct cx88_core *core = dev->core;
  899. /* cx2388x has a 24-bit register space */
  900. reg->val = cx_read(reg->reg & 0xfffffc);
  901. reg->size = 4;
  902. return 0;
  903. }
  904. static int vidioc_s_register (struct file *file, void *fh,
  905. const struct v4l2_dbg_register *reg)
  906. {
  907. struct cx8800_dev *dev = video_drvdata(file);
  908. struct cx88_core *core = dev->core;
  909. cx_write(reg->reg & 0xfffffc, reg->val);
  910. return 0;
  911. }
  912. #endif
  913. /* ----------------------------------------------------------- */
  914. /* RADIO ESPECIFIC IOCTLS */
  915. /* ----------------------------------------------------------- */
  916. static int radio_g_tuner (struct file *file, void *priv,
  917. struct v4l2_tuner *t)
  918. {
  919. struct cx8800_dev *dev = video_drvdata(file);
  920. struct cx88_core *core = dev->core;
  921. if (unlikely(t->index > 0))
  922. return -EINVAL;
  923. strcpy(t->name, "Radio");
  924. call_all(core, tuner, g_tuner, t);
  925. return 0;
  926. }
  927. static int radio_s_tuner (struct file *file, void *priv,
  928. const struct v4l2_tuner *t)
  929. {
  930. struct cx8800_dev *dev = video_drvdata(file);
  931. struct cx88_core *core = dev->core;
  932. if (0 != t->index)
  933. return -EINVAL;
  934. call_all(core, tuner, s_tuner, t);
  935. return 0;
  936. }
  937. /* ----------------------------------------------------------- */
  938. static const char *cx88_vid_irqs[32] = {
  939. "y_risci1", "u_risci1", "v_risci1", "vbi_risc1",
  940. "y_risci2", "u_risci2", "v_risci2", "vbi_risc2",
  941. "y_oflow", "u_oflow", "v_oflow", "vbi_oflow",
  942. "y_sync", "u_sync", "v_sync", "vbi_sync",
  943. "opc_err", "par_err", "rip_err", "pci_abort",
  944. };
  945. static void cx8800_vid_irq(struct cx8800_dev *dev)
  946. {
  947. struct cx88_core *core = dev->core;
  948. u32 status, mask, count;
  949. status = cx_read(MO_VID_INTSTAT);
  950. mask = cx_read(MO_VID_INTMSK);
  951. if (0 == (status & mask))
  952. return;
  953. cx_write(MO_VID_INTSTAT, status);
  954. if (irq_debug || (status & mask & ~0xff))
  955. cx88_print_irqbits(core->name, "irq vid",
  956. cx88_vid_irqs, ARRAY_SIZE(cx88_vid_irqs),
  957. status, mask);
  958. /* risc op code error */
  959. if (status & (1 << 16)) {
  960. printk(KERN_WARNING "%s/0: video risc op code error\n",core->name);
  961. cx_clear(MO_VID_DMACNTRL, 0x11);
  962. cx_clear(VID_CAPTURE_CONTROL, 0x06);
  963. cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
  964. }
  965. /* risc1 y */
  966. if (status & 0x01) {
  967. spin_lock(&dev->slock);
  968. count = cx_read(MO_VIDY_GPCNT);
  969. cx88_wakeup(core, &dev->vidq, count);
  970. spin_unlock(&dev->slock);
  971. }
  972. /* risc1 vbi */
  973. if (status & 0x08) {
  974. spin_lock(&dev->slock);
  975. count = cx_read(MO_VBI_GPCNT);
  976. cx88_wakeup(core, &dev->vbiq, count);
  977. spin_unlock(&dev->slock);
  978. }
  979. }
  980. static irqreturn_t cx8800_irq(int irq, void *dev_id)
  981. {
  982. struct cx8800_dev *dev = dev_id;
  983. struct cx88_core *core = dev->core;
  984. u32 status;
  985. int loop, handled = 0;
  986. for (loop = 0; loop < 10; loop++) {
  987. status = cx_read(MO_PCI_INTSTAT) &
  988. (core->pci_irqmask | PCI_INT_VIDINT);
  989. if (0 == status)
  990. goto out;
  991. cx_write(MO_PCI_INTSTAT, status);
  992. handled = 1;
  993. if (status & core->pci_irqmask)
  994. cx88_core_irq(core,status);
  995. if (status & PCI_INT_VIDINT)
  996. cx8800_vid_irq(dev);
  997. }
  998. if (10 == loop) {
  999. printk(KERN_WARNING "%s/0: irq loop -- clearing mask\n",
  1000. core->name);
  1001. cx_write(MO_PCI_INTMSK,0);
  1002. }
  1003. out:
  1004. return IRQ_RETVAL(handled);
  1005. }
  1006. /* ----------------------------------------------------------- */
  1007. /* exported stuff */
  1008. static const struct v4l2_file_operations video_fops =
  1009. {
  1010. .owner = THIS_MODULE,
  1011. .open = v4l2_fh_open,
  1012. .release = vb2_fop_release,
  1013. .read = vb2_fop_read,
  1014. .poll = vb2_fop_poll,
  1015. .mmap = vb2_fop_mmap,
  1016. .unlocked_ioctl = video_ioctl2,
  1017. };
  1018. static const struct v4l2_ioctl_ops video_ioctl_ops = {
  1019. .vidioc_querycap = vidioc_querycap,
  1020. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1021. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  1022. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  1023. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1024. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  1025. .vidioc_querybuf = vb2_ioctl_querybuf,
  1026. .vidioc_qbuf = vb2_ioctl_qbuf,
  1027. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  1028. .vidioc_g_std = vidioc_g_std,
  1029. .vidioc_s_std = vidioc_s_std,
  1030. .vidioc_enum_input = vidioc_enum_input,
  1031. .vidioc_g_input = vidioc_g_input,
  1032. .vidioc_s_input = vidioc_s_input,
  1033. .vidioc_streamon = vb2_ioctl_streamon,
  1034. .vidioc_streamoff = vb2_ioctl_streamoff,
  1035. .vidioc_g_tuner = vidioc_g_tuner,
  1036. .vidioc_s_tuner = vidioc_s_tuner,
  1037. .vidioc_g_frequency = vidioc_g_frequency,
  1038. .vidioc_s_frequency = vidioc_s_frequency,
  1039. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1040. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1041. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1042. .vidioc_g_register = vidioc_g_register,
  1043. .vidioc_s_register = vidioc_s_register,
  1044. #endif
  1045. };
  1046. static const struct video_device cx8800_video_template = {
  1047. .name = "cx8800-video",
  1048. .fops = &video_fops,
  1049. .ioctl_ops = &video_ioctl_ops,
  1050. .tvnorms = CX88_NORMS,
  1051. };
  1052. static const struct v4l2_ioctl_ops vbi_ioctl_ops = {
  1053. .vidioc_querycap = vidioc_querycap,
  1054. .vidioc_g_fmt_vbi_cap = cx8800_vbi_fmt,
  1055. .vidioc_try_fmt_vbi_cap = cx8800_vbi_fmt,
  1056. .vidioc_s_fmt_vbi_cap = cx8800_vbi_fmt,
  1057. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  1058. .vidioc_querybuf = vb2_ioctl_querybuf,
  1059. .vidioc_qbuf = vb2_ioctl_qbuf,
  1060. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  1061. .vidioc_g_std = vidioc_g_std,
  1062. .vidioc_s_std = vidioc_s_std,
  1063. .vidioc_enum_input = vidioc_enum_input,
  1064. .vidioc_g_input = vidioc_g_input,
  1065. .vidioc_s_input = vidioc_s_input,
  1066. .vidioc_streamon = vb2_ioctl_streamon,
  1067. .vidioc_streamoff = vb2_ioctl_streamoff,
  1068. .vidioc_g_tuner = vidioc_g_tuner,
  1069. .vidioc_s_tuner = vidioc_s_tuner,
  1070. .vidioc_g_frequency = vidioc_g_frequency,
  1071. .vidioc_s_frequency = vidioc_s_frequency,
  1072. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1073. .vidioc_g_register = vidioc_g_register,
  1074. .vidioc_s_register = vidioc_s_register,
  1075. #endif
  1076. };
  1077. static const struct video_device cx8800_vbi_template = {
  1078. .name = "cx8800-vbi",
  1079. .fops = &video_fops,
  1080. .ioctl_ops = &vbi_ioctl_ops,
  1081. .tvnorms = CX88_NORMS,
  1082. };
  1083. static const struct v4l2_file_operations radio_fops =
  1084. {
  1085. .owner = THIS_MODULE,
  1086. .open = radio_open,
  1087. .poll = v4l2_ctrl_poll,
  1088. .release = v4l2_fh_release,
  1089. .unlocked_ioctl = video_ioctl2,
  1090. };
  1091. static const struct v4l2_ioctl_ops radio_ioctl_ops = {
  1092. .vidioc_querycap = vidioc_querycap,
  1093. .vidioc_g_tuner = radio_g_tuner,
  1094. .vidioc_s_tuner = radio_s_tuner,
  1095. .vidioc_g_frequency = vidioc_g_frequency,
  1096. .vidioc_s_frequency = vidioc_s_frequency,
  1097. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1098. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1099. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1100. .vidioc_g_register = vidioc_g_register,
  1101. .vidioc_s_register = vidioc_s_register,
  1102. #endif
  1103. };
  1104. static const struct video_device cx8800_radio_template = {
  1105. .name = "cx8800-radio",
  1106. .fops = &radio_fops,
  1107. .ioctl_ops = &radio_ioctl_ops,
  1108. };
  1109. static const struct v4l2_ctrl_ops cx8800_ctrl_vid_ops = {
  1110. .s_ctrl = cx8800_s_vid_ctrl,
  1111. };
  1112. static const struct v4l2_ctrl_ops cx8800_ctrl_aud_ops = {
  1113. .s_ctrl = cx8800_s_aud_ctrl,
  1114. };
  1115. /* ----------------------------------------------------------- */
  1116. static void cx8800_unregister_video(struct cx8800_dev *dev)
  1117. {
  1118. video_unregister_device(&dev->radio_dev);
  1119. video_unregister_device(&dev->vbi_dev);
  1120. video_unregister_device(&dev->video_dev);
  1121. }
  1122. static int cx8800_initdev(struct pci_dev *pci_dev,
  1123. const struct pci_device_id *pci_id)
  1124. {
  1125. struct cx8800_dev *dev;
  1126. struct cx88_core *core;
  1127. struct vb2_queue *q;
  1128. int err;
  1129. int i;
  1130. dev = kzalloc(sizeof(*dev),GFP_KERNEL);
  1131. if (NULL == dev)
  1132. return -ENOMEM;
  1133. /* pci init */
  1134. dev->pci = pci_dev;
  1135. if (pci_enable_device(pci_dev)) {
  1136. err = -EIO;
  1137. goto fail_free;
  1138. }
  1139. core = cx88_core_get(dev->pci);
  1140. if (NULL == core) {
  1141. err = -EINVAL;
  1142. goto fail_free;
  1143. }
  1144. dev->core = core;
  1145. /* print pci info */
  1146. dev->pci_rev = pci_dev->revision;
  1147. pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &dev->pci_lat);
  1148. printk(KERN_INFO "%s/0: found at %s, rev: %d, irq: %d, "
  1149. "latency: %d, mmio: 0x%llx\n", core->name,
  1150. pci_name(pci_dev), dev->pci_rev, pci_dev->irq,
  1151. dev->pci_lat,(unsigned long long)pci_resource_start(pci_dev,0));
  1152. pci_set_master(pci_dev);
  1153. err = pci_set_dma_mask(pci_dev,DMA_BIT_MASK(32));
  1154. if (err) {
  1155. printk("%s/0: Oops: no 32bit PCI DMA ???\n",core->name);
  1156. goto fail_core;
  1157. }
  1158. /* initialize driver struct */
  1159. spin_lock_init(&dev->slock);
  1160. /* init video dma queues */
  1161. INIT_LIST_HEAD(&dev->vidq.active);
  1162. /* init vbi dma queues */
  1163. INIT_LIST_HEAD(&dev->vbiq.active);
  1164. /* get irq */
  1165. err = request_irq(pci_dev->irq, cx8800_irq,
  1166. IRQF_SHARED, core->name, dev);
  1167. if (err < 0) {
  1168. printk(KERN_ERR "%s/0: can't get IRQ %d\n",
  1169. core->name,pci_dev->irq);
  1170. goto fail_core;
  1171. }
  1172. cx_set(MO_PCI_INTMSK, core->pci_irqmask);
  1173. for (i = 0; i < CX8800_AUD_CTLS; i++) {
  1174. const struct cx88_ctrl *cc = &cx8800_aud_ctls[i];
  1175. struct v4l2_ctrl *vc;
  1176. vc = v4l2_ctrl_new_std(&core->audio_hdl, &cx8800_ctrl_aud_ops,
  1177. cc->id, cc->minimum, cc->maximum, cc->step, cc->default_value);
  1178. if (vc == NULL) {
  1179. err = core->audio_hdl.error;
  1180. goto fail_core;
  1181. }
  1182. vc->priv = (void *)cc;
  1183. }
  1184. for (i = 0; i < CX8800_VID_CTLS; i++) {
  1185. const struct cx88_ctrl *cc = &cx8800_vid_ctls[i];
  1186. struct v4l2_ctrl *vc;
  1187. vc = v4l2_ctrl_new_std(&core->video_hdl, &cx8800_ctrl_vid_ops,
  1188. cc->id, cc->minimum, cc->maximum, cc->step, cc->default_value);
  1189. if (vc == NULL) {
  1190. err = core->video_hdl.error;
  1191. goto fail_core;
  1192. }
  1193. vc->priv = (void *)cc;
  1194. if (vc->id == V4L2_CID_CHROMA_AGC)
  1195. core->chroma_agc = vc;
  1196. }
  1197. v4l2_ctrl_add_handler(&core->video_hdl, &core->audio_hdl, NULL);
  1198. /* load and configure helper modules */
  1199. if (core->board.audio_chip == CX88_AUDIO_WM8775) {
  1200. struct i2c_board_info wm8775_info = {
  1201. .type = "wm8775",
  1202. .addr = 0x36 >> 1,
  1203. .platform_data = &core->wm8775_data,
  1204. };
  1205. struct v4l2_subdev *sd;
  1206. if (core->boardnr == CX88_BOARD_HAUPPAUGE_NOVASPLUS_S1)
  1207. core->wm8775_data.is_nova_s = true;
  1208. else
  1209. core->wm8775_data.is_nova_s = false;
  1210. sd = v4l2_i2c_new_subdev_board(&core->v4l2_dev, &core->i2c_adap,
  1211. &wm8775_info, NULL);
  1212. if (sd != NULL) {
  1213. core->sd_wm8775 = sd;
  1214. sd->grp_id = WM8775_GID;
  1215. }
  1216. }
  1217. if (core->board.audio_chip == CX88_AUDIO_TVAUDIO) {
  1218. /* This probes for a tda9874 as is used on some
  1219. Pixelview Ultra boards. */
  1220. v4l2_i2c_new_subdev(&core->v4l2_dev, &core->i2c_adap,
  1221. "tvaudio", 0, I2C_ADDRS(0xb0 >> 1));
  1222. }
  1223. switch (core->boardnr) {
  1224. case CX88_BOARD_DVICO_FUSIONHDTV_5_GOLD:
  1225. case CX88_BOARD_DVICO_FUSIONHDTV_7_GOLD: {
  1226. static const struct i2c_board_info rtc_info = {
  1227. I2C_BOARD_INFO("isl1208", 0x6f)
  1228. };
  1229. request_module("rtc-isl1208");
  1230. core->i2c_rtc = i2c_new_device(&core->i2c_adap, &rtc_info);
  1231. }
  1232. /* break intentionally omitted */
  1233. case CX88_BOARD_DVICO_FUSIONHDTV_5_PCI_NANO:
  1234. request_module("ir-kbd-i2c");
  1235. }
  1236. /* Sets device info at pci_dev */
  1237. pci_set_drvdata(pci_dev, dev);
  1238. dev->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
  1239. /* Maintain a reference so cx88-blackbird can query the 8800 device. */
  1240. core->v4ldev = dev;
  1241. /* initial device configuration */
  1242. mutex_lock(&core->lock);
  1243. cx88_set_tvnorm(core, V4L2_STD_NTSC_M);
  1244. v4l2_ctrl_handler_setup(&core->video_hdl);
  1245. v4l2_ctrl_handler_setup(&core->audio_hdl);
  1246. cx88_video_mux(core, 0);
  1247. q = &dev->vb2_vidq;
  1248. q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1249. q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ;
  1250. q->gfp_flags = GFP_DMA32;
  1251. q->min_buffers_needed = 2;
  1252. q->drv_priv = dev;
  1253. q->buf_struct_size = sizeof(struct cx88_buffer);
  1254. q->ops = &cx8800_video_qops;
  1255. q->mem_ops = &vb2_dma_sg_memops;
  1256. q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  1257. q->lock = &core->lock;
  1258. q->dev = &dev->pci->dev;
  1259. err = vb2_queue_init(q);
  1260. if (err < 0)
  1261. goto fail_unreg;
  1262. q = &dev->vb2_vbiq;
  1263. q->type = V4L2_BUF_TYPE_VBI_CAPTURE;
  1264. q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ;
  1265. q->gfp_flags = GFP_DMA32;
  1266. q->min_buffers_needed = 2;
  1267. q->drv_priv = dev;
  1268. q->buf_struct_size = sizeof(struct cx88_buffer);
  1269. q->ops = &cx8800_vbi_qops;
  1270. q->mem_ops = &vb2_dma_sg_memops;
  1271. q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  1272. q->lock = &core->lock;
  1273. q->dev = &dev->pci->dev;
  1274. err = vb2_queue_init(q);
  1275. if (err < 0)
  1276. goto fail_unreg;
  1277. /* register v4l devices */
  1278. cx88_vdev_init(core, dev->pci, &dev->video_dev,
  1279. &cx8800_video_template, "video");
  1280. video_set_drvdata(&dev->video_dev, dev);
  1281. dev->video_dev.ctrl_handler = &core->video_hdl;
  1282. dev->video_dev.queue = &dev->vb2_vidq;
  1283. err = video_register_device(&dev->video_dev, VFL_TYPE_GRABBER,
  1284. video_nr[core->nr]);
  1285. if (err < 0) {
  1286. printk(KERN_ERR "%s/0: can't register video device\n",
  1287. core->name);
  1288. goto fail_unreg;
  1289. }
  1290. printk(KERN_INFO "%s/0: registered device %s [v4l2]\n",
  1291. core->name, video_device_node_name(&dev->video_dev));
  1292. cx88_vdev_init(core, dev->pci, &dev->vbi_dev,
  1293. &cx8800_vbi_template, "vbi");
  1294. video_set_drvdata(&dev->vbi_dev, dev);
  1295. dev->vbi_dev.queue = &dev->vb2_vbiq;
  1296. err = video_register_device(&dev->vbi_dev, VFL_TYPE_VBI,
  1297. vbi_nr[core->nr]);
  1298. if (err < 0) {
  1299. printk(KERN_ERR "%s/0: can't register vbi device\n",
  1300. core->name);
  1301. goto fail_unreg;
  1302. }
  1303. printk(KERN_INFO "%s/0: registered device %s\n",
  1304. core->name, video_device_node_name(&dev->vbi_dev));
  1305. if (core->board.radio.type == CX88_RADIO) {
  1306. cx88_vdev_init(core, dev->pci, &dev->radio_dev,
  1307. &cx8800_radio_template, "radio");
  1308. video_set_drvdata(&dev->radio_dev, dev);
  1309. dev->radio_dev.ctrl_handler = &core->audio_hdl;
  1310. err = video_register_device(&dev->radio_dev, VFL_TYPE_RADIO,
  1311. radio_nr[core->nr]);
  1312. if (err < 0) {
  1313. printk(KERN_ERR "%s/0: can't register radio device\n",
  1314. core->name);
  1315. goto fail_unreg;
  1316. }
  1317. printk(KERN_INFO "%s/0: registered device %s\n",
  1318. core->name, video_device_node_name(&dev->radio_dev));
  1319. }
  1320. /* start tvaudio thread */
  1321. if (core->board.tuner_type != UNSET) {
  1322. core->kthread = kthread_run(cx88_audio_thread, core, "cx88 tvaudio");
  1323. if (IS_ERR(core->kthread)) {
  1324. err = PTR_ERR(core->kthread);
  1325. printk(KERN_ERR "%s/0: failed to create cx88 audio thread, err=%d\n",
  1326. core->name, err);
  1327. }
  1328. }
  1329. mutex_unlock(&core->lock);
  1330. return 0;
  1331. fail_unreg:
  1332. cx8800_unregister_video(dev);
  1333. free_irq(pci_dev->irq, dev);
  1334. mutex_unlock(&core->lock);
  1335. fail_core:
  1336. core->v4ldev = NULL;
  1337. cx88_core_put(core,dev->pci);
  1338. fail_free:
  1339. kfree(dev);
  1340. return err;
  1341. }
  1342. static void cx8800_finidev(struct pci_dev *pci_dev)
  1343. {
  1344. struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
  1345. struct cx88_core *core = dev->core;
  1346. /* stop thread */
  1347. if (core->kthread) {
  1348. kthread_stop(core->kthread);
  1349. core->kthread = NULL;
  1350. }
  1351. if (core->ir)
  1352. cx88_ir_stop(core);
  1353. cx88_shutdown(core); /* FIXME */
  1354. /* unregister stuff */
  1355. free_irq(pci_dev->irq, dev);
  1356. cx8800_unregister_video(dev);
  1357. pci_disable_device(pci_dev);
  1358. core->v4ldev = NULL;
  1359. /* free memory */
  1360. cx88_core_put(core,dev->pci);
  1361. kfree(dev);
  1362. }
  1363. #ifdef CONFIG_PM
  1364. static int cx8800_suspend(struct pci_dev *pci_dev, pm_message_t state)
  1365. {
  1366. struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
  1367. struct cx88_core *core = dev->core;
  1368. unsigned long flags;
  1369. /* stop video+vbi capture */
  1370. spin_lock_irqsave(&dev->slock, flags);
  1371. if (!list_empty(&dev->vidq.active)) {
  1372. printk("%s/0: suspend video\n", core->name);
  1373. stop_video_dma(dev);
  1374. }
  1375. if (!list_empty(&dev->vbiq.active)) {
  1376. printk("%s/0: suspend vbi\n", core->name);
  1377. cx8800_stop_vbi_dma(dev);
  1378. }
  1379. spin_unlock_irqrestore(&dev->slock, flags);
  1380. if (core->ir)
  1381. cx88_ir_stop(core);
  1382. /* FIXME -- shutdown device */
  1383. cx88_shutdown(core);
  1384. pci_save_state(pci_dev);
  1385. if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
  1386. pci_disable_device(pci_dev);
  1387. dev->state.disabled = 1;
  1388. }
  1389. return 0;
  1390. }
  1391. static int cx8800_resume(struct pci_dev *pci_dev)
  1392. {
  1393. struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
  1394. struct cx88_core *core = dev->core;
  1395. unsigned long flags;
  1396. int err;
  1397. if (dev->state.disabled) {
  1398. err=pci_enable_device(pci_dev);
  1399. if (err) {
  1400. printk(KERN_ERR "%s/0: can't enable device\n",
  1401. core->name);
  1402. return err;
  1403. }
  1404. dev->state.disabled = 0;
  1405. }
  1406. err= pci_set_power_state(pci_dev, PCI_D0);
  1407. if (err) {
  1408. printk(KERN_ERR "%s/0: can't set power state\n", core->name);
  1409. pci_disable_device(pci_dev);
  1410. dev->state.disabled = 1;
  1411. return err;
  1412. }
  1413. pci_restore_state(pci_dev);
  1414. /* FIXME: re-initialize hardware */
  1415. cx88_reset(core);
  1416. if (core->ir)
  1417. cx88_ir_start(core);
  1418. cx_set(MO_PCI_INTMSK, core->pci_irqmask);
  1419. /* restart video+vbi capture */
  1420. spin_lock_irqsave(&dev->slock, flags);
  1421. if (!list_empty(&dev->vidq.active)) {
  1422. printk("%s/0: resume video\n", core->name);
  1423. restart_video_queue(dev,&dev->vidq);
  1424. }
  1425. if (!list_empty(&dev->vbiq.active)) {
  1426. printk("%s/0: resume vbi\n", core->name);
  1427. cx8800_restart_vbi_queue(dev,&dev->vbiq);
  1428. }
  1429. spin_unlock_irqrestore(&dev->slock, flags);
  1430. return 0;
  1431. }
  1432. #endif
  1433. /* ----------------------------------------------------------- */
  1434. static const struct pci_device_id cx8800_pci_tbl[] = {
  1435. {
  1436. .vendor = 0x14f1,
  1437. .device = 0x8800,
  1438. .subvendor = PCI_ANY_ID,
  1439. .subdevice = PCI_ANY_ID,
  1440. },{
  1441. /* --- end of list --- */
  1442. }
  1443. };
  1444. MODULE_DEVICE_TABLE(pci, cx8800_pci_tbl);
  1445. static struct pci_driver cx8800_pci_driver = {
  1446. .name = "cx8800",
  1447. .id_table = cx8800_pci_tbl,
  1448. .probe = cx8800_initdev,
  1449. .remove = cx8800_finidev,
  1450. #ifdef CONFIG_PM
  1451. .suspend = cx8800_suspend,
  1452. .resume = cx8800_resume,
  1453. #endif
  1454. };
  1455. module_pci_driver(cx8800_pci_driver);