sunplus.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429
  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 enought */
  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. struct gspca_frame *frame, /* target */
  1030. u8 *data, /* isoc packet */
  1031. int len) /* iso packet length */
  1032. {
  1033. struct sd *sd = (struct sd *) gspca_dev;
  1034. int i, sof = 0;
  1035. static u8 ffd9[] = {0xff, 0xd9};
  1036. /* frames are jpeg 4.1.1 without 0xff escape */
  1037. switch (sd->bridge) {
  1038. case BRIDGE_SPCA533:
  1039. if (data[0] == 0xff) {
  1040. if (data[1] != 0x01) { /* drop packet */
  1041. /* gspca_dev->last_packet_type = DISCARD_PACKET; */
  1042. return;
  1043. }
  1044. sof = 1;
  1045. data += SPCA533_OFFSET_DATA;
  1046. len -= SPCA533_OFFSET_DATA;
  1047. } else {
  1048. data += 1;
  1049. len -= 1;
  1050. }
  1051. break;
  1052. case BRIDGE_SPCA536:
  1053. if (data[0] == 0xff) {
  1054. sof = 1;
  1055. data += SPCA536_OFFSET_DATA;
  1056. len -= SPCA536_OFFSET_DATA;
  1057. } else {
  1058. data += 2;
  1059. len -= 2;
  1060. }
  1061. break;
  1062. default:
  1063. /* case BRIDGE_SPCA504: */
  1064. /* case BRIDGE_SPCA504B: */
  1065. switch (data[0]) {
  1066. case 0xfe: /* start of frame */
  1067. sof = 1;
  1068. data += SPCA50X_OFFSET_DATA;
  1069. len -= SPCA50X_OFFSET_DATA;
  1070. break;
  1071. case 0xff: /* drop packet */
  1072. /* gspca_dev->last_packet_type = DISCARD_PACKET; */
  1073. return;
  1074. default:
  1075. data += 1;
  1076. len -= 1;
  1077. break;
  1078. }
  1079. break;
  1080. case BRIDGE_SPCA504C:
  1081. switch (data[0]) {
  1082. case 0xfe: /* start of frame */
  1083. sof = 1;
  1084. data += SPCA504_PCCAM600_OFFSET_DATA;
  1085. len -= SPCA504_PCCAM600_OFFSET_DATA;
  1086. break;
  1087. case 0xff: /* drop packet */
  1088. /* gspca_dev->last_packet_type = DISCARD_PACKET; */
  1089. return;
  1090. default:
  1091. data += 1;
  1092. len -= 1;
  1093. break;
  1094. }
  1095. break;
  1096. }
  1097. if (sof) { /* start of frame */
  1098. frame = gspca_frame_add(gspca_dev, LAST_PACKET, frame,
  1099. ffd9, 2);
  1100. /* put the JPEG header in the new frame */
  1101. gspca_frame_add(gspca_dev, FIRST_PACKET, frame,
  1102. sd->jpeg_hdr, JPEG_HDR_SZ);
  1103. }
  1104. /* add 0x00 after 0xff */
  1105. i = 0;
  1106. do {
  1107. if (data[i] == 0xff) {
  1108. gspca_frame_add(gspca_dev, INTER_PACKET, frame,
  1109. data, i + 1);
  1110. len -= i;
  1111. data += i;
  1112. *data = 0x00;
  1113. i = 0;
  1114. }
  1115. i++;
  1116. } while (i < len);
  1117. gspca_frame_add(gspca_dev, INTER_PACKET, frame, data, len);
  1118. }
  1119. static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
  1120. {
  1121. struct sd *sd = (struct sd *) gspca_dev;
  1122. sd->brightness = val;
  1123. if (gspca_dev->streaming)
  1124. setbrightness(gspca_dev);
  1125. return 0;
  1126. }
  1127. static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
  1128. {
  1129. struct sd *sd = (struct sd *) gspca_dev;
  1130. *val = sd->brightness;
  1131. return 0;
  1132. }
  1133. static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
  1134. {
  1135. struct sd *sd = (struct sd *) gspca_dev;
  1136. sd->contrast = val;
  1137. if (gspca_dev->streaming)
  1138. setcontrast(gspca_dev);
  1139. return 0;
  1140. }
  1141. static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val)
  1142. {
  1143. struct sd *sd = (struct sd *) gspca_dev;
  1144. *val = sd->contrast;
  1145. return 0;
  1146. }
  1147. static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val)
  1148. {
  1149. struct sd *sd = (struct sd *) gspca_dev;
  1150. sd->colors = val;
  1151. if (gspca_dev->streaming)
  1152. setcolors(gspca_dev);
  1153. return 0;
  1154. }
  1155. static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val)
  1156. {
  1157. struct sd *sd = (struct sd *) gspca_dev;
  1158. *val = sd->colors;
  1159. return 0;
  1160. }
  1161. static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val)
  1162. {
  1163. struct sd *sd = (struct sd *) gspca_dev;
  1164. sd->autogain = val;
  1165. return 0;
  1166. }
  1167. static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val)
  1168. {
  1169. struct sd *sd = (struct sd *) gspca_dev;
  1170. *val = sd->autogain;
  1171. return 0;
  1172. }
  1173. static int sd_set_jcomp(struct gspca_dev *gspca_dev,
  1174. struct v4l2_jpegcompression *jcomp)
  1175. {
  1176. struct sd *sd = (struct sd *) gspca_dev;
  1177. if (jcomp->quality < QUALITY_MIN)
  1178. sd->quality = QUALITY_MIN;
  1179. else if (jcomp->quality > QUALITY_MAX)
  1180. sd->quality = QUALITY_MAX;
  1181. else
  1182. sd->quality = jcomp->quality;
  1183. if (gspca_dev->streaming)
  1184. jpeg_set_qual(sd->jpeg_hdr, sd->quality);
  1185. return 0;
  1186. }
  1187. static int sd_get_jcomp(struct gspca_dev *gspca_dev,
  1188. struct v4l2_jpegcompression *jcomp)
  1189. {
  1190. struct sd *sd = (struct sd *) gspca_dev;
  1191. memset(jcomp, 0, sizeof *jcomp);
  1192. jcomp->quality = sd->quality;
  1193. jcomp->jpeg_markers = V4L2_JPEG_MARKER_DHT
  1194. | V4L2_JPEG_MARKER_DQT;
  1195. return 0;
  1196. }
  1197. /* sub-driver description */
  1198. static const struct sd_desc sd_desc = {
  1199. .name = MODULE_NAME,
  1200. .ctrls = sd_ctrls,
  1201. .nctrls = ARRAY_SIZE(sd_ctrls),
  1202. .config = sd_config,
  1203. .init = sd_init,
  1204. .start = sd_start,
  1205. .stopN = sd_stopN,
  1206. .stop0 = sd_stop0,
  1207. .pkt_scan = sd_pkt_scan,
  1208. .get_jcomp = sd_get_jcomp,
  1209. .set_jcomp = sd_set_jcomp,
  1210. };
  1211. /* -- module initialisation -- */
  1212. #define BS(bridge, subtype) \
  1213. .driver_info = (BRIDGE_ ## bridge << 8) \
  1214. | (subtype)
  1215. static const __devinitdata struct usb_device_id device_table[] = {
  1216. {USB_DEVICE(0x041e, 0x400b), BS(SPCA504C, 0)},
  1217. {USB_DEVICE(0x041e, 0x4012), BS(SPCA504C, 0)},
  1218. {USB_DEVICE(0x041e, 0x4013), BS(SPCA504C, 0)},
  1219. {USB_DEVICE(0x0458, 0x7006), BS(SPCA504B, 0)},
  1220. {USB_DEVICE(0x0461, 0x0821), BS(SPCA533, 0)},
  1221. {USB_DEVICE(0x046d, 0x0905), BS(SPCA533, LogitechClickSmart820)},
  1222. {USB_DEVICE(0x046d, 0x0960), BS(SPCA504C, LogitechClickSmart420)},
  1223. {USB_DEVICE(0x0471, 0x0322), BS(SPCA504B, 0)},
  1224. {USB_DEVICE(0x04a5, 0x3003), BS(SPCA504B, 0)},
  1225. {USB_DEVICE(0x04a5, 0x3008), BS(SPCA533, 0)},
  1226. {USB_DEVICE(0x04a5, 0x300a), BS(SPCA533, 0)},
  1227. {USB_DEVICE(0x04f1, 0x1001), BS(SPCA504B, 0)},
  1228. {USB_DEVICE(0x04fc, 0x500c), BS(SPCA504B, 0)},
  1229. {USB_DEVICE(0x04fc, 0x504a), BS(SPCA504, AiptekMiniPenCam13)},
  1230. {USB_DEVICE(0x04fc, 0x504b), BS(SPCA504B, 0)},
  1231. {USB_DEVICE(0x04fc, 0x5330), BS(SPCA533, 0)},
  1232. {USB_DEVICE(0x04fc, 0x5360), BS(SPCA536, 0)},
  1233. {USB_DEVICE(0x04fc, 0xffff), BS(SPCA504B, 0)},
  1234. {USB_DEVICE(0x052b, 0x1513), BS(SPCA533, MegapixV4)},
  1235. {USB_DEVICE(0x052b, 0x1803), BS(SPCA533, MegaImageVI)},
  1236. {USB_DEVICE(0x0546, 0x3155), BS(SPCA533, 0)},
  1237. {USB_DEVICE(0x0546, 0x3191), BS(SPCA504B, 0)},
  1238. {USB_DEVICE(0x0546, 0x3273), BS(SPCA504B, 0)},
  1239. {USB_DEVICE(0x055f, 0xc211), BS(SPCA536, 0)},
  1240. {USB_DEVICE(0x055f, 0xc230), BS(SPCA533, 0)},
  1241. {USB_DEVICE(0x055f, 0xc232), BS(SPCA533, 0)},
  1242. {USB_DEVICE(0x055f, 0xc360), BS(SPCA536, 0)},
  1243. {USB_DEVICE(0x055f, 0xc420), BS(SPCA504, 0)},
  1244. {USB_DEVICE(0x055f, 0xc430), BS(SPCA533, 0)},
  1245. {USB_DEVICE(0x055f, 0xc440), BS(SPCA533, 0)},
  1246. {USB_DEVICE(0x055f, 0xc520), BS(SPCA504, 0)},
  1247. {USB_DEVICE(0x055f, 0xc530), BS(SPCA533, 0)},
  1248. {USB_DEVICE(0x055f, 0xc540), BS(SPCA533, 0)},
  1249. {USB_DEVICE(0x055f, 0xc630), BS(SPCA533, 0)},
  1250. {USB_DEVICE(0x055f, 0xc650), BS(SPCA533, 0)},
  1251. {USB_DEVICE(0x05da, 0x1018), BS(SPCA504B, 0)},
  1252. {USB_DEVICE(0x06d6, 0x0031), BS(SPCA533, 0)},
  1253. {USB_DEVICE(0x0733, 0x1311), BS(SPCA533, 0)},
  1254. {USB_DEVICE(0x0733, 0x1314), BS(SPCA533, 0)},
  1255. {USB_DEVICE(0x0733, 0x2211), BS(SPCA533, 0)},
  1256. {USB_DEVICE(0x0733, 0x2221), BS(SPCA533, 0)},
  1257. {USB_DEVICE(0x0733, 0x3261), BS(SPCA536, 0)},
  1258. {USB_DEVICE(0x0733, 0x3281), BS(SPCA536, 0)},
  1259. {USB_DEVICE(0x08ca, 0x0104), BS(SPCA533, 0)},
  1260. {USB_DEVICE(0x08ca, 0x0106), BS(SPCA533, 0)},
  1261. {USB_DEVICE(0x08ca, 0x2008), BS(SPCA504B, 0)},
  1262. {USB_DEVICE(0x08ca, 0x2010), BS(SPCA533, 0)},
  1263. {USB_DEVICE(0x08ca, 0x2016), BS(SPCA504B, 0)},
  1264. {USB_DEVICE(0x08ca, 0x2018), BS(SPCA504B, 0)},
  1265. {USB_DEVICE(0x08ca, 0x2020), BS(SPCA533, 0)},
  1266. {USB_DEVICE(0x08ca, 0x2022), BS(SPCA533, 0)},
  1267. {USB_DEVICE(0x08ca, 0x2024), BS(SPCA536, 0)},
  1268. {USB_DEVICE(0x08ca, 0x2028), BS(SPCA533, 0)},
  1269. {USB_DEVICE(0x08ca, 0x2040), BS(SPCA536, 0)},
  1270. {USB_DEVICE(0x08ca, 0x2042), BS(SPCA536, 0)},
  1271. {USB_DEVICE(0x08ca, 0x2050), BS(SPCA536, 0)},
  1272. {USB_DEVICE(0x08ca, 0x2060), BS(SPCA536, 0)},
  1273. {USB_DEVICE(0x0d64, 0x0303), BS(SPCA536, 0)},
  1274. {}
  1275. };
  1276. MODULE_DEVICE_TABLE(usb, device_table);
  1277. /* -- device connect -- */
  1278. static int sd_probe(struct usb_interface *intf,
  1279. const struct usb_device_id *id)
  1280. {
  1281. return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
  1282. THIS_MODULE);
  1283. }
  1284. static struct usb_driver sd_driver = {
  1285. .name = MODULE_NAME,
  1286. .id_table = device_table,
  1287. .probe = sd_probe,
  1288. .disconnect = gspca_disconnect,
  1289. #ifdef CONFIG_PM
  1290. .suspend = gspca_suspend,
  1291. .resume = gspca_resume,
  1292. #endif
  1293. };
  1294. /* -- module insert / remove -- */
  1295. static int __init sd_mod_init(void)
  1296. {
  1297. int ret;
  1298. ret = usb_register(&sd_driver);
  1299. if (ret < 0)
  1300. return ret;
  1301. PDEBUG(D_PROBE, "registered");
  1302. return 0;
  1303. }
  1304. static void __exit sd_mod_exit(void)
  1305. {
  1306. usb_deregister(&sd_driver);
  1307. PDEBUG(D_PROBE, "deregistered");
  1308. }
  1309. module_init(sd_mod_init);
  1310. module_exit(sd_mod_exit);