em28xx-core.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. /*
  2. em28xx-core.c - driver for Empia EM2800/EM2820/2840 USB video capture devices
  3. Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
  4. Markus Rechberger <mrechberger@gmail.com>
  5. Mauro Carvalho Chehab <mchehab@infradead.org>
  6. Sascha Sommer <saschasommer@freenet.de>
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. #include <linux/init.h>
  20. #include <linux/list.h>
  21. #include <linux/module.h>
  22. #include <linux/usb.h>
  23. #include <linux/vmalloc.h>
  24. #include "em28xx.h"
  25. /* #define ENABLE_DEBUG_ISOC_FRAMES */
  26. static unsigned int core_debug;
  27. module_param(core_debug,int,0644);
  28. MODULE_PARM_DESC(core_debug,"enable debug messages [core]");
  29. #define em28xx_coredbg(fmt, arg...) do {\
  30. if (core_debug) \
  31. printk(KERN_INFO "%s %s :"fmt, \
  32. dev->name, __func__ , ##arg); } while (0)
  33. static unsigned int reg_debug;
  34. module_param(reg_debug,int,0644);
  35. MODULE_PARM_DESC(reg_debug,"enable debug messages [URB reg]");
  36. #define em28xx_regdbg(fmt, arg...) do {\
  37. if (reg_debug) \
  38. printk(KERN_INFO "%s %s :"fmt, \
  39. dev->name, __func__ , ##arg); } while (0)
  40. static int alt = EM28XX_PINOUT;
  41. module_param(alt, int, 0644);
  42. MODULE_PARM_DESC(alt, "alternate setting to use for video endpoint");
  43. /*
  44. * em28xx_read_reg_req()
  45. * reads data from the usb device specifying bRequest
  46. */
  47. int em28xx_read_reg_req_len(struct em28xx *dev, u8 req, u16 reg,
  48. char *buf, int len)
  49. {
  50. int ret, byte;
  51. if (dev->state & DEV_DISCONNECTED)
  52. return(-ENODEV);
  53. em28xx_regdbg("req=%02x, reg=%02x ", req, reg);
  54. ret = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0), req,
  55. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  56. 0x0000, reg, buf, len, HZ);
  57. if (reg_debug){
  58. printk(ret < 0 ? " failed!\n" : "%02x values: ", ret);
  59. for (byte = 0; byte < len; byte++) {
  60. printk(" %02x", (unsigned char)buf[byte]);
  61. }
  62. printk("\n");
  63. }
  64. return ret;
  65. }
  66. /*
  67. * em28xx_read_reg_req()
  68. * reads data from the usb device specifying bRequest
  69. */
  70. int em28xx_read_reg_req(struct em28xx *dev, u8 req, u16 reg)
  71. {
  72. u8 val;
  73. int ret;
  74. if (dev->state & DEV_DISCONNECTED)
  75. return(-ENODEV);
  76. em28xx_regdbg("req=%02x, reg=%02x:", req, reg);
  77. ret = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0), req,
  78. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  79. 0x0000, reg, &val, 1, HZ);
  80. if (reg_debug)
  81. printk(ret < 0 ? " failed!\n" :
  82. "%02x\n", (unsigned char) val);
  83. if (ret < 0)
  84. return ret;
  85. return val;
  86. }
  87. int em28xx_read_reg(struct em28xx *dev, u16 reg)
  88. {
  89. return em28xx_read_reg_req(dev, USB_REQ_GET_STATUS, reg);
  90. }
  91. /*
  92. * em28xx_write_regs_req()
  93. * sends data to the usb device, specifying bRequest
  94. */
  95. int em28xx_write_regs_req(struct em28xx *dev, u8 req, u16 reg, char *buf,
  96. int len)
  97. {
  98. int ret;
  99. /*usb_control_msg seems to expect a kmalloced buffer */
  100. unsigned char *bufs;
  101. if (dev->state & DEV_DISCONNECTED)
  102. return(-ENODEV);
  103. bufs = kmalloc(len, GFP_KERNEL);
  104. em28xx_regdbg("req=%02x reg=%02x:", req, reg);
  105. if (reg_debug) {
  106. int i;
  107. for (i = 0; i < len; ++i)
  108. printk (" %02x", (unsigned char)buf[i]);
  109. printk ("\n");
  110. }
  111. if (!bufs)
  112. return -ENOMEM;
  113. memcpy(bufs, buf, len);
  114. ret = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0), req,
  115. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  116. 0x0000, reg, bufs, len, HZ);
  117. msleep(5); /* FIXME: magic number */
  118. kfree(bufs);
  119. return ret;
  120. }
  121. int em28xx_write_regs(struct em28xx *dev, u16 reg, char *buf, int len)
  122. {
  123. return em28xx_write_regs_req(dev, USB_REQ_GET_STATUS, reg, buf, len);
  124. }
  125. /*
  126. * em28xx_write_reg_bits()
  127. * sets only some bits (specified by bitmask) of a register, by first reading
  128. * the actual value
  129. */
  130. static int em28xx_write_reg_bits(struct em28xx *dev, u16 reg, u8 val,
  131. u8 bitmask)
  132. {
  133. int oldval;
  134. u8 newval;
  135. if ((oldval = em28xx_read_reg(dev, reg)) < 0)
  136. return oldval;
  137. newval = (((u8) oldval) & ~bitmask) | (val & bitmask);
  138. return em28xx_write_regs(dev, reg, &newval, 1);
  139. }
  140. /*
  141. * em28xx_write_ac97()
  142. * write a 16 bit value to the specified AC97 address (LSB first!)
  143. */
  144. static int em28xx_write_ac97(struct em28xx *dev, u8 reg, u8 *val)
  145. {
  146. int ret, i;
  147. u8 addr = reg & 0x7f;
  148. if ((ret = em28xx_write_regs(dev, AC97LSB_REG, val, 2)) < 0)
  149. return ret;
  150. if ((ret = em28xx_write_regs(dev, AC97ADDR_REG, &addr, 1)) < 0)
  151. return ret;
  152. /* Wait up to 50 ms for AC97 command to complete */
  153. for (i = 0; i < 10; i++) {
  154. if ((ret = em28xx_read_reg(dev, AC97BUSY_REG)) < 0)
  155. return ret;
  156. if (!(ret & 0x01))
  157. return 0;
  158. msleep(5);
  159. }
  160. em28xx_warn ("AC97 command still being executed: not handled properly!\n");
  161. return 0;
  162. }
  163. static int em28xx_set_audio_source(struct em28xx *dev)
  164. {
  165. static char *enable = "\x08\x08";
  166. static char *disable = "\x08\x88";
  167. char *video = enable, *line = disable;
  168. int ret;
  169. u8 input;
  170. if (dev->is_em2800) {
  171. if (dev->ctl_ainput)
  172. input = EM2800_AUDIO_SRC_LINE;
  173. else
  174. input = EM2800_AUDIO_SRC_TUNER;
  175. ret = em28xx_write_regs(dev, EM2800_AUDIOSRC_REG, &input, 1);
  176. if (ret < 0)
  177. return ret;
  178. }
  179. if (dev->has_msp34xx)
  180. input = EM28XX_AUDIO_SRC_TUNER;
  181. else {
  182. switch (dev->ctl_ainput) {
  183. case EM28XX_AMUX_VIDEO:
  184. input = EM28XX_AUDIO_SRC_TUNER;
  185. break;
  186. case EM28XX_AMUX_LINE_IN:
  187. input = EM28XX_AUDIO_SRC_LINE;
  188. break;
  189. case EM28XX_AMUX_AC97_VIDEO:
  190. input = EM28XX_AUDIO_SRC_LINE;
  191. break;
  192. case EM28XX_AMUX_AC97_LINE_IN:
  193. input = EM28XX_AUDIO_SRC_LINE;
  194. video = disable;
  195. line = enable;
  196. break;
  197. }
  198. }
  199. ret = em28xx_write_reg_bits(dev, AUDIOSRC_REG, input, 0xc0);
  200. if (ret < 0)
  201. return ret;
  202. msleep(5);
  203. /* Sets AC97 mixer registers
  204. This is seems to be needed, even for non-ac97 configs
  205. */
  206. ret = em28xx_write_ac97(dev, VIDEO_AC97, video);
  207. if (ret < 0)
  208. return ret;
  209. ret = em28xx_write_ac97(dev, LINE_IN_AC97, line);
  210. return ret;
  211. }
  212. int em28xx_audio_analog_set(struct em28xx *dev)
  213. {
  214. int ret;
  215. char s[2] = { 0x00, 0x00 };
  216. u8 xclk = 0x07;
  217. s[0] |= 0x1f - dev->volume;
  218. s[1] |= 0x1f - dev->volume;
  219. /* Mute */
  220. s[1] |= 0x80;
  221. ret = em28xx_write_ac97(dev, MASTER_AC97, s);
  222. if (ret < 0)
  223. return ret;
  224. if (dev->has_12mhz_i2s)
  225. xclk |= 0x20;
  226. if (!dev->mute)
  227. xclk |= 0x80;
  228. ret = em28xx_write_reg_bits(dev, XCLK_REG, xclk, 0xa7);
  229. if (ret < 0)
  230. return ret;
  231. msleep(10);
  232. /* Selects the proper audio input */
  233. ret = em28xx_set_audio_source(dev);
  234. /* Unmute device */
  235. if (!dev->mute)
  236. s[1] &= ~0x80;
  237. ret = em28xx_write_ac97(dev, MASTER_AC97, s);
  238. return ret;
  239. }
  240. EXPORT_SYMBOL_GPL(em28xx_audio_analog_set);
  241. int em28xx_colorlevels_set_default(struct em28xx *dev)
  242. {
  243. em28xx_write_regs(dev, YGAIN_REG, "\x10", 1); /* contrast */
  244. em28xx_write_regs(dev, YOFFSET_REG, "\x00", 1); /* brightness */
  245. em28xx_write_regs(dev, UVGAIN_REG, "\x10", 1); /* saturation */
  246. em28xx_write_regs(dev, UOFFSET_REG, "\x00", 1);
  247. em28xx_write_regs(dev, VOFFSET_REG, "\x00", 1);
  248. em28xx_write_regs(dev, SHARPNESS_REG, "\x00", 1);
  249. em28xx_write_regs(dev, GAMMA_REG, "\x20", 1);
  250. em28xx_write_regs(dev, RGAIN_REG, "\x20", 1);
  251. em28xx_write_regs(dev, GGAIN_REG, "\x20", 1);
  252. em28xx_write_regs(dev, BGAIN_REG, "\x20", 1);
  253. em28xx_write_regs(dev, ROFFSET_REG, "\x00", 1);
  254. em28xx_write_regs(dev, GOFFSET_REG, "\x00", 1);
  255. return em28xx_write_regs(dev, BOFFSET_REG, "\x00", 1);
  256. }
  257. int em28xx_capture_start(struct em28xx *dev, int start)
  258. {
  259. int rc;
  260. /* FIXME: which is the best order? */
  261. /* video registers are sampled by VREF */
  262. rc = em28xx_write_reg_bits(dev, USBSUSP_REG,
  263. start ? 0x10 : 0x00, 0x10);
  264. if (rc < 0)
  265. return rc;
  266. if (!start) {
  267. /* disable video capture */
  268. rc = em28xx_write_regs(dev, VINENABLE_REG, "\x27", 1);
  269. if (rc < 0)
  270. return rc;
  271. }
  272. /* enable video capture */
  273. rc = em28xx_write_regs_req(dev, 0x00, 0x48, "\x00", 1);
  274. if (rc < 0)
  275. return rc;
  276. if (dev->mode == EM28XX_ANALOG_MODE)
  277. rc = em28xx_write_regs(dev, VINENABLE_REG,"\x67", 1);
  278. else
  279. rc = em28xx_write_regs(dev, VINENABLE_REG,"\x37", 1);
  280. if (rc < 0)
  281. return rc;
  282. msleep (6);
  283. return rc;
  284. }
  285. int em28xx_outfmt_set_yuv422(struct em28xx *dev)
  286. {
  287. em28xx_write_regs(dev, OUTFMT_REG, "\x34", 1);
  288. em28xx_write_regs(dev, VINMODE_REG, "\x10", 1);
  289. return em28xx_write_regs(dev, VINCTRL_REG, "\x11", 1);
  290. }
  291. static int em28xx_accumulator_set(struct em28xx *dev, u8 xmin, u8 xmax,
  292. u8 ymin, u8 ymax)
  293. {
  294. em28xx_coredbg("em28xx Scale: (%d,%d)-(%d,%d)\n", xmin, ymin, xmax, ymax);
  295. em28xx_write_regs(dev, XMIN_REG, &xmin, 1);
  296. em28xx_write_regs(dev, XMAX_REG, &xmax, 1);
  297. em28xx_write_regs(dev, YMIN_REG, &ymin, 1);
  298. return em28xx_write_regs(dev, YMAX_REG, &ymax, 1);
  299. }
  300. static int em28xx_capture_area_set(struct em28xx *dev, u8 hstart, u8 vstart,
  301. u16 width, u16 height)
  302. {
  303. u8 cwidth = width;
  304. u8 cheight = height;
  305. u8 overflow = (height >> 7 & 0x02) | (width >> 8 & 0x01);
  306. em28xx_coredbg("em28xx Area Set: (%d,%d)\n", (width | (overflow & 2) << 7),
  307. (height | (overflow & 1) << 8));
  308. em28xx_write_regs(dev, HSTART_REG, &hstart, 1);
  309. em28xx_write_regs(dev, VSTART_REG, &vstart, 1);
  310. em28xx_write_regs(dev, CWIDTH_REG, &cwidth, 1);
  311. em28xx_write_regs(dev, CHEIGHT_REG, &cheight, 1);
  312. return em28xx_write_regs(dev, OFLOW_REG, &overflow, 1);
  313. }
  314. static int em28xx_scaler_set(struct em28xx *dev, u16 h, u16 v)
  315. {
  316. u8 mode;
  317. /* the em2800 scaler only supports scaling down to 50% */
  318. if(dev->is_em2800)
  319. mode = (v ? 0x20 : 0x00) | (h ? 0x10 : 0x00);
  320. else {
  321. u8 buf[2];
  322. buf[0] = h;
  323. buf[1] = h >> 8;
  324. em28xx_write_regs(dev, HSCALELOW_REG, (char *)buf, 2);
  325. buf[0] = v;
  326. buf[1] = v >> 8;
  327. em28xx_write_regs(dev, VSCALELOW_REG, (char *)buf, 2);
  328. /* it seems that both H and V scalers must be active to work correctly */
  329. mode = (h || v)? 0x30: 0x00;
  330. }
  331. return em28xx_write_reg_bits(dev, COMPR_REG, mode, 0x30);
  332. }
  333. /* FIXME: this only function read values from dev */
  334. int em28xx_resolution_set(struct em28xx *dev)
  335. {
  336. int width, height;
  337. width = norm_maxw(dev);
  338. height = norm_maxh(dev) >> 1;
  339. em28xx_outfmt_set_yuv422(dev);
  340. em28xx_accumulator_set(dev, 1, (width - 4) >> 2, 1, (height - 4) >> 2);
  341. em28xx_capture_area_set(dev, 0, 0, width >> 2, height >> 2);
  342. return em28xx_scaler_set(dev, dev->hscale, dev->vscale);
  343. }
  344. int em28xx_set_alternate(struct em28xx *dev)
  345. {
  346. int errCode, prev_alt = dev->alt;
  347. int i;
  348. unsigned int min_pkt_size = dev->width * 2 + 4;
  349. /* When image size is bigger than a certain value,
  350. the frame size should be increased, otherwise, only
  351. green screen will be received.
  352. */
  353. if (dev->width * 2 * dev->height > 720 * 240 * 2)
  354. min_pkt_size *= 2;
  355. for (i = 0; i < dev->num_alt; i++) {
  356. /* stop when the selected alt setting offers enough bandwidth */
  357. if (dev->alt_max_pkt_size[i] >= min_pkt_size) {
  358. dev->alt = i;
  359. break;
  360. /* otherwise make sure that we end up with the maximum bandwidth
  361. because the min_pkt_size equation might be wrong...
  362. */
  363. } else if (dev->alt_max_pkt_size[i] >
  364. dev->alt_max_pkt_size[dev->alt])
  365. dev->alt = i;
  366. }
  367. if (dev->alt != prev_alt) {
  368. em28xx_coredbg("minimum isoc packet size: %u (alt=%d)\n",
  369. min_pkt_size, dev->alt);
  370. dev->max_pkt_size = dev->alt_max_pkt_size[dev->alt];
  371. em28xx_coredbg("setting alternate %d with wMaxPacketSize=%u\n",
  372. dev->alt, dev->max_pkt_size);
  373. errCode = usb_set_interface(dev->udev, 0, dev->alt);
  374. if (errCode < 0) {
  375. em28xx_errdev ("cannot change alternate number to %d (error=%i)\n",
  376. dev->alt, errCode);
  377. return errCode;
  378. }
  379. }
  380. return 0;
  381. }