pwc-v4l.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131
  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. if (!(pdev->features & FEATURE_MOTOR_PANTILT))
  298. return hdl->error;
  299. /* Motor pan / tilt / reset */
  300. pdev->motor_pan = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
  301. V4L2_CID_PAN_RELATIVE, -4480, 4480, 64, 0);
  302. if (!pdev->motor_pan)
  303. return hdl->error;
  304. pdev->motor_tilt = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
  305. V4L2_CID_TILT_RELATIVE, -1920, 1920, 64, 0);
  306. pdev->motor_pan_reset = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
  307. V4L2_CID_PAN_RESET, 0, 0, 0, 0);
  308. pdev->motor_tilt_reset = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
  309. V4L2_CID_TILT_RESET, 0, 0, 0, 0);
  310. v4l2_ctrl_cluster(4, &pdev->motor_pan);
  311. return hdl->error;
  312. }
  313. static void pwc_vidioc_fill_fmt(const struct pwc_device *pdev, struct v4l2_format *f)
  314. {
  315. memset(&f->fmt.pix, 0, sizeof(struct v4l2_pix_format));
  316. f->fmt.pix.width = pdev->view.x;
  317. f->fmt.pix.height = pdev->view.y;
  318. f->fmt.pix.field = V4L2_FIELD_NONE;
  319. if (pdev->pixfmt == V4L2_PIX_FMT_YUV420) {
  320. f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUV420;
  321. f->fmt.pix.bytesperline = (f->fmt.pix.width * 3)/2;
  322. f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
  323. } else {
  324. /* vbandlength contains 4 lines ... */
  325. f->fmt.pix.bytesperline = pdev->vbandlength/4;
  326. f->fmt.pix.sizeimage = pdev->frame_size + sizeof(struct pwc_raw_frame);
  327. if (DEVICE_USE_CODEC1(pdev->type))
  328. f->fmt.pix.pixelformat = V4L2_PIX_FMT_PWC1;
  329. else
  330. f->fmt.pix.pixelformat = V4L2_PIX_FMT_PWC2;
  331. }
  332. PWC_DEBUG_IOCTL("pwc_vidioc_fill_fmt() "
  333. "width=%d, height=%d, bytesperline=%d, sizeimage=%d, pixelformat=%c%c%c%c\n",
  334. f->fmt.pix.width,
  335. f->fmt.pix.height,
  336. f->fmt.pix.bytesperline,
  337. f->fmt.pix.sizeimage,
  338. (f->fmt.pix.pixelformat)&255,
  339. (f->fmt.pix.pixelformat>>8)&255,
  340. (f->fmt.pix.pixelformat>>16)&255,
  341. (f->fmt.pix.pixelformat>>24)&255);
  342. }
  343. /* ioctl(VIDIOC_TRY_FMT) */
  344. static int pwc_vidioc_try_fmt(struct pwc_device *pdev, struct v4l2_format *f)
  345. {
  346. if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  347. PWC_DEBUG_IOCTL("Bad video type must be V4L2_BUF_TYPE_VIDEO_CAPTURE\n");
  348. return -EINVAL;
  349. }
  350. switch (f->fmt.pix.pixelformat) {
  351. case V4L2_PIX_FMT_YUV420:
  352. break;
  353. case V4L2_PIX_FMT_PWC1:
  354. if (DEVICE_USE_CODEC23(pdev->type)) {
  355. PWC_DEBUG_IOCTL("codec1 is only supported for old pwc webcam\n");
  356. return -EINVAL;
  357. }
  358. break;
  359. case V4L2_PIX_FMT_PWC2:
  360. if (DEVICE_USE_CODEC1(pdev->type)) {
  361. PWC_DEBUG_IOCTL("codec23 is only supported for new pwc webcam\n");
  362. return -EINVAL;
  363. }
  364. break;
  365. default:
  366. PWC_DEBUG_IOCTL("Unsupported pixel format\n");
  367. return -EINVAL;
  368. }
  369. if (f->fmt.pix.width > pdev->view_max.x)
  370. f->fmt.pix.width = pdev->view_max.x;
  371. else if (f->fmt.pix.width < pdev->view_min.x)
  372. f->fmt.pix.width = pdev->view_min.x;
  373. if (f->fmt.pix.height > pdev->view_max.y)
  374. f->fmt.pix.height = pdev->view_max.y;
  375. else if (f->fmt.pix.height < pdev->view_min.y)
  376. f->fmt.pix.height = pdev->view_min.y;
  377. return 0;
  378. }
  379. /* ioctl(VIDIOC_SET_FMT) */
  380. static int pwc_s_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f)
  381. {
  382. struct pwc_device *pdev = video_drvdata(file);
  383. int ret, fps, snapshot, compression, pixelformat;
  384. if (!pdev->udev)
  385. return -ENODEV;
  386. if (pdev->capt_file != NULL &&
  387. pdev->capt_file != file)
  388. return -EBUSY;
  389. pdev->capt_file = file;
  390. ret = pwc_vidioc_try_fmt(pdev, f);
  391. if (ret<0)
  392. return ret;
  393. pixelformat = f->fmt.pix.pixelformat;
  394. compression = pdev->vcompression;
  395. snapshot = 0;
  396. fps = pdev->vframes;
  397. if (f->fmt.pix.priv) {
  398. compression = (f->fmt.pix.priv & PWC_QLT_MASK) >> PWC_QLT_SHIFT;
  399. snapshot = !!(f->fmt.pix.priv & PWC_FPS_SNAPSHOT);
  400. fps = (f->fmt.pix.priv & PWC_FPS_FRMASK) >> PWC_FPS_SHIFT;
  401. if (fps == 0)
  402. fps = pdev->vframes;
  403. }
  404. if (pixelformat != V4L2_PIX_FMT_YUV420 &&
  405. pixelformat != V4L2_PIX_FMT_PWC1 &&
  406. pixelformat != V4L2_PIX_FMT_PWC2)
  407. return -EINVAL;
  408. if (vb2_is_streaming(&pdev->vb_queue))
  409. return -EBUSY;
  410. PWC_DEBUG_IOCTL("Trying to set format to: width=%d height=%d fps=%d "
  411. "compression=%d snapshot=%d format=%c%c%c%c\n",
  412. f->fmt.pix.width, f->fmt.pix.height, fps,
  413. compression, snapshot,
  414. (pixelformat)&255,
  415. (pixelformat>>8)&255,
  416. (pixelformat>>16)&255,
  417. (pixelformat>>24)&255);
  418. ret = pwc_set_video_mode(pdev,
  419. f->fmt.pix.width,
  420. f->fmt.pix.height,
  421. fps,
  422. compression,
  423. snapshot);
  424. PWC_DEBUG_IOCTL("pwc_set_video_mode(), return=%d\n", ret);
  425. if (ret)
  426. return ret;
  427. pdev->pixfmt = pixelformat;
  428. pwc_vidioc_fill_fmt(pdev, f);
  429. return 0;
  430. }
  431. static int pwc_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
  432. {
  433. struct pwc_device *pdev = video_drvdata(file);
  434. if (!pdev->udev)
  435. return -ENODEV;
  436. strcpy(cap->driver, PWC_NAME);
  437. strlcpy(cap->card, pdev->vdev.name, sizeof(cap->card));
  438. usb_make_path(pdev->udev, cap->bus_info, sizeof(cap->bus_info));
  439. cap->capabilities =
  440. V4L2_CAP_VIDEO_CAPTURE |
  441. V4L2_CAP_STREAMING |
  442. V4L2_CAP_READWRITE;
  443. return 0;
  444. }
  445. static int pwc_enum_input(struct file *file, void *fh, struct v4l2_input *i)
  446. {
  447. if (i->index) /* Only one INPUT is supported */
  448. return -EINVAL;
  449. strcpy(i->name, "usb");
  450. return 0;
  451. }
  452. static int pwc_g_input(struct file *file, void *fh, unsigned int *i)
  453. {
  454. *i = 0;
  455. return 0;
  456. }
  457. static int pwc_s_input(struct file *file, void *fh, unsigned int i)
  458. {
  459. return i ? -EINVAL : 0;
  460. }
  461. static int pwc_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
  462. {
  463. struct pwc_device *pdev =
  464. container_of(ctrl->handler, struct pwc_device, ctrl_handler);
  465. int ret = 0;
  466. /*
  467. * Sometimes it can take quite long for the pwc to complete usb control
  468. * transfers, so release the modlock to give streaming by another
  469. * process / thread the chance to continue with a dqbuf.
  470. */
  471. mutex_unlock(&pdev->modlock);
  472. /*
  473. * Take the udev-lock to protect against the disconnect handler
  474. * completing and setting dev->udev to NULL underneath us. Other code
  475. * does not need to do this since it is protected by the modlock.
  476. */
  477. mutex_lock(&pdev->udevlock);
  478. if (!pdev->udev) {
  479. ret = -ENODEV;
  480. goto leave;
  481. }
  482. switch (ctrl->id) {
  483. case V4L2_CID_AUTO_WHITE_BALANCE:
  484. if (pdev->color_bal_valid && time_before(jiffies,
  485. pdev->last_color_bal_update + HZ / 4)) {
  486. pdev->red_balance->val = pdev->last_red_balance;
  487. pdev->blue_balance->val = pdev->last_blue_balance;
  488. break;
  489. }
  490. ret = pwc_get_u8_ctrl(pdev, GET_STATUS_CTL,
  491. READ_RED_GAIN_FORMATTER,
  492. &pdev->red_balance->val);
  493. if (ret)
  494. break;
  495. ret = pwc_get_u8_ctrl(pdev, GET_STATUS_CTL,
  496. READ_BLUE_GAIN_FORMATTER,
  497. &pdev->blue_balance->val);
  498. if (ret)
  499. break;
  500. pdev->last_red_balance = pdev->red_balance->val;
  501. pdev->last_blue_balance = pdev->blue_balance->val;
  502. pdev->last_color_bal_update = jiffies;
  503. pdev->color_bal_valid = true;
  504. break;
  505. case V4L2_CID_AUTOGAIN:
  506. if (pdev->gain_valid && time_before(jiffies,
  507. pdev->last_gain_update + HZ / 4)) {
  508. pdev->gain->val = pdev->last_gain;
  509. break;
  510. }
  511. ret = pwc_get_u8_ctrl(pdev, GET_STATUS_CTL,
  512. READ_AGC_FORMATTER, &pdev->gain->val);
  513. if (ret)
  514. break;
  515. pdev->last_gain = pdev->gain->val;
  516. pdev->last_gain_update = jiffies;
  517. pdev->gain_valid = true;
  518. if (!DEVICE_USE_CODEC3(pdev->type))
  519. break;
  520. /* Fall through for CODEC3 where autogain also controls expo */
  521. case V4L2_CID_EXPOSURE_AUTO:
  522. if (pdev->exposure_valid && time_before(jiffies,
  523. pdev->last_exposure_update + HZ / 4)) {
  524. pdev->exposure->val = pdev->last_exposure;
  525. break;
  526. }
  527. ret = pwc_get_u16_ctrl(pdev, GET_STATUS_CTL,
  528. READ_SHUTTER_FORMATTER,
  529. &pdev->exposure->val);
  530. if (ret)
  531. break;
  532. pdev->last_exposure = pdev->exposure->val;
  533. pdev->last_exposure_update = jiffies;
  534. pdev->exposure_valid = true;
  535. break;
  536. default:
  537. ret = -EINVAL;
  538. }
  539. if (ret)
  540. PWC_ERROR("g_ctrl %s error %d\n", ctrl->name, ret);
  541. leave:
  542. mutex_unlock(&pdev->udevlock);
  543. mutex_lock(&pdev->modlock);
  544. return ret;
  545. }
  546. static int pwc_set_awb(struct pwc_device *pdev)
  547. {
  548. int ret = 0;
  549. if (pdev->auto_white_balance->is_new) {
  550. ret = pwc_set_u8_ctrl(pdev, SET_CHROM_CTL,
  551. WB_MODE_FORMATTER,
  552. pdev->auto_white_balance->val);
  553. if (ret)
  554. return ret;
  555. /* Update val when coming from auto or going to a preset */
  556. if (pdev->red_balance->is_volatile ||
  557. pdev->auto_white_balance->val == awb_indoor ||
  558. pdev->auto_white_balance->val == awb_outdoor ||
  559. pdev->auto_white_balance->val == awb_fl) {
  560. if (!pdev->red_balance->is_new)
  561. pwc_get_u8_ctrl(pdev, GET_STATUS_CTL,
  562. READ_RED_GAIN_FORMATTER,
  563. &pdev->red_balance->val);
  564. if (!pdev->blue_balance->is_new)
  565. pwc_get_u8_ctrl(pdev, GET_STATUS_CTL,
  566. READ_BLUE_GAIN_FORMATTER,
  567. &pdev->blue_balance->val);
  568. }
  569. if (pdev->auto_white_balance->val == awb_auto) {
  570. pdev->red_balance->is_volatile = true;
  571. pdev->blue_balance->is_volatile = true;
  572. pdev->color_bal_valid = false; /* Force cache update */
  573. } else {
  574. pdev->red_balance->is_volatile = false;
  575. pdev->blue_balance->is_volatile = false;
  576. }
  577. }
  578. if (ret == 0 && pdev->red_balance->is_new) {
  579. if (pdev->auto_white_balance->val != awb_manual)
  580. return -EBUSY;
  581. ret = pwc_set_u8_ctrl(pdev, SET_CHROM_CTL,
  582. PRESET_MANUAL_RED_GAIN_FORMATTER,
  583. pdev->red_balance->val);
  584. }
  585. if (ret == 0 && pdev->blue_balance->is_new) {
  586. if (pdev->auto_white_balance->val != awb_manual)
  587. return -EBUSY;
  588. ret = pwc_set_u8_ctrl(pdev, SET_CHROM_CTL,
  589. PRESET_MANUAL_BLUE_GAIN_FORMATTER,
  590. pdev->blue_balance->val);
  591. }
  592. return ret;
  593. }
  594. /* For CODEC2 models which have separate autogain and auto exposure */
  595. static int pwc_set_autogain(struct pwc_device *pdev)
  596. {
  597. int ret = 0;
  598. if (pdev->autogain->is_new) {
  599. ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
  600. AGC_MODE_FORMATTER,
  601. pdev->autogain->val ? 0 : 0xff);
  602. if (ret)
  603. return ret;
  604. if (pdev->autogain->val)
  605. pdev->gain_valid = false; /* Force cache update */
  606. else if (!pdev->gain->is_new)
  607. pwc_get_u8_ctrl(pdev, GET_STATUS_CTL,
  608. READ_AGC_FORMATTER,
  609. &pdev->gain->val);
  610. }
  611. if (ret == 0 && pdev->gain->is_new) {
  612. if (pdev->autogain->val)
  613. return -EBUSY;
  614. ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
  615. PRESET_AGC_FORMATTER,
  616. pdev->gain->val);
  617. }
  618. return ret;
  619. }
  620. /* For CODEC2 models which have separate autogain and auto exposure */
  621. static int pwc_set_exposure_auto(struct pwc_device *pdev)
  622. {
  623. int ret = 0;
  624. int is_auto = pdev->exposure_auto->val == V4L2_EXPOSURE_AUTO;
  625. if (pdev->exposure_auto->is_new) {
  626. ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
  627. SHUTTER_MODE_FORMATTER,
  628. is_auto ? 0 : 0xff);
  629. if (ret)
  630. return ret;
  631. if (is_auto)
  632. pdev->exposure_valid = false; /* Force cache update */
  633. else if (!pdev->exposure->is_new)
  634. pwc_get_u16_ctrl(pdev, GET_STATUS_CTL,
  635. READ_SHUTTER_FORMATTER,
  636. &pdev->exposure->val);
  637. }
  638. if (ret == 0 && pdev->exposure->is_new) {
  639. if (is_auto)
  640. return -EBUSY;
  641. ret = pwc_set_u16_ctrl(pdev, SET_LUM_CTL,
  642. PRESET_SHUTTER_FORMATTER,
  643. pdev->exposure->val);
  644. }
  645. return ret;
  646. }
  647. /* For CODEC3 models which have autogain controlling both gain and exposure */
  648. static int pwc_set_autogain_expo(struct pwc_device *pdev)
  649. {
  650. int ret = 0;
  651. if (pdev->autogain->is_new) {
  652. ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
  653. AGC_MODE_FORMATTER,
  654. pdev->autogain->val ? 0 : 0xff);
  655. if (ret)
  656. return ret;
  657. if (pdev->autogain->val) {
  658. pdev->gain_valid = false; /* Force cache update */
  659. pdev->exposure_valid = false; /* Force cache update */
  660. } else {
  661. if (!pdev->gain->is_new)
  662. pwc_get_u8_ctrl(pdev, GET_STATUS_CTL,
  663. READ_AGC_FORMATTER,
  664. &pdev->gain->val);
  665. if (!pdev->exposure->is_new)
  666. pwc_get_u16_ctrl(pdev, GET_STATUS_CTL,
  667. READ_SHUTTER_FORMATTER,
  668. &pdev->exposure->val);
  669. }
  670. }
  671. if (ret == 0 && pdev->gain->is_new) {
  672. if (pdev->autogain->val)
  673. return -EBUSY;
  674. ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
  675. PRESET_AGC_FORMATTER,
  676. pdev->gain->val);
  677. }
  678. if (ret == 0 && pdev->exposure->is_new) {
  679. if (pdev->autogain->val)
  680. return -EBUSY;
  681. ret = pwc_set_u16_ctrl(pdev, SET_LUM_CTL,
  682. PRESET_SHUTTER_FORMATTER,
  683. pdev->exposure->val);
  684. }
  685. return ret;
  686. }
  687. static int pwc_set_motor(struct pwc_device *pdev)
  688. {
  689. int ret;
  690. u8 buf[4];
  691. buf[0] = 0;
  692. if (pdev->motor_pan_reset->is_new)
  693. buf[0] |= 0x01;
  694. if (pdev->motor_tilt_reset->is_new)
  695. buf[0] |= 0x02;
  696. if (pdev->motor_pan_reset->is_new || pdev->motor_tilt_reset->is_new) {
  697. ret = send_control_msg(pdev, SET_MPT_CTL,
  698. PT_RESET_CONTROL_FORMATTER, buf, 1);
  699. if (ret < 0)
  700. return ret;
  701. }
  702. memset(buf, 0, sizeof(buf));
  703. if (pdev->motor_pan->is_new) {
  704. buf[0] = pdev->motor_pan->val & 0xFF;
  705. buf[1] = (pdev->motor_pan->val >> 8);
  706. }
  707. if (pdev->motor_tilt->is_new) {
  708. buf[2] = pdev->motor_tilt->val & 0xFF;
  709. buf[3] = (pdev->motor_tilt->val >> 8);
  710. }
  711. if (pdev->motor_pan->is_new || pdev->motor_tilt->is_new) {
  712. ret = send_control_msg(pdev, SET_MPT_CTL,
  713. PT_RELATIVE_CONTROL_FORMATTER,
  714. buf, sizeof(buf));
  715. if (ret < 0)
  716. return ret;
  717. }
  718. return 0;
  719. }
  720. static int pwc_s_ctrl(struct v4l2_ctrl *ctrl)
  721. {
  722. struct pwc_device *pdev =
  723. container_of(ctrl->handler, struct pwc_device, ctrl_handler);
  724. int ret = 0;
  725. /* See the comments on locking in pwc_g_volatile_ctrl */
  726. mutex_unlock(&pdev->modlock);
  727. mutex_lock(&pdev->udevlock);
  728. if (!pdev->udev) {
  729. ret = -ENODEV;
  730. goto leave;
  731. }
  732. switch (ctrl->id) {
  733. case V4L2_CID_BRIGHTNESS:
  734. ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
  735. BRIGHTNESS_FORMATTER, ctrl->val);
  736. break;
  737. case V4L2_CID_CONTRAST:
  738. ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
  739. CONTRAST_FORMATTER, ctrl->val);
  740. break;
  741. case V4L2_CID_SATURATION:
  742. ret = pwc_set_s8_ctrl(pdev, SET_CHROM_CTL,
  743. pdev->saturation_fmt, ctrl->val);
  744. break;
  745. case V4L2_CID_GAMMA:
  746. ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
  747. GAMMA_FORMATTER, ctrl->val);
  748. break;
  749. case V4L2_CID_AUTO_WHITE_BALANCE:
  750. ret = pwc_set_awb(pdev);
  751. break;
  752. case V4L2_CID_AUTOGAIN:
  753. if (DEVICE_USE_CODEC2(pdev->type))
  754. ret = pwc_set_autogain(pdev);
  755. else if (DEVICE_USE_CODEC3(pdev->type))
  756. ret = pwc_set_autogain_expo(pdev);
  757. else
  758. ret = -EINVAL;
  759. break;
  760. case V4L2_CID_EXPOSURE_AUTO:
  761. if (DEVICE_USE_CODEC2(pdev->type))
  762. ret = pwc_set_exposure_auto(pdev);
  763. else
  764. ret = -EINVAL;
  765. break;
  766. case V4L2_CID_COLORFX:
  767. ret = pwc_set_u8_ctrl(pdev, SET_CHROM_CTL,
  768. COLOUR_MODE_FORMATTER,
  769. ctrl->val ? 0 : 0xff);
  770. break;
  771. case PWC_CID_CUSTOM(autocontour):
  772. if (pdev->autocontour->is_new) {
  773. ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
  774. AUTO_CONTOUR_FORMATTER,
  775. pdev->autocontour->val ? 0 : 0xff);
  776. }
  777. if (ret == 0 && pdev->contour->is_new) {
  778. if (pdev->autocontour->val) {
  779. ret = -EBUSY;
  780. break;
  781. }
  782. ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
  783. PRESET_CONTOUR_FORMATTER,
  784. pdev->contour->val);
  785. }
  786. break;
  787. case V4L2_CID_BACKLIGHT_COMPENSATION:
  788. ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
  789. BACK_LIGHT_COMPENSATION_FORMATTER,
  790. ctrl->val ? 0 : 0xff);
  791. break;
  792. case V4L2_CID_BAND_STOP_FILTER:
  793. ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
  794. FLICKERLESS_MODE_FORMATTER,
  795. ctrl->val ? 0 : 0xff);
  796. break;
  797. case PWC_CID_CUSTOM(noise_reduction):
  798. ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
  799. DYNAMIC_NOISE_CONTROL_FORMATTER,
  800. ctrl->val);
  801. break;
  802. case PWC_CID_CUSTOM(save_user):
  803. ret = pwc_button_ctrl(pdev, SAVE_USER_DEFAULTS_FORMATTER);
  804. break;
  805. case PWC_CID_CUSTOM(restore_user):
  806. ret = pwc_button_ctrl(pdev, RESTORE_USER_DEFAULTS_FORMATTER);
  807. break;
  808. case PWC_CID_CUSTOM(restore_factory):
  809. ret = pwc_button_ctrl(pdev,
  810. RESTORE_FACTORY_DEFAULTS_FORMATTER);
  811. break;
  812. case V4L2_CID_PAN_RELATIVE:
  813. ret = pwc_set_motor(pdev);
  814. break;
  815. default:
  816. ret = -EINVAL;
  817. }
  818. if (ret)
  819. PWC_ERROR("s_ctrl %s error %d\n", ctrl->name, ret);
  820. leave:
  821. mutex_unlock(&pdev->udevlock);
  822. mutex_lock(&pdev->modlock);
  823. return ret;
  824. }
  825. static int pwc_enum_fmt_vid_cap(struct file *file, void *fh, struct v4l2_fmtdesc *f)
  826. {
  827. struct pwc_device *pdev = video_drvdata(file);
  828. /* We only support two format: the raw format, and YUV */
  829. switch (f->index) {
  830. case 0:
  831. /* RAW format */
  832. f->pixelformat = pdev->type <= 646 ? V4L2_PIX_FMT_PWC1 : V4L2_PIX_FMT_PWC2;
  833. f->flags = V4L2_FMT_FLAG_COMPRESSED;
  834. strlcpy(f->description, "Raw Philips Webcam", sizeof(f->description));
  835. break;
  836. case 1:
  837. f->pixelformat = V4L2_PIX_FMT_YUV420;
  838. strlcpy(f->description, "4:2:0, planar, Y-Cb-Cr", sizeof(f->description));
  839. break;
  840. default:
  841. return -EINVAL;
  842. }
  843. return 0;
  844. }
  845. static int pwc_g_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f)
  846. {
  847. struct pwc_device *pdev = video_drvdata(file);
  848. PWC_DEBUG_IOCTL("ioctl(VIDIOC_G_FMT) return size %dx%d\n",
  849. pdev->image.x, pdev->image.y);
  850. pwc_vidioc_fill_fmt(pdev, f);
  851. return 0;
  852. }
  853. static int pwc_try_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f)
  854. {
  855. struct pwc_device *pdev = video_drvdata(file);
  856. return pwc_vidioc_try_fmt(pdev, f);
  857. }
  858. static int pwc_reqbufs(struct file *file, void *fh,
  859. struct v4l2_requestbuffers *rb)
  860. {
  861. struct pwc_device *pdev = video_drvdata(file);
  862. if (pdev->capt_file != NULL &&
  863. pdev->capt_file != file)
  864. return -EBUSY;
  865. pdev->capt_file = file;
  866. return vb2_reqbufs(&pdev->vb_queue, rb);
  867. }
  868. static int pwc_querybuf(struct file *file, void *fh, struct v4l2_buffer *buf)
  869. {
  870. struct pwc_device *pdev = video_drvdata(file);
  871. return vb2_querybuf(&pdev->vb_queue, buf);
  872. }
  873. static int pwc_qbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
  874. {
  875. struct pwc_device *pdev = video_drvdata(file);
  876. if (!pdev->udev)
  877. return -ENODEV;
  878. if (pdev->capt_file != file)
  879. return -EBUSY;
  880. return vb2_qbuf(&pdev->vb_queue, buf);
  881. }
  882. static int pwc_dqbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
  883. {
  884. struct pwc_device *pdev = video_drvdata(file);
  885. if (!pdev->udev)
  886. return -ENODEV;
  887. if (pdev->capt_file != file)
  888. return -EBUSY;
  889. return vb2_dqbuf(&pdev->vb_queue, buf, file->f_flags & O_NONBLOCK);
  890. }
  891. static int pwc_streamon(struct file *file, void *fh, enum v4l2_buf_type i)
  892. {
  893. struct pwc_device *pdev = video_drvdata(file);
  894. if (!pdev->udev)
  895. return -ENODEV;
  896. if (pdev->capt_file != file)
  897. return -EBUSY;
  898. return vb2_streamon(&pdev->vb_queue, i);
  899. }
  900. static int pwc_streamoff(struct file *file, void *fh, enum v4l2_buf_type i)
  901. {
  902. struct pwc_device *pdev = video_drvdata(file);
  903. if (!pdev->udev)
  904. return -ENODEV;
  905. if (pdev->capt_file != file)
  906. return -EBUSY;
  907. return vb2_streamoff(&pdev->vb_queue, i);
  908. }
  909. static int pwc_enum_framesizes(struct file *file, void *fh,
  910. struct v4l2_frmsizeenum *fsize)
  911. {
  912. struct pwc_device *pdev = video_drvdata(file);
  913. unsigned int i = 0, index = fsize->index;
  914. if (fsize->pixel_format == V4L2_PIX_FMT_YUV420) {
  915. for (i = 0; i < PSZ_MAX; i++) {
  916. if (pdev->image_mask & (1UL << i)) {
  917. if (!index--) {
  918. fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
  919. fsize->discrete.width = pwc_image_sizes[i].x;
  920. fsize->discrete.height = pwc_image_sizes[i].y;
  921. return 0;
  922. }
  923. }
  924. }
  925. } else if (fsize->index == 0 &&
  926. ((fsize->pixel_format == V4L2_PIX_FMT_PWC1 && DEVICE_USE_CODEC1(pdev->type)) ||
  927. (fsize->pixel_format == V4L2_PIX_FMT_PWC2 && DEVICE_USE_CODEC23(pdev->type)))) {
  928. fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
  929. fsize->discrete.width = pdev->abs_max.x;
  930. fsize->discrete.height = pdev->abs_max.y;
  931. return 0;
  932. }
  933. return -EINVAL;
  934. }
  935. static int pwc_enum_frameintervals(struct file *file, void *fh,
  936. struct v4l2_frmivalenum *fival)
  937. {
  938. struct pwc_device *pdev = video_drvdata(file);
  939. int size = -1;
  940. unsigned int i;
  941. for (i = 0; i < PSZ_MAX; i++) {
  942. if (pwc_image_sizes[i].x == fival->width &&
  943. pwc_image_sizes[i].y == fival->height) {
  944. size = i;
  945. break;
  946. }
  947. }
  948. /* TODO: Support raw format */
  949. if (size < 0 || fival->pixel_format != V4L2_PIX_FMT_YUV420)
  950. return -EINVAL;
  951. i = pwc_get_fps(pdev, fival->index, size);
  952. if (!i)
  953. return -EINVAL;
  954. fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
  955. fival->discrete.numerator = 1;
  956. fival->discrete.denominator = i;
  957. return 0;
  958. }
  959. static long pwc_default(struct file *file, void *fh, bool valid_prio,
  960. int cmd, void *arg)
  961. {
  962. struct pwc_device *pdev = video_drvdata(file);
  963. return pwc_ioctl(pdev, cmd, arg);
  964. }
  965. const struct v4l2_ioctl_ops pwc_ioctl_ops = {
  966. .vidioc_querycap = pwc_querycap,
  967. .vidioc_enum_input = pwc_enum_input,
  968. .vidioc_g_input = pwc_g_input,
  969. .vidioc_s_input = pwc_s_input,
  970. .vidioc_enum_fmt_vid_cap = pwc_enum_fmt_vid_cap,
  971. .vidioc_g_fmt_vid_cap = pwc_g_fmt_vid_cap,
  972. .vidioc_s_fmt_vid_cap = pwc_s_fmt_vid_cap,
  973. .vidioc_try_fmt_vid_cap = pwc_try_fmt_vid_cap,
  974. .vidioc_reqbufs = pwc_reqbufs,
  975. .vidioc_querybuf = pwc_querybuf,
  976. .vidioc_qbuf = pwc_qbuf,
  977. .vidioc_dqbuf = pwc_dqbuf,
  978. .vidioc_streamon = pwc_streamon,
  979. .vidioc_streamoff = pwc_streamoff,
  980. .vidioc_enum_framesizes = pwc_enum_framesizes,
  981. .vidioc_enum_frameintervals = pwc_enum_frameintervals,
  982. .vidioc_default = pwc_default,
  983. };
  984. /* vim: set cino= formatoptions=croql cindent shiftwidth=8 tabstop=8: */