etoms.c 24 KB

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