cx23885-video.c 35 KB

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