w996Xcf.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. /**
  2. *
  3. * GSPCA sub driver for W996[78]CF JPEG USB Dual Mode Camera Chip.
  4. *
  5. * Copyright (C) 2009 Hans de Goede <hdegoede@redhat.com>
  6. *
  7. * This module is adapted from the in kernel v4l1 w9968cf driver:
  8. *
  9. * Copyright (C) 2002-2004 by Luca Risolia <luca.risolia@studio.unibo.it>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  24. *
  25. */
  26. /* Note this is not a stand alone driver, it gets included in ov519.c, this
  27. is a bit of a hack, but it needs the driver code for a lot of different
  28. ov sensors which is already present in ov519.c (the old v4l1 driver used
  29. the ovchipcam framework). When we have the time we really should move
  30. the sensor drivers to v4l2 sub drivers, and properly split of this
  31. driver from ov519.c */
  32. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  33. #define W9968CF_I2C_BUS_DELAY 4 /* delay in us for I2C bit r/w operations */
  34. #define Y_QUANTABLE (&sd->jpeg_hdr[JPEG_QT0_OFFSET])
  35. #define UV_QUANTABLE (&sd->jpeg_hdr[JPEG_QT1_OFFSET])
  36. static const struct v4l2_pix_format w9968cf_vga_mode[] = {
  37. {160, 120, V4L2_PIX_FMT_UYVY, V4L2_FIELD_NONE,
  38. .bytesperline = 160 * 2,
  39. .sizeimage = 160 * 120 * 2,
  40. .colorspace = V4L2_COLORSPACE_JPEG},
  41. {176, 144, V4L2_PIX_FMT_UYVY, V4L2_FIELD_NONE,
  42. .bytesperline = 176 * 2,
  43. .sizeimage = 176 * 144 * 2,
  44. .colorspace = V4L2_COLORSPACE_JPEG},
  45. {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
  46. .bytesperline = 320 * 2,
  47. .sizeimage = 320 * 240 * 2,
  48. .colorspace = V4L2_COLORSPACE_JPEG},
  49. {352, 288, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
  50. .bytesperline = 352 * 2,
  51. .sizeimage = 352 * 288 * 2,
  52. .colorspace = V4L2_COLORSPACE_JPEG},
  53. {640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
  54. .bytesperline = 640 * 2,
  55. .sizeimage = 640 * 480 * 2,
  56. .colorspace = V4L2_COLORSPACE_JPEG},
  57. };
  58. static void reg_w(struct sd *sd, u16 index, u16 value);
  59. /*--------------------------------------------------------------------------
  60. Write 64-bit data to the fast serial bus registers.
  61. Return 0 on success, -1 otherwise.
  62. --------------------------------------------------------------------------*/
  63. static void w9968cf_write_fsb(struct sd *sd, u16* data)
  64. {
  65. struct usb_device *udev = sd->gspca_dev.dev;
  66. u16 value;
  67. int ret;
  68. if (sd->gspca_dev.usb_err < 0)
  69. return;
  70. value = *data++;
  71. memcpy(sd->gspca_dev.usb_buf, data, 6);
  72. ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0,
  73. USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
  74. value, 0x06, sd->gspca_dev.usb_buf, 6, 500);
  75. if (ret < 0) {
  76. pr_err("Write FSB registers failed (%d)\n", ret);
  77. sd->gspca_dev.usb_err = ret;
  78. }
  79. }
  80. /*--------------------------------------------------------------------------
  81. Write data to the serial bus control register.
  82. Return 0 on success, a negative number otherwise.
  83. --------------------------------------------------------------------------*/
  84. static void w9968cf_write_sb(struct sd *sd, u16 value)
  85. {
  86. int ret;
  87. if (sd->gspca_dev.usb_err < 0)
  88. return;
  89. /* We don't use reg_w here, as that would cause all writes when
  90. bitbanging i2c to be logged, making the logs impossible to read */
  91. ret = usb_control_msg(sd->gspca_dev.dev,
  92. usb_sndctrlpipe(sd->gspca_dev.dev, 0),
  93. 0,
  94. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  95. value, 0x01, NULL, 0, 500);
  96. udelay(W9968CF_I2C_BUS_DELAY);
  97. if (ret < 0) {
  98. pr_err("Write SB reg [01] %04x failed\n", value);
  99. sd->gspca_dev.usb_err = ret;
  100. }
  101. }
  102. /*--------------------------------------------------------------------------
  103. Read data from the serial bus control register.
  104. Return 0 on success, a negative number otherwise.
  105. --------------------------------------------------------------------------*/
  106. static int w9968cf_read_sb(struct sd *sd)
  107. {
  108. int ret;
  109. if (sd->gspca_dev.usb_err < 0)
  110. return -1;
  111. /* We don't use reg_r here, as the w9968cf is special and has 16
  112. bit registers instead of 8 bit */
  113. ret = usb_control_msg(sd->gspca_dev.dev,
  114. usb_rcvctrlpipe(sd->gspca_dev.dev, 0),
  115. 1,
  116. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  117. 0, 0x01, sd->gspca_dev.usb_buf, 2, 500);
  118. if (ret >= 0) {
  119. ret = sd->gspca_dev.usb_buf[0] |
  120. (sd->gspca_dev.usb_buf[1] << 8);
  121. } else {
  122. pr_err("Read SB reg [01] failed\n");
  123. sd->gspca_dev.usb_err = ret;
  124. }
  125. udelay(W9968CF_I2C_BUS_DELAY);
  126. return ret;
  127. }
  128. /*--------------------------------------------------------------------------
  129. Upload quantization tables for the JPEG compression.
  130. This function is called by w9968cf_start_transfer().
  131. Return 0 on success, a negative number otherwise.
  132. --------------------------------------------------------------------------*/
  133. static void w9968cf_upload_quantizationtables(struct sd *sd)
  134. {
  135. u16 a, b;
  136. int i, j;
  137. reg_w(sd, 0x39, 0x0010); /* JPEG clock enable */
  138. for (i = 0, j = 0; i < 32; i++, j += 2) {
  139. a = Y_QUANTABLE[j] | ((unsigned)(Y_QUANTABLE[j + 1]) << 8);
  140. b = UV_QUANTABLE[j] | ((unsigned)(UV_QUANTABLE[j + 1]) << 8);
  141. reg_w(sd, 0x40 + i, a);
  142. reg_w(sd, 0x60 + i, b);
  143. }
  144. reg_w(sd, 0x39, 0x0012); /* JPEG encoder enable */
  145. }
  146. /****************************************************************************
  147. * Low-level I2C I/O functions. *
  148. * The adapter supports the following I2C transfer functions: *
  149. * i2c_adap_fastwrite_byte_data() (at 400 kHz bit frequency only) *
  150. * i2c_adap_read_byte_data() *
  151. * i2c_adap_read_byte() *
  152. ****************************************************************************/
  153. static void w9968cf_smbus_start(struct sd *sd)
  154. {
  155. w9968cf_write_sb(sd, 0x0011); /* SDE=1, SDA=0, SCL=1 */
  156. w9968cf_write_sb(sd, 0x0010); /* SDE=1, SDA=0, SCL=0 */
  157. }
  158. static void w9968cf_smbus_stop(struct sd *sd)
  159. {
  160. w9968cf_write_sb(sd, 0x0010); /* SDE=1, SDA=0, SCL=0 */
  161. w9968cf_write_sb(sd, 0x0011); /* SDE=1, SDA=0, SCL=1 */
  162. w9968cf_write_sb(sd, 0x0013); /* SDE=1, SDA=1, SCL=1 */
  163. }
  164. static void w9968cf_smbus_write_byte(struct sd *sd, u8 v)
  165. {
  166. u8 bit;
  167. int sda;
  168. for (bit = 0 ; bit < 8 ; bit++) {
  169. sda = (v & 0x80) ? 2 : 0;
  170. v <<= 1;
  171. /* SDE=1, SDA=sda, SCL=0 */
  172. w9968cf_write_sb(sd, 0x10 | sda);
  173. /* SDE=1, SDA=sda, SCL=1 */
  174. w9968cf_write_sb(sd, 0x11 | sda);
  175. /* SDE=1, SDA=sda, SCL=0 */
  176. w9968cf_write_sb(sd, 0x10 | sda);
  177. }
  178. }
  179. static void w9968cf_smbus_read_byte(struct sd *sd, u8 *v)
  180. {
  181. u8 bit;
  182. /* No need to ensure SDA is high as we are always called after
  183. read_ack which ends with SDA high */
  184. *v = 0;
  185. for (bit = 0 ; bit < 8 ; bit++) {
  186. *v <<= 1;
  187. /* SDE=1, SDA=1, SCL=1 */
  188. w9968cf_write_sb(sd, 0x0013);
  189. *v |= (w9968cf_read_sb(sd) & 0x0008) ? 1 : 0;
  190. /* SDE=1, SDA=1, SCL=0 */
  191. w9968cf_write_sb(sd, 0x0012);
  192. }
  193. }
  194. static void w9968cf_smbus_write_nack(struct sd *sd)
  195. {
  196. /* No need to ensure SDA is high as we are always called after
  197. read_byte which ends with SDA high */
  198. w9968cf_write_sb(sd, 0x0013); /* SDE=1, SDA=1, SCL=1 */
  199. w9968cf_write_sb(sd, 0x0012); /* SDE=1, SDA=1, SCL=0 */
  200. }
  201. static void w9968cf_smbus_read_ack(struct sd *sd)
  202. {
  203. struct gspca_dev *gspca_dev = (struct gspca_dev *)sd;
  204. int sda;
  205. /* Ensure SDA is high before raising clock to avoid a spurious stop */
  206. w9968cf_write_sb(sd, 0x0012); /* SDE=1, SDA=1, SCL=0 */
  207. w9968cf_write_sb(sd, 0x0013); /* SDE=1, SDA=1, SCL=1 */
  208. sda = w9968cf_read_sb(sd);
  209. w9968cf_write_sb(sd, 0x0012); /* SDE=1, SDA=1, SCL=0 */
  210. if (sda >= 0 && (sda & 0x08)) {
  211. PDEBUG(D_USBI, "Did not receive i2c ACK");
  212. sd->gspca_dev.usb_err = -EIO;
  213. }
  214. }
  215. /* SMBus protocol: S Addr Wr [A] Subaddr [A] Value [A] P */
  216. static void w9968cf_i2c_w(struct sd *sd, u8 reg, u8 value)
  217. {
  218. struct gspca_dev *gspca_dev = (struct gspca_dev *)sd;
  219. u16* data = (u16 *)sd->gspca_dev.usb_buf;
  220. data[0] = 0x082f | ((sd->sensor_addr & 0x80) ? 0x1500 : 0x0);
  221. data[0] |= (sd->sensor_addr & 0x40) ? 0x4000 : 0x0;
  222. data[1] = 0x2082 | ((sd->sensor_addr & 0x40) ? 0x0005 : 0x0);
  223. data[1] |= (sd->sensor_addr & 0x20) ? 0x0150 : 0x0;
  224. data[1] |= (sd->sensor_addr & 0x10) ? 0x5400 : 0x0;
  225. data[2] = 0x8208 | ((sd->sensor_addr & 0x08) ? 0x0015 : 0x0);
  226. data[2] |= (sd->sensor_addr & 0x04) ? 0x0540 : 0x0;
  227. data[2] |= (sd->sensor_addr & 0x02) ? 0x5000 : 0x0;
  228. data[3] = 0x1d20 | ((sd->sensor_addr & 0x02) ? 0x0001 : 0x0);
  229. data[3] |= (sd->sensor_addr & 0x01) ? 0x0054 : 0x0;
  230. w9968cf_write_fsb(sd, data);
  231. data[0] = 0x8208 | ((reg & 0x80) ? 0x0015 : 0x0);
  232. data[0] |= (reg & 0x40) ? 0x0540 : 0x0;
  233. data[0] |= (reg & 0x20) ? 0x5000 : 0x0;
  234. data[1] = 0x0820 | ((reg & 0x20) ? 0x0001 : 0x0);
  235. data[1] |= (reg & 0x10) ? 0x0054 : 0x0;
  236. data[1] |= (reg & 0x08) ? 0x1500 : 0x0;
  237. data[1] |= (reg & 0x04) ? 0x4000 : 0x0;
  238. data[2] = 0x2082 | ((reg & 0x04) ? 0x0005 : 0x0);
  239. data[2] |= (reg & 0x02) ? 0x0150 : 0x0;
  240. data[2] |= (reg & 0x01) ? 0x5400 : 0x0;
  241. data[3] = 0x001d;
  242. w9968cf_write_fsb(sd, data);
  243. data[0] = 0x8208 | ((value & 0x80) ? 0x0015 : 0x0);
  244. data[0] |= (value & 0x40) ? 0x0540 : 0x0;
  245. data[0] |= (value & 0x20) ? 0x5000 : 0x0;
  246. data[1] = 0x0820 | ((value & 0x20) ? 0x0001 : 0x0);
  247. data[1] |= (value & 0x10) ? 0x0054 : 0x0;
  248. data[1] |= (value & 0x08) ? 0x1500 : 0x0;
  249. data[1] |= (value & 0x04) ? 0x4000 : 0x0;
  250. data[2] = 0x2082 | ((value & 0x04) ? 0x0005 : 0x0);
  251. data[2] |= (value & 0x02) ? 0x0150 : 0x0;
  252. data[2] |= (value & 0x01) ? 0x5400 : 0x0;
  253. data[3] = 0xfe1d;
  254. w9968cf_write_fsb(sd, data);
  255. PDEBUG(D_USBO, "i2c 0x%02x -> [0x%02x]", value, reg);
  256. }
  257. /* SMBus protocol: S Addr Wr [A] Subaddr [A] P S Addr+1 Rd [A] [Value] NA P */
  258. static int w9968cf_i2c_r(struct sd *sd, u8 reg)
  259. {
  260. struct gspca_dev *gspca_dev = (struct gspca_dev *)sd;
  261. int ret = 0;
  262. u8 value;
  263. /* Fast serial bus data control disable */
  264. w9968cf_write_sb(sd, 0x0013); /* don't change ! */
  265. w9968cf_smbus_start(sd);
  266. w9968cf_smbus_write_byte(sd, sd->sensor_addr);
  267. w9968cf_smbus_read_ack(sd);
  268. w9968cf_smbus_write_byte(sd, reg);
  269. w9968cf_smbus_read_ack(sd);
  270. w9968cf_smbus_stop(sd);
  271. w9968cf_smbus_start(sd);
  272. w9968cf_smbus_write_byte(sd, sd->sensor_addr + 1);
  273. w9968cf_smbus_read_ack(sd);
  274. w9968cf_smbus_read_byte(sd, &value);
  275. /* signal we don't want to read anymore, the v4l1 driver used to
  276. send an ack here which is very wrong! (and then fixed
  277. the issues this gave by retrying reads) */
  278. w9968cf_smbus_write_nack(sd);
  279. w9968cf_smbus_stop(sd);
  280. /* Fast serial bus data control re-enable */
  281. w9968cf_write_sb(sd, 0x0030);
  282. if (sd->gspca_dev.usb_err >= 0) {
  283. ret = value;
  284. PDEBUG(D_USBI, "i2c [0x%02X] -> 0x%02X", reg, value);
  285. } else
  286. PERR("i2c read [0x%02x] failed", reg);
  287. return ret;
  288. }
  289. /*--------------------------------------------------------------------------
  290. Turn on the LED on some webcams. A beep should be heard too.
  291. Return 0 on success, a negative number otherwise.
  292. --------------------------------------------------------------------------*/
  293. static void w9968cf_configure(struct sd *sd)
  294. {
  295. reg_w(sd, 0x00, 0xff00); /* power-down */
  296. reg_w(sd, 0x00, 0xbf17); /* reset everything */
  297. reg_w(sd, 0x00, 0xbf10); /* normal operation */
  298. reg_w(sd, 0x01, 0x0010); /* serial bus, SDS high */
  299. reg_w(sd, 0x01, 0x0000); /* serial bus, SDS low */
  300. reg_w(sd, 0x01, 0x0010); /* ..high 'beep-beep' */
  301. reg_w(sd, 0x01, 0x0030); /* Set sda scl to FSB mode */
  302. sd->stopped = 1;
  303. }
  304. static void w9968cf_init(struct sd *sd)
  305. {
  306. unsigned long hw_bufsize = sd->sif ? (352 * 288 * 2) : (640 * 480 * 2),
  307. y0 = 0x0000,
  308. u0 = y0 + hw_bufsize / 2,
  309. v0 = u0 + hw_bufsize / 4,
  310. y1 = v0 + hw_bufsize / 4,
  311. u1 = y1 + hw_bufsize / 2,
  312. v1 = u1 + hw_bufsize / 4;
  313. reg_w(sd, 0x00, 0xff00); /* power off */
  314. reg_w(sd, 0x00, 0xbf10); /* power on */
  315. reg_w(sd, 0x03, 0x405d); /* DRAM timings */
  316. reg_w(sd, 0x04, 0x0030); /* SDRAM timings */
  317. reg_w(sd, 0x20, y0 & 0xffff); /* Y buf.0, low */
  318. reg_w(sd, 0x21, y0 >> 16); /* Y buf.0, high */
  319. reg_w(sd, 0x24, u0 & 0xffff); /* U buf.0, low */
  320. reg_w(sd, 0x25, u0 >> 16); /* U buf.0, high */
  321. reg_w(sd, 0x28, v0 & 0xffff); /* V buf.0, low */
  322. reg_w(sd, 0x29, v0 >> 16); /* V buf.0, high */
  323. reg_w(sd, 0x22, y1 & 0xffff); /* Y buf.1, low */
  324. reg_w(sd, 0x23, y1 >> 16); /* Y buf.1, high */
  325. reg_w(sd, 0x26, u1 & 0xffff); /* U buf.1, low */
  326. reg_w(sd, 0x27, u1 >> 16); /* U buf.1, high */
  327. reg_w(sd, 0x2a, v1 & 0xffff); /* V buf.1, low */
  328. reg_w(sd, 0x2b, v1 >> 16); /* V buf.1, high */
  329. reg_w(sd, 0x32, y1 & 0xffff); /* JPEG buf 0 low */
  330. reg_w(sd, 0x33, y1 >> 16); /* JPEG buf 0 high */
  331. reg_w(sd, 0x34, y1 & 0xffff); /* JPEG buf 1 low */
  332. reg_w(sd, 0x35, y1 >> 16); /* JPEG bug 1 high */
  333. reg_w(sd, 0x36, 0x0000);/* JPEG restart interval */
  334. reg_w(sd, 0x37, 0x0804);/*JPEG VLE FIFO threshold*/
  335. reg_w(sd, 0x38, 0x0000);/* disable hw up-scaling */
  336. reg_w(sd, 0x3f, 0x0000); /* JPEG/MCTL test data */
  337. }
  338. static void w9968cf_set_crop_window(struct sd *sd)
  339. {
  340. int start_cropx, start_cropy, x, y, fw, fh, cw, ch,
  341. max_width, max_height;
  342. if (sd->sif) {
  343. max_width = 352;
  344. max_height = 288;
  345. } else {
  346. max_width = 640;
  347. max_height = 480;
  348. }
  349. if (sd->sensor == SEN_OV7620) {
  350. /*
  351. * Sigh, this is dependend on the clock / framerate changes
  352. * made by the frequency control, sick.
  353. *
  354. * Note we cannot use v4l2_ctrl_g_ctrl here, as we get called
  355. * from ov519.c:setfreq() with the ctrl lock held!
  356. */
  357. if (sd->freq->val == 1) {
  358. start_cropx = 277;
  359. start_cropy = 37;
  360. } else {
  361. start_cropx = 105;
  362. start_cropy = 37;
  363. }
  364. } else {
  365. start_cropx = 320;
  366. start_cropy = 35;
  367. }
  368. /* Work around to avoid FP arithmetics */
  369. #define SC(x) ((x) << 10)
  370. /* Scaling factors */
  371. fw = SC(sd->gspca_dev.pixfmt.width) / max_width;
  372. fh = SC(sd->gspca_dev.pixfmt.height) / max_height;
  373. cw = (fw >= fh) ? max_width : SC(sd->gspca_dev.pixfmt.width) / fh;
  374. ch = (fw >= fh) ? SC(sd->gspca_dev.pixfmt.height) / fw : max_height;
  375. sd->sensor_width = max_width;
  376. sd->sensor_height = max_height;
  377. x = (max_width - cw) / 2;
  378. y = (max_height - ch) / 2;
  379. reg_w(sd, 0x10, start_cropx + x);
  380. reg_w(sd, 0x11, start_cropy + y);
  381. reg_w(sd, 0x12, start_cropx + x + cw);
  382. reg_w(sd, 0x13, start_cropy + y + ch);
  383. }
  384. static void w9968cf_mode_init_regs(struct sd *sd)
  385. {
  386. int val, vs_polarity, hs_polarity;
  387. w9968cf_set_crop_window(sd);
  388. reg_w(sd, 0x14, sd->gspca_dev.pixfmt.width);
  389. reg_w(sd, 0x15, sd->gspca_dev.pixfmt.height);
  390. /* JPEG width & height */
  391. reg_w(sd, 0x30, sd->gspca_dev.pixfmt.width);
  392. reg_w(sd, 0x31, sd->gspca_dev.pixfmt.height);
  393. /* Y & UV frame buffer strides (in WORD) */
  394. if (w9968cf_vga_mode[sd->gspca_dev.curr_mode].pixelformat ==
  395. V4L2_PIX_FMT_JPEG) {
  396. reg_w(sd, 0x2c, sd->gspca_dev.pixfmt.width / 2);
  397. reg_w(sd, 0x2d, sd->gspca_dev.pixfmt.width / 4);
  398. } else
  399. reg_w(sd, 0x2c, sd->gspca_dev.pixfmt.width);
  400. reg_w(sd, 0x00, 0xbf17); /* reset everything */
  401. reg_w(sd, 0x00, 0xbf10); /* normal operation */
  402. /* Transfer size in WORDS (for UYVY format only) */
  403. val = sd->gspca_dev.pixfmt.width * sd->gspca_dev.pixfmt.height;
  404. reg_w(sd, 0x3d, val & 0xffff); /* low bits */
  405. reg_w(sd, 0x3e, val >> 16); /* high bits */
  406. if (w9968cf_vga_mode[sd->gspca_dev.curr_mode].pixelformat ==
  407. V4L2_PIX_FMT_JPEG) {
  408. /* We may get called multiple times (usb isoc bw negotiat.) */
  409. jpeg_define(sd->jpeg_hdr, sd->gspca_dev.pixfmt.height,
  410. sd->gspca_dev.pixfmt.width, 0x22); /* JPEG 420 */
  411. jpeg_set_qual(sd->jpeg_hdr, v4l2_ctrl_g_ctrl(sd->jpegqual));
  412. w9968cf_upload_quantizationtables(sd);
  413. v4l2_ctrl_grab(sd->jpegqual, true);
  414. }
  415. /* Video Capture Control Register */
  416. if (sd->sensor == SEN_OV7620) {
  417. /* Seems to work around a bug in the image sensor */
  418. vs_polarity = 1;
  419. hs_polarity = 1;
  420. } else {
  421. vs_polarity = 1;
  422. hs_polarity = 0;
  423. }
  424. val = (vs_polarity << 12) | (hs_polarity << 11);
  425. /* NOTE: We may not have enough memory to do double buffering while
  426. doing compression (amount of memory differs per model cam).
  427. So we use the second image buffer also as jpeg stream buffer
  428. (see w9968cf_init), and disable double buffering. */
  429. if (w9968cf_vga_mode[sd->gspca_dev.curr_mode].pixelformat ==
  430. V4L2_PIX_FMT_JPEG) {
  431. /* val |= 0x0002; YUV422P */
  432. val |= 0x0003; /* YUV420P */
  433. } else
  434. val |= 0x0080; /* Enable HW double buffering */
  435. /* val |= 0x0020; enable clamping */
  436. /* val |= 0x0008; enable (1-2-1) filter */
  437. /* val |= 0x000c; enable (2-3-6-3-2) filter */
  438. val |= 0x8000; /* capt. enable */
  439. reg_w(sd, 0x16, val);
  440. sd->gspca_dev.empty_packet = 0;
  441. }
  442. static void w9968cf_stop0(struct sd *sd)
  443. {
  444. v4l2_ctrl_grab(sd->jpegqual, false);
  445. reg_w(sd, 0x39, 0x0000); /* disable JPEG encoder */
  446. reg_w(sd, 0x16, 0x0000); /* stop video capture */
  447. }
  448. /* The w9968cf docs say that a 0 sized packet means EOF (and also SOF
  449. for the next frame). This seems to simply not be true when operating
  450. in JPEG mode, in this case there may be empty packets within the
  451. frame. So in JPEG mode use the JPEG SOI marker to detect SOF.
  452. Note to make things even more interesting the w9968cf sends *PLANAR* jpeg,
  453. to be precise it sends: SOI, SOF, DRI, SOS, Y-data, SOS, U-data, SOS,
  454. V-data, EOI. */
  455. static void w9968cf_pkt_scan(struct gspca_dev *gspca_dev,
  456. u8 *data, /* isoc packet */
  457. int len) /* iso packet length */
  458. {
  459. struct sd *sd = (struct sd *) gspca_dev;
  460. if (w9968cf_vga_mode[gspca_dev->curr_mode].pixelformat ==
  461. V4L2_PIX_FMT_JPEG) {
  462. if (len >= 2 &&
  463. data[0] == 0xff &&
  464. data[1] == 0xd8) {
  465. gspca_frame_add(gspca_dev, LAST_PACKET,
  466. NULL, 0);
  467. gspca_frame_add(gspca_dev, FIRST_PACKET,
  468. sd->jpeg_hdr, JPEG_HDR_SZ);
  469. /* Strip the ff d8, our own header (which adds
  470. huffman and quantization tables) already has this */
  471. len -= 2;
  472. data += 2;
  473. }
  474. } else {
  475. /* In UYVY mode an empty packet signals EOF */
  476. if (gspca_dev->empty_packet) {
  477. gspca_frame_add(gspca_dev, LAST_PACKET,
  478. NULL, 0);
  479. gspca_frame_add(gspca_dev, FIRST_PACKET,
  480. NULL, 0);
  481. gspca_dev->empty_packet = 0;
  482. }
  483. }
  484. gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
  485. }