mr97310a.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263
  1. /*
  2. * Mars MR97310A library
  3. *
  4. * The original mr97310a driver, which supported the Aiptek Pencam VGA+, is
  5. * Copyright (C) 2009 Kyle Guinn <elyk03@gmail.com>
  6. *
  7. * Support for the MR97310A cameras in addition to the Aiptek Pencam VGA+
  8. * and for the routines for detecting and classifying these various cameras,
  9. * is Copyright (C) 2009 Theodore Kilgore <kilgota@auburn.edu>
  10. *
  11. * Support for the control settings for the CIF cameras is
  12. * Copyright (C) 2009 Hans de Goede <hdegoede@redhat.com> and
  13. * Thomas Kaiser <thomas@kaiser-linux.li>
  14. *
  15. * Support for the control settings for the VGA cameras is
  16. * Copyright (C) 2009 Theodore Kilgore <kilgota@auburn.edu>
  17. *
  18. * Several previously unsupported cameras are owned and have been tested by
  19. * Hans de Goede <hdegoede@redhat.com> and
  20. * Thomas Kaiser <thomas@kaiser-linux.li> and
  21. * Theodore Kilgore <kilgota@auburn.edu> and
  22. * Edmond Rodriguez <erodrig_97@yahoo.com> and
  23. * Aurelien Jacobs <aurel@gnuage.org>
  24. *
  25. * The MR97311A support in gspca/mars.c has been helpful in understanding some
  26. * of the registers in these cameras.
  27. *
  28. * This program is free software; you can redistribute it and/or modify
  29. * it under the terms of the GNU General Public License as published by
  30. * the Free Software Foundation; either version 2 of the License, or
  31. * any later version.
  32. *
  33. * This program is distributed in the hope that it will be useful,
  34. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  35. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  36. * GNU General Public License for more details.
  37. *
  38. * You should have received a copy of the GNU General Public License
  39. * along with this program; if not, write to the Free Software
  40. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  41. */
  42. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  43. #define MODULE_NAME "mr97310a"
  44. #include "gspca.h"
  45. #define CAM_TYPE_CIF 0
  46. #define CAM_TYPE_VGA 1
  47. #define MR97310A_BRIGHTNESS_DEFAULT 0
  48. #define MR97310A_EXPOSURE_MIN 0
  49. #define MR97310A_EXPOSURE_MAX 4095
  50. #define MR97310A_EXPOSURE_DEFAULT 1000
  51. #define MR97310A_GAIN_MIN 0
  52. #define MR97310A_GAIN_MAX 31
  53. #define MR97310A_GAIN_DEFAULT 25
  54. #define MR97310A_CONTRAST_MIN 0
  55. #define MR97310A_CONTRAST_MAX 31
  56. #define MR97310A_CONTRAST_DEFAULT 23
  57. #define MR97310A_CS_GAIN_MIN 0
  58. #define MR97310A_CS_GAIN_MAX 0x7ff
  59. #define MR97310A_CS_GAIN_DEFAULT 0x110
  60. #define MR97310A_MIN_CLOCKDIV_MIN 3
  61. #define MR97310A_MIN_CLOCKDIV_MAX 8
  62. #define MR97310A_MIN_CLOCKDIV_DEFAULT 3
  63. MODULE_AUTHOR("Kyle Guinn <elyk03@gmail.com>,"
  64. "Theodore Kilgore <kilgota@auburn.edu>");
  65. MODULE_DESCRIPTION("GSPCA/Mars-Semi MR97310A USB Camera Driver");
  66. MODULE_LICENSE("GPL");
  67. /* global parameters */
  68. static int force_sensor_type = -1;
  69. module_param(force_sensor_type, int, 0644);
  70. MODULE_PARM_DESC(force_sensor_type, "Force sensor type (-1 (auto), 0 or 1)");
  71. /* specific webcam descriptor */
  72. struct sd {
  73. struct gspca_dev gspca_dev; /* !! must be the first item */
  74. u8 sof_read;
  75. u8 cam_type; /* 0 is CIF and 1 is VGA */
  76. u8 sensor_type; /* We use 0 and 1 here, too. */
  77. u8 do_lcd_stop;
  78. u8 adj_colors;
  79. int brightness;
  80. u16 exposure;
  81. u32 gain;
  82. u8 contrast;
  83. u8 min_clockdiv;
  84. };
  85. struct sensor_w_data {
  86. u8 reg;
  87. u8 flags;
  88. u8 data[16];
  89. int len;
  90. };
  91. static void sd_stopN(struct gspca_dev *gspca_dev);
  92. static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
  93. static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
  94. static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val);
  95. static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val);
  96. static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val);
  97. static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val);
  98. static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val);
  99. static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val);
  100. static int sd_setmin_clockdiv(struct gspca_dev *gspca_dev, __s32 val);
  101. static int sd_getmin_clockdiv(struct gspca_dev *gspca_dev, __s32 *val);
  102. static void setbrightness(struct gspca_dev *gspca_dev);
  103. static void setexposure(struct gspca_dev *gspca_dev);
  104. static void setgain(struct gspca_dev *gspca_dev);
  105. static void setcontrast(struct gspca_dev *gspca_dev);
  106. /* V4L2 controls supported by the driver */
  107. static const struct ctrl sd_ctrls[] = {
  108. /* Separate brightness control description for Argus QuickClix as it has
  109. * different limits from the other mr97310a cameras, and separate gain
  110. * control for Sakar CyberPix camera. */
  111. {
  112. #define NORM_BRIGHTNESS_IDX 0
  113. {
  114. .id = V4L2_CID_BRIGHTNESS,
  115. .type = V4L2_CTRL_TYPE_INTEGER,
  116. .name = "Brightness",
  117. .minimum = -254,
  118. .maximum = 255,
  119. .step = 1,
  120. .default_value = MR97310A_BRIGHTNESS_DEFAULT,
  121. .flags = 0,
  122. },
  123. .set = sd_setbrightness,
  124. .get = sd_getbrightness,
  125. },
  126. {
  127. #define ARGUS_QC_BRIGHTNESS_IDX 1
  128. {
  129. .id = V4L2_CID_BRIGHTNESS,
  130. .type = V4L2_CTRL_TYPE_INTEGER,
  131. .name = "Brightness",
  132. .minimum = 0,
  133. .maximum = 15,
  134. .step = 1,
  135. .default_value = MR97310A_BRIGHTNESS_DEFAULT,
  136. .flags = 0,
  137. },
  138. .set = sd_setbrightness,
  139. .get = sd_getbrightness,
  140. },
  141. {
  142. #define EXPOSURE_IDX 2
  143. {
  144. .id = V4L2_CID_EXPOSURE,
  145. .type = V4L2_CTRL_TYPE_INTEGER,
  146. .name = "Exposure",
  147. .minimum = MR97310A_EXPOSURE_MIN,
  148. .maximum = MR97310A_EXPOSURE_MAX,
  149. .step = 1,
  150. .default_value = MR97310A_EXPOSURE_DEFAULT,
  151. .flags = 0,
  152. },
  153. .set = sd_setexposure,
  154. .get = sd_getexposure,
  155. },
  156. {
  157. #define GAIN_IDX 3
  158. {
  159. .id = V4L2_CID_GAIN,
  160. .type = V4L2_CTRL_TYPE_INTEGER,
  161. .name = "Gain",
  162. .minimum = MR97310A_GAIN_MIN,
  163. .maximum = MR97310A_GAIN_MAX,
  164. .step = 1,
  165. .default_value = MR97310A_GAIN_DEFAULT,
  166. .flags = 0,
  167. },
  168. .set = sd_setgain,
  169. .get = sd_getgain,
  170. },
  171. {
  172. #define SAKAR_CS_GAIN_IDX 4
  173. {
  174. .id = V4L2_CID_GAIN,
  175. .type = V4L2_CTRL_TYPE_INTEGER,
  176. .name = "Gain",
  177. .minimum = MR97310A_CS_GAIN_MIN,
  178. .maximum = MR97310A_CS_GAIN_MAX,
  179. .step = 1,
  180. .default_value = MR97310A_CS_GAIN_DEFAULT,
  181. .flags = 0,
  182. },
  183. .set = sd_setgain,
  184. .get = sd_getgain,
  185. },
  186. {
  187. #define CONTRAST_IDX 5
  188. {
  189. .id = V4L2_CID_CONTRAST,
  190. .type = V4L2_CTRL_TYPE_INTEGER,
  191. .name = "Contrast",
  192. .minimum = MR97310A_CONTRAST_MIN,
  193. .maximum = MR97310A_CONTRAST_MAX,
  194. .step = 1,
  195. .default_value = MR97310A_CONTRAST_DEFAULT,
  196. .flags = 0,
  197. },
  198. .set = sd_setcontrast,
  199. .get = sd_getcontrast,
  200. },
  201. {
  202. #define MIN_CLOCKDIV_IDX 6
  203. {
  204. .id = V4L2_CID_PRIVATE_BASE,
  205. .type = V4L2_CTRL_TYPE_INTEGER,
  206. .name = "Minimum Clock Divider",
  207. .minimum = MR97310A_MIN_CLOCKDIV_MIN,
  208. .maximum = MR97310A_MIN_CLOCKDIV_MAX,
  209. .step = 1,
  210. .default_value = MR97310A_MIN_CLOCKDIV_DEFAULT,
  211. .flags = 0,
  212. },
  213. .set = sd_setmin_clockdiv,
  214. .get = sd_getmin_clockdiv,
  215. },
  216. };
  217. static const struct v4l2_pix_format vga_mode[] = {
  218. {160, 120, V4L2_PIX_FMT_MR97310A, V4L2_FIELD_NONE,
  219. .bytesperline = 160,
  220. .sizeimage = 160 * 120,
  221. .colorspace = V4L2_COLORSPACE_SRGB,
  222. .priv = 4},
  223. {176, 144, V4L2_PIX_FMT_MR97310A, V4L2_FIELD_NONE,
  224. .bytesperline = 176,
  225. .sizeimage = 176 * 144,
  226. .colorspace = V4L2_COLORSPACE_SRGB,
  227. .priv = 3},
  228. {320, 240, V4L2_PIX_FMT_MR97310A, V4L2_FIELD_NONE,
  229. .bytesperline = 320,
  230. .sizeimage = 320 * 240,
  231. .colorspace = V4L2_COLORSPACE_SRGB,
  232. .priv = 2},
  233. {352, 288, V4L2_PIX_FMT_MR97310A, V4L2_FIELD_NONE,
  234. .bytesperline = 352,
  235. .sizeimage = 352 * 288,
  236. .colorspace = V4L2_COLORSPACE_SRGB,
  237. .priv = 1},
  238. {640, 480, V4L2_PIX_FMT_MR97310A, V4L2_FIELD_NONE,
  239. .bytesperline = 640,
  240. .sizeimage = 640 * 480,
  241. .colorspace = V4L2_COLORSPACE_SRGB,
  242. .priv = 0},
  243. };
  244. /* the bytes to write are in gspca_dev->usb_buf */
  245. static int mr_write(struct gspca_dev *gspca_dev, int len)
  246. {
  247. int rc;
  248. rc = usb_bulk_msg(gspca_dev->dev,
  249. usb_sndbulkpipe(gspca_dev->dev, 4),
  250. gspca_dev->usb_buf, len, NULL, 500);
  251. if (rc < 0)
  252. pr_err("reg write [%02x] error %d\n",
  253. gspca_dev->usb_buf[0], rc);
  254. return rc;
  255. }
  256. /* the bytes are read into gspca_dev->usb_buf */
  257. static int mr_read(struct gspca_dev *gspca_dev, int len)
  258. {
  259. int rc;
  260. rc = usb_bulk_msg(gspca_dev->dev,
  261. usb_rcvbulkpipe(gspca_dev->dev, 3),
  262. gspca_dev->usb_buf, len, NULL, 500);
  263. if (rc < 0)
  264. pr_err("reg read [%02x] error %d\n",
  265. gspca_dev->usb_buf[0], rc);
  266. return rc;
  267. }
  268. static int sensor_write_reg(struct gspca_dev *gspca_dev, u8 reg, u8 flags,
  269. const u8 *data, int len)
  270. {
  271. gspca_dev->usb_buf[0] = 0x1f;
  272. gspca_dev->usb_buf[1] = flags;
  273. gspca_dev->usb_buf[2] = reg;
  274. memcpy(gspca_dev->usb_buf + 3, data, len);
  275. return mr_write(gspca_dev, len + 3);
  276. }
  277. static int sensor_write_regs(struct gspca_dev *gspca_dev,
  278. const struct sensor_w_data *data, int len)
  279. {
  280. int i, rc;
  281. for (i = 0; i < len; i++) {
  282. rc = sensor_write_reg(gspca_dev, data[i].reg, data[i].flags,
  283. data[i].data, data[i].len);
  284. if (rc < 0)
  285. return rc;
  286. }
  287. return 0;
  288. }
  289. static int sensor_write1(struct gspca_dev *gspca_dev, u8 reg, u8 data)
  290. {
  291. struct sd *sd = (struct sd *) gspca_dev;
  292. u8 buf, confirm_reg;
  293. int rc;
  294. buf = data;
  295. if (sd->cam_type == CAM_TYPE_CIF) {
  296. rc = sensor_write_reg(gspca_dev, reg, 0x01, &buf, 1);
  297. confirm_reg = sd->sensor_type ? 0x13 : 0x11;
  298. } else {
  299. rc = sensor_write_reg(gspca_dev, reg, 0x00, &buf, 1);
  300. confirm_reg = 0x11;
  301. }
  302. if (rc < 0)
  303. return rc;
  304. buf = 0x01;
  305. rc = sensor_write_reg(gspca_dev, confirm_reg, 0x00, &buf, 1);
  306. if (rc < 0)
  307. return rc;
  308. return 0;
  309. }
  310. static int cam_get_response16(struct gspca_dev *gspca_dev, u8 reg, int verbose)
  311. {
  312. int err_code;
  313. gspca_dev->usb_buf[0] = reg;
  314. err_code = mr_write(gspca_dev, 1);
  315. if (err_code < 0)
  316. return err_code;
  317. err_code = mr_read(gspca_dev, 16);
  318. if (err_code < 0)
  319. return err_code;
  320. if (verbose)
  321. PDEBUG(D_PROBE, "Register: %02x reads %02x%02x%02x", reg,
  322. gspca_dev->usb_buf[0],
  323. gspca_dev->usb_buf[1],
  324. gspca_dev->usb_buf[2]);
  325. return 0;
  326. }
  327. static int zero_the_pointer(struct gspca_dev *gspca_dev)
  328. {
  329. __u8 *data = gspca_dev->usb_buf;
  330. int err_code;
  331. u8 status = 0;
  332. int tries = 0;
  333. err_code = cam_get_response16(gspca_dev, 0x21, 0);
  334. if (err_code < 0)
  335. return err_code;
  336. data[0] = 0x19;
  337. data[1] = 0x51;
  338. err_code = mr_write(gspca_dev, 2);
  339. if (err_code < 0)
  340. return err_code;
  341. err_code = cam_get_response16(gspca_dev, 0x21, 0);
  342. if (err_code < 0)
  343. return err_code;
  344. data[0] = 0x19;
  345. data[1] = 0xba;
  346. err_code = mr_write(gspca_dev, 2);
  347. if (err_code < 0)
  348. return err_code;
  349. err_code = cam_get_response16(gspca_dev, 0x21, 0);
  350. if (err_code < 0)
  351. return err_code;
  352. data[0] = 0x19;
  353. data[1] = 0x00;
  354. err_code = mr_write(gspca_dev, 2);
  355. if (err_code < 0)
  356. return err_code;
  357. err_code = cam_get_response16(gspca_dev, 0x21, 0);
  358. if (err_code < 0)
  359. return err_code;
  360. data[0] = 0x19;
  361. data[1] = 0x00;
  362. err_code = mr_write(gspca_dev, 2);
  363. if (err_code < 0)
  364. return err_code;
  365. while (status != 0x0a && tries < 256) {
  366. err_code = cam_get_response16(gspca_dev, 0x21, 0);
  367. status = data[0];
  368. tries++;
  369. if (err_code < 0)
  370. return err_code;
  371. }
  372. if (status != 0x0a)
  373. PDEBUG(D_ERR, "status is %02x", status);
  374. tries = 0;
  375. while (tries < 4) {
  376. data[0] = 0x19;
  377. data[1] = 0x00;
  378. err_code = mr_write(gspca_dev, 2);
  379. if (err_code < 0)
  380. return err_code;
  381. err_code = cam_get_response16(gspca_dev, 0x21, 0);
  382. status = data[0];
  383. tries++;
  384. if (err_code < 0)
  385. return err_code;
  386. }
  387. data[0] = 0x19;
  388. err_code = mr_write(gspca_dev, 1);
  389. if (err_code < 0)
  390. return err_code;
  391. err_code = mr_read(gspca_dev, 16);
  392. if (err_code < 0)
  393. return err_code;
  394. return 0;
  395. }
  396. static int stream_start(struct gspca_dev *gspca_dev)
  397. {
  398. gspca_dev->usb_buf[0] = 0x01;
  399. gspca_dev->usb_buf[1] = 0x01;
  400. return mr_write(gspca_dev, 2);
  401. }
  402. static void stream_stop(struct gspca_dev *gspca_dev)
  403. {
  404. gspca_dev->usb_buf[0] = 0x01;
  405. gspca_dev->usb_buf[1] = 0x00;
  406. if (mr_write(gspca_dev, 2) < 0)
  407. PDEBUG(D_ERR, "Stream Stop failed");
  408. }
  409. static void lcd_stop(struct gspca_dev *gspca_dev)
  410. {
  411. gspca_dev->usb_buf[0] = 0x19;
  412. gspca_dev->usb_buf[1] = 0x54;
  413. if (mr_write(gspca_dev, 2) < 0)
  414. PDEBUG(D_ERR, "LCD Stop failed");
  415. }
  416. static int isoc_enable(struct gspca_dev *gspca_dev)
  417. {
  418. gspca_dev->usb_buf[0] = 0x00;
  419. gspca_dev->usb_buf[1] = 0x4d; /* ISOC transferring enable... */
  420. return mr_write(gspca_dev, 2);
  421. }
  422. /* This function is called at probe time */
  423. static int sd_config(struct gspca_dev *gspca_dev,
  424. const struct usb_device_id *id)
  425. {
  426. struct sd *sd = (struct sd *) gspca_dev;
  427. struct cam *cam;
  428. int gain_default = MR97310A_GAIN_DEFAULT;
  429. int err_code;
  430. cam = &gspca_dev->cam;
  431. cam->cam_mode = vga_mode;
  432. cam->nmodes = ARRAY_SIZE(vga_mode);
  433. sd->do_lcd_stop = 0;
  434. /* Several of the supported CIF cameras share the same USB ID but
  435. * require different initializations and different control settings.
  436. * The same is true of the VGA cameras. Therefore, we are forced
  437. * to start the initialization process in order to determine which
  438. * camera is present. Some of the supported cameras require the
  439. * memory pointer to be set to 0 as the very first item of business
  440. * or else they will not stream. So we do that immediately.
  441. */
  442. err_code = zero_the_pointer(gspca_dev);
  443. if (err_code < 0)
  444. return err_code;
  445. err_code = stream_start(gspca_dev);
  446. if (err_code < 0)
  447. return err_code;
  448. /* Now, the query for sensor type. */
  449. err_code = cam_get_response16(gspca_dev, 0x07, 1);
  450. if (err_code < 0)
  451. return err_code;
  452. if (id->idProduct == 0x0110 || id->idProduct == 0x010e) {
  453. sd->cam_type = CAM_TYPE_CIF;
  454. cam->nmodes--;
  455. /*
  456. * All but one of the known CIF cameras share the same USB ID,
  457. * but two different init routines are in use, and the control
  458. * settings are different, too. We need to detect which camera
  459. * of the two known varieties is connected!
  460. *
  461. * A list of known CIF cameras follows. They all report either
  462. * 0200 for type 0 or 0300 for type 1.
  463. * If you have another to report, please do
  464. *
  465. * Name sd->sensor_type reported by
  466. *
  467. * Sakar 56379 Spy-shot 0 T. Kilgore
  468. * Innovage 0 T. Kilgore
  469. * Vivitar Mini 0 H. De Goede
  470. * Vivitar Mini 0 E. Rodriguez
  471. * Vivitar Mini 1 T. Kilgore
  472. * Elta-Media 8212dc 1 T. Kaiser
  473. * Philips dig. keych. 1 T. Kilgore
  474. * Trust Spyc@m 100 1 A. Jacobs
  475. */
  476. switch (gspca_dev->usb_buf[0]) {
  477. case 2:
  478. sd->sensor_type = 0;
  479. break;
  480. case 3:
  481. sd->sensor_type = 1;
  482. break;
  483. default:
  484. pr_err("Unknown CIF Sensor id : %02x\n",
  485. gspca_dev->usb_buf[1]);
  486. return -ENODEV;
  487. }
  488. PDEBUG(D_PROBE, "MR97310A CIF camera detected, sensor: %d",
  489. sd->sensor_type);
  490. } else {
  491. sd->cam_type = CAM_TYPE_VGA;
  492. /*
  493. * Here is a table of the responses to the query for sensor
  494. * type, from the known MR97310A VGA cameras. Six different
  495. * cameras of which five share the same USB ID.
  496. *
  497. * Name gspca_dev->usb_buf[] sd->sensor_type
  498. * sd->do_lcd_stop
  499. * Aiptek Pencam VGA+ 0300 0 1
  500. * ION digital 0300 0 1
  501. * Argus DC-1620 0450 1 0
  502. * Argus QuickClix 0420 1 1
  503. * Sakar 77379 Digital 0350 0 1
  504. * Sakar 1638x CyberPix 0120 0 2
  505. *
  506. * Based upon these results, we assume default settings
  507. * and then correct as necessary, as follows.
  508. *
  509. */
  510. sd->sensor_type = 1;
  511. sd->do_lcd_stop = 0;
  512. sd->adj_colors = 0;
  513. if (gspca_dev->usb_buf[0] == 0x01) {
  514. sd->sensor_type = 2;
  515. } else if ((gspca_dev->usb_buf[0] != 0x03) &&
  516. (gspca_dev->usb_buf[0] != 0x04)) {
  517. pr_err("Unknown VGA Sensor id Byte 0: %02x\n",
  518. gspca_dev->usb_buf[0]);
  519. pr_err("Defaults assumed, may not work\n");
  520. pr_err("Please report this\n");
  521. }
  522. /* Sakar Digital color needs to be adjusted. */
  523. if ((gspca_dev->usb_buf[0] == 0x03) &&
  524. (gspca_dev->usb_buf[1] == 0x50))
  525. sd->adj_colors = 1;
  526. if (gspca_dev->usb_buf[0] == 0x04) {
  527. sd->do_lcd_stop = 1;
  528. switch (gspca_dev->usb_buf[1]) {
  529. case 0x50:
  530. sd->sensor_type = 0;
  531. PDEBUG(D_PROBE, "sensor_type corrected to 0");
  532. break;
  533. case 0x20:
  534. /* Nothing to do here. */
  535. break;
  536. default:
  537. pr_err("Unknown VGA Sensor id Byte 1: %02x\n",
  538. gspca_dev->usb_buf[1]);
  539. pr_err("Defaults assumed, may not work\n");
  540. pr_err("Please report this\n");
  541. }
  542. }
  543. PDEBUG(D_PROBE, "MR97310A VGA camera detected, sensor: %d",
  544. sd->sensor_type);
  545. }
  546. /* Stop streaming as we've started it only to probe the sensor type. */
  547. sd_stopN(gspca_dev);
  548. if (force_sensor_type != -1) {
  549. sd->sensor_type = !!force_sensor_type;
  550. PDEBUG(D_PROBE, "Forcing sensor type to: %d",
  551. sd->sensor_type);
  552. }
  553. /* Setup controls depending on camera type */
  554. if (sd->cam_type == CAM_TYPE_CIF) {
  555. /* No brightness for sensor_type 0 */
  556. if (sd->sensor_type == 0)
  557. gspca_dev->ctrl_dis = (1 << NORM_BRIGHTNESS_IDX) |
  558. (1 << ARGUS_QC_BRIGHTNESS_IDX) |
  559. (1 << CONTRAST_IDX) |
  560. (1 << SAKAR_CS_GAIN_IDX);
  561. else
  562. gspca_dev->ctrl_dis = (1 << ARGUS_QC_BRIGHTNESS_IDX) |
  563. (1 << CONTRAST_IDX) |
  564. (1 << SAKAR_CS_GAIN_IDX) |
  565. (1 << MIN_CLOCKDIV_IDX);
  566. } else {
  567. /* All controls need to be disabled if VGA sensor_type is 0 */
  568. if (sd->sensor_type == 0)
  569. gspca_dev->ctrl_dis = (1 << NORM_BRIGHTNESS_IDX) |
  570. (1 << ARGUS_QC_BRIGHTNESS_IDX) |
  571. (1 << EXPOSURE_IDX) |
  572. (1 << GAIN_IDX) |
  573. (1 << CONTRAST_IDX) |
  574. (1 << SAKAR_CS_GAIN_IDX) |
  575. (1 << MIN_CLOCKDIV_IDX);
  576. else if (sd->sensor_type == 2) {
  577. gspca_dev->ctrl_dis = (1 << NORM_BRIGHTNESS_IDX) |
  578. (1 << ARGUS_QC_BRIGHTNESS_IDX) |
  579. (1 << GAIN_IDX) |
  580. (1 << MIN_CLOCKDIV_IDX);
  581. gain_default = MR97310A_CS_GAIN_DEFAULT;
  582. } else if (sd->do_lcd_stop)
  583. /* Argus QuickClix has different brightness limits */
  584. gspca_dev->ctrl_dis = (1 << NORM_BRIGHTNESS_IDX) |
  585. (1 << CONTRAST_IDX) |
  586. (1 << SAKAR_CS_GAIN_IDX);
  587. else
  588. gspca_dev->ctrl_dis = (1 << ARGUS_QC_BRIGHTNESS_IDX) |
  589. (1 << CONTRAST_IDX) |
  590. (1 << SAKAR_CS_GAIN_IDX);
  591. }
  592. sd->brightness = MR97310A_BRIGHTNESS_DEFAULT;
  593. sd->exposure = MR97310A_EXPOSURE_DEFAULT;
  594. sd->gain = gain_default;
  595. sd->contrast = MR97310A_CONTRAST_DEFAULT;
  596. sd->min_clockdiv = MR97310A_MIN_CLOCKDIV_DEFAULT;
  597. return 0;
  598. }
  599. /* this function is called at probe and resume time */
  600. static int sd_init(struct gspca_dev *gspca_dev)
  601. {
  602. return 0;
  603. }
  604. static int start_cif_cam(struct gspca_dev *gspca_dev)
  605. {
  606. struct sd *sd = (struct sd *) gspca_dev;
  607. __u8 *data = gspca_dev->usb_buf;
  608. int err_code;
  609. static const __u8 startup_string[] = {
  610. 0x00,
  611. 0x0d,
  612. 0x01,
  613. 0x00, /* Hsize/8 for 352 or 320 */
  614. 0x00, /* Vsize/4 for 288 or 240 */
  615. 0x13, /* or 0xbb, depends on sensor */
  616. 0x00, /* Hstart, depends on res. */
  617. 0x00, /* reserved ? */
  618. 0x00, /* Vstart, depends on res. and sensor */
  619. 0x50, /* 0x54 to get 176 or 160 */
  620. 0xc0
  621. };
  622. /* Note: Some of the above descriptions guessed from MR97113A driver */
  623. memcpy(data, startup_string, 11);
  624. if (sd->sensor_type)
  625. data[5] = 0xbb;
  626. switch (gspca_dev->width) {
  627. case 160:
  628. data[9] |= 0x04; /* reg 8, 2:1 scale down from 320 */
  629. /* fall thru */
  630. case 320:
  631. default:
  632. data[3] = 0x28; /* reg 2, H size/8 */
  633. data[4] = 0x3c; /* reg 3, V size/4 */
  634. data[6] = 0x14; /* reg 5, H start */
  635. data[8] = 0x1a + sd->sensor_type; /* reg 7, V start */
  636. break;
  637. case 176:
  638. data[9] |= 0x04; /* reg 8, 2:1 scale down from 352 */
  639. /* fall thru */
  640. case 352:
  641. data[3] = 0x2c; /* reg 2, H size/8 */
  642. data[4] = 0x48; /* reg 3, V size/4 */
  643. data[6] = 0x06; /* reg 5, H start */
  644. data[8] = 0x06 - sd->sensor_type; /* reg 7, V start */
  645. break;
  646. }
  647. err_code = mr_write(gspca_dev, 11);
  648. if (err_code < 0)
  649. return err_code;
  650. if (!sd->sensor_type) {
  651. static const struct sensor_w_data cif_sensor0_init_data[] = {
  652. {0x02, 0x00, {0x03, 0x5a, 0xb5, 0x01,
  653. 0x0f, 0x14, 0x0f, 0x10}, 8},
  654. {0x0c, 0x00, {0x04, 0x01, 0x01, 0x00, 0x1f}, 5},
  655. {0x12, 0x00, {0x07}, 1},
  656. {0x1f, 0x00, {0x06}, 1},
  657. {0x27, 0x00, {0x04}, 1},
  658. {0x29, 0x00, {0x0c}, 1},
  659. {0x40, 0x00, {0x40, 0x00, 0x04}, 3},
  660. {0x50, 0x00, {0x60}, 1},
  661. {0x60, 0x00, {0x06}, 1},
  662. {0x6b, 0x00, {0x85, 0x85, 0xc8, 0xc8, 0xc8, 0xc8}, 6},
  663. {0x72, 0x00, {0x1e, 0x56}, 2},
  664. {0x75, 0x00, {0x58, 0x40, 0xa2, 0x02, 0x31, 0x02,
  665. 0x31, 0x80, 0x00}, 9},
  666. {0x11, 0x00, {0x01}, 1},
  667. {0, 0, {0}, 0}
  668. };
  669. err_code = sensor_write_regs(gspca_dev, cif_sensor0_init_data,
  670. ARRAY_SIZE(cif_sensor0_init_data));
  671. } else { /* sd->sensor_type = 1 */
  672. static const struct sensor_w_data cif_sensor1_init_data[] = {
  673. /* Reg 3,4, 7,8 get set by the controls */
  674. {0x02, 0x00, {0x10}, 1},
  675. {0x05, 0x01, {0x22}, 1}, /* 5/6 also seen as 65h/32h */
  676. {0x06, 0x01, {0x00}, 1},
  677. {0x09, 0x02, {0x0e}, 1},
  678. {0x0a, 0x02, {0x05}, 1},
  679. {0x0b, 0x02, {0x05}, 1},
  680. {0x0c, 0x02, {0x0f}, 1},
  681. {0x0d, 0x02, {0x07}, 1},
  682. {0x0e, 0x02, {0x0c}, 1},
  683. {0x0f, 0x00, {0x00}, 1},
  684. {0x10, 0x00, {0x06}, 1},
  685. {0x11, 0x00, {0x07}, 1},
  686. {0x12, 0x00, {0x00}, 1},
  687. {0x13, 0x00, {0x01}, 1},
  688. {0, 0, {0}, 0}
  689. };
  690. /* Without this command the cam won't work with USB-UHCI */
  691. gspca_dev->usb_buf[0] = 0x0a;
  692. gspca_dev->usb_buf[1] = 0x00;
  693. err_code = mr_write(gspca_dev, 2);
  694. if (err_code < 0)
  695. return err_code;
  696. err_code = sensor_write_regs(gspca_dev, cif_sensor1_init_data,
  697. ARRAY_SIZE(cif_sensor1_init_data));
  698. }
  699. return err_code;
  700. }
  701. static int start_vga_cam(struct gspca_dev *gspca_dev)
  702. {
  703. struct sd *sd = (struct sd *) gspca_dev;
  704. __u8 *data = gspca_dev->usb_buf;
  705. int err_code;
  706. static const __u8 startup_string[] =
  707. {0x00, 0x0d, 0x01, 0x00, 0x00, 0x2b, 0x00, 0x00,
  708. 0x00, 0x50, 0xc0};
  709. /* What some of these mean is explained in start_cif_cam(), above */
  710. memcpy(data, startup_string, 11);
  711. if (!sd->sensor_type) {
  712. data[5] = 0x00;
  713. data[10] = 0x91;
  714. }
  715. if (sd->sensor_type == 2) {
  716. data[5] = 0x00;
  717. data[10] = 0x18;
  718. }
  719. switch (gspca_dev->width) {
  720. case 160:
  721. data[9] |= 0x0c; /* reg 8, 4:1 scale down */
  722. /* fall thru */
  723. case 320:
  724. data[9] |= 0x04; /* reg 8, 2:1 scale down */
  725. /* fall thru */
  726. case 640:
  727. default:
  728. data[3] = 0x50; /* reg 2, H size/8 */
  729. data[4] = 0x78; /* reg 3, V size/4 */
  730. data[6] = 0x04; /* reg 5, H start */
  731. data[8] = 0x03; /* reg 7, V start */
  732. if (sd->sensor_type == 2) {
  733. data[6] = 2;
  734. data[8] = 1;
  735. }
  736. if (sd->do_lcd_stop)
  737. data[8] = 0x04; /* Bayer tile shifted */
  738. break;
  739. case 176:
  740. data[9] |= 0x04; /* reg 8, 2:1 scale down */
  741. /* fall thru */
  742. case 352:
  743. data[3] = 0x2c; /* reg 2, H size */
  744. data[4] = 0x48; /* reg 3, V size */
  745. data[6] = 0x94; /* reg 5, H start */
  746. data[8] = 0x63; /* reg 7, V start */
  747. if (sd->do_lcd_stop)
  748. data[8] = 0x64; /* Bayer tile shifted */
  749. break;
  750. }
  751. err_code = mr_write(gspca_dev, 11);
  752. if (err_code < 0)
  753. return err_code;
  754. if (!sd->sensor_type) {
  755. static const struct sensor_w_data vga_sensor0_init_data[] = {
  756. {0x01, 0x00, {0x0c, 0x00, 0x04}, 3},
  757. {0x14, 0x00, {0x01, 0xe4, 0x02, 0x84}, 4},
  758. {0x20, 0x00, {0x00, 0x80, 0x00, 0x08}, 4},
  759. {0x25, 0x00, {0x03, 0xa9, 0x80}, 3},
  760. {0x30, 0x00, {0x30, 0x18, 0x10, 0x18}, 4},
  761. {0, 0, {0}, 0}
  762. };
  763. err_code = sensor_write_regs(gspca_dev, vga_sensor0_init_data,
  764. ARRAY_SIZE(vga_sensor0_init_data));
  765. } else if (sd->sensor_type == 1) {
  766. static const struct sensor_w_data color_adj[] = {
  767. {0x02, 0x00, {0x06, 0x59, 0x0c, 0x16, 0x00,
  768. /* adjusted blue, green, red gain correct
  769. too much blue from the Sakar Digital */
  770. 0x05, 0x01, 0x04}, 8}
  771. };
  772. static const struct sensor_w_data color_no_adj[] = {
  773. {0x02, 0x00, {0x06, 0x59, 0x0c, 0x16, 0x00,
  774. /* default blue, green, red gain settings */
  775. 0x07, 0x00, 0x01}, 8}
  776. };
  777. static const struct sensor_w_data vga_sensor1_init_data[] = {
  778. {0x11, 0x04, {0x01}, 1},
  779. {0x0a, 0x00, {0x00, 0x01, 0x00, 0x00, 0x01,
  780. /* These settings may be better for some cameras */
  781. /* {0x0a, 0x00, {0x01, 0x06, 0x00, 0x00, 0x01, */
  782. 0x00, 0x0a}, 7},
  783. {0x11, 0x04, {0x01}, 1},
  784. {0x12, 0x00, {0x00, 0x63, 0x00, 0x70, 0x00, 0x00}, 6},
  785. {0x11, 0x04, {0x01}, 1},
  786. {0, 0, {0}, 0}
  787. };
  788. if (sd->adj_colors)
  789. err_code = sensor_write_regs(gspca_dev, color_adj,
  790. ARRAY_SIZE(color_adj));
  791. else
  792. err_code = sensor_write_regs(gspca_dev, color_no_adj,
  793. ARRAY_SIZE(color_no_adj));
  794. if (err_code < 0)
  795. return err_code;
  796. err_code = sensor_write_regs(gspca_dev, vga_sensor1_init_data,
  797. ARRAY_SIZE(vga_sensor1_init_data));
  798. } else { /* sensor type == 2 */
  799. static const struct sensor_w_data vga_sensor2_init_data[] = {
  800. {0x01, 0x00, {0x48}, 1},
  801. {0x02, 0x00, {0x22}, 1},
  802. /* Reg 3 msb and 4 is lsb of the exposure setting*/
  803. {0x05, 0x00, {0x10}, 1},
  804. {0x06, 0x00, {0x00}, 1},
  805. {0x07, 0x00, {0x00}, 1},
  806. {0x08, 0x00, {0x00}, 1},
  807. {0x09, 0x00, {0x00}, 1},
  808. /* The following are used in the gain control
  809. * which is BTW completely borked in the OEM driver
  810. * The values for each color go from 0 to 0x7ff
  811. *{0x0a, 0x00, {0x01}, 1}, green1 gain msb
  812. *{0x0b, 0x00, {0x10}, 1}, green1 gain lsb
  813. *{0x0c, 0x00, {0x01}, 1}, red gain msb
  814. *{0x0d, 0x00, {0x10}, 1}, red gain lsb
  815. *{0x0e, 0x00, {0x01}, 1}, blue gain msb
  816. *{0x0f, 0x00, {0x10}, 1}, blue gain lsb
  817. *{0x10, 0x00, {0x01}, 1}, green2 gain msb
  818. *{0x11, 0x00, {0x10}, 1}, green2 gain lsb
  819. */
  820. {0x12, 0x00, {0x00}, 1},
  821. {0x13, 0x00, {0x04}, 1}, /* weird effect on colors */
  822. {0x14, 0x00, {0x00}, 1},
  823. {0x15, 0x00, {0x06}, 1},
  824. {0x16, 0x00, {0x01}, 1},
  825. {0x17, 0x00, {0xe2}, 1}, /* vertical alignment */
  826. {0x18, 0x00, {0x02}, 1},
  827. {0x19, 0x00, {0x82}, 1}, /* don't mess with */
  828. {0x1a, 0x00, {0x00}, 1},
  829. {0x1b, 0x00, {0x20}, 1},
  830. /* {0x1c, 0x00, {0x17}, 1}, contrast control */
  831. {0x1d, 0x00, {0x80}, 1}, /* moving causes a mess */
  832. {0x1e, 0x00, {0x08}, 1}, /* moving jams the camera */
  833. {0x1f, 0x00, {0x0c}, 1},
  834. {0x20, 0x00, {0x00}, 1},
  835. {0, 0, {0}, 0}
  836. };
  837. err_code = sensor_write_regs(gspca_dev, vga_sensor2_init_data,
  838. ARRAY_SIZE(vga_sensor2_init_data));
  839. }
  840. return err_code;
  841. }
  842. static int sd_start(struct gspca_dev *gspca_dev)
  843. {
  844. struct sd *sd = (struct sd *) gspca_dev;
  845. int err_code;
  846. sd->sof_read = 0;
  847. /* Some of the VGA cameras require the memory pointer
  848. * to be set to 0 again. We have been forced to start the
  849. * stream in sd_config() to detect the hardware, and closed it.
  850. * Thus, we need here to do a completely fresh and clean start. */
  851. err_code = zero_the_pointer(gspca_dev);
  852. if (err_code < 0)
  853. return err_code;
  854. err_code = stream_start(gspca_dev);
  855. if (err_code < 0)
  856. return err_code;
  857. if (sd->cam_type == CAM_TYPE_CIF) {
  858. err_code = start_cif_cam(gspca_dev);
  859. } else {
  860. err_code = start_vga_cam(gspca_dev);
  861. }
  862. if (err_code < 0)
  863. return err_code;
  864. setbrightness(gspca_dev);
  865. setcontrast(gspca_dev);
  866. setexposure(gspca_dev);
  867. setgain(gspca_dev);
  868. return isoc_enable(gspca_dev);
  869. }
  870. static void sd_stopN(struct gspca_dev *gspca_dev)
  871. {
  872. struct sd *sd = (struct sd *) gspca_dev;
  873. stream_stop(gspca_dev);
  874. /* Not all the cams need this, but even if not, probably a good idea */
  875. zero_the_pointer(gspca_dev);
  876. if (sd->do_lcd_stop)
  877. lcd_stop(gspca_dev);
  878. }
  879. static void setbrightness(struct gspca_dev *gspca_dev)
  880. {
  881. struct sd *sd = (struct sd *) gspca_dev;
  882. u8 val;
  883. u8 sign_reg = 7; /* This reg and the next one used on CIF cams. */
  884. u8 value_reg = 8; /* VGA cams seem to use regs 0x0b and 0x0c */
  885. static const u8 quick_clix_table[] =
  886. /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
  887. { 0, 4, 8, 12, 1, 2, 3, 5, 6, 9, 7, 10, 13, 11, 14, 15};
  888. /*
  889. * This control is disabled for CIF type 1 and VGA type 0 cameras.
  890. * It does not quite act linearly for the Argus QuickClix camera,
  891. * but it does control brightness. The values are 0 - 15 only, and
  892. * the table above makes them act consecutively.
  893. */
  894. if ((gspca_dev->ctrl_dis & (1 << NORM_BRIGHTNESS_IDX)) &&
  895. (gspca_dev->ctrl_dis & (1 << ARGUS_QC_BRIGHTNESS_IDX)))
  896. return;
  897. if (sd->cam_type == CAM_TYPE_VGA) {
  898. sign_reg += 4;
  899. value_reg += 4;
  900. }
  901. /* Note register 7 is also seen as 0x8x or 0xCx in some dumps */
  902. if (sd->brightness > 0) {
  903. sensor_write1(gspca_dev, sign_reg, 0x00);
  904. val = sd->brightness;
  905. } else {
  906. sensor_write1(gspca_dev, sign_reg, 0x01);
  907. val = (257 - sd->brightness);
  908. }
  909. /* Use lookup table for funky Argus QuickClix brightness */
  910. if (sd->do_lcd_stop)
  911. val = quick_clix_table[val];
  912. sensor_write1(gspca_dev, value_reg, val);
  913. }
  914. static void setexposure(struct gspca_dev *gspca_dev)
  915. {
  916. struct sd *sd = (struct sd *) gspca_dev;
  917. int exposure = MR97310A_EXPOSURE_DEFAULT;
  918. u8 buf[2];
  919. if (gspca_dev->ctrl_dis & (1 << EXPOSURE_IDX))
  920. return;
  921. if (sd->cam_type == CAM_TYPE_CIF && sd->sensor_type == 1) {
  922. /* This cam does not like exposure settings < 300,
  923. so scale 0 - 4095 to 300 - 4095 */
  924. exposure = (sd->exposure * 9267) / 10000 + 300;
  925. sensor_write1(gspca_dev, 3, exposure >> 4);
  926. sensor_write1(gspca_dev, 4, exposure & 0x0f);
  927. } else if (sd->sensor_type == 2) {
  928. exposure = sd->exposure;
  929. exposure >>= 3;
  930. sensor_write1(gspca_dev, 3, exposure >> 8);
  931. sensor_write1(gspca_dev, 4, exposure & 0xff);
  932. } else {
  933. /* We have both a clock divider and an exposure register.
  934. We first calculate the clock divider, as that determines
  935. the maximum exposure and then we calculate the exposure
  936. register setting (which goes from 0 - 511).
  937. Note our 0 - 4095 exposure is mapped to 0 - 511
  938. milliseconds exposure time */
  939. u8 clockdiv = (60 * sd->exposure + 7999) / 8000;
  940. /* Limit framerate to not exceed usb bandwidth */
  941. if (clockdiv < sd->min_clockdiv && gspca_dev->width >= 320)
  942. clockdiv = sd->min_clockdiv;
  943. else if (clockdiv < 2)
  944. clockdiv = 2;
  945. if (sd->cam_type == CAM_TYPE_VGA && clockdiv < 4)
  946. clockdiv = 4;
  947. /* Frame exposure time in ms = 1000 * clockdiv / 60 ->
  948. exposure = (sd->exposure / 8) * 511 / (1000 * clockdiv / 60) */
  949. exposure = (60 * 511 * sd->exposure) / (8000 * clockdiv);
  950. if (exposure > 511)
  951. exposure = 511;
  952. /* exposure register value is reversed! */
  953. exposure = 511 - exposure;
  954. buf[0] = exposure & 0xff;
  955. buf[1] = exposure >> 8;
  956. sensor_write_reg(gspca_dev, 0x0e, 0, buf, 2);
  957. sensor_write1(gspca_dev, 0x02, clockdiv);
  958. }
  959. }
  960. static void setgain(struct gspca_dev *gspca_dev)
  961. {
  962. struct sd *sd = (struct sd *) gspca_dev;
  963. u8 gainreg;
  964. if ((gspca_dev->ctrl_dis & (1 << GAIN_IDX)) &&
  965. (gspca_dev->ctrl_dis & (1 << SAKAR_CS_GAIN_IDX)))
  966. return;
  967. if (sd->cam_type == CAM_TYPE_CIF && sd->sensor_type == 1)
  968. sensor_write1(gspca_dev, 0x0e, sd->gain);
  969. else if (sd->cam_type == CAM_TYPE_VGA && sd->sensor_type == 2)
  970. for (gainreg = 0x0a; gainreg < 0x11; gainreg += 2) {
  971. sensor_write1(gspca_dev, gainreg, sd->gain >> 8);
  972. sensor_write1(gspca_dev, gainreg + 1, sd->gain & 0xff);
  973. }
  974. else
  975. sensor_write1(gspca_dev, 0x10, sd->gain);
  976. }
  977. static void setcontrast(struct gspca_dev *gspca_dev)
  978. {
  979. struct sd *sd = (struct sd *) gspca_dev;
  980. if (gspca_dev->ctrl_dis & (1 << CONTRAST_IDX))
  981. return;
  982. sensor_write1(gspca_dev, 0x1c, sd->contrast);
  983. }
  984. static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
  985. {
  986. struct sd *sd = (struct sd *) gspca_dev;
  987. sd->brightness = val;
  988. if (gspca_dev->streaming)
  989. setbrightness(gspca_dev);
  990. return 0;
  991. }
  992. static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
  993. {
  994. struct sd *sd = (struct sd *) gspca_dev;
  995. *val = sd->brightness;
  996. return 0;
  997. }
  998. static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val)
  999. {
  1000. struct sd *sd = (struct sd *) gspca_dev;
  1001. sd->exposure = val;
  1002. if (gspca_dev->streaming)
  1003. setexposure(gspca_dev);
  1004. return 0;
  1005. }
  1006. static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val)
  1007. {
  1008. struct sd *sd = (struct sd *) gspca_dev;
  1009. *val = sd->exposure;
  1010. return 0;
  1011. }
  1012. static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val)
  1013. {
  1014. struct sd *sd = (struct sd *) gspca_dev;
  1015. sd->gain = val;
  1016. if (gspca_dev->streaming)
  1017. setgain(gspca_dev);
  1018. return 0;
  1019. }
  1020. static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val)
  1021. {
  1022. struct sd *sd = (struct sd *) gspca_dev;
  1023. *val = sd->gain;
  1024. return 0;
  1025. }
  1026. static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
  1027. {
  1028. struct sd *sd = (struct sd *) gspca_dev;
  1029. sd->contrast = val;
  1030. if (gspca_dev->streaming)
  1031. setcontrast(gspca_dev);
  1032. return 0;
  1033. }
  1034. static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val)
  1035. {
  1036. struct sd *sd = (struct sd *) gspca_dev;
  1037. *val = sd->contrast;
  1038. return 0;
  1039. }
  1040. static int sd_setmin_clockdiv(struct gspca_dev *gspca_dev, __s32 val)
  1041. {
  1042. struct sd *sd = (struct sd *) gspca_dev;
  1043. sd->min_clockdiv = val;
  1044. if (gspca_dev->streaming)
  1045. setexposure(gspca_dev);
  1046. return 0;
  1047. }
  1048. static int sd_getmin_clockdiv(struct gspca_dev *gspca_dev, __s32 *val)
  1049. {
  1050. struct sd *sd = (struct sd *) gspca_dev;
  1051. *val = sd->min_clockdiv;
  1052. return 0;
  1053. }
  1054. /* Include pac common sof detection functions */
  1055. #include "pac_common.h"
  1056. static void sd_pkt_scan(struct gspca_dev *gspca_dev,
  1057. u8 *data, /* isoc packet */
  1058. int len) /* iso packet length */
  1059. {
  1060. struct sd *sd = (struct sd *) gspca_dev;
  1061. unsigned char *sof;
  1062. sof = pac_find_sof(&sd->sof_read, data, len);
  1063. if (sof) {
  1064. int n;
  1065. /* finish decoding current frame */
  1066. n = sof - data;
  1067. if (n > sizeof pac_sof_marker)
  1068. n -= sizeof pac_sof_marker;
  1069. else
  1070. n = 0;
  1071. gspca_frame_add(gspca_dev, LAST_PACKET,
  1072. data, n);
  1073. /* Start next frame. */
  1074. gspca_frame_add(gspca_dev, FIRST_PACKET,
  1075. pac_sof_marker, sizeof pac_sof_marker);
  1076. len -= sof - data;
  1077. data = sof;
  1078. }
  1079. gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
  1080. }
  1081. /* sub-driver description */
  1082. static const struct sd_desc sd_desc = {
  1083. .name = MODULE_NAME,
  1084. .ctrls = sd_ctrls,
  1085. .nctrls = ARRAY_SIZE(sd_ctrls),
  1086. .config = sd_config,
  1087. .init = sd_init,
  1088. .start = sd_start,
  1089. .stopN = sd_stopN,
  1090. .pkt_scan = sd_pkt_scan,
  1091. };
  1092. /* -- module initialisation -- */
  1093. static const struct usb_device_id device_table[] = {
  1094. {USB_DEVICE(0x08ca, 0x0110)}, /* Trust Spyc@m 100 */
  1095. {USB_DEVICE(0x08ca, 0x0111)}, /* Aiptek Pencam VGA+ */
  1096. {USB_DEVICE(0x093a, 0x010f)}, /* All other known MR97310A VGA cams */
  1097. {USB_DEVICE(0x093a, 0x010e)}, /* All known MR97310A CIF cams */
  1098. {}
  1099. };
  1100. MODULE_DEVICE_TABLE(usb, device_table);
  1101. /* -- device connect -- */
  1102. static int sd_probe(struct usb_interface *intf,
  1103. const struct usb_device_id *id)
  1104. {
  1105. return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
  1106. THIS_MODULE);
  1107. }
  1108. static struct usb_driver sd_driver = {
  1109. .name = MODULE_NAME,
  1110. .id_table = device_table,
  1111. .probe = sd_probe,
  1112. .disconnect = gspca_disconnect,
  1113. #ifdef CONFIG_PM
  1114. .suspend = gspca_suspend,
  1115. .resume = gspca_resume,
  1116. #endif
  1117. };
  1118. module_usb_driver(sd_driver);