mr97310a.c 30 KB

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