pwc-v4l.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  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_vidioc_set_fmt(struct pwc_device *pdev, struct v4l2_format *f)
  271. {
  272. int ret, fps, snapshot, compression, pixelformat;
  273. ret = pwc_vidioc_try_fmt(pdev, f);
  274. if (ret<0)
  275. return ret;
  276. pixelformat = f->fmt.pix.pixelformat;
  277. compression = pdev->vcompression;
  278. snapshot = 0;
  279. fps = pdev->vframes;
  280. if (f->fmt.pix.priv) {
  281. compression = (f->fmt.pix.priv & PWC_QLT_MASK) >> PWC_QLT_SHIFT;
  282. snapshot = !!(f->fmt.pix.priv & PWC_FPS_SNAPSHOT);
  283. fps = (f->fmt.pix.priv & PWC_FPS_FRMASK) >> PWC_FPS_SHIFT;
  284. if (fps == 0)
  285. fps = pdev->vframes;
  286. }
  287. if (pixelformat != V4L2_PIX_FMT_YUV420 &&
  288. pixelformat != V4L2_PIX_FMT_PWC1 &&
  289. pixelformat != V4L2_PIX_FMT_PWC2)
  290. return -EINVAL;
  291. if (vb2_is_streaming(&pdev->vb_queue))
  292. return -EBUSY;
  293. PWC_DEBUG_IOCTL("Trying to set format to: width=%d height=%d fps=%d "
  294. "compression=%d snapshot=%d format=%c%c%c%c\n",
  295. f->fmt.pix.width, f->fmt.pix.height, fps,
  296. compression, snapshot,
  297. (pixelformat)&255,
  298. (pixelformat>>8)&255,
  299. (pixelformat>>16)&255,
  300. (pixelformat>>24)&255);
  301. ret = pwc_set_video_mode(pdev,
  302. f->fmt.pix.width,
  303. f->fmt.pix.height,
  304. fps,
  305. compression,
  306. snapshot);
  307. PWC_DEBUG_IOCTL("pwc_set_video_mode(), return=%d\n", ret);
  308. if (ret)
  309. return ret;
  310. pdev->pixfmt = pixelformat;
  311. pwc_vidioc_fill_fmt(pdev, f);
  312. return 0;
  313. }
  314. static int pwc_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
  315. {
  316. struct video_device *vdev = video_devdata(file);
  317. struct pwc_device *pdev = video_drvdata(file);
  318. strcpy(cap->driver, PWC_NAME);
  319. strlcpy(cap->card, vdev->name, sizeof(cap->card));
  320. usb_make_path(pdev->udev, cap->bus_info, sizeof(cap->bus_info));
  321. cap->capabilities =
  322. V4L2_CAP_VIDEO_CAPTURE |
  323. V4L2_CAP_STREAMING |
  324. V4L2_CAP_READWRITE;
  325. return 0;
  326. }
  327. static int pwc_enum_input(struct file *file, void *fh, struct v4l2_input *i)
  328. {
  329. if (i->index) /* Only one INPUT is supported */
  330. return -EINVAL;
  331. strcpy(i->name, "usb");
  332. return 0;
  333. }
  334. static int pwc_g_input(struct file *file, void *fh, unsigned int *i)
  335. {
  336. *i = 0;
  337. return 0;
  338. }
  339. static int pwc_s_input(struct file *file, void *fh, unsigned int i)
  340. {
  341. return i ? -EINVAL : 0;
  342. }
  343. static int pwc_queryctrl(struct file *file, void *fh, struct v4l2_queryctrl *c)
  344. {
  345. int i, idx;
  346. u32 id;
  347. id = c->id;
  348. if (id & V4L2_CTRL_FLAG_NEXT_CTRL) {
  349. id &= V4L2_CTRL_ID_MASK;
  350. id++;
  351. idx = -1;
  352. for (i = 0; i < ARRAY_SIZE(pwc_controls); i++) {
  353. if (pwc_controls[i].id < id)
  354. continue;
  355. if (idx >= 0
  356. && pwc_controls[i].id > pwc_controls[idx].id)
  357. continue;
  358. idx = i;
  359. }
  360. if (idx < 0)
  361. return -EINVAL;
  362. memcpy(c, &pwc_controls[idx], sizeof pwc_controls[0]);
  363. return 0;
  364. }
  365. for (i = 0; i < sizeof(pwc_controls) / sizeof(struct v4l2_queryctrl); i++) {
  366. if (pwc_controls[i].id == c->id) {
  367. PWC_DEBUG_IOCTL("ioctl(VIDIOC_QUERYCTRL) found\n");
  368. memcpy(c, &pwc_controls[i], sizeof(struct v4l2_queryctrl));
  369. return 0;
  370. }
  371. }
  372. return -EINVAL;
  373. }
  374. static int pwc_g_ctrl(struct file *file, void *fh, struct v4l2_control *c)
  375. {
  376. struct pwc_device *pdev = video_drvdata(file);
  377. int ret;
  378. switch (c->id) {
  379. case V4L2_CID_BRIGHTNESS:
  380. c->value = pwc_get_brightness(pdev);
  381. if (c->value < 0)
  382. return -EINVAL;
  383. return 0;
  384. case V4L2_CID_CONTRAST:
  385. c->value = pwc_get_contrast(pdev);
  386. if (c->value < 0)
  387. return -EINVAL;
  388. return 0;
  389. case V4L2_CID_SATURATION:
  390. ret = pwc_get_saturation(pdev, &c->value);
  391. if (ret < 0)
  392. return -EINVAL;
  393. return 0;
  394. case V4L2_CID_GAMMA:
  395. c->value = pwc_get_gamma(pdev);
  396. if (c->value < 0)
  397. return -EINVAL;
  398. return 0;
  399. case V4L2_CID_RED_BALANCE:
  400. ret = pwc_get_red_gain(pdev, &c->value);
  401. if (ret < 0)
  402. return -EINVAL;
  403. c->value >>= 8;
  404. return 0;
  405. case V4L2_CID_BLUE_BALANCE:
  406. ret = pwc_get_blue_gain(pdev, &c->value);
  407. if (ret < 0)
  408. return -EINVAL;
  409. c->value >>= 8;
  410. return 0;
  411. case V4L2_CID_AUTO_WHITE_BALANCE:
  412. ret = pwc_get_awb(pdev);
  413. if (ret < 0)
  414. return -EINVAL;
  415. c->value = (ret == PWC_WB_MANUAL) ? 0 : 1;
  416. return 0;
  417. case V4L2_CID_GAIN:
  418. ret = pwc_get_agc(pdev, &c->value);
  419. if (ret < 0)
  420. return -EINVAL;
  421. c->value >>= 8;
  422. return 0;
  423. case V4L2_CID_AUTOGAIN:
  424. ret = pwc_get_agc(pdev, &c->value);
  425. if (ret < 0)
  426. return -EINVAL;
  427. c->value = (c->value < 0) ? 1 : 0;
  428. return 0;
  429. case V4L2_CID_EXPOSURE:
  430. ret = pwc_get_shutter_speed(pdev, &c->value);
  431. if (ret < 0)
  432. return -EINVAL;
  433. return 0;
  434. case V4L2_CID_PRIVATE_COLOUR_MODE:
  435. ret = pwc_get_colour_mode(pdev, &c->value);
  436. if (ret < 0)
  437. return -EINVAL;
  438. return 0;
  439. case V4L2_CID_PRIVATE_AUTOCONTOUR:
  440. ret = pwc_get_contour(pdev, &c->value);
  441. if (ret < 0)
  442. return -EINVAL;
  443. c->value = (c->value == -1 ? 1 : 0);
  444. return 0;
  445. case V4L2_CID_PRIVATE_CONTOUR:
  446. ret = pwc_get_contour(pdev, &c->value);
  447. if (ret < 0)
  448. return -EINVAL;
  449. c->value >>= 10;
  450. return 0;
  451. case V4L2_CID_PRIVATE_BACKLIGHT:
  452. ret = pwc_get_backlight(pdev, &c->value);
  453. if (ret < 0)
  454. return -EINVAL;
  455. return 0;
  456. case V4L2_CID_PRIVATE_FLICKERLESS:
  457. ret = pwc_get_flicker(pdev, &c->value);
  458. if (ret < 0)
  459. return -EINVAL;
  460. c->value = (c->value ? 1 : 0);
  461. return 0;
  462. case V4L2_CID_PRIVATE_NOISE_REDUCTION:
  463. ret = pwc_get_dynamic_noise(pdev, &c->value);
  464. if (ret < 0)
  465. return -EINVAL;
  466. return 0;
  467. case V4L2_CID_PRIVATE_SAVE_USER:
  468. case V4L2_CID_PRIVATE_RESTORE_USER:
  469. case V4L2_CID_PRIVATE_RESTORE_FACTORY:
  470. return -EINVAL;
  471. }
  472. return -EINVAL;
  473. }
  474. static int pwc_s_ctrl(struct file *file, void *fh, struct v4l2_control *c)
  475. {
  476. struct pwc_device *pdev = video_drvdata(file);
  477. int ret;
  478. switch (c->id) {
  479. case V4L2_CID_BRIGHTNESS:
  480. c->value <<= 9;
  481. ret = pwc_set_brightness(pdev, c->value);
  482. if (ret < 0)
  483. return -EINVAL;
  484. return 0;
  485. case V4L2_CID_CONTRAST:
  486. c->value <<= 10;
  487. ret = pwc_set_contrast(pdev, c->value);
  488. if (ret < 0)
  489. return -EINVAL;
  490. return 0;
  491. case V4L2_CID_SATURATION:
  492. ret = pwc_set_saturation(pdev, c->value);
  493. if (ret < 0)
  494. return -EINVAL;
  495. return 0;
  496. case V4L2_CID_GAMMA:
  497. c->value <<= 11;
  498. ret = pwc_set_gamma(pdev, c->value);
  499. if (ret < 0)
  500. return -EINVAL;
  501. return 0;
  502. case V4L2_CID_RED_BALANCE:
  503. c->value <<= 8;
  504. ret = pwc_set_red_gain(pdev, c->value);
  505. if (ret < 0)
  506. return -EINVAL;
  507. return 0;
  508. case V4L2_CID_BLUE_BALANCE:
  509. c->value <<= 8;
  510. ret = pwc_set_blue_gain(pdev, c->value);
  511. if (ret < 0)
  512. return -EINVAL;
  513. return 0;
  514. case V4L2_CID_AUTO_WHITE_BALANCE:
  515. c->value = (c->value == 0) ? PWC_WB_MANUAL : PWC_WB_AUTO;
  516. ret = pwc_set_awb(pdev, c->value);
  517. if (ret < 0)
  518. return -EINVAL;
  519. return 0;
  520. case V4L2_CID_EXPOSURE:
  521. c->value <<= 8;
  522. ret = pwc_set_shutter_speed(pdev, c->value ? 0 : 1, c->value);
  523. if (ret < 0)
  524. return -EINVAL;
  525. return 0;
  526. case V4L2_CID_AUTOGAIN:
  527. /* autogain off means nothing without a gain */
  528. if (c->value == 0)
  529. return 0;
  530. ret = pwc_set_agc(pdev, c->value, 0);
  531. if (ret < 0)
  532. return -EINVAL;
  533. return 0;
  534. case V4L2_CID_GAIN:
  535. c->value <<= 8;
  536. ret = pwc_set_agc(pdev, 0, c->value);
  537. if (ret < 0)
  538. return -EINVAL;
  539. return 0;
  540. case V4L2_CID_PRIVATE_SAVE_USER:
  541. if (pwc_save_user(pdev))
  542. return -EINVAL;
  543. return 0;
  544. case V4L2_CID_PRIVATE_RESTORE_USER:
  545. if (pwc_restore_user(pdev))
  546. return -EINVAL;
  547. return 0;
  548. case V4L2_CID_PRIVATE_RESTORE_FACTORY:
  549. if (pwc_restore_factory(pdev))
  550. return -EINVAL;
  551. return 0;
  552. case V4L2_CID_PRIVATE_COLOUR_MODE:
  553. ret = pwc_set_colour_mode(pdev, c->value);
  554. if (ret < 0)
  555. return -EINVAL;
  556. return 0;
  557. case V4L2_CID_PRIVATE_AUTOCONTOUR:
  558. c->value = (c->value == 1) ? -1 : 0;
  559. ret = pwc_set_contour(pdev, c->value);
  560. if (ret < 0)
  561. return -EINVAL;
  562. return 0;
  563. case V4L2_CID_PRIVATE_CONTOUR:
  564. c->value <<= 10;
  565. ret = pwc_set_contour(pdev, c->value);
  566. if (ret < 0)
  567. return -EINVAL;
  568. return 0;
  569. case V4L2_CID_PRIVATE_BACKLIGHT:
  570. ret = pwc_set_backlight(pdev, c->value);
  571. if (ret < 0)
  572. return -EINVAL;
  573. return 0;
  574. case V4L2_CID_PRIVATE_FLICKERLESS:
  575. ret = pwc_set_flicker(pdev, c->value);
  576. if (ret < 0)
  577. return -EINVAL;
  578. case V4L2_CID_PRIVATE_NOISE_REDUCTION:
  579. ret = pwc_set_dynamic_noise(pdev, c->value);
  580. if (ret < 0)
  581. return -EINVAL;
  582. return 0;
  583. }
  584. return -EINVAL;
  585. }
  586. static int pwc_enum_fmt_vid_cap(struct file *file, void *fh, struct v4l2_fmtdesc *f)
  587. {
  588. struct pwc_device *pdev = video_drvdata(file);
  589. /* We only support two format: the raw format, and YUV */
  590. switch (f->index) {
  591. case 0:
  592. /* RAW format */
  593. f->pixelformat = pdev->type <= 646 ? V4L2_PIX_FMT_PWC1 : V4L2_PIX_FMT_PWC2;
  594. f->flags = V4L2_FMT_FLAG_COMPRESSED;
  595. strlcpy(f->description, "Raw Philips Webcam", sizeof(f->description));
  596. break;
  597. case 1:
  598. f->pixelformat = V4L2_PIX_FMT_YUV420;
  599. strlcpy(f->description, "4:2:0, planar, Y-Cb-Cr", sizeof(f->description));
  600. break;
  601. default:
  602. return -EINVAL;
  603. }
  604. return 0;
  605. }
  606. static int pwc_g_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f)
  607. {
  608. struct pwc_device *pdev = video_drvdata(file);
  609. PWC_DEBUG_IOCTL("ioctl(VIDIOC_G_FMT) return size %dx%d\n",
  610. pdev->image.x, pdev->image.y);
  611. pwc_vidioc_fill_fmt(pdev, f);
  612. return 0;
  613. }
  614. static int pwc_try_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f)
  615. {
  616. struct pwc_device *pdev = video_drvdata(file);
  617. return pwc_vidioc_try_fmt(pdev, f);
  618. }
  619. static int pwc_s_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f)
  620. {
  621. struct pwc_device *pdev = video_drvdata(file);
  622. return pwc_vidioc_set_fmt(pdev, f);
  623. }
  624. static int pwc_reqbufs(struct file *file, void *fh,
  625. struct v4l2_requestbuffers *rb)
  626. {
  627. struct pwc_device *pdev = video_drvdata(file);
  628. return vb2_reqbufs(&pdev->vb_queue, rb);
  629. }
  630. static int pwc_querybuf(struct file *file, void *fh, struct v4l2_buffer *buf)
  631. {
  632. struct pwc_device *pdev = video_drvdata(file);
  633. return vb2_querybuf(&pdev->vb_queue, buf);
  634. }
  635. static int pwc_qbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
  636. {
  637. struct pwc_device *pdev = video_drvdata(file);
  638. return vb2_qbuf(&pdev->vb_queue, buf);
  639. }
  640. static int pwc_dqbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
  641. {
  642. struct pwc_device *pdev = video_drvdata(file);
  643. return vb2_dqbuf(&pdev->vb_queue, buf, file->f_flags & O_NONBLOCK);
  644. }
  645. static int pwc_streamon(struct file *file, void *fh, enum v4l2_buf_type i)
  646. {
  647. struct pwc_device *pdev = video_drvdata(file);
  648. return vb2_streamon(&pdev->vb_queue, i);
  649. }
  650. static int pwc_streamoff(struct file *file, void *fh, enum v4l2_buf_type i)
  651. {
  652. struct pwc_device *pdev = video_drvdata(file);
  653. return vb2_streamoff(&pdev->vb_queue, i);
  654. }
  655. static int pwc_enum_framesizes(struct file *file, void *fh,
  656. struct v4l2_frmsizeenum *fsize)
  657. {
  658. struct pwc_device *pdev = video_drvdata(file);
  659. unsigned int i = 0, index = fsize->index;
  660. if (fsize->pixel_format == V4L2_PIX_FMT_YUV420) {
  661. for (i = 0; i < PSZ_MAX; i++) {
  662. if (pdev->image_mask & (1UL << i)) {
  663. if (!index--) {
  664. fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
  665. fsize->discrete.width = pwc_image_sizes[i].x;
  666. fsize->discrete.height = pwc_image_sizes[i].y;
  667. return 0;
  668. }
  669. }
  670. }
  671. } else if (fsize->index == 0 &&
  672. ((fsize->pixel_format == V4L2_PIX_FMT_PWC1 && DEVICE_USE_CODEC1(pdev->type)) ||
  673. (fsize->pixel_format == V4L2_PIX_FMT_PWC2 && DEVICE_USE_CODEC23(pdev->type)))) {
  674. fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
  675. fsize->discrete.width = pdev->abs_max.x;
  676. fsize->discrete.height = pdev->abs_max.y;
  677. return 0;
  678. }
  679. return -EINVAL;
  680. }
  681. static int pwc_enum_frameintervals(struct file *file, void *fh,
  682. struct v4l2_frmivalenum *fival)
  683. {
  684. struct pwc_device *pdev = video_drvdata(file);
  685. int size = -1;
  686. unsigned int i;
  687. for (i = 0; i < PSZ_MAX; i++) {
  688. if (pwc_image_sizes[i].x == fival->width &&
  689. pwc_image_sizes[i].y == fival->height) {
  690. size = i;
  691. break;
  692. }
  693. }
  694. /* TODO: Support raw format */
  695. if (size < 0 || fival->pixel_format != V4L2_PIX_FMT_YUV420)
  696. return -EINVAL;
  697. i = pwc_get_fps(pdev, fival->index, size);
  698. if (!i)
  699. return -EINVAL;
  700. fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
  701. fival->discrete.numerator = 1;
  702. fival->discrete.denominator = i;
  703. return 0;
  704. }
  705. static long pwc_default(struct file *file, void *fh, bool valid_prio,
  706. int cmd, void *arg)
  707. {
  708. struct pwc_device *pdev = video_drvdata(file);
  709. return pwc_ioctl(pdev, cmd, arg);
  710. }
  711. const struct v4l2_ioctl_ops pwc_ioctl_ops = {
  712. .vidioc_querycap = pwc_querycap,
  713. .vidioc_enum_input = pwc_enum_input,
  714. .vidioc_g_input = pwc_g_input,
  715. .vidioc_s_input = pwc_s_input,
  716. .vidioc_enum_fmt_vid_cap = pwc_enum_fmt_vid_cap,
  717. .vidioc_g_fmt_vid_cap = pwc_g_fmt_vid_cap,
  718. .vidioc_s_fmt_vid_cap = pwc_s_fmt_vid_cap,
  719. .vidioc_try_fmt_vid_cap = pwc_try_fmt_vid_cap,
  720. .vidioc_queryctrl = pwc_queryctrl,
  721. .vidioc_g_ctrl = pwc_g_ctrl,
  722. .vidioc_s_ctrl = pwc_s_ctrl,
  723. .vidioc_reqbufs = pwc_reqbufs,
  724. .vidioc_querybuf = pwc_querybuf,
  725. .vidioc_qbuf = pwc_qbuf,
  726. .vidioc_dqbuf = pwc_dqbuf,
  727. .vidioc_streamon = pwc_streamon,
  728. .vidioc_streamoff = pwc_streamoff,
  729. .vidioc_enum_framesizes = pwc_enum_framesizes,
  730. .vidioc_enum_frameintervals = pwc_enum_frameintervals,
  731. .vidioc_default = pwc_default,
  732. };
  733. /* vim: set cino= formatoptions=croql cindent shiftwidth=8 tabstop=8: */