pac7311.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  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. #include "jpeg.h"
  24. MODULE_AUTHOR("Thomas Kaiser thomas@kaiser-linux.li");
  25. MODULE_DESCRIPTION("Pixart PAC7311");
  26. MODULE_LICENSE("GPL");
  27. /* specific webcam descriptor */
  28. struct sd {
  29. struct gspca_dev gspca_dev; /* !! must be the first item */
  30. int lum_sum;
  31. atomic_t avg_lum;
  32. atomic_t do_gain;
  33. unsigned char brightness;
  34. unsigned char contrast;
  35. unsigned char colors;
  36. unsigned char autogain;
  37. char tosof; /* number of bytes before next start of frame */
  38. signed char ag_cnt;
  39. #define AG_CNT_START 13
  40. __u8 sensor;
  41. #define SENSOR_PAC7302 0
  42. #define SENSOR_PAC7311 1
  43. };
  44. /* V4L2 controls supported by the driver */
  45. static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
  46. static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
  47. static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val);
  48. static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val);
  49. static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val);
  50. static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val);
  51. static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val);
  52. static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val);
  53. static struct ctrl sd_ctrls[] = {
  54. {
  55. {
  56. .id = V4L2_CID_BRIGHTNESS,
  57. .type = V4L2_CTRL_TYPE_INTEGER,
  58. .name = "Brightness",
  59. .minimum = 0,
  60. #define BRIGHTNESS_MAX 0x20
  61. .maximum = BRIGHTNESS_MAX,
  62. .step = 1,
  63. #define BRIGHTNESS_DEF 0x10
  64. .default_value = BRIGHTNESS_DEF,
  65. },
  66. .set = sd_setbrightness,
  67. .get = sd_getbrightness,
  68. },
  69. {
  70. {
  71. .id = V4L2_CID_CONTRAST,
  72. .type = V4L2_CTRL_TYPE_INTEGER,
  73. .name = "Contrast",
  74. .minimum = 0,
  75. .maximum = 255,
  76. .step = 1,
  77. #define CONTRAST_DEF 127
  78. .default_value = CONTRAST_DEF,
  79. },
  80. .set = sd_setcontrast,
  81. .get = sd_getcontrast,
  82. },
  83. {
  84. {
  85. .id = V4L2_CID_SATURATION,
  86. .type = V4L2_CTRL_TYPE_INTEGER,
  87. .name = "Color",
  88. .minimum = 0,
  89. .maximum = 255,
  90. .step = 1,
  91. #define COLOR_DEF 127
  92. .default_value = COLOR_DEF,
  93. },
  94. .set = sd_setcolors,
  95. .get = sd_getcolors,
  96. },
  97. {
  98. {
  99. .id = V4L2_CID_AUTOGAIN,
  100. .type = V4L2_CTRL_TYPE_BOOLEAN,
  101. .name = "Auto Gain",
  102. .minimum = 0,
  103. .maximum = 1,
  104. .step = 1,
  105. #define AUTOGAIN_DEF 1
  106. .default_value = AUTOGAIN_DEF,
  107. },
  108. .set = sd_setautogain,
  109. .get = sd_getautogain,
  110. },
  111. };
  112. static struct v4l2_pix_format vga_mode[] = {
  113. {160, 120, V4L2_PIX_FMT_PJPG, V4L2_FIELD_NONE,
  114. .bytesperline = 160,
  115. .sizeimage = 160 * 120 * 3 / 8 + 590,
  116. .colorspace = V4L2_COLORSPACE_JPEG,
  117. .priv = 2},
  118. {320, 240, V4L2_PIX_FMT_PJPG, V4L2_FIELD_NONE,
  119. .bytesperline = 320,
  120. .sizeimage = 320 * 240 * 3 / 8 + 590,
  121. .colorspace = V4L2_COLORSPACE_JPEG,
  122. .priv = 1},
  123. {640, 480, V4L2_PIX_FMT_PJPG, V4L2_FIELD_NONE,
  124. .bytesperline = 640,
  125. .sizeimage = 640 * 480 * 3 / 8 + 590,
  126. .colorspace = V4L2_COLORSPACE_JPEG,
  127. .priv = 0},
  128. };
  129. /* pac 7302 */
  130. static const __u8 probe_7302[] = {
  131. /* index,value */
  132. 0xff, 0x01, /* page 1 */
  133. 0x78, 0x00, /* deactivate */
  134. 0xff, 0x01,
  135. 0x78, 0x40, /* led off */
  136. };
  137. static const __u8 start_7302[] = {
  138. /* index, len, [value]* */
  139. 0xff, 1, 0x00, /* page 0 */
  140. 0x00, 12, 0x01, 0x40, 0x40, 0x40, 0x01, 0xe0, 0x02, 0x80,
  141. 0x00, 0x00, 0x00, 0x00,
  142. 0x0d, 24, 0x03, 0x01, 0x00, 0xb5, 0x07, 0xcb, 0x00, 0x00,
  143. 0x07, 0xc8, 0x00, 0xea, 0x07, 0xcf, 0x07, 0xf7,
  144. 0x07, 0x7e, 0x01, 0x0b, 0x00, 0x00, 0x00, 0x11,
  145. 0x26, 2, 0xaa, 0xaa,
  146. 0x2e, 1, 0x31,
  147. 0x38, 1, 0x01,
  148. 0x3a, 3, 0x14, 0xff, 0x5a,
  149. 0x43, 11, 0x00, 0x0a, 0x18, 0x11, 0x01, 0x2c, 0x88, 0x11,
  150. 0x00, 0x54, 0x11,
  151. 0x55, 1, 0x00,
  152. 0x62, 4, 0x10, 0x1e, 0x1e, 0x18,
  153. 0x6b, 1, 0x00,
  154. 0x6e, 3, 0x08, 0x06, 0x00,
  155. 0x72, 3, 0x00, 0xff, 0x00,
  156. 0x7d, 23, 0x01, 0x01, 0x58, 0x46, 0x50, 0x3c, 0x50, 0x3c,
  157. 0x54, 0x46, 0x54, 0x56, 0x52, 0x50, 0x52, 0x50,
  158. 0x56, 0x64, 0xa4, 0x00, 0xda, 0x00, 0x00,
  159. 0xa2, 10, 0x22, 0x2c, 0x3c, 0x54, 0x69, 0x7c, 0x9c, 0xb9,
  160. 0xd2, 0xeb,
  161. 0xaf, 1, 0x02,
  162. 0xb5, 2, 0x08, 0x08,
  163. 0xb8, 2, 0x08, 0x88,
  164. 0xc4, 4, 0xae, 0x01, 0x04, 0x01,
  165. 0xcc, 1, 0x00,
  166. 0xd1, 11, 0x01, 0x30, 0x49, 0x5e, 0x6f, 0x7f, 0x8e, 0xa9,
  167. 0xc1, 0xd7, 0xec,
  168. 0xdc, 1, 0x01,
  169. 0xff, 1, 0x01, /* page 1 */
  170. 0x12, 3, 0x02, 0x00, 0x01,
  171. 0x3e, 2, 0x00, 0x00,
  172. 0x76, 5, 0x01, 0x20, 0x40, 0x00, 0xf2,
  173. 0x7c, 1, 0x00,
  174. 0x7f, 10, 0x4b, 0x0f, 0x01, 0x2c, 0x02, 0x58, 0x03, 0x20,
  175. 0x02, 0x00,
  176. 0x96, 5, 0x01, 0x10, 0x04, 0x01, 0x04,
  177. 0xc8, 14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00,
  178. 0x07, 0x00, 0x01, 0x07, 0x04, 0x01,
  179. 0xd8, 1, 0x01,
  180. 0xdb, 2, 0x00, 0x01,
  181. 0xde, 7, 0x00, 0x01, 0x04, 0x04, 0x00, 0x00, 0x00,
  182. 0xe6, 4, 0x00, 0x00, 0x00, 0x01,
  183. 0xeb, 1, 0x00,
  184. 0xff, 1, 0x02, /* page 2 */
  185. 0x22, 1, 0x00,
  186. 0xff, 1, 0x03, /* page 3 */
  187. 0x00, 255, /* load the page 3 */
  188. 0x11, 1, 0x01,
  189. 0xff, 1, 0x02, /* page 2 */
  190. 0x13, 1, 0x00,
  191. 0x22, 4, 0x1f, 0xa4, 0xf0, 0x96,
  192. 0x27, 2, 0x14, 0x0c,
  193. 0x2a, 5, 0xc8, 0x00, 0x18, 0x12, 0x22,
  194. 0x64, 8, 0x00, 0x00, 0xf0, 0x01, 0x14, 0x44, 0x44, 0x44,
  195. 0x6e, 1, 0x08,
  196. 0xff, 1, 0x03, /* page 1 */
  197. 0x78, 1, 0x00,
  198. 0, 0 /* end of sequence */
  199. };
  200. /* page 3 - the value 0xaa says skip the index - see reg_w_page() */
  201. static const __u8 page3_7302[] = {
  202. 0x90, 0x40, 0x03, 0x50, 0xc2, 0x01, 0x14, 0x16,
  203. 0x14, 0x12, 0x00, 0x00, 0x00, 0x02, 0x33, 0x00,
  204. 0x0f, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  205. 0x00, 0x00, 0x00, 0x47, 0x01, 0xb3, 0x01, 0x00,
  206. 0x00, 0x08, 0x00, 0x00, 0x0d, 0x00, 0x00, 0x21,
  207. 0x00, 0x00, 0x00, 0x54, 0xf4, 0x02, 0x52, 0x54,
  208. 0xa4, 0xb8, 0xe0, 0x2a, 0xf6, 0x00, 0x00, 0x00,
  209. 0x00, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  210. 0x00, 0xfc, 0x00, 0xf2, 0x1f, 0x04, 0x00, 0x00,
  211. 0x00, 0x00, 0x00, 0xc0, 0xc0, 0x10, 0x00, 0x00,
  212. 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  213. 0x00, 0x40, 0xff, 0x03, 0x19, 0x00, 0x00, 0x00,
  214. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  215. 0x00, 0x00, 0x00, 0x00, 0x00, 0xc8, 0xc8, 0xc8,
  216. 0xc8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50,
  217. 0x08, 0x10, 0x24, 0x40, 0x00, 0x00, 0x00, 0x00,
  218. 0x01, 0x00, 0x02, 0x47, 0x00, 0x00, 0x00, 0x00,
  219. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  220. 0x00, 0x02, 0xfa, 0x00, 0x64, 0x5a, 0x28, 0x00,
  221. 0x00
  222. };
  223. /* pac 7311 */
  224. static const __u8 probe_7311[] = {
  225. 0x78, 0x40, /* Bit_0=start stream, Bit_7=LED */
  226. 0x78, 0x40, /* Bit_0=start stream, Bit_7=LED */
  227. 0x78, 0x44, /* Bit_0=start stream, Bit_7=LED */
  228. 0xff, 0x04,
  229. 0x27, 0x80,
  230. 0x28, 0xca,
  231. 0x29, 0x53,
  232. 0x2a, 0x0e,
  233. 0xff, 0x01,
  234. 0x3e, 0x20,
  235. };
  236. static const __u8 start_7311[] = {
  237. /* index, len, [value]* */
  238. 0xff, 1, 0x01, /* page 1 */
  239. 0x02, 43, 0x48, 0x0a, 0x40, 0x08, 0x00, 0x00, 0x08, 0x00,
  240. 0x06, 0xff, 0x11, 0xff, 0x5a, 0x30, 0x90, 0x4c,
  241. 0x00, 0x07, 0x00, 0x0a, 0x10, 0x00, 0xa0, 0x10,
  242. 0x02, 0x00, 0x00, 0x00, 0x00, 0x0b, 0x01, 0x00,
  243. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  244. 0x00, 0x00, 0x00,
  245. 0x3e, 42, 0x00, 0x00, 0x78, 0x52, 0x4a, 0x52, 0x78, 0x6e,
  246. 0x48, 0x46, 0x48, 0x6e, 0x5f, 0x49, 0x42, 0x49,
  247. 0x5f, 0x5f, 0x49, 0x42, 0x49, 0x5f, 0x6e, 0x48,
  248. 0x46, 0x48, 0x6e, 0x78, 0x52, 0x4a, 0x52, 0x78,
  249. 0x00, 0x00, 0x09, 0x1b, 0x34, 0x49, 0x5c, 0x9b,
  250. 0xd0, 0xff,
  251. 0x78, 6, 0x44, 0x00, 0xf2, 0x01, 0x01, 0x80,
  252. 0x7f, 18, 0x2a, 0x1c, 0x00, 0xc8, 0x02, 0x58, 0x03, 0x84,
  253. 0x12, 0x00, 0x1a, 0x04, 0x08, 0x0c, 0x10, 0x14,
  254. 0x18, 0x20,
  255. 0x96, 3, 0x01, 0x08, 0x04,
  256. 0xa0, 4, 0x44, 0x44, 0x44, 0x04,
  257. 0xf0, 13, 0x01, 0x00, 0x00, 0x00, 0x22, 0x00, 0x20, 0x00,
  258. 0x3f, 0x00, 0x0a, 0x01, 0x00,
  259. 0xff, 1, 0x04, /* page 4 */
  260. 0x00, 254, /* load the page 4 */
  261. 0x11, 1, 0x01,
  262. 0, 0 /* end of sequence */
  263. };
  264. /* page 4 - the value 0xaa says skip the index - see reg_w_page() */
  265. static const __u8 page4_7311[] = {
  266. 0xaa, 0xaa, 0x04, 0x54, 0x07, 0x2b, 0x09, 0x0f,
  267. 0x09, 0x00, 0xaa, 0xaa, 0x07, 0x00, 0x00, 0x62,
  268. 0x08, 0xaa, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00,
  269. 0x00, 0x00, 0x00, 0x03, 0xa0, 0x01, 0xf4, 0xaa,
  270. 0xaa, 0x00, 0x08, 0xaa, 0x03, 0xaa, 0x00, 0x01,
  271. 0xca, 0x10, 0x06, 0x78, 0x00, 0x00, 0x00, 0x00,
  272. 0x23, 0x28, 0x04, 0x11, 0x00, 0x00
  273. };
  274. static void reg_w_buf(struct gspca_dev *gspca_dev,
  275. __u8 index,
  276. const char *buffer, int len)
  277. {
  278. memcpy(gspca_dev->usb_buf, buffer, len);
  279. usb_control_msg(gspca_dev->dev,
  280. usb_sndctrlpipe(gspca_dev->dev, 0),
  281. 1, /* request */
  282. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  283. 0, /* value */
  284. index, gspca_dev->usb_buf, len,
  285. 500);
  286. }
  287. static __u8 reg_r(struct gspca_dev *gspca_dev,
  288. __u8 index)
  289. {
  290. usb_control_msg(gspca_dev->dev,
  291. usb_rcvctrlpipe(gspca_dev->dev, 0),
  292. 0, /* request */
  293. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  294. 0, /* value */
  295. index, gspca_dev->usb_buf, 1,
  296. 500);
  297. return gspca_dev->usb_buf[0];
  298. }
  299. static void reg_w(struct gspca_dev *gspca_dev,
  300. __u8 index,
  301. __u8 value)
  302. {
  303. gspca_dev->usb_buf[0] = value;
  304. usb_control_msg(gspca_dev->dev,
  305. usb_sndctrlpipe(gspca_dev->dev, 0),
  306. 0, /* request */
  307. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  308. value, index, gspca_dev->usb_buf, 1,
  309. 500);
  310. }
  311. static void reg_w_seq(struct gspca_dev *gspca_dev,
  312. const __u8 *seq, int len)
  313. {
  314. while (--len >= 0) {
  315. reg_w(gspca_dev, seq[0], seq[1]);
  316. seq += 2;
  317. }
  318. }
  319. /* load the beginning of a page */
  320. static void reg_w_page(struct gspca_dev *gspca_dev,
  321. const __u8 *page, int len)
  322. {
  323. int index;
  324. for (index = 0; index < len; index++) {
  325. if (page[index] == 0xaa) /* skip this index */
  326. continue;
  327. gspca_dev->usb_buf[0] = page[index];
  328. usb_control_msg(gspca_dev->dev,
  329. usb_sndctrlpipe(gspca_dev->dev, 0),
  330. 0, /* request */
  331. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  332. 0, index, gspca_dev->usb_buf, 1,
  333. 500);
  334. }
  335. }
  336. /* output a variable sequence */
  337. static void reg_w_var(struct gspca_dev *gspca_dev,
  338. const __u8 *seq)
  339. {
  340. int index, len;
  341. for (;;) {
  342. index = *seq++;
  343. len = *seq++;
  344. switch (len) {
  345. case 0:
  346. return;
  347. case 254:
  348. reg_w_page(gspca_dev, page4_7311, sizeof page4_7311);
  349. break;
  350. case 255:
  351. reg_w_page(gspca_dev, page3_7302, sizeof page3_7302);
  352. break;
  353. default:
  354. if (len > 32) {
  355. PDEBUG(D_ERR|D_STREAM,
  356. "Incorrect variable sequence");
  357. return;
  358. }
  359. while (len > 0) {
  360. if (len < 8) {
  361. reg_w_buf(gspca_dev, index, seq, len);
  362. seq += len;
  363. break;
  364. }
  365. reg_w_buf(gspca_dev, index, seq, 8);
  366. seq += 8;
  367. index += 8;
  368. len -= 8;
  369. }
  370. }
  371. }
  372. /* not reached */
  373. }
  374. /* this function is called at probe time */
  375. static int sd_config(struct gspca_dev *gspca_dev,
  376. const struct usb_device_id *id)
  377. {
  378. struct sd *sd = (struct sd *) gspca_dev;
  379. struct cam *cam;
  380. cam = &gspca_dev->cam;
  381. cam->epaddr = 0x05;
  382. sd->sensor = id->driver_info;
  383. if (sd->sensor == SENSOR_PAC7302) {
  384. PDEBUG(D_CONF, "Find Sensor PAC7302");
  385. reg_w_seq(gspca_dev, probe_7302, sizeof probe_7302);
  386. cam->cam_mode = &vga_mode[2]; /* only 640x480 */
  387. cam->nmodes = 1;
  388. } else {
  389. PDEBUG(D_CONF, "Find Sensor PAC7311");
  390. reg_w_seq(gspca_dev, probe_7302, sizeof probe_7302);
  391. cam->cam_mode = vga_mode;
  392. cam->nmodes = ARRAY_SIZE(vga_mode);
  393. }
  394. sd->brightness = BRIGHTNESS_DEF;
  395. sd->contrast = CONTRAST_DEF;
  396. sd->colors = COLOR_DEF;
  397. sd->autogain = AUTOGAIN_DEF;
  398. sd->ag_cnt = -1;
  399. return 0;
  400. }
  401. static void setbrightness(struct gspca_dev *gspca_dev)
  402. {
  403. struct sd *sd = (struct sd *) gspca_dev;
  404. int brightness;
  405. if (sd->sensor == SENSOR_PAC7302)
  406. return;
  407. /*jfm: inverted?*/
  408. brightness = BRIGHTNESS_MAX - sd->brightness;
  409. reg_w(gspca_dev, 0xff, 0x04);
  410. reg_w(gspca_dev, 0x0f, brightness);
  411. /* load registers to sensor (Bit 0, auto clear) */
  412. reg_w(gspca_dev, 0x11, 0x01);
  413. PDEBUG(D_CONF|D_STREAM, "brightness: %i", brightness);
  414. }
  415. static void setcontrast(struct gspca_dev *gspca_dev)
  416. {
  417. struct sd *sd = (struct sd *) gspca_dev;
  418. if (sd->sensor == SENSOR_PAC7302)
  419. return;
  420. reg_w(gspca_dev, 0xff, 0x01);
  421. reg_w(gspca_dev, 0x80, sd->contrast);
  422. /* load registers to sensor (Bit 0, auto clear) */
  423. reg_w(gspca_dev, 0x11, 0x01);
  424. PDEBUG(D_CONF|D_STREAM, "contrast: %i", sd->contrast);
  425. }
  426. static void setcolors(struct gspca_dev *gspca_dev)
  427. {
  428. struct sd *sd = (struct sd *) gspca_dev;
  429. if (sd->sensor == SENSOR_PAC7302)
  430. return;
  431. reg_w(gspca_dev, 0xff, 0x01);
  432. reg_w(gspca_dev, 0x10, sd->colors);
  433. /* load registers to sensor (Bit 0, auto clear) */
  434. reg_w(gspca_dev, 0x11, 0x01);
  435. PDEBUG(D_CONF|D_STREAM, "color: %i", sd->colors);
  436. }
  437. static void setautogain(struct gspca_dev *gspca_dev)
  438. {
  439. struct sd *sd = (struct sd *) gspca_dev;
  440. if (sd->autogain) {
  441. sd->lum_sum = 0;
  442. sd->ag_cnt = AG_CNT_START;
  443. } else {
  444. sd->ag_cnt = -1;
  445. }
  446. }
  447. /* this function is called at open time */
  448. static int sd_open(struct gspca_dev *gspca_dev)
  449. {
  450. reg_w(gspca_dev, 0x78, 0x00); /* Turn on LED */
  451. return 0;
  452. }
  453. static void sd_start(struct gspca_dev *gspca_dev)
  454. {
  455. struct sd *sd = (struct sd *) gspca_dev;
  456. sd->tosof = 0;
  457. if (sd->sensor == SENSOR_PAC7302)
  458. reg_w_var(gspca_dev, start_7302);
  459. else
  460. reg_w_var(gspca_dev, start_7311);
  461. setcontrast(gspca_dev);
  462. setbrightness(gspca_dev);
  463. setcolors(gspca_dev);
  464. setautogain(gspca_dev);
  465. /* set correct resolution */
  466. switch (gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv) {
  467. case 2: /* 160x120 pac7311 */
  468. reg_w(gspca_dev, 0xff, 0x04);
  469. reg_w(gspca_dev, 0x02, 0x03);
  470. reg_w(gspca_dev, 0xff, 0x01);
  471. reg_w(gspca_dev, 0x08, 0x09);
  472. reg_w(gspca_dev, 0x17, 0x20);
  473. reg_w(gspca_dev, 0x1b, 0x00);
  474. /* reg_w(gspca_dev, 0x80, 0x69); */
  475. reg_w(gspca_dev, 0x87, 0x10);
  476. break;
  477. case 1: /* 320x240 pac7311 */
  478. reg_w(gspca_dev, 0xff, 0x04);
  479. reg_w(gspca_dev, 0x02, 0x03);
  480. reg_w(gspca_dev, 0xff, 0x01);
  481. reg_w(gspca_dev, 0x08, 0x09);
  482. reg_w(gspca_dev, 0x17, 0x30);
  483. /* reg_w(gspca_dev, 0x80, 0x3f); */
  484. reg_w(gspca_dev, 0x87, 0x11);
  485. break;
  486. case 0: /* 640x480 */
  487. if (sd->sensor == SENSOR_PAC7302)
  488. break;
  489. reg_w(gspca_dev, 0xff, 0x04);
  490. reg_w(gspca_dev, 0x02, 0x03);
  491. reg_w(gspca_dev, 0xff, 0x01);
  492. reg_w(gspca_dev, 0x08, 0x08);
  493. reg_w(gspca_dev, 0x17, 0x00);
  494. /* reg_w(gspca_dev, 0x80, 0x1c); */
  495. reg_w(gspca_dev, 0x87, 0x12);
  496. break;
  497. }
  498. /* start stream */
  499. reg_w(gspca_dev, 0xff, 0x01);
  500. if (sd->sensor == SENSOR_PAC7302) {
  501. reg_w(gspca_dev, 0x78, 0x01);
  502. reg_w(gspca_dev, 0xff, 0x01);
  503. reg_w(gspca_dev, 0x78, 0x01);
  504. } else {
  505. reg_w(gspca_dev, 0x78, 0x04);
  506. reg_w(gspca_dev, 0x78, 0x05);
  507. }
  508. }
  509. static void sd_stopN(struct gspca_dev *gspca_dev)
  510. {
  511. struct sd *sd = (struct sd *) gspca_dev;
  512. if (sd->sensor == SENSOR_PAC7302) {
  513. reg_w(gspca_dev, 0x78, 0x00);
  514. reg_w(gspca_dev, 0x78, 0x00);
  515. return;
  516. }
  517. reg_w(gspca_dev, 0xff, 0x04);
  518. reg_w(gspca_dev, 0x27, 0x80);
  519. reg_w(gspca_dev, 0x28, 0xca);
  520. reg_w(gspca_dev, 0x29, 0x53);
  521. reg_w(gspca_dev, 0x2a, 0x0e);
  522. reg_w(gspca_dev, 0xff, 0x01);
  523. reg_w(gspca_dev, 0x3e, 0x20);
  524. reg_w(gspca_dev, 0x78, 0x04); /* Bit_0=start stream, Bit_7=LED */
  525. reg_w(gspca_dev, 0x78, 0x44); /* Bit_0=start stream, Bit_7=LED */
  526. reg_w(gspca_dev, 0x78, 0x44); /* Bit_0=start stream, Bit_7=LED */
  527. }
  528. static void sd_stop0(struct gspca_dev *gspca_dev)
  529. {
  530. struct sd *sd = (struct sd *) gspca_dev;
  531. if (sd->sensor == SENSOR_PAC7302) {
  532. reg_w(gspca_dev, 0xff, 0x01);
  533. reg_w(gspca_dev, 0x78, 0x40);
  534. }
  535. }
  536. /* this function is called at close time */
  537. static void sd_close(struct gspca_dev *gspca_dev)
  538. {
  539. }
  540. static void do_autogain(struct gspca_dev *gspca_dev)
  541. {
  542. struct sd *sd = (struct sd *) gspca_dev;
  543. int luma;
  544. int luma_mean = 128;
  545. int luma_delta = 20;
  546. __u8 spring = 5;
  547. int Gbright;
  548. if (!atomic_read(&sd->do_gain))
  549. return;
  550. atomic_set(&sd->do_gain, 0);
  551. luma = atomic_read(&sd->avg_lum);
  552. Gbright = reg_r(gspca_dev, 0x02);
  553. PDEBUG(D_FRAM, "luma mean %d", luma);
  554. if (luma < luma_mean - luma_delta ||
  555. luma > luma_mean + luma_delta) {
  556. Gbright += (luma_mean - luma) >> spring;
  557. if (Gbright > 0x1a)
  558. Gbright = 0x1a;
  559. else if (Gbright < 4)
  560. Gbright = 4;
  561. PDEBUG(D_FRAM, "gbright %d", Gbright);
  562. if (sd->sensor == SENSOR_PAC7302) {
  563. reg_w(gspca_dev, 0xff, 0x03);
  564. reg_w(gspca_dev, 0x10, Gbright);
  565. /* load registers to sensor (Bit 0, auto clear) */
  566. reg_w(gspca_dev, 0x11, 0x01);
  567. } else {
  568. reg_w(gspca_dev, 0xff, 0x04);
  569. reg_w(gspca_dev, 0x0f, Gbright);
  570. /* load registers to sensor (Bit 0, auto clear) */
  571. reg_w(gspca_dev, 0x11, 0x01);
  572. }
  573. }
  574. }
  575. /* this function is run at interrupt level */
  576. static void sd_pkt_scan(struct gspca_dev *gspca_dev,
  577. struct gspca_frame *frame, /* target */
  578. __u8 *data, /* isoc packet */
  579. int len) /* iso packet length */
  580. {
  581. struct sd *sd = (struct sd *) gspca_dev;
  582. int i;
  583. #define INTER_FRAME 0x53 /* eof + inter frame + sof */
  584. #define LUM_OFFSET 0x1e /* reverse offset / start of frame */
  585. /*
  586. * inside a frame, there may be:
  587. * escaped ff ('ff 00')
  588. * sequences'ff ff ff xx' to remove
  589. * end of frame ('ff d9')
  590. * at the end of frame, there are:
  591. * ff d9 end of frame
  592. * 0x33 bytes
  593. * one byte luminosity
  594. * 0x16 bytes
  595. * ff ff 00 ff 96 62 44 start of frame
  596. */
  597. if (sd->tosof != 0) { /* if outside a frame */
  598. /* get the luminosity and go to the start of frame */
  599. data += sd->tosof;
  600. len -= sd->tosof;
  601. if (sd->tosof > LUM_OFFSET)
  602. sd->lum_sum += data[-LUM_OFFSET];
  603. sd->tosof = 0;
  604. jpeg_put_header(gspca_dev, frame, 1, 0x21);
  605. }
  606. for (i = 0; i < len; i++) {
  607. if (data[i] != 0xff)
  608. continue;
  609. switch (data[i + 1]) {
  610. case 0xd9: /* 'ff d9' end of frame */
  611. frame = gspca_frame_add(gspca_dev,
  612. LAST_PACKET,
  613. frame, data, i + 2);
  614. data += i + INTER_FRAME;
  615. len -= i + INTER_FRAME;
  616. i = 0;
  617. if (len > -LUM_OFFSET)
  618. sd->lum_sum += data[-LUM_OFFSET];
  619. if (len < 0) {
  620. sd->tosof = -len;
  621. break;
  622. }
  623. jpeg_put_header(gspca_dev, frame, 1, 0x21);
  624. break;
  625. }
  626. }
  627. gspca_frame_add(gspca_dev, INTER_PACKET,
  628. frame, data, i);
  629. }
  630. static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
  631. {
  632. struct sd *sd = (struct sd *) gspca_dev;
  633. sd->brightness = val;
  634. if (gspca_dev->streaming)
  635. setbrightness(gspca_dev);
  636. return 0;
  637. }
  638. static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
  639. {
  640. struct sd *sd = (struct sd *) gspca_dev;
  641. *val = sd->brightness;
  642. return 0;
  643. }
  644. static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
  645. {
  646. struct sd *sd = (struct sd *) gspca_dev;
  647. sd->contrast = val;
  648. if (gspca_dev->streaming)
  649. setcontrast(gspca_dev);
  650. return 0;
  651. }
  652. static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val)
  653. {
  654. struct sd *sd = (struct sd *) gspca_dev;
  655. *val = sd->contrast;
  656. return 0;
  657. }
  658. static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val)
  659. {
  660. struct sd *sd = (struct sd *) gspca_dev;
  661. sd->colors = val;
  662. if (gspca_dev->streaming)
  663. setcolors(gspca_dev);
  664. return 0;
  665. }
  666. static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val)
  667. {
  668. struct sd *sd = (struct sd *) gspca_dev;
  669. *val = sd->colors;
  670. return 0;
  671. }
  672. static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val)
  673. {
  674. struct sd *sd = (struct sd *) gspca_dev;
  675. sd->autogain = val;
  676. if (gspca_dev->streaming)
  677. setautogain(gspca_dev);
  678. return 0;
  679. }
  680. static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val)
  681. {
  682. struct sd *sd = (struct sd *) gspca_dev;
  683. *val = sd->autogain;
  684. return 0;
  685. }
  686. /* sub-driver description */
  687. static struct sd_desc sd_desc = {
  688. .name = MODULE_NAME,
  689. .ctrls = sd_ctrls,
  690. .nctrls = ARRAY_SIZE(sd_ctrls),
  691. .config = sd_config,
  692. .open = sd_open,
  693. .start = sd_start,
  694. .stopN = sd_stopN,
  695. .stop0 = sd_stop0,
  696. .close = sd_close,
  697. .pkt_scan = sd_pkt_scan,
  698. .dq_callback = do_autogain,
  699. };
  700. /* -- module initialisation -- */
  701. static __devinitdata struct usb_device_id device_table[] = {
  702. {USB_DEVICE(0x093a, 0x2600), .driver_info = SENSOR_PAC7311},
  703. {USB_DEVICE(0x093a, 0x2601), .driver_info = SENSOR_PAC7311},
  704. {USB_DEVICE(0x093a, 0x2603), .driver_info = SENSOR_PAC7311},
  705. {USB_DEVICE(0x093a, 0x2608), .driver_info = SENSOR_PAC7311},
  706. {USB_DEVICE(0x093a, 0x260e), .driver_info = SENSOR_PAC7311},
  707. {USB_DEVICE(0x093a, 0x260f), .driver_info = SENSOR_PAC7311},
  708. {USB_DEVICE(0x093a, 0x2621), .driver_info = SENSOR_PAC7302},
  709. {}
  710. };
  711. MODULE_DEVICE_TABLE(usb, device_table);
  712. /* -- device connect -- */
  713. static int sd_probe(struct usb_interface *intf,
  714. const struct usb_device_id *id)
  715. {
  716. return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
  717. THIS_MODULE);
  718. }
  719. static struct usb_driver sd_driver = {
  720. .name = MODULE_NAME,
  721. .id_table = device_table,
  722. .probe = sd_probe,
  723. .disconnect = gspca_disconnect,
  724. };
  725. /* -- module insert / remove -- */
  726. static int __init sd_mod_init(void)
  727. {
  728. if (usb_register(&sd_driver) < 0)
  729. return -1;
  730. PDEBUG(D_PROBE, "registered");
  731. return 0;
  732. }
  733. static void __exit sd_mod_exit(void)
  734. {
  735. usb_deregister(&sd_driver);
  736. PDEBUG(D_PROBE, "deregistered");
  737. }
  738. module_init(sd_mod_init);
  739. module_exit(sd_mod_exit);