cx25821-video.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476
  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. static unsigned int vid_limit = 16;
  41. module_param(vid_limit, int, 0644);
  42. MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");
  43. #define FORMAT_FLAGS_PACKED 0x01
  44. static const struct cx25821_fmt formats[] = {
  45. {
  46. .name = "8 bpp, gray",
  47. .fourcc = V4L2_PIX_FMT_GREY,
  48. .depth = 8,
  49. .flags = FORMAT_FLAGS_PACKED,
  50. }, {
  51. .name = "4:1:1, packed, Y41P",
  52. .fourcc = V4L2_PIX_FMT_Y41P,
  53. .depth = 12,
  54. .flags = FORMAT_FLAGS_PACKED,
  55. }, {
  56. .name = "4:2:2, packed, YUYV",
  57. .fourcc = V4L2_PIX_FMT_YUYV,
  58. .depth = 16,
  59. .flags = FORMAT_FLAGS_PACKED,
  60. }, {
  61. .name = "4:2:2, packed, UYVY",
  62. .fourcc = V4L2_PIX_FMT_UYVY,
  63. .depth = 16,
  64. .flags = FORMAT_FLAGS_PACKED,
  65. }, {
  66. .name = "4:2:0, YUV",
  67. .fourcc = V4L2_PIX_FMT_YUV420,
  68. .depth = 12,
  69. .flags = FORMAT_FLAGS_PACKED,
  70. },
  71. };
  72. static const struct cx25821_fmt *cx25821_format_by_fourcc(unsigned int fourcc)
  73. {
  74. unsigned int i;
  75. if (fourcc == V4L2_PIX_FMT_Y41P || fourcc == V4L2_PIX_FMT_YUV411P)
  76. return formats + 1;
  77. for (i = 0; i < ARRAY_SIZE(formats); i++)
  78. if (formats[i].fourcc == fourcc)
  79. return formats + i;
  80. pr_err("%s(0x%08x) NOT FOUND\n", __func__, fourcc);
  81. return NULL;
  82. }
  83. void cx25821_video_wakeup(struct cx25821_dev *dev, struct cx25821_dmaqueue *q,
  84. u32 count)
  85. {
  86. struct cx25821_buffer *buf;
  87. int bc;
  88. for (bc = 0;; bc++) {
  89. if (list_empty(&q->active)) {
  90. dprintk(1, "bc=%d (=0: active empty)\n", bc);
  91. break;
  92. }
  93. buf = list_entry(q->active.next, struct cx25821_buffer,
  94. vb.queue);
  95. /* count comes from the hw and it is 16bit wide --
  96. * this trick handles wrap-arounds correctly for
  97. * up to 32767 buffers in flight... */
  98. if ((s16) (count - buf->count) < 0)
  99. break;
  100. v4l2_get_timestamp(&buf->vb.ts);
  101. buf->vb.state = VIDEOBUF_DONE;
  102. list_del(&buf->vb.queue);
  103. wake_up(&buf->vb.done);
  104. }
  105. if (list_empty(&q->active))
  106. del_timer(&q->timeout);
  107. else
  108. mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
  109. if (bc != 1)
  110. pr_err("%s: %d buffers handled (should be 1)\n", __func__, bc);
  111. }
  112. static int cx25821_set_tvnorm(struct cx25821_dev *dev, v4l2_std_id norm)
  113. {
  114. dprintk(1, "%s(norm = 0x%08x) name: [%s]\n",
  115. __func__, (unsigned int)norm, v4l2_norm_to_name(norm));
  116. dev->tvnorm = norm;
  117. /* Tell the internal A/V decoder */
  118. cx25821_call_all(dev, core, s_std, norm);
  119. return 0;
  120. }
  121. static int cx25821_video_mux(struct cx25821_dev *dev, unsigned int input)
  122. {
  123. struct v4l2_routing route;
  124. memset(&route, 0, sizeof(route));
  125. dprintk(1, "%s(): video_mux: %d [vmux=%d, gpio=0x%x,0x%x,0x%x,0x%x]\n",
  126. __func__, input, INPUT(input)->vmux, INPUT(input)->gpio0,
  127. INPUT(input)->gpio1, INPUT(input)->gpio2, INPUT(input)->gpio3);
  128. dev->input = input;
  129. route.input = INPUT(input)->vmux;
  130. /* Tell the internal A/V decoder */
  131. cx25821_call_all(dev, video, s_routing, INPUT(input)->vmux, 0, 0);
  132. return 0;
  133. }
  134. int cx25821_start_video_dma(struct cx25821_dev *dev,
  135. struct cx25821_dmaqueue *q,
  136. struct cx25821_buffer *buf,
  137. const struct sram_channel *channel)
  138. {
  139. int tmp = 0;
  140. /* setup fifo + format */
  141. cx25821_sram_channel_setup(dev, channel, buf->bpl, buf->risc.dma);
  142. /* reset counter */
  143. cx_write(channel->gpcnt_ctl, 3);
  144. q->count = 1;
  145. /* enable irq */
  146. cx_set(PCI_INT_MSK, cx_read(PCI_INT_MSK) | (1 << channel->i));
  147. cx_set(channel->int_msk, 0x11);
  148. /* start dma */
  149. cx_write(channel->dma_ctl, 0x11); /* FIFO and RISC enable */
  150. /* make sure upstream setting if any is reversed */
  151. tmp = cx_read(VID_CH_MODE_SEL);
  152. cx_write(VID_CH_MODE_SEL, tmp & 0xFFFFFE00);
  153. return 0;
  154. }
  155. static int cx25821_restart_video_queue(struct cx25821_dev *dev,
  156. struct cx25821_dmaqueue *q,
  157. const struct sram_channel *channel)
  158. {
  159. struct cx25821_buffer *buf, *prev;
  160. struct list_head *item;
  161. if (!list_empty(&q->active)) {
  162. buf = list_entry(q->active.next, struct cx25821_buffer,
  163. vb.queue);
  164. cx25821_start_video_dma(dev, q, buf, channel);
  165. list_for_each(item, &q->active) {
  166. buf = list_entry(item, struct cx25821_buffer, vb.queue);
  167. buf->count = q->count++;
  168. }
  169. mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
  170. return 0;
  171. }
  172. prev = NULL;
  173. for (;;) {
  174. if (list_empty(&q->queued))
  175. return 0;
  176. buf = list_entry(q->queued.next, struct cx25821_buffer,
  177. vb.queue);
  178. if (NULL == prev) {
  179. list_move_tail(&buf->vb.queue, &q->active);
  180. cx25821_start_video_dma(dev, q, buf, channel);
  181. buf->vb.state = VIDEOBUF_ACTIVE;
  182. buf->count = q->count++;
  183. mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
  184. } else if (prev->vb.width == buf->vb.width &&
  185. prev->vb.height == buf->vb.height &&
  186. prev->fmt == buf->fmt) {
  187. list_move_tail(&buf->vb.queue, &q->active);
  188. buf->vb.state = VIDEOBUF_ACTIVE;
  189. buf->count = q->count++;
  190. prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
  191. prev->risc.jmp[2] = cpu_to_le32(0); /* Bits 63 - 32 */
  192. } else {
  193. return 0;
  194. }
  195. prev = buf;
  196. }
  197. }
  198. static void cx25821_vid_timeout(unsigned long data)
  199. {
  200. struct cx25821_data *timeout_data = (struct cx25821_data *)data;
  201. struct cx25821_dev *dev = timeout_data->dev;
  202. const struct sram_channel *channel = timeout_data->channel;
  203. struct cx25821_dmaqueue *q = &dev->channels[channel->i].dma_vidq;
  204. struct cx25821_buffer *buf;
  205. unsigned long flags;
  206. /* cx25821_sram_channel_dump(dev, channel); */
  207. cx_clear(channel->dma_ctl, 0x11);
  208. spin_lock_irqsave(&dev->slock, flags);
  209. while (!list_empty(&q->active)) {
  210. buf = list_entry(q->active.next, struct cx25821_buffer,
  211. vb.queue);
  212. list_del(&buf->vb.queue);
  213. buf->vb.state = VIDEOBUF_ERROR;
  214. wake_up(&buf->vb.done);
  215. }
  216. cx25821_restart_video_queue(dev, q, channel);
  217. spin_unlock_irqrestore(&dev->slock, flags);
  218. }
  219. int cx25821_video_irq(struct cx25821_dev *dev, int chan_num, u32 status)
  220. {
  221. u32 count = 0;
  222. int handled = 0;
  223. u32 mask;
  224. const struct sram_channel *channel = dev->channels[chan_num].sram_channels;
  225. mask = cx_read(channel->int_msk);
  226. if (0 == (status & mask))
  227. return handled;
  228. cx_write(channel->int_stat, status);
  229. /* risc op code error */
  230. if (status & (1 << 16)) {
  231. pr_warn("%s, %s: video risc op code error\n",
  232. dev->name, channel->name);
  233. cx_clear(channel->dma_ctl, 0x11);
  234. cx25821_sram_channel_dump(dev, channel);
  235. }
  236. /* risc1 y */
  237. if (status & FLD_VID_DST_RISC1) {
  238. spin_lock(&dev->slock);
  239. count = cx_read(channel->gpcnt);
  240. cx25821_video_wakeup(dev, &dev->channels[channel->i].dma_vidq,
  241. count);
  242. spin_unlock(&dev->slock);
  243. handled++;
  244. }
  245. /* risc2 y */
  246. if (status & 0x10) {
  247. dprintk(2, "stopper video\n");
  248. spin_lock(&dev->slock);
  249. cx25821_restart_video_queue(dev,
  250. &dev->channels[channel->i].dma_vidq, channel);
  251. spin_unlock(&dev->slock);
  252. handled++;
  253. }
  254. return handled;
  255. }
  256. static int cx25821_buffer_setup(struct videobuf_queue *q, unsigned int *count,
  257. unsigned int *size)
  258. {
  259. struct cx25821_channel *chan = q->priv_data;
  260. *size = chan->fmt->depth * chan->width * chan->height >> 3;
  261. if (0 == *count)
  262. *count = 32;
  263. if (*size * *count > vid_limit * 1024 * 1024)
  264. *count = (vid_limit * 1024 * 1024) / *size;
  265. return 0;
  266. }
  267. static int cx25821_buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
  268. enum v4l2_field field)
  269. {
  270. struct cx25821_channel *chan = q->priv_data;
  271. struct cx25821_dev *dev = chan->dev;
  272. struct cx25821_buffer *buf =
  273. container_of(vb, struct cx25821_buffer, vb);
  274. int rc, init_buffer = 0;
  275. u32 line0_offset;
  276. struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
  277. int bpl_local = LINE_SIZE_D1;
  278. int channel_opened = chan->id;
  279. BUG_ON(NULL == chan->fmt);
  280. if (chan->width < 48 || chan->width > 720 ||
  281. chan->height < 32 || chan->height > 576)
  282. return -EINVAL;
  283. buf->vb.size = (chan->width * chan->height * chan->fmt->depth) >> 3;
  284. if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
  285. return -EINVAL;
  286. if (buf->fmt != chan->fmt ||
  287. buf->vb.width != chan->width ||
  288. buf->vb.height != chan->height || buf->vb.field != field) {
  289. buf->fmt = chan->fmt;
  290. buf->vb.width = chan->width;
  291. buf->vb.height = chan->height;
  292. buf->vb.field = field;
  293. init_buffer = 1;
  294. }
  295. if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
  296. init_buffer = 1;
  297. rc = videobuf_iolock(q, &buf->vb, NULL);
  298. if (0 != rc) {
  299. printk(KERN_DEBUG pr_fmt("videobuf_iolock failed!\n"));
  300. goto fail;
  301. }
  302. }
  303. dprintk(1, "init_buffer=%d\n", init_buffer);
  304. if (init_buffer) {
  305. channel_opened = dev->channel_opened;
  306. if (channel_opened < 0 || channel_opened > 7)
  307. channel_opened = 7;
  308. if (dev->channels[channel_opened].pixel_formats ==
  309. PIXEL_FRMT_411)
  310. buf->bpl = (buf->fmt->depth * buf->vb.width) >> 3;
  311. else
  312. buf->bpl = (buf->fmt->depth >> 3) * (buf->vb.width);
  313. if (dev->channels[channel_opened].pixel_formats ==
  314. PIXEL_FRMT_411) {
  315. bpl_local = buf->bpl;
  316. } else {
  317. bpl_local = buf->bpl; /* Default */
  318. if (channel_opened >= 0 && channel_opened <= 7) {
  319. if (dev->channels[channel_opened]
  320. .use_cif_resolution) {
  321. if (dev->tvnorm & V4L2_STD_PAL_BG ||
  322. dev->tvnorm & V4L2_STD_PAL_DK)
  323. bpl_local = 352 << 1;
  324. else
  325. bpl_local = dev->channels[
  326. channel_opened].
  327. cif_width << 1;
  328. }
  329. }
  330. }
  331. switch (buf->vb.field) {
  332. case V4L2_FIELD_TOP:
  333. cx25821_risc_buffer(dev->pci, &buf->risc,
  334. dma->sglist, 0, UNSET,
  335. buf->bpl, 0, buf->vb.height);
  336. break;
  337. case V4L2_FIELD_BOTTOM:
  338. cx25821_risc_buffer(dev->pci, &buf->risc,
  339. dma->sglist, UNSET, 0,
  340. buf->bpl, 0, buf->vb.height);
  341. break;
  342. case V4L2_FIELD_INTERLACED:
  343. /* All other formats are top field first */
  344. line0_offset = 0;
  345. dprintk(1, "top field first\n");
  346. cx25821_risc_buffer(dev->pci, &buf->risc,
  347. dma->sglist, line0_offset,
  348. bpl_local, bpl_local, bpl_local,
  349. buf->vb.height >> 1);
  350. break;
  351. case V4L2_FIELD_SEQ_TB:
  352. cx25821_risc_buffer(dev->pci, &buf->risc,
  353. dma->sglist,
  354. 0, buf->bpl * (buf->vb.height >> 1),
  355. buf->bpl, 0, buf->vb.height >> 1);
  356. break;
  357. case V4L2_FIELD_SEQ_BT:
  358. cx25821_risc_buffer(dev->pci, &buf->risc,
  359. dma->sglist,
  360. buf->bpl * (buf->vb.height >> 1), 0,
  361. buf->bpl, 0, buf->vb.height >> 1);
  362. break;
  363. default:
  364. BUG();
  365. }
  366. }
  367. dprintk(2, "[%p/%d] buffer_prep - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
  368. buf, buf->vb.i, chan->width, chan->height, chan->fmt->depth,
  369. chan->fmt->name, (unsigned long)buf->risc.dma);
  370. buf->vb.state = VIDEOBUF_PREPARED;
  371. return 0;
  372. fail:
  373. cx25821_free_buffer(q, buf);
  374. return rc;
  375. }
  376. static void cx25821_buffer_release(struct videobuf_queue *q,
  377. struct videobuf_buffer *vb)
  378. {
  379. struct cx25821_buffer *buf =
  380. container_of(vb, struct cx25821_buffer, vb);
  381. cx25821_free_buffer(q, buf);
  382. }
  383. static int cx25821_video_mmap(struct file *file, struct vm_area_struct *vma)
  384. {
  385. struct cx25821_channel *chan = video_drvdata(file);
  386. return videobuf_mmap_mapper(&chan->vidq, vma);
  387. }
  388. static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  389. {
  390. struct cx25821_buffer *buf =
  391. container_of(vb, struct cx25821_buffer, vb);
  392. struct cx25821_buffer *prev;
  393. struct cx25821_channel *chan = vq->priv_data;
  394. struct cx25821_dev *dev = chan->dev;
  395. struct cx25821_dmaqueue *q = &dev->channels[chan->id].dma_vidq;
  396. /* add jump to stopper */
  397. buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
  398. buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
  399. buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
  400. dprintk(2, "jmp to stopper (0x%x)\n", buf->risc.jmp[1]);
  401. if (!list_empty(&q->queued)) {
  402. list_add_tail(&buf->vb.queue, &q->queued);
  403. buf->vb.state = VIDEOBUF_QUEUED;
  404. dprintk(2, "[%p/%d] buffer_queue - append to queued\n", buf,
  405. buf->vb.i);
  406. } else if (list_empty(&q->active)) {
  407. list_add_tail(&buf->vb.queue, &q->active);
  408. cx25821_start_video_dma(dev, q, buf, chan->sram_channels);
  409. buf->vb.state = VIDEOBUF_ACTIVE;
  410. buf->count = q->count++;
  411. mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
  412. dprintk(2, "[%p/%d] buffer_queue - first active, buf cnt = %d, q->count = %d\n",
  413. buf, buf->vb.i, buf->count, q->count);
  414. } else {
  415. prev = list_entry(q->active.prev, struct cx25821_buffer,
  416. vb.queue);
  417. if (prev->vb.width == buf->vb.width
  418. && prev->vb.height == buf->vb.height
  419. && prev->fmt == buf->fmt) {
  420. list_add_tail(&buf->vb.queue, &q->active);
  421. buf->vb.state = VIDEOBUF_ACTIVE;
  422. buf->count = q->count++;
  423. prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
  424. /* 64 bit bits 63-32 */
  425. prev->risc.jmp[2] = cpu_to_le32(0);
  426. dprintk(2, "[%p/%d] buffer_queue - append to active, buf->count=%d\n",
  427. buf, buf->vb.i, buf->count);
  428. } else {
  429. list_add_tail(&buf->vb.queue, &q->queued);
  430. buf->vb.state = VIDEOBUF_QUEUED;
  431. dprintk(2, "[%p/%d] buffer_queue - first queued\n", buf,
  432. buf->vb.i);
  433. }
  434. }
  435. if (list_empty(&q->active))
  436. dprintk(2, "active queue empty!\n");
  437. }
  438. static struct videobuf_queue_ops cx25821_video_qops = {
  439. .buf_setup = cx25821_buffer_setup,
  440. .buf_prepare = cx25821_buffer_prepare,
  441. .buf_queue = buffer_queue,
  442. .buf_release = cx25821_buffer_release,
  443. };
  444. static int video_open(struct file *file)
  445. {
  446. struct cx25821_channel *chan = video_drvdata(file);
  447. struct cx25821_dev *dev = chan->dev;
  448. struct cx25821_fh *fh;
  449. /* allocate + initialize per filehandle data */
  450. fh = kzalloc(sizeof(*fh), GFP_KERNEL);
  451. if (NULL == fh)
  452. return -ENOMEM;
  453. file->private_data = fh;
  454. fh->dev = dev;
  455. fh->channel_id = chan->id;
  456. dev->channel_opened = fh->channel_id;
  457. v4l2_prio_open(&chan->prio, &fh->prio);
  458. dprintk(1, "post videobuf_queue_init()\n");
  459. return 0;
  460. }
  461. static ssize_t video_read(struct file *file, char __user * data, size_t count,
  462. loff_t *ppos)
  463. {
  464. struct cx25821_fh *fh = file->private_data;
  465. struct cx25821_channel *chan = video_drvdata(file);
  466. struct cx25821_dev *dev = fh->dev;
  467. int err = 0;
  468. if (mutex_lock_interruptible(&dev->lock))
  469. return -ERESTARTSYS;
  470. if (chan->streaming_fh && chan->streaming_fh != fh) {
  471. err = -EBUSY;
  472. goto unlock;
  473. }
  474. chan->streaming_fh = fh;
  475. err = videobuf_read_one(&chan->vidq, data, count, ppos,
  476. file->f_flags & O_NONBLOCK);
  477. unlock:
  478. mutex_unlock(&dev->lock);
  479. return err;
  480. }
  481. static unsigned int video_poll(struct file *file,
  482. struct poll_table_struct *wait)
  483. {
  484. struct cx25821_channel *chan = video_drvdata(file);
  485. return videobuf_poll_stream(file, &chan->vidq, wait);
  486. /* This doesn't belong in poll(). This can be done
  487. * much better with vb2. We keep this code here as a
  488. * reminder.
  489. if ((res & POLLIN) && buf->vb.state == VIDEOBUF_DONE) {
  490. struct cx25821_dev *dev = chan->dev;
  491. if (dev && chan->use_cif_resolution) {
  492. u8 cam_id = *((char *)buf->vb.baddr + 3);
  493. memcpy((char *)buf->vb.baddr,
  494. (char *)buf->vb.baddr + (chan->width * 2),
  495. (chan->width * 2));
  496. *((char *)buf->vb.baddr + 3) = cam_id;
  497. }
  498. }
  499. */
  500. }
  501. static int video_release(struct file *file)
  502. {
  503. struct cx25821_channel *chan = video_drvdata(file);
  504. struct cx25821_fh *fh = file->private_data;
  505. struct cx25821_dev *dev = chan->dev;
  506. const struct sram_channel *sram_ch =
  507. dev->channels[0].sram_channels;
  508. mutex_lock(&dev->lock);
  509. /* stop the risc engine and fifo */
  510. cx_write(sram_ch->dma_ctl, 0); /* FIFO and RISC disable */
  511. /* stop video capture */
  512. if (chan->streaming_fh == fh) {
  513. videobuf_queue_cancel(&chan->vidq);
  514. chan->streaming_fh = NULL;
  515. }
  516. if (chan->vidq.read_buf) {
  517. cx25821_buffer_release(&chan->vidq, chan->vidq.read_buf);
  518. kfree(chan->vidq.read_buf);
  519. }
  520. videobuf_mmap_free(&chan->vidq);
  521. mutex_unlock(&dev->lock);
  522. v4l2_prio_close(&chan->prio, fh->prio);
  523. file->private_data = NULL;
  524. kfree(fh);
  525. return 0;
  526. }
  527. /* VIDEO IOCTLS */
  528. static int cx25821_vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  529. struct v4l2_format *f)
  530. {
  531. struct cx25821_channel *chan = video_drvdata(file);
  532. f->fmt.pix.width = chan->width;
  533. f->fmt.pix.height = chan->height;
  534. f->fmt.pix.field = chan->vidq.field;
  535. f->fmt.pix.pixelformat = chan->fmt->fourcc;
  536. f->fmt.pix.bytesperline = (f->fmt.pix.width * chan->fmt->depth) >> 3;
  537. f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
  538. return 0;
  539. }
  540. static int cx25821_vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  541. struct v4l2_format *f)
  542. {
  543. const struct cx25821_fmt *fmt;
  544. enum v4l2_field field;
  545. unsigned int maxw, maxh;
  546. fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
  547. if (NULL == fmt)
  548. return -EINVAL;
  549. field = f->fmt.pix.field;
  550. maxw = 720;
  551. maxh = 576;
  552. if (V4L2_FIELD_ANY == field) {
  553. if (f->fmt.pix.height > maxh / 2)
  554. field = V4L2_FIELD_INTERLACED;
  555. else
  556. field = V4L2_FIELD_TOP;
  557. }
  558. switch (field) {
  559. case V4L2_FIELD_TOP:
  560. case V4L2_FIELD_BOTTOM:
  561. maxh = maxh / 2;
  562. break;
  563. case V4L2_FIELD_INTERLACED:
  564. break;
  565. default:
  566. return -EINVAL;
  567. }
  568. f->fmt.pix.field = field;
  569. if (f->fmt.pix.height < 32)
  570. f->fmt.pix.height = 32;
  571. if (f->fmt.pix.height > maxh)
  572. f->fmt.pix.height = maxh;
  573. if (f->fmt.pix.width < 48)
  574. f->fmt.pix.width = 48;
  575. if (f->fmt.pix.width > maxw)
  576. f->fmt.pix.width = maxw;
  577. f->fmt.pix.width &= ~0x03;
  578. f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
  579. f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
  580. return 0;
  581. }
  582. static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
  583. {
  584. struct cx25821_channel *chan = video_drvdata(file);
  585. struct cx25821_fh *fh = priv;
  586. if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  587. return -EINVAL;
  588. if (chan->streaming_fh && chan->streaming_fh != fh)
  589. return -EBUSY;
  590. chan->streaming_fh = fh;
  591. return videobuf_streamon(&chan->vidq);
  592. }
  593. static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
  594. {
  595. struct cx25821_channel *chan = video_drvdata(file);
  596. struct cx25821_fh *fh = priv;
  597. if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  598. return -EINVAL;
  599. if (chan->streaming_fh && chan->streaming_fh != fh)
  600. return -EBUSY;
  601. if (chan->streaming_fh == NULL)
  602. return 0;
  603. chan->streaming_fh = NULL;
  604. return videobuf_streamoff(&chan->vidq);
  605. }
  606. static int cx25821_is_valid_width(u32 width, v4l2_std_id tvnorm)
  607. {
  608. if (tvnorm == V4L2_STD_PAL_BG) {
  609. if (width == 352 || width == 720)
  610. return 1;
  611. else
  612. return 0;
  613. }
  614. if (tvnorm == V4L2_STD_NTSC_M) {
  615. if (width == 320 || width == 352 || width == 720)
  616. return 1;
  617. else
  618. return 0;
  619. }
  620. return 0;
  621. }
  622. static int cx25821_is_valid_height(u32 height, v4l2_std_id tvnorm)
  623. {
  624. if (tvnorm == V4L2_STD_PAL_BG) {
  625. if (height == 576 || height == 288)
  626. return 1;
  627. else
  628. return 0;
  629. }
  630. if (tvnorm == V4L2_STD_NTSC_M) {
  631. if (height == 480 || height == 240)
  632. return 1;
  633. else
  634. return 0;
  635. }
  636. return 0;
  637. }
  638. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  639. struct v4l2_format *f)
  640. {
  641. struct cx25821_fh *fh = priv;
  642. struct cx25821_channel *chan = video_drvdata(file);
  643. struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
  644. struct v4l2_mbus_framefmt mbus_fmt;
  645. int err;
  646. int pix_format = PIXEL_FRMT_422;
  647. if (fh) {
  648. err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
  649. fh->prio);
  650. if (0 != err)
  651. return err;
  652. }
  653. err = cx25821_vidioc_try_fmt_vid_cap(file, priv, f);
  654. if (0 != err)
  655. return err;
  656. chan->fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
  657. chan->vidq.field = f->fmt.pix.field;
  658. /* check if width and height is valid based on set standard */
  659. if (cx25821_is_valid_width(f->fmt.pix.width, dev->tvnorm))
  660. chan->width = f->fmt.pix.width;
  661. if (cx25821_is_valid_height(f->fmt.pix.height, dev->tvnorm))
  662. chan->height = f->fmt.pix.height;
  663. if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_Y41P)
  664. pix_format = PIXEL_FRMT_411;
  665. else if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_YUYV)
  666. pix_format = PIXEL_FRMT_422;
  667. else
  668. return -EINVAL;
  669. cx25821_set_pixel_format(dev, SRAM_CH00, pix_format);
  670. /* check if cif resolution */
  671. if (chan->width == 320 || chan->width == 352)
  672. chan->use_cif_resolution = 1;
  673. else
  674. chan->use_cif_resolution = 0;
  675. chan->cif_width = chan->width;
  676. medusa_set_resolution(dev, chan->width, SRAM_CH00);
  677. v4l2_fill_mbus_format(&mbus_fmt, &f->fmt.pix, V4L2_MBUS_FMT_FIXED);
  678. cx25821_call_all(dev, video, s_mbus_fmt, &mbus_fmt);
  679. return 0;
  680. }
  681. static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
  682. {
  683. int ret_val = 0;
  684. struct cx25821_channel *chan = video_drvdata(file);
  685. ret_val = videobuf_dqbuf(&chan->vidq, p, file->f_flags & O_NONBLOCK);
  686. p->sequence = chan->dma_vidq.count;
  687. return ret_val;
  688. }
  689. static int vidioc_log_status(struct file *file, void *priv)
  690. {
  691. struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
  692. struct cx25821_fh *fh = priv;
  693. const struct sram_channel *sram_ch =
  694. dev->channels[fh->channel_id].sram_channels;
  695. u32 tmp = 0;
  696. cx25821_call_all(dev, core, log_status);
  697. tmp = cx_read(sram_ch->dma_ctl);
  698. pr_info("Video input 0 is %s\n",
  699. (tmp & 0x11) ? "streaming" : "stopped");
  700. return 0;
  701. }
  702. static int cx25821_vidioc_querycap(struct file *file, void *priv,
  703. struct v4l2_capability *cap)
  704. {
  705. struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
  706. struct cx25821_fh *fh = priv;
  707. const u32 cap_input = V4L2_CAP_VIDEO_CAPTURE |
  708. V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
  709. const u32 cap_output = V4L2_CAP_VIDEO_OUTPUT;
  710. strcpy(cap->driver, "cx25821");
  711. strlcpy(cap->card, cx25821_boards[dev->board].name, sizeof(cap->card));
  712. sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
  713. if (fh->channel_id >= VID_CHANNEL_NUM)
  714. cap->device_caps = cap_output;
  715. else
  716. cap->device_caps = cap_input;
  717. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  718. return 0;
  719. }
  720. static int cx25821_vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  721. struct v4l2_fmtdesc *f)
  722. {
  723. if (unlikely(f->index >= ARRAY_SIZE(formats)))
  724. return -EINVAL;
  725. strlcpy(f->description, formats[f->index].name, sizeof(f->description));
  726. f->pixelformat = formats[f->index].fourcc;
  727. return 0;
  728. }
  729. static int cx25821_vidioc_reqbufs(struct file *file, void *priv,
  730. struct v4l2_requestbuffers *p)
  731. {
  732. struct cx25821_channel *chan = video_drvdata(file);
  733. return videobuf_reqbufs(&chan->vidq, p);
  734. }
  735. static int cx25821_vidioc_querybuf(struct file *file, void *priv,
  736. struct v4l2_buffer *p)
  737. {
  738. struct cx25821_channel *chan = video_drvdata(file);
  739. return videobuf_querybuf(&chan->vidq, p);
  740. }
  741. static int cx25821_vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
  742. {
  743. struct cx25821_channel *chan = video_drvdata(file);
  744. return videobuf_qbuf(&chan->vidq, p);
  745. }
  746. static int cx25821_vidioc_g_priority(struct file *file, void *f, enum v4l2_priority *p)
  747. {
  748. struct cx25821_dev *dev = ((struct cx25821_fh *)f)->dev;
  749. struct cx25821_fh *fh = f;
  750. *p = v4l2_prio_max(&dev->channels[fh->channel_id].prio);
  751. return 0;
  752. }
  753. static int cx25821_vidioc_s_priority(struct file *file, void *f,
  754. enum v4l2_priority prio)
  755. {
  756. struct cx25821_fh *fh = f;
  757. struct cx25821_dev *dev = ((struct cx25821_fh *)f)->dev;
  758. return v4l2_prio_change(&dev->channels[fh->channel_id].prio, &fh->prio,
  759. prio);
  760. }
  761. static int cx25821_vidioc_g_std(struct file *file, void *priv, v4l2_std_id *tvnorms)
  762. {
  763. struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
  764. *tvnorms = dev->tvnorm;
  765. return 0;
  766. }
  767. int cx25821_vidioc_s_std(struct file *file, void *priv, v4l2_std_id tvnorms)
  768. {
  769. struct cx25821_fh *fh = priv;
  770. struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
  771. int err;
  772. if (fh) {
  773. err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
  774. fh->prio);
  775. if (0 != err)
  776. return err;
  777. }
  778. if (dev->tvnorm == tvnorms)
  779. return 0;
  780. cx25821_set_tvnorm(dev, tvnorms);
  781. medusa_set_videostandard(dev);
  782. return 0;
  783. }
  784. static int cx25821_vidioc_enum_input(struct file *file, void *priv,
  785. struct v4l2_input *i)
  786. {
  787. static const char * const iname[] = {
  788. [CX25821_VMUX_COMPOSITE] = "Composite",
  789. [CX25821_VMUX_SVIDEO] = "S-Video",
  790. [CX25821_VMUX_DEBUG] = "for debug only",
  791. };
  792. struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
  793. unsigned int n;
  794. n = i->index;
  795. if (n >= CX25821_NR_INPUT)
  796. return -EINVAL;
  797. if (0 == INPUT(n)->type)
  798. return -EINVAL;
  799. i->type = V4L2_INPUT_TYPE_CAMERA;
  800. strcpy(i->name, iname[INPUT(n)->type]);
  801. i->std = CX25821_NORMS;
  802. return 0;
  803. }
  804. static int cx25821_vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  805. {
  806. struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
  807. *i = dev->input;
  808. return 0;
  809. }
  810. static int cx25821_vidioc_s_input(struct file *file, void *priv, unsigned int i)
  811. {
  812. struct cx25821_fh *fh = priv;
  813. struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
  814. int err;
  815. if (fh) {
  816. err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
  817. fh->prio);
  818. if (0 != err)
  819. return err;
  820. }
  821. if (i >= CX25821_NR_INPUT || INPUT(i)->type == 0)
  822. return -EINVAL;
  823. cx25821_video_mux(dev, i);
  824. return 0;
  825. }
  826. #ifdef CONFIG_VIDEO_ADV_DEBUG
  827. int cx25821_vidioc_g_register(struct file *file, void *fh,
  828. struct v4l2_dbg_register *reg)
  829. {
  830. struct cx25821_dev *dev = ((struct cx25821_fh *)fh)->dev;
  831. if (!v4l2_chip_match_host(&reg->match))
  832. return -EINVAL;
  833. cx25821_call_all(dev, core, g_register, reg);
  834. return 0;
  835. }
  836. int cx25821_vidioc_s_register(struct file *file, void *fh,
  837. const struct v4l2_dbg_register *reg)
  838. {
  839. struct cx25821_dev *dev = ((struct cx25821_fh *)fh)->dev;
  840. if (!v4l2_chip_match_host(&reg->match))
  841. return -EINVAL;
  842. cx25821_call_all(dev, core, s_register, reg);
  843. return 0;
  844. }
  845. #endif
  846. static int cx25821_s_ctrl(struct v4l2_ctrl *ctrl)
  847. {
  848. struct cx25821_channel *chan =
  849. container_of(ctrl->handler, struct cx25821_channel, hdl);
  850. struct cx25821_dev *dev = chan->dev;
  851. switch (ctrl->id) {
  852. case V4L2_CID_BRIGHTNESS:
  853. medusa_set_brightness(dev, ctrl->val, chan->id);
  854. break;
  855. case V4L2_CID_HUE:
  856. medusa_set_hue(dev, ctrl->val, chan->id);
  857. break;
  858. case V4L2_CID_CONTRAST:
  859. medusa_set_contrast(dev, ctrl->val, chan->id);
  860. break;
  861. case V4L2_CID_SATURATION:
  862. medusa_set_saturation(dev, ctrl->val, chan->id);
  863. break;
  864. default:
  865. return -EINVAL;
  866. }
  867. return 0;
  868. }
  869. static long video_ioctl_upstream9(struct file *file, unsigned int cmd,
  870. unsigned long arg)
  871. {
  872. struct cx25821_fh *fh = file->private_data;
  873. struct cx25821_dev *dev = fh->dev;
  874. int command = 0;
  875. struct upstream_user_struct *data_from_user;
  876. data_from_user = (struct upstream_user_struct *)arg;
  877. if (!data_from_user) {
  878. pr_err("%s(): Upstream data is INVALID. Returning\n", __func__);
  879. return 0;
  880. }
  881. command = data_from_user->command;
  882. if (command != UPSTREAM_START_VIDEO && command != UPSTREAM_STOP_VIDEO)
  883. return 0;
  884. dev->input_filename = data_from_user->input_filename;
  885. dev->input_audiofilename = data_from_user->input_filename;
  886. dev->vid_stdname = data_from_user->vid_stdname;
  887. dev->pixel_format = data_from_user->pixel_format;
  888. dev->channel_select = data_from_user->channel_select;
  889. dev->command = data_from_user->command;
  890. switch (command) {
  891. case UPSTREAM_START_VIDEO:
  892. cx25821_start_upstream_video_ch1(dev, data_from_user);
  893. break;
  894. case UPSTREAM_STOP_VIDEO:
  895. cx25821_stop_upstream_video_ch1(dev);
  896. break;
  897. }
  898. return 0;
  899. }
  900. static long video_ioctl_upstream10(struct file *file, unsigned int cmd,
  901. unsigned long arg)
  902. {
  903. struct cx25821_fh *fh = file->private_data;
  904. struct cx25821_dev *dev = fh->dev;
  905. int command = 0;
  906. struct upstream_user_struct *data_from_user;
  907. data_from_user = (struct upstream_user_struct *)arg;
  908. if (!data_from_user) {
  909. pr_err("%s(): Upstream data is INVALID. Returning\n", __func__);
  910. return 0;
  911. }
  912. command = data_from_user->command;
  913. if (command != UPSTREAM_START_VIDEO && command != UPSTREAM_STOP_VIDEO)
  914. return 0;
  915. dev->input_filename_ch2 = data_from_user->input_filename;
  916. dev->input_audiofilename = data_from_user->input_filename;
  917. dev->vid_stdname_ch2 = data_from_user->vid_stdname;
  918. dev->pixel_format_ch2 = data_from_user->pixel_format;
  919. dev->channel_select_ch2 = data_from_user->channel_select;
  920. dev->command_ch2 = data_from_user->command;
  921. switch (command) {
  922. case UPSTREAM_START_VIDEO:
  923. cx25821_start_upstream_video_ch2(dev, data_from_user);
  924. break;
  925. case UPSTREAM_STOP_VIDEO:
  926. cx25821_stop_upstream_video_ch2(dev);
  927. break;
  928. }
  929. return 0;
  930. }
  931. static long video_ioctl_upstream11(struct file *file, unsigned int cmd,
  932. unsigned long arg)
  933. {
  934. struct cx25821_fh *fh = file->private_data;
  935. struct cx25821_dev *dev = fh->dev;
  936. int command = 0;
  937. struct upstream_user_struct *data_from_user;
  938. data_from_user = (struct upstream_user_struct *)arg;
  939. if (!data_from_user) {
  940. pr_err("%s(): Upstream data is INVALID. Returning\n", __func__);
  941. return 0;
  942. }
  943. command = data_from_user->command;
  944. if (command != UPSTREAM_START_AUDIO && command != UPSTREAM_STOP_AUDIO)
  945. return 0;
  946. dev->input_filename = data_from_user->input_filename;
  947. dev->input_audiofilename = data_from_user->input_filename;
  948. dev->vid_stdname = data_from_user->vid_stdname;
  949. dev->pixel_format = data_from_user->pixel_format;
  950. dev->channel_select = data_from_user->channel_select;
  951. dev->command = data_from_user->command;
  952. switch (command) {
  953. case UPSTREAM_START_AUDIO:
  954. cx25821_start_upstream_audio(dev, data_from_user);
  955. break;
  956. case UPSTREAM_STOP_AUDIO:
  957. cx25821_stop_upstream_audio(dev);
  958. break;
  959. }
  960. return 0;
  961. }
  962. static long video_ioctl_set(struct file *file, unsigned int cmd,
  963. unsigned long arg)
  964. {
  965. struct cx25821_fh *fh = file->private_data;
  966. struct cx25821_dev *dev = fh->dev;
  967. struct downstream_user_struct *data_from_user;
  968. int command;
  969. int width = 720;
  970. int selected_channel = 0;
  971. int pix_format = 0;
  972. int i = 0;
  973. int cif_enable = 0;
  974. int cif_width = 0;
  975. data_from_user = (struct downstream_user_struct *)arg;
  976. if (!data_from_user) {
  977. pr_err("%s(): User data is INVALID. Returning\n", __func__);
  978. return 0;
  979. }
  980. command = data_from_user->command;
  981. if (command != SET_VIDEO_STD && command != SET_PIXEL_FORMAT
  982. && command != ENABLE_CIF_RESOLUTION && command != REG_READ
  983. && command != REG_WRITE && command != MEDUSA_READ
  984. && command != MEDUSA_WRITE) {
  985. return 0;
  986. }
  987. switch (command) {
  988. case SET_VIDEO_STD:
  989. if (!strcmp(data_from_user->vid_stdname, "PAL"))
  990. dev->tvnorm = V4L2_STD_PAL_BG;
  991. else
  992. dev->tvnorm = V4L2_STD_NTSC_M;
  993. medusa_set_videostandard(dev);
  994. break;
  995. case SET_PIXEL_FORMAT:
  996. selected_channel = data_from_user->decoder_select;
  997. pix_format = data_from_user->pixel_format;
  998. if (!(selected_channel <= 7 && selected_channel >= 0)) {
  999. selected_channel -= 4;
  1000. selected_channel = selected_channel % 8;
  1001. }
  1002. if (selected_channel >= 0)
  1003. cx25821_set_pixel_format(dev, selected_channel,
  1004. pix_format);
  1005. break;
  1006. case ENABLE_CIF_RESOLUTION:
  1007. selected_channel = data_from_user->decoder_select;
  1008. cif_enable = data_from_user->cif_resolution_enable;
  1009. cif_width = data_from_user->cif_width;
  1010. if (cif_enable) {
  1011. if (dev->tvnorm & V4L2_STD_PAL_BG
  1012. || dev->tvnorm & V4L2_STD_PAL_DK) {
  1013. width = 352;
  1014. } else {
  1015. width = cif_width;
  1016. if (cif_width != 320 && cif_width != 352)
  1017. width = 320;
  1018. }
  1019. }
  1020. if (!(selected_channel <= 7 && selected_channel >= 0)) {
  1021. selected_channel -= 4;
  1022. selected_channel = selected_channel % 8;
  1023. }
  1024. if (selected_channel <= 7 && selected_channel >= 0) {
  1025. dev->channels[selected_channel].use_cif_resolution =
  1026. cif_enable;
  1027. dev->channels[selected_channel].cif_width = width;
  1028. } else {
  1029. for (i = 0; i < VID_CHANNEL_NUM; i++) {
  1030. dev->channels[i].use_cif_resolution =
  1031. cif_enable;
  1032. dev->channels[i].cif_width = width;
  1033. }
  1034. }
  1035. medusa_set_resolution(dev, width, selected_channel);
  1036. break;
  1037. case REG_READ:
  1038. data_from_user->reg_data = cx_read(data_from_user->reg_address);
  1039. break;
  1040. case REG_WRITE:
  1041. cx_write(data_from_user->reg_address, data_from_user->reg_data);
  1042. break;
  1043. case MEDUSA_READ:
  1044. cx25821_i2c_read(&dev->i2c_bus[0],
  1045. (u16) data_from_user->reg_address,
  1046. &data_from_user->reg_data);
  1047. break;
  1048. case MEDUSA_WRITE:
  1049. cx25821_i2c_write(&dev->i2c_bus[0],
  1050. (u16) data_from_user->reg_address,
  1051. data_from_user->reg_data);
  1052. break;
  1053. }
  1054. return 0;
  1055. }
  1056. static long cx25821_video_ioctl(struct file *file,
  1057. unsigned int cmd, unsigned long arg)
  1058. {
  1059. int ret = 0;
  1060. struct cx25821_fh *fh = file->private_data;
  1061. /* check to see if it's the video upstream */
  1062. if (fh->channel_id == SRAM_CH09) {
  1063. ret = video_ioctl_upstream9(file, cmd, arg);
  1064. return ret;
  1065. } else if (fh->channel_id == SRAM_CH10) {
  1066. ret = video_ioctl_upstream10(file, cmd, arg);
  1067. return ret;
  1068. } else if (fh->channel_id == SRAM_CH11) {
  1069. ret = video_ioctl_upstream11(file, cmd, arg);
  1070. ret = video_ioctl_set(file, cmd, arg);
  1071. return ret;
  1072. }
  1073. return video_ioctl2(file, cmd, arg);
  1074. }
  1075. static const struct v4l2_ctrl_ops cx25821_ctrl_ops = {
  1076. .s_ctrl = cx25821_s_ctrl,
  1077. };
  1078. static const struct v4l2_file_operations video_fops = {
  1079. .owner = THIS_MODULE,
  1080. .open = video_open,
  1081. .release = video_release,
  1082. .read = video_read,
  1083. .poll = video_poll,
  1084. .mmap = cx25821_video_mmap,
  1085. .unlocked_ioctl = cx25821_video_ioctl,
  1086. };
  1087. static const struct v4l2_ioctl_ops video_ioctl_ops = {
  1088. .vidioc_querycap = cx25821_vidioc_querycap,
  1089. .vidioc_enum_fmt_vid_cap = cx25821_vidioc_enum_fmt_vid_cap,
  1090. .vidioc_g_fmt_vid_cap = cx25821_vidioc_g_fmt_vid_cap,
  1091. .vidioc_try_fmt_vid_cap = cx25821_vidioc_try_fmt_vid_cap,
  1092. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1093. .vidioc_reqbufs = cx25821_vidioc_reqbufs,
  1094. .vidioc_querybuf = cx25821_vidioc_querybuf,
  1095. .vidioc_qbuf = cx25821_vidioc_qbuf,
  1096. .vidioc_dqbuf = vidioc_dqbuf,
  1097. .vidioc_g_std = cx25821_vidioc_g_std,
  1098. .vidioc_s_std = cx25821_vidioc_s_std,
  1099. .vidioc_enum_input = cx25821_vidioc_enum_input,
  1100. .vidioc_g_input = cx25821_vidioc_g_input,
  1101. .vidioc_s_input = cx25821_vidioc_s_input,
  1102. .vidioc_streamon = vidioc_streamon,
  1103. .vidioc_streamoff = vidioc_streamoff,
  1104. .vidioc_log_status = vidioc_log_status,
  1105. .vidioc_g_priority = cx25821_vidioc_g_priority,
  1106. .vidioc_s_priority = cx25821_vidioc_s_priority,
  1107. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1108. .vidioc_g_register = cx25821_vidioc_g_register,
  1109. .vidioc_s_register = cx25821_vidioc_s_register,
  1110. #endif
  1111. };
  1112. static const struct video_device cx25821_video_device = {
  1113. .name = "cx25821-video",
  1114. .fops = &video_fops,
  1115. .release = video_device_release_empty,
  1116. .minor = -1,
  1117. .ioctl_ops = &video_ioctl_ops,
  1118. .tvnorms = CX25821_NORMS,
  1119. };
  1120. void cx25821_video_unregister(struct cx25821_dev *dev, int chan_num)
  1121. {
  1122. cx_clear(PCI_INT_MSK, 1);
  1123. if (video_is_registered(&dev->channels[chan_num].vdev)) {
  1124. video_unregister_device(&dev->channels[chan_num].vdev);
  1125. v4l2_ctrl_handler_free(&dev->channels[chan_num].hdl);
  1126. btcx_riscmem_free(dev->pci,
  1127. &dev->channels[chan_num].dma_vidq.stopper);
  1128. }
  1129. }
  1130. int cx25821_video_register(struct cx25821_dev *dev)
  1131. {
  1132. int err;
  1133. int i;
  1134. /* initial device configuration */
  1135. dev->tvnorm = V4L2_STD_NTSC_M,
  1136. cx25821_set_tvnorm(dev, dev->tvnorm);
  1137. spin_lock_init(&dev->slock);
  1138. for (i = 0; i < VID_CHANNEL_NUM; ++i) {
  1139. struct cx25821_channel *chan = &dev->channels[i];
  1140. struct video_device *vdev = &chan->vdev;
  1141. struct v4l2_ctrl_handler *hdl = &chan->hdl;
  1142. if (i == SRAM_CH08) /* audio channel */
  1143. continue;
  1144. v4l2_ctrl_handler_init(hdl, 4);
  1145. v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
  1146. V4L2_CID_BRIGHTNESS, 0, 10000, 1, 6200);
  1147. v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
  1148. V4L2_CID_CONTRAST, 0, 10000, 1, 5000);
  1149. v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
  1150. V4L2_CID_SATURATION, 0, 10000, 1, 5000);
  1151. v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
  1152. V4L2_CID_HUE, 0, 10000, 1, 5000);
  1153. if (hdl->error) {
  1154. err = hdl->error;
  1155. goto fail_unreg;
  1156. }
  1157. err = v4l2_ctrl_handler_setup(hdl);
  1158. if (err)
  1159. goto fail_unreg;
  1160. cx25821_risc_stopper(dev->pci, &chan->dma_vidq.stopper,
  1161. chan->sram_channels->dma_ctl, 0x11, 0);
  1162. chan->sram_channels = &cx25821_sram_channels[i];
  1163. chan->width = 720;
  1164. if (dev->tvnorm & V4L2_STD_625_50)
  1165. chan->height = 576;
  1166. else
  1167. chan->height = 480;
  1168. if (chan->pixel_formats == PIXEL_FRMT_411)
  1169. chan->fmt = cx25821_format_by_fourcc(V4L2_PIX_FMT_Y41P);
  1170. else
  1171. chan->fmt = cx25821_format_by_fourcc(V4L2_PIX_FMT_YUYV);
  1172. cx_write(chan->sram_channels->int_stat, 0xffffffff);
  1173. INIT_LIST_HEAD(&chan->dma_vidq.active);
  1174. INIT_LIST_HEAD(&chan->dma_vidq.queued);
  1175. chan->timeout_data.dev = dev;
  1176. chan->timeout_data.channel = &cx25821_sram_channels[i];
  1177. chan->dma_vidq.timeout.function = cx25821_vid_timeout;
  1178. chan->dma_vidq.timeout.data = (unsigned long)&chan->timeout_data;
  1179. init_timer(&chan->dma_vidq.timeout);
  1180. videobuf_queue_sg_init(&chan->vidq, &cx25821_video_qops, &dev->pci->dev,
  1181. &dev->slock, V4L2_BUF_TYPE_VIDEO_CAPTURE,
  1182. V4L2_FIELD_INTERLACED, sizeof(struct cx25821_buffer),
  1183. chan, &dev->lock);
  1184. /* register v4l devices */
  1185. *vdev = cx25821_video_device;
  1186. vdev->v4l2_dev = &dev->v4l2_dev;
  1187. vdev->ctrl_handler = hdl;
  1188. vdev->lock = &dev->lock;
  1189. snprintf(vdev->name, sizeof(vdev->name), "%s #%d", dev->name, i);
  1190. video_set_drvdata(vdev, chan);
  1191. err = video_register_device(vdev, VFL_TYPE_GRABBER,
  1192. video_nr[dev->nr]);
  1193. if (err < 0)
  1194. goto fail_unreg;
  1195. }
  1196. /* set PCI interrupt */
  1197. cx_set(PCI_INT_MSK, 0xff);
  1198. return 0;
  1199. fail_unreg:
  1200. while (i >= 0)
  1201. cx25821_video_unregister(dev, i--);
  1202. return err;
  1203. }