mr97310a.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985
  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. /* global parameters */
  53. int force_sensor_type = -1;
  54. module_param(force_sensor_type, int, 0644);
  55. MODULE_PARM_DESC(force_sensor_type, "Force sensor type (-1 (auto), 0 or 1)");
  56. /* specific webcam descriptor */
  57. struct sd {
  58. struct gspca_dev gspca_dev; /* !! must be the first item */
  59. u8 sof_read;
  60. u8 cam_type; /* 0 is CIF and 1 is VGA */
  61. u8 sensor_type; /* We use 0 and 1 here, too. */
  62. u8 do_lcd_stop;
  63. int brightness;
  64. u16 exposure;
  65. u8 gain;
  66. };
  67. struct sensor_w_data {
  68. u8 reg;
  69. u8 flags;
  70. u8 data[16];
  71. int len;
  72. };
  73. static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
  74. static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
  75. static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val);
  76. static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val);
  77. static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val);
  78. static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val);
  79. static void setbrightness(struct gspca_dev *gspca_dev);
  80. static void setexposure(struct gspca_dev *gspca_dev);
  81. static void setgain(struct gspca_dev *gspca_dev);
  82. /* V4L2 controls supported by the driver */
  83. static struct ctrl sd_ctrls[] = {
  84. {
  85. #define BRIGHTNESS_IDX 0
  86. {
  87. .id = V4L2_CID_BRIGHTNESS,
  88. .type = V4L2_CTRL_TYPE_INTEGER,
  89. .name = "Brightness",
  90. .minimum = MR97310A_BRIGHTNESS_MIN,
  91. .maximum = MR97310A_BRIGHTNESS_MAX,
  92. .step = 1,
  93. .default_value = MR97310A_BRIGHTNESS_DEFAULT,
  94. .flags = 0,
  95. },
  96. .set = sd_setbrightness,
  97. .get = sd_getbrightness,
  98. },
  99. {
  100. #define EXPOSURE_IDX 1
  101. {
  102. .id = V4L2_CID_EXPOSURE,
  103. .type = V4L2_CTRL_TYPE_INTEGER,
  104. .name = "Exposure",
  105. .minimum = MR97310A_EXPOSURE_MIN,
  106. .maximum = MR97310A_EXPOSURE_MAX,
  107. .step = 1,
  108. .default_value = MR97310A_EXPOSURE_DEFAULT,
  109. .flags = 0,
  110. },
  111. .set = sd_setexposure,
  112. .get = sd_getexposure,
  113. },
  114. {
  115. #define GAIN_IDX 2
  116. {
  117. .id = V4L2_CID_GAIN,
  118. .type = V4L2_CTRL_TYPE_INTEGER,
  119. .name = "Gain",
  120. .minimum = MR97310A_GAIN_MIN,
  121. .maximum = MR97310A_GAIN_MAX,
  122. .step = 1,
  123. .default_value = MR97310A_GAIN_DEFAULT,
  124. .flags = 0,
  125. },
  126. .set = sd_setgain,
  127. .get = sd_getgain,
  128. },
  129. };
  130. static const struct v4l2_pix_format vga_mode[] = {
  131. {160, 120, V4L2_PIX_FMT_MR97310A, V4L2_FIELD_NONE,
  132. .bytesperline = 160,
  133. .sizeimage = 160 * 120,
  134. .colorspace = V4L2_COLORSPACE_SRGB,
  135. .priv = 4},
  136. {176, 144, V4L2_PIX_FMT_MR97310A, V4L2_FIELD_NONE,
  137. .bytesperline = 176,
  138. .sizeimage = 176 * 144,
  139. .colorspace = V4L2_COLORSPACE_SRGB,
  140. .priv = 3},
  141. {320, 240, V4L2_PIX_FMT_MR97310A, V4L2_FIELD_NONE,
  142. .bytesperline = 320,
  143. .sizeimage = 320 * 240,
  144. .colorspace = V4L2_COLORSPACE_SRGB,
  145. .priv = 2},
  146. {352, 288, V4L2_PIX_FMT_MR97310A, V4L2_FIELD_NONE,
  147. .bytesperline = 352,
  148. .sizeimage = 352 * 288,
  149. .colorspace = V4L2_COLORSPACE_SRGB,
  150. .priv = 1},
  151. {640, 480, V4L2_PIX_FMT_MR97310A, V4L2_FIELD_NONE,
  152. .bytesperline = 640,
  153. .sizeimage = 640 * 480,
  154. .colorspace = V4L2_COLORSPACE_SRGB,
  155. .priv = 0},
  156. };
  157. /* the bytes to write are in gspca_dev->usb_buf */
  158. static int mr_write(struct gspca_dev *gspca_dev, int len)
  159. {
  160. int rc;
  161. rc = usb_bulk_msg(gspca_dev->dev,
  162. usb_sndbulkpipe(gspca_dev->dev, 4),
  163. gspca_dev->usb_buf, len, NULL, 500);
  164. if (rc < 0)
  165. PDEBUG(D_ERR, "reg write [%02x] error %d",
  166. gspca_dev->usb_buf[0], rc);
  167. return rc;
  168. }
  169. /* the bytes are read into gspca_dev->usb_buf */
  170. static int mr_read(struct gspca_dev *gspca_dev, int len)
  171. {
  172. int rc;
  173. rc = usb_bulk_msg(gspca_dev->dev,
  174. usb_rcvbulkpipe(gspca_dev->dev, 3),
  175. gspca_dev->usb_buf, len, NULL, 500);
  176. if (rc < 0)
  177. PDEBUG(D_ERR, "reg read [%02x] error %d",
  178. gspca_dev->usb_buf[0], rc);
  179. return rc;
  180. }
  181. static int sensor_write_reg(struct gspca_dev *gspca_dev, u8 reg, u8 flags,
  182. const u8 *data, int len)
  183. {
  184. gspca_dev->usb_buf[0] = 0x1f;
  185. gspca_dev->usb_buf[1] = flags;
  186. gspca_dev->usb_buf[2] = reg;
  187. memcpy(gspca_dev->usb_buf + 3, data, len);
  188. return mr_write(gspca_dev, len + 3);
  189. }
  190. static int sensor_write_regs(struct gspca_dev *gspca_dev,
  191. const struct sensor_w_data *data, int len)
  192. {
  193. int i, rc;
  194. for (i = 0; i < len; i++) {
  195. rc = sensor_write_reg(gspca_dev, data[i].reg, data[i].flags,
  196. data[i].data, data[i].len);
  197. if (rc < 0)
  198. return rc;
  199. }
  200. return 0;
  201. }
  202. static int sensor_write1(struct gspca_dev *gspca_dev, u8 reg, u8 data)
  203. {
  204. struct sd *sd = (struct sd *) gspca_dev;
  205. u8 buf, confirm_reg;
  206. int rc;
  207. buf = data;
  208. rc = sensor_write_reg(gspca_dev, reg, 0x01, &buf, 1);
  209. if (rc < 0)
  210. return rc;
  211. buf = 0x01;
  212. confirm_reg = sd->sensor_type ? 0x13 : 0x11;
  213. rc = sensor_write_reg(gspca_dev, confirm_reg, 0x00, &buf, 1);
  214. if (rc < 0)
  215. return rc;
  216. return 0;
  217. }
  218. static int cam_get_response16(struct gspca_dev *gspca_dev)
  219. {
  220. __u8 *data = gspca_dev->usb_buf;
  221. int err_code;
  222. data[0] = 0x21;
  223. err_code = mr_write(gspca_dev, 1);
  224. if (err_code < 0)
  225. return err_code;
  226. err_code = mr_read(gspca_dev, 16);
  227. return err_code;
  228. }
  229. static int zero_the_pointer(struct gspca_dev *gspca_dev)
  230. {
  231. __u8 *data = gspca_dev->usb_buf;
  232. int err_code;
  233. u8 status = 0;
  234. int tries = 0;
  235. err_code = cam_get_response16(gspca_dev);
  236. if (err_code < 0)
  237. return err_code;
  238. err_code = mr_write(gspca_dev, 1);
  239. data[0] = 0x19;
  240. data[1] = 0x51;
  241. err_code = mr_write(gspca_dev, 2);
  242. if (err_code < 0)
  243. return err_code;
  244. err_code = cam_get_response16(gspca_dev);
  245. if (err_code < 0)
  246. return err_code;
  247. data[0] = 0x19;
  248. data[1] = 0xba;
  249. err_code = mr_write(gspca_dev, 2);
  250. if (err_code < 0)
  251. return err_code;
  252. err_code = cam_get_response16(gspca_dev);
  253. if (err_code < 0)
  254. return err_code;
  255. data[0] = 0x19;
  256. data[1] = 0x00;
  257. err_code = mr_write(gspca_dev, 2);
  258. if (err_code < 0)
  259. return err_code;
  260. err_code = cam_get_response16(gspca_dev);
  261. if (err_code < 0)
  262. return err_code;
  263. data[0] = 0x19;
  264. data[1] = 0x00;
  265. err_code = mr_write(gspca_dev, 2);
  266. if (err_code < 0)
  267. return err_code;
  268. while (status != 0x0a && tries < 256) {
  269. err_code = cam_get_response16(gspca_dev);
  270. status = data[0];
  271. tries++;
  272. if (err_code < 0)
  273. return err_code;
  274. }
  275. if (status != 0x0a)
  276. PDEBUG(D_ERR, "status is %02x", status);
  277. tries = 0;
  278. while (tries < 4) {
  279. data[0] = 0x19;
  280. data[1] = 0x00;
  281. err_code = mr_write(gspca_dev, 2);
  282. if (err_code < 0)
  283. return err_code;
  284. err_code = cam_get_response16(gspca_dev);
  285. status = data[0];
  286. tries++;
  287. if (err_code < 0)
  288. return err_code;
  289. }
  290. data[0] = 0x19;
  291. err_code = mr_write(gspca_dev, 1);
  292. if (err_code < 0)
  293. return err_code;
  294. err_code = mr_read(gspca_dev, 16);
  295. if (err_code < 0)
  296. return err_code;
  297. return 0;
  298. }
  299. static u8 get_sensor_id(struct gspca_dev *gspca_dev)
  300. {
  301. int err_code;
  302. gspca_dev->usb_buf[0] = 0x1e;
  303. err_code = mr_write(gspca_dev, 1);
  304. if (err_code < 0)
  305. return err_code;
  306. err_code = mr_read(gspca_dev, 16);
  307. if (err_code < 0)
  308. return err_code;
  309. PDEBUG(D_PROBE, "Byte zero reported is %01x", gspca_dev->usb_buf[0]);
  310. return gspca_dev->usb_buf[0];
  311. }
  312. /* this function is called at probe time */
  313. static int sd_config(struct gspca_dev *gspca_dev,
  314. const struct usb_device_id *id)
  315. {
  316. struct sd *sd = (struct sd *) gspca_dev;
  317. struct cam *cam;
  318. __u8 *data = gspca_dev->usb_buf;
  319. int err_code;
  320. cam = &gspca_dev->cam;
  321. cam->cam_mode = vga_mode;
  322. cam->nmodes = ARRAY_SIZE(vga_mode);
  323. if (id->idProduct == 0x010e) {
  324. sd->cam_type = CAM_TYPE_CIF;
  325. cam->nmodes--;
  326. data[0] = 0x01;
  327. data[1] = 0x01;
  328. err_code = mr_write(gspca_dev, 2);
  329. if (err_code < 0)
  330. return err_code;
  331. msleep(200);
  332. data[0] = get_sensor_id(gspca_dev);
  333. /*
  334. * Known CIF cameras. If you have another to report, please do
  335. *
  336. * Name byte just read sd->sensor_type
  337. * reported by
  338. * Sakar Spy-shot 0x28 T. Kilgore 0
  339. * Innovage 0xf5 (unstable) T. Kilgore 0
  340. * Vivitar Mini 0x53 H. De Goede 0
  341. * Vivitar Mini 0x04 / 0x24 E. Rodriguez 0
  342. * Vivitar Mini 0x08 T. Kilgore 1
  343. * Elta-Media 8212dc 0x23 T. Kaiser 1
  344. * Philips dig. keych. 0x37 T. Kilgore 1
  345. */
  346. if ((data[0] & 0x78) == 8 ||
  347. ((data[0] & 0x2) == 0x2 && data[0] != 0x53))
  348. sd->sensor_type = 1;
  349. else
  350. sd->sensor_type = 0;
  351. PDEBUG(D_PROBE, "MR97310A CIF camera detected, sensor: %d",
  352. sd->sensor_type);
  353. if (force_sensor_type != -1) {
  354. sd->sensor_type = !! force_sensor_type;
  355. PDEBUG(D_PROBE, "Forcing sensor type to: %d",
  356. sd->sensor_type);
  357. }
  358. if (sd->sensor_type == 0)
  359. gspca_dev->ctrl_dis = (1 << BRIGHTNESS_IDX);
  360. } else {
  361. sd->cam_type = CAM_TYPE_VGA;
  362. PDEBUG(D_PROBE, "MR97310A VGA camera detected");
  363. gspca_dev->ctrl_dis = (1 << BRIGHTNESS_IDX) |
  364. (1 << EXPOSURE_IDX) | (1 << GAIN_IDX);
  365. }
  366. sd->brightness = MR97310A_BRIGHTNESS_DEFAULT;
  367. sd->exposure = MR97310A_EXPOSURE_DEFAULT;
  368. sd->gain = MR97310A_GAIN_DEFAULT;
  369. return 0;
  370. }
  371. /* this function is called at probe and resume time */
  372. static int sd_init(struct gspca_dev *gspca_dev)
  373. {
  374. return 0;
  375. }
  376. static int start_cif_cam(struct gspca_dev *gspca_dev)
  377. {
  378. struct sd *sd = (struct sd *) gspca_dev;
  379. __u8 *data = gspca_dev->usb_buf;
  380. int err_code;
  381. const __u8 startup_string[] = {
  382. 0x00,
  383. 0x0d,
  384. 0x01,
  385. 0x00, /* Hsize/8 for 352 or 320 */
  386. 0x00, /* Vsize/4 for 288 or 240 */
  387. 0x13, /* or 0xbb, depends on sensor */
  388. 0x00, /* Hstart, depends on res. */
  389. 0x00, /* reserved ? */
  390. 0x00, /* Vstart, depends on res. and sensor */
  391. 0x50, /* 0x54 to get 176 or 160 */
  392. 0xc0
  393. };
  394. /* Note: Some of the above descriptions guessed from MR97113A driver */
  395. data[0] = 0x01;
  396. data[1] = 0x01;
  397. err_code = mr_write(gspca_dev, 2);
  398. if (err_code < 0)
  399. return err_code;
  400. memcpy(data, startup_string, 11);
  401. if (sd->sensor_type)
  402. data[5] = 0xbb;
  403. switch (gspca_dev->width) {
  404. case 160:
  405. data[9] |= 0x04; /* reg 8, 2:1 scale down from 320 */
  406. /* fall thru */
  407. case 320:
  408. default:
  409. data[3] = 0x28; /* reg 2, H size/8 */
  410. data[4] = 0x3c; /* reg 3, V size/4 */
  411. data[6] = 0x14; /* reg 5, H start */
  412. data[8] = 0x1a + sd->sensor_type; /* reg 7, V start */
  413. break;
  414. case 176:
  415. data[9] |= 0x04; /* reg 8, 2:1 scale down from 352 */
  416. /* fall thru */
  417. case 352:
  418. data[3] = 0x2c; /* reg 2, H size/8 */
  419. data[4] = 0x48; /* reg 3, V size/4 */
  420. data[6] = 0x06; /* reg 5, H start */
  421. data[8] = 0x06 - sd->sensor_type; /* reg 7, V start */
  422. break;
  423. }
  424. err_code = mr_write(gspca_dev, 11);
  425. if (err_code < 0)
  426. return err_code;
  427. if (!sd->sensor_type) {
  428. const struct sensor_w_data cif_sensor0_init_data[] = {
  429. {0x02, 0x00, {0x03, 0x5a, 0xb5, 0x01,
  430. 0x0f, 0x14, 0x0f, 0x10}, 8},
  431. {0x0c, 0x00, {0x04, 0x01, 0x01, 0x00, 0x1f}, 5},
  432. {0x12, 0x00, {0x07}, 1},
  433. {0x1f, 0x00, {0x06}, 1},
  434. {0x27, 0x00, {0x04}, 1},
  435. {0x29, 0x00, {0x0c}, 1},
  436. {0x40, 0x00, {0x40, 0x00, 0x04}, 3},
  437. {0x50, 0x00, {0x60}, 1},
  438. {0x60, 0x00, {0x06}, 1},
  439. {0x6b, 0x00, {0x85, 0x85, 0xc8, 0xc8, 0xc8, 0xc8}, 6},
  440. {0x72, 0x00, {0x1e, 0x56}, 2},
  441. {0x75, 0x00, {0x58, 0x40, 0xa2, 0x02, 0x31, 0x02,
  442. 0x31, 0x80, 0x00}, 9},
  443. {0x11, 0x00, {0x01}, 1},
  444. {0, 0, {0}, 0}
  445. };
  446. err_code = sensor_write_regs(gspca_dev, cif_sensor0_init_data,
  447. ARRAY_SIZE(cif_sensor0_init_data));
  448. } else { /* sd->sensor_type = 1 */
  449. const struct sensor_w_data cif_sensor1_init_data[] = {
  450. /* Reg 3,4, 7,8 get set by the controls */
  451. {0x02, 0x00, {0x10}, 1},
  452. {0x05, 0x01, {0x22}, 1}, /* 5/6 also seen as 65h/32h */
  453. {0x06, 0x01, {0x00}, 1},
  454. {0x09, 0x02, {0x0e}, 1},
  455. {0x0a, 0x02, {0x05}, 1},
  456. {0x0b, 0x02, {0x05}, 1},
  457. {0x0c, 0x02, {0x0f}, 1},
  458. {0x0d, 0x02, {0x07}, 1},
  459. {0x0e, 0x02, {0x0c}, 1},
  460. {0x0f, 0x00, {0x00}, 1},
  461. {0x10, 0x00, {0x06}, 1},
  462. {0x11, 0x00, {0x07}, 1},
  463. {0x12, 0x00, {0x00}, 1},
  464. {0x13, 0x00, {0x01}, 1},
  465. {0, 0, {0}, 0}
  466. };
  467. err_code = sensor_write_regs(gspca_dev, cif_sensor1_init_data,
  468. ARRAY_SIZE(cif_sensor1_init_data));
  469. }
  470. if (err_code < 0)
  471. return err_code;
  472. setbrightness(gspca_dev);
  473. setexposure(gspca_dev);
  474. setgain(gspca_dev);
  475. msleep(200);
  476. data[0] = 0x00;
  477. data[1] = 0x4d; /* ISOC transfering enable... */
  478. err_code = mr_write(gspca_dev, 2);
  479. if (err_code < 0)
  480. return err_code;
  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_PROBE, "Byte reported is %02x", data[0]);
  507. msleep(200);
  508. /*
  509. * Known VGA cameras. If you have another to report, please do
  510. *
  511. * Name byte just read sd->sensor_type
  512. * sd->do_lcd_stop
  513. * Aiptek Pencam VGA+ 0x31 0 1
  514. * ION digital 0x31 0 1
  515. * Argus DC-1620 0x30 1 0
  516. * Argus QuickClix 0x30 1 1 (not caught here)
  517. */
  518. sd->sensor_type = data[0] & 1;
  519. sd->do_lcd_stop = (~data[0]) & 1;
  520. /* Streaming setup begins here. */
  521. data[0] = 0x01;
  522. data[1] = 0x01;
  523. err_code = mr_write(gspca_dev, 2);
  524. if (err_code < 0)
  525. return err_code;
  526. /*
  527. * A second test can now resolve any remaining ambiguity in the
  528. * identification of the camera type,
  529. */
  530. if (!sd->sensor_type) {
  531. data[0] = get_sensor_id(gspca_dev);
  532. if (data[0] == 0x7f) {
  533. sd->sensor_type = 1;
  534. PDEBUG(D_PROBE, "sensor_type corrected to 1");
  535. }
  536. msleep(200);
  537. }
  538. if (force_sensor_type != -1) {
  539. sd->sensor_type = !! force_sensor_type;
  540. PDEBUG(D_PROBE, "Forcing sensor type to: %d",
  541. sd->sensor_type);
  542. }
  543. /*
  544. * Known VGA cameras.
  545. * This test is only run if the previous test returned 0x30, but
  546. * here is the information for all others, too, just for reference.
  547. *
  548. * Name byte just read sd->sensor_type
  549. *
  550. * Aiptek Pencam VGA+ 0xfb (this test not run) 1
  551. * ION digital 0xbd (this test not run) 1
  552. * Argus DC-1620 0xe5 (no change) 0
  553. * Argus QuickClix 0x7f (reclassified) 1
  554. */
  555. memcpy(data, startup_string, 11);
  556. if (!sd->sensor_type) {
  557. data[5] = 0x00;
  558. data[10] = 0x91;
  559. }
  560. switch (gspca_dev->width) {
  561. case 160:
  562. data[9] |= 0x0c; /* reg 8, 4:1 scale down */
  563. /* fall thru */
  564. case 320:
  565. data[9] |= 0x04; /* reg 8, 2:1 scale down */
  566. /* fall thru */
  567. case 640:
  568. default:
  569. data[3] = 0x50; /* reg 2, H size/8 */
  570. data[4] = 0x78; /* reg 3, V size/4 */
  571. data[6] = 0x04; /* reg 5, H start */
  572. data[8] = 0x03; /* reg 7, V start */
  573. if (sd->do_lcd_stop)
  574. data[8] = 0x04; /* Bayer tile shifted */
  575. break;
  576. case 176:
  577. data[9] |= 0x04; /* reg 8, 2:1 scale down */
  578. /* fall thru */
  579. case 352:
  580. data[3] = 0x2c; /* reg 2, H size */
  581. data[4] = 0x48; /* reg 3, V size */
  582. data[6] = 0x94; /* reg 5, H start */
  583. data[8] = 0x63; /* reg 7, V start */
  584. if (sd->do_lcd_stop)
  585. data[8] = 0x64; /* Bayer tile shifted */
  586. break;
  587. }
  588. err_code = mr_write(gspca_dev, 11);
  589. if (err_code < 0)
  590. return err_code;
  591. if (!sd->sensor_type) {
  592. /* The only known sensor_type 0 cam is the Argus DC-1620 */
  593. const struct sensor_w_data vga_sensor0_init_data[] = {
  594. {0x01, 0x00, {0x0c, 0x00, 0x04}, 3},
  595. {0x14, 0x00, {0x01, 0xe4, 0x02, 0x84}, 4},
  596. {0x20, 0x00, {0x00, 0x80, 0x00, 0x08}, 4},
  597. {0x25, 0x00, {0x03, 0xa9, 0x80}, 3},
  598. {0x30, 0x00, {0x30, 0x18, 0x10, 0x18}, 4},
  599. {0, 0, {0}, 0}
  600. };
  601. err_code = sensor_write_regs(gspca_dev, vga_sensor0_init_data,
  602. ARRAY_SIZE(vga_sensor0_init_data));
  603. } else { /* sd->sensor_type = 1 */
  604. const struct sensor_w_data vga_sensor1_init_data[] = {
  605. {0x02, 0x00, {0x06, 0x59, 0x0c, 0x16, 0x00,
  606. 0x07, 0x00, 0x01}, 8},
  607. {0x11, 0x04, {0x01}, 1},
  608. /*{0x0a, 0x00, {0x00, 0x01, 0x00, 0x00, 0x01, */
  609. {0x0a, 0x00, {0x01, 0x06, 0x00, 0x00, 0x01,
  610. 0x00, 0x0a}, 7},
  611. {0x11, 0x04, {0x01}, 1},
  612. {0x12, 0x00, {0x00, 0x63, 0x00, 0x70, 0x00, 0x00}, 6},
  613. {0x11, 0x04, {0x01}, 1},
  614. {0, 0, {0}, 0}
  615. };
  616. err_code = sensor_write_regs(gspca_dev, vga_sensor1_init_data,
  617. ARRAY_SIZE(vga_sensor1_init_data));
  618. }
  619. if (err_code < 0)
  620. return err_code;
  621. msleep(200);
  622. data[0] = 0x00;
  623. data[1] = 0x4d; /* ISOC transfering enable... */
  624. err_code = mr_write(gspca_dev, 2);
  625. return err_code;
  626. }
  627. static int sd_start(struct gspca_dev *gspca_dev)
  628. {
  629. struct sd *sd = (struct sd *) gspca_dev;
  630. int err_code;
  631. struct cam *cam;
  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. err_code = start_cif_cam(gspca_dev);
  642. } else {
  643. err_code = start_vga_cam(gspca_dev);
  644. }
  645. return err_code;
  646. }
  647. static void sd_stopN(struct gspca_dev *gspca_dev)
  648. {
  649. struct sd *sd = (struct sd *) gspca_dev;
  650. int result;
  651. gspca_dev->usb_buf[0] = 1;
  652. gspca_dev->usb_buf[1] = 0;
  653. result = mr_write(gspca_dev, 2);
  654. if (result < 0)
  655. PDEBUG(D_ERR, "Camera Stop failed");
  656. /* Not all the cams need this, but even if not, probably a good idea */
  657. zero_the_pointer(gspca_dev);
  658. if (sd->do_lcd_stop) {
  659. gspca_dev->usb_buf[0] = 0x19;
  660. gspca_dev->usb_buf[1] = 0x54;
  661. result = mr_write(gspca_dev, 2);
  662. if (result < 0)
  663. PDEBUG(D_ERR, "Camera Stop failed");
  664. }
  665. }
  666. static void setbrightness(struct gspca_dev *gspca_dev)
  667. {
  668. struct sd *sd = (struct sd *) gspca_dev;
  669. u8 val;
  670. if (gspca_dev->ctrl_dis & (1 << BRIGHTNESS_IDX))
  671. return;
  672. /* Note register 7 is also seen as 0x8x or 0xCx in dumps */
  673. if (sd->brightness > 0) {
  674. sensor_write1(gspca_dev, 7, 0x00);
  675. val = sd->brightness;
  676. } else {
  677. sensor_write1(gspca_dev, 7, 0x01);
  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. if (gspca_dev->ctrl_dis & (1 << EXPOSURE_IDX))
  687. return;
  688. if (sd->sensor_type) {
  689. val = sd->exposure >> 4;
  690. sensor_write1(gspca_dev, 3, val);
  691. val = sd->exposure & 0xf;
  692. sensor_write1(gspca_dev, 4, val);
  693. } else {
  694. u8 clockdiv;
  695. int exposure;
  696. /* We have both a clock divider and an exposure register.
  697. We first calculate the clock divider, as that determines
  698. the maximum exposure and then we calculayte the exposure
  699. register setting (which goes from 0 - 511).
  700. Note our 0 - 4095 exposure is mapped to 0 - 511
  701. milliseconds exposure time */
  702. clockdiv = (60 * sd->exposure + 7999) / 8000;
  703. /* Limit framerate to not exceed usb bandwidth */
  704. if (clockdiv < 3 && gspca_dev->width >= 320)
  705. clockdiv = 3;
  706. else if (clockdiv < 2)
  707. clockdiv = 2;
  708. /* Frame exposure time in ms = 1000 * clockdiv / 60 ->
  709. exposure = (sd->exposure / 8) * 511 / (1000 * clockdiv / 60) */
  710. exposure = (60 * 511 * sd->exposure) / (8000 * clockdiv);
  711. if (exposure > 511)
  712. exposure = 511;
  713. /* exposure register value is reversed! */
  714. exposure = 511 - exposure;
  715. sensor_write1(gspca_dev, 0x02, clockdiv);
  716. sensor_write1(gspca_dev, 0x0e, exposure & 0xff);
  717. sensor_write1(gspca_dev, 0x0f, exposure >> 8);
  718. }
  719. }
  720. static void setgain(struct gspca_dev *gspca_dev)
  721. {
  722. struct sd *sd = (struct sd *) gspca_dev;
  723. if (gspca_dev->ctrl_dis & (1 << GAIN_IDX))
  724. return;
  725. if (sd->sensor_type) {
  726. sensor_write1(gspca_dev, 0x0e, sd->gain);
  727. } else {
  728. sensor_write1(gspca_dev, 0x10, sd->gain);
  729. }
  730. }
  731. static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
  732. {
  733. struct sd *sd = (struct sd *) gspca_dev;
  734. sd->brightness = val;
  735. if (gspca_dev->streaming)
  736. setbrightness(gspca_dev);
  737. return 0;
  738. }
  739. static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
  740. {
  741. struct sd *sd = (struct sd *) gspca_dev;
  742. *val = sd->brightness;
  743. return 0;
  744. }
  745. static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val)
  746. {
  747. struct sd *sd = (struct sd *) gspca_dev;
  748. sd->exposure = val;
  749. if (gspca_dev->streaming)
  750. setexposure(gspca_dev);
  751. return 0;
  752. }
  753. static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val)
  754. {
  755. struct sd *sd = (struct sd *) gspca_dev;
  756. *val = sd->exposure;
  757. return 0;
  758. }
  759. static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val)
  760. {
  761. struct sd *sd = (struct sd *) gspca_dev;
  762. sd->gain = val;
  763. if (gspca_dev->streaming)
  764. setgain(gspca_dev);
  765. return 0;
  766. }
  767. static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val)
  768. {
  769. struct sd *sd = (struct sd *) gspca_dev;
  770. *val = sd->gain;
  771. return 0;
  772. }
  773. /* Include pac common sof detection functions */
  774. #include "pac_common.h"
  775. static void sd_pkt_scan(struct gspca_dev *gspca_dev,
  776. struct gspca_frame *frame, /* target */
  777. __u8 *data, /* isoc packet */
  778. int len) /* iso packet length */
  779. {
  780. unsigned char *sof;
  781. sof = pac_find_sof(gspca_dev, data, len);
  782. if (sof) {
  783. int n;
  784. /* finish decoding current frame */
  785. n = sof - data;
  786. if (n > sizeof pac_sof_marker)
  787. n -= sizeof pac_sof_marker;
  788. else
  789. n = 0;
  790. frame = gspca_frame_add(gspca_dev, LAST_PACKET, frame,
  791. data, n);
  792. /* Start next frame. */
  793. gspca_frame_add(gspca_dev, FIRST_PACKET, frame,
  794. pac_sof_marker, sizeof pac_sof_marker);
  795. len -= sof - data;
  796. data = sof;
  797. }
  798. gspca_frame_add(gspca_dev, INTER_PACKET, frame, data, len);
  799. }
  800. /* sub-driver description */
  801. static const struct sd_desc sd_desc = {
  802. .name = MODULE_NAME,
  803. .ctrls = sd_ctrls,
  804. .nctrls = ARRAY_SIZE(sd_ctrls),
  805. .config = sd_config,
  806. .init = sd_init,
  807. .start = sd_start,
  808. .stopN = sd_stopN,
  809. .pkt_scan = sd_pkt_scan,
  810. };
  811. /* -- module initialisation -- */
  812. static const __devinitdata struct usb_device_id device_table[] = {
  813. {USB_DEVICE(0x08ca, 0x0111)}, /* Aiptek Pencam VGA+ */
  814. {USB_DEVICE(0x093a, 0x010f)}, /* All other known MR97310A VGA cams */
  815. {USB_DEVICE(0x093a, 0x010e)}, /* All known MR97310A CIF cams */
  816. {}
  817. };
  818. MODULE_DEVICE_TABLE(usb, device_table);
  819. /* -- device connect -- */
  820. static int sd_probe(struct usb_interface *intf,
  821. const struct usb_device_id *id)
  822. {
  823. return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
  824. THIS_MODULE);
  825. }
  826. static struct usb_driver sd_driver = {
  827. .name = MODULE_NAME,
  828. .id_table = device_table,
  829. .probe = sd_probe,
  830. .disconnect = gspca_disconnect,
  831. #ifdef CONFIG_PM
  832. .suspend = gspca_suspend,
  833. .resume = gspca_resume,
  834. #endif
  835. };
  836. /* -- module insert / remove -- */
  837. static int __init sd_mod_init(void)
  838. {
  839. int ret;
  840. ret = usb_register(&sd_driver);
  841. if (ret < 0)
  842. return ret;
  843. PDEBUG(D_PROBE, "registered");
  844. return 0;
  845. }
  846. static void __exit sd_mod_exit(void)
  847. {
  848. usb_deregister(&sd_driver);
  849. PDEBUG(D_PROBE, "deregistered");
  850. }
  851. module_init(sd_mod_init);
  852. module_exit(sd_mod_exit);