sq930x.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140
  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_MI0360,
  45. SENSOR_LZ24BP,
  46. } sensors;
  47. u8 type;
  48. #define Generic 0
  49. #define Creative_live_motion 1
  50. u8 jpeg_hdr[JPEG_HDR_SZ];
  51. };
  52. static int sd_setexpo(struct gspca_dev *gspca_dev, __s32 val);
  53. static int sd_getexpo(struct gspca_dev *gspca_dev, __s32 *val);
  54. static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val);
  55. static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val);
  56. static const struct ctrl sd_ctrls[] = {
  57. {
  58. {
  59. .id = V4L2_CID_EXPOSURE,
  60. .type = V4L2_CTRL_TYPE_INTEGER,
  61. .name = "Exposure",
  62. .minimum = 0x0001,
  63. .maximum = 0x0fff,
  64. .step = 1,
  65. #define EXPO_DEF 0x027d
  66. .default_value = EXPO_DEF,
  67. },
  68. .set = sd_setexpo,
  69. .get = sd_getexpo,
  70. },
  71. {
  72. {
  73. .id = V4L2_CID_GAIN,
  74. .type = V4L2_CTRL_TYPE_INTEGER,
  75. .name = "Gain",
  76. .minimum = 0x01,
  77. .maximum = 0xff,
  78. .step = 1,
  79. #define GAIN_DEF 0x61
  80. .default_value = GAIN_DEF,
  81. },
  82. .set = sd_setgain,
  83. .get = sd_getgain,
  84. },
  85. };
  86. static struct v4l2_pix_format vga_mode[] = {
  87. {160, 120, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
  88. .bytesperline = 160,
  89. .sizeimage = 160 * 120 * 5 / 8 + 590,
  90. .colorspace = V4L2_COLORSPACE_JPEG,
  91. .priv = 0},
  92. {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
  93. .bytesperline = 320,
  94. .sizeimage = 320 * 240 * 4 / 8 + 590,
  95. .colorspace = V4L2_COLORSPACE_JPEG,
  96. .priv = 1},
  97. {640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
  98. .bytesperline = 640,
  99. .sizeimage = 640 * 480 * 3 / 8 + 590,
  100. .colorspace = V4L2_COLORSPACE_JPEG,
  101. .priv = 2},
  102. };
  103. /* JPEG quality indexed by webcam quality */
  104. #define QUAL_0 90
  105. #define QUAL_1 85
  106. #define QUAL_2 75
  107. #define QUAL_3 70
  108. static const u8 quality_tb[4] = { QUAL_0, QUAL_1, QUAL_2, QUAL_3 };
  109. /* sq930x registers */
  110. #define SQ930_CTRL_UCBUS_IO 0x0001
  111. #define SQ930_CTRL_I2C_IO 0x0002
  112. #define SQ930_CTRL_GPIO 0x0005
  113. #define SQ930_CTRL_CAP_START 0x0010
  114. #define SQ930_CTRL_CAP_STOP 0x0011
  115. #define SQ930_CTRL_SET_EXPOSURE 0x001d
  116. #define SQ930_CTRL_RESET 0x001e
  117. #define SQ930_CTRL_GET_DEV_INFO 0x001f
  118. /* gpio 1 (8..15) */
  119. #define SQ930_GPIO_DFL_I2C_SDA 0x0001
  120. #define SQ930_GPIO_DFL_I2C_SCL 0x0002
  121. #define SQ930_GPIO_RSTBAR 0x0004
  122. #define SQ930_GPIO_EXTRA1 0x0040
  123. #define SQ930_GPIO_EXTRA2 0x0080
  124. /* gpio 3 (24..31) */
  125. #define SQ930_GPIO_POWER 0x0200
  126. #define SQ930_GPIO_DFL_LED 0x1000
  127. struct ucbus_write_cmd {
  128. u16 bw_addr;
  129. u8 bw_data;
  130. };
  131. struct i2c_write_cmd {
  132. u8 reg;
  133. u16 val;
  134. };
  135. static const struct ucbus_write_cmd icx098bq_start_0[] = {
  136. {0x0354, 0x00}, {0x03fa, 0x00}, {0xf800, 0x02}, {0xf801, 0xce},
  137. {0xf802, 0xc1}, {0xf804, 0x00}, {0xf808, 0x00}, {0xf809, 0x0e},
  138. {0xf80a, 0x01}, {0xf80b, 0xee}, {0xf807, 0x60}, {0xf80c, 0x02},
  139. {0xf80d, 0xf0}, {0xf80e, 0x03}, {0xf80f, 0x0a}, {0xf81c, 0x02},
  140. {0xf81d, 0xf0}, {0xf81e, 0x03}, {0xf81f, 0x0a}, {0xf83a, 0x00},
  141. {0xf83b, 0x10}, {0xf83c, 0x00}, {0xf83d, 0x4e}, {0xf810, 0x04},
  142. {0xf811, 0x00}, {0xf812, 0x02}, {0xf813, 0x10}, {0xf803, 0x00},
  143. {0xf814, 0x01}, {0xf815, 0x18}, {0xf816, 0x00}, {0xf817, 0x48},
  144. {0xf818, 0x00}, {0xf819, 0x25}, {0xf81a, 0x00}, {0xf81b, 0x3c},
  145. {0xf82f, 0x03}, {0xf820, 0xff}, {0xf821, 0x0d}, {0xf822, 0xff},
  146. {0xf823, 0x07}, {0xf824, 0xff}, {0xf825, 0x03}, {0xf826, 0xff},
  147. {0xf827, 0x06}, {0xf828, 0xff}, {0xf829, 0x03}, {0xf82a, 0xff},
  148. {0xf82b, 0x0c}, {0xf82c, 0xfd}, {0xf82d, 0x01}, {0xf82e, 0x00},
  149. {0xf830, 0x00}, {0xf831, 0x47}, {0xf832, 0x00}, {0xf833, 0x00},
  150. {0xf850, 0x00}, {0xf851, 0x00}, {0xf852, 0x00}, {0xf853, 0x24},
  151. {0xf854, 0x00}, {0xf855, 0x18}, {0xf856, 0x00}, {0xf857, 0x3c},
  152. {0xf858, 0x00}, {0xf859, 0x0c}, {0xf85a, 0x00}, {0xf85b, 0x30},
  153. {0xf85c, 0x00}, {0xf85d, 0x0c}, {0xf85e, 0x00}, {0xf85f, 0x30},
  154. {0xf860, 0x00}, {0xf861, 0x48}, {0xf862, 0x01}, {0xf863, 0xdc},
  155. {0xf864, 0xff}, {0xf865, 0x98}, {0xf866, 0xff}, {0xf867, 0xc0},
  156. {0xf868, 0xff}, {0xf869, 0x70}, {0xf86c, 0xff}, {0xf86d, 0x00},
  157. {0xf86a, 0xff}, {0xf86b, 0x48}, {0xf86e, 0xff}, {0xf86f, 0x00},
  158. {0xf870, 0x01}, {0xf871, 0xdb}, {0xf872, 0x01}, {0xf873, 0xfa},
  159. {0xf874, 0x01}, {0xf875, 0xdb}, {0xf876, 0x01}, {0xf877, 0xfa},
  160. {0xf878, 0x0f}, {0xf879, 0x0f}, {0xf87a, 0xff}, {0xf87b, 0xff},
  161. {0xf800, 0x03}
  162. };
  163. static const struct ucbus_write_cmd icx098bq_start_1[] = {
  164. {0xf5f0, 0x00}, {0xf5f1, 0xcd}, {0xf5f2, 0x80}, {0xf5f3, 0x80},
  165. {0xf5f4, 0xc0},
  166. {0xf5f0, 0x49}, {0xf5f1, 0xcd}, {0xf5f2, 0x80}, {0xf5f3, 0x80},
  167. {0xf5f4, 0xc0},
  168. {0xf5fa, 0x00}, {0xf5f6, 0x00}, {0xf5f7, 0x00}, {0xf5f8, 0x00},
  169. {0xf5f9, 0x00}
  170. };
  171. static const struct ucbus_write_cmd icx098bq_start_2[] = {
  172. {0xf800, 0x02}, {0xf807, 0xff}, {0xf805, 0x82}, {0xf806, 0x00},
  173. {0xf807, 0x7f}, {0xf800, 0x03},
  174. {0xf800, 0x02}, {0xf807, 0xff}, {0xf805, 0x40}, {0xf806, 0x00},
  175. {0xf807, 0x7f}, {0xf800, 0x03},
  176. {0xf800, 0x02}, {0xf807, 0xff}, {0xf805, 0xcf}, {0xf806, 0xd0},
  177. {0xf807, 0x7f}, {0xf800, 0x03},
  178. {0xf800, 0x02}, {0xf807, 0xff}, {0xf805, 0x00}, {0xf806, 0x00},
  179. {0xf807, 0x7f}, {0xf800, 0x03}
  180. };
  181. static const struct ucbus_write_cmd lz24bp_start_0[] = {
  182. {0x0354, 0x00}, {0x03fa, 0x00}, {0xf800, 0x02}, {0xf801, 0xbe},
  183. {0xf802, 0xc6}, {0xf804, 0x00}, {0xf808, 0x00}, {0xf809, 0x06},
  184. {0xf80a, 0x01}, {0xf80b, 0xfe}, {0xf807, 0x84}, {0xf80c, 0x02},
  185. {0xf80d, 0xf7}, {0xf80e, 0x03}, {0xf80f, 0x0b}, {0xf81c, 0x00},
  186. {0xf81d, 0x49}, {0xf81e, 0x03}, {0xf81f, 0x0b}, {0xf83a, 0x00},
  187. {0xf83b, 0x01}, {0xf83c, 0x00}, {0xf83d, 0x6b}, {0xf810, 0x03},
  188. {0xf811, 0x10}, {0xf812, 0x02}, {0xf813, 0x6f}, {0xf803, 0x00},
  189. {0xf814, 0x00}, {0xf815, 0x44}, {0xf816, 0x00}, {0xf817, 0x48},
  190. {0xf818, 0x00}, {0xf819, 0x25}, {0xf81a, 0x00}, {0xf81b, 0x3c},
  191. {0xf82f, 0x03}, {0xf820, 0xff}, {0xf821, 0x0d}, {0xf822, 0xff},
  192. {0xf823, 0x07}, {0xf824, 0xfd}, {0xf825, 0x07}, {0xf826, 0xf0},
  193. {0xf827, 0x0c}, {0xf828, 0xff}, {0xf829, 0x03}, {0xf82a, 0xff},
  194. {0xf82b, 0x0c}, {0xf82c, 0xfc}, {0xf82d, 0x01}, {0xf82e, 0x00},
  195. {0xf830, 0x00}, {0xf831, 0x47}, {0xf832, 0x00}, {0xf833, 0x00},
  196. {0xf850, 0x00}, {0xf851, 0x00}, {0xf852, 0x00}, {0xf853, 0x24},
  197. {0xf854, 0x00}, {0xf855, 0x0c}, {0xf856, 0x00}, {0xf857, 0x30},
  198. {0xf858, 0x00}, {0xf859, 0x18}, {0xf85a, 0x00}, {0xf85b, 0x3c},
  199. {0xf85c, 0x00}, {0xf85d, 0x18}, {0xf85e, 0x00}, {0xf85f, 0x3c},
  200. {0xf860, 0xff}, {0xf861, 0x37}, {0xf862, 0xff}, {0xf863, 0x1d},
  201. {0xf864, 0xff}, {0xf865, 0x98}, {0xf866, 0xff}, {0xf867, 0xc0},
  202. {0xf868, 0x00}, {0xf869, 0x37}, {0xf86c, 0x02}, {0xf86d, 0x1d},
  203. {0xf86a, 0x00}, {0xf86b, 0x37}, {0xf86e, 0x02}, {0xf86f, 0x1d},
  204. {0xf870, 0x01}, {0xf871, 0xc6}, {0xf872, 0x02}, {0xf873, 0x04},
  205. {0xf874, 0x01}, {0xf875, 0xc6}, {0xf876, 0x02}, {0xf877, 0x04},
  206. {0xf878, 0x0f}, {0xf879, 0x0f}, {0xf87a, 0xff}, {0xf87b, 0xff},
  207. {0xf800, 0x03}
  208. };
  209. static const struct ucbus_write_cmd lz24bp_start_1_gen[] = {
  210. {0xf5f0, 0x00}, {0xf5f1, 0xff}, {0xf5f2, 0x80}, {0xf5f3, 0x80},
  211. {0xf5f4, 0xb3},
  212. {0xf5f0, 0x40}, {0xf5f1, 0xff}, {0xf5f2, 0x80}, {0xf5f3, 0x80},
  213. {0xf5f4, 0xb3},
  214. {0xf5fa, 0x00}, {0xf5f6, 0x00}, {0xf5f7, 0x00}, {0xf5f8, 0x00},
  215. {0xf5f9, 0x00}
  216. };
  217. static const struct ucbus_write_cmd lz24bp_start_1_clm[] = {
  218. {0xf5f0, 0x00}, {0xf5f1, 0xff}, {0xf5f2, 0x88}, {0xf5f3, 0x88},
  219. {0xf5f4, 0xc0},
  220. {0xf5f0, 0x40}, {0xf5f1, 0xff}, {0xf5f2, 0x88}, {0xf5f3, 0x88},
  221. {0xf5f4, 0xc0},
  222. {0xf5fa, 0x00}, {0xf5f6, 0x00}, {0xf5f7, 0x00}, {0xf5f8, 0x00},
  223. {0xf5f9, 0x00}
  224. };
  225. static const struct ucbus_write_cmd lz24bp_start_2[] = {
  226. {0xf800, 0x02}, {0xf807, 0xff}, {0xf805, 0x80}, {0xf806, 0x00},
  227. {0xf807, 0x7f}, {0xf800, 0x03},
  228. {0xf800, 0x02}, {0xf807, 0xff}, {0xf805, 0x4e}, {0xf806, 0x00},
  229. {0xf807, 0x7f}, {0xf800, 0x03},
  230. {0xf800, 0x02}, {0xf807, 0xff}, {0xf805, 0xc0}, {0xf806, 0x48},
  231. {0xf807, 0x7f}, {0xf800, 0x03},
  232. {0xf800, 0x02}, {0xf807, 0xff}, {0xf805, 0x00}, {0xf806, 0x00},
  233. {0xf807, 0x7f}, {0xf800, 0x03}
  234. };
  235. static const struct ucbus_write_cmd mi0360_start_0[] = {
  236. {0x0354, 0x00}, {0x03fa, 0x00}, {0xf332, 0xcc}, {0xf333, 0xcc},
  237. {0xf334, 0xcc}, {0xf335, 0xcc}, {0xf33f, 0x00}
  238. };
  239. static const struct i2c_write_cmd mi0360_init_23[] = {
  240. {0x30, 0x0040}, /* reserved - def 0x0005 */
  241. {0x31, 0x0000}, /* reserved - def 0x002a */
  242. {0x34, 0x0100}, /* reserved - def 0x0100 */
  243. {0x3d, 0x068f}, /* reserved - def 0x068f */
  244. };
  245. static const struct i2c_write_cmd mi0360_init_24[] = {
  246. {0x03, 0x01e5}, /* window height */
  247. {0x04, 0x0285}, /* window width */
  248. };
  249. static const struct i2c_write_cmd mi0360_init_25[] = {
  250. {0x35, 0x0020}, /* global gain */
  251. {0x2b, 0x0020}, /* green1 gain */
  252. {0x2c, 0x002a}, /* blue gain */
  253. {0x2d, 0x0028}, /* red gain */
  254. {0x2e, 0x0020}, /* green2 gain */
  255. };
  256. static const struct ucbus_write_cmd mi0360_start_1[] = {
  257. {0xf5f0, 0x11}, {0xf5f1, 0x99}, {0xf5f2, 0x80}, {0xf5f3, 0x80},
  258. {0xf5f4, 0xa6},
  259. {0xf5f0, 0x51}, {0xf5f1, 0x99}, {0xf5f2, 0x80}, {0xf5f3, 0x80},
  260. {0xf5f4, 0xa6},
  261. {0xf5fa, 0x00}, {0xf5f6, 0x00}, {0xf5f7, 0x00}, {0xf5f8, 0x00},
  262. {0xf5f9, 0x00}
  263. };
  264. static const struct i2c_write_cmd mi0360_start_2[] = {
  265. {0x62, 0x041d}, /* reserved - def 0x0418 */
  266. };
  267. static const struct i2c_write_cmd mi0360_start_3[] = {
  268. {0x05, 0x007b}, /* horiz blanking */
  269. };
  270. static const struct i2c_write_cmd mi0360_start_4[] = {
  271. {0x05, 0x03f5}, /* horiz blanking */
  272. };
  273. static const struct cap_s {
  274. u8 cc_sizeid;
  275. u8 cc_bytes[32];
  276. } capconfig[3][3] = {
  277. [SENSOR_ICX098BQ] = {
  278. {0, /* JPEG, 160x120 */
  279. {0x01, 0x1f, 0x20, 0x0e, 0x00, 0x9f, 0x02, 0xee,
  280. 0x01, 0x02, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
  281. 0x02, 0x8b, 0x00, 0x8b, 0x00, 0x41, 0x01, 0x41,
  282. 0x01, 0x41, 0x01, 0x05, 0x40, 0x01, 0xf0, 0x00} },
  283. {2, /* JPEG, 320x240 */
  284. {0x01, 0x1f, 0x20, 0x0e, 0x00, 0x9f, 0x02, 0xee,
  285. 0x01, 0x02, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
  286. 0x02, 0xdf, 0x01, 0x00, 0x00, 0x3f, 0x01, 0x3f,
  287. 0x01, 0x00, 0x00, 0x05, 0x40, 0x01, 0xf0, 0x00} },
  288. {4, /* JPEG, 640x480 */
  289. {0x01, 0x22, 0x20, 0x0e, 0x00, 0xa2, 0x02, 0xf0,
  290. 0x01, 0x02, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
  291. 0x07, 0xe1, 0x01, 0xe1, 0x01, 0x3f, 0x01, 0x3f,
  292. 0x01, 0x3f, 0x01, 0x05, 0x80, 0x02, 0xe0, 0x01} },
  293. },
  294. [SENSOR_LZ24BP] = {
  295. {0, /* JPEG, 160x120 */
  296. {0x01, 0x1f, 0x20, 0x0e, 0x00, 0x9f, 0x02, 0xee,
  297. 0x01, 0x02, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
  298. 0x02, 0x8b, 0x00, 0x8b, 0x00, 0x41, 0x01, 0x41,
  299. 0x01, 0x41, 0x01, 0x05, 0x40, 0x01, 0xf0, 0x00} },
  300. {2, /* JPEG, 320x240 */
  301. {0x01, 0x22, 0x20, 0x0e, 0x00, 0xa2, 0x02, 0xee,
  302. 0x01, 0x02, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
  303. 0x02, 0xdf, 0x01, 0x00, 0x00, 0x3f, 0x01, 0x3f,
  304. 0x01, 0x00, 0x00, 0x05, 0x40, 0x01, 0xf0, 0x00} },
  305. {4, /* JPEG, 640x480 */
  306. {0x01, 0x22, 0x20, 0x0e, 0x00, 0xa2, 0x02, 0xf0,
  307. 0x01, 0x02, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
  308. 0x07, 0xe1, 0x01, 0xe1, 0x01, 0x3f, 0x01, 0x3f,
  309. 0x01, 0x3f, 0x01, 0x05, 0x80, 0x02, 0xe0, 0x01} },
  310. },
  311. [SENSOR_MI0360] = {
  312. {0, /* JPEG, 160x120 */
  313. {0x05, 0x3d, 0x20, 0x0b, 0x00, 0xbd, 0x02, 0x0b,
  314. 0x02, 0x02, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
  315. 0x02, 0x01, 0x01, 0x01, 0x01, 0x9f, 0x00, 0x9f,
  316. 0x00, 0x9f, 0x01, 0x05, 0xa0, 0x00, 0x80, 0x00} },
  317. {2, /* JPEG, 320x240 */
  318. {0x01, 0x02, 0x20, 0x01, 0x20, 0x82, 0x02, 0xe1,
  319. /*fixme 03 e3 */
  320. 0x01, 0x02, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
  321. 0x02, 0xdf, 0x01, 0x00, 0x00, 0x3f, 0x01, 0x3f,
  322. 0x01, 0x00, 0x00, 0x05, 0x40, 0x01, 0xf0, 0x00} },
  323. {4, /* JPEG, 640x480 */
  324. {0x01, 0x02, 0x20, 0x01, 0x20, 0x82, 0x02, 0xe3,
  325. 0x01, 0x02, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
  326. 0x07, 0xe1, 0x01, 0xe1, 0x01, 0x3f, 0x01, 0x3f,
  327. 0x01, 0x3f, 0x01, 0x05, 0x80, 0x02, 0xe0, 0x01} },
  328. },
  329. };
  330. static void reg_r(struct gspca_dev *gspca_dev,
  331. u16 value, int len)
  332. {
  333. usb_control_msg(gspca_dev->dev,
  334. usb_rcvctrlpipe(gspca_dev->dev, 0),
  335. 0x0c,
  336. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  337. value, 0, gspca_dev->usb_buf, len,
  338. 500);
  339. }
  340. static void reg_w(struct gspca_dev *gspca_dev, u16 value, u16 index)
  341. {
  342. int ret;
  343. if (gspca_dev->usb_err < 0)
  344. return;
  345. PDEBUG(D_USBO, "reg_w v: %04x i: %04x", value, index);
  346. ret = usb_control_msg(gspca_dev->dev,
  347. usb_sndctrlpipe(gspca_dev->dev, 0),
  348. 0x0c, /* request */
  349. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  350. value, index, NULL, 0,
  351. 500);
  352. msleep(30);
  353. if (ret < 0) {
  354. PDEBUG(D_ERR, "reg_w %04x %04x failed %d", value, index, ret);
  355. gspca_dev->usb_err = ret;
  356. }
  357. }
  358. static void reg_wb(struct gspca_dev *gspca_dev, u16 value, u16 index,
  359. const u8 *data, int len)
  360. {
  361. int ret;
  362. if (gspca_dev->usb_err < 0)
  363. return;
  364. PDEBUG(D_USBO, "reg_wb v: %04x i: %04x %02x...%02x",
  365. value, index, *data, data[len - 1]);
  366. memcpy(gspca_dev->usb_buf, data, len);
  367. ret = usb_control_msg(gspca_dev->dev,
  368. usb_sndctrlpipe(gspca_dev->dev, 0),
  369. 0x0c, /* request */
  370. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  371. value, index, gspca_dev->usb_buf, len,
  372. 1000);
  373. msleep(30);
  374. if (ret < 0) {
  375. PDEBUG(D_ERR, "reg_wb %04x %04x failed %d", value, index, ret);
  376. gspca_dev->usb_err = ret;
  377. }
  378. }
  379. static void i2c_write(struct gspca_dev *gspca_dev,
  380. const struct i2c_write_cmd *cmd,
  381. int ncmds)
  382. {
  383. u16 val, idx;
  384. u8 *buf;
  385. int ret;
  386. if (gspca_dev->usb_err < 0)
  387. return;
  388. val = (0x5d << 8) | SQ930_CTRL_I2C_IO; /* 0x5d = mi0360 i2c addr */
  389. idx = (cmd->val & 0xff00) | cmd->reg;
  390. buf = gspca_dev->usb_buf;
  391. *buf++ = 0x80;
  392. *buf++ = cmd->val;
  393. while (--ncmds > 0) {
  394. cmd++;
  395. *buf++ = cmd->reg;
  396. *buf++ = cmd->val >> 8;
  397. *buf++ = 0x80;
  398. *buf++ = cmd->val;
  399. }
  400. PDEBUG(D_USBO, "i2c_w v: %04x i: %04x %02x...%02x",
  401. val, idx, gspca_dev->usb_buf[0], buf[-1]);
  402. ret = usb_control_msg(gspca_dev->dev,
  403. usb_sndctrlpipe(gspca_dev->dev, 0),
  404. 0x0c, /* request */
  405. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  406. val, idx,
  407. gspca_dev->usb_buf, buf - gspca_dev->usb_buf,
  408. 500);
  409. if (ret < 0) {
  410. PDEBUG(D_ERR, "i2c_write failed %d", ret);
  411. gspca_dev->usb_err = ret;
  412. }
  413. }
  414. static void ucbus_write(struct gspca_dev *gspca_dev,
  415. const struct ucbus_write_cmd *cmd,
  416. int ncmds,
  417. int batchsize)
  418. {
  419. u8 *buf;
  420. u16 val, idx;
  421. int len, ret;
  422. if (gspca_dev->usb_err < 0)
  423. return;
  424. #ifdef GSPCA_DEBUG
  425. if ((batchsize - 1) * 3 > USB_BUF_SZ) {
  426. err("Bug: usb_buf overflow");
  427. gspca_dev->usb_err = -ENOMEM;
  428. return;
  429. }
  430. #endif
  431. for (;;) {
  432. len = ncmds;
  433. if (len > batchsize)
  434. len = batchsize;
  435. ncmds -= len;
  436. val = (cmd->bw_addr << 8) | SQ930_CTRL_UCBUS_IO;
  437. idx = (cmd->bw_data << 8) | (cmd->bw_addr >> 8);
  438. buf = gspca_dev->usb_buf;
  439. while (--len > 0) {
  440. cmd++;
  441. *buf++ = cmd->bw_addr;
  442. *buf++ = cmd->bw_addr >> 8;
  443. *buf++ = cmd->bw_data;
  444. }
  445. if (buf != gspca_dev->usb_buf)
  446. PDEBUG(D_USBO, "ucbus v: %04x i: %04x %02x...%02x",
  447. val, idx,
  448. gspca_dev->usb_buf[0], buf[-1]);
  449. else
  450. PDEBUG(D_USBO, "ucbus v: %04x i: %04x",
  451. val, idx);
  452. ret = usb_control_msg(gspca_dev->dev,
  453. usb_sndctrlpipe(gspca_dev->dev, 0),
  454. 0x0c, /* request */
  455. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  456. val, idx,
  457. gspca_dev->usb_buf, buf - gspca_dev->usb_buf,
  458. 500);
  459. if (ret < 0) {
  460. PDEBUG(D_ERR, "ucbus_write failed %d", ret);
  461. gspca_dev->usb_err = ret;
  462. return;
  463. }
  464. msleep(30);
  465. if (ncmds <= 0)
  466. break;
  467. cmd++;
  468. }
  469. }
  470. static void gpio_set(struct sd *sd, u16 val, u16 mask)
  471. {
  472. struct gspca_dev *gspca_dev = &sd->gspca_dev;
  473. if (mask & 0x00ff) {
  474. sd->gpio[0] &= ~mask;
  475. sd->gpio[0] |= val;
  476. reg_w(gspca_dev, 0x0100 | SQ930_CTRL_GPIO,
  477. ~sd->gpio[0] << 8);
  478. }
  479. mask >>= 8;
  480. val >>= 8;
  481. if (mask) {
  482. sd->gpio[1] &= ~mask;
  483. sd->gpio[1] |= val;
  484. reg_w(gspca_dev, 0x0300 | SQ930_CTRL_GPIO,
  485. ~sd->gpio[1] << 8);
  486. }
  487. }
  488. static void global_init(struct sd *sd, int first_time)
  489. {
  490. static const struct ucbus_write_cmd clkfreq_cmd = {
  491. 0xf031, 0 /* SQ930_CLKFREQ_60MHZ */
  492. };
  493. ucbus_write(&sd->gspca_dev, &clkfreq_cmd, 1, 1);
  494. gpio_set(sd, SQ930_GPIO_POWER, 0xff00);
  495. switch (sd->sensor) {
  496. case SENSOR_ICX098BQ:
  497. if (first_time)
  498. ucbus_write(&sd->gspca_dev,
  499. icx098bq_start_0,
  500. 8, 8);
  501. gpio_set(sd, 0, 0x00ff);
  502. gpio_set(sd, SQ930_GPIO_DFL_I2C_SCL | SQ930_GPIO_DFL_I2C_SDA,
  503. SQ930_GPIO_DFL_I2C_SCL | SQ930_GPIO_DFL_I2C_SDA);
  504. gpio_set(sd, 0, SQ930_GPIO_DFL_I2C_SCL);
  505. gpio_set(sd, 0, SQ930_GPIO_DFL_I2C_SDA);
  506. gpio_set(sd, SQ930_GPIO_RSTBAR,
  507. SQ930_GPIO_RSTBAR);
  508. break;
  509. case SENSOR_LZ24BP:
  510. if (sd->type != Creative_live_motion)
  511. gpio_set(sd, SQ930_GPIO_EXTRA1, 0x00ff);
  512. else
  513. gpio_set(sd, 0, 0x00ff);
  514. msleep(50);
  515. if (first_time)
  516. ucbus_write(&sd->gspca_dev,
  517. lz24bp_start_0,
  518. 8, 8);
  519. gpio_set(sd, 0, 0x0001); /* no change */
  520. gpio_set(sd, SQ930_GPIO_DFL_I2C_SCL | SQ930_GPIO_DFL_I2C_SDA,
  521. SQ930_GPIO_DFL_I2C_SCL | SQ930_GPIO_DFL_I2C_SDA);
  522. gpio_set(sd, 0, SQ930_GPIO_DFL_I2C_SCL);
  523. gpio_set(sd, 0, SQ930_GPIO_DFL_I2C_SDA);
  524. gpio_set(sd, SQ930_GPIO_RSTBAR,
  525. SQ930_GPIO_RSTBAR);
  526. break;
  527. default:
  528. /* case SENSOR_MI0360: */
  529. if (first_time) {
  530. ucbus_write(&sd->gspca_dev,
  531. mi0360_start_0,
  532. ARRAY_SIZE(mi0360_start_0),
  533. 8);
  534. gpio_set(sd, SQ930_GPIO_RSTBAR, 0x00ff);
  535. } else {
  536. gpio_set(sd, SQ930_GPIO_EXTRA2 | SQ930_GPIO_RSTBAR,
  537. 0x00ff);
  538. }
  539. gpio_set(sd, SQ930_GPIO_DFL_I2C_SCL | SQ930_GPIO_DFL_I2C_SDA,
  540. SQ930_GPIO_RSTBAR |
  541. SQ930_GPIO_DFL_I2C_SCL | SQ930_GPIO_DFL_I2C_SDA);
  542. gpio_set(sd, 0, SQ930_GPIO_DFL_I2C_SCL);
  543. gpio_set(sd, 0, SQ930_GPIO_DFL_I2C_SDA);
  544. gpio_set(sd, 0, SQ930_GPIO_DFL_I2C_SDA);
  545. gpio_set(sd, SQ930_GPIO_EXTRA2, SQ930_GPIO_EXTRA2);
  546. break;
  547. }
  548. }
  549. static void lz24bp_ppl(struct sd *sd, u16 ppl)
  550. {
  551. struct ucbus_write_cmd cmds[2] = {
  552. {0xf810, ppl >> 8},
  553. {0xf811, ppl}
  554. };
  555. ucbus_write(&sd->gspca_dev, cmds, ARRAY_SIZE(cmds), 2);
  556. }
  557. static void setexposure(struct gspca_dev *gspca_dev)
  558. {
  559. struct sd *sd = (struct sd *) gspca_dev;
  560. int i, integclks, intstartclk, frameclks, min_frclk;
  561. u16 cmd;
  562. u8 buf[15];
  563. integclks = sd->expo;
  564. i = 0;
  565. cmd = SQ930_CTRL_SET_EXPOSURE;
  566. if (sd->sensor == SENSOR_MI0360) {
  567. cmd |= 0x0100;
  568. buf[i++] = 0x5d; /* i2c_slave_addr */
  569. buf[i++] = 0x08; /* 2 * ni2c */
  570. buf[i++] = 0x09; /* reg = shutter width */
  571. buf[i++] = integclks >> 8; /* val H */
  572. buf[i++] = 0x80;
  573. buf[i++] = integclks; /* val L */
  574. buf[i++] = 0x35; /* reg = global gain */
  575. buf[i++] = 0x00; /* val H */
  576. buf[i++] = 0x80;
  577. buf[i++] = sd->gain; /* val L */
  578. buf[i++] = 0x00;
  579. buf[i++] = 0x00;
  580. buf[i++] = 0x00;
  581. buf[i++] = 0x00;
  582. buf[i++] = 0x83;
  583. } else {
  584. min_frclk = sd->sensor == SENSOR_ICX098BQ ? 0x210 : 0x26f;
  585. if (integclks >= min_frclk) {
  586. intstartclk = 0;
  587. frameclks = integclks;
  588. } else {
  589. intstartclk = min_frclk - integclks;
  590. frameclks = min_frclk;
  591. }
  592. buf[i++] = intstartclk >> 8;
  593. buf[i++] = intstartclk;
  594. buf[i++] = frameclks >> 8;
  595. buf[i++] = frameclks;
  596. buf[i++] = sd->gain;
  597. }
  598. reg_wb(gspca_dev, cmd, 0, buf, i);
  599. }
  600. /* This function is called at probe time just before sd_init */
  601. static int sd_config(struct gspca_dev *gspca_dev,
  602. const struct usb_device_id *id)
  603. {
  604. struct sd *sd = (struct sd *) gspca_dev;
  605. struct cam *cam = &gspca_dev->cam;
  606. sd->sensor = id->driver_info >> 8;
  607. sd->type = id->driver_info;
  608. cam->cam_mode = vga_mode;
  609. cam->nmodes = ARRAY_SIZE(vga_mode);
  610. cam->bulk = 1;
  611. cam->bulk_size = BULK_TRANSFER_LEN;
  612. /* cam->bulk_nurbs = 2; fixme: if no setexpo sync */
  613. sd->quality = QUALITY_DEF;
  614. sd->gain = GAIN_DEF;
  615. sd->expo = EXPO_DEF;
  616. return 0;
  617. }
  618. /* this function is called at probe and resume time */
  619. static int sd_init(struct gspca_dev *gspca_dev)
  620. {
  621. struct sd *sd = (struct sd *) gspca_dev;
  622. sd->gpio[0] = sd->gpio[1] = 0xff; /* force gpio rewrite */
  623. if (sd->sensor != SENSOR_LZ24BP)
  624. reg_w(gspca_dev, SQ930_CTRL_RESET, 0x0000);
  625. reg_r(gspca_dev, SQ930_CTRL_GET_DEV_INFO, 8);
  626. /* it returns:
  627. * 03 00 12 93 0b f6 c9 00 live! ultra
  628. * 03 00 07 93 0b f6 ca 00 live! ultra for notebook
  629. * 03 00 12 93 0b fe c8 00 Trust WB-3500T
  630. * 02 00 06 93 0b fe c8 00 Joy-IT 318S
  631. * 03 00 12 93 0b f6 cf 00 icam tracer - sensor icx098bq
  632. * 02 00 12 93 0b fe cf 00 ProQ Motion Webcam
  633. *
  634. * byte
  635. * 0: 02 = usb 1.0 (12Mbit) / 03 = usb2.0 (480Mbit)
  636. * 1: 00
  637. * 2: 06 / 07 / 12 = mode webcam? firmware??
  638. * 3: 93 chip = 930b (930b or 930c)
  639. * 4: 0b
  640. * 5: f6 = cdd (icx098bq, lz24bp) / fe = cmos (i2c) (mi0360, ov9630)
  641. * 6: c8 / c9 / ca / cf = mode webcam?, sensor? webcam?
  642. * 7: 00
  643. */
  644. PDEBUG(D_PROBE, "info: %02x %02x %02x %02x %02x %02x %02x %02x",
  645. gspca_dev->usb_buf[0],
  646. gspca_dev->usb_buf[1],
  647. gspca_dev->usb_buf[2],
  648. gspca_dev->usb_buf[3],
  649. gspca_dev->usb_buf[4],
  650. gspca_dev->usb_buf[5],
  651. gspca_dev->usb_buf[6],
  652. gspca_dev->usb_buf[7]);
  653. /*fixme: no sensor probe - special case for icam tracer */
  654. if (gspca_dev->usb_buf[5] == 0xf6
  655. && sd->sensor == SENSOR_MI0360) {
  656. sd->sensor = SENSOR_ICX098BQ;
  657. gspca_dev->cam.cam_mode = &vga_mode[1]; /* only 320x240 */
  658. gspca_dev->cam.nmodes = 1;
  659. }
  660. global_init(sd, 1);
  661. return gspca_dev->usb_err;
  662. }
  663. /* special function to create the quantization tables of the JPEG header */
  664. static void sd_jpeg_set_qual(u8 *jpeg_hdr,
  665. int quality)
  666. {
  667. int i, sc1, sc2;
  668. quality = quality_tb[quality]; /* convert to JPEG quality */
  669. /*
  670. * approximative qualities for Y and U/V:
  671. * quant = 0:94%/91% 1:91%/87% 2:82%/73% 3:69%/56%
  672. * should have:
  673. * quant = 0:94%/91% 1:91%/87.5% 2:81.5%/72% 3:69%/54.5%
  674. */
  675. sc1 = 200 - quality * 2;
  676. quality = quality * 7 / 5 - 40; /* UV quality */
  677. sc2 = 200 - quality * 2;
  678. for (i = 0; i < 64; i++) {
  679. jpeg_hdr[JPEG_QT0_OFFSET + i] =
  680. (jpeg_head[JPEG_QT0_OFFSET + i] * sc1 + 50) / 100;
  681. jpeg_hdr[JPEG_QT1_OFFSET + i] =
  682. (jpeg_head[JPEG_QT1_OFFSET + i] * sc2 + 50) / 100;
  683. }
  684. }
  685. /* send the start/stop commands to the webcam */
  686. static void send_start(struct gspca_dev *gspca_dev)
  687. {
  688. struct sd *sd = (struct sd *) gspca_dev;
  689. const struct cap_s *cap;
  690. int mode, quality;
  691. mode = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
  692. cap = &capconfig[sd->sensor][mode];
  693. quality = sd->quality;
  694. reg_wb(gspca_dev, (quality << 12)
  695. | 0x0a00 /* 900 for Bayer */
  696. | SQ930_CTRL_CAP_START,
  697. 0x0500 /* a00 for Bayer */
  698. | cap->cc_sizeid,
  699. cap->cc_bytes, 32);
  700. };
  701. static void send_stop(struct gspca_dev *gspca_dev)
  702. {
  703. reg_w(gspca_dev, SQ930_CTRL_CAP_STOP, 0);
  704. };
  705. /* function called at start time before URB creation */
  706. static int sd_isoc_init(struct gspca_dev *gspca_dev)
  707. {
  708. struct sd *sd = (struct sd *) gspca_dev;
  709. gspca_dev->cam.bulk_nurbs = 1; /* there must be one URB only */
  710. sd->do_ctrl = 0;
  711. return 0;
  712. }
  713. /* start the capture */
  714. static int sd_start(struct gspca_dev *gspca_dev)
  715. {
  716. struct sd *sd = (struct sd *) gspca_dev;
  717. int mode;
  718. /* initialize the JPEG header */
  719. jpeg_define(sd->jpeg_hdr, gspca_dev->height, gspca_dev->width,
  720. 0x21); /* JPEG 422 */
  721. sd_jpeg_set_qual(sd->jpeg_hdr, sd->quality);
  722. global_init(sd, 0);
  723. msleep(100);
  724. switch (sd->sensor) {
  725. case SENSOR_ICX098BQ:
  726. ucbus_write(gspca_dev, icx098bq_start_0,
  727. ARRAY_SIZE(icx098bq_start_0),
  728. 8);
  729. ucbus_write(gspca_dev, icx098bq_start_1,
  730. ARRAY_SIZE(icx098bq_start_1),
  731. 5);
  732. ucbus_write(gspca_dev, icx098bq_start_2,
  733. ARRAY_SIZE(icx098bq_start_2),
  734. 6);
  735. msleep(50);
  736. /* 1st start */
  737. send_start(gspca_dev);
  738. gpio_set(sd, SQ930_GPIO_EXTRA2 | SQ930_GPIO_RSTBAR, 0x00ff);
  739. msleep(70);
  740. reg_w(gspca_dev, SQ930_CTRL_CAP_STOP, 0x0000);
  741. gpio_set(sd, 0x7f, 0x00ff);
  742. /* 2nd start */
  743. send_start(gspca_dev);
  744. gpio_set(sd, SQ930_GPIO_EXTRA2 | SQ930_GPIO_RSTBAR, 0x00ff);
  745. goto out;
  746. case SENSOR_LZ24BP:
  747. ucbus_write(gspca_dev, lz24bp_start_0,
  748. ARRAY_SIZE(lz24bp_start_0),
  749. 8);
  750. if (sd->type != Creative_live_motion)
  751. ucbus_write(gspca_dev, lz24bp_start_1_gen,
  752. ARRAY_SIZE(lz24bp_start_1_gen),
  753. 5);
  754. else
  755. ucbus_write(gspca_dev, lz24bp_start_1_clm,
  756. ARRAY_SIZE(lz24bp_start_1_clm),
  757. 5);
  758. ucbus_write(gspca_dev, lz24bp_start_2,
  759. ARRAY_SIZE(lz24bp_start_2),
  760. 6);
  761. mode = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
  762. lz24bp_ppl(sd, mode == 2 ? 0x0564 : 0x0310);
  763. msleep(10);
  764. break;
  765. default:
  766. /* case SENSOR_MI0360: */
  767. ucbus_write(gspca_dev, mi0360_start_0,
  768. ARRAY_SIZE(mi0360_start_0),
  769. 8);
  770. i2c_write(gspca_dev, mi0360_init_23,
  771. ARRAY_SIZE(mi0360_init_23));
  772. i2c_write(gspca_dev, mi0360_init_24,
  773. ARRAY_SIZE(mi0360_init_24));
  774. i2c_write(gspca_dev, mi0360_init_25,
  775. ARRAY_SIZE(mi0360_init_25));
  776. ucbus_write(gspca_dev, mi0360_start_1,
  777. ARRAY_SIZE(mi0360_start_1),
  778. 5);
  779. i2c_write(gspca_dev, mi0360_start_2,
  780. ARRAY_SIZE(mi0360_start_2));
  781. i2c_write(gspca_dev, mi0360_start_3,
  782. ARRAY_SIZE(mi0360_start_3));
  783. /* 1st start */
  784. send_start(gspca_dev);
  785. msleep(60);
  786. reg_w(gspca_dev, SQ930_CTRL_CAP_STOP, 0x0000);
  787. i2c_write(gspca_dev,
  788. mi0360_start_4, ARRAY_SIZE(mi0360_start_4));
  789. break;
  790. }
  791. send_start(gspca_dev);
  792. out:
  793. msleep(1000);
  794. sd->eof_len = 0; /* init packet scan */
  795. sd->do_ctrl = 1; /* set the exposure */
  796. return gspca_dev->usb_err;
  797. }
  798. static void sd_stopN(struct gspca_dev *gspca_dev)
  799. {
  800. send_stop(gspca_dev);
  801. }
  802. /* function called when the application gets a new frame */
  803. /* It sets the exposure if required and restart the bulk transfer. */
  804. static void sd_dq_callback(struct gspca_dev *gspca_dev)
  805. {
  806. struct sd *sd = (struct sd *) gspca_dev;
  807. int ret;
  808. if (!sd->do_ctrl || gspca_dev->cam.bulk_nurbs != 0)
  809. return;
  810. sd->do_ctrl = 0;
  811. setexposure(gspca_dev);
  812. gspca_dev->cam.bulk_nurbs = 1;
  813. ret = usb_submit_urb(gspca_dev->urb[0], GFP_ATOMIC);
  814. if (ret < 0)
  815. PDEBUG(D_ERR|D_PACK, "sd_dq_callback() err %d", ret);
  816. /* wait a little time, otherwise the webcam crashes */
  817. msleep(100);
  818. }
  819. /* move a packet adding 0x00 after 0xff */
  820. static void add_packet(struct gspca_dev *gspca_dev,
  821. u8 *data,
  822. int len)
  823. {
  824. int i;
  825. i = 0;
  826. do {
  827. if (data[i] == 0xff) {
  828. gspca_frame_add(gspca_dev, INTER_PACKET,
  829. data, i + 1);
  830. len -= i;
  831. data += i;
  832. *data = 0x00;
  833. i = 0;
  834. }
  835. } while (++i < len);
  836. gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
  837. }
  838. /* end a frame and start a new one */
  839. static void eof_sof(struct gspca_dev *gspca_dev)
  840. {
  841. struct sd *sd = (struct sd *) gspca_dev;
  842. static const u8 ffd9[] = {0xff, 0xd9};
  843. /* if control set, stop bulk transfer */
  844. if (sd->do_ctrl
  845. && gspca_dev->last_packet_type == INTER_PACKET)
  846. gspca_dev->cam.bulk_nurbs = 0;
  847. gspca_frame_add(gspca_dev, LAST_PACKET,
  848. ffd9, 2);
  849. gspca_frame_add(gspca_dev, FIRST_PACKET,
  850. sd->jpeg_hdr, JPEG_HDR_SZ);
  851. }
  852. static void sd_pkt_scan(struct gspca_dev *gspca_dev,
  853. u8 *data, /* isoc packet */
  854. int len) /* iso packet length */
  855. {
  856. struct sd *sd = (struct sd *) gspca_dev;
  857. u8 *p;
  858. int l;
  859. len -= 8; /* ignore last 8 bytes (00 00 55 aa 55 aa 00 00) */
  860. /*
  861. * the end/start of frame is indicated by
  862. * 0x00 * 16 - 0xab * 8
  863. * aligned on 8 bytes boundary
  864. */
  865. if (sd->eof_len != 0) { /* if 'abababab' in previous pkt */
  866. if (*((u32 *) data) == 0xabababab) {
  867. /*fixme: should remove previous 0000ababab*/
  868. eof_sof(gspca_dev);
  869. data += 4;
  870. len -= 4;
  871. }
  872. sd->eof_len = 0;
  873. }
  874. p = data;
  875. l = len;
  876. for (;;) {
  877. if (*((u32 *) p) == 0xabababab) {
  878. if (l < 8) { /* (may be 4 only) */
  879. sd->eof_len = 1;
  880. break;
  881. }
  882. if (*((u32 *) p + 1) == 0xabababab) {
  883. add_packet(gspca_dev, data, p - data - 16);
  884. /* remove previous zeros */
  885. eof_sof(gspca_dev);
  886. p += 8;
  887. l -= 8;
  888. if (l <= 0)
  889. return;
  890. len = l;
  891. data = p;
  892. continue;
  893. }
  894. }
  895. p += 4;
  896. l -= 4;
  897. if (l <= 0)
  898. break;
  899. }
  900. add_packet(gspca_dev, data, len);
  901. }
  902. static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val)
  903. {
  904. struct sd *sd = (struct sd *) gspca_dev;
  905. sd->gain = val;
  906. if (gspca_dev->streaming)
  907. sd->do_ctrl = 1;
  908. return 0;
  909. }
  910. static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val)
  911. {
  912. struct sd *sd = (struct sd *) gspca_dev;
  913. *val = sd->gain;
  914. return 0;
  915. }
  916. static int sd_setexpo(struct gspca_dev *gspca_dev, __s32 val)
  917. {
  918. struct sd *sd = (struct sd *) gspca_dev;
  919. sd->expo = val;
  920. if (gspca_dev->streaming)
  921. sd->do_ctrl = 1;
  922. return 0;
  923. }
  924. static int sd_getexpo(struct gspca_dev *gspca_dev, __s32 *val)
  925. {
  926. struct sd *sd = (struct sd *) gspca_dev;
  927. *val = sd->expo;
  928. return 0;
  929. }
  930. static int sd_set_jcomp(struct gspca_dev *gspca_dev,
  931. struct v4l2_jpegcompression *jcomp)
  932. {
  933. struct sd *sd = (struct sd *) gspca_dev;
  934. int quality;
  935. if (jcomp->quality >= (QUAL_0 + QUAL_1) / 2)
  936. quality = 0;
  937. else if (jcomp->quality >= (QUAL_1 + QUAL_2) / 2)
  938. quality = 1;
  939. else if (jcomp->quality >= (QUAL_2 + QUAL_3) / 2)
  940. quality = 2;
  941. else
  942. quality = 3;
  943. if (quality != sd->quality) {
  944. sd->quality = quality;
  945. if (gspca_dev->streaming) {
  946. send_stop(gspca_dev);
  947. sd_jpeg_set_qual(sd->jpeg_hdr, sd->quality);
  948. msleep(70);
  949. send_start(gspca_dev);
  950. }
  951. }
  952. return gspca_dev->usb_err;
  953. }
  954. static int sd_get_jcomp(struct gspca_dev *gspca_dev,
  955. struct v4l2_jpegcompression *jcomp)
  956. {
  957. struct sd *sd = (struct sd *) gspca_dev;
  958. memset(jcomp, 0, sizeof *jcomp);
  959. jcomp->quality = quality_tb[sd->quality];
  960. jcomp->jpeg_markers = V4L2_JPEG_MARKER_DHT
  961. | V4L2_JPEG_MARKER_DQT;
  962. return 0;
  963. }
  964. /* sub-driver description */
  965. static const struct sd_desc sd_desc = {
  966. .name = MODULE_NAME,
  967. .ctrls = sd_ctrls,
  968. .nctrls = ARRAY_SIZE(sd_ctrls),
  969. .config = sd_config,
  970. .init = sd_init,
  971. .isoc_init = sd_isoc_init,
  972. .start = sd_start,
  973. .stopN = sd_stopN,
  974. .pkt_scan = sd_pkt_scan,
  975. .dq_callback = sd_dq_callback,
  976. .get_jcomp = sd_get_jcomp,
  977. .set_jcomp = sd_set_jcomp,
  978. };
  979. /* Table of supported USB devices */
  980. #define ST(sensor, type) \
  981. .driver_info = (SENSOR_ ## sensor << 8) \
  982. | (type)
  983. static const __devinitdata struct usb_device_id device_table[] = {
  984. {USB_DEVICE(0x041e, 0x4038), ST(MI0360, 0)},
  985. {USB_DEVICE(0x041e, 0x403c), ST(LZ24BP, 0)},
  986. {USB_DEVICE(0x041e, 0x403d), ST(LZ24BP, 0)},
  987. {USB_DEVICE(0x041e, 0x4041), ST(LZ24BP, Creative_live_motion)},
  988. {USB_DEVICE(0x2770, 0x930b), ST(MI0360, 0)}, /* or ICX098BQ */
  989. {USB_DEVICE(0x2770, 0x930c), ST(MI0360, 0)},
  990. {}
  991. };
  992. MODULE_DEVICE_TABLE(usb, device_table);
  993. /* -- device connect -- */
  994. static int sd_probe(struct usb_interface *intf,
  995. const struct usb_device_id *id)
  996. {
  997. return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
  998. THIS_MODULE);
  999. }
  1000. static struct usb_driver sd_driver = {
  1001. .name = MODULE_NAME,
  1002. .id_table = device_table,
  1003. .probe = sd_probe,
  1004. .disconnect = gspca_disconnect,
  1005. #ifdef CONFIG_PM
  1006. .suspend = gspca_suspend,
  1007. .resume = gspca_resume,
  1008. #endif
  1009. };
  1010. /* -- module insert / remove -- */
  1011. static int __init sd_mod_init(void)
  1012. {
  1013. int ret;
  1014. ret = usb_register(&sd_driver);
  1015. if (ret < 0)
  1016. return ret;
  1017. info("registered");
  1018. return 0;
  1019. }
  1020. static void __exit sd_mod_exit(void)
  1021. {
  1022. usb_deregister(&sd_driver);
  1023. info("deregistered");
  1024. }
  1025. module_init(sd_mod_init);
  1026. module_exit(sd_mod_exit);