pwc-v4l.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  1. /* Linux driver for Philips webcam
  2. USB and Video4Linux interface part.
  3. (C) 1999-2004 Nemosoft Unv.
  4. (C) 2004-2006 Luc Saillard (luc@saillard.org)
  5. NOTE: this version of pwc is an unofficial (modified) release of pwc & pcwx
  6. driver and thus may have bugs that are not present in the original version.
  7. Please send bug reports and support requests to <luc@saillard.org>.
  8. The decompression routines have been implemented by reverse-engineering the
  9. Nemosoft binary pwcx module. Caveat emptor.
  10. This program is free software; you can redistribute it and/or modify
  11. it under the terms of the GNU General Public License as published by
  12. the Free Software Foundation; either version 2 of the License, or
  13. (at your option) any later version.
  14. This program is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. GNU General Public License for more details.
  18. You should have received a copy of the GNU General Public License
  19. along with this program; if not, write to the Free Software
  20. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include <linux/errno.h>
  23. #include <linux/init.h>
  24. #include <linux/mm.h>
  25. #include <linux/module.h>
  26. #include <linux/poll.h>
  27. #include <linux/vmalloc.h>
  28. #include <asm/io.h>
  29. #include "pwc.h"
  30. static struct v4l2_queryctrl pwc_controls[] = {
  31. {
  32. .id = V4L2_CID_BRIGHTNESS,
  33. .type = V4L2_CTRL_TYPE_INTEGER,
  34. .name = "Brightness",
  35. .minimum = 0,
  36. .maximum = 128,
  37. .step = 1,
  38. .default_value = 64,
  39. },
  40. {
  41. .id = V4L2_CID_CONTRAST,
  42. .type = V4L2_CTRL_TYPE_INTEGER,
  43. .name = "Contrast",
  44. .minimum = 0,
  45. .maximum = 64,
  46. .step = 1,
  47. .default_value = 0,
  48. },
  49. {
  50. .id = V4L2_CID_SATURATION,
  51. .type = V4L2_CTRL_TYPE_INTEGER,
  52. .name = "Saturation",
  53. .minimum = -100,
  54. .maximum = 100,
  55. .step = 1,
  56. .default_value = 0,
  57. },
  58. {
  59. .id = V4L2_CID_GAMMA,
  60. .type = V4L2_CTRL_TYPE_INTEGER,
  61. .name = "Gamma",
  62. .minimum = 0,
  63. .maximum = 32,
  64. .step = 1,
  65. .default_value = 0,
  66. },
  67. {
  68. .id = V4L2_CID_RED_BALANCE,
  69. .type = V4L2_CTRL_TYPE_INTEGER,
  70. .name = "Red Gain",
  71. .minimum = 0,
  72. .maximum = 256,
  73. .step = 1,
  74. .default_value = 0,
  75. },
  76. {
  77. .id = V4L2_CID_BLUE_BALANCE,
  78. .type = V4L2_CTRL_TYPE_INTEGER,
  79. .name = "Blue Gain",
  80. .minimum = 0,
  81. .maximum = 256,
  82. .step = 1,
  83. .default_value = 0,
  84. },
  85. {
  86. .id = V4L2_CID_AUTO_WHITE_BALANCE,
  87. .type = V4L2_CTRL_TYPE_BOOLEAN,
  88. .name = "Auto White Balance",
  89. .minimum = 0,
  90. .maximum = 1,
  91. .step = 1,
  92. .default_value = 0,
  93. },
  94. {
  95. .id = V4L2_CID_EXPOSURE,
  96. .type = V4L2_CTRL_TYPE_INTEGER,
  97. .name = "Shutter Speed (Exposure)",
  98. .minimum = 0,
  99. .maximum = 256,
  100. .step = 1,
  101. .default_value = 200,
  102. },
  103. {
  104. .id = V4L2_CID_AUTOGAIN,
  105. .type = V4L2_CTRL_TYPE_BOOLEAN,
  106. .name = "Auto Gain Enabled",
  107. .minimum = 0,
  108. .maximum = 1,
  109. .step = 1,
  110. .default_value = 1,
  111. },
  112. {
  113. .id = V4L2_CID_GAIN,
  114. .type = V4L2_CTRL_TYPE_INTEGER,
  115. .name = "Gain Level",
  116. .minimum = 0,
  117. .maximum = 256,
  118. .step = 1,
  119. .default_value = 0,
  120. },
  121. {
  122. .id = V4L2_CID_PRIVATE_SAVE_USER,
  123. .type = V4L2_CTRL_TYPE_BUTTON,
  124. .name = "Save User Settings",
  125. .minimum = 0,
  126. .maximum = 0,
  127. .step = 0,
  128. .default_value = 0,
  129. },
  130. {
  131. .id = V4L2_CID_PRIVATE_RESTORE_USER,
  132. .type = V4L2_CTRL_TYPE_BUTTON,
  133. .name = "Restore User Settings",
  134. .minimum = 0,
  135. .maximum = 0,
  136. .step = 0,
  137. .default_value = 0,
  138. },
  139. {
  140. .id = V4L2_CID_PRIVATE_RESTORE_FACTORY,
  141. .type = V4L2_CTRL_TYPE_BUTTON,
  142. .name = "Restore Factory Settings",
  143. .minimum = 0,
  144. .maximum = 0,
  145. .step = 0,
  146. .default_value = 0,
  147. },
  148. {
  149. .id = V4L2_CID_PRIVATE_COLOUR_MODE,
  150. .type = V4L2_CTRL_TYPE_BOOLEAN,
  151. .name = "Colour mode",
  152. .minimum = 0,
  153. .maximum = 1,
  154. .step = 1,
  155. .default_value = 0,
  156. },
  157. {
  158. .id = V4L2_CID_PRIVATE_AUTOCONTOUR,
  159. .type = V4L2_CTRL_TYPE_BOOLEAN,
  160. .name = "Auto contour",
  161. .minimum = 0,
  162. .maximum = 1,
  163. .step = 1,
  164. .default_value = 0,
  165. },
  166. {
  167. .id = V4L2_CID_PRIVATE_CONTOUR,
  168. .type = V4L2_CTRL_TYPE_INTEGER,
  169. .name = "Contour",
  170. .minimum = 0,
  171. .maximum = 63,
  172. .step = 1,
  173. .default_value = 0,
  174. },
  175. {
  176. .id = V4L2_CID_PRIVATE_BACKLIGHT,
  177. .type = V4L2_CTRL_TYPE_BOOLEAN,
  178. .name = "Backlight compensation",
  179. .minimum = 0,
  180. .maximum = 1,
  181. .step = 1,
  182. .default_value = 0,
  183. },
  184. {
  185. .id = V4L2_CID_PRIVATE_FLICKERLESS,
  186. .type = V4L2_CTRL_TYPE_BOOLEAN,
  187. .name = "Flickerless",
  188. .minimum = 0,
  189. .maximum = 1,
  190. .step = 1,
  191. .default_value = 0,
  192. },
  193. {
  194. .id = V4L2_CID_PRIVATE_NOISE_REDUCTION,
  195. .type = V4L2_CTRL_TYPE_INTEGER,
  196. .name = "Noise reduction",
  197. .minimum = 0,
  198. .maximum = 3,
  199. .step = 1,
  200. .default_value = 0,
  201. },
  202. };
  203. static void pwc_vidioc_fill_fmt(const struct pwc_device *pdev, struct v4l2_format *f)
  204. {
  205. memset(&f->fmt.pix, 0, sizeof(struct v4l2_pix_format));
  206. f->fmt.pix.width = pdev->view.x;
  207. f->fmt.pix.height = pdev->view.y;
  208. f->fmt.pix.field = V4L2_FIELD_NONE;
  209. if (pdev->pixfmt == V4L2_PIX_FMT_YUV420) {
  210. f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUV420;
  211. f->fmt.pix.bytesperline = (f->fmt.pix.width * 3)/2;
  212. f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
  213. } else {
  214. /* vbandlength contains 4 lines ... */
  215. f->fmt.pix.bytesperline = pdev->vbandlength/4;
  216. f->fmt.pix.sizeimage = pdev->frame_size + sizeof(struct pwc_raw_frame);
  217. if (DEVICE_USE_CODEC1(pdev->type))
  218. f->fmt.pix.pixelformat = V4L2_PIX_FMT_PWC1;
  219. else
  220. f->fmt.pix.pixelformat = V4L2_PIX_FMT_PWC2;
  221. }
  222. PWC_DEBUG_IOCTL("pwc_vidioc_fill_fmt() "
  223. "width=%d, height=%d, bytesperline=%d, sizeimage=%d, pixelformat=%c%c%c%c\n",
  224. f->fmt.pix.width,
  225. f->fmt.pix.height,
  226. f->fmt.pix.bytesperline,
  227. f->fmt.pix.sizeimage,
  228. (f->fmt.pix.pixelformat)&255,
  229. (f->fmt.pix.pixelformat>>8)&255,
  230. (f->fmt.pix.pixelformat>>16)&255,
  231. (f->fmt.pix.pixelformat>>24)&255);
  232. }
  233. /* ioctl(VIDIOC_TRY_FMT) */
  234. static int pwc_vidioc_try_fmt(struct pwc_device *pdev, struct v4l2_format *f)
  235. {
  236. if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  237. PWC_DEBUG_IOCTL("Bad video type must be V4L2_BUF_TYPE_VIDEO_CAPTURE\n");
  238. return -EINVAL;
  239. }
  240. switch (f->fmt.pix.pixelformat) {
  241. case V4L2_PIX_FMT_YUV420:
  242. break;
  243. case V4L2_PIX_FMT_PWC1:
  244. if (DEVICE_USE_CODEC23(pdev->type)) {
  245. PWC_DEBUG_IOCTL("codec1 is only supported for old pwc webcam\n");
  246. return -EINVAL;
  247. }
  248. break;
  249. case V4L2_PIX_FMT_PWC2:
  250. if (DEVICE_USE_CODEC1(pdev->type)) {
  251. PWC_DEBUG_IOCTL("codec23 is only supported for new pwc webcam\n");
  252. return -EINVAL;
  253. }
  254. break;
  255. default:
  256. PWC_DEBUG_IOCTL("Unsupported pixel format\n");
  257. return -EINVAL;
  258. }
  259. if (f->fmt.pix.width > pdev->view_max.x)
  260. f->fmt.pix.width = pdev->view_max.x;
  261. else if (f->fmt.pix.width < pdev->view_min.x)
  262. f->fmt.pix.width = pdev->view_min.x;
  263. if (f->fmt.pix.height > pdev->view_max.y)
  264. f->fmt.pix.height = pdev->view_max.y;
  265. else if (f->fmt.pix.height < pdev->view_min.y)
  266. f->fmt.pix.height = pdev->view_min.y;
  267. return 0;
  268. }
  269. /* ioctl(VIDIOC_SET_FMT) */
  270. static int pwc_s_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f)
  271. {
  272. struct pwc_device *pdev = video_drvdata(file);
  273. int ret, fps, snapshot, compression, pixelformat;
  274. if (!pdev->udev)
  275. return -ENODEV;
  276. if (pdev->capt_file != NULL &&
  277. pdev->capt_file != file)
  278. return -EBUSY;
  279. pdev->capt_file = file;
  280. ret = pwc_vidioc_try_fmt(pdev, f);
  281. if (ret<0)
  282. return ret;
  283. pixelformat = f->fmt.pix.pixelformat;
  284. compression = pdev->vcompression;
  285. snapshot = 0;
  286. fps = pdev->vframes;
  287. if (f->fmt.pix.priv) {
  288. compression = (f->fmt.pix.priv & PWC_QLT_MASK) >> PWC_QLT_SHIFT;
  289. snapshot = !!(f->fmt.pix.priv & PWC_FPS_SNAPSHOT);
  290. fps = (f->fmt.pix.priv & PWC_FPS_FRMASK) >> PWC_FPS_SHIFT;
  291. if (fps == 0)
  292. fps = pdev->vframes;
  293. }
  294. if (pixelformat != V4L2_PIX_FMT_YUV420 &&
  295. pixelformat != V4L2_PIX_FMT_PWC1 &&
  296. pixelformat != V4L2_PIX_FMT_PWC2)
  297. return -EINVAL;
  298. if (vb2_is_streaming(&pdev->vb_queue))
  299. return -EBUSY;
  300. PWC_DEBUG_IOCTL("Trying to set format to: width=%d height=%d fps=%d "
  301. "compression=%d snapshot=%d format=%c%c%c%c\n",
  302. f->fmt.pix.width, f->fmt.pix.height, fps,
  303. compression, snapshot,
  304. (pixelformat)&255,
  305. (pixelformat>>8)&255,
  306. (pixelformat>>16)&255,
  307. (pixelformat>>24)&255);
  308. ret = pwc_set_video_mode(pdev,
  309. f->fmt.pix.width,
  310. f->fmt.pix.height,
  311. fps,
  312. compression,
  313. snapshot);
  314. PWC_DEBUG_IOCTL("pwc_set_video_mode(), return=%d\n", ret);
  315. if (ret)
  316. return ret;
  317. pdev->pixfmt = pixelformat;
  318. pwc_vidioc_fill_fmt(pdev, f);
  319. return 0;
  320. }
  321. static int pwc_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
  322. {
  323. struct video_device *vdev = video_devdata(file);
  324. struct pwc_device *pdev = video_drvdata(file);
  325. if (!pdev->udev)
  326. return -ENODEV;
  327. strcpy(cap->driver, PWC_NAME);
  328. strlcpy(cap->card, vdev->name, sizeof(cap->card));
  329. usb_make_path(pdev->udev, cap->bus_info, sizeof(cap->bus_info));
  330. cap->capabilities =
  331. V4L2_CAP_VIDEO_CAPTURE |
  332. V4L2_CAP_STREAMING |
  333. V4L2_CAP_READWRITE;
  334. return 0;
  335. }
  336. static int pwc_enum_input(struct file *file, void *fh, struct v4l2_input *i)
  337. {
  338. if (i->index) /* Only one INPUT is supported */
  339. return -EINVAL;
  340. strcpy(i->name, "usb");
  341. return 0;
  342. }
  343. static int pwc_g_input(struct file *file, void *fh, unsigned int *i)
  344. {
  345. *i = 0;
  346. return 0;
  347. }
  348. static int pwc_s_input(struct file *file, void *fh, unsigned int i)
  349. {
  350. return i ? -EINVAL : 0;
  351. }
  352. static int pwc_queryctrl(struct file *file, void *fh, struct v4l2_queryctrl *c)
  353. {
  354. int i, idx;
  355. u32 id;
  356. id = c->id;
  357. if (id & V4L2_CTRL_FLAG_NEXT_CTRL) {
  358. id &= V4L2_CTRL_ID_MASK;
  359. id++;
  360. idx = -1;
  361. for (i = 0; i < ARRAY_SIZE(pwc_controls); i++) {
  362. if (pwc_controls[i].id < id)
  363. continue;
  364. if (idx >= 0
  365. && pwc_controls[i].id > pwc_controls[idx].id)
  366. continue;
  367. idx = i;
  368. }
  369. if (idx < 0)
  370. return -EINVAL;
  371. memcpy(c, &pwc_controls[idx], sizeof pwc_controls[0]);
  372. return 0;
  373. }
  374. for (i = 0; i < sizeof(pwc_controls) / sizeof(struct v4l2_queryctrl); i++) {
  375. if (pwc_controls[i].id == c->id) {
  376. PWC_DEBUG_IOCTL("ioctl(VIDIOC_QUERYCTRL) found\n");
  377. memcpy(c, &pwc_controls[i], sizeof(struct v4l2_queryctrl));
  378. return 0;
  379. }
  380. }
  381. return -EINVAL;
  382. }
  383. static int pwc_g_ctrl(struct file *file, void *fh, struct v4l2_control *c)
  384. {
  385. struct pwc_device *pdev = video_drvdata(file);
  386. int ret;
  387. if (!pdev->udev)
  388. return -ENODEV;
  389. switch (c->id) {
  390. case V4L2_CID_BRIGHTNESS:
  391. c->value = pwc_get_brightness(pdev);
  392. if (c->value < 0)
  393. return -EINVAL;
  394. return 0;
  395. case V4L2_CID_CONTRAST:
  396. c->value = pwc_get_contrast(pdev);
  397. if (c->value < 0)
  398. return -EINVAL;
  399. return 0;
  400. case V4L2_CID_SATURATION:
  401. ret = pwc_get_saturation(pdev, &c->value);
  402. if (ret < 0)
  403. return -EINVAL;
  404. return 0;
  405. case V4L2_CID_GAMMA:
  406. c->value = pwc_get_gamma(pdev);
  407. if (c->value < 0)
  408. return -EINVAL;
  409. return 0;
  410. case V4L2_CID_RED_BALANCE:
  411. ret = pwc_get_red_gain(pdev, &c->value);
  412. if (ret < 0)
  413. return -EINVAL;
  414. c->value >>= 8;
  415. return 0;
  416. case V4L2_CID_BLUE_BALANCE:
  417. ret = pwc_get_blue_gain(pdev, &c->value);
  418. if (ret < 0)
  419. return -EINVAL;
  420. c->value >>= 8;
  421. return 0;
  422. case V4L2_CID_AUTO_WHITE_BALANCE:
  423. ret = pwc_get_awb(pdev);
  424. if (ret < 0)
  425. return -EINVAL;
  426. c->value = (ret == PWC_WB_MANUAL) ? 0 : 1;
  427. return 0;
  428. case V4L2_CID_GAIN:
  429. ret = pwc_get_agc(pdev, &c->value);
  430. if (ret < 0)
  431. return -EINVAL;
  432. c->value >>= 8;
  433. return 0;
  434. case V4L2_CID_AUTOGAIN:
  435. ret = pwc_get_agc(pdev, &c->value);
  436. if (ret < 0)
  437. return -EINVAL;
  438. c->value = (c->value < 0) ? 1 : 0;
  439. return 0;
  440. case V4L2_CID_EXPOSURE:
  441. ret = pwc_get_shutter_speed(pdev, &c->value);
  442. if (ret < 0)
  443. return -EINVAL;
  444. return 0;
  445. case V4L2_CID_PRIVATE_COLOUR_MODE:
  446. ret = pwc_get_colour_mode(pdev, &c->value);
  447. if (ret < 0)
  448. return -EINVAL;
  449. return 0;
  450. case V4L2_CID_PRIVATE_AUTOCONTOUR:
  451. ret = pwc_get_contour(pdev, &c->value);
  452. if (ret < 0)
  453. return -EINVAL;
  454. c->value = (c->value == -1 ? 1 : 0);
  455. return 0;
  456. case V4L2_CID_PRIVATE_CONTOUR:
  457. ret = pwc_get_contour(pdev, &c->value);
  458. if (ret < 0)
  459. return -EINVAL;
  460. c->value >>= 10;
  461. return 0;
  462. case V4L2_CID_PRIVATE_BACKLIGHT:
  463. ret = pwc_get_backlight(pdev, &c->value);
  464. if (ret < 0)
  465. return -EINVAL;
  466. return 0;
  467. case V4L2_CID_PRIVATE_FLICKERLESS:
  468. ret = pwc_get_flicker(pdev, &c->value);
  469. if (ret < 0)
  470. return -EINVAL;
  471. c->value = (c->value ? 1 : 0);
  472. return 0;
  473. case V4L2_CID_PRIVATE_NOISE_REDUCTION:
  474. ret = pwc_get_dynamic_noise(pdev, &c->value);
  475. if (ret < 0)
  476. return -EINVAL;
  477. return 0;
  478. case V4L2_CID_PRIVATE_SAVE_USER:
  479. case V4L2_CID_PRIVATE_RESTORE_USER:
  480. case V4L2_CID_PRIVATE_RESTORE_FACTORY:
  481. return -EINVAL;
  482. }
  483. return -EINVAL;
  484. }
  485. static int pwc_s_ctrl(struct file *file, void *fh, struct v4l2_control *c)
  486. {
  487. struct pwc_device *pdev = video_drvdata(file);
  488. int ret;
  489. if (!pdev->udev)
  490. return -ENODEV;
  491. switch (c->id) {
  492. case V4L2_CID_BRIGHTNESS:
  493. c->value <<= 9;
  494. ret = pwc_set_brightness(pdev, c->value);
  495. if (ret < 0)
  496. return -EINVAL;
  497. return 0;
  498. case V4L2_CID_CONTRAST:
  499. c->value <<= 10;
  500. ret = pwc_set_contrast(pdev, c->value);
  501. if (ret < 0)
  502. return -EINVAL;
  503. return 0;
  504. case V4L2_CID_SATURATION:
  505. ret = pwc_set_saturation(pdev, c->value);
  506. if (ret < 0)
  507. return -EINVAL;
  508. return 0;
  509. case V4L2_CID_GAMMA:
  510. c->value <<= 11;
  511. ret = pwc_set_gamma(pdev, c->value);
  512. if (ret < 0)
  513. return -EINVAL;
  514. return 0;
  515. case V4L2_CID_RED_BALANCE:
  516. c->value <<= 8;
  517. ret = pwc_set_red_gain(pdev, c->value);
  518. if (ret < 0)
  519. return -EINVAL;
  520. return 0;
  521. case V4L2_CID_BLUE_BALANCE:
  522. c->value <<= 8;
  523. ret = pwc_set_blue_gain(pdev, c->value);
  524. if (ret < 0)
  525. return -EINVAL;
  526. return 0;
  527. case V4L2_CID_AUTO_WHITE_BALANCE:
  528. c->value = (c->value == 0) ? PWC_WB_MANUAL : PWC_WB_AUTO;
  529. ret = pwc_set_awb(pdev, c->value);
  530. if (ret < 0)
  531. return -EINVAL;
  532. return 0;
  533. case V4L2_CID_EXPOSURE:
  534. c->value <<= 8;
  535. ret = pwc_set_shutter_speed(pdev, c->value ? 0 : 1, c->value);
  536. if (ret < 0)
  537. return -EINVAL;
  538. return 0;
  539. case V4L2_CID_AUTOGAIN:
  540. /* autogain off means nothing without a gain */
  541. if (c->value == 0)
  542. return 0;
  543. ret = pwc_set_agc(pdev, c->value, 0);
  544. if (ret < 0)
  545. return -EINVAL;
  546. return 0;
  547. case V4L2_CID_GAIN:
  548. c->value <<= 8;
  549. ret = pwc_set_agc(pdev, 0, c->value);
  550. if (ret < 0)
  551. return -EINVAL;
  552. return 0;
  553. case V4L2_CID_PRIVATE_SAVE_USER:
  554. if (pwc_save_user(pdev))
  555. return -EINVAL;
  556. return 0;
  557. case V4L2_CID_PRIVATE_RESTORE_USER:
  558. if (pwc_restore_user(pdev))
  559. return -EINVAL;
  560. return 0;
  561. case V4L2_CID_PRIVATE_RESTORE_FACTORY:
  562. if (pwc_restore_factory(pdev))
  563. return -EINVAL;
  564. return 0;
  565. case V4L2_CID_PRIVATE_COLOUR_MODE:
  566. ret = pwc_set_colour_mode(pdev, c->value);
  567. if (ret < 0)
  568. return -EINVAL;
  569. return 0;
  570. case V4L2_CID_PRIVATE_AUTOCONTOUR:
  571. c->value = (c->value == 1) ? -1 : 0;
  572. ret = pwc_set_contour(pdev, c->value);
  573. if (ret < 0)
  574. return -EINVAL;
  575. return 0;
  576. case V4L2_CID_PRIVATE_CONTOUR:
  577. c->value <<= 10;
  578. ret = pwc_set_contour(pdev, c->value);
  579. if (ret < 0)
  580. return -EINVAL;
  581. return 0;
  582. case V4L2_CID_PRIVATE_BACKLIGHT:
  583. ret = pwc_set_backlight(pdev, c->value);
  584. if (ret < 0)
  585. return -EINVAL;
  586. return 0;
  587. case V4L2_CID_PRIVATE_FLICKERLESS:
  588. ret = pwc_set_flicker(pdev, c->value);
  589. if (ret < 0)
  590. return -EINVAL;
  591. case V4L2_CID_PRIVATE_NOISE_REDUCTION:
  592. ret = pwc_set_dynamic_noise(pdev, c->value);
  593. if (ret < 0)
  594. return -EINVAL;
  595. return 0;
  596. }
  597. return -EINVAL;
  598. }
  599. static int pwc_enum_fmt_vid_cap(struct file *file, void *fh, struct v4l2_fmtdesc *f)
  600. {
  601. struct pwc_device *pdev = video_drvdata(file);
  602. /* We only support two format: the raw format, and YUV */
  603. switch (f->index) {
  604. case 0:
  605. /* RAW format */
  606. f->pixelformat = pdev->type <= 646 ? V4L2_PIX_FMT_PWC1 : V4L2_PIX_FMT_PWC2;
  607. f->flags = V4L2_FMT_FLAG_COMPRESSED;
  608. strlcpy(f->description, "Raw Philips Webcam", sizeof(f->description));
  609. break;
  610. case 1:
  611. f->pixelformat = V4L2_PIX_FMT_YUV420;
  612. strlcpy(f->description, "4:2:0, planar, Y-Cb-Cr", sizeof(f->description));
  613. break;
  614. default:
  615. return -EINVAL;
  616. }
  617. return 0;
  618. }
  619. static int pwc_g_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f)
  620. {
  621. struct pwc_device *pdev = video_drvdata(file);
  622. PWC_DEBUG_IOCTL("ioctl(VIDIOC_G_FMT) return size %dx%d\n",
  623. pdev->image.x, pdev->image.y);
  624. pwc_vidioc_fill_fmt(pdev, f);
  625. return 0;
  626. }
  627. static int pwc_try_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f)
  628. {
  629. struct pwc_device *pdev = video_drvdata(file);
  630. return pwc_vidioc_try_fmt(pdev, f);
  631. }
  632. static int pwc_reqbufs(struct file *file, void *fh,
  633. struct v4l2_requestbuffers *rb)
  634. {
  635. struct pwc_device *pdev = video_drvdata(file);
  636. if (pdev->capt_file != NULL &&
  637. pdev->capt_file != file)
  638. return -EBUSY;
  639. pdev->capt_file = file;
  640. return vb2_reqbufs(&pdev->vb_queue, rb);
  641. }
  642. static int pwc_querybuf(struct file *file, void *fh, struct v4l2_buffer *buf)
  643. {
  644. struct pwc_device *pdev = video_drvdata(file);
  645. return vb2_querybuf(&pdev->vb_queue, buf);
  646. }
  647. static int pwc_qbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
  648. {
  649. struct pwc_device *pdev = video_drvdata(file);
  650. if (!pdev->udev)
  651. return -ENODEV;
  652. if (pdev->capt_file != file)
  653. return -EBUSY;
  654. return vb2_qbuf(&pdev->vb_queue, buf);
  655. }
  656. static int pwc_dqbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
  657. {
  658. struct pwc_device *pdev = video_drvdata(file);
  659. if (!pdev->udev)
  660. return -ENODEV;
  661. if (pdev->capt_file != file)
  662. return -EBUSY;
  663. return vb2_dqbuf(&pdev->vb_queue, buf, file->f_flags & O_NONBLOCK);
  664. }
  665. static int pwc_streamon(struct file *file, void *fh, enum v4l2_buf_type i)
  666. {
  667. struct pwc_device *pdev = video_drvdata(file);
  668. if (!pdev->udev)
  669. return -ENODEV;
  670. if (pdev->capt_file != file)
  671. return -EBUSY;
  672. return vb2_streamon(&pdev->vb_queue, i);
  673. }
  674. static int pwc_streamoff(struct file *file, void *fh, enum v4l2_buf_type i)
  675. {
  676. struct pwc_device *pdev = video_drvdata(file);
  677. if (!pdev->udev)
  678. return -ENODEV;
  679. if (pdev->capt_file != file)
  680. return -EBUSY;
  681. return vb2_streamoff(&pdev->vb_queue, i);
  682. }
  683. static int pwc_enum_framesizes(struct file *file, void *fh,
  684. struct v4l2_frmsizeenum *fsize)
  685. {
  686. struct pwc_device *pdev = video_drvdata(file);
  687. unsigned int i = 0, index = fsize->index;
  688. if (fsize->pixel_format == V4L2_PIX_FMT_YUV420) {
  689. for (i = 0; i < PSZ_MAX; i++) {
  690. if (pdev->image_mask & (1UL << i)) {
  691. if (!index--) {
  692. fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
  693. fsize->discrete.width = pwc_image_sizes[i].x;
  694. fsize->discrete.height = pwc_image_sizes[i].y;
  695. return 0;
  696. }
  697. }
  698. }
  699. } else if (fsize->index == 0 &&
  700. ((fsize->pixel_format == V4L2_PIX_FMT_PWC1 && DEVICE_USE_CODEC1(pdev->type)) ||
  701. (fsize->pixel_format == V4L2_PIX_FMT_PWC2 && DEVICE_USE_CODEC23(pdev->type)))) {
  702. fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
  703. fsize->discrete.width = pdev->abs_max.x;
  704. fsize->discrete.height = pdev->abs_max.y;
  705. return 0;
  706. }
  707. return -EINVAL;
  708. }
  709. static int pwc_enum_frameintervals(struct file *file, void *fh,
  710. struct v4l2_frmivalenum *fival)
  711. {
  712. struct pwc_device *pdev = video_drvdata(file);
  713. int size = -1;
  714. unsigned int i;
  715. for (i = 0; i < PSZ_MAX; i++) {
  716. if (pwc_image_sizes[i].x == fival->width &&
  717. pwc_image_sizes[i].y == fival->height) {
  718. size = i;
  719. break;
  720. }
  721. }
  722. /* TODO: Support raw format */
  723. if (size < 0 || fival->pixel_format != V4L2_PIX_FMT_YUV420)
  724. return -EINVAL;
  725. i = pwc_get_fps(pdev, fival->index, size);
  726. if (!i)
  727. return -EINVAL;
  728. fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
  729. fival->discrete.numerator = 1;
  730. fival->discrete.denominator = i;
  731. return 0;
  732. }
  733. static long pwc_default(struct file *file, void *fh, bool valid_prio,
  734. int cmd, void *arg)
  735. {
  736. struct pwc_device *pdev = video_drvdata(file);
  737. return pwc_ioctl(pdev, cmd, arg);
  738. }
  739. const struct v4l2_ioctl_ops pwc_ioctl_ops = {
  740. .vidioc_querycap = pwc_querycap,
  741. .vidioc_enum_input = pwc_enum_input,
  742. .vidioc_g_input = pwc_g_input,
  743. .vidioc_s_input = pwc_s_input,
  744. .vidioc_enum_fmt_vid_cap = pwc_enum_fmt_vid_cap,
  745. .vidioc_g_fmt_vid_cap = pwc_g_fmt_vid_cap,
  746. .vidioc_s_fmt_vid_cap = pwc_s_fmt_vid_cap,
  747. .vidioc_try_fmt_vid_cap = pwc_try_fmt_vid_cap,
  748. .vidioc_queryctrl = pwc_queryctrl,
  749. .vidioc_g_ctrl = pwc_g_ctrl,
  750. .vidioc_s_ctrl = pwc_s_ctrl,
  751. .vidioc_reqbufs = pwc_reqbufs,
  752. .vidioc_querybuf = pwc_querybuf,
  753. .vidioc_qbuf = pwc_qbuf,
  754. .vidioc_dqbuf = pwc_dqbuf,
  755. .vidioc_streamon = pwc_streamon,
  756. .vidioc_streamoff = pwc_streamoff,
  757. .vidioc_enum_framesizes = pwc_enum_framesizes,
  758. .vidioc_enum_frameintervals = pwc_enum_frameintervals,
  759. .vidioc_default = pwc_default,
  760. };
  761. /* vim: set cino= formatoptions=croql cindent shiftwidth=8 tabstop=8: */