saa7146_video.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509
  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. int saa7146_stop_preview(struct saa7146_fh *fh)
  279. {
  280. struct saa7146_dev *dev = fh->dev;
  281. struct saa7146_vv *vv = dev->vv_data;
  282. DEB_EE(("dev:%p, fh:%p\n",dev,fh));
  283. /* check if streaming capture is running */
  284. if (IS_CAPTURE_ACTIVE(fh) != 0) {
  285. DEB_D(("streaming capture is active.\n"));
  286. return -EBUSY;
  287. }
  288. /* check if overlay is running at all */
  289. if ((vv->video_status & STATUS_OVERLAY) == 0) {
  290. DEB_D(("no active overlay.\n"));
  291. return 0;
  292. }
  293. if (vv->video_fh != fh) {
  294. DEB_D(("overlay is active, but in another open.\n"));
  295. return -EBUSY;
  296. }
  297. vv->video_status = 0;
  298. vv->video_fh = NULL;
  299. saa7146_disable_overlay(fh);
  300. saa7146_res_free(fh, RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP);
  301. return 0;
  302. }
  303. static int s_fmt(struct saa7146_fh *fh, struct v4l2_format *f)
  304. {
  305. struct saa7146_dev *dev = fh->dev;
  306. struct saa7146_vv *vv = dev->vv_data;
  307. int err;
  308. switch (f->type) {
  309. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  310. DEB_EE(("V4L2_BUF_TYPE_VIDEO_CAPTURE: dev:%p, fh:%p\n",dev,fh));
  311. if (IS_CAPTURE_ACTIVE(fh) != 0) {
  312. DEB_EE(("streaming capture is active\n"));
  313. return -EBUSY;
  314. }
  315. err = try_fmt(fh,f);
  316. if (0 != err)
  317. return err;
  318. fh->video_fmt = f->fmt.pix;
  319. DEB_EE(("set to pixelformat '%4.4s'\n",(char *)&fh->video_fmt.pixelformat));
  320. return 0;
  321. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  322. DEB_EE(("V4L2_BUF_TYPE_VIDEO_OVERLAY: dev:%p, fh:%p\n",dev,fh));
  323. err = try_win(dev,&f->fmt.win);
  324. if (0 != err)
  325. return err;
  326. down(&dev->lock);
  327. fh->ov.win = f->fmt.win;
  328. fh->ov.nclips = f->fmt.win.clipcount;
  329. if (fh->ov.nclips > 16)
  330. fh->ov.nclips = 16;
  331. if (copy_from_user(fh->ov.clips,f->fmt.win.clips,sizeof(struct v4l2_clip)*fh->ov.nclips)) {
  332. up(&dev->lock);
  333. return -EFAULT;
  334. }
  335. /* fh->ov.fh is used to indicate that we have valid overlay informations, too */
  336. fh->ov.fh = fh;
  337. up(&dev->lock);
  338. /* check if our current overlay is active */
  339. if (IS_OVERLAY_ACTIVE(fh) != 0) {
  340. saa7146_stop_preview(fh);
  341. saa7146_start_preview(fh);
  342. }
  343. return 0;
  344. default:
  345. DEB_D(("unknown format type '%d'\n",f->type));
  346. return -EINVAL;
  347. }
  348. }
  349. /********************************************************************************/
  350. /* device controls */
  351. static struct v4l2_queryctrl controls[] = {
  352. {
  353. .id = V4L2_CID_BRIGHTNESS,
  354. .name = "Brightness",
  355. .minimum = 0,
  356. .maximum = 255,
  357. .step = 1,
  358. .default_value = 128,
  359. .type = V4L2_CTRL_TYPE_INTEGER,
  360. },{
  361. .id = V4L2_CID_CONTRAST,
  362. .name = "Contrast",
  363. .minimum = 0,
  364. .maximum = 127,
  365. .step = 1,
  366. .default_value = 64,
  367. .type = V4L2_CTRL_TYPE_INTEGER,
  368. },{
  369. .id = V4L2_CID_SATURATION,
  370. .name = "Saturation",
  371. .minimum = 0,
  372. .maximum = 127,
  373. .step = 1,
  374. .default_value = 64,
  375. .type = V4L2_CTRL_TYPE_INTEGER,
  376. },{
  377. .id = V4L2_CID_VFLIP,
  378. .name = "Vertical flip",
  379. .minimum = 0,
  380. .maximum = 1,
  381. .type = V4L2_CTRL_TYPE_BOOLEAN,
  382. },{
  383. .id = V4L2_CID_HFLIP,
  384. .name = "Horizontal flip",
  385. .minimum = 0,
  386. .maximum = 1,
  387. .type = V4L2_CTRL_TYPE_BOOLEAN,
  388. },
  389. };
  390. static int NUM_CONTROLS = sizeof(controls)/sizeof(struct v4l2_queryctrl);
  391. #define V4L2_CID_PRIVATE_LASTP1 (V4L2_CID_PRIVATE_BASE + 0)
  392. static struct v4l2_queryctrl* ctrl_by_id(int id)
  393. {
  394. int i;
  395. for (i = 0; i < NUM_CONTROLS; i++)
  396. if (controls[i].id == id)
  397. return controls+i;
  398. return NULL;
  399. }
  400. static int get_control(struct saa7146_fh *fh, struct v4l2_control *c)
  401. {
  402. struct saa7146_dev *dev = fh->dev;
  403. struct saa7146_vv *vv = dev->vv_data;
  404. const struct v4l2_queryctrl* ctrl;
  405. u32 value = 0;
  406. ctrl = ctrl_by_id(c->id);
  407. if (NULL == ctrl)
  408. return -EINVAL;
  409. switch (c->id) {
  410. case V4L2_CID_BRIGHTNESS:
  411. value = saa7146_read(dev, BCS_CTRL);
  412. c->value = 0xff & (value >> 24);
  413. DEB_D(("V4L2_CID_BRIGHTNESS: %d\n",c->value));
  414. break;
  415. case V4L2_CID_CONTRAST:
  416. value = saa7146_read(dev, BCS_CTRL);
  417. c->value = 0x7f & (value >> 16);
  418. DEB_D(("V4L2_CID_CONTRAST: %d\n",c->value));
  419. break;
  420. case V4L2_CID_SATURATION:
  421. value = saa7146_read(dev, BCS_CTRL);
  422. c->value = 0x7f & (value >> 0);
  423. DEB_D(("V4L2_CID_SATURATION: %d\n",c->value));
  424. break;
  425. case V4L2_CID_VFLIP:
  426. c->value = vv->vflip;
  427. DEB_D(("V4L2_CID_VFLIP: %d\n",c->value));
  428. break;
  429. case V4L2_CID_HFLIP:
  430. c->value = vv->hflip;
  431. DEB_D(("V4L2_CID_HFLIP: %d\n",c->value));
  432. break;
  433. default:
  434. return -EINVAL;
  435. }
  436. return 0;
  437. }
  438. static int set_control(struct saa7146_fh *fh, struct v4l2_control *c)
  439. {
  440. struct saa7146_dev *dev = fh->dev;
  441. struct saa7146_vv *vv = dev->vv_data;
  442. const struct v4l2_queryctrl* ctrl;
  443. ctrl = ctrl_by_id(c->id);
  444. if (NULL == ctrl) {
  445. DEB_D(("unknown control %d\n",c->id));
  446. return -EINVAL;
  447. }
  448. down(&dev->lock);
  449. switch (ctrl->type) {
  450. case V4L2_CTRL_TYPE_BOOLEAN:
  451. case V4L2_CTRL_TYPE_MENU:
  452. case V4L2_CTRL_TYPE_INTEGER:
  453. if (c->value < ctrl->minimum)
  454. c->value = ctrl->minimum;
  455. if (c->value > ctrl->maximum)
  456. c->value = ctrl->maximum;
  457. break;
  458. default:
  459. /* nothing */;
  460. };
  461. switch (c->id) {
  462. case V4L2_CID_BRIGHTNESS: {
  463. u32 value = saa7146_read(dev, BCS_CTRL);
  464. value &= 0x00ffffff;
  465. value |= (c->value << 24);
  466. saa7146_write(dev, BCS_CTRL, value);
  467. saa7146_write(dev, MC2, MASK_22 | MASK_06 );
  468. break;
  469. }
  470. case V4L2_CID_CONTRAST: {
  471. u32 value = saa7146_read(dev, BCS_CTRL);
  472. value &= 0xff00ffff;
  473. value |= (c->value << 16);
  474. saa7146_write(dev, BCS_CTRL, value);
  475. saa7146_write(dev, MC2, MASK_22 | MASK_06 );
  476. break;
  477. }
  478. case V4L2_CID_SATURATION: {
  479. u32 value = saa7146_read(dev, BCS_CTRL);
  480. value &= 0xffffff00;
  481. value |= (c->value << 0);
  482. saa7146_write(dev, BCS_CTRL, value);
  483. saa7146_write(dev, MC2, MASK_22 | MASK_06 );
  484. break;
  485. }
  486. case V4L2_CID_HFLIP:
  487. /* fixme: we can support changing VFLIP and HFLIP here... */
  488. if (IS_CAPTURE_ACTIVE(fh) != 0) {
  489. DEB_D(("V4L2_CID_HFLIP while active capture.\n"));
  490. up(&dev->lock);
  491. return -EINVAL;
  492. }
  493. vv->hflip = c->value;
  494. break;
  495. case V4L2_CID_VFLIP:
  496. if (IS_CAPTURE_ACTIVE(fh) != 0) {
  497. DEB_D(("V4L2_CID_VFLIP while active capture.\n"));
  498. up(&dev->lock);
  499. return -EINVAL;
  500. }
  501. vv->vflip = c->value;
  502. break;
  503. default: {
  504. return -EINVAL;
  505. }
  506. }
  507. up(&dev->lock);
  508. if (IS_OVERLAY_ACTIVE(fh) != 0) {
  509. saa7146_stop_preview(fh);
  510. saa7146_start_preview(fh);
  511. }
  512. return 0;
  513. }
  514. /********************************************************************************/
  515. /* common pagetable functions */
  516. static int saa7146_pgtable_build(struct saa7146_dev *dev, struct saa7146_buf *buf)
  517. {
  518. struct pci_dev *pci = dev->pci;
  519. struct scatterlist *list = buf->vb.dma.sglist;
  520. int length = buf->vb.dma.sglen;
  521. struct saa7146_format *sfmt = format_by_fourcc(dev,buf->fmt->pixelformat);
  522. DEB_EE(("dev:%p, buf:%p, sg_len:%d\n",dev,buf,length));
  523. if( 0 != IS_PLANAR(sfmt->trans)) {
  524. struct saa7146_pgtable *pt1 = &buf->pt[0];
  525. struct saa7146_pgtable *pt2 = &buf->pt[1];
  526. struct saa7146_pgtable *pt3 = &buf->pt[2];
  527. u32 *ptr1, *ptr2, *ptr3;
  528. u32 fill;
  529. int size = buf->fmt->width*buf->fmt->height;
  530. int i,p,m1,m2,m3,o1,o2;
  531. switch( sfmt->depth ) {
  532. case 12: {
  533. /* create some offsets inside the page table */
  534. m1 = ((size+PAGE_SIZE)/PAGE_SIZE)-1;
  535. m2 = ((size+(size/4)+PAGE_SIZE)/PAGE_SIZE)-1;
  536. m3 = ((size+(size/2)+PAGE_SIZE)/PAGE_SIZE)-1;
  537. o1 = size%PAGE_SIZE;
  538. o2 = (size+(size/4))%PAGE_SIZE;
  539. DEB_CAP(("size:%d, m1:%d, m2:%d, m3:%d, o1:%d, o2:%d\n",size,m1,m2,m3,o1,o2));
  540. break;
  541. }
  542. case 16: {
  543. /* create some offsets inside the page table */
  544. m1 = ((size+PAGE_SIZE)/PAGE_SIZE)-1;
  545. m2 = ((size+(size/2)+PAGE_SIZE)/PAGE_SIZE)-1;
  546. m3 = ((2*size+PAGE_SIZE)/PAGE_SIZE)-1;
  547. o1 = size%PAGE_SIZE;
  548. o2 = (size+(size/2))%PAGE_SIZE;
  549. DEB_CAP(("size:%d, m1:%d, m2:%d, m3:%d, o1:%d, o2:%d\n",size,m1,m2,m3,o1,o2));
  550. break;
  551. }
  552. default: {
  553. return -1;
  554. }
  555. }
  556. ptr1 = pt1->cpu;
  557. ptr2 = pt2->cpu;
  558. ptr3 = pt3->cpu;
  559. /* walk all pages, copy all page addresses to ptr1 */
  560. for (i = 0; i < length; i++, list++) {
  561. for (p = 0; p * 4096 < list->length; p++, ptr1++) {
  562. *ptr1 = cpu_to_le32(sg_dma_address(list) - list->offset);
  563. }
  564. }
  565. /*
  566. ptr1 = pt1->cpu;
  567. for(j=0;j<40;j++) {
  568. printk("ptr1 %d: 0x%08x\n",j,ptr1[j]);
  569. }
  570. */
  571. /* if we have a user buffer, the first page may not be
  572. aligned to a page boundary. */
  573. pt1->offset = buf->vb.dma.sglist->offset;
  574. pt2->offset = pt1->offset+o1;
  575. pt3->offset = pt1->offset+o2;
  576. /* create video-dma2 page table */
  577. ptr1 = pt1->cpu;
  578. for(i = m1; i <= m2 ; i++, ptr2++) {
  579. *ptr2 = ptr1[i];
  580. }
  581. fill = *(ptr2-1);
  582. for(;i<1024;i++,ptr2++) {
  583. *ptr2 = fill;
  584. }
  585. /* create video-dma3 page table */
  586. ptr1 = pt1->cpu;
  587. for(i = m2; i <= m3; i++,ptr3++) {
  588. *ptr3 = ptr1[i];
  589. }
  590. fill = *(ptr3-1);
  591. for(;i<1024;i++,ptr3++) {
  592. *ptr3 = fill;
  593. }
  594. /* finally: finish up video-dma1 page table */
  595. ptr1 = pt1->cpu+m1;
  596. fill = pt1->cpu[m1];
  597. for(i=m1;i<1024;i++,ptr1++) {
  598. *ptr1 = fill;
  599. }
  600. /*
  601. ptr1 = pt1->cpu;
  602. ptr2 = pt2->cpu;
  603. ptr3 = pt3->cpu;
  604. for(j=0;j<40;j++) {
  605. printk("ptr1 %d: 0x%08x\n",j,ptr1[j]);
  606. }
  607. for(j=0;j<40;j++) {
  608. printk("ptr2 %d: 0x%08x\n",j,ptr2[j]);
  609. }
  610. for(j=0;j<40;j++) {
  611. printk("ptr3 %d: 0x%08x\n",j,ptr3[j]);
  612. }
  613. */
  614. } else {
  615. struct saa7146_pgtable *pt = &buf->pt[0];
  616. return saa7146_pgtable_build_single(pci, pt, list, length);
  617. }
  618. return 0;
  619. }
  620. /********************************************************************************/
  621. /* file operations */
  622. static int video_begin(struct saa7146_fh *fh)
  623. {
  624. struct saa7146_dev *dev = fh->dev;
  625. struct saa7146_vv *vv = dev->vv_data;
  626. struct saa7146_format *fmt = NULL;
  627. unsigned int resource;
  628. int ret = 0, err = 0;
  629. DEB_EE(("dev:%p, fh:%p\n",dev,fh));
  630. if ((vv->video_status & STATUS_CAPTURE) != 0) {
  631. if (vv->video_fh == fh) {
  632. DEB_S(("already capturing.\n"));
  633. return 0;
  634. }
  635. DEB_S(("already capturing in another open.\n"));
  636. return -EBUSY;
  637. }
  638. if ((vv->video_status & STATUS_OVERLAY) != 0) {
  639. DEB_S(("warning: suspending overlay video for streaming capture.\n"));
  640. vv->ov_suspend = vv->video_fh;
  641. err = saa7146_stop_preview(vv->video_fh); /* side effect: video_status is now 0, video_fh is NULL */
  642. if (0 != err) {
  643. DEB_D(("suspending video failed. aborting\n"));
  644. return err;
  645. }
  646. }
  647. fmt = format_by_fourcc(dev,fh->video_fmt.pixelformat);
  648. /* we need to have a valid format set here */
  649. BUG_ON(NULL == fmt);
  650. if (0 != (fmt->flags & FORMAT_IS_PLANAR)) {
  651. resource = RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP|RESOURCE_DMA3_BRS;
  652. } else {
  653. resource = RESOURCE_DMA1_HPS;
  654. }
  655. ret = saa7146_res_get(fh, resource);
  656. if (0 == ret) {
  657. DEB_S(("cannot get capture resource %d\n",resource));
  658. if (vv->ov_suspend != NULL) {
  659. saa7146_start_preview(vv->ov_suspend);
  660. vv->ov_suspend = NULL;
  661. }
  662. return -EBUSY;
  663. }
  664. /* clear out beginning of streaming bit (rps register 0)*/
  665. saa7146_write(dev, MC2, MASK_27 );
  666. /* enable rps0 irqs */
  667. SAA7146_IER_ENABLE(dev, MASK_27);
  668. vv->video_fh = fh;
  669. vv->video_status = STATUS_CAPTURE;
  670. return 0;
  671. }
  672. static int video_end(struct saa7146_fh *fh, struct file *file)
  673. {
  674. struct saa7146_dev *dev = fh->dev;
  675. struct saa7146_vv *vv = dev->vv_data;
  676. struct saa7146_format *fmt = NULL;
  677. unsigned long flags;
  678. unsigned int resource;
  679. u32 dmas = 0;
  680. DEB_EE(("dev:%p, fh:%p\n",dev,fh));
  681. if ((vv->video_status & STATUS_CAPTURE) != STATUS_CAPTURE) {
  682. DEB_S(("not capturing.\n"));
  683. return 0;
  684. }
  685. if (vv->video_fh != fh) {
  686. DEB_S(("capturing, but in another open.\n"));
  687. return -EBUSY;
  688. }
  689. fmt = format_by_fourcc(dev,fh->video_fmt.pixelformat);
  690. /* we need to have a valid format set here */
  691. BUG_ON(NULL == fmt);
  692. if (0 != (fmt->flags & FORMAT_IS_PLANAR)) {
  693. resource = RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP|RESOURCE_DMA3_BRS;
  694. dmas = MASK_22 | MASK_21 | MASK_20;
  695. } else {
  696. resource = RESOURCE_DMA1_HPS;
  697. dmas = MASK_22;
  698. }
  699. spin_lock_irqsave(&dev->slock,flags);
  700. /* disable rps0 */
  701. saa7146_write(dev, MC1, MASK_28);
  702. /* disable rps0 irqs */
  703. SAA7146_IER_DISABLE(dev, MASK_27);
  704. /* shut down all used video dma transfers */
  705. saa7146_write(dev, MC1, dmas);
  706. spin_unlock_irqrestore(&dev->slock, flags);
  707. vv->video_fh = NULL;
  708. vv->video_status = 0;
  709. saa7146_res_free(fh, resource);
  710. if (vv->ov_suspend != NULL) {
  711. saa7146_start_preview(vv->ov_suspend);
  712. vv->ov_suspend = NULL;
  713. }
  714. return 0;
  715. }
  716. /*
  717. * This function is _not_ called directly, but from
  718. * video_generic_ioctl (and maybe others). userspace
  719. * copying is done already, arg is a kernel pointer.
  720. */
  721. int saa7146_video_do_ioctl(struct inode *inode, struct file *file, unsigned int cmd, void *arg)
  722. {
  723. struct saa7146_fh *fh = file->private_data;
  724. struct saa7146_dev *dev = fh->dev;
  725. struct saa7146_vv *vv = dev->vv_data;
  726. int err = 0, result = 0, ee = 0;
  727. struct saa7146_use_ops *ops;
  728. struct videobuf_queue *q;
  729. /* check if extension handles the command */
  730. for(ee = 0; dev->ext_vv_data->ioctls[ee].flags != 0; ee++) {
  731. if( cmd == dev->ext_vv_data->ioctls[ee].cmd )
  732. break;
  733. }
  734. if( 0 != (dev->ext_vv_data->ioctls[ee].flags & SAA7146_EXCLUSIVE) ) {
  735. DEB_D(("extension handles ioctl exclusive.\n"));
  736. result = dev->ext_vv_data->ioctl(fh, cmd, arg);
  737. return result;
  738. }
  739. if( 0 != (dev->ext_vv_data->ioctls[ee].flags & SAA7146_BEFORE) ) {
  740. DEB_D(("extension handles ioctl before.\n"));
  741. result = dev->ext_vv_data->ioctl(fh, cmd, arg);
  742. if( -EAGAIN != result ) {
  743. return result;
  744. }
  745. }
  746. /* fixme: add handle "after" case (is it still needed?) */
  747. switch (fh->type) {
  748. case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
  749. ops = &saa7146_video_uops;
  750. q = &fh->video_q;
  751. break;
  752. }
  753. case V4L2_BUF_TYPE_VBI_CAPTURE: {
  754. ops = &saa7146_vbi_uops;
  755. q = &fh->vbi_q;
  756. break;
  757. }
  758. default:
  759. BUG();
  760. return 0;
  761. }
  762. switch (cmd) {
  763. case VIDIOC_QUERYCAP:
  764. {
  765. struct v4l2_capability *cap = arg;
  766. memset(cap,0,sizeof(*cap));
  767. DEB_EE(("VIDIOC_QUERYCAP\n"));
  768. strcpy(cap->driver, "saa7146 v4l2");
  769. strlcpy(cap->card, dev->ext->name, sizeof(cap->card));
  770. sprintf(cap->bus_info,"PCI:%s", pci_name(dev->pci));
  771. cap->version = SAA7146_VERSION_CODE;
  772. cap->capabilities =
  773. V4L2_CAP_VIDEO_CAPTURE |
  774. V4L2_CAP_VIDEO_OVERLAY |
  775. V4L2_CAP_READWRITE |
  776. V4L2_CAP_STREAMING;
  777. cap->capabilities |= dev->ext_vv_data->capabilities;
  778. return 0;
  779. }
  780. case VIDIOC_G_FBUF:
  781. {
  782. struct v4l2_framebuffer *fb = arg;
  783. DEB_EE(("VIDIOC_G_FBUF\n"));
  784. *fb = vv->ov_fb;
  785. fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
  786. return 0;
  787. }
  788. case VIDIOC_S_FBUF:
  789. {
  790. struct v4l2_framebuffer *fb = arg;
  791. struct saa7146_format *fmt;
  792. DEB_EE(("VIDIOC_S_FBUF\n"));
  793. if(!capable(CAP_SYS_ADMIN) &&
  794. !capable(CAP_SYS_RAWIO))
  795. return -EPERM;
  796. /* check args */
  797. fmt = format_by_fourcc(dev,fb->fmt.pixelformat);
  798. if (NULL == fmt) {
  799. return -EINVAL;
  800. }
  801. /* planar formats are not allowed for overlay video, clipping and video dma would clash */
  802. if (0 != (fmt->flags & FORMAT_IS_PLANAR)) {
  803. DEB_S(("planar pixelformat '%4.4s' not allowed for overlay\n",(char *)&fmt->pixelformat));
  804. }
  805. /* check if overlay is running */
  806. if (IS_OVERLAY_ACTIVE(fh) != 0) {
  807. if (vv->video_fh != fh) {
  808. DEB_D(("refusing to change framebuffer informations while overlay is active in another open.\n"));
  809. return -EBUSY;
  810. }
  811. }
  812. down(&dev->lock);
  813. /* ok, accept it */
  814. vv->ov_fb = *fb;
  815. vv->ov_fmt = fmt;
  816. if (0 == vv->ov_fb.fmt.bytesperline)
  817. vv->ov_fb.fmt.bytesperline =
  818. vv->ov_fb.fmt.width*fmt->depth/8;
  819. up(&dev->lock);
  820. return 0;
  821. }
  822. case VIDIOC_ENUM_FMT:
  823. {
  824. struct v4l2_fmtdesc *f = arg;
  825. int index;
  826. switch (f->type) {
  827. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  828. case V4L2_BUF_TYPE_VIDEO_OVERLAY: {
  829. index = f->index;
  830. if (index < 0 || index >= NUM_FORMATS) {
  831. return -EINVAL;
  832. }
  833. memset(f,0,sizeof(*f));
  834. f->index = index;
  835. strlcpy(f->description,formats[index].name,sizeof(f->description));
  836. f->pixelformat = formats[index].pixelformat;
  837. break;
  838. }
  839. default:
  840. return -EINVAL;
  841. }
  842. DEB_EE(("VIDIOC_ENUM_FMT: type:%d, index:%d\n",f->type,f->index));
  843. return 0;
  844. }
  845. case VIDIOC_QUERYCTRL:
  846. {
  847. const struct v4l2_queryctrl *ctrl;
  848. struct v4l2_queryctrl *c = arg;
  849. if ((c->id < V4L2_CID_BASE ||
  850. c->id >= V4L2_CID_LASTP1) &&
  851. (c->id < V4L2_CID_PRIVATE_BASE ||
  852. c->id >= V4L2_CID_PRIVATE_LASTP1))
  853. return -EINVAL;
  854. ctrl = ctrl_by_id(c->id);
  855. if( NULL == ctrl ) {
  856. return -EINVAL;
  857. /*
  858. c->flags = V4L2_CTRL_FLAG_DISABLED;
  859. return 0;
  860. */
  861. }
  862. DEB_EE(("VIDIOC_QUERYCTRL: id:%d\n",c->id));
  863. *c = *ctrl;
  864. return 0;
  865. }
  866. case VIDIOC_G_CTRL: {
  867. DEB_EE(("VIDIOC_G_CTRL\n"));
  868. return get_control(fh,arg);
  869. }
  870. case VIDIOC_S_CTRL:
  871. {
  872. DEB_EE(("VIDIOC_S_CTRL\n"));
  873. err = set_control(fh,arg);
  874. return err;
  875. }
  876. case VIDIOC_G_PARM:
  877. {
  878. struct v4l2_streamparm *parm = arg;
  879. if( parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ) {
  880. return -EINVAL;
  881. }
  882. memset(&parm->parm.capture,0,sizeof(struct v4l2_captureparm));
  883. parm->parm.capture.readbuffers = 1;
  884. // fixme: only for PAL!
  885. parm->parm.capture.timeperframe.numerator = 1;
  886. parm->parm.capture.timeperframe.denominator = 25;
  887. return 0;
  888. }
  889. case VIDIOC_G_FMT:
  890. {
  891. struct v4l2_format *f = arg;
  892. DEB_EE(("VIDIOC_G_FMT\n"));
  893. return g_fmt(fh,f);
  894. }
  895. case VIDIOC_S_FMT:
  896. {
  897. struct v4l2_format *f = arg;
  898. DEB_EE(("VIDIOC_S_FMT\n"));
  899. return s_fmt(fh,f);
  900. }
  901. case VIDIOC_TRY_FMT:
  902. {
  903. struct v4l2_format *f = arg;
  904. DEB_EE(("VIDIOC_TRY_FMT\n"));
  905. return try_fmt(fh,f);
  906. }
  907. case VIDIOC_G_STD:
  908. {
  909. v4l2_std_id *id = arg;
  910. DEB_EE(("VIDIOC_G_STD\n"));
  911. *id = vv->standard->id;
  912. return 0;
  913. }
  914. /* the saa7146 supfhrts (used in conjunction with the saa7111a for example)
  915. PAL / NTSC / SECAM. if your hardware does not (or does more)
  916. -- override this function in your extension */
  917. case VIDIOC_ENUMSTD:
  918. {
  919. struct v4l2_standard *e = arg;
  920. if (e->index < 0 )
  921. return -EINVAL;
  922. if( e->index < dev->ext_vv_data->num_stds ) {
  923. DEB_EE(("VIDIOC_ENUMSTD: index:%d\n",e->index));
  924. v4l2_video_std_construct(e, dev->ext_vv_data->stds[e->index].id, dev->ext_vv_data->stds[e->index].name);
  925. return 0;
  926. }
  927. return -EINVAL;
  928. }
  929. case VIDIOC_S_STD:
  930. {
  931. v4l2_std_id *id = arg;
  932. int found = 0;
  933. int i, err;
  934. DEB_EE(("VIDIOC_S_STD\n"));
  935. if ((vv->video_status & STATUS_CAPTURE) == STATUS_CAPTURE) {
  936. DEB_D(("cannot change video standard while streaming capture is active\n"));
  937. return -EBUSY;
  938. }
  939. if ((vv->video_status & STATUS_OVERLAY) != 0) {
  940. vv->ov_suspend = vv->video_fh;
  941. err = saa7146_stop_preview(vv->video_fh); /* side effect: video_status is now 0, video_fh is NULL */
  942. if (0 != err) {
  943. DEB_D(("suspending video failed. aborting\n"));
  944. return err;
  945. }
  946. }
  947. down(&dev->lock);
  948. for(i = 0; i < dev->ext_vv_data->num_stds; i++)
  949. if (*id & dev->ext_vv_data->stds[i].id)
  950. break;
  951. if (i != dev->ext_vv_data->num_stds) {
  952. vv->standard = &dev->ext_vv_data->stds[i];
  953. if( NULL != dev->ext_vv_data->std_callback )
  954. dev->ext_vv_data->std_callback(dev, vv->standard);
  955. found = 1;
  956. }
  957. up(&dev->lock);
  958. if (vv->ov_suspend != NULL) {
  959. saa7146_start_preview(vv->ov_suspend);
  960. vv->ov_suspend = NULL;
  961. }
  962. if( 0 == found ) {
  963. DEB_EE(("VIDIOC_S_STD: standard not found.\n"));
  964. return -EINVAL;
  965. }
  966. DEB_EE(("VIDIOC_S_STD: set to standard to '%s'\n",vv->standard->name));
  967. return 0;
  968. }
  969. case VIDIOC_OVERLAY:
  970. {
  971. int on = *(int *)arg;
  972. int err = 0;
  973. DEB_D(("VIDIOC_OVERLAY on:%d\n",on));
  974. if (on != 0) {
  975. err = saa7146_start_preview(fh);
  976. } else {
  977. err = saa7146_stop_preview(fh);
  978. }
  979. return err;
  980. }
  981. case VIDIOC_REQBUFS: {
  982. struct v4l2_requestbuffers *req = arg;
  983. DEB_D(("VIDIOC_REQBUFS, type:%d\n",req->type));
  984. return videobuf_reqbufs(q,req);
  985. }
  986. case VIDIOC_QUERYBUF: {
  987. struct v4l2_buffer *buf = arg;
  988. DEB_D(("VIDIOC_QUERYBUF, type:%d, offset:%d\n",buf->type,buf->m.offset));
  989. return videobuf_querybuf(q,buf);
  990. }
  991. case VIDIOC_QBUF: {
  992. struct v4l2_buffer *buf = arg;
  993. int ret = 0;
  994. ret = videobuf_qbuf(q,buf);
  995. DEB_D(("VIDIOC_QBUF: ret:%d, index:%d\n",ret,buf->index));
  996. return ret;
  997. }
  998. case VIDIOC_DQBUF: {
  999. struct v4l2_buffer *buf = arg;
  1000. int ret = 0;
  1001. ret = videobuf_dqbuf(q,buf,file->f_flags & O_NONBLOCK);
  1002. DEB_D(("VIDIOC_DQBUF: ret:%d, index:%d\n",ret,buf->index));
  1003. return ret;
  1004. }
  1005. case VIDIOC_STREAMON: {
  1006. int *type = arg;
  1007. DEB_D(("VIDIOC_STREAMON, type:%d\n",*type));
  1008. err = video_begin(fh);
  1009. if( 0 != err) {
  1010. return err;
  1011. }
  1012. err = videobuf_streamon(q);
  1013. return err;
  1014. }
  1015. case VIDIOC_STREAMOFF: {
  1016. int *type = arg;
  1017. DEB_D(("VIDIOC_STREAMOFF, type:%d\n",*type));
  1018. /* ugly: we need to copy some checks from video_end(),
  1019. because videobuf_streamoff() relies on the capture running.
  1020. check and fix this */
  1021. if ((vv->video_status & STATUS_CAPTURE) != STATUS_CAPTURE) {
  1022. DEB_S(("not capturing.\n"));
  1023. return 0;
  1024. }
  1025. if (vv->video_fh != fh) {
  1026. DEB_S(("capturing, but in another open.\n"));
  1027. return -EBUSY;
  1028. }
  1029. err = videobuf_streamoff(q);
  1030. if (0 != err) {
  1031. DEB_D(("warning: videobuf_streamoff() failed.\n"));
  1032. video_end(fh, file);
  1033. } else {
  1034. err = video_end(fh, file);
  1035. }
  1036. return err;
  1037. }
  1038. case VIDIOCGMBUF:
  1039. {
  1040. struct video_mbuf *mbuf = arg;
  1041. struct videobuf_queue *q;
  1042. int i;
  1043. /* fixme: number of capture buffers and sizes for v4l apps */
  1044. int gbuffers = 2;
  1045. int gbufsize = 768*576*4;
  1046. DEB_D(("VIDIOCGMBUF \n"));
  1047. q = &fh->video_q;
  1048. down(&q->lock);
  1049. err = videobuf_mmap_setup(q,gbuffers,gbufsize,
  1050. V4L2_MEMORY_MMAP);
  1051. if (err < 0) {
  1052. up(&q->lock);
  1053. return err;
  1054. }
  1055. memset(mbuf,0,sizeof(*mbuf));
  1056. mbuf->frames = gbuffers;
  1057. mbuf->size = gbuffers * gbufsize;
  1058. for (i = 0; i < gbuffers; i++)
  1059. mbuf->offsets[i] = i * gbufsize;
  1060. up(&q->lock);
  1061. return 0;
  1062. }
  1063. default:
  1064. return v4l_compat_translate_ioctl(inode,file,cmd,arg,
  1065. saa7146_video_do_ioctl);
  1066. }
  1067. return 0;
  1068. }
  1069. /*********************************************************************************/
  1070. /* buffer handling functions */
  1071. static int buffer_activate (struct saa7146_dev *dev,
  1072. struct saa7146_buf *buf,
  1073. struct saa7146_buf *next)
  1074. {
  1075. struct saa7146_vv *vv = dev->vv_data;
  1076. buf->vb.state = STATE_ACTIVE;
  1077. saa7146_set_capture(dev,buf,next);
  1078. mod_timer(&vv->video_q.timeout, jiffies+BUFFER_TIMEOUT);
  1079. return 0;
  1080. }
  1081. static int buffer_prepare(struct videobuf_queue *q,
  1082. struct videobuf_buffer *vb, enum v4l2_field field)
  1083. {
  1084. struct file *file = q->priv_data;
  1085. struct saa7146_fh *fh = file->private_data;
  1086. struct saa7146_dev *dev = fh->dev;
  1087. struct saa7146_vv *vv = dev->vv_data;
  1088. struct saa7146_buf *buf = (struct saa7146_buf *)vb;
  1089. int size,err = 0;
  1090. DEB_CAP(("vbuf:%p\n",vb));
  1091. /* sanity checks */
  1092. if (fh->video_fmt.width < 48 ||
  1093. fh->video_fmt.height < 32 ||
  1094. fh->video_fmt.width > vv->standard->h_max_out ||
  1095. fh->video_fmt.height > vv->standard->v_max_out) {
  1096. DEB_D(("w (%d) / h (%d) out of bounds.\n",fh->video_fmt.width,fh->video_fmt.height));
  1097. return -EINVAL;
  1098. }
  1099. size = fh->video_fmt.sizeimage;
  1100. if (0 != buf->vb.baddr && buf->vb.bsize < size) {
  1101. DEB_D(("size mismatch.\n"));
  1102. return -EINVAL;
  1103. }
  1104. DEB_CAP(("buffer_prepare [size=%dx%d,bytes=%d,fields=%s]\n",
  1105. fh->video_fmt.width,fh->video_fmt.height,size,v4l2_field_names[fh->video_fmt.field]));
  1106. if (buf->vb.width != fh->video_fmt.width ||
  1107. buf->vb.bytesperline != fh->video_fmt.bytesperline ||
  1108. buf->vb.height != fh->video_fmt.height ||
  1109. buf->vb.size != size ||
  1110. buf->vb.field != field ||
  1111. buf->vb.field != fh->video_fmt.field ||
  1112. buf->fmt != &fh->video_fmt) {
  1113. saa7146_dma_free(dev,buf);
  1114. }
  1115. if (STATE_NEEDS_INIT == buf->vb.state) {
  1116. struct saa7146_format *sfmt;
  1117. buf->vb.bytesperline = fh->video_fmt.bytesperline;
  1118. buf->vb.width = fh->video_fmt.width;
  1119. buf->vb.height = fh->video_fmt.height;
  1120. buf->vb.size = size;
  1121. buf->vb.field = field;
  1122. buf->fmt = &fh->video_fmt;
  1123. buf->vb.field = fh->video_fmt.field;
  1124. sfmt = format_by_fourcc(dev,buf->fmt->pixelformat);
  1125. if( 0 != IS_PLANAR(sfmt->trans)) {
  1126. saa7146_pgtable_free(dev->pci, &buf->pt[0]);
  1127. saa7146_pgtable_free(dev->pci, &buf->pt[1]);
  1128. saa7146_pgtable_free(dev->pci, &buf->pt[2]);
  1129. saa7146_pgtable_alloc(dev->pci, &buf->pt[0]);
  1130. saa7146_pgtable_alloc(dev->pci, &buf->pt[1]);
  1131. saa7146_pgtable_alloc(dev->pci, &buf->pt[2]);
  1132. } else {
  1133. saa7146_pgtable_free(dev->pci, &buf->pt[0]);
  1134. saa7146_pgtable_alloc(dev->pci, &buf->pt[0]);
  1135. }
  1136. err = videobuf_iolock(dev->pci,&buf->vb, &vv->ov_fb);
  1137. if (err)
  1138. goto oops;
  1139. err = saa7146_pgtable_build(dev,buf);
  1140. if (err)
  1141. goto oops;
  1142. }
  1143. buf->vb.state = STATE_PREPARED;
  1144. buf->activate = buffer_activate;
  1145. return 0;
  1146. oops:
  1147. DEB_D(("error out.\n"));
  1148. saa7146_dma_free(dev,buf);
  1149. return err;
  1150. }
  1151. static int buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
  1152. {
  1153. struct file *file = q->priv_data;
  1154. struct saa7146_fh *fh = file->private_data;
  1155. if (0 == *count || *count > MAX_SAA7146_CAPTURE_BUFFERS)
  1156. *count = MAX_SAA7146_CAPTURE_BUFFERS;
  1157. *size = fh->video_fmt.sizeimage;
  1158. /* check if we exceed the "max_memory" parameter */
  1159. if( (*count * *size) > (max_memory*1048576) ) {
  1160. *count = (max_memory*1048576) / *size;
  1161. }
  1162. DEB_CAP(("%d buffers, %d bytes each.\n",*count,*size));
  1163. return 0;
  1164. }
  1165. static void buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
  1166. {
  1167. struct file *file = q->priv_data;
  1168. struct saa7146_fh *fh = file->private_data;
  1169. struct saa7146_dev *dev = fh->dev;
  1170. struct saa7146_vv *vv = dev->vv_data;
  1171. struct saa7146_buf *buf = (struct saa7146_buf *)vb;
  1172. DEB_CAP(("vbuf:%p\n",vb));
  1173. saa7146_buffer_queue(fh->dev,&vv->video_q,buf);
  1174. }
  1175. static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
  1176. {
  1177. struct file *file = q->priv_data;
  1178. struct saa7146_fh *fh = file->private_data;
  1179. struct saa7146_dev *dev = fh->dev;
  1180. struct saa7146_buf *buf = (struct saa7146_buf *)vb;
  1181. DEB_CAP(("vbuf:%p\n",vb));
  1182. saa7146_dma_free(dev,buf);
  1183. }
  1184. static struct videobuf_queue_ops video_qops = {
  1185. .buf_setup = buffer_setup,
  1186. .buf_prepare = buffer_prepare,
  1187. .buf_queue = buffer_queue,
  1188. .buf_release = buffer_release,
  1189. };
  1190. /********************************************************************************/
  1191. /* file operations */
  1192. static void video_init(struct saa7146_dev *dev, struct saa7146_vv *vv)
  1193. {
  1194. INIT_LIST_HEAD(&vv->video_q.queue);
  1195. init_timer(&vv->video_q.timeout);
  1196. vv->video_q.timeout.function = saa7146_buffer_timeout;
  1197. vv->video_q.timeout.data = (unsigned long)(&vv->video_q);
  1198. vv->video_q.dev = dev;
  1199. /* set some default values */
  1200. vv->standard = &dev->ext_vv_data->stds[0];
  1201. /* FIXME: what's this? */
  1202. vv->current_hps_source = SAA7146_HPS_SOURCE_PORT_A;
  1203. vv->current_hps_sync = SAA7146_HPS_SYNC_PORT_A;
  1204. }
  1205. static int video_open(struct saa7146_dev *dev, struct file *file)
  1206. {
  1207. struct saa7146_fh *fh = (struct saa7146_fh *)file->private_data;
  1208. struct saa7146_format *sfmt;
  1209. fh->video_fmt.width = 384;
  1210. fh->video_fmt.height = 288;
  1211. fh->video_fmt.pixelformat = V4L2_PIX_FMT_BGR24;
  1212. fh->video_fmt.bytesperline = 0;
  1213. fh->video_fmt.field = V4L2_FIELD_ANY;
  1214. sfmt = format_by_fourcc(dev,fh->video_fmt.pixelformat);
  1215. fh->video_fmt.sizeimage = (fh->video_fmt.width * fh->video_fmt.height * sfmt->depth)/8;
  1216. videobuf_queue_init(&fh->video_q, &video_qops,
  1217. dev->pci, &dev->slock,
  1218. V4L2_BUF_TYPE_VIDEO_CAPTURE,
  1219. V4L2_FIELD_INTERLACED,
  1220. sizeof(struct saa7146_buf),
  1221. file);
  1222. init_MUTEX(&fh->video_q.lock);
  1223. return 0;
  1224. }
  1225. static void video_close(struct saa7146_dev *dev, struct file *file)
  1226. {
  1227. struct saa7146_fh *fh = (struct saa7146_fh *)file->private_data;
  1228. struct saa7146_vv *vv = dev->vv_data;
  1229. int err;
  1230. if (IS_CAPTURE_ACTIVE(fh) != 0) {
  1231. err = video_end(fh, file);
  1232. } else if (IS_OVERLAY_ACTIVE(fh) != 0) {
  1233. err = saa7146_stop_preview(fh);
  1234. }
  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,STATE_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. };