spca561.c 26 KB

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