cx23885-video.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289
  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.sequence = q->count++;
  88. v4l2_get_timestamp(&buf->vb.timestamp);
  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. 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 void *parg,
  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. alloc_ctxs[0] = dev->alloc_ctx;
  267. return 0;
  268. }
  269. static int buffer_prepare(struct vb2_buffer *vb)
  270. {
  271. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  272. struct cx23885_dev *dev = vb->vb2_queue->drv_priv;
  273. struct cx23885_buffer *buf =
  274. container_of(vbuf, struct cx23885_buffer, vb);
  275. u32 line0_offset, line1_offset;
  276. struct sg_table *sgt = vb2_dma_sg_plane_desc(vb, 0);
  277. int field_tff;
  278. buf->bpl = (dev->width * dev->fmt->depth) >> 3;
  279. if (vb2_plane_size(vb, 0) < dev->height * buf->bpl)
  280. return -EINVAL;
  281. vb2_set_plane_payload(vb, 0, dev->height * buf->bpl);
  282. switch (dev->field) {
  283. case V4L2_FIELD_TOP:
  284. cx23885_risc_buffer(dev->pci, &buf->risc,
  285. sgt->sgl, 0, UNSET,
  286. buf->bpl, 0, dev->height);
  287. break;
  288. case V4L2_FIELD_BOTTOM:
  289. cx23885_risc_buffer(dev->pci, &buf->risc,
  290. sgt->sgl, UNSET, 0,
  291. buf->bpl, 0, dev->height);
  292. break;
  293. case V4L2_FIELD_INTERLACED:
  294. if (dev->tvnorm & V4L2_STD_525_60)
  295. /* NTSC or */
  296. field_tff = 1;
  297. else
  298. field_tff = 0;
  299. if (cx23885_boards[dev->board].force_bff)
  300. /* PAL / SECAM OR 888 in NTSC MODE */
  301. field_tff = 0;
  302. if (field_tff) {
  303. /* cx25840 transmits NTSC bottom field first */
  304. dprintk(1, "%s() Creating TFF/NTSC risc\n",
  305. __func__);
  306. line0_offset = buf->bpl;
  307. line1_offset = 0;
  308. } else {
  309. /* All other formats are top field first */
  310. dprintk(1, "%s() Creating BFF/PAL/SECAM risc\n",
  311. __func__);
  312. line0_offset = 0;
  313. line1_offset = buf->bpl;
  314. }
  315. cx23885_risc_buffer(dev->pci, &buf->risc,
  316. sgt->sgl, line0_offset,
  317. line1_offset,
  318. buf->bpl, buf->bpl,
  319. dev->height >> 1);
  320. break;
  321. case V4L2_FIELD_SEQ_TB:
  322. cx23885_risc_buffer(dev->pci, &buf->risc,
  323. sgt->sgl,
  324. 0, buf->bpl * (dev->height >> 1),
  325. buf->bpl, 0,
  326. dev->height >> 1);
  327. break;
  328. case V4L2_FIELD_SEQ_BT:
  329. cx23885_risc_buffer(dev->pci, &buf->risc,
  330. sgt->sgl,
  331. buf->bpl * (dev->height >> 1), 0,
  332. buf->bpl, 0,
  333. dev->height >> 1);
  334. break;
  335. default:
  336. BUG();
  337. }
  338. dprintk(2, "[%p/%d] buffer_init - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
  339. buf, buf->vb.vb2_buf.index,
  340. dev->width, dev->height, dev->fmt->depth, dev->fmt->name,
  341. (unsigned long)buf->risc.dma);
  342. return 0;
  343. }
  344. static void buffer_finish(struct vb2_buffer *vb)
  345. {
  346. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  347. struct cx23885_buffer *buf = container_of(vbuf,
  348. struct cx23885_buffer, vb);
  349. cx23885_free_buffer(vb->vb2_queue->drv_priv, buf);
  350. }
  351. /*
  352. * The risc program for each buffer works as follows: it starts with a simple
  353. * 'JUMP to addr + 12', which is effectively a NOP. Then the code to DMA the
  354. * buffer follows and at the end we have a JUMP back to the start + 12 (skipping
  355. * the initial JUMP).
  356. *
  357. * This is the risc program of the first buffer to be queued if the active list
  358. * is empty and it just keeps DMAing this buffer without generating any
  359. * interrupts.
  360. *
  361. * If a new buffer is added then the initial JUMP in the code for that buffer
  362. * will generate an interrupt which signals that the previous buffer has been
  363. * DMAed successfully and that it can be returned to userspace.
  364. *
  365. * It also sets the final jump of the previous buffer to the start of the new
  366. * buffer, thus chaining the new buffer into the DMA chain. This is a single
  367. * atomic u32 write, so there is no race condition.
  368. *
  369. * The end-result of all this that you only get an interrupt when a buffer
  370. * is ready, so the control flow is very easy.
  371. */
  372. static void buffer_queue(struct vb2_buffer *vb)
  373. {
  374. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  375. struct cx23885_dev *dev = vb->vb2_queue->drv_priv;
  376. struct cx23885_buffer *buf = container_of(vbuf,
  377. struct cx23885_buffer, vb);
  378. struct cx23885_buffer *prev;
  379. struct cx23885_dmaqueue *q = &dev->vidq;
  380. unsigned long flags;
  381. /* add jump to start */
  382. buf->risc.cpu[1] = cpu_to_le32(buf->risc.dma + 12);
  383. buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_CNT_INC);
  384. buf->risc.jmp[1] = cpu_to_le32(buf->risc.dma + 12);
  385. buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
  386. spin_lock_irqsave(&dev->slock, flags);
  387. if (list_empty(&q->active)) {
  388. list_add_tail(&buf->queue, &q->active);
  389. dprintk(2, "[%p/%d] buffer_queue - first active\n",
  390. buf, buf->vb.vb2_buf.index);
  391. } else {
  392. buf->risc.cpu[0] |= cpu_to_le32(RISC_IRQ1);
  393. prev = list_entry(q->active.prev, struct cx23885_buffer,
  394. queue);
  395. list_add_tail(&buf->queue, &q->active);
  396. prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
  397. dprintk(2, "[%p/%d] buffer_queue - append to active\n",
  398. buf, buf->vb.vb2_buf.index);
  399. }
  400. spin_unlock_irqrestore(&dev->slock, flags);
  401. }
  402. static int cx23885_start_streaming(struct vb2_queue *q, unsigned int count)
  403. {
  404. struct cx23885_dev *dev = q->drv_priv;
  405. struct cx23885_dmaqueue *dmaq = &dev->vidq;
  406. struct cx23885_buffer *buf = list_entry(dmaq->active.next,
  407. struct cx23885_buffer, queue);
  408. cx23885_start_video_dma(dev, dmaq, buf);
  409. return 0;
  410. }
  411. static void cx23885_stop_streaming(struct vb2_queue *q)
  412. {
  413. struct cx23885_dev *dev = q->drv_priv;
  414. struct cx23885_dmaqueue *dmaq = &dev->vidq;
  415. unsigned long flags;
  416. cx_clear(VID_A_DMA_CTL, 0x11);
  417. spin_lock_irqsave(&dev->slock, flags);
  418. while (!list_empty(&dmaq->active)) {
  419. struct cx23885_buffer *buf = list_entry(dmaq->active.next,
  420. struct cx23885_buffer, queue);
  421. list_del(&buf->queue);
  422. vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
  423. }
  424. spin_unlock_irqrestore(&dev->slock, flags);
  425. }
  426. static struct vb2_ops cx23885_video_qops = {
  427. .queue_setup = queue_setup,
  428. .buf_prepare = buffer_prepare,
  429. .buf_finish = buffer_finish,
  430. .buf_queue = buffer_queue,
  431. .wait_prepare = vb2_ops_wait_prepare,
  432. .wait_finish = vb2_ops_wait_finish,
  433. .start_streaming = cx23885_start_streaming,
  434. .stop_streaming = cx23885_stop_streaming,
  435. };
  436. /* ------------------------------------------------------------------ */
  437. /* VIDEO IOCTLS */
  438. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  439. struct v4l2_format *f)
  440. {
  441. struct cx23885_dev *dev = video_drvdata(file);
  442. f->fmt.pix.width = dev->width;
  443. f->fmt.pix.height = dev->height;
  444. f->fmt.pix.field = dev->field;
  445. f->fmt.pix.pixelformat = dev->fmt->fourcc;
  446. f->fmt.pix.bytesperline =
  447. (f->fmt.pix.width * dev->fmt->depth) >> 3;
  448. f->fmt.pix.sizeimage =
  449. f->fmt.pix.height * f->fmt.pix.bytesperline;
  450. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  451. return 0;
  452. }
  453. static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  454. struct v4l2_format *f)
  455. {
  456. struct cx23885_dev *dev = video_drvdata(file);
  457. struct cx23885_fmt *fmt;
  458. enum v4l2_field field;
  459. unsigned int maxw, maxh;
  460. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  461. if (NULL == fmt)
  462. return -EINVAL;
  463. field = f->fmt.pix.field;
  464. maxw = norm_maxw(dev->tvnorm);
  465. maxh = norm_maxh(dev->tvnorm);
  466. if (V4L2_FIELD_ANY == field) {
  467. field = (f->fmt.pix.height > maxh/2)
  468. ? V4L2_FIELD_INTERLACED
  469. : V4L2_FIELD_BOTTOM;
  470. }
  471. switch (field) {
  472. case V4L2_FIELD_TOP:
  473. case V4L2_FIELD_BOTTOM:
  474. maxh = maxh / 2;
  475. break;
  476. case V4L2_FIELD_INTERLACED:
  477. case V4L2_FIELD_SEQ_TB:
  478. case V4L2_FIELD_SEQ_BT:
  479. break;
  480. default:
  481. field = V4L2_FIELD_INTERLACED;
  482. break;
  483. }
  484. f->fmt.pix.field = field;
  485. v4l_bound_align_image(&f->fmt.pix.width, 48, maxw, 2,
  486. &f->fmt.pix.height, 32, maxh, 0, 0);
  487. f->fmt.pix.bytesperline =
  488. (f->fmt.pix.width * fmt->depth) >> 3;
  489. f->fmt.pix.sizeimage =
  490. f->fmt.pix.height * f->fmt.pix.bytesperline;
  491. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  492. return 0;
  493. }
  494. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  495. struct v4l2_format *f)
  496. {
  497. struct cx23885_dev *dev = video_drvdata(file);
  498. struct v4l2_subdev_format format = {
  499. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  500. };
  501. int err;
  502. dprintk(2, "%s()\n", __func__);
  503. err = vidioc_try_fmt_vid_cap(file, priv, f);
  504. if (0 != err)
  505. return err;
  506. if (vb2_is_busy(&dev->vb2_vidq) || vb2_is_busy(&dev->vb2_vbiq) ||
  507. vb2_is_busy(&dev->vb2_mpegq))
  508. return -EBUSY;
  509. dev->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  510. dev->width = f->fmt.pix.width;
  511. dev->height = f->fmt.pix.height;
  512. dev->field = f->fmt.pix.field;
  513. dprintk(2, "%s() width=%d height=%d field=%d\n", __func__,
  514. dev->width, dev->height, dev->field);
  515. v4l2_fill_mbus_format(&format.format, &f->fmt.pix, MEDIA_BUS_FMT_FIXED);
  516. call_all(dev, pad, set_fmt, NULL, &format);
  517. v4l2_fill_pix_format(&f->fmt.pix, &format.format);
  518. /* set_fmt overwrites f->fmt.pix.field, restore it */
  519. f->fmt.pix.field = dev->field;
  520. return 0;
  521. }
  522. static int vidioc_querycap(struct file *file, void *priv,
  523. struct v4l2_capability *cap)
  524. {
  525. struct cx23885_dev *dev = video_drvdata(file);
  526. struct video_device *vdev = video_devdata(file);
  527. strcpy(cap->driver, "cx23885");
  528. strlcpy(cap->card, cx23885_boards[dev->board].name,
  529. sizeof(cap->card));
  530. sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
  531. cap->device_caps = V4L2_CAP_READWRITE | V4L2_CAP_STREAMING | V4L2_CAP_AUDIO;
  532. if (dev->tuner_type != TUNER_ABSENT)
  533. cap->device_caps |= V4L2_CAP_TUNER;
  534. if (vdev->vfl_type == VFL_TYPE_VBI)
  535. cap->device_caps |= V4L2_CAP_VBI_CAPTURE;
  536. else
  537. cap->device_caps |= V4L2_CAP_VIDEO_CAPTURE;
  538. cap->capabilities = cap->device_caps | V4L2_CAP_VBI_CAPTURE |
  539. V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_DEVICE_CAPS;
  540. return 0;
  541. }
  542. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  543. struct v4l2_fmtdesc *f)
  544. {
  545. if (unlikely(f->index >= ARRAY_SIZE(formats)))
  546. return -EINVAL;
  547. strlcpy(f->description, formats[f->index].name,
  548. sizeof(f->description));
  549. f->pixelformat = formats[f->index].fourcc;
  550. return 0;
  551. }
  552. static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
  553. {
  554. struct cx23885_dev *dev = video_drvdata(file);
  555. dprintk(1, "%s()\n", __func__);
  556. *id = dev->tvnorm;
  557. return 0;
  558. }
  559. static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id tvnorms)
  560. {
  561. struct cx23885_dev *dev = video_drvdata(file);
  562. dprintk(1, "%s()\n", __func__);
  563. return cx23885_set_tvnorm(dev, tvnorms);
  564. }
  565. int cx23885_enum_input(struct cx23885_dev *dev, struct v4l2_input *i)
  566. {
  567. static const char *iname[] = {
  568. [CX23885_VMUX_COMPOSITE1] = "Composite1",
  569. [CX23885_VMUX_COMPOSITE2] = "Composite2",
  570. [CX23885_VMUX_COMPOSITE3] = "Composite3",
  571. [CX23885_VMUX_COMPOSITE4] = "Composite4",
  572. [CX23885_VMUX_SVIDEO] = "S-Video",
  573. [CX23885_VMUX_COMPONENT] = "Component",
  574. [CX23885_VMUX_TELEVISION] = "Television",
  575. [CX23885_VMUX_CABLE] = "Cable TV",
  576. [CX23885_VMUX_DVB] = "DVB",
  577. [CX23885_VMUX_DEBUG] = "for debug only",
  578. };
  579. unsigned int n;
  580. dprintk(1, "%s()\n", __func__);
  581. n = i->index;
  582. if (n >= MAX_CX23885_INPUT)
  583. return -EINVAL;
  584. if (0 == INPUT(n)->type)
  585. return -EINVAL;
  586. i->index = n;
  587. i->type = V4L2_INPUT_TYPE_CAMERA;
  588. strcpy(i->name, iname[INPUT(n)->type]);
  589. i->std = CX23885_NORMS;
  590. if ((CX23885_VMUX_TELEVISION == INPUT(n)->type) ||
  591. (CX23885_VMUX_CABLE == INPUT(n)->type)) {
  592. i->type = V4L2_INPUT_TYPE_TUNER;
  593. i->audioset = 4;
  594. } else {
  595. /* Two selectable audio inputs for non-tv inputs */
  596. i->audioset = 3;
  597. }
  598. if (dev->input == n) {
  599. /* enum'd input matches our configured input.
  600. * Ask the video decoder to process the call
  601. * and give it an oppertunity to update the
  602. * status field.
  603. */
  604. call_all(dev, video, g_input_status, &i->status);
  605. }
  606. return 0;
  607. }
  608. static int vidioc_enum_input(struct file *file, void *priv,
  609. struct v4l2_input *i)
  610. {
  611. struct cx23885_dev *dev = video_drvdata(file);
  612. dprintk(1, "%s()\n", __func__);
  613. return cx23885_enum_input(dev, i);
  614. }
  615. int cx23885_get_input(struct file *file, void *priv, unsigned int *i)
  616. {
  617. struct cx23885_dev *dev = video_drvdata(file);
  618. *i = dev->input;
  619. dprintk(1, "%s() returns %d\n", __func__, *i);
  620. return 0;
  621. }
  622. static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  623. {
  624. return cx23885_get_input(file, priv, i);
  625. }
  626. int cx23885_set_input(struct file *file, void *priv, unsigned int i)
  627. {
  628. struct cx23885_dev *dev = video_drvdata(file);
  629. dprintk(1, "%s(%d)\n", __func__, i);
  630. if (i >= MAX_CX23885_INPUT) {
  631. dprintk(1, "%s() -EINVAL\n", __func__);
  632. return -EINVAL;
  633. }
  634. if (INPUT(i)->type == 0)
  635. return -EINVAL;
  636. cx23885_video_mux(dev, i);
  637. /* By default establish the default audio input for the card also */
  638. /* Caller is free to use VIDIOC_S_AUDIO to override afterwards */
  639. cx23885_audio_mux(dev, i);
  640. return 0;
  641. }
  642. static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  643. {
  644. return cx23885_set_input(file, priv, i);
  645. }
  646. static int vidioc_log_status(struct file *file, void *priv)
  647. {
  648. struct cx23885_dev *dev = video_drvdata(file);
  649. call_all(dev, core, log_status);
  650. return 0;
  651. }
  652. static int cx23885_query_audinput(struct file *file, void *priv,
  653. struct v4l2_audio *i)
  654. {
  655. struct cx23885_dev *dev = video_drvdata(file);
  656. static const char *iname[] = {
  657. [0] = "Baseband L/R 1",
  658. [1] = "Baseband L/R 2",
  659. [2] = "TV",
  660. };
  661. unsigned int n;
  662. dprintk(1, "%s()\n", __func__);
  663. n = i->index;
  664. if (n >= 3)
  665. return -EINVAL;
  666. memset(i, 0, sizeof(*i));
  667. i->index = n;
  668. strcpy(i->name, iname[n]);
  669. i->capability = V4L2_AUDCAP_STEREO;
  670. return 0;
  671. }
  672. static int vidioc_enum_audinput(struct file *file, void *priv,
  673. struct v4l2_audio *i)
  674. {
  675. return cx23885_query_audinput(file, priv, i);
  676. }
  677. static int vidioc_g_audinput(struct file *file, void *priv,
  678. struct v4l2_audio *i)
  679. {
  680. struct cx23885_dev *dev = video_drvdata(file);
  681. if ((CX23885_VMUX_TELEVISION == INPUT(dev->input)->type) ||
  682. (CX23885_VMUX_CABLE == INPUT(dev->input)->type))
  683. i->index = 2;
  684. else
  685. i->index = dev->audinput;
  686. dprintk(1, "%s(input=%d)\n", __func__, i->index);
  687. return cx23885_query_audinput(file, priv, i);
  688. }
  689. static int vidioc_s_audinput(struct file *file, void *priv,
  690. const struct v4l2_audio *i)
  691. {
  692. struct cx23885_dev *dev = video_drvdata(file);
  693. if ((CX23885_VMUX_TELEVISION == INPUT(dev->input)->type) ||
  694. (CX23885_VMUX_CABLE == INPUT(dev->input)->type)) {
  695. return i->index != 2 ? -EINVAL : 0;
  696. }
  697. if (i->index > 1)
  698. return -EINVAL;
  699. dprintk(1, "%s(%d)\n", __func__, i->index);
  700. dev->audinput = i->index;
  701. /* Skip the audio defaults from the cards struct, caller wants
  702. * directly touch the audio mux hardware. */
  703. cx23885_flatiron_mux(dev, dev->audinput + 1);
  704. return 0;
  705. }
  706. static int vidioc_g_tuner(struct file *file, void *priv,
  707. struct v4l2_tuner *t)
  708. {
  709. struct cx23885_dev *dev = video_drvdata(file);
  710. if (dev->tuner_type == TUNER_ABSENT)
  711. return -EINVAL;
  712. if (0 != t->index)
  713. return -EINVAL;
  714. strcpy(t->name, "Television");
  715. call_all(dev, tuner, g_tuner, t);
  716. return 0;
  717. }
  718. static int vidioc_s_tuner(struct file *file, void *priv,
  719. const struct v4l2_tuner *t)
  720. {
  721. struct cx23885_dev *dev = video_drvdata(file);
  722. if (dev->tuner_type == TUNER_ABSENT)
  723. return -EINVAL;
  724. if (0 != t->index)
  725. return -EINVAL;
  726. /* Update the A/V core */
  727. call_all(dev, tuner, s_tuner, t);
  728. return 0;
  729. }
  730. static int vidioc_g_frequency(struct file *file, void *priv,
  731. struct v4l2_frequency *f)
  732. {
  733. struct cx23885_dev *dev = video_drvdata(file);
  734. if (dev->tuner_type == TUNER_ABSENT)
  735. return -EINVAL;
  736. f->type = V4L2_TUNER_ANALOG_TV;
  737. f->frequency = dev->freq;
  738. call_all(dev, tuner, g_frequency, f);
  739. return 0;
  740. }
  741. static int cx23885_set_freq(struct cx23885_dev *dev, const struct v4l2_frequency *f)
  742. {
  743. struct v4l2_ctrl *mute;
  744. int old_mute_val = 1;
  745. if (dev->tuner_type == TUNER_ABSENT)
  746. return -EINVAL;
  747. if (unlikely(f->tuner != 0))
  748. return -EINVAL;
  749. dev->freq = f->frequency;
  750. /* I need to mute audio here */
  751. mute = v4l2_ctrl_find(&dev->ctrl_handler, V4L2_CID_AUDIO_MUTE);
  752. if (mute) {
  753. old_mute_val = v4l2_ctrl_g_ctrl(mute);
  754. if (!old_mute_val)
  755. v4l2_ctrl_s_ctrl(mute, 1);
  756. }
  757. call_all(dev, tuner, s_frequency, f);
  758. /* When changing channels it is required to reset TVAUDIO */
  759. msleep(100);
  760. /* I need to unmute audio here */
  761. if (old_mute_val == 0)
  762. v4l2_ctrl_s_ctrl(mute, old_mute_val);
  763. return 0;
  764. }
  765. static int cx23885_set_freq_via_ops(struct cx23885_dev *dev,
  766. const struct v4l2_frequency *f)
  767. {
  768. struct v4l2_ctrl *mute;
  769. int old_mute_val = 1;
  770. struct vb2_dvb_frontend *vfe;
  771. struct dvb_frontend *fe;
  772. struct analog_parameters params = {
  773. .mode = V4L2_TUNER_ANALOG_TV,
  774. .audmode = V4L2_TUNER_MODE_STEREO,
  775. .std = dev->tvnorm,
  776. .frequency = f->frequency
  777. };
  778. dev->freq = f->frequency;
  779. /* I need to mute audio here */
  780. mute = v4l2_ctrl_find(&dev->ctrl_handler, V4L2_CID_AUDIO_MUTE);
  781. if (mute) {
  782. old_mute_val = v4l2_ctrl_g_ctrl(mute);
  783. if (!old_mute_val)
  784. v4l2_ctrl_s_ctrl(mute, 1);
  785. }
  786. /* If HVR1850 */
  787. dprintk(1, "%s() frequency=%d tuner=%d std=0x%llx\n", __func__,
  788. params.frequency, f->tuner, params.std);
  789. vfe = vb2_dvb_get_frontend(&dev->ts2.frontends, 1);
  790. if (!vfe) {
  791. return -EINVAL;
  792. }
  793. fe = vfe->dvb.frontend;
  794. if ((dev->board == CX23885_BOARD_HAUPPAUGE_HVR1850) ||
  795. (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1255) ||
  796. (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1255_22111))
  797. fe = &dev->ts1.analog_fe;
  798. if (fe && fe->ops.tuner_ops.set_analog_params) {
  799. call_all(dev, video, s_std, dev->tvnorm);
  800. fe->ops.tuner_ops.set_analog_params(fe, &params);
  801. }
  802. else
  803. printk(KERN_ERR "%s() No analog tuner, aborting\n", __func__);
  804. /* When changing channels it is required to reset TVAUDIO */
  805. msleep(100);
  806. /* I need to unmute audio here */
  807. if (old_mute_val == 0)
  808. v4l2_ctrl_s_ctrl(mute, old_mute_val);
  809. return 0;
  810. }
  811. int cx23885_set_frequency(struct file *file, void *priv,
  812. const struct v4l2_frequency *f)
  813. {
  814. struct cx23885_dev *dev = video_drvdata(file);
  815. int ret;
  816. switch (dev->board) {
  817. case CX23885_BOARD_HAUPPAUGE_HVR1255:
  818. case CX23885_BOARD_HAUPPAUGE_HVR1255_22111:
  819. case CX23885_BOARD_HAUPPAUGE_HVR1850:
  820. ret = cx23885_set_freq_via_ops(dev, f);
  821. break;
  822. default:
  823. ret = cx23885_set_freq(dev, f);
  824. }
  825. return ret;
  826. }
  827. static int vidioc_s_frequency(struct file *file, void *priv,
  828. const struct v4l2_frequency *f)
  829. {
  830. return cx23885_set_frequency(file, priv, f);
  831. }
  832. /* ----------------------------------------------------------- */
  833. int cx23885_video_irq(struct cx23885_dev *dev, u32 status)
  834. {
  835. u32 mask, count;
  836. int handled = 0;
  837. mask = cx_read(VID_A_INT_MSK);
  838. if (0 == (status & mask))
  839. return handled;
  840. cx_write(VID_A_INT_STAT, status);
  841. /* risc op code error, fifo overflow or line sync detection error */
  842. if ((status & VID_BC_MSK_OPC_ERR) ||
  843. (status & VID_BC_MSK_SYNC) ||
  844. (status & VID_BC_MSK_OF)) {
  845. if (status & VID_BC_MSK_OPC_ERR) {
  846. dprintk(7, " (VID_BC_MSK_OPC_ERR 0x%08x)\n",
  847. VID_BC_MSK_OPC_ERR);
  848. printk(KERN_WARNING "%s: video risc op code error\n",
  849. dev->name);
  850. cx23885_sram_channel_dump(dev,
  851. &dev->sram_channels[SRAM_CH01]);
  852. }
  853. if (status & VID_BC_MSK_SYNC)
  854. dprintk(7, " (VID_BC_MSK_SYNC 0x%08x) "
  855. "video lines miss-match\n",
  856. VID_BC_MSK_SYNC);
  857. if (status & VID_BC_MSK_OF)
  858. dprintk(7, " (VID_BC_MSK_OF 0x%08x) fifo overflow\n",
  859. VID_BC_MSK_OF);
  860. }
  861. /* Video */
  862. if (status & VID_BC_MSK_RISCI1) {
  863. spin_lock(&dev->slock);
  864. count = cx_read(VID_A_GPCNT);
  865. cx23885_video_wakeup(dev, &dev->vidq, count);
  866. spin_unlock(&dev->slock);
  867. handled++;
  868. }
  869. /* Allow the VBI framework to process it's payload */
  870. handled += cx23885_vbi_irq(dev, status);
  871. return handled;
  872. }
  873. /* ----------------------------------------------------------- */
  874. /* exported stuff */
  875. static const struct v4l2_file_operations video_fops = {
  876. .owner = THIS_MODULE,
  877. .open = v4l2_fh_open,
  878. .release = vb2_fop_release,
  879. .read = vb2_fop_read,
  880. .poll = vb2_fop_poll,
  881. .unlocked_ioctl = video_ioctl2,
  882. .mmap = vb2_fop_mmap,
  883. };
  884. static const struct v4l2_ioctl_ops video_ioctl_ops = {
  885. .vidioc_querycap = vidioc_querycap,
  886. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  887. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  888. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  889. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  890. .vidioc_g_fmt_vbi_cap = cx23885_vbi_fmt,
  891. .vidioc_try_fmt_vbi_cap = cx23885_vbi_fmt,
  892. .vidioc_s_fmt_vbi_cap = cx23885_vbi_fmt,
  893. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  894. .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
  895. .vidioc_querybuf = vb2_ioctl_querybuf,
  896. .vidioc_qbuf = vb2_ioctl_qbuf,
  897. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  898. .vidioc_streamon = vb2_ioctl_streamon,
  899. .vidioc_streamoff = vb2_ioctl_streamoff,
  900. .vidioc_s_std = vidioc_s_std,
  901. .vidioc_g_std = vidioc_g_std,
  902. .vidioc_enum_input = vidioc_enum_input,
  903. .vidioc_g_input = vidioc_g_input,
  904. .vidioc_s_input = vidioc_s_input,
  905. .vidioc_log_status = vidioc_log_status,
  906. .vidioc_g_tuner = vidioc_g_tuner,
  907. .vidioc_s_tuner = vidioc_s_tuner,
  908. .vidioc_g_frequency = vidioc_g_frequency,
  909. .vidioc_s_frequency = vidioc_s_frequency,
  910. #ifdef CONFIG_VIDEO_ADV_DEBUG
  911. .vidioc_g_chip_info = cx23885_g_chip_info,
  912. .vidioc_g_register = cx23885_g_register,
  913. .vidioc_s_register = cx23885_s_register,
  914. #endif
  915. .vidioc_enumaudio = vidioc_enum_audinput,
  916. .vidioc_g_audio = vidioc_g_audinput,
  917. .vidioc_s_audio = vidioc_s_audinput,
  918. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  919. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  920. };
  921. static struct video_device cx23885_vbi_template;
  922. static struct video_device cx23885_video_template = {
  923. .name = "cx23885-video",
  924. .fops = &video_fops,
  925. .ioctl_ops = &video_ioctl_ops,
  926. .tvnorms = CX23885_NORMS,
  927. };
  928. void cx23885_video_unregister(struct cx23885_dev *dev)
  929. {
  930. dprintk(1, "%s()\n", __func__);
  931. cx23885_irq_remove(dev, 0x01);
  932. if (dev->vbi_dev) {
  933. if (video_is_registered(dev->vbi_dev))
  934. video_unregister_device(dev->vbi_dev);
  935. else
  936. video_device_release(dev->vbi_dev);
  937. dev->vbi_dev = NULL;
  938. }
  939. if (dev->video_dev) {
  940. if (video_is_registered(dev->video_dev))
  941. video_unregister_device(dev->video_dev);
  942. else
  943. video_device_release(dev->video_dev);
  944. dev->video_dev = NULL;
  945. }
  946. if (dev->audio_dev)
  947. cx23885_audio_unregister(dev);
  948. }
  949. int cx23885_video_register(struct cx23885_dev *dev)
  950. {
  951. struct vb2_queue *q;
  952. int err;
  953. dprintk(1, "%s()\n", __func__);
  954. /* Initialize VBI template */
  955. cx23885_vbi_template = cx23885_video_template;
  956. strcpy(cx23885_vbi_template.name, "cx23885-vbi");
  957. dev->tvnorm = V4L2_STD_NTSC_M;
  958. dev->fmt = format_by_fourcc(V4L2_PIX_FMT_YUYV);
  959. dev->field = V4L2_FIELD_INTERLACED;
  960. dev->width = 720;
  961. dev->height = norm_maxh(dev->tvnorm);
  962. /* init video dma queues */
  963. INIT_LIST_HEAD(&dev->vidq.active);
  964. /* init vbi dma queues */
  965. INIT_LIST_HEAD(&dev->vbiq.active);
  966. cx23885_irq_add_enable(dev, 0x01);
  967. if ((TUNER_ABSENT != dev->tuner_type) &&
  968. ((dev->tuner_bus == 0) || (dev->tuner_bus == 1))) {
  969. struct v4l2_subdev *sd = NULL;
  970. if (dev->tuner_addr)
  971. sd = v4l2_i2c_new_subdev(&dev->v4l2_dev,
  972. &dev->i2c_bus[dev->tuner_bus].i2c_adap,
  973. "tuner", dev->tuner_addr, NULL);
  974. else
  975. sd = v4l2_i2c_new_subdev(&dev->v4l2_dev,
  976. &dev->i2c_bus[dev->tuner_bus].i2c_adap,
  977. "tuner", 0, v4l2_i2c_tuner_addrs(ADDRS_TV));
  978. if (sd) {
  979. struct tuner_setup tun_setup;
  980. memset(&tun_setup, 0, sizeof(tun_setup));
  981. tun_setup.mode_mask = T_ANALOG_TV;
  982. tun_setup.type = dev->tuner_type;
  983. tun_setup.addr = v4l2_i2c_subdev_addr(sd);
  984. tun_setup.tuner_callback = cx23885_tuner_callback;
  985. v4l2_subdev_call(sd, tuner, s_type_addr, &tun_setup);
  986. if ((dev->board == CX23885_BOARD_LEADTEK_WINFAST_PXTV1200) ||
  987. (dev->board == CX23885_BOARD_LEADTEK_WINFAST_PXPVR2200)) {
  988. struct xc2028_ctrl ctrl = {
  989. .fname = XC2028_DEFAULT_FIRMWARE,
  990. .max_len = 64
  991. };
  992. struct v4l2_priv_tun_config cfg = {
  993. .tuner = dev->tuner_type,
  994. .priv = &ctrl
  995. };
  996. v4l2_subdev_call(sd, tuner, s_config, &cfg);
  997. }
  998. if (dev->board == CX23885_BOARD_AVERMEDIA_HC81R) {
  999. struct xc2028_ctrl ctrl = {
  1000. .fname = "xc3028L-v36.fw",
  1001. .max_len = 64
  1002. };
  1003. struct v4l2_priv_tun_config cfg = {
  1004. .tuner = dev->tuner_type,
  1005. .priv = &ctrl
  1006. };
  1007. v4l2_subdev_call(sd, tuner, s_config, &cfg);
  1008. }
  1009. }
  1010. }
  1011. /* initial device configuration */
  1012. mutex_lock(&dev->lock);
  1013. cx23885_set_tvnorm(dev, dev->tvnorm);
  1014. cx23885_video_mux(dev, 0);
  1015. cx23885_audio_mux(dev, 0);
  1016. mutex_unlock(&dev->lock);
  1017. q = &dev->vb2_vidq;
  1018. q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1019. q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ;
  1020. q->gfp_flags = GFP_DMA32;
  1021. q->min_buffers_needed = 2;
  1022. q->drv_priv = dev;
  1023. q->buf_struct_size = sizeof(struct cx23885_buffer);
  1024. q->ops = &cx23885_video_qops;
  1025. q->mem_ops = &vb2_dma_sg_memops;
  1026. q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  1027. q->lock = &dev->lock;
  1028. err = vb2_queue_init(q);
  1029. if (err < 0)
  1030. goto fail_unreg;
  1031. q = &dev->vb2_vbiq;
  1032. q->type = V4L2_BUF_TYPE_VBI_CAPTURE;
  1033. q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ;
  1034. q->gfp_flags = GFP_DMA32;
  1035. q->min_buffers_needed = 2;
  1036. q->drv_priv = dev;
  1037. q->buf_struct_size = sizeof(struct cx23885_buffer);
  1038. q->ops = &cx23885_vbi_qops;
  1039. q->mem_ops = &vb2_dma_sg_memops;
  1040. q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  1041. q->lock = &dev->lock;
  1042. err = vb2_queue_init(q);
  1043. if (err < 0)
  1044. goto fail_unreg;
  1045. /* register Video device */
  1046. dev->video_dev = cx23885_vdev_init(dev, dev->pci,
  1047. &cx23885_video_template, "video");
  1048. dev->video_dev->queue = &dev->vb2_vidq;
  1049. err = video_register_device(dev->video_dev, VFL_TYPE_GRABBER,
  1050. video_nr[dev->nr]);
  1051. if (err < 0) {
  1052. printk(KERN_INFO "%s: can't register video device\n",
  1053. dev->name);
  1054. goto fail_unreg;
  1055. }
  1056. printk(KERN_INFO "%s: registered device %s [v4l2]\n",
  1057. dev->name, video_device_node_name(dev->video_dev));
  1058. /* register VBI device */
  1059. dev->vbi_dev = cx23885_vdev_init(dev, dev->pci,
  1060. &cx23885_vbi_template, "vbi");
  1061. dev->vbi_dev->queue = &dev->vb2_vbiq;
  1062. err = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
  1063. vbi_nr[dev->nr]);
  1064. if (err < 0) {
  1065. printk(KERN_INFO "%s: can't register vbi device\n",
  1066. dev->name);
  1067. goto fail_unreg;
  1068. }
  1069. printk(KERN_INFO "%s: registered device %s\n",
  1070. dev->name, video_device_node_name(dev->vbi_dev));
  1071. /* Register ALSA audio device */
  1072. dev->audio_dev = cx23885_audio_register(dev);
  1073. return 0;
  1074. fail_unreg:
  1075. cx23885_video_unregister(dev);
  1076. return err;
  1077. }