cx25821-video.c 40 KB

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