pwc-v4l.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075
  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. (C) 2011 Hans de Goede <hdegoede@redhat.com>
  6. NOTE: this version of pwc is an unofficial (modified) release of pwc & pcwx
  7. driver and thus may have bugs that are not present in the original version.
  8. Please send bug reports and support requests to <luc@saillard.org>.
  9. The decompression routines have been implemented by reverse-engineering the
  10. Nemosoft binary pwcx module. Caveat emptor.
  11. This program is free software; you can redistribute it and/or modify
  12. it under the terms of the GNU General Public License as published by
  13. the Free Software Foundation; either version 2 of the License, or
  14. (at your option) any later version.
  15. This program is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. GNU General Public License for more details.
  19. You should have received a copy of the GNU General Public License
  20. along with this program; if not, write to the Free Software
  21. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. */
  23. #include <linux/errno.h>
  24. #include <linux/init.h>
  25. #include <linux/mm.h>
  26. #include <linux/module.h>
  27. #include <linux/poll.h>
  28. #include <linux/vmalloc.h>
  29. #include <linux/jiffies.h>
  30. #include <asm/io.h>
  31. #include "pwc.h"
  32. #define PWC_CID_CUSTOM(ctrl) ((V4L2_CID_USER_BASE | 0xf000) + custom_ ## ctrl)
  33. static int pwc_g_volatile_ctrl(struct v4l2_ctrl *ctrl);
  34. static int pwc_s_ctrl(struct v4l2_ctrl *ctrl);
  35. static const struct v4l2_ctrl_ops pwc_ctrl_ops = {
  36. .g_volatile_ctrl = pwc_g_volatile_ctrl,
  37. .s_ctrl = pwc_s_ctrl,
  38. };
  39. enum { awb_indoor, awb_outdoor, awb_fl, awb_manual, awb_auto };
  40. enum { custom_autocontour, custom_contour, custom_noise_reduction,
  41. custom_save_user, custom_restore_user, custom_restore_factory };
  42. const char * const pwc_auto_whitebal_qmenu[] = {
  43. "Indoor (Incandescant Lighting) Mode",
  44. "Outdoor (Sunlight) Mode",
  45. "Indoor (Fluorescent Lighting) Mode",
  46. "Manual Mode",
  47. "Auto Mode",
  48. NULL
  49. };
  50. static const struct v4l2_ctrl_config pwc_auto_white_balance_cfg = {
  51. .ops = &pwc_ctrl_ops,
  52. .id = V4L2_CID_AUTO_WHITE_BALANCE,
  53. .type = V4L2_CTRL_TYPE_MENU,
  54. .max = awb_auto,
  55. .qmenu = pwc_auto_whitebal_qmenu,
  56. };
  57. static const struct v4l2_ctrl_config pwc_autocontour_cfg = {
  58. .ops = &pwc_ctrl_ops,
  59. .id = PWC_CID_CUSTOM(autocontour),
  60. .type = V4L2_CTRL_TYPE_BOOLEAN,
  61. .name = "Auto contour",
  62. .min = 0,
  63. .max = 1,
  64. .step = 1,
  65. };
  66. static const struct v4l2_ctrl_config pwc_contour_cfg = {
  67. .ops = &pwc_ctrl_ops,
  68. .id = PWC_CID_CUSTOM(contour),
  69. .type = V4L2_CTRL_TYPE_INTEGER,
  70. .name = "Contour",
  71. .min = 0,
  72. .max = 63,
  73. .step = 1,
  74. };
  75. static const struct v4l2_ctrl_config pwc_backlight_cfg = {
  76. .ops = &pwc_ctrl_ops,
  77. .id = V4L2_CID_BACKLIGHT_COMPENSATION,
  78. .type = V4L2_CTRL_TYPE_BOOLEAN,
  79. .min = 0,
  80. .max = 1,
  81. .step = 1,
  82. };
  83. static const struct v4l2_ctrl_config pwc_flicker_cfg = {
  84. .ops = &pwc_ctrl_ops,
  85. .id = V4L2_CID_BAND_STOP_FILTER,
  86. .type = V4L2_CTRL_TYPE_BOOLEAN,
  87. .min = 0,
  88. .max = 1,
  89. .step = 1,
  90. };
  91. static const struct v4l2_ctrl_config pwc_noise_reduction_cfg = {
  92. .ops = &pwc_ctrl_ops,
  93. .id = PWC_CID_CUSTOM(noise_reduction),
  94. .type = V4L2_CTRL_TYPE_INTEGER,
  95. .name = "Dynamic Noise Reduction",
  96. .min = 0,
  97. .max = 3,
  98. .step = 1,
  99. };
  100. static const struct v4l2_ctrl_config pwc_save_user_cfg = {
  101. .ops = &pwc_ctrl_ops,
  102. .id = PWC_CID_CUSTOM(save_user),
  103. .type = V4L2_CTRL_TYPE_BUTTON,
  104. .name = "Save User Settings",
  105. };
  106. static const struct v4l2_ctrl_config pwc_restore_user_cfg = {
  107. .ops = &pwc_ctrl_ops,
  108. .id = PWC_CID_CUSTOM(restore_user),
  109. .type = V4L2_CTRL_TYPE_BUTTON,
  110. .name = "Restore User Settings",
  111. };
  112. static const struct v4l2_ctrl_config pwc_restore_factory_cfg = {
  113. .ops = &pwc_ctrl_ops,
  114. .id = PWC_CID_CUSTOM(restore_factory),
  115. .type = V4L2_CTRL_TYPE_BUTTON,
  116. .name = "Restore Factory Settings",
  117. };
  118. int pwc_init_controls(struct pwc_device *pdev)
  119. {
  120. struct v4l2_ctrl_handler *hdl;
  121. struct v4l2_ctrl_config cfg;
  122. int r, def;
  123. hdl = &pdev->ctrl_handler;
  124. r = v4l2_ctrl_handler_init(hdl, 20);
  125. if (r)
  126. return r;
  127. /* Brightness, contrast, saturation, gamma */
  128. r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, BRIGHTNESS_FORMATTER, &def);
  129. if (r || def > 127)
  130. def = 63;
  131. pdev->brightness = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
  132. V4L2_CID_BRIGHTNESS, 0, 127, 1, def);
  133. r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, CONTRAST_FORMATTER, &def);
  134. if (r || def > 63)
  135. def = 31;
  136. pdev->contrast = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
  137. V4L2_CID_CONTRAST, 0, 63, 1, def);
  138. if (pdev->type >= 675) {
  139. if (pdev->type < 730)
  140. pdev->saturation_fmt = SATURATION_MODE_FORMATTER2;
  141. else
  142. pdev->saturation_fmt = SATURATION_MODE_FORMATTER1;
  143. r = pwc_get_s8_ctrl(pdev, GET_CHROM_CTL, pdev->saturation_fmt,
  144. &def);
  145. if (r || def < -100 || def > 100)
  146. def = 0;
  147. pdev->saturation = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
  148. V4L2_CID_SATURATION, -100, 100, 1, def);
  149. }
  150. r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, GAMMA_FORMATTER, &def);
  151. if (r || def > 31)
  152. def = 15;
  153. pdev->gamma = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
  154. V4L2_CID_GAMMA, 0, 31, 1, def);
  155. /* auto white balance, red gain, blue gain */
  156. r = pwc_get_u8_ctrl(pdev, GET_CHROM_CTL, WB_MODE_FORMATTER, &def);
  157. if (r || def > awb_auto)
  158. def = awb_auto;
  159. cfg = pwc_auto_white_balance_cfg;
  160. cfg.name = v4l2_ctrl_get_name(cfg.id);
  161. cfg.def = def;
  162. pdev->auto_white_balance = v4l2_ctrl_new_custom(hdl, &cfg, NULL);
  163. /* check auto controls to avoid NULL deref in v4l2_ctrl_auto_cluster */
  164. if (!pdev->auto_white_balance)
  165. return hdl->error;
  166. r = pwc_get_u8_ctrl(pdev, GET_CHROM_CTL,
  167. PRESET_MANUAL_RED_GAIN_FORMATTER, &def);
  168. if (r)
  169. def = 127;
  170. pdev->red_balance = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
  171. V4L2_CID_RED_BALANCE, 0, 255, 1, def);
  172. r = pwc_get_u8_ctrl(pdev, GET_CHROM_CTL,
  173. PRESET_MANUAL_BLUE_GAIN_FORMATTER, &def);
  174. if (r)
  175. def = 127;
  176. pdev->blue_balance = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
  177. V4L2_CID_BLUE_BALANCE, 0, 255, 1, def);
  178. v4l2_ctrl_auto_cluster(3, &pdev->auto_white_balance, awb_manual,
  179. pdev->auto_white_balance->cur.val == awb_auto);
  180. /* autogain, gain */
  181. r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, AGC_MODE_FORMATTER, &def);
  182. if (r || (def != 0 && def != 0xff))
  183. def = 0;
  184. /* Note a register value if 0 means auto gain is on */
  185. pdev->autogain = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
  186. V4L2_CID_AUTOGAIN, 0, 1, 1, def == 0);
  187. if (!pdev->autogain)
  188. return hdl->error;
  189. r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, PRESET_AGC_FORMATTER, &def);
  190. if (r || def > 63)
  191. def = 31;
  192. pdev->gain = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
  193. V4L2_CID_GAIN, 0, 63, 1, def);
  194. /* auto exposure, exposure */
  195. if (DEVICE_USE_CODEC2(pdev->type)) {
  196. r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, SHUTTER_MODE_FORMATTER,
  197. &def);
  198. if (r || (def != 0 && def != 0xff))
  199. def = 0;
  200. /*
  201. * def = 0 auto, def = ff manual
  202. * menu idx 0 = auto, idx 1 = manual
  203. */
  204. pdev->exposure_auto = v4l2_ctrl_new_std_menu(hdl,
  205. &pwc_ctrl_ops,
  206. V4L2_CID_EXPOSURE_AUTO,
  207. 1, 0, def != 0);
  208. if (!pdev->exposure_auto)
  209. return hdl->error;
  210. /* GET_LUM_CTL, PRESET_SHUTTER_FORMATTER is unreliable */
  211. r = pwc_get_u16_ctrl(pdev, GET_STATUS_CTL,
  212. READ_SHUTTER_FORMATTER, &def);
  213. if (r || def > 655)
  214. def = 655;
  215. pdev->exposure = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
  216. V4L2_CID_EXPOSURE, 0, 655, 1, def);
  217. /* CODEC2: separate auto gain & auto exposure */
  218. v4l2_ctrl_auto_cluster(2, &pdev->autogain, 0, true);
  219. v4l2_ctrl_auto_cluster(2, &pdev->exposure_auto,
  220. V4L2_EXPOSURE_MANUAL, true);
  221. } else if (DEVICE_USE_CODEC3(pdev->type)) {
  222. /* GET_LUM_CTL, PRESET_SHUTTER_FORMATTER is unreliable */
  223. r = pwc_get_u16_ctrl(pdev, GET_STATUS_CTL,
  224. READ_SHUTTER_FORMATTER, &def);
  225. if (r || def > 255)
  226. def = 255;
  227. pdev->exposure = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
  228. V4L2_CID_EXPOSURE, 0, 255, 1, def);
  229. /* CODEC3: both gain and exposure controlled by autogain */
  230. pdev->autogain_expo_cluster[0] = pdev->autogain;
  231. pdev->autogain_expo_cluster[1] = pdev->gain;
  232. pdev->autogain_expo_cluster[2] = pdev->exposure;
  233. v4l2_ctrl_auto_cluster(3, pdev->autogain_expo_cluster,
  234. 0, true);
  235. }
  236. /* color / bw setting */
  237. r = pwc_get_u8_ctrl(pdev, GET_CHROM_CTL, COLOUR_MODE_FORMATTER,
  238. &def);
  239. if (r || (def != 0 && def != 0xff))
  240. def = 0xff;
  241. /* def = 0 bw, def = ff color, menu idx 0 = color, idx 1 = bw */
  242. pdev->colorfx = v4l2_ctrl_new_std_menu(hdl, &pwc_ctrl_ops,
  243. V4L2_CID_COLORFX, 1, 0, def == 0);
  244. /* autocontour, contour */
  245. r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, AUTO_CONTOUR_FORMATTER, &def);
  246. if (r || (def != 0 && def != 0xff))
  247. def = 0;
  248. cfg = pwc_autocontour_cfg;
  249. cfg.def = def == 0;
  250. pdev->autocontour = v4l2_ctrl_new_custom(hdl, &cfg, NULL);
  251. if (!pdev->autocontour)
  252. return hdl->error;
  253. r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, PRESET_CONTOUR_FORMATTER, &def);
  254. if (r || def > 63)
  255. def = 31;
  256. cfg = pwc_contour_cfg;
  257. cfg.def = def;
  258. pdev->contour = v4l2_ctrl_new_custom(hdl, &cfg, NULL);
  259. v4l2_ctrl_auto_cluster(2, &pdev->autocontour, 0, false);
  260. /* backlight */
  261. r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL,
  262. BACK_LIGHT_COMPENSATION_FORMATTER, &def);
  263. if (r || (def != 0 && def != 0xff))
  264. def = 0;
  265. cfg = pwc_backlight_cfg;
  266. cfg.name = v4l2_ctrl_get_name(cfg.id);
  267. cfg.def = def == 0;
  268. pdev->backlight = v4l2_ctrl_new_custom(hdl, &cfg, NULL);
  269. /* flikker rediction */
  270. r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL,
  271. FLICKERLESS_MODE_FORMATTER, &def);
  272. if (r || (def != 0 && def != 0xff))
  273. def = 0;
  274. cfg = pwc_flicker_cfg;
  275. cfg.name = v4l2_ctrl_get_name(cfg.id);
  276. cfg.def = def == 0;
  277. pdev->flicker = v4l2_ctrl_new_custom(hdl, &cfg, NULL);
  278. /* Dynamic noise reduction */
  279. r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL,
  280. DYNAMIC_NOISE_CONTROL_FORMATTER, &def);
  281. if (r || def > 3)
  282. def = 2;
  283. cfg = pwc_noise_reduction_cfg;
  284. cfg.def = def;
  285. pdev->noise_reduction = v4l2_ctrl_new_custom(hdl, &cfg, NULL);
  286. /* Save / Restore User / Factory Settings */
  287. pdev->save_user = v4l2_ctrl_new_custom(hdl, &pwc_save_user_cfg, NULL);
  288. pdev->restore_user = v4l2_ctrl_new_custom(hdl, &pwc_restore_user_cfg,
  289. NULL);
  290. if (pdev->restore_user)
  291. pdev->restore_user->flags = V4L2_CTRL_FLAG_UPDATE;
  292. pdev->restore_factory = v4l2_ctrl_new_custom(hdl,
  293. &pwc_restore_factory_cfg,
  294. NULL);
  295. if (pdev->restore_factory)
  296. pdev->restore_factory->flags = V4L2_CTRL_FLAG_UPDATE;
  297. return hdl->error;
  298. }
  299. static void pwc_vidioc_fill_fmt(const struct pwc_device *pdev, struct v4l2_format *f)
  300. {
  301. memset(&f->fmt.pix, 0, sizeof(struct v4l2_pix_format));
  302. f->fmt.pix.width = pdev->view.x;
  303. f->fmt.pix.height = pdev->view.y;
  304. f->fmt.pix.field = V4L2_FIELD_NONE;
  305. if (pdev->pixfmt == V4L2_PIX_FMT_YUV420) {
  306. f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUV420;
  307. f->fmt.pix.bytesperline = (f->fmt.pix.width * 3)/2;
  308. f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
  309. } else {
  310. /* vbandlength contains 4 lines ... */
  311. f->fmt.pix.bytesperline = pdev->vbandlength/4;
  312. f->fmt.pix.sizeimage = pdev->frame_size + sizeof(struct pwc_raw_frame);
  313. if (DEVICE_USE_CODEC1(pdev->type))
  314. f->fmt.pix.pixelformat = V4L2_PIX_FMT_PWC1;
  315. else
  316. f->fmt.pix.pixelformat = V4L2_PIX_FMT_PWC2;
  317. }
  318. PWC_DEBUG_IOCTL("pwc_vidioc_fill_fmt() "
  319. "width=%d, height=%d, bytesperline=%d, sizeimage=%d, pixelformat=%c%c%c%c\n",
  320. f->fmt.pix.width,
  321. f->fmt.pix.height,
  322. f->fmt.pix.bytesperline,
  323. f->fmt.pix.sizeimage,
  324. (f->fmt.pix.pixelformat)&255,
  325. (f->fmt.pix.pixelformat>>8)&255,
  326. (f->fmt.pix.pixelformat>>16)&255,
  327. (f->fmt.pix.pixelformat>>24)&255);
  328. }
  329. /* ioctl(VIDIOC_TRY_FMT) */
  330. static int pwc_vidioc_try_fmt(struct pwc_device *pdev, struct v4l2_format *f)
  331. {
  332. if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  333. PWC_DEBUG_IOCTL("Bad video type must be V4L2_BUF_TYPE_VIDEO_CAPTURE\n");
  334. return -EINVAL;
  335. }
  336. switch (f->fmt.pix.pixelformat) {
  337. case V4L2_PIX_FMT_YUV420:
  338. break;
  339. case V4L2_PIX_FMT_PWC1:
  340. if (DEVICE_USE_CODEC23(pdev->type)) {
  341. PWC_DEBUG_IOCTL("codec1 is only supported for old pwc webcam\n");
  342. return -EINVAL;
  343. }
  344. break;
  345. case V4L2_PIX_FMT_PWC2:
  346. if (DEVICE_USE_CODEC1(pdev->type)) {
  347. PWC_DEBUG_IOCTL("codec23 is only supported for new pwc webcam\n");
  348. return -EINVAL;
  349. }
  350. break;
  351. default:
  352. PWC_DEBUG_IOCTL("Unsupported pixel format\n");
  353. return -EINVAL;
  354. }
  355. if (f->fmt.pix.width > pdev->view_max.x)
  356. f->fmt.pix.width = pdev->view_max.x;
  357. else if (f->fmt.pix.width < pdev->view_min.x)
  358. f->fmt.pix.width = pdev->view_min.x;
  359. if (f->fmt.pix.height > pdev->view_max.y)
  360. f->fmt.pix.height = pdev->view_max.y;
  361. else if (f->fmt.pix.height < pdev->view_min.y)
  362. f->fmt.pix.height = pdev->view_min.y;
  363. return 0;
  364. }
  365. /* ioctl(VIDIOC_SET_FMT) */
  366. static int pwc_s_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f)
  367. {
  368. struct pwc_device *pdev = video_drvdata(file);
  369. int ret, fps, snapshot, compression, pixelformat;
  370. if (!pdev->udev)
  371. return -ENODEV;
  372. if (pdev->capt_file != NULL &&
  373. pdev->capt_file != file)
  374. return -EBUSY;
  375. pdev->capt_file = file;
  376. ret = pwc_vidioc_try_fmt(pdev, f);
  377. if (ret<0)
  378. return ret;
  379. pixelformat = f->fmt.pix.pixelformat;
  380. compression = pdev->vcompression;
  381. snapshot = 0;
  382. fps = pdev->vframes;
  383. if (f->fmt.pix.priv) {
  384. compression = (f->fmt.pix.priv & PWC_QLT_MASK) >> PWC_QLT_SHIFT;
  385. snapshot = !!(f->fmt.pix.priv & PWC_FPS_SNAPSHOT);
  386. fps = (f->fmt.pix.priv & PWC_FPS_FRMASK) >> PWC_FPS_SHIFT;
  387. if (fps == 0)
  388. fps = pdev->vframes;
  389. }
  390. if (pixelformat != V4L2_PIX_FMT_YUV420 &&
  391. pixelformat != V4L2_PIX_FMT_PWC1 &&
  392. pixelformat != V4L2_PIX_FMT_PWC2)
  393. return -EINVAL;
  394. if (vb2_is_streaming(&pdev->vb_queue))
  395. return -EBUSY;
  396. PWC_DEBUG_IOCTL("Trying to set format to: width=%d height=%d fps=%d "
  397. "compression=%d snapshot=%d format=%c%c%c%c\n",
  398. f->fmt.pix.width, f->fmt.pix.height, fps,
  399. compression, snapshot,
  400. (pixelformat)&255,
  401. (pixelformat>>8)&255,
  402. (pixelformat>>16)&255,
  403. (pixelformat>>24)&255);
  404. ret = pwc_set_video_mode(pdev,
  405. f->fmt.pix.width,
  406. f->fmt.pix.height,
  407. fps,
  408. compression,
  409. snapshot);
  410. PWC_DEBUG_IOCTL("pwc_set_video_mode(), return=%d\n", ret);
  411. if (ret)
  412. return ret;
  413. pdev->pixfmt = pixelformat;
  414. pwc_vidioc_fill_fmt(pdev, f);
  415. return 0;
  416. }
  417. static int pwc_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
  418. {
  419. struct pwc_device *pdev = video_drvdata(file);
  420. if (!pdev->udev)
  421. return -ENODEV;
  422. strcpy(cap->driver, PWC_NAME);
  423. strlcpy(cap->card, pdev->vdev.name, sizeof(cap->card));
  424. usb_make_path(pdev->udev, cap->bus_info, sizeof(cap->bus_info));
  425. cap->capabilities =
  426. V4L2_CAP_VIDEO_CAPTURE |
  427. V4L2_CAP_STREAMING |
  428. V4L2_CAP_READWRITE;
  429. return 0;
  430. }
  431. static int pwc_enum_input(struct file *file, void *fh, struct v4l2_input *i)
  432. {
  433. if (i->index) /* Only one INPUT is supported */
  434. return -EINVAL;
  435. strcpy(i->name, "usb");
  436. return 0;
  437. }
  438. static int pwc_g_input(struct file *file, void *fh, unsigned int *i)
  439. {
  440. *i = 0;
  441. return 0;
  442. }
  443. static int pwc_s_input(struct file *file, void *fh, unsigned int i)
  444. {
  445. return i ? -EINVAL : 0;
  446. }
  447. static int pwc_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
  448. {
  449. struct pwc_device *pdev =
  450. container_of(ctrl->handler, struct pwc_device, ctrl_handler);
  451. int ret = 0;
  452. /*
  453. * Sometimes it can take quite long for the pwc to complete usb control
  454. * transfers, so release the modlock to give streaming by another
  455. * process / thread the chance to continue with a dqbuf.
  456. */
  457. mutex_unlock(&pdev->modlock);
  458. /*
  459. * Take the udev-lock to protect against the disconnect handler
  460. * completing and setting dev->udev to NULL underneath us. Other code
  461. * does not need to do this since it is protected by the modlock.
  462. */
  463. mutex_lock(&pdev->udevlock);
  464. if (!pdev->udev) {
  465. ret = -ENODEV;
  466. goto leave;
  467. }
  468. switch (ctrl->id) {
  469. case V4L2_CID_AUTO_WHITE_BALANCE:
  470. if (pdev->color_bal_valid && time_before(jiffies,
  471. pdev->last_color_bal_update + HZ / 4)) {
  472. pdev->red_balance->val = pdev->last_red_balance;
  473. pdev->blue_balance->val = pdev->last_blue_balance;
  474. break;
  475. }
  476. ret = pwc_get_u8_ctrl(pdev, GET_STATUS_CTL,
  477. READ_RED_GAIN_FORMATTER,
  478. &pdev->red_balance->val);
  479. if (ret)
  480. break;
  481. ret = pwc_get_u8_ctrl(pdev, GET_STATUS_CTL,
  482. READ_BLUE_GAIN_FORMATTER,
  483. &pdev->blue_balance->val);
  484. if (ret)
  485. break;
  486. pdev->last_red_balance = pdev->red_balance->val;
  487. pdev->last_blue_balance = pdev->blue_balance->val;
  488. pdev->last_color_bal_update = jiffies;
  489. pdev->color_bal_valid = true;
  490. break;
  491. case V4L2_CID_AUTOGAIN:
  492. if (pdev->gain_valid && time_before(jiffies,
  493. pdev->last_gain_update + HZ / 4)) {
  494. pdev->gain->val = pdev->last_gain;
  495. break;
  496. }
  497. ret = pwc_get_u8_ctrl(pdev, GET_STATUS_CTL,
  498. READ_AGC_FORMATTER, &pdev->gain->val);
  499. if (ret)
  500. break;
  501. pdev->last_gain = pdev->gain->val;
  502. pdev->last_gain_update = jiffies;
  503. pdev->gain_valid = true;
  504. if (!DEVICE_USE_CODEC3(pdev->type))
  505. break;
  506. /* Fall through for CODEC3 where autogain also controls expo */
  507. case V4L2_CID_EXPOSURE_AUTO:
  508. if (pdev->exposure_valid && time_before(jiffies,
  509. pdev->last_exposure_update + HZ / 4)) {
  510. pdev->exposure->val = pdev->last_exposure;
  511. break;
  512. }
  513. ret = pwc_get_u16_ctrl(pdev, GET_STATUS_CTL,
  514. READ_SHUTTER_FORMATTER,
  515. &pdev->exposure->val);
  516. if (ret)
  517. break;
  518. pdev->last_exposure = pdev->exposure->val;
  519. pdev->last_exposure_update = jiffies;
  520. pdev->exposure_valid = true;
  521. break;
  522. default:
  523. ret = -EINVAL;
  524. }
  525. if (ret)
  526. PWC_ERROR("g_ctrl %s error %d\n", ctrl->name, ret);
  527. leave:
  528. mutex_unlock(&pdev->udevlock);
  529. mutex_lock(&pdev->modlock);
  530. return ret;
  531. }
  532. static int pwc_set_awb(struct pwc_device *pdev)
  533. {
  534. int ret = 0;
  535. if (pdev->auto_white_balance->is_new) {
  536. ret = pwc_set_u8_ctrl(pdev, SET_CHROM_CTL,
  537. WB_MODE_FORMATTER,
  538. pdev->auto_white_balance->val);
  539. if (ret)
  540. return ret;
  541. /* Update val when coming from auto or going to a preset */
  542. if (pdev->red_balance->is_volatile ||
  543. pdev->auto_white_balance->val == awb_indoor ||
  544. pdev->auto_white_balance->val == awb_outdoor ||
  545. pdev->auto_white_balance->val == awb_fl) {
  546. if (!pdev->red_balance->is_new)
  547. pwc_get_u8_ctrl(pdev, GET_STATUS_CTL,
  548. READ_RED_GAIN_FORMATTER,
  549. &pdev->red_balance->val);
  550. if (!pdev->blue_balance->is_new)
  551. pwc_get_u8_ctrl(pdev, GET_STATUS_CTL,
  552. READ_BLUE_GAIN_FORMATTER,
  553. &pdev->blue_balance->val);
  554. }
  555. if (pdev->auto_white_balance->val == awb_auto) {
  556. pdev->red_balance->is_volatile = true;
  557. pdev->blue_balance->is_volatile = true;
  558. pdev->color_bal_valid = false; /* Force cache update */
  559. } else {
  560. pdev->red_balance->is_volatile = false;
  561. pdev->blue_balance->is_volatile = false;
  562. }
  563. }
  564. if (ret == 0 && pdev->red_balance->is_new) {
  565. if (pdev->auto_white_balance->val != awb_manual)
  566. return -EBUSY;
  567. ret = pwc_set_u8_ctrl(pdev, SET_CHROM_CTL,
  568. PRESET_MANUAL_RED_GAIN_FORMATTER,
  569. pdev->red_balance->val);
  570. }
  571. if (ret == 0 && pdev->blue_balance->is_new) {
  572. if (pdev->auto_white_balance->val != awb_manual)
  573. return -EBUSY;
  574. ret = pwc_set_u8_ctrl(pdev, SET_CHROM_CTL,
  575. PRESET_MANUAL_BLUE_GAIN_FORMATTER,
  576. pdev->blue_balance->val);
  577. }
  578. return ret;
  579. }
  580. /* For CODEC2 models which have separate autogain and auto exposure */
  581. static int pwc_set_autogain(struct pwc_device *pdev)
  582. {
  583. int ret = 0;
  584. if (pdev->autogain->is_new) {
  585. ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
  586. AGC_MODE_FORMATTER,
  587. pdev->autogain->val ? 0 : 0xff);
  588. if (ret)
  589. return ret;
  590. if (pdev->autogain->val)
  591. pdev->gain_valid = false; /* Force cache update */
  592. else if (!pdev->gain->is_new)
  593. pwc_get_u8_ctrl(pdev, GET_STATUS_CTL,
  594. READ_AGC_FORMATTER,
  595. &pdev->gain->val);
  596. }
  597. if (ret == 0 && pdev->gain->is_new) {
  598. if (pdev->autogain->val)
  599. return -EBUSY;
  600. ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
  601. PRESET_AGC_FORMATTER,
  602. pdev->gain->val);
  603. }
  604. return ret;
  605. }
  606. /* For CODEC2 models which have separate autogain and auto exposure */
  607. static int pwc_set_exposure_auto(struct pwc_device *pdev)
  608. {
  609. int ret = 0;
  610. int is_auto = pdev->exposure_auto->val == V4L2_EXPOSURE_AUTO;
  611. if (pdev->exposure_auto->is_new) {
  612. ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
  613. SHUTTER_MODE_FORMATTER,
  614. is_auto ? 0 : 0xff);
  615. if (ret)
  616. return ret;
  617. if (is_auto)
  618. pdev->exposure_valid = false; /* Force cache update */
  619. else if (!pdev->exposure->is_new)
  620. pwc_get_u16_ctrl(pdev, GET_STATUS_CTL,
  621. READ_SHUTTER_FORMATTER,
  622. &pdev->exposure->val);
  623. }
  624. if (ret == 0 && pdev->exposure->is_new) {
  625. if (is_auto)
  626. return -EBUSY;
  627. ret = pwc_set_u16_ctrl(pdev, SET_LUM_CTL,
  628. PRESET_SHUTTER_FORMATTER,
  629. pdev->exposure->val);
  630. }
  631. return ret;
  632. }
  633. /* For CODEC3 models which have autogain controlling both gain and exposure */
  634. static int pwc_set_autogain_expo(struct pwc_device *pdev)
  635. {
  636. int ret = 0;
  637. if (pdev->autogain->is_new) {
  638. ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
  639. AGC_MODE_FORMATTER,
  640. pdev->autogain->val ? 0 : 0xff);
  641. if (ret)
  642. return ret;
  643. if (pdev->autogain->val) {
  644. pdev->gain_valid = false; /* Force cache update */
  645. pdev->exposure_valid = false; /* Force cache update */
  646. } else {
  647. if (!pdev->gain->is_new)
  648. pwc_get_u8_ctrl(pdev, GET_STATUS_CTL,
  649. READ_AGC_FORMATTER,
  650. &pdev->gain->val);
  651. if (!pdev->exposure->is_new)
  652. pwc_get_u16_ctrl(pdev, GET_STATUS_CTL,
  653. READ_SHUTTER_FORMATTER,
  654. &pdev->exposure->val);
  655. }
  656. }
  657. if (ret == 0 && pdev->gain->is_new) {
  658. if (pdev->autogain->val)
  659. return -EBUSY;
  660. ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
  661. PRESET_AGC_FORMATTER,
  662. pdev->gain->val);
  663. }
  664. if (ret == 0 && pdev->exposure->is_new) {
  665. if (pdev->autogain->val)
  666. return -EBUSY;
  667. ret = pwc_set_u16_ctrl(pdev, SET_LUM_CTL,
  668. PRESET_SHUTTER_FORMATTER,
  669. pdev->exposure->val);
  670. }
  671. return ret;
  672. }
  673. static int pwc_s_ctrl(struct v4l2_ctrl *ctrl)
  674. {
  675. struct pwc_device *pdev =
  676. container_of(ctrl->handler, struct pwc_device, ctrl_handler);
  677. int ret = 0;
  678. /* See the comments on locking in pwc_g_volatile_ctrl */
  679. mutex_unlock(&pdev->modlock);
  680. mutex_lock(&pdev->udevlock);
  681. if (!pdev->udev) {
  682. ret = -ENODEV;
  683. goto leave;
  684. }
  685. switch (ctrl->id) {
  686. case V4L2_CID_BRIGHTNESS:
  687. ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
  688. BRIGHTNESS_FORMATTER, ctrl->val);
  689. break;
  690. case V4L2_CID_CONTRAST:
  691. ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
  692. CONTRAST_FORMATTER, ctrl->val);
  693. break;
  694. case V4L2_CID_SATURATION:
  695. ret = pwc_set_s8_ctrl(pdev, SET_CHROM_CTL,
  696. pdev->saturation_fmt, ctrl->val);
  697. break;
  698. case V4L2_CID_GAMMA:
  699. ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
  700. GAMMA_FORMATTER, ctrl->val);
  701. break;
  702. case V4L2_CID_AUTO_WHITE_BALANCE:
  703. ret = pwc_set_awb(pdev);
  704. break;
  705. case V4L2_CID_AUTOGAIN:
  706. if (DEVICE_USE_CODEC2(pdev->type))
  707. ret = pwc_set_autogain(pdev);
  708. else if (DEVICE_USE_CODEC3(pdev->type))
  709. ret = pwc_set_autogain_expo(pdev);
  710. else
  711. ret = -EINVAL;
  712. break;
  713. case V4L2_CID_EXPOSURE_AUTO:
  714. if (DEVICE_USE_CODEC2(pdev->type))
  715. ret = pwc_set_exposure_auto(pdev);
  716. else
  717. ret = -EINVAL;
  718. break;
  719. case V4L2_CID_COLORFX:
  720. ret = pwc_set_u8_ctrl(pdev, SET_CHROM_CTL,
  721. COLOUR_MODE_FORMATTER,
  722. ctrl->val ? 0 : 0xff);
  723. break;
  724. case PWC_CID_CUSTOM(autocontour):
  725. if (pdev->autocontour->is_new) {
  726. ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
  727. AUTO_CONTOUR_FORMATTER,
  728. pdev->autocontour->val ? 0 : 0xff);
  729. }
  730. if (ret == 0 && pdev->contour->is_new) {
  731. if (pdev->autocontour->val) {
  732. ret = -EBUSY;
  733. break;
  734. }
  735. ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
  736. PRESET_CONTOUR_FORMATTER,
  737. pdev->contour->val);
  738. }
  739. break;
  740. case V4L2_CID_BACKLIGHT_COMPENSATION:
  741. ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
  742. BACK_LIGHT_COMPENSATION_FORMATTER,
  743. ctrl->val ? 0 : 0xff);
  744. break;
  745. case V4L2_CID_BAND_STOP_FILTER:
  746. ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
  747. FLICKERLESS_MODE_FORMATTER,
  748. ctrl->val ? 0 : 0xff);
  749. break;
  750. case PWC_CID_CUSTOM(noise_reduction):
  751. ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
  752. DYNAMIC_NOISE_CONTROL_FORMATTER,
  753. ctrl->val);
  754. break;
  755. case PWC_CID_CUSTOM(save_user):
  756. ret = pwc_button_ctrl(pdev, SAVE_USER_DEFAULTS_FORMATTER);
  757. break;
  758. case PWC_CID_CUSTOM(restore_user):
  759. ret = pwc_button_ctrl(pdev, RESTORE_USER_DEFAULTS_FORMATTER);
  760. break;
  761. case PWC_CID_CUSTOM(restore_factory):
  762. ret = pwc_button_ctrl(pdev,
  763. RESTORE_FACTORY_DEFAULTS_FORMATTER);
  764. break;
  765. default:
  766. ret = -EINVAL;
  767. }
  768. if (ret)
  769. PWC_ERROR("s_ctrl %s error %d\n", ctrl->name, ret);
  770. leave:
  771. mutex_unlock(&pdev->udevlock);
  772. mutex_lock(&pdev->modlock);
  773. return ret;
  774. }
  775. static int pwc_enum_fmt_vid_cap(struct file *file, void *fh, struct v4l2_fmtdesc *f)
  776. {
  777. struct pwc_device *pdev = video_drvdata(file);
  778. /* We only support two format: the raw format, and YUV */
  779. switch (f->index) {
  780. case 0:
  781. /* RAW format */
  782. f->pixelformat = pdev->type <= 646 ? V4L2_PIX_FMT_PWC1 : V4L2_PIX_FMT_PWC2;
  783. f->flags = V4L2_FMT_FLAG_COMPRESSED;
  784. strlcpy(f->description, "Raw Philips Webcam", sizeof(f->description));
  785. break;
  786. case 1:
  787. f->pixelformat = V4L2_PIX_FMT_YUV420;
  788. strlcpy(f->description, "4:2:0, planar, Y-Cb-Cr", sizeof(f->description));
  789. break;
  790. default:
  791. return -EINVAL;
  792. }
  793. return 0;
  794. }
  795. static int pwc_g_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f)
  796. {
  797. struct pwc_device *pdev = video_drvdata(file);
  798. PWC_DEBUG_IOCTL("ioctl(VIDIOC_G_FMT) return size %dx%d\n",
  799. pdev->image.x, pdev->image.y);
  800. pwc_vidioc_fill_fmt(pdev, f);
  801. return 0;
  802. }
  803. static int pwc_try_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f)
  804. {
  805. struct pwc_device *pdev = video_drvdata(file);
  806. return pwc_vidioc_try_fmt(pdev, f);
  807. }
  808. static int pwc_reqbufs(struct file *file, void *fh,
  809. struct v4l2_requestbuffers *rb)
  810. {
  811. struct pwc_device *pdev = video_drvdata(file);
  812. if (pdev->capt_file != NULL &&
  813. pdev->capt_file != file)
  814. return -EBUSY;
  815. pdev->capt_file = file;
  816. return vb2_reqbufs(&pdev->vb_queue, rb);
  817. }
  818. static int pwc_querybuf(struct file *file, void *fh, struct v4l2_buffer *buf)
  819. {
  820. struct pwc_device *pdev = video_drvdata(file);
  821. return vb2_querybuf(&pdev->vb_queue, buf);
  822. }
  823. static int pwc_qbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
  824. {
  825. struct pwc_device *pdev = video_drvdata(file);
  826. if (!pdev->udev)
  827. return -ENODEV;
  828. if (pdev->capt_file != file)
  829. return -EBUSY;
  830. return vb2_qbuf(&pdev->vb_queue, buf);
  831. }
  832. static int pwc_dqbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
  833. {
  834. struct pwc_device *pdev = video_drvdata(file);
  835. if (!pdev->udev)
  836. return -ENODEV;
  837. if (pdev->capt_file != file)
  838. return -EBUSY;
  839. return vb2_dqbuf(&pdev->vb_queue, buf, file->f_flags & O_NONBLOCK);
  840. }
  841. static int pwc_streamon(struct file *file, void *fh, enum v4l2_buf_type i)
  842. {
  843. struct pwc_device *pdev = video_drvdata(file);
  844. if (!pdev->udev)
  845. return -ENODEV;
  846. if (pdev->capt_file != file)
  847. return -EBUSY;
  848. return vb2_streamon(&pdev->vb_queue, i);
  849. }
  850. static int pwc_streamoff(struct file *file, void *fh, enum v4l2_buf_type i)
  851. {
  852. struct pwc_device *pdev = video_drvdata(file);
  853. if (!pdev->udev)
  854. return -ENODEV;
  855. if (pdev->capt_file != file)
  856. return -EBUSY;
  857. return vb2_streamoff(&pdev->vb_queue, i);
  858. }
  859. static int pwc_enum_framesizes(struct file *file, void *fh,
  860. struct v4l2_frmsizeenum *fsize)
  861. {
  862. struct pwc_device *pdev = video_drvdata(file);
  863. unsigned int i = 0, index = fsize->index;
  864. if (fsize->pixel_format == V4L2_PIX_FMT_YUV420) {
  865. for (i = 0; i < PSZ_MAX; i++) {
  866. if (pdev->image_mask & (1UL << i)) {
  867. if (!index--) {
  868. fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
  869. fsize->discrete.width = pwc_image_sizes[i].x;
  870. fsize->discrete.height = pwc_image_sizes[i].y;
  871. return 0;
  872. }
  873. }
  874. }
  875. } else if (fsize->index == 0 &&
  876. ((fsize->pixel_format == V4L2_PIX_FMT_PWC1 && DEVICE_USE_CODEC1(pdev->type)) ||
  877. (fsize->pixel_format == V4L2_PIX_FMT_PWC2 && DEVICE_USE_CODEC23(pdev->type)))) {
  878. fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
  879. fsize->discrete.width = pdev->abs_max.x;
  880. fsize->discrete.height = pdev->abs_max.y;
  881. return 0;
  882. }
  883. return -EINVAL;
  884. }
  885. static int pwc_enum_frameintervals(struct file *file, void *fh,
  886. struct v4l2_frmivalenum *fival)
  887. {
  888. struct pwc_device *pdev = video_drvdata(file);
  889. int size = -1;
  890. unsigned int i;
  891. for (i = 0; i < PSZ_MAX; i++) {
  892. if (pwc_image_sizes[i].x == fival->width &&
  893. pwc_image_sizes[i].y == fival->height) {
  894. size = i;
  895. break;
  896. }
  897. }
  898. /* TODO: Support raw format */
  899. if (size < 0 || fival->pixel_format != V4L2_PIX_FMT_YUV420)
  900. return -EINVAL;
  901. i = pwc_get_fps(pdev, fival->index, size);
  902. if (!i)
  903. return -EINVAL;
  904. fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
  905. fival->discrete.numerator = 1;
  906. fival->discrete.denominator = i;
  907. return 0;
  908. }
  909. static long pwc_default(struct file *file, void *fh, bool valid_prio,
  910. int cmd, void *arg)
  911. {
  912. struct pwc_device *pdev = video_drvdata(file);
  913. return pwc_ioctl(pdev, cmd, arg);
  914. }
  915. const struct v4l2_ioctl_ops pwc_ioctl_ops = {
  916. .vidioc_querycap = pwc_querycap,
  917. .vidioc_enum_input = pwc_enum_input,
  918. .vidioc_g_input = pwc_g_input,
  919. .vidioc_s_input = pwc_s_input,
  920. .vidioc_enum_fmt_vid_cap = pwc_enum_fmt_vid_cap,
  921. .vidioc_g_fmt_vid_cap = pwc_g_fmt_vid_cap,
  922. .vidioc_s_fmt_vid_cap = pwc_s_fmt_vid_cap,
  923. .vidioc_try_fmt_vid_cap = pwc_try_fmt_vid_cap,
  924. .vidioc_reqbufs = pwc_reqbufs,
  925. .vidioc_querybuf = pwc_querybuf,
  926. .vidioc_qbuf = pwc_qbuf,
  927. .vidioc_dqbuf = pwc_dqbuf,
  928. .vidioc_streamon = pwc_streamon,
  929. .vidioc_streamoff = pwc_streamoff,
  930. .vidioc_enum_framesizes = pwc_enum_framesizes,
  931. .vidioc_enum_frameintervals = pwc_enum_frameintervals,
  932. .vidioc_default = pwc_default,
  933. };
  934. /* vim: set cino= formatoptions=croql cindent shiftwidth=8 tabstop=8: */