mr97310a.c 30 KB

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