spca561.c 26 KB

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