ov534.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340
  1. /*
  2. * ov534-ov772x gspca driver
  3. *
  4. * Copyright (C) 2008 Antonio Ospite <ospite@studenti.unina.it>
  5. * Copyright (C) 2008 Jim Paris <jim@jtan.com>
  6. * Copyright (C) 2009 Jean-Francois Moine http://moinejf.free.fr
  7. *
  8. * Based on a prototype written by Mark Ferrell <majortrips@gmail.com>
  9. * USB protocol reverse engineered by Jim Paris <jim@jtan.com>
  10. * https://jim.sh/svn/jim/devl/playstation/ps3/eye/test/
  11. *
  12. * PS3 Eye camera enhanced by Richard Kaswy http://kaswy.free.fr
  13. * PS3 Eye camera - brightness, contrast, awb, agc, aec controls
  14. * added by Max Thrun <bear24rw@gmail.com>
  15. *
  16. * This program is free software; you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License as published by
  18. * the Free Software Foundation; either version 2 of the License, or
  19. * any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program; if not, write to the Free Software
  28. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  29. */
  30. #define MODULE_NAME "ov534"
  31. #include "gspca.h"
  32. #define OV534_REG_ADDRESS 0xf1 /* sensor address */
  33. #define OV534_REG_SUBADDR 0xf2
  34. #define OV534_REG_WRITE 0xf3
  35. #define OV534_REG_READ 0xf4
  36. #define OV534_REG_OPERATION 0xf5
  37. #define OV534_REG_STATUS 0xf6
  38. #define OV534_OP_WRITE_3 0x37
  39. #define OV534_OP_WRITE_2 0x33
  40. #define OV534_OP_READ_2 0xf9
  41. #define CTRL_TIMEOUT 500
  42. MODULE_AUTHOR("Antonio Ospite <ospite@studenti.unina.it>");
  43. MODULE_DESCRIPTION("GSPCA/OV534 USB Camera Driver");
  44. MODULE_LICENSE("GPL");
  45. /* specific webcam descriptor */
  46. struct sd {
  47. struct gspca_dev gspca_dev; /* !! must be the first item */
  48. __u32 last_pts;
  49. u16 last_fid;
  50. u8 frame_rate;
  51. u8 brightness;
  52. u8 contrast;
  53. u8 gain;
  54. u8 exposure;
  55. u8 agc;
  56. u8 awb;
  57. u8 aec;
  58. s8 sharpness;
  59. u8 hflip;
  60. u8 vflip;
  61. u8 freqfltr;
  62. };
  63. /* V4L2 controls supported by the driver */
  64. static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val);
  65. static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val);
  66. static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val);
  67. static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val);
  68. static int sd_setagc(struct gspca_dev *gspca_dev, __s32 val);
  69. static int sd_getagc(struct gspca_dev *gspca_dev, __s32 *val);
  70. static int sd_setsharpness(struct gspca_dev *gspca_dev, __s32 val);
  71. static int sd_getsharpness(struct gspca_dev *gspca_dev, __s32 *val);
  72. static int sd_sethflip(struct gspca_dev *gspca_dev, __s32 val);
  73. static int sd_gethflip(struct gspca_dev *gspca_dev, __s32 *val);
  74. static int sd_setvflip(struct gspca_dev *gspca_dev, __s32 val);
  75. static int sd_getvflip(struct gspca_dev *gspca_dev, __s32 *val);
  76. static int sd_setawb(struct gspca_dev *gspca_dev, __s32 val);
  77. static int sd_getawb(struct gspca_dev *gspca_dev, __s32 *val);
  78. static int sd_setaec(struct gspca_dev *gspca_dev, __s32 val);
  79. static int sd_getaec(struct gspca_dev *gspca_dev, __s32 *val);
  80. static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
  81. static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
  82. static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val);
  83. static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val);
  84. static int sd_setfreqfltr(struct gspca_dev *gspca_dev, __s32 val);
  85. static int sd_getfreqfltr(struct gspca_dev *gspca_dev, __s32 *val);
  86. static int sd_querymenu(struct gspca_dev *gspca_dev,
  87. struct v4l2_querymenu *menu);
  88. static const struct ctrl sd_ctrls[] = {
  89. { /* 0 */
  90. {
  91. .id = V4L2_CID_BRIGHTNESS,
  92. .type = V4L2_CTRL_TYPE_INTEGER,
  93. .name = "Brightness",
  94. .minimum = 0,
  95. .maximum = 255,
  96. .step = 1,
  97. #define BRIGHTNESS_DEF 0
  98. .default_value = BRIGHTNESS_DEF,
  99. },
  100. .set = sd_setbrightness,
  101. .get = sd_getbrightness,
  102. },
  103. { /* 1 */
  104. {
  105. .id = V4L2_CID_CONTRAST,
  106. .type = V4L2_CTRL_TYPE_INTEGER,
  107. .name = "Contrast",
  108. .minimum = 0,
  109. .maximum = 255,
  110. .step = 1,
  111. #define CONTRAST_DEF 32
  112. .default_value = CONTRAST_DEF,
  113. },
  114. .set = sd_setcontrast,
  115. .get = sd_getcontrast,
  116. },
  117. { /* 2 */
  118. {
  119. .id = V4L2_CID_GAIN,
  120. .type = V4L2_CTRL_TYPE_INTEGER,
  121. .name = "Main Gain",
  122. .minimum = 0,
  123. .maximum = 63,
  124. .step = 1,
  125. #define GAIN_DEF 20
  126. .default_value = GAIN_DEF,
  127. },
  128. .set = sd_setgain,
  129. .get = sd_getgain,
  130. },
  131. { /* 3 */
  132. {
  133. .id = V4L2_CID_EXPOSURE,
  134. .type = V4L2_CTRL_TYPE_INTEGER,
  135. .name = "Exposure",
  136. .minimum = 0,
  137. .maximum = 255,
  138. .step = 1,
  139. #define EXPO_DEF 120
  140. .default_value = EXPO_DEF,
  141. },
  142. .set = sd_setexposure,
  143. .get = sd_getexposure,
  144. },
  145. { /* 4 */
  146. {
  147. .id = V4L2_CID_AUTOGAIN,
  148. .type = V4L2_CTRL_TYPE_BOOLEAN,
  149. .name = "Auto Gain",
  150. .minimum = 0,
  151. .maximum = 1,
  152. .step = 1,
  153. #define AGC_DEF 1
  154. .default_value = AGC_DEF,
  155. },
  156. .set = sd_setagc,
  157. .get = sd_getagc,
  158. },
  159. #define AWB_IDX 5
  160. { /* 5 */
  161. {
  162. .id = V4L2_CID_AUTO_WHITE_BALANCE,
  163. .type = V4L2_CTRL_TYPE_BOOLEAN,
  164. .name = "Auto White Balance",
  165. .minimum = 0,
  166. .maximum = 1,
  167. .step = 1,
  168. #define AWB_DEF 1
  169. .default_value = AWB_DEF,
  170. },
  171. .set = sd_setawb,
  172. .get = sd_getawb,
  173. },
  174. { /* 6 */
  175. {
  176. .id = V4L2_CID_EXPOSURE_AUTO,
  177. .type = V4L2_CTRL_TYPE_BOOLEAN,
  178. .name = "Auto Exposure",
  179. .minimum = 0,
  180. .maximum = 1,
  181. .step = 1,
  182. #define AEC_DEF 1
  183. .default_value = AEC_DEF,
  184. },
  185. .set = sd_setaec,
  186. .get = sd_getaec,
  187. },
  188. { /* 7 */
  189. {
  190. .id = V4L2_CID_SHARPNESS,
  191. .type = V4L2_CTRL_TYPE_INTEGER,
  192. .name = "Sharpness",
  193. .minimum = 0,
  194. .maximum = 63,
  195. .step = 1,
  196. #define SHARPNESS_DEF 0
  197. .default_value = SHARPNESS_DEF,
  198. },
  199. .set = sd_setsharpness,
  200. .get = sd_getsharpness,
  201. },
  202. { /* 8 */
  203. {
  204. .id = V4L2_CID_HFLIP,
  205. .type = V4L2_CTRL_TYPE_BOOLEAN,
  206. .name = "HFlip",
  207. .minimum = 0,
  208. .maximum = 1,
  209. .step = 1,
  210. #define HFLIP_DEF 0
  211. .default_value = HFLIP_DEF,
  212. },
  213. .set = sd_sethflip,
  214. .get = sd_gethflip,
  215. },
  216. { /* 9 */
  217. {
  218. .id = V4L2_CID_VFLIP,
  219. .type = V4L2_CTRL_TYPE_BOOLEAN,
  220. .name = "VFlip",
  221. .minimum = 0,
  222. .maximum = 1,
  223. .step = 1,
  224. #define VFLIP_DEF 0
  225. .default_value = VFLIP_DEF,
  226. },
  227. .set = sd_setvflip,
  228. .get = sd_getvflip,
  229. },
  230. { /* 10 */
  231. {
  232. .id = V4L2_CID_POWER_LINE_FREQUENCY,
  233. .type = V4L2_CTRL_TYPE_MENU,
  234. .name = "Light Frequency Filter",
  235. .minimum = 0,
  236. .maximum = 1,
  237. .step = 1,
  238. #define FREQFLTR_DEF 0
  239. .default_value = FREQFLTR_DEF,
  240. },
  241. .set = sd_setfreqfltr,
  242. .get = sd_getfreqfltr,
  243. },
  244. };
  245. static const struct v4l2_pix_format ov772x_mode[] = {
  246. {320, 240, V4L2_PIX_FMT_YUYV, V4L2_FIELD_NONE,
  247. .bytesperline = 320 * 2,
  248. .sizeimage = 320 * 240 * 2,
  249. .colorspace = V4L2_COLORSPACE_SRGB,
  250. .priv = 1},
  251. {640, 480, V4L2_PIX_FMT_YUYV, V4L2_FIELD_NONE,
  252. .bytesperline = 640 * 2,
  253. .sizeimage = 640 * 480 * 2,
  254. .colorspace = V4L2_COLORSPACE_SRGB,
  255. .priv = 0},
  256. };
  257. static const u8 qvga_rates[] = {125, 100, 75, 60, 50, 40, 30};
  258. static const u8 vga_rates[] = {60, 50, 40, 30, 15};
  259. static const struct framerates ov772x_framerates[] = {
  260. { /* 320x240 */
  261. .rates = qvga_rates,
  262. .nrates = ARRAY_SIZE(qvga_rates),
  263. },
  264. { /* 640x480 */
  265. .rates = vga_rates,
  266. .nrates = ARRAY_SIZE(vga_rates),
  267. },
  268. };
  269. static const u8 bridge_init[][2] = {
  270. { 0xc2, 0x0c },
  271. { 0x88, 0xf8 },
  272. { 0xc3, 0x69 },
  273. { 0x89, 0xff },
  274. { 0x76, 0x03 },
  275. { 0x92, 0x01 },
  276. { 0x93, 0x18 },
  277. { 0x94, 0x10 },
  278. { 0x95, 0x10 },
  279. { 0xe2, 0x00 },
  280. { 0xe7, 0x3e },
  281. { 0x96, 0x00 },
  282. { 0x97, 0x20 },
  283. { 0x97, 0x20 },
  284. { 0x97, 0x20 },
  285. { 0x97, 0x0a },
  286. { 0x97, 0x3f },
  287. { 0x97, 0x4a },
  288. { 0x97, 0x20 },
  289. { 0x97, 0x15 },
  290. { 0x97, 0x0b },
  291. { 0x8e, 0x40 },
  292. { 0x1f, 0x81 },
  293. { 0x34, 0x05 },
  294. { 0xe3, 0x04 },
  295. { 0x88, 0x00 },
  296. { 0x89, 0x00 },
  297. { 0x76, 0x00 },
  298. { 0xe7, 0x2e },
  299. { 0x31, 0xf9 },
  300. { 0x25, 0x42 },
  301. { 0x21, 0xf0 },
  302. { 0x1c, 0x00 },
  303. { 0x1d, 0x40 },
  304. { 0x1d, 0x02 }, /* payload size 0x0200 * 4 = 2048 bytes */
  305. { 0x1d, 0x00 }, /* payload size */
  306. { 0x1d, 0x02 }, /* frame size 0x025800 * 4 = 614400 */
  307. { 0x1d, 0x58 }, /* frame size */
  308. { 0x1d, 0x00 }, /* frame size */
  309. { 0x1c, 0x0a },
  310. { 0x1d, 0x08 }, /* turn on UVC header */
  311. { 0x1d, 0x0e }, /* .. */
  312. { 0x8d, 0x1c },
  313. { 0x8e, 0x80 },
  314. { 0xe5, 0x04 },
  315. { 0xc0, 0x50 },
  316. { 0xc1, 0x3c },
  317. { 0xc2, 0x0c },
  318. };
  319. static const u8 sensor_init[][2] = {
  320. { 0x12, 0x80 },
  321. { 0x11, 0x01 },
  322. /*fixme: better have a delay?*/
  323. { 0x11, 0x01 },
  324. { 0x11, 0x01 },
  325. { 0x11, 0x01 },
  326. { 0x11, 0x01 },
  327. { 0x11, 0x01 },
  328. { 0x11, 0x01 },
  329. { 0x11, 0x01 },
  330. { 0x11, 0x01 },
  331. { 0x11, 0x01 },
  332. { 0x11, 0x01 },
  333. { 0x3d, 0x03 },
  334. { 0x17, 0x26 },
  335. { 0x18, 0xa0 },
  336. { 0x19, 0x07 },
  337. { 0x1a, 0xf0 },
  338. { 0x32, 0x00 },
  339. { 0x29, 0xa0 },
  340. { 0x2c, 0xf0 },
  341. { 0x65, 0x20 },
  342. { 0x11, 0x01 },
  343. { 0x42, 0x7f },
  344. { 0x63, 0xaa }, /* AWB - was e0 */
  345. { 0x64, 0xff },
  346. { 0x66, 0x00 },
  347. { 0x13, 0xf0 }, /* com8 */
  348. { 0x0d, 0x41 },
  349. { 0x0f, 0xc5 },
  350. { 0x14, 0x11 },
  351. { 0x22, 0x7f },
  352. { 0x23, 0x03 },
  353. { 0x24, 0x40 },
  354. { 0x25, 0x30 },
  355. { 0x26, 0xa1 },
  356. { 0x2a, 0x00 },
  357. { 0x2b, 0x00 },
  358. { 0x6b, 0xaa },
  359. { 0x13, 0xff }, /* AWB */
  360. { 0x90, 0x05 },
  361. { 0x91, 0x01 },
  362. { 0x92, 0x03 },
  363. { 0x93, 0x00 },
  364. { 0x94, 0x60 },
  365. { 0x95, 0x3c },
  366. { 0x96, 0x24 },
  367. { 0x97, 0x1e },
  368. { 0x98, 0x62 },
  369. { 0x99, 0x80 },
  370. { 0x9a, 0x1e },
  371. { 0x9b, 0x08 },
  372. { 0x9c, 0x20 },
  373. { 0x9e, 0x81 },
  374. { 0xa6, 0x04 },
  375. { 0x7e, 0x0c },
  376. { 0x7f, 0x16 },
  377. { 0x80, 0x2a },
  378. { 0x81, 0x4e },
  379. { 0x82, 0x61 },
  380. { 0x83, 0x6f },
  381. { 0x84, 0x7b },
  382. { 0x85, 0x86 },
  383. { 0x86, 0x8e },
  384. { 0x87, 0x97 },
  385. { 0x88, 0xa4 },
  386. { 0x89, 0xaf },
  387. { 0x8a, 0xc5 },
  388. { 0x8b, 0xd7 },
  389. { 0x8c, 0xe8 },
  390. { 0x8d, 0x20 },
  391. { 0x0c, 0x90 },
  392. { 0x2b, 0x00 },
  393. { 0x22, 0x7f },
  394. { 0x23, 0x03 },
  395. { 0x11, 0x01 },
  396. { 0x0c, 0xd0 },
  397. { 0x64, 0xff },
  398. { 0x0d, 0x41 },
  399. { 0x14, 0x41 },
  400. { 0x0e, 0xcd },
  401. { 0xac, 0xbf },
  402. { 0x8e, 0x00 }, /* De-noise threshold */
  403. { 0x0c, 0xd0 }
  404. };
  405. static const u8 bridge_start_vga[][2] = {
  406. {0x1c, 0x00},
  407. {0x1d, 0x40},
  408. {0x1d, 0x02},
  409. {0x1d, 0x00},
  410. {0x1d, 0x02},
  411. {0x1d, 0x58},
  412. {0x1d, 0x00},
  413. {0xc0, 0x50},
  414. {0xc1, 0x3c},
  415. };
  416. static const u8 sensor_start_vga[][2] = {
  417. {0x12, 0x00},
  418. {0x17, 0x26},
  419. {0x18, 0xa0},
  420. {0x19, 0x07},
  421. {0x1a, 0xf0},
  422. {0x29, 0xa0},
  423. {0x2c, 0xf0},
  424. {0x65, 0x20},
  425. };
  426. static const u8 bridge_start_qvga[][2] = {
  427. {0x1c, 0x00},
  428. {0x1d, 0x40},
  429. {0x1d, 0x02},
  430. {0x1d, 0x00},
  431. {0x1d, 0x01},
  432. {0x1d, 0x4b},
  433. {0x1d, 0x00},
  434. {0xc0, 0x28},
  435. {0xc1, 0x1e},
  436. };
  437. static const u8 sensor_start_qvga[][2] = {
  438. {0x12, 0x40},
  439. {0x17, 0x3f},
  440. {0x18, 0x50},
  441. {0x19, 0x03},
  442. {0x1a, 0x78},
  443. {0x29, 0x50},
  444. {0x2c, 0x78},
  445. {0x65, 0x2f},
  446. };
  447. static void ov534_reg_write(struct gspca_dev *gspca_dev, u16 reg, u8 val)
  448. {
  449. struct usb_device *udev = gspca_dev->dev;
  450. int ret;
  451. if (gspca_dev->usb_err < 0)
  452. return;
  453. PDEBUG(D_USBO, "SET 01 0000 %04x %02x", reg, val);
  454. gspca_dev->usb_buf[0] = val;
  455. ret = usb_control_msg(udev,
  456. usb_sndctrlpipe(udev, 0),
  457. 0x01,
  458. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  459. 0x00, reg, gspca_dev->usb_buf, 1, CTRL_TIMEOUT);
  460. if (ret < 0) {
  461. err("write failed %d", ret);
  462. gspca_dev->usb_err = ret;
  463. }
  464. }
  465. static u8 ov534_reg_read(struct gspca_dev *gspca_dev, u16 reg)
  466. {
  467. struct usb_device *udev = gspca_dev->dev;
  468. int ret;
  469. if (gspca_dev->usb_err < 0)
  470. return 0;
  471. ret = usb_control_msg(udev,
  472. usb_rcvctrlpipe(udev, 0),
  473. 0x01,
  474. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  475. 0x00, reg, gspca_dev->usb_buf, 1, CTRL_TIMEOUT);
  476. PDEBUG(D_USBI, "GET 01 0000 %04x %02x", reg, gspca_dev->usb_buf[0]);
  477. if (ret < 0) {
  478. err("read failed %d", ret);
  479. gspca_dev->usb_err = ret;
  480. }
  481. return gspca_dev->usb_buf[0];
  482. }
  483. /* Two bits control LED: 0x21 bit 7 and 0x23 bit 7.
  484. * (direction and output)? */
  485. static void ov534_set_led(struct gspca_dev *gspca_dev, int status)
  486. {
  487. u8 data;
  488. PDEBUG(D_CONF, "led status: %d", status);
  489. data = ov534_reg_read(gspca_dev, 0x21);
  490. data |= 0x80;
  491. ov534_reg_write(gspca_dev, 0x21, data);
  492. data = ov534_reg_read(gspca_dev, 0x23);
  493. if (status)
  494. data |= 0x80;
  495. else
  496. data &= ~0x80;
  497. ov534_reg_write(gspca_dev, 0x23, data);
  498. if (!status) {
  499. data = ov534_reg_read(gspca_dev, 0x21);
  500. data &= ~0x80;
  501. ov534_reg_write(gspca_dev, 0x21, data);
  502. }
  503. }
  504. static int sccb_check_status(struct gspca_dev *gspca_dev)
  505. {
  506. u8 data;
  507. int i;
  508. for (i = 0; i < 5; i++) {
  509. data = ov534_reg_read(gspca_dev, OV534_REG_STATUS);
  510. switch (data) {
  511. case 0x00:
  512. return 1;
  513. case 0x04:
  514. return 0;
  515. case 0x03:
  516. break;
  517. default:
  518. PDEBUG(D_ERR, "sccb status 0x%02x, attempt %d/5",
  519. data, i + 1);
  520. }
  521. }
  522. return 0;
  523. }
  524. static void sccb_reg_write(struct gspca_dev *gspca_dev, u8 reg, u8 val)
  525. {
  526. PDEBUG(D_USBO, "sccb write: %02x %02x", reg, val);
  527. ov534_reg_write(gspca_dev, OV534_REG_SUBADDR, reg);
  528. ov534_reg_write(gspca_dev, OV534_REG_WRITE, val);
  529. ov534_reg_write(gspca_dev, OV534_REG_OPERATION, OV534_OP_WRITE_3);
  530. if (!sccb_check_status(gspca_dev)) {
  531. err("sccb_reg_write failed");
  532. gspca_dev->usb_err = -EIO;
  533. }
  534. }
  535. static u8 sccb_reg_read(struct gspca_dev *gspca_dev, u16 reg)
  536. {
  537. ov534_reg_write(gspca_dev, OV534_REG_SUBADDR, reg);
  538. ov534_reg_write(gspca_dev, OV534_REG_OPERATION, OV534_OP_WRITE_2);
  539. if (!sccb_check_status(gspca_dev))
  540. err("sccb_reg_read failed 1");
  541. ov534_reg_write(gspca_dev, OV534_REG_OPERATION, OV534_OP_READ_2);
  542. if (!sccb_check_status(gspca_dev))
  543. err("sccb_reg_read failed 2");
  544. return ov534_reg_read(gspca_dev, OV534_REG_READ);
  545. }
  546. /* output a bridge sequence (reg - val) */
  547. static void reg_w_array(struct gspca_dev *gspca_dev,
  548. const u8 (*data)[2], int len)
  549. {
  550. while (--len >= 0) {
  551. ov534_reg_write(gspca_dev, (*data)[0], (*data)[1]);
  552. data++;
  553. }
  554. }
  555. /* output a sensor sequence (reg - val) */
  556. static void sccb_w_array(struct gspca_dev *gspca_dev,
  557. const u8 (*data)[2], int len)
  558. {
  559. while (--len >= 0) {
  560. if ((*data)[0] != 0xff) {
  561. sccb_reg_write(gspca_dev, (*data)[0], (*data)[1]);
  562. } else {
  563. sccb_reg_read(gspca_dev, (*data)[1]);
  564. sccb_reg_write(gspca_dev, 0xff, 0x00);
  565. }
  566. data++;
  567. }
  568. }
  569. /* ov772x specific controls */
  570. static void set_frame_rate(struct gspca_dev *gspca_dev)
  571. {
  572. struct sd *sd = (struct sd *) gspca_dev;
  573. int i;
  574. struct rate_s {
  575. u8 fps;
  576. u8 r11;
  577. u8 r0d;
  578. u8 re5;
  579. };
  580. const struct rate_s *r;
  581. static const struct rate_s rate_0[] = { /* 640x480 */
  582. {60, 0x01, 0xc1, 0x04},
  583. {50, 0x01, 0x41, 0x02},
  584. {40, 0x02, 0xc1, 0x04},
  585. {30, 0x04, 0x81, 0x02},
  586. {15, 0x03, 0x41, 0x04},
  587. };
  588. static const struct rate_s rate_1[] = { /* 320x240 */
  589. {125, 0x02, 0x81, 0x02},
  590. {100, 0x02, 0xc1, 0x04},
  591. {75, 0x03, 0xc1, 0x04},
  592. {60, 0x04, 0xc1, 0x04},
  593. {50, 0x02, 0x41, 0x04},
  594. {40, 0x03, 0x41, 0x04},
  595. {30, 0x04, 0x41, 0x04},
  596. };
  597. if (gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv == 0) {
  598. r = rate_0;
  599. i = ARRAY_SIZE(rate_0);
  600. } else {
  601. r = rate_1;
  602. i = ARRAY_SIZE(rate_1);
  603. }
  604. while (--i > 0) {
  605. if (sd->frame_rate >= r->fps)
  606. break;
  607. r++;
  608. }
  609. sccb_reg_write(gspca_dev, 0x11, r->r11);
  610. sccb_reg_write(gspca_dev, 0x0d, r->r0d);
  611. ov534_reg_write(gspca_dev, 0xe5, r->re5);
  612. PDEBUG(D_PROBE, "frame_rate: %d", r->fps);
  613. }
  614. static void setbrightness(struct gspca_dev *gspca_dev)
  615. {
  616. struct sd *sd = (struct sd *) gspca_dev;
  617. sccb_reg_write(gspca_dev, 0x9b, sd->brightness);
  618. }
  619. static void setcontrast(struct gspca_dev *gspca_dev)
  620. {
  621. struct sd *sd = (struct sd *) gspca_dev;
  622. sccb_reg_write(gspca_dev, 0x9c, sd->contrast);
  623. }
  624. static void setgain(struct gspca_dev *gspca_dev)
  625. {
  626. struct sd *sd = (struct sd *) gspca_dev;
  627. u8 val;
  628. if (sd->agc)
  629. return;
  630. val = sd->gain;
  631. switch (val & 0x30) {
  632. case 0x00:
  633. val &= 0x0f;
  634. break;
  635. case 0x10:
  636. val &= 0x0f;
  637. val |= 0x30;
  638. break;
  639. case 0x20:
  640. val &= 0x0f;
  641. val |= 0x70;
  642. break;
  643. default:
  644. /* case 0x30: */
  645. val &= 0x0f;
  646. val |= 0xf0;
  647. break;
  648. }
  649. sccb_reg_write(gspca_dev, 0x00, val);
  650. }
  651. static void setexposure(struct gspca_dev *gspca_dev)
  652. {
  653. struct sd *sd = (struct sd *) gspca_dev;
  654. u8 val;
  655. if (sd->aec)
  656. return;
  657. /* 'val' is one byte and represents half of the exposure value we are
  658. * going to set into registers, a two bytes value:
  659. *
  660. * MSB: ((u16) val << 1) >> 8 == val >> 7
  661. * LSB: ((u16) val << 1) & 0xff == val << 1
  662. */
  663. val = sd->exposure;
  664. sccb_reg_write(gspca_dev, 0x08, val >> 7);
  665. sccb_reg_write(gspca_dev, 0x10, val << 1);
  666. }
  667. static void setagc(struct gspca_dev *gspca_dev)
  668. {
  669. struct sd *sd = (struct sd *) gspca_dev;
  670. if (sd->agc) {
  671. sccb_reg_write(gspca_dev, 0x13,
  672. sccb_reg_read(gspca_dev, 0x13) | 0x04);
  673. sccb_reg_write(gspca_dev, 0x64,
  674. sccb_reg_read(gspca_dev, 0x64) | 0x03);
  675. } else {
  676. sccb_reg_write(gspca_dev, 0x13,
  677. sccb_reg_read(gspca_dev, 0x13) & ~0x04);
  678. sccb_reg_write(gspca_dev, 0x64,
  679. sccb_reg_read(gspca_dev, 0x64) & ~0x03);
  680. setgain(gspca_dev);
  681. }
  682. }
  683. static void setawb(struct gspca_dev *gspca_dev)
  684. {
  685. struct sd *sd = (struct sd *) gspca_dev;
  686. if (sd->awb) {
  687. sccb_reg_write(gspca_dev, 0x13,
  688. sccb_reg_read(gspca_dev, 0x13) | 0x02);
  689. sccb_reg_write(gspca_dev, 0x63,
  690. sccb_reg_read(gspca_dev, 0x63) | 0xc0);
  691. } else {
  692. sccb_reg_write(gspca_dev, 0x13,
  693. sccb_reg_read(gspca_dev, 0x13) & ~0x02);
  694. sccb_reg_write(gspca_dev, 0x63,
  695. sccb_reg_read(gspca_dev, 0x63) & ~0xc0);
  696. }
  697. }
  698. static void setaec(struct gspca_dev *gspca_dev)
  699. {
  700. struct sd *sd = (struct sd *) gspca_dev;
  701. if (sd->aec)
  702. sccb_reg_write(gspca_dev, 0x13,
  703. sccb_reg_read(gspca_dev, 0x13) | 0x01);
  704. else {
  705. sccb_reg_write(gspca_dev, 0x13,
  706. sccb_reg_read(gspca_dev, 0x13) & ~0x01);
  707. setexposure(gspca_dev);
  708. }
  709. }
  710. static void setsharpness(struct gspca_dev *gspca_dev)
  711. {
  712. struct sd *sd = (struct sd *) gspca_dev;
  713. u8 val;
  714. val = sd->sharpness;
  715. sccb_reg_write(gspca_dev, 0x91, val); /* Auto de-noise threshold */
  716. sccb_reg_write(gspca_dev, 0x8e, val); /* De-noise threshold */
  717. }
  718. static void sethflip(struct gspca_dev *gspca_dev)
  719. {
  720. struct sd *sd = (struct sd *) gspca_dev;
  721. if (sd->hflip == 0)
  722. sccb_reg_write(gspca_dev, 0x0c,
  723. sccb_reg_read(gspca_dev, 0x0c) | 0x40);
  724. else
  725. sccb_reg_write(gspca_dev, 0x0c,
  726. sccb_reg_read(gspca_dev, 0x0c) & ~0x40);
  727. }
  728. static void setvflip(struct gspca_dev *gspca_dev)
  729. {
  730. struct sd *sd = (struct sd *) gspca_dev;
  731. if (sd->vflip == 0)
  732. sccb_reg_write(gspca_dev, 0x0c,
  733. sccb_reg_read(gspca_dev, 0x0c) | 0x80);
  734. else
  735. sccb_reg_write(gspca_dev, 0x0c,
  736. sccb_reg_read(gspca_dev, 0x0c) & ~0x80);
  737. }
  738. static void setfreqfltr(struct gspca_dev *gspca_dev)
  739. {
  740. struct sd *sd = (struct sd *) gspca_dev;
  741. if (sd->freqfltr == 0)
  742. sccb_reg_write(gspca_dev, 0x2b, 0x00);
  743. else
  744. sccb_reg_write(gspca_dev, 0x2b, 0x9e);
  745. }
  746. /* this function is called at probe time */
  747. static int sd_config(struct gspca_dev *gspca_dev,
  748. const struct usb_device_id *id)
  749. {
  750. struct sd *sd = (struct sd *) gspca_dev;
  751. struct cam *cam;
  752. cam = &gspca_dev->cam;
  753. cam->cam_mode = ov772x_mode;
  754. cam->nmodes = ARRAY_SIZE(ov772x_mode);
  755. cam->mode_framerates = ov772x_framerates;
  756. cam->bulk = 1;
  757. cam->bulk_size = 16384;
  758. cam->bulk_nurbs = 2;
  759. sd->frame_rate = 30;
  760. sd->brightness = BRIGHTNESS_DEF;
  761. sd->contrast = CONTRAST_DEF;
  762. sd->gain = GAIN_DEF;
  763. sd->exposure = EXPO_DEF;
  764. #if AGC_DEF != 0
  765. sd->agc = AGC_DEF;
  766. #else
  767. gspca_dev->ctrl_inac |= (1 << AWB_IDX);
  768. #endif
  769. sd->awb = AWB_DEF;
  770. sd->aec = AEC_DEF;
  771. sd->sharpness = SHARPNESS_DEF;
  772. sd->hflip = HFLIP_DEF;
  773. sd->vflip = VFLIP_DEF;
  774. sd->freqfltr = FREQFLTR_DEF;
  775. return 0;
  776. }
  777. /* this function is called at probe and resume time */
  778. static int sd_init(struct gspca_dev *gspca_dev)
  779. {
  780. u16 sensor_id;
  781. /* reset bridge */
  782. ov534_reg_write(gspca_dev, 0xe7, 0x3a);
  783. ov534_reg_write(gspca_dev, 0xe0, 0x08);
  784. msleep(100);
  785. /* initialize the sensor address */
  786. ov534_reg_write(gspca_dev, OV534_REG_ADDRESS, 0x42);
  787. /* reset sensor */
  788. sccb_reg_write(gspca_dev, 0x12, 0x80);
  789. msleep(10);
  790. /* probe the sensor */
  791. sccb_reg_read(gspca_dev, 0x0a);
  792. sensor_id = sccb_reg_read(gspca_dev, 0x0a) << 8;
  793. sccb_reg_read(gspca_dev, 0x0b);
  794. sensor_id |= sccb_reg_read(gspca_dev, 0x0b);
  795. PDEBUG(D_PROBE, "Sensor ID: %04x", sensor_id);
  796. /* initialize */
  797. reg_w_array(gspca_dev, bridge_init,
  798. ARRAY_SIZE(bridge_init));
  799. ov534_set_led(gspca_dev, 1);
  800. sccb_w_array(gspca_dev, sensor_init,
  801. ARRAY_SIZE(sensor_init));
  802. ov534_reg_write(gspca_dev, 0xe0, 0x09);
  803. ov534_set_led(gspca_dev, 0);
  804. set_frame_rate(gspca_dev);
  805. return gspca_dev->usb_err;
  806. }
  807. static int sd_start(struct gspca_dev *gspca_dev)
  808. {
  809. int mode;
  810. mode = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
  811. if (mode != 0) { /* 320x240 */
  812. reg_w_array(gspca_dev, bridge_start_qvga,
  813. ARRAY_SIZE(bridge_start_qvga));
  814. sccb_w_array(gspca_dev, sensor_start_qvga,
  815. ARRAY_SIZE(sensor_start_qvga));
  816. } else { /* 640x480 */
  817. reg_w_array(gspca_dev, bridge_start_vga,
  818. ARRAY_SIZE(bridge_start_vga));
  819. sccb_w_array(gspca_dev, sensor_start_vga,
  820. ARRAY_SIZE(sensor_start_vga));
  821. }
  822. set_frame_rate(gspca_dev);
  823. setagc(gspca_dev);
  824. setawb(gspca_dev);
  825. setaec(gspca_dev);
  826. setgain(gspca_dev);
  827. setexposure(gspca_dev);
  828. setbrightness(gspca_dev);
  829. setcontrast(gspca_dev);
  830. setsharpness(gspca_dev);
  831. setvflip(gspca_dev);
  832. sethflip(gspca_dev);
  833. setfreqfltr(gspca_dev);
  834. ov534_set_led(gspca_dev, 1);
  835. ov534_reg_write(gspca_dev, 0xe0, 0x00);
  836. return gspca_dev->usb_err;
  837. }
  838. static void sd_stopN(struct gspca_dev *gspca_dev)
  839. {
  840. ov534_reg_write(gspca_dev, 0xe0, 0x09);
  841. ov534_set_led(gspca_dev, 0);
  842. }
  843. /* Values for bmHeaderInfo (Video and Still Image Payload Headers, 2.4.3.3) */
  844. #define UVC_STREAM_EOH (1 << 7)
  845. #define UVC_STREAM_ERR (1 << 6)
  846. #define UVC_STREAM_STI (1 << 5)
  847. #define UVC_STREAM_RES (1 << 4)
  848. #define UVC_STREAM_SCR (1 << 3)
  849. #define UVC_STREAM_PTS (1 << 2)
  850. #define UVC_STREAM_EOF (1 << 1)
  851. #define UVC_STREAM_FID (1 << 0)
  852. static void sd_pkt_scan(struct gspca_dev *gspca_dev,
  853. u8 *data, int len)
  854. {
  855. struct sd *sd = (struct sd *) gspca_dev;
  856. __u32 this_pts;
  857. u16 this_fid;
  858. int remaining_len = len;
  859. do {
  860. len = min(remaining_len, 2048);
  861. /* Payloads are prefixed with a UVC-style header. We
  862. consider a frame to start when the FID toggles, or the PTS
  863. changes. A frame ends when EOF is set, and we've received
  864. the correct number of bytes. */
  865. /* Verify UVC header. Header length is always 12 */
  866. if (data[0] != 12 || len < 12) {
  867. PDEBUG(D_PACK, "bad header");
  868. goto discard;
  869. }
  870. /* Check errors */
  871. if (data[1] & UVC_STREAM_ERR) {
  872. PDEBUG(D_PACK, "payload error");
  873. goto discard;
  874. }
  875. /* Extract PTS and FID */
  876. if (!(data[1] & UVC_STREAM_PTS)) {
  877. PDEBUG(D_PACK, "PTS not present");
  878. goto discard;
  879. }
  880. this_pts = (data[5] << 24) | (data[4] << 16)
  881. | (data[3] << 8) | data[2];
  882. this_fid = (data[1] & UVC_STREAM_FID) ? 1 : 0;
  883. /* If PTS or FID has changed, start a new frame. */
  884. if (this_pts != sd->last_pts || this_fid != sd->last_fid) {
  885. if (gspca_dev->last_packet_type == INTER_PACKET)
  886. gspca_frame_add(gspca_dev, LAST_PACKET,
  887. NULL, 0);
  888. sd->last_pts = this_pts;
  889. sd->last_fid = this_fid;
  890. gspca_frame_add(gspca_dev, FIRST_PACKET,
  891. data + 12, len - 12);
  892. /* If this packet is marked as EOF, end the frame */
  893. } else if (data[1] & UVC_STREAM_EOF) {
  894. sd->last_pts = 0;
  895. if (gspca_dev->image_len + len - 12 !=
  896. gspca_dev->width * gspca_dev->height * 2) {
  897. PDEBUG(D_PACK, "wrong sized frame");
  898. goto discard;
  899. }
  900. gspca_frame_add(gspca_dev, LAST_PACKET,
  901. data + 12, len - 12);
  902. } else {
  903. /* Add the data from this payload */
  904. gspca_frame_add(gspca_dev, INTER_PACKET,
  905. data + 12, len - 12);
  906. }
  907. /* Done this payload */
  908. goto scan_next;
  909. discard:
  910. /* Discard data until a new frame starts. */
  911. gspca_dev->last_packet_type = DISCARD_PACKET;
  912. scan_next:
  913. remaining_len -= len;
  914. data += len;
  915. } while (remaining_len > 0);
  916. }
  917. /* controls */
  918. static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val)
  919. {
  920. struct sd *sd = (struct sd *) gspca_dev;
  921. sd->gain = val;
  922. if (gspca_dev->streaming)
  923. setgain(gspca_dev);
  924. return 0;
  925. }
  926. static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val)
  927. {
  928. struct sd *sd = (struct sd *) gspca_dev;
  929. *val = sd->gain;
  930. return 0;
  931. }
  932. static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val)
  933. {
  934. struct sd *sd = (struct sd *) gspca_dev;
  935. sd->exposure = val;
  936. if (gspca_dev->streaming)
  937. setexposure(gspca_dev);
  938. return 0;
  939. }
  940. static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val)
  941. {
  942. struct sd *sd = (struct sd *) gspca_dev;
  943. *val = sd->exposure;
  944. return 0;
  945. }
  946. static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
  947. {
  948. struct sd *sd = (struct sd *) gspca_dev;
  949. sd->brightness = val;
  950. if (gspca_dev->streaming)
  951. setbrightness(gspca_dev);
  952. return 0;
  953. }
  954. static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
  955. {
  956. struct sd *sd = (struct sd *) gspca_dev;
  957. *val = sd->brightness;
  958. return 0;
  959. }
  960. static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
  961. {
  962. struct sd *sd = (struct sd *) gspca_dev;
  963. sd->contrast = val;
  964. if (gspca_dev->streaming)
  965. setcontrast(gspca_dev);
  966. return 0;
  967. }
  968. static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val)
  969. {
  970. struct sd *sd = (struct sd *) gspca_dev;
  971. *val = sd->contrast;
  972. return 0;
  973. }
  974. static int sd_setagc(struct gspca_dev *gspca_dev, __s32 val)
  975. {
  976. struct sd *sd = (struct sd *) gspca_dev;
  977. sd->agc = val;
  978. if (gspca_dev->streaming) {
  979. /* the auto white balance control works only
  980. * when auto gain is set */
  981. if (val)
  982. gspca_dev->ctrl_inac &= ~(1 << AWB_IDX);
  983. else
  984. gspca_dev->ctrl_inac |= (1 << AWB_IDX);
  985. setagc(gspca_dev);
  986. }
  987. return 0;
  988. }
  989. static int sd_getagc(struct gspca_dev *gspca_dev, __s32 *val)
  990. {
  991. struct sd *sd = (struct sd *) gspca_dev;
  992. *val = sd->agc;
  993. return 0;
  994. }
  995. static int sd_setawb(struct gspca_dev *gspca_dev, __s32 val)
  996. {
  997. struct sd *sd = (struct sd *) gspca_dev;
  998. sd->awb = val;
  999. if (gspca_dev->streaming)
  1000. setawb(gspca_dev);
  1001. return 0;
  1002. }
  1003. static int sd_getawb(struct gspca_dev *gspca_dev, __s32 *val)
  1004. {
  1005. struct sd *sd = (struct sd *) gspca_dev;
  1006. *val = sd->awb;
  1007. return 0;
  1008. }
  1009. static int sd_setaec(struct gspca_dev *gspca_dev, __s32 val)
  1010. {
  1011. struct sd *sd = (struct sd *) gspca_dev;
  1012. sd->aec = val;
  1013. if (gspca_dev->streaming)
  1014. setaec(gspca_dev);
  1015. return 0;
  1016. }
  1017. static int sd_getaec(struct gspca_dev *gspca_dev, __s32 *val)
  1018. {
  1019. struct sd *sd = (struct sd *) gspca_dev;
  1020. *val = sd->aec;
  1021. return 0;
  1022. }
  1023. static int sd_setsharpness(struct gspca_dev *gspca_dev, __s32 val)
  1024. {
  1025. struct sd *sd = (struct sd *) gspca_dev;
  1026. sd->sharpness = val;
  1027. if (gspca_dev->streaming)
  1028. setsharpness(gspca_dev);
  1029. return 0;
  1030. }
  1031. static int sd_getsharpness(struct gspca_dev *gspca_dev, __s32 *val)
  1032. {
  1033. struct sd *sd = (struct sd *) gspca_dev;
  1034. *val = sd->sharpness;
  1035. return 0;
  1036. }
  1037. static int sd_sethflip(struct gspca_dev *gspca_dev, __s32 val)
  1038. {
  1039. struct sd *sd = (struct sd *) gspca_dev;
  1040. sd->hflip = val;
  1041. if (gspca_dev->streaming)
  1042. sethflip(gspca_dev);
  1043. return 0;
  1044. }
  1045. static int sd_gethflip(struct gspca_dev *gspca_dev, __s32 *val)
  1046. {
  1047. struct sd *sd = (struct sd *) gspca_dev;
  1048. *val = sd->hflip;
  1049. return 0;
  1050. }
  1051. static int sd_setvflip(struct gspca_dev *gspca_dev, __s32 val)
  1052. {
  1053. struct sd *sd = (struct sd *) gspca_dev;
  1054. sd->vflip = val;
  1055. if (gspca_dev->streaming)
  1056. setvflip(gspca_dev);
  1057. return 0;
  1058. }
  1059. static int sd_getvflip(struct gspca_dev *gspca_dev, __s32 *val)
  1060. {
  1061. struct sd *sd = (struct sd *) gspca_dev;
  1062. *val = sd->vflip;
  1063. return 0;
  1064. }
  1065. static int sd_setfreqfltr(struct gspca_dev *gspca_dev, __s32 val)
  1066. {
  1067. struct sd *sd = (struct sd *) gspca_dev;
  1068. sd->freqfltr = val;
  1069. if (gspca_dev->streaming)
  1070. setfreqfltr(gspca_dev);
  1071. return 0;
  1072. }
  1073. static int sd_getfreqfltr(struct gspca_dev *gspca_dev, __s32 *val)
  1074. {
  1075. struct sd *sd = (struct sd *) gspca_dev;
  1076. *val = sd->freqfltr;
  1077. return 0;
  1078. }
  1079. static int sd_querymenu(struct gspca_dev *gspca_dev,
  1080. struct v4l2_querymenu *menu)
  1081. {
  1082. switch (menu->id) {
  1083. case V4L2_CID_POWER_LINE_FREQUENCY:
  1084. switch (menu->index) {
  1085. case 0: /* V4L2_CID_POWER_LINE_FREQUENCY_DISABLED */
  1086. strcpy((char *) menu->name, "Disabled");
  1087. return 0;
  1088. case 1: /* V4L2_CID_POWER_LINE_FREQUENCY_50HZ */
  1089. strcpy((char *) menu->name, "50 Hz");
  1090. return 0;
  1091. }
  1092. break;
  1093. }
  1094. return -EINVAL;
  1095. }
  1096. /* get stream parameters (framerate) */
  1097. static void sd_get_streamparm(struct gspca_dev *gspca_dev,
  1098. struct v4l2_streamparm *parm)
  1099. {
  1100. struct v4l2_captureparm *cp = &parm->parm.capture;
  1101. struct v4l2_fract *tpf = &cp->timeperframe;
  1102. struct sd *sd = (struct sd *) gspca_dev;
  1103. cp->capability |= V4L2_CAP_TIMEPERFRAME;
  1104. tpf->numerator = 1;
  1105. tpf->denominator = sd->frame_rate;
  1106. }
  1107. /* set stream parameters (framerate) */
  1108. static void sd_set_streamparm(struct gspca_dev *gspca_dev,
  1109. struct v4l2_streamparm *parm)
  1110. {
  1111. struct v4l2_captureparm *cp = &parm->parm.capture;
  1112. struct v4l2_fract *tpf = &cp->timeperframe;
  1113. struct sd *sd = (struct sd *) gspca_dev;
  1114. /* Set requested framerate */
  1115. sd->frame_rate = tpf->denominator / tpf->numerator;
  1116. if (gspca_dev->streaming)
  1117. set_frame_rate(gspca_dev);
  1118. /* Return the actual framerate */
  1119. tpf->numerator = 1;
  1120. tpf->denominator = sd->frame_rate;
  1121. }
  1122. /* sub-driver description */
  1123. static const struct sd_desc sd_desc = {
  1124. .name = MODULE_NAME,
  1125. .ctrls = sd_ctrls,
  1126. .nctrls = ARRAY_SIZE(sd_ctrls),
  1127. .config = sd_config,
  1128. .init = sd_init,
  1129. .start = sd_start,
  1130. .stopN = sd_stopN,
  1131. .pkt_scan = sd_pkt_scan,
  1132. .querymenu = sd_querymenu,
  1133. .get_streamparm = sd_get_streamparm,
  1134. .set_streamparm = sd_set_streamparm,
  1135. };
  1136. /* -- module initialisation -- */
  1137. static const struct usb_device_id device_table[] = {
  1138. {USB_DEVICE(0x1415, 0x2000)},
  1139. {}
  1140. };
  1141. MODULE_DEVICE_TABLE(usb, device_table);
  1142. /* -- device connect -- */
  1143. static int sd_probe(struct usb_interface *intf, const struct usb_device_id *id)
  1144. {
  1145. return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
  1146. THIS_MODULE);
  1147. }
  1148. static struct usb_driver sd_driver = {
  1149. .name = MODULE_NAME,
  1150. .id_table = device_table,
  1151. .probe = sd_probe,
  1152. .disconnect = gspca_disconnect,
  1153. #ifdef CONFIG_PM
  1154. .suspend = gspca_suspend,
  1155. .resume = gspca_resume,
  1156. #endif
  1157. };
  1158. /* -- module insert / remove -- */
  1159. static int __init sd_mod_init(void)
  1160. {
  1161. return usb_register(&sd_driver);
  1162. }
  1163. static void __exit sd_mod_exit(void)
  1164. {
  1165. usb_deregister(&sd_driver);
  1166. }
  1167. module_init(sd_mod_init);
  1168. module_exit(sd_mod_exit);