se401.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  1. /*
  2. * GSPCA Endpoints (formerly known as AOX) se401 USB Camera sub Driver
  3. *
  4. * Copyright (C) 2011 Hans de Goede <hdegoede@redhat.com>
  5. *
  6. * Based on the v4l1 se401 driver which is:
  7. *
  8. * Copyright (c) 2000 Jeroen B. Vreeken (pe1rxq@amsat.org)
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  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. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. *
  24. */
  25. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  26. #define MODULE_NAME "se401"
  27. #define BULK_SIZE 4096
  28. #define PACKET_SIZE 1024
  29. #define READ_REQ_SIZE 64
  30. #define MAX_MODES ((READ_REQ_SIZE - 6) / 4)
  31. /* The se401 compression algorithm uses a fixed quant factor, which
  32. can be configured by setting the high nibble of the SE401_OPERATINGMODE
  33. feature. This needs to exactly match what is in libv4l! */
  34. #define SE401_QUANT_FACT 8
  35. #include <linux/input.h>
  36. #include <linux/slab.h>
  37. #include "gspca.h"
  38. #include "se401.h"
  39. MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
  40. MODULE_DESCRIPTION("Endpoints se401");
  41. MODULE_LICENSE("GPL");
  42. /* exposure change state machine states */
  43. enum {
  44. EXPO_CHANGED,
  45. EXPO_DROP_FRAME,
  46. EXPO_NO_CHANGE,
  47. };
  48. /* specific webcam descriptor */
  49. struct sd {
  50. struct gspca_dev gspca_dev; /* !! must be the first item */
  51. struct { /* exposure/freq control cluster */
  52. struct v4l2_ctrl *exposure;
  53. struct v4l2_ctrl *freq;
  54. };
  55. bool has_brightness;
  56. struct v4l2_pix_format fmts[MAX_MODES];
  57. int pixels_read;
  58. int packet_read;
  59. u8 packet[PACKET_SIZE];
  60. u8 restart_stream;
  61. u8 button_state;
  62. u8 resetlevel;
  63. u8 resetlevel_frame_count;
  64. int resetlevel_adjust_dir;
  65. int expo_change_state;
  66. };
  67. static void se401_write_req(struct gspca_dev *gspca_dev, u16 req, u16 value,
  68. int silent)
  69. {
  70. int err;
  71. if (gspca_dev->usb_err < 0)
  72. return;
  73. err = usb_control_msg(gspca_dev->dev,
  74. usb_sndctrlpipe(gspca_dev->dev, 0), req,
  75. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  76. value, 0, NULL, 0, 1000);
  77. if (err < 0) {
  78. if (!silent)
  79. pr_err("write req failed req %#04x val %#04x error %d\n",
  80. req, value, err);
  81. gspca_dev->usb_err = err;
  82. }
  83. }
  84. static void se401_read_req(struct gspca_dev *gspca_dev, u16 req, int silent)
  85. {
  86. int err;
  87. if (gspca_dev->usb_err < 0)
  88. return;
  89. if (USB_BUF_SZ < READ_REQ_SIZE) {
  90. pr_err("USB_BUF_SZ too small!!\n");
  91. gspca_dev->usb_err = -ENOBUFS;
  92. return;
  93. }
  94. err = usb_control_msg(gspca_dev->dev,
  95. usb_rcvctrlpipe(gspca_dev->dev, 0), req,
  96. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  97. 0, 0, gspca_dev->usb_buf, READ_REQ_SIZE, 1000);
  98. if (err < 0) {
  99. if (!silent)
  100. pr_err("read req failed req %#04x error %d\n",
  101. req, err);
  102. gspca_dev->usb_err = err;
  103. }
  104. }
  105. static void se401_set_feature(struct gspca_dev *gspca_dev,
  106. u16 selector, u16 param)
  107. {
  108. int err;
  109. if (gspca_dev->usb_err < 0)
  110. return;
  111. err = usb_control_msg(gspca_dev->dev,
  112. usb_sndctrlpipe(gspca_dev->dev, 0),
  113. SE401_REQ_SET_EXT_FEATURE,
  114. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  115. param, selector, NULL, 0, 1000);
  116. if (err < 0) {
  117. pr_err("set feature failed sel %#04x param %#04x error %d\n",
  118. selector, param, err);
  119. gspca_dev->usb_err = err;
  120. }
  121. }
  122. static int se401_get_feature(struct gspca_dev *gspca_dev, u16 selector)
  123. {
  124. int err;
  125. if (gspca_dev->usb_err < 0)
  126. return gspca_dev->usb_err;
  127. if (USB_BUF_SZ < 2) {
  128. pr_err("USB_BUF_SZ too small!!\n");
  129. gspca_dev->usb_err = -ENOBUFS;
  130. return gspca_dev->usb_err;
  131. }
  132. err = usb_control_msg(gspca_dev->dev,
  133. usb_rcvctrlpipe(gspca_dev->dev, 0),
  134. SE401_REQ_GET_EXT_FEATURE,
  135. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  136. 0, selector, gspca_dev->usb_buf, 2, 1000);
  137. if (err < 0) {
  138. pr_err("get feature failed sel %#04x error %d\n",
  139. selector, err);
  140. gspca_dev->usb_err = err;
  141. return err;
  142. }
  143. return gspca_dev->usb_buf[0] | (gspca_dev->usb_buf[1] << 8);
  144. }
  145. static void setbrightness(struct gspca_dev *gspca_dev, s32 val)
  146. {
  147. /* HDG: this does not seem to do anything on my cam */
  148. se401_write_req(gspca_dev, SE401_REQ_SET_BRT, val, 0);
  149. }
  150. static void setgain(struct gspca_dev *gspca_dev, s32 val)
  151. {
  152. u16 gain = 63 - val;
  153. /* red color gain */
  154. se401_set_feature(gspca_dev, HV7131_REG_ARCG, gain);
  155. /* green color gain */
  156. se401_set_feature(gspca_dev, HV7131_REG_AGCG, gain);
  157. /* blue color gain */
  158. se401_set_feature(gspca_dev, HV7131_REG_ABCG, gain);
  159. }
  160. static void setexposure(struct gspca_dev *gspca_dev, s32 val, s32 freq)
  161. {
  162. struct sd *sd = (struct sd *) gspca_dev;
  163. int integration = val << 6;
  164. u8 expose_h, expose_m, expose_l;
  165. /* Do this before the set_feature calls, for proper timing wrt
  166. the interrupt driven pkt_scan. Note we may still race but that
  167. is not a big issue, the expo change state machine is merely for
  168. avoiding underexposed frames getting send out, if one sneaks
  169. through so be it */
  170. sd->expo_change_state = EXPO_CHANGED;
  171. if (freq == V4L2_CID_POWER_LINE_FREQUENCY_50HZ)
  172. integration = integration - integration % 106667;
  173. if (freq == V4L2_CID_POWER_LINE_FREQUENCY_60HZ)
  174. integration = integration - integration % 88889;
  175. expose_h = (integration >> 16);
  176. expose_m = (integration >> 8);
  177. expose_l = integration;
  178. /* integration time low */
  179. se401_set_feature(gspca_dev, HV7131_REG_TITL, expose_l);
  180. /* integration time mid */
  181. se401_set_feature(gspca_dev, HV7131_REG_TITM, expose_m);
  182. /* integration time high */
  183. se401_set_feature(gspca_dev, HV7131_REG_TITU, expose_h);
  184. }
  185. static int sd_config(struct gspca_dev *gspca_dev,
  186. const struct usb_device_id *id)
  187. {
  188. struct sd *sd = (struct sd *)gspca_dev;
  189. struct cam *cam = &gspca_dev->cam;
  190. u8 *cd = gspca_dev->usb_buf;
  191. int i, j, n;
  192. int widths[MAX_MODES], heights[MAX_MODES];
  193. /* Read the camera descriptor */
  194. se401_read_req(gspca_dev, SE401_REQ_GET_CAMERA_DESCRIPTOR, 1);
  195. if (gspca_dev->usb_err) {
  196. /* Sometimes after being idle for a while the se401 won't
  197. respond and needs a good kicking */
  198. usb_reset_device(gspca_dev->dev);
  199. gspca_dev->usb_err = 0;
  200. se401_read_req(gspca_dev, SE401_REQ_GET_CAMERA_DESCRIPTOR, 0);
  201. }
  202. /* Some cameras start with their LED on */
  203. se401_write_req(gspca_dev, SE401_REQ_LED_CONTROL, 0, 0);
  204. if (gspca_dev->usb_err)
  205. return gspca_dev->usb_err;
  206. if (cd[1] != 0x41) {
  207. pr_err("Wrong descriptor type\n");
  208. return -ENODEV;
  209. }
  210. if (!(cd[2] & SE401_FORMAT_BAYER)) {
  211. pr_err("Bayer format not supported!\n");
  212. return -ENODEV;
  213. }
  214. if (cd[3])
  215. pr_info("ExtraFeatures: %d\n", cd[3]);
  216. n = cd[4] | (cd[5] << 8);
  217. if (n > MAX_MODES) {
  218. pr_err("Too many frame sizes\n");
  219. return -ENODEV;
  220. }
  221. for (i = 0; i < n ; i++) {
  222. widths[i] = cd[6 + i * 4 + 0] | (cd[6 + i * 4 + 1] << 8);
  223. heights[i] = cd[6 + i * 4 + 2] | (cd[6 + i * 4 + 3] << 8);
  224. }
  225. for (i = 0; i < n ; i++) {
  226. sd->fmts[i].width = widths[i];
  227. sd->fmts[i].height = heights[i];
  228. sd->fmts[i].field = V4L2_FIELD_NONE;
  229. sd->fmts[i].colorspace = V4L2_COLORSPACE_SRGB;
  230. sd->fmts[i].priv = 1;
  231. /* janggu compression only works for 1/4th or 1/16th res */
  232. for (j = 0; j < n; j++) {
  233. if (widths[j] / 2 == widths[i] &&
  234. heights[j] / 2 == heights[i]) {
  235. sd->fmts[i].priv = 2;
  236. break;
  237. }
  238. }
  239. /* 1/16th if available too is better then 1/4th, because
  240. we then use a larger area of the sensor */
  241. for (j = 0; j < n; j++) {
  242. if (widths[j] / 4 == widths[i] &&
  243. heights[j] / 4 == heights[i]) {
  244. sd->fmts[i].priv = 4;
  245. break;
  246. }
  247. }
  248. if (sd->fmts[i].priv == 1) {
  249. /* Not a 1/4th or 1/16th res, use bayer */
  250. sd->fmts[i].pixelformat = V4L2_PIX_FMT_SBGGR8;
  251. sd->fmts[i].bytesperline = widths[i];
  252. sd->fmts[i].sizeimage = widths[i] * heights[i];
  253. pr_info("Frame size: %dx%d bayer\n",
  254. widths[i], heights[i]);
  255. } else {
  256. /* Found a match use janggu compression */
  257. sd->fmts[i].pixelformat = V4L2_PIX_FMT_SE401;
  258. sd->fmts[i].bytesperline = 0;
  259. sd->fmts[i].sizeimage = widths[i] * heights[i] * 3;
  260. pr_info("Frame size: %dx%d 1/%dth janggu\n",
  261. widths[i], heights[i],
  262. sd->fmts[i].priv * sd->fmts[i].priv);
  263. }
  264. }
  265. cam->cam_mode = sd->fmts;
  266. cam->nmodes = n;
  267. cam->bulk = 1;
  268. cam->bulk_size = BULK_SIZE;
  269. cam->bulk_nurbs = 4;
  270. sd->resetlevel = 0x2d; /* Set initial resetlevel */
  271. /* See if the camera supports brightness */
  272. se401_read_req(gspca_dev, SE401_REQ_GET_BRT, 1);
  273. sd->has_brightness = !!gspca_dev->usb_err;
  274. gspca_dev->usb_err = 0;
  275. return 0;
  276. }
  277. /* this function is called at probe and resume time */
  278. static int sd_init(struct gspca_dev *gspca_dev)
  279. {
  280. return 0;
  281. }
  282. /* function called at start time before URB creation */
  283. static int sd_isoc_init(struct gspca_dev *gspca_dev)
  284. {
  285. gspca_dev->alt = 1; /* Ignore the bogus isoc alt settings */
  286. return gspca_dev->usb_err;
  287. }
  288. /* -- start the camera -- */
  289. static int sd_start(struct gspca_dev *gspca_dev)
  290. {
  291. struct sd *sd = (struct sd *)gspca_dev;
  292. int mult = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
  293. int mode = 0;
  294. se401_write_req(gspca_dev, SE401_REQ_CAMERA_POWER, 1, 1);
  295. if (gspca_dev->usb_err) {
  296. /* Sometimes after being idle for a while the se401 won't
  297. respond and needs a good kicking */
  298. usb_reset_device(gspca_dev->dev);
  299. gspca_dev->usb_err = 0;
  300. se401_write_req(gspca_dev, SE401_REQ_CAMERA_POWER, 1, 0);
  301. }
  302. se401_write_req(gspca_dev, SE401_REQ_LED_CONTROL, 1, 0);
  303. se401_set_feature(gspca_dev, HV7131_REG_MODE_B, 0x05);
  304. /* set size + mode */
  305. se401_write_req(gspca_dev, SE401_REQ_SET_WIDTH,
  306. gspca_dev->pixfmt.width * mult, 0);
  307. se401_write_req(gspca_dev, SE401_REQ_SET_HEIGHT,
  308. gspca_dev->pixfmt.height * mult, 0);
  309. /*
  310. * HDG: disabled this as it does not seem to do anything
  311. * se401_write_req(gspca_dev, SE401_REQ_SET_OUTPUT_MODE,
  312. * SE401_FORMAT_BAYER, 0);
  313. */
  314. switch (mult) {
  315. case 1: /* Raw bayer */
  316. mode = 0x03; break;
  317. case 2: /* 1/4th janggu */
  318. mode = SE401_QUANT_FACT << 4; break;
  319. case 4: /* 1/16th janggu */
  320. mode = (SE401_QUANT_FACT << 4) | 0x02; break;
  321. }
  322. se401_set_feature(gspca_dev, SE401_OPERATINGMODE, mode);
  323. se401_set_feature(gspca_dev, HV7131_REG_ARLV, sd->resetlevel);
  324. sd->packet_read = 0;
  325. sd->pixels_read = 0;
  326. sd->restart_stream = 0;
  327. sd->resetlevel_frame_count = 0;
  328. sd->resetlevel_adjust_dir = 0;
  329. sd->expo_change_state = EXPO_NO_CHANGE;
  330. se401_write_req(gspca_dev, SE401_REQ_START_CONTINUOUS_CAPTURE, 0, 0);
  331. return gspca_dev->usb_err;
  332. }
  333. static void sd_stopN(struct gspca_dev *gspca_dev)
  334. {
  335. se401_write_req(gspca_dev, SE401_REQ_STOP_CONTINUOUS_CAPTURE, 0, 0);
  336. se401_write_req(gspca_dev, SE401_REQ_LED_CONTROL, 0, 0);
  337. se401_write_req(gspca_dev, SE401_REQ_CAMERA_POWER, 0, 0);
  338. }
  339. static void sd_dq_callback(struct gspca_dev *gspca_dev)
  340. {
  341. struct sd *sd = (struct sd *)gspca_dev;
  342. unsigned int ahrc, alrc;
  343. int oldreset, adjust_dir;
  344. /* Restart the stream if requested do so by pkt_scan */
  345. if (sd->restart_stream) {
  346. sd_stopN(gspca_dev);
  347. sd_start(gspca_dev);
  348. sd->restart_stream = 0;
  349. }
  350. /* Automatically adjust sensor reset level
  351. Hyundai have some really nice docs about this and other sensor
  352. related stuff on their homepage: www.hei.co.kr */
  353. sd->resetlevel_frame_count++;
  354. if (sd->resetlevel_frame_count < 20)
  355. return;
  356. /* For some reason this normally read-only register doesn't get reset
  357. to zero after reading them just once... */
  358. se401_get_feature(gspca_dev, HV7131_REG_HIREFNOH);
  359. se401_get_feature(gspca_dev, HV7131_REG_HIREFNOL);
  360. se401_get_feature(gspca_dev, HV7131_REG_LOREFNOH);
  361. se401_get_feature(gspca_dev, HV7131_REG_LOREFNOL);
  362. ahrc = 256*se401_get_feature(gspca_dev, HV7131_REG_HIREFNOH) +
  363. se401_get_feature(gspca_dev, HV7131_REG_HIREFNOL);
  364. alrc = 256*se401_get_feature(gspca_dev, HV7131_REG_LOREFNOH) +
  365. se401_get_feature(gspca_dev, HV7131_REG_LOREFNOL);
  366. /* Not an exact science, but it seems to work pretty well... */
  367. oldreset = sd->resetlevel;
  368. if (alrc > 10) {
  369. while (alrc >= 10 && sd->resetlevel < 63) {
  370. sd->resetlevel++;
  371. alrc /= 2;
  372. }
  373. } else if (ahrc > 20) {
  374. while (ahrc >= 20 && sd->resetlevel > 0) {
  375. sd->resetlevel--;
  376. ahrc /= 2;
  377. }
  378. }
  379. /* Detect ping-pong-ing and halve adjustment to avoid overshoot */
  380. if (sd->resetlevel > oldreset)
  381. adjust_dir = 1;
  382. else
  383. adjust_dir = -1;
  384. if (sd->resetlevel_adjust_dir &&
  385. sd->resetlevel_adjust_dir != adjust_dir)
  386. sd->resetlevel = oldreset + (sd->resetlevel - oldreset) / 2;
  387. if (sd->resetlevel != oldreset) {
  388. sd->resetlevel_adjust_dir = adjust_dir;
  389. se401_set_feature(gspca_dev, HV7131_REG_ARLV, sd->resetlevel);
  390. }
  391. sd->resetlevel_frame_count = 0;
  392. }
  393. static void sd_complete_frame(struct gspca_dev *gspca_dev, u8 *data, int len)
  394. {
  395. struct sd *sd = (struct sd *)gspca_dev;
  396. switch (sd->expo_change_state) {
  397. case EXPO_CHANGED:
  398. /* The exposure was changed while this frame
  399. was being send, so this frame is ok */
  400. sd->expo_change_state = EXPO_DROP_FRAME;
  401. break;
  402. case EXPO_DROP_FRAME:
  403. /* The exposure was changed while this frame
  404. was being captured, drop it! */
  405. gspca_dev->last_packet_type = DISCARD_PACKET;
  406. sd->expo_change_state = EXPO_NO_CHANGE;
  407. break;
  408. case EXPO_NO_CHANGE:
  409. break;
  410. }
  411. gspca_frame_add(gspca_dev, LAST_PACKET, data, len);
  412. }
  413. static void sd_pkt_scan_janggu(struct gspca_dev *gspca_dev, u8 *data, int len)
  414. {
  415. struct sd *sd = (struct sd *)gspca_dev;
  416. int imagesize = gspca_dev->pixfmt.width * gspca_dev->pixfmt.height;
  417. int i, plen, bits, pixels, info, count;
  418. if (sd->restart_stream)
  419. return;
  420. /* Sometimes a 1024 bytes garbage bulk packet is send between frames */
  421. if (gspca_dev->last_packet_type == LAST_PACKET && len == 1024) {
  422. gspca_dev->last_packet_type = DISCARD_PACKET;
  423. return;
  424. }
  425. i = 0;
  426. while (i < len) {
  427. /* Read header if not already be present from prev bulk pkt */
  428. if (sd->packet_read < 4) {
  429. count = 4 - sd->packet_read;
  430. if (count > len - i)
  431. count = len - i;
  432. memcpy(&sd->packet[sd->packet_read], &data[i], count);
  433. sd->packet_read += count;
  434. i += count;
  435. if (sd->packet_read < 4)
  436. break;
  437. }
  438. bits = sd->packet[3] + (sd->packet[2] << 8);
  439. pixels = sd->packet[1] + ((sd->packet[0] & 0x3f) << 8);
  440. info = (sd->packet[0] & 0xc0) >> 6;
  441. plen = ((bits + 47) >> 4) << 1;
  442. /* Sanity checks */
  443. if (plen > 1024) {
  444. pr_err("invalid packet len %d restarting stream\n",
  445. plen);
  446. goto error;
  447. }
  448. if (info == 3) {
  449. pr_err("unknown frame info value restarting stream\n");
  450. goto error;
  451. }
  452. /* Read (remainder of) packet contents */
  453. count = plen - sd->packet_read;
  454. if (count > len - i)
  455. count = len - i;
  456. memcpy(&sd->packet[sd->packet_read], &data[i], count);
  457. sd->packet_read += count;
  458. i += count;
  459. if (sd->packet_read < plen)
  460. break;
  461. sd->pixels_read += pixels;
  462. sd->packet_read = 0;
  463. switch (info) {
  464. case 0: /* Frame data */
  465. gspca_frame_add(gspca_dev, INTER_PACKET, sd->packet,
  466. plen);
  467. break;
  468. case 1: /* EOF */
  469. if (sd->pixels_read != imagesize) {
  470. pr_err("frame size %d expected %d\n",
  471. sd->pixels_read, imagesize);
  472. goto error;
  473. }
  474. sd_complete_frame(gspca_dev, sd->packet, plen);
  475. return; /* Discard the rest of the bulk packet !! */
  476. case 2: /* SOF */
  477. gspca_frame_add(gspca_dev, FIRST_PACKET, sd->packet,
  478. plen);
  479. sd->pixels_read = pixels;
  480. break;
  481. }
  482. }
  483. return;
  484. error:
  485. sd->restart_stream = 1;
  486. /* Give userspace a 0 bytes frame, so our dq callback gets
  487. called and it can restart the stream */
  488. gspca_frame_add(gspca_dev, FIRST_PACKET, NULL, 0);
  489. gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0);
  490. }
  491. static void sd_pkt_scan_bayer(struct gspca_dev *gspca_dev, u8 *data, int len)
  492. {
  493. struct cam *cam = &gspca_dev->cam;
  494. int imagesize = cam->cam_mode[gspca_dev->curr_mode].sizeimage;
  495. if (gspca_dev->image_len == 0) {
  496. gspca_frame_add(gspca_dev, FIRST_PACKET, data, len);
  497. return;
  498. }
  499. if (gspca_dev->image_len + len >= imagesize) {
  500. sd_complete_frame(gspca_dev, data, len);
  501. return;
  502. }
  503. gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
  504. }
  505. static void sd_pkt_scan(struct gspca_dev *gspca_dev, u8 *data, int len)
  506. {
  507. int mult = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
  508. if (len == 0)
  509. return;
  510. if (mult == 1) /* mult == 1 means raw bayer */
  511. sd_pkt_scan_bayer(gspca_dev, data, len);
  512. else
  513. sd_pkt_scan_janggu(gspca_dev, data, len);
  514. }
  515. #if IS_ENABLED(CONFIG_INPUT)
  516. static int sd_int_pkt_scan(struct gspca_dev *gspca_dev, u8 *data, int len)
  517. {
  518. struct sd *sd = (struct sd *)gspca_dev;
  519. u8 state;
  520. if (len != 2)
  521. return -EINVAL;
  522. switch (data[0]) {
  523. case 0:
  524. case 1:
  525. state = data[0];
  526. break;
  527. default:
  528. return -EINVAL;
  529. }
  530. if (sd->button_state != state) {
  531. input_report_key(gspca_dev->input_dev, KEY_CAMERA, state);
  532. input_sync(gspca_dev->input_dev);
  533. sd->button_state = state;
  534. }
  535. return 0;
  536. }
  537. #endif
  538. static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
  539. {
  540. struct gspca_dev *gspca_dev =
  541. container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
  542. struct sd *sd = (struct sd *)gspca_dev;
  543. gspca_dev->usb_err = 0;
  544. if (!gspca_dev->streaming)
  545. return 0;
  546. switch (ctrl->id) {
  547. case V4L2_CID_BRIGHTNESS:
  548. setbrightness(gspca_dev, ctrl->val);
  549. break;
  550. case V4L2_CID_GAIN:
  551. setgain(gspca_dev, ctrl->val);
  552. break;
  553. case V4L2_CID_EXPOSURE:
  554. setexposure(gspca_dev, ctrl->val, sd->freq->val);
  555. break;
  556. }
  557. return gspca_dev->usb_err;
  558. }
  559. static const struct v4l2_ctrl_ops sd_ctrl_ops = {
  560. .s_ctrl = sd_s_ctrl,
  561. };
  562. static int sd_init_controls(struct gspca_dev *gspca_dev)
  563. {
  564. struct sd *sd = (struct sd *)gspca_dev;
  565. struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
  566. gspca_dev->vdev.ctrl_handler = hdl;
  567. v4l2_ctrl_handler_init(hdl, 4);
  568. if (sd->has_brightness)
  569. v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  570. V4L2_CID_BRIGHTNESS, 0, 255, 1, 15);
  571. /* max is really 63 but > 50 is not pretty */
  572. v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  573. V4L2_CID_GAIN, 0, 50, 1, 25);
  574. sd->exposure = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  575. V4L2_CID_EXPOSURE, 0, 32767, 1, 15000);
  576. sd->freq = v4l2_ctrl_new_std_menu(hdl, &sd_ctrl_ops,
  577. V4L2_CID_POWER_LINE_FREQUENCY,
  578. V4L2_CID_POWER_LINE_FREQUENCY_60HZ, 0, 0);
  579. if (hdl->error) {
  580. pr_err("Could not initialize controls\n");
  581. return hdl->error;
  582. }
  583. v4l2_ctrl_cluster(2, &sd->exposure);
  584. return 0;
  585. }
  586. /* sub-driver description */
  587. static const struct sd_desc sd_desc = {
  588. .name = MODULE_NAME,
  589. .config = sd_config,
  590. .init = sd_init,
  591. .init_controls = sd_init_controls,
  592. .isoc_init = sd_isoc_init,
  593. .start = sd_start,
  594. .stopN = sd_stopN,
  595. .dq_callback = sd_dq_callback,
  596. .pkt_scan = sd_pkt_scan,
  597. #if IS_ENABLED(CONFIG_INPUT)
  598. .int_pkt_scan = sd_int_pkt_scan,
  599. #endif
  600. };
  601. /* -- module initialisation -- */
  602. static const struct usb_device_id device_table[] = {
  603. {USB_DEVICE(0x03e8, 0x0004)}, /* Endpoints/Aox SE401 */
  604. {USB_DEVICE(0x0471, 0x030b)}, /* Philips PCVC665K */
  605. {USB_DEVICE(0x047d, 0x5001)}, /* Kensington 67014 */
  606. {USB_DEVICE(0x047d, 0x5002)}, /* Kensington 6701(5/7) */
  607. {USB_DEVICE(0x047d, 0x5003)}, /* Kensington 67016 */
  608. {}
  609. };
  610. MODULE_DEVICE_TABLE(usb, device_table);
  611. /* -- device connect -- */
  612. static int sd_probe(struct usb_interface *intf,
  613. const struct usb_device_id *id)
  614. {
  615. return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
  616. THIS_MODULE);
  617. }
  618. static int sd_pre_reset(struct usb_interface *intf)
  619. {
  620. return 0;
  621. }
  622. static int sd_post_reset(struct usb_interface *intf)
  623. {
  624. return 0;
  625. }
  626. static struct usb_driver sd_driver = {
  627. .name = MODULE_NAME,
  628. .id_table = device_table,
  629. .probe = sd_probe,
  630. .disconnect = gspca_disconnect,
  631. #ifdef CONFIG_PM
  632. .suspend = gspca_suspend,
  633. .resume = gspca_resume,
  634. .reset_resume = gspca_resume,
  635. #endif
  636. .pre_reset = sd_pre_reset,
  637. .post_reset = sd_post_reset,
  638. };
  639. module_usb_driver(sd_driver);