pwc-v4l.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182
  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_awb_speed, custom_awb_delay,
  42. custom_save_user, custom_restore_user, custom_restore_factory };
  43. const char * const pwc_auto_whitebal_qmenu[] = {
  44. "Indoor (Incandescant Lighting) Mode",
  45. "Outdoor (Sunlight) Mode",
  46. "Indoor (Fluorescent Lighting) Mode",
  47. "Manual Mode",
  48. "Auto Mode",
  49. NULL
  50. };
  51. static const struct v4l2_ctrl_config pwc_auto_white_balance_cfg = {
  52. .ops = &pwc_ctrl_ops,
  53. .id = V4L2_CID_AUTO_WHITE_BALANCE,
  54. .type = V4L2_CTRL_TYPE_MENU,
  55. .max = awb_auto,
  56. .qmenu = pwc_auto_whitebal_qmenu,
  57. };
  58. static const struct v4l2_ctrl_config pwc_autocontour_cfg = {
  59. .ops = &pwc_ctrl_ops,
  60. .id = PWC_CID_CUSTOM(autocontour),
  61. .type = V4L2_CTRL_TYPE_BOOLEAN,
  62. .name = "Auto contour",
  63. .min = 0,
  64. .max = 1,
  65. .step = 1,
  66. };
  67. static const struct v4l2_ctrl_config pwc_contour_cfg = {
  68. .ops = &pwc_ctrl_ops,
  69. .id = PWC_CID_CUSTOM(contour),
  70. .type = V4L2_CTRL_TYPE_INTEGER,
  71. .name = "Contour",
  72. .flags = V4L2_CTRL_FLAG_SLIDER,
  73. .min = 0,
  74. .max = 63,
  75. .step = 1,
  76. };
  77. static const struct v4l2_ctrl_config pwc_backlight_cfg = {
  78. .ops = &pwc_ctrl_ops,
  79. .id = V4L2_CID_BACKLIGHT_COMPENSATION,
  80. .type = V4L2_CTRL_TYPE_BOOLEAN,
  81. .min = 0,
  82. .max = 1,
  83. .step = 1,
  84. };
  85. static const struct v4l2_ctrl_config pwc_flicker_cfg = {
  86. .ops = &pwc_ctrl_ops,
  87. .id = V4L2_CID_BAND_STOP_FILTER,
  88. .type = V4L2_CTRL_TYPE_BOOLEAN,
  89. .min = 0,
  90. .max = 1,
  91. .step = 1,
  92. };
  93. static const struct v4l2_ctrl_config pwc_noise_reduction_cfg = {
  94. .ops = &pwc_ctrl_ops,
  95. .id = PWC_CID_CUSTOM(noise_reduction),
  96. .type = V4L2_CTRL_TYPE_INTEGER,
  97. .name = "Dynamic Noise Reduction",
  98. .min = 0,
  99. .max = 3,
  100. .step = 1,
  101. };
  102. static const struct v4l2_ctrl_config pwc_save_user_cfg = {
  103. .ops = &pwc_ctrl_ops,
  104. .id = PWC_CID_CUSTOM(save_user),
  105. .type = V4L2_CTRL_TYPE_BUTTON,
  106. .name = "Save User Settings",
  107. };
  108. static const struct v4l2_ctrl_config pwc_restore_user_cfg = {
  109. .ops = &pwc_ctrl_ops,
  110. .id = PWC_CID_CUSTOM(restore_user),
  111. .type = V4L2_CTRL_TYPE_BUTTON,
  112. .name = "Restore User Settings",
  113. };
  114. static const struct v4l2_ctrl_config pwc_restore_factory_cfg = {
  115. .ops = &pwc_ctrl_ops,
  116. .id = PWC_CID_CUSTOM(restore_factory),
  117. .type = V4L2_CTRL_TYPE_BUTTON,
  118. .name = "Restore Factory Settings",
  119. };
  120. static const struct v4l2_ctrl_config pwc_awb_speed_cfg = {
  121. .ops = &pwc_ctrl_ops,
  122. .id = PWC_CID_CUSTOM(awb_speed),
  123. .type = V4L2_CTRL_TYPE_INTEGER,
  124. .name = "Auto White Balance Speed",
  125. .min = 1,
  126. .max = 32,
  127. .step = 1,
  128. };
  129. static const struct v4l2_ctrl_config pwc_awb_delay_cfg = {
  130. .ops = &pwc_ctrl_ops,
  131. .id = PWC_CID_CUSTOM(awb_delay),
  132. .type = V4L2_CTRL_TYPE_INTEGER,
  133. .name = "Auto White Balance Delay",
  134. .min = 0,
  135. .max = 63,
  136. .step = 1,
  137. };
  138. int pwc_init_controls(struct pwc_device *pdev)
  139. {
  140. struct v4l2_ctrl_handler *hdl;
  141. struct v4l2_ctrl_config cfg;
  142. int r, def;
  143. hdl = &pdev->ctrl_handler;
  144. r = v4l2_ctrl_handler_init(hdl, 20);
  145. if (r)
  146. return r;
  147. /* Brightness, contrast, saturation, gamma */
  148. r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, BRIGHTNESS_FORMATTER, &def);
  149. if (r || def > 127)
  150. def = 63;
  151. pdev->brightness = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
  152. V4L2_CID_BRIGHTNESS, 0, 127, 1, def);
  153. r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, CONTRAST_FORMATTER, &def);
  154. if (r || def > 63)
  155. def = 31;
  156. pdev->contrast = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
  157. V4L2_CID_CONTRAST, 0, 63, 1, def);
  158. if (pdev->type >= 675) {
  159. if (pdev->type < 730)
  160. pdev->saturation_fmt = SATURATION_MODE_FORMATTER2;
  161. else
  162. pdev->saturation_fmt = SATURATION_MODE_FORMATTER1;
  163. r = pwc_get_s8_ctrl(pdev, GET_CHROM_CTL, pdev->saturation_fmt,
  164. &def);
  165. if (r || def < -100 || def > 100)
  166. def = 0;
  167. pdev->saturation = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
  168. V4L2_CID_SATURATION, -100, 100, 1, def);
  169. }
  170. r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, GAMMA_FORMATTER, &def);
  171. if (r || def > 31)
  172. def = 15;
  173. pdev->gamma = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
  174. V4L2_CID_GAMMA, 0, 31, 1, def);
  175. /* auto white balance, red gain, blue gain */
  176. r = pwc_get_u8_ctrl(pdev, GET_CHROM_CTL, WB_MODE_FORMATTER, &def);
  177. if (r || def > awb_auto)
  178. def = awb_auto;
  179. cfg = pwc_auto_white_balance_cfg;
  180. cfg.name = v4l2_ctrl_get_name(cfg.id);
  181. cfg.def = def;
  182. pdev->auto_white_balance = v4l2_ctrl_new_custom(hdl, &cfg, NULL);
  183. /* check auto controls to avoid NULL deref in v4l2_ctrl_auto_cluster */
  184. if (!pdev->auto_white_balance)
  185. return hdl->error;
  186. r = pwc_get_u8_ctrl(pdev, GET_CHROM_CTL,
  187. PRESET_MANUAL_RED_GAIN_FORMATTER, &def);
  188. if (r)
  189. def = 127;
  190. pdev->red_balance = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
  191. V4L2_CID_RED_BALANCE, 0, 255, 1, def);
  192. r = pwc_get_u8_ctrl(pdev, GET_CHROM_CTL,
  193. PRESET_MANUAL_BLUE_GAIN_FORMATTER, &def);
  194. if (r)
  195. def = 127;
  196. pdev->blue_balance = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
  197. V4L2_CID_BLUE_BALANCE, 0, 255, 1, def);
  198. v4l2_ctrl_auto_cluster(3, &pdev->auto_white_balance, awb_manual, true);
  199. /* autogain, gain */
  200. r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, AGC_MODE_FORMATTER, &def);
  201. if (r || (def != 0 && def != 0xff))
  202. def = 0;
  203. /* Note a register value if 0 means auto gain is on */
  204. pdev->autogain = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
  205. V4L2_CID_AUTOGAIN, 0, 1, 1, def == 0);
  206. if (!pdev->autogain)
  207. return hdl->error;
  208. r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, PRESET_AGC_FORMATTER, &def);
  209. if (r || def > 63)
  210. def = 31;
  211. pdev->gain = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
  212. V4L2_CID_GAIN, 0, 63, 1, def);
  213. /* auto exposure, exposure */
  214. if (DEVICE_USE_CODEC2(pdev->type)) {
  215. r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, SHUTTER_MODE_FORMATTER,
  216. &def);
  217. if (r || (def != 0 && def != 0xff))
  218. def = 0;
  219. /*
  220. * def = 0 auto, def = ff manual
  221. * menu idx 0 = auto, idx 1 = manual
  222. */
  223. pdev->exposure_auto = v4l2_ctrl_new_std_menu(hdl,
  224. &pwc_ctrl_ops,
  225. V4L2_CID_EXPOSURE_AUTO,
  226. 1, 0, def != 0);
  227. if (!pdev->exposure_auto)
  228. return hdl->error;
  229. /* GET_LUM_CTL, PRESET_SHUTTER_FORMATTER is unreliable */
  230. r = pwc_get_u16_ctrl(pdev, GET_STATUS_CTL,
  231. READ_SHUTTER_FORMATTER, &def);
  232. if (r || def > 655)
  233. def = 655;
  234. pdev->exposure = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
  235. V4L2_CID_EXPOSURE, 0, 655, 1, def);
  236. /* CODEC2: separate auto gain & auto exposure */
  237. v4l2_ctrl_auto_cluster(2, &pdev->autogain, 0, true);
  238. v4l2_ctrl_auto_cluster(2, &pdev->exposure_auto,
  239. V4L2_EXPOSURE_MANUAL, true);
  240. } else if (DEVICE_USE_CODEC3(pdev->type)) {
  241. /* GET_LUM_CTL, PRESET_SHUTTER_FORMATTER is unreliable */
  242. r = pwc_get_u16_ctrl(pdev, GET_STATUS_CTL,
  243. READ_SHUTTER_FORMATTER, &def);
  244. if (r || def > 255)
  245. def = 255;
  246. pdev->exposure = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
  247. V4L2_CID_EXPOSURE, 0, 255, 1, def);
  248. /* CODEC3: both gain and exposure controlled by autogain */
  249. pdev->autogain_expo_cluster[0] = pdev->autogain;
  250. pdev->autogain_expo_cluster[1] = pdev->gain;
  251. pdev->autogain_expo_cluster[2] = pdev->exposure;
  252. v4l2_ctrl_auto_cluster(3, pdev->autogain_expo_cluster,
  253. 0, true);
  254. }
  255. /* color / bw setting */
  256. r = pwc_get_u8_ctrl(pdev, GET_CHROM_CTL, COLOUR_MODE_FORMATTER,
  257. &def);
  258. if (r || (def != 0 && def != 0xff))
  259. def = 0xff;
  260. /* def = 0 bw, def = ff color, menu idx 0 = color, idx 1 = bw */
  261. pdev->colorfx = v4l2_ctrl_new_std_menu(hdl, &pwc_ctrl_ops,
  262. V4L2_CID_COLORFX, 1, 0, def == 0);
  263. /* autocontour, contour */
  264. r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, AUTO_CONTOUR_FORMATTER, &def);
  265. if (r || (def != 0 && def != 0xff))
  266. def = 0;
  267. cfg = pwc_autocontour_cfg;
  268. cfg.def = def == 0;
  269. pdev->autocontour = v4l2_ctrl_new_custom(hdl, &cfg, NULL);
  270. if (!pdev->autocontour)
  271. return hdl->error;
  272. r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, PRESET_CONTOUR_FORMATTER, &def);
  273. if (r || def > 63)
  274. def = 31;
  275. cfg = pwc_contour_cfg;
  276. cfg.def = def;
  277. pdev->contour = v4l2_ctrl_new_custom(hdl, &cfg, NULL);
  278. v4l2_ctrl_auto_cluster(2, &pdev->autocontour, 0, false);
  279. /* backlight */
  280. r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL,
  281. BACK_LIGHT_COMPENSATION_FORMATTER, &def);
  282. if (r || (def != 0 && def != 0xff))
  283. def = 0;
  284. cfg = pwc_backlight_cfg;
  285. cfg.name = v4l2_ctrl_get_name(cfg.id);
  286. cfg.def = def == 0;
  287. pdev->backlight = v4l2_ctrl_new_custom(hdl, &cfg, NULL);
  288. /* flikker rediction */
  289. r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL,
  290. FLICKERLESS_MODE_FORMATTER, &def);
  291. if (r || (def != 0 && def != 0xff))
  292. def = 0;
  293. cfg = pwc_flicker_cfg;
  294. cfg.name = v4l2_ctrl_get_name(cfg.id);
  295. cfg.def = def == 0;
  296. pdev->flicker = v4l2_ctrl_new_custom(hdl, &cfg, NULL);
  297. /* Dynamic noise reduction */
  298. r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL,
  299. DYNAMIC_NOISE_CONTROL_FORMATTER, &def);
  300. if (r || def > 3)
  301. def = 2;
  302. cfg = pwc_noise_reduction_cfg;
  303. cfg.def = def;
  304. pdev->noise_reduction = v4l2_ctrl_new_custom(hdl, &cfg, NULL);
  305. /* Save / Restore User / Factory Settings */
  306. pdev->save_user = v4l2_ctrl_new_custom(hdl, &pwc_save_user_cfg, NULL);
  307. pdev->restore_user = v4l2_ctrl_new_custom(hdl, &pwc_restore_user_cfg,
  308. NULL);
  309. if (pdev->restore_user)
  310. pdev->restore_user->flags |= V4L2_CTRL_FLAG_UPDATE;
  311. pdev->restore_factory = v4l2_ctrl_new_custom(hdl,
  312. &pwc_restore_factory_cfg,
  313. NULL);
  314. if (pdev->restore_factory)
  315. pdev->restore_factory->flags |= V4L2_CTRL_FLAG_UPDATE;
  316. /* Auto White Balance speed & delay */
  317. r = pwc_get_u8_ctrl(pdev, GET_CHROM_CTL,
  318. AWB_CONTROL_SPEED_FORMATTER, &def);
  319. if (r || def < 1 || def > 32)
  320. def = 1;
  321. cfg = pwc_awb_speed_cfg;
  322. cfg.def = def;
  323. pdev->awb_speed = v4l2_ctrl_new_custom(hdl, &cfg, NULL);
  324. r = pwc_get_u8_ctrl(pdev, GET_CHROM_CTL,
  325. AWB_CONTROL_DELAY_FORMATTER, &def);
  326. if (r || def > 63)
  327. def = 0;
  328. cfg = pwc_awb_delay_cfg;
  329. cfg.def = def;
  330. pdev->awb_delay = v4l2_ctrl_new_custom(hdl, &cfg, NULL);
  331. if (!(pdev->features & FEATURE_MOTOR_PANTILT))
  332. return hdl->error;
  333. /* Motor pan / tilt / reset */
  334. pdev->motor_pan = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
  335. V4L2_CID_PAN_RELATIVE, -4480, 4480, 64, 0);
  336. if (!pdev->motor_pan)
  337. return hdl->error;
  338. pdev->motor_tilt = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
  339. V4L2_CID_TILT_RELATIVE, -1920, 1920, 64, 0);
  340. pdev->motor_pan_reset = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
  341. V4L2_CID_PAN_RESET, 0, 0, 0, 0);
  342. pdev->motor_tilt_reset = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
  343. V4L2_CID_TILT_RESET, 0, 0, 0, 0);
  344. v4l2_ctrl_cluster(4, &pdev->motor_pan);
  345. return hdl->error;
  346. }
  347. static void pwc_vidioc_fill_fmt(struct v4l2_format *f,
  348. int width, int height, u32 pixfmt)
  349. {
  350. memset(&f->fmt.pix, 0, sizeof(struct v4l2_pix_format));
  351. f->fmt.pix.width = width;
  352. f->fmt.pix.height = height;
  353. f->fmt.pix.field = V4L2_FIELD_NONE;
  354. f->fmt.pix.pixelformat = pixfmt;
  355. f->fmt.pix.bytesperline = f->fmt.pix.width;
  356. f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.width * 3 / 2;
  357. PWC_DEBUG_IOCTL("pwc_vidioc_fill_fmt() "
  358. "width=%d, height=%d, bytesperline=%d, sizeimage=%d, pixelformat=%c%c%c%c\n",
  359. f->fmt.pix.width,
  360. f->fmt.pix.height,
  361. f->fmt.pix.bytesperline,
  362. f->fmt.pix.sizeimage,
  363. (f->fmt.pix.pixelformat)&255,
  364. (f->fmt.pix.pixelformat>>8)&255,
  365. (f->fmt.pix.pixelformat>>16)&255,
  366. (f->fmt.pix.pixelformat>>24)&255);
  367. }
  368. /* ioctl(VIDIOC_TRY_FMT) */
  369. static int pwc_vidioc_try_fmt(struct pwc_device *pdev, struct v4l2_format *f)
  370. {
  371. int size;
  372. if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  373. PWC_DEBUG_IOCTL("Bad video type must be V4L2_BUF_TYPE_VIDEO_CAPTURE\n");
  374. return -EINVAL;
  375. }
  376. switch (f->fmt.pix.pixelformat) {
  377. case V4L2_PIX_FMT_YUV420:
  378. break;
  379. case V4L2_PIX_FMT_PWC1:
  380. if (DEVICE_USE_CODEC23(pdev->type)) {
  381. PWC_DEBUG_IOCTL("codec1 is only supported for old pwc webcam\n");
  382. return -EINVAL;
  383. }
  384. break;
  385. case V4L2_PIX_FMT_PWC2:
  386. if (DEVICE_USE_CODEC1(pdev->type)) {
  387. PWC_DEBUG_IOCTL("codec23 is only supported for new pwc webcam\n");
  388. return -EINVAL;
  389. }
  390. break;
  391. default:
  392. PWC_DEBUG_IOCTL("Unsupported pixel format\n");
  393. return -EINVAL;
  394. }
  395. size = pwc_get_size(pdev, f->fmt.pix.width, f->fmt.pix.height);
  396. pwc_vidioc_fill_fmt(f,
  397. pwc_image_sizes[size][0],
  398. pwc_image_sizes[size][1],
  399. f->fmt.pix.pixelformat);
  400. return 0;
  401. }
  402. /* ioctl(VIDIOC_SET_FMT) */
  403. static int pwc_s_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f)
  404. {
  405. struct pwc_device *pdev = video_drvdata(file);
  406. int ret, pixelformat, compression = 0;
  407. if (pwc_test_n_set_capt_file(pdev, file))
  408. return -EBUSY;
  409. ret = pwc_vidioc_try_fmt(pdev, f);
  410. if (ret < 0)
  411. return ret;
  412. pixelformat = f->fmt.pix.pixelformat;
  413. mutex_lock(&pdev->udevlock);
  414. if (!pdev->udev) {
  415. ret = -ENODEV;
  416. goto leave;
  417. }
  418. if (pdev->iso_init) {
  419. ret = -EBUSY;
  420. goto leave;
  421. }
  422. PWC_DEBUG_IOCTL("Trying to set format to: width=%d height=%d fps=%d "
  423. "format=%c%c%c%c\n",
  424. f->fmt.pix.width, f->fmt.pix.height, pdev->vframes,
  425. (pixelformat)&255,
  426. (pixelformat>>8)&255,
  427. (pixelformat>>16)&255,
  428. (pixelformat>>24)&255);
  429. ret = pwc_set_video_mode(pdev, f->fmt.pix.width, f->fmt.pix.height,
  430. 30, &compression);
  431. PWC_DEBUG_IOCTL("pwc_set_video_mode(), return=%d\n", ret);
  432. if (ret == 0) {
  433. pdev->pixfmt = pixelformat;
  434. pwc_vidioc_fill_fmt(f, pdev->width, pdev->height,
  435. pdev->pixfmt);
  436. }
  437. leave:
  438. mutex_unlock(&pdev->udevlock);
  439. return ret;
  440. }
  441. static int pwc_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
  442. {
  443. struct pwc_device *pdev = video_drvdata(file);
  444. if (!pdev->udev)
  445. return -ENODEV;
  446. strcpy(cap->driver, PWC_NAME);
  447. strlcpy(cap->card, pdev->vdev.name, sizeof(cap->card));
  448. usb_make_path(pdev->udev, cap->bus_info, sizeof(cap->bus_info));
  449. cap->capabilities =
  450. V4L2_CAP_VIDEO_CAPTURE |
  451. V4L2_CAP_STREAMING |
  452. V4L2_CAP_READWRITE;
  453. return 0;
  454. }
  455. static int pwc_enum_input(struct file *file, void *fh, struct v4l2_input *i)
  456. {
  457. if (i->index) /* Only one INPUT is supported */
  458. return -EINVAL;
  459. strcpy(i->name, "usb");
  460. return 0;
  461. }
  462. static int pwc_g_input(struct file *file, void *fh, unsigned int *i)
  463. {
  464. *i = 0;
  465. return 0;
  466. }
  467. static int pwc_s_input(struct file *file, void *fh, unsigned int i)
  468. {
  469. return i ? -EINVAL : 0;
  470. }
  471. static int pwc_g_volatile_ctrl_unlocked(struct v4l2_ctrl *ctrl)
  472. {
  473. struct pwc_device *pdev =
  474. container_of(ctrl->handler, struct pwc_device, ctrl_handler);
  475. int ret = 0;
  476. if (!pdev->udev)
  477. return -ENODEV;
  478. switch (ctrl->id) {
  479. case V4L2_CID_AUTO_WHITE_BALANCE:
  480. if (pdev->color_bal_valid &&
  481. (pdev->auto_white_balance->val != awb_auto ||
  482. time_before(jiffies,
  483. pdev->last_color_bal_update + HZ / 4))) {
  484. pdev->red_balance->val = pdev->last_red_balance;
  485. pdev->blue_balance->val = pdev->last_blue_balance;
  486. break;
  487. }
  488. ret = pwc_get_u8_ctrl(pdev, GET_STATUS_CTL,
  489. READ_RED_GAIN_FORMATTER,
  490. &pdev->red_balance->val);
  491. if (ret)
  492. break;
  493. ret = pwc_get_u8_ctrl(pdev, GET_STATUS_CTL,
  494. READ_BLUE_GAIN_FORMATTER,
  495. &pdev->blue_balance->val);
  496. if (ret)
  497. break;
  498. pdev->last_red_balance = pdev->red_balance->val;
  499. pdev->last_blue_balance = pdev->blue_balance->val;
  500. pdev->last_color_bal_update = jiffies;
  501. pdev->color_bal_valid = true;
  502. break;
  503. case V4L2_CID_AUTOGAIN:
  504. if (pdev->gain_valid && time_before(jiffies,
  505. pdev->last_gain_update + HZ / 4)) {
  506. pdev->gain->val = pdev->last_gain;
  507. break;
  508. }
  509. ret = pwc_get_u8_ctrl(pdev, GET_STATUS_CTL,
  510. READ_AGC_FORMATTER, &pdev->gain->val);
  511. if (ret)
  512. break;
  513. pdev->last_gain = pdev->gain->val;
  514. pdev->last_gain_update = jiffies;
  515. pdev->gain_valid = true;
  516. if (!DEVICE_USE_CODEC3(pdev->type))
  517. break;
  518. /* Fall through for CODEC3 where autogain also controls expo */
  519. case V4L2_CID_EXPOSURE_AUTO:
  520. if (pdev->exposure_valid && time_before(jiffies,
  521. pdev->last_exposure_update + HZ / 4)) {
  522. pdev->exposure->val = pdev->last_exposure;
  523. break;
  524. }
  525. ret = pwc_get_u16_ctrl(pdev, GET_STATUS_CTL,
  526. READ_SHUTTER_FORMATTER,
  527. &pdev->exposure->val);
  528. if (ret)
  529. break;
  530. pdev->last_exposure = pdev->exposure->val;
  531. pdev->last_exposure_update = jiffies;
  532. pdev->exposure_valid = true;
  533. break;
  534. default:
  535. ret = -EINVAL;
  536. }
  537. if (ret)
  538. PWC_ERROR("g_ctrl %s error %d\n", ctrl->name, ret);
  539. return ret;
  540. }
  541. static int pwc_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
  542. {
  543. struct pwc_device *pdev =
  544. container_of(ctrl->handler, struct pwc_device, ctrl_handler);
  545. int ret;
  546. mutex_lock(&pdev->udevlock);
  547. ret = pwc_g_volatile_ctrl_unlocked(ctrl);
  548. mutex_unlock(&pdev->udevlock);
  549. return ret;
  550. }
  551. static int pwc_set_awb(struct pwc_device *pdev)
  552. {
  553. int ret;
  554. if (pdev->auto_white_balance->is_new) {
  555. ret = pwc_set_u8_ctrl(pdev, SET_CHROM_CTL,
  556. WB_MODE_FORMATTER,
  557. pdev->auto_white_balance->val);
  558. if (ret)
  559. return ret;
  560. if (pdev->auto_white_balance->val != awb_manual)
  561. pdev->color_bal_valid = false; /* Force cache update */
  562. /*
  563. * If this is a preset, update our red / blue balance values
  564. * so that events get generated for the new preset values
  565. */
  566. if (pdev->auto_white_balance->val == awb_indoor ||
  567. pdev->auto_white_balance->val == awb_outdoor ||
  568. pdev->auto_white_balance->val == awb_fl)
  569. pwc_g_volatile_ctrl_unlocked(pdev->auto_white_balance);
  570. }
  571. if (pdev->auto_white_balance->val != awb_manual)
  572. return 0;
  573. if (pdev->red_balance->is_new) {
  574. ret = pwc_set_u8_ctrl(pdev, SET_CHROM_CTL,
  575. PRESET_MANUAL_RED_GAIN_FORMATTER,
  576. pdev->red_balance->val);
  577. if (ret)
  578. return ret;
  579. }
  580. if (pdev->blue_balance->is_new) {
  581. ret = pwc_set_u8_ctrl(pdev, SET_CHROM_CTL,
  582. PRESET_MANUAL_BLUE_GAIN_FORMATTER,
  583. pdev->blue_balance->val);
  584. if (ret)
  585. return ret;
  586. }
  587. return 0;
  588. }
  589. /* For CODEC2 models which have separate autogain and auto exposure */
  590. static int pwc_set_autogain(struct pwc_device *pdev)
  591. {
  592. int ret;
  593. if (pdev->autogain->is_new) {
  594. ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
  595. AGC_MODE_FORMATTER,
  596. pdev->autogain->val ? 0 : 0xff);
  597. if (ret)
  598. return ret;
  599. if (pdev->autogain->val)
  600. pdev->gain_valid = false; /* Force cache update */
  601. }
  602. if (pdev->autogain->val)
  603. return 0;
  604. if (pdev->gain->is_new) {
  605. ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
  606. PRESET_AGC_FORMATTER,
  607. pdev->gain->val);
  608. if (ret)
  609. return ret;
  610. }
  611. return 0;
  612. }
  613. /* For CODEC2 models which have separate autogain and auto exposure */
  614. static int pwc_set_exposure_auto(struct pwc_device *pdev)
  615. {
  616. int ret;
  617. int is_auto = pdev->exposure_auto->val == V4L2_EXPOSURE_AUTO;
  618. if (pdev->exposure_auto->is_new) {
  619. ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
  620. SHUTTER_MODE_FORMATTER,
  621. is_auto ? 0 : 0xff);
  622. if (ret)
  623. return ret;
  624. if (is_auto)
  625. pdev->exposure_valid = false; /* Force cache update */
  626. }
  627. if (is_auto)
  628. return 0;
  629. if (pdev->exposure->is_new) {
  630. ret = pwc_set_u16_ctrl(pdev, SET_LUM_CTL,
  631. PRESET_SHUTTER_FORMATTER,
  632. pdev->exposure->val);
  633. if (ret)
  634. return ret;
  635. }
  636. return 0;
  637. }
  638. /* For CODEC3 models which have autogain controlling both gain and exposure */
  639. static int pwc_set_autogain_expo(struct pwc_device *pdev)
  640. {
  641. int ret;
  642. if (pdev->autogain->is_new) {
  643. ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
  644. AGC_MODE_FORMATTER,
  645. pdev->autogain->val ? 0 : 0xff);
  646. if (ret)
  647. return ret;
  648. if (pdev->autogain->val) {
  649. pdev->gain_valid = false; /* Force cache update */
  650. pdev->exposure_valid = false; /* Force cache update */
  651. }
  652. }
  653. if (pdev->autogain->val)
  654. return 0;
  655. if (pdev->gain->is_new) {
  656. ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
  657. PRESET_AGC_FORMATTER,
  658. pdev->gain->val);
  659. if (ret)
  660. return ret;
  661. }
  662. if (pdev->exposure->is_new) {
  663. ret = pwc_set_u16_ctrl(pdev, SET_LUM_CTL,
  664. PRESET_SHUTTER_FORMATTER,
  665. pdev->exposure->val);
  666. if (ret)
  667. return ret;
  668. }
  669. return 0;
  670. }
  671. static int pwc_set_motor(struct pwc_device *pdev)
  672. {
  673. int ret;
  674. u8 buf[4];
  675. buf[0] = 0;
  676. if (pdev->motor_pan_reset->is_new)
  677. buf[0] |= 0x01;
  678. if (pdev->motor_tilt_reset->is_new)
  679. buf[0] |= 0x02;
  680. if (pdev->motor_pan_reset->is_new || pdev->motor_tilt_reset->is_new) {
  681. ret = send_control_msg(pdev, SET_MPT_CTL,
  682. PT_RESET_CONTROL_FORMATTER, buf, 1);
  683. if (ret < 0)
  684. return ret;
  685. }
  686. memset(buf, 0, sizeof(buf));
  687. if (pdev->motor_pan->is_new) {
  688. buf[0] = pdev->motor_pan->val & 0xFF;
  689. buf[1] = (pdev->motor_pan->val >> 8);
  690. }
  691. if (pdev->motor_tilt->is_new) {
  692. buf[2] = pdev->motor_tilt->val & 0xFF;
  693. buf[3] = (pdev->motor_tilt->val >> 8);
  694. }
  695. if (pdev->motor_pan->is_new || pdev->motor_tilt->is_new) {
  696. ret = send_control_msg(pdev, SET_MPT_CTL,
  697. PT_RELATIVE_CONTROL_FORMATTER,
  698. buf, sizeof(buf));
  699. if (ret < 0)
  700. return ret;
  701. }
  702. return 0;
  703. }
  704. static int pwc_s_ctrl(struct v4l2_ctrl *ctrl)
  705. {
  706. struct pwc_device *pdev =
  707. container_of(ctrl->handler, struct pwc_device, ctrl_handler);
  708. int ret = 0;
  709. mutex_lock(&pdev->udevlock);
  710. if (!pdev->udev) {
  711. ret = -ENODEV;
  712. goto leave;
  713. }
  714. switch (ctrl->id) {
  715. case V4L2_CID_BRIGHTNESS:
  716. ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
  717. BRIGHTNESS_FORMATTER, ctrl->val);
  718. break;
  719. case V4L2_CID_CONTRAST:
  720. ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
  721. CONTRAST_FORMATTER, ctrl->val);
  722. break;
  723. case V4L2_CID_SATURATION:
  724. ret = pwc_set_s8_ctrl(pdev, SET_CHROM_CTL,
  725. pdev->saturation_fmt, ctrl->val);
  726. break;
  727. case V4L2_CID_GAMMA:
  728. ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
  729. GAMMA_FORMATTER, ctrl->val);
  730. break;
  731. case V4L2_CID_AUTO_WHITE_BALANCE:
  732. ret = pwc_set_awb(pdev);
  733. break;
  734. case V4L2_CID_AUTOGAIN:
  735. if (DEVICE_USE_CODEC2(pdev->type))
  736. ret = pwc_set_autogain(pdev);
  737. else if (DEVICE_USE_CODEC3(pdev->type))
  738. ret = pwc_set_autogain_expo(pdev);
  739. else
  740. ret = -EINVAL;
  741. break;
  742. case V4L2_CID_EXPOSURE_AUTO:
  743. if (DEVICE_USE_CODEC2(pdev->type))
  744. ret = pwc_set_exposure_auto(pdev);
  745. else
  746. ret = -EINVAL;
  747. break;
  748. case V4L2_CID_COLORFX:
  749. ret = pwc_set_u8_ctrl(pdev, SET_CHROM_CTL,
  750. COLOUR_MODE_FORMATTER,
  751. ctrl->val ? 0 : 0xff);
  752. break;
  753. case PWC_CID_CUSTOM(autocontour):
  754. if (pdev->autocontour->is_new) {
  755. ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
  756. AUTO_CONTOUR_FORMATTER,
  757. pdev->autocontour->val ? 0 : 0xff);
  758. }
  759. if (ret == 0 && pdev->contour->is_new) {
  760. ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
  761. PRESET_CONTOUR_FORMATTER,
  762. pdev->contour->val);
  763. }
  764. break;
  765. case V4L2_CID_BACKLIGHT_COMPENSATION:
  766. ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
  767. BACK_LIGHT_COMPENSATION_FORMATTER,
  768. ctrl->val ? 0 : 0xff);
  769. break;
  770. case V4L2_CID_BAND_STOP_FILTER:
  771. ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
  772. FLICKERLESS_MODE_FORMATTER,
  773. ctrl->val ? 0 : 0xff);
  774. break;
  775. case PWC_CID_CUSTOM(noise_reduction):
  776. ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
  777. DYNAMIC_NOISE_CONTROL_FORMATTER,
  778. ctrl->val);
  779. break;
  780. case PWC_CID_CUSTOM(save_user):
  781. ret = pwc_button_ctrl(pdev, SAVE_USER_DEFAULTS_FORMATTER);
  782. break;
  783. case PWC_CID_CUSTOM(restore_user):
  784. ret = pwc_button_ctrl(pdev, RESTORE_USER_DEFAULTS_FORMATTER);
  785. break;
  786. case PWC_CID_CUSTOM(restore_factory):
  787. ret = pwc_button_ctrl(pdev,
  788. RESTORE_FACTORY_DEFAULTS_FORMATTER);
  789. break;
  790. case PWC_CID_CUSTOM(awb_speed):
  791. ret = pwc_set_u8_ctrl(pdev, SET_CHROM_CTL,
  792. AWB_CONTROL_SPEED_FORMATTER,
  793. ctrl->val);
  794. break;
  795. case PWC_CID_CUSTOM(awb_delay):
  796. ret = pwc_set_u8_ctrl(pdev, SET_CHROM_CTL,
  797. AWB_CONTROL_DELAY_FORMATTER,
  798. ctrl->val);
  799. break;
  800. case V4L2_CID_PAN_RELATIVE:
  801. ret = pwc_set_motor(pdev);
  802. break;
  803. default:
  804. ret = -EINVAL;
  805. }
  806. if (ret)
  807. PWC_ERROR("s_ctrl %s error %d\n", ctrl->name, ret);
  808. leave:
  809. mutex_unlock(&pdev->udevlock);
  810. return ret;
  811. }
  812. static int pwc_enum_fmt_vid_cap(struct file *file, void *fh, struct v4l2_fmtdesc *f)
  813. {
  814. struct pwc_device *pdev = video_drvdata(file);
  815. /* We only support two format: the raw format, and YUV */
  816. switch (f->index) {
  817. case 0:
  818. /* RAW format */
  819. f->pixelformat = pdev->type <= 646 ? V4L2_PIX_FMT_PWC1 : V4L2_PIX_FMT_PWC2;
  820. f->flags = V4L2_FMT_FLAG_COMPRESSED;
  821. strlcpy(f->description, "Raw Philips Webcam", sizeof(f->description));
  822. break;
  823. case 1:
  824. f->pixelformat = V4L2_PIX_FMT_YUV420;
  825. strlcpy(f->description, "4:2:0, planar, Y-Cb-Cr", sizeof(f->description));
  826. break;
  827. default:
  828. return -EINVAL;
  829. }
  830. return 0;
  831. }
  832. static int pwc_g_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f)
  833. {
  834. struct pwc_device *pdev = video_drvdata(file);
  835. if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  836. return -EINVAL;
  837. mutex_lock(&pdev->udevlock); /* To avoid race with s_fmt */
  838. PWC_DEBUG_IOCTL("ioctl(VIDIOC_G_FMT) return size %dx%d\n",
  839. pdev->width, pdev->height);
  840. pwc_vidioc_fill_fmt(f, pdev->width, pdev->height, pdev->pixfmt);
  841. mutex_unlock(&pdev->udevlock);
  842. return 0;
  843. }
  844. static int pwc_try_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f)
  845. {
  846. struct pwc_device *pdev = video_drvdata(file);
  847. return pwc_vidioc_try_fmt(pdev, f);
  848. }
  849. static int pwc_reqbufs(struct file *file, void *fh,
  850. struct v4l2_requestbuffers *rb)
  851. {
  852. struct pwc_device *pdev = video_drvdata(file);
  853. if (pwc_test_n_set_capt_file(pdev, file))
  854. return -EBUSY;
  855. return vb2_reqbufs(&pdev->vb_queue, rb);
  856. }
  857. static int pwc_querybuf(struct file *file, void *fh, struct v4l2_buffer *buf)
  858. {
  859. struct pwc_device *pdev = video_drvdata(file);
  860. return vb2_querybuf(&pdev->vb_queue, buf);
  861. }
  862. static int pwc_qbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
  863. {
  864. struct pwc_device *pdev = video_drvdata(file);
  865. if (!pdev->udev)
  866. return -ENODEV;
  867. if (pdev->capt_file != file)
  868. return -EBUSY;
  869. return vb2_qbuf(&pdev->vb_queue, buf);
  870. }
  871. static int pwc_dqbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
  872. {
  873. struct pwc_device *pdev = video_drvdata(file);
  874. if (!pdev->udev)
  875. return -ENODEV;
  876. if (pdev->capt_file != file)
  877. return -EBUSY;
  878. return vb2_dqbuf(&pdev->vb_queue, buf, file->f_flags & O_NONBLOCK);
  879. }
  880. static int pwc_streamon(struct file *file, void *fh, enum v4l2_buf_type i)
  881. {
  882. struct pwc_device *pdev = video_drvdata(file);
  883. if (!pdev->udev)
  884. return -ENODEV;
  885. if (pdev->capt_file != file)
  886. return -EBUSY;
  887. return vb2_streamon(&pdev->vb_queue, i);
  888. }
  889. static int pwc_streamoff(struct file *file, void *fh, enum v4l2_buf_type i)
  890. {
  891. struct pwc_device *pdev = video_drvdata(file);
  892. if (!pdev->udev)
  893. return -ENODEV;
  894. if (pdev->capt_file != file)
  895. return -EBUSY;
  896. return vb2_streamoff(&pdev->vb_queue, i);
  897. }
  898. static int pwc_enum_framesizes(struct file *file, void *fh,
  899. struct v4l2_frmsizeenum *fsize)
  900. {
  901. struct pwc_device *pdev = video_drvdata(file);
  902. unsigned int i = 0, index = fsize->index;
  903. if (fsize->pixel_format == V4L2_PIX_FMT_YUV420 ||
  904. (fsize->pixel_format == V4L2_PIX_FMT_PWC1 &&
  905. DEVICE_USE_CODEC1(pdev->type)) ||
  906. (fsize->pixel_format == V4L2_PIX_FMT_PWC2 &&
  907. DEVICE_USE_CODEC23(pdev->type))) {
  908. for (i = 0; i < PSZ_MAX; i++) {
  909. if (!(pdev->image_mask & (1UL << i)))
  910. continue;
  911. if (!index--) {
  912. fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
  913. fsize->discrete.width = pwc_image_sizes[i][0];
  914. fsize->discrete.height = pwc_image_sizes[i][1];
  915. return 0;
  916. }
  917. }
  918. }
  919. return -EINVAL;
  920. }
  921. static int pwc_enum_frameintervals(struct file *file, void *fh,
  922. struct v4l2_frmivalenum *fival)
  923. {
  924. struct pwc_device *pdev = video_drvdata(file);
  925. int size = -1;
  926. unsigned int i;
  927. for (i = 0; i < PSZ_MAX; i++) {
  928. if (pwc_image_sizes[i][0] == fival->width &&
  929. pwc_image_sizes[i][1] == fival->height) {
  930. size = i;
  931. break;
  932. }
  933. }
  934. /* TODO: Support raw format */
  935. if (size < 0 || fival->pixel_format != V4L2_PIX_FMT_YUV420)
  936. return -EINVAL;
  937. i = pwc_get_fps(pdev, fival->index, size);
  938. if (!i)
  939. return -EINVAL;
  940. fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
  941. fival->discrete.numerator = 1;
  942. fival->discrete.denominator = i;
  943. return 0;
  944. }
  945. static int pwc_g_parm(struct file *file, void *fh,
  946. struct v4l2_streamparm *parm)
  947. {
  948. struct pwc_device *pdev = video_drvdata(file);
  949. if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  950. return -EINVAL;
  951. memset(parm, 0, sizeof(*parm));
  952. parm->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  953. parm->parm.capture.readbuffers = MIN_FRAMES;
  954. parm->parm.capture.capability |= V4L2_CAP_TIMEPERFRAME;
  955. parm->parm.capture.timeperframe.denominator = pdev->vframes;
  956. parm->parm.capture.timeperframe.numerator = 1;
  957. return 0;
  958. }
  959. static int pwc_s_parm(struct file *file, void *fh,
  960. struct v4l2_streamparm *parm)
  961. {
  962. struct pwc_device *pdev = video_drvdata(file);
  963. int compression = 0;
  964. int ret, fps;
  965. if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  966. parm->parm.capture.timeperframe.numerator == 0)
  967. return -EINVAL;
  968. if (pwc_test_n_set_capt_file(pdev, file))
  969. return -EBUSY;
  970. fps = parm->parm.capture.timeperframe.denominator /
  971. parm->parm.capture.timeperframe.numerator;
  972. mutex_lock(&pdev->udevlock);
  973. if (!pdev->udev) {
  974. ret = -ENODEV;
  975. goto leave;
  976. }
  977. if (pdev->iso_init) {
  978. ret = -EBUSY;
  979. goto leave;
  980. }
  981. ret = pwc_set_video_mode(pdev, pdev->width, pdev->height, fps,
  982. &compression);
  983. pwc_g_parm(file, fh, parm);
  984. leave:
  985. mutex_unlock(&pdev->udevlock);
  986. return ret;
  987. }
  988. static int pwc_log_status(struct file *file, void *priv)
  989. {
  990. struct pwc_device *pdev = video_drvdata(file);
  991. v4l2_ctrl_handler_log_status(&pdev->ctrl_handler, PWC_NAME);
  992. return 0;
  993. }
  994. const struct v4l2_ioctl_ops pwc_ioctl_ops = {
  995. .vidioc_querycap = pwc_querycap,
  996. .vidioc_enum_input = pwc_enum_input,
  997. .vidioc_g_input = pwc_g_input,
  998. .vidioc_s_input = pwc_s_input,
  999. .vidioc_enum_fmt_vid_cap = pwc_enum_fmt_vid_cap,
  1000. .vidioc_g_fmt_vid_cap = pwc_g_fmt_vid_cap,
  1001. .vidioc_s_fmt_vid_cap = pwc_s_fmt_vid_cap,
  1002. .vidioc_try_fmt_vid_cap = pwc_try_fmt_vid_cap,
  1003. .vidioc_reqbufs = pwc_reqbufs,
  1004. .vidioc_querybuf = pwc_querybuf,
  1005. .vidioc_qbuf = pwc_qbuf,
  1006. .vidioc_dqbuf = pwc_dqbuf,
  1007. .vidioc_streamon = pwc_streamon,
  1008. .vidioc_streamoff = pwc_streamoff,
  1009. .vidioc_log_status = pwc_log_status,
  1010. .vidioc_enum_framesizes = pwc_enum_framesizes,
  1011. .vidioc_enum_frameintervals = pwc_enum_frameintervals,
  1012. .vidioc_g_parm = pwc_g_parm,
  1013. .vidioc_s_parm = pwc_s_parm,
  1014. };