sunplus.c 38 KB

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