etoms.c 22 KB

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