pwc-v4l.c 31 KB

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