sq930x.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402
  1. /*
  2. * SQ930x subdriver
  3. *
  4. * Copyright (C) 2010 Jean-François Moine <http://moinejf.free.fr>
  5. * Copyright (C) 2006 -2008 Gerard Klaver <gerard at gkall dot hobby dot nl>
  6. * Copyright (C) 2007 Sam Revitch <samr7@cs.washington.edu>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #define MODULE_NAME "sq930x"
  23. #include "gspca.h"
  24. #include "jpeg.h"
  25. MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>\n"
  26. "Gerard Klaver <gerard at gkall dot hobby dot nl\n"
  27. "Sam Revitch <samr7@cs.washington.edu>");
  28. MODULE_DESCRIPTION("GSPCA/SQ930x USB Camera Driver");
  29. MODULE_LICENSE("GPL");
  30. #define BULK_TRANSFER_LEN 5128
  31. /* Structure to hold all of our device specific stuff */
  32. struct sd {
  33. struct gspca_dev gspca_dev; /* !! must be the first item */
  34. u16 expo;
  35. u8 gain;
  36. u8 quality; /* webcam quality 0..3 */
  37. #define QUALITY_DEF 1
  38. u8 gpio[2];
  39. u8 eof_len;
  40. u8 do_ctrl;
  41. u8 sensor;
  42. enum {
  43. SENSOR_ICX098BQ,
  44. SENSOR_LZ24BP,
  45. SENSOR_MI0360,
  46. SENSOR_MT9V111,
  47. SENSOR_OV7660,
  48. SENSOR_OV9630,
  49. } sensors;
  50. u8 type;
  51. #define Generic 0
  52. #define Creative_live_motion 1
  53. u8 jpeg_hdr[JPEG_HDR_SZ];
  54. };
  55. static int sd_setexpo(struct gspca_dev *gspca_dev, __s32 val);
  56. static int sd_getexpo(struct gspca_dev *gspca_dev, __s32 *val);
  57. static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val);
  58. static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val);
  59. static const struct ctrl sd_ctrls[] = {
  60. {
  61. {
  62. .id = V4L2_CID_EXPOSURE,
  63. .type = V4L2_CTRL_TYPE_INTEGER,
  64. .name = "Exposure",
  65. .minimum = 0x0001,
  66. .maximum = 0x0fff,
  67. .step = 1,
  68. #define EXPO_DEF 0x027d
  69. .default_value = EXPO_DEF,
  70. },
  71. .set = sd_setexpo,
  72. .get = sd_getexpo,
  73. },
  74. {
  75. {
  76. .id = V4L2_CID_GAIN,
  77. .type = V4L2_CTRL_TYPE_INTEGER,
  78. .name = "Gain",
  79. .minimum = 0x01,
  80. .maximum = 0xff,
  81. .step = 1,
  82. #define GAIN_DEF 0x61
  83. .default_value = GAIN_DEF,
  84. },
  85. .set = sd_setgain,
  86. .get = sd_getgain,
  87. },
  88. };
  89. static struct v4l2_pix_format vga_mode[] = {
  90. {160, 120, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
  91. .bytesperline = 160,
  92. .sizeimage = 160 * 120 * 5 / 8 + 590,
  93. .colorspace = V4L2_COLORSPACE_JPEG,
  94. .priv = 0},
  95. {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
  96. .bytesperline = 320,
  97. .sizeimage = 320 * 240 * 4 / 8 + 590,
  98. .colorspace = V4L2_COLORSPACE_JPEG,
  99. .priv = 1},
  100. {640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
  101. .bytesperline = 640,
  102. .sizeimage = 640 * 480 * 3 / 8 + 590,
  103. .colorspace = V4L2_COLORSPACE_JPEG,
  104. .priv = 2},
  105. };
  106. /* JPEG quality indexed by webcam quality */
  107. #define QUAL_0 90
  108. #define QUAL_1 85
  109. #define QUAL_2 75
  110. #define QUAL_3 70
  111. static const u8 quality_tb[4] = { QUAL_0, QUAL_1, QUAL_2, QUAL_3 };
  112. /* sq930x registers */
  113. #define SQ930_CTRL_UCBUS_IO 0x0001
  114. #define SQ930_CTRL_I2C_IO 0x0002
  115. #define SQ930_CTRL_GPIO 0x0005
  116. #define SQ930_CTRL_CAP_START 0x0010
  117. #define SQ930_CTRL_CAP_STOP 0x0011
  118. #define SQ930_CTRL_SET_EXPOSURE 0x001d
  119. #define SQ930_CTRL_RESET 0x001e
  120. #define SQ930_CTRL_GET_DEV_INFO 0x001f
  121. /* gpio 1 (8..15) */
  122. #define SQ930_GPIO_DFL_I2C_SDA 0x0001
  123. #define SQ930_GPIO_DFL_I2C_SCL 0x0002
  124. #define SQ930_GPIO_RSTBAR 0x0004
  125. #define SQ930_GPIO_EXTRA1 0x0040
  126. #define SQ930_GPIO_EXTRA2 0x0080
  127. /* gpio 3 (24..31) */
  128. #define SQ930_GPIO_POWER 0x0200
  129. #define SQ930_GPIO_DFL_LED 0x1000
  130. struct ucbus_write_cmd {
  131. u16 bw_addr;
  132. u8 bw_data;
  133. };
  134. struct i2c_write_cmd {
  135. u8 reg;
  136. u16 val;
  137. };
  138. static const struct ucbus_write_cmd icx098bq_start_0[] = {
  139. {0x0354, 0x00}, {0x03fa, 0x00}, {0xf800, 0x02}, {0xf801, 0xce},
  140. {0xf802, 0xc1}, {0xf804, 0x00}, {0xf808, 0x00}, {0xf809, 0x0e},
  141. {0xf80a, 0x01}, {0xf80b, 0xee}, {0xf807, 0x60}, {0xf80c, 0x02},
  142. {0xf80d, 0xf0}, {0xf80e, 0x03}, {0xf80f, 0x0a}, {0xf81c, 0x02},
  143. {0xf81d, 0xf0}, {0xf81e, 0x03}, {0xf81f, 0x0a}, {0xf83a, 0x00},
  144. {0xf83b, 0x10}, {0xf83c, 0x00}, {0xf83d, 0x4e}, {0xf810, 0x04},
  145. {0xf811, 0x00}, {0xf812, 0x02}, {0xf813, 0x10}, {0xf803, 0x00},
  146. {0xf814, 0x01}, {0xf815, 0x18}, {0xf816, 0x00}, {0xf817, 0x48},
  147. {0xf818, 0x00}, {0xf819, 0x25}, {0xf81a, 0x00}, {0xf81b, 0x3c},
  148. {0xf82f, 0x03}, {0xf820, 0xff}, {0xf821, 0x0d}, {0xf822, 0xff},
  149. {0xf823, 0x07}, {0xf824, 0xff}, {0xf825, 0x03}, {0xf826, 0xff},
  150. {0xf827, 0x06}, {0xf828, 0xff}, {0xf829, 0x03}, {0xf82a, 0xff},
  151. {0xf82b, 0x0c}, {0xf82c, 0xfd}, {0xf82d, 0x01}, {0xf82e, 0x00},
  152. {0xf830, 0x00}, {0xf831, 0x47}, {0xf832, 0x00}, {0xf833, 0x00},
  153. {0xf850, 0x00}, {0xf851, 0x00}, {0xf852, 0x00}, {0xf853, 0x24},
  154. {0xf854, 0x00}, {0xf855, 0x18}, {0xf856, 0x00}, {0xf857, 0x3c},
  155. {0xf858, 0x00}, {0xf859, 0x0c}, {0xf85a, 0x00}, {0xf85b, 0x30},
  156. {0xf85c, 0x00}, {0xf85d, 0x0c}, {0xf85e, 0x00}, {0xf85f, 0x30},
  157. {0xf860, 0x00}, {0xf861, 0x48}, {0xf862, 0x01}, {0xf863, 0xdc},
  158. {0xf864, 0xff}, {0xf865, 0x98}, {0xf866, 0xff}, {0xf867, 0xc0},
  159. {0xf868, 0xff}, {0xf869, 0x70}, {0xf86c, 0xff}, {0xf86d, 0x00},
  160. {0xf86a, 0xff}, {0xf86b, 0x48}, {0xf86e, 0xff}, {0xf86f, 0x00},
  161. {0xf870, 0x01}, {0xf871, 0xdb}, {0xf872, 0x01}, {0xf873, 0xfa},
  162. {0xf874, 0x01}, {0xf875, 0xdb}, {0xf876, 0x01}, {0xf877, 0xfa},
  163. {0xf878, 0x0f}, {0xf879, 0x0f}, {0xf87a, 0xff}, {0xf87b, 0xff},
  164. {0xf800, 0x03}
  165. };
  166. static const struct ucbus_write_cmd icx098bq_start_1[] = {
  167. {0xf5f0, 0x00}, {0xf5f1, 0xcd}, {0xf5f2, 0x80}, {0xf5f3, 0x80},
  168. {0xf5f4, 0xc0},
  169. {0xf5f0, 0x49}, {0xf5f1, 0xcd}, {0xf5f2, 0x80}, {0xf5f3, 0x80},
  170. {0xf5f4, 0xc0},
  171. {0xf5fa, 0x00}, {0xf5f6, 0x00}, {0xf5f7, 0x00}, {0xf5f8, 0x00},
  172. {0xf5f9, 0x00}
  173. };
  174. static const struct ucbus_write_cmd icx098bq_start_2[] = {
  175. {0xf800, 0x02}, {0xf807, 0xff}, {0xf805, 0x82}, {0xf806, 0x00},
  176. {0xf807, 0x7f}, {0xf800, 0x03},
  177. {0xf800, 0x02}, {0xf807, 0xff}, {0xf805, 0x40}, {0xf806, 0x00},
  178. {0xf807, 0x7f}, {0xf800, 0x03},
  179. {0xf800, 0x02}, {0xf807, 0xff}, {0xf805, 0xcf}, {0xf806, 0xd0},
  180. {0xf807, 0x7f}, {0xf800, 0x03},
  181. {0xf800, 0x02}, {0xf807, 0xff}, {0xf805, 0x00}, {0xf806, 0x00},
  182. {0xf807, 0x7f}, {0xf800, 0x03}
  183. };
  184. static const struct ucbus_write_cmd lz24bp_start_0[] = {
  185. {0x0354, 0x00}, {0x03fa, 0x00}, {0xf800, 0x02}, {0xf801, 0xbe},
  186. {0xf802, 0xc6}, {0xf804, 0x00}, {0xf808, 0x00}, {0xf809, 0x06},
  187. {0xf80a, 0x01}, {0xf80b, 0xfe}, {0xf807, 0x84}, {0xf80c, 0x02},
  188. {0xf80d, 0xf7}, {0xf80e, 0x03}, {0xf80f, 0x0b}, {0xf81c, 0x00},
  189. {0xf81d, 0x49}, {0xf81e, 0x03}, {0xf81f, 0x0b}, {0xf83a, 0x00},
  190. {0xf83b, 0x01}, {0xf83c, 0x00}, {0xf83d, 0x6b}, {0xf810, 0x03},
  191. {0xf811, 0x10}, {0xf812, 0x02}, {0xf813, 0x6f}, {0xf803, 0x00},
  192. {0xf814, 0x00}, {0xf815, 0x44}, {0xf816, 0x00}, {0xf817, 0x48},
  193. {0xf818, 0x00}, {0xf819, 0x25}, {0xf81a, 0x00}, {0xf81b, 0x3c},
  194. {0xf82f, 0x03}, {0xf820, 0xff}, {0xf821, 0x0d}, {0xf822, 0xff},
  195. {0xf823, 0x07}, {0xf824, 0xfd}, {0xf825, 0x07}, {0xf826, 0xf0},
  196. {0xf827, 0x0c}, {0xf828, 0xff}, {0xf829, 0x03}, {0xf82a, 0xff},
  197. {0xf82b, 0x0c}, {0xf82c, 0xfc}, {0xf82d, 0x01}, {0xf82e, 0x00},
  198. {0xf830, 0x00}, {0xf831, 0x47}, {0xf832, 0x00}, {0xf833, 0x00},
  199. {0xf850, 0x00}, {0xf851, 0x00}, {0xf852, 0x00}, {0xf853, 0x24},
  200. {0xf854, 0x00}, {0xf855, 0x0c}, {0xf856, 0x00}, {0xf857, 0x30},
  201. {0xf858, 0x00}, {0xf859, 0x18}, {0xf85a, 0x00}, {0xf85b, 0x3c},
  202. {0xf85c, 0x00}, {0xf85d, 0x18}, {0xf85e, 0x00}, {0xf85f, 0x3c},
  203. {0xf860, 0xff}, {0xf861, 0x37}, {0xf862, 0xff}, {0xf863, 0x1d},
  204. {0xf864, 0xff}, {0xf865, 0x98}, {0xf866, 0xff}, {0xf867, 0xc0},
  205. {0xf868, 0x00}, {0xf869, 0x37}, {0xf86c, 0x02}, {0xf86d, 0x1d},
  206. {0xf86a, 0x00}, {0xf86b, 0x37}, {0xf86e, 0x02}, {0xf86f, 0x1d},
  207. {0xf870, 0x01}, {0xf871, 0xc6}, {0xf872, 0x02}, {0xf873, 0x04},
  208. {0xf874, 0x01}, {0xf875, 0xc6}, {0xf876, 0x02}, {0xf877, 0x04},
  209. {0xf878, 0x0f}, {0xf879, 0x0f}, {0xf87a, 0xff}, {0xf87b, 0xff},
  210. {0xf800, 0x03}
  211. };
  212. static const struct ucbus_write_cmd lz24bp_start_1_gen[] = {
  213. {0xf5f0, 0x00}, {0xf5f1, 0xff}, {0xf5f2, 0x80}, {0xf5f3, 0x80},
  214. {0xf5f4, 0xb3},
  215. {0xf5f0, 0x40}, {0xf5f1, 0xff}, {0xf5f2, 0x80}, {0xf5f3, 0x80},
  216. {0xf5f4, 0xb3},
  217. {0xf5fa, 0x00}, {0xf5f6, 0x00}, {0xf5f7, 0x00}, {0xf5f8, 0x00},
  218. {0xf5f9, 0x00}
  219. };
  220. static const struct ucbus_write_cmd lz24bp_start_1_clm[] = {
  221. {0xf5f0, 0x00}, {0xf5f1, 0xff}, {0xf5f2, 0x88}, {0xf5f3, 0x88},
  222. {0xf5f4, 0xc0},
  223. {0xf5f0, 0x40}, {0xf5f1, 0xff}, {0xf5f2, 0x88}, {0xf5f3, 0x88},
  224. {0xf5f4, 0xc0},
  225. {0xf5fa, 0x00}, {0xf5f6, 0x00}, {0xf5f7, 0x00}, {0xf5f8, 0x00},
  226. {0xf5f9, 0x00}
  227. };
  228. static const struct ucbus_write_cmd lz24bp_start_2[] = {
  229. {0xf800, 0x02}, {0xf807, 0xff}, {0xf805, 0x80}, {0xf806, 0x00},
  230. {0xf807, 0x7f}, {0xf800, 0x03},
  231. {0xf800, 0x02}, {0xf807, 0xff}, {0xf805, 0x4e}, {0xf806, 0x00},
  232. {0xf807, 0x7f}, {0xf800, 0x03},
  233. {0xf800, 0x02}, {0xf807, 0xff}, {0xf805, 0xc0}, {0xf806, 0x48},
  234. {0xf807, 0x7f}, {0xf800, 0x03},
  235. {0xf800, 0x02}, {0xf807, 0xff}, {0xf805, 0x00}, {0xf806, 0x00},
  236. {0xf807, 0x7f}, {0xf800, 0x03}
  237. };
  238. static const struct ucbus_write_cmd mi0360_start_0[] = {
  239. {0x0354, 0x00}, {0x03fa, 0x00}, {0xf332, 0xcc}, {0xf333, 0xcc},
  240. {0xf334, 0xcc}, {0xf335, 0xcc}, {0xf33f, 0x00}
  241. };
  242. static const struct i2c_write_cmd mi0360_init_23[] = {
  243. {0x30, 0x0040}, /* reserved - def 0x0005 */
  244. {0x31, 0x0000}, /* reserved - def 0x002a */
  245. {0x34, 0x0100}, /* reserved - def 0x0100 */
  246. {0x3d, 0x068f}, /* reserved - def 0x068f */
  247. };
  248. static const struct i2c_write_cmd mi0360_init_24[] = {
  249. {0x03, 0x01e5}, /* window height */
  250. {0x04, 0x0285}, /* window width */
  251. };
  252. static const struct i2c_write_cmd mi0360_init_25[] = {
  253. {0x35, 0x0020}, /* global gain */
  254. {0x2b, 0x0020}, /* green1 gain */
  255. {0x2c, 0x002a}, /* blue gain */
  256. {0x2d, 0x0028}, /* red gain */
  257. {0x2e, 0x0020}, /* green2 gain */
  258. };
  259. static const struct ucbus_write_cmd mi0360_start_1[] = {
  260. {0xf5f0, 0x11}, {0xf5f1, 0x99}, {0xf5f2, 0x80}, {0xf5f3, 0x80},
  261. {0xf5f4, 0xa6},
  262. {0xf5f0, 0x51}, {0xf5f1, 0x99}, {0xf5f2, 0x80}, {0xf5f3, 0x80},
  263. {0xf5f4, 0xa6},
  264. {0xf5fa, 0x00}, {0xf5f6, 0x00}, {0xf5f7, 0x00}, {0xf5f8, 0x00},
  265. {0xf5f9, 0x00}
  266. };
  267. static const struct i2c_write_cmd mi0360_start_2[] = {
  268. {0x62, 0x041d}, /* reserved - def 0x0418 */
  269. };
  270. static const struct i2c_write_cmd mi0360_start_3[] = {
  271. {0x05, 0x007b}, /* horiz blanking */
  272. };
  273. static const struct i2c_write_cmd mi0360_start_4[] = {
  274. {0x05, 0x03f5}, /* horiz blanking */
  275. };
  276. static const struct i2c_write_cmd mt9v111_init_0[] = {
  277. {0x01, 0x0001}, /* select IFP/SOC registers */
  278. {0x06, 0x300c}, /* operating mode control */
  279. {0x08, 0xcc00}, /* output format control (RGB) */
  280. {0x01, 0x0004}, /* select core registers */
  281. };
  282. static const struct i2c_write_cmd mt9v111_init_1[] = {
  283. {0x03, 0x01e5}, /* window height */
  284. {0x04, 0x0285}, /* window width */
  285. };
  286. static const struct i2c_write_cmd mt9v111_init_2[] = {
  287. {0x30, 0x7800},
  288. {0x31, 0x0000},
  289. {0x07, 0x3002}, /* output control */
  290. {0x35, 0x0020}, /* global gain */
  291. {0x2b, 0x0020}, /* green1 gain */
  292. {0x2c, 0x0020}, /* blue gain */
  293. {0x2d, 0x0020}, /* red gain */
  294. {0x2e, 0x0020}, /* green2 gain */
  295. };
  296. static const struct ucbus_write_cmd mt9v111_start_1[] = {
  297. {0xf5f0, 0x11}, {0xf5f1, 0x96}, {0xf5f2, 0x80}, {0xf5f3, 0x80},
  298. {0xf5f4, 0xaa},
  299. {0xf5f0, 0x51}, {0xf5f1, 0x96}, {0xf5f2, 0x80}, {0xf5f3, 0x80},
  300. {0xf5f4, 0xaa},
  301. {0xf5fa, 0x00}, {0xf5f6, 0x0a}, {0xf5f7, 0x0a}, {0xf5f8, 0x0a},
  302. {0xf5f9, 0x0a}
  303. };
  304. static const struct i2c_write_cmd mt9v111_init_3[] = {
  305. {0x62, 0x0405},
  306. };
  307. static const struct i2c_write_cmd mt9v111_init_4[] = {
  308. {0x05, 0x00ce}, /* horizontal blanking */
  309. };
  310. static const struct ucbus_write_cmd ov7660_start_0[] = {
  311. {0x0354, 0x00}, {0x03fa, 0x00}, {0xf332, 0x00}, {0xf333, 0xc0},
  312. {0xf334, 0x39}, {0xf335, 0xe7}, {0xf33f, 0x03}
  313. };
  314. static const struct ucbus_write_cmd ov9630_start_0[] = {
  315. {0x0354, 0x00}, {0x03fa, 0x00}, {0xf332, 0x00}, {0xf333, 0x00},
  316. {0xf334, 0x3e}, {0xf335, 0xf8}, {0xf33f, 0x03}
  317. };
  318. static const struct cap_s {
  319. u8 cc_sizeid;
  320. u8 cc_bytes[32];
  321. } capconfig[4][3] = {
  322. [SENSOR_ICX098BQ] = {
  323. {0, /* JPEG, 160x120 */
  324. {0x01, 0x1f, 0x20, 0x0e, 0x00, 0x9f, 0x02, 0xee,
  325. 0x01, 0x02, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
  326. 0x02, 0x8b, 0x00, 0x8b, 0x00, 0x41, 0x01, 0x41,
  327. 0x01, 0x41, 0x01, 0x05, 0x40, 0x01, 0xf0, 0x00} },
  328. {2, /* JPEG, 320x240 */
  329. {0x01, 0x1f, 0x20, 0x0e, 0x00, 0x9f, 0x02, 0xee,
  330. 0x01, 0x02, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
  331. 0x02, 0xdf, 0x01, 0x00, 0x00, 0x3f, 0x01, 0x3f,
  332. 0x01, 0x00, 0x00, 0x05, 0x40, 0x01, 0xf0, 0x00} },
  333. {4, /* JPEG, 640x480 */
  334. {0x01, 0x22, 0x20, 0x0e, 0x00, 0xa2, 0x02, 0xf0,
  335. 0x01, 0x02, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
  336. 0x07, 0xe1, 0x01, 0xe1, 0x01, 0x3f, 0x01, 0x3f,
  337. 0x01, 0x3f, 0x01, 0x05, 0x80, 0x02, 0xe0, 0x01} },
  338. },
  339. [SENSOR_LZ24BP] = {
  340. {0, /* JPEG, 160x120 */
  341. {0x01, 0x1f, 0x20, 0x0e, 0x00, 0x9f, 0x02, 0xee,
  342. 0x01, 0x02, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
  343. 0x02, 0x8b, 0x00, 0x8b, 0x00, 0x41, 0x01, 0x41,
  344. 0x01, 0x41, 0x01, 0x05, 0x40, 0x01, 0xf0, 0x00} },
  345. {2, /* JPEG, 320x240 */
  346. {0x01, 0x22, 0x20, 0x0e, 0x00, 0xa2, 0x02, 0xee,
  347. 0x01, 0x02, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
  348. 0x02, 0xdf, 0x01, 0x00, 0x00, 0x3f, 0x01, 0x3f,
  349. 0x01, 0x00, 0x00, 0x05, 0x40, 0x01, 0xf0, 0x00} },
  350. {4, /* JPEG, 640x480 */
  351. {0x01, 0x22, 0x20, 0x0e, 0x00, 0xa2, 0x02, 0xf0,
  352. 0x01, 0x02, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
  353. 0x07, 0xe1, 0x01, 0xe1, 0x01, 0x3f, 0x01, 0x3f,
  354. 0x01, 0x3f, 0x01, 0x05, 0x80, 0x02, 0xe0, 0x01} },
  355. },
  356. [SENSOR_MI0360] = {
  357. {0, /* JPEG, 160x120 */
  358. {0x05, 0x3d, 0x20, 0x0b, 0x00, 0xbd, 0x02, 0x0b,
  359. 0x02, 0x02, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
  360. 0x02, 0x01, 0x01, 0x01, 0x01, 0x9f, 0x00, 0x9f,
  361. 0x00, 0x9f, 0x01, 0x05, 0xa0, 0x00, 0x80, 0x00} },
  362. {2, /* JPEG, 320x240 */
  363. {0x01, 0x02, 0x20, 0x01, 0x20, 0x82, 0x02, 0xe1,
  364. /*fixme 03 e3 */
  365. 0x01, 0x02, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
  366. 0x02, 0xdf, 0x01, 0x00, 0x00, 0x3f, 0x01, 0x3f,
  367. 0x01, 0x00, 0x00, 0x05, 0x40, 0x01, 0xf0, 0x00} },
  368. {4, /* JPEG, 640x480 */
  369. {0x01, 0x02, 0x20, 0x01, 0x20, 0x82, 0x02, 0xe3,
  370. 0x01, 0x02, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
  371. 0x07, 0xe1, 0x01, 0xe1, 0x01, 0x3f, 0x01, 0x3f,
  372. 0x01, 0x3f, 0x01, 0x05, 0x80, 0x02, 0xe0, 0x01} },
  373. },
  374. [SENSOR_MT9V111] = {
  375. {0, /* JPEG, 160x120 */
  376. {0x05, 0x3d, 0x20, 0x0b, 0x00, 0xbd, 0x02, 0x0b,
  377. 0x02, 0x02, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
  378. 0x02, 0x01, 0x01, 0x01, 0x01, 0x9f, 0x00, 0x9f,
  379. 0x00, 0x9f, 0x01, 0x05, 0xa0, 0x00, 0x80, 0x00} },
  380. {2, /* JPEG, 320x240 */
  381. {0x01, 0x02, 0x20, 0x03, 0x20, 0x82, 0x02, 0xe3,
  382. 0x01, 0x02, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
  383. 0x02, 0xdf, 0x01, 0x00, 0x00, 0x3f, 0x01, 0x3f,
  384. 0x01, 0x00, 0x00, 0x05, 0x40, 0x01, 0xf0, 0x00} },
  385. {4, /* JPEG, 640x480 */
  386. {0x01, 0x02, 0x20, 0x03, 0x20, 0x82, 0x02, 0xe3,
  387. 0x01, 0x02, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
  388. 0x07, 0xe1, 0x01, 0xe1, 0x01, 0x3f, 0x01, 0x3f,
  389. 0x01, 0x3f, 0x01, 0x05, 0x80, 0x02, 0xe0, 0x01} },
  390. },
  391. };
  392. struct sensor_s {
  393. const char *name;
  394. u8 i2c_addr;
  395. u8 i2c_dum;
  396. u8 gpio[5];
  397. u8 cmd_len;
  398. const struct ucbus_write_cmd *cmd;
  399. };
  400. static const struct sensor_s sensor_tb[] = {
  401. [SENSOR_ICX098BQ] = {
  402. "icx098bp",
  403. 0x00, 0x00,
  404. {0,
  405. SQ930_GPIO_DFL_I2C_SDA | SQ930_GPIO_DFL_I2C_SCL,
  406. SQ930_GPIO_DFL_I2C_SDA,
  407. 0,
  408. SQ930_GPIO_RSTBAR
  409. },
  410. 8, icx098bq_start_0
  411. },
  412. [SENSOR_LZ24BP] = {
  413. "lz24bp",
  414. 0x00, 0x00,
  415. {0,
  416. SQ930_GPIO_DFL_I2C_SDA | SQ930_GPIO_DFL_I2C_SCL,
  417. SQ930_GPIO_DFL_I2C_SDA,
  418. 0,
  419. SQ930_GPIO_RSTBAR
  420. },
  421. 8, lz24bp_start_0
  422. },
  423. [SENSOR_MI0360] = {
  424. "mi0360",
  425. 0x5d, 0x80,
  426. {SQ930_GPIO_RSTBAR,
  427. SQ930_GPIO_DFL_I2C_SDA | SQ930_GPIO_DFL_I2C_SCL,
  428. SQ930_GPIO_DFL_I2C_SDA,
  429. 0,
  430. 0
  431. },
  432. 7, mi0360_start_0
  433. },
  434. [SENSOR_MT9V111] = {
  435. "mt9v111",
  436. 0x5c, 0x7f,
  437. {SQ930_GPIO_RSTBAR,
  438. SQ930_GPIO_DFL_I2C_SDA | SQ930_GPIO_DFL_I2C_SCL,
  439. SQ930_GPIO_DFL_I2C_SDA,
  440. 0,
  441. 0
  442. },
  443. 7, mi0360_start_0
  444. },
  445. [SENSOR_OV7660] = {
  446. "ov7660",
  447. 0x21, 0x00,
  448. {0,
  449. SQ930_GPIO_DFL_I2C_SDA | SQ930_GPIO_DFL_I2C_SCL,
  450. SQ930_GPIO_DFL_I2C_SDA,
  451. 0,
  452. SQ930_GPIO_RSTBAR
  453. },
  454. 7, ov7660_start_0
  455. },
  456. [SENSOR_OV9630] = {
  457. "ov9630",
  458. 0x30, 0x00,
  459. {0,
  460. SQ930_GPIO_DFL_I2C_SDA | SQ930_GPIO_DFL_I2C_SCL,
  461. SQ930_GPIO_DFL_I2C_SDA,
  462. 0,
  463. SQ930_GPIO_RSTBAR
  464. },
  465. 7, ov9630_start_0
  466. },
  467. };
  468. static void reg_r(struct gspca_dev *gspca_dev,
  469. u16 value, int len)
  470. {
  471. int ret;
  472. if (gspca_dev->usb_err < 0)
  473. return;
  474. ret = usb_control_msg(gspca_dev->dev,
  475. usb_rcvctrlpipe(gspca_dev->dev, 0),
  476. 0x0c,
  477. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  478. value, 0, gspca_dev->usb_buf, len,
  479. 500);
  480. if (ret < 0) {
  481. PDEBUG(D_ERR, "reg_r %04x failed %d", value, ret);
  482. gspca_dev->usb_err = ret;
  483. }
  484. }
  485. static void reg_w(struct gspca_dev *gspca_dev, u16 value, u16 index)
  486. {
  487. int ret;
  488. if (gspca_dev->usb_err < 0)
  489. return;
  490. PDEBUG(D_USBO, "reg_w v: %04x i: %04x", value, index);
  491. ret = usb_control_msg(gspca_dev->dev,
  492. usb_sndctrlpipe(gspca_dev->dev, 0),
  493. 0x0c, /* request */
  494. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  495. value, index, NULL, 0,
  496. 500);
  497. msleep(30);
  498. if (ret < 0) {
  499. PDEBUG(D_ERR, "reg_w %04x %04x failed %d", value, index, ret);
  500. gspca_dev->usb_err = ret;
  501. }
  502. }
  503. static void reg_wb(struct gspca_dev *gspca_dev, u16 value, u16 index,
  504. const u8 *data, int len)
  505. {
  506. int ret;
  507. if (gspca_dev->usb_err < 0)
  508. return;
  509. PDEBUG(D_USBO, "reg_wb v: %04x i: %04x %02x...%02x",
  510. value, index, *data, data[len - 1]);
  511. memcpy(gspca_dev->usb_buf, data, len);
  512. ret = usb_control_msg(gspca_dev->dev,
  513. usb_sndctrlpipe(gspca_dev->dev, 0),
  514. 0x0c, /* request */
  515. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  516. value, index, gspca_dev->usb_buf, len,
  517. 1000);
  518. msleep(30);
  519. if (ret < 0) {
  520. PDEBUG(D_ERR, "reg_wb %04x %04x failed %d", value, index, ret);
  521. gspca_dev->usb_err = ret;
  522. }
  523. }
  524. static void i2c_write(struct sd *sd,
  525. const struct i2c_write_cmd *cmd,
  526. int ncmds)
  527. {
  528. struct gspca_dev *gspca_dev = &sd->gspca_dev;
  529. const struct sensor_s *sensor;
  530. u16 val, idx;
  531. u8 *buf;
  532. int ret;
  533. if (gspca_dev->usb_err < 0)
  534. return;
  535. sensor = &sensor_tb[sd->sensor];
  536. val = (sensor->i2c_addr << 8) | SQ930_CTRL_I2C_IO;
  537. idx = (cmd->val & 0xff00) | cmd->reg;
  538. buf = gspca_dev->usb_buf;
  539. *buf++ = sensor->i2c_dum;
  540. *buf++ = cmd->val;
  541. while (--ncmds > 0) {
  542. cmd++;
  543. *buf++ = cmd->reg;
  544. *buf++ = cmd->val >> 8;
  545. *buf++ = sensor->i2c_dum;
  546. *buf++ = cmd->val;
  547. }
  548. PDEBUG(D_USBO, "i2c_w v: %04x i: %04x %02x...%02x",
  549. val, idx, gspca_dev->usb_buf[0], buf[-1]);
  550. ret = usb_control_msg(gspca_dev->dev,
  551. usb_sndctrlpipe(gspca_dev->dev, 0),
  552. 0x0c, /* request */
  553. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  554. val, idx,
  555. gspca_dev->usb_buf, buf - gspca_dev->usb_buf,
  556. 500);
  557. if (ret < 0) {
  558. PDEBUG(D_ERR, "i2c_write failed %d", ret);
  559. gspca_dev->usb_err = ret;
  560. }
  561. }
  562. static void ucbus_write(struct gspca_dev *gspca_dev,
  563. const struct ucbus_write_cmd *cmd,
  564. int ncmds,
  565. int batchsize)
  566. {
  567. u8 *buf;
  568. u16 val, idx;
  569. int len, ret;
  570. if (gspca_dev->usb_err < 0)
  571. return;
  572. #ifdef GSPCA_DEBUG
  573. if ((batchsize - 1) * 3 > USB_BUF_SZ) {
  574. err("Bug: usb_buf overflow");
  575. gspca_dev->usb_err = -ENOMEM;
  576. return;
  577. }
  578. #endif
  579. for (;;) {
  580. len = ncmds;
  581. if (len > batchsize)
  582. len = batchsize;
  583. ncmds -= len;
  584. val = (cmd->bw_addr << 8) | SQ930_CTRL_UCBUS_IO;
  585. idx = (cmd->bw_data << 8) | (cmd->bw_addr >> 8);
  586. buf = gspca_dev->usb_buf;
  587. while (--len > 0) {
  588. cmd++;
  589. *buf++ = cmd->bw_addr;
  590. *buf++ = cmd->bw_addr >> 8;
  591. *buf++ = cmd->bw_data;
  592. }
  593. if (buf != gspca_dev->usb_buf)
  594. PDEBUG(D_USBO, "ucbus v: %04x i: %04x %02x...%02x",
  595. val, idx,
  596. gspca_dev->usb_buf[0], buf[-1]);
  597. else
  598. PDEBUG(D_USBO, "ucbus v: %04x i: %04x",
  599. val, idx);
  600. ret = usb_control_msg(gspca_dev->dev,
  601. usb_sndctrlpipe(gspca_dev->dev, 0),
  602. 0x0c, /* request */
  603. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  604. val, idx,
  605. gspca_dev->usb_buf, buf - gspca_dev->usb_buf,
  606. 500);
  607. if (ret < 0) {
  608. PDEBUG(D_ERR, "ucbus_write failed %d", ret);
  609. gspca_dev->usb_err = ret;
  610. return;
  611. }
  612. msleep(30);
  613. if (ncmds <= 0)
  614. break;
  615. cmd++;
  616. }
  617. }
  618. static void gpio_set(struct sd *sd, u16 val, u16 mask)
  619. {
  620. struct gspca_dev *gspca_dev = &sd->gspca_dev;
  621. if (mask & 0x00ff) {
  622. sd->gpio[0] &= ~mask;
  623. sd->gpio[0] |= val;
  624. reg_w(gspca_dev, 0x0100 | SQ930_CTRL_GPIO,
  625. ~sd->gpio[0] << 8);
  626. }
  627. mask >>= 8;
  628. val >>= 8;
  629. if (mask) {
  630. sd->gpio[1] &= ~mask;
  631. sd->gpio[1] |= val;
  632. reg_w(gspca_dev, 0x0300 | SQ930_CTRL_GPIO,
  633. ~sd->gpio[1] << 8);
  634. }
  635. }
  636. static void gpio_init(struct sd *sd,
  637. const u8 *gpio)
  638. {
  639. gpio_set(sd, *gpio++, 0x000f);
  640. gpio_set(sd, *gpio++, 0x000f);
  641. gpio_set(sd, *gpio++, 0x000f);
  642. gpio_set(sd, *gpio++, 0x000f);
  643. gpio_set(sd, *gpio, 0x000f);
  644. }
  645. static void bridge_init(struct sd *sd)
  646. {
  647. static const struct ucbus_write_cmd clkfreq_cmd = {
  648. 0xf031, 0 /* SQ930_CLKFREQ_60MHZ */
  649. };
  650. ucbus_write(&sd->gspca_dev, &clkfreq_cmd, 1, 1);
  651. gpio_set(sd, SQ930_GPIO_POWER, 0xff00);
  652. }
  653. static void cmos_probe(struct gspca_dev *gspca_dev)
  654. {
  655. struct sd *sd = (struct sd *) gspca_dev;
  656. int i;
  657. const struct sensor_s *sensor;
  658. static const u8 probe_order[] = {
  659. /* SENSOR_LZ24BP, (tested as ccd) */
  660. SENSOR_OV9630,
  661. SENSOR_MI0360,
  662. SENSOR_OV7660,
  663. SENSOR_MT9V111,
  664. };
  665. for (i = 0; i < ARRAY_SIZE(probe_order); i++) {
  666. sensor = &sensor_tb[probe_order[i]];
  667. ucbus_write(&sd->gspca_dev, sensor->cmd, sensor->cmd_len, 8);
  668. gpio_init(sd, sensor->gpio);
  669. msleep(100);
  670. reg_r(gspca_dev, (sensor->i2c_addr << 8) | 0x001c, 1);
  671. msleep(100);
  672. if (gspca_dev->usb_buf[0] != 0)
  673. break;
  674. }
  675. if (i >= ARRAY_SIZE(probe_order))
  676. PDEBUG(D_PROBE, "Unknown sensor");
  677. else
  678. sd->sensor = probe_order[i];
  679. }
  680. static void mt9v111_init(struct gspca_dev *gspca_dev)
  681. {
  682. int i, nwait;
  683. static const u8 cmd_001b[] = {
  684. 0x00, 0x3b, 0xf6, 0x01, 0x03, 0x02, 0x00, 0x00,
  685. 0x00, 0x00, 0x00
  686. };
  687. static const u8 cmd_011b[][7] = {
  688. {0x10, 0x01, 0x66, 0x08, 0x00, 0x00, 0x00},
  689. {0x01, 0x00, 0x1a, 0x04, 0x00, 0x00, 0x00},
  690. {0x20, 0x00, 0x10, 0x04, 0x00, 0x00, 0x00},
  691. {0x02, 0x01, 0xae, 0x01, 0x00, 0x00, 0x00},
  692. };
  693. reg_wb(gspca_dev, 0x001b, 0x0000, cmd_001b, sizeof cmd_001b);
  694. for (i = 0; i < ARRAY_SIZE(cmd_011b); i++) {
  695. reg_wb(gspca_dev, 0x001b, 0x0000, cmd_011b[i],
  696. ARRAY_SIZE(cmd_011b[0]));
  697. msleep(400);
  698. nwait = 20;
  699. for (;;) {
  700. reg_r(gspca_dev, 0x031b, 1);
  701. if (gspca_dev->usb_buf[0] == 0
  702. || gspca_dev->usb_err != 0)
  703. break;
  704. if (--nwait < 0) {
  705. PDEBUG(D_PROBE, "mt9v111_init timeout");
  706. gspca_dev->usb_err = -ETIME;
  707. return;
  708. }
  709. msleep(50);
  710. }
  711. }
  712. }
  713. static void global_init(struct sd *sd, int first_time)
  714. {
  715. switch (sd->sensor) {
  716. case SENSOR_ICX098BQ:
  717. if (first_time)
  718. ucbus_write(&sd->gspca_dev,
  719. icx098bq_start_0,
  720. 8, 8);
  721. gpio_init(sd, sensor_tb[sd->sensor].gpio);
  722. break;
  723. case SENSOR_LZ24BP:
  724. if (sd->type != Creative_live_motion)
  725. gpio_set(sd, SQ930_GPIO_EXTRA1, 0x00ff);
  726. else
  727. gpio_set(sd, 0, 0x00ff);
  728. msleep(50);
  729. if (first_time)
  730. ucbus_write(&sd->gspca_dev,
  731. lz24bp_start_0,
  732. 8, 8);
  733. gpio_init(sd, sensor_tb[sd->sensor].gpio);
  734. break;
  735. case SENSOR_MI0360:
  736. if (first_time)
  737. ucbus_write(&sd->gspca_dev,
  738. mi0360_start_0,
  739. ARRAY_SIZE(mi0360_start_0),
  740. 8);
  741. gpio_init(sd, sensor_tb[sd->sensor].gpio);
  742. gpio_set(sd, SQ930_GPIO_EXTRA2, SQ930_GPIO_EXTRA2);
  743. break;
  744. default:
  745. /* case SENSOR_MT9V111: */
  746. if (first_time)
  747. mt9v111_init(&sd->gspca_dev);
  748. else
  749. gpio_init(sd, sensor_tb[sd->sensor].gpio);
  750. break;
  751. }
  752. }
  753. static void lz24bp_ppl(struct sd *sd, u16 ppl)
  754. {
  755. struct ucbus_write_cmd cmds[2] = {
  756. {0xf810, ppl >> 8},
  757. {0xf811, ppl}
  758. };
  759. ucbus_write(&sd->gspca_dev, cmds, ARRAY_SIZE(cmds), 2);
  760. }
  761. static void setexposure(struct gspca_dev *gspca_dev)
  762. {
  763. struct sd *sd = (struct sd *) gspca_dev;
  764. int i, integclks, intstartclk, frameclks, min_frclk;
  765. const struct sensor_s *sensor;
  766. u16 cmd;
  767. u8 buf[15];
  768. integclks = sd->expo;
  769. i = 0;
  770. cmd = SQ930_CTRL_SET_EXPOSURE;
  771. switch (sd->sensor) {
  772. case SENSOR_ICX098BQ: /* ccd */
  773. case SENSOR_LZ24BP:
  774. min_frclk = sd->sensor == SENSOR_ICX098BQ ? 0x210 : 0x26f;
  775. if (integclks >= min_frclk) {
  776. intstartclk = 0;
  777. frameclks = integclks;
  778. } else {
  779. intstartclk = min_frclk - integclks;
  780. frameclks = min_frclk;
  781. }
  782. buf[i++] = intstartclk >> 8;
  783. buf[i++] = intstartclk;
  784. buf[i++] = frameclks >> 8;
  785. buf[i++] = frameclks;
  786. buf[i++] = sd->gain;
  787. break;
  788. default: /* cmos */
  789. /* case SENSOR_MI0360: */
  790. /* case SENSOR_MT9V111: */
  791. cmd |= 0x0100;
  792. sensor = &sensor_tb[sd->sensor];
  793. buf[i++] = sensor->i2c_addr; /* i2c_slave_addr */
  794. buf[i++] = 0x08; /* 2 * ni2c */
  795. buf[i++] = 0x09; /* reg = shutter width */
  796. buf[i++] = integclks >> 8; /* val H */
  797. buf[i++] = sensor->i2c_dum;
  798. buf[i++] = integclks; /* val L */
  799. buf[i++] = 0x35; /* reg = global gain */
  800. buf[i++] = 0x00; /* val H */
  801. buf[i++] = sensor->i2c_dum;
  802. buf[i++] = sd->gain; /* val L */
  803. buf[i++] = 0x00;
  804. buf[i++] = 0x00;
  805. buf[i++] = 0x00;
  806. buf[i++] = 0x00;
  807. buf[i++] = 0x83;
  808. break;
  809. }
  810. reg_wb(gspca_dev, cmd, 0, buf, i);
  811. }
  812. /* This function is called at probe time just before sd_init */
  813. static int sd_config(struct gspca_dev *gspca_dev,
  814. const struct usb_device_id *id)
  815. {
  816. struct sd *sd = (struct sd *) gspca_dev;
  817. struct cam *cam = &gspca_dev->cam;
  818. sd->sensor = id->driver_info >> 8;
  819. sd->type = id->driver_info;
  820. cam->cam_mode = vga_mode;
  821. cam->nmodes = ARRAY_SIZE(vga_mode);
  822. cam->bulk = 1;
  823. cam->bulk_size = BULK_TRANSFER_LEN;
  824. /* cam->bulk_nurbs = 2; fixme: if no setexpo sync */
  825. sd->quality = QUALITY_DEF;
  826. sd->gain = GAIN_DEF;
  827. sd->expo = EXPO_DEF;
  828. return 0;
  829. }
  830. /* this function is called at probe and resume time */
  831. static int sd_init(struct gspca_dev *gspca_dev)
  832. {
  833. struct sd *sd = (struct sd *) gspca_dev;
  834. sd->gpio[0] = sd->gpio[1] = 0xff; /* force gpio rewrite */
  835. /*fixme: is this needed for icx098bp and mi0360?
  836. if (sd->sensor != SENSOR_LZ24BP)
  837. reg_w(gspca_dev, SQ930_CTRL_RESET, 0x0000);
  838. */
  839. reg_r(gspca_dev, SQ930_CTRL_GET_DEV_INFO, 8);
  840. /* it returns:
  841. * 03 00 12 93 0b f6 c9 00 live! ultra
  842. * 03 00 07 93 0b f6 ca 00 live! ultra for notebook
  843. * 03 00 12 93 0b fe c8 00 Trust WB-3500T
  844. * 02 00 06 93 0b fe c8 00 Joy-IT 318S
  845. * 03 00 12 93 0b f6 cf 00 icam tracer - sensor icx098bq
  846. * 02 00 12 93 0b fe cf 00 ProQ Motion Webcam
  847. *
  848. * byte
  849. * 0: 02 = usb 1.0 (12Mbit) / 03 = usb2.0 (480Mbit)
  850. * 1: 00
  851. * 2: 06 / 07 / 12 = mode webcam? firmware??
  852. * 3: 93 chip = 930b (930b or 930c)
  853. * 4: 0b
  854. * 5: f6 = cdd (icx098bq, lz24bp) / fe or de = cmos (i2c) (other sensors)
  855. * 6: c8 / c9 / ca / cf = mode webcam?, sensor? webcam?
  856. * 7: 00
  857. */
  858. PDEBUG(D_PROBE, "info: %02x %02x %02x %02x %02x %02x %02x %02x",
  859. gspca_dev->usb_buf[0],
  860. gspca_dev->usb_buf[1],
  861. gspca_dev->usb_buf[2],
  862. gspca_dev->usb_buf[3],
  863. gspca_dev->usb_buf[4],
  864. gspca_dev->usb_buf[5],
  865. gspca_dev->usb_buf[6],
  866. gspca_dev->usb_buf[7]);
  867. bridge_init(sd);
  868. if (sd->sensor == SENSOR_MI0360) {
  869. /* no sensor probe for icam tracer */
  870. if (gspca_dev->usb_buf[5] == 0xf6) { /* if CMOS */
  871. sd->sensor = SENSOR_ICX098BQ;
  872. gspca_dev->cam.cam_mode = &vga_mode[1];
  873. gspca_dev->cam.nmodes = 1; /* only 320x240 */
  874. } else {
  875. cmos_probe(gspca_dev);
  876. }
  877. }
  878. PDEBUG(D_PROBE, "Sensor %s", sensor_tb[sd->sensor].name);
  879. global_init(sd, 1);
  880. return gspca_dev->usb_err;
  881. }
  882. /* special function to create the quantization tables of the JPEG header */
  883. static void sd_jpeg_set_qual(u8 *jpeg_hdr,
  884. int quality)
  885. {
  886. int i, sc1, sc2;
  887. quality = quality_tb[quality]; /* convert to JPEG quality */
  888. /*
  889. * approximative qualities for Y and U/V:
  890. * quant = 0:94%/91% 1:91%/87% 2:82%/73% 3:69%/56%
  891. * should have:
  892. * quant = 0:94%/91% 1:91%/87.5% 2:81.5%/72% 3:69%/54.5%
  893. */
  894. sc1 = 200 - quality * 2;
  895. quality = quality * 7 / 5 - 40; /* UV quality */
  896. sc2 = 200 - quality * 2;
  897. for (i = 0; i < 64; i++) {
  898. jpeg_hdr[JPEG_QT0_OFFSET + i] =
  899. (jpeg_head[JPEG_QT0_OFFSET + i] * sc1 + 50) / 100;
  900. jpeg_hdr[JPEG_QT1_OFFSET + i] =
  901. (jpeg_head[JPEG_QT1_OFFSET + i] * sc2 + 50) / 100;
  902. }
  903. }
  904. /* send the start/stop commands to the webcam */
  905. static void send_start(struct gspca_dev *gspca_dev)
  906. {
  907. struct sd *sd = (struct sd *) gspca_dev;
  908. const struct cap_s *cap;
  909. int mode, quality;
  910. mode = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
  911. cap = &capconfig[sd->sensor][mode];
  912. quality = sd->quality;
  913. reg_wb(gspca_dev, (quality << 12)
  914. | 0x0a00 /* 900 for Bayer */
  915. | SQ930_CTRL_CAP_START,
  916. 0x0500 /* a00 for Bayer */
  917. | cap->cc_sizeid,
  918. cap->cc_bytes, 32);
  919. };
  920. static void send_stop(struct gspca_dev *gspca_dev)
  921. {
  922. reg_w(gspca_dev, SQ930_CTRL_CAP_STOP, 0);
  923. };
  924. /* function called at start time before URB creation */
  925. static int sd_isoc_init(struct gspca_dev *gspca_dev)
  926. {
  927. struct sd *sd = (struct sd *) gspca_dev;
  928. gspca_dev->cam.bulk_nurbs = 1; /* there must be one URB only */
  929. sd->do_ctrl = 0;
  930. return 0;
  931. }
  932. /* start the capture */
  933. static int sd_start(struct gspca_dev *gspca_dev)
  934. {
  935. struct sd *sd = (struct sd *) gspca_dev;
  936. int mode;
  937. /* initialize the JPEG header */
  938. jpeg_define(sd->jpeg_hdr, gspca_dev->height, gspca_dev->width,
  939. 0x21); /* JPEG 422 */
  940. sd_jpeg_set_qual(sd->jpeg_hdr, sd->quality);
  941. bridge_init(sd);
  942. global_init(sd, 0);
  943. msleep(100);
  944. switch (sd->sensor) {
  945. case SENSOR_ICX098BQ:
  946. ucbus_write(gspca_dev, icx098bq_start_0,
  947. ARRAY_SIZE(icx098bq_start_0),
  948. 8);
  949. ucbus_write(gspca_dev, icx098bq_start_1,
  950. ARRAY_SIZE(icx098bq_start_1),
  951. 5);
  952. ucbus_write(gspca_dev, icx098bq_start_2,
  953. ARRAY_SIZE(icx098bq_start_2),
  954. 6);
  955. msleep(50);
  956. /* 1st start */
  957. send_start(gspca_dev);
  958. gpio_set(sd, SQ930_GPIO_EXTRA2 | SQ930_GPIO_RSTBAR, 0x00ff);
  959. msleep(70);
  960. reg_w(gspca_dev, SQ930_CTRL_CAP_STOP, 0x0000);
  961. gpio_set(sd, 0x7f, 0x00ff);
  962. /* 2nd start */
  963. send_start(gspca_dev);
  964. gpio_set(sd, SQ930_GPIO_EXTRA2 | SQ930_GPIO_RSTBAR, 0x00ff);
  965. goto out;
  966. case SENSOR_LZ24BP:
  967. ucbus_write(gspca_dev, lz24bp_start_0,
  968. ARRAY_SIZE(lz24bp_start_0),
  969. 8);
  970. if (sd->type != Creative_live_motion)
  971. ucbus_write(gspca_dev, lz24bp_start_1_gen,
  972. ARRAY_SIZE(lz24bp_start_1_gen),
  973. 5);
  974. else
  975. ucbus_write(gspca_dev, lz24bp_start_1_clm,
  976. ARRAY_SIZE(lz24bp_start_1_clm),
  977. 5);
  978. ucbus_write(gspca_dev, lz24bp_start_2,
  979. ARRAY_SIZE(lz24bp_start_2),
  980. 6);
  981. mode = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
  982. lz24bp_ppl(sd, mode == 2 ? 0x0564 : 0x0310);
  983. msleep(10);
  984. break;
  985. case SENSOR_MI0360:
  986. ucbus_write(gspca_dev, mi0360_start_0,
  987. ARRAY_SIZE(mi0360_start_0),
  988. 8);
  989. i2c_write(sd, mi0360_init_23,
  990. ARRAY_SIZE(mi0360_init_23));
  991. i2c_write(sd, mi0360_init_24,
  992. ARRAY_SIZE(mi0360_init_24));
  993. i2c_write(sd, mi0360_init_25,
  994. ARRAY_SIZE(mi0360_init_25));
  995. ucbus_write(gspca_dev, mi0360_start_1,
  996. ARRAY_SIZE(mi0360_start_1),
  997. 5);
  998. i2c_write(sd, mi0360_start_2,
  999. ARRAY_SIZE(mi0360_start_2));
  1000. i2c_write(sd, mi0360_start_3,
  1001. ARRAY_SIZE(mi0360_start_3));
  1002. /* 1st start */
  1003. send_start(gspca_dev);
  1004. msleep(60);
  1005. reg_w(gspca_dev, SQ930_CTRL_CAP_STOP, 0x0000);
  1006. i2c_write(sd,
  1007. mi0360_start_4, ARRAY_SIZE(mi0360_start_4));
  1008. break;
  1009. default:
  1010. /* case SENSOR_MT9V111: */
  1011. ucbus_write(gspca_dev, mi0360_start_0,
  1012. ARRAY_SIZE(mi0360_start_0),
  1013. 8);
  1014. i2c_write(sd, mt9v111_init_0,
  1015. ARRAY_SIZE(mt9v111_init_0));
  1016. i2c_write(sd, mt9v111_init_1,
  1017. ARRAY_SIZE(mt9v111_init_1));
  1018. i2c_write(sd, mt9v111_init_2,
  1019. ARRAY_SIZE(mt9v111_init_2));
  1020. ucbus_write(gspca_dev, mt9v111_start_1,
  1021. ARRAY_SIZE(mt9v111_start_1),
  1022. 8);
  1023. i2c_write(sd, mt9v111_init_3,
  1024. ARRAY_SIZE(mt9v111_init_3));
  1025. i2c_write(sd, mt9v111_init_4,
  1026. ARRAY_SIZE(mt9v111_init_4));
  1027. break;
  1028. }
  1029. send_start(gspca_dev);
  1030. out:
  1031. msleep(1000);
  1032. sd->eof_len = 0; /* init packet scan */
  1033. if (sd->sensor == SENSOR_MT9V111)
  1034. gpio_set(sd, SQ930_GPIO_DFL_LED, SQ930_GPIO_DFL_LED);
  1035. sd->do_ctrl = 1; /* set the exposure */
  1036. return gspca_dev->usb_err;
  1037. }
  1038. static void sd_stopN(struct gspca_dev *gspca_dev)
  1039. {
  1040. struct sd *sd = (struct sd *) gspca_dev;
  1041. if (sd->sensor == SENSOR_MT9V111)
  1042. gpio_set(sd, 0, SQ930_GPIO_DFL_LED);
  1043. send_stop(gspca_dev);
  1044. }
  1045. /* function called when the application gets a new frame */
  1046. /* It sets the exposure if required and restart the bulk transfer. */
  1047. static void sd_dq_callback(struct gspca_dev *gspca_dev)
  1048. {
  1049. struct sd *sd = (struct sd *) gspca_dev;
  1050. int ret;
  1051. if (!sd->do_ctrl || gspca_dev->cam.bulk_nurbs != 0)
  1052. return;
  1053. sd->do_ctrl = 0;
  1054. setexposure(gspca_dev);
  1055. gspca_dev->cam.bulk_nurbs = 1;
  1056. ret = usb_submit_urb(gspca_dev->urb[0], GFP_ATOMIC);
  1057. if (ret < 0)
  1058. PDEBUG(D_ERR|D_PACK, "sd_dq_callback() err %d", ret);
  1059. /* wait a little time, otherwise the webcam crashes */
  1060. msleep(100);
  1061. }
  1062. /* move a packet adding 0x00 after 0xff */
  1063. static void add_packet(struct gspca_dev *gspca_dev,
  1064. u8 *data,
  1065. int len)
  1066. {
  1067. int i;
  1068. i = 0;
  1069. do {
  1070. if (data[i] == 0xff) {
  1071. gspca_frame_add(gspca_dev, INTER_PACKET,
  1072. data, i + 1);
  1073. len -= i;
  1074. data += i;
  1075. *data = 0x00;
  1076. i = 0;
  1077. }
  1078. } while (++i < len);
  1079. gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
  1080. }
  1081. /* end a frame and start a new one */
  1082. static void eof_sof(struct gspca_dev *gspca_dev)
  1083. {
  1084. struct sd *sd = (struct sd *) gspca_dev;
  1085. static const u8 ffd9[] = {0xff, 0xd9};
  1086. /* if control set, stop bulk transfer */
  1087. if (sd->do_ctrl
  1088. && gspca_dev->last_packet_type == INTER_PACKET)
  1089. gspca_dev->cam.bulk_nurbs = 0;
  1090. gspca_frame_add(gspca_dev, LAST_PACKET,
  1091. ffd9, 2);
  1092. gspca_frame_add(gspca_dev, FIRST_PACKET,
  1093. sd->jpeg_hdr, JPEG_HDR_SZ);
  1094. }
  1095. static void sd_pkt_scan(struct gspca_dev *gspca_dev,
  1096. u8 *data, /* isoc packet */
  1097. int len) /* iso packet length */
  1098. {
  1099. struct sd *sd = (struct sd *) gspca_dev;
  1100. u8 *p;
  1101. int l;
  1102. len -= 8; /* ignore last 8 bytes (00 00 55 aa 55 aa 00 00) */
  1103. /*
  1104. * the end/start of frame is indicated by
  1105. * 0x00 * 16 - 0xab * 8
  1106. * aligned on 8 bytes boundary
  1107. */
  1108. if (sd->eof_len != 0) { /* if 'abababab' in previous pkt */
  1109. if (*((u32 *) data) == 0xabababab) {
  1110. /*fixme: should remove previous 0000ababab*/
  1111. eof_sof(gspca_dev);
  1112. data += 4;
  1113. len -= 4;
  1114. }
  1115. sd->eof_len = 0;
  1116. }
  1117. p = data;
  1118. l = len;
  1119. for (;;) {
  1120. if (*((u32 *) p) == 0xabababab) {
  1121. if (l < 8) { /* (may be 4 only) */
  1122. sd->eof_len = 1;
  1123. break;
  1124. }
  1125. if (*((u32 *) p + 1) == 0xabababab) {
  1126. add_packet(gspca_dev, data, p - data - 16);
  1127. /* remove previous zeros */
  1128. eof_sof(gspca_dev);
  1129. p += 8;
  1130. l -= 8;
  1131. if (l <= 0)
  1132. return;
  1133. len = l;
  1134. data = p;
  1135. continue;
  1136. }
  1137. }
  1138. p += 4;
  1139. l -= 4;
  1140. if (l <= 0)
  1141. break;
  1142. }
  1143. add_packet(gspca_dev, data, len);
  1144. }
  1145. static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val)
  1146. {
  1147. struct sd *sd = (struct sd *) gspca_dev;
  1148. sd->gain = val;
  1149. if (gspca_dev->streaming)
  1150. sd->do_ctrl = 1;
  1151. return 0;
  1152. }
  1153. static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val)
  1154. {
  1155. struct sd *sd = (struct sd *) gspca_dev;
  1156. *val = sd->gain;
  1157. return 0;
  1158. }
  1159. static int sd_setexpo(struct gspca_dev *gspca_dev, __s32 val)
  1160. {
  1161. struct sd *sd = (struct sd *) gspca_dev;
  1162. sd->expo = val;
  1163. if (gspca_dev->streaming)
  1164. sd->do_ctrl = 1;
  1165. return 0;
  1166. }
  1167. static int sd_getexpo(struct gspca_dev *gspca_dev, __s32 *val)
  1168. {
  1169. struct sd *sd = (struct sd *) gspca_dev;
  1170. *val = sd->expo;
  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. int quality;
  1178. if (jcomp->quality >= (QUAL_0 + QUAL_1) / 2)
  1179. quality = 0;
  1180. else if (jcomp->quality >= (QUAL_1 + QUAL_2) / 2)
  1181. quality = 1;
  1182. else if (jcomp->quality >= (QUAL_2 + QUAL_3) / 2)
  1183. quality = 2;
  1184. else
  1185. quality = 3;
  1186. if (quality != sd->quality) {
  1187. sd->quality = quality;
  1188. if (gspca_dev->streaming) {
  1189. send_stop(gspca_dev);
  1190. sd_jpeg_set_qual(sd->jpeg_hdr, sd->quality);
  1191. msleep(70);
  1192. send_start(gspca_dev);
  1193. }
  1194. }
  1195. return gspca_dev->usb_err;
  1196. }
  1197. static int sd_get_jcomp(struct gspca_dev *gspca_dev,
  1198. struct v4l2_jpegcompression *jcomp)
  1199. {
  1200. struct sd *sd = (struct sd *) gspca_dev;
  1201. memset(jcomp, 0, sizeof *jcomp);
  1202. jcomp->quality = quality_tb[sd->quality];
  1203. jcomp->jpeg_markers = V4L2_JPEG_MARKER_DHT
  1204. | V4L2_JPEG_MARKER_DQT;
  1205. return 0;
  1206. }
  1207. /* sub-driver description */
  1208. static const struct sd_desc sd_desc = {
  1209. .name = MODULE_NAME,
  1210. .ctrls = sd_ctrls,
  1211. .nctrls = ARRAY_SIZE(sd_ctrls),
  1212. .config = sd_config,
  1213. .init = sd_init,
  1214. .isoc_init = sd_isoc_init,
  1215. .start = sd_start,
  1216. .stopN = sd_stopN,
  1217. .pkt_scan = sd_pkt_scan,
  1218. .dq_callback = sd_dq_callback,
  1219. .get_jcomp = sd_get_jcomp,
  1220. .set_jcomp = sd_set_jcomp,
  1221. };
  1222. /* Table of supported USB devices */
  1223. #define ST(sensor, type) \
  1224. .driver_info = (SENSOR_ ## sensor << 8) \
  1225. | (type)
  1226. static const __devinitdata struct usb_device_id device_table[] = {
  1227. {USB_DEVICE(0x041e, 0x4038), ST(MI0360, 0)},
  1228. {USB_DEVICE(0x041e, 0x403c), ST(LZ24BP, 0)},
  1229. {USB_DEVICE(0x041e, 0x403d), ST(LZ24BP, 0)},
  1230. {USB_DEVICE(0x041e, 0x4041), ST(LZ24BP, Creative_live_motion)},
  1231. {USB_DEVICE(0x2770, 0x930b), ST(MI0360, 0)},
  1232. {USB_DEVICE(0x2770, 0x930c), ST(MI0360, 0)},
  1233. {}
  1234. };
  1235. MODULE_DEVICE_TABLE(usb, device_table);
  1236. /* -- device connect -- */
  1237. static int sd_probe(struct usb_interface *intf,
  1238. const struct usb_device_id *id)
  1239. {
  1240. return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
  1241. THIS_MODULE);
  1242. }
  1243. static struct usb_driver sd_driver = {
  1244. .name = MODULE_NAME,
  1245. .id_table = device_table,
  1246. .probe = sd_probe,
  1247. .disconnect = gspca_disconnect,
  1248. #ifdef CONFIG_PM
  1249. .suspend = gspca_suspend,
  1250. .resume = gspca_resume,
  1251. #endif
  1252. };
  1253. /* -- module insert / remove -- */
  1254. static int __init sd_mod_init(void)
  1255. {
  1256. int ret;
  1257. ret = usb_register(&sd_driver);
  1258. if (ret < 0)
  1259. return ret;
  1260. info("registered");
  1261. return 0;
  1262. }
  1263. static void __exit sd_mod_exit(void)
  1264. {
  1265. usb_deregister(&sd_driver);
  1266. info("deregistered");
  1267. }
  1268. module_init(sd_mod_init);
  1269. module_exit(sd_mod_exit);