cx23885-video.c 34 KB

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