w996Xcf.c 19 KB

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