pac7311.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  1. /*
  2. * Pixart PAC7311 library
  3. * Copyright (C) 2005 Thomas Kaiser thomas@kaiser-linux.li
  4. *
  5. * V4L2 by Jean-Francois Moine <http://moinejf.free.fr>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #define MODULE_NAME "pac7311"
  22. #include "gspca.h"
  23. MODULE_AUTHOR("Thomas Kaiser thomas@kaiser-linux.li");
  24. MODULE_DESCRIPTION("Pixart PAC7311");
  25. MODULE_LICENSE("GPL");
  26. /* specific webcam descriptor */
  27. struct sd {
  28. struct gspca_dev gspca_dev; /* !! must be the first item */
  29. int lum_sum;
  30. atomic_t avg_lum;
  31. atomic_t do_gain;
  32. unsigned char brightness;
  33. unsigned char contrast;
  34. unsigned char colors;
  35. unsigned char autogain;
  36. char ffseq;
  37. signed char ag_cnt;
  38. #define AG_CNT_START 13
  39. };
  40. /* V4L2 controls supported by the driver */
  41. static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
  42. static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
  43. static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val);
  44. static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val);
  45. static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val);
  46. static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val);
  47. static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val);
  48. static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val);
  49. static struct ctrl sd_ctrls[] = {
  50. {
  51. {
  52. .id = V4L2_CID_BRIGHTNESS,
  53. .type = V4L2_CTRL_TYPE_INTEGER,
  54. .name = "Brightness",
  55. .minimum = 0,
  56. #define BRIGHTNESS_MAX 0x20
  57. .maximum = BRIGHTNESS_MAX,
  58. .step = 1,
  59. #define BRIGHTNESS_DEF 0x10
  60. .default_value = BRIGHTNESS_DEF,
  61. },
  62. .set = sd_setbrightness,
  63. .get = sd_getbrightness,
  64. },
  65. {
  66. {
  67. .id = V4L2_CID_CONTRAST,
  68. .type = V4L2_CTRL_TYPE_INTEGER,
  69. .name = "Contrast",
  70. .minimum = 0,
  71. .maximum = 255,
  72. .step = 1,
  73. #define CONTRAST_DEF 127
  74. .default_value = CONTRAST_DEF,
  75. },
  76. .set = sd_setcontrast,
  77. .get = sd_getcontrast,
  78. },
  79. {
  80. {
  81. .id = V4L2_CID_SATURATION,
  82. .type = V4L2_CTRL_TYPE_INTEGER,
  83. .name = "Color",
  84. .minimum = 0,
  85. .maximum = 255,
  86. .step = 1,
  87. #define COLOR_DEF 127
  88. .default_value = COLOR_DEF,
  89. },
  90. .set = sd_setcolors,
  91. .get = sd_getcolors,
  92. },
  93. {
  94. {
  95. .id = V4L2_CID_AUTOGAIN,
  96. .type = V4L2_CTRL_TYPE_BOOLEAN,
  97. .name = "Auto Gain",
  98. .minimum = 0,
  99. .maximum = 1,
  100. .step = 1,
  101. #define AUTOGAIN_DEF 1
  102. .default_value = AUTOGAIN_DEF,
  103. },
  104. .set = sd_setautogain,
  105. .get = sd_getautogain,
  106. },
  107. };
  108. static struct v4l2_pix_format vga_mode[] = {
  109. {160, 120, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
  110. .bytesperline = 160,
  111. .sizeimage = 160 * 120 * 3 / 8 + 590,
  112. .colorspace = V4L2_COLORSPACE_JPEG,
  113. .priv = 2},
  114. {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
  115. .bytesperline = 320,
  116. .sizeimage = 320 * 240 * 3 / 8 + 590,
  117. .colorspace = V4L2_COLORSPACE_JPEG,
  118. .priv = 1},
  119. {640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
  120. .bytesperline = 640,
  121. .sizeimage = 640 * 480 * 3 / 8 + 590,
  122. .colorspace = V4L2_COLORSPACE_JPEG,
  123. .priv = 0},
  124. };
  125. #define PAC7311_JPEG_HEADER_SIZE (sizeof pac7311_jpeg_header) /* (594) */
  126. static const __u8 pac7311_jpeg_header[] = {
  127. 0xff, 0xd8,
  128. 0xff, 0xe0, 0x00, 0x03, 0x20,
  129. 0xff, 0xc0, 0x00, 0x11, 0x08,
  130. 0x01, 0xe0, /* 12: height */
  131. 0x02, 0x80, /* 14: width */
  132. 0x03, /* 16 */
  133. 0x01, 0x21, 0x00,
  134. 0x02, 0x11, 0x01,
  135. 0x03, 0x11, 0x01,
  136. 0xff, 0xdb, 0x00, 0x84,
  137. 0x00, 0x10, 0x0b, 0x0c, 0x0e, 0x0c, 0x0a, 0x10, 0x0e, 0x0d,
  138. 0x0e, 0x12, 0x11, 0x10, 0x13, 0x18, 0x28, 0x1a, 0x18, 0x16,
  139. 0x16, 0x18, 0x31, 0x23, 0x25, 0x1d, 0x28, 0x3a, 0x33, 0x3d,
  140. 0x3c, 0x39, 0x33, 0x38, 0x37, 0x40, 0x48, 0x5c, 0x4e, 0x40,
  141. 0x44, 0x57, 0x45, 0x37, 0x38, 0x50, 0x6d, 0x51, 0x57, 0x5f,
  142. 0x62, 0x67, 0x68, 0x67, 0x3e, 0x4d, 0x71, 0x79, 0x70, 0x64,
  143. 0x78, 0x5c, 0x65, 0x67, 0x63, 0x01, 0x11, 0x12, 0x12, 0x18,
  144. 0x15, 0x18, 0x2f, 0x1a, 0x1a, 0x2f, 0x63, 0x42, 0x38, 0x42,
  145. 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
  146. 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
  147. 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
  148. 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
  149. 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
  150. 0xff, 0xc4, 0x01, 0xa2, 0x00, 0x00, 0x01, 0x05, 0x01, 0x01,
  151. 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  152. 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  153. 0x09, 0x0a, 0x0b, 0x10, 0x00, 0x02, 0x01, 0x03, 0x03, 0x02,
  154. 0x04, 0x03, 0x05, 0x05, 0x04, 0x04, 0x00, 0x00, 0x01, 0x7d,
  155. 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12, 0x21, 0x31,
  156. 0x41, 0x06, 0x13, 0x51, 0x61, 0x07, 0x22, 0x71, 0x14, 0x32,
  157. 0x81, 0x91, 0xa1, 0x08, 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52,
  158. 0xd1, 0xf0, 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
  159. 0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a,
  160. 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x43, 0x44, 0x45,
  161. 0x46, 0x47, 0x48, 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57,
  162. 0x58, 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
  163. 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x83,
  164. 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94,
  165. 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
  166. 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
  167. 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  168. 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8,
  169. 0xd9, 0xda, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8,
  170. 0xe9, 0xea, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
  171. 0xf9, 0xfa, 0x01, 0x00, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01,
  172. 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  173. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
  174. 0x0b, 0x11, 0x00, 0x02, 0x01, 0x02, 0x04, 0x04, 0x03, 0x04,
  175. 0x07, 0x05, 0x04, 0x04, 0x00, 0x01, 0x02, 0x77, 0x00, 0x01,
  176. 0x02, 0x03, 0x11, 0x04, 0x05, 0x21, 0x31, 0x06, 0x12, 0x41,
  177. 0x51, 0x07, 0x61, 0x71, 0x13, 0x22, 0x32, 0x81, 0x08, 0x14,
  178. 0x42, 0x91, 0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,
  179. 0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34, 0xe1, 0x25,
  180. 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26, 0x27, 0x28, 0x29, 0x2a,
  181. 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46,
  182. 0x47, 0x48, 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
  183. 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a,
  184. 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x82, 0x83,
  185. 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94,
  186. 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
  187. 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
  188. 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  189. 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8,
  190. 0xd9, 0xda, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
  191. 0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa,
  192. 0xff, 0xda, 0x00, 0x0c, 0x03, 0x01, 0x00, 0x02, 0x11, 0x03,
  193. 0x11, 0x00, 0x3f, 0x00
  194. };
  195. static void reg_w_buf(struct gspca_dev *gspca_dev,
  196. __u16 index,
  197. const char *buffer, __u16 len)
  198. {
  199. memcpy(gspca_dev->usb_buf, buffer, len);
  200. usb_control_msg(gspca_dev->dev,
  201. usb_sndctrlpipe(gspca_dev->dev, 0),
  202. 1, /* request */
  203. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  204. 0, /* value */
  205. index, gspca_dev->usb_buf, len,
  206. 500);
  207. }
  208. static __u8 reg_r(struct gspca_dev *gspca_dev,
  209. __u16 index)
  210. {
  211. usb_control_msg(gspca_dev->dev,
  212. usb_rcvctrlpipe(gspca_dev->dev, 0),
  213. 0, /* request */
  214. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  215. 0, /* value */
  216. index, gspca_dev->usb_buf, 1,
  217. 500);
  218. return gspca_dev->usb_buf[0];
  219. }
  220. static void reg_w(struct gspca_dev *gspca_dev,
  221. __u16 index,
  222. __u8 value)
  223. {
  224. gspca_dev->usb_buf[0] = value;
  225. usb_control_msg(gspca_dev->dev,
  226. usb_sndctrlpipe(gspca_dev->dev, 0),
  227. 0, /* request */
  228. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  229. value, index, gspca_dev->usb_buf, 1,
  230. 500);
  231. }
  232. /* this function is called at probe time */
  233. static int sd_config(struct gspca_dev *gspca_dev,
  234. const struct usb_device_id *id)
  235. {
  236. struct sd *sd = (struct sd *) gspca_dev;
  237. struct cam *cam;
  238. PDEBUG(D_CONF, "Find Sensor PAC7311");
  239. reg_w(gspca_dev, 0x78, 0x40); /* Bit_0=start stream, Bit_7=LED */
  240. reg_w(gspca_dev, 0x78, 0x40); /* Bit_0=start stream, Bit_7=LED */
  241. reg_w(gspca_dev, 0x78, 0x44); /* Bit_0=start stream, Bit_7=LED */
  242. reg_w(gspca_dev, 0xff, 0x04);
  243. reg_w(gspca_dev, 0x27, 0x80);
  244. reg_w(gspca_dev, 0x28, 0xca);
  245. reg_w(gspca_dev, 0x29, 0x53);
  246. reg_w(gspca_dev, 0x2a, 0x0e);
  247. reg_w(gspca_dev, 0xff, 0x01);
  248. reg_w(gspca_dev, 0x3e, 0x20);
  249. cam = &gspca_dev->cam;
  250. cam->epaddr = 0x05;
  251. cam->cam_mode = vga_mode;
  252. cam->nmodes = ARRAY_SIZE(vga_mode);
  253. sd->brightness = BRIGHTNESS_DEF;
  254. sd->contrast = CONTRAST_DEF;
  255. sd->colors = COLOR_DEF;
  256. sd->autogain = AUTOGAIN_DEF;
  257. sd->ag_cnt = -1;
  258. return 0;
  259. }
  260. static void setbrightness(struct gspca_dev *gspca_dev)
  261. {
  262. struct sd *sd = (struct sd *) gspca_dev;
  263. int brightness;
  264. /*jfm: inverted?*/
  265. brightness = BRIGHTNESS_MAX - sd->brightness;
  266. reg_w(gspca_dev, 0xff, 0x04);
  267. /* reg_w(gspca_dev, 0x0e, 0x00); */
  268. reg_w(gspca_dev, 0x0f, brightness);
  269. /* load registers to sensor (Bit 0, auto clear) */
  270. reg_w(gspca_dev, 0x11, 0x01);
  271. PDEBUG(D_CONF|D_STREAM, "brightness: %i", brightness);
  272. }
  273. static void setcontrast(struct gspca_dev *gspca_dev)
  274. {
  275. struct sd *sd = (struct sd *) gspca_dev;
  276. reg_w(gspca_dev, 0xff, 0x01);
  277. reg_w(gspca_dev, 0x80, sd->contrast);
  278. /* load registers to sensor (Bit 0, auto clear) */
  279. reg_w(gspca_dev, 0x11, 0x01);
  280. PDEBUG(D_CONF|D_STREAM, "contrast: %i", sd->contrast);
  281. }
  282. static void setcolors(struct gspca_dev *gspca_dev)
  283. {
  284. struct sd *sd = (struct sd *) gspca_dev;
  285. reg_w(gspca_dev, 0xff, 0x01);
  286. reg_w(gspca_dev, 0x10, sd->colors);
  287. /* load registers to sensor (Bit 0, auto clear) */
  288. reg_w(gspca_dev, 0x11, 0x01);
  289. PDEBUG(D_CONF|D_STREAM, "color: %i", sd->colors);
  290. }
  291. static void setautogain(struct gspca_dev *gspca_dev)
  292. {
  293. struct sd *sd = (struct sd *) gspca_dev;
  294. if (sd->autogain) {
  295. sd->lum_sum = 0;
  296. sd->ag_cnt = AG_CNT_START;
  297. } else {
  298. sd->ag_cnt = -1;
  299. }
  300. }
  301. /* this function is called at open time */
  302. static int sd_open(struct gspca_dev *gspca_dev)
  303. {
  304. reg_w(gspca_dev, 0x78, 0x00); /* Turn on LED */
  305. return 0;
  306. }
  307. static void sd_start(struct gspca_dev *gspca_dev)
  308. {
  309. reg_w(gspca_dev, 0xff, 0x01);
  310. reg_w_buf(gspca_dev, 0x0002, "\x48\x0a\x40\x08\x00\x00\x08\x00", 8);
  311. reg_w_buf(gspca_dev, 0x000a, "\x06\xff\x11\xff\x5a\x30\x90\x4c", 8);
  312. reg_w_buf(gspca_dev, 0x0012, "\x00\x07\x00\x0a\x10\x00\xa0\x10", 8);
  313. reg_w_buf(gspca_dev, 0x001a, "\x02\x00\x00\x00\x00\x0b\x01\x00", 8);
  314. reg_w_buf(gspca_dev, 0x0022, "\x00\x00\x00\x00\x00\x00\x00\x00", 8);
  315. reg_w_buf(gspca_dev, 0x002a, "\x00\x00\x00", 3);
  316. reg_w_buf(gspca_dev, 0x003e, "\x00\x00\x78\x52\x4a\x52\x78\x6e", 8);
  317. reg_w_buf(gspca_dev, 0x0046, "\x48\x46\x48\x6e\x5f\x49\x42\x49", 8);
  318. reg_w_buf(gspca_dev, 0x004e, "\x5f\x5f\x49\x42\x49\x5f\x6e\x48", 8);
  319. reg_w_buf(gspca_dev, 0x0056, "\x46\x48\x6e\x78\x52\x4a\x52\x78", 8);
  320. reg_w_buf(gspca_dev, 0x005e, "\x00\x00\x09\x1b\x34\x49\x5c\x9b", 8);
  321. reg_w_buf(gspca_dev, 0x0066, "\xd0\xff", 2);
  322. reg_w_buf(gspca_dev, 0x0078, "\x44\x00\xf2\x01\x01\x80", 6);
  323. reg_w_buf(gspca_dev, 0x007f, "\x2a\x1c\x00\xc8\x02\x58\x03\x84", 8);
  324. reg_w_buf(gspca_dev, 0x0087, "\x12\x00\x1a\x04\x08\x0c\x10\x14", 8);
  325. reg_w_buf(gspca_dev, 0x008f, "\x18\x20", 2);
  326. reg_w_buf(gspca_dev, 0x0096, "\x01\x08\x04", 3);
  327. reg_w_buf(gspca_dev, 0x00a0, "\x44\x44\x44\x04", 4);
  328. reg_w_buf(gspca_dev, 0x00f0, "\x01\x00\x00\x00\x22\x00\x20\x00", 8);
  329. reg_w_buf(gspca_dev, 0x00f8, "\x3f\x00\x0a\x01\x00", 5);
  330. reg_w(gspca_dev, 0xff, 0x04);
  331. reg_w(gspca_dev, 0x02, 0x04);
  332. reg_w(gspca_dev, 0x03, 0x54);
  333. reg_w(gspca_dev, 0x04, 0x07);
  334. reg_w(gspca_dev, 0x05, 0x2b);
  335. reg_w(gspca_dev, 0x06, 0x09);
  336. reg_w(gspca_dev, 0x07, 0x0f);
  337. reg_w(gspca_dev, 0x08, 0x09);
  338. reg_w(gspca_dev, 0x09, 0x00);
  339. reg_w(gspca_dev, 0x0c, 0x07);
  340. reg_w(gspca_dev, 0x0d, 0x00);
  341. reg_w(gspca_dev, 0x0e, 0x00);
  342. reg_w(gspca_dev, 0x0f, 0x62);
  343. reg_w(gspca_dev, 0x10, 0x08);
  344. reg_w(gspca_dev, 0x12, 0x07);
  345. reg_w(gspca_dev, 0x13, 0x00);
  346. reg_w(gspca_dev, 0x14, 0x00);
  347. reg_w(gspca_dev, 0x15, 0x00);
  348. reg_w(gspca_dev, 0x16, 0x00);
  349. reg_w(gspca_dev, 0x17, 0x00);
  350. reg_w(gspca_dev, 0x18, 0x00);
  351. reg_w(gspca_dev, 0x19, 0x00);
  352. reg_w(gspca_dev, 0x1a, 0x00);
  353. reg_w(gspca_dev, 0x1b, 0x03);
  354. reg_w(gspca_dev, 0x1c, 0xa0);
  355. reg_w(gspca_dev, 0x1d, 0x01);
  356. reg_w(gspca_dev, 0x1e, 0xf4);
  357. reg_w(gspca_dev, 0x21, 0x00);
  358. reg_w(gspca_dev, 0x22, 0x08);
  359. reg_w(gspca_dev, 0x24, 0x03);
  360. reg_w(gspca_dev, 0x26, 0x00);
  361. reg_w(gspca_dev, 0x27, 0x01);
  362. reg_w(gspca_dev, 0x28, 0xca);
  363. reg_w(gspca_dev, 0x29, 0x10);
  364. reg_w(gspca_dev, 0x2a, 0x06);
  365. reg_w(gspca_dev, 0x2b, 0x78);
  366. reg_w(gspca_dev, 0x2c, 0x00);
  367. reg_w(gspca_dev, 0x2d, 0x00);
  368. reg_w(gspca_dev, 0x2e, 0x00);
  369. reg_w(gspca_dev, 0x2f, 0x00);
  370. reg_w(gspca_dev, 0x30, 0x23);
  371. reg_w(gspca_dev, 0x31, 0x28);
  372. reg_w(gspca_dev, 0x32, 0x04);
  373. reg_w(gspca_dev, 0x33, 0x11);
  374. reg_w(gspca_dev, 0x34, 0x00);
  375. reg_w(gspca_dev, 0x35, 0x00);
  376. reg_w(gspca_dev, 0x11, 0x01);
  377. setcontrast(gspca_dev);
  378. setbrightness(gspca_dev);
  379. setcolors(gspca_dev);
  380. setautogain(gspca_dev);
  381. /* set correct resolution */
  382. switch (gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv) {
  383. case 2: /* 160x120 */
  384. reg_w(gspca_dev, 0xff, 0x04);
  385. reg_w(gspca_dev, 0x02, 0x03);
  386. reg_w(gspca_dev, 0xff, 0x01);
  387. reg_w(gspca_dev, 0x08, 0x09);
  388. reg_w(gspca_dev, 0x17, 0x20);
  389. reg_w(gspca_dev, 0x1b, 0x00);
  390. /* reg_w(gspca_dev, 0x80, 0x69); */
  391. reg_w(gspca_dev, 0x87, 0x10);
  392. break;
  393. case 1: /* 320x240 */
  394. reg_w(gspca_dev, 0xff, 0x04);
  395. reg_w(gspca_dev, 0x02, 0x03);
  396. reg_w(gspca_dev, 0xff, 0x01);
  397. reg_w(gspca_dev, 0x08, 0x09);
  398. reg_w(gspca_dev, 0x17, 0x30);
  399. /* reg_w(gspca_dev, 0x80, 0x3f); */
  400. reg_w(gspca_dev, 0x87, 0x11);
  401. break;
  402. case 0: /* 640x480 */
  403. reg_w(gspca_dev, 0xff, 0x04);
  404. reg_w(gspca_dev, 0x02, 0x03);
  405. reg_w(gspca_dev, 0xff, 0x01);
  406. reg_w(gspca_dev, 0x08, 0x08);
  407. reg_w(gspca_dev, 0x17, 0x00);
  408. /* reg_w(gspca_dev, 0x80, 0x1c); */
  409. reg_w(gspca_dev, 0x87, 0x12);
  410. break;
  411. }
  412. /* start stream */
  413. reg_w(gspca_dev, 0xff, 0x01);
  414. reg_w(gspca_dev, 0x78, 0x04);
  415. reg_w(gspca_dev, 0x78, 0x05);
  416. }
  417. static void sd_stopN(struct gspca_dev *gspca_dev)
  418. {
  419. reg_w(gspca_dev, 0xff, 0x04);
  420. reg_w(gspca_dev, 0x27, 0x80);
  421. reg_w(gspca_dev, 0x28, 0xca);
  422. reg_w(gspca_dev, 0x29, 0x53);
  423. reg_w(gspca_dev, 0x2a, 0x0e);
  424. reg_w(gspca_dev, 0xff, 0x01);
  425. reg_w(gspca_dev, 0x3e, 0x20);
  426. reg_w(gspca_dev, 0x78, 0x04); /* Bit_0=start stream, Bit_7=LED */
  427. reg_w(gspca_dev, 0x78, 0x44); /* Bit_0=start stream, Bit_7=LED */
  428. reg_w(gspca_dev, 0x78, 0x44); /* Bit_0=start stream, Bit_7=LED */
  429. }
  430. static void sd_stop0(struct gspca_dev *gspca_dev)
  431. {
  432. }
  433. /* this function is called at close time */
  434. static void sd_close(struct gspca_dev *gspca_dev)
  435. {
  436. reg_w(gspca_dev, 0xff, 0x04);
  437. reg_w(gspca_dev, 0x27, 0x80);
  438. reg_w(gspca_dev, 0x28, 0xca);
  439. reg_w(gspca_dev, 0x29, 0x53);
  440. reg_w(gspca_dev, 0x2a, 0x0e);
  441. reg_w(gspca_dev, 0xff, 0x01);
  442. reg_w(gspca_dev, 0x3e, 0x20);
  443. reg_w(gspca_dev, 0x78, 0x04); /* Bit_0=start stream, Bit_7=LED */
  444. reg_w(gspca_dev, 0x78, 0x44); /* Bit_0=start stream, Bit_7=LED */
  445. reg_w(gspca_dev, 0x78, 0x44); /* Bit_0=start stream, Bit_7=LED */
  446. }
  447. static void do_autogain(struct gspca_dev *gspca_dev)
  448. {
  449. struct sd *sd = (struct sd *) gspca_dev;
  450. int luma;
  451. int luma_mean = 128;
  452. int luma_delta = 20;
  453. __u8 spring = 5;
  454. int Gbright;
  455. if (!atomic_read(&sd->do_gain))
  456. return;
  457. atomic_set(&sd->do_gain, 0);
  458. luma = atomic_read(&sd->avg_lum);
  459. Gbright = reg_r(gspca_dev, 0x02);
  460. PDEBUG(D_FRAM, "luma mean %d", luma);
  461. if (luma < luma_mean - luma_delta ||
  462. luma > luma_mean + luma_delta) {
  463. Gbright += (luma_mean - luma) >> spring;
  464. if (Gbright > 0x1a)
  465. Gbright = 0x1a;
  466. else if (Gbright < 4)
  467. Gbright = 4;
  468. PDEBUG(D_FRAM, "gbright %d", Gbright);
  469. reg_w(gspca_dev, 0xff, 0x04);
  470. reg_w(gspca_dev, 0x0f, Gbright);
  471. /* load registers to sensor (Bit 0, auto clear) */
  472. reg_w(gspca_dev, 0x11, 0x01);
  473. }
  474. }
  475. static void sd_pkt_scan(struct gspca_dev *gspca_dev,
  476. struct gspca_frame *frame, /* target */
  477. __u8 *data, /* isoc packet */
  478. int len) /* iso packet length */
  479. {
  480. struct sd *sd = (struct sd *) gspca_dev;
  481. unsigned char tmpbuf[4];
  482. int i, p, ffseq;
  483. /* if (len < 5) { */
  484. if (len < 6) {
  485. /* gspca_dev->last_packet_type = DISCARD_PACKET; */
  486. return;
  487. }
  488. ffseq = sd->ffseq;
  489. for (p = 0; p < len - 6; p++) {
  490. if ((data[0 + p] == 0xff)
  491. && (data[1 + p] == 0xff)
  492. && (data[2 + p] == 0x00)
  493. && (data[3 + p] == 0xff)
  494. && (data[4 + p] == 0x96)) {
  495. /* start of frame */
  496. if (sd->ag_cnt >= 0 && p > 28) {
  497. sd->lum_sum += data[p - 23];
  498. if (--sd->ag_cnt < 0) {
  499. sd->ag_cnt = AG_CNT_START;
  500. atomic_set(&sd->avg_lum,
  501. sd->lum_sum / AG_CNT_START);
  502. sd->lum_sum = 0;
  503. atomic_set(&sd->do_gain, 1);
  504. }
  505. }
  506. /* copy the end of data to the current frame */
  507. frame = gspca_frame_add(gspca_dev, LAST_PACKET, frame,
  508. data, p);
  509. /* put the JPEG header in the new frame */
  510. gspca_frame_add(gspca_dev, FIRST_PACKET, frame,
  511. (unsigned char *) pac7311_jpeg_header,
  512. 12);
  513. tmpbuf[0] = gspca_dev->height >> 8;
  514. tmpbuf[1] = gspca_dev->height & 0xff;
  515. tmpbuf[2] = gspca_dev->width >> 8;
  516. tmpbuf[3] = gspca_dev->width & 0xff;
  517. gspca_frame_add(gspca_dev, INTER_PACKET, frame,
  518. tmpbuf, 4);
  519. gspca_frame_add(gspca_dev, INTER_PACKET, frame,
  520. (unsigned char *) &pac7311_jpeg_header[16],
  521. PAC7311_JPEG_HEADER_SIZE - 16);
  522. data += p + 7;
  523. len -= p + 7;
  524. ffseq = 0;
  525. break;
  526. }
  527. }
  528. /* remove the 'ff ff ff xx' sequences */
  529. switch (ffseq) {
  530. case 3:
  531. data += 1;
  532. len -= 1;
  533. break;
  534. case 2:
  535. if (data[0] == 0xff) {
  536. data += 2;
  537. len -= 2;
  538. frame->data_end -= 2;
  539. }
  540. break;
  541. case 1:
  542. if (data[0] == 0xff
  543. && data[1] == 0xff) {
  544. data += 3;
  545. len -= 3;
  546. frame->data_end -= 1;
  547. }
  548. break;
  549. }
  550. for (i = 0; i < len - 4; i++) {
  551. if (data[i] == 0xff
  552. && data[i + 1] == 0xff
  553. && data[i + 2] == 0xff) {
  554. memmove(&data[i], &data[i + 4], len - i - 4);
  555. len -= 4;
  556. }
  557. }
  558. ffseq = 0;
  559. if (data[len - 4] == 0xff) {
  560. if (data[len - 3] == 0xff
  561. && data[len - 2] == 0xff) {
  562. len -= 4;
  563. }
  564. } else if (data[len - 3] == 0xff) {
  565. if (data[len - 2] == 0xff
  566. && data[len - 1] == 0xff)
  567. ffseq = 3;
  568. } else if (data[len - 2] == 0xff) {
  569. if (data[len - 1] == 0xff)
  570. ffseq = 2;
  571. } else if (data[len - 1] == 0xff)
  572. ffseq = 1;
  573. sd->ffseq = ffseq;
  574. gspca_frame_add(gspca_dev, INTER_PACKET, frame, data, len);
  575. }
  576. static void getbrightness(struct gspca_dev *gspca_dev)
  577. {
  578. /* sd->brightness = reg_r(gspca_dev, 0x08);
  579. return sd->brightness; */
  580. /* PDEBUG(D_CONF, "Called pac7311_getbrightness: Not implemented yet"); */
  581. }
  582. static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
  583. {
  584. struct sd *sd = (struct sd *) gspca_dev;
  585. sd->brightness = val;
  586. if (gspca_dev->streaming)
  587. setbrightness(gspca_dev);
  588. return 0;
  589. }
  590. static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
  591. {
  592. struct sd *sd = (struct sd *) gspca_dev;
  593. getbrightness(gspca_dev);
  594. *val = sd->brightness;
  595. return 0;
  596. }
  597. static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
  598. {
  599. struct sd *sd = (struct sd *) gspca_dev;
  600. sd->contrast = val;
  601. if (gspca_dev->streaming)
  602. setcontrast(gspca_dev);
  603. return 0;
  604. }
  605. static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val)
  606. {
  607. struct sd *sd = (struct sd *) gspca_dev;
  608. /* getcontrast(gspca_dev); */
  609. *val = sd->contrast;
  610. return 0;
  611. }
  612. static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val)
  613. {
  614. struct sd *sd = (struct sd *) gspca_dev;
  615. sd->colors = val;
  616. if (gspca_dev->streaming)
  617. setcolors(gspca_dev);
  618. return 0;
  619. }
  620. static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val)
  621. {
  622. struct sd *sd = (struct sd *) gspca_dev;
  623. /* getcolors(gspca_dev); */
  624. *val = sd->colors;
  625. return 0;
  626. }
  627. static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val)
  628. {
  629. struct sd *sd = (struct sd *) gspca_dev;
  630. sd->autogain = val;
  631. if (gspca_dev->streaming)
  632. setautogain(gspca_dev);
  633. return 0;
  634. }
  635. static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val)
  636. {
  637. struct sd *sd = (struct sd *) gspca_dev;
  638. *val = sd->autogain;
  639. return 0;
  640. }
  641. /* sub-driver description */
  642. static struct sd_desc sd_desc = {
  643. .name = MODULE_NAME,
  644. .ctrls = sd_ctrls,
  645. .nctrls = ARRAY_SIZE(sd_ctrls),
  646. .config = sd_config,
  647. .open = sd_open,
  648. .start = sd_start,
  649. .stopN = sd_stopN,
  650. .stop0 = sd_stop0,
  651. .close = sd_close,
  652. .pkt_scan = sd_pkt_scan,
  653. .dq_callback = do_autogain,
  654. };
  655. /* -- module initialisation -- */
  656. static __devinitdata struct usb_device_id device_table[] = {
  657. {USB_DEVICE(0x093a, 0x2600)},
  658. {USB_DEVICE(0x093a, 0x2601)},
  659. {USB_DEVICE(0x093a, 0x2603)},
  660. {USB_DEVICE(0x093a, 0x2608)},
  661. {USB_DEVICE(0x093a, 0x260e)},
  662. {USB_DEVICE(0x093a, 0x260f)},
  663. {USB_DEVICE(0x093a, 0x2621)},
  664. {}
  665. };
  666. MODULE_DEVICE_TABLE(usb, device_table);
  667. /* -- device connect -- */
  668. static int sd_probe(struct usb_interface *intf,
  669. const struct usb_device_id *id)
  670. {
  671. return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
  672. THIS_MODULE);
  673. }
  674. static struct usb_driver sd_driver = {
  675. .name = MODULE_NAME,
  676. .id_table = device_table,
  677. .probe = sd_probe,
  678. .disconnect = gspca_disconnect,
  679. };
  680. /* -- module insert / remove -- */
  681. static int __init sd_mod_init(void)
  682. {
  683. if (usb_register(&sd_driver) < 0)
  684. return -1;
  685. PDEBUG(D_PROBE, "registered");
  686. return 0;
  687. }
  688. static void __exit sd_mod_exit(void)
  689. {
  690. usb_deregister(&sd_driver);
  691. PDEBUG(D_PROBE, "deregistered");
  692. }
  693. module_init(sd_mod_init);
  694. module_exit(sd_mod_exit);