pac7311.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961
  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. /* Some documentation about various registers as determined by trial and error.
  22. When the register addresses differ between the 7202 and the 7311 the 2
  23. different addresses are written as 7302addr/7311addr, when one of the 2
  24. addresses is a - sign that register description is not valid for the
  25. matching IC.
  26. Register page 1:
  27. Address Description
  28. -/0x08 Unknown compressor related, must always be 8 except when not
  29. in 640x480 resolution and page 4 reg 2 <= 3 then set it to 9 !
  30. -/0x1b Auto white balance related, bit 0 is AWB enable (inverted)
  31. bits 345 seem to toggle per color gains on/off (inverted)
  32. 0x78 Global control, bit 6 controls the LED (inverted)
  33. -/0x80 JPEG compression ratio ? Best not touched
  34. Register page 3/4:
  35. Address Description
  36. 0x02 Clock divider 2-63, fps =~ 60 / val. Must be a multiple of 3 on
  37. the 7302, so one of 3, 6, 9, ...
  38. -/0x0f Master gain 1-245, low value = high gain
  39. 0x10/- Master gain 0-31
  40. -/0x10 Another gain 0-15, limited influence (1-2x gain I guess)
  41. 0x21 Bitfield: 0-1 unused, 2-3 vflip/hflip, 4-5 unknown, 6-7 unused
  42. */
  43. #define MODULE_NAME "pac7311"
  44. #include "gspca.h"
  45. MODULE_AUTHOR("Thomas Kaiser thomas@kaiser-linux.li");
  46. MODULE_DESCRIPTION("Pixart PAC7311");
  47. MODULE_LICENSE("GPL");
  48. /* specific webcam descriptor */
  49. struct sd {
  50. struct gspca_dev gspca_dev; /* !! must be the first item */
  51. unsigned char brightness;
  52. unsigned char contrast;
  53. unsigned char colors;
  54. unsigned char autogain;
  55. __u8 hflip;
  56. __u8 vflip;
  57. __u8 sensor;
  58. #define SENSOR_PAC7302 0
  59. #define SENSOR_PAC7311 1
  60. u8 sof_read;
  61. u8 autogain_ignore_frames;
  62. atomic_t avg_lum;
  63. };
  64. /* V4L2 controls supported by the driver */
  65. static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
  66. static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
  67. static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val);
  68. static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val);
  69. static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val);
  70. static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val);
  71. static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val);
  72. static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val);
  73. static int sd_sethflip(struct gspca_dev *gspca_dev, __s32 val);
  74. static int sd_gethflip(struct gspca_dev *gspca_dev, __s32 *val);
  75. static int sd_setvflip(struct gspca_dev *gspca_dev, __s32 val);
  76. static int sd_getvflip(struct gspca_dev *gspca_dev, __s32 *val);
  77. static struct ctrl sd_ctrls[] = {
  78. {
  79. {
  80. .id = V4L2_CID_BRIGHTNESS,
  81. .type = V4L2_CTRL_TYPE_INTEGER,
  82. .name = "Brightness",
  83. .minimum = 0,
  84. #define BRIGHTNESS_MAX 0x20
  85. .maximum = BRIGHTNESS_MAX,
  86. .step = 1,
  87. #define BRIGHTNESS_DEF 0x10
  88. .default_value = BRIGHTNESS_DEF,
  89. },
  90. .set = sd_setbrightness,
  91. .get = sd_getbrightness,
  92. },
  93. {
  94. {
  95. .id = V4L2_CID_CONTRAST,
  96. .type = V4L2_CTRL_TYPE_INTEGER,
  97. .name = "Contrast",
  98. .minimum = 0,
  99. #define CONTRAST_MAX 255
  100. .maximum = CONTRAST_MAX,
  101. .step = 1,
  102. #define CONTRAST_DEF 127
  103. .default_value = CONTRAST_DEF,
  104. },
  105. .set = sd_setcontrast,
  106. .get = sd_getcontrast,
  107. },
  108. {
  109. {
  110. .id = V4L2_CID_SATURATION,
  111. .type = V4L2_CTRL_TYPE_INTEGER,
  112. .name = "Saturation",
  113. .minimum = 0,
  114. #define COLOR_MAX 255
  115. .maximum = COLOR_MAX,
  116. .step = 1,
  117. #define COLOR_DEF 127
  118. .default_value = COLOR_DEF,
  119. },
  120. .set = sd_setcolors,
  121. .get = sd_getcolors,
  122. },
  123. {
  124. {
  125. .id = V4L2_CID_AUTOGAIN,
  126. .type = V4L2_CTRL_TYPE_BOOLEAN,
  127. .name = "Auto Gain",
  128. .minimum = 0,
  129. .maximum = 1,
  130. .step = 1,
  131. #define AUTOGAIN_DEF 1
  132. .default_value = AUTOGAIN_DEF,
  133. },
  134. .set = sd_setautogain,
  135. .get = sd_getautogain,
  136. },
  137. /* next controls work with pac7302 only */
  138. #define HFLIP_IDX 4
  139. {
  140. {
  141. .id = V4L2_CID_HFLIP,
  142. .type = V4L2_CTRL_TYPE_BOOLEAN,
  143. .name = "Mirror",
  144. .minimum = 0,
  145. .maximum = 1,
  146. .step = 1,
  147. #define HFLIP_DEF 0
  148. .default_value = HFLIP_DEF,
  149. },
  150. .set = sd_sethflip,
  151. .get = sd_gethflip,
  152. },
  153. #define VFLIP_IDX 5
  154. {
  155. {
  156. .id = V4L2_CID_VFLIP,
  157. .type = V4L2_CTRL_TYPE_BOOLEAN,
  158. .name = "Vflip",
  159. .minimum = 0,
  160. .maximum = 1,
  161. .step = 1,
  162. #define VFLIP_DEF 0
  163. .default_value = VFLIP_DEF,
  164. },
  165. .set = sd_setvflip,
  166. .get = sd_getvflip,
  167. },
  168. };
  169. static struct v4l2_pix_format vga_mode[] = {
  170. {160, 120, V4L2_PIX_FMT_PJPG, V4L2_FIELD_NONE,
  171. .bytesperline = 160,
  172. .sizeimage = 160 * 120 * 3 / 8 + 590,
  173. .colorspace = V4L2_COLORSPACE_JPEG,
  174. .priv = 2},
  175. {320, 240, V4L2_PIX_FMT_PJPG, V4L2_FIELD_NONE,
  176. .bytesperline = 320,
  177. .sizeimage = 320 * 240 * 3 / 8 + 590,
  178. .colorspace = V4L2_COLORSPACE_JPEG,
  179. .priv = 1},
  180. {640, 480, V4L2_PIX_FMT_PJPG, V4L2_FIELD_NONE,
  181. .bytesperline = 640,
  182. .sizeimage = 640 * 480 * 3 / 8 + 590,
  183. .colorspace = V4L2_COLORSPACE_JPEG,
  184. .priv = 0},
  185. };
  186. /* pac 7302 */
  187. static const __u8 probe_7302[] = {
  188. /* index,value */
  189. 0xff, 0x01, /* page 1 */
  190. 0x78, 0x00, /* deactivate */
  191. 0xff, 0x01,
  192. 0x78, 0x40, /* led off */
  193. };
  194. static const __u8 start_7302[] = {
  195. /* index, len, [value]* */
  196. 0xff, 1, 0x00, /* page 0 */
  197. 0x00, 12, 0x01, 0x40, 0x40, 0x40, 0x01, 0xe0, 0x02, 0x80,
  198. 0x00, 0x00, 0x00, 0x00,
  199. 0x0d, 24, 0x03, 0x01, 0x00, 0xb5, 0x07, 0xcb, 0x00, 0x00,
  200. 0x07, 0xc8, 0x00, 0xea, 0x07, 0xcf, 0x07, 0xf7,
  201. 0x07, 0x7e, 0x01, 0x0b, 0x00, 0x00, 0x00, 0x11,
  202. 0x26, 2, 0xaa, 0xaa,
  203. 0x2e, 1, 0x31,
  204. 0x38, 1, 0x01,
  205. 0x3a, 3, 0x14, 0xff, 0x5a,
  206. 0x43, 11, 0x00, 0x0a, 0x18, 0x11, 0x01, 0x2c, 0x88, 0x11,
  207. 0x00, 0x54, 0x11,
  208. 0x55, 1, 0x00,
  209. 0x62, 4, 0x10, 0x1e, 0x1e, 0x18,
  210. 0x6b, 1, 0x00,
  211. 0x6e, 3, 0x08, 0x06, 0x00,
  212. 0x72, 3, 0x00, 0xff, 0x00,
  213. 0x7d, 23, 0x01, 0x01, 0x58, 0x46, 0x50, 0x3c, 0x50, 0x3c,
  214. 0x54, 0x46, 0x54, 0x56, 0x52, 0x50, 0x52, 0x50,
  215. 0x56, 0x64, 0xa4, 0x00, 0xda, 0x00, 0x00,
  216. 0xa2, 10, 0x22, 0x2c, 0x3c, 0x54, 0x69, 0x7c, 0x9c, 0xb9,
  217. 0xd2, 0xeb,
  218. 0xaf, 1, 0x02,
  219. 0xb5, 2, 0x08, 0x08,
  220. 0xb8, 2, 0x08, 0x88,
  221. 0xc4, 4, 0xae, 0x01, 0x04, 0x01,
  222. 0xcc, 1, 0x00,
  223. 0xd1, 11, 0x01, 0x30, 0x49, 0x5e, 0x6f, 0x7f, 0x8e, 0xa9,
  224. 0xc1, 0xd7, 0xec,
  225. 0xdc, 1, 0x01,
  226. 0xff, 1, 0x01, /* page 1 */
  227. 0x12, 3, 0x02, 0x00, 0x01,
  228. 0x3e, 2, 0x00, 0x00,
  229. 0x76, 5, 0x01, 0x20, 0x40, 0x00, 0xf2,
  230. 0x7c, 1, 0x00,
  231. 0x7f, 10, 0x4b, 0x0f, 0x01, 0x2c, 0x02, 0x58, 0x03, 0x20,
  232. 0x02, 0x00,
  233. 0x96, 5, 0x01, 0x10, 0x04, 0x01, 0x04,
  234. 0xc8, 14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00,
  235. 0x07, 0x00, 0x01, 0x07, 0x04, 0x01,
  236. 0xd8, 1, 0x01,
  237. 0xdb, 2, 0x00, 0x01,
  238. 0xde, 7, 0x00, 0x01, 0x04, 0x04, 0x00, 0x00, 0x00,
  239. 0xe6, 4, 0x00, 0x00, 0x00, 0x01,
  240. 0xeb, 1, 0x00,
  241. 0xff, 1, 0x02, /* page 2 */
  242. 0x22, 1, 0x00,
  243. 0xff, 1, 0x03, /* page 3 */
  244. 0x00, 255, /* load the page 3 */
  245. 0x11, 1, 0x01,
  246. 0xff, 1, 0x02, /* page 2 */
  247. 0x13, 1, 0x00,
  248. 0x22, 4, 0x1f, 0xa4, 0xf0, 0x96,
  249. 0x27, 2, 0x14, 0x0c,
  250. 0x2a, 5, 0xc8, 0x00, 0x18, 0x12, 0x22,
  251. 0x64, 8, 0x00, 0x00, 0xf0, 0x01, 0x14, 0x44, 0x44, 0x44,
  252. 0x6e, 1, 0x08,
  253. 0xff, 1, 0x01, /* page 1 */
  254. 0x78, 1, 0x00,
  255. 0, 0 /* end of sequence */
  256. };
  257. /* page 3 - the value 0xaa says skip the index - see reg_w_page() */
  258. static const __u8 page3_7302[] = {
  259. 0x90, 0x40, 0x03, 0x50, 0xc2, 0x01, 0x14, 0x16,
  260. 0x14, 0x12, 0x00, 0x00, 0x00, 0x02, 0x33, 0x00,
  261. 0x0f, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  262. 0x00, 0x00, 0x00, 0x47, 0x01, 0xb3, 0x01, 0x00,
  263. 0x00, 0x08, 0x00, 0x00, 0x0d, 0x00, 0x00, 0x21,
  264. 0x00, 0x00, 0x00, 0x54, 0xf4, 0x02, 0x52, 0x54,
  265. 0xa4, 0xb8, 0xe0, 0x2a, 0xf6, 0x00, 0x00, 0x00,
  266. 0x00, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  267. 0x00, 0xfc, 0x00, 0xf2, 0x1f, 0x04, 0x00, 0x00,
  268. 0x00, 0x00, 0x00, 0xc0, 0xc0, 0x10, 0x00, 0x00,
  269. 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  270. 0x00, 0x40, 0xff, 0x03, 0x19, 0x00, 0x00, 0x00,
  271. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  272. 0x00, 0x00, 0x00, 0x00, 0x00, 0xc8, 0xc8, 0xc8,
  273. 0xc8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50,
  274. 0x08, 0x10, 0x24, 0x40, 0x00, 0x00, 0x00, 0x00,
  275. 0x01, 0x00, 0x02, 0x47, 0x00, 0x00, 0x00, 0x00,
  276. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  277. 0x00, 0x02, 0xfa, 0x00, 0x64, 0x5a, 0x28, 0x00,
  278. 0x00
  279. };
  280. /* pac 7311 */
  281. static const __u8 probe_7311[] = {
  282. 0x78, 0x40, /* Bit_0=start stream, Bit_6=LED */
  283. 0x78, 0x40, /* Bit_0=start stream, Bit_6=LED */
  284. 0x78, 0x44, /* Bit_0=start stream, Bit_6=LED */
  285. 0xff, 0x04,
  286. 0x27, 0x80,
  287. 0x28, 0xca,
  288. 0x29, 0x53,
  289. 0x2a, 0x0e,
  290. 0xff, 0x01,
  291. 0x3e, 0x20,
  292. };
  293. static const __u8 start_7311[] = {
  294. /* index, len, [value]* */
  295. 0xff, 1, 0x01, /* page 1 */
  296. 0x02, 43, 0x48, 0x0a, 0x40, 0x08, 0x00, 0x00, 0x08, 0x00,
  297. 0x06, 0xff, 0x11, 0xff, 0x5a, 0x30, 0x90, 0x4c,
  298. 0x00, 0x07, 0x00, 0x0a, 0x10, 0x00, 0xa0, 0x10,
  299. 0x02, 0x00, 0x00, 0x00, 0x00, 0x0b, 0x01, 0x00,
  300. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  301. 0x00, 0x00, 0x00,
  302. 0x3e, 42, 0x00, 0x00, 0x78, 0x52, 0x4a, 0x52, 0x78, 0x6e,
  303. 0x48, 0x46, 0x48, 0x6e, 0x5f, 0x49, 0x42, 0x49,
  304. 0x5f, 0x5f, 0x49, 0x42, 0x49, 0x5f, 0x6e, 0x48,
  305. 0x46, 0x48, 0x6e, 0x78, 0x52, 0x4a, 0x52, 0x78,
  306. 0x00, 0x00, 0x09, 0x1b, 0x34, 0x49, 0x5c, 0x9b,
  307. 0xd0, 0xff,
  308. 0x78, 6, 0x44, 0x00, 0xf2, 0x01, 0x01, 0x80,
  309. 0x7f, 18, 0x2a, 0x1c, 0x00, 0xc8, 0x02, 0x58, 0x03, 0x84,
  310. 0x12, 0x00, 0x1a, 0x04, 0x08, 0x0c, 0x10, 0x14,
  311. 0x18, 0x20,
  312. 0x96, 3, 0x01, 0x08, 0x04,
  313. 0xa0, 4, 0x44, 0x44, 0x44, 0x04,
  314. 0xf0, 13, 0x01, 0x00, 0x00, 0x00, 0x22, 0x00, 0x20, 0x00,
  315. 0x3f, 0x00, 0x0a, 0x01, 0x00,
  316. 0xff, 1, 0x04, /* page 4 */
  317. 0x00, 254, /* load the page 4 */
  318. 0x11, 1, 0x01,
  319. 0, 0 /* end of sequence */
  320. };
  321. /* page 4 - the value 0xaa says skip the index - see reg_w_page() */
  322. static const __u8 page4_7311[] = {
  323. 0xaa, 0xaa, 0x04, 0x54, 0x07, 0x2b, 0x09, 0x0f,
  324. 0x09, 0x00, 0xaa, 0xaa, 0x07, 0x00, 0x00, 0x62,
  325. 0x08, 0xaa, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00,
  326. 0x00, 0x00, 0x00, 0x03, 0xa0, 0x01, 0xf4, 0xaa,
  327. 0xaa, 0x00, 0x08, 0xaa, 0x03, 0xaa, 0x00, 0x01,
  328. 0xca, 0x10, 0x06, 0x78, 0x00, 0x00, 0x00, 0x00,
  329. 0x23, 0x28, 0x04, 0x11, 0x00, 0x00
  330. };
  331. static void reg_w_buf(struct gspca_dev *gspca_dev,
  332. __u8 index,
  333. const char *buffer, int len)
  334. {
  335. memcpy(gspca_dev->usb_buf, buffer, len);
  336. usb_control_msg(gspca_dev->dev,
  337. usb_sndctrlpipe(gspca_dev->dev, 0),
  338. 1, /* request */
  339. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  340. 0, /* value */
  341. index, gspca_dev->usb_buf, len,
  342. 500);
  343. }
  344. static void reg_w(struct gspca_dev *gspca_dev,
  345. __u8 index,
  346. __u8 value)
  347. {
  348. gspca_dev->usb_buf[0] = value;
  349. usb_control_msg(gspca_dev->dev,
  350. usb_sndctrlpipe(gspca_dev->dev, 0),
  351. 0, /* request */
  352. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  353. value, index, gspca_dev->usb_buf, 1,
  354. 500);
  355. }
  356. static void reg_w_seq(struct gspca_dev *gspca_dev,
  357. const __u8 *seq, int len)
  358. {
  359. while (--len >= 0) {
  360. reg_w(gspca_dev, seq[0], seq[1]);
  361. seq += 2;
  362. }
  363. }
  364. /* load the beginning of a page */
  365. static void reg_w_page(struct gspca_dev *gspca_dev,
  366. const __u8 *page, int len)
  367. {
  368. int index;
  369. for (index = 0; index < len; index++) {
  370. if (page[index] == 0xaa) /* skip this index */
  371. continue;
  372. gspca_dev->usb_buf[0] = page[index];
  373. usb_control_msg(gspca_dev->dev,
  374. usb_sndctrlpipe(gspca_dev->dev, 0),
  375. 0, /* request */
  376. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  377. 0, index, gspca_dev->usb_buf, 1,
  378. 500);
  379. }
  380. }
  381. /* output a variable sequence */
  382. static void reg_w_var(struct gspca_dev *gspca_dev,
  383. const __u8 *seq)
  384. {
  385. int index, len;
  386. for (;;) {
  387. index = *seq++;
  388. len = *seq++;
  389. switch (len) {
  390. case 0:
  391. return;
  392. case 254:
  393. reg_w_page(gspca_dev, page4_7311, sizeof page4_7311);
  394. break;
  395. case 255:
  396. reg_w_page(gspca_dev, page3_7302, sizeof page3_7302);
  397. break;
  398. default:
  399. if (len > 64) {
  400. PDEBUG(D_ERR|D_STREAM,
  401. "Incorrect variable sequence");
  402. return;
  403. }
  404. while (len > 0) {
  405. if (len < 8) {
  406. reg_w_buf(gspca_dev, index, seq, len);
  407. seq += len;
  408. break;
  409. }
  410. reg_w_buf(gspca_dev, index, seq, 8);
  411. seq += 8;
  412. index += 8;
  413. len -= 8;
  414. }
  415. }
  416. }
  417. /* not reached */
  418. }
  419. /* this function is called at probe time */
  420. static int sd_config(struct gspca_dev *gspca_dev,
  421. const struct usb_device_id *id)
  422. {
  423. struct sd *sd = (struct sd *) gspca_dev;
  424. struct cam *cam;
  425. cam = &gspca_dev->cam;
  426. cam->epaddr = 0x05;
  427. sd->sensor = id->driver_info;
  428. if (sd->sensor == SENSOR_PAC7302) {
  429. PDEBUG(D_CONF, "Find Sensor PAC7302");
  430. reg_w_seq(gspca_dev, probe_7302, sizeof probe_7302);
  431. cam->cam_mode = &vga_mode[2]; /* only 640x480 */
  432. cam->nmodes = 1;
  433. } else {
  434. PDEBUG(D_CONF, "Find Sensor PAC7311");
  435. reg_w_seq(gspca_dev, probe_7311, sizeof probe_7311);
  436. cam->cam_mode = vga_mode;
  437. cam->nmodes = ARRAY_SIZE(vga_mode);
  438. gspca_dev->ctrl_dis = (1 << HFLIP_IDX)
  439. | (1 << VFLIP_IDX);
  440. }
  441. sd->brightness = BRIGHTNESS_DEF;
  442. sd->contrast = CONTRAST_DEF;
  443. sd->colors = COLOR_DEF;
  444. sd->autogain = AUTOGAIN_DEF;
  445. sd->hflip = HFLIP_DEF;
  446. sd->vflip = VFLIP_DEF;
  447. return 0;
  448. }
  449. /* rev 12a only */
  450. static void setbrightcont(struct gspca_dev *gspca_dev)
  451. {
  452. struct sd *sd = (struct sd *) gspca_dev;
  453. int i, v;
  454. static const __u8 max[10] =
  455. {0x29, 0x33, 0x42, 0x5a, 0x6e, 0x80, 0x9f, 0xbb,
  456. 0xd4, 0xec};
  457. static const __u8 delta[10] =
  458. {0x35, 0x33, 0x33, 0x2f, 0x2a, 0x25, 0x1e, 0x17,
  459. 0x11, 0x0b};
  460. reg_w(gspca_dev, 0xff, 0x00); /* page 0 */
  461. for (i = 0; i < 10; i++) {
  462. v = max[i];
  463. v += (sd->brightness - BRIGHTNESS_MAX)
  464. * 150 / BRIGHTNESS_MAX; /* 200 ? */
  465. v -= delta[i] * sd->contrast / CONTRAST_MAX;
  466. if (v < 0)
  467. v = 0;
  468. else if (v > 0xff)
  469. v = 0xff;
  470. reg_w(gspca_dev, 0xa2 + i, v);
  471. }
  472. reg_w(gspca_dev, 0xdc, 0x01);
  473. }
  474. /* This function is used by pac7302 only */
  475. static void setbrightness(struct gspca_dev *gspca_dev)
  476. {
  477. struct sd *sd = (struct sd *) gspca_dev;
  478. int brightness;
  479. if (sd->sensor == SENSOR_PAC7302) {
  480. setbrightcont(gspca_dev);
  481. return;
  482. }
  483. /* HDG: this is not brightness but gain, I'll add gain and exposure controls
  484. in a next patch */
  485. return;
  486. brightness = BRIGHTNESS_MAX - sd->brightness;
  487. reg_w(gspca_dev, 0xff, 0x04);
  488. reg_w(gspca_dev, 0x0e, 0x00);
  489. reg_w(gspca_dev, 0x0f, brightness);
  490. /* load registers to sensor (Bit 0, auto clear) */
  491. reg_w(gspca_dev, 0x11, 0x01);
  492. PDEBUG(D_CONF|D_STREAM, "brightness: %i", brightness);
  493. }
  494. static void setcontrast(struct gspca_dev *gspca_dev)
  495. {
  496. struct sd *sd = (struct sd *) gspca_dev;
  497. if (sd->sensor == SENSOR_PAC7302) {
  498. setbrightcont(gspca_dev);
  499. return;
  500. }
  501. reg_w(gspca_dev, 0xff, 0x04);
  502. reg_w(gspca_dev, 0x10, sd->contrast >> 4);
  503. /* load registers to sensor (Bit 0, auto clear) */
  504. reg_w(gspca_dev, 0x11, 0x01);
  505. }
  506. /* This function is used by pac7302 only */
  507. static void setcolors(struct gspca_dev *gspca_dev)
  508. {
  509. struct sd *sd = (struct sd *) gspca_dev;
  510. if (sd->sensor == SENSOR_PAC7302) {
  511. int i, v;
  512. static const int a[9] =
  513. {217, -212, 0, -101, 170, -67, -38, -315, 355};
  514. static const int b[9] =
  515. {19, 106, 0, 19, 106, 1, 19, 106, 1};
  516. reg_w(gspca_dev, 0xff, 0x03); /* page 3 */
  517. reg_w(gspca_dev, 0x11, 0x01);
  518. reg_w(gspca_dev, 0xff, 0x00); /* page 0 */
  519. reg_w(gspca_dev, 0xff, 0x00); /* page 0 */
  520. for (i = 0; i < 9; i++) {
  521. v = a[i] * sd->colors / COLOR_MAX + b[i];
  522. reg_w(gspca_dev, 0x0f + 2 * i, (v >> 8) & 0x07);
  523. reg_w(gspca_dev, 0x0f + 2 * i + 1, v);
  524. }
  525. reg_w(gspca_dev, 0xdc, 0x01);
  526. PDEBUG(D_CONF|D_STREAM, "color: %i", sd->colors);
  527. }
  528. }
  529. static void sethvflip(struct gspca_dev *gspca_dev)
  530. {
  531. struct sd *sd = (struct sd *) gspca_dev;
  532. __u8 data;
  533. if (sd->sensor == SENSOR_PAC7302) {
  534. reg_w(gspca_dev, 0xff, 0x03); /* page 3 */
  535. data = (sd->hflip ? 0x00 : 0x08)
  536. | (sd->vflip ? 0x04 : 0x00);
  537. } else {
  538. reg_w(gspca_dev, 0xff, 0x04); /* page 3 */
  539. data = (sd->hflip ? 0x04 : 0x00)
  540. | (sd->vflip ? 0x08 : 0x00);
  541. }
  542. reg_w(gspca_dev, 0x21, data);
  543. reg_w(gspca_dev, 0x11, 0x01);
  544. }
  545. /* this function is called at open time */
  546. static int sd_open(struct gspca_dev *gspca_dev)
  547. {
  548. return 0;
  549. }
  550. static void sd_start(struct gspca_dev *gspca_dev)
  551. {
  552. struct sd *sd = (struct sd *) gspca_dev;
  553. sd->sof_read = 0;
  554. if (sd->sensor == SENSOR_PAC7302)
  555. reg_w_var(gspca_dev, start_7302);
  556. else
  557. reg_w_var(gspca_dev, start_7311);
  558. setcontrast(gspca_dev);
  559. setbrightness(gspca_dev);
  560. setcolors(gspca_dev);
  561. /* set correct resolution */
  562. switch (gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv) {
  563. case 2: /* 160x120 pac7311 */
  564. reg_w(gspca_dev, 0xff, 0x04);
  565. reg_w(gspca_dev, 0x02, 0x03);
  566. reg_w(gspca_dev, 0xff, 0x01);
  567. reg_w(gspca_dev, 0x08, 0x09);
  568. reg_w(gspca_dev, 0x17, 0x20);
  569. reg_w(gspca_dev, 0x1b, 0x00);
  570. reg_w(gspca_dev, 0x87, 0x10);
  571. break;
  572. case 1: /* 320x240 pac7311 */
  573. reg_w(gspca_dev, 0xff, 0x04);
  574. reg_w(gspca_dev, 0x02, 0x03);
  575. reg_w(gspca_dev, 0xff, 0x01);
  576. reg_w(gspca_dev, 0x08, 0x09);
  577. reg_w(gspca_dev, 0x17, 0x30);
  578. reg_w(gspca_dev, 0x87, 0x11);
  579. break;
  580. case 0: /* 640x480 */
  581. if (sd->sensor == SENSOR_PAC7302)
  582. break;
  583. reg_w(gspca_dev, 0xff, 0x04);
  584. reg_w(gspca_dev, 0x02, 0x07);
  585. reg_w(gspca_dev, 0xff, 0x01);
  586. reg_w(gspca_dev, 0x08, 0x08);
  587. reg_w(gspca_dev, 0x17, 0x00);
  588. reg_w(gspca_dev, 0x87, 0x12);
  589. break;
  590. }
  591. /* start stream */
  592. reg_w(gspca_dev, 0xff, 0x01);
  593. if (sd->sensor == SENSOR_PAC7302) {
  594. sethvflip(gspca_dev);
  595. reg_w(gspca_dev, 0x78, 0x01);
  596. reg_w(gspca_dev, 0xff, 0x01);
  597. reg_w(gspca_dev, 0x78, 0x01);
  598. } else {
  599. reg_w(gspca_dev, 0x78, 0x44);
  600. reg_w(gspca_dev, 0x78, 0x45);
  601. }
  602. sd->sof_read = 0;
  603. sd->autogain_ignore_frames = 0;
  604. atomic_set(&sd->avg_lum, -1);
  605. }
  606. static void sd_stopN(struct gspca_dev *gspca_dev)
  607. {
  608. struct sd *sd = (struct sd *) gspca_dev;
  609. if (sd->sensor == SENSOR_PAC7302) {
  610. reg_w(gspca_dev, 0xff, 0x01);
  611. reg_w(gspca_dev, 0x78, 0x00);
  612. reg_w(gspca_dev, 0x78, 0x00);
  613. return;
  614. }
  615. reg_w(gspca_dev, 0xff, 0x04);
  616. reg_w(gspca_dev, 0x27, 0x80);
  617. reg_w(gspca_dev, 0x28, 0xca);
  618. reg_w(gspca_dev, 0x29, 0x53);
  619. reg_w(gspca_dev, 0x2a, 0x0e);
  620. reg_w(gspca_dev, 0xff, 0x01);
  621. reg_w(gspca_dev, 0x3e, 0x20);
  622. reg_w(gspca_dev, 0x78, 0x44); /* Bit_0=start stream, Bit_6=LED */
  623. reg_w(gspca_dev, 0x78, 0x44); /* Bit_0=start stream, Bit_6=LED */
  624. reg_w(gspca_dev, 0x78, 0x44); /* Bit_0=start stream, Bit_6=LED */
  625. }
  626. static void sd_stop0(struct gspca_dev *gspca_dev)
  627. {
  628. struct sd *sd = (struct sd *) gspca_dev;
  629. if (sd->sensor == SENSOR_PAC7302) {
  630. reg_w(gspca_dev, 0xff, 0x01);
  631. reg_w(gspca_dev, 0x78, 0x40);
  632. }
  633. }
  634. /* this function is called at close time */
  635. static void sd_close(struct gspca_dev *gspca_dev)
  636. {
  637. }
  638. static void do_autogain(struct gspca_dev *gspca_dev)
  639. {
  640. }
  641. static const unsigned char pac7311_jpeg_header1[] = {
  642. 0xff, 0xd8, 0xff, 0xc0, 0x00, 0x11, 0x08
  643. };
  644. static const unsigned char pac7311_jpeg_header2[] = {
  645. 0x03, 0x01, 0x21, 0x00, 0x02, 0x11, 0x01, 0x03, 0x11, 0x01, 0xff, 0xda,
  646. 0x00, 0x0c, 0x03, 0x01, 0x00, 0x02, 0x11, 0x03, 0x11, 0x00, 0x3f, 0x00
  647. };
  648. /* Include pac common sof detection functions */
  649. #include "pac_common.h"
  650. /* this function is run at interrupt level */
  651. static void sd_pkt_scan(struct gspca_dev *gspca_dev,
  652. struct gspca_frame *frame, /* target */
  653. __u8 *data, /* isoc packet */
  654. int len) /* iso packet length */
  655. {
  656. struct sd *sd = (struct sd *) gspca_dev;
  657. unsigned char *sof;
  658. sof = pac_find_sof(gspca_dev, data, len);
  659. if (sof) {
  660. unsigned char tmpbuf[4];
  661. int n, lum_offset, footer_length;
  662. if (sd->sensor == SENSOR_PAC7302) {
  663. lum_offset = 34 + sizeof pac_sof_marker;
  664. footer_length = 74;
  665. } else {
  666. lum_offset = 24 + sizeof pac_sof_marker;
  667. footer_length = 26;
  668. }
  669. /* Finish decoding current frame */
  670. n = (sof - data) - (footer_length + sizeof pac_sof_marker);
  671. if (n < 0) {
  672. frame->data_end += n;
  673. n = 0;
  674. }
  675. frame = gspca_frame_add(gspca_dev, INTER_PACKET, frame,
  676. data, n);
  677. if (gspca_dev->last_packet_type != DISCARD_PACKET &&
  678. frame->data_end[-2] == 0xff &&
  679. frame->data_end[-1] == 0xd9)
  680. frame = gspca_frame_add(gspca_dev, LAST_PACKET, frame,
  681. NULL, 0);
  682. n = sof - data;
  683. len -= n;
  684. data = sof;
  685. /* Get average lumination */
  686. if (gspca_dev->last_packet_type == LAST_PACKET &&
  687. n >= lum_offset) {
  688. if (sd->sensor == SENSOR_PAC7302)
  689. atomic_set(&sd->avg_lum,
  690. (data[-lum_offset] << 8) |
  691. data[-lum_offset + 1]);
  692. else
  693. atomic_set(&sd->avg_lum,
  694. data[-lum_offset] +
  695. data[-lum_offset + 1]);
  696. } else {
  697. atomic_set(&sd->avg_lum, -1);
  698. }
  699. /* Start the new frame with the jpeg header */
  700. gspca_frame_add(gspca_dev, FIRST_PACKET, frame,
  701. pac7311_jpeg_header1, sizeof(pac7311_jpeg_header1));
  702. if (sd->sensor == SENSOR_PAC7302) {
  703. /* The PAC7302 has the image rotated 90 degrees */
  704. tmpbuf[0] = gspca_dev->width >> 8;
  705. tmpbuf[1] = gspca_dev->width & 0xff;
  706. tmpbuf[2] = gspca_dev->height >> 8;
  707. tmpbuf[3] = gspca_dev->height & 0xff;
  708. } else {
  709. tmpbuf[0] = gspca_dev->height >> 8;
  710. tmpbuf[1] = gspca_dev->height & 0xff;
  711. tmpbuf[2] = gspca_dev->width >> 8;
  712. tmpbuf[3] = gspca_dev->width & 0xff;
  713. }
  714. gspca_frame_add(gspca_dev, INTER_PACKET, frame, tmpbuf, 4);
  715. gspca_frame_add(gspca_dev, INTER_PACKET, frame,
  716. pac7311_jpeg_header2, sizeof(pac7311_jpeg_header2));
  717. }
  718. gspca_frame_add(gspca_dev, INTER_PACKET, frame, data, len);
  719. }
  720. static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
  721. {
  722. struct sd *sd = (struct sd *) gspca_dev;
  723. sd->brightness = val;
  724. if (gspca_dev->streaming)
  725. setbrightness(gspca_dev);
  726. return 0;
  727. }
  728. static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
  729. {
  730. struct sd *sd = (struct sd *) gspca_dev;
  731. *val = sd->brightness;
  732. return 0;
  733. }
  734. static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
  735. {
  736. struct sd *sd = (struct sd *) gspca_dev;
  737. sd->contrast = val;
  738. if (gspca_dev->streaming)
  739. setcontrast(gspca_dev);
  740. return 0;
  741. }
  742. static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val)
  743. {
  744. struct sd *sd = (struct sd *) gspca_dev;
  745. *val = sd->contrast;
  746. return 0;
  747. }
  748. static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val)
  749. {
  750. struct sd *sd = (struct sd *) gspca_dev;
  751. sd->colors = val;
  752. if (gspca_dev->streaming)
  753. setcolors(gspca_dev);
  754. return 0;
  755. }
  756. static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val)
  757. {
  758. struct sd *sd = (struct sd *) gspca_dev;
  759. *val = sd->colors;
  760. return 0;
  761. }
  762. static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val)
  763. {
  764. struct sd *sd = (struct sd *) gspca_dev;
  765. sd->autogain = val;
  766. return 0;
  767. }
  768. static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val)
  769. {
  770. struct sd *sd = (struct sd *) gspca_dev;
  771. *val = sd->autogain;
  772. return 0;
  773. }
  774. static int sd_sethflip(struct gspca_dev *gspca_dev, __s32 val)
  775. {
  776. struct sd *sd = (struct sd *) gspca_dev;
  777. sd->hflip = val;
  778. if (gspca_dev->streaming)
  779. sethvflip(gspca_dev);
  780. return 0;
  781. }
  782. static int sd_gethflip(struct gspca_dev *gspca_dev, __s32 *val)
  783. {
  784. struct sd *sd = (struct sd *) gspca_dev;
  785. *val = sd->hflip;
  786. return 0;
  787. }
  788. static int sd_setvflip(struct gspca_dev *gspca_dev, __s32 val)
  789. {
  790. struct sd *sd = (struct sd *) gspca_dev;
  791. sd->vflip = val;
  792. if (gspca_dev->streaming)
  793. sethvflip(gspca_dev);
  794. return 0;
  795. }
  796. static int sd_getvflip(struct gspca_dev *gspca_dev, __s32 *val)
  797. {
  798. struct sd *sd = (struct sd *) gspca_dev;
  799. *val = sd->vflip;
  800. return 0;
  801. }
  802. /* sub-driver description */
  803. static struct sd_desc sd_desc = {
  804. .name = MODULE_NAME,
  805. .ctrls = sd_ctrls,
  806. .nctrls = ARRAY_SIZE(sd_ctrls),
  807. .config = sd_config,
  808. .open = sd_open,
  809. .start = sd_start,
  810. .stopN = sd_stopN,
  811. .stop0 = sd_stop0,
  812. .close = sd_close,
  813. .pkt_scan = sd_pkt_scan,
  814. .dq_callback = do_autogain,
  815. };
  816. /* -- module initialisation -- */
  817. static __devinitdata struct usb_device_id device_table[] = {
  818. {USB_DEVICE(0x093a, 0x2600), .driver_info = SENSOR_PAC7311},
  819. {USB_DEVICE(0x093a, 0x2601), .driver_info = SENSOR_PAC7311},
  820. {USB_DEVICE(0x093a, 0x2603), .driver_info = SENSOR_PAC7311},
  821. {USB_DEVICE(0x093a, 0x2608), .driver_info = SENSOR_PAC7311},
  822. {USB_DEVICE(0x093a, 0x260e), .driver_info = SENSOR_PAC7311},
  823. {USB_DEVICE(0x093a, 0x260f), .driver_info = SENSOR_PAC7311},
  824. {USB_DEVICE(0x093a, 0x2621), .driver_info = SENSOR_PAC7302},
  825. {}
  826. };
  827. MODULE_DEVICE_TABLE(usb, device_table);
  828. /* -- device connect -- */
  829. static int sd_probe(struct usb_interface *intf,
  830. const struct usb_device_id *id)
  831. {
  832. return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
  833. THIS_MODULE);
  834. }
  835. static struct usb_driver sd_driver = {
  836. .name = MODULE_NAME,
  837. .id_table = device_table,
  838. .probe = sd_probe,
  839. .disconnect = gspca_disconnect,
  840. #ifdef CONFIG_PM
  841. .suspend = gspca_suspend,
  842. .resume = gspca_resume,
  843. #endif
  844. };
  845. /* -- module insert / remove -- */
  846. static int __init sd_mod_init(void)
  847. {
  848. if (usb_register(&sd_driver) < 0)
  849. return -1;
  850. PDEBUG(D_PROBE, "registered");
  851. return 0;
  852. }
  853. static void __exit sd_mod_exit(void)
  854. {
  855. usb_deregister(&sd_driver);
  856. PDEBUG(D_PROBE, "deregistered");
  857. }
  858. module_init(sd_mod_init);
  859. module_exit(sd_mod_exit);