cx23885-video.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330
  1. /*
  2. * Driver for the Conexant CX23885 PCIe bridge
  3. *
  4. * Copyright (c) 2007 Steven Toth <stoth@linuxtv.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. *
  15. * GNU General Public License for more details.
  16. */
  17. #include "cx23885.h"
  18. #include "cx23885-video.h"
  19. #include <linux/init.h>
  20. #include <linux/list.h>
  21. #include <linux/module.h>
  22. #include <linux/moduleparam.h>
  23. #include <linux/kmod.h>
  24. #include <linux/kernel.h>
  25. #include <linux/slab.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/delay.h>
  28. #include <linux/kthread.h>
  29. #include <asm/div64.h>
  30. #include <media/v4l2-common.h>
  31. #include <media/v4l2-ioctl.h>
  32. #include <media/v4l2-event.h>
  33. #include "cx23885-ioctl.h"
  34. #include "tuner-xc2028.h"
  35. #include <media/drv-intf/cx25840.h>
  36. MODULE_DESCRIPTION("v4l2 driver module for cx23885 based TV cards");
  37. MODULE_AUTHOR("Steven Toth <stoth@linuxtv.org>");
  38. MODULE_LICENSE("GPL");
  39. /* ------------------------------------------------------------------ */
  40. static unsigned int video_nr[] = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET };
  41. static unsigned int vbi_nr[] = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET };
  42. module_param_array(video_nr, int, NULL, 0444);
  43. module_param_array(vbi_nr, int, NULL, 0444);
  44. MODULE_PARM_DESC(video_nr, "video device numbers");
  45. MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
  46. static unsigned int video_debug;
  47. module_param(video_debug, int, 0644);
  48. MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
  49. static unsigned int irq_debug;
  50. module_param(irq_debug, int, 0644);
  51. MODULE_PARM_DESC(irq_debug, "enable debug messages [IRQ handler]");
  52. static unsigned int vid_limit = 16;
  53. module_param(vid_limit, int, 0644);
  54. MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");
  55. #define dprintk(level, fmt, arg...)\
  56. do { if (video_debug >= level)\
  57. printk(KERN_DEBUG pr_fmt("%s: video:" fmt), \
  58. __func__, ##arg); \
  59. } while (0)
  60. /* ------------------------------------------------------------------- */
  61. /* static data */
  62. #define FORMAT_FLAGS_PACKED 0x01
  63. static struct cx23885_fmt formats[] = {
  64. {
  65. .name = "4:2:2, packed, YUYV",
  66. .fourcc = V4L2_PIX_FMT_YUYV,
  67. .depth = 16,
  68. .flags = FORMAT_FLAGS_PACKED,
  69. }
  70. };
  71. static struct cx23885_fmt *format_by_fourcc(unsigned int fourcc)
  72. {
  73. unsigned int i;
  74. for (i = 0; i < ARRAY_SIZE(formats); i++)
  75. if (formats[i].fourcc == fourcc)
  76. return formats+i;
  77. return NULL;
  78. }
  79. /* ------------------------------------------------------------------- */
  80. void cx23885_video_wakeup(struct cx23885_dev *dev,
  81. struct cx23885_dmaqueue *q, u32 count)
  82. {
  83. struct cx23885_buffer *buf;
  84. if (list_empty(&q->active))
  85. return;
  86. buf = list_entry(q->active.next,
  87. struct cx23885_buffer, queue);
  88. buf->vb.sequence = q->count++;
  89. buf->vb.vb2_buf.timestamp = ktime_get_ns();
  90. dprintk(2, "[%p/%d] wakeup reg=%d buf=%d\n", buf,
  91. buf->vb.vb2_buf.index, count, q->count);
  92. list_del(&buf->queue);
  93. vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
  94. }
  95. int cx23885_set_tvnorm(struct cx23885_dev *dev, v4l2_std_id norm)
  96. {
  97. struct v4l2_subdev_format format = {
  98. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  99. .format.code = MEDIA_BUS_FMT_FIXED,
  100. };
  101. dprintk(1, "%s(norm = 0x%08x) name: [%s]\n",
  102. __func__,
  103. (unsigned int)norm,
  104. v4l2_norm_to_name(norm));
  105. if (dev->tvnorm == norm)
  106. return 0;
  107. if (dev->tvnorm != norm) {
  108. if (vb2_is_busy(&dev->vb2_vidq) || vb2_is_busy(&dev->vb2_vbiq) ||
  109. vb2_is_busy(&dev->vb2_mpegq))
  110. return -EBUSY;
  111. }
  112. dev->tvnorm = norm;
  113. dev->width = 720;
  114. dev->height = norm_maxh(norm);
  115. dev->field = V4L2_FIELD_INTERLACED;
  116. call_all(dev, video, s_std, norm);
  117. format.format.width = dev->width;
  118. format.format.height = dev->height;
  119. format.format.field = dev->field;
  120. call_all(dev, pad, set_fmt, NULL, &format);
  121. return 0;
  122. }
  123. static struct video_device *cx23885_vdev_init(struct cx23885_dev *dev,
  124. struct pci_dev *pci,
  125. struct video_device *template,
  126. char *type)
  127. {
  128. struct video_device *vfd;
  129. dprintk(1, "%s()\n", __func__);
  130. vfd = video_device_alloc();
  131. if (NULL == vfd)
  132. return NULL;
  133. *vfd = *template;
  134. vfd->v4l2_dev = &dev->v4l2_dev;
  135. vfd->release = video_device_release;
  136. vfd->lock = &dev->lock;
  137. snprintf(vfd->name, sizeof(vfd->name), "%s (%s)",
  138. cx23885_boards[dev->board].name, type);
  139. video_set_drvdata(vfd, dev);
  140. return vfd;
  141. }
  142. int cx23885_flatiron_write(struct cx23885_dev *dev, u8 reg, u8 data)
  143. {
  144. /* 8 bit registers, 8 bit values */
  145. u8 buf[] = { reg, data };
  146. struct i2c_msg msg = { .addr = 0x98 >> 1,
  147. .flags = 0, .buf = buf, .len = 2 };
  148. return i2c_transfer(&dev->i2c_bus[2].i2c_adap, &msg, 1);
  149. }
  150. u8 cx23885_flatiron_read(struct cx23885_dev *dev, u8 reg)
  151. {
  152. /* 8 bit registers, 8 bit values */
  153. int ret;
  154. u8 b0[] = { reg };
  155. u8 b1[] = { 0 };
  156. struct i2c_msg msg[] = {
  157. { .addr = 0x98 >> 1, .flags = 0, .buf = b0, .len = 1 },
  158. { .addr = 0x98 >> 1, .flags = I2C_M_RD, .buf = b1, .len = 1 }
  159. };
  160. ret = i2c_transfer(&dev->i2c_bus[2].i2c_adap, &msg[0], 2);
  161. if (ret != 2)
  162. pr_err("%s() error\n", __func__);
  163. return b1[0];
  164. }
  165. static void cx23885_flatiron_dump(struct cx23885_dev *dev)
  166. {
  167. int i;
  168. dprintk(1, "Flatiron dump\n");
  169. for (i = 0; i < 0x24; i++) {
  170. dprintk(1, "FI[%02x] = %02x\n", i,
  171. cx23885_flatiron_read(dev, i));
  172. }
  173. }
  174. static int cx23885_flatiron_mux(struct cx23885_dev *dev, int input)
  175. {
  176. u8 val;
  177. dprintk(1, "%s(input = %d)\n", __func__, input);
  178. if (input == 1)
  179. val = cx23885_flatiron_read(dev, CH_PWR_CTRL1) & ~FLD_CH_SEL;
  180. else if (input == 2)
  181. val = cx23885_flatiron_read(dev, CH_PWR_CTRL1) | FLD_CH_SEL;
  182. else
  183. return -EINVAL;
  184. val |= 0x20; /* Enable clock to delta-sigma and dec filter */
  185. cx23885_flatiron_write(dev, CH_PWR_CTRL1, val);
  186. /* Wake up */
  187. cx23885_flatiron_write(dev, CH_PWR_CTRL2, 0);
  188. if (video_debug)
  189. cx23885_flatiron_dump(dev);
  190. return 0;
  191. }
  192. static int cx23885_video_mux(struct cx23885_dev *dev, unsigned int input)
  193. {
  194. dprintk(1, "%s() video_mux: %d [vmux=%d, gpio=0x%x,0x%x,0x%x,0x%x]\n",
  195. __func__,
  196. input, INPUT(input)->vmux,
  197. INPUT(input)->gpio0, INPUT(input)->gpio1,
  198. INPUT(input)->gpio2, INPUT(input)->gpio3);
  199. dev->input = input;
  200. if (dev->board == CX23885_BOARD_MYGICA_X8506 ||
  201. dev->board == CX23885_BOARD_MAGICPRO_PROHDTVE2 ||
  202. dev->board == CX23885_BOARD_MYGICA_X8507) {
  203. /* Select Analog TV */
  204. if (INPUT(input)->type == CX23885_VMUX_TELEVISION)
  205. cx23885_gpio_clear(dev, GPIO_0);
  206. }
  207. /* Tell the internal A/V decoder */
  208. v4l2_subdev_call(dev->sd_cx25840, video, s_routing,
  209. INPUT(input)->vmux, 0, 0);
  210. if ((dev->board == CX23885_BOARD_HAUPPAUGE_HVR1800) ||
  211. (dev->board == CX23885_BOARD_MPX885) ||
  212. (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1250) ||
  213. (dev->board == CX23885_BOARD_HAUPPAUGE_IMPACTVCBE) ||
  214. (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1255) ||
  215. (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1255_22111) ||
  216. (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1850) ||
  217. (dev->board == CX23885_BOARD_MYGICA_X8507) ||
  218. (dev->board == CX23885_BOARD_AVERMEDIA_HC81R) ||
  219. (dev->board == CX23885_BOARD_VIEWCAST_260E) ||
  220. (dev->board == CX23885_BOARD_VIEWCAST_460E)) {
  221. /* Configure audio routing */
  222. v4l2_subdev_call(dev->sd_cx25840, audio, s_routing,
  223. INPUT(input)->amux, 0, 0);
  224. if (INPUT(input)->amux == CX25840_AUDIO7)
  225. cx23885_flatiron_mux(dev, 1);
  226. else if (INPUT(input)->amux == CX25840_AUDIO6)
  227. cx23885_flatiron_mux(dev, 2);
  228. }
  229. return 0;
  230. }
  231. static int cx23885_audio_mux(struct cx23885_dev *dev, unsigned int input)
  232. {
  233. dprintk(1, "%s(input=%d)\n", __func__, input);
  234. /* The baseband video core of the cx23885 has two audio inputs.
  235. * LR1 and LR2. In almost every single case so far only HVR1xxx
  236. * cards we've only ever supported LR1. Time to support LR2,
  237. * which is available via the optional white breakout header on
  238. * the board.
  239. * We'll use a could of existing enums in the card struct to allow
  240. * devs to specify which baseband input they need, or just default
  241. * to what we've always used.
  242. */
  243. if (INPUT(input)->amux == CX25840_AUDIO7)
  244. cx23885_flatiron_mux(dev, 1);
  245. else if (INPUT(input)->amux == CX25840_AUDIO6)
  246. cx23885_flatiron_mux(dev, 2);
  247. else {
  248. /* Not specifically defined, assume the default. */
  249. cx23885_flatiron_mux(dev, 1);
  250. }
  251. return 0;
  252. }
  253. /* ------------------------------------------------------------------ */
  254. static int cx23885_start_video_dma(struct cx23885_dev *dev,
  255. struct cx23885_dmaqueue *q,
  256. struct cx23885_buffer *buf)
  257. {
  258. dprintk(1, "%s()\n", __func__);
  259. /* Stop the dma/fifo before we tamper with it's risc programs */
  260. cx_clear(VID_A_DMA_CTL, 0x11);
  261. /* setup fifo + format */
  262. cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH01],
  263. buf->bpl, buf->risc.dma);
  264. /* reset counter */
  265. cx_write(VID_A_GPCNT_CTL, 3);
  266. q->count = 0;
  267. /* enable irq */
  268. cx23885_irq_add_enable(dev, 0x01);
  269. cx_set(VID_A_INT_MSK, 0x000011);
  270. /* start dma */
  271. cx_set(DEV_CNTRL2, (1<<5));
  272. cx_set(VID_A_DMA_CTL, 0x11); /* FIFO and RISC enable */
  273. return 0;
  274. }
  275. static int queue_setup(struct vb2_queue *q,
  276. unsigned int *num_buffers, unsigned int *num_planes,
  277. unsigned int sizes[], struct device *alloc_devs[])
  278. {
  279. struct cx23885_dev *dev = q->drv_priv;
  280. *num_planes = 1;
  281. sizes[0] = (dev->fmt->depth * dev->width * dev->height) >> 3;
  282. return 0;
  283. }
  284. static int buffer_prepare(struct vb2_buffer *vb)
  285. {
  286. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  287. struct cx23885_dev *dev = vb->vb2_queue->drv_priv;
  288. struct cx23885_buffer *buf =
  289. container_of(vbuf, struct cx23885_buffer, vb);
  290. u32 line0_offset, line1_offset;
  291. struct sg_table *sgt = vb2_dma_sg_plane_desc(vb, 0);
  292. int field_tff;
  293. buf->bpl = (dev->width * dev->fmt->depth) >> 3;
  294. if (vb2_plane_size(vb, 0) < dev->height * buf->bpl)
  295. return -EINVAL;
  296. vb2_set_plane_payload(vb, 0, dev->height * buf->bpl);
  297. switch (dev->field) {
  298. case V4L2_FIELD_TOP:
  299. cx23885_risc_buffer(dev->pci, &buf->risc,
  300. sgt->sgl, 0, UNSET,
  301. buf->bpl, 0, dev->height);
  302. break;
  303. case V4L2_FIELD_BOTTOM:
  304. cx23885_risc_buffer(dev->pci, &buf->risc,
  305. sgt->sgl, UNSET, 0,
  306. buf->bpl, 0, dev->height);
  307. break;
  308. case V4L2_FIELD_INTERLACED:
  309. if (dev->tvnorm & V4L2_STD_525_60)
  310. /* NTSC or */
  311. field_tff = 1;
  312. else
  313. field_tff = 0;
  314. if (cx23885_boards[dev->board].force_bff)
  315. /* PAL / SECAM OR 888 in NTSC MODE */
  316. field_tff = 0;
  317. if (field_tff) {
  318. /* cx25840 transmits NTSC bottom field first */
  319. dprintk(1, "%s() Creating TFF/NTSC risc\n",
  320. __func__);
  321. line0_offset = buf->bpl;
  322. line1_offset = 0;
  323. } else {
  324. /* All other formats are top field first */
  325. dprintk(1, "%s() Creating BFF/PAL/SECAM risc\n",
  326. __func__);
  327. line0_offset = 0;
  328. line1_offset = buf->bpl;
  329. }
  330. cx23885_risc_buffer(dev->pci, &buf->risc,
  331. sgt->sgl, line0_offset,
  332. line1_offset,
  333. buf->bpl, buf->bpl,
  334. dev->height >> 1);
  335. break;
  336. case V4L2_FIELD_SEQ_TB:
  337. cx23885_risc_buffer(dev->pci, &buf->risc,
  338. sgt->sgl,
  339. 0, buf->bpl * (dev->height >> 1),
  340. buf->bpl, 0,
  341. dev->height >> 1);
  342. break;
  343. case V4L2_FIELD_SEQ_BT:
  344. cx23885_risc_buffer(dev->pci, &buf->risc,
  345. sgt->sgl,
  346. buf->bpl * (dev->height >> 1), 0,
  347. buf->bpl, 0,
  348. dev->height >> 1);
  349. break;
  350. default:
  351. BUG();
  352. }
  353. dprintk(2, "[%p/%d] buffer_init - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
  354. buf, buf->vb.vb2_buf.index,
  355. dev->width, dev->height, dev->fmt->depth, dev->fmt->name,
  356. (unsigned long)buf->risc.dma);
  357. return 0;
  358. }
  359. static void buffer_finish(struct vb2_buffer *vb)
  360. {
  361. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  362. struct cx23885_buffer *buf = container_of(vbuf,
  363. struct cx23885_buffer, vb);
  364. cx23885_free_buffer(vb->vb2_queue->drv_priv, buf);
  365. }
  366. /*
  367. * The risc program for each buffer works as follows: it starts with a simple
  368. * 'JUMP to addr + 12', which is effectively a NOP. Then the code to DMA the
  369. * buffer follows and at the end we have a JUMP back to the start + 12 (skipping
  370. * the initial JUMP).
  371. *
  372. * This is the risc program of the first buffer to be queued if the active list
  373. * is empty and it just keeps DMAing this buffer without generating any
  374. * interrupts.
  375. *
  376. * If a new buffer is added then the initial JUMP in the code for that buffer
  377. * will generate an interrupt which signals that the previous buffer has been
  378. * DMAed successfully and that it can be returned to userspace.
  379. *
  380. * It also sets the final jump of the previous buffer to the start of the new
  381. * buffer, thus chaining the new buffer into the DMA chain. This is a single
  382. * atomic u32 write, so there is no race condition.
  383. *
  384. * The end-result of all this that you only get an interrupt when a buffer
  385. * is ready, so the control flow is very easy.
  386. */
  387. static void buffer_queue(struct vb2_buffer *vb)
  388. {
  389. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  390. struct cx23885_dev *dev = vb->vb2_queue->drv_priv;
  391. struct cx23885_buffer *buf = container_of(vbuf,
  392. struct cx23885_buffer, vb);
  393. struct cx23885_buffer *prev;
  394. struct cx23885_dmaqueue *q = &dev->vidq;
  395. unsigned long flags;
  396. /* add jump to start */
  397. buf->risc.cpu[1] = cpu_to_le32(buf->risc.dma + 12);
  398. buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_CNT_INC);
  399. buf->risc.jmp[1] = cpu_to_le32(buf->risc.dma + 12);
  400. buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
  401. spin_lock_irqsave(&dev->slock, flags);
  402. if (list_empty(&q->active)) {
  403. list_add_tail(&buf->queue, &q->active);
  404. dprintk(2, "[%p/%d] buffer_queue - first active\n",
  405. buf, buf->vb.vb2_buf.index);
  406. } else {
  407. buf->risc.cpu[0] |= cpu_to_le32(RISC_IRQ1);
  408. prev = list_entry(q->active.prev, struct cx23885_buffer,
  409. queue);
  410. list_add_tail(&buf->queue, &q->active);
  411. prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
  412. dprintk(2, "[%p/%d] buffer_queue - append to active\n",
  413. buf, buf->vb.vb2_buf.index);
  414. }
  415. spin_unlock_irqrestore(&dev->slock, flags);
  416. }
  417. static int cx23885_start_streaming(struct vb2_queue *q, unsigned int count)
  418. {
  419. struct cx23885_dev *dev = q->drv_priv;
  420. struct cx23885_dmaqueue *dmaq = &dev->vidq;
  421. struct cx23885_buffer *buf = list_entry(dmaq->active.next,
  422. struct cx23885_buffer, queue);
  423. cx23885_start_video_dma(dev, dmaq, buf);
  424. return 0;
  425. }
  426. static void cx23885_stop_streaming(struct vb2_queue *q)
  427. {
  428. struct cx23885_dev *dev = q->drv_priv;
  429. struct cx23885_dmaqueue *dmaq = &dev->vidq;
  430. unsigned long flags;
  431. cx_clear(VID_A_DMA_CTL, 0x11);
  432. spin_lock_irqsave(&dev->slock, flags);
  433. while (!list_empty(&dmaq->active)) {
  434. struct cx23885_buffer *buf = list_entry(dmaq->active.next,
  435. struct cx23885_buffer, queue);
  436. list_del(&buf->queue);
  437. vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
  438. }
  439. spin_unlock_irqrestore(&dev->slock, flags);
  440. }
  441. static const struct vb2_ops cx23885_video_qops = {
  442. .queue_setup = queue_setup,
  443. .buf_prepare = buffer_prepare,
  444. .buf_finish = buffer_finish,
  445. .buf_queue = buffer_queue,
  446. .wait_prepare = vb2_ops_wait_prepare,
  447. .wait_finish = vb2_ops_wait_finish,
  448. .start_streaming = cx23885_start_streaming,
  449. .stop_streaming = cx23885_stop_streaming,
  450. };
  451. /* ------------------------------------------------------------------ */
  452. /* VIDEO IOCTLS */
  453. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  454. struct v4l2_format *f)
  455. {
  456. struct cx23885_dev *dev = video_drvdata(file);
  457. f->fmt.pix.width = dev->width;
  458. f->fmt.pix.height = dev->height;
  459. f->fmt.pix.field = dev->field;
  460. f->fmt.pix.pixelformat = dev->fmt->fourcc;
  461. f->fmt.pix.bytesperline =
  462. (f->fmt.pix.width * dev->fmt->depth) >> 3;
  463. f->fmt.pix.sizeimage =
  464. f->fmt.pix.height * f->fmt.pix.bytesperline;
  465. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  466. return 0;
  467. }
  468. static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  469. struct v4l2_format *f)
  470. {
  471. struct cx23885_dev *dev = video_drvdata(file);
  472. struct cx23885_fmt *fmt;
  473. enum v4l2_field field;
  474. unsigned int maxw, maxh;
  475. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  476. if (NULL == fmt)
  477. return -EINVAL;
  478. field = f->fmt.pix.field;
  479. maxw = 720;
  480. maxh = norm_maxh(dev->tvnorm);
  481. if (V4L2_FIELD_ANY == field) {
  482. field = (f->fmt.pix.height > maxh/2)
  483. ? V4L2_FIELD_INTERLACED
  484. : V4L2_FIELD_BOTTOM;
  485. }
  486. switch (field) {
  487. case V4L2_FIELD_TOP:
  488. case V4L2_FIELD_BOTTOM:
  489. maxh = maxh / 2;
  490. break;
  491. case V4L2_FIELD_INTERLACED:
  492. case V4L2_FIELD_SEQ_TB:
  493. case V4L2_FIELD_SEQ_BT:
  494. break;
  495. default:
  496. field = V4L2_FIELD_INTERLACED;
  497. break;
  498. }
  499. f->fmt.pix.field = field;
  500. v4l_bound_align_image(&f->fmt.pix.width, 48, maxw, 2,
  501. &f->fmt.pix.height, 32, maxh, 0, 0);
  502. f->fmt.pix.bytesperline =
  503. (f->fmt.pix.width * fmt->depth) >> 3;
  504. f->fmt.pix.sizeimage =
  505. f->fmt.pix.height * f->fmt.pix.bytesperline;
  506. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  507. return 0;
  508. }
  509. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  510. struct v4l2_format *f)
  511. {
  512. struct cx23885_dev *dev = video_drvdata(file);
  513. struct v4l2_subdev_format format = {
  514. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  515. };
  516. int err;
  517. dprintk(2, "%s()\n", __func__);
  518. err = vidioc_try_fmt_vid_cap(file, priv, f);
  519. if (0 != err)
  520. return err;
  521. if (vb2_is_busy(&dev->vb2_vidq) || vb2_is_busy(&dev->vb2_vbiq) ||
  522. vb2_is_busy(&dev->vb2_mpegq))
  523. return -EBUSY;
  524. dev->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  525. dev->width = f->fmt.pix.width;
  526. dev->height = f->fmt.pix.height;
  527. dev->field = f->fmt.pix.field;
  528. dprintk(2, "%s() width=%d height=%d field=%d\n", __func__,
  529. dev->width, dev->height, dev->field);
  530. v4l2_fill_mbus_format(&format.format, &f->fmt.pix, MEDIA_BUS_FMT_FIXED);
  531. call_all(dev, pad, set_fmt, NULL, &format);
  532. v4l2_fill_pix_format(&f->fmt.pix, &format.format);
  533. /* set_fmt overwrites f->fmt.pix.field, restore it */
  534. f->fmt.pix.field = dev->field;
  535. return 0;
  536. }
  537. static int vidioc_querycap(struct file *file, void *priv,
  538. struct v4l2_capability *cap)
  539. {
  540. struct cx23885_dev *dev = video_drvdata(file);
  541. struct video_device *vdev = video_devdata(file);
  542. strcpy(cap->driver, "cx23885");
  543. strlcpy(cap->card, cx23885_boards[dev->board].name,
  544. sizeof(cap->card));
  545. sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
  546. cap->device_caps = V4L2_CAP_READWRITE | V4L2_CAP_STREAMING | V4L2_CAP_AUDIO;
  547. if (dev->tuner_type != TUNER_ABSENT)
  548. cap->device_caps |= V4L2_CAP_TUNER;
  549. if (vdev->vfl_type == VFL_TYPE_VBI)
  550. cap->device_caps |= V4L2_CAP_VBI_CAPTURE;
  551. else
  552. cap->device_caps |= V4L2_CAP_VIDEO_CAPTURE;
  553. cap->capabilities = cap->device_caps | V4L2_CAP_VBI_CAPTURE |
  554. V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_DEVICE_CAPS;
  555. return 0;
  556. }
  557. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  558. struct v4l2_fmtdesc *f)
  559. {
  560. if (unlikely(f->index >= ARRAY_SIZE(formats)))
  561. return -EINVAL;
  562. strlcpy(f->description, formats[f->index].name,
  563. sizeof(f->description));
  564. f->pixelformat = formats[f->index].fourcc;
  565. return 0;
  566. }
  567. static int vidioc_cropcap(struct file *file, void *priv,
  568. struct v4l2_cropcap *cc)
  569. {
  570. struct cx23885_dev *dev = video_drvdata(file);
  571. bool is_50hz = dev->tvnorm & V4L2_STD_625_50;
  572. if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  573. return -EINVAL;
  574. cc->bounds.left = 0;
  575. cc->bounds.top = 0;
  576. cc->bounds.width = 720;
  577. cc->bounds.height = norm_maxh(dev->tvnorm);
  578. cc->defrect = cc->bounds;
  579. cc->pixelaspect.numerator = is_50hz ? 54 : 11;
  580. cc->pixelaspect.denominator = is_50hz ? 59 : 10;
  581. return 0;
  582. }
  583. static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
  584. {
  585. struct cx23885_dev *dev = video_drvdata(file);
  586. dprintk(1, "%s()\n", __func__);
  587. *id = dev->tvnorm;
  588. return 0;
  589. }
  590. static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id tvnorms)
  591. {
  592. struct cx23885_dev *dev = video_drvdata(file);
  593. dprintk(1, "%s()\n", __func__);
  594. return cx23885_set_tvnorm(dev, tvnorms);
  595. }
  596. int cx23885_enum_input(struct cx23885_dev *dev, struct v4l2_input *i)
  597. {
  598. static const char *iname[] = {
  599. [CX23885_VMUX_COMPOSITE1] = "Composite1",
  600. [CX23885_VMUX_COMPOSITE2] = "Composite2",
  601. [CX23885_VMUX_COMPOSITE3] = "Composite3",
  602. [CX23885_VMUX_COMPOSITE4] = "Composite4",
  603. [CX23885_VMUX_SVIDEO] = "S-Video",
  604. [CX23885_VMUX_COMPONENT] = "Component",
  605. [CX23885_VMUX_TELEVISION] = "Television",
  606. [CX23885_VMUX_CABLE] = "Cable TV",
  607. [CX23885_VMUX_DVB] = "DVB",
  608. [CX23885_VMUX_DEBUG] = "for debug only",
  609. };
  610. unsigned int n;
  611. dprintk(1, "%s()\n", __func__);
  612. n = i->index;
  613. if (n >= MAX_CX23885_INPUT)
  614. return -EINVAL;
  615. if (0 == INPUT(n)->type)
  616. return -EINVAL;
  617. i->index = n;
  618. i->type = V4L2_INPUT_TYPE_CAMERA;
  619. strcpy(i->name, iname[INPUT(n)->type]);
  620. i->std = CX23885_NORMS;
  621. if ((CX23885_VMUX_TELEVISION == INPUT(n)->type) ||
  622. (CX23885_VMUX_CABLE == INPUT(n)->type)) {
  623. i->type = V4L2_INPUT_TYPE_TUNER;
  624. i->audioset = 4;
  625. } else {
  626. /* Two selectable audio inputs for non-tv inputs */
  627. i->audioset = 3;
  628. }
  629. if (dev->input == n) {
  630. /* enum'd input matches our configured input.
  631. * Ask the video decoder to process the call
  632. * and give it an oppertunity to update the
  633. * status field.
  634. */
  635. call_all(dev, video, g_input_status, &i->status);
  636. }
  637. return 0;
  638. }
  639. static int vidioc_enum_input(struct file *file, void *priv,
  640. struct v4l2_input *i)
  641. {
  642. struct cx23885_dev *dev = video_drvdata(file);
  643. dprintk(1, "%s()\n", __func__);
  644. return cx23885_enum_input(dev, i);
  645. }
  646. int cx23885_get_input(struct file *file, void *priv, unsigned int *i)
  647. {
  648. struct cx23885_dev *dev = video_drvdata(file);
  649. *i = dev->input;
  650. dprintk(1, "%s() returns %d\n", __func__, *i);
  651. return 0;
  652. }
  653. static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  654. {
  655. return cx23885_get_input(file, priv, i);
  656. }
  657. int cx23885_set_input(struct file *file, void *priv, unsigned int i)
  658. {
  659. struct cx23885_dev *dev = video_drvdata(file);
  660. dprintk(1, "%s(%d)\n", __func__, i);
  661. if (i >= MAX_CX23885_INPUT) {
  662. dprintk(1, "%s() -EINVAL\n", __func__);
  663. return -EINVAL;
  664. }
  665. if (INPUT(i)->type == 0)
  666. return -EINVAL;
  667. cx23885_video_mux(dev, i);
  668. /* By default establish the default audio input for the card also */
  669. /* Caller is free to use VIDIOC_S_AUDIO to override afterwards */
  670. cx23885_audio_mux(dev, i);
  671. return 0;
  672. }
  673. static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  674. {
  675. return cx23885_set_input(file, priv, i);
  676. }
  677. static int vidioc_log_status(struct file *file, void *priv)
  678. {
  679. struct cx23885_dev *dev = video_drvdata(file);
  680. call_all(dev, core, log_status);
  681. return 0;
  682. }
  683. static int cx23885_query_audinput(struct file *file, void *priv,
  684. struct v4l2_audio *i)
  685. {
  686. static const char *iname[] = {
  687. [0] = "Baseband L/R 1",
  688. [1] = "Baseband L/R 2",
  689. [2] = "TV",
  690. };
  691. unsigned int n;
  692. dprintk(1, "%s()\n", __func__);
  693. n = i->index;
  694. if (n >= 3)
  695. return -EINVAL;
  696. memset(i, 0, sizeof(*i));
  697. i->index = n;
  698. strcpy(i->name, iname[n]);
  699. i->capability = V4L2_AUDCAP_STEREO;
  700. return 0;
  701. }
  702. static int vidioc_enum_audinput(struct file *file, void *priv,
  703. struct v4l2_audio *i)
  704. {
  705. return cx23885_query_audinput(file, priv, i);
  706. }
  707. static int vidioc_g_audinput(struct file *file, void *priv,
  708. struct v4l2_audio *i)
  709. {
  710. struct cx23885_dev *dev = video_drvdata(file);
  711. if ((CX23885_VMUX_TELEVISION == INPUT(dev->input)->type) ||
  712. (CX23885_VMUX_CABLE == INPUT(dev->input)->type))
  713. i->index = 2;
  714. else
  715. i->index = dev->audinput;
  716. dprintk(1, "%s(input=%d)\n", __func__, i->index);
  717. return cx23885_query_audinput(file, priv, i);
  718. }
  719. static int vidioc_s_audinput(struct file *file, void *priv,
  720. const struct v4l2_audio *i)
  721. {
  722. struct cx23885_dev *dev = video_drvdata(file);
  723. if ((CX23885_VMUX_TELEVISION == INPUT(dev->input)->type) ||
  724. (CX23885_VMUX_CABLE == INPUT(dev->input)->type)) {
  725. return i->index != 2 ? -EINVAL : 0;
  726. }
  727. if (i->index > 1)
  728. return -EINVAL;
  729. dprintk(1, "%s(%d)\n", __func__, i->index);
  730. dev->audinput = i->index;
  731. /* Skip the audio defaults from the cards struct, caller wants
  732. * directly touch the audio mux hardware. */
  733. cx23885_flatiron_mux(dev, dev->audinput + 1);
  734. return 0;
  735. }
  736. static int vidioc_g_tuner(struct file *file, void *priv,
  737. struct v4l2_tuner *t)
  738. {
  739. struct cx23885_dev *dev = video_drvdata(file);
  740. if (dev->tuner_type == TUNER_ABSENT)
  741. return -EINVAL;
  742. if (0 != t->index)
  743. return -EINVAL;
  744. strcpy(t->name, "Television");
  745. call_all(dev, tuner, g_tuner, t);
  746. return 0;
  747. }
  748. static int vidioc_s_tuner(struct file *file, void *priv,
  749. const struct v4l2_tuner *t)
  750. {
  751. struct cx23885_dev *dev = video_drvdata(file);
  752. if (dev->tuner_type == TUNER_ABSENT)
  753. return -EINVAL;
  754. if (0 != t->index)
  755. return -EINVAL;
  756. /* Update the A/V core */
  757. call_all(dev, tuner, s_tuner, t);
  758. return 0;
  759. }
  760. static int vidioc_g_frequency(struct file *file, void *priv,
  761. struct v4l2_frequency *f)
  762. {
  763. struct cx23885_dev *dev = video_drvdata(file);
  764. if (dev->tuner_type == TUNER_ABSENT)
  765. return -EINVAL;
  766. f->type = V4L2_TUNER_ANALOG_TV;
  767. f->frequency = dev->freq;
  768. call_all(dev, tuner, g_frequency, f);
  769. return 0;
  770. }
  771. static int cx23885_set_freq(struct cx23885_dev *dev, const struct v4l2_frequency *f)
  772. {
  773. struct v4l2_ctrl *mute;
  774. int old_mute_val = 1;
  775. if (dev->tuner_type == TUNER_ABSENT)
  776. return -EINVAL;
  777. if (unlikely(f->tuner != 0))
  778. return -EINVAL;
  779. dev->freq = f->frequency;
  780. /* I need to mute audio here */
  781. mute = v4l2_ctrl_find(&dev->ctrl_handler, V4L2_CID_AUDIO_MUTE);
  782. if (mute) {
  783. old_mute_val = v4l2_ctrl_g_ctrl(mute);
  784. if (!old_mute_val)
  785. v4l2_ctrl_s_ctrl(mute, 1);
  786. }
  787. call_all(dev, tuner, s_frequency, f);
  788. /* When changing channels it is required to reset TVAUDIO */
  789. msleep(100);
  790. /* I need to unmute audio here */
  791. if (old_mute_val == 0)
  792. v4l2_ctrl_s_ctrl(mute, old_mute_val);
  793. return 0;
  794. }
  795. static int cx23885_set_freq_via_ops(struct cx23885_dev *dev,
  796. const struct v4l2_frequency *f)
  797. {
  798. struct v4l2_ctrl *mute;
  799. int old_mute_val = 1;
  800. struct vb2_dvb_frontend *vfe;
  801. struct dvb_frontend *fe;
  802. struct analog_parameters params = {
  803. .mode = V4L2_TUNER_ANALOG_TV,
  804. .audmode = V4L2_TUNER_MODE_STEREO,
  805. .std = dev->tvnorm,
  806. .frequency = f->frequency
  807. };
  808. dev->freq = f->frequency;
  809. /* I need to mute audio here */
  810. mute = v4l2_ctrl_find(&dev->ctrl_handler, V4L2_CID_AUDIO_MUTE);
  811. if (mute) {
  812. old_mute_val = v4l2_ctrl_g_ctrl(mute);
  813. if (!old_mute_val)
  814. v4l2_ctrl_s_ctrl(mute, 1);
  815. }
  816. /* If HVR1850 */
  817. dprintk(1, "%s() frequency=%d tuner=%d std=0x%llx\n", __func__,
  818. params.frequency, f->tuner, params.std);
  819. vfe = vb2_dvb_get_frontend(&dev->ts2.frontends, 1);
  820. if (!vfe) {
  821. return -EINVAL;
  822. }
  823. fe = vfe->dvb.frontend;
  824. if ((dev->board == CX23885_BOARD_HAUPPAUGE_HVR1850) ||
  825. (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1255) ||
  826. (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1255_22111))
  827. fe = &dev->ts1.analog_fe;
  828. if (fe && fe->ops.tuner_ops.set_analog_params) {
  829. call_all(dev, video, s_std, dev->tvnorm);
  830. fe->ops.tuner_ops.set_analog_params(fe, &params);
  831. }
  832. else
  833. pr_err("%s() No analog tuner, aborting\n", __func__);
  834. /* When changing channels it is required to reset TVAUDIO */
  835. msleep(100);
  836. /* I need to unmute audio here */
  837. if (old_mute_val == 0)
  838. v4l2_ctrl_s_ctrl(mute, old_mute_val);
  839. return 0;
  840. }
  841. int cx23885_set_frequency(struct file *file, void *priv,
  842. const struct v4l2_frequency *f)
  843. {
  844. struct cx23885_dev *dev = video_drvdata(file);
  845. int ret;
  846. switch (dev->board) {
  847. case CX23885_BOARD_HAUPPAUGE_HVR1255:
  848. case CX23885_BOARD_HAUPPAUGE_HVR1255_22111:
  849. case CX23885_BOARD_HAUPPAUGE_HVR1850:
  850. ret = cx23885_set_freq_via_ops(dev, f);
  851. break;
  852. default:
  853. ret = cx23885_set_freq(dev, f);
  854. }
  855. return ret;
  856. }
  857. static int vidioc_s_frequency(struct file *file, void *priv,
  858. const struct v4l2_frequency *f)
  859. {
  860. return cx23885_set_frequency(file, priv, f);
  861. }
  862. /* ----------------------------------------------------------- */
  863. int cx23885_video_irq(struct cx23885_dev *dev, u32 status)
  864. {
  865. u32 mask, count;
  866. int handled = 0;
  867. mask = cx_read(VID_A_INT_MSK);
  868. if (0 == (status & mask))
  869. return handled;
  870. cx_write(VID_A_INT_STAT, status);
  871. /* risc op code error, fifo overflow or line sync detection error */
  872. if ((status & VID_BC_MSK_OPC_ERR) ||
  873. (status & VID_BC_MSK_SYNC) ||
  874. (status & VID_BC_MSK_OF)) {
  875. if (status & VID_BC_MSK_OPC_ERR) {
  876. dprintk(7, " (VID_BC_MSK_OPC_ERR 0x%08x)\n",
  877. VID_BC_MSK_OPC_ERR);
  878. pr_warn("%s: video risc op code error\n",
  879. dev->name);
  880. cx23885_sram_channel_dump(dev,
  881. &dev->sram_channels[SRAM_CH01]);
  882. }
  883. if (status & VID_BC_MSK_SYNC)
  884. dprintk(7, " (VID_BC_MSK_SYNC 0x%08x) video lines miss-match\n",
  885. VID_BC_MSK_SYNC);
  886. if (status & VID_BC_MSK_OF)
  887. dprintk(7, " (VID_BC_MSK_OF 0x%08x) fifo overflow\n",
  888. VID_BC_MSK_OF);
  889. }
  890. /* Video */
  891. if (status & VID_BC_MSK_RISCI1) {
  892. spin_lock(&dev->slock);
  893. count = cx_read(VID_A_GPCNT);
  894. cx23885_video_wakeup(dev, &dev->vidq, count);
  895. spin_unlock(&dev->slock);
  896. handled++;
  897. }
  898. /* Allow the VBI framework to process it's payload */
  899. handled += cx23885_vbi_irq(dev, status);
  900. return handled;
  901. }
  902. /* ----------------------------------------------------------- */
  903. /* exported stuff */
  904. static const struct v4l2_file_operations video_fops = {
  905. .owner = THIS_MODULE,
  906. .open = v4l2_fh_open,
  907. .release = vb2_fop_release,
  908. .read = vb2_fop_read,
  909. .poll = vb2_fop_poll,
  910. .unlocked_ioctl = video_ioctl2,
  911. .mmap = vb2_fop_mmap,
  912. };
  913. static const struct v4l2_ioctl_ops video_ioctl_ops = {
  914. .vidioc_querycap = vidioc_querycap,
  915. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  916. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  917. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  918. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  919. .vidioc_g_fmt_vbi_cap = cx23885_vbi_fmt,
  920. .vidioc_try_fmt_vbi_cap = cx23885_vbi_fmt,
  921. .vidioc_s_fmt_vbi_cap = cx23885_vbi_fmt,
  922. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  923. .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
  924. .vidioc_querybuf = vb2_ioctl_querybuf,
  925. .vidioc_qbuf = vb2_ioctl_qbuf,
  926. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  927. .vidioc_streamon = vb2_ioctl_streamon,
  928. .vidioc_streamoff = vb2_ioctl_streamoff,
  929. .vidioc_cropcap = vidioc_cropcap,
  930. .vidioc_s_std = vidioc_s_std,
  931. .vidioc_g_std = vidioc_g_std,
  932. .vidioc_enum_input = vidioc_enum_input,
  933. .vidioc_g_input = vidioc_g_input,
  934. .vidioc_s_input = vidioc_s_input,
  935. .vidioc_log_status = vidioc_log_status,
  936. .vidioc_g_tuner = vidioc_g_tuner,
  937. .vidioc_s_tuner = vidioc_s_tuner,
  938. .vidioc_g_frequency = vidioc_g_frequency,
  939. .vidioc_s_frequency = vidioc_s_frequency,
  940. #ifdef CONFIG_VIDEO_ADV_DEBUG
  941. .vidioc_g_chip_info = cx23885_g_chip_info,
  942. .vidioc_g_register = cx23885_g_register,
  943. .vidioc_s_register = cx23885_s_register,
  944. #endif
  945. .vidioc_enumaudio = vidioc_enum_audinput,
  946. .vidioc_g_audio = vidioc_g_audinput,
  947. .vidioc_s_audio = vidioc_s_audinput,
  948. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  949. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  950. };
  951. static struct video_device cx23885_vbi_template;
  952. static struct video_device cx23885_video_template = {
  953. .name = "cx23885-video",
  954. .fops = &video_fops,
  955. .ioctl_ops = &video_ioctl_ops,
  956. .tvnorms = CX23885_NORMS,
  957. };
  958. void cx23885_video_unregister(struct cx23885_dev *dev)
  959. {
  960. dprintk(1, "%s()\n", __func__);
  961. cx23885_irq_remove(dev, 0x01);
  962. if (dev->vbi_dev) {
  963. if (video_is_registered(dev->vbi_dev))
  964. video_unregister_device(dev->vbi_dev);
  965. else
  966. video_device_release(dev->vbi_dev);
  967. dev->vbi_dev = NULL;
  968. }
  969. if (dev->video_dev) {
  970. if (video_is_registered(dev->video_dev))
  971. video_unregister_device(dev->video_dev);
  972. else
  973. video_device_release(dev->video_dev);
  974. dev->video_dev = NULL;
  975. }
  976. if (dev->audio_dev)
  977. cx23885_audio_unregister(dev);
  978. }
  979. int cx23885_video_register(struct cx23885_dev *dev)
  980. {
  981. struct vb2_queue *q;
  982. int err;
  983. dprintk(1, "%s()\n", __func__);
  984. /* Initialize VBI template */
  985. cx23885_vbi_template = cx23885_video_template;
  986. strcpy(cx23885_vbi_template.name, "cx23885-vbi");
  987. dev->tvnorm = V4L2_STD_NTSC_M;
  988. dev->fmt = format_by_fourcc(V4L2_PIX_FMT_YUYV);
  989. dev->field = V4L2_FIELD_INTERLACED;
  990. dev->width = 720;
  991. dev->height = norm_maxh(dev->tvnorm);
  992. /* init video dma queues */
  993. INIT_LIST_HEAD(&dev->vidq.active);
  994. /* init vbi dma queues */
  995. INIT_LIST_HEAD(&dev->vbiq.active);
  996. cx23885_irq_add_enable(dev, 0x01);
  997. if ((TUNER_ABSENT != dev->tuner_type) &&
  998. ((dev->tuner_bus == 0) || (dev->tuner_bus == 1))) {
  999. struct v4l2_subdev *sd = NULL;
  1000. if (dev->tuner_addr)
  1001. sd = v4l2_i2c_new_subdev(&dev->v4l2_dev,
  1002. &dev->i2c_bus[dev->tuner_bus].i2c_adap,
  1003. "tuner", dev->tuner_addr, NULL);
  1004. else
  1005. sd = v4l2_i2c_new_subdev(&dev->v4l2_dev,
  1006. &dev->i2c_bus[dev->tuner_bus].i2c_adap,
  1007. "tuner", 0, v4l2_i2c_tuner_addrs(ADDRS_TV));
  1008. if (sd) {
  1009. struct tuner_setup tun_setup;
  1010. memset(&tun_setup, 0, sizeof(tun_setup));
  1011. tun_setup.mode_mask = T_ANALOG_TV;
  1012. tun_setup.type = dev->tuner_type;
  1013. tun_setup.addr = v4l2_i2c_subdev_addr(sd);
  1014. tun_setup.tuner_callback = cx23885_tuner_callback;
  1015. v4l2_subdev_call(sd, tuner, s_type_addr, &tun_setup);
  1016. if ((dev->board == CX23885_BOARD_LEADTEK_WINFAST_PXTV1200) ||
  1017. (dev->board == CX23885_BOARD_LEADTEK_WINFAST_PXPVR2200)) {
  1018. struct xc2028_ctrl ctrl = {
  1019. .fname = XC2028_DEFAULT_FIRMWARE,
  1020. .max_len = 64
  1021. };
  1022. struct v4l2_priv_tun_config cfg = {
  1023. .tuner = dev->tuner_type,
  1024. .priv = &ctrl
  1025. };
  1026. v4l2_subdev_call(sd, tuner, s_config, &cfg);
  1027. }
  1028. if (dev->board == CX23885_BOARD_AVERMEDIA_HC81R) {
  1029. struct xc2028_ctrl ctrl = {
  1030. .fname = "xc3028L-v36.fw",
  1031. .max_len = 64
  1032. };
  1033. struct v4l2_priv_tun_config cfg = {
  1034. .tuner = dev->tuner_type,
  1035. .priv = &ctrl
  1036. };
  1037. v4l2_subdev_call(sd, tuner, s_config, &cfg);
  1038. }
  1039. }
  1040. }
  1041. /* initial device configuration */
  1042. mutex_lock(&dev->lock);
  1043. cx23885_set_tvnorm(dev, dev->tvnorm);
  1044. cx23885_video_mux(dev, 0);
  1045. cx23885_audio_mux(dev, 0);
  1046. mutex_unlock(&dev->lock);
  1047. q = &dev->vb2_vidq;
  1048. q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1049. q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ;
  1050. q->gfp_flags = GFP_DMA32;
  1051. q->min_buffers_needed = 2;
  1052. q->drv_priv = dev;
  1053. q->buf_struct_size = sizeof(struct cx23885_buffer);
  1054. q->ops = &cx23885_video_qops;
  1055. q->mem_ops = &vb2_dma_sg_memops;
  1056. q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  1057. q->lock = &dev->lock;
  1058. q->dev = &dev->pci->dev;
  1059. err = vb2_queue_init(q);
  1060. if (err < 0)
  1061. goto fail_unreg;
  1062. q = &dev->vb2_vbiq;
  1063. q->type = V4L2_BUF_TYPE_VBI_CAPTURE;
  1064. q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ;
  1065. q->gfp_flags = GFP_DMA32;
  1066. q->min_buffers_needed = 2;
  1067. q->drv_priv = dev;
  1068. q->buf_struct_size = sizeof(struct cx23885_buffer);
  1069. q->ops = &cx23885_vbi_qops;
  1070. q->mem_ops = &vb2_dma_sg_memops;
  1071. q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  1072. q->lock = &dev->lock;
  1073. q->dev = &dev->pci->dev;
  1074. err = vb2_queue_init(q);
  1075. if (err < 0)
  1076. goto fail_unreg;
  1077. /* register Video device */
  1078. dev->video_dev = cx23885_vdev_init(dev, dev->pci,
  1079. &cx23885_video_template, "video");
  1080. dev->video_dev->queue = &dev->vb2_vidq;
  1081. err = video_register_device(dev->video_dev, VFL_TYPE_GRABBER,
  1082. video_nr[dev->nr]);
  1083. if (err < 0) {
  1084. pr_info("%s: can't register video device\n",
  1085. dev->name);
  1086. goto fail_unreg;
  1087. }
  1088. pr_info("%s: registered device %s [v4l2]\n",
  1089. dev->name, video_device_node_name(dev->video_dev));
  1090. /* register VBI device */
  1091. dev->vbi_dev = cx23885_vdev_init(dev, dev->pci,
  1092. &cx23885_vbi_template, "vbi");
  1093. dev->vbi_dev->queue = &dev->vb2_vbiq;
  1094. err = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
  1095. vbi_nr[dev->nr]);
  1096. if (err < 0) {
  1097. pr_info("%s: can't register vbi device\n",
  1098. dev->name);
  1099. goto fail_unreg;
  1100. }
  1101. pr_info("%s: registered device %s\n",
  1102. dev->name, video_device_node_name(dev->vbi_dev));
  1103. /* Register ALSA audio device */
  1104. dev->audio_dev = cx23885_audio_register(dev);
  1105. return 0;
  1106. fail_unreg:
  1107. cx23885_video_unregister(dev);
  1108. return err;
  1109. }