cx25821-video.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858
  1. /*
  2. * Driver for the Conexant CX25821 PCIe bridge
  3. *
  4. * Copyright (C) 2009 Conexant Systems Inc.
  5. * Authors <shu.lin@conexant.com>, <hiep.huynh@conexant.com>
  6. * Based on Steven Toth <stoth@linuxtv.org> cx23885 driver
  7. * Parts adapted/taken from Eduardo Moscoso Rubino
  8. * Copyright (C) 2009 Eduardo Moscoso Rubino <moscoso@TopoLogica.com>
  9. *
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. *
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  25. */
  26. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  27. #include "cx25821-video.h"
  28. MODULE_DESCRIPTION("v4l2 driver module for cx25821 based TV cards");
  29. MODULE_AUTHOR("Hiep Huynh <hiep.huynh@conexant.com>");
  30. MODULE_LICENSE("GPL");
  31. static unsigned int video_nr[] = {[0 ... (CX25821_MAXBOARDS - 1)] = UNSET };
  32. module_param_array(video_nr, int, NULL, 0444);
  33. MODULE_PARM_DESC(video_nr, "video device numbers");
  34. static unsigned int video_debug = VIDEO_DEBUG;
  35. module_param(video_debug, int, 0644);
  36. MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
  37. static unsigned int irq_debug;
  38. module_param(irq_debug, int, 0644);
  39. MODULE_PARM_DESC(irq_debug, "enable debug messages [IRQ handler]");
  40. unsigned int vid_limit = 16;
  41. module_param(vid_limit, int, 0644);
  42. MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");
  43. static void cx25821_init_controls(struct cx25821_dev *dev, int chan_num);
  44. #define FORMAT_FLAGS_PACKED 0x01
  45. struct cx25821_fmt formats[] = {
  46. {
  47. .name = "8 bpp, gray",
  48. .fourcc = V4L2_PIX_FMT_GREY,
  49. .depth = 8,
  50. .flags = FORMAT_FLAGS_PACKED,
  51. }, {
  52. .name = "4:1:1, packed, Y41P",
  53. .fourcc = V4L2_PIX_FMT_Y41P,
  54. .depth = 12,
  55. .flags = FORMAT_FLAGS_PACKED,
  56. }, {
  57. .name = "4:2:2, packed, YUYV",
  58. .fourcc = V4L2_PIX_FMT_YUYV,
  59. .depth = 16,
  60. .flags = FORMAT_FLAGS_PACKED,
  61. }, {
  62. .name = "4:2:2, packed, UYVY",
  63. .fourcc = V4L2_PIX_FMT_UYVY,
  64. .depth = 16,
  65. .flags = FORMAT_FLAGS_PACKED,
  66. }, {
  67. .name = "4:2:0, YUV",
  68. .fourcc = V4L2_PIX_FMT_YUV420,
  69. .depth = 12,
  70. .flags = FORMAT_FLAGS_PACKED,
  71. },
  72. };
  73. int cx25821_get_format_size(void)
  74. {
  75. return ARRAY_SIZE(formats);
  76. }
  77. struct cx25821_fmt *cx25821_format_by_fourcc(unsigned int fourcc)
  78. {
  79. unsigned int i;
  80. if (fourcc == V4L2_PIX_FMT_Y41P || fourcc == V4L2_PIX_FMT_YUV411P)
  81. return formats + 1;
  82. for (i = 0; i < ARRAY_SIZE(formats); i++)
  83. if (formats[i].fourcc == fourcc)
  84. return formats + i;
  85. pr_err("%s(0x%08x) NOT FOUND\n", __func__, fourcc);
  86. return NULL;
  87. }
  88. void cx25821_video_wakeup(struct cx25821_dev *dev, struct cx25821_dmaqueue *q,
  89. u32 count)
  90. {
  91. struct cx25821_buffer *buf;
  92. int bc;
  93. for (bc = 0;; bc++) {
  94. if (list_empty(&q->active)) {
  95. dprintk(1, "bc=%d (=0: active empty)\n", bc);
  96. break;
  97. }
  98. buf = list_entry(q->active.next, struct cx25821_buffer,
  99. vb.queue);
  100. /* count comes from the hw and it is 16bit wide --
  101. * this trick handles wrap-arounds correctly for
  102. * up to 32767 buffers in flight... */
  103. if ((s16) (count - buf->count) < 0)
  104. break;
  105. v4l2_get_timestamp(&buf->vb.ts);
  106. buf->vb.state = VIDEOBUF_DONE;
  107. list_del(&buf->vb.queue);
  108. wake_up(&buf->vb.done);
  109. }
  110. if (list_empty(&q->active))
  111. del_timer(&q->timeout);
  112. else
  113. mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
  114. if (bc != 1)
  115. pr_err("%s: %d buffers handled (should be 1)\n", __func__, bc);
  116. }
  117. int cx25821_set_tvnorm(struct cx25821_dev *dev, v4l2_std_id norm)
  118. {
  119. dprintk(1, "%s(norm = 0x%08x) name: [%s]\n",
  120. __func__, (unsigned int)norm, v4l2_norm_to_name(norm));
  121. dev->tvnorm = norm;
  122. /* Tell the internal A/V decoder */
  123. cx25821_call_all(dev, core, s_std, norm);
  124. return 0;
  125. }
  126. struct video_device *cx25821_vdev_init(struct cx25821_dev *dev,
  127. struct pci_dev *pci,
  128. const struct video_device *template,
  129. char *type)
  130. {
  131. struct video_device *vfd;
  132. dprintk(1, "%s()\n", __func__);
  133. vfd = video_device_alloc();
  134. if (NULL == vfd)
  135. return NULL;
  136. *vfd = *template;
  137. vfd->v4l2_dev = &dev->v4l2_dev;
  138. vfd->release = video_device_release;
  139. snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)", dev->name, type,
  140. cx25821_boards[dev->board].name);
  141. video_set_drvdata(vfd, dev);
  142. return vfd;
  143. }
  144. /*
  145. static int cx25821_ctrl_query(struct v4l2_queryctrl *qctrl)
  146. {
  147. int i;
  148. if (qctrl->id < V4L2_CID_BASE || qctrl->id >= V4L2_CID_LASTP1)
  149. return -EINVAL;
  150. for (i = 0; i < CX25821_CTLS; i++)
  151. if (cx25821_ctls[i].v.id == qctrl->id)
  152. break;
  153. if (i == CX25821_CTLS) {
  154. *qctrl = no_ctl;
  155. return 0;
  156. }
  157. *qctrl = cx25821_ctls[i].v;
  158. return 0;
  159. }
  160. */
  161. /* resource management */
  162. int cx25821_res_get(struct cx25821_dev *dev, struct cx25821_fh *fh,
  163. unsigned int bit)
  164. {
  165. dprintk(1, "%s()\n", __func__);
  166. if (fh->resources & bit)
  167. /* have it already allocated */
  168. return 1;
  169. /* is it free? */
  170. mutex_lock(&dev->lock);
  171. if (dev->channels[fh->channel_id].resources & bit) {
  172. /* no, someone else uses it */
  173. mutex_unlock(&dev->lock);
  174. return 0;
  175. }
  176. /* it's free, grab it */
  177. fh->resources |= bit;
  178. dev->channels[fh->channel_id].resources |= bit;
  179. dprintk(1, "res: get %d\n", bit);
  180. mutex_unlock(&dev->lock);
  181. return 1;
  182. }
  183. int cx25821_res_check(struct cx25821_fh *fh, unsigned int bit)
  184. {
  185. return fh->resources & bit;
  186. }
  187. int cx25821_res_locked(struct cx25821_fh *fh, unsigned int bit)
  188. {
  189. return fh->dev->channels[fh->channel_id].resources & bit;
  190. }
  191. void cx25821_res_free(struct cx25821_dev *dev, struct cx25821_fh *fh,
  192. unsigned int bits)
  193. {
  194. BUG_ON((fh->resources & bits) != bits);
  195. dprintk(1, "%s()\n", __func__);
  196. mutex_lock(&dev->lock);
  197. fh->resources &= ~bits;
  198. dev->channels[fh->channel_id].resources &= ~bits;
  199. dprintk(1, "res: put %d\n", bits);
  200. mutex_unlock(&dev->lock);
  201. }
  202. int cx25821_video_mux(struct cx25821_dev *dev, unsigned int input)
  203. {
  204. struct v4l2_routing route;
  205. memset(&route, 0, sizeof(route));
  206. dprintk(1, "%s(): video_mux: %d [vmux=%d, gpio=0x%x,0x%x,0x%x,0x%x]\n",
  207. __func__, input, INPUT(input)->vmux, INPUT(input)->gpio0,
  208. INPUT(input)->gpio1, INPUT(input)->gpio2, INPUT(input)->gpio3);
  209. dev->input = input;
  210. route.input = INPUT(input)->vmux;
  211. /* Tell the internal A/V decoder */
  212. cx25821_call_all(dev, video, s_routing, INPUT(input)->vmux, 0, 0);
  213. return 0;
  214. }
  215. int cx25821_start_video_dma(struct cx25821_dev *dev,
  216. struct cx25821_dmaqueue *q,
  217. struct cx25821_buffer *buf,
  218. const struct sram_channel *channel)
  219. {
  220. int tmp = 0;
  221. /* setup fifo + format */
  222. cx25821_sram_channel_setup(dev, channel, buf->bpl, buf->risc.dma);
  223. /* reset counter */
  224. cx_write(channel->gpcnt_ctl, 3);
  225. q->count = 1;
  226. /* enable irq */
  227. cx_set(PCI_INT_MSK, cx_read(PCI_INT_MSK) | (1 << channel->i));
  228. cx_set(channel->int_msk, 0x11);
  229. /* start dma */
  230. cx_write(channel->dma_ctl, 0x11); /* FIFO and RISC enable */
  231. /* make sure upstream setting if any is reversed */
  232. tmp = cx_read(VID_CH_MODE_SEL);
  233. cx_write(VID_CH_MODE_SEL, tmp & 0xFFFFFE00);
  234. return 0;
  235. }
  236. static int cx25821_restart_video_queue(struct cx25821_dev *dev,
  237. struct cx25821_dmaqueue *q,
  238. const struct sram_channel *channel)
  239. {
  240. struct cx25821_buffer *buf, *prev;
  241. struct list_head *item;
  242. if (!list_empty(&q->active)) {
  243. buf = list_entry(q->active.next, struct cx25821_buffer,
  244. vb.queue);
  245. cx25821_start_video_dma(dev, q, buf, channel);
  246. list_for_each(item, &q->active) {
  247. buf = list_entry(item, struct cx25821_buffer, vb.queue);
  248. buf->count = q->count++;
  249. }
  250. mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
  251. return 0;
  252. }
  253. prev = NULL;
  254. for (;;) {
  255. if (list_empty(&q->queued))
  256. return 0;
  257. buf = list_entry(q->queued.next, struct cx25821_buffer,
  258. vb.queue);
  259. if (NULL == prev) {
  260. list_move_tail(&buf->vb.queue, &q->active);
  261. cx25821_start_video_dma(dev, q, buf, channel);
  262. buf->vb.state = VIDEOBUF_ACTIVE;
  263. buf->count = q->count++;
  264. mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
  265. } else if (prev->vb.width == buf->vb.width &&
  266. prev->vb.height == buf->vb.height &&
  267. prev->fmt == buf->fmt) {
  268. list_move_tail(&buf->vb.queue, &q->active);
  269. buf->vb.state = VIDEOBUF_ACTIVE;
  270. buf->count = q->count++;
  271. prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
  272. prev->risc.jmp[2] = cpu_to_le32(0); /* Bits 63 - 32 */
  273. } else {
  274. return 0;
  275. }
  276. prev = buf;
  277. }
  278. }
  279. static void cx25821_vid_timeout(unsigned long data)
  280. {
  281. struct cx25821_data *timeout_data = (struct cx25821_data *)data;
  282. struct cx25821_dev *dev = timeout_data->dev;
  283. const struct sram_channel *channel = timeout_data->channel;
  284. struct cx25821_dmaqueue *q = &dev->channels[channel->i].vidq;
  285. struct cx25821_buffer *buf;
  286. unsigned long flags;
  287. /* cx25821_sram_channel_dump(dev, channel); */
  288. cx_clear(channel->dma_ctl, 0x11);
  289. spin_lock_irqsave(&dev->slock, flags);
  290. while (!list_empty(&q->active)) {
  291. buf = list_entry(q->active.next, struct cx25821_buffer,
  292. vb.queue);
  293. list_del(&buf->vb.queue);
  294. buf->vb.state = VIDEOBUF_ERROR;
  295. wake_up(&buf->vb.done);
  296. }
  297. cx25821_restart_video_queue(dev, q, channel);
  298. spin_unlock_irqrestore(&dev->slock, flags);
  299. }
  300. int cx25821_video_irq(struct cx25821_dev *dev, int chan_num, u32 status)
  301. {
  302. u32 count = 0;
  303. int handled = 0;
  304. u32 mask;
  305. const struct sram_channel *channel = dev->channels[chan_num].sram_channels;
  306. mask = cx_read(channel->int_msk);
  307. if (0 == (status & mask))
  308. return handled;
  309. cx_write(channel->int_stat, status);
  310. /* risc op code error */
  311. if (status & (1 << 16)) {
  312. pr_warn("%s, %s: video risc op code error\n",
  313. dev->name, channel->name);
  314. cx_clear(channel->dma_ctl, 0x11);
  315. cx25821_sram_channel_dump(dev, channel);
  316. }
  317. /* risc1 y */
  318. if (status & FLD_VID_DST_RISC1) {
  319. spin_lock(&dev->slock);
  320. count = cx_read(channel->gpcnt);
  321. cx25821_video_wakeup(dev, &dev->channels[channel->i].vidq,
  322. count);
  323. spin_unlock(&dev->slock);
  324. handled++;
  325. }
  326. /* risc2 y */
  327. if (status & 0x10) {
  328. dprintk(2, "stopper video\n");
  329. spin_lock(&dev->slock);
  330. cx25821_restart_video_queue(dev,
  331. &dev->channels[channel->i].vidq, channel);
  332. spin_unlock(&dev->slock);
  333. handled++;
  334. }
  335. return handled;
  336. }
  337. int cx25821_buffer_setup(struct videobuf_queue *q, unsigned int *count,
  338. unsigned int *size)
  339. {
  340. struct cx25821_fh *fh = q->priv_data;
  341. *size = fh->fmt->depth * fh->width * fh->height >> 3;
  342. if (0 == *count)
  343. *count = 32;
  344. if (*size * *count > vid_limit * 1024 * 1024)
  345. *count = (vid_limit * 1024 * 1024) / *size;
  346. return 0;
  347. }
  348. int cx25821_buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
  349. enum v4l2_field field)
  350. {
  351. struct cx25821_fh *fh = q->priv_data;
  352. struct cx25821_dev *dev = fh->dev;
  353. struct cx25821_buffer *buf =
  354. container_of(vb, struct cx25821_buffer, vb);
  355. int rc, init_buffer = 0;
  356. u32 line0_offset;
  357. struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
  358. int bpl_local = LINE_SIZE_D1;
  359. int channel_opened = fh->channel_id;
  360. BUG_ON(NULL == fh->fmt);
  361. if (fh->width < 48 || fh->width > 720 ||
  362. fh->height < 32 || fh->height > 576)
  363. return -EINVAL;
  364. buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
  365. if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
  366. return -EINVAL;
  367. if (buf->fmt != fh->fmt ||
  368. buf->vb.width != fh->width ||
  369. buf->vb.height != fh->height || buf->vb.field != field) {
  370. buf->fmt = fh->fmt;
  371. buf->vb.width = fh->width;
  372. buf->vb.height = fh->height;
  373. buf->vb.field = field;
  374. init_buffer = 1;
  375. }
  376. if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
  377. init_buffer = 1;
  378. rc = videobuf_iolock(q, &buf->vb, NULL);
  379. if (0 != rc) {
  380. printk(KERN_DEBUG pr_fmt("videobuf_iolock failed!\n"));
  381. goto fail;
  382. }
  383. }
  384. dprintk(1, "init_buffer=%d\n", init_buffer);
  385. if (init_buffer) {
  386. channel_opened = dev->channel_opened;
  387. if (channel_opened < 0 || channel_opened > 7)
  388. channel_opened = 7;
  389. if (dev->channels[channel_opened].pixel_formats ==
  390. PIXEL_FRMT_411)
  391. buf->bpl = (buf->fmt->depth * buf->vb.width) >> 3;
  392. else
  393. buf->bpl = (buf->fmt->depth >> 3) * (buf->vb.width);
  394. if (dev->channels[channel_opened].pixel_formats ==
  395. PIXEL_FRMT_411) {
  396. bpl_local = buf->bpl;
  397. } else {
  398. bpl_local = buf->bpl; /* Default */
  399. if (channel_opened >= 0 && channel_opened <= 7) {
  400. if (dev->channels[channel_opened]
  401. .use_cif_resolution) {
  402. if (dev->tvnorm & V4L2_STD_PAL_BG ||
  403. dev->tvnorm & V4L2_STD_PAL_DK)
  404. bpl_local = 352 << 1;
  405. else
  406. bpl_local = dev->channels[
  407. channel_opened].
  408. cif_width << 1;
  409. }
  410. }
  411. }
  412. switch (buf->vb.field) {
  413. case V4L2_FIELD_TOP:
  414. cx25821_risc_buffer(dev->pci, &buf->risc,
  415. dma->sglist, 0, UNSET,
  416. buf->bpl, 0, buf->vb.height);
  417. break;
  418. case V4L2_FIELD_BOTTOM:
  419. cx25821_risc_buffer(dev->pci, &buf->risc,
  420. dma->sglist, UNSET, 0,
  421. buf->bpl, 0, buf->vb.height);
  422. break;
  423. case V4L2_FIELD_INTERLACED:
  424. /* All other formats are top field first */
  425. line0_offset = 0;
  426. dprintk(1, "top field first\n");
  427. cx25821_risc_buffer(dev->pci, &buf->risc,
  428. dma->sglist, line0_offset,
  429. bpl_local, bpl_local, bpl_local,
  430. buf->vb.height >> 1);
  431. break;
  432. case V4L2_FIELD_SEQ_TB:
  433. cx25821_risc_buffer(dev->pci, &buf->risc,
  434. dma->sglist,
  435. 0, buf->bpl * (buf->vb.height >> 1),
  436. buf->bpl, 0, buf->vb.height >> 1);
  437. break;
  438. case V4L2_FIELD_SEQ_BT:
  439. cx25821_risc_buffer(dev->pci, &buf->risc,
  440. dma->sglist,
  441. buf->bpl * (buf->vb.height >> 1), 0,
  442. buf->bpl, 0, buf->vb.height >> 1);
  443. break;
  444. default:
  445. BUG();
  446. }
  447. }
  448. dprintk(2, "[%p/%d] buffer_prep - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
  449. buf, buf->vb.i, fh->width, fh->height, fh->fmt->depth,
  450. fh->fmt->name, (unsigned long)buf->risc.dma);
  451. buf->vb.state = VIDEOBUF_PREPARED;
  452. return 0;
  453. fail:
  454. cx25821_free_buffer(q, buf);
  455. return rc;
  456. }
  457. void cx25821_buffer_release(struct videobuf_queue *q,
  458. struct videobuf_buffer *vb)
  459. {
  460. struct cx25821_buffer *buf =
  461. container_of(vb, struct cx25821_buffer, vb);
  462. cx25821_free_buffer(q, buf);
  463. }
  464. struct videobuf_queue *get_queue(struct cx25821_fh *fh)
  465. {
  466. switch (fh->type) {
  467. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  468. return &fh->vidq;
  469. default:
  470. BUG();
  471. return NULL;
  472. }
  473. }
  474. int cx25821_get_resource(struct cx25821_fh *fh, int resource)
  475. {
  476. switch (fh->type) {
  477. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  478. return resource;
  479. default:
  480. BUG();
  481. return 0;
  482. }
  483. }
  484. int cx25821_video_mmap(struct file *file, struct vm_area_struct *vma)
  485. {
  486. struct cx25821_fh *fh = file->private_data;
  487. return videobuf_mmap_mapper(get_queue(fh), vma);
  488. }
  489. static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  490. {
  491. struct cx25821_buffer *buf =
  492. container_of(vb, struct cx25821_buffer, vb);
  493. struct cx25821_buffer *prev;
  494. struct cx25821_fh *fh = vq->priv_data;
  495. struct cx25821_dev *dev = fh->dev;
  496. struct cx25821_dmaqueue *q = &dev->channels[fh->channel_id].vidq;
  497. /* add jump to stopper */
  498. buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
  499. buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
  500. buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
  501. dprintk(2, "jmp to stopper (0x%x)\n", buf->risc.jmp[1]);
  502. if (!list_empty(&q->queued)) {
  503. list_add_tail(&buf->vb.queue, &q->queued);
  504. buf->vb.state = VIDEOBUF_QUEUED;
  505. dprintk(2, "[%p/%d] buffer_queue - append to queued\n", buf,
  506. buf->vb.i);
  507. } else if (list_empty(&q->active)) {
  508. list_add_tail(&buf->vb.queue, &q->active);
  509. cx25821_start_video_dma(dev, q, buf,
  510. dev->channels[fh->channel_id].sram_channels);
  511. buf->vb.state = VIDEOBUF_ACTIVE;
  512. buf->count = q->count++;
  513. mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
  514. dprintk(2, "[%p/%d] buffer_queue - first active, buf cnt = %d, q->count = %d\n",
  515. buf, buf->vb.i, buf->count, q->count);
  516. } else {
  517. prev = list_entry(q->active.prev, struct cx25821_buffer,
  518. vb.queue);
  519. if (prev->vb.width == buf->vb.width
  520. && prev->vb.height == buf->vb.height
  521. && prev->fmt == buf->fmt) {
  522. list_add_tail(&buf->vb.queue, &q->active);
  523. buf->vb.state = VIDEOBUF_ACTIVE;
  524. buf->count = q->count++;
  525. prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
  526. /* 64 bit bits 63-32 */
  527. prev->risc.jmp[2] = cpu_to_le32(0);
  528. dprintk(2, "[%p/%d] buffer_queue - append to active, buf->count=%d\n",
  529. buf, buf->vb.i, buf->count);
  530. } else {
  531. list_add_tail(&buf->vb.queue, &q->queued);
  532. buf->vb.state = VIDEOBUF_QUEUED;
  533. dprintk(2, "[%p/%d] buffer_queue - first queued\n", buf,
  534. buf->vb.i);
  535. }
  536. }
  537. if (list_empty(&q->active))
  538. dprintk(2, "active queue empty!\n");
  539. }
  540. static struct videobuf_queue_ops cx25821_video_qops = {
  541. .buf_setup = cx25821_buffer_setup,
  542. .buf_prepare = cx25821_buffer_prepare,
  543. .buf_queue = buffer_queue,
  544. .buf_release = cx25821_buffer_release,
  545. };
  546. static int video_open(struct file *file)
  547. {
  548. struct video_device *vdev = video_devdata(file);
  549. struct cx25821_dev *h, *dev = video_drvdata(file);
  550. struct cx25821_fh *fh;
  551. struct list_head *list;
  552. int minor = video_devdata(file)->minor;
  553. enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  554. u32 pix_format;
  555. int ch_id = 0;
  556. int i;
  557. dprintk(1, "open dev=%s type=%s\n", video_device_node_name(vdev),
  558. v4l2_type_names[type]);
  559. /* allocate + initialize per filehandle data */
  560. fh = kzalloc(sizeof(*fh), GFP_KERNEL);
  561. if (NULL == fh)
  562. return -ENOMEM;
  563. mutex_lock(&cx25821_devlist_mutex);
  564. list_for_each(list, &cx25821_devlist)
  565. {
  566. h = list_entry(list, struct cx25821_dev, devlist);
  567. for (i = 0; i < MAX_VID_CHANNEL_NUM - 1; i++) {
  568. if (h->channels[i].video_dev &&
  569. h->channels[i].video_dev->minor == minor) {
  570. dev = h;
  571. ch_id = i;
  572. type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  573. }
  574. }
  575. }
  576. if (NULL == dev) {
  577. mutex_unlock(&cx25821_devlist_mutex);
  578. kfree(fh);
  579. return -ENODEV;
  580. }
  581. file->private_data = fh;
  582. fh->dev = dev;
  583. fh->type = type;
  584. fh->width = 720;
  585. fh->channel_id = ch_id;
  586. if (dev->tvnorm & V4L2_STD_PAL_BG || dev->tvnorm & V4L2_STD_PAL_DK)
  587. fh->height = 576;
  588. else
  589. fh->height = 480;
  590. dev->channel_opened = fh->channel_id;
  591. if (dev->channels[ch_id].pixel_formats == PIXEL_FRMT_411)
  592. pix_format = V4L2_PIX_FMT_Y41P;
  593. else
  594. pix_format = V4L2_PIX_FMT_YUYV;
  595. fh->fmt = cx25821_format_by_fourcc(pix_format);
  596. v4l2_prio_open(&dev->channels[ch_id].prio, &fh->prio);
  597. videobuf_queue_sg_init(&fh->vidq, &cx25821_video_qops, &dev->pci->dev,
  598. &dev->slock, V4L2_BUF_TYPE_VIDEO_CAPTURE,
  599. V4L2_FIELD_INTERLACED, sizeof(struct cx25821_buffer),
  600. fh, NULL);
  601. dprintk(1, "post videobuf_queue_init()\n");
  602. mutex_unlock(&cx25821_devlist_mutex);
  603. return 0;
  604. }
  605. static ssize_t video_read(struct file *file, char __user * data, size_t count,
  606. loff_t *ppos)
  607. {
  608. struct cx25821_fh *fh = file->private_data;
  609. switch (fh->type) {
  610. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  611. if (cx25821_res_locked(fh, RESOURCE_VIDEO0))
  612. return -EBUSY;
  613. return videobuf_read_one(&fh->vidq, data, count, ppos,
  614. file->f_flags & O_NONBLOCK);
  615. default:
  616. BUG();
  617. return 0;
  618. }
  619. }
  620. static unsigned int video_poll(struct file *file,
  621. struct poll_table_struct *wait)
  622. {
  623. struct cx25821_fh *fh = file->private_data;
  624. struct cx25821_buffer *buf;
  625. if (cx25821_res_check(fh, RESOURCE_VIDEO0)) {
  626. /* streaming capture */
  627. if (list_empty(&fh->vidq.stream))
  628. return POLLERR;
  629. buf = list_entry(fh->vidq.stream.next,
  630. struct cx25821_buffer, vb.stream);
  631. } else {
  632. /* read() capture */
  633. buf = (struct cx25821_buffer *)fh->vidq.read_buf;
  634. if (NULL == buf)
  635. return POLLERR;
  636. }
  637. poll_wait(file, &buf->vb.done, wait);
  638. if (buf->vb.state == VIDEOBUF_DONE || buf->vb.state == VIDEOBUF_ERROR) {
  639. if (buf->vb.state == VIDEOBUF_DONE) {
  640. struct cx25821_dev *dev = fh->dev;
  641. if (dev && dev->channels[fh->channel_id]
  642. .use_cif_resolution) {
  643. u8 cam_id = *((char *)buf->vb.baddr + 3);
  644. memcpy((char *)buf->vb.baddr,
  645. (char *)buf->vb.baddr + (fh->width * 2),
  646. (fh->width * 2));
  647. *((char *)buf->vb.baddr + 3) = cam_id;
  648. }
  649. }
  650. return POLLIN | POLLRDNORM;
  651. }
  652. return 0;
  653. }
  654. static int video_release(struct file *file)
  655. {
  656. struct cx25821_fh *fh = file->private_data;
  657. struct cx25821_dev *dev = fh->dev;
  658. const struct sram_channel *sram_ch =
  659. dev->channels[0].sram_channels;
  660. /* stop the risc engine and fifo */
  661. cx_write(sram_ch->dma_ctl, 0); /* FIFO and RISC disable */
  662. /* stop video capture */
  663. if (cx25821_res_check(fh, RESOURCE_VIDEO0)) {
  664. videobuf_queue_cancel(&fh->vidq);
  665. cx25821_res_free(dev, fh, RESOURCE_VIDEO0);
  666. }
  667. if (fh->vidq.read_buf) {
  668. cx25821_buffer_release(&fh->vidq, fh->vidq.read_buf);
  669. kfree(fh->vidq.read_buf);
  670. }
  671. videobuf_mmap_free(&fh->vidq);
  672. v4l2_prio_close(&dev->channels[fh->channel_id].prio, fh->prio);
  673. file->private_data = NULL;
  674. kfree(fh);
  675. return 0;
  676. }
  677. static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
  678. {
  679. struct cx25821_fh *fh = priv;
  680. struct cx25821_dev *dev = fh->dev;
  681. if (unlikely(fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE))
  682. return -EINVAL;
  683. if (unlikely(i != fh->type))
  684. return -EINVAL;
  685. if (unlikely(!cx25821_res_get(dev, fh, cx25821_get_resource(fh,
  686. RESOURCE_VIDEO0))))
  687. return -EBUSY;
  688. return videobuf_streamon(get_queue(fh));
  689. }
  690. static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
  691. {
  692. struct cx25821_fh *fh = priv;
  693. struct cx25821_dev *dev = fh->dev;
  694. int err, res;
  695. if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  696. return -EINVAL;
  697. if (i != fh->type)
  698. return -EINVAL;
  699. res = cx25821_get_resource(fh, RESOURCE_VIDEO0);
  700. err = videobuf_streamoff(get_queue(fh));
  701. if (err < 0)
  702. return err;
  703. cx25821_res_free(dev, fh, res);
  704. return 0;
  705. }
  706. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  707. struct v4l2_format *f)
  708. {
  709. struct cx25821_fh *fh = priv;
  710. struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
  711. struct v4l2_mbus_framefmt mbus_fmt;
  712. int err;
  713. int pix_format = PIXEL_FRMT_422;
  714. if (fh) {
  715. err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
  716. fh->prio);
  717. if (0 != err)
  718. return err;
  719. }
  720. dprintk(2, "%s()\n", __func__);
  721. err = cx25821_vidioc_try_fmt_vid_cap(file, priv, f);
  722. if (0 != err)
  723. return err;
  724. fh->fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
  725. fh->vidq.field = f->fmt.pix.field;
  726. /* check if width and height is valid based on set standard */
  727. if (cx25821_is_valid_width(f->fmt.pix.width, dev->tvnorm))
  728. fh->width = f->fmt.pix.width;
  729. if (cx25821_is_valid_height(f->fmt.pix.height, dev->tvnorm))
  730. fh->height = f->fmt.pix.height;
  731. if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_Y41P)
  732. pix_format = PIXEL_FRMT_411;
  733. else if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_YUYV)
  734. pix_format = PIXEL_FRMT_422;
  735. else
  736. return -EINVAL;
  737. cx25821_set_pixel_format(dev, SRAM_CH00, pix_format);
  738. /* check if cif resolution */
  739. if (fh->width == 320 || fh->width == 352)
  740. dev->channels[fh->channel_id].use_cif_resolution = 1;
  741. else
  742. dev->channels[fh->channel_id].use_cif_resolution = 0;
  743. dev->channels[fh->channel_id].cif_width = fh->width;
  744. medusa_set_resolution(dev, fh->width, SRAM_CH00);
  745. dprintk(2, "%s(): width=%d height=%d field=%d\n", __func__, fh->width,
  746. fh->height, fh->vidq.field);
  747. v4l2_fill_mbus_format(&mbus_fmt, &f->fmt.pix, V4L2_MBUS_FMT_FIXED);
  748. cx25821_call_all(dev, video, s_mbus_fmt, &mbus_fmt);
  749. return 0;
  750. }
  751. static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
  752. {
  753. int ret_val = 0;
  754. struct cx25821_fh *fh = priv;
  755. struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
  756. ret_val = videobuf_dqbuf(get_queue(fh), p, file->f_flags & O_NONBLOCK);
  757. p->sequence = dev->channels[fh->channel_id].vidq.count;
  758. return ret_val;
  759. }
  760. static int vidioc_log_status(struct file *file, void *priv)
  761. {
  762. struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
  763. struct cx25821_fh *fh = priv;
  764. const struct sram_channel *sram_ch =
  765. dev->channels[fh->channel_id].sram_channels;
  766. u32 tmp = 0;
  767. cx25821_call_all(dev, core, log_status);
  768. tmp = cx_read(sram_ch->dma_ctl);
  769. pr_info("Video input 0 is %s\n",
  770. (tmp & 0x11) ? "streaming" : "stopped");
  771. return 0;
  772. }
  773. static int vidioc_s_ctrl(struct file *file, void *priv,
  774. struct v4l2_control *ctl)
  775. {
  776. struct cx25821_fh *fh = priv;
  777. struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
  778. int err;
  779. if (fh) {
  780. err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
  781. fh->prio);
  782. if (0 != err)
  783. return err;
  784. }
  785. return cx25821_set_control(dev, ctl, fh->channel_id);
  786. }
  787. /* VIDEO IOCTLS */
  788. int cx25821_vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  789. struct v4l2_format *f)
  790. {
  791. struct cx25821_fh *fh = priv;
  792. f->fmt.pix.width = fh->width;
  793. f->fmt.pix.height = fh->height;
  794. f->fmt.pix.field = fh->vidq.field;
  795. f->fmt.pix.pixelformat = fh->fmt->fourcc;
  796. f->fmt.pix.bytesperline = (f->fmt.pix.width * fh->fmt->depth) >> 3;
  797. f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
  798. return 0;
  799. }
  800. int cx25821_vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  801. struct v4l2_format *f)
  802. {
  803. struct cx25821_fmt *fmt;
  804. enum v4l2_field field;
  805. unsigned int maxw, maxh;
  806. fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
  807. if (NULL == fmt)
  808. return -EINVAL;
  809. field = f->fmt.pix.field;
  810. maxw = 720;
  811. maxh = 576;
  812. if (V4L2_FIELD_ANY == field) {
  813. if (f->fmt.pix.height > maxh / 2)
  814. field = V4L2_FIELD_INTERLACED;
  815. else
  816. field = V4L2_FIELD_TOP;
  817. }
  818. switch (field) {
  819. case V4L2_FIELD_TOP:
  820. case V4L2_FIELD_BOTTOM:
  821. maxh = maxh / 2;
  822. break;
  823. case V4L2_FIELD_INTERLACED:
  824. break;
  825. default:
  826. return -EINVAL;
  827. }
  828. f->fmt.pix.field = field;
  829. if (f->fmt.pix.height < 32)
  830. f->fmt.pix.height = 32;
  831. if (f->fmt.pix.height > maxh)
  832. f->fmt.pix.height = maxh;
  833. if (f->fmt.pix.width < 48)
  834. f->fmt.pix.width = 48;
  835. if (f->fmt.pix.width > maxw)
  836. f->fmt.pix.width = maxw;
  837. f->fmt.pix.width &= ~0x03;
  838. f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
  839. f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
  840. return 0;
  841. }
  842. int cx25821_vidioc_querycap(struct file *file, void *priv,
  843. struct v4l2_capability *cap)
  844. {
  845. struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
  846. struct cx25821_fh *fh = priv;
  847. const u32 cap_input = V4L2_CAP_VIDEO_CAPTURE |
  848. V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
  849. const u32 cap_output = V4L2_CAP_VIDEO_OUTPUT;
  850. strcpy(cap->driver, "cx25821");
  851. strlcpy(cap->card, cx25821_boards[dev->board].name, sizeof(cap->card));
  852. sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
  853. if (fh->channel_id >= VID_CHANNEL_NUM)
  854. cap->device_caps = cap_output;
  855. else
  856. cap->device_caps = cap_input;
  857. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  858. return 0;
  859. }
  860. int cx25821_vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  861. struct v4l2_fmtdesc *f)
  862. {
  863. if (unlikely(f->index >= ARRAY_SIZE(formats)))
  864. return -EINVAL;
  865. strlcpy(f->description, formats[f->index].name, sizeof(f->description));
  866. f->pixelformat = formats[f->index].fourcc;
  867. return 0;
  868. }
  869. int cx25821_vidioc_reqbufs(struct file *file, void *priv,
  870. struct v4l2_requestbuffers *p)
  871. {
  872. struct cx25821_fh *fh = priv;
  873. return videobuf_reqbufs(get_queue(fh), p);
  874. }
  875. int cx25821_vidioc_querybuf(struct file *file, void *priv,
  876. struct v4l2_buffer *p)
  877. {
  878. struct cx25821_fh *fh = priv;
  879. return videobuf_querybuf(get_queue(fh), p);
  880. }
  881. int cx25821_vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
  882. {
  883. struct cx25821_fh *fh = priv;
  884. return videobuf_qbuf(get_queue(fh), p);
  885. }
  886. int cx25821_vidioc_g_priority(struct file *file, void *f, enum v4l2_priority *p)
  887. {
  888. struct cx25821_dev *dev = ((struct cx25821_fh *)f)->dev;
  889. struct cx25821_fh *fh = f;
  890. *p = v4l2_prio_max(&dev->channels[fh->channel_id].prio);
  891. return 0;
  892. }
  893. int cx25821_vidioc_s_priority(struct file *file, void *f,
  894. enum v4l2_priority prio)
  895. {
  896. struct cx25821_fh *fh = f;
  897. struct cx25821_dev *dev = ((struct cx25821_fh *)f)->dev;
  898. return v4l2_prio_change(&dev->channels[fh->channel_id].prio, &fh->prio,
  899. prio);
  900. }
  901. int cx25821_vidioc_g_std(struct file *file, void *priv, v4l2_std_id *tvnorms)
  902. {
  903. struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
  904. *tvnorms = dev->tvnorm;
  905. return 0;
  906. }
  907. int cx25821_vidioc_s_std(struct file *file, void *priv, v4l2_std_id tvnorms)
  908. {
  909. struct cx25821_fh *fh = priv;
  910. struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
  911. int err;
  912. dprintk(1, "%s()\n", __func__);
  913. if (fh) {
  914. err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
  915. fh->prio);
  916. if (0 != err)
  917. return err;
  918. }
  919. if (dev->tvnorm == tvnorms)
  920. return 0;
  921. mutex_lock(&dev->lock);
  922. cx25821_set_tvnorm(dev, tvnorms);
  923. mutex_unlock(&dev->lock);
  924. medusa_set_videostandard(dev);
  925. return 0;
  926. }
  927. int cx25821_enum_input(struct cx25821_dev *dev, struct v4l2_input *i)
  928. {
  929. static const char * const iname[] = {
  930. [CX25821_VMUX_COMPOSITE] = "Composite",
  931. [CX25821_VMUX_SVIDEO] = "S-Video",
  932. [CX25821_VMUX_DEBUG] = "for debug only",
  933. };
  934. unsigned int n;
  935. dprintk(1, "%s()\n", __func__);
  936. n = i->index;
  937. if (n >= 2)
  938. return -EINVAL;
  939. if (0 == INPUT(n)->type)
  940. return -EINVAL;
  941. i->type = V4L2_INPUT_TYPE_CAMERA;
  942. strcpy(i->name, iname[INPUT(n)->type]);
  943. i->std = CX25821_NORMS;
  944. return 0;
  945. }
  946. int cx25821_vidioc_enum_input(struct file *file, void *priv,
  947. struct v4l2_input *i)
  948. {
  949. struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
  950. dprintk(1, "%s()\n", __func__);
  951. return cx25821_enum_input(dev, i);
  952. }
  953. int cx25821_vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  954. {
  955. struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
  956. *i = dev->input;
  957. dprintk(1, "%s(): returns %d\n", __func__, *i);
  958. return 0;
  959. }
  960. int cx25821_vidioc_s_input(struct file *file, void *priv, unsigned int i)
  961. {
  962. struct cx25821_fh *fh = priv;
  963. struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
  964. int err;
  965. dprintk(1, "%s(%d)\n", __func__, i);
  966. if (fh) {
  967. err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
  968. fh->prio);
  969. if (0 != err)
  970. return err;
  971. }
  972. if (i >= CX25821_NR_INPUT) {
  973. dprintk(1, "%s(): -EINVAL\n", __func__);
  974. return -EINVAL;
  975. }
  976. mutex_lock(&dev->lock);
  977. cx25821_video_mux(dev, i);
  978. mutex_unlock(&dev->lock);
  979. return 0;
  980. }
  981. #ifdef CONFIG_VIDEO_ADV_DEBUG
  982. int cx25821_vidioc_g_register(struct file *file, void *fh,
  983. struct v4l2_dbg_register *reg)
  984. {
  985. struct cx25821_dev *dev = ((struct cx25821_fh *)fh)->dev;
  986. if (!v4l2_chip_match_host(&reg->match))
  987. return -EINVAL;
  988. cx25821_call_all(dev, core, g_register, reg);
  989. return 0;
  990. }
  991. int cx25821_vidioc_s_register(struct file *file, void *fh,
  992. const struct v4l2_dbg_register *reg)
  993. {
  994. struct cx25821_dev *dev = ((struct cx25821_fh *)fh)->dev;
  995. if (!v4l2_chip_match_host(&reg->match))
  996. return -EINVAL;
  997. cx25821_call_all(dev, core, s_register, reg);
  998. return 0;
  999. }
  1000. #endif
  1001. /*****************************************************************************/
  1002. static const struct v4l2_queryctrl no_ctl = {
  1003. .name = "42",
  1004. .flags = V4L2_CTRL_FLAG_DISABLED,
  1005. };
  1006. static struct v4l2_queryctrl cx25821_ctls[] = {
  1007. /* --- video --- */
  1008. {
  1009. .id = V4L2_CID_BRIGHTNESS,
  1010. .name = "Brightness",
  1011. .minimum = 0,
  1012. .maximum = 10000,
  1013. .step = 1,
  1014. .default_value = 6200,
  1015. .type = V4L2_CTRL_TYPE_INTEGER,
  1016. }, {
  1017. .id = V4L2_CID_CONTRAST,
  1018. .name = "Contrast",
  1019. .minimum = 0,
  1020. .maximum = 10000,
  1021. .step = 1,
  1022. .default_value = 5000,
  1023. .type = V4L2_CTRL_TYPE_INTEGER,
  1024. }, {
  1025. .id = V4L2_CID_SATURATION,
  1026. .name = "Saturation",
  1027. .minimum = 0,
  1028. .maximum = 10000,
  1029. .step = 1,
  1030. .default_value = 5000,
  1031. .type = V4L2_CTRL_TYPE_INTEGER,
  1032. }, {
  1033. .id = V4L2_CID_HUE,
  1034. .name = "Hue",
  1035. .minimum = 0,
  1036. .maximum = 10000,
  1037. .step = 1,
  1038. .default_value = 5000,
  1039. .type = V4L2_CTRL_TYPE_INTEGER,
  1040. }
  1041. };
  1042. static const int CX25821_CTLS = ARRAY_SIZE(cx25821_ctls);
  1043. static int cx25821_ctrl_query(struct v4l2_queryctrl *qctrl)
  1044. {
  1045. int i;
  1046. if (qctrl->id < V4L2_CID_BASE || qctrl->id >= V4L2_CID_LASTP1)
  1047. return -EINVAL;
  1048. for (i = 0; i < CX25821_CTLS; i++)
  1049. if (cx25821_ctls[i].id == qctrl->id)
  1050. break;
  1051. if (i == CX25821_CTLS) {
  1052. *qctrl = no_ctl;
  1053. return 0;
  1054. }
  1055. *qctrl = cx25821_ctls[i];
  1056. return 0;
  1057. }
  1058. int cx25821_vidioc_queryctrl(struct file *file, void *priv,
  1059. struct v4l2_queryctrl *qctrl)
  1060. {
  1061. return cx25821_ctrl_query(qctrl);
  1062. }
  1063. /* ------------------------------------------------------------------ */
  1064. /* VIDEO CTRL IOCTLS */
  1065. static const struct v4l2_queryctrl *ctrl_by_id(unsigned int id)
  1066. {
  1067. unsigned int i;
  1068. for (i = 0; i < CX25821_CTLS; i++)
  1069. if (cx25821_ctls[i].id == id)
  1070. return cx25821_ctls + i;
  1071. return NULL;
  1072. }
  1073. int cx25821_vidioc_g_ctrl(struct file *file, void *priv,
  1074. struct v4l2_control *ctl)
  1075. {
  1076. struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
  1077. struct cx25821_fh *fh = priv;
  1078. const struct v4l2_queryctrl *ctrl;
  1079. ctrl = ctrl_by_id(ctl->id);
  1080. if (NULL == ctrl)
  1081. return -EINVAL;
  1082. switch (ctl->id) {
  1083. case V4L2_CID_BRIGHTNESS:
  1084. ctl->value = dev->channels[fh->channel_id].ctl_bright;
  1085. break;
  1086. case V4L2_CID_HUE:
  1087. ctl->value = dev->channels[fh->channel_id].ctl_hue;
  1088. break;
  1089. case V4L2_CID_CONTRAST:
  1090. ctl->value = dev->channels[fh->channel_id].ctl_contrast;
  1091. break;
  1092. case V4L2_CID_SATURATION:
  1093. ctl->value = dev->channels[fh->channel_id].ctl_saturation;
  1094. break;
  1095. }
  1096. return 0;
  1097. }
  1098. int cx25821_set_control(struct cx25821_dev *dev,
  1099. struct v4l2_control *ctl, int chan_num)
  1100. {
  1101. int err;
  1102. const struct v4l2_queryctrl *ctrl;
  1103. err = -EINVAL;
  1104. ctrl = ctrl_by_id(ctl->id);
  1105. if (NULL == ctrl)
  1106. return err;
  1107. switch (ctrl->type) {
  1108. case V4L2_CTRL_TYPE_BOOLEAN:
  1109. case V4L2_CTRL_TYPE_MENU:
  1110. case V4L2_CTRL_TYPE_INTEGER:
  1111. if (ctl->value < ctrl->minimum)
  1112. ctl->value = ctrl->minimum;
  1113. if (ctl->value > ctrl->maximum)
  1114. ctl->value = ctrl->maximum;
  1115. break;
  1116. default:
  1117. /* nothing */ ;
  1118. }
  1119. switch (ctl->id) {
  1120. case V4L2_CID_BRIGHTNESS:
  1121. dev->channels[chan_num].ctl_bright = ctl->value;
  1122. medusa_set_brightness(dev, ctl->value, chan_num);
  1123. break;
  1124. case V4L2_CID_HUE:
  1125. dev->channels[chan_num].ctl_hue = ctl->value;
  1126. medusa_set_hue(dev, ctl->value, chan_num);
  1127. break;
  1128. case V4L2_CID_CONTRAST:
  1129. dev->channels[chan_num].ctl_contrast = ctl->value;
  1130. medusa_set_contrast(dev, ctl->value, chan_num);
  1131. break;
  1132. case V4L2_CID_SATURATION:
  1133. dev->channels[chan_num].ctl_saturation = ctl->value;
  1134. medusa_set_saturation(dev, ctl->value, chan_num);
  1135. break;
  1136. }
  1137. err = 0;
  1138. return err;
  1139. }
  1140. static void cx25821_init_controls(struct cx25821_dev *dev, int chan_num)
  1141. {
  1142. struct v4l2_control ctrl;
  1143. int i;
  1144. for (i = 0; i < CX25821_CTLS; i++) {
  1145. ctrl.id = cx25821_ctls[i].id;
  1146. ctrl.value = cx25821_ctls[i].default_value;
  1147. cx25821_set_control(dev, &ctrl, chan_num);
  1148. }
  1149. }
  1150. int cx25821_vidioc_cropcap(struct file *file, void *priv,
  1151. struct v4l2_cropcap *cropcap)
  1152. {
  1153. struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
  1154. if (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1155. return -EINVAL;
  1156. cropcap->bounds.top = 0;
  1157. cropcap->bounds.left = 0;
  1158. cropcap->bounds.width = 720;
  1159. cropcap->bounds.height = dev->tvnorm == V4L2_STD_PAL_BG ? 576 : 480;
  1160. cropcap->pixelaspect.numerator =
  1161. dev->tvnorm == V4L2_STD_PAL_BG ? 59 : 10;
  1162. cropcap->pixelaspect.denominator =
  1163. dev->tvnorm == V4L2_STD_PAL_BG ? 54 : 11;
  1164. cropcap->defrect = cropcap->bounds;
  1165. return 0;
  1166. }
  1167. int cx25821_vidioc_s_crop(struct file *file, void *priv, const struct v4l2_crop *crop)
  1168. {
  1169. struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
  1170. struct cx25821_fh *fh = priv;
  1171. int err;
  1172. if (fh) {
  1173. err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
  1174. fh->prio);
  1175. if (0 != err)
  1176. return err;
  1177. }
  1178. /* cx25821_vidioc_s_crop not supported */
  1179. return -EINVAL;
  1180. }
  1181. int cx25821_vidioc_g_crop(struct file *file, void *priv, struct v4l2_crop *crop)
  1182. {
  1183. /* cx25821_vidioc_g_crop not supported */
  1184. return -EINVAL;
  1185. }
  1186. int cx25821_is_valid_width(u32 width, v4l2_std_id tvnorm)
  1187. {
  1188. if (tvnorm == V4L2_STD_PAL_BG) {
  1189. if (width == 352 || width == 720)
  1190. return 1;
  1191. else
  1192. return 0;
  1193. }
  1194. if (tvnorm == V4L2_STD_NTSC_M) {
  1195. if (width == 320 || width == 352 || width == 720)
  1196. return 1;
  1197. else
  1198. return 0;
  1199. }
  1200. return 0;
  1201. }
  1202. int cx25821_is_valid_height(u32 height, v4l2_std_id tvnorm)
  1203. {
  1204. if (tvnorm == V4L2_STD_PAL_BG) {
  1205. if (height == 576 || height == 288)
  1206. return 1;
  1207. else
  1208. return 0;
  1209. }
  1210. if (tvnorm == V4L2_STD_NTSC_M) {
  1211. if (height == 480 || height == 240)
  1212. return 1;
  1213. else
  1214. return 0;
  1215. }
  1216. return 0;
  1217. }
  1218. static long video_ioctl_upstream9(struct file *file, unsigned int cmd,
  1219. unsigned long arg)
  1220. {
  1221. struct cx25821_fh *fh = file->private_data;
  1222. struct cx25821_dev *dev = fh->dev;
  1223. int command = 0;
  1224. struct upstream_user_struct *data_from_user;
  1225. data_from_user = (struct upstream_user_struct *)arg;
  1226. if (!data_from_user) {
  1227. pr_err("%s(): Upstream data is INVALID. Returning\n", __func__);
  1228. return 0;
  1229. }
  1230. command = data_from_user->command;
  1231. if (command != UPSTREAM_START_VIDEO && command != UPSTREAM_STOP_VIDEO)
  1232. return 0;
  1233. dev->input_filename = data_from_user->input_filename;
  1234. dev->input_audiofilename = data_from_user->input_filename;
  1235. dev->vid_stdname = data_from_user->vid_stdname;
  1236. dev->pixel_format = data_from_user->pixel_format;
  1237. dev->channel_select = data_from_user->channel_select;
  1238. dev->command = data_from_user->command;
  1239. switch (command) {
  1240. case UPSTREAM_START_VIDEO:
  1241. cx25821_start_upstream_video_ch1(dev, data_from_user);
  1242. break;
  1243. case UPSTREAM_STOP_VIDEO:
  1244. cx25821_stop_upstream_video_ch1(dev);
  1245. break;
  1246. }
  1247. return 0;
  1248. }
  1249. static long video_ioctl_upstream10(struct file *file, unsigned int cmd,
  1250. unsigned long arg)
  1251. {
  1252. struct cx25821_fh *fh = file->private_data;
  1253. struct cx25821_dev *dev = fh->dev;
  1254. int command = 0;
  1255. struct upstream_user_struct *data_from_user;
  1256. data_from_user = (struct upstream_user_struct *)arg;
  1257. if (!data_from_user) {
  1258. pr_err("%s(): Upstream data is INVALID. Returning\n", __func__);
  1259. return 0;
  1260. }
  1261. command = data_from_user->command;
  1262. if (command != UPSTREAM_START_VIDEO && command != UPSTREAM_STOP_VIDEO)
  1263. return 0;
  1264. dev->input_filename_ch2 = data_from_user->input_filename;
  1265. dev->input_audiofilename = data_from_user->input_filename;
  1266. dev->vid_stdname_ch2 = data_from_user->vid_stdname;
  1267. dev->pixel_format_ch2 = data_from_user->pixel_format;
  1268. dev->channel_select_ch2 = data_from_user->channel_select;
  1269. dev->command_ch2 = data_from_user->command;
  1270. switch (command) {
  1271. case UPSTREAM_START_VIDEO:
  1272. cx25821_start_upstream_video_ch2(dev, data_from_user);
  1273. break;
  1274. case UPSTREAM_STOP_VIDEO:
  1275. cx25821_stop_upstream_video_ch2(dev);
  1276. break;
  1277. }
  1278. return 0;
  1279. }
  1280. static long video_ioctl_upstream11(struct file *file, unsigned int cmd,
  1281. unsigned long arg)
  1282. {
  1283. struct cx25821_fh *fh = file->private_data;
  1284. struct cx25821_dev *dev = fh->dev;
  1285. int command = 0;
  1286. struct upstream_user_struct *data_from_user;
  1287. data_from_user = (struct upstream_user_struct *)arg;
  1288. if (!data_from_user) {
  1289. pr_err("%s(): Upstream data is INVALID. Returning\n", __func__);
  1290. return 0;
  1291. }
  1292. command = data_from_user->command;
  1293. if (command != UPSTREAM_START_AUDIO && command != UPSTREAM_STOP_AUDIO)
  1294. return 0;
  1295. dev->input_filename = data_from_user->input_filename;
  1296. dev->input_audiofilename = data_from_user->input_filename;
  1297. dev->vid_stdname = data_from_user->vid_stdname;
  1298. dev->pixel_format = data_from_user->pixel_format;
  1299. dev->channel_select = data_from_user->channel_select;
  1300. dev->command = data_from_user->command;
  1301. switch (command) {
  1302. case UPSTREAM_START_AUDIO:
  1303. cx25821_start_upstream_audio(dev, data_from_user);
  1304. break;
  1305. case UPSTREAM_STOP_AUDIO:
  1306. cx25821_stop_upstream_audio(dev);
  1307. break;
  1308. }
  1309. return 0;
  1310. }
  1311. static long video_ioctl_set(struct file *file, unsigned int cmd,
  1312. unsigned long arg)
  1313. {
  1314. struct cx25821_fh *fh = file->private_data;
  1315. struct cx25821_dev *dev = fh->dev;
  1316. struct downstream_user_struct *data_from_user;
  1317. int command;
  1318. int width = 720;
  1319. int selected_channel = 0;
  1320. int pix_format = 0;
  1321. int i = 0;
  1322. int cif_enable = 0;
  1323. int cif_width = 0;
  1324. data_from_user = (struct downstream_user_struct *)arg;
  1325. if (!data_from_user) {
  1326. pr_err("%s(): User data is INVALID. Returning\n", __func__);
  1327. return 0;
  1328. }
  1329. command = data_from_user->command;
  1330. if (command != SET_VIDEO_STD && command != SET_PIXEL_FORMAT
  1331. && command != ENABLE_CIF_RESOLUTION && command != REG_READ
  1332. && command != REG_WRITE && command != MEDUSA_READ
  1333. && command != MEDUSA_WRITE) {
  1334. return 0;
  1335. }
  1336. switch (command) {
  1337. case SET_VIDEO_STD:
  1338. if (!strcmp(data_from_user->vid_stdname, "PAL"))
  1339. dev->tvnorm = V4L2_STD_PAL_BG;
  1340. else
  1341. dev->tvnorm = V4L2_STD_NTSC_M;
  1342. medusa_set_videostandard(dev);
  1343. break;
  1344. case SET_PIXEL_FORMAT:
  1345. selected_channel = data_from_user->decoder_select;
  1346. pix_format = data_from_user->pixel_format;
  1347. if (!(selected_channel <= 7 && selected_channel >= 0)) {
  1348. selected_channel -= 4;
  1349. selected_channel = selected_channel % 8;
  1350. }
  1351. if (selected_channel >= 0)
  1352. cx25821_set_pixel_format(dev, selected_channel,
  1353. pix_format);
  1354. break;
  1355. case ENABLE_CIF_RESOLUTION:
  1356. selected_channel = data_from_user->decoder_select;
  1357. cif_enable = data_from_user->cif_resolution_enable;
  1358. cif_width = data_from_user->cif_width;
  1359. if (cif_enable) {
  1360. if (dev->tvnorm & V4L2_STD_PAL_BG
  1361. || dev->tvnorm & V4L2_STD_PAL_DK) {
  1362. width = 352;
  1363. } else {
  1364. width = cif_width;
  1365. if (cif_width != 320 && cif_width != 352)
  1366. width = 320;
  1367. }
  1368. }
  1369. if (!(selected_channel <= 7 && selected_channel >= 0)) {
  1370. selected_channel -= 4;
  1371. selected_channel = selected_channel % 8;
  1372. }
  1373. if (selected_channel <= 7 && selected_channel >= 0) {
  1374. dev->channels[selected_channel].use_cif_resolution =
  1375. cif_enable;
  1376. dev->channels[selected_channel].cif_width = width;
  1377. } else {
  1378. for (i = 0; i < VID_CHANNEL_NUM; i++) {
  1379. dev->channels[i].use_cif_resolution =
  1380. cif_enable;
  1381. dev->channels[i].cif_width = width;
  1382. }
  1383. }
  1384. medusa_set_resolution(dev, width, selected_channel);
  1385. break;
  1386. case REG_READ:
  1387. data_from_user->reg_data = cx_read(data_from_user->reg_address);
  1388. break;
  1389. case REG_WRITE:
  1390. cx_write(data_from_user->reg_address, data_from_user->reg_data);
  1391. break;
  1392. case MEDUSA_READ:
  1393. cx25821_i2c_read(&dev->i2c_bus[0],
  1394. (u16) data_from_user->reg_address,
  1395. &data_from_user->reg_data);
  1396. break;
  1397. case MEDUSA_WRITE:
  1398. cx25821_i2c_write(&dev->i2c_bus[0],
  1399. (u16) data_from_user->reg_address,
  1400. data_from_user->reg_data);
  1401. break;
  1402. }
  1403. return 0;
  1404. }
  1405. static long cx25821_video_ioctl(struct file *file,
  1406. unsigned int cmd, unsigned long arg)
  1407. {
  1408. int ret = 0;
  1409. struct cx25821_fh *fh = file->private_data;
  1410. /* check to see if it's the video upstream */
  1411. if (fh->channel_id == SRAM_CH09) {
  1412. ret = video_ioctl_upstream9(file, cmd, arg);
  1413. return ret;
  1414. } else if (fh->channel_id == SRAM_CH10) {
  1415. ret = video_ioctl_upstream10(file, cmd, arg);
  1416. return ret;
  1417. } else if (fh->channel_id == SRAM_CH11) {
  1418. ret = video_ioctl_upstream11(file, cmd, arg);
  1419. ret = video_ioctl_set(file, cmd, arg);
  1420. return ret;
  1421. }
  1422. return video_ioctl2(file, cmd, arg);
  1423. }
  1424. /* exported stuff */
  1425. static const struct v4l2_file_operations video_fops = {
  1426. .owner = THIS_MODULE,
  1427. .open = video_open,
  1428. .release = video_release,
  1429. .read = video_read,
  1430. .poll = video_poll,
  1431. .mmap = cx25821_video_mmap,
  1432. .ioctl = cx25821_video_ioctl,
  1433. };
  1434. static const struct v4l2_ioctl_ops video_ioctl_ops = {
  1435. .vidioc_querycap = cx25821_vidioc_querycap,
  1436. .vidioc_enum_fmt_vid_cap = cx25821_vidioc_enum_fmt_vid_cap,
  1437. .vidioc_g_fmt_vid_cap = cx25821_vidioc_g_fmt_vid_cap,
  1438. .vidioc_try_fmt_vid_cap = cx25821_vidioc_try_fmt_vid_cap,
  1439. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1440. .vidioc_reqbufs = cx25821_vidioc_reqbufs,
  1441. .vidioc_querybuf = cx25821_vidioc_querybuf,
  1442. .vidioc_qbuf = cx25821_vidioc_qbuf,
  1443. .vidioc_dqbuf = vidioc_dqbuf,
  1444. .vidioc_g_std = cx25821_vidioc_g_std,
  1445. .vidioc_s_std = cx25821_vidioc_s_std,
  1446. .vidioc_cropcap = cx25821_vidioc_cropcap,
  1447. .vidioc_s_crop = cx25821_vidioc_s_crop,
  1448. .vidioc_g_crop = cx25821_vidioc_g_crop,
  1449. .vidioc_enum_input = cx25821_vidioc_enum_input,
  1450. .vidioc_g_input = cx25821_vidioc_g_input,
  1451. .vidioc_s_input = cx25821_vidioc_s_input,
  1452. .vidioc_g_ctrl = cx25821_vidioc_g_ctrl,
  1453. .vidioc_s_ctrl = vidioc_s_ctrl,
  1454. .vidioc_queryctrl = cx25821_vidioc_queryctrl,
  1455. .vidioc_streamon = vidioc_streamon,
  1456. .vidioc_streamoff = vidioc_streamoff,
  1457. .vidioc_log_status = vidioc_log_status,
  1458. .vidioc_g_priority = cx25821_vidioc_g_priority,
  1459. .vidioc_s_priority = cx25821_vidioc_s_priority,
  1460. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1461. .vidioc_g_register = cx25821_vidioc_g_register,
  1462. .vidioc_s_register = cx25821_vidioc_s_register,
  1463. #endif
  1464. };
  1465. static const struct video_device cx25821_video_device = {
  1466. .name = "cx25821-video",
  1467. .fops = &video_fops,
  1468. .minor = -1,
  1469. .ioctl_ops = &video_ioctl_ops,
  1470. .tvnorms = CX25821_NORMS,
  1471. };
  1472. void cx25821_video_unregister(struct cx25821_dev *dev, int chan_num)
  1473. {
  1474. cx_clear(PCI_INT_MSK, 1);
  1475. if (dev->channels[chan_num].video_dev) {
  1476. if (video_is_registered(dev->channels[chan_num].video_dev))
  1477. video_unregister_device(
  1478. dev->channels[chan_num].video_dev);
  1479. else
  1480. video_device_release(
  1481. dev->channels[chan_num].video_dev);
  1482. dev->channels[chan_num].video_dev = NULL;
  1483. btcx_riscmem_free(dev->pci,
  1484. &dev->channels[chan_num].vidq.stopper);
  1485. pr_warn("device %d released!\n", chan_num);
  1486. }
  1487. }
  1488. int cx25821_video_register(struct cx25821_dev *dev)
  1489. {
  1490. int err;
  1491. int i;
  1492. spin_lock_init(&dev->slock);
  1493. for (i = 0; i < VID_CHANNEL_NUM; ++i) {
  1494. if (i == SRAM_CH08) /* audio channel */
  1495. continue;
  1496. cx25821_init_controls(dev, i);
  1497. cx25821_risc_stopper(dev->pci, &dev->channels[i].vidq.stopper,
  1498. dev->channels[i].sram_channels->dma_ctl, 0x11, 0);
  1499. dev->channels[i].sram_channels = &cx25821_sram_channels[i];
  1500. dev->channels[i].video_dev = NULL;
  1501. dev->channels[i].resources = 0;
  1502. cx_write(dev->channels[i].sram_channels->int_stat, 0xffffffff);
  1503. INIT_LIST_HEAD(&dev->channels[i].vidq.active);
  1504. INIT_LIST_HEAD(&dev->channels[i].vidq.queued);
  1505. dev->channels[i].timeout_data.dev = dev;
  1506. dev->channels[i].timeout_data.channel =
  1507. &cx25821_sram_channels[i];
  1508. dev->channels[i].vidq.timeout.function = cx25821_vid_timeout;
  1509. dev->channels[i].vidq.timeout.data =
  1510. (unsigned long)&dev->channels[i].timeout_data;
  1511. init_timer(&dev->channels[i].vidq.timeout);
  1512. /* register v4l devices */
  1513. dev->channels[i].video_dev = cx25821_vdev_init(dev, dev->pci,
  1514. &cx25821_video_device, "video");
  1515. err = video_register_device(dev->channels[i].video_dev,
  1516. VFL_TYPE_GRABBER, video_nr[dev->nr]);
  1517. if (err < 0)
  1518. goto fail_unreg;
  1519. }
  1520. /* set PCI interrupt */
  1521. cx_set(PCI_INT_MSK, 0xff);
  1522. /* initial device configuration */
  1523. mutex_lock(&dev->lock);
  1524. dev->tvnorm = V4L2_STD_NTSC_M,
  1525. cx25821_set_tvnorm(dev, dev->tvnorm);
  1526. mutex_unlock(&dev->lock);
  1527. return 0;
  1528. fail_unreg:
  1529. cx25821_video_unregister(dev, i);
  1530. return err;
  1531. }