cx25821-video.c 43 KB

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