cx23885-video.c 38 KB

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