se401.c 20 KB

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