mr97310a.c 22 KB

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