saa7146_video.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304
  1. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  2. #include <media/saa7146_vv.h>
  3. #include <media/v4l2-chip-ident.h>
  4. #include <linux/module.h>
  5. static int max_memory = 32;
  6. module_param(max_memory, int, 0644);
  7. MODULE_PARM_DESC(max_memory, "maximum memory usage for capture buffers (default: 32Mb)");
  8. #define IS_CAPTURE_ACTIVE(fh) \
  9. (((vv->video_status & STATUS_CAPTURE) != 0) && (vv->video_fh == fh))
  10. #define IS_OVERLAY_ACTIVE(fh) \
  11. (((vv->video_status & STATUS_OVERLAY) != 0) && (vv->video_fh == fh))
  12. /* format descriptions for capture and preview */
  13. static struct saa7146_format formats[] = {
  14. {
  15. .name = "RGB-8 (3-3-2)",
  16. .pixelformat = V4L2_PIX_FMT_RGB332,
  17. .trans = RGB08_COMPOSED,
  18. .depth = 8,
  19. .flags = 0,
  20. }, {
  21. .name = "RGB-16 (5/B-6/G-5/R)",
  22. .pixelformat = V4L2_PIX_FMT_RGB565,
  23. .trans = RGB16_COMPOSED,
  24. .depth = 16,
  25. .flags = 0,
  26. }, {
  27. .name = "RGB-24 (B-G-R)",
  28. .pixelformat = V4L2_PIX_FMT_BGR24,
  29. .trans = RGB24_COMPOSED,
  30. .depth = 24,
  31. .flags = 0,
  32. }, {
  33. .name = "RGB-32 (B-G-R)",
  34. .pixelformat = V4L2_PIX_FMT_BGR32,
  35. .trans = RGB32_COMPOSED,
  36. .depth = 32,
  37. .flags = 0,
  38. }, {
  39. .name = "RGB-32 (R-G-B)",
  40. .pixelformat = V4L2_PIX_FMT_RGB32,
  41. .trans = RGB32_COMPOSED,
  42. .depth = 32,
  43. .flags = 0,
  44. .swap = 0x2,
  45. }, {
  46. .name = "Greyscale-8",
  47. .pixelformat = V4L2_PIX_FMT_GREY,
  48. .trans = Y8,
  49. .depth = 8,
  50. .flags = 0,
  51. }, {
  52. .name = "YUV 4:2:2 planar (Y-Cb-Cr)",
  53. .pixelformat = V4L2_PIX_FMT_YUV422P,
  54. .trans = YUV422_DECOMPOSED,
  55. .depth = 16,
  56. .flags = FORMAT_BYTE_SWAP|FORMAT_IS_PLANAR,
  57. }, {
  58. .name = "YVU 4:2:0 planar (Y-Cb-Cr)",
  59. .pixelformat = V4L2_PIX_FMT_YVU420,
  60. .trans = YUV420_DECOMPOSED,
  61. .depth = 12,
  62. .flags = FORMAT_BYTE_SWAP|FORMAT_IS_PLANAR,
  63. }, {
  64. .name = "YUV 4:2:0 planar (Y-Cb-Cr)",
  65. .pixelformat = V4L2_PIX_FMT_YUV420,
  66. .trans = YUV420_DECOMPOSED,
  67. .depth = 12,
  68. .flags = FORMAT_IS_PLANAR,
  69. }, {
  70. .name = "YUV 4:2:2 (U-Y-V-Y)",
  71. .pixelformat = V4L2_PIX_FMT_UYVY,
  72. .trans = YUV422_COMPOSED,
  73. .depth = 16,
  74. .flags = 0,
  75. }
  76. };
  77. /* unfortunately, the saa7146 contains a bug which prevents it from doing on-the-fly byte swaps.
  78. due to this, it's impossible to provide additional *packed* formats, which are simply byte swapped
  79. (like V4L2_PIX_FMT_YUYV) ... 8-( */
  80. static int NUM_FORMATS = sizeof(formats)/sizeof(struct saa7146_format);
  81. struct saa7146_format* saa7146_format_by_fourcc(struct saa7146_dev *dev, int fourcc)
  82. {
  83. int i, j = NUM_FORMATS;
  84. for (i = 0; i < j; i++) {
  85. if (formats[i].pixelformat == fourcc) {
  86. return formats+i;
  87. }
  88. }
  89. DEB_D("unknown pixelformat:'%4.4s'\n", (char *)&fourcc);
  90. return NULL;
  91. }
  92. static int vidioc_try_fmt_vid_overlay(struct file *file, void *fh, struct v4l2_format *f);
  93. int saa7146_start_preview(struct saa7146_fh *fh)
  94. {
  95. struct saa7146_dev *dev = fh->dev;
  96. struct saa7146_vv *vv = dev->vv_data;
  97. struct v4l2_format fmt;
  98. int ret = 0, err = 0;
  99. DEB_EE("dev:%p, fh:%p\n", dev, fh);
  100. /* check if we have overlay information */
  101. if (vv->ov.fh == NULL) {
  102. DEB_D("no overlay data available. try S_FMT first.\n");
  103. return -EAGAIN;
  104. }
  105. /* check if streaming capture is running */
  106. if (IS_CAPTURE_ACTIVE(fh) != 0) {
  107. DEB_D("streaming capture is active\n");
  108. return -EBUSY;
  109. }
  110. /* check if overlay is running */
  111. if (IS_OVERLAY_ACTIVE(fh) != 0) {
  112. if (vv->video_fh == fh) {
  113. DEB_D("overlay is already active\n");
  114. return 0;
  115. }
  116. DEB_D("overlay is already active in another open\n");
  117. return -EBUSY;
  118. }
  119. if (0 == saa7146_res_get(fh, RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP)) {
  120. DEB_D("cannot get necessary overlay resources\n");
  121. return -EBUSY;
  122. }
  123. fmt.fmt.win = vv->ov.win;
  124. err = vidioc_try_fmt_vid_overlay(NULL, fh, &fmt);
  125. if (0 != err) {
  126. saa7146_res_free(vv->video_fh, RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP);
  127. return -EBUSY;
  128. }
  129. vv->ov.win = fmt.fmt.win;
  130. DEB_D("%dx%d+%d+%d %s field=%s\n",
  131. vv->ov.win.w.width, vv->ov.win.w.height,
  132. vv->ov.win.w.left, vv->ov.win.w.top,
  133. vv->ov_fmt->name, v4l2_field_names[vv->ov.win.field]);
  134. if (0 != (ret = saa7146_enable_overlay(fh))) {
  135. DEB_D("enabling overlay failed: %d\n", ret);
  136. saa7146_res_free(vv->video_fh, RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP);
  137. return ret;
  138. }
  139. vv->video_status = STATUS_OVERLAY;
  140. vv->video_fh = fh;
  141. return 0;
  142. }
  143. EXPORT_SYMBOL_GPL(saa7146_start_preview);
  144. int saa7146_stop_preview(struct saa7146_fh *fh)
  145. {
  146. struct saa7146_dev *dev = fh->dev;
  147. struct saa7146_vv *vv = dev->vv_data;
  148. DEB_EE("dev:%p, fh:%p\n", dev, fh);
  149. /* check if streaming capture is running */
  150. if (IS_CAPTURE_ACTIVE(fh) != 0) {
  151. DEB_D("streaming capture is active\n");
  152. return -EBUSY;
  153. }
  154. /* check if overlay is running at all */
  155. if ((vv->video_status & STATUS_OVERLAY) == 0) {
  156. DEB_D("no active overlay\n");
  157. return 0;
  158. }
  159. if (vv->video_fh != fh) {
  160. DEB_D("overlay is active, but in another open\n");
  161. return -EBUSY;
  162. }
  163. vv->video_status = 0;
  164. vv->video_fh = NULL;
  165. saa7146_disable_overlay(fh);
  166. saa7146_res_free(fh, RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP);
  167. return 0;
  168. }
  169. EXPORT_SYMBOL_GPL(saa7146_stop_preview);
  170. /********************************************************************************/
  171. /* common pagetable functions */
  172. static int saa7146_pgtable_build(struct saa7146_dev *dev, struct saa7146_buf *buf)
  173. {
  174. struct pci_dev *pci = dev->pci;
  175. struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
  176. struct scatterlist *list = dma->sglist;
  177. int length = dma->sglen;
  178. struct saa7146_format *sfmt = saa7146_format_by_fourcc(dev,buf->fmt->pixelformat);
  179. DEB_EE("dev:%p, buf:%p, sg_len:%d\n", dev, buf, length);
  180. if( 0 != IS_PLANAR(sfmt->trans)) {
  181. struct saa7146_pgtable *pt1 = &buf->pt[0];
  182. struct saa7146_pgtable *pt2 = &buf->pt[1];
  183. struct saa7146_pgtable *pt3 = &buf->pt[2];
  184. __le32 *ptr1, *ptr2, *ptr3;
  185. __le32 fill;
  186. int size = buf->fmt->width*buf->fmt->height;
  187. int i,p,m1,m2,m3,o1,o2;
  188. switch( sfmt->depth ) {
  189. case 12: {
  190. /* create some offsets inside the page table */
  191. m1 = ((size+PAGE_SIZE)/PAGE_SIZE)-1;
  192. m2 = ((size+(size/4)+PAGE_SIZE)/PAGE_SIZE)-1;
  193. m3 = ((size+(size/2)+PAGE_SIZE)/PAGE_SIZE)-1;
  194. o1 = size%PAGE_SIZE;
  195. o2 = (size+(size/4))%PAGE_SIZE;
  196. DEB_CAP("size:%d, m1:%d, m2:%d, m3:%d, o1:%d, o2:%d\n",
  197. size, m1, m2, m3, o1, o2);
  198. break;
  199. }
  200. case 16: {
  201. /* create some offsets inside the page table */
  202. m1 = ((size+PAGE_SIZE)/PAGE_SIZE)-1;
  203. m2 = ((size+(size/2)+PAGE_SIZE)/PAGE_SIZE)-1;
  204. m3 = ((2*size+PAGE_SIZE)/PAGE_SIZE)-1;
  205. o1 = size%PAGE_SIZE;
  206. o2 = (size+(size/2))%PAGE_SIZE;
  207. DEB_CAP("size:%d, m1:%d, m2:%d, m3:%d, o1:%d, o2:%d\n",
  208. size, m1, m2, m3, o1, o2);
  209. break;
  210. }
  211. default: {
  212. return -1;
  213. }
  214. }
  215. ptr1 = pt1->cpu;
  216. ptr2 = pt2->cpu;
  217. ptr3 = pt3->cpu;
  218. /* walk all pages, copy all page addresses to ptr1 */
  219. for (i = 0; i < length; i++, list++) {
  220. for (p = 0; p * 4096 < list->length; p++, ptr1++) {
  221. *ptr1 = cpu_to_le32(sg_dma_address(list) - list->offset);
  222. }
  223. }
  224. /*
  225. ptr1 = pt1->cpu;
  226. for(j=0;j<40;j++) {
  227. printk("ptr1 %d: 0x%08x\n",j,ptr1[j]);
  228. }
  229. */
  230. /* if we have a user buffer, the first page may not be
  231. aligned to a page boundary. */
  232. pt1->offset = dma->sglist->offset;
  233. pt2->offset = pt1->offset+o1;
  234. pt3->offset = pt1->offset+o2;
  235. /* create video-dma2 page table */
  236. ptr1 = pt1->cpu;
  237. for(i = m1; i <= m2 ; i++, ptr2++) {
  238. *ptr2 = ptr1[i];
  239. }
  240. fill = *(ptr2-1);
  241. for(;i<1024;i++,ptr2++) {
  242. *ptr2 = fill;
  243. }
  244. /* create video-dma3 page table */
  245. ptr1 = pt1->cpu;
  246. for(i = m2; i <= m3; i++,ptr3++) {
  247. *ptr3 = ptr1[i];
  248. }
  249. fill = *(ptr3-1);
  250. for(;i<1024;i++,ptr3++) {
  251. *ptr3 = fill;
  252. }
  253. /* finally: finish up video-dma1 page table */
  254. ptr1 = pt1->cpu+m1;
  255. fill = pt1->cpu[m1];
  256. for(i=m1;i<1024;i++,ptr1++) {
  257. *ptr1 = fill;
  258. }
  259. /*
  260. ptr1 = pt1->cpu;
  261. ptr2 = pt2->cpu;
  262. ptr3 = pt3->cpu;
  263. for(j=0;j<40;j++) {
  264. printk("ptr1 %d: 0x%08x\n",j,ptr1[j]);
  265. }
  266. for(j=0;j<40;j++) {
  267. printk("ptr2 %d: 0x%08x\n",j,ptr2[j]);
  268. }
  269. for(j=0;j<40;j++) {
  270. printk("ptr3 %d: 0x%08x\n",j,ptr3[j]);
  271. }
  272. */
  273. } else {
  274. struct saa7146_pgtable *pt = &buf->pt[0];
  275. return saa7146_pgtable_build_single(pci, pt, list, length);
  276. }
  277. return 0;
  278. }
  279. /********************************************************************************/
  280. /* file operations */
  281. static int video_begin(struct saa7146_fh *fh)
  282. {
  283. struct saa7146_dev *dev = fh->dev;
  284. struct saa7146_vv *vv = dev->vv_data;
  285. struct saa7146_format *fmt = NULL;
  286. unsigned int resource;
  287. int ret = 0, err = 0;
  288. DEB_EE("dev:%p, fh:%p\n", dev, fh);
  289. if ((vv->video_status & STATUS_CAPTURE) != 0) {
  290. if (vv->video_fh == fh) {
  291. DEB_S("already capturing\n");
  292. return 0;
  293. }
  294. DEB_S("already capturing in another open\n");
  295. return -EBUSY;
  296. }
  297. if ((vv->video_status & STATUS_OVERLAY) != 0) {
  298. DEB_S("warning: suspending overlay video for streaming capture\n");
  299. vv->ov_suspend = vv->video_fh;
  300. err = saa7146_stop_preview(vv->video_fh); /* side effect: video_status is now 0, video_fh is NULL */
  301. if (0 != err) {
  302. DEB_D("suspending video failed. aborting\n");
  303. return err;
  304. }
  305. }
  306. fmt = saa7146_format_by_fourcc(dev,fh->video_fmt.pixelformat);
  307. /* we need to have a valid format set here */
  308. BUG_ON(NULL == fmt);
  309. if (0 != (fmt->flags & FORMAT_IS_PLANAR)) {
  310. resource = RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP|RESOURCE_DMA3_BRS;
  311. } else {
  312. resource = RESOURCE_DMA1_HPS;
  313. }
  314. ret = saa7146_res_get(fh, resource);
  315. if (0 == ret) {
  316. DEB_S("cannot get capture resource %d\n", resource);
  317. if (vv->ov_suspend != NULL) {
  318. saa7146_start_preview(vv->ov_suspend);
  319. vv->ov_suspend = NULL;
  320. }
  321. return -EBUSY;
  322. }
  323. /* clear out beginning of streaming bit (rps register 0)*/
  324. saa7146_write(dev, MC2, MASK_27 );
  325. /* enable rps0 irqs */
  326. SAA7146_IER_ENABLE(dev, MASK_27);
  327. vv->video_fh = fh;
  328. vv->video_status = STATUS_CAPTURE;
  329. return 0;
  330. }
  331. static int video_end(struct saa7146_fh *fh, struct file *file)
  332. {
  333. struct saa7146_dev *dev = fh->dev;
  334. struct saa7146_vv *vv = dev->vv_data;
  335. struct saa7146_format *fmt = NULL;
  336. unsigned long flags;
  337. unsigned int resource;
  338. u32 dmas = 0;
  339. DEB_EE("dev:%p, fh:%p\n", dev, fh);
  340. if ((vv->video_status & STATUS_CAPTURE) != STATUS_CAPTURE) {
  341. DEB_S("not capturing\n");
  342. return 0;
  343. }
  344. if (vv->video_fh != fh) {
  345. DEB_S("capturing, but in another open\n");
  346. return -EBUSY;
  347. }
  348. fmt = saa7146_format_by_fourcc(dev,fh->video_fmt.pixelformat);
  349. /* we need to have a valid format set here */
  350. BUG_ON(NULL == fmt);
  351. if (0 != (fmt->flags & FORMAT_IS_PLANAR)) {
  352. resource = RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP|RESOURCE_DMA3_BRS;
  353. dmas = MASK_22 | MASK_21 | MASK_20;
  354. } else {
  355. resource = RESOURCE_DMA1_HPS;
  356. dmas = MASK_22;
  357. }
  358. spin_lock_irqsave(&dev->slock,flags);
  359. /* disable rps0 */
  360. saa7146_write(dev, MC1, MASK_28);
  361. /* disable rps0 irqs */
  362. SAA7146_IER_DISABLE(dev, MASK_27);
  363. /* shut down all used video dma transfers */
  364. saa7146_write(dev, MC1, dmas);
  365. spin_unlock_irqrestore(&dev->slock, flags);
  366. vv->video_fh = NULL;
  367. vv->video_status = 0;
  368. saa7146_res_free(fh, resource);
  369. if (vv->ov_suspend != NULL) {
  370. saa7146_start_preview(vv->ov_suspend);
  371. vv->ov_suspend = NULL;
  372. }
  373. return 0;
  374. }
  375. static int vidioc_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
  376. {
  377. struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
  378. strcpy((char *)cap->driver, "saa7146 v4l2");
  379. strlcpy((char *)cap->card, dev->ext->name, sizeof(cap->card));
  380. sprintf((char *)cap->bus_info, "PCI:%s", pci_name(dev->pci));
  381. cap->version = SAA7146_VERSION_CODE;
  382. cap->device_caps =
  383. V4L2_CAP_VIDEO_CAPTURE |
  384. V4L2_CAP_VIDEO_OVERLAY |
  385. V4L2_CAP_READWRITE |
  386. V4L2_CAP_STREAMING;
  387. cap->device_caps |= dev->ext_vv_data->capabilities;
  388. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  389. return 0;
  390. }
  391. static int vidioc_g_fbuf(struct file *file, void *fh, struct v4l2_framebuffer *fb)
  392. {
  393. struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
  394. struct saa7146_vv *vv = dev->vv_data;
  395. *fb = vv->ov_fb;
  396. fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
  397. fb->flags = V4L2_FBUF_FLAG_PRIMARY;
  398. return 0;
  399. }
  400. static int vidioc_s_fbuf(struct file *file, void *fh, struct v4l2_framebuffer *fb)
  401. {
  402. struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
  403. struct saa7146_vv *vv = dev->vv_data;
  404. struct saa7146_format *fmt;
  405. DEB_EE("VIDIOC_S_FBUF\n");
  406. if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
  407. return -EPERM;
  408. /* check args */
  409. fmt = saa7146_format_by_fourcc(dev, fb->fmt.pixelformat);
  410. if (NULL == fmt)
  411. return -EINVAL;
  412. /* planar formats are not allowed for overlay video, clipping and video dma would clash */
  413. if (fmt->flags & FORMAT_IS_PLANAR)
  414. DEB_S("planar pixelformat '%4.4s' not allowed for overlay\n",
  415. (char *)&fmt->pixelformat);
  416. /* check if overlay is running */
  417. if (IS_OVERLAY_ACTIVE(fh) != 0) {
  418. if (vv->video_fh != fh) {
  419. DEB_D("refusing to change framebuffer informations while overlay is active in another open\n");
  420. return -EBUSY;
  421. }
  422. }
  423. /* ok, accept it */
  424. vv->ov_fb = *fb;
  425. vv->ov_fmt = fmt;
  426. if (vv->ov_fb.fmt.bytesperline < vv->ov_fb.fmt.width) {
  427. vv->ov_fb.fmt.bytesperline = vv->ov_fb.fmt.width * fmt->depth / 8;
  428. DEB_D("setting bytesperline to %d\n", vv->ov_fb.fmt.bytesperline);
  429. }
  430. return 0;
  431. }
  432. static int vidioc_enum_fmt_vid_cap(struct file *file, void *fh, struct v4l2_fmtdesc *f)
  433. {
  434. if (f->index >= NUM_FORMATS)
  435. return -EINVAL;
  436. strlcpy((char *)f->description, formats[f->index].name,
  437. sizeof(f->description));
  438. f->pixelformat = formats[f->index].pixelformat;
  439. return 0;
  440. }
  441. int saa7146_s_ctrl(struct v4l2_ctrl *ctrl)
  442. {
  443. struct saa7146_dev *dev = container_of(ctrl->handler,
  444. struct saa7146_dev, ctrl_handler);
  445. struct saa7146_vv *vv = dev->vv_data;
  446. u32 val;
  447. switch (ctrl->id) {
  448. case V4L2_CID_BRIGHTNESS:
  449. val = saa7146_read(dev, BCS_CTRL);
  450. val &= 0x00ffffff;
  451. val |= (ctrl->val << 24);
  452. saa7146_write(dev, BCS_CTRL, val);
  453. saa7146_write(dev, MC2, MASK_22 | MASK_06);
  454. break;
  455. case V4L2_CID_CONTRAST:
  456. val = saa7146_read(dev, BCS_CTRL);
  457. val &= 0xff00ffff;
  458. val |= (ctrl->val << 16);
  459. saa7146_write(dev, BCS_CTRL, val);
  460. saa7146_write(dev, MC2, MASK_22 | MASK_06);
  461. break;
  462. case V4L2_CID_SATURATION:
  463. val = saa7146_read(dev, BCS_CTRL);
  464. val &= 0xffffff00;
  465. val |= (ctrl->val << 0);
  466. saa7146_write(dev, BCS_CTRL, val);
  467. saa7146_write(dev, MC2, MASK_22 | MASK_06);
  468. break;
  469. case V4L2_CID_HFLIP:
  470. /* fixme: we can support changing VFLIP and HFLIP here... */
  471. if ((vv->video_status & STATUS_CAPTURE))
  472. return -EBUSY;
  473. vv->hflip = ctrl->val;
  474. break;
  475. case V4L2_CID_VFLIP:
  476. if ((vv->video_status & STATUS_CAPTURE))
  477. return -EBUSY;
  478. vv->vflip = ctrl->val;
  479. break;
  480. default:
  481. return -EINVAL;
  482. }
  483. if ((vv->video_status & STATUS_OVERLAY) != 0) { /* CHECK: && (vv->video_fh == fh)) */
  484. struct saa7146_fh *fh = vv->video_fh;
  485. saa7146_stop_preview(fh);
  486. saa7146_start_preview(fh);
  487. }
  488. return 0;
  489. }
  490. static int vidioc_g_parm(struct file *file, void *fh,
  491. struct v4l2_streamparm *parm)
  492. {
  493. struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
  494. struct saa7146_vv *vv = dev->vv_data;
  495. if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  496. return -EINVAL;
  497. parm->parm.capture.readbuffers = 1;
  498. v4l2_video_std_frame_period(vv->standard->id,
  499. &parm->parm.capture.timeperframe);
  500. return 0;
  501. }
  502. static int vidioc_g_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f)
  503. {
  504. f->fmt.pix = ((struct saa7146_fh *)fh)->video_fmt;
  505. return 0;
  506. }
  507. static int vidioc_g_fmt_vid_overlay(struct file *file, void *fh, struct v4l2_format *f)
  508. {
  509. struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
  510. struct saa7146_vv *vv = dev->vv_data;
  511. f->fmt.win = vv->ov.win;
  512. return 0;
  513. }
  514. static int vidioc_g_fmt_vbi_cap(struct file *file, void *fh, struct v4l2_format *f)
  515. {
  516. f->fmt.vbi = ((struct saa7146_fh *)fh)->vbi_fmt;
  517. return 0;
  518. }
  519. static int vidioc_try_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f)
  520. {
  521. struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
  522. struct saa7146_vv *vv = dev->vv_data;
  523. struct saa7146_format *fmt;
  524. enum v4l2_field field;
  525. int maxw, maxh;
  526. int calc_bpl;
  527. DEB_EE("V4L2_BUF_TYPE_VIDEO_CAPTURE: dev:%p, fh:%p\n", dev, fh);
  528. fmt = saa7146_format_by_fourcc(dev, f->fmt.pix.pixelformat);
  529. if (NULL == fmt)
  530. return -EINVAL;
  531. field = f->fmt.pix.field;
  532. maxw = vv->standard->h_max_out;
  533. maxh = vv->standard->v_max_out;
  534. if (V4L2_FIELD_ANY == field) {
  535. field = (f->fmt.pix.height > maxh / 2)
  536. ? V4L2_FIELD_INTERLACED
  537. : V4L2_FIELD_BOTTOM;
  538. }
  539. switch (field) {
  540. case V4L2_FIELD_ALTERNATE:
  541. vv->last_field = V4L2_FIELD_TOP;
  542. maxh = maxh / 2;
  543. break;
  544. case V4L2_FIELD_TOP:
  545. case V4L2_FIELD_BOTTOM:
  546. vv->last_field = V4L2_FIELD_INTERLACED;
  547. maxh = maxh / 2;
  548. break;
  549. case V4L2_FIELD_INTERLACED:
  550. vv->last_field = V4L2_FIELD_INTERLACED;
  551. break;
  552. default:
  553. DEB_D("no known field mode '%d'\n", field);
  554. return -EINVAL;
  555. }
  556. f->fmt.pix.field = field;
  557. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  558. if (f->fmt.pix.width > maxw)
  559. f->fmt.pix.width = maxw;
  560. if (f->fmt.pix.height > maxh)
  561. f->fmt.pix.height = maxh;
  562. calc_bpl = (f->fmt.pix.width * fmt->depth) / 8;
  563. if (f->fmt.pix.bytesperline < calc_bpl)
  564. f->fmt.pix.bytesperline = calc_bpl;
  565. if (f->fmt.pix.bytesperline > (2 * PAGE_SIZE * fmt->depth) / 8) /* arbitrary constraint */
  566. f->fmt.pix.bytesperline = calc_bpl;
  567. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * f->fmt.pix.height;
  568. DEB_D("w:%d, h:%d, bytesperline:%d, sizeimage:%d\n",
  569. f->fmt.pix.width, f->fmt.pix.height,
  570. f->fmt.pix.bytesperline, f->fmt.pix.sizeimage);
  571. return 0;
  572. }
  573. static int vidioc_try_fmt_vid_overlay(struct file *file, void *fh, struct v4l2_format *f)
  574. {
  575. struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
  576. struct saa7146_vv *vv = dev->vv_data;
  577. struct v4l2_window *win = &f->fmt.win;
  578. enum v4l2_field field;
  579. int maxw, maxh;
  580. DEB_EE("dev:%p\n", dev);
  581. if (NULL == vv->ov_fb.base) {
  582. DEB_D("no fb base set\n");
  583. return -EINVAL;
  584. }
  585. if (NULL == vv->ov_fmt) {
  586. DEB_D("no fb fmt set\n");
  587. return -EINVAL;
  588. }
  589. if (win->w.width < 48 || win->w.height < 32) {
  590. DEB_D("min width/height. (%d,%d)\n",
  591. win->w.width, win->w.height);
  592. return -EINVAL;
  593. }
  594. if (win->clipcount > 16) {
  595. DEB_D("clipcount too big\n");
  596. return -EINVAL;
  597. }
  598. field = win->field;
  599. maxw = vv->standard->h_max_out;
  600. maxh = vv->standard->v_max_out;
  601. if (V4L2_FIELD_ANY == field) {
  602. field = (win->w.height > maxh / 2)
  603. ? V4L2_FIELD_INTERLACED
  604. : V4L2_FIELD_TOP;
  605. }
  606. switch (field) {
  607. case V4L2_FIELD_TOP:
  608. case V4L2_FIELD_BOTTOM:
  609. case V4L2_FIELD_ALTERNATE:
  610. maxh = maxh / 2;
  611. break;
  612. case V4L2_FIELD_INTERLACED:
  613. break;
  614. default:
  615. DEB_D("no known field mode '%d'\n", field);
  616. return -EINVAL;
  617. }
  618. win->field = field;
  619. if (win->w.width > maxw)
  620. win->w.width = maxw;
  621. if (win->w.height > maxh)
  622. win->w.height = maxh;
  623. return 0;
  624. }
  625. static int vidioc_s_fmt_vid_cap(struct file *file, void *__fh, struct v4l2_format *f)
  626. {
  627. struct saa7146_fh *fh = __fh;
  628. struct saa7146_dev *dev = fh->dev;
  629. struct saa7146_vv *vv = dev->vv_data;
  630. int err;
  631. DEB_EE("V4L2_BUF_TYPE_VIDEO_CAPTURE: dev:%p, fh:%p\n", dev, fh);
  632. if (IS_CAPTURE_ACTIVE(fh) != 0) {
  633. DEB_EE("streaming capture is active\n");
  634. return -EBUSY;
  635. }
  636. err = vidioc_try_fmt_vid_cap(file, fh, f);
  637. if (0 != err)
  638. return err;
  639. fh->video_fmt = f->fmt.pix;
  640. DEB_EE("set to pixelformat '%4.4s'\n",
  641. (char *)&fh->video_fmt.pixelformat);
  642. return 0;
  643. }
  644. static int vidioc_s_fmt_vid_overlay(struct file *file, void *__fh, struct v4l2_format *f)
  645. {
  646. struct saa7146_fh *fh = __fh;
  647. struct saa7146_dev *dev = fh->dev;
  648. struct saa7146_vv *vv = dev->vv_data;
  649. int err;
  650. DEB_EE("V4L2_BUF_TYPE_VIDEO_OVERLAY: dev:%p, fh:%p\n", dev, fh);
  651. err = vidioc_try_fmt_vid_overlay(file, fh, f);
  652. if (0 != err)
  653. return err;
  654. vv->ov.win = f->fmt.win;
  655. vv->ov.nclips = f->fmt.win.clipcount;
  656. if (vv->ov.nclips > 16)
  657. vv->ov.nclips = 16;
  658. if (copy_from_user(vv->ov.clips, f->fmt.win.clips,
  659. sizeof(struct v4l2_clip) * vv->ov.nclips)) {
  660. return -EFAULT;
  661. }
  662. /* vv->ov.fh is used to indicate that we have valid overlay informations, too */
  663. vv->ov.fh = fh;
  664. /* check if our current overlay is active */
  665. if (IS_OVERLAY_ACTIVE(fh) != 0) {
  666. saa7146_stop_preview(fh);
  667. saa7146_start_preview(fh);
  668. }
  669. return 0;
  670. }
  671. static int vidioc_g_std(struct file *file, void *fh, v4l2_std_id *norm)
  672. {
  673. struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
  674. struct saa7146_vv *vv = dev->vv_data;
  675. *norm = vv->standard->id;
  676. return 0;
  677. }
  678. /* the saa7146 supfhrts (used in conjunction with the saa7111a for example)
  679. PAL / NTSC / SECAM. if your hardware does not (or does more)
  680. -- override this function in your extension */
  681. /*
  682. case VIDIOC_ENUMSTD:
  683. {
  684. struct v4l2_standard *e = arg;
  685. if (e->index < 0 )
  686. return -EINVAL;
  687. if( e->index < dev->ext_vv_data->num_stds ) {
  688. DEB_EE("VIDIOC_ENUMSTD: index:%d\n", e->index);
  689. v4l2_video_std_construct(e, dev->ext_vv_data->stds[e->index].id, dev->ext_vv_data->stds[e->index].name);
  690. return 0;
  691. }
  692. return -EINVAL;
  693. }
  694. */
  695. static int vidioc_s_std(struct file *file, void *fh, v4l2_std_id *id)
  696. {
  697. struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
  698. struct saa7146_vv *vv = dev->vv_data;
  699. int found = 0;
  700. int err, i;
  701. DEB_EE("VIDIOC_S_STD\n");
  702. if ((vv->video_status & STATUS_CAPTURE) == STATUS_CAPTURE) {
  703. DEB_D("cannot change video standard while streaming capture is active\n");
  704. return -EBUSY;
  705. }
  706. if ((vv->video_status & STATUS_OVERLAY) != 0) {
  707. vv->ov_suspend = vv->video_fh;
  708. err = saa7146_stop_preview(vv->video_fh); /* side effect: video_status is now 0, video_fh is NULL */
  709. if (0 != err) {
  710. DEB_D("suspending video failed. aborting\n");
  711. return err;
  712. }
  713. }
  714. for (i = 0; i < dev->ext_vv_data->num_stds; i++)
  715. if (*id & dev->ext_vv_data->stds[i].id)
  716. break;
  717. if (i != dev->ext_vv_data->num_stds) {
  718. vv->standard = &dev->ext_vv_data->stds[i];
  719. if (NULL != dev->ext_vv_data->std_callback)
  720. dev->ext_vv_data->std_callback(dev, vv->standard);
  721. found = 1;
  722. }
  723. if (vv->ov_suspend != NULL) {
  724. saa7146_start_preview(vv->ov_suspend);
  725. vv->ov_suspend = NULL;
  726. }
  727. if (!found) {
  728. DEB_EE("VIDIOC_S_STD: standard not found\n");
  729. return -EINVAL;
  730. }
  731. DEB_EE("VIDIOC_S_STD: set to standard to '%s'\n", vv->standard->name);
  732. return 0;
  733. }
  734. static int vidioc_overlay(struct file *file, void *fh, unsigned int on)
  735. {
  736. int err;
  737. DEB_D("VIDIOC_OVERLAY on:%d\n", on);
  738. if (on)
  739. err = saa7146_start_preview(fh);
  740. else
  741. err = saa7146_stop_preview(fh);
  742. return err;
  743. }
  744. static int vidioc_reqbufs(struct file *file, void *__fh, struct v4l2_requestbuffers *b)
  745. {
  746. struct saa7146_fh *fh = __fh;
  747. if (b->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  748. return videobuf_reqbufs(&fh->video_q, b);
  749. if (b->type == V4L2_BUF_TYPE_VBI_CAPTURE)
  750. return videobuf_reqbufs(&fh->vbi_q, b);
  751. return -EINVAL;
  752. }
  753. static int vidioc_querybuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
  754. {
  755. struct saa7146_fh *fh = __fh;
  756. if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  757. return videobuf_querybuf(&fh->video_q, buf);
  758. if (buf->type == V4L2_BUF_TYPE_VBI_CAPTURE)
  759. return videobuf_querybuf(&fh->vbi_q, buf);
  760. return -EINVAL;
  761. }
  762. static int vidioc_qbuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
  763. {
  764. struct saa7146_fh *fh = __fh;
  765. if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  766. return videobuf_qbuf(&fh->video_q, buf);
  767. if (buf->type == V4L2_BUF_TYPE_VBI_CAPTURE)
  768. return videobuf_qbuf(&fh->vbi_q, buf);
  769. return -EINVAL;
  770. }
  771. static int vidioc_dqbuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
  772. {
  773. struct saa7146_fh *fh = __fh;
  774. if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  775. return videobuf_dqbuf(&fh->video_q, buf, file->f_flags & O_NONBLOCK);
  776. if (buf->type == V4L2_BUF_TYPE_VBI_CAPTURE)
  777. return videobuf_dqbuf(&fh->vbi_q, buf, file->f_flags & O_NONBLOCK);
  778. return -EINVAL;
  779. }
  780. static int vidioc_streamon(struct file *file, void *__fh, enum v4l2_buf_type type)
  781. {
  782. struct saa7146_fh *fh = __fh;
  783. int err;
  784. DEB_D("VIDIOC_STREAMON, type:%d\n", type);
  785. err = video_begin(fh);
  786. if (err)
  787. return err;
  788. if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  789. return videobuf_streamon(&fh->video_q);
  790. if (type == V4L2_BUF_TYPE_VBI_CAPTURE)
  791. return videobuf_streamon(&fh->vbi_q);
  792. return -EINVAL;
  793. }
  794. static int vidioc_streamoff(struct file *file, void *__fh, enum v4l2_buf_type type)
  795. {
  796. struct saa7146_fh *fh = __fh;
  797. struct saa7146_dev *dev = fh->dev;
  798. struct saa7146_vv *vv = dev->vv_data;
  799. int err;
  800. DEB_D("VIDIOC_STREAMOFF, type:%d\n", type);
  801. /* ugly: we need to copy some checks from video_end(),
  802. because videobuf_streamoff() relies on the capture running.
  803. check and fix this */
  804. if ((vv->video_status & STATUS_CAPTURE) != STATUS_CAPTURE) {
  805. DEB_S("not capturing\n");
  806. return 0;
  807. }
  808. if (vv->video_fh != fh) {
  809. DEB_S("capturing, but in another open\n");
  810. return -EBUSY;
  811. }
  812. err = -EINVAL;
  813. if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  814. err = videobuf_streamoff(&fh->video_q);
  815. else if (type == V4L2_BUF_TYPE_VBI_CAPTURE)
  816. err = videobuf_streamoff(&fh->vbi_q);
  817. if (0 != err) {
  818. DEB_D("warning: videobuf_streamoff() failed\n");
  819. video_end(fh, file);
  820. } else {
  821. err = video_end(fh, file);
  822. }
  823. return err;
  824. }
  825. static int vidioc_g_chip_ident(struct file *file, void *__fh,
  826. struct v4l2_dbg_chip_ident *chip)
  827. {
  828. struct saa7146_fh *fh = __fh;
  829. struct saa7146_dev *dev = fh->dev;
  830. chip->ident = V4L2_IDENT_NONE;
  831. chip->revision = 0;
  832. if (chip->match.type == V4L2_CHIP_MATCH_HOST && !chip->match.addr) {
  833. chip->ident = V4L2_IDENT_SAA7146;
  834. return 0;
  835. }
  836. return v4l2_device_call_until_err(&dev->v4l2_dev, 0,
  837. core, g_chip_ident, chip);
  838. }
  839. const struct v4l2_ioctl_ops saa7146_video_ioctl_ops = {
  840. .vidioc_querycap = vidioc_querycap,
  841. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  842. .vidioc_enum_fmt_vid_overlay = vidioc_enum_fmt_vid_cap,
  843. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  844. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  845. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  846. .vidioc_g_fmt_vid_overlay = vidioc_g_fmt_vid_overlay,
  847. .vidioc_try_fmt_vid_overlay = vidioc_try_fmt_vid_overlay,
  848. .vidioc_s_fmt_vid_overlay = vidioc_s_fmt_vid_overlay,
  849. .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
  850. .vidioc_g_chip_ident = vidioc_g_chip_ident,
  851. .vidioc_overlay = vidioc_overlay,
  852. .vidioc_g_fbuf = vidioc_g_fbuf,
  853. .vidioc_s_fbuf = vidioc_s_fbuf,
  854. .vidioc_reqbufs = vidioc_reqbufs,
  855. .vidioc_querybuf = vidioc_querybuf,
  856. .vidioc_qbuf = vidioc_qbuf,
  857. .vidioc_dqbuf = vidioc_dqbuf,
  858. .vidioc_g_std = vidioc_g_std,
  859. .vidioc_s_std = vidioc_s_std,
  860. .vidioc_streamon = vidioc_streamon,
  861. .vidioc_streamoff = vidioc_streamoff,
  862. .vidioc_g_parm = vidioc_g_parm,
  863. };
  864. /*********************************************************************************/
  865. /* buffer handling functions */
  866. static int buffer_activate (struct saa7146_dev *dev,
  867. struct saa7146_buf *buf,
  868. struct saa7146_buf *next)
  869. {
  870. struct saa7146_vv *vv = dev->vv_data;
  871. buf->vb.state = VIDEOBUF_ACTIVE;
  872. saa7146_set_capture(dev,buf,next);
  873. mod_timer(&vv->video_q.timeout, jiffies+BUFFER_TIMEOUT);
  874. return 0;
  875. }
  876. static void release_all_pagetables(struct saa7146_dev *dev, struct saa7146_buf *buf)
  877. {
  878. saa7146_pgtable_free(dev->pci, &buf->pt[0]);
  879. saa7146_pgtable_free(dev->pci, &buf->pt[1]);
  880. saa7146_pgtable_free(dev->pci, &buf->pt[2]);
  881. }
  882. static int buffer_prepare(struct videobuf_queue *q,
  883. struct videobuf_buffer *vb, enum v4l2_field field)
  884. {
  885. struct file *file = q->priv_data;
  886. struct saa7146_fh *fh = file->private_data;
  887. struct saa7146_dev *dev = fh->dev;
  888. struct saa7146_vv *vv = dev->vv_data;
  889. struct saa7146_buf *buf = (struct saa7146_buf *)vb;
  890. int size,err = 0;
  891. DEB_CAP("vbuf:%p\n", vb);
  892. /* sanity checks */
  893. if (fh->video_fmt.width < 48 ||
  894. fh->video_fmt.height < 32 ||
  895. fh->video_fmt.width > vv->standard->h_max_out ||
  896. fh->video_fmt.height > vv->standard->v_max_out) {
  897. DEB_D("w (%d) / h (%d) out of bounds\n",
  898. fh->video_fmt.width, fh->video_fmt.height);
  899. return -EINVAL;
  900. }
  901. size = fh->video_fmt.sizeimage;
  902. if (0 != buf->vb.baddr && buf->vb.bsize < size) {
  903. DEB_D("size mismatch\n");
  904. return -EINVAL;
  905. }
  906. DEB_CAP("buffer_prepare [size=%dx%d,bytes=%d,fields=%s]\n",
  907. fh->video_fmt.width, fh->video_fmt.height,
  908. size, v4l2_field_names[fh->video_fmt.field]);
  909. if (buf->vb.width != fh->video_fmt.width ||
  910. buf->vb.bytesperline != fh->video_fmt.bytesperline ||
  911. buf->vb.height != fh->video_fmt.height ||
  912. buf->vb.size != size ||
  913. buf->vb.field != field ||
  914. buf->vb.field != fh->video_fmt.field ||
  915. buf->fmt != &fh->video_fmt) {
  916. saa7146_dma_free(dev,q,buf);
  917. }
  918. if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
  919. struct saa7146_format *sfmt;
  920. buf->vb.bytesperline = fh->video_fmt.bytesperline;
  921. buf->vb.width = fh->video_fmt.width;
  922. buf->vb.height = fh->video_fmt.height;
  923. buf->vb.size = size;
  924. buf->vb.field = field;
  925. buf->fmt = &fh->video_fmt;
  926. buf->vb.field = fh->video_fmt.field;
  927. sfmt = saa7146_format_by_fourcc(dev,buf->fmt->pixelformat);
  928. release_all_pagetables(dev, buf);
  929. if( 0 != IS_PLANAR(sfmt->trans)) {
  930. saa7146_pgtable_alloc(dev->pci, &buf->pt[0]);
  931. saa7146_pgtable_alloc(dev->pci, &buf->pt[1]);
  932. saa7146_pgtable_alloc(dev->pci, &buf->pt[2]);
  933. } else {
  934. saa7146_pgtable_alloc(dev->pci, &buf->pt[0]);
  935. }
  936. err = videobuf_iolock(q,&buf->vb, &vv->ov_fb);
  937. if (err)
  938. goto oops;
  939. err = saa7146_pgtable_build(dev,buf);
  940. if (err)
  941. goto oops;
  942. }
  943. buf->vb.state = VIDEOBUF_PREPARED;
  944. buf->activate = buffer_activate;
  945. return 0;
  946. oops:
  947. DEB_D("error out\n");
  948. saa7146_dma_free(dev,q,buf);
  949. return err;
  950. }
  951. static int buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
  952. {
  953. struct file *file = q->priv_data;
  954. struct saa7146_fh *fh = file->private_data;
  955. if (0 == *count || *count > MAX_SAA7146_CAPTURE_BUFFERS)
  956. *count = MAX_SAA7146_CAPTURE_BUFFERS;
  957. *size = fh->video_fmt.sizeimage;
  958. /* check if we exceed the "max_memory" parameter */
  959. if( (*count * *size) > (max_memory*1048576) ) {
  960. *count = (max_memory*1048576) / *size;
  961. }
  962. DEB_CAP("%d buffers, %d bytes each\n", *count, *size);
  963. return 0;
  964. }
  965. static void buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
  966. {
  967. struct file *file = q->priv_data;
  968. struct saa7146_fh *fh = file->private_data;
  969. struct saa7146_dev *dev = fh->dev;
  970. struct saa7146_vv *vv = dev->vv_data;
  971. struct saa7146_buf *buf = (struct saa7146_buf *)vb;
  972. DEB_CAP("vbuf:%p\n", vb);
  973. saa7146_buffer_queue(fh->dev,&vv->video_q,buf);
  974. }
  975. static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
  976. {
  977. struct file *file = q->priv_data;
  978. struct saa7146_fh *fh = file->private_data;
  979. struct saa7146_dev *dev = fh->dev;
  980. struct saa7146_buf *buf = (struct saa7146_buf *)vb;
  981. DEB_CAP("vbuf:%p\n", vb);
  982. saa7146_dma_free(dev,q,buf);
  983. release_all_pagetables(dev, buf);
  984. }
  985. static struct videobuf_queue_ops video_qops = {
  986. .buf_setup = buffer_setup,
  987. .buf_prepare = buffer_prepare,
  988. .buf_queue = buffer_queue,
  989. .buf_release = buffer_release,
  990. };
  991. /********************************************************************************/
  992. /* file operations */
  993. static void video_init(struct saa7146_dev *dev, struct saa7146_vv *vv)
  994. {
  995. INIT_LIST_HEAD(&vv->video_q.queue);
  996. init_timer(&vv->video_q.timeout);
  997. vv->video_q.timeout.function = saa7146_buffer_timeout;
  998. vv->video_q.timeout.data = (unsigned long)(&vv->video_q);
  999. vv->video_q.dev = dev;
  1000. /* set some default values */
  1001. vv->standard = &dev->ext_vv_data->stds[0];
  1002. /* FIXME: what's this? */
  1003. vv->current_hps_source = SAA7146_HPS_SOURCE_PORT_A;
  1004. vv->current_hps_sync = SAA7146_HPS_SYNC_PORT_A;
  1005. }
  1006. static int video_open(struct saa7146_dev *dev, struct file *file)
  1007. {
  1008. struct saa7146_fh *fh = file->private_data;
  1009. struct saa7146_format *sfmt;
  1010. fh->video_fmt.width = 384;
  1011. fh->video_fmt.height = 288;
  1012. fh->video_fmt.pixelformat = V4L2_PIX_FMT_BGR24;
  1013. fh->video_fmt.bytesperline = 0;
  1014. fh->video_fmt.field = V4L2_FIELD_ANY;
  1015. fh->video_fmt.colorspace = V4L2_COLORSPACE_SMPTE170M;
  1016. sfmt = saa7146_format_by_fourcc(dev,fh->video_fmt.pixelformat);
  1017. fh->video_fmt.sizeimage = (fh->video_fmt.width * fh->video_fmt.height * sfmt->depth)/8;
  1018. videobuf_queue_sg_init(&fh->video_q, &video_qops,
  1019. &dev->pci->dev, &dev->slock,
  1020. V4L2_BUF_TYPE_VIDEO_CAPTURE,
  1021. V4L2_FIELD_INTERLACED,
  1022. sizeof(struct saa7146_buf),
  1023. file, &dev->v4l2_lock);
  1024. return 0;
  1025. }
  1026. static void video_close(struct saa7146_dev *dev, struct file *file)
  1027. {
  1028. struct saa7146_fh *fh = file->private_data;
  1029. struct saa7146_vv *vv = dev->vv_data;
  1030. struct videobuf_queue *q = &fh->video_q;
  1031. if (IS_CAPTURE_ACTIVE(fh) != 0)
  1032. video_end(fh, file);
  1033. else if (IS_OVERLAY_ACTIVE(fh) != 0)
  1034. saa7146_stop_preview(fh);
  1035. videobuf_stop(q);
  1036. /* hmm, why is this function declared void? */
  1037. }
  1038. static void video_irq_done(struct saa7146_dev *dev, unsigned long st)
  1039. {
  1040. struct saa7146_vv *vv = dev->vv_data;
  1041. struct saa7146_dmaqueue *q = &vv->video_q;
  1042. spin_lock(&dev->slock);
  1043. DEB_CAP("called\n");
  1044. /* only finish the buffer if we have one... */
  1045. if( NULL != q->curr ) {
  1046. saa7146_buffer_finish(dev,q,VIDEOBUF_DONE);
  1047. }
  1048. saa7146_buffer_next(dev,q,0);
  1049. spin_unlock(&dev->slock);
  1050. }
  1051. static ssize_t video_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
  1052. {
  1053. struct saa7146_fh *fh = file->private_data;
  1054. struct saa7146_dev *dev = fh->dev;
  1055. struct saa7146_vv *vv = dev->vv_data;
  1056. ssize_t ret = 0;
  1057. DEB_EE("called\n");
  1058. if ((vv->video_status & STATUS_CAPTURE) != 0) {
  1059. /* fixme: should we allow read() captures while streaming capture? */
  1060. if (vv->video_fh == fh) {
  1061. DEB_S("already capturing\n");
  1062. return -EBUSY;
  1063. }
  1064. DEB_S("already capturing in another open\n");
  1065. return -EBUSY;
  1066. }
  1067. ret = video_begin(fh);
  1068. if( 0 != ret) {
  1069. goto out;
  1070. }
  1071. ret = videobuf_read_one(&fh->video_q , data, count, ppos,
  1072. file->f_flags & O_NONBLOCK);
  1073. if (ret != 0) {
  1074. video_end(fh, file);
  1075. } else {
  1076. ret = video_end(fh, file);
  1077. }
  1078. out:
  1079. /* restart overlay if it was active before */
  1080. if (vv->ov_suspend != NULL) {
  1081. saa7146_start_preview(vv->ov_suspend);
  1082. vv->ov_suspend = NULL;
  1083. }
  1084. return ret;
  1085. }
  1086. struct saa7146_use_ops saa7146_video_uops = {
  1087. .init = video_init,
  1088. .open = video_open,
  1089. .release = video_close,
  1090. .irq_done = video_irq_done,
  1091. .read = video_read,
  1092. };