cx25821-video.c 49 KB

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