spca561.c 28 KB

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