saa7146_video.c 36 KB

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