mr97310a.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933
  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 || (data[0] & 0x2) == 0x2)
  393. sd->sensor_type = 1;
  394. PDEBUG(D_ERR, "Sensor type is %01x", sd->sensor_type);
  395. memcpy(data, startup_string, 11);
  396. if (sd->sensor_type)
  397. data[5] = 0xbb;
  398. switch (gspca_dev->width) {
  399. case 160:
  400. data[9] |= 0x04; /* reg 8, 2:1 scale down from 320 */
  401. /* fall thru */
  402. case 320:
  403. default:
  404. data[3] = 0x28; /* reg 2, H size/8 */
  405. data[4] = 0x3c; /* reg 3, V size/4 */
  406. data[6] = 0x14; /* reg 5, H start */
  407. data[8] = 0x1a + sd->sensor_type; /* reg 7, V start */
  408. break;
  409. case 176:
  410. data[9] |= 0x04; /* reg 8, 2:1 scale down from 352 */
  411. /* fall thru */
  412. case 352:
  413. data[3] = 0x2c; /* reg 2, H size/8 */
  414. data[4] = 0x48; /* reg 3, V size/4 */
  415. data[6] = 0x06; /* reg 5, H start */
  416. data[8] = 0x06 + sd->sensor_type; /* reg 7, V start */
  417. break;
  418. }
  419. err_code = mr_write(gspca_dev, 11);
  420. if (err_code < 0)
  421. return err_code;
  422. if (!sd->sensor_type) {
  423. const struct sensor_w_data cif_sensor0_init_data[] = {
  424. {0x02, 0x00, {0x03, 0x5a, 0xb5, 0x01,
  425. 0x0f, 0x14, 0x0f, 0x10}, 8},
  426. {0x0c, 0x00, {0x04, 0x01, 0x01, 0x00, 0x1f}, 5},
  427. {0x12, 0x00, {0x07}, 1},
  428. {0x1f, 0x00, {0x06}, 1},
  429. {0x27, 0x00, {0x04}, 1},
  430. {0x29, 0x00, {0x0c}, 1},
  431. {0x40, 0x00, {0x40, 0x00, 0x04}, 3},
  432. {0x50, 0x00, {0x60}, 1},
  433. {0x60, 0x00, {0x06}, 1},
  434. {0x6b, 0x00, {0x85, 0x85, 0xc8, 0xc8, 0xc8, 0xc8}, 6},
  435. {0x72, 0x00, {0x1e, 0x56}, 2},
  436. {0x75, 0x00, {0x58, 0x40, 0xa2, 0x02, 0x31, 0x02,
  437. 0x31, 0x80, 0x00}, 9},
  438. {0x11, 0x00, {0x01}, 1},
  439. {0, 0, {0}, 0}
  440. };
  441. err_code = sensor_write_regs(gspca_dev, cif_sensor0_init_data,
  442. ARRAY_SIZE(cif_sensor0_init_data));
  443. } else { /* sd->sensor_type = 1 */
  444. const struct sensor_w_data cif_sensor1_init_data[] = {
  445. {0x02, 0x00, {0x10}, 1},
  446. {0x03, 0x01, {0x12}, 1},
  447. {0x04, 0x01, {0x05}, 1},
  448. {0x05, 0x01, {0x65}, 1},
  449. {0x06, 0x01, {0x32}, 1},
  450. {0x07, 0x01, {0x00}, 1},
  451. {0x08, 0x02, {0x06}, 1},
  452. {0x09, 0x02, {0x0e}, 1},
  453. {0x0a, 0x02, {0x05}, 1},
  454. {0x0b, 0x02, {0x05}, 1},
  455. {0x0c, 0x02, {0x0f}, 1},
  456. {0x0d, 0x02, {0x00}, 1},
  457. {0x0e, 0x02, {0x0c}, 1},
  458. {0x0f, 0x00, {0x00}, 1},
  459. {0x10, 0x00, {0x06}, 1},
  460. {0x11, 0x00, {0x07}, 1},
  461. {0x12, 0x00, {0x00}, 1},
  462. {0x13, 0x00, {0x01}, 1},
  463. {0, 0, {0}, 0}
  464. };
  465. err_code = sensor_write_regs(gspca_dev, cif_sensor1_init_data,
  466. ARRAY_SIZE(cif_sensor1_init_data));
  467. }
  468. if (err_code < 0)
  469. return err_code;
  470. msleep(200);
  471. data[0] = 0x00;
  472. data[1] = 0x4d; /* ISOC transfering enable... */
  473. err_code = mr_write(gspca_dev, 2);
  474. if (err_code < 0)
  475. return err_code;
  476. msleep(200);
  477. err_code = adjust_cif_sensor(gspca_dev);
  478. if (err_code < 0)
  479. return err_code;
  480. msleep(200);
  481. return 0;
  482. }
  483. static int start_vga_cam(struct gspca_dev *gspca_dev)
  484. {
  485. struct sd *sd = (struct sd *) gspca_dev;
  486. __u8 *data = gspca_dev->usb_buf;
  487. int err_code;
  488. const __u8 startup_string[] = {0x00, 0x0d, 0x01, 0x00, 0x00, 0x2b,
  489. 0x00, 0x00, 0x00, 0x50, 0xc0};
  490. /* What some of these mean is explained in start_cif_cam(), above */
  491. sd->sof_read = 0;
  492. /*
  493. * We have to know which camera we have, because the register writes
  494. * depend upon the camera. This test, run before we actually enter
  495. * the initialization routine, distinguishes most of the cameras, If
  496. * needed, another routine is done later, too.
  497. */
  498. memset(data, 0, 16);
  499. data[0] = 0x20;
  500. err_code = mr_write(gspca_dev, 1);
  501. if (err_code < 0)
  502. return err_code;
  503. err_code = mr_read(gspca_dev, 16);
  504. if (err_code < 0)
  505. return err_code;
  506. PDEBUG(D_ERR, "Read 16 bytes from camera");
  507. PDEBUG(D_ERR, "Byte reported is %02x", data[0]);
  508. msleep(200);
  509. /*
  510. * Known VGA cameras. If you have another to report, please do
  511. *
  512. * Name byte just read sd->sensor_type
  513. * sd->do_lcd_stop
  514. * Aiptek Pencam VGA+ 0x31 0 1
  515. * ION digital 0x31 0 1
  516. * Argus DC-1620 0x30 1 0
  517. * Argus QuickClix 0x30 1 1 (not caught here)
  518. */
  519. sd->sensor_type = data[0] & 1;
  520. sd->do_lcd_stop = (~data[0]) & 1;
  521. /* Streaming setup begins here. */
  522. data[0] = 0x01;
  523. data[1] = 0x01;
  524. err_code = mr_write(gspca_dev, 2);
  525. if (err_code < 0)
  526. return err_code;
  527. /*
  528. * A second test can now resolve any remaining ambiguity in the
  529. * identification of the camera type,
  530. */
  531. if (!sd->sensor_type) {
  532. data[0] = get_sensor_id(gspca_dev);
  533. if (data[0] == 0x7f) {
  534. sd->sensor_type = 1;
  535. PDEBUG(D_ERR, "sensor_type corrected to 1");
  536. }
  537. msleep(200);
  538. }
  539. /*
  540. * Known VGA cameras.
  541. * This test is only run if the previous test returned 0x30, but
  542. * here is the information for all others, too, just for reference.
  543. *
  544. * Name byte just read sd->sensor_type
  545. *
  546. * Aiptek Pencam VGA+ 0xfb (this test not run) 1
  547. * ION digital 0xbd (this test not run) 1
  548. * Argus DC-1620 0xe5 (no change) 0
  549. * Argus QuickClix 0x7f (reclassified) 1
  550. */
  551. memcpy(data, startup_string, 11);
  552. if (!sd->sensor_type) {
  553. data[5] = 0x00;
  554. data[10] = 0x91;
  555. }
  556. switch (gspca_dev->width) {
  557. case 160:
  558. data[9] |= 0x0c; /* reg 8, 4:1 scale down */
  559. /* fall thru */
  560. case 320:
  561. data[9] |= 0x04; /* reg 8, 2:1 scale down */
  562. /* fall thru */
  563. case 640:
  564. default:
  565. data[3] = 0x50; /* reg 2, H size/8 */
  566. data[4] = 0x78; /* reg 3, V size/4 */
  567. data[6] = 0x04; /* reg 5, H start */
  568. data[8] = 0x03; /* reg 7, V start */
  569. if (sd->do_lcd_stop)
  570. data[8] = 0x04; /* Bayer tile shifted */
  571. break;
  572. case 176:
  573. data[9] |= 0x04; /* reg 8, 2:1 scale down */
  574. /* fall thru */
  575. case 352:
  576. data[3] = 0x2c; /* reg 2, H size */
  577. data[4] = 0x48; /* reg 3, V size */
  578. data[6] = 0x94; /* reg 5, H start */
  579. data[8] = 0x63; /* reg 7, V start */
  580. if (sd->do_lcd_stop)
  581. data[8] = 0x64; /* Bayer tile shifted */
  582. break;
  583. }
  584. err_code = mr_write(gspca_dev, 11);
  585. if (err_code < 0)
  586. return err_code;
  587. if (!sd->sensor_type) {
  588. /* The only known sensor_type 0 cam is the Argus DC-1620 */
  589. const struct sensor_w_data vga_sensor0_init_data[] = {
  590. {0x01, 0x00, {0x0c, 0x00, 0x04}, 3},
  591. {0x14, 0x00, {0x01, 0xe4, 0x02, 0x84}, 4},
  592. {0x20, 0x00, {0x00, 0x80, 0x00, 0x08}, 4},
  593. {0x25, 0x00, {0x03, 0xa9, 0x80}, 3},
  594. {0x30, 0x00, {0x30, 0x18, 0x10, 0x18}, 4},
  595. {0, 0, {0}, 0}
  596. };
  597. err_code = sensor_write_regs(gspca_dev, vga_sensor0_init_data,
  598. ARRAY_SIZE(vga_sensor0_init_data));
  599. } else { /* sd->sensor_type = 1 */
  600. const struct sensor_w_data vga_sensor1_init_data[] = {
  601. {0x02, 0x00, {0x06, 0x59, 0x0c, 0x16, 0x00,
  602. 0x07, 0x00, 0x01}, 8},
  603. {0x11, 0x04, {0x01}, 1},
  604. /*{0x0a, 0x00, {0x00, 0x01, 0x00, 0x00, 0x01, */
  605. {0x0a, 0x00, {0x01, 0x06, 0x00, 0x00, 0x01,
  606. 0x00, 0x0a}, 7},
  607. {0x11, 0x04, {0x01}, 1},
  608. {0x12, 0x00, {0x00, 0x63, 0x00, 0x70, 0x00, 0x00}, 6},
  609. {0x11, 0x04, {0x01}, 1},
  610. {0, 0, {0}, 0}
  611. };
  612. err_code = sensor_write_regs(gspca_dev, vga_sensor1_init_data,
  613. ARRAY_SIZE(vga_sensor1_init_data));
  614. }
  615. if (err_code < 0)
  616. return err_code;
  617. msleep(200);
  618. data[0] = 0x00;
  619. data[1] = 0x4d; /* ISOC transfering enable... */
  620. err_code = mr_write(gspca_dev, 2);
  621. return err_code;
  622. }
  623. static int sd_start(struct gspca_dev *gspca_dev)
  624. {
  625. struct sd *sd = (struct sd *) gspca_dev;
  626. int err_code;
  627. struct cam *cam;
  628. /* TEST TEST */
  629. int i;
  630. for (i = 2; i <= 14; i++)
  631. sd->regs[i] = sd_ctrls[i - 2].qctrl.default_value;
  632. cam = &gspca_dev->cam;
  633. sd->sof_read = 0;
  634. /*
  635. * Some of the supported cameras require the memory pointer to be
  636. * set to 0, or else they will not stream.
  637. */
  638. zero_the_pointer(gspca_dev);
  639. msleep(200);
  640. if (sd->cam_type == CAM_TYPE_CIF) {
  641. PDEBUG(D_ERR, "CIF camera");
  642. err_code = start_cif_cam(gspca_dev);
  643. } else {
  644. PDEBUG(D_ERR, "VGA camera");
  645. err_code = start_vga_cam(gspca_dev);
  646. }
  647. return err_code;
  648. }
  649. static void sd_stopN(struct gspca_dev *gspca_dev)
  650. {
  651. struct sd *sd = (struct sd *) gspca_dev;
  652. int result;
  653. gspca_dev->usb_buf[0] = 1;
  654. gspca_dev->usb_buf[1] = 0;
  655. result = mr_write(gspca_dev, 2);
  656. if (result < 0)
  657. PDEBUG(D_ERR, "Camera Stop failed");
  658. /* Not all the cams need this, but even if not, probably a good idea */
  659. zero_the_pointer(gspca_dev);
  660. if (sd->do_lcd_stop) {
  661. gspca_dev->usb_buf[0] = 0x19;
  662. gspca_dev->usb_buf[1] = 0x54;
  663. result = mr_write(gspca_dev, 2);
  664. if (result < 0)
  665. PDEBUG(D_ERR, "Camera Stop failed");
  666. }
  667. }
  668. static void setbrightness(struct gspca_dev *gspca_dev)
  669. {
  670. struct sd *sd = (struct sd *) gspca_dev;
  671. u8 val;
  672. if (sd->brightness > 0) {
  673. sensor_write1(gspca_dev, 7, 0);
  674. val = sd->brightness;
  675. } else {
  676. sensor_write1(gspca_dev, 7, 1);
  677. val = 257 - sd->brightness;
  678. }
  679. sensor_write1(gspca_dev, 8, val);
  680. }
  681. static void setexposure(struct gspca_dev *gspca_dev)
  682. {
  683. struct sd *sd = (struct sd *) gspca_dev;
  684. u8 val;
  685. val = sd->exposure >> 4;
  686. sensor_write1(gspca_dev, 3, val);
  687. val = sd->exposure & 0xf;
  688. sensor_write1(gspca_dev, 4, val);
  689. }
  690. static void setgain(struct gspca_dev *gspca_dev)
  691. {
  692. struct sd *sd = (struct sd *) gspca_dev;
  693. sensor_write1(gspca_dev, 3, sd->gain);
  694. }
  695. static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
  696. {
  697. struct sd *sd = (struct sd *) gspca_dev;
  698. sd->brightness = val;
  699. if (gspca_dev->streaming)
  700. setbrightness(gspca_dev);
  701. return 0;
  702. }
  703. static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
  704. {
  705. struct sd *sd = (struct sd *) gspca_dev;
  706. *val = sd->brightness;
  707. return 0;
  708. }
  709. static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val)
  710. {
  711. struct sd *sd = (struct sd *) gspca_dev;
  712. sd->exposure = val;
  713. if (gspca_dev->streaming)
  714. setexposure(gspca_dev);
  715. return 0;
  716. }
  717. static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val)
  718. {
  719. struct sd *sd = (struct sd *) gspca_dev;
  720. *val = sd->exposure;
  721. return 0;
  722. }
  723. static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val)
  724. {
  725. struct sd *sd = (struct sd *) gspca_dev;
  726. sd->gain = val;
  727. if (gspca_dev->streaming)
  728. setgain(gspca_dev);
  729. return 0;
  730. }
  731. static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val)
  732. {
  733. struct sd *sd = (struct sd *) gspca_dev;
  734. *val = sd->gain;
  735. return 0;
  736. }
  737. /* Include pac common sof detection functions */
  738. #include "pac_common.h"
  739. static void sd_pkt_scan(struct gspca_dev *gspca_dev,
  740. struct gspca_frame *frame, /* target */
  741. __u8 *data, /* isoc packet */
  742. int len) /* iso packet length */
  743. {
  744. unsigned char *sof;
  745. sof = pac_find_sof(gspca_dev, data, len);
  746. if (sof) {
  747. int n;
  748. /* finish decoding current frame */
  749. n = sof - data;
  750. if (n > sizeof pac_sof_marker)
  751. n -= sizeof pac_sof_marker;
  752. else
  753. n = 0;
  754. frame = gspca_frame_add(gspca_dev, LAST_PACKET, frame,
  755. data, n);
  756. /* Start next frame. */
  757. gspca_frame_add(gspca_dev, FIRST_PACKET, frame,
  758. pac_sof_marker, sizeof pac_sof_marker);
  759. len -= sof - data;
  760. data = sof;
  761. }
  762. gspca_frame_add(gspca_dev, INTER_PACKET, frame, data, len);
  763. }
  764. /* sub-driver description */
  765. static const struct sd_desc sd_desc = {
  766. .name = MODULE_NAME,
  767. .ctrls = sd_ctrls,
  768. .nctrls = ARRAY_SIZE(sd_ctrls),
  769. .config = sd_config,
  770. .init = sd_init,
  771. .start = sd_start,
  772. .stopN = sd_stopN,
  773. .pkt_scan = sd_pkt_scan,
  774. };
  775. /* -- module initialisation -- */
  776. static const __devinitdata struct usb_device_id device_table[] = {
  777. {USB_DEVICE(0x08ca, 0x0111)}, /* Aiptek Pencam VGA+ */
  778. {USB_DEVICE(0x093a, 0x010f)}, /* All other known MR97310A VGA cams */
  779. {USB_DEVICE(0x093a, 0x010e)}, /* All known MR97310A CIF cams */
  780. {}
  781. };
  782. MODULE_DEVICE_TABLE(usb, device_table);
  783. /* -- device connect -- */
  784. static int sd_probe(struct usb_interface *intf,
  785. const struct usb_device_id *id)
  786. {
  787. return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
  788. THIS_MODULE);
  789. }
  790. static struct usb_driver sd_driver = {
  791. .name = MODULE_NAME,
  792. .id_table = device_table,
  793. .probe = sd_probe,
  794. .disconnect = gspca_disconnect,
  795. #ifdef CONFIG_PM
  796. .suspend = gspca_suspend,
  797. .resume = gspca_resume,
  798. #endif
  799. };
  800. /* -- module insert / remove -- */
  801. static int __init sd_mod_init(void)
  802. {
  803. int ret;
  804. ret = usb_register(&sd_driver);
  805. if (ret < 0)
  806. return ret;
  807. PDEBUG(D_PROBE, "registered");
  808. return 0;
  809. }
  810. static void __exit sd_mod_exit(void)
  811. {
  812. usb_deregister(&sd_driver);
  813. PDEBUG(D_PROBE, "deregistered");
  814. }
  815. module_init(sd_mod_init);
  816. module_exit(sd_mod_exit);