spca561.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038
  1. /*
  2. * Sunplus spca561 subdriver
  3. *
  4. * Copyright (C) 2004 Michel Xhaard mxhaard@magic.fr
  5. *
  6. * V4L2 by Jean-Francois Moine <http://moinejf.free.fr>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #define MODULE_NAME "spca561"
  23. #include "gspca.h"
  24. MODULE_AUTHOR("Michel Xhaard <mxhaard@users.sourceforge.net>");
  25. MODULE_DESCRIPTION("GSPCA/SPCA561 USB Camera Driver");
  26. MODULE_LICENSE("GPL");
  27. /* specific webcam descriptor */
  28. struct sd {
  29. struct gspca_dev gspca_dev; /* !! must be the first item */
  30. unsigned short contrast;
  31. __u8 brightness;
  32. __u8 autogain;
  33. __u8 chip_revision;
  34. signed char ag_cnt;
  35. #define AG_CNT_START 13
  36. };
  37. /* V4L2 controls supported by the driver */
  38. static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
  39. static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
  40. static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val);
  41. static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val);
  42. static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val);
  43. static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val);
  44. static struct ctrl sd_ctrls[] = {
  45. #define SD_BRIGHTNESS 0
  46. {
  47. {
  48. .id = V4L2_CID_BRIGHTNESS,
  49. .type = V4L2_CTRL_TYPE_INTEGER,
  50. .name = "Brightness",
  51. .minimum = 0,
  52. .maximum = 63,
  53. .step = 1,
  54. .default_value = 32,
  55. },
  56. .set = sd_setbrightness,
  57. .get = sd_getbrightness,
  58. },
  59. #define SD_CONTRAST 1
  60. {
  61. {
  62. .id = V4L2_CID_CONTRAST,
  63. .type = V4L2_CTRL_TYPE_INTEGER,
  64. .name = "Contrast",
  65. .minimum = 0,
  66. .maximum = 0x3fff,
  67. .step = 1,
  68. .default_value = 0x2000,
  69. },
  70. .set = sd_setcontrast,
  71. .get = sd_getcontrast,
  72. },
  73. #define SD_AUTOGAIN 2
  74. {
  75. {
  76. .id = V4L2_CID_AUTOGAIN,
  77. .type = V4L2_CTRL_TYPE_BOOLEAN,
  78. .name = "Auto Gain",
  79. .minimum = 0,
  80. .maximum = 1,
  81. .step = 1,
  82. .default_value = 1,
  83. },
  84. .set = sd_setautogain,
  85. .get = sd_getautogain,
  86. },
  87. };
  88. static struct v4l2_pix_format sif_mode[] = {
  89. {160, 120, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
  90. .bytesperline = 160,
  91. .sizeimage = 160 * 120,
  92. .colorspace = V4L2_COLORSPACE_SRGB,
  93. .priv = 3},
  94. {176, 144, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
  95. .bytesperline = 176,
  96. .sizeimage = 176 * 144,
  97. .colorspace = V4L2_COLORSPACE_SRGB,
  98. .priv = 2},
  99. {320, 240, V4L2_PIX_FMT_SPCA561, V4L2_FIELD_NONE,
  100. .bytesperline = 320,
  101. .sizeimage = 320 * 240 * 4 / 8,
  102. .colorspace = V4L2_COLORSPACE_SRGB,
  103. .priv = 1},
  104. {352, 288, V4L2_PIX_FMT_SPCA561, V4L2_FIELD_NONE,
  105. .bytesperline = 352,
  106. .sizeimage = 352 * 288 * 4 / 8,
  107. .colorspace = V4L2_COLORSPACE_SRGB,
  108. .priv = 0},
  109. };
  110. /*
  111. * Initialization data
  112. * I'm not very sure how to split initialization from open data
  113. * chunks. For now, we'll consider everything as initialization
  114. */
  115. /* Frame packet header offsets for the spca561 */
  116. #define SPCA561_OFFSET_SNAP 1
  117. #define SPCA561_OFFSET_TYPE 2
  118. #define SPCA561_OFFSET_COMPRESS 3
  119. #define SPCA561_OFFSET_FRAMSEQ 4
  120. #define SPCA561_OFFSET_GPIO 5
  121. #define SPCA561_OFFSET_USBBUFF 6
  122. #define SPCA561_OFFSET_WIN2GRAVE 7
  123. #define SPCA561_OFFSET_WIN2RAVE 8
  124. #define SPCA561_OFFSET_WIN2BAVE 9
  125. #define SPCA561_OFFSET_WIN2GBAVE 10
  126. #define SPCA561_OFFSET_WIN1GRAVE 11
  127. #define SPCA561_OFFSET_WIN1RAVE 12
  128. #define SPCA561_OFFSET_WIN1BAVE 13
  129. #define SPCA561_OFFSET_WIN1GBAVE 14
  130. #define SPCA561_OFFSET_FREQ 15
  131. #define SPCA561_OFFSET_VSYNC 16
  132. #define SPCA561_OFFSET_DATA 1
  133. #define SPCA561_INDEX_I2C_BASE 0x8800
  134. #define SPCA561_SNAPBIT 0x20
  135. #define SPCA561_SNAPCTRL 0x40
  136. enum {
  137. Rev072A = 0,
  138. Rev012A,
  139. };
  140. static void reg_w_val(struct usb_device *dev, __u16 index, __u16 value)
  141. {
  142. int ret;
  143. ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  144. 0, /* request */
  145. USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  146. value, index, NULL, 0, 500);
  147. PDEBUG(D_USBO, "reg write: 0x%02x:0x%02x", index, value);
  148. if (ret < 0)
  149. PDEBUG(D_ERR, "reg write: error %d", ret);
  150. }
  151. static void write_vector(struct gspca_dev *gspca_dev,
  152. const __u16 data[][2])
  153. {
  154. struct usb_device *dev = gspca_dev->dev;
  155. int i;
  156. i = 0;
  157. while (data[i][1] != 0) {
  158. reg_w_val(dev, data[i][1], data[i][0]);
  159. i++;
  160. }
  161. }
  162. /* read 'len' bytes to gspca_dev->usb_buf */
  163. static void reg_r(struct gspca_dev *gspca_dev,
  164. __u16 index, __u16 length)
  165. {
  166. usb_control_msg(gspca_dev->dev,
  167. usb_rcvctrlpipe(gspca_dev->dev, 0),
  168. 0, /* request */
  169. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  170. 0, /* value */
  171. index, gspca_dev->usb_buf, length, 500);
  172. }
  173. static void reg_w_buf(struct gspca_dev *gspca_dev,
  174. __u16 index, const __u8 *buffer, __u16 len)
  175. {
  176. memcpy(gspca_dev->usb_buf, buffer, len);
  177. usb_control_msg(gspca_dev->dev,
  178. usb_sndctrlpipe(gspca_dev->dev, 0),
  179. 0, /* request */
  180. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  181. 0, /* value */
  182. index, gspca_dev->usb_buf, len, 500);
  183. }
  184. static void i2c_init(struct gspca_dev *gspca_dev, __u8 mode)
  185. {
  186. reg_w_val(gspca_dev->dev, 0x92, 0x8804);
  187. reg_w_val(gspca_dev->dev, mode, 0x8802);
  188. }
  189. static void i2c_write(struct gspca_dev *gspca_dev, __u16 valeur, __u16 reg)
  190. {
  191. int retry = 60;
  192. __u8 DataLow;
  193. __u8 DataHight;
  194. DataLow = valeur;
  195. DataHight = valeur >> 8;
  196. reg_w_val(gspca_dev->dev, reg, 0x8801);
  197. reg_w_val(gspca_dev->dev, DataLow, 0x8805);
  198. reg_w_val(gspca_dev->dev, DataHight, 0x8800);
  199. while (retry--) {
  200. reg_r(gspca_dev, 0x8803, 1);
  201. if (!gspca_dev->usb_buf[0])
  202. break;
  203. }
  204. }
  205. static int i2c_read(struct gspca_dev *gspca_dev, __u16 reg, __u8 mode)
  206. {
  207. int retry = 60;
  208. __u8 value;
  209. __u8 vallsb;
  210. reg_w_val(gspca_dev->dev, 0x92, 0x8804);
  211. reg_w_val(gspca_dev->dev, reg, 0x8801);
  212. reg_w_val(gspca_dev->dev, (mode | 0x01), 0x8802);
  213. while (retry--) {
  214. reg_r(gspca_dev, 0x8803, 1);
  215. if (!gspca_dev->usb_buf)
  216. break;
  217. }
  218. if (retry == 0)
  219. return -1;
  220. reg_r(gspca_dev, 0x8800, 1);
  221. value = gspca_dev->usb_buf[0];
  222. reg_r(gspca_dev, 0x8805, 1);
  223. vallsb = gspca_dev->usb_buf[0];
  224. return ((int) value << 8) | vallsb;
  225. }
  226. static const __u16 spca561_init_data[][2] = {
  227. {0x0000, 0x8114}, /* Software GPIO output data */
  228. {0x0001, 0x8114}, /* Software GPIO output data */
  229. {0x0000, 0x8112}, /* Some kind of reset */
  230. {0x0003, 0x8701}, /* PCLK clock delay adjustment */
  231. {0x0001, 0x8703}, /* HSYNC from cmos inverted */
  232. {0x0011, 0x8118}, /* Enable and conf sensor */
  233. {0x0001, 0x8118}, /* Conf sensor */
  234. {0x0092, 0x8804}, /* I know nothing about these */
  235. {0x0010, 0x8802}, /* 0x88xx registers, so I won't */
  236. /***************/
  237. {0x000d, 0x8805}, /* sensor default setting */
  238. {0x0001, 0x8801}, /* 1 <- 0x0d */
  239. {0x0000, 0x8800},
  240. {0x0018, 0x8805},
  241. {0x0002, 0x8801}, /* 2 <- 0x18 */
  242. {0x0000, 0x8800},
  243. {0x0065, 0x8805},
  244. {0x0004, 0x8801}, /* 4 <- 0x01 0x65 */
  245. {0x0001, 0x8800},
  246. {0x0021, 0x8805},
  247. {0x0005, 0x8801}, /* 5 <- 0x21 */
  248. {0x0000, 0x8800},
  249. {0x00aa, 0x8805},
  250. {0x0007, 0x8801}, /* 7 <- 0xaa */
  251. {0x0000, 0x8800},
  252. {0x0004, 0x8805},
  253. {0x0020, 0x8801}, /* 0x20 <- 0x15 0x04 */
  254. {0x0015, 0x8800},
  255. {0x0002, 0x8805},
  256. {0x0039, 0x8801}, /* 0x39 <- 0x02 */
  257. {0x0000, 0x8800},
  258. {0x0010, 0x8805},
  259. {0x0035, 0x8801}, /* 0x35 <- 0x10 */
  260. {0x0000, 0x8800},
  261. {0x0049, 0x8805},
  262. {0x0009, 0x8801}, /* 0x09 <- 0x10 0x49 */
  263. {0x0010, 0x8800},
  264. {0x000b, 0x8805},
  265. {0x0028, 0x8801}, /* 0x28 <- 0x0b */
  266. {0x0000, 0x8800},
  267. {0x000f, 0x8805},
  268. {0x003b, 0x8801}, /* 0x3b <- 0x0f */
  269. {0x0000, 0x8800},
  270. {0x0000, 0x8805},
  271. {0x003c, 0x8801}, /* 0x3c <- 0x00 */
  272. {0x0000, 0x8800},
  273. /***************/
  274. {0x0018, 0x8601}, /* Pixel/line selection for color separation */
  275. {0x0000, 0x8602}, /* Optical black level for user setting */
  276. {0x0060, 0x8604}, /* Optical black horizontal offset */
  277. {0x0002, 0x8605}, /* Optical black vertical offset */
  278. {0x0000, 0x8603}, /* Non-automatic optical black level */
  279. {0x0002, 0x865b}, /* Horizontal offset for valid pixels */
  280. {0x0000, 0x865f}, /* Vertical valid pixels window (x2) */
  281. {0x00b0, 0x865d}, /* Horizontal valid pixels window (x2) */
  282. {0x0090, 0x865e}, /* Vertical valid lines window (x2) */
  283. {0x00e0, 0x8406}, /* Memory buffer threshold */
  284. {0x0000, 0x8660}, /* Compensation memory stuff */
  285. {0x0002, 0x8201}, /* Output address for r/w serial EEPROM */
  286. {0x0008, 0x8200}, /* Clear valid bit for serial EEPROM */
  287. {0x0001, 0x8200}, /* OprMode to be executed by hardware */
  288. {0x0007, 0x8201}, /* Output address for r/w serial EEPROM */
  289. {0x0008, 0x8200}, /* Clear valid bit for serial EEPROM */
  290. {0x0001, 0x8200}, /* OprMode to be executed by hardware */
  291. {0x0010, 0x8660}, /* Compensation memory stuff */
  292. {0x0018, 0x8660}, /* Compensation memory stuff */
  293. {0x0004, 0x8611}, /* R offset for white balance */
  294. {0x0004, 0x8612}, /* Gr offset for white balance */
  295. {0x0007, 0x8613}, /* B offset for white balance */
  296. {0x0000, 0x8614}, /* Gb offset for white balance */
  297. {0x008c, 0x8651}, /* R gain for white balance */
  298. {0x008c, 0x8652}, /* Gr gain for white balance */
  299. {0x00b5, 0x8653}, /* B gain for white balance */
  300. {0x008c, 0x8654}, /* Gb gain for white balance */
  301. {0x0002, 0x8502}, /* Maximum average bit rate stuff */
  302. {0x0011, 0x8802},
  303. {0x0087, 0x8700}, /* Set master clock (96Mhz????) */
  304. {0x0081, 0x8702}, /* Master clock output enable */
  305. {0x0000, 0x8500}, /* Set image type (352x288 no compression) */
  306. /* Originally was 0x0010 (352x288 compression) */
  307. {0x0002, 0x865b}, /* Horizontal offset for valid pixels */
  308. {0x0003, 0x865c}, /* Vertical offset for valid lines */
  309. /***************//* sensor active */
  310. {0x0003, 0x8801}, /* 0x03 <- 0x01 0x21 //289 */
  311. {0x0021, 0x8805},
  312. {0x0001, 0x8800},
  313. {0x0004, 0x8801}, /* 0x04 <- 0x01 0x65 //357 */
  314. {0x0065, 0x8805},
  315. {0x0001, 0x8800},
  316. {0x0005, 0x8801}, /* 0x05 <- 0x2f */
  317. {0x002f, 0x8805},
  318. {0x0000, 0x8800},
  319. {0x0006, 0x8801}, /* 0x06 <- 0 */
  320. {0x0000, 0x8805},
  321. {0x0000, 0x8800},
  322. {0x000a, 0x8801}, /* 0x0a <- 2 */
  323. {0x0002, 0x8805},
  324. {0x0000, 0x8800},
  325. {0x0009, 0x8801}, /* 0x09 <- 0x1061 */
  326. {0x0061, 0x8805},
  327. {0x0010, 0x8800},
  328. {0x0035, 0x8801}, /* 0x35 <-0x14 */
  329. {0x0014, 0x8805},
  330. {0x0000, 0x8800},
  331. {0x0030, 0x8112}, /* ISO and drop packet enable */
  332. {0x0000, 0x8112}, /* Some kind of reset ???? */
  333. {0x0009, 0x8118}, /* Enable sensor and set standby */
  334. {0x0000, 0x8114}, /* Software GPIO output data */
  335. {0x0000, 0x8114}, /* Software GPIO output data */
  336. {0x0001, 0x8114}, /* Software GPIO output data */
  337. {0x0000, 0x8112}, /* Some kind of reset ??? */
  338. {0x0003, 0x8701},
  339. {0x0001, 0x8703},
  340. {0x0011, 0x8118},
  341. {0x0001, 0x8118},
  342. /***************/
  343. {0x0092, 0x8804},
  344. {0x0010, 0x8802},
  345. {0x000d, 0x8805},
  346. {0x0001, 0x8801},
  347. {0x0000, 0x8800},
  348. {0x0018, 0x8805},
  349. {0x0002, 0x8801},
  350. {0x0000, 0x8800},
  351. {0x0065, 0x8805},
  352. {0x0004, 0x8801},
  353. {0x0001, 0x8800},
  354. {0x0021, 0x8805},
  355. {0x0005, 0x8801},
  356. {0x0000, 0x8800},
  357. {0x00aa, 0x8805},
  358. {0x0007, 0x8801}, /* mode 0xaa */
  359. {0x0000, 0x8800},
  360. {0x0004, 0x8805},
  361. {0x0020, 0x8801},
  362. {0x0015, 0x8800}, /* mode 0x0415 */
  363. {0x0002, 0x8805},
  364. {0x0039, 0x8801},
  365. {0x0000, 0x8800},
  366. {0x0010, 0x8805},
  367. {0x0035, 0x8801},
  368. {0x0000, 0x8800},
  369. {0x0049, 0x8805},
  370. {0x0009, 0x8801},
  371. {0x0010, 0x8800},
  372. {0x000b, 0x8805},
  373. {0x0028, 0x8801},
  374. {0x0000, 0x8800},
  375. {0x000f, 0x8805},
  376. {0x003b, 0x8801},
  377. {0x0000, 0x8800},
  378. {0x0000, 0x8805},
  379. {0x003c, 0x8801},
  380. {0x0000, 0x8800},
  381. {0x0002, 0x8502},
  382. {0x0039, 0x8801},
  383. {0x0000, 0x8805},
  384. {0x0000, 0x8800},
  385. {0x0087, 0x8700}, /* overwrite by start */
  386. {0x0081, 0x8702},
  387. {0x0000, 0x8500},
  388. /* {0x0010, 0x8500}, -- Previous line was this */
  389. {0x0002, 0x865b},
  390. {0x0003, 0x865c},
  391. /***************/
  392. {0x0003, 0x8801}, /* 0x121-> 289 */
  393. {0x0021, 0x8805},
  394. {0x0001, 0x8800},
  395. {0x0004, 0x8801}, /* 0x165 -> 357 */
  396. {0x0065, 0x8805},
  397. {0x0001, 0x8800},
  398. {0x0005, 0x8801}, /* 0x2f //blanking control colonne */
  399. {0x002f, 0x8805},
  400. {0x0000, 0x8800},
  401. {0x0006, 0x8801}, /* 0x00 //blanking mode row */
  402. {0x0000, 0x8805},
  403. {0x0000, 0x8800},
  404. {0x000a, 0x8801}, /* 0x01 //0x02 */
  405. {0x0001, 0x8805},
  406. {0x0000, 0x8800},
  407. {0x0009, 0x8801}, /* 0x1061 - setexposure times && pixel clock
  408. * 0001 0 | 000 0110 0001 */
  409. {0x0061, 0x8805}, /* 61 31 */
  410. {0x0008, 0x8800}, /* 08 */
  411. {0x0035, 0x8801}, /* 0x14 - set gain general */
  412. {0x001f, 0x8805}, /* 0x14 */
  413. {0x0000, 0x8800},
  414. {0x0030, 0x8112},
  415. {}
  416. };
  417. static void sensor_reset(struct gspca_dev *gspca_dev)
  418. {
  419. reg_w_val(gspca_dev->dev, 0x8631, 0xc8);
  420. reg_w_val(gspca_dev->dev, 0x8634, 0xc8);
  421. reg_w_val(gspca_dev->dev, 0x8112, 0x00);
  422. reg_w_val(gspca_dev->dev, 0x8114, 0x00);
  423. reg_w_val(gspca_dev->dev, 0x8118, 0x21);
  424. i2c_init(gspca_dev, 0x14);
  425. i2c_write(gspca_dev, 1, 0x0d);
  426. i2c_write(gspca_dev, 0, 0x0d);
  427. }
  428. /******************** QC Express etch2 stuff ********************/
  429. static const __u16 Pb100_1map8300[][2] = {
  430. /* reg, value */
  431. {0x8320, 0x3304},
  432. {0x8303, 0x0125}, /* image area */
  433. {0x8304, 0x0169},
  434. {0x8328, 0x000b},
  435. {0x833c, 0x0001},
  436. {0x832f, 0x0419},
  437. {0x8307, 0x00aa},
  438. {0x8301, 0x0003},
  439. {0x8302, 0x000e},
  440. {}
  441. };
  442. static const __u16 Pb100_2map8300[][2] = {
  443. /* reg, value */
  444. {0x8339, 0x0000},
  445. {0x8307, 0x00aa},
  446. {}
  447. };
  448. static const __u16 spca561_161rev12A_data1[][2] = {
  449. {0x21, 0x8118},
  450. {0x01, 0x8114},
  451. {0x00, 0x8112},
  452. {0x92, 0x8804},
  453. {0x04, 0x8802}, /* windows uses 08 */
  454. {}
  455. };
  456. static const __u16 spca561_161rev12A_data2[][2] = {
  457. {0x21, 0x8118},
  458. {0x10, 0x8500},
  459. {0x07, 0x8601},
  460. {0x07, 0x8602},
  461. {0x04, 0x8501},
  462. {0x21, 0x8118},
  463. {0x07, 0x8201}, /* windows uses 02 */
  464. {0x08, 0x8200},
  465. {0x01, 0x8200},
  466. {0x00, 0x8114},
  467. {0x01, 0x8114}, /* windows uses 00 */
  468. {0x90, 0x8604},
  469. {0x00, 0x8605},
  470. {0xb0, 0x8603},
  471. /* sensor gains */
  472. {0x00, 0x8610}, /* *red */
  473. {0x00, 0x8611}, /* 3f *green */
  474. {0x00, 0x8612}, /* green *blue */
  475. {0x00, 0x8613}, /* blue *green */
  476. {0x35, 0x8614}, /* green *red */
  477. {0x35, 0x8615}, /* 40 *green */
  478. {0x35, 0x8616}, /* 7a *blue */
  479. {0x35, 0x8617}, /* 40 *green */
  480. {0x0c, 0x8620}, /* 0c */
  481. {0xc8, 0x8631}, /* c8 */
  482. {0xc8, 0x8634}, /* c8 */
  483. {0x23, 0x8635}, /* 23 */
  484. {0x1f, 0x8636}, /* 1f */
  485. {0xdd, 0x8637}, /* dd */
  486. {0xe1, 0x8638}, /* e1 */
  487. {0x1d, 0x8639}, /* 1d */
  488. {0x21, 0x863a}, /* 21 */
  489. {0xe3, 0x863b}, /* e3 */
  490. {0xdf, 0x863c}, /* df */
  491. {0xf0, 0x8505},
  492. {0x32, 0x850a},
  493. {}
  494. };
  495. static void sensor_mapwrite(struct gspca_dev *gspca_dev,
  496. const __u16 sensormap[][2])
  497. {
  498. int i = 0;
  499. __u8 usbval[2];
  500. while (sensormap[i][0]) {
  501. usbval[0] = sensormap[i][1];
  502. usbval[1] = sensormap[i][1] >> 8;
  503. reg_w_buf(gspca_dev, sensormap[i][0], usbval, 2);
  504. i++;
  505. }
  506. }
  507. static void init_161rev12A(struct gspca_dev *gspca_dev)
  508. {
  509. sensor_reset(gspca_dev);
  510. write_vector(gspca_dev, spca561_161rev12A_data1);
  511. sensor_mapwrite(gspca_dev, Pb100_1map8300);
  512. write_vector(gspca_dev, spca561_161rev12A_data2);
  513. sensor_mapwrite(gspca_dev, Pb100_2map8300);
  514. }
  515. /* this function is called at probe time */
  516. static int sd_config(struct gspca_dev *gspca_dev,
  517. const struct usb_device_id *id)
  518. {
  519. struct sd *sd = (struct sd *) gspca_dev;
  520. struct cam *cam;
  521. __u16 vendor, product;
  522. __u8 data1, data2;
  523. /* Read frm global register the USB product and vendor IDs, just to
  524. * prove that we can communicate with the device. This works, which
  525. * confirms at we are communicating properly and that the device
  526. * is a 561. */
  527. reg_r(gspca_dev, 0x8104, 1);
  528. data1 = gspca_dev->usb_buf[0];
  529. reg_r(gspca_dev, 0x8105, 1);
  530. data2 = gspca_dev->usb_buf[0];
  531. vendor = (data2 << 8) | data1;
  532. reg_r(gspca_dev, 0x8106, 1);
  533. data1 = gspca_dev->usb_buf[0];
  534. reg_r(gspca_dev, 0x8107, 1);
  535. data2 = gspca_dev->usb_buf[0];
  536. product = (data2 << 8) | data1;
  537. if (vendor != id->idVendor || product != id->idProduct) {
  538. PDEBUG(D_PROBE, "Bad vendor / product from device");
  539. return -EINVAL;
  540. }
  541. cam = &gspca_dev->cam;
  542. cam->dev_name = (char *) id->driver_info;
  543. cam->epaddr = 0x01;
  544. gspca_dev->nbalt = 7 + 1; /* choose alternate 7 first */
  545. cam->cam_mode = sif_mode;
  546. cam->nmodes = sizeof sif_mode / sizeof sif_mode[0];
  547. sd->chip_revision = id->driver_info;
  548. sd->brightness = sd_ctrls[SD_BRIGHTNESS].qctrl.default_value;
  549. sd->contrast = sd_ctrls[SD_CONTRAST].qctrl.default_value;
  550. sd->autogain = sd_ctrls[SD_AUTOGAIN].qctrl.default_value;
  551. return 0;
  552. }
  553. /* this function is called at open time */
  554. static int sd_open(struct gspca_dev *gspca_dev)
  555. {
  556. struct sd *sd = (struct sd *) gspca_dev;
  557. switch (sd->chip_revision) {
  558. case Rev072A:
  559. PDEBUG(D_STREAM, "Chip revision id: 072a");
  560. write_vector(gspca_dev, spca561_init_data);
  561. break;
  562. default:
  563. /* case Rev012A: */
  564. PDEBUG(D_STREAM, "Chip revision id: 012a");
  565. init_161rev12A(gspca_dev);
  566. break;
  567. }
  568. return 0;
  569. }
  570. static void setcontrast(struct gspca_dev *gspca_dev)
  571. {
  572. struct sd *sd = (struct sd *) gspca_dev;
  573. struct usb_device *dev = gspca_dev->dev;
  574. __u8 lowb;
  575. int expotimes;
  576. switch (sd->chip_revision) {
  577. case Rev072A:
  578. lowb = sd->contrast >> 8;
  579. reg_w_val(dev, lowb, 0x8651);
  580. reg_w_val(dev, lowb, 0x8652);
  581. reg_w_val(dev, lowb, 0x8653);
  582. reg_w_val(dev, lowb, 0x8654);
  583. break;
  584. case Rev012A: {
  585. __u8 Reg8391[] =
  586. { 0x00, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00 };
  587. /* Write camera sensor settings */
  588. expotimes = (sd->contrast >> 5) & 0x07ff;
  589. Reg8391[0] = expotimes & 0xff; /* exposure */
  590. Reg8391[1] = 0x18 | (expotimes >> 8);
  591. Reg8391[2] = sd->brightness; /* gain */
  592. reg_w_buf(gspca_dev, 0x8391, Reg8391, 8);
  593. reg_w_buf(gspca_dev, 0x8390, Reg8391, 8);
  594. break;
  595. }
  596. }
  597. }
  598. static void setautogain(struct gspca_dev *gspca_dev)
  599. {
  600. struct sd *sd = (struct sd *) gspca_dev;
  601. if (sd->chip_revision == Rev072A) {
  602. if (sd->autogain)
  603. sd->ag_cnt = AG_CNT_START;
  604. else
  605. sd->ag_cnt = -1;
  606. }
  607. }
  608. static void sd_start(struct gspca_dev *gspca_dev)
  609. {
  610. struct sd *sd = (struct sd *) gspca_dev;
  611. struct usb_device *dev = gspca_dev->dev;
  612. int Clck;
  613. __u8 Reg8307[] = { 0xaa, 0x00 };
  614. int mode;
  615. mode = gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv;
  616. switch (sd->chip_revision) {
  617. case Rev072A:
  618. switch (mode) {
  619. default:
  620. /* case 0:
  621. case 1: */
  622. Clck = 0x25;
  623. break;
  624. case 2:
  625. Clck = 0x22;
  626. break;
  627. case 3:
  628. Clck = 0x21;
  629. break;
  630. }
  631. reg_w_val(dev, 0x8500, mode); /* mode */
  632. reg_w_val(dev, 0x8700, Clck); /* 0x27 clock */
  633. reg_w_val(dev, 0x8112, 0x10 | 0x20);
  634. setautogain(gspca_dev);
  635. break;
  636. default:
  637. /* case Rev012A: */
  638. switch (mode) {
  639. case 0:
  640. case 1:
  641. Clck = 0x8a;
  642. break;
  643. case 2:
  644. Clck = 0x85;
  645. break;
  646. default:
  647. Clck = 0x83;
  648. break;
  649. }
  650. if (mode <= 1) {
  651. /* Use compression on 320x240 and above */
  652. reg_w_val(dev, 0x8500, 0x10 | mode);
  653. } else {
  654. /* I couldn't get the compression to work below 320x240
  655. * Fortunately at these resolutions the bandwidth
  656. * is sufficient to push raw frames at ~20fps */
  657. reg_w_val(dev, 0x8500, mode);
  658. } /* -- qq@kuku.eu.org */
  659. reg_w_buf(gspca_dev, 0x8307, Reg8307, 2);
  660. reg_w_val(gspca_dev->dev, 0x8700, Clck);
  661. /* 0x8f 0x85 0x27 clock */
  662. reg_w_val(gspca_dev->dev, 0x8112, 0x1e | 0x20);
  663. reg_w_val(gspca_dev->dev, 0x850b, 0x03);
  664. setcontrast(gspca_dev);
  665. break;
  666. }
  667. }
  668. static void sd_stopN(struct gspca_dev *gspca_dev)
  669. {
  670. reg_w_val(gspca_dev->dev, 0x8112, 0x20);
  671. }
  672. static void sd_stop0(struct gspca_dev *gspca_dev)
  673. {
  674. }
  675. /* this function is called at close time */
  676. static void sd_close(struct gspca_dev *gspca_dev)
  677. {
  678. reg_w_val(gspca_dev->dev, 0x8114, 0);
  679. }
  680. static void do_autogain(struct gspca_dev *gspca_dev)
  681. {
  682. struct sd *sd = (struct sd *) gspca_dev;
  683. int expotimes;
  684. int pixelclk;
  685. int gainG;
  686. __u8 R, Gr, Gb, B;
  687. int y;
  688. __u8 luma_mean = 110;
  689. __u8 luma_delta = 20;
  690. __u8 spring = 4;
  691. if (sd->ag_cnt < 0)
  692. return;
  693. if (--sd->ag_cnt >= 0)
  694. return;
  695. sd->ag_cnt = AG_CNT_START;
  696. switch (sd->chip_revision) {
  697. case Rev072A:
  698. reg_r(gspca_dev, 0x8621, 1);
  699. Gr = gspca_dev->usb_buf[0];
  700. reg_r(gspca_dev, 0x8622, 1);
  701. R = gspca_dev->usb_buf[0];
  702. reg_r(gspca_dev, 0x8623, 1);
  703. B = gspca_dev->usb_buf[0];
  704. reg_r(gspca_dev, 0x8624, 1);
  705. Gb = gspca_dev->usb_buf[0];
  706. y = (77 * R + 75 * (Gr + Gb) + 29 * B) >> 8;
  707. /* u= (128*B-(43*(Gr+Gb+R))) >> 8; */
  708. /* v= (128*R-(53*(Gr+Gb))-21*B) >> 8; */
  709. /* PDEBUG(D_CONF,"reading Y %d U %d V %d ",y,u,v); */
  710. if (y < luma_mean - luma_delta ||
  711. y > luma_mean + luma_delta) {
  712. expotimes = i2c_read(gspca_dev, 0x09, 0x10);
  713. pixelclk = 0x0800;
  714. expotimes = expotimes & 0x07ff;
  715. /* PDEBUG(D_PACK,
  716. "Exposition Times 0x%03X Clock 0x%04X ",
  717. expotimes,pixelclk); */
  718. gainG = i2c_read(gspca_dev, 0x35, 0x10);
  719. /* PDEBUG(D_PACK,
  720. "reading Gain register %d", gainG); */
  721. expotimes += (luma_mean - y) >> spring;
  722. gainG += (luma_mean - y) / 50;
  723. /* PDEBUG(D_PACK,
  724. "compute expotimes %d gain %d",
  725. expotimes,gainG); */
  726. if (gainG > 0x3f)
  727. gainG = 0x3f;
  728. else if (gainG < 4)
  729. gainG = 3;
  730. i2c_write(gspca_dev, gainG, 0x35);
  731. if (expotimes >= 0x0256)
  732. expotimes = 0x0256;
  733. else if (expotimes < 4)
  734. expotimes = 3;
  735. i2c_write(gspca_dev, expotimes | pixelclk, 0x09);
  736. }
  737. break;
  738. case Rev012A:
  739. /* sensor registers is access and memory mapped to 0x8300 */
  740. /* readind all 0x83xx block the sensor */
  741. /*
  742. * The data from the header seem wrong where is the luma
  743. * and chroma mean value
  744. * at the moment set exposure in contrast set
  745. */
  746. break;
  747. }
  748. }
  749. static void sd_pkt_scan(struct gspca_dev *gspca_dev,
  750. struct gspca_frame *frame, /* target */
  751. __u8 *data, /* isoc packet */
  752. int len) /* iso packet length */
  753. {
  754. switch (data[0]) {
  755. case 0: /* start of frame */
  756. frame = gspca_frame_add(gspca_dev, LAST_PACKET, frame,
  757. data, 0);
  758. data += SPCA561_OFFSET_DATA;
  759. len -= SPCA561_OFFSET_DATA;
  760. if (data[1] & 0x10) {
  761. /* compressed bayer */
  762. gspca_frame_add(gspca_dev, FIRST_PACKET,
  763. frame, data, len);
  764. } else {
  765. /* raw bayer (with a header, which we skip) */
  766. data += 20;
  767. len -= 20;
  768. gspca_frame_add(gspca_dev, FIRST_PACKET,
  769. frame, data, len);
  770. }
  771. return;
  772. case 0xff: /* drop */
  773. /* gspca_dev->last_packet_type = DISCARD_PACKET; */
  774. return;
  775. }
  776. data++;
  777. len--;
  778. gspca_frame_add(gspca_dev, INTER_PACKET, frame, data, len);
  779. }
  780. static void setbrightness(struct gspca_dev *gspca_dev)
  781. {
  782. struct sd *sd = (struct sd *) gspca_dev;
  783. __u8 value;
  784. switch (sd->chip_revision) {
  785. case Rev072A:
  786. value = sd->brightness;
  787. reg_w_val(gspca_dev->dev, value, 0x8611);
  788. reg_w_val(gspca_dev->dev, value, 0x8612);
  789. reg_w_val(gspca_dev->dev, value, 0x8613);
  790. reg_w_val(gspca_dev->dev, value, 0x8614);
  791. break;
  792. default:
  793. /* case Rev012A: */
  794. setcontrast(gspca_dev);
  795. break;
  796. }
  797. }
  798. static void getbrightness(struct gspca_dev *gspca_dev)
  799. {
  800. struct sd *sd = (struct sd *) gspca_dev;
  801. __u16 tot;
  802. switch (sd->chip_revision) {
  803. case Rev072A:
  804. tot = 0;
  805. reg_r(gspca_dev, 0x8611, 1);
  806. tot += gspca_dev->usb_buf[0];
  807. reg_r(gspca_dev, 0x8612, 1);
  808. tot += gspca_dev->usb_buf[0];
  809. reg_r(gspca_dev, 0x8613, 1);
  810. tot += gspca_dev->usb_buf[0];
  811. reg_r(gspca_dev, 0x8614, 1);
  812. tot += gspca_dev->usb_buf[0];
  813. sd->brightness = tot >> 2;
  814. break;
  815. default:
  816. /* case Rev012A: */
  817. /* no way to read sensor settings */
  818. break;
  819. }
  820. }
  821. static void getcontrast(struct gspca_dev *gspca_dev)
  822. {
  823. struct sd *sd = (struct sd *) gspca_dev;
  824. __u16 tot;
  825. switch (sd->chip_revision) {
  826. case Rev072A:
  827. tot = 0;
  828. reg_r(gspca_dev, 0x8651, 1);
  829. tot += gspca_dev->usb_buf[0];
  830. reg_r(gspca_dev, 0x8652, 1);
  831. tot += gspca_dev->usb_buf[0];
  832. reg_r(gspca_dev, 0x8653, 1);
  833. tot += gspca_dev->usb_buf[0];
  834. reg_r(gspca_dev, 0x8654, 1);
  835. tot += gspca_dev->usb_buf[0];
  836. sd->contrast = tot << 6;
  837. break;
  838. default:
  839. /* case Rev012A: */
  840. /* no way to read sensor settings */
  841. break;
  842. }
  843. PDEBUG(D_CONF, "get contrast %d", sd->contrast);
  844. }
  845. static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
  846. {
  847. struct sd *sd = (struct sd *) gspca_dev;
  848. sd->brightness = val;
  849. if (gspca_dev->streaming)
  850. setbrightness(gspca_dev);
  851. return 0;
  852. }
  853. static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
  854. {
  855. struct sd *sd = (struct sd *) gspca_dev;
  856. getbrightness(gspca_dev);
  857. *val = sd->brightness;
  858. return 0;
  859. }
  860. static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
  861. {
  862. struct sd *sd = (struct sd *) gspca_dev;
  863. sd->contrast = val;
  864. if (gspca_dev->streaming)
  865. setcontrast(gspca_dev);
  866. return 0;
  867. }
  868. static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val)
  869. {
  870. struct sd *sd = (struct sd *) gspca_dev;
  871. getcontrast(gspca_dev);
  872. *val = sd->contrast;
  873. return 0;
  874. }
  875. static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val)
  876. {
  877. struct sd *sd = (struct sd *) gspca_dev;
  878. sd->autogain = val;
  879. if (gspca_dev->streaming)
  880. setautogain(gspca_dev);
  881. return 0;
  882. }
  883. static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val)
  884. {
  885. struct sd *sd = (struct sd *) gspca_dev;
  886. *val = sd->autogain;
  887. return 0;
  888. }
  889. /* sub-driver description */
  890. static const struct sd_desc sd_desc = {
  891. .name = MODULE_NAME,
  892. .ctrls = sd_ctrls,
  893. .nctrls = ARRAY_SIZE(sd_ctrls),
  894. .config = sd_config,
  895. .open = sd_open,
  896. .start = sd_start,
  897. .stopN = sd_stopN,
  898. .stop0 = sd_stop0,
  899. .close = sd_close,
  900. .pkt_scan = sd_pkt_scan,
  901. .dq_callback = do_autogain,
  902. };
  903. /* -- module initialisation -- */
  904. static const __devinitdata struct usb_device_id device_table[] = {
  905. {USB_DEVICE(0x041e, 0x401a), .driver_info = Rev072A},
  906. {USB_DEVICE(0x041e, 0x403b), .driver_info = Rev012A},
  907. {USB_DEVICE(0x0458, 0x7004), .driver_info = Rev072A},
  908. {USB_DEVICE(0x046d, 0x0928), .driver_info = Rev012A},
  909. {USB_DEVICE(0x046d, 0x0929), .driver_info = Rev012A},
  910. {USB_DEVICE(0x046d, 0x092a), .driver_info = Rev012A},
  911. {USB_DEVICE(0x046d, 0x092b), .driver_info = Rev012A},
  912. {USB_DEVICE(0x046d, 0x092c), .driver_info = Rev012A},
  913. {USB_DEVICE(0x046d, 0x092d), .driver_info = Rev012A},
  914. {USB_DEVICE(0x046d, 0x092e), .driver_info = Rev012A},
  915. {USB_DEVICE(0x046d, 0x092f), .driver_info = Rev012A},
  916. {USB_DEVICE(0x04fc, 0x0561), .driver_info = Rev072A},
  917. {USB_DEVICE(0x060b, 0xa001), .driver_info = Rev072A},
  918. {USB_DEVICE(0x10fd, 0x7e50), .driver_info = Rev072A},
  919. {USB_DEVICE(0xabcd, 0xcdee), .driver_info = Rev072A},
  920. {}
  921. };
  922. MODULE_DEVICE_TABLE(usb, device_table);
  923. /* -- device connect -- */
  924. static int sd_probe(struct usb_interface *intf,
  925. const struct usb_device_id *id)
  926. {
  927. return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
  928. THIS_MODULE);
  929. }
  930. static struct usb_driver sd_driver = {
  931. .name = MODULE_NAME,
  932. .id_table = device_table,
  933. .probe = sd_probe,
  934. .disconnect = gspca_disconnect,
  935. };
  936. /* -- module insert / remove -- */
  937. static int __init sd_mod_init(void)
  938. {
  939. if (usb_register(&sd_driver) < 0)
  940. return -1;
  941. PDEBUG(D_PROBE, "registered");
  942. return 0;
  943. }
  944. static void __exit sd_mod_exit(void)
  945. {
  946. usb_deregister(&sd_driver);
  947. PDEBUG(D_PROBE, "deregistered");
  948. }
  949. module_init(sd_mod_init);
  950. module_exit(sd_mod_exit);