sunplus.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428
  1. /*
  2. * Sunplus spca504(abc) spca533 spca536 library
  3. * Copyright (C) 2005 Michel Xhaard mxhaard@magic.fr
  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 "sunplus"
  22. #include "gspca.h"
  23. #include "jpeg.h"
  24. MODULE_AUTHOR("Michel Xhaard <mxhaard@users.sourceforge.net>");
  25. MODULE_DESCRIPTION("GSPCA/SPCA5xx USB Camera Driver");
  26. MODULE_LICENSE("GPL");
  27. /* specific webcam descriptor */
  28. struct sd {
  29. struct gspca_dev gspca_dev; /* !! must be the first item */
  30. s8 brightness;
  31. u8 contrast;
  32. u8 colors;
  33. u8 autogain;
  34. u8 quality;
  35. #define QUALITY_MIN 70
  36. #define QUALITY_MAX 95
  37. #define QUALITY_DEF 85
  38. u8 bridge;
  39. #define BRIDGE_SPCA504 0
  40. #define BRIDGE_SPCA504B 1
  41. #define BRIDGE_SPCA504C 2
  42. #define BRIDGE_SPCA533 3
  43. #define BRIDGE_SPCA536 4
  44. u8 subtype;
  45. #define AiptekMiniPenCam13 1
  46. #define LogitechClickSmart420 2
  47. #define LogitechClickSmart820 3
  48. #define MegapixV4 4
  49. #define MegaImageVI 5
  50. u8 *jpeg_hdr;
  51. };
  52. /* V4L2 controls supported by the driver */
  53. static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
  54. static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
  55. static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val);
  56. static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val);
  57. static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val);
  58. static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val);
  59. static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val);
  60. static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val);
  61. static struct ctrl sd_ctrls[] = {
  62. {
  63. {
  64. .id = V4L2_CID_BRIGHTNESS,
  65. .type = V4L2_CTRL_TYPE_INTEGER,
  66. .name = "Brightness",
  67. .minimum = -128,
  68. .maximum = 127,
  69. .step = 1,
  70. #define BRIGHTNESS_DEF 0
  71. .default_value = BRIGHTNESS_DEF,
  72. },
  73. .set = sd_setbrightness,
  74. .get = sd_getbrightness,
  75. },
  76. {
  77. {
  78. .id = V4L2_CID_CONTRAST,
  79. .type = V4L2_CTRL_TYPE_INTEGER,
  80. .name = "Contrast",
  81. .minimum = 0,
  82. .maximum = 0xff,
  83. .step = 1,
  84. #define CONTRAST_DEF 0x20
  85. .default_value = CONTRAST_DEF,
  86. },
  87. .set = sd_setcontrast,
  88. .get = sd_getcontrast,
  89. },
  90. {
  91. {
  92. .id = V4L2_CID_SATURATION,
  93. .type = V4L2_CTRL_TYPE_INTEGER,
  94. .name = "Color",
  95. .minimum = 0,
  96. .maximum = 0xff,
  97. .step = 1,
  98. #define COLOR_DEF 0x1a
  99. .default_value = COLOR_DEF,
  100. },
  101. .set = sd_setcolors,
  102. .get = sd_getcolors,
  103. },
  104. {
  105. {
  106. .id = V4L2_CID_AUTOGAIN,
  107. .type = V4L2_CTRL_TYPE_BOOLEAN,
  108. .name = "Auto Gain",
  109. .minimum = 0,
  110. .maximum = 1,
  111. .step = 1,
  112. #define AUTOGAIN_DEF 1
  113. .default_value = AUTOGAIN_DEF,
  114. },
  115. .set = sd_setautogain,
  116. .get = sd_getautogain,
  117. },
  118. };
  119. static const struct v4l2_pix_format vga_mode[] = {
  120. {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
  121. .bytesperline = 320,
  122. .sizeimage = 320 * 240 * 3 / 8 + 590,
  123. .colorspace = V4L2_COLORSPACE_JPEG,
  124. .priv = 2},
  125. {640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
  126. .bytesperline = 640,
  127. .sizeimage = 640 * 480 * 3 / 8 + 590,
  128. .colorspace = V4L2_COLORSPACE_JPEG,
  129. .priv = 1},
  130. };
  131. static const struct v4l2_pix_format custom_mode[] = {
  132. {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
  133. .bytesperline = 320,
  134. .sizeimage = 320 * 240 * 3 / 8 + 590,
  135. .colorspace = V4L2_COLORSPACE_JPEG,
  136. .priv = 2},
  137. {464, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
  138. .bytesperline = 464,
  139. .sizeimage = 464 * 480 * 3 / 8 + 590,
  140. .colorspace = V4L2_COLORSPACE_JPEG,
  141. .priv = 1},
  142. };
  143. static const struct v4l2_pix_format vga_mode2[] = {
  144. {176, 144, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
  145. .bytesperline = 176,
  146. .sizeimage = 176 * 144 * 3 / 8 + 590,
  147. .colorspace = V4L2_COLORSPACE_JPEG,
  148. .priv = 4},
  149. {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
  150. .bytesperline = 320,
  151. .sizeimage = 320 * 240 * 3 / 8 + 590,
  152. .colorspace = V4L2_COLORSPACE_JPEG,
  153. .priv = 3},
  154. {352, 288, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
  155. .bytesperline = 352,
  156. .sizeimage = 352 * 288 * 3 / 8 + 590,
  157. .colorspace = V4L2_COLORSPACE_JPEG,
  158. .priv = 2},
  159. {640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
  160. .bytesperline = 640,
  161. .sizeimage = 640 * 480 * 3 / 8 + 590,
  162. .colorspace = V4L2_COLORSPACE_JPEG,
  163. .priv = 1},
  164. };
  165. #define SPCA50X_OFFSET_DATA 10
  166. #define SPCA504_PCCAM600_OFFSET_SNAPSHOT 3
  167. #define SPCA504_PCCAM600_OFFSET_COMPRESS 4
  168. #define SPCA504_PCCAM600_OFFSET_MODE 5
  169. #define SPCA504_PCCAM600_OFFSET_DATA 14
  170. /* Frame packet header offsets for the spca533 */
  171. #define SPCA533_OFFSET_DATA 16
  172. #define SPCA533_OFFSET_FRAMSEQ 15
  173. /* Frame packet header offsets for the spca536 */
  174. #define SPCA536_OFFSET_DATA 4
  175. #define SPCA536_OFFSET_FRAMSEQ 1
  176. struct cmd {
  177. u8 req;
  178. u16 val;
  179. u16 idx;
  180. };
  181. /* Initialisation data for the Creative PC-CAM 600 */
  182. static const struct cmd spca504_pccam600_init_data[] = {
  183. /* {0xa0, 0x0000, 0x0503}, * capture mode */
  184. {0x00, 0x0000, 0x2000},
  185. {0x00, 0x0013, 0x2301},
  186. {0x00, 0x0003, 0x2000},
  187. {0x00, 0x0001, 0x21ac},
  188. {0x00, 0x0001, 0x21a6},
  189. {0x00, 0x0000, 0x21a7}, /* brightness */
  190. {0x00, 0x0020, 0x21a8}, /* contrast */
  191. {0x00, 0x0001, 0x21ac}, /* sat/hue */
  192. {0x00, 0x0000, 0x21ad}, /* hue */
  193. {0x00, 0x001a, 0x21ae}, /* saturation */
  194. {0x00, 0x0002, 0x21a3}, /* gamma */
  195. {0x30, 0x0154, 0x0008},
  196. {0x30, 0x0004, 0x0006},
  197. {0x30, 0x0258, 0x0009},
  198. {0x30, 0x0004, 0x0000},
  199. {0x30, 0x0093, 0x0004},
  200. {0x30, 0x0066, 0x0005},
  201. {0x00, 0x0000, 0x2000},
  202. {0x00, 0x0013, 0x2301},
  203. {0x00, 0x0003, 0x2000},
  204. {0x00, 0x0013, 0x2301},
  205. {0x00, 0x0003, 0x2000},
  206. };
  207. /* Creative PC-CAM 600 specific open data, sent before using the
  208. * generic initialisation data from spca504_open_data.
  209. */
  210. static const struct cmd spca504_pccam600_open_data[] = {
  211. {0x00, 0x0001, 0x2501},
  212. {0x20, 0x0500, 0x0001}, /* snapshot mode */
  213. {0x00, 0x0003, 0x2880},
  214. {0x00, 0x0001, 0x2881},
  215. };
  216. /* Initialisation data for the logitech clicksmart 420 */
  217. static const struct cmd spca504A_clicksmart420_init_data[] = {
  218. /* {0xa0, 0x0000, 0x0503}, * capture mode */
  219. {0x00, 0x0000, 0x2000},
  220. {0x00, 0x0013, 0x2301},
  221. {0x00, 0x0003, 0x2000},
  222. {0x00, 0x0001, 0x21ac},
  223. {0x00, 0x0001, 0x21a6},
  224. {0x00, 0x0000, 0x21a7}, /* brightness */
  225. {0x00, 0x0020, 0x21a8}, /* contrast */
  226. {0x00, 0x0001, 0x21ac}, /* sat/hue */
  227. {0x00, 0x0000, 0x21ad}, /* hue */
  228. {0x00, 0x001a, 0x21ae}, /* saturation */
  229. {0x00, 0x0002, 0x21a3}, /* gamma */
  230. {0x30, 0x0004, 0x000a},
  231. {0xb0, 0x0001, 0x0000},
  232. {0xa1, 0x0080, 0x0001},
  233. {0x30, 0x0049, 0x0000},
  234. {0x30, 0x0060, 0x0005},
  235. {0x0c, 0x0004, 0x0000},
  236. {0x00, 0x0000, 0x0000},
  237. {0x00, 0x0000, 0x2000},
  238. {0x00, 0x0013, 0x2301},
  239. {0x00, 0x0003, 0x2000},
  240. {0x00, 0x0000, 0x2000},
  241. };
  242. /* clicksmart 420 open data ? */
  243. static const struct cmd spca504A_clicksmart420_open_data[] = {
  244. {0x00, 0x0001, 0x2501},
  245. {0x20, 0x0502, 0x0000},
  246. {0x06, 0x0000, 0x0000},
  247. {0x00, 0x0004, 0x2880},
  248. {0x00, 0x0001, 0x2881},
  249. /* look like setting a qTable */
  250. {0x00, 0x0006, 0x2800},
  251. {0x00, 0x0004, 0x2801},
  252. {0x00, 0x0004, 0x2802},
  253. {0x00, 0x0006, 0x2803},
  254. {0x00, 0x000a, 0x2804},
  255. {0x00, 0x0010, 0x2805},
  256. {0x00, 0x0014, 0x2806},
  257. {0x00, 0x0018, 0x2807},
  258. {0x00, 0x0005, 0x2808},
  259. {0x00, 0x0005, 0x2809},
  260. {0x00, 0x0006, 0x280a},
  261. {0x00, 0x0008, 0x280b},
  262. {0x00, 0x000a, 0x280c},
  263. {0x00, 0x0017, 0x280d},
  264. {0x00, 0x0018, 0x280e},
  265. {0x00, 0x0016, 0x280f},
  266. {0x00, 0x0006, 0x2810},
  267. {0x00, 0x0005, 0x2811},
  268. {0x00, 0x0006, 0x2812},
  269. {0x00, 0x000a, 0x2813},
  270. {0x00, 0x0010, 0x2814},
  271. {0x00, 0x0017, 0x2815},
  272. {0x00, 0x001c, 0x2816},
  273. {0x00, 0x0016, 0x2817},
  274. {0x00, 0x0006, 0x2818},
  275. {0x00, 0x0007, 0x2819},
  276. {0x00, 0x0009, 0x281a},
  277. {0x00, 0x000c, 0x281b},
  278. {0x00, 0x0014, 0x281c},
  279. {0x00, 0x0023, 0x281d},
  280. {0x00, 0x0020, 0x281e},
  281. {0x00, 0x0019, 0x281f},
  282. {0x00, 0x0007, 0x2820},
  283. {0x00, 0x0009, 0x2821},
  284. {0x00, 0x000f, 0x2822},
  285. {0x00, 0x0016, 0x2823},
  286. {0x00, 0x001b, 0x2824},
  287. {0x00, 0x002c, 0x2825},
  288. {0x00, 0x0029, 0x2826},
  289. {0x00, 0x001f, 0x2827},
  290. {0x00, 0x000a, 0x2828},
  291. {0x00, 0x000e, 0x2829},
  292. {0x00, 0x0016, 0x282a},
  293. {0x00, 0x001a, 0x282b},
  294. {0x00, 0x0020, 0x282c},
  295. {0x00, 0x002a, 0x282d},
  296. {0x00, 0x002d, 0x282e},
  297. {0x00, 0x0025, 0x282f},
  298. {0x00, 0x0014, 0x2830},
  299. {0x00, 0x001a, 0x2831},
  300. {0x00, 0x001f, 0x2832},
  301. {0x00, 0x0023, 0x2833},
  302. {0x00, 0x0029, 0x2834},
  303. {0x00, 0x0030, 0x2835},
  304. {0x00, 0x0030, 0x2836},
  305. {0x00, 0x0028, 0x2837},
  306. {0x00, 0x001d, 0x2838},
  307. {0x00, 0x0025, 0x2839},
  308. {0x00, 0x0026, 0x283a},
  309. {0x00, 0x0027, 0x283b},
  310. {0x00, 0x002d, 0x283c},
  311. {0x00, 0x0028, 0x283d},
  312. {0x00, 0x0029, 0x283e},
  313. {0x00, 0x0028, 0x283f},
  314. {0x00, 0x0007, 0x2840},
  315. {0x00, 0x0007, 0x2841},
  316. {0x00, 0x000a, 0x2842},
  317. {0x00, 0x0013, 0x2843},
  318. {0x00, 0x0028, 0x2844},
  319. {0x00, 0x0028, 0x2845},
  320. {0x00, 0x0028, 0x2846},
  321. {0x00, 0x0028, 0x2847},
  322. {0x00, 0x0007, 0x2848},
  323. {0x00, 0x0008, 0x2849},
  324. {0x00, 0x000a, 0x284a},
  325. {0x00, 0x001a, 0x284b},
  326. {0x00, 0x0028, 0x284c},
  327. {0x00, 0x0028, 0x284d},
  328. {0x00, 0x0028, 0x284e},
  329. {0x00, 0x0028, 0x284f},
  330. {0x00, 0x000a, 0x2850},
  331. {0x00, 0x000a, 0x2851},
  332. {0x00, 0x0016, 0x2852},
  333. {0x00, 0x0028, 0x2853},
  334. {0x00, 0x0028, 0x2854},
  335. {0x00, 0x0028, 0x2855},
  336. {0x00, 0x0028, 0x2856},
  337. {0x00, 0x0028, 0x2857},
  338. {0x00, 0x0013, 0x2858},
  339. {0x00, 0x001a, 0x2859},
  340. {0x00, 0x0028, 0x285a},
  341. {0x00, 0x0028, 0x285b},
  342. {0x00, 0x0028, 0x285c},
  343. {0x00, 0x0028, 0x285d},
  344. {0x00, 0x0028, 0x285e},
  345. {0x00, 0x0028, 0x285f},
  346. {0x00, 0x0028, 0x2860},
  347. {0x00, 0x0028, 0x2861},
  348. {0x00, 0x0028, 0x2862},
  349. {0x00, 0x0028, 0x2863},
  350. {0x00, 0x0028, 0x2864},
  351. {0x00, 0x0028, 0x2865},
  352. {0x00, 0x0028, 0x2866},
  353. {0x00, 0x0028, 0x2867},
  354. {0x00, 0x0028, 0x2868},
  355. {0x00, 0x0028, 0x2869},
  356. {0x00, 0x0028, 0x286a},
  357. {0x00, 0x0028, 0x286b},
  358. {0x00, 0x0028, 0x286c},
  359. {0x00, 0x0028, 0x286d},
  360. {0x00, 0x0028, 0x286e},
  361. {0x00, 0x0028, 0x286f},
  362. {0x00, 0x0028, 0x2870},
  363. {0x00, 0x0028, 0x2871},
  364. {0x00, 0x0028, 0x2872},
  365. {0x00, 0x0028, 0x2873},
  366. {0x00, 0x0028, 0x2874},
  367. {0x00, 0x0028, 0x2875},
  368. {0x00, 0x0028, 0x2876},
  369. {0x00, 0x0028, 0x2877},
  370. {0x00, 0x0028, 0x2878},
  371. {0x00, 0x0028, 0x2879},
  372. {0x00, 0x0028, 0x287a},
  373. {0x00, 0x0028, 0x287b},
  374. {0x00, 0x0028, 0x287c},
  375. {0x00, 0x0028, 0x287d},
  376. {0x00, 0x0028, 0x287e},
  377. {0x00, 0x0028, 0x287f},
  378. {0xa0, 0x0000, 0x0503},
  379. };
  380. static const u8 qtable_creative_pccam[2][64] = {
  381. { /* Q-table Y-components */
  382. 0x05, 0x03, 0x03, 0x05, 0x07, 0x0c, 0x0f, 0x12,
  383. 0x04, 0x04, 0x04, 0x06, 0x08, 0x11, 0x12, 0x11,
  384. 0x04, 0x04, 0x05, 0x07, 0x0c, 0x11, 0x15, 0x11,
  385. 0x04, 0x05, 0x07, 0x09, 0x0f, 0x1a, 0x18, 0x13,
  386. 0x05, 0x07, 0x0b, 0x11, 0x14, 0x21, 0x1f, 0x17,
  387. 0x07, 0x0b, 0x11, 0x13, 0x18, 0x1f, 0x22, 0x1c,
  388. 0x0f, 0x13, 0x17, 0x1a, 0x1f, 0x24, 0x24, 0x1e,
  389. 0x16, 0x1c, 0x1d, 0x1d, 0x22, 0x1e, 0x1f, 0x1e},
  390. { /* Q-table C-components */
  391. 0x05, 0x05, 0x07, 0x0e, 0x1e, 0x1e, 0x1e, 0x1e,
  392. 0x05, 0x06, 0x08, 0x14, 0x1e, 0x1e, 0x1e, 0x1e,
  393. 0x07, 0x08, 0x11, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e,
  394. 0x0e, 0x14, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e,
  395. 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e,
  396. 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e,
  397. 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e,
  398. 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e}
  399. };
  400. /* FIXME: This Q-table is identical to the Creative PC-CAM one,
  401. * except for one byte. Possibly a typo?
  402. * NWG: 18/05/2003.
  403. */
  404. static const u8 qtable_spca504_default[2][64] = {
  405. { /* Q-table Y-components */
  406. 0x05, 0x03, 0x03, 0x05, 0x07, 0x0c, 0x0f, 0x12,
  407. 0x04, 0x04, 0x04, 0x06, 0x08, 0x11, 0x12, 0x11,
  408. 0x04, 0x04, 0x05, 0x07, 0x0c, 0x11, 0x15, 0x11,
  409. 0x04, 0x05, 0x07, 0x09, 0x0f, 0x1a, 0x18, 0x13,
  410. 0x05, 0x07, 0x0b, 0x11, 0x14, 0x21, 0x1f, 0x17,
  411. 0x07, 0x0b, 0x11, 0x13, 0x18, 0x1f, 0x22, 0x1c,
  412. 0x0f, 0x13, 0x17, 0x1a, 0x1f, 0x24, 0x24, 0x1e,
  413. 0x16, 0x1c, 0x1d, 0x1d, 0x1d /* 0x22 */ , 0x1e, 0x1f, 0x1e,
  414. },
  415. { /* Q-table C-components */
  416. 0x05, 0x05, 0x07, 0x0e, 0x1e, 0x1e, 0x1e, 0x1e,
  417. 0x05, 0x06, 0x08, 0x14, 0x1e, 0x1e, 0x1e, 0x1e,
  418. 0x07, 0x08, 0x11, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e,
  419. 0x0e, 0x14, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e,
  420. 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e,
  421. 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e,
  422. 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e,
  423. 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e}
  424. };
  425. /* read <len> bytes to gspca_dev->usb_buf */
  426. static void reg_r(struct gspca_dev *gspca_dev,
  427. u8 req,
  428. u16 index,
  429. u16 len)
  430. {
  431. #ifdef GSPCA_DEBUG
  432. if (len > USB_BUF_SZ) {
  433. err("reg_r: buffer overflow");
  434. return;
  435. }
  436. #endif
  437. usb_control_msg(gspca_dev->dev,
  438. usb_rcvctrlpipe(gspca_dev->dev, 0),
  439. req,
  440. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  441. 0, /* value */
  442. index,
  443. len ? gspca_dev->usb_buf : NULL, len,
  444. 500);
  445. }
  446. /* write one byte */
  447. static void reg_w_1(struct gspca_dev *gspca_dev,
  448. u8 req,
  449. u16 value,
  450. u16 index,
  451. u16 byte)
  452. {
  453. gspca_dev->usb_buf[0] = byte;
  454. usb_control_msg(gspca_dev->dev,
  455. usb_sndctrlpipe(gspca_dev->dev, 0),
  456. req,
  457. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  458. value, index,
  459. gspca_dev->usb_buf, 1,
  460. 500);
  461. }
  462. /* write req / index / value */
  463. static int reg_w_riv(struct usb_device *dev,
  464. u8 req, u16 index, u16 value)
  465. {
  466. int ret;
  467. ret = usb_control_msg(dev,
  468. usb_sndctrlpipe(dev, 0),
  469. req,
  470. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  471. value, index, NULL, 0, 500);
  472. PDEBUG(D_USBO, "reg write: 0x%02x,0x%02x:0x%02x, %d",
  473. req, index, value, ret);
  474. if (ret < 0)
  475. PDEBUG(D_ERR, "reg write: error %d", ret);
  476. return ret;
  477. }
  478. /* read 1 byte */
  479. static int reg_r_1(struct gspca_dev *gspca_dev,
  480. u16 value) /* wValue */
  481. {
  482. int ret;
  483. ret = usb_control_msg(gspca_dev->dev,
  484. usb_rcvctrlpipe(gspca_dev->dev, 0),
  485. 0x20, /* request */
  486. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  487. value,
  488. 0, /* index */
  489. gspca_dev->usb_buf, 1,
  490. 500); /* timeout */
  491. if (ret < 0) {
  492. PDEBUG(D_ERR, "reg_r_1 err %d", ret);
  493. return 0;
  494. }
  495. return gspca_dev->usb_buf[0];
  496. }
  497. /* read 1 or 2 bytes - returns < 0 if error */
  498. static int reg_r_12(struct gspca_dev *gspca_dev,
  499. u8 req, /* bRequest */
  500. u16 index, /* wIndex */
  501. u16 length) /* wLength (1 or 2 only) */
  502. {
  503. int ret;
  504. gspca_dev->usb_buf[1] = 0;
  505. ret = usb_control_msg(gspca_dev->dev,
  506. usb_rcvctrlpipe(gspca_dev->dev, 0),
  507. req,
  508. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  509. 0, /* value */
  510. index,
  511. gspca_dev->usb_buf, length,
  512. 500);
  513. if (ret < 0) {
  514. PDEBUG(D_ERR, "reg_read err %d", ret);
  515. return -1;
  516. }
  517. return (gspca_dev->usb_buf[1] << 8) + gspca_dev->usb_buf[0];
  518. }
  519. static int write_vector(struct gspca_dev *gspca_dev,
  520. const struct cmd *data, int ncmds)
  521. {
  522. struct usb_device *dev = gspca_dev->dev;
  523. int ret;
  524. while (--ncmds >= 0) {
  525. ret = reg_w_riv(dev, data->req, data->idx, data->val);
  526. if (ret < 0) {
  527. PDEBUG(D_ERR,
  528. "Register write failed for 0x%02x, 0x%04x, 0x%04x",
  529. data->req, data->val, data->idx);
  530. return ret;
  531. }
  532. data++;
  533. }
  534. return 0;
  535. }
  536. static int spca50x_setup_qtable(struct gspca_dev *gspca_dev,
  537. const u8 qtable[2][64])
  538. {
  539. struct usb_device *dev = gspca_dev->dev;
  540. int i, err;
  541. /* loop over y components */
  542. for (i = 0; i < 64; i++) {
  543. err = reg_w_riv(dev, 0x00, 0x2800 + i, qtable[0][i]);
  544. if (err < 0)
  545. return err;
  546. }
  547. /* loop over c components */
  548. for (i = 0; i < 64; i++) {
  549. err = reg_w_riv(dev, 0x00, 0x2840 + i, qtable[1][i]);
  550. if (err < 0)
  551. return err;
  552. }
  553. return 0;
  554. }
  555. static void spca504_acknowledged_command(struct gspca_dev *gspca_dev,
  556. u8 req, u16 idx, u16 val)
  557. {
  558. struct usb_device *dev = gspca_dev->dev;
  559. int notdone;
  560. reg_w_riv(dev, req, idx, val);
  561. notdone = reg_r_12(gspca_dev, 0x01, 0x0001, 1);
  562. reg_w_riv(dev, req, idx, val);
  563. PDEBUG(D_FRAM, "before wait 0x%04x", notdone);
  564. msleep(200);
  565. notdone = reg_r_12(gspca_dev, 0x01, 0x0001, 1);
  566. PDEBUG(D_FRAM, "after wait 0x%04x", notdone);
  567. }
  568. static void spca504A_acknowledged_command(struct gspca_dev *gspca_dev,
  569. u8 req,
  570. u16 idx, u16 val, u8 stat, u8 count)
  571. {
  572. struct usb_device *dev = gspca_dev->dev;
  573. int status;
  574. u8 endcode;
  575. reg_w_riv(dev, req, idx, val);
  576. status = reg_r_12(gspca_dev, 0x01, 0x0001, 1);
  577. endcode = stat;
  578. PDEBUG(D_FRAM, "Status 0x%x Need 0x%04x", status, stat);
  579. if (!count)
  580. return;
  581. count = 200;
  582. while (--count > 0) {
  583. msleep(10);
  584. /* gsmart mini2 write a each wait setting 1 ms is enough */
  585. /* reg_w_riv(dev, req, idx, val); */
  586. status = reg_r_12(gspca_dev, 0x01, 0x0001, 1);
  587. if (status == endcode) {
  588. PDEBUG(D_FRAM, "status 0x%04x after wait %d",
  589. status, 200 - count);
  590. break;
  591. }
  592. }
  593. }
  594. static int spca504B_PollingDataReady(struct gspca_dev *gspca_dev)
  595. {
  596. int count = 10;
  597. while (--count > 0) {
  598. reg_r(gspca_dev, 0x21, 0, 1);
  599. if ((gspca_dev->usb_buf[0] & 0x01) == 0)
  600. break;
  601. msleep(10);
  602. }
  603. return gspca_dev->usb_buf[0];
  604. }
  605. static void spca504B_WaitCmdStatus(struct gspca_dev *gspca_dev)
  606. {
  607. int count = 50;
  608. while (--count > 0) {
  609. reg_r(gspca_dev, 0x21, 1, 1);
  610. if (gspca_dev->usb_buf[0] != 0) {
  611. reg_w_1(gspca_dev, 0x21, 0, 1, 0);
  612. reg_r(gspca_dev, 0x21, 1, 1);
  613. spca504B_PollingDataReady(gspca_dev);
  614. break;
  615. }
  616. msleep(10);
  617. }
  618. }
  619. static void spca50x_GetFirmware(struct gspca_dev *gspca_dev)
  620. {
  621. u8 *data;
  622. data = gspca_dev->usb_buf;
  623. reg_r(gspca_dev, 0x20, 0, 5);
  624. PDEBUG(D_STREAM, "FirmWare : %d %d %d %d %d ",
  625. data[0], data[1], data[2], data[3], data[4]);
  626. reg_r(gspca_dev, 0x23, 0, 64);
  627. reg_r(gspca_dev, 0x23, 1, 64);
  628. }
  629. static void spca504B_SetSizeType(struct gspca_dev *gspca_dev)
  630. {
  631. struct sd *sd = (struct sd *) gspca_dev;
  632. struct usb_device *dev = gspca_dev->dev;
  633. u8 Size;
  634. int rc;
  635. Size = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
  636. switch (sd->bridge) {
  637. case BRIDGE_SPCA533:
  638. reg_w_riv(dev, 0x31, 0, 0);
  639. spca504B_WaitCmdStatus(gspca_dev);
  640. rc = spca504B_PollingDataReady(gspca_dev);
  641. spca50x_GetFirmware(gspca_dev);
  642. reg_w_1(gspca_dev, 0x24, 0, 8, 2); /* type */
  643. reg_r(gspca_dev, 0x24, 8, 1);
  644. reg_w_1(gspca_dev, 0x25, 0, 4, Size);
  645. reg_r(gspca_dev, 0x25, 4, 1); /* size */
  646. rc = spca504B_PollingDataReady(gspca_dev);
  647. /* Init the cam width height with some values get on init ? */
  648. reg_w_riv(dev, 0x31, 0, 0x04);
  649. spca504B_WaitCmdStatus(gspca_dev);
  650. rc = spca504B_PollingDataReady(gspca_dev);
  651. break;
  652. default:
  653. /* case BRIDGE_SPCA504B: */
  654. /* case BRIDGE_SPCA536: */
  655. reg_w_1(gspca_dev, 0x25, 0, 4, Size);
  656. reg_r(gspca_dev, 0x25, 4, 1); /* size */
  657. reg_w_1(gspca_dev, 0x27, 0, 0, 6);
  658. reg_r(gspca_dev, 0x27, 0, 1); /* type */
  659. rc = spca504B_PollingDataReady(gspca_dev);
  660. break;
  661. case BRIDGE_SPCA504:
  662. Size += 3;
  663. if (sd->subtype == AiptekMiniPenCam13) {
  664. /* spca504a aiptek */
  665. spca504A_acknowledged_command(gspca_dev,
  666. 0x08, Size, 0,
  667. 0x80 | (Size & 0x0f), 1);
  668. spca504A_acknowledged_command(gspca_dev,
  669. 1, 3, 0, 0x9f, 0);
  670. } else {
  671. spca504_acknowledged_command(gspca_dev, 0x08, Size, 0);
  672. }
  673. break;
  674. case BRIDGE_SPCA504C:
  675. /* capture mode */
  676. reg_w_riv(dev, 0xa0, (0x0500 | (Size & 0x0f)), 0x00);
  677. reg_w_riv(dev, 0x20, 0x01, 0x0500 | (Size & 0x0f));
  678. break;
  679. }
  680. }
  681. static void spca504_wait_status(struct gspca_dev *gspca_dev)
  682. {
  683. int cnt;
  684. cnt = 256;
  685. while (--cnt > 0) {
  686. /* With this we get the status, when return 0 it's all ok */
  687. if (reg_r_12(gspca_dev, 0x06, 0x00, 1) == 0)
  688. return;
  689. msleep(10);
  690. }
  691. }
  692. static void spca504B_setQtable(struct gspca_dev *gspca_dev)
  693. {
  694. reg_w_1(gspca_dev, 0x26, 0, 0, 3);
  695. reg_r(gspca_dev, 0x26, 0, 1);
  696. spca504B_PollingDataReady(gspca_dev);
  697. }
  698. static void setbrightness(struct gspca_dev *gspca_dev)
  699. {
  700. struct sd *sd = (struct sd *) gspca_dev;
  701. struct usb_device *dev = gspca_dev->dev;
  702. u16 reg;
  703. reg = sd->bridge == BRIDGE_SPCA536 ? 0x20f0 : 0x21a7;
  704. reg_w_riv(dev, 0x00, reg, sd->brightness);
  705. }
  706. static void setcontrast(struct gspca_dev *gspca_dev)
  707. {
  708. struct sd *sd = (struct sd *) gspca_dev;
  709. struct usb_device *dev = gspca_dev->dev;
  710. u16 reg;
  711. reg = sd->bridge == BRIDGE_SPCA536 ? 0x20f1 : 0x21a8;
  712. reg_w_riv(dev, 0x00, reg, sd->contrast);
  713. }
  714. static void setcolors(struct gspca_dev *gspca_dev)
  715. {
  716. struct sd *sd = (struct sd *) gspca_dev;
  717. struct usb_device *dev = gspca_dev->dev;
  718. u16 reg;
  719. reg = sd->bridge == BRIDGE_SPCA536 ? 0x20f6 : 0x21ae;
  720. reg_w_riv(dev, 0x00, reg, sd->colors);
  721. }
  722. static void init_ctl_reg(struct gspca_dev *gspca_dev)
  723. {
  724. struct sd *sd = (struct sd *) gspca_dev;
  725. struct usb_device *dev = gspca_dev->dev;
  726. int pollreg = 1;
  727. setbrightness(gspca_dev);
  728. setcontrast(gspca_dev);
  729. setcolors(gspca_dev);
  730. switch (sd->bridge) {
  731. case BRIDGE_SPCA504:
  732. case BRIDGE_SPCA504C:
  733. pollreg = 0;
  734. /* fall thru */
  735. default:
  736. /* case BRIDGE_SPCA533: */
  737. /* case BRIDGE_SPCA504B: */
  738. reg_w_riv(dev, 0, 0x00, 0x21ad); /* hue */
  739. reg_w_riv(dev, 0, 0x01, 0x21ac); /* sat/hue */
  740. reg_w_riv(dev, 0, 0x00, 0x21a3); /* gamma */
  741. break;
  742. case BRIDGE_SPCA536:
  743. reg_w_riv(dev, 0, 0x40, 0x20f5);
  744. reg_w_riv(dev, 0, 0x01, 0x20f4);
  745. reg_w_riv(dev, 0, 0x00, 0x2089);
  746. break;
  747. }
  748. if (pollreg)
  749. spca504B_PollingDataReady(gspca_dev);
  750. }
  751. /* this function is called at probe time */
  752. static int sd_config(struct gspca_dev *gspca_dev,
  753. const struct usb_device_id *id)
  754. {
  755. struct sd *sd = (struct sd *) gspca_dev;
  756. struct cam *cam;
  757. cam = &gspca_dev->cam;
  758. sd->bridge = id->driver_info >> 8;
  759. sd->subtype = id->driver_info;
  760. if (sd->subtype == AiptekMiniPenCam13) {
  761. /* try to get the firmware as some cam answer 2.0.1.2.2
  762. * and should be a spca504b then overwrite that setting */
  763. reg_r(gspca_dev, 0x20, 0, 1);
  764. switch (gspca_dev->usb_buf[0]) {
  765. case 1:
  766. break; /* (right bridge/subtype) */
  767. case 2:
  768. sd->bridge = BRIDGE_SPCA504B;
  769. sd->subtype = 0;
  770. break;
  771. default:
  772. return -ENODEV;
  773. }
  774. }
  775. switch (sd->bridge) {
  776. default:
  777. /* case BRIDGE_SPCA504B: */
  778. /* case BRIDGE_SPCA504: */
  779. /* case BRIDGE_SPCA536: */
  780. cam->cam_mode = vga_mode;
  781. cam->nmodes =ARRAY_SIZE(vga_mode);
  782. break;
  783. case BRIDGE_SPCA533:
  784. cam->cam_mode = custom_mode;
  785. if (sd->subtype == MegaImageVI) /* 320x240 only */
  786. cam->nmodes = ARRAY_SIZE(custom_mode) - 1;
  787. else
  788. cam->nmodes = ARRAY_SIZE(custom_mode);
  789. break;
  790. case BRIDGE_SPCA504C:
  791. cam->cam_mode = vga_mode2;
  792. cam->nmodes = ARRAY_SIZE(vga_mode2);
  793. break;
  794. }
  795. sd->brightness = BRIGHTNESS_DEF;
  796. sd->contrast = CONTRAST_DEF;
  797. sd->colors = COLOR_DEF;
  798. sd->autogain = AUTOGAIN_DEF;
  799. sd->quality = QUALITY_DEF;
  800. return 0;
  801. }
  802. /* this function is called at probe and resume time */
  803. static int sd_init(struct gspca_dev *gspca_dev)
  804. {
  805. struct sd *sd = (struct sd *) gspca_dev;
  806. struct usb_device *dev = gspca_dev->dev;
  807. int i, err_code;
  808. u8 info[6];
  809. switch (sd->bridge) {
  810. case BRIDGE_SPCA504B:
  811. reg_w_riv(dev, 0x1d, 0x00, 0);
  812. reg_w_riv(dev, 0, 0x01, 0x2306);
  813. reg_w_riv(dev, 0, 0x00, 0x0d04);
  814. reg_w_riv(dev, 0, 0x00, 0x2000);
  815. reg_w_riv(dev, 0, 0x13, 0x2301);
  816. reg_w_riv(dev, 0, 0x00, 0x2306);
  817. /* fall thru */
  818. case BRIDGE_SPCA533:
  819. spca504B_PollingDataReady(gspca_dev);
  820. spca50x_GetFirmware(gspca_dev);
  821. break;
  822. case BRIDGE_SPCA536:
  823. spca50x_GetFirmware(gspca_dev);
  824. reg_r(gspca_dev, 0x00, 0x5002, 1);
  825. reg_w_1(gspca_dev, 0x24, 0, 0, 0);
  826. reg_r(gspca_dev, 0x24, 0, 1);
  827. spca504B_PollingDataReady(gspca_dev);
  828. reg_w_riv(dev, 0x34, 0, 0);
  829. spca504B_WaitCmdStatus(gspca_dev);
  830. break;
  831. case BRIDGE_SPCA504C: /* pccam600 */
  832. PDEBUG(D_STREAM, "Opening SPCA504 (PC-CAM 600)");
  833. reg_w_riv(dev, 0xe0, 0x0000, 0x0000);
  834. reg_w_riv(dev, 0xe0, 0x0000, 0x0001); /* reset */
  835. spca504_wait_status(gspca_dev);
  836. if (sd->subtype == LogitechClickSmart420)
  837. write_vector(gspca_dev,
  838. spca504A_clicksmart420_open_data,
  839. ARRAY_SIZE(spca504A_clicksmart420_open_data));
  840. else
  841. write_vector(gspca_dev, spca504_pccam600_open_data,
  842. ARRAY_SIZE(spca504_pccam600_open_data));
  843. err_code = spca50x_setup_qtable(gspca_dev,
  844. qtable_creative_pccam);
  845. if (err_code < 0) {
  846. PDEBUG(D_ERR|D_STREAM, "spca50x_setup_qtable failed");
  847. return err_code;
  848. }
  849. break;
  850. default:
  851. /* case BRIDGE_SPCA504: */
  852. PDEBUG(D_STREAM, "Opening SPCA504");
  853. if (sd->subtype == AiptekMiniPenCam13) {
  854. /*****************************/
  855. for (i = 0; i < 6; i++)
  856. info[i] = reg_r_1(gspca_dev, i);
  857. PDEBUG(D_STREAM,
  858. "Read info: %d %d %d %d %d %d."
  859. " Should be 1,0,2,2,0,0",
  860. info[0], info[1], info[2],
  861. info[3], info[4], info[5]);
  862. /* spca504a aiptek */
  863. /* Set AE AWB Banding Type 3-> 50Hz 2-> 60Hz */
  864. spca504A_acknowledged_command(gspca_dev, 0x24,
  865. 8, 3, 0x9e, 1);
  866. /* Twice sequencial need status 0xff->0x9e->0x9d */
  867. spca504A_acknowledged_command(gspca_dev, 0x24,
  868. 8, 3, 0x9e, 0);
  869. spca504A_acknowledged_command(gspca_dev, 0x24,
  870. 0, 0, 0x9d, 1);
  871. /******************************/
  872. /* spca504a aiptek */
  873. spca504A_acknowledged_command(gspca_dev, 0x08,
  874. 6, 0, 0x86, 1);
  875. /* reg_write (dev, 0, 0x2000, 0); */
  876. /* reg_write (dev, 0, 0x2883, 1); */
  877. /* spca504A_acknowledged_command (gspca_dev, 0x08,
  878. 6, 0, 0x86, 1); */
  879. /* spca504A_acknowledged_command (gspca_dev, 0x24,
  880. 0, 0, 0x9D, 1); */
  881. reg_w_riv(dev, 0x00, 0x270c, 0x05); /* L92 sno1t.txt */
  882. reg_w_riv(dev, 0x00, 0x2310, 0x05);
  883. spca504A_acknowledged_command(gspca_dev, 0x01,
  884. 0x0f, 0, 0xff, 0);
  885. }
  886. /* setup qtable */
  887. reg_w_riv(dev, 0, 0x2000, 0);
  888. reg_w_riv(dev, 0, 0x2883, 1);
  889. err_code = spca50x_setup_qtable(gspca_dev,
  890. qtable_spca504_default);
  891. if (err_code < 0) {
  892. PDEBUG(D_ERR, "spca50x_setup_qtable failed");
  893. return err_code;
  894. }
  895. break;
  896. }
  897. return 0;
  898. }
  899. static int sd_start(struct gspca_dev *gspca_dev)
  900. {
  901. struct sd *sd = (struct sd *) gspca_dev;
  902. struct usb_device *dev = gspca_dev->dev;
  903. int enable;
  904. int i;
  905. u8 info[6];
  906. /* create the JPEG header */
  907. sd->jpeg_hdr = kmalloc(JPEG_HDR_SZ, GFP_KERNEL);
  908. if (!sd->jpeg_hdr)
  909. return -ENOMEM;
  910. jpeg_define(sd->jpeg_hdr, gspca_dev->height, gspca_dev->width,
  911. 0x22); /* JPEG 411 */
  912. jpeg_set_qual(sd->jpeg_hdr, sd->quality);
  913. if (sd->bridge == BRIDGE_SPCA504B)
  914. spca504B_setQtable(gspca_dev);
  915. spca504B_SetSizeType(gspca_dev);
  916. switch (sd->bridge) {
  917. default:
  918. /* case BRIDGE_SPCA504B: */
  919. /* case BRIDGE_SPCA533: */
  920. /* case BRIDGE_SPCA536: */
  921. switch (sd->subtype) {
  922. case MegapixV4:
  923. case LogitechClickSmart820:
  924. case MegaImageVI:
  925. reg_w_riv(dev, 0xf0, 0, 0);
  926. spca504B_WaitCmdStatus(gspca_dev);
  927. reg_r(gspca_dev, 0xf0, 4, 0);
  928. spca504B_WaitCmdStatus(gspca_dev);
  929. break;
  930. default:
  931. reg_w_riv(dev, 0x31, 0, 0x04);
  932. spca504B_WaitCmdStatus(gspca_dev);
  933. spca504B_PollingDataReady(gspca_dev);
  934. break;
  935. }
  936. break;
  937. case BRIDGE_SPCA504:
  938. if (sd->subtype == AiptekMiniPenCam13) {
  939. for (i = 0; i < 6; i++)
  940. info[i] = reg_r_1(gspca_dev, i);
  941. PDEBUG(D_STREAM,
  942. "Read info: %d %d %d %d %d %d."
  943. " Should be 1,0,2,2,0,0",
  944. info[0], info[1], info[2],
  945. info[3], info[4], info[5]);
  946. /* spca504a aiptek */
  947. /* Set AE AWB Banding Type 3-> 50Hz 2-> 60Hz */
  948. spca504A_acknowledged_command(gspca_dev, 0x24,
  949. 8, 3, 0x9e, 1);
  950. /* Twice sequencial need status 0xff->0x9e->0x9d */
  951. spca504A_acknowledged_command(gspca_dev, 0x24,
  952. 8, 3, 0x9e, 0);
  953. spca504A_acknowledged_command(gspca_dev, 0x24,
  954. 0, 0, 0x9d, 1);
  955. } else {
  956. spca504_acknowledged_command(gspca_dev, 0x24, 8, 3);
  957. for (i = 0; i < 6; i++)
  958. info[i] = reg_r_1(gspca_dev, i);
  959. PDEBUG(D_STREAM,
  960. "Read info: %d %d %d %d %d %d."
  961. " Should be 1,0,2,2,0,0",
  962. info[0], info[1], info[2],
  963. info[3], info[4], info[5]);
  964. spca504_acknowledged_command(gspca_dev, 0x24, 8, 3);
  965. spca504_acknowledged_command(gspca_dev, 0x24, 0, 0);
  966. }
  967. spca504B_SetSizeType(gspca_dev);
  968. reg_w_riv(dev, 0x00, 0x270c, 0x05); /* L92 sno1t.txt */
  969. reg_w_riv(dev, 0x00, 0x2310, 0x05);
  970. break;
  971. case BRIDGE_SPCA504C:
  972. if (sd->subtype == LogitechClickSmart420) {
  973. write_vector(gspca_dev,
  974. spca504A_clicksmart420_init_data,
  975. ARRAY_SIZE(spca504A_clicksmart420_init_data));
  976. } else {
  977. write_vector(gspca_dev, spca504_pccam600_init_data,
  978. ARRAY_SIZE(spca504_pccam600_init_data));
  979. }
  980. enable = (sd->autogain ? 0x04 : 0x01);
  981. reg_w_riv(dev, 0x0c, 0x0000, enable); /* auto exposure */
  982. reg_w_riv(dev, 0xb0, 0x0000, enable); /* auto whiteness */
  983. /* set default exposure compensation and whiteness balance */
  984. reg_w_riv(dev, 0x30, 0x0001, 800); /* ~ 20 fps */
  985. reg_w_riv(dev, 0x30, 0x0002, 1600);
  986. spca504B_SetSizeType(gspca_dev);
  987. break;
  988. }
  989. init_ctl_reg(gspca_dev);
  990. return 0;
  991. }
  992. static void sd_stopN(struct gspca_dev *gspca_dev)
  993. {
  994. struct sd *sd = (struct sd *) gspca_dev;
  995. struct usb_device *dev = gspca_dev->dev;
  996. switch (sd->bridge) {
  997. default:
  998. /* case BRIDGE_SPCA533: */
  999. /* case BRIDGE_SPCA536: */
  1000. /* case BRIDGE_SPCA504B: */
  1001. reg_w_riv(dev, 0x31, 0, 0);
  1002. spca504B_WaitCmdStatus(gspca_dev);
  1003. spca504B_PollingDataReady(gspca_dev);
  1004. break;
  1005. case BRIDGE_SPCA504:
  1006. case BRIDGE_SPCA504C:
  1007. reg_w_riv(dev, 0x00, 0x2000, 0x0000);
  1008. if (sd->subtype == AiptekMiniPenCam13) {
  1009. /* spca504a aiptek */
  1010. /* spca504A_acknowledged_command(gspca_dev, 0x08,
  1011. 6, 0, 0x86, 1); */
  1012. spca504A_acknowledged_command(gspca_dev, 0x24,
  1013. 0x00, 0x00, 0x9d, 1);
  1014. spca504A_acknowledged_command(gspca_dev, 0x01,
  1015. 0x0f, 0x00, 0xff, 1);
  1016. } else {
  1017. spca504_acknowledged_command(gspca_dev, 0x24, 0, 0);
  1018. reg_w_riv(dev, 0x01, 0x000f, 0x0000);
  1019. }
  1020. break;
  1021. }
  1022. }
  1023. static void sd_stop0(struct gspca_dev *gspca_dev)
  1024. {
  1025. struct sd *sd = (struct sd *) gspca_dev;
  1026. kfree(sd->jpeg_hdr);
  1027. }
  1028. static void sd_pkt_scan(struct gspca_dev *gspca_dev,
  1029. u8 *data, /* isoc packet */
  1030. int len) /* iso packet length */
  1031. {
  1032. struct sd *sd = (struct sd *) gspca_dev;
  1033. int i, sof = 0;
  1034. static u8 ffd9[] = {0xff, 0xd9};
  1035. /* frames are jpeg 4.1.1 without 0xff escape */
  1036. switch (sd->bridge) {
  1037. case BRIDGE_SPCA533:
  1038. if (data[0] == 0xff) {
  1039. if (data[1] != 0x01) { /* drop packet */
  1040. /* gspca_dev->last_packet_type = DISCARD_PACKET; */
  1041. return;
  1042. }
  1043. sof = 1;
  1044. data += SPCA533_OFFSET_DATA;
  1045. len -= SPCA533_OFFSET_DATA;
  1046. } else {
  1047. data += 1;
  1048. len -= 1;
  1049. }
  1050. break;
  1051. case BRIDGE_SPCA536:
  1052. if (data[0] == 0xff) {
  1053. sof = 1;
  1054. data += SPCA536_OFFSET_DATA;
  1055. len -= SPCA536_OFFSET_DATA;
  1056. } else {
  1057. data += 2;
  1058. len -= 2;
  1059. }
  1060. break;
  1061. default:
  1062. /* case BRIDGE_SPCA504: */
  1063. /* case BRIDGE_SPCA504B: */
  1064. switch (data[0]) {
  1065. case 0xfe: /* start of frame */
  1066. sof = 1;
  1067. data += SPCA50X_OFFSET_DATA;
  1068. len -= SPCA50X_OFFSET_DATA;
  1069. break;
  1070. case 0xff: /* drop packet */
  1071. /* gspca_dev->last_packet_type = DISCARD_PACKET; */
  1072. return;
  1073. default:
  1074. data += 1;
  1075. len -= 1;
  1076. break;
  1077. }
  1078. break;
  1079. case BRIDGE_SPCA504C:
  1080. switch (data[0]) {
  1081. case 0xfe: /* start of frame */
  1082. sof = 1;
  1083. data += SPCA504_PCCAM600_OFFSET_DATA;
  1084. len -= SPCA504_PCCAM600_OFFSET_DATA;
  1085. break;
  1086. case 0xff: /* drop packet */
  1087. /* gspca_dev->last_packet_type = DISCARD_PACKET; */
  1088. return;
  1089. default:
  1090. data += 1;
  1091. len -= 1;
  1092. break;
  1093. }
  1094. break;
  1095. }
  1096. if (sof) { /* start of frame */
  1097. gspca_frame_add(gspca_dev, LAST_PACKET,
  1098. ffd9, 2);
  1099. /* put the JPEG header in the new frame */
  1100. gspca_frame_add(gspca_dev, FIRST_PACKET,
  1101. sd->jpeg_hdr, JPEG_HDR_SZ);
  1102. }
  1103. /* add 0x00 after 0xff */
  1104. i = 0;
  1105. do {
  1106. if (data[i] == 0xff) {
  1107. gspca_frame_add(gspca_dev, INTER_PACKET,
  1108. data, i + 1);
  1109. len -= i;
  1110. data += i;
  1111. *data = 0x00;
  1112. i = 0;
  1113. }
  1114. i++;
  1115. } while (i < len);
  1116. gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
  1117. }
  1118. static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
  1119. {
  1120. struct sd *sd = (struct sd *) gspca_dev;
  1121. sd->brightness = val;
  1122. if (gspca_dev->streaming)
  1123. setbrightness(gspca_dev);
  1124. return 0;
  1125. }
  1126. static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
  1127. {
  1128. struct sd *sd = (struct sd *) gspca_dev;
  1129. *val = sd->brightness;
  1130. return 0;
  1131. }
  1132. static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
  1133. {
  1134. struct sd *sd = (struct sd *) gspca_dev;
  1135. sd->contrast = val;
  1136. if (gspca_dev->streaming)
  1137. setcontrast(gspca_dev);
  1138. return 0;
  1139. }
  1140. static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val)
  1141. {
  1142. struct sd *sd = (struct sd *) gspca_dev;
  1143. *val = sd->contrast;
  1144. return 0;
  1145. }
  1146. static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val)
  1147. {
  1148. struct sd *sd = (struct sd *) gspca_dev;
  1149. sd->colors = val;
  1150. if (gspca_dev->streaming)
  1151. setcolors(gspca_dev);
  1152. return 0;
  1153. }
  1154. static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val)
  1155. {
  1156. struct sd *sd = (struct sd *) gspca_dev;
  1157. *val = sd->colors;
  1158. return 0;
  1159. }
  1160. static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val)
  1161. {
  1162. struct sd *sd = (struct sd *) gspca_dev;
  1163. sd->autogain = val;
  1164. return 0;
  1165. }
  1166. static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val)
  1167. {
  1168. struct sd *sd = (struct sd *) gspca_dev;
  1169. *val = sd->autogain;
  1170. return 0;
  1171. }
  1172. static int sd_set_jcomp(struct gspca_dev *gspca_dev,
  1173. struct v4l2_jpegcompression *jcomp)
  1174. {
  1175. struct sd *sd = (struct sd *) gspca_dev;
  1176. if (jcomp->quality < QUALITY_MIN)
  1177. sd->quality = QUALITY_MIN;
  1178. else if (jcomp->quality > QUALITY_MAX)
  1179. sd->quality = QUALITY_MAX;
  1180. else
  1181. sd->quality = jcomp->quality;
  1182. if (gspca_dev->streaming)
  1183. jpeg_set_qual(sd->jpeg_hdr, sd->quality);
  1184. return 0;
  1185. }
  1186. static int sd_get_jcomp(struct gspca_dev *gspca_dev,
  1187. struct v4l2_jpegcompression *jcomp)
  1188. {
  1189. struct sd *sd = (struct sd *) gspca_dev;
  1190. memset(jcomp, 0, sizeof *jcomp);
  1191. jcomp->quality = sd->quality;
  1192. jcomp->jpeg_markers = V4L2_JPEG_MARKER_DHT
  1193. | V4L2_JPEG_MARKER_DQT;
  1194. return 0;
  1195. }
  1196. /* sub-driver description */
  1197. static const struct sd_desc sd_desc = {
  1198. .name = MODULE_NAME,
  1199. .ctrls = sd_ctrls,
  1200. .nctrls = ARRAY_SIZE(sd_ctrls),
  1201. .config = sd_config,
  1202. .init = sd_init,
  1203. .start = sd_start,
  1204. .stopN = sd_stopN,
  1205. .stop0 = sd_stop0,
  1206. .pkt_scan = sd_pkt_scan,
  1207. .get_jcomp = sd_get_jcomp,
  1208. .set_jcomp = sd_set_jcomp,
  1209. };
  1210. /* -- module initialisation -- */
  1211. #define BS(bridge, subtype) \
  1212. .driver_info = (BRIDGE_ ## bridge << 8) \
  1213. | (subtype)
  1214. static const __devinitdata struct usb_device_id device_table[] = {
  1215. {USB_DEVICE(0x041e, 0x400b), BS(SPCA504C, 0)},
  1216. {USB_DEVICE(0x041e, 0x4012), BS(SPCA504C, 0)},
  1217. {USB_DEVICE(0x041e, 0x4013), BS(SPCA504C, 0)},
  1218. {USB_DEVICE(0x0458, 0x7006), BS(SPCA504B, 0)},
  1219. {USB_DEVICE(0x0461, 0x0821), BS(SPCA533, 0)},
  1220. {USB_DEVICE(0x046d, 0x0905), BS(SPCA533, LogitechClickSmart820)},
  1221. {USB_DEVICE(0x046d, 0x0960), BS(SPCA504C, LogitechClickSmart420)},
  1222. {USB_DEVICE(0x0471, 0x0322), BS(SPCA504B, 0)},
  1223. {USB_DEVICE(0x04a5, 0x3003), BS(SPCA504B, 0)},
  1224. {USB_DEVICE(0x04a5, 0x3008), BS(SPCA533, 0)},
  1225. {USB_DEVICE(0x04a5, 0x300a), BS(SPCA533, 0)},
  1226. {USB_DEVICE(0x04f1, 0x1001), BS(SPCA504B, 0)},
  1227. {USB_DEVICE(0x04fc, 0x500c), BS(SPCA504B, 0)},
  1228. {USB_DEVICE(0x04fc, 0x504a), BS(SPCA504, AiptekMiniPenCam13)},
  1229. {USB_DEVICE(0x04fc, 0x504b), BS(SPCA504B, 0)},
  1230. {USB_DEVICE(0x04fc, 0x5330), BS(SPCA533, 0)},
  1231. {USB_DEVICE(0x04fc, 0x5360), BS(SPCA536, 0)},
  1232. {USB_DEVICE(0x04fc, 0xffff), BS(SPCA504B, 0)},
  1233. {USB_DEVICE(0x052b, 0x1513), BS(SPCA533, MegapixV4)},
  1234. {USB_DEVICE(0x052b, 0x1803), BS(SPCA533, MegaImageVI)},
  1235. {USB_DEVICE(0x0546, 0x3155), BS(SPCA533, 0)},
  1236. {USB_DEVICE(0x0546, 0x3191), BS(SPCA504B, 0)},
  1237. {USB_DEVICE(0x0546, 0x3273), BS(SPCA504B, 0)},
  1238. {USB_DEVICE(0x055f, 0xc211), BS(SPCA536, 0)},
  1239. {USB_DEVICE(0x055f, 0xc230), BS(SPCA533, 0)},
  1240. {USB_DEVICE(0x055f, 0xc232), BS(SPCA533, 0)},
  1241. {USB_DEVICE(0x055f, 0xc360), BS(SPCA536, 0)},
  1242. {USB_DEVICE(0x055f, 0xc420), BS(SPCA504, 0)},
  1243. {USB_DEVICE(0x055f, 0xc430), BS(SPCA533, 0)},
  1244. {USB_DEVICE(0x055f, 0xc440), BS(SPCA533, 0)},
  1245. {USB_DEVICE(0x055f, 0xc520), BS(SPCA504, 0)},
  1246. {USB_DEVICE(0x055f, 0xc530), BS(SPCA533, 0)},
  1247. {USB_DEVICE(0x055f, 0xc540), BS(SPCA533, 0)},
  1248. {USB_DEVICE(0x055f, 0xc630), BS(SPCA533, 0)},
  1249. {USB_DEVICE(0x055f, 0xc650), BS(SPCA533, 0)},
  1250. {USB_DEVICE(0x05da, 0x1018), BS(SPCA504B, 0)},
  1251. {USB_DEVICE(0x06d6, 0x0031), BS(SPCA533, 0)},
  1252. {USB_DEVICE(0x0733, 0x1311), BS(SPCA533, 0)},
  1253. {USB_DEVICE(0x0733, 0x1314), BS(SPCA533, 0)},
  1254. {USB_DEVICE(0x0733, 0x2211), BS(SPCA533, 0)},
  1255. {USB_DEVICE(0x0733, 0x2221), BS(SPCA533, 0)},
  1256. {USB_DEVICE(0x0733, 0x3261), BS(SPCA536, 0)},
  1257. {USB_DEVICE(0x0733, 0x3281), BS(SPCA536, 0)},
  1258. {USB_DEVICE(0x08ca, 0x0104), BS(SPCA533, 0)},
  1259. {USB_DEVICE(0x08ca, 0x0106), BS(SPCA533, 0)},
  1260. {USB_DEVICE(0x08ca, 0x2008), BS(SPCA504B, 0)},
  1261. {USB_DEVICE(0x08ca, 0x2010), BS(SPCA533, 0)},
  1262. {USB_DEVICE(0x08ca, 0x2016), BS(SPCA504B, 0)},
  1263. {USB_DEVICE(0x08ca, 0x2018), BS(SPCA504B, 0)},
  1264. {USB_DEVICE(0x08ca, 0x2020), BS(SPCA533, 0)},
  1265. {USB_DEVICE(0x08ca, 0x2022), BS(SPCA533, 0)},
  1266. {USB_DEVICE(0x08ca, 0x2024), BS(SPCA536, 0)},
  1267. {USB_DEVICE(0x08ca, 0x2028), BS(SPCA533, 0)},
  1268. {USB_DEVICE(0x08ca, 0x2040), BS(SPCA536, 0)},
  1269. {USB_DEVICE(0x08ca, 0x2042), BS(SPCA536, 0)},
  1270. {USB_DEVICE(0x08ca, 0x2050), BS(SPCA536, 0)},
  1271. {USB_DEVICE(0x08ca, 0x2060), BS(SPCA536, 0)},
  1272. {USB_DEVICE(0x0d64, 0x0303), BS(SPCA536, 0)},
  1273. {}
  1274. };
  1275. MODULE_DEVICE_TABLE(usb, device_table);
  1276. /* -- device connect -- */
  1277. static int sd_probe(struct usb_interface *intf,
  1278. const struct usb_device_id *id)
  1279. {
  1280. return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
  1281. THIS_MODULE);
  1282. }
  1283. static struct usb_driver sd_driver = {
  1284. .name = MODULE_NAME,
  1285. .id_table = device_table,
  1286. .probe = sd_probe,
  1287. .disconnect = gspca_disconnect,
  1288. #ifdef CONFIG_PM
  1289. .suspend = gspca_suspend,
  1290. .resume = gspca_resume,
  1291. #endif
  1292. };
  1293. /* -- module insert / remove -- */
  1294. static int __init sd_mod_init(void)
  1295. {
  1296. int ret;
  1297. ret = usb_register(&sd_driver);
  1298. if (ret < 0)
  1299. return ret;
  1300. PDEBUG(D_PROBE, "registered");
  1301. return 0;
  1302. }
  1303. static void __exit sd_mod_exit(void)
  1304. {
  1305. usb_deregister(&sd_driver);
  1306. PDEBUG(D_PROBE, "deregistered");
  1307. }
  1308. module_init(sd_mod_init);
  1309. module_exit(sd_mod_exit);