mr97310a.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  1. /*
  2. * Mars MR97310A library
  3. *
  4. * Copyright (C) 2009 Kyle Guinn <elyk03@gmail.com>
  5. *
  6. * Support for the MR97310A cameras in addition to the Aiptek Pencam VGA+
  7. * and for the routines for detecting and classifying these various cameras,
  8. *
  9. * Copyright (C) 2009 Theodore Kilgore <kilgota@auburn.edu>
  10. *
  11. * Acknowledgements:
  12. *
  13. * The MR97311A support in gspca/mars.c has been helpful in understanding some
  14. * of the registers in these cameras.
  15. *
  16. * Hans de Goede <hdgoede@redhat.com> and
  17. * Thomas Kaiser <thomas@kaiser-linux.li>
  18. * have assisted with their experience. Each of them has also helped by
  19. * testing a previously unsupported camera.
  20. *
  21. * This program is free software; you can redistribute it and/or modify
  22. * it under the terms of the GNU General Public License as published by
  23. * the Free Software Foundation; either version 2 of the License, or
  24. * any later version.
  25. *
  26. * This program is distributed in the hope that it will be useful,
  27. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  28. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  29. * GNU General Public License for more details.
  30. *
  31. * You should have received a copy of the GNU General Public License
  32. * along with this program; if not, write to the Free Software
  33. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  34. */
  35. #define MODULE_NAME "mr97310a"
  36. #include "gspca.h"
  37. #define CAM_TYPE_CIF 0
  38. #define CAM_TYPE_VGA 1
  39. #define MR97310A_BRIGHTNESS_MIN -254
  40. #define MR97310A_BRIGHTNESS_MAX 255
  41. #define MR97310A_BRIGHTNESS_DEFAULT 0
  42. #define MR97310A_EXPOSURE_MIN 300
  43. #define MR97310A_EXPOSURE_MAX 4095
  44. #define MR97310A_EXPOSURE_DEFAULT 1000
  45. #define MR97310A_GAIN_MIN 0
  46. #define MR97310A_GAIN_MAX 31
  47. #define MR97310A_GAIN_DEFAULT 25
  48. MODULE_AUTHOR("Kyle Guinn <elyk03@gmail.com>,"
  49. "Theodore Kilgore <kilgota@auburn.edu>");
  50. MODULE_DESCRIPTION("GSPCA/Mars-Semi MR97310A USB Camera Driver");
  51. MODULE_LICENSE("GPL");
  52. /* specific webcam descriptor */
  53. struct sd {
  54. struct gspca_dev gspca_dev; /* !! must be the first item */
  55. u8 sof_read;
  56. u8 cam_type; /* 0 is CIF and 1 is VGA */
  57. u8 sensor_type; /* We use 0 and 1 here, too. */
  58. u8 do_lcd_stop;
  59. u8 regs[15];
  60. int brightness;
  61. u16 exposure;
  62. u8 autogain;
  63. u8 gain;
  64. };
  65. struct sensor_w_data {
  66. u8 reg;
  67. u8 flags;
  68. u8 data[16];
  69. int len;
  70. };
  71. static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
  72. static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
  73. static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val);
  74. static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val);
  75. static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val);
  76. static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val);
  77. /* V4L2 controls supported by the driver */
  78. static struct ctrl sd_ctrls[] = {
  79. {
  80. {
  81. .id = V4L2_CID_BRIGHTNESS,
  82. .type = V4L2_CTRL_TYPE_INTEGER,
  83. .name = "Brightness",
  84. .minimum = MR97310A_BRIGHTNESS_MIN,
  85. .maximum = MR97310A_BRIGHTNESS_MAX,
  86. .step = 1,
  87. .default_value = MR97310A_BRIGHTNESS_DEFAULT,
  88. .flags = 0,
  89. },
  90. .set = sd_setbrightness,
  91. .get = sd_getbrightness,
  92. },
  93. {
  94. {
  95. .id = V4L2_CID_EXPOSURE,
  96. .type = V4L2_CTRL_TYPE_INTEGER,
  97. .name = "Exposure",
  98. .minimum = MR97310A_EXPOSURE_MIN,
  99. .maximum = MR97310A_EXPOSURE_MAX,
  100. .step = 1,
  101. .default_value = MR97310A_EXPOSURE_DEFAULT,
  102. .flags = 0,
  103. },
  104. .set = sd_setexposure,
  105. .get = sd_getexposure,
  106. },
  107. {
  108. {
  109. .id = V4L2_CID_GAIN,
  110. .type = V4L2_CTRL_TYPE_INTEGER,
  111. .name = "Gain",
  112. .minimum = MR97310A_GAIN_MIN,
  113. .maximum = MR97310A_GAIN_MAX,
  114. .step = 1,
  115. .default_value = MR97310A_GAIN_DEFAULT,
  116. .flags = 0,
  117. },
  118. .set = sd_setgain,
  119. .get = sd_getgain,
  120. },
  121. };
  122. static const struct v4l2_pix_format vga_mode[] = {
  123. {160, 120, V4L2_PIX_FMT_MR97310A, V4L2_FIELD_NONE,
  124. .bytesperline = 160,
  125. .sizeimage = 160 * 120,
  126. .colorspace = V4L2_COLORSPACE_SRGB,
  127. .priv = 4},
  128. {176, 144, V4L2_PIX_FMT_MR97310A, V4L2_FIELD_NONE,
  129. .bytesperline = 176,
  130. .sizeimage = 176 * 144,
  131. .colorspace = V4L2_COLORSPACE_SRGB,
  132. .priv = 3},
  133. {320, 240, V4L2_PIX_FMT_MR97310A, V4L2_FIELD_NONE,
  134. .bytesperline = 320,
  135. .sizeimage = 320 * 240,
  136. .colorspace = V4L2_COLORSPACE_SRGB,
  137. .priv = 2},
  138. {352, 288, V4L2_PIX_FMT_MR97310A, V4L2_FIELD_NONE,
  139. .bytesperline = 352,
  140. .sizeimage = 352 * 288,
  141. .colorspace = V4L2_COLORSPACE_SRGB,
  142. .priv = 1},
  143. {640, 480, V4L2_PIX_FMT_MR97310A, V4L2_FIELD_NONE,
  144. .bytesperline = 640,
  145. .sizeimage = 640 * 480,
  146. .colorspace = V4L2_COLORSPACE_SRGB,
  147. .priv = 0},
  148. };
  149. /* the bytes to write are in gspca_dev->usb_buf */
  150. static int mr_write(struct gspca_dev *gspca_dev, int len)
  151. {
  152. int rc;
  153. rc = usb_bulk_msg(gspca_dev->dev,
  154. usb_sndbulkpipe(gspca_dev->dev, 4),
  155. gspca_dev->usb_buf, len, NULL, 500);
  156. if (rc < 0)
  157. PDEBUG(D_ERR, "reg write [%02x] error %d",
  158. gspca_dev->usb_buf[0], rc);
  159. return rc;
  160. }
  161. /* the bytes are read into gspca_dev->usb_buf */
  162. static int mr_read(struct gspca_dev *gspca_dev, int len)
  163. {
  164. int rc;
  165. rc = usb_bulk_msg(gspca_dev->dev,
  166. usb_rcvbulkpipe(gspca_dev->dev, 3),
  167. gspca_dev->usb_buf, len, NULL, 500);
  168. if (rc < 0)
  169. PDEBUG(D_ERR, "reg read [%02x] error %d",
  170. gspca_dev->usb_buf[0], rc);
  171. return rc;
  172. }
  173. static int sensor_write_reg(struct gspca_dev *gspca_dev, u8 reg, u8 flags,
  174. const u8 *data, int len)
  175. {
  176. gspca_dev->usb_buf[0] = 0x1f;
  177. gspca_dev->usb_buf[1] = flags;
  178. gspca_dev->usb_buf[2] = reg;
  179. memcpy(gspca_dev->usb_buf + 3, data, len);
  180. return mr_write(gspca_dev, len + 3);
  181. }
  182. static int sensor_write_regs(struct gspca_dev *gspca_dev,
  183. const struct sensor_w_data *data, int len)
  184. {
  185. int i, rc;
  186. for (i = 0; i < len; i++) {
  187. rc = sensor_write_reg(gspca_dev, data[i].reg, data[i].flags,
  188. data[i].data, data[i].len);
  189. if (rc < 0)
  190. return rc;
  191. }
  192. return 0;
  193. }
  194. static int sensor_write1(struct gspca_dev *gspca_dev, u8 reg, u8 data)
  195. {
  196. u8 buf;
  197. int rc;
  198. buf = data;
  199. rc = sensor_write_reg(gspca_dev, reg, 0x01, &buf, 1);
  200. if (rc < 0)
  201. return rc;
  202. buf = 0x01;
  203. rc = sensor_write_reg(gspca_dev, 0x13, 0x00, &buf, 1);
  204. if (rc < 0)
  205. return rc;
  206. return 0;
  207. }
  208. static int cam_get_response16(struct gspca_dev *gspca_dev)
  209. {
  210. __u8 *data = gspca_dev->usb_buf;
  211. int err_code;
  212. data[0] = 0x21;
  213. err_code = mr_write(gspca_dev, 1);
  214. if (err_code < 0)
  215. return err_code;
  216. err_code = mr_read(gspca_dev, 16);
  217. return err_code;
  218. }
  219. static int zero_the_pointer(struct gspca_dev *gspca_dev)
  220. {
  221. __u8 *data = gspca_dev->usb_buf;
  222. int err_code;
  223. u8 status = 0;
  224. int tries = 0;
  225. err_code = cam_get_response16(gspca_dev);
  226. if (err_code < 0)
  227. return err_code;
  228. err_code = mr_write(gspca_dev, 1);
  229. data[0] = 0x19;
  230. data[1] = 0x51;
  231. err_code = mr_write(gspca_dev, 2);
  232. if (err_code < 0)
  233. return err_code;
  234. err_code = cam_get_response16(gspca_dev);
  235. if (err_code < 0)
  236. return err_code;
  237. data[0] = 0x19;
  238. data[1] = 0xba;
  239. err_code = mr_write(gspca_dev, 2);
  240. if (err_code < 0)
  241. return err_code;
  242. err_code = cam_get_response16(gspca_dev);
  243. if (err_code < 0)
  244. return err_code;
  245. data[0] = 0x19;
  246. data[1] = 0x00;
  247. err_code = mr_write(gspca_dev, 2);
  248. if (err_code < 0)
  249. return err_code;
  250. err_code = cam_get_response16(gspca_dev);
  251. if (err_code < 0)
  252. return err_code;
  253. data[0] = 0x19;
  254. data[1] = 0x00;
  255. err_code = mr_write(gspca_dev, 2);
  256. if (err_code < 0)
  257. return err_code;
  258. while (status != 0x0a && tries < 256) {
  259. err_code = cam_get_response16(gspca_dev);
  260. status = data[0];
  261. tries++;
  262. if (err_code < 0)
  263. return err_code;
  264. }
  265. PDEBUG(D_ERR, "status is %02x", status);
  266. tries = 0;
  267. while (tries < 4) {
  268. data[0] = 0x19;
  269. data[1] = 0x00;
  270. err_code = mr_write(gspca_dev, 2);
  271. if (err_code < 0)
  272. return err_code;
  273. err_code = cam_get_response16(gspca_dev);
  274. status = data[0];
  275. tries++;
  276. if (err_code < 0)
  277. return err_code;
  278. }
  279. PDEBUG(D_ERR, "Read 16 bytes from camera");
  280. data[0] = 0x19;
  281. err_code = mr_write(gspca_dev, 1);
  282. if (err_code < 0)
  283. return err_code;
  284. err_code = mr_read(gspca_dev, 16);
  285. if (err_code < 0)
  286. return err_code;
  287. return 0;
  288. }
  289. static u8 get_sensor_id(struct gspca_dev *gspca_dev)
  290. {
  291. int err_code;
  292. gspca_dev->usb_buf[0] = 0x1e;
  293. err_code = mr_write(gspca_dev, 1);
  294. if (err_code < 0)
  295. return err_code;
  296. err_code = mr_read(gspca_dev, 16);
  297. if (err_code < 0)
  298. return err_code;
  299. PDEBUG(D_ERR, "Read 16 bytes from camera");
  300. PDEBUG(D_ERR, "Byte zero reported is %01x", gspca_dev->usb_buf[0]);
  301. return gspca_dev->usb_buf[0];
  302. }
  303. /* this function is called at probe time */
  304. static int sd_config(struct gspca_dev *gspca_dev,
  305. const struct usb_device_id *id)
  306. {
  307. struct sd *sd = (struct sd *) gspca_dev;
  308. struct cam *cam;
  309. cam = &gspca_dev->cam;
  310. cam->cam_mode = vga_mode;
  311. cam->nmodes = ARRAY_SIZE(vga_mode);
  312. sd->cam_type = CAM_TYPE_VGA;
  313. PDEBUG(D_PROBE,
  314. "MR97310A camera detected"
  315. " (vid/pid 0x%04X:0x%04X)", id->idVendor, id->idProduct);
  316. if (id->idProduct == 0x010e) {
  317. cam->nmodes--;
  318. sd->cam_type = CAM_TYPE_CIF;
  319. }
  320. return 0;
  321. }
  322. /* this function is called at probe and resume time */
  323. static int sd_init(struct gspca_dev *gspca_dev)
  324. {
  325. return 0;
  326. }
  327. static int adjust_cif_sensor(struct gspca_dev *gspca_dev)
  328. {
  329. /*
  330. * FIXME: The following sequence resets brightness, contrast, and
  331. * related settings. Some of the values are adjustable, presumably
  332. * based upon what is detected in the frames. Here, only some
  333. * vaules are used which are compromises. When more is known about
  334. * what is done here, this needs to be moved out to presently
  335. * nonexistent functions which do controls. The same control messages
  336. * do work for all of the CIF cameras.
  337. */
  338. const struct sensor_w_data cif_sensor1_adjust_data[] = {
  339. {0x02, 0x01, {0x10, 0x12, 0x0a}, 3},
  340. /* Last or possibly two last bytes adjustable, above. */
  341. {0x13, 0x04, {0x01}, 1}, /* seems to mean "write" */
  342. {0x05, 0x01, {0x22, 0x00, 0x81, 0x06}, 4},
  343. /* Last or possibly two last bytes adjustable, above. */
  344. {0x13, 0x04, {0x01}, 1},
  345. {0x09, 0x02, {0x05, 0x00, 0x00, 0x05, 0x07, 0x16}, 6},
  346. /* Last or possibly two last bytes adjustable, above. */
  347. {0x13, 0x04, {0x01}, 1},
  348. {0, 0, {0}, 0}
  349. };
  350. return sensor_write_regs(gspca_dev, cif_sensor1_adjust_data,
  351. ARRAY_SIZE(cif_sensor1_adjust_data));
  352. }
  353. static int start_cif_cam(struct gspca_dev *gspca_dev)
  354. {
  355. struct sd *sd = (struct sd *) gspca_dev;
  356. __u8 *data = gspca_dev->usb_buf;
  357. int err_code;
  358. const __u8 startup_string[] = {
  359. 0x00,
  360. 0x0d,
  361. 0x01,
  362. 0x00, /* Hsize/8 for 352 or 320 */
  363. 0x00, /* Vsize/4 for 288 or 240 */
  364. 0x13, /* or 0xbb, depends on sensor */
  365. 0x00, /* Hstart, depends on res. */
  366. 0x00, /* reserved ? */
  367. 0x00, /* Vstart, depends on res. and sensor */
  368. 0x50, /* 0x54 to get 176 or 160 */
  369. 0xc0
  370. };
  371. /* Note: Some of the above descriptions guessed from MR97113A driver */
  372. sd->sensor_type = 0;
  373. data[0] = 0x01;
  374. data[1] = 0x01;
  375. err_code = mr_write(gspca_dev, 2);
  376. if (err_code < 0)
  377. return err_code;
  378. msleep(200);
  379. data[0] = get_sensor_id(gspca_dev);
  380. /*
  381. * Known CIF cameras. If you have another to report, please do
  382. *
  383. * Name byte just read sd->sensor_type
  384. * reported by
  385. * Sakar Spy-shot 0x28 T. Kilgore 0
  386. * Innovage 0xf5 (unstable) T. Kilgore 0
  387. * Vivitar Mini 0x53 H. De Goede 0
  388. * Vivitar Mini 0x08 T. Kilgore 1
  389. * Elta-Media 8212dc 0x23 T. Kaiser 1
  390. * Philips dig. keych. 0x37 T. Kilgore 1
  391. */
  392. if ((data[0] & 0x78) == 8 ||
  393. ((data[0] & 0x2) == 0x2 && data[0] != 0x53))
  394. sd->sensor_type = 1;
  395. PDEBUG(D_ERR, "Sensor type is %01x", sd->sensor_type);
  396. memcpy(data, startup_string, 11);
  397. if (sd->sensor_type)
  398. data[5] = 0xbb;
  399. switch (gspca_dev->width) {
  400. case 160:
  401. data[9] |= 0x04; /* reg 8, 2:1 scale down from 320 */
  402. /* fall thru */
  403. case 320:
  404. default:
  405. data[3] = 0x28; /* reg 2, H size/8 */
  406. data[4] = 0x3c; /* reg 3, V size/4 */
  407. data[6] = 0x14; /* reg 5, H start */
  408. data[8] = 0x1a + sd->sensor_type; /* reg 7, V start */
  409. break;
  410. case 176:
  411. data[9] |= 0x04; /* reg 8, 2:1 scale down from 352 */
  412. /* fall thru */
  413. case 352:
  414. data[3] = 0x2c; /* reg 2, H size/8 */
  415. data[4] = 0x48; /* reg 3, V size/4 */
  416. data[6] = 0x06; /* reg 5, H start */
  417. data[8] = 0x06 + sd->sensor_type; /* reg 7, V start */
  418. break;
  419. }
  420. err_code = mr_write(gspca_dev, 11);
  421. if (err_code < 0)
  422. return err_code;
  423. if (!sd->sensor_type) {
  424. const struct sensor_w_data cif_sensor0_init_data[] = {
  425. {0x02, 0x00, {0x03, 0x5a, 0xb5, 0x01,
  426. 0x0f, 0x14, 0x0f, 0x10}, 8},
  427. {0x0c, 0x00, {0x04, 0x01, 0x01, 0x00, 0x1f}, 5},
  428. {0x12, 0x00, {0x07}, 1},
  429. {0x1f, 0x00, {0x06}, 1},
  430. {0x27, 0x00, {0x04}, 1},
  431. {0x29, 0x00, {0x0c}, 1},
  432. {0x40, 0x00, {0x40, 0x00, 0x04}, 3},
  433. {0x50, 0x00, {0x60}, 1},
  434. {0x60, 0x00, {0x06}, 1},
  435. {0x6b, 0x00, {0x85, 0x85, 0xc8, 0xc8, 0xc8, 0xc8}, 6},
  436. {0x72, 0x00, {0x1e, 0x56}, 2},
  437. {0x75, 0x00, {0x58, 0x40, 0xa2, 0x02, 0x31, 0x02,
  438. 0x31, 0x80, 0x00}, 9},
  439. {0x11, 0x00, {0x01}, 1},
  440. {0, 0, {0}, 0}
  441. };
  442. err_code = sensor_write_regs(gspca_dev, cif_sensor0_init_data,
  443. ARRAY_SIZE(cif_sensor0_init_data));
  444. } else { /* sd->sensor_type = 1 */
  445. const struct sensor_w_data cif_sensor1_init_data[] = {
  446. {0x02, 0x00, {0x10}, 1},
  447. {0x03, 0x01, {0x12}, 1},
  448. {0x04, 0x01, {0x05}, 1},
  449. {0x05, 0x01, {0x65}, 1},
  450. {0x06, 0x01, {0x32}, 1},
  451. {0x07, 0x01, {0x00}, 1},
  452. {0x08, 0x02, {0x06}, 1},
  453. {0x09, 0x02, {0x0e}, 1},
  454. {0x0a, 0x02, {0x05}, 1},
  455. {0x0b, 0x02, {0x05}, 1},
  456. {0x0c, 0x02, {0x0f}, 1},
  457. {0x0d, 0x02, {0x00}, 1},
  458. {0x0e, 0x02, {0x0c}, 1},
  459. {0x0f, 0x00, {0x00}, 1},
  460. {0x10, 0x00, {0x06}, 1},
  461. {0x11, 0x00, {0x07}, 1},
  462. {0x12, 0x00, {0x00}, 1},
  463. {0x13, 0x00, {0x01}, 1},
  464. {0, 0, {0}, 0}
  465. };
  466. err_code = sensor_write_regs(gspca_dev, cif_sensor1_init_data,
  467. ARRAY_SIZE(cif_sensor1_init_data));
  468. }
  469. if (err_code < 0)
  470. return err_code;
  471. msleep(200);
  472. data[0] = 0x00;
  473. data[1] = 0x4d; /* ISOC transfering enable... */
  474. err_code = mr_write(gspca_dev, 2);
  475. if (err_code < 0)
  476. return err_code;
  477. msleep(200);
  478. err_code = adjust_cif_sensor(gspca_dev);
  479. if (err_code < 0)
  480. return err_code;
  481. msleep(200);
  482. return 0;
  483. }
  484. static int start_vga_cam(struct gspca_dev *gspca_dev)
  485. {
  486. struct sd *sd = (struct sd *) gspca_dev;
  487. __u8 *data = gspca_dev->usb_buf;
  488. int err_code;
  489. const __u8 startup_string[] = {0x00, 0x0d, 0x01, 0x00, 0x00, 0x2b,
  490. 0x00, 0x00, 0x00, 0x50, 0xc0};
  491. /* What some of these mean is explained in start_cif_cam(), above */
  492. sd->sof_read = 0;
  493. /*
  494. * We have to know which camera we have, because the register writes
  495. * depend upon the camera. This test, run before we actually enter
  496. * the initialization routine, distinguishes most of the cameras, If
  497. * needed, another routine is done later, too.
  498. */
  499. memset(data, 0, 16);
  500. data[0] = 0x20;
  501. err_code = mr_write(gspca_dev, 1);
  502. if (err_code < 0)
  503. return err_code;
  504. err_code = mr_read(gspca_dev, 16);
  505. if (err_code < 0)
  506. return err_code;
  507. PDEBUG(D_ERR, "Read 16 bytes from camera");
  508. PDEBUG(D_ERR, "Byte reported is %02x", data[0]);
  509. msleep(200);
  510. /*
  511. * Known VGA cameras. If you have another to report, please do
  512. *
  513. * Name byte just read sd->sensor_type
  514. * sd->do_lcd_stop
  515. * Aiptek Pencam VGA+ 0x31 0 1
  516. * ION digital 0x31 0 1
  517. * Argus DC-1620 0x30 1 0
  518. * Argus QuickClix 0x30 1 1 (not caught here)
  519. */
  520. sd->sensor_type = data[0] & 1;
  521. sd->do_lcd_stop = (~data[0]) & 1;
  522. /* Streaming setup begins here. */
  523. data[0] = 0x01;
  524. data[1] = 0x01;
  525. err_code = mr_write(gspca_dev, 2);
  526. if (err_code < 0)
  527. return err_code;
  528. /*
  529. * A second test can now resolve any remaining ambiguity in the
  530. * identification of the camera type,
  531. */
  532. if (!sd->sensor_type) {
  533. data[0] = get_sensor_id(gspca_dev);
  534. if (data[0] == 0x7f) {
  535. sd->sensor_type = 1;
  536. PDEBUG(D_ERR, "sensor_type corrected to 1");
  537. }
  538. msleep(200);
  539. }
  540. /*
  541. * Known VGA cameras.
  542. * This test is only run if the previous test returned 0x30, but
  543. * here is the information for all others, too, just for reference.
  544. *
  545. * Name byte just read sd->sensor_type
  546. *
  547. * Aiptek Pencam VGA+ 0xfb (this test not run) 1
  548. * ION digital 0xbd (this test not run) 1
  549. * Argus DC-1620 0xe5 (no change) 0
  550. * Argus QuickClix 0x7f (reclassified) 1
  551. */
  552. memcpy(data, startup_string, 11);
  553. if (!sd->sensor_type) {
  554. data[5] = 0x00;
  555. data[10] = 0x91;
  556. }
  557. switch (gspca_dev->width) {
  558. case 160:
  559. data[9] |= 0x0c; /* reg 8, 4:1 scale down */
  560. /* fall thru */
  561. case 320:
  562. data[9] |= 0x04; /* reg 8, 2:1 scale down */
  563. /* fall thru */
  564. case 640:
  565. default:
  566. data[3] = 0x50; /* reg 2, H size/8 */
  567. data[4] = 0x78; /* reg 3, V size/4 */
  568. data[6] = 0x04; /* reg 5, H start */
  569. data[8] = 0x03; /* reg 7, V start */
  570. if (sd->do_lcd_stop)
  571. data[8] = 0x04; /* Bayer tile shifted */
  572. break;
  573. case 176:
  574. data[9] |= 0x04; /* reg 8, 2:1 scale down */
  575. /* fall thru */
  576. case 352:
  577. data[3] = 0x2c; /* reg 2, H size */
  578. data[4] = 0x48; /* reg 3, V size */
  579. data[6] = 0x94; /* reg 5, H start */
  580. data[8] = 0x63; /* reg 7, V start */
  581. if (sd->do_lcd_stop)
  582. data[8] = 0x64; /* Bayer tile shifted */
  583. break;
  584. }
  585. err_code = mr_write(gspca_dev, 11);
  586. if (err_code < 0)
  587. return err_code;
  588. if (!sd->sensor_type) {
  589. /* The only known sensor_type 0 cam is the Argus DC-1620 */
  590. const struct sensor_w_data vga_sensor0_init_data[] = {
  591. {0x01, 0x00, {0x0c, 0x00, 0x04}, 3},
  592. {0x14, 0x00, {0x01, 0xe4, 0x02, 0x84}, 4},
  593. {0x20, 0x00, {0x00, 0x80, 0x00, 0x08}, 4},
  594. {0x25, 0x00, {0x03, 0xa9, 0x80}, 3},
  595. {0x30, 0x00, {0x30, 0x18, 0x10, 0x18}, 4},
  596. {0, 0, {0}, 0}
  597. };
  598. err_code = sensor_write_regs(gspca_dev, vga_sensor0_init_data,
  599. ARRAY_SIZE(vga_sensor0_init_data));
  600. } else { /* sd->sensor_type = 1 */
  601. const struct sensor_w_data vga_sensor1_init_data[] = {
  602. {0x02, 0x00, {0x06, 0x59, 0x0c, 0x16, 0x00,
  603. 0x07, 0x00, 0x01}, 8},
  604. {0x11, 0x04, {0x01}, 1},
  605. /*{0x0a, 0x00, {0x00, 0x01, 0x00, 0x00, 0x01, */
  606. {0x0a, 0x00, {0x01, 0x06, 0x00, 0x00, 0x01,
  607. 0x00, 0x0a}, 7},
  608. {0x11, 0x04, {0x01}, 1},
  609. {0x12, 0x00, {0x00, 0x63, 0x00, 0x70, 0x00, 0x00}, 6},
  610. {0x11, 0x04, {0x01}, 1},
  611. {0, 0, {0}, 0}
  612. };
  613. err_code = sensor_write_regs(gspca_dev, vga_sensor1_init_data,
  614. ARRAY_SIZE(vga_sensor1_init_data));
  615. }
  616. if (err_code < 0)
  617. return err_code;
  618. msleep(200);
  619. data[0] = 0x00;
  620. data[1] = 0x4d; /* ISOC transfering enable... */
  621. err_code = mr_write(gspca_dev, 2);
  622. return err_code;
  623. }
  624. static int sd_start(struct gspca_dev *gspca_dev)
  625. {
  626. struct sd *sd = (struct sd *) gspca_dev;
  627. int err_code;
  628. struct cam *cam;
  629. /* TEST TEST */
  630. int i;
  631. for (i = 2; i <= 14; i++)
  632. sd->regs[i] = sd_ctrls[i - 2].qctrl.default_value;
  633. cam = &gspca_dev->cam;
  634. sd->sof_read = 0;
  635. /*
  636. * Some of the supported cameras require the memory pointer to be
  637. * set to 0, or else they will not stream.
  638. */
  639. zero_the_pointer(gspca_dev);
  640. msleep(200);
  641. if (sd->cam_type == CAM_TYPE_CIF) {
  642. PDEBUG(D_ERR, "CIF camera");
  643. err_code = start_cif_cam(gspca_dev);
  644. } else {
  645. PDEBUG(D_ERR, "VGA camera");
  646. err_code = start_vga_cam(gspca_dev);
  647. }
  648. return err_code;
  649. }
  650. static void sd_stopN(struct gspca_dev *gspca_dev)
  651. {
  652. struct sd *sd = (struct sd *) gspca_dev;
  653. int result;
  654. gspca_dev->usb_buf[0] = 1;
  655. gspca_dev->usb_buf[1] = 0;
  656. result = mr_write(gspca_dev, 2);
  657. if (result < 0)
  658. PDEBUG(D_ERR, "Camera Stop failed");
  659. /* Not all the cams need this, but even if not, probably a good idea */
  660. zero_the_pointer(gspca_dev);
  661. if (sd->do_lcd_stop) {
  662. gspca_dev->usb_buf[0] = 0x19;
  663. gspca_dev->usb_buf[1] = 0x54;
  664. result = mr_write(gspca_dev, 2);
  665. if (result < 0)
  666. PDEBUG(D_ERR, "Camera Stop failed");
  667. }
  668. }
  669. static void setbrightness(struct gspca_dev *gspca_dev)
  670. {
  671. struct sd *sd = (struct sd *) gspca_dev;
  672. u8 val;
  673. if (sd->brightness > 0) {
  674. sensor_write1(gspca_dev, 7, 0);
  675. val = sd->brightness;
  676. } else {
  677. sensor_write1(gspca_dev, 7, 1);
  678. val = 257 - sd->brightness;
  679. }
  680. sensor_write1(gspca_dev, 8, val);
  681. }
  682. static void setexposure(struct gspca_dev *gspca_dev)
  683. {
  684. struct sd *sd = (struct sd *) gspca_dev;
  685. u8 val;
  686. val = sd->exposure >> 4;
  687. sensor_write1(gspca_dev, 3, val);
  688. val = sd->exposure & 0xf;
  689. sensor_write1(gspca_dev, 4, val);
  690. }
  691. static void setgain(struct gspca_dev *gspca_dev)
  692. {
  693. struct sd *sd = (struct sd *) gspca_dev;
  694. sensor_write1(gspca_dev, 3, sd->gain);
  695. }
  696. static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
  697. {
  698. struct sd *sd = (struct sd *) gspca_dev;
  699. sd->brightness = val;
  700. if (gspca_dev->streaming)
  701. setbrightness(gspca_dev);
  702. return 0;
  703. }
  704. static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
  705. {
  706. struct sd *sd = (struct sd *) gspca_dev;
  707. *val = sd->brightness;
  708. return 0;
  709. }
  710. static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val)
  711. {
  712. struct sd *sd = (struct sd *) gspca_dev;
  713. sd->exposure = val;
  714. if (gspca_dev->streaming)
  715. setexposure(gspca_dev);
  716. return 0;
  717. }
  718. static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val)
  719. {
  720. struct sd *sd = (struct sd *) gspca_dev;
  721. *val = sd->exposure;
  722. return 0;
  723. }
  724. static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val)
  725. {
  726. struct sd *sd = (struct sd *) gspca_dev;
  727. sd->gain = val;
  728. if (gspca_dev->streaming)
  729. setgain(gspca_dev);
  730. return 0;
  731. }
  732. static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val)
  733. {
  734. struct sd *sd = (struct sd *) gspca_dev;
  735. *val = sd->gain;
  736. return 0;
  737. }
  738. /* Include pac common sof detection functions */
  739. #include "pac_common.h"
  740. static void sd_pkt_scan(struct gspca_dev *gspca_dev,
  741. struct gspca_frame *frame, /* target */
  742. __u8 *data, /* isoc packet */
  743. int len) /* iso packet length */
  744. {
  745. unsigned char *sof;
  746. sof = pac_find_sof(gspca_dev, data, len);
  747. if (sof) {
  748. int n;
  749. /* finish decoding current frame */
  750. n = sof - data;
  751. if (n > sizeof pac_sof_marker)
  752. n -= sizeof pac_sof_marker;
  753. else
  754. n = 0;
  755. frame = gspca_frame_add(gspca_dev, LAST_PACKET, frame,
  756. data, n);
  757. /* Start next frame. */
  758. gspca_frame_add(gspca_dev, FIRST_PACKET, frame,
  759. pac_sof_marker, sizeof pac_sof_marker);
  760. len -= sof - data;
  761. data = sof;
  762. }
  763. gspca_frame_add(gspca_dev, INTER_PACKET, frame, data, len);
  764. }
  765. /* sub-driver description */
  766. static const struct sd_desc sd_desc = {
  767. .name = MODULE_NAME,
  768. .ctrls = sd_ctrls,
  769. .nctrls = ARRAY_SIZE(sd_ctrls),
  770. .config = sd_config,
  771. .init = sd_init,
  772. .start = sd_start,
  773. .stopN = sd_stopN,
  774. .pkt_scan = sd_pkt_scan,
  775. };
  776. /* -- module initialisation -- */
  777. static const __devinitdata struct usb_device_id device_table[] = {
  778. {USB_DEVICE(0x08ca, 0x0111)}, /* Aiptek Pencam VGA+ */
  779. {USB_DEVICE(0x093a, 0x010f)}, /* All other known MR97310A VGA cams */
  780. {USB_DEVICE(0x093a, 0x010e)}, /* All known MR97310A CIF cams */
  781. {}
  782. };
  783. MODULE_DEVICE_TABLE(usb, device_table);
  784. /* -- device connect -- */
  785. static int sd_probe(struct usb_interface *intf,
  786. const struct usb_device_id *id)
  787. {
  788. return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
  789. THIS_MODULE);
  790. }
  791. static struct usb_driver sd_driver = {
  792. .name = MODULE_NAME,
  793. .id_table = device_table,
  794. .probe = sd_probe,
  795. .disconnect = gspca_disconnect,
  796. #ifdef CONFIG_PM
  797. .suspend = gspca_suspend,
  798. .resume = gspca_resume,
  799. #endif
  800. };
  801. /* -- module insert / remove -- */
  802. static int __init sd_mod_init(void)
  803. {
  804. int ret;
  805. ret = usb_register(&sd_driver);
  806. if (ret < 0)
  807. return ret;
  808. PDEBUG(D_PROBE, "registered");
  809. return 0;
  810. }
  811. static void __exit sd_mod_exit(void)
  812. {
  813. usb_deregister(&sd_driver);
  814. PDEBUG(D_PROBE, "deregistered");
  815. }
  816. module_init(sd_mod_init);
  817. module_exit(sd_mod_exit);