cx23885-video.c 35 KB

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