cx25821-video.c 39 KB

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