spca561.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128
  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. __u16 exposure; /* rev12a only */
  31. #define EXPOSURE_MIN 1
  32. #define EXPOSURE_DEF 200
  33. #define EXPOSURE_MAX (4095 - 900) /* see set_exposure */
  34. __u8 contrast; /* rev72a only */
  35. #define CONTRAST_MIN 0x00
  36. #define CONTRAST_DEF 0x20
  37. #define CONTRAST_MAX 0x3f
  38. __u8 brightness; /* rev72a only */
  39. #define BRIGHTNESS_MIN 0
  40. #define BRIGHTNESS_DEF 0x20
  41. #define BRIGHTNESS_MAX 0x3f
  42. __u8 white;
  43. #define WHITE_MIN 1
  44. #define WHITE_DEF 0x40
  45. #define WHITE_MAX 0x7f
  46. __u8 autogain;
  47. #define AUTOGAIN_MIN 0
  48. #define AUTOGAIN_DEF 1
  49. #define AUTOGAIN_MAX 1
  50. __u8 gain; /* rev12a only */
  51. #define GAIN_MIN 0x0
  52. #define GAIN_DEF 0x24
  53. #define GAIN_MAX 0x24
  54. #define EXPO12A_DEF 3
  55. __u8 expo12a; /* expo/gain? for rev 12a */
  56. __u8 chip_revision;
  57. #define Rev012A 0
  58. #define Rev072A 1
  59. signed char ag_cnt;
  60. #define AG_CNT_START 13
  61. };
  62. static const struct v4l2_pix_format sif_012a_mode[] = {
  63. {160, 120, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
  64. .bytesperline = 160,
  65. .sizeimage = 160 * 120,
  66. .colorspace = V4L2_COLORSPACE_SRGB,
  67. .priv = 3},
  68. {176, 144, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
  69. .bytesperline = 176,
  70. .sizeimage = 176 * 144,
  71. .colorspace = V4L2_COLORSPACE_SRGB,
  72. .priv = 2},
  73. {320, 240, V4L2_PIX_FMT_SPCA561, V4L2_FIELD_NONE,
  74. .bytesperline = 320,
  75. .sizeimage = 320 * 240 * 4 / 8,
  76. .colorspace = V4L2_COLORSPACE_SRGB,
  77. .priv = 1},
  78. {352, 288, V4L2_PIX_FMT_SPCA561, V4L2_FIELD_NONE,
  79. .bytesperline = 352,
  80. .sizeimage = 352 * 288 * 4 / 8,
  81. .colorspace = V4L2_COLORSPACE_SRGB,
  82. .priv = 0},
  83. };
  84. static const struct v4l2_pix_format sif_072a_mode[] = {
  85. {160, 120, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
  86. .bytesperline = 160,
  87. .sizeimage = 160 * 120,
  88. .colorspace = V4L2_COLORSPACE_SRGB,
  89. .priv = 3},
  90. {176, 144, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
  91. .bytesperline = 176,
  92. .sizeimage = 176 * 144,
  93. .colorspace = V4L2_COLORSPACE_SRGB,
  94. .priv = 2},
  95. {320, 240, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
  96. .bytesperline = 320,
  97. .sizeimage = 320 * 240,
  98. .colorspace = V4L2_COLORSPACE_SRGB,
  99. .priv = 1},
  100. {352, 288, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
  101. .bytesperline = 352,
  102. .sizeimage = 352 * 288,
  103. .colorspace = V4L2_COLORSPACE_SRGB,
  104. .priv = 0},
  105. };
  106. /*
  107. * Initialization data
  108. * I'm not very sure how to split initialization from open data
  109. * chunks. For now, we'll consider everything as initialization
  110. */
  111. /* Frame packet header offsets for the spca561 */
  112. #define SPCA561_OFFSET_SNAP 1
  113. #define SPCA561_OFFSET_TYPE 2
  114. #define SPCA561_OFFSET_COMPRESS 3
  115. #define SPCA561_OFFSET_FRAMSEQ 4
  116. #define SPCA561_OFFSET_GPIO 5
  117. #define SPCA561_OFFSET_USBBUFF 6
  118. #define SPCA561_OFFSET_WIN2GRAVE 7
  119. #define SPCA561_OFFSET_WIN2RAVE 8
  120. #define SPCA561_OFFSET_WIN2BAVE 9
  121. #define SPCA561_OFFSET_WIN2GBAVE 10
  122. #define SPCA561_OFFSET_WIN1GRAVE 11
  123. #define SPCA561_OFFSET_WIN1RAVE 12
  124. #define SPCA561_OFFSET_WIN1BAVE 13
  125. #define SPCA561_OFFSET_WIN1GBAVE 14
  126. #define SPCA561_OFFSET_FREQ 15
  127. #define SPCA561_OFFSET_VSYNC 16
  128. #define SPCA561_INDEX_I2C_BASE 0x8800
  129. #define SPCA561_SNAPBIT 0x20
  130. #define SPCA561_SNAPCTRL 0x40
  131. static const u16 rev72a_reset[][2] = {
  132. {0x0000, 0x8114}, /* Software GPIO output data */
  133. {0x0001, 0x8114}, /* Software GPIO output data */
  134. {0x0000, 0x8112}, /* Some kind of reset */
  135. {}
  136. };
  137. static const __u16 rev72a_init_data1[][2] = {
  138. {0x0003, 0x8701}, /* PCLK clock delay adjustment */
  139. {0x0001, 0x8703}, /* HSYNC from cmos inverted */
  140. {0x0011, 0x8118}, /* Enable and conf sensor */
  141. {0x0001, 0x8118}, /* Conf sensor */
  142. {0x0092, 0x8804}, /* I know nothing about these */
  143. {0x0010, 0x8802}, /* 0x88xx registers, so I won't */
  144. {}
  145. };
  146. static const u16 rev72a_init_sensor1[][2] = {
  147. {0x0001, 0x000d},
  148. {0x0002, 0x0018},
  149. {0x0004, 0x0165},
  150. {0x0005, 0x0021},
  151. {0x0007, 0x00aa},
  152. {0x0020, 0x1504},
  153. {0x0039, 0x0002},
  154. {0x0035, 0x0010},
  155. {0x0009, 0x1049},
  156. {0x0028, 0x000b},
  157. {0x003b, 0x000f},
  158. {0x003c, 0x0000},
  159. {}
  160. };
  161. static const __u16 rev72a_init_data2[][2] = {
  162. {0x0018, 0x8601}, /* Pixel/line selection for color separation */
  163. {0x0000, 0x8602}, /* Optical black level for user setting */
  164. {0x0060, 0x8604}, /* Optical black horizontal offset */
  165. {0x0002, 0x8605}, /* Optical black vertical offset */
  166. {0x0000, 0x8603}, /* Non-automatic optical black level */
  167. {0x0002, 0x865b}, /* Horizontal offset for valid pixels */
  168. {0x0000, 0x865f}, /* Vertical valid pixels window (x2) */
  169. {0x00b0, 0x865d}, /* Horizontal valid pixels window (x2) */
  170. {0x0090, 0x865e}, /* Vertical valid lines window (x2) */
  171. {0x00e0, 0x8406}, /* Memory buffer threshold */
  172. {0x0000, 0x8660}, /* Compensation memory stuff */
  173. {0x0002, 0x8201}, /* Output address for r/w serial EEPROM */
  174. {0x0008, 0x8200}, /* Clear valid bit for serial EEPROM */
  175. {0x0001, 0x8200}, /* OprMode to be executed by hardware */
  176. /* from ms-win */
  177. {0x0000, 0x8611}, /* R offset for white balance */
  178. {0x00fd, 0x8612}, /* Gr offset for white balance */
  179. {0x0003, 0x8613}, /* B offset for white balance */
  180. {0x0000, 0x8614}, /* Gb offset for white balance */
  181. /* from ms-win */
  182. {0x0035, 0x8651}, /* R gain for white balance */
  183. {0x0040, 0x8652}, /* Gr gain for white balance */
  184. {0x005f, 0x8653}, /* B gain for white balance */
  185. {0x0040, 0x8654}, /* Gb gain for white balance */
  186. {0x0002, 0x8502}, /* Maximum average bit rate stuff */
  187. {0x0011, 0x8802},
  188. {0x0087, 0x8700}, /* Set master clock (96Mhz????) */
  189. {0x0081, 0x8702}, /* Master clock output enable */
  190. {0x0000, 0x8500}, /* Set image type (352x288 no compression) */
  191. /* Originally was 0x0010 (352x288 compression) */
  192. {0x0002, 0x865b}, /* Horizontal offset for valid pixels */
  193. {0x0003, 0x865c}, /* Vertical offset for valid lines */
  194. {}
  195. };
  196. static const u16 rev72a_init_sensor2[][2] = {
  197. {0x0003, 0x0121},
  198. {0x0004, 0x0165},
  199. {0x0005, 0x002f}, /* blanking control column */
  200. {0x0006, 0x0000}, /* blanking mode row*/
  201. {0x000a, 0x0002},
  202. {0x0009, 0x1061}, /* setexposure times && pixel clock
  203. * 0001 0 | 000 0110 0001 */
  204. {0x0035, 0x0014},
  205. {}
  206. };
  207. /******************** QC Express etch2 stuff ********************/
  208. static const __u16 Pb100_1map8300[][2] = {
  209. /* reg, value */
  210. {0x8320, 0x3304},
  211. {0x8303, 0x0125}, /* image area */
  212. {0x8304, 0x0169},
  213. {0x8328, 0x000b},
  214. {0x833c, 0x0001}, /*fixme: win:07*/
  215. {0x832f, 0x1904}, /*fixme: was 0419*/
  216. {0x8307, 0x00aa},
  217. {0x8301, 0x0003},
  218. {0x8302, 0x000e},
  219. {}
  220. };
  221. static const __u16 Pb100_2map8300[][2] = {
  222. /* reg, value */
  223. {0x8339, 0x0000},
  224. {0x8307, 0x00aa},
  225. {}
  226. };
  227. static const __u16 spca561_161rev12A_data1[][2] = {
  228. {0x29, 0x8118}, /* white balance - was 21 */
  229. {0x08, 0x8114}, /* white balance - was 01 */
  230. {0x0e, 0x8112}, /* white balance - was 00 */
  231. {0x00, 0x8102}, /* white balance - new */
  232. {0x92, 0x8804},
  233. {0x04, 0x8802}, /* windows uses 08 */
  234. {}
  235. };
  236. static const __u16 spca561_161rev12A_data2[][2] = {
  237. {0x21, 0x8118},
  238. {0x10, 0x8500},
  239. {0x07, 0x8601},
  240. {0x07, 0x8602},
  241. {0x04, 0x8501},
  242. {0x21, 0x8118},
  243. {0x07, 0x8201}, /* windows uses 02 */
  244. {0x08, 0x8200},
  245. {0x01, 0x8200},
  246. {0x00, 0x8114},
  247. {0x01, 0x8114}, /* windows uses 00 */
  248. {0x90, 0x8604},
  249. {0x00, 0x8605},
  250. {0xb0, 0x8603},
  251. /* sensor gains */
  252. {0x07, 0x8601}, /* white balance - new */
  253. {0x07, 0x8602}, /* white balance - new */
  254. {0x00, 0x8610}, /* *red */
  255. {0x00, 0x8611}, /* 3f *green */
  256. {0x00, 0x8612}, /* green *blue */
  257. {0x00, 0x8613}, /* blue *green */
  258. {0x43, 0x8614}, /* green *red - white balance - was 0x35 */
  259. {0x40, 0x8615}, /* 40 *green - white balance - was 0x35 */
  260. {0x71, 0x8616}, /* 7a *blue - white balance - was 0x35 */
  261. {0x40, 0x8617}, /* 40 *green - white balance - was 0x35 */
  262. {0x0c, 0x8620}, /* 0c */
  263. {0xc8, 0x8631}, /* c8 */
  264. {0xc8, 0x8634}, /* c8 */
  265. {0x23, 0x8635}, /* 23 */
  266. {0x1f, 0x8636}, /* 1f */
  267. {0xdd, 0x8637}, /* dd */
  268. {0xe1, 0x8638}, /* e1 */
  269. {0x1d, 0x8639}, /* 1d */
  270. {0x21, 0x863a}, /* 21 */
  271. {0xe3, 0x863b}, /* e3 */
  272. {0xdf, 0x863c}, /* df */
  273. {0xf0, 0x8505},
  274. {0x32, 0x850a},
  275. /* {0x99, 0x8700}, * - white balance - new (removed) */
  276. {}
  277. };
  278. static void reg_w_val(struct usb_device *dev, __u16 index, __u8 value)
  279. {
  280. int ret;
  281. ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  282. 0, /* request */
  283. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  284. value, index, NULL, 0, 500);
  285. PDEBUG(D_USBO, "reg write: 0x%02x:0x%02x", index, value);
  286. if (ret < 0)
  287. PDEBUG(D_ERR, "reg write: error %d", ret);
  288. }
  289. static void write_vector(struct gspca_dev *gspca_dev,
  290. const __u16 data[][2])
  291. {
  292. struct usb_device *dev = gspca_dev->dev;
  293. int i;
  294. i = 0;
  295. while (data[i][1] != 0) {
  296. reg_w_val(dev, data[i][1], data[i][0]);
  297. i++;
  298. }
  299. }
  300. /* read 'len' bytes to gspca_dev->usb_buf */
  301. static void reg_r(struct gspca_dev *gspca_dev,
  302. __u16 index, __u16 length)
  303. {
  304. usb_control_msg(gspca_dev->dev,
  305. usb_rcvctrlpipe(gspca_dev->dev, 0),
  306. 0, /* request */
  307. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  308. 0, /* value */
  309. index, gspca_dev->usb_buf, length, 500);
  310. }
  311. /* write 'len' bytes from gspca_dev->usb_buf */
  312. static void reg_w_buf(struct gspca_dev *gspca_dev,
  313. __u16 index, __u16 len)
  314. {
  315. usb_control_msg(gspca_dev->dev,
  316. usb_sndctrlpipe(gspca_dev->dev, 0),
  317. 0, /* request */
  318. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  319. 0, /* value */
  320. index, gspca_dev->usb_buf, len, 500);
  321. }
  322. static void i2c_write(struct gspca_dev *gspca_dev, __u16 value, __u16 reg)
  323. {
  324. int retry = 60;
  325. reg_w_val(gspca_dev->dev, 0x8801, reg);
  326. reg_w_val(gspca_dev->dev, 0x8805, value);
  327. reg_w_val(gspca_dev->dev, 0x8800, value >> 8);
  328. do {
  329. reg_r(gspca_dev, 0x8803, 1);
  330. if (!gspca_dev->usb_buf[0])
  331. return;
  332. msleep(10);
  333. } while (--retry);
  334. }
  335. static int i2c_read(struct gspca_dev *gspca_dev, __u16 reg, __u8 mode)
  336. {
  337. int retry = 60;
  338. __u8 value;
  339. reg_w_val(gspca_dev->dev, 0x8804, 0x92);
  340. reg_w_val(gspca_dev->dev, 0x8801, reg);
  341. reg_w_val(gspca_dev->dev, 0x8802, mode | 0x01);
  342. do {
  343. reg_r(gspca_dev, 0x8803, 1);
  344. if (!gspca_dev->usb_buf[0]) {
  345. reg_r(gspca_dev, 0x8800, 1);
  346. value = gspca_dev->usb_buf[0];
  347. reg_r(gspca_dev, 0x8805, 1);
  348. return ((int) value << 8) | gspca_dev->usb_buf[0];
  349. }
  350. msleep(10);
  351. } while (--retry);
  352. return -1;
  353. }
  354. static void sensor_mapwrite(struct gspca_dev *gspca_dev,
  355. const __u16 (*sensormap)[2])
  356. {
  357. while ((*sensormap)[0]) {
  358. gspca_dev->usb_buf[0] = (*sensormap)[1];
  359. gspca_dev->usb_buf[1] = (*sensormap)[1] >> 8;
  360. reg_w_buf(gspca_dev, (*sensormap)[0], 2);
  361. sensormap++;
  362. }
  363. }
  364. static void write_sensor_72a(struct gspca_dev *gspca_dev,
  365. const __u16 (*sensor)[2])
  366. {
  367. while ((*sensor)[0]) {
  368. i2c_write(gspca_dev, (*sensor)[1], (*sensor)[0]);
  369. sensor++;
  370. }
  371. }
  372. static void init_161rev12A(struct gspca_dev *gspca_dev)
  373. {
  374. write_vector(gspca_dev, spca561_161rev12A_data1);
  375. sensor_mapwrite(gspca_dev, Pb100_1map8300);
  376. /*fixme: should be in sd_start*/
  377. write_vector(gspca_dev, spca561_161rev12A_data2);
  378. sensor_mapwrite(gspca_dev, Pb100_2map8300);
  379. }
  380. /* this function is called at probe time */
  381. static int sd_config(struct gspca_dev *gspca_dev,
  382. const struct usb_device_id *id)
  383. {
  384. struct sd *sd = (struct sd *) gspca_dev;
  385. struct cam *cam;
  386. __u16 vendor, product;
  387. __u8 data1, data2;
  388. /* Read frm global register the USB product and vendor IDs, just to
  389. * prove that we can communicate with the device. This works, which
  390. * confirms at we are communicating properly and that the device
  391. * is a 561. */
  392. reg_r(gspca_dev, 0x8104, 1);
  393. data1 = gspca_dev->usb_buf[0];
  394. reg_r(gspca_dev, 0x8105, 1);
  395. data2 = gspca_dev->usb_buf[0];
  396. vendor = (data2 << 8) | data1;
  397. reg_r(gspca_dev, 0x8106, 1);
  398. data1 = gspca_dev->usb_buf[0];
  399. reg_r(gspca_dev, 0x8107, 1);
  400. data2 = gspca_dev->usb_buf[0];
  401. product = (data2 << 8) | data1;
  402. if (vendor != id->idVendor || product != id->idProduct) {
  403. PDEBUG(D_PROBE, "Bad vendor / product from device");
  404. return -EINVAL;
  405. }
  406. cam = &gspca_dev->cam;
  407. gspca_dev->nbalt = 7 + 1; /* choose alternate 7 first */
  408. sd->chip_revision = id->driver_info;
  409. if (sd->chip_revision == Rev012A) {
  410. cam->cam_mode = sif_012a_mode;
  411. cam->nmodes = ARRAY_SIZE(sif_012a_mode);
  412. } else {
  413. cam->cam_mode = sif_072a_mode;
  414. cam->nmodes = ARRAY_SIZE(sif_072a_mode);
  415. }
  416. sd->brightness = BRIGHTNESS_DEF;
  417. sd->contrast = CONTRAST_DEF;
  418. sd->white = WHITE_DEF;
  419. sd->exposure = EXPOSURE_DEF;
  420. sd->autogain = AUTOGAIN_DEF;
  421. sd->gain = GAIN_DEF;
  422. sd->expo12a = EXPO12A_DEF;
  423. return 0;
  424. }
  425. /* this function is called at probe and resume time */
  426. static int sd_init_12a(struct gspca_dev *gspca_dev)
  427. {
  428. PDEBUG(D_STREAM, "Chip revision: 012a");
  429. init_161rev12A(gspca_dev);
  430. return 0;
  431. }
  432. static int sd_init_72a(struct gspca_dev *gspca_dev)
  433. {
  434. PDEBUG(D_STREAM, "Chip revision: 072a");
  435. write_vector(gspca_dev, rev72a_reset);
  436. msleep(200);
  437. write_vector(gspca_dev, rev72a_init_data1);
  438. write_sensor_72a(gspca_dev, rev72a_init_sensor1);
  439. write_vector(gspca_dev, rev72a_init_data2);
  440. write_sensor_72a(gspca_dev, rev72a_init_sensor2);
  441. reg_w_val(gspca_dev->dev, 0x8112, 0x30);
  442. return 0;
  443. }
  444. /* rev 72a only */
  445. static void setbrightness(struct gspca_dev *gspca_dev)
  446. {
  447. struct sd *sd = (struct sd *) gspca_dev;
  448. struct usb_device *dev = gspca_dev->dev;
  449. __u8 value;
  450. value = sd->brightness;
  451. /* offsets for white balance */
  452. reg_w_val(dev, 0x8611, value); /* R */
  453. reg_w_val(dev, 0x8612, value); /* Gr */
  454. reg_w_val(dev, 0x8613, value); /* B */
  455. reg_w_val(dev, 0x8614, value); /* Gb */
  456. }
  457. static void setwhite(struct gspca_dev *gspca_dev)
  458. {
  459. struct sd *sd = (struct sd *) gspca_dev;
  460. __u16 white;
  461. __u8 blue, red;
  462. __u16 reg;
  463. /* try to emulate MS-win as possible */
  464. white = sd->white;
  465. red = 0x20 + white * 3 / 8;
  466. blue = 0x90 - white * 5 / 8;
  467. if (sd->chip_revision == Rev012A) {
  468. reg = 0x8614;
  469. } else {
  470. reg = 0x8651;
  471. red += sd->contrast - 0x20;
  472. blue += sd->contrast - 0x20;
  473. }
  474. reg_w_val(gspca_dev->dev, reg, red);
  475. reg_w_val(gspca_dev->dev, reg + 2, blue);
  476. }
  477. static void setcontrast(struct gspca_dev *gspca_dev)
  478. {
  479. struct sd *sd = (struct sd *) gspca_dev;
  480. struct usb_device *dev = gspca_dev->dev;
  481. __u8 value;
  482. if (sd->chip_revision != Rev072A)
  483. return;
  484. value = sd->contrast + 0x20;
  485. /* gains for white balance */
  486. setwhite(gspca_dev);
  487. /* reg_w_val(dev, 0x8651, value); * R - done by setwhite */
  488. reg_w_val(dev, 0x8652, value); /* Gr */
  489. /* reg_w_val(dev, 0x8653, value); * B - done by setwhite */
  490. reg_w_val(dev, 0x8654, value); /* Gb */
  491. }
  492. /* rev 12a only */
  493. static void setexposure(struct gspca_dev *gspca_dev)
  494. {
  495. struct sd *sd = (struct sd *) gspca_dev;
  496. int expo;
  497. int clock_divider;
  498. /* Register 0x8309 controls exposure for the spca561,
  499. the basic exposure setting goes from 1-2047, where 1 is completely
  500. dark and 2047 is very bright. It not only influences exposure but
  501. also the framerate (to allow for longer exposure) from 1 - 300 it
  502. only raises the exposure time then from 300 - 600 it halves the
  503. framerate to be able to further raise the exposure time and for every
  504. 300 more it halves the framerate again. This allows for a maximum
  505. exposure time of circa 0.2 - 0.25 seconds (30 / (2000/3000) fps).
  506. Sometimes this is not enough, the 1-2047 uses bits 0-10, bits 11-12
  507. configure a divider for the base framerate which us used at the
  508. exposure setting of 1-300. These bits configure the base framerate
  509. according to the following formula: fps = 60 / (value + 2) */
  510. if (sd->exposure < 2048) {
  511. expo = sd->exposure;
  512. clock_divider = 0;
  513. } else {
  514. /* Add 900 to make the 0 setting of the second part of the
  515. exposure equal to the 2047 setting of the first part. */
  516. expo = (sd->exposure - 2048) + 900;
  517. clock_divider = 3;
  518. }
  519. expo |= clock_divider << 11;
  520. gspca_dev->usb_buf[0] = expo;
  521. gspca_dev->usb_buf[1] = expo >> 8;
  522. reg_w_buf(gspca_dev, 0x8309, 2);
  523. }
  524. /* rev 12a only */
  525. static void setgain(struct gspca_dev *gspca_dev)
  526. {
  527. struct sd *sd = (struct sd *) gspca_dev;
  528. gspca_dev->usb_buf[0] = sd->gain;
  529. gspca_dev->usb_buf[1] = 0;
  530. reg_w_buf(gspca_dev, 0x8335, 2);
  531. }
  532. static void setautogain(struct gspca_dev *gspca_dev)
  533. {
  534. struct sd *sd = (struct sd *) gspca_dev;
  535. if (sd->autogain)
  536. sd->ag_cnt = AG_CNT_START;
  537. else
  538. sd->ag_cnt = -1;
  539. }
  540. static int sd_start_12a(struct gspca_dev *gspca_dev)
  541. {
  542. struct usb_device *dev = gspca_dev->dev;
  543. int mode;
  544. static const __u8 Reg8391[8] =
  545. {0x92, 0x30, 0x20, 0x00, 0x0c, 0x00, 0x00, 0x00};
  546. mode = gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv;
  547. if (mode <= 1) {
  548. /* Use compression on 320x240 and above */
  549. reg_w_val(dev, 0x8500, 0x10 | mode);
  550. } else {
  551. /* I couldn't get the compression to work below 320x240
  552. * Fortunately at these resolutions the bandwidth
  553. * is sufficient to push raw frames at ~20fps */
  554. reg_w_val(dev, 0x8500, mode);
  555. } /* -- qq@kuku.eu.org */
  556. gspca_dev->usb_buf[0] = 0xaa;
  557. gspca_dev->usb_buf[1] = 0x00;
  558. reg_w_buf(gspca_dev, 0x8307, 2);
  559. /* clock - lower 0x8X values lead to fps > 30 */
  560. reg_w_val(gspca_dev->dev, 0x8700, 0x8a);
  561. /* 0x8f 0x85 0x27 clock */
  562. reg_w_val(gspca_dev->dev, 0x8112, 0x1e | 0x20);
  563. reg_w_val(gspca_dev->dev, 0x850b, 0x03);
  564. memcpy(gspca_dev->usb_buf, Reg8391, 8);
  565. reg_w_buf(gspca_dev, 0x8391, 8);
  566. reg_w_buf(gspca_dev, 0x8390, 8);
  567. setwhite(gspca_dev);
  568. setautogain(gspca_dev);
  569. /* setgain(gspca_dev); */
  570. setexposure(gspca_dev);
  571. return 0;
  572. }
  573. static int sd_start_72a(struct gspca_dev *gspca_dev)
  574. {
  575. struct usb_device *dev = gspca_dev->dev;
  576. int Clck;
  577. int mode;
  578. write_vector(gspca_dev, rev72a_reset);
  579. msleep(200);
  580. write_vector(gspca_dev, rev72a_init_data1);
  581. write_sensor_72a(gspca_dev, rev72a_init_sensor1);
  582. mode = gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv;
  583. switch (mode) {
  584. default:
  585. case 0:
  586. Clck = 0x27; /* ms-win 0x87 */
  587. break;
  588. case 1:
  589. Clck = 0x25;
  590. break;
  591. case 2:
  592. Clck = 0x22;
  593. break;
  594. case 3:
  595. Clck = 0x21;
  596. break;
  597. }
  598. reg_w_val(dev, 0x8700, Clck); /* 0x27 clock */
  599. reg_w_val(dev, 0x8702, 0x81);
  600. reg_w_val(dev, 0x8500, mode); /* mode */
  601. write_sensor_72a(gspca_dev, rev72a_init_sensor2);
  602. setcontrast(gspca_dev);
  603. /* setbrightness(gspca_dev); * fixme: bad values */
  604. setautogain(gspca_dev);
  605. reg_w_val(dev, 0x8112, 0x10 | 0x20);
  606. return 0;
  607. }
  608. static void sd_stopN(struct gspca_dev *gspca_dev)
  609. {
  610. struct sd *sd = (struct sd *) gspca_dev;
  611. if (sd->chip_revision == Rev012A) {
  612. reg_w_val(gspca_dev->dev, 0x8112, 0x0e);
  613. } else {
  614. reg_w_val(gspca_dev->dev, 0x8112, 0x20);
  615. /* reg_w_val(gspca_dev->dev, 0x8102, 0x00); ?? */
  616. }
  617. }
  618. /* called on streamoff with alt 0 and on disconnect */
  619. static void sd_stop0(struct gspca_dev *gspca_dev)
  620. {
  621. struct sd *sd = (struct sd *) gspca_dev;
  622. if (!gspca_dev->present)
  623. return;
  624. if (sd->chip_revision == Rev012A) {
  625. reg_w_val(gspca_dev->dev, 0x8118, 0x29);
  626. reg_w_val(gspca_dev->dev, 0x8114, 0x08);
  627. }
  628. /* reg_w_val(gspca_dev->dev, 0x8114, 0); */
  629. }
  630. static void do_autogain(struct gspca_dev *gspca_dev)
  631. {
  632. struct sd *sd = (struct sd *) gspca_dev;
  633. int expotimes;
  634. int pixelclk;
  635. int gainG;
  636. __u8 R, Gr, Gb, B;
  637. int y;
  638. __u8 luma_mean = 110;
  639. __u8 luma_delta = 20;
  640. __u8 spring = 4;
  641. if (sd->ag_cnt < 0)
  642. return;
  643. if (--sd->ag_cnt >= 0)
  644. return;
  645. sd->ag_cnt = AG_CNT_START;
  646. switch (sd->chip_revision) {
  647. case Rev072A:
  648. reg_r(gspca_dev, 0x8621, 1);
  649. Gr = gspca_dev->usb_buf[0];
  650. reg_r(gspca_dev, 0x8622, 1);
  651. R = gspca_dev->usb_buf[0];
  652. reg_r(gspca_dev, 0x8623, 1);
  653. B = gspca_dev->usb_buf[0];
  654. reg_r(gspca_dev, 0x8624, 1);
  655. Gb = gspca_dev->usb_buf[0];
  656. y = (77 * R + 75 * (Gr + Gb) + 29 * B) >> 8;
  657. /* u= (128*B-(43*(Gr+Gb+R))) >> 8; */
  658. /* v= (128*R-(53*(Gr+Gb))-21*B) >> 8; */
  659. /* PDEBUG(D_CONF,"reading Y %d U %d V %d ",y,u,v); */
  660. if (y < luma_mean - luma_delta ||
  661. y > luma_mean + luma_delta) {
  662. expotimes = i2c_read(gspca_dev, 0x09, 0x10);
  663. pixelclk = 0x0800;
  664. expotimes = expotimes & 0x07ff;
  665. /* PDEBUG(D_PACK,
  666. "Exposition Times 0x%03X Clock 0x%04X ",
  667. expotimes,pixelclk); */
  668. gainG = i2c_read(gspca_dev, 0x35, 0x10);
  669. /* PDEBUG(D_PACK,
  670. "reading Gain register %d", gainG); */
  671. expotimes += (luma_mean - y) >> spring;
  672. gainG += (luma_mean - y) / 50;
  673. /* PDEBUG(D_PACK,
  674. "compute expotimes %d gain %d",
  675. expotimes,gainG); */
  676. if (gainG > 0x3f)
  677. gainG = 0x3f;
  678. else if (gainG < 3)
  679. gainG = 3;
  680. i2c_write(gspca_dev, gainG, 0x35);
  681. if (expotimes > 0x0256)
  682. expotimes = 0x0256;
  683. else if (expotimes < 3)
  684. expotimes = 3;
  685. i2c_write(gspca_dev, expotimes | pixelclk, 0x09);
  686. }
  687. break;
  688. case Rev012A:
  689. reg_r(gspca_dev, 0x8330, 2);
  690. if (gspca_dev->usb_buf[1] > 0x08) {
  691. gspca_dev->usb_buf[0] = ++sd->expo12a;
  692. gspca_dev->usb_buf[1] = 0;
  693. reg_w_buf(gspca_dev, 0x8339, 2);
  694. } else if (gspca_dev->usb_buf[1] < 0x02) {
  695. gspca_dev->usb_buf[0] = --sd->expo12a;
  696. gspca_dev->usb_buf[1] = 0;
  697. reg_w_buf(gspca_dev, 0x8339, 2);
  698. }
  699. break;
  700. }
  701. }
  702. static void sd_pkt_scan(struct gspca_dev *gspca_dev,
  703. struct gspca_frame *frame, /* target */
  704. __u8 *data, /* isoc packet */
  705. int len) /* iso packet length */
  706. {
  707. struct sd *sd = (struct sd *) gspca_dev;
  708. len--;
  709. switch (*data++) { /* sequence number */
  710. case 0: /* start of frame */
  711. frame = gspca_frame_add(gspca_dev, LAST_PACKET, frame,
  712. data, 0);
  713. if (data[1] & 0x10) {
  714. /* compressed bayer */
  715. gspca_frame_add(gspca_dev, FIRST_PACKET,
  716. frame, data, len);
  717. } else {
  718. /* raw bayer (with a header, which we skip) */
  719. if (sd->chip_revision == Rev012A) {
  720. data += 20;
  721. len -= 20;
  722. } else {
  723. data += 16;
  724. len -= 16;
  725. }
  726. gspca_frame_add(gspca_dev, FIRST_PACKET,
  727. frame, data, len);
  728. }
  729. return;
  730. case 0xff: /* drop (empty mpackets) */
  731. return;
  732. }
  733. gspca_frame_add(gspca_dev, INTER_PACKET, frame, data, len);
  734. }
  735. /* rev 72a only */
  736. static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
  737. {
  738. struct sd *sd = (struct sd *) gspca_dev;
  739. sd->brightness = val;
  740. if (gspca_dev->streaming)
  741. setbrightness(gspca_dev);
  742. return 0;
  743. }
  744. static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
  745. {
  746. struct sd *sd = (struct sd *) gspca_dev;
  747. *val = sd->brightness;
  748. return 0;
  749. }
  750. /* rev 72a only */
  751. static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
  752. {
  753. struct sd *sd = (struct sd *) gspca_dev;
  754. sd->contrast = val;
  755. if (gspca_dev->streaming)
  756. setcontrast(gspca_dev);
  757. return 0;
  758. }
  759. static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val)
  760. {
  761. struct sd *sd = (struct sd *) gspca_dev;
  762. *val = sd->contrast;
  763. return 0;
  764. }
  765. static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val)
  766. {
  767. struct sd *sd = (struct sd *) gspca_dev;
  768. sd->autogain = val;
  769. if (gspca_dev->streaming)
  770. setautogain(gspca_dev);
  771. return 0;
  772. }
  773. static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val)
  774. {
  775. struct sd *sd = (struct sd *) gspca_dev;
  776. *val = sd->autogain;
  777. return 0;
  778. }
  779. static int sd_setwhite(struct gspca_dev *gspca_dev, __s32 val)
  780. {
  781. struct sd *sd = (struct sd *) gspca_dev;
  782. sd->white = val;
  783. if (gspca_dev->streaming)
  784. setwhite(gspca_dev);
  785. return 0;
  786. }
  787. static int sd_getwhite(struct gspca_dev *gspca_dev, __s32 *val)
  788. {
  789. struct sd *sd = (struct sd *) gspca_dev;
  790. *val = sd->white;
  791. return 0;
  792. }
  793. /* rev12a only */
  794. static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val)
  795. {
  796. struct sd *sd = (struct sd *) gspca_dev;
  797. sd->exposure = val;
  798. if (gspca_dev->streaming)
  799. setexposure(gspca_dev);
  800. return 0;
  801. }
  802. static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val)
  803. {
  804. struct sd *sd = (struct sd *) gspca_dev;
  805. *val = sd->exposure;
  806. return 0;
  807. }
  808. /* rev12a only */
  809. static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val)
  810. {
  811. struct sd *sd = (struct sd *) gspca_dev;
  812. sd->gain = val;
  813. if (gspca_dev->streaming)
  814. setgain(gspca_dev);
  815. return 0;
  816. }
  817. static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val)
  818. {
  819. struct sd *sd = (struct sd *) gspca_dev;
  820. *val = sd->gain;
  821. return 0;
  822. }
  823. /* control tables */
  824. static struct ctrl sd_ctrls_12a[] = {
  825. {
  826. {
  827. .id = V4L2_CID_DO_WHITE_BALANCE,
  828. .type = V4L2_CTRL_TYPE_INTEGER,
  829. .name = "White Balance",
  830. .minimum = WHITE_MIN,
  831. .maximum = WHITE_MAX,
  832. .step = 1,
  833. .default_value = WHITE_DEF,
  834. },
  835. .set = sd_setwhite,
  836. .get = sd_getwhite,
  837. },
  838. {
  839. {
  840. .id = V4L2_CID_EXPOSURE,
  841. .type = V4L2_CTRL_TYPE_INTEGER,
  842. .name = "Exposure",
  843. .minimum = EXPOSURE_MIN,
  844. .maximum = EXPOSURE_MAX,
  845. .step = 1,
  846. .default_value = EXPOSURE_DEF,
  847. },
  848. .set = sd_setexposure,
  849. .get = sd_getexposure,
  850. },
  851. {
  852. {
  853. .id = V4L2_CID_AUTOGAIN,
  854. .type = V4L2_CTRL_TYPE_BOOLEAN,
  855. .name = "Auto Gain",
  856. .minimum = AUTOGAIN_MIN,
  857. .maximum = AUTOGAIN_MAX,
  858. .step = 1,
  859. .default_value = AUTOGAIN_DEF,
  860. },
  861. .set = sd_setautogain,
  862. .get = sd_getautogain,
  863. },
  864. {
  865. {
  866. .id = V4L2_CID_GAIN,
  867. .type = V4L2_CTRL_TYPE_INTEGER,
  868. .name = "Gain",
  869. .minimum = GAIN_MIN,
  870. .maximum = GAIN_MAX,
  871. .step = 1,
  872. .default_value = GAIN_DEF,
  873. },
  874. .set = sd_setgain,
  875. .get = sd_getgain,
  876. },
  877. };
  878. static struct ctrl sd_ctrls_72a[] = {
  879. {
  880. {
  881. .id = V4L2_CID_DO_WHITE_BALANCE,
  882. .type = V4L2_CTRL_TYPE_INTEGER,
  883. .name = "White Balance",
  884. .minimum = WHITE_MIN,
  885. .maximum = WHITE_MAX,
  886. .step = 1,
  887. .default_value = WHITE_DEF,
  888. },
  889. .set = sd_setwhite,
  890. .get = sd_getwhite,
  891. },
  892. {
  893. {
  894. .id = V4L2_CID_BRIGHTNESS,
  895. .type = V4L2_CTRL_TYPE_INTEGER,
  896. .name = "Brightness",
  897. .minimum = BRIGHTNESS_MIN,
  898. .maximum = BRIGHTNESS_MAX,
  899. .step = 1,
  900. .default_value = BRIGHTNESS_DEF,
  901. },
  902. .set = sd_setbrightness,
  903. .get = sd_getbrightness,
  904. },
  905. {
  906. {
  907. .id = V4L2_CID_CONTRAST,
  908. .type = V4L2_CTRL_TYPE_INTEGER,
  909. .name = "Contrast",
  910. .minimum = CONTRAST_MIN,
  911. .maximum = CONTRAST_MAX,
  912. .step = 1,
  913. .default_value = CONTRAST_DEF,
  914. },
  915. .set = sd_setcontrast,
  916. .get = sd_getcontrast,
  917. },
  918. {
  919. {
  920. .id = V4L2_CID_AUTOGAIN,
  921. .type = V4L2_CTRL_TYPE_BOOLEAN,
  922. .name = "Auto Gain",
  923. .minimum = AUTOGAIN_MIN,
  924. .maximum = AUTOGAIN_MAX,
  925. .step = 1,
  926. .default_value = AUTOGAIN_DEF,
  927. },
  928. .set = sd_setautogain,
  929. .get = sd_getautogain,
  930. },
  931. };
  932. /* sub-driver description */
  933. static const struct sd_desc sd_desc_12a = {
  934. .name = MODULE_NAME,
  935. .ctrls = sd_ctrls_12a,
  936. .nctrls = ARRAY_SIZE(sd_ctrls_12a),
  937. .config = sd_config,
  938. .init = sd_init_12a,
  939. .start = sd_start_12a,
  940. .stopN = sd_stopN,
  941. .stop0 = sd_stop0,
  942. .pkt_scan = sd_pkt_scan,
  943. /* .dq_callback = do_autogain, * fixme */
  944. };
  945. static const struct sd_desc sd_desc_72a = {
  946. .name = MODULE_NAME,
  947. .ctrls = sd_ctrls_72a,
  948. .nctrls = ARRAY_SIZE(sd_ctrls_72a),
  949. .config = sd_config,
  950. .init = sd_init_72a,
  951. .start = sd_start_72a,
  952. .stopN = sd_stopN,
  953. .stop0 = sd_stop0,
  954. .pkt_scan = sd_pkt_scan,
  955. .dq_callback = do_autogain,
  956. };
  957. static const struct sd_desc *sd_desc[2] = {
  958. &sd_desc_12a,
  959. &sd_desc_72a
  960. };
  961. /* -- module initialisation -- */
  962. static const __devinitdata struct usb_device_id device_table[] = {
  963. {USB_DEVICE(0x041e, 0x401a), .driver_info = Rev072A},
  964. {USB_DEVICE(0x041e, 0x403b), .driver_info = Rev012A},
  965. {USB_DEVICE(0x0458, 0x7004), .driver_info = Rev072A},
  966. {USB_DEVICE(0x046d, 0x0928), .driver_info = Rev012A},
  967. {USB_DEVICE(0x046d, 0x0929), .driver_info = Rev012A},
  968. {USB_DEVICE(0x046d, 0x092a), .driver_info = Rev012A},
  969. {USB_DEVICE(0x046d, 0x092b), .driver_info = Rev012A},
  970. {USB_DEVICE(0x046d, 0x092c), .driver_info = Rev012A},
  971. {USB_DEVICE(0x046d, 0x092d), .driver_info = Rev012A},
  972. {USB_DEVICE(0x046d, 0x092e), .driver_info = Rev012A},
  973. {USB_DEVICE(0x046d, 0x092f), .driver_info = Rev012A},
  974. {USB_DEVICE(0x04fc, 0x0561), .driver_info = Rev072A},
  975. {USB_DEVICE(0x060b, 0xa001), .driver_info = Rev072A},
  976. {USB_DEVICE(0x10fd, 0x7e50), .driver_info = Rev072A},
  977. {USB_DEVICE(0xabcd, 0xcdee), .driver_info = Rev072A},
  978. {}
  979. };
  980. MODULE_DEVICE_TABLE(usb, device_table);
  981. /* -- device connect -- */
  982. static int sd_probe(struct usb_interface *intf,
  983. const struct usb_device_id *id)
  984. {
  985. return gspca_dev_probe(intf, id,
  986. sd_desc[id->driver_info],
  987. sizeof(struct sd),
  988. THIS_MODULE);
  989. }
  990. static struct usb_driver sd_driver = {
  991. .name = MODULE_NAME,
  992. .id_table = device_table,
  993. .probe = sd_probe,
  994. .disconnect = gspca_disconnect,
  995. #ifdef CONFIG_PM
  996. .suspend = gspca_suspend,
  997. .resume = gspca_resume,
  998. #endif
  999. };
  1000. /* -- module insert / remove -- */
  1001. static int __init sd_mod_init(void)
  1002. {
  1003. int ret;
  1004. ret = usb_register(&sd_driver);
  1005. if (ret < 0)
  1006. return ret;
  1007. PDEBUG(D_PROBE, "registered");
  1008. return 0;
  1009. }
  1010. static void __exit sd_mod_exit(void)
  1011. {
  1012. usb_deregister(&sd_driver);
  1013. PDEBUG(D_PROBE, "deregistered");
  1014. }
  1015. module_init(sd_mod_init);
  1016. module_exit(sd_mod_exit);