etoms.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944
  1. /*
  2. * Etoms Et61x151 GPL Linux driver by Michel Xhaard (09/09/2004)
  3. *
  4. * V4L2 by Jean-Francois Moine <http://moinejf.free.fr>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #define MODULE_NAME "etoms"
  21. #include "gspca.h"
  22. MODULE_AUTHOR("Michel Xhaard <mxhaard@users.sourceforge.net>");
  23. MODULE_DESCRIPTION("Etoms USB Camera Driver");
  24. MODULE_LICENSE("GPL");
  25. /* specific webcam descriptor */
  26. struct sd {
  27. struct gspca_dev gspca_dev; /* !! must be the first item */
  28. unsigned char brightness;
  29. unsigned char contrast;
  30. unsigned char colors;
  31. unsigned char autogain;
  32. char sensor;
  33. #define SENSOR_PAS106 0
  34. #define SENSOR_TAS5130CXX 1
  35. signed char ag_cnt;
  36. #define AG_CNT_START 13
  37. };
  38. /* V4L2 controls supported by the driver */
  39. static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
  40. static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
  41. static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val);
  42. static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val);
  43. static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val);
  44. static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val);
  45. static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val);
  46. static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val);
  47. static struct ctrl sd_ctrls[] = {
  48. {
  49. {
  50. .id = V4L2_CID_BRIGHTNESS,
  51. .type = V4L2_CTRL_TYPE_INTEGER,
  52. .name = "Brightness",
  53. .minimum = 1,
  54. .maximum = 127,
  55. .step = 1,
  56. #define BRIGHTNESS_DEF 63
  57. .default_value = BRIGHTNESS_DEF,
  58. },
  59. .set = sd_setbrightness,
  60. .get = sd_getbrightness,
  61. },
  62. {
  63. {
  64. .id = V4L2_CID_CONTRAST,
  65. .type = V4L2_CTRL_TYPE_INTEGER,
  66. .name = "Contrast",
  67. .minimum = 0,
  68. .maximum = 255,
  69. .step = 1,
  70. #define CONTRAST_DEF 127
  71. .default_value = CONTRAST_DEF,
  72. },
  73. .set = sd_setcontrast,
  74. .get = sd_getcontrast,
  75. },
  76. #define COLOR_IDX 2
  77. {
  78. {
  79. .id = V4L2_CID_SATURATION,
  80. .type = V4L2_CTRL_TYPE_INTEGER,
  81. .name = "Color",
  82. .minimum = 0,
  83. .maximum = 15,
  84. .step = 1,
  85. #define COLOR_DEF 7
  86. .default_value = COLOR_DEF,
  87. },
  88. .set = sd_setcolors,
  89. .get = sd_getcolors,
  90. },
  91. {
  92. {
  93. .id = V4L2_CID_AUTOGAIN,
  94. .type = V4L2_CTRL_TYPE_BOOLEAN,
  95. .name = "Auto Gain",
  96. .minimum = 0,
  97. .maximum = 1,
  98. .step = 1,
  99. #define AUTOGAIN_DEF 1
  100. .default_value = AUTOGAIN_DEF,
  101. },
  102. .set = sd_setautogain,
  103. .get = sd_getautogain,
  104. },
  105. };
  106. static const struct v4l2_pix_format vga_mode[] = {
  107. {320, 240, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
  108. .bytesperline = 320,
  109. .sizeimage = 320 * 240,
  110. .colorspace = V4L2_COLORSPACE_SRGB,
  111. .priv = 1},
  112. /* {640, 480, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
  113. .bytesperline = 640,
  114. .sizeimage = 640 * 480,
  115. .colorspace = V4L2_COLORSPACE_SRGB,
  116. .priv = 0}, */
  117. };
  118. static const struct v4l2_pix_format sif_mode[] = {
  119. {176, 144, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
  120. .bytesperline = 176,
  121. .sizeimage = 176 * 144,
  122. .colorspace = V4L2_COLORSPACE_SRGB,
  123. .priv = 1},
  124. {352, 288, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
  125. .bytesperline = 352,
  126. .sizeimage = 352 * 288,
  127. .colorspace = V4L2_COLORSPACE_SRGB,
  128. .priv = 0},
  129. };
  130. #define ETOMS_ALT_SIZE_1000 12
  131. #define ET_GPIO_DIR_CTRL 0x04 /* Control IO bit[0..5] (0 in 1 out) */
  132. #define ET_GPIO_OUT 0x05 /* Only IO data */
  133. #define ET_GPIO_IN 0x06 /* Read Only IO data */
  134. #define ET_RESET_ALL 0x03
  135. #define ET_ClCK 0x01
  136. #define ET_CTRL 0x02 /* enable i2c OutClck Powerdown mode */
  137. #define ET_COMP 0x12 /* Compression register */
  138. #define ET_MAXQt 0x13
  139. #define ET_MINQt 0x14
  140. #define ET_COMP_VAL0 0x02
  141. #define ET_COMP_VAL1 0x03
  142. #define ET_REG1d 0x1d
  143. #define ET_REG1e 0x1e
  144. #define ET_REG1f 0x1f
  145. #define ET_REG20 0x20
  146. #define ET_REG21 0x21
  147. #define ET_REG22 0x22
  148. #define ET_REG23 0x23
  149. #define ET_REG24 0x24
  150. #define ET_REG25 0x25
  151. /* base registers for luma calculation */
  152. #define ET_LUMA_CENTER 0x39
  153. #define ET_G_RED 0x4d
  154. #define ET_G_GREEN1 0x4e
  155. #define ET_G_BLUE 0x4f
  156. #define ET_G_GREEN2 0x50
  157. #define ET_G_GR_H 0x51
  158. #define ET_G_GB_H 0x52
  159. #define ET_O_RED 0x34
  160. #define ET_O_GREEN1 0x35
  161. #define ET_O_BLUE 0x36
  162. #define ET_O_GREEN2 0x37
  163. #define ET_SYNCHRO 0x68
  164. #define ET_STARTX 0x69
  165. #define ET_STARTY 0x6a
  166. #define ET_WIDTH_LOW 0x6b
  167. #define ET_HEIGTH_LOW 0x6c
  168. #define ET_W_H_HEIGTH 0x6d
  169. #define ET_REG6e 0x6e /* OBW */
  170. #define ET_REG6f 0x6f /* OBW */
  171. #define ET_REG70 0x70 /* OBW_AWB */
  172. #define ET_REG71 0x71 /* OBW_AWB */
  173. #define ET_REG72 0x72 /* OBW_AWB */
  174. #define ET_REG73 0x73 /* Clkdelay ns */
  175. #define ET_REG74 0x74 /* test pattern */
  176. #define ET_REG75 0x75 /* test pattern */
  177. #define ET_I2C_CLK 0x8c
  178. #define ET_PXL_CLK 0x60
  179. #define ET_I2C_BASE 0x89
  180. #define ET_I2C_COUNT 0x8a
  181. #define ET_I2C_PREFETCH 0x8b
  182. #define ET_I2C_REG 0x88
  183. #define ET_I2C_DATA7 0x87
  184. #define ET_I2C_DATA6 0x86
  185. #define ET_I2C_DATA5 0x85
  186. #define ET_I2C_DATA4 0x84
  187. #define ET_I2C_DATA3 0x83
  188. #define ET_I2C_DATA2 0x82
  189. #define ET_I2C_DATA1 0x81
  190. #define ET_I2C_DATA0 0x80
  191. #define PAS106_REG2 0x02 /* pxlClk = systemClk/(reg2) */
  192. #define PAS106_REG3 0x03 /* line/frame H [11..4] */
  193. #define PAS106_REG4 0x04 /* line/frame L [3..0] */
  194. #define PAS106_REG5 0x05 /* exposure time line offset(default 5) */
  195. #define PAS106_REG6 0x06 /* exposure time pixel offset(default 6) */
  196. #define PAS106_REG7 0x07 /* signbit Dac (default 0) */
  197. #define PAS106_REG9 0x09
  198. #define PAS106_REG0e 0x0e /* global gain [4..0](default 0x0e) */
  199. #define PAS106_REG13 0x13 /* end i2c write */
  200. static const __u8 GainRGBG[] = { 0x80, 0x80, 0x80, 0x80, 0x00, 0x00 };
  201. static const __u8 I2c2[] = { 0x08, 0x08, 0x08, 0x08, 0x0d };
  202. static const __u8 I2c3[] = { 0x12, 0x05 };
  203. static const __u8 I2c4[] = { 0x41, 0x08 };
  204. /* read 'len' bytes to gspca_dev->usb_buf */
  205. static void reg_r(struct gspca_dev *gspca_dev,
  206. __u16 index,
  207. __u16 len)
  208. {
  209. struct usb_device *dev = gspca_dev->dev;
  210. #ifdef GSPCA_DEBUG
  211. if (len > USB_BUF_SZ) {
  212. err("reg_r: buffer overflow");
  213. return;
  214. }
  215. #endif
  216. usb_control_msg(dev,
  217. usb_rcvctrlpipe(dev, 0),
  218. 0,
  219. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
  220. 0,
  221. index, gspca_dev->usb_buf, len, 500);
  222. PDEBUG(D_USBI, "reg read [%02x] -> %02x ..",
  223. index, gspca_dev->usb_buf[0]);
  224. }
  225. static void reg_w_val(struct gspca_dev *gspca_dev,
  226. __u16 index,
  227. __u8 val)
  228. {
  229. struct usb_device *dev = gspca_dev->dev;
  230. gspca_dev->usb_buf[0] = val;
  231. usb_control_msg(dev,
  232. usb_sndctrlpipe(dev, 0),
  233. 0,
  234. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
  235. 0,
  236. index, gspca_dev->usb_buf, 1, 500);
  237. }
  238. static void reg_w(struct gspca_dev *gspca_dev,
  239. __u16 index,
  240. const __u8 *buffer,
  241. __u16 len)
  242. {
  243. struct usb_device *dev = gspca_dev->dev;
  244. #ifdef GSPCA_DEBUG
  245. if (len > USB_BUF_SZ) {
  246. err("reg_w: buffer overflow");
  247. return;
  248. }
  249. PDEBUG(D_USBO, "reg write [%02x] = %02x..", index, *buffer);
  250. #endif
  251. memcpy(gspca_dev->usb_buf, buffer, len);
  252. usb_control_msg(dev,
  253. usb_sndctrlpipe(dev, 0),
  254. 0,
  255. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
  256. 0, index, gspca_dev->usb_buf, len, 500);
  257. }
  258. static int i2c_w(struct gspca_dev *gspca_dev,
  259. __u8 reg,
  260. const __u8 *buffer,
  261. int len, __u8 mode)
  262. {
  263. /* buffer should be [D0..D7] */
  264. __u8 ptchcount;
  265. /* set the base address */
  266. reg_w_val(gspca_dev, ET_I2C_BASE, 0x40);
  267. /* sensor base for the pas106 */
  268. /* set count and prefetch */
  269. ptchcount = ((len & 0x07) << 4) | (mode & 0x03);
  270. reg_w_val(gspca_dev, ET_I2C_COUNT, ptchcount);
  271. /* set the register base */
  272. reg_w_val(gspca_dev, ET_I2C_REG, reg);
  273. while (--len >= 0)
  274. reg_w_val(gspca_dev, ET_I2C_DATA0 + len, buffer[len]);
  275. return 0;
  276. }
  277. static int i2c_r(struct gspca_dev *gspca_dev,
  278. __u8 reg)
  279. {
  280. /* set the base address */
  281. reg_w_val(gspca_dev, ET_I2C_BASE, 0x40);
  282. /* sensor base for the pas106 */
  283. /* set count and prefetch (cnd: 4 bits - mode: 4 bits) */
  284. reg_w_val(gspca_dev, ET_I2C_COUNT, 0x11);
  285. reg_w_val(gspca_dev, ET_I2C_REG, reg); /* set the register base */
  286. reg_w_val(gspca_dev, ET_I2C_PREFETCH, 0x02); /* prefetch */
  287. reg_w_val(gspca_dev, ET_I2C_PREFETCH, 0x00);
  288. reg_r(gspca_dev, ET_I2C_DATA0, 1); /* read one byte */
  289. return 0;
  290. }
  291. static int Et_WaitStatus(struct gspca_dev *gspca_dev)
  292. {
  293. int retry = 10;
  294. while (retry--) {
  295. reg_r(gspca_dev, ET_ClCK, 1);
  296. if (gspca_dev->usb_buf[0] != 0)
  297. return 1;
  298. }
  299. return 0;
  300. }
  301. static int et_video(struct gspca_dev *gspca_dev,
  302. int on)
  303. {
  304. int ret;
  305. reg_w_val(gspca_dev, ET_GPIO_OUT,
  306. on ? 0x10 /* startvideo - set Bit5 */
  307. : 0); /* stopvideo */
  308. ret = Et_WaitStatus(gspca_dev);
  309. if (ret != 0)
  310. PDEBUG(D_ERR, "timeout video on/off");
  311. return ret;
  312. }
  313. static void Et_init2(struct gspca_dev *gspca_dev)
  314. {
  315. __u8 value;
  316. static const __u8 FormLine[] = { 0x84, 0x03, 0x14, 0xf4, 0x01, 0x05 };
  317. PDEBUG(D_STREAM, "Open Init2 ET");
  318. reg_w_val(gspca_dev, ET_GPIO_DIR_CTRL, 0x2f);
  319. reg_w_val(gspca_dev, ET_GPIO_OUT, 0x10);
  320. reg_r(gspca_dev, ET_GPIO_IN, 1);
  321. reg_w_val(gspca_dev, ET_ClCK, 0x14); /* 0x14 // 0x16 enabled pattern */
  322. reg_w_val(gspca_dev, ET_CTRL, 0x1b);
  323. /* compression et subsampling */
  324. if (gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv)
  325. value = ET_COMP_VAL1; /* 320 */
  326. else
  327. value = ET_COMP_VAL0; /* 640 */
  328. reg_w_val(gspca_dev, ET_COMP, value);
  329. reg_w_val(gspca_dev, ET_MAXQt, 0x1f);
  330. reg_w_val(gspca_dev, ET_MINQt, 0x04);
  331. /* undocumented registers */
  332. reg_w_val(gspca_dev, ET_REG1d, 0xff);
  333. reg_w_val(gspca_dev, ET_REG1e, 0xff);
  334. reg_w_val(gspca_dev, ET_REG1f, 0xff);
  335. reg_w_val(gspca_dev, ET_REG20, 0x35);
  336. reg_w_val(gspca_dev, ET_REG21, 0x01);
  337. reg_w_val(gspca_dev, ET_REG22, 0x00);
  338. reg_w_val(gspca_dev, ET_REG23, 0xff);
  339. reg_w_val(gspca_dev, ET_REG24, 0xff);
  340. reg_w_val(gspca_dev, ET_REG25, 0x0f);
  341. /* colors setting */
  342. reg_w_val(gspca_dev, 0x30, 0x11); /* 0x30 */
  343. reg_w_val(gspca_dev, 0x31, 0x40);
  344. reg_w_val(gspca_dev, 0x32, 0x00);
  345. reg_w_val(gspca_dev, ET_O_RED, 0x00); /* 0x34 */
  346. reg_w_val(gspca_dev, ET_O_GREEN1, 0x00);
  347. reg_w_val(gspca_dev, ET_O_BLUE, 0x00);
  348. reg_w_val(gspca_dev, ET_O_GREEN2, 0x00);
  349. /*************/
  350. reg_w_val(gspca_dev, ET_G_RED, 0x80); /* 0x4d */
  351. reg_w_val(gspca_dev, ET_G_GREEN1, 0x80);
  352. reg_w_val(gspca_dev, ET_G_BLUE, 0x80);
  353. reg_w_val(gspca_dev, ET_G_GREEN2, 0x80);
  354. reg_w_val(gspca_dev, ET_G_GR_H, 0x00);
  355. reg_w_val(gspca_dev, ET_G_GB_H, 0x00); /* 0x52 */
  356. /* Window control registers */
  357. reg_w_val(gspca_dev, 0x61, 0x80); /* use cmc_out */
  358. reg_w_val(gspca_dev, 0x62, 0x02);
  359. reg_w_val(gspca_dev, 0x63, 0x03);
  360. reg_w_val(gspca_dev, 0x64, 0x14);
  361. reg_w_val(gspca_dev, 0x65, 0x0e);
  362. reg_w_val(gspca_dev, 0x66, 0x02);
  363. reg_w_val(gspca_dev, 0x67, 0x02);
  364. /**************************************/
  365. reg_w_val(gspca_dev, ET_SYNCHRO, 0x8f); /* 0x68 */
  366. reg_w_val(gspca_dev, ET_STARTX, 0x69); /* 0x6a //0x69 */
  367. reg_w_val(gspca_dev, ET_STARTY, 0x0d); /* 0x0d //0x0c */
  368. reg_w_val(gspca_dev, ET_WIDTH_LOW, 0x80);
  369. reg_w_val(gspca_dev, ET_HEIGTH_LOW, 0xe0);
  370. reg_w_val(gspca_dev, ET_W_H_HEIGTH, 0x60); /* 6d */
  371. reg_w_val(gspca_dev, ET_REG6e, 0x86);
  372. reg_w_val(gspca_dev, ET_REG6f, 0x01);
  373. reg_w_val(gspca_dev, ET_REG70, 0x26);
  374. reg_w_val(gspca_dev, ET_REG71, 0x7a);
  375. reg_w_val(gspca_dev, ET_REG72, 0x01);
  376. /* Clock Pattern registers ***************** */
  377. reg_w_val(gspca_dev, ET_REG73, 0x00);
  378. reg_w_val(gspca_dev, ET_REG74, 0x18); /* 0x28 */
  379. reg_w_val(gspca_dev, ET_REG75, 0x0f); /* 0x01 */
  380. /**********************************************/
  381. reg_w_val(gspca_dev, 0x8a, 0x20);
  382. reg_w_val(gspca_dev, 0x8d, 0x0f);
  383. reg_w_val(gspca_dev, 0x8e, 0x08);
  384. /**************************************/
  385. reg_w_val(gspca_dev, 0x03, 0x08);
  386. reg_w_val(gspca_dev, ET_PXL_CLK, 0x03);
  387. reg_w_val(gspca_dev, 0x81, 0xff);
  388. reg_w_val(gspca_dev, 0x80, 0x00);
  389. reg_w_val(gspca_dev, 0x81, 0xff);
  390. reg_w_val(gspca_dev, 0x80, 0x20);
  391. reg_w_val(gspca_dev, 0x03, 0x01);
  392. reg_w_val(gspca_dev, 0x03, 0x00);
  393. reg_w_val(gspca_dev, 0x03, 0x08);
  394. /********************************************/
  395. /* reg_r(gspca_dev, ET_I2C_BASE, 1);
  396. always 0x40 as the pas106 ??? */
  397. /* set the sensor */
  398. if (gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv)
  399. value = 0x04; /* 320 */
  400. else /* 640 */
  401. value = 0x1e; /* 0x17 * setting PixelClock
  402. * 0x03 mean 24/(3+1) = 6 Mhz
  403. * 0x05 -> 24/(5+1) = 4 Mhz
  404. * 0x0b -> 24/(11+1) = 2 Mhz
  405. * 0x17 -> 24/(23+1) = 1 Mhz
  406. */
  407. reg_w_val(gspca_dev, ET_PXL_CLK, value);
  408. /* now set by fifo the FormatLine setting */
  409. reg_w(gspca_dev, 0x62, FormLine, 6);
  410. /* set exposure times [ 0..0x78] 0->longvalue 0x78->shortvalue */
  411. reg_w_val(gspca_dev, 0x81, 0x47); /* 0x47; */
  412. reg_w_val(gspca_dev, 0x80, 0x40); /* 0x40; */
  413. /* Pedro change */
  414. /* Brightness change Brith+ decrease value */
  415. /* Brigth- increase value */
  416. /* original value = 0x70; */
  417. reg_w_val(gspca_dev, 0x81, 0x30); /* 0x20; - set brightness */
  418. reg_w_val(gspca_dev, 0x80, 0x20); /* 0x20; */
  419. }
  420. static void setbrightness(struct gspca_dev *gspca_dev)
  421. {
  422. struct sd *sd = (struct sd *) gspca_dev;
  423. int i;
  424. __u8 brightness = sd->brightness;
  425. for (i = 0; i < 4; i++)
  426. reg_w_val(gspca_dev, ET_O_RED + i, brightness);
  427. }
  428. static void getbrightness(struct gspca_dev *gspca_dev)
  429. {
  430. struct sd *sd = (struct sd *) gspca_dev;
  431. int i;
  432. int brightness = 0;
  433. for (i = 0; i < 4; i++) {
  434. reg_r(gspca_dev, ET_O_RED + i, 1);
  435. brightness += gspca_dev->usb_buf[0];
  436. }
  437. sd->brightness = brightness >> 3;
  438. }
  439. static void setcontrast(struct gspca_dev *gspca_dev)
  440. {
  441. struct sd *sd = (struct sd *) gspca_dev;
  442. __u8 RGBG[] = { 0x80, 0x80, 0x80, 0x80, 0x00, 0x00 };
  443. __u8 contrast = sd->contrast;
  444. memset(RGBG, contrast, sizeof(RGBG) - 2);
  445. reg_w(gspca_dev, ET_G_RED, RGBG, 6);
  446. }
  447. static void getcontrast(struct gspca_dev *gspca_dev)
  448. {
  449. struct sd *sd = (struct sd *) gspca_dev;
  450. int i;
  451. int contrast = 0;
  452. for (i = 0; i < 4; i++) {
  453. reg_r(gspca_dev, ET_G_RED + i, 1);
  454. contrast += gspca_dev->usb_buf[0];
  455. }
  456. sd->contrast = contrast >> 2;
  457. }
  458. static void setcolors(struct gspca_dev *gspca_dev)
  459. {
  460. struct sd *sd = (struct sd *) gspca_dev;
  461. __u8 I2cc[] = { 0x05, 0x02, 0x02, 0x05, 0x0d };
  462. __u8 i2cflags = 0x01;
  463. /* __u8 green = 0; */
  464. __u8 colors = sd->colors;
  465. I2cc[3] = colors; /* red */
  466. I2cc[0] = 15 - colors; /* blue */
  467. /* green = 15 - ((((7*I2cc[0]) >> 2 ) + I2cc[3]) >> 1); */
  468. /* I2cc[1] = I2cc[2] = green; */
  469. if (sd->sensor == SENSOR_PAS106) {
  470. i2c_w(gspca_dev, PAS106_REG13, &i2cflags, 1, 3);
  471. i2c_w(gspca_dev, PAS106_REG9, I2cc, sizeof I2cc, 1);
  472. }
  473. /* PDEBUG(D_CONF , "Etoms red %d blue %d green %d",
  474. I2cc[3], I2cc[0], green); */
  475. }
  476. static void getcolors(struct gspca_dev *gspca_dev)
  477. {
  478. struct sd *sd = (struct sd *) gspca_dev;
  479. if (sd->sensor == SENSOR_PAS106) {
  480. /* i2c_r(gspca_dev, PAS106_REG9); * blue */
  481. i2c_r(gspca_dev, PAS106_REG9 + 3); /* red */
  482. sd->colors = gspca_dev->usb_buf[0] & 0x0f;
  483. }
  484. }
  485. static void setautogain(struct gspca_dev *gspca_dev)
  486. {
  487. struct sd *sd = (struct sd *) gspca_dev;
  488. if (sd->autogain)
  489. sd->ag_cnt = AG_CNT_START;
  490. else
  491. sd->ag_cnt = -1;
  492. }
  493. static void Et_init1(struct gspca_dev *gspca_dev)
  494. {
  495. __u8 value;
  496. /* __u8 I2c0 [] = {0x0a, 0x12, 0x05, 0x22, 0xac, 0x00, 0x01, 0x00}; */
  497. __u8 I2c0[] = { 0x0a, 0x12, 0x05, 0x6d, 0xcd, 0x00, 0x01, 0x00 };
  498. /* try 1/120 0x6d 0xcd 0x40 */
  499. /* __u8 I2c0 [] = {0x0a, 0x12, 0x05, 0xfe, 0xfe, 0xc0, 0x01, 0x00};
  500. * 1/60000 hmm ?? */
  501. PDEBUG(D_STREAM, "Open Init1 ET");
  502. reg_w_val(gspca_dev, ET_GPIO_DIR_CTRL, 7);
  503. reg_r(gspca_dev, ET_GPIO_IN, 1);
  504. reg_w_val(gspca_dev, ET_RESET_ALL, 1);
  505. reg_w_val(gspca_dev, ET_RESET_ALL, 0);
  506. reg_w_val(gspca_dev, ET_ClCK, 0x10);
  507. reg_w_val(gspca_dev, ET_CTRL, 0x19);
  508. /* compression et subsampling */
  509. if (gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv)
  510. value = ET_COMP_VAL1;
  511. else
  512. value = ET_COMP_VAL0;
  513. PDEBUG(D_STREAM, "Open mode %d Compression %d",
  514. gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv,
  515. value);
  516. reg_w_val(gspca_dev, ET_COMP, value);
  517. reg_w_val(gspca_dev, ET_MAXQt, 0x1d);
  518. reg_w_val(gspca_dev, ET_MINQt, 0x02);
  519. /* undocumented registers */
  520. reg_w_val(gspca_dev, ET_REG1d, 0xff);
  521. reg_w_val(gspca_dev, ET_REG1e, 0xff);
  522. reg_w_val(gspca_dev, ET_REG1f, 0xff);
  523. reg_w_val(gspca_dev, ET_REG20, 0x35);
  524. reg_w_val(gspca_dev, ET_REG21, 0x01);
  525. reg_w_val(gspca_dev, ET_REG22, 0x00);
  526. reg_w_val(gspca_dev, ET_REG23, 0xf7);
  527. reg_w_val(gspca_dev, ET_REG24, 0xff);
  528. reg_w_val(gspca_dev, ET_REG25, 0x07);
  529. /* colors setting */
  530. reg_w_val(gspca_dev, ET_G_RED, 0x80);
  531. reg_w_val(gspca_dev, ET_G_GREEN1, 0x80);
  532. reg_w_val(gspca_dev, ET_G_BLUE, 0x80);
  533. reg_w_val(gspca_dev, ET_G_GREEN2, 0x80);
  534. reg_w_val(gspca_dev, ET_G_GR_H, 0x00);
  535. reg_w_val(gspca_dev, ET_G_GB_H, 0x00);
  536. /* Window control registers */
  537. reg_w_val(gspca_dev, ET_SYNCHRO, 0xf0);
  538. reg_w_val(gspca_dev, ET_STARTX, 0x56); /* 0x56 */
  539. reg_w_val(gspca_dev, ET_STARTY, 0x05); /* 0x04 */
  540. reg_w_val(gspca_dev, ET_WIDTH_LOW, 0x60);
  541. reg_w_val(gspca_dev, ET_HEIGTH_LOW, 0x20);
  542. reg_w_val(gspca_dev, ET_W_H_HEIGTH, 0x50);
  543. reg_w_val(gspca_dev, ET_REG6e, 0x86);
  544. reg_w_val(gspca_dev, ET_REG6f, 0x01);
  545. reg_w_val(gspca_dev, ET_REG70, 0x86);
  546. reg_w_val(gspca_dev, ET_REG71, 0x14);
  547. reg_w_val(gspca_dev, ET_REG72, 0x00);
  548. /* Clock Pattern registers */
  549. reg_w_val(gspca_dev, ET_REG73, 0x00);
  550. reg_w_val(gspca_dev, ET_REG74, 0x00);
  551. reg_w_val(gspca_dev, ET_REG75, 0x0a);
  552. reg_w_val(gspca_dev, ET_I2C_CLK, 0x04);
  553. reg_w_val(gspca_dev, ET_PXL_CLK, 0x01);
  554. /* set the sensor */
  555. if (gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv) {
  556. I2c0[0] = 0x06;
  557. i2c_w(gspca_dev, PAS106_REG2, I2c0, sizeof I2c0, 1);
  558. i2c_w(gspca_dev, PAS106_REG9, I2c2, sizeof I2c2, 1);
  559. value = 0x06;
  560. i2c_w(gspca_dev, PAS106_REG2, &value, 1, 1);
  561. i2c_w(gspca_dev, PAS106_REG3, I2c3, sizeof I2c3, 1);
  562. /* value = 0x1f; */
  563. value = 0x04;
  564. i2c_w(gspca_dev, PAS106_REG0e, &value, 1, 1);
  565. } else {
  566. I2c0[0] = 0x0a;
  567. i2c_w(gspca_dev, PAS106_REG2, I2c0, sizeof I2c0, 1);
  568. i2c_w(gspca_dev, PAS106_REG9, I2c2, sizeof I2c2, 1);
  569. value = 0x0a;
  570. i2c_w(gspca_dev, PAS106_REG2, &value, 1, 1);
  571. i2c_w(gspca_dev, PAS106_REG3, I2c3, sizeof I2c3, 1);
  572. value = 0x04;
  573. /* value = 0x10; */
  574. i2c_w(gspca_dev, PAS106_REG0e, &value, 1, 1);
  575. /* bit 2 enable bit 1:2 select 0 1 2 3
  576. value = 0x07; * curve 0 *
  577. i2c_w(gspca_dev, PAS106_REG0f, &value, 1, 1);
  578. */
  579. }
  580. /* value = 0x01; */
  581. /* value = 0x22; */
  582. /* i2c_w(gspca_dev, PAS106_REG5, &value, 1, 1); */
  583. /* magnetude and sign bit for DAC */
  584. i2c_w(gspca_dev, PAS106_REG7, I2c4, sizeof I2c4, 1);
  585. /* now set by fifo the whole colors setting */
  586. reg_w(gspca_dev, ET_G_RED, GainRGBG, 6);
  587. getcolors(gspca_dev);
  588. setcolors(gspca_dev);
  589. }
  590. /* this function is called at probe time */
  591. static int sd_config(struct gspca_dev *gspca_dev,
  592. const struct usb_device_id *id)
  593. {
  594. struct sd *sd = (struct sd *) gspca_dev;
  595. struct cam *cam;
  596. cam = &gspca_dev->cam;
  597. cam->epaddr = 1;
  598. sd->sensor = id->driver_info;
  599. if (sd->sensor == SENSOR_PAS106) {
  600. cam->cam_mode = sif_mode;
  601. cam->nmodes = sizeof sif_mode / sizeof sif_mode[0];
  602. } else {
  603. cam->cam_mode = vga_mode;
  604. cam->nmodes = sizeof vga_mode / sizeof vga_mode[0];
  605. gspca_dev->ctrl_dis = (1 << COLOR_IDX);
  606. }
  607. sd->brightness = BRIGHTNESS_DEF;
  608. sd->contrast = CONTRAST_DEF;
  609. sd->colors = COLOR_DEF;
  610. sd->autogain = AUTOGAIN_DEF;
  611. sd->ag_cnt = -1;
  612. return 0;
  613. }
  614. /* this function is called at probe and resume time */
  615. static int sd_init(struct gspca_dev *gspca_dev)
  616. {
  617. struct sd *sd = (struct sd *) gspca_dev;
  618. if (sd->sensor == SENSOR_PAS106)
  619. Et_init1(gspca_dev);
  620. else
  621. Et_init2(gspca_dev);
  622. reg_w_val(gspca_dev, ET_RESET_ALL, 0x08);
  623. et_video(gspca_dev, 0); /* video off */
  624. return 0;
  625. }
  626. /* -- start the camera -- */
  627. static int sd_start(struct gspca_dev *gspca_dev)
  628. {
  629. struct sd *sd = (struct sd *) gspca_dev;
  630. if (sd->sensor == SENSOR_PAS106)
  631. Et_init1(gspca_dev);
  632. else
  633. Et_init2(gspca_dev);
  634. setautogain(gspca_dev);
  635. reg_w_val(gspca_dev, ET_RESET_ALL, 0x08);
  636. et_video(gspca_dev, 1); /* video on */
  637. return 0;
  638. }
  639. static void sd_stopN(struct gspca_dev *gspca_dev)
  640. {
  641. et_video(gspca_dev, 0); /* video off */
  642. }
  643. static __u8 Et_getgainG(struct gspca_dev *gspca_dev)
  644. {
  645. struct sd *sd = (struct sd *) gspca_dev;
  646. if (sd->sensor == SENSOR_PAS106) {
  647. i2c_r(gspca_dev, PAS106_REG0e);
  648. PDEBUG(D_CONF, "Etoms gain G %d", gspca_dev->usb_buf[0]);
  649. return gspca_dev->usb_buf[0];
  650. }
  651. return 0x1f;
  652. }
  653. static void Et_setgainG(struct gspca_dev *gspca_dev, __u8 gain)
  654. {
  655. struct sd *sd = (struct sd *) gspca_dev;
  656. if (sd->sensor == SENSOR_PAS106) {
  657. __u8 i2cflags = 0x01;
  658. i2c_w(gspca_dev, PAS106_REG13, &i2cflags, 1, 3);
  659. i2c_w(gspca_dev, PAS106_REG0e, &gain, 1, 1);
  660. }
  661. }
  662. #define BLIMIT(bright) \
  663. (__u8)((bright > 0x1f)?0x1f:((bright < 4)?3:bright))
  664. #define LIMIT(color) \
  665. (unsigned char)((color > 0xff)?0xff:((color < 0)?0:color))
  666. static void do_autogain(struct gspca_dev *gspca_dev)
  667. {
  668. struct sd *sd = (struct sd *) gspca_dev;
  669. __u8 luma;
  670. __u8 luma_mean = 128;
  671. __u8 luma_delta = 20;
  672. __u8 spring = 4;
  673. int Gbright;
  674. __u8 r, g, b;
  675. if (sd->ag_cnt < 0)
  676. return;
  677. if (--sd->ag_cnt >= 0)
  678. return;
  679. sd->ag_cnt = AG_CNT_START;
  680. Gbright = Et_getgainG(gspca_dev);
  681. reg_r(gspca_dev, ET_LUMA_CENTER, 4);
  682. g = (gspca_dev->usb_buf[0] + gspca_dev->usb_buf[3]) >> 1;
  683. r = gspca_dev->usb_buf[1];
  684. b = gspca_dev->usb_buf[2];
  685. r = ((r << 8) - (r << 4) - (r << 3)) >> 10;
  686. b = ((b << 7) >> 10);
  687. g = ((g << 9) + (g << 7) + (g << 5)) >> 10;
  688. luma = LIMIT(r + g + b);
  689. PDEBUG(D_FRAM, "Etoms luma G %d", luma);
  690. if (luma < luma_mean - luma_delta || luma > luma_mean + luma_delta) {
  691. Gbright += (luma_mean - luma) >> spring;
  692. Gbright = BLIMIT(Gbright);
  693. PDEBUG(D_FRAM, "Etoms Gbright %d", Gbright);
  694. Et_setgainG(gspca_dev, (__u8) Gbright);
  695. }
  696. }
  697. #undef BLIMIT
  698. #undef LIMIT
  699. static void sd_pkt_scan(struct gspca_dev *gspca_dev,
  700. struct gspca_frame *frame, /* target */
  701. __u8 *data, /* isoc packet */
  702. int len) /* iso packet length */
  703. {
  704. int seqframe;
  705. seqframe = data[0] & 0x3f;
  706. len = (int) (((data[0] & 0xc0) << 2) | data[1]);
  707. if (seqframe == 0x3f) {
  708. PDEBUG(D_FRAM,
  709. "header packet found datalength %d !!", len);
  710. PDEBUG(D_FRAM, "G %d R %d G %d B %d",
  711. data[2], data[3], data[4], data[5]);
  712. data += 30;
  713. /* don't change datalength as the chips provided it */
  714. frame = gspca_frame_add(gspca_dev, LAST_PACKET, frame,
  715. data, 0);
  716. gspca_frame_add(gspca_dev, FIRST_PACKET, frame, data, len);
  717. return;
  718. }
  719. if (len) {
  720. data += 8;
  721. gspca_frame_add(gspca_dev, INTER_PACKET, frame, data, len);
  722. } else { /* Drop Packet */
  723. gspca_dev->last_packet_type = DISCARD_PACKET;
  724. }
  725. }
  726. static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
  727. {
  728. struct sd *sd = (struct sd *) gspca_dev;
  729. sd->brightness = val;
  730. if (gspca_dev->streaming)
  731. setbrightness(gspca_dev);
  732. return 0;
  733. }
  734. static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
  735. {
  736. struct sd *sd = (struct sd *) gspca_dev;
  737. getbrightness(gspca_dev);
  738. *val = sd->brightness;
  739. return 0;
  740. }
  741. static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
  742. {
  743. struct sd *sd = (struct sd *) gspca_dev;
  744. sd->contrast = val;
  745. if (gspca_dev->streaming)
  746. setcontrast(gspca_dev);
  747. return 0;
  748. }
  749. static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val)
  750. {
  751. struct sd *sd = (struct sd *) gspca_dev;
  752. getcontrast(gspca_dev);
  753. *val = sd->contrast;
  754. return 0;
  755. }
  756. static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val)
  757. {
  758. struct sd *sd = (struct sd *) gspca_dev;
  759. sd->colors = val;
  760. if (gspca_dev->streaming)
  761. setcolors(gspca_dev);
  762. return 0;
  763. }
  764. static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val)
  765. {
  766. struct sd *sd = (struct sd *) gspca_dev;
  767. getcolors(gspca_dev);
  768. *val = sd->colors;
  769. return 0;
  770. }
  771. static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val)
  772. {
  773. struct sd *sd = (struct sd *) gspca_dev;
  774. sd->autogain = val;
  775. if (gspca_dev->streaming)
  776. setautogain(gspca_dev);
  777. return 0;
  778. }
  779. static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val)
  780. {
  781. struct sd *sd = (struct sd *) gspca_dev;
  782. *val = sd->autogain;
  783. return 0;
  784. }
  785. /* sub-driver description */
  786. static struct sd_desc sd_desc = {
  787. .name = MODULE_NAME,
  788. .ctrls = sd_ctrls,
  789. .nctrls = ARRAY_SIZE(sd_ctrls),
  790. .config = sd_config,
  791. .init = sd_init,
  792. .start = sd_start,
  793. .stopN = sd_stopN,
  794. .pkt_scan = sd_pkt_scan,
  795. .dq_callback = do_autogain,
  796. };
  797. /* -- module initialisation -- */
  798. static __devinitdata struct usb_device_id device_table[] = {
  799. {USB_DEVICE(0x102c, 0x6151), .driver_info = SENSOR_PAS106},
  800. #if !defined CONFIG_USB_ET61X251 && !defined CONFIG_USB_ET61X251_MODULE
  801. {USB_DEVICE(0x102c, 0x6251), .driver_info = SENSOR_TAS5130CXX},
  802. #endif
  803. {}
  804. };
  805. MODULE_DEVICE_TABLE(usb, device_table);
  806. /* -- device connect -- */
  807. static int sd_probe(struct usb_interface *intf,
  808. const struct usb_device_id *id)
  809. {
  810. return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
  811. THIS_MODULE);
  812. }
  813. static struct usb_driver sd_driver = {
  814. .name = MODULE_NAME,
  815. .id_table = device_table,
  816. .probe = sd_probe,
  817. .disconnect = gspca_disconnect,
  818. #ifdef CONFIG_PM
  819. .suspend = gspca_suspend,
  820. .resume = gspca_resume,
  821. #endif
  822. };
  823. /* -- module insert / remove -- */
  824. static int __init sd_mod_init(void)
  825. {
  826. if (usb_register(&sd_driver) < 0)
  827. return -1;
  828. PDEBUG(D_PROBE, "registered");
  829. return 0;
  830. }
  831. static void __exit sd_mod_exit(void)
  832. {
  833. usb_deregister(&sd_driver);
  834. PDEBUG(D_PROBE, "deregistered");
  835. }
  836. module_init(sd_mod_init);
  837. module_exit(sd_mod_exit);