sunplus.c 38 KB

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