cx25821-video.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857
  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. 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. 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. 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. 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. /* stop the risc engine and fifo */
  659. cx_write(channel0->dma_ctl, 0); /* FIFO and RISC disable */
  660. /* stop video capture */
  661. if (cx25821_res_check(fh, RESOURCE_VIDEO0)) {
  662. videobuf_queue_cancel(&fh->vidq);
  663. cx25821_res_free(dev, fh, RESOURCE_VIDEO0);
  664. }
  665. if (fh->vidq.read_buf) {
  666. cx25821_buffer_release(&fh->vidq, fh->vidq.read_buf);
  667. kfree(fh->vidq.read_buf);
  668. }
  669. videobuf_mmap_free(&fh->vidq);
  670. v4l2_prio_close(&dev->channels[fh->channel_id].prio, fh->prio);
  671. file->private_data = NULL;
  672. kfree(fh);
  673. return 0;
  674. }
  675. static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
  676. {
  677. struct cx25821_fh *fh = priv;
  678. struct cx25821_dev *dev = fh->dev;
  679. if (unlikely(fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE))
  680. return -EINVAL;
  681. if (unlikely(i != fh->type))
  682. return -EINVAL;
  683. if (unlikely(!cx25821_res_get(dev, fh, cx25821_get_resource(fh,
  684. RESOURCE_VIDEO0))))
  685. return -EBUSY;
  686. return videobuf_streamon(get_queue(fh));
  687. }
  688. static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
  689. {
  690. struct cx25821_fh *fh = priv;
  691. struct cx25821_dev *dev = fh->dev;
  692. int err, res;
  693. if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  694. return -EINVAL;
  695. if (i != fh->type)
  696. return -EINVAL;
  697. res = cx25821_get_resource(fh, RESOURCE_VIDEO0);
  698. err = videobuf_streamoff(get_queue(fh));
  699. if (err < 0)
  700. return err;
  701. cx25821_res_free(dev, fh, res);
  702. return 0;
  703. }
  704. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  705. struct v4l2_format *f)
  706. {
  707. struct cx25821_fh *fh = priv;
  708. struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
  709. struct v4l2_mbus_framefmt mbus_fmt;
  710. int err;
  711. int pix_format = PIXEL_FRMT_422;
  712. if (fh) {
  713. err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
  714. fh->prio);
  715. if (0 != err)
  716. return err;
  717. }
  718. dprintk(2, "%s()\n", __func__);
  719. err = cx25821_vidioc_try_fmt_vid_cap(file, priv, f);
  720. if (0 != err)
  721. return err;
  722. fh->fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
  723. fh->vidq.field = f->fmt.pix.field;
  724. /* check if width and height is valid based on set standard */
  725. if (cx25821_is_valid_width(f->fmt.pix.width, dev->tvnorm))
  726. fh->width = f->fmt.pix.width;
  727. if (cx25821_is_valid_height(f->fmt.pix.height, dev->tvnorm))
  728. fh->height = f->fmt.pix.height;
  729. if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_Y41P)
  730. pix_format = PIXEL_FRMT_411;
  731. else if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_YUYV)
  732. pix_format = PIXEL_FRMT_422;
  733. else
  734. return -EINVAL;
  735. cx25821_set_pixel_format(dev, SRAM_CH00, pix_format);
  736. /* check if cif resolution */
  737. if (fh->width == 320 || fh->width == 352)
  738. dev->channels[fh->channel_id].use_cif_resolution = 1;
  739. else
  740. dev->channels[fh->channel_id].use_cif_resolution = 0;
  741. dev->channels[fh->channel_id].cif_width = fh->width;
  742. medusa_set_resolution(dev, fh->width, SRAM_CH00);
  743. dprintk(2, "%s(): width=%d height=%d field=%d\n", __func__, fh->width,
  744. fh->height, fh->vidq.field);
  745. v4l2_fill_mbus_format(&mbus_fmt, &f->fmt.pix, V4L2_MBUS_FMT_FIXED);
  746. cx25821_call_all(dev, video, s_mbus_fmt, &mbus_fmt);
  747. return 0;
  748. }
  749. static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
  750. {
  751. int ret_val = 0;
  752. struct cx25821_fh *fh = priv;
  753. struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
  754. ret_val = videobuf_dqbuf(get_queue(fh), p, file->f_flags & O_NONBLOCK);
  755. p->sequence = dev->channels[fh->channel_id].vidq.count;
  756. return ret_val;
  757. }
  758. static int vidioc_log_status(struct file *file, void *priv)
  759. {
  760. struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
  761. struct cx25821_fh *fh = priv;
  762. char name[32 + 2];
  763. struct sram_channel *sram_ch = dev->channels[fh->channel_id]
  764. .sram_channels;
  765. u32 tmp = 0;
  766. snprintf(name, sizeof(name), "%s/2", dev->name);
  767. pr_info("%s/2: ============ START LOG STATUS ============\n",
  768. dev->name);
  769. cx25821_call_all(dev, core, log_status);
  770. tmp = cx_read(sram_ch->dma_ctl);
  771. pr_info("Video input 0 is %s\n",
  772. (tmp & 0x11) ? "streaming" : "stopped");
  773. pr_info("%s/2: ============= END LOG STATUS =============\n",
  774. dev->name);
  775. return 0;
  776. }
  777. static int vidioc_s_ctrl(struct file *file, void *priv,
  778. struct v4l2_control *ctl)
  779. {
  780. struct cx25821_fh *fh = priv;
  781. struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
  782. int err;
  783. if (fh) {
  784. err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
  785. fh->prio);
  786. if (0 != err)
  787. return err;
  788. }
  789. return cx25821_set_control(dev, ctl, fh->channel_id);
  790. }
  791. /* VIDEO IOCTLS */
  792. int cx25821_vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  793. struct v4l2_format *f)
  794. {
  795. struct cx25821_fh *fh = priv;
  796. f->fmt.pix.width = fh->width;
  797. f->fmt.pix.height = fh->height;
  798. f->fmt.pix.field = fh->vidq.field;
  799. f->fmt.pix.pixelformat = fh->fmt->fourcc;
  800. f->fmt.pix.bytesperline = (f->fmt.pix.width * fh->fmt->depth) >> 3;
  801. f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
  802. return 0;
  803. }
  804. int cx25821_vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  805. struct v4l2_format *f)
  806. {
  807. struct cx25821_fmt *fmt;
  808. enum v4l2_field field;
  809. unsigned int maxw, maxh;
  810. fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
  811. if (NULL == fmt)
  812. return -EINVAL;
  813. field = f->fmt.pix.field;
  814. maxw = 720;
  815. maxh = 576;
  816. if (V4L2_FIELD_ANY == field) {
  817. if (f->fmt.pix.height > maxh / 2)
  818. field = V4L2_FIELD_INTERLACED;
  819. else
  820. field = V4L2_FIELD_TOP;
  821. }
  822. switch (field) {
  823. case V4L2_FIELD_TOP:
  824. case V4L2_FIELD_BOTTOM:
  825. maxh = maxh / 2;
  826. break;
  827. case V4L2_FIELD_INTERLACED:
  828. break;
  829. default:
  830. return -EINVAL;
  831. }
  832. f->fmt.pix.field = field;
  833. if (f->fmt.pix.height < 32)
  834. f->fmt.pix.height = 32;
  835. if (f->fmt.pix.height > maxh)
  836. f->fmt.pix.height = maxh;
  837. if (f->fmt.pix.width < 48)
  838. f->fmt.pix.width = 48;
  839. if (f->fmt.pix.width > maxw)
  840. f->fmt.pix.width = maxw;
  841. f->fmt.pix.width &= ~0x03;
  842. f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
  843. f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
  844. return 0;
  845. }
  846. int cx25821_vidioc_querycap(struct file *file, void *priv,
  847. struct v4l2_capability *cap)
  848. {
  849. struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
  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. cap->version = CX25821_VERSION_CODE;
  854. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
  855. V4L2_CAP_STREAMING;
  856. return 0;
  857. }
  858. int cx25821_vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  859. struct v4l2_fmtdesc *f)
  860. {
  861. if (unlikely(f->index >= ARRAY_SIZE(formats)))
  862. return -EINVAL;
  863. strlcpy(f->description, formats[f->index].name, sizeof(f->description));
  864. f->pixelformat = formats[f->index].fourcc;
  865. return 0;
  866. }
  867. int cx25821_vidioc_reqbufs(struct file *file, void *priv,
  868. struct v4l2_requestbuffers *p)
  869. {
  870. struct cx25821_fh *fh = priv;
  871. return videobuf_reqbufs(get_queue(fh), p);
  872. }
  873. int cx25821_vidioc_querybuf(struct file *file, void *priv,
  874. struct v4l2_buffer *p)
  875. {
  876. struct cx25821_fh *fh = priv;
  877. return videobuf_querybuf(get_queue(fh), p);
  878. }
  879. int cx25821_vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
  880. {
  881. struct cx25821_fh *fh = priv;
  882. return videobuf_qbuf(get_queue(fh), p);
  883. }
  884. int cx25821_vidioc_g_priority(struct file *file, void *f, enum v4l2_priority *p)
  885. {
  886. struct cx25821_dev *dev = ((struct cx25821_fh *)f)->dev;
  887. struct cx25821_fh *fh = f;
  888. *p = v4l2_prio_max(&dev->channels[fh->channel_id].prio);
  889. return 0;
  890. }
  891. int cx25821_vidioc_s_priority(struct file *file, void *f,
  892. enum v4l2_priority prio)
  893. {
  894. struct cx25821_fh *fh = f;
  895. struct cx25821_dev *dev = ((struct cx25821_fh *)f)->dev;
  896. return v4l2_prio_change(&dev->channels[fh->channel_id].prio, &fh->prio,
  897. prio);
  898. }
  899. int cx25821_vidioc_g_std(struct file *file, void *priv, v4l2_std_id *tvnorms)
  900. {
  901. struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
  902. *tvnorms = dev->tvnorm;
  903. return 0;
  904. }
  905. int cx25821_vidioc_s_std(struct file *file, void *priv, v4l2_std_id tvnorms)
  906. {
  907. struct cx25821_fh *fh = priv;
  908. struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
  909. int err;
  910. dprintk(1, "%s()\n", __func__);
  911. if (fh) {
  912. err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
  913. fh->prio);
  914. if (0 != err)
  915. return err;
  916. }
  917. if (dev->tvnorm == tvnorms)
  918. return 0;
  919. mutex_lock(&dev->lock);
  920. cx25821_set_tvnorm(dev, tvnorms);
  921. mutex_unlock(&dev->lock);
  922. medusa_set_videostandard(dev);
  923. return 0;
  924. }
  925. int cx25821_enum_input(struct cx25821_dev *dev, struct v4l2_input *i)
  926. {
  927. static const char * const iname[] = {
  928. [CX25821_VMUX_COMPOSITE] = "Composite",
  929. [CX25821_VMUX_SVIDEO] = "S-Video",
  930. [CX25821_VMUX_DEBUG] = "for debug only",
  931. };
  932. unsigned int n;
  933. dprintk(1, "%s()\n", __func__);
  934. n = i->index;
  935. if (n >= 2)
  936. return -EINVAL;
  937. if (0 == INPUT(n)->type)
  938. return -EINVAL;
  939. i->type = V4L2_INPUT_TYPE_CAMERA;
  940. strcpy(i->name, iname[INPUT(n)->type]);
  941. i->std = CX25821_NORMS;
  942. return 0;
  943. }
  944. int cx25821_vidioc_enum_input(struct file *file, void *priv,
  945. struct v4l2_input *i)
  946. {
  947. struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
  948. dprintk(1, "%s()\n", __func__);
  949. return cx25821_enum_input(dev, i);
  950. }
  951. int cx25821_vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  952. {
  953. struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
  954. *i = dev->input;
  955. dprintk(1, "%s(): returns %d\n", __func__, *i);
  956. return 0;
  957. }
  958. int cx25821_vidioc_s_input(struct file *file, void *priv, unsigned int i)
  959. {
  960. struct cx25821_fh *fh = priv;
  961. struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
  962. int err;
  963. dprintk(1, "%s(%d)\n", __func__, i);
  964. if (fh) {
  965. err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
  966. fh->prio);
  967. if (0 != err)
  968. return err;
  969. }
  970. if (i >= CX25821_NR_INPUT) {
  971. dprintk(1, "%s(): -EINVAL\n", __func__);
  972. return -EINVAL;
  973. }
  974. mutex_lock(&dev->lock);
  975. cx25821_video_mux(dev, i);
  976. mutex_unlock(&dev->lock);
  977. return 0;
  978. }
  979. #ifdef CONFIG_VIDEO_ADV_DEBUG
  980. int cx25821_vidioc_g_register(struct file *file, void *fh,
  981. struct v4l2_dbg_register *reg)
  982. {
  983. struct cx25821_dev *dev = ((struct cx25821_fh *)fh)->dev;
  984. if (!v4l2_chip_match_host(&reg->match))
  985. return -EINVAL;
  986. cx25821_call_all(dev, core, g_register, reg);
  987. return 0;
  988. }
  989. int cx25821_vidioc_s_register(struct file *file, void *fh,
  990. const struct v4l2_dbg_register *reg)
  991. {
  992. struct cx25821_dev *dev = ((struct cx25821_fh *)fh)->dev;
  993. if (!v4l2_chip_match_host(&reg->match))
  994. return -EINVAL;
  995. cx25821_call_all(dev, core, s_register, reg);
  996. return 0;
  997. }
  998. #endif
  999. /*****************************************************************************/
  1000. static const struct v4l2_queryctrl no_ctl = {
  1001. .name = "42",
  1002. .flags = V4L2_CTRL_FLAG_DISABLED,
  1003. };
  1004. static struct v4l2_queryctrl cx25821_ctls[] = {
  1005. /* --- video --- */
  1006. {
  1007. .id = V4L2_CID_BRIGHTNESS,
  1008. .name = "Brightness",
  1009. .minimum = 0,
  1010. .maximum = 10000,
  1011. .step = 1,
  1012. .default_value = 6200,
  1013. .type = V4L2_CTRL_TYPE_INTEGER,
  1014. }, {
  1015. .id = V4L2_CID_CONTRAST,
  1016. .name = "Contrast",
  1017. .minimum = 0,
  1018. .maximum = 10000,
  1019. .step = 1,
  1020. .default_value = 5000,
  1021. .type = V4L2_CTRL_TYPE_INTEGER,
  1022. }, {
  1023. .id = V4L2_CID_SATURATION,
  1024. .name = "Saturation",
  1025. .minimum = 0,
  1026. .maximum = 10000,
  1027. .step = 1,
  1028. .default_value = 5000,
  1029. .type = V4L2_CTRL_TYPE_INTEGER,
  1030. }, {
  1031. .id = V4L2_CID_HUE,
  1032. .name = "Hue",
  1033. .minimum = 0,
  1034. .maximum = 10000,
  1035. .step = 1,
  1036. .default_value = 5000,
  1037. .type = V4L2_CTRL_TYPE_INTEGER,
  1038. }
  1039. };
  1040. static const int CX25821_CTLS = ARRAY_SIZE(cx25821_ctls);
  1041. static int cx25821_ctrl_query(struct v4l2_queryctrl *qctrl)
  1042. {
  1043. int i;
  1044. if (qctrl->id < V4L2_CID_BASE || qctrl->id >= V4L2_CID_LASTP1)
  1045. return -EINVAL;
  1046. for (i = 0; i < CX25821_CTLS; i++)
  1047. if (cx25821_ctls[i].id == qctrl->id)
  1048. break;
  1049. if (i == CX25821_CTLS) {
  1050. *qctrl = no_ctl;
  1051. return 0;
  1052. }
  1053. *qctrl = cx25821_ctls[i];
  1054. return 0;
  1055. }
  1056. int cx25821_vidioc_queryctrl(struct file *file, void *priv,
  1057. struct v4l2_queryctrl *qctrl)
  1058. {
  1059. return cx25821_ctrl_query(qctrl);
  1060. }
  1061. /* ------------------------------------------------------------------ */
  1062. /* VIDEO CTRL IOCTLS */
  1063. static const struct v4l2_queryctrl *ctrl_by_id(unsigned int id)
  1064. {
  1065. unsigned int i;
  1066. for (i = 0; i < CX25821_CTLS; i++)
  1067. if (cx25821_ctls[i].id == id)
  1068. return cx25821_ctls + i;
  1069. return NULL;
  1070. }
  1071. int cx25821_vidioc_g_ctrl(struct file *file, void *priv,
  1072. struct v4l2_control *ctl)
  1073. {
  1074. struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
  1075. struct cx25821_fh *fh = priv;
  1076. const struct v4l2_queryctrl *ctrl;
  1077. ctrl = ctrl_by_id(ctl->id);
  1078. if (NULL == ctrl)
  1079. return -EINVAL;
  1080. switch (ctl->id) {
  1081. case V4L2_CID_BRIGHTNESS:
  1082. ctl->value = dev->channels[fh->channel_id].ctl_bright;
  1083. break;
  1084. case V4L2_CID_HUE:
  1085. ctl->value = dev->channels[fh->channel_id].ctl_hue;
  1086. break;
  1087. case V4L2_CID_CONTRAST:
  1088. ctl->value = dev->channels[fh->channel_id].ctl_contrast;
  1089. break;
  1090. case V4L2_CID_SATURATION:
  1091. ctl->value = dev->channels[fh->channel_id].ctl_saturation;
  1092. break;
  1093. }
  1094. return 0;
  1095. }
  1096. int cx25821_set_control(struct cx25821_dev *dev,
  1097. struct v4l2_control *ctl, int chan_num)
  1098. {
  1099. int err;
  1100. const struct v4l2_queryctrl *ctrl;
  1101. err = -EINVAL;
  1102. ctrl = ctrl_by_id(ctl->id);
  1103. if (NULL == ctrl)
  1104. return err;
  1105. switch (ctrl->type) {
  1106. case V4L2_CTRL_TYPE_BOOLEAN:
  1107. case V4L2_CTRL_TYPE_MENU:
  1108. case V4L2_CTRL_TYPE_INTEGER:
  1109. if (ctl->value < ctrl->minimum)
  1110. ctl->value = ctrl->minimum;
  1111. if (ctl->value > ctrl->maximum)
  1112. ctl->value = ctrl->maximum;
  1113. break;
  1114. default:
  1115. /* nothing */ ;
  1116. }
  1117. switch (ctl->id) {
  1118. case V4L2_CID_BRIGHTNESS:
  1119. dev->channels[chan_num].ctl_bright = ctl->value;
  1120. medusa_set_brightness(dev, ctl->value, chan_num);
  1121. break;
  1122. case V4L2_CID_HUE:
  1123. dev->channels[chan_num].ctl_hue = ctl->value;
  1124. medusa_set_hue(dev, ctl->value, chan_num);
  1125. break;
  1126. case V4L2_CID_CONTRAST:
  1127. dev->channels[chan_num].ctl_contrast = ctl->value;
  1128. medusa_set_contrast(dev, ctl->value, chan_num);
  1129. break;
  1130. case V4L2_CID_SATURATION:
  1131. dev->channels[chan_num].ctl_saturation = ctl->value;
  1132. medusa_set_saturation(dev, ctl->value, chan_num);
  1133. break;
  1134. }
  1135. err = 0;
  1136. return err;
  1137. }
  1138. static void cx25821_init_controls(struct cx25821_dev *dev, int chan_num)
  1139. {
  1140. struct v4l2_control ctrl;
  1141. int i;
  1142. for (i = 0; i < CX25821_CTLS; i++) {
  1143. ctrl.id = cx25821_ctls[i].id;
  1144. ctrl.value = cx25821_ctls[i].default_value;
  1145. cx25821_set_control(dev, &ctrl, chan_num);
  1146. }
  1147. }
  1148. int cx25821_vidioc_cropcap(struct file *file, void *priv,
  1149. struct v4l2_cropcap *cropcap)
  1150. {
  1151. struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
  1152. if (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1153. return -EINVAL;
  1154. cropcap->bounds.top = 0;
  1155. cropcap->bounds.left = 0;
  1156. cropcap->bounds.width = 720;
  1157. cropcap->bounds.height = dev->tvnorm == V4L2_STD_PAL_BG ? 576 : 480;
  1158. cropcap->pixelaspect.numerator =
  1159. dev->tvnorm == V4L2_STD_PAL_BG ? 59 : 10;
  1160. cropcap->pixelaspect.denominator =
  1161. dev->tvnorm == V4L2_STD_PAL_BG ? 54 : 11;
  1162. cropcap->defrect = cropcap->bounds;
  1163. return 0;
  1164. }
  1165. int cx25821_vidioc_s_crop(struct file *file, void *priv, const struct v4l2_crop *crop)
  1166. {
  1167. struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
  1168. struct cx25821_fh *fh = priv;
  1169. int err;
  1170. if (fh) {
  1171. err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
  1172. fh->prio);
  1173. if (0 != err)
  1174. return err;
  1175. }
  1176. /* cx25821_vidioc_s_crop not supported */
  1177. return -EINVAL;
  1178. }
  1179. int cx25821_vidioc_g_crop(struct file *file, void *priv, struct v4l2_crop *crop)
  1180. {
  1181. /* cx25821_vidioc_g_crop not supported */
  1182. return -EINVAL;
  1183. }
  1184. int cx25821_is_valid_width(u32 width, v4l2_std_id tvnorm)
  1185. {
  1186. if (tvnorm == V4L2_STD_PAL_BG) {
  1187. if (width == 352 || width == 720)
  1188. return 1;
  1189. else
  1190. return 0;
  1191. }
  1192. if (tvnorm == V4L2_STD_NTSC_M) {
  1193. if (width == 320 || width == 352 || width == 720)
  1194. return 1;
  1195. else
  1196. return 0;
  1197. }
  1198. return 0;
  1199. }
  1200. int cx25821_is_valid_height(u32 height, v4l2_std_id tvnorm)
  1201. {
  1202. if (tvnorm == V4L2_STD_PAL_BG) {
  1203. if (height == 576 || height == 288)
  1204. return 1;
  1205. else
  1206. return 0;
  1207. }
  1208. if (tvnorm == V4L2_STD_NTSC_M) {
  1209. if (height == 480 || height == 240)
  1210. return 1;
  1211. else
  1212. return 0;
  1213. }
  1214. return 0;
  1215. }
  1216. static long video_ioctl_upstream9(struct file *file, unsigned int cmd,
  1217. unsigned long arg)
  1218. {
  1219. struct cx25821_fh *fh = file->private_data;
  1220. struct cx25821_dev *dev = fh->dev;
  1221. int command = 0;
  1222. struct upstream_user_struct *data_from_user;
  1223. data_from_user = (struct upstream_user_struct *)arg;
  1224. if (!data_from_user) {
  1225. pr_err("%s(): Upstream data is INVALID. Returning\n", __func__);
  1226. return 0;
  1227. }
  1228. command = data_from_user->command;
  1229. if (command != UPSTREAM_START_VIDEO && command != UPSTREAM_STOP_VIDEO)
  1230. return 0;
  1231. dev->input_filename = data_from_user->input_filename;
  1232. dev->input_audiofilename = data_from_user->input_filename;
  1233. dev->vid_stdname = data_from_user->vid_stdname;
  1234. dev->pixel_format = data_from_user->pixel_format;
  1235. dev->channel_select = data_from_user->channel_select;
  1236. dev->command = data_from_user->command;
  1237. switch (command) {
  1238. case UPSTREAM_START_VIDEO:
  1239. cx25821_start_upstream_video_ch1(dev, data_from_user);
  1240. break;
  1241. case UPSTREAM_STOP_VIDEO:
  1242. cx25821_stop_upstream_video_ch1(dev);
  1243. break;
  1244. }
  1245. return 0;
  1246. }
  1247. static long video_ioctl_upstream10(struct file *file, unsigned int cmd,
  1248. unsigned long arg)
  1249. {
  1250. struct cx25821_fh *fh = file->private_data;
  1251. struct cx25821_dev *dev = fh->dev;
  1252. int command = 0;
  1253. struct upstream_user_struct *data_from_user;
  1254. data_from_user = (struct upstream_user_struct *)arg;
  1255. if (!data_from_user) {
  1256. pr_err("%s(): Upstream data is INVALID. Returning\n", __func__);
  1257. return 0;
  1258. }
  1259. command = data_from_user->command;
  1260. if (command != UPSTREAM_START_VIDEO && command != UPSTREAM_STOP_VIDEO)
  1261. return 0;
  1262. dev->input_filename_ch2 = data_from_user->input_filename;
  1263. dev->input_audiofilename = data_from_user->input_filename;
  1264. dev->vid_stdname_ch2 = data_from_user->vid_stdname;
  1265. dev->pixel_format_ch2 = data_from_user->pixel_format;
  1266. dev->channel_select_ch2 = data_from_user->channel_select;
  1267. dev->command_ch2 = data_from_user->command;
  1268. switch (command) {
  1269. case UPSTREAM_START_VIDEO:
  1270. cx25821_start_upstream_video_ch2(dev, data_from_user);
  1271. break;
  1272. case UPSTREAM_STOP_VIDEO:
  1273. cx25821_stop_upstream_video_ch2(dev);
  1274. break;
  1275. }
  1276. return 0;
  1277. }
  1278. static long video_ioctl_upstream11(struct file *file, unsigned int cmd,
  1279. unsigned long arg)
  1280. {
  1281. struct cx25821_fh *fh = file->private_data;
  1282. struct cx25821_dev *dev = fh->dev;
  1283. int command = 0;
  1284. struct upstream_user_struct *data_from_user;
  1285. data_from_user = (struct upstream_user_struct *)arg;
  1286. if (!data_from_user) {
  1287. pr_err("%s(): Upstream data is INVALID. Returning\n", __func__);
  1288. return 0;
  1289. }
  1290. command = data_from_user->command;
  1291. if (command != UPSTREAM_START_AUDIO && command != UPSTREAM_STOP_AUDIO)
  1292. return 0;
  1293. dev->input_filename = data_from_user->input_filename;
  1294. dev->input_audiofilename = data_from_user->input_filename;
  1295. dev->vid_stdname = data_from_user->vid_stdname;
  1296. dev->pixel_format = data_from_user->pixel_format;
  1297. dev->channel_select = data_from_user->channel_select;
  1298. dev->command = data_from_user->command;
  1299. switch (command) {
  1300. case UPSTREAM_START_AUDIO:
  1301. cx25821_start_upstream_audio(dev, data_from_user);
  1302. break;
  1303. case UPSTREAM_STOP_AUDIO:
  1304. cx25821_stop_upstream_audio(dev);
  1305. break;
  1306. }
  1307. return 0;
  1308. }
  1309. static long video_ioctl_set(struct file *file, unsigned int cmd,
  1310. unsigned long arg)
  1311. {
  1312. struct cx25821_fh *fh = file->private_data;
  1313. struct cx25821_dev *dev = fh->dev;
  1314. struct downstream_user_struct *data_from_user;
  1315. int command;
  1316. int width = 720;
  1317. int selected_channel = 0;
  1318. int pix_format = 0;
  1319. int i = 0;
  1320. int cif_enable = 0;
  1321. int cif_width = 0;
  1322. data_from_user = (struct downstream_user_struct *)arg;
  1323. if (!data_from_user) {
  1324. pr_err("%s(): User data is INVALID. Returning\n", __func__);
  1325. return 0;
  1326. }
  1327. command = data_from_user->command;
  1328. if (command != SET_VIDEO_STD && command != SET_PIXEL_FORMAT
  1329. && command != ENABLE_CIF_RESOLUTION && command != REG_READ
  1330. && command != REG_WRITE && command != MEDUSA_READ
  1331. && command != MEDUSA_WRITE) {
  1332. return 0;
  1333. }
  1334. switch (command) {
  1335. case SET_VIDEO_STD:
  1336. if (!strcmp(data_from_user->vid_stdname, "PAL"))
  1337. dev->tvnorm = V4L2_STD_PAL_BG;
  1338. else
  1339. dev->tvnorm = V4L2_STD_NTSC_M;
  1340. medusa_set_videostandard(dev);
  1341. break;
  1342. case SET_PIXEL_FORMAT:
  1343. selected_channel = data_from_user->decoder_select;
  1344. pix_format = data_from_user->pixel_format;
  1345. if (!(selected_channel <= 7 && selected_channel >= 0)) {
  1346. selected_channel -= 4;
  1347. selected_channel = selected_channel % 8;
  1348. }
  1349. if (selected_channel >= 0)
  1350. cx25821_set_pixel_format(dev, selected_channel,
  1351. pix_format);
  1352. break;
  1353. case ENABLE_CIF_RESOLUTION:
  1354. selected_channel = data_from_user->decoder_select;
  1355. cif_enable = data_from_user->cif_resolution_enable;
  1356. cif_width = data_from_user->cif_width;
  1357. if (cif_enable) {
  1358. if (dev->tvnorm & V4L2_STD_PAL_BG
  1359. || dev->tvnorm & V4L2_STD_PAL_DK) {
  1360. width = 352;
  1361. } else {
  1362. width = cif_width;
  1363. if (cif_width != 320 && cif_width != 352)
  1364. width = 320;
  1365. }
  1366. }
  1367. if (!(selected_channel <= 7 && selected_channel >= 0)) {
  1368. selected_channel -= 4;
  1369. selected_channel = selected_channel % 8;
  1370. }
  1371. if (selected_channel <= 7 && selected_channel >= 0) {
  1372. dev->channels[selected_channel].use_cif_resolution =
  1373. cif_enable;
  1374. dev->channels[selected_channel].cif_width = width;
  1375. } else {
  1376. for (i = 0; i < VID_CHANNEL_NUM; i++) {
  1377. dev->channels[i].use_cif_resolution =
  1378. cif_enable;
  1379. dev->channels[i].cif_width = width;
  1380. }
  1381. }
  1382. medusa_set_resolution(dev, width, selected_channel);
  1383. break;
  1384. case REG_READ:
  1385. data_from_user->reg_data = cx_read(data_from_user->reg_address);
  1386. break;
  1387. case REG_WRITE:
  1388. cx_write(data_from_user->reg_address, data_from_user->reg_data);
  1389. break;
  1390. case MEDUSA_READ:
  1391. cx25821_i2c_read(&dev->i2c_bus[0],
  1392. (u16) data_from_user->reg_address,
  1393. &data_from_user->reg_data);
  1394. break;
  1395. case MEDUSA_WRITE:
  1396. cx25821_i2c_write(&dev->i2c_bus[0],
  1397. (u16) data_from_user->reg_address,
  1398. data_from_user->reg_data);
  1399. break;
  1400. }
  1401. return 0;
  1402. }
  1403. static long cx25821_video_ioctl(struct file *file,
  1404. unsigned int cmd, unsigned long arg)
  1405. {
  1406. int ret = 0;
  1407. struct cx25821_fh *fh = file->private_data;
  1408. /* check to see if it's the video upstream */
  1409. if (fh->channel_id == SRAM_CH09) {
  1410. ret = video_ioctl_upstream9(file, cmd, arg);
  1411. return ret;
  1412. } else if (fh->channel_id == SRAM_CH10) {
  1413. ret = video_ioctl_upstream10(file, cmd, arg);
  1414. return ret;
  1415. } else if (fh->channel_id == SRAM_CH11) {
  1416. ret = video_ioctl_upstream11(file, cmd, arg);
  1417. ret = video_ioctl_set(file, cmd, arg);
  1418. return ret;
  1419. }
  1420. return video_ioctl2(file, cmd, arg);
  1421. }
  1422. /* exported stuff */
  1423. static const struct v4l2_file_operations video_fops = {
  1424. .owner = THIS_MODULE,
  1425. .open = video_open,
  1426. .release = video_release,
  1427. .read = video_read,
  1428. .poll = video_poll,
  1429. .mmap = cx25821_video_mmap,
  1430. .ioctl = cx25821_video_ioctl,
  1431. };
  1432. static const struct v4l2_ioctl_ops video_ioctl_ops = {
  1433. .vidioc_querycap = cx25821_vidioc_querycap,
  1434. .vidioc_enum_fmt_vid_cap = cx25821_vidioc_enum_fmt_vid_cap,
  1435. .vidioc_g_fmt_vid_cap = cx25821_vidioc_g_fmt_vid_cap,
  1436. .vidioc_try_fmt_vid_cap = cx25821_vidioc_try_fmt_vid_cap,
  1437. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1438. .vidioc_reqbufs = cx25821_vidioc_reqbufs,
  1439. .vidioc_querybuf = cx25821_vidioc_querybuf,
  1440. .vidioc_qbuf = cx25821_vidioc_qbuf,
  1441. .vidioc_dqbuf = vidioc_dqbuf,
  1442. .vidioc_g_std = cx25821_vidioc_g_std,
  1443. .vidioc_s_std = cx25821_vidioc_s_std,
  1444. .vidioc_cropcap = cx25821_vidioc_cropcap,
  1445. .vidioc_s_crop = cx25821_vidioc_s_crop,
  1446. .vidioc_g_crop = cx25821_vidioc_g_crop,
  1447. .vidioc_enum_input = cx25821_vidioc_enum_input,
  1448. .vidioc_g_input = cx25821_vidioc_g_input,
  1449. .vidioc_s_input = cx25821_vidioc_s_input,
  1450. .vidioc_g_ctrl = cx25821_vidioc_g_ctrl,
  1451. .vidioc_s_ctrl = vidioc_s_ctrl,
  1452. .vidioc_queryctrl = cx25821_vidioc_queryctrl,
  1453. .vidioc_streamon = vidioc_streamon,
  1454. .vidioc_streamoff = vidioc_streamoff,
  1455. .vidioc_log_status = vidioc_log_status,
  1456. .vidioc_g_priority = cx25821_vidioc_g_priority,
  1457. .vidioc_s_priority = cx25821_vidioc_s_priority,
  1458. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1459. .vidioc_g_register = cx25821_vidioc_g_register,
  1460. .vidioc_s_register = cx25821_vidioc_s_register,
  1461. #endif
  1462. };
  1463. static const struct video_device cx25821_video_device = {
  1464. .name = "cx25821-video",
  1465. .fops = &video_fops,
  1466. .minor = -1,
  1467. .ioctl_ops = &video_ioctl_ops,
  1468. .tvnorms = CX25821_NORMS,
  1469. };
  1470. void cx25821_video_unregister(struct cx25821_dev *dev, int chan_num)
  1471. {
  1472. cx_clear(PCI_INT_MSK, 1);
  1473. if (dev->channels[chan_num].video_dev) {
  1474. if (video_is_registered(dev->channels[chan_num].video_dev))
  1475. video_unregister_device(
  1476. dev->channels[chan_num].video_dev);
  1477. else
  1478. video_device_release(
  1479. dev->channels[chan_num].video_dev);
  1480. dev->channels[chan_num].video_dev = NULL;
  1481. btcx_riscmem_free(dev->pci,
  1482. &dev->channels[chan_num].vidq.stopper);
  1483. pr_warn("device %d released!\n", chan_num);
  1484. }
  1485. }
  1486. int cx25821_video_register(struct cx25821_dev *dev)
  1487. {
  1488. int err;
  1489. int i;
  1490. spin_lock_init(&dev->slock);
  1491. for (i = 0; i < VID_CHANNEL_NUM; ++i) {
  1492. if (i == SRAM_CH08) /* audio channel */
  1493. continue;
  1494. cx25821_init_controls(dev, i);
  1495. cx25821_risc_stopper(dev->pci, &dev->channels[i].vidq.stopper,
  1496. dev->channels[i].sram_channels->dma_ctl, 0x11, 0);
  1497. dev->channels[i].sram_channels = &cx25821_sram_channels[i];
  1498. dev->channels[i].video_dev = NULL;
  1499. dev->channels[i].resources = 0;
  1500. cx_write(dev->channels[i].sram_channels->int_stat, 0xffffffff);
  1501. INIT_LIST_HEAD(&dev->channels[i].vidq.active);
  1502. INIT_LIST_HEAD(&dev->channels[i].vidq.queued);
  1503. dev->channels[i].timeout_data.dev = dev;
  1504. dev->channels[i].timeout_data.channel =
  1505. &cx25821_sram_channels[i];
  1506. dev->channels[i].vidq.timeout.function = cx25821_vid_timeout;
  1507. dev->channels[i].vidq.timeout.data =
  1508. (unsigned long)&dev->channels[i].timeout_data;
  1509. init_timer(&dev->channels[i].vidq.timeout);
  1510. /* register v4l devices */
  1511. dev->channels[i].video_dev = cx25821_vdev_init(dev, dev->pci,
  1512. &cx25821_video_device, "video");
  1513. err = video_register_device(dev->channels[i].video_dev,
  1514. VFL_TYPE_GRABBER, video_nr[dev->nr]);
  1515. if (err < 0)
  1516. goto fail_unreg;
  1517. }
  1518. /* set PCI interrupt */
  1519. cx_set(PCI_INT_MSK, 0xff);
  1520. /* initial device configuration */
  1521. mutex_lock(&dev->lock);
  1522. dev->tvnorm = V4L2_STD_NTSC_M,
  1523. cx25821_set_tvnorm(dev, dev->tvnorm);
  1524. mutex_unlock(&dev->lock);
  1525. return 0;
  1526. fail_unreg:
  1527. cx25821_video_unregister(dev, i);
  1528. return err;
  1529. }